export.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2015, Primary Data, Inc. All rights reserved.
  4. *
  5. * Tao Peng <[email protected]>
  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 nfs_fattr *fattr = NULL;
  58. struct nfs_fh *server_fh = nfs_exp_embedfh(fid->raw);
  59. size_t fh_size = offsetof(struct nfs_fh, data) + server_fh->size;
  60. const struct nfs_rpc_ops *rpc_ops;
  61. struct dentry *dentry;
  62. struct inode *inode;
  63. int len = EMBED_FH_OFF + XDR_QUADLEN(fh_size);
  64. u32 *p = fid->raw;
  65. int ret;
  66. /* NULL translates to ESTALE */
  67. if (fh_len < len || fh_type != len)
  68. return NULL;
  69. fattr = nfs_alloc_fattr_with_label(NFS_SB(sb));
  70. if (fattr == NULL) {
  71. dentry = ERR_PTR(-ENOMEM);
  72. goto out;
  73. }
  74. fattr->fileid = ((u64)p[FILEID_HIGH_OFF] << 32) + p[FILEID_LOW_OFF];
  75. fattr->mode = p[FILE_I_TYPE_OFF];
  76. fattr->valid |= NFS_ATTR_FATTR_FILEID | NFS_ATTR_FATTR_TYPE;
  77. dprintk("%s: fileid %llu mode %d\n", __func__, fattr->fileid, fattr->mode);
  78. inode = nfs_ilookup(sb, fattr, server_fh);
  79. if (inode)
  80. goto out_found;
  81. rpc_ops = NFS_SB(sb)->nfs_client->rpc_ops;
  82. ret = rpc_ops->getattr(NFS_SB(sb), server_fh, fattr, NULL);
  83. if (ret) {
  84. dprintk("%s: getattr failed %d\n", __func__, ret);
  85. trace_nfs_fh_to_dentry(sb, server_fh, fattr->fileid, ret);
  86. dentry = ERR_PTR(ret);
  87. goto out_free_fattr;
  88. }
  89. inode = nfs_fhget(sb, server_fh, fattr);
  90. out_found:
  91. dentry = d_obtain_alias(inode);
  92. out_free_fattr:
  93. nfs_free_fattr(fattr);
  94. out:
  95. return dentry;
  96. }
  97. static struct dentry *
  98. nfs_get_parent(struct dentry *dentry)
  99. {
  100. int ret;
  101. struct inode *inode = d_inode(dentry), *pinode;
  102. struct super_block *sb = inode->i_sb;
  103. struct nfs_server *server = NFS_SB(sb);
  104. struct nfs_fattr *fattr = NULL;
  105. struct dentry *parent;
  106. struct nfs_rpc_ops const *ops = server->nfs_client->rpc_ops;
  107. struct nfs_fh fh;
  108. if (!ops->lookupp)
  109. return ERR_PTR(-EACCES);
  110. fattr = nfs_alloc_fattr_with_label(server);
  111. if (fattr == NULL)
  112. return ERR_PTR(-ENOMEM);
  113. ret = ops->lookupp(inode, &fh, fattr);
  114. if (ret) {
  115. parent = ERR_PTR(ret);
  116. goto out;
  117. }
  118. pinode = nfs_fhget(sb, &fh, fattr);
  119. parent = d_obtain_alias(pinode);
  120. out:
  121. nfs_free_fattr(fattr);
  122. return parent;
  123. }
  124. static u64 nfs_fetch_iversion(struct inode *inode)
  125. {
  126. nfs_revalidate_inode(inode, NFS_INO_INVALID_CHANGE);
  127. return inode_peek_iversion_raw(inode);
  128. }
  129. const struct export_operations nfs_export_ops = {
  130. .encode_fh = nfs_encode_fh,
  131. .fh_to_dentry = nfs_fh_to_dentry,
  132. .get_parent = nfs_get_parent,
  133. .fetch_iversion = nfs_fetch_iversion,
  134. .flags = EXPORT_OP_NOWCC|EXPORT_OP_NOSUBTREECHK|
  135. EXPORT_OP_CLOSE_BEFORE_UNLINK|EXPORT_OP_REMOTE_FS|
  136. EXPORT_OP_NOATOMIC_ATTR,
  137. };