export.c 5.7 KB

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