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 const struct cred *ovl_setup_cred_for_create(struct dentry *dentry, 557 struct inode *inode, 558 umode_t mode, 559 const struct cred *old_cred) 560 { 561 int err; 562 struct cred *override_cred; 563 564 override_cred = prepare_creds(); 565 if (!override_cred) 566 return ERR_PTR(-ENOMEM); 567 568 override_cred->fsuid = inode->i_uid; 569 override_cred->fsgid = inode->i_gid; 570 err = security_dentry_create_files_as(dentry, mode, &dentry->d_name, 571 old_cred, override_cred); 572 if (err) { 573 put_cred(override_cred); 574 return ERR_PTR(err); 575 } 576 577 /* 578 * Caller is going to match this with revert_creds_light() and drop 579 * referenec on the returned creds. 580 * We must be called with creator creds already, otherwise we risk 581 * leaking creds. 582 */ 583 old_cred = override_creds_light(override_cred); 584 WARN_ON_ONCE(old_cred != ovl_creds(dentry->d_sb)); 585 586 return override_cred; 587 } 588 589 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode, 590 struct ovl_cattr *attr, bool origin) 591 { 592 int err; 593 const struct cred *old_cred, *new_cred = NULL; 594 struct dentry *parent = dentry->d_parent; 595 596 old_cred = ovl_override_creds(dentry->d_sb); 597 598 /* 599 * When linking a file with copy up origin into a new parent, mark the 600 * new parent dir "impure". 601 */ 602 if (origin) { 603 err = ovl_set_impure(parent, ovl_dentry_upper(parent)); 604 if (err) 605 goto out_revert_creds; 606 } 607 608 if (!attr->hardlink) { 609 /* 610 * In the creation cases(create, mkdir, mknod, symlink), 611 * ovl should transfer current's fs{u,g}id to underlying 612 * fs. Because underlying fs want to initialize its new 613 * inode owner using current's fs{u,g}id. And in this 614 * case, the @inode is a new inode that is initialized 615 * in inode_init_owner() to current's fs{u,g}id. So use 616 * the inode's i_{u,g}id to override the cred's fs{u,g}id. 617 * 618 * But in the other hardlink case, ovl_link() does not 619 * create a new inode, so just use the ovl mounter's 620 * fs{u,g}id. 621 */ 622 new_cred = ovl_setup_cred_for_create(dentry, inode, attr->mode, 623 old_cred); 624 err = PTR_ERR(new_cred); 625 if (IS_ERR(new_cred)) { 626 new_cred = NULL; 627 goto out_revert_creds; 628 } 629 } 630 631 if (!ovl_dentry_is_whiteout(dentry)) 632 err = ovl_create_upper(dentry, inode, attr); 633 else 634 err = ovl_create_over_whiteout(dentry, inode, attr); 635 636 out_revert_creds: 637 ovl_revert_creds(old_cred); 638 put_cred(new_cred); 639 return err; 640 } 641 642 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev, 643 const char *link) 644 { 645 int err; 646 struct inode *inode; 647 struct ovl_cattr attr = { 648 .rdev = rdev, 649 .link = link, 650 }; 651 652 err = ovl_copy_up(dentry->d_parent); 653 if (err) 654 return err; 655 656 err = ovl_want_write(dentry); 657 if (err) 658 goto out; 659 660 /* Preallocate inode to be used by ovl_get_inode() */ 661 err = -ENOMEM; 662 inode = ovl_new_inode(dentry->d_sb, mode, rdev); 663 if (!inode) 664 goto out_drop_write; 665 666 spin_lock(&inode->i_lock); 667 inode->i_state |= I_CREATING; 668 spin_unlock(&inode->i_lock); 669 670 inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode); 671 attr.mode = inode->i_mode; 672 673 err = ovl_create_or_link(dentry, inode, &attr, false); 674 /* Did we end up using the preallocated inode? */ 675 if (inode != d_inode(dentry)) 676 iput(inode); 677 678 out_drop_write: 679 ovl_drop_write(dentry); 680 out: 681 return err; 682 } 683 684 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir, 685 struct dentry *dentry, umode_t mode, bool excl) 686 { 687 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL); 688 } 689 690 static int ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir, 691 struct dentry *dentry, umode_t mode) 692 { 693 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL); 694 } 695 696 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir, 697 struct dentry *dentry, umode_t mode, dev_t rdev) 698 { 699 /* Don't allow creation of "whiteout" on overlay */ 700 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV) 701 return -EPERM; 702 703 return ovl_create_object(dentry, mode, rdev, NULL); 704 } 705 706 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir, 707 struct dentry *dentry, const char *link) 708 { 709 return ovl_create_object(dentry, S_IFLNK, 0, link); 710 } 711 712 static int ovl_set_link_redirect(struct dentry *dentry) 713 { 714 const struct cred *old_cred; 715 int err; 716 717 old_cred = ovl_override_creds(dentry->d_sb); 718 err = ovl_set_redirect(dentry, false); 719 ovl_revert_creds(old_cred); 720 721 return err; 722 } 723 724 static int ovl_link(struct dentry *old, struct inode *newdir, 725 struct dentry *new) 726 { 727 int err; 728 struct inode *inode; 729 730 err = ovl_copy_up(old); 731 if (err) 732 goto out; 733 734 err = ovl_copy_up(new->d_parent); 735 if (err) 736 goto out; 737 738 err = ovl_nlink_start(old); 739 if (err) 740 goto out; 741 742 if (ovl_is_metacopy_dentry(old)) { 743 err = ovl_set_link_redirect(old); 744 if (err) 745 goto out_nlink_end; 746 } 747 748 inode = d_inode(old); 749 ihold(inode); 750 751 err = ovl_create_or_link(new, inode, 752 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)}, 753 ovl_type_origin(old)); 754 if (err) 755 iput(inode); 756 757 out_nlink_end: 758 ovl_nlink_end(old); 759 out: 760 return err; 761 } 762 763 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper) 764 { 765 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper); 766 } 767 768 static int ovl_remove_and_whiteout(struct dentry *dentry, 769 struct list_head *list) 770 { 771 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 772 struct dentry *workdir = ovl_workdir(dentry); 773 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 774 struct dentry *upper; 775 struct dentry *opaquedir = NULL; 776 int err; 777 778 if (WARN_ON(!workdir)) 779 return -EROFS; 780 781 if (!list_empty(list)) { 782 opaquedir = ovl_clear_empty(dentry, list); 783 err = PTR_ERR(opaquedir); 784 if (IS_ERR(opaquedir)) 785 goto out; 786 } 787 788 err = ovl_lock_rename_workdir(workdir, upperdir); 789 if (err) 790 goto out_dput; 791 792 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir, 793 dentry->d_name.len); 794 err = PTR_ERR(upper); 795 if (IS_ERR(upper)) 796 goto out_unlock; 797 798 err = -ESTALE; 799 if ((opaquedir && upper != opaquedir) || 800 (!opaquedir && ovl_dentry_upper(dentry) && 801 !ovl_matches_upper(dentry, upper))) { 802 goto out_dput_upper; 803 } 804 805 err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper); 806 if (err) 807 goto out_d_drop; 808 809 ovl_dir_modified(dentry->d_parent, true); 810 out_d_drop: 811 d_drop(dentry); 812 out_dput_upper: 813 dput(upper); 814 out_unlock: 815 unlock_rename(workdir, upperdir); 816 out_dput: 817 dput(opaquedir); 818 out: 819 return err; 820 } 821 822 static int ovl_remove_upper(struct dentry *dentry, bool is_dir, 823 struct list_head *list) 824 { 825 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 826 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent); 827 struct inode *dir = upperdir->d_inode; 828 struct dentry *upper; 829 struct dentry *opaquedir = NULL; 830 int err; 831 832 if (!list_empty(list)) { 833 opaquedir = ovl_clear_empty(dentry, list); 834 err = PTR_ERR(opaquedir); 835 if (IS_ERR(opaquedir)) 836 goto out; 837 } 838 839 inode_lock_nested(dir, I_MUTEX_PARENT); 840 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir, 841 dentry->d_name.len); 842 err = PTR_ERR(upper); 843 if (IS_ERR(upper)) 844 goto out_unlock; 845 846 err = -ESTALE; 847 if ((opaquedir && upper != opaquedir) || 848 (!opaquedir && !ovl_matches_upper(dentry, upper))) 849 goto out_dput_upper; 850 851 if (is_dir) 852 err = ovl_do_rmdir(ofs, dir, upper); 853 else 854 err = ovl_do_unlink(ofs, dir, upper); 855 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry)); 856 857 /* 858 * Keeping this dentry hashed would mean having to release 859 * upperpath/lowerpath, which could only be done if we are the 860 * sole user of this dentry. Too tricky... Just unhash for 861 * now. 862 */ 863 if (!err) 864 d_drop(dentry); 865 out_dput_upper: 866 dput(upper); 867 out_unlock: 868 inode_unlock(dir); 869 dput(opaquedir); 870 out: 871 return err; 872 } 873 874 static bool ovl_pure_upper(struct dentry *dentry) 875 { 876 return !ovl_dentry_lower(dentry) && 877 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry)); 878 } 879 880 static void ovl_drop_nlink(struct dentry *dentry) 881 { 882 struct inode *inode = d_inode(dentry); 883 struct dentry *alias; 884 885 /* Try to find another, hashed alias */ 886 spin_lock(&inode->i_lock); 887 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { 888 if (alias != dentry && !d_unhashed(alias)) 889 break; 890 } 891 spin_unlock(&inode->i_lock); 892 893 /* 894 * Changes to underlying layers may cause i_nlink to lose sync with 895 * reality. In this case prevent the link count from going to zero 896 * prematurely. 897 */ 898 if (inode->i_nlink > !!alias) 899 drop_nlink(inode); 900 } 901 902 static int ovl_do_remove(struct dentry *dentry, bool is_dir) 903 { 904 int err; 905 const struct cred *old_cred; 906 bool lower_positive = ovl_lower_positive(dentry); 907 LIST_HEAD(list); 908 909 /* No need to clean pure upper removed by vfs_rmdir() */ 910 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) { 911 err = ovl_check_empty_dir(dentry, &list); 912 if (err) 913 goto out; 914 } 915 916 err = ovl_copy_up(dentry->d_parent); 917 if (err) 918 goto out; 919 920 err = ovl_nlink_start(dentry); 921 if (err) 922 goto out; 923 924 old_cred = ovl_override_creds(dentry->d_sb); 925 if (!lower_positive) 926 err = ovl_remove_upper(dentry, is_dir, &list); 927 else 928 err = ovl_remove_and_whiteout(dentry, &list); 929 ovl_revert_creds(old_cred); 930 if (!err) { 931 if (is_dir) 932 clear_nlink(dentry->d_inode); 933 else 934 ovl_drop_nlink(dentry); 935 } 936 ovl_nlink_end(dentry); 937 938 /* 939 * Copy ctime 940 * 941 * Note: we fail to update ctime if there was no copy-up, only a 942 * whiteout 943 */ 944 if (ovl_dentry_upper(dentry)) 945 ovl_copyattr(d_inode(dentry)); 946 947 out: 948 ovl_cache_free(&list); 949 return err; 950 } 951 952 static int ovl_unlink(struct inode *dir, struct dentry *dentry) 953 { 954 return ovl_do_remove(dentry, false); 955 } 956 957 static int ovl_rmdir(struct inode *dir, struct dentry *dentry) 958 { 959 return ovl_do_remove(dentry, true); 960 } 961 962 static bool ovl_type_merge_or_lower(struct dentry *dentry) 963 { 964 enum ovl_path_type type = ovl_path_type(dentry); 965 966 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type); 967 } 968 969 static bool ovl_can_move(struct dentry *dentry) 970 { 971 return ovl_redirect_dir(OVL_FS(dentry->d_sb)) || 972 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry); 973 } 974 975 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect) 976 { 977 char *buf, *ret; 978 struct dentry *d, *tmp; 979 int buflen = ovl_redirect_max + 1; 980 981 if (!abs_redirect) { 982 ret = kstrndup(dentry->d_name.name, dentry->d_name.len, 983 GFP_KERNEL); 984 goto out; 985 } 986 987 buf = ret = kmalloc(buflen, GFP_KERNEL); 988 if (!buf) 989 goto out; 990 991 buflen--; 992 buf[buflen] = '\0'; 993 for (d = dget(dentry); !IS_ROOT(d);) { 994 const char *name; 995 int thislen; 996 997 spin_lock(&d->d_lock); 998 name = ovl_dentry_get_redirect(d); 999 if (name) { 1000 thislen = strlen(name); 1001 } else { 1002 name = d->d_name.name; 1003 thislen = d->d_name.len; 1004 } 1005 1006 /* If path is too long, fall back to userspace move */ 1007 if (thislen + (name[0] != '/') > buflen) { 1008 ret = ERR_PTR(-EXDEV); 1009 spin_unlock(&d->d_lock); 1010 goto out_put; 1011 } 1012 1013 buflen -= thislen; 1014 memcpy(&buf[buflen], name, thislen); 1015 spin_unlock(&d->d_lock); 1016 tmp = dget_parent(d); 1017 1018 dput(d); 1019 d = tmp; 1020 1021 /* Absolute redirect: finished */ 1022 if (buf[buflen] == '/') 1023 break; 1024 buflen--; 1025 buf[buflen] = '/'; 1026 } 1027 ret = kstrdup(&buf[buflen], GFP_KERNEL); 1028 out_put: 1029 dput(d); 1030 kfree(buf); 1031 out: 1032 return ret ? ret : ERR_PTR(-ENOMEM); 1033 } 1034 1035 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir) 1036 { 1037 struct dentry *lowerdentry; 1038 1039 if (!samedir) 1040 return true; 1041 1042 if (d_is_dir(dentry)) 1043 return false; 1044 1045 /* 1046 * For non-dir hardlinked files, we need absolute redirects 1047 * in general as two upper hardlinks could be in different 1048 * dirs. We could put a relative redirect now and convert 1049 * it to absolute redirect later. But when nlink > 1 and 1050 * indexing is on, that means relative redirect needs to be 1051 * converted to absolute during copy up of another lower 1052 * hardllink as well. 1053 * 1054 * So without optimizing too much, just check if lower is 1055 * a hard link or not. If lower is hard link, put absolute 1056 * redirect. 1057 */ 1058 lowerdentry = ovl_dentry_lower(dentry); 1059 return (d_inode(lowerdentry)->i_nlink > 1); 1060 } 1061 1062 static int ovl_set_redirect(struct dentry *dentry, bool samedir) 1063 { 1064 int err; 1065 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 1066 const char *redirect = ovl_dentry_get_redirect(dentry); 1067 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir); 1068 1069 if (redirect && (!absolute_redirect || redirect[0] == '/')) 1070 return 0; 1071 1072 redirect = ovl_get_redirect(dentry, absolute_redirect); 1073 if (IS_ERR(redirect)) 1074 return PTR_ERR(redirect); 1075 1076 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry), 1077 OVL_XATTR_REDIRECT, 1078 redirect, strlen(redirect), -EXDEV); 1079 if (!err) { 1080 spin_lock(&dentry->d_lock); 1081 ovl_dentry_set_redirect(dentry, redirect); 1082 spin_unlock(&dentry->d_lock); 1083 } else { 1084 kfree(redirect); 1085 pr_warn_ratelimited("failed to set redirect (%i)\n", 1086 err); 1087 /* Fall back to userspace copy-up */ 1088 err = -EXDEV; 1089 } 1090 return err; 1091 } 1092 1093 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir, 1094 struct dentry *old, struct inode *newdir, 1095 struct dentry *new, unsigned int flags) 1096 { 1097 int err; 1098 struct dentry *old_upperdir; 1099 struct dentry *new_upperdir; 1100 struct dentry *olddentry; 1101 struct dentry *newdentry; 1102 struct dentry *trap; 1103 bool old_opaque; 1104 bool new_opaque; 1105 bool cleanup_whiteout = false; 1106 bool update_nlink = false; 1107 bool overwrite = !(flags & RENAME_EXCHANGE); 1108 bool is_dir = d_is_dir(old); 1109 bool new_is_dir = d_is_dir(new); 1110 bool samedir = olddir == newdir; 1111 struct dentry *opaquedir = NULL; 1112 const struct cred *old_cred = NULL; 1113 struct ovl_fs *ofs = OVL_FS(old->d_sb); 1114 LIST_HEAD(list); 1115 1116 err = -EINVAL; 1117 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE)) 1118 goto out; 1119 1120 flags &= ~RENAME_NOREPLACE; 1121 1122 /* Don't copy up directory trees */ 1123 err = -EXDEV; 1124 if (!ovl_can_move(old)) 1125 goto out; 1126 if (!overwrite && !ovl_can_move(new)) 1127 goto out; 1128 1129 if (overwrite && new_is_dir && !ovl_pure_upper(new)) { 1130 err = ovl_check_empty_dir(new, &list); 1131 if (err) 1132 goto out; 1133 } 1134 1135 if (overwrite) { 1136 if (ovl_lower_positive(old)) { 1137 if (!ovl_dentry_is_whiteout(new)) { 1138 /* Whiteout source */ 1139 flags |= RENAME_WHITEOUT; 1140 } else { 1141 /* Switch whiteouts */ 1142 flags |= RENAME_EXCHANGE; 1143 } 1144 } else if (is_dir && ovl_dentry_is_whiteout(new)) { 1145 flags |= RENAME_EXCHANGE; 1146 cleanup_whiteout = true; 1147 } 1148 } 1149 1150 err = ovl_copy_up(old); 1151 if (err) 1152 goto out; 1153 1154 err = ovl_copy_up(new->d_parent); 1155 if (err) 1156 goto out; 1157 if (!overwrite) { 1158 err = ovl_copy_up(new); 1159 if (err) 1160 goto out; 1161 } else if (d_inode(new)) { 1162 err = ovl_nlink_start(new); 1163 if (err) 1164 goto out; 1165 1166 update_nlink = true; 1167 } 1168 1169 if (!update_nlink) { 1170 /* ovl_nlink_start() took ovl_want_write() */ 1171 err = ovl_want_write(old); 1172 if (err) 1173 goto out; 1174 } 1175 1176 old_cred = ovl_override_creds(old->d_sb); 1177 1178 if (!list_empty(&list)) { 1179 opaquedir = ovl_clear_empty(new, &list); 1180 err = PTR_ERR(opaquedir); 1181 if (IS_ERR(opaquedir)) { 1182 opaquedir = NULL; 1183 goto out_revert_creds; 1184 } 1185 } 1186 1187 old_upperdir = ovl_dentry_upper(old->d_parent); 1188 new_upperdir = ovl_dentry_upper(new->d_parent); 1189 1190 if (!samedir) { 1191 /* 1192 * When moving a merge dir or non-dir with copy up origin into 1193 * a new parent, we are marking the new parent dir "impure". 1194 * When ovl_iterate() iterates an "impure" upper dir, it will 1195 * lookup the origin inodes of the entries to fill d_ino. 1196 */ 1197 if (ovl_type_origin(old)) { 1198 err = ovl_set_impure(new->d_parent, new_upperdir); 1199 if (err) 1200 goto out_revert_creds; 1201 } 1202 if (!overwrite && ovl_type_origin(new)) { 1203 err = ovl_set_impure(old->d_parent, old_upperdir); 1204 if (err) 1205 goto out_revert_creds; 1206 } 1207 } 1208 1209 trap = lock_rename(new_upperdir, old_upperdir); 1210 if (IS_ERR(trap)) { 1211 err = PTR_ERR(trap); 1212 goto out_revert_creds; 1213 } 1214 1215 olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir, 1216 old->d_name.len); 1217 err = PTR_ERR(olddentry); 1218 if (IS_ERR(olddentry)) 1219 goto out_unlock; 1220 1221 err = -ESTALE; 1222 if (!ovl_matches_upper(old, olddentry)) 1223 goto out_dput_old; 1224 1225 newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir, 1226 new->d_name.len); 1227 err = PTR_ERR(newdentry); 1228 if (IS_ERR(newdentry)) 1229 goto out_dput_old; 1230 1231 old_opaque = ovl_dentry_is_opaque(old); 1232 new_opaque = ovl_dentry_is_opaque(new); 1233 1234 err = -ESTALE; 1235 if (d_inode(new) && ovl_dentry_upper(new)) { 1236 if (opaquedir) { 1237 if (newdentry != opaquedir) 1238 goto out_dput; 1239 } else { 1240 if (!ovl_matches_upper(new, newdentry)) 1241 goto out_dput; 1242 } 1243 } else { 1244 if (!d_is_negative(newdentry)) { 1245 if (!new_opaque || !ovl_upper_is_whiteout(ofs, newdentry)) 1246 goto out_dput; 1247 } else { 1248 if (flags & RENAME_EXCHANGE) 1249 goto out_dput; 1250 } 1251 } 1252 1253 if (olddentry == trap) 1254 goto out_dput; 1255 if (newdentry == trap) 1256 goto out_dput; 1257 1258 if (olddentry->d_inode == newdentry->d_inode) 1259 goto out_dput; 1260 1261 err = 0; 1262 if (ovl_type_merge_or_lower(old)) 1263 err = ovl_set_redirect(old, samedir); 1264 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent)) 1265 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV); 1266 if (err) 1267 goto out_dput; 1268 1269 if (!overwrite && ovl_type_merge_or_lower(new)) 1270 err = ovl_set_redirect(new, samedir); 1271 else if (!overwrite && new_is_dir && !new_opaque && 1272 ovl_type_merge(old->d_parent)) 1273 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV); 1274 if (err) 1275 goto out_dput; 1276 1277 err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry, 1278 new_upperdir->d_inode, newdentry, flags); 1279 if (err) 1280 goto out_dput; 1281 1282 if (cleanup_whiteout) 1283 ovl_cleanup(ofs, old_upperdir->d_inode, newdentry); 1284 1285 if (overwrite && d_inode(new)) { 1286 if (new_is_dir) 1287 clear_nlink(d_inode(new)); 1288 else 1289 ovl_drop_nlink(new); 1290 } 1291 1292 ovl_dir_modified(old->d_parent, ovl_type_origin(old) || 1293 (!overwrite && ovl_type_origin(new))); 1294 ovl_dir_modified(new->d_parent, ovl_type_origin(old) || 1295 (d_inode(new) && ovl_type_origin(new))); 1296 1297 /* copy ctime: */ 1298 ovl_copyattr(d_inode(old)); 1299 if (d_inode(new) && ovl_dentry_upper(new)) 1300 ovl_copyattr(d_inode(new)); 1301 1302 out_dput: 1303 dput(newdentry); 1304 out_dput_old: 1305 dput(olddentry); 1306 out_unlock: 1307 unlock_rename(new_upperdir, old_upperdir); 1308 out_revert_creds: 1309 ovl_revert_creds(old_cred); 1310 if (update_nlink) 1311 ovl_nlink_end(new); 1312 else 1313 ovl_drop_write(old); 1314 out: 1315 dput(opaquedir); 1316 ovl_cache_free(&list); 1317 return err; 1318 } 1319 1320 static int ovl_create_tmpfile(struct file *file, struct dentry *dentry, 1321 struct inode *inode, umode_t mode) 1322 { 1323 const struct cred *old_cred, *new_cred = NULL; 1324 struct path realparentpath; 1325 struct file *realfile; 1326 struct ovl_file *of; 1327 struct dentry *newdentry; 1328 /* It's okay to set O_NOATIME, since the owner will be current fsuid */ 1329 int flags = file->f_flags | OVL_OPEN_FLAGS; 1330 int err; 1331 1332 old_cred = ovl_override_creds(dentry->d_sb); 1333 new_cred = ovl_setup_cred_for_create(dentry, inode, mode, old_cred); 1334 err = PTR_ERR(new_cred); 1335 if (IS_ERR(new_cred)) { 1336 new_cred = NULL; 1337 goto out_revert_creds; 1338 } 1339 1340 ovl_path_upper(dentry->d_parent, &realparentpath); 1341 realfile = backing_tmpfile_open(&file->f_path, flags, &realparentpath, 1342 mode, current_cred()); 1343 err = PTR_ERR_OR_ZERO(realfile); 1344 pr_debug("tmpfile/open(%pd2, 0%o) = %i\n", realparentpath.dentry, mode, err); 1345 if (err) 1346 goto out_revert_creds; 1347 1348 of = ovl_file_alloc(realfile); 1349 if (!of) { 1350 fput(realfile); 1351 err = -ENOMEM; 1352 goto out_revert_creds; 1353 } 1354 1355 /* ovl_instantiate() consumes the newdentry reference on success */ 1356 newdentry = dget(realfile->f_path.dentry); 1357 err = ovl_instantiate(dentry, inode, newdentry, false, file); 1358 if (!err) { 1359 file->private_data = of; 1360 } else { 1361 dput(newdentry); 1362 ovl_file_free(of); 1363 } 1364 out_revert_creds: 1365 ovl_revert_creds(old_cred); 1366 put_cred(new_cred); 1367 return err; 1368 } 1369 1370 static int ovl_dummy_open(struct inode *inode, struct file *file) 1371 { 1372 return 0; 1373 } 1374 1375 static int ovl_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 1376 struct file *file, umode_t mode) 1377 { 1378 int err; 1379 struct dentry *dentry = file->f_path.dentry; 1380 struct inode *inode; 1381 1382 if (!OVL_FS(dentry->d_sb)->tmpfile) 1383 return -EOPNOTSUPP; 1384 1385 err = ovl_copy_up(dentry->d_parent); 1386 if (err) 1387 return err; 1388 1389 err = ovl_want_write(dentry); 1390 if (err) 1391 return err; 1392 1393 err = -ENOMEM; 1394 inode = ovl_new_inode(dentry->d_sb, mode, 0); 1395 if (!inode) 1396 goto drop_write; 1397 1398 inode_init_owner(&nop_mnt_idmap, inode, dir, mode); 1399 err = ovl_create_tmpfile(file, dentry, inode, inode->i_mode); 1400 if (err) 1401 goto put_inode; 1402 1403 /* 1404 * Check if the preallocated inode was actually used. Having something 1405 * else assigned to the dentry shouldn't happen as that would indicate 1406 * that the backing tmpfile "leaked" out of overlayfs. 1407 */ 1408 err = -EIO; 1409 if (WARN_ON(inode != d_inode(dentry))) 1410 goto put_realfile; 1411 1412 /* inode reference was transferred to dentry */ 1413 inode = NULL; 1414 err = finish_open(file, dentry, ovl_dummy_open); 1415 put_realfile: 1416 /* Without FMODE_OPENED ->release() won't be called on @file */ 1417 if (!(file->f_mode & FMODE_OPENED)) 1418 ovl_file_free(file->private_data); 1419 put_inode: 1420 iput(inode); 1421 drop_write: 1422 ovl_drop_write(dentry); 1423 return err; 1424 } 1425 1426 const struct inode_operations ovl_dir_inode_operations = { 1427 .lookup = ovl_lookup, 1428 .mkdir = ovl_mkdir, 1429 .symlink = ovl_symlink, 1430 .unlink = ovl_unlink, 1431 .rmdir = ovl_rmdir, 1432 .rename = ovl_rename, 1433 .link = ovl_link, 1434 .setattr = ovl_setattr, 1435 .create = ovl_create, 1436 .mknod = ovl_mknod, 1437 .permission = ovl_permission, 1438 .getattr = ovl_getattr, 1439 .listxattr = ovl_listxattr, 1440 .get_inode_acl = ovl_get_inode_acl, 1441 .get_acl = ovl_get_acl, 1442 .set_acl = ovl_set_acl, 1443 .update_time = ovl_update_time, 1444 .fileattr_get = ovl_fileattr_get, 1445 .fileattr_set = ovl_fileattr_set, 1446 .tmpfile = ovl_tmpfile, 1447 }; 1448