-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstress-getdent.c
260 lines (223 loc) · 5.55 KB
/
stress-getdent.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Copyright (C) 2013-2018 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(__linux__) && defined(__NR_getdents64)
#define BUF_SIZE (256 * 1024)
typedef int (getdents_func)(
const args_t *args,
const char *path,
const bool recurse,
const int depth,
const size_t page_size);
#if defined(__NR_getdents)
static getdents_func stress_getdents_dir;
#endif
#if defined(__NR_getdents64)
static getdents_func stress_getdents64_dir;
#endif
static getdents_func * getdents_funcs[] = {
#if defined(__NR_getdents)
stress_getdents_dir,
#endif
#if defined(__NR_getdents64)
stress_getdents64_dir,
#endif
};
static inline int stress_getdents_rand(
const args_t *args,
const char *path,
const bool recurse,
const int depth,
const size_t page_size)
{
int ret = -ENOSYS;
const size_t n = SIZEOF_ARRAY(getdents_funcs);
size_t i, j = mwc32() % n;
for (i = 0; i < n; i++) {
getdents_func *func = getdents_funcs[j];
if (func) {
ret = func(args, path, recurse, depth, page_size);
if (ret == -ENOSYS)
getdents_funcs[j] = NULL;
else
return ret;
}
j++;
j = j % n;
}
pr_fail("%s: getdents: errno=%d (%s)\n",
args->name, -ret, strerror(-ret));
return ret;
}
#if defined(__NR_getdents)
/*
* stress_getdents_dir()
* read directory via the old 32 bit interface
*/
static int stress_getdents_dir(
const args_t *args,
const char *path,
const bool recurse,
const int depth,
const size_t page_size)
{
int fd, rc = 0;
char *buf;
size_t buf_sz;
if (!keep_stressing())
return 0;
fd = open(path, O_RDONLY | O_DIRECTORY);
if (fd < 0)
return 0;
buf_sz = ((mwc32() % BUF_SIZE) + page_size) & ~(page_size - 1);
buf = malloc(buf_sz);
if (!buf)
goto exit_close;
do {
int nread;
char *ptr = buf;
nread = shim_getdents(fd, (struct shim_linux_dirent *)buf, buf_sz);
if (nread < 0) {
rc = -errno;
goto exit_free;
}
if (nread == 0)
break;
inc_counter(args);
if (!recurse || depth < 1)
continue;
while (ptr < buf + nread) {
struct shim_linux_dirent *d = (struct shim_linux_dirent *)ptr;
unsigned char d_type = *(ptr + d->d_reclen - 1);
if (d_type == DT_DIR &&
strcmp(d->d_name, ".") &&
strcmp(d->d_name, "..")) {
char newpath[PATH_MAX];
(void)snprintf(newpath, sizeof(newpath), "%s/%s", path, d->d_name);
rc = stress_getdents_rand(args, newpath, recurse, depth - 1, page_size);
if (rc < 0)
goto exit_free;
}
ptr += d->d_reclen;
}
} while (keep_stressing());
exit_free:
free(buf);
exit_close:
(void)close(fd);
return rc;
}
#endif
#if defined(__NR_getdents64)
/*
* stress_getdents64_dir()
* read directory via the 64 bit interface
*/
static int stress_getdents64_dir(
const args_t *args,
const char *path,
const bool recurse,
const int depth,
const size_t page_size)
{
int fd, rc = 0;
char *buf;
size_t buf_sz;
if (!keep_stressing())
return 0;
fd = open(path, O_RDONLY | O_DIRECTORY);
if (fd < 0)
return 0;
buf_sz = ((mwc32() % BUF_SIZE) + page_size) & ~(page_size - 1);
buf = malloc(buf_sz);
if (!buf)
goto exit_close;
do {
int nread;
char *ptr = buf;
nread = shim_getdents64(fd, (struct shim_linux_dirent64 *)buf, buf_sz);
if (nread < 0) {
rc = -errno;
goto exit_free;
}
if (nread == 0)
break;
inc_counter(args);
if (!recurse || depth < 1)
continue;
while (ptr < buf + nread) {
struct shim_linux_dirent64 *d = (struct shim_linux_dirent64 *)ptr;
if (d->d_type == DT_DIR &&
strcmp(d->d_name, ".") &&
strcmp(d->d_name, "..")) {
char newpath[PATH_MAX];
(void)snprintf(newpath, sizeof(newpath), "%s/%s", path, d->d_name);
rc = stress_getdents_rand(args, newpath, recurse, depth - 1, page_size);
if (rc < 0)
goto exit_free;
}
ptr += d->d_reclen;
}
} while (keep_stressing());
exit_free:
free(buf);
exit_close:
(void)close(fd);
return rc;
}
#endif
/*
* stress_getdent
* stress reading directories
*/
int stress_getdent(const args_t *args)
{
const size_t page_size = args->page_size;
do {
int ret;
ret = stress_getdents_rand(args, "/proc", true, 8, page_size);
if (ret == -ENOSYS)
break;
ret = stress_getdents_rand(args, "/dev", true, 1, page_size);
if (ret == -ENOSYS)
break;
ret = stress_getdents_rand(args, "/tmp", true, 4, page_size);
if (ret == -ENOSYS)
break;
ret = stress_getdents_rand(args, "/sys", true, 8, page_size);
if (ret == -ENOSYS)
break;
ret = stress_getdents_rand(args, "/run", true, 2, page_size);
if (ret == -ENOSYS)
break;
} while (keep_stressing());
return EXIT_SUCCESS;
}
#else
int stress_getdent(const args_t *args)
{
return stress_not_implemented(args);
}
#endif