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"
23 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24 MODULE_DESCRIPTION("Overlay filesystem");
25 MODULE_LICENSE("GPL");
27 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
35 /* private information held for overlayfs's superblock */
37 struct vfsmount *upper_mnt;
38 struct vfsmount *lower_mnt;
39 struct dentry *workdir;
41 /* pathnames of lower and upper dirs, for show_options */
42 struct ovl_config config;
47 /* private information held for every overlayfs dentry */
49 struct dentry *__upperdentry;
50 struct dentry *lowerdentry;
51 struct ovl_dir_cache *cache;
61 const char *ovl_opaque_xattr = "trusted.overlay.opaque";
64 enum ovl_path_type ovl_path_type(struct dentry *dentry)
66 struct ovl_entry *oe = dentry->d_fsdata;
68 if (oe->__upperdentry) {
69 if (oe->lowerdentry) {
70 if (S_ISDIR(dentry->d_inode->i_mode))
71 return OVL_PATH_MERGE;
73 return OVL_PATH_UPPER;
76 return OVL_PATH_UPPER;
78 return OVL_PATH_PURE_UPPER;
81 return OVL_PATH_LOWER;
85 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
87 struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
89 * Make sure to order reads to upperdentry wrt ovl_dentry_update()
91 smp_read_barrier_depends();
95 void ovl_path_upper(struct dentry *dentry, struct path *path)
97 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
98 struct ovl_entry *oe = dentry->d_fsdata;
100 path->mnt = ofs->upper_mnt;
101 path->dentry = ovl_upperdentry_dereference(oe);
104 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 (type == OVL_PATH_LOWER)
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 oe->lowerdentry;
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 = oe->lowerdentry;
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 = oe->lowerdentry;
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_fs *ofs = dentry->d_sb->s_fs_info;
174 struct ovl_entry *oe = dentry->d_fsdata;
176 path->mnt = ofs->lower_mnt;
177 path->dentry = oe->lowerdentry;
180 int ovl_want_write(struct dentry *dentry)
182 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
183 return mnt_want_write(ofs->upper_mnt);
186 void ovl_drop_write(struct dentry *dentry)
188 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
189 mnt_drop_write(ofs->upper_mnt);
192 struct dentry *ovl_workdir(struct dentry *dentry)
194 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
198 bool ovl_dentry_is_opaque(struct dentry *dentry)
200 struct ovl_entry *oe = dentry->d_fsdata;
204 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
206 struct ovl_entry *oe = dentry->d_fsdata;
210 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
212 struct ovl_entry *oe = dentry->d_fsdata;
214 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
215 WARN_ON(oe->__upperdentry);
216 BUG_ON(!upperdentry->d_inode);
218 * Make sure upperdentry is consistent before making it visible to
219 * ovl_upperdentry_dereference().
222 oe->__upperdentry = upperdentry;
225 void ovl_dentry_version_inc(struct dentry *dentry)
227 struct ovl_entry *oe = dentry->d_fsdata;
229 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
233 u64 ovl_dentry_version_get(struct dentry *dentry)
235 struct ovl_entry *oe = dentry->d_fsdata;
237 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
241 bool ovl_is_whiteout(struct dentry *dentry)
243 struct inode *inode = dentry->d_inode;
245 return inode && IS_WHITEOUT(inode);
248 static bool ovl_is_opaquedir(struct dentry *dentry)
252 struct inode *inode = dentry->d_inode;
254 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
257 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
258 if (res == 1 && val == 'y')
264 static void ovl_dentry_release(struct dentry *dentry)
266 struct ovl_entry *oe = dentry->d_fsdata;
269 dput(oe->__upperdentry);
270 dput(oe->lowerdentry);
275 static const struct dentry_operations ovl_dentry_operations = {
276 .d_release = ovl_dentry_release,
279 static struct ovl_entry *ovl_alloc_entry(void)
281 return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
284 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
287 struct dentry *dentry;
289 mutex_lock(&dir->d_inode->i_mutex);
290 dentry = lookup_one_len(name->name, dir, name->len);
291 mutex_unlock(&dir->d_inode->i_mutex);
293 if (IS_ERR(dentry)) {
294 if (PTR_ERR(dentry) == -ENOENT)
296 } else if (!dentry->d_inode) {
303 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
306 struct ovl_entry *oe;
307 struct dentry *upperdir;
308 struct dentry *lowerdir;
309 struct dentry *upperdentry = NULL;
310 struct dentry *lowerdentry = NULL;
311 struct inode *inode = NULL;
315 oe = ovl_alloc_entry();
319 upperdir = ovl_dentry_upper(dentry->d_parent);
320 lowerdir = ovl_dentry_lower(dentry->d_parent);
323 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
324 err = PTR_ERR(upperdentry);
325 if (IS_ERR(upperdentry))
328 if (lowerdir && upperdentry) {
329 if (ovl_is_whiteout(upperdentry)) {
333 } else if (ovl_is_opaquedir(upperdentry)) {
338 if (lowerdir && !oe->opaque) {
339 lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
340 err = PTR_ERR(lowerdentry);
341 if (IS_ERR(lowerdentry))
345 if (lowerdentry && upperdentry &&
346 (!S_ISDIR(upperdentry->d_inode->i_mode) ||
347 !S_ISDIR(lowerdentry->d_inode->i_mode))) {
353 if (lowerdentry || upperdentry) {
354 struct dentry *realdentry;
356 realdentry = upperdentry ? upperdentry : lowerdentry;
358 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
362 ovl_copyattr(realdentry->d_inode, inode);
365 oe->__upperdentry = upperdentry;
366 oe->lowerdentry = lowerdentry;
368 dentry->d_fsdata = oe;
369 d_add(dentry, inode);
383 struct file *ovl_path_open(struct path *path, int flags)
385 return dentry_open(path, flags, current_cred());
388 static void ovl_put_super(struct super_block *sb)
390 struct ovl_fs *ufs = sb->s_fs_info;
393 mntput(ufs->upper_mnt);
394 mntput(ufs->lower_mnt);
396 kfree(ufs->config.lowerdir);
397 kfree(ufs->config.upperdir);
398 kfree(ufs->config.workdir);
404 * @sb: The overlayfs super block
405 * @buf: The struct kstatfs to fill in with stats
407 * Get the filesystem statistics. As writes always target the upper layer
408 * filesystem pass the statfs to the same filesystem.
410 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
412 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
413 struct dentry *root_dentry = dentry->d_sb->s_root;
417 ovl_path_upper(root_dentry, &path);
419 err = vfs_statfs(&path, buf);
421 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
422 buf->f_type = OVERLAYFS_SUPER_MAGIC;
431 * Prints the mount options for a given superblock.
432 * Returns zero; does not fail.
434 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
436 struct super_block *sb = dentry->d_sb;
437 struct ovl_fs *ufs = sb->s_fs_info;
439 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
440 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
441 seq_printf(m, ",workdir=%s", ufs->config.workdir);
445 static const struct super_operations ovl_super_operations = {
446 .put_super = ovl_put_super,
447 .statfs = ovl_statfs,
448 .show_options = ovl_show_options,
458 static const match_table_t ovl_tokens = {
459 {OPT_LOWERDIR, "lowerdir=%s"},
460 {OPT_UPPERDIR, "upperdir=%s"},
461 {OPT_WORKDIR, "workdir=%s"},
465 static int ovl_parse_opt(char *opt, struct ovl_config *config)
469 while ((p = strsep(&opt, ",")) != NULL) {
471 substring_t args[MAX_OPT_ARGS];
476 token = match_token(p, ovl_tokens, args);
479 kfree(config->upperdir);
480 config->upperdir = match_strdup(&args[0]);
481 if (!config->upperdir)
486 kfree(config->lowerdir);
487 config->lowerdir = match_strdup(&args[0]);
488 if (!config->lowerdir)
493 kfree(config->workdir);
494 config->workdir = match_strdup(&args[0]);
495 if (!config->workdir)
506 #define OVL_WORKDIR_NAME "work"
508 static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
509 struct dentry *dentry)
511 struct inode *dir = dentry->d_inode;
514 bool retried = false;
516 err = mnt_want_write(mnt);
520 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
522 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
523 strlen(OVL_WORKDIR_NAME));
526 struct kstat stat = {
536 ovl_cleanup(dir, work);
541 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
546 mutex_unlock(&dir->i_mutex);
557 static int ovl_mount_dir(const char *name, struct path *path)
561 err = kern_path(name, LOOKUP_FOLLOW, path);
563 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
569 static bool ovl_is_allowed_fs_type(struct dentry *root)
571 const struct dentry_operations *dop = root->d_op;
575 * - automount filesystems
576 * - filesystems with revalidate (FIXME for lower layer)
577 * - filesystems with case insensitive names
580 (dop->d_manage || dop->d_automount ||
581 dop->d_revalidate || dop->d_weak_revalidate ||
582 dop->d_compare || dop->d_hash)) {
588 /* Workdir should not be subdir of upperdir and vice versa */
589 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
593 if (workdir != upperdir) {
594 ok = (lock_rename(workdir, upperdir) == NULL);
595 unlock_rename(workdir, upperdir);
600 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
602 struct path lowerpath;
603 struct path upperpath;
604 struct path workpath;
605 struct inode *root_inode;
606 struct dentry *root_dentry;
607 struct ovl_entry *oe;
609 struct kstatfs statfs;
613 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
617 err = ovl_parse_opt((char *) data, &ufs->config);
619 goto out_free_config;
621 /* FIXME: workdir is not needed for a R/O mount */
623 if (!ufs->config.upperdir || !ufs->config.lowerdir ||
624 !ufs->config.workdir) {
625 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
626 goto out_free_config;
630 oe = ovl_alloc_entry();
632 goto out_free_config;
634 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
638 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
640 goto out_put_upperpath;
642 err = ovl_mount_dir(ufs->config.workdir, &workpath);
644 goto out_put_lowerpath;
647 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
648 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
649 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
650 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
651 goto out_put_workpath;
654 if (upperpath.mnt != workpath.mnt) {
655 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
656 goto out_put_workpath;
658 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
659 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
660 goto out_put_workpath;
663 if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
664 pr_err("overlayfs: filesystem of upperdir is not supported\n");
665 goto out_put_workpath;
668 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
669 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
670 goto out_put_workpath;
673 err = vfs_statfs(&lowerpath, &statfs);
675 pr_err("overlayfs: statfs failed on lowerpath\n");
676 goto out_put_workpath;
678 ufs->lower_namelen = statfs.f_namelen;
680 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
681 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
684 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
685 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
686 goto out_put_workpath;
689 ufs->upper_mnt = clone_private_mount(&upperpath);
690 err = PTR_ERR(ufs->upper_mnt);
691 if (IS_ERR(ufs->upper_mnt)) {
692 pr_err("overlayfs: failed to clone upperpath\n");
693 goto out_put_workpath;
696 ufs->lower_mnt = clone_private_mount(&lowerpath);
697 err = PTR_ERR(ufs->lower_mnt);
698 if (IS_ERR(ufs->lower_mnt)) {
699 pr_err("overlayfs: failed to clone lowerpath\n");
700 goto out_put_upper_mnt;
703 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
704 err = PTR_ERR(ufs->workdir);
705 if (IS_ERR(ufs->workdir)) {
706 pr_err("overlayfs: failed to create directory %s/%s\n",
707 ufs->config.workdir, OVL_WORKDIR_NAME);
708 goto out_put_lower_mnt;
712 * Make lower_mnt R/O. That way fchmod/fchown on lower file
713 * will fail instead of modifying lower fs.
715 ufs->lower_mnt->mnt_flags |= MNT_READONLY;
717 /* If the upper fs is r/o, we mark overlayfs r/o too */
718 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
719 sb->s_flags |= MS_RDONLY;
721 sb->s_d_op = &ovl_dentry_operations;
724 root_inode = ovl_new_inode(sb, S_IFDIR, oe);
726 goto out_put_workdir;
728 root_dentry = d_make_root(root_inode);
730 goto out_put_workdir;
732 mntput(upperpath.mnt);
733 mntput(lowerpath.mnt);
736 oe->__upperdentry = upperpath.dentry;
737 oe->lowerdentry = lowerpath.dentry;
739 root_dentry->d_fsdata = oe;
741 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
742 sb->s_op = &ovl_super_operations;
743 sb->s_root = root_dentry;
751 mntput(ufs->lower_mnt);
753 mntput(ufs->upper_mnt);
757 path_put(&lowerpath);
759 path_put(&upperpath);
763 kfree(ufs->config.lowerdir);
764 kfree(ufs->config.upperdir);
765 kfree(ufs->config.workdir);
771 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
772 const char *dev_name, void *raw_data)
774 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
777 static struct file_system_type ovl_fs_type = {
778 .owner = THIS_MODULE,
781 .kill_sb = kill_anon_super,
783 MODULE_ALIAS_FS("overlayfs");
785 static int __init ovl_init(void)
787 return register_filesystem(&ovl_fs_type);
790 static void __exit ovl_exit(void)
792 unregister_filesystem(&ovl_fs_type);
795 module_init(ovl_init);
796 module_exit(ovl_exit);