1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/statvfs.h>
10
11 #define TST_NO_DEFAULT_MAIN
12 #include "tst_test.h"
13 #include "tst_fs.h"
14
tst_fill_fs(const char * path,int verbose)15 void tst_fill_fs(const char *path, int verbose)
16 {
17 int i = 0;
18 char file[PATH_MAX];
19 char buf[4096];
20 size_t len;
21 ssize_t ret;
22 int fd;
23 struct statvfs fi;
24 statvfs(path, &fi);
25
26 for (;;) {
27 len = random() % (1024 * 102400);
28
29 snprintf(file, sizeof(file), "%s/file%i", path, i++);
30
31 if (verbose)
32 tst_res(TINFO, "Creating file %s size %zu", file, len);
33
34 fd = open(file, O_WRONLY | O_CREAT, 0700);
35 if (fd == -1) {
36 if (errno != ENOSPC)
37 tst_brk(TBROK | TERRNO, "open()");
38
39 tst_res(TINFO | TERRNO, "open()");
40 return;
41 }
42
43 while (len) {
44 ret = write(fd, buf, MIN(len, sizeof(buf)));
45
46 if (ret < 0) {
47 /* retry on ENOSPC to make sure filesystem is really full */
48 if (errno == ENOSPC && len >= fi.f_bsize/2) {
49 SAFE_FSYNC(fd);
50 len /= 2;
51 continue;
52 }
53
54 SAFE_CLOSE(fd);
55
56 if (errno != ENOSPC)
57 tst_brk(TBROK | TERRNO, "write()");
58
59 tst_res(TINFO | TERRNO, "write()");
60 return;
61 }
62
63 len -= ret;
64 }
65
66 SAFE_CLOSE(fd);
67 }
68 }
69