export.c 5.5 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 (inode == NULL)
  28. return ERR_PTR(-ENOMEM);
  29. if (is_bad_inode(inode)
  30. || (generation && inode->i_generation != generation))
  31. {
  32. iput(inode);
  33. return ERR_PTR(-ESTALE);
  34. }
  35. result = d_alloc_anon(inode);
  36. if (!result) {
  37. iput(inode);
  38. return ERR_PTR(-ENOMEM);
  39. }
  40. return result;
  41. }
  42. /* This function is surprisingly simple. The trick is understanding
  43. * that "child" is always a directory. So, to find its parent, you
  44. * simply need to find its ".." entry, normalize its block and offset,
  45. * and return the underlying inode. See the comments for
  46. * isofs_normalize_block_and_offset(). */
  47. static struct dentry *isofs_export_get_parent(struct dentry *child)
  48. {
  49. unsigned long parent_block = 0;
  50. unsigned long parent_offset = 0;
  51. struct inode *child_inode = child->d_inode;
  52. struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
  53. struct inode *parent_inode = NULL;
  54. struct iso_directory_record *de = NULL;
  55. struct buffer_head * bh = NULL;
  56. struct dentry *rv = NULL;
  57. /* "child" must always be a directory. */
  58. if (!S_ISDIR(child_inode->i_mode)) {
  59. printk(KERN_ERR "isofs: isofs_export_get_parent(): "
  60. "child is not a directory!\n");
  61. rv = ERR_PTR(-EACCES);
  62. goto out;
  63. }
  64. /* It is an invariant that the directory offset is zero. If
  65. * it is not zero, it means the directory failed to be
  66. * normalized for some reason. */
  67. if (e_child_inode->i_iget5_offset != 0) {
  68. printk(KERN_ERR "isofs: isofs_export_get_parent(): "
  69. "child directory not normalized!\n");
  70. rv = ERR_PTR(-EACCES);
  71. goto out;
  72. }
  73. /* The child inode has been normalized such that its
  74. * i_iget5_block value points to the "." entry. Fortunately,
  75. * the ".." entry is located in the same block. */
  76. parent_block = e_child_inode->i_iget5_block;
  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. /* This is the "." entry. */
  84. de = (struct iso_directory_record*)bh->b_data;
  85. /* The ".." entry is always the second entry. */
  86. parent_offset = (unsigned long)isonum_711(de->length);
  87. de = (struct iso_directory_record*)(bh->b_data + parent_offset);
  88. /* Verify it is in fact the ".." entry. */
  89. if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
  90. printk(KERN_ERR "isofs: Unable to find the \"..\" "
  91. "directory for NFS.\n");
  92. rv = ERR_PTR(-EACCES);
  93. goto out;
  94. }
  95. /* Normalize */
  96. isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
  97. /* Get the inode. */
  98. parent_inode = isofs_iget(child_inode->i_sb,
  99. parent_block,
  100. parent_offset);
  101. if (parent_inode == NULL) {
  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. };