1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_log_format.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 "scrub/scrub.h"
20 #include "scrub/common.h"
21 #include "scrub/dabtree.h"
22 #include "scrub/attr.h"
24 /* Free the buffers linked from the xattr buffer. */
26 xchk_xattr_buf_cleanup(
29 struct xchk_xattr_buf *ab = priv;
41 * Allocate the free space bitmap if we're trying harder; there are leaf blocks
42 * in the attr fork; or we can't tell if there are leaf blocks.
45 xchk_xattr_want_freemap(
48 struct xfs_ifork *ifp;
50 if (sc->flags & XCHK_TRY_HARDER)
56 ifp = xfs_ifork_ptr(sc->ip, XFS_ATTR_FORK);
60 return xfs_ifork_has_extents(ifp);
64 * Allocate enough memory to hold an attr value and attr block bitmaps,
65 * reallocating the buffer if necessary. Buffer contents are not preserved
66 * across a reallocation.
74 struct xchk_xattr_buf *ab = sc->buf;
77 bmp_sz = sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
82 ab = kvzalloc(sizeof(struct xchk_xattr_buf), XCHK_GFP_FLAGS);
86 sc->buf_cleanup = xchk_xattr_buf_cleanup;
88 ab->usedmap = kvmalloc(bmp_sz, XCHK_GFP_FLAGS);
92 if (xchk_xattr_want_freemap(sc)) {
93 ab->freemap = kvmalloc(bmp_sz, XCHK_GFP_FLAGS);
99 if (ab->value_sz >= value_size)
108 new_val = kvmalloc(value_size, XCHK_GFP_FLAGS);
113 ab->value_sz = value_size;
117 /* Set us up to scrub an inode's extended attributes. */
120 struct xfs_scrub *sc)
125 * We failed to get memory while checking attrs, so this time try to
126 * get all the memory we're ever going to need. Allocate the buffer
127 * without the inode lock held, which means we can sleep.
129 if (sc->flags & XCHK_TRY_HARDER) {
130 error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX);
135 return xchk_setup_inode_contents(sc, 0);
138 /* Extended Attributes */
141 struct xfs_attr_list_context context;
142 struct xfs_scrub *sc;
146 * Check that an extended attribute key can be looked up by hash.
148 * We use the XFS attribute list iterator (i.e. xfs_attr_list_ilocked)
149 * to call this function for every attribute key in an inode. Once
150 * we're here, we load the attribute value to see if any errors happen,
151 * or if we get more or less data than we expected.
155 struct xfs_attr_list_context *context,
161 struct xfs_da_args args = {
162 .op_flags = XFS_DA_OP_NOTIME,
163 .attr_filter = flags & XFS_ATTR_NSP_ONDISK_MASK,
164 .geo = context->dp->i_mount->m_attr_geo,
165 .whichfork = XFS_ATTR_FORK,
169 .hashval = xfs_da_hashname(name, namelen),
170 .trans = context->tp,
171 .valuelen = valuelen,
173 struct xchk_xattr_buf *ab;
174 struct xchk_xattr *sx;
177 sx = container_of(context, struct xchk_xattr, context);
180 if (xchk_should_terminate(sx->sc, &error)) {
181 context->seen_enough = error;
185 if (flags & XFS_ATTR_INCOMPLETE) {
186 /* Incomplete attr key, just mark the inode for preening. */
187 xchk_ino_set_preen(sx->sc, context->dp->i_ino);
191 /* Only one namespace bit allowed. */
192 if (hweight32(flags & XFS_ATTR_NSP_ONDISK_MASK) > 1) {
193 xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK, args.blkno);
197 /* Does this name make sense? */
198 if (!xfs_attr_namecheck(name, namelen)) {
199 xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK, args.blkno);
204 * Local xattr values are stored in the attr leaf block, so we don't
205 * need to retrieve the value from a remote block to detect corruption
208 if (flags & XFS_ATTR_LOCAL)
212 * Try to allocate enough memory to extrat the attr value. If that
213 * doesn't work, we overload the seen_enough variable to convey
214 * the error message back to the main scrub function.
216 error = xchk_setup_xattr_buf(sx->sc, valuelen);
217 if (error == -ENOMEM)
220 context->seen_enough = error;
224 args.value = ab->value;
226 error = xfs_attr_get_ilocked(&args);
227 /* ENODATA means the hash lookup failed and the attr is bad */
228 if (error == -ENODATA)
229 error = -EFSCORRUPTED;
230 if (!xchk_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
233 if (args.valuelen != valuelen)
234 xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK,
237 if (sx->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
238 context->seen_enough = 1;
243 * Mark a range [start, start+len) in this map. Returns true if the
244 * region was free, and false if there's a conflict or a problem.
246 * Within a char, the lowest bit of the char represents the byte with
247 * the smallest address
251 struct xfs_scrub *sc,
256 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
259 if (start >= mapsize)
261 if (start + len > mapsize) {
262 len = mapsize - start;
266 if (find_next_bit(map, mapsize, start) < start + len)
268 bitmap_set(map, start, len);
274 * Check the leaf freemap from the usage bitmap. Returns false if the
275 * attr freemap has problems or points to used space.
278 xchk_xattr_check_freemap(
279 struct xfs_scrub *sc,
280 struct xfs_attr3_icleaf_hdr *leafhdr)
282 struct xchk_xattr_buf *ab = sc->buf;
283 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
286 /* Construct bitmap of freemap contents. */
287 bitmap_zero(ab->freemap, mapsize);
288 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
289 if (!xchk_xattr_set_map(sc, ab->freemap,
290 leafhdr->freemap[i].base,
291 leafhdr->freemap[i].size))
295 /* Look for bits that are set in freemap and are marked in use. */
296 return !bitmap_intersects(ab->freemap, ab->usedmap, mapsize);
300 * Check this leaf entry's relations to everything else.
301 * Returns the number of bytes used for the name/value data.
305 struct xchk_da_btree *ds,
308 struct xfs_attr_leafblock *leaf,
309 struct xfs_attr3_icleaf_hdr *leafhdr,
310 struct xfs_attr_leaf_entry *ent,
312 unsigned int *usedbytes,
315 struct xfs_mount *mp = ds->state->mp;
316 struct xchk_xattr_buf *ab = ds->sc->buf;
318 struct xfs_attr_leaf_name_local *lentry;
319 struct xfs_attr_leaf_name_remote *rentry;
320 unsigned int nameidx;
321 unsigned int namesize;
324 xchk_da_set_corrupt(ds, level);
326 /* Hash values in order? */
327 if (be32_to_cpu(ent->hashval) < *last_hashval)
328 xchk_da_set_corrupt(ds, level);
329 *last_hashval = be32_to_cpu(ent->hashval);
331 nameidx = be16_to_cpu(ent->nameidx);
332 if (nameidx < leafhdr->firstused ||
333 nameidx >= mp->m_attr_geo->blksize) {
334 xchk_da_set_corrupt(ds, level);
338 /* Check the name information. */
339 if (ent->flags & XFS_ATTR_LOCAL) {
340 lentry = xfs_attr3_leaf_name_local(leaf, idx);
341 namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
342 be16_to_cpu(lentry->valuelen));
343 name_end = (char *)lentry + namesize;
344 if (lentry->namelen == 0)
345 xchk_da_set_corrupt(ds, level);
347 rentry = xfs_attr3_leaf_name_remote(leaf, idx);
348 namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
349 name_end = (char *)rentry + namesize;
350 if (rentry->namelen == 0 || rentry->valueblk == 0)
351 xchk_da_set_corrupt(ds, level);
353 if (name_end > buf_end)
354 xchk_da_set_corrupt(ds, level);
356 if (!xchk_xattr_set_map(ds->sc, ab->usedmap, nameidx, namesize))
357 xchk_da_set_corrupt(ds, level);
358 if (!(ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
359 *usedbytes += namesize;
362 /* Scrub an attribute leaf. */
365 struct xchk_da_btree *ds,
368 struct xfs_attr3_icleaf_hdr leafhdr;
369 struct xfs_mount *mp = ds->state->mp;
370 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
371 struct xfs_buf *bp = blk->bp;
372 xfs_dablk_t *last_checked = ds->private;
373 struct xfs_attr_leafblock *leaf = bp->b_addr;
374 struct xfs_attr_leaf_entry *ent;
375 struct xfs_attr_leaf_entry *entries;
376 struct xchk_xattr_buf *ab = ds->sc->buf;
379 __u32 last_hashval = 0;
380 unsigned int usedbytes = 0;
381 unsigned int hdrsize;
384 if (*last_checked == blk->blkno)
387 *last_checked = blk->blkno;
388 bitmap_zero(ab->usedmap, mp->m_attr_geo->blksize);
390 /* Check all the padding. */
391 if (xfs_has_crc(ds->sc->mp)) {
392 struct xfs_attr3_leafblock *leaf3 = bp->b_addr;
394 if (leaf3->hdr.pad1 != 0 || leaf3->hdr.pad2 != 0 ||
395 leaf3->hdr.info.hdr.pad != 0)
396 xchk_da_set_corrupt(ds, level);
398 if (leaf->hdr.pad1 != 0 || leaf->hdr.info.pad != 0)
399 xchk_da_set_corrupt(ds, level);
402 /* Check the leaf header */
403 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
404 hdrsize = xfs_attr3_leaf_hdr_size(leaf);
406 if (leafhdr.usedbytes > mp->m_attr_geo->blksize)
407 xchk_da_set_corrupt(ds, level);
408 if (leafhdr.firstused > mp->m_attr_geo->blksize)
409 xchk_da_set_corrupt(ds, level);
410 if (leafhdr.firstused < hdrsize)
411 xchk_da_set_corrupt(ds, level);
412 if (!xchk_xattr_set_map(ds->sc, ab->usedmap, 0, hdrsize))
413 xchk_da_set_corrupt(ds, level);
415 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
418 entries = xfs_attr3_leaf_entryp(leaf);
419 if ((char *)&entries[leafhdr.count] > (char *)leaf + leafhdr.firstused)
420 xchk_da_set_corrupt(ds, level);
422 buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
423 for (i = 0, ent = entries; i < leafhdr.count; ent++, i++) {
424 /* Mark the leaf entry itself. */
425 off = (char *)ent - (char *)leaf;
426 if (!xchk_xattr_set_map(ds->sc, ab->usedmap, off,
427 sizeof(xfs_attr_leaf_entry_t))) {
428 xchk_da_set_corrupt(ds, level);
432 /* Check the entry and nameval. */
433 xchk_xattr_entry(ds, level, buf_end, leaf, &leafhdr,
434 ent, i, &usedbytes, &last_hashval);
436 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
440 if (!xchk_xattr_check_freemap(ds->sc, &leafhdr))
441 xchk_da_set_corrupt(ds, level);
443 if (leafhdr.usedbytes != usedbytes)
444 xchk_da_set_corrupt(ds, level);
450 /* Scrub a attribute btree record. */
453 struct xchk_da_btree *ds,
456 struct xfs_mount *mp = ds->state->mp;
457 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
458 struct xfs_attr_leaf_name_local *lentry;
459 struct xfs_attr_leaf_name_remote *rentry;
461 struct xfs_attr_leaf_entry *ent;
462 xfs_dahash_t calc_hash;
466 unsigned int badflags;
469 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
471 ent = xfs_attr3_leaf_entryp(blk->bp->b_addr) + blk->index;
473 /* Check the whole block, if necessary. */
474 error = xchk_xattr_block(ds, level);
477 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
480 /* Check the hash of the entry. */
481 error = xchk_da_btree_hash(ds, level, &ent->hashval);
485 /* Find the attr entry's location. */
487 hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr);
488 nameidx = be16_to_cpu(ent->nameidx);
489 if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) {
490 xchk_da_set_corrupt(ds, level);
494 /* Retrieve the entry and check it. */
495 hash = be32_to_cpu(ent->hashval);
496 badflags = ~(XFS_ATTR_LOCAL | XFS_ATTR_ROOT | XFS_ATTR_SECURE |
497 XFS_ATTR_INCOMPLETE);
498 if ((ent->flags & badflags) != 0)
499 xchk_da_set_corrupt(ds, level);
500 if (ent->flags & XFS_ATTR_LOCAL) {
501 lentry = (struct xfs_attr_leaf_name_local *)
502 (((char *)bp->b_addr) + nameidx);
503 if (lentry->namelen <= 0) {
504 xchk_da_set_corrupt(ds, level);
507 calc_hash = xfs_da_hashname(lentry->nameval, lentry->namelen);
509 rentry = (struct xfs_attr_leaf_name_remote *)
510 (((char *)bp->b_addr) + nameidx);
511 if (rentry->namelen <= 0) {
512 xchk_da_set_corrupt(ds, level);
515 calc_hash = xfs_da_hashname(rentry->name, rentry->namelen);
517 if (calc_hash != hash)
518 xchk_da_set_corrupt(ds, level);
524 /* Check space usage of shortform attrs. */
527 struct xfs_scrub *sc)
529 struct xchk_xattr_buf *ab = sc->buf;
530 struct xfs_attr_shortform *sf;
531 struct xfs_attr_sf_entry *sfe;
532 struct xfs_attr_sf_entry *next;
533 struct xfs_ifork *ifp;
538 ifp = xfs_ifork_ptr(sc->ip, XFS_ATTR_FORK);
540 bitmap_zero(ab->usedmap, ifp->if_bytes);
541 sf = (struct xfs_attr_shortform *)sc->ip->i_af.if_u1.if_data;
542 end = (unsigned char *)ifp->if_u1.if_data + ifp->if_bytes;
543 xchk_xattr_set_map(sc, ab->usedmap, 0, sizeof(sf->hdr));
546 if ((unsigned char *)sfe > end) {
547 xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
551 for (i = 0; i < sf->hdr.count; i++) {
552 unsigned char *name = sfe->nameval;
553 unsigned char *value = &sfe->nameval[sfe->namelen];
555 if (xchk_should_terminate(sc, &error))
558 next = xfs_attr_sf_nextentry(sfe);
559 if ((unsigned char *)next > end) {
560 xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
564 if (!xchk_xattr_set_map(sc, ab->usedmap,
565 (char *)sfe - (char *)sf,
566 sizeof(struct xfs_attr_sf_entry))) {
567 xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
571 if (!xchk_xattr_set_map(sc, ab->usedmap,
572 (char *)name - (char *)sf,
574 xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
578 if (!xchk_xattr_set_map(sc, ab->usedmap,
579 (char *)value - (char *)sf,
581 xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0);
591 /* Scrub the extended attribute metadata. */
594 struct xfs_scrub *sc)
596 struct xchk_xattr sx = {
602 .put_listent = xchk_xattr_listent,
603 .allow_incomplete = true,
606 xfs_dablk_t last_checked = -1U;
609 if (!xfs_inode_hasattr(sc->ip))
612 /* Allocate memory for xattr checking. */
613 error = xchk_setup_xattr_buf(sc, 0);
614 if (error == -ENOMEM)
619 /* Check the physical structure of the xattr. */
620 if (sc->ip->i_af.if_format == XFS_DINODE_FMT_LOCAL)
621 error = xchk_xattr_check_sf(sc);
623 error = xchk_da_btree(sc, XFS_ATTR_FORK, xchk_xattr_rec,
628 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
632 * Look up every xattr in this file by name and hash.
634 * Use the backend implementation of xfs_attr_list to call
635 * xchk_xattr_listent on every attribute key in this inode.
636 * In other words, we use the same iterator/callback mechanism
637 * that listattr uses to scrub extended attributes, though in our
638 * _listent function, we check the value of the attribute.
640 * The VFS only locks i_rwsem when modifying attrs, so keep all
641 * three locks held because that's the only way to ensure we're
642 * the only thread poking into the da btree. We traverse the da
643 * btree while holding a leaf buffer locked for the xattr name
644 * iteration, which doesn't really follow the usual buffer
647 error = xfs_attr_list_ilocked(&sx.context);
648 if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))
651 /* Did our listent function try to return any errors? */
652 if (sx.context.seen_enough < 0)
653 return sx.context.seen_enough;