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