listxattr.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2022-2024 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_da_format.h"
  15. #include "xfs_da_btree.h"
  16. #include "xfs_attr.h"
  17. #include "xfs_attr_leaf.h"
  18. #include "xfs_attr_sf.h"
  19. #include "xfs_trans.h"
  20. #include "scrub/scrub.h"
  21. #include "scrub/bitmap.h"
  22. #include "scrub/dab_bitmap.h"
  23. #include "scrub/listxattr.h"
  24. /* Call a function for every entry in a shortform xattr structure. */
  25. STATIC int
  26. xchk_xattr_walk_sf(
  27. struct xfs_scrub *sc,
  28. struct xfs_inode *ip,
  29. xchk_xattr_fn attr_fn,
  30. void *priv)
  31. {
  32. struct xfs_attr_sf_hdr *hdr = ip->i_af.if_data;
  33. struct xfs_attr_sf_entry *sfe;
  34. unsigned int i;
  35. int error;
  36. sfe = xfs_attr_sf_firstentry(hdr);
  37. for (i = 0; i < hdr->count; i++) {
  38. error = attr_fn(sc, ip, sfe->flags, sfe->nameval, sfe->namelen,
  39. &sfe->nameval[sfe->namelen], sfe->valuelen,
  40. priv);
  41. if (error)
  42. return error;
  43. sfe = xfs_attr_sf_nextentry(sfe);
  44. }
  45. return 0;
  46. }
  47. /* Call a function for every entry in this xattr leaf block. */
  48. STATIC int
  49. xchk_xattr_walk_leaf_entries(
  50. struct xfs_scrub *sc,
  51. struct xfs_inode *ip,
  52. xchk_xattr_fn attr_fn,
  53. struct xfs_buf *bp,
  54. void *priv)
  55. {
  56. struct xfs_attr3_icleaf_hdr ichdr;
  57. struct xfs_mount *mp = sc->mp;
  58. struct xfs_attr_leafblock *leaf = bp->b_addr;
  59. struct xfs_attr_leaf_entry *entry;
  60. unsigned int i;
  61. int error;
  62. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
  63. entry = xfs_attr3_leaf_entryp(leaf);
  64. for (i = 0; i < ichdr.count; entry++, i++) {
  65. void *value;
  66. unsigned char *name;
  67. unsigned int namelen, valuelen;
  68. if (entry->flags & XFS_ATTR_LOCAL) {
  69. struct xfs_attr_leaf_name_local *name_loc;
  70. name_loc = xfs_attr3_leaf_name_local(leaf, i);
  71. name = name_loc->nameval;
  72. namelen = name_loc->namelen;
  73. value = &name_loc->nameval[name_loc->namelen];
  74. valuelen = be16_to_cpu(name_loc->valuelen);
  75. } else {
  76. struct xfs_attr_leaf_name_remote *name_rmt;
  77. name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
  78. name = name_rmt->name;
  79. namelen = name_rmt->namelen;
  80. value = NULL;
  81. valuelen = be32_to_cpu(name_rmt->valuelen);
  82. }
  83. error = attr_fn(sc, ip, entry->flags, name, namelen, value,
  84. valuelen, priv);
  85. if (error)
  86. return error;
  87. }
  88. return 0;
  89. }
  90. /*
  91. * Call a function for every entry in a leaf-format xattr structure. Avoid
  92. * memory allocations for the loop detector since there's only one block.
  93. */
  94. STATIC int
  95. xchk_xattr_walk_leaf(
  96. struct xfs_scrub *sc,
  97. struct xfs_inode *ip,
  98. xchk_xattr_fn attr_fn,
  99. void *priv)
  100. {
  101. struct xfs_buf *leaf_bp;
  102. int error;
  103. error = xfs_attr3_leaf_read(sc->tp, ip, ip->i_ino, 0, &leaf_bp);
  104. if (error)
  105. return error;
  106. error = xchk_xattr_walk_leaf_entries(sc, ip, attr_fn, leaf_bp, priv);
  107. xfs_trans_brelse(sc->tp, leaf_bp);
  108. return error;
  109. }
  110. /* Find the leftmost leaf in the xattr dabtree. */
  111. STATIC int
  112. xchk_xattr_find_leftmost_leaf(
  113. struct xfs_scrub *sc,
  114. struct xfs_inode *ip,
  115. struct xdab_bitmap *seen_dablks,
  116. struct xfs_buf **leaf_bpp)
  117. {
  118. struct xfs_da3_icnode_hdr nodehdr;
  119. struct xfs_mount *mp = sc->mp;
  120. struct xfs_trans *tp = sc->tp;
  121. struct xfs_da_intnode *node;
  122. struct xfs_da_node_entry *btree;
  123. struct xfs_buf *bp;
  124. xfs_failaddr_t fa;
  125. xfs_dablk_t blkno = 0;
  126. unsigned int expected_level = 0;
  127. int error;
  128. for (;;) {
  129. xfs_extlen_t len = 1;
  130. uint16_t magic;
  131. /* Make sure we haven't seen this new block already. */
  132. if (xdab_bitmap_test(seen_dablks, blkno, &len))
  133. return -EFSCORRUPTED;
  134. error = xfs_da3_node_read(tp, ip, blkno, &bp, XFS_ATTR_FORK);
  135. if (error)
  136. return error;
  137. node = bp->b_addr;
  138. magic = be16_to_cpu(node->hdr.info.magic);
  139. if (magic == XFS_ATTR_LEAF_MAGIC ||
  140. magic == XFS_ATTR3_LEAF_MAGIC)
  141. break;
  142. error = -EFSCORRUPTED;
  143. if (magic != XFS_DA_NODE_MAGIC &&
  144. magic != XFS_DA3_NODE_MAGIC)
  145. goto out_buf;
  146. fa = xfs_da3_node_header_check(bp, ip->i_ino);
  147. if (fa)
  148. goto out_buf;
  149. xfs_da3_node_hdr_from_disk(mp, &nodehdr, node);
  150. if (nodehdr.count == 0 || nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
  151. goto out_buf;
  152. /* Check the level from the root node. */
  153. if (blkno == 0)
  154. expected_level = nodehdr.level - 1;
  155. else if (expected_level != nodehdr.level)
  156. goto out_buf;
  157. else
  158. expected_level--;
  159. /* Remember that we've seen this node. */
  160. error = xdab_bitmap_set(seen_dablks, blkno, 1);
  161. if (error)
  162. goto out_buf;
  163. /* Find the next level towards the leaves of the dabtree. */
  164. btree = nodehdr.btree;
  165. blkno = be32_to_cpu(btree->before);
  166. xfs_trans_brelse(tp, bp);
  167. }
  168. error = -EFSCORRUPTED;
  169. fa = xfs_attr3_leaf_header_check(bp, ip->i_ino);
  170. if (fa)
  171. goto out_buf;
  172. if (expected_level != 0)
  173. goto out_buf;
  174. /* Remember that we've seen this leaf. */
  175. error = xdab_bitmap_set(seen_dablks, blkno, 1);
  176. if (error)
  177. goto out_buf;
  178. *leaf_bpp = bp;
  179. return 0;
  180. out_buf:
  181. xfs_trans_brelse(tp, bp);
  182. return error;
  183. }
  184. /* Call a function for every entry in a node-format xattr structure. */
  185. STATIC int
  186. xchk_xattr_walk_node(
  187. struct xfs_scrub *sc,
  188. struct xfs_inode *ip,
  189. xchk_xattr_fn attr_fn,
  190. xchk_xattrleaf_fn leaf_fn,
  191. void *priv)
  192. {
  193. struct xfs_attr3_icleaf_hdr leafhdr;
  194. struct xdab_bitmap seen_dablks;
  195. struct xfs_mount *mp = sc->mp;
  196. struct xfs_attr_leafblock *leaf;
  197. struct xfs_buf *leaf_bp;
  198. int error;
  199. xdab_bitmap_init(&seen_dablks);
  200. error = xchk_xattr_find_leftmost_leaf(sc, ip, &seen_dablks, &leaf_bp);
  201. if (error)
  202. goto out_bitmap;
  203. for (;;) {
  204. xfs_extlen_t len;
  205. error = xchk_xattr_walk_leaf_entries(sc, ip, attr_fn, leaf_bp,
  206. priv);
  207. if (error)
  208. goto out_leaf;
  209. /* Find the right sibling of this leaf block. */
  210. leaf = leaf_bp->b_addr;
  211. xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
  212. if (leafhdr.forw == 0)
  213. goto out_leaf;
  214. xfs_trans_brelse(sc->tp, leaf_bp);
  215. if (leaf_fn) {
  216. error = leaf_fn(sc, priv);
  217. if (error)
  218. goto out_bitmap;
  219. }
  220. /* Make sure we haven't seen this new leaf already. */
  221. len = 1;
  222. if (xdab_bitmap_test(&seen_dablks, leafhdr.forw, &len)) {
  223. error = -EFSCORRUPTED;
  224. goto out_bitmap;
  225. }
  226. error = xfs_attr3_leaf_read(sc->tp, ip, ip->i_ino,
  227. leafhdr.forw, &leaf_bp);
  228. if (error)
  229. goto out_bitmap;
  230. /* Remember that we've seen this new leaf. */
  231. error = xdab_bitmap_set(&seen_dablks, leafhdr.forw, 1);
  232. if (error)
  233. goto out_leaf;
  234. }
  235. out_leaf:
  236. xfs_trans_brelse(sc->tp, leaf_bp);
  237. out_bitmap:
  238. xdab_bitmap_destroy(&seen_dablks);
  239. return error;
  240. }
  241. /*
  242. * Call a function for every extended attribute in a file.
  243. *
  244. * Callers must hold the ILOCK. No validation or cursor restarts allowed.
  245. * Returns -EFSCORRUPTED on any problem, including loops in the dabtree.
  246. */
  247. int
  248. xchk_xattr_walk(
  249. struct xfs_scrub *sc,
  250. struct xfs_inode *ip,
  251. xchk_xattr_fn attr_fn,
  252. xchk_xattrleaf_fn leaf_fn,
  253. void *priv)
  254. {
  255. int error;
  256. xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
  257. if (!xfs_inode_hasattr(ip))
  258. return 0;
  259. if (ip->i_af.if_format == XFS_DINODE_FMT_LOCAL)
  260. return xchk_xattr_walk_sf(sc, ip, attr_fn, priv);
  261. /* attr functions require that the attr fork is loaded */
  262. error = xfs_iread_extents(sc->tp, ip, XFS_ATTR_FORK);
  263. if (error)
  264. return error;
  265. if (xfs_attr_is_leaf(ip))
  266. return xchk_xattr_walk_leaf(sc, ip, attr_fn, priv);
  267. return xchk_xattr_walk_node(sc, ip, attr_fn, leaf_fn, priv);
  268. }