export.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. * Basic fh
  9. */
  10. struct ceph_nfs_fh {
  11. u64 ino;
  12. } __attribute__ ((packed));
  13. /*
  14. * Larger fh that includes parent ino.
  15. */
  16. struct ceph_nfs_confh {
  17. u64 ino, parent_ino;
  18. } __attribute__ ((packed));
  19. static int ceph_encode_fh(struct inode *inode, u32 *rawfh, int *max_len,
  20. struct inode *parent_inode)
  21. {
  22. int type;
  23. struct ceph_nfs_fh *fh = (void *)rawfh;
  24. struct ceph_nfs_confh *cfh = (void *)rawfh;
  25. int connected_handle_length = sizeof(*cfh)/4;
  26. int handle_length = sizeof(*fh)/4;
  27. /* don't re-export snaps */
  28. if (ceph_snap(inode) != CEPH_NOSNAP)
  29. return -EINVAL;
  30. if (parent_inode && (*max_len < connected_handle_length)) {
  31. *max_len = connected_handle_length;
  32. return FILEID_INVALID;
  33. } else if (*max_len < handle_length) {
  34. *max_len = handle_length;
  35. return FILEID_INVALID;
  36. }
  37. if (parent_inode) {
  38. dout("encode_fh %llx with parent %llx\n",
  39. ceph_ino(inode), ceph_ino(parent_inode));
  40. cfh->ino = ceph_ino(inode);
  41. cfh->parent_ino = ceph_ino(parent_inode);
  42. *max_len = connected_handle_length;
  43. type = FILEID_INO32_GEN_PARENT;
  44. } else {
  45. dout("encode_fh %llx\n", ceph_ino(inode));
  46. fh->ino = ceph_ino(inode);
  47. *max_len = handle_length;
  48. type = FILEID_INO32_GEN;
  49. }
  50. return type;
  51. }
  52. static struct dentry *__fh_to_dentry(struct super_block *sb, u64 ino)
  53. {
  54. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  55. struct inode *inode;
  56. struct dentry *dentry;
  57. struct ceph_vino vino;
  58. int err;
  59. vino.ino = ino;
  60. vino.snap = CEPH_NOSNAP;
  61. inode = ceph_find_inode(sb, vino);
  62. if (!inode) {
  63. struct ceph_mds_request *req;
  64. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
  65. USE_ANY_MDS);
  66. if (IS_ERR(req))
  67. return ERR_CAST(req);
  68. req->r_ino1 = vino;
  69. req->r_num_caps = 1;
  70. err = ceph_mdsc_do_request(mdsc, NULL, req);
  71. inode = req->r_target_inode;
  72. if (inode)
  73. ihold(inode);
  74. ceph_mdsc_put_request(req);
  75. if (!inode)
  76. return ERR_PTR(-ESTALE);
  77. }
  78. dentry = d_obtain_alias(inode);
  79. if (IS_ERR(dentry)) {
  80. iput(inode);
  81. return dentry;
  82. }
  83. err = ceph_init_dentry(dentry);
  84. if (err < 0) {
  85. dput(dentry);
  86. return ERR_PTR(err);
  87. }
  88. dout("__fh_to_dentry %llx %p dentry %p\n", ino, inode, dentry);
  89. return dentry;
  90. }
  91. /*
  92. * convert regular fh to dentry
  93. */
  94. static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
  95. struct fid *fid,
  96. int fh_len, int fh_type)
  97. {
  98. struct ceph_nfs_fh *fh = (void *)fid->raw;
  99. if (fh_type != FILEID_INO32_GEN &&
  100. fh_type != FILEID_INO32_GEN_PARENT)
  101. return NULL;
  102. if (fh_len < sizeof(*fh) / 4)
  103. return NULL;
  104. dout("fh_to_dentry %llx\n", fh->ino);
  105. return __fh_to_dentry(sb, fh->ino);
  106. }
  107. static struct dentry *__get_parent(struct super_block *sb,
  108. struct dentry *child, u64 ino)
  109. {
  110. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  111. struct ceph_mds_request *req;
  112. struct inode *inode;
  113. struct dentry *dentry;
  114. int err;
  115. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
  116. USE_ANY_MDS);
  117. if (IS_ERR(req))
  118. return ERR_CAST(req);
  119. if (child) {
  120. req->r_inode = d_inode(child);
  121. ihold(d_inode(child));
  122. } else {
  123. req->r_ino1 = (struct ceph_vino) {
  124. .ino = ino,
  125. .snap = CEPH_NOSNAP,
  126. };
  127. }
  128. req->r_num_caps = 1;
  129. err = ceph_mdsc_do_request(mdsc, NULL, req);
  130. inode = req->r_target_inode;
  131. if (inode)
  132. ihold(inode);
  133. ceph_mdsc_put_request(req);
  134. if (!inode)
  135. return ERR_PTR(-ENOENT);
  136. dentry = d_obtain_alias(inode);
  137. if (IS_ERR(dentry)) {
  138. iput(inode);
  139. return dentry;
  140. }
  141. err = ceph_init_dentry(dentry);
  142. if (err < 0) {
  143. dput(dentry);
  144. return ERR_PTR(err);
  145. }
  146. dout("__get_parent ino %llx parent %p ino %llx.%llx\n",
  147. child ? ceph_ino(d_inode(child)) : ino,
  148. dentry, ceph_vinop(inode));
  149. return dentry;
  150. }
  151. static struct dentry *ceph_get_parent(struct dentry *child)
  152. {
  153. /* don't re-export snaps */
  154. if (ceph_snap(d_inode(child)) != CEPH_NOSNAP)
  155. return ERR_PTR(-EINVAL);
  156. dout("get_parent %p ino %llx.%llx\n",
  157. child, ceph_vinop(d_inode(child)));
  158. return __get_parent(child->d_sb, child, 0);
  159. }
  160. /*
  161. * convert regular fh to parent
  162. */
  163. static struct dentry *ceph_fh_to_parent(struct super_block *sb,
  164. struct fid *fid,
  165. int fh_len, int fh_type)
  166. {
  167. struct ceph_nfs_confh *cfh = (void *)fid->raw;
  168. struct dentry *dentry;
  169. if (fh_type != FILEID_INO32_GEN_PARENT)
  170. return NULL;
  171. if (fh_len < sizeof(*cfh) / 4)
  172. return NULL;
  173. dout("fh_to_parent %llx\n", cfh->parent_ino);
  174. dentry = __get_parent(sb, NULL, cfh->ino);
  175. if (IS_ERR(dentry) && PTR_ERR(dentry) == -ENOENT)
  176. dentry = __fh_to_dentry(sb, cfh->parent_ino);
  177. return dentry;
  178. }
  179. static int ceph_get_name(struct dentry *parent, char *name,
  180. struct dentry *child)
  181. {
  182. struct ceph_mds_client *mdsc;
  183. struct ceph_mds_request *req;
  184. int err;
  185. mdsc = ceph_inode_to_client(d_inode(child))->mdsc;
  186. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
  187. USE_ANY_MDS);
  188. if (IS_ERR(req))
  189. return PTR_ERR(req);
  190. mutex_lock(&d_inode(parent)->i_mutex);
  191. req->r_inode = d_inode(child);
  192. ihold(d_inode(child));
  193. req->r_ino2 = ceph_vino(d_inode(parent));
  194. req->r_locked_dir = d_inode(parent);
  195. req->r_num_caps = 2;
  196. err = ceph_mdsc_do_request(mdsc, NULL, req);
  197. mutex_unlock(&d_inode(parent)->i_mutex);
  198. if (!err) {
  199. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  200. memcpy(name, rinfo->dname, rinfo->dname_len);
  201. name[rinfo->dname_len] = 0;
  202. dout("get_name %p ino %llx.%llx name %s\n",
  203. child, ceph_vinop(d_inode(child)), name);
  204. } else {
  205. dout("get_name %p ino %llx.%llx err %d\n",
  206. child, ceph_vinop(d_inode(child)), err);
  207. }
  208. ceph_mdsc_put_request(req);
  209. return err;
  210. }
  211. const struct export_operations ceph_export_ops = {
  212. .encode_fh = ceph_encode_fh,
  213. .fh_to_dentry = ceph_fh_to_dentry,
  214. .fh_to_parent = ceph_fh_to_parent,
  215. .get_parent = ceph_get_parent,
  216. .get_name = ceph_get_name,
  217. };