export.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. static struct dentry *
  43. isofs_export_get_dentry(struct super_block *sb, void *vobjp)
  44. {
  45. __u32 *objp = vobjp;
  46. unsigned long block = objp[0];
  47. unsigned long offset = objp[1];
  48. __u32 generation = objp[2];
  49. return isofs_export_iget(sb, block, offset, generation);
  50. }
  51. /* This function is surprisingly simple. The trick is understanding
  52. * that "child" is always a directory. So, to find its parent, you
  53. * simply need to find its ".." entry, normalize its block and offset,
  54. * and return the underlying inode. See the comments for
  55. * isofs_normalize_block_and_offset(). */
  56. static struct dentry *isofs_export_get_parent(struct dentry *child)
  57. {
  58. unsigned long parent_block = 0;
  59. unsigned long parent_offset = 0;
  60. struct inode *child_inode = child->d_inode;
  61. struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
  62. struct inode *parent_inode = NULL;
  63. struct iso_directory_record *de = NULL;
  64. struct buffer_head * bh = NULL;
  65. struct dentry *rv = NULL;
  66. /* "child" must always be a directory. */
  67. if (!S_ISDIR(child_inode->i_mode)) {
  68. printk(KERN_ERR "isofs: isofs_export_get_parent(): "
  69. "child is not a directory!\n");
  70. rv = ERR_PTR(-EACCES);
  71. goto out;
  72. }
  73. /* It is an invariant that the directory offset is zero. If
  74. * it is not zero, it means the directory failed to be
  75. * normalized for some reason. */
  76. if (e_child_inode->i_iget5_offset != 0) {
  77. printk(KERN_ERR "isofs: isofs_export_get_parent(): "
  78. "child directory not normalized!\n");
  79. rv = ERR_PTR(-EACCES);
  80. goto out;
  81. }
  82. /* The child inode has been normalized such that its
  83. * i_iget5_block value points to the "." entry. Fortunately,
  84. * the ".." entry is located in the same block. */
  85. parent_block = e_child_inode->i_iget5_block;
  86. /* Get the block in question. */
  87. bh = sb_bread(child_inode->i_sb, parent_block);
  88. if (bh == NULL) {
  89. rv = ERR_PTR(-EACCES);
  90. goto out;
  91. }
  92. /* This is the "." entry. */
  93. de = (struct iso_directory_record*)bh->b_data;
  94. /* The ".." entry is always the second entry. */
  95. parent_offset = (unsigned long)isonum_711(de->length);
  96. de = (struct iso_directory_record*)(bh->b_data + parent_offset);
  97. /* Verify it is in fact the ".." entry. */
  98. if ((isonum_711(de->name_len) != 1) || (de->name[0] != 1)) {
  99. printk(KERN_ERR "isofs: Unable to find the \"..\" "
  100. "directory for NFS.\n");
  101. rv = ERR_PTR(-EACCES);
  102. goto out;
  103. }
  104. /* Normalize */
  105. isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
  106. /* Get the inode. */
  107. parent_inode = isofs_iget(child_inode->i_sb,
  108. parent_block,
  109. parent_offset);
  110. if (parent_inode == NULL) {
  111. rv = ERR_PTR(-EACCES);
  112. goto out;
  113. }
  114. /* Allocate the dentry. */
  115. rv = d_alloc_anon(parent_inode);
  116. if (rv == NULL) {
  117. rv = ERR_PTR(-ENOMEM);
  118. goto out;
  119. }
  120. out:
  121. if (bh) {
  122. brelse(bh);
  123. }
  124. return rv;
  125. }
  126. static int
  127. isofs_export_encode_fh(struct dentry *dentry,
  128. __u32 *fh32,
  129. int *max_len,
  130. int connectable)
  131. {
  132. struct inode * inode = dentry->d_inode;
  133. struct iso_inode_info * ei = ISOFS_I(inode);
  134. int len = *max_len;
  135. int type = 1;
  136. __u16 *fh16 = (__u16*)fh32;
  137. /*
  138. * WARNING: max_len is 5 for NFSv2. Because of this
  139. * limitation, we use the lower 16 bits of fh32[1] to hold the
  140. * offset of the inode and the upper 16 bits of fh32[1] to
  141. * hold the offset of the parent.
  142. */
  143. if (len < 3 || (connectable && len < 5))
  144. return 255;
  145. len = 3;
  146. fh32[0] = ei->i_iget5_block;
  147. fh16[2] = (__u16)ei->i_iget5_offset; /* fh16 [sic] */
  148. fh32[2] = inode->i_generation;
  149. if (connectable && !S_ISDIR(inode->i_mode)) {
  150. struct inode *parent;
  151. struct iso_inode_info *eparent;
  152. spin_lock(&dentry->d_lock);
  153. parent = dentry->d_parent->d_inode;
  154. eparent = ISOFS_I(parent);
  155. fh32[3] = eparent->i_iget5_block;
  156. fh16[3] = (__u16)eparent->i_iget5_offset; /* fh16 [sic] */
  157. fh32[4] = parent->i_generation;
  158. spin_unlock(&dentry->d_lock);
  159. len = 5;
  160. type = 2;
  161. }
  162. *max_len = len;
  163. return type;
  164. }
  165. static struct dentry *
  166. isofs_export_decode_fh(struct super_block *sb,
  167. __u32 *fh32,
  168. int fh_len,
  169. int fileid_type,
  170. int (*acceptable)(void *context, struct dentry *de),
  171. void *context)
  172. {
  173. __u16 *fh16 = (__u16*)fh32;
  174. __u32 child[3]; /* The child is what triggered all this. */
  175. __u32 parent[3]; /* The parent is just along for the ride. */
  176. if (fh_len < 3 || fileid_type > 2)
  177. return NULL;
  178. child[0] = fh32[0];
  179. child[1] = fh16[2]; /* fh16 [sic] */
  180. child[2] = fh32[2];
  181. parent[0] = 0;
  182. parent[1] = 0;
  183. parent[2] = 0;
  184. if (fileid_type == 2) {
  185. if (fh_len > 2) parent[0] = fh32[3];
  186. parent[1] = fh16[3]; /* fh16 [sic] */
  187. if (fh_len > 4) parent[2] = fh32[4];
  188. }
  189. return sb->s_export_op->find_exported_dentry(sb, child, parent,
  190. acceptable, context);
  191. }
  192. struct export_operations isofs_export_ops = {
  193. .decode_fh = isofs_export_decode_fh,
  194. .encode_fh = isofs_export_encode_fh,
  195. .get_dentry = isofs_export_get_dentry,
  196. .get_parent = isofs_export_get_parent,
  197. };