Home | History | Annotate | Line # | Download | only in vfs
t_vnops.c revision 1.22
      1 /*	$NetBSD: t_vnops.c,v 1.22 2011/03/19 20:05:21 hannken 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_RUMPFS(tc))
    206 		atf_tc_skip("rename not supported by fs");
    207 
    208 	USES_DIRS;
    209 
    210 	md(pb1, mp, "dir1");
    211 	if (rump_sys_mkdir(pb1, 0777) == -1)
    212 		atf_tc_fail_errno("mkdir 1");
    213 
    214 	md(pb2, mp, "dir2");
    215 	if (rump_sys_mkdir(pb2, 0777) == -1)
    216 		atf_tc_fail_errno("mkdir 2");
    217 	md(pb2, mp, "dir2/subdir");
    218 	if (rump_sys_mkdir(pb2, 0777) == -1)
    219 		atf_tc_fail_errno("mkdir 3");
    220 
    221 	md(pb3, mp, "dir1/file");
    222 	if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
    223 		atf_tc_fail_errno("create file");
    224 	if (rump_sys_stat(pb3, &ref) == -1)
    225 		atf_tc_fail_errno("stat of file");
    226 
    227 	/*
    228 	 * First try ops which should succeed.
    229 	 */
    230 
    231 	/* rename within directory */
    232 	md(pb3, mp, "dir3");
    233 	if (rump_sys_rename(pb1, pb3) == -1)
    234 		atf_tc_fail_errno("rename 1");
    235 	checkfile(pb3, &ref);
    236 
    237 	/* rename directory onto itself (two ways, should fail) */
    238 	md(pb1, mp, "dir3/.");
    239 	if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
    240 		atf_tc_fail_errno("rename 2");
    241 	if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
    242 		atf_tc_fail_errno("rename 3");
    243 
    244 	checkfile(pb3, &ref);
    245 
    246 	/* rename father of directory into directory */
    247 	md(pb1, mp, "dir2/dir");
    248 	md(pb2, mp, "dir2");
    249 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    250 		atf_tc_fail_errno("rename 4");
    251 
    252 	/* same for grandfather */
    253 	md(pb1, mp, "dir2/subdir/dir2");
    254 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    255 		atf_tc_fail("rename 5");
    256 
    257 	checkfile(pb3, &ref);
    258 
    259 	/* rename directory over a non-empty directory */
    260 	if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
    261 		atf_tc_fail("rename 6");
    262 
    263 	/* cross-directory rename */
    264 	md(pb1, mp, "dir3");
    265 	md(pb2, mp, "dir2/somedir");
    266 	if (rump_sys_rename(pb1, pb2) == -1)
    267 		atf_tc_fail_errno("rename 7");
    268 	checkfile(pb2, &ref);
    269 
    270 	/* move to parent directory */
    271 	md(pb1, mp, "dir2/somedir/../../dir3");
    272 	if (rump_sys_rename(pb2, pb1) == -1)
    273 		atf_tc_fail_errno("rename 8");
    274 	md(pb1, mp, "dir2/../dir3");
    275 	checkfile(pb1, &ref);
    276 
    277 	/* atomic cross-directory rename */
    278 	md(pb3, mp, "dir2/subdir");
    279 	if (rump_sys_rename(pb1, pb3) == -1)
    280 		atf_tc_fail_errno("rename 9");
    281 	checkfile(pb3, &ref);
    282 
    283 	/* rename directory over an empty directory */
    284 	md(pb1, mp, "parent");
    285 	md(pb2, mp, "parent/dir1");
    286 	md(pb3, mp, "parent/dir2");
    287 	RL(rump_sys_mkdir(pb1, 0777));
    288 	RL(rump_sys_mkdir(pb2, 0777));
    289 	RL(rump_sys_mkdir(pb3, 0777));
    290 	RL(rump_sys_rename(pb2, pb3));
    291 
    292 	RL(rump_sys_stat(pb1, &sb));
    293 	if (! FSTYPE_MSDOS(tc))
    294 		ATF_CHECK_EQ(sb.st_nlink, 3);
    295 	RL(rump_sys_rmdir(pb3));
    296 	if (FSTYPE_TMPFS(tc))
    297 		atf_tc_expect_signal(-1, "PR kern/44288");
    298 	RL(rump_sys_rmdir(pb1));
    299 }
    300 
    301 static void
    302 rename_dotdot(const atf_tc_t *tc, const char *mp)
    303 {
    304 
    305 	if (FSTYPE_RUMPFS(tc))
    306 		atf_tc_skip("rename not supported by fs");
    307 
    308 	USES_DIRS;
    309 
    310 	if (rump_sys_chdir(mp) == -1)
    311 		atf_tc_fail_errno("chdir mountpoint");
    312 
    313 	if (rump_sys_mkdir("dir1", 0777) == -1)
    314 		atf_tc_fail_errno("mkdir 1");
    315 	if (rump_sys_mkdir("dir2", 0777) == -1)
    316 		atf_tc_fail_errno("mkdir 2");
    317 
    318 	if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
    319 		atf_tc_fail_errno("self-dotdot to");
    320 
    321 	if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
    322 		atf_tc_fail_errno("self-dotdot from");
    323 	atf_tc_expect_pass();
    324 
    325 	if (FSTYPE_TMPFS(tc)) {
    326 		atf_tc_expect_fail("PR kern/43617");
    327 	}
    328 	if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
    329 		atf_tc_fail("other-dotdot");
    330 
    331 	rump_sys_chdir("/");
    332 }
    333 
    334 static void
    335 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
    336 {
    337 	bool haslinks;
    338 	struct stat sb;
    339 	ino_t f1ino, f2ino;
    340 
    341 	if (FSTYPE_RUMPFS(tc))
    342 		atf_tc_skip("rename not supported by fs");
    343 
    344 	if (rump_sys_chdir(mp) == -1)
    345 		atf_tc_fail_errno("chdir mountpoint");
    346 
    347 	if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
    348 		haslinks = false;
    349 	else
    350 		haslinks = true;
    351 
    352 	if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
    353 		atf_tc_fail_errno("create file");
    354 	if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
    355 		atf_tc_fail_errno("create file");
    356 
    357 	if (rump_sys_stat("file1", &sb) == -1)
    358 		atf_tc_fail_errno("stat");
    359 	f1ino = sb.st_ino;
    360 
    361 	if (haslinks) {
    362 		if (rump_sys_link("file1", "file_link") == -1)
    363 			atf_tc_fail_errno("link");
    364 		if (rump_sys_stat("file_link", &sb) == -1)
    365 			atf_tc_fail_errno("stat");
    366 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    367 		ATF_REQUIRE_EQ(sb.st_nlink, 2);
    368 	}
    369 
    370 	if (rump_sys_stat("file2", &sb) == -1)
    371 		atf_tc_fail_errno("stat");
    372 	f2ino = sb.st_ino;
    373 
    374 	if (rump_sys_rename("file1", "file3") == -1)
    375 		atf_tc_fail_errno("rename 1");
    376 	if (rump_sys_stat("file3", &sb) == -1)
    377 		atf_tc_fail_errno("stat 1");
    378 	if (haslinks) {
    379 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    380 	}
    381 	if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
    382 		atf_tc_fail_errno("source 1");
    383 
    384 	if (rump_sys_rename("file3", "file2") == -1)
    385 		atf_tc_fail_errno("rename 2");
    386 	if (rump_sys_stat("file2", &sb) == -1)
    387 		atf_tc_fail_errno("stat 2");
    388 	if (haslinks) {
    389 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    390 	}
    391 
    392 	if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
    393 		atf_tc_fail_errno("source 2");
    394 
    395 	if (haslinks) {
    396 		if (rump_sys_rename("file2", "file_link") == -1)
    397 			atf_tc_fail_errno("rename hardlink");
    398 		if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
    399 			atf_tc_fail_errno("source 3");
    400 		if (rump_sys_stat("file_link", &sb) == -1)
    401 			atf_tc_fail_errno("stat 2");
    402 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    403 		ATF_REQUIRE_EQ(sb.st_nlink, 1);
    404 	}
    405 
    406 	rump_sys_chdir("/");
    407 }
    408 
    409 static void
    410 create_nametoolong(const atf_tc_t *tc, const char *mp)
    411 {
    412 	char *name;
    413 	int fd;
    414 	long val;
    415 	size_t len;
    416 
    417 	if (rump_sys_chdir(mp) == -1)
    418 		atf_tc_fail_errno("chdir mountpoint");
    419 
    420 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
    421 	if (val == -1)
    422 		atf_tc_fail_errno("pathconf");
    423 
    424 	len = val + 1;
    425 	name = malloc(len+1);
    426 	if (name == NULL)
    427 		atf_tc_fail_errno("malloc");
    428 
    429 	memset(name, 'a', len);
    430 	*(name+len) = '\0';
    431 
    432 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
    433 	if (val == -1)
    434 		atf_tc_fail_errno("pathconf");
    435 
    436 	if (FSTYPE_MSDOS(tc))
    437 		atf_tc_expect_fail("PR kern/43670");
    438 	fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
    439 	if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
    440 		atf_tc_fail_errno("open");
    441 
    442 	if (val == 0 && rump_sys_close(fd) == -1)
    443 		atf_tc_fail_errno("close");
    444 	if (val == 0 && rump_sys_unlink(name) == -1)
    445 		atf_tc_fail_errno("unlink");
    446 
    447 	free(name);
    448 
    449 	rump_sys_chdir("/");
    450 }
    451 
    452 static void
    453 create_exist(const atf_tc_t *tc, const char *mp)
    454 {
    455 	const char *name = "hoge";
    456 	int fd;
    457 
    458 	RL(rump_sys_chdir(mp));
    459 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
    460 	RL(rump_sys_close(fd));
    461 	RL(rump_sys_unlink(name));
    462 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
    463 	RL(rump_sys_close(fd));
    464 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
    465 	RL(rump_sys_close(fd));
    466 	ATF_REQUIRE_ERRNO(EEXIST,
    467 	    (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
    468 	RL(rump_sys_unlink(name));
    469 	RL(rump_sys_chdir("/"));
    470 }
    471 
    472 static void
    473 rename_nametoolong(const atf_tc_t *tc, const char *mp)
    474 {
    475 	char *name;
    476 	int res, fd;
    477 	long val;
    478 	size_t len;
    479 
    480 	if (FSTYPE_RUMPFS(tc))
    481 		atf_tc_skip("rename not supported by fs");
    482 
    483 	if (rump_sys_chdir(mp) == -1)
    484 		atf_tc_fail_errno("chdir mountpoint");
    485 
    486 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
    487 	if (val == -1)
    488 		atf_tc_fail_errno("pathconf");
    489 
    490 	len = val + 1;
    491 	name = malloc(len+1);
    492 	if (name == NULL)
    493 		atf_tc_fail_errno("malloc");
    494 
    495 	memset(name, 'a', len);
    496 	*(name+len) = '\0';
    497 
    498 	fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
    499 	if (fd == -1)
    500 		atf_tc_fail_errno("open");
    501 	if (rump_sys_close(fd) == -1)
    502 		atf_tc_fail_errno("close");
    503 
    504 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
    505 	if (val == -1)
    506 		atf_tc_fail_errno("pathconf");
    507 
    508 	if (FSTYPE_MSDOS(tc))
    509 		atf_tc_expect_fail("PR kern/43670");
    510 	res = rump_sys_rename("dummy", name);
    511 	if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
    512 		atf_tc_fail_errno("rename");
    513 
    514 	if (val == 0 && rump_sys_unlink(name) == -1)
    515 		atf_tc_fail_errno("unlink");
    516 
    517 	free(name);
    518 
    519 	rump_sys_chdir("/");
    520 }
    521 
    522 static void
    523 symlink_zerolen(const atf_tc_t *tc, const char *mp)
    524 {
    525 
    526 	USES_SYMLINKS;
    527 
    528 	RL(rump_sys_chdir(mp));
    529 
    530 	if (FSTYPE_TMPFS(tc)) {
    531 		atf_tc_expect_signal(SIGABRT, "PR kern/43843");
    532 	}
    533 
    534 	RL(rump_sys_symlink("", "afile"));
    535 	RL(rump_sys_chdir("/"));
    536 }
    537 
    538 static void
    539 attrs(const atf_tc_t *tc, const char *mp)
    540 {
    541 	struct stat sb, sb2;
    542 	struct timeval tv[2];
    543 	int fd;
    544 
    545 	FSTEST_ENTER();
    546 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    547 	RL(rump_sys_close(fd));
    548 	RL(rump_sys_stat(TESTFILE, &sb));
    549 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    550 		RL(rump_sys_chown(TESTFILE, 1, 2));
    551 		sb.st_uid = 1;
    552 		sb.st_gid = 2;
    553 		RL(rump_sys_chmod(TESTFILE, 0123));
    554 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
    555 	}
    556 
    557 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
    558 	tv[0].tv_usec = 1;
    559 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
    560 	tv[1].tv_usec = 3;
    561 	RL(rump_sys_utimes(TESTFILE, tv));
    562 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
    563 	sb.st_atimespec.tv_sec = 1000000000;
    564 	sb.st_atimespec.tv_nsec = 1000;
    565 	sb.st_mtimespec.tv_sec = 1000000002;
    566 	sb.st_mtimespec.tv_nsec = 3000;
    567 
    568 	RL(rump_sys_stat(TESTFILE, &sb2));
    569 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
    570 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    571 		CHECK(st_uid);
    572 		CHECK(st_gid);
    573 		CHECK(st_mode);
    574 	}
    575 	if (!FSTYPE_MSDOS(tc)) {
    576 		/* msdosfs has only access date, not time */
    577 		CHECK(st_atimespec.tv_sec);
    578 	}
    579 	CHECK(st_mtimespec.tv_sec);
    580 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(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 static void
    642 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
    643 {
    644 	/* test non-overlaping ranges */
    645 	struct flock expect[4];
    646 	const struct flock lock[4] = {
    647 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
    648 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
    649 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
    650 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
    651 	};
    652 
    653 	int fd[4];
    654 	struct lwp *lwp[4];
    655 	pid_t prevpid = 0;
    656 
    657 	unsigned int i, j;
    658 	const off_t sz = 8192;
    659 	int omode  = 0755;
    660 	int oflags = O_RDWR | O_CREAT;
    661 
    662 	memcpy(expect, lock, sizeof(lock));
    663 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
    664 
    665 	FSTEST_ENTER();
    666 
    667 	/*
    668 	 * First, we create 4 processes and let each lock a range of the
    669 	 * file.  Note that the third and fourth processes lock in
    670 	 * "reverse" order, i.e. the greater pid locks a range before
    671 	 * the lesser pid.
    672 	 */
    673 	for(i = 0; i < __arraycount(lwp); i++) {
    674 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    675 
    676 		lwp[i] = rump_pub_lwproc_curlwp();
    677 		assert(rump_sys_getpid() > prevpid);
    678 		prevpid = rump_sys_getpid();
    679 
    680 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
    681 		oflags = O_RDWR;
    682 		omode  = 0;
    683 
    684 		RL(rump_sys_ftruncate(fd[i], sz));
    685 		RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
    686 	}
    687 
    688 	atf_tc_expect_fail("PR kern/44494");
    689 	/*
    690 	 * In the context of each pid , do GETLK for a readlock from
    691 	 * i = [0,__arraycount(locks)].  If we try to lock from the same
    692 	 * start offset as the lock our current process holds, check
    693 	 * that we fail on the offset of the next lock ("else if" branch).
    694 	 * Otherwise, expect to get a lock for the current offset
    695 	 * ("if" branch).  The "else" branch is purely for the last
    696 	 * process where we expect no blocking locks.
    697 	 */
    698 	for(i = 0; i < __arraycount(lwp); i++) {
    699 		rump_pub_lwproc_switch(lwp[i]);
    700 
    701 		for(j = 0; j < __arraycount(lwp); j++) {
    702 			struct flock l;
    703 			l = expect[j];
    704 			l.l_len = sz;
    705 			l.l_type = F_RDLCK;
    706 
    707 			RL(rump_sys_fcntl(fd[i], F_GETLK, &l));
    708 
    709 			if(expect[j].l_start != lock[i].l_start) {
    710 				/*
    711 				 * lock set by another process
    712 				 */
    713 				ATF_CHECK(l.l_type != F_UNLCK);
    714 				ATF_CHECK_EQ(l.l_start, expect[j].l_start);
    715 				ATF_CHECK_EQ(l.l_len,   expect[j].l_len);
    716 			} else if (j != __arraycount(lwp) - 1) {
    717 				/*
    718 				 * lock set by the current process
    719 				 */
    720 				ATF_CHECK(l.l_type != F_UNLCK);
    721 				ATF_CHECK_EQ(l.l_start, expect[j+1].l_start);
    722 				ATF_CHECK_EQ(l.l_len,   expect[j+1].l_len);
    723 			} else {
    724 				/*
    725 				 * there are no other locks after the
    726 				 * current process lock
    727 				 */
    728 				ATF_CHECK_EQ(l.l_type,   F_UNLCK);
    729 				ATF_CHECK_EQ(l.l_start,  expect[j].l_start);
    730 				ATF_CHECK_EQ(l.l_len,    sz);
    731 				ATF_CHECK_EQ(l.l_pid,    expect[j].l_pid);
    732 				ATF_CHECK_EQ(l.l_whence, expect[j].l_whence);
    733 			}
    734 		}
    735 	}
    736 
    737 	/*
    738 	 * Release processes.  This also releases the fds and locks
    739 	 * making fs unmount possible
    740 	 */
    741 	for(i = 0; i < __arraycount(lwp); i++) {
    742 		rump_pub_lwproc_switch(lwp[i]);
    743 		rump_pub_lwproc_releaselwp();
    744 	}
    745 
    746 	FSTEST_EXIT();
    747 }
    748 
    749 static void
    750 access_simple(const atf_tc_t *tc, const char *mp)
    751 {
    752 	int fd;
    753 	int tmode;
    754 
    755 	FSTEST_ENTER();
    756 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
    757 	RL(rump_sys_close(fd));
    758 
    759 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
    760 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
    761 		tmode = F_OK;
    762 	else
    763 		tmode = ALLACC;
    764 
    765 	RL(rump_sys_access("tfile", tmode));
    766 
    767 	/* PR kern/44648 */
    768 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
    769 #undef ALLACC
    770 	FSTEST_EXIT();
    771 }
    772 
    773 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
    774 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
    775 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
    776 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
    777 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out");
    778 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
    779 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
    780 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
    781 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
    782 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
    783 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
    784 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with 0-len target");
    785 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
    786 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
    787 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
    788 ATF_TC_FSAPPLY(access_simple, "access(2)");
    789 
    790 ATF_TP_ADD_TCS(tp)
    791 {
    792 
    793 	ATF_TP_FSAPPLY(lookup_simple);
    794 	ATF_TP_FSAPPLY(lookup_complex);
    795 	ATF_TP_FSAPPLY(dir_simple);
    796 	ATF_TP_FSAPPLY(dir_notempty);
    797 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
    798 	ATF_TP_FSAPPLY(rename_dir);
    799 	ATF_TP_FSAPPLY(rename_dotdot);
    800 	ATF_TP_FSAPPLY(rename_reg_nodir);
    801 	ATF_TP_FSAPPLY(create_nametoolong);
    802 	ATF_TP_FSAPPLY(create_exist);
    803 	ATF_TP_FSAPPLY(rename_nametoolong);
    804 	ATF_TP_FSAPPLY(symlink_zerolen);
    805 	ATF_TP_FSAPPLY(attrs);
    806 	ATF_TP_FSAPPLY(fcntl_lock);
    807 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
    808 	ATF_TP_FSAPPLY(access_simple);
    809 
    810 	return atf_no_error();
    811 }
    812