Home | History | Annotate | Line # | Download | only in vfs
t_vnops.c revision 1.19
      1 /*	$NetBSD: t_vnops.c,v 1.19 2011/03/01 15:33:35 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)) atf_tc_skip("dirs not supported by file system")
     50 
     51 #define USES_SYMLINKS					\
     52     if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))		\
     53 	atf_tc_skip("symlinks not supported by file system")
     54 
     55 static char *
     56 md(char *buf, const char *base, const char *tail)
     57 {
     58 
     59 	sprintf(buf, "%s/%s", base, tail);
     60 	return buf;
     61 }
     62 
     63 static void
     64 lookup_simple(const atf_tc_t *tc, const char *mountpath)
     65 {
     66 	char pb[MAXPATHLEN], final[MAXPATHLEN];
     67 	struct stat sb1, sb2;
     68 
     69 	strcpy(final, mountpath);
     70 	sprintf(pb, "%s/../%s", mountpath, basename(final));
     71 	if (rump_sys_stat(pb, &sb1) == -1)
     72 		atf_tc_fail_errno("stat 1");
     73 
     74 	sprintf(pb, "%s/./../%s", mountpath, basename(final));
     75 	if (rump_sys_stat(pb, &sb2) == -1)
     76 		atf_tc_fail_errno("stat 2");
     77 
     78 	ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
     79 }
     80 
     81 static void
     82 lookup_complex(const atf_tc_t *tc, const char *mountpath)
     83 {
     84 	char pb[MAXPATHLEN];
     85 	struct stat sb1, sb2;
     86 
     87 	USES_DIRS;
     88 
     89 	sprintf(pb, "%s/dir", mountpath);
     90 	if (rump_sys_mkdir(pb, 0777) == -1)
     91 		atf_tc_fail_errno("mkdir");
     92 	if (rump_sys_stat(pb, &sb1) == -1)
     93 		atf_tc_fail_errno("stat 1");
     94 
     95 	sprintf(pb, "%s/./dir/../././dir/.", mountpath);
     96 	if (rump_sys_stat(pb, &sb2) == -1)
     97 		atf_tc_fail_errno("stat 2");
     98 
     99 	ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
    100 }
    101 
    102 static void
    103 dir_simple(const atf_tc_t *tc, const char *mountpath)
    104 {
    105 	char pb[MAXPATHLEN];
    106 	struct stat sb;
    107 
    108 	USES_DIRS;
    109 
    110 	/* check we can create directories */
    111 	sprintf(pb, "%s/dir", mountpath);
    112 	if (rump_sys_mkdir(pb, 0777) == -1)
    113 		atf_tc_fail_errno("mkdir");
    114 	if (rump_sys_stat(pb, &sb) == -1)
    115 		atf_tc_fail_errno("stat new directory");
    116 
    117 	/* check we can remove then and that it makes them unreachable */
    118 	if (rump_sys_rmdir(pb) == -1)
    119 		atf_tc_fail_errno("rmdir");
    120 	if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
    121 		atf_tc_fail("ENOENT expected from stat");
    122 }
    123 
    124 static void
    125 dir_notempty(const atf_tc_t *tc, const char *mountpath)
    126 {
    127 	char pb[MAXPATHLEN], pb2[MAXPATHLEN];
    128 	int fd, rv;
    129 
    130 	USES_DIRS;
    131 
    132 	/* check we can create directories */
    133 	sprintf(pb, "%s/dir", mountpath);
    134 	if (rump_sys_mkdir(pb, 0777) == -1)
    135 		atf_tc_fail_errno("mkdir");
    136 
    137 	sprintf(pb2, "%s/dir/file", mountpath);
    138 	fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
    139 	if (fd == -1)
    140 		atf_tc_fail_errno("create file");
    141 	rump_sys_close(fd);
    142 
    143 	rv = rump_sys_rmdir(pb);
    144 	if (rv != -1 || errno != ENOTEMPTY)
    145 		atf_tc_fail("non-empty directory removed succesfully");
    146 
    147 	if (rump_sys_unlink(pb2) == -1)
    148 		atf_tc_fail_errno("cannot remove dir/file");
    149 
    150 	if (rump_sys_rmdir(pb) == -1)
    151 		atf_tc_fail_errno("remove directory");
    152 }
    153 
    154 static void
    155 dir_rmdirdotdot(const atf_tc_t *tc, const char *mp)
    156 {
    157 	char pb[MAXPATHLEN];
    158 	int xerrno;
    159 
    160 	USES_DIRS;
    161 
    162 	FSTEST_ENTER();
    163 	RL(rump_sys_mkdir("test", 0777));
    164 	RL(rump_sys_chdir("test"));
    165 
    166 	RL(rump_sys_mkdir("subtest", 0777));
    167 	RL(rump_sys_chdir("subtest"));
    168 
    169 	md(pb, mp, "test/subtest");
    170 	RL(rump_sys_rmdir(pb));
    171 	md(pb, mp, "test");
    172 	RL(rump_sys_rmdir(pb));
    173 
    174 	if (FSTYPE_NFS(tc))
    175 		xerrno = ESTALE;
    176 	else
    177 		xerrno = ENOENT;
    178 	if (FSTYPE_TMPFS(tc))
    179 		atf_tc_expect_signal(-1, "PR kern/44657");
    180 	ATF_REQUIRE_ERRNO(xerrno, rump_sys_chdir("..") == -1);
    181 	FSTEST_EXIT();
    182 }
    183 
    184 static void
    185 checkfile(const char *path, struct stat *refp)
    186 {
    187 	char buf[MAXPATHLEN];
    188 	struct stat sb;
    189 	static int n = 1;
    190 
    191 	md(buf, path, "file");
    192 	if (rump_sys_stat(buf, &sb) == -1)
    193 		atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
    194 	if (memcmp(&sb, refp, sizeof(sb)) != 0)
    195 		atf_tc_fail("stat mismatch %d", n);
    196 	n++;
    197 }
    198 
    199 static void
    200 rename_dir(const atf_tc_t *tc, const char *mp)
    201 {
    202 	char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
    203 	struct stat ref, sb;
    204 
    205 	//if (FSTYPE_MSDOS(tc))
    206 		//atf_tc_skip("test fails in some setups, reason unknown");
    207 
    208 	if (FSTYPE_RUMPFS(tc))
    209 		atf_tc_skip("rename not supported by fs");
    210 
    211 	USES_DIRS;
    212 
    213 	md(pb1, mp, "dir1");
    214 	if (rump_sys_mkdir(pb1, 0777) == -1)
    215 		atf_tc_fail_errno("mkdir 1");
    216 
    217 	md(pb2, mp, "dir2");
    218 	if (rump_sys_mkdir(pb2, 0777) == -1)
    219 		atf_tc_fail_errno("mkdir 2");
    220 	md(pb2, mp, "dir2/subdir");
    221 	if (rump_sys_mkdir(pb2, 0777) == -1)
    222 		atf_tc_fail_errno("mkdir 3");
    223 
    224 	md(pb3, mp, "dir1/file");
    225 	if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
    226 		atf_tc_fail_errno("create file");
    227 	if (rump_sys_stat(pb3, &ref) == -1)
    228 		atf_tc_fail_errno("stat of file");
    229 
    230 	/*
    231 	 * First try ops which should succeed.
    232 	 */
    233 
    234 	/* rename within directory */
    235 	md(pb3, mp, "dir3");
    236 	if (rump_sys_rename(pb1, pb3) == -1)
    237 		atf_tc_fail_errno("rename 1");
    238 	checkfile(pb3, &ref);
    239 
    240 	/* rename directory onto itself (two ways, should fail) */
    241 	md(pb1, mp, "dir3/.");
    242 	if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
    243 		atf_tc_fail_errno("rename 2");
    244 	if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
    245 		atf_tc_fail_errno("rename 3");
    246 
    247 	checkfile(pb3, &ref);
    248 
    249 	/* rename father of directory into directory */
    250 	md(pb1, mp, "dir2/dir");
    251 	md(pb2, mp, "dir2");
    252 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    253 		atf_tc_fail_errno("rename 4");
    254 
    255 	/* same for grandfather */
    256 	md(pb1, mp, "dir2/subdir/dir2");
    257 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    258 		atf_tc_fail("rename 5");
    259 
    260 	checkfile(pb3, &ref);
    261 
    262 	/* rename directory over a non-empty directory */
    263 	if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
    264 		atf_tc_fail("rename 6");
    265 
    266 	/* cross-directory rename */
    267 	md(pb1, mp, "dir3");
    268 	md(pb2, mp, "dir2/somedir");
    269 	if (rump_sys_rename(pb1, pb2) == -1)
    270 		atf_tc_fail_errno("rename 7");
    271 	checkfile(pb2, &ref);
    272 
    273 	/* move to parent directory */
    274 	md(pb1, mp, "dir2/somedir/../../dir3");
    275 	if (rump_sys_rename(pb2, pb1) == -1)
    276 		atf_tc_fail_errno("rename 8");
    277 	md(pb1, mp, "dir2/../dir3");
    278 	checkfile(pb1, &ref);
    279 
    280 	/* atomic cross-directory rename */
    281 	md(pb3, mp, "dir2/subdir");
    282 	if (rump_sys_rename(pb1, pb3) == -1)
    283 		atf_tc_fail_errno("rename 9");
    284 	checkfile(pb3, &ref);
    285 
    286 	/* rename directory over an empty directory */
    287 	md(pb1, mp, "parent");
    288 	md(pb2, mp, "parent/dir1");
    289 	md(pb3, mp, "parent/dir2");
    290 	RL(rump_sys_mkdir(pb1, 0777));
    291 	RL(rump_sys_mkdir(pb2, 0777));
    292 	RL(rump_sys_mkdir(pb3, 0777));
    293 	RL(rump_sys_rename(pb2, pb3));
    294 
    295 	RL(rump_sys_stat(pb1, &sb));
    296 	ATF_CHECK_EQ(sb.st_nlink, 3);
    297 	RL(rump_sys_rmdir(pb3));
    298 	if (FSTYPE_TMPFS(tc))
    299 		atf_tc_expect_signal(-1, "PR kern/44288");
    300 	RL(rump_sys_rmdir(pb1));
    301 }
    302 
    303 static void
    304 rename_dotdot(const atf_tc_t *tc, const char *mp)
    305 {
    306 
    307 	if (FSTYPE_RUMPFS(tc))
    308 		atf_tc_skip("rename not supported by fs");
    309 
    310 	USES_DIRS;
    311 
    312 	if (rump_sys_chdir(mp) == -1)
    313 		atf_tc_fail_errno("chdir mountpoint");
    314 
    315 	if (rump_sys_mkdir("dir1", 0777) == -1)
    316 		atf_tc_fail_errno("mkdir 1");
    317 	if (rump_sys_mkdir("dir2", 0777) == -1)
    318 		atf_tc_fail_errno("mkdir 2");
    319 
    320 	if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
    321 		atf_tc_fail_errno("self-dotdot to");
    322 
    323 	if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
    324 		atf_tc_fail_errno("self-dotdot from");
    325 	atf_tc_expect_pass();
    326 
    327 	if (FSTYPE_TMPFS(tc)) {
    328 		atf_tc_expect_fail("PR kern/43617");
    329 	}
    330 	if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
    331 		atf_tc_fail("other-dotdot");
    332 
    333 	rump_sys_chdir("/");
    334 }
    335 
    336 static void
    337 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
    338 {
    339 	bool haslinks;
    340 	struct stat sb;
    341 	ino_t f1ino, f2ino;
    342 
    343 	if (FSTYPE_RUMPFS(tc))
    344 		atf_tc_skip("rename not supported by fs");
    345 
    346 	if (FSTYPE_MSDOS(tc))
    347 		atf_tc_skip("test fails in some setups, reason unknown");
    348 
    349 	if (rump_sys_chdir(mp) == -1)
    350 		atf_tc_fail_errno("chdir mountpoint");
    351 
    352 	if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
    353 		haslinks = false;
    354 	else
    355 		haslinks = true;
    356 
    357 	if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
    358 		atf_tc_fail_errno("create file");
    359 	if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
    360 		atf_tc_fail_errno("create file");
    361 
    362 	if (rump_sys_stat("file1", &sb) == -1)
    363 		atf_tc_fail_errno("stat");
    364 	f1ino = sb.st_ino;
    365 
    366 	if (haslinks) {
    367 		if (rump_sys_link("file1", "file_link") == -1)
    368 			atf_tc_fail_errno("link");
    369 		if (rump_sys_stat("file_link", &sb) == -1)
    370 			atf_tc_fail_errno("stat");
    371 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    372 		ATF_REQUIRE_EQ(sb.st_nlink, 2);
    373 	}
    374 
    375 	if (rump_sys_stat("file2", &sb) == -1)
    376 		atf_tc_fail_errno("stat");
    377 	f2ino = sb.st_ino;
    378 
    379 	if (rump_sys_rename("file1", "file3") == -1)
    380 		atf_tc_fail_errno("rename 1");
    381 	if (rump_sys_stat("file3", &sb) == -1)
    382 		atf_tc_fail_errno("stat 1");
    383 	if (haslinks) {
    384 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    385 	}
    386 	if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
    387 		atf_tc_fail_errno("source 1");
    388 
    389 	if (rump_sys_rename("file3", "file2") == -1)
    390 		atf_tc_fail_errno("rename 2");
    391 	if (rump_sys_stat("file2", &sb) == -1)
    392 		atf_tc_fail_errno("stat 2");
    393 	if (haslinks) {
    394 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    395 	}
    396 
    397 	if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
    398 		atf_tc_fail_errno("source 2");
    399 
    400 	if (haslinks) {
    401 		if (rump_sys_rename("file2", "file_link") == -1)
    402 			atf_tc_fail_errno("rename hardlink");
    403 		if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
    404 			atf_tc_fail_errno("source 3");
    405 		if (rump_sys_stat("file_link", &sb) == -1)
    406 			atf_tc_fail_errno("stat 2");
    407 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    408 		ATF_REQUIRE_EQ(sb.st_nlink, 1);
    409 	}
    410 
    411 	rump_sys_chdir("/");
    412 }
    413 
    414 static void
    415 create_nametoolong(const atf_tc_t *tc, const char *mp)
    416 {
    417 	char *name;
    418 	int fd;
    419 	long val;
    420 	size_t len;
    421 
    422 	if (rump_sys_chdir(mp) == -1)
    423 		atf_tc_fail_errno("chdir mountpoint");
    424 
    425 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
    426 	if (val == -1)
    427 		atf_tc_fail_errno("pathconf");
    428 
    429 	len = val + 1;
    430 	name = malloc(len+1);
    431 	if (name == NULL)
    432 		atf_tc_fail_errno("malloc");
    433 
    434 	memset(name, 'a', len);
    435 	*(name+len) = '\0';
    436 
    437 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
    438 	if (val == -1)
    439 		atf_tc_fail_errno("pathconf");
    440 
    441 	if (FSTYPE_MSDOS(tc))
    442 		atf_tc_expect_fail("PR kern/43670");
    443 	fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
    444 	if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
    445 		atf_tc_fail_errno("open");
    446 
    447 	if (val == 0 && rump_sys_close(fd) == -1)
    448 		atf_tc_fail_errno("close");
    449 	if (val == 0 && rump_sys_unlink(name) == -1)
    450 		atf_tc_fail_errno("unlink");
    451 
    452 	free(name);
    453 
    454 	rump_sys_chdir("/");
    455 }
    456 
    457 static void
    458 create_exist(const atf_tc_t *tc, const char *mp)
    459 {
    460 	const char *name = "hoge";
    461 	int fd;
    462 
    463 	RL(rump_sys_chdir(mp));
    464 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
    465 	RL(rump_sys_close(fd));
    466 	RL(rump_sys_unlink(name));
    467 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
    468 	RL(rump_sys_close(fd));
    469 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
    470 	RL(rump_sys_close(fd));
    471 	ATF_REQUIRE_ERRNO(EEXIST,
    472 	    (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
    473 	RL(rump_sys_unlink(name));
    474 	RL(rump_sys_chdir("/"));
    475 }
    476 
    477 static void
    478 rename_nametoolong(const atf_tc_t *tc, const char *mp)
    479 {
    480 	char *name;
    481 	int res, fd;
    482 	long val;
    483 	size_t len;
    484 
    485 	if (FSTYPE_RUMPFS(tc))
    486 		atf_tc_skip("rename not supported by fs");
    487 
    488 	if (rump_sys_chdir(mp) == -1)
    489 		atf_tc_fail_errno("chdir mountpoint");
    490 
    491 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
    492 	if (val == -1)
    493 		atf_tc_fail_errno("pathconf");
    494 
    495 	len = val + 1;
    496 	name = malloc(len+1);
    497 	if (name == NULL)
    498 		atf_tc_fail_errno("malloc");
    499 
    500 	memset(name, 'a', len);
    501 	*(name+len) = '\0';
    502 
    503 	fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
    504 	if (fd == -1)
    505 		atf_tc_fail_errno("open");
    506 	if (rump_sys_close(fd) == -1)
    507 		atf_tc_fail_errno("close");
    508 
    509 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
    510 	if (val == -1)
    511 		atf_tc_fail_errno("pathconf");
    512 
    513 	if (FSTYPE_MSDOS(tc))
    514 		atf_tc_expect_fail("PR kern/43670");
    515 	res = rump_sys_rename("dummy", name);
    516 	if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
    517 		atf_tc_fail_errno("rename");
    518 
    519 	if (val == 0 && rump_sys_unlink(name) == -1)
    520 		atf_tc_fail_errno("unlink");
    521 
    522 	free(name);
    523 
    524 	rump_sys_chdir("/");
    525 }
    526 
    527 static void
    528 symlink_zerolen(const atf_tc_t *tc, const char *mp)
    529 {
    530 
    531 	USES_SYMLINKS;
    532 
    533 	RL(rump_sys_chdir(mp));
    534 
    535 	if (FSTYPE_TMPFS(tc)) {
    536 		atf_tc_expect_signal(SIGABRT, "PR kern/43843");
    537 	}
    538 
    539 	RL(rump_sys_symlink("", "afile"));
    540 	RL(rump_sys_chdir("/"));
    541 }
    542 
    543 static void
    544 attrs(const atf_tc_t *tc, const char *mp)
    545 {
    546 	struct stat sb, sb2;
    547 	struct timeval tv[2];
    548 	int fd;
    549 
    550 	FSTEST_ENTER();
    551 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    552 	RL(rump_sys_close(fd));
    553 	RL(rump_sys_stat(TESTFILE, &sb));
    554 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    555 		RL(rump_sys_chown(TESTFILE, 1, 2));
    556 		sb.st_uid = 1;
    557 		sb.st_gid = 2;
    558 		RL(rump_sys_chmod(TESTFILE, 0123));
    559 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
    560 	}
    561 
    562 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
    563 	tv[0].tv_usec = 1;
    564 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
    565 	tv[1].tv_usec = 3;
    566 	RL(rump_sys_utimes(TESTFILE, tv));
    567 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
    568 	sb.st_atimespec.tv_sec = 1000000000;
    569 	sb.st_atimespec.tv_nsec = 1000;
    570 	sb.st_mtimespec.tv_sec = 1000000002;
    571 	sb.st_mtimespec.tv_nsec = 3000;
    572 
    573 	RL(rump_sys_stat(TESTFILE, &sb2));
    574 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
    575 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    576 		CHECK(st_uid);
    577 		CHECK(st_gid);
    578 		CHECK(st_mode);
    579 	}
    580 	if (!FSTYPE_MSDOS(tc)) {
    581 		/* msdosfs has only access date, not time */
    582 		CHECK(st_atimespec.tv_sec);
    583 	}
    584 	CHECK(st_mtimespec.tv_sec);
    585 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    586 		CHECK(st_atimespec.tv_nsec);
    587 		CHECK(st_mtimespec.tv_nsec);
    588 	}
    589 #undef  CHECK
    590 
    591 	FSTEST_EXIT();
    592 }
    593 
    594 static void
    595 fcntl_lock(const atf_tc_t *tc, const char *mp)
    596 {
    597 	int fd, fd2;
    598 	struct flock l;
    599 	struct lwp *lwp1, *lwp2;
    600 
    601 	FSTEST_ENTER();
    602 	l.l_pid = 0;
    603 	l.l_start = l.l_len = 1024;
    604 	l.l_type = F_RDLCK | F_WRLCK;
    605 	l.l_whence = SEEK_END;
    606 
    607 	lwp1 = rump_pub_lwproc_curlwp();
    608 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    609 	RL(rump_sys_ftruncate(fd, 8192));
    610 
    611 	/* PR kern/43321 */
    612 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
    613 
    614 	/* Next, we fork and try to lock the same area */
    615 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    616 	lwp2 = rump_pub_lwproc_curlwp();
    617 	RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
    618 	ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
    619 
    620 	/* Switch back and unlock... */
    621 	rump_pub_lwproc_switch(lwp1);
    622 	l.l_type = F_UNLCK;
    623 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
    624 
    625 	/* ... and try to lock again */
    626 	rump_pub_lwproc_switch(lwp2);
    627 	l.l_type = F_RDLCK | F_WRLCK;
    628 	RL(rump_sys_fcntl(fd2, F_SETLK, &l));
    629 
    630 	RL(rump_sys_close(fd2));
    631 	rump_pub_lwproc_releaselwp();
    632 
    633 	RL(rump_sys_close(fd));
    634 
    635 	FSTEST_EXIT();
    636 }
    637 
    638 static int
    639 flock_compare(const void *p, const void *q)
    640 {
    641 	int a = ((const struct flock *)p)->l_start;
    642 	int b = ((const struct flock *)q)->l_start;
    643 	return a < b ? -1 : (a > b ? 1 : 0);
    644 }
    645 
    646 static void
    647 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
    648 {
    649 	/* test non-overlaping ranges */
    650 	struct flock expect[4];
    651 	const struct flock lock[4] = {
    652 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
    653 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
    654 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
    655 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
    656 	};
    657 
    658 	int fd[4];
    659 	struct lwp *lwp[4];
    660 	pid_t prevpid = 0;
    661 
    662 	unsigned int i, j;
    663 	const off_t sz = 8192;
    664 	int omode  = 0755;
    665 	int oflags = O_RDWR | O_CREAT;
    666 
    667 	memcpy(expect, lock, sizeof(lock));
    668 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
    669 
    670 	FSTEST_ENTER();
    671 
    672 	/*
    673 	 * First, we create 4 processes and let each lock a range of the
    674 	 * file.  Note that the third and fourth processes lock in
    675 	 * "reverse" order, i.e. the greater pid locks a range before
    676 	 * the lesser pid.
    677 	 */
    678 	for(i = 0; i < __arraycount(lwp); i++) {
    679 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    680 
    681 		lwp[i] = rump_pub_lwproc_curlwp();
    682 		assert(rump_sys_getpid() > prevpid);
    683 		prevpid = rump_sys_getpid();
    684 
    685 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
    686 		oflags = O_RDWR;
    687 		omode  = 0;
    688 
    689 		RL(rump_sys_ftruncate(fd[i], sz));
    690 		RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
    691 	}
    692 
    693 	atf_tc_expect_fail("PR kern/44494");
    694 	/*
    695 	 * In the context of each pid , do GETLK for a readlock from
    696 	 * i = [0,__arraycount(locks)].  If we try to lock from the same
    697 	 * start offset as the lock our current process holds, check
    698 	 * that we fail on the offset of the next lock ("else if" branch).
    699 	 * Otherwise, expect to get a lock for the current offset
    700 	 * ("if" branch).  The "else" branch is purely for the last
    701 	 * process where we expect no blocking locks.
    702 	 */
    703 	for(i = 0; i < __arraycount(lwp); i++) {
    704 		rump_pub_lwproc_switch(lwp[i]);
    705 
    706 		for(j = 0; j < __arraycount(lwp); j++) {
    707 			struct flock l;
    708 			l = expect[j];
    709 			l.l_len = sz;
    710 			l.l_type = F_RDLCK;
    711 
    712 			RL(rump_sys_fcntl(fd[i], F_GETLK, &l));
    713 
    714 			if(expect[j].l_start != lock[i].l_start) {
    715 				/*
    716 				 * lock set by another process
    717 				 */
    718 				ATF_CHECK(l.l_type != F_UNLCK);
    719 				ATF_CHECK_EQ(l.l_start, expect[j].l_start);
    720 				ATF_CHECK_EQ(l.l_len,   expect[j].l_len);
    721 			} else if (j != __arraycount(lwp) - 1) {
    722 				/*
    723 				 * lock set by the current process
    724 				 */
    725 				ATF_CHECK(l.l_type != F_UNLCK);
    726 				ATF_CHECK_EQ(l.l_start, expect[j+1].l_start);
    727 				ATF_CHECK_EQ(l.l_len,   expect[j+1].l_len);
    728 			} else {
    729 				/*
    730 				 * there are no other locks after the
    731 				 * current process lock
    732 				 */
    733 				ATF_CHECK_EQ(l.l_type,   F_UNLCK);
    734 				ATF_CHECK_EQ(l.l_start,  expect[j].l_start);
    735 				ATF_CHECK_EQ(l.l_len,    sz);
    736 				ATF_CHECK_EQ(l.l_pid,    expect[j].l_pid);
    737 				ATF_CHECK_EQ(l.l_whence, expect[j].l_whence);
    738 			}
    739 		}
    740 	}
    741 
    742 	/*
    743 	 * Release processes.  This also releases the fds and locks
    744 	 * making fs unmount possible
    745 	 */
    746 	for(i = 0; i < __arraycount(lwp); i++) {
    747 		rump_pub_lwproc_switch(lwp[i]);
    748 		rump_pub_lwproc_releaselwp();
    749 	}
    750 
    751 	FSTEST_EXIT();
    752 }
    753 
    754 static void
    755 access_simple(const atf_tc_t *tc, const char *mp)
    756 {
    757 	int fd;
    758 	int tmode;
    759 
    760 	FSTEST_ENTER();
    761 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
    762 	RL(rump_sys_close(fd));
    763 
    764 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
    765 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
    766 		tmode = F_OK;
    767 	else
    768 		tmode = ALLACC;
    769 
    770 	RL(rump_sys_access("tfile", tmode));
    771 
    772 	/* PR kern/44648 */
    773 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
    774 #undef ALLACC
    775 	FSTEST_EXIT();
    776 }
    777 
    778 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
    779 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
    780 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
    781 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
    782 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out");
    783 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
    784 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
    785 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
    786 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
    787 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
    788 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
    789 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
    790 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
    791 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
    792 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
    793 ATF_TC_FSAPPLY(access_simple, "access(2)");
    794 
    795 ATF_TP_ADD_TCS(tp)
    796 {
    797 
    798 	ATF_TP_FSAPPLY(lookup_simple);
    799 	ATF_TP_FSAPPLY(lookup_complex);
    800 	ATF_TP_FSAPPLY(dir_simple);
    801 	ATF_TP_FSAPPLY(dir_notempty);
    802 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
    803 	ATF_TP_FSAPPLY(rename_dir);
    804 	ATF_TP_FSAPPLY(rename_dotdot);
    805 	ATF_TP_FSAPPLY(rename_reg_nodir);
    806 	ATF_TP_FSAPPLY(create_nametoolong);
    807 	ATF_TP_FSAPPLY(create_exist);
    808 	ATF_TP_FSAPPLY(rename_nametoolong);
    809 	ATF_TP_FSAPPLY(symlink_zerolen);
    810 	ATF_TP_FSAPPLY(attrs);
    811 	ATF_TP_FSAPPLY(fcntl_lock);
    812 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
    813 	ATF_TP_FSAPPLY(access_simple);
    814 
    815 	return atf_no_error();
    816 }
    817