agb_bitmap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2018-2023 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <djwong@kernel.org>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_shared.h"
  8. #include "xfs_bit.h"
  9. #include "xfs_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_btree.h"
  13. #include "bitmap.h"
  14. #include "scrub/agb_bitmap.h"
  15. /*
  16. * Record all btree blocks seen while iterating all records of a btree.
  17. *
  18. * We know that the btree query_all function starts at the left edge and walks
  19. * towards the right edge of the tree. Therefore, we know that we can walk up
  20. * the btree cursor towards the root; if the pointer for a given level points
  21. * to the first record/key in that block, we haven't seen this block before;
  22. * and therefore we need to remember that we saw this block in the btree.
  23. *
  24. * So if our btree is:
  25. *
  26. * 4
  27. * / | \
  28. * 1 2 3
  29. *
  30. * Pretend for this example that each leaf block has 100 btree records. For
  31. * the first btree record, we'll observe that bc_levels[0].ptr == 1, so we
  32. * record that we saw block 1. Then we observe that bc_levels[1].ptr == 1, so
  33. * we record block 4. The list is [1, 4].
  34. *
  35. * For the second btree record, we see that bc_levels[0].ptr == 2, so we exit
  36. * the loop. The list remains [1, 4].
  37. *
  38. * For the 101st btree record, we've moved onto leaf block 2. Now
  39. * bc_levels[0].ptr == 1 again, so we record that we saw block 2. We see that
  40. * bc_levels[1].ptr == 2, so we exit the loop. The list is now [1, 4, 2].
  41. *
  42. * For the 102nd record, bc_levels[0].ptr == 2, so we continue.
  43. *
  44. * For the 201st record, we've moved on to leaf block 3.
  45. * bc_levels[0].ptr == 1, so we add 3 to the list. Now it is [1, 4, 2, 3].
  46. *
  47. * For the 300th record we just exit, with the list being [1, 4, 2, 3].
  48. */
  49. /* Mark a btree block to the agblock bitmap. */
  50. STATIC int
  51. xagb_bitmap_visit_btblock(
  52. struct xfs_btree_cur *cur,
  53. int level,
  54. void *priv)
  55. {
  56. struct xagb_bitmap *bitmap = priv;
  57. struct xfs_buf *bp;
  58. xfs_fsblock_t fsbno;
  59. xfs_agblock_t agbno;
  60. xfs_btree_get_block(cur, level, &bp);
  61. if (!bp)
  62. return 0;
  63. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, xfs_buf_daddr(bp));
  64. agbno = XFS_FSB_TO_AGBNO(cur->bc_mp, fsbno);
  65. return xagb_bitmap_set(bitmap, agbno, 1);
  66. }
  67. /* Mark all (per-AG) btree blocks in the agblock bitmap. */
  68. int
  69. xagb_bitmap_set_btblocks(
  70. struct xagb_bitmap *bitmap,
  71. struct xfs_btree_cur *cur)
  72. {
  73. return xfs_btree_visit_blocks(cur, xagb_bitmap_visit_btblock,
  74. XFS_BTREE_VISIT_ALL, bitmap);
  75. }
  76. /*
  77. * Record all the buffers pointed to by the btree cursor. Callers already
  78. * engaged in a btree walk should call this function to capture the list of
  79. * blocks going from the leaf towards the root.
  80. */
  81. int
  82. xagb_bitmap_set_btcur_path(
  83. struct xagb_bitmap *bitmap,
  84. struct xfs_btree_cur *cur)
  85. {
  86. int i;
  87. int error;
  88. for (i = 0; i < cur->bc_nlevels && cur->bc_levels[i].ptr == 1; i++) {
  89. error = xagb_bitmap_visit_btblock(cur, i, bitmap);
  90. if (error)
  91. return error;
  92. }
  93. return 0;
  94. }