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_btree.h"
14 #include "xfs_log_format.h"
15 #include "xfs_trans.h"
16 #include "xfs_inode.h"
17 #include "xfs_alloc.h"
19 #include "xfs_bmap_btree.h"
21 #include "xfs_rmap_btree.h"
22 #include "xfs_health.h"
23 #include "scrub/scrub.h"
24 #include "scrub/common.h"
25 #include "scrub/btree.h"
26 #include "scrub/health.h"
29 /* Set us up with an inode's bmap. */
31 xchk_setup_inode_bmap(
36 if (xchk_need_intent_drain(sc))
37 xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
39 error = xchk_iget_for_scrubbing(sc);
43 xchk_ilock(sc, XFS_IOLOCK_EXCL);
46 * We don't want any ephemeral data/cow fork updates sitting around
47 * while we inspect block mappings, so wait for directio to finish
48 * and flush dirty data if we have delalloc reservations.
50 if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
51 sc->sm->sm_type != XFS_SCRUB_TYPE_BMBTA) {
52 struct address_space *mapping = VFS_I(sc->ip)->i_mapping;
53 bool is_repair = xchk_could_repair(sc);
55 xchk_ilock(sc, XFS_MMAPLOCK_EXCL);
57 /* Break all our leases, we're going to mess with things. */
59 error = xfs_break_layouts(VFS_I(sc->ip),
60 &sc->ilock_flags, BREAK_WRITE);
65 inode_dio_wait(VFS_I(sc->ip));
68 * Try to flush all incore state to disk before we examine the
69 * space mappings for the data fork. Leave accumulated errors
70 * in the mapping for the writer threads to consume.
72 * On ENOSPC or EIO writeback errors, we continue into the
73 * extent mapping checks because write failures do not
74 * necessarily imply anything about the correctness of the file
75 * metadata. The metadata and the file data could be on
76 * completely separate devices; a media failure might only
77 * affect a subset of the disk, etc. We can handle delalloc
78 * extents in the scrubber, so leaving them in memory is fine.
80 error = filemap_fdatawrite(mapping);
82 error = filemap_fdatawait_keep_errors(mapping);
83 if (error && (error != -ENOSPC && error != -EIO))
86 /* Drop the page cache if we're repairing block mappings. */
88 error = invalidate_inode_pages2(
89 VFS_I(sc->ip)->i_mapping);
96 /* Got the inode, lock it and we're ready to go. */
97 error = xchk_trans_alloc(sc, 0);
101 error = xchk_ino_dqattach(sc);
105 xchk_ilock(sc, XFS_ILOCK_EXCL);
107 /* scrub teardown will unlock and release the inode */
112 * Inode fork block mapping (BMBT) scrubber.
113 * More complex than the others because we have to scrub
114 * all the extents regardless of whether or not the fork
115 * is in btree format.
118 struct xchk_bmap_info {
119 struct xfs_scrub *sc;
121 /* Incore extent tree cursor */
122 struct xfs_iext_cursor icur;
124 /* Previous fork mapping that we examined */
125 struct xfs_bmbt_irec prev_rec;
127 /* Is this a realtime fork? */
130 /* May mappings point to shared space? */
133 /* Was the incore extent tree loaded? */
136 /* Which inode fork are we checking? */
140 /* Look for a corresponding rmap for this irec. */
143 struct xchk_bmap_info *info,
144 struct xfs_bmbt_irec *irec,
147 struct xfs_rmap_irec *rmap)
149 xfs_fileoff_t offset;
150 unsigned int rflags = 0;
154 if (info->whichfork == XFS_ATTR_FORK)
155 rflags |= XFS_RMAP_ATTR_FORK;
156 if (irec->br_state == XFS_EXT_UNWRITTEN)
157 rflags |= XFS_RMAP_UNWRITTEN;
160 * CoW staging extents are owned (on disk) by the refcountbt, so
161 * their rmaps do not have offsets.
163 if (info->whichfork == XFS_COW_FORK)
166 offset = irec->br_startoff;
169 * If the caller thinks this could be a shared bmbt extent (IOWs,
170 * any data fork extent of a reflink inode) then we have to use the
171 * range rmap lookup to make sure we get the correct owner/offset.
173 if (info->is_shared) {
174 error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
175 owner, offset, rflags, rmap, &has_rmap);
177 error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno,
178 owner, offset, rflags, rmap, &has_rmap);
180 if (!xchk_should_check_xref(info->sc, &error, &info->sc->sa.rmap_cur))
184 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
189 /* Make sure that we have rmapbt records for this data/attr fork extent. */
192 struct xchk_bmap_info *info,
193 struct xfs_bmbt_irec *irec,
196 struct xfs_rmap_irec rmap;
197 unsigned long long rmap_end;
198 uint64_t owner = info->sc->ip->i_ino;
200 if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
203 /* Find the rmap record for this irec. */
204 if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
208 * The rmap must be an exact match for this incore file mapping record,
209 * which may have arisen from multiple ondisk records.
211 if (rmap.rm_startblock != agbno)
212 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
215 rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
216 if (rmap_end != agbno + irec->br_blockcount)
217 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
220 /* Check the logical offsets. */
221 if (rmap.rm_offset != irec->br_startoff)
222 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
225 rmap_end = (unsigned long long)rmap.rm_offset + rmap.rm_blockcount;
226 if (rmap_end != irec->br_startoff + irec->br_blockcount)
227 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
230 /* Check the owner */
231 if (rmap.rm_owner != owner)
232 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
236 * Check for discrepancies between the unwritten flag in the irec and
237 * the rmap. Note that the (in-memory) CoW fork distinguishes between
238 * unwritten and written extents, but we don't track that in the rmap
239 * records because the blocks are owned (on-disk) by the refcountbt,
240 * which doesn't track unwritten state.
242 if (!!(irec->br_state == XFS_EXT_UNWRITTEN) !=
243 !!(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
244 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
247 if (!!(info->whichfork == XFS_ATTR_FORK) !=
248 !!(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
249 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
251 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
252 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
256 /* Make sure that we have rmapbt records for this COW fork extent. */
258 xchk_bmap_xref_rmap_cow(
259 struct xchk_bmap_info *info,
260 struct xfs_bmbt_irec *irec,
263 struct xfs_rmap_irec rmap;
264 unsigned long long rmap_end;
265 uint64_t owner = XFS_RMAP_OWN_COW;
267 if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
270 /* Find the rmap record for this irec. */
271 if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
275 * CoW staging extents are owned by the refcount btree, so the rmap
276 * can start before and end after the physical space allocated to this
277 * mapping. There are no offsets to check.
279 if (rmap.rm_startblock > agbno)
280 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
283 rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
284 if (rmap_end < agbno + irec->br_blockcount)
285 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
288 /* Check the owner */
289 if (rmap.rm_owner != owner)
290 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
294 * No flags allowed. Note that the (in-memory) CoW fork distinguishes
295 * between unwritten and written extents, but we don't track that in
296 * the rmap records because the blocks are owned (on-disk) by the
297 * refcountbt, which doesn't track unwritten state.
299 if (rmap.rm_flags & XFS_RMAP_ATTR_FORK)
300 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
302 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
303 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
305 if (rmap.rm_flags & XFS_RMAP_UNWRITTEN)
306 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
310 /* Cross-reference a single rtdev extent record. */
312 xchk_bmap_rt_iextent_xref(
313 struct xfs_inode *ip,
314 struct xchk_bmap_info *info,
315 struct xfs_bmbt_irec *irec)
317 xchk_xref_is_used_rt_space(info->sc, irec->br_startblock,
318 irec->br_blockcount);
321 /* Cross-reference a single datadev extent record. */
323 xchk_bmap_iextent_xref(
324 struct xfs_inode *ip,
325 struct xchk_bmap_info *info,
326 struct xfs_bmbt_irec *irec)
328 struct xfs_owner_info oinfo;
329 struct xfs_mount *mp = info->sc->mp;
335 agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
336 agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
337 len = irec->br_blockcount;
339 error = xchk_ag_init_existing(info->sc, agno, &info->sc->sa);
340 if (!xchk_fblock_process_error(info->sc, info->whichfork,
341 irec->br_startoff, &error))
344 xchk_xref_is_used_space(info->sc, agbno, len);
345 xchk_xref_is_not_inode_chunk(info->sc, agbno, len);
346 switch (info->whichfork) {
348 xchk_bmap_xref_rmap(info, irec, agbno);
349 if (!xfs_is_reflink_inode(info->sc->ip)) {
350 xfs_rmap_ino_owner(&oinfo, info->sc->ip->i_ino,
351 info->whichfork, irec->br_startoff);
352 xchk_xref_is_only_owned_by(info->sc, agbno,
353 irec->br_blockcount, &oinfo);
354 xchk_xref_is_not_shared(info->sc, agbno,
355 irec->br_blockcount);
357 xchk_xref_is_not_cow_staging(info->sc, agbno,
358 irec->br_blockcount);
361 xchk_bmap_xref_rmap(info, irec, agbno);
362 xfs_rmap_ino_owner(&oinfo, info->sc->ip->i_ino,
363 info->whichfork, irec->br_startoff);
364 xchk_xref_is_only_owned_by(info->sc, agbno, irec->br_blockcount,
366 xchk_xref_is_not_shared(info->sc, agbno,
367 irec->br_blockcount);
368 xchk_xref_is_not_cow_staging(info->sc, agbno,
369 irec->br_blockcount);
372 xchk_bmap_xref_rmap_cow(info, irec, agbno);
373 xchk_xref_is_only_owned_by(info->sc, agbno, irec->br_blockcount,
374 &XFS_RMAP_OINFO_COW);
375 xchk_xref_is_cow_staging(info->sc, agbno,
376 irec->br_blockcount);
377 xchk_xref_is_not_shared(info->sc, agbno,
378 irec->br_blockcount);
383 xchk_ag_free(info->sc, &info->sc->sa);
387 * Directories and attr forks should never have blocks that can't be addressed
391 xchk_bmap_dirattr_extent(
392 struct xfs_inode *ip,
393 struct xchk_bmap_info *info,
394 struct xfs_bmbt_irec *irec)
396 struct xfs_mount *mp = ip->i_mount;
399 if (!S_ISDIR(VFS_I(ip)->i_mode) && info->whichfork != XFS_ATTR_FORK)
402 if (!xfs_verify_dablk(mp, irec->br_startoff))
403 xchk_fblock_set_corrupt(info->sc, info->whichfork,
406 off = irec->br_startoff + irec->br_blockcount - 1;
407 if (!xfs_verify_dablk(mp, off))
408 xchk_fblock_set_corrupt(info->sc, info->whichfork, off);
411 /* Scrub a single extent record. */
414 struct xfs_inode *ip,
415 struct xchk_bmap_info *info,
416 struct xfs_bmbt_irec *irec)
418 struct xfs_mount *mp = info->sc->mp;
421 * Check for out-of-order extents. This record could have come
422 * from the incore list, for which there is no ordering check.
424 if (irec->br_startoff < info->prev_rec.br_startoff +
425 info->prev_rec.br_blockcount)
426 xchk_fblock_set_corrupt(info->sc, info->whichfork,
429 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
430 xchk_fblock_set_corrupt(info->sc, info->whichfork,
433 xchk_bmap_dirattr_extent(ip, info, irec);
435 /* Make sure the extent points to a valid place. */
437 !xfs_verify_rtbext(mp, irec->br_startblock, irec->br_blockcount))
438 xchk_fblock_set_corrupt(info->sc, info->whichfork,
441 !xfs_verify_fsbext(mp, irec->br_startblock, irec->br_blockcount))
442 xchk_fblock_set_corrupt(info->sc, info->whichfork,
445 /* We don't allow unwritten extents on attr forks. */
446 if (irec->br_state == XFS_EXT_UNWRITTEN &&
447 info->whichfork == XFS_ATTR_FORK)
448 xchk_fblock_set_corrupt(info->sc, info->whichfork,
451 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
455 xchk_bmap_rt_iextent_xref(ip, info, irec);
457 xchk_bmap_iextent_xref(ip, info, irec);
460 /* Scrub a bmbt record. */
463 struct xchk_btree *bs,
464 const union xfs_btree_rec *rec)
466 struct xfs_bmbt_irec irec;
467 struct xfs_bmbt_irec iext_irec;
468 struct xfs_iext_cursor icur;
469 struct xchk_bmap_info *info = bs->private;
470 struct xfs_inode *ip = bs->cur->bc_ino.ip;
471 struct xfs_buf *bp = NULL;
472 struct xfs_btree_block *block;
473 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, info->whichfork);
478 * Check the owners of the btree blocks up to the level below
479 * the root since the verifiers don't do that.
481 if (xfs_has_crc(bs->cur->bc_mp) &&
482 bs->cur->bc_levels[0].ptr == 1) {
483 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
484 block = xfs_btree_get_block(bs->cur, i, &bp);
485 owner = be64_to_cpu(block->bb_u.l.bb_owner);
486 if (owner != ip->i_ino)
487 xchk_fblock_set_corrupt(bs->sc,
493 * Check that the incore extent tree contains an extent that matches
494 * this one exactly. We validate those cached bmaps later, so we don't
495 * need to check them here. If the incore extent tree was just loaded
496 * from disk by the scrubber, we assume that its contents match what's
497 * on disk (we still hold the ILOCK) and skip the equivalence check.
499 if (!info->was_loaded)
502 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
503 if (xfs_bmap_validate_extent(ip, info->whichfork, &irec) != NULL) {
504 xchk_fblock_set_corrupt(bs->sc, info->whichfork,
509 if (!xfs_iext_lookup_extent(ip, ifp, irec.br_startoff, &icur,
511 irec.br_startoff != iext_irec.br_startoff ||
512 irec.br_startblock != iext_irec.br_startblock ||
513 irec.br_blockcount != iext_irec.br_blockcount ||
514 irec.br_state != iext_irec.br_state)
515 xchk_fblock_set_corrupt(bs->sc, info->whichfork,
520 /* Scan the btree records. */
523 struct xfs_scrub *sc,
525 struct xchk_bmap_info *info)
527 struct xfs_owner_info oinfo;
528 struct xfs_ifork *ifp = xfs_ifork_ptr(sc->ip, whichfork);
529 struct xfs_mount *mp = sc->mp;
530 struct xfs_inode *ip = sc->ip;
531 struct xfs_btree_cur *cur;
534 /* Load the incore bmap cache if it's not loaded. */
535 info->was_loaded = !xfs_need_iread_extents(ifp);
537 error = xfs_iread_extents(sc->tp, ip, whichfork);
538 if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
541 /* Check the btree structure. */
542 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
543 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
544 error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info);
545 xfs_btree_del_cursor(cur, error);
550 struct xchk_bmap_check_rmap_info {
551 struct xfs_scrub *sc;
553 struct xfs_iext_cursor icur;
556 /* Can we find bmaps that fit this rmap? */
558 xchk_bmap_check_rmap(
559 struct xfs_btree_cur *cur,
560 const struct xfs_rmap_irec *rec,
563 struct xfs_bmbt_irec irec;
564 struct xfs_rmap_irec check_rec;
565 struct xchk_bmap_check_rmap_info *sbcri = priv;
566 struct xfs_ifork *ifp;
567 struct xfs_scrub *sc = sbcri->sc;
570 /* Is this even the right fork? */
571 if (rec->rm_owner != sc->ip->i_ino)
573 if ((sbcri->whichfork == XFS_ATTR_FORK) ^
574 !!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
576 if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
579 /* Now look up the bmbt record. */
580 ifp = xfs_ifork_ptr(sc->ip, sbcri->whichfork);
582 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
586 have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
587 &sbcri->icur, &irec);
589 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
592 * bmap extent record lengths are constrained to 2^21 blocks in length
593 * because of space constraints in the on-disk metadata structure.
594 * However, rmap extent record lengths are constrained only by AG
595 * length, so we have to loop through the bmbt to make sure that the
596 * entire rmap is covered by bmbt records.
600 if (irec.br_startoff != check_rec.rm_offset)
601 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
602 check_rec.rm_offset);
603 if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
604 cur->bc_ag.pag->pag_agno,
605 check_rec.rm_startblock))
606 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
607 check_rec.rm_offset);
608 if (irec.br_blockcount > check_rec.rm_blockcount)
609 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
610 check_rec.rm_offset);
611 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
613 check_rec.rm_startblock += irec.br_blockcount;
614 check_rec.rm_offset += irec.br_blockcount;
615 check_rec.rm_blockcount -= irec.br_blockcount;
616 if (check_rec.rm_blockcount == 0)
618 have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
620 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
621 check_rec.rm_offset);
625 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
630 /* Make sure each rmap has a corresponding bmbt entry. */
632 xchk_bmap_check_ag_rmaps(
633 struct xfs_scrub *sc,
635 struct xfs_perag *pag)
637 struct xchk_bmap_check_rmap_info sbcri;
638 struct xfs_btree_cur *cur;
642 error = xfs_alloc_read_agf(pag, sc->tp, 0, &agf);
646 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, pag);
649 sbcri.whichfork = whichfork;
650 error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri);
651 if (error == -ECANCELED)
654 xfs_btree_del_cursor(cur, error);
655 xfs_trans_brelse(sc->tp, agf);
660 * Decide if we want to scan the reverse mappings to determine if the attr
661 * fork /really/ has zero space mappings.
664 xchk_bmap_check_empty_attrfork(
665 struct xfs_inode *ip)
667 struct xfs_ifork *ifp = &ip->i_af;
670 * If the dinode repair found a bad attr fork, it will reset the fork
671 * to extents format with zero records and wait for the this scrubber
672 * to reconstruct the block mappings. If the fork is not in this
673 * state, then the fork cannot have been zapped.
675 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS || ifp->if_nextents != 0)
679 * Files can have an attr fork in EXTENTS format with zero records for
682 * a) an attr set created a fork but ran out of space
683 * b) attr replace deleted an old attr but failed during the set step
684 * c) the data fork was in btree format when all attrs were deleted, so
685 * the fork was left in place
686 * d) the inode repair code zapped the fork
688 * Only in case (d) do we want to scan the rmapbt to see if we need to
689 * rebuild the attr fork. The fork zap code clears all DAC permission
690 * bits and zeroes the uid and gid, so avoid the scan if any of those
691 * three conditions are not met.
693 if ((VFS_I(ip)->i_mode & 0777) != 0)
695 if (!uid_eq(VFS_I(ip)->i_uid, GLOBAL_ROOT_UID))
697 if (!gid_eq(VFS_I(ip)->i_gid, GLOBAL_ROOT_GID))
704 * Decide if we want to scan the reverse mappings to determine if the data
705 * fork /really/ has zero space mappings.
708 xchk_bmap_check_empty_datafork(
709 struct xfs_inode *ip)
711 struct xfs_ifork *ifp = &ip->i_df;
713 /* Don't support realtime rmap checks yet. */
714 if (XFS_IS_REALTIME_INODE(ip))
718 * If the dinode repair found a bad data fork, it will reset the fork
719 * to extents format with zero records and wait for the this scrubber
720 * to reconstruct the block mappings. If the fork is not in this
721 * state, then the fork cannot have been zapped.
723 if (ifp->if_format != XFS_DINODE_FMT_EXTENTS || ifp->if_nextents != 0)
727 * If we encounter an empty data fork along with evidence that the fork
728 * might not really be empty, we need to scan the reverse mappings to
729 * decide if we're going to rebuild the fork. Data forks with nonzero
730 * file size are scanned.
732 return i_size_read(VFS_I(ip)) != 0;
736 * Decide if we want to walk every rmap btree in the fs to make sure that each
737 * rmap for this file fork has corresponding bmbt entries.
740 xchk_bmap_want_check_rmaps(
741 struct xchk_bmap_info *info)
743 struct xfs_scrub *sc = info->sc;
745 if (!xfs_has_rmapbt(sc->mp))
747 if (info->whichfork == XFS_COW_FORK)
749 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
752 if (info->whichfork == XFS_ATTR_FORK)
753 return xchk_bmap_check_empty_attrfork(sc->ip);
755 return xchk_bmap_check_empty_datafork(sc->ip);
758 /* Make sure each rmap has a corresponding bmbt entry. */
760 xchk_bmap_check_rmaps(
761 struct xfs_scrub *sc,
764 struct xfs_perag *pag;
768 for_each_perag(sc->mp, agno, pag) {
769 error = xchk_bmap_check_ag_rmaps(sc, whichfork, pag);
771 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) {
780 /* Scrub a delalloc reservation from the incore extent map tree. */
782 xchk_bmap_iextent_delalloc(
783 struct xfs_inode *ip,
784 struct xchk_bmap_info *info,
785 struct xfs_bmbt_irec *irec)
787 struct xfs_mount *mp = info->sc->mp;
790 * Check for out-of-order extents. This record could have come
791 * from the incore list, for which there is no ordering check.
793 if (irec->br_startoff < info->prev_rec.br_startoff +
794 info->prev_rec.br_blockcount)
795 xchk_fblock_set_corrupt(info->sc, info->whichfork,
798 if (!xfs_verify_fileext(mp, irec->br_startoff, irec->br_blockcount))
799 xchk_fblock_set_corrupt(info->sc, info->whichfork,
802 /* Make sure the extent points to a valid place. */
803 if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN)
804 xchk_fblock_set_corrupt(info->sc, info->whichfork,
808 /* Decide if this individual fork mapping is ok. */
810 xchk_bmap_iext_mapping(
811 struct xchk_bmap_info *info,
812 const struct xfs_bmbt_irec *irec)
814 /* There should never be a "hole" extent in either extent list. */
815 if (irec->br_startblock == HOLESTARTBLOCK)
817 if (irec->br_blockcount > XFS_MAX_BMBT_EXTLEN)
822 /* Are these two mappings contiguous with each other? */
824 xchk_are_bmaps_contiguous(
825 const struct xfs_bmbt_irec *b1,
826 const struct xfs_bmbt_irec *b2)
828 /* Don't try to combine unallocated mappings. */
829 if (!xfs_bmap_is_real_extent(b1))
831 if (!xfs_bmap_is_real_extent(b2))
834 /* Does b2 come right after b1 in the logical and physical range? */
835 if (b1->br_startoff + b1->br_blockcount != b2->br_startoff)
837 if (b1->br_startblock + b1->br_blockcount != b2->br_startblock)
839 if (b1->br_state != b2->br_state)
845 * Walk the incore extent records, accumulating consecutive contiguous records
846 * into a single incore mapping. Returns true if @irec has been set to a
847 * mapping or false if there are no more mappings. Caller must ensure that
848 * @info.icur is zeroed before the first call.
852 struct xchk_bmap_info *info,
853 struct xfs_bmbt_irec *irec)
855 struct xfs_bmbt_irec got;
856 struct xfs_ifork *ifp;
859 ifp = xfs_ifork_ptr(info->sc->ip, info->whichfork);
861 /* Advance to the next iextent record and check the mapping. */
862 xfs_iext_next(ifp, &info->icur);
863 if (!xfs_iext_get_extent(ifp, &info->icur, irec))
866 if (!xchk_bmap_iext_mapping(info, irec)) {
867 xchk_fblock_set_corrupt(info->sc, info->whichfork,
874 * Iterate subsequent iextent records and merge them with the one
875 * that we just read, if possible.
877 while (xfs_iext_peek_next_extent(ifp, &info->icur, &got)) {
878 if (!xchk_are_bmaps_contiguous(irec, &got))
881 if (!xchk_bmap_iext_mapping(info, &got)) {
882 xchk_fblock_set_corrupt(info->sc, info->whichfork,
888 irec->br_blockcount += got.br_blockcount;
889 xfs_iext_next(ifp, &info->icur);
893 * If the merged mapping could be expressed with fewer bmbt records
894 * than we actually found, notify the user that this fork could be
895 * optimized. CoW forks only exist in memory so we ignore them.
897 if (nr > 1 && info->whichfork != XFS_COW_FORK &&
898 howmany_64(irec->br_blockcount, XFS_MAX_BMBT_EXTLEN) < nr)
899 xchk_ino_set_preen(info->sc, info->sc->ip->i_ino);
905 * Scrub an inode fork's block mappings.
907 * First we scan every record in every btree block, if applicable.
908 * Then we unconditionally scan the incore extent cache.
912 struct xfs_scrub *sc,
915 struct xfs_bmbt_irec irec;
916 struct xchk_bmap_info info = { NULL };
917 struct xfs_mount *mp = sc->mp;
918 struct xfs_inode *ip = sc->ip;
919 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
920 xfs_fileoff_t endoff;
923 /* Non-existent forks can be ignored. */
927 info.is_rt = xfs_ifork_is_realtime(ip, whichfork);
928 info.whichfork = whichfork;
929 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
934 /* No CoW forks on non-reflink filesystems. */
935 if (!xfs_has_reflink(mp)) {
936 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
941 if (!xfs_has_attr(mp) && !xfs_has_attr2(mp))
942 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
945 ASSERT(whichfork == XFS_DATA_FORK);
949 /* Check the fork values */
950 switch (ifp->if_format) {
951 case XFS_DINODE_FMT_UUID:
952 case XFS_DINODE_FMT_DEV:
953 case XFS_DINODE_FMT_LOCAL:
954 /* No mappings to check. */
955 if (whichfork == XFS_COW_FORK)
956 xchk_fblock_set_corrupt(sc, whichfork, 0);
958 case XFS_DINODE_FMT_EXTENTS:
960 case XFS_DINODE_FMT_BTREE:
961 if (whichfork == XFS_COW_FORK) {
962 xchk_fblock_set_corrupt(sc, whichfork, 0);
966 error = xchk_bmap_btree(sc, whichfork, &info);
971 xchk_fblock_set_corrupt(sc, whichfork, 0);
975 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
978 /* Find the offset of the last extent in the mapping. */
979 error = xfs_bmap_last_offset(ip, &endoff, whichfork);
980 if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
984 * Scrub extent records. We use a special iterator function here that
985 * combines adjacent mappings if they are logically and physically
986 * contiguous. For large allocations that require multiple bmbt
987 * records, this reduces the number of cross-referencing calls, which
988 * reduces runtime. Cross referencing with the rmap is simpler because
989 * the rmap must match the combined mapping exactly.
991 while (xchk_bmap_iext_iter(&info, &irec)) {
992 if (xchk_should_terminate(sc, &error) ||
993 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
996 if (irec.br_startoff >= endoff) {
997 xchk_fblock_set_corrupt(sc, whichfork,
1002 if (isnullstartblock(irec.br_startblock))
1003 xchk_bmap_iextent_delalloc(ip, &info, &irec);
1005 xchk_bmap_iextent(ip, &info, &irec);
1006 memcpy(&info.prev_rec, &irec, sizeof(struct xfs_bmbt_irec));
1009 if (xchk_bmap_want_check_rmaps(&info)) {
1010 error = xchk_bmap_check_rmaps(sc, whichfork);
1011 if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error))
1018 /* Scrub an inode's data fork. */
1021 struct xfs_scrub *sc)
1025 if (xchk_file_looks_zapped(sc, XFS_SICK_INO_BMBTD_ZAPPED)) {
1026 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1030 error = xchk_bmap(sc, XFS_DATA_FORK);
1034 /* If the data fork is clean, it is clearly not zapped. */
1035 xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTD_ZAPPED);
1039 /* Scrub an inode's attr fork. */
1042 struct xfs_scrub *sc)
1047 * If the attr fork has been zapped, it's possible that forkoff was
1048 * reset to zero and hence sc->ip->i_afp is NULL. We don't want the
1049 * NULL ifp check in xchk_bmap to conclude that the attr fork is ok,
1050 * so short circuit that logic by setting the corruption flag and
1051 * returning immediately.
1053 if (xchk_file_looks_zapped(sc, XFS_SICK_INO_BMBTA_ZAPPED)) {
1054 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
1058 error = xchk_bmap(sc, XFS_ATTR_FORK);
1062 /* If the attr fork is clean, it is clearly not zapped. */
1063 xchk_mark_healthy_if_clean(sc, XFS_SICK_INO_BMBTA_ZAPPED);
1067 /* Scrub an inode's CoW fork. */
1070 struct xfs_scrub *sc)
1072 return xchk_bmap(sc, XFS_COW_FORK);