Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / fs / overlayfs / super.c
blob5d972e6cd3fe97fcae5c782b59c72c0007668566
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/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/statfs.h>
22 #include <linux/seq_file.h>
23 #include "overlayfs.h"
25 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26 MODULE_DESCRIPTION("Overlay filesystem");
27 MODULE_LICENSE("GPL");
29 struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
33 bool default_permissions;
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;
47 struct ovl_dir_cache;
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;
58 struct rcu_head rcu;
60 unsigned numlower;
61 struct path lowerstack[];
64 #define OVL_MAX_STACK 500
66 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
68 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
71 enum ovl_path_type ovl_path_type(struct dentry *dentry)
73 struct ovl_entry *oe = dentry->d_fsdata;
74 enum ovl_path_type type = 0;
76 if (oe->__upperdentry) {
77 type = __OVL_PATH_UPPER;
80 * Non-dir dentry can hold lower dentry from previous
81 * location. Its purity depends only on opaque flag.
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;
91 return type;
94 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
96 return lockless_dereference(oe->__upperdentry);
99 void ovl_path_upper(struct dentry *dentry, struct path *path)
101 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
102 struct ovl_entry *oe = dentry->d_fsdata;
104 path->mnt = ofs->upper_mnt;
105 path->dentry = ovl_upperdentry_dereference(oe);
108 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
110 enum ovl_path_type type = ovl_path_type(dentry);
112 if (!OVL_TYPE_UPPER(type))
113 ovl_path_lower(dentry, path);
114 else
115 ovl_path_upper(dentry, path);
117 return type;
120 struct dentry *ovl_dentry_upper(struct dentry *dentry)
122 struct ovl_entry *oe = dentry->d_fsdata;
124 return ovl_upperdentry_dereference(oe);
127 struct dentry *ovl_dentry_lower(struct dentry *dentry)
129 struct ovl_entry *oe = dentry->d_fsdata;
131 return __ovl_dentry_lower(oe);
134 struct dentry *ovl_dentry_real(struct dentry *dentry)
136 struct ovl_entry *oe = dentry->d_fsdata;
137 struct dentry *realdentry;
139 realdentry = ovl_upperdentry_dereference(oe);
140 if (!realdentry)
141 realdentry = __ovl_dentry_lower(oe);
143 return realdentry;
146 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
148 struct dentry *realdentry;
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;
157 return realdentry;
160 struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
161 bool is_upper)
163 if (is_upper) {
164 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
166 return ofs->upper_mnt;
167 } else {
168 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
172 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
174 struct ovl_entry *oe = dentry->d_fsdata;
176 return oe->cache;
179 bool ovl_is_default_permissions(struct inode *inode)
181 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
183 return ofs->config.default_permissions;
186 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
188 struct ovl_entry *oe = dentry->d_fsdata;
190 oe->cache = cache;
193 void ovl_path_lower(struct dentry *dentry, struct path *path)
195 struct ovl_entry *oe = dentry->d_fsdata;
197 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
200 int ovl_want_write(struct dentry *dentry)
202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
203 return mnt_want_write(ofs->upper_mnt);
206 void ovl_drop_write(struct dentry *dentry)
208 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
209 mnt_drop_write(ofs->upper_mnt);
212 struct dentry *ovl_workdir(struct dentry *dentry)
214 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
215 return ofs->workdir;
218 bool ovl_dentry_is_opaque(struct dentry *dentry)
220 struct ovl_entry *oe = dentry->d_fsdata;
221 return oe->opaque;
224 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
226 struct ovl_entry *oe = dentry->d_fsdata;
227 oe->opaque = opaque;
230 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
232 struct ovl_entry *oe = dentry->d_fsdata;
234 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
235 WARN_ON(oe->__upperdentry);
236 BUG_ON(!upperdentry->d_inode);
238 * Make sure upperdentry is consistent before making it visible to
239 * ovl_upperdentry_dereference().
241 smp_wmb();
242 oe->__upperdentry = upperdentry;
245 void ovl_dentry_version_inc(struct dentry *dentry)
247 struct ovl_entry *oe = dentry->d_fsdata;
249 WARN_ON(!inode_is_locked(dentry->d_inode));
250 oe->version++;
253 u64 ovl_dentry_version_get(struct dentry *dentry)
255 struct ovl_entry *oe = dentry->d_fsdata;
257 WARN_ON(!inode_is_locked(dentry->d_inode));
258 return oe->version;
261 bool ovl_is_whiteout(struct dentry *dentry)
263 struct inode *inode = dentry->d_inode;
265 return inode && IS_WHITEOUT(inode);
268 static bool ovl_is_opaquedir(struct dentry *dentry)
270 int res;
271 char val;
272 struct inode *inode = dentry->d_inode;
274 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
275 return false;
277 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
278 if (res == 1 && val == 'y')
279 return true;
281 return false;
284 static void ovl_dentry_release(struct dentry *dentry)
286 struct ovl_entry *oe = dentry->d_fsdata;
288 if (oe) {
289 unsigned int i;
291 dput(oe->__upperdentry);
292 for (i = 0; i < oe->numlower; i++)
293 dput(oe->lowerstack[i].dentry);
294 kfree_rcu(oe, rcu);
298 static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
300 struct dentry *real;
302 if (d_is_dir(dentry)) {
303 if (!inode || inode == d_inode(dentry))
304 return dentry;
305 goto bug;
308 real = ovl_dentry_upper(dentry);
309 if (real && (!inode || inode == d_inode(real)))
310 return real;
312 real = ovl_dentry_lower(dentry);
313 if (!real)
314 goto bug;
316 if (!inode || inode == d_inode(real))
317 return real;
319 /* Handle recursion */
320 if (real->d_flags & DCACHE_OP_REAL)
321 return real->d_op->d_real(real, inode);
323 bug:
324 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
325 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
326 return dentry;
329 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
331 struct ovl_entry *oe = dentry->d_fsdata;
332 unsigned int i;
333 int ret = 1;
335 for (i = 0; i < oe->numlower; i++) {
336 struct dentry *d = oe->lowerstack[i].dentry;
338 if (d->d_flags & DCACHE_OP_REVALIDATE) {
339 ret = d->d_op->d_revalidate(d, flags);
340 if (ret < 0)
341 return ret;
342 if (!ret) {
343 if (!(flags & LOOKUP_RCU))
344 d_invalidate(d);
345 return -ESTALE;
349 return 1;
352 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
354 struct ovl_entry *oe = dentry->d_fsdata;
355 unsigned int i;
356 int ret = 1;
358 for (i = 0; i < oe->numlower; i++) {
359 struct dentry *d = oe->lowerstack[i].dentry;
361 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
362 ret = d->d_op->d_weak_revalidate(d, flags);
363 if (ret <= 0)
364 break;
367 return ret;
370 static const struct dentry_operations ovl_dentry_operations = {
371 .d_release = ovl_dentry_release,
372 .d_select_inode = ovl_d_select_inode,
373 .d_real = ovl_d_real,
376 static const struct dentry_operations ovl_reval_dentry_operations = {
377 .d_release = ovl_dentry_release,
378 .d_select_inode = ovl_d_select_inode,
379 .d_real = ovl_d_real,
380 .d_revalidate = ovl_dentry_revalidate,
381 .d_weak_revalidate = ovl_dentry_weak_revalidate,
384 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
386 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
387 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
389 if (oe)
390 oe->numlower = numlower;
392 return oe;
395 static bool ovl_dentry_remote(struct dentry *dentry)
397 return dentry->d_flags &
398 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
401 static bool ovl_dentry_weird(struct dentry *dentry)
403 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
404 DCACHE_MANAGE_TRANSIT |
405 DCACHE_OP_HASH |
406 DCACHE_OP_COMPARE);
409 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
410 struct qstr *name)
412 struct dentry *dentry;
414 inode_lock(dir->d_inode);
415 dentry = lookup_one_len(name->name, dir, name->len);
416 inode_unlock(dir->d_inode);
418 if (IS_ERR(dentry)) {
419 if (PTR_ERR(dentry) == -ENOENT)
420 dentry = NULL;
421 } else if (!dentry->d_inode) {
422 dput(dentry);
423 dentry = NULL;
424 } else if (ovl_dentry_weird(dentry)) {
425 dput(dentry);
426 /* Don't support traversing automounts and other weirdness */
427 dentry = ERR_PTR(-EREMOTE);
429 return dentry;
433 * Returns next layer in stack starting from top.
434 * Returns -1 if this is the last layer.
436 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
438 struct ovl_entry *oe = dentry->d_fsdata;
440 BUG_ON(idx < 0);
441 if (idx == 0) {
442 ovl_path_upper(dentry, path);
443 if (path->dentry)
444 return oe->numlower ? 1 : -1;
445 idx++;
447 BUG_ON(idx > oe->numlower);
448 *path = oe->lowerstack[idx - 1];
450 return (idx < oe->numlower) ? idx + 1 : -1;
453 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
454 unsigned int flags)
456 struct ovl_entry *oe;
457 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
458 struct path *stack = NULL;
459 struct dentry *upperdir, *upperdentry = NULL;
460 unsigned int ctr = 0;
461 struct inode *inode = NULL;
462 bool upperopaque = false;
463 struct dentry *this, *prev = NULL;
464 unsigned int i;
465 int err;
467 upperdir = ovl_upperdentry_dereference(poe);
468 if (upperdir) {
469 this = ovl_lookup_real(upperdir, &dentry->d_name);
470 err = PTR_ERR(this);
471 if (IS_ERR(this))
472 goto out;
474 if (this) {
475 if (unlikely(ovl_dentry_remote(this))) {
476 dput(this);
477 err = -EREMOTE;
478 goto out;
480 if (ovl_is_whiteout(this)) {
481 dput(this);
482 this = NULL;
483 upperopaque = true;
484 } else if (poe->numlower && ovl_is_opaquedir(this)) {
485 upperopaque = true;
488 upperdentry = prev = this;
491 if (!upperopaque && poe->numlower) {
492 err = -ENOMEM;
493 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
494 if (!stack)
495 goto out_put_upper;
498 for (i = 0; !upperopaque && i < poe->numlower; i++) {
499 bool opaque = false;
500 struct path lowerpath = poe->lowerstack[i];
502 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
503 err = PTR_ERR(this);
504 if (IS_ERR(this)) {
506 * If it's positive, then treat ENAMETOOLONG as ENOENT.
508 if (err == -ENAMETOOLONG && (upperdentry || ctr))
509 continue;
510 goto out_put;
512 if (!this)
513 continue;
514 if (ovl_is_whiteout(this)) {
515 dput(this);
516 break;
519 * Only makes sense to check opaque dir if this is not the
520 * lowermost layer.
522 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
523 opaque = true;
525 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
526 !S_ISDIR(this->d_inode->i_mode))) {
528 * FIXME: check for upper-opaqueness maybe better done
529 * in remove code.
531 if (prev == upperdentry)
532 upperopaque = true;
533 dput(this);
534 break;
537 * If this is a non-directory then stop here.
539 if (!S_ISDIR(this->d_inode->i_mode))
540 opaque = true;
542 stack[ctr].dentry = this;
543 stack[ctr].mnt = lowerpath.mnt;
544 ctr++;
545 prev = this;
546 if (opaque)
547 break;
550 oe = ovl_alloc_entry(ctr);
551 err = -ENOMEM;
552 if (!oe)
553 goto out_put;
555 if (upperdentry || ctr) {
556 struct dentry *realdentry;
558 realdentry = upperdentry ? upperdentry : stack[0].dentry;
560 err = -ENOMEM;
561 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
562 oe);
563 if (!inode)
564 goto out_free_oe;
565 ovl_copyattr(realdentry->d_inode, inode);
568 oe->opaque = upperopaque;
569 oe->__upperdentry = upperdentry;
570 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
571 kfree(stack);
572 dentry->d_fsdata = oe;
573 d_add(dentry, inode);
575 return NULL;
577 out_free_oe:
578 kfree(oe);
579 out_put:
580 for (i = 0; i < ctr; i++)
581 dput(stack[i].dentry);
582 kfree(stack);
583 out_put_upper:
584 dput(upperdentry);
585 out:
586 return ERR_PTR(err);
589 struct file *ovl_path_open(struct path *path, int flags)
591 return dentry_open(path, flags, current_cred());
594 static void ovl_put_super(struct super_block *sb)
596 struct ovl_fs *ufs = sb->s_fs_info;
597 unsigned i;
599 dput(ufs->workdir);
600 mntput(ufs->upper_mnt);
601 for (i = 0; i < ufs->numlower; i++)
602 mntput(ufs->lower_mnt[i]);
603 kfree(ufs->lower_mnt);
605 kfree(ufs->config.lowerdir);
606 kfree(ufs->config.upperdir);
607 kfree(ufs->config.workdir);
608 kfree(ufs);
612 * ovl_statfs
613 * @sb: The overlayfs super block
614 * @buf: The struct kstatfs to fill in with stats
616 * Get the filesystem statistics. As writes always target the upper layer
617 * filesystem pass the statfs to the upper filesystem (if it exists)
619 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
621 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
622 struct dentry *root_dentry = dentry->d_sb->s_root;
623 struct path path;
624 int err;
626 ovl_path_real(root_dentry, &path);
628 err = vfs_statfs(&path, buf);
629 if (!err) {
630 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
631 buf->f_type = OVERLAYFS_SUPER_MAGIC;
634 return err;
638 * ovl_show_options
640 * Prints the mount options for a given superblock.
641 * Returns zero; does not fail.
643 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
645 struct super_block *sb = dentry->d_sb;
646 struct ovl_fs *ufs = sb->s_fs_info;
648 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
649 if (ufs->config.upperdir) {
650 seq_show_option(m, "upperdir", ufs->config.upperdir);
651 seq_show_option(m, "workdir", ufs->config.workdir);
653 if (ufs->config.default_permissions)
654 seq_puts(m, ",default_permissions");
655 return 0;
658 static int ovl_remount(struct super_block *sb, int *flags, char *data)
660 struct ovl_fs *ufs = sb->s_fs_info;
662 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
663 return -EROFS;
665 return 0;
668 static const struct super_operations ovl_super_operations = {
669 .put_super = ovl_put_super,
670 .statfs = ovl_statfs,
671 .show_options = ovl_show_options,
672 .remount_fs = ovl_remount,
675 enum {
676 OPT_LOWERDIR,
677 OPT_UPPERDIR,
678 OPT_WORKDIR,
679 OPT_DEFAULT_PERMISSIONS,
680 OPT_ERR,
683 static const match_table_t ovl_tokens = {
684 {OPT_LOWERDIR, "lowerdir=%s"},
685 {OPT_UPPERDIR, "upperdir=%s"},
686 {OPT_WORKDIR, "workdir=%s"},
687 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
688 {OPT_ERR, NULL}
691 static char *ovl_next_opt(char **s)
693 char *sbegin = *s;
694 char *p;
696 if (sbegin == NULL)
697 return NULL;
699 for (p = sbegin; *p; p++) {
700 if (*p == '\\') {
701 p++;
702 if (!*p)
703 break;
704 } else if (*p == ',') {
705 *p = '\0';
706 *s = p + 1;
707 return sbegin;
710 *s = NULL;
711 return sbegin;
714 static int ovl_parse_opt(char *opt, struct ovl_config *config)
716 char *p;
718 while ((p = ovl_next_opt(&opt)) != NULL) {
719 int token;
720 substring_t args[MAX_OPT_ARGS];
722 if (!*p)
723 continue;
725 token = match_token(p, ovl_tokens, args);
726 switch (token) {
727 case OPT_UPPERDIR:
728 kfree(config->upperdir);
729 config->upperdir = match_strdup(&args[0]);
730 if (!config->upperdir)
731 return -ENOMEM;
732 break;
734 case OPT_LOWERDIR:
735 kfree(config->lowerdir);
736 config->lowerdir = match_strdup(&args[0]);
737 if (!config->lowerdir)
738 return -ENOMEM;
739 break;
741 case OPT_WORKDIR:
742 kfree(config->workdir);
743 config->workdir = match_strdup(&args[0]);
744 if (!config->workdir)
745 return -ENOMEM;
746 break;
748 case OPT_DEFAULT_PERMISSIONS:
749 config->default_permissions = true;
750 break;
752 default:
753 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
754 return -EINVAL;
758 /* Workdir is useless in non-upper mount */
759 if (!config->upperdir && config->workdir) {
760 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
761 config->workdir);
762 kfree(config->workdir);
763 config->workdir = NULL;
766 return 0;
769 #define OVL_WORKDIR_NAME "work"
771 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
772 struct dentry *dentry)
774 struct inode *dir = dentry->d_inode;
775 struct dentry *work;
776 int err;
777 bool retried = false;
779 err = mnt_want_write(mnt);
780 if (err)
781 return ERR_PTR(err);
783 inode_lock_nested(dir, I_MUTEX_PARENT);
784 retry:
785 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
786 strlen(OVL_WORKDIR_NAME));
788 if (!IS_ERR(work)) {
789 struct kstat stat = {
790 .mode = S_IFDIR | 0,
793 if (work->d_inode) {
794 err = -EEXIST;
795 if (retried)
796 goto out_dput;
798 retried = true;
799 ovl_cleanup(dir, work);
800 dput(work);
801 goto retry;
804 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
805 if (err)
806 goto out_dput;
808 out_unlock:
809 inode_unlock(dir);
810 mnt_drop_write(mnt);
812 return work;
814 out_dput:
815 dput(work);
816 work = ERR_PTR(err);
817 goto out_unlock;
820 static void ovl_unescape(char *s)
822 char *d = s;
824 for (;; s++, d++) {
825 if (*s == '\\')
826 s++;
827 *d = *s;
828 if (!*s)
829 break;
833 static int ovl_mount_dir_noesc(const char *name, struct path *path)
835 int err = -EINVAL;
837 if (!*name) {
838 pr_err("overlayfs: empty lowerdir\n");
839 goto out;
841 err = kern_path(name, LOOKUP_FOLLOW, path);
842 if (err) {
843 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
844 goto out;
846 err = -EINVAL;
847 if (ovl_dentry_weird(path->dentry)) {
848 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
849 goto out_put;
851 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
852 pr_err("overlayfs: '%s' not a directory\n", name);
853 goto out_put;
855 return 0;
857 out_put:
858 path_put(path);
859 out:
860 return err;
863 static int ovl_mount_dir(const char *name, struct path *path)
865 int err = -ENOMEM;
866 char *tmp = kstrdup(name, GFP_KERNEL);
868 if (tmp) {
869 ovl_unescape(tmp);
870 err = ovl_mount_dir_noesc(tmp, path);
872 if (!err)
873 if (ovl_dentry_remote(path->dentry)) {
874 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
875 tmp);
876 path_put(path);
877 err = -EINVAL;
879 kfree(tmp);
881 return err;
884 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
885 int *stack_depth, bool *remote)
887 int err;
888 struct kstatfs statfs;
890 err = ovl_mount_dir_noesc(name, path);
891 if (err)
892 goto out;
894 err = vfs_statfs(path, &statfs);
895 if (err) {
896 pr_err("overlayfs: statfs failed on '%s'\n", name);
897 goto out_put;
899 *namelen = max(*namelen, statfs.f_namelen);
900 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
902 if (ovl_dentry_remote(path->dentry))
903 *remote = true;
905 return 0;
907 out_put:
908 path_put(path);
909 out:
910 return err;
913 /* Workdir should not be subdir of upperdir and vice versa */
914 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
916 bool ok = false;
918 if (workdir != upperdir) {
919 ok = (lock_rename(workdir, upperdir) == NULL);
920 unlock_rename(workdir, upperdir);
922 return ok;
925 static unsigned int ovl_split_lowerdirs(char *str)
927 unsigned int ctr = 1;
928 char *s, *d;
930 for (s = d = str;; s++, d++) {
931 if (*s == '\\') {
932 s++;
933 } else if (*s == ':') {
934 *d = '\0';
935 ctr++;
936 continue;
938 *d = *s;
939 if (!*s)
940 break;
942 return ctr;
945 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
947 struct path upperpath = { NULL, NULL };
948 struct path workpath = { NULL, NULL };
949 struct dentry *root_dentry;
950 struct ovl_entry *oe;
951 struct ovl_fs *ufs;
952 struct path *stack = NULL;
953 char *lowertmp;
954 char *lower;
955 unsigned int numlower;
956 unsigned int stacklen = 0;
957 unsigned int i;
958 bool remote = false;
959 int err;
961 err = -ENOMEM;
962 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
963 if (!ufs)
964 goto out;
966 err = ovl_parse_opt((char *) data, &ufs->config);
967 if (err)
968 goto out_free_config;
970 err = -EINVAL;
971 if (!ufs->config.lowerdir) {
972 if (!silent)
973 pr_err("overlayfs: missing 'lowerdir'\n");
974 goto out_free_config;
977 sb->s_stack_depth = 0;
978 sb->s_maxbytes = MAX_LFS_FILESIZE;
979 if (ufs->config.upperdir) {
980 if (!ufs->config.workdir) {
981 pr_err("overlayfs: missing 'workdir'\n");
982 goto out_free_config;
985 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
986 if (err)
987 goto out_free_config;
989 /* Upper fs should not be r/o */
990 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
991 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
992 err = -EINVAL;
993 goto out_put_upperpath;
996 err = ovl_mount_dir(ufs->config.workdir, &workpath);
997 if (err)
998 goto out_put_upperpath;
1000 err = -EINVAL;
1001 if (upperpath.mnt != workpath.mnt) {
1002 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1003 goto out_put_workpath;
1005 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1006 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1007 goto out_put_workpath;
1009 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
1011 err = -ENOMEM;
1012 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1013 if (!lowertmp)
1014 goto out_put_workpath;
1016 err = -EINVAL;
1017 stacklen = ovl_split_lowerdirs(lowertmp);
1018 if (stacklen > OVL_MAX_STACK) {
1019 pr_err("overlayfs: too many lower directries, limit is %d\n",
1020 OVL_MAX_STACK);
1021 goto out_free_lowertmp;
1022 } else if (!ufs->config.upperdir && stacklen == 1) {
1023 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1024 goto out_free_lowertmp;
1027 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1028 if (!stack)
1029 goto out_free_lowertmp;
1031 lower = lowertmp;
1032 for (numlower = 0; numlower < stacklen; numlower++) {
1033 err = ovl_lower_dir(lower, &stack[numlower],
1034 &ufs->lower_namelen, &sb->s_stack_depth,
1035 &remote);
1036 if (err)
1037 goto out_put_lowerpath;
1039 lower = strchr(lower, '\0') + 1;
1042 err = -EINVAL;
1043 sb->s_stack_depth++;
1044 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1045 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1046 goto out_put_lowerpath;
1049 if (ufs->config.upperdir) {
1050 ufs->upper_mnt = clone_private_mount(&upperpath);
1051 err = PTR_ERR(ufs->upper_mnt);
1052 if (IS_ERR(ufs->upper_mnt)) {
1053 pr_err("overlayfs: failed to clone upperpath\n");
1054 goto out_put_lowerpath;
1057 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1058 err = PTR_ERR(ufs->workdir);
1059 if (IS_ERR(ufs->workdir)) {
1060 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1061 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1062 sb->s_flags |= MS_RDONLY;
1063 ufs->workdir = NULL;
1067 * Upper should support d_type, else whiteouts are visible.
1068 * Given workdir and upper are on same fs, we can do
1069 * iterate_dir() on workdir.
1071 err = ovl_check_d_type_supported(&workpath);
1072 if (err < 0)
1073 goto out_put_workdir;
1075 if (!err) {
1076 pr_err("overlayfs: upper fs needs to support d_type.\n");
1077 err = -EINVAL;
1078 goto out_put_workdir;
1082 err = -ENOMEM;
1083 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1084 if (ufs->lower_mnt == NULL)
1085 goto out_put_workdir;
1086 for (i = 0; i < numlower; i++) {
1087 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1089 err = PTR_ERR(mnt);
1090 if (IS_ERR(mnt)) {
1091 pr_err("overlayfs: failed to clone lowerpath\n");
1092 goto out_put_lower_mnt;
1095 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1096 * will fail instead of modifying lower fs.
1098 mnt->mnt_flags |= MNT_READONLY;
1100 ufs->lower_mnt[ufs->numlower] = mnt;
1101 ufs->numlower++;
1104 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1105 if (!ufs->upper_mnt)
1106 sb->s_flags |= MS_RDONLY;
1108 if (remote)
1109 sb->s_d_op = &ovl_reval_dentry_operations;
1110 else
1111 sb->s_d_op = &ovl_dentry_operations;
1113 err = -ENOMEM;
1114 oe = ovl_alloc_entry(numlower);
1115 if (!oe)
1116 goto out_put_lower_mnt;
1118 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
1119 if (!root_dentry)
1120 goto out_free_oe;
1122 mntput(upperpath.mnt);
1123 for (i = 0; i < numlower; i++)
1124 mntput(stack[i].mnt);
1125 path_put(&workpath);
1126 kfree(lowertmp);
1128 oe->__upperdentry = upperpath.dentry;
1129 for (i = 0; i < numlower; i++) {
1130 oe->lowerstack[i].dentry = stack[i].dentry;
1131 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1133 kfree(stack);
1135 root_dentry->d_fsdata = oe;
1137 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1138 root_dentry->d_inode);
1140 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1141 sb->s_op = &ovl_super_operations;
1142 sb->s_root = root_dentry;
1143 sb->s_fs_info = ufs;
1145 return 0;
1147 out_free_oe:
1148 kfree(oe);
1149 out_put_lower_mnt:
1150 for (i = 0; i < ufs->numlower; i++)
1151 mntput(ufs->lower_mnt[i]);
1152 kfree(ufs->lower_mnt);
1153 out_put_workdir:
1154 dput(ufs->workdir);
1155 mntput(ufs->upper_mnt);
1156 out_put_lowerpath:
1157 for (i = 0; i < numlower; i++)
1158 path_put(&stack[i]);
1159 kfree(stack);
1160 out_free_lowertmp:
1161 kfree(lowertmp);
1162 out_put_workpath:
1163 path_put(&workpath);
1164 out_put_upperpath:
1165 path_put(&upperpath);
1166 out_free_config:
1167 kfree(ufs->config.lowerdir);
1168 kfree(ufs->config.upperdir);
1169 kfree(ufs->config.workdir);
1170 kfree(ufs);
1171 out:
1172 return err;
1175 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1176 const char *dev_name, void *raw_data)
1178 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1181 static struct file_system_type ovl_fs_type = {
1182 .owner = THIS_MODULE,
1183 .name = "overlay",
1184 .mount = ovl_mount,
1185 .kill_sb = kill_anon_super,
1187 MODULE_ALIAS_FS("overlay");
1189 static int __init ovl_init(void)
1191 return register_filesystem(&ovl_fs_type);
1194 static void __exit ovl_exit(void)
1196 unregister_filesystem(&ovl_fs_type);
1199 module_init(ovl_init);
1200 module_exit(ovl_exit);