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