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