export.c 5.9 KB

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