xref: /netbsd-src/tests/fs/vfs/t_vnops.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: t_vnops.c,v 1.52 2016/01/02 12:11:30 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 static void
442 create_many(const atf_tc_t *tc, const char *mp)
443 {
444 	char buf[64];
445 	int nfiles = 2324; /* #Nancy */
446 	int i;
447 
448 	if (FSTYPE_EXT2FS(tc))
449 		atf_tc_expect_fail("PR kern/50607");
450 
451 	if (FSTYPE_UDF(tc))
452 		atf_tc_expect_fail("PR kern/50608");
453 
454 	/* takes forever with many files */
455 	if (FSTYPE_MSDOS(tc))
456 		nfiles /= 4;
457 
458 	RL(rump_sys_chdir(mp));
459 
460 	if (FSTYPE_SYSVBFS(tc)) {
461 		/* fs doesn't support many files or subdirectories */
462 		nfiles = 5;
463 	} else {
464 		/* msdosfs doesn't like many entries in the root directory */
465 		RL(rump_sys_mkdir("subdir", 0777));
466 		RL(rump_sys_chdir("subdir"));
467 	}
468 
469 	/* create them */
470 #define TESTFN "testfile"
471 	for (i = 0; i < nfiles; i++) {
472 		int fd;
473 
474 		sprintf(buf, TESTFN "%d\n", i);
475 		RL(fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666));
476 		RL(rump_sys_close(fd));
477 	}
478 
479 	/* wipe them out */
480 	for (i = 0; i < nfiles; i++) {
481 		sprintf(buf, TESTFN "%d\n", i);
482 		RL(rump_sys_unlink(buf));
483 	}
484 #undef TESTFN
485 
486 	rump_sys_chdir("/");
487 }
488 
489 static void
490 create_nametoolong(const atf_tc_t *tc, const char *mp)
491 {
492 	char *name;
493 	int fd;
494 	long val;
495 	size_t len;
496 
497 	if (rump_sys_chdir(mp) == -1)
498 		atf_tc_fail_errno("chdir mountpoint");
499 
500 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
501 	if (val == -1)
502 		atf_tc_fail_errno("pathconf");
503 
504 	len = val + 1;
505 	name = malloc(len+1);
506 	if (name == NULL)
507 		atf_tc_fail_errno("malloc");
508 
509 	memset(name, 'a', len);
510 	*(name+len) = '\0';
511 
512 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
513 	if (val == -1)
514 		atf_tc_fail_errno("pathconf");
515 
516 	fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
517 	if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
518 		atf_tc_fail_errno("open");
519 
520 	if (val == 0 && rump_sys_close(fd) == -1)
521 		atf_tc_fail_errno("close");
522 	if (val == 0 && rump_sys_unlink(name) == -1)
523 		atf_tc_fail_errno("unlink");
524 
525 	free(name);
526 
527 	rump_sys_chdir("/");
528 }
529 
530 static void
531 create_exist(const atf_tc_t *tc, const char *mp)
532 {
533 	const char *name = "hoge";
534 	int fd;
535 
536 	RL(rump_sys_chdir(mp));
537 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
538 	RL(rump_sys_close(fd));
539 	RL(rump_sys_unlink(name));
540 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
541 	RL(rump_sys_close(fd));
542 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
543 	RL(rump_sys_close(fd));
544 	ATF_REQUIRE_ERRNO(EEXIST,
545 	    (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
546 	RL(rump_sys_unlink(name));
547 	RL(rump_sys_chdir("/"));
548 }
549 
550 static void
551 rename_nametoolong(const atf_tc_t *tc, const char *mp)
552 {
553 	char *name;
554 	int res, fd;
555 	long val;
556 	size_t len;
557 
558 	if (FSTYPE_RUMPFS(tc))
559 		atf_tc_skip("rename not supported by file system");
560 
561 	if (rump_sys_chdir(mp) == -1)
562 		atf_tc_fail_errno("chdir mountpoint");
563 
564 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
565 	if (val == -1)
566 		atf_tc_fail_errno("pathconf");
567 
568 	len = val + 1;
569 	name = malloc(len+1);
570 	if (name == NULL)
571 		atf_tc_fail_errno("malloc");
572 
573 	memset(name, 'a', len);
574 	*(name+len) = '\0';
575 
576 	fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
577 	if (fd == -1)
578 		atf_tc_fail_errno("open");
579 	if (rump_sys_close(fd) == -1)
580 		atf_tc_fail_errno("close");
581 
582 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
583 	if (val == -1)
584 		atf_tc_fail_errno("pathconf");
585 
586 	res = rump_sys_rename("dummy", name);
587 	if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
588 		atf_tc_fail_errno("rename");
589 
590 	if (val == 0 && rump_sys_unlink(name) == -1)
591 		atf_tc_fail_errno("unlink");
592 
593 	free(name);
594 
595 	rump_sys_chdir("/");
596 }
597 
598 /*
599  * Test creating a symlink whose length is "len" bytes, not including
600  * the terminating NUL.
601  */
602 static void
603 symlink_len(const atf_tc_t *tc, const char *mp, size_t len)
604 {
605 	char *buf;
606 	int r;
607 
608 	USES_SYMLINKS;
609 
610 	RL(rump_sys_chdir(mp));
611 
612 	buf = malloc(len + 1);
613 	ATF_REQUIRE(buf);
614 	memset(buf, 'a', len);
615 	buf[len] = '\0';
616 	r = rump_sys_symlink(buf, "afile");
617 	if (r == -1) {
618 		ATF_REQUIRE_ERRNO(ENAMETOOLONG, r);
619 	} else {
620 		RL(rump_sys_unlink("afile"));
621 	}
622 	free(buf);
623 
624 	RL(rump_sys_chdir("/"));
625 }
626 
627 static void
628 symlink_zerolen(const atf_tc_t *tc, const char *mp)
629 {
630 	symlink_len(tc, mp, 0);
631 }
632 
633 static void
634 symlink_long(const atf_tc_t *tc, const char *mp)
635 {
636 	/*
637 	 * Test lengths close to powers of two, as those are likely
638 	 * to be edge cases.
639 	 */
640 	size_t len;
641 	int fuzz;
642 	for (len = 2; len <= 65536; len *= 2) {
643 		for (fuzz = -1; fuzz <= 1; fuzz++) {
644 			symlink_len(tc, mp, len + fuzz);
645 		}
646 	}
647 }
648 
649 static void
650 symlink_root(const atf_tc_t *tc, const char *mp)
651 {
652 
653 	USES_SYMLINKS;
654 
655 	RL(rump_sys_chdir(mp));
656 	RL(rump_sys_symlink("/", "foo"));
657 	RL(rump_sys_chdir("foo"));
658 }
659 
660 static void
661 attrs(const atf_tc_t *tc, const char *mp)
662 {
663 	struct stat sb, sb2;
664 	struct timeval tv[2];
665 	int fd;
666 
667 	FSTEST_ENTER();
668 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
669 	RL(rump_sys_close(fd));
670 	RL(rump_sys_stat(TESTFILE, &sb));
671 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
672 		RL(rump_sys_chown(TESTFILE, 1, 2));
673 		sb.st_uid = 1;
674 		sb.st_gid = 2;
675 		RL(rump_sys_chmod(TESTFILE, 0123));
676 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
677 	}
678 
679 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
680 	tv[0].tv_usec = 1;
681 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
682 	tv[1].tv_usec = 3;
683 	RL(rump_sys_utimes(TESTFILE, tv));
684 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
685 	sb.st_atimespec.tv_sec = 1000000000;
686 	sb.st_atimespec.tv_nsec = 1000;
687 	sb.st_mtimespec.tv_sec = 1000000002;
688 	sb.st_mtimespec.tv_nsec = 3000;
689 
690 	RL(rump_sys_stat(TESTFILE, &sb2));
691 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
692 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
693 		CHECK(st_uid);
694 		CHECK(st_gid);
695 		CHECK(st_mode);
696 	}
697 	if (!FSTYPE_MSDOS(tc)) {
698 		/* msdosfs has only access date, not time */
699 		CHECK(st_atimespec.tv_sec);
700 	}
701 	CHECK(st_mtimespec.tv_sec);
702 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
703 	      FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
704 		CHECK(st_atimespec.tv_nsec);
705 		CHECK(st_mtimespec.tv_nsec);
706 	}
707 #undef  CHECK
708 
709 	FSTEST_EXIT();
710 }
711 
712 static void
713 fcntl_lock(const atf_tc_t *tc, const char *mp)
714 {
715 	int fd, fd2;
716 	struct flock l;
717 	struct lwp *lwp1, *lwp2;
718 
719 	FSTEST_ENTER();
720 	l.l_pid = 0;
721 	l.l_start = l.l_len = 1024;
722 	l.l_type = F_RDLCK | F_WRLCK;
723 	l.l_whence = SEEK_END;
724 
725 	lwp1 = rump_pub_lwproc_curlwp();
726 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
727 	RL(rump_sys_ftruncate(fd, 8192));
728 
729 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
730 
731 	/* Next, we fork and try to lock the same area */
732 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
733 	lwp2 = rump_pub_lwproc_curlwp();
734 	RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
735 	ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
736 
737 	/* Switch back and unlock... */
738 	rump_pub_lwproc_switch(lwp1);
739 	l.l_type = F_UNLCK;
740 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
741 
742 	/* ... and try to lock again */
743 	rump_pub_lwproc_switch(lwp2);
744 	l.l_type = F_RDLCK | F_WRLCK;
745 	RL(rump_sys_fcntl(fd2, F_SETLK, &l));
746 
747 	RL(rump_sys_close(fd2));
748 	rump_pub_lwproc_releaselwp();
749 
750 	RL(rump_sys_close(fd));
751 
752 	FSTEST_EXIT();
753 }
754 
755 static int
756 flock_compare(const void *p, const void *q)
757 {
758 	int a = ((const struct flock *)p)->l_start;
759 	int b = ((const struct flock *)q)->l_start;
760 	return a < b ? -1 : (a > b ? 1 : 0);
761 }
762 
763 /*
764  * Find all locks set by fcntl_getlock_pids test
765  * using GETLK for a range [start, start+end], and,
766  * if there is a blocking lock, recursively find
767  * all locks to the left (toward the beginning of
768  * a file) and to the right of the lock.
769  * The function also understands "until end of file"
770  * convention when len==0.
771  */
772 static unsigned int
773 fcntl_getlocks(int fildes, off_t start, off_t len,
774     struct flock *lock, struct flock *end)
775 {
776 	unsigned int rv = 0;
777 	const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
778 
779 	if (lock == end)
780 		return rv;
781 
782 	RL(rump_sys_fcntl(fildes, F_GETLK, &l));
783 
784 	if (l.l_type == F_UNLCK)
785 		return rv;
786 
787 	*lock++ = l;
788 	rv += 1;
789 
790 	ATF_REQUIRE(l.l_whence == SEEK_SET);
791 
792 	if (l.l_start > start) {
793 		unsigned int n =
794 		    fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
795 		rv += n;
796 		lock += n;
797 		if (lock == end)
798 			return rv;
799 	}
800 
801 	if (l.l_len == 0) /* does l spans until the end? */
802 		return rv;
803 
804 	if (len == 0) /* are we looking for locks until the end? */ {
805 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
806 	} else if (l.l_start + l.l_len < start + len) {
807 		len -= l.l_start + l.l_len - start;
808 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
809 	}
810 
811 	return rv;
812 }
813 
814 static void
815 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
816 {
817 	/* test non-overlaping ranges */
818 	struct flock expect[4];
819 	const struct flock lock[4] = {
820 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
821 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
822 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
823 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
824 	};
825 
826     /* Add extra element to make sure recursion does't stop at array end */
827 	struct flock result[5];
828 
829 	/* Add 5th process */
830 	int fd[5];
831 	pid_t pid[5];
832 	struct lwp *lwp[5];
833 
834 	unsigned int i, j;
835 	const off_t sz = 8192;
836 	int omode  = 0755;
837 	int oflags = O_RDWR | O_CREAT;
838 
839 	memcpy(expect, lock, sizeof(lock));
840 
841 	FSTEST_ENTER();
842 
843 	/*
844 	 * First, we create 4 processes and let each lock a range of the
845 	 * file.  Note that the third and fourth processes lock in
846 	 * "reverse" order, i.e. the greater pid locks a range before
847 	 * the lesser pid.
848 	 * Then, we create 5th process which doesn't lock anything.
849 	 */
850 	for (i = 0; i < __arraycount(lwp); i++) {
851 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
852 
853 		lwp[i] = rump_pub_lwproc_curlwp();
854 		pid[i] = rump_sys_getpid();
855 
856 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
857 		oflags = O_RDWR;
858 		omode  = 0;
859 
860 		RL(rump_sys_ftruncate(fd[i], sz));
861 
862 		if (i < __arraycount(lock)) {
863 			RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
864 			expect[i].l_pid = pid[i];
865 		}
866 	}
867 
868 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
869 
870 	/*
871 	 * In the context of each process, recursively find all locks
872 	 * that would block the current process. Processes 1-4 don't
873 	 * see their own lock, we insert it to simplify checks.
874 	 * Process 5 sees all 4 locks.
875 	 */
876 	for (i = 0; i < __arraycount(lwp); i++) {
877 		unsigned int nlocks;
878 
879 		rump_pub_lwproc_switch(lwp[i]);
880 
881 		memset(result, 0, sizeof(result));
882 		nlocks = fcntl_getlocks(fd[i], 0, sz,
883 		    result, result + __arraycount(result));
884 
885 		if (i < __arraycount(lock)) {
886 			ATF_REQUIRE(nlocks < __arraycount(result));
887 			result[nlocks] = lock[i];
888 			result[nlocks].l_pid = pid[i];
889 			nlocks++;
890 		}
891 
892 		ATF_CHECK_EQ(nlocks, __arraycount(expect));
893 
894 		qsort(result, nlocks, sizeof(result[0]), &flock_compare);
895 
896 		for (j = 0; j < nlocks; j++) {
897 			ATF_CHECK_EQ(result[j].l_start,  expect[j].l_start );
898 			ATF_CHECK_EQ(result[j].l_len,    expect[j].l_len   );
899 			ATF_CHECK_EQ(result[j].l_pid,    expect[j].l_pid   );
900 			ATF_CHECK_EQ(result[j].l_type,   expect[j].l_type  );
901 			ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
902 		}
903 	}
904 
905 	/*
906 	 * Release processes.  This also releases the fds and locks
907 	 * making fs unmount possible
908 	 */
909 	for (i = 0; i < __arraycount(lwp); i++) {
910 		rump_pub_lwproc_switch(lwp[i]);
911 		rump_pub_lwproc_releaselwp();
912 	}
913 
914 	FSTEST_EXIT();
915 }
916 
917 static void
918 access_simple(const atf_tc_t *tc, const char *mp)
919 {
920 	int fd;
921 	int tmode;
922 
923 	FSTEST_ENTER();
924 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
925 	RL(rump_sys_close(fd));
926 
927 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
928 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
929 		tmode = F_OK;
930 	else
931 		tmode = ALLACC;
932 
933 	RL(rump_sys_access("tfile", tmode));
934 
935 	/* PR kern/44648 */
936 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
937 #undef ALLACC
938 	FSTEST_EXIT();
939 }
940 
941 static void
942 read_directory(const atf_tc_t *tc, const char *mp)
943 {
944 	char buf[1024];
945 	int fd, res;
946 	ssize_t size;
947 
948 	FSTEST_ENTER();
949 	fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777);
950 	ATF_REQUIRE(fd != -1);
951 
952 	size = rump_sys_pread(fd, buf, sizeof(buf), 0);
953 	ATF_CHECK(size != -1 || errno == EISDIR);
954 	size = rump_sys_read(fd, buf, sizeof(buf));
955 	ATF_CHECK(size != -1 || errno == EISDIR);
956 
957 	res = rump_sys_close(fd);
958 	ATF_REQUIRE(res != -1);
959 	FSTEST_EXIT();
960 }
961 
962 static void
963 lstat_symlink(const atf_tc_t *tc, const char *mp)
964 {
965 	const char *src, *dst;
966 	int res;
967 	struct stat st;
968 
969 	USES_SYMLINKS;
970 
971 	FSTEST_ENTER();
972 
973 	src = "source";
974 	dst = "destination";
975 
976 	res = rump_sys_symlink(src, dst);
977 	ATF_REQUIRE(res != -1);
978 	res = rump_sys_lstat(dst, &st);
979 	ATF_REQUIRE(res != -1);
980 
981 	ATF_CHECK(S_ISLNK(st.st_mode) != 0);
982 	ATF_CHECK(st.st_size == (off_t)strlen(src));
983 
984 	FSTEST_EXIT();
985 }
986 
987 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
988 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
989 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
990 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
991 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
992 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
993 "(PR kern/44288)");
994 ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
995 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
996 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
997 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
998 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
999 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with target of length 0");
1000 ATF_TC_FSAPPLY(symlink_long, "symlink with target of length > 0");
1001 ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
1002 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
1003 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
1004 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
1005 ATF_TC_FSAPPLY(access_simple, "access(2)");
1006 ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
1007 ATF_TC_FSAPPLY(lstat_symlink, "lstat(2) values for symbolic links");
1008 
1009 #undef FSTEST_IMGSIZE
1010 #define FSTEST_IMGSIZE (1024*1024*64)
1011 ATF_TC_FSAPPLY(create_many, "create many directory entries");
1012 
1013 ATF_TP_ADD_TCS(tp)
1014 {
1015 
1016 	ATF_TP_FSAPPLY(lookup_simple);
1017 	ATF_TP_FSAPPLY(lookup_complex);
1018 	ATF_TP_FSAPPLY(dir_simple);
1019 	ATF_TP_FSAPPLY(dir_notempty);
1020 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
1021 	ATF_TP_FSAPPLY(rename_dir);
1022 	ATF_TP_FSAPPLY(rename_dotdot);
1023 	ATF_TP_FSAPPLY(rename_reg_nodir);
1024 	ATF_TP_FSAPPLY(create_many);
1025 	ATF_TP_FSAPPLY(create_nametoolong);
1026 	ATF_TP_FSAPPLY(create_exist);
1027 	ATF_TP_FSAPPLY(rename_nametoolong);
1028 	ATF_TP_FSAPPLY(symlink_zerolen);
1029 	ATF_TP_FSAPPLY(symlink_long);
1030 	ATF_TP_FSAPPLY(symlink_root);
1031 	ATF_TP_FSAPPLY(attrs);
1032 	ATF_TP_FSAPPLY(fcntl_lock);
1033 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
1034 	ATF_TP_FSAPPLY(access_simple);
1035 	ATF_TP_FSAPPLY(read_directory);
1036 	ATF_TP_FSAPPLY(lstat_symlink);
1037 
1038 	return atf_no_error();
1039 }
1040