export.c 4.2 KB

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