export.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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;
  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. spin_lock(&dentry->d_lock);
  51. parent = dentry->d_parent;
  52. if (*max_len >= connected_handle_length) {
  53. dout("encode_fh %p connectable\n", dentry);
  54. cfh->ino = ceph_ino(dentry->d_inode);
  55. cfh->parent_ino = ceph_ino(parent->d_inode);
  56. cfh->parent_name_hash = ceph_dentry_hash(parent->d_inode,
  57. dentry);
  58. *max_len = connected_handle_length;
  59. type = 2;
  60. } else if (*max_len >= handle_length) {
  61. if (connectable) {
  62. *max_len = connected_handle_length;
  63. type = 255;
  64. } else {
  65. dout("encode_fh %p\n", dentry);
  66. fh->ino = ceph_ino(dentry->d_inode);
  67. *max_len = handle_length;
  68. type = 1;
  69. }
  70. } else {
  71. *max_len = handle_length;
  72. type = 255;
  73. }
  74. spin_unlock(&dentry->d_lock);
  75. return type;
  76. }
  77. /*
  78. * convert regular fh to dentry
  79. *
  80. * FIXME: we should try harder by querying the mds for the ino.
  81. */
  82. static struct dentry *__fh_to_dentry(struct super_block *sb,
  83. struct ceph_nfs_fh *fh, int fh_len)
  84. {
  85. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  86. struct inode *inode;
  87. struct dentry *dentry;
  88. struct ceph_vino vino;
  89. int err;
  90. if (fh_len < sizeof(*fh) / 4)
  91. return ERR_PTR(-ESTALE);
  92. dout("__fh_to_dentry %llx\n", fh->ino);
  93. vino.ino = fh->ino;
  94. vino.snap = CEPH_NOSNAP;
  95. inode = ceph_find_inode(sb, vino);
  96. if (!inode) {
  97. struct ceph_mds_request *req;
  98. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
  99. USE_ANY_MDS);
  100. if (IS_ERR(req))
  101. return ERR_CAST(req);
  102. req->r_ino1 = vino;
  103. req->r_num_caps = 1;
  104. err = ceph_mdsc_do_request(mdsc, NULL, req);
  105. inode = req->r_target_inode;
  106. if (inode)
  107. ihold(inode);
  108. ceph_mdsc_put_request(req);
  109. if (!inode)
  110. return ERR_PTR(-ESTALE);
  111. }
  112. dentry = d_obtain_alias(inode);
  113. if (IS_ERR(dentry)) {
  114. pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
  115. fh->ino, inode);
  116. iput(inode);
  117. return dentry;
  118. }
  119. err = ceph_init_dentry(dentry);
  120. if (err < 0) {
  121. iput(inode);
  122. return ERR_PTR(err);
  123. }
  124. dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry);
  125. return dentry;
  126. }
  127. /*
  128. * convert connectable fh to dentry
  129. */
  130. static struct dentry *__cfh_to_dentry(struct super_block *sb,
  131. struct ceph_nfs_confh *cfh, int fh_len)
  132. {
  133. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  134. struct inode *inode;
  135. struct dentry *dentry;
  136. struct ceph_vino vino;
  137. int err;
  138. if (fh_len < sizeof(*cfh) / 4)
  139. return ERR_PTR(-ESTALE);
  140. dout("__cfh_to_dentry %llx (%llx/%x)\n",
  141. cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
  142. vino.ino = cfh->ino;
  143. vino.snap = CEPH_NOSNAP;
  144. inode = ceph_find_inode(sb, vino);
  145. if (!inode) {
  146. struct ceph_mds_request *req;
  147. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH,
  148. USE_ANY_MDS);
  149. if (IS_ERR(req))
  150. return ERR_CAST(req);
  151. req->r_ino1 = vino;
  152. req->r_ino2.ino = cfh->parent_ino;
  153. req->r_ino2.snap = CEPH_NOSNAP;
  154. req->r_path2 = kmalloc(16, GFP_NOFS);
  155. snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash);
  156. req->r_num_caps = 1;
  157. err = ceph_mdsc_do_request(mdsc, NULL, req);
  158. inode = req->r_target_inode;
  159. if (inode)
  160. ihold(inode);
  161. ceph_mdsc_put_request(req);
  162. if (!inode)
  163. return ERR_PTR(err ? err : -ESTALE);
  164. }
  165. dentry = d_obtain_alias(inode);
  166. if (IS_ERR(dentry)) {
  167. pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
  168. cfh->ino, inode);
  169. iput(inode);
  170. return dentry;
  171. }
  172. err = ceph_init_dentry(dentry);
  173. if (err < 0) {
  174. iput(inode);
  175. return ERR_PTR(err);
  176. }
  177. dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry);
  178. return dentry;
  179. }
  180. static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
  181. int fh_len, int fh_type)
  182. {
  183. if (fh_type == 1)
  184. return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw,
  185. fh_len);
  186. else
  187. return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw,
  188. fh_len);
  189. }
  190. /*
  191. * get parent, if possible.
  192. *
  193. * FIXME: we could do better by querying the mds to discover the
  194. * parent.
  195. */
  196. static struct dentry *ceph_fh_to_parent(struct super_block *sb,
  197. struct fid *fid,
  198. int fh_len, int fh_type)
  199. {
  200. struct ceph_nfs_confh *cfh = (void *)fid->raw;
  201. struct ceph_vino vino;
  202. struct inode *inode;
  203. struct dentry *dentry;
  204. int err;
  205. if (fh_type == 1)
  206. return ERR_PTR(-ESTALE);
  207. if (fh_len < sizeof(*cfh) / 4)
  208. return ERR_PTR(-ESTALE);
  209. pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
  210. cfh->parent_name_hash);
  211. vino.ino = cfh->ino;
  212. vino.snap = CEPH_NOSNAP;
  213. inode = ceph_find_inode(sb, vino);
  214. if (!inode)
  215. return ERR_PTR(-ESTALE);
  216. dentry = d_obtain_alias(inode);
  217. if (IS_ERR(dentry)) {
  218. pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
  219. cfh->ino, inode);
  220. iput(inode);
  221. return dentry;
  222. }
  223. err = ceph_init_dentry(dentry);
  224. if (err < 0) {
  225. iput(inode);
  226. return ERR_PTR(err);
  227. }
  228. dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry);
  229. return dentry;
  230. }
  231. const struct export_operations ceph_export_ops = {
  232. .encode_fh = ceph_encode_fh,
  233. .fh_to_dentry = ceph_fh_to_dentry,
  234. .fh_to_parent = ceph_fh_to_parent,
  235. };