]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/scan.c
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "err_protos.h"
33 extern int verify_set_agheader(xfs_mount_t
*mp
, xfs_buf_t
*sbuf
, xfs_sb_t
*sb
,
34 xfs_agf_t
*agf
, xfs_agi_t
*agi
, xfs_agnumber_t i
);
36 static xfs_mount_t
*mp
= NULL
;
39 * Variables to validate AG header values against the manual count
40 * from the btree traversal.
44 xfs_extlen_t agffreeblks
;
45 xfs_extlen_t agflongest
;
46 __uint64_t agfbtreeblks
;
48 __uint32_t agifreecount
;
51 __uint64_t ifreecount
;
56 struct xfs_btree_block
*block
,
63 struct aghdr_cnts
*agcnts
);
66 set_mp(xfs_mount_t
*mpp
)
68 libxfs_bcache_purge();
78 void (*func
)(struct xfs_btree_block
*block
,
90 bp
= libxfs_readbuf(mp
->m_dev
, XFS_AGB_TO_DADDR(mp
, agno
, root
),
91 XFS_FSB_TO_BB(mp
, 1), 0);
93 do_error(_("can't read btree block %d/%d\n"), agno
, root
);
96 (*func
)(XFS_BUF_TO_BLOCK(bp
), nlevels
- 1, root
, agno
, suspect
,
102 * returns 1 on bad news (inode needs to be cleared), 0 on good
108 int (*func
)(struct xfs_btree_block
*block
,
117 bmap_cursor_t
*bm_cursor
,
127 bmap_cursor_t
*bm_cursor
,
135 bp
= libxfs_readbuf(mp
->m_dev
, XFS_FSB_TO_DADDR(mp
, root
),
136 XFS_FSB_TO_BB(mp
, 1), 0);
138 do_error(_("can't read btree block %d/%d\n"),
139 XFS_FSB_TO_AGNO(mp
, root
),
140 XFS_FSB_TO_AGBNO(mp
, root
));
143 err
= (*func
)(XFS_BUF_TO_BLOCK(bp
), nlevels
- 1,
144 type
, whichfork
, root
, ino
, tot
, nex
, blkmapp
,
145 bm_cursor
, isroot
, check_dups
, &dirty
);
147 ASSERT(dirty
== 0 || (dirty
&& !no_modify
));
149 if (dirty
&& !no_modify
)
150 libxfs_writebuf(bp
, 0);
159 struct xfs_btree_block
*block
,
168 bmap_cursor_t
*bm_cursor
,
176 xfs_bmbt_key_t
*pkey
;
178 xfs_dfiloff_t first_key
;
179 xfs_dfiloff_t last_key
;
186 if (whichfork
== XFS_DATA_FORK
)
187 forkname
= _("data");
189 forkname
= _("attr");
192 * unlike the ag freeblock btrees, if anything looks wrong
193 * in an inode bmap tree, just bail. it's possible that
194 * we'll miss a case where the to-be-toasted inode and
195 * another inode are claiming the same block but that's
198 if (be32_to_cpu(block
->bb_magic
) != XFS_BMAP_MAGIC
) {
200 _("bad magic # %#x in inode %" PRIu64
" (%s fork) bmbt block %" PRIu64
"\n"),
201 be32_to_cpu(block
->bb_magic
), ino
, forkname
, bno
);
204 if (be16_to_cpu(block
->bb_level
) != level
) {
206 _("expected level %d got %d in inode %" PRIu64
", (%s fork) bmbt block %" PRIu64
"\n"),
207 level
, be16_to_cpu(block
->bb_level
),
212 if (check_dups
== 0) {
214 * check sibling pointers. if bad we have a conflict
215 * between the sibling pointers and the child pointers
216 * in the parent block. blow out the inode if that happens
218 if (bm_cursor
->level
[level
].fsbno
!= NULLDFSBNO
) {
220 * this is not the first block on this level
221 * so the cursor for this level has recorded the
222 * values for this's block left-sibling.
224 if (bno
!= bm_cursor
->level
[level
].right_fsbno
) {
226 _("bad fwd (right) sibling pointer (saw %" PRIu64
" parent block says %" PRIu64
")\n"
227 "\tin inode %" PRIu64
" (%s fork) bmap btree block %" PRIu64
"\n"),
228 bm_cursor
->level
[level
].right_fsbno
,
230 bm_cursor
->level
[level
].fsbno
);
233 if (be64_to_cpu(block
->bb_u
.l
.bb_leftsib
) !=
234 bm_cursor
->level
[level
].fsbno
) {
236 _("bad back (left) sibling pointer (saw %llu parent block says %" PRIu64
")\n"
237 "\tin inode %" PRIu64
" (%s fork) bmap btree block %" PRIu64
"\n"),
239 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
),
240 bm_cursor
->level
[level
].fsbno
,
246 * This is the first or only block on this level.
247 * Check that the left sibling pointer is NULL
249 if (be64_to_cpu(block
->bb_u
.l
.bb_leftsib
) != NULLDFSBNO
) {
251 _("bad back (left) sibling pointer (saw %llu should be NULL (0))\n"
252 "\tin inode %" PRIu64
" (%s fork) bmap btree block %" PRIu64
"\n"),
254 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
),
261 * update cursor block pointers to reflect this block
263 bm_cursor
->level
[level
].fsbno
= bno
;
264 bm_cursor
->level
[level
].left_fsbno
=
265 be64_to_cpu(block
->bb_u
.l
.bb_leftsib
);
266 bm_cursor
->level
[level
].right_fsbno
=
267 be64_to_cpu(block
->bb_u
.l
.bb_rightsib
);
269 agno
= XFS_FSB_TO_AGNO(mp
, bno
);
270 agbno
= XFS_FSB_TO_AGBNO(mp
, bno
);
272 pthread_mutex_lock(&ag_locks
[agno
]);
273 state
= get_bmap(agno
, agbno
);
278 set_bmap(agno
, agbno
, XR_E_INUSE
);
283 * we'll try and continue searching here since
284 * the block looks like it's been claimed by file
285 * to store user data, a directory to store directory
286 * data, or the space allocation btrees but since
287 * we made it here, the block probably
288 * contains btree data.
290 set_bmap(agno
, agbno
, XR_E_MULT
);
292 _("inode 0x%" PRIx64
"bmap block 0x%" PRIx64
" claimed, state is %d\n"),
297 set_bmap(agno
, agbno
, XR_E_MULT
);
299 _("inode 0x%" PRIx64
" bmap block 0x%" PRIx64
" claimed, state is %d\n"),
302 * if we made it to here, this is probably a bmap block
303 * that is being used by *another* file as a bmap block
304 * so the block will be valid. Both files should be
305 * trashed along with any other file that impinges on
306 * any blocks referenced by either file. So we
307 * continue searching down this btree to mark all
314 _("bad state %d, inode %" PRIu64
" bmap block 0x%" PRIx64
"\n"),
318 pthread_mutex_unlock(&ag_locks
[agno
]);
321 * attribute fork for realtime files is in the regular
324 if (type
!= XR_INO_RTDATA
|| whichfork
!= XFS_DATA_FORK
) {
325 if (search_dup_extent(XFS_FSB_TO_AGNO(mp
, bno
),
326 XFS_FSB_TO_AGBNO(mp
, bno
),
327 XFS_FSB_TO_AGBNO(mp
, bno
) + 1))
330 if (search_rt_dup_extent(mp
, bno
))
335 numrecs
= be16_to_cpu(block
->bb_numrecs
);
338 if (numrecs
> mp
->m_bmap_dmxr
[0] || (isroot
== 0 && numrecs
<
339 mp
->m_bmap_dmnr
[0])) {
341 _("inode %" PRIu64
" bad # of bmap records (%u, min - %u, max - %u)\n"),
342 ino
, numrecs
, mp
->m_bmap_dmnr
[0],
346 rp
= XFS_BMBT_REC_ADDR(mp
, block
, 1);
349 * XXX - if we were going to fix up the btree record,
350 * we'd do it right here. For now, if there's a problem,
351 * we'll bail out and presumably clear the inode.
353 if (check_dups
== 0) {
354 err
= process_bmbt_reclist(mp
, rp
, numrecs
,
355 type
, ino
, tot
, blkmapp
,
356 &first_key
, &last_key
,
361 * check that key ordering is monotonically increasing.
362 * if the last_key value in the cursor is set to
363 * NULLDFILOFF, then we know this is the first block
364 * on the leaf level and we shouldn't check the
367 if (first_key
<= bm_cursor
->level
[level
].last_key
&&
368 bm_cursor
->level
[level
].last_key
!=
371 _("out-of-order bmap key (file offset) in inode %" PRIu64
", %s fork, fsbno %" PRIu64
"\n"),
376 * update cursor keys to reflect this block.
377 * don't have to check if last_key is > first_key
378 * since that gets checked by process_bmbt_reclist.
380 bm_cursor
->level
[level
].first_key
= first_key
;
381 bm_cursor
->level
[level
].last_key
= last_key
;
385 return(scan_bmbt_reclist(mp
, rp
, numrecs
,
386 type
, ino
, tot
, whichfork
));
388 if (numrecs
> mp
->m_bmap_dmxr
[1] || (isroot
== 0 && numrecs
<
389 mp
->m_bmap_dmnr
[1])) {
391 _("inode %" PRIu64
" bad # of bmap records (%u, min - %u, max - %u)\n"),
392 ino
, numrecs
, mp
->m_bmap_dmnr
[1], mp
->m_bmap_dmxr
[1]);
395 pp
= XFS_BMBT_PTR_ADDR(mp
, block
, 1, mp
->m_bmap_dmxr
[1]);
396 pkey
= XFS_BMBT_KEY_ADDR(mp
, block
, 1);
398 last_key
= NULLDFILOFF
;
400 for (i
= 0, err
= 0; i
< numrecs
; i
++) {
402 * XXX - if we were going to fix up the interior btree nodes,
403 * we'd do it right here. For now, if there's a problem,
404 * we'll bail out and presumably clear the inode.
406 if (!verify_dfsbno(mp
, be64_to_cpu(pp
[i
]))) {
408 _("bad bmap btree ptr 0x%llx in ino %" PRIu64
"\n"),
409 (unsigned long long) be64_to_cpu(pp
[i
]), ino
);
413 err
= scan_lbtree(be64_to_cpu(pp
[i
]), level
, scanfunc_bmap
,
414 type
, whichfork
, ino
, tot
, nex
, blkmapp
,
415 bm_cursor
, 0, check_dups
);
420 * fix key (offset) mismatches between the first key
421 * in the child block (as recorded in the cursor) and the
422 * key in the interior node referencing the child block.
424 * fixes cases where entries have been shifted between
425 * child blocks but the parent hasn't been updated. We
426 * don't have to worry about the key values in the cursor
427 * not being set since we only look at the key values of
428 * our child and those are guaranteed to be set by the
429 * call to scan_lbtree() above.
431 if (check_dups
== 0 && be64_to_cpu(pkey
[i
].br_startoff
) !=
432 bm_cursor
->level
[level
-1].first_key
) {
435 _("correcting bt key (was %llu, now %" PRIu64
") in inode %" PRIu64
"\n"
436 "\t\t%s fork, btree block %" PRIu64
"\n"),
438 be64_to_cpu(pkey
[i
].br_startoff
),
439 bm_cursor
->level
[level
-1].first_key
,
443 pkey
[i
].br_startoff
= cpu_to_be64(
444 bm_cursor
->level
[level
-1].first_key
);
447 _("bad btree key (is %llu, should be %" PRIu64
") in inode %" PRIu64
"\n"
448 "\t\t%s fork, btree block %" PRIu64
"\n"),
450 be64_to_cpu(pkey
[i
].br_startoff
),
451 bm_cursor
->level
[level
-1].first_key
,
458 * If we're the last node at our level, check that the last child
459 * block's forward sibling pointer is NULL.
461 if (check_dups
== 0 &&
462 bm_cursor
->level
[level
].right_fsbno
== NULLDFSBNO
&&
463 bm_cursor
->level
[level
- 1].right_fsbno
!= NULLDFSBNO
) {
465 _("bad fwd (right) sibling pointer (saw %" PRIu64
" should be NULLDFSBNO)\n"
466 "\tin inode %" PRIu64
" (%s fork) bmap btree block %" PRIu64
"\n"),
467 bm_cursor
->level
[level
- 1].right_fsbno
,
468 ino
, forkname
, bm_cursor
->level
[level
- 1].fsbno
);
473 * update cursor keys to reflect this block
475 if (check_dups
== 0) {
476 bm_cursor
->level
[level
].first_key
=
477 be64_to_cpu(pkey
[0].br_startoff
);
478 bm_cursor
->level
[level
].last_key
=
479 be64_to_cpu(pkey
[numrecs
- 1].br_startoff
);
487 struct xfs_btree_block
*block
,
495 return scanfunc_allocbt(block
, level
, bno
, agno
,
496 suspect
, isroot
, XFS_ABTB_MAGIC
, agcnts
);
501 struct xfs_btree_block
*block
,
509 return scanfunc_allocbt(block
, level
, bno
, agno
,
510 suspect
, isroot
, XFS_ABTC_MAGIC
, agcnts
);
515 struct xfs_btree_block
*block
,
522 struct aghdr_cnts
*agcnts
)
531 xfs_extlen_t lastcount
= 0;
532 xfs_agblock_t lastblock
= 0;
534 assert(magic
== XFS_ABTB_MAGIC
|| magic
== XFS_ABTC_MAGIC
);
536 name
= (magic
== XFS_ABTB_MAGIC
) ? "bno" : "cnt";
538 if (be32_to_cpu(block
->bb_magic
) != magic
) {
539 do_warn(_("bad magic # %#x in bt%s block %d/%d\n"),
540 be32_to_cpu(block
->bb_magic
), name
, agno
, bno
);
547 * All freespace btree blocks except the roots are freed for a
548 * fully used filesystem, thus they are counted towards the
549 * free data block counter.
552 agcnts
->agfbtreeblks
++;
556 if (be16_to_cpu(block
->bb_level
) != level
) {
557 do_warn(_("expected level %d got %d in bt%s block %d/%d\n"),
558 level
, be16_to_cpu(block
->bb_level
), name
, agno
, bno
);
565 * check for btree blocks multiply claimed
567 state
= get_bmap(agno
, bno
);
568 if (state
!= XR_E_UNKNOWN
) {
569 set_bmap(agno
, bno
, XR_E_MULT
);
571 _("%s freespace btree block claimed (state %d), agno %d, bno %d, suspect %d\n"),
572 name
, state
, agno
, bno
, suspect
);
575 set_bmap(agno
, bno
, XR_E_FS_MAP
);
577 numrecs
= be16_to_cpu(block
->bb_numrecs
);
580 if (numrecs
> mp
->m_alloc_mxr
[0]) {
581 numrecs
= mp
->m_alloc_mxr
[0];
584 if (isroot
== 0 && numrecs
< mp
->m_alloc_mnr
[0]) {
585 numrecs
= mp
->m_alloc_mnr
[0];
591 _("bad btree nrecs (%u, min=%u, max=%u) in bt%s block %u/%u\n"),
592 be16_to_cpu(block
->bb_numrecs
),
593 mp
->m_alloc_mnr
[0], mp
->m_alloc_mxr
[0],
598 rp
= XFS_ALLOC_REC_ADDR(mp
, block
, 1);
599 for (i
= 0; i
< numrecs
; i
++) {
600 xfs_agblock_t b
, end
;
601 xfs_extlen_t len
, blen
;
603 b
= be32_to_cpu(rp
[i
].ar_startblock
);
604 len
= be32_to_cpu(rp
[i
].ar_blockcount
);
607 if (b
== 0 || !verify_agbno(mp
, agno
, b
)) {
609 _("invalid start block %u in record %u of %s btree block %u/%u\n"),
610 b
, i
, name
, agno
, bno
);
613 if (len
== 0 || !verify_agbno(mp
, agno
, end
- 1)) {
615 _("invalid length %u in record %u of %s btree block %u/%u\n"),
616 len
, i
, name
, agno
, bno
);
620 if (magic
== XFS_ABTB_MAGIC
) {
621 if (b
<= lastblock
) {
623 "out-of-order bno btree record %d (%u %u) block %u/%u\n"),
624 i
, b
, len
, agno
, bno
);
629 agcnts
->fdblocks
+= len
;
630 agcnts
->agffreeblks
+= len
;
631 if (len
> agcnts
->agflongest
)
632 agcnts
->agflongest
= len
;
633 if (len
< lastcount
) {
635 "out-of-order cnt btree record %d (%u %u) block %u/%u\n"),
636 i
, b
, len
, agno
, bno
);
642 for ( ; b
< end
; b
+= blen
) {
643 state
= get_bmap_ext(agno
, b
, end
, &blen
);
646 set_bmap(agno
, b
, XR_E_FREE1
);
650 * no warning messages -- we'll catch
653 if (magic
== XFS_ABTC_MAGIC
) {
654 set_bmap_ext(agno
, b
, blen
,
660 _("block (%d,%d-%d) multiply claimed by %s space tree, state - %d\n"),
661 agno
, b
, b
+ blen
- 1,
673 pp
= XFS_ALLOC_PTR_ADDR(mp
, block
, 1, mp
->m_alloc_mxr
[1]);
675 if (numrecs
> mp
->m_alloc_mxr
[1]) {
676 numrecs
= mp
->m_alloc_mxr
[1];
679 if (isroot
== 0 && numrecs
< mp
->m_alloc_mnr
[1]) {
680 numrecs
= mp
->m_alloc_mnr
[1];
685 * don't pass bogus tree flag down further if this block
686 * looked ok. bail out if two levels in a row look bad.
690 _("bad btree nrecs (%u, min=%u, max=%u) in bt%s block %u/%u\n"),
691 be16_to_cpu(block
->bb_numrecs
),
692 mp
->m_alloc_mnr
[1], mp
->m_alloc_mxr
[1],
697 } else if (suspect
) {
701 for (i
= 0; i
< numrecs
; i
++) {
702 xfs_agblock_t bno
= be32_to_cpu(pp
[i
]);
705 * XXX - put sibling detection right here.
706 * we know our sibling chain is good. So as we go,
707 * we check the entry before and after each entry.
708 * If either of the entries references a different block,
709 * check the sibling pointer. If there's a sibling
710 * pointer mismatch, try and extract as much data
713 if (bno
!= 0 && verify_agbno(mp
, agno
, bno
)) {
714 scan_sbtree(bno
, level
, agno
, suspect
,
715 (magic
== XFS_ABTB_MAGIC
) ?
716 scanfunc_bno
: scanfunc_cnt
, 0,
723 scan_single_ino_chunk(
735 ino_tree_node_t
*ino_rec
, *first_rec
, *last_rec
;
737 ino
= be32_to_cpu(rp
->ir_startino
);
738 off
= XFS_AGINO_TO_OFFSET(mp
, ino
);
739 agbno
= XFS_AGINO_TO_AGBNO(mp
, ino
);
740 lino
= XFS_AGINO_TO_INO(mp
, agno
, ino
);
743 * on multi-block block chunks, all chunks start
744 * at the beginning of the block. with multi-chunk
745 * blocks, all chunks must start on 64-inode boundaries
746 * since each block can hold N complete chunks. if
747 * fs has aligned inodes, all chunks must start
748 * at a fs_ino_alignment*N'th agbno. skip recs
749 * with badly aligned starting inodes.
752 (inodes_per_block
<= XFS_INODES_PER_CHUNK
&& off
!= 0) ||
753 (inodes_per_block
> XFS_INODES_PER_CHUNK
&&
754 off
% XFS_INODES_PER_CHUNK
!= 0) ||
755 (fs_aligned_inodes
&& agbno
% fs_ino_alignment
!= 0)) {
757 _("badly aligned inode rec (starting inode = %" PRIu64
")\n"),
763 * verify numeric validity of inode chunk first
764 * before inserting into a tree. don't have to
765 * worry about the overflow case because the
766 * starting ino number of a chunk can only get
767 * within 255 inodes of max (NULLAGINO). if it
768 * gets closer, the agino number will be illegal
769 * as the agbno will be too large.
771 if (verify_aginum(mp
, agno
, ino
)) {
773 _("bad starting inode # (%" PRIu64
" (0x%x 0x%x)) in ino rec, skipping rec\n"),
778 if (verify_aginum(mp
, agno
,
779 ino
+ XFS_INODES_PER_CHUNK
- 1)) {
781 _("bad ending inode # (%" PRIu64
" (0x%x 0x%zx)) in ino rec, skipping rec\n"),
782 lino
+ XFS_INODES_PER_CHUNK
- 1,
784 ino
+ XFS_INODES_PER_CHUNK
- 1);
789 * set state of each block containing inodes
791 if (off
== 0 && !suspect
) {
793 j
< XFS_INODES_PER_CHUNK
;
794 j
+= mp
->m_sb
.sb_inopblock
) {
795 agbno
= XFS_AGINO_TO_AGBNO(mp
, ino
+ j
);
797 state
= get_bmap(agno
, agbno
);
798 if (state
== XR_E_UNKNOWN
) {
799 set_bmap(agno
, agbno
, XR_E_INO
);
800 } else if (state
== XR_E_INUSE_FS
&& agno
== 0 &&
801 ino
+ j
>= first_prealloc_ino
&&
802 ino
+ j
< last_prealloc_ino
) {
803 set_bmap(agno
, agbno
, XR_E_INO
);
806 _("inode chunk claims used block, inobt block - agno %d, bno %d, inopb %d\n"),
807 agno
, agbno
, mp
->m_sb
.sb_inopblock
);
809 * XXX - maybe should mark
818 * ensure only one avl entry per chunk
820 find_inode_rec_range(mp
, agno
, ino
, ino
+ XFS_INODES_PER_CHUNK
,
821 &first_rec
, &last_rec
);
822 if (first_rec
!= NULL
) {
824 * this chunk overlaps with one (or more)
825 * already in the tree
828 _("inode rec for ino %" PRIu64
" (%d/%d) overlaps existing rec (start %d/%d)\n"),
829 lino
, agno
, ino
, agno
, first_rec
->ino_startnum
);
833 * if the 2 chunks start at the same place,
834 * then we don't have to put this one
835 * in the uncertain list. go to the next one.
837 if (first_rec
->ino_startnum
== ino
)
844 * now mark all the inodes as existing and free or used.
845 * if the tree is suspect, put them into the uncertain
849 if (XFS_INOBT_IS_FREE_DISK(rp
, 0)) {
851 ino_rec
= set_inode_free_alloc(mp
, agno
, ino
);
853 ino_rec
= set_inode_used_alloc(mp
, agno
, ino
);
855 for (j
= 1; j
< XFS_INODES_PER_CHUNK
; j
++) {
856 if (XFS_INOBT_IS_FREE_DISK(rp
, j
)) {
858 set_inode_free(ino_rec
, j
);
860 set_inode_used(ino_rec
, j
);
864 for (j
= 0; j
< XFS_INODES_PER_CHUNK
; j
++) {
865 if (XFS_INOBT_IS_FREE_DISK(rp
, j
)) {
867 add_aginode_uncertain(agno
, ino
+ j
, 1);
869 add_aginode_uncertain(agno
, ino
+ j
, 0);
874 if (nfree
!= be32_to_cpu(rp
->ir_freecount
)) {
875 do_warn(_("ir_freecount/free mismatch, inode "
876 "chunk %d/%u, freecount %d nfree %d\n"),
877 agno
, ino
, be32_to_cpu(rp
->ir_freecount
), nfree
);
885 * this one walks the inode btrees sucking the info there into
886 * the incore avl tree. We try and rescue corrupted btree records
887 * to minimize our chances of losing inodes. Inode info from potentially
888 * corrupt sources could be bogus so rather than put the info straight
889 * into the tree, instead we put it on a list and try and verify the
890 * info in the next phase by examining what's on disk. At that point,
891 * we'll be able to figure out what's what and stick the corrected info
892 * into the tree. We do bail out at some point and give up on a subtree
893 * so as to avoid walking randomly all over the ag.
895 * Note that it's also ok if the free/inuse info wrong, we can correct
896 * that when we examine the on-disk inode. The important thing is to
897 * get the start and alignment of the inode chunks right. Those chunks
898 * that we aren't sure about go into the uncertain list.
902 struct xfs_btree_block
*block
,
910 struct aghdr_cnts
*agcnts
= priv
;
920 if (be32_to_cpu(block
->bb_magic
) != XFS_IBT_MAGIC
) {
921 do_warn(_("bad magic # %#x in inobt block %d/%d\n"),
922 be32_to_cpu(block
->bb_magic
), agno
, bno
);
928 if (be16_to_cpu(block
->bb_level
) != level
) {
929 do_warn(_("expected level %d got %d in inobt block %d/%d\n"),
930 level
, be16_to_cpu(block
->bb_level
), agno
, bno
);
938 * check for btree blocks multiply claimed, any unknown/free state
939 * is ok in the bitmap block.
941 state
= get_bmap(agno
, bno
);
946 set_bmap(agno
, bno
, XR_E_FS_MAP
);
949 set_bmap(agno
, bno
, XR_E_MULT
);
951 _("inode btree block claimed (state %d), agno %d, bno %d, suspect %d\n"),
952 state
, agno
, bno
, suspect
);
955 numrecs
= be16_to_cpu(block
->bb_numrecs
);
958 * leaf record in btree
961 /* check for trashed btree block */
963 if (numrecs
> mp
->m_inobt_mxr
[0]) {
964 numrecs
= mp
->m_inobt_mxr
[0];
967 if (isroot
== 0 && numrecs
< mp
->m_inobt_mnr
[0]) {
968 numrecs
= mp
->m_inobt_mnr
[0];
974 do_warn(_("dubious inode btree block header %d/%d\n"),
979 rp
= XFS_INOBT_REC_ADDR(mp
, block
, 1);
982 * step through the records, each record points to
983 * a chunk of inodes. The start of inode chunks should
984 * be block-aligned. Each inode btree rec should point
985 * to the start of a block of inodes or the start of a group
986 * of INODES_PER_CHUNK (64) inodes. off is the offset into
987 * the block. skip processing of bogus records.
989 for (i
= 0; i
< numrecs
; i
++) {
990 agcnts
->agicount
+= XFS_INODES_PER_CHUNK
;
991 agcnts
->icount
+= XFS_INODES_PER_CHUNK
;
992 agcnts
->agifreecount
+= be32_to_cpu(rp
[i
].ir_freecount
);
993 agcnts
->ifreecount
+= be32_to_cpu(rp
[i
].ir_freecount
);
995 suspect
= scan_single_ino_chunk(agno
, &rp
[i
], suspect
);
1005 * interior record, continue on
1007 if (numrecs
> mp
->m_inobt_mxr
[1]) {
1008 numrecs
= mp
->m_inobt_mxr
[1];
1011 if (isroot
== 0 && numrecs
< mp
->m_inobt_mnr
[1]) {
1012 numrecs
= mp
->m_inobt_mnr
[1];
1016 pp
= XFS_INOBT_PTR_ADDR(mp
, block
, 1, mp
->m_inobt_mxr
[1]);
1019 * don't pass bogus tree flag down further if this block
1020 * looked ok. bail out if two levels in a row look bad.
1023 if (suspect
&& !hdr_errors
)
1033 for (i
= 0; i
< numrecs
; i
++) {
1034 if (be32_to_cpu(pp
[i
]) != 0 && verify_agbno(mp
, agno
,
1035 be32_to_cpu(pp
[i
])))
1036 scan_sbtree(be32_to_cpu(pp
[i
]), level
, agno
,
1037 suspect
, scanfunc_ino
, 0, priv
);
1044 struct aghdr_cnts
*agcnts
)
1048 xfs_agnumber_t agno
;
1053 agno
= be32_to_cpu(agf
->agf_seqno
);
1055 if (XFS_SB_BLOCK(mp
) != XFS_AGFL_BLOCK(mp
) &&
1056 XFS_AGF_BLOCK(mp
) != XFS_AGFL_BLOCK(mp
) &&
1057 XFS_AGI_BLOCK(mp
) != XFS_AGFL_BLOCK(mp
))
1058 set_bmap(agno
, XFS_AGFL_BLOCK(mp
), XR_E_FS_MAP
);
1060 if (be32_to_cpu(agf
->agf_flcount
) == 0)
1063 agflbuf
= libxfs_readbuf(mp
->m_dev
,
1064 XFS_AG_DADDR(mp
, agno
, XFS_AGFL_DADDR(mp
)),
1065 XFS_FSS_TO_BB(mp
, 1), 0);
1067 do_abort(_("can't read agfl block for ag %d\n"), agno
);
1070 agfl
= XFS_BUF_TO_AGFL(agflbuf
);
1071 i
= be32_to_cpu(agf
->agf_flfirst
);
1074 bno
= be32_to_cpu(agfl
->agfl_bno
[i
]);
1075 if (verify_agbno(mp
, agno
, bno
))
1076 set_bmap(agno
, bno
, XR_E_FREE
);
1078 do_warn(_("bad agbno %u in agfl, agno %d\n"),
1081 if (i
== be32_to_cpu(agf
->agf_fllast
))
1083 if (++i
== XFS_AGFL_SIZE(mp
))
1086 if (count
!= be32_to_cpu(agf
->agf_flcount
)) {
1087 do_warn(_("freeblk count %d != flcount %d in ag %d\n"), count
,
1088 be32_to_cpu(agf
->agf_flcount
), agno
);
1091 agcnts
->fdblocks
+= count
;
1093 libxfs_putbuf(agflbuf
);
1098 struct xfs_agf
*agf
,
1099 xfs_agnumber_t agno
,
1100 struct aghdr_cnts
*agcnts
)
1104 bno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_BNO
]);
1105 if (bno
!= 0 && verify_agbno(mp
, agno
, bno
)) {
1106 scan_sbtree(bno
, be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]),
1107 agno
, 0, scanfunc_bno
, 1, agcnts
);
1109 do_warn(_("bad agbno %u for btbno root, agno %d\n"),
1113 bno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_CNT
]);
1114 if (bno
!= 0 && verify_agbno(mp
, agno
, bno
)) {
1115 scan_sbtree(bno
, be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]),
1116 agno
, 0, scanfunc_cnt
, 1, agcnts
);
1118 do_warn(_("bad agbno %u for btbcnt root, agno %d\n"),
1122 if (be32_to_cpu(agf
->agf_freeblks
) != agcnts
->agffreeblks
) {
1123 do_warn(_("agf_freeblks %u, counted %u in ag %u\n"),
1124 be32_to_cpu(agf
->agf_freeblks
), agcnts
->agffreeblks
, agno
);
1127 if (be32_to_cpu(agf
->agf_longest
) != agcnts
->agflongest
) {
1128 do_warn(_("agf_longest %u, counted %u in ag %u\n"),
1129 be32_to_cpu(agf
->agf_longest
), agcnts
->agflongest
, agno
);
1132 if (xfs_sb_version_haslazysbcount(&mp
->m_sb
) &&
1133 be32_to_cpu(agf
->agf_btreeblks
) != agcnts
->agfbtreeblks
) {
1134 do_warn(_("agf_btreeblks %u, counted %" PRIu64
" in ag %u\n"),
1135 be32_to_cpu(agf
->agf_btreeblks
), agcnts
->agfbtreeblks
, agno
);
1141 struct xfs_agi
*agi
,
1142 xfs_agnumber_t agno
,
1143 struct aghdr_cnts
*agcnts
)
1148 bno
= be32_to_cpu(agi
->agi_root
);
1149 if (bno
!= 0 && verify_agbno(mp
, agno
, bno
)) {
1150 scan_sbtree(bno
, be32_to_cpu(agi
->agi_level
),
1151 agno
, 0, scanfunc_ino
, 1, agcnts
);
1153 do_warn(_("bad agbno %u for inobt root, agno %d\n"),
1154 be32_to_cpu(agi
->agi_root
), agno
);
1157 if (be32_to_cpu(agi
->agi_count
) != agcnts
->agicount
) {
1158 do_warn(_("agi_count %u, counted %u in ag %u\n"),
1159 be32_to_cpu(agi
->agi_count
), agcnts
->agicount
, agno
);
1162 if (be32_to_cpu(agi
->agi_freecount
) != agcnts
->agifreecount
) {
1163 do_warn(_("agi_freecount %u, counted %u in ag %u\n"),
1164 be32_to_cpu(agi
->agi_freecount
), agcnts
->agifreecount
, agno
);
1167 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++) {
1168 xfs_agino_t agino
= be32_to_cpu(agi
->agi_unlinked
[i
]);
1170 if (agino
!= NULLAGINO
) {
1172 _("agi unlinked bucket %d is %u in ag %u (inode=%" PRIu64
")\n"),
1174 XFS_AGINO_TO_INO(mp
, agno
, agino
));
1180 * Scan an AG for obvious corruption.
1185 xfs_agnumber_t agno
,
1188 struct aghdr_cnts
*agcnts
= arg
;
1200 sbbuf
= libxfs_readbuf(mp
->m_dev
, XFS_AG_DADDR(mp
, agno
, XFS_SB_DADDR
),
1201 XFS_FSS_TO_BB(mp
, 1), 0);
1203 do_error(_("can't get root superblock for ag %d\n"), agno
);
1207 sb
= (xfs_sb_t
*)calloc(BBSIZE
, 1);
1209 do_error(_("can't allocate memory for superblock\n"));
1210 libxfs_putbuf(sbbuf
);
1213 libxfs_sb_from_disk(sb
, XFS_BUF_TO_SBP(sbbuf
));
1215 agfbuf
= libxfs_readbuf(mp
->m_dev
,
1216 XFS_AG_DADDR(mp
, agno
, XFS_AGF_DADDR(mp
)),
1217 XFS_FSS_TO_BB(mp
, 1), 0);
1219 do_error(_("can't read agf block for ag %d\n"), agno
);
1220 libxfs_putbuf(sbbuf
);
1224 agf
= XFS_BUF_TO_AGF(agfbuf
);
1226 agibuf
= libxfs_readbuf(mp
->m_dev
,
1227 XFS_AG_DADDR(mp
, agno
, XFS_AGI_DADDR(mp
)),
1228 XFS_FSS_TO_BB(mp
, 1), 0);
1230 do_error(_("can't read agi block for ag %d\n"), agno
);
1231 libxfs_putbuf(agfbuf
);
1232 libxfs_putbuf(sbbuf
);
1236 agi
= XFS_BUF_TO_AGI(agibuf
);
1238 /* fix up bad ag headers */
1240 status
= verify_set_agheader(mp
, sbbuf
, sb
, agf
, agi
, agno
);
1242 if (status
& XR_AG_SB_SEC
) {
1246 * clear bad sector bit because we don't want
1247 * to skip further processing. we just want to
1248 * ensure that we write out the modified sb buffer.
1250 status
&= ~XR_AG_SB_SEC
;
1252 if (status
& XR_AG_SB
) {
1254 do_warn(_("reset bad sb for ag %d\n"), agno
);
1257 do_warn(_("would reset bad sb for ag %d\n"), agno
);
1260 if (status
& XR_AG_AGF
) {
1262 do_warn(_("reset bad agf for ag %d\n"), agno
);
1265 do_warn(_("would reset bad agf for ag %d\n"), agno
);
1268 if (status
& XR_AG_AGI
) {
1270 do_warn(_("reset bad agi for ag %d\n"), agno
);
1273 do_warn(_("would reset bad agi for ag %d\n"), agno
);
1277 if (status
&& no_modify
) {
1278 libxfs_putbuf(agibuf
);
1279 libxfs_putbuf(agfbuf
);
1280 libxfs_putbuf(sbbuf
);
1283 do_warn(_("bad uncorrected agheader %d, skipping ag...\n"),
1289 scan_freelist(agf
, agcnts
);
1291 validate_agf(agf
, agno
, agcnts
);
1292 validate_agi(agi
, agno
, agcnts
);
1294 ASSERT(agi_dirty
== 0 || (agi_dirty
&& !no_modify
));
1296 if (agi_dirty
&& !no_modify
)
1297 libxfs_writebuf(agibuf
, 0);
1299 libxfs_putbuf(agibuf
);
1301 ASSERT(agf_dirty
== 0 || (agf_dirty
&& !no_modify
));
1303 if (agf_dirty
&& !no_modify
)
1304 libxfs_writebuf(agfbuf
, 0);
1306 libxfs_putbuf(agfbuf
);
1308 ASSERT(sb_dirty
== 0 || (sb_dirty
&& !no_modify
));
1310 if (sb_dirty
&& !no_modify
) {
1312 memcpy(&mp
->m_sb
, sb
, sizeof(xfs_sb_t
));
1313 libxfs_sb_to_disk(XFS_BUF_TO_SBP(sbbuf
), sb
, XFS_SB_ALL_BITS
);
1314 libxfs_writebuf(sbbuf
, 0);
1316 libxfs_putbuf(sbbuf
);
1318 PROG_RPT_INC(prog_rpt_done
[agno
], 1);
1320 #ifdef XR_INODE_TRACE
1321 print_inode_list(i
);
1326 #define SCAN_THREADS 32
1330 struct xfs_mount
*mp
,
1333 struct aghdr_cnts
*agcnts
;
1334 __uint64_t fdblocks
= 0;
1335 __uint64_t icount
= 0;
1336 __uint64_t ifreecount
= 0;
1340 agcnts
= malloc(mp
->m_sb
.sb_agcount
* sizeof(*agcnts
));
1342 do_abort(_("no memory for ag header counts\n"));
1345 memset(agcnts
, 0, mp
->m_sb
.sb_agcount
* sizeof(*agcnts
));
1347 create_work_queue(&wq
, mp
, scan_threads
);
1349 for (i
= 0; i
< mp
->m_sb
.sb_agcount
; i
++)
1350 queue_work(&wq
, scan_ag
, i
, &agcnts
[i
]);
1352 destroy_work_queue(&wq
);
1354 /* tally up the counts */
1355 for (i
= 0; i
< mp
->m_sb
.sb_agcount
; i
++) {
1356 fdblocks
+= agcnts
[i
].fdblocks
;
1357 icount
+= agcnts
[i
].icount
;
1358 ifreecount
+= agcnts
[i
].ifreecount
;
1362 * Validate that our manual counts match the superblock.
1364 if (mp
->m_sb
.sb_icount
!= icount
) {
1365 do_warn(_("sb_icount %" PRIu64
", counted %" PRIu64
"\n"),
1366 mp
->m_sb
.sb_icount
, icount
);
1369 if (mp
->m_sb
.sb_ifree
!= ifreecount
) {
1370 do_warn(_("sb_ifree %" PRIu64
", counted %" PRIu64
"\n"),
1371 mp
->m_sb
.sb_ifree
, ifreecount
);
1374 if (mp
->m_sb
.sb_fdblocks
!= fdblocks
) {
1375 do_warn(_("sb_fdblocks %" PRIu64
", counted %" PRIu64
"\n"),
1376 mp
->m_sb
.sb_fdblocks
, fdblocks
);