at v5.14 978 lines 24 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (C) 2011 Novell Inc. 4 * Copyright (C) 2016 Red Hat, Inc. 5 */ 6 7#include <linux/fs.h> 8#include <linux/mount.h> 9#include <linux/slab.h> 10#include <linux/cred.h> 11#include <linux/xattr.h> 12#include <linux/exportfs.h> 13#include <linux/uuid.h> 14#include <linux/namei.h> 15#include <linux/ratelimit.h> 16#include "overlayfs.h" 17 18int ovl_want_write(struct dentry *dentry) 19{ 20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 21 return mnt_want_write(ovl_upper_mnt(ofs)); 22} 23 24void ovl_drop_write(struct dentry *dentry) 25{ 26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 27 mnt_drop_write(ovl_upper_mnt(ofs)); 28} 29 30struct dentry *ovl_workdir(struct dentry *dentry) 31{ 32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 33 return ofs->workdir; 34} 35 36const struct cred *ovl_override_creds(struct super_block *sb) 37{ 38 struct ovl_fs *ofs = sb->s_fs_info; 39 40 return override_creds(ofs->creator_cred); 41} 42 43/* 44 * Check if underlying fs supports file handles and try to determine encoding 45 * type, in order to deduce maximum inode number used by fs. 46 * 47 * Return 0 if file handles are not supported. 48 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. 49 * Return -1 if fs uses a non default encoding with unknown inode size. 50 */ 51int ovl_can_decode_fh(struct super_block *sb) 52{ 53 if (!capable(CAP_DAC_READ_SEARCH)) 54 return 0; 55 56 if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry) 57 return 0; 58 59 return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; 60} 61 62struct dentry *ovl_indexdir(struct super_block *sb) 63{ 64 struct ovl_fs *ofs = sb->s_fs_info; 65 66 return ofs->indexdir; 67} 68 69/* Index all files on copy up. For now only enabled for NFS export */ 70bool ovl_index_all(struct super_block *sb) 71{ 72 struct ovl_fs *ofs = sb->s_fs_info; 73 74 return ofs->config.nfs_export && ofs->config.index; 75} 76 77/* Verify lower origin on lookup. For now only enabled for NFS export */ 78bool ovl_verify_lower(struct super_block *sb) 79{ 80 struct ovl_fs *ofs = sb->s_fs_info; 81 82 return ofs->config.nfs_export && ofs->config.index; 83} 84 85struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 86{ 87 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 88 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 89 90 if (oe) 91 oe->numlower = numlower; 92 93 return oe; 94} 95 96bool ovl_dentry_remote(struct dentry *dentry) 97{ 98 return dentry->d_flags & 99 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); 100} 101 102void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry, 103 unsigned int mask) 104{ 105 struct ovl_entry *oe = OVL_E(dentry); 106 unsigned int i, flags = 0; 107 108 if (upperdentry) 109 flags |= upperdentry->d_flags; 110 for (i = 0; i < oe->numlower; i++) 111 flags |= oe->lowerstack[i].dentry->d_flags; 112 113 spin_lock(&dentry->d_lock); 114 dentry->d_flags &= ~mask; 115 dentry->d_flags |= flags & mask; 116 spin_unlock(&dentry->d_lock); 117} 118 119bool ovl_dentry_weird(struct dentry *dentry) 120{ 121 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 122 DCACHE_MANAGE_TRANSIT | 123 DCACHE_OP_HASH | 124 DCACHE_OP_COMPARE); 125} 126 127enum ovl_path_type ovl_path_type(struct dentry *dentry) 128{ 129 struct ovl_entry *oe = dentry->d_fsdata; 130 enum ovl_path_type type = 0; 131 132 if (ovl_dentry_upper(dentry)) { 133 type = __OVL_PATH_UPPER; 134 135 /* 136 * Non-dir dentry can hold lower dentry of its copy up origin. 137 */ 138 if (oe->numlower) { 139 if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) 140 type |= __OVL_PATH_ORIGIN; 141 if (d_is_dir(dentry) || 142 !ovl_has_upperdata(d_inode(dentry))) 143 type |= __OVL_PATH_MERGE; 144 } 145 } else { 146 if (oe->numlower > 1) 147 type |= __OVL_PATH_MERGE; 148 } 149 return type; 150} 151 152void ovl_path_upper(struct dentry *dentry, struct path *path) 153{ 154 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 155 156 path->mnt = ovl_upper_mnt(ofs); 157 path->dentry = ovl_dentry_upper(dentry); 158} 159 160void ovl_path_lower(struct dentry *dentry, struct path *path) 161{ 162 struct ovl_entry *oe = dentry->d_fsdata; 163 164 if (oe->numlower) { 165 path->mnt = oe->lowerstack[0].layer->mnt; 166 path->dentry = oe->lowerstack[0].dentry; 167 } else { 168 *path = (struct path) { }; 169 } 170} 171 172void ovl_path_lowerdata(struct dentry *dentry, struct path *path) 173{ 174 struct ovl_entry *oe = dentry->d_fsdata; 175 176 if (oe->numlower) { 177 path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; 178 path->dentry = oe->lowerstack[oe->numlower - 1].dentry; 179 } else { 180 *path = (struct path) { }; 181 } 182} 183 184enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 185{ 186 enum ovl_path_type type = ovl_path_type(dentry); 187 188 if (!OVL_TYPE_UPPER(type)) 189 ovl_path_lower(dentry, path); 190 else 191 ovl_path_upper(dentry, path); 192 193 return type; 194} 195 196struct dentry *ovl_dentry_upper(struct dentry *dentry) 197{ 198 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); 199} 200 201struct dentry *ovl_dentry_lower(struct dentry *dentry) 202{ 203 struct ovl_entry *oe = dentry->d_fsdata; 204 205 return oe->numlower ? oe->lowerstack[0].dentry : NULL; 206} 207 208const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) 209{ 210 struct ovl_entry *oe = dentry->d_fsdata; 211 212 return oe->numlower ? oe->lowerstack[0].layer : NULL; 213} 214 215/* 216 * ovl_dentry_lower() could return either a data dentry or metacopy dentry 217 * depending on what is stored in lowerstack[0]. At times we need to find 218 * lower dentry which has data (and not metacopy dentry). This helper 219 * returns the lower data dentry. 220 */ 221struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) 222{ 223 struct ovl_entry *oe = dentry->d_fsdata; 224 225 return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; 226} 227 228struct dentry *ovl_dentry_real(struct dentry *dentry) 229{ 230 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); 231} 232 233struct dentry *ovl_i_dentry_upper(struct inode *inode) 234{ 235 return ovl_upperdentry_dereference(OVL_I(inode)); 236} 237 238struct inode *ovl_inode_upper(struct inode *inode) 239{ 240 struct dentry *upperdentry = ovl_i_dentry_upper(inode); 241 242 return upperdentry ? d_inode(upperdentry) : NULL; 243} 244 245struct inode *ovl_inode_lower(struct inode *inode) 246{ 247 return OVL_I(inode)->lower; 248} 249 250struct inode *ovl_inode_real(struct inode *inode) 251{ 252 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); 253} 254 255/* Return inode which contains lower data. Do not return metacopy */ 256struct inode *ovl_inode_lowerdata(struct inode *inode) 257{ 258 if (WARN_ON(!S_ISREG(inode->i_mode))) 259 return NULL; 260 261 return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); 262} 263 264/* Return real inode which contains data. Does not return metacopy inode */ 265struct inode *ovl_inode_realdata(struct inode *inode) 266{ 267 struct inode *upperinode; 268 269 upperinode = ovl_inode_upper(inode); 270 if (upperinode && ovl_has_upperdata(inode)) 271 return upperinode; 272 273 return ovl_inode_lowerdata(inode); 274} 275 276struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) 277{ 278 return OVL_I(inode)->cache; 279} 280 281void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) 282{ 283 OVL_I(inode)->cache = cache; 284} 285 286void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) 287{ 288 set_bit(flag, &OVL_E(dentry)->flags); 289} 290 291void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) 292{ 293 clear_bit(flag, &OVL_E(dentry)->flags); 294} 295 296bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) 297{ 298 return test_bit(flag, &OVL_E(dentry)->flags); 299} 300 301bool ovl_dentry_is_opaque(struct dentry *dentry) 302{ 303 return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); 304} 305 306bool ovl_dentry_is_whiteout(struct dentry *dentry) 307{ 308 return !dentry->d_inode && ovl_dentry_is_opaque(dentry); 309} 310 311void ovl_dentry_set_opaque(struct dentry *dentry) 312{ 313 ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); 314} 315 316/* 317 * For hard links and decoded file handles, it's possible for ovl_dentry_upper() 318 * to return positive, while there's no actual upper alias for the inode. 319 * Copy up code needs to know about the existence of the upper alias, so it 320 * can't use ovl_dentry_upper(). 321 */ 322bool ovl_dentry_has_upper_alias(struct dentry *dentry) 323{ 324 return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); 325} 326 327void ovl_dentry_set_upper_alias(struct dentry *dentry) 328{ 329 ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); 330} 331 332static bool ovl_should_check_upperdata(struct inode *inode) 333{ 334 if (!S_ISREG(inode->i_mode)) 335 return false; 336 337 if (!ovl_inode_lower(inode)) 338 return false; 339 340 return true; 341} 342 343bool ovl_has_upperdata(struct inode *inode) 344{ 345 if (!ovl_should_check_upperdata(inode)) 346 return true; 347 348 if (!ovl_test_flag(OVL_UPPERDATA, inode)) 349 return false; 350 /* 351 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of 352 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure 353 * if setting of OVL_UPPERDATA is visible, then effects of writes 354 * before that are visible too. 355 */ 356 smp_rmb(); 357 return true; 358} 359 360void ovl_set_upperdata(struct inode *inode) 361{ 362 /* 363 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure 364 * if OVL_UPPERDATA flag is visible, then effects of write operations 365 * before it are visible as well. 366 */ 367 smp_wmb(); 368 ovl_set_flag(OVL_UPPERDATA, inode); 369} 370 371/* Caller should hold ovl_inode->lock */ 372bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) 373{ 374 if (!ovl_open_flags_need_copy_up(flags)) 375 return false; 376 377 return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); 378} 379 380bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) 381{ 382 if (!ovl_open_flags_need_copy_up(flags)) 383 return false; 384 385 return !ovl_has_upperdata(d_inode(dentry)); 386} 387 388bool ovl_redirect_dir(struct super_block *sb) 389{ 390 struct ovl_fs *ofs = sb->s_fs_info; 391 392 return ofs->config.redirect_dir && !ofs->noxattr; 393} 394 395const char *ovl_dentry_get_redirect(struct dentry *dentry) 396{ 397 return OVL_I(d_inode(dentry))->redirect; 398} 399 400void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) 401{ 402 struct ovl_inode *oi = OVL_I(d_inode(dentry)); 403 404 kfree(oi->redirect); 405 oi->redirect = redirect; 406} 407 408void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) 409{ 410 struct inode *upperinode = d_inode(upperdentry); 411 412 WARN_ON(OVL_I(inode)->__upperdentry); 413 414 /* 415 * Make sure upperdentry is consistent before making it visible 416 */ 417 smp_wmb(); 418 OVL_I(inode)->__upperdentry = upperdentry; 419 if (inode_unhashed(inode)) { 420 inode->i_private = upperinode; 421 __insert_inode_hash(inode, (unsigned long) upperinode); 422 } 423} 424 425static void ovl_dir_version_inc(struct dentry *dentry, bool impurity) 426{ 427 struct inode *inode = d_inode(dentry); 428 429 WARN_ON(!inode_is_locked(inode)); 430 WARN_ON(!d_is_dir(dentry)); 431 /* 432 * Version is used by readdir code to keep cache consistent. 433 * For merge dirs (or dirs with origin) all changes need to be noted. 434 * For non-merge dirs, cache contains only impure entries (i.e. ones 435 * which have been copied up and have origins), so only need to note 436 * changes to impure entries. 437 */ 438 if (!ovl_dir_is_real(dentry) || impurity) 439 OVL_I(inode)->version++; 440} 441 442void ovl_dir_modified(struct dentry *dentry, bool impurity) 443{ 444 /* Copy mtime/ctime */ 445 ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); 446 447 ovl_dir_version_inc(dentry, impurity); 448} 449 450u64 ovl_dentry_version_get(struct dentry *dentry) 451{ 452 struct inode *inode = d_inode(dentry); 453 454 WARN_ON(!inode_is_locked(inode)); 455 return OVL_I(inode)->version; 456} 457 458bool ovl_is_whiteout(struct dentry *dentry) 459{ 460 struct inode *inode = dentry->d_inode; 461 462 return inode && IS_WHITEOUT(inode); 463} 464 465struct file *ovl_path_open(struct path *path, int flags) 466{ 467 struct inode *inode = d_inode(path->dentry); 468 int err, acc_mode; 469 470 if (flags & ~(O_ACCMODE | O_LARGEFILE)) 471 BUG(); 472 473 switch (flags & O_ACCMODE) { 474 case O_RDONLY: 475 acc_mode = MAY_READ; 476 break; 477 case O_WRONLY: 478 acc_mode = MAY_WRITE; 479 break; 480 default: 481 BUG(); 482 } 483 484 err = inode_permission(&init_user_ns, inode, acc_mode | MAY_OPEN); 485 if (err) 486 return ERR_PTR(err); 487 488 /* O_NOATIME is an optimization, don't fail if not permitted */ 489 if (inode_owner_or_capable(&init_user_ns, inode)) 490 flags |= O_NOATIME; 491 492 return dentry_open(path, flags, current_cred()); 493} 494 495/* Caller should hold ovl_inode->lock */ 496static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) 497{ 498 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 499 500 if (ovl_dentry_upper(dentry) && 501 (ovl_dentry_has_upper_alias(dentry) || disconnected) && 502 !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) 503 return true; 504 505 return false; 506} 507 508bool ovl_already_copied_up(struct dentry *dentry, int flags) 509{ 510 bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; 511 512 /* 513 * Check if copy-up has happened as well as for upper alias (in 514 * case of hard links) is there. 515 * 516 * Both checks are lockless: 517 * - false negatives: will recheck under oi->lock 518 * - false positives: 519 * + ovl_dentry_upper() uses memory barriers to ensure the 520 * upper dentry is up-to-date 521 * + ovl_dentry_has_upper_alias() relies on locking of 522 * upper parent i_rwsem to prevent reordering copy-up 523 * with rename. 524 */ 525 if (ovl_dentry_upper(dentry) && 526 (ovl_dentry_has_upper_alias(dentry) || disconnected) && 527 !ovl_dentry_needs_data_copy_up(dentry, flags)) 528 return true; 529 530 return false; 531} 532 533int ovl_copy_up_start(struct dentry *dentry, int flags) 534{ 535 struct inode *inode = d_inode(dentry); 536 int err; 537 538 err = ovl_inode_lock_interruptible(inode); 539 if (!err && ovl_already_copied_up_locked(dentry, flags)) { 540 err = 1; /* Already copied up */ 541 ovl_inode_unlock(inode); 542 } 543 544 return err; 545} 546 547void ovl_copy_up_end(struct dentry *dentry) 548{ 549 ovl_inode_unlock(d_inode(dentry)); 550} 551 552bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry) 553{ 554 int res; 555 556 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0); 557 558 /* Zero size value means "copied up but origin unknown" */ 559 if (res >= 0) 560 return true; 561 562 return false; 563} 564 565bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry, 566 enum ovl_xattr ox) 567{ 568 int res; 569 char val; 570 571 if (!d_is_dir(dentry)) 572 return false; 573 574 res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1); 575 if (res == 1 && val == 'y') 576 return true; 577 578 return false; 579} 580 581#define OVL_XATTR_OPAQUE_POSTFIX "opaque" 582#define OVL_XATTR_REDIRECT_POSTFIX "redirect" 583#define OVL_XATTR_ORIGIN_POSTFIX "origin" 584#define OVL_XATTR_IMPURE_POSTFIX "impure" 585#define OVL_XATTR_NLINK_POSTFIX "nlink" 586#define OVL_XATTR_UPPER_POSTFIX "upper" 587#define OVL_XATTR_METACOPY_POSTFIX "metacopy" 588 589#define OVL_XATTR_TAB_ENTRY(x) \ 590 [x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \ 591 [true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX } 592 593const char *const ovl_xattr_table[][2] = { 594 OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE), 595 OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT), 596 OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN), 597 OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE), 598 OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK), 599 OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER), 600 OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY), 601}; 602 603int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, 604 enum ovl_xattr ox, const void *value, size_t size, 605 int xerr) 606{ 607 int err; 608 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 609 610 if (ofs->noxattr) 611 return xerr; 612 613 err = ovl_do_setxattr(ofs, upperdentry, ox, value, size); 614 615 if (err == -EOPNOTSUPP) { 616 pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox)); 617 ofs->noxattr = true; 618 return xerr; 619 } 620 621 return err; 622} 623 624int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) 625{ 626 int err; 627 628 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) 629 return 0; 630 631 /* 632 * Do not fail when upper doesn't support xattrs. 633 * Upper inodes won't have origin nor redirect xattr anyway. 634 */ 635 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, 636 "y", 1, 0); 637 if (!err) 638 ovl_set_flag(OVL_IMPURE, d_inode(dentry)); 639 640 return err; 641} 642 643/** 644 * Caller must hold a reference to inode to prevent it from being freed while 645 * it is marked inuse. 646 */ 647bool ovl_inuse_trylock(struct dentry *dentry) 648{ 649 struct inode *inode = d_inode(dentry); 650 bool locked = false; 651 652 spin_lock(&inode->i_lock); 653 if (!(inode->i_state & I_OVL_INUSE)) { 654 inode->i_state |= I_OVL_INUSE; 655 locked = true; 656 } 657 spin_unlock(&inode->i_lock); 658 659 return locked; 660} 661 662void ovl_inuse_unlock(struct dentry *dentry) 663{ 664 if (dentry) { 665 struct inode *inode = d_inode(dentry); 666 667 spin_lock(&inode->i_lock); 668 WARN_ON(!(inode->i_state & I_OVL_INUSE)); 669 inode->i_state &= ~I_OVL_INUSE; 670 spin_unlock(&inode->i_lock); 671 } 672} 673 674bool ovl_is_inuse(struct dentry *dentry) 675{ 676 struct inode *inode = d_inode(dentry); 677 bool inuse; 678 679 spin_lock(&inode->i_lock); 680 inuse = (inode->i_state & I_OVL_INUSE); 681 spin_unlock(&inode->i_lock); 682 683 return inuse; 684} 685 686/* 687 * Does this overlay dentry need to be indexed on copy up? 688 */ 689bool ovl_need_index(struct dentry *dentry) 690{ 691 struct dentry *lower = ovl_dentry_lower(dentry); 692 693 if (!lower || !ovl_indexdir(dentry->d_sb)) 694 return false; 695 696 /* Index all files for NFS export and consistency verification */ 697 if (ovl_index_all(dentry->d_sb)) 698 return true; 699 700 /* Index only lower hardlinks on copy up */ 701 if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) 702 return true; 703 704 return false; 705} 706 707/* Caller must hold OVL_I(inode)->lock */ 708static void ovl_cleanup_index(struct dentry *dentry) 709{ 710 struct ovl_fs *ofs = OVL_FS(dentry->d_sb); 711 struct dentry *indexdir = ovl_indexdir(dentry->d_sb); 712 struct inode *dir = indexdir->d_inode; 713 struct dentry *lowerdentry = ovl_dentry_lower(dentry); 714 struct dentry *upperdentry = ovl_dentry_upper(dentry); 715 struct dentry *index = NULL; 716 struct inode *inode; 717 struct qstr name = { }; 718 int err; 719 720 err = ovl_get_index_name(ofs, lowerdentry, &name); 721 if (err) 722 goto fail; 723 724 inode = d_inode(upperdentry); 725 if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { 726 pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", 727 upperdentry, inode->i_ino, inode->i_nlink); 728 /* 729 * We either have a bug with persistent union nlink or a lower 730 * hardlink was added while overlay is mounted. Adding a lower 731 * hardlink and then unlinking all overlay hardlinks would drop 732 * overlay nlink to zero before all upper inodes are unlinked. 733 * As a safety measure, when that situation is detected, set 734 * the overlay nlink to the index inode nlink minus one for the 735 * index entry itself. 736 */ 737 set_nlink(d_inode(dentry), inode->i_nlink - 1); 738 ovl_set_nlink_upper(dentry); 739 goto out; 740 } 741 742 inode_lock_nested(dir, I_MUTEX_PARENT); 743 index = lookup_one_len(name.name, indexdir, name.len); 744 err = PTR_ERR(index); 745 if (IS_ERR(index)) { 746 index = NULL; 747 } else if (ovl_index_all(dentry->d_sb)) { 748 /* Whiteout orphan index to block future open by handle */ 749 err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), 750 dir, index); 751 } else { 752 /* Cleanup orphan index entries */ 753 err = ovl_cleanup(dir, index); 754 } 755 756 inode_unlock(dir); 757 if (err) 758 goto fail; 759 760out: 761 kfree(name.name); 762 dput(index); 763 return; 764 765fail: 766 pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); 767 goto out; 768} 769 770/* 771 * Operations that change overlay inode and upper inode nlink need to be 772 * synchronized with copy up for persistent nlink accounting. 773 */ 774int ovl_nlink_start(struct dentry *dentry) 775{ 776 struct inode *inode = d_inode(dentry); 777 const struct cred *old_cred; 778 int err; 779 780 if (WARN_ON(!inode)) 781 return -ENOENT; 782 783 /* 784 * With inodes index is enabled, we store the union overlay nlink 785 * in an xattr on the index inode. When whiting out an indexed lower, 786 * we need to decrement the overlay persistent nlink, but before the 787 * first copy up, we have no upper index inode to store the xattr. 788 * 789 * As a workaround, before whiteout/rename over an indexed lower, 790 * copy up to create the upper index. Creating the upper index will 791 * initialize the overlay nlink, so it could be dropped if unlink 792 * or rename succeeds. 793 * 794 * TODO: implement metadata only index copy up when called with 795 * ovl_copy_up_flags(dentry, O_PATH). 796 */ 797 if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { 798 err = ovl_copy_up(dentry); 799 if (err) 800 return err; 801 } 802 803 err = ovl_inode_lock_interruptible(inode); 804 if (err) 805 return err; 806 807 if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) 808 goto out; 809 810 old_cred = ovl_override_creds(dentry->d_sb); 811 /* 812 * The overlay inode nlink should be incremented/decremented IFF the 813 * upper operation succeeds, along with nlink change of upper inode. 814 * Therefore, before link/unlink/rename, we store the union nlink 815 * value relative to the upper inode nlink in an upper inode xattr. 816 */ 817 err = ovl_set_nlink_upper(dentry); 818 revert_creds(old_cred); 819 820out: 821 if (err) 822 ovl_inode_unlock(inode); 823 824 return err; 825} 826 827void ovl_nlink_end(struct dentry *dentry) 828{ 829 struct inode *inode = d_inode(dentry); 830 831 if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { 832 const struct cred *old_cred; 833 834 old_cred = ovl_override_creds(dentry->d_sb); 835 ovl_cleanup_index(dentry); 836 revert_creds(old_cred); 837 } 838 839 ovl_inode_unlock(inode); 840} 841 842int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) 843{ 844 /* Workdir should not be the same as upperdir */ 845 if (workdir == upperdir) 846 goto err; 847 848 /* Workdir should not be subdir of upperdir and vice versa */ 849 if (lock_rename(workdir, upperdir) != NULL) 850 goto err_unlock; 851 852 return 0; 853 854err_unlock: 855 unlock_rename(workdir, upperdir); 856err: 857 pr_err("failed to lock workdir+upperdir\n"); 858 return -EIO; 859} 860 861/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ 862int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry) 863{ 864 int res; 865 866 /* Only regular files can have metacopy xattr */ 867 if (!S_ISREG(d_inode(dentry)->i_mode)) 868 return 0; 869 870 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0); 871 if (res < 0) { 872 if (res == -ENODATA || res == -EOPNOTSUPP) 873 return 0; 874 /* 875 * getxattr on user.* may fail with EACCES in case there's no 876 * read permission on the inode. Not much we can do, other than 877 * tell the caller that this is not a metacopy inode. 878 */ 879 if (ofs->config.userxattr && res == -EACCES) 880 return 0; 881 goto out; 882 } 883 884 return 1; 885out: 886 pr_warn_ratelimited("failed to get metacopy (%i)\n", res); 887 return res; 888} 889 890bool ovl_is_metacopy_dentry(struct dentry *dentry) 891{ 892 struct ovl_entry *oe = dentry->d_fsdata; 893 894 if (!d_is_reg(dentry)) 895 return false; 896 897 if (ovl_dentry_upper(dentry)) { 898 if (!ovl_has_upperdata(d_inode(dentry))) 899 return true; 900 return false; 901 } 902 903 return (oe->numlower > 1); 904} 905 906char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry, 907 int padding) 908{ 909 int res; 910 char *s, *next, *buf = NULL; 911 912 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0); 913 if (res == -ENODATA || res == -EOPNOTSUPP) 914 return NULL; 915 if (res < 0) 916 goto fail; 917 if (res == 0) 918 goto invalid; 919 920 buf = kzalloc(res + padding + 1, GFP_KERNEL); 921 if (!buf) 922 return ERR_PTR(-ENOMEM); 923 924 res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res); 925 if (res < 0) 926 goto fail; 927 if (res == 0) 928 goto invalid; 929 930 if (buf[0] == '/') { 931 for (s = buf; *s++ == '/'; s = next) { 932 next = strchrnul(s, '/'); 933 if (s == next) 934 goto invalid; 935 } 936 } else { 937 if (strchr(buf, '/') != NULL) 938 goto invalid; 939 } 940 941 return buf; 942invalid: 943 pr_warn_ratelimited("invalid redirect (%s)\n", buf); 944 res = -EINVAL; 945 goto err_free; 946fail: 947 pr_warn_ratelimited("failed to get redirect (%i)\n", res); 948err_free: 949 kfree(buf); 950 return ERR_PTR(res); 951} 952 953/* 954 * ovl_sync_status() - Check fs sync status for volatile mounts 955 * 956 * Returns 1 if this is not a volatile mount and a real sync is required. 957 * 958 * Returns 0 if syncing can be skipped because mount is volatile, and no errors 959 * have occurred on the upperdir since the mount. 960 * 961 * Returns -errno if it is a volatile mount, and the error that occurred since 962 * the last mount. If the error code changes, it'll return the latest error 963 * code. 964 */ 965 966int ovl_sync_status(struct ovl_fs *ofs) 967{ 968 struct vfsmount *mnt; 969 970 if (ovl_should_sync(ofs)) 971 return 1; 972 973 mnt = ovl_upper_mnt(ofs); 974 if (!mnt) 975 return 0; 976 977 return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq); 978}