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