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