HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / overlayfs / super.c
blobfa20c95bd45695a2d16fa346a046d7548c4e20fd
1 /*
3 * Copyright (C) 2011 Novell Inc.
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 */
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"
24 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
25 MODULE_DESCRIPTION("Overlay filesystem");
26 MODULE_LICENSE("GPL");
28 #define OVERLAYFS_SUPER_MAGIC 0x794c7630
30 struct ovl_config {
31 char *lowerdir;
32 char *upperdir;
33 char *workdir;
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;
49 struct ovl_dir_cache;
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;
60 struct rcu_head rcu;
62 unsigned numlower;
63 struct path lowerstack[];
66 #define OVL_MAX_STACK 500
68 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
70 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
73 enum ovl_path_type ovl_path_type(struct dentry *dentry)
75 struct ovl_entry *oe = dentry->d_fsdata;
76 enum ovl_path_type type = 0;
78 if (oe->__upperdentry) {
79 type = __OVL_PATH_UPPER;
82 * Non-dir dentry can hold lower dentry from previous
83 * location. Its purity depends only on opaque flag.
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;
93 return type;
96 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
98 return lockless_dereference(oe->__upperdentry);
101 void ovl_path_upper(struct dentry *dentry, struct path *path)
103 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
104 struct ovl_entry *oe = dentry->d_fsdata;
106 path->mnt = ofs->upper_mnt;
107 path->dentry = ovl_upperdentry_dereference(oe);
110 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
112 enum ovl_path_type type = ovl_path_type(dentry);
114 if (!OVL_TYPE_UPPER(type))
115 ovl_path_lower(dentry, path);
116 else
117 ovl_path_upper(dentry, path);
119 return type;
122 struct dentry *ovl_dentry_upper(struct dentry *dentry)
124 struct ovl_entry *oe = dentry->d_fsdata;
126 return ovl_upperdentry_dereference(oe);
129 struct dentry *ovl_dentry_lower(struct dentry *dentry)
131 struct ovl_entry *oe = dentry->d_fsdata;
133 return __ovl_dentry_lower(oe);
136 struct dentry *ovl_dentry_real(struct dentry *dentry)
138 struct ovl_entry *oe = dentry->d_fsdata;
139 struct dentry *realdentry;
141 realdentry = ovl_upperdentry_dereference(oe);
142 if (!realdentry)
143 realdentry = __ovl_dentry_lower(oe);
145 return realdentry;
148 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
150 struct dentry *realdentry;
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;
159 return realdentry;
162 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
164 struct ovl_entry *oe = dentry->d_fsdata;
166 return oe->cache;
169 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
171 struct ovl_entry *oe = dentry->d_fsdata;
173 oe->cache = cache;
176 void ovl_path_lower(struct dentry *dentry, struct path *path)
178 struct ovl_entry *oe = dentry->d_fsdata;
180 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
183 int ovl_want_write(struct dentry *dentry)
185 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
186 return mnt_want_write(ofs->upper_mnt);
189 void ovl_drop_write(struct dentry *dentry)
191 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
192 mnt_drop_write(ofs->upper_mnt);
195 struct dentry *ovl_workdir(struct dentry *dentry)
197 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
198 return ofs->workdir;
201 bool ovl_dentry_is_opaque(struct dentry *dentry)
203 struct ovl_entry *oe = dentry->d_fsdata;
204 return oe->opaque;
207 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
209 struct ovl_entry *oe = dentry->d_fsdata;
210 oe->opaque = opaque;
213 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
215 struct ovl_entry *oe = dentry->d_fsdata;
217 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
218 WARN_ON(oe->__upperdentry);
219 BUG_ON(!upperdentry->d_inode);
221 * Make sure upperdentry is consistent before making it visible to
222 * ovl_upperdentry_dereference().
224 smp_wmb();
225 oe->__upperdentry = upperdentry;
228 void ovl_dentry_version_inc(struct dentry *dentry)
230 struct ovl_entry *oe = dentry->d_fsdata;
232 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
233 oe->version++;
236 u64 ovl_dentry_version_get(struct dentry *dentry)
238 struct ovl_entry *oe = dentry->d_fsdata;
240 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
241 return oe->version;
244 bool ovl_is_whiteout(struct dentry *dentry)
246 struct inode *inode = dentry->d_inode;
248 return inode && IS_WHITEOUT(inode);
251 const struct cred *ovl_override_creds(struct super_block *sb)
253 struct ovl_fs *ofs = sb->s_fs_info;
255 return override_creds(ofs->creator_cred);
258 static bool ovl_is_opaquedir(struct dentry *dentry)
260 int res;
261 char val;
262 struct inode *inode = dentry->d_inode;
264 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
265 return false;
267 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
268 if (res == 1 && val == 'y')
269 return true;
271 return false;
274 static void ovl_dentry_release(struct dentry *dentry)
276 struct ovl_entry *oe = dentry->d_fsdata;
278 if (oe) {
279 unsigned int i;
281 dput(oe->__upperdentry);
282 for (i = 0; i < oe->numlower; i++)
283 dput(oe->lowerstack[i].dentry);
284 kfree_rcu(oe, rcu);
288 static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
290 struct dentry *real;
292 if (d_is_dir(dentry)) {
293 if (!inode || inode == d_inode(dentry))
294 return dentry;
295 goto bug;
298 real = ovl_dentry_upper(dentry);
299 if (real && (!inode || inode == d_inode(real)))
300 return real;
302 real = ovl_dentry_lower(dentry);
303 if (!real)
304 goto bug;
306 if (!inode || inode == d_inode(real))
307 return real;
309 /* Handle recursion */
310 if (real->d_flags & DCACHE_OP_REAL)
311 return real->d_op->d_real(real, inode);
313 bug:
314 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
315 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
316 return dentry;
319 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
321 struct ovl_entry *oe = dentry->d_fsdata;
322 unsigned int i;
323 int ret = 1;
325 for (i = 0; i < oe->numlower; i++) {
326 struct dentry *d = oe->lowerstack[i].dentry;
328 if (d->d_flags & DCACHE_OP_REVALIDATE) {
329 ret = d->d_op->d_revalidate(d, flags);
330 if (ret < 0)
331 return ret;
332 if (!ret) {
333 if (!(flags & LOOKUP_RCU))
334 d_invalidate(d);
335 return -ESTALE;
339 return 1;
342 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
344 struct ovl_entry *oe = dentry->d_fsdata;
345 unsigned int i;
346 int ret = 1;
348 for (i = 0; i < oe->numlower; i++) {
349 struct dentry *d = oe->lowerstack[i].dentry;
351 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
352 ret = d->d_op->d_weak_revalidate(d, flags);
353 if (ret <= 0)
354 break;
357 return ret;
360 static const struct dentry_operations ovl_dentry_operations = {
361 .d_release = ovl_dentry_release,
362 .d_select_inode = ovl_d_select_inode,
363 .d_real = ovl_d_real,
366 static const struct dentry_operations ovl_reval_dentry_operations = {
367 .d_release = ovl_dentry_release,
368 .d_select_inode = ovl_d_select_inode,
369 .d_real = ovl_d_real,
370 .d_revalidate = ovl_dentry_revalidate,
371 .d_weak_revalidate = ovl_dentry_weak_revalidate,
374 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
376 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
377 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
379 if (oe)
380 oe->numlower = numlower;
382 return oe;
385 static bool ovl_dentry_remote(struct dentry *dentry)
387 return dentry->d_flags &
388 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
389 DCACHE_OP_REAL);
392 static bool ovl_dentry_weird(struct dentry *dentry)
394 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
395 DCACHE_MANAGE_TRANSIT |
396 DCACHE_OP_HASH |
397 DCACHE_OP_COMPARE);
400 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
401 struct qstr *name)
403 struct dentry *dentry;
405 mutex_lock(&dir->d_inode->i_mutex);
406 dentry = lookup_one_len(name->name, dir, name->len);
407 mutex_unlock(&dir->d_inode->i_mutex);
409 if (IS_ERR(dentry)) {
410 if (PTR_ERR(dentry) == -ENOENT)
411 dentry = NULL;
412 } else if (!dentry->d_inode) {
413 dput(dentry);
414 dentry = NULL;
415 } else if (ovl_dentry_weird(dentry)) {
416 dput(dentry);
417 /* Don't support traversing automounts and other weirdness */
418 dentry = ERR_PTR(-EREMOTE);
420 return dentry;
424 * Returns next layer in stack starting from top.
425 * Returns -1 if this is the last layer.
427 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
429 struct ovl_entry *oe = dentry->d_fsdata;
431 BUG_ON(idx < 0);
432 if (idx == 0) {
433 ovl_path_upper(dentry, path);
434 if (path->dentry)
435 return oe->numlower ? 1 : -1;
436 idx++;
438 BUG_ON(idx > oe->numlower);
439 *path = oe->lowerstack[idx - 1];
441 return (idx < oe->numlower) ? idx + 1 : -1;
444 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
445 unsigned int flags)
447 struct ovl_entry *oe;
448 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
449 struct path *stack = NULL;
450 struct dentry *upperdir, *upperdentry = NULL;
451 unsigned int ctr = 0;
452 struct inode *inode = NULL;
453 bool upperopaque = false;
454 struct dentry *this, *prev = NULL;
455 unsigned int i;
456 int err;
458 upperdir = ovl_upperdentry_dereference(poe);
459 if (upperdir) {
460 this = ovl_lookup_real(upperdir, &dentry->d_name);
461 err = PTR_ERR(this);
462 if (IS_ERR(this))
463 goto out;
465 if (this) {
466 if (unlikely(ovl_dentry_remote(this))) {
467 dput(this);
468 err = -EREMOTE;
469 goto out;
471 if (ovl_is_whiteout(this)) {
472 dput(this);
473 this = NULL;
474 upperopaque = true;
475 } else if (poe->numlower && ovl_is_opaquedir(this)) {
476 upperopaque = true;
479 upperdentry = prev = this;
482 if (!upperopaque && poe->numlower) {
483 err = -ENOMEM;
484 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
485 if (!stack)
486 goto out_put_upper;
489 for (i = 0; !upperopaque && i < poe->numlower; i++) {
490 bool opaque = false;
491 struct path lowerpath = poe->lowerstack[i];
493 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
494 err = PTR_ERR(this);
495 if (IS_ERR(this)) {
497 * If it's positive, then treat ENAMETOOLONG as ENOENT.
499 if (err == -ENAMETOOLONG && (upperdentry || ctr))
500 continue;
501 goto out_put;
503 if (!this)
504 continue;
505 if (ovl_is_whiteout(this)) {
506 dput(this);
507 break;
510 * Only makes sense to check opaque dir if this is not the
511 * lowermost layer.
513 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
514 opaque = true;
516 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
517 !S_ISDIR(this->d_inode->i_mode))) {
519 * FIXME: check for upper-opaqueness maybe better done
520 * in remove code.
522 if (prev == upperdentry)
523 upperopaque = true;
524 dput(this);
525 break;
528 * If this is a non-directory then stop here.
530 if (!S_ISDIR(this->d_inode->i_mode))
531 opaque = true;
533 stack[ctr].dentry = this;
534 stack[ctr].mnt = lowerpath.mnt;
535 ctr++;
536 prev = this;
537 if (opaque)
538 break;
541 oe = ovl_alloc_entry(ctr);
542 err = -ENOMEM;
543 if (!oe)
544 goto out_put;
546 if (upperdentry || ctr) {
547 struct dentry *realdentry;
549 realdentry = upperdentry ? upperdentry : stack[0].dentry;
551 err = -ENOMEM;
552 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
553 oe);
554 if (!inode)
555 goto out_free_oe;
556 ovl_copyattr(realdentry->d_inode, inode);
559 oe->opaque = upperopaque;
560 oe->__upperdentry = upperdentry;
561 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
562 kfree(stack);
563 dentry->d_fsdata = oe;
564 d_add(dentry, inode);
566 return NULL;
568 out_free_oe:
569 kfree(oe);
570 out_put:
571 for (i = 0; i < ctr; i++)
572 dput(stack[i].dentry);
573 kfree(stack);
574 out_put_upper:
575 dput(upperdentry);
576 out:
577 return ERR_PTR(err);
580 struct file *ovl_path_open(struct path *path, int flags)
582 return dentry_open(path, flags, current_cred());
585 static void ovl_put_super(struct super_block *sb)
587 struct ovl_fs *ufs = sb->s_fs_info;
588 unsigned i;
590 dput(ufs->workdir);
591 mntput(ufs->upper_mnt);
592 for (i = 0; i < ufs->numlower; i++)
593 mntput(ufs->lower_mnt[i]);
594 kfree(ufs->lower_mnt);
596 kfree(ufs->config.lowerdir);
597 kfree(ufs->config.upperdir);
598 kfree(ufs->config.workdir);
599 put_cred(ufs->creator_cred);
600 kfree(ufs);
604 * ovl_statfs
605 * @sb: The overlayfs super block
606 * @buf: The struct kstatfs to fill in with stats
608 * Get the filesystem statistics. As writes always target the upper layer
609 * filesystem pass the statfs to the upper filesystem (if it exists)
611 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
613 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
614 struct dentry *root_dentry = dentry->d_sb->s_root;
615 struct path path;
616 int err;
618 ovl_path_real(root_dentry, &path);
620 err = vfs_statfs(&path, buf);
621 if (!err) {
622 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
623 buf->f_type = OVERLAYFS_SUPER_MAGIC;
626 return err;
630 * ovl_show_options
632 * Prints the mount options for a given superblock.
633 * Returns zero; does not fail.
635 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
637 struct super_block *sb = dentry->d_sb;
638 struct ovl_fs *ufs = sb->s_fs_info;
640 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
641 if (ufs->config.upperdir) {
642 seq_show_option(m, "upperdir", ufs->config.upperdir);
643 seq_show_option(m, "workdir", ufs->config.workdir);
645 return 0;
648 static int ovl_remount(struct super_block *sb, int *flags, char *data)
650 struct ovl_fs *ufs = sb->s_fs_info;
652 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
653 return -EROFS;
655 return 0;
658 static const struct super_operations ovl_super_operations = {
659 .put_super = ovl_put_super,
660 .statfs = ovl_statfs,
661 .show_options = ovl_show_options,
662 .remount_fs = ovl_remount,
665 enum {
666 OPT_LOWERDIR,
667 OPT_UPPERDIR,
668 OPT_WORKDIR,
669 OPT_ERR,
672 static const match_table_t ovl_tokens = {
673 {OPT_LOWERDIR, "lowerdir=%s"},
674 {OPT_UPPERDIR, "upperdir=%s"},
675 {OPT_WORKDIR, "workdir=%s"},
676 {OPT_ERR, NULL}
679 static char *ovl_next_opt(char **s)
681 char *sbegin = *s;
682 char *p;
684 if (sbegin == NULL)
685 return NULL;
687 for (p = sbegin; *p; p++) {
688 if (*p == '\\') {
689 p++;
690 if (!*p)
691 break;
692 } else if (*p == ',') {
693 *p = '\0';
694 *s = p + 1;
695 return sbegin;
698 *s = NULL;
699 return sbegin;
702 static int ovl_parse_opt(char *opt, struct ovl_config *config)
704 char *p;
706 while ((p = ovl_next_opt(&opt)) != NULL) {
707 int token;
708 substring_t args[MAX_OPT_ARGS];
710 if (!*p)
711 continue;
713 token = match_token(p, ovl_tokens, args);
714 switch (token) {
715 case OPT_UPPERDIR:
716 kfree(config->upperdir);
717 config->upperdir = match_strdup(&args[0]);
718 if (!config->upperdir)
719 return -ENOMEM;
720 break;
722 case OPT_LOWERDIR:
723 kfree(config->lowerdir);
724 config->lowerdir = match_strdup(&args[0]);
725 if (!config->lowerdir)
726 return -ENOMEM;
727 break;
729 case OPT_WORKDIR:
730 kfree(config->workdir);
731 config->workdir = match_strdup(&args[0]);
732 if (!config->workdir)
733 return -ENOMEM;
734 break;
736 default:
737 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
738 return -EINVAL;
742 /* Workdir is useless in non-upper mount */
743 if (!config->upperdir && config->workdir) {
744 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
745 config->workdir);
746 kfree(config->workdir);
747 config->workdir = NULL;
750 return 0;
753 #define OVL_WORKDIR_NAME "work"
755 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
756 struct dentry *dentry)
758 struct inode *dir = dentry->d_inode;
759 struct dentry *work;
760 int err;
761 bool retried = false;
763 err = mnt_want_write(mnt);
764 if (err)
765 return ERR_PTR(err);
767 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
768 retry:
769 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
770 strlen(OVL_WORKDIR_NAME));
772 if (!IS_ERR(work)) {
773 struct kstat stat = {
774 .mode = S_IFDIR | 0,
776 struct iattr attr = {
777 .ia_valid = ATTR_MODE,
778 .ia_mode = stat.mode,
781 if (work->d_inode) {
782 err = -EEXIST;
783 if (retried)
784 goto out_dput;
786 retried = true;
787 ovl_workdir_cleanup(dir, mnt, work, 0);
788 dput(work);
789 goto retry;
792 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
793 if (err)
794 goto out_dput;
796 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
797 if (err && err != -ENODATA && err != -EOPNOTSUPP)
798 goto out_dput;
800 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
801 if (err && err != -ENODATA && err != -EOPNOTSUPP)
802 goto out_dput;
804 /* Clear any inherited mode bits */
805 inode_lock(work->d_inode);
806 err = notify_change(work, &attr, NULL);
807 inode_unlock(work->d_inode);
808 if (err)
809 goto out_dput;
811 out_unlock:
812 mutex_unlock(&dir->i_mutex);
813 mnt_drop_write(mnt);
815 return work;
817 out_dput:
818 dput(work);
819 work = ERR_PTR(err);
820 goto out_unlock;
823 static void ovl_unescape(char *s)
825 char *d = s;
827 for (;; s++, d++) {
828 if (*s == '\\')
829 s++;
830 *d = *s;
831 if (!*s)
832 break;
836 static int ovl_mount_dir_noesc(const char *name, struct path *path)
838 int err = -EINVAL;
840 if (!*name) {
841 pr_err("overlayfs: empty lowerdir\n");
842 goto out;
844 err = kern_path(name, LOOKUP_FOLLOW, path);
845 if (err) {
846 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
847 goto out;
849 err = -EINVAL;
850 if (ovl_dentry_weird(path->dentry)) {
851 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
852 goto out_put;
854 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
855 pr_err("overlayfs: '%s' not a directory\n", name);
856 goto out_put;
858 return 0;
860 out_put:
861 path_put(path);
862 out:
863 return err;
866 static int ovl_mount_dir(const char *name, struct path *path)
868 int err = -ENOMEM;
869 char *tmp = kstrdup(name, GFP_KERNEL);
871 if (tmp) {
872 ovl_unescape(tmp);
873 err = ovl_mount_dir_noesc(tmp, path);
875 if (!err)
876 if (ovl_dentry_remote(path->dentry)) {
877 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
878 tmp);
879 path_put(path);
880 err = -EINVAL;
882 kfree(tmp);
884 return err;
887 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
888 int *stack_depth, bool *remote)
890 int err;
891 struct kstatfs statfs;
893 err = ovl_mount_dir_noesc(name, path);
894 if (err)
895 goto out;
897 err = vfs_statfs(path, &statfs);
898 if (err) {
899 pr_err("overlayfs: statfs failed on '%s'\n", name);
900 goto out_put;
902 *namelen = max(*namelen, statfs.f_namelen);
903 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
905 if (ovl_dentry_remote(path->dentry))
906 *remote = true;
908 return 0;
910 out_put:
911 path_put(path);
912 out:
913 return err;
916 /* Workdir should not be subdir of upperdir and vice versa */
917 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
919 bool ok = false;
921 if (workdir != upperdir) {
922 ok = (lock_rename(workdir, upperdir) == NULL);
923 unlock_rename(workdir, upperdir);
925 return ok;
928 static unsigned int ovl_split_lowerdirs(char *str)
930 unsigned int ctr = 1;
931 char *s, *d;
933 for (s = d = str;; s++, d++) {
934 if (*s == '\\') {
935 s++;
936 } else if (*s == ':') {
937 *d = '\0';
938 ctr++;
939 continue;
941 *d = *s;
942 if (!*s)
943 break;
945 return ctr;
948 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
950 struct path upperpath = { NULL, NULL };
951 struct path workpath = { NULL, NULL };
952 struct dentry *root_dentry;
953 struct ovl_entry *oe;
954 struct ovl_fs *ufs;
955 struct path *stack = NULL;
956 char *lowertmp;
957 char *lower;
958 unsigned int numlower;
959 unsigned int stacklen = 0;
960 unsigned int i;
961 bool remote = false;
962 int err;
964 err = -ENOMEM;
965 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
966 if (!ufs)
967 goto out;
969 err = ovl_parse_opt((char *) data, &ufs->config);
970 if (err)
971 goto out_free_config;
973 err = -EINVAL;
974 if (!ufs->config.lowerdir) {
975 pr_err("overlayfs: missing 'lowerdir'\n");
976 goto out_free_config;
979 sb->s_stack_depth = 0;
980 sb->s_maxbytes = MAX_LFS_FILESIZE;
981 if (ufs->config.upperdir) {
982 if (!ufs->config.workdir) {
983 pr_err("overlayfs: missing 'workdir'\n");
984 goto out_free_config;
987 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
988 if (err)
989 goto out_free_config;
991 /* Upper fs should not be r/o */
992 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
993 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
994 err = -EINVAL;
995 goto out_put_upperpath;
998 err = ovl_mount_dir(ufs->config.workdir, &workpath);
999 if (err)
1000 goto out_put_upperpath;
1002 err = -EINVAL;
1003 if (upperpath.mnt != workpath.mnt) {
1004 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1005 goto out_put_workpath;
1007 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1008 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1009 goto out_put_workpath;
1011 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1013 err = -ENOMEM;
1014 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1015 if (!lowertmp)
1016 goto out_put_workpath;
1018 err = -EINVAL;
1019 stacklen = ovl_split_lowerdirs(lowertmp);
1020 if (stacklen > OVL_MAX_STACK) {
1021 pr_err("overlayfs: too many lower directries, limit is %d\n",
1022 OVL_MAX_STACK);
1023 goto out_free_lowertmp;
1024 } else if (!ufs->config.upperdir && stacklen == 1) {
1025 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1026 goto out_free_lowertmp;
1029 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1030 if (!stack)
1031 goto out_free_lowertmp;
1033 lower = lowertmp;
1034 for (numlower = 0; numlower < stacklen; numlower++) {
1035 err = ovl_lower_dir(lower, &stack[numlower],
1036 &ufs->lower_namelen, &sb->s_stack_depth,
1037 &remote);
1038 if (err)
1039 goto out_put_lowerpath;
1041 lower = strchr(lower, '\0') + 1;
1044 err = -EINVAL;
1045 sb->s_stack_depth++;
1046 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1047 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1048 goto out_put_lowerpath;
1051 if (ufs->config.upperdir) {
1052 ufs->upper_mnt = clone_private_mount(&upperpath);
1053 err = PTR_ERR(ufs->upper_mnt);
1054 if (IS_ERR(ufs->upper_mnt)) {
1055 pr_err("overlayfs: failed to clone upperpath\n");
1056 goto out_put_lowerpath;
1059 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1060 err = PTR_ERR(ufs->workdir);
1061 if (IS_ERR(ufs->workdir)) {
1062 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1063 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1064 sb->s_flags |= MS_RDONLY;
1065 ufs->workdir = NULL;
1069 * Upper should support d_type, else whiteouts are visible.
1070 * Given workdir and upper are on same fs, we can do
1071 * iterate_dir() on workdir. This check requires successful
1072 * creation of workdir in previous step.
1074 if (ufs->workdir) {
1075 err = ovl_check_d_type_supported(&workpath);
1076 if (err < 0)
1077 goto out_put_workdir;
1080 * We allowed this configuration and don't want to
1081 * break users over kernel upgrade. So warn instead
1082 * of erroring out.
1084 if (!err)
1085 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1089 err = -ENOMEM;
1090 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1091 if (ufs->lower_mnt == NULL)
1092 goto out_put_workdir;
1093 for (i = 0; i < numlower; i++) {
1094 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1096 err = PTR_ERR(mnt);
1097 if (IS_ERR(mnt)) {
1098 pr_err("overlayfs: failed to clone lowerpath\n");
1099 goto out_put_lower_mnt;
1102 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1103 * will fail instead of modifying lower fs.
1105 mnt->mnt_flags |= MNT_READONLY;
1107 ufs->lower_mnt[ufs->numlower] = mnt;
1108 ufs->numlower++;
1111 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1112 if (!ufs->upper_mnt)
1113 sb->s_flags |= MS_RDONLY;
1115 if (remote)
1116 sb->s_d_op = &ovl_reval_dentry_operations;
1117 else
1118 sb->s_d_op = &ovl_dentry_operations;
1120 ufs->creator_cred = prepare_creds();
1121 if (!ufs->creator_cred)
1122 goto out_put_lower_mnt;
1124 err = -ENOMEM;
1125 oe = ovl_alloc_entry(numlower);
1126 if (!oe)
1127 goto out_put_cred;
1129 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
1130 if (!root_dentry)
1131 goto out_free_oe;
1133 mntput(upperpath.mnt);
1134 for (i = 0; i < numlower; i++)
1135 mntput(stack[i].mnt);
1136 path_put(&workpath);
1137 kfree(lowertmp);
1139 oe->__upperdentry = upperpath.dentry;
1140 for (i = 0; i < numlower; i++) {
1141 oe->lowerstack[i].dentry = stack[i].dentry;
1142 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1144 kfree(stack);
1146 root_dentry->d_fsdata = oe;
1148 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1149 root_dentry->d_inode);
1151 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1152 sb->s_op = &ovl_super_operations;
1153 sb->s_root = root_dentry;
1154 sb->s_fs_info = ufs;
1156 return 0;
1158 out_free_oe:
1159 kfree(oe);
1160 out_put_cred:
1161 put_cred(ufs->creator_cred);
1162 out_put_lower_mnt:
1163 for (i = 0; i < ufs->numlower; i++)
1164 mntput(ufs->lower_mnt[i]);
1165 kfree(ufs->lower_mnt);
1166 out_put_workdir:
1167 dput(ufs->workdir);
1168 mntput(ufs->upper_mnt);
1169 out_put_lowerpath:
1170 for (i = 0; i < numlower; i++)
1171 path_put(&stack[i]);
1172 kfree(stack);
1173 out_free_lowertmp:
1174 kfree(lowertmp);
1175 out_put_workpath:
1176 path_put(&workpath);
1177 out_put_upperpath:
1178 path_put(&upperpath);
1179 out_free_config:
1180 kfree(ufs->config.lowerdir);
1181 kfree(ufs->config.upperdir);
1182 kfree(ufs->config.workdir);
1183 kfree(ufs);
1184 out:
1185 return err;
1188 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1189 const char *dev_name, void *raw_data)
1191 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1194 static struct file_system_type ovl_fs_type = {
1195 .owner = THIS_MODULE,
1196 .name = "overlay",
1197 .mount = ovl_mount,
1198 .kill_sb = kill_anon_super,
1200 MODULE_ALIAS_FS("overlay");
1202 static int __init ovl_init(void)
1204 return register_filesystem(&ovl_fs_type);
1207 static void __exit ovl_exit(void)
1209 unregister_filesystem(&ovl_fs_type);
1212 module_init(ovl_init);
1213 module_exit(ovl_exit);