forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-filename.c
341 lines (297 loc) · 8.2 KB
/
stress-filename.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (C) 2013-2017 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"
#include <sys/statvfs.h>
#define STRESS_FILENAME_PROBE (0) /* Default */
#define STRESS_FILENAME_POSIX (1) /* POSIX 2008.1 */
#define STRESS_FILENAME_EXT (2) /* EXT* filesystems */
typedef struct {
uint8_t opt;
const char *opt_text;
} filename_opts_t;
static const filename_opts_t filename_opts[] = {
{ STRESS_FILENAME_PROBE, "probe" },
{ STRESS_FILENAME_POSIX, "posix" },
{ STRESS_FILENAME_EXT, "ext" },
{ -1, NULL }
};
/* Allowed filename characters */
static char allowed[256];
/*
* The Open Group Base Specifications Issue 7
* POSIX.1-2008, 3.278 Portable Filename Character Set
*/
static char posix_allowed[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789._-";
int stress_filename_opts(const char *opt)
{
size_t i;
for (i = 0; filename_opts[i].opt_text; i++) {
if (!strcmp(opt, filename_opts[i].opt_text)) {
uint8_t filename_opt = filename_opts[i].opt;
set_setting("filename-opts", TYPE_ID_UINT8, &filename_opt);
return 0;
}
}
(void)fprintf(stderr, "filename-opts option '%s' not known, options are:", opt);
for (i = 0; filename_opts[i].opt_text; i++)
(void)fprintf(stderr, "%s %s",
i == 0 ? "" : ",", filename_opts[i].opt_text);
(void)fprintf(stderr, "\n");
return -1;
}
/*
* stress_filename_probe()
* determine allowed filename chars by probing
*/
static int stress_filename_probe(
const args_t *args,
char *filename,
char *ptr,
size_t *chars_allowed)
{
size_t i, j;
/*
* Determine allowed char set for filenames
*/
for (j = 0, i = 0; i < 256; i++) {
int fd;
if ((i == 0) || (i == '/'))
continue;
*ptr = i;
*(ptr + 1) = 'X';
*(ptr + 2) = '\0';
if ((fd = creat(filename, S_IRUSR | S_IWUSR)) < 0) {
/* We only expect EINVAL on bad filenames */
if (errno != EINVAL) {
pr_err("%s: creat() failed when probing "
"for allowed filename characters, "
"errno = %d (%s)\n",
args->name, errno, strerror(errno));
pr_inf("%s: perhaps retry and use "
"--filename-opts posix\n", args->name);
*chars_allowed = 0;
return -errno;
}
} else {
(void)close(fd);
(void)unlink(filename);
allowed[j] = i;
j++;
}
}
*chars_allowed = j;
return 0;
}
/*
* stress_filename_ext()
* determine allowed for ext* filesystems
*/
static void stress_filename_ext(size_t *chars_allowed)
{
size_t i, j;
for (j = 0, i = 0; i < 256; i++) {
if ((i == 0) || (i == '/'))
continue;
allowed[j] = i;
j++;
}
*chars_allowed = j;
}
/*
* stress_filename_generate()
* generate a filename of length sz_max
*/
static void stress_filename_generate(
char *filename,
const size_t sz_max,
const char ch)
{
size_t i;
for (i = 0; i < sz_max; i++) {
filename[i] = ch;
}
if (*filename == '.')
*filename = '_';
filename[i] = '\0';
}
/*
* stress_filename_generate_random()
* generate a filename of length sz_max with
* random selection from possible char set
*/
static void stress_filename_generate_random(
char *filename,
const size_t sz_max,
const size_t chars_allowed)
{
size_t i;
for (i = 0; i < sz_max; i++) {
const int j = mwc32() % chars_allowed;
filename[i] = allowed[j];
}
if (*filename == '.')
*filename = '_';
filename[i] = '\0';
}
/*
* stress_filename_test()
* create a file, and check if it fails.
* should_pass = true - create must pass
* should_pass = false - expect it to fail (name too long)
*/
static void stress_filename_test(
const args_t *args,
const char *filename,
const size_t sz_max,
const bool should_pass)
{
int fd;
if ((fd = creat(filename, S_IRUSR | S_IWUSR)) < 0) {
if ((!should_pass) && (errno == ENAMETOOLONG))
return;
pr_fail("%s: open failed on file of length "
"%zu bytes, errno=%d (%s)\n",
args->name, sz_max, errno, strerror(errno));
} else {
(void)close(fd);
(void)unlink(filename);
}
}
/*
* stress_filename()
* stress filename sizes etc
*/
int stress_filename (const args_t *args)
{
int ret, rc = EXIT_FAILURE;
size_t sz_left, sz_max;
char dirname[PATH_MAX];
char filename[PATH_MAX];
char *ptr;
struct statvfs buf;
size_t i, chars_allowed = 0, sz;
uint8_t filename_opt = STRESS_FILENAME_PROBE;
(void)get_setting("filename-opts", &filename_opt);
stress_temp_dir_args(args, dirname, sizeof(dirname));
if (mkdir(dirname, S_IRWXU) < 0) {
if (errno != EEXIST) {
pr_fail_err("mkdir");
return EXIT_FAILURE;
}
}
if (statvfs(dirname, &buf) < 0) {
pr_fail_err("statvfs");
goto tidy_dir;
}
if (args->instance == 0)
pr_dbg("%s: maximum file size: %lu bytes\n",
args->name, (long unsigned) buf.f_namemax);
(void)strncpy(filename, dirname, sizeof(filename) - 1);
ptr = filename + strlen(dirname);
*(ptr++) = '/';
*(ptr) = '\0';
sz_left = sizeof(filename) - (ptr - filename);
sz_max = (size_t)buf.f_namemax;
if (sz_left >= PATH_MAX) {
pr_fail("%s: max file name larger than PATH_MAX\n", args->name);
goto tidy_dir;
}
switch (filename_opt) {
case STRESS_FILENAME_POSIX:
(void)strncpy(allowed, posix_allowed, sizeof(allowed));
chars_allowed = strlen(allowed);
break;
case STRESS_FILENAME_EXT:
stress_filename_ext(&chars_allowed);
break;
case STRESS_FILENAME_PROBE:
default:
ret = stress_filename_probe(args, filename, ptr, &chars_allowed);
if (ret < 0) {
rc = exit_status(-ret);
goto tidy_dir;
}
break;
}
if (args->instance == 0)
pr_dbg("%s: filesystem allows %zu unique "
"characters in a filename\n",
args->name, chars_allowed);
if (chars_allowed == 0) {
pr_fail("%s: cannot determine allowed characters "
"in a filename\n", args->name);
goto tidy_dir;
}
i = 0;
sz = 1;
do {
char ch = allowed[i];
size_t rnd_sz = 1 + (mwc32() % sz_max);
i++;
if (i >= chars_allowed)
i = 0;
/* Should succeed */
stress_filename_generate(ptr, 1, ch);
stress_filename_test(args, filename, 1, true);
stress_filename_generate_random(ptr, 1, chars_allowed);
stress_filename_test(args, filename, 1, true);
/* Should succeed */
stress_filename_generate(ptr, sz_max, ch);
stress_filename_test(args, filename, sz_max, true);
stress_filename_generate_random(ptr, sz_max, chars_allowed);
stress_filename_test(args, filename, sz_max, true);
/* Should succeed */
stress_filename_generate(ptr, sz_max - 1, ch);
stress_filename_test(args, filename, sz_max - 1, true);
stress_filename_generate_random(ptr, sz_max - 1, chars_allowed);
stress_filename_test(args, filename, sz_max - 1, true);
/* Should fail */
stress_filename_generate(ptr, sz_max + 1, ch);
stress_filename_test(args, filename, sz_max + 1, false);
stress_filename_generate_random(ptr, sz_max + 1, chars_allowed);
stress_filename_test(args, filename, sz_max + 1, false);
/* Should succeed */
stress_filename_generate(ptr, sz, ch);
stress_filename_test(args, filename, sz, true);
stress_filename_generate_random(ptr, sz, chars_allowed);
stress_filename_test(args, filename, sz, true);
/* Should succeed */
stress_filename_generate(ptr, rnd_sz, ch);
stress_filename_test(args, filename, rnd_sz, true);
stress_filename_generate_random(ptr, rnd_sz, chars_allowed);
stress_filename_test(args, filename, rnd_sz, true);
sz++;
if (sz > sz_max)
sz = 1;
inc_counter(args);
} while (keep_stressing());
rc = EXIT_SUCCESS;
tidy_dir:
(void)rmdir(dirname);
return rc;
}