Home | History | Annotate | Line # | Download | only in vfs
t_vnops.c revision 1.40.2.1
      1 /*	$NetBSD: t_vnops.c,v 1.40.2.1 2015/01/17 11:51:33 martin 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 static void
    555 symlink_zerolen(const atf_tc_t *tc, const char *mp)
    556 {
    557 
    558 	USES_SYMLINKS;
    559 
    560 	RL(rump_sys_chdir(mp));
    561 
    562 	RL(rump_sys_symlink("", "afile"));
    563 	RL(rump_sys_chdir("/"));
    564 }
    565 
    566 static void
    567 symlink_root(const atf_tc_t *tc, const char *mp)
    568 {
    569 
    570 	USES_SYMLINKS;
    571 
    572 	RL(rump_sys_chdir(mp));
    573 	RL(rump_sys_symlink("/", "foo"));
    574 	RL(rump_sys_chdir("foo"));
    575 }
    576 
    577 static void
    578 attrs(const atf_tc_t *tc, const char *mp)
    579 {
    580 	struct stat sb, sb2;
    581 	struct timeval tv[2];
    582 	int fd;
    583 
    584 	FSTEST_ENTER();
    585 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    586 	RL(rump_sys_close(fd));
    587 	RL(rump_sys_stat(TESTFILE, &sb));
    588 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    589 		RL(rump_sys_chown(TESTFILE, 1, 2));
    590 		sb.st_uid = 1;
    591 		sb.st_gid = 2;
    592 		RL(rump_sys_chmod(TESTFILE, 0123));
    593 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
    594 	}
    595 
    596 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
    597 	tv[0].tv_usec = 1;
    598 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
    599 	tv[1].tv_usec = 3;
    600 	RL(rump_sys_utimes(TESTFILE, tv));
    601 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
    602 	sb.st_atimespec.tv_sec = 1000000000;
    603 	sb.st_atimespec.tv_nsec = 1000;
    604 	sb.st_mtimespec.tv_sec = 1000000002;
    605 	sb.st_mtimespec.tv_nsec = 3000;
    606 
    607 	RL(rump_sys_stat(TESTFILE, &sb2));
    608 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
    609 	if (FSTYPE_ZFS(tc))
    610 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
    611 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    612 		CHECK(st_uid);
    613 		CHECK(st_gid);
    614 		CHECK(st_mode);
    615 	}
    616 	if (!FSTYPE_MSDOS(tc)) {
    617 		/* msdosfs has only access date, not time */
    618 		CHECK(st_atimespec.tv_sec);
    619 	}
    620 	CHECK(st_mtimespec.tv_sec);
    621 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
    622 	      FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
    623 		CHECK(st_atimespec.tv_nsec);
    624 		CHECK(st_mtimespec.tv_nsec);
    625 	}
    626 #undef  CHECK
    627 
    628 	FSTEST_EXIT();
    629 }
    630 
    631 static void
    632 fcntl_lock(const atf_tc_t *tc, const char *mp)
    633 {
    634 	int fd, fd2;
    635 	struct flock l;
    636 	struct lwp *lwp1, *lwp2;
    637 
    638 	FSTEST_ENTER();
    639 	l.l_pid = 0;
    640 	l.l_start = l.l_len = 1024;
    641 	l.l_type = F_RDLCK | F_WRLCK;
    642 	l.l_whence = SEEK_END;
    643 
    644 	lwp1 = rump_pub_lwproc_curlwp();
    645 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    646 	RL(rump_sys_ftruncate(fd, 8192));
    647 
    648 	/* PR kern/43321 */
    649 	if (FSTYPE_ZFS(tc))
    650 		atf_tc_expect_fail("PR kern/47656: Test known to be broken");
    651 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
    652 
    653 	/* Next, we fork and try to lock the same area */
    654 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    655 	lwp2 = rump_pub_lwproc_curlwp();
    656 	RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
    657 	ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
    658 
    659 	/* Switch back and unlock... */
    660 	rump_pub_lwproc_switch(lwp1);
    661 	l.l_type = F_UNLCK;
    662 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
    663 
    664 	/* ... and try to lock again */
    665 	rump_pub_lwproc_switch(lwp2);
    666 	l.l_type = F_RDLCK | F_WRLCK;
    667 	RL(rump_sys_fcntl(fd2, F_SETLK, &l));
    668 
    669 	RL(rump_sys_close(fd2));
    670 	rump_pub_lwproc_releaselwp();
    671 
    672 	RL(rump_sys_close(fd));
    673 
    674 	FSTEST_EXIT();
    675 }
    676 
    677 static int
    678 flock_compare(const void *p, const void *q)
    679 {
    680 	int a = ((const struct flock *)p)->l_start;
    681 	int b = ((const struct flock *)q)->l_start;
    682 	return a < b ? -1 : (a > b ? 1 : 0);
    683 }
    684 
    685 /*
    686  * Find all locks set by fcntl_getlock_pids test
    687  * using GETLK for a range [start, start+end], and,
    688  * if there is a blocking lock, recursively find
    689  * all locks to the left (toward the beginning of
    690  * a file) and to the right of the lock.
    691  * The function also understands "until end of file"
    692  * convention when len==0.
    693  */
    694 static unsigned int
    695 fcntl_getlocks(int fildes, off_t start, off_t len,
    696     struct flock *lock, struct flock *end)
    697 {
    698 	unsigned int rv = 0;
    699 	const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
    700 
    701 	if (lock == end)
    702 		return rv;
    703 
    704 	RL(rump_sys_fcntl(fildes, F_GETLK, &l));
    705 
    706 	if (l.l_type == F_UNLCK)
    707 		return rv;
    708 
    709 	*lock++ = l;
    710 	rv += 1;
    711 
    712 	ATF_REQUIRE(l.l_whence == SEEK_SET);
    713 
    714 	if (l.l_start > start) {
    715 		unsigned int n =
    716 		    fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
    717 		rv += n;
    718 		lock += n;
    719 		if (lock == end)
    720 			return rv;
    721 	}
    722 
    723 	if (l.l_len == 0) /* does l spans until the end? */
    724 		return rv;
    725 
    726 	if (len == 0) /* are we looking for locks until the end? */ {
    727 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
    728 	} else if (l.l_start + l.l_len < start + len) {
    729 		len -= l.l_start + l.l_len - start;
    730 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
    731 	}
    732 
    733 	return rv;
    734 }
    735 
    736 static void
    737 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
    738 {
    739 	/* test non-overlaping ranges */
    740 	struct flock expect[4];
    741 	const struct flock lock[4] = {
    742 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
    743 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
    744 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
    745 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
    746 	};
    747 
    748     /* Add extra element to make sure recursion does't stop at array end */
    749 	struct flock result[5];
    750 
    751 	/* Add 5th process */
    752 	int fd[5];
    753 	pid_t pid[5];
    754 	struct lwp *lwp[5];
    755 
    756 	unsigned int i, j;
    757 	const off_t sz = 8192;
    758 	int omode  = 0755;
    759 	int oflags = O_RDWR | O_CREAT;
    760 
    761 	memcpy(expect, lock, sizeof(lock));
    762 
    763 	FSTEST_ENTER();
    764 
    765 	/*
    766 	 * First, we create 4 processes and let each lock a range of the
    767 	 * file.  Note that the third and fourth processes lock in
    768 	 * "reverse" order, i.e. the greater pid locks a range before
    769 	 * the lesser pid.
    770 	 * Then, we create 5th process which doesn't lock anything.
    771 	 */
    772 	for (i = 0; i < __arraycount(lwp); i++) {
    773 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    774 
    775 		lwp[i] = rump_pub_lwproc_curlwp();
    776 		pid[i] = rump_sys_getpid();
    777 
    778 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
    779 		oflags = O_RDWR;
    780 		omode  = 0;
    781 
    782 		RL(rump_sys_ftruncate(fd[i], sz));
    783 
    784 		if (FSTYPE_ZFS(tc))
    785 			atf_tc_expect_fail("PR kern/47656: Test known to be "
    786 			    "broken");
    787 		if (i < __arraycount(lock)) {
    788 			RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
    789 			expect[i].l_pid = pid[i];
    790 		}
    791 	}
    792 
    793 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
    794 
    795 	/*
    796 	 * In the context of each process, recursively find all locks
    797 	 * that would block the current process. Processes 1-4 don't
    798 	 * see their own lock, we insert it to simplify checks.
    799 	 * Process 5 sees all 4 locks.
    800 	 */
    801 	for (i = 0; i < __arraycount(lwp); i++) {
    802 		unsigned int nlocks;
    803 
    804 		rump_pub_lwproc_switch(lwp[i]);
    805 
    806 		memset(result, 0, sizeof(result));
    807 		nlocks = fcntl_getlocks(fd[i], 0, sz,
    808 		    result, result + __arraycount(result));
    809 
    810 		if (i < __arraycount(lock)) {
    811 			ATF_REQUIRE(nlocks < __arraycount(result));
    812 			result[nlocks] = lock[i];
    813 			result[nlocks].l_pid = pid[i];
    814 			nlocks++;
    815 		}
    816 
    817 		ATF_CHECK_EQ(nlocks, __arraycount(expect));
    818 
    819 		qsort(result, nlocks, sizeof(result[0]), &flock_compare);
    820 
    821 		for (j = 0; j < nlocks; j++) {
    822 			ATF_CHECK_EQ(result[j].l_start,  expect[j].l_start );
    823 			ATF_CHECK_EQ(result[j].l_len,    expect[j].l_len   );
    824 			ATF_CHECK_EQ(result[j].l_pid,    expect[j].l_pid   );
    825 			ATF_CHECK_EQ(result[j].l_type,   expect[j].l_type  );
    826 			ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
    827 		}
    828 	}
    829 
    830 	/*
    831 	 * Release processes.  This also releases the fds and locks
    832 	 * making fs unmount possible
    833 	 */
    834 	for (i = 0; i < __arraycount(lwp); i++) {
    835 		rump_pub_lwproc_switch(lwp[i]);
    836 		rump_pub_lwproc_releaselwp();
    837 	}
    838 
    839 	FSTEST_EXIT();
    840 }
    841 
    842 static void
    843 access_simple(const atf_tc_t *tc, const char *mp)
    844 {
    845 	int fd;
    846 	int tmode;
    847 
    848 	FSTEST_ENTER();
    849 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
    850 	RL(rump_sys_close(fd));
    851 
    852 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
    853 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
    854 		tmode = F_OK;
    855 	else
    856 		tmode = ALLACC;
    857 
    858 	RL(rump_sys_access("tfile", tmode));
    859 
    860 	/* PR kern/44648 */
    861 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
    862 #undef ALLACC
    863 	FSTEST_EXIT();
    864 }
    865 
    866 static void
    867 read_directory(const atf_tc_t *tc, const char *mp)
    868 {
    869 	char buf[1024];
    870 	int fd, res;
    871 	ssize_t size;
    872 
    873 	FSTEST_ENTER();
    874 	fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777);
    875 	ATF_REQUIRE(fd != -1);
    876 
    877 	size = rump_sys_pread(fd, buf, sizeof(buf), 0);
    878 	ATF_CHECK(size != -1 || errno == EISDIR);
    879 	size = rump_sys_read(fd, buf, sizeof(buf));
    880 	ATF_CHECK(size != -1 || errno == EISDIR);
    881 
    882 	res = rump_sys_close(fd);
    883 	ATF_REQUIRE(res != -1);
    884 	FSTEST_EXIT();
    885 }
    886 
    887 static void
    888 lstat_symlink(const atf_tc_t *tc, const char *mp)
    889 {
    890 	const char *src, *dst;
    891 	int res;
    892 	struct stat st;
    893 
    894 	USES_SYMLINKS;
    895 	FSTEST_ENTER();
    896 
    897 	src = "source";
    898 	dst = "destination";
    899 
    900 	res = rump_sys_symlink(src, dst);
    901 	ATF_REQUIRE(res != -1);
    902 	res = rump_sys_lstat(dst, &st);
    903 	ATF_REQUIRE(res != -1);
    904 
    905 	ATF_CHECK(S_ISLNK(st.st_mode) != 0);
    906 	ATF_CHECK(st.st_size == (off_t)strlen(src));
    907 
    908 	FSTEST_EXIT();
    909 }
    910 
    911 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
    912 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
    913 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
    914 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
    915 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
    916 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
    917 "(PR kern/44288)");
    918 ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
    919 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
    920 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
    921 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
    922 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
    923 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
    924 ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
    925 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
    926 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
    927 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
    928 ATF_TC_FSAPPLY(access_simple, "access(2)");
    929 ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
    930 ATF_TC_FSAPPLY(lstat_symlink, "lstat(2) values for symbolic links");
    931 
    932 ATF_TP_ADD_TCS(tp)
    933 {
    934 
    935 	ATF_TP_FSAPPLY(lookup_simple);
    936 	ATF_TP_FSAPPLY(lookup_complex);
    937 	ATF_TP_FSAPPLY(dir_simple);
    938 	ATF_TP_FSAPPLY(dir_notempty);
    939 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
    940 	ATF_TP_FSAPPLY(rename_dir);
    941 	ATF_TP_FSAPPLY(rename_dotdot);
    942 	ATF_TP_FSAPPLY(rename_reg_nodir);
    943 	ATF_TP_FSAPPLY(create_nametoolong);
    944 	ATF_TP_FSAPPLY(create_exist);
    945 	ATF_TP_FSAPPLY(rename_nametoolong);
    946 	ATF_TP_FSAPPLY(symlink_zerolen);
    947 	ATF_TP_FSAPPLY(symlink_root);
    948 	ATF_TP_FSAPPLY(attrs);
    949 	ATF_TP_FSAPPLY(fcntl_lock);
    950 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
    951 	ATF_TP_FSAPPLY(access_simple);
    952 	ATF_TP_FSAPPLY(read_directory);
    953 	ATF_TP_FSAPPLY(lstat_symlink);
    954 
    955 	return atf_no_error();
    956 }
    957