^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2011 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <uapi/linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/parser.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/statfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/posix_acl_xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) MODULE_DESCRIPTION("Overlay filesystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) MODULE_IMPORT_NS(ANDROID_GKI_VFS_EXPORT_ONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct ovl_dir_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define OVL_MAX_STACK 500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_PARM_DESC(redirect_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) "Default to on or off for the redirect_dir feature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static bool ovl_redirect_always_follow =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) module_param_named(redirect_always_follow, ovl_redirect_always_follow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) MODULE_PARM_DESC(redirect_always_follow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) "Follow redirects even if redirect_dir feature is turned off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) module_param_named(index, ovl_index_def, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) MODULE_PARM_DESC(index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) "Default to on or off for the inodes index feature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) MODULE_PARM_DESC(nfs_export,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) "Default to on or off for the NFS export feature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) MODULE_PARM_DESC(xino_auto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) "Auto enable xino feature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static bool __read_mostly ovl_override_creds_def = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) module_param_named(override_creds, ovl_override_creds_def, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) MODULE_PARM_DESC(ovl_override_creds_def,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "Use mounter's credentials for accesses");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static void ovl_entry_stack_free(struct ovl_entry *oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) for (i = 0; i < oe->numlower; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) dput(oe->lowerstack[i].dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) MODULE_PARM_DESC(metacopy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) "Default to on or off for the metadata only copy up feature");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void ovl_dentry_release(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (oe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) ovl_entry_stack_free(oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kfree_rcu(oe, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct dentry *ovl_d_real(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct dentry *real = NULL, *lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* It's an overlay file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (inode && d_inode(dentry) == inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!d_is_reg(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!inode || inode == d_inode(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) goto bug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) real = ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (real && (inode == d_inode(real)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) lower = ovl_dentry_lowerdata(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (!lower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto bug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) real = lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* Handle recursion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) real = d_real(real, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!inode || inode == d_inode(real))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) bug:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) inode ? inode->i_ino : 0, real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) real && d_inode(real) ? d_inode(real)->i_ino : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (weak) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ret = d->d_op->d_weak_revalidate(d, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) } else if (d->d_flags & DCACHE_OP_REVALIDATE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = d->d_op->d_revalidate(d, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (!(flags & LOOKUP_RCU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) d_invalidate(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ret = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static int ovl_dentry_revalidate_common(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned int flags, bool weak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct dentry *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) upper = ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ret = ovl_revalidate_real(upper, flags, weak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) for (i = 0; ret > 0 && i < oe->numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) weak);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return ovl_dentry_revalidate_common(dentry, flags, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return ovl_dentry_revalidate_common(dentry, flags, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static const struct dentry_operations ovl_dentry_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .d_release = ovl_dentry_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) .d_real = ovl_d_real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .d_revalidate = ovl_dentry_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .d_weak_revalidate = ovl_dentry_weak_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static struct kmem_cache *ovl_inode_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static struct inode *ovl_alloc_inode(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!oi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) oi->cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) oi->redirect = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) oi->version = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) oi->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) oi->__upperdentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) oi->lower = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) oi->lowerdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) mutex_init(&oi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return &oi->vfs_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static void ovl_free_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct ovl_inode *oi = OVL_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) kfree(oi->redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) mutex_destroy(&oi->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) kmem_cache_free(ovl_inode_cachep, oi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static void ovl_destroy_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct ovl_inode *oi = OVL_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) dput(oi->__upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) iput(oi->lower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) ovl_dir_cache_free(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) iput(oi->lowerdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void ovl_free_fs(struct ovl_fs *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct vfsmount **mounts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) iput(ofs->workbasedir_trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) iput(ofs->indexdir_trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) iput(ofs->workdir_trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) dput(ofs->whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) dput(ofs->indexdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dput(ofs->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (ofs->workdir_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) ovl_inuse_unlock(ofs->workbasedir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dput(ofs->workbasedir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (ofs->upperdir_locked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) mounts = (struct vfsmount **) ofs->layers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) for (i = 0; i < ofs->numlayer; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) iput(ofs->layers[i].trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) mounts[i] = ofs->layers[i].mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) kern_unmount_array(mounts, ofs->numlayer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) kfree(ofs->layers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) for (i = 0; i < ofs->numfs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) free_anon_bdev(ofs->fs[i].pseudo_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) kfree(ofs->fs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) kfree(ofs->config.lowerdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) kfree(ofs->config.upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) kfree(ofs->config.workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) kfree(ofs->config.redirect_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (ofs->creator_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) put_cred(ofs->creator_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) kfree(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static void ovl_put_super(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) ovl_free_fs(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /* Sync real dirty inodes in upper filesystem (if it exists) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int ovl_sync_fs(struct super_block *sb, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct super_block *upper_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = ovl_sync_status(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * We have to always set the err, because the return value isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * checked in syncfs, and instead indirectly return an error via
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * the sb's writeback errseq, which VFS inspects after this call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) errseq_set(&sb->s_wb_err, -EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * All the super blocks will be iterated, including upper_sb.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * If this is a syncfs(2) call, then we do need to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * sync_filesystem() on upper_sb, but enough if we do it when being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * called with wait == 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) down_read(&upper_sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ret = sync_filesystem(upper_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) up_read(&upper_sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * ovl_statfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * @sb: The overlayfs super block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * @buf: The struct kstatfs to fill in with stats
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * Get the filesystem statistics. As writes always target the upper layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * filesystem pass the statfs to the upper filesystem (if it exists)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct dentry *root_dentry = dentry->d_sb->s_root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) struct path path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ovl_path_real(root_dentry, &path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) err = vfs_statfs(&path, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) buf->f_namelen = ofs->namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) buf->f_type = OVERLAYFS_SUPER_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* Will this overlay be forced to mount/remount ro? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static bool ovl_force_readonly(struct ovl_fs *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return (!ovl_upper_mnt(ofs) || !ofs->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static const char *ovl_redirect_mode_def(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return ovl_redirect_dir_def ? "on" : "off";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static const char * const ovl_xino_str[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) "off",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) "auto",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) "on",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static inline int ovl_xino_def(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * ovl_show_options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * Prints the mount options for a given superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * Returns zero; does not fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) seq_show_option(m, "lowerdir", ofs->config.lowerdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) if (ofs->config.upperdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) seq_show_option(m, "upperdir", ofs->config.upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) seq_show_option(m, "workdir", ofs->config.workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (ofs->config.default_permissions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) seq_puts(m, ",default_permissions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (ofs->config.index != ovl_index_def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (ofs->config.nfs_export != ovl_nfs_export_def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) "on" : "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (ofs->config.metacopy != ovl_metacopy_def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) seq_printf(m, ",metacopy=%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) ofs->config.metacopy ? "on" : "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (ofs->config.ovl_volatile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) seq_puts(m, ",volatile");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (ofs->config.override_creds != ovl_override_creds_def)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) seq_show_option(m, "override_creds",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ofs->config.override_creds ? "on" : "off");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static int ovl_remount(struct super_block *sb, int *flags, char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) struct super_block *upper_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (ovl_should_sync(ofs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) down_read(&upper_sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ret = sync_filesystem(upper_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) up_read(&upper_sb->s_umount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) static const struct super_operations ovl_super_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) .alloc_inode = ovl_alloc_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) .free_inode = ovl_free_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) .destroy_inode = ovl_destroy_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) .drop_inode = generic_delete_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) .put_super = ovl_put_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) .sync_fs = ovl_sync_fs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) .statfs = ovl_statfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .show_options = ovl_show_options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .remount_fs = ovl_remount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) OPT_LOWERDIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) OPT_UPPERDIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) OPT_WORKDIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) OPT_DEFAULT_PERMISSIONS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) OPT_REDIRECT_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) OPT_INDEX_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) OPT_INDEX_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) OPT_NFS_EXPORT_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) OPT_NFS_EXPORT_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) OPT_XINO_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) OPT_XINO_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) OPT_XINO_AUTO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) OPT_METACOPY_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) OPT_METACOPY_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) OPT_VOLATILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) OPT_OVERRIDE_CREDS_ON,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) OPT_OVERRIDE_CREDS_OFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) OPT_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) static const match_table_t ovl_tokens = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {OPT_LOWERDIR, "lowerdir=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) {OPT_UPPERDIR, "upperdir=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {OPT_WORKDIR, "workdir=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) {OPT_REDIRECT_DIR, "redirect_dir=%s"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {OPT_INDEX_ON, "index=on"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {OPT_INDEX_OFF, "index=off"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {OPT_NFS_EXPORT_ON, "nfs_export=on"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {OPT_NFS_EXPORT_OFF, "nfs_export=off"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {OPT_XINO_ON, "xino=on"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {OPT_XINO_OFF, "xino=off"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {OPT_XINO_AUTO, "xino=auto"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {OPT_METACOPY_ON, "metacopy=on"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {OPT_METACOPY_OFF, "metacopy=off"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {OPT_VOLATILE, "volatile"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {OPT_OVERRIDE_CREDS_ON, "override_creds=on"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {OPT_OVERRIDE_CREDS_OFF, "override_creds=off"},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {OPT_ERR, NULL}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static char *ovl_next_opt(char **s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) char *sbegin = *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (sbegin == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) for (p = sbegin; *p; p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (*p == '\\') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) } else if (*p == ',') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) *p = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) *s = p + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return sbegin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) *s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) return sbegin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (strcmp(mode, "on") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) config->redirect_dir = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) * Does not make sense to have redirect creation without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) * redirect following.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) config->redirect_follow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) } else if (strcmp(mode, "follow") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) config->redirect_follow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) } else if (strcmp(mode, "off") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (ovl_redirect_always_follow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) config->redirect_follow = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) } else if (strcmp(mode, "nofollow") != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) pr_err("bad mount option \"redirect_dir=%s\"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int ovl_parse_opt(char *opt, struct ovl_config *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) bool metacopy_opt = false, redirect_opt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) bool nfs_export_opt = false, index_opt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (!config->redirect_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) config->override_creds = ovl_override_creds_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) while ((p = ovl_next_opt(&opt)) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) int token;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) substring_t args[MAX_OPT_ARGS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (!*p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) token = match_token(p, ovl_tokens, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) switch (token) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) case OPT_UPPERDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) kfree(config->upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) config->upperdir = match_strdup(&args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) if (!config->upperdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) case OPT_LOWERDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) kfree(config->lowerdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) config->lowerdir = match_strdup(&args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (!config->lowerdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) case OPT_WORKDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) kfree(config->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) config->workdir = match_strdup(&args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!config->workdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) case OPT_DEFAULT_PERMISSIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) config->default_permissions = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) case OPT_REDIRECT_DIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) kfree(config->redirect_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) config->redirect_mode = match_strdup(&args[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) if (!config->redirect_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) redirect_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) case OPT_INDEX_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) config->index = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) index_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) case OPT_INDEX_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) config->index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) index_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) case OPT_NFS_EXPORT_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) config->nfs_export = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) nfs_export_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) case OPT_NFS_EXPORT_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) config->nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) nfs_export_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) case OPT_XINO_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) config->xino = OVL_XINO_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) case OPT_XINO_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) config->xino = OVL_XINO_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) case OPT_XINO_AUTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) config->xino = OVL_XINO_AUTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) case OPT_METACOPY_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) config->metacopy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) metacopy_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) case OPT_METACOPY_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) config->metacopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) metacopy_opt = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) case OPT_VOLATILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) config->ovl_volatile = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) case OPT_OVERRIDE_CREDS_ON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) config->override_creds = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) case OPT_OVERRIDE_CREDS_OFF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) config->override_creds = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) pr_err("unrecognized mount option \"%s\" or missing value\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /* Workdir/index are useless in non-upper mount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (!config->upperdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (config->workdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) config->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) kfree(config->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) config->workdir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (config->index && index_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) index_opt = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) config->index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (!config->upperdir && config->ovl_volatile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) config->ovl_volatile = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) err = ovl_parse_redirect_mode(config, config->redirect_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) * This is to make the logic below simpler. It doesn't make any other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) * difference, since config->redirect_dir is only used for upper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (!config->upperdir && config->redirect_follow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) config->redirect_dir = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /* Resolve metacopy -> redirect_dir dependency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (config->metacopy && !config->redirect_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (metacopy_opt && redirect_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) config->redirect_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (redirect_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * There was an explicit redirect_dir=... that resulted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * in this conflict.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) pr_info("disabling metacopy due to redirect_dir=%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) config->redirect_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) config->metacopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) /* Automatically enable redirect otherwise. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) config->redirect_follow = config->redirect_dir = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) /* Resolve nfs_export -> index dependency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) if (config->nfs_export && !config->index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) if (!config->upperdir && config->redirect_follow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) config->nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) } else if (nfs_export_opt && index_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) pr_err("conflicting options: nfs_export=on,index=off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) } else if (index_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * There was an explicit index=off that resulted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * in this conflict.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) pr_info("disabling nfs_export due to index=off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) config->nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) /* Automatically enable index otherwise. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) config->index = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) /* Resolve nfs_export -> !metacopy dependency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (config->nfs_export && config->metacopy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (nfs_export_opt && metacopy_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) pr_err("conflicting options: nfs_export=on,metacopy=on\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (metacopy_opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * There was an explicit metacopy=on that resulted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * in this conflict.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) pr_info("disabling nfs_export due to metacopy=on\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) config->nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * There was an explicit nfs_export=on that resulted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * in this conflict.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) pr_info("disabling metacopy due to nfs_export=on\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) config->metacopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) #define OVL_WORKDIR_NAME "work"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) #define OVL_INDEXDIR_NAME "index"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) const char *name, bool persist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) struct inode *dir = ofs->workbasedir->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) struct vfsmount *mnt = ovl_upper_mnt(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) struct dentry *work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) bool retried = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) work = lookup_one_len(name, ofs->workbasedir, strlen(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) if (!IS_ERR(work)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct iattr attr = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) .ia_valid = ATTR_MODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) .ia_mode = S_IFDIR | 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) if (work->d_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) if (retried)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (persist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) retried = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) err = ovl_workdir_cleanup(dir, mnt, work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) dput(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (err == -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) work = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) err = ovl_mkdir_real(dir, &work, attr.ia_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) /* Weird filesystem returning with hashed negative (kernfs)? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (d_really_is_negative(work))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) * Try to remove POSIX ACL xattrs from workdir. We are good if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * a) success (there was a POSIX ACL xattr and was removed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * b) -ENODATA (there was no POSIX ACL xattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * There are various other error values that could effectively
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * mean that the xattr doesn't exist (e.g. -ERANGE is returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * if the xattr name is too long), but the set of filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * allowed as upper are limited to "normal" ones, where checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * for the above two errors is sufficient.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (err && err != -ENODATA && err != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) if (err && err != -ENODATA && err != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) /* Clear any inherited mode bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) inode_lock(work->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) err = notify_change(work, &attr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) inode_unlock(work->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) err = PTR_ERR(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) dput(work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) ofs->config.workdir, name, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) work = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) static void ovl_unescape(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) char *d = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) for (;; s++, d++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (*s == '\\')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) *d = *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (!*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) static int ovl_mount_dir_noesc(const char *name, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (!*name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) pr_err("empty lowerdir\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) err = kern_path(name, LOOKUP_FOLLOW, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) pr_err("failed to resolve '%s': %i\n", name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (ovl_dentry_weird(path->dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) pr_err("filesystem on '%s' not supported\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) if (!d_is_dir(path->dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) pr_err("'%s' not a directory\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) path_put_init(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) static int ovl_mount_dir(const char *name, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) int err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) char *tmp = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) ovl_unescape(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) err = ovl_mount_dir_noesc(tmp, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) pr_err("filesystem on '%s' not supported as upperdir\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) path_put_init(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) kfree(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) struct kstatfs statfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) int err = vfs_statfs(path, &statfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) pr_err("statfs failed on '%s'\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) ofs->namelen = max(ofs->namelen, statfs.f_namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) static int ovl_lower_dir(const char *name, struct path *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) struct ovl_fs *ofs, int *stack_depth)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) int fh_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) err = ovl_mount_dir_noesc(name, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) err = ovl_check_namelen(path, ofs, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) * The inodes index feature and NFS export need to encode and decode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) * file handles, so they require that all layers support them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) fh_type = ovl_can_decode_fh(path->dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) if ((ofs->config.nfs_export ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) ofs->config.index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) ofs->config.nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /* Check if lower fs has 32bit inode numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (fh_type != FILEID_INO32_GEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) ofs->xino_mode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) /* Workdir should not be subdir of upperdir and vice versa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) bool ok = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) if (workdir != upperdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) ok = (lock_rename(workdir, upperdir) == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) unlock_rename(workdir, upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) return ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) static unsigned int ovl_split_lowerdirs(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) unsigned int ctr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) char *s, *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) for (s = d = str;; s++, d++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (*s == '\\') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) } else if (*s == ':') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) *d = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) ctr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) *d = *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (!*s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) static int __maybe_unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) const char *name, void *buffer, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) return ovl_xattr_get(dentry, inode, handler->name, buffer, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) static int __maybe_unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) struct dentry *workdir = ovl_workdir(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) struct inode *realinode = ovl_inode_real(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) struct posix_acl *acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) /* Check that everything is OK before copy-up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) acl = posix_acl_from_xattr(&init_user_ns, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (IS_ERR(acl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return PTR_ERR(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (!IS_POSIXACL(d_inode(workdir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) goto out_acl_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) if (!realinode->i_op->set_acl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) goto out_acl_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) err = acl ? -EACCES : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) goto out_acl_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) if (!inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) goto out_acl_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) posix_acl_release(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * Check if sgid bit needs to be cleared (actual setacl operation will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * be done with mounter's capabilities and so that won't do it for us).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) if (unlikely(inode->i_mode & S_ISGID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) handler->flags == ACL_TYPE_ACCESS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) !in_group_p(inode->i_gid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) err = ovl_setattr(dentry, &iattr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) ovl_copyattr(ovl_inode_real(inode), inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) out_acl_release:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) posix_acl_release(acl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) static int ovl_own_xattr_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) const char *name, void *buffer, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) static int ovl_own_xattr_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static int ovl_other_xattr_get(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) const char *name, void *buffer, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) return ovl_xattr_get(dentry, inode, name, buffer, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) static int ovl_other_xattr_set(const struct xattr_handler *handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) struct dentry *dentry, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) const char *name, const void *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) return ovl_xattr_set(dentry, inode, name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) static const struct xattr_handler __maybe_unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) ovl_posix_acl_access_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) .name = XATTR_NAME_POSIX_ACL_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) .flags = ACL_TYPE_ACCESS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) .get = ovl_posix_acl_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) .set = ovl_posix_acl_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) static const struct xattr_handler __maybe_unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) ovl_posix_acl_default_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) .name = XATTR_NAME_POSIX_ACL_DEFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) .flags = ACL_TYPE_DEFAULT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) .get = ovl_posix_acl_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) .set = ovl_posix_acl_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static const struct xattr_handler ovl_own_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) .prefix = OVL_XATTR_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) .get = ovl_own_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) .set = ovl_own_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) static const struct xattr_handler ovl_other_xattr_handler = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) .prefix = "", /* catch all */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) .get = ovl_other_xattr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) .set = ovl_other_xattr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) static const struct xattr_handler *ovl_xattr_handlers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) #ifdef CONFIG_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) &ovl_posix_acl_access_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) &ovl_posix_acl_default_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) &ovl_own_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) &ovl_other_xattr_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) struct inode **ptrap, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) struct inode *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) trap = ovl_get_trap_inode(sb, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) err = PTR_ERR_OR_ZERO(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (err == -ELOOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) pr_err("conflicting %s path\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) *ptrap = trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) * Determine how we treat concurrent use of upperdir/workdir based on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) * index feature. This is papering over mount leaks of container runtimes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) * for example, an old overlay mount is leaked and now its upperdir is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * attempted to be used as a lower layer in a new overlay mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) if (ofs->config.index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) struct ovl_layer *upper_layer, struct path *upperpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) struct vfsmount *upper_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) err = ovl_mount_dir(ofs->config.upperdir, upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) /* Upper fs should not be r/o */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (sb_rdonly(upperpath->mnt->mnt_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) pr_err("upper fs is r/o, try multi-lower layers mount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) "upperdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) upper_mnt = clone_private_mount(upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) err = PTR_ERR(upper_mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (IS_ERR(upper_mnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) pr_err("failed to clone upperpath\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) /* Don't inherit atime flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) upper_layer->mnt = upper_mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) upper_layer->idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) upper_layer->fsid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) * Inherit SB_NOSEC flag from upperdir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) * This optimization changes behavior when a security related attribute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) * (suid/sgid/security.*) is changed on an underlying layer. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) * okay because we don't yet have guarantees in that case, but it will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * need careful treatment once we want to honour changes to underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * filesystems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) sb->s_flags |= SB_NOSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) ofs->upperdir_locked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) err = ovl_report_in_use(ofs, "upperdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) * negative values if error is encountered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static int ovl_check_rename_whiteout(struct dentry *workdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) struct inode *dir = d_inode(workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) struct dentry *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) struct dentry *dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) struct dentry *whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) struct name_snapshot name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) temp = ovl_create_temp(workdir, OVL_CATTR(S_IFREG | 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) err = PTR_ERR(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (IS_ERR(temp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) dest = ovl_lookup_temp(workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) err = PTR_ERR(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) if (IS_ERR(dest)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) dput(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) /* Name is inline and stable - using snapshot as a copy helper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) take_dentry_name_snapshot(&name, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) err = ovl_do_rename(dir, temp, dir, dest, RENAME_WHITEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if (err == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) goto cleanup_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) whiteout = lookup_one_len(name.name.name, workdir, name.name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) err = PTR_ERR(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) if (IS_ERR(whiteout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) goto cleanup_temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) err = ovl_is_whiteout(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) /* Best effort cleanup of whiteout and temp file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) ovl_cleanup(dir, whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) dput(whiteout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) cleanup_temp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) ovl_cleanup(dir, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) release_dentry_name_snapshot(&name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) dput(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) dput(dest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) static struct dentry *ovl_lookup_or_create(struct dentry *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) const char *name, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) size_t len = strlen(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) struct dentry *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) child = lookup_one_len(name, parent, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) if (!IS_ERR(child) && !child->d_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) child = ovl_create_real(parent->d_inode, child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) OVL_CATTR(mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) inode_unlock(parent->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) return child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) * Creates $workdir/work/incompat/volatile/dirty file if it is not already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) * present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) unsigned int ctr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) struct dentry *d = dget(ofs->workbasedir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) static const char *const volatile_path[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) const char *const *name = volatile_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) d = ovl_lookup_or_create(d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) if (IS_ERR(d))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) return PTR_ERR(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) dput(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) struct path *workpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) struct vfsmount *mnt = ovl_upper_mnt(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) struct dentry *temp, *workdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) bool rename_whiteout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) bool d_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) int fh_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) err = mnt_want_write(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) err = PTR_ERR(workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) if (IS_ERR_OR_NULL(workdir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) ofs->workdir = workdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) * Upper should support d_type, else whiteouts are visible. Given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) * workdir and upper are on same fs, we can do iterate_dir() on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) * workdir. This check requires successful creation of workdir in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) * previous step.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) err = ovl_check_d_type_supported(workpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) d_type = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) if (!d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) pr_warn("upper fs needs to support d_type.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) /* Check if upper/work fs supports O_TMPFILE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) ofs->tmpfile = !IS_ERR(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) if (ofs->tmpfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) dput(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) pr_warn("upper fs does not support tmpfile.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) /* Check if upper/work fs supports RENAME_WHITEOUT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) err = ovl_check_rename_whiteout(ofs->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) rename_whiteout = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) if (!rename_whiteout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) * Check if upper/work fs supports trusted.overlay.* xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) err = ovl_do_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) ofs->noxattr = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) ofs->config.index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) ofs->config.metacopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) ovl_do_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) * We allowed sub-optimal upper fs configuration and don't want to break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) * users over kernel upgrade, but we never allowed remote upper fs, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) * we can enforce strict requirements for remote upper fs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (ovl_dentry_remote(ofs->workdir) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) (!d_type || !rename_whiteout || ofs->noxattr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) pr_err("upper fs missing required features.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) * For volatile mount, create a incompat/volatile/dirty file to keep
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) * track of it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) if (ofs->config.ovl_volatile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) err = ovl_create_volatile_dirty(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) pr_err("Failed to create volatile/dirty file.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) /* Check if upper/work fs supports file handles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (ofs->config.index && !fh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) ofs->config.index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) pr_warn("upper fs does not support file handles, falling back to index=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) /* Check if upper fs has 32bit inode numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (fh_type != FILEID_INO32_GEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) ofs->xino_mode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) /* NFS export of r/w mount depends on index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) if (ofs->config.nfs_export && !ofs->config.index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) ofs->config.nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) mnt_drop_write(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) struct path *upperpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) struct path workpath = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) err = ovl_mount_dir(ofs->config.workdir, &workpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) if (upperpath->mnt != workpath.mnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) pr_err("workdir and upperdir must reside under the same mount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) pr_err("workdir and upperdir must be separate subtrees\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) ofs->workbasedir = dget(workpath.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) if (ovl_inuse_trylock(ofs->workbasedir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) ofs->workdir_locked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) err = ovl_report_in_use(ofs, "workdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) "workdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) err = ovl_make_workdir(sb, ofs, &workpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) path_put(&workpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) struct ovl_entry *oe, struct path *upperpath)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) struct vfsmount *mnt = ovl_upper_mnt(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) struct dentry *indexdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) err = mnt_want_write(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) /* Verify lower root is upper root origin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) err = ovl_verify_origin(ofs, upperpath->dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) oe->lowerstack[0].dentry, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) pr_err("failed to verify upper root origin\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) /* index dir will act also as workdir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) iput(ofs->workdir_trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) ofs->workdir_trap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) dput(ofs->workdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) ofs->workdir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) if (IS_ERR(indexdir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) err = PTR_ERR(indexdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) } else if (indexdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) ofs->indexdir = indexdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) ofs->workdir = dget(indexdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) "indexdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) * Verify upper root is exclusively associated with index dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) * Older kernels stored upper fh in "trusted.overlay.origin"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) * xattr. If that xattr exists, verify that it is a match to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) * upper dir file handle. In any case, verify or set xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) * "trusted.overlay.upper" to indicate that index may have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) * directory entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) err = ovl_verify_set_fh(ofs, ofs->indexdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) OVL_XATTR_ORIGIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) upperpath->dentry, true, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) pr_err("failed to verify index dir 'origin' xattr\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) pr_err("failed to verify index dir 'upper' xattr\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) /* Cleanup bad/stale/orphan index entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) err = ovl_indexdir_cleanup(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (err || !ofs->indexdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) mnt_drop_write(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) * We allow using single lower with null uuid for index and nfs_export
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) * for example to support those features with single lower squashfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) * To avoid regressions in setups of overlay with re-formatted lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) * squashfs, do not allow decoding origin with lower null uuid unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) * user opted-in to one of the new features that require following the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) * lower inode of non-dir upper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) uuid_is_null(uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) for (i = 0; i < ofs->numfs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) * We use uuid to associate an overlay lower file handle with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) * lower layer, so we can accept lower fs with null uuid as long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) * as all lower layers with null uuid are on the same fs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) * if we detect multiple lower fs with the same uuid, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) * disable lower file handle decoding on all of them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) if (ofs->fs[i].is_lower &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) ofs->fs[i].bad_uuid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) /* Get a unique fsid for the layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) struct super_block *sb = path->mnt->mnt_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) dev_t dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) bool bad_uuid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) for (i = 0; i < ofs->numfs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) if (ofs->fs[i].sb == sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) bad_uuid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (ofs->config.index || ofs->config.nfs_export) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) ofs->config.index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) ofs->config.nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) pr_warn("%s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) uuid_is_null(&sb->s_uuid) ? "null" :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) "conflicting",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) err = get_anon_bdev(&dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) pr_err("failed to get anonymous bdev for lowerpath\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) ofs->fs[ofs->numfs].sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) ofs->fs[ofs->numfs].pseudo_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) return ofs->numfs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) struct path *stack, unsigned int numlower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) struct ovl_layer *layers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) if (ofs->fs == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) /* idx/fsid 0 are reserved for upper fs even with lower only overlay */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) ofs->numfs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) * All lower layers that share the same fs as upper layer, use the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) * only overlay to simplify ovl_fs_free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) * is_lower will be set if upper fs is shared with a lower layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) pr_err("failed to get anonymous bdev for upper fs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (ovl_upper_mnt(ofs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) ofs->fs[0].is_lower = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) for (i = 0; i < numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) struct inode *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) int fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) err = fsid = ovl_get_fsid(ofs, &stack[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) * Check if lower root conflicts with this overlay layers before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) * checking if it is in-use as upperdir/workdir of "another"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) * mount, because we do not bother to check in ovl_is_inuse() if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) * the upperdir/workdir is in fact in-use by our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) * upperdir/workdir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) if (ovl_is_inuse(stack[i].dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) err = ovl_report_in_use(ofs, "lowerdir");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) iput(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) mnt = clone_private_mount(&stack[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) err = PTR_ERR(mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) if (IS_ERR(mnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) pr_err("failed to clone lowerpath\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) iput(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) * Make lower layers R/O. That way fchmod/fchown on lower file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) * will fail instead of modifying lower fs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) layers[ofs->numlayer].trap = trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) layers[ofs->numlayer].mnt = mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) layers[ofs->numlayer].idx = ofs->numlayer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) layers[ofs->numlayer].fsid = fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) layers[ofs->numlayer].fs = &ofs->fs[fsid];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) ofs->numlayer++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) ofs->fs[fsid].is_lower = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) * When all layers on same fs, overlay can use real inode numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) * With mount option "xino=<on|auto>", mounter declares that there are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) * enough free high bits in underlying fs to hold the unique fsid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) * If overlayfs does encounter underlying inodes using the high xino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) * bits reserved for fsid, it emits a warning and uses the original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) * inode number or a non persistent inode number allocated from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) * dedicated range.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (ofs->config.xino == OVL_XINO_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) ofs->xino_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) } else if (ofs->config.xino == OVL_XINO_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) ofs->xino_mode = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) } else if (ofs->xino_mode < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) * This is a roundup of number of bits needed for encoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) * fsid, where fsid 0 is reserved for upper fs (even with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) * lower only overlay) +1 extra bit is reserved for the non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) * persistent inode number range that is used for resolving
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) * xino lower bits overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) if (ofs->xino_mode > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) ofs->xino_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) const char *lower, unsigned int numlower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) struct ovl_fs *ofs, struct ovl_layer *layers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) struct path *stack = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) struct ovl_entry *oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) if (!ofs->config.upperdir && numlower == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) if (!stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) for (i = 0; i < numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) lower = strchr(lower, '\0') + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) sb->s_stack_depth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) pr_err("maximum fs stacking depth exceeded\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) err = ovl_get_layers(sb, ofs, stack, numlower, layers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) oe = ovl_alloc_entry(numlower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) if (!oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) for (i = 0; i < numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) oe->lowerstack[i].dentry = dget(stack[i].dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) oe->lowerstack[i].layer = &ofs->layers[i+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) for (i = 0; i < numlower; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) path_put(&stack[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) kfree(stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) return oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) oe = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) * Check if this layer root is a descendant of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) * - another layer of this overlayfs instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) * - upper/work dir of any overlayfs instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) struct dentry *dentry, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) bool is_lower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) struct dentry *next = dentry, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) parent = dget_parent(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) /* Walk back ancestors to root (inclusive) looking for traps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) while (!err && parent != next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) err = -ELOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) pr_err("overlapping %s path\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) } else if (ovl_is_inuse(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) err = ovl_report_in_use(ofs, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) next = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) parent = dget_parent(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) * Check if any of the layers or work dirs overlap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) static int ovl_check_overlapping_layers(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) struct ovl_fs *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) int i, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) if (ovl_upper_mnt(ofs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) "upperdir", false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) * this instance and covers overlapping work and index dirs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) * unless work or index dir have been moved since created inside
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) * workbasedir. In that case, we already have their traps in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) * inode cache and we will catch that case on lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) for (i = 1; i < ofs->numlayer; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) err = ovl_check_layer(sb, ofs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) ofs->layers[i].mnt->mnt_root,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) "lowerdir", true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) static struct dentry *ovl_get_root(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) struct dentry *upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) struct ovl_entry *oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) struct dentry *root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) struct ovl_path *lowerpath = &oe->lowerstack[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) int fsid = lowerpath->layer->fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) struct ovl_inode_params oip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) .upperdentry = upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) .lowerpath = lowerpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) if (!root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) root->d_fsdata = oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) if (upperdentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) /* Root inode uses upper st_ino/i_ino */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) ino = d_inode(upperdentry)->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) fsid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) ovl_dentry_set_upper_alias(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) if (ovl_is_impuredir(sb, upperdentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) ovl_set_flag(OVL_IMPURE, d_inode(root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) /* Root is always merge -> can have whiteouts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) ovl_dentry_set_flag(OVL_E_CONNECTED, root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) ovl_set_upperdata(d_inode(root));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) ovl_inode_init(d_inode(root), &oip, ino, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) return root;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) static int ovl_fill_super(struct super_block *sb, void *data, int silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) struct path upperpath = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) struct dentry *root_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) struct ovl_entry *oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) struct ovl_fs *ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) struct ovl_layer *layers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) char *splitlower = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) unsigned int numlower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) sb->s_d_op = &ovl_dentry_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) if (!ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) ofs->creator_cred = cred = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (!cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) /* Is there a reason anyone would want not to share whiteouts? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) ofs->share_whiteout = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) ofs->config.index = ovl_index_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) ofs->config.nfs_export = ovl_nfs_export_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) ofs->config.xino = ovl_xino_def();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) ofs->config.metacopy = ovl_metacopy_def;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) err = ovl_parse_opt((char *) data, &ofs->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) if (!ofs->config.lowerdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) if (!silent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) pr_err("missing 'lowerdir'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) if (!splitlower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) numlower = ovl_split_lowerdirs(splitlower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) if (numlower > OVL_MAX_STACK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) pr_err("too many lower directories, limit is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) OVL_MAX_STACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) if (!layers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) ofs->layers = layers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) /* Layer 0 is reserved for upper even if there's no upper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) ofs->numlayer = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) sb->s_stack_depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) sb->s_maxbytes = MAX_LFS_FILESIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) atomic_long_set(&ofs->last_ino, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) /* Assume underlaying fs uses 32bit inodes unless proven otherwise */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) if (ofs->config.xino != OVL_XINO_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) ofs->xino_mode = BITS_PER_LONG - 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) if (!ofs->xino_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) ofs->config.xino = OVL_XINO_OFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) /* alloc/destroy_inode needed for setting up traps in inode cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) sb->s_op = &ovl_super_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) if (ofs->config.upperdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) struct super_block *upper_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) if (!ofs->config.workdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) pr_err("missing 'workdir'\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) if (!ovl_should_sync(ofs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) err = ovl_get_workdir(sb, ofs, &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) if (!ofs->workdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) sb->s_stack_depth = upper_sb->s_stack_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) sb->s_time_gran = upper_sb->s_time_gran;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) err = PTR_ERR(oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) if (IS_ERR(oe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) /* If the upper fs is nonexistent, we mark overlayfs r/o too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) if (!ovl_upper_mnt(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) if (!ovl_force_readonly(ofs) && ofs->config.index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) goto out_free_oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) /* Force r/o mount with no index dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) if (!ofs->indexdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) sb->s_flags |= SB_RDONLY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) err = ovl_check_overlapping_layers(sb, ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) goto out_free_oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) /* Show index=off in /proc/mounts for forced r/o mount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) if (!ofs->indexdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) ofs->config.index = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) ofs->config.nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) if (ofs->config.metacopy && ofs->config.nfs_export) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) ofs->config.nfs_export = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) if (ofs->config.nfs_export)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) sb->s_export_op = &ovl_export_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) /* Never override disk quota limits or use reserved space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) sb->s_magic = OVERLAYFS_SUPER_MAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) sb->s_xattr = ovl_xattr_handlers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) sb->s_fs_info = ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) sb->s_flags |= SB_POSIXACL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) sb->s_iflags |= SB_I_SKIP_SYNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) if (!root_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) goto out_free_oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) mntput(upperpath.mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) kfree(splitlower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) sb->s_root = root_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) out_free_oe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) ovl_entry_stack_free(oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) kfree(oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) kfree(splitlower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) path_put(&upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) ovl_free_fs(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) const char *dev_name, void *raw_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) static struct file_system_type ovl_fs_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) .name = "overlay",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) .mount = ovl_mount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) .kill_sb = kill_anon_super,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) MODULE_ALIAS_FS("overlay");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) static void ovl_inode_init_once(void *foo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) struct ovl_inode *oi = foo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) inode_init_once(&oi->vfs_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) static int __init ovl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) ovl_inode_cachep = kmem_cache_create("ovl_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) sizeof(struct ovl_inode), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) (SLAB_RECLAIM_ACCOUNT|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) SLAB_MEM_SPREAD|SLAB_ACCOUNT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) ovl_inode_init_once);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) if (ovl_inode_cachep == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) err = ovl_aio_request_cache_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) err = register_filesystem(&ovl_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) ovl_aio_request_cache_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) kmem_cache_destroy(ovl_inode_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) static void __exit ovl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) unregister_filesystem(&ovl_fs_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) * Make sure all delayed rcu free inodes are flushed before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) * destroy cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) kmem_cache_destroy(ovl_inode_cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) ovl_aio_request_cache_destroy();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) module_init(ovl_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) module_exit(ovl_exit);