xfs_export.c 6.2 KB

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