export.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. int mask;
  65. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPINO,
  66. USE_ANY_MDS);
  67. if (IS_ERR(req))
  68. return ERR_CAST(req);
  69. mask = CEPH_STAT_CAP_INODE;
  70. if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
  71. mask |= CEPH_CAP_XATTR_SHARED;
  72. req->r_args.getattr.mask = cpu_to_le32(mask);
  73. req->r_ino1 = vino;
  74. req->r_num_caps = 1;
  75. err = ceph_mdsc_do_request(mdsc, NULL, req);
  76. inode = req->r_target_inode;
  77. if (inode)
  78. ihold(inode);
  79. ceph_mdsc_put_request(req);
  80. if (!inode)
  81. return ERR_PTR(-ESTALE);
  82. }
  83. dentry = d_obtain_alias(inode);
  84. if (IS_ERR(dentry))
  85. return dentry;
  86. err = ceph_init_dentry(dentry);
  87. if (err < 0) {
  88. dput(dentry);
  89. return ERR_PTR(err);
  90. }
  91. dout("__fh_to_dentry %llx %p dentry %p\n", ino, inode, dentry);
  92. return dentry;
  93. }
  94. /*
  95. * convert regular fh to dentry
  96. */
  97. static struct dentry *ceph_fh_to_dentry(struct super_block *sb,
  98. struct fid *fid,
  99. int fh_len, int fh_type)
  100. {
  101. struct ceph_nfs_fh *fh = (void *)fid->raw;
  102. if (fh_type != FILEID_INO32_GEN &&
  103. fh_type != FILEID_INO32_GEN_PARENT)
  104. return NULL;
  105. if (fh_len < sizeof(*fh) / 4)
  106. return NULL;
  107. dout("fh_to_dentry %llx\n", fh->ino);
  108. return __fh_to_dentry(sb, fh->ino);
  109. }
  110. static struct dentry *__get_parent(struct super_block *sb,
  111. struct dentry *child, u64 ino)
  112. {
  113. struct ceph_mds_client *mdsc = ceph_sb_to_client(sb)->mdsc;
  114. struct ceph_mds_request *req;
  115. struct inode *inode;
  116. struct dentry *dentry;
  117. int mask;
  118. int err;
  119. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPPARENT,
  120. USE_ANY_MDS);
  121. if (IS_ERR(req))
  122. return ERR_CAST(req);
  123. if (child) {
  124. req->r_inode = d_inode(child);
  125. ihold(d_inode(child));
  126. } else {
  127. req->r_ino1 = (struct ceph_vino) {
  128. .ino = ino,
  129. .snap = CEPH_NOSNAP,
  130. };
  131. }
  132. mask = CEPH_STAT_CAP_INODE;
  133. if (ceph_security_xattr_wanted(d_inode(sb->s_root)))
  134. mask |= CEPH_CAP_XATTR_SHARED;
  135. req->r_args.getattr.mask = cpu_to_le32(mask);
  136. req->r_num_caps = 1;
  137. err = ceph_mdsc_do_request(mdsc, NULL, req);
  138. inode = req->r_target_inode;
  139. if (inode)
  140. ihold(inode);
  141. ceph_mdsc_put_request(req);
  142. if (!inode)
  143. return ERR_PTR(-ENOENT);
  144. dentry = d_obtain_alias(inode);
  145. if (IS_ERR(dentry))
  146. return dentry;
  147. err = ceph_init_dentry(dentry);
  148. if (err < 0) {
  149. dput(dentry);
  150. return ERR_PTR(err);
  151. }
  152. dout("__get_parent ino %llx parent %p ino %llx.%llx\n",
  153. child ? ceph_ino(d_inode(child)) : ino,
  154. dentry, ceph_vinop(inode));
  155. return dentry;
  156. }
  157. static struct dentry *ceph_get_parent(struct dentry *child)
  158. {
  159. /* don't re-export snaps */
  160. if (ceph_snap(d_inode(child)) != CEPH_NOSNAP)
  161. return ERR_PTR(-EINVAL);
  162. dout("get_parent %p ino %llx.%llx\n",
  163. child, ceph_vinop(d_inode(child)));
  164. return __get_parent(child->d_sb, child, 0);
  165. }
  166. /*
  167. * convert regular fh to parent
  168. */
  169. static struct dentry *ceph_fh_to_parent(struct super_block *sb,
  170. struct fid *fid,
  171. int fh_len, int fh_type)
  172. {
  173. struct ceph_nfs_confh *cfh = (void *)fid->raw;
  174. struct dentry *dentry;
  175. if (fh_type != FILEID_INO32_GEN_PARENT)
  176. return NULL;
  177. if (fh_len < sizeof(*cfh) / 4)
  178. return NULL;
  179. dout("fh_to_parent %llx\n", cfh->parent_ino);
  180. dentry = __get_parent(sb, NULL, cfh->ino);
  181. if (unlikely(dentry == ERR_PTR(-ENOENT)))
  182. dentry = __fh_to_dentry(sb, cfh->parent_ino);
  183. return dentry;
  184. }
  185. static int ceph_get_name(struct dentry *parent, char *name,
  186. struct dentry *child)
  187. {
  188. struct ceph_mds_client *mdsc;
  189. struct ceph_mds_request *req;
  190. int err;
  191. mdsc = ceph_inode_to_client(d_inode(child))->mdsc;
  192. req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPNAME,
  193. USE_ANY_MDS);
  194. if (IS_ERR(req))
  195. return PTR_ERR(req);
  196. inode_lock(d_inode(parent));
  197. req->r_inode = d_inode(child);
  198. ihold(d_inode(child));
  199. req->r_ino2 = ceph_vino(d_inode(parent));
  200. req->r_locked_dir = d_inode(parent);
  201. req->r_num_caps = 2;
  202. err = ceph_mdsc_do_request(mdsc, NULL, req);
  203. inode_unlock(d_inode(parent));
  204. if (!err) {
  205. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  206. memcpy(name, rinfo->dname, rinfo->dname_len);
  207. name[rinfo->dname_len] = 0;
  208. dout("get_name %p ino %llx.%llx name %s\n",
  209. child, ceph_vinop(d_inode(child)), name);
  210. } else {
  211. dout("get_name %p ino %llx.%llx err %d\n",
  212. child, ceph_vinop(d_inode(child)), err);
  213. }
  214. ceph_mdsc_put_request(req);
  215. return err;
  216. }
  217. const struct export_operations ceph_export_ops = {
  218. .encode_fh = ceph_encode_fh,
  219. .fh_to_dentry = ceph_fh_to_dentry,
  220. .fh_to_parent = ceph_fh_to_parent,
  221. .get_parent = ceph_get_parent,
  222. .get_name = ceph_get_name,
  223. };