ovl: fix working on distributed fs as lower layer
[cascardo/linux.git] / fs / overlayfs / super.c
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9
10 #include <linux/fs.h>
11 #include <linux/namei.h>
12 #include <linux/pagemap.h>
13 #include <linux/xattr.h>
14 #include <linux/security.h>
15 #include <linux/mount.h>
16 #include <linux/slab.h>
17 #include <linux/parser.h>
18 #include <linux/module.h>
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/statfs.h>
22 #include <linux/seq_file.h>
23 #include "overlayfs.h"
24
25 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26 MODULE_DESCRIPTION("Overlay filesystem");
27 MODULE_LICENSE("GPL");
28
29 struct ovl_config {
30         char *lowerdir;
31         char *upperdir;
32         char *workdir;
33         bool default_permissions;
34 };
35
36 /* private information held for overlayfs's superblock */
37 struct ovl_fs {
38         struct vfsmount *upper_mnt;
39         unsigned numlower;
40         struct vfsmount **lower_mnt;
41         struct dentry *workdir;
42         long lower_namelen;
43         /* pathnames of lower and upper dirs, for show_options */
44         struct ovl_config config;
45 };
46
47 struct ovl_dir_cache;
48
49 /* private information held for every overlayfs dentry */
50 struct ovl_entry {
51         struct dentry *__upperdentry;
52         struct ovl_dir_cache *cache;
53         union {
54                 struct {
55                         u64 version;
56                         bool opaque;
57                 };
58                 struct rcu_head rcu;
59         };
60         unsigned numlower;
61         struct path lowerstack[];
62 };
63
64 #define OVL_MAX_STACK 500
65
66 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
67 {
68         return oe->numlower ? oe->lowerstack[0].dentry : NULL;
69 }
70
71 enum ovl_path_type ovl_path_type(struct dentry *dentry)
72 {
73         struct ovl_entry *oe = dentry->d_fsdata;
74         enum ovl_path_type type = 0;
75
76         if (oe->__upperdentry) {
77                 type = __OVL_PATH_UPPER;
78
79                 if (oe->numlower) {
80                         if (S_ISDIR(dentry->d_inode->i_mode))
81                                 type |= __OVL_PATH_MERGE;
82                 } else if (!oe->opaque) {
83                         type |= __OVL_PATH_PURE;
84                 }
85         } else {
86                 if (oe->numlower > 1)
87                         type |= __OVL_PATH_MERGE;
88         }
89         return type;
90 }
91
92 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
93 {
94         return lockless_dereference(oe->__upperdentry);
95 }
96
97 void ovl_path_upper(struct dentry *dentry, struct path *path)
98 {
99         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
100         struct ovl_entry *oe = dentry->d_fsdata;
101
102         path->mnt = ofs->upper_mnt;
103         path->dentry = ovl_upperdentry_dereference(oe);
104 }
105
106 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
107 {
108         enum ovl_path_type type = ovl_path_type(dentry);
109
110         if (!OVL_TYPE_UPPER(type))
111                 ovl_path_lower(dentry, path);
112         else
113                 ovl_path_upper(dentry, path);
114
115         return type;
116 }
117
118 struct dentry *ovl_dentry_upper(struct dentry *dentry)
119 {
120         struct ovl_entry *oe = dentry->d_fsdata;
121
122         return ovl_upperdentry_dereference(oe);
123 }
124
125 struct dentry *ovl_dentry_lower(struct dentry *dentry)
126 {
127         struct ovl_entry *oe = dentry->d_fsdata;
128
129         return __ovl_dentry_lower(oe);
130 }
131
132 struct dentry *ovl_dentry_real(struct dentry *dentry)
133 {
134         struct ovl_entry *oe = dentry->d_fsdata;
135         struct dentry *realdentry;
136
137         realdentry = ovl_upperdentry_dereference(oe);
138         if (!realdentry)
139                 realdentry = __ovl_dentry_lower(oe);
140
141         return realdentry;
142 }
143
144 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
145 {
146         struct dentry *realdentry;
147
148         realdentry = ovl_upperdentry_dereference(oe);
149         if (realdentry) {
150                 *is_upper = true;
151         } else {
152                 realdentry = __ovl_dentry_lower(oe);
153                 *is_upper = false;
154         }
155         return realdentry;
156 }
157
158 struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
159                                     bool is_upper)
160 {
161         if (is_upper) {
162                 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
163
164                 return ofs->upper_mnt;
165         } else {
166                 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
167         }
168 }
169
170 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
171 {
172         struct ovl_entry *oe = dentry->d_fsdata;
173
174         return oe->cache;
175 }
176
177 bool ovl_is_default_permissions(struct inode *inode)
178 {
179         struct ovl_fs *ofs = inode->i_sb->s_fs_info;
180
181         return ofs->config.default_permissions;
182 }
183
184 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
185 {
186         struct ovl_entry *oe = dentry->d_fsdata;
187
188         oe->cache = cache;
189 }
190
191 void ovl_path_lower(struct dentry *dentry, struct path *path)
192 {
193         struct ovl_entry *oe = dentry->d_fsdata;
194
195         *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
196 }
197
198 int ovl_want_write(struct dentry *dentry)
199 {
200         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
201         return mnt_want_write(ofs->upper_mnt);
202 }
203
204 void ovl_drop_write(struct dentry *dentry)
205 {
206         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
207         mnt_drop_write(ofs->upper_mnt);
208 }
209
210 struct dentry *ovl_workdir(struct dentry *dentry)
211 {
212         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
213         return ofs->workdir;
214 }
215
216 bool ovl_dentry_is_opaque(struct dentry *dentry)
217 {
218         struct ovl_entry *oe = dentry->d_fsdata;
219         return oe->opaque;
220 }
221
222 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
223 {
224         struct ovl_entry *oe = dentry->d_fsdata;
225         oe->opaque = opaque;
226 }
227
228 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
229 {
230         struct ovl_entry *oe = dentry->d_fsdata;
231
232         WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
233         WARN_ON(oe->__upperdentry);
234         BUG_ON(!upperdentry->d_inode);
235         /*
236          * Make sure upperdentry is consistent before making it visible to
237          * ovl_upperdentry_dereference().
238          */
239         smp_wmb();
240         oe->__upperdentry = upperdentry;
241 }
242
243 void ovl_dentry_version_inc(struct dentry *dentry)
244 {
245         struct ovl_entry *oe = dentry->d_fsdata;
246
247         WARN_ON(!inode_is_locked(dentry->d_inode));
248         oe->version++;
249 }
250
251 u64 ovl_dentry_version_get(struct dentry *dentry)
252 {
253         struct ovl_entry *oe = dentry->d_fsdata;
254
255         WARN_ON(!inode_is_locked(dentry->d_inode));
256         return oe->version;
257 }
258
259 bool ovl_is_whiteout(struct dentry *dentry)
260 {
261         struct inode *inode = dentry->d_inode;
262
263         return inode && IS_WHITEOUT(inode);
264 }
265
266 static bool ovl_is_opaquedir(struct dentry *dentry)
267 {
268         int res;
269         char val;
270         struct inode *inode = dentry->d_inode;
271
272         if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
273                 return false;
274
275         res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
276         if (res == 1 && val == 'y')
277                 return true;
278
279         return false;
280 }
281
282 static void ovl_dentry_release(struct dentry *dentry)
283 {
284         struct ovl_entry *oe = dentry->d_fsdata;
285
286         if (oe) {
287                 unsigned int i;
288
289                 dput(oe->__upperdentry);
290                 for (i = 0; i < oe->numlower; i++)
291                         dput(oe->lowerstack[i].dentry);
292                 kfree_rcu(oe, rcu);
293         }
294 }
295
296 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
297 {
298         struct ovl_entry *oe = dentry->d_fsdata;
299         unsigned int i;
300         int ret = 1;
301
302         for (i = 0; i < oe->numlower; i++) {
303                 struct dentry *d = oe->lowerstack[i].dentry;
304
305                 if (d->d_flags & DCACHE_OP_REVALIDATE) {
306                         ret = d->d_op->d_revalidate(d, flags);
307                         if (ret < 0)
308                                 return ret;
309                         if (!ret) {
310                                 if (!(flags & LOOKUP_RCU))
311                                         d_invalidate(d);
312                                 return -ESTALE;
313                         }
314                 }
315         }
316         return 1;
317 }
318
319 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
320 {
321         struct ovl_entry *oe = dentry->d_fsdata;
322         unsigned int i;
323         int ret = 1;
324
325         for (i = 0; i < oe->numlower; i++) {
326                 struct dentry *d = oe->lowerstack[i].dentry;
327
328                 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
329                         ret = d->d_op->d_weak_revalidate(d, flags);
330                         if (ret <= 0)
331                                 break;
332                 }
333         }
334         return ret;
335 }
336
337 static const struct dentry_operations ovl_dentry_operations = {
338         .d_release = ovl_dentry_release,
339         .d_select_inode = ovl_d_select_inode,
340 };
341
342 static const struct dentry_operations ovl_reval_dentry_operations = {
343         .d_release = ovl_dentry_release,
344         .d_select_inode = ovl_d_select_inode,
345         .d_revalidate = ovl_dentry_revalidate,
346         .d_weak_revalidate = ovl_dentry_weak_revalidate,
347 };
348
349 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
350 {
351         size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
352         struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
353
354         if (oe)
355                 oe->numlower = numlower;
356
357         return oe;
358 }
359
360 static bool ovl_dentry_remote(struct dentry *dentry)
361 {
362         return dentry->d_flags &
363                 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
364 }
365
366 static bool ovl_dentry_weird(struct dentry *dentry)
367 {
368         return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
369                                   DCACHE_MANAGE_TRANSIT |
370                                   DCACHE_OP_HASH |
371                                   DCACHE_OP_COMPARE);
372 }
373
374 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
375                                              struct qstr *name)
376 {
377         struct dentry *dentry;
378
379         inode_lock(dir->d_inode);
380         dentry = lookup_one_len(name->name, dir, name->len);
381         inode_unlock(dir->d_inode);
382
383         if (IS_ERR(dentry)) {
384                 if (PTR_ERR(dentry) == -ENOENT)
385                         dentry = NULL;
386         } else if (!dentry->d_inode) {
387                 dput(dentry);
388                 dentry = NULL;
389         } else if (ovl_dentry_weird(dentry)) {
390                 dput(dentry);
391                 /* Don't support traversing automounts and other weirdness */
392                 dentry = ERR_PTR(-EREMOTE);
393         }
394         return dentry;
395 }
396
397 /*
398  * Returns next layer in stack starting from top.
399  * Returns -1 if this is the last layer.
400  */
401 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
402 {
403         struct ovl_entry *oe = dentry->d_fsdata;
404
405         BUG_ON(idx < 0);
406         if (idx == 0) {
407                 ovl_path_upper(dentry, path);
408                 if (path->dentry)
409                         return oe->numlower ? 1 : -1;
410                 idx++;
411         }
412         BUG_ON(idx > oe->numlower);
413         *path = oe->lowerstack[idx - 1];
414
415         return (idx < oe->numlower) ? idx + 1 : -1;
416 }
417
418 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
419                           unsigned int flags)
420 {
421         struct ovl_entry *oe;
422         struct ovl_entry *poe = dentry->d_parent->d_fsdata;
423         struct path *stack = NULL;
424         struct dentry *upperdir, *upperdentry = NULL;
425         unsigned int ctr = 0;
426         struct inode *inode = NULL;
427         bool upperopaque = false;
428         struct dentry *this, *prev = NULL;
429         unsigned int i;
430         int err;
431
432         upperdir = ovl_upperdentry_dereference(poe);
433         if (upperdir) {
434                 this = ovl_lookup_real(upperdir, &dentry->d_name);
435                 err = PTR_ERR(this);
436                 if (IS_ERR(this))
437                         goto out;
438
439                 if (this) {
440                         if (unlikely(ovl_dentry_remote(this))) {
441                                 dput(this);
442                                 err = -EREMOTE;
443                                 goto out;
444                         }
445                         if (ovl_is_whiteout(this)) {
446                                 dput(this);
447                                 this = NULL;
448                                 upperopaque = true;
449                         } else if (poe->numlower && ovl_is_opaquedir(this)) {
450                                 upperopaque = true;
451                         }
452                 }
453                 upperdentry = prev = this;
454         }
455
456         if (!upperopaque && poe->numlower) {
457                 err = -ENOMEM;
458                 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
459                 if (!stack)
460                         goto out_put_upper;
461         }
462
463         for (i = 0; !upperopaque && i < poe->numlower; i++) {
464                 bool opaque = false;
465                 struct path lowerpath = poe->lowerstack[i];
466
467                 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
468                 err = PTR_ERR(this);
469                 if (IS_ERR(this)) {
470                         /*
471                          * If it's positive, then treat ENAMETOOLONG as ENOENT.
472                          */
473                         if (err == -ENAMETOOLONG && (upperdentry || ctr))
474                                 continue;
475                         goto out_put;
476                 }
477                 if (!this)
478                         continue;
479                 if (ovl_is_whiteout(this)) {
480                         dput(this);
481                         break;
482                 }
483                 /*
484                  * Only makes sense to check opaque dir if this is not the
485                  * lowermost layer.
486                  */
487                 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
488                         opaque = true;
489
490                 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
491                              !S_ISDIR(this->d_inode->i_mode))) {
492                         /*
493                          * FIXME: check for upper-opaqueness maybe better done
494                          * in remove code.
495                          */
496                         if (prev == upperdentry)
497                                 upperopaque = true;
498                         dput(this);
499                         break;
500                 }
501                 /*
502                  * If this is a non-directory then stop here.
503                  */
504                 if (!S_ISDIR(this->d_inode->i_mode))
505                         opaque = true;
506
507                 stack[ctr].dentry = this;
508                 stack[ctr].mnt = lowerpath.mnt;
509                 ctr++;
510                 prev = this;
511                 if (opaque)
512                         break;
513         }
514
515         oe = ovl_alloc_entry(ctr);
516         err = -ENOMEM;
517         if (!oe)
518                 goto out_put;
519
520         if (upperdentry || ctr) {
521                 struct dentry *realdentry;
522
523                 realdentry = upperdentry ? upperdentry : stack[0].dentry;
524
525                 err = -ENOMEM;
526                 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
527                                       oe);
528                 if (!inode)
529                         goto out_free_oe;
530                 ovl_copyattr(realdentry->d_inode, inode);
531         }
532
533         oe->opaque = upperopaque;
534         oe->__upperdentry = upperdentry;
535         memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
536         kfree(stack);
537         dentry->d_fsdata = oe;
538         d_add(dentry, inode);
539
540         return NULL;
541
542 out_free_oe:
543         kfree(oe);
544 out_put:
545         for (i = 0; i < ctr; i++)
546                 dput(stack[i].dentry);
547         kfree(stack);
548 out_put_upper:
549         dput(upperdentry);
550 out:
551         return ERR_PTR(err);
552 }
553
554 struct file *ovl_path_open(struct path *path, int flags)
555 {
556         return dentry_open(path, flags, current_cred());
557 }
558
559 static void ovl_put_super(struct super_block *sb)
560 {
561         struct ovl_fs *ufs = sb->s_fs_info;
562         unsigned i;
563
564         dput(ufs->workdir);
565         mntput(ufs->upper_mnt);
566         for (i = 0; i < ufs->numlower; i++)
567                 mntput(ufs->lower_mnt[i]);
568         kfree(ufs->lower_mnt);
569
570         kfree(ufs->config.lowerdir);
571         kfree(ufs->config.upperdir);
572         kfree(ufs->config.workdir);
573         kfree(ufs);
574 }
575
576 /**
577  * ovl_statfs
578  * @sb: The overlayfs super block
579  * @buf: The struct kstatfs to fill in with stats
580  *
581  * Get the filesystem statistics.  As writes always target the upper layer
582  * filesystem pass the statfs to the upper filesystem (if it exists)
583  */
584 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
585 {
586         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
587         struct dentry *root_dentry = dentry->d_sb->s_root;
588         struct path path;
589         int err;
590
591         ovl_path_real(root_dentry, &path);
592
593         err = vfs_statfs(&path, buf);
594         if (!err) {
595                 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
596                 buf->f_type = OVERLAYFS_SUPER_MAGIC;
597         }
598
599         return err;
600 }
601
602 /**
603  * ovl_show_options
604  *
605  * Prints the mount options for a given superblock.
606  * Returns zero; does not fail.
607  */
608 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
609 {
610         struct super_block *sb = dentry->d_sb;
611         struct ovl_fs *ufs = sb->s_fs_info;
612
613         seq_show_option(m, "lowerdir", ufs->config.lowerdir);
614         if (ufs->config.upperdir) {
615                 seq_show_option(m, "upperdir", ufs->config.upperdir);
616                 seq_show_option(m, "workdir", ufs->config.workdir);
617         }
618         if (ufs->config.default_permissions)
619                 seq_puts(m, ",default_permissions");
620         return 0;
621 }
622
623 static int ovl_remount(struct super_block *sb, int *flags, char *data)
624 {
625         struct ovl_fs *ufs = sb->s_fs_info;
626
627         if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
628                 return -EROFS;
629
630         return 0;
631 }
632
633 static const struct super_operations ovl_super_operations = {
634         .put_super      = ovl_put_super,
635         .statfs         = ovl_statfs,
636         .show_options   = ovl_show_options,
637         .remount_fs     = ovl_remount,
638 };
639
640 enum {
641         OPT_LOWERDIR,
642         OPT_UPPERDIR,
643         OPT_WORKDIR,
644         OPT_DEFAULT_PERMISSIONS,
645         OPT_ERR,
646 };
647
648 static const match_table_t ovl_tokens = {
649         {OPT_LOWERDIR,                  "lowerdir=%s"},
650         {OPT_UPPERDIR,                  "upperdir=%s"},
651         {OPT_WORKDIR,                   "workdir=%s"},
652         {OPT_DEFAULT_PERMISSIONS,       "default_permissions"},
653         {OPT_ERR,                       NULL}
654 };
655
656 static char *ovl_next_opt(char **s)
657 {
658         char *sbegin = *s;
659         char *p;
660
661         if (sbegin == NULL)
662                 return NULL;
663
664         for (p = sbegin; *p; p++) {
665                 if (*p == '\\') {
666                         p++;
667                         if (!*p)
668                                 break;
669                 } else if (*p == ',') {
670                         *p = '\0';
671                         *s = p + 1;
672                         return sbegin;
673                 }
674         }
675         *s = NULL;
676         return sbegin;
677 }
678
679 static int ovl_parse_opt(char *opt, struct ovl_config *config)
680 {
681         char *p;
682
683         while ((p = ovl_next_opt(&opt)) != NULL) {
684                 int token;
685                 substring_t args[MAX_OPT_ARGS];
686
687                 if (!*p)
688                         continue;
689
690                 token = match_token(p, ovl_tokens, args);
691                 switch (token) {
692                 case OPT_UPPERDIR:
693                         kfree(config->upperdir);
694                         config->upperdir = match_strdup(&args[0]);
695                         if (!config->upperdir)
696                                 return -ENOMEM;
697                         break;
698
699                 case OPT_LOWERDIR:
700                         kfree(config->lowerdir);
701                         config->lowerdir = match_strdup(&args[0]);
702                         if (!config->lowerdir)
703                                 return -ENOMEM;
704                         break;
705
706                 case OPT_WORKDIR:
707                         kfree(config->workdir);
708                         config->workdir = match_strdup(&args[0]);
709                         if (!config->workdir)
710                                 return -ENOMEM;
711                         break;
712
713                 case OPT_DEFAULT_PERMISSIONS:
714                         config->default_permissions = true;
715                         break;
716
717                 default:
718                         pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
719                         return -EINVAL;
720                 }
721         }
722
723         /* Workdir is useless in non-upper mount */
724         if (!config->upperdir && config->workdir) {
725                 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
726                         config->workdir);
727                 kfree(config->workdir);
728                 config->workdir = NULL;
729         }
730
731         return 0;
732 }
733
734 #define OVL_WORKDIR_NAME "work"
735
736 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
737                                          struct dentry *dentry)
738 {
739         struct inode *dir = dentry->d_inode;
740         struct dentry *work;
741         int err;
742         bool retried = false;
743
744         err = mnt_want_write(mnt);
745         if (err)
746                 return ERR_PTR(err);
747
748         inode_lock_nested(dir, I_MUTEX_PARENT);
749 retry:
750         work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
751                               strlen(OVL_WORKDIR_NAME));
752
753         if (!IS_ERR(work)) {
754                 struct kstat stat = {
755                         .mode = S_IFDIR | 0,
756                 };
757
758                 if (work->d_inode) {
759                         err = -EEXIST;
760                         if (retried)
761                                 goto out_dput;
762
763                         retried = true;
764                         ovl_cleanup(dir, work);
765                         dput(work);
766                         goto retry;
767                 }
768
769                 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
770                 if (err)
771                         goto out_dput;
772         }
773 out_unlock:
774         inode_unlock(dir);
775         mnt_drop_write(mnt);
776
777         return work;
778
779 out_dput:
780         dput(work);
781         work = ERR_PTR(err);
782         goto out_unlock;
783 }
784
785 static void ovl_unescape(char *s)
786 {
787         char *d = s;
788
789         for (;; s++, d++) {
790                 if (*s == '\\')
791                         s++;
792                 *d = *s;
793                 if (!*s)
794                         break;
795         }
796 }
797
798 static int ovl_mount_dir_noesc(const char *name, struct path *path)
799 {
800         int err = -EINVAL;
801
802         if (!*name) {
803                 pr_err("overlayfs: empty lowerdir\n");
804                 goto out;
805         }
806         err = kern_path(name, LOOKUP_FOLLOW, path);
807         if (err) {
808                 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
809                 goto out;
810         }
811         err = -EINVAL;
812         if (ovl_dentry_weird(path->dentry)) {
813                 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
814                 goto out_put;
815         }
816         if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
817                 pr_err("overlayfs: '%s' not a directory\n", name);
818                 goto out_put;
819         }
820         return 0;
821
822 out_put:
823         path_put(path);
824 out:
825         return err;
826 }
827
828 static int ovl_mount_dir(const char *name, struct path *path)
829 {
830         int err = -ENOMEM;
831         char *tmp = kstrdup(name, GFP_KERNEL);
832
833         if (tmp) {
834                 ovl_unescape(tmp);
835                 err = ovl_mount_dir_noesc(tmp, path);
836
837                 if (!err)
838                         if (ovl_dentry_remote(path->dentry)) {
839                                 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
840                                        tmp);
841                                 path_put(path);
842                                 err = -EINVAL;
843                         }
844                 kfree(tmp);
845         }
846         return err;
847 }
848
849 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
850                          int *stack_depth, bool *remote)
851 {
852         int err;
853         struct kstatfs statfs;
854
855         err = ovl_mount_dir_noesc(name, path);
856         if (err)
857                 goto out;
858
859         err = vfs_statfs(path, &statfs);
860         if (err) {
861                 pr_err("overlayfs: statfs failed on '%s'\n", name);
862                 goto out_put;
863         }
864         *namelen = max(*namelen, statfs.f_namelen);
865         *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
866
867         if (ovl_dentry_remote(path->dentry))
868                 *remote = true;
869
870         return 0;
871
872 out_put:
873         path_put(path);
874 out:
875         return err;
876 }
877
878 /* Workdir should not be subdir of upperdir and vice versa */
879 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
880 {
881         bool ok = false;
882
883         if (workdir != upperdir) {
884                 ok = (lock_rename(workdir, upperdir) == NULL);
885                 unlock_rename(workdir, upperdir);
886         }
887         return ok;
888 }
889
890 static unsigned int ovl_split_lowerdirs(char *str)
891 {
892         unsigned int ctr = 1;
893         char *s, *d;
894
895         for (s = d = str;; s++, d++) {
896                 if (*s == '\\') {
897                         s++;
898                 } else if (*s == ':') {
899                         *d = '\0';
900                         ctr++;
901                         continue;
902                 }
903                 *d = *s;
904                 if (!*s)
905                         break;
906         }
907         return ctr;
908 }
909
910 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
911 {
912         struct path upperpath = { NULL, NULL };
913         struct path workpath = { NULL, NULL };
914         struct dentry *root_dentry;
915         struct ovl_entry *oe;
916         struct ovl_fs *ufs;
917         struct path *stack = NULL;
918         char *lowertmp;
919         char *lower;
920         unsigned int numlower;
921         unsigned int stacklen = 0;
922         unsigned int i;
923         bool remote = false;
924         int err;
925
926         err = -ENOMEM;
927         ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
928         if (!ufs)
929                 goto out;
930
931         err = ovl_parse_opt((char *) data, &ufs->config);
932         if (err)
933                 goto out_free_config;
934
935         err = -EINVAL;
936         if (!ufs->config.lowerdir) {
937                 pr_err("overlayfs: missing 'lowerdir'\n");
938                 goto out_free_config;
939         }
940
941         sb->s_stack_depth = 0;
942         sb->s_maxbytes = MAX_LFS_FILESIZE;
943         if (ufs->config.upperdir) {
944                 if (!ufs->config.workdir) {
945                         pr_err("overlayfs: missing 'workdir'\n");
946                         goto out_free_config;
947                 }
948
949                 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
950                 if (err)
951                         goto out_free_config;
952
953                 /* Upper fs should not be r/o */
954                 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
955                         pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
956                         err = -EINVAL;
957                         goto out_put_upperpath;
958                 }
959
960                 err = ovl_mount_dir(ufs->config.workdir, &workpath);
961                 if (err)
962                         goto out_put_upperpath;
963
964                 err = -EINVAL;
965                 if (upperpath.mnt != workpath.mnt) {
966                         pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
967                         goto out_put_workpath;
968                 }
969                 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
970                         pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
971                         goto out_put_workpath;
972                 }
973                 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
974         }
975         err = -ENOMEM;
976         lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
977         if (!lowertmp)
978                 goto out_put_workpath;
979
980         err = -EINVAL;
981         stacklen = ovl_split_lowerdirs(lowertmp);
982         if (stacklen > OVL_MAX_STACK) {
983                 pr_err("overlayfs: too many lower directries, limit is %d\n",
984                        OVL_MAX_STACK);
985                 goto out_free_lowertmp;
986         } else if (!ufs->config.upperdir && stacklen == 1) {
987                 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
988                 goto out_free_lowertmp;
989         }
990
991         stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
992         if (!stack)
993                 goto out_free_lowertmp;
994
995         lower = lowertmp;
996         for (numlower = 0; numlower < stacklen; numlower++) {
997                 err = ovl_lower_dir(lower, &stack[numlower],
998                                     &ufs->lower_namelen, &sb->s_stack_depth,
999                                     &remote);
1000                 if (err)
1001                         goto out_put_lowerpath;
1002
1003                 lower = strchr(lower, '\0') + 1;
1004         }
1005
1006         err = -EINVAL;
1007         sb->s_stack_depth++;
1008         if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1009                 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1010                 goto out_put_lowerpath;
1011         }
1012
1013         if (ufs->config.upperdir) {
1014                 ufs->upper_mnt = clone_private_mount(&upperpath);
1015                 err = PTR_ERR(ufs->upper_mnt);
1016                 if (IS_ERR(ufs->upper_mnt)) {
1017                         pr_err("overlayfs: failed to clone upperpath\n");
1018                         goto out_put_lowerpath;
1019                 }
1020
1021                 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1022                 err = PTR_ERR(ufs->workdir);
1023                 if (IS_ERR(ufs->workdir)) {
1024                         pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1025                                 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1026                         sb->s_flags |= MS_RDONLY;
1027                         ufs->workdir = NULL;
1028                 }
1029         }
1030
1031         err = -ENOMEM;
1032         ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1033         if (ufs->lower_mnt == NULL)
1034                 goto out_put_workdir;
1035         for (i = 0; i < numlower; i++) {
1036                 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1037
1038                 err = PTR_ERR(mnt);
1039                 if (IS_ERR(mnt)) {
1040                         pr_err("overlayfs: failed to clone lowerpath\n");
1041                         goto out_put_lower_mnt;
1042                 }
1043                 /*
1044                  * Make lower_mnt R/O.  That way fchmod/fchown on lower file
1045                  * will fail instead of modifying lower fs.
1046                  */
1047                 mnt->mnt_flags |= MNT_READONLY;
1048
1049                 ufs->lower_mnt[ufs->numlower] = mnt;
1050                 ufs->numlower++;
1051         }
1052
1053         /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1054         if (!ufs->upper_mnt)
1055                 sb->s_flags |= MS_RDONLY;
1056
1057         if (remote)
1058                 sb->s_d_op = &ovl_reval_dentry_operations;
1059         else
1060                 sb->s_d_op = &ovl_dentry_operations;
1061
1062         err = -ENOMEM;
1063         oe = ovl_alloc_entry(numlower);
1064         if (!oe)
1065                 goto out_put_lower_mnt;
1066
1067         root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
1068         if (!root_dentry)
1069                 goto out_free_oe;
1070
1071         mntput(upperpath.mnt);
1072         for (i = 0; i < numlower; i++)
1073                 mntput(stack[i].mnt);
1074         path_put(&workpath);
1075         kfree(lowertmp);
1076
1077         oe->__upperdentry = upperpath.dentry;
1078         for (i = 0; i < numlower; i++) {
1079                 oe->lowerstack[i].dentry = stack[i].dentry;
1080                 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1081         }
1082         kfree(stack);
1083
1084         root_dentry->d_fsdata = oe;
1085
1086         ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1087                      root_dentry->d_inode);
1088
1089         sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1090         sb->s_op = &ovl_super_operations;
1091         sb->s_root = root_dentry;
1092         sb->s_fs_info = ufs;
1093
1094         return 0;
1095
1096 out_free_oe:
1097         kfree(oe);
1098 out_put_lower_mnt:
1099         for (i = 0; i < ufs->numlower; i++)
1100                 mntput(ufs->lower_mnt[i]);
1101         kfree(ufs->lower_mnt);
1102 out_put_workdir:
1103         dput(ufs->workdir);
1104         mntput(ufs->upper_mnt);
1105 out_put_lowerpath:
1106         for (i = 0; i < numlower; i++)
1107                 path_put(&stack[i]);
1108         kfree(stack);
1109 out_free_lowertmp:
1110         kfree(lowertmp);
1111 out_put_workpath:
1112         path_put(&workpath);
1113 out_put_upperpath:
1114         path_put(&upperpath);
1115 out_free_config:
1116         kfree(ufs->config.lowerdir);
1117         kfree(ufs->config.upperdir);
1118         kfree(ufs->config.workdir);
1119         kfree(ufs);
1120 out:
1121         return err;
1122 }
1123
1124 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1125                                 const char *dev_name, void *raw_data)
1126 {
1127         return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1128 }
1129
1130 static struct file_system_type ovl_fs_type = {
1131         .owner          = THIS_MODULE,
1132         .name           = "overlay",
1133         .mount          = ovl_mount,
1134         .kill_sb        = kill_anon_super,
1135 };
1136 MODULE_ALIAS_FS("overlay");
1137
1138 static int __init ovl_init(void)
1139 {
1140         return register_filesystem(&ovl_fs_type);
1141 }
1142
1143 static void __exit ovl_exit(void)
1144 {
1145         unregister_filesystem(&ovl_fs_type);
1146 }
1147
1148 module_init(ovl_init);
1149 module_exit(ovl_exit);