1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2022-2024 Oracle. All Rights Reserved.
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"
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"
25 /* Call a function for every entry in a shortform xattr structure. */
30 xchk_xattr_fn attr_fn,
33 struct xfs_attr_sf_hdr *hdr = ip->i_af.if_data;
34 struct xfs_attr_sf_entry *sfe;
38 sfe = xfs_attr_sf_firstentry(hdr);
39 for (i = 0; i < hdr->count; i++) {
40 error = attr_fn(sc, ip, sfe->flags, sfe->nameval, sfe->namelen,
41 &sfe->nameval[sfe->namelen], sfe->valuelen,
46 sfe = xfs_attr_sf_nextentry(sfe);
52 /* Call a function for every entry in this xattr leaf block. */
54 xchk_xattr_walk_leaf_entries(
57 xchk_xattr_fn attr_fn,
61 struct xfs_attr3_icleaf_hdr ichdr;
62 struct xfs_mount *mp = sc->mp;
63 struct xfs_attr_leafblock *leaf = bp->b_addr;
64 struct xfs_attr_leaf_entry *entry;
68 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &ichdr, leaf);
69 entry = xfs_attr3_leaf_entryp(leaf);
71 for (i = 0; i < ichdr.count; entry++, i++) {
74 unsigned int namelen, valuelen;
76 if (entry->flags & XFS_ATTR_LOCAL) {
77 struct xfs_attr_leaf_name_local *name_loc;
79 name_loc = xfs_attr3_leaf_name_local(leaf, i);
80 name = name_loc->nameval;
81 namelen = name_loc->namelen;
82 value = &name_loc->nameval[name_loc->namelen];
83 valuelen = be16_to_cpu(name_loc->valuelen);
85 struct xfs_attr_leaf_name_remote *name_rmt;
87 name_rmt = xfs_attr3_leaf_name_remote(leaf, i);
88 name = name_rmt->name;
89 namelen = name_rmt->namelen;
91 valuelen = be32_to_cpu(name_rmt->valuelen);
94 error = attr_fn(sc, ip, entry->flags, name, namelen, value,
105 * Call a function for every entry in a leaf-format xattr structure. Avoid
106 * memory allocations for the loop detector since there's only one block.
109 xchk_xattr_walk_leaf(
110 struct xfs_scrub *sc,
111 struct xfs_inode *ip,
112 xchk_xattr_fn attr_fn,
115 struct xfs_buf *leaf_bp;
118 error = xfs_attr3_leaf_read(sc->tp, ip, ip->i_ino, 0, &leaf_bp);
122 error = xchk_xattr_walk_leaf_entries(sc, ip, attr_fn, leaf_bp, priv);
123 xfs_trans_brelse(sc->tp, leaf_bp);
127 /* Find the leftmost leaf in the xattr dabtree. */
129 xchk_xattr_find_leftmost_leaf(
130 struct xfs_scrub *sc,
131 struct xfs_inode *ip,
132 struct xdab_bitmap *seen_dablks,
133 struct xfs_buf **leaf_bpp)
135 struct xfs_da3_icnode_hdr nodehdr;
136 struct xfs_mount *mp = sc->mp;
137 struct xfs_trans *tp = sc->tp;
138 struct xfs_da_intnode *node;
139 struct xfs_da_node_entry *btree;
142 xfs_dablk_t blkno = 0;
143 unsigned int expected_level = 0;
147 xfs_extlen_t len = 1;
150 /* Make sure we haven't seen this new block already. */
151 if (xdab_bitmap_test(seen_dablks, blkno, &len))
152 return -EFSCORRUPTED;
154 error = xfs_da3_node_read(tp, ip, blkno, &bp, XFS_ATTR_FORK);
159 magic = be16_to_cpu(node->hdr.info.magic);
160 if (magic == XFS_ATTR_LEAF_MAGIC ||
161 magic == XFS_ATTR3_LEAF_MAGIC)
164 error = -EFSCORRUPTED;
165 if (magic != XFS_DA_NODE_MAGIC &&
166 magic != XFS_DA3_NODE_MAGIC)
169 fa = xfs_da3_node_header_check(bp, ip->i_ino);
173 xfs_da3_node_hdr_from_disk(mp, &nodehdr, node);
175 if (nodehdr.count == 0 || nodehdr.level >= XFS_DA_NODE_MAXDEPTH)
178 /* Check the level from the root node. */
180 expected_level = nodehdr.level - 1;
181 else if (expected_level != nodehdr.level)
186 /* Remember that we've seen this node. */
187 error = xdab_bitmap_set(seen_dablks, blkno, 1);
191 /* Find the next level towards the leaves of the dabtree. */
192 btree = nodehdr.btree;
193 blkno = be32_to_cpu(btree->before);
194 xfs_trans_brelse(tp, bp);
197 error = -EFSCORRUPTED;
198 fa = xfs_attr3_leaf_header_check(bp, ip->i_ino);
202 if (expected_level != 0)
205 /* Remember that we've seen this leaf. */
206 error = xdab_bitmap_set(seen_dablks, blkno, 1);
214 xfs_trans_brelse(tp, bp);
218 /* Call a function for every entry in a node-format xattr structure. */
220 xchk_xattr_walk_node(
221 struct xfs_scrub *sc,
222 struct xfs_inode *ip,
223 xchk_xattr_fn attr_fn,
224 xchk_xattrleaf_fn leaf_fn,
227 struct xfs_attr3_icleaf_hdr leafhdr;
228 struct xdab_bitmap seen_dablks;
229 struct xfs_mount *mp = sc->mp;
230 struct xfs_attr_leafblock *leaf;
231 struct xfs_buf *leaf_bp;
234 xdab_bitmap_init(&seen_dablks);
236 error = xchk_xattr_find_leftmost_leaf(sc, ip, &seen_dablks, &leaf_bp);
243 error = xchk_xattr_walk_leaf_entries(sc, ip, attr_fn, leaf_bp,
248 /* Find the right sibling of this leaf block. */
249 leaf = leaf_bp->b_addr;
250 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
251 if (leafhdr.forw == 0)
254 xfs_trans_brelse(sc->tp, leaf_bp);
257 error = leaf_fn(sc, priv);
262 /* Make sure we haven't seen this new leaf already. */
264 if (xdab_bitmap_test(&seen_dablks, leafhdr.forw, &len)) {
265 error = -EFSCORRUPTED;
269 error = xfs_attr3_leaf_read(sc->tp, ip, ip->i_ino,
270 leafhdr.forw, &leaf_bp);
274 /* Remember that we've seen this new leaf. */
275 error = xdab_bitmap_set(&seen_dablks, leafhdr.forw, 1);
281 xfs_trans_brelse(sc->tp, leaf_bp);
283 xdab_bitmap_destroy(&seen_dablks);
288 * Call a function for every extended attribute in a file.
290 * Callers must hold the ILOCK. No validation or cursor restarts allowed.
291 * Returns -EFSCORRUPTED on any problem, including loops in the dabtree.
295 struct xfs_scrub *sc,
296 struct xfs_inode *ip,
297 xchk_xattr_fn attr_fn,
298 xchk_xattrleaf_fn leaf_fn,
303 xfs_assert_ilocked(ip, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
305 if (!xfs_inode_hasattr(ip))
308 if (ip->i_af.if_format == XFS_DINODE_FMT_LOCAL)
309 return xchk_xattr_walk_sf(sc, ip, attr_fn, priv);
311 /* attr functions require that the attr fork is loaded */
312 error = xfs_iread_extents(sc->tp, ip, XFS_ATTR_FORK);
316 if (xfs_attr_is_leaf(ip))
317 return xchk_xattr_walk_leaf(sc, ip, attr_fn, priv);
319 return xchk_xattr_walk_node(sc, ip, attr_fn, leaf_fn, priv);