1 /* $NetBSD: t_vnops.c,v 1.55 2016/01/28 10:10:09 martin Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/statvfs.h>
41 #include <rump/rump_syscalls.h>
42 #include <rump/rump.h>
44 #include "../common/h_fsmacros.h"
45 #include "../../h_macros.h"
47 #define TESTFILE "afile"
50 if (FSTYPE_SYSVBFS(tc)) \
51 atf_tc_skip("directories not supported by file system")
53 #define USES_SYMLINKS \
54 if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc)) \
55 atf_tc_skip("symlinks not supported by file system")
58 md(char *buf, const char *base, const char *tail)
61 sprintf(buf, "%s/%s", base, tail);
66 lookup_simple(const atf_tc_t *tc, const char *mountpath)
68 char pb[MAXPATHLEN], final[MAXPATHLEN];
71 strcpy(final, mountpath);
72 sprintf(pb, "%s/../%s", mountpath, basename(final));
73 if (rump_sys_stat(pb, &sb1) == -1)
74 atf_tc_fail_errno("stat 1");
76 sprintf(pb, "%s/./../%s", mountpath, basename(final));
77 if (rump_sys_stat(pb, &sb2) == -1)
78 atf_tc_fail_errno("stat 2");
80 ATF_REQUIRE(memcmp(&sb1, &sb2, sizeof(sb1)) == 0);
84 lookup_complex(const atf_tc_t *tc, const char *mountpath)
92 atf_tc_expect_fail("PR kern/49033");
94 sprintf(pb, "%s/dir", mountpath);
95 if (rump_sys_mkdir(pb, 0777) == -1)
96 atf_tc_fail_errno("mkdir");
97 if (rump_sys_stat(pb, &sb1) == -1)
98 atf_tc_fail_errno("stat 1");
100 sprintf(pb, "%s/./dir/../././dir/.", mountpath);
101 if (rump_sys_stat(pb, &sb2) == -1)
102 atf_tc_fail_errno("stat 2");
104 if (memcmp(&sb1, &sb2, sizeof(sb1)) != 0) {
105 printf("what\tsb1\t\tsb2\n");
108 printf(#FN "\t%lld\t%lld\n", \
109 (long long)sb1.FN, (long long)sb2.FN)
111 printf(#FN "\t%lld.%ld\t%lld.%ld\n", \
112 (long long)sb1.FN.tv_sec, sb1.FN.tv_nsec, \
113 (long long)sb2.FN.tv_sec, sb2.FN.tv_nsec)
125 TIME(st_birthtimespec);
134 atf_tc_fail("stat results differ, see ouput for more details");
137 atf_tc_fail("random failure of PR kern/49033 "
138 "did not happen this time");
142 dir_simple(const atf_tc_t *tc, const char *mountpath)
149 /* check we can create directories */
150 sprintf(pb, "%s/dir", mountpath);
151 if (rump_sys_mkdir(pb, 0777) == -1)
152 atf_tc_fail_errno("mkdir");
153 if (rump_sys_stat(pb, &sb) == -1)
154 atf_tc_fail_errno("stat new directory");
156 /* check we can remove then and that it makes them unreachable */
157 if (rump_sys_rmdir(pb) == -1)
158 atf_tc_fail_errno("rmdir");
159 if (rump_sys_stat(pb, &sb) != -1 || errno != ENOENT)
160 atf_tc_fail("ENOENT expected from stat");
164 dir_notempty(const atf_tc_t *tc, const char *mountpath)
166 char pb[MAXPATHLEN], pb2[MAXPATHLEN];
171 /* check we can create directories */
172 sprintf(pb, "%s/dir", mountpath);
173 if (rump_sys_mkdir(pb, 0777) == -1)
174 atf_tc_fail_errno("mkdir");
176 sprintf(pb2, "%s/dir/file", mountpath);
177 fd = rump_sys_open(pb2, O_RDWR | O_CREAT, 0777);
179 atf_tc_fail_errno("create file");
182 rv = rump_sys_rmdir(pb);
183 if (rv != -1 || errno != ENOTEMPTY)
184 atf_tc_fail("non-empty directory removed succesfully");
186 if (rump_sys_unlink(pb2) == -1)
187 atf_tc_fail_errno("cannot remove dir/file");
189 if (rump_sys_rmdir(pb) == -1)
190 atf_tc_fail_errno("remove directory");
194 dir_rmdirdotdot(const atf_tc_t *tc, const char *mp)
202 RL(rump_sys_mkdir("test", 0777));
203 RL(rump_sys_chdir("test"));
205 RL(rump_sys_mkdir("subtest", 0777));
206 RL(rump_sys_chdir("subtest"));
208 md(pb, mp, "test/subtest");
209 RL(rump_sys_rmdir(pb));
211 RL(rump_sys_rmdir(pb));
217 ATF_REQUIRE_ERRNO(xerrno, rump_sys_chdir("..") == -1);
222 checkfile(const char *path, struct stat *refp)
224 char buf[MAXPATHLEN];
228 md(buf, path, "file");
229 if (rump_sys_stat(buf, &sb) == -1)
230 atf_tc_fail_errno("cannot stat file %d (%s)", n, buf);
231 if (memcmp(&sb, refp, sizeof(sb)) != 0)
232 atf_tc_fail("stat mismatch %d", n);
237 rename_dir(const atf_tc_t *tc, const char *mp)
239 char pb1[MAXPATHLEN], pb2[MAXPATHLEN], pb3[MAXPATHLEN];
242 if (FSTYPE_RUMPFS(tc))
243 atf_tc_skip("rename not supported by file system");
248 if (rump_sys_mkdir(pb1, 0777) == -1)
249 atf_tc_fail_errno("mkdir 1");
252 if (rump_sys_mkdir(pb2, 0777) == -1)
253 atf_tc_fail_errno("mkdir 2");
254 md(pb2, mp, "dir2/subdir");
255 if (rump_sys_mkdir(pb2, 0777) == -1)
256 atf_tc_fail_errno("mkdir 3");
258 md(pb3, mp, "dir1/file");
259 if (rump_sys_mknod(pb3, S_IFREG | 0777, -1) == -1)
260 atf_tc_fail_errno("create file");
261 if (rump_sys_stat(pb3, &ref) == -1)
262 atf_tc_fail_errno("stat of file");
265 * First try ops which should succeed.
268 /* rename within directory */
270 if (rump_sys_rename(pb1, pb3) == -1)
271 atf_tc_fail_errno("rename 1");
272 checkfile(pb3, &ref);
274 /* rename directory onto itself (two ways, should fail) */
275 md(pb1, mp, "dir3/.");
276 if (rump_sys_rename(pb1, pb3) != -1 || errno != EINVAL)
277 atf_tc_fail_errno("rename 2");
278 if (rump_sys_rename(pb3, pb1) != -1 || errno != EISDIR)
279 atf_tc_fail_errno("rename 3");
281 checkfile(pb3, &ref);
283 /* rename father of directory into directory */
284 md(pb1, mp, "dir2/dir");
286 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
287 atf_tc_fail_errno("rename 4");
289 /* same for grandfather */
290 md(pb1, mp, "dir2/subdir/dir2");
291 if (rump_sys_rename(pb2, pb1) != -1 || errno != EINVAL)
292 atf_tc_fail("rename 5");
294 checkfile(pb3, &ref);
296 /* rename directory over a non-empty directory */
297 if (rump_sys_rename(pb2, pb3) != -1 || errno != ENOTEMPTY)
298 atf_tc_fail("rename 6");
300 /* cross-directory rename */
302 md(pb2, mp, "dir2/somedir");
303 if (rump_sys_rename(pb1, pb2) == -1)
304 atf_tc_fail_errno("rename 7");
305 checkfile(pb2, &ref);
307 /* move to parent directory */
308 md(pb1, mp, "dir2/somedir/../../dir3");
309 if (rump_sys_rename(pb2, pb1) == -1)
310 atf_tc_fail_errno("rename 8");
311 md(pb1, mp, "dir2/../dir3");
312 checkfile(pb1, &ref);
314 /* atomic cross-directory rename */
315 md(pb3, mp, "dir2/subdir");
316 if (rump_sys_rename(pb1, pb3) == -1)
317 atf_tc_fail_errno("rename 9");
318 checkfile(pb3, &ref);
320 /* rename directory over an empty directory */
321 md(pb1, mp, "parent");
322 md(pb2, mp, "parent/dir1");
323 md(pb3, mp, "parent/dir2");
324 RL(rump_sys_mkdir(pb1, 0777));
325 RL(rump_sys_mkdir(pb2, 0777));
326 RL(rump_sys_mkdir(pb3, 0777));
327 RL(rump_sys_rename(pb2, pb3));
329 RL(rump_sys_stat(pb1, &sb));
330 if (! FSTYPE_MSDOS(tc))
331 ATF_CHECK_EQ(sb.st_nlink, 3);
332 RL(rump_sys_rmdir(pb3));
333 RL(rump_sys_rmdir(pb1));
337 rename_dotdot(const atf_tc_t *tc, const char *mp)
340 if (FSTYPE_RUMPFS(tc))
341 atf_tc_skip("rename not supported by file system");
345 if (rump_sys_chdir(mp) == -1)
346 atf_tc_fail_errno("chdir mountpoint");
348 if (rump_sys_mkdir("dir1", 0777) == -1)
349 atf_tc_fail_errno("mkdir 1");
350 if (rump_sys_mkdir("dir2", 0777) == -1)
351 atf_tc_fail_errno("mkdir 2");
353 if (rump_sys_rename("dir1", "dir1/..") != -1 || errno != EINVAL)
354 atf_tc_fail_errno("self-dotdot to");
356 if (rump_sys_rename("dir1/..", "sometarget") != -1 || errno != EINVAL)
357 atf_tc_fail_errno("self-dotdot from");
359 if (rump_sys_rename("dir1", "dir2/..") != -1 || errno != EINVAL)
360 atf_tc_fail("other-dotdot");
366 rename_reg_nodir(const atf_tc_t *tc, const char *mp)
372 if (FSTYPE_RUMPFS(tc))
373 atf_tc_skip("rename not supported by file system");
375 if (rump_sys_chdir(mp) == -1)
376 atf_tc_fail_errno("chdir mountpoint");
378 if (FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))
383 if (rump_sys_mknod("file1", S_IFREG | 0777, -1) == -1)
384 atf_tc_fail_errno("create file");
385 if (rump_sys_mknod("file2", S_IFREG | 0777, -1) == -1)
386 atf_tc_fail_errno("create file");
388 if (rump_sys_stat("file1", &sb) == -1)
389 atf_tc_fail_errno("stat");
393 if (rump_sys_link("file1", "file_link") == -1)
394 atf_tc_fail_errno("link");
395 if (rump_sys_stat("file_link", &sb) == -1)
396 atf_tc_fail_errno("stat");
397 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
398 ATF_REQUIRE_EQ(sb.st_nlink, 2);
401 if (rump_sys_stat("file2", &sb) == -1)
402 atf_tc_fail_errno("stat");
404 if (rump_sys_rename("file1", "file3") == -1)
405 atf_tc_fail_errno("rename 1");
406 if (rump_sys_stat("file3", &sb) == -1)
407 atf_tc_fail_errno("stat 1");
409 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
411 if (rump_sys_stat("file1", &sb) != -1 || errno != ENOENT)
412 atf_tc_fail_errno("source 1");
414 if (rump_sys_rename("file3", "file2") == -1)
415 atf_tc_fail_errno("rename 2");
416 if (rump_sys_stat("file2", &sb) == -1)
417 atf_tc_fail_errno("stat 2");
419 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
422 if (rump_sys_stat("file3", &sb) != -1 || errno != ENOENT)
423 atf_tc_fail_errno("source 2");
426 if (rump_sys_rename("file2", "file_link") == -1)
427 atf_tc_fail_errno("rename hardlink");
428 if (rump_sys_stat("file2", &sb) != -1 || errno != ENOENT)
429 atf_tc_fail_errno("source 3");
430 if (rump_sys_stat("file_link", &sb) == -1)
431 atf_tc_fail_errno("stat 2");
432 ATF_REQUIRE_EQ(sb.st_ino, f1ino);
433 ATF_REQUIRE_EQ(sb.st_nlink, 1);
436 ATF_CHECK_ERRNO(EFAULT, rump_sys_rename("file2", NULL) == -1);
437 ATF_CHECK_ERRNO(EFAULT, rump_sys_rename(NULL, "file2") == -1);
444 create_many(const atf_tc_t *tc, const char *mp)
447 int nfiles = 2324; /* #Nancy */
450 /* takes forever with many files */
451 if (FSTYPE_MSDOS(tc))
454 RL(rump_sys_chdir(mp));
456 if (FSTYPE_SYSVBFS(tc)) {
457 /* fs doesn't support many files or subdirectories */
460 /* msdosfs doesn't like many entries in the root directory */
461 RL(rump_sys_mkdir("subdir", 0777));
462 RL(rump_sys_chdir("subdir"));
466 #define TESTFN "testfile"
467 for (i = 0; i < nfiles; i++) {
470 sprintf(buf, TESTFN "%d", i);
471 RL(fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666));
472 RL(rump_sys_close(fd));
476 for (i = 0; i < nfiles; i++) {
477 sprintf(buf, TESTFN "%d", i);
478 RL(rump_sys_unlink(buf));
486 * Test creating files with one-character names using all possible
487 * character values. Failures to create the file are ignored as the
488 * characters allowed in file names vary by file system, but at least
489 * we can check that the fs does not crash, and if the file is
490 * successfully created, unlinking it should also succeed.
493 create_nonalphanum(const atf_tc_t *tc, const char *mp)
498 RL(rump_sys_chdir(mp));
500 for (i = 0; i < 256; i++) {
502 sprintf(buf, "%c", i);
503 fd = rump_sys_open(buf, O_RDWR|O_CREAT|O_EXCL, 0666);
506 RL(rump_sys_close(fd));
507 RL(rump_sys_unlink(buf));
515 create_nametoolong(const atf_tc_t *tc, const char *mp)
522 if (rump_sys_chdir(mp) == -1)
523 atf_tc_fail_errno("chdir mountpoint");
525 val = rump_sys_pathconf(".", _PC_NAME_MAX);
527 atf_tc_fail_errno("pathconf");
530 name = malloc(len+1);
532 atf_tc_fail_errno("malloc");
534 memset(name, 'a', len);
537 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
539 atf_tc_fail_errno("pathconf");
541 fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666);
542 if (val != 0 && (fd != -1 || errno != ENAMETOOLONG))
543 atf_tc_fail_errno("open");
545 if (val == 0 && rump_sys_close(fd) == -1)
546 atf_tc_fail_errno("close");
547 if (val == 0 && rump_sys_unlink(name) == -1)
548 atf_tc_fail_errno("unlink");
556 create_exist(const atf_tc_t *tc, const char *mp)
558 const char *name = "hoge";
561 RL(rump_sys_chdir(mp));
562 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666));
563 RL(rump_sys_close(fd));
564 RL(rump_sys_unlink(name));
565 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
566 RL(rump_sys_close(fd));
567 RL(fd = rump_sys_open(name, O_RDWR|O_CREAT, 0666));
568 RL(rump_sys_close(fd));
569 ATF_REQUIRE_ERRNO(EEXIST,
570 (fd = rump_sys_open(name, O_RDWR|O_CREAT|O_EXCL, 0666)));
571 RL(rump_sys_unlink(name));
572 RL(rump_sys_chdir("/"));
576 rename_nametoolong(const atf_tc_t *tc, const char *mp)
583 if (FSTYPE_RUMPFS(tc))
584 atf_tc_skip("rename not supported by file system");
586 if (rump_sys_chdir(mp) == -1)
587 atf_tc_fail_errno("chdir mountpoint");
589 val = rump_sys_pathconf(".", _PC_NAME_MAX);
591 atf_tc_fail_errno("pathconf");
594 name = malloc(len+1);
596 atf_tc_fail_errno("malloc");
598 memset(name, 'a', len);
601 fd = rump_sys_open("dummy", O_RDWR|O_CREAT, 0666);
603 atf_tc_fail_errno("open");
604 if (rump_sys_close(fd) == -1)
605 atf_tc_fail_errno("close");
607 val = rump_sys_pathconf(".", _PC_NO_TRUNC);
609 atf_tc_fail_errno("pathconf");
611 res = rump_sys_rename("dummy", name);
612 if (val != 0 && (res != -1 || errno != ENAMETOOLONG))
613 atf_tc_fail_errno("rename");
615 if (val == 0 && rump_sys_unlink(name) == -1)
616 atf_tc_fail_errno("unlink");
624 * Test creating a symlink whose length is "len" bytes, not including
625 * the terminating NUL.
628 symlink_len(const atf_tc_t *tc, const char *mp, size_t len)
635 RL(rump_sys_chdir(mp));
637 buf = malloc(len + 1);
639 memset(buf, 'a', len);
641 r = rump_sys_symlink(buf, "afile");
643 ATF_REQUIRE_ERRNO(ENAMETOOLONG, r);
645 RL(rump_sys_unlink("afile"));
649 RL(rump_sys_chdir("/"));
653 symlink_zerolen(const atf_tc_t *tc, const char *mp)
655 symlink_len(tc, mp, 0);
659 symlink_long(const atf_tc_t *tc, const char *mp)
662 * Test lengths close to powers of two, as those are likely
667 for (len = 2; len <= 65536; len *= 2) {
668 for (fuzz = -1; fuzz <= 1; fuzz++) {
669 symlink_len(tc, mp, len + fuzz);
675 symlink_root(const atf_tc_t *tc, const char *mp)
680 RL(rump_sys_chdir(mp));
681 RL(rump_sys_symlink("/", "foo"));
682 RL(rump_sys_chdir("foo"));
686 attrs(const atf_tc_t *tc, const char *mp)
689 struct timeval tv[2];
693 RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
694 RL(rump_sys_close(fd));
695 RL(rump_sys_stat(TESTFILE, &sb));
696 if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
697 RL(rump_sys_chown(TESTFILE, 1, 2));
700 RL(rump_sys_chmod(TESTFILE, 0123));
701 sb.st_mode = (sb.st_mode & ~ACCESSPERMS) | 0123;
704 tv[0].tv_sec = 1000000000; /* need something >1980 for msdosfs */
706 tv[1].tv_sec = 1000000002; /* need even seconds for msdosfs */
708 RL(rump_sys_utimes(TESTFILE, tv));
709 RL(rump_sys_utimes(TESTFILE, tv)); /* XXX: utimes & birthtime */
710 sb.st_atimespec.tv_sec = 1000000000;
711 sb.st_atimespec.tv_nsec = 1000;
712 sb.st_mtimespec.tv_sec = 1000000002;
713 sb.st_mtimespec.tv_nsec = 3000;
715 RL(rump_sys_stat(TESTFILE, &sb2));
716 #define CHECK(a) ATF_REQUIRE_EQ(sb.a, sb2.a)
717 if (!(FSTYPE_MSDOS(tc) || FSTYPE_SYSVBFS(tc))) {
722 if (!FSTYPE_MSDOS(tc)) {
723 /* msdosfs has only access date, not time */
724 CHECK(st_atimespec.tv_sec);
726 CHECK(st_mtimespec.tv_sec);
727 if (!(FSTYPE_EXT2FS(tc) || FSTYPE_MSDOS(tc) ||
728 FSTYPE_SYSVBFS(tc) || FSTYPE_V7FS(tc))) {
729 CHECK(st_atimespec.tv_nsec);
730 CHECK(st_mtimespec.tv_nsec);
738 fcntl_lock(const atf_tc_t *tc, const char *mp)
742 struct lwp *lwp1, *lwp2;
746 l.l_start = l.l_len = 1024;
747 l.l_type = F_RDLCK | F_WRLCK;
748 l.l_whence = SEEK_END;
750 lwp1 = rump_pub_lwproc_curlwp();
751 RL(fd = rump_sys_open(TESTFILE, O_RDWR | O_CREAT, 0755));
752 RL(rump_sys_ftruncate(fd, 8192));
754 RL(rump_sys_fcntl(fd, F_SETLK, &l));
756 /* Next, we fork and try to lock the same area */
757 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
758 lwp2 = rump_pub_lwproc_curlwp();
759 RL(fd2 = rump_sys_open(TESTFILE, O_RDWR, 0));
760 ATF_REQUIRE_ERRNO(EAGAIN, rump_sys_fcntl(fd2, F_SETLK, &l));
762 /* Switch back and unlock... */
763 rump_pub_lwproc_switch(lwp1);
765 RL(rump_sys_fcntl(fd, F_SETLK, &l));
767 /* ... and try to lock again */
768 rump_pub_lwproc_switch(lwp2);
769 l.l_type = F_RDLCK | F_WRLCK;
770 RL(rump_sys_fcntl(fd2, F_SETLK, &l));
772 RL(rump_sys_close(fd2));
773 rump_pub_lwproc_releaselwp();
775 RL(rump_sys_close(fd));
781 flock_compare(const void *p, const void *q)
783 int a = ((const struct flock *)p)->l_start;
784 int b = ((const struct flock *)q)->l_start;
785 return a < b ? -1 : (a > b ? 1 : 0);
789 * Find all locks set by fcntl_getlock_pids test
790 * using GETLK for a range [start, start+end], and,
791 * if there is a blocking lock, recursively find
792 * all locks to the left (toward the beginning of
793 * a file) and to the right of the lock.
794 * The function also understands "until end of file"
795 * convention when len==0.
798 fcntl_getlocks(int fildes, off_t start, off_t len,
799 struct flock *lock, struct flock *end)
802 const struct flock l = { start, len, 0, F_RDLCK, SEEK_SET };
807 RL(rump_sys_fcntl(fildes, F_GETLK, &l));
809 if (l.l_type == F_UNLCK)
815 ATF_REQUIRE(l.l_whence == SEEK_SET);
817 if (l.l_start > start) {
819 fcntl_getlocks(fildes, start, l.l_start - start, lock, end);
826 if (l.l_len == 0) /* does l spans until the end? */
829 if (len == 0) /* are we looking for locks until the end? */ {
830 rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
831 } else if (l.l_start + l.l_len < start + len) {
832 len -= l.l_start + l.l_len - start;
833 rv += fcntl_getlocks(fildes, l.l_start + l.l_len, len, lock, end);
840 fcntl_getlock_pids(const atf_tc_t *tc, const char *mp)
842 /* test non-overlaping ranges */
843 struct flock expect[4];
844 const struct flock lock[4] = {
845 { 0, 2, 0, F_WRLCK, SEEK_SET },
846 { 2, 1, 0, F_WRLCK, SEEK_SET },
847 { 7, 5, 0, F_WRLCK, SEEK_SET },
848 { 4, 3, 0, F_WRLCK, SEEK_SET },
851 /* Add extra element to make sure recursion does't stop at array end */
852 struct flock result[5];
854 /* Add 5th process */
860 const off_t sz = 8192;
862 int oflags = O_RDWR | O_CREAT;
864 memcpy(expect, lock, sizeof(lock));
869 * First, we create 4 processes and let each lock a range of the
870 * file. Note that the third and fourth processes lock in
871 * "reverse" order, i.e. the greater pid locks a range before
873 * Then, we create 5th process which doesn't lock anything.
875 for (i = 0; i < __arraycount(lwp); i++) {
876 RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
878 lwp[i] = rump_pub_lwproc_curlwp();
879 pid[i] = rump_sys_getpid();
881 RL(fd[i] = rump_sys_open(TESTFILE, oflags, omode));
885 RL(rump_sys_ftruncate(fd[i], sz));
887 if (i < __arraycount(lock)) {
888 RL(rump_sys_fcntl(fd[i], F_SETLK, &lock[i]));
889 expect[i].l_pid = pid[i];
893 qsort(expect, __arraycount(expect), sizeof(expect[0]), &flock_compare);
896 * In the context of each process, recursively find all locks
897 * that would block the current process. Processes 1-4 don't
898 * see their own lock, we insert it to simplify checks.
899 * Process 5 sees all 4 locks.
901 for (i = 0; i < __arraycount(lwp); i++) {
904 rump_pub_lwproc_switch(lwp[i]);
906 memset(result, 0, sizeof(result));
907 nlocks = fcntl_getlocks(fd[i], 0, sz,
908 result, result + __arraycount(result));
910 if (i < __arraycount(lock)) {
911 ATF_REQUIRE(nlocks < __arraycount(result));
912 result[nlocks] = lock[i];
913 result[nlocks].l_pid = pid[i];
917 ATF_CHECK_EQ(nlocks, __arraycount(expect));
919 qsort(result, nlocks, sizeof(result[0]), &flock_compare);
921 for (j = 0; j < nlocks; j++) {
922 ATF_CHECK_EQ(result[j].l_start, expect[j].l_start );
923 ATF_CHECK_EQ(result[j].l_len, expect[j].l_len );
924 ATF_CHECK_EQ(result[j].l_pid, expect[j].l_pid );
925 ATF_CHECK_EQ(result[j].l_type, expect[j].l_type );
926 ATF_CHECK_EQ(result[j].l_whence, expect[j].l_whence);
931 * Release processes. This also releases the fds and locks
932 * making fs unmount possible
934 for (i = 0; i < __arraycount(lwp); i++) {
935 rump_pub_lwproc_switch(lwp[i]);
936 rump_pub_lwproc_releaselwp();
943 access_simple(const atf_tc_t *tc, const char *mp)
949 RL(fd = rump_sys_open("tfile", O_CREAT | O_RDWR, 0777));
950 RL(rump_sys_close(fd));
952 #define ALLACC (F_OK | X_OK | W_OK | R_OK)
953 if (FSTYPE_SYSVBFS(tc) || FSTYPE_MSDOS(tc))
958 RL(rump_sys_access("tfile", tmode));
961 ATF_REQUIRE_ERRNO(EINVAL, rump_sys_access("tfile", ALLACC+1) == -1);
967 read_directory(const atf_tc_t *tc, const char *mp)
974 fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777);
975 ATF_REQUIRE(fd != -1);
977 size = rump_sys_pread(fd, buf, sizeof(buf), 0);
978 ATF_CHECK(size != -1 || errno == EISDIR);
979 size = rump_sys_read(fd, buf, sizeof(buf));
980 ATF_CHECK(size != -1 || errno == EISDIR);
982 res = rump_sys_close(fd);
983 ATF_REQUIRE(res != -1);
988 lstat_symlink(const atf_tc_t *tc, const char *mp)
990 const char *src, *dst;
1001 res = rump_sys_symlink(src, dst);
1002 ATF_REQUIRE(res != -1);
1003 res = rump_sys_lstat(dst, &st);
1004 ATF_REQUIRE(res != -1);
1006 ATF_CHECK(S_ISLNK(st.st_mode) != 0);
1007 ATF_CHECK(st.st_size == (off_t)strlen(src));
1012 ATF_TC_FSAPPLY(lookup_simple, "simple lookup (./.. on root)");
1013 ATF_TC_FSAPPLY(lookup_complex, "lookup of non-dot entries");
1014 ATF_TC_FSAPPLY(dir_simple, "mkdir/rmdir");
1015 ATF_TC_FSAPPLY(dir_notempty, "non-empty directories cannot be removed");
1016 ATF_TC_FSAPPLY(dir_rmdirdotdot, "remove .. and try to cd out (PR kern/44657)");
1017 ATF_TC_FSAPPLY(rename_dir, "exercise various directory renaming ops "
1019 ATF_TC_FSAPPLY(rename_dotdot, "rename dir .. (PR kern/43617)");
1020 ATF_TC_FSAPPLY(rename_reg_nodir, "rename regular files, no subdirectories");
1021 ATF_TC_FSAPPLY(create_nametoolong, "create file with name too long");
1022 ATF_TC_FSAPPLY(create_exist, "create with O_EXCL");
1023 ATF_TC_FSAPPLY(rename_nametoolong, "rename to file with name too long");
1024 ATF_TC_FSAPPLY(symlink_zerolen, "symlink with target of length 0");
1025 ATF_TC_FSAPPLY(symlink_long, "symlink with target of length > 0");
1026 ATF_TC_FSAPPLY(symlink_root, "symlink to root directory");
1027 ATF_TC_FSAPPLY(attrs, "check setting attributes works");
1028 ATF_TC_FSAPPLY(fcntl_lock, "check fcntl F_SETLK");
1029 ATF_TC_FSAPPLY(fcntl_getlock_pids,"fcntl F_GETLK w/ many procs, PR kern/44494");
1030 ATF_TC_FSAPPLY(access_simple, "access(2)");
1031 ATF_TC_FSAPPLY(read_directory, "read(2) on directories");
1032 ATF_TC_FSAPPLY(lstat_symlink, "lstat(2) values for symbolic links");
1034 #undef FSTEST_IMGSIZE
1035 #define FSTEST_IMGSIZE (1024*1024*64)
1036 ATF_TC_FSAPPLY(create_many, "create many directory entries");
1037 ATF_TC_FSAPPLY(create_nonalphanum, "non-alphanumeric filenames");
1042 ATF_TP_FSAPPLY(lookup_simple);
1043 ATF_TP_FSAPPLY(lookup_complex);
1044 ATF_TP_FSAPPLY(dir_simple);
1045 ATF_TP_FSAPPLY(dir_notempty);
1046 ATF_TP_FSAPPLY(dir_rmdirdotdot);
1047 ATF_TP_FSAPPLY(rename_dir);
1048 ATF_TP_FSAPPLY(rename_dotdot);
1049 ATF_TP_FSAPPLY(rename_reg_nodir);
1050 ATF_TP_FSAPPLY(create_many);
1051 ATF_TP_FSAPPLY(create_nonalphanum);
1052 ATF_TP_FSAPPLY(create_nametoolong);
1053 ATF_TP_FSAPPLY(create_exist);
1054 ATF_TP_FSAPPLY(rename_nametoolong);
1055 ATF_TP_FSAPPLY(symlink_zerolen);
1056 ATF_TP_FSAPPLY(symlink_long);
1057 ATF_TP_FSAPPLY(symlink_root);
1058 ATF_TP_FSAPPLY(attrs);
1059 ATF_TP_FSAPPLY(fcntl_lock);
1060 ATF_TP_FSAPPLY(fcntl_getlock_pids);
1061 ATF_TP_FSAPPLY(access_simple);
1062 ATF_TP_FSAPPLY(read_directory);
1063 ATF_TP_FSAPPLY(lstat_symlink);
1065 return atf_no_error();