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, 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);
184 oi->__upperdentry = NULL;
186 mutex_init(&oi->lock);
188 return &oi->vfs_inode;
191 static void ovl_i_callback(struct rcu_head *head)
193 struct inode *inode = container_of(head, struct inode, i_rcu);
195 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
198 static void ovl_destroy_inode(struct inode *inode)
200 struct ovl_inode *oi = OVL_I(inode);
202 dput(oi->__upperdentry);
205 ovl_dir_cache_free(inode);
206 mutex_destroy(&oi->lock);
208 call_rcu(&inode->i_rcu, ovl_i_callback);
211 static void ovl_put_super(struct super_block *sb)
213 struct ovl_fs *ufs = sb->s_fs_info;
218 if (ufs->workdir_locked)
219 ovl_inuse_unlock(ufs->workbasedir);
220 dput(ufs->workbasedir);
221 if (ufs->upper_mnt && ufs->upperdir_locked)
222 ovl_inuse_unlock(ufs->upper_mnt->mnt_root);
223 mntput(ufs->upper_mnt);
224 for (i = 0; i < ufs->numlower; i++)
225 mntput(ufs->lower_mnt[i]);
226 kfree(ufs->lower_mnt);
228 kfree(ufs->config.lowerdir);
229 kfree(ufs->config.upperdir);
230 kfree(ufs->config.workdir);
231 put_cred(ufs->creator_cred);
235 /* Sync real dirty inodes in upper filesystem (if it exists) */
236 static int ovl_sync_fs(struct super_block *sb, int wait)
238 struct ovl_fs *ufs = sb->s_fs_info;
239 struct super_block *upper_sb;
246 * If this is a sync(2) call or an emergency sync, all the super blocks
247 * will be iterated, including upper_sb, so no need to do anything.
249 * If this is a syncfs(2) call, then we do need to call
250 * sync_filesystem() on upper_sb, but enough if we do it when being
251 * called with wait == 1.
256 upper_sb = ufs->upper_mnt->mnt_sb;
258 down_read(&upper_sb->s_umount);
259 ret = sync_filesystem(upper_sb);
260 up_read(&upper_sb->s_umount);
267 * @sb: The overlayfs super block
268 * @buf: The struct kstatfs to fill in with stats
270 * Get the filesystem statistics. As writes always target the upper layer
271 * filesystem pass the statfs to the upper filesystem (if it exists)
273 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
275 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
276 struct dentry *root_dentry = dentry->d_sb->s_root;
280 ovl_path_real(root_dentry, &path);
282 err = vfs_statfs(&path, buf);
284 buf->f_namelen = ofs->namelen;
285 buf->f_type = OVERLAYFS_SUPER_MAGIC;
291 /* Will this overlay be forced to mount/remount ro? */
292 static bool ovl_force_readonly(struct ovl_fs *ufs)
294 return (!ufs->upper_mnt || !ufs->workdir);
300 * Prints the mount options for a given superblock.
301 * Returns zero; does not fail.
303 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
305 struct super_block *sb = dentry->d_sb;
306 struct ovl_fs *ufs = sb->s_fs_info;
308 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
309 if (ufs->config.upperdir) {
310 seq_show_option(m, "upperdir", ufs->config.upperdir);
311 seq_show_option(m, "workdir", ufs->config.workdir);
313 if (ufs->config.default_permissions)
314 seq_puts(m, ",default_permissions");
315 if (ufs->config.redirect_dir != ovl_redirect_dir_def)
316 seq_printf(m, ",redirect_dir=%s",
317 ufs->config.redirect_dir ? "on" : "off");
318 if (ufs->config.index != ovl_index_def)
319 seq_printf(m, ",index=%s",
320 ufs->config.index ? "on" : "off");
324 static int ovl_remount(struct super_block *sb, int *flags, char *data)
326 struct ovl_fs *ufs = sb->s_fs_info;
328 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ufs))
334 static const struct super_operations ovl_super_operations = {
335 .alloc_inode = ovl_alloc_inode,
336 .destroy_inode = ovl_destroy_inode,
337 .drop_inode = generic_delete_inode,
338 .put_super = ovl_put_super,
339 .sync_fs = ovl_sync_fs,
340 .statfs = ovl_statfs,
341 .show_options = ovl_show_options,
342 .remount_fs = ovl_remount,
349 OPT_DEFAULT_PERMISSIONS,
351 OPT_REDIRECT_DIR_OFF,
357 static const match_table_t ovl_tokens = {
358 {OPT_LOWERDIR, "lowerdir=%s"},
359 {OPT_UPPERDIR, "upperdir=%s"},
360 {OPT_WORKDIR, "workdir=%s"},
361 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
362 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
363 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
364 {OPT_INDEX_ON, "index=on"},
365 {OPT_INDEX_OFF, "index=off"},
369 static char *ovl_next_opt(char **s)
377 for (p = sbegin; *p; p++) {
382 } else if (*p == ',') {
392 static int ovl_parse_opt(char *opt, struct ovl_config *config)
396 while ((p = ovl_next_opt(&opt)) != NULL) {
398 substring_t args[MAX_OPT_ARGS];
403 token = match_token(p, ovl_tokens, args);
406 kfree(config->upperdir);
407 config->upperdir = match_strdup(&args[0]);
408 if (!config->upperdir)
413 kfree(config->lowerdir);
414 config->lowerdir = match_strdup(&args[0]);
415 if (!config->lowerdir)
420 kfree(config->workdir);
421 config->workdir = match_strdup(&args[0]);
422 if (!config->workdir)
426 case OPT_DEFAULT_PERMISSIONS:
427 config->default_permissions = true;
430 case OPT_REDIRECT_DIR_ON:
431 config->redirect_dir = true;
434 case OPT_REDIRECT_DIR_OFF:
435 config->redirect_dir = false;
439 config->index = true;
443 config->index = false;
447 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
452 /* Workdir is useless in non-upper mount */
453 if (!config->upperdir && config->workdir) {
454 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
456 kfree(config->workdir);
457 config->workdir = NULL;
463 #define OVL_WORKDIR_NAME "work"
464 #define OVL_INDEXDIR_NAME "index"
466 static struct dentry *ovl_workdir_create(struct super_block *sb,
468 struct dentry *dentry,
469 const char *name, bool persist)
471 struct inode *dir = dentry->d_inode;
472 struct vfsmount *mnt = ufs->upper_mnt;
475 bool retried = false;
478 err = mnt_want_write(mnt);
482 inode_lock_nested(dir, I_MUTEX_PARENT);
486 work = lookup_one_len(name, dentry, strlen(name));
489 struct iattr attr = {
490 .ia_valid = ATTR_MODE,
491 .ia_mode = S_IFDIR | 0,
503 ovl_workdir_cleanup(dir, mnt, work, 0);
508 err = ovl_create_real(dir, work,
509 &(struct cattr){.mode = S_IFDIR | 0},
515 * Try to remove POSIX ACL xattrs from workdir. We are good if:
517 * a) success (there was a POSIX ACL xattr and was removed)
518 * b) -ENODATA (there was no POSIX ACL xattr)
519 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
521 * There are various other error values that could effectively
522 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
523 * if the xattr name is too long), but the set of filesystems
524 * allowed as upper are limited to "normal" ones, where checking
525 * for the above two errors is sufficient.
527 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
528 if (err && err != -ENODATA && err != -EOPNOTSUPP)
531 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
532 if (err && err != -ENODATA && err != -EOPNOTSUPP)
535 /* Clear any inherited mode bits */
536 inode_lock(work->d_inode);
537 err = notify_change(work, &attr, NULL);
538 inode_unlock(work->d_inode);
555 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
556 ufs->config.workdir, name, -err);
557 sb->s_flags |= MS_RDONLY;
562 static void ovl_unescape(char *s)
575 static int ovl_mount_dir_noesc(const char *name, struct path *path)
580 pr_err("overlayfs: empty lowerdir\n");
583 err = kern_path(name, LOOKUP_FOLLOW, path);
585 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
589 if (ovl_dentry_weird(path->dentry)) {
590 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
593 if (!d_is_dir(path->dentry)) {
594 pr_err("overlayfs: '%s' not a directory\n", name);
605 static int ovl_mount_dir(const char *name, struct path *path)
608 char *tmp = kstrdup(name, GFP_KERNEL);
612 err = ovl_mount_dir_noesc(tmp, path);
615 if (ovl_dentry_remote(path->dentry)) {
616 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
626 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
629 struct kstatfs statfs;
630 int err = vfs_statfs(path, &statfs);
633 pr_err("overlayfs: statfs failed on '%s'\n", name);
635 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
640 static int ovl_lower_dir(const char *name, struct path *path,
641 struct ovl_fs *ofs, int *stack_depth, bool *remote)
645 err = ovl_mount_dir_noesc(name, path);
649 err = ovl_check_namelen(path, ofs, name);
653 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
655 if (ovl_dentry_remote(path->dentry))
659 * The inodes index feature needs to encode and decode file
660 * handles, so it requires that all layers support them.
662 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
663 ofs->config.index = false;
664 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
675 /* Workdir should not be subdir of upperdir and vice versa */
676 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
680 if (workdir != upperdir) {
681 ok = (lock_rename(workdir, upperdir) == NULL);
682 unlock_rename(workdir, upperdir);
687 static unsigned int ovl_split_lowerdirs(char *str)
689 unsigned int ctr = 1;
692 for (s = d = str;; s++, d++) {
695 } else if (*s == ':') {
707 static int __maybe_unused
708 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
709 struct dentry *dentry, struct inode *inode,
710 const char *name, void *buffer, size_t size)
712 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
715 static int __maybe_unused
716 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
717 struct dentry *dentry, struct inode *inode,
718 const char *name, const void *value,
719 size_t size, int flags)
721 struct dentry *workdir = ovl_workdir(dentry);
722 struct inode *realinode = ovl_inode_real(inode);
723 struct posix_acl *acl = NULL;
726 /* Check that everything is OK before copy-up */
728 acl = posix_acl_from_xattr(&init_user_ns, value, size);
733 if (!IS_POSIXACL(d_inode(workdir)))
734 goto out_acl_release;
735 if (!realinode->i_op->set_acl)
736 goto out_acl_release;
737 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
738 err = acl ? -EACCES : 0;
739 goto out_acl_release;
742 if (!inode_owner_or_capable(inode))
743 goto out_acl_release;
745 posix_acl_release(acl);
748 * Check if sgid bit needs to be cleared (actual setacl operation will
749 * be done with mounter's capabilities and so that won't do it for us).
751 if (unlikely(inode->i_mode & S_ISGID) &&
752 handler->flags == ACL_TYPE_ACCESS &&
753 !in_group_p(inode->i_gid) &&
754 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
755 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
757 err = ovl_setattr(dentry, &iattr);
762 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
764 ovl_copyattr(ovl_inode_real(inode), inode);
769 posix_acl_release(acl);
773 static int ovl_own_xattr_get(const struct xattr_handler *handler,
774 struct dentry *dentry, struct inode *inode,
775 const char *name, void *buffer, size_t size)
780 static int ovl_own_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)
788 static int ovl_other_xattr_get(const struct xattr_handler *handler,
789 struct dentry *dentry, struct inode *inode,
790 const char *name, void *buffer, size_t size)
792 return ovl_xattr_get(dentry, inode, name, buffer, size);
795 static int ovl_other_xattr_set(const struct xattr_handler *handler,
796 struct dentry *dentry, struct inode *inode,
797 const char *name, const void *value,
798 size_t size, int flags)
800 return ovl_xattr_set(dentry, inode, name, value, size, flags);
803 static const struct xattr_handler __maybe_unused
804 ovl_posix_acl_access_xattr_handler = {
805 .name = XATTR_NAME_POSIX_ACL_ACCESS,
806 .flags = ACL_TYPE_ACCESS,
807 .get = ovl_posix_acl_xattr_get,
808 .set = ovl_posix_acl_xattr_set,
811 static const struct xattr_handler __maybe_unused
812 ovl_posix_acl_default_xattr_handler = {
813 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
814 .flags = ACL_TYPE_DEFAULT,
815 .get = ovl_posix_acl_xattr_get,
816 .set = ovl_posix_acl_xattr_set,
819 static const struct xattr_handler ovl_own_xattr_handler = {
820 .prefix = OVL_XATTR_PREFIX,
821 .get = ovl_own_xattr_get,
822 .set = ovl_own_xattr_set,
825 static const struct xattr_handler ovl_other_xattr_handler = {
826 .prefix = "", /* catch all */
827 .get = ovl_other_xattr_get,
828 .set = ovl_other_xattr_set,
831 static const struct xattr_handler *ovl_xattr_handlers[] = {
832 #ifdef CONFIG_FS_POSIX_ACL
833 &ovl_posix_acl_access_xattr_handler,
834 &ovl_posix_acl_default_xattr_handler,
836 &ovl_own_xattr_handler,
837 &ovl_other_xattr_handler,
841 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
843 struct path upperpath = { };
844 struct path workpath = { };
845 struct dentry *root_dentry;
846 struct ovl_entry *oe;
848 struct path *stack = NULL;
851 unsigned int numlower;
852 unsigned int stacklen = 0;
859 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
863 ufs->config.redirect_dir = ovl_redirect_dir_def;
864 ufs->config.index = ovl_index_def;
865 err = ovl_parse_opt((char *) data, &ufs->config);
867 goto out_free_config;
870 if (!ufs->config.lowerdir) {
872 pr_err("overlayfs: missing 'lowerdir'\n");
873 goto out_free_config;
876 sb->s_stack_depth = 0;
877 sb->s_maxbytes = MAX_LFS_FILESIZE;
878 if (ufs->config.upperdir) {
879 if (!ufs->config.workdir) {
880 pr_err("overlayfs: missing 'workdir'\n");
881 goto out_free_config;
884 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
886 goto out_free_config;
888 /* Upper fs should not be r/o */
889 if (sb_rdonly(upperpath.mnt->mnt_sb)) {
890 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
892 goto out_put_upperpath;
895 err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir);
897 goto out_put_upperpath;
900 if (ovl_inuse_trylock(upperpath.dentry)) {
901 ufs->upperdir_locked = true;
902 } else if (ufs->config.index) {
903 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
904 goto out_put_upperpath;
906 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
909 err = ovl_mount_dir(ufs->config.workdir, &workpath);
911 goto out_unlock_upperdentry;
914 if (upperpath.mnt != workpath.mnt) {
915 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
916 goto out_put_workpath;
918 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
919 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
920 goto out_put_workpath;
924 if (ovl_inuse_trylock(workpath.dentry)) {
925 ufs->workdir_locked = true;
926 } else if (ufs->config.index) {
927 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
928 goto out_put_workpath;
930 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
933 ufs->workbasedir = workpath.dentry;
934 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
937 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
939 goto out_unlock_workdentry;
942 stacklen = ovl_split_lowerdirs(lowertmp);
943 if (stacklen > OVL_MAX_STACK) {
944 pr_err("overlayfs: too many lower directories, limit is %d\n",
946 goto out_free_lowertmp;
947 } else if (!ufs->config.upperdir && stacklen == 1) {
948 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
949 goto out_free_lowertmp;
953 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
955 goto out_free_lowertmp;
959 for (numlower = 0; numlower < stacklen; numlower++) {
960 err = ovl_lower_dir(lower, &stack[numlower], ufs,
961 &sb->s_stack_depth, &remote);
963 goto out_put_lowerpath;
965 lower = strchr(lower, '\0') + 1;
970 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
971 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
972 goto out_put_lowerpath;
975 if (ufs->config.upperdir) {
976 ufs->upper_mnt = clone_private_mount(&upperpath);
977 err = PTR_ERR(ufs->upper_mnt);
978 if (IS_ERR(ufs->upper_mnt)) {
979 pr_err("overlayfs: failed to clone upperpath\n");
980 goto out_put_lowerpath;
983 /* Don't inherit atime flags */
984 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
986 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
988 ufs->workdir = ovl_workdir_create(sb, ufs, workpath.dentry,
989 OVL_WORKDIR_NAME, false);
991 * Upper should support d_type, else whiteouts are visible.
992 * Given workdir and upper are on same fs, we can do
993 * iterate_dir() on workdir. This check requires successful
994 * creation of workdir in previous step.
999 err = ovl_check_d_type_supported(&workpath);
1001 goto out_put_workdir;
1004 * We allowed this configuration and don't want to
1005 * break users over kernel upgrade. So warn instead
1009 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1011 /* Check if upper/work fs supports O_TMPFILE */
1012 temp = ovl_do_tmpfile(ufs->workdir, S_IFREG | 0);
1013 ufs->tmpfile = !IS_ERR(temp);
1017 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1020 * Check if upper/work fs supports trusted.overlay.*
1023 err = ovl_do_setxattr(ufs->workdir, OVL_XATTR_OPAQUE,
1026 ufs->noxattr = true;
1027 pr_warn("overlayfs: upper fs does not support xattr.\n");
1029 vfs_removexattr(ufs->workdir, OVL_XATTR_OPAQUE);
1032 /* Check if upper/work fs supports file handles */
1033 if (ufs->config.index &&
1034 !ovl_can_decode_fh(ufs->workdir->d_sb)) {
1035 ufs->config.index = false;
1036 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1042 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
1043 if (ufs->lower_mnt == NULL)
1044 goto out_put_workdir;
1045 for (i = 0; i < numlower; i++) {
1046 struct vfsmount *mnt = clone_private_mount(&stack[i]);
1050 pr_err("overlayfs: failed to clone lowerpath\n");
1051 goto out_put_lower_mnt;
1054 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1055 * will fail instead of modifying lower fs.
1057 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1059 ufs->lower_mnt[ufs->numlower] = mnt;
1062 /* Check if all lower layers are on same sb */
1064 ufs->same_sb = mnt->mnt_sb;
1065 else if (ufs->same_sb != mnt->mnt_sb)
1066 ufs->same_sb = NULL;
1069 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1070 if (!ufs->upper_mnt)
1071 sb->s_flags |= MS_RDONLY;
1072 else if (ufs->upper_mnt->mnt_sb != ufs->same_sb)
1073 ufs->same_sb = NULL;
1075 if (!(ovl_force_readonly(ufs)) && ufs->config.index) {
1076 /* Verify lower root is upper root origin */
1077 err = ovl_verify_origin(upperpath.dentry, ufs->lower_mnt[0],
1078 stack[0].dentry, false, true);
1080 pr_err("overlayfs: failed to verify upper root origin\n");
1081 goto out_put_lower_mnt;
1084 ufs->indexdir = ovl_workdir_create(sb, ufs, workpath.dentry,
1085 OVL_INDEXDIR_NAME, true);
1086 if (ufs->indexdir) {
1087 /* Verify upper root is index dir origin */
1088 err = ovl_verify_origin(ufs->indexdir, ufs->upper_mnt,
1089 upperpath.dentry, true, true);
1091 pr_err("overlayfs: failed to verify index dir origin\n");
1093 /* Cleanup bad/stale/orphan index entries */
1095 err = ovl_indexdir_cleanup(ufs->indexdir,
1099 if (err || !ufs->indexdir)
1100 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1102 goto out_put_indexdir;
1105 /* Show index=off/on in /proc/mounts for any of the reasons above */
1107 ufs->config.index = false;
1110 sb->s_d_op = &ovl_reval_dentry_operations;
1112 sb->s_d_op = &ovl_dentry_operations;
1115 ufs->creator_cred = cred = prepare_creds();
1117 goto out_put_indexdir;
1119 /* Never override disk quota limits or use reserved space */
1120 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1123 oe = ovl_alloc_entry(numlower);
1127 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1128 sb->s_op = &ovl_super_operations;
1129 sb->s_xattr = ovl_xattr_handlers;
1130 sb->s_fs_info = ufs;
1131 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1133 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1137 mntput(upperpath.mnt);
1138 for (i = 0; i < numlower; i++)
1139 mntput(stack[i].mnt);
1140 mntput(workpath.mnt);
1143 if (upperpath.dentry) {
1144 oe->has_upper = true;
1145 if (ovl_is_impuredir(upperpath.dentry))
1146 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1148 for (i = 0; i < numlower; i++) {
1149 oe->lowerstack[i].dentry = stack[i].dentry;
1150 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1154 root_dentry->d_fsdata = oe;
1156 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1157 ovl_dentry_lower(root_dentry));
1159 sb->s_root = root_dentry;
1166 put_cred(ufs->creator_cred);
1168 dput(ufs->indexdir);
1170 for (i = 0; i < ufs->numlower; i++)
1171 mntput(ufs->lower_mnt[i]);
1172 kfree(ufs->lower_mnt);
1175 mntput(ufs->upper_mnt);
1177 for (i = 0; i < numlower; i++)
1178 path_put(&stack[i]);
1182 out_unlock_workdentry:
1183 if (ufs->workdir_locked)
1184 ovl_inuse_unlock(workpath.dentry);
1186 path_put(&workpath);
1187 out_unlock_upperdentry:
1188 if (ufs->upperdir_locked)
1189 ovl_inuse_unlock(upperpath.dentry);
1191 path_put(&upperpath);
1193 kfree(ufs->config.lowerdir);
1194 kfree(ufs->config.upperdir);
1195 kfree(ufs->config.workdir);
1201 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1202 const char *dev_name, void *raw_data)
1204 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1207 static struct file_system_type ovl_fs_type = {
1208 .owner = THIS_MODULE,
1211 .kill_sb = kill_anon_super,
1213 MODULE_ALIAS_FS("overlay");
1215 static void ovl_inode_init_once(void *foo)
1217 struct ovl_inode *oi = foo;
1219 inode_init_once(&oi->vfs_inode);
1222 static int __init ovl_init(void)
1226 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1227 sizeof(struct ovl_inode), 0,
1228 (SLAB_RECLAIM_ACCOUNT|
1229 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1230 ovl_inode_init_once);
1231 if (ovl_inode_cachep == NULL)
1234 err = register_filesystem(&ovl_fs_type);
1236 kmem_cache_destroy(ovl_inode_cachep);
1241 static void __exit ovl_exit(void)
1243 unregister_filesystem(&ovl_fs_type);
1246 * Make sure all delayed rcu free inodes are flushed before we
1250 kmem_cache_destroy(ovl_inode_cachep);
1254 module_init(ovl_init);
1255 module_exit(ovl_exit);