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

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

Al Viro ce7b9fac a95104fd

+537 -339
+28
Documentation/filesystems/overlayfs.txt
··· 159 159 rename or unlink will of course be noticed and handled). 160 160 161 161 162 + Multiple lower layers 163 + --------------------- 164 + 165 + Multiple lower layers can now be given using the the colon (":") as a 166 + separator character between the directory names. For example: 167 + 168 + mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged 169 + 170 + As the example shows, "upperdir=" and "workdir=" may be omitted. In 171 + that case the overlay will be read-only. 172 + 173 + The specified lower directories will be stacked beginning from the 174 + rightmost one and going left. In the above example lower1 will be the 175 + top, lower2 the middle and lower3 the bottom layer. 176 + 177 + 162 178 Non-standard behavior 163 179 --------------------- 164 180 ··· 212 196 filesystem are not allowed. If the underlying filesystem is changed, 213 197 the behavior of the overlay is undefined, though it will not result in 214 198 a crash or deadlock. 199 + 200 + Testsuite 201 + --------- 202 + 203 + There's testsuite developed by David Howells at: 204 + 205 + git://git.infradead.org/users/dhowells/unionmount-testsuite.git 206 + 207 + Run as root: 208 + 209 + # cd unionmount-testsuite 210 + # ./run --ov
+2 -3
fs/overlayfs/copy_up.c
··· 191 191 ovl_set_timestamps(upperdentry, stat); 192 192 193 193 return err; 194 - 195 194 } 196 195 197 196 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, ··· 384 385 struct kstat stat; 385 386 enum ovl_path_type type = ovl_path_type(dentry); 386 387 387 - if (type != OVL_PATH_LOWER) 388 + if (OVL_TYPE_UPPER(type)) 388 389 break; 389 390 390 391 next = dget(dentry); ··· 393 394 parent = dget_parent(next); 394 395 395 396 type = ovl_path_type(parent); 396 - if (type != OVL_PATH_LOWER) 397 + if (OVL_TYPE_UPPER(type)) 397 398 break; 398 399 399 400 dput(next);
+14 -14
fs/overlayfs/dir.c
··· 118 118 119 119 static int ovl_set_opaque(struct dentry *upperdentry) 120 120 { 121 - return ovl_do_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0); 121 + return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0); 122 122 } 123 123 124 124 static void ovl_remove_opaque(struct dentry *upperdentry) 125 125 { 126 126 int err; 127 127 128 - err = ovl_do_removexattr(upperdentry, ovl_opaque_xattr); 128 + err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE); 129 129 if (err) { 130 130 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n", 131 131 upperdentry->d_name.name, err); ··· 152 152 * correct link count. nlink=1 seems to pacify 'find' and 153 153 * other utilities. 154 154 */ 155 - if (type == OVL_PATH_MERGE) 155 + if (OVL_TYPE_MERGE(type)) 156 156 stat->nlink = 1; 157 157 158 158 return 0; ··· 506 506 struct dentry *opaquedir = NULL; 507 507 int err; 508 508 509 - if (is_dir) { 509 + if (is_dir && OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) { 510 510 opaquedir = ovl_check_empty_and_clear(dentry); 511 511 err = PTR_ERR(opaquedir); 512 512 if (IS_ERR(opaquedir)) ··· 630 630 goto out_drop_write; 631 631 632 632 type = ovl_path_type(dentry); 633 - if (type == OVL_PATH_PURE_UPPER) { 633 + if (OVL_TYPE_PURE_UPPER(type)) { 634 634 err = ovl_remove_upper(dentry, is_dir); 635 635 } else { 636 636 const struct cred *old_cred; ··· 712 712 /* Don't copy up directory trees */ 713 713 old_type = ovl_path_type(old); 714 714 err = -EXDEV; 715 - if ((old_type == OVL_PATH_LOWER || old_type == OVL_PATH_MERGE) && is_dir) 715 + if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir) 716 716 goto out; 717 717 718 718 if (new->d_inode) { ··· 725 725 726 726 new_type = ovl_path_type(new); 727 727 err = -EXDEV; 728 - if (!overwrite && (new_type == OVL_PATH_LOWER || new_type == OVL_PATH_MERGE) && new_is_dir) 728 + if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) 729 729 goto out; 730 730 731 731 err = 0; 732 - if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) { 732 + if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) { 733 733 if (ovl_dentry_lower(old)->d_inode == 734 734 ovl_dentry_lower(new)->d_inode) 735 735 goto out; 736 736 } 737 - if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) { 737 + if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) { 738 738 if (ovl_dentry_upper(old)->d_inode == 739 739 ovl_dentry_upper(new)->d_inode) 740 740 goto out; 741 741 } 742 742 } else { 743 743 if (ovl_dentry_is_opaque(new)) 744 - new_type = OVL_PATH_UPPER; 744 + new_type = __OVL_PATH_UPPER; 745 745 else 746 - new_type = OVL_PATH_PURE_UPPER; 746 + new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE; 747 747 } 748 748 749 749 err = ovl_want_write(old); ··· 763 763 goto out_drop_write; 764 764 } 765 765 766 - old_opaque = old_type != OVL_PATH_PURE_UPPER; 767 - new_opaque = new_type != OVL_PATH_PURE_UPPER; 766 + old_opaque = !OVL_TYPE_PURE_UPPER(old_type); 767 + new_opaque = !OVL_TYPE_PURE_UPPER(new_type); 768 768 769 769 if (old_opaque || new_opaque) { 770 770 err = -ENOMEM; ··· 787 787 old_cred = override_creds(override_cred); 788 788 } 789 789 790 - if (overwrite && (new_type == OVL_PATH_LOWER || new_type == OVL_PATH_MERGE) && new_is_dir) { 790 + if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) { 791 791 opaquedir = ovl_check_empty_and_clear(new); 792 792 err = PTR_ERR(opaquedir); 793 793 if (IS_ERR(opaquedir)) {
+7 -5
fs/overlayfs/inode.c
··· 205 205 206 206 static bool ovl_is_private_xattr(const char *name) 207 207 { 208 - return strncmp(name, "trusted.overlay.", 14) == 0; 208 + return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0; 209 209 } 210 210 211 211 int ovl_setxattr(struct dentry *dentry, const char *name, ··· 238 238 static bool ovl_need_xattr_filter(struct dentry *dentry, 239 239 enum ovl_path_type type) 240 240 { 241 - return type == OVL_PATH_UPPER && S_ISDIR(dentry->d_inode->i_mode); 241 + if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER) 242 + return S_ISDIR(dentry->d_inode->i_mode); 243 + else 244 + return false; 242 245 } 243 246 244 247 ssize_t ovl_getxattr(struct dentry *dentry, const char *name, ··· 302 299 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name)) 303 300 goto out_drop_write; 304 301 305 - if (type == OVL_PATH_LOWER) { 302 + if (!OVL_TYPE_UPPER(type)) { 306 303 err = vfs_getxattr(realpath.dentry, name, NULL, 0); 307 304 if (err < 0) 308 305 goto out_drop_write; ··· 324 321 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, 325 322 struct dentry *realdentry) 326 323 { 327 - if (type != OVL_PATH_LOWER) 324 + if (OVL_TYPE_UPPER(type)) 328 325 return false; 329 326 330 327 if (special_file(realdentry->d_inode->i_mode)) ··· 433 430 } 434 431 435 432 return inode; 436 - 437 433 }
+13 -5
fs/overlayfs/overlayfs.h
··· 12 12 struct ovl_entry; 13 13 14 14 enum ovl_path_type { 15 - OVL_PATH_PURE_UPPER, 16 - OVL_PATH_UPPER, 17 - OVL_PATH_MERGE, 18 - OVL_PATH_LOWER, 15 + __OVL_PATH_PURE = (1 << 0), 16 + __OVL_PATH_UPPER = (1 << 1), 17 + __OVL_PATH_MERGE = (1 << 2), 19 18 }; 20 19 21 - extern const char *ovl_opaque_xattr; 20 + #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER) 21 + #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE) 22 + #define OVL_TYPE_PURE_UPPER(type) ((type) & __OVL_PATH_PURE) 23 + #define OVL_TYPE_MERGE_OR_LOWER(type) \ 24 + (OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type)) 25 + 26 + #define OVL_XATTR_PRE_NAME "trusted.overlay." 27 + #define OVL_XATTR_PRE_LEN 16 28 + #define OVL_XATTR_OPAQUE OVL_XATTR_PRE_NAME"opaque" 22 29 23 30 static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry) 24 31 { ··· 137 130 void ovl_path_upper(struct dentry *dentry, struct path *path); 138 131 void ovl_path_lower(struct dentry *dentry, struct path *path); 139 132 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path); 133 + int ovl_path_next(int idx, struct dentry *dentry, struct path *path); 140 134 struct dentry *ovl_dentry_upper(struct dentry *dentry); 141 135 struct dentry *ovl_dentry_lower(struct dentry *dentry); 142 136 struct dentry *ovl_dentry_real(struct dentry *dentry);
+75 -106
fs/overlayfs/readdir.c
··· 24 24 struct list_head l_node; 25 25 struct rb_node node; 26 26 bool is_whiteout; 27 - bool is_cursor; 28 27 char name[]; 29 28 }; 30 29 ··· 39 40 struct rb_root root; 40 41 struct list_head *list; 41 42 struct list_head middle; 43 + struct dentry *dir; 42 44 int count; 43 45 int err; 44 46 }; ··· 48 48 bool is_real; 49 49 bool is_upper; 50 50 struct ovl_dir_cache *cache; 51 - struct ovl_cache_entry cursor; 51 + struct list_head *cursor; 52 52 struct file *realfile; 53 53 struct file *upperfile; 54 54 }; ··· 79 79 return NULL; 80 80 } 81 81 82 - static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len, 82 + static struct ovl_cache_entry *ovl_cache_entry_new(struct dentry *dir, 83 + const char *name, int len, 83 84 u64 ino, unsigned int d_type) 84 85 { 85 86 struct ovl_cache_entry *p; 86 87 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]); 87 88 88 89 p = kmalloc(size, GFP_KERNEL); 89 - if (p) { 90 - memcpy(p->name, name, len); 91 - p->name[len] = '\0'; 92 - p->len = len; 93 - p->type = d_type; 94 - p->ino = ino; 95 - p->is_whiteout = false; 96 - p->is_cursor = false; 97 - } 90 + if (!p) 91 + return NULL; 98 92 93 + memcpy(p->name, name, len); 94 + p->name[len] = '\0'; 95 + p->len = len; 96 + p->type = d_type; 97 + p->ino = ino; 98 + p->is_whiteout = false; 99 + 100 + if (d_type == DT_CHR) { 101 + struct dentry *dentry; 102 + const struct cred *old_cred; 103 + struct cred *override_cred; 104 + 105 + override_cred = prepare_creds(); 106 + if (!override_cred) { 107 + kfree(p); 108 + return NULL; 109 + } 110 + 111 + /* 112 + * CAP_DAC_OVERRIDE for lookup 113 + */ 114 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); 115 + old_cred = override_creds(override_cred); 116 + 117 + dentry = lookup_one_len(name, dir, len); 118 + if (!IS_ERR(dentry)) { 119 + p->is_whiteout = ovl_is_whiteout(dentry); 120 + dput(dentry); 121 + } 122 + revert_creds(old_cred); 123 + put_cred(override_cred); 124 + } 99 125 return p; 100 126 } 101 127 ··· 148 122 return 0; 149 123 } 150 124 151 - p = ovl_cache_entry_new(name, len, ino, d_type); 125 + p = ovl_cache_entry_new(rdd->dir, name, len, ino, d_type); 152 126 if (p == NULL) 153 127 return -ENOMEM; 154 128 ··· 169 143 if (p) { 170 144 list_move_tail(&p->l_node, &rdd->middle); 171 145 } else { 172 - p = ovl_cache_entry_new(name, namelen, ino, d_type); 146 + p = ovl_cache_entry_new(rdd->dir, name, namelen, ino, d_type); 173 147 if (p == NULL) 174 148 rdd->err = -ENOMEM; 175 149 else ··· 194 168 { 195 169 struct ovl_dir_cache *cache = od->cache; 196 170 197 - list_del_init(&od->cursor.l_node); 198 171 WARN_ON(cache->refcount <= 0); 199 172 cache->refcount--; 200 173 if (!cache->refcount) { ··· 229 204 if (IS_ERR(realfile)) 230 205 return PTR_ERR(realfile); 231 206 207 + rdd->dir = realpath->dentry; 232 208 rdd->ctx.pos = 0; 233 209 do { 234 210 rdd->count = 0; ··· 253 227 if (cache && ovl_dentry_version_get(dentry) != cache->version) { 254 228 ovl_cache_put(od, dentry); 255 229 od->cache = NULL; 230 + od->cursor = NULL; 256 231 } 257 - WARN_ON(!od->is_real && type != OVL_PATH_MERGE); 258 - if (od->is_real && type == OVL_PATH_MERGE) 232 + WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type)); 233 + if (od->is_real && OVL_TYPE_MERGE(type)) 259 234 od->is_real = false; 260 - } 261 - 262 - static int ovl_dir_mark_whiteouts(struct dentry *dir, 263 - struct ovl_readdir_data *rdd) 264 - { 265 - struct ovl_cache_entry *p; 266 - struct dentry *dentry; 267 - const struct cred *old_cred; 268 - struct cred *override_cred; 269 - 270 - override_cred = prepare_creds(); 271 - if (!override_cred) { 272 - ovl_cache_free(rdd->list); 273 - return -ENOMEM; 274 - } 275 - 276 - /* 277 - * CAP_DAC_OVERRIDE for lookup 278 - */ 279 - cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); 280 - old_cred = override_creds(override_cred); 281 - 282 - mutex_lock(&dir->d_inode->i_mutex); 283 - list_for_each_entry(p, rdd->list, l_node) { 284 - if (p->is_cursor) 285 - continue; 286 - 287 - if (p->type != DT_CHR) 288 - continue; 289 - 290 - dentry = lookup_one_len(p->name, dir, p->len); 291 - if (IS_ERR(dentry)) 292 - continue; 293 - 294 - p->is_whiteout = ovl_is_whiteout(dentry); 295 - dput(dentry); 296 - } 297 - mutex_unlock(&dir->d_inode->i_mutex); 298 - 299 - revert_creds(old_cred); 300 - put_cred(override_cred); 301 - 302 - return 0; 303 235 } 304 236 305 237 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list) 306 238 { 307 239 int err; 308 - struct path lowerpath; 309 - struct path upperpath; 240 + struct path realpath; 310 241 struct ovl_readdir_data rdd = { 311 242 .ctx.actor = ovl_fill_merge, 312 243 .list = list, 313 244 .root = RB_ROOT, 314 245 .is_merge = false, 315 246 }; 247 + int idx, next; 316 248 317 - ovl_path_lower(dentry, &lowerpath); 318 - ovl_path_upper(dentry, &upperpath); 249 + for (idx = 0; idx != -1; idx = next) { 250 + next = ovl_path_next(idx, dentry, &realpath); 319 251 320 - if (upperpath.dentry) { 321 - err = ovl_dir_read(&upperpath, &rdd); 322 - if (err) 323 - goto out; 324 - 325 - if (lowerpath.dentry) { 326 - err = ovl_dir_mark_whiteouts(upperpath.dentry, &rdd); 252 + if (next != -1) { 253 + err = ovl_dir_read(&realpath, &rdd); 327 254 if (err) 328 - goto out; 255 + break; 256 + } else { 257 + /* 258 + * Insert lowest layer entries before upper ones, this 259 + * allows offsets to be reasonably constant 260 + */ 261 + list_add(&rdd.middle, rdd.list); 262 + rdd.is_merge = true; 263 + err = ovl_dir_read(&realpath, &rdd); 264 + list_del(&rdd.middle); 329 265 } 330 266 } 331 - if (lowerpath.dentry) { 332 - /* 333 - * Insert lowerpath entries before upperpath ones, this allows 334 - * offsets to be reasonably constant 335 - */ 336 - list_add(&rdd.middle, rdd.list); 337 - rdd.is_merge = true; 338 - err = ovl_dir_read(&lowerpath, &rdd); 339 - list_del(&rdd.middle); 340 - } 341 - out: 342 267 return err; 343 268 } 344 269 345 270 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) 346 271 { 347 - struct ovl_cache_entry *p; 272 + struct list_head *p; 348 273 loff_t off = 0; 349 274 350 - list_for_each_entry(p, &od->cache->entries, l_node) { 351 - if (p->is_cursor) 352 - continue; 275 + list_for_each(p, &od->cache->entries) { 353 276 if (off >= pos) 354 277 break; 355 278 off++; 356 279 } 357 - list_move_tail(&od->cursor.l_node, &p->l_node); 280 + /* Cursor is safe since the cache is stable */ 281 + od->cursor = p; 358 282 } 359 283 360 284 static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry) ··· 343 367 { 344 368 struct ovl_dir_file *od = file->private_data; 345 369 struct dentry *dentry = file->f_path.dentry; 370 + struct ovl_cache_entry *p; 346 371 347 372 if (!ctx->pos) 348 373 ovl_dir_reset(file); ··· 362 385 ovl_seek_cursor(od, ctx->pos); 363 386 } 364 387 365 - while (od->cursor.l_node.next != &od->cache->entries) { 366 - struct ovl_cache_entry *p; 367 - 368 - p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node); 369 - /* Skip cursors */ 370 - if (!p->is_cursor) { 371 - if (!p->is_whiteout) { 372 - if (!dir_emit(ctx, p->name, p->len, p->ino, p->type)) 373 - break; 374 - } 375 - ctx->pos++; 376 - } 377 - list_move(&od->cursor.l_node, &p->l_node); 388 + while (od->cursor != &od->cache->entries) { 389 + p = list_entry(od->cursor, struct ovl_cache_entry, l_node); 390 + if (!p->is_whiteout) 391 + if (!dir_emit(ctx, p->name, p->len, p->ino, p->type)) 392 + break; 393 + od->cursor = p->l_node.next; 394 + ctx->pos++; 378 395 } 379 396 return 0; 380 397 } ··· 423 452 /* 424 453 * Need to check if we started out being a lower dir, but got copied up 425 454 */ 426 - if (!od->is_upper && ovl_path_type(dentry) != OVL_PATH_LOWER) { 455 + if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) { 427 456 struct inode *inode = file_inode(file); 428 457 429 458 realfile = lockless_dereference(od->upperfile); ··· 487 516 kfree(od); 488 517 return PTR_ERR(realfile); 489 518 } 490 - INIT_LIST_HEAD(&od->cursor.l_node); 491 519 od->realfile = realfile; 492 - od->is_real = (type != OVL_PATH_MERGE); 493 - od->is_upper = (type != OVL_PATH_LOWER); 494 - od->cursor.is_cursor = true; 520 + od->is_real = !OVL_TYPE_MERGE(type); 521 + od->is_upper = OVL_TYPE_UPPER(type); 495 522 file->private_data = od; 496 523 497 524 return 0;
+398 -206
fs/overlayfs/super.c
··· 35 35 /* private information held for overlayfs's superblock */ 36 36 struct ovl_fs { 37 37 struct vfsmount *upper_mnt; 38 - struct vfsmount *lower_mnt; 38 + unsigned numlower; 39 + struct vfsmount **lower_mnt; 39 40 struct dentry *workdir; 40 41 long lower_namelen; 41 42 /* pathnames of lower and upper dirs, for show_options */ ··· 48 47 /* private information held for every overlayfs dentry */ 49 48 struct ovl_entry { 50 49 struct dentry *__upperdentry; 51 - struct dentry *lowerdentry; 52 50 struct ovl_dir_cache *cache; 53 51 union { 54 52 struct { ··· 56 56 }; 57 57 struct rcu_head rcu; 58 58 }; 59 + unsigned numlower; 60 + struct path lowerstack[]; 59 61 }; 60 62 61 - const char *ovl_opaque_xattr = "trusted.overlay.opaque"; 63 + #define OVL_MAX_STACK 500 62 64 65 + static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) 66 + { 67 + return oe->numlower ? oe->lowerstack[0].dentry : NULL; 68 + } 63 69 64 70 enum ovl_path_type ovl_path_type(struct dentry *dentry) 65 71 { 66 72 struct ovl_entry *oe = dentry->d_fsdata; 73 + enum ovl_path_type type = 0; 67 74 68 75 if (oe->__upperdentry) { 69 - if (oe->lowerdentry) { 76 + type = __OVL_PATH_UPPER; 77 + 78 + if (oe->numlower) { 70 79 if (S_ISDIR(dentry->d_inode->i_mode)) 71 - return OVL_PATH_MERGE; 72 - else 73 - return OVL_PATH_UPPER; 74 - } else { 75 - if (oe->opaque) 76 - return OVL_PATH_UPPER; 77 - else 78 - return OVL_PATH_PURE_UPPER; 80 + type |= __OVL_PATH_MERGE; 81 + } else if (!oe->opaque) { 82 + type |= __OVL_PATH_PURE; 79 83 } 80 84 } else { 81 - return OVL_PATH_LOWER; 85 + if (oe->numlower > 1) 86 + type |= __OVL_PATH_MERGE; 82 87 } 88 + return type; 83 89 } 84 90 85 91 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) ··· 104 98 105 99 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 106 100 { 107 - 108 101 enum ovl_path_type type = ovl_path_type(dentry); 109 102 110 - if (type == OVL_PATH_LOWER) 103 + if (!OVL_TYPE_UPPER(type)) 111 104 ovl_path_lower(dentry, path); 112 105 else 113 106 ovl_path_upper(dentry, path); ··· 125 120 { 126 121 struct ovl_entry *oe = dentry->d_fsdata; 127 122 128 - return oe->lowerdentry; 123 + return __ovl_dentry_lower(oe); 129 124 } 130 125 131 126 struct dentry *ovl_dentry_real(struct dentry *dentry) ··· 135 130 136 131 realdentry = ovl_upperdentry_dereference(oe); 137 132 if (!realdentry) 138 - realdentry = oe->lowerdentry; 133 + realdentry = __ovl_dentry_lower(oe); 139 134 140 135 return realdentry; 141 136 } ··· 148 143 if (realdentry) { 149 144 *is_upper = true; 150 145 } else { 151 - realdentry = oe->lowerdentry; 146 + realdentry = __ovl_dentry_lower(oe); 152 147 *is_upper = false; 153 148 } 154 149 return realdentry; ··· 170 165 171 166 void ovl_path_lower(struct dentry *dentry, struct path *path) 172 167 { 173 - struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 174 168 struct ovl_entry *oe = dentry->d_fsdata; 175 169 176 - path->mnt = ofs->lower_mnt; 177 - path->dentry = oe->lowerdentry; 170 + *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; 178 171 } 179 172 180 173 int ovl_want_write(struct dentry *dentry) ··· 252 249 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) 253 250 return false; 254 251 255 - res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); 252 + res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); 256 253 if (res == 1 && val == 'y') 257 254 return true; 258 255 ··· 264 261 struct ovl_entry *oe = dentry->d_fsdata; 265 262 266 263 if (oe) { 264 + unsigned int i; 265 + 267 266 dput(oe->__upperdentry); 268 - dput(oe->lowerdentry); 267 + for (i = 0; i < oe->numlower; i++) 268 + dput(oe->lowerstack[i].dentry); 269 269 kfree_rcu(oe, rcu); 270 270 } 271 271 } ··· 277 271 .d_release = ovl_dentry_release, 278 272 }; 279 273 280 - static struct ovl_entry *ovl_alloc_entry(void) 274 + static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 281 275 { 282 - return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL); 276 + size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 277 + struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 278 + 279 + if (oe) 280 + oe->numlower = numlower; 281 + 282 + return oe; 283 283 } 284 284 285 285 static inline struct dentry *ovl_lookup_real(struct dentry *dir, ··· 307 295 return dentry; 308 296 } 309 297 298 + /* 299 + * Returns next layer in stack starting from top. 300 + * Returns -1 if this is the last layer. 301 + */ 302 + int ovl_path_next(int idx, struct dentry *dentry, struct path *path) 303 + { 304 + struct ovl_entry *oe = dentry->d_fsdata; 305 + 306 + BUG_ON(idx < 0); 307 + if (idx == 0) { 308 + ovl_path_upper(dentry, path); 309 + if (path->dentry) 310 + return oe->numlower ? 1 : -1; 311 + idx++; 312 + } 313 + BUG_ON(idx > oe->numlower); 314 + *path = oe->lowerstack[idx - 1]; 315 + 316 + return (idx < oe->numlower) ? idx + 1 : -1; 317 + } 318 + 310 319 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 311 320 unsigned int flags) 312 321 { 313 322 struct ovl_entry *oe; 314 - struct dentry *upperdir; 315 - struct dentry *lowerdir; 316 - struct dentry *upperdentry = NULL; 317 - struct dentry *lowerdentry = NULL; 323 + struct ovl_entry *poe = dentry->d_parent->d_fsdata; 324 + struct path *stack = NULL; 325 + struct dentry *upperdir, *upperdentry = NULL; 326 + unsigned int ctr = 0; 318 327 struct inode *inode = NULL; 328 + bool upperopaque = false; 329 + struct dentry *this, *prev = NULL; 330 + unsigned int i; 319 331 int err; 320 332 321 - err = -ENOMEM; 322 - oe = ovl_alloc_entry(); 323 - if (!oe) 324 - goto out; 325 - 326 - upperdir = ovl_dentry_upper(dentry->d_parent); 327 - lowerdir = ovl_dentry_lower(dentry->d_parent); 328 - 333 + upperdir = ovl_upperdentry_dereference(poe); 329 334 if (upperdir) { 330 - upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); 331 - err = PTR_ERR(upperdentry); 332 - if (IS_ERR(upperdentry)) 333 - goto out_put_dir; 335 + this = ovl_lookup_real(upperdir, &dentry->d_name); 336 + err = PTR_ERR(this); 337 + if (IS_ERR(this)) 338 + goto out; 334 339 335 - if (lowerdir && upperdentry) { 336 - if (ovl_is_whiteout(upperdentry)) { 337 - dput(upperdentry); 338 - upperdentry = NULL; 339 - oe->opaque = true; 340 - } else if (ovl_is_opaquedir(upperdentry)) { 341 - oe->opaque = true; 340 + if (this) { 341 + if (ovl_is_whiteout(this)) { 342 + dput(this); 343 + this = NULL; 344 + upperopaque = true; 345 + } else if (poe->numlower && ovl_is_opaquedir(this)) { 346 + upperopaque = true; 342 347 } 343 348 } 344 - } 345 - if (lowerdir && !oe->opaque) { 346 - lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name); 347 - err = PTR_ERR(lowerdentry); 348 - if (IS_ERR(lowerdentry)) 349 - goto out_dput_upper; 349 + upperdentry = prev = this; 350 350 } 351 351 352 - if (lowerdentry && upperdentry && 353 - (!S_ISDIR(upperdentry->d_inode->i_mode) || 354 - !S_ISDIR(lowerdentry->d_inode->i_mode))) { 355 - dput(lowerdentry); 356 - lowerdentry = NULL; 357 - oe->opaque = true; 352 + if (!upperopaque && poe->numlower) { 353 + err = -ENOMEM; 354 + stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); 355 + if (!stack) 356 + goto out_put_upper; 358 357 } 359 358 360 - if (lowerdentry || upperdentry) { 359 + for (i = 0; !upperopaque && i < poe->numlower; i++) { 360 + bool opaque = false; 361 + struct path lowerpath = poe->lowerstack[i]; 362 + 363 + this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); 364 + err = PTR_ERR(this); 365 + if (IS_ERR(this)) { 366 + /* 367 + * If it's positive, then treat ENAMETOOLONG as ENOENT. 368 + */ 369 + if (err == -ENAMETOOLONG && (upperdentry || ctr)) 370 + continue; 371 + goto out_put; 372 + } 373 + if (!this) 374 + continue; 375 + if (ovl_is_whiteout(this)) { 376 + dput(this); 377 + break; 378 + } 379 + /* 380 + * Only makes sense to check opaque dir if this is not the 381 + * lowermost layer. 382 + */ 383 + if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) 384 + opaque = true; 385 + 386 + if (prev && (!S_ISDIR(prev->d_inode->i_mode) || 387 + !S_ISDIR(this->d_inode->i_mode))) { 388 + /* 389 + * FIXME: check for upper-opaqueness maybe better done 390 + * in remove code. 391 + */ 392 + if (prev == upperdentry) 393 + upperopaque = true; 394 + dput(this); 395 + break; 396 + } 397 + /* 398 + * If this is a non-directory then stop here. 399 + */ 400 + if (!S_ISDIR(this->d_inode->i_mode)) 401 + opaque = true; 402 + 403 + stack[ctr].dentry = this; 404 + stack[ctr].mnt = lowerpath.mnt; 405 + ctr++; 406 + prev = this; 407 + if (opaque) 408 + break; 409 + } 410 + 411 + oe = ovl_alloc_entry(ctr); 412 + err = -ENOMEM; 413 + if (!oe) 414 + goto out_put; 415 + 416 + if (upperdentry || ctr) { 361 417 struct dentry *realdentry; 362 418 363 - realdentry = upperdentry ? upperdentry : lowerdentry; 419 + realdentry = upperdentry ? upperdentry : stack[0].dentry; 420 + 364 421 err = -ENOMEM; 365 422 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, 366 423 oe); 367 424 if (!inode) 368 - goto out_dput; 425 + goto out_free_oe; 369 426 ovl_copyattr(realdentry->d_inode, inode); 370 427 } 371 428 429 + oe->opaque = upperopaque; 372 430 oe->__upperdentry = upperdentry; 373 - oe->lowerdentry = lowerdentry; 374 - 431 + memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); 432 + kfree(stack); 375 433 dentry->d_fsdata = oe; 376 434 d_add(dentry, inode); 377 435 378 436 return NULL; 379 437 380 - out_dput: 381 - dput(lowerdentry); 382 - out_dput_upper: 383 - dput(upperdentry); 384 - out_put_dir: 438 + out_free_oe: 385 439 kfree(oe); 440 + out_put: 441 + for (i = 0; i < ctr; i++) 442 + dput(stack[i].dentry); 443 + kfree(stack); 444 + out_put_upper: 445 + dput(upperdentry); 386 446 out: 387 447 return ERR_PTR(err); 388 448 } ··· 467 383 static void ovl_put_super(struct super_block *sb) 468 384 { 469 385 struct ovl_fs *ufs = sb->s_fs_info; 386 + unsigned i; 470 387 471 388 dput(ufs->workdir); 472 389 mntput(ufs->upper_mnt); 473 - mntput(ufs->lower_mnt); 390 + for (i = 0; i < ufs->numlower; i++) 391 + mntput(ufs->lower_mnt[i]); 474 392 475 393 kfree(ufs->config.lowerdir); 476 394 kfree(ufs->config.upperdir); ··· 486 400 * @buf: The struct kstatfs to fill in with stats 487 401 * 488 402 * Get the filesystem statistics. As writes always target the upper layer 489 - * filesystem pass the statfs to the same filesystem. 403 + * filesystem pass the statfs to the upper filesystem (if it exists) 490 404 */ 491 405 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 492 406 { ··· 495 409 struct path path; 496 410 int err; 497 411 498 - ovl_path_upper(root_dentry, &path); 412 + ovl_path_real(root_dentry, &path); 499 413 500 414 err = vfs_statfs(&path, buf); 501 415 if (!err) { ··· 518 432 struct ovl_fs *ufs = sb->s_fs_info; 519 433 520 434 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); 521 - seq_printf(m, ",upperdir=%s", ufs->config.upperdir); 522 - seq_printf(m, ",workdir=%s", ufs->config.workdir); 435 + if (ufs->config.upperdir) { 436 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir); 437 + seq_printf(m, ",workdir=%s", ufs->config.workdir); 438 + } 439 + return 0; 440 + } 441 + 442 + static int ovl_remount(struct super_block *sb, int *flags, char *data) 443 + { 444 + struct ovl_fs *ufs = sb->s_fs_info; 445 + 446 + if (!(*flags & MS_RDONLY) && 447 + (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))) 448 + return -EROFS; 449 + 523 450 return 0; 524 451 } 525 452 ··· 540 441 .put_super = ovl_put_super, 541 442 .statfs = ovl_statfs, 542 443 .show_options = ovl_show_options, 444 + .remount_fs = ovl_remount, 543 445 }; 544 446 545 447 enum { ··· 685 585 } 686 586 } 687 587 688 - static int ovl_mount_dir(const char *name, struct path *path) 689 - { 690 - int err; 691 - char *tmp = kstrdup(name, GFP_KERNEL); 692 - 693 - if (!tmp) 694 - return -ENOMEM; 695 - 696 - ovl_unescape(tmp); 697 - err = kern_path(tmp, LOOKUP_FOLLOW, path); 698 - if (err) { 699 - pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err); 700 - err = -EINVAL; 701 - } 702 - kfree(tmp); 703 - return err; 704 - } 705 - 706 588 static bool ovl_is_allowed_fs_type(struct dentry *root) 707 589 { 708 590 const struct dentry_operations *dop = root->d_op; ··· 704 622 return true; 705 623 } 706 624 625 + static int ovl_mount_dir_noesc(const char *name, struct path *path) 626 + { 627 + int err = -EINVAL; 628 + 629 + if (!*name) { 630 + pr_err("overlayfs: empty lowerdir\n"); 631 + goto out; 632 + } 633 + err = kern_path(name, LOOKUP_FOLLOW, path); 634 + if (err) { 635 + pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); 636 + goto out; 637 + } 638 + err = -EINVAL; 639 + if (!ovl_is_allowed_fs_type(path->dentry)) { 640 + pr_err("overlayfs: filesystem on '%s' not supported\n", name); 641 + goto out_put; 642 + } 643 + if (!S_ISDIR(path->dentry->d_inode->i_mode)) { 644 + pr_err("overlayfs: '%s' not a directory\n", name); 645 + goto out_put; 646 + } 647 + return 0; 648 + 649 + out_put: 650 + path_put(path); 651 + out: 652 + return err; 653 + } 654 + 655 + static int ovl_mount_dir(const char *name, struct path *path) 656 + { 657 + int err = -ENOMEM; 658 + char *tmp = kstrdup(name, GFP_KERNEL); 659 + 660 + if (tmp) { 661 + ovl_unescape(tmp); 662 + err = ovl_mount_dir_noesc(tmp, path); 663 + kfree(tmp); 664 + } 665 + return err; 666 + } 667 + 668 + static int ovl_lower_dir(const char *name, struct path *path, long *namelen, 669 + int *stack_depth) 670 + { 671 + int err; 672 + struct kstatfs statfs; 673 + 674 + err = ovl_mount_dir_noesc(name, path); 675 + if (err) 676 + goto out; 677 + 678 + err = vfs_statfs(path, &statfs); 679 + if (err) { 680 + pr_err("overlayfs: statfs failed on '%s'\n", name); 681 + goto out_put; 682 + } 683 + *namelen = max(*namelen, statfs.f_namelen); 684 + *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); 685 + 686 + return 0; 687 + 688 + out_put: 689 + path_put(path); 690 + out: 691 + return err; 692 + } 693 + 707 694 /* Workdir should not be subdir of upperdir and vice versa */ 708 695 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 709 696 { ··· 785 634 return ok; 786 635 } 787 636 637 + static unsigned int ovl_split_lowerdirs(char *str) 638 + { 639 + unsigned int ctr = 1; 640 + char *s, *d; 641 + 642 + for (s = d = str;; s++, d++) { 643 + if (*s == '\\') { 644 + s++; 645 + } else if (*s == ':') { 646 + *d = '\0'; 647 + ctr++; 648 + continue; 649 + } 650 + *d = *s; 651 + if (!*s) 652 + break; 653 + } 654 + return ctr; 655 + } 656 + 788 657 static int ovl_fill_super(struct super_block *sb, void *data, int silent) 789 658 { 790 - struct path lowerpath; 791 - struct path upperpath; 792 - struct path workpath; 793 - struct inode *root_inode; 659 + struct path upperpath = { NULL, NULL }; 660 + struct path workpath = { NULL, NULL }; 794 661 struct dentry *root_dentry; 795 662 struct ovl_entry *oe; 796 663 struct ovl_fs *ufs; 797 - struct kstatfs statfs; 664 + struct path *stack = NULL; 665 + char *lowertmp; 666 + char *lower; 667 + unsigned int numlower; 668 + unsigned int stacklen = 0; 669 + unsigned int i; 798 670 int err; 799 671 800 672 err = -ENOMEM; ··· 829 655 if (err) 830 656 goto out_free_config; 831 657 832 - /* FIXME: workdir is not needed for a R/O mount */ 833 658 err = -EINVAL; 834 - if (!ufs->config.upperdir || !ufs->config.lowerdir || 835 - !ufs->config.workdir) { 836 - pr_err("overlayfs: missing upperdir or lowerdir or workdir\n"); 659 + if (!ufs->config.lowerdir) { 660 + pr_err("overlayfs: missing 'lowerdir'\n"); 837 661 goto out_free_config; 662 + } 663 + 664 + sb->s_stack_depth = 0; 665 + if (ufs->config.upperdir) { 666 + /* FIXME: workdir is not needed for a R/O mount */ 667 + if (!ufs->config.workdir) { 668 + pr_err("overlayfs: missing 'workdir'\n"); 669 + goto out_free_config; 670 + } 671 + 672 + err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 673 + if (err) 674 + goto out_free_config; 675 + 676 + err = ovl_mount_dir(ufs->config.workdir, &workpath); 677 + if (err) 678 + goto out_put_upperpath; 679 + 680 + err = -EINVAL; 681 + if (upperpath.mnt != workpath.mnt) { 682 + pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 683 + goto out_put_workpath; 684 + } 685 + if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 686 + pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 687 + goto out_put_workpath; 688 + } 689 + sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; 690 + } 691 + err = -ENOMEM; 692 + lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); 693 + if (!lowertmp) 694 + goto out_put_workpath; 695 + 696 + err = -EINVAL; 697 + stacklen = ovl_split_lowerdirs(lowertmp); 698 + if (stacklen > OVL_MAX_STACK) 699 + goto out_free_lowertmp; 700 + 701 + stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); 702 + if (!stack) 703 + goto out_free_lowertmp; 704 + 705 + lower = lowertmp; 706 + for (numlower = 0; numlower < stacklen; numlower++) { 707 + err = ovl_lower_dir(lower, &stack[numlower], 708 + &ufs->lower_namelen, &sb->s_stack_depth); 709 + if (err) 710 + goto out_put_lowerpath; 711 + 712 + lower = strchr(lower, '\0') + 1; 713 + } 714 + 715 + err = -EINVAL; 716 + sb->s_stack_depth++; 717 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 718 + pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 719 + goto out_put_lowerpath; 720 + } 721 + 722 + if (ufs->config.upperdir) { 723 + ufs->upper_mnt = clone_private_mount(&upperpath); 724 + err = PTR_ERR(ufs->upper_mnt); 725 + if (IS_ERR(ufs->upper_mnt)) { 726 + pr_err("overlayfs: failed to clone upperpath\n"); 727 + goto out_put_lowerpath; 728 + } 729 + 730 + ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); 731 + err = PTR_ERR(ufs->workdir); 732 + if (IS_ERR(ufs->workdir)) { 733 + pr_err("overlayfs: failed to create directory %s/%s\n", 734 + ufs->config.workdir, OVL_WORKDIR_NAME); 735 + goto out_put_upper_mnt; 736 + } 838 737 } 839 738 840 739 err = -ENOMEM; 841 - oe = ovl_alloc_entry(); 842 - if (oe == NULL) 843 - goto out_free_config; 740 + ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); 741 + if (ufs->lower_mnt == NULL) 742 + goto out_put_workdir; 743 + for (i = 0; i < numlower; i++) { 744 + struct vfsmount *mnt = clone_private_mount(&stack[i]); 844 745 845 - err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 846 - if (err) 847 - goto out_free_oe; 746 + err = PTR_ERR(mnt); 747 + if (IS_ERR(mnt)) { 748 + pr_err("overlayfs: failed to clone lowerpath\n"); 749 + goto out_put_lower_mnt; 750 + } 751 + /* 752 + * Make lower_mnt R/O. That way fchmod/fchown on lower file 753 + * will fail instead of modifying lower fs. 754 + */ 755 + mnt->mnt_flags |= MNT_READONLY; 848 756 849 - err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath); 850 - if (err) 851 - goto out_put_upperpath; 852 - 853 - err = ovl_mount_dir(ufs->config.workdir, &workpath); 854 - if (err) 855 - goto out_put_lowerpath; 856 - 857 - err = -EINVAL; 858 - if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || 859 - !S_ISDIR(lowerpath.dentry->d_inode->i_mode) || 860 - !S_ISDIR(workpath.dentry->d_inode->i_mode)) { 861 - pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n"); 862 - goto out_put_workpath; 757 + ufs->lower_mnt[ufs->numlower] = mnt; 758 + ufs->numlower++; 863 759 } 864 760 865 - if (upperpath.mnt != workpath.mnt) { 866 - pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 867 - goto out_put_workpath; 868 - } 869 - if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 870 - pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 871 - goto out_put_workpath; 872 - } 873 - 874 - if (!ovl_is_allowed_fs_type(upperpath.dentry)) { 875 - pr_err("overlayfs: filesystem of upperdir is not supported\n"); 876 - goto out_put_workpath; 877 - } 878 - 879 - if (!ovl_is_allowed_fs_type(lowerpath.dentry)) { 880 - pr_err("overlayfs: filesystem of lowerdir is not supported\n"); 881 - goto out_put_workpath; 882 - } 883 - 884 - err = vfs_statfs(&lowerpath, &statfs); 885 - if (err) { 886 - pr_err("overlayfs: statfs failed on lowerpath\n"); 887 - goto out_put_workpath; 888 - } 889 - ufs->lower_namelen = statfs.f_namelen; 890 - 891 - sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth, 892 - lowerpath.mnt->mnt_sb->s_stack_depth) + 1; 893 - 894 - err = -EINVAL; 895 - if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 896 - pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 897 - goto out_put_workpath; 898 - } 899 - 900 - ufs->upper_mnt = clone_private_mount(&upperpath); 901 - err = PTR_ERR(ufs->upper_mnt); 902 - if (IS_ERR(ufs->upper_mnt)) { 903 - pr_err("overlayfs: failed to clone upperpath\n"); 904 - goto out_put_workpath; 905 - } 906 - 907 - ufs->lower_mnt = clone_private_mount(&lowerpath); 908 - err = PTR_ERR(ufs->lower_mnt); 909 - if (IS_ERR(ufs->lower_mnt)) { 910 - pr_err("overlayfs: failed to clone lowerpath\n"); 911 - goto out_put_upper_mnt; 912 - } 913 - 914 - ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); 915 - err = PTR_ERR(ufs->workdir); 916 - if (IS_ERR(ufs->workdir)) { 917 - pr_err("overlayfs: failed to create directory %s/%s\n", 918 - ufs->config.workdir, OVL_WORKDIR_NAME); 919 - goto out_put_lower_mnt; 920 - } 921 - 922 - /* 923 - * Make lower_mnt R/O. That way fchmod/fchown on lower file 924 - * will fail instead of modifying lower fs. 925 - */ 926 - ufs->lower_mnt->mnt_flags |= MNT_READONLY; 927 - 928 - /* If the upper fs is r/o, we mark overlayfs r/o too */ 929 - if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY) 761 + /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */ 762 + if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)) 930 763 sb->s_flags |= MS_RDONLY; 931 764 932 765 sb->s_d_op = &ovl_dentry_operations; 933 766 934 767 err = -ENOMEM; 935 - root_inode = ovl_new_inode(sb, S_IFDIR, oe); 936 - if (!root_inode) 937 - goto out_put_workdir; 768 + oe = ovl_alloc_entry(numlower); 769 + if (!oe) 770 + goto out_put_lower_mnt; 938 771 939 - root_dentry = d_make_root(root_inode); 772 + root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); 940 773 if (!root_dentry) 941 - goto out_put_workdir; 774 + goto out_free_oe; 942 775 943 776 mntput(upperpath.mnt); 944 - mntput(lowerpath.mnt); 777 + for (i = 0; i < numlower; i++) 778 + mntput(stack[i].mnt); 945 779 path_put(&workpath); 780 + kfree(lowertmp); 946 781 947 782 oe->__upperdentry = upperpath.dentry; 948 - oe->lowerdentry = lowerpath.dentry; 783 + for (i = 0; i < numlower; i++) { 784 + oe->lowerstack[i].dentry = stack[i].dentry; 785 + oe->lowerstack[i].mnt = ufs->lower_mnt[i]; 786 + } 949 787 950 788 root_dentry->d_fsdata = oe; 951 789 ··· 968 782 969 783 return 0; 970 784 971 - out_put_workdir: 972 - dput(ufs->workdir); 973 - out_put_lower_mnt: 974 - mntput(ufs->lower_mnt); 975 - out_put_upper_mnt: 976 - mntput(ufs->upper_mnt); 977 - out_put_workpath: 978 - path_put(&workpath); 979 - out_put_lowerpath: 980 - path_put(&lowerpath); 981 - out_put_upperpath: 982 - path_put(&upperpath); 983 785 out_free_oe: 984 786 kfree(oe); 787 + out_put_lower_mnt: 788 + for (i = 0; i < ufs->numlower; i++) 789 + mntput(ufs->lower_mnt[i]); 790 + kfree(ufs->lower_mnt); 791 + out_put_workdir: 792 + dput(ufs->workdir); 793 + out_put_upper_mnt: 794 + mntput(ufs->upper_mnt); 795 + out_put_lowerpath: 796 + for (i = 0; i < numlower; i++) 797 + path_put(&stack[i]); 798 + kfree(stack); 799 + out_free_lowertmp: 800 + kfree(lowertmp); 801 + out_put_workpath: 802 + path_put(&workpath); 803 + out_put_upperpath: 804 + path_put(&upperpath); 985 805 out_free_config: 986 806 kfree(ufs->config.lowerdir); 987 807 kfree(ufs->config.upperdir);