export.c 5.4 KB

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