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