export.c 5.1 KB

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