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