export.c 5.1 KB

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