aboutsummaryrefslogtreecommitdiffstats
path: root/fs/overlayfs/super.c
diff options
context:
space:
mode:
authorAl Viro <viro@zeniv.linux.org.uk>2015-02-20 04:58:52 -0500
committerAl Viro <viro@zeniv.linux.org.uk>2015-02-20 04:58:52 -0500
commitce7b9facdf43b42fb95bdff9069aefeddb7d0a69 (patch)
treedb49cfb72630e88172b51e586acfa9ddb22b5dc1 /fs/overlayfs/super.c
parenta95104fd3393080e8bcca348f51996f5f0f5ccb6 (diff)
parent4330397e4e8a662f36d101659e2a59ce32e76ff4 (diff)
Merge branch 'overlayfs-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs into for-next
Diffstat (limited to 'fs/overlayfs/super.c')
-rw-r--r--fs/overlayfs/super.c564
1 files changed, 378 insertions, 186 deletions
diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
index f16d318b71f8..b90952f528b1 100644
--- a/fs/overlayfs/super.c
+++ b/fs/overlayfs/super.c
@@ -35,7 +35,8 @@ struct ovl_config {
35/* private information held for overlayfs's superblock */ 35/* private information held for overlayfs's superblock */
36struct ovl_fs { 36struct ovl_fs {
37 struct vfsmount *upper_mnt; 37 struct vfsmount *upper_mnt;
38 struct vfsmount *lower_mnt; 38 unsigned numlower;
39 struct vfsmount **lower_mnt;
39 struct dentry *workdir; 40 struct dentry *workdir;
40 long lower_namelen; 41 long lower_namelen;
41 /* pathnames of lower and upper dirs, for show_options */ 42 /* pathnames of lower and upper dirs, for show_options */
@@ -47,7 +48,6 @@ struct ovl_dir_cache;
47/* private information held for every overlayfs dentry */ 48/* private information held for every overlayfs dentry */
48struct ovl_entry { 49struct ovl_entry {
49 struct dentry *__upperdentry; 50 struct dentry *__upperdentry;
50 struct dentry *lowerdentry;
51 struct ovl_dir_cache *cache; 51 struct ovl_dir_cache *cache;
52 union { 52 union {
53 struct { 53 struct {
@@ -56,30 +56,36 @@ struct ovl_entry {
56 }; 56 };
57 struct rcu_head rcu; 57 struct rcu_head rcu;
58 }; 58 };
59 unsigned numlower;
60 struct path lowerstack[];
59}; 61};
60 62
61const char *ovl_opaque_xattr = "trusted.overlay.opaque"; 63#define OVL_MAX_STACK 500
62 64
65static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
66{
67 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
68}
63 69
64enum ovl_path_type ovl_path_type(struct dentry *dentry) 70enum ovl_path_type ovl_path_type(struct dentry *dentry)
65{ 71{
66 struct ovl_entry *oe = dentry->d_fsdata; 72 struct ovl_entry *oe = dentry->d_fsdata;
73 enum ovl_path_type type = 0;
67 74
68 if (oe->__upperdentry) { 75 if (oe->__upperdentry) {
69 if (oe->lowerdentry) { 76 type = __OVL_PATH_UPPER;
77
78 if (oe->numlower) {
70 if (S_ISDIR(dentry->d_inode->i_mode)) 79 if (S_ISDIR(dentry->d_inode->i_mode))
71 return OVL_PATH_MERGE; 80 type |= __OVL_PATH_MERGE;
72 else 81 } else if (!oe->opaque) {
73 return OVL_PATH_UPPER; 82 type |= __OVL_PATH_PURE;
74 } else {
75 if (oe->opaque)
76 return OVL_PATH_UPPER;
77 else
78 return OVL_PATH_PURE_UPPER;
79 } 83 }
80 } else { 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
85static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) 91static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
@@ -98,10 +104,9 @@ void ovl_path_upper(struct dentry *dentry, struct path *path)
98 104
99enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 105enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
100{ 106{
101
102 enum ovl_path_type type = ovl_path_type(dentry); 107 enum ovl_path_type type = ovl_path_type(dentry);
103 108
104 if (type == OVL_PATH_LOWER) 109 if (!OVL_TYPE_UPPER(type))
105 ovl_path_lower(dentry, path); 110 ovl_path_lower(dentry, path);
106 else 111 else
107 ovl_path_upper(dentry, path); 112 ovl_path_upper(dentry, path);
@@ -120,7 +125,7 @@ struct dentry *ovl_dentry_lower(struct dentry *dentry)
120{ 125{
121 struct ovl_entry *oe = dentry->d_fsdata; 126 struct ovl_entry *oe = dentry->d_fsdata;
122 127
123 return oe->lowerdentry; 128 return __ovl_dentry_lower(oe);
124} 129}
125 130
126struct dentry *ovl_dentry_real(struct dentry *dentry) 131struct dentry *ovl_dentry_real(struct dentry *dentry)
@@ -130,7 +135,7 @@ struct dentry *ovl_dentry_real(struct dentry *dentry)
130 135
131 realdentry = ovl_upperdentry_dereference(oe); 136 realdentry = ovl_upperdentry_dereference(oe);
132 if (!realdentry) 137 if (!realdentry)
133 realdentry = oe->lowerdentry; 138 realdentry = __ovl_dentry_lower(oe);
134 139
135 return realdentry; 140 return realdentry;
136} 141}
@@ -143,7 +148,7 @@ struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
143 if (realdentry) { 148 if (realdentry) {
144 *is_upper = true; 149 *is_upper = true;
145 } else { 150 } else {
146 realdentry = oe->lowerdentry; 151 realdentry = __ovl_dentry_lower(oe);
147 *is_upper = false; 152 *is_upper = false;
148 } 153 }
149 return realdentry; 154 return realdentry;
@@ -165,11 +170,9 @@ void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
165 170
166void ovl_path_lower(struct dentry *dentry, struct path *path) 171void ovl_path_lower(struct dentry *dentry, struct path *path)
167{ 172{
168 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
169 struct ovl_entry *oe = dentry->d_fsdata; 173 struct ovl_entry *oe = dentry->d_fsdata;
170 174
171 path->mnt = ofs->lower_mnt; 175 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
172 path->dentry = oe->lowerdentry;
173} 176}
174 177
175int ovl_want_write(struct dentry *dentry) 178int ovl_want_write(struct dentry *dentry)
@@ -249,7 +252,7 @@ static bool ovl_is_opaquedir(struct dentry *dentry)
249 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) 252 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
250 return false; 253 return false;
251 254
252 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); 255 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
253 if (res == 1 && val == 'y') 256 if (res == 1 && val == 'y')
254 return true; 257 return true;
255 258
@@ -261,8 +264,11 @@ static void ovl_dentry_release(struct dentry *dentry)
261 struct ovl_entry *oe = dentry->d_fsdata; 264 struct ovl_entry *oe = dentry->d_fsdata;
262 265
263 if (oe) { 266 if (oe) {
267 unsigned int i;
268
264 dput(oe->__upperdentry); 269 dput(oe->__upperdentry);
265 dput(oe->lowerdentry); 270 for (i = 0; i < oe->numlower; i++)
271 dput(oe->lowerstack[i].dentry);
266 kfree_rcu(oe, rcu); 272 kfree_rcu(oe, rcu);
267 } 273 }
268} 274}
@@ -271,9 +277,15 @@ static const struct dentry_operations ovl_dentry_operations = {
271 .d_release = ovl_dentry_release, 277 .d_release = ovl_dentry_release,
272}; 278};
273 279
274static struct ovl_entry *ovl_alloc_entry(void) 280static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
275{ 281{
276 return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL); 282 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
283 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
284
285 if (oe)
286 oe->numlower = numlower;
287
288 return oe;
277} 289}
278 290
279static inline struct dentry *ovl_lookup_real(struct dentry *dir, 291static inline struct dentry *ovl_lookup_real(struct dentry *dir,
@@ -295,82 +307,154 @@ static inline struct dentry *ovl_lookup_real(struct dentry *dir,
295 return dentry; 307 return dentry;
296} 308}
297 309
310/*
311 * Returns next layer in stack starting from top.
312 * Returns -1 if this is the last layer.
313 */
314int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
315{
316 struct ovl_entry *oe = dentry->d_fsdata;
317
318 BUG_ON(idx < 0);
319 if (idx == 0) {
320 ovl_path_upper(dentry, path);
321 if (path->dentry)
322 return oe->numlower ? 1 : -1;
323 idx++;
324 }
325 BUG_ON(idx > oe->numlower);
326 *path = oe->lowerstack[idx - 1];
327
328 return (idx < oe->numlower) ? idx + 1 : -1;
329}
330
298struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 331struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
299 unsigned int flags) 332 unsigned int flags)
300{ 333{
301 struct ovl_entry *oe; 334 struct ovl_entry *oe;
302 struct dentry *upperdir; 335 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
303 struct dentry *lowerdir; 336 struct path *stack = NULL;
304 struct dentry *upperdentry = NULL; 337 struct dentry *upperdir, *upperdentry = NULL;
305 struct dentry *lowerdentry = NULL; 338 unsigned int ctr = 0;
306 struct inode *inode = NULL; 339 struct inode *inode = NULL;
340 bool upperopaque = false;
341 struct dentry *this, *prev = NULL;
342 unsigned int i;
307 int err; 343 int err;
308 344
309 err = -ENOMEM; 345 upperdir = ovl_upperdentry_dereference(poe);
310 oe = ovl_alloc_entry();
311 if (!oe)
312 goto out;
313
314 upperdir = ovl_dentry_upper(dentry->d_parent);
315 lowerdir = ovl_dentry_lower(dentry->d_parent);
316
317 if (upperdir) { 346 if (upperdir) {
318 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); 347 this = ovl_lookup_real(upperdir, &dentry->d_name);
319 err = PTR_ERR(upperdentry); 348 err = PTR_ERR(this);
320 if (IS_ERR(upperdentry)) 349 if (IS_ERR(this))
321 goto out_put_dir; 350 goto out;
322 351
323 if (lowerdir && upperdentry) { 352 if (this) {
324 if (ovl_is_whiteout(upperdentry)) { 353 if (ovl_is_whiteout(this)) {
325 dput(upperdentry); 354 dput(this);
326 upperdentry = NULL; 355 this = NULL;
327 oe->opaque = true; 356 upperopaque = true;
328 } else if (ovl_is_opaquedir(upperdentry)) { 357 } else if (poe->numlower && ovl_is_opaquedir(this)) {
329 oe->opaque = true; 358 upperopaque = true;
330 } 359 }
331 } 360 }
361 upperdentry = prev = this;
332 } 362 }
333 if (lowerdir && !oe->opaque) { 363
334 lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name); 364 if (!upperopaque && poe->numlower) {
335 err = PTR_ERR(lowerdentry); 365 err = -ENOMEM;
336 if (IS_ERR(lowerdentry)) 366 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
337 goto out_dput_upper; 367 if (!stack)
368 goto out_put_upper;
338 } 369 }
339 370
340 if (lowerdentry && upperdentry && 371 for (i = 0; !upperopaque && i < poe->numlower; i++) {
341 (!S_ISDIR(upperdentry->d_inode->i_mode) || 372 bool opaque = false;
342 !S_ISDIR(lowerdentry->d_inode->i_mode))) { 373 struct path lowerpath = poe->lowerstack[i];
343 dput(lowerdentry); 374
344 lowerdentry = NULL; 375 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
345 oe->opaque = true; 376 err = PTR_ERR(this);
377 if (IS_ERR(this)) {
378 /*
379 * If it's positive, then treat ENAMETOOLONG as ENOENT.
380 */
381 if (err == -ENAMETOOLONG && (upperdentry || ctr))
382 continue;
383 goto out_put;
384 }
385 if (!this)
386 continue;
387 if (ovl_is_whiteout(this)) {
388 dput(this);
389 break;
390 }
391 /*
392 * Only makes sense to check opaque dir if this is not the
393 * lowermost layer.
394 */
395 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
396 opaque = true;
397
398 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
399 !S_ISDIR(this->d_inode->i_mode))) {
400 /*
401 * FIXME: check for upper-opaqueness maybe better done
402 * in remove code.
403 */
404 if (prev == upperdentry)
405 upperopaque = true;
406 dput(this);
407 break;
408 }
409 /*
410 * If this is a non-directory then stop here.
411 */
412 if (!S_ISDIR(this->d_inode->i_mode))
413 opaque = true;
414
415 stack[ctr].dentry = this;
416 stack[ctr].mnt = lowerpath.mnt;
417 ctr++;
418 prev = this;
419 if (opaque)
420 break;
346 } 421 }
347 422
348 if (lowerdentry || upperdentry) { 423 oe = ovl_alloc_entry(ctr);
424 err = -ENOMEM;
425 if (!oe)
426 goto out_put;
427
428 if (upperdentry || ctr) {
349 struct dentry *realdentry; 429 struct dentry *realdentry;
350 430
351 realdentry = upperdentry ? upperdentry : lowerdentry; 431 realdentry = upperdentry ? upperdentry : stack[0].dentry;
432
352 err = -ENOMEM; 433 err = -ENOMEM;
353 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, 434 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
354 oe); 435 oe);
355 if (!inode) 436 if (!inode)
356 goto out_dput; 437 goto out_free_oe;
357 ovl_copyattr(realdentry->d_inode, inode); 438 ovl_copyattr(realdentry->d_inode, inode);
358 } 439 }
359 440
441 oe->opaque = upperopaque;
360 oe->__upperdentry = upperdentry; 442 oe->__upperdentry = upperdentry;
361 oe->lowerdentry = lowerdentry; 443 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
362 444 kfree(stack);
363 dentry->d_fsdata = oe; 445 dentry->d_fsdata = oe;
364 d_add(dentry, inode); 446 d_add(dentry, inode);
365 447
366 return NULL; 448 return NULL;
367 449
368out_dput: 450out_free_oe:
369 dput(lowerdentry);
370out_dput_upper:
371 dput(upperdentry);
372out_put_dir:
373 kfree(oe); 451 kfree(oe);
452out_put:
453 for (i = 0; i < ctr; i++)
454 dput(stack[i].dentry);
455 kfree(stack);
456out_put_upper:
457 dput(upperdentry);
374out: 458out:
375 return ERR_PTR(err); 459 return ERR_PTR(err);
376} 460}
@@ -383,10 +467,12 @@ struct file *ovl_path_open(struct path *path, int flags)
383static void ovl_put_super(struct super_block *sb) 467static void ovl_put_super(struct super_block *sb)
384{ 468{
385 struct ovl_fs *ufs = sb->s_fs_info; 469 struct ovl_fs *ufs = sb->s_fs_info;
470 unsigned i;
386 471
387 dput(ufs->workdir); 472 dput(ufs->workdir);
388 mntput(ufs->upper_mnt); 473 mntput(ufs->upper_mnt);
389 mntput(ufs->lower_mnt); 474 for (i = 0; i < ufs->numlower; i++)
475 mntput(ufs->lower_mnt[i]);
390 476
391 kfree(ufs->config.lowerdir); 477 kfree(ufs->config.lowerdir);
392 kfree(ufs->config.upperdir); 478 kfree(ufs->config.upperdir);
@@ -400,7 +486,7 @@ static void ovl_put_super(struct super_block *sb)
400 * @buf: The struct kstatfs to fill in with stats 486 * @buf: The struct kstatfs to fill in with stats
401 * 487 *
402 * Get the filesystem statistics. As writes always target the upper layer 488 * Get the filesystem statistics. As writes always target the upper layer
403 * filesystem pass the statfs to the same filesystem. 489 * filesystem pass the statfs to the upper filesystem (if it exists)
404 */ 490 */
405static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 491static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
406{ 492{
@@ -409,7 +495,7 @@ static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
409 struct path path; 495 struct path path;
410 int err; 496 int err;
411 497
412 ovl_path_upper(root_dentry, &path); 498 ovl_path_real(root_dentry, &path);
413 499
414 err = vfs_statfs(&path, buf); 500 err = vfs_statfs(&path, buf);
415 if (!err) { 501 if (!err) {
@@ -432,8 +518,21 @@ static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
432 struct ovl_fs *ufs = sb->s_fs_info; 518 struct ovl_fs *ufs = sb->s_fs_info;
433 519
434 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); 520 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
435 seq_printf(m, ",upperdir=%s", ufs->config.upperdir); 521 if (ufs->config.upperdir) {
436 seq_printf(m, ",workdir=%s", ufs->config.workdir); 522 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
523 seq_printf(m, ",workdir=%s", ufs->config.workdir);
524 }
525 return 0;
526}
527
528static int ovl_remount(struct super_block *sb, int *flags, char *data)
529{
530 struct ovl_fs *ufs = sb->s_fs_info;
531
532 if (!(*flags & MS_RDONLY) &&
533 (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)))
534 return -EROFS;
535
437 return 0; 536 return 0;
438} 537}
439 538
@@ -441,6 +540,7 @@ static const struct super_operations ovl_super_operations = {
441 .put_super = ovl_put_super, 540 .put_super = ovl_put_super,
442 .statfs = ovl_statfs, 541 .statfs = ovl_statfs,
443 .show_options = ovl_show_options, 542 .show_options = ovl_show_options,
543 .remount_fs = ovl_remount,
444}; 544};
445 545
446enum { 546enum {
@@ -585,24 +685,6 @@ static void ovl_unescape(char *s)
585 } 685 }
586} 686}
587 687
588static int ovl_mount_dir(const char *name, struct path *path)
589{
590 int err;
591 char *tmp = kstrdup(name, GFP_KERNEL);
592
593 if (!tmp)
594 return -ENOMEM;
595
596 ovl_unescape(tmp);
597 err = kern_path(tmp, LOOKUP_FOLLOW, path);
598 if (err) {
599 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
600 err = -EINVAL;
601 }
602 kfree(tmp);
603 return err;
604}
605
606static bool ovl_is_allowed_fs_type(struct dentry *root) 688static bool ovl_is_allowed_fs_type(struct dentry *root)
607{ 689{
608 const struct dentry_operations *dop = root->d_op; 690 const struct dentry_operations *dop = root->d_op;
@@ -622,6 +704,75 @@ static bool ovl_is_allowed_fs_type(struct dentry *root)
622 return true; 704 return true;
623} 705}
624 706
707static int ovl_mount_dir_noesc(const char *name, struct path *path)
708{
709 int err = -EINVAL;
710
711 if (!*name) {
712 pr_err("overlayfs: empty lowerdir\n");
713 goto out;
714 }
715 err = kern_path(name, LOOKUP_FOLLOW, path);
716 if (err) {
717 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
718 goto out;
719 }
720 err = -EINVAL;
721 if (!ovl_is_allowed_fs_type(path->dentry)) {
722 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
723 goto out_put;
724 }
725 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
726 pr_err("overlayfs: '%s' not a directory\n", name);
727 goto out_put;
728 }
729 return 0;
730
731out_put:
732 path_put(path);
733out:
734 return err;
735}
736
737static int ovl_mount_dir(const char *name, struct path *path)
738{
739 int err = -ENOMEM;
740 char *tmp = kstrdup(name, GFP_KERNEL);
741
742 if (tmp) {
743 ovl_unescape(tmp);
744 err = ovl_mount_dir_noesc(tmp, path);
745 kfree(tmp);
746 }
747 return err;
748}
749
750static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
751 int *stack_depth)
752{
753 int err;
754 struct kstatfs statfs;
755
756 err = ovl_mount_dir_noesc(name, path);
757 if (err)
758 goto out;
759
760 err = vfs_statfs(path, &statfs);
761 if (err) {
762 pr_err("overlayfs: statfs failed on '%s'\n", name);
763 goto out_put;
764 }
765 *namelen = max(*namelen, statfs.f_namelen);
766 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
767
768 return 0;
769
770out_put:
771 path_put(path);
772out:
773 return err;
774}
775
625/* Workdir should not be subdir of upperdir and vice versa */ 776/* Workdir should not be subdir of upperdir and vice versa */
626static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 777static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
627{ 778{
@@ -634,16 +785,39 @@ static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
634 return ok; 785 return ok;
635} 786}
636 787
788static unsigned int ovl_split_lowerdirs(char *str)
789{
790 unsigned int ctr = 1;
791 char *s, *d;
792
793 for (s = d = str;; s++, d++) {
794 if (*s == '\\') {
795 s++;
796 } else if (*s == ':') {
797 *d = '\0';
798 ctr++;
799 continue;
800 }
801 *d = *s;
802 if (!*s)
803 break;
804 }
805 return ctr;
806}
807
637static int ovl_fill_super(struct super_block *sb, void *data, int silent) 808static int ovl_fill_super(struct super_block *sb, void *data, int silent)
638{ 809{
639 struct path lowerpath; 810 struct path upperpath = { NULL, NULL };
640 struct path upperpath; 811 struct path workpath = { NULL, NULL };
641 struct path workpath;
642 struct inode *root_inode;
643 struct dentry *root_dentry; 812 struct dentry *root_dentry;
644 struct ovl_entry *oe; 813 struct ovl_entry *oe;
645 struct ovl_fs *ufs; 814 struct ovl_fs *ufs;
646 struct kstatfs statfs; 815 struct path *stack = NULL;
816 char *lowertmp;
817 char *lower;
818 unsigned int numlower;
819 unsigned int stacklen = 0;
820 unsigned int i;
647 int err; 821 int err;
648 822
649 err = -ENOMEM; 823 err = -ENOMEM;
@@ -655,123 +829,135 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
655 if (err) 829 if (err)
656 goto out_free_config; 830 goto out_free_config;
657 831
658 /* FIXME: workdir is not needed for a R/O mount */
659 err = -EINVAL; 832 err = -EINVAL;
660 if (!ufs->config.upperdir || !ufs->config.lowerdir || 833 if (!ufs->config.lowerdir) {
661 !ufs->config.workdir) { 834 pr_err("overlayfs: missing 'lowerdir'\n");
662 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
663 goto out_free_config; 835 goto out_free_config;
664 } 836 }
665 837
666 err = -ENOMEM; 838 sb->s_stack_depth = 0;
667 oe = ovl_alloc_entry(); 839 if (ufs->config.upperdir) {
668 if (oe == NULL) 840 /* FIXME: workdir is not needed for a R/O mount */
669 goto out_free_config; 841 if (!ufs->config.workdir) {
670 842 pr_err("overlayfs: missing 'workdir'\n");
671 err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 843 goto out_free_config;
672 if (err) 844 }
673 goto out_free_oe;
674
675 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
676 if (err)
677 goto out_put_upperpath;
678 845
679 err = ovl_mount_dir(ufs->config.workdir, &workpath); 846 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
680 if (err) 847 if (err)
681 goto out_put_lowerpath; 848 goto out_free_config;
682 849
683 err = -EINVAL; 850 err = ovl_mount_dir(ufs->config.workdir, &workpath);
684 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || 851 if (err)
685 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) || 852 goto out_put_upperpath;
686 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
687 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
688 goto out_put_workpath;
689 }
690 853
691 if (upperpath.mnt != workpath.mnt) { 854 err = -EINVAL;
692 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 855 if (upperpath.mnt != workpath.mnt) {
693 goto out_put_workpath; 856 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
694 } 857 goto out_put_workpath;
695 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 858 }
696 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 859 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
697 goto out_put_workpath; 860 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
861 goto out_put_workpath;
862 }
863 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
698 } 864 }
699 865 err = -ENOMEM;
700 if (!ovl_is_allowed_fs_type(upperpath.dentry)) { 866 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
701 pr_err("overlayfs: filesystem of upperdir is not supported\n"); 867 if (!lowertmp)
702 goto out_put_workpath; 868 goto out_put_workpath;
703 }
704 869
705 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) { 870 err = -EINVAL;
706 pr_err("overlayfs: filesystem of lowerdir is not supported\n"); 871 stacklen = ovl_split_lowerdirs(lowertmp);
707 goto out_put_workpath; 872 if (stacklen > OVL_MAX_STACK)
708 } 873 goto out_free_lowertmp;
874
875 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
876 if (!stack)
877 goto out_free_lowertmp;
878
879 lower = lowertmp;
880 for (numlower = 0; numlower < stacklen; numlower++) {
881 err = ovl_lower_dir(lower, &stack[numlower],
882 &ufs->lower_namelen, &sb->s_stack_depth);
883 if (err)
884 goto out_put_lowerpath;
709 885
710 err = vfs_statfs(&lowerpath, &statfs); 886 lower = strchr(lower, '\0') + 1;
711 if (err) {
712 pr_err("overlayfs: statfs failed on lowerpath\n");
713 goto out_put_workpath;
714 } 887 }
715 ufs->lower_namelen = statfs.f_namelen;
716
717 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
718 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
719 888
720 err = -EINVAL; 889 err = -EINVAL;
890 sb->s_stack_depth++;
721 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 891 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
722 pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 892 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
723 goto out_put_workpath; 893 goto out_put_lowerpath;
724 } 894 }
725 895
726 ufs->upper_mnt = clone_private_mount(&upperpath); 896 if (ufs->config.upperdir) {
727 err = PTR_ERR(ufs->upper_mnt); 897 ufs->upper_mnt = clone_private_mount(&upperpath);
728 if (IS_ERR(ufs->upper_mnt)) { 898 err = PTR_ERR(ufs->upper_mnt);
729 pr_err("overlayfs: failed to clone upperpath\n"); 899 if (IS_ERR(ufs->upper_mnt)) {
730 goto out_put_workpath; 900 pr_err("overlayfs: failed to clone upperpath\n");
731 } 901 goto out_put_lowerpath;
902 }
732 903
733 ufs->lower_mnt = clone_private_mount(&lowerpath); 904 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
734 err = PTR_ERR(ufs->lower_mnt); 905 err = PTR_ERR(ufs->workdir);
735 if (IS_ERR(ufs->lower_mnt)) { 906 if (IS_ERR(ufs->workdir)) {
736 pr_err("overlayfs: failed to clone lowerpath\n"); 907 pr_err("overlayfs: failed to create directory %s/%s\n",
737 goto out_put_upper_mnt; 908 ufs->config.workdir, OVL_WORKDIR_NAME);
909 goto out_put_upper_mnt;
910 }
738 } 911 }
739 912
740 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); 913 err = -ENOMEM;
741 err = PTR_ERR(ufs->workdir); 914 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
742 if (IS_ERR(ufs->workdir)) { 915 if (ufs->lower_mnt == NULL)
743 pr_err("overlayfs: failed to create directory %s/%s\n", 916 goto out_put_workdir;
744 ufs->config.workdir, OVL_WORKDIR_NAME); 917 for (i = 0; i < numlower; i++) {
745 goto out_put_lower_mnt; 918 struct vfsmount *mnt = clone_private_mount(&stack[i]);
746 }
747 919
748 /* 920 err = PTR_ERR(mnt);
749 * Make lower_mnt R/O. That way fchmod/fchown on lower file 921 if (IS_ERR(mnt)) {
750 * will fail instead of modifying lower fs. 922 pr_err("overlayfs: failed to clone lowerpath\n");
751 */ 923 goto out_put_lower_mnt;
752 ufs->lower_mnt->mnt_flags |= MNT_READONLY; 924 }
925 /*
926 * Make lower_mnt R/O. That way fchmod/fchown on lower file
927 * will fail instead of modifying lower fs.
928 */
929 mnt->mnt_flags |= MNT_READONLY;
930
931 ufs->lower_mnt[ufs->numlower] = mnt;
932 ufs->numlower++;
933 }
753 934
754 /* If the upper fs is r/o, we mark overlayfs r/o too */ 935 /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
755 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY) 936 if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
756 sb->s_flags |= MS_RDONLY; 937 sb->s_flags |= MS_RDONLY;
757 938
758 sb->s_d_op = &ovl_dentry_operations; 939 sb->s_d_op = &ovl_dentry_operations;
759 940
760 err = -ENOMEM; 941 err = -ENOMEM;
761 root_inode = ovl_new_inode(sb, S_IFDIR, oe); 942 oe = ovl_alloc_entry(numlower);
762 if (!root_inode) 943 if (!oe)
763 goto out_put_workdir; 944 goto out_put_lower_mnt;
764 945
765 root_dentry = d_make_root(root_inode); 946 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
766 if (!root_dentry) 947 if (!root_dentry)
767 goto out_put_workdir; 948 goto out_free_oe;
768 949
769 mntput(upperpath.mnt); 950 mntput(upperpath.mnt);
770 mntput(lowerpath.mnt); 951 for (i = 0; i < numlower; i++)
952 mntput(stack[i].mnt);
771 path_put(&workpath); 953 path_put(&workpath);
954 kfree(lowertmp);
772 955
773 oe->__upperdentry = upperpath.dentry; 956 oe->__upperdentry = upperpath.dentry;
774 oe->lowerdentry = lowerpath.dentry; 957 for (i = 0; i < numlower; i++) {
958 oe->lowerstack[i].dentry = stack[i].dentry;
959 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
960 }
775 961
776 root_dentry->d_fsdata = oe; 962 root_dentry->d_fsdata = oe;
777 963
@@ -782,20 +968,26 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
782 968
783 return 0; 969 return 0;
784 970
971out_free_oe:
972 kfree(oe);
973out_put_lower_mnt:
974 for (i = 0; i < ufs->numlower; i++)
975 mntput(ufs->lower_mnt[i]);
976 kfree(ufs->lower_mnt);
785out_put_workdir: 977out_put_workdir:
786 dput(ufs->workdir); 978 dput(ufs->workdir);
787out_put_lower_mnt:
788 mntput(ufs->lower_mnt);
789out_put_upper_mnt: 979out_put_upper_mnt:
790 mntput(ufs->upper_mnt); 980 mntput(ufs->upper_mnt);
981out_put_lowerpath:
982 for (i = 0; i < numlower; i++)
983 path_put(&stack[i]);
984 kfree(stack);
985out_free_lowertmp:
986 kfree(lowertmp);
791out_put_workpath: 987out_put_workpath:
792 path_put(&workpath); 988 path_put(&workpath);
793out_put_lowerpath:
794 path_put(&lowerpath);
795out_put_upperpath: 989out_put_upperpath:
796 path_put(&upperpath); 990 path_put(&upperpath);
797out_free_oe:
798 kfree(oe);
799out_free_config: 991out_free_config:
800 kfree(ufs->config.lowerdir); 992 kfree(ufs->config.lowerdir);
801 kfree(ufs->config.upperdir); 993 kfree(ufs->config.upperdir);