t_vnops.c revision 1.9 1 /* $NetBSD: t_vnops.c,v 1.9 2010/09/09 11:42:52 njoly Exp $ */
2
3 /*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/stat.h>
30 #include <sys/statvfs.h>
31
32 #include <atf-c.h>
33 #include <fcntl.h>
34 #include <libgen.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #include <rump/rump_syscalls.h>
39 #include <rump/rump.h>
40
41 #include "../common/h_fsmacros.h"
42 #include "../../h_macros.h"
43
44 #define USES_DIRS \
45 if (FSTYPE_SYSVBFS(tc)) atf_tc_skip("dirs not supported by file system")
46
47 #define USES_SYMLINKS \
48 if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc)) \
49 atf_tc_skip("symlinks not supported by file system")
50
51 static char *
52 md(char *buf, const char *base, const char *tail)
53 {
54
55 sprintf(buf, "%s/%s", base, tail);
56 return buf;
57 }
58
59 static void
60 lookup_simple(const atf_tc_t *tc, const char *mountpath)
61 {
62 char pb[MAXPATHLEN], final[MAXPATHLEN];
63 struct stat sb1, sb2;
64
65 strcpy(final, mountpath);
66 sprintf(pb, "%s/../%s", mountpath, basename(final));
67 if (rump_sys_stat(pb, &sb1) == -1)
68 atf_tc_fail_errno("stat 1");
69
70 sprintf(pb, "%s/./../%s", mountpath, basename(final));
71 if (rump_sys_stat(pb, &sb2) == -1)
72 atf_tc_fail_errno("stat 2");
73
74 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
75 }
76
77 static void
78 lookup_complex(const atf_tc_t *tc, const char *mountpath)
79 {
80 char pb[MAXPATHLEN];
81 struct stat sb1, sb2;
82
83 USES_DIRS;
84
85 sprintf(pb, "%s/dir", mountpath);
86 if (rump_sys_mkdir(pb, 0777) == -1)
87 atf_tc_fail_errno("mkdir");
88 if (rump_sys_stat(pb, &sb1) == -1)
89 atf_tc_fail_errno("stat 1");
90
91 sprintf(pb, "%s/./dir/../././dir/.", mountpath);
92 if (rump_sys_stat(pb, &sb2) == -1)
93 atf_tc_fail_errno("stat 2");
94
95 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
96 }
97
98 static void
99 dir_simple(const atf_tc_t *tc, const char *mountpath)
100 {
101 char pb[MAXPATHLEN];
102 struct stat sb;
103
104 USES_DIRS;
105
106 /* check we can create directories */
107 sprintf(pb, "%s/dir", mountpath);
108 if (rump_sys_mkdir(pb, 0777) == -1)
109 atf_tc_fail_errno("mkdir");
110 if (rump_sys_stat(pb, &sb) == -1)
111 atf_tc_fail_errno("stat new directory");
112
113 /* check we can remove then and that it makes them unreachable */
114 if (rump_sys_rmdir(pb) == -1)
115 atf_tc_fail_errno("rmdir");
116 if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
117 atf_tc_fail("ENOENT expected from stat");
118 }
119
120 static void
121 dir_notempty(const atf_tc_t *tc, const char *mountpath)
122 {
123 char pb[MAXPATHLEN], pb2[MAXPATHLEN];
124 int fd, rv;
125
126 USES_DIRS;
127
128 /* check we can create directories */
129 sprintf(pb, "%s/dir", mountpath);
130 if (rump_sys_mkdir(pb, 0777) == -1)
131 atf_tc_fail_errno("mkdir");
132
133 sprintf(pb2, "%s/dir/file", mountpath);
134 fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
135 if (fd == -1)
136 atf_tc_fail_errno("create file");
137 rump_sys_close(fd);
138
139 rv = rump_sys_rmdir(pb);
140 if (rv != -1 || errno != ENOTEMPTY)
141 atf_tc_fail("non-empty directory removed succesfully");
142
143 if (rump_sys_unlink(pb2) == -1)
144 atf_tc_fail_errno("cannot remove dir/file");
145
146 if (rump_sys_rmdir(pb) == -1)
147 atf_tc_fail_errno("remove directory");
148 }
149
150 static void
151 checkfile(const char *path, struct stat *refp)
152 {
153 char buf[MAXPATHLEN];
154 struct stat sb;
155 static int n = 1;
156
157 md(buf, path, "file");
158 if (rump_sys_stat(buf, &sb) == -1)
159 atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
160 if (memcmp(&sb, refp, sizeof(sb)) != 0)
161 atf_tc_fail("stat mismatch %d", n);
162 n++;
163 }
164
165 static void
166 rename_dir(const atf_tc_t *tc, const char *mp)
167 {
168 char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
169 struct stat ref;
170
171 if (FSTYPE_MSDOS(tc))
172 atf_tc_skip("test fails in some setups, reason unknown");
173
174 USES_DIRS;
175
176 md(pb1, mp, "dir1");
177 if (rump_sys_mkdir(pb1, 0777) == -1)
178 atf_tc_fail_errno("mkdir 1");
179
180 md(pb2, mp, "dir2");
181 if (rump_sys_mkdir(pb2, 0777) == -1)
182 atf_tc_fail_errno("mkdir 2");
183 md(pb2, mp, "dir2/subdir");
184 if (rump_sys_mkdir(pb2, 0777) == -1)
185 atf_tc_fail_errno("mkdir 3");
186
187 md(pb3, mp, "dir1/file");
188 if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
189 atf_tc_fail_errno("create file");
190 if (rump_sys_stat(pb3, &ref) == -1)
191 atf_tc_fail_errno("stat of file");
192
193 /*
194 * First try ops which should succeed.
195 */
196
197 /* rename within directory */
198 md(pb3, mp, "dir3");
199 if (rump_sys_rename(pb1, pb3) == -1)
200 atf_tc_fail_errno("rename 1");
201 checkfile(pb3, &ref);
202
203 /* rename directory onto itself (two ways, should fail) */
204 md(pb1, mp, "dir3/.");
205 if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
206 atf_tc_fail_errno("rename 2");
207 if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
208 atf_tc_fail_errno("rename 3");
209
210 checkfile(pb3, &ref);
211
212 /* rename father of directory into directory */
213 md(pb1, mp, "dir2/dir");
214 md(pb2, mp, "dir2");
215 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
216 atf_tc_fail_errno("rename 4");
217
218 /* same for grandfather */
219 md(pb1, mp, "dir2/subdir/dir2");
220 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
221 atf_tc_fail("rename 5");
222
223 checkfile(pb3, &ref);
224
225 /* rename directory over a non-empty directory */
226 if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
227 atf_tc_fail("rename 6");
228
229 /* cross-directory rename */
230 md(pb1, mp, "dir3");
231 md(pb2, mp, "dir2/somedir");
232 if (rump_sys_rename(pb1, pb2) == -1)
233 atf_tc_fail_errno("rename 7");
234 checkfile(pb2, &ref);
235
236 /* move to parent directory */
237 md(pb1, mp, "dir2/somedir/../../dir3");
238 if (rump_sys_rename(pb2, pb1) == -1)
239 atf_tc_fail_errno("rename 8");
240 md(pb1, mp, "dir2/../dir3");
241 checkfile(pb1, &ref);
242
243 /* finally, atomic cross-directory rename */
244 md(pb3, mp, "dir2/subdir");
245 if (rump_sys_rename(pb1, pb3) == -1)
246 atf_tc_fail_errno("rename 9");
247 checkfile(pb3, &ref);
248 }
249
250 static void
251 rename_dotdot(const atf_tc_t *tc, const char *mp)
252 {
253
254 USES_DIRS;
255
256 if (rump_sys_chdir(mp) == -1)
257 atf_tc_fail_errno("chdir mountpoint");
258
259 if (rump_sys_mkdir("dir1", 0777) == -1)
260 atf_tc_fail_errno("mkdir 1");
261 if (rump_sys_mkdir("dir2", 0777) == -1)
262 atf_tc_fail_errno("mkdir 2");
263
264 if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
265 atf_tc_fail_errno("self-dotdot to");
266
267 if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
268 atf_tc_fail_errno("self-dotdot from");
269 atf_tc_expect_pass();
270
271 if (FSTYPE_TMPFS(tc)) {
272 atf_tc_expect_fail("PR kern/43617");
273 }
274 if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
275 atf_tc_fail("other-dotdot");
276
277 rump_sys_chdir("/");
278 }
279
280 static void
281 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
282 {
283 bool haslinks;
284 struct stat sb;
285 ino_t f1ino, f2ino;
286
287 if (FSTYPE_MSDOS(tc))
288 atf_tc_skip("test fails in some setups, reason unknown");
289
290 if (rump_sys_chdir(mp) == -1)
291 atf_tc_fail_errno("chdir mountpoint");
292
293 if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
294 haslinks = false;
295 else
296 haslinks = true;
297
298 if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
299 atf_tc_fail_errno("create file");
300 if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
301 atf_tc_fail_errno("create file");
302
303 if (rump_sys_stat("file1", &sb) == -1)
304 atf_tc_fail_errno("stat");
305 f1ino = sb.st_ino;
306
307 if (haslinks) {
308 if (rump_sys_link("file1", "file_link") == -1)
309 atf_tc_fail_errno("link");
310 if (rump_sys_stat("file_link", &sb) == -1)
311 atf_tc_fail_errno("stat");
312 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
313 ATF_REQUIRE_EQ(sb.st_nlink, 2);
314 }
315
316 if (rump_sys_stat("file2", &sb) == -1)
317 atf_tc_fail_errno("stat");
318 f2ino = sb.st_ino;
319
320 if (rump_sys_rename("file1", "file3") == -1)
321 atf_tc_fail_errno("rename 1");
322 if (rump_sys_stat("file3", &sb) == -1)
323 atf_tc_fail_errno("stat 1");
324 if (haslinks) {
325 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
326 }
327 if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
328 atf_tc_fail_errno("source 1");
329
330 if (rump_sys_rename("file3", "file2") == -1)
331 atf_tc_fail_errno("rename 2");
332 if (rump_sys_stat("file2", &sb) == -1)
333 atf_tc_fail_errno("stat 2");
334 if (haslinks) {
335 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
336 }
337
338 if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
339 atf_tc_fail_errno("source 2");
340
341 if (haslinks) {
342 if (rump_sys_rename("file2", "file_link") == -1)
343 atf_tc_fail_errno("rename hardlink");
344 if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
345 atf_tc_fail_errno("source 3");
346 if (rump_sys_stat("file_link", &sb) == -1)
347 atf_tc_fail_errno("stat 2");
348 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
349 ATF_REQUIRE_EQ(sb.st_nlink, 1);
350 }
351
352 rump_sys_chdir("/");
353 }
354
355 static void
356 create_nametoolong(const atf_tc_t *tc, const char *mp)
357 {
358 char *name;
359 int fd;
360 long val;
361 size_t len;
362
363 if (rump_sys_chdir(mp) == -1)
364 atf_tc_fail_errno("chdir mountpoint");
365
366 val = rump_sys_pathconf(".", _PC_NAME_MAX);
367 if (val == -1)
368 atf_tc_fail_errno("pathconf");
369
370 len = val + 1;
371 name = malloc(len+1);
372 if (name == NULL)
373 atf_tc_fail_errno("malloc");
374
375 memset(name, 'a', len);
376 *(name+len) = '\0';
377
378 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
379 if (val == -1)
380 atf_tc_fail_errno("pathconf");
381
382 if (FSTYPE_MSDOS(tc))
383 atf_tc_expect_fail("PR kern/43670");
384 fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
385 if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
386 atf_tc_fail_errno("open");
387
388 if (val == 0 && rump_sys_close(fd) == -1)
389 atf_tc_fail_errno("close");
390 if (val == 0 && rump_sys_unlink(name) == -1)
391 atf_tc_fail_errno("unlink");
392
393 free(name);
394
395 rump_sys_chdir("/");
396 }
397
398 static void
399 rename_nametoolong(const atf_tc_t *tc, const char *mp)
400 {
401 char *name;
402 int res, fd;
403 long val;
404 size_t len;
405
406 if (rump_sys_chdir(mp) == -1)
407 atf_tc_fail_errno("chdir mountpoint");
408
409 val = rump_sys_pathconf(".", _PC_NAME_MAX);
410 if (val == -1)
411 atf_tc_fail_errno("pathconf");
412
413 len = val + 1;
414 name = malloc(len+1);
415 if (name == NULL)
416 atf_tc_fail_errno("malloc");
417
418 memset(name, 'a', len);
419 *(name+len) = '\0';
420
421 fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
422 if (fd == -1)
423 atf_tc_fail_errno("open");
424 if (rump_sys_close(fd) == -1)
425 atf_tc_fail_errno("close");
426
427 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
428 if (val == -1)
429 atf_tc_fail_errno("pathconf");
430
431 if (FSTYPE_MSDOS(tc))
432 atf_tc_expect_fail("PR kern/43670");
433 res = rump_sys_rename("dummy", name);
434 if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
435 atf_tc_fail_errno("rename");
436
437 if (val == 0 && rump_sys_unlink(name) == -1)
438 atf_tc_fail_errno("unlink");
439
440 free(name);
441
442 rump_sys_chdir("/");
443 }
444
445 static void
446 symlink_zerolen(const atf_tc_t *tc, const char *mp)
447 {
448
449 USES_SYMLINKS;
450
451 RL(rump_sys_chdir(mp));
452
453 if (FSTYPE_TMPFS(tc)) {
454 atf_tc_expect_signal(SIGABRT, "PR kern/43843");
455 }
456
457 RL(rump_sys_symlink("", "afile"));
458 RL(rump_sys_chdir("/"));
459 }
460
461 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
462 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
463 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
464 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
465 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
466 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
467 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
468 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
469 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
470 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
471
472 ATF_TP_ADD_TCS(tp)
473 {
474
475 ATF_TP_FSAPPLY(lookup_simple);
476 ATF_TP_FSAPPLY(lookup_complex);
477 ATF_TP_FSAPPLY(dir_simple);
478 ATF_TP_FSAPPLY(dir_notempty);
479 ATF_TP_FSAPPLY(rename_dir);
480 ATF_TP_FSAPPLY(rename_dotdot);
481 ATF_TP_FSAPPLY(rename_reg_nodir);
482 ATF_TP_FSAPPLY(create_nametoolong);
483 ATF_TP_FSAPPLY(rename_nametoolong);
484 ATF_TP_FSAPPLY(symlink_zerolen);
485
486 return atf_no_error();
487 }
488