Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs

Pull overlayfs updates from Miklos Szeredi:

- Report constant st_ino values across copy-up even if underlying
layers are on different filesystems, but using different st_dev
values for each layer.

Ideally we'd report the same st_dev across the overlay, and it's
possible to do for filesystems that use only 32bits for st_ino by
unifying the inum space. It would be nice if it wasn't a choice of 32
or 64, rather filesystems could report their current maximum (that
could change on resize, so it wouldn't be set in stone).

- miscellaneus fixes and a cleanup of ovl_fill_super(), that was long
overdue.

- created a path_put_init() helper that clears out the pointers after
putting the ref.

I think this could be useful elsewhere, so added it to <linux/path.h>

* 'overlayfs-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs: (30 commits)
ovl: remove unneeded arg from ovl_verify_origin()
ovl: Put upperdentry if ovl_check_origin() fails
ovl: rename ufs to ofs
ovl: clean up getting lower layers
ovl: clean up workdir creation
ovl: clean up getting upper layer
ovl: move ovl_get_workdir() and ovl_get_lower_layers()
ovl: reduce the number of arguments for ovl_workdir_create()
ovl: change order of setup in ovl_fill_super()
ovl: factor out ovl_free_fs() helper
ovl: grab reference to workbasedir early
ovl: split out ovl_get_indexdir() from ovl_fill_super()
ovl: split out ovl_get_lower_layers() from ovl_fill_super()
ovl: split out ovl_get_workdir() from ovl_fill_super()
ovl: split out ovl_get_upper() from ovl_fill_super()
ovl: split out ovl_get_lowerstack() from ovl_fill_super()
ovl: split out ovl_get_workpath() from ovl_fill_super()
ovl: split out ovl_get_upperpath() from ovl_fill_super()
ovl: use path_put_init() in error paths for ovl_fill_super()
vfs: add path_put_init()
...

+592 -392
+6 -2
fs/overlayfs/copy_up.c
··· 22 22 #include <linux/ratelimit.h> 23 23 #include <linux/exportfs.h> 24 24 #include "overlayfs.h" 25 - #include "ovl_entry.h" 26 25 27 26 #define OVL_COPY_UP_CHUNK_SIZE (1 << 20) 28 27 ··· 485 486 static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c) 486 487 { 487 488 struct inode *udir = c->destdir->d_inode; 489 + struct inode *inode; 488 490 struct dentry *newdentry = NULL; 489 491 struct dentry *temp = NULL; 490 492 int err; ··· 508 508 if (err) 509 509 goto out_cleanup; 510 510 511 - ovl_inode_update(d_inode(c->dentry), newdentry); 511 + inode = d_inode(c->dentry); 512 + ovl_inode_update(inode, newdentry); 513 + if (S_ISDIR(inode->i_mode)) 514 + ovl_set_flag(OVL_WHITEOUTS, inode); 515 + 512 516 out: 513 517 dput(temp); 514 518 return err;
+16 -9
fs/overlayfs/dir.c
··· 181 181 return OVL_TYPE_ORIGIN(ovl_path_type(dentry)); 182 182 } 183 183 184 + static bool ovl_may_have_whiteouts(struct dentry *dentry) 185 + { 186 + return ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry)); 187 + } 188 + 184 189 static int ovl_create_upper(struct dentry *dentry, struct inode *inode, 185 190 struct cattr *attr, struct dentry *hardlink) 186 191 { ··· 305 300 { 306 301 int err; 307 302 struct dentry *ret = NULL; 308 - enum ovl_path_type type = ovl_path_type(dentry); 309 303 LIST_HEAD(list); 310 304 311 305 err = ovl_check_empty_dir(dentry, &list); ··· 317 313 * When removing an empty opaque directory, then it makes no sense to 318 314 * replace it with an exact replica of itself. 319 315 * 320 - * If no upperdentry then skip clearing whiteouts. 316 + * If upperdentry has whiteouts, clear them. 321 317 * 322 318 * Can race with copy-up, since we don't hold the upperdir mutex. 323 319 * Doesn't matter, since copy-up can't create a non-empty directory 324 320 * from an empty one. 325 321 */ 326 - if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type)) 322 + if (!list_empty(&list)) 327 323 ret = ovl_clear_empty(dentry, &list); 328 324 329 325 out_free: ··· 702 698 struct dentry *opaquedir = NULL; 703 699 int err; 704 700 705 - /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */ 706 - if (is_dir && ovl_dentry_get_redirect(dentry)) { 701 + /* Redirect/origin dir can be !ovl_lower_positive && not clean */ 702 + if (is_dir && (ovl_dentry_get_redirect(dentry) || 703 + ovl_may_have_whiteouts(dentry))) { 707 704 opaquedir = ovl_check_empty_and_clear(dentry); 708 705 err = PTR_ERR(opaquedir); 709 706 if (IS_ERR(opaquedir)) ··· 951 946 952 947 old_cred = ovl_override_creds(old->d_sb); 953 948 954 - if (overwrite && new_is_dir && ovl_type_merge_or_lower(new)) { 949 + if (overwrite && new_is_dir && (ovl_type_merge_or_lower(new) || 950 + ovl_may_have_whiteouts(new))) { 955 951 opaquedir = ovl_check_empty_and_clear(new); 956 952 err = PTR_ERR(opaquedir); 957 953 if (IS_ERR(opaquedir)) { ··· 1075 1069 drop_nlink(d_inode(new)); 1076 1070 } 1077 1071 1078 - ovl_dentry_version_inc(old->d_parent, 1079 - !overwrite && ovl_type_origin(new)); 1080 - ovl_dentry_version_inc(new->d_parent, ovl_type_origin(old)); 1072 + ovl_dentry_version_inc(old->d_parent, ovl_type_origin(old) || 1073 + (!overwrite && ovl_type_origin(new))); 1074 + ovl_dentry_version_inc(new->d_parent, ovl_type_origin(old) || 1075 + (d_inode(new) && ovl_type_origin(new))); 1081 1076 1082 1077 out_dput: 1083 1078 dput(newdentry);
+50 -13
fs/overlayfs/inode.c
··· 15 15 #include <linux/ratelimit.h> 16 16 #include "overlayfs.h" 17 17 18 + 19 + static dev_t ovl_get_pseudo_dev(struct dentry *dentry) 20 + { 21 + struct ovl_entry *oe = dentry->d_fsdata; 22 + 23 + return oe->lowerstack[0].layer->pseudo_dev; 24 + } 25 + 18 26 int ovl_setattr(struct dentry *dentry, struct iattr *attr) 19 27 { 20 28 int err; ··· 74 66 struct path realpath; 75 67 const struct cred *old_cred; 76 68 bool is_dir = S_ISDIR(dentry->d_inode->i_mode); 69 + bool samefs = ovl_same_sb(dentry->d_sb); 77 70 int err; 78 71 79 72 type = ovl_path_real(dentry, &realpath); ··· 84 75 goto out; 85 76 86 77 /* 87 - * When all layers are on the same fs, all real inode number are 88 - * unique, so we use the overlay st_dev, which is friendly to du -x. 89 - * 90 - * We also use st_ino of the copy up origin, if we know it. 91 - * This guaranties constant st_dev/st_ino across copy up. 78 + * For non-dir or same fs, we use st_ino of the copy up origin, if we 79 + * know it. This guaranties constant st_dev/st_ino across copy up. 92 80 * 93 81 * If filesystem supports NFS export ops, this also guaranties 94 82 * persistent st_ino across mount cycle. 95 83 */ 96 - if (ovl_same_sb(dentry->d_sb)) { 84 + if (!is_dir || samefs) { 97 85 if (OVL_TYPE_ORIGIN(type)) { 98 86 struct kstat lowerstat; 99 87 u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0); ··· 101 95 if (err) 102 96 goto out; 103 97 104 - WARN_ON_ONCE(stat->dev != lowerstat.dev); 105 98 /* 106 99 * Lower hardlinks may be broken on copy up to different 107 100 * upper files, so we cannot use the lower origin st_ino ··· 112 107 if (is_dir || lowerstat.nlink == 1 || 113 108 ovl_test_flag(OVL_INDEX, d_inode(dentry))) 114 109 stat->ino = lowerstat.ino; 110 + 111 + if (samefs) 112 + WARN_ON_ONCE(stat->dev != lowerstat.dev); 113 + else 114 + stat->dev = ovl_get_pseudo_dev(dentry); 115 115 } 116 - stat->dev = dentry->d_sb->s_dev; 117 - } else if (is_dir) { 116 + if (samefs) { 117 + /* 118 + * When all layers are on the same fs, all real inode 119 + * number are unique, so we use the overlay st_dev, 120 + * which is friendly to du -x. 121 + */ 122 + stat->dev = dentry->d_sb->s_dev; 123 + } else if (!OVL_TYPE_UPPER(type)) { 124 + /* 125 + * For non-samefs setup, to make sure that st_dev/st_ino 126 + * pair is unique across the system, we use a unique 127 + * anonymous st_dev for lower layer inode. 128 + */ 129 + stat->dev = ovl_get_pseudo_dev(dentry); 130 + } 131 + } else { 118 132 /* 119 - * If not all layers are on the same fs the pair {real st_ino; 120 - * overlay st_dev} is not unique, so use the non persistent 121 - * overlay st_ino. 122 - * 123 133 * Always use the overlay st_dev for directories, so 'find 124 134 * -xdev' will scan the entire overlay mount and won't cross the 125 135 * overlay mount boundaries. 136 + * 137 + * If not all layers are on the same fs the pair {real st_ino; 138 + * overlay st_dev} is not unique, so use the non persistent 139 + * overlay st_ino for directories. 126 140 */ 127 141 stat->dev = dentry->d_sb->s_dev; 128 142 stat->ino = dentry->d_inode->i_ino; ··· 433 409 #ifdef CONFIG_LOCKDEP 434 410 static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; 435 411 static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; 412 + static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING]; 436 413 437 414 int depth = inode->i_sb->s_stack_depth - 1; 438 415 ··· 444 419 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); 445 420 else 446 421 lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); 422 + 423 + lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]); 447 424 #endif 448 425 } 449 426 ··· 683 656 684 657 if (upperdentry && ovl_is_impuredir(upperdentry)) 685 658 ovl_set_flag(OVL_IMPURE, inode); 659 + 660 + /* Check for non-merge dir that may have whiteouts */ 661 + if (S_ISDIR(realinode->i_mode)) { 662 + struct ovl_entry *oe = dentry->d_fsdata; 663 + 664 + if (((upperdentry && lowerdentry) || oe->numlower > 1) || 665 + ovl_check_origin_xattr(upperdentry ?: lowerdentry)) { 666 + ovl_set_flag(OVL_WHITEOUTS, inode); 667 + } 668 + } 686 669 687 670 if (inode->i_state & I_NEW) 688 671 unlock_new_inode(inode);
+34 -25
fs/overlayfs/namei.c
··· 15 15 #include <linux/mount.h> 16 16 #include <linux/exportfs.h> 17 17 #include "overlayfs.h" 18 - #include "ovl_entry.h" 19 18 20 19 struct ovl_lookup_data { 21 20 struct qstr name; ··· 285 286 286 287 287 288 static int ovl_check_origin(struct dentry *upperdentry, 288 - struct path *lowerstack, unsigned int numlower, 289 - struct path **stackp, unsigned int *ctrp) 289 + struct ovl_path *lower, unsigned int numlower, 290 + struct ovl_path **stackp, unsigned int *ctrp) 290 291 { 291 292 struct vfsmount *mnt; 292 293 struct dentry *origin = NULL; 293 294 int i; 294 295 295 - 296 296 for (i = 0; i < numlower; i++) { 297 - mnt = lowerstack[i].mnt; 297 + mnt = lower[i].layer->mnt; 298 298 origin = ovl_get_origin(upperdentry, mnt); 299 299 if (IS_ERR(origin)) 300 300 return PTR_ERR(origin); ··· 307 309 308 310 BUG_ON(*ctrp); 309 311 if (!*stackp) 310 - *stackp = kmalloc(sizeof(struct path), GFP_KERNEL); 312 + *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); 311 313 if (!*stackp) { 312 314 dput(origin); 313 315 return -ENOMEM; 314 316 } 315 - **stackp = (struct path) { .dentry = origin, .mnt = mnt }; 317 + **stackp = (struct ovl_path){.dentry = origin, .layer = lower[i].layer}; 316 318 *ctrp = 1; 317 319 318 320 return 0; ··· 348 350 * 349 351 * Return 0 on match, -ESTALE on mismatch, < 0 on error. 350 352 */ 351 - int ovl_verify_origin(struct dentry *dentry, struct vfsmount *mnt, 352 - struct dentry *origin, bool is_upper, bool set) 353 + int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, 354 + bool is_upper, bool set) 353 355 { 354 356 struct inode *inode; 355 357 struct ovl_fh *fh; ··· 382 384 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. 383 385 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. 384 386 */ 385 - int ovl_verify_index(struct dentry *index, struct path *lowerstack, 387 + int ovl_verify_index(struct dentry *index, struct ovl_path *lower, 386 388 unsigned int numlower) 387 389 { 388 390 struct ovl_fh *fh = NULL; 389 391 size_t len; 390 - struct path origin = { }; 391 - struct path *stack = &origin; 392 + struct ovl_path origin = { }; 393 + struct ovl_path *stack = &origin; 392 394 unsigned int ctr = 0; 393 395 int err; 394 396 ··· 427 429 if (err) 428 430 goto fail; 429 431 430 - err = ovl_check_origin(index, lowerstack, numlower, &stack, &ctr); 432 + err = ovl_check_origin(index, lower, numlower, &stack, &ctr); 431 433 if (!err && !ctr) 432 434 err = -ESTALE; 433 435 if (err) ··· 566 568 idx++; 567 569 } 568 570 BUG_ON(idx > oe->numlower); 569 - *path = oe->lowerstack[idx - 1]; 571 + path->dentry = oe->lowerstack[idx - 1].dentry; 572 + path->mnt = oe->lowerstack[idx - 1].layer->mnt; 570 573 571 574 return (idx < oe->numlower) ? idx + 1 : -1; 575 + } 576 + 577 + static int ovl_find_layer(struct ovl_fs *ofs, struct ovl_path *path) 578 + { 579 + int i; 580 + 581 + for (i = 0; i < ofs->numlower; i++) { 582 + if (ofs->lower_layers[i].mnt == path->layer->mnt) 583 + break; 584 + } 585 + 586 + return i; 572 587 } 573 588 574 589 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, ··· 592 581 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 593 582 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 594 583 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; 595 - struct path *stack = NULL; 584 + struct ovl_path *stack = NULL; 596 585 struct dentry *upperdir, *upperdentry = NULL; 597 586 struct dentry *index = NULL; 598 587 unsigned int ctr = 0; ··· 641 630 err = ovl_check_origin(upperdentry, roe->lowerstack, 642 631 roe->numlower, &stack, &ctr); 643 632 if (err) 644 - goto out; 633 + goto out_put_upper; 645 634 } 646 635 647 636 if (d.redirect) { ··· 657 646 658 647 if (!d.stop && poe->numlower) { 659 648 err = -ENOMEM; 660 - stack = kcalloc(ofs->numlower, sizeof(struct path), 649 + stack = kcalloc(ofs->numlower, sizeof(struct ovl_path), 661 650 GFP_KERNEL); 662 651 if (!stack) 663 652 goto out_put_upper; 664 653 } 665 654 666 655 for (i = 0; !d.stop && i < poe->numlower; i++) { 667 - struct path lowerpath = poe->lowerstack[i]; 656 + struct ovl_path lower = poe->lowerstack[i]; 668 657 669 658 d.last = i == poe->numlower - 1; 670 - err = ovl_lookup_layer(lowerpath.dentry, &d, &this); 659 + err = ovl_lookup_layer(lower.dentry, &d, &this); 671 660 if (err) 672 661 goto out_put; 673 662 ··· 675 664 continue; 676 665 677 666 stack[ctr].dentry = this; 678 - stack[ctr].mnt = lowerpath.mnt; 667 + stack[ctr].layer = lower.layer; 679 668 ctr++; 680 669 681 670 if (d.stop) ··· 685 674 poe = roe; 686 675 687 676 /* Find the current layer on the root dentry */ 688 - for (i = 0; i < poe->numlower; i++) 689 - if (poe->lowerstack[i].mnt == lowerpath.mnt) 690 - break; 691 - if (WARN_ON(i == poe->numlower)) 677 + i = ovl_find_layer(ofs, &lower); 678 + if (WARN_ON(i == ofs->numlower)) 692 679 break; 693 680 } 694 681 } ··· 709 700 goto out_put; 710 701 711 702 oe->opaque = upperopaque; 712 - memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); 703 + memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); 713 704 dentry->d_fsdata = oe; 714 705 715 706 if (upperdentry)
+9 -4
fs/overlayfs/overlayfs.h
··· 9 9 10 10 #include <linux/kernel.h> 11 11 #include <linux/uuid.h> 12 + #include "ovl_entry.h" 12 13 13 14 enum ovl_path_type { 14 15 __OVL_PATH_UPPER = (1 << 0), ··· 29 28 #define OVL_XATTR_NLINK OVL_XATTR_PREFIX "nlink" 30 29 31 30 enum ovl_flag { 31 + /* Pure upper dir that may contain non pure upper entries */ 32 32 OVL_IMPURE, 33 + /* Non-merge dir that may contain whiteout entries */ 34 + OVL_WHITEOUTS, 33 35 OVL_INDEX, 34 36 }; 35 37 ··· 227 223 struct file *ovl_path_open(struct path *path, int flags); 228 224 int ovl_copy_up_start(struct dentry *dentry); 229 225 void ovl_copy_up_end(struct dentry *dentry); 226 + bool ovl_check_origin_xattr(struct dentry *dentry); 230 227 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name); 231 228 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, 232 229 const char *name, const void *value, size_t size, ··· 249 244 250 245 251 246 /* namei.c */ 252 - int ovl_verify_origin(struct dentry *dentry, struct vfsmount *mnt, 253 - struct dentry *origin, bool is_upper, bool set); 254 - int ovl_verify_index(struct dentry *index, struct path *lowerstack, 247 + int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, 248 + bool is_upper, bool set); 249 + int ovl_verify_index(struct dentry *index, struct ovl_path *lower, 255 250 unsigned int numlower); 256 251 int ovl_get_index_name(struct dentry *origin, struct qstr *name); 257 252 int ovl_path_next(int idx, struct dentry *dentry, struct path *path); ··· 268 263 void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt, 269 264 struct dentry *dentry, int level); 270 265 int ovl_indexdir_cleanup(struct dentry *dentry, struct vfsmount *mnt, 271 - struct path *lowerstack, unsigned int numlower); 266 + struct ovl_path *lower, unsigned int numlower); 272 267 273 268 /* inode.c */ 274 269 int ovl_set_nlink_upper(struct dentry *dentry);
+12 -2
fs/overlayfs/ovl_entry.h
··· 17 17 bool index; 18 18 }; 19 19 20 + struct ovl_layer { 21 + struct vfsmount *mnt; 22 + dev_t pseudo_dev; 23 + }; 24 + 25 + struct ovl_path { 26 + struct ovl_layer *layer; 27 + struct dentry *dentry; 28 + }; 29 + 20 30 /* private information held for overlayfs's superblock */ 21 31 struct ovl_fs { 22 32 struct vfsmount *upper_mnt; 23 33 unsigned numlower; 24 - struct vfsmount **lower_mnt; 34 + struct ovl_layer *lower_layers; 25 35 /* workbasedir is the path at workdir= mount option */ 26 36 struct dentry *workbasedir; 27 37 /* workdir is the 'work' directory under workbasedir */ ··· 62 52 struct rcu_head rcu; 63 53 }; 64 54 unsigned numlower; 65 - struct path lowerstack[]; 55 + struct ovl_path lowerstack[]; 66 56 }; 67 57 68 58 struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
+42 -13
fs/overlayfs/readdir.c
··· 26 26 struct list_head l_node; 27 27 struct rb_node node; 28 28 struct ovl_cache_entry *next_maybe_whiteout; 29 + bool is_upper; 29 30 bool is_whiteout; 30 31 char name[]; 31 32 }; ··· 159 158 /* Defer setting d_ino for upper entry to ovl_iterate() */ 160 159 if (ovl_calc_d_ino(rdd, p)) 161 160 p->ino = 0; 161 + p->is_upper = rdd->is_upper; 162 162 p->is_whiteout = false; 163 163 164 164 if (d_type == DT_CHR) { ··· 318 316 return err; 319 317 } 320 318 319 + /* 320 + * Can we iterate real dir directly? 321 + * 322 + * Non-merge dir may contain whiteouts from a time it was a merge upper, before 323 + * lower dir was removed under it and possibly before it was rotated from upper 324 + * to lower layer. 325 + */ 326 + static bool ovl_dir_is_real(struct dentry *dir) 327 + { 328 + return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir)); 329 + } 330 + 321 331 static void ovl_dir_reset(struct file *file) 322 332 { 323 333 struct ovl_dir_file *od = file->private_data; 324 334 struct ovl_dir_cache *cache = od->cache; 325 335 struct dentry *dentry = file->f_path.dentry; 326 - enum ovl_path_type type = ovl_path_type(dentry); 336 + bool is_real; 327 337 328 338 if (cache && ovl_dentry_version_get(dentry) != cache->version) { 329 339 ovl_cache_put(od, dentry); 330 340 od->cache = NULL; 331 341 od->cursor = NULL; 332 342 } 333 - WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type)); 334 - if (od->is_real && OVL_TYPE_MERGE(type)) 343 + is_real = ovl_dir_is_real(dentry); 344 + if (od->is_real != is_real) { 345 + /* is_real can only become false when dir is copied up */ 346 + if (WARN_ON(is_real)) 347 + return; 335 348 od->is_real = false; 349 + } 336 350 } 337 351 338 352 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list, ··· 834 816 return PTR_ERR(realfile); 835 817 } 836 818 od->realfile = realfile; 837 - od->is_real = !OVL_TYPE_MERGE(type); 819 + od->is_real = ovl_dir_is_real(file->f_path.dentry); 838 820 od->is_upper = OVL_TYPE_UPPER(type); 839 821 file->private_data = od; 840 822 ··· 853 835 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list) 854 836 { 855 837 int err; 856 - struct ovl_cache_entry *p; 838 + struct ovl_cache_entry *p, *n; 857 839 struct rb_root root = RB_ROOT; 858 840 859 841 err = ovl_dir_read_merged(dentry, list, &root); ··· 862 844 863 845 err = 0; 864 846 865 - list_for_each_entry(p, list, l_node) { 866 - if (p->is_whiteout) 867 - continue; 847 + list_for_each_entry_safe(p, n, list, l_node) { 848 + /* 849 + * Select whiteouts in upperdir, they should 850 + * be cleared when deleting this directory. 851 + */ 852 + if (p->is_whiteout) { 853 + if (p->is_upper) 854 + continue; 855 + goto del_entry; 856 + } 868 857 869 858 if (p->name[0] == '.') { 870 859 if (p->len == 1) 871 - continue; 860 + goto del_entry; 872 861 if (p->len == 2 && p->name[1] == '.') 873 - continue; 862 + goto del_entry; 874 863 } 875 864 err = -ENOTEMPTY; 876 865 break; 866 + 867 + del_entry: 868 + list_del(&p->l_node); 869 + kfree(p); 877 870 } 878 871 879 872 return err; ··· 898 869 list_for_each_entry(p, list, l_node) { 899 870 struct dentry *dentry; 900 871 901 - if (!p->is_whiteout) 872 + if (WARN_ON(!p->is_whiteout || !p->is_upper)) 902 873 continue; 903 874 904 875 dentry = lookup_one_len(p->name, upper, p->len); ··· 1014 985 } 1015 986 1016 987 int ovl_indexdir_cleanup(struct dentry *dentry, struct vfsmount *mnt, 1017 - struct path *lowerstack, unsigned int numlower) 988 + struct ovl_path *lower, unsigned int numlower) 1018 989 { 1019 990 int err; 1020 991 struct dentry *index = NULL; ··· 1049 1020 index = NULL; 1050 1021 break; 1051 1022 } 1052 - err = ovl_verify_index(index, lowerstack, numlower); 1023 + err = ovl_verify_index(index, lower, numlower); 1053 1024 /* Cleanup stale and orphan index entries */ 1054 1025 if (err && (err == -ESTALE || err == -ENOENT)) 1055 1026 err = ovl_cleanup(dir, index);
+398 -322
fs/overlayfs/super.c
··· 18 18 #include <linux/seq_file.h> 19 19 #include <linux/posix_acl_xattr.h> 20 20 #include "overlayfs.h" 21 - #include "ovl_entry.h" 22 21 23 22 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); 24 23 MODULE_DESCRIPTION("Overlay filesystem"); ··· 38 39 MODULE_PARM_DESC(ovl_index_def, 39 40 "Default to on or off for the inodes index feature"); 40 41 42 + static void ovl_entry_stack_free(struct ovl_entry *oe) 43 + { 44 + unsigned int i; 45 + 46 + for (i = 0; i < oe->numlower; i++) 47 + dput(oe->lowerstack[i].dentry); 48 + } 49 + 41 50 static void ovl_dentry_release(struct dentry *dentry) 42 51 { 43 52 struct ovl_entry *oe = dentry->d_fsdata; 44 53 45 54 if (oe) { 46 - unsigned int i; 47 - 48 - for (i = 0; i < oe->numlower; i++) 49 - dput(oe->lowerstack[i].dentry); 55 + ovl_entry_stack_free(oe); 50 56 kfree_rcu(oe, rcu); 51 57 } 52 58 } ··· 211 207 call_rcu(&inode->i_rcu, ovl_i_callback); 212 208 } 213 209 214 - static void ovl_put_super(struct super_block *sb) 210 + static void ovl_free_fs(struct ovl_fs *ofs) 215 211 { 216 - struct ovl_fs *ufs = sb->s_fs_info; 217 212 unsigned i; 218 213 219 - dput(ufs->indexdir); 220 - dput(ufs->workdir); 221 - if (ufs->workdir_locked) 222 - ovl_inuse_unlock(ufs->workbasedir); 223 - dput(ufs->workbasedir); 224 - if (ufs->upper_mnt && ufs->upperdir_locked) 225 - ovl_inuse_unlock(ufs->upper_mnt->mnt_root); 226 - mntput(ufs->upper_mnt); 227 - for (i = 0; i < ufs->numlower; i++) 228 - mntput(ufs->lower_mnt[i]); 229 - kfree(ufs->lower_mnt); 214 + dput(ofs->indexdir); 215 + dput(ofs->workdir); 216 + if (ofs->workdir_locked) 217 + ovl_inuse_unlock(ofs->workbasedir); 218 + dput(ofs->workbasedir); 219 + if (ofs->upperdir_locked) 220 + ovl_inuse_unlock(ofs->upper_mnt->mnt_root); 221 + mntput(ofs->upper_mnt); 222 + for (i = 0; i < ofs->numlower; i++) { 223 + mntput(ofs->lower_layers[i].mnt); 224 + free_anon_bdev(ofs->lower_layers[i].pseudo_dev); 225 + } 226 + kfree(ofs->lower_layers); 230 227 231 - kfree(ufs->config.lowerdir); 232 - kfree(ufs->config.upperdir); 233 - kfree(ufs->config.workdir); 234 - put_cred(ufs->creator_cred); 235 - kfree(ufs); 228 + kfree(ofs->config.lowerdir); 229 + kfree(ofs->config.upperdir); 230 + kfree(ofs->config.workdir); 231 + if (ofs->creator_cred) 232 + put_cred(ofs->creator_cred); 233 + kfree(ofs); 234 + } 235 + 236 + static void ovl_put_super(struct super_block *sb) 237 + { 238 + struct ovl_fs *ofs = sb->s_fs_info; 239 + 240 + ovl_free_fs(ofs); 236 241 } 237 242 238 243 static int ovl_sync_fs(struct super_block *sb, int wait) 239 244 { 240 - struct ovl_fs *ufs = sb->s_fs_info; 245 + struct ovl_fs *ofs = sb->s_fs_info; 241 246 struct super_block *upper_sb; 242 247 int ret; 243 248 244 - if (!ufs->upper_mnt) 249 + if (!ofs->upper_mnt) 245 250 return 0; 246 - upper_sb = ufs->upper_mnt->mnt_sb; 251 + upper_sb = ofs->upper_mnt->mnt_sb; 247 252 if (!upper_sb->s_op->sync_fs) 248 253 return 0; 249 254 ··· 290 277 } 291 278 292 279 /* Will this overlay be forced to mount/remount ro? */ 293 - static bool ovl_force_readonly(struct ovl_fs *ufs) 280 + static bool ovl_force_readonly(struct ovl_fs *ofs) 294 281 { 295 - return (!ufs->upper_mnt || !ufs->workdir); 282 + return (!ofs->upper_mnt || !ofs->workdir); 296 283 } 297 284 298 285 /** ··· 304 291 static int ovl_show_options(struct seq_file *m, struct dentry *dentry) 305 292 { 306 293 struct super_block *sb = dentry->d_sb; 307 - struct ovl_fs *ufs = sb->s_fs_info; 294 + struct ovl_fs *ofs = sb->s_fs_info; 308 295 309 - seq_show_option(m, "lowerdir", ufs->config.lowerdir); 310 - if (ufs->config.upperdir) { 311 - seq_show_option(m, "upperdir", ufs->config.upperdir); 312 - seq_show_option(m, "workdir", ufs->config.workdir); 296 + seq_show_option(m, "lowerdir", ofs->config.lowerdir); 297 + if (ofs->config.upperdir) { 298 + seq_show_option(m, "upperdir", ofs->config.upperdir); 299 + seq_show_option(m, "workdir", ofs->config.workdir); 313 300 } 314 - if (ufs->config.default_permissions) 301 + if (ofs->config.default_permissions) 315 302 seq_puts(m, ",default_permissions"); 316 - if (ufs->config.redirect_dir != ovl_redirect_dir_def) 303 + if (ofs->config.redirect_dir != ovl_redirect_dir_def) 317 304 seq_printf(m, ",redirect_dir=%s", 318 - ufs->config.redirect_dir ? "on" : "off"); 319 - if (ufs->config.index != ovl_index_def) 305 + ofs->config.redirect_dir ? "on" : "off"); 306 + if (ofs->config.index != ovl_index_def) 320 307 seq_printf(m, ",index=%s", 321 - ufs->config.index ? "on" : "off"); 308 + ofs->config.index ? "on" : "off"); 322 309 return 0; 323 310 } 324 311 325 312 static int ovl_remount(struct super_block *sb, int *flags, char *data) 326 313 { 327 - struct ovl_fs *ufs = sb->s_fs_info; 314 + struct ovl_fs *ofs = sb->s_fs_info; 328 315 329 - if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs)) 316 + if (!(*flags & MS_RDONLY) && ovl_force_readonly(ofs)) 330 317 return -EROFS; 331 318 332 319 return 0; ··· 464 451 #define OVL_WORKDIR_NAME "work" 465 452 #define OVL_INDEXDIR_NAME "index" 466 453 467 - static struct dentry *ovl_workdir_create(struct super_block *sb, 468 - struct ovl_fs *ufs, 469 - struct dentry *dentry, 454 + static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, 470 455 const char *name, bool persist) 471 456 { 472 - struct inode *dir = dentry->d_inode; 473 - struct vfsmount *mnt = ufs->upper_mnt; 457 + struct inode *dir = ofs->workbasedir->d_inode; 458 + struct vfsmount *mnt = ofs->upper_mnt; 474 459 struct dentry *work; 475 460 int err; 476 461 bool retried = false; ··· 482 471 locked = true; 483 472 484 473 retry: 485 - work = lookup_one_len(name, dentry, strlen(name)); 474 + work = lookup_one_len(name, ofs->workbasedir, strlen(name)); 486 475 487 476 if (!IS_ERR(work)) { 488 477 struct iattr attr = { ··· 552 541 dput(work); 553 542 out_err: 554 543 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", 555 - ufs->config.workdir, name, -err); 556 - sb->s_flags |= MS_RDONLY; 544 + ofs->config.workdir, name, -err); 557 545 work = NULL; 558 546 goto out_unlock; 559 547 } ··· 595 585 return 0; 596 586 597 587 out_put: 598 - path_put(path); 588 + path_put_init(path); 599 589 out: 600 590 return err; 601 591 } ··· 613 603 if (ovl_dentry_remote(path->dentry)) { 614 604 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", 615 605 tmp); 616 - path_put(path); 606 + path_put_init(path); 617 607 err = -EINVAL; 618 608 } 619 609 kfree(tmp); ··· 665 655 return 0; 666 656 667 657 out_put: 668 - path_put(path); 658 + path_put_init(path); 669 659 out: 670 660 return err; 671 661 } ··· 836 826 NULL 837 827 }; 838 828 839 - static int ovl_fill_super(struct super_block *sb, void *data, int silent) 829 + static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath) 840 830 { 841 - struct path upperpath = { }; 842 - struct path workpath = { }; 843 - struct dentry *root_dentry; 844 - struct ovl_entry *oe; 845 - struct ovl_fs *ufs; 846 - struct path *stack = NULL; 847 - char *lowertmp; 848 - char *lower; 849 - unsigned int numlower; 850 - unsigned int stacklen = 0; 851 - unsigned int i; 852 - bool remote = false; 853 - struct cred *cred; 831 + struct vfsmount *upper_mnt; 854 832 int err; 855 833 856 - err = -ENOMEM; 857 - ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); 858 - if (!ufs) 834 + err = ovl_mount_dir(ofs->config.upperdir, upperpath); 835 + if (err) 859 836 goto out; 860 837 861 - ufs->config.redirect_dir = ovl_redirect_dir_def; 862 - ufs->config.index = ovl_index_def; 863 - err = ovl_parse_opt((char *) data, &ufs->config); 838 + /* Upper fs should not be r/o */ 839 + if (sb_rdonly(upperpath->mnt->mnt_sb)) { 840 + pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); 841 + err = -EINVAL; 842 + goto out; 843 + } 844 + 845 + err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); 864 846 if (err) 865 - goto out_free_config; 847 + goto out; 848 + 849 + err = -EBUSY; 850 + if (ovl_inuse_trylock(upperpath->dentry)) { 851 + ofs->upperdir_locked = true; 852 + } else if (ofs->config.index) { 853 + pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n"); 854 + goto out; 855 + } else { 856 + pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); 857 + } 858 + 859 + upper_mnt = clone_private_mount(upperpath); 860 + err = PTR_ERR(upper_mnt); 861 + if (IS_ERR(upper_mnt)) { 862 + pr_err("overlayfs: failed to clone upperpath\n"); 863 + goto out; 864 + } 865 + 866 + /* Don't inherit atime flags */ 867 + upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); 868 + ofs->upper_mnt = upper_mnt; 869 + err = 0; 870 + out: 871 + return err; 872 + } 873 + 874 + static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath) 875 + { 876 + struct dentry *temp; 877 + int err; 878 + 879 + ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); 880 + if (!ofs->workdir) 881 + return 0; 882 + 883 + /* 884 + * Upper should support d_type, else whiteouts are visible. Given 885 + * workdir and upper are on same fs, we can do iterate_dir() on 886 + * workdir. This check requires successful creation of workdir in 887 + * previous step. 888 + */ 889 + err = ovl_check_d_type_supported(workpath); 890 + if (err < 0) 891 + return err; 892 + 893 + /* 894 + * We allowed this configuration and don't want to break users over 895 + * kernel upgrade. So warn instead of erroring out. 896 + */ 897 + if (!err) 898 + pr_warn("overlayfs: upper fs needs to support d_type.\n"); 899 + 900 + /* Check if upper/work fs supports O_TMPFILE */ 901 + temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0); 902 + ofs->tmpfile = !IS_ERR(temp); 903 + if (ofs->tmpfile) 904 + dput(temp); 905 + else 906 + pr_warn("overlayfs: upper fs does not support tmpfile.\n"); 907 + 908 + /* 909 + * Check if upper/work fs supports trusted.overlay.* xattr 910 + */ 911 + err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0); 912 + if (err) { 913 + ofs->noxattr = true; 914 + pr_warn("overlayfs: upper fs does not support xattr.\n"); 915 + } else { 916 + vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE); 917 + } 918 + 919 + /* Check if upper/work fs supports file handles */ 920 + if (ofs->config.index && 921 + !ovl_can_decode_fh(ofs->workdir->d_sb)) { 922 + ofs->config.index = false; 923 + pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n"); 924 + } 925 + 926 + return 0; 927 + } 928 + 929 + static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath) 930 + { 931 + int err; 932 + struct path workpath = { }; 933 + 934 + err = ovl_mount_dir(ofs->config.workdir, &workpath); 935 + if (err) 936 + goto out; 866 937 867 938 err = -EINVAL; 868 - if (!ufs->config.lowerdir) { 869 - if (!silent) 870 - pr_err("overlayfs: missing 'lowerdir'\n"); 871 - goto out_free_config; 939 + if (upperpath->mnt != workpath.mnt) { 940 + pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 941 + goto out; 942 + } 943 + if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { 944 + pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 945 + goto out; 872 946 } 873 947 874 - sb->s_stack_depth = 0; 875 - sb->s_maxbytes = MAX_LFS_FILESIZE; 876 - if (ufs->config.upperdir) { 877 - if (!ufs->config.workdir) { 878 - pr_err("overlayfs: missing 'workdir'\n"); 879 - goto out_free_config; 880 - } 881 - 882 - err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 883 - if (err) 884 - goto out_free_config; 885 - 886 - /* Upper fs should not be r/o */ 887 - if (sb_rdonly(upperpath.mnt->mnt_sb)) { 888 - pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); 889 - err = -EINVAL; 890 - goto out_put_upperpath; 891 - } 892 - 893 - err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir); 894 - if (err) 895 - goto out_put_upperpath; 896 - 897 - err = -EBUSY; 898 - if (ovl_inuse_trylock(upperpath.dentry)) { 899 - ufs->upperdir_locked = true; 900 - } else if (ufs->config.index) { 901 - pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n"); 902 - goto out_put_upperpath; 903 - } else { 904 - pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); 905 - } 906 - 907 - err = ovl_mount_dir(ufs->config.workdir, &workpath); 908 - if (err) 909 - goto out_unlock_upperdentry; 910 - 911 - err = -EINVAL; 912 - if (upperpath.mnt != workpath.mnt) { 913 - pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 914 - goto out_put_workpath; 915 - } 916 - if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 917 - pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 918 - goto out_put_workpath; 919 - } 920 - 921 - err = -EBUSY; 922 - if (ovl_inuse_trylock(workpath.dentry)) { 923 - ufs->workdir_locked = true; 924 - } else if (ufs->config.index) { 925 - pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n"); 926 - goto out_put_workpath; 927 - } else { 928 - pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); 929 - } 930 - 931 - ufs->workbasedir = workpath.dentry; 932 - sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; 948 + err = -EBUSY; 949 + if (ovl_inuse_trylock(workpath.dentry)) { 950 + ofs->workdir_locked = true; 951 + } else if (ofs->config.index) { 952 + pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n"); 953 + goto out; 954 + } else { 955 + pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); 933 956 } 957 + 958 + ofs->workbasedir = dget(workpath.dentry); 959 + err = ovl_make_workdir(ofs, &workpath); 960 + if (err) 961 + goto out; 962 + 963 + err = 0; 964 + out: 965 + path_put(&workpath); 966 + 967 + return err; 968 + } 969 + 970 + static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe, 971 + struct path *upperpath) 972 + { 973 + int err; 974 + 975 + /* Verify lower root is upper root origin */ 976 + err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry, 977 + false, true); 978 + if (err) { 979 + pr_err("overlayfs: failed to verify upper root origin\n"); 980 + goto out; 981 + } 982 + 983 + ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); 984 + if (ofs->indexdir) { 985 + /* Verify upper root is index dir origin */ 986 + err = ovl_verify_origin(ofs->indexdir, upperpath->dentry, 987 + true, true); 988 + if (err) 989 + pr_err("overlayfs: failed to verify index dir origin\n"); 990 + 991 + /* Cleanup bad/stale/orphan index entries */ 992 + if (!err) 993 + err = ovl_indexdir_cleanup(ofs->indexdir, 994 + ofs->upper_mnt, 995 + oe->lowerstack, 996 + oe->numlower); 997 + } 998 + if (err || !ofs->indexdir) 999 + pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); 1000 + 1001 + out: 1002 + return err; 1003 + } 1004 + 1005 + static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack, 1006 + unsigned int numlower) 1007 + { 1008 + int err; 1009 + unsigned int i; 1010 + 934 1011 err = -ENOMEM; 935 - lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); 1012 + ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer), 1013 + GFP_KERNEL); 1014 + if (ofs->lower_layers == NULL) 1015 + goto out; 1016 + for (i = 0; i < numlower; i++) { 1017 + struct vfsmount *mnt; 1018 + dev_t dev; 1019 + 1020 + err = get_anon_bdev(&dev); 1021 + if (err) { 1022 + pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n"); 1023 + goto out; 1024 + } 1025 + 1026 + mnt = clone_private_mount(&stack[i]); 1027 + err = PTR_ERR(mnt); 1028 + if (IS_ERR(mnt)) { 1029 + pr_err("overlayfs: failed to clone lowerpath\n"); 1030 + free_anon_bdev(dev); 1031 + goto out; 1032 + } 1033 + /* 1034 + * Make lower layers R/O. That way fchmod/fchown on lower file 1035 + * will fail instead of modifying lower fs. 1036 + */ 1037 + mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; 1038 + 1039 + ofs->lower_layers[ofs->numlower].mnt = mnt; 1040 + ofs->lower_layers[ofs->numlower].pseudo_dev = dev; 1041 + ofs->numlower++; 1042 + 1043 + /* Check if all lower layers are on same sb */ 1044 + if (i == 0) 1045 + ofs->same_sb = mnt->mnt_sb; 1046 + else if (ofs->same_sb != mnt->mnt_sb) 1047 + ofs->same_sb = NULL; 1048 + } 1049 + err = 0; 1050 + out: 1051 + return err; 1052 + } 1053 + 1054 + static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, 1055 + struct ovl_fs *ofs) 1056 + { 1057 + int err; 1058 + char *lowertmp, *lower; 1059 + struct path *stack = NULL; 1060 + unsigned int stacklen, numlower = 0, i; 1061 + bool remote = false; 1062 + struct ovl_entry *oe; 1063 + 1064 + err = -ENOMEM; 1065 + lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL); 936 1066 if (!lowertmp) 937 - goto out_unlock_workdentry; 1067 + goto out_err; 938 1068 939 1069 err = -EINVAL; 940 1070 stacklen = ovl_split_lowerdirs(lowertmp); 941 1071 if (stacklen > OVL_MAX_STACK) { 942 1072 pr_err("overlayfs: too many lower directories, limit is %d\n", 943 1073 OVL_MAX_STACK); 944 - goto out_free_lowertmp; 945 - } else if (!ufs->config.upperdir && stacklen == 1) { 1074 + goto out_err; 1075 + } else if (!ofs->config.upperdir && stacklen == 1) { 946 1076 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); 947 - goto out_free_lowertmp; 1077 + goto out_err; 948 1078 } 949 1079 950 1080 err = -ENOMEM; 951 1081 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); 952 1082 if (!stack) 953 - goto out_free_lowertmp; 1083 + goto out_err; 954 1084 955 1085 err = -EINVAL; 956 1086 lower = lowertmp; 957 1087 for (numlower = 0; numlower < stacklen; numlower++) { 958 - err = ovl_lower_dir(lower, &stack[numlower], ufs, 1088 + err = ovl_lower_dir(lower, &stack[numlower], ofs, 959 1089 &sb->s_stack_depth, &remote); 960 1090 if (err) 961 - goto out_put_lowerpath; 1091 + goto out_err; 962 1092 963 1093 lower = strchr(lower, '\0') + 1; 964 1094 } ··· 1107 957 sb->s_stack_depth++; 1108 958 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 1109 959 pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 1110 - goto out_put_lowerpath; 960 + goto out_err; 1111 961 } 1112 962 1113 - if (ufs->config.upperdir) { 1114 - ufs->upper_mnt = clone_private_mount(&upperpath); 1115 - err = PTR_ERR(ufs->upper_mnt); 1116 - if (IS_ERR(ufs->upper_mnt)) { 1117 - pr_err("overlayfs: failed to clone upperpath\n"); 1118 - goto out_put_lowerpath; 1119 - } 1120 - 1121 - /* Don't inherit atime flags */ 1122 - ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); 1123 - 1124 - sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran; 1125 - 1126 - ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry, 1127 - OVL_WORKDIR_NAME, false); 1128 - /* 1129 - * Upper should support d_type, else whiteouts are visible. 1130 - * Given workdir and upper are on same fs, we can do 1131 - * iterate_dir() on workdir. This check requires successful 1132 - * creation of workdir in previous step. 1133 - */ 1134 - if (ufs->workdir) { 1135 - struct dentry *temp; 1136 - 1137 - err = ovl_check_d_type_supported(&workpath); 1138 - if (err < 0) 1139 - goto out_put_workdir; 1140 - 1141 - /* 1142 - * We allowed this configuration and don't want to 1143 - * break users over kernel upgrade. So warn instead 1144 - * of erroring out. 1145 - */ 1146 - if (!err) 1147 - pr_warn("overlayfs: upper fs needs to support d_type.\n"); 1148 - 1149 - /* Check if upper/work fs supports O_TMPFILE */ 1150 - temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0); 1151 - ufs->tmpfile = !IS_ERR(temp); 1152 - if (ufs->tmpfile) 1153 - dput(temp); 1154 - else 1155 - pr_warn("overlayfs: upper fs does not support tmpfile.\n"); 1156 - 1157 - /* 1158 - * Check if upper/work fs supports trusted.overlay.* 1159 - * xattr 1160 - */ 1161 - err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE, 1162 - "0", 1, 0); 1163 - if (err) { 1164 - ufs->noxattr = true; 1165 - pr_warn("overlayfs: upper fs does not support xattr.\n"); 1166 - } else { 1167 - vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE); 1168 - } 1169 - 1170 - /* Check if upper/work fs supports file handles */ 1171 - if (ufs->config.index && 1172 - !ovl_can_decode_fh(ufs->workdir->d_sb)) { 1173 - ufs->config.index = false; 1174 - pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n"); 1175 - } 1176 - } 1177 - } 963 + err = ovl_get_lower_layers(ofs, stack, numlower); 964 + if (err) 965 + goto out_err; 1178 966 1179 967 err = -ENOMEM; 1180 - ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); 1181 - if (ufs->lower_mnt == NULL) 1182 - goto out_put_workdir; 968 + oe = ovl_alloc_entry(numlower); 969 + if (!oe) 970 + goto out_err; 971 + 1183 972 for (i = 0; i < numlower; i++) { 1184 - struct vfsmount *mnt = clone_private_mount(&stack[i]); 1185 - 1186 - err = PTR_ERR(mnt); 1187 - if (IS_ERR(mnt)) { 1188 - pr_err("overlayfs: failed to clone lowerpath\n"); 1189 - goto out_put_lower_mnt; 1190 - } 1191 - /* 1192 - * Make lower_mnt R/O. That way fchmod/fchown on lower file 1193 - * will fail instead of modifying lower fs. 1194 - */ 1195 - mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; 1196 - 1197 - ufs->lower_mnt[ufs->numlower] = mnt; 1198 - ufs->numlower++; 1199 - 1200 - /* Check if all lower layers are on same sb */ 1201 - if (i == 0) 1202 - ufs->same_sb = mnt->mnt_sb; 1203 - else if (ufs->same_sb != mnt->mnt_sb) 1204 - ufs->same_sb = NULL; 973 + oe->lowerstack[i].dentry = dget(stack[i].dentry); 974 + oe->lowerstack[i].layer = &ofs->lower_layers[i]; 1205 975 } 1206 - 1207 - /* If the upper fs is nonexistent, we mark overlayfs r/o too */ 1208 - if (!ufs->upper_mnt) 1209 - sb->s_flags |= MS_RDONLY; 1210 - else if (ufs->upper_mnt->mnt_sb != ufs->same_sb) 1211 - ufs->same_sb = NULL; 1212 - 1213 - if (!(ovl_force_readonly(ufs)) && ufs->config.index) { 1214 - /* Verify lower root is upper root origin */ 1215 - err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0], 1216 - stack[0].dentry, false, true); 1217 - if (err) { 1218 - pr_err("overlayfs: failed to verify upper root origin\n"); 1219 - goto out_put_lower_mnt; 1220 - } 1221 - 1222 - ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry, 1223 - OVL_INDEXDIR_NAME, true); 1224 - if (ufs->indexdir) { 1225 - /* Verify upper root is index dir origin */ 1226 - err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt, 1227 - upperpath.dentry, true, true); 1228 - if (err) 1229 - pr_err("overlayfs: failed to verify index dir origin\n"); 1230 - 1231 - /* Cleanup bad/stale/orphan index entries */ 1232 - if (!err) 1233 - err = ovl_indexdir_cleanup(ufs->indexdir, 1234 - ufs->upper_mnt, 1235 - stack, numlower); 1236 - } 1237 - if (err || !ufs->indexdir) 1238 - pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); 1239 - if (err) 1240 - goto out_put_indexdir; 1241 - } 1242 - 1243 - /* Show index=off/on in /proc/mounts for any of the reasons above */ 1244 - if (!ufs->indexdir) 1245 - ufs->config.index = false; 1246 976 1247 977 if (remote) 1248 978 sb->s_d_op = &ovl_reval_dentry_operations; 1249 979 else 1250 980 sb->s_d_op = &ovl_dentry_operations; 1251 981 982 + out: 983 + for (i = 0; i < numlower; i++) 984 + path_put(&stack[i]); 985 + kfree(stack); 986 + kfree(lowertmp); 987 + 988 + return oe; 989 + 990 + out_err: 991 + oe = ERR_PTR(err); 992 + goto out; 993 + } 994 + 995 + static int ovl_fill_super(struct super_block *sb, void *data, int silent) 996 + { 997 + struct path upperpath = { }; 998 + struct dentry *root_dentry; 999 + struct ovl_entry *oe; 1000 + struct ovl_fs *ofs; 1001 + struct cred *cred; 1002 + int err; 1003 + 1252 1004 err = -ENOMEM; 1253 - ufs->creator_cred = cred = prepare_creds(); 1005 + ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); 1006 + if (!ofs) 1007 + goto out; 1008 + 1009 + ofs->creator_cred = cred = prepare_creds(); 1254 1010 if (!cred) 1255 - goto out_put_indexdir; 1011 + goto out_err; 1012 + 1013 + ofs->config.redirect_dir = ovl_redirect_dir_def; 1014 + ofs->config.index = ovl_index_def; 1015 + err = ovl_parse_opt((char *) data, &ofs->config); 1016 + if (err) 1017 + goto out_err; 1018 + 1019 + err = -EINVAL; 1020 + if (!ofs->config.lowerdir) { 1021 + if (!silent) 1022 + pr_err("overlayfs: missing 'lowerdir'\n"); 1023 + goto out_err; 1024 + } 1025 + 1026 + sb->s_stack_depth = 0; 1027 + sb->s_maxbytes = MAX_LFS_FILESIZE; 1028 + if (ofs->config.upperdir) { 1029 + if (!ofs->config.workdir) { 1030 + pr_err("overlayfs: missing 'workdir'\n"); 1031 + goto out_err; 1032 + } 1033 + 1034 + err = ovl_get_upper(ofs, &upperpath); 1035 + if (err) 1036 + goto out_err; 1037 + 1038 + err = ovl_get_workdir(ofs, &upperpath); 1039 + if (err) 1040 + goto out_err; 1041 + 1042 + if (!ofs->workdir) 1043 + sb->s_flags |= MS_RDONLY; 1044 + 1045 + sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth; 1046 + sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran; 1047 + 1048 + } 1049 + oe = ovl_get_lowerstack(sb, ofs); 1050 + err = PTR_ERR(oe); 1051 + if (IS_ERR(oe)) 1052 + goto out_err; 1053 + 1054 + /* If the upper fs is nonexistent, we mark overlayfs r/o too */ 1055 + if (!ofs->upper_mnt) 1056 + sb->s_flags |= MS_RDONLY; 1057 + else if (ofs->upper_mnt->mnt_sb != ofs->same_sb) 1058 + ofs->same_sb = NULL; 1059 + 1060 + if (!(ovl_force_readonly(ofs)) && ofs->config.index) { 1061 + err = ovl_get_indexdir(ofs, oe, &upperpath); 1062 + if (err) 1063 + goto out_free_oe; 1064 + 1065 + if (!ofs->indexdir) 1066 + sb->s_flags |= MS_RDONLY; 1067 + } 1068 + 1069 + /* Show index=off/on in /proc/mounts for any of the reasons above */ 1070 + if (!ofs->indexdir) 1071 + ofs->config.index = false; 1256 1072 1257 1073 /* Never override disk quota limits or use reserved space */ 1258 1074 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); 1259 1075 1260 - err = -ENOMEM; 1261 - oe = ovl_alloc_entry(numlower); 1262 - if (!oe) 1263 - goto out_put_cred; 1264 - 1265 1076 sb->s_magic = OVERLAYFS_SUPER_MAGIC; 1266 1077 sb->s_op = &ovl_super_operations; 1267 1078 sb->s_xattr = ovl_xattr_handlers; 1268 - sb->s_fs_info = ufs; 1079 + sb->s_fs_info = ofs; 1269 1080 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK; 1270 1081 1082 + err = -ENOMEM; 1271 1083 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); 1272 1084 if (!root_dentry) 1273 1085 goto out_free_oe; 1274 1086 1275 1087 mntput(upperpath.mnt); 1276 - for (i = 0; i < numlower; i++) 1277 - mntput(stack[i].mnt); 1278 - mntput(workpath.mnt); 1279 - kfree(lowertmp); 1280 - 1281 1088 if (upperpath.dentry) { 1282 1089 oe->has_upper = true; 1283 1090 if (ovl_is_impuredir(upperpath.dentry)) 1284 1091 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry)); 1285 1092 } 1286 - for (i = 0; i < numlower; i++) { 1287 - oe->lowerstack[i].dentry = stack[i].dentry; 1288 - oe->lowerstack[i].mnt = ufs->lower_mnt[i]; 1289 - } 1290 - kfree(stack); 1291 1093 1292 1094 root_dentry->d_fsdata = oe; 1293 1095 1096 + /* Root is always merge -> can have whiteouts */ 1097 + ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry)); 1294 1098 ovl_inode_init(d_inode(root_dentry), upperpath.dentry, 1295 1099 ovl_dentry_lower(root_dentry)); 1296 1100 ··· 1253 1149 return 0; 1254 1150 1255 1151 out_free_oe: 1152 + ovl_entry_stack_free(oe); 1256 1153 kfree(oe); 1257 - out_put_cred: 1258 - put_cred(ufs->creator_cred); 1259 - out_put_indexdir: 1260 - dput(ufs->indexdir); 1261 - out_put_lower_mnt: 1262 - for (i = 0; i < ufs->numlower; i++) 1263 - mntput(ufs->lower_mnt[i]); 1264 - kfree(ufs->lower_mnt); 1265 - out_put_workdir: 1266 - dput(ufs->workdir); 1267 - mntput(ufs->upper_mnt); 1268 - out_put_lowerpath: 1269 - for (i = 0; i < numlower; i++) 1270 - path_put(&stack[i]); 1271 - kfree(stack); 1272 - out_free_lowertmp: 1273 - kfree(lowertmp); 1274 - out_unlock_workdentry: 1275 - if (ufs->workdir_locked) 1276 - ovl_inuse_unlock(workpath.dentry); 1277 - out_put_workpath: 1278 - path_put(&workpath); 1279 - out_unlock_upperdentry: 1280 - if (ufs->upperdir_locked) 1281 - ovl_inuse_unlock(upperpath.dentry); 1282 - out_put_upperpath: 1154 + out_err: 1283 1155 path_put(&upperpath); 1284 - out_free_config: 1285 - kfree(ufs->config.lowerdir); 1286 - kfree(ufs->config.upperdir); 1287 - kfree(ufs->config.workdir); 1288 - kfree(ufs); 1156 + ovl_free_fs(ofs); 1289 1157 out: 1290 1158 return err; 1291 1159 }
+19 -2
fs/overlayfs/util.c
··· 17 17 #include <linux/namei.h> 18 18 #include <linux/ratelimit.h> 19 19 #include "overlayfs.h" 20 - #include "ovl_entry.h" 21 20 22 21 int ovl_want_write(struct dentry *dentry) 23 22 { ··· 124 125 { 125 126 struct ovl_entry *oe = dentry->d_fsdata; 126 127 127 - *path = oe->numlower ? oe->lowerstack[0] : (struct path) { }; 128 + if (oe->numlower) { 129 + path->mnt = oe->lowerstack[0].layer->mnt; 130 + path->dentry = oe->lowerstack[0].dentry; 131 + } else { 132 + *path = (struct path) { }; 133 + } 128 134 } 129 135 130 136 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) ··· 331 327 void ovl_copy_up_end(struct dentry *dentry) 332 328 { 333 329 mutex_unlock(&OVL_I(d_inode(dentry))->lock); 330 + } 331 + 332 + bool ovl_check_origin_xattr(struct dentry *dentry) 333 + { 334 + int res; 335 + 336 + res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); 337 + 338 + /* Zero size value means "copied up but origin unknown" */ 339 + if (res >= 0) 340 + return true; 341 + 342 + return false; 334 343 } 335 344 336 345 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
+6
include/linux/path.h
··· 18 18 return path1->mnt == path2->mnt && path1->dentry == path2->dentry; 19 19 } 20 20 21 + static inline void path_put_init(struct path *path) 22 + { 23 + path_put(path); 24 + *path = (struct path) { }; 25 + } 26 + 21 27 #endif /* _LINUX_PATH_H */