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
;
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 return lockless_dereference(oe
->__upperdentry
);
90 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
92 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
93 struct ovl_entry
*oe
= dentry
->d_fsdata
;
95 path
->mnt
= ofs
->upper_mnt
;
96 path
->dentry
= ovl_upperdentry_dereference(oe
);
99 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
102 enum ovl_path_type type
= ovl_path_type(dentry
);
104 if (type
== OVL_PATH_LOWER
)
105 ovl_path_lower(dentry
, path
);
107 ovl_path_upper(dentry
, path
);
112 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
114 struct ovl_entry
*oe
= dentry
->d_fsdata
;
116 return ovl_upperdentry_dereference(oe
);
119 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
121 struct ovl_entry
*oe
= dentry
->d_fsdata
;
123 return oe
->lowerdentry
;
126 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
128 struct ovl_entry
*oe
= dentry
->d_fsdata
;
129 struct dentry
*realdentry
;
131 realdentry
= ovl_upperdentry_dereference(oe
);
133 realdentry
= oe
->lowerdentry
;
138 struct dentry
*ovl_entry_real(struct ovl_entry
*oe
, bool *is_upper
)
140 struct dentry
*realdentry
;
142 realdentry
= ovl_upperdentry_dereference(oe
);
146 realdentry
= oe
->lowerdentry
;
152 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
154 struct ovl_entry
*oe
= dentry
->d_fsdata
;
159 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
161 struct ovl_entry
*oe
= dentry
->d_fsdata
;
166 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
168 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
169 struct ovl_entry
*oe
= dentry
->d_fsdata
;
171 path
->mnt
= ofs
->lower_mnt
;
172 path
->dentry
= oe
->lowerdentry
;
175 int ovl_want_write(struct dentry
*dentry
)
177 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
178 return mnt_want_write(ofs
->upper_mnt
);
181 void ovl_drop_write(struct dentry
*dentry
)
183 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
184 mnt_drop_write(ofs
->upper_mnt
);
187 struct dentry
*ovl_workdir(struct dentry
*dentry
)
189 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
193 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
195 struct ovl_entry
*oe
= dentry
->d_fsdata
;
199 void ovl_dentry_set_opaque(struct dentry
*dentry
, bool opaque
)
201 struct ovl_entry
*oe
= dentry
->d_fsdata
;
205 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
207 struct ovl_entry
*oe
= dentry
->d_fsdata
;
209 WARN_ON(!mutex_is_locked(&upperdentry
->d_parent
->d_inode
->i_mutex
));
210 WARN_ON(oe
->__upperdentry
);
211 BUG_ON(!upperdentry
->d_inode
);
213 * Make sure upperdentry is consistent before making it visible to
214 * ovl_upperdentry_dereference().
217 oe
->__upperdentry
= upperdentry
;
220 void ovl_dentry_version_inc(struct dentry
*dentry
)
222 struct ovl_entry
*oe
= dentry
->d_fsdata
;
224 WARN_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
228 u64
ovl_dentry_version_get(struct dentry
*dentry
)
230 struct ovl_entry
*oe
= dentry
->d_fsdata
;
232 WARN_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
236 bool ovl_is_whiteout(struct dentry
*dentry
)
238 struct inode
*inode
= dentry
->d_inode
;
240 return inode
&& IS_WHITEOUT(inode
);
243 static bool ovl_is_opaquedir(struct dentry
*dentry
)
247 struct inode
*inode
= dentry
->d_inode
;
249 if (!S_ISDIR(inode
->i_mode
) || !inode
->i_op
->getxattr
)
252 res
= inode
->i_op
->getxattr(dentry
, ovl_opaque_xattr
, &val
, 1);
253 if (res
== 1 && val
== 'y')
259 static void ovl_dentry_release(struct dentry
*dentry
)
261 struct ovl_entry
*oe
= dentry
->d_fsdata
;
264 dput(oe
->__upperdentry
);
265 dput(oe
->lowerdentry
);
270 static const struct dentry_operations ovl_dentry_operations
= {
271 .d_release
= ovl_dentry_release
,
274 static struct ovl_entry
*ovl_alloc_entry(void)
276 return kzalloc(sizeof(struct ovl_entry
), GFP_KERNEL
);
279 static inline struct dentry
*ovl_lookup_real(struct dentry
*dir
,
282 struct dentry
*dentry
;
284 mutex_lock(&dir
->d_inode
->i_mutex
);
285 dentry
= lookup_one_len(name
->name
, dir
, name
->len
);
286 mutex_unlock(&dir
->d_inode
->i_mutex
);
288 if (IS_ERR(dentry
)) {
289 if (PTR_ERR(dentry
) == -ENOENT
)
291 } else if (!dentry
->d_inode
) {
298 struct dentry
*ovl_lookup(struct inode
*dir
, struct dentry
*dentry
,
301 struct ovl_entry
*oe
;
302 struct dentry
*upperdir
;
303 struct dentry
*lowerdir
;
304 struct dentry
*upperdentry
= NULL
;
305 struct dentry
*lowerdentry
= NULL
;
306 struct inode
*inode
= NULL
;
310 oe
= ovl_alloc_entry();
314 upperdir
= ovl_dentry_upper(dentry
->d_parent
);
315 lowerdir
= ovl_dentry_lower(dentry
->d_parent
);
318 upperdentry
= ovl_lookup_real(upperdir
, &dentry
->d_name
);
319 err
= PTR_ERR(upperdentry
);
320 if (IS_ERR(upperdentry
))
323 if (lowerdir
&& upperdentry
) {
324 if (ovl_is_whiteout(upperdentry
)) {
328 } else if (ovl_is_opaquedir(upperdentry
)) {
333 if (lowerdir
&& !oe
->opaque
) {
334 lowerdentry
= ovl_lookup_real(lowerdir
, &dentry
->d_name
);
335 err
= PTR_ERR(lowerdentry
);
336 if (IS_ERR(lowerdentry
))
340 if (lowerdentry
&& upperdentry
&&
341 (!S_ISDIR(upperdentry
->d_inode
->i_mode
) ||
342 !S_ISDIR(lowerdentry
->d_inode
->i_mode
))) {
348 if (lowerdentry
|| upperdentry
) {
349 struct dentry
*realdentry
;
351 realdentry
= upperdentry
? upperdentry
: lowerdentry
;
353 inode
= ovl_new_inode(dentry
->d_sb
, realdentry
->d_inode
->i_mode
,
357 ovl_copyattr(realdentry
->d_inode
, inode
);
360 oe
->__upperdentry
= upperdentry
;
361 oe
->lowerdentry
= lowerdentry
;
363 dentry
->d_fsdata
= oe
;
364 d_add(dentry
, inode
);
378 struct file
*ovl_path_open(struct path
*path
, int flags
)
380 return dentry_open(path
, flags
, current_cred());
383 static void ovl_put_super(struct super_block
*sb
)
385 struct ovl_fs
*ufs
= sb
->s_fs_info
;
388 mntput(ufs
->upper_mnt
);
389 mntput(ufs
->lower_mnt
);
391 kfree(ufs
->config
.lowerdir
);
392 kfree(ufs
->config
.upperdir
);
393 kfree(ufs
->config
.workdir
);
399 * @sb: The overlayfs super block
400 * @buf: The struct kstatfs to fill in with stats
402 * Get the filesystem statistics. As writes always target the upper layer
403 * filesystem pass the statfs to the same filesystem.
405 static int ovl_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
407 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
408 struct dentry
*root_dentry
= dentry
->d_sb
->s_root
;
412 ovl_path_upper(root_dentry
, &path
);
414 err
= vfs_statfs(&path
, buf
);
416 buf
->f_namelen
= max(buf
->f_namelen
, ofs
->lower_namelen
);
417 buf
->f_type
= OVERLAYFS_SUPER_MAGIC
;
426 * Prints the mount options for a given superblock.
427 * Returns zero; does not fail.
429 static int ovl_show_options(struct seq_file
*m
, struct dentry
*dentry
)
431 struct super_block
*sb
= dentry
->d_sb
;
432 struct ovl_fs
*ufs
= sb
->s_fs_info
;
434 seq_printf(m
, ",lowerdir=%s", ufs
->config
.lowerdir
);
435 seq_printf(m
, ",upperdir=%s", ufs
->config
.upperdir
);
436 seq_printf(m
, ",workdir=%s", ufs
->config
.workdir
);
440 static const struct super_operations ovl_super_operations
= {
441 .put_super
= ovl_put_super
,
442 .statfs
= ovl_statfs
,
443 .show_options
= ovl_show_options
,
453 static const match_table_t ovl_tokens
= {
454 {OPT_LOWERDIR
, "lowerdir=%s"},
455 {OPT_UPPERDIR
, "upperdir=%s"},
456 {OPT_WORKDIR
, "workdir=%s"},
460 static char *ovl_next_opt(char **s
)
468 for (p
= sbegin
; *p
; p
++) {
473 } else if (*p
== ',') {
483 static int ovl_parse_opt(char *opt
, struct ovl_config
*config
)
487 while ((p
= ovl_next_opt(&opt
)) != NULL
) {
489 substring_t args
[MAX_OPT_ARGS
];
494 token
= match_token(p
, ovl_tokens
, args
);
497 kfree(config
->upperdir
);
498 config
->upperdir
= match_strdup(&args
[0]);
499 if (!config
->upperdir
)
504 kfree(config
->lowerdir
);
505 config
->lowerdir
= match_strdup(&args
[0]);
506 if (!config
->lowerdir
)
511 kfree(config
->workdir
);
512 config
->workdir
= match_strdup(&args
[0]);
513 if (!config
->workdir
)
524 #define OVL_WORKDIR_NAME "work"
526 static struct dentry
*ovl_workdir_create(struct vfsmount
*mnt
,
527 struct dentry
*dentry
)
529 struct inode
*dir
= dentry
->d_inode
;
532 bool retried
= false;
534 err
= mnt_want_write(mnt
);
538 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
540 work
= lookup_one_len(OVL_WORKDIR_NAME
, dentry
,
541 strlen(OVL_WORKDIR_NAME
));
544 struct kstat stat
= {
554 ovl_cleanup(dir
, work
);
559 err
= ovl_create_real(dir
, work
, &stat
, NULL
, NULL
, true);
564 mutex_unlock(&dir
->i_mutex
);
575 static void ovl_unescape(char *s
)
588 static int ovl_mount_dir(const char *name
, struct path
*path
)
591 char *tmp
= kstrdup(name
, GFP_KERNEL
);
597 err
= kern_path(tmp
, LOOKUP_FOLLOW
, path
);
599 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp
, err
);
606 static bool ovl_is_allowed_fs_type(struct dentry
*root
)
608 const struct dentry_operations
*dop
= root
->d_op
;
612 * - automount filesystems
613 * - filesystems with revalidate (FIXME for lower layer)
614 * - filesystems with case insensitive names
617 (dop
->d_manage
|| dop
->d_automount
||
618 dop
->d_revalidate
|| dop
->d_weak_revalidate
||
619 dop
->d_compare
|| dop
->d_hash
)) {
625 /* Workdir should not be subdir of upperdir and vice versa */
626 static bool ovl_workdir_ok(struct dentry
*workdir
, struct dentry
*upperdir
)
630 if (workdir
!= upperdir
) {
631 ok
= (lock_rename(workdir
, upperdir
) == NULL
);
632 unlock_rename(workdir
, upperdir
);
637 static int ovl_fill_super(struct super_block
*sb
, void *data
, int silent
)
639 struct path lowerpath
;
640 struct path upperpath
;
641 struct path workpath
;
642 struct inode
*root_inode
;
643 struct dentry
*root_dentry
;
644 struct ovl_entry
*oe
;
646 struct kstatfs statfs
;
650 ufs
= kzalloc(sizeof(struct ovl_fs
), GFP_KERNEL
);
654 err
= ovl_parse_opt((char *) data
, &ufs
->config
);
656 goto out_free_config
;
658 /* FIXME: workdir is not needed for a R/O mount */
660 if (!ufs
->config
.upperdir
|| !ufs
->config
.lowerdir
||
661 !ufs
->config
.workdir
) {
662 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
663 goto out_free_config
;
667 oe
= ovl_alloc_entry();
669 goto out_free_config
;
671 err
= ovl_mount_dir(ufs
->config
.upperdir
, &upperpath
);
675 err
= ovl_mount_dir(ufs
->config
.lowerdir
, &lowerpath
);
677 goto out_put_upperpath
;
679 err
= ovl_mount_dir(ufs
->config
.workdir
, &workpath
);
681 goto out_put_lowerpath
;
684 if (!S_ISDIR(upperpath
.dentry
->d_inode
->i_mode
) ||
685 !S_ISDIR(lowerpath
.dentry
->d_inode
->i_mode
) ||
686 !S_ISDIR(workpath
.dentry
->d_inode
->i_mode
)) {
687 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
688 goto out_put_workpath
;
691 if (upperpath
.mnt
!= workpath
.mnt
) {
692 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
693 goto out_put_workpath
;
695 if (!ovl_workdir_ok(workpath
.dentry
, upperpath
.dentry
)) {
696 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
697 goto out_put_workpath
;
700 if (!ovl_is_allowed_fs_type(upperpath
.dentry
)) {
701 pr_err("overlayfs: filesystem of upperdir is not supported\n");
702 goto out_put_workpath
;
705 if (!ovl_is_allowed_fs_type(lowerpath
.dentry
)) {
706 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
707 goto out_put_workpath
;
710 err
= vfs_statfs(&lowerpath
, &statfs
);
712 pr_err("overlayfs: statfs failed on lowerpath\n");
713 goto out_put_workpath
;
715 ufs
->lower_namelen
= statfs
.f_namelen
;
717 sb
->s_stack_depth
= max(upperpath
.mnt
->mnt_sb
->s_stack_depth
,
718 lowerpath
.mnt
->mnt_sb
->s_stack_depth
) + 1;
721 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
722 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
723 goto out_put_workpath
;
726 ufs
->upper_mnt
= clone_private_mount(&upperpath
);
727 err
= PTR_ERR(ufs
->upper_mnt
);
728 if (IS_ERR(ufs
->upper_mnt
)) {
729 pr_err("overlayfs: failed to clone upperpath\n");
730 goto out_put_workpath
;
733 ufs
->lower_mnt
= clone_private_mount(&lowerpath
);
734 err
= PTR_ERR(ufs
->lower_mnt
);
735 if (IS_ERR(ufs
->lower_mnt
)) {
736 pr_err("overlayfs: failed to clone lowerpath\n");
737 goto out_put_upper_mnt
;
740 ufs
->workdir
= ovl_workdir_create(ufs
->upper_mnt
, workpath
.dentry
);
741 err
= PTR_ERR(ufs
->workdir
);
742 if (IS_ERR(ufs
->workdir
)) {
743 pr_err("overlayfs: failed to create directory %s/%s\n",
744 ufs
->config
.workdir
, OVL_WORKDIR_NAME
);
745 goto out_put_lower_mnt
;
749 * Make lower_mnt R/O. That way fchmod/fchown on lower file
750 * will fail instead of modifying lower fs.
752 ufs
->lower_mnt
->mnt_flags
|= MNT_READONLY
;
754 /* If the upper fs is r/o, we mark overlayfs r/o too */
755 if (ufs
->upper_mnt
->mnt_sb
->s_flags
& MS_RDONLY
)
756 sb
->s_flags
|= MS_RDONLY
;
758 sb
->s_d_op
= &ovl_dentry_operations
;
761 root_inode
= ovl_new_inode(sb
, S_IFDIR
, oe
);
763 goto out_put_workdir
;
765 root_dentry
= d_make_root(root_inode
);
767 goto out_put_workdir
;
769 mntput(upperpath
.mnt
);
770 mntput(lowerpath
.mnt
);
773 oe
->__upperdentry
= upperpath
.dentry
;
774 oe
->lowerdentry
= lowerpath
.dentry
;
776 root_dentry
->d_fsdata
= oe
;
778 sb
->s_magic
= OVERLAYFS_SUPER_MAGIC
;
779 sb
->s_op
= &ovl_super_operations
;
780 sb
->s_root
= root_dentry
;
788 mntput(ufs
->lower_mnt
);
790 mntput(ufs
->upper_mnt
);
794 path_put(&lowerpath
);
796 path_put(&upperpath
);
800 kfree(ufs
->config
.lowerdir
);
801 kfree(ufs
->config
.upperdir
);
802 kfree(ufs
->config
.workdir
);
808 static struct dentry
*ovl_mount(struct file_system_type
*fs_type
, int flags
,
809 const char *dev_name
, void *raw_data
)
811 return mount_nodev(fs_type
, flags
, raw_data
, ovl_fill_super
);
814 static struct file_system_type ovl_fs_type
= {
815 .owner
= THIS_MODULE
,
818 .kill_sb
= kill_anon_super
,
820 MODULE_ALIAS_FS("overlay");
822 static int __init
ovl_init(void)
824 return register_filesystem(&ovl_fs_type
);
827 static void __exit
ovl_exit(void)
829 unregister_filesystem(&ovl_fs_type
);
832 module_init(ovl_init
);
833 module_exit(ovl_exit
);