^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * fs/isofs/export.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) 2004 Paul Serice - The new inode scheme requires switching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * from iget() to iget5_locked() which means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * the NFS export operations have to be hand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * coded because the default routines rely on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * iget().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * The following files are helpful:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Documentation/filesystems/nfs/exporting.rst
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * fs/exportfs/expfs.c.
^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) #include "isofs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) isofs_export_iget(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) __u32 generation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (block == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) inode = isofs_iget(sb, block, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return ERR_CAST(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (generation && inode->i_generation != generation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) return d_obtain_alias(inode);
^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) /* This function is surprisingly simple. The trick is understanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * that "child" is always a directory. So, to find its parent, you
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * simply need to find its ".." entry, normalize its block and offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * and return the underlying inode. See the comments for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * isofs_normalize_block_and_offset(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static struct dentry *isofs_export_get_parent(struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned long parent_block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned long parent_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct inode *child_inode = d_inode(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct iso_directory_record *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct buffer_head * bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct dentry *rv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* "child" must always be a directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (!S_ISDIR(child_inode->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) printk(KERN_ERR "isofs: isofs_export_get_parent(): "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) "child is not a directory!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) rv = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* It is an invariant that the directory offset is zero. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * it is not zero, it means the directory failed to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * normalized for some reason. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (e_child_inode->i_iget5_offset != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) printk(KERN_ERR "isofs: isofs_export_get_parent(): "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) "child directory not normalized!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) rv = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* The child inode has been normalized such that its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * i_iget5_block value points to the "." entry. Fortunately,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * the ".." entry is located in the same block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) parent_block = e_child_inode->i_iget5_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) /* Get the block in question. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) bh = sb_bread(child_inode->i_sb, parent_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) rv = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* This is the "." entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) de = (struct iso_directory_record*)bh->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* The ".." entry is always the second entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) parent_offset = (unsigned long)isonum_711(de->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) de = (struct iso_directory_record*)(bh->b_data + parent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* Verify it is in fact the ".." entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) printk(KERN_ERR "isofs: Unable to find the \"..\" "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) "directory for NFS.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) rv = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /* Normalize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) parent_offset));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return rv;
^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) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) isofs_export_encode_fh(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) __u32 *fh32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) int *max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct iso_inode_info * ei = ISOFS_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int len = *max_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) int type = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) __u16 *fh16 = (__u16*)fh32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * WARNING: max_len is 5 for NFSv2. Because of this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * limitation, we use the lower 16 bits of fh32[1] to hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * offset of the inode and the upper 16 bits of fh32[1] to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * hold the offset of the parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (parent && (len < 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) *max_len = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else if (len < 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *max_len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) len = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) fh32[0] = ei->i_iget5_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) fh16[2] = (__u16)ei->i_iget5_offset; /* fh16 [sic] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) fh16[3] = 0; /* avoid leaking uninitialized data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) fh32[2] = inode->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (parent) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct iso_inode_info *eparent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) eparent = ISOFS_I(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) fh32[3] = eparent->i_iget5_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) fh16[3] = (__u16)eparent->i_iget5_offset; /* fh16 [sic] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) fh32[4] = parent->i_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) len = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) type = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *max_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct isofs_fid {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) u32 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) u16 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u16 parent_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u32 generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u32 parent_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) u32 parent_generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static struct dentry *isofs_fh_to_dentry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct fid *fid, int fh_len, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct isofs_fid *ifid = (struct isofs_fid *)fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (fh_len < 3 || fh_type > 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return isofs_export_iget(sb, ifid->block, ifid->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) ifid->generation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static struct dentry *isofs_fh_to_parent(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct fid *fid, int fh_len, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) struct isofs_fid *ifid = (struct isofs_fid *)fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (fh_len < 2 || fh_type != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return isofs_export_iget(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) fh_len > 2 ? ifid->parent_block : 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) ifid->parent_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) fh_len > 4 ? ifid->parent_generation : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) const struct export_operations isofs_export_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) .encode_fh = isofs_export_encode_fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) .fh_to_dentry = isofs_fh_to_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) .fh_to_parent = isofs_fh_to_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .get_parent = isofs_export_get_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) };