export.c 5.6 KB

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