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