GNU Linux-libre 4.14.328-gnu1
[releases.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 <uapi/linux/magic.h>
11 #include <linux/fs.h>
12 #include <linux/namei.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.h>
15 #include <linux/parser.h>
16 #include <linux/module.h>
17 #include <linux/statfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/posix_acl_xattr.h>
20 #include "overlayfs.h"
21 #include "ovl_entry.h"
22
23 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24 MODULE_DESCRIPTION("Overlay filesystem");
25 MODULE_LICENSE("GPL");
26
27
28 struct ovl_dir_cache;
29
30 #define OVL_MAX_STACK 500
31
32 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34 MODULE_PARM_DESC(ovl_redirect_dir_def,
35                  "Default to on or off for the redirect_dir feature");
36
37 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
38 module_param_named(index, ovl_index_def, bool, 0644);
39 MODULE_PARM_DESC(ovl_index_def,
40                  "Default to on or off for the inodes index feature");
41
42 static void ovl_dentry_release(struct dentry *dentry)
43 {
44         struct ovl_entry *oe = dentry->d_fsdata;
45
46         if (oe) {
47                 unsigned int i;
48
49                 for (i = 0; i < oe->numlower; i++)
50                         dput(oe->lowerstack[i].dentry);
51                 kfree_rcu(oe, rcu);
52         }
53 }
54
55 static int ovl_check_append_only(struct inode *inode, int flag)
56 {
57         /*
58          * This test was moot in vfs may_open() because overlay inode does
59          * not have the S_APPEND flag, so re-check on real upper inode
60          */
61         if (IS_APPEND(inode)) {
62                 if  ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
63                         return -EPERM;
64                 if (flag & O_TRUNC)
65                         return -EPERM;
66         }
67
68         return 0;
69 }
70
71 static struct dentry *ovl_d_real(struct dentry *dentry,
72                                  const struct inode *inode,
73                                  unsigned int open_flags, unsigned int flags)
74 {
75         struct dentry *real;
76         int err;
77
78         if (flags & D_REAL_UPPER)
79                 return ovl_dentry_upper(dentry);
80
81         if (!d_is_reg(dentry)) {
82                 if (!inode || inode == d_inode(dentry))
83                         return dentry;
84                 goto bug;
85         }
86
87         if (open_flags) {
88                 err = ovl_open_maybe_copy_up(dentry, open_flags);
89                 if (err)
90                         return ERR_PTR(err);
91         }
92
93         real = ovl_dentry_upper(dentry);
94         if (real && (!inode || inode == d_inode(real))) {
95                 if (!inode) {
96                         err = ovl_check_append_only(d_inode(real), open_flags);
97                         if (err)
98                                 return ERR_PTR(err);
99                 }
100                 return real;
101         }
102
103         real = ovl_dentry_lower(dentry);
104         if (!real)
105                 goto bug;
106
107         /* Handle recursion */
108         real = d_real(real, inode, open_flags, 0);
109
110         if (!inode || inode == d_inode(real))
111                 return real;
112 bug:
113         WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
114              inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
115         return dentry;
116 }
117
118 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
119 {
120         struct ovl_entry *oe = dentry->d_fsdata;
121         unsigned int i;
122         int ret = 1;
123
124         for (i = 0; i < oe->numlower; i++) {
125                 struct dentry *d = oe->lowerstack[i].dentry;
126
127                 if (d->d_flags & DCACHE_OP_REVALIDATE) {
128                         ret = d->d_op->d_revalidate(d, flags);
129                         if (ret < 0)
130                                 return ret;
131                         if (!ret) {
132                                 if (!(flags & LOOKUP_RCU))
133                                         d_invalidate(d);
134                                 return -ESTALE;
135                         }
136                 }
137         }
138         return 1;
139 }
140
141 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
142 {
143         struct ovl_entry *oe = dentry->d_fsdata;
144         unsigned int i;
145         int ret = 1;
146
147         for (i = 0; i < oe->numlower; i++) {
148                 struct dentry *d = oe->lowerstack[i].dentry;
149
150                 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
151                         ret = d->d_op->d_weak_revalidate(d, flags);
152                         if (ret <= 0)
153                                 break;
154                 }
155         }
156         return ret;
157 }
158
159 static const struct dentry_operations ovl_dentry_operations = {
160         .d_release = ovl_dentry_release,
161         .d_real = ovl_d_real,
162 };
163
164 static const struct dentry_operations ovl_reval_dentry_operations = {
165         .d_release = ovl_dentry_release,
166         .d_real = ovl_d_real,
167         .d_revalidate = ovl_dentry_revalidate,
168         .d_weak_revalidate = ovl_dentry_weak_revalidate,
169 };
170
171 static struct kmem_cache *ovl_inode_cachep;
172
173 static struct inode *ovl_alloc_inode(struct super_block *sb)
174 {
175         struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
176
177         if (!oi)
178                 return NULL;
179
180         oi->cache = NULL;
181         oi->redirect = NULL;
182         oi->version = 0;
183         oi->flags = 0;
184         oi->__upperdentry = NULL;
185         oi->lower = NULL;
186         mutex_init(&oi->lock);
187
188         return &oi->vfs_inode;
189 }
190
191 static void ovl_i_callback(struct rcu_head *head)
192 {
193         struct inode *inode = container_of(head, struct inode, i_rcu);
194
195         kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
196 }
197
198 static void ovl_destroy_inode(struct inode *inode)
199 {
200         struct ovl_inode *oi = OVL_I(inode);
201
202         dput(oi->__upperdentry);
203         iput(oi->lower);
204         kfree(oi->redirect);
205         ovl_dir_cache_free(inode);
206         mutex_destroy(&oi->lock);
207
208         call_rcu(&inode->i_rcu, ovl_i_callback);
209 }
210
211 static void ovl_put_super(struct super_block *sb)
212 {
213         struct ovl_fs *ufs = sb->s_fs_info;
214         unsigned i;
215
216         dput(ufs->indexdir);
217         dput(ufs->workdir);
218         if (ufs->workdir_locked)
219                 ovl_inuse_unlock(ufs->workbasedir);
220         dput(ufs->workbasedir);
221         if (ufs->upper_mnt && ufs->upperdir_locked)
222                 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
223         mntput(ufs->upper_mnt);
224         for (i = 0; i < ufs->numlower; i++)
225                 mntput(ufs->lower_mnt[i]);
226         kfree(ufs->lower_mnt);
227
228         kfree(ufs->config.lowerdir);
229         kfree(ufs->config.upperdir);
230         kfree(ufs->config.workdir);
231         put_cred(ufs->creator_cred);
232         kfree(ufs);
233 }
234
235 /* Sync real dirty inodes in upper filesystem (if it exists) */
236 static int ovl_sync_fs(struct super_block *sb, int wait)
237 {
238         struct ovl_fs *ufs = sb->s_fs_info;
239         struct super_block *upper_sb;
240         int ret;
241
242         if (!ufs->upper_mnt)
243                 return 0;
244
245         /*
246          * If this is a sync(2) call or an emergency sync, all the super blocks
247          * will be iterated, including upper_sb, so no need to do anything.
248          *
249          * If this is a syncfs(2) call, then we do need to call
250          * sync_filesystem() on upper_sb, but enough if we do it when being
251          * called with wait == 1.
252          */
253         if (!wait)
254                 return 0;
255
256         upper_sb = ufs->upper_mnt->mnt_sb;
257
258         down_read(&upper_sb->s_umount);
259         ret = sync_filesystem(upper_sb);
260         up_read(&upper_sb->s_umount);
261
262         return ret;
263 }
264
265 /**
266  * ovl_statfs
267  * @sb: The overlayfs super block
268  * @buf: The struct kstatfs to fill in with stats
269  *
270  * Get the filesystem statistics.  As writes always target the upper layer
271  * filesystem pass the statfs to the upper filesystem (if it exists)
272  */
273 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
274 {
275         struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
276         struct dentry *root_dentry = dentry->d_sb->s_root;
277         struct path path;
278         int err;
279
280         ovl_path_real(root_dentry, &path);
281
282         err = vfs_statfs(&path, buf);
283         if (!err) {
284                 buf->f_namelen = ofs->namelen;
285                 buf->f_type = OVERLAYFS_SUPER_MAGIC;
286         }
287
288         return err;
289 }
290
291 /* Will this overlay be forced to mount/remount ro? */
292 static bool ovl_force_readonly(struct ovl_fs *ufs)
293 {
294         return (!ufs->upper_mnt || !ufs->workdir);
295 }
296
297 /**
298  * ovl_show_options
299  *
300  * Prints the mount options for a given superblock.
301  * Returns zero; does not fail.
302  */
303 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
304 {
305         struct super_block *sb = dentry->d_sb;
306         struct ovl_fs *ufs = sb->s_fs_info;
307
308         seq_show_option(m, "lowerdir", ufs->config.lowerdir);
309         if (ufs->config.upperdir) {
310                 seq_show_option(m, "upperdir", ufs->config.upperdir);
311                 seq_show_option(m, "workdir", ufs->config.workdir);
312         }
313         if (ufs->config.default_permissions)
314                 seq_puts(m, ",default_permissions");
315         if (ufs->config.redirect_dir != ovl_redirect_dir_def)
316                 seq_printf(m, ",redirect_dir=%s",
317                            ufs->config.redirect_dir ? "on" : "off");
318         if (ufs->config.index != ovl_index_def)
319                 seq_printf(m, ",index=%s",
320                            ufs->config.index ? "on" : "off");
321         return 0;
322 }
323
324 static int ovl_remount(struct super_block *sb, int *flags, char *data)
325 {
326         struct ovl_fs *ufs = sb->s_fs_info;
327
328         if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
329                 return -EROFS;
330
331         return 0;
332 }
333
334 static const struct super_operations ovl_super_operations = {
335         .alloc_inode    = ovl_alloc_inode,
336         .destroy_inode  = ovl_destroy_inode,
337         .drop_inode     = generic_delete_inode,
338         .put_super      = ovl_put_super,
339         .sync_fs        = ovl_sync_fs,
340         .statfs         = ovl_statfs,
341         .show_options   = ovl_show_options,
342         .remount_fs     = ovl_remount,
343 };
344
345 enum {
346         OPT_LOWERDIR,
347         OPT_UPPERDIR,
348         OPT_WORKDIR,
349         OPT_DEFAULT_PERMISSIONS,
350         OPT_REDIRECT_DIR_ON,
351         OPT_REDIRECT_DIR_OFF,
352         OPT_INDEX_ON,
353         OPT_INDEX_OFF,
354         OPT_ERR,
355 };
356
357 static const match_table_t ovl_tokens = {
358         {OPT_LOWERDIR,                  "lowerdir=%s"},
359         {OPT_UPPERDIR,                  "upperdir=%s"},
360         {OPT_WORKDIR,                   "workdir=%s"},
361         {OPT_DEFAULT_PERMISSIONS,       "default_permissions"},
362         {OPT_REDIRECT_DIR_ON,           "redirect_dir=on"},
363         {OPT_REDIRECT_DIR_OFF,          "redirect_dir=off"},
364         {OPT_INDEX_ON,                  "index=on"},
365         {OPT_INDEX_OFF,                 "index=off"},
366         {OPT_ERR,                       NULL}
367 };
368
369 static char *ovl_next_opt(char **s)
370 {
371         char *sbegin = *s;
372         char *p;
373
374         if (sbegin == NULL)
375                 return NULL;
376
377         for (p = sbegin; *p; p++) {
378                 if (*p == '\\') {
379                         p++;
380                         if (!*p)
381                                 break;
382                 } else if (*p == ',') {
383                         *p = '\0';
384                         *s = p + 1;
385                         return sbegin;
386                 }
387         }
388         *s = NULL;
389         return sbegin;
390 }
391
392 static int ovl_parse_opt(char *opt, struct ovl_config *config)
393 {
394         char *p;
395
396         while ((p = ovl_next_opt(&opt)) != NULL) {
397                 int token;
398                 substring_t args[MAX_OPT_ARGS];
399
400                 if (!*p)
401                         continue;
402
403                 token = match_token(p, ovl_tokens, args);
404                 switch (token) {
405                 case OPT_UPPERDIR:
406                         kfree(config->upperdir);
407                         config->upperdir = match_strdup(&args[0]);
408                         if (!config->upperdir)
409                                 return -ENOMEM;
410                         break;
411
412                 case OPT_LOWERDIR:
413                         kfree(config->lowerdir);
414                         config->lowerdir = match_strdup(&args[0]);
415                         if (!config->lowerdir)
416                                 return -ENOMEM;
417                         break;
418
419                 case OPT_WORKDIR:
420                         kfree(config->workdir);
421                         config->workdir = match_strdup(&args[0]);
422                         if (!config->workdir)
423                                 return -ENOMEM;
424                         break;
425
426                 case OPT_DEFAULT_PERMISSIONS:
427                         config->default_permissions = true;
428                         break;
429
430                 case OPT_REDIRECT_DIR_ON:
431                         config->redirect_dir = true;
432                         break;
433
434                 case OPT_REDIRECT_DIR_OFF:
435                         config->redirect_dir = false;
436                         break;
437
438                 case OPT_INDEX_ON:
439                         config->index = true;
440                         break;
441
442                 case OPT_INDEX_OFF:
443                         config->index = false;
444                         break;
445
446                 default:
447                         pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
448                         return -EINVAL;
449                 }
450         }
451
452         /* Workdir is useless in non-upper mount */
453         if (!config->upperdir && config->workdir) {
454                 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
455                         config->workdir);
456                 kfree(config->workdir);
457                 config->workdir = NULL;
458         }
459
460         return 0;
461 }
462
463 #define OVL_WORKDIR_NAME "work"
464 #define OVL_INDEXDIR_NAME "index"
465
466 static struct dentry *ovl_workdir_create(struct super_block *sb,
467                                          struct ovl_fs *ufs,
468                                          struct dentry *dentry,
469                                          const char *name, bool persist)
470 {
471         struct inode *dir = dentry->d_inode;
472         struct vfsmount *mnt = ufs->upper_mnt;
473         struct dentry *work;
474         int err;
475         bool retried = false;
476         bool locked = false;
477
478         err = mnt_want_write(mnt);
479         if (err)
480                 goto out_err;
481
482         inode_lock_nested(dir, I_MUTEX_PARENT);
483         locked = true;
484
485 retry:
486         work = lookup_one_len(name, dentry, strlen(name));
487
488         if (!IS_ERR(work)) {
489                 struct iattr attr = {
490                         .ia_valid = ATTR_MODE,
491                         .ia_mode = S_IFDIR | 0,
492                 };
493
494                 if (work->d_inode) {
495                         err = -EEXIST;
496                         if (retried)
497                                 goto out_dput;
498
499                         if (persist)
500                                 goto out_unlock;
501
502                         retried = true;
503                         ovl_workdir_cleanup(dir, mnt, work, 0);
504                         dput(work);
505                         goto retry;
506                 }
507
508                 err = ovl_create_real(dir, work,
509                                       &(struct cattr){.mode = S_IFDIR | 0},
510                                       NULL, true);
511                 if (err)
512                         goto out_dput;
513
514                 /*
515                  * Try to remove POSIX ACL xattrs from workdir.  We are good if:
516                  *
517                  * a) success (there was a POSIX ACL xattr and was removed)
518                  * b) -ENODATA (there was no POSIX ACL xattr)
519                  * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
520                  *
521                  * There are various other error values that could effectively
522                  * mean that the xattr doesn't exist (e.g. -ERANGE is returned
523                  * if the xattr name is too long), but the set of filesystems
524                  * allowed as upper are limited to "normal" ones, where checking
525                  * for the above two errors is sufficient.
526                  */
527                 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
528                 if (err && err != -ENODATA && err != -EOPNOTSUPP)
529                         goto out_dput;
530
531                 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
532                 if (err && err != -ENODATA && err != -EOPNOTSUPP)
533                         goto out_dput;
534
535                 /* Clear any inherited mode bits */
536                 inode_lock(work->d_inode);
537                 err = notify_change(work, &attr, NULL);
538                 inode_unlock(work->d_inode);
539                 if (err)
540                         goto out_dput;
541         } else {
542                 err = PTR_ERR(work);
543                 goto out_err;
544         }
545 out_unlock:
546         mnt_drop_write(mnt);
547         if (locked)
548                 inode_unlock(dir);
549
550         return work;
551
552 out_dput:
553         dput(work);
554 out_err:
555         pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
556                 ufs->config.workdir, name, -err);
557         sb->s_flags |= MS_RDONLY;
558         work = NULL;
559         goto out_unlock;
560 }
561
562 static void ovl_unescape(char *s)
563 {
564         char *d = s;
565
566         for (;; s++, d++) {
567                 if (*s == '\\')
568                         s++;
569                 *d = *s;
570                 if (!*s)
571                         break;
572         }
573 }
574
575 static int ovl_mount_dir_noesc(const char *name, struct path *path)
576 {
577         int err = -EINVAL;
578
579         if (!*name) {
580                 pr_err("overlayfs: empty lowerdir\n");
581                 goto out;
582         }
583         err = kern_path(name, LOOKUP_FOLLOW, path);
584         if (err) {
585                 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
586                 goto out;
587         }
588         err = -EINVAL;
589         if (ovl_dentry_weird(path->dentry)) {
590                 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
591                 goto out_put;
592         }
593         if (!d_is_dir(path->dentry)) {
594                 pr_err("overlayfs: '%s' not a directory\n", name);
595                 goto out_put;
596         }
597         return 0;
598
599 out_put:
600         path_put(path);
601 out:
602         return err;
603 }
604
605 static int ovl_mount_dir(const char *name, struct path *path)
606 {
607         int err = -ENOMEM;
608         char *tmp = kstrdup(name, GFP_KERNEL);
609
610         if (tmp) {
611                 ovl_unescape(tmp);
612                 err = ovl_mount_dir_noesc(tmp, path);
613
614                 if (!err)
615                         if (ovl_dentry_remote(path->dentry)) {
616                                 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
617                                        tmp);
618                                 path_put(path);
619                                 err = -EINVAL;
620                         }
621                 kfree(tmp);
622         }
623         return err;
624 }
625
626 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
627                              const char *name)
628 {
629         struct kstatfs statfs;
630         int err = vfs_statfs(path, &statfs);
631
632         if (err)
633                 pr_err("overlayfs: statfs failed on '%s'\n", name);
634         else
635                 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
636
637         return err;
638 }
639
640 static int ovl_lower_dir(const char *name, struct path *path,
641                          struct ovl_fs *ofs, int *stack_depth, bool *remote)
642 {
643         int err;
644
645         err = ovl_mount_dir_noesc(name, path);
646         if (err)
647                 goto out;
648
649         err = ovl_check_namelen(path, ofs, name);
650         if (err)
651                 goto out_put;
652
653         *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
654
655         if (ovl_dentry_remote(path->dentry))
656                 *remote = true;
657
658         /*
659          * The inodes index feature needs to encode and decode file
660          * handles, so it requires that all layers support them.
661          */
662         if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
663                 ofs->config.index = false;
664                 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
665         }
666
667         return 0;
668
669 out_put:
670         path_put(path);
671 out:
672         return err;
673 }
674
675 /* Workdir should not be subdir of upperdir and vice versa */
676 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
677 {
678         bool ok = false;
679
680         if (workdir != upperdir) {
681                 ok = (lock_rename(workdir, upperdir) == NULL);
682                 unlock_rename(workdir, upperdir);
683         }
684         return ok;
685 }
686
687 static unsigned int ovl_split_lowerdirs(char *str)
688 {
689         unsigned int ctr = 1;
690         char *s, *d;
691
692         for (s = d = str;; s++, d++) {
693                 if (*s == '\\') {
694                         s++;
695                 } else if (*s == ':') {
696                         *d = '\0';
697                         ctr++;
698                         continue;
699                 }
700                 *d = *s;
701                 if (!*s)
702                         break;
703         }
704         return ctr;
705 }
706
707 static int __maybe_unused
708 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
709                         struct dentry *dentry, struct inode *inode,
710                         const char *name, void *buffer, size_t size)
711 {
712         return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
713 }
714
715 static int __maybe_unused
716 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
717                         struct dentry *dentry, struct inode *inode,
718                         const char *name, const void *value,
719                         size_t size, int flags)
720 {
721         struct dentry *workdir = ovl_workdir(dentry);
722         struct inode *realinode = ovl_inode_real(inode);
723         struct posix_acl *acl = NULL;
724         int err;
725
726         /* Check that everything is OK before copy-up */
727         if (value) {
728                 acl = posix_acl_from_xattr(&init_user_ns, value, size);
729                 if (IS_ERR(acl))
730                         return PTR_ERR(acl);
731         }
732         err = -EOPNOTSUPP;
733         if (!IS_POSIXACL(d_inode(workdir)))
734                 goto out_acl_release;
735         if (!realinode->i_op->set_acl)
736                 goto out_acl_release;
737         if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
738                 err = acl ? -EACCES : 0;
739                 goto out_acl_release;
740         }
741         err = -EPERM;
742         if (!inode_owner_or_capable(inode))
743                 goto out_acl_release;
744
745         posix_acl_release(acl);
746
747         /*
748          * Check if sgid bit needs to be cleared (actual setacl operation will
749          * be done with mounter's capabilities and so that won't do it for us).
750          */
751         if (unlikely(inode->i_mode & S_ISGID) &&
752             handler->flags == ACL_TYPE_ACCESS &&
753             !in_group_p(inode->i_gid) &&
754             !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
755                 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
756
757                 err = ovl_setattr(dentry, &iattr);
758                 if (err)
759                         return err;
760         }
761
762         err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
763         if (!err)
764                 ovl_copyattr(ovl_inode_real(inode), inode);
765
766         return err;
767
768 out_acl_release:
769         posix_acl_release(acl);
770         return err;
771 }
772
773 static int ovl_own_xattr_get(const struct xattr_handler *handler,
774                              struct dentry *dentry, struct inode *inode,
775                              const char *name, void *buffer, size_t size)
776 {
777         return -EOPNOTSUPP;
778 }
779
780 static int ovl_own_xattr_set(const struct xattr_handler *handler,
781                              struct dentry *dentry, struct inode *inode,
782                              const char *name, const void *value,
783                              size_t size, int flags)
784 {
785         return -EOPNOTSUPP;
786 }
787
788 static int ovl_other_xattr_get(const struct xattr_handler *handler,
789                                struct dentry *dentry, struct inode *inode,
790                                const char *name, void *buffer, size_t size)
791 {
792         return ovl_xattr_get(dentry, inode, name, buffer, size);
793 }
794
795 static int ovl_other_xattr_set(const struct xattr_handler *handler,
796                                struct dentry *dentry, struct inode *inode,
797                                const char *name, const void *value,
798                                size_t size, int flags)
799 {
800         return ovl_xattr_set(dentry, inode, name, value, size, flags);
801 }
802
803 static const struct xattr_handler __maybe_unused
804 ovl_posix_acl_access_xattr_handler = {
805         .name = XATTR_NAME_POSIX_ACL_ACCESS,
806         .flags = ACL_TYPE_ACCESS,
807         .get = ovl_posix_acl_xattr_get,
808         .set = ovl_posix_acl_xattr_set,
809 };
810
811 static const struct xattr_handler __maybe_unused
812 ovl_posix_acl_default_xattr_handler = {
813         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
814         .flags = ACL_TYPE_DEFAULT,
815         .get = ovl_posix_acl_xattr_get,
816         .set = ovl_posix_acl_xattr_set,
817 };
818
819 static const struct xattr_handler ovl_own_xattr_handler = {
820         .prefix = OVL_XATTR_PREFIX,
821         .get = ovl_own_xattr_get,
822         .set = ovl_own_xattr_set,
823 };
824
825 static const struct xattr_handler ovl_other_xattr_handler = {
826         .prefix = "", /* catch all */
827         .get = ovl_other_xattr_get,
828         .set = ovl_other_xattr_set,
829 };
830
831 static const struct xattr_handler *ovl_xattr_handlers[] = {
832 #ifdef CONFIG_FS_POSIX_ACL
833         &ovl_posix_acl_access_xattr_handler,
834         &ovl_posix_acl_default_xattr_handler,
835 #endif
836         &ovl_own_xattr_handler,
837         &ovl_other_xattr_handler,
838         NULL
839 };
840
841 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
842 {
843         struct path upperpath = { };
844         struct path workpath = { };
845         struct dentry *root_dentry;
846         struct ovl_entry *oe;
847         struct ovl_fs *ufs;
848         struct path *stack = NULL;
849         char *lowertmp;
850         char *lower;
851         unsigned int numlower;
852         unsigned int stacklen = 0;
853         unsigned int i;
854         bool remote = false;
855         struct cred *cred;
856         int err;
857
858         err = -ENOMEM;
859         ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
860         if (!ufs)
861                 goto out;
862
863         ufs->config.redirect_dir = ovl_redirect_dir_def;
864         ufs->config.index = ovl_index_def;
865         err = ovl_parse_opt((char *) data, &ufs->config);
866         if (err)
867                 goto out_free_config;
868
869         err = -EINVAL;
870         if (!ufs->config.lowerdir) {
871                 if (!silent)
872                         pr_err("overlayfs: missing 'lowerdir'\n");
873                 goto out_free_config;
874         }
875
876         sb->s_stack_depth = 0;
877         sb->s_maxbytes = MAX_LFS_FILESIZE;
878         if (ufs->config.upperdir) {
879                 if (!ufs->config.workdir) {
880                         pr_err("overlayfs: missing 'workdir'\n");
881                         goto out_free_config;
882                 }
883
884                 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
885                 if (err)
886                         goto out_free_config;
887
888                 /* Upper fs should not be r/o */
889                 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
890                         pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
891                         err = -EINVAL;
892                         goto out_put_upperpath;
893                 }
894
895                 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
896                 if (err)
897                         goto out_put_upperpath;
898
899                 err = -EBUSY;
900                 if (ovl_inuse_trylock(upperpath.dentry)) {
901                         ufs->upperdir_locked = true;
902                 } else if (ufs->config.index) {
903                         pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
904                         goto out_put_upperpath;
905                 } else {
906                         pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
907                 }
908
909                 err = ovl_mount_dir(ufs->config.workdir, &workpath);
910                 if (err)
911                         goto out_unlock_upperdentry;
912
913                 err = -EINVAL;
914                 if (upperpath.mnt != workpath.mnt) {
915                         pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
916                         goto out_put_workpath;
917                 }
918                 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
919                         pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
920                         goto out_put_workpath;
921                 }
922
923                 err = -EBUSY;
924                 if (ovl_inuse_trylock(workpath.dentry)) {
925                         ufs->workdir_locked = true;
926                 } else if (ufs->config.index) {
927                         pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
928                         goto out_put_workpath;
929                 } else {
930                         pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
931                 }
932
933                 ufs->workbasedir = workpath.dentry;
934                 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
935         }
936         err = -ENOMEM;
937         lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
938         if (!lowertmp)
939                 goto out_unlock_workdentry;
940
941         err = -EINVAL;
942         stacklen = ovl_split_lowerdirs(lowertmp);
943         if (stacklen > OVL_MAX_STACK) {
944                 pr_err("overlayfs: too many lower directories, limit is %d\n",
945                        OVL_MAX_STACK);
946                 goto out_free_lowertmp;
947         } else if (!ufs->config.upperdir && stacklen == 1) {
948                 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
949                 goto out_free_lowertmp;
950         }
951
952         err = -ENOMEM;
953         stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
954         if (!stack)
955                 goto out_free_lowertmp;
956
957         err = -EINVAL;
958         lower = lowertmp;
959         for (numlower = 0; numlower < stacklen; numlower++) {
960                 err = ovl_lower_dir(lower, &stack[numlower], ufs,
961                                     &sb->s_stack_depth, &remote);
962                 if (err)
963                         goto out_put_lowerpath;
964
965                 lower = strchr(lower, '\0') + 1;
966         }
967
968         err = -EINVAL;
969         sb->s_stack_depth++;
970         if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
971                 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
972                 goto out_put_lowerpath;
973         }
974
975         if (ufs->config.upperdir) {
976                 ufs->upper_mnt = clone_private_mount(&upperpath);
977                 err = PTR_ERR(ufs->upper_mnt);
978                 if (IS_ERR(ufs->upper_mnt)) {
979                         pr_err("overlayfs: failed to clone upperpath\n");
980                         goto out_put_lowerpath;
981                 }
982
983                 /* Don't inherit atime flags */
984                 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
985
986                 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
987
988                 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
989                                                   OVL_WORKDIR_NAME, false);
990                 /*
991                  * Upper should support d_type, else whiteouts are visible.
992                  * Given workdir and upper are on same fs, we can do
993                  * iterate_dir() on workdir. This check requires successful
994                  * creation of workdir in previous step.
995                  */
996                 if (ufs->workdir) {
997                         struct dentry *temp;
998
999                         err = ovl_check_d_type_supported(&workpath);
1000                         if (err < 0)
1001                                 goto out_put_workdir;
1002
1003                         /*
1004                          * We allowed this configuration and don't want to
1005                          * break users over kernel upgrade. So warn instead
1006                          * of erroring out.
1007                          */
1008                         if (!err)
1009                                 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1010
1011                         /* Check if upper/work fs supports O_TMPFILE */
1012                         temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
1013                         ufs->tmpfile = !IS_ERR(temp);
1014                         if (ufs->tmpfile)
1015                                 dput(temp);
1016                         else
1017                                 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1018
1019                         /*
1020                          * Check if upper/work fs supports trusted.overlay.*
1021                          * xattr
1022                          */
1023                         err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1024                                               "0", 1, 0);
1025                         if (err) {
1026                                 ufs->noxattr = true;
1027                                 pr_warn("overlayfs: upper fs does not support xattr.\n");
1028                         } else {
1029                                 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1030                         }
1031
1032                         /* Check if upper/work fs supports file handles */
1033                         if (ufs->config.index &&
1034                             !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1035                                 ufs->config.index = false;
1036                                 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1037                         }
1038                 }
1039         }
1040
1041         err = -ENOMEM;
1042         ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1043         if (ufs->lower_mnt == NULL)
1044                 goto out_put_workdir;
1045         for (i = 0; i < numlower; i++) {
1046                 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1047
1048                 err = PTR_ERR(mnt);
1049                 if (IS_ERR(mnt)) {
1050                         pr_err("overlayfs: failed to clone lowerpath\n");
1051                         goto out_put_lower_mnt;
1052                 }
1053                 /*
1054                  * Make lower_mnt R/O.  That way fchmod/fchown on lower file
1055                  * will fail instead of modifying lower fs.
1056                  */
1057                 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1058
1059                 ufs->lower_mnt[ufs->numlower] = mnt;
1060                 ufs->numlower++;
1061
1062                 /* Check if all lower layers are on same sb */
1063                 if (i == 0)
1064                         ufs->same_sb = mnt->mnt_sb;
1065                 else if (ufs->same_sb != mnt->mnt_sb)
1066                         ufs->same_sb = NULL;
1067         }
1068
1069         /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1070         if (!ufs->upper_mnt)
1071                 sb->s_flags |= MS_RDONLY;
1072         else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1073                 ufs->same_sb = NULL;
1074
1075         if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
1076                 /* Verify lower root is upper root origin */
1077                 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
1078                                         stack[0].dentry, false, true);
1079                 if (err) {
1080                         pr_err("overlayfs: failed to verify upper root origin\n");
1081                         goto out_put_lower_mnt;
1082                 }
1083
1084                 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1085                                                    OVL_INDEXDIR_NAME, true);
1086                 if (ufs->indexdir) {
1087                         /* Verify upper root is index dir origin */
1088                         err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1089                                                 upperpath.dentry, true, true);
1090                         if (err)
1091                                 pr_err("overlayfs: failed to verify index dir origin\n");
1092
1093                         /* Cleanup bad/stale/orphan index entries */
1094                         if (!err)
1095                                 err = ovl_indexdir_cleanup(ufs->indexdir,
1096                                                            ufs->upper_mnt,
1097                                                            stack, numlower);
1098                 }
1099                 if (err || !ufs->indexdir)
1100                         pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1101                 if (err)
1102                         goto out_put_indexdir;
1103         }
1104
1105         /* Show index=off/on in /proc/mounts for any of the reasons above */
1106         if (!ufs->indexdir)
1107                 ufs->config.index = false;
1108
1109         if (remote)
1110                 sb->s_d_op = &ovl_reval_dentry_operations;
1111         else
1112                 sb->s_d_op = &ovl_dentry_operations;
1113
1114         err = -ENOMEM;
1115         ufs->creator_cred = cred = prepare_creds();
1116         if (!cred)
1117                 goto out_put_indexdir;
1118
1119         /* Never override disk quota limits or use reserved space */
1120         cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1121
1122         err = -ENOMEM;
1123         oe = ovl_alloc_entry(numlower);
1124         if (!oe)
1125                 goto out_put_cred;
1126
1127         sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1128         sb->s_op = &ovl_super_operations;
1129         sb->s_xattr = ovl_xattr_handlers;
1130         sb->s_fs_info = ufs;
1131         sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1132
1133         root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1134         if (!root_dentry)
1135                 goto out_free_oe;
1136
1137         mntput(upperpath.mnt);
1138         for (i = 0; i < numlower; i++)
1139                 mntput(stack[i].mnt);
1140         mntput(workpath.mnt);
1141         kfree(lowertmp);
1142
1143         if (upperpath.dentry) {
1144                 oe->has_upper = true;
1145                 if (ovl_is_impuredir(upperpath.dentry))
1146                         ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1147         }
1148         for (i = 0; i < numlower; i++) {
1149                 oe->lowerstack[i].dentry = stack[i].dentry;
1150                 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1151         }
1152         kfree(stack);
1153
1154         root_dentry->d_fsdata = oe;
1155
1156         ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1157                        ovl_dentry_lower(root_dentry));
1158
1159         sb->s_root = root_dentry;
1160
1161         return 0;
1162
1163 out_free_oe:
1164         kfree(oe);
1165 out_put_cred:
1166         put_cred(ufs->creator_cred);
1167 out_put_indexdir:
1168         dput(ufs->indexdir);
1169 out_put_lower_mnt:
1170         for (i = 0; i < ufs->numlower; i++)
1171                 mntput(ufs->lower_mnt[i]);
1172         kfree(ufs->lower_mnt);
1173 out_put_workdir:
1174         dput(ufs->workdir);
1175         mntput(ufs->upper_mnt);
1176 out_put_lowerpath:
1177         for (i = 0; i < numlower; i++)
1178                 path_put(&stack[i]);
1179         kfree(stack);
1180 out_free_lowertmp:
1181         kfree(lowertmp);
1182 out_unlock_workdentry:
1183         if (ufs->workdir_locked)
1184                 ovl_inuse_unlock(workpath.dentry);
1185 out_put_workpath:
1186         path_put(&workpath);
1187 out_unlock_upperdentry:
1188         if (ufs->upperdir_locked)
1189                 ovl_inuse_unlock(upperpath.dentry);
1190 out_put_upperpath:
1191         path_put(&upperpath);
1192 out_free_config:
1193         kfree(ufs->config.lowerdir);
1194         kfree(ufs->config.upperdir);
1195         kfree(ufs->config.workdir);
1196         kfree(ufs);
1197 out:
1198         return err;
1199 }
1200
1201 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1202                                 const char *dev_name, void *raw_data)
1203 {
1204         return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1205 }
1206
1207 static struct file_system_type ovl_fs_type = {
1208         .owner          = THIS_MODULE,
1209         .name           = "overlay",
1210         .mount          = ovl_mount,
1211         .kill_sb        = kill_anon_super,
1212 };
1213 MODULE_ALIAS_FS("overlay");
1214
1215 static void ovl_inode_init_once(void *foo)
1216 {
1217         struct ovl_inode *oi = foo;
1218
1219         inode_init_once(&oi->vfs_inode);
1220 }
1221
1222 static int __init ovl_init(void)
1223 {
1224         int err;
1225
1226         ovl_inode_cachep = kmem_cache_create("ovl_inode",
1227                                              sizeof(struct ovl_inode), 0,
1228                                              (SLAB_RECLAIM_ACCOUNT|
1229                                               SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1230                                              ovl_inode_init_once);
1231         if (ovl_inode_cachep == NULL)
1232                 return -ENOMEM;
1233
1234         err = register_filesystem(&ovl_fs_type);
1235         if (err)
1236                 kmem_cache_destroy(ovl_inode_cachep);
1237
1238         return err;
1239 }
1240
1241 static void __exit ovl_exit(void)
1242 {
1243         unregister_filesystem(&ovl_fs_type);
1244
1245         /*
1246          * Make sure all delayed rcu free inodes are flushed before we
1247          * destroy cache.
1248          */
1249         rcu_barrier();
1250         kmem_cache_destroy(ovl_inode_cachep);
1251
1252 }
1253
1254 module_init(ovl_init);
1255 module_exit(ovl_exit);