hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/fs/btrfs/tests/extent-map-tests.c
....@@ -6,14 +6,17 @@
66 #include <linux/types.h>
77 #include "btrfs-tests.h"
88 #include "../ctree.h"
9
+#include "../volumes.h"
10
+#include "../disk-io.h"
11
+#include "../block-group.h"
912
1013 static void free_extent_map_tree(struct extent_map_tree *em_tree)
1114 {
1215 struct extent_map *em;
1316 struct rb_node *node;
1417
15
- while (!RB_EMPTY_ROOT(&em_tree->map)) {
16
- node = rb_first(&em_tree->map);
18
+ while (!RB_EMPTY_ROOT(&em_tree->map.rb_root)) {
19
+ node = rb_first_cached(&em_tree->map);
1720 em = rb_entry(node, struct extent_map, rb_node);
1821 remove_extent_mapping(em_tree, em);
1922
....@@ -47,7 +50,7 @@
4750 * ->add_extent_mapping(0, 16K)
4851 * -> #handle -EEXIST
4952 */
50
-static void test_case_1(struct btrfs_fs_info *fs_info,
53
+static int test_case_1(struct btrfs_fs_info *fs_info,
5154 struct extent_map_tree *em_tree)
5255 {
5356 struct extent_map *em;
....@@ -56,55 +59,79 @@
5659 int ret;
5760
5861 em = alloc_extent_map();
59
- if (!em)
60
- /* Skip the test on error. */
61
- return;
62
+ if (!em) {
63
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
64
+ return -ENOMEM;
65
+ }
6266
6367 /* Add [0, 16K) */
6468 em->start = 0;
6569 em->len = SZ_16K;
6670 em->block_start = 0;
6771 em->block_len = SZ_16K;
72
+ write_lock(&em_tree->lock);
6873 ret = add_extent_mapping(em_tree, em, 0);
69
- ASSERT(ret == 0);
74
+ write_unlock(&em_tree->lock);
75
+ if (ret < 0) {
76
+ test_err("cannot add extent range [0, 16K)");
77
+ goto out;
78
+ }
7079 free_extent_map(em);
7180
7281 /* Add [16K, 20K) following [0, 16K) */
7382 em = alloc_extent_map();
74
- if (!em)
83
+ if (!em) {
84
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
85
+ ret = -ENOMEM;
7586 goto out;
87
+ }
7688
7789 em->start = SZ_16K;
7890 em->len = SZ_4K;
7991 em->block_start = SZ_32K; /* avoid merging */
8092 em->block_len = SZ_4K;
93
+ write_lock(&em_tree->lock);
8194 ret = add_extent_mapping(em_tree, em, 0);
82
- ASSERT(ret == 0);
95
+ write_unlock(&em_tree->lock);
96
+ if (ret < 0) {
97
+ test_err("cannot add extent range [16K, 20K)");
98
+ goto out;
99
+ }
83100 free_extent_map(em);
84101
85102 em = alloc_extent_map();
86
- if (!em)
103
+ if (!em) {
104
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
105
+ ret = -ENOMEM;
87106 goto out;
107
+ }
88108
89109 /* Add [0, 8K), should return [0, 16K) instead. */
90110 em->start = start;
91111 em->len = len;
92112 em->block_start = start;
93113 em->block_len = len;
114
+ write_lock(&em_tree->lock);
94115 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
95
- if (ret)
116
+ write_unlock(&em_tree->lock);
117
+ if (ret) {
96118 test_err("case1 [%llu %llu]: ret %d", start, start + len, ret);
119
+ goto out;
120
+ }
97121 if (em &&
98122 (em->start != 0 || extent_map_end(em) != SZ_16K ||
99
- em->block_start != 0 || em->block_len != SZ_16K))
123
+ em->block_start != 0 || em->block_len != SZ_16K)) {
100124 test_err(
101125 "case1 [%llu %llu]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
102126 start, start + len, ret, em->start, em->len,
103127 em->block_start, em->block_len);
128
+ ret = -EINVAL;
129
+ }
104130 free_extent_map(em);
105131 out:
106
- /* free memory */
107132 free_extent_map_tree(em_tree);
133
+
134
+ return ret;
108135 }
109136
110137 /*
....@@ -113,65 +140,89 @@
113140 * Reading the inline ending up with EEXIST, ie. read an inline
114141 * extent and discard page cache and read it again.
115142 */
116
-static void test_case_2(struct btrfs_fs_info *fs_info,
143
+static int test_case_2(struct btrfs_fs_info *fs_info,
117144 struct extent_map_tree *em_tree)
118145 {
119146 struct extent_map *em;
120147 int ret;
121148
122149 em = alloc_extent_map();
123
- if (!em)
124
- /* Skip the test on error. */
125
- return;
150
+ if (!em) {
151
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
152
+ return -ENOMEM;
153
+ }
126154
127155 /* Add [0, 1K) */
128156 em->start = 0;
129157 em->len = SZ_1K;
130158 em->block_start = EXTENT_MAP_INLINE;
131159 em->block_len = (u64)-1;
160
+ write_lock(&em_tree->lock);
132161 ret = add_extent_mapping(em_tree, em, 0);
133
- ASSERT(ret == 0);
162
+ write_unlock(&em_tree->lock);
163
+ if (ret < 0) {
164
+ test_err("cannot add extent range [0, 1K)");
165
+ goto out;
166
+ }
134167 free_extent_map(em);
135168
136
- /* Add [4K, 4K) following [0, 1K) */
169
+ /* Add [4K, 8K) following [0, 1K) */
137170 em = alloc_extent_map();
138
- if (!em)
171
+ if (!em) {
172
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
173
+ ret = -ENOMEM;
139174 goto out;
175
+ }
140176
141177 em->start = SZ_4K;
142178 em->len = SZ_4K;
143179 em->block_start = SZ_4K;
144180 em->block_len = SZ_4K;
181
+ write_lock(&em_tree->lock);
145182 ret = add_extent_mapping(em_tree, em, 0);
146
- ASSERT(ret == 0);
183
+ write_unlock(&em_tree->lock);
184
+ if (ret < 0) {
185
+ test_err("cannot add extent range [4K, 8K)");
186
+ goto out;
187
+ }
147188 free_extent_map(em);
148189
149190 em = alloc_extent_map();
150
- if (!em)
191
+ if (!em) {
192
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
193
+ ret = -ENOMEM;
151194 goto out;
195
+ }
152196
153197 /* Add [0, 1K) */
154198 em->start = 0;
155199 em->len = SZ_1K;
156200 em->block_start = EXTENT_MAP_INLINE;
157201 em->block_len = (u64)-1;
202
+ write_lock(&em_tree->lock);
158203 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, em->start, em->len);
159
- if (ret)
204
+ write_unlock(&em_tree->lock);
205
+ if (ret) {
160206 test_err("case2 [0 1K]: ret %d", ret);
207
+ goto out;
208
+ }
161209 if (em &&
162210 (em->start != 0 || extent_map_end(em) != SZ_1K ||
163
- em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1))
211
+ em->block_start != EXTENT_MAP_INLINE || em->block_len != (u64)-1)) {
164212 test_err(
165213 "case2 [0 1K]: ret %d return a wrong em (start %llu len %llu block_start %llu block_len %llu",
166214 ret, em->start, em->len, em->block_start,
167215 em->block_len);
216
+ ret = -EINVAL;
217
+ }
168218 free_extent_map(em);
169219 out:
170
- /* free memory */
171220 free_extent_map_tree(em_tree);
221
+
222
+ return ret;
172223 }
173224
174
-static void __test_case_3(struct btrfs_fs_info *fs_info,
225
+static int __test_case_3(struct btrfs_fs_info *fs_info,
175226 struct extent_map_tree *em_tree, u64 start)
176227 {
177228 struct extent_map *em;
....@@ -179,47 +230,63 @@
179230 int ret;
180231
181232 em = alloc_extent_map();
182
- if (!em)
183
- /* Skip this test on error. */
184
- return;
233
+ if (!em) {
234
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
235
+ return -ENOMEM;
236
+ }
185237
186238 /* Add [4K, 8K) */
187239 em->start = SZ_4K;
188240 em->len = SZ_4K;
189241 em->block_start = SZ_4K;
190242 em->block_len = SZ_4K;
243
+ write_lock(&em_tree->lock);
191244 ret = add_extent_mapping(em_tree, em, 0);
192
- ASSERT(ret == 0);
245
+ write_unlock(&em_tree->lock);
246
+ if (ret < 0) {
247
+ test_err("cannot add extent range [4K, 8K)");
248
+ goto out;
249
+ }
193250 free_extent_map(em);
194251
195252 em = alloc_extent_map();
196
- if (!em)
253
+ if (!em) {
254
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
255
+ ret = -ENOMEM;
197256 goto out;
257
+ }
198258
199259 /* Add [0, 16K) */
200260 em->start = 0;
201261 em->len = SZ_16K;
202262 em->block_start = 0;
203263 em->block_len = SZ_16K;
264
+ write_lock(&em_tree->lock);
204265 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
205
- if (ret)
266
+ write_unlock(&em_tree->lock);
267
+ if (ret) {
206268 test_err("case3 [0x%llx 0x%llx): ret %d",
207269 start, start + len, ret);
270
+ goto out;
271
+ }
208272 /*
209273 * Since bytes within em are contiguous, em->block_start is identical to
210274 * em->start.
211275 */
212276 if (em &&
213277 (start < em->start || start + len > extent_map_end(em) ||
214
- em->start != em->block_start || em->len != em->block_len))
278
+ em->start != em->block_start || em->len != em->block_len)) {
215279 test_err(
216280 "case3 [0x%llx 0x%llx): ret %d em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
217281 start, start + len, ret, em->start, em->len,
218282 em->block_start, em->block_len);
283
+ ret = -EINVAL;
284
+ }
219285 free_extent_map(em);
220286 out:
221
- /* free memory */
222287 free_extent_map_tree(em_tree);
288
+
289
+ return ret;
223290 }
224291
225292 /*
....@@ -238,15 +305,23 @@
238305 * -> add_extent_mapping()
239306 * -> add_extent_mapping()
240307 */
241
-static void test_case_3(struct btrfs_fs_info *fs_info,
308
+static int test_case_3(struct btrfs_fs_info *fs_info,
242309 struct extent_map_tree *em_tree)
243310 {
244
- __test_case_3(fs_info, em_tree, 0);
245
- __test_case_3(fs_info, em_tree, SZ_8K);
246
- __test_case_3(fs_info, em_tree, (12 * 1024ULL));
311
+ int ret;
312
+
313
+ ret = __test_case_3(fs_info, em_tree, 0);
314
+ if (ret)
315
+ return ret;
316
+ ret = __test_case_3(fs_info, em_tree, SZ_8K);
317
+ if (ret)
318
+ return ret;
319
+ ret = __test_case_3(fs_info, em_tree, (12 * SZ_1K));
320
+
321
+ return ret;
247322 }
248323
249
-static void __test_case_4(struct btrfs_fs_info *fs_info,
324
+static int __test_case_4(struct btrfs_fs_info *fs_info,
250325 struct extent_map_tree *em_tree, u64 start)
251326 {
252327 struct extent_map *em;
....@@ -254,54 +329,77 @@
254329 int ret;
255330
256331 em = alloc_extent_map();
257
- if (!em)
258
- /* Skip this test on error. */
259
- return;
332
+ if (!em) {
333
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
334
+ return -ENOMEM;
335
+ }
260336
261337 /* Add [0K, 8K) */
262338 em->start = 0;
263339 em->len = SZ_8K;
264340 em->block_start = 0;
265341 em->block_len = SZ_8K;
342
+ write_lock(&em_tree->lock);
266343 ret = add_extent_mapping(em_tree, em, 0);
267
- ASSERT(ret == 0);
344
+ write_unlock(&em_tree->lock);
345
+ if (ret < 0) {
346
+ test_err("cannot add extent range [0, 8K)");
347
+ goto out;
348
+ }
268349 free_extent_map(em);
269350
270351 em = alloc_extent_map();
271
- if (!em)
352
+ if (!em) {
353
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
354
+ ret = -ENOMEM;
272355 goto out;
356
+ }
273357
274
- /* Add [8K, 24K) */
358
+ /* Add [8K, 32K) */
275359 em->start = SZ_8K;
276
- em->len = 24 * 1024ULL;
360
+ em->len = 24 * SZ_1K;
277361 em->block_start = SZ_16K; /* avoid merging */
278
- em->block_len = 24 * 1024ULL;
362
+ em->block_len = 24 * SZ_1K;
363
+ write_lock(&em_tree->lock);
279364 ret = add_extent_mapping(em_tree, em, 0);
280
- ASSERT(ret == 0);
365
+ write_unlock(&em_tree->lock);
366
+ if (ret < 0) {
367
+ test_err("cannot add extent range [8K, 32K)");
368
+ goto out;
369
+ }
281370 free_extent_map(em);
282371
283372 em = alloc_extent_map();
284
- if (!em)
373
+ if (!em) {
374
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
375
+ ret = -ENOMEM;
285376 goto out;
377
+ }
286378 /* Add [0K, 32K) */
287379 em->start = 0;
288380 em->len = SZ_32K;
289381 em->block_start = 0;
290382 em->block_len = SZ_32K;
383
+ write_lock(&em_tree->lock);
291384 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
292
- if (ret)
385
+ write_unlock(&em_tree->lock);
386
+ if (ret) {
293387 test_err("case4 [0x%llx 0x%llx): ret %d",
294388 start, len, ret);
295
- if (em &&
296
- (start < em->start || start + len > extent_map_end(em)))
389
+ goto out;
390
+ }
391
+ if (em && (start < em->start || start + len > extent_map_end(em))) {
297392 test_err(
298393 "case4 [0x%llx 0x%llx): ret %d, added wrong em (start 0x%llx len 0x%llx block_start 0x%llx block_len 0x%llx)",
299394 start, len, ret, em->start, em->len, em->block_start,
300395 em->block_len);
396
+ ret = -EINVAL;
397
+ }
301398 free_extent_map(em);
302399 out:
303
- /* free memory */
304400 free_extent_map_tree(em_tree);
401
+
402
+ return ret;
305403 }
306404
307405 /*
....@@ -329,17 +427,166 @@
329427 * # handle -EEXIST when adding
330428 * # [0, 32K)
331429 */
332
-static void test_case_4(struct btrfs_fs_info *fs_info,
430
+static int test_case_4(struct btrfs_fs_info *fs_info,
333431 struct extent_map_tree *em_tree)
334432 {
335
- __test_case_4(fs_info, em_tree, 0);
336
- __test_case_4(fs_info, em_tree, SZ_4K);
433
+ int ret;
434
+
435
+ ret = __test_case_4(fs_info, em_tree, 0);
436
+ if (ret)
437
+ return ret;
438
+ ret = __test_case_4(fs_info, em_tree, SZ_4K);
439
+
440
+ return ret;
441
+}
442
+
443
+struct rmap_test_vector {
444
+ u64 raid_type;
445
+ u64 physical_start;
446
+ u64 data_stripe_size;
447
+ u64 num_data_stripes;
448
+ u64 num_stripes;
449
+ /* Assume we won't have more than 5 physical stripes */
450
+ u64 data_stripe_phys_start[5];
451
+ bool expected_mapped_addr;
452
+ /* Physical to logical addresses */
453
+ u64 mapped_logical[5];
454
+};
455
+
456
+static int test_rmap_block(struct btrfs_fs_info *fs_info,
457
+ struct rmap_test_vector *test)
458
+{
459
+ struct extent_map *em;
460
+ struct map_lookup *map = NULL;
461
+ u64 *logical = NULL;
462
+ int i, out_ndaddrs, out_stripe_len;
463
+ int ret;
464
+
465
+ em = alloc_extent_map();
466
+ if (!em) {
467
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
468
+ return -ENOMEM;
469
+ }
470
+
471
+ map = kmalloc(map_lookup_size(test->num_stripes), GFP_KERNEL);
472
+ if (!map) {
473
+ kfree(em);
474
+ test_std_err(TEST_ALLOC_EXTENT_MAP);
475
+ return -ENOMEM;
476
+ }
477
+
478
+ set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
479
+ /* Start at 4GiB logical address */
480
+ em->start = SZ_4G;
481
+ em->len = test->data_stripe_size * test->num_data_stripes;
482
+ em->block_len = em->len;
483
+ em->orig_block_len = test->data_stripe_size;
484
+ em->map_lookup = map;
485
+
486
+ map->num_stripes = test->num_stripes;
487
+ map->stripe_len = BTRFS_STRIPE_LEN;
488
+ map->type = test->raid_type;
489
+
490
+ for (i = 0; i < map->num_stripes; i++) {
491
+ struct btrfs_device *dev = btrfs_alloc_dummy_device(fs_info);
492
+
493
+ if (IS_ERR(dev)) {
494
+ test_err("cannot allocate device");
495
+ ret = PTR_ERR(dev);
496
+ goto out;
497
+ }
498
+ map->stripes[i].dev = dev;
499
+ map->stripes[i].physical = test->data_stripe_phys_start[i];
500
+ }
501
+
502
+ write_lock(&fs_info->mapping_tree.lock);
503
+ ret = add_extent_mapping(&fs_info->mapping_tree, em, 0);
504
+ write_unlock(&fs_info->mapping_tree.lock);
505
+ if (ret) {
506
+ test_err("error adding block group mapping to mapping tree");
507
+ goto out_free;
508
+ }
509
+
510
+ ret = btrfs_rmap_block(fs_info, em->start, btrfs_sb_offset(1),
511
+ &logical, &out_ndaddrs, &out_stripe_len);
512
+ if (ret || (out_ndaddrs == 0 && test->expected_mapped_addr)) {
513
+ test_err("didn't rmap anything but expected %d",
514
+ test->expected_mapped_addr);
515
+ goto out;
516
+ }
517
+
518
+ if (out_stripe_len != BTRFS_STRIPE_LEN) {
519
+ test_err("calculated stripe length doesn't match");
520
+ goto out;
521
+ }
522
+
523
+ if (out_ndaddrs != test->expected_mapped_addr) {
524
+ for (i = 0; i < out_ndaddrs; i++)
525
+ test_msg("mapped %llu", logical[i]);
526
+ test_err("unexpected number of mapped addresses: %d", out_ndaddrs);
527
+ goto out;
528
+ }
529
+
530
+ for (i = 0; i < out_ndaddrs; i++) {
531
+ if (logical[i] != test->mapped_logical[i]) {
532
+ test_err("unexpected logical address mapped");
533
+ goto out;
534
+ }
535
+ }
536
+
537
+ ret = 0;
538
+out:
539
+ write_lock(&fs_info->mapping_tree.lock);
540
+ remove_extent_mapping(&fs_info->mapping_tree, em);
541
+ write_unlock(&fs_info->mapping_tree.lock);
542
+ /* For us */
543
+ free_extent_map(em);
544
+out_free:
545
+ /* For the tree */
546
+ free_extent_map(em);
547
+ kfree(logical);
548
+ return ret;
337549 }
338550
339551 int btrfs_test_extent_map(void)
340552 {
341553 struct btrfs_fs_info *fs_info = NULL;
342554 struct extent_map_tree *em_tree;
555
+ int ret = 0, i;
556
+ struct rmap_test_vector rmap_tests[] = {
557
+ {
558
+ /*
559
+ * Test a chunk with 2 data stripes one of which
560
+ * interesects the physical address of the super block
561
+ * is correctly recognised.
562
+ */
563
+ .raid_type = BTRFS_BLOCK_GROUP_RAID1,
564
+ .physical_start = SZ_64M - SZ_4M,
565
+ .data_stripe_size = SZ_256M,
566
+ .num_data_stripes = 2,
567
+ .num_stripes = 2,
568
+ .data_stripe_phys_start =
569
+ {SZ_64M - SZ_4M, SZ_64M - SZ_4M + SZ_256M},
570
+ .expected_mapped_addr = true,
571
+ .mapped_logical= {SZ_4G + SZ_4M}
572
+ },
573
+ {
574
+ /*
575
+ * Test that out-of-range physical addresses are
576
+ * ignored
577
+ */
578
+
579
+ /* SINGLE chunk type */
580
+ .raid_type = 0,
581
+ .physical_start = SZ_4G,
582
+ .data_stripe_size = SZ_256M,
583
+ .num_data_stripes = 1,
584
+ .num_stripes = 1,
585
+ .data_stripe_phys_start = {SZ_256M},
586
+ .expected_mapped_addr = false,
587
+ .mapped_logical = {0}
588
+ }
589
+ };
343590
344591 test_msg("running extent_map tests");
345592
....@@ -349,25 +596,39 @@
349596 */
350597 fs_info = btrfs_alloc_dummy_fs_info(PAGE_SIZE, PAGE_SIZE);
351598 if (!fs_info) {
352
- test_msg("Couldn't allocate dummy fs info");
599
+ test_std_err(TEST_ALLOC_FS_INFO);
353600 return -ENOMEM;
354601 }
355602
356603 em_tree = kzalloc(sizeof(*em_tree), GFP_KERNEL);
357
- if (!em_tree)
358
- /* Skip the test on error. */
604
+ if (!em_tree) {
605
+ ret = -ENOMEM;
359606 goto out;
607
+ }
360608
361609 extent_map_tree_init(em_tree);
362610
363
- test_case_1(fs_info, em_tree);
364
- test_case_2(fs_info, em_tree);
365
- test_case_3(fs_info, em_tree);
366
- test_case_4(fs_info, em_tree);
611
+ ret = test_case_1(fs_info, em_tree);
612
+ if (ret)
613
+ goto out;
614
+ ret = test_case_2(fs_info, em_tree);
615
+ if (ret)
616
+ goto out;
617
+ ret = test_case_3(fs_info, em_tree);
618
+ if (ret)
619
+ goto out;
620
+ ret = test_case_4(fs_info, em_tree);
367621
368
- kfree(em_tree);
622
+ test_msg("running rmap tests");
623
+ for (i = 0; i < ARRAY_SIZE(rmap_tests); i++) {
624
+ ret = test_rmap_block(fs_info, &rmap_tests[i]);
625
+ if (ret)
626
+ goto out;
627
+ }
628
+
369629 out:
630
+ kfree(em_tree);
370631 btrfs_free_dummy_fs_info(fs_info);
371632
372
- return 0;
633
+ return ret;
373634 }