1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 */ 6 7 #include <linux/fs.h> 8 #include <linux/namei.h> 9 #include <linux/xattr.h> 10 #include <linux/security.h> 11 #include <linux/cred.h> 12 #include <linux/module.h> 13 #include <linux/posix_acl.h> 14 #include <linux/posix_acl_xattr.h> 15 #include <linux/atomic.h> 16 #include <linux/ratelimit.h> 17 #include <linux/backing-file.h> 18 #include "overlayfs.h" 19 20 static unsigned short ovl_redirect_max = 256; 21 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644); 22 MODULE_PARM_DESC(redirect_max, 23 "Maximum length of absolute redirect xattr value"); 24 25 static int ovl_set_redirect(struct dentry *dentry, bool samedir); 26 27 int ovl_cleanup(struct ovl_fs *ofs, struct inode *wdir, struct dentry *wdentry) 28 { 29 int err; 30 31 dget(wdentry); 32 if (d_is_dir(wdentry)) 33 err = ovl_do_rmdir(ofs, wdir, wdentry); 34 else 35 err = ovl_do_unlink(ofs, wdir, wdentry); 36 dput(wdentry); 37 38 if (err) { 39 pr_err("cleanup of '%pd2' failed (%i)\n", 40 wdentry, err); 41 } 42 43 return err; 44 } 45 46 struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir) 47 { 48 struct dentry *temp; 49 char name[20]; 50 static atomic_t temp_id = ATOMIC_INIT(0); 51 52 /* counter is allowed to wrap, since temp dentries are ephemeral */ 53 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id)); 54 55 temp = ovl_lookup_upper(ofs, name, workdir, strlen(name)); 56 if (!IS_ERR(temp) && temp->d_inode) { 57 pr_err("workdir/%s already exists\n", name); 58 dput(temp); 59 temp = ERR_PTR(-EIO); 60 } 61 62 return temp; 63 } 64 65 /* caller holds i_mutex on workdir */ 66 static struct dentry *ovl_whiteout(struct ovl_fs *ofs) 67 { 68 int err; 69 struct dentry *whiteout; 70 struct dentry *workdir = ofs->workdir; 71 struct inode *wdir = workdir->d_inode; 72 73 if (!ofs->whiteout) { 74 whiteout = ovl_lookup_temp(ofs, workdir); 75 if (IS_ERR(whiteout)) 76 goto out; 77 78 err = ovl_do_whiteout(ofs, wdir, whiteout); 79 if (err) { 80 dput(whiteout); 81 whiteout = ERR_PTR(err); 82 goto out; 83 } 84 ofs->whiteout = whiteout; 85 } 86 87 if (!ofs->no_shared_whiteout) { 88 whiteout = ovl_lookup_temp(ofs, workdir); 89 if (IS_ERR(whiteout)) 90 goto out; 91 92 err = ovl_do_link(ofs, ofs->whiteout, wdir, whiteout); 93 if (!err) 94 goto out; 95 96 if (err != -EMLINK) { 97 pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n", 98 ofs->whiteout->d_inode->i_nlink, err); 99 ofs->no_shared_whiteout = true; 100 } 101 dput(whiteout); 102 } 103 whiteout = ofs->whiteout; 104 ofs->whiteout = NULL; 105 out: 106 return whiteout; 107 } 108 109 /* Caller must hold i_mutex on both workdir and dir */ 110 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir, 111 struct dentry *dentry) 112 { 113 struct inode *wdir = ofs->workdir->d_inode; 114 struct dentry *whiteout; 115 int err; 116 int flags = 0; 117 118 whiteout = ovl_whiteout(ofs); 119 err = PTR_ERR(whiteout); 120 if (IS_ERR(whiteout)) 121 return err; 122 123 if (d_is_dir(dentry)) 124 flags = RENAME_EXCHANGE; 125 126 err = ovl_do_rename(ofs, wdir, whiteout, dir, dentry, flags); 127 if (err) 128 goto kill_whiteout; 129 if (flags) 130 ovl_cleanup(ofs, wdir, dentry); 131 132 out: 133 dput(whiteout); 134 return err; 135 136 kill_whiteout: 137 ovl_cleanup(ofs, wdir, whiteout); 138 goto out; 139 } 140 141 int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir, 142 struct dentry **newdentry, umode_t mode) 143 { 144 int err; 145 struct dentry *d, *dentry = *newdentry; 146 147 err = ovl_do_mkdir(ofs, dir, dentry, mode); 148 if (err) 149 return err; 150 151 if (likely(!d_unhashed(dentry))) 152 return 0; 153 154 /* 155 * vfs_mkdir() may succeed and leave the dentry passed 156 * to it unhashed and negative. If that happens, try to 157 * lookup a new hashed and positive dentry. 158 */ 159 d = ovl_lookup_upper(ofs, dentry->d_name.name, dentry->d_parent, 160 dentry->d_name.len); 161 if (IS_ERR(d)) { 162 pr_warn("failed lookup after mkdir (%pd2, err=%i).\n", 163 dentry, err); 164 return PTR_ERR(d); 165 } 166 dput(dentry); 167 *newdentry = d; 168 169 return 0; 170 } 171 172 struct dentry *ovl_create_real(struct ovl_fs *ofs, struct inode *dir, 173 struct dentry *newdentry, struct ovl_cattr *attr) 174 { 175 int err; 176 177 if (IS_ERR(newdentry)) 178 return newdentry; 179 180 err = -ESTALE; 181 if (newdentry->d_inode) 182 goto out; 183 184 if (attr->hardlink) { 185 err = ovl_do_link(ofs, attr->hardlink, dir, newdentry); 186 } else { 187 switch (attr->mode & S_IFMT) { 188 case S_IFREG: 189 err = ovl_do_create(ofs, dir, newdentry, attr->mode); 190 break; 191 192 case S_IFDIR: 193 /* mkdir is special... */ 194 err = ovl_mkdir_real(ofs, dir, &newdentry, attr->mode); 195 break; 196 197 case S_IFCHR: 198 case S_IFBLK: 199 case S_IFIFO: 200 case S_IFSOCK: 201 err = ovl_do_mknod(ofs, dir, newdentry, attr->mode, 202 attr->rdev); 203 break; 204 205 case S_IFLNK: 206 err = ovl_do_symlink(ofs, dir, newdentry, attr->link); 207 break; 208 209 default: 210 err = -EPERM; 211 } 212 } 213 if (!err && WARN_ON(!newdentry->d_inode)) { 214 /* 215 * Not quite sure if non-instantiated dentry is legal or not. 216 * VFS doesn't seem to care so check and warn here. 217 */ 218 err = -EIO; 219 } 220 out: 221 if (err) { 222 dput(newdentry); 223 return ERR_PTR(err); 224 } 225 return newdentry; 226 } 227 228 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir, 229 struct ovl_cattr *attr) 230 { 231 return ovl_create_real(ofs, d_inode(workdir), 232 ovl_lookup_temp(ofs, workdir), attr); 233 } 234 235 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper, 236 int xerr) 237 { 238 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 239 int err; 240 241 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr); 242 if (!err) 243 ovl_dentry_set_opaque(dentry); 244 245 return err; 246 } 247 248 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry) 249 { 250 /* 251 * Fail with -EIO when trying to create opaque dir and upper doesn't 252 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to 253 * return a specific error for noxattr case. 254 */ 255 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO); 256 } 257 258 /* 259 * Common operations required to be done after creation of file on upper. 260 * If @hardlink is false, then @inode is a pre-allocated inode, we may or 261 * may not use to instantiate the new dentry. 262 */ 263 static int ovl_instantiate(struct dentry *dentry, struct inode *inode, 264 struct dentry *newdentry, bool hardlink, struct file *tmpfile) 265 { 266 struct ovl_inode_params oip = { 267 .upperdentry = newdentry, 268 .newinode = inode, 269 }; 270 271 ovl_dentry_set_upper_alias(dentry); 272 ovl_dentry_init_reval(dentry, newdentry, NULL); 273 274 if (!hardlink) { 275 /* 276 * ovl_obtain_alias() can be called after ovl_create_real() 277 * and before we get here, so we may get an inode from cache 278 * with the same real upperdentry that is not the inode we 279 * pre-allocated. In this case we will use the cached inode 280 * to instantiate the new dentry. 281 * 282 * XXX: if we ever use ovl_obtain_alias() to decode directory 283 * file handles, need to use ovl_get_inode_locked() and 284 * d_instantiate_new() here to prevent from creating two 285 * hashed directory inode aliases. 286 */ 287 inode = ovl_get_inode(dentry->d_sb, &oip); 288 if (IS_ERR(inode)) 289 return PTR_ERR(inode); 290 if (inode == oip.newinode) 291 ovl_set_flag(OVL_UPPERDATA, inode); 292 } else { 293 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry)); 294 dput(newdentry); 295 inc_nlink(inode); 296 } 297 298 if (tmpfile) 299 d_mark_tmpfile(tmpfile, inode); 300 301 d_instantiate(dentry, inode); 302 if (inode != oip.newinode) { 303 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n", 304 dentry); 305 } 306 307 /* Force lookup of new upper hardlink to find its lower */ 308 if (hardlink) 309 d_drop(dentry); 310 311 return 0; 312 } 313 314 static bool ovl_type_merge(struct dentry *dentry) 315 { 316 return OVL_TYPE_MERGE(ovl_path_type(dentry)); 317 } 318 319 static bool ovl_type_origin(struct dentry *dentry) 320 { 321 return OVL_TYPE_ORIGIN(ovl_path_type(dentry)); 322 } 323 324 static int ovl_create_upper(struct dentry *dentry, struct inode *inode, 325 struct ovl_cattr *attr) 326 { 327 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 328 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 329 struct inode *udir = upperdir->d_inode; 330 struct dentry *newdentry; 331 int err; 332 333 inode_lock_nested(udir, I_MUTEX_PARENT); 334 newdentry = ovl_create_real(ofs, udir, 335 ovl_lookup_upper(ofs, dentry->d_name.name, 336 upperdir, dentry->d_name.len), 337 attr); 338 err = PTR_ERR(newdentry); 339 if (IS_ERR(newdentry)) 340 goto out_unlock; 341 342 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) && 343 !ovl_allow_offline_changes(ofs)) { 344 /* Setting opaque here is just an optimization, allow to fail */ 345 ovl_set_opaque(dentry, newdentry); 346 } 347 348 ovl_dir_modified(dentry->d_parent, false); 349 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink, NULL); 350 if (err) 351 goto out_cleanup; 352 out_unlock: 353 inode_unlock(udir); 354 return err; 355 356 out_cleanup: 357 ovl_cleanup(ofs, udir, newdentry); 358 dput(newdentry); 359 goto out_unlock; 360 } 361 362 static struct dentry *ovl_clear_empty(struct dentry *dentry, 363 struct list_head *list) 364 { 365 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 366 struct dentry *workdir = ovl_workdir(dentry); 367 struct inode *wdir = workdir->d_inode; 368 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 369 struct inode *udir = upperdir->d_inode; 370 struct path upperpath; 371 struct dentry *upper; 372 struct dentry *opaquedir; 373 struct kstat stat; 374 int err; 375 376 if (WARN_ON(!workdir)) 377 return ERR_PTR(-EROFS); 378 379 err = ovl_lock_rename_workdir(workdir, upperdir); 380 if (err) 381 goto out; 382 383 ovl_path_upper(dentry, &upperpath); 384 err = vfs_getattr(&upperpath, &stat, 385 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT); 386 if (err) 387 goto out_unlock; 388 389 err = -ESTALE; 390 if (!S_ISDIR(stat.mode)) 391 goto out_unlock; 392 upper = upperpath.dentry; 393 if (upper->d_parent->d_inode != udir) 394 goto out_unlock; 395 396 opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode)); 397 err = PTR_ERR(opaquedir); 398 if (IS_ERR(opaquedir)) 399 goto out_unlock; 400 401 err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir); 402 if (err) 403 goto out_cleanup; 404 405 err = ovl_set_opaque(dentry, opaquedir); 406 if (err) 407 goto out_cleanup; 408 409 inode_lock(opaquedir->d_inode); 410 err = ovl_set_attr(ofs, opaquedir, &stat); 411 inode_unlock(opaquedir->d_inode); 412 if (err) 413 goto out_cleanup; 414 415 err = ovl_do_rename(ofs, wdir, opaquedir, udir, upper, RENAME_EXCHANGE); 416 if (err) 417 goto out_cleanup; 418 419 ovl_cleanup_whiteouts(ofs, upper, list); 420 ovl_cleanup(ofs, wdir, upper); 421 unlock_rename(workdir, upperdir); 422 423 /* dentry's upper doesn't match now, get rid of it */ 424 d_drop(dentry); 425 426 return opaquedir; 427 428 out_cleanup: 429 ovl_cleanup(ofs, wdir, opaquedir); 430 dput(opaquedir); 431 out_unlock: 432 unlock_rename(workdir, upperdir); 433 out: 434 return ERR_PTR(err); 435 } 436 437 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry, 438 const char *acl_name, struct posix_acl *acl) 439 { 440 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl) 441 return 0; 442 443 return ovl_do_set_acl(ofs, upperdentry, acl_name, acl); 444 } 445 446 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode, 447 struct ovl_cattr *cattr) 448 { 449 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 450 struct dentry *workdir = ovl_workdir(dentry); 451 struct inode *wdir = workdir->d_inode; 452 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 453 struct inode *udir = upperdir->d_inode; 454 struct dentry *upper; 455 struct dentry *newdentry; 456 int err; 457 struct posix_acl *acl, *default_acl; 458 bool hardlink = !!cattr->hardlink; 459 460 if (WARN_ON(!workdir)) 461 return -EROFS; 462 463 if (!hardlink) { 464 err = posix_acl_create(dentry->d_parent->d_inode, 465 &cattr->mode, &default_acl, &acl); 466 if (err) 467 return err; 468 } 469 470 err = ovl_lock_rename_workdir(workdir, upperdir); 471 if (err) 472 goto out; 473 474 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir, 475 dentry->d_name.len); 476 err = PTR_ERR(upper); 477 if (IS_ERR(upper)) 478 goto out_unlock; 479 480 err = -ESTALE; 481 if (d_is_negative(upper) || !ovl_upper_is_whiteout(ofs, upper)) 482 goto out_dput; 483 484 newdentry = ovl_create_temp(ofs, workdir, cattr); 485 err = PTR_ERR(newdentry); 486 if (IS_ERR(newdentry)) 487 goto out_dput; 488 489 /* 490 * mode could have been mutilated due to umask (e.g. sgid directory) 491 */ 492 if (!hardlink && 493 !S_ISLNK(cattr->mode) && 494 newdentry->d_inode->i_mode != cattr->mode) { 495 struct iattr attr = { 496 .ia_valid = ATTR_MODE, 497 .ia_mode = cattr->mode, 498 }; 499 inode_lock(newdentry->d_inode); 500 err = ovl_do_notify_change(ofs, newdentry, &attr); 501 inode_unlock(newdentry->d_inode); 502 if (err) 503 goto out_cleanup; 504 } 505 if (!hardlink) { 506 err = ovl_set_upper_acl(ofs, newdentry, 507 XATTR_NAME_POSIX_ACL_ACCESS, acl); 508 if (err) 509 goto out_cleanup; 510 511 err = ovl_set_upper_acl(ofs, newdentry, 512 XATTR_NAME_POSIX_ACL_DEFAULT, default_acl); 513 if (err) 514 goto out_cleanup; 515 } 516 517 if (!hardlink && S_ISDIR(cattr->mode)) { 518 err = ovl_set_opaque(dentry, newdentry); 519 if (err) 520 goto out_cleanup; 521 522 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 523 RENAME_EXCHANGE); 524 if (err) 525 goto out_cleanup; 526 527 ovl_cleanup(ofs, wdir, upper); 528 } else { 529 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 0); 530 if (err) 531 goto out_cleanup; 532 } 533 ovl_dir_modified(dentry->d_parent, false); 534 err = ovl_instantiate(dentry, inode, newdentry, hardlink, NULL); 535 if (err) { 536 ovl_cleanup(ofs, udir, newdentry); 537 dput(newdentry); 538 } 539 out_dput: 540 dput(upper); 541 out_unlock: 542 unlock_rename(workdir, upperdir); 543 out: 544 if (!hardlink) { 545 posix_acl_release(acl); 546 posix_acl_release(default_acl); 547 } 548 return err; 549 550 out_cleanup: 551 ovl_cleanup(ofs, wdir, newdentry); 552 dput(newdentry); 553 goto out_dput; 554 } 555 556 static int ovl_setup_cred_for_create(struct dentry *dentry, struct inode *inode, 557 umode_t mode, const struct cred *old_cred) 558 { 559 int err; 560 struct cred *override_cred; 561 562 override_cred = prepare_creds(); 563 if (!override_cred) 564 return -ENOMEM; 565 566 override_cred->fsuid = inode->i_uid; 567 override_cred->fsgid = inode->i_gid; 568 err = security_dentry_create_files_as(dentry, mode, &dentry->d_name, 569 old_cred, override_cred); 570 if (err) { 571 put_cred(override_cred); 572 return err; 573 } 574 put_cred(override_creds(override_cred)); 575 put_cred(override_cred); 576 577 return 0; 578 } 579 580 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode, 581 struct ovl_cattr *attr, bool origin) 582 { 583 int err; 584 const struct cred *old_cred; 585 struct dentry *parent = dentry->d_parent; 586 587 old_cred = ovl_override_creds(dentry->d_sb); 588 589 /* 590 * When linking a file with copy up origin into a new parent, mark the 591 * new parent dir "impure". 592 */ 593 if (origin) { 594 err = ovl_set_impure(parent, ovl_dentry_upper(parent)); 595 if (err) 596 goto out_revert_creds; 597 } 598 599 if (!attr->hardlink) { 600 /* 601 * In the creation cases(create, mkdir, mknod, symlink), 602 * ovl should transfer current's fs{u,g}id to underlying 603 * fs. Because underlying fs want to initialize its new 604 * inode owner using current's fs{u,g}id. And in this 605 * case, the @inode is a new inode that is initialized 606 * in inode_init_owner() to current's fs{u,g}id. So use 607 * the inode's i_{u,g}id to override the cred's fs{u,g}id. 608 * 609 * But in the other hardlink case, ovl_link() does not 610 * create a new inode, so just use the ovl mounter's 611 * fs{u,g}id. 612 */ 613 err = ovl_setup_cred_for_create(dentry, inode, attr->mode, old_cred); 614 if (err) 615 goto out_revert_creds; 616 } 617 618 if (!ovl_dentry_is_whiteout(dentry)) 619 err = ovl_create_upper(dentry, inode, attr); 620 else 621 err = ovl_create_over_whiteout(dentry, inode, attr); 622 623 out_revert_creds: 624 revert_creds(old_cred); 625 return err; 626 } 627 628 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev, 629 const char *link) 630 { 631 int err; 632 struct inode *inode; 633 struct ovl_cattr attr = { 634 .rdev = rdev, 635 .link = link, 636 }; 637 638 err = ovl_copy_up(dentry->d_parent); 639 if (err) 640 return err; 641 642 err = ovl_want_write(dentry); 643 if (err) 644 goto out; 645 646 /* Preallocate inode to be used by ovl_get_inode() */ 647 err = -ENOMEM; 648 inode = ovl_new_inode(dentry->d_sb, mode, rdev); 649 if (!inode) 650 goto out_drop_write; 651 652 spin_lock(&inode->i_lock); 653 inode->i_state |= I_CREATING; 654 spin_unlock(&inode->i_lock); 655 656 inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode); 657 attr.mode = inode->i_mode; 658 659 err = ovl_create_or_link(dentry, inode, &attr, false); 660 /* Did we end up using the preallocated inode? */ 661 if (inode != d_inode(dentry)) 662 iput(inode); 663 664 out_drop_write: 665 ovl_drop_write(dentry); 666 out: 667 return err; 668 } 669 670 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir, 671 struct dentry *dentry, umode_t mode, bool excl) 672 { 673 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL); 674 } 675 676 static int ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir, 677 struct dentry *dentry, umode_t mode) 678 { 679 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL); 680 } 681 682 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir, 683 struct dentry *dentry, umode_t mode, dev_t rdev) 684 { 685 /* Don't allow creation of "whiteout" on overlay */ 686 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV) 687 return -EPERM; 688 689 return ovl_create_object(dentry, mode, rdev, NULL); 690 } 691 692 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir, 693 struct dentry *dentry, const char *link) 694 { 695 return ovl_create_object(dentry, S_IFLNK, 0, link); 696 } 697 698 static int ovl_set_link_redirect(struct dentry *dentry) 699 { 700 const struct cred *old_cred; 701 int err; 702 703 old_cred = ovl_override_creds(dentry->d_sb); 704 err = ovl_set_redirect(dentry, false); 705 revert_creds(old_cred); 706 707 return err; 708 } 709 710 static int ovl_link(struct dentry *old, struct inode *newdir, 711 struct dentry *new) 712 { 713 int err; 714 struct inode *inode; 715 716 err = ovl_copy_up(old); 717 if (err) 718 goto out; 719 720 err = ovl_copy_up(new->d_parent); 721 if (err) 722 goto out; 723 724 err = ovl_nlink_start(old); 725 if (err) 726 goto out; 727 728 if (ovl_is_metacopy_dentry(old)) { 729 err = ovl_set_link_redirect(old); 730 if (err) 731 goto out_nlink_end; 732 } 733 734 inode = d_inode(old); 735 ihold(inode); 736 737 err = ovl_create_or_link(new, inode, 738 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)}, 739 ovl_type_origin(old)); 740 if (err) 741 iput(inode); 742 743 out_nlink_end: 744 ovl_nlink_end(old); 745 out: 746 return err; 747 } 748 749 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper) 750 { 751 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper); 752 } 753 754 static int ovl_remove_and_whiteout(struct dentry *dentry, 755 struct list_head *list) 756 { 757 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 758 struct dentry *workdir = ovl_workdir(dentry); 759 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 760 struct dentry *upper; 761 struct dentry *opaquedir = NULL; 762 int err; 763 764 if (WARN_ON(!workdir)) 765 return -EROFS; 766 767 if (!list_empty(list)) { 768 opaquedir = ovl_clear_empty(dentry, list); 769 err = PTR_ERR(opaquedir); 770 if (IS_ERR(opaquedir)) 771 goto out; 772 } 773 774 err = ovl_lock_rename_workdir(workdir, upperdir); 775 if (err) 776 goto out_dput; 777 778 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir, 779 dentry->d_name.len); 780 err = PTR_ERR(upper); 781 if (IS_ERR(upper)) 782 goto out_unlock; 783 784 err = -ESTALE; 785 if ((opaquedir && upper != opaquedir) || 786 (!opaquedir && ovl_dentry_upper(dentry) && 787 !ovl_matches_upper(dentry, upper))) { 788 goto out_dput_upper; 789 } 790 791 err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper); 792 if (err) 793 goto out_d_drop; 794 795 ovl_dir_modified(dentry->d_parent, true); 796 out_d_drop: 797 d_drop(dentry); 798 out_dput_upper: 799 dput(upper); 800 out_unlock: 801 unlock_rename(workdir, upperdir); 802 out_dput: 803 dput(opaquedir); 804 out: 805 return err; 806 } 807 808 static int ovl_remove_upper(struct dentry *dentry, bool is_dir, 809 struct list_head *list) 810 { 811 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 812 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 813 struct inode *dir = upperdir->d_inode; 814 struct dentry *upper; 815 struct dentry *opaquedir = NULL; 816 int err; 817 818 if (!list_empty(list)) { 819 opaquedir = ovl_clear_empty(dentry, list); 820 err = PTR_ERR(opaquedir); 821 if (IS_ERR(opaquedir)) 822 goto out; 823 } 824 825 inode_lock_nested(dir, I_MUTEX_PARENT); 826 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir, 827 dentry->d_name.len); 828 err = PTR_ERR(upper); 829 if (IS_ERR(upper)) 830 goto out_unlock; 831 832 err = -ESTALE; 833 if ((opaquedir && upper != opaquedir) || 834 (!opaquedir && !ovl_matches_upper(dentry, upper))) 835 goto out_dput_upper; 836 837 if (is_dir) 838 err = ovl_do_rmdir(ofs, dir, upper); 839 else 840 err = ovl_do_unlink(ofs, dir, upper); 841 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry)); 842 843 /* 844 * Keeping this dentry hashed would mean having to release 845 * upperpath/lowerpath, which could only be done if we are the 846 * sole user of this dentry. Too tricky... Just unhash for 847 * now. 848 */ 849 if (!err) 850 d_drop(dentry); 851 out_dput_upper: 852 dput(upper); 853 out_unlock: 854 inode_unlock(dir); 855 dput(opaquedir); 856 out: 857 return err; 858 } 859 860 static bool ovl_pure_upper(struct dentry *dentry) 861 { 862 return !ovl_dentry_lower(dentry) && 863 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry)); 864 } 865 866 static void ovl_drop_nlink(struct dentry *dentry) 867 { 868 struct inode *inode = d_inode(dentry); 869 struct dentry *alias; 870 871 /* Try to find another, hashed alias */ 872 spin_lock(&inode->i_lock); 873 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 874 if (alias != dentry && !d_unhashed(alias)) 875 break; 876 } 877 spin_unlock(&inode->i_lock); 878 879 /* 880 * Changes to underlying layers may cause i_nlink to lose sync with 881 * reality. In this case prevent the link count from going to zero 882 * prematurely. 883 */ 884 if (inode->i_nlink > !!alias) 885 drop_nlink(inode); 886 } 887 888 static int ovl_do_remove(struct dentry *dentry, bool is_dir) 889 { 890 int err; 891 const struct cred *old_cred; 892 bool lower_positive = ovl_lower_positive(dentry); 893 LIST_HEAD(list); 894 895 /* No need to clean pure upper removed by vfs_rmdir() */ 896 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) { 897 err = ovl_check_empty_dir(dentry, &list); 898 if (err) 899 goto out; 900 } 901 902 err = ovl_copy_up(dentry->d_parent); 903 if (err) 904 goto out; 905 906 err = ovl_nlink_start(dentry); 907 if (err) 908 goto out; 909 910 old_cred = ovl_override_creds(dentry->d_sb); 911 if (!lower_positive) 912 err = ovl_remove_upper(dentry, is_dir, &list); 913 else 914 err = ovl_remove_and_whiteout(dentry, &list); 915 revert_creds(old_cred); 916 if (!err) { 917 if (is_dir) 918 clear_nlink(dentry->d_inode); 919 else 920 ovl_drop_nlink(dentry); 921 } 922 ovl_nlink_end(dentry); 923 924 /* 925 * Copy ctime 926 * 927 * Note: we fail to update ctime if there was no copy-up, only a 928 * whiteout 929 */ 930 if (ovl_dentry_upper(dentry)) 931 ovl_copyattr(d_inode(dentry)); 932 933 out: 934 ovl_cache_free(&list); 935 return err; 936 } 937 938 static int ovl_unlink(struct inode *dir, struct dentry *dentry) 939 { 940 return ovl_do_remove(dentry, false); 941 } 942 943 static int ovl_rmdir(struct inode *dir, struct dentry *dentry) 944 { 945 return ovl_do_remove(dentry, true); 946 } 947 948 static bool ovl_type_merge_or_lower(struct dentry *dentry) 949 { 950 enum ovl_path_type type = ovl_path_type(dentry); 951 952 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type); 953 } 954 955 static bool ovl_can_move(struct dentry *dentry) 956 { 957 return ovl_redirect_dir(OVL_FS(dentry->d_sb)) || 958 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry); 959 } 960 961 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect) 962 { 963 char *buf, *ret; 964 struct dentry *d, *tmp; 965 int buflen = ovl_redirect_max + 1; 966 967 if (!abs_redirect) { 968 ret = kstrndup(dentry->d_name.name, dentry->d_name.len, 969 GFP_KERNEL); 970 goto out; 971 } 972 973 buf = ret = kmalloc(buflen, GFP_KERNEL); 974 if (!buf) 975 goto out; 976 977 buflen--; 978 buf[buflen] = '\0'; 979 for (d = dget(dentry); !IS_ROOT(d);) { 980 const char *name; 981 int thislen; 982 983 spin_lock(&d->d_lock); 984 name = ovl_dentry_get_redirect(d); 985 if (name) { 986 thislen = strlen(name); 987 } else { 988 name = d->d_name.name; 989 thislen = d->d_name.len; 990 } 991 992 /* If path is too long, fall back to userspace move */ 993 if (thislen + (name[0] != '/') > buflen) { 994 ret = ERR_PTR(-EXDEV); 995 spin_unlock(&d->d_lock); 996 goto out_put; 997 } 998 999 buflen -= thislen; 1000 memcpy(&buf[buflen], name, thislen); 1001 spin_unlock(&d->d_lock); 1002 tmp = dget_parent(d); 1003 1004 dput(d); 1005 d = tmp; 1006 1007 /* Absolute redirect: finished */ 1008 if (buf[buflen] == '/') 1009 break; 1010 buflen--; 1011 buf[buflen] = '/'; 1012 } 1013 ret = kstrdup(&buf[buflen], GFP_KERNEL); 1014 out_put: 1015 dput(d); 1016 kfree(buf); 1017 out: 1018 return ret ? ret : ERR_PTR(-ENOMEM); 1019 } 1020 1021 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir) 1022 { 1023 struct dentry *lowerdentry; 1024 1025 if (!samedir) 1026 return true; 1027 1028 if (d_is_dir(dentry)) 1029 return false; 1030 1031 /* 1032 * For non-dir hardlinked files, we need absolute redirects 1033 * in general as two upper hardlinks could be in different 1034 * dirs. We could put a relative redirect now and convert 1035 * it to absolute redirect later. But when nlink > 1 and 1036 * indexing is on, that means relative redirect needs to be 1037 * converted to absolute during copy up of another lower 1038 * hardllink as well. 1039 * 1040 * So without optimizing too much, just check if lower is 1041 * a hard link or not. If lower is hard link, put absolute 1042 * redirect. 1043 */ 1044 lowerdentry = ovl_dentry_lower(dentry); 1045 return (d_inode(lowerdentry)->i_nlink > 1); 1046 } 1047 1048 static int ovl_set_redirect(struct dentry *dentry, bool samedir) 1049 { 1050 int err; 1051 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 1052 const char *redirect = ovl_dentry_get_redirect(dentry); 1053 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir); 1054 1055 if (redirect && (!absolute_redirect || redirect[0] == '/')) 1056 return 0; 1057 1058 redirect = ovl_get_redirect(dentry, absolute_redirect); 1059 if (IS_ERR(redirect)) 1060 return PTR_ERR(redirect); 1061 1062 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry), 1063 OVL_XATTR_REDIRECT, 1064 redirect, strlen(redirect), -EXDEV); 1065 if (!err) { 1066 spin_lock(&dentry->d_lock); 1067 ovl_dentry_set_redirect(dentry, redirect); 1068 spin_unlock(&dentry->d_lock); 1069 } else { 1070 kfree(redirect); 1071 pr_warn_ratelimited("failed to set redirect (%i)\n", 1072 err); 1073 /* Fall back to userspace copy-up */ 1074 err = -EXDEV; 1075 } 1076 return err; 1077 } 1078 1079 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir, 1080 struct dentry *old, struct inode *newdir, 1081 struct dentry *new, unsigned int flags) 1082 { 1083 int err; 1084 struct dentry *old_upperdir; 1085 struct dentry *new_upperdir; 1086 struct dentry *olddentry; 1087 struct dentry *newdentry; 1088 struct dentry *trap; 1089 bool old_opaque; 1090 bool new_opaque; 1091 bool cleanup_whiteout = false; 1092 bool update_nlink = false; 1093 bool overwrite = !(flags & RENAME_EXCHANGE); 1094 bool is_dir = d_is_dir(old); 1095 bool new_is_dir = d_is_dir(new); 1096 bool samedir = olddir == newdir; 1097 struct dentry *opaquedir = NULL; 1098 const struct cred *old_cred = NULL; 1099 struct ovl_fs *ofs = OVL_FS(old->d_sb); 1100 LIST_HEAD(list); 1101 1102 err = -EINVAL; 1103 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE)) 1104 goto out; 1105 1106 flags &= ~RENAME_NOREPLACE; 1107 1108 /* Don't copy up directory trees */ 1109 err = -EXDEV; 1110 if (!ovl_can_move(old)) 1111 goto out; 1112 if (!overwrite && !ovl_can_move(new)) 1113 goto out; 1114 1115 if (overwrite && new_is_dir && !ovl_pure_upper(new)) { 1116 err = ovl_check_empty_dir(new, &list); 1117 if (err) 1118 goto out; 1119 } 1120 1121 if (overwrite) { 1122 if (ovl_lower_positive(old)) { 1123 if (!ovl_dentry_is_whiteout(new)) { 1124 /* Whiteout source */ 1125 flags |= RENAME_WHITEOUT; 1126 } else { 1127 /* Switch whiteouts */ 1128 flags |= RENAME_EXCHANGE; 1129 } 1130 } else if (is_dir && ovl_dentry_is_whiteout(new)) { 1131 flags |= RENAME_EXCHANGE; 1132 cleanup_whiteout = true; 1133 } 1134 } 1135 1136 err = ovl_copy_up(old); 1137 if (err) 1138 goto out; 1139 1140 err = ovl_copy_up(new->d_parent); 1141 if (err) 1142 goto out; 1143 if (!overwrite) { 1144 err = ovl_copy_up(new); 1145 if (err) 1146 goto out; 1147 } else if (d_inode(new)) { 1148 err = ovl_nlink_start(new); 1149 if (err) 1150 goto out; 1151 1152 update_nlink = true; 1153 } 1154 1155 if (!update_nlink) { 1156 /* ovl_nlink_start() took ovl_want_write() */ 1157 err = ovl_want_write(old); 1158 if (err) 1159 goto out; 1160 } 1161 1162 old_cred = ovl_override_creds(old->d_sb); 1163 1164 if (!list_empty(&list)) { 1165 opaquedir = ovl_clear_empty(new, &list); 1166 err = PTR_ERR(opaquedir); 1167 if (IS_ERR(opaquedir)) { 1168 opaquedir = NULL; 1169 goto out_revert_creds; 1170 } 1171 } 1172 1173 old_upperdir = ovl_dentry_upper(old->d_parent); 1174 new_upperdir = ovl_dentry_upper(new->d_parent); 1175 1176 if (!samedir) { 1177 /* 1178 * When moving a merge dir or non-dir with copy up origin into 1179 * a new parent, we are marking the new parent dir "impure". 1180 * When ovl_iterate() iterates an "impure" upper dir, it will 1181 * lookup the origin inodes of the entries to fill d_ino. 1182 */ 1183 if (ovl_type_origin(old)) { 1184 err = ovl_set_impure(new->d_parent, new_upperdir); 1185 if (err) 1186 goto out_revert_creds; 1187 } 1188 if (!overwrite && ovl_type_origin(new)) { 1189 err = ovl_set_impure(old->d_parent, old_upperdir); 1190 if (err) 1191 goto out_revert_creds; 1192 } 1193 } 1194 1195 trap = lock_rename(new_upperdir, old_upperdir); 1196 if (IS_ERR(trap)) { 1197 err = PTR_ERR(trap); 1198 goto out_revert_creds; 1199 } 1200 1201 olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir, 1202 old->d_name.len); 1203 err = PTR_ERR(olddentry); 1204 if (IS_ERR(olddentry)) 1205 goto out_unlock; 1206 1207 err = -ESTALE; 1208 if (!ovl_matches_upper(old, olddentry)) 1209 goto out_dput_old; 1210 1211 newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir, 1212 new->d_name.len); 1213 err = PTR_ERR(newdentry); 1214 if (IS_ERR(newdentry)) 1215 goto out_dput_old; 1216 1217 old_opaque = ovl_dentry_is_opaque(old); 1218 new_opaque = ovl_dentry_is_opaque(new); 1219 1220 err = -ESTALE; 1221 if (d_inode(new) && ovl_dentry_upper(new)) { 1222 if (opaquedir) { 1223 if (newdentry != opaquedir) 1224 goto out_dput; 1225 } else { 1226 if (!ovl_matches_upper(new, newdentry)) 1227 goto out_dput; 1228 } 1229 } else { 1230 if (!d_is_negative(newdentry)) { 1231 if (!new_opaque || !ovl_upper_is_whiteout(ofs, newdentry)) 1232 goto out_dput; 1233 } else { 1234 if (flags & RENAME_EXCHANGE) 1235 goto out_dput; 1236 } 1237 } 1238 1239 if (olddentry == trap) 1240 goto out_dput; 1241 if (newdentry == trap) 1242 goto out_dput; 1243 1244 if (olddentry->d_inode == newdentry->d_inode) 1245 goto out_dput; 1246 1247 err = 0; 1248 if (ovl_type_merge_or_lower(old)) 1249 err = ovl_set_redirect(old, samedir); 1250 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent)) 1251 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV); 1252 if (err) 1253 goto out_dput; 1254 1255 if (!overwrite && ovl_type_merge_or_lower(new)) 1256 err = ovl_set_redirect(new, samedir); 1257 else if (!overwrite && new_is_dir && !new_opaque && 1258 ovl_type_merge(old->d_parent)) 1259 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV); 1260 if (err) 1261 goto out_dput; 1262 1263 err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry, 1264 new_upperdir->d_inode, newdentry, flags); 1265 if (err) 1266 goto out_dput; 1267 1268 if (cleanup_whiteout) 1269 ovl_cleanup(ofs, old_upperdir->d_inode, newdentry); 1270 1271 if (overwrite && d_inode(new)) { 1272 if (new_is_dir) 1273 clear_nlink(d_inode(new)); 1274 else 1275 ovl_drop_nlink(new); 1276 } 1277 1278 ovl_dir_modified(old->d_parent, ovl_type_origin(old) || 1279 (!overwrite && ovl_type_origin(new))); 1280 ovl_dir_modified(new->d_parent, ovl_type_origin(old) || 1281 (d_inode(new) && ovl_type_origin(new))); 1282 1283 /* copy ctime: */ 1284 ovl_copyattr(d_inode(old)); 1285 if (d_inode(new) && ovl_dentry_upper(new)) 1286 ovl_copyattr(d_inode(new)); 1287 1288 out_dput: 1289 dput(newdentry); 1290 out_dput_old: 1291 dput(olddentry); 1292 out_unlock: 1293 unlock_rename(new_upperdir, old_upperdir); 1294 out_revert_creds: 1295 revert_creds(old_cred); 1296 if (update_nlink) 1297 ovl_nlink_end(new); 1298 else 1299 ovl_drop_write(old); 1300 out: 1301 dput(opaquedir); 1302 ovl_cache_free(&list); 1303 return err; 1304 } 1305 1306 static int ovl_create_tmpfile(struct file *file, struct dentry *dentry, 1307 struct inode *inode, umode_t mode) 1308 { 1309 const struct cred *old_cred; 1310 struct path realparentpath; 1311 struct file *realfile; 1312 struct dentry *newdentry; 1313 /* It's okay to set O_NOATIME, since the owner will be current fsuid */ 1314 int flags = file->f_flags | OVL_OPEN_FLAGS; 1315 int err; 1316 1317 err = ovl_copy_up(dentry->d_parent); 1318 if (err) 1319 return err; 1320 1321 old_cred = ovl_override_creds(dentry->d_sb); 1322 err = ovl_setup_cred_for_create(dentry, inode, mode, old_cred); 1323 if (err) 1324 goto out_revert_creds; 1325 1326 ovl_path_upper(dentry->d_parent, &realparentpath); 1327 realfile = backing_tmpfile_open(&file->f_path, flags, &realparentpath, 1328 mode, current_cred()); 1329 err = PTR_ERR_OR_ZERO(realfile); 1330 pr_debug("tmpfile/open(%pd2, 0%o) = %i\n", realparentpath.dentry, mode, err); 1331 if (err) 1332 goto out_revert_creds; 1333 1334 /* ovl_instantiate() consumes the newdentry reference on success */ 1335 newdentry = dget(realfile->f_path.dentry); 1336 err = ovl_instantiate(dentry, inode, newdentry, false, file); 1337 if (!err) { 1338 file->private_data = realfile; 1339 } else { 1340 dput(newdentry); 1341 fput(realfile); 1342 } 1343 out_revert_creds: 1344 revert_creds(old_cred); 1345 return err; 1346 } 1347 1348 static int ovl_dummy_open(struct inode *inode, struct file *file) 1349 { 1350 return 0; 1351 } 1352 1353 static int ovl_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 1354 struct file *file, umode_t mode) 1355 { 1356 int err; 1357 struct dentry *dentry = file->f_path.dentry; 1358 struct inode *inode; 1359 1360 if (!OVL_FS(dentry->d_sb)->tmpfile) 1361 return -EOPNOTSUPP; 1362 1363 err = ovl_want_write(dentry); 1364 if (err) 1365 return err; 1366 1367 err = -ENOMEM; 1368 inode = ovl_new_inode(dentry->d_sb, mode, 0); 1369 if (!inode) 1370 goto drop_write; 1371 1372 inode_init_owner(&nop_mnt_idmap, inode, dir, mode); 1373 err = ovl_create_tmpfile(file, dentry, inode, inode->i_mode); 1374 if (err) 1375 goto put_inode; 1376 1377 /* 1378 * Check if the preallocated inode was actually used. Having something 1379 * else assigned to the dentry shouldn't happen as that would indicate 1380 * that the backing tmpfile "leaked" out of overlayfs. 1381 */ 1382 err = -EIO; 1383 if (WARN_ON(inode != d_inode(dentry))) 1384 goto put_realfile; 1385 1386 /* inode reference was transferred to dentry */ 1387 inode = NULL; 1388 err = finish_open(file, dentry, ovl_dummy_open); 1389 put_realfile: 1390 /* Without FMODE_OPENED ->release() won't be called on @file */ 1391 if (!(file->f_mode & FMODE_OPENED)) 1392 fput(file->private_data); 1393 put_inode: 1394 iput(inode); 1395 drop_write: 1396 ovl_drop_write(dentry); 1397 return err; 1398 } 1399 1400 const struct inode_operations ovl_dir_inode_operations = { 1401 .lookup = ovl_lookup, 1402 .mkdir = ovl_mkdir, 1403 .symlink = ovl_symlink, 1404 .unlink = ovl_unlink, 1405 .rmdir = ovl_rmdir, 1406 .rename = ovl_rename, 1407 .link = ovl_link, 1408 .setattr = ovl_setattr, 1409 .create = ovl_create, 1410 .mknod = ovl_mknod, 1411 .permission = ovl_permission, 1412 .getattr = ovl_getattr, 1413 .listxattr = ovl_listxattr, 1414 .get_inode_acl = ovl_get_inode_acl, 1415 .get_acl = ovl_get_acl, 1416 .set_acl = ovl_set_acl, 1417 .update_time = ovl_update_time, 1418 .fileattr_get = ovl_fileattr_get, 1419 .fileattr_set = ovl_fileattr_set, 1420 .tmpfile = ovl_tmpfile, 1421 }; 1422