chunk-allocation-tests.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2026 Meta. All rights reserved.
  4. */
  5. #include <linux/sizes.h>
  6. #include "btrfs-tests.h"
  7. #include "../volumes.h"
  8. #include "../disk-io.h"
  9. #include "../extent-io-tree.h"
  10. /*
  11. * Tests for chunk allocator pending extent internals.
  12. * These two functions form the core of searching the chunk allocation pending
  13. * extent bitmap and have relatively easily definable semantics, so unit
  14. * testing them can help ensure the correctness of chunk allocation.
  15. */
  16. /*
  17. * Describes the inputs to the system and expected results
  18. * when testing btrfs_find_hole_in_pending_extents().
  19. */
  20. struct pending_extent_test_case {
  21. const char *name;
  22. /* Input range to search. */
  23. u64 hole_start;
  24. u64 hole_len;
  25. /* The size of hole we are searching for. */
  26. u64 min_hole_size;
  27. /*
  28. * Pending extents to set up (up to 2 for up to 3 holes)
  29. * If len == 0, then it is skipped.
  30. */
  31. struct {
  32. u64 start;
  33. u64 len;
  34. } pending_extents[2];
  35. /* Expected outputs. */
  36. bool expected_found;
  37. u64 expected_start;
  38. u64 expected_len;
  39. };
  40. static const struct pending_extent_test_case find_hole_tests[] = {
  41. {
  42. .name = "no pending extents",
  43. .hole_start = 0,
  44. .hole_len = 10ULL * SZ_1G,
  45. .min_hole_size = SZ_1G,
  46. .pending_extents = { },
  47. .expected_found = true,
  48. .expected_start = 0,
  49. .expected_len = 10ULL * SZ_1G,
  50. },
  51. {
  52. .name = "pending extent at start of range",
  53. .hole_start = 0,
  54. .hole_len = 10ULL * SZ_1G,
  55. .min_hole_size = SZ_1G,
  56. .pending_extents = {
  57. { .start = 0, .len = SZ_1G },
  58. },
  59. .expected_found = true,
  60. .expected_start = SZ_1G,
  61. .expected_len = 9ULL * SZ_1G,
  62. },
  63. {
  64. .name = "pending extent overlapping start of range",
  65. .hole_start = SZ_1G,
  66. .hole_len = 9ULL * SZ_1G,
  67. .min_hole_size = SZ_1G,
  68. .pending_extents = {
  69. { .start = 0, .len = SZ_2G },
  70. },
  71. .expected_found = true,
  72. .expected_start = SZ_2G,
  73. .expected_len = 8ULL * SZ_1G,
  74. },
  75. {
  76. .name = "two holes; first hole is exactly big enough",
  77. .hole_start = 0,
  78. .hole_len = 10ULL * SZ_1G,
  79. .min_hole_size = SZ_1G,
  80. .pending_extents = {
  81. { .start = SZ_1G, .len = SZ_1G },
  82. },
  83. .expected_found = true,
  84. .expected_start = 0,
  85. .expected_len = SZ_1G,
  86. },
  87. {
  88. .name = "two holes; first hole is big enough",
  89. .hole_start = 0,
  90. .hole_len = 10ULL * SZ_1G,
  91. .min_hole_size = SZ_1G,
  92. .pending_extents = {
  93. { .start = SZ_2G, .len = SZ_1G },
  94. },
  95. .expected_found = true,
  96. .expected_start = 0,
  97. .expected_len = SZ_2G,
  98. },
  99. {
  100. .name = "two holes; second hole is big enough",
  101. .hole_start = 0,
  102. .hole_len = 10ULL * SZ_1G,
  103. .min_hole_size = SZ_2G,
  104. .pending_extents = {
  105. { .start = SZ_1G, .len = SZ_1G },
  106. },
  107. .expected_found = true,
  108. .expected_start = SZ_2G,
  109. .expected_len = 8ULL * SZ_1G,
  110. },
  111. {
  112. .name = "three holes; first hole big enough",
  113. .hole_start = 0,
  114. .hole_len = 10ULL * SZ_1G,
  115. .min_hole_size = SZ_2G,
  116. .pending_extents = {
  117. { .start = SZ_2G, .len = SZ_1G },
  118. { .start = 4ULL * SZ_1G, .len = SZ_1G },
  119. },
  120. .expected_found = true,
  121. .expected_start = 0,
  122. .expected_len = SZ_2G,
  123. },
  124. {
  125. .name = "three holes; second hole big enough",
  126. .hole_start = 0,
  127. .hole_len = 10ULL * SZ_1G,
  128. .min_hole_size = SZ_2G,
  129. .pending_extents = {
  130. { .start = SZ_1G, .len = SZ_1G },
  131. { .start = 5ULL * SZ_1G, .len = SZ_1G },
  132. },
  133. .expected_found = true,
  134. .expected_start = SZ_2G,
  135. .expected_len = 3ULL * SZ_1G,
  136. },
  137. {
  138. .name = "three holes; third hole big enough",
  139. .hole_start = 0,
  140. .hole_len = 10ULL * SZ_1G,
  141. .min_hole_size = SZ_2G,
  142. .pending_extents = {
  143. { .start = SZ_1G, .len = SZ_1G },
  144. { .start = 3ULL * SZ_1G, .len = 5ULL * SZ_1G },
  145. },
  146. .expected_found = true,
  147. .expected_start = 8ULL * SZ_1G,
  148. .expected_len = SZ_2G,
  149. },
  150. {
  151. .name = "three holes; all holes too small",
  152. .hole_start = 0,
  153. .hole_len = 10ULL * SZ_1G,
  154. .min_hole_size = SZ_2G,
  155. .pending_extents = {
  156. { .start = SZ_1G, .len = SZ_1G },
  157. { .start = 3ULL * SZ_1G, .len = 6ULL * SZ_1G },
  158. },
  159. .expected_found = false,
  160. .expected_start = 0,
  161. .expected_len = SZ_1G,
  162. },
  163. {
  164. .name = "three holes; all holes too small; first biggest",
  165. .hole_start = 0,
  166. .hole_len = 10ULL * SZ_1G,
  167. .min_hole_size = 3ULL * SZ_1G,
  168. .pending_extents = {
  169. { .start = SZ_2G, .len = SZ_1G },
  170. { .start = 4ULL * SZ_1G, .len = 5ULL * SZ_1G },
  171. },
  172. .expected_found = false,
  173. .expected_start = 0,
  174. .expected_len = SZ_2G,
  175. },
  176. {
  177. .name = "three holes; all holes too small; second biggest",
  178. .hole_start = 0,
  179. .hole_len = 10ULL * SZ_1G,
  180. .min_hole_size = 3ULL * SZ_1G,
  181. .pending_extents = {
  182. { .start = SZ_1G, .len = SZ_1G },
  183. { .start = 4ULL * SZ_1G, .len = 5ULL * SZ_1G },
  184. },
  185. .expected_found = false,
  186. .expected_start = SZ_2G,
  187. .expected_len = SZ_2G,
  188. },
  189. {
  190. .name = "three holes; all holes too small; third biggest",
  191. .hole_start = 0,
  192. .hole_len = 10ULL * SZ_1G,
  193. .min_hole_size = 3ULL * SZ_1G,
  194. .pending_extents = {
  195. { .start = SZ_1G, .len = SZ_1G },
  196. { .start = 3ULL * SZ_1G, .len = 5ULL * SZ_1G },
  197. },
  198. .expected_found = false,
  199. .expected_start = 8ULL * SZ_1G,
  200. .expected_len = SZ_2G,
  201. },
  202. {
  203. .name = "hole entirely allocated by pending",
  204. .hole_start = 0,
  205. .hole_len = 10ULL * SZ_1G,
  206. .min_hole_size = SZ_1G,
  207. .pending_extents = {
  208. { .start = 0, .len = 10ULL * SZ_1G },
  209. },
  210. .expected_found = false,
  211. .expected_start = 10ULL * SZ_1G,
  212. .expected_len = 0,
  213. },
  214. {
  215. .name = "pending extent at end of range",
  216. .hole_start = 0,
  217. .hole_len = 10ULL * SZ_1G,
  218. .min_hole_size = SZ_1G,
  219. .pending_extents = {
  220. { .start = 9ULL * SZ_1G, .len = SZ_2G },
  221. },
  222. .expected_found = true,
  223. .expected_start = 0,
  224. .expected_len = 9ULL * SZ_1G,
  225. },
  226. {
  227. .name = "zero length input",
  228. .hole_start = SZ_1G,
  229. .hole_len = 0,
  230. .min_hole_size = SZ_1G,
  231. .pending_extents = { },
  232. .expected_found = false,
  233. .expected_start = SZ_1G,
  234. .expected_len = 0,
  235. },
  236. };
  237. static int test_find_hole_in_pending(u32 sectorsize, u32 nodesize)
  238. {
  239. struct btrfs_fs_info *fs_info;
  240. struct btrfs_device *device;
  241. int ret = 0;
  242. test_msg("running find_hole_in_pending_extents tests");
  243. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  244. if (!fs_info) {
  245. test_std_err(TEST_ALLOC_FS_INFO);
  246. return -ENOMEM;
  247. }
  248. device = btrfs_alloc_dummy_device(fs_info);
  249. if (IS_ERR(device)) {
  250. test_err("failed to allocate dummy device");
  251. ret = PTR_ERR(device);
  252. goto out_free_fs_info;
  253. }
  254. device->fs_info = fs_info;
  255. for (int i = 0; i < ARRAY_SIZE(find_hole_tests); i++) {
  256. const struct pending_extent_test_case *test_case = &find_hole_tests[i];
  257. u64 hole_start = test_case->hole_start;
  258. u64 hole_len = test_case->hole_len;
  259. bool found;
  260. for (int j = 0; j < ARRAY_SIZE(test_case->pending_extents); j++) {
  261. u64 start = test_case->pending_extents[j].start;
  262. u64 len = test_case->pending_extents[j].len;
  263. if (!len)
  264. continue;
  265. btrfs_set_extent_bit(&device->alloc_state,
  266. start, start + len - 1,
  267. CHUNK_ALLOCATED, NULL);
  268. }
  269. mutex_lock(&fs_info->chunk_mutex);
  270. found = btrfs_find_hole_in_pending_extents(device, &hole_start, &hole_len,
  271. test_case->min_hole_size);
  272. mutex_unlock(&fs_info->chunk_mutex);
  273. if (found != test_case->expected_found) {
  274. test_err("%s: expected found=%d, got found=%d",
  275. test_case->name, test_case->expected_found, found);
  276. ret = -EINVAL;
  277. goto out_clear_pending_extents;
  278. }
  279. if (hole_start != test_case->expected_start ||
  280. hole_len != test_case->expected_len) {
  281. test_err("%s: expected [%llu, %llu), got [%llu, %llu)",
  282. test_case->name, test_case->expected_start,
  283. test_case->expected_start +
  284. test_case->expected_len,
  285. hole_start, hole_start + hole_len);
  286. ret = -EINVAL;
  287. goto out_clear_pending_extents;
  288. }
  289. out_clear_pending_extents:
  290. btrfs_clear_extent_bit(&device->alloc_state, 0, (u64)-1,
  291. CHUNK_ALLOCATED, NULL);
  292. if (ret)
  293. break;
  294. }
  295. out_free_fs_info:
  296. btrfs_free_dummy_fs_info(fs_info);
  297. return ret;
  298. }
  299. /*
  300. * Describes the inputs to the system and expected results
  301. * when testing btrfs_first_pending_extent().
  302. */
  303. struct first_pending_test_case {
  304. const char *name;
  305. /* The range to look for a pending extent in. */
  306. u64 hole_start;
  307. u64 hole_len;
  308. /* The pending extent to look for. */
  309. struct {
  310. u64 start;
  311. u64 len;
  312. } pending_extent;
  313. /* Expected outputs. */
  314. bool expected_found;
  315. u64 expected_pending_start;
  316. u64 expected_pending_end;
  317. };
  318. static const struct first_pending_test_case first_pending_tests[] = {
  319. {
  320. .name = "no pending extent",
  321. .hole_start = 0,
  322. .hole_len = 10ULL * SZ_1G,
  323. .pending_extent = { 0, 0 },
  324. .expected_found = false,
  325. },
  326. {
  327. .name = "pending extent at search start",
  328. .hole_start = SZ_1G,
  329. .hole_len = 9ULL * SZ_1G,
  330. .pending_extent = { SZ_1G, SZ_1G },
  331. .expected_found = true,
  332. .expected_pending_start = SZ_1G,
  333. .expected_pending_end = SZ_2G - 1,
  334. },
  335. {
  336. .name = "pending extent overlapping search start",
  337. .hole_start = SZ_1G,
  338. .hole_len = 9ULL * SZ_1G,
  339. .pending_extent = { 0, SZ_2G },
  340. .expected_found = true,
  341. .expected_pending_start = 0,
  342. .expected_pending_end = SZ_2G - 1,
  343. },
  344. {
  345. .name = "pending extent inside search range",
  346. .hole_start = 0,
  347. .hole_len = 10ULL * SZ_1G,
  348. .pending_extent = { SZ_2G, SZ_1G },
  349. .expected_found = true,
  350. .expected_pending_start = SZ_2G,
  351. .expected_pending_end = 3ULL * SZ_1G - 1,
  352. },
  353. {
  354. .name = "pending extent outside search range",
  355. .hole_start = 0,
  356. .hole_len = SZ_1G,
  357. .pending_extent = { SZ_2G, SZ_1G },
  358. .expected_found = false,
  359. },
  360. {
  361. .name = "pending extent overlapping end of search range",
  362. .hole_start = 0,
  363. .hole_len = SZ_2G,
  364. .pending_extent = { SZ_1G, SZ_2G },
  365. .expected_found = true,
  366. .expected_pending_start = SZ_1G,
  367. .expected_pending_end = 3ULL * SZ_1G - 1,
  368. },
  369. };
  370. static int test_first_pending_extent(u32 sectorsize, u32 nodesize)
  371. {
  372. struct btrfs_fs_info *fs_info;
  373. struct btrfs_device *device;
  374. int ret = 0;
  375. test_msg("running first_pending_extent tests");
  376. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  377. if (!fs_info) {
  378. test_std_err(TEST_ALLOC_FS_INFO);
  379. return -ENOMEM;
  380. }
  381. device = btrfs_alloc_dummy_device(fs_info);
  382. if (IS_ERR(device)) {
  383. test_err("failed to allocate dummy device");
  384. ret = PTR_ERR(device);
  385. goto out_free_fs_info;
  386. }
  387. device->fs_info = fs_info;
  388. for (int i = 0; i < ARRAY_SIZE(first_pending_tests); i++) {
  389. const struct first_pending_test_case *test_case = &first_pending_tests[i];
  390. u64 start = test_case->pending_extent.start;
  391. u64 len = test_case->pending_extent.len;
  392. u64 pending_start, pending_end;
  393. bool found;
  394. if (len) {
  395. btrfs_set_extent_bit(&device->alloc_state,
  396. start, start + len - 1,
  397. CHUNK_ALLOCATED, NULL);
  398. }
  399. mutex_lock(&fs_info->chunk_mutex);
  400. found = btrfs_first_pending_extent(device, test_case->hole_start,
  401. test_case->hole_len,
  402. &pending_start, &pending_end);
  403. mutex_unlock(&fs_info->chunk_mutex);
  404. if (found != test_case->expected_found) {
  405. test_err("%s: expected found=%d, got found=%d",
  406. test_case->name, test_case->expected_found, found);
  407. ret = -EINVAL;
  408. goto out_clear_pending_extents;
  409. }
  410. if (!found)
  411. goto out_clear_pending_extents;
  412. if (pending_start != test_case->expected_pending_start ||
  413. pending_end != test_case->expected_pending_end) {
  414. test_err("%s: expected pending [%llu, %llu], got [%llu, %llu]",
  415. test_case->name,
  416. test_case->expected_pending_start,
  417. test_case->expected_pending_end,
  418. pending_start, pending_end);
  419. ret = -EINVAL;
  420. goto out_clear_pending_extents;
  421. }
  422. out_clear_pending_extents:
  423. btrfs_clear_extent_bit(&device->alloc_state, 0, (u64)-1,
  424. CHUNK_ALLOCATED, NULL);
  425. if (ret)
  426. break;
  427. }
  428. out_free_fs_info:
  429. btrfs_free_dummy_fs_info(fs_info);
  430. return ret;
  431. }
  432. int btrfs_test_chunk_allocation(u32 sectorsize, u32 nodesize)
  433. {
  434. int ret;
  435. test_msg("running chunk allocation tests");
  436. ret = test_first_pending_extent(sectorsize, nodesize);
  437. if (ret)
  438. return ret;
  439. ret = test_find_hole_in_pending(sectorsize, nodesize);
  440. if (ret)
  441. return ret;
  442. return 0;
  443. }