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.
11 #include <linux/namei.h>
12 #include <linux/xattr.h>
13 #include <linux/security.h>
14 #include <linux/mount.h>
15 #include <linux/slab.h>
16 #include <linux/parser.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/statfs.h>
20 #include <linux/seq_file.h>
21 #include "overlayfs.h"
24 MODULE_DESCRIPTION("Overlay filesystem");
25 MODULE_LICENSE("GPL");
27 #define OVERLAYFS_SUPER_MAGIC 0x794c7630
35 /* private information held for overlayfs's superblock */
37 struct vfsmount *upper_mnt;
39 struct vfsmount **lower_mnt;
40 struct dentry *workdir;
42 /* pathnames of lower and upper dirs, for show_options */
43 struct ovl_config config;
48 /* private information held for every overlayfs dentry */
50 struct dentry *__upperdentry;
51 struct ovl_dir_cache *cache;
60 struct path lowerstack[];
63 #define OVL_MAX_STACK 500
65 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
67 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
70 enum ovl_path_type ovl_path_type(struct dentry *dentry)
72 struct ovl_entry *oe = dentry->d_fsdata;
73 enum ovl_path_type type = 0;
75 if (oe->__upperdentry) {
76 type = __OVL_PATH_UPPER;
79 if (S_ISDIR(dentry->d_inode->i_mode))
80 type |= __OVL_PATH_MERGE;
81 } else if (!oe->opaque) {
82 type |= __OVL_PATH_PURE;
86 type |= __OVL_PATH_MERGE;
91 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
93 return lockless_dereference(oe->__upperdentry);
96 void ovl_path_upper(struct dentry *dentry, struct path *path)
98 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
99 struct ovl_entry *oe = dentry->d_fsdata;
101 path->mnt = ofs->upper_mnt;
102 path->dentry = ovl_upperdentry_dereference(oe);
105 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
107 enum ovl_path_type type = ovl_path_type(dentry);
109 if (!OVL_TYPE_UPPER(type))
110 ovl_path_lower(dentry, path);
112 ovl_path_upper(dentry, path);
117 struct dentry *ovl_dentry_upper(struct dentry *dentry)
119 struct ovl_entry *oe = dentry->d_fsdata;
121 return ovl_upperdentry_dereference(oe);
124 struct dentry *ovl_dentry_lower(struct dentry *dentry)
126 struct ovl_entry *oe = dentry->d_fsdata;
128 return __ovl_dentry_lower(oe);
131 struct dentry *ovl_dentry_real(struct dentry *dentry)
133 struct ovl_entry *oe = dentry->d_fsdata;
134 struct dentry *realdentry;
136 realdentry = ovl_upperdentry_dereference(oe);
138 realdentry = __ovl_dentry_lower(oe);
143 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
145 struct dentry *realdentry;
147 realdentry = ovl_upperdentry_dereference(oe);
151 realdentry = __ovl_dentry_lower(oe);
157 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
159 struct ovl_entry *oe = dentry->d_fsdata;
164 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
166 struct ovl_entry *oe = dentry->d_fsdata;
171 void ovl_path_lower(struct dentry *dentry, struct path *path)
173 struct ovl_entry *oe = dentry->d_fsdata;
175 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
178 int ovl_want_write(struct dentry *dentry)
180 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
181 return mnt_want_write(ofs->upper_mnt);
184 void ovl_drop_write(struct dentry *dentry)
186 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
187 mnt_drop_write(ofs->upper_mnt);
190 struct dentry *ovl_workdir(struct dentry *dentry)
192 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
196 bool ovl_dentry_is_opaque(struct dentry *dentry)
198 struct ovl_entry *oe = dentry->d_fsdata;
202 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
204 struct ovl_entry *oe = dentry->d_fsdata;
208 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
210 struct ovl_entry *oe = dentry->d_fsdata;
212 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
213 WARN_ON(oe->__upperdentry);
214 BUG_ON(!upperdentry->d_inode);
216 * Make sure upperdentry is consistent before making it visible to
217 * ovl_upperdentry_dereference().
220 oe->__upperdentry = upperdentry;
223 void ovl_dentry_version_inc(struct dentry *dentry)
225 struct ovl_entry *oe = dentry->d_fsdata;
227 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
231 u64 ovl_dentry_version_get(struct dentry *dentry)
233 struct ovl_entry *oe = dentry->d_fsdata;
235 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
239 bool ovl_is_whiteout(struct dentry *dentry)
241 struct inode *inode = dentry->d_inode;
243 return inode && IS_WHITEOUT(inode);
246 static bool ovl_is_opaquedir(struct dentry *dentry)
250 struct inode *inode = dentry->d_inode;
252 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
255 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
256 if (res == 1 && val == 'y')
262 static void ovl_dentry_release(struct dentry *dentry)
264 struct ovl_entry *oe = dentry->d_fsdata;
269 dput(oe->__upperdentry);
270 for (i = 0; i < oe->numlower; i++)
271 dput(oe->lowerstack[i].dentry);
276 static const struct dentry_operations ovl_dentry_operations = {
277 .d_release = ovl_dentry_release,
280 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
282 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
283 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
286 oe->numlower = numlower;
291 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
294 struct dentry *dentry;
296 mutex_lock(&dir->d_inode->i_mutex);
297 dentry = lookup_one_len(name->name, dir, name->len);
298 mutex_unlock(&dir->d_inode->i_mutex);
300 if (IS_ERR(dentry)) {
301 if (PTR_ERR(dentry) == -ENOENT)
303 } else if (!dentry->d_inode) {
311 * Returns next layer in stack starting from top.
312 * Returns -1 if this is the last layer.
314 int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
316 struct ovl_entry *oe = dentry->d_fsdata;
320 ovl_path_upper(dentry, path);
322 return oe->numlower ? 1 : -1;
325 BUG_ON(idx > oe->numlower);
326 *path = oe->lowerstack[idx - 1];
328 return (idx < oe->numlower) ? idx + 1 : -1;
331 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
334 struct ovl_entry *oe;
335 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
336 struct path *stack = NULL;
337 struct dentry *upperdir, *upperdentry = NULL;
338 unsigned int ctr = 0;
339 struct inode *inode = NULL;
340 bool upperopaque = false;
341 struct dentry *this, *prev = NULL;
345 upperdir = ovl_upperdentry_dereference(poe);
347 this = ovl_lookup_real(upperdir, &dentry->d_name);
353 if (ovl_is_whiteout(this)) {
357 } else if (poe->numlower && ovl_is_opaquedir(this)) {
361 upperdentry = prev = this;
364 if (!upperopaque && poe->numlower) {
366 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
371 for (i = 0; !upperopaque && i < poe->numlower; i++) {
373 struct path lowerpath = poe->lowerstack[i];
375 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
379 * If it's positive, then treat ENAMETOOLONG as ENOENT.
381 if (err == -ENAMETOOLONG && (upperdentry || ctr))
387 if (ovl_is_whiteout(this)) {
392 * Only makes sense to check opaque dir if this is not the
395 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
398 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
399 !S_ISDIR(this->d_inode->i_mode))) {
401 * FIXME: check for upper-opaqueness maybe better done
404 if (prev == upperdentry)
410 * If this is a non-directory then stop here.
412 if (!S_ISDIR(this->d_inode->i_mode))
415 stack[ctr].dentry = this;
416 stack[ctr].mnt = lowerpath.mnt;
423 oe = ovl_alloc_entry(ctr);
428 if (upperdentry || ctr) {
429 struct dentry *realdentry;
431 realdentry = upperdentry ? upperdentry : stack[0].dentry;
434 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
438 ovl_copyattr(realdentry->d_inode, inode);
441 oe->opaque = upperopaque;
442 oe->__upperdentry = upperdentry;
443 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
445 dentry->d_fsdata = oe;
446 d_add(dentry, inode);
453 for (i = 0; i < ctr; i++)
454 dput(stack[i].dentry);
462 struct file *ovl_path_open(struct path *path, int flags)
464 return dentry_open(path, flags, current_cred());
467 static void ovl_put_super(struct super_block *sb)
469 struct ovl_fs *ufs = sb->s_fs_info;
473 mntput(ufs->upper_mnt);
474 for (i = 0; i < ufs->numlower; i++)
475 mntput(ufs->lower_mnt[i]);
477 kfree(ufs->config.lowerdir);
478 kfree(ufs->config.upperdir);
479 kfree(ufs->config.workdir);
485 * @sb: The overlayfs super block
486 * @buf: The struct kstatfs to fill in with stats
488 * Get the filesystem statistics. As writes always target the upper layer
489 * filesystem pass the statfs to the upper filesystem (if it exists)
491 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
493 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
494 struct dentry *root_dentry = dentry->d_sb->s_root;
498 ovl_path_real(root_dentry, &path);
500 err = vfs_statfs(&path, buf);
502 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
503 buf->f_type = OVERLAYFS_SUPER_MAGIC;
512 * Prints the mount options for a given superblock.
513 * Returns zero; does not fail.
515 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
517 struct super_block *sb = dentry->d_sb;
518 struct ovl_fs *ufs = sb->s_fs_info;
520 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
521 if (ufs->config.upperdir) {
522 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
523 seq_printf(m, ",workdir=%s", ufs->config.workdir);
528 static int ovl_remount(struct super_block *sb, int *flags, char *data)
530 struct ovl_fs *ufs = sb->s_fs_info;
532 if (!(*flags & MS_RDONLY) &&
533 (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)))
539 static const struct super_operations ovl_super_operations = {
540 .put_super = ovl_put_super,
541 .statfs = ovl_statfs,
542 .show_options = ovl_show_options,
543 .remount_fs = ovl_remount,
553 static const match_table_t ovl_tokens = {
554 {OPT_LOWERDIR, "lowerdir=%s"},
555 {OPT_UPPERDIR, "upperdir=%s"},
556 {OPT_WORKDIR, "workdir=%s"},
560 static char *ovl_next_opt(char **s)
568 for (p = sbegin; *p; p++) {
573 } else if (*p == ',') {
583 static int ovl_parse_opt(char *opt, struct ovl_config *config)
587 while ((p = ovl_next_opt(&opt)) != NULL) {
589 substring_t args[MAX_OPT_ARGS];
594 token = match_token(p, ovl_tokens, args);
597 kfree(config->upperdir);
598 config->upperdir = match_strdup(&args[0]);
599 if (!config->upperdir)
604 kfree(config->lowerdir);
605 config->lowerdir = match_strdup(&args[0]);
606 if (!config->lowerdir)
611 kfree(config->workdir);
612 config->workdir = match_strdup(&args[0]);
613 if (!config->workdir)
624 #define OVL_WORKDIR_NAME "work"
626 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
627 struct dentry *dentry)
629 struct inode *dir = dentry->d_inode;
632 bool retried = false;
634 err = mnt_want_write(mnt);
638 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
640 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
641 strlen(OVL_WORKDIR_NAME));
644 struct kstat stat = {
654 ovl_cleanup(dir, work);
659 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
664 mutex_unlock(&dir->i_mutex);
675 static void ovl_unescape(char *s)
688 static bool ovl_is_allowed_fs_type(struct dentry *root)
690 const struct dentry_operations *dop = root->d_op;
694 * - automount filesystems
695 * - filesystems with revalidate (FIXME for lower layer)
696 * - filesystems with case insensitive names
699 (dop->d_manage || dop->d_automount ||
700 dop->d_revalidate || dop->d_weak_revalidate ||
701 dop->d_compare || dop->d_hash)) {
707 static int ovl_mount_dir_noesc(const char *name, struct path *path)
712 pr_err("overlayfs: empty lowerdir\n");
715 err = kern_path(name, LOOKUP_FOLLOW, path);
717 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
721 if (!ovl_is_allowed_fs_type(path->dentry)) {
722 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
725 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
726 pr_err("overlayfs: '%s' not a directory\n", name);
737 static int ovl_mount_dir(const char *name, struct path *path)
740 char *tmp = kstrdup(name, GFP_KERNEL);
744 err = ovl_mount_dir_noesc(tmp, path);
750 static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
754 struct kstatfs statfs;
756 err = ovl_mount_dir_noesc(name, path);
760 err = vfs_statfs(path, &statfs);
762 pr_err("overlayfs: statfs failed on '%s'\n", name);
765 *namelen = max(*namelen, statfs.f_namelen);
766 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
776 /* Workdir should not be subdir of upperdir and vice versa */
777 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
781 if (workdir != upperdir) {
782 ok = (lock_rename(workdir, upperdir) == NULL);
783 unlock_rename(workdir, upperdir);
788 static unsigned int ovl_split_lowerdirs(char *str)
790 unsigned int ctr = 1;
793 for (s = d = str;; s++, d++) {
796 } else if (*s == ':') {
808 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
810 struct path upperpath = { NULL, NULL };
811 struct path workpath = { NULL, NULL };
812 struct dentry *root_dentry;
813 struct ovl_entry *oe;
815 struct path *stack = NULL;
818 unsigned int numlower;
819 unsigned int stacklen = 0;
824 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
828 err = ovl_parse_opt((char *) data, &ufs->config);
830 goto out_free_config;
833 if (!ufs->config.lowerdir) {
834 pr_err("overlayfs: missing 'lowerdir'\n");
835 goto out_free_config;
838 sb->s_stack_depth = 0;
839 if (ufs->config.upperdir) {
840 /* FIXME: workdir is not needed for a R/O mount */
841 if (!ufs->config.workdir) {
842 pr_err("overlayfs: missing 'workdir'\n");
843 goto out_free_config;
846 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
848 goto out_free_config;
850 err = ovl_mount_dir(ufs->config.workdir, &workpath);
852 goto out_put_upperpath;
855 if (upperpath.mnt != workpath.mnt) {
856 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
857 goto out_put_workpath;
859 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
860 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
861 goto out_put_workpath;
863 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
866 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
868 goto out_put_workpath;
871 stacklen = ovl_split_lowerdirs(lowertmp);
872 if (stacklen > OVL_MAX_STACK)
873 goto out_free_lowertmp;
875 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
877 goto out_free_lowertmp;
880 for (numlower = 0; numlower < stacklen; numlower++) {
881 err = ovl_lower_dir(lower, &stack[numlower],
882 &ufs->lower_namelen, &sb->s_stack_depth);
884 goto out_put_lowerpath;
886 lower = strchr(lower, '\0') + 1;
891 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
892 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
893 goto out_put_lowerpath;
896 if (ufs->config.upperdir) {
897 ufs->upper_mnt = clone_private_mount(&upperpath);
898 err = PTR_ERR(ufs->upper_mnt);
899 if (IS_ERR(ufs->upper_mnt)) {
900 pr_err("overlayfs: failed to clone upperpath\n");
901 goto out_put_lowerpath;
904 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
905 err = PTR_ERR(ufs->workdir);
906 if (IS_ERR(ufs->workdir)) {
907 pr_err("overlayfs: failed to create directory %s/%s\n",
908 ufs->config.workdir, OVL_WORKDIR_NAME);
909 goto out_put_upper_mnt;
914 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
915 if (ufs->lower_mnt == NULL)
916 goto out_put_workdir;
917 for (i = 0; i < numlower; i++) {
918 struct vfsmount *mnt = clone_private_mount(&stack[i]);
922 pr_err("overlayfs: failed to clone lowerpath\n");
923 goto out_put_lower_mnt;
926 * Make lower_mnt R/O. That way fchmod/fchown on lower file
927 * will fail instead of modifying lower fs.
929 mnt->mnt_flags |= MNT_READONLY;
931 ufs->lower_mnt[ufs->numlower] = mnt;
935 /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
936 if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
937 sb->s_flags |= MS_RDONLY;
939 sb->s_d_op = &ovl_dentry_operations;
942 oe = ovl_alloc_entry(numlower);
944 goto out_put_lower_mnt;
946 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
950 mntput(upperpath.mnt);
951 for (i = 0; i < numlower; i++)
952 mntput(stack[i].mnt);
956 oe->__upperdentry = upperpath.dentry;
957 for (i = 0; i < numlower; i++) {
958 oe->lowerstack[i].dentry = stack[i].dentry;
959 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
962 root_dentry->d_fsdata = oe;
964 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
965 sb->s_op = &ovl_super_operations;
966 sb->s_root = root_dentry;
974 for (i = 0; i < ufs->numlower; i++)
975 mntput(ufs->lower_mnt[i]);
976 kfree(ufs->lower_mnt);
980 mntput(ufs->upper_mnt);
982 for (i = 0; i < numlower; i++)
990 path_put(&upperpath);
992 kfree(ufs->config.lowerdir);
993 kfree(ufs->config.upperdir);
994 kfree(ufs->config.workdir);
1000 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1001 const char *dev_name, void *raw_data)
1003 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1006 static struct file_system_type ovl_fs_type = {
1007 .owner = THIS_MODULE,
1010 .kill_sb = kill_anon_super,
1012 MODULE_ALIAS_FS("overlay");
1014 static int __init ovl_init(void)
1016 return register_filesystem(&ovl_fs_type);
1019 static void __exit ovl_exit(void)
1021 unregister_filesystem(&ovl_fs_type);
1024 module_init(ovl_init);
1025 module_exit(ovl_exit);