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