xref: /aosp_15_r20/external/ltp/testcases/kernel/fs/fs_fill/fs_fill.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Cyril Hrubis <[email protected]>
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 #define THREADS_DIR MNTPOINT "/subdir"
22 
23 static volatile int run;
24 static unsigned int nthreads;
25 static int enospc_cnt;
26 static struct worker *workers;
27 
28 struct worker {
29 	enum tst_fill_access_pattern pattern;
30 	char dir[PATH_MAX];
31 };
32 
worker(void * p)33 static void *worker(void *p)
34 {
35 	struct worker *w = p;
36 	DIR *d;
37 	struct dirent *ent;
38 	char file[PATH_MAX];
39 
40 	while (run) {
41 		tst_fill_fs(w->dir, 1, w->pattern);
42 
43 		tst_atomic_inc(&enospc_cnt);
44 
45 		d = SAFE_OPENDIR(w->dir);
46 		while ((ent = SAFE_READDIR(d))) {
47 
48 			if (!strcmp(ent->d_name, ".") ||
49 			    !strcmp(ent->d_name, ".."))
50 				continue;
51 
52 			snprintf(file, sizeof(file), "%s/%s",
53 				 w->dir, ent->d_name);
54 
55 			tst_res(TINFO, "Unlinking %s", file);
56 
57 			SAFE_UNLINK(file);
58 			break;
59 		}
60 		SAFE_CLOSEDIR(d);
61 	}
62 
63 	return NULL;
64 }
65 
testrun(unsigned int n)66 static void testrun(unsigned int n)
67 {
68 	pthread_t threads[nthreads];
69 	unsigned int i, ms;
70 
71 	tst_atomic_store(0, &enospc_cnt);
72 
73 	run = 1;
74 	for (i = 0; i < nthreads; i++) {
75 		workers[i].pattern = n;
76 		SAFE_PTHREAD_CREATE(&threads[i], NULL, worker, &workers[i]);
77 	}
78 
79 	for (ms = 0; ; ms++) {
80 		usleep(1000);
81 
82 		if (ms >= 1000 && tst_atomic_load(&enospc_cnt))
83 			break;
84 
85 		if (tst_atomic_load(&enospc_cnt) > 100)
86 			break;
87 	}
88 
89 	run = 0;
90 	for (i = 0; i < nthreads; i++)
91 		SAFE_PTHREAD_JOIN(threads[i], NULL);
92 
93 	tst_res(TPASS, "Got %i ENOSPC runtime %ims", enospc_cnt, ms);
94 }
95 
setup(void)96 static void setup(void)
97 {
98 	unsigned int i;
99 
100 	nthreads = tst_ncpus_conf() + 2;
101 	workers = SAFE_MALLOC(sizeof(struct worker) * nthreads);
102 
103 	/*
104 	 * Avoid creating the thread directories in the root of the filesystem
105 	 * to not hit the root entries limit on a FAT16 filesystem.
106 	 */
107 	SAFE_MKDIR(THREADS_DIR, 0700);
108 
109 	for (i = 0; i < nthreads; i++) {
110 		snprintf(workers[i].dir, sizeof(workers[i].dir),
111 			 THREADS_DIR "/thread%i", i + 1);
112 		SAFE_MKDIR(workers[i].dir, 0700);
113 	}
114 
115 	tst_res(TINFO, "Running %i writer threads", nthreads);
116 }
117 
cleanup(void)118 static void cleanup(void)
119 {
120 	free(workers);
121 }
122 
123 static struct tst_test test = {
124 	.max_runtime = 300,
125 	.needs_root = 1,
126 	.dev_min_size = 1024,
127 	.mount_device = 1,
128 	.mntpoint = MNTPOINT,
129 	.all_filesystems = 1,
130 	.setup = setup,
131 	.cleanup = cleanup,
132 	.test = testrun,
133 	.tcnt = 2
134 };
135