1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2017 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6 /*
7 * Runs several threads that fills up the filesystem repeatedly.
8 */
9
10 #define _GNU_SOURCE
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <pthread.h>
17 #include "tst_safe_pthread.h"
18 #include "tst_test.h"
19
20 #define MNTPOINT "mntpoint"
21
22 static volatile int run;
23 static unsigned int nthreads;
24 static int enospc_cnt;
25 static struct worker *workers;
26
27 struct worker {
28 char dir[PATH_MAX];
29 };
30
worker(void * p)31 static void *worker(void *p)
32 {
33 struct worker *w = p;
34 DIR *d;
35 struct dirent *ent;
36 char file[PATH_MAX];
37
38 while (run) {
39 tst_fill_fs(w->dir, 0);
40
41 tst_atomic_inc(&enospc_cnt);
42
43 d = SAFE_OPENDIR(w->dir);
44 while ((ent = SAFE_READDIR(d))) {
45
46 if (!strcmp(ent->d_name, ".") ||
47 !strcmp(ent->d_name, ".."))
48 continue;
49
50 snprintf(file, sizeof(file), "%s/%s",
51 w->dir, ent->d_name);
52
53 tst_res(TINFO, "Unlinking %s", file);
54
55 SAFE_UNLINK(file);
56 break;
57 }
58 SAFE_CLOSEDIR(d);
59 }
60
61 return NULL;
62 }
63
testrun(void)64 static void testrun(void)
65 {
66 pthread_t threads[nthreads];
67 unsigned int i, ms;
68
69 run = 1;
70 for (i = 0; i < nthreads; i++)
71 SAFE_PTHREAD_CREATE(&threads[i], NULL, worker, &workers[i]);
72
73 for (ms = 0; ; ms++) {
74 usleep(1000);
75
76 if (ms >= 1000 && enospc_cnt)
77 break;
78
79 if (enospc_cnt > 100)
80 break;
81 }
82
83 run = 0;
84 for (i = 0; i < nthreads; i++)
85 SAFE_PTHREAD_JOIN(threads[i], NULL);
86
87 tst_res(TPASS, "Got %i ENOSPC runtime %ims", enospc_cnt, ms);
88 }
89
setup(void)90 static void setup(void)
91 {
92 unsigned int i;
93
94 nthreads = tst_ncpus_conf() + 2;
95 workers = SAFE_MALLOC(sizeof(struct worker) * nthreads);
96
97 for (i = 0; i < nthreads; i++) {
98 snprintf(workers[i].dir, sizeof(workers[i].dir),
99 MNTPOINT "/thread%i", i + 1);
100 SAFE_MKDIR(workers[i].dir, 0700);
101 }
102
103 tst_res(TINFO, "Running %i writer threads", nthreads);
104 }
105
cleanup(void)106 static void cleanup(void)
107 {
108 free(workers);
109 }
110
111 static struct tst_test test = {
112 .needs_root = 1,
113 .mount_device = 1,
114 .mntpoint = MNTPOINT,
115 .all_filesystems = 1,
116 .setup = setup,
117 .cleanup = cleanup,
118 .test_all = testrun,
119 };
120