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.
10 #include <uapi/linux/magic.h>
12 #include <linux/namei.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.h>
15 #include <linux/parser.h>
16 #include <linux/module.h>
17 #include <linux/statfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/posix_acl_xattr.h>
20 #include "overlayfs.h"
22 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23 MODULE_DESCRIPTION("Overlay filesystem");
24 MODULE_LICENSE("GPL");
29 #define OVL_MAX_STACK 500
31 static bool ovl_redirect_dir_def
= IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR
);
32 module_param_named(redirect_dir
, ovl_redirect_dir_def
, bool, 0644);
33 MODULE_PARM_DESC(ovl_redirect_dir_def
,
34 "Default to on or off for the redirect_dir feature");
36 static bool ovl_redirect_always_follow
=
37 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW
);
38 module_param_named(redirect_always_follow
, ovl_redirect_always_follow
,
40 MODULE_PARM_DESC(ovl_redirect_always_follow
,
41 "Follow redirects even if redirect_dir feature is turned off");
43 static bool ovl_index_def
= IS_ENABLED(CONFIG_OVERLAY_FS_INDEX
);
44 module_param_named(index
, ovl_index_def
, bool, 0644);
45 MODULE_PARM_DESC(ovl_index_def
,
46 "Default to on or off for the inodes index feature");
48 static bool ovl_nfs_export_def
= IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT
);
49 module_param_named(nfs_export
, ovl_nfs_export_def
, bool, 0644);
50 MODULE_PARM_DESC(ovl_nfs_export_def
,
51 "Default to on or off for the NFS export feature");
53 static void ovl_entry_stack_free(struct ovl_entry
*oe
)
57 for (i
= 0; i
< oe
->numlower
; i
++)
58 dput(oe
->lowerstack
[i
].dentry
);
61 static void ovl_dentry_release(struct dentry
*dentry
)
63 struct ovl_entry
*oe
= dentry
->d_fsdata
;
66 ovl_entry_stack_free(oe
);
71 static int ovl_check_append_only(struct inode
*inode
, int flag
)
74 * This test was moot in vfs may_open() because overlay inode does
75 * not have the S_APPEND flag, so re-check on real upper inode
77 if (IS_APPEND(inode
)) {
78 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
87 static struct dentry
*ovl_d_real(struct dentry
*dentry
,
88 const struct inode
*inode
,
89 unsigned int open_flags
, unsigned int flags
)
94 if (flags
& D_REAL_UPPER
)
95 return ovl_dentry_upper(dentry
);
97 if (!d_is_reg(dentry
)) {
98 if (!inode
|| inode
== d_inode(dentry
))
104 err
= ovl_open_maybe_copy_up(dentry
, open_flags
);
109 real
= ovl_dentry_upper(dentry
);
110 if (real
&& (!inode
|| inode
== d_inode(real
))) {
112 err
= ovl_check_append_only(d_inode(real
), open_flags
);
119 real
= ovl_dentry_lower(dentry
);
123 /* Handle recursion */
124 real
= d_real(real
, inode
, open_flags
, 0);
126 if (!inode
|| inode
== d_inode(real
))
129 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry
,
130 inode
? inode
->i_sb
->s_id
: "NULL", inode
? inode
->i_ino
: 0);
134 static int ovl_dentry_revalidate(struct dentry
*dentry
, unsigned int flags
)
136 struct ovl_entry
*oe
= dentry
->d_fsdata
;
140 for (i
= 0; i
< oe
->numlower
; i
++) {
141 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
143 if (d
->d_flags
& DCACHE_OP_REVALIDATE
) {
144 ret
= d
->d_op
->d_revalidate(d
, flags
);
148 if (!(flags
& LOOKUP_RCU
))
157 static int ovl_dentry_weak_revalidate(struct dentry
*dentry
, unsigned int flags
)
159 struct ovl_entry
*oe
= dentry
->d_fsdata
;
163 for (i
= 0; i
< oe
->numlower
; i
++) {
164 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
166 if (d
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
) {
167 ret
= d
->d_op
->d_weak_revalidate(d
, flags
);
175 static const struct dentry_operations ovl_dentry_operations
= {
176 .d_release
= ovl_dentry_release
,
177 .d_real
= ovl_d_real
,
180 static const struct dentry_operations ovl_reval_dentry_operations
= {
181 .d_release
= ovl_dentry_release
,
182 .d_real
= ovl_d_real
,
183 .d_revalidate
= ovl_dentry_revalidate
,
184 .d_weak_revalidate
= ovl_dentry_weak_revalidate
,
187 static struct kmem_cache
*ovl_inode_cachep
;
189 static struct inode
*ovl_alloc_inode(struct super_block
*sb
)
191 struct ovl_inode
*oi
= kmem_cache_alloc(ovl_inode_cachep
, GFP_KERNEL
);
200 oi
->__upperdentry
= NULL
;
202 mutex_init(&oi
->lock
);
204 return &oi
->vfs_inode
;
207 static void ovl_i_callback(struct rcu_head
*head
)
209 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
211 kmem_cache_free(ovl_inode_cachep
, OVL_I(inode
));
214 static void ovl_destroy_inode(struct inode
*inode
)
216 struct ovl_inode
*oi
= OVL_I(inode
);
218 dput(oi
->__upperdentry
);
221 ovl_dir_cache_free(inode
);
222 mutex_destroy(&oi
->lock
);
224 call_rcu(&inode
->i_rcu
, ovl_i_callback
);
227 static void ovl_free_fs(struct ovl_fs
*ofs
)
233 if (ofs
->workdir_locked
)
234 ovl_inuse_unlock(ofs
->workbasedir
);
235 dput(ofs
->workbasedir
);
236 if (ofs
->upperdir_locked
)
237 ovl_inuse_unlock(ofs
->upper_mnt
->mnt_root
);
238 mntput(ofs
->upper_mnt
);
239 for (i
= 0; i
< ofs
->numlower
; i
++) {
240 mntput(ofs
->lower_layers
[i
].mnt
);
241 free_anon_bdev(ofs
->lower_layers
[i
].pseudo_dev
);
243 kfree(ofs
->lower_layers
);
245 kfree(ofs
->config
.lowerdir
);
246 kfree(ofs
->config
.upperdir
);
247 kfree(ofs
->config
.workdir
);
248 kfree(ofs
->config
.redirect_mode
);
249 if (ofs
->creator_cred
)
250 put_cred(ofs
->creator_cred
);
254 static void ovl_put_super(struct super_block
*sb
)
256 struct ovl_fs
*ofs
= sb
->s_fs_info
;
261 /* Sync real dirty inodes in upper filesystem (if it exists) */
262 static int ovl_sync_fs(struct super_block
*sb
, int wait
)
264 struct ovl_fs
*ofs
= sb
->s_fs_info
;
265 struct super_block
*upper_sb
;
272 * If this is a sync(2) call or an emergency sync, all the super blocks
273 * will be iterated, including upper_sb, so no need to do anything.
275 * If this is a syncfs(2) call, then we do need to call
276 * sync_filesystem() on upper_sb, but enough if we do it when being
277 * called with wait == 1.
282 upper_sb
= ofs
->upper_mnt
->mnt_sb
;
284 down_read(&upper_sb
->s_umount
);
285 ret
= sync_filesystem(upper_sb
);
286 up_read(&upper_sb
->s_umount
);
293 * @sb: The overlayfs super block
294 * @buf: The struct kstatfs to fill in with stats
296 * Get the filesystem statistics. As writes always target the upper layer
297 * filesystem pass the statfs to the upper filesystem (if it exists)
299 static int ovl_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
301 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
302 struct dentry
*root_dentry
= dentry
->d_sb
->s_root
;
306 ovl_path_real(root_dentry
, &path
);
308 err
= vfs_statfs(&path
, buf
);
310 buf
->f_namelen
= ofs
->namelen
;
311 buf
->f_type
= OVERLAYFS_SUPER_MAGIC
;
317 /* Will this overlay be forced to mount/remount ro? */
318 static bool ovl_force_readonly(struct ovl_fs
*ofs
)
320 return (!ofs
->upper_mnt
|| !ofs
->workdir
);
323 static const char *ovl_redirect_mode_def(void)
325 return ovl_redirect_dir_def
? "on" : "off";
331 * Prints the mount options for a given superblock.
332 * Returns zero; does not fail.
334 static int ovl_show_options(struct seq_file
*m
, struct dentry
*dentry
)
336 struct super_block
*sb
= dentry
->d_sb
;
337 struct ovl_fs
*ofs
= sb
->s_fs_info
;
339 seq_show_option(m
, "lowerdir", ofs
->config
.lowerdir
);
340 if (ofs
->config
.upperdir
) {
341 seq_show_option(m
, "upperdir", ofs
->config
.upperdir
);
342 seq_show_option(m
, "workdir", ofs
->config
.workdir
);
344 if (ofs
->config
.default_permissions
)
345 seq_puts(m
, ",default_permissions");
346 if (strcmp(ofs
->config
.redirect_mode
, ovl_redirect_mode_def()) != 0)
347 seq_printf(m
, ",redirect_dir=%s", ofs
->config
.redirect_mode
);
348 if (ofs
->config
.index
!= ovl_index_def
)
349 seq_printf(m
, ",index=%s", ofs
->config
.index
? "on" : "off");
350 if (ofs
->config
.nfs_export
!= ovl_nfs_export_def
)
351 seq_printf(m
, ",nfs_export=%s", ofs
->config
.nfs_export
?
356 static int ovl_remount(struct super_block
*sb
, int *flags
, char *data
)
358 struct ovl_fs
*ofs
= sb
->s_fs_info
;
360 if (!(*flags
& SB_RDONLY
) && ovl_force_readonly(ofs
))
366 static const struct super_operations ovl_super_operations
= {
367 .alloc_inode
= ovl_alloc_inode
,
368 .destroy_inode
= ovl_destroy_inode
,
369 .drop_inode
= generic_delete_inode
,
370 .put_super
= ovl_put_super
,
371 .sync_fs
= ovl_sync_fs
,
372 .statfs
= ovl_statfs
,
373 .show_options
= ovl_show_options
,
374 .remount_fs
= ovl_remount
,
381 OPT_DEFAULT_PERMISSIONS
,
390 static const match_table_t ovl_tokens
= {
391 {OPT_LOWERDIR
, "lowerdir=%s"},
392 {OPT_UPPERDIR
, "upperdir=%s"},
393 {OPT_WORKDIR
, "workdir=%s"},
394 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
395 {OPT_REDIRECT_DIR
, "redirect_dir=%s"},
396 {OPT_INDEX_ON
, "index=on"},
397 {OPT_INDEX_OFF
, "index=off"},
398 {OPT_NFS_EXPORT_ON
, "nfs_export=on"},
399 {OPT_NFS_EXPORT_OFF
, "nfs_export=off"},
403 static char *ovl_next_opt(char **s
)
411 for (p
= sbegin
; *p
; p
++) {
416 } else if (*p
== ',') {
426 static int ovl_parse_redirect_mode(struct ovl_config
*config
, const char *mode
)
428 if (strcmp(mode
, "on") == 0) {
429 config
->redirect_dir
= true;
431 * Does not make sense to have redirect creation without
432 * redirect following.
434 config
->redirect_follow
= true;
435 } else if (strcmp(mode
, "follow") == 0) {
436 config
->redirect_follow
= true;
437 } else if (strcmp(mode
, "off") == 0) {
438 if (ovl_redirect_always_follow
)
439 config
->redirect_follow
= true;
440 } else if (strcmp(mode
, "nofollow") != 0) {
441 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
449 static int ovl_parse_opt(char *opt
, struct ovl_config
*config
)
453 config
->redirect_mode
= kstrdup(ovl_redirect_mode_def(), GFP_KERNEL
);
454 if (!config
->redirect_mode
)
457 while ((p
= ovl_next_opt(&opt
)) != NULL
) {
459 substring_t args
[MAX_OPT_ARGS
];
464 token
= match_token(p
, ovl_tokens
, args
);
467 kfree(config
->upperdir
);
468 config
->upperdir
= match_strdup(&args
[0]);
469 if (!config
->upperdir
)
474 kfree(config
->lowerdir
);
475 config
->lowerdir
= match_strdup(&args
[0]);
476 if (!config
->lowerdir
)
481 kfree(config
->workdir
);
482 config
->workdir
= match_strdup(&args
[0]);
483 if (!config
->workdir
)
487 case OPT_DEFAULT_PERMISSIONS
:
488 config
->default_permissions
= true;
491 case OPT_REDIRECT_DIR
:
492 kfree(config
->redirect_mode
);
493 config
->redirect_mode
= match_strdup(&args
[0]);
494 if (!config
->redirect_mode
)
499 config
->index
= true;
503 config
->index
= false;
506 case OPT_NFS_EXPORT_ON
:
507 config
->nfs_export
= true;
510 case OPT_NFS_EXPORT_OFF
:
511 config
->nfs_export
= false;
515 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p
);
520 /* Workdir is useless in non-upper mount */
521 if (!config
->upperdir
&& config
->workdir
) {
522 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
524 kfree(config
->workdir
);
525 config
->workdir
= NULL
;
528 return ovl_parse_redirect_mode(config
, config
->redirect_mode
);
531 #define OVL_WORKDIR_NAME "work"
532 #define OVL_INDEXDIR_NAME "index"
534 static struct dentry
*ovl_workdir_create(struct ovl_fs
*ofs
,
535 const char *name
, bool persist
)
537 struct inode
*dir
= ofs
->workbasedir
->d_inode
;
538 struct vfsmount
*mnt
= ofs
->upper_mnt
;
541 bool retried
= false;
544 inode_lock_nested(dir
, I_MUTEX_PARENT
);
548 work
= lookup_one_len(name
, ofs
->workbasedir
, strlen(name
));
551 struct iattr attr
= {
552 .ia_valid
= ATTR_MODE
,
553 .ia_mode
= S_IFDIR
| 0,
565 ovl_workdir_cleanup(dir
, mnt
, work
, 0);
570 err
= ovl_create_real(dir
, work
,
571 &(struct cattr
){.mode
= S_IFDIR
| 0},
577 * Try to remove POSIX ACL xattrs from workdir. We are good if:
579 * a) success (there was a POSIX ACL xattr and was removed)
580 * b) -ENODATA (there was no POSIX ACL xattr)
581 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
583 * There are various other error values that could effectively
584 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
585 * if the xattr name is too long), but the set of filesystems
586 * allowed as upper are limited to "normal" ones, where checking
587 * for the above two errors is sufficient.
589 err
= vfs_removexattr(work
, XATTR_NAME_POSIX_ACL_DEFAULT
);
590 if (err
&& err
!= -ENODATA
&& err
!= -EOPNOTSUPP
)
593 err
= vfs_removexattr(work
, XATTR_NAME_POSIX_ACL_ACCESS
);
594 if (err
&& err
!= -ENODATA
&& err
!= -EOPNOTSUPP
)
597 /* Clear any inherited mode bits */
598 inode_lock(work
->d_inode
);
599 err
= notify_change(work
, &attr
, NULL
);
600 inode_unlock(work
->d_inode
);
616 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
617 ofs
->config
.workdir
, name
, -err
);
622 static void ovl_unescape(char *s
)
635 static int ovl_mount_dir_noesc(const char *name
, struct path
*path
)
640 pr_err("overlayfs: empty lowerdir\n");
643 err
= kern_path(name
, LOOKUP_FOLLOW
, path
);
645 pr_err("overlayfs: failed to resolve '%s': %i\n", name
, err
);
649 if (ovl_dentry_weird(path
->dentry
)) {
650 pr_err("overlayfs: filesystem on '%s' not supported\n", name
);
653 if (!d_is_dir(path
->dentry
)) {
654 pr_err("overlayfs: '%s' not a directory\n", name
);
665 static int ovl_mount_dir(const char *name
, struct path
*path
)
668 char *tmp
= kstrdup(name
, GFP_KERNEL
);
672 err
= ovl_mount_dir_noesc(tmp
, path
);
675 if (ovl_dentry_remote(path
->dentry
)) {
676 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
686 static int ovl_check_namelen(struct path
*path
, struct ovl_fs
*ofs
,
689 struct kstatfs statfs
;
690 int err
= vfs_statfs(path
, &statfs
);
693 pr_err("overlayfs: statfs failed on '%s'\n", name
);
695 ofs
->namelen
= max(ofs
->namelen
, statfs
.f_namelen
);
700 static int ovl_lower_dir(const char *name
, struct path
*path
,
701 struct ovl_fs
*ofs
, int *stack_depth
, bool *remote
)
705 err
= ovl_mount_dir_noesc(name
, path
);
709 err
= ovl_check_namelen(path
, ofs
, name
);
713 *stack_depth
= max(*stack_depth
, path
->mnt
->mnt_sb
->s_stack_depth
);
715 if (ovl_dentry_remote(path
->dentry
))
719 * The inodes index feature and NFS export need to encode and decode
720 * file handles, so they require that all layers support them.
722 if ((ofs
->config
.nfs_export
||
723 (ofs
->config
.index
&& ofs
->config
.upperdir
)) &&
724 !ovl_can_decode_fh(path
->dentry
->d_sb
)) {
725 ofs
->config
.index
= false;
726 ofs
->config
.nfs_export
= false;
727 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
739 /* Workdir should not be subdir of upperdir and vice versa */
740 static bool ovl_workdir_ok(struct dentry
*workdir
, struct dentry
*upperdir
)
744 if (workdir
!= upperdir
) {
745 ok
= (lock_rename(workdir
, upperdir
) == NULL
);
746 unlock_rename(workdir
, upperdir
);
751 static unsigned int ovl_split_lowerdirs(char *str
)
753 unsigned int ctr
= 1;
756 for (s
= d
= str
;; s
++, d
++) {
759 } else if (*s
== ':') {
771 static int __maybe_unused
772 ovl_posix_acl_xattr_get(const struct xattr_handler
*handler
,
773 struct dentry
*dentry
, struct inode
*inode
,
774 const char *name
, void *buffer
, size_t size
)
776 return ovl_xattr_get(dentry
, inode
, handler
->name
, buffer
, size
);
779 static int __maybe_unused
780 ovl_posix_acl_xattr_set(const struct xattr_handler
*handler
,
781 struct dentry
*dentry
, struct inode
*inode
,
782 const char *name
, const void *value
,
783 size_t size
, int flags
)
785 struct dentry
*workdir
= ovl_workdir(dentry
);
786 struct inode
*realinode
= ovl_inode_real(inode
);
787 struct posix_acl
*acl
= NULL
;
790 /* Check that everything is OK before copy-up */
792 acl
= posix_acl_from_xattr(&init_user_ns
, value
, size
);
797 if (!IS_POSIXACL(d_inode(workdir
)))
798 goto out_acl_release
;
799 if (!realinode
->i_op
->set_acl
)
800 goto out_acl_release
;
801 if (handler
->flags
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
)) {
802 err
= acl
? -EACCES
: 0;
803 goto out_acl_release
;
806 if (!inode_owner_or_capable(inode
))
807 goto out_acl_release
;
809 posix_acl_release(acl
);
812 * Check if sgid bit needs to be cleared (actual setacl operation will
813 * be done with mounter's capabilities and so that won't do it for us).
815 if (unlikely(inode
->i_mode
& S_ISGID
) &&
816 handler
->flags
== ACL_TYPE_ACCESS
&&
817 !in_group_p(inode
->i_gid
) &&
818 !capable_wrt_inode_uidgid(inode
, CAP_FSETID
)) {
819 struct iattr iattr
= { .ia_valid
= ATTR_KILL_SGID
};
821 err
= ovl_setattr(dentry
, &iattr
);
826 err
= ovl_xattr_set(dentry
, inode
, handler
->name
, value
, size
, flags
);
828 ovl_copyattr(ovl_inode_real(inode
), inode
);
833 posix_acl_release(acl
);
837 static int ovl_own_xattr_get(const struct xattr_handler
*handler
,
838 struct dentry
*dentry
, struct inode
*inode
,
839 const char *name
, void *buffer
, size_t size
)
844 static int ovl_own_xattr_set(const struct xattr_handler
*handler
,
845 struct dentry
*dentry
, struct inode
*inode
,
846 const char *name
, const void *value
,
847 size_t size
, int flags
)
852 static int ovl_other_xattr_get(const struct xattr_handler
*handler
,
853 struct dentry
*dentry
, struct inode
*inode
,
854 const char *name
, void *buffer
, size_t size
)
856 return ovl_xattr_get(dentry
, inode
, name
, buffer
, size
);
859 static int ovl_other_xattr_set(const struct xattr_handler
*handler
,
860 struct dentry
*dentry
, struct inode
*inode
,
861 const char *name
, const void *value
,
862 size_t size
, int flags
)
864 return ovl_xattr_set(dentry
, inode
, name
, value
, size
, flags
);
867 static const struct xattr_handler __maybe_unused
868 ovl_posix_acl_access_xattr_handler
= {
869 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
870 .flags
= ACL_TYPE_ACCESS
,
871 .get
= ovl_posix_acl_xattr_get
,
872 .set
= ovl_posix_acl_xattr_set
,
875 static const struct xattr_handler __maybe_unused
876 ovl_posix_acl_default_xattr_handler
= {
877 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
878 .flags
= ACL_TYPE_DEFAULT
,
879 .get
= ovl_posix_acl_xattr_get
,
880 .set
= ovl_posix_acl_xattr_set
,
883 static const struct xattr_handler ovl_own_xattr_handler
= {
884 .prefix
= OVL_XATTR_PREFIX
,
885 .get
= ovl_own_xattr_get
,
886 .set
= ovl_own_xattr_set
,
889 static const struct xattr_handler ovl_other_xattr_handler
= {
890 .prefix
= "", /* catch all */
891 .get
= ovl_other_xattr_get
,
892 .set
= ovl_other_xattr_set
,
895 static const struct xattr_handler
*ovl_xattr_handlers
[] = {
896 #ifdef CONFIG_FS_POSIX_ACL
897 &ovl_posix_acl_access_xattr_handler
,
898 &ovl_posix_acl_default_xattr_handler
,
900 &ovl_own_xattr_handler
,
901 &ovl_other_xattr_handler
,
905 static int ovl_get_upper(struct ovl_fs
*ofs
, struct path
*upperpath
)
907 struct vfsmount
*upper_mnt
;
910 err
= ovl_mount_dir(ofs
->config
.upperdir
, upperpath
);
914 /* Upper fs should not be r/o */
915 if (sb_rdonly(upperpath
->mnt
->mnt_sb
)) {
916 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
921 err
= ovl_check_namelen(upperpath
, ofs
, ofs
->config
.upperdir
);
926 if (ovl_inuse_trylock(upperpath
->dentry
)) {
927 ofs
->upperdir_locked
= true;
928 } else if (ofs
->config
.index
) {
929 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
932 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
935 upper_mnt
= clone_private_mount(upperpath
);
936 err
= PTR_ERR(upper_mnt
);
937 if (IS_ERR(upper_mnt
)) {
938 pr_err("overlayfs: failed to clone upperpath\n");
942 /* Don't inherit atime flags */
943 upper_mnt
->mnt_flags
&= ~(MNT_NOATIME
| MNT_NODIRATIME
| MNT_RELATIME
);
944 ofs
->upper_mnt
= upper_mnt
;
950 static int ovl_make_workdir(struct ovl_fs
*ofs
, struct path
*workpath
)
952 struct vfsmount
*mnt
= ofs
->upper_mnt
;
956 err
= mnt_want_write(mnt
);
960 ofs
->workdir
= ovl_workdir_create(ofs
, OVL_WORKDIR_NAME
, false);
965 * Upper should support d_type, else whiteouts are visible. Given
966 * workdir and upper are on same fs, we can do iterate_dir() on
967 * workdir. This check requires successful creation of workdir in
970 err
= ovl_check_d_type_supported(workpath
);
975 * We allowed this configuration and don't want to break users over
976 * kernel upgrade. So warn instead of erroring out.
979 pr_warn("overlayfs: upper fs needs to support d_type.\n");
981 /* Check if upper/work fs supports O_TMPFILE */
982 temp
= ovl_do_tmpfile(ofs
->workdir
, S_IFREG
| 0);
983 ofs
->tmpfile
= !IS_ERR(temp
);
987 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
990 * Check if upper/work fs supports trusted.overlay.* xattr
992 err
= ovl_do_setxattr(ofs
->workdir
, OVL_XATTR_OPAQUE
, "0", 1, 0);
995 ofs
->config
.index
= false;
996 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
999 vfs_removexattr(ofs
->workdir
, OVL_XATTR_OPAQUE
);
1002 /* Check if upper/work fs supports file handles */
1003 if (ofs
->config
.index
&&
1004 !ovl_can_decode_fh(ofs
->workdir
->d_sb
)) {
1005 ofs
->config
.index
= false;
1006 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1009 /* NFS export of r/w mount depends on index */
1010 if (ofs
->config
.nfs_export
&& !ofs
->config
.index
) {
1011 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1012 ofs
->config
.nfs_export
= false;
1016 mnt_drop_write(mnt
);
1020 static int ovl_get_workdir(struct ovl_fs
*ofs
, struct path
*upperpath
)
1023 struct path workpath
= { };
1025 err
= ovl_mount_dir(ofs
->config
.workdir
, &workpath
);
1030 if (upperpath
->mnt
!= workpath
.mnt
) {
1031 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1034 if (!ovl_workdir_ok(workpath
.dentry
, upperpath
->dentry
)) {
1035 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1040 if (ovl_inuse_trylock(workpath
.dentry
)) {
1041 ofs
->workdir_locked
= true;
1042 } else if (ofs
->config
.index
) {
1043 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1046 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1049 ofs
->workbasedir
= dget(workpath
.dentry
);
1050 err
= ovl_make_workdir(ofs
, &workpath
);
1056 path_put(&workpath
);
1061 static int ovl_get_indexdir(struct ovl_fs
*ofs
, struct ovl_entry
*oe
,
1062 struct path
*upperpath
)
1064 struct vfsmount
*mnt
= ofs
->upper_mnt
;
1067 err
= mnt_want_write(mnt
);
1071 /* Verify lower root is upper root origin */
1072 err
= ovl_verify_origin(upperpath
->dentry
, oe
->lowerstack
[0].dentry
,
1075 pr_err("overlayfs: failed to verify upper root origin\n");
1079 ofs
->indexdir
= ovl_workdir_create(ofs
, OVL_INDEXDIR_NAME
, true);
1080 if (ofs
->indexdir
) {
1082 * Verify upper root is exclusively associated with index dir.
1083 * Older kernels stored upper fh in "trusted.overlay.origin"
1084 * xattr. If that xattr exists, verify that it is a match to
1085 * upper dir file handle. In any case, verify or set xattr
1086 * "trusted.overlay.upper" to indicate that index may have
1087 * directory entries.
1089 if (ovl_check_origin_xattr(ofs
->indexdir
)) {
1090 err
= ovl_verify_set_fh(ofs
->indexdir
, OVL_XATTR_ORIGIN
,
1091 upperpath
->dentry
, true, false);
1093 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1095 err
= ovl_verify_upper(ofs
->indexdir
, upperpath
->dentry
, true);
1097 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
1099 /* Cleanup bad/stale/orphan index entries */
1101 err
= ovl_indexdir_cleanup(ofs
);
1103 if (err
|| !ofs
->indexdir
)
1104 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1107 mnt_drop_write(mnt
);
1111 static int ovl_get_lower_layers(struct ovl_fs
*ofs
, struct path
*stack
,
1112 unsigned int numlower
)
1118 ofs
->lower_layers
= kcalloc(numlower
, sizeof(struct ovl_layer
),
1120 if (ofs
->lower_layers
== NULL
)
1122 for (i
= 0; i
< numlower
; i
++) {
1123 struct vfsmount
*mnt
;
1126 err
= get_anon_bdev(&dev
);
1128 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1132 mnt
= clone_private_mount(&stack
[i
]);
1135 pr_err("overlayfs: failed to clone lowerpath\n");
1136 free_anon_bdev(dev
);
1140 * Make lower layers R/O. That way fchmod/fchown on lower file
1141 * will fail instead of modifying lower fs.
1143 mnt
->mnt_flags
|= MNT_READONLY
| MNT_NOATIME
;
1145 ofs
->lower_layers
[ofs
->numlower
].mnt
= mnt
;
1146 ofs
->lower_layers
[ofs
->numlower
].pseudo_dev
= dev
;
1147 ofs
->lower_layers
[ofs
->numlower
].idx
= i
+ 1;
1150 /* Check if all lower layers are on same sb */
1152 ofs
->same_sb
= mnt
->mnt_sb
;
1153 else if (ofs
->same_sb
!= mnt
->mnt_sb
)
1154 ofs
->same_sb
= NULL
;
1161 static struct ovl_entry
*ovl_get_lowerstack(struct super_block
*sb
,
1165 char *lowertmp
, *lower
;
1166 struct path
*stack
= NULL
;
1167 unsigned int stacklen
, numlower
= 0, i
;
1168 bool remote
= false;
1169 struct ovl_entry
*oe
;
1172 lowertmp
= kstrdup(ofs
->config
.lowerdir
, GFP_KERNEL
);
1177 stacklen
= ovl_split_lowerdirs(lowertmp
);
1178 if (stacklen
> OVL_MAX_STACK
) {
1179 pr_err("overlayfs: too many lower directories, limit is %d\n",
1182 } else if (!ofs
->config
.upperdir
&& stacklen
== 1) {
1183 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1185 } else if (!ofs
->config
.upperdir
&& ofs
->config
.nfs_export
&&
1186 ofs
->config
.redirect_follow
) {
1187 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1188 ofs
->config
.nfs_export
= false;
1192 stack
= kcalloc(stacklen
, sizeof(struct path
), GFP_KERNEL
);
1198 for (numlower
= 0; numlower
< stacklen
; numlower
++) {
1199 err
= ovl_lower_dir(lower
, &stack
[numlower
], ofs
,
1200 &sb
->s_stack_depth
, &remote
);
1204 lower
= strchr(lower
, '\0') + 1;
1208 sb
->s_stack_depth
++;
1209 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
1210 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1214 err
= ovl_get_lower_layers(ofs
, stack
, numlower
);
1219 oe
= ovl_alloc_entry(numlower
);
1223 for (i
= 0; i
< numlower
; i
++) {
1224 oe
->lowerstack
[i
].dentry
= dget(stack
[i
].dentry
);
1225 oe
->lowerstack
[i
].layer
= &ofs
->lower_layers
[i
];
1229 sb
->s_d_op
= &ovl_reval_dentry_operations
;
1231 sb
->s_d_op
= &ovl_dentry_operations
;
1234 for (i
= 0; i
< numlower
; i
++)
1235 path_put(&stack
[i
]);
1246 static int ovl_fill_super(struct super_block
*sb
, void *data
, int silent
)
1248 struct path upperpath
= { };
1249 struct dentry
*root_dentry
;
1250 struct ovl_entry
*oe
;
1256 ofs
= kzalloc(sizeof(struct ovl_fs
), GFP_KERNEL
);
1260 ofs
->creator_cred
= cred
= prepare_creds();
1264 ofs
->config
.index
= ovl_index_def
;
1265 ofs
->config
.nfs_export
= ovl_nfs_export_def
;
1266 err
= ovl_parse_opt((char *) data
, &ofs
->config
);
1271 if (!ofs
->config
.lowerdir
) {
1273 pr_err("overlayfs: missing 'lowerdir'\n");
1277 sb
->s_stack_depth
= 0;
1278 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1279 if (ofs
->config
.upperdir
) {
1280 if (!ofs
->config
.workdir
) {
1281 pr_err("overlayfs: missing 'workdir'\n");
1285 err
= ovl_get_upper(ofs
, &upperpath
);
1289 err
= ovl_get_workdir(ofs
, &upperpath
);
1294 sb
->s_flags
|= SB_RDONLY
;
1296 sb
->s_stack_depth
= ofs
->upper_mnt
->mnt_sb
->s_stack_depth
;
1297 sb
->s_time_gran
= ofs
->upper_mnt
->mnt_sb
->s_time_gran
;
1300 oe
= ovl_get_lowerstack(sb
, ofs
);
1305 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1306 if (!ofs
->upper_mnt
)
1307 sb
->s_flags
|= SB_RDONLY
;
1308 else if (ofs
->upper_mnt
->mnt_sb
!= ofs
->same_sb
)
1309 ofs
->same_sb
= NULL
;
1311 if (!(ovl_force_readonly(ofs
)) && ofs
->config
.index
) {
1312 err
= ovl_get_indexdir(ofs
, oe
, &upperpath
);
1316 /* Force r/o mount with no index dir */
1317 if (!ofs
->indexdir
) {
1319 ofs
->workdir
= NULL
;
1320 sb
->s_flags
|= SB_RDONLY
;
1325 /* Show index=off in /proc/mounts for forced r/o mount */
1326 if (!ofs
->indexdir
) {
1327 ofs
->config
.index
= false;
1328 if (ofs
->upper_mnt
&& ofs
->config
.nfs_export
) {
1329 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1330 ofs
->config
.nfs_export
= false;
1334 if (ofs
->config
.nfs_export
)
1335 sb
->s_export_op
= &ovl_export_operations
;
1337 /* Never override disk quota limits or use reserved space */
1338 cap_lower(cred
->cap_effective
, CAP_SYS_RESOURCE
);
1340 sb
->s_magic
= OVERLAYFS_SUPER_MAGIC
;
1341 sb
->s_op
= &ovl_super_operations
;
1342 sb
->s_xattr
= ovl_xattr_handlers
;
1343 sb
->s_fs_info
= ofs
;
1344 sb
->s_flags
|= SB_POSIXACL
| SB_NOREMOTELOCK
;
1347 root_dentry
= d_make_root(ovl_new_inode(sb
, S_IFDIR
, 0));
1351 root_dentry
->d_fsdata
= oe
;
1353 mntput(upperpath
.mnt
);
1354 if (upperpath
.dentry
) {
1355 ovl_dentry_set_upper_alias(root_dentry
);
1356 if (ovl_is_impuredir(upperpath
.dentry
))
1357 ovl_set_flag(OVL_IMPURE
, d_inode(root_dentry
));
1360 /* Root is always merge -> can have whiteouts */
1361 ovl_set_flag(OVL_WHITEOUTS
, d_inode(root_dentry
));
1362 ovl_inode_init(d_inode(root_dentry
), upperpath
.dentry
,
1363 ovl_dentry_lower(root_dentry
));
1365 sb
->s_root
= root_dentry
;
1370 ovl_entry_stack_free(oe
);
1373 path_put(&upperpath
);
1379 static struct dentry
*ovl_mount(struct file_system_type
*fs_type
, int flags
,
1380 const char *dev_name
, void *raw_data
)
1382 return mount_nodev(fs_type
, flags
, raw_data
, ovl_fill_super
);
1385 static struct file_system_type ovl_fs_type
= {
1386 .owner
= THIS_MODULE
,
1389 .kill_sb
= kill_anon_super
,
1391 MODULE_ALIAS_FS("overlay");
1393 static void ovl_inode_init_once(void *foo
)
1395 struct ovl_inode
*oi
= foo
;
1397 inode_init_once(&oi
->vfs_inode
);
1400 static int __init
ovl_init(void)
1404 ovl_inode_cachep
= kmem_cache_create("ovl_inode",
1405 sizeof(struct ovl_inode
), 0,
1406 (SLAB_RECLAIM_ACCOUNT
|
1407 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
),
1408 ovl_inode_init_once
);
1409 if (ovl_inode_cachep
== NULL
)
1412 err
= register_filesystem(&ovl_fs_type
);
1414 kmem_cache_destroy(ovl_inode_cachep
);
1419 static void __exit
ovl_exit(void)
1421 unregister_filesystem(&ovl_fs_type
);
1424 * Make sure all delayed rcu free inodes are flushed before we
1428 kmem_cache_destroy(ovl_inode_cachep
);
1432 module_init(ovl_init
);
1433 module_exit(ovl_exit
);