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