OSDN Git Service

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