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