18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * fs/isofs/export.c 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * (C) 2004 Paul Serice - The new inode scheme requires switching 68c2ecf20Sopenharmony_ci * from iget() to iget5_locked() which means 78c2ecf20Sopenharmony_ci * the NFS export operations have to be hand 88c2ecf20Sopenharmony_ci * coded because the default routines rely on 98c2ecf20Sopenharmony_ci * iget(). 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * The following files are helpful: 128c2ecf20Sopenharmony_ci * 138c2ecf20Sopenharmony_ci * Documentation/filesystems/nfs/exporting.rst 148c2ecf20Sopenharmony_ci * fs/exportfs/expfs.c. 158c2ecf20Sopenharmony_ci */ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci#include "isofs.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistatic struct dentry * 208c2ecf20Sopenharmony_ciisofs_export_iget(struct super_block *sb, 218c2ecf20Sopenharmony_ci unsigned long block, 228c2ecf20Sopenharmony_ci unsigned long offset, 238c2ecf20Sopenharmony_ci __u32 generation) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci struct inode *inode; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci if (block == 0) 288c2ecf20Sopenharmony_ci return ERR_PTR(-ESTALE); 298c2ecf20Sopenharmony_ci inode = isofs_iget(sb, block, offset); 308c2ecf20Sopenharmony_ci if (IS_ERR(inode)) 318c2ecf20Sopenharmony_ci return ERR_CAST(inode); 328c2ecf20Sopenharmony_ci if (generation && inode->i_generation != generation) { 338c2ecf20Sopenharmony_ci iput(inode); 348c2ecf20Sopenharmony_ci return ERR_PTR(-ESTALE); 358c2ecf20Sopenharmony_ci } 368c2ecf20Sopenharmony_ci return d_obtain_alias(inode); 378c2ecf20Sopenharmony_ci} 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* This function is surprisingly simple. The trick is understanding 408c2ecf20Sopenharmony_ci * that "child" is always a directory. So, to find its parent, you 418c2ecf20Sopenharmony_ci * simply need to find its ".." entry, normalize its block and offset, 428c2ecf20Sopenharmony_ci * and return the underlying inode. See the comments for 438c2ecf20Sopenharmony_ci * isofs_normalize_block_and_offset(). */ 448c2ecf20Sopenharmony_cistatic struct dentry *isofs_export_get_parent(struct dentry *child) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci unsigned long parent_block = 0; 478c2ecf20Sopenharmony_ci unsigned long parent_offset = 0; 488c2ecf20Sopenharmony_ci struct inode *child_inode = d_inode(child); 498c2ecf20Sopenharmony_ci struct iso_inode_info *e_child_inode = ISOFS_I(child_inode); 508c2ecf20Sopenharmony_ci struct iso_directory_record *de = NULL; 518c2ecf20Sopenharmony_ci struct buffer_head * bh = NULL; 528c2ecf20Sopenharmony_ci struct dentry *rv = NULL; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* "child" must always be a directory. */ 558c2ecf20Sopenharmony_ci if (!S_ISDIR(child_inode->i_mode)) { 568c2ecf20Sopenharmony_ci printk(KERN_ERR "isofs: isofs_export_get_parent(): " 578c2ecf20Sopenharmony_ci "child is not a directory!\n"); 588c2ecf20Sopenharmony_ci rv = ERR_PTR(-EACCES); 598c2ecf20Sopenharmony_ci goto out; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci /* It is an invariant that the directory offset is zero. If 638c2ecf20Sopenharmony_ci * it is not zero, it means the directory failed to be 648c2ecf20Sopenharmony_ci * normalized for some reason. */ 658c2ecf20Sopenharmony_ci if (e_child_inode->i_iget5_offset != 0) { 668c2ecf20Sopenharmony_ci printk(KERN_ERR "isofs: isofs_export_get_parent(): " 678c2ecf20Sopenharmony_ci "child directory not normalized!\n"); 688c2ecf20Sopenharmony_ci rv = ERR_PTR(-EACCES); 698c2ecf20Sopenharmony_ci goto out; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci /* The child inode has been normalized such that its 738c2ecf20Sopenharmony_ci * i_iget5_block value points to the "." entry. Fortunately, 748c2ecf20Sopenharmony_ci * the ".." entry is located in the same block. */ 758c2ecf20Sopenharmony_ci parent_block = e_child_inode->i_iget5_block; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci /* Get the block in question. */ 788c2ecf20Sopenharmony_ci bh = sb_bread(child_inode->i_sb, parent_block); 798c2ecf20Sopenharmony_ci if (bh == NULL) { 808c2ecf20Sopenharmony_ci rv = ERR_PTR(-EACCES); 818c2ecf20Sopenharmony_ci goto out; 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci /* This is the "." entry. */ 858c2ecf20Sopenharmony_ci de = (struct iso_directory_record*)bh->b_data; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* The ".." entry is always the second entry. */ 888c2ecf20Sopenharmony_ci parent_offset = (unsigned long)isonum_711(de->length); 898c2ecf20Sopenharmony_ci de = (struct iso_directory_record*)(bh->b_data + parent_offset); 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* Verify it is in fact the ".." entry. */ 928c2ecf20Sopenharmony_ci if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) { 938c2ecf20Sopenharmony_ci printk(KERN_ERR "isofs: Unable to find the \"..\" " 948c2ecf20Sopenharmony_ci "directory for NFS.\n"); 958c2ecf20Sopenharmony_ci rv = ERR_PTR(-EACCES); 968c2ecf20Sopenharmony_ci goto out; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* Normalize */ 1008c2ecf20Sopenharmony_ci isofs_normalize_block_and_offset(de, &parent_block, &parent_offset); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block, 1038c2ecf20Sopenharmony_ci parent_offset)); 1048c2ecf20Sopenharmony_ci out: 1058c2ecf20Sopenharmony_ci if (bh) 1068c2ecf20Sopenharmony_ci brelse(bh); 1078c2ecf20Sopenharmony_ci return rv; 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_cistatic int 1118c2ecf20Sopenharmony_ciisofs_export_encode_fh(struct inode *inode, 1128c2ecf20Sopenharmony_ci __u32 *fh32, 1138c2ecf20Sopenharmony_ci int *max_len, 1148c2ecf20Sopenharmony_ci struct inode *parent) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci struct iso_inode_info * ei = ISOFS_I(inode); 1178c2ecf20Sopenharmony_ci int len = *max_len; 1188c2ecf20Sopenharmony_ci int type = 1; 1198c2ecf20Sopenharmony_ci __u16 *fh16 = (__u16*)fh32; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci /* 1228c2ecf20Sopenharmony_ci * WARNING: max_len is 5 for NFSv2. Because of this 1238c2ecf20Sopenharmony_ci * limitation, we use the lower 16 bits of fh32[1] to hold the 1248c2ecf20Sopenharmony_ci * offset of the inode and the upper 16 bits of fh32[1] to 1258c2ecf20Sopenharmony_ci * hold the offset of the parent. 1268c2ecf20Sopenharmony_ci */ 1278c2ecf20Sopenharmony_ci if (parent && (len < 5)) { 1288c2ecf20Sopenharmony_ci *max_len = 5; 1298c2ecf20Sopenharmony_ci return FILEID_INVALID; 1308c2ecf20Sopenharmony_ci } else if (len < 3) { 1318c2ecf20Sopenharmony_ci *max_len = 3; 1328c2ecf20Sopenharmony_ci return FILEID_INVALID; 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci len = 3; 1368c2ecf20Sopenharmony_ci fh32[0] = ei->i_iget5_block; 1378c2ecf20Sopenharmony_ci fh16[2] = (__u16)ei->i_iget5_offset; /* fh16 [sic] */ 1388c2ecf20Sopenharmony_ci fh16[3] = 0; /* avoid leaking uninitialized data */ 1398c2ecf20Sopenharmony_ci fh32[2] = inode->i_generation; 1408c2ecf20Sopenharmony_ci if (parent) { 1418c2ecf20Sopenharmony_ci struct iso_inode_info *eparent; 1428c2ecf20Sopenharmony_ci eparent = ISOFS_I(parent); 1438c2ecf20Sopenharmony_ci fh32[3] = eparent->i_iget5_block; 1448c2ecf20Sopenharmony_ci fh16[3] = (__u16)eparent->i_iget5_offset; /* fh16 [sic] */ 1458c2ecf20Sopenharmony_ci fh32[4] = parent->i_generation; 1468c2ecf20Sopenharmony_ci len = 5; 1478c2ecf20Sopenharmony_ci type = 2; 1488c2ecf20Sopenharmony_ci } 1498c2ecf20Sopenharmony_ci *max_len = len; 1508c2ecf20Sopenharmony_ci return type; 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistruct isofs_fid { 1548c2ecf20Sopenharmony_ci u32 block; 1558c2ecf20Sopenharmony_ci u16 offset; 1568c2ecf20Sopenharmony_ci u16 parent_offset; 1578c2ecf20Sopenharmony_ci u32 generation; 1588c2ecf20Sopenharmony_ci u32 parent_block; 1598c2ecf20Sopenharmony_ci u32 parent_generation; 1608c2ecf20Sopenharmony_ci}; 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic struct dentry *isofs_fh_to_dentry(struct super_block *sb, 1638c2ecf20Sopenharmony_ci struct fid *fid, int fh_len, int fh_type) 1648c2ecf20Sopenharmony_ci{ 1658c2ecf20Sopenharmony_ci struct isofs_fid *ifid = (struct isofs_fid *)fid; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (fh_len < 3 || fh_type > 2) 1688c2ecf20Sopenharmony_ci return NULL; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci return isofs_export_iget(sb, ifid->block, ifid->offset, 1718c2ecf20Sopenharmony_ci ifid->generation); 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic struct dentry *isofs_fh_to_parent(struct super_block *sb, 1758c2ecf20Sopenharmony_ci struct fid *fid, int fh_len, int fh_type) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct isofs_fid *ifid = (struct isofs_fid *)fid; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci if (fh_len < 2 || fh_type != 2) 1808c2ecf20Sopenharmony_ci return NULL; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci return isofs_export_iget(sb, 1838c2ecf20Sopenharmony_ci fh_len > 2 ? ifid->parent_block : 0, 1848c2ecf20Sopenharmony_ci ifid->parent_offset, 1858c2ecf20Sopenharmony_ci fh_len > 4 ? ifid->parent_generation : 0); 1868c2ecf20Sopenharmony_ci} 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ciconst struct export_operations isofs_export_ops = { 1898c2ecf20Sopenharmony_ci .encode_fh = isofs_export_encode_fh, 1908c2ecf20Sopenharmony_ci .fh_to_dentry = isofs_fh_to_dentry, 1918c2ecf20Sopenharmony_ci .fh_to_parent = isofs_fh_to_parent, 1928c2ecf20Sopenharmony_ci .get_parent = isofs_export_get_parent, 1938c2ecf20Sopenharmony_ci}; 194