forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/fs/xfs/scrub/repair.c
....@@ -9,27 +9,21 @@
99 #include "xfs_format.h"
1010 #include "xfs_trans_resv.h"
1111 #include "xfs_mount.h"
12
-#include "xfs_defer.h"
1312 #include "xfs_btree.h"
14
-#include "xfs_bit.h"
1513 #include "xfs_log_format.h"
1614 #include "xfs_trans.h"
1715 #include "xfs_sb.h"
1816 #include "xfs_inode.h"
19
-#include "xfs_icache.h"
2017 #include "xfs_alloc.h"
2118 #include "xfs_alloc_btree.h"
2219 #include "xfs_ialloc.h"
2320 #include "xfs_ialloc_btree.h"
2421 #include "xfs_rmap.h"
2522 #include "xfs_rmap_btree.h"
26
-#include "xfs_refcount.h"
2723 #include "xfs_refcount_btree.h"
2824 #include "xfs_extent_busy.h"
2925 #include "xfs_ag_resv.h"
30
-#include "xfs_trans_space.h"
3126 #include "xfs_quota.h"
32
-#include "scrub/xfs_scrub.h"
3327 #include "scrub/scrub.h"
3428 #include "scrub/common.h"
3529 #include "scrub/trace.h"
....@@ -44,8 +38,7 @@
4438 int
4539 xrep_attempt(
4640 struct xfs_inode *ip,
47
- struct xfs_scrub *sc,
48
- bool *fixed)
41
+ struct xfs_scrub *sc)
4942 {
5043 int error = 0;
5144
....@@ -64,13 +57,13 @@
6457 * scrub so that we can tell userspace if we fixed the problem.
6558 */
6659 sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
67
- *fixed = true;
60
+ sc->flags |= XREP_ALREADY_FIXED;
6861 return -EAGAIN;
6962 case -EDEADLOCK:
7063 case -EAGAIN:
7164 /* Tell the caller to try again having grabbed all the locks. */
72
- if (!sc->try_harder) {
73
- sc->try_harder = true;
65
+ if (!(sc->flags & XCHK_TRY_HARDER)) {
66
+ sc->flags |= XCHK_TRY_HARDER;
7467 return -EAGAIN;
7568 }
7669 /*
....@@ -135,10 +128,16 @@
135128 if (sc->sa.agfl_bp)
136129 xfs_trans_bhold(sc->tp, sc->sa.agfl_bp);
137130
138
- /* Roll the transaction. */
131
+ /*
132
+ * Roll the transaction. We still own the buffer and the buffer lock
133
+ * regardless of whether or not the roll succeeds. If the roll fails,
134
+ * the buffers will be released during teardown on our way out of the
135
+ * kernel. If it succeeds, we join them to the new transaction and
136
+ * move on.
137
+ */
139138 error = xfs_trans_roll(&sc->tp);
140139 if (error)
141
- goto out_release;
140
+ return error;
142141
143142 /* Join AG headers to the new transaction. */
144143 if (sc->sa.agi_bp)
....@@ -149,21 +148,6 @@
149148 xfs_trans_bjoin(sc->tp, sc->sa.agfl_bp);
150149
151150 return 0;
152
-
153
-out_release:
154
- /*
155
- * Rolling failed, so release the hold on the buffers. The
156
- * buffers will be released during teardown on our way out
157
- * of the kernel.
158
- */
159
- if (sc->sa.agi_bp)
160
- xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp);
161
- if (sc->sa.agf_bp)
162
- xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp);
163
- if (sc->sa.agfl_bp)
164
- xfs_trans_bhold_release(sc->tp, sc->sa.agfl_bp);
165
-
166
- return error;
167151 }
168152
169153 /*
....@@ -224,8 +208,10 @@
224208 /* Now grab the block counters from the AGF. */
225209 error = xfs_alloc_read_agf(mp, NULL, sm->sm_agno, 0, &bp);
226210 if (!error) {
227
- aglen = be32_to_cpu(XFS_BUF_TO_AGF(bp)->agf_length);
228
- freelen = be32_to_cpu(XFS_BUF_TO_AGF(bp)->agf_freeblks);
211
+ struct xfs_agf *agf = bp->b_addr;
212
+
213
+ aglen = be32_to_cpu(agf->agf_length);
214
+ freelen = be32_to_cpu(agf->agf_freeblks);
229215 usedlen = aglen - freelen;
230216 xfs_buf_relse(bp);
231217 }
....@@ -297,14 +283,14 @@
297283 /* Allocate a block in an AG. */
298284 int
299285 xrep_alloc_ag_block(
300
- struct xfs_scrub *sc,
301
- struct xfs_owner_info *oinfo,
302
- xfs_fsblock_t *fsbno,
303
- enum xfs_ag_resv_type resv)
286
+ struct xfs_scrub *sc,
287
+ const struct xfs_owner_info *oinfo,
288
+ xfs_fsblock_t *fsbno,
289
+ enum xfs_ag_resv_type resv)
304290 {
305
- struct xfs_alloc_arg args = {0};
306
- xfs_agblock_t bno;
307
- int error;
291
+ struct xfs_alloc_arg args = {0};
292
+ xfs_agblock_t bno;
293
+ int error;
308294
309295 switch (resv) {
310296 case XFS_AG_RESV_AGFL:
....@@ -357,17 +343,21 @@
357343 struct xfs_trans *tp = sc->tp;
358344 struct xfs_mount *mp = sc->mp;
359345 struct xfs_buf *bp;
346
+ int error;
360347
361348 trace_xrep_init_btblock(mp, XFS_FSB_TO_AGNO(mp, fsb),
362349 XFS_FSB_TO_AGBNO(mp, fsb), btnum);
363350
364351 ASSERT(XFS_FSB_TO_AGNO(mp, fsb) == sc->sa.agno);
365
- bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, XFS_FSB_TO_DADDR(mp, fsb),
366
- XFS_FSB_TO_BB(mp, 1), 0);
352
+ error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
353
+ XFS_FSB_TO_DADDR(mp, fsb), XFS_FSB_TO_BB(mp, 1), 0,
354
+ &bp);
355
+ if (error)
356
+ return error;
367357 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
368
- xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.agno, 0);
358
+ xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.agno);
369359 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
370
- xfs_trans_log_buf(tp, bp, 0, bp->b_length);
360
+ xfs_trans_log_buf(tp, bp, 0, BBTOB(bp->b_length) - 1);
371361 bp->b_ops = ops;
372362 *bpp = bp;
373363
....@@ -446,10 +436,10 @@
446436 int
447437 xrep_invalidate_blocks(
448438 struct xfs_scrub *sc,
449
- struct xfs_bitmap *bitmap)
439
+ struct xbitmap *bitmap)
450440 {
451
- struct xfs_bitmap_range *bmr;
452
- struct xfs_bitmap_range *n;
441
+ struct xbitmap_range *bmr;
442
+ struct xbitmap_range *n;
453443 struct xfs_buf *bp;
454444 xfs_fsblock_t fsbno;
455445
....@@ -461,7 +451,7 @@
461451 * because we never own those; and if we can't TRYLOCK the buffer we
462452 * assume it's owned by someone else.
463453 */
464
- for_each_xfs_bitmap_block(fsbno, bmr, n, bitmap) {
454
+ for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
465455 /* Skip AG headers and post-EOFS blocks */
466456 if (!xfs_verify_fsbno(sc->mp, fsbno))
467457 continue;
....@@ -503,7 +493,6 @@
503493 struct xfs_scrub *sc,
504494 xfs_agblock_t agbno)
505495 {
506
- struct xfs_owner_info oinfo;
507496 int error;
508497
509498 /* Make sure there's space on the freelist. */
....@@ -516,9 +505,8 @@
516505 * create an rmap for the block prior to merging it or else other
517506 * parts will break.
518507 */
519
- xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_AG);
520508 error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.agno, agbno, 1,
521
- &oinfo);
509
+ &XFS_RMAP_OINFO_AG);
522510 if (error)
523511 return error;
524512
....@@ -536,17 +524,17 @@
536524 /* Dispose of a single block. */
537525 STATIC int
538526 xrep_reap_block(
539
- struct xfs_scrub *sc,
540
- xfs_fsblock_t fsbno,
541
- struct xfs_owner_info *oinfo,
542
- enum xfs_ag_resv_type resv)
527
+ struct xfs_scrub *sc,
528
+ xfs_fsblock_t fsbno,
529
+ const struct xfs_owner_info *oinfo,
530
+ enum xfs_ag_resv_type resv)
543531 {
544
- struct xfs_btree_cur *cur;
545
- struct xfs_buf *agf_bp = NULL;
546
- xfs_agnumber_t agno;
547
- xfs_agblock_t agbno;
548
- bool has_other_rmap;
549
- int error;
532
+ struct xfs_btree_cur *cur;
533
+ struct xfs_buf *agf_bp = NULL;
534
+ xfs_agnumber_t agno;
535
+ xfs_agblock_t agbno;
536
+ bool has_other_rmap;
537
+ int error;
550538
551539 agno = XFS_FSB_TO_AGNO(sc->mp, fsbno);
552540 agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
....@@ -560,8 +548,6 @@
560548 error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf_bp);
561549 if (error)
562550 return error;
563
- if (!agf_bp)
564
- return -ENOMEM;
565551 } else {
566552 agf_bp = sc->sa.agf_bp;
567553 }
....@@ -610,19 +596,19 @@
610596 /* Dispose of every block of every extent in the bitmap. */
611597 int
612598 xrep_reap_extents(
613
- struct xfs_scrub *sc,
614
- struct xfs_bitmap *bitmap,
615
- struct xfs_owner_info *oinfo,
616
- enum xfs_ag_resv_type type)
599
+ struct xfs_scrub *sc,
600
+ struct xbitmap *bitmap,
601
+ const struct xfs_owner_info *oinfo,
602
+ enum xfs_ag_resv_type type)
617603 {
618
- struct xfs_bitmap_range *bmr;
619
- struct xfs_bitmap_range *n;
620
- xfs_fsblock_t fsbno;
621
- int error = 0;
604
+ struct xbitmap_range *bmr;
605
+ struct xbitmap_range *n;
606
+ xfs_fsblock_t fsbno;
607
+ int error = 0;
622608
623609 ASSERT(xfs_sb_version_hasrmapbt(&sc->mp->m_sb));
624610
625
- for_each_xfs_bitmap_block(fsbno, bmr, n, bitmap) {
611
+ for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
626612 ASSERT(sc->ip != NULL ||
627613 XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.agno);
628614 trace_xrep_dispose_btree_extent(sc->mp,
....@@ -631,11 +617,9 @@
631617
632618 error = xrep_reap_block(sc, fsbno, oinfo, type);
633619 if (error)
634
- goto out;
620
+ break;
635621 }
636622
637
-out:
638
- xfs_bitmap_destroy(bitmap);
639623 return error;
640624 }
641625
....@@ -682,7 +666,7 @@
682666 {
683667 xfs_agblock_t *agbno = priv;
684668
685
- return (*agbno == bno) ? XFS_BTREE_QUERY_RANGE_ABORT : 0;
669
+ return (*agbno == bno) ? -ECANCELED : 0;
686670 }
687671
688672 /* Does this block match the btree information passed in? */
....@@ -692,13 +676,14 @@
692676 struct xrep_find_ag_btree *fab,
693677 uint64_t owner,
694678 xfs_agblock_t agbno,
695
- bool *found_it)
679
+ bool *done_with_block)
696680 {
697681 struct xfs_mount *mp = ri->sc->mp;
698682 struct xfs_buf *bp;
699683 struct xfs_btree_block *btblock;
700684 xfs_daddr_t daddr;
701
- int error;
685
+ int block_level;
686
+ int error = 0;
702687
703688 daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.agno, agbno);
704689
....@@ -711,42 +696,123 @@
711696 if (owner == XFS_RMAP_OWN_AG) {
712697 error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
713698 xrep_findroot_agfl_walk, &agbno);
714
- if (error == XFS_BTREE_QUERY_RANGE_ABORT)
699
+ if (error == -ECANCELED)
715700 return 0;
716701 if (error)
717702 return error;
718703 }
719704
705
+ /*
706
+ * Read the buffer into memory so that we can see if it's a match for
707
+ * our btree type. We have no clue if it is beforehand, and we want to
708
+ * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
709
+ * will cause needless disk reads in subsequent calls to this function)
710
+ * and logging metadata verifier failures.
711
+ *
712
+ * Therefore, pass in NULL buffer ops. If the buffer was already in
713
+ * memory from some other caller it will already have b_ops assigned.
714
+ * If it was in memory from a previous unsuccessful findroot_block
715
+ * call, the buffer won't have b_ops but it should be clean and ready
716
+ * for us to try to verify if the read call succeeds. The same applies
717
+ * if the buffer wasn't in memory at all.
718
+ *
719
+ * Note: If we never match a btree type with this buffer, it will be
720
+ * left in memory with NULL b_ops. This shouldn't be a problem unless
721
+ * the buffer gets written.
722
+ */
720723 error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
721724 mp->m_bsize, 0, &bp, NULL);
722725 if (error)
723726 return error;
724727
725
- /*
726
- * Does this look like a block matching our fs and higher than any
727
- * other block we've found so far? If so, reattach buffer verifiers
728
- * so the AIL won't complain if the buffer is also dirty.
729
- */
728
+ /* Ensure the block magic matches the btree type we're looking for. */
730729 btblock = XFS_BUF_TO_BLOCK(bp);
731
- if (be32_to_cpu(btblock->bb_magic) != fab->magic)
732
- goto out;
733
- if (xfs_sb_version_hascrc(&mp->m_sb) &&
734
- !uuid_equal(&btblock->bb_u.s.bb_uuid, &mp->m_sb.sb_meta_uuid))
735
- goto out;
736
- bp->b_ops = fab->buf_ops;
737
-
738
- /* Ignore this block if it's lower in the tree than we've seen. */
739
- if (fab->root != NULLAGBLOCK &&
740
- xfs_btree_get_level(btblock) < fab->height)
730
+ ASSERT(fab->buf_ops->magic[1] != 0);
731
+ if (btblock->bb_magic != fab->buf_ops->magic[1])
741732 goto out;
742733
743
- /* Make sure we pass the verifiers. */
744
- bp->b_ops->verify_read(bp);
745
- if (bp->b_error)
734
+ /*
735
+ * If the buffer already has ops applied and they're not the ones for
736
+ * this btree type, we know this block doesn't match the btree and we
737
+ * can bail out.
738
+ *
739
+ * If the buffer ops match ours, someone else has already validated
740
+ * the block for us, so we can move on to checking if this is a root
741
+ * block candidate.
742
+ *
743
+ * If the buffer does not have ops, nobody has successfully validated
744
+ * the contents and the buffer cannot be dirty. If the magic, uuid,
745
+ * and structure match this btree type then we'll move on to checking
746
+ * if it's a root block candidate. If there is no match, bail out.
747
+ */
748
+ if (bp->b_ops) {
749
+ if (bp->b_ops != fab->buf_ops)
750
+ goto out;
751
+ } else {
752
+ ASSERT(!xfs_trans_buf_is_dirty(bp));
753
+ if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
754
+ &mp->m_sb.sb_meta_uuid))
755
+ goto out;
756
+ /*
757
+ * Read verifiers can reference b_ops, so we set the pointer
758
+ * here. If the verifier fails we'll reset the buffer state
759
+ * to what it was before we touched the buffer.
760
+ */
761
+ bp->b_ops = fab->buf_ops;
762
+ fab->buf_ops->verify_read(bp);
763
+ if (bp->b_error) {
764
+ bp->b_ops = NULL;
765
+ bp->b_error = 0;
766
+ goto out;
767
+ }
768
+
769
+ /*
770
+ * Some read verifiers will (re)set b_ops, so we must be
771
+ * careful not to change b_ops after running the verifier.
772
+ */
773
+ }
774
+
775
+ /*
776
+ * This block passes the magic/uuid and verifier tests for this btree
777
+ * type. We don't need the caller to try the other tree types.
778
+ */
779
+ *done_with_block = true;
780
+
781
+ /*
782
+ * Compare this btree block's level to the height of the current
783
+ * candidate root block.
784
+ *
785
+ * If the level matches the root we found previously, throw away both
786
+ * blocks because there can't be two candidate roots.
787
+ *
788
+ * If level is lower in the tree than the root we found previously,
789
+ * ignore this block.
790
+ */
791
+ block_level = xfs_btree_get_level(btblock);
792
+ if (block_level + 1 == fab->height) {
793
+ fab->root = NULLAGBLOCK;
746794 goto out;
747
- fab->root = agbno;
748
- fab->height = xfs_btree_get_level(btblock) + 1;
749
- *found_it = true;
795
+ } else if (block_level < fab->height) {
796
+ goto out;
797
+ }
798
+
799
+ /*
800
+ * This is the highest block in the tree that we've found so far.
801
+ * Update the btree height to reflect what we've learned from this
802
+ * block.
803
+ */
804
+ fab->height = block_level + 1;
805
+
806
+ /*
807
+ * If this block doesn't have sibling pointers, then it's the new root
808
+ * block candidate. Otherwise, the root will be found farther up the
809
+ * tree.
810
+ */
811
+ if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
812
+ btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
813
+ fab->root = agbno;
814
+ else
815
+ fab->root = NULLAGBLOCK;
750816
751817 trace_xrep_findroot_block(mp, ri->sc->sa.agno, agbno,
752818 be32_to_cpu(btblock->bb_magic), fab->height - 1);
....@@ -768,7 +834,7 @@
768834 struct xrep_findroot *ri = priv;
769835 struct xrep_find_ag_btree *fab;
770836 xfs_agblock_t b;
771
- bool found_it;
837
+ bool done;
772838 int error = 0;
773839
774840 /* Ignore anything that isn't AG metadata. */
....@@ -777,16 +843,16 @@
777843
778844 /* Otherwise scan each block + btree type. */
779845 for (b = 0; b < rec->rm_blockcount; b++) {
780
- found_it = false;
846
+ done = false;
781847 for (fab = ri->btree_info; fab->buf_ops; fab++) {
782848 if (rec->rm_owner != fab->rmap_owner)
783849 continue;
784850 error = xrep_findroot_block(ri, fab,
785851 rec->rm_owner, rec->rm_startblock + b,
786
- &found_it);
852
+ &done);
787853 if (error)
788854 return error;
789
- if (found_it)
855
+ if (done)
790856 break;
791857 }
792858 }
....@@ -813,7 +879,7 @@
813879
814880 ri.sc = sc;
815881 ri.btree_info = btree_info;
816
- ri.agf = XFS_BUF_TO_AGF(agf_bp);
882
+ ri.agf = agf_bp->b_addr;
817883 ri.agfl_bp = agfl_bp;
818884 for (fab = btree_info; fab->buf_ops; fab++) {
819885 ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
....@@ -833,11 +899,11 @@
833899 void
834900 xrep_force_quotacheck(
835901 struct xfs_scrub *sc,
836
- uint dqtype)
902
+ xfs_dqtype_t type)
837903 {
838904 uint flag;
839905
840
- flag = xfs_quota_chkd_flag(dqtype);
906
+ flag = xfs_quota_chkd_flag(type);
841907 if (!(flag & sc->mp->m_qflags))
842908 return;
843909
....@@ -873,11 +939,11 @@
873939 "inode %llu repair encountered quota error %d, quotacheck forced.",
874940 (unsigned long long)sc->ip->i_ino, error);
875941 if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
876
- xrep_force_quotacheck(sc, XFS_DQ_USER);
942
+ xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
877943 if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
878
- xrep_force_quotacheck(sc, XFS_DQ_GROUP);
944
+ xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
879945 if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
880
- xrep_force_quotacheck(sc, XFS_DQ_PROJ);
946
+ xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
881947 /* fall through */
882948 case -ESRCH:
883949 error = 0;