Home | History | Annotate | Line # | Download | only in vfs
t_vnops.c revision 1.62
      1  1.62    martin /*	$NetBSD: t_vnops.c,v 1.62 2022/02/01 18:27:24 martin Exp $	*/
      2   1.1     pooka 
      3   1.1     pooka /*-
      4   1.1     pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5   1.1     pooka  * All rights reserved.
      6   1.1     pooka  *
      7   1.1     pooka  * Redistribution and use in source and binary forms, with or without
      8   1.1     pooka  * modification, are permitted provided that the following conditions
      9   1.1     pooka  * are met:
     10   1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     11   1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     12   1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     pooka  *    documentation and/or other materials provided with the distribution.
     15   1.1     pooka  *
     16   1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17   1.1     pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18   1.1     pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19   1.1     pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20   1.1     pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21   1.1     pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22   1.1     pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23   1.1     pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24   1.1     pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25   1.1     pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26   1.1     pooka  * POSSIBILITY OF SUCH DAMAGE.
     27   1.1     pooka  */
     28   1.1     pooka 
     29   1.1     pooka #include <sys/stat.h>
     30   1.1     pooka #include <sys/statvfs.h>
     31  1.58       kre #include <sys/time.h>
     32   1.1     pooka 
     33  1.13     pooka #include <assert.h>
     34   1.1     pooka #include <atf-c.h>
     35  1.54      gson #include <ctype.h>
     36   1.1     pooka #include <fcntl.h>
     37   1.1     pooka #include <libgen.h>
     38   1.5     njoly #include <stdlib.h>
     39  1.13     pooka #include <string.h>
     40   1.1     pooka #include <unistd.h>
     41   1.1     pooka 
     42   1.1     pooka #include <rump/rump_syscalls.h>
     43   1.1     pooka #include <rump/rump.h>
     44   1.1     pooka 
     45   1.1     pooka #include "../common/h_fsmacros.h"
     46  1.59  christos #include "h_macros.h"
     47   1.1     pooka 
     48  1.11     pooka #define TESTFILE "afile"
     49  1.11     pooka 
     50  1.29     njoly #define USES_DIRS					\
     51  1.29     njoly     if (FSTYPE_SYSVBFS(tc))				\
     52  1.29     njoly 	atf_tc_skip("directories not supported by file system")
     53   1.1     pooka 
     54   1.7     pooka #define USES_SYMLINKS					\
     55   1.7     pooka     if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))		\
     56   1.9     njoly 	atf_tc_skip("symlinks not supported by file system")
     57   1.7     pooka 
     58   1.2     pooka static char *
     59  1.56  christos md(char *buf, size_t buflen, const char *base, const char *tail)
     60   1.2     pooka {
     61   1.2     pooka 
     62  1.56  christos 	snprintf(buf, buflen, "%s/%s", base, tail);
     63   1.2     pooka 	return buf;
     64   1.2     pooka }
     65   1.2     pooka 
     66   1.1     pooka static void
     67   1.1     pooka lookup_simple(const atf_tc_t *tc, const char *mountpath)
     68   1.1     pooka {
     69   1.1     pooka 	char pb[MAXPATHLEN], final[MAXPATHLEN];
     70   1.1     pooka 	struct stat sb1, sb2;
     71   1.1     pooka 
     72   1.1     pooka 	strcpy(final, mountpath);
     73  1.56  christos 	snprintf(pb, sizeof(pb), "%s/../%s", mountpath, basename(final));
     74   1.1     pooka 	if (rump_sys_stat(pb, &sb1) == -1)
     75   1.1     pooka 		atf_tc_fail_errno("stat 1");
     76   1.1     pooka 
     77  1.56  christos 	snprintf(pb, sizeof(pb), "%s/./../%s", mountpath, basename(final));
     78   1.1     pooka 	if (rump_sys_stat(pb, &sb2) == -1)
     79   1.1     pooka 		atf_tc_fail_errno("stat 2");
     80   1.1     pooka 
     81   1.1     pooka 	ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
     82   1.1     pooka }
     83   1.1     pooka 
     84   1.1     pooka static void
     85   1.1     pooka lookup_complex(const atf_tc_t *tc, const char *mountpath)
     86   1.1     pooka {
     87   1.1     pooka 	char pb[MAXPATHLEN];
     88   1.1     pooka 	struct stat sb1, sb2;
     89  1.58       kre 	struct timespec atplus1, onesec;
     90   1.1     pooka 
     91   1.1     pooka 	USES_DIRS;
     92   1.1     pooka 
     93  1.56  christos 	snprintf(pb, sizeof(pb), "%s/dir", mountpath);
     94   1.1     pooka 	if (rump_sys_mkdir(pb, 0777) == -1)
     95   1.1     pooka 		atf_tc_fail_errno("mkdir");
     96   1.1     pooka 	if (rump_sys_stat(pb, &sb1) == -1)
     97   1.1     pooka 		atf_tc_fail_errno("stat 1");
     98   1.1     pooka 
     99  1.56  christos 	snprintf(pb, sizeof(pb), "%s/./dir/../././dir/.", mountpath);
    100   1.1     pooka 	if (rump_sys_stat(pb, &sb2) == -1)
    101   1.1     pooka 		atf_tc_fail_errno("stat 2");
    102   1.1     pooka 
    103  1.58       kre 	/*
    104  1.58       kre 	 * The lookup is permitted to modify the access time of
    105  1.58       kre 	 * any directories searched - such a directory is the
    106  1.58       kre 	 * subject of this test.   Any difference should cause
    107  1.58       kre 	 * the 2nd lookup atime tp be >= the first, if it is ==, all is
    108  1.58       kre 	 * OK (atime is not required to be modified by the search, or
    109  1.58       kre 	 * both references may happen within the came clock tick), if the
    110  1.58       kre 	 * 2nd lookup atime is > the first, but not "too much" greater,
    111  1.58       kre 	 * just set it back, so the memcmp just below succeeds
    112  1.58       kre 	 * (assuming all else is OK).
    113  1.58       kre 	 */
    114  1.58       kre 	onesec.tv_sec = 1;
    115  1.58       kre 	onesec.tv_nsec = 0;
    116  1.58       kre 	timespecadd(&sb1.st_atimespec, &onesec, &atplus1);
    117  1.58       kre 	if (timespeccmp(&sb2.st_atimespec, &sb1.st_atimespec, >) &&
    118  1.58       kre 	    timespeccmp(&sb2.st_atimespec, &atplus1, <))
    119  1.58       kre 		sb2.st_atimespec = sb1.st_atimespec;
    120  1.58       kre 
    121  1.40    martin 	if (memcmp(&sb1, &sb2, sizeof(sb1)) != 0) {
    122  1.40    martin 		printf("what\tsb1\t\tsb2\n");
    123  1.40    martin 
    124  1.40    martin #define FIELD(FN)	\
    125  1.40    martin 		printf(#FN "\t%lld\t%lld\n", \
    126  1.40    martin 		(long long)sb1.FN, (long long)sb2.FN)
    127  1.40    martin #define TIME(FN)	\
    128  1.40    martin 		printf(#FN "\t%lld.%ld\t%lld.%ld\n", \
    129  1.40    martin 		(long long)sb1.FN.tv_sec, sb1.FN.tv_nsec, \
    130  1.40    martin 		(long long)sb2.FN.tv_sec, sb2.FN.tv_nsec)
    131  1.40    martin 
    132  1.40    martin 		FIELD(st_dev);
    133  1.40    martin 		FIELD(st_mode);
    134  1.40    martin 		FIELD(st_ino);
    135  1.40    martin 		FIELD(st_nlink);
    136  1.40    martin 		FIELD(st_uid);
    137  1.40    martin 		FIELD(st_gid);
    138  1.40    martin 		FIELD(st_rdev);
    139  1.45  riastrad 		TIME(st_atimespec);
    140  1.45  riastrad 		TIME(st_mtimespec);
    141  1.45  riastrad 		TIME(st_ctimespec);
    142  1.45  riastrad 		TIME(st_birthtimespec);
    143  1.40    martin 		FIELD(st_size);
    144  1.40    martin 		FIELD(st_blocks);
    145  1.40    martin 		FIELD(st_flags);
    146  1.40    martin 		FIELD(st_gen);
    147  1.40    martin 
    148  1.40    martin #undef FIELD
    149  1.40    martin #undef TIME
    150  1.40    martin 
    151  1.60    andvar 		atf_tc_fail("stat results differ, see output for more details");
    152  1.40    martin 	}
    153   1.1     pooka }
    154   1.1     pooka 
    155   1.1     pooka static void
    156   1.1     pooka dir_simple(const atf_tc_t *tc, const char *mountpath)
    157   1.1     pooka {
    158   1.1     pooka 	char pb[MAXPATHLEN];
    159   1.1     pooka 	struct stat sb;
    160   1.1     pooka 
    161   1.1     pooka 	USES_DIRS;
    162   1.1     pooka 
    163   1.1     pooka 	/* check we can create directories */
    164  1.56  christos 	snprintf(pb, sizeof(pb), "%s/dir", mountpath);
    165   1.1     pooka 	if (rump_sys_mkdir(pb, 0777) == -1)
    166   1.1     pooka 		atf_tc_fail_errno("mkdir");
    167   1.1     pooka 	if (rump_sys_stat(pb, &sb) == -1)
    168   1.1     pooka 		atf_tc_fail_errno("stat new directory");
    169   1.1     pooka 
    170   1.1     pooka 	/* check we can remove then and that it makes them unreachable */
    171   1.1     pooka 	if (rump_sys_rmdir(pb) == -1)
    172   1.1     pooka 		atf_tc_fail_errno("rmdir");
    173   1.1     pooka 	if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
    174   1.1     pooka 		atf_tc_fail("ENOENT expected from stat");
    175   1.1     pooka }
    176   1.1     pooka 
    177   1.1     pooka static void
    178  1.62    martin do_dir_slash(const atf_tc_t *tc, const char *mountpath, const char *addend)
    179  1.62    martin {
    180  1.62    martin 	char plain[MAXPATHLEN], with_slash[MAXPATHLEN];
    181  1.62    martin 	struct stat sb;
    182  1.62    martin 
    183  1.62    martin 	USES_DIRS;
    184  1.62    martin 
    185  1.62    martin 	/* check we can create directories with one or more / appended */
    186  1.62    martin 	snprintf(plain, sizeof(plain), "%s/dir%s", mountpath, addend);
    187  1.62    martin 	snprintf(with_slash, sizeof(with_slash), "%s/dir/", mountpath);
    188  1.62    martin 	if (rump_sys_mkdir(with_slash, 0777) == -1)
    189  1.62    martin 		atf_tc_fail_errno("mkdir");
    190  1.62    martin 	if (rump_sys_stat(plain, &sb) == -1)
    191  1.62    martin 		atf_tc_fail_errno("stat new directory");
    192  1.62    martin 	if (rump_sys_rmdir(plain) == -1)
    193  1.62    martin 		atf_tc_fail_errno("rmdir");
    194  1.62    martin 	if (rump_sys_stat(with_slash, &sb) != -1 || errno != ENOENT)
    195  1.62    martin 		atf_tc_fail("ENOENT expected from stat");
    196  1.62    martin }
    197  1.62    martin 
    198  1.62    martin static void
    199  1.62    martin dir_slash(const atf_tc_t *tc, const char *mountpath)
    200  1.62    martin {
    201  1.62    martin 	do_dir_slash(tc, mountpath, "/");
    202  1.62    martin }
    203  1.62    martin 
    204  1.62    martin static void
    205  1.62    martin dir_2slash(const atf_tc_t *tc, const char *mountpath)
    206  1.62    martin {
    207  1.62    martin 	do_dir_slash(tc, mountpath, "//");
    208  1.62    martin }
    209  1.62    martin 
    210  1.62    martin static void
    211  1.62    martin dir_3slash(const atf_tc_t *tc, const char *mountpath)
    212  1.62    martin {
    213  1.62    martin 	do_dir_slash(tc, mountpath, "///");
    214  1.62    martin }
    215  1.62    martin 
    216  1.62    martin static void
    217   1.1     pooka dir_notempty(const atf_tc_t *tc, const char *mountpath)
    218   1.1     pooka {
    219   1.1     pooka 	char pb[MAXPATHLEN], pb2[MAXPATHLEN];
    220   1.1     pooka 	int fd, rv;
    221   1.1     pooka 
    222   1.1     pooka 	USES_DIRS;
    223   1.1     pooka 
    224   1.1     pooka 	/* check we can create directories */
    225  1.56  christos 	snprintf(pb, sizeof(pb), "%s/dir", mountpath);
    226   1.1     pooka 	if (rump_sys_mkdir(pb, 0777) == -1)
    227   1.1     pooka 		atf_tc_fail_errno("mkdir");
    228   1.1     pooka 
    229  1.56  christos 	snprintf(pb2, sizeof(pb2), "%s/dir/file", mountpath);
    230   1.1     pooka 	fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
    231   1.1     pooka 	if (fd == -1)
    232   1.1     pooka 		atf_tc_fail_errno("create file");
    233   1.1     pooka 	rump_sys_close(fd);
    234   1.1     pooka 
    235   1.1     pooka 	rv = rump_sys_rmdir(pb);
    236   1.1     pooka 	if (rv != -1 || errno != ENOTEMPTY)
    237  1.61    andvar 		atf_tc_fail("non-empty directory removed successfully");
    238   1.1     pooka 
    239   1.1     pooka 	if (rump_sys_unlink(pb2) == -1)
    240   1.1     pooka 		atf_tc_fail_errno("cannot remove dir/file");
    241   1.1     pooka 
    242   1.1     pooka 	if (rump_sys_rmdir(pb) == -1)
    243   1.1     pooka 		atf_tc_fail_errno("remove directory");
    244   1.1     pooka }
    245   1.1     pooka 
    246   1.2     pooka static void
    247  1.18     pooka dir_rmdirdotdot(const atf_tc_t *tc, const char *mp)
    248  1.18     pooka {
    249  1.18     pooka 	char pb[MAXPATHLEN];
    250  1.18     pooka 	int xerrno;
    251  1.18     pooka 
    252  1.18     pooka 	USES_DIRS;
    253  1.18     pooka 
    254  1.18     pooka 	FSTEST_ENTER();
    255  1.18     pooka 	RL(rump_sys_mkdir("test", 0777));
    256  1.18     pooka 	RL(rump_sys_chdir("test"));
    257  1.18     pooka 
    258  1.18     pooka 	RL(rump_sys_mkdir("subtest", 0777));
    259  1.18     pooka 	RL(rump_sys_chdir("subtest"));
    260  1.18     pooka 
    261  1.56  christos 	md(pb, sizeof(pb), mp, "test/subtest");
    262  1.18     pooka 	RL(rump_sys_rmdir(pb));
    263  1.56  christos 	md(pb, sizeof(pb), mp, "test");
    264  1.18     pooka 	RL(rump_sys_rmdir(pb));
    265  1.18     pooka 
    266  1.18     pooka 	if (FSTYPE_NFS(tc))
    267  1.18     pooka 		xerrno = ESTALE;
    268  1.18     pooka 	else
    269  1.18     pooka 		xerrno = ENOENT;
    270  1.18     pooka 	ATF_REQUIRE_ERRNO(xerrno, rump_sys_chdir("..") == -1);
    271  1.18     pooka 	FSTEST_EXIT();
    272  1.18     pooka }
    273  1.18     pooka 
    274  1.18     pooka static void
    275   1.2     pooka checkfile(const char *path, struct stat *refp)
    276   1.2     pooka {
    277   1.2     pooka 	char buf[MAXPATHLEN];
    278   1.2     pooka 	struct stat sb;
    279   1.2     pooka 	static int n = 1;
    280   1.2     pooka 
    281  1.56  christos 	md(buf, sizeof(buf), path, "file");
    282   1.2     pooka 	if (rump_sys_stat(buf, &sb) == -1)
    283   1.2     pooka 		atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
    284   1.2     pooka 	if (memcmp(&sb, refp, sizeof(sb)) != 0)
    285   1.2     pooka 		atf_tc_fail("stat mismatch %d", n);
    286   1.2     pooka 	n++;
    287   1.2     pooka }
    288   1.2     pooka 
    289   1.2     pooka static void
    290   1.2     pooka rename_dir(const atf_tc_t *tc, const char *mp)
    291   1.2     pooka {
    292   1.2     pooka 	char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
    293  1.16     pooka 	struct stat ref, sb;
    294   1.2     pooka 
    295  1.10     pooka 	if (FSTYPE_RUMPFS(tc))
    296  1.29     njoly 		atf_tc_skip("rename not supported by file system");
    297  1.10     pooka 
    298   1.2     pooka 	USES_DIRS;
    299   1.2     pooka 
    300  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir1");
    301   1.2     pooka 	if (rump_sys_mkdir(pb1, 0777) == -1)
    302   1.2     pooka 		atf_tc_fail_errno("mkdir 1");
    303   1.2     pooka 
    304  1.56  christos 	md(pb2, sizeof(pb2), mp, "dir2");
    305   1.2     pooka 	if (rump_sys_mkdir(pb2, 0777) == -1)
    306   1.2     pooka 		atf_tc_fail_errno("mkdir 2");
    307  1.56  christos 	md(pb2, sizeof(pb2), mp, "dir2/subdir");
    308   1.2     pooka 	if (rump_sys_mkdir(pb2, 0777) == -1)
    309   1.2     pooka 		atf_tc_fail_errno("mkdir 3");
    310   1.2     pooka 
    311  1.56  christos 	md(pb3, sizeof(pb3), mp, "dir1/file");
    312   1.2     pooka 	if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
    313   1.2     pooka 		atf_tc_fail_errno("create file");
    314   1.2     pooka 	if (rump_sys_stat(pb3, &ref) == -1)
    315   1.2     pooka 		atf_tc_fail_errno("stat of file");
    316   1.2     pooka 
    317   1.2     pooka 	/*
    318   1.2     pooka 	 * First try ops which should succeed.
    319   1.2     pooka 	 */
    320   1.2     pooka 
    321   1.2     pooka 	/* rename within directory */
    322  1.56  christos 	md(pb3, sizeof(pb3), mp, "dir3");
    323   1.2     pooka 	if (rump_sys_rename(pb1, pb3) == -1)
    324   1.2     pooka 		atf_tc_fail_errno("rename 1");
    325   1.2     pooka 	checkfile(pb3, &ref);
    326   1.2     pooka 
    327   1.2     pooka 	/* rename directory onto itself (two ways, should fail) */
    328  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir3/.");
    329   1.2     pooka 	if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
    330   1.2     pooka 		atf_tc_fail_errno("rename 2");
    331   1.2     pooka 	if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
    332   1.2     pooka 		atf_tc_fail_errno("rename 3");
    333   1.2     pooka 
    334   1.2     pooka 	checkfile(pb3, &ref);
    335   1.2     pooka 
    336   1.2     pooka 	/* rename father of directory into directory */
    337  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir2/dir");
    338  1.56  christos 	md(pb2, sizeof(pb2), mp, "dir2");
    339   1.2     pooka 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    340   1.2     pooka 		atf_tc_fail_errno("rename 4");
    341   1.2     pooka 
    342   1.2     pooka 	/* same for grandfather */
    343  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir2/subdir/dir2");
    344   1.2     pooka 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    345   1.2     pooka 		atf_tc_fail("rename 5");
    346   1.2     pooka 
    347   1.2     pooka 	checkfile(pb3, &ref);
    348   1.2     pooka 
    349   1.2     pooka 	/* rename directory over a non-empty directory */
    350   1.2     pooka 	if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
    351   1.2     pooka 		atf_tc_fail("rename 6");
    352   1.2     pooka 
    353   1.2     pooka 	/* cross-directory rename */
    354  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir3");
    355  1.56  christos 	md(pb2, sizeof(pb2), mp, "dir2/somedir");
    356   1.2     pooka 	if (rump_sys_rename(pb1, pb2) == -1)
    357   1.2     pooka 		atf_tc_fail_errno("rename 7");
    358   1.2     pooka 	checkfile(pb2, &ref);
    359   1.2     pooka 
    360   1.2     pooka 	/* move to parent directory */
    361  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir2/somedir/../../dir3");
    362   1.2     pooka 	if (rump_sys_rename(pb2, pb1) == -1)
    363   1.2     pooka 		atf_tc_fail_errno("rename 8");
    364  1.56  christos 	md(pb1, sizeof(pb1), mp, "dir2/../dir3");
    365   1.2     pooka 	checkfile(pb1, &ref);
    366   1.2     pooka 
    367  1.16     pooka 	/* atomic cross-directory rename */
    368  1.56  christos 	md(pb3, sizeof(pb3), mp, "dir2/subdir");
    369   1.2     pooka 	if (rump_sys_rename(pb1, pb3) == -1)
    370   1.2     pooka 		atf_tc_fail_errno("rename 9");
    371   1.2     pooka 	checkfile(pb3, &ref);
    372  1.16     pooka 
    373  1.16     pooka 	/* rename directory over an empty directory */
    374  1.56  christos 	md(pb1, sizeof(pb1), mp, "parent");
    375  1.56  christos 	md(pb2, sizeof(pb2), mp, "parent/dir1");
    376  1.56  christos 	md(pb3, sizeof(pb3), mp, "parent/dir2");
    377  1.16     pooka 	RL(rump_sys_mkdir(pb1, 0777));
    378  1.16     pooka 	RL(rump_sys_mkdir(pb2, 0777));
    379  1.16     pooka 	RL(rump_sys_mkdir(pb3, 0777));
    380  1.16     pooka 	RL(rump_sys_rename(pb2, pb3));
    381  1.16     pooka 
    382  1.16     pooka 	RL(rump_sys_stat(pb1, &sb));
    383  1.22   hannken 	if (! FSTYPE_MSDOS(tc))
    384  1.22   hannken 		ATF_CHECK_EQ(sb.st_nlink, 3);
    385  1.16     pooka 	RL(rump_sys_rmdir(pb3));
    386  1.16     pooka 	RL(rump_sys_rmdir(pb1));
    387   1.2     pooka }
    388   1.2     pooka 
    389   1.2     pooka static void
    390   1.2     pooka rename_dotdot(const atf_tc_t *tc, const char *mp)
    391   1.2     pooka {
    392   1.2     pooka 
    393  1.10     pooka 	if (FSTYPE_RUMPFS(tc))
    394  1.29     njoly 		atf_tc_skip("rename not supported by file system");
    395  1.10     pooka 
    396   1.2     pooka 	USES_DIRS;
    397   1.2     pooka 
    398   1.2     pooka 	if (rump_sys_chdir(mp) == -1)
    399   1.2     pooka 		atf_tc_fail_errno("chdir mountpoint");
    400   1.2     pooka 
    401   1.2     pooka 	if (rump_sys_mkdir("dir1", 0777) == -1)
    402   1.2     pooka 		atf_tc_fail_errno("mkdir 1");
    403   1.2     pooka 	if (rump_sys_mkdir("dir2", 0777) == -1)
    404   1.2     pooka 		atf_tc_fail_errno("mkdir 2");
    405   1.2     pooka 
    406   1.2     pooka 	if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
    407   1.2     pooka 		atf_tc_fail_errno("self-dotdot to");
    408   1.2     pooka 
    409   1.2     pooka 	if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
    410   1.2     pooka 		atf_tc_fail_errno("self-dotdot from");
    411   1.2     pooka 
    412   1.2     pooka 	if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
    413   1.2     pooka 		atf_tc_fail("other-dotdot");
    414   1.2     pooka 
    415   1.2     pooka 	rump_sys_chdir("/");
    416   1.2     pooka }
    417   1.2     pooka 
    418   1.2     pooka static void
    419   1.2     pooka rename_reg_nodir(const atf_tc_t *tc, const char *mp)
    420   1.2     pooka {
    421   1.2     pooka 	bool haslinks;
    422   1.2     pooka 	struct stat sb;
    423  1.38  christos 	ino_t f1ino;
    424   1.2     pooka 
    425  1.10     pooka 	if (FSTYPE_RUMPFS(tc))
    426  1.29     njoly 		atf_tc_skip("rename not supported by file system");
    427  1.10     pooka 
    428   1.2     pooka 	if (rump_sys_chdir(mp) == -1)
    429   1.2     pooka 		atf_tc_fail_errno("chdir mountpoint");
    430   1.2     pooka 
    431   1.2     pooka 	if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
    432   1.2     pooka 		haslinks = false;
    433   1.2     pooka 	else
    434   1.2     pooka 		haslinks = true;
    435   1.2     pooka 
    436   1.2     pooka 	if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
    437   1.2     pooka 		atf_tc_fail_errno("create file");
    438   1.2     pooka 	if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
    439   1.2     pooka 		atf_tc_fail_errno("create file");
    440   1.2     pooka 
    441   1.2     pooka 	if (rump_sys_stat("file1", &sb) == -1)
    442   1.2     pooka 		atf_tc_fail_errno("stat");
    443   1.2     pooka 	f1ino = sb.st_ino;
    444   1.2     pooka 
    445   1.2     pooka 	if (haslinks) {
    446   1.2     pooka 		if (rump_sys_link("file1", "file_link") == -1)
    447   1.2     pooka 			atf_tc_fail_errno("link");
    448   1.2     pooka 		if (rump_sys_stat("file_link", &sb) == -1)
    449   1.2     pooka 			atf_tc_fail_errno("stat");
    450   1.2     pooka 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    451   1.2     pooka 		ATF_REQUIRE_EQ(sb.st_nlink, 2);
    452   1.2     pooka 	}
    453   1.2     pooka 
    454   1.2     pooka 	if (rump_sys_stat("file2", &sb) == -1)
    455   1.2     pooka 		atf_tc_fail_errno("stat");
    456   1.2     pooka 
    457   1.2     pooka 	if (rump_sys_rename("file1", "file3") == -1)
    458   1.2     pooka 		atf_tc_fail_errno("rename 1");
    459   1.2     pooka 	if (rump_sys_stat("file3", &sb) == -1)
    460   1.2     pooka 		atf_tc_fail_errno("stat 1");
    461   1.2     pooka 	if (haslinks) {
    462   1.2     pooka 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    463   1.2     pooka 	}
    464   1.2     pooka 	if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
    465   1.2     pooka 		atf_tc_fail_errno("source 1");
    466   1.2     pooka 
    467   1.2     pooka 	if (rump_sys_rename("file3", "file2") == -1)
    468   1.2     pooka 		atf_tc_fail_errno("rename 2");
    469   1.2     pooka 	if (rump_sys_stat("file2", &sb) == -1)
    470   1.2     pooka 		atf_tc_fail_errno("stat 2");
    471   1.2     pooka 	if (haslinks) {
    472   1.2     pooka 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    473   1.2     pooka 	}
    474   1.2     pooka 
    475   1.2     pooka 	if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
    476   1.2     pooka 		atf_tc_fail_errno("source 2");
    477   1.2     pooka 
    478   1.2     pooka 	if (haslinks) {
    479   1.2     pooka 		if (rump_sys_rename("file2", "file_link") == -1)
    480   1.2     pooka 			atf_tc_fail_errno("rename hardlink");
    481   1.2     pooka 		if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
    482   1.2     pooka 			atf_tc_fail_errno("source 3");
    483   1.2     pooka 		if (rump_sys_stat("file_link", &sb) == -1)
    484   1.2     pooka 			atf_tc_fail_errno("stat 2");
    485   1.2     pooka 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    486   1.2     pooka 		ATF_REQUIRE_EQ(sb.st_nlink, 1);
    487   1.2     pooka 	}
    488   1.2     pooka 
    489  1.37     njoly 	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename("file2", NULL) == -1);
    490  1.37     njoly 	ATF_CHECK_ERRNO(EFAULT, rump_sys_rename(NULL, "file2") == -1);
    491  1.37     njoly 
    492   1.2     pooka 	rump_sys_chdir("/");
    493   1.2     pooka }
    494   1.2     pooka 
    495  1.53     pooka /* PR kern/50607 */
    496   1.5     njoly static void
    497  1.50     pooka create_many(const atf_tc_t *tc, const char *mp)
    498  1.50     pooka {
    499  1.50     pooka 	char buf[64];
    500  1.50     pooka 	int nfiles = 2324; /* #Nancy */
    501  1.50     pooka 	int i;
    502  1.50     pooka 
    503  1.50     pooka 	/* takes forever with many files */
    504  1.50     pooka 	if (FSTYPE_MSDOS(tc))
    505  1.50     pooka 		nfiles /= 4;
    506  1.50     pooka 
    507  1.50     pooka 	RL(rump_sys_chdir(mp));
    508  1.50     pooka 
    509  1.52     pooka 	if (FSTYPE_SYSVBFS(tc)) {
    510  1.52     pooka 		/* fs doesn't support many files or subdirectories */
    511  1.52     pooka 		nfiles = 5;
    512  1.52     pooka 	} else {
    513  1.52     pooka 		/* msdosfs doesn't like many entries in the root directory */
    514  1.52     pooka 		RL(rump_sys_mkdir("subdir", 0777));
    515  1.52     pooka 		RL(rump_sys_chdir("subdir"));
    516  1.52     pooka 	}
    517  1.50     pooka 
    518  1.50     pooka 	/* create them */
    519  1.51     pooka #define TESTFN "testfile"
    520  1.50     pooka 	for (i = 0; i < nfiles; i++) {
    521  1.50     pooka 		int fd;
    522  1.50     pooka 
    523  1.56  christos 		snprintf(buf, sizeof(buf), TESTFN "%d", i);
    524  1.50     pooka 		RL(fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666));
    525  1.50     pooka 		RL(rump_sys_close(fd));
    526  1.50     pooka 	}
    527  1.50     pooka 
    528  1.50     pooka 	/* wipe them out */
    529  1.50     pooka 	for (i = 0; i < nfiles; i++) {
    530  1.56  christos 		snprintf(buf, sizeof(buf), TESTFN "%d", i);
    531  1.56  christos 		RLF(rump_sys_unlink(buf), "%s", buf);
    532  1.50     pooka 	}
    533  1.50     pooka #undef TESTFN
    534  1.50     pooka 
    535  1.50     pooka 	rump_sys_chdir("/");
    536  1.50     pooka }
    537  1.50     pooka 
    538  1.54      gson /*
    539  1.54      gson  * Test creating files with one-character names using all possible
    540  1.54      gson  * character values.  Failures to create the file are ignored as the
    541  1.54      gson  * characters allowed in file names vary by file system, but at least
    542  1.54      gson  * we can check that the fs does not crash, and if the file is
    543  1.54      gson  * successfully created, unlinking it should also succeed.
    544  1.54      gson  */
    545  1.54      gson static void
    546  1.54      gson create_nonalphanum(const atf_tc_t *tc, const char *mp)
    547  1.54      gson {
    548  1.54      gson 	char buf[64];
    549  1.54      gson 	int i;
    550  1.54      gson 
    551  1.54      gson 	RL(rump_sys_chdir(mp));
    552  1.54      gson 
    553  1.54      gson 	for (i = 0; i < 256; i++) {
    554  1.54      gson 		int fd;
    555  1.56  christos 		snprintf(buf, sizeof(buf), "%c", i);
    556  1.54      gson 		fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666);
    557  1.54      gson 		if (fd == -1)
    558  1.54      gson 			continue;
    559  1.56  christos 		RLF(rump_sys_close(fd), "%d", fd);
    560  1.56  christos 		RLF(rump_sys_unlink(buf), "%s", buf);
    561  1.54      gson 	}
    562  1.54      gson 	printf("\n");
    563  1.54      gson 
    564  1.54      gson 	rump_sys_chdir("/");
    565  1.54      gson }
    566  1.54      gson 
    567  1.50     pooka static void
    568   1.5     njoly create_nametoolong(const atf_tc_t *tc, const char *mp)
    569   1.5     njoly {
    570   1.5     njoly 	char *name;
    571   1.5     njoly 	int fd;
    572   1.5     njoly 	long val;
    573   1.5     njoly 	size_t len;
    574   1.5     njoly 
    575   1.5     njoly 	if (rump_sys_chdir(mp) == -1)
    576   1.5     njoly 		atf_tc_fail_errno("chdir mountpoint");
    577   1.5     njoly 
    578   1.5     njoly 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
    579   1.5     njoly 	if (val == -1)
    580   1.5     njoly 		atf_tc_fail_errno("pathconf");
    581   1.5     njoly 
    582   1.5     njoly 	len = val + 1;
    583   1.5     njoly 	name = malloc(len+1);
    584   1.5     njoly 	if (name == NULL)
    585   1.5     njoly 		atf_tc_fail_errno("malloc");
    586   1.5     njoly 
    587   1.5     njoly 	memset(name, 'a', len);
    588   1.5     njoly 	*(name+len) = '\0';
    589   1.5     njoly 
    590   1.5     njoly 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
    591   1.5     njoly 	if (val == -1)
    592   1.5     njoly 		atf_tc_fail_errno("pathconf");
    593   1.5     njoly 
    594   1.5     njoly 	fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
    595   1.5     njoly 	if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
    596   1.5     njoly 		atf_tc_fail_errno("open");
    597   1.5     njoly 
    598   1.5     njoly 	if (val == 0 && rump_sys_close(fd) == -1)
    599   1.5     njoly 		atf_tc_fail_errno("close");
    600   1.5     njoly 	if (val == 0 && rump_sys_unlink(name) == -1)
    601   1.5     njoly 		atf_tc_fail_errno("unlink");
    602   1.5     njoly 
    603   1.5     njoly 	free(name);
    604   1.5     njoly 
    605   1.5     njoly 	rump_sys_chdir("/");
    606   1.5     njoly }
    607   1.5     njoly 
    608   1.5     njoly static void
    609  1.14      yamt create_exist(const atf_tc_t *tc, const char *mp)
    610  1.14      yamt {
    611  1.14      yamt 	const char *name = "hoge";
    612  1.14      yamt 	int fd;
    613  1.14      yamt 
    614  1.14      yamt 	RL(rump_sys_chdir(mp));
    615  1.14      yamt 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
    616  1.14      yamt 	RL(rump_sys_close(fd));
    617  1.14      yamt 	RL(rump_sys_unlink(name));
    618  1.14      yamt 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
    619  1.14      yamt 	RL(rump_sys_close(fd));
    620  1.14      yamt 	RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
    621  1.14      yamt 	RL(rump_sys_close(fd));
    622  1.14      yamt 	ATF_REQUIRE_ERRNO(EEXIST,
    623  1.14      yamt 	    (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
    624  1.14      yamt 	RL(rump_sys_unlink(name));
    625  1.14      yamt 	RL(rump_sys_chdir("/"));
    626  1.14      yamt }
    627  1.14      yamt 
    628  1.14      yamt static void
    629   1.5     njoly rename_nametoolong(const atf_tc_t *tc, const char *mp)
    630   1.5     njoly {
    631   1.5     njoly 	char *name;
    632   1.5     njoly 	int res, fd;
    633   1.5     njoly 	long val;
    634   1.5     njoly 	size_t len;
    635   1.5     njoly 
    636  1.10     pooka 	if (FSTYPE_RUMPFS(tc))
    637  1.29     njoly 		atf_tc_skip("rename not supported by file system");
    638  1.10     pooka 
    639   1.5     njoly 	if (rump_sys_chdir(mp) == -1)
    640   1.5     njoly 		atf_tc_fail_errno("chdir mountpoint");
    641   1.5     njoly 
    642   1.5     njoly 	val = rump_sys_pathconf(".", _PC_NAME_MAX);
    643   1.5     njoly 	if (val == -1)
    644   1.5     njoly 		atf_tc_fail_errno("pathconf");
    645   1.5     njoly 
    646   1.5     njoly 	len = val + 1;
    647   1.5     njoly 	name = malloc(len+1);
    648   1.5     njoly 	if (name == NULL)
    649   1.5     njoly 		atf_tc_fail_errno("malloc");
    650   1.5     njoly 
    651   1.5     njoly 	memset(name, 'a', len);
    652   1.5     njoly 	*(name+len) = '\0';
    653   1.5     njoly 
    654   1.5     njoly 	fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
    655   1.5     njoly 	if (fd == -1)
    656   1.5     njoly 		atf_tc_fail_errno("open");
    657   1.5     njoly 	if (rump_sys_close(fd) == -1)
    658   1.5     njoly 		atf_tc_fail_errno("close");
    659   1.5     njoly 
    660   1.5     njoly 	val = rump_sys_pathconf(".", _PC_NO_TRUNC);
    661   1.5     njoly 	if (val == -1)
    662   1.5     njoly 		atf_tc_fail_errno("pathconf");
    663   1.5     njoly 
    664   1.5     njoly 	res = rump_sys_rename("dummy", name);
    665   1.5     njoly 	if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
    666   1.5     njoly 		atf_tc_fail_errno("rename");
    667   1.5     njoly 
    668   1.5     njoly 	if (val == 0 && rump_sys_unlink(name) == -1)
    669   1.5     njoly 		atf_tc_fail_errno("unlink");
    670   1.5     njoly 
    671   1.5     njoly 	free(name);
    672   1.5     njoly 
    673   1.5     njoly 	rump_sys_chdir("/");
    674   1.5     njoly }
    675   1.5     njoly 
    676  1.42      gson /*
    677  1.42      gson  * Test creating a symlink whose length is "len" bytes, not including
    678  1.42      gson  * the terminating NUL.
    679  1.42      gson  */
    680   1.7     pooka static void
    681  1.42      gson symlink_len(const atf_tc_t *tc, const char *mp, size_t len)
    682   1.7     pooka {
    683  1.42      gson 	char *buf;
    684  1.42      gson 	int r;
    685   1.7     pooka 
    686   1.7     pooka 	USES_SYMLINKS;
    687   1.7     pooka 
    688  1.57  christos 	RLF(rump_sys_chdir(mp), "%s", mp);
    689   1.8     pooka 
    690  1.42      gson 	buf = malloc(len + 1);
    691  1.42      gson 	ATF_REQUIRE(buf);
    692  1.42      gson 	memset(buf, 'a', len);
    693  1.42      gson 	buf[len] = '\0';
    694  1.42      gson 	r = rump_sys_symlink(buf, "afile");
    695  1.42      gson 	if (r == -1) {
    696  1.42      gson 		ATF_REQUIRE_ERRNO(ENAMETOOLONG, r);
    697  1.42      gson 	} else {
    698  1.42      gson 		RL(rump_sys_unlink("afile"));
    699  1.42      gson 	}
    700  1.42      gson 	free(buf);
    701  1.42      gson 
    702   1.7     pooka 	RL(rump_sys_chdir("/"));
    703   1.7     pooka }
    704   1.7     pooka 
    705  1.11     pooka static void
    706  1.42      gson symlink_zerolen(const atf_tc_t *tc, const char *mp)
    707  1.42      gson {
    708  1.42      gson 	symlink_len(tc, mp, 0);
    709  1.42      gson }
    710  1.42      gson 
    711  1.42      gson static void
    712  1.42      gson symlink_long(const atf_tc_t *tc, const char *mp)
    713  1.42      gson {
    714  1.42      gson 	/*
    715  1.42      gson 	 * Test lengths close to powers of two, as those are likely
    716  1.42      gson 	 * to be edge cases.
    717  1.42      gson 	 */
    718  1.42      gson 	size_t len;
    719  1.42      gson 	int fuzz;
    720  1.42      gson 	for (len = 2; len <= 65536; len *= 2) {
    721  1.42      gson 		for (fuzz = -1; fuzz <= 1; fuzz++) {
    722  1.42      gson 			symlink_len(tc, mp, len + fuzz);
    723  1.42      gson 		}
    724  1.42      gson 	}
    725  1.42      gson }
    726  1.42      gson 
    727  1.42      gson static void
    728  1.28  riastrad symlink_root(const atf_tc_t *tc, const char *mp)
    729  1.28  riastrad {
    730  1.28  riastrad 
    731  1.28  riastrad 	USES_SYMLINKS;
    732  1.28  riastrad 
    733  1.28  riastrad 	RL(rump_sys_chdir(mp));
    734  1.28  riastrad 	RL(rump_sys_symlink("/", "foo"));
    735  1.28  riastrad 	RL(rump_sys_chdir("foo"));
    736  1.28  riastrad }
    737  1.28  riastrad 
    738  1.28  riastrad static void
    739  1.11     pooka attrs(const atf_tc_t *tc, const char *mp)
    740  1.11     pooka {
    741  1.11     pooka 	struct stat sb, sb2;
    742  1.11     pooka 	struct timeval tv[2];
    743  1.11     pooka 	int fd;
    744  1.11     pooka 
    745  1.11     pooka 	FSTEST_ENTER();
    746  1.11     pooka 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    747  1.11     pooka 	RL(rump_sys_close(fd));
    748  1.11     pooka 	RL(rump_sys_stat(TESTFILE, &sb));
    749  1.11     pooka 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    750  1.11     pooka 		RL(rump_sys_chown(TESTFILE, 1, 2));
    751  1.11     pooka 		sb.st_uid = 1;
    752  1.11     pooka 		sb.st_gid = 2;
    753  1.11     pooka 		RL(rump_sys_chmod(TESTFILE, 0123));
    754  1.11     pooka 		sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
    755  1.11     pooka 	}
    756  1.11     pooka 
    757  1.11     pooka 	tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
    758  1.11     pooka 	tv[0].tv_usec = 1;
    759  1.11     pooka 	tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
    760  1.11     pooka 	tv[1].tv_usec = 3;
    761  1.11     pooka 	RL(rump_sys_utimes(TESTFILE, tv));
    762  1.11     pooka 	RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
    763  1.11     pooka 	sb.st_atimespec.tv_sec = 1000000000;
    764  1.11     pooka 	sb.st_atimespec.tv_nsec = 1000;
    765  1.11     pooka 	sb.st_mtimespec.tv_sec = 1000000002;
    766  1.11     pooka 	sb.st_mtimespec.tv_nsec = 3000;
    767  1.11     pooka 
    768  1.11     pooka 	RL(rump_sys_stat(TESTFILE, &sb2));
    769  1.11     pooka #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
    770  1.11     pooka 	if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
    771  1.11     pooka 		CHECK(st_uid);
    772  1.11     pooka 		CHECK(st_gid);
    773  1.11     pooka 		CHECK(st_mode);
    774  1.11     pooka 	}
    775  1.11     pooka 	if (!FSTYPE_MSDOS(tc)) {
    776  1.11     pooka 		/* msdosfs has only access date, not time */
    777  1.11     pooka 		CHECK(st_atimespec.tv_sec);
    778  1.11     pooka 	}
    779  1.11     pooka 	CHECK(st_mtimespec.tv_sec);
    780  1.27       uch 	if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
    781  1.27       uch 	      FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
    782  1.11     pooka 		CHECK(st_atimespec.tv_nsec);
    783  1.11     pooka 		CHECK(st_mtimespec.tv_nsec);
    784  1.11     pooka 	}
    785  1.11     pooka #undef  CHECK
    786  1.11     pooka 
    787  1.11     pooka 	FSTEST_EXIT();
    788  1.11     pooka }
    789  1.11     pooka 
    790  1.12    kefren static void
    791  1.12    kefren fcntl_lock(const atf_tc_t *tc, const char *mp)
    792  1.12    kefren {
    793  1.12    kefren 	int fd, fd2;
    794  1.12    kefren 	struct flock l;
    795  1.12    kefren 	struct lwp *lwp1, *lwp2;
    796  1.12    kefren 
    797  1.12    kefren 	FSTEST_ENTER();
    798  1.12    kefren 	l.l_pid = 0;
    799  1.12    kefren 	l.l_start = l.l_len = 1024;
    800  1.12    kefren 	l.l_type = F_RDLCK | F_WRLCK;
    801  1.12    kefren 	l.l_whence = SEEK_END;
    802  1.12    kefren 
    803  1.12    kefren 	lwp1 = rump_pub_lwproc_curlwp();
    804  1.12    kefren 	RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
    805  1.12    kefren 	RL(rump_sys_ftruncate(fd, 8192));
    806  1.12    kefren 
    807  1.12    kefren 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
    808  1.12    kefren 
    809  1.12    kefren 	/* Next, we fork and try to lock the same area */
    810  1.12    kefren 	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    811  1.12    kefren 	lwp2 = rump_pub_lwproc_curlwp();
    812  1.12    kefren 	RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
    813  1.12    kefren 	ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
    814  1.12    kefren 
    815  1.12    kefren 	/* Switch back and unlock... */
    816  1.12    kefren 	rump_pub_lwproc_switch(lwp1);
    817  1.12    kefren 	l.l_type = F_UNLCK;
    818  1.12    kefren 	RL(rump_sys_fcntl(fd, F_SETLK, &l));
    819  1.12    kefren 
    820  1.12    kefren 	/* ... and try to lock again */
    821  1.12    kefren 	rump_pub_lwproc_switch(lwp2);
    822  1.12    kefren 	l.l_type = F_RDLCK | F_WRLCK;
    823  1.12    kefren 	RL(rump_sys_fcntl(fd2, F_SETLK, &l));
    824  1.12    kefren 
    825  1.12    kefren 	RL(rump_sys_close(fd2));
    826  1.12    kefren 	rump_pub_lwproc_releaselwp();
    827  1.12    kefren 
    828  1.12    kefren 	RL(rump_sys_close(fd));
    829  1.12    kefren 
    830  1.12    kefren 	FSTEST_EXIT();
    831  1.12    kefren }
    832  1.12    kefren 
    833  1.13     pooka static int
    834  1.13     pooka flock_compare(const void *p, const void *q)
    835  1.13     pooka {
    836  1.13     pooka 	int a = ((const struct flock *)p)->l_start;
    837  1.13     pooka 	int b = ((const struct flock *)q)->l_start;
    838  1.13     pooka 	return a < b ? -1 : (a > b ? 1 : 0);
    839  1.13     pooka }
    840  1.13     pooka 
    841  1.26     alnsn /*
    842  1.26     alnsn  * Find all locks set by fcntl_getlock_pids test
    843  1.26     alnsn  * using GETLK for a range [start, start+end], and,
    844  1.26     alnsn  * if there is a blocking lock, recursively find
    845  1.26     alnsn  * all locks to the left (toward the beginning of
    846  1.26     alnsn  * a file) and to the right of the lock.
    847  1.26     alnsn  * The function also understands "until end of file"
    848  1.26     alnsn  * convention when len==0.
    849  1.26     alnsn  */
    850  1.26     alnsn static unsigned int
    851  1.26     alnsn fcntl_getlocks(int fildes, off_t start, off_t len,
    852  1.26     alnsn     struct flock *lock, struct flock *end)
    853  1.26     alnsn {
    854  1.26     alnsn 	unsigned int rv = 0;
    855  1.26     alnsn 	const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
    856  1.26     alnsn 
    857  1.26     alnsn 	if (lock == end)
    858  1.26     alnsn 		return rv;
    859  1.26     alnsn 
    860  1.26     alnsn 	RL(rump_sys_fcntl(fildes, F_GETLK, &l));
    861  1.26     alnsn 
    862  1.26     alnsn 	if (l.l_type == F_UNLCK)
    863  1.26     alnsn 		return rv;
    864  1.26     alnsn 
    865  1.26     alnsn 	*lock++ = l;
    866  1.26     alnsn 	rv += 1;
    867  1.26     alnsn 
    868  1.26     alnsn 	ATF_REQUIRE(l.l_whence == SEEK_SET);
    869  1.26     alnsn 
    870  1.26     alnsn 	if (l.l_start > start) {
    871  1.26     alnsn 		unsigned int n =
    872  1.26     alnsn 		    fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
    873  1.26     alnsn 		rv += n;
    874  1.26     alnsn 		lock += n;
    875  1.26     alnsn 		if (lock == end)
    876  1.26     alnsn 			return rv;
    877  1.26     alnsn 	}
    878  1.26     alnsn 
    879  1.26     alnsn 	if (l.l_len == 0) /* does l spans until the end? */
    880  1.26     alnsn 		return rv;
    881  1.26     alnsn 
    882  1.26     alnsn 	if (len == 0) /* are we looking for locks until the end? */ {
    883  1.26     alnsn 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
    884  1.26     alnsn 	} else if (l.l_start + l.l_len < start + len) {
    885  1.26     alnsn 		len -= l.l_start + l.l_len - start;
    886  1.26     alnsn 		rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
    887  1.26     alnsn 	}
    888  1.26     alnsn 
    889  1.26     alnsn 	return rv;
    890  1.26     alnsn }
    891  1.26     alnsn 
    892  1.13     pooka static void
    893  1.13     pooka fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
    894  1.13     pooka {
    895  1.13     pooka 	/* test non-overlaping ranges */
    896  1.13     pooka 	struct flock expect[4];
    897  1.13     pooka 	const struct flock lock[4] = {
    898  1.13     pooka 		{ 0, 2, 0, F_WRLCK, SEEK_SET },
    899  1.13     pooka 		{ 2, 1, 0, F_WRLCK, SEEK_SET },
    900  1.13     pooka 		{ 7, 5, 0, F_WRLCK, SEEK_SET },
    901  1.13     pooka 		{ 4, 3, 0, F_WRLCK, SEEK_SET },
    902  1.13     pooka 	};
    903  1.13     pooka 
    904  1.26     alnsn     /* Add extra element to make sure recursion does't stop at array end */
    905  1.26     alnsn 	struct flock result[5];
    906  1.26     alnsn 
    907  1.26     alnsn 	/* Add 5th process */
    908  1.26     alnsn 	int fd[5];
    909  1.26     alnsn 	pid_t pid[5];
    910  1.26     alnsn 	struct lwp *lwp[5];
    911  1.13     pooka 
    912  1.13     pooka 	unsigned int i, j;
    913  1.13     pooka 	const off_t sz = 8192;
    914  1.13     pooka 	int omode  = 0755;
    915  1.13     pooka 	int oflags = O_RDWR | O_CREAT;
    916  1.13     pooka 
    917  1.13     pooka 	memcpy(expect, lock, sizeof(lock));
    918  1.13     pooka 
    919  1.13     pooka 	FSTEST_ENTER();
    920  1.13     pooka 
    921  1.13     pooka 	/*
    922  1.13     pooka 	 * First, we create 4 processes and let each lock a range of the
    923  1.13     pooka 	 * file.  Note that the third and fourth processes lock in
    924  1.13     pooka 	 * "reverse" order, i.e. the greater pid locks a range before
    925  1.13     pooka 	 * the lesser pid.
    926  1.26     alnsn 	 * Then, we create 5th process which doesn't lock anything.
    927  1.13     pooka 	 */
    928  1.26     alnsn 	for (i = 0; i < __arraycount(lwp); i++) {
    929  1.13     pooka 		RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
    930  1.13     pooka 
    931  1.13     pooka 		lwp[i] = rump_pub_lwproc_curlwp();
    932  1.26     alnsn 		pid[i] = rump_sys_getpid();
    933  1.13     pooka 
    934  1.13     pooka 		RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
    935  1.13     pooka 		oflags = O_RDWR;
    936  1.13     pooka 		omode  = 0;
    937  1.13     pooka 
    938  1.13     pooka 		RL(rump_sys_ftruncate(fd[i], sz));
    939  1.26     alnsn 
    940  1.26     alnsn 		if (i < __arraycount(lock)) {
    941  1.26     alnsn 			RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
    942  1.26     alnsn 			expect[i].l_pid = pid[i];
    943  1.26     alnsn 		}
    944  1.13     pooka 	}
    945  1.13     pooka 
    946  1.26     alnsn 	qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
    947  1.26     alnsn 
    948  1.13     pooka 	/*
    949  1.26     alnsn 	 * In the context of each process, recursively find all locks
    950  1.26     alnsn 	 * that would block the current process. Processes 1-4 don't
    951  1.26     alnsn 	 * see their own lock, we insert it to simplify checks.
    952  1.26     alnsn 	 * Process 5 sees all 4 locks.
    953  1.13     pooka 	 */
    954  1.26     alnsn 	for (i = 0; i < __arraycount(lwp); i++) {
    955  1.26     alnsn 		unsigned int nlocks;
    956  1.26     alnsn 
    957  1.13     pooka 		rump_pub_lwproc_switch(lwp[i]);
    958  1.13     pooka 
    959  1.26     alnsn 		memset(result, 0, sizeof(result));
    960  1.26     alnsn 		nlocks = fcntl_getlocks(fd[i], 0, sz,
    961  1.26     alnsn 		    result, result + __arraycount(result));
    962  1.26     alnsn 
    963  1.26     alnsn 		if (i < __arraycount(lock)) {
    964  1.26     alnsn 			ATF_REQUIRE(nlocks < __arraycount(result));
    965  1.26     alnsn 			result[nlocks] = lock[i];
    966  1.26     alnsn 			result[nlocks].l_pid = pid[i];
    967  1.26     alnsn 			nlocks++;
    968  1.26     alnsn 		}
    969  1.26     alnsn 
    970  1.26     alnsn 		ATF_CHECK_EQ(nlocks, __arraycount(expect));
    971  1.26     alnsn 
    972  1.26     alnsn 		qsort(result, nlocks, sizeof(result[0]), &flock_compare);
    973  1.26     alnsn 
    974  1.26     alnsn 		for (j = 0; j < nlocks; j++) {
    975  1.26     alnsn 			ATF_CHECK_EQ(result[j].l_start,  expect[j].l_start );
    976  1.26     alnsn 			ATF_CHECK_EQ(result[j].l_len,    expect[j].l_len   );
    977  1.26     alnsn 			ATF_CHECK_EQ(result[j].l_pid,    expect[j].l_pid   );
    978  1.26     alnsn 			ATF_CHECK_EQ(result[j].l_type,   expect[j].l_type  );
    979  1.26     alnsn 			ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
    980  1.13     pooka 		}
    981  1.13     pooka 	}
    982  1.13     pooka 
    983  1.13     pooka 	/*
    984  1.13     pooka 	 * Release processes.  This also releases the fds and locks
    985  1.13     pooka 	 * making fs unmount possible
    986  1.13     pooka 	 */
    987  1.26     alnsn 	for (i = 0; i < __arraycount(lwp); i++) {
    988  1.13     pooka 		rump_pub_lwproc_switch(lwp[i]);
    989  1.13     pooka 		rump_pub_lwproc_releaselwp();
    990  1.13     pooka 	}
    991  1.13     pooka 
    992  1.13     pooka 	FSTEST_EXIT();
    993  1.13     pooka }
    994  1.13     pooka 
    995  1.15     pooka static void
    996  1.15     pooka access_simple(const atf_tc_t *tc, const char *mp)
    997  1.15     pooka {
    998  1.15     pooka 	int fd;
    999  1.15     pooka 	int tmode;
   1000  1.15     pooka 
   1001  1.15     pooka 	FSTEST_ENTER();
   1002  1.15     pooka 	RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
   1003  1.15     pooka 	RL(rump_sys_close(fd));
   1004  1.15     pooka 
   1005  1.15     pooka #define ALLACC (F_OK | X_OK | W_OK | R_OK)
   1006  1.15     pooka 	if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
   1007  1.15     pooka 		tmode = F_OK;
   1008  1.15     pooka 	else
   1009  1.15     pooka 		tmode = ALLACC;
   1010  1.15     pooka 
   1011  1.15     pooka 	RL(rump_sys_access("tfile", tmode));
   1012  1.15     pooka 
   1013  1.15     pooka 	/* PR kern/44648 */
   1014  1.15     pooka 	ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
   1015  1.15     pooka #undef ALLACC
   1016  1.15     pooka 	FSTEST_EXIT();
   1017  1.15     pooka }
   1018  1.15     pooka 
   1019  1.30     njoly static void
   1020  1.30     njoly read_directory(const atf_tc_t *tc, const char *mp)
   1021  1.30     njoly {
   1022  1.30     njoly 	char buf[1024];
   1023  1.30     njoly 	int fd, res;
   1024  1.30     njoly 	ssize_t size;
   1025  1.30     njoly 
   1026  1.30     njoly 	FSTEST_ENTER();
   1027  1.30     njoly 	fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777);
   1028  1.30     njoly 	ATF_REQUIRE(fd != -1);
   1029  1.30     njoly 
   1030  1.30     njoly 	size = rump_sys_pread(fd, buf, sizeof(buf), 0);
   1031  1.30     njoly 	ATF_CHECK(size != -1 || errno == EISDIR);
   1032  1.30     njoly 	size = rump_sys_read(fd, buf, sizeof(buf));
   1033  1.30     njoly 	ATF_CHECK(size != -1 || errno == EISDIR);
   1034  1.30     njoly 
   1035  1.30     njoly 	res = rump_sys_close(fd);
   1036  1.30     njoly 	ATF_REQUIRE(res != -1);
   1037  1.30     njoly 	FSTEST_EXIT();
   1038  1.30     njoly }
   1039  1.30     njoly 
   1040  1.39     njoly static void
   1041  1.39     njoly lstat_symlink(const atf_tc_t *tc, const char *mp)
   1042  1.39     njoly {
   1043  1.39     njoly 	const char *src, *dst;
   1044  1.39     njoly 	int res;
   1045  1.39     njoly 	struct stat st;
   1046  1.39     njoly 
   1047  1.39     njoly 	USES_SYMLINKS;
   1048  1.43      gson 
   1049  1.39     njoly 	FSTEST_ENTER();
   1050  1.39     njoly 
   1051  1.39     njoly 	src = "source";
   1052  1.39     njoly 	dst = "destination";
   1053  1.39     njoly 
   1054  1.39     njoly 	res = rump_sys_symlink(src, dst);
   1055  1.39     njoly 	ATF_REQUIRE(res != -1);
   1056  1.39     njoly 	res = rump_sys_lstat(dst, &st);
   1057  1.39     njoly 	ATF_REQUIRE(res != -1);
   1058  1.39     njoly 
   1059  1.39     njoly 	ATF_CHECK(S_ISLNK(st.st_mode) != 0);
   1060  1.39     njoly 	ATF_CHECK(st.st_size == (off_t)strlen(src));
   1061  1.39     njoly 
   1062  1.39     njoly 	FSTEST_EXIT();
   1063  1.39     njoly }
   1064  1.39     njoly 
   1065   1.1     pooka ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
   1066   1.1     pooka ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
   1067   1.1     pooka ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
   1068  1.62    martin ATF_TC_FSAPPLY(dir_slash, "mkdir with appended slash");
   1069  1.62    martin ATF_TC_FSAPPLY(dir_2slash, "mkdir with two slashes appended");
   1070  1.62    martin ATF_TC_FSAPPLY(dir_3slash, "mkdir with three slashes appended");
   1071   1.1     pooka ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
   1072  1.31  christos ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
   1073  1.31  christos ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
   1074  1.31  christos "(PR kern/44288)");
   1075  1.31  christos ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
   1076   1.2     pooka ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
   1077   1.5     njoly ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
   1078  1.14      yamt ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
   1079   1.5     njoly ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
   1080  1.42      gson ATF_TC_FSAPPLY(symlink_zerolen, "symlink with target of length 0");
   1081  1.42      gson ATF_TC_FSAPPLY(symlink_long, "symlink with target of length > 0");
   1082  1.28  riastrad ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
   1083  1.11     pooka ATF_TC_FSAPPLY(attrs, "check setting attributes works");
   1084  1.12    kefren ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
   1085  1.13     pooka ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
   1086  1.15     pooka ATF_TC_FSAPPLY(access_simple, "access(2)");
   1087  1.30     njoly ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
   1088  1.39     njoly ATF_TC_FSAPPLY(lstat_symlink, "lstat(2) values for symbolic links");
   1089   1.1     pooka 
   1090  1.50     pooka #undef FSTEST_IMGSIZE
   1091  1.50     pooka #define FSTEST_IMGSIZE (1024*1024*64)
   1092  1.50     pooka ATF_TC_FSAPPLY(create_many, "create many directory entries");
   1093  1.54      gson ATF_TC_FSAPPLY(create_nonalphanum, "non-alphanumeric filenames");
   1094  1.50     pooka 
   1095   1.1     pooka ATF_TP_ADD_TCS(tp)
   1096   1.1     pooka {
   1097   1.1     pooka 
   1098   1.1     pooka 	ATF_TP_FSAPPLY(lookup_simple);
   1099   1.1     pooka 	ATF_TP_FSAPPLY(lookup_complex);
   1100   1.1     pooka 	ATF_TP_FSAPPLY(dir_simple);
   1101   1.1     pooka 	ATF_TP_FSAPPLY(dir_notempty);
   1102  1.18     pooka 	ATF_TP_FSAPPLY(dir_rmdirdotdot);
   1103  1.62    martin 	ATF_TP_FSAPPLY(dir_slash);
   1104  1.62    martin 	ATF_TP_FSAPPLY(dir_2slash);
   1105  1.62    martin 	ATF_TP_FSAPPLY(dir_3slash);
   1106   1.2     pooka 	ATF_TP_FSAPPLY(rename_dir);
   1107   1.2     pooka 	ATF_TP_FSAPPLY(rename_dotdot);
   1108   1.2     pooka 	ATF_TP_FSAPPLY(rename_reg_nodir);
   1109  1.50     pooka 	ATF_TP_FSAPPLY(create_many);
   1110  1.54      gson 	ATF_TP_FSAPPLY(create_nonalphanum);
   1111   1.5     njoly 	ATF_TP_FSAPPLY(create_nametoolong);
   1112  1.14      yamt 	ATF_TP_FSAPPLY(create_exist);
   1113   1.5     njoly 	ATF_TP_FSAPPLY(rename_nametoolong);
   1114   1.7     pooka 	ATF_TP_FSAPPLY(symlink_zerolen);
   1115  1.42      gson 	ATF_TP_FSAPPLY(symlink_long);
   1116  1.28  riastrad 	ATF_TP_FSAPPLY(symlink_root);
   1117  1.11     pooka 	ATF_TP_FSAPPLY(attrs);
   1118  1.12    kefren 	ATF_TP_FSAPPLY(fcntl_lock);
   1119  1.13     pooka 	ATF_TP_FSAPPLY(fcntl_getlock_pids);
   1120  1.15     pooka 	ATF_TP_FSAPPLY(access_simple);
   1121  1.30     njoly 	ATF_TP_FSAPPLY(read_directory);
   1122  1.39     njoly 	ATF_TP_FSAPPLY(lstat_symlink);
   1123   1.1     pooka 
   1124   1.1     pooka 	return atf_no_error();
   1125   1.1     pooka }
   1126