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 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 const char *ovl_opaque_xattr
= "trusted.overlay.opaque";
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
)
108 enum ovl_path_type type
= ovl_path_type(dentry
);
110 if (!OVL_TYPE_UPPER(type
))
111 ovl_path_lower(dentry
, path
);
113 ovl_path_upper(dentry
, path
);
118 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
120 struct ovl_entry
*oe
= dentry
->d_fsdata
;
122 return ovl_upperdentry_dereference(oe
);
125 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
127 struct ovl_entry
*oe
= dentry
->d_fsdata
;
129 return __ovl_dentry_lower(oe
);
132 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
134 struct ovl_entry
*oe
= dentry
->d_fsdata
;
135 struct dentry
*realdentry
;
137 realdentry
= ovl_upperdentry_dereference(oe
);
139 realdentry
= __ovl_dentry_lower(oe
);
144 struct dentry
*ovl_entry_real(struct ovl_entry
*oe
, bool *is_upper
)
146 struct dentry
*realdentry
;
148 realdentry
= ovl_upperdentry_dereference(oe
);
152 realdentry
= __ovl_dentry_lower(oe
);
158 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
160 struct ovl_entry
*oe
= dentry
->d_fsdata
;
165 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
167 struct ovl_entry
*oe
= dentry
->d_fsdata
;
172 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
174 struct ovl_entry
*oe
= dentry
->d_fsdata
;
176 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { NULL
, NULL
};
179 int ovl_want_write(struct dentry
*dentry
)
181 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
182 return mnt_want_write(ofs
->upper_mnt
);
185 void ovl_drop_write(struct dentry
*dentry
)
187 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
188 mnt_drop_write(ofs
->upper_mnt
);
191 struct dentry
*ovl_workdir(struct dentry
*dentry
)
193 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
197 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
199 struct ovl_entry
*oe
= dentry
->d_fsdata
;
203 void ovl_dentry_set_opaque(struct dentry
*dentry
, bool opaque
)
205 struct ovl_entry
*oe
= dentry
->d_fsdata
;
209 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
211 struct ovl_entry
*oe
= dentry
->d_fsdata
;
213 WARN_ON(!mutex_is_locked(&upperdentry
->d_parent
->d_inode
->i_mutex
));
214 WARN_ON(oe
->__upperdentry
);
215 BUG_ON(!upperdentry
->d_inode
);
217 * Make sure upperdentry is consistent before making it visible to
218 * ovl_upperdentry_dereference().
221 oe
->__upperdentry
= upperdentry
;
224 void ovl_dentry_version_inc(struct dentry
*dentry
)
226 struct ovl_entry
*oe
= dentry
->d_fsdata
;
228 WARN_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
232 u64
ovl_dentry_version_get(struct dentry
*dentry
)
234 struct ovl_entry
*oe
= dentry
->d_fsdata
;
236 WARN_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
240 bool ovl_is_whiteout(struct dentry
*dentry
)
242 struct inode
*inode
= dentry
->d_inode
;
244 return inode
&& IS_WHITEOUT(inode
);
247 static bool ovl_is_opaquedir(struct dentry
*dentry
)
251 struct inode
*inode
= dentry
->d_inode
;
253 if (!S_ISDIR(inode
->i_mode
) || !inode
->i_op
->getxattr
)
256 res
= inode
->i_op
->getxattr(dentry
, ovl_opaque_xattr
, &val
, 1);
257 if (res
== 1 && val
== 'y')
263 static void ovl_dentry_release(struct dentry
*dentry
)
265 struct ovl_entry
*oe
= dentry
->d_fsdata
;
270 dput(oe
->__upperdentry
);
271 for (i
= 0; i
< oe
->numlower
; i
++)
272 dput(oe
->lowerstack
[i
].dentry
);
277 static const struct dentry_operations ovl_dentry_operations
= {
278 .d_release
= ovl_dentry_release
,
281 static struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
283 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
284 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
287 oe
->numlower
= numlower
;
292 static inline struct dentry
*ovl_lookup_real(struct dentry
*dir
,
295 struct dentry
*dentry
;
297 mutex_lock(&dir
->d_inode
->i_mutex
);
298 dentry
= lookup_one_len(name
->name
, dir
, name
->len
);
299 mutex_unlock(&dir
->d_inode
->i_mutex
);
301 if (IS_ERR(dentry
)) {
302 if (PTR_ERR(dentry
) == -ENOENT
)
304 } else if (!dentry
->d_inode
) {
312 * Returns next layer in stack starting from top.
313 * Returns -1 if this is the last layer.
315 int ovl_path_next(int idx
, struct dentry
*dentry
, struct path
*path
)
317 struct ovl_entry
*oe
= dentry
->d_fsdata
;
321 ovl_path_upper(dentry
, path
);
323 return oe
->numlower
? 1 : -1;
326 BUG_ON(idx
> oe
->numlower
);
327 *path
= oe
->lowerstack
[idx
- 1];
329 return (idx
< oe
->numlower
) ? idx
+ 1 : -1;
332 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
335 struct ovl_entry
*oe
;
336 struct ovl_entry
*poe
= dentry
->d_parent
->d_fsdata
;
337 struct path
*stack
= NULL
;
338 struct dentry
*upperdir
, *upperdentry
= NULL
;
339 unsigned int ctr
= 0;
340 struct inode
*inode
= NULL
;
341 bool upperopaque
= false;
342 struct dentry
*this, *prev
= NULL
;
346 upperdir
= ovl_upperdentry_dereference(poe
);
348 this = ovl_lookup_real(upperdir
, &dentry
->d_name
);
354 if (ovl_is_whiteout(this)) {
358 } else if (poe
->numlower
&& ovl_is_opaquedir(this)) {
362 upperdentry
= prev
= this;
365 if (!upperopaque
&& poe
->numlower
) {
367 stack
= kcalloc(poe
->numlower
, sizeof(struct path
), GFP_KERNEL
);
372 for (i
= 0; !upperopaque
&& i
< poe
->numlower
; i
++) {
374 struct path lowerpath
= poe
->lowerstack
[i
];
377 this = ovl_lookup_real(lowerpath
.dentry
, &dentry
->d_name
);
381 * If it's positive, then treat ENAMETOOLONG as ENOENT.
383 if (err
== -ENAMETOOLONG
&& (upperdentry
|| ctr
))
389 if (ovl_is_whiteout(this)) {
394 * Only makes sense to check opaque dir if this is not the
397 if (i
< poe
->numlower
- 1 && ovl_is_opaquedir(this))
400 * If this is a non-directory then stop here.
402 * FIXME: check for opaqueness maybe better done in remove code.
404 if (!S_ISDIR(this->d_inode
->i_mode
)) {
406 } else if (prev
&& (!S_ISDIR(prev
->d_inode
->i_mode
) ||
407 !S_ISDIR(this->d_inode
->i_mode
))) {
408 if (prev
== upperdentry
)
413 stack
[ctr
].dentry
= this;
414 stack
[ctr
].mnt
= lowerpath
.mnt
;
421 oe
= ovl_alloc_entry(ctr
);
426 if (upperdentry
|| ctr
) {
427 struct dentry
*realdentry
;
429 realdentry
= upperdentry
? upperdentry
: stack
[0].dentry
;
432 inode
= ovl_new_inode(dentry
->d_sb
, realdentry
->d_inode
->i_mode
,
436 ovl_copyattr(realdentry
->d_inode
, inode
);
439 oe
->opaque
= upperopaque
;
440 oe
->__upperdentry
= upperdentry
;
441 memcpy(oe
->lowerstack
, stack
, sizeof(struct path
) * ctr
);
443 dentry
->d_fsdata
= oe
;
444 d_add(dentry
, inode
);
451 for (i
= 0; i
< ctr
; i
++)
452 dput(stack
[i
].dentry
);
460 struct file
*ovl_path_open(struct path
*path
, int flags
)
462 return dentry_open(path
, flags
, current_cred());
465 static void ovl_put_super(struct super_block
*sb
)
467 struct ovl_fs
*ufs
= sb
->s_fs_info
;
471 mntput(ufs
->upper_mnt
);
472 for (i
= 0; i
< ufs
->numlower
; i
++)
473 mntput(ufs
->lower_mnt
[i
]);
475 kfree(ufs
->config
.lowerdir
);
476 kfree(ufs
->config
.upperdir
);
477 kfree(ufs
->config
.workdir
);
483 * @sb: The overlayfs super block
484 * @buf: The struct kstatfs to fill in with stats
486 * Get the filesystem statistics. As writes always target the upper layer
487 * filesystem pass the statfs to the same filesystem.
489 static int ovl_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
491 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
492 struct dentry
*root_dentry
= dentry
->d_sb
->s_root
;
496 ovl_path_upper(root_dentry
, &path
);
498 err
= vfs_statfs(&path
, buf
);
500 buf
->f_namelen
= max(buf
->f_namelen
, ofs
->lower_namelen
);
501 buf
->f_type
= OVERLAYFS_SUPER_MAGIC
;
510 * Prints the mount options for a given superblock.
511 * Returns zero; does not fail.
513 static int ovl_show_options(struct seq_file
*m
, struct dentry
*dentry
)
515 struct super_block
*sb
= dentry
->d_sb
;
516 struct ovl_fs
*ufs
= sb
->s_fs_info
;
518 seq_printf(m
, ",lowerdir=%s", ufs
->config
.lowerdir
);
519 seq_printf(m
, ",upperdir=%s", ufs
->config
.upperdir
);
520 seq_printf(m
, ",workdir=%s", ufs
->config
.workdir
);
524 static const struct super_operations ovl_super_operations
= {
525 .put_super
= ovl_put_super
,
526 .statfs
= ovl_statfs
,
527 .show_options
= ovl_show_options
,
537 static const match_table_t ovl_tokens
= {
538 {OPT_LOWERDIR
, "lowerdir=%s"},
539 {OPT_UPPERDIR
, "upperdir=%s"},
540 {OPT_WORKDIR
, "workdir=%s"},
544 static char *ovl_next_opt(char **s
)
552 for (p
= sbegin
; *p
; p
++) {
557 } else if (*p
== ',') {
567 static int ovl_parse_opt(char *opt
, struct ovl_config
*config
)
571 while ((p
= ovl_next_opt(&opt
)) != NULL
) {
573 substring_t args
[MAX_OPT_ARGS
];
578 token
= match_token(p
, ovl_tokens
, args
);
581 kfree(config
->upperdir
);
582 config
->upperdir
= match_strdup(&args
[0]);
583 if (!config
->upperdir
)
588 kfree(config
->lowerdir
);
589 config
->lowerdir
= match_strdup(&args
[0]);
590 if (!config
->lowerdir
)
595 kfree(config
->workdir
);
596 config
->workdir
= match_strdup(&args
[0]);
597 if (!config
->workdir
)
608 #define OVL_WORKDIR_NAME "work"
610 static struct dentry
*ovl_workdir_create(struct vfsmount
*mnt
,
611 struct dentry
*dentry
)
613 struct inode
*dir
= dentry
->d_inode
;
616 bool retried
= false;
618 err
= mnt_want_write(mnt
);
622 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
624 work
= lookup_one_len(OVL_WORKDIR_NAME
, dentry
,
625 strlen(OVL_WORKDIR_NAME
));
628 struct kstat stat
= {
638 ovl_cleanup(dir
, work
);
643 err
= ovl_create_real(dir
, work
, &stat
, NULL
, NULL
, true);
648 mutex_unlock(&dir
->i_mutex
);
659 static void ovl_unescape(char *s
)
672 static int ovl_mount_dir(const char *name
, struct path
*path
)
675 char *tmp
= kstrdup(name
, GFP_KERNEL
);
681 err
= kern_path(tmp
, LOOKUP_FOLLOW
, path
);
683 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp
, err
);
690 static bool ovl_is_allowed_fs_type(struct dentry
*root
)
692 const struct dentry_operations
*dop
= root
->d_op
;
696 * - automount filesystems
697 * - filesystems with revalidate (FIXME for lower layer)
698 * - filesystems with case insensitive names
701 (dop
->d_manage
|| dop
->d_automount
||
702 dop
->d_revalidate
|| dop
->d_weak_revalidate
||
703 dop
->d_compare
|| dop
->d_hash
)) {
709 /* Workdir should not be subdir of upperdir and vice versa */
710 static bool ovl_workdir_ok(struct dentry
*workdir
, struct dentry
*upperdir
)
714 if (workdir
!= upperdir
) {
715 ok
= (lock_rename(workdir
, upperdir
) == NULL
);
716 unlock_rename(workdir
, upperdir
);
721 static int ovl_fill_super(struct super_block
*sb
, void *data
, int silent
)
723 struct path lowerpath
;
724 struct path upperpath
;
725 struct path workpath
;
726 struct inode
*root_inode
;
727 struct dentry
*root_dentry
;
728 struct ovl_entry
*oe
;
730 struct kstatfs statfs
;
731 struct vfsmount
*mnt
;
736 ufs
= kzalloc(sizeof(struct ovl_fs
), GFP_KERNEL
);
740 err
= ovl_parse_opt((char *) data
, &ufs
->config
);
742 goto out_free_config
;
744 /* FIXME: workdir is not needed for a R/O mount */
746 if (!ufs
->config
.upperdir
|| !ufs
->config
.lowerdir
||
747 !ufs
->config
.workdir
) {
748 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
749 goto out_free_config
;
753 oe
= ovl_alloc_entry(1);
755 goto out_free_config
;
757 err
= ovl_mount_dir(ufs
->config
.upperdir
, &upperpath
);
761 err
= ovl_mount_dir(ufs
->config
.lowerdir
, &lowerpath
);
763 goto out_put_upperpath
;
765 err
= ovl_mount_dir(ufs
->config
.workdir
, &workpath
);
767 goto out_put_lowerpath
;
770 if (!S_ISDIR(upperpath
.dentry
->d_inode
->i_mode
) ||
771 !S_ISDIR(lowerpath
.dentry
->d_inode
->i_mode
) ||
772 !S_ISDIR(workpath
.dentry
->d_inode
->i_mode
)) {
773 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
774 goto out_put_workpath
;
777 if (upperpath
.mnt
!= workpath
.mnt
) {
778 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
779 goto out_put_workpath
;
781 if (!ovl_workdir_ok(workpath
.dentry
, upperpath
.dentry
)) {
782 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
783 goto out_put_workpath
;
786 if (!ovl_is_allowed_fs_type(upperpath
.dentry
)) {
787 pr_err("overlayfs: filesystem of upperdir is not supported\n");
788 goto out_put_workpath
;
791 if (!ovl_is_allowed_fs_type(lowerpath
.dentry
)) {
792 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
793 goto out_put_workpath
;
796 err
= vfs_statfs(&lowerpath
, &statfs
);
798 pr_err("overlayfs: statfs failed on lowerpath\n");
799 goto out_put_workpath
;
801 ufs
->lower_namelen
= statfs
.f_namelen
;
803 sb
->s_stack_depth
= max(upperpath
.mnt
->mnt_sb
->s_stack_depth
,
804 lowerpath
.mnt
->mnt_sb
->s_stack_depth
) + 1;
807 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
808 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
809 goto out_put_workpath
;
812 ufs
->upper_mnt
= clone_private_mount(&upperpath
);
813 err
= PTR_ERR(ufs
->upper_mnt
);
814 if (IS_ERR(ufs
->upper_mnt
)) {
815 pr_err("overlayfs: failed to clone upperpath\n");
816 goto out_put_workpath
;
819 ufs
->lower_mnt
= kcalloc(1, sizeof(struct vfsmount
*), GFP_KERNEL
);
820 if (ufs
->lower_mnt
== NULL
)
821 goto out_put_upper_mnt
;
823 mnt
= clone_private_mount(&lowerpath
);
826 pr_err("overlayfs: failed to clone lowerpath\n");
827 goto out_put_lower_mnt
;
830 * Make lower_mnt R/O. That way fchmod/fchown on lower file
831 * will fail instead of modifying lower fs.
833 mnt
->mnt_flags
|= MNT_READONLY
;
835 ufs
->lower_mnt
[0] = mnt
;
838 ufs
->workdir
= ovl_workdir_create(ufs
->upper_mnt
, workpath
.dentry
);
839 err
= PTR_ERR(ufs
->workdir
);
840 if (IS_ERR(ufs
->workdir
)) {
841 pr_err("overlayfs: failed to create directory %s/%s\n",
842 ufs
->config
.workdir
, OVL_WORKDIR_NAME
);
843 goto out_put_lower_mnt
;
846 /* If the upper fs is r/o, we mark overlayfs r/o too */
847 if (ufs
->upper_mnt
->mnt_sb
->s_flags
& MS_RDONLY
)
848 sb
->s_flags
|= MS_RDONLY
;
850 sb
->s_d_op
= &ovl_dentry_operations
;
853 root_inode
= ovl_new_inode(sb
, S_IFDIR
, oe
);
855 goto out_put_workdir
;
857 root_dentry
= d_make_root(root_inode
);
859 goto out_put_workdir
;
861 mntput(upperpath
.mnt
);
862 mntput(lowerpath
.mnt
);
865 oe
->__upperdentry
= upperpath
.dentry
;
866 oe
->lowerstack
[0].dentry
= lowerpath
.dentry
;
867 oe
->lowerstack
[0].mnt
= ufs
->lower_mnt
[0];
869 root_dentry
->d_fsdata
= oe
;
871 sb
->s_magic
= OVERLAYFS_SUPER_MAGIC
;
872 sb
->s_op
= &ovl_super_operations
;
873 sb
->s_root
= root_dentry
;
881 for (i
= 0; i
< ufs
->numlower
; i
++)
882 mntput(ufs
->lower_mnt
[i
]);
883 kfree(ufs
->lower_mnt
);
885 mntput(ufs
->upper_mnt
);
889 path_put(&lowerpath
);
891 path_put(&upperpath
);
895 kfree(ufs
->config
.lowerdir
);
896 kfree(ufs
->config
.upperdir
);
897 kfree(ufs
->config
.workdir
);
903 static struct dentry
*ovl_mount(struct file_system_type
*fs_type
, int flags
,
904 const char *dev_name
, void *raw_data
)
906 return mount_nodev(fs_type
, flags
, raw_data
, ovl_fill_super
);
909 static struct file_system_type ovl_fs_type
= {
910 .owner
= THIS_MODULE
,
913 .kill_sb
= kill_anon_super
,
915 MODULE_ALIAS_FS("overlay");
917 static int __init
ovl_init(void)
919 return register_filesystem(&ovl_fs_type
);
922 static void __exit
ovl_exit(void)
924 unregister_filesystem(&ovl_fs_type
);
927 module_init(ovl_init
);
928 module_exit(ovl_exit
);