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