xfs_export.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2004-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_shared.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_dir2.h"
  13. #include "xfs_export.h"
  14. #include "xfs_inode.h"
  15. #include "xfs_trans.h"
  16. #include "xfs_inode_item.h"
  17. #include "xfs_icache.h"
  18. #include "xfs_pnfs.h"
  19. /*
  20. * Note that we only accept fileids which are long enough rather than allow
  21. * the parent generation number to default to zero. XFS considers zero a
  22. * valid generation number not an invalid/wildcard value.
  23. */
  24. static int xfs_fileid_length(int fileid_type)
  25. {
  26. switch (fileid_type) {
  27. case FILEID_INO32_GEN:
  28. return 2;
  29. case FILEID_INO32_GEN_PARENT:
  30. return 4;
  31. case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
  32. return 3;
  33. case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
  34. return 6;
  35. }
  36. return FILEID_INVALID;
  37. }
  38. STATIC int
  39. xfs_fs_encode_fh(
  40. struct inode *inode,
  41. __u32 *fh,
  42. int *max_len,
  43. struct inode *parent)
  44. {
  45. struct fid *fid = (struct fid *)fh;
  46. struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh;
  47. int fileid_type;
  48. int len;
  49. /* Directories don't need their parent encoded, they have ".." */
  50. if (!parent)
  51. fileid_type = FILEID_INO32_GEN;
  52. else
  53. fileid_type = FILEID_INO32_GEN_PARENT;
  54. /*
  55. * If the filesystem may contain 64bit inode numbers, we need
  56. * to use larger file handles that can represent them.
  57. *
  58. * While we only allocate inodes that do not fit into 32 bits any
  59. * large enough filesystem may contain them, thus the slightly
  60. * confusing looking conditional below.
  61. */
  62. if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS) ||
  63. (XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_32BITINODES))
  64. fileid_type |= XFS_FILEID_TYPE_64FLAG;
  65. /*
  66. * Only encode if there is enough space given. In practice
  67. * this means we can't export a filesystem with 64bit inodes
  68. * over NFSv2 with the subtree_check export option; the other
  69. * seven combinations work. The real answer is "don't use v2".
  70. */
  71. len = xfs_fileid_length(fileid_type);
  72. if (*max_len < len) {
  73. *max_len = len;
  74. return FILEID_INVALID;
  75. }
  76. *max_len = len;
  77. switch (fileid_type) {
  78. case FILEID_INO32_GEN_PARENT:
  79. fid->i32.parent_ino = XFS_I(parent)->i_ino;
  80. fid->i32.parent_gen = parent->i_generation;
  81. /*FALLTHRU*/
  82. case FILEID_INO32_GEN:
  83. fid->i32.ino = XFS_I(inode)->i_ino;
  84. fid->i32.gen = inode->i_generation;
  85. break;
  86. case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
  87. fid64->parent_ino = XFS_I(parent)->i_ino;
  88. fid64->parent_gen = parent->i_generation;
  89. /*FALLTHRU*/
  90. case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
  91. fid64->ino = XFS_I(inode)->i_ino;
  92. fid64->gen = inode->i_generation;
  93. break;
  94. }
  95. return fileid_type;
  96. }
  97. STATIC struct inode *
  98. xfs_nfs_get_inode(
  99. struct super_block *sb,
  100. u64 ino,
  101. u32 generation)
  102. {
  103. xfs_mount_t *mp = XFS_M(sb);
  104. xfs_inode_t *ip;
  105. int error;
  106. /*
  107. * NFS can sometimes send requests for ino 0. Fail them gracefully.
  108. */
  109. if (ino == 0)
  110. return ERR_PTR(-ESTALE);
  111. /*
  112. * The XFS_IGET_UNTRUSTED means that an invalid inode number is just
  113. * fine and not an indication of a corrupted filesystem as clients can
  114. * send invalid file handles and we have to handle it gracefully..
  115. */
  116. error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
  117. if (error) {
  118. /*
  119. * EINVAL means the inode cluster doesn't exist anymore.
  120. * EFSCORRUPTED means the metadata pointing to the inode cluster
  121. * or the inode cluster itself is corrupt. This implies the
  122. * filehandle is stale, so we should translate it here.
  123. * We don't use ESTALE directly down the chain to not
  124. * confuse applications using bulkstat that expect EINVAL.
  125. */
  126. switch (error) {
  127. case -EINVAL:
  128. case -ENOENT:
  129. case -EFSCORRUPTED:
  130. error = -ESTALE;
  131. break;
  132. default:
  133. break;
  134. }
  135. return ERR_PTR(error);
  136. }
  137. if (VFS_I(ip)->i_generation != generation) {
  138. xfs_irele(ip);
  139. return ERR_PTR(-ESTALE);
  140. }
  141. return VFS_I(ip);
  142. }
  143. STATIC struct dentry *
  144. xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  145. int fh_len, int fileid_type)
  146. {
  147. struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
  148. struct inode *inode = NULL;
  149. if (fh_len < xfs_fileid_length(fileid_type))
  150. return NULL;
  151. switch (fileid_type) {
  152. case FILEID_INO32_GEN_PARENT:
  153. case FILEID_INO32_GEN:
  154. inode = xfs_nfs_get_inode(sb, fid->i32.ino, fid->i32.gen);
  155. break;
  156. case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
  157. case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
  158. inode = xfs_nfs_get_inode(sb, fid64->ino, fid64->gen);
  159. break;
  160. }
  161. return d_obtain_alias(inode);
  162. }
  163. STATIC struct dentry *
  164. xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  165. int fh_len, int fileid_type)
  166. {
  167. struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
  168. struct inode *inode = NULL;
  169. if (fh_len < xfs_fileid_length(fileid_type))
  170. return NULL;
  171. switch (fileid_type) {
  172. case FILEID_INO32_GEN_PARENT:
  173. inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino,
  174. fid->i32.parent_gen);
  175. break;
  176. case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
  177. inode = xfs_nfs_get_inode(sb, fid64->parent_ino,
  178. fid64->parent_gen);
  179. break;
  180. }
  181. return d_obtain_alias(inode);
  182. }
  183. STATIC struct dentry *
  184. xfs_fs_get_parent(
  185. struct dentry *child)
  186. {
  187. int error;
  188. struct xfs_inode *cip;
  189. error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL);
  190. if (unlikely(error))
  191. return ERR_PTR(error);
  192. return d_obtain_alias(VFS_I(cip));
  193. }
  194. STATIC int
  195. xfs_fs_nfs_commit_metadata(
  196. struct inode *inode)
  197. {
  198. return xfs_log_force_inode(XFS_I(inode));
  199. }
  200. const struct export_operations xfs_export_operations = {
  201. .encode_fh = xfs_fs_encode_fh,
  202. .fh_to_dentry = xfs_fs_fh_to_dentry,
  203. .fh_to_parent = xfs_fs_fh_to_parent,
  204. .get_parent = xfs_fs_get_parent,
  205. .commit_metadata = xfs_fs_nfs_commit_metadata,
  206. #ifdef CONFIG_EXPORTFS_BLOCK_OPS
  207. .get_uuid = xfs_fs_get_uuid,
  208. .map_blocks = xfs_fs_map_blocks,
  209. .commit_blocks = xfs_fs_commit_blocks,
  210. #endif
  211. };