forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-chattr.c
246 lines (212 loc) · 6.8 KB
/
stress-chattr.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
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
static const stress_help_t help[] = {
{ NULL, "chattr N", "start N workers thrashing chattr file mode bits " },
{ NULL, "chattr-ops N", "stop chattr workers after N bogo operations" },
{ NULL, NULL, NULL }
};
#if defined(__linux__)
#define SHIM_EXT2_SECRM_FL 0x00000001 /* Secure deletion */
#define SHIM_EXT2_UNRM_FL 0x00000002 /* Undelete */
#define SHIM_EXT2_COMPR_FL 0x00000004 /* Compress file */
#define SHIM_EXT2_SYNC_FL 0x00000008 /* Synchronous updates */
#define SHIM_EXT2_IMMUTABLE_FL 0x00000010 /* Immutable file */
#define SHIM_EXT2_APPEND_FL 0x00000020 /* Writes to file may only append */
#define SHIM_EXT2_NODUMP_FL 0x00000040 /* Do not dump file */
#define SHIM_EXT2_NOATIME_FL 0x00000080 /* Do not update atime */
#define SHIM_EXT3_JOURNAL_DATA_FL 0x00004000 /* File data should be journaled */
#define SHIM_EXT2_NOTAIL_FL 0x00008000 /* File tail should not be merged */
#define SHIM_EXT2_DIRSYNC_FL 0x00010000 /* Synchronous directory modifications */
#define SHIM_EXT2_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
#define SHIM_EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
#define SHIM_FS_NOCOW_FL 0x00800000 /* Do not cow file */
#define SHIM_EXT4_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
#define SHIM_EXT2_IOC_GETFLAGS _IOR('f', 1, long)
#define SHIM_EXT2_IOC_SETFLAGS _IOW('f', 2, long)
static const unsigned int flags[] = {
SHIM_EXT2_NOATIME_FL, /* chattr 'A' */
SHIM_EXT2_SYNC_FL, /* chattr 'S' */
SHIM_EXT2_DIRSYNC_FL, /* chattr 'D' */
SHIM_EXT2_APPEND_FL, /* chattr 'a' */
SHIM_EXT2_COMPR_FL, /* chattr 'c' */
SHIM_EXT2_NODUMP_FL, /* chattr 'd' */
SHIM_EXT4_EXTENTS_FL, /* chattr 'e' */
SHIM_EXT2_IMMUTABLE_FL, /* chattr 'i' */
SHIM_EXT3_JOURNAL_DATA_FL, /* chattr 'j' */
SHIM_EXT4_PROJINHERIT_FL, /* chattr 'P' */
SHIM_EXT2_SECRM_FL, /* chattr 's' */
SHIM_EXT2_UNRM_FL, /* chattr 'u' */
SHIM_EXT2_NOTAIL_FL, /* chattr 't' */
SHIM_EXT2_TOPDIR_FL, /* chattr 'T' */
SHIM_FS_NOCOW_FL, /* chattr 'C' */
};
/*
* do_chattr()
*/
static int do_chattr(
const stress_args_t *args,
const char *filename,
const unsigned long flag)
{
int i;
int rc = 0;
for (i = 0; (i < 128) && keep_stressing(args); i++) {
int fd, fdw, ret;
unsigned long zero = 0UL;
unsigned long orig, rnd;
fd = open(filename, O_RDONLY | O_NONBLOCK | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0)
continue;
if (!keep_stressing(args))
goto tidy_fd;
ret = ioctl(fd, SHIM_EXT2_IOC_GETFLAGS, &orig);
if (ret < 0) {
if ((errno != EOPNOTSUPP) &&
(errno != ENOTTY) &&
(errno != EINVAL))
pr_inf("%s: ioctl SHIM_EXT2_IOC_GETFLAGS failed: errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto tidy_fd;
}
if (!keep_stressing(args))
goto tidy_fd;
ret = ioctl(fd, SHIM_EXT2_IOC_SETFLAGS, &zero);
if (ret < 0) {
rc = -1;
if ((errno != EOPNOTSUPP) &&
(errno != ENOTTY) &&
(errno != EINVAL))
pr_inf("%s: ioctl SHIM_EXT2_IOC_SETFLAGS failed: errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto tidy_fd;
}
if (!keep_stressing(args))
goto tidy_fd;
fdw = open(filename, O_RDWR);
if (fdw < 0)
goto tidy_fd;
VOID_RET(ssize_t, write(fdw, &zero, sizeof(zero)));
if (!keep_stressing(args))
goto tidy_fdw;
ret = ioctl(fd, SHIM_EXT2_IOC_SETFLAGS, &flag);
if ((ret < 0) &&
((errno == EOPNOTSUPP) ||
(errno == ENOTTY) ||
(errno == EINTR)))
rc = -1;
if (!keep_stressing(args))
goto tidy_fdw;
VOID_RET(ssize_t, write(fdw, &zero, sizeof(zero)));
if (!keep_stressing(args))
goto tidy_fdw;
VOID_RET(int, ioctl(fd, SHIM_EXT2_IOC_SETFLAGS, &zero));
/*
* Try some random flag, exercises any illegal flags
*/
if (!keep_stressing(args))
goto tidy_fdw;
rnd = 1ULL << (stress_mwc8() & 0x1f);
VOID_RET(int, ioctl(fd, SHIM_EXT2_IOC_SETFLAGS, &rnd));
/*
* Restore original setting
*/
if (!keep_stressing(args))
goto tidy_fdw;
VOID_RET(int, ioctl(fd, SHIM_EXT2_IOC_SETFLAGS, &orig));
tidy_fdw:
(void)close(fdw);
tidy_fd:
(void)close(fd);
(void)shim_unlink(filename);
return rc;
}
return rc;
}
/*
* stress_chattr
* stress chattr
*/
static int stress_chattr(const stress_args_t *args)
{
const pid_t ppid = getppid();
int rc = EXIT_SUCCESS;
char filename[PATH_MAX], pathname[PATH_MAX];
int all_flags, *flag_perms;
size_t i, index, flag_count;
for (all_flags = 0, i = 0; i < SIZEOF_ARRAY(flags); i++)
all_flags |= flags[i];
flag_count = stress_flag_permutation(all_flags, &flag_perms);
/*
* Allow for multiple workers to chattr the *same* file
*/
stress_temp_dir(pathname, sizeof(pathname), args->name, ppid, 0);
if (mkdir(pathname, S_IRUSR | S_IRWXU) < 0) {
if (errno != EEXIST) {
rc = exit_status(errno);
pr_fail("%s: mkdir of %s failed, errno=%d (%s)\n",
args->name, pathname, errno, strerror(errno));
return rc;
}
}
(void)stress_temp_filename(filename, sizeof(filename),
args->name, ppid, 0, 0);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
index = 0;
do {
size_t fail = 0;
for (i = 0; i < SIZEOF_ARRAY(flags); i++) {
if (do_chattr(args, filename, (unsigned long)flags[i]) < 0)
fail++;
}
if (fail == i) {
if (args->instance == 0)
pr_inf_skip("%s: chattr not supported on filesystem, skipping stressor\n",
args->name);
rc = EXIT_NOT_IMPLEMENTED;
break;
}
/* Try next flag permutation */
if ((flag_count > 0) && (flag_perms)) {
(void)do_chattr(args, filename, (unsigned long)flag_perms[index]);
index++;
index %= flag_count;
}
inc_counter(args);
} while (keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
(void)shim_unlink(filename);
(void)shim_rmdir(pathname);
if (flag_perms)
free(flag_perms);
return rc;
}
stressor_info_t stress_chattr_info = {
.stressor = stress_chattr,
.class = CLASS_FILESYSTEM | CLASS_OS,
.help = help
};
#else
stressor_info_t stress_chattr_info = {
.stressor = stress_not_implemented,
.class = CLASS_FILESYSTEM | CLASS_OS,
.help = help
};
#endif