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