export.c 5.6 KB

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