export.c 6.1 KB

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