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"
23 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
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)
78 if (!d_is_reg(dentry)) {
79 if (!inode || inode == d_inode(dentry))
84 if (d_is_negative(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);
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 mutex_destroy(&oi->lock);
203 call_rcu(&inode->i_rcu, ovl_i_callback);
206 static void ovl_put_super(struct super_block *sb)
208 struct ovl_fs *ufs = sb->s_fs_info;
213 ovl_inuse_unlock(ufs->workbasedir);
214 dput(ufs->workbasedir);
216 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
217 mntput(ufs->upper_mnt);
218 for (i = 0; i < ufs->numlower; i++)
219 mntput(ufs->lower_mnt[i]);
220 kfree(ufs->lower_mnt);
222 kfree(ufs->config.lowerdir);
223 kfree(ufs->config.upperdir);
224 kfree(ufs->config.workdir);
225 put_cred(ufs->creator_cred);
229 static int ovl_sync_fs(struct super_block *sb, int wait)
231 struct ovl_fs *ufs = sb->s_fs_info;
232 struct super_block *upper_sb;
237 upper_sb = ufs->upper_mnt->mnt_sb;
238 if (!upper_sb->s_op->sync_fs)
241 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
242 down_read(&upper_sb->s_umount);
243 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
244 up_read(&upper_sb->s_umount);
250 * @sb: The overlayfs super block
251 * @buf: The struct kstatfs to fill in with stats
253 * Get the filesystem statistics. As writes always target the upper layer
254 * filesystem pass the statfs to the upper filesystem (if it exists)
256 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
258 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
259 struct dentry *root_dentry = dentry->d_sb->s_root;
263 ovl_path_real(root_dentry, &path);
265 err = vfs_statfs(&path, buf);
267 buf->f_namelen = ofs->namelen;
268 buf->f_type = OVERLAYFS_SUPER_MAGIC;
274 /* Will this overlay be forced to mount/remount ro? */
275 static bool ovl_force_readonly(struct ovl_fs *ufs)
277 return (!ufs->upper_mnt || !ufs->workdir);
283 * Prints the mount options for a given superblock.
284 * Returns zero; does not fail.
286 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
288 struct super_block *sb = dentry->d_sb;
289 struct ovl_fs *ufs = sb->s_fs_info;
291 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
292 if (ufs->config.upperdir) {
293 seq_show_option(m, "upperdir", ufs->config.upperdir);
294 seq_show_option(m, "workdir", ufs->config.workdir);
296 if (ufs->config.default_permissions)
297 seq_puts(m, ",default_permissions");
298 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
299 seq_printf(m, ",redirect_dir=%s",
300 ufs->config.redirect_dir ? "on" : "off");
301 if (ufs->config.index != ovl_index_def)
302 seq_printf(m, ",index=%s",
303 ufs->config.index ? "on" : "off");
307 static int ovl_remount(struct super_block *sb, int *flags, char *data)
309 struct ovl_fs *ufs = sb->s_fs_info;
311 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
317 static const struct super_operations ovl_super_operations = {
318 .alloc_inode = ovl_alloc_inode,
319 .destroy_inode = ovl_destroy_inode,
320 .drop_inode = generic_delete_inode,
321 .put_super = ovl_put_super,
322 .sync_fs = ovl_sync_fs,
323 .statfs = ovl_statfs,
324 .show_options = ovl_show_options,
325 .remount_fs = ovl_remount,
332 OPT_DEFAULT_PERMISSIONS,
334 OPT_REDIRECT_DIR_OFF,
340 static const match_table_t ovl_tokens = {
341 {OPT_LOWERDIR, "lowerdir=%s"},
342 {OPT_UPPERDIR, "upperdir=%s"},
343 {OPT_WORKDIR, "workdir=%s"},
344 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
345 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
346 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
347 {OPT_INDEX_ON, "index=on"},
348 {OPT_INDEX_OFF, "index=off"},
352 static char *ovl_next_opt(char **s)
360 for (p = sbegin; *p; p++) {
365 } else if (*p == ',') {
375 static int ovl_parse_opt(char *opt, struct ovl_config *config)
379 while ((p = ovl_next_opt(&opt)) != NULL) {
381 substring_t args[MAX_OPT_ARGS];
386 token = match_token(p, ovl_tokens, args);
389 kfree(config->upperdir);
390 config->upperdir = match_strdup(&args[0]);
391 if (!config->upperdir)
396 kfree(config->lowerdir);
397 config->lowerdir = match_strdup(&args[0]);
398 if (!config->lowerdir)
403 kfree(config->workdir);
404 config->workdir = match_strdup(&args[0]);
405 if (!config->workdir)
409 case OPT_DEFAULT_PERMISSIONS:
410 config->default_permissions = true;
413 case OPT_REDIRECT_DIR_ON:
414 config->redirect_dir = true;
417 case OPT_REDIRECT_DIR_OFF:
418 config->redirect_dir = false;
422 config->index = true;
426 config->index = false;
430 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
435 /* Workdir is useless in non-upper mount */
436 if (!config->upperdir && config->workdir) {
437 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
439 kfree(config->workdir);
440 config->workdir = NULL;
446 #define OVL_WORKDIR_NAME "work"
447 #define OVL_INDEXDIR_NAME "index"
449 static struct dentry *ovl_workdir_create(struct super_block *sb,
451 struct dentry *dentry,
452 const char *name, bool persist)
454 struct inode *dir = dentry->d_inode;
455 struct vfsmount *mnt = ufs->upper_mnt;
458 bool retried = false;
461 err = mnt_want_write(mnt);
465 inode_lock_nested(dir, I_MUTEX_PARENT);
469 work = lookup_one_len(name, dentry, strlen(name));
472 struct iattr attr = {
473 .ia_valid = ATTR_MODE,
474 .ia_mode = S_IFDIR | 0,
486 ovl_workdir_cleanup(dir, mnt, work, 0);
491 err = ovl_create_real(dir, work,
492 &(struct cattr){.mode = S_IFDIR | 0},
498 * Try to remove POSIX ACL xattrs from workdir. We are good if:
500 * a) success (there was a POSIX ACL xattr and was removed)
501 * b) -ENODATA (there was no POSIX ACL xattr)
502 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
504 * There are various other error values that could effectively
505 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
506 * if the xattr name is too long), but the set of filesystems
507 * allowed as upper are limited to "normal" ones, where checking
508 * for the above two errors is sufficient.
510 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
511 if (err && err != -ENODATA && err != -EOPNOTSUPP)
514 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
515 if (err && err != -ENODATA && err != -EOPNOTSUPP)
518 /* Clear any inherited mode bits */
519 inode_lock(work->d_inode);
520 err = notify_change(work, &attr, NULL);
521 inode_unlock(work->d_inode);
538 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
539 ufs->config.workdir, name, -err);
540 sb->s_flags |= MS_RDONLY;
545 static void ovl_unescape(char *s)
558 static int ovl_mount_dir_noesc(const char *name, struct path *path)
563 pr_err("overlayfs: empty lowerdir\n");
566 err = kern_path(name, LOOKUP_FOLLOW, path);
568 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
572 if (ovl_dentry_weird(path->dentry)) {
573 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
576 if (!d_is_dir(path->dentry)) {
577 pr_err("overlayfs: '%s' not a directory\n", name);
588 static int ovl_mount_dir(const char *name, struct path *path)
591 char *tmp = kstrdup(name, GFP_KERNEL);
595 err = ovl_mount_dir_noesc(tmp, path);
598 if (ovl_dentry_remote(path->dentry)) {
599 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
609 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
612 struct kstatfs statfs;
613 int err = vfs_statfs(path, &statfs);
616 pr_err("overlayfs: statfs failed on '%s'\n", name);
618 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
623 static int ovl_lower_dir(const char *name, struct path *path,
624 struct ovl_fs *ofs, int *stack_depth, bool *remote)
628 err = ovl_mount_dir_noesc(name, path);
632 err = ovl_check_namelen(path, ofs, name);
636 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
638 if (ovl_dentry_remote(path->dentry))
642 * The inodes index feature needs to encode and decode file
643 * handles, so it requires that all layers support them.
645 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
646 ofs->config.index = false;
647 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
658 /* Workdir should not be subdir of upperdir and vice versa */
659 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
663 if (workdir != upperdir) {
664 ok = (lock_rename(workdir, upperdir) == NULL);
665 unlock_rename(workdir, upperdir);
670 static unsigned int ovl_split_lowerdirs(char *str)
672 unsigned int ctr = 1;
675 for (s = d = str;; s++, d++) {
678 } else if (*s == ':') {
690 static int __maybe_unused
691 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
692 struct dentry *dentry, struct inode *inode,
693 const char *name, void *buffer, size_t size)
695 return ovl_xattr_get(dentry, handler->name, buffer, size);
698 static int __maybe_unused
699 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
700 struct dentry *dentry, struct inode *inode,
701 const char *name, const void *value,
702 size_t size, int flags)
704 struct dentry *workdir = ovl_workdir(dentry);
705 struct inode *realinode = ovl_inode_real(inode);
706 struct posix_acl *acl = NULL;
709 /* Check that everything is OK before copy-up */
711 acl = posix_acl_from_xattr(&init_user_ns, value, size);
716 if (!IS_POSIXACL(d_inode(workdir)))
717 goto out_acl_release;
718 if (!realinode->i_op->set_acl)
719 goto out_acl_release;
720 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
721 err = acl ? -EACCES : 0;
722 goto out_acl_release;
725 if (!inode_owner_or_capable(inode))
726 goto out_acl_release;
728 posix_acl_release(acl);
731 * Check if sgid bit needs to be cleared (actual setacl operation will
732 * be done with mounter's capabilities and so that won't do it for us).
734 if (unlikely(inode->i_mode & S_ISGID) &&
735 handler->flags == ACL_TYPE_ACCESS &&
736 !in_group_p(inode->i_gid) &&
737 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
738 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
740 err = ovl_setattr(dentry, &iattr);
745 err = ovl_xattr_set(dentry, handler->name, value, size, flags);
747 ovl_copyattr(ovl_inode_real(inode), inode);
752 posix_acl_release(acl);
756 static int ovl_own_xattr_get(const struct xattr_handler *handler,
757 struct dentry *dentry, struct inode *inode,
758 const char *name, void *buffer, size_t size)
763 static int ovl_own_xattr_set(const struct xattr_handler *handler,
764 struct dentry *dentry, struct inode *inode,
765 const char *name, const void *value,
766 size_t size, int flags)
771 static int ovl_other_xattr_get(const struct xattr_handler *handler,
772 struct dentry *dentry, struct inode *inode,
773 const char *name, void *buffer, size_t size)
775 return ovl_xattr_get(dentry, name, buffer, size);
778 static int ovl_other_xattr_set(const struct xattr_handler *handler,
779 struct dentry *dentry, struct inode *inode,
780 const char *name, const void *value,
781 size_t size, int flags)
783 return ovl_xattr_set(dentry, name, value, size, flags);
786 static const struct xattr_handler __maybe_unused
787 ovl_posix_acl_access_xattr_handler = {
788 .name = XATTR_NAME_POSIX_ACL_ACCESS,
789 .flags = ACL_TYPE_ACCESS,
790 .get = ovl_posix_acl_xattr_get,
791 .set = ovl_posix_acl_xattr_set,
794 static const struct xattr_handler __maybe_unused
795 ovl_posix_acl_default_xattr_handler = {
796 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
797 .flags = ACL_TYPE_DEFAULT,
798 .get = ovl_posix_acl_xattr_get,
799 .set = ovl_posix_acl_xattr_set,
802 static const struct xattr_handler ovl_own_xattr_handler = {
803 .prefix = OVL_XATTR_PREFIX,
804 .get = ovl_own_xattr_get,
805 .set = ovl_own_xattr_set,
808 static const struct xattr_handler ovl_other_xattr_handler = {
809 .prefix = "", /* catch all */
810 .get = ovl_other_xattr_get,
811 .set = ovl_other_xattr_set,
814 static const struct xattr_handler *ovl_xattr_handlers[] = {
815 #ifdef CONFIG_FS_POSIX_ACL
816 &ovl_posix_acl_access_xattr_handler,
817 &ovl_posix_acl_default_xattr_handler,
819 &ovl_own_xattr_handler,
820 &ovl_other_xattr_handler,
824 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
826 struct path upperpath = { };
827 struct path workpath = { };
828 struct dentry *root_dentry;
829 struct ovl_entry *oe;
831 struct path *stack = NULL;
834 unsigned int numlower;
835 unsigned int stacklen = 0;
842 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
846 ufs->config.redirect_dir = ovl_redirect_dir_def;
847 ufs->config.index = ovl_index_def;
848 err = ovl_parse_opt((char *) data, &ufs->config);
850 goto out_free_config;
853 if (!ufs->config.lowerdir) {
855 pr_err("overlayfs: missing 'lowerdir'\n");
856 goto out_free_config;
859 sb->s_stack_depth = 0;
860 sb->s_maxbytes = MAX_LFS_FILESIZE;
861 if (ufs->config.upperdir) {
862 if (!ufs->config.workdir) {
863 pr_err("overlayfs: missing 'workdir'\n");
864 goto out_free_config;
867 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
869 goto out_free_config;
871 /* Upper fs should not be r/o */
872 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
873 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
875 goto out_put_upperpath;
878 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
880 goto out_put_upperpath;
883 if (!ovl_inuse_trylock(upperpath.dentry)) {
884 pr_err("overlayfs: upperdir is in-use by another mount\n");
885 goto out_put_upperpath;
888 err = ovl_mount_dir(ufs->config.workdir, &workpath);
890 goto out_unlock_upperdentry;
893 if (upperpath.mnt != workpath.mnt) {
894 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
895 goto out_put_workpath;
897 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
898 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
899 goto out_put_workpath;
903 if (!ovl_inuse_trylock(workpath.dentry)) {
904 pr_err("overlayfs: workdir is in-use by another mount\n");
905 goto out_put_workpath;
908 ufs->workbasedir = workpath.dentry;
909 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
912 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
914 goto out_unlock_workdentry;
917 stacklen = ovl_split_lowerdirs(lowertmp);
918 if (stacklen > OVL_MAX_STACK) {
919 pr_err("overlayfs: too many lower directories, limit is %d\n",
921 goto out_free_lowertmp;
922 } else if (!ufs->config.upperdir && stacklen == 1) {
923 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
924 goto out_free_lowertmp;
928 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
930 goto out_free_lowertmp;
934 for (numlower = 0; numlower < stacklen; numlower++) {
935 err = ovl_lower_dir(lower, &stack[numlower], ufs,
936 &sb->s_stack_depth, &remote);
938 goto out_put_lowerpath;
940 lower = strchr(lower, '\0') + 1;
945 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
946 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
947 goto out_put_lowerpath;
950 if (ufs->config.upperdir) {
951 ufs->upper_mnt = clone_private_mount(&upperpath);
952 err = PTR_ERR(ufs->upper_mnt);
953 if (IS_ERR(ufs->upper_mnt)) {
954 pr_err("overlayfs: failed to clone upperpath\n");
955 goto out_put_lowerpath;
958 /* Don't inherit atime flags */
959 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
961 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
963 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
964 OVL_WORKDIR_NAME, false);
966 * Upper should support d_type, else whiteouts are visible.
967 * Given workdir and upper are on same fs, we can do
968 * iterate_dir() on workdir. This check requires successful
969 * creation of workdir in previous step.
974 err = ovl_check_d_type_supported(&workpath);
976 goto out_put_workdir;
979 * We allowed this configuration and don't want to
980 * break users over kernel upgrade. So warn instead
984 pr_warn("overlayfs: upper fs needs to support d_type.\n");
986 /* Check if upper/work fs supports O_TMPFILE */
987 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
988 ufs->tmpfile = !IS_ERR(temp);
992 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
995 * Check if upper/work fs supports trusted.overlay.*
998 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1001 ufs->noxattr = true;
1002 pr_warn("overlayfs: upper fs does not support xattr.\n");
1004 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1007 /* Check if upper/work fs supports file handles */
1008 if (ufs->config.index &&
1009 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1010 ufs->config.index = false;
1011 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1017 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1018 if (ufs->lower_mnt == NULL)
1019 goto out_put_workdir;
1020 for (i = 0; i < numlower; i++) {
1021 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1025 pr_err("overlayfs: failed to clone lowerpath\n");
1026 goto out_put_lower_mnt;
1029 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1030 * will fail instead of modifying lower fs.
1032 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1034 ufs->lower_mnt[ufs->numlower] = mnt;
1037 /* Check if all lower layers are on same sb */
1039 ufs->same_sb = mnt->mnt_sb;
1040 else if (ufs->same_sb != mnt->mnt_sb)
1041 ufs->same_sb = NULL;
1044 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1045 if (!ufs->upper_mnt)
1046 sb->s_flags |= MS_RDONLY;
1047 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1048 ufs->same_sb = NULL;
1050 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
1051 /* Verify lower root is upper root origin */
1052 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
1053 stack[0].dentry, false, true);
1055 pr_err("overlayfs: failed to verify upper root origin\n");
1056 goto out_put_lower_mnt;
1059 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1060 OVL_INDEXDIR_NAME, true);
1061 err = PTR_ERR(ufs->indexdir);
1062 if (IS_ERR(ufs->indexdir))
1063 goto out_put_lower_mnt;
1065 if (ufs->indexdir) {
1066 /* Verify upper root is index dir origin */
1067 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1068 upperpath.dentry, true, true);
1070 pr_err("overlayfs: failed to verify index dir origin\n");
1072 /* Cleanup bad/stale/orphan index entries */
1074 err = ovl_indexdir_cleanup(ufs->indexdir,
1078 if (err || !ufs->indexdir)
1079 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1081 goto out_put_indexdir;
1084 /* Show index=off/on in /proc/mounts for any of the reasons above */
1086 ufs->config.index = false;
1089 sb->s_d_op = &ovl_reval_dentry_operations;
1091 sb->s_d_op = &ovl_dentry_operations;
1093 ufs->creator_cred = cred = prepare_creds();
1095 goto out_put_indexdir;
1097 /* Never override disk quota limits or use reserved space */
1098 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1101 oe = ovl_alloc_entry(numlower);
1105 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1106 sb->s_op = &ovl_super_operations;
1107 sb->s_xattr = ovl_xattr_handlers;
1108 sb->s_fs_info = ufs;
1109 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1111 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1115 mntput(upperpath.mnt);
1116 for (i = 0; i < numlower; i++)
1117 mntput(stack[i].mnt);
1118 mntput(workpath.mnt);
1121 if (upperpath.dentry) {
1122 oe->has_upper = true;
1123 if (ovl_is_impuredir(upperpath.dentry))
1124 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1126 for (i = 0; i < numlower; i++) {
1127 oe->lowerstack[i].dentry = stack[i].dentry;
1128 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1132 root_dentry->d_fsdata = oe;
1134 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1135 ovl_dentry_lower(root_dentry));
1137 sb->s_root = root_dentry;
1144 put_cred(ufs->creator_cred);
1146 dput(ufs->indexdir);
1148 for (i = 0; i < ufs->numlower; i++)
1149 mntput(ufs->lower_mnt[i]);
1150 kfree(ufs->lower_mnt);
1153 mntput(ufs->upper_mnt);
1155 for (i = 0; i < numlower; i++)
1156 path_put(&stack[i]);
1160 out_unlock_workdentry:
1161 ovl_inuse_unlock(workpath.dentry);
1163 path_put(&workpath);
1164 out_unlock_upperdentry:
1165 ovl_inuse_unlock(upperpath.dentry);
1167 path_put(&upperpath);
1169 kfree(ufs->config.lowerdir);
1170 kfree(ufs->config.upperdir);
1171 kfree(ufs->config.workdir);
1177 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1178 const char *dev_name, void *raw_data)
1180 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1183 static struct file_system_type ovl_fs_type = {
1184 .owner = THIS_MODULE,
1187 .kill_sb = kill_anon_super,
1189 MODULE_ALIAS_FS("overlay");
1191 static void ovl_inode_init_once(void *foo)
1193 struct ovl_inode *oi = foo;
1195 inode_init_once(&oi->vfs_inode);
1198 static int __init ovl_init(void)
1202 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1203 sizeof(struct ovl_inode), 0,
1204 (SLAB_RECLAIM_ACCOUNT|
1205 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1206 ovl_inode_init_once);
1207 if (ovl_inode_cachep == NULL)
1210 err = register_filesystem(&ovl_fs_type);
1212 kmem_cache_destroy(ovl_inode_cachep);
1217 static void __exit ovl_exit(void)
1219 unregister_filesystem(&ovl_fs_type);
1222 * Make sure all delayed rcu free inodes are flushed before we
1226 kmem_cache_destroy(ovl_inode_cachep);
1230 module_init(ovl_init);
1231 module_exit(ovl_exit);