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