export.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. struct ceph_nfs_fh *fh = (void *)rawfh;
  40. struct ceph_nfs_confh *cfh = (void *)rawfh;
  41. struct dentry *parent = dentry->d_parent;
  42. struct inode *inode = dentry->d_inode;
  43. int type;
  44. /* don't re-export snaps */
  45. if (ceph_snap(inode) != CEPH_NOSNAP)
  46. return -EINVAL;
  47. if (*max_len >= sizeof(*cfh)) {
  48. dout("encode_fh %p connectable\n", dentry);
  49. cfh->ino = ceph_ino(dentry->d_inode);
  50. cfh->parent_ino = ceph_ino(parent->d_inode);
  51. cfh->parent_name_hash = parent->d_name.hash;
  52. *max_len = sizeof(*cfh);
  53. type = 2;
  54. } else if (*max_len > sizeof(*fh)) {
  55. if (connectable)
  56. return -ENOSPC;
  57. dout("encode_fh %p\n", dentry);
  58. fh->ino = ceph_ino(dentry->d_inode);
  59. *max_len = sizeof(*fh);
  60. type = 1;
  61. } else {
  62. return -ENOSPC;
  63. }
  64. return type;
  65. }
  66. /*
  67. * convert regular fh to dentry
  68. *
  69. * FIXME: we should try harder by querying the mds for the ino.
  70. */
  71. static struct dentry *__fh_to_dentry(struct super_block *sb,
  72. struct ceph_nfs_fh *fh)
  73. {
  74. struct inode *inode;
  75. struct dentry *dentry;
  76. struct ceph_vino vino;
  77. int err;
  78. dout("__fh_to_dentry %llx\n", fh->ino);
  79. vino.ino = fh->ino;
  80. vino.snap = CEPH_NOSNAP;
  81. inode = ceph_find_inode(sb, vino);
  82. if (!inode)
  83. return ERR_PTR(-ESTALE);
  84. dentry = d_obtain_alias(inode);
  85. if (IS_ERR(dentry)) {
  86. pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n",
  87. fh->ino, inode);
  88. iput(inode);
  89. return dentry;
  90. }
  91. err = ceph_init_dentry(dentry);
  92. if (err < 0) {
  93. iput(inode);
  94. return ERR_PTR(err);
  95. }
  96. dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry);
  97. return dentry;
  98. }
  99. /*
  100. * convert connectable fh to dentry
  101. */
  102. static struct dentry *__cfh_to_dentry(struct super_block *sb,
  103. struct ceph_nfs_confh *cfh)
  104. {
  105. struct ceph_mds_client *mdsc = &ceph_sb_to_client(sb)->mdsc;
  106. struct inode *inode;
  107. struct dentry *dentry;
  108. struct ceph_vino vino;
  109. int err;
  110. dout("__cfh_to_dentry %llx (%llx/%x)\n",
  111. cfh->ino, cfh->parent_ino, cfh->parent_name_hash);
  112. vino.ino = cfh->ino;
  113. vino.snap = CEPH_NOSNAP;
  114. inode = ceph_find_inode(sb, vino);
  115. if (!inode) {
  116. struct ceph_mds_request *req;
  117. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH,
  118. USE_ANY_MDS);
  119. if (IS_ERR(req))
  120. return ERR_CAST(req);
  121. req->r_ino1 = vino;
  122. req->r_ino2.ino = cfh->parent_ino;
  123. req->r_ino2.snap = CEPH_NOSNAP;
  124. req->r_path2 = kmalloc(16, GFP_NOFS);
  125. snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash);
  126. req->r_num_caps = 1;
  127. err = ceph_mdsc_do_request(mdsc, NULL, req);
  128. ceph_mdsc_put_request(req);
  129. inode = ceph_find_inode(sb, vino);
  130. if (!inode)
  131. return ERR_PTR(err ? err : -ESTALE);
  132. }
  133. dentry = d_obtain_alias(inode);
  134. if (IS_ERR(dentry)) {
  135. pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n",
  136. cfh->ino, inode);
  137. iput(inode);
  138. return dentry;
  139. }
  140. err = ceph_init_dentry(dentry);
  141. if (err < 0) {
  142. iput(inode);
  143. return ERR_PTR(err);
  144. }
  145. dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry);
  146. return dentry;
  147. }
  148. static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid,
  149. int fh_len, int fh_type)
  150. {
  151. if (fh_type == 1)
  152. return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw);
  153. else
  154. return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw);
  155. }
  156. /*
  157. * get parent, if possible.
  158. *
  159. * FIXME: we could do better by querying the mds to discover the
  160. * parent.
  161. */
  162. static struct dentry *ceph_fh_to_parent(struct super_block *sb,
  163. struct fid *fid,
  164. int fh_len, int fh_type)
  165. {
  166. struct ceph_nfs_confh *cfh = (void *)fid->raw;
  167. struct ceph_vino vino;
  168. struct inode *inode;
  169. struct dentry *dentry;
  170. int err;
  171. if (fh_type == 1)
  172. return ERR_PTR(-ESTALE);
  173. pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino,
  174. cfh->parent_name_hash);
  175. vino.ino = cfh->ino;
  176. vino.snap = CEPH_NOSNAP;
  177. inode = ceph_find_inode(sb, vino);
  178. if (!inode)
  179. return ERR_PTR(-ESTALE);
  180. dentry = d_obtain_alias(inode);
  181. if (IS_ERR(dentry)) {
  182. pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n",
  183. cfh->ino, inode);
  184. iput(inode);
  185. return dentry;
  186. }
  187. err = ceph_init_dentry(dentry);
  188. if (err < 0) {
  189. iput(inode);
  190. return ERR_PTR(err);
  191. }
  192. dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry);
  193. return dentry;
  194. }
  195. const struct export_operations ceph_export_ops = {
  196. .encode_fh = ceph_encode_fh,
  197. .fh_to_dentry = ceph_fh_to_dentry,
  198. .fh_to_parent = ceph_fh_to_parent,
  199. };