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"
21 #include "ovl_entry.h"
24 MODULE_DESCRIPTION("Overlay filesystem");
25 MODULE_LICENSE("GPL");
30 #define OVL_MAX_STACK 500
32 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34 MODULE_PARM_DESC(ovl_redirect_dir_def,
35 "Default to on or off for the redirect_dir feature");
37 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
38 module_param_named(index, ovl_index_def, bool, 0644);
39 MODULE_PARM_DESC(ovl_index_def,
40 "Default to on or off for the inodes index feature");
42 static void ovl_dentry_release(struct dentry *dentry)
44 struct ovl_entry *oe = dentry->d_fsdata;
49 for (i = 0; i < oe->numlower; i++)
50 dput(oe->lowerstack[i].dentry);
55 static int ovl_check_append_only(struct inode *inode, int flag)
58 * This test was moot in vfs may_open() because overlay inode does
59 * not have the S_APPEND flag, so re-check on real upper inode
61 if (IS_APPEND(inode)) {
62 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
71 static struct dentry *ovl_d_real(struct dentry *dentry,
72 const struct inode *inode,
73 unsigned int open_flags, unsigned int flags)
78 if (flags & D_REAL_UPPER)
79 return ovl_dentry_upper(dentry);
81 if (!d_is_reg(dentry)) {
82 if (!inode || inode == d_inode(dentry))
88 err = ovl_open_maybe_copy_up(dentry, open_flags);
93 real = ovl_dentry_upper(dentry);
94 if (real && (!inode || inode == d_inode(real))) {
96 err = ovl_check_append_only(d_inode(real), open_flags);
103 real = ovl_dentry_lower(dentry);
107 /* Handle recursion */
108 real = d_real(real, inode, open_flags, 0);
110 if (!inode || inode == d_inode(real))
113 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
114 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
118 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
120 struct ovl_entry *oe = dentry->d_fsdata;
124 for (i = 0; i < oe->numlower; i++) {
125 struct dentry *d = oe->lowerstack[i].dentry;
127 if (d->d_flags & DCACHE_OP_REVALIDATE) {
128 ret = d->d_op->d_revalidate(d, flags);
132 if (!(flags & LOOKUP_RCU))
141 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
143 struct ovl_entry *oe = dentry->d_fsdata;
147 for (i = 0; i < oe->numlower; i++) {
148 struct dentry *d = oe->lowerstack[i].dentry;
150 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
151 ret = d->d_op->d_weak_revalidate(d, flags);
159 static const struct dentry_operations ovl_dentry_operations = {
160 .d_release = ovl_dentry_release,
161 .d_real = ovl_d_real,
164 static const struct dentry_operations ovl_reval_dentry_operations = {
165 .d_release = ovl_dentry_release,
166 .d_real = ovl_d_real,
167 .d_revalidate = ovl_dentry_revalidate,
168 .d_weak_revalidate = ovl_dentry_weak_revalidate,
171 static struct kmem_cache *ovl_inode_cachep;
173 static struct inode *ovl_alloc_inode(struct super_block *sb)
175 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
181 oi->__upperdentry = NULL;
183 mutex_init(&oi->lock);
185 return &oi->vfs_inode;
188 static void ovl_i_callback(struct rcu_head *head)
190 struct inode *inode = container_of(head, struct inode, i_rcu);
192 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
195 static void ovl_destroy_inode(struct inode *inode)
197 struct ovl_inode *oi = OVL_I(inode);
199 dput(oi->__upperdentry);
201 ovl_dir_cache_free(inode);
202 mutex_destroy(&oi->lock);
204 call_rcu(&inode->i_rcu, ovl_i_callback);
207 static void ovl_put_super(struct super_block *sb)
209 struct ovl_fs *ufs = sb->s_fs_info;
214 ovl_inuse_unlock(ufs->workbasedir);
215 dput(ufs->workbasedir);
217 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
218 mntput(ufs->upper_mnt);
219 for (i = 0; i < ufs->numlower; i++)
220 mntput(ufs->lower_mnt[i]);
221 kfree(ufs->lower_mnt);
223 kfree(ufs->config.lowerdir);
224 kfree(ufs->config.upperdir);
225 kfree(ufs->config.workdir);
226 put_cred(ufs->creator_cred);
230 static int ovl_sync_fs(struct super_block *sb, int wait)
232 struct ovl_fs *ufs = sb->s_fs_info;
233 struct super_block *upper_sb;
238 upper_sb = ufs->upper_mnt->mnt_sb;
239 if (!upper_sb->s_op->sync_fs)
242 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
243 down_read(&upper_sb->s_umount);
244 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
245 up_read(&upper_sb->s_umount);
251 * @sb: The overlayfs super block
252 * @buf: The struct kstatfs to fill in with stats
254 * Get the filesystem statistics. As writes always target the upper layer
255 * filesystem pass the statfs to the upper filesystem (if it exists)
257 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
259 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
260 struct dentry *root_dentry = dentry->d_sb->s_root;
264 ovl_path_real(root_dentry, &path);
266 err = vfs_statfs(&path, buf);
268 buf->f_namelen = ofs->namelen;
269 buf->f_type = OVERLAYFS_SUPER_MAGIC;
275 /* Will this overlay be forced to mount/remount ro? */
276 static bool ovl_force_readonly(struct ovl_fs *ufs)
278 return (!ufs->upper_mnt || !ufs->workdir);
284 * Prints the mount options for a given superblock.
285 * Returns zero; does not fail.
287 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
289 struct super_block *sb = dentry->d_sb;
290 struct ovl_fs *ufs = sb->s_fs_info;
292 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
293 if (ufs->config.upperdir) {
294 seq_show_option(m, "upperdir", ufs->config.upperdir);
295 seq_show_option(m, "workdir", ufs->config.workdir);
297 if (ufs->config.default_permissions)
298 seq_puts(m, ",default_permissions");
299 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
300 seq_printf(m, ",redirect_dir=%s",
301 ufs->config.redirect_dir ? "on" : "off");
302 if (ufs->config.index != ovl_index_def)
303 seq_printf(m, ",index=%s",
304 ufs->config.index ? "on" : "off");
308 static int ovl_remount(struct super_block *sb, int *flags, char *data)
310 struct ovl_fs *ufs = sb->s_fs_info;
312 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
318 static const struct super_operations ovl_super_operations = {
319 .alloc_inode = ovl_alloc_inode,
320 .destroy_inode = ovl_destroy_inode,
321 .drop_inode = generic_delete_inode,
322 .put_super = ovl_put_super,
323 .sync_fs = ovl_sync_fs,
324 .statfs = ovl_statfs,
325 .show_options = ovl_show_options,
326 .remount_fs = ovl_remount,
333 OPT_DEFAULT_PERMISSIONS,
335 OPT_REDIRECT_DIR_OFF,
341 static const match_table_t ovl_tokens = {
342 {OPT_LOWERDIR, "lowerdir=%s"},
343 {OPT_UPPERDIR, "upperdir=%s"},
344 {OPT_WORKDIR, "workdir=%s"},
345 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
346 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
347 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
348 {OPT_INDEX_ON, "index=on"},
349 {OPT_INDEX_OFF, "index=off"},
353 static char *ovl_next_opt(char **s)
361 for (p = sbegin; *p; p++) {
366 } else if (*p == ',') {
376 static int ovl_parse_opt(char *opt, struct ovl_config *config)
380 while ((p = ovl_next_opt(&opt)) != NULL) {
382 substring_t args[MAX_OPT_ARGS];
387 token = match_token(p, ovl_tokens, args);
390 kfree(config->upperdir);
391 config->upperdir = match_strdup(&args[0]);
392 if (!config->upperdir)
397 kfree(config->lowerdir);
398 config->lowerdir = match_strdup(&args[0]);
399 if (!config->lowerdir)
404 kfree(config->workdir);
405 config->workdir = match_strdup(&args[0]);
406 if (!config->workdir)
410 case OPT_DEFAULT_PERMISSIONS:
411 config->default_permissions = true;
414 case OPT_REDIRECT_DIR_ON:
415 config->redirect_dir = true;
418 case OPT_REDIRECT_DIR_OFF:
419 config->redirect_dir = false;
423 config->index = true;
427 config->index = false;
431 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
436 /* Workdir is useless in non-upper mount */
437 if (!config->upperdir && config->workdir) {
438 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
440 kfree(config->workdir);
441 config->workdir = NULL;
447 #define OVL_WORKDIR_NAME "work"
448 #define OVL_INDEXDIR_NAME "index"
450 static struct dentry *ovl_workdir_create(struct super_block *sb,
452 struct dentry *dentry,
453 const char *name, bool persist)
455 struct inode *dir = dentry->d_inode;
456 struct vfsmount *mnt = ufs->upper_mnt;
459 bool retried = false;
462 err = mnt_want_write(mnt);
466 inode_lock_nested(dir, I_MUTEX_PARENT);
470 work = lookup_one_len(name, dentry, strlen(name));
473 struct iattr attr = {
474 .ia_valid = ATTR_MODE,
475 .ia_mode = S_IFDIR | 0,
487 ovl_workdir_cleanup(dir, mnt, work, 0);
492 err = ovl_create_real(dir, work,
493 &(struct cattr){.mode = S_IFDIR | 0},
499 * Try to remove POSIX ACL xattrs from workdir. We are good if:
501 * a) success (there was a POSIX ACL xattr and was removed)
502 * b) -ENODATA (there was no POSIX ACL xattr)
503 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
505 * There are various other error values that could effectively
506 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
507 * if the xattr name is too long), but the set of filesystems
508 * allowed as upper are limited to "normal" ones, where checking
509 * for the above two errors is sufficient.
511 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
512 if (err && err != -ENODATA && err != -EOPNOTSUPP)
515 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
516 if (err && err != -ENODATA && err != -EOPNOTSUPP)
519 /* Clear any inherited mode bits */
520 inode_lock(work->d_inode);
521 err = notify_change(work, &attr, NULL);
522 inode_unlock(work->d_inode);
539 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
540 ufs->config.workdir, name, -err);
541 sb->s_flags |= MS_RDONLY;
546 static void ovl_unescape(char *s)
559 static int ovl_mount_dir_noesc(const char *name, struct path *path)
564 pr_err("overlayfs: empty lowerdir\n");
567 err = kern_path(name, LOOKUP_FOLLOW, path);
569 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
573 if (ovl_dentry_weird(path->dentry)) {
574 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
577 if (!d_is_dir(path->dentry)) {
578 pr_err("overlayfs: '%s' not a directory\n", name);
589 static int ovl_mount_dir(const char *name, struct path *path)
592 char *tmp = kstrdup(name, GFP_KERNEL);
596 err = ovl_mount_dir_noesc(tmp, path);
599 if (ovl_dentry_remote(path->dentry)) {
600 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
610 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
613 struct kstatfs statfs;
614 int err = vfs_statfs(path, &statfs);
617 pr_err("overlayfs: statfs failed on '%s'\n", name);
619 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
624 static int ovl_lower_dir(const char *name, struct path *path,
625 struct ovl_fs *ofs, int *stack_depth, bool *remote)
629 err = ovl_mount_dir_noesc(name, path);
633 err = ovl_check_namelen(path, ofs, name);
637 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
639 if (ovl_dentry_remote(path->dentry))
643 * The inodes index feature needs to encode and decode file
644 * handles, so it requires that all layers support them.
646 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
647 ofs->config.index = false;
648 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
659 /* Workdir should not be subdir of upperdir and vice versa */
660 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
664 if (workdir != upperdir) {
665 ok = (lock_rename(workdir, upperdir) == NULL);
666 unlock_rename(workdir, upperdir);
671 static unsigned int ovl_split_lowerdirs(char *str)
673 unsigned int ctr = 1;
676 for (s = d = str;; s++, d++) {
679 } else if (*s == ':') {
691 static int __maybe_unused
692 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
693 struct dentry *dentry, struct inode *inode,
694 const char *name, void *buffer, size_t size)
696 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
699 static int __maybe_unused
700 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
701 struct dentry *dentry, struct inode *inode,
702 const char *name, const void *value,
703 size_t size, int flags)
705 struct dentry *workdir = ovl_workdir(dentry);
706 struct inode *realinode = ovl_inode_real(inode);
707 struct posix_acl *acl = NULL;
710 /* Check that everything is OK before copy-up */
712 acl = posix_acl_from_xattr(&init_user_ns, value, size);
717 if (!IS_POSIXACL(d_inode(workdir)))
718 goto out_acl_release;
719 if (!realinode->i_op->set_acl)
720 goto out_acl_release;
721 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
722 err = acl ? -EACCES : 0;
723 goto out_acl_release;
726 if (!inode_owner_or_capable(inode))
727 goto out_acl_release;
729 posix_acl_release(acl);
732 * Check if sgid bit needs to be cleared (actual setacl operation will
733 * be done with mounter's capabilities and so that won't do it for us).
735 if (unlikely(inode->i_mode & S_ISGID) &&
736 handler->flags == ACL_TYPE_ACCESS &&
737 !in_group_p(inode->i_gid) &&
738 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
739 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
741 err = ovl_setattr(dentry, &iattr);
746 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
748 ovl_copyattr(ovl_inode_real(inode), inode);
753 posix_acl_release(acl);
757 static int ovl_own_xattr_get(const struct xattr_handler *handler,
758 struct dentry *dentry, struct inode *inode,
759 const char *name, void *buffer, size_t size)
764 static int ovl_own_xattr_set(const struct xattr_handler *handler,
765 struct dentry *dentry, struct inode *inode,
766 const char *name, const void *value,
767 size_t size, int flags)
772 static int ovl_other_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, name, buffer, size);
779 static int ovl_other_xattr_set(const struct xattr_handler *handler,
780 struct dentry *dentry, struct inode *inode,
781 const char *name, const void *value,
782 size_t size, int flags)
784 return ovl_xattr_set(dentry, inode, name, value, size, flags);
787 static const struct xattr_handler __maybe_unused
788 ovl_posix_acl_access_xattr_handler = {
789 .name = XATTR_NAME_POSIX_ACL_ACCESS,
790 .flags = ACL_TYPE_ACCESS,
791 .get = ovl_posix_acl_xattr_get,
792 .set = ovl_posix_acl_xattr_set,
795 static const struct xattr_handler __maybe_unused
796 ovl_posix_acl_default_xattr_handler = {
797 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
798 .flags = ACL_TYPE_DEFAULT,
799 .get = ovl_posix_acl_xattr_get,
800 .set = ovl_posix_acl_xattr_set,
803 static const struct xattr_handler ovl_own_xattr_handler = {
804 .prefix = OVL_XATTR_PREFIX,
805 .get = ovl_own_xattr_get,
806 .set = ovl_own_xattr_set,
809 static const struct xattr_handler ovl_other_xattr_handler = {
810 .prefix = "", /* catch all */
811 .get = ovl_other_xattr_get,
812 .set = ovl_other_xattr_set,
815 static const struct xattr_handler *ovl_xattr_handlers[] = {
816 #ifdef CONFIG_FS_POSIX_ACL
817 &ovl_posix_acl_access_xattr_handler,
818 &ovl_posix_acl_default_xattr_handler,
820 &ovl_own_xattr_handler,
821 &ovl_other_xattr_handler,
825 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
827 struct path upperpath = { };
828 struct path workpath = { };
829 struct dentry *root_dentry;
830 struct ovl_entry *oe;
832 struct path *stack = NULL;
835 unsigned int numlower;
836 unsigned int stacklen = 0;
843 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
847 ufs->config.redirect_dir = ovl_redirect_dir_def;
848 ufs->config.index = ovl_index_def;
849 err = ovl_parse_opt((char *) data, &ufs->config);
851 goto out_free_config;
854 if (!ufs->config.lowerdir) {
856 pr_err("overlayfs: missing 'lowerdir'\n");
857 goto out_free_config;
860 sb->s_stack_depth = 0;
861 sb->s_maxbytes = MAX_LFS_FILESIZE;
862 if (ufs->config.upperdir) {
863 if (!ufs->config.workdir) {
864 pr_err("overlayfs: missing 'workdir'\n");
865 goto out_free_config;
868 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
870 goto out_free_config;
872 /* Upper fs should not be r/o */
873 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
874 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
876 goto out_put_upperpath;
879 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
881 goto out_put_upperpath;
884 if (!ovl_inuse_trylock(upperpath.dentry)) {
885 pr_err("overlayfs: upperdir is in-use by another mount\n");
886 goto out_put_upperpath;
889 err = ovl_mount_dir(ufs->config.workdir, &workpath);
891 goto out_unlock_upperdentry;
894 if (upperpath.mnt != workpath.mnt) {
895 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
896 goto out_put_workpath;
898 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
899 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
900 goto out_put_workpath;
904 if (!ovl_inuse_trylock(workpath.dentry)) {
905 pr_err("overlayfs: workdir is in-use by another mount\n");
906 goto out_put_workpath;
909 ufs->workbasedir = workpath.dentry;
910 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
913 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
915 goto out_unlock_workdentry;
918 stacklen = ovl_split_lowerdirs(lowertmp);
919 if (stacklen > OVL_MAX_STACK) {
920 pr_err("overlayfs: too many lower directories, limit is %d\n",
922 goto out_free_lowertmp;
923 } else if (!ufs->config.upperdir && stacklen == 1) {
924 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
925 goto out_free_lowertmp;
929 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
931 goto out_free_lowertmp;
935 for (numlower = 0; numlower < stacklen; numlower++) {
936 err = ovl_lower_dir(lower, &stack[numlower], ufs,
937 &sb->s_stack_depth, &remote);
939 goto out_put_lowerpath;
941 lower = strchr(lower, '\0') + 1;
946 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
947 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
948 goto out_put_lowerpath;
951 if (ufs->config.upperdir) {
952 ufs->upper_mnt = clone_private_mount(&upperpath);
953 err = PTR_ERR(ufs->upper_mnt);
954 if (IS_ERR(ufs->upper_mnt)) {
955 pr_err("overlayfs: failed to clone upperpath\n");
956 goto out_put_lowerpath;
959 /* Don't inherit atime flags */
960 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
962 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
964 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
965 OVL_WORKDIR_NAME, false);
967 * Upper should support d_type, else whiteouts are visible.
968 * Given workdir and upper are on same fs, we can do
969 * iterate_dir() on workdir. This check requires successful
970 * creation of workdir in previous step.
975 err = ovl_check_d_type_supported(&workpath);
977 goto out_put_workdir;
980 * We allowed this configuration and don't want to
981 * break users over kernel upgrade. So warn instead
985 pr_warn("overlayfs: upper fs needs to support d_type.\n");
987 /* Check if upper/work fs supports O_TMPFILE */
988 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
989 ufs->tmpfile = !IS_ERR(temp);
993 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
996 * Check if upper/work fs supports trusted.overlay.*
999 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1002 ufs->noxattr = true;
1003 pr_warn("overlayfs: upper fs does not support xattr.\n");
1005 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1008 /* Check if upper/work fs supports file handles */
1009 if (ufs->config.index &&
1010 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1011 ufs->config.index = false;
1012 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1018 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1019 if (ufs->lower_mnt == NULL)
1020 goto out_put_workdir;
1021 for (i = 0; i < numlower; i++) {
1022 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1026 pr_err("overlayfs: failed to clone lowerpath\n");
1027 goto out_put_lower_mnt;
1030 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1031 * will fail instead of modifying lower fs.
1033 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1035 ufs->lower_mnt[ufs->numlower] = mnt;
1038 /* Check if all lower layers are on same sb */
1040 ufs->same_sb = mnt->mnt_sb;
1041 else if (ufs->same_sb != mnt->mnt_sb)
1042 ufs->same_sb = NULL;
1045 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1046 if (!ufs->upper_mnt)
1047 sb->s_flags |= MS_RDONLY;
1048 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1049 ufs->same_sb = NULL;
1051 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
1052 /* Verify lower root is upper root origin */
1053 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
1054 stack[0].dentry, false, true);
1056 pr_err("overlayfs: failed to verify upper root origin\n");
1057 goto out_put_lower_mnt;
1060 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1061 OVL_INDEXDIR_NAME, true);
1062 if (ufs->indexdir) {
1063 /* Verify upper root is index dir origin */
1064 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1065 upperpath.dentry, true, true);
1067 pr_err("overlayfs: failed to verify index dir origin\n");
1069 /* Cleanup bad/stale/orphan index entries */
1071 err = ovl_indexdir_cleanup(ufs->indexdir,
1075 if (err || !ufs->indexdir)
1076 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1078 goto out_put_indexdir;
1081 /* Show index=off/on in /proc/mounts for any of the reasons above */
1083 ufs->config.index = false;
1086 sb->s_d_op = &ovl_reval_dentry_operations;
1088 sb->s_d_op = &ovl_dentry_operations;
1091 ufs->creator_cred = cred = prepare_creds();
1093 goto out_put_indexdir;
1095 /* Never override disk quota limits or use reserved space */
1096 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1099 oe = ovl_alloc_entry(numlower);
1103 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1104 sb->s_op = &ovl_super_operations;
1105 sb->s_xattr = ovl_xattr_handlers;
1106 sb->s_fs_info = ufs;
1107 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1109 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1113 mntput(upperpath.mnt);
1114 for (i = 0; i < numlower; i++)
1115 mntput(stack[i].mnt);
1116 mntput(workpath.mnt);
1119 if (upperpath.dentry) {
1120 oe->has_upper = true;
1121 if (ovl_is_impuredir(upperpath.dentry))
1122 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1124 for (i = 0; i < numlower; i++) {
1125 oe->lowerstack[i].dentry = stack[i].dentry;
1126 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1130 root_dentry->d_fsdata = oe;
1132 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1133 ovl_dentry_lower(root_dentry));
1135 sb->s_root = root_dentry;
1142 put_cred(ufs->creator_cred);
1144 dput(ufs->indexdir);
1146 for (i = 0; i < ufs->numlower; i++)
1147 mntput(ufs->lower_mnt[i]);
1148 kfree(ufs->lower_mnt);
1151 mntput(ufs->upper_mnt);
1153 for (i = 0; i < numlower; i++)
1154 path_put(&stack[i]);
1158 out_unlock_workdentry:
1159 ovl_inuse_unlock(workpath.dentry);
1161 path_put(&workpath);
1162 out_unlock_upperdentry:
1163 ovl_inuse_unlock(upperpath.dentry);
1165 path_put(&upperpath);
1167 kfree(ufs->config.lowerdir);
1168 kfree(ufs->config.upperdir);
1169 kfree(ufs->config.workdir);
1175 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1176 const char *dev_name, void *raw_data)
1178 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1181 static struct file_system_type ovl_fs_type = {
1182 .owner = THIS_MODULE,
1185 .kill_sb = kill_anon_super,
1187 MODULE_ALIAS_FS("overlay");
1189 static void ovl_inode_init_once(void *foo)
1191 struct ovl_inode *oi = foo;
1193 inode_init_once(&oi->vfs_inode);
1196 static int __init ovl_init(void)
1200 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1201 sizeof(struct ovl_inode), 0,
1202 (SLAB_RECLAIM_ACCOUNT|
1203 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1204 ovl_inode_init_once);
1205 if (ovl_inode_cachep == NULL)
1208 err = register_filesystem(&ovl_fs_type);
1210 kmem_cache_destroy(ovl_inode_cachep);
1215 static void __exit ovl_exit(void)
1217 unregister_filesystem(&ovl_fs_type);
1220 * Make sure all delayed rcu free inodes are flushed before we
1224 kmem_cache_destroy(ovl_inode_cachep);
1228 module_init(ovl_init);
1229 module_exit(ovl_exit);