1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 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_defer.h"
13 #include "xfs_btree.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
18 #include "xfs_inode.h"
19 #include "xfs_inode_fork.h"
20 #include "xfs_alloc.h"
21 #include "xfs_rtalloc.h"
23 #include "xfs_bmap_util.h"
24 #include "xfs_bmap_btree.h"
26 #include "xfs_rmap_btree.h"
27 #include "xfs_refcount.h"
28 #include "scrub/xfs_scrub.h"
29 #include "scrub/scrub.h"
30 #include "scrub/common.h"
31 #include "scrub/btree.h"
32 #include "scrub/trace.h"
34 /* Set us up with an inode's bmap. */
36 xchk_setup_inode_bmap(
42 error = xchk_get_inode(sc, ip);
46 sc->ilock_flags = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
47 xfs_ilock(sc->ip, sc->ilock_flags);
50 * We don't want any ephemeral data fork updates sitting around
51 * while we inspect block mappings, so wait for directio to finish
52 * and flush dirty data if we have delalloc reservations.
54 if (S_ISREG(VFS_I(sc->ip)->i_mode) &&
55 sc->sm->sm_type == XFS_SCRUB_TYPE_BMBTD) {
56 inode_dio_wait(VFS_I(sc->ip));
57 error = filemap_write_and_wait(VFS_I(sc->ip)->i_mapping);
62 /* Got the inode, lock it and we're ready to go. */
63 error = xchk_trans_alloc(sc, 0);
66 sc->ilock_flags |= XFS_ILOCK_EXCL;
67 xfs_ilock(sc->ip, XFS_ILOCK_EXCL);
70 /* scrub teardown will unlock and release the inode */
75 * Inode fork block mapping (BMBT) scrubber.
76 * More complex than the others because we have to scrub
77 * all the extents regardless of whether or not the fork
81 struct xchk_bmap_info {
83 xfs_fileoff_t lastoff;
89 /* Look for a corresponding rmap for this irec. */
92 struct xchk_bmap_info *info,
93 struct xfs_bmbt_irec *irec,
96 struct xfs_rmap_irec *rmap)
99 unsigned int rflags = 0;
103 if (info->whichfork == XFS_ATTR_FORK)
104 rflags |= XFS_RMAP_ATTR_FORK;
107 * CoW staging extents are owned (on disk) by the refcountbt, so
108 * their rmaps do not have offsets.
110 if (info->whichfork == XFS_COW_FORK)
113 offset = irec->br_startoff;
116 * If the caller thinks this could be a shared bmbt extent (IOWs,
117 * any data fork extent of a reflink inode) then we have to use the
118 * range rmap lookup to make sure we get the correct owner/offset.
120 if (info->is_shared) {
121 error = xfs_rmap_lookup_le_range(info->sc->sa.rmap_cur, agbno,
122 owner, offset, rflags, rmap, &has_rmap);
123 if (!xchk_should_check_xref(info->sc, &error,
124 &info->sc->sa.rmap_cur))
130 * Otherwise, use the (faster) regular lookup.
132 error = xfs_rmap_lookup_le(info->sc->sa.rmap_cur, agbno, 0, owner,
133 offset, rflags, &has_rmap);
134 if (!xchk_should_check_xref(info->sc, &error,
135 &info->sc->sa.rmap_cur))
140 error = xfs_rmap_get_rec(info->sc->sa.rmap_cur, rmap, &has_rmap);
141 if (!xchk_should_check_xref(info->sc, &error,
142 &info->sc->sa.rmap_cur))
147 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
152 /* Make sure that we have rmapbt records for this extent. */
155 struct xchk_bmap_info *info,
156 struct xfs_bmbt_irec *irec,
159 struct xfs_rmap_irec rmap;
160 unsigned long long rmap_end;
163 if (!info->sc->sa.rmap_cur || xchk_skip_xref(info->sc->sm))
166 if (info->whichfork == XFS_COW_FORK)
167 owner = XFS_RMAP_OWN_COW;
169 owner = info->sc->ip->i_ino;
171 /* Find the rmap record for this irec. */
172 if (!xchk_bmap_get_rmap(info, irec, agbno, owner, &rmap))
175 /* Check the rmap. */
176 rmap_end = (unsigned long long)rmap.rm_startblock + rmap.rm_blockcount;
177 if (rmap.rm_startblock > agbno ||
178 agbno + irec->br_blockcount > rmap_end)
179 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
183 * Check the logical offsets if applicable. CoW staging extents
184 * don't track logical offsets since the mappings only exist in
187 if (info->whichfork != XFS_COW_FORK) {
188 rmap_end = (unsigned long long)rmap.rm_offset +
190 if (rmap.rm_offset > irec->br_startoff ||
191 irec->br_startoff + irec->br_blockcount > rmap_end)
192 xchk_fblock_xref_set_corrupt(info->sc,
193 info->whichfork, irec->br_startoff);
196 if (rmap.rm_owner != owner)
197 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
201 * Check for discrepancies between the unwritten flag in the irec and
202 * the rmap. Note that the (in-memory) CoW fork distinguishes between
203 * unwritten and written extents, but we don't track that in the rmap
204 * records because the blocks are owned (on-disk) by the refcountbt,
205 * which doesn't track unwritten state.
207 if (owner != XFS_RMAP_OWN_COW &&
208 irec->br_state == XFS_EXT_UNWRITTEN &&
209 !(rmap.rm_flags & XFS_RMAP_UNWRITTEN))
210 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
213 if (info->whichfork == XFS_ATTR_FORK &&
214 !(rmap.rm_flags & XFS_RMAP_ATTR_FORK))
215 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
217 if (rmap.rm_flags & XFS_RMAP_BMBT_BLOCK)
218 xchk_fblock_xref_set_corrupt(info->sc, info->whichfork,
222 /* Cross-reference a single rtdev extent record. */
224 xchk_bmap_rt_extent_xref(
225 struct xchk_bmap_info *info,
226 struct xfs_inode *ip,
227 struct xfs_btree_cur *cur,
228 struct xfs_bmbt_irec *irec)
230 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
233 xchk_xref_is_used_rt_space(info->sc, irec->br_startblock,
234 irec->br_blockcount);
237 /* Cross-reference a single datadev extent record. */
239 xchk_bmap_extent_xref(
240 struct xchk_bmap_info *info,
241 struct xfs_inode *ip,
242 struct xfs_btree_cur *cur,
243 struct xfs_bmbt_irec *irec)
245 struct xfs_mount *mp = info->sc->mp;
251 if (info->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
254 agno = XFS_FSB_TO_AGNO(mp, irec->br_startblock);
255 agbno = XFS_FSB_TO_AGBNO(mp, irec->br_startblock);
256 len = irec->br_blockcount;
258 error = xchk_ag_init(info->sc, agno, &info->sc->sa);
259 if (!xchk_fblock_process_error(info->sc, info->whichfork,
260 irec->br_startoff, &error))
263 xchk_xref_is_used_space(info->sc, agbno, len);
264 xchk_xref_is_not_inode_chunk(info->sc, agbno, len);
265 xchk_bmap_xref_rmap(info, irec, agbno);
266 switch (info->whichfork) {
268 if (xfs_is_reflink_inode(info->sc->ip))
272 xchk_xref_is_not_shared(info->sc, agbno,
273 irec->br_blockcount);
276 xchk_xref_is_cow_staging(info->sc, agbno,
277 irec->br_blockcount);
281 xchk_ag_free(info->sc, &info->sc->sa);
284 /* Scrub a single extent record. */
287 struct xfs_inode *ip,
288 struct xfs_btree_cur *cur,
289 struct xchk_bmap_info *info,
290 struct xfs_bmbt_irec *irec)
292 struct xfs_mount *mp = info->sc->mp;
293 struct xfs_buf *bp = NULL;
298 xfs_btree_get_block(cur, 0, &bp);
301 * Check for out-of-order extents. This record could have come
302 * from the incore list, for which there is no ordering check.
304 if (irec->br_startoff < info->lastoff)
305 xchk_fblock_set_corrupt(info->sc, info->whichfork,
308 /* There should never be a "hole" extent in either extent list. */
309 if (irec->br_startblock == HOLESTARTBLOCK)
310 xchk_fblock_set_corrupt(info->sc, info->whichfork,
314 * Check for delalloc extents. We never iterate the ones in the
315 * in-core extent scan, and we should never see these in the bmbt.
317 if (isnullstartblock(irec->br_startblock))
318 xchk_fblock_set_corrupt(info->sc, info->whichfork,
321 /* Make sure the extent points to a valid place. */
322 if (irec->br_blockcount > MAXEXTLEN)
323 xchk_fblock_set_corrupt(info->sc, info->whichfork,
325 if (irec->br_startblock + irec->br_blockcount <= irec->br_startblock)
326 xchk_fblock_set_corrupt(info->sc, info->whichfork,
328 end = irec->br_startblock + irec->br_blockcount - 1;
330 (!xfs_verify_rtbno(mp, irec->br_startblock) ||
331 !xfs_verify_rtbno(mp, end)))
332 xchk_fblock_set_corrupt(info->sc, info->whichfork,
335 (!xfs_verify_fsbno(mp, irec->br_startblock) ||
336 !xfs_verify_fsbno(mp, end) ||
337 XFS_FSB_TO_AGNO(mp, irec->br_startblock) !=
338 XFS_FSB_TO_AGNO(mp, end)))
339 xchk_fblock_set_corrupt(info->sc, info->whichfork,
342 /* We don't allow unwritten extents on attr forks. */
343 if (irec->br_state == XFS_EXT_UNWRITTEN &&
344 info->whichfork == XFS_ATTR_FORK)
345 xchk_fblock_set_corrupt(info->sc, info->whichfork,
349 xchk_bmap_rt_extent_xref(info, ip, cur, irec);
351 xchk_bmap_extent_xref(info, ip, cur, irec);
353 info->lastoff = irec->br_startoff + irec->br_blockcount;
357 /* Scrub a bmbt record. */
360 struct xchk_btree *bs,
361 union xfs_btree_rec *rec)
363 struct xfs_bmbt_irec irec;
364 struct xchk_bmap_info *info = bs->private;
365 struct xfs_inode *ip = bs->cur->bc_private.b.ip;
366 struct xfs_buf *bp = NULL;
367 struct xfs_btree_block *block;
372 * Check the owners of the btree blocks up to the level below
373 * the root since the verifiers don't do that.
375 if (xfs_sb_version_hascrc(&bs->cur->bc_mp->m_sb) &&
376 bs->cur->bc_ptrs[0] == 1) {
377 for (i = 0; i < bs->cur->bc_nlevels - 1; i++) {
378 block = xfs_btree_get_block(bs->cur, i, &bp);
379 owner = be64_to_cpu(block->bb_u.l.bb_owner);
380 if (owner != ip->i_ino)
381 xchk_fblock_set_corrupt(bs->sc,
386 /* Set up the in-core record and scrub it. */
387 xfs_bmbt_disk_get_all(&rec->bmbt, &irec);
388 return xchk_bmap_extent(ip, bs->cur, info, &irec);
391 /* Scan the btree records. */
394 struct xfs_scrub *sc,
396 struct xchk_bmap_info *info)
398 struct xfs_owner_info oinfo;
399 struct xfs_mount *mp = sc->mp;
400 struct xfs_inode *ip = sc->ip;
401 struct xfs_btree_cur *cur;
404 cur = xfs_bmbt_init_cursor(mp, sc->tp, ip, whichfork);
405 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
406 error = xchk_btree(sc, cur, xchk_bmapbt_rec, &oinfo, info);
407 xfs_btree_del_cursor(cur, error);
411 struct xchk_bmap_check_rmap_info {
412 struct xfs_scrub *sc;
414 struct xfs_iext_cursor icur;
417 /* Can we find bmaps that fit this rmap? */
419 xchk_bmap_check_rmap(
420 struct xfs_btree_cur *cur,
421 struct xfs_rmap_irec *rec,
424 struct xfs_bmbt_irec irec;
425 struct xchk_bmap_check_rmap_info *sbcri = priv;
426 struct xfs_ifork *ifp;
427 struct xfs_scrub *sc = sbcri->sc;
430 /* Is this even the right fork? */
431 if (rec->rm_owner != sc->ip->i_ino)
433 if ((sbcri->whichfork == XFS_ATTR_FORK) ^
434 !!(rec->rm_flags & XFS_RMAP_ATTR_FORK))
436 if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
439 /* Now look up the bmbt record. */
440 ifp = XFS_IFORK_PTR(sc->ip, sbcri->whichfork);
442 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
446 have_map = xfs_iext_lookup_extent(sc->ip, ifp, rec->rm_offset,
447 &sbcri->icur, &irec);
449 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
452 * bmap extent record lengths are constrained to 2^21 blocks in length
453 * because of space constraints in the on-disk metadata structure.
454 * However, rmap extent record lengths are constrained only by AG
455 * length, so we have to loop through the bmbt to make sure that the
456 * entire rmap is covered by bmbt records.
459 if (irec.br_startoff != rec->rm_offset)
460 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
462 if (irec.br_startblock != XFS_AGB_TO_FSB(sc->mp,
463 cur->bc_private.a.agno, rec->rm_startblock))
464 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
466 if (irec.br_blockcount > rec->rm_blockcount)
467 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
469 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
471 rec->rm_startblock += irec.br_blockcount;
472 rec->rm_offset += irec.br_blockcount;
473 rec->rm_blockcount -= irec.br_blockcount;
474 if (rec->rm_blockcount == 0)
476 have_map = xfs_iext_next_extent(ifp, &sbcri->icur, &irec);
478 xchk_fblock_set_corrupt(sc, sbcri->whichfork,
483 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
484 return XFS_BTREE_QUERY_RANGE_ABORT;
488 /* Make sure each rmap has a corresponding bmbt entry. */
490 xchk_bmap_check_ag_rmaps(
491 struct xfs_scrub *sc,
495 struct xchk_bmap_check_rmap_info sbcri;
496 struct xfs_btree_cur *cur;
500 error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf);
504 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf, agno);
511 sbcri.whichfork = whichfork;
512 error = xfs_rmap_query_all(cur, xchk_bmap_check_rmap, &sbcri);
513 if (error == XFS_BTREE_QUERY_RANGE_ABORT)
516 xfs_btree_del_cursor(cur, error);
518 xfs_trans_brelse(sc->tp, agf);
522 /* Make sure each rmap has a corresponding bmbt entry. */
524 xchk_bmap_check_rmaps(
525 struct xfs_scrub *sc,
532 if (!xfs_sb_version_hasrmapbt(&sc->mp->m_sb) ||
533 whichfork == XFS_COW_FORK ||
534 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
537 /* Don't support realtime rmap checks yet. */
538 if (XFS_IS_REALTIME_INODE(sc->ip) && whichfork == XFS_DATA_FORK)
542 * Only do this for complex maps that are in btree format, or for
543 * situations where we would seem to have a size but zero extents.
544 * The inode repair code can zap broken iforks, which means we have
545 * to flag this bmap as corrupt if there are rmaps that need to be
550 size = i_size_read(VFS_I(sc->ip));
553 size = XFS_IFORK_Q(sc->ip);
559 if (XFS_IFORK_FORMAT(sc->ip, whichfork) != XFS_DINODE_FMT_BTREE &&
560 (size == 0 || XFS_IFORK_NEXTENTS(sc->ip, whichfork) > 0))
563 for (agno = 0; agno < sc->mp->m_sb.sb_agcount; agno++) {
564 error = xchk_bmap_check_ag_rmaps(sc, whichfork, agno);
567 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
575 * Scrub an inode fork's block mappings.
577 * First we scan every record in every btree block, if applicable.
578 * Then we unconditionally scan the incore extent cache.
582 struct xfs_scrub *sc,
585 struct xfs_bmbt_irec irec;
586 struct xchk_bmap_info info = { NULL };
587 struct xfs_mount *mp = sc->mp;
588 struct xfs_inode *ip = sc->ip;
589 struct xfs_ifork *ifp;
590 xfs_fileoff_t endoff;
591 struct xfs_iext_cursor icur;
594 ifp = XFS_IFORK_PTR(ip, whichfork);
596 info.is_rt = whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip);
597 info.whichfork = whichfork;
598 info.is_shared = whichfork == XFS_DATA_FORK && xfs_is_reflink_inode(ip);
603 /* Non-existent CoW forks are ignorable. */
606 /* No CoW forks on non-reflink inodes/filesystems. */
607 if (!xfs_is_reflink_inode(ip)) {
608 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
615 if (!xfs_sb_version_hasattr(&mp->m_sb) &&
616 !xfs_sb_version_hasattr2(&mp->m_sb))
617 xchk_ino_set_corrupt(sc, sc->ip->i_ino);
620 ASSERT(whichfork == XFS_DATA_FORK);
624 /* Check the fork values */
625 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
626 case XFS_DINODE_FMT_UUID:
627 case XFS_DINODE_FMT_DEV:
628 case XFS_DINODE_FMT_LOCAL:
629 /* No mappings to check. */
631 case XFS_DINODE_FMT_EXTENTS:
632 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
633 xchk_fblock_set_corrupt(sc, whichfork, 0);
637 case XFS_DINODE_FMT_BTREE:
638 if (whichfork == XFS_COW_FORK) {
639 xchk_fblock_set_corrupt(sc, whichfork, 0);
643 error = xchk_bmap_btree(sc, whichfork, &info);
648 xchk_fblock_set_corrupt(sc, whichfork, 0);
652 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
655 /* Now try to scrub the in-memory extent list. */
656 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
657 error = xfs_iread_extents(sc->tp, ip, whichfork);
658 if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
662 /* Find the offset of the last extent in the mapping. */
663 error = xfs_bmap_last_offset(ip, &endoff, whichfork);
664 if (!xchk_fblock_process_error(sc, whichfork, 0, &error))
667 /* Scrub extent records. */
669 ifp = XFS_IFORK_PTR(ip, whichfork);
670 for_each_xfs_iext(ifp, &icur, &irec) {
671 if (xchk_should_terminate(sc, &error) ||
672 (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
674 if (isnullstartblock(irec.br_startblock))
676 if (irec.br_startoff >= endoff) {
677 xchk_fblock_set_corrupt(sc, whichfork,
681 error = xchk_bmap_extent(ip, NULL, &info, &irec);
687 error = xchk_bmap_check_rmaps(sc, whichfork);
688 if (!xchk_fblock_xref_process_error(sc, whichfork, 0, &error))
694 /* Scrub an inode's data fork. */
697 struct xfs_scrub *sc)
699 return xchk_bmap(sc, XFS_DATA_FORK);
702 /* Scrub an inode's attr fork. */
705 struct xfs_scrub *sc)
707 return xchk_bmap(sc, XFS_ATTR_FORK);
710 /* Scrub an inode's CoW fork. */
713 struct xfs_scrub *sc)
715 if (!xfs_is_reflink_inode(sc->ip))
718 return xchk_bmap(sc, XFS_COW_FORK);