forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-link.c
277 lines (240 loc) · 7.33 KB
/
stress-link.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
/*
* 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"
#if defined(HAVE_LIBGEN_H)
#include <libgen.h>
#endif
static const stress_help_t hardlink_help[] = {
{ NULL, "link N", "start N workers creating hard links" },
{ NULL, "link-ops N", "stop after N link bogo operations" },
{ NULL, NULL, NULL }
};
static const stress_help_t symlink_help[] = {
{ NULL, "symlink N", "start N workers creating symbolic links" },
{ NULL, "symlink-ops N", "stop after N symbolic link bogo operations" },
{ NULL, NULL, NULL }
};
#define MOUNTS_MAX (128)
/*
* stress_link_unlink()
* remove all links
*/
static void stress_link_unlink(
const stress_args_t *args,
const uint64_t n)
{
uint64_t i;
for (i = 0; i < n; i++) {
char path[PATH_MAX];
(void)stress_temp_filename_args(args,
path, sizeof(path), i);
(void)shim_force_unlink(path);
}
(void)sync();
}
static inline size_t random_mount(const int mounts_max)
{
return (size_t)stress_mwc32() % (size_t)mounts_max;
}
/*
* stress_link_generic
* stress links, generic case
*/
static int stress_link_generic(
const stress_args_t *args,
int (*linkfunc)(const char *oldpath, const char *newpath),
const char *funcname)
{
int rc, ret, fd, mounts_max;
char oldpath[PATH_MAX], tmp_newpath[PATH_MAX];
size_t oldpathlen;
bool symlink_func = (linkfunc == symlink);
char *mnts[MOUNTS_MAX];
(void)memset(tmp_newpath, 0, sizeof(tmp_newpath));
(void)snprintf(tmp_newpath, sizeof(tmp_newpath),
"/tmp/stress-ng-%s-%d-%" PRIu64 "-link",
args->name, (int)getpid(), stress_mwc64());
ret = stress_temp_dir_mk_args(args);
if (ret < 0)
return exit_status(-ret);
(void)stress_temp_filename_args(args, oldpath, sizeof(oldpath), ~0UL);
if ((fd = open(oldpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
if ((errno == ENFILE) || (errno == ENOMEM) || (errno == ENOSPC))
return EXIT_NO_RESOURCE;
pr_fail("%s: open %s failed, errno=%d (%s)\n",
args->name, oldpath, errno, strerror(errno));
(void)stress_temp_dir_rm_args(args);
return EXIT_FAILURE;
}
(void)close(fd);
mounts_max = stress_mount_get(mnts, MOUNTS_MAX);
oldpathlen = strlen(oldpath);
stress_set_proc_state(args->name, STRESS_STATE_RUN);
rc = EXIT_SUCCESS;
do {
uint64_t i, n = DEFAULT_LINKS;
char testpath[PATH_MAX];
ssize_t rret;
for (i = 0; keep_stressing(args) && (i < n); i++) {
char newpath[PATH_MAX];
struct stat stbuf;
(void)stress_temp_filename_args(args,
newpath, sizeof(newpath), i);
if (linkfunc(oldpath, newpath) < 0) {
if ((errno == EDQUOT) ||
(errno == ENOMEM) ||
(errno == EMLINK) ||
(errno == ENOSPC)) {
/* Try again */
continue;
}
if (errno == EPERM) {
pr_inf_skip("%s: link calls not allowed on "
"the filesystem, skipping "
"stressor\n", args->name);
rc = EXIT_NO_RESOURCE;
goto err_unlink;
}
rc = exit_status(errno);
pr_fail("%s: %s failed, errno=%d (%s)\n",
args->name, funcname, errno, strerror(errno));
n = i;
break;
}
if (symlink_func) {
char buf[PATH_MAX];
#if defined(O_DIRECTORY) && \
defined(HAVE_READLINKAT)
{
char tmpfilename[PATH_MAX], *filename;
char tmpdir[PATH_MAX], *dir;
int dir_fd;
shim_strlcpy(tmpfilename, newpath, sizeof(tmpfilename));
filename = basename(tmpfilename);
shim_strlcpy(tmpdir, newpath, sizeof(tmpdir));
dir = dirname(tmpdir);
/*
* Relatively naive readlinkat exercising
*/
dir_fd = open(dir, O_DIRECTORY | O_RDONLY);
if (dir_fd >= 0) {
rret = readlinkat(dir_fd, filename, buf, sizeof(buf) - 1);
if ((rret < 0) && (errno != ENOSYS)) {
pr_fail("%s: readlinkat failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
(void)close(dir_fd);
}
}
#endif
rret = shim_readlink(newpath, buf, sizeof(buf) - 1);
if (rret < 0) {
rc = exit_status(errno);
pr_fail("%s: readlink failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
} else {
buf[rret] = '\0';
if ((size_t)rret != oldpathlen)
pr_fail("%s: readlink length error, got %zd, expected: %zd\n",
args->name, (size_t)rret, oldpathlen);
else
if (strncmp(oldpath, buf, (size_t)rret))
pr_fail("%s: readlink path error, got %s, expected %s\n",
args->name, buf, oldpath);
}
} else {
/* Hard link, exercise illegal cross device link, EXDEV error */
if (mounts_max > 0) {
/* Try hard link on differet random mount point */
const size_t idx = random_mount(mounts_max);
ret = linkfunc(mnts[idx], tmp_newpath);
if (ret == 0)
(void)shim_unlink(tmp_newpath);
}
}
if (lstat(newpath, &stbuf) < 0) {
rc = exit_status(errno);
pr_fail("%s: lstat failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
}
}
/* exercise invalid newpath size, EINVAL */
VOID_RET(ssize_t, readlink(oldpath, testpath, 0));
/* exercise empty oldpath, ENOENT */
VOID_RET(ssize_t, readlink("", testpath, sizeof(testpath)));
/* exercise non-link, EINVAL */
VOID_RET(ssize_t, readlink("/", testpath, sizeof(testpath)));
#if defined(HAVE_READLINKAT) && \
defined(AT_FDCWD)
/* exercise invalid newpath size, EINVAL */
VOID_RET(ssize_t, readlinkat(AT_FDCWD, ".", testpath, 0));
/* exercise invalid newpath size, EINVAL */
VOID_RET(ssize_t, readlinkat(AT_FDCWD, "", testpath, sizeof(testpath)));
/* exercise non-link, EINVAL */
VOID_RET(ssize_t, readlinkat(AT_FDCWD, "/", testpath, sizeof(testpath)));
#endif
err_unlink:
stress_link_unlink(args, n);
inc_counter(args);
} while ((rc == EXIT_SUCCESS) && keep_stressing(args));
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
(void)shim_unlink(oldpath);
(void)stress_temp_dir_rm_args(args);
stress_mount_free(mnts, mounts_max);
return rc;
}
#if !defined(__HAIKU__)
/*
* stress_link
* stress hard links
*/
static int stress_link(const stress_args_t *args)
{
return stress_link_generic(args, link, "link");
}
#endif
/*
* stress_symlink
* stress symbolic links
*/
static int stress_symlink(const stress_args_t *args)
{
return stress_link_generic(args, symlink, "symlink");
}
#if !defined(__HAIKU__)
stressor_info_t stress_link_info = {
.stressor = stress_link,
.class = CLASS_FILESYSTEM | CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = hardlink_help
};
#else
stressor_info_t stress_link_info = {
.stressor = stress_not_implemented,
.class = CLASS_FILESYSTEM | CLASS_OS,
.help = hardlink_help
};
#endif
stressor_info_t stress_symlink_info = {
.stressor = stress_symlink,
.class = CLASS_FILESYSTEM | CLASS_OS,
.verify = VERIFY_ALWAYS,
.help = symlink_help
};