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