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 void ovl_entry_stack_free(struct ovl_entry
*oe
)
52 for (i
= 0; i
< oe
->numlower
; i
++)
53 dput(oe
->lowerstack
[i
].dentry
);
56 static void ovl_dentry_release(struct dentry
*dentry
)
58 struct ovl_entry
*oe
= dentry
->d_fsdata
;
61 ovl_entry_stack_free(oe
);
66 static int ovl_check_append_only(struct inode
*inode
, int flag
)
69 * This test was moot in vfs may_open() because overlay inode does
70 * not have the S_APPEND flag, so re-check on real upper inode
72 if (IS_APPEND(inode
)) {
73 if ((flag
& O_ACCMODE
) != O_RDONLY
&& !(flag
& O_APPEND
))
82 static struct dentry
*ovl_d_real(struct dentry
*dentry
,
83 const struct inode
*inode
,
84 unsigned int open_flags
, unsigned int flags
)
89 if (flags
& D_REAL_UPPER
)
90 return ovl_dentry_upper(dentry
);
92 if (!d_is_reg(dentry
)) {
93 if (!inode
|| inode
== d_inode(dentry
))
99 err
= ovl_open_maybe_copy_up(dentry
, open_flags
);
104 real
= ovl_dentry_upper(dentry
);
105 if (real
&& (!inode
|| inode
== d_inode(real
))) {
107 err
= ovl_check_append_only(d_inode(real
), open_flags
);
114 real
= ovl_dentry_lower(dentry
);
118 /* Handle recursion */
119 real
= d_real(real
, inode
, open_flags
, 0);
121 if (!inode
|| inode
== d_inode(real
))
124 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry
,
125 inode
? inode
->i_sb
->s_id
: "NULL", inode
? inode
->i_ino
: 0);
129 static int ovl_dentry_revalidate(struct dentry
*dentry
, unsigned int flags
)
131 struct ovl_entry
*oe
= dentry
->d_fsdata
;
135 for (i
= 0; i
< oe
->numlower
; i
++) {
136 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
138 if (d
->d_flags
& DCACHE_OP_REVALIDATE
) {
139 ret
= d
->d_op
->d_revalidate(d
, flags
);
143 if (!(flags
& LOOKUP_RCU
))
152 static int ovl_dentry_weak_revalidate(struct dentry
*dentry
, unsigned int flags
)
154 struct ovl_entry
*oe
= dentry
->d_fsdata
;
158 for (i
= 0; i
< oe
->numlower
; i
++) {
159 struct dentry
*d
= oe
->lowerstack
[i
].dentry
;
161 if (d
->d_flags
& DCACHE_OP_WEAK_REVALIDATE
) {
162 ret
= d
->d_op
->d_weak_revalidate(d
, flags
);
170 static const struct dentry_operations ovl_dentry_operations
= {
171 .d_release
= ovl_dentry_release
,
172 .d_real
= ovl_d_real
,
175 static const struct dentry_operations ovl_reval_dentry_operations
= {
176 .d_release
= ovl_dentry_release
,
177 .d_real
= ovl_d_real
,
178 .d_revalidate
= ovl_dentry_revalidate
,
179 .d_weak_revalidate
= ovl_dentry_weak_revalidate
,
182 static struct kmem_cache
*ovl_inode_cachep
;
184 static struct inode
*ovl_alloc_inode(struct super_block
*sb
)
186 struct ovl_inode
*oi
= kmem_cache_alloc(ovl_inode_cachep
, GFP_KERNEL
);
195 oi
->__upperdentry
= NULL
;
197 mutex_init(&oi
->lock
);
199 return &oi
->vfs_inode
;
202 static void ovl_i_callback(struct rcu_head
*head
)
204 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
206 kmem_cache_free(ovl_inode_cachep
, OVL_I(inode
));
209 static void ovl_destroy_inode(struct inode
*inode
)
211 struct ovl_inode
*oi
= OVL_I(inode
);
213 dput(oi
->__upperdentry
);
216 ovl_dir_cache_free(inode
);
217 mutex_destroy(&oi
->lock
);
219 call_rcu(&inode
->i_rcu
, ovl_i_callback
);
222 static void ovl_free_fs(struct ovl_fs
*ofs
)
228 if (ofs
->workdir_locked
)
229 ovl_inuse_unlock(ofs
->workbasedir
);
230 dput(ofs
->workbasedir
);
231 if (ofs
->upperdir_locked
)
232 ovl_inuse_unlock(ofs
->upper_mnt
->mnt_root
);
233 mntput(ofs
->upper_mnt
);
234 for (i
= 0; i
< ofs
->numlower
; i
++) {
235 mntput(ofs
->lower_layers
[i
].mnt
);
236 free_anon_bdev(ofs
->lower_layers
[i
].pseudo_dev
);
238 kfree(ofs
->lower_layers
);
240 kfree(ofs
->config
.lowerdir
);
241 kfree(ofs
->config
.upperdir
);
242 kfree(ofs
->config
.workdir
);
243 kfree(ofs
->config
.redirect_mode
);
244 if (ofs
->creator_cred
)
245 put_cred(ofs
->creator_cred
);
249 static void ovl_put_super(struct super_block
*sb
)
251 struct ovl_fs
*ofs
= sb
->s_fs_info
;
256 /* Sync real dirty inodes in upper filesystem (if it exists) */
257 static int ovl_sync_fs(struct super_block
*sb
, int wait
)
259 struct ovl_fs
*ofs
= sb
->s_fs_info
;
260 struct super_block
*upper_sb
;
267 * If this is a sync(2) call or an emergency sync, all the super blocks
268 * will be iterated, including upper_sb, so no need to do anything.
270 * If this is a syncfs(2) call, then we do need to call
271 * sync_filesystem() on upper_sb, but enough if we do it when being
272 * called with wait == 1.
277 upper_sb
= ofs
->upper_mnt
->mnt_sb
;
279 down_read(&upper_sb
->s_umount
);
280 ret
= sync_filesystem(upper_sb
);
281 up_read(&upper_sb
->s_umount
);
288 * @sb: The overlayfs super block
289 * @buf: The struct kstatfs to fill in with stats
291 * Get the filesystem statistics. As writes always target the upper layer
292 * filesystem pass the statfs to the upper filesystem (if it exists)
294 static int ovl_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
296 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
297 struct dentry
*root_dentry
= dentry
->d_sb
->s_root
;
301 ovl_path_real(root_dentry
, &path
);
303 err
= vfs_statfs(&path
, buf
);
305 buf
->f_namelen
= ofs
->namelen
;
306 buf
->f_type
= OVERLAYFS_SUPER_MAGIC
;
312 /* Will this overlay be forced to mount/remount ro? */
313 static bool ovl_force_readonly(struct ovl_fs
*ofs
)
315 return (!ofs
->upper_mnt
|| !ofs
->workdir
);
318 static const char *ovl_redirect_mode_def(void)
320 return ovl_redirect_dir_def
? "on" : "off";
326 * Prints the mount options for a given superblock.
327 * Returns zero; does not fail.
329 static int ovl_show_options(struct seq_file
*m
, struct dentry
*dentry
)
331 struct super_block
*sb
= dentry
->d_sb
;
332 struct ovl_fs
*ofs
= sb
->s_fs_info
;
334 seq_show_option(m
, "lowerdir", ofs
->config
.lowerdir
);
335 if (ofs
->config
.upperdir
) {
336 seq_show_option(m
, "upperdir", ofs
->config
.upperdir
);
337 seq_show_option(m
, "workdir", ofs
->config
.workdir
);
339 if (ofs
->config
.default_permissions
)
340 seq_puts(m
, ",default_permissions");
341 if (strcmp(ofs
->config
.redirect_mode
, ovl_redirect_mode_def()) != 0)
342 seq_printf(m
, ",redirect_dir=%s", ofs
->config
.redirect_mode
);
343 if (ofs
->config
.index
!= ovl_index_def
)
344 seq_printf(m
, ",index=%s", ofs
->config
.index
? "on" : "off");
348 static int ovl_remount(struct super_block
*sb
, int *flags
, char *data
)
350 struct ovl_fs
*ofs
= sb
->s_fs_info
;
352 if (!(*flags
& SB_RDONLY
) && ovl_force_readonly(ofs
))
358 static const struct super_operations ovl_super_operations
= {
359 .alloc_inode
= ovl_alloc_inode
,
360 .destroy_inode
= ovl_destroy_inode
,
361 .drop_inode
= generic_delete_inode
,
362 .put_super
= ovl_put_super
,
363 .sync_fs
= ovl_sync_fs
,
364 .statfs
= ovl_statfs
,
365 .show_options
= ovl_show_options
,
366 .remount_fs
= ovl_remount
,
373 OPT_DEFAULT_PERMISSIONS
,
380 static const match_table_t ovl_tokens
= {
381 {OPT_LOWERDIR
, "lowerdir=%s"},
382 {OPT_UPPERDIR
, "upperdir=%s"},
383 {OPT_WORKDIR
, "workdir=%s"},
384 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
385 {OPT_REDIRECT_DIR
, "redirect_dir=%s"},
386 {OPT_INDEX_ON
, "index=on"},
387 {OPT_INDEX_OFF
, "index=off"},
391 static char *ovl_next_opt(char **s
)
399 for (p
= sbegin
; *p
; p
++) {
404 } else if (*p
== ',') {
414 static int ovl_parse_redirect_mode(struct ovl_config
*config
, const char *mode
)
416 if (strcmp(mode
, "on") == 0) {
417 config
->redirect_dir
= true;
419 * Does not make sense to have redirect creation without
420 * redirect following.
422 config
->redirect_follow
= true;
423 } else if (strcmp(mode
, "follow") == 0) {
424 config
->redirect_follow
= true;
425 } else if (strcmp(mode
, "off") == 0) {
426 if (ovl_redirect_always_follow
)
427 config
->redirect_follow
= true;
428 } else if (strcmp(mode
, "nofollow") != 0) {
429 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
437 static int ovl_parse_opt(char *opt
, struct ovl_config
*config
)
441 config
->redirect_mode
= kstrdup(ovl_redirect_mode_def(), GFP_KERNEL
);
442 if (!config
->redirect_mode
)
445 while ((p
= ovl_next_opt(&opt
)) != NULL
) {
447 substring_t args
[MAX_OPT_ARGS
];
452 token
= match_token(p
, ovl_tokens
, args
);
455 kfree(config
->upperdir
);
456 config
->upperdir
= match_strdup(&args
[0]);
457 if (!config
->upperdir
)
462 kfree(config
->lowerdir
);
463 config
->lowerdir
= match_strdup(&args
[0]);
464 if (!config
->lowerdir
)
469 kfree(config
->workdir
);
470 config
->workdir
= match_strdup(&args
[0]);
471 if (!config
->workdir
)
475 case OPT_DEFAULT_PERMISSIONS
:
476 config
->default_permissions
= true;
479 case OPT_REDIRECT_DIR
:
480 kfree(config
->redirect_mode
);
481 config
->redirect_mode
= match_strdup(&args
[0]);
482 if (!config
->redirect_mode
)
487 config
->index
= true;
491 config
->index
= false;
495 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p
);
500 /* Workdir is useless in non-upper mount */
501 if (!config
->upperdir
&& config
->workdir
) {
502 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
504 kfree(config
->workdir
);
505 config
->workdir
= NULL
;
508 return ovl_parse_redirect_mode(config
, config
->redirect_mode
);
511 #define OVL_WORKDIR_NAME "work"
512 #define OVL_INDEXDIR_NAME "index"
514 static struct dentry
*ovl_workdir_create(struct ovl_fs
*ofs
,
515 const char *name
, bool persist
)
517 struct inode
*dir
= ofs
->workbasedir
->d_inode
;
518 struct vfsmount
*mnt
= ofs
->upper_mnt
;
521 bool retried
= false;
524 inode_lock_nested(dir
, I_MUTEX_PARENT
);
528 work
= lookup_one_len(name
, ofs
->workbasedir
, strlen(name
));
531 struct iattr attr
= {
532 .ia_valid
= ATTR_MODE
,
533 .ia_mode
= S_IFDIR
| 0,
545 ovl_workdir_cleanup(dir
, mnt
, work
, 0);
550 err
= ovl_create_real(dir
, work
,
551 &(struct cattr
){.mode
= S_IFDIR
| 0},
557 * Try to remove POSIX ACL xattrs from workdir. We are good if:
559 * a) success (there was a POSIX ACL xattr and was removed)
560 * b) -ENODATA (there was no POSIX ACL xattr)
561 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
563 * There are various other error values that could effectively
564 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
565 * if the xattr name is too long), but the set of filesystems
566 * allowed as upper are limited to "normal" ones, where checking
567 * for the above two errors is sufficient.
569 err
= vfs_removexattr(work
, XATTR_NAME_POSIX_ACL_DEFAULT
);
570 if (err
&& err
!= -ENODATA
&& err
!= -EOPNOTSUPP
)
573 err
= vfs_removexattr(work
, XATTR_NAME_POSIX_ACL_ACCESS
);
574 if (err
&& err
!= -ENODATA
&& err
!= -EOPNOTSUPP
)
577 /* Clear any inherited mode bits */
578 inode_lock(work
->d_inode
);
579 err
= notify_change(work
, &attr
, NULL
);
580 inode_unlock(work
->d_inode
);
596 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
597 ofs
->config
.workdir
, name
, -err
);
602 static void ovl_unescape(char *s
)
615 static int ovl_mount_dir_noesc(const char *name
, struct path
*path
)
620 pr_err("overlayfs: empty lowerdir\n");
623 err
= kern_path(name
, LOOKUP_FOLLOW
, path
);
625 pr_err("overlayfs: failed to resolve '%s': %i\n", name
, err
);
629 if (ovl_dentry_weird(path
->dentry
)) {
630 pr_err("overlayfs: filesystem on '%s' not supported\n", name
);
633 if (!d_is_dir(path
->dentry
)) {
634 pr_err("overlayfs: '%s' not a directory\n", name
);
645 static int ovl_mount_dir(const char *name
, struct path
*path
)
648 char *tmp
= kstrdup(name
, GFP_KERNEL
);
652 err
= ovl_mount_dir_noesc(tmp
, path
);
655 if (ovl_dentry_remote(path
->dentry
)) {
656 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
666 static int ovl_check_namelen(struct path
*path
, struct ovl_fs
*ofs
,
669 struct kstatfs statfs
;
670 int err
= vfs_statfs(path
, &statfs
);
673 pr_err("overlayfs: statfs failed on '%s'\n", name
);
675 ofs
->namelen
= max(ofs
->namelen
, statfs
.f_namelen
);
680 static int ovl_lower_dir(const char *name
, struct path
*path
,
681 struct ovl_fs
*ofs
, int *stack_depth
, bool *remote
)
685 err
= ovl_mount_dir_noesc(name
, path
);
689 err
= ovl_check_namelen(path
, ofs
, name
);
693 *stack_depth
= max(*stack_depth
, path
->mnt
->mnt_sb
->s_stack_depth
);
695 if (ovl_dentry_remote(path
->dentry
))
699 * The inodes index feature needs to encode and decode file
700 * handles, so it requires that all layers support them.
702 if (ofs
->config
.index
&& ofs
->config
.upperdir
&&
703 !ovl_can_decode_fh(path
->dentry
->d_sb
)) {
704 ofs
->config
.index
= false;
705 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name
);
716 /* Workdir should not be subdir of upperdir and vice versa */
717 static bool ovl_workdir_ok(struct dentry
*workdir
, struct dentry
*upperdir
)
721 if (workdir
!= upperdir
) {
722 ok
= (lock_rename(workdir
, upperdir
) == NULL
);
723 unlock_rename(workdir
, upperdir
);
728 static unsigned int ovl_split_lowerdirs(char *str
)
730 unsigned int ctr
= 1;
733 for (s
= d
= str
;; s
++, d
++) {
736 } else if (*s
== ':') {
748 static int __maybe_unused
749 ovl_posix_acl_xattr_get(const struct xattr_handler
*handler
,
750 struct dentry
*dentry
, struct inode
*inode
,
751 const char *name
, void *buffer
, size_t size
)
753 return ovl_xattr_get(dentry
, inode
, handler
->name
, buffer
, size
);
756 static int __maybe_unused
757 ovl_posix_acl_xattr_set(const struct xattr_handler
*handler
,
758 struct dentry
*dentry
, struct inode
*inode
,
759 const char *name
, const void *value
,
760 size_t size
, int flags
)
762 struct dentry
*workdir
= ovl_workdir(dentry
);
763 struct inode
*realinode
= ovl_inode_real(inode
);
764 struct posix_acl
*acl
= NULL
;
767 /* Check that everything is OK before copy-up */
769 acl
= posix_acl_from_xattr(&init_user_ns
, value
, size
);
774 if (!IS_POSIXACL(d_inode(workdir
)))
775 goto out_acl_release
;
776 if (!realinode
->i_op
->set_acl
)
777 goto out_acl_release
;
778 if (handler
->flags
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
)) {
779 err
= acl
? -EACCES
: 0;
780 goto out_acl_release
;
783 if (!inode_owner_or_capable(inode
))
784 goto out_acl_release
;
786 posix_acl_release(acl
);
789 * Check if sgid bit needs to be cleared (actual setacl operation will
790 * be done with mounter's capabilities and so that won't do it for us).
792 if (unlikely(inode
->i_mode
& S_ISGID
) &&
793 handler
->flags
== ACL_TYPE_ACCESS
&&
794 !in_group_p(inode
->i_gid
) &&
795 !capable_wrt_inode_uidgid(inode
, CAP_FSETID
)) {
796 struct iattr iattr
= { .ia_valid
= ATTR_KILL_SGID
};
798 err
= ovl_setattr(dentry
, &iattr
);
803 err
= ovl_xattr_set(dentry
, inode
, handler
->name
, value
, size
, flags
);
805 ovl_copyattr(ovl_inode_real(inode
), inode
);
810 posix_acl_release(acl
);
814 static int ovl_own_xattr_get(const struct xattr_handler
*handler
,
815 struct dentry
*dentry
, struct inode
*inode
,
816 const char *name
, void *buffer
, size_t size
)
821 static int ovl_own_xattr_set(const struct xattr_handler
*handler
,
822 struct dentry
*dentry
, struct inode
*inode
,
823 const char *name
, const void *value
,
824 size_t size
, int flags
)
829 static int ovl_other_xattr_get(const struct xattr_handler
*handler
,
830 struct dentry
*dentry
, struct inode
*inode
,
831 const char *name
, void *buffer
, size_t size
)
833 return ovl_xattr_get(dentry
, inode
, name
, buffer
, size
);
836 static int ovl_other_xattr_set(const struct xattr_handler
*handler
,
837 struct dentry
*dentry
, struct inode
*inode
,
838 const char *name
, const void *value
,
839 size_t size
, int flags
)
841 return ovl_xattr_set(dentry
, inode
, name
, value
, size
, flags
);
844 static const struct xattr_handler __maybe_unused
845 ovl_posix_acl_access_xattr_handler
= {
846 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
847 .flags
= ACL_TYPE_ACCESS
,
848 .get
= ovl_posix_acl_xattr_get
,
849 .set
= ovl_posix_acl_xattr_set
,
852 static const struct xattr_handler __maybe_unused
853 ovl_posix_acl_default_xattr_handler
= {
854 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
855 .flags
= ACL_TYPE_DEFAULT
,
856 .get
= ovl_posix_acl_xattr_get
,
857 .set
= ovl_posix_acl_xattr_set
,
860 static const struct xattr_handler ovl_own_xattr_handler
= {
861 .prefix
= OVL_XATTR_PREFIX
,
862 .get
= ovl_own_xattr_get
,
863 .set
= ovl_own_xattr_set
,
866 static const struct xattr_handler ovl_other_xattr_handler
= {
867 .prefix
= "", /* catch all */
868 .get
= ovl_other_xattr_get
,
869 .set
= ovl_other_xattr_set
,
872 static const struct xattr_handler
*ovl_xattr_handlers
[] = {
873 #ifdef CONFIG_FS_POSIX_ACL
874 &ovl_posix_acl_access_xattr_handler
,
875 &ovl_posix_acl_default_xattr_handler
,
877 &ovl_own_xattr_handler
,
878 &ovl_other_xattr_handler
,
882 static int ovl_get_upper(struct ovl_fs
*ofs
, struct path
*upperpath
)
884 struct vfsmount
*upper_mnt
;
887 err
= ovl_mount_dir(ofs
->config
.upperdir
, upperpath
);
891 /* Upper fs should not be r/o */
892 if (sb_rdonly(upperpath
->mnt
->mnt_sb
)) {
893 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
898 err
= ovl_check_namelen(upperpath
, ofs
, ofs
->config
.upperdir
);
903 if (ovl_inuse_trylock(upperpath
->dentry
)) {
904 ofs
->upperdir_locked
= true;
905 } else if (ofs
->config
.index
) {
906 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
909 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
912 upper_mnt
= clone_private_mount(upperpath
);
913 err
= PTR_ERR(upper_mnt
);
914 if (IS_ERR(upper_mnt
)) {
915 pr_err("overlayfs: failed to clone upperpath\n");
919 /* Don't inherit atime flags */
920 upper_mnt
->mnt_flags
&= ~(MNT_NOATIME
| MNT_NODIRATIME
| MNT_RELATIME
);
921 ofs
->upper_mnt
= upper_mnt
;
927 static int ovl_make_workdir(struct ovl_fs
*ofs
, struct path
*workpath
)
929 struct vfsmount
*mnt
= ofs
->upper_mnt
;
933 err
= mnt_want_write(mnt
);
937 ofs
->workdir
= ovl_workdir_create(ofs
, OVL_WORKDIR_NAME
, false);
942 * Upper should support d_type, else whiteouts are visible. Given
943 * workdir and upper are on same fs, we can do iterate_dir() on
944 * workdir. This check requires successful creation of workdir in
947 err
= ovl_check_d_type_supported(workpath
);
952 * We allowed this configuration and don't want to break users over
953 * kernel upgrade. So warn instead of erroring out.
956 pr_warn("overlayfs: upper fs needs to support d_type.\n");
958 /* Check if upper/work fs supports O_TMPFILE */
959 temp
= ovl_do_tmpfile(ofs
->workdir
, S_IFREG
| 0);
960 ofs
->tmpfile
= !IS_ERR(temp
);
964 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
967 * Check if upper/work fs supports trusted.overlay.* xattr
969 err
= ovl_do_setxattr(ofs
->workdir
, OVL_XATTR_OPAQUE
, "0", 1, 0);
972 ofs
->config
.index
= false;
973 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off.\n");
976 vfs_removexattr(ofs
->workdir
, OVL_XATTR_OPAQUE
);
979 /* Check if upper/work fs supports file handles */
980 if (ofs
->config
.index
&&
981 !ovl_can_decode_fh(ofs
->workdir
->d_sb
)) {
982 ofs
->config
.index
= false;
983 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
991 static int ovl_get_workdir(struct ovl_fs
*ofs
, struct path
*upperpath
)
994 struct path workpath
= { };
996 err
= ovl_mount_dir(ofs
->config
.workdir
, &workpath
);
1001 if (upperpath
->mnt
!= workpath
.mnt
) {
1002 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1005 if (!ovl_workdir_ok(workpath
.dentry
, upperpath
->dentry
)) {
1006 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1011 if (ovl_inuse_trylock(workpath
.dentry
)) {
1012 ofs
->workdir_locked
= true;
1013 } else if (ofs
->config
.index
) {
1014 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1017 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1020 ofs
->workbasedir
= dget(workpath
.dentry
);
1021 err
= ovl_make_workdir(ofs
, &workpath
);
1027 path_put(&workpath
);
1032 static int ovl_get_indexdir(struct ovl_fs
*ofs
, struct ovl_entry
*oe
,
1033 struct path
*upperpath
)
1035 struct vfsmount
*mnt
= ofs
->upper_mnt
;
1038 err
= mnt_want_write(mnt
);
1042 /* Verify lower root is upper root origin */
1043 err
= ovl_verify_origin(upperpath
->dentry
, oe
->lowerstack
[0].dentry
,
1046 pr_err("overlayfs: failed to verify upper root origin\n");
1050 ofs
->indexdir
= ovl_workdir_create(ofs
, OVL_INDEXDIR_NAME
, true);
1051 if (ofs
->indexdir
) {
1052 /* Verify upper root is index dir origin */
1053 err
= ovl_verify_origin(ofs
->indexdir
, upperpath
->dentry
,
1056 pr_err("overlayfs: failed to verify index dir origin\n");
1058 /* Cleanup bad/stale/orphan index entries */
1060 err
= ovl_indexdir_cleanup(ofs
->indexdir
,
1065 if (err
|| !ofs
->indexdir
)
1066 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1069 mnt_drop_write(mnt
);
1073 static int ovl_get_lower_layers(struct ovl_fs
*ofs
, struct path
*stack
,
1074 unsigned int numlower
)
1080 ofs
->lower_layers
= kcalloc(numlower
, sizeof(struct ovl_layer
),
1082 if (ofs
->lower_layers
== NULL
)
1084 for (i
= 0; i
< numlower
; i
++) {
1085 struct vfsmount
*mnt
;
1088 err
= get_anon_bdev(&dev
);
1090 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1094 mnt
= clone_private_mount(&stack
[i
]);
1097 pr_err("overlayfs: failed to clone lowerpath\n");
1098 free_anon_bdev(dev
);
1102 * Make lower layers R/O. That way fchmod/fchown on lower file
1103 * will fail instead of modifying lower fs.
1105 mnt
->mnt_flags
|= MNT_READONLY
| MNT_NOATIME
;
1107 ofs
->lower_layers
[ofs
->numlower
].mnt
= mnt
;
1108 ofs
->lower_layers
[ofs
->numlower
].pseudo_dev
= dev
;
1111 /* Check if all lower layers are on same sb */
1113 ofs
->same_sb
= mnt
->mnt_sb
;
1114 else if (ofs
->same_sb
!= mnt
->mnt_sb
)
1115 ofs
->same_sb
= NULL
;
1122 static struct ovl_entry
*ovl_get_lowerstack(struct super_block
*sb
,
1126 char *lowertmp
, *lower
;
1127 struct path
*stack
= NULL
;
1128 unsigned int stacklen
, numlower
= 0, i
;
1129 bool remote
= false;
1130 struct ovl_entry
*oe
;
1133 lowertmp
= kstrdup(ofs
->config
.lowerdir
, GFP_KERNEL
);
1138 stacklen
= ovl_split_lowerdirs(lowertmp
);
1139 if (stacklen
> OVL_MAX_STACK
) {
1140 pr_err("overlayfs: too many lower directories, limit is %d\n",
1143 } else if (!ofs
->config
.upperdir
&& stacklen
== 1) {
1144 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1149 stack
= kcalloc(stacklen
, sizeof(struct path
), GFP_KERNEL
);
1155 for (numlower
= 0; numlower
< stacklen
; numlower
++) {
1156 err
= ovl_lower_dir(lower
, &stack
[numlower
], ofs
,
1157 &sb
->s_stack_depth
, &remote
);
1161 lower
= strchr(lower
, '\0') + 1;
1165 sb
->s_stack_depth
++;
1166 if (sb
->s_stack_depth
> FILESYSTEM_MAX_STACK_DEPTH
) {
1167 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1171 err
= ovl_get_lower_layers(ofs
, stack
, numlower
);
1176 oe
= ovl_alloc_entry(numlower
);
1180 for (i
= 0; i
< numlower
; i
++) {
1181 oe
->lowerstack
[i
].dentry
= dget(stack
[i
].dentry
);
1182 oe
->lowerstack
[i
].layer
= &ofs
->lower_layers
[i
];
1186 sb
->s_d_op
= &ovl_reval_dentry_operations
;
1188 sb
->s_d_op
= &ovl_dentry_operations
;
1191 for (i
= 0; i
< numlower
; i
++)
1192 path_put(&stack
[i
]);
1203 static int ovl_fill_super(struct super_block
*sb
, void *data
, int silent
)
1205 struct path upperpath
= { };
1206 struct dentry
*root_dentry
;
1207 struct ovl_entry
*oe
;
1213 ofs
= kzalloc(sizeof(struct ovl_fs
), GFP_KERNEL
);
1217 ofs
->creator_cred
= cred
= prepare_creds();
1221 ofs
->config
.index
= ovl_index_def
;
1222 err
= ovl_parse_opt((char *) data
, &ofs
->config
);
1227 if (!ofs
->config
.lowerdir
) {
1229 pr_err("overlayfs: missing 'lowerdir'\n");
1233 sb
->s_stack_depth
= 0;
1234 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
1235 if (ofs
->config
.upperdir
) {
1236 if (!ofs
->config
.workdir
) {
1237 pr_err("overlayfs: missing 'workdir'\n");
1241 err
= ovl_get_upper(ofs
, &upperpath
);
1245 err
= ovl_get_workdir(ofs
, &upperpath
);
1250 sb
->s_flags
|= SB_RDONLY
;
1252 sb
->s_stack_depth
= ofs
->upper_mnt
->mnt_sb
->s_stack_depth
;
1253 sb
->s_time_gran
= ofs
->upper_mnt
->mnt_sb
->s_time_gran
;
1256 oe
= ovl_get_lowerstack(sb
, ofs
);
1261 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1262 if (!ofs
->upper_mnt
)
1263 sb
->s_flags
|= SB_RDONLY
;
1264 else if (ofs
->upper_mnt
->mnt_sb
!= ofs
->same_sb
)
1265 ofs
->same_sb
= NULL
;
1267 if (!(ovl_force_readonly(ofs
)) && ofs
->config
.index
) {
1268 err
= ovl_get_indexdir(ofs
, oe
, &upperpath
);
1272 /* Force r/o mount with no index dir */
1273 if (!ofs
->indexdir
) {
1275 ofs
->workdir
= NULL
;
1276 sb
->s_flags
|= SB_RDONLY
;
1281 /* Show index=off in /proc/mounts for forced r/o mount */
1283 ofs
->config
.index
= false;
1285 /* Never override disk quota limits or use reserved space */
1286 cap_lower(cred
->cap_effective
, CAP_SYS_RESOURCE
);
1288 sb
->s_magic
= OVERLAYFS_SUPER_MAGIC
;
1289 sb
->s_op
= &ovl_super_operations
;
1290 sb
->s_xattr
= ovl_xattr_handlers
;
1291 sb
->s_fs_info
= ofs
;
1292 sb
->s_flags
|= SB_POSIXACL
| SB_NOREMOTELOCK
;
1295 root_dentry
= d_make_root(ovl_new_inode(sb
, S_IFDIR
, 0));
1299 mntput(upperpath
.mnt
);
1300 if (upperpath
.dentry
) {
1301 oe
->has_upper
= true;
1302 if (ovl_is_impuredir(upperpath
.dentry
))
1303 ovl_set_flag(OVL_IMPURE
, d_inode(root_dentry
));
1306 root_dentry
->d_fsdata
= oe
;
1308 /* Root is always merge -> can have whiteouts */
1309 ovl_set_flag(OVL_WHITEOUTS
, d_inode(root_dentry
));
1310 ovl_inode_init(d_inode(root_dentry
), upperpath
.dentry
,
1311 ovl_dentry_lower(root_dentry
));
1313 sb
->s_root
= root_dentry
;
1318 ovl_entry_stack_free(oe
);
1321 path_put(&upperpath
);
1327 static struct dentry
*ovl_mount(struct file_system_type
*fs_type
, int flags
,
1328 const char *dev_name
, void *raw_data
)
1330 return mount_nodev(fs_type
, flags
, raw_data
, ovl_fill_super
);
1333 static struct file_system_type ovl_fs_type
= {
1334 .owner
= THIS_MODULE
,
1337 .kill_sb
= kill_anon_super
,
1339 MODULE_ALIAS_FS("overlay");
1341 static void ovl_inode_init_once(void *foo
)
1343 struct ovl_inode
*oi
= foo
;
1345 inode_init_once(&oi
->vfs_inode
);
1348 static int __init
ovl_init(void)
1352 ovl_inode_cachep
= kmem_cache_create("ovl_inode",
1353 sizeof(struct ovl_inode
), 0,
1354 (SLAB_RECLAIM_ACCOUNT
|
1355 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
),
1356 ovl_inode_init_once
);
1357 if (ovl_inode_cachep
== NULL
)
1360 err
= register_filesystem(&ovl_fs_type
);
1362 kmem_cache_destroy(ovl_inode_cachep
);
1367 static void __exit
ovl_exit(void)
1369 unregister_filesystem(&ovl_fs_type
);
1372 * Make sure all delayed rcu free inodes are flushed before we
1376 kmem_cache_destroy(ovl_inode_cachep
);
1380 module_init(ovl_init
);
1381 module_exit(ovl_exit
);