xref: /netbsd-src/tests/fs/vfs/t_vnops.c (revision fa28c6faa16e0b00edee7acdcaf4899797043def)
1 /*	$NetBSD: t_vnops.c,v 1.53 2016/01/13 12:05:49 pooka 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 <assert.h>
33 #include <atf-c.h>
34 #include <fcntl.h>
35 #include <libgen.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include <rump/rump_syscalls.h>
41 #include <rump/rump.h>
42 
43 #include "../common/h_fsmacros.h"
44 #include "../../h_macros.h"
45 
46 #define TESTFILE "afile"
47 
48 #define USES_DIRS					\
49     if (FSTYPE_SYSVBFS(tc))				\
50 	atf_tc_skip("directories not supported by file system")
51 
52 #define USES_SYMLINKS					\
53     if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))		\
54 	atf_tc_skip("symlinks not supported by file system")
55 
56 static char *
57 md(char *buf, const char *base, const char *tail)
58 {
59 
60 	sprintf(buf, "%s/%s", base, tail);
61 	return buf;
62 }
63 
64 static void
65 lookup_simple(const atf_tc_t *tc, const char *mountpath)
66 {
67 	char pb[MAXPATHLEN], final[MAXPATHLEN];
68 	struct stat sb1, sb2;
69 
70 	strcpy(final, mountpath);
71 	sprintf(pb, "%s/../%s", mountpath, basename(final));
72 	if (rump_sys_stat(pb, &sb1) == -1)
73 		atf_tc_fail_errno("stat 1");
74 
75 	sprintf(pb, "%s/./../%s", mountpath, basename(final));
76 	if (rump_sys_stat(pb, &sb2) == -1)
77 		atf_tc_fail_errno("stat 2");
78 
79 	ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
80 }
81 
82 static void
83 lookup_complex(const atf_tc_t *tc, const char *mountpath)
84 {
85 	char pb[MAXPATHLEN];
86 	struct stat sb1, sb2;
87 
88 	USES_DIRS;
89 
90 	if (FSTYPE_UDF(tc))
91 		atf_tc_expect_fail("PR kern/49033");
92 
93 	sprintf(pb, "%s/dir", mountpath);
94 	if (rump_sys_mkdir(pb, 0777) == -1)
95 		atf_tc_fail_errno("mkdir");
96 	if (rump_sys_stat(pb, &sb1) == -1)
97 		atf_tc_fail_errno("stat 1");
98 
99 	sprintf(pb, "%s/./dir/../././dir/.", mountpath);
100 	if (rump_sys_stat(pb, &sb2) == -1)
101 		atf_tc_fail_errno("stat 2");
102 
103 	if (memcmp(&sb1, &sb2, sizeof(sb1)) != 0) {
104 		printf("what\tsb1\t\tsb2\n");
105 
106 #define FIELD(FN)	\
107 		printf(#FN "\t%lld\t%lld\n", \
108 		(long long)sb1.FN, (long long)sb2.FN)
109 #define TIME(FN)	\
110 		printf(#FN "\t%lld.%ld\t%lld.%ld\n", \
111 		(long long)sb1.FN.tv_sec, sb1.FN.tv_nsec, \
112 		(long long)sb2.FN.tv_sec, sb2.FN.tv_nsec)
113 
114 		FIELD(st_dev);
115 		FIELD(st_mode);
116 		FIELD(st_ino);
117 		FIELD(st_nlink);
118 		FIELD(st_uid);
119 		FIELD(st_gid);
120 		FIELD(st_rdev);
121 		TIME(st_atimespec);
122 		TIME(st_mtimespec);
123 		TIME(st_ctimespec);
124 		TIME(st_birthtimespec);
125 		FIELD(st_size);
126 		FIELD(st_blocks);
127 		FIELD(st_flags);
128 		FIELD(st_gen);
129 
130 #undef FIELD
131 #undef TIME
132 
133 		atf_tc_fail("stat results differ, see ouput for more details");
134 	}
135 	if (FSTYPE_UDF(tc))
136 		atf_tc_fail("random failure of PR kern/49033 "
137 			    "did not happen this time");
138 }
139 
140 static void
141 dir_simple(const atf_tc_t *tc, const char *mountpath)
142 {
143 	char pb[MAXPATHLEN];
144 	struct stat sb;
145 
146 	USES_DIRS;
147 
148 	/* check we can create directories */
149 	sprintf(pb, "%s/dir", mountpath);
150 	if (rump_sys_mkdir(pb, 0777) == -1)
151 		atf_tc_fail_errno("mkdir");
152 	if (rump_sys_stat(pb, &sb) == -1)
153 		atf_tc_fail_errno("stat new directory");
154 
155 	/* check we can remove then and that it makes them unreachable */
156 	if (rump_sys_rmdir(pb) == -1)
157 		atf_tc_fail_errno("rmdir");
158 	if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
159 		atf_tc_fail("ENOENT expected from stat");
160 }
161 
162 static void
163 dir_notempty(const atf_tc_t *tc, const char *mountpath)
164 {
165 	char pb[MAXPATHLEN], pb2[MAXPATHLEN];
166 	int fd, rv;
167 
168 	USES_DIRS;
169 
170 	/* check we can create directories */
171 	sprintf(pb, "%s/dir", mountpath);
172 	if (rump_sys_mkdir(pb, 0777) == -1)
173 		atf_tc_fail_errno("mkdir");
174 
175 	sprintf(pb2, "%s/dir/file", mountpath);
176 	fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
177 	if (fd == -1)
178 		atf_tc_fail_errno("create file");
179 	rump_sys_close(fd);
180 
181 	rv = rump_sys_rmdir(pb);
182 	if (rv != -1 || errno != ENOTEMPTY)
183 		atf_tc_fail("non-empty directory removed succesfully");
184 
185 	if (rump_sys_unlink(pb2) == -1)
186 		atf_tc_fail_errno("cannot remove dir/file");
187 
188 	if (rump_sys_rmdir(pb) == -1)
189 		atf_tc_fail_errno("remove directory");
190 }
191 
192 static void
193 dir_rmdirdotdot(const atf_tc_t *tc, const char *mp)
194 {
195 	char pb[MAXPATHLEN];
196 	int xerrno;
197 
198 	USES_DIRS;
199 
200 	FSTEST_ENTER();
201 	RL(rump_sys_mkdir("test", 0777));
202 	RL(rump_sys_chdir("test"));
203 
204 	RL(rump_sys_mkdir("subtest", 0777));
205 	RL(rump_sys_chdir("subtest"));
206 
207 	md(pb, mp, "test/subtest");
208 	RL(rump_sys_rmdir(pb));
209 	md(pb, mp, "test");
210 	RL(rump_sys_rmdir(pb));
211 
212 	if (FSTYPE_NFS(tc))
213 		xerrno = ESTALE;
214 	else
215 		xerrno = ENOENT;
216 	ATF_REQUIRE_ERRNO(xerrno, rump_sys_chdir("..") == -1);
217 	FSTEST_EXIT();
218 }
219 
220 static void
221 checkfile(const char *path, struct stat *refp)
222 {
223 	char buf[MAXPATHLEN];
224 	struct stat sb;
225 	static int n = 1;
226 
227 	md(buf, path, "file");
228 	if (rump_sys_stat(buf, &sb) == -1)
229 		atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
230 	if (memcmp(&sb, refp, sizeof(sb)) != 0)
231 		atf_tc_fail("stat mismatch %d", n);
232 	n++;
233 }
234 
235 static void
236 rename_dir(const atf_tc_t *tc, const char *mp)
237 {
238 	char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
239 	struct stat ref, sb;
240 
241 	if (FSTYPE_RUMPFS(tc))
242 		atf_tc_skip("rename not supported by file system");
243 
244 	USES_DIRS;
245 
246 	md(pb1, mp, "dir1");
247 	if (rump_sys_mkdir(pb1, 0777) == -1)
248 		atf_tc_fail_errno("mkdir 1");
249 
250 	md(pb2, mp, "dir2");
251 	if (rump_sys_mkdir(pb2, 0777) == -1)
252 		atf_tc_fail_errno("mkdir 2");
253 	md(pb2, mp, "dir2/subdir");
254 	if (rump_sys_mkdir(pb2, 0777) == -1)
255 		atf_tc_fail_errno("mkdir 3");
256 
257 	md(pb3, mp, "dir1/file");
258 	if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
259 		atf_tc_fail_errno("create file");
260 	if (rump_sys_stat(pb3, &ref) == -1)
261 		atf_tc_fail_errno("stat of file");
262 
263 	/*
264 	 * First try ops which should succeed.
265 	 */
266 
267 	/* rename within directory */
268 	md(pb3, mp, "dir3");
269 	if (rump_sys_rename(pb1, pb3) == -1)
270 		atf_tc_fail_errno("rename 1");
271 	checkfile(pb3, &ref);
272 
273 	/* rename directory onto itself (two ways, should fail) */
274 	md(pb1, mp, "dir3/.");
275 	if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
276 		atf_tc_fail_errno("rename 2");
277 	if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
278 		atf_tc_fail_errno("rename 3");
279 
280 	checkfile(pb3, &ref);
281 
282 	/* rename father of directory into directory */
283 	md(pb1, mp, "dir2/dir");
284 	md(pb2, mp, "dir2");
285 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
286 		atf_tc_fail_errno("rename 4");
287 
288 	/* same for grandfather */
289 	md(pb1, mp, "dir2/subdir/dir2");
290 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
291 		atf_tc_fail("rename 5");
292 
293 	checkfile(pb3, &ref);
294 
295 	/* rename directory over a non-empty directory */
296 	if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
297 		atf_tc_fail("rename 6");
298 
299 	/* cross-directory rename */
300 	md(pb1, mp, "dir3");
301 	md(pb2, mp, "dir2/somedir");
302 	if (rump_sys_rename(pb1, pb2) == -1)
303 		atf_tc_fail_errno("rename 7");
304 	checkfile(pb2, &ref);
305 
306 	/* move to parent directory */
307 	md(pb1, mp, "dir2/somedir/../../dir3");
308 	if (rump_sys_rename(pb2, pb1) == -1)
309 		atf_tc_fail_errno("rename 8");
310 	md(pb1, mp, "dir2/../dir3");
311 	checkfile(pb1, &ref);
312 
313 	/* atomic cross-directory rename */
314 	md(pb3, mp, "dir2/subdir");
315 	if (rump_sys_rename(pb1, pb3) == -1)
316 		atf_tc_fail_errno("rename 9");
317 	checkfile(pb3, &ref);
318 
319 	/* rename directory over an empty directory */
320 	md(pb1, mp, "parent");
321 	md(pb2, mp, "parent/dir1");
322 	md(pb3, mp, "parent/dir2");
323 	RL(rump_sys_mkdir(pb1, 0777));
324 	RL(rump_sys_mkdir(pb2, 0777));
325 	RL(rump_sys_mkdir(pb3, 0777));
326 	RL(rump_sys_rename(pb2, pb3));
327 
328 	RL(rump_sys_stat(pb1, &sb));
329 	if (! FSTYPE_MSDOS(tc))
330 		ATF_CHECK_EQ(sb.st_nlink, 3);
331 	RL(rump_sys_rmdir(pb3));
332 	RL(rump_sys_rmdir(pb1));
333 }
334 
335 static void
336 rename_dotdot(const atf_tc_t *tc, const char *mp)
337 {
338 
339 	if (FSTYPE_RUMPFS(tc))
340 		atf_tc_skip("rename not supported by file system");
341 
342 	USES_DIRS;
343 
344 	if (rump_sys_chdir(mp) == -1)
345 		atf_tc_fail_errno("chdir mountpoint");
346 
347 	if (rump_sys_mkdir("dir1", 0777) == -1)
348 		atf_tc_fail_errno("mkdir 1");
349 	if (rump_sys_mkdir("dir2", 0777) == -1)
350 		atf_tc_fail_errno("mkdir 2");
351 
352 	if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
353 		atf_tc_fail_errno("self-dotdot to");
354 
355 	if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
356 		atf_tc_fail_errno("self-dotdot from");
357 
358 	if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
359 		atf_tc_fail("other-dotdot");
360 
361 	rump_sys_chdir("/");
362 }
363 
364 static void
365 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
366 {
367 	bool haslinks;
368 	struct stat sb;
369 	ino_t f1ino;
370 
371 	if (FSTYPE_RUMPFS(tc))
372 		atf_tc_skip("rename not supported by file system");
373 
374 	if (rump_sys_chdir(mp) == -1)
375 		atf_tc_fail_errno("chdir mountpoint");
376 
377 	if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
378 		haslinks = false;
379 	else
380 		haslinks = true;
381 
382 	if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
383 		atf_tc_fail_errno("create file");
384 	if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
385 		atf_tc_fail_errno("create file");
386 
387 	if (rump_sys_stat("file1", &sb) == -1)
388 		atf_tc_fail_errno("stat");
389 	f1ino = sb.st_ino;
390 
391 	if (haslinks) {
392 		if (rump_sys_link("file1", "file_link") == -1)
393 			atf_tc_fail_errno("link");
394 		if (rump_sys_stat("file_link", &sb) == -1)
395 			atf_tc_fail_errno("stat");
396 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
397 		ATF_REQUIRE_EQ(sb.st_nlink, 2);
398 	}
399 
400 	if (rump_sys_stat("file2", &sb) == -1)
401 		atf_tc_fail_errno("stat");
402 
403 	if (rump_sys_rename("file1", "file3") == -1)
404 		atf_tc_fail_errno("rename 1");
405 	if (rump_sys_stat("file3", &sb) == -1)
406 		atf_tc_fail_errno("stat 1");
407 	if (haslinks) {
408 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
409 	}
410 	if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
411 		atf_tc_fail_errno("source 1");
412 
413 	if (rump_sys_rename("file3", "file2") == -1)
414 		atf_tc_fail_errno("rename 2");
415 	if (rump_sys_stat("file2", &sb) == -1)
416 		atf_tc_fail_errno("stat 2");
417 	if (haslinks) {
418 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
419 	}
420 
421 	if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
422 		atf_tc_fail_errno("source 2");
423 
424 	if (haslinks) {
425 		if (rump_sys_rename("file2", "file_link") == -1)
426 			atf_tc_fail_errno("rename hardlink");
427 		if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
428 			atf_tc_fail_errno("source 3");
429 		if (rump_sys_stat("file_link", &sb) == -1)
430 			atf_tc_fail_errno("stat 2");
431 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
432 		ATF_REQUIRE_EQ(sb.st_nlink, 1);
433 	}
434 
435 	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename("file2", NULL) == -1);
436 	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename(NULL, "file2") == -1);
437 
438 	rump_sys_chdir("/");
439 }
440 
441 /* PR kern/50607 */
442 static void
443 create_many(const atf_tc_t *tc, const char *mp)
444 {
445 	char buf[64];
446 	int nfiles = 2324; /* #Nancy */
447 	int i;
448 
449 	if (FSTYPE_UDF(tc))
450 		atf_tc_expect_fail("PR kern/50608");
451 
452 	/* takes forever with many files */
453 	if (FSTYPE_MSDOS(tc))
454 		nfiles /= 4;
455 
456 	RL(rump_sys_chdir(mp));
457 
458 	if (FSTYPE_SYSVBFS(tc)) {
459 		/* fs doesn't support many files or subdirectories */
460 		nfiles = 5;
461 	} else {
462 		/* msdosfs doesn't like many entries in the root directory */
463 		RL(rump_sys_mkdir("subdir", 0777));
464 		RL(rump_sys_chdir("subdir"));
465 	}
466 
467 	/* create them */
468 #define TESTFN "testfile"
469 	for (i = 0; i < nfiles; i++) {
470 		int fd;
471 
472 		sprintf(buf, TESTFN "%d\n", i);
473 		RL(fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666));
474 		RL(rump_sys_close(fd));
475 	}
476 
477 	/* wipe them out */
478 	for (i = 0; i < nfiles; i++) {
479 		sprintf(buf, TESTFN "%d\n", i);
480 		RL(rump_sys_unlink(buf));
481 	}
482 #undef TESTFN
483 
484 	rump_sys_chdir("/");
485 }
486 
487 static void
488 create_nametoolong(const atf_tc_t *tc, const char *mp)
489 {
490 	char *name;
491 	int fd;
492 	long val;
493 	size_t len;
494 
495 	if (rump_sys_chdir(mp) == -1)
496 		atf_tc_fail_errno("chdir mountpoint");
497 
498 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
499 	if (val == -1)
500 		atf_tc_fail_errno("pathconf");
501 
502 	len = val + 1;
503 	name = malloc(len+1);
504 	if (name == NULL)
505 		atf_tc_fail_errno("malloc");
506 
507 	memset(name, 'a', len);
508 	*(name+len) = '\0';
509 
510 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
511 	if (val == -1)
512 		atf_tc_fail_errno("pathconf");
513 
514 	fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
515 	if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
516 		atf_tc_fail_errno("open");
517 
518 	if (val == 0 && rump_sys_close(fd) == -1)
519 		atf_tc_fail_errno("close");
520 	if (val == 0 && rump_sys_unlink(name) == -1)
521 		atf_tc_fail_errno("unlink");
522 
523 	free(name);
524 
525 	rump_sys_chdir("/");
526 }
527 
528 static void
529 create_exist(const atf_tc_t *tc, const char *mp)
530 {
531 	const char *name = "hoge";
532 	int fd;
533 
534 	RL(rump_sys_chdir(mp));
535 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
536 	RL(rump_sys_close(fd));
537 	RL(rump_sys_unlink(name));
538 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
539 	RL(rump_sys_close(fd));
540 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
541 	RL(rump_sys_close(fd));
542 	ATF_REQUIRE_ERRNO(EEXIST,
543 	    (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
544 	RL(rump_sys_unlink(name));
545 	RL(rump_sys_chdir("/"));
546 }
547 
548 static void
549 rename_nametoolong(const atf_tc_t *tc, const char *mp)
550 {
551 	char *name;
552 	int res, fd;
553 	long val;
554 	size_t len;
555 
556 	if (FSTYPE_RUMPFS(tc))
557 		atf_tc_skip("rename not supported by file system");
558 
559 	if (rump_sys_chdir(mp) == -1)
560 		atf_tc_fail_errno("chdir mountpoint");
561 
562 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
563 	if (val == -1)
564 		atf_tc_fail_errno("pathconf");
565 
566 	len = val + 1;
567 	name = malloc(len+1);
568 	if (name == NULL)
569 		atf_tc_fail_errno("malloc");
570 
571 	memset(name, 'a', len);
572 	*(name+len) = '\0';
573 
574 	fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
575 	if (fd == -1)
576 		atf_tc_fail_errno("open");
577 	if (rump_sys_close(fd) == -1)
578 		atf_tc_fail_errno("close");
579 
580 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
581 	if (val == -1)
582 		atf_tc_fail_errno("pathconf");
583 
584 	res = rump_sys_rename("dummy", name);
585 	if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
586 		atf_tc_fail_errno("rename");
587 
588 	if (val == 0 && rump_sys_unlink(name) == -1)
589 		atf_tc_fail_errno("unlink");
590 
591 	free(name);
592 
593 	rump_sys_chdir("/");
594 }
595 
596 /*
597  * Test creating a symlink whose length is "len" bytes, not including
598  * the terminating NUL.
599  */
600 static void
601 symlink_len(const atf_tc_t *tc, const char *mp, size_t len)
602 {
603 	char *buf;
604 	int r;
605 
606 	USES_SYMLINKS;
607 
608 	RL(rump_sys_chdir(mp));
609 
610 	buf = malloc(len + 1);
611 	ATF_REQUIRE(buf);
612 	memset(buf, 'a', len);
613 	buf[len] = '\0';
614 	r = rump_sys_symlink(buf, "afile");
615 	if (r == -1) {
616 		ATF_REQUIRE_ERRNO(ENAMETOOLONG, r);
617 	} else {
618 		RL(rump_sys_unlink("afile"));
619 	}
620 	free(buf);
621 
622 	RL(rump_sys_chdir("/"));
623 }
624 
625 static void
626 symlink_zerolen(const atf_tc_t *tc, const char *mp)
627 {
628 	symlink_len(tc, mp, 0);
629 }
630 
631 static void
632 symlink_long(const atf_tc_t *tc, const char *mp)
633 {
634 	/*
635 	 * Test lengths close to powers of two, as those are likely
636 	 * to be edge cases.
637 	 */
638 	size_t len;
639 	int fuzz;
640 	for (len = 2; len <= 65536; len *= 2) {
641 		for (fuzz = -1; fuzz <= 1; fuzz++) {
642 			symlink_len(tc, mp, len + fuzz);
643 		}
644 	}
645 }
646 
647 static void
648 symlink_root(const atf_tc_t *tc, const char *mp)
649 {
650 
651 	USES_SYMLINKS;
652 
653 	RL(rump_sys_chdir(mp));
654 	RL(rump_sys_symlink("/", "foo"));
655 	RL(rump_sys_chdir("foo"));
656 }
657 
658 static void
659 attrs(const atf_tc_t *tc, const char *mp)
660 {
661 	struct stat sb, sb2;
662 	struct timeval tv[2];
663 	int fd;
664 
665 	FSTEST_ENTER();
666 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
667 	RL(rump_sys_close(fd));
668 	RL(rump_sys_stat(TESTFILE, &sb));
669 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
670 		RL(rump_sys_chown(TESTFILE, 1, 2));
671 		sb.st_uid = 1;
672 		sb.st_gid = 2;
673 		RL(rump_sys_chmod(TESTFILE, 0123));
674 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
675 	}
676 
677 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
678 	tv[0].tv_usec = 1;
679 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
680 	tv[1].tv_usec = 3;
681 	RL(rump_sys_utimes(TESTFILE, tv));
682 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
683 	sb.st_atimespec.tv_sec = 1000000000;
684 	sb.st_atimespec.tv_nsec = 1000;
685 	sb.st_mtimespec.tv_sec = 1000000002;
686 	sb.st_mtimespec.tv_nsec = 3000;
687 
688 	RL(rump_sys_stat(TESTFILE, &sb2));
689 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
690 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
691 		CHECK(st_uid);
692 		CHECK(st_gid);
693 		CHECK(st_mode);
694 	}
695 	if (!FSTYPE_MSDOS(tc)) {
696 		/* msdosfs has only access date, not time */
697 		CHECK(st_atimespec.tv_sec);
698 	}
699 	CHECK(st_mtimespec.tv_sec);
700 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
701 	      FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
702 		CHECK(st_atimespec.tv_nsec);
703 		CHECK(st_mtimespec.tv_nsec);
704 	}
705 #undef  CHECK
706 
707 	FSTEST_EXIT();
708 }
709 
710 static void
711 fcntl_lock(const atf_tc_t *tc, const char *mp)
712 {
713 	int fd, fd2;
714 	struct flock l;
715 	struct lwp *lwp1, *lwp2;
716 
717 	FSTEST_ENTER();
718 	l.l_pid = 0;
719 	l.l_start = l.l_len = 1024;
720 	l.l_type = F_RDLCK | F_WRLCK;
721 	l.l_whence = SEEK_END;
722 
723 	lwp1 = rump_pub_lwproc_curlwp();
724 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
725 	RL(rump_sys_ftruncate(fd, 8192));
726 
727 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
728 
729 	/* Next, we fork and try to lock the same area */
730 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
731 	lwp2 = rump_pub_lwproc_curlwp();
732 	RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
733 	ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
734 
735 	/* Switch back and unlock... */
736 	rump_pub_lwproc_switch(lwp1);
737 	l.l_type = F_UNLCK;
738 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
739 
740 	/* ... and try to lock again */
741 	rump_pub_lwproc_switch(lwp2);
742 	l.l_type = F_RDLCK | F_WRLCK;
743 	RL(rump_sys_fcntl(fd2, F_SETLK, &l));
744 
745 	RL(rump_sys_close(fd2));
746 	rump_pub_lwproc_releaselwp();
747 
748 	RL(rump_sys_close(fd));
749 
750 	FSTEST_EXIT();
751 }
752 
753 static int
754 flock_compare(const void *p, const void *q)
755 {
756 	int a = ((const struct flock *)p)->l_start;
757 	int b = ((const struct flock *)q)->l_start;
758 	return a < b ? -1 : (a > b ? 1 : 0);
759 }
760 
761 /*
762  * Find all locks set by fcntl_getlock_pids test
763  * using GETLK for a range [start, start+end], and,
764  * if there is a blocking lock, recursively find
765  * all locks to the left (toward the beginning of
766  * a file) and to the right of the lock.
767  * The function also understands "until end of file"
768  * convention when len==0.
769  */
770 static unsigned int
771 fcntl_getlocks(int fildes, off_t start, off_t len,
772     struct flock *lock, struct flock *end)
773 {
774 	unsigned int rv = 0;
775 	const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
776 
777 	if (lock == end)
778 		return rv;
779 
780 	RL(rump_sys_fcntl(fildes, F_GETLK, &l));
781 
782 	if (l.l_type == F_UNLCK)
783 		return rv;
784 
785 	*lock++ = l;
786 	rv += 1;
787 
788 	ATF_REQUIRE(l.l_whence == SEEK_SET);
789 
790 	if (l.l_start > start) {
791 		unsigned int n =
792 		    fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
793 		rv += n;
794 		lock += n;
795 		if (lock == end)
796 			return rv;
797 	}
798 
799 	if (l.l_len == 0) /* does l spans until the end? */
800 		return rv;
801 
802 	if (len == 0) /* are we looking for locks until the end? */ {
803 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
804 	} else if (l.l_start + l.l_len < start + len) {
805 		len -= l.l_start + l.l_len - start;
806 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
807 	}
808 
809 	return rv;
810 }
811 
812 static void
813 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
814 {
815 	/* test non-overlaping ranges */
816 	struct flock expect[4];
817 	const struct flock lock[4] = {
818 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
819 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
820 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
821 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
822 	};
823 
824     /* Add extra element to make sure recursion does't stop at array end */
825 	struct flock result[5];
826 
827 	/* Add 5th process */
828 	int fd[5];
829 	pid_t pid[5];
830 	struct lwp *lwp[5];
831 
832 	unsigned int i, j;
833 	const off_t sz = 8192;
834 	int omode  = 0755;
835 	int oflags = O_RDWR | O_CREAT;
836 
837 	memcpy(expect, lock, sizeof(lock));
838 
839 	FSTEST_ENTER();
840 
841 	/*
842 	 * First, we create 4 processes and let each lock a range of the
843 	 * file.  Note that the third and fourth processes lock in
844 	 * "reverse" order, i.e. the greater pid locks a range before
845 	 * the lesser pid.
846 	 * Then, we create 5th process which doesn't lock anything.
847 	 */
848 	for (i = 0; i < __arraycount(lwp); i++) {
849 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
850 
851 		lwp[i] = rump_pub_lwproc_curlwp();
852 		pid[i] = rump_sys_getpid();
853 
854 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
855 		oflags = O_RDWR;
856 		omode  = 0;
857 
858 		RL(rump_sys_ftruncate(fd[i], sz));
859 
860 		if (i < __arraycount(lock)) {
861 			RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
862 			expect[i].l_pid = pid[i];
863 		}
864 	}
865 
866 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
867 
868 	/*
869 	 * In the context of each process, recursively find all locks
870 	 * that would block the current process. Processes 1-4 don't
871 	 * see their own lock, we insert it to simplify checks.
872 	 * Process 5 sees all 4 locks.
873 	 */
874 	for (i = 0; i < __arraycount(lwp); i++) {
875 		unsigned int nlocks;
876 
877 		rump_pub_lwproc_switch(lwp[i]);
878 
879 		memset(result, 0, sizeof(result));
880 		nlocks = fcntl_getlocks(fd[i], 0, sz,
881 		    result, result + __arraycount(result));
882 
883 		if (i < __arraycount(lock)) {
884 			ATF_REQUIRE(nlocks < __arraycount(result));
885 			result[nlocks] = lock[i];
886 			result[nlocks].l_pid = pid[i];
887 			nlocks++;
888 		}
889 
890 		ATF_CHECK_EQ(nlocks, __arraycount(expect));
891 
892 		qsort(result, nlocks, sizeof(result[0]), &flock_compare);
893 
894 		for (j = 0; j < nlocks; j++) {
895 			ATF_CHECK_EQ(result[j].l_start,  expect[j].l_start );
896 			ATF_CHECK_EQ(result[j].l_len,    expect[j].l_len   );
897 			ATF_CHECK_EQ(result[j].l_pid,    expect[j].l_pid   );
898 			ATF_CHECK_EQ(result[j].l_type,   expect[j].l_type  );
899 			ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
900 		}
901 	}
902 
903 	/*
904 	 * Release processes.  This also releases the fds and locks
905 	 * making fs unmount possible
906 	 */
907 	for (i = 0; i < __arraycount(lwp); i++) {
908 		rump_pub_lwproc_switch(lwp[i]);
909 		rump_pub_lwproc_releaselwp();
910 	}
911 
912 	FSTEST_EXIT();
913 }
914 
915 static void
916 access_simple(const atf_tc_t *tc, const char *mp)
917 {
918 	int fd;
919 	int tmode;
920 
921 	FSTEST_ENTER();
922 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
923 	RL(rump_sys_close(fd));
924 
925 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
926 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
927 		tmode = F_OK;
928 	else
929 		tmode = ALLACC;
930 
931 	RL(rump_sys_access("tfile", tmode));
932 
933 	/* PR kern/44648 */
934 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
935 #undef ALLACC
936 	FSTEST_EXIT();
937 }
938 
939 static void
940 read_directory(const atf_tc_t *tc, const char *mp)
941 {
942 	char buf[1024];
943 	int fd, res;
944 	ssize_t size;
945 
946 	FSTEST_ENTER();
947 	fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777);
948 	ATF_REQUIRE(fd != -1);
949 
950 	size = rump_sys_pread(fd, buf, sizeof(buf), 0);
951 	ATF_CHECK(size != -1 || errno == EISDIR);
952 	size = rump_sys_read(fd, buf, sizeof(buf));
953 	ATF_CHECK(size != -1 || errno == EISDIR);
954 
955 	res = rump_sys_close(fd);
956 	ATF_REQUIRE(res != -1);
957 	FSTEST_EXIT();
958 }
959 
960 static void
961 lstat_symlink(const atf_tc_t *tc, const char *mp)
962 {
963 	const char *src, *dst;
964 	int res;
965 	struct stat st;
966 
967 	USES_SYMLINKS;
968 
969 	FSTEST_ENTER();
970 
971 	src = "source";
972 	dst = "destination";
973 
974 	res = rump_sys_symlink(src, dst);
975 	ATF_REQUIRE(res != -1);
976 	res = rump_sys_lstat(dst, &st);
977 	ATF_REQUIRE(res != -1);
978 
979 	ATF_CHECK(S_ISLNK(st.st_mode) != 0);
980 	ATF_CHECK(st.st_size == (off_t)strlen(src));
981 
982 	FSTEST_EXIT();
983 }
984 
985 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
986 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
987 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
988 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
989 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
990 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
991 "(PR kern/44288)");
992 ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
993 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
994 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
995 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
996 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
997 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with target of length 0");
998 ATF_TC_FSAPPLY(symlink_long, "symlink with target of length > 0");
999 ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
1000 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
1001 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
1002 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
1003 ATF_TC_FSAPPLY(access_simple, "access(2)");
1004 ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
1005 ATF_TC_FSAPPLY(lstat_symlink, "lstat(2) values for symbolic links");
1006 
1007 #undef FSTEST_IMGSIZE
1008 #define FSTEST_IMGSIZE (1024*1024*64)
1009 ATF_TC_FSAPPLY(create_many, "create many directory entries");
1010 
1011 ATF_TP_ADD_TCS(tp)
1012 {
1013 
1014 	ATF_TP_FSAPPLY(lookup_simple);
1015 	ATF_TP_FSAPPLY(lookup_complex);
1016 	ATF_TP_FSAPPLY(dir_simple);
1017 	ATF_TP_FSAPPLY(dir_notempty);
1018 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
1019 	ATF_TP_FSAPPLY(rename_dir);
1020 	ATF_TP_FSAPPLY(rename_dotdot);
1021 	ATF_TP_FSAPPLY(rename_reg_nodir);
1022 	ATF_TP_FSAPPLY(create_many);
1023 	ATF_TP_FSAPPLY(create_nametoolong);
1024 	ATF_TP_FSAPPLY(create_exist);
1025 	ATF_TP_FSAPPLY(rename_nametoolong);
1026 	ATF_TP_FSAPPLY(symlink_zerolen);
1027 	ATF_TP_FSAPPLY(symlink_long);
1028 	ATF_TP_FSAPPLY(symlink_root);
1029 	ATF_TP_FSAPPLY(attrs);
1030 	ATF_TP_FSAPPLY(fcntl_lock);
1031 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
1032 	ATF_TP_FSAPPLY(access_simple);
1033 	ATF_TP_FSAPPLY(read_directory);
1034 	ATF_TP_FSAPPLY(lstat_symlink);
1035 
1036 	return atf_no_error();
1037 }
1038