export.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /*
  38. * The presence of @parent_inode here tells us whether NFS wants a
  39. * connectable file handle. However, we want to make a connectionable
  40. * file handle unconditionally so that the MDS gets as much of a hint
  41. * as possible. That means we only use @parent_dentry to indicate
  42. * whether nfsd wants a connectable fh, and whether we should indicate
  43. * failure from a too-small @max_len.
  44. */
  45. static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
  46. struct inode *parent_inode)
  47. {
  48. int type;
  49. struct ceph_nfs_fh *fh = (void *)rawfh;
  50. struct ceph_nfs_confh *cfh = (void *)rawfh;
  51. int connected_handle_length = sizeof(*cfh)/4;
  52. int handle_length = sizeof(*fh)/4;
  53. struct dentry *dentry;
  54. struct dentry *parent;
  55. /* don't re-export snaps */
  56. if (ceph_snap(inode) != CEPH_NOSNAP)
  57. return -EINVAL;
  58. dentry = d_find_alias(inode);
  59. /* if we found an alias, generate a connectable fh */
  60. if (*max_len >= connected_handle_length && dentry) {
  61. dout("encode_fh %p connectable\n", dentry);
  62. spin_lock(&dentry->d_lock);
  63. parent = dentry->d_parent;
  64. cfh->ino = ceph_ino(inode);
  65. cfh->parent_ino = ceph_ino(parent->d_inode);
  66. cfh->parent_name_hash = ceph_dentry_hash(parent->d_inode,
  67. dentry);
  68. *max_len = connected_handle_length;
  69. type = 2;
  70. spin_unlock(&dentry->d_lock);
  71. } else if (*max_len >= handle_length) {
  72. if (parent_inode) {
  73. /* nfsd wants connectable */
  74. *max_len = connected_handle_length;
  75. type = FILEID_INVALID;
  76. } else {
  77. dout("encode_fh %p\n", dentry);
  78. fh->ino = ceph_ino(inode);
  79. *max_len = handle_length;
  80. type = 1;
  81. }
  82. } else {
  83. *max_len = handle_length;
  84. type = FILEID_INVALID;
  85. }
  86. if (dentry)
  87. dput(dentry);
  88. return type;
  89. }
  90. /*
  91. * convert regular fh to dentry
  92. *
  93. * FIXME: we should try harder by querying the mds for the ino.
  94. */
  95. static struct dentry *__fh_to_dentry(struct super_block *sb,
  96. struct ceph_nfs_fh *fh, int fh_len)
  97. {
  98. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  99. struct inode *inode;
  100. struct dentry *dentry;
  101. struct ceph_vino vino;
  102. int err;
  103. if (fh_len < sizeof(*fh) / 4)
  104. return ERR_PTR(-ESTALE);
  105. dout("__fh_to_dentry %llx\n", fh->ino);
  106. vino.ino = fh->ino;
  107. vino.snap = CEPH_NOSNAP;
  108. inode = ceph_find_inode(sb, vino);
  109. if (!inode) {
  110. struct ceph_mds_request *req;
  111. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
  112. USE_ANY_MDS);
  113. if (IS_ERR(req))
  114. return ERR_CAST(req);
  115. req->r_ino1 = vino;
  116. req->r_num_caps = 1;
  117. err = ceph_mdsc_do_request(mdsc, NULL, req);
  118. inode = req->r_target_inode;
  119. if (inode)
  120. ihold(inode);
  121. ceph_mdsc_put_request(req);
  122. if (!inode)
  123. return ERR_PTR(-ESTALE);
  124. }
  125. dentry = d_obtain_alias(inode);
  126. if (IS_ERR(dentry)) {
  127. pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
  128. fh->ino, inode);
  129. iput(inode);
  130. return dentry;
  131. }
  132. err = ceph_init_dentry(dentry);
  133. if (err < 0) {
  134. iput(inode);
  135. return ERR_PTR(err);
  136. }
  137. dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry);
  138. return dentry;
  139. }
  140. /*
  141. * convert connectable fh to dentry
  142. */
  143. static struct dentry *__cfh_to_dentry(struct super_block *sb,
  144. struct ceph_nfs_confh *cfh, int fh_len)
  145. {
  146. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  147. struct inode *inode;
  148. struct dentry *dentry;
  149. struct ceph_vino vino;
  150. int err;
  151. if (fh_len < sizeof(*cfh) / 4)
  152. return ERR_PTR(-ESTALE);
  153. dout("__cfh_to_dentry %llx (%llx/%x)\n",
  154. cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
  155. vino.ino = cfh->ino;
  156. vino.snap = CEPH_NOSNAP;
  157. inode = ceph_find_inode(sb, vino);
  158. if (!inode) {
  159. struct ceph_mds_request *req;
  160. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH,
  161. USE_ANY_MDS);
  162. if (IS_ERR(req))
  163. return ERR_CAST(req);
  164. req->r_ino1 = vino;
  165. req->r_ino2.ino = cfh->parent_ino;
  166. req->r_ino2.snap = CEPH_NOSNAP;
  167. req->r_path2 = kmalloc(16, GFP_NOFS);
  168. snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash);
  169. req->r_num_caps = 1;
  170. err = ceph_mdsc_do_request(mdsc, NULL, req);
  171. inode = req->r_target_inode;
  172. if (inode)
  173. ihold(inode);
  174. ceph_mdsc_put_request(req);
  175. if (!inode)
  176. return ERR_PTR(err ? err : -ESTALE);
  177. }
  178. dentry = d_obtain_alias(inode);
  179. if (IS_ERR(dentry)) {
  180. pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
  181. cfh->ino, inode);
  182. iput(inode);
  183. return dentry;
  184. }
  185. err = ceph_init_dentry(dentry);
  186. if (err < 0) {
  187. iput(inode);
  188. return ERR_PTR(err);
  189. }
  190. dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry);
  191. return dentry;
  192. }
  193. static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
  194. int fh_len, int fh_type)
  195. {
  196. if (fh_type == 1)
  197. return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw,
  198. fh_len);
  199. else
  200. return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw,
  201. fh_len);
  202. }
  203. /*
  204. * get parent, if possible.
  205. *
  206. * FIXME: we could do better by querying the mds to discover the
  207. * parent.
  208. */
  209. static struct dentry *ceph_fh_to_parent(struct super_block *sb,
  210. struct fid *fid,
  211. int fh_len, int fh_type)
  212. {
  213. struct ceph_nfs_confh *cfh = (void *)fid->raw;
  214. struct ceph_vino vino;
  215. struct inode *inode;
  216. struct dentry *dentry;
  217. int err;
  218. if (fh_type == 1)
  219. return ERR_PTR(-ESTALE);
  220. if (fh_len < sizeof(*cfh) / 4)
  221. return ERR_PTR(-ESTALE);
  222. pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
  223. cfh->parent_name_hash);
  224. vino.ino = cfh->ino;
  225. vino.snap = CEPH_NOSNAP;
  226. inode = ceph_find_inode(sb, vino);
  227. if (!inode)
  228. return ERR_PTR(-ESTALE);
  229. dentry = d_obtain_alias(inode);
  230. if (IS_ERR(dentry)) {
  231. pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
  232. cfh->ino, inode);
  233. iput(inode);
  234. return dentry;
  235. }
  236. err = ceph_init_dentry(dentry);
  237. if (err < 0) {
  238. iput(inode);
  239. return ERR_PTR(err);
  240. }
  241. dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry);
  242. return dentry;
  243. }
  244. const struct export_operations ceph_export_ops = {
  245. .encode_fh = ceph_encode_fh,
  246. .fh_to_dentry = ceph_fh_to_dentry,
  247. .fh_to_parent = ceph_fh_to_parent,
  248. };