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_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
37 module_param_named(index, ovl_index_def, bool, 0644);
38 MODULE_PARM_DESC(ovl_index_def,
39 "Default to on or off for the inodes index feature");
41 static void ovl_entry_stack_free(struct ovl_entry *oe)
45 for (i = 0; i < oe->numlower; i++)
46 dput(oe->lowerstack[i].dentry);
49 static void ovl_dentry_release(struct dentry *dentry)
51 struct ovl_entry *oe = dentry->d_fsdata;
54 ovl_entry_stack_free(oe);
59 static int ovl_check_append_only(struct inode *inode, int flag)
62 * This test was moot in vfs may_open() because overlay inode does
63 * not have the S_APPEND flag, so re-check on real upper inode
65 if (IS_APPEND(inode)) {
66 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
75 static struct dentry *ovl_d_real(struct dentry *dentry,
76 const struct inode *inode,
77 unsigned int open_flags, unsigned int flags)
82 if (flags & D_REAL_UPPER)
83 return ovl_dentry_upper(dentry);
85 if (!d_is_reg(dentry)) {
86 if (!inode || inode == d_inode(dentry))
92 err = ovl_open_maybe_copy_up(dentry, open_flags);
97 real = ovl_dentry_upper(dentry);
98 if (real && (!inode || inode == d_inode(real))) {
100 err = ovl_check_append_only(d_inode(real), open_flags);
107 real = ovl_dentry_lower(dentry);
111 /* Handle recursion */
112 real = d_real(real, inode, open_flags, 0);
114 if (!inode || inode == d_inode(real))
117 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
118 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
122 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
124 struct ovl_entry *oe = dentry->d_fsdata;
128 for (i = 0; i < oe->numlower; i++) {
129 struct dentry *d = oe->lowerstack[i].dentry;
131 if (d->d_flags & DCACHE_OP_REVALIDATE) {
132 ret = d->d_op->d_revalidate(d, flags);
136 if (!(flags & LOOKUP_RCU))
145 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
147 struct ovl_entry *oe = dentry->d_fsdata;
151 for (i = 0; i < oe->numlower; i++) {
152 struct dentry *d = oe->lowerstack[i].dentry;
154 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
155 ret = d->d_op->d_weak_revalidate(d, flags);
163 static const struct dentry_operations ovl_dentry_operations = {
164 .d_release = ovl_dentry_release,
165 .d_real = ovl_d_real,
168 static const struct dentry_operations ovl_reval_dentry_operations = {
169 .d_release = ovl_dentry_release,
170 .d_real = ovl_d_real,
171 .d_revalidate = ovl_dentry_revalidate,
172 .d_weak_revalidate = ovl_dentry_weak_revalidate,
175 static struct kmem_cache *ovl_inode_cachep;
177 static struct inode *ovl_alloc_inode(struct super_block *sb)
179 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
188 oi->__upperdentry = NULL;
190 mutex_init(&oi->lock);
192 return &oi->vfs_inode;
195 static void ovl_i_callback(struct rcu_head *head)
197 struct inode *inode = container_of(head, struct inode, i_rcu);
199 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
202 static void ovl_destroy_inode(struct inode *inode)
204 struct ovl_inode *oi = OVL_I(inode);
206 dput(oi->__upperdentry);
208 ovl_dir_cache_free(inode);
209 mutex_destroy(&oi->lock);
211 call_rcu(&inode->i_rcu, ovl_i_callback);
214 static void ovl_free_fs(struct ovl_fs *ofs)
220 if (ofs->workdir_locked)
221 ovl_inuse_unlock(ofs->workbasedir);
222 dput(ofs->workbasedir);
223 if (ofs->upperdir_locked)
224 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
225 mntput(ofs->upper_mnt);
226 for (i = 0; i < ofs->numlower; i++) {
227 mntput(ofs->lower_layers[i].mnt);
228 free_anon_bdev(ofs->lower_layers[i].pseudo_dev);
230 kfree(ofs->lower_layers);
232 kfree(ofs->config.lowerdir);
233 kfree(ofs->config.upperdir);
234 kfree(ofs->config.workdir);
235 if (ofs->creator_cred)
236 put_cred(ofs->creator_cred);
240 static void ovl_put_super(struct super_block *sb)
242 struct ovl_fs *ofs = sb->s_fs_info;
247 static int ovl_sync_fs(struct super_block *sb, int wait)
249 struct ovl_fs *ofs = sb->s_fs_info;
250 struct super_block *upper_sb;
255 upper_sb = ofs->upper_mnt->mnt_sb;
256 if (!upper_sb->s_op->sync_fs)
259 /* real inodes have already been synced by sync_filesystem(ovl_sb) */
260 down_read(&upper_sb->s_umount);
261 ret = upper_sb->s_op->sync_fs(upper_sb, wait);
262 up_read(&upper_sb->s_umount);
268 * @sb: The overlayfs super block
269 * @buf: The struct kstatfs to fill in with stats
271 * Get the filesystem statistics. As writes always target the upper layer
272 * filesystem pass the statfs to the upper filesystem (if it exists)
274 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
276 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
277 struct dentry *root_dentry = dentry->d_sb->s_root;
281 ovl_path_real(root_dentry, &path);
283 err = vfs_statfs(&path, buf);
285 buf->f_namelen = ofs->namelen;
286 buf->f_type = OVERLAYFS_SUPER_MAGIC;
292 /* Will this overlay be forced to mount/remount ro? */
293 static bool ovl_force_readonly(struct ovl_fs *ofs)
295 return (!ofs->upper_mnt || !ofs->workdir);
301 * Prints the mount options for a given superblock.
302 * Returns zero; does not fail.
304 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
306 struct super_block *sb = dentry->d_sb;
307 struct ovl_fs *ofs = sb->s_fs_info;
309 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
310 if (ofs->config.upperdir) {
311 seq_show_option(m, "upperdir", ofs->config.upperdir);
312 seq_show_option(m, "workdir", ofs->config.workdir);
314 if (ofs->config.default_permissions)
315 seq_puts(m, ",default_permissions");
316 if (ofs->config.redirect_dir != ovl_redirect_dir_def)
317 seq_printf(m, ",redirect_dir=%s",
318 ofs->config.redirect_dir ? "on" : "off");
319 if (ofs->config.index != ovl_index_def)
320 seq_printf(m, ",index=%s",
321 ofs->config.index ? "on" : "off");
325 static int ovl_remount(struct super_block *sb, int *flags, char *data)
327 struct ovl_fs *ofs = sb->s_fs_info;
329 if (!(*flags & MS_RDONLY) && ovl_force_readonly(ofs))
335 static const struct super_operations ovl_super_operations = {
336 .alloc_inode = ovl_alloc_inode,
337 .destroy_inode = ovl_destroy_inode,
338 .drop_inode = generic_delete_inode,
339 .put_super = ovl_put_super,
340 .sync_fs = ovl_sync_fs,
341 .statfs = ovl_statfs,
342 .show_options = ovl_show_options,
343 .remount_fs = ovl_remount,
350 OPT_DEFAULT_PERMISSIONS,
352 OPT_REDIRECT_DIR_OFF,
358 static const match_table_t ovl_tokens = {
359 {OPT_LOWERDIR, "lowerdir=%s"},
360 {OPT_UPPERDIR, "upperdir=%s"},
361 {OPT_WORKDIR, "workdir=%s"},
362 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
363 {OPT_REDIRECT_DIR_ON, "redirect_dir=on"},
364 {OPT_REDIRECT_DIR_OFF, "redirect_dir=off"},
365 {OPT_INDEX_ON, "index=on"},
366 {OPT_INDEX_OFF, "index=off"},
370 static char *ovl_next_opt(char **s)
378 for (p = sbegin; *p; p++) {
383 } else if (*p == ',') {
393 static int ovl_parse_opt(char *opt, struct ovl_config *config)
397 while ((p = ovl_next_opt(&opt)) != NULL) {
399 substring_t args[MAX_OPT_ARGS];
404 token = match_token(p, ovl_tokens, args);
407 kfree(config->upperdir);
408 config->upperdir = match_strdup(&args[0]);
409 if (!config->upperdir)
414 kfree(config->lowerdir);
415 config->lowerdir = match_strdup(&args[0]);
416 if (!config->lowerdir)
421 kfree(config->workdir);
422 config->workdir = match_strdup(&args[0]);
423 if (!config->workdir)
427 case OPT_DEFAULT_PERMISSIONS:
428 config->default_permissions = true;
431 case OPT_REDIRECT_DIR_ON:
432 config->redirect_dir = true;
435 case OPT_REDIRECT_DIR_OFF:
436 config->redirect_dir = false;
440 config->index = true;
444 config->index = false;
448 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
453 /* Workdir is useless in non-upper mount */
454 if (!config->upperdir && config->workdir) {
455 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
457 kfree(config->workdir);
458 config->workdir = NULL;
464 #define OVL_WORKDIR_NAME "work"
465 #define OVL_INDEXDIR_NAME "index"
467 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
468 const char *name, bool persist)
470 struct inode *dir = ofs->workbasedir->d_inode;
471 struct vfsmount *mnt = ofs->upper_mnt;
474 bool retried = false;
477 err = mnt_want_write(mnt);
481 inode_lock_nested(dir, I_MUTEX_PARENT);
485 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
488 struct iattr attr = {
489 .ia_valid = ATTR_MODE,
490 .ia_mode = S_IFDIR | 0,
502 ovl_workdir_cleanup(dir, mnt, work, 0);
507 err = ovl_create_real(dir, work,
508 &(struct cattr){.mode = S_IFDIR | 0},
514 * Try to remove POSIX ACL xattrs from workdir. We are good if:
516 * a) success (there was a POSIX ACL xattr and was removed)
517 * b) -ENODATA (there was no POSIX ACL xattr)
518 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
520 * There are various other error values that could effectively
521 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
522 * if the xattr name is too long), but the set of filesystems
523 * allowed as upper are limited to "normal" ones, where checking
524 * for the above two errors is sufficient.
526 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
527 if (err && err != -ENODATA && err != -EOPNOTSUPP)
530 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
531 if (err && err != -ENODATA && err != -EOPNOTSUPP)
534 /* Clear any inherited mode bits */
535 inode_lock(work->d_inode);
536 err = notify_change(work, &attr, NULL);
537 inode_unlock(work->d_inode);
554 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
555 ofs->config.workdir, name, -err);
560 static void ovl_unescape(char *s)
573 static int ovl_mount_dir_noesc(const char *name, struct path *path)
578 pr_err("overlayfs: empty lowerdir\n");
581 err = kern_path(name, LOOKUP_FOLLOW, path);
583 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
587 if (ovl_dentry_weird(path->dentry)) {
588 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
591 if (!d_is_dir(path->dentry)) {
592 pr_err("overlayfs: '%s' not a directory\n", name);
603 static int ovl_mount_dir(const char *name, struct path *path)
606 char *tmp = kstrdup(name, GFP_KERNEL);
610 err = ovl_mount_dir_noesc(tmp, path);
613 if (ovl_dentry_remote(path->dentry)) {
614 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
624 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
627 struct kstatfs statfs;
628 int err = vfs_statfs(path, &statfs);
631 pr_err("overlayfs: statfs failed on '%s'\n", name);
633 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
638 static int ovl_lower_dir(const char *name, struct path *path,
639 struct ovl_fs *ofs, int *stack_depth, bool *remote)
643 err = ovl_mount_dir_noesc(name, path);
647 err = ovl_check_namelen(path, ofs, name);
651 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
653 if (ovl_dentry_remote(path->dentry))
657 * The inodes index feature needs to encode and decode file
658 * handles, so it requires that all layers support them.
660 if (ofs->config.index && !ovl_can_decode_fh(path->dentry->d_sb)) {
661 ofs->config.index = false;
662 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off.\n", name);
673 /* Workdir should not be subdir of upperdir and vice versa */
674 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
678 if (workdir != upperdir) {
679 ok = (lock_rename(workdir, upperdir) == NULL);
680 unlock_rename(workdir, upperdir);
685 static unsigned int ovl_split_lowerdirs(char *str)
687 unsigned int ctr = 1;
690 for (s = d = str;; s++, d++) {
693 } else if (*s == ':') {
705 static int __maybe_unused
706 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
707 struct dentry *dentry, struct inode *inode,
708 const char *name, void *buffer, size_t size)
710 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
713 static int __maybe_unused
714 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
715 struct dentry *dentry, struct inode *inode,
716 const char *name, const void *value,
717 size_t size, int flags)
719 struct dentry *workdir = ovl_workdir(dentry);
720 struct inode *realinode = ovl_inode_real(inode);
721 struct posix_acl *acl = NULL;
724 /* Check that everything is OK before copy-up */
726 acl = posix_acl_from_xattr(&init_user_ns, value, size);
731 if (!IS_POSIXACL(d_inode(workdir)))
732 goto out_acl_release;
733 if (!realinode->i_op->set_acl)
734 goto out_acl_release;
735 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
736 err = acl ? -EACCES : 0;
737 goto out_acl_release;
740 if (!inode_owner_or_capable(inode))
741 goto out_acl_release;
743 posix_acl_release(acl);
746 * Check if sgid bit needs to be cleared (actual setacl operation will
747 * be done with mounter's capabilities and so that won't do it for us).
749 if (unlikely(inode->i_mode & S_ISGID) &&
750 handler->flags == ACL_TYPE_ACCESS &&
751 !in_group_p(inode->i_gid) &&
752 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
753 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
755 err = ovl_setattr(dentry, &iattr);
760 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
762 ovl_copyattr(ovl_inode_real(inode), inode);
767 posix_acl_release(acl);
771 static int ovl_own_xattr_get(const struct xattr_handler *handler,
772 struct dentry *dentry, struct inode *inode,
773 const char *name, void *buffer, size_t size)
778 static int ovl_own_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)
786 static int ovl_other_xattr_get(const struct xattr_handler *handler,
787 struct dentry *dentry, struct inode *inode,
788 const char *name, void *buffer, size_t size)
790 return ovl_xattr_get(dentry, inode, name, buffer, size);
793 static int ovl_other_xattr_set(const struct xattr_handler *handler,
794 struct dentry *dentry, struct inode *inode,
795 const char *name, const void *value,
796 size_t size, int flags)
798 return ovl_xattr_set(dentry, inode, name, value, size, flags);
801 static const struct xattr_handler __maybe_unused
802 ovl_posix_acl_access_xattr_handler = {
803 .name = XATTR_NAME_POSIX_ACL_ACCESS,
804 .flags = ACL_TYPE_ACCESS,
805 .get = ovl_posix_acl_xattr_get,
806 .set = ovl_posix_acl_xattr_set,
809 static const struct xattr_handler __maybe_unused
810 ovl_posix_acl_default_xattr_handler = {
811 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
812 .flags = ACL_TYPE_DEFAULT,
813 .get = ovl_posix_acl_xattr_get,
814 .set = ovl_posix_acl_xattr_set,
817 static const struct xattr_handler ovl_own_xattr_handler = {
818 .prefix = OVL_XATTR_PREFIX,
819 .get = ovl_own_xattr_get,
820 .set = ovl_own_xattr_set,
823 static const struct xattr_handler ovl_other_xattr_handler = {
824 .prefix = "", /* catch all */
825 .get = ovl_other_xattr_get,
826 .set = ovl_other_xattr_set,
829 static const struct xattr_handler *ovl_xattr_handlers[] = {
830 #ifdef CONFIG_FS_POSIX_ACL
831 &ovl_posix_acl_access_xattr_handler,
832 &ovl_posix_acl_default_xattr_handler,
834 &ovl_own_xattr_handler,
835 &ovl_other_xattr_handler,
839 static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
841 struct vfsmount *upper_mnt;
844 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
848 /* Upper fs should not be r/o */
849 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
850 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
855 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
860 if (ovl_inuse_trylock(upperpath->dentry)) {
861 ofs->upperdir_locked = true;
862 } else if (ofs->config.index) {
863 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
866 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
869 upper_mnt = clone_private_mount(upperpath);
870 err = PTR_ERR(upper_mnt);
871 if (IS_ERR(upper_mnt)) {
872 pr_err("overlayfs: failed to clone upperpath\n");
876 /* Don't inherit atime flags */
877 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
878 ofs->upper_mnt = upper_mnt;
884 static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
889 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
894 * Upper should support d_type, else whiteouts are visible. Given
895 * workdir and upper are on same fs, we can do iterate_dir() on
896 * workdir. This check requires successful creation of workdir in
899 err = ovl_check_d_type_supported(workpath);
904 * We allowed this configuration and don't want to break users over
905 * kernel upgrade. So warn instead of erroring out.
908 pr_warn("overlayfs: upper fs needs to support d_type.\n");
910 /* Check if upper/work fs supports O_TMPFILE */
911 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
912 ofs->tmpfile = !IS_ERR(temp);
916 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
919 * Check if upper/work fs supports trusted.overlay.* xattr
921 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
924 pr_warn("overlayfs: upper fs does not support xattr.\n");
926 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
929 /* Check if upper/work fs supports file handles */
930 if (ofs->config.index &&
931 !ovl_can_decode_fh(ofs->workdir->d_sb)) {
932 ofs->config.index = false;
933 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
939 static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
942 struct path workpath = { };
944 err = ovl_mount_dir(ofs->config.workdir, &workpath);
949 if (upperpath->mnt != workpath.mnt) {
950 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
953 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
954 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
959 if (ovl_inuse_trylock(workpath.dentry)) {
960 ofs->workdir_locked = true;
961 } else if (ofs->config.index) {
962 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
965 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
968 ofs->workbasedir = dget(workpath.dentry);
969 err = ovl_make_workdir(ofs, &workpath);
980 static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
981 struct path *upperpath)
985 /* Verify lower root is upper root origin */
986 err = ovl_verify_origin(upperpath->dentry,
987 oe->lowerstack[0].layer->mnt,
988 oe->lowerstack[0].dentry,
991 pr_err("overlayfs: failed to verify upper root origin\n");
995 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
997 /* Verify upper root is index dir origin */
998 err = ovl_verify_origin(ofs->indexdir, ofs->upper_mnt,
999 upperpath->dentry, true, true);
1001 pr_err("overlayfs: failed to verify index dir origin\n");
1003 /* Cleanup bad/stale/orphan index entries */
1005 err = ovl_indexdir_cleanup(ofs->indexdir,
1010 if (err || !ofs->indexdir)
1011 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1017 static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
1018 unsigned int numlower)
1024 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1026 if (ofs->lower_layers == NULL)
1028 for (i = 0; i < numlower; i++) {
1029 struct vfsmount *mnt;
1032 err = get_anon_bdev(&dev);
1034 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1038 mnt = clone_private_mount(&stack[i]);
1041 pr_err("overlayfs: failed to clone lowerpath\n");
1042 free_anon_bdev(dev);
1046 * Make lower layers R/O. That way fchmod/fchown on lower file
1047 * will fail instead of modifying lower fs.
1049 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1051 ofs->lower_layers[ofs->numlower].mnt = mnt;
1052 ofs->lower_layers[ofs->numlower].pseudo_dev = dev;
1055 /* Check if all lower layers are on same sb */
1057 ofs->same_sb = mnt->mnt_sb;
1058 else if (ofs->same_sb != mnt->mnt_sb)
1059 ofs->same_sb = NULL;
1066 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1070 char *lowertmp, *lower;
1071 struct path *stack = NULL;
1072 unsigned int stacklen, numlower = 0, i;
1073 bool remote = false;
1074 struct ovl_entry *oe;
1077 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1082 stacklen = ovl_split_lowerdirs(lowertmp);
1083 if (stacklen > OVL_MAX_STACK) {
1084 pr_err("overlayfs: too many lower directories, limit is %d\n",
1087 } else if (!ofs->config.upperdir && stacklen == 1) {
1088 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1093 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1099 for (numlower = 0; numlower < stacklen; numlower++) {
1100 err = ovl_lower_dir(lower, &stack[numlower], ofs,
1101 &sb->s_stack_depth, &remote);
1105 lower = strchr(lower, '\0') + 1;
1109 sb->s_stack_depth++;
1110 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1111 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1115 err = ovl_get_lower_layers(ofs, stack, numlower);
1120 oe = ovl_alloc_entry(numlower);
1124 for (i = 0; i < numlower; i++) {
1125 oe->lowerstack[i].dentry = dget(stack[i].dentry);
1126 oe->lowerstack[i].layer = &ofs->lower_layers[i];
1130 sb->s_d_op = &ovl_reval_dentry_operations;
1132 sb->s_d_op = &ovl_dentry_operations;
1135 for (i = 0; i < numlower; i++)
1136 path_put(&stack[i]);
1147 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1149 struct path upperpath = { };
1150 struct dentry *root_dentry;
1151 struct ovl_entry *oe;
1157 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1161 ofs->creator_cred = cred = prepare_creds();
1165 ofs->config.redirect_dir = ovl_redirect_dir_def;
1166 ofs->config.index = ovl_index_def;
1167 err = ovl_parse_opt((char *) data, &ofs->config);
1172 if (!ofs->config.lowerdir) {
1174 pr_err("overlayfs: missing 'lowerdir'\n");
1178 sb->s_stack_depth = 0;
1179 sb->s_maxbytes = MAX_LFS_FILESIZE;
1180 if (ofs->config.upperdir) {
1181 if (!ofs->config.workdir) {
1182 pr_err("overlayfs: missing 'workdir'\n");
1186 err = ovl_get_upper(ofs, &upperpath);
1190 err = ovl_get_workdir(ofs, &upperpath);
1195 sb->s_flags |= MS_RDONLY;
1197 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1198 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
1201 oe = ovl_get_lowerstack(sb, ofs);
1206 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1207 if (!ofs->upper_mnt)
1208 sb->s_flags |= MS_RDONLY;
1209 else if (ofs->upper_mnt->mnt_sb != ofs->same_sb)
1210 ofs->same_sb = NULL;
1212 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1213 err = ovl_get_indexdir(ofs, oe, &upperpath);
1218 sb->s_flags |= MS_RDONLY;
1221 /* Show index=off/on in /proc/mounts for any of the reasons above */
1223 ofs->config.index = false;
1225 /* Never override disk quota limits or use reserved space */
1226 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1228 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1229 sb->s_op = &ovl_super_operations;
1230 sb->s_xattr = ovl_xattr_handlers;
1231 sb->s_fs_info = ofs;
1232 sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK;
1235 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1239 mntput(upperpath.mnt);
1240 if (upperpath.dentry) {
1241 oe->has_upper = true;
1242 if (ovl_is_impuredir(upperpath.dentry))
1243 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1246 root_dentry->d_fsdata = oe;
1248 /* Root is always merge -> can have whiteouts */
1249 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
1250 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1251 ovl_dentry_lower(root_dentry));
1253 sb->s_root = root_dentry;
1258 ovl_entry_stack_free(oe);
1261 path_put(&upperpath);
1267 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1268 const char *dev_name, void *raw_data)
1270 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1273 static struct file_system_type ovl_fs_type = {
1274 .owner = THIS_MODULE,
1277 .kill_sb = kill_anon_super,
1279 MODULE_ALIAS_FS("overlay");
1281 static void ovl_inode_init_once(void *foo)
1283 struct ovl_inode *oi = foo;
1285 inode_init_once(&oi->vfs_inode);
1288 static int __init ovl_init(void)
1292 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1293 sizeof(struct ovl_inode), 0,
1294 (SLAB_RECLAIM_ACCOUNT|
1295 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1296 ovl_inode_init_once);
1297 if (ovl_inode_cachep == NULL)
1300 err = register_filesystem(&ovl_fs_type);
1302 kmem_cache_destroy(ovl_inode_cachep);
1307 static void __exit ovl_exit(void)
1309 unregister_filesystem(&ovl_fs_type);
1312 * Make sure all delayed rcu free inodes are flushed before we
1316 kmem_cache_destroy(ovl_inode_cachep);
1320 module_init(ovl_init);
1321 module_exit(ovl_exit);