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