export.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "ceph_debug.h"
  2. #include <linux/exportfs.h>
  3. #include <linux/slab.h>
  4. #include <asm/unaligned.h>
  5. #include "super.h"
  6. /*
  7. * NFS export support
  8. *
  9. * NFS re-export of a ceph mount is, at present, only semireliable.
  10. * The basic issue is that the Ceph architectures doesn't lend itself
  11. * well to generating filehandles that will remain valid forever.
  12. *
  13. * So, we do our best. If you're lucky, your inode will be in the
  14. * client's cache. If it's not, and you have a connectable fh, then
  15. * the MDS server may be able to find it for you. Otherwise, you get
  16. * ESTALE.
  17. *
  18. * There are ways to this more reliable, but in the non-connectable fh
  19. * case, we won't every work perfectly, and in the connectable case,
  20. * some changes are needed on the MDS side to work better.
  21. */
  22. /*
  23. * Basic fh
  24. */
  25. struct ceph_nfs_fh {
  26. u64 ino;
  27. } __attribute__ ((packed));
  28. /*
  29. * Larger 'connectable' fh that includes parent ino and name hash.
  30. * Use this whenever possible, as it works more reliably.
  31. */
  32. struct ceph_nfs_confh {
  33. u64 ino, parent_ino;
  34. u32 parent_name_hash;
  35. } __attribute__ ((packed));
  36. static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len,
  37. int connectable)
  38. {
  39. int type;
  40. struct ceph_nfs_fh *fh = (void *)rawfh;
  41. struct ceph_nfs_confh *cfh = (void *)rawfh;
  42. struct dentry *parent = dentry->d_parent;
  43. struct inode *inode = dentry->d_inode;
  44. int connected_handle_length = sizeof(*cfh)/4;
  45. int handle_length = sizeof(*fh)/4;
  46. /* don't re-export snaps */
  47. if (ceph_snap(inode) != CEPH_NOSNAP)
  48. return -EINVAL;
  49. if (*max_len >= connected_handle_length) {
  50. dout("encode_fh %p connectable\n", dentry);
  51. cfh->ino = ceph_ino(dentry->d_inode);
  52. cfh->parent_ino = ceph_ino(parent->d_inode);
  53. cfh->parent_name_hash = parent->d_name.hash;
  54. *max_len = connected_handle_length;
  55. type = 2;
  56. } else if (*max_len >= handle_length) {
  57. if (connectable) {
  58. *max_len = connected_handle_length;
  59. return 255;
  60. }
  61. dout("encode_fh %p\n", dentry);
  62. fh->ino = ceph_ino(dentry->d_inode);
  63. *max_len = handle_length;
  64. type = 1;
  65. } else {
  66. *max_len = handle_length;
  67. return 255;
  68. }
  69. return type;
  70. }
  71. /*
  72. * convert regular fh to dentry
  73. *
  74. * FIXME: we should try harder by querying the mds for the ino.
  75. */
  76. static struct dentry *__fh_to_dentry(struct super_block *sb,
  77. struct ceph_nfs_fh *fh)
  78. {
  79. struct inode *inode;
  80. struct dentry *dentry;
  81. struct ceph_vino vino;
  82. int err;
  83. dout("__fh_to_dentry %llx\n", fh->ino);
  84. vino.ino = fh->ino;
  85. vino.snap = CEPH_NOSNAP;
  86. inode = ceph_find_inode(sb, vino);
  87. if (!inode)
  88. return ERR_PTR(-ESTALE);
  89. dentry = d_obtain_alias(inode);
  90. if (IS_ERR(dentry)) {
  91. pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
  92. fh->ino, inode);
  93. iput(inode);
  94. return dentry;
  95. }
  96. err = ceph_init_dentry(dentry);
  97. if (err < 0) {
  98. iput(inode);
  99. return ERR_PTR(err);
  100. }
  101. dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry);
  102. return dentry;
  103. }
  104. /*
  105. * convert connectable fh to dentry
  106. */
  107. static struct dentry *__cfh_to_dentry(struct super_block *sb,
  108. struct ceph_nfs_confh *cfh)
  109. {
  110. struct ceph_mds_client *mdsc = &ceph_sb_to_client(sb)->mdsc;
  111. struct inode *inode;
  112. struct dentry *dentry;
  113. struct ceph_vino vino;
  114. int err;
  115. dout("__cfh_to_dentry %llx (%llx/%x)\n",
  116. cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
  117. vino.ino = cfh->ino;
  118. vino.snap = CEPH_NOSNAP;
  119. inode = ceph_find_inode(sb, vino);
  120. if (!inode) {
  121. struct ceph_mds_request *req;
  122. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH,
  123. USE_ANY_MDS);
  124. if (IS_ERR(req))
  125. return ERR_CAST(req);
  126. req->r_ino1 = vino;
  127. req->r_ino2.ino = cfh->parent_ino;
  128. req->r_ino2.snap = CEPH_NOSNAP;
  129. req->r_path2 = kmalloc(16, GFP_NOFS);
  130. snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash);
  131. req->r_num_caps = 1;
  132. err = ceph_mdsc_do_request(mdsc, NULL, req);
  133. ceph_mdsc_put_request(req);
  134. inode = ceph_find_inode(sb, vino);
  135. if (!inode)
  136. return ERR_PTR(err ? err : -ESTALE);
  137. }
  138. dentry = d_obtain_alias(inode);
  139. if (IS_ERR(dentry)) {
  140. pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
  141. cfh->ino, inode);
  142. iput(inode);
  143. return dentry;
  144. }
  145. err = ceph_init_dentry(dentry);
  146. if (err < 0) {
  147. iput(inode);
  148. return ERR_PTR(err);
  149. }
  150. dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry);
  151. return dentry;
  152. }
  153. static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
  154. int fh_len, int fh_type)
  155. {
  156. if (fh_type == 1)
  157. return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw);
  158. else
  159. return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw);
  160. }
  161. /*
  162. * get parent, if possible.
  163. *
  164. * FIXME: we could do better by querying the mds to discover the
  165. * parent.
  166. */
  167. static struct dentry *ceph_fh_to_parent(struct super_block *sb,
  168. struct fid *fid,
  169. int fh_len, int fh_type)
  170. {
  171. struct ceph_nfs_confh *cfh = (void *)fid->raw;
  172. struct ceph_vino vino;
  173. struct inode *inode;
  174. struct dentry *dentry;
  175. int err;
  176. if (fh_type == 1)
  177. return ERR_PTR(-ESTALE);
  178. pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
  179. cfh->parent_name_hash);
  180. vino.ino = cfh->ino;
  181. vino.snap = CEPH_NOSNAP;
  182. inode = ceph_find_inode(sb, vino);
  183. if (!inode)
  184. return ERR_PTR(-ESTALE);
  185. dentry = d_obtain_alias(inode);
  186. if (IS_ERR(dentry)) {
  187. pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
  188. cfh->ino, inode);
  189. iput(inode);
  190. return dentry;
  191. }
  192. err = ceph_init_dentry(dentry);
  193. if (err < 0) {
  194. iput(inode);
  195. return ERR_PTR(err);
  196. }
  197. dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry);
  198. return dentry;
  199. }
  200. const struct export_operations ceph_export_ops = {
  201. .encode_fh = ceph_encode_fh,
  202. .fh_to_dentry = ceph_fh_to_dentry,
  203. .fh_to_parent = ceph_fh_to_parent,
  204. };