1 /* 2 * 3 * Copyright (C) 2011 Novell Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <linux/fs.h> 11 #include <linux/namei.h> 12 #include <linux/xattr.h> 13 #include <linux/security.h> 14 #include <linux/cred.h> 15 #include "overlayfs.h" 16 17 void ovl_cleanup(struct inode *wdir, struct dentry *wdentry) 18 { 19 int err; 20 21 dget(wdentry); 22 if (d_is_dir(wdentry)) 23 err = ovl_do_rmdir(wdir, wdentry); 24 else 25 err = ovl_do_unlink(wdir, wdentry); 26 dput(wdentry); 27 28 if (err) { 29 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n", 30 wdentry, err); 31 } 32 } 33 34 struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry) 35 { 36 struct dentry *temp; 37 char name[20]; 38 39 snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry); 40 41 temp = lookup_one_len(name, workdir, strlen(name)); 42 if (!IS_ERR(temp) && temp->d_inode) { 43 pr_err("overlayfs: workdir/%s already exists\n", name); 44 dput(temp); 45 temp = ERR_PTR(-EIO); 46 } 47 48 return temp; 49 } 50 51 /* caller holds i_mutex on workdir */ 52 static struct dentry *ovl_whiteout(struct dentry *workdir, 53 struct dentry *dentry) 54 { 55 int err; 56 struct dentry *whiteout; 57 struct inode *wdir = workdir->d_inode; 58 59 whiteout = ovl_lookup_temp(workdir, dentry); 60 if (IS_ERR(whiteout)) 61 return whiteout; 62 63 err = ovl_do_whiteout(wdir, whiteout); 64 if (err) { 65 dput(whiteout); 66 whiteout = ERR_PTR(err); 67 } 68 69 return whiteout; 70 } 71 72 int ovl_create_real(struct inode *dir, struct dentry *newdentry, 73 struct kstat *stat, const char *link, 74 struct dentry *hardlink, bool debug) 75 { 76 int err; 77 78 if (newdentry->d_inode) 79 return -ESTALE; 80 81 if (hardlink) { 82 err = ovl_do_link(hardlink, dir, newdentry, debug); 83 } else { 84 switch (stat->mode & S_IFMT) { 85 case S_IFREG: 86 err = ovl_do_create(dir, newdentry, stat->mode, debug); 87 break; 88 89 case S_IFDIR: 90 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug); 91 break; 92 93 case S_IFCHR: 94 case S_IFBLK: 95 case S_IFIFO: 96 case S_IFSOCK: 97 err = ovl_do_mknod(dir, newdentry, 98 stat->mode, stat->rdev, debug); 99 break; 100 101 case S_IFLNK: 102 err = ovl_do_symlink(dir, newdentry, link, debug); 103 break; 104 105 default: 106 err = -EPERM; 107 } 108 } 109 if (!err && WARN_ON(!newdentry->d_inode)) { 110 /* 111 * Not quite sure if non-instantiated dentry is legal or not. 112 * VFS doesn't seem to care so check and warn here. 113 */ 114 err = -ENOENT; 115 } 116 return err; 117 } 118 119 static int ovl_set_opaque(struct dentry *upperdentry) 120 { 121 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0); 122 } 123 124 static void ovl_remove_opaque(struct dentry *upperdentry) 125 { 126 int err; 127 128 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE); 129 if (err) { 130 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n", 131 upperdentry->d_name.name, err); 132 } 133 } 134 135 static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, 136 struct kstat *stat) 137 { 138 int err; 139 enum ovl_path_type type; 140 struct path realpath; 141 142 type = ovl_path_real(dentry, &realpath); 143 err = vfs_getattr(&realpath, stat); 144 if (err) 145 return err; 146 147 stat->dev = dentry->d_sb->s_dev; 148 stat->ino = dentry->d_inode->i_ino; 149 150 /* 151 * It's probably not worth it to count subdirs to get the 152 * correct link count. nlink=1 seems to pacify 'find' and 153 * other utilities. 154 */ 155 if (OVL_TYPE_MERGE(type)) 156 stat->nlink = 1; 157 158 return 0; 159 } 160 161 static int ovl_create_upper(struct dentry *dentry, struct inode *inode, 162 struct kstat *stat, const char *link, 163 struct dentry *hardlink) 164 { 165 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 166 struct inode *udir = upperdir->d_inode; 167 struct dentry *newdentry; 168 int err; 169 170 inode_lock_nested(udir, I_MUTEX_PARENT); 171 newdentry = lookup_one_len(dentry->d_name.name, upperdir, 172 dentry->d_name.len); 173 err = PTR_ERR(newdentry); 174 if (IS_ERR(newdentry)) 175 goto out_unlock; 176 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false); 177 if (err) 178 goto out_dput; 179 180 ovl_dentry_version_inc(dentry->d_parent); 181 ovl_dentry_update(dentry, newdentry); 182 ovl_copyattr(newdentry->d_inode, inode); 183 d_instantiate(dentry, inode); 184 newdentry = NULL; 185 out_dput: 186 dput(newdentry); 187 out_unlock: 188 inode_unlock(udir); 189 return err; 190 } 191 192 static int ovl_lock_rename_workdir(struct dentry *workdir, 193 struct dentry *upperdir) 194 { 195 /* Workdir should not be the same as upperdir */ 196 if (workdir == upperdir) 197 goto err; 198 199 /* Workdir should not be subdir of upperdir and vice versa */ 200 if (lock_rename(workdir, upperdir) != NULL) 201 goto err_unlock; 202 203 return 0; 204 205 err_unlock: 206 unlock_rename(workdir, upperdir); 207 err: 208 pr_err("overlayfs: failed to lock workdir+upperdir\n"); 209 return -EIO; 210 } 211 212 static struct dentry *ovl_clear_empty(struct dentry *dentry, 213 struct list_head *list) 214 { 215 struct dentry *workdir = ovl_workdir(dentry); 216 struct inode *wdir = workdir->d_inode; 217 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 218 struct inode *udir = upperdir->d_inode; 219 struct path upperpath; 220 struct dentry *upper; 221 struct dentry *opaquedir; 222 struct kstat stat; 223 int err; 224 225 if (WARN_ON(!workdir)) 226 return ERR_PTR(-EROFS); 227 228 err = ovl_lock_rename_workdir(workdir, upperdir); 229 if (err) 230 goto out; 231 232 ovl_path_upper(dentry, &upperpath); 233 err = vfs_getattr(&upperpath, &stat); 234 if (err) 235 goto out_unlock; 236 237 err = -ESTALE; 238 if (!S_ISDIR(stat.mode)) 239 goto out_unlock; 240 upper = upperpath.dentry; 241 if (upper->d_parent->d_inode != udir) 242 goto out_unlock; 243 244 opaquedir = ovl_lookup_temp(workdir, dentry); 245 err = PTR_ERR(opaquedir); 246 if (IS_ERR(opaquedir)) 247 goto out_unlock; 248 249 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true); 250 if (err) 251 goto out_dput; 252 253 err = ovl_copy_xattr(upper, opaquedir); 254 if (err) 255 goto out_cleanup; 256 257 err = ovl_set_opaque(opaquedir); 258 if (err) 259 goto out_cleanup; 260 261 inode_lock(opaquedir->d_inode); 262 err = ovl_set_attr(opaquedir, &stat); 263 inode_unlock(opaquedir->d_inode); 264 if (err) 265 goto out_cleanup; 266 267 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE); 268 if (err) 269 goto out_cleanup; 270 271 ovl_cleanup_whiteouts(upper, list); 272 ovl_cleanup(wdir, upper); 273 unlock_rename(workdir, upperdir); 274 275 /* dentry's upper doesn't match now, get rid of it */ 276 d_drop(dentry); 277 278 return opaquedir; 279 280 out_cleanup: 281 ovl_cleanup(wdir, opaquedir); 282 out_dput: 283 dput(opaquedir); 284 out_unlock: 285 unlock_rename(workdir, upperdir); 286 out: 287 return ERR_PTR(err); 288 } 289 290 static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry) 291 { 292 int err; 293 struct dentry *ret = NULL; 294 LIST_HEAD(list); 295 296 err = ovl_check_empty_dir(dentry, &list); 297 if (err) 298 ret = ERR_PTR(err); 299 else { 300 /* 301 * If no upperdentry then skip clearing whiteouts. 302 * 303 * Can race with copy-up, since we don't hold the upperdir 304 * mutex. Doesn't matter, since copy-up can't create a 305 * non-empty directory from an empty one. 306 */ 307 if (ovl_dentry_upper(dentry)) 308 ret = ovl_clear_empty(dentry, &list); 309 } 310 311 ovl_cache_free(&list); 312 313 return ret; 314 } 315 316 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, 317 struct kstat *stat, const char *link, 318 struct dentry *hardlink) 319 { 320 struct dentry *workdir = ovl_workdir(dentry); 321 struct inode *wdir = workdir->d_inode; 322 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 323 struct inode *udir = upperdir->d_inode; 324 struct dentry *upper; 325 struct dentry *newdentry; 326 int err; 327 328 if (WARN_ON(!workdir)) 329 return -EROFS; 330 331 err = ovl_lock_rename_workdir(workdir, upperdir); 332 if (err) 333 goto out; 334 335 newdentry = ovl_lookup_temp(workdir, dentry); 336 err = PTR_ERR(newdentry); 337 if (IS_ERR(newdentry)) 338 goto out_unlock; 339 340 upper = lookup_one_len(dentry->d_name.name, upperdir, 341 dentry->d_name.len); 342 err = PTR_ERR(upper); 343 if (IS_ERR(upper)) 344 goto out_dput; 345 346 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true); 347 if (err) 348 goto out_dput2; 349 350 if (S_ISDIR(stat->mode)) { 351 err = ovl_set_opaque(newdentry); 352 if (err) 353 goto out_cleanup; 354 355 err = ovl_do_rename(wdir, newdentry, udir, upper, 356 RENAME_EXCHANGE); 357 if (err) 358 goto out_cleanup; 359 360 ovl_cleanup(wdir, upper); 361 } else { 362 err = ovl_do_rename(wdir, newdentry, udir, upper, 0); 363 if (err) 364 goto out_cleanup; 365 } 366 ovl_dentry_version_inc(dentry->d_parent); 367 ovl_dentry_update(dentry, newdentry); 368 ovl_copyattr(newdentry->d_inode, inode); 369 d_instantiate(dentry, inode); 370 newdentry = NULL; 371 out_dput2: 372 dput(upper); 373 out_dput: 374 dput(newdentry); 375 out_unlock: 376 unlock_rename(workdir, upperdir); 377 out: 378 return err; 379 380 out_cleanup: 381 ovl_cleanup(wdir, newdentry); 382 goto out_dput2; 383 } 384 385 static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev, 386 const char *link, struct dentry *hardlink) 387 { 388 int err; 389 struct inode *inode; 390 struct kstat stat = { 391 .mode = mode, 392 .rdev = rdev, 393 }; 394 395 err = -ENOMEM; 396 inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata); 397 if (!inode) 398 goto out; 399 400 err = ovl_copy_up(dentry->d_parent); 401 if (err) 402 goto out_iput; 403 404 if (!ovl_dentry_is_opaque(dentry)) { 405 err = ovl_create_upper(dentry, inode, &stat, link, hardlink); 406 } else { 407 const struct cred *old_cred; 408 409 old_cred = ovl_override_creds(dentry->d_sb); 410 411 err = ovl_create_over_whiteout(dentry, inode, &stat, link, 412 hardlink); 413 414 revert_creds(old_cred); 415 } 416 417 if (!err) 418 inode = NULL; 419 out_iput: 420 iput(inode); 421 out: 422 return err; 423 } 424 425 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev, 426 const char *link) 427 { 428 int err; 429 430 err = ovl_want_write(dentry); 431 if (!err) { 432 err = ovl_create_or_link(dentry, mode, rdev, link, NULL); 433 ovl_drop_write(dentry); 434 } 435 436 return err; 437 } 438 439 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode, 440 bool excl) 441 { 442 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL); 443 } 444 445 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 446 { 447 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL); 448 } 449 450 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, 451 dev_t rdev) 452 { 453 /* Don't allow creation of "whiteout" on overlay */ 454 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV) 455 return -EPERM; 456 457 return ovl_create_object(dentry, mode, rdev, NULL); 458 } 459 460 static int ovl_symlink(struct inode *dir, struct dentry *dentry, 461 const char *link) 462 { 463 return ovl_create_object(dentry, S_IFLNK, 0, link); 464 } 465 466 static int ovl_link(struct dentry *old, struct inode *newdir, 467 struct dentry *new) 468 { 469 int err; 470 struct dentry *upper; 471 472 err = ovl_want_write(old); 473 if (err) 474 goto out; 475 476 err = ovl_copy_up(old); 477 if (err) 478 goto out_drop_write; 479 480 upper = ovl_dentry_upper(old); 481 err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper); 482 483 out_drop_write: 484 ovl_drop_write(old); 485 out: 486 return err; 487 } 488 489 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir) 490 { 491 struct dentry *workdir = ovl_workdir(dentry); 492 struct inode *wdir = workdir->d_inode; 493 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 494 struct inode *udir = upperdir->d_inode; 495 struct dentry *whiteout; 496 struct dentry *upper; 497 struct dentry *opaquedir = NULL; 498 int err; 499 500 if (WARN_ON(!workdir)) 501 return -EROFS; 502 503 if (is_dir) { 504 if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) { 505 opaquedir = ovl_check_empty_and_clear(dentry); 506 err = PTR_ERR(opaquedir); 507 if (IS_ERR(opaquedir)) 508 goto out; 509 } else { 510 LIST_HEAD(list); 511 512 /* 513 * When removing an empty opaque directory, then it 514 * makes no sense to replace it with an exact replica of 515 * itself. But emptiness still needs to be checked. 516 */ 517 err = ovl_check_empty_dir(dentry, &list); 518 ovl_cache_free(&list); 519 if (err) 520 goto out; 521 } 522 } 523 524 err = ovl_lock_rename_workdir(workdir, upperdir); 525 if (err) 526 goto out_dput; 527 528 whiteout = ovl_whiteout(workdir, dentry); 529 err = PTR_ERR(whiteout); 530 if (IS_ERR(whiteout)) 531 goto out_unlock; 532 533 upper = ovl_dentry_upper(dentry); 534 if (!upper) { 535 upper = lookup_one_len(dentry->d_name.name, upperdir, 536 dentry->d_name.len); 537 err = PTR_ERR(upper); 538 if (IS_ERR(upper)) 539 goto kill_whiteout; 540 541 err = ovl_do_rename(wdir, whiteout, udir, upper, 0); 542 dput(upper); 543 if (err) 544 goto kill_whiteout; 545 } else { 546 int flags = 0; 547 548 if (opaquedir) 549 upper = opaquedir; 550 err = -ESTALE; 551 if (upper->d_parent != upperdir) 552 goto kill_whiteout; 553 554 if (is_dir) 555 flags |= RENAME_EXCHANGE; 556 557 err = ovl_do_rename(wdir, whiteout, udir, upper, flags); 558 if (err) 559 goto kill_whiteout; 560 561 if (is_dir) 562 ovl_cleanup(wdir, upper); 563 } 564 ovl_dentry_version_inc(dentry->d_parent); 565 out_d_drop: 566 d_drop(dentry); 567 dput(whiteout); 568 out_unlock: 569 unlock_rename(workdir, upperdir); 570 out_dput: 571 dput(opaquedir); 572 out: 573 return err; 574 575 kill_whiteout: 576 ovl_cleanup(wdir, whiteout); 577 goto out_d_drop; 578 } 579 580 static int ovl_remove_upper(struct dentry *dentry, bool is_dir) 581 { 582 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 583 struct inode *dir = upperdir->d_inode; 584 struct dentry *upper; 585 int err; 586 587 inode_lock_nested(dir, I_MUTEX_PARENT); 588 upper = lookup_one_len(dentry->d_name.name, upperdir, 589 dentry->d_name.len); 590 err = PTR_ERR(upper); 591 if (IS_ERR(upper)) 592 goto out_unlock; 593 594 err = -ESTALE; 595 if (upper == ovl_dentry_upper(dentry)) { 596 if (is_dir) 597 err = vfs_rmdir(dir, upper); 598 else 599 err = vfs_unlink(dir, upper, NULL); 600 ovl_dentry_version_inc(dentry->d_parent); 601 } 602 dput(upper); 603 604 /* 605 * Keeping this dentry hashed would mean having to release 606 * upperpath/lowerpath, which could only be done if we are the 607 * sole user of this dentry. Too tricky... Just unhash for 608 * now. 609 */ 610 if (!err) 611 d_drop(dentry); 612 out_unlock: 613 inode_unlock(dir); 614 615 return err; 616 } 617 618 static inline int ovl_check_sticky(struct dentry *dentry) 619 { 620 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode; 621 struct inode *inode = ovl_dentry_real(dentry)->d_inode; 622 623 if (check_sticky(dir, inode)) 624 return -EPERM; 625 626 return 0; 627 } 628 629 static int ovl_do_remove(struct dentry *dentry, bool is_dir) 630 { 631 enum ovl_path_type type; 632 int err; 633 634 err = ovl_check_sticky(dentry); 635 if (err) 636 goto out; 637 638 err = ovl_want_write(dentry); 639 if (err) 640 goto out; 641 642 err = ovl_copy_up(dentry->d_parent); 643 if (err) 644 goto out_drop_write; 645 646 type = ovl_path_type(dentry); 647 if (OVL_TYPE_PURE_UPPER(type)) { 648 err = ovl_remove_upper(dentry, is_dir); 649 } else { 650 const struct cred *old_cred = ovl_override_creds(dentry->d_sb); 651 652 err = ovl_remove_and_whiteout(dentry, is_dir); 653 654 revert_creds(old_cred); 655 } 656 out_drop_write: 657 ovl_drop_write(dentry); 658 out: 659 return err; 660 } 661 662 static int ovl_unlink(struct inode *dir, struct dentry *dentry) 663 { 664 return ovl_do_remove(dentry, false); 665 } 666 667 static int ovl_rmdir(struct inode *dir, struct dentry *dentry) 668 { 669 return ovl_do_remove(dentry, true); 670 } 671 672 static int ovl_rename2(struct inode *olddir, struct dentry *old, 673 struct inode *newdir, struct dentry *new, 674 unsigned int flags) 675 { 676 int err; 677 enum ovl_path_type old_type; 678 enum ovl_path_type new_type; 679 struct dentry *old_upperdir; 680 struct dentry *new_upperdir; 681 struct dentry *olddentry; 682 struct dentry *newdentry; 683 struct dentry *trap; 684 bool old_opaque; 685 bool new_opaque; 686 bool cleanup_whiteout = false; 687 bool overwrite = !(flags & RENAME_EXCHANGE); 688 bool is_dir = d_is_dir(old); 689 bool new_is_dir = false; 690 struct dentry *opaquedir = NULL; 691 const struct cred *old_cred = NULL; 692 693 err = -EINVAL; 694 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE)) 695 goto out; 696 697 flags &= ~RENAME_NOREPLACE; 698 699 err = ovl_check_sticky(old); 700 if (err) 701 goto out; 702 703 /* Don't copy up directory trees */ 704 old_type = ovl_path_type(old); 705 err = -EXDEV; 706 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir) 707 goto out; 708 709 if (new->d_inode) { 710 err = ovl_check_sticky(new); 711 if (err) 712 goto out; 713 714 if (d_is_dir(new)) 715 new_is_dir = true; 716 717 new_type = ovl_path_type(new); 718 err = -EXDEV; 719 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) 720 goto out; 721 722 err = 0; 723 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) { 724 if (ovl_dentry_lower(old)->d_inode == 725 ovl_dentry_lower(new)->d_inode) 726 goto out; 727 } 728 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) { 729 if (ovl_dentry_upper(old)->d_inode == 730 ovl_dentry_upper(new)->d_inode) 731 goto out; 732 } 733 } else { 734 if (ovl_dentry_is_opaque(new)) 735 new_type = __OVL_PATH_UPPER; 736 else 737 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE; 738 } 739 740 err = ovl_want_write(old); 741 if (err) 742 goto out; 743 744 err = ovl_copy_up(old); 745 if (err) 746 goto out_drop_write; 747 748 err = ovl_copy_up(new->d_parent); 749 if (err) 750 goto out_drop_write; 751 if (!overwrite) { 752 err = ovl_copy_up(new); 753 if (err) 754 goto out_drop_write; 755 } 756 757 old_opaque = !OVL_TYPE_PURE_UPPER(old_type); 758 new_opaque = !OVL_TYPE_PURE_UPPER(new_type); 759 760 if (old_opaque || new_opaque) 761 old_cred = ovl_override_creds(old->d_sb); 762 763 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) { 764 opaquedir = ovl_check_empty_and_clear(new); 765 err = PTR_ERR(opaquedir); 766 if (IS_ERR(opaquedir)) { 767 opaquedir = NULL; 768 goto out_revert_creds; 769 } 770 } 771 772 if (overwrite) { 773 if (old_opaque) { 774 if (new->d_inode || !new_opaque) { 775 /* Whiteout source */ 776 flags |= RENAME_WHITEOUT; 777 } else { 778 /* Switch whiteouts */ 779 flags |= RENAME_EXCHANGE; 780 } 781 } else if (is_dir && !new->d_inode && new_opaque) { 782 flags |= RENAME_EXCHANGE; 783 cleanup_whiteout = true; 784 } 785 } 786 787 old_upperdir = ovl_dentry_upper(old->d_parent); 788 new_upperdir = ovl_dentry_upper(new->d_parent); 789 790 trap = lock_rename(new_upperdir, old_upperdir); 791 792 793 olddentry = lookup_one_len(old->d_name.name, old_upperdir, 794 old->d_name.len); 795 err = PTR_ERR(olddentry); 796 if (IS_ERR(olddentry)) 797 goto out_unlock; 798 799 err = -ESTALE; 800 if (olddentry != ovl_dentry_upper(old)) 801 goto out_dput_old; 802 803 newdentry = lookup_one_len(new->d_name.name, new_upperdir, 804 new->d_name.len); 805 err = PTR_ERR(newdentry); 806 if (IS_ERR(newdentry)) 807 goto out_dput_old; 808 809 err = -ESTALE; 810 if (ovl_dentry_upper(new)) { 811 if (opaquedir) { 812 if (newdentry != opaquedir) 813 goto out_dput; 814 } else { 815 if (newdentry != ovl_dentry_upper(new)) 816 goto out_dput; 817 } 818 } else { 819 if (!d_is_negative(newdentry) && 820 (!new_opaque || !ovl_is_whiteout(newdentry))) 821 goto out_dput; 822 } 823 824 if (olddentry == trap) 825 goto out_dput; 826 if (newdentry == trap) 827 goto out_dput; 828 829 if (is_dir && !old_opaque && new_opaque) { 830 err = ovl_set_opaque(olddentry); 831 if (err) 832 goto out_dput; 833 } 834 if (!overwrite && new_is_dir && old_opaque && !new_opaque) { 835 err = ovl_set_opaque(newdentry); 836 if (err) 837 goto out_dput; 838 } 839 840 if (old_opaque || new_opaque) { 841 err = ovl_do_rename(old_upperdir->d_inode, olddentry, 842 new_upperdir->d_inode, newdentry, 843 flags); 844 } else { 845 /* No debug for the plain case */ 846 BUG_ON(flags & ~RENAME_EXCHANGE); 847 err = vfs_rename(old_upperdir->d_inode, olddentry, 848 new_upperdir->d_inode, newdentry, 849 NULL, flags); 850 } 851 852 if (err) { 853 if (is_dir && !old_opaque && new_opaque) 854 ovl_remove_opaque(olddentry); 855 if (!overwrite && new_is_dir && old_opaque && !new_opaque) 856 ovl_remove_opaque(newdentry); 857 goto out_dput; 858 } 859 860 if (is_dir && old_opaque && !new_opaque) 861 ovl_remove_opaque(olddentry); 862 if (!overwrite && new_is_dir && !old_opaque && new_opaque) 863 ovl_remove_opaque(newdentry); 864 865 /* 866 * Old dentry now lives in different location. Dentries in 867 * lowerstack are stale. We cannot drop them here because 868 * access to them is lockless. This could be only pure upper 869 * or opaque directory - numlower is zero. Or upper non-dir 870 * entry - its pureness is tracked by flag opaque. 871 */ 872 if (old_opaque != new_opaque) { 873 ovl_dentry_set_opaque(old, new_opaque); 874 if (!overwrite) 875 ovl_dentry_set_opaque(new, old_opaque); 876 } 877 878 if (cleanup_whiteout) 879 ovl_cleanup(old_upperdir->d_inode, newdentry); 880 881 ovl_dentry_version_inc(old->d_parent); 882 ovl_dentry_version_inc(new->d_parent); 883 884 out_dput: 885 dput(newdentry); 886 out_dput_old: 887 dput(olddentry); 888 out_unlock: 889 unlock_rename(new_upperdir, old_upperdir); 890 out_revert_creds: 891 if (old_opaque || new_opaque) 892 revert_creds(old_cred); 893 out_drop_write: 894 ovl_drop_write(old); 895 out: 896 dput(opaquedir); 897 return err; 898 } 899 900 const struct inode_operations ovl_dir_inode_operations = { 901 .lookup = ovl_lookup, 902 .mkdir = ovl_mkdir, 903 .symlink = ovl_symlink, 904 .unlink = ovl_unlink, 905 .rmdir = ovl_rmdir, 906 .rename2 = ovl_rename2, 907 .link = ovl_link, 908 .setattr = ovl_setattr, 909 .create = ovl_create, 910 .mknod = ovl_mknod, 911 .permission = ovl_permission, 912 .getattr = ovl_dir_getattr, 913 .setxattr = ovl_setxattr, 914 .getxattr = ovl_getxattr, 915 .listxattr = ovl_listxattr, 916 .removexattr = ovl_removexattr, 917 }; 918