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