export.c 5.5 KB

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