blob: cc287d4f46d2a7d02d8ed05b8b953460d79dea7f [file] [log] [blame]
Josef Bacikdc11dd52013-08-14 15:05:12 -04001/*
2 * Copyright (C) 2013 Fusion IO. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19#include <linux/slab.h>
20#include "btrfs-tests.h"
21#include "../ctree.h"
Josef Bacikd0bd4562015-09-23 14:54:14 -040022#include "../disk-io.h"
Josef Bacikdc11dd52013-08-14 15:05:12 -040023#include "../free-space-cache.h"
24
25#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
26static struct btrfs_block_group_cache *init_test_block_group(void)
27{
28 struct btrfs_block_group_cache *cache;
29
30 cache = kzalloc(sizeof(*cache), GFP_NOFS);
31 if (!cache)
32 return NULL;
33 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
34 GFP_NOFS);
35 if (!cache->free_space_ctl) {
36 kfree(cache);
37 return NULL;
38 }
Josef Bacikd0bd4562015-09-23 14:54:14 -040039 cache->fs_info = btrfs_alloc_dummy_fs_info();
40 if (!cache->fs_info) {
41 kfree(cache->free_space_ctl);
42 kfree(cache);
43 return NULL;
44 }
Josef Bacikdc11dd52013-08-14 15:05:12 -040045
46 cache->key.objectid = 0;
Byongho Leeee221842015-12-15 01:42:10 +090047 cache->key.offset = SZ_1G;
Josef Bacikdc11dd52013-08-14 15:05:12 -040048 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
49 cache->sectorsize = 4096;
Filipe Manana20005522014-08-29 13:35:13 +010050 cache->full_stripe_len = 4096;
Josef Bacikdc11dd52013-08-14 15:05:12 -040051
52 spin_lock_init(&cache->lock);
53 INIT_LIST_HEAD(&cache->list);
54 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik47ab2a62014-09-18 11:20:02 -040055 INIT_LIST_HEAD(&cache->bg_list);
Josef Bacikdc11dd52013-08-14 15:05:12 -040056
57 btrfs_init_free_space_ctl(cache);
58
59 return cache;
60}
61
62/*
63 * This test just does basic sanity checking, making sure we can add an exten
64 * entry and remove space from either end and the middle, and make sure we can
65 * remove space that covers adjacent extent entries.
66 */
67static int test_extents(struct btrfs_block_group_cache *cache)
68{
69 int ret = 0;
70
71 test_msg("Running extent only tests\n");
72
73 /* First just make sure we can remove an entire entry */
Byongho Leeee221842015-12-15 01:42:10 +090074 ret = btrfs_add_free_space(cache, 0, SZ_4M);
Josef Bacikdc11dd52013-08-14 15:05:12 -040075 if (ret) {
76 test_msg("Error adding initial extents %d\n", ret);
77 return ret;
78 }
79
Byongho Leeee221842015-12-15 01:42:10 +090080 ret = btrfs_remove_free_space(cache, 0, SZ_4M);
Josef Bacikdc11dd52013-08-14 15:05:12 -040081 if (ret) {
82 test_msg("Error removing extent %d\n", ret);
83 return ret;
84 }
85
Byongho Leeee221842015-12-15 01:42:10 +090086 if (test_check_exists(cache, 0, SZ_4M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -040087 test_msg("Full remove left some lingering space\n");
88 return -1;
89 }
90
91 /* Ok edge and middle cases now */
Byongho Leeee221842015-12-15 01:42:10 +090092 ret = btrfs_add_free_space(cache, 0, SZ_4M);
Josef Bacikdc11dd52013-08-14 15:05:12 -040093 if (ret) {
94 test_msg("Error adding half extent %d\n", ret);
95 return ret;
96 }
97
Byongho Leeee221842015-12-15 01:42:10 +090098 ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -040099 if (ret) {
100 test_msg("Error removing tail end %d\n", ret);
101 return ret;
102 }
103
Byongho Leeee221842015-12-15 01:42:10 +0900104 ret = btrfs_remove_free_space(cache, 0, SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400105 if (ret) {
106 test_msg("Error removing front end %d\n", ret);
107 return ret;
108 }
109
Byongho Leeee221842015-12-15 01:42:10 +0900110 ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400111 if (ret) {
Masanari Iida77d84ff2013-12-09 00:22:53 +0900112 test_msg("Error removing middle piece %d\n", ret);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400113 return ret;
114 }
115
Byongho Leeee221842015-12-15 01:42:10 +0900116 if (test_check_exists(cache, 0, SZ_1M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400117 test_msg("Still have space at the front\n");
118 return -1;
119 }
120
Byongho Leeee221842015-12-15 01:42:10 +0900121 if (test_check_exists(cache, SZ_2M, 4096)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400122 test_msg("Still have space in the middle\n");
123 return -1;
124 }
125
Byongho Leeee221842015-12-15 01:42:10 +0900126 if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400127 test_msg("Still have space at the end\n");
128 return -1;
129 }
130
131 /* Cleanup */
132 __btrfs_remove_free_space_cache(cache->free_space_ctl);
133
134 return 0;
135}
136
137static int test_bitmaps(struct btrfs_block_group_cache *cache)
138{
139 u64 next_bitmap_offset;
140 int ret;
141
142 test_msg("Running bitmap only tests\n");
143
Byongho Leeee221842015-12-15 01:42:10 +0900144 ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400145 if (ret) {
146 test_msg("Couldn't create a bitmap entry %d\n", ret);
147 return ret;
148 }
149
Byongho Leeee221842015-12-15 01:42:10 +0900150 ret = btrfs_remove_free_space(cache, 0, SZ_4M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400151 if (ret) {
152 test_msg("Error removing bitmap full range %d\n", ret);
153 return ret;
154 }
155
Byongho Leeee221842015-12-15 01:42:10 +0900156 if (test_check_exists(cache, 0, SZ_4M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400157 test_msg("Left some space in bitmap\n");
158 return -1;
159 }
160
Byongho Leeee221842015-12-15 01:42:10 +0900161 ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400162 if (ret) {
163 test_msg("Couldn't add to our bitmap entry %d\n", ret);
164 return ret;
165 }
166
Byongho Leeee221842015-12-15 01:42:10 +0900167 ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400168 if (ret) {
169 test_msg("Couldn't remove middle chunk %d\n", ret);
170 return ret;
171 }
172
173 /*
174 * The first bitmap we have starts at offset 0 so the next one is just
175 * at the end of the first bitmap.
176 */
177 next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
178
179 /* Test a bit straddling two bitmaps */
Byongho Leeee221842015-12-15 01:42:10 +0900180 ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
181 SZ_4M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400182 if (ret) {
183 test_msg("Couldn't add space that straddles two bitmaps %d\n",
184 ret);
185 return ret;
186 }
187
Byongho Leeee221842015-12-15 01:42:10 +0900188 ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400189 if (ret) {
190 test_msg("Couldn't remove overlapping space %d\n", ret);
191 return ret;
192 }
193
Byongho Leeee221842015-12-15 01:42:10 +0900194 if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400195 test_msg("Left some space when removing overlapping\n");
196 return -1;
197 }
198
199 __btrfs_remove_free_space_cache(cache->free_space_ctl);
200
201 return 0;
202}
203
204/* This is the high grade jackassery */
205static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
206{
207 u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
208 int ret;
209
210 test_msg("Running bitmap and extent tests\n");
211
212 /*
213 * First let's do something simple, an extent at the same offset as the
214 * bitmap, but the free space completely in the extent and then
215 * completely in the bitmap.
216 */
Byongho Leeee221842015-12-15 01:42:10 +0900217 ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400218 if (ret) {
219 test_msg("Couldn't create bitmap entry %d\n", ret);
220 return ret;
221 }
222
Byongho Leeee221842015-12-15 01:42:10 +0900223 ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400224 if (ret) {
225 test_msg("Couldn't add extent entry %d\n", ret);
226 return ret;
227 }
228
Byongho Leeee221842015-12-15 01:42:10 +0900229 ret = btrfs_remove_free_space(cache, 0, SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400230 if (ret) {
231 test_msg("Couldn't remove extent entry %d\n", ret);
232 return ret;
233 }
234
Byongho Leeee221842015-12-15 01:42:10 +0900235 if (test_check_exists(cache, 0, SZ_1M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400236 test_msg("Left remnants after our remove\n");
237 return -1;
238 }
239
240 /* Now to add back the extent entry and remove from the bitmap */
Byongho Leeee221842015-12-15 01:42:10 +0900241 ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400242 if (ret) {
243 test_msg("Couldn't re-add extent entry %d\n", ret);
244 return ret;
245 }
246
Byongho Leeee221842015-12-15 01:42:10 +0900247 ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400248 if (ret) {
249 test_msg("Couldn't remove from bitmap %d\n", ret);
250 return ret;
251 }
252
Byongho Leeee221842015-12-15 01:42:10 +0900253 if (test_check_exists(cache, SZ_4M, SZ_1M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400254 test_msg("Left remnants in the bitmap\n");
255 return -1;
256 }
257
258 /*
259 * Ok so a little more evil, extent entry and bitmap at the same offset,
260 * removing an overlapping chunk.
261 */
Byongho Leeee221842015-12-15 01:42:10 +0900262 ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400263 if (ret) {
264 test_msg("Couldn't add to a bitmap %d\n", ret);
265 return ret;
266 }
267
Byongho Leeee221842015-12-15 01:42:10 +0900268 ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400269 if (ret) {
270 test_msg("Couldn't remove overlapping space %d\n", ret);
271 return ret;
272 }
273
Byongho Leeee221842015-12-15 01:42:10 +0900274 if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
Masanari Iida8faaaea2014-01-07 21:58:06 +0900275 test_msg("Left over pieces after removing overlapping\n");
Josef Bacikdc11dd52013-08-14 15:05:12 -0400276 return -1;
277 }
278
279 __btrfs_remove_free_space_cache(cache->free_space_ctl);
280
281 /* Now with the extent entry offset into the bitmap */
Byongho Leeee221842015-12-15 01:42:10 +0900282 ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400283 if (ret) {
284 test_msg("Couldn't add space to the bitmap %d\n", ret);
285 return ret;
286 }
287
Byongho Leeee221842015-12-15 01:42:10 +0900288 ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400289 if (ret) {
290 test_msg("Couldn't add extent to the cache %d\n", ret);
291 return ret;
292 }
293
Byongho Leeee221842015-12-15 01:42:10 +0900294 ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400295 if (ret) {
296 test_msg("Problem removing overlapping space %d\n", ret);
297 return ret;
298 }
299
Byongho Leeee221842015-12-15 01:42:10 +0900300 if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400301 test_msg("Left something behind when removing space");
302 return -1;
303 }
304
305 /*
306 * This has blown up in the past, the extent entry starts before the
307 * bitmap entry, but we're trying to remove an offset that falls
308 * completely within the bitmap range and is in both the extent entry
309 * and the bitmap entry, looks like this
310 *
311 * [ extent ]
312 * [ bitmap ]
313 * [ del ]
314 */
315 __btrfs_remove_free_space_cache(cache->free_space_ctl);
Byongho Leeee221842015-12-15 01:42:10 +0900316 ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400317 if (ret) {
318 test_msg("Couldn't add bitmap %d\n", ret);
319 return ret;
320 }
321
Byongho Leeee221842015-12-15 01:42:10 +0900322 ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
323 5 * SZ_1M, 0);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400324 if (ret) {
325 test_msg("Couldn't add extent entry %d\n", ret);
326 return ret;
327 }
328
Byongho Leeee221842015-12-15 01:42:10 +0900329 ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400330 if (ret) {
331 test_msg("Failed to free our space %d\n", ret);
332 return ret;
333 }
334
Byongho Leeee221842015-12-15 01:42:10 +0900335 if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
Josef Bacikdc11dd52013-08-14 15:05:12 -0400336 test_msg("Left stuff over\n");
337 return -1;
338 }
339
340 __btrfs_remove_free_space_cache(cache->free_space_ctl);
341
342 /*
343 * This blew up before, we have part of the free space in a bitmap and
344 * then the entirety of the rest of the space in an extent. This used
345 * to return -EAGAIN back from btrfs_remove_extent, make sure this
346 * doesn't happen.
347 */
Byongho Leeee221842015-12-15 01:42:10 +0900348 ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400349 if (ret) {
350 test_msg("Couldn't add bitmap entry %d\n", ret);
351 return ret;
352 }
353
Byongho Leeee221842015-12-15 01:42:10 +0900354 ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400355 if (ret) {
356 test_msg("Couldn't add extent entry %d\n", ret);
357 return ret;
358 }
359
Byongho Leeee221842015-12-15 01:42:10 +0900360 ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400361 if (ret) {
362 test_msg("Error removing bitmap and extent overlapping %d\n", ret);
363 return ret;
364 }
365
366 __btrfs_remove_free_space_cache(cache->free_space_ctl);
367 return 0;
368}
369
Filipe Manana20005522014-08-29 13:35:13 +0100370/* Used by test_steal_space_from_bitmap_to_extent(). */
371static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
372 struct btrfs_free_space *info)
373{
374 return ctl->free_extents > 0;
375}
376
377/* Used by test_steal_space_from_bitmap_to_extent(). */
378static int
379check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
380 const int num_extents,
381 const int num_bitmaps)
382{
383 if (cache->free_space_ctl->free_extents != num_extents) {
384 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
385 cache->free_space_ctl->free_extents, num_extents);
386 return -EINVAL;
387 }
388 if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
389 test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
390 cache->free_space_ctl->total_bitmaps, num_bitmaps);
391 return -EINVAL;
392 }
393 return 0;
394}
395
396/* Used by test_steal_space_from_bitmap_to_extent(). */
397static int check_cache_empty(struct btrfs_block_group_cache *cache)
398{
399 u64 offset;
400 u64 max_extent_size;
401
402 /*
403 * Now lets confirm that there's absolutely no free space left to
404 * allocate.
405 */
406 if (cache->free_space_ctl->free_space != 0) {
407 test_msg("Cache free space is not 0\n");
408 return -EINVAL;
409 }
410
411 /* And any allocation request, no matter how small, should fail now. */
412 offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
413 &max_extent_size);
414 if (offset != 0) {
415 test_msg("Space allocation did not fail, returned offset: %llu",
416 offset);
417 return -EINVAL;
418 }
419
420 /* And no extent nor bitmap entries in the cache anymore. */
421 return check_num_extents_and_bitmaps(cache, 0, 0);
422}
423
424/*
425 * Before we were able to steal free space from a bitmap entry to an extent
426 * entry, we could end up with 2 entries representing a contiguous free space.
427 * One would be an extent entry and the other a bitmap entry. Since in order
428 * to allocate space to a caller we use only 1 entry, we couldn't return that
429 * whole range to the caller if it was requested. This forced the caller to
430 * either assume ENOSPC or perform several smaller space allocations, which
431 * wasn't optimal as they could be spread all over the block group while under
432 * concurrency (extra overhead and fragmentation).
433 *
434 * This stealing approach is benefical, since we always prefer to allocate from
435 * extent entries, both for clustered and non-clustered allocation requests.
436 */
437static int
438test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
439{
440 int ret;
441 u64 offset;
442 u64 max_extent_size;
443
444 bool (*use_bitmap_op)(struct btrfs_free_space_ctl *,
445 struct btrfs_free_space *);
446
447 test_msg("Running space stealing from bitmap to extent\n");
448
449 /*
450 * For this test, we want to ensure we end up with an extent entry
451 * immediately adjacent to a bitmap entry, where the bitmap starts
452 * at an offset where the extent entry ends. We keep adding and
453 * removing free space to reach into this state, but to get there
454 * we need to reach a point where marking new free space doesn't
455 * result in adding new extent entries or merging the new space
456 * with existing extent entries - the space ends up being marked
457 * in an existing bitmap that covers the new free space range.
458 *
459 * To get there, we need to reach the threshold defined set at
460 * cache->free_space_ctl->extents_thresh, which currently is
461 * 256 extents on a x86_64 system at least, and a few other
462 * conditions (check free_space_cache.c). Instead of making the
463 * test much longer and complicated, use a "use_bitmap" operation
464 * that forces use of bitmaps as soon as we have at least 1
465 * extent entry.
466 */
467 use_bitmap_op = cache->free_space_ctl->op->use_bitmap;
468 cache->free_space_ctl->op->use_bitmap = test_use_bitmap;
469
470 /*
471 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
472 */
Byongho Leeee221842015-12-15 01:42:10 +0900473 ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
Filipe Manana20005522014-08-29 13:35:13 +0100474 if (ret) {
475 test_msg("Couldn't add extent entry %d\n", ret);
476 return ret;
477 }
478
479 /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
Byongho Leeee221842015-12-15 01:42:10 +0900480 ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
481 SZ_128M - SZ_512K, 1);
Filipe Manana20005522014-08-29 13:35:13 +0100482 if (ret) {
483 test_msg("Couldn't add bitmap entry %d\n", ret);
484 return ret;
485 }
486
487 ret = check_num_extents_and_bitmaps(cache, 2, 1);
488 if (ret)
489 return ret;
490
491 /*
492 * Now make only the first 256Kb of the bitmap marked as free, so that
493 * we end up with only the following ranges marked as free space:
494 *
495 * [128Mb - 256Kb, 128Mb - 128Kb[
496 * [128Mb + 512Kb, 128Mb + 768Kb[
497 */
498 ret = btrfs_remove_free_space(cache,
Byongho Leeee221842015-12-15 01:42:10 +0900499 SZ_128M + 768 * SZ_1K,
500 SZ_128M - 768 * SZ_1K);
Filipe Manana20005522014-08-29 13:35:13 +0100501 if (ret) {
502 test_msg("Failed to free part of bitmap space %d\n", ret);
503 return ret;
504 }
505
506 /* Confirm that only those 2 ranges are marked as free. */
Byongho Leeee221842015-12-15 01:42:10 +0900507 if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100508 test_msg("Free space range missing\n");
509 return -ENOENT;
510 }
Byongho Leeee221842015-12-15 01:42:10 +0900511 if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100512 test_msg("Free space range missing\n");
513 return -ENOENT;
514 }
515
516 /*
517 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
518 * as free anymore.
519 */
Byongho Leeee221842015-12-15 01:42:10 +0900520 if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
521 SZ_128M - 768 * SZ_1K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100522 test_msg("Bitmap region not removed from space cache\n");
523 return -EINVAL;
524 }
525
526 /*
527 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
528 * covered by the bitmap, isn't marked as free.
529 */
Byongho Leeee221842015-12-15 01:42:10 +0900530 if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100531 test_msg("Invalid bitmap region marked as free\n");
532 return -EINVAL;
533 }
534
535 /*
536 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
537 * by the bitmap too, isn't marked as free either.
538 */
Byongho Leeee221842015-12-15 01:42:10 +0900539 if (test_check_exists(cache, SZ_128M, SZ_256K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100540 test_msg("Invalid bitmap region marked as free\n");
541 return -EINVAL;
542 }
543
544 /*
545 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
546 * lets make sure the free space cache marks it as free in the bitmap,
547 * and doesn't insert a new extent entry to represent this region.
548 */
Byongho Leeee221842015-12-15 01:42:10 +0900549 ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
Filipe Manana20005522014-08-29 13:35:13 +0100550 if (ret) {
551 test_msg("Error adding free space: %d\n", ret);
552 return ret;
553 }
554 /* Confirm the region is marked as free. */
Byongho Leeee221842015-12-15 01:42:10 +0900555 if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100556 test_msg("Bitmap region not marked as free\n");
557 return -ENOENT;
558 }
559
560 /*
561 * Confirm that no new extent entries or bitmap entries were added to
562 * the cache after adding that free space region.
563 */
564 ret = check_num_extents_and_bitmaps(cache, 2, 1);
565 if (ret)
566 return ret;
567
568 /*
569 * Now lets add a small free space region to the right of the previous
570 * one, which is not contiguous with it and is part of the bitmap too.
571 * The goal is to test that the bitmap entry space stealing doesn't
572 * steal this space region.
573 */
Byongho Leeee221842015-12-15 01:42:10 +0900574 ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, 4096);
Filipe Manana20005522014-08-29 13:35:13 +0100575 if (ret) {
576 test_msg("Error adding free space: %d\n", ret);
577 return ret;
578 }
579
580 /*
581 * Confirm that no new extent entries or bitmap entries were added to
582 * the cache after adding that free space region.
583 */
584 ret = check_num_extents_and_bitmaps(cache, 2, 1);
585 if (ret)
586 return ret;
587
588 /*
589 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
590 * expand the range covered by the existing extent entry that represents
591 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
592 */
Byongho Leeee221842015-12-15 01:42:10 +0900593 ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
Filipe Manana20005522014-08-29 13:35:13 +0100594 if (ret) {
595 test_msg("Error adding free space: %d\n", ret);
596 return ret;
597 }
598 /* Confirm the region is marked as free. */
Byongho Leeee221842015-12-15 01:42:10 +0900599 if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100600 test_msg("Extent region not marked as free\n");
601 return -ENOENT;
602 }
603
604 /*
605 * Confirm that our extent entry didn't stole all free space from the
606 * bitmap, because of the small 4Kb free space region.
607 */
608 ret = check_num_extents_and_bitmaps(cache, 2, 1);
609 if (ret)
610 return ret;
611
612 /*
613 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
614 * space. Without stealing bitmap free space into extent entry space,
615 * we would have all this free space represented by 2 entries in the
616 * cache:
617 *
618 * extent entry covering range: [128Mb - 256Kb, 128Mb[
619 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
620 *
621 * Attempting to allocate the whole free space (1Mb) would fail, because
622 * we can't allocate from multiple entries.
623 * With the bitmap free space stealing, we get a single extent entry
624 * that represents the 1Mb free space, and therefore we're able to
625 * allocate the whole free space at once.
626 */
Byongho Leeee221842015-12-15 01:42:10 +0900627 if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
Filipe Manana20005522014-08-29 13:35:13 +0100628 test_msg("Expected region not marked as free\n");
629 return -ENOENT;
630 }
631
Byongho Leeee221842015-12-15 01:42:10 +0900632 if (cache->free_space_ctl->free_space != (SZ_1M + 4096)) {
Filipe Manana20005522014-08-29 13:35:13 +0100633 test_msg("Cache free space is not 1Mb + 4Kb\n");
634 return -EINVAL;
635 }
636
637 offset = btrfs_find_space_for_alloc(cache,
Byongho Leeee221842015-12-15 01:42:10 +0900638 0, SZ_1M, 0,
Filipe Manana20005522014-08-29 13:35:13 +0100639 &max_extent_size);
Byongho Leeee221842015-12-15 01:42:10 +0900640 if (offset != (SZ_128M - SZ_256K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100641 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
642 offset);
643 return -EINVAL;
644 }
645
646 /* All that remains is a 4Kb free space region in a bitmap. Confirm. */
647 ret = check_num_extents_and_bitmaps(cache, 1, 1);
648 if (ret)
649 return ret;
650
651 if (cache->free_space_ctl->free_space != 4096) {
652 test_msg("Cache free space is not 4Kb\n");
653 return -EINVAL;
654 }
655
656 offset = btrfs_find_space_for_alloc(cache,
657 0, 4096, 0,
658 &max_extent_size);
Byongho Leeee221842015-12-15 01:42:10 +0900659 if (offset != (SZ_128M + SZ_16M)) {
Filipe Manana20005522014-08-29 13:35:13 +0100660 test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
661 offset);
662 return -EINVAL;
663 }
664
665 ret = check_cache_empty(cache);
666 if (ret)
667 return ret;
668
669 __btrfs_remove_free_space_cache(cache->free_space_ctl);
670
671 /*
672 * Now test a similar scenario, but where our extent entry is located
673 * to the right of the bitmap entry, so that we can check that stealing
674 * space from a bitmap to the front of an extent entry works.
675 */
676
677 /*
678 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
679 */
Byongho Leeee221842015-12-15 01:42:10 +0900680 ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
Filipe Manana20005522014-08-29 13:35:13 +0100681 if (ret) {
682 test_msg("Couldn't add extent entry %d\n", ret);
683 return ret;
684 }
685
686 /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
Byongho Leeee221842015-12-15 01:42:10 +0900687 ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
Filipe Manana20005522014-08-29 13:35:13 +0100688 if (ret) {
689 test_msg("Couldn't add bitmap entry %d\n", ret);
690 return ret;
691 }
692
693 ret = check_num_extents_and_bitmaps(cache, 2, 1);
694 if (ret)
695 return ret;
696
697 /*
698 * Now make only the last 256Kb of the bitmap marked as free, so that
699 * we end up with only the following ranges marked as free space:
700 *
701 * [128Mb + 128b, 128Mb + 256Kb[
702 * [128Mb - 768Kb, 128Mb - 512Kb[
703 */
Byongho Leeee221842015-12-15 01:42:10 +0900704 ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
Filipe Manana20005522014-08-29 13:35:13 +0100705 if (ret) {
706 test_msg("Failed to free part of bitmap space %d\n", ret);
707 return ret;
708 }
709
710 /* Confirm that only those 2 ranges are marked as free. */
Byongho Leeee221842015-12-15 01:42:10 +0900711 if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100712 test_msg("Free space range missing\n");
713 return -ENOENT;
714 }
Byongho Leeee221842015-12-15 01:42:10 +0900715 if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100716 test_msg("Free space range missing\n");
717 return -ENOENT;
718 }
719
720 /*
721 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
722 * as free anymore.
723 */
Byongho Leeee221842015-12-15 01:42:10 +0900724 if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100725 test_msg("Bitmap region not removed from space cache\n");
726 return -EINVAL;
727 }
728
729 /*
730 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
731 * covered by the bitmap, isn't marked as free.
732 */
Byongho Leeee221842015-12-15 01:42:10 +0900733 if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100734 test_msg("Invalid bitmap region marked as free\n");
735 return -EINVAL;
736 }
737
738 /*
739 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
740 * lets make sure the free space cache marks it as free in the bitmap,
741 * and doesn't insert a new extent entry to represent this region.
742 */
Byongho Leeee221842015-12-15 01:42:10 +0900743 ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
Filipe Manana20005522014-08-29 13:35:13 +0100744 if (ret) {
745 test_msg("Error adding free space: %d\n", ret);
746 return ret;
747 }
748 /* Confirm the region is marked as free. */
Byongho Leeee221842015-12-15 01:42:10 +0900749 if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100750 test_msg("Bitmap region not marked as free\n");
751 return -ENOENT;
752 }
753
754 /*
755 * Confirm that no new extent entries or bitmap entries were added to
756 * the cache after adding that free space region.
757 */
758 ret = check_num_extents_and_bitmaps(cache, 2, 1);
759 if (ret)
760 return ret;
761
762 /*
763 * Now lets add a small free space region to the left of the previous
764 * one, which is not contiguous with it and is part of the bitmap too.
765 * The goal is to test that the bitmap entry space stealing doesn't
766 * steal this space region.
767 */
Byongho Leeee221842015-12-15 01:42:10 +0900768 ret = btrfs_add_free_space(cache, SZ_32M, 8192);
Filipe Manana20005522014-08-29 13:35:13 +0100769 if (ret) {
770 test_msg("Error adding free space: %d\n", ret);
771 return ret;
772 }
773
774 /*
775 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
776 * expand the range covered by the existing extent entry that represents
777 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
778 */
Byongho Leeee221842015-12-15 01:42:10 +0900779 ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
Filipe Manana20005522014-08-29 13:35:13 +0100780 if (ret) {
781 test_msg("Error adding free space: %d\n", ret);
782 return ret;
783 }
784 /* Confirm the region is marked as free. */
Byongho Leeee221842015-12-15 01:42:10 +0900785 if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100786 test_msg("Extent region not marked as free\n");
787 return -ENOENT;
788 }
789
790 /*
791 * Confirm that our extent entry didn't stole all free space from the
792 * bitmap, because of the small 8Kb free space region.
793 */
794 ret = check_num_extents_and_bitmaps(cache, 2, 1);
795 if (ret)
796 return ret;
797
798 /*
799 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
800 * space. Without stealing bitmap free space into extent entry space,
801 * we would have all this free space represented by 2 entries in the
802 * cache:
803 *
804 * extent entry covering range: [128Mb, 128Mb + 256Kb[
805 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
806 *
807 * Attempting to allocate the whole free space (1Mb) would fail, because
808 * we can't allocate from multiple entries.
809 * With the bitmap free space stealing, we get a single extent entry
810 * that represents the 1Mb free space, and therefore we're able to
811 * allocate the whole free space at once.
812 */
Byongho Leeee221842015-12-15 01:42:10 +0900813 if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
Filipe Manana20005522014-08-29 13:35:13 +0100814 test_msg("Expected region not marked as free\n");
815 return -ENOENT;
816 }
817
Byongho Leeee221842015-12-15 01:42:10 +0900818 if (cache->free_space_ctl->free_space != (SZ_1M + 8192)) {
Filipe Manana20005522014-08-29 13:35:13 +0100819 test_msg("Cache free space is not 1Mb + 8Kb\n");
820 return -EINVAL;
821 }
822
Byongho Leeee221842015-12-15 01:42:10 +0900823 offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
Filipe Manana20005522014-08-29 13:35:13 +0100824 &max_extent_size);
Byongho Leeee221842015-12-15 01:42:10 +0900825 if (offset != (SZ_128M - 768 * SZ_1K)) {
Filipe Manana20005522014-08-29 13:35:13 +0100826 test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
827 offset);
828 return -EINVAL;
829 }
830
831 /* All that remains is a 8Kb free space region in a bitmap. Confirm. */
832 ret = check_num_extents_and_bitmaps(cache, 1, 1);
833 if (ret)
834 return ret;
835
836 if (cache->free_space_ctl->free_space != 8192) {
837 test_msg("Cache free space is not 8Kb\n");
838 return -EINVAL;
839 }
840
841 offset = btrfs_find_space_for_alloc(cache,
842 0, 8192, 0,
843 &max_extent_size);
Byongho Leeee221842015-12-15 01:42:10 +0900844 if (offset != SZ_32M) {
Filipe Manana20005522014-08-29 13:35:13 +0100845 test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
846 offset);
847 return -EINVAL;
848 }
849
850 ret = check_cache_empty(cache);
851 if (ret)
852 return ret;
853
854 cache->free_space_ctl->op->use_bitmap = use_bitmap_op;
855 __btrfs_remove_free_space_cache(cache->free_space_ctl);
856
857 return 0;
858}
859
Josef Bacikdc11dd52013-08-14 15:05:12 -0400860int btrfs_test_free_space_cache(void)
861{
862 struct btrfs_block_group_cache *cache;
Josef Bacikd0bd4562015-09-23 14:54:14 -0400863 struct btrfs_root *root = NULL;
864 int ret = -ENOMEM;
Josef Bacikdc11dd52013-08-14 15:05:12 -0400865
866 test_msg("Running btrfs free space cache tests\n");
867
868 cache = init_test_block_group();
869 if (!cache) {
870 test_msg("Couldn't run the tests\n");
871 return 0;
872 }
873
Josef Bacikd0bd4562015-09-23 14:54:14 -0400874 root = btrfs_alloc_dummy_root();
Dan Carpenter89b6c8d2015-11-10 12:10:03 +0300875 if (IS_ERR(root)) {
876 ret = PTR_ERR(root);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400877 goto out;
Dan Carpenter89b6c8d2015-11-10 12:10:03 +0300878 }
Josef Bacikd0bd4562015-09-23 14:54:14 -0400879
880 root->fs_info = btrfs_alloc_dummy_fs_info();
881 if (!root->fs_info)
882 goto out;
883
884 root->fs_info->extent_root = root;
885 cache->fs_info = root->fs_info;
886
Josef Bacikdc11dd52013-08-14 15:05:12 -0400887 ret = test_extents(cache);
888 if (ret)
889 goto out;
890 ret = test_bitmaps(cache);
891 if (ret)
892 goto out;
893 ret = test_bitmaps_and_extents(cache);
894 if (ret)
895 goto out;
Filipe Manana20005522014-08-29 13:35:13 +0100896
897 ret = test_steal_space_from_bitmap_to_extent(cache);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400898out:
899 __btrfs_remove_free_space_cache(cache->free_space_ctl);
900 kfree(cache->free_space_ctl);
901 kfree(cache);
Josef Bacikd0bd4562015-09-23 14:54:14 -0400902 btrfs_free_dummy_root(root);
Josef Bacikdc11dd52013-08-14 15:05:12 -0400903 test_msg("Free space cache tests finished\n");
904 return ret;
905}