^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) * Copyright (C) Neil Brown 2002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) Christoph Hellwig 2007
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file contains the code mapping from inodes to NFS file handles,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * and for mapping back from file handles to dentries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * For details on why we do all the strange and hairy things in here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * take a look at Documentation/filesystems/nfs/exporting.rst.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define dprintk(fmt, args...) do{}while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static int get_name(const struct path *path, char *name, struct dentry *child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static int exportfs_get_name(struct vfsmount *mnt, struct dentry *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char *name, struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) const struct export_operations *nop = dir->d_sb->s_export_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct path path = {.mnt = mnt, .dentry = dir};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (nop->get_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return nop->get_name(dir, name, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return get_name(&path, name, child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Check if the dentry or any of it's aliases is acceptable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) find_acceptable_alias(struct dentry *result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int (*acceptable)(void *context, struct dentry *dentry),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct dentry *dentry, *toput = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (acceptable(context, result))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) inode = result->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (toput)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dput(toput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (dentry != result && acceptable(context, dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) toput = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (toput)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dput(toput);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static bool dentry_connected(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) while (dentry->d_flags & DCACHE_DISCONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct dentry *parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) if (dentry == parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) dentry = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static void clear_disconnected(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) while (dentry->d_flags & DCACHE_DISCONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct dentry *parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) WARN_ON_ONCE(IS_ROOT(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) dentry->d_flags &= ~DCACHE_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) dentry = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * Reconnect a directory dentry with its parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * This can return a dentry, or NULL, or an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * In the first case the returned dentry is the parent of the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * dentry, and may itself need to be reconnected to its parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * In the NULL case, a concurrent VFS operation has either renamed or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * removed this directory. The concurrent operation has reconnected our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * dentry, so we no longer need to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static struct dentry *reconnect_one(struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct dentry *dentry, char *nbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct dentry *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct dentry *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) parent = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) inode_lock(dentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (mnt->mnt_sb->s_export_op->get_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) parent = mnt->mnt_sb->s_export_op->get_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) inode_unlock(dentry->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (IS_ERR(parent)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dprintk("%s: get_parent of %ld failed, err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) __func__, dentry->d_inode->i_ino, PTR_ERR(parent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return parent;
^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) dprintk("%s: find name of %lu in %lu\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) dentry->d_inode->i_ino, parent->d_inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) err = exportfs_get_name(mnt, parent, nbuf, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (err == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) goto out_reconnected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) dprintk("%s: found name: %s\n", __func__, nbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) tmp = lookup_one_len_unlocked(nbuf, parent, strlen(nbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (IS_ERR(tmp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) dprintk("%s: lookup failed: %d\n", __func__, PTR_ERR(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) err = PTR_ERR(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (tmp != dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Somebody has renamed it since exportfs_get_name();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * great, since it could've only been renamed if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * got looked up and thus connected, and it would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * remain connected afterwards. We are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) dput(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) goto out_reconnected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) dput(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (IS_ROOT(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) out_reconnected:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * Someone must have renamed our entry into another parent, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * which case it has been reconnected by the rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Or someone removed it entirely, in which case filehandle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * lookup will succeed but the directory is now IS_DEAD and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * subsequent operations on it will fail.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * Alternatively, maybe there was no race at all, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * filesystem is just corrupt and gave us a parent that doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * actually contain any entry pointing to this inode. So,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * double check that this worked and return -ESTALE if not:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!dentry_connected(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * Make sure target_dir is fully connected to the dentry tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * On successful return, DCACHE_DISCONNECTED will be cleared on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * target_dir, and target_dir->d_parent->...->d_parent will reach the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * root of the filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * Whenever DCACHE_DISCONNECTED is unset, target_dir is fully connected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * But the converse is not true: target_dir may have DCACHE_DISCONNECTED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * set but already be connected. In that case we'll verify the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * connection to root and then clear the flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * Note that target_dir could be removed by a concurrent operation. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * that case reconnect_path may still succeed with target_dir fully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * connected, but further operations using the filehandle will fail when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * necessary (due to S_DEAD being set on the directory).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) reconnect_path(struct vfsmount *mnt, struct dentry *target_dir, char *nbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) struct dentry *dentry, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dentry = dget(target_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) while (dentry->d_flags & DCACHE_DISCONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) BUG_ON(dentry == mnt->mnt_sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (IS_ROOT(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) parent = reconnect_one(mnt, dentry, nbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) parent = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (IS_ERR(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return PTR_ERR(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) dentry = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) clear_disconnected(target_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct getdents_callback {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) struct dir_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) char *name; /* name that was found. It already points to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) buffer NAME_MAX+1 is size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) u64 ino; /* the inum we are looking for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int found; /* inode matched? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int sequence; /* sequence counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * A rather strange filldir function to capture
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * the name matching the specified inode number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int filldir_one(struct dir_context *ctx, const char *name, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) loff_t pos, u64 ino, unsigned int d_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct getdents_callback *buf =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) container_of(ctx, struct getdents_callback, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) buf->sequence++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (buf->ino == ino && len <= NAME_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) memcpy(buf->name, name, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) buf->name[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) buf->found = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) result = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * get_name - default export_operations->get_name function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * @path: the directory in which to find a name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * @child: the dentry for the child directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * calls readdir on the parent until it finds an entry with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * the same inode number as the child, and returns that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int get_name(const struct path *path, char *name, struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) const struct cred *cred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct inode *dir = path->dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct kstat stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct path child_path = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) .mnt = path->mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) .dentry = child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct getdents_callback buffer = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) .ctx.actor = filldir_one,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .name = name,
^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) error = -ENOTDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (!dir || !S_ISDIR(dir->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!dir->i_fop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * inode->i_ino is unsigned long, kstat->ino is u64, so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * former would be insufficient on 32-bit hosts when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * filesystem supports 64-bit inode numbers. So we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * actually call ->getattr, not just read i_ino:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) error = vfs_getattr_nosec(&child_path, &stat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) STATX_INO, AT_STATX_SYNC_AS_STAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) buffer.ino = stat.ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * Open the directory ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) file = dentry_open(path, O_RDONLY, cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) error = PTR_ERR(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (IS_ERR(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!file->f_op->iterate && !file->f_op->iterate_shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) goto out_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) buffer.sequence = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) int old_seq = buffer.sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) error = iterate_dir(file, &buffer.ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (buffer.found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) error = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (old_seq == buffer.sequence)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) out_close:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * export_encode_fh - default export_operations->encode_fh function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * @inode: the object to encode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * @fid: where to store the file handle fragment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * @max_len: maximum length to store there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * @parent: parent directory inode, if wanted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * This default encode_fh function assumes that the 32 inode number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * is suitable for locating an inode, and that the generation number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * can be used to check that it is still valid. It places them in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * filehandle fragment where export_decode_fh expects to find them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static int export_encode_fh(struct inode *inode, struct fid *fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) int *max_len, struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int len = *max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int type = FILEID_INO32_GEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (parent && (len < 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) *max_len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) } else if (len < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) *max_len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) len = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) fid->i32.ino = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) fid->i32.gen = inode->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) fid->i32.parent_ino = parent->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) fid->i32.parent_gen = parent->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) len = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) type = FILEID_INO32_GEN_PARENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) *max_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) int *max_len, struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) const struct export_operations *nop = inode->i_sb->s_export_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (nop && nop->encode_fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) return nop->encode_fh(inode, fid->raw, max_len, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return export_encode_fh(inode, fid, max_len, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) EXPORT_SYMBOL_GPL(exportfs_encode_inode_fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) int exportfs_encode_fh(struct dentry *dentry, struct fid *fid, int *max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int connectable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct dentry *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct inode *inode = dentry->d_inode, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (connectable && !S_ISDIR(inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) p = dget_parent(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * note that while p might've ceased to be our parent already,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * it's still pinned by and still positive.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) parent = p->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) error = exportfs_encode_inode_fh(inode, fid, max_len, parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) dput(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) EXPORT_SYMBOL_GPL(exportfs_encode_fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int fh_len, int fileid_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int (*acceptable)(void *, struct dentry *), void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) const struct export_operations *nop = mnt->mnt_sb->s_export_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) struct dentry *result, *alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) char nbuf[NAME_MAX+1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * Try to get any dentry for the given file handle from the filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (!nop || !nop->fh_to_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (PTR_ERR(result) == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return ERR_CAST(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (IS_ERR_OR_NULL(result))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) return ERR_PTR(-ESTALE);
^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) * If no acceptance criteria was specified by caller, a disconnected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * dentry is also accepatable. Callers may use this mode to query if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * file handle is stale or to get a reference to an inode without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * risking the high overhead caused by directory reconnect.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (!acceptable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (d_is_dir(result)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * This request is for a directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * On the positive side there is only one dentry for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * directory inode. On the negative side this implies that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * to ensure our dentry is connected all the way up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * filesystem root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (result->d_flags & DCACHE_DISCONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) err = reconnect_path(mnt, result, nbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (!acceptable(context, result)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * It's not a directory. Life is a little more complicated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct dentry *target_dir, *nresult;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * See if either the dentry we just got from the filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * or any alias for it is acceptable. This is always true
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * if this filesystem is exported without the subtreecheck
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * option. If the filesystem is exported with the subtree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * check option there's a fair chance we need to look at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * the parent directory in the file handle and make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * it's connected to the filesystem root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) alias = find_acceptable_alias(result, acceptable, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (alias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * Try to extract a dentry for the parent directory from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * file handle. If this fails we'll have to give up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (!nop->fh_to_parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) fh_len, fileid_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) if (!target_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) err = PTR_ERR(target_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (IS_ERR(target_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * And as usual we need to make sure the parent directory is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * connected to the filesystem root. The VFS really doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * like disconnected directories..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) err = reconnect_path(mnt, target_dir, nbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) dput(target_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * Now that we've got both a well-connected parent and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * dentry for the inode we're after, make sure that our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * inode is actually connected to the parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = exportfs_get_name(mnt, target_dir, nbuf, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) dput(target_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) goto err_result;
^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) inode_lock(target_dir->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (!IS_ERR(nresult)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (unlikely(nresult->d_inode != result->d_inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) dput(nresult);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) nresult = ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) inode_unlock(target_dir->d_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * At this point we are done with the parent, but it's pinned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * by the child dentry anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) dput(target_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (IS_ERR(nresult)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) err = PTR_ERR(nresult);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) dput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) result = nresult;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * And finally make sure the dentry is actually acceptable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) * to NFSD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) alias = find_acceptable_alias(result, acceptable, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (!alias) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) goto err_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) err_result:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) dput(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (err != -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) EXPORT_SYMBOL_GPL(exportfs_decode_fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) MODULE_LICENSE("GPL");