Home | History | Annotate | Line # | Download | only in vfs
t_vnops.c revision 1.2
      1 /*	$NetBSD: t_vnops.c,v 1.2 2010/07/14 20:45:48 pooka Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/stat.h>
     30 #include <sys/statvfs.h>
     31 
     32 #include <atf-c.h>
     33 #include <fcntl.h>
     34 #include <libgen.h>
     35 #include <unistd.h>
     36 
     37 #include <rump/rump_syscalls.h>
     38 #include <rump/rump.h>
     39 
     40 #include "../common/h_fsmacros.h"
     41 #include "../../h_macros.h"
     42 
     43 /* make work when run with 5.0 userland */
     44 #if defined(__NetBSD_Version__) && (__NetBSD_Version__ < 599000700)
     45 #define rump_sys_stat rump_pub_sys___stat30
     46 #endif
     47 
     48 #define USES_DIRS \
     49     if (FSTYPE_SYSVBFS(tc)) atf_tc_skip("dirs not supported by file system")
     50 
     51 static char *
     52 md(char *buf, const char *base, const char *tail)
     53 {
     54 
     55 	sprintf(buf, "%s/%s", base, tail);
     56 	return buf;
     57 }
     58 
     59 static void
     60 lookup_simple(const atf_tc_t *tc, const char *mountpath)
     61 {
     62 	char pb[MAXPATHLEN], final[MAXPATHLEN];
     63 	struct stat sb1, sb2;
     64 
     65 	strcpy(final, mountpath);
     66 	sprintf(pb, "%s/../%s", mountpath, basename(final));
     67 	if (rump_sys_stat(pb, &sb1) == -1)
     68 		atf_tc_fail_errno("stat 1");
     69 
     70 	sprintf(pb, "%s/./../%s", mountpath, basename(final));
     71 	if (rump_sys_stat(pb, &sb2) == -1)
     72 		atf_tc_fail_errno("stat 2");
     73 
     74 	ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
     75 }
     76 
     77 static void
     78 lookup_complex(const atf_tc_t *tc, const char *mountpath)
     79 {
     80 	char pb[MAXPATHLEN];
     81 	struct stat sb1, sb2;
     82 
     83 	USES_DIRS;
     84 
     85 	sprintf(pb, "%s/dir", mountpath);
     86 	if (rump_sys_mkdir(pb, 0777) == -1)
     87 		atf_tc_fail_errno("mkdir");
     88 	if (rump_sys_stat(pb, &sb1) == -1)
     89 		atf_tc_fail_errno("stat 1");
     90 
     91 	sprintf(pb, "%s/./dir/../././dir/.", mountpath);
     92 	if (rump_sys_stat(pb, &sb2) == -1)
     93 		atf_tc_fail_errno("stat 2");
     94 
     95 	ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
     96 }
     97 
     98 static void
     99 dir_simple(const atf_tc_t *tc, const char *mountpath)
    100 {
    101 	char pb[MAXPATHLEN];
    102 	struct stat sb;
    103 
    104 	USES_DIRS;
    105 
    106 	/* check we can create directories */
    107 	sprintf(pb, "%s/dir", mountpath);
    108 	if (rump_sys_mkdir(pb, 0777) == -1)
    109 		atf_tc_fail_errno("mkdir");
    110 	if (rump_sys_stat(pb, &sb) == -1)
    111 		atf_tc_fail_errno("stat new directory");
    112 
    113 	/* check we can remove then and that it makes them unreachable */
    114 	if (rump_sys_rmdir(pb) == -1)
    115 		atf_tc_fail_errno("rmdir");
    116 	if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
    117 		atf_tc_fail("ENOENT expected from stat");
    118 }
    119 
    120 static void
    121 dir_notempty(const atf_tc_t *tc, const char *mountpath)
    122 {
    123 	char pb[MAXPATHLEN], pb2[MAXPATHLEN];
    124 	int fd, rv;
    125 
    126 	USES_DIRS;
    127 
    128 	/* check we can create directories */
    129 	sprintf(pb, "%s/dir", mountpath);
    130 	if (rump_sys_mkdir(pb, 0777) == -1)
    131 		atf_tc_fail_errno("mkdir");
    132 
    133 	sprintf(pb2, "%s/dir/file", mountpath);
    134 	fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
    135 	if (fd == -1)
    136 		atf_tc_fail_errno("create file");
    137 	rump_sys_close(fd);
    138 
    139 	rv = rump_sys_rmdir(pb);
    140 	if (rv != -1 || errno != ENOTEMPTY)
    141 		atf_tc_fail("non-empty directory removed succesfully");
    142 
    143 	if (rump_sys_unlink(pb2) == -1)
    144 		atf_tc_fail_errno("cannot remove dir/file");
    145 
    146 	if (rump_sys_rmdir(pb) == -1)
    147 		atf_tc_fail_errno("remove directory");
    148 }
    149 
    150 static void
    151 checkfile(const char *path, struct stat *refp)
    152 {
    153 	char buf[MAXPATHLEN];
    154 	struct stat sb;
    155 	static int n = 1;
    156 
    157 	md(buf, path, "file");
    158 	if (rump_sys_stat(buf, &sb) == -1)
    159 		atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
    160 	if (memcmp(&sb, refp, sizeof(sb)) != 0)
    161 		atf_tc_fail("stat mismatch %d", n);
    162 	n++;
    163 }
    164 
    165 static void
    166 rename_dir(const atf_tc_t *tc, const char *mp)
    167 {
    168 	char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
    169 	struct stat ref;
    170 
    171 	USES_DIRS;
    172 
    173 	md(pb1, mp, "dir1");
    174 	if (rump_sys_mkdir(pb1, 0777) == -1)
    175 		atf_tc_fail_errno("mkdir 1");
    176 
    177 	md(pb2, mp, "dir2");
    178 	if (rump_sys_mkdir(pb2, 0777) == -1)
    179 		atf_tc_fail_errno("mkdir 2");
    180 	md(pb2, mp, "dir2/subdir");
    181 	if (rump_sys_mkdir(pb2, 0777) == -1)
    182 		atf_tc_fail_errno("mkdir 3");
    183 
    184 	md(pb3, mp, "dir1/file");
    185 	if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
    186 		atf_tc_fail_errno("create file");
    187 	if (rump_sys_stat(pb3, &ref) == -1)
    188 		atf_tc_fail_errno("stat of file");
    189 
    190 	/*
    191 	 * First try ops which should succeed.
    192 	 */
    193 
    194 	/* rename within directory */
    195 	md(pb3, mp, "dir3");
    196 	if (rump_sys_rename(pb1, pb3) == -1)
    197 		atf_tc_fail_errno("rename 1");
    198 	checkfile(pb3, &ref);
    199 
    200 	/* rename directory onto itself (two ways, should fail) */
    201 	md(pb1, mp, "dir3/.");
    202 	if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
    203 		atf_tc_fail_errno("rename 2");
    204 	if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
    205 		atf_tc_fail_errno("rename 3");
    206 
    207 	checkfile(pb3, &ref);
    208 
    209 	/* rename father of directory into directory */
    210 	md(pb1, mp, "dir2/dir");
    211 	md(pb2, mp, "dir2");
    212 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    213 		atf_tc_fail_errno("rename 4");
    214 
    215 	/* same for grandfather */
    216 	md(pb1, mp, "dir2/subdir/dir2");
    217 	if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
    218 		atf_tc_fail("rename 5");
    219 
    220 	checkfile(pb3, &ref);
    221 
    222 	/* rename directory over a non-empty directory */
    223 	if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
    224 		atf_tc_fail("rename 6");
    225 
    226 	/* cross-directory rename */
    227 	md(pb1, mp, "dir3");
    228 	md(pb2, mp, "dir2/somedir");
    229 	if (rump_sys_rename(pb1, pb2) == -1)
    230 		atf_tc_fail_errno("rename 7");
    231 	checkfile(pb2, &ref);
    232 
    233 	/* move to parent directory */
    234 	md(pb1, mp, "dir2/somedir/../../dir3");
    235 	if (rump_sys_rename(pb2, pb1) == -1)
    236 		atf_tc_fail_errno("rename 8");
    237 	md(pb1, mp, "dir2/../dir3");
    238 	checkfile(pb1, &ref);
    239 
    240 	/* finally, atomic cross-directory rename */
    241 	md(pb3, mp, "dir2/subdir");
    242 	if (rump_sys_rename(pb1, pb3) == -1)
    243 		atf_tc_fail_errno("rename 9");
    244 	checkfile(pb3, &ref);
    245 }
    246 
    247 static void
    248 rename_dotdot(const atf_tc_t *tc, const char *mp)
    249 {
    250 
    251 	USES_DIRS;
    252 
    253 	if (rump_sys_chdir(mp) == -1)
    254 		atf_tc_fail_errno("chdir mountpoint");
    255 
    256 	if (rump_sys_mkdir("dir1", 0777) == -1)
    257 		atf_tc_fail_errno("mkdir 1");
    258 	if (rump_sys_mkdir("dir2", 0777) == -1)
    259 		atf_tc_fail_errno("mkdir 2");
    260 
    261 	/* msdosfs fails both at least currently */
    262 	if (FSTYPE_MSDOS(tc)) {
    263 		atf_tc_expect_fail("PR kern/43616");
    264 	}
    265 	if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
    266 		atf_tc_fail_errno("self-dotdot to");
    267 
    268 	if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
    269 		atf_tc_fail_errno("self-dotdot from");
    270 	atf_tc_expect_pass();
    271 
    272 	if (FSTYPE_TMPFS(tc)) {
    273 		atf_tc_expect_fail("PR kern/43617");
    274 	}
    275 	if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
    276 		atf_tc_fail("other-dotdot");
    277 
    278 	rump_sys_chdir("/");
    279 }
    280 
    281 static void
    282 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
    283 {
    284 	bool haslinks;
    285 	struct stat sb;
    286 	ino_t f1ino, f2ino;
    287 
    288 	if (rump_sys_chdir(mp) == -1)
    289 		atf_tc_fail_errno("chdir mountpoint");
    290 
    291 	if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
    292 		haslinks = false;
    293 	else
    294 		haslinks = true;
    295 
    296 	if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
    297 		atf_tc_fail_errno("create file");
    298 	if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
    299 		atf_tc_fail_errno("create file");
    300 
    301 	if (rump_sys_stat("file1", &sb) == -1)
    302 		atf_tc_fail_errno("stat");
    303 	f1ino = sb.st_ino;
    304 
    305 	if (haslinks) {
    306 		if (rump_sys_link("file1", "file_link") == -1)
    307 			atf_tc_fail_errno("link");
    308 		if (rump_sys_stat("file_link", &sb) == -1)
    309 			atf_tc_fail_errno("stat");
    310 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    311 		ATF_REQUIRE_EQ(sb.st_nlink, 2);
    312 	}
    313 
    314 	if (rump_sys_stat("file2", &sb) == -1)
    315 		atf_tc_fail_errno("stat");
    316 	f2ino = sb.st_ino;
    317 
    318 	if (rump_sys_rename("file1", "file3") == -1)
    319 		atf_tc_fail_errno("rename 1");
    320 	if (rump_sys_stat("file3", &sb) == -1)
    321 		atf_tc_fail_errno("stat 1");
    322 	if (haslinks) {
    323 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    324 	}
    325 	if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
    326 		atf_tc_fail_errno("source 1");
    327 
    328 	if (rump_sys_rename("file3", "file2") == -1)
    329 		atf_tc_fail_errno("rename 2");
    330 	if (rump_sys_stat("file2", &sb) == -1)
    331 		atf_tc_fail_errno("stat 2");
    332 	if (haslinks) {
    333 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    334 	}
    335 
    336 	if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
    337 		atf_tc_fail_errno("source 2");
    338 
    339 	if (haslinks) {
    340 		if (rump_sys_rename("file2", "file_link") == -1)
    341 			atf_tc_fail_errno("rename hardlink");
    342 		if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
    343 			atf_tc_fail_errno("source 3");
    344 		if (rump_sys_stat("file_link", &sb) == -1)
    345 			atf_tc_fail_errno("stat 2");
    346 		ATF_REQUIRE_EQ(sb.st_ino, f1ino);
    347 		ATF_REQUIRE_EQ(sb.st_nlink, 1);
    348 	}
    349 
    350 	rump_sys_chdir("/");
    351 }
    352 
    353 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
    354 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
    355 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
    356 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
    357 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops");
    358 ATF_TC_FSAPPLY(rename_dotdot, "rename dir ..");
    359 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
    360 
    361 ATF_TP_ADD_TCS(tp)
    362 {
    363 
    364 	ATF_TP_FSAPPLY(lookup_simple);
    365 	ATF_TP_FSAPPLY(lookup_complex);
    366 	ATF_TP_FSAPPLY(dir_simple);
    367 	ATF_TP_FSAPPLY(dir_notempty);
    368 	ATF_TP_FSAPPLY(rename_dir);
    369 	ATF_TP_FSAPPLY(rename_dotdot);
    370 	ATF_TP_FSAPPLY(rename_reg_nodir);
    371 
    372 	return atf_no_error();
    373 }
    374