xref: /netbsd-src/tests/fs/vfs/t_vnops.c (revision 7788a0781fe6ff2cce37368b4578a7ade0850cb1)
1 /*	$NetBSD: t_vnops.c,v 1.37 2013/07/28 09:03:43 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 <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 	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename("file2", NULL) == -1);
404 	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename(NULL, "file2") == -1);
405 
406 	rump_sys_chdir("/");
407 }
408 
409 static void
410 create_nametoolong(const atf_tc_t *tc, const char *mp)
411 {
412 	char *name;
413 	int fd;
414 	long val;
415 	size_t len;
416 
417 	if (rump_sys_chdir(mp) == -1)
418 		atf_tc_fail_errno("chdir mountpoint");
419 
420 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
421 	if (val == -1)
422 		atf_tc_fail_errno("pathconf");
423 
424 	len = val + 1;
425 	name = malloc(len+1);
426 	if (name == NULL)
427 		atf_tc_fail_errno("malloc");
428 
429 	memset(name, 'a', len);
430 	*(name+len) = '\0';
431 
432 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
433 	if (val == -1)
434 		atf_tc_fail_errno("pathconf");
435 
436 	fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
437 	if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
438 		atf_tc_fail_errno("open");
439 
440 	if (val == 0 && rump_sys_close(fd) == -1)
441 		atf_tc_fail_errno("close");
442 	if (val == 0 && rump_sys_unlink(name) == -1)
443 		atf_tc_fail_errno("unlink");
444 
445 	free(name);
446 
447 	rump_sys_chdir("/");
448 }
449 
450 static void
451 create_exist(const atf_tc_t *tc, const char *mp)
452 {
453 	const char *name = "hoge";
454 	int fd;
455 
456 	RL(rump_sys_chdir(mp));
457 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
458 	RL(rump_sys_close(fd));
459 	RL(rump_sys_unlink(name));
460 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
461 	RL(rump_sys_close(fd));
462 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
463 	RL(rump_sys_close(fd));
464 	ATF_REQUIRE_ERRNO(EEXIST,
465 	    (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
466 	RL(rump_sys_unlink(name));
467 	RL(rump_sys_chdir("/"));
468 }
469 
470 static void
471 rename_nametoolong(const atf_tc_t *tc, const char *mp)
472 {
473 	char *name;
474 	int res, fd;
475 	long val;
476 	size_t len;
477 
478 	if (FSTYPE_RUMPFS(tc))
479 		atf_tc_skip("rename not supported by file system");
480 
481 	if (rump_sys_chdir(mp) == -1)
482 		atf_tc_fail_errno("chdir mountpoint");
483 
484 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
485 	if (val == -1)
486 		atf_tc_fail_errno("pathconf");
487 
488 	len = val + 1;
489 	name = malloc(len+1);
490 	if (name == NULL)
491 		atf_tc_fail_errno("malloc");
492 
493 	memset(name, 'a', len);
494 	*(name+len) = '\0';
495 
496 	fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
497 	if (fd == -1)
498 		atf_tc_fail_errno("open");
499 	if (rump_sys_close(fd) == -1)
500 		atf_tc_fail_errno("close");
501 
502 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
503 	if (val == -1)
504 		atf_tc_fail_errno("pathconf");
505 
506 	res = rump_sys_rename("dummy", name);
507 	if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
508 		atf_tc_fail_errno("rename");
509 
510 	if (val == 0 && rump_sys_unlink(name) == -1)
511 		atf_tc_fail_errno("unlink");
512 
513 	free(name);
514 
515 	rump_sys_chdir("/");
516 }
517 
518 static void
519 symlink_zerolen(const atf_tc_t *tc, const char *mp)
520 {
521 
522 	USES_SYMLINKS;
523 
524 	RL(rump_sys_chdir(mp));
525 
526 	RL(rump_sys_symlink("", "afile"));
527 	RL(rump_sys_chdir("/"));
528 }
529 
530 static void
531 symlink_root(const atf_tc_t *tc, const char *mp)
532 {
533 
534 	USES_SYMLINKS;
535 
536 	RL(rump_sys_chdir(mp));
537 	RL(rump_sys_symlink("/", "foo"));
538 	RL(rump_sys_chdir("foo"));
539 }
540 
541 static void
542 attrs(const atf_tc_t *tc, const char *mp)
543 {
544 	struct stat sb, sb2;
545 	struct timeval tv[2];
546 	int fd;
547 
548 	FSTEST_ENTER();
549 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
550 	RL(rump_sys_close(fd));
551 	RL(rump_sys_stat(TESTFILE, &sb));
552 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
553 		RL(rump_sys_chown(TESTFILE, 1, 2));
554 		sb.st_uid = 1;
555 		sb.st_gid = 2;
556 		RL(rump_sys_chmod(TESTFILE, 0123));
557 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
558 	}
559 
560 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
561 	tv[0].tv_usec = 1;
562 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
563 	tv[1].tv_usec = 3;
564 	RL(rump_sys_utimes(TESTFILE, tv));
565 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
566 	sb.st_atimespec.tv_sec = 1000000000;
567 	sb.st_atimespec.tv_nsec = 1000;
568 	sb.st_mtimespec.tv_sec = 1000000002;
569 	sb.st_mtimespec.tv_nsec = 3000;
570 
571 	RL(rump_sys_stat(TESTFILE, &sb2));
572 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
573 	if (FSTYPE_ZFS(tc))
574 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
575 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
576 		CHECK(st_uid);
577 		CHECK(st_gid);
578 		CHECK(st_mode);
579 	}
580 	if (!FSTYPE_MSDOS(tc)) {
581 		/* msdosfs has only access date, not time */
582 		CHECK(st_atimespec.tv_sec);
583 	}
584 	CHECK(st_mtimespec.tv_sec);
585 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
586 	      FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
587 		CHECK(st_atimespec.tv_nsec);
588 		CHECK(st_mtimespec.tv_nsec);
589 	}
590 #undef  CHECK
591 
592 	FSTEST_EXIT();
593 }
594 
595 static void
596 fcntl_lock(const atf_tc_t *tc, const char *mp)
597 {
598 	int fd, fd2;
599 	struct flock l;
600 	struct lwp *lwp1, *lwp2;
601 
602 	FSTEST_ENTER();
603 	l.l_pid = 0;
604 	l.l_start = l.l_len = 1024;
605 	l.l_type = F_RDLCK | F_WRLCK;
606 	l.l_whence = SEEK_END;
607 
608 	lwp1 = rump_pub_lwproc_curlwp();
609 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
610 	RL(rump_sys_ftruncate(fd, 8192));
611 
612 	/* PR kern/43321 */
613 	if (FSTYPE_ZFS(tc))
614 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
615 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
616 
617 	/* Next, we fork and try to lock the same area */
618 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
619 	lwp2 = rump_pub_lwproc_curlwp();
620 	RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
621 	ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
622 
623 	/* Switch back and unlock... */
624 	rump_pub_lwproc_switch(lwp1);
625 	l.l_type = F_UNLCK;
626 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
627 
628 	/* ... and try to lock again */
629 	rump_pub_lwproc_switch(lwp2);
630 	l.l_type = F_RDLCK | F_WRLCK;
631 	RL(rump_sys_fcntl(fd2, F_SETLK, &l));
632 
633 	RL(rump_sys_close(fd2));
634 	rump_pub_lwproc_releaselwp();
635 
636 	RL(rump_sys_close(fd));
637 
638 	FSTEST_EXIT();
639 }
640 
641 static int
642 flock_compare(const void *p, const void *q)
643 {
644 	int a = ((const struct flock *)p)->l_start;
645 	int b = ((const struct flock *)q)->l_start;
646 	return a < b ? -1 : (a > b ? 1 : 0);
647 }
648 
649 /*
650  * Find all locks set by fcntl_getlock_pids test
651  * using GETLK for a range [start, start+end], and,
652  * if there is a blocking lock, recursively find
653  * all locks to the left (toward the beginning of
654  * a file) and to the right of the lock.
655  * The function also understands "until end of file"
656  * convention when len==0.
657  */
658 static unsigned int
659 fcntl_getlocks(int fildes, off_t start, off_t len,
660     struct flock *lock, struct flock *end)
661 {
662 	unsigned int rv = 0;
663 	const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
664 
665 	if (lock == end)
666 		return rv;
667 
668 	RL(rump_sys_fcntl(fildes, F_GETLK, &l));
669 
670 	if (l.l_type == F_UNLCK)
671 		return rv;
672 
673 	*lock++ = l;
674 	rv += 1;
675 
676 	ATF_REQUIRE(l.l_whence == SEEK_SET);
677 
678 	if (l.l_start > start) {
679 		unsigned int n =
680 		    fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
681 		rv += n;
682 		lock += n;
683 		if (lock == end)
684 			return rv;
685 	}
686 
687 	if (l.l_len == 0) /* does l spans until the end? */
688 		return rv;
689 
690 	if (len == 0) /* are we looking for locks until the end? */ {
691 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
692 	} else if (l.l_start + l.l_len < start + len) {
693 		len -= l.l_start + l.l_len - start;
694 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
695 	}
696 
697 	return rv;
698 }
699 
700 static void
701 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
702 {
703 	/* test non-overlaping ranges */
704 	struct flock expect[4];
705 	const struct flock lock[4] = {
706 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
707 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
708 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
709 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
710 	};
711 
712     /* Add extra element to make sure recursion does't stop at array end */
713 	struct flock result[5];
714 
715 	/* Add 5th process */
716 	int fd[5];
717 	pid_t pid[5];
718 	struct lwp *lwp[5];
719 
720 	unsigned int i, j;
721 	const off_t sz = 8192;
722 	int omode  = 0755;
723 	int oflags = O_RDWR | O_CREAT;
724 
725 	memcpy(expect, lock, sizeof(lock));
726 
727 	FSTEST_ENTER();
728 
729 	/*
730 	 * First, we create 4 processes and let each lock a range of the
731 	 * file.  Note that the third and fourth processes lock in
732 	 * "reverse" order, i.e. the greater pid locks a range before
733 	 * the lesser pid.
734 	 * Then, we create 5th process which doesn't lock anything.
735 	 */
736 	for (i = 0; i < __arraycount(lwp); i++) {
737 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
738 
739 		lwp[i] = rump_pub_lwproc_curlwp();
740 		pid[i] = rump_sys_getpid();
741 
742 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
743 		oflags = O_RDWR;
744 		omode  = 0;
745 
746 		RL(rump_sys_ftruncate(fd[i], sz));
747 
748 		if (FSTYPE_ZFS(tc))
749 			atf_tc_expect_fail("PR kern/47656: Test known to be "
750 			    "broken");
751 		if (i < __arraycount(lock)) {
752 			RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
753 			expect[i].l_pid = pid[i];
754 		}
755 	}
756 
757 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
758 
759 	/*
760 	 * In the context of each process, recursively find all locks
761 	 * that would block the current process. Processes 1-4 don't
762 	 * see their own lock, we insert it to simplify checks.
763 	 * Process 5 sees all 4 locks.
764 	 */
765 	for (i = 0; i < __arraycount(lwp); i++) {
766 		unsigned int nlocks;
767 
768 		rump_pub_lwproc_switch(lwp[i]);
769 
770 		memset(result, 0, sizeof(result));
771 		nlocks = fcntl_getlocks(fd[i], 0, sz,
772 		    result, result + __arraycount(result));
773 
774 		if (i < __arraycount(lock)) {
775 			ATF_REQUIRE(nlocks < __arraycount(result));
776 			result[nlocks] = lock[i];
777 			result[nlocks].l_pid = pid[i];
778 			nlocks++;
779 		}
780 
781 		ATF_CHECK_EQ(nlocks, __arraycount(expect));
782 
783 		qsort(result, nlocks, sizeof(result[0]), &flock_compare);
784 
785 		for (j = 0; j < nlocks; j++) {
786 			ATF_CHECK_EQ(result[j].l_start,  expect[j].l_start );
787 			ATF_CHECK_EQ(result[j].l_len,    expect[j].l_len   );
788 			ATF_CHECK_EQ(result[j].l_pid,    expect[j].l_pid   );
789 			ATF_CHECK_EQ(result[j].l_type,   expect[j].l_type  );
790 			ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
791 		}
792 	}
793 
794 	/*
795 	 * Release processes.  This also releases the fds and locks
796 	 * making fs unmount possible
797 	 */
798 	for (i = 0; i < __arraycount(lwp); i++) {
799 		rump_pub_lwproc_switch(lwp[i]);
800 		rump_pub_lwproc_releaselwp();
801 	}
802 
803 	FSTEST_EXIT();
804 }
805 
806 static void
807 access_simple(const atf_tc_t *tc, const char *mp)
808 {
809 	int fd;
810 	int tmode;
811 
812 	FSTEST_ENTER();
813 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
814 	RL(rump_sys_close(fd));
815 
816 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
817 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
818 		tmode = F_OK;
819 	else
820 		tmode = ALLACC;
821 
822 	RL(rump_sys_access("tfile", tmode));
823 
824 	/* PR kern/44648 */
825 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
826 #undef ALLACC
827 	FSTEST_EXIT();
828 }
829 
830 static void
831 read_directory(const atf_tc_t *tc, const char *mp)
832 {
833 	char buf[1024];
834 	int fd, res;
835 	ssize_t size;
836 
837 	FSTEST_ENTER();
838 	fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777);
839 	ATF_REQUIRE(fd != -1);
840 
841 	size = rump_sys_pread(fd, buf, sizeof(buf), 0);
842 	ATF_CHECK(size != -1 || errno == EISDIR);
843 	size = rump_sys_read(fd, buf, sizeof(buf));
844 	ATF_CHECK(size != -1 || errno == EISDIR);
845 
846 	res = rump_sys_close(fd);
847 	ATF_REQUIRE(res != -1);
848 	FSTEST_EXIT();
849 }
850 
851 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
852 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
853 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
854 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
855 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
856 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
857 "(PR kern/44288)");
858 ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
859 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
860 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
861 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
862 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
863 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
864 ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
865 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
866 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
867 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
868 ATF_TC_FSAPPLY(access_simple, "access(2)");
869 ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
870 
871 ATF_TP_ADD_TCS(tp)
872 {
873 
874 	ATF_TP_FSAPPLY(lookup_simple);
875 	ATF_TP_FSAPPLY(lookup_complex);
876 	ATF_TP_FSAPPLY(dir_simple);
877 	ATF_TP_FSAPPLY(dir_notempty);
878 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
879 	ATF_TP_FSAPPLY(rename_dir);
880 	ATF_TP_FSAPPLY(rename_dotdot);
881 	ATF_TP_FSAPPLY(rename_reg_nodir);
882 	ATF_TP_FSAPPLY(create_nametoolong);
883 	ATF_TP_FSAPPLY(create_exist);
884 	ATF_TP_FSAPPLY(rename_nametoolong);
885 	ATF_TP_FSAPPLY(symlink_zerolen);
886 	ATF_TP_FSAPPLY(symlink_root);
887 	ATF_TP_FSAPPLY(attrs);
888 	ATF_TP_FSAPPLY(fcntl_lock);
889 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
890 	ATF_TP_FSAPPLY(access_simple);
891 	ATF_TP_FSAPPLY(read_directory);
892 
893 	return atf_no_error();
894 }
895