^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 <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/cred.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/posix_acl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fiemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int ovl_setattr(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) bool full_copy_up = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct dentry *upperdentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) err = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) err = ovl_want_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct inode *realinode = d_inode(ovl_dentry_real(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) err = -ETXTBSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (atomic_read(&realinode->i_writecount) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /* Truncate should trigger data copy up as well */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) full_copy_up = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (!full_copy_up)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) err = ovl_copy_up(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) err = ovl_copy_up_with_data(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct inode *winode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) upperdentry = ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (attr->ia_valid & ATTR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) winode = d_inode(upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) err = get_write_access(winode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) attr->ia_valid &= ~ATTR_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * We might have to translate ovl file into real file object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * once use cases emerge. For now, simply don't let underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * filesystem rely on attr->ia_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) attr->ia_valid &= ~ATTR_FILE;
^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) * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * set. Overlayfs does not pass O_TRUNC flag to underlying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * filesystem during open -> do not pass ATTR_OPEN. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * disables optimization in fuse which assumes open(O_TRUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * already set file size to 0. But we never passed O_TRUNC to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * fuse. So by clearing ATTR_OPEN, fuse will be forced to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * setattr request to server.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) attr->ia_valid &= ~ATTR_OPEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) inode_lock(upperdentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) err = notify_change(upperdentry, attr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) inode_unlock(upperdentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (winode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) put_write_access(winode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) ovl_drop_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) bool samefs = ovl_same_fs(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) unsigned int xinobits = ovl_xino_bits(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int xinoshift = 64 - xinobits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (samefs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * When all layers are on the same fs, all real inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * number are unique, so we use the overlay st_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * which is friendly to du -x.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) stat->dev = dentry->d_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) } else if (xinobits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * All inode numbers of underlying fs should not be using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * high xinobits, so we use high xinobits to partition the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * overlay st_ino address space. The high bits holds the fsid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * (upper fsid is 0). The lowest xinobit is reserved for mapping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * the non-peresistent inode numbers range in case of overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * This way all overlay inode numbers are unique and use the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * overlay st_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (likely(!(stat->ino >> xinoshift))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) stat->ino |= ((u64)fsid) << (xinoshift + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) stat->dev = dentry->d_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) } else if (ovl_xino_warn(dentry->d_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dentry, stat->ino, xinobits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* The inode could not be mapped to a unified st_ino address space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (S_ISDIR(dentry->d_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * Always use the overlay st_dev for directories, so 'find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * -xdev' will scan the entire overlay mount and won't cross the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * overlay mount boundaries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * If not all layers are on the same fs the pair {real st_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * overlay st_dev} is not unique, so use the non persistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * overlay st_ino for directories.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) stat->dev = dentry->d_sb->s_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) stat->ino = dentry->d_inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * For non-samefs setup, if we cannot map all layers st_ino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * to a unified address space, we need to make sure that st_dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * is unique per underlying fs, so we use the unique anonymous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * bdev assigned to the underlying fs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) stat->dev = OVL_FS(dentry->d_sb)->fs[fsid].pseudo_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int ovl_getattr(const struct path *path, struct kstat *stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u32 request_mask, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct dentry *dentry = path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) enum ovl_path_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct path realpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bool is_dir = S_ISDIR(dentry->d_inode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int fsid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) bool metacopy_blocks = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) metacopy_blocks = ovl_is_metacopy_dentry(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) type = ovl_path_real(dentry, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) err = vfs_getattr(&realpath, stat, request_mask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * For non-dir or same fs, we use st_ino of the copy up origin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * This guaranties constant st_dev/st_ino across copy up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * With xino feature and non-samefs, we use st_ino of the copy up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * origin masked with high bits that represent the layer id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * If lower filesystem supports NFS file handles, this also guaranties
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * persistent st_ino across mount cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (!is_dir || ovl_same_dev(dentry->d_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!OVL_TYPE_UPPER(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) fsid = ovl_layer_lower(dentry)->fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) } else if (OVL_TYPE_ORIGIN(type)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct kstat lowerstat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) u32 lowermask = STATX_INO | STATX_BLOCKS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) (!is_dir ? STATX_NLINK : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) ovl_path_lower(dentry, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) err = vfs_getattr(&realpath, &lowerstat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) lowermask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Lower hardlinks may be broken on copy up to different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * upper files, so we cannot use the lower origin st_ino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * for those different files, even for the same fs case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * Similarly, several redirected dirs can point to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * same dir on a lower layer. With the "verify_lower"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * feature, we do not use the lower origin st_ino, if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * we haven't verified that this redirect is unique.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * With inodes index enabled, it is safe to use st_ino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * of an indexed origin. The index validates that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) * upper hardlink is not broken and that a redirected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * dir is the only redirect to that origin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) (!ovl_verify_lower(dentry->d_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) (is_dir || lowerstat.nlink == 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) fsid = ovl_layer_lower(dentry)->fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) stat->ino = lowerstat.ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * If we are querying a metacopy dentry and lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * dentry is data dentry, then use the blocks we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) * queried just now. We don't have to do additional
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) * vfs_getattr(). If lower itself is metacopy, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * additional vfs_getattr() is unavoidable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (metacopy_blocks &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) realpath.dentry == ovl_dentry_lowerdata(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) stat->blocks = lowerstat.blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) metacopy_blocks = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (metacopy_blocks) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * If lower is not same as lowerdata or if there was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * no origin on upper, we can end up here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct kstat lowerdatastat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) u32 lowermask = STATX_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) ovl_path_lowerdata(dentry, &realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) err = vfs_getattr(&realpath, &lowerdatastat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) lowermask, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) stat->blocks = lowerdatastat.blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) err = ovl_map_dev_ino(dentry, stat, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * It's probably not worth it to count subdirs to get the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * correct link count. nlink=1 seems to pacify 'find' and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * other utilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (is_dir && OVL_TYPE_MERGE(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) stat->nlink = 1;
^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) * Return the overlay inode nlinks for indexed upper inodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * Overlay inode nlink counts the union of the upper hardlinks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * and non-covered lower hardlinks. It does not include the upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * index hardlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) stat->nlink = dentry->d_inode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int ovl_permission(struct inode *inode, int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct inode *upperinode = ovl_inode_upper(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct inode *realinode = upperinode ?: ovl_inode_lower(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) /* Careful in RCU walk mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!realinode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) WARN_ON(!(mask & MAY_NOT_BLOCK));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * Check overlay inode with the creds of task and underlying inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * with creds of mounter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) err = generic_permission(inode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) old_cred = ovl_override_creds(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!upperinode &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) !special_file(realinode->i_mode) && mask & MAY_WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) mask &= ~(MAY_WRITE | MAY_APPEND);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /* Make sure mounter can read file for copy up later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) mask |= MAY_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) err = inode_permission(realinode, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) ovl_revert_creds(inode->i_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static const char *ovl_get_link(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct delayed_call *done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) const char *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return ERR_PTR(-ECHILD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) p = vfs_get_link(ovl_dentry_real(dentry), done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return p;
^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) bool ovl_is_private_xattr(struct super_block *sb, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return strncmp(name, OVL_XATTR_PREFIX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) sizeof(OVL_XATTR_PREFIX) - 1) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) const void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) struct dentry *upperdentry = ovl_i_dentry_upper(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) err = ovl_want_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!value && !upperdentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) err = vfs_getxattr(realdentry, name, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) goto out_drop_write;
^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) if (!upperdentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) err = ovl_copy_up(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) goto out_drop_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) realdentry = ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) err = vfs_setxattr(realdentry, name, value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) WARN_ON(flags != XATTR_REPLACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) err = vfs_removexattr(realdentry, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) /* copy c/mtime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ovl_copyattr(d_inode(realdentry), inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) out_drop_write:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) ovl_drop_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) void *value, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) struct dentry *realdentry =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) res = __vfs_getxattr(realdentry, d_inode(realdentry), name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) value, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static bool ovl_can_list(struct super_block *sb, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) /* Never list private (.overlay) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (ovl_is_private_xattr(sb, s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) /* List all non-trusted xatts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /* list other trusted for superuser only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
^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) ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct dentry *realdentry = ovl_dentry_real(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) char *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) res = vfs_listxattr(realdentry, list, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (res <= 0 || size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* filter out private xattrs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) for (s = list, len = res; len;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) size_t slen = strnlen(s, len) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /* underlying fs providing us with an broken xattr list? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (WARN_ON(slen > len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) len -= slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (!ovl_can_list(dentry->d_sb, s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) res -= slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) memmove(s, s + slen, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) s += slen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) struct posix_acl *ovl_get_acl(struct inode *inode, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct inode *realinode = ovl_inode_real(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) old_cred = ovl_override_creds(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) acl = get_acl(realinode, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) ovl_revert_creds(inode->i_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (flags & S_ATIME) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct ovl_fs *ofs = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct path upperpath = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) .mnt = ovl_upper_mnt(ofs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (upperpath.dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) touch_atime(&upperpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) inode->i_atime = d_inode(upperpath.dentry)->i_atime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) u64 start, u64 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) struct inode *realinode = ovl_inode_real(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) if (!realinode->i_op->fiemap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) old_cred = ovl_override_creds(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) err = realinode->i_op->fiemap(realinode, fieinfo, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) ovl_revert_creds(inode->i_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) static const struct inode_operations ovl_file_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) .setattr = ovl_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) .permission = ovl_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) .getattr = ovl_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) .listxattr = ovl_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) .get_acl = ovl_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) .update_time = ovl_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) .fiemap = ovl_fiemap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) static const struct inode_operations ovl_symlink_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) .setattr = ovl_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .get_link = ovl_get_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) .getattr = ovl_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) .listxattr = ovl_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) .update_time = ovl_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) static const struct inode_operations ovl_special_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) .setattr = ovl_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) .permission = ovl_permission,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) .getattr = ovl_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) .listxattr = ovl_listxattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) .get_acl = ovl_get_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) .update_time = ovl_update_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) static const struct address_space_operations ovl_aops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) .direct_IO = noop_direct_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * It is possible to stack overlayfs instance on top of another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * overlayfs instance as lower layer. We need to annotate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * stackable i_mutex locks according to stack level of the super
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * block instance. An overlayfs instance can never be in stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * depth 0 (there is always a real fs below it). An overlayfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * For example, here is a snip from /proc/lockdep_chains after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * dir_iterate of nested overlayfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * [...] &type->i_mutex_dir_key (stack_depth=0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * This chain is valid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * - inode->i_rwsem (inode_lock[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) * - OVL_I(inode)->lock (ovl_inode_lock[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * And this chain is valid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * - inode->i_rwsem (inode_lock[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) * - OVL_I(inode)->lock (ovl_inode_lock[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * - lowerinode->i_rwsem (inode_lock[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * held, because it is in reverse order of the non-nested case using the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * upper fs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * - inode->i_rwsem (inode_lock[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * - OVL_I(inode)->lock (ovl_inode_lock[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) #ifdef CONFIG_LOCKDEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int depth = inode->i_sb->s_stack_depth - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (S_ISDIR(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) static void ovl_next_ino(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) struct ovl_fs *ofs = inode->i_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (unlikely(!inode->i_ino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) int xinobits = ovl_xino_bits(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) unsigned int xinoshift = 64 - xinobits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) * When d_ino is consistent with st_ino (samefs or i_ino has enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * bits to encode layer), set the same value used for st_ino to i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * so inode number exposed via /proc/locks and a like will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * consistent with d_ino and st_ino values. An i_ino value inconsistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * with d_ino also causes nfsd readdirplus to fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) inode->i_ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (ovl_same_fs(inode->i_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) } else if (xinobits && likely(!(ino >> xinoshift))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) inode->i_ino |= (unsigned long)fsid << (xinoshift + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) * For directory inodes on non-samefs with xino disabled or xino
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) * overflow, we allocate a non-persistent inode number, to be used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) * resolving st_ino collisions in ovl_map_dev_ino().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) * To avoid ino collision with legitimate xino values from upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * layer (fsid 0), use the lowest xinobit to map the non
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * persistent inode numbers to the unified st_ino address space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) ovl_next_ino(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (xinobits) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) inode->i_ino &= ~0UL >> xinobits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) inode->i_ino |= 1UL << xinoshift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) unsigned long ino, int fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) struct inode *realinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (oip->upperdentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) OVL_I(inode)->__upperdentry = oip->upperdentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (oip->lowerpath && oip->lowerpath->dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) OVL_I(inode)->lower = igrab(d_inode(oip->lowerpath->dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (oip->lowerdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) OVL_I(inode)->lowerdata = igrab(d_inode(oip->lowerdata));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) realinode = ovl_inode_real(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) ovl_copyattr(realinode, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ovl_copyflags(realinode, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) ovl_map_ino(inode, ino, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) inode->i_mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) inode->i_flags |= S_NOCMTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) #ifdef CONFIG_FS_POSIX_ACL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) ovl_lockdep_annotate_inode_mutex_key(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) switch (mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) inode->i_op = &ovl_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) inode->i_fop = &ovl_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) inode->i_mapping->a_ops = &ovl_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) inode->i_op = &ovl_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) inode->i_fop = &ovl_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) inode->i_op = &ovl_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) inode->i_op = &ovl_special_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) init_special_inode(inode, mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) * With inodes index enabled, an overlay inode nlink counts the union of upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) * upper inode, the following nlink modifying operations can happen:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) * 1. Lower hardlink copy up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) * 2. Upper hardlink created, unlinked or renamed over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * 3. Lower hardlink whiteout or renamed over
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * For the first, copy up case, the union nlink does not change, whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) * operation succeeds or fails, but the upper inode nlink may change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) * Therefore, before copy up, we store the union nlink value relative to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) * lower inode nlink in the index inode xattr trusted.overlay.nlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * For the second, upper hardlink case, the union nlink should be incremented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * or decremented IFF the operation succeeds, aligned with nlink change of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * upper inode. Therefore, before link/unlink/rename, we store the union nlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * value relative to the upper inode nlink in the index inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * For the last, lower cover up case, we simplify things by preceding the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * whiteout or cover up with copy up. This makes sure that there is an index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * upper inode where the nlink xattr can be stored before the copied up upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * entry is unlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) #define OVL_NLINK_ADD_UPPER (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * On-disk format for indexed nlink:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * nlink relative to the upper inode - "U[+-]NUM"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * nlink relative to the lower inode - "L[+-]NUM"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) static int ovl_set_nlink_common(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) struct dentry *realdentry, const char *format)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct inode *realinode = d_inode(realdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) char buf[13];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) len = snprintf(buf, sizeof(buf), format,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) (int) (inode->i_nlink - realinode->i_nlink));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (WARN_ON(len >= sizeof(buf)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return ovl_do_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) OVL_XATTR_NLINK, buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) int ovl_set_nlink_upper(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) int ovl_set_nlink_lower(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct dentry *upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) unsigned int fallback)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int nlink_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) int nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) char buf[13];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) err = ovl_do_getxattr(ofs, upperdentry, OVL_XATTR_NLINK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) &buf, sizeof(buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) buf[err] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) if ((buf[0] != 'L' && buf[0] != 'U') ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) (buf[1] != '+' && buf[1] != '-'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) err = kstrtoint(buf + 1, 10, &nlink_diff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) nlink += nlink_diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (nlink <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) return nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) upperdentry, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) return fallback;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) ovl_fill_inode(inode, mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static int ovl_inode_test(struct inode *inode, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) return inode->i_private == data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) static int ovl_inode_set(struct inode *inode, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) inode->i_private = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) struct dentry *upperdentry, bool strict)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) * For directories, @strict verify from lookup path performs consistency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) * checks, so NULL lower/upper in dentry must match NULL lower/upper in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) * inode. Non @strict verify from NFS handle decode path passes NULL for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) * 'unknown' lower/upper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) if (S_ISDIR(inode->i_mode) && strict) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) /* Real lower dir moved to upper layer under us? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (!lowerdentry && ovl_inode_lower(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) /* Lookup of an uncovered redirect origin? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (!upperdentry && ovl_inode_upper(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) * This happens when finding a copied up overlay inode for a renamed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) * or hardlinked overlay dentry and lower dentry cannot be followed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) * by origin because lower fs does not support file handles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * This happens when finding a lower alias for a copied up hard link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) bool is_upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) struct inode *inode, *key = d_inode(real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (!ovl_verify_inode(inode, is_upper ? NULL : real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) is_upper ? real : NULL, false)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) struct inode *key = d_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) struct inode *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) bool res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (!trap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) !ovl_inode_lower(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) iput(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * Create an inode cache entry for layer root dir, that will intentionally
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * fail ovl_verify_inode(), so any lookup that will find some layer root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * will fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) struct inode *key = d_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) struct inode *trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (!d_is_dir(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) return ERR_PTR(-ENOTDIR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) ovl_inode_set, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (!trap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if (!(trap->i_state & I_NEW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) /* Conflicting layer roots? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) iput(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return ERR_PTR(-ELOOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) trap->i_mode = S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) trap->i_flags = S_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) unlock_new_inode(trap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) return trap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * Does overlay inode need to be hashed by lower inode?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) struct dentry *lower, bool index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) /* No, if pure upper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (!lower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) /* Yes, if already indexed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /* Yes, if won't be copied up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) if (!ovl_upper_mnt(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) /* No, if lower hardlink is or will be broken on copy up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) if ((upper || !ovl_indexdir(sb)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) /* No, if non-indexed upper with NFS export */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) if (sb->s_export_op && upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) /* Otherwise, hash by lower inode for fsnotify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) struct inode *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) return newinode ? inode_insert5(newinode, (unsigned long) key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) ovl_inode_test, ovl_inode_set, key) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) iget5_locked(sb, (unsigned long) key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) ovl_inode_test, ovl_inode_set, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) struct inode *ovl_get_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) struct ovl_inode_params *oip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) struct ovl_fs *ofs = OVL_FS(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) struct dentry *upperdentry = oip->upperdentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) struct ovl_path *lowerpath = oip->lowerpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) oip->index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) int fsid = bylower ? lowerpath->layer->fsid : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) bool is_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) unsigned long ino = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) int err = oip->newinode ? -EEXIST : -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (!realinode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) realinode = d_inode(lowerdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * Copy up origin (lower) may exist for non-indexed upper, but we must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * not use lower as hash key if this is a broken hardlink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) is_dir = S_ISDIR(realinode->i_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) if (upperdentry || bylower) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct inode *key = d_inode(bylower ? lowerdentry :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) inode = ovl_iget5(sb, oip->newinode, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) if (!(inode->i_state & I_NEW)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) * Verify that the underlying files stored in the inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) * match those in the dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) true)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) dput(upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) kfree(oip->redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) /* Recalculate nlink for non-dir due to indexing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) if (!is_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) set_nlink(inode, nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) ino = key->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) /* Lower hardlink that will be broken on copy up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) inode = new_inode(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) ino = realinode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) fsid = lowerpath->layer->fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) ovl_inode_init(inode, oip, ino, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) if (upperdentry && ovl_is_impuredir(sb, upperdentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) ovl_set_flag(OVL_IMPURE, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (oip->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) ovl_set_flag(OVL_INDEX, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) OVL_I(inode)->redirect = oip->redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) if (bylower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) ovl_set_flag(OVL_CONST_INO, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) /* Check for non-merge dir that may have whiteouts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) if (((upperdentry && lowerdentry) || oip->numlower > 1) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) ovl_check_origin_xattr(ofs, upperdentry ?: lowerdentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) ovl_set_flag(OVL_WHITEOUTS, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) }
^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) if (inode->i_state & I_NEW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) pr_warn_ratelimited("failed to get inode (%i)\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) inode = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }