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