bitmap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  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"
  13. #include "scrub/bitmap.h"
  14. /*
  15. * Set a range of this bitmap. Caller must ensure the range is not set.
  16. *
  17. * This is the logical equivalent of bitmap |= mask(start, len).
  18. */
  19. int
  20. xbitmap_set(
  21. struct xbitmap *bitmap,
  22. uint64_t start,
  23. uint64_t len)
  24. {
  25. struct xbitmap_range *bmr;
  26. bmr = kmem_alloc(sizeof(struct xbitmap_range), KM_MAYFAIL);
  27. if (!bmr)
  28. return -ENOMEM;
  29. INIT_LIST_HEAD(&bmr->list);
  30. bmr->start = start;
  31. bmr->len = len;
  32. list_add_tail(&bmr->list, &bitmap->list);
  33. return 0;
  34. }
  35. /* Free everything related to this bitmap. */
  36. void
  37. xbitmap_destroy(
  38. struct xbitmap *bitmap)
  39. {
  40. struct xbitmap_range *bmr;
  41. struct xbitmap_range *n;
  42. for_each_xbitmap_extent(bmr, n, bitmap) {
  43. list_del(&bmr->list);
  44. kmem_free(bmr);
  45. }
  46. }
  47. /* Set up a per-AG block bitmap. */
  48. void
  49. xbitmap_init(
  50. struct xbitmap *bitmap)
  51. {
  52. INIT_LIST_HEAD(&bitmap->list);
  53. }
  54. /* Compare two btree extents. */
  55. static int
  56. xbitmap_range_cmp(
  57. void *priv,
  58. const struct list_head *a,
  59. const struct list_head *b)
  60. {
  61. struct xbitmap_range *ap;
  62. struct xbitmap_range *bp;
  63. ap = container_of(a, struct xbitmap_range, list);
  64. bp = container_of(b, struct xbitmap_range, list);
  65. if (ap->start > bp->start)
  66. return 1;
  67. if (ap->start < bp->start)
  68. return -1;
  69. return 0;
  70. }
  71. /*
  72. * Remove all the blocks mentioned in @sub from the extents in @bitmap.
  73. *
  74. * The intent is that callers will iterate the rmapbt for all of its records
  75. * for a given owner to generate @bitmap; and iterate all the blocks of the
  76. * metadata structures that are not being rebuilt and have the same rmapbt
  77. * owner to generate @sub. This routine subtracts all the extents
  78. * mentioned in sub from all the extents linked in @bitmap, which leaves
  79. * @bitmap as the list of blocks that are not accounted for, which we assume
  80. * are the dead blocks of the old metadata structure. The blocks mentioned in
  81. * @bitmap can be reaped.
  82. *
  83. * This is the logical equivalent of bitmap &= ~sub.
  84. */
  85. #define LEFT_ALIGNED (1 << 0)
  86. #define RIGHT_ALIGNED (1 << 1)
  87. int
  88. xbitmap_disunion(
  89. struct xbitmap *bitmap,
  90. struct xbitmap *sub)
  91. {
  92. struct list_head *lp;
  93. struct xbitmap_range *br;
  94. struct xbitmap_range *new_br;
  95. struct xbitmap_range *sub_br;
  96. uint64_t sub_start;
  97. uint64_t sub_len;
  98. int state;
  99. int error = 0;
  100. if (list_empty(&bitmap->list) || list_empty(&sub->list))
  101. return 0;
  102. ASSERT(!list_empty(&sub->list));
  103. list_sort(NULL, &bitmap->list, xbitmap_range_cmp);
  104. list_sort(NULL, &sub->list, xbitmap_range_cmp);
  105. /*
  106. * Now that we've sorted both lists, we iterate bitmap once, rolling
  107. * forward through sub and/or bitmap as necessary until we find an
  108. * overlap or reach the end of either list. We do not reset lp to the
  109. * head of bitmap nor do we reset sub_br to the head of sub. The
  110. * list traversal is similar to merge sort, but we're deleting
  111. * instead. In this manner we avoid O(n^2) operations.
  112. */
  113. sub_br = list_first_entry(&sub->list, struct xbitmap_range,
  114. list);
  115. lp = bitmap->list.next;
  116. while (lp != &bitmap->list) {
  117. br = list_entry(lp, struct xbitmap_range, list);
  118. /*
  119. * Advance sub_br and/or br until we find a pair that
  120. * intersect or we run out of extents.
  121. */
  122. while (sub_br->start + sub_br->len <= br->start) {
  123. if (list_is_last(&sub_br->list, &sub->list))
  124. goto out;
  125. sub_br = list_next_entry(sub_br, list);
  126. }
  127. if (sub_br->start >= br->start + br->len) {
  128. lp = lp->next;
  129. continue;
  130. }
  131. /* trim sub_br to fit the extent we have */
  132. sub_start = sub_br->start;
  133. sub_len = sub_br->len;
  134. if (sub_br->start < br->start) {
  135. sub_len -= br->start - sub_br->start;
  136. sub_start = br->start;
  137. }
  138. if (sub_len > br->len)
  139. sub_len = br->len;
  140. state = 0;
  141. if (sub_start == br->start)
  142. state |= LEFT_ALIGNED;
  143. if (sub_start + sub_len == br->start + br->len)
  144. state |= RIGHT_ALIGNED;
  145. switch (state) {
  146. case LEFT_ALIGNED:
  147. /* Coincides with only the left. */
  148. br->start += sub_len;
  149. br->len -= sub_len;
  150. break;
  151. case RIGHT_ALIGNED:
  152. /* Coincides with only the right. */
  153. br->len -= sub_len;
  154. lp = lp->next;
  155. break;
  156. case LEFT_ALIGNED | RIGHT_ALIGNED:
  157. /* Total overlap, just delete ex. */
  158. lp = lp->next;
  159. list_del(&br->list);
  160. kmem_free(br);
  161. break;
  162. case 0:
  163. /*
  164. * Deleting from the middle: add the new right extent
  165. * and then shrink the left extent.
  166. */
  167. new_br = kmem_alloc(sizeof(struct xbitmap_range),
  168. KM_MAYFAIL);
  169. if (!new_br) {
  170. error = -ENOMEM;
  171. goto out;
  172. }
  173. INIT_LIST_HEAD(&new_br->list);
  174. new_br->start = sub_start + sub_len;
  175. new_br->len = br->start + br->len - new_br->start;
  176. list_add(&new_br->list, &br->list);
  177. br->len = sub_start - br->start;
  178. lp = lp->next;
  179. break;
  180. default:
  181. ASSERT(0);
  182. break;
  183. }
  184. }
  185. out:
  186. return error;
  187. }
  188. #undef LEFT_ALIGNED
  189. #undef RIGHT_ALIGNED
  190. /*
  191. * Record all btree blocks seen while iterating all records of a btree.
  192. *
  193. * We know that the btree query_all function starts at the left edge and walks
  194. * towards the right edge of the tree. Therefore, we know that we can walk up
  195. * the btree cursor towards the root; if the pointer for a given level points
  196. * to the first record/key in that block, we haven't seen this block before;
  197. * and therefore we need to remember that we saw this block in the btree.
  198. *
  199. * So if our btree is:
  200. *
  201. * 4
  202. * / | \
  203. * 1 2 3
  204. *
  205. * Pretend for this example that each leaf block has 100 btree records. For
  206. * the first btree record, we'll observe that bc_levels[0].ptr == 1, so we
  207. * record that we saw block 1. Then we observe that bc_levels[1].ptr == 1, so
  208. * we record block 4. The list is [1, 4].
  209. *
  210. * For the second btree record, we see that bc_levels[0].ptr == 2, so we exit
  211. * the loop. The list remains [1, 4].
  212. *
  213. * For the 101st btree record, we've moved onto leaf block 2. Now
  214. * bc_levels[0].ptr == 1 again, so we record that we saw block 2. We see that
  215. * bc_levels[1].ptr == 2, so we exit the loop. The list is now [1, 4, 2].
  216. *
  217. * For the 102nd record, bc_levels[0].ptr == 2, so we continue.
  218. *
  219. * For the 201st record, we've moved on to leaf block 3.
  220. * bc_levels[0].ptr == 1, so we add 3 to the list. Now it is [1, 4, 2, 3].
  221. *
  222. * For the 300th record we just exit, with the list being [1, 4, 2, 3].
  223. */
  224. /*
  225. * Record all the buffers pointed to by the btree cursor. Callers already
  226. * engaged in a btree walk should call this function to capture the list of
  227. * blocks going from the leaf towards the root.
  228. */
  229. int
  230. xbitmap_set_btcur_path(
  231. struct xbitmap *bitmap,
  232. struct xfs_btree_cur *cur)
  233. {
  234. struct xfs_buf *bp;
  235. xfs_fsblock_t fsb;
  236. int i;
  237. int error;
  238. for (i = 0; i < cur->bc_nlevels && cur->bc_levels[i].ptr == 1; i++) {
  239. xfs_btree_get_block(cur, i, &bp);
  240. if (!bp)
  241. continue;
  242. fsb = XFS_DADDR_TO_FSB(cur->bc_mp, xfs_buf_daddr(bp));
  243. error = xbitmap_set(bitmap, fsb, 1);
  244. if (error)
  245. return error;
  246. }
  247. return 0;
  248. }
  249. /* Collect a btree's block in the bitmap. */
  250. STATIC int
  251. xbitmap_collect_btblock(
  252. struct xfs_btree_cur *cur,
  253. int level,
  254. void *priv)
  255. {
  256. struct xbitmap *bitmap = priv;
  257. struct xfs_buf *bp;
  258. xfs_fsblock_t fsbno;
  259. xfs_btree_get_block(cur, level, &bp);
  260. if (!bp)
  261. return 0;
  262. fsbno = XFS_DADDR_TO_FSB(cur->bc_mp, xfs_buf_daddr(bp));
  263. return xbitmap_set(bitmap, fsbno, 1);
  264. }
  265. /* Walk the btree and mark the bitmap wherever a btree block is found. */
  266. int
  267. xbitmap_set_btblocks(
  268. struct xbitmap *bitmap,
  269. struct xfs_btree_cur *cur)
  270. {
  271. return xfs_btree_visit_blocks(cur, xbitmap_collect_btblock,
  272. XFS_BTREE_VISIT_ALL, bitmap);
  273. }
  274. /* How many bits are set in this bitmap? */
  275. uint64_t
  276. xbitmap_hweight(
  277. struct xbitmap *bitmap)
  278. {
  279. struct xbitmap_range *bmr;
  280. struct xbitmap_range *n;
  281. uint64_t ret = 0;
  282. for_each_xbitmap_extent(bmr, n, bitmap)
  283. ret += bmr->len;
  284. return ret;
  285. }