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