export.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2015, Primary Data, Inc. All rights reserved.
  3. *
  4. * Tao Peng <bergwolf@primarydata.com>
  5. */
  6. #include <linux/dcache.h>
  7. #include <linux/exportfs.h>
  8. #include <linux/nfs.h>
  9. #include <linux/nfs_fs.h>
  10. #include "internal.h"
  11. #include "nfstrace.h"
  12. #define NFSDBG_FACILITY NFSDBG_VFS
  13. enum {
  14. FILEID_HIGH_OFF = 0, /* inode fileid high */
  15. FILEID_LOW_OFF, /* inode fileid low */
  16. FILE_I_TYPE_OFF, /* inode type */
  17. EMBED_FH_OFF /* embeded server fh */
  18. };
  19. static struct nfs_fh *nfs_exp_embedfh(__u32 *p)
  20. {
  21. return (struct nfs_fh *)(p + EMBED_FH_OFF);
  22. }
  23. /*
  24. * Let's break subtree checking for now... otherwise we'll have to embed parent fh
  25. * but there might not be enough space.
  26. */
  27. static int
  28. nfs_encode_fh(struct inode *inode, __u32 *p, int *max_len, struct inode *parent)
  29. {
  30. struct nfs_fh *server_fh = NFS_FH(inode);
  31. struct nfs_fh *clnt_fh = nfs_exp_embedfh(p);
  32. size_t fh_size = offsetof(struct nfs_fh, data) + server_fh->size;
  33. int len = EMBED_FH_OFF + XDR_QUADLEN(fh_size);
  34. dprintk("%s: max fh len %d inode %p parent %p",
  35. __func__, *max_len, inode, parent);
  36. if (*max_len < len || IS_AUTOMOUNT(inode)) {
  37. dprintk("%s: fh len %d too small, required %d\n",
  38. __func__, *max_len, len);
  39. *max_len = len;
  40. return FILEID_INVALID;
  41. }
  42. if (IS_AUTOMOUNT(inode)) {
  43. *max_len = FILEID_INVALID;
  44. goto out;
  45. }
  46. p[FILEID_HIGH_OFF] = NFS_FILEID(inode) >> 32;
  47. p[FILEID_LOW_OFF] = NFS_FILEID(inode);
  48. p[FILE_I_TYPE_OFF] = inode->i_mode & S_IFMT;
  49. p[len - 1] = 0; /* Padding */
  50. nfs_copy_fh(clnt_fh, server_fh);
  51. *max_len = len;
  52. out:
  53. dprintk("%s: result fh fileid %llu mode %u size %d\n",
  54. __func__, NFS_FILEID(inode), inode->i_mode, *max_len);
  55. return *max_len;
  56. }
  57. static struct dentry *
  58. nfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  59. int fh_len, int fh_type)
  60. {
  61. struct nfs4_label *label = NULL;
  62. struct nfs_fattr *fattr = NULL;
  63. struct nfs_fh *server_fh = nfs_exp_embedfh(fid->raw);
  64. size_t fh_size = offsetof(struct nfs_fh, data) + server_fh->size;
  65. const struct nfs_rpc_ops *rpc_ops;
  66. struct dentry *dentry;
  67. struct inode *inode;
  68. int len = EMBED_FH_OFF + XDR_QUADLEN(fh_size);
  69. u32 *p = fid->raw;
  70. int ret;
  71. /* NULL translates to ESTALE */
  72. if (fh_len < len || fh_type != len)
  73. return NULL;
  74. fattr = nfs_alloc_fattr();
  75. if (fattr == NULL) {
  76. dentry = ERR_PTR(-ENOMEM);
  77. goto out;
  78. }
  79. fattr->fileid = ((u64)p[FILEID_HIGH_OFF] << 32) + p[FILEID_LOW_OFF];
  80. fattr->mode = p[FILE_I_TYPE_OFF];
  81. fattr->valid |= NFS_ATTR_FATTR_FILEID | NFS_ATTR_FATTR_TYPE;
  82. dprintk("%s: fileid %llu mode %d\n", __func__, fattr->fileid, fattr->mode);
  83. inode = nfs_ilookup(sb, fattr, server_fh);
  84. if (inode)
  85. goto out_found;
  86. label = nfs4_label_alloc(NFS_SB(sb), GFP_KERNEL);
  87. if (IS_ERR(label)) {
  88. dentry = ERR_CAST(label);
  89. goto out_free_fattr;
  90. }
  91. rpc_ops = NFS_SB(sb)->nfs_client->rpc_ops;
  92. ret = rpc_ops->getattr(NFS_SB(sb), server_fh, fattr, label);
  93. if (ret) {
  94. dprintk("%s: getattr failed %d\n", __func__, ret);
  95. dentry = ERR_PTR(ret);
  96. goto out_free_label;
  97. }
  98. inode = nfs_fhget(sb, server_fh, fattr, label);
  99. out_found:
  100. dentry = d_obtain_alias(inode);
  101. out_free_label:
  102. nfs4_label_free(label);
  103. out_free_fattr:
  104. nfs_free_fattr(fattr);
  105. out:
  106. return dentry;
  107. }
  108. static struct dentry *
  109. nfs_get_parent(struct dentry *dentry)
  110. {
  111. int ret;
  112. struct inode *inode = d_inode(dentry), *pinode;
  113. struct super_block *sb = inode->i_sb;
  114. struct nfs_server *server = NFS_SB(sb);
  115. struct nfs_fattr *fattr = NULL;
  116. struct nfs4_label *label = NULL;
  117. struct dentry *parent;
  118. struct nfs_rpc_ops const *ops = server->nfs_client->rpc_ops;
  119. struct nfs_fh fh;
  120. if (!ops->lookupp)
  121. return ERR_PTR(-EACCES);
  122. fattr = nfs_alloc_fattr();
  123. if (fattr == NULL) {
  124. parent = ERR_PTR(-ENOMEM);
  125. goto out;
  126. }
  127. label = nfs4_label_alloc(server, GFP_KERNEL);
  128. if (IS_ERR(label)) {
  129. parent = ERR_CAST(label);
  130. goto out_free_fattr;
  131. }
  132. ret = ops->lookupp(inode, &fh, fattr, label);
  133. if (ret) {
  134. parent = ERR_PTR(ret);
  135. goto out_free_label;
  136. }
  137. pinode = nfs_fhget(sb, &fh, fattr, label);
  138. parent = d_obtain_alias(pinode);
  139. out_free_label:
  140. nfs4_label_free(label);
  141. out_free_fattr:
  142. nfs_free_fattr(fattr);
  143. out:
  144. return parent;
  145. }
  146. const struct export_operations nfs_export_ops = {
  147. .encode_fh = nfs_encode_fh,
  148. .fh_to_dentry = nfs_fh_to_dentry,
  149. .get_parent = nfs_get_parent,
  150. };