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 <linux/exportfs.h>
21 #include "overlayfs.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_redirect_always_follow =
38 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
39 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
41 MODULE_PARM_DESC(ovl_redirect_always_follow,
42 "Follow redirects even if redirect_dir feature is turned off");
44 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
45 module_param_named(index, ovl_index_def, bool, 0644);
46 MODULE_PARM_DESC(ovl_index_def,
47 "Default to on or off for the inodes index feature");
49 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
50 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
51 MODULE_PARM_DESC(ovl_nfs_export_def,
52 "Default to on or off for the NFS export feature");
54 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
55 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
56 MODULE_PARM_DESC(ovl_xino_auto_def,
57 "Auto enable xino feature");
59 static void ovl_entry_stack_free(struct ovl_entry *oe)
63 for (i = 0; i < oe->numlower; i++)
64 dput(oe->lowerstack[i].dentry);
67 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
68 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
69 MODULE_PARM_DESC(ovl_metacopy_def,
70 "Default to on or off for the metadata only copy up feature");
72 static void ovl_dentry_release(struct dentry *dentry)
74 struct ovl_entry *oe = dentry->d_fsdata;
77 ovl_entry_stack_free(oe);
82 static struct dentry *ovl_d_real(struct dentry *dentry,
83 const struct inode *inode)
87 /* It's an overlay file */
88 if (inode && d_inode(dentry) == inode)
91 if (!d_is_reg(dentry)) {
92 if (!inode || inode == d_inode(dentry))
97 real = ovl_dentry_upper(dentry);
98 if (real && (inode == d_inode(real)))
101 if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
104 real = ovl_dentry_lowerdata(dentry);
108 /* Handle recursion */
109 real = d_real(real, inode);
111 if (!inode || inode == d_inode(real))
114 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
115 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
119 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
121 struct ovl_entry *oe = dentry->d_fsdata;
125 for (i = 0; i < oe->numlower; i++) {
126 struct dentry *d = oe->lowerstack[i].dentry;
128 if (d->d_flags & DCACHE_OP_REVALIDATE) {
129 ret = d->d_op->d_revalidate(d, flags);
133 if (!(flags & LOOKUP_RCU))
142 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
144 struct ovl_entry *oe = dentry->d_fsdata;
148 for (i = 0; i < oe->numlower; i++) {
149 struct dentry *d = oe->lowerstack[i].dentry;
151 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
152 ret = d->d_op->d_weak_revalidate(d, flags);
160 static const struct dentry_operations ovl_dentry_operations = {
161 .d_release = ovl_dentry_release,
162 .d_real = ovl_d_real,
165 static const struct dentry_operations ovl_reval_dentry_operations = {
166 .d_release = ovl_dentry_release,
167 .d_real = ovl_d_real,
168 .d_revalidate = ovl_dentry_revalidate,
169 .d_weak_revalidate = ovl_dentry_weak_revalidate,
172 static struct kmem_cache *ovl_inode_cachep;
174 static struct inode *ovl_alloc_inode(struct super_block *sb)
176 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
185 oi->__upperdentry = NULL;
187 oi->lowerdata = NULL;
188 mutex_init(&oi->lock);
190 return &oi->vfs_inode;
193 static void ovl_i_callback(struct rcu_head *head)
195 struct inode *inode = container_of(head, struct inode, i_rcu);
197 kmem_cache_free(ovl_inode_cachep, OVL_I(inode));
200 static void ovl_destroy_inode(struct inode *inode)
202 struct ovl_inode *oi = OVL_I(inode);
204 dput(oi->__upperdentry);
206 if (S_ISDIR(inode->i_mode))
207 ovl_dir_cache_free(inode);
211 mutex_destroy(&oi->lock);
213 call_rcu(&inode->i_rcu, ovl_i_callback);
216 static void ovl_free_fs(struct ovl_fs *ofs)
222 if (ofs->workdir_locked)
223 ovl_inuse_unlock(ofs->workbasedir);
224 dput(ofs->workbasedir);
225 if (ofs->upperdir_locked)
226 ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
227 mntput(ofs->upper_mnt);
228 for (i = 0; i < ofs->numlower; i++)
229 mntput(ofs->lower_layers[i].mnt);
230 for (i = 0; i < ofs->numlowerfs; i++)
231 free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
232 kfree(ofs->lower_layers);
233 kfree(ofs->lower_fs);
235 kfree(ofs->config.lowerdir);
236 kfree(ofs->config.upperdir);
237 kfree(ofs->config.workdir);
238 kfree(ofs->config.redirect_mode);
239 if (ofs->creator_cred)
240 put_cred(ofs->creator_cred);
244 static void ovl_put_super(struct super_block *sb)
246 struct ovl_fs *ofs = sb->s_fs_info;
251 /* Sync real dirty inodes in upper filesystem (if it exists) */
252 static int ovl_sync_fs(struct super_block *sb, int wait)
254 struct ovl_fs *ofs = sb->s_fs_info;
255 struct super_block *upper_sb;
262 * If this is a sync(2) call or an emergency sync, all the super blocks
263 * will be iterated, including upper_sb, so no need to do anything.
265 * If this is a syncfs(2) call, then we do need to call
266 * sync_filesystem() on upper_sb, but enough if we do it when being
267 * called with wait == 1.
272 upper_sb = ofs->upper_mnt->mnt_sb;
274 down_read(&upper_sb->s_umount);
275 ret = sync_filesystem(upper_sb);
276 up_read(&upper_sb->s_umount);
283 * @sb: The overlayfs super block
284 * @buf: The struct kstatfs to fill in with stats
286 * Get the filesystem statistics. As writes always target the upper layer
287 * filesystem pass the statfs to the upper filesystem (if it exists)
289 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
291 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
292 struct dentry *root_dentry = dentry->d_sb->s_root;
296 ovl_path_real(root_dentry, &path);
298 err = vfs_statfs(&path, buf);
300 buf->f_namelen = ofs->namelen;
301 buf->f_type = OVERLAYFS_SUPER_MAGIC;
307 /* Will this overlay be forced to mount/remount ro? */
308 static bool ovl_force_readonly(struct ovl_fs *ofs)
310 return (!ofs->upper_mnt || !ofs->workdir);
313 static const char *ovl_redirect_mode_def(void)
315 return ovl_redirect_dir_def ? "on" : "off";
324 static const char * const ovl_xino_str[] = {
330 static inline int ovl_xino_def(void)
332 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
338 * Prints the mount options for a given superblock.
339 * Returns zero; does not fail.
341 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
343 struct super_block *sb = dentry->d_sb;
344 struct ovl_fs *ofs = sb->s_fs_info;
346 seq_show_option(m, "lowerdir", ofs->config.lowerdir);
347 if (ofs->config.upperdir) {
348 seq_show_option(m, "upperdir", ofs->config.upperdir);
349 seq_show_option(m, "workdir", ofs->config.workdir);
351 if (ofs->config.default_permissions)
352 seq_puts(m, ",default_permissions");
353 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
354 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
355 if (ofs->config.index != ovl_index_def)
356 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
357 if (ofs->config.nfs_export != ovl_nfs_export_def)
358 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
360 if (ofs->config.xino != ovl_xino_def())
361 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
362 if (ofs->config.metacopy != ovl_metacopy_def)
363 seq_printf(m, ",metacopy=%s",
364 ofs->config.metacopy ? "on" : "off");
368 static int ovl_remount(struct super_block *sb, int *flags, char *data)
370 struct ovl_fs *ofs = sb->s_fs_info;
372 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
378 static const struct super_operations ovl_super_operations = {
379 .alloc_inode = ovl_alloc_inode,
380 .destroy_inode = ovl_destroy_inode,
381 .drop_inode = generic_delete_inode,
382 .put_super = ovl_put_super,
383 .sync_fs = ovl_sync_fs,
384 .statfs = ovl_statfs,
385 .show_options = ovl_show_options,
386 .remount_fs = ovl_remount,
393 OPT_DEFAULT_PERMISSIONS,
407 static const match_table_t ovl_tokens = {
408 {OPT_LOWERDIR, "lowerdir=%s"},
409 {OPT_UPPERDIR, "upperdir=%s"},
410 {OPT_WORKDIR, "workdir=%s"},
411 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
412 {OPT_REDIRECT_DIR, "redirect_dir=%s"},
413 {OPT_INDEX_ON, "index=on"},
414 {OPT_INDEX_OFF, "index=off"},
415 {OPT_NFS_EXPORT_ON, "nfs_export=on"},
416 {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
417 {OPT_XINO_ON, "xino=on"},
418 {OPT_XINO_OFF, "xino=off"},
419 {OPT_XINO_AUTO, "xino=auto"},
420 {OPT_METACOPY_ON, "metacopy=on"},
421 {OPT_METACOPY_OFF, "metacopy=off"},
425 static char *ovl_next_opt(char **s)
433 for (p = sbegin; *p; p++) {
438 } else if (*p == ',') {
448 static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
450 if (strcmp(mode, "on") == 0) {
451 config->redirect_dir = true;
453 * Does not make sense to have redirect creation without
454 * redirect following.
456 config->redirect_follow = true;
457 } else if (strcmp(mode, "follow") == 0) {
458 config->redirect_follow = true;
459 } else if (strcmp(mode, "off") == 0) {
460 if (ovl_redirect_always_follow)
461 config->redirect_follow = true;
462 } else if (strcmp(mode, "nofollow") != 0) {
463 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
471 static int ovl_parse_opt(char *opt, struct ovl_config *config)
475 bool metacopy_opt = false, redirect_opt = false;
477 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
478 if (!config->redirect_mode)
481 while ((p = ovl_next_opt(&opt)) != NULL) {
483 substring_t args[MAX_OPT_ARGS];
488 token = match_token(p, ovl_tokens, args);
491 kfree(config->upperdir);
492 config->upperdir = match_strdup(&args[0]);
493 if (!config->upperdir)
498 kfree(config->lowerdir);
499 config->lowerdir = match_strdup(&args[0]);
500 if (!config->lowerdir)
505 kfree(config->workdir);
506 config->workdir = match_strdup(&args[0]);
507 if (!config->workdir)
511 case OPT_DEFAULT_PERMISSIONS:
512 config->default_permissions = true;
515 case OPT_REDIRECT_DIR:
516 kfree(config->redirect_mode);
517 config->redirect_mode = match_strdup(&args[0]);
518 if (!config->redirect_mode)
524 config->index = true;
528 config->index = false;
531 case OPT_NFS_EXPORT_ON:
532 config->nfs_export = true;
535 case OPT_NFS_EXPORT_OFF:
536 config->nfs_export = false;
540 config->xino = OVL_XINO_ON;
544 config->xino = OVL_XINO_OFF;
548 config->xino = OVL_XINO_AUTO;
551 case OPT_METACOPY_ON:
552 config->metacopy = true;
556 case OPT_METACOPY_OFF:
557 config->metacopy = false;
561 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
566 /* Workdir is useless in non-upper mount */
567 if (!config->upperdir && config->workdir) {
568 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
570 kfree(config->workdir);
571 config->workdir = NULL;
574 err = ovl_parse_redirect_mode(config, config->redirect_mode);
579 * This is to make the logic below simpler. It doesn't make any other
580 * difference, since config->redirect_dir is only used for upper.
582 if (!config->upperdir && config->redirect_follow)
583 config->redirect_dir = true;
585 /* Resolve metacopy -> redirect_dir dependency */
586 if (config->metacopy && !config->redirect_dir) {
587 if (metacopy_opt && redirect_opt) {
588 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
589 config->redirect_mode);
594 * There was an explicit redirect_dir=... that resulted
597 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
598 config->redirect_mode);
599 config->metacopy = false;
601 /* Automatically enable redirect otherwise. */
602 config->redirect_follow = config->redirect_dir = true;
609 #define OVL_WORKDIR_NAME "work"
610 #define OVL_INDEXDIR_NAME "index"
612 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
613 const char *name, bool persist)
615 struct inode *dir = ofs->workbasedir->d_inode;
616 struct vfsmount *mnt = ofs->upper_mnt;
619 bool retried = false;
622 inode_lock_nested(dir, I_MUTEX_PARENT);
626 work = lookup_one_len(name, ofs->workbasedir, strlen(name));
629 struct iattr attr = {
630 .ia_valid = ATTR_MODE,
631 .ia_mode = S_IFDIR | 0,
643 ovl_workdir_cleanup(dir, mnt, work, 0);
648 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
654 * Try to remove POSIX ACL xattrs from workdir. We are good if:
656 * a) success (there was a POSIX ACL xattr and was removed)
657 * b) -ENODATA (there was no POSIX ACL xattr)
658 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
660 * There are various other error values that could effectively
661 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
662 * if the xattr name is too long), but the set of filesystems
663 * allowed as upper are limited to "normal" ones, where checking
664 * for the above two errors is sufficient.
666 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
667 if (err && err != -ENODATA && err != -EOPNOTSUPP)
670 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
671 if (err && err != -ENODATA && err != -EOPNOTSUPP)
674 /* Clear any inherited mode bits */
675 inode_lock(work->d_inode);
676 err = notify_change(work, &attr, NULL);
677 inode_unlock(work->d_inode);
693 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
694 ofs->config.workdir, name, -err);
699 static void ovl_unescape(char *s)
712 static int ovl_mount_dir_noesc(const char *name, struct path *path)
717 pr_err("overlayfs: empty lowerdir\n");
720 err = kern_path(name, LOOKUP_FOLLOW, path);
722 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
726 if (ovl_dentry_weird(path->dentry)) {
727 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
730 if (!d_is_dir(path->dentry)) {
731 pr_err("overlayfs: '%s' not a directory\n", name);
742 static int ovl_mount_dir(const char *name, struct path *path)
745 char *tmp = kstrdup(name, GFP_KERNEL);
749 err = ovl_mount_dir_noesc(tmp, path);
752 if (ovl_dentry_remote(path->dentry)) {
753 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
763 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
766 struct kstatfs statfs;
767 int err = vfs_statfs(path, &statfs);
770 pr_err("overlayfs: statfs failed on '%s'\n", name);
772 ofs->namelen = max(ofs->namelen, statfs.f_namelen);
777 static int ovl_lower_dir(const char *name, struct path *path,
778 struct ovl_fs *ofs, int *stack_depth, bool *remote)
783 err = ovl_mount_dir_noesc(name, path);
787 err = ovl_check_namelen(path, ofs, name);
791 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
793 if (ovl_dentry_remote(path->dentry))
797 * The inodes index feature and NFS export need to encode and decode
798 * file handles, so they require that all layers support them.
800 fh_type = ovl_can_decode_fh(path->dentry->d_sb);
801 if ((ofs->config.nfs_export ||
802 (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
803 ofs->config.index = false;
804 ofs->config.nfs_export = false;
805 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
809 /* Check if lower fs has 32bit inode numbers */
810 if (fh_type != FILEID_INO32_GEN)
821 /* Workdir should not be subdir of upperdir and vice versa */
822 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
826 if (workdir != upperdir) {
827 ok = (lock_rename(workdir, upperdir) == NULL);
828 unlock_rename(workdir, upperdir);
833 static unsigned int ovl_split_lowerdirs(char *str)
835 unsigned int ctr = 1;
838 for (s = d = str;; s++, d++) {
841 } else if (*s == ':') {
853 static int __maybe_unused
854 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
855 struct dentry *dentry, struct inode *inode,
856 const char *name, void *buffer, size_t size)
858 return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
861 static int __maybe_unused
862 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
863 struct dentry *dentry, struct inode *inode,
864 const char *name, const void *value,
865 size_t size, int flags)
867 struct dentry *workdir = ovl_workdir(dentry);
868 struct inode *realinode = ovl_inode_real(inode);
869 struct posix_acl *acl = NULL;
872 /* Check that everything is OK before copy-up */
874 acl = posix_acl_from_xattr(&init_user_ns, value, size);
879 if (!IS_POSIXACL(d_inode(workdir)))
880 goto out_acl_release;
881 if (!realinode->i_op->set_acl)
882 goto out_acl_release;
883 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
884 err = acl ? -EACCES : 0;
885 goto out_acl_release;
888 if (!inode_owner_or_capable(inode))
889 goto out_acl_release;
891 posix_acl_release(acl);
894 * Check if sgid bit needs to be cleared (actual setacl operation will
895 * be done with mounter's capabilities and so that won't do it for us).
897 if (unlikely(inode->i_mode & S_ISGID) &&
898 handler->flags == ACL_TYPE_ACCESS &&
899 !in_group_p(inode->i_gid) &&
900 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
901 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
903 err = ovl_setattr(dentry, &iattr);
908 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
910 ovl_copyattr(ovl_inode_real(inode), inode);
915 posix_acl_release(acl);
919 static int ovl_own_xattr_get(const struct xattr_handler *handler,
920 struct dentry *dentry, struct inode *inode,
921 const char *name, void *buffer, size_t size)
926 static int ovl_own_xattr_set(const struct xattr_handler *handler,
927 struct dentry *dentry, struct inode *inode,
928 const char *name, const void *value,
929 size_t size, int flags)
934 static int ovl_other_xattr_get(const struct xattr_handler *handler,
935 struct dentry *dentry, struct inode *inode,
936 const char *name, void *buffer, size_t size)
938 return ovl_xattr_get(dentry, inode, name, buffer, size);
941 static int ovl_other_xattr_set(const struct xattr_handler *handler,
942 struct dentry *dentry, struct inode *inode,
943 const char *name, const void *value,
944 size_t size, int flags)
946 return ovl_xattr_set(dentry, inode, name, value, size, flags);
949 static const struct xattr_handler __maybe_unused
950 ovl_posix_acl_access_xattr_handler = {
951 .name = XATTR_NAME_POSIX_ACL_ACCESS,
952 .flags = ACL_TYPE_ACCESS,
953 .get = ovl_posix_acl_xattr_get,
954 .set = ovl_posix_acl_xattr_set,
957 static const struct xattr_handler __maybe_unused
958 ovl_posix_acl_default_xattr_handler = {
959 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
960 .flags = ACL_TYPE_DEFAULT,
961 .get = ovl_posix_acl_xattr_get,
962 .set = ovl_posix_acl_xattr_set,
965 static const struct xattr_handler ovl_own_xattr_handler = {
966 .prefix = OVL_XATTR_PREFIX,
967 .get = ovl_own_xattr_get,
968 .set = ovl_own_xattr_set,
971 static const struct xattr_handler ovl_other_xattr_handler = {
972 .prefix = "", /* catch all */
973 .get = ovl_other_xattr_get,
974 .set = ovl_other_xattr_set,
977 static const struct xattr_handler *ovl_xattr_handlers[] = {
978 #ifdef CONFIG_FS_POSIX_ACL
979 &ovl_posix_acl_access_xattr_handler,
980 &ovl_posix_acl_default_xattr_handler,
982 &ovl_own_xattr_handler,
983 &ovl_other_xattr_handler,
987 static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
989 struct vfsmount *upper_mnt;
992 err = ovl_mount_dir(ofs->config.upperdir, upperpath);
996 /* Upper fs should not be r/o */
997 if (sb_rdonly(upperpath->mnt->mnt_sb)) {
998 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1003 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1007 upper_mnt = clone_private_mount(upperpath);
1008 err = PTR_ERR(upper_mnt);
1009 if (IS_ERR(upper_mnt)) {
1010 pr_err("overlayfs: failed to clone upperpath\n");
1014 /* Don't inherit atime flags */
1015 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1016 ofs->upper_mnt = upper_mnt;
1019 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1020 ofs->upperdir_locked = true;
1021 } else if (ofs->config.index) {
1022 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
1025 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1033 static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
1035 struct vfsmount *mnt = ofs->upper_mnt;
1036 struct dentry *temp;
1040 err = mnt_want_write(mnt);
1044 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1049 * Upper should support d_type, else whiteouts are visible. Given
1050 * workdir and upper are on same fs, we can do iterate_dir() on
1051 * workdir. This check requires successful creation of workdir in
1054 err = ovl_check_d_type_supported(workpath);
1059 * We allowed this configuration and don't want to break users over
1060 * kernel upgrade. So warn instead of erroring out.
1063 pr_warn("overlayfs: upper fs needs to support d_type.\n");
1065 /* Check if upper/work fs supports O_TMPFILE */
1066 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1067 ofs->tmpfile = !IS_ERR(temp);
1071 pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1074 * Check if upper/work fs supports trusted.overlay.* xattr
1076 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
1078 ofs->noxattr = true;
1079 ofs->config.index = false;
1080 ofs->config.metacopy = false;
1081 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1084 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
1087 /* Check if upper/work fs supports file handles */
1088 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1089 if (ofs->config.index && !fh_type) {
1090 ofs->config.index = false;
1091 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1094 /* Check if upper fs has 32bit inode numbers */
1095 if (fh_type != FILEID_INO32_GEN)
1098 /* NFS export of r/w mount depends on index */
1099 if (ofs->config.nfs_export && !ofs->config.index) {
1100 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1101 ofs->config.nfs_export = false;
1104 mnt_drop_write(mnt);
1108 static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
1111 struct path workpath = { };
1113 err = ovl_mount_dir(ofs->config.workdir, &workpath);
1118 if (upperpath->mnt != workpath.mnt) {
1119 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1122 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1123 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1127 ofs->workbasedir = dget(workpath.dentry);
1130 if (ovl_inuse_trylock(ofs->workbasedir)) {
1131 ofs->workdir_locked = true;
1132 } else if (ofs->config.index) {
1133 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1136 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1139 err = ovl_make_workdir(ofs, &workpath);
1145 path_put(&workpath);
1150 static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
1151 struct path *upperpath)
1153 struct vfsmount *mnt = ofs->upper_mnt;
1156 err = mnt_want_write(mnt);
1160 /* Verify lower root is upper root origin */
1161 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
1164 pr_err("overlayfs: failed to verify upper root origin\n");
1168 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1169 if (ofs->indexdir) {
1171 * Verify upper root is exclusively associated with index dir.
1172 * Older kernels stored upper fh in "trusted.overlay.origin"
1173 * xattr. If that xattr exists, verify that it is a match to
1174 * upper dir file handle. In any case, verify or set xattr
1175 * "trusted.overlay.upper" to indicate that index may have
1176 * directory entries.
1178 if (ovl_check_origin_xattr(ofs->indexdir)) {
1179 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1180 upperpath->dentry, true, false);
1182 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1184 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
1186 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
1188 /* Cleanup bad/stale/orphan index entries */
1190 err = ovl_indexdir_cleanup(ofs);
1192 if (err || !ofs->indexdir)
1193 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1196 mnt_drop_write(mnt);
1200 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1204 if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt))
1207 for (i = 0; i < ofs->numlowerfs; i++) {
1209 * We use uuid to associate an overlay lower file handle with a
1210 * lower layer, so we can accept lower fs with null uuid as long
1211 * as all lower layers with null uuid are on the same fs.
1213 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid))
1219 /* Get a unique fsid for the layer */
1220 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1222 struct super_block *sb = path->mnt->mnt_sb;
1227 /* fsid 0 is reserved for upper fs even with non upper overlay */
1228 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1231 for (i = 0; i < ofs->numlowerfs; i++) {
1232 if (ofs->lower_fs[i].sb == sb)
1236 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1237 ofs->config.index = false;
1238 ofs->config.nfs_export = false;
1239 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1240 uuid_is_null(&sb->s_uuid) ? "null" : "conflicting",
1244 err = get_anon_bdev(&dev);
1246 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1250 ofs->lower_fs[ofs->numlowerfs].sb = sb;
1251 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1254 return ofs->numlowerfs;
1257 static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
1258 unsigned int numlower)
1264 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1266 if (ofs->lower_layers == NULL)
1269 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1271 if (ofs->lower_fs == NULL)
1274 for (i = 0; i < numlower; i++) {
1275 struct vfsmount *mnt;
1278 err = fsid = ovl_get_fsid(ofs, &stack[i]);
1282 mnt = clone_private_mount(&stack[i]);
1285 pr_err("overlayfs: failed to clone lowerpath\n");
1290 * Make lower layers R/O. That way fchmod/fchown on lower file
1291 * will fail instead of modifying lower fs.
1293 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1295 ofs->lower_layers[ofs->numlower].mnt = mnt;
1296 ofs->lower_layers[ofs->numlower].idx = i + 1;
1297 ofs->lower_layers[ofs->numlower].fsid = fsid;
1299 ofs->lower_layers[ofs->numlower].fs =
1300 &ofs->lower_fs[fsid - 1];
1306 * When all layers on same fs, overlay can use real inode numbers.
1307 * With mount option "xino=on", mounter declares that there are enough
1308 * free high bits in underlying fs to hold the unique fsid.
1309 * If overlayfs does encounter underlying inodes using the high xino
1310 * bits reserved for fsid, it emits a warning and uses the original
1313 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
1315 ofs->config.xino = OVL_XINO_OFF;
1316 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1318 * This is a roundup of number of bits needed for numlowerfs+1
1319 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1320 * upper fs even with non upper overlay.
1322 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1323 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1326 if (ofs->xino_bits) {
1327 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1336 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1340 char *lowertmp, *lower;
1341 struct path *stack = NULL;
1342 unsigned int stacklen, numlower = 0, i;
1343 bool remote = false;
1344 struct ovl_entry *oe;
1347 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1352 stacklen = ovl_split_lowerdirs(lowertmp);
1353 if (stacklen > OVL_MAX_STACK) {
1354 pr_err("overlayfs: too many lower directories, limit is %d\n",
1357 } else if (!ofs->config.upperdir && stacklen == 1) {
1358 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1360 } else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1361 ofs->config.redirect_follow) {
1362 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1363 ofs->config.nfs_export = false;
1367 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1373 for (numlower = 0; numlower < stacklen; numlower++) {
1374 err = ovl_lower_dir(lower, &stack[numlower], ofs,
1375 &sb->s_stack_depth, &remote);
1379 lower = strchr(lower, '\0') + 1;
1383 sb->s_stack_depth++;
1384 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1385 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1389 err = ovl_get_lower_layers(ofs, stack, numlower);
1394 oe = ovl_alloc_entry(numlower);
1398 for (i = 0; i < numlower; i++) {
1399 oe->lowerstack[i].dentry = dget(stack[i].dentry);
1400 oe->lowerstack[i].layer = &ofs->lower_layers[i];
1404 sb->s_d_op = &ovl_reval_dentry_operations;
1406 sb->s_d_op = &ovl_dentry_operations;
1409 for (i = 0; i < numlower; i++)
1410 path_put(&stack[i]);
1421 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1423 struct path upperpath = { };
1424 struct dentry *root_dentry;
1425 struct ovl_entry *oe;
1431 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1435 ofs->creator_cred = cred = prepare_creds();
1439 ofs->config.index = ovl_index_def;
1440 ofs->config.nfs_export = ovl_nfs_export_def;
1441 ofs->config.xino = ovl_xino_def();
1442 ofs->config.metacopy = ovl_metacopy_def;
1443 err = ovl_parse_opt((char *) data, &ofs->config);
1448 if (!ofs->config.lowerdir) {
1450 pr_err("overlayfs: missing 'lowerdir'\n");
1454 sb->s_stack_depth = 0;
1455 sb->s_maxbytes = MAX_LFS_FILESIZE;
1456 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
1457 if (ofs->config.xino != OVL_XINO_OFF)
1458 ofs->xino_bits = BITS_PER_LONG - 32;
1460 if (ofs->config.upperdir) {
1461 if (!ofs->config.workdir) {
1462 pr_err("overlayfs: missing 'workdir'\n");
1466 err = ovl_get_upper(ofs, &upperpath);
1470 err = ovl_get_workdir(ofs, &upperpath);
1475 sb->s_flags |= SB_RDONLY;
1477 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1478 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
1481 oe = ovl_get_lowerstack(sb, ofs);
1486 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1487 if (!ofs->upper_mnt)
1488 sb->s_flags |= SB_RDONLY;
1490 if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1491 err = ovl_get_indexdir(ofs, oe, &upperpath);
1495 /* Force r/o mount with no index dir */
1496 if (!ofs->indexdir) {
1498 ofs->workdir = NULL;
1499 sb->s_flags |= SB_RDONLY;
1504 /* Show index=off in /proc/mounts for forced r/o mount */
1505 if (!ofs->indexdir) {
1506 ofs->config.index = false;
1507 if (ofs->upper_mnt && ofs->config.nfs_export) {
1508 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1509 ofs->config.nfs_export = false;
1513 if (ofs->config.metacopy && ofs->config.nfs_export) {
1514 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1515 ofs->config.nfs_export = false;
1518 if (ofs->config.nfs_export)
1519 sb->s_export_op = &ovl_export_operations;
1521 /* Never override disk quota limits or use reserved space */
1522 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1524 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1525 sb->s_op = &ovl_super_operations;
1526 sb->s_xattr = ovl_xattr_handlers;
1527 sb->s_fs_info = ofs;
1528 sb->s_flags |= SB_POSIXACL;
1531 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1535 root_dentry->d_fsdata = oe;
1537 mntput(upperpath.mnt);
1538 if (upperpath.dentry) {
1539 ovl_dentry_set_upper_alias(root_dentry);
1540 if (ovl_is_impuredir(upperpath.dentry))
1541 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1544 /* Root is always merge -> can have whiteouts */
1545 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
1546 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
1547 ovl_set_upperdata(d_inode(root_dentry));
1548 ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1549 ovl_dentry_lower(root_dentry), NULL);
1551 sb->s_root = root_dentry;
1556 ovl_entry_stack_free(oe);
1559 path_put(&upperpath);
1565 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1566 const char *dev_name, void *raw_data)
1568 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1571 static struct file_system_type ovl_fs_type = {
1572 .owner = THIS_MODULE,
1575 .kill_sb = kill_anon_super,
1577 MODULE_ALIAS_FS("overlay");
1579 static void ovl_inode_init_once(void *foo)
1581 struct ovl_inode *oi = foo;
1583 inode_init_once(&oi->vfs_inode);
1586 static int __init ovl_init(void)
1590 ovl_inode_cachep = kmem_cache_create("ovl_inode",
1591 sizeof(struct ovl_inode), 0,
1592 (SLAB_RECLAIM_ACCOUNT|
1593 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1594 ovl_inode_init_once);
1595 if (ovl_inode_cachep == NULL)
1598 err = register_filesystem(&ovl_fs_type);
1600 kmem_cache_destroy(ovl_inode_cachep);
1605 static void __exit ovl_exit(void)
1607 unregister_filesystem(&ovl_fs_type);
1610 * Make sure all delayed rcu free inodes are flushed before we
1614 kmem_cache_destroy(ovl_inode_cachep);
1618 module_init(ovl_init);
1619 module_exit(ovl_exit);