Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013 Fusion IO.  All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 */
  5
  6#include <linux/slab.h>
  7#include "btrfs-tests.h"
  8#include "../ctree.h"
  9#include "../disk-io.h"
 10#include "../free-space-cache.h"
 11#include "../block-group.h"
 12
 13#define BITS_PER_BITMAP		(PAGE_SIZE * 8UL)
 14
 15/*
 16 * This test just does basic sanity checking, making sure we can add an extent
 17 * entry and remove space from either end and the middle, and make sure we can
 18 * remove space that covers adjacent extent entries.
 19 */
 20static int test_extents(struct btrfs_block_group *cache)
 21{
 22	int ret = 0;
 23
 24	test_msg("running extent only tests");
 25
 26	/* First just make sure we can remove an entire entry */
 27	ret = btrfs_add_free_space(cache, 0, SZ_4M);
 28	if (ret) {
 29		test_err("error adding initial extents %d", ret);
 30		return ret;
 31	}
 32
 33	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
 34	if (ret) {
 35		test_err("error removing extent %d", ret);
 36		return ret;
 37	}
 38
 39	if (test_check_exists(cache, 0, SZ_4M)) {
 40		test_err("full remove left some lingering space");
 41		return -1;
 42	}
 43
 44	/* Ok edge and middle cases now */
 45	ret = btrfs_add_free_space(cache, 0, SZ_4M);
 46	if (ret) {
 47		test_err("error adding half extent %d", ret);
 48		return ret;
 49	}
 50
 51	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
 52	if (ret) {
 53		test_err("error removing tail end %d", ret);
 54		return ret;
 55	}
 56
 57	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
 58	if (ret) {
 59		test_err("error removing front end %d", ret);
 60		return ret;
 61	}
 62
 63	ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
 64	if (ret) {
 65		test_err("error removing middle piece %d", ret);
 66		return ret;
 67	}
 68
 69	if (test_check_exists(cache, 0, SZ_1M)) {
 70		test_err("still have space at the front");
 71		return -1;
 72	}
 73
 74	if (test_check_exists(cache, SZ_2M, 4096)) {
 75		test_err("still have space in the middle");
 76		return -1;
 77	}
 78
 79	if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
 80		test_err("still have space at the end");
 81		return -1;
 82	}
 83
 84	/* Cleanup */
 85	__btrfs_remove_free_space_cache(cache->free_space_ctl);
 86
 87	return 0;
 88}
 89
 90static int test_bitmaps(struct btrfs_block_group *cache, u32 sectorsize)
 91{
 92	u64 next_bitmap_offset;
 93	int ret;
 94
 95	test_msg("running bitmap only tests");
 96
 97	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
 98	if (ret) {
 99		test_err("couldn't create a bitmap entry %d", ret);
100		return ret;
101	}
102
103	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
104	if (ret) {
105		test_err("error removing bitmap full range %d", ret);
106		return ret;
107	}
108
109	if (test_check_exists(cache, 0, SZ_4M)) {
110		test_err("left some space in bitmap");
111		return -1;
112	}
113
114	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
115	if (ret) {
116		test_err("couldn't add to our bitmap entry %d", ret);
117		return ret;
118	}
119
120	ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
121	if (ret) {
122		test_err("couldn't remove middle chunk %d", ret);
123		return ret;
124	}
125
126	/*
127	 * The first bitmap we have starts at offset 0 so the next one is just
128	 * at the end of the first bitmap.
129	 */
130	next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
131
132	/* Test a bit straddling two bitmaps */
133	ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
134					SZ_4M, 1);
135	if (ret) {
136		test_err("couldn't add space that straddles two bitmaps %d",
137				ret);
138		return ret;
139	}
140
141	ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
142	if (ret) {
143		test_err("couldn't remove overlapping space %d", ret);
144		return ret;
145	}
146
147	if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
148		test_err("left some space when removing overlapping");
149		return -1;
150	}
151
152	__btrfs_remove_free_space_cache(cache->free_space_ctl);
153
154	return 0;
155}
156
157/* This is the high grade jackassery */
158static int test_bitmaps_and_extents(struct btrfs_block_group *cache,
159				    u32 sectorsize)
160{
161	u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
162	int ret;
163
164	test_msg("running bitmap and extent tests");
165
166	/*
167	 * First let's do something simple, an extent at the same offset as the
168	 * bitmap, but the free space completely in the extent and then
169	 * completely in the bitmap.
170	 */
171	ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
172	if (ret) {
173		test_err("couldn't create bitmap entry %d", ret);
174		return ret;
175	}
176
177	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
178	if (ret) {
179		test_err("couldn't add extent entry %d", ret);
180		return ret;
181	}
182
183	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
184	if (ret) {
185		test_err("couldn't remove extent entry %d", ret);
186		return ret;
187	}
188
189	if (test_check_exists(cache, 0, SZ_1M)) {
190		test_err("left remnants after our remove");
191		return -1;
192	}
193
194	/* Now to add back the extent entry and remove from the bitmap */
195	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
196	if (ret) {
197		test_err("couldn't re-add extent entry %d", ret);
198		return ret;
199	}
200
201	ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
202	if (ret) {
203		test_err("couldn't remove from bitmap %d", ret);
204		return ret;
205	}
206
207	if (test_check_exists(cache, SZ_4M, SZ_1M)) {
208		test_err("left remnants in the bitmap");
209		return -1;
210	}
211
212	/*
213	 * Ok so a little more evil, extent entry and bitmap at the same offset,
214	 * removing an overlapping chunk.
215	 */
216	ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
217	if (ret) {
218		test_err("couldn't add to a bitmap %d", ret);
219		return ret;
220	}
221
222	ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
223	if (ret) {
224		test_err("couldn't remove overlapping space %d", ret);
225		return ret;
226	}
227
228	if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
229		test_err("left over pieces after removing overlapping");
230		return -1;
231	}
232
233	__btrfs_remove_free_space_cache(cache->free_space_ctl);
234
235	/* Now with the extent entry offset into the bitmap */
236	ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
237	if (ret) {
238		test_err("couldn't add space to the bitmap %d", ret);
239		return ret;
240	}
241
242	ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
243	if (ret) {
244		test_err("couldn't add extent to the cache %d", ret);
245		return ret;
246	}
247
248	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
249	if (ret) {
250		test_err("problem removing overlapping space %d", ret);
251		return ret;
252	}
253
254	if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
255		test_err("left something behind when removing space");
256		return -1;
257	}
258
259	/*
260	 * This has blown up in the past, the extent entry starts before the
261	 * bitmap entry, but we're trying to remove an offset that falls
262	 * completely within the bitmap range and is in both the extent entry
263	 * and the bitmap entry, looks like this
264	 *
265	 *   [ extent ]
266	 *      [ bitmap ]
267	 *        [ del ]
268	 */
269	__btrfs_remove_free_space_cache(cache->free_space_ctl);
270	ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
271	if (ret) {
272		test_err("couldn't add bitmap %d", ret);
273		return ret;
274	}
275
276	ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
277					5 * SZ_1M, 0);
278	if (ret) {
279		test_err("couldn't add extent entry %d", ret);
280		return ret;
281	}
282
283	ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
284	if (ret) {
285		test_err("failed to free our space %d", ret);
286		return ret;
287	}
288
289	if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
290		test_err("left stuff over");
291		return -1;
292	}
293
294	__btrfs_remove_free_space_cache(cache->free_space_ctl);
295
296	/*
297	 * This blew up before, we have part of the free space in a bitmap and
298	 * then the entirety of the rest of the space in an extent.  This used
299	 * to return -EAGAIN back from btrfs_remove_extent, make sure this
300	 * doesn't happen.
301	 */
302	ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
303	if (ret) {
304		test_err("couldn't add bitmap entry %d", ret);
305		return ret;
306	}
307
308	ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
309	if (ret) {
310		test_err("couldn't add extent entry %d", ret);
311		return ret;
312	}
313
314	ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
315	if (ret) {
316		test_err("error removing bitmap and extent overlapping %d", ret);
317		return ret;
318	}
319
320	__btrfs_remove_free_space_cache(cache->free_space_ctl);
321	return 0;
322}
323
324/* Used by test_steal_space_from_bitmap_to_extent(). */
325static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
326			    struct btrfs_free_space *info)
327{
328	return ctl->free_extents > 0;
329}
330
331/* Used by test_steal_space_from_bitmap_to_extent(). */
332static int
333check_num_extents_and_bitmaps(const struct btrfs_block_group *cache,
334			      const int num_extents,
335			      const int num_bitmaps)
336{
337	if (cache->free_space_ctl->free_extents != num_extents) {
338		test_err(
339		"incorrect # of extent entries in the cache: %d, expected %d",
340			 cache->free_space_ctl->free_extents, num_extents);
341		return -EINVAL;
342	}
343	if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
344		test_err(
345		"incorrect # of extent entries in the cache: %d, expected %d",
346			 cache->free_space_ctl->total_bitmaps, num_bitmaps);
347		return -EINVAL;
348	}
349	return 0;
350}
351
352/* Used by test_steal_space_from_bitmap_to_extent(). */
353static int check_cache_empty(struct btrfs_block_group *cache)
354{
355	u64 offset;
356	u64 max_extent_size;
357
358	/*
359	 * Now lets confirm that there's absolutely no free space left to
360	 * allocate.
361	 */
362	if (cache->free_space_ctl->free_space != 0) {
363		test_err("cache free space is not 0");
364		return -EINVAL;
365	}
366
367	/* And any allocation request, no matter how small, should fail now. */
368	offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
369					    &max_extent_size);
370	if (offset != 0) {
371		test_err("space allocation did not fail, returned offset: %llu",
372			 offset);
373		return -EINVAL;
374	}
375
376	/* And no extent nor bitmap entries in the cache anymore. */
377	return check_num_extents_and_bitmaps(cache, 0, 0);
378}
379
380/*
381 * Before we were able to steal free space from a bitmap entry to an extent
382 * entry, we could end up with 2 entries representing a contiguous free space.
383 * One would be an extent entry and the other a bitmap entry. Since in order
384 * to allocate space to a caller we use only 1 entry, we couldn't return that
385 * whole range to the caller if it was requested. This forced the caller to
386 * either assume ENOSPC or perform several smaller space allocations, which
387 * wasn't optimal as they could be spread all over the block group while under
388 * concurrency (extra overhead and fragmentation).
389 *
390 * This stealing approach is beneficial, since we always prefer to allocate
391 * from extent entries, both for clustered and non-clustered allocation
392 * requests.
393 */
394static int
395test_steal_space_from_bitmap_to_extent(struct btrfs_block_group *cache,
396				       u32 sectorsize)
397{
398	int ret;
399	u64 offset;
400	u64 max_extent_size;
401	const struct btrfs_free_space_op test_free_space_ops = {
 
402		.use_bitmap = test_use_bitmap,
403	};
404	const struct btrfs_free_space_op *orig_free_space_ops;
405
406	test_msg("running space stealing from bitmap to extent tests");
407
408	/*
409	 * For this test, we want to ensure we end up with an extent entry
410	 * immediately adjacent to a bitmap entry, where the bitmap starts
411	 * at an offset where the extent entry ends. We keep adding and
412	 * removing free space to reach into this state, but to get there
413	 * we need to reach a point where marking new free space doesn't
414	 * result in adding new extent entries or merging the new space
415	 * with existing extent entries - the space ends up being marked
416	 * in an existing bitmap that covers the new free space range.
417	 *
418	 * To get there, we need to reach the threshold defined set at
419	 * cache->free_space_ctl->extents_thresh, which currently is
420	 * 256 extents on a x86_64 system at least, and a few other
421	 * conditions (check free_space_cache.c). Instead of making the
422	 * test much longer and complicated, use a "use_bitmap" operation
423	 * that forces use of bitmaps as soon as we have at least 1
424	 * extent entry.
425	 */
426	orig_free_space_ops = cache->free_space_ctl->op;
427	cache->free_space_ctl->op = &test_free_space_ops;
428
429	/*
430	 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
431	 */
432	ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
433	if (ret) {
434		test_err("couldn't add extent entry %d", ret);
435		return ret;
436	}
437
438	/* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
439	ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
440					SZ_128M - SZ_512K, 1);
441	if (ret) {
442		test_err("couldn't add bitmap entry %d", ret);
443		return ret;
444	}
445
446	ret = check_num_extents_and_bitmaps(cache, 2, 1);
447	if (ret)
448		return ret;
449
450	/*
451	 * Now make only the first 256Kb of the bitmap marked as free, so that
452	 * we end up with only the following ranges marked as free space:
453	 *
454	 * [128Mb - 256Kb, 128Mb - 128Kb[
455	 * [128Mb + 512Kb, 128Mb + 768Kb[
456	 */
457	ret = btrfs_remove_free_space(cache,
458				      SZ_128M + 768 * SZ_1K,
459				      SZ_128M - 768 * SZ_1K);
460	if (ret) {
461		test_err("failed to free part of bitmap space %d", ret);
462		return ret;
463	}
464
465	/* Confirm that only those 2 ranges are marked as free. */
466	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
467		test_err("free space range missing");
468		return -ENOENT;
469	}
470	if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
471		test_err("free space range missing");
472		return -ENOENT;
473	}
474
475	/*
476	 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
477	 * as free anymore.
478	 */
479	if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
480			      SZ_128M - 768 * SZ_1K)) {
481		test_err("bitmap region not removed from space cache");
482		return -EINVAL;
483	}
484
485	/*
486	 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
487	 * covered by the bitmap, isn't marked as free.
488	 */
489	if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
490		test_err("invalid bitmap region marked as free");
491		return -EINVAL;
492	}
493
494	/*
495	 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
496	 * by the bitmap too, isn't marked as free either.
497	 */
498	if (test_check_exists(cache, SZ_128M, SZ_256K)) {
499		test_err("invalid bitmap region marked as free");
500		return -EINVAL;
501	}
502
503	/*
504	 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
505	 * lets make sure the free space cache marks it as free in the bitmap,
506	 * and doesn't insert a new extent entry to represent this region.
507	 */
508	ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
509	if (ret) {
510		test_err("error adding free space: %d", ret);
511		return ret;
512	}
513	/* Confirm the region is marked as free. */
514	if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
515		test_err("bitmap region not marked as free");
516		return -ENOENT;
517	}
518
519	/*
520	 * Confirm that no new extent entries or bitmap entries were added to
521	 * the cache after adding that free space region.
522	 */
523	ret = check_num_extents_and_bitmaps(cache, 2, 1);
524	if (ret)
525		return ret;
526
527	/*
528	 * Now lets add a small free space region to the right of the previous
529	 * one, which is not contiguous with it and is part of the bitmap too.
530	 * The goal is to test that the bitmap entry space stealing doesn't
531	 * steal this space region.
532	 */
533	ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize);
534	if (ret) {
535		test_err("error adding free space: %d", ret);
536		return ret;
537	}
538
539	/*
540	 * Confirm that no new extent entries or bitmap entries were added to
541	 * the cache after adding that free space region.
542	 */
543	ret = check_num_extents_and_bitmaps(cache, 2, 1);
544	if (ret)
545		return ret;
546
547	/*
548	 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
549	 * expand the range covered by the existing extent entry that represents
550	 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
551	 */
552	ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
553	if (ret) {
554		test_err("error adding free space: %d", ret);
555		return ret;
556	}
557	/* Confirm the region is marked as free. */
558	if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
559		test_err("extent region not marked as free");
560		return -ENOENT;
561	}
562
563	/*
564	 * Confirm that our extent entry didn't stole all free space from the
565	 * bitmap, because of the small 4Kb free space region.
566	 */
567	ret = check_num_extents_and_bitmaps(cache, 2, 1);
568	if (ret)
569		return ret;
570
571	/*
572	 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
573	 * space. Without stealing bitmap free space into extent entry space,
574	 * we would have all this free space represented by 2 entries in the
575	 * cache:
576	 *
577	 * extent entry covering range: [128Mb - 256Kb, 128Mb[
578	 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
579	 *
580	 * Attempting to allocate the whole free space (1Mb) would fail, because
581	 * we can't allocate from multiple entries.
582	 * With the bitmap free space stealing, we get a single extent entry
583	 * that represents the 1Mb free space, and therefore we're able to
584	 * allocate the whole free space at once.
585	 */
586	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
587		test_err("expected region not marked as free");
588		return -ENOENT;
589	}
590
591	if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) {
592		test_err("cache free space is not 1Mb + %u", sectorsize);
593		return -EINVAL;
594	}
595
596	offset = btrfs_find_space_for_alloc(cache,
597					    0, SZ_1M, 0,
598					    &max_extent_size);
599	if (offset != (SZ_128M - SZ_256K)) {
600		test_err(
601	"failed to allocate 1Mb from space cache, returned offset is: %llu",
602			 offset);
603		return -EINVAL;
604	}
605
606	/*
607	 * All that remains is a sectorsize free space region in a bitmap.
608	 * Confirm.
609	 */
610	ret = check_num_extents_and_bitmaps(cache, 1, 1);
611	if (ret)
612		return ret;
613
614	if (cache->free_space_ctl->free_space != sectorsize) {
615		test_err("cache free space is not %u", sectorsize);
616		return -EINVAL;
617	}
618
619	offset = btrfs_find_space_for_alloc(cache,
620					    0, sectorsize, 0,
621					    &max_extent_size);
622	if (offset != (SZ_128M + SZ_16M)) {
623		test_err("failed to allocate %u, returned offset : %llu",
624			 sectorsize, offset);
625		return -EINVAL;
626	}
627
628	ret = check_cache_empty(cache);
629	if (ret)
630		return ret;
631
632	__btrfs_remove_free_space_cache(cache->free_space_ctl);
633
634	/*
635	 * Now test a similar scenario, but where our extent entry is located
636	 * to the right of the bitmap entry, so that we can check that stealing
637	 * space from a bitmap to the front of an extent entry works.
638	 */
639
640	/*
641	 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
642	 */
643	ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
644	if (ret) {
645		test_err("couldn't add extent entry %d", ret);
646		return ret;
647	}
648
649	/* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
650	ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
651	if (ret) {
652		test_err("couldn't add bitmap entry %d", ret);
653		return ret;
654	}
655
656	ret = check_num_extents_and_bitmaps(cache, 2, 1);
657	if (ret)
658		return ret;
659
660	/*
661	 * Now make only the last 256Kb of the bitmap marked as free, so that
662	 * we end up with only the following ranges marked as free space:
663	 *
664	 * [128Mb + 128b, 128Mb + 256Kb[
665	 * [128Mb - 768Kb, 128Mb - 512Kb[
666	 */
667	ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
668	if (ret) {
669		test_err("failed to free part of bitmap space %d", ret);
670		return ret;
671	}
672
673	/* Confirm that only those 2 ranges are marked as free. */
674	if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
675		test_err("free space range missing");
676		return -ENOENT;
677	}
678	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
679		test_err("free space range missing");
680		return -ENOENT;
681	}
682
683	/*
684	 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
685	 * as free anymore.
686	 */
687	if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
688		test_err("bitmap region not removed from space cache");
689		return -EINVAL;
690	}
691
692	/*
693	 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
694	 * covered by the bitmap, isn't marked as free.
695	 */
696	if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
697		test_err("invalid bitmap region marked as free");
698		return -EINVAL;
699	}
700
701	/*
702	 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
703	 * lets make sure the free space cache marks it as free in the bitmap,
704	 * and doesn't insert a new extent entry to represent this region.
705	 */
706	ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
707	if (ret) {
708		test_err("error adding free space: %d", ret);
709		return ret;
710	}
711	/* Confirm the region is marked as free. */
712	if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
713		test_err("bitmap region not marked as free");
714		return -ENOENT;
715	}
716
717	/*
718	 * Confirm that no new extent entries or bitmap entries were added to
719	 * the cache after adding that free space region.
720	 */
721	ret = check_num_extents_and_bitmaps(cache, 2, 1);
722	if (ret)
723		return ret;
724
725	/*
726	 * Now lets add a small free space region to the left of the previous
727	 * one, which is not contiguous with it and is part of the bitmap too.
728	 * The goal is to test that the bitmap entry space stealing doesn't
729	 * steal this space region.
730	 */
731	ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize);
732	if (ret) {
733		test_err("error adding free space: %d", ret);
734		return ret;
735	}
736
737	/*
738	 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
739	 * expand the range covered by the existing extent entry that represents
740	 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
741	 */
742	ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
743	if (ret) {
744		test_err("error adding free space: %d", ret);
745		return ret;
746	}
747	/* Confirm the region is marked as free. */
748	if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
749		test_err("extent region not marked as free");
750		return -ENOENT;
751	}
752
753	/*
754	 * Confirm that our extent entry didn't stole all free space from the
755	 * bitmap, because of the small 2 * sectorsize free space region.
756	 */
757	ret = check_num_extents_and_bitmaps(cache, 2, 1);
758	if (ret)
759		return ret;
760
761	/*
762	 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
763	 * space. Without stealing bitmap free space into extent entry space,
764	 * we would have all this free space represented by 2 entries in the
765	 * cache:
766	 *
767	 * extent entry covering range: [128Mb, 128Mb + 256Kb[
768	 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
769	 *
770	 * Attempting to allocate the whole free space (1Mb) would fail, because
771	 * we can't allocate from multiple entries.
772	 * With the bitmap free space stealing, we get a single extent entry
773	 * that represents the 1Mb free space, and therefore we're able to
774	 * allocate the whole free space at once.
775	 */
776	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
777		test_err("expected region not marked as free");
778		return -ENOENT;
779	}
780
781	if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) {
782		test_err("cache free space is not 1Mb + %u", 2 * sectorsize);
783		return -EINVAL;
784	}
785
786	offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
787					    &max_extent_size);
788	if (offset != (SZ_128M - 768 * SZ_1K)) {
789		test_err(
790	"failed to allocate 1Mb from space cache, returned offset is: %llu",
791			 offset);
792		return -EINVAL;
793	}
794
795	/*
796	 * All that remains is 2 * sectorsize free space region
797	 * in a bitmap. Confirm.
798	 */
799	ret = check_num_extents_and_bitmaps(cache, 1, 1);
800	if (ret)
801		return ret;
802
803	if (cache->free_space_ctl->free_space != 2 * sectorsize) {
804		test_err("cache free space is not %u", 2 * sectorsize);
805		return -EINVAL;
806	}
807
808	offset = btrfs_find_space_for_alloc(cache,
809					    0, 2 * sectorsize, 0,
810					    &max_extent_size);
811	if (offset != SZ_32M) {
812		test_err("failed to allocate %u, offset: %llu",
813			 2 * sectorsize, offset);
814		return -EINVAL;
815	}
816
817	ret = check_cache_empty(cache);
818	if (ret)
819		return ret;
820
821	cache->free_space_ctl->op = orig_free_space_ops;
822	__btrfs_remove_free_space_cache(cache->free_space_ctl);
823
824	return 0;
825}
826
827int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
828{
829	struct btrfs_fs_info *fs_info;
830	struct btrfs_block_group *cache;
831	struct btrfs_root *root = NULL;
832	int ret = -ENOMEM;
833
834	test_msg("running btrfs free space cache tests");
835	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
836	if (!fs_info) {
837		test_std_err(TEST_ALLOC_FS_INFO);
838		return -ENOMEM;
839	}
840
841	/*
842	 * For ppc64 (with 64k page size), bytes per bitmap might be
843	 * larger than 1G.  To make bitmap test available in ppc64,
844	 * alloc dummy block group whose size cross bitmaps.
845	 */
846	cache = btrfs_alloc_dummy_block_group(fs_info,
847				      BITS_PER_BITMAP * sectorsize + PAGE_SIZE);
848	if (!cache) {
849		test_std_err(TEST_ALLOC_BLOCK_GROUP);
850		btrfs_free_dummy_fs_info(fs_info);
851		return 0;
852	}
853
854	root = btrfs_alloc_dummy_root(fs_info);
855	if (IS_ERR(root)) {
856		test_std_err(TEST_ALLOC_ROOT);
857		ret = PTR_ERR(root);
858		goto out;
859	}
860
 
 
 
 
861	root->fs_info->extent_root = root;
 
862
863	ret = test_extents(cache);
864	if (ret)
865		goto out;
866	ret = test_bitmaps(cache, sectorsize);
867	if (ret)
868		goto out;
869	ret = test_bitmaps_and_extents(cache, sectorsize);
870	if (ret)
871		goto out;
872
873	ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize);
874out:
875	btrfs_free_dummy_block_group(cache);
876	btrfs_free_dummy_root(root);
877	btrfs_free_dummy_fs_info(fs_info);
878	return ret;
879}
v4.6
 
  1/*
  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"
 22#include "../disk-io.h"
 23#include "../free-space-cache.h"
 
 24
 25#define BITS_PER_BITMAP		(PAGE_SIZE * 8)
 26
 27/*
 28 * This test just does basic sanity checking, making sure we can add an exten
 29 * entry and remove space from either end and the middle, and make sure we can
 30 * remove space that covers adjacent extent entries.
 31 */
 32static int test_extents(struct btrfs_block_group_cache *cache)
 33{
 34	int ret = 0;
 35
 36	test_msg("Running extent only tests\n");
 37
 38	/* First just make sure we can remove an entire entry */
 39	ret = btrfs_add_free_space(cache, 0, SZ_4M);
 40	if (ret) {
 41		test_msg("Error adding initial extents %d\n", ret);
 42		return ret;
 43	}
 44
 45	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
 46	if (ret) {
 47		test_msg("Error removing extent %d\n", ret);
 48		return ret;
 49	}
 50
 51	if (test_check_exists(cache, 0, SZ_4M)) {
 52		test_msg("Full remove left some lingering space\n");
 53		return -1;
 54	}
 55
 56	/* Ok edge and middle cases now */
 57	ret = btrfs_add_free_space(cache, 0, SZ_4M);
 58	if (ret) {
 59		test_msg("Error adding half extent %d\n", ret);
 60		return ret;
 61	}
 62
 63	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
 64	if (ret) {
 65		test_msg("Error removing tail end %d\n", ret);
 66		return ret;
 67	}
 68
 69	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
 70	if (ret) {
 71		test_msg("Error removing front end %d\n", ret);
 72		return ret;
 73	}
 74
 75	ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
 76	if (ret) {
 77		test_msg("Error removing middle piece %d\n", ret);
 78		return ret;
 79	}
 80
 81	if (test_check_exists(cache, 0, SZ_1M)) {
 82		test_msg("Still have space at the front\n");
 83		return -1;
 84	}
 85
 86	if (test_check_exists(cache, SZ_2M, 4096)) {
 87		test_msg("Still have space in the middle\n");
 88		return -1;
 89	}
 90
 91	if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
 92		test_msg("Still have space at the end\n");
 93		return -1;
 94	}
 95
 96	/* Cleanup */
 97	__btrfs_remove_free_space_cache(cache->free_space_ctl);
 98
 99	return 0;
100}
101
102static int test_bitmaps(struct btrfs_block_group_cache *cache)
103{
104	u64 next_bitmap_offset;
105	int ret;
106
107	test_msg("Running bitmap only tests\n");
108
109	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
110	if (ret) {
111		test_msg("Couldn't create a bitmap entry %d\n", ret);
112		return ret;
113	}
114
115	ret = btrfs_remove_free_space(cache, 0, SZ_4M);
116	if (ret) {
117		test_msg("Error removing bitmap full range %d\n", ret);
118		return ret;
119	}
120
121	if (test_check_exists(cache, 0, SZ_4M)) {
122		test_msg("Left some space in bitmap\n");
123		return -1;
124	}
125
126	ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
127	if (ret) {
128		test_msg("Couldn't add to our bitmap entry %d\n", ret);
129		return ret;
130	}
131
132	ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
133	if (ret) {
134		test_msg("Couldn't remove middle chunk %d\n", ret);
135		return ret;
136	}
137
138	/*
139	 * The first bitmap we have starts at offset 0 so the next one is just
140	 * at the end of the first bitmap.
141	 */
142	next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
143
144	/* Test a bit straddling two bitmaps */
145	ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
146					SZ_4M, 1);
147	if (ret) {
148		test_msg("Couldn't add space that straddles two bitmaps %d\n",
149				ret);
150		return ret;
151	}
152
153	ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
154	if (ret) {
155		test_msg("Couldn't remove overlapping space %d\n", ret);
156		return ret;
157	}
158
159	if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
160		test_msg("Left some space when removing overlapping\n");
161		return -1;
162	}
163
164	__btrfs_remove_free_space_cache(cache->free_space_ctl);
165
166	return 0;
167}
168
169/* This is the high grade jackassery */
170static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
 
171{
172	u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
173	int ret;
174
175	test_msg("Running bitmap and extent tests\n");
176
177	/*
178	 * First let's do something simple, an extent at the same offset as the
179	 * bitmap, but the free space completely in the extent and then
180	 * completely in the bitmap.
181	 */
182	ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
183	if (ret) {
184		test_msg("Couldn't create bitmap entry %d\n", ret);
185		return ret;
186	}
187
188	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
189	if (ret) {
190		test_msg("Couldn't add extent entry %d\n", ret);
191		return ret;
192	}
193
194	ret = btrfs_remove_free_space(cache, 0, SZ_1M);
195	if (ret) {
196		test_msg("Couldn't remove extent entry %d\n", ret);
197		return ret;
198	}
199
200	if (test_check_exists(cache, 0, SZ_1M)) {
201		test_msg("Left remnants after our remove\n");
202		return -1;
203	}
204
205	/* Now to add back the extent entry and remove from the bitmap */
206	ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
207	if (ret) {
208		test_msg("Couldn't re-add extent entry %d\n", ret);
209		return ret;
210	}
211
212	ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
213	if (ret) {
214		test_msg("Couldn't remove from bitmap %d\n", ret);
215		return ret;
216	}
217
218	if (test_check_exists(cache, SZ_4M, SZ_1M)) {
219		test_msg("Left remnants in the bitmap\n");
220		return -1;
221	}
222
223	/*
224	 * Ok so a little more evil, extent entry and bitmap at the same offset,
225	 * removing an overlapping chunk.
226	 */
227	ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
228	if (ret) {
229		test_msg("Couldn't add to a bitmap %d\n", ret);
230		return ret;
231	}
232
233	ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
234	if (ret) {
235		test_msg("Couldn't remove overlapping space %d\n", ret);
236		return ret;
237	}
238
239	if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
240		test_msg("Left over pieces after removing overlapping\n");
241		return -1;
242	}
243
244	__btrfs_remove_free_space_cache(cache->free_space_ctl);
245
246	/* Now with the extent entry offset into the bitmap */
247	ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
248	if (ret) {
249		test_msg("Couldn't add space to the bitmap %d\n", ret);
250		return ret;
251	}
252
253	ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
254	if (ret) {
255		test_msg("Couldn't add extent to the cache %d\n", ret);
256		return ret;
257	}
258
259	ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
260	if (ret) {
261		test_msg("Problem removing overlapping space %d\n", ret);
262		return ret;
263	}
264
265	if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
266		test_msg("Left something behind when removing space");
267		return -1;
268	}
269
270	/*
271	 * This has blown up in the past, the extent entry starts before the
272	 * bitmap entry, but we're trying to remove an offset that falls
273	 * completely within the bitmap range and is in both the extent entry
274	 * and the bitmap entry, looks like this
275	 *
276	 *   [ extent ]
277	 *      [ bitmap ]
278	 *        [ del ]
279	 */
280	__btrfs_remove_free_space_cache(cache->free_space_ctl);
281	ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
282	if (ret) {
283		test_msg("Couldn't add bitmap %d\n", ret);
284		return ret;
285	}
286
287	ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
288					5 * SZ_1M, 0);
289	if (ret) {
290		test_msg("Couldn't add extent entry %d\n", ret);
291		return ret;
292	}
293
294	ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
295	if (ret) {
296		test_msg("Failed to free our space %d\n", ret);
297		return ret;
298	}
299
300	if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
301		test_msg("Left stuff over\n");
302		return -1;
303	}
304
305	__btrfs_remove_free_space_cache(cache->free_space_ctl);
306
307	/*
308	 * This blew up before, we have part of the free space in a bitmap and
309	 * then the entirety of the rest of the space in an extent.  This used
310	 * to return -EAGAIN back from btrfs_remove_extent, make sure this
311	 * doesn't happen.
312	 */
313	ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
314	if (ret) {
315		test_msg("Couldn't add bitmap entry %d\n", ret);
316		return ret;
317	}
318
319	ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
320	if (ret) {
321		test_msg("Couldn't add extent entry %d\n", ret);
322		return ret;
323	}
324
325	ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
326	if (ret) {
327		test_msg("Error removing bitmap and extent overlapping %d\n", ret);
328		return ret;
329	}
330
331	__btrfs_remove_free_space_cache(cache->free_space_ctl);
332	return 0;
333}
334
335/* Used by test_steal_space_from_bitmap_to_extent(). */
336static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
337			    struct btrfs_free_space *info)
338{
339	return ctl->free_extents > 0;
340}
341
342/* Used by test_steal_space_from_bitmap_to_extent(). */
343static int
344check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
345			      const int num_extents,
346			      const int num_bitmaps)
347{
348	if (cache->free_space_ctl->free_extents != num_extents) {
349		test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
 
350			 cache->free_space_ctl->free_extents, num_extents);
351		return -EINVAL;
352	}
353	if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
354		test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
 
355			 cache->free_space_ctl->total_bitmaps, num_bitmaps);
356		return -EINVAL;
357	}
358	return 0;
359}
360
361/* Used by test_steal_space_from_bitmap_to_extent(). */
362static int check_cache_empty(struct btrfs_block_group_cache *cache)
363{
364	u64 offset;
365	u64 max_extent_size;
366
367	/*
368	 * Now lets confirm that there's absolutely no free space left to
369	 * allocate.
370	 */
371	if (cache->free_space_ctl->free_space != 0) {
372		test_msg("Cache free space is not 0\n");
373		return -EINVAL;
374	}
375
376	/* And any allocation request, no matter how small, should fail now. */
377	offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
378					    &max_extent_size);
379	if (offset != 0) {
380		test_msg("Space allocation did not fail, returned offset: %llu",
381			 offset);
382		return -EINVAL;
383	}
384
385	/* And no extent nor bitmap entries in the cache anymore. */
386	return check_num_extents_and_bitmaps(cache, 0, 0);
387}
388
389/*
390 * Before we were able to steal free space from a bitmap entry to an extent
391 * entry, we could end up with 2 entries representing a contiguous free space.
392 * One would be an extent entry and the other a bitmap entry. Since in order
393 * to allocate space to a caller we use only 1 entry, we couldn't return that
394 * whole range to the caller if it was requested. This forced the caller to
395 * either assume ENOSPC or perform several smaller space allocations, which
396 * wasn't optimal as they could be spread all over the block group while under
397 * concurrency (extra overhead and fragmentation).
398 *
399 * This stealing approach is benefical, since we always prefer to allocate from
400 * extent entries, both for clustered and non-clustered allocation requests.
 
401 */
402static int
403test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
 
404{
405	int ret;
406	u64 offset;
407	u64 max_extent_size;
408	const struct btrfs_free_space_op test_free_space_ops = {
409		.recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
410		.use_bitmap = test_use_bitmap,
411	};
412	const struct btrfs_free_space_op *orig_free_space_ops;
413
414	test_msg("Running space stealing from bitmap to extent\n");
415
416	/*
417	 * For this test, we want to ensure we end up with an extent entry
418	 * immediately adjacent to a bitmap entry, where the bitmap starts
419	 * at an offset where the extent entry ends. We keep adding and
420	 * removing free space to reach into this state, but to get there
421	 * we need to reach a point where marking new free space doesn't
422	 * result in adding new extent entries or merging the new space
423	 * with existing extent entries - the space ends up being marked
424	 * in an existing bitmap that covers the new free space range.
425	 *
426	 * To get there, we need to reach the threshold defined set at
427	 * cache->free_space_ctl->extents_thresh, which currently is
428	 * 256 extents on a x86_64 system at least, and a few other
429	 * conditions (check free_space_cache.c). Instead of making the
430	 * test much longer and complicated, use a "use_bitmap" operation
431	 * that forces use of bitmaps as soon as we have at least 1
432	 * extent entry.
433	 */
434	orig_free_space_ops = cache->free_space_ctl->op;
435	cache->free_space_ctl->op = &test_free_space_ops;
436
437	/*
438	 * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
439	 */
440	ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
441	if (ret) {
442		test_msg("Couldn't add extent entry %d\n", ret);
443		return ret;
444	}
445
446	/* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
447	ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
448					SZ_128M - SZ_512K, 1);
449	if (ret) {
450		test_msg("Couldn't add bitmap entry %d\n", ret);
451		return ret;
452	}
453
454	ret = check_num_extents_and_bitmaps(cache, 2, 1);
455	if (ret)
456		return ret;
457
458	/*
459	 * Now make only the first 256Kb of the bitmap marked as free, so that
460	 * we end up with only the following ranges marked as free space:
461	 *
462	 * [128Mb - 256Kb, 128Mb - 128Kb[
463	 * [128Mb + 512Kb, 128Mb + 768Kb[
464	 */
465	ret = btrfs_remove_free_space(cache,
466				      SZ_128M + 768 * SZ_1K,
467				      SZ_128M - 768 * SZ_1K);
468	if (ret) {
469		test_msg("Failed to free part of bitmap space %d\n", ret);
470		return ret;
471	}
472
473	/* Confirm that only those 2 ranges are marked as free. */
474	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
475		test_msg("Free space range missing\n");
476		return -ENOENT;
477	}
478	if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
479		test_msg("Free space range missing\n");
480		return -ENOENT;
481	}
482
483	/*
484	 * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
485	 * as free anymore.
486	 */
487	if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
488			      SZ_128M - 768 * SZ_1K)) {
489		test_msg("Bitmap region not removed from space cache\n");
490		return -EINVAL;
491	}
492
493	/*
494	 * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
495	 * covered by the bitmap, isn't marked as free.
496	 */
497	if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
498		test_msg("Invalid bitmap region marked as free\n");
499		return -EINVAL;
500	}
501
502	/*
503	 * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
504	 * by the bitmap too, isn't marked as free either.
505	 */
506	if (test_check_exists(cache, SZ_128M, SZ_256K)) {
507		test_msg("Invalid bitmap region marked as free\n");
508		return -EINVAL;
509	}
510
511	/*
512	 * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
513	 * lets make sure the free space cache marks it as free in the bitmap,
514	 * and doesn't insert a new extent entry to represent this region.
515	 */
516	ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
517	if (ret) {
518		test_msg("Error adding free space: %d\n", ret);
519		return ret;
520	}
521	/* Confirm the region is marked as free. */
522	if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
523		test_msg("Bitmap region not marked as free\n");
524		return -ENOENT;
525	}
526
527	/*
528	 * Confirm that no new extent entries or bitmap entries were added to
529	 * the cache after adding that free space region.
530	 */
531	ret = check_num_extents_and_bitmaps(cache, 2, 1);
532	if (ret)
533		return ret;
534
535	/*
536	 * Now lets add a small free space region to the right of the previous
537	 * one, which is not contiguous with it and is part of the bitmap too.
538	 * The goal is to test that the bitmap entry space stealing doesn't
539	 * steal this space region.
540	 */
541	ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, 4096);
542	if (ret) {
543		test_msg("Error adding free space: %d\n", ret);
544		return ret;
545	}
546
547	/*
548	 * Confirm that no new extent entries or bitmap entries were added to
549	 * the cache after adding that free space region.
550	 */
551	ret = check_num_extents_and_bitmaps(cache, 2, 1);
552	if (ret)
553		return ret;
554
555	/*
556	 * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
557	 * expand the range covered by the existing extent entry that represents
558	 * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
559	 */
560	ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
561	if (ret) {
562		test_msg("Error adding free space: %d\n", ret);
563		return ret;
564	}
565	/* Confirm the region is marked as free. */
566	if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
567		test_msg("Extent region not marked as free\n");
568		return -ENOENT;
569	}
570
571	/*
572	 * Confirm that our extent entry didn't stole all free space from the
573	 * bitmap, because of the small 4Kb free space region.
574	 */
575	ret = check_num_extents_and_bitmaps(cache, 2, 1);
576	if (ret)
577		return ret;
578
579	/*
580	 * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
581	 * space. Without stealing bitmap free space into extent entry space,
582	 * we would have all this free space represented by 2 entries in the
583	 * cache:
584	 *
585	 * extent entry covering range: [128Mb - 256Kb, 128Mb[
586	 * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
587	 *
588	 * Attempting to allocate the whole free space (1Mb) would fail, because
589	 * we can't allocate from multiple entries.
590	 * With the bitmap free space stealing, we get a single extent entry
591	 * that represents the 1Mb free space, and therefore we're able to
592	 * allocate the whole free space at once.
593	 */
594	if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
595		test_msg("Expected region not marked as free\n");
596		return -ENOENT;
597	}
598
599	if (cache->free_space_ctl->free_space != (SZ_1M + 4096)) {
600		test_msg("Cache free space is not 1Mb + 4Kb\n");
601		return -EINVAL;
602	}
603
604	offset = btrfs_find_space_for_alloc(cache,
605					    0, SZ_1M, 0,
606					    &max_extent_size);
607	if (offset != (SZ_128M - SZ_256K)) {
608		test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
 
609			 offset);
610		return -EINVAL;
611	}
612
613	/* All that remains is a 4Kb free space region in a bitmap. Confirm. */
 
 
 
614	ret = check_num_extents_and_bitmaps(cache, 1, 1);
615	if (ret)
616		return ret;
617
618	if (cache->free_space_ctl->free_space != 4096) {
619		test_msg("Cache free space is not 4Kb\n");
620		return -EINVAL;
621	}
622
623	offset = btrfs_find_space_for_alloc(cache,
624					    0, 4096, 0,
625					    &max_extent_size);
626	if (offset != (SZ_128M + SZ_16M)) {
627		test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
628			 offset);
629		return -EINVAL;
630	}
631
632	ret = check_cache_empty(cache);
633	if (ret)
634		return ret;
635
636	__btrfs_remove_free_space_cache(cache->free_space_ctl);
637
638	/*
639	 * Now test a similar scenario, but where our extent entry is located
640	 * to the right of the bitmap entry, so that we can check that stealing
641	 * space from a bitmap to the front of an extent entry works.
642	 */
643
644	/*
645	 * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
646	 */
647	ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
648	if (ret) {
649		test_msg("Couldn't add extent entry %d\n", ret);
650		return ret;
651	}
652
653	/* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
654	ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
655	if (ret) {
656		test_msg("Couldn't add bitmap entry %d\n", ret);
657		return ret;
658	}
659
660	ret = check_num_extents_and_bitmaps(cache, 2, 1);
661	if (ret)
662		return ret;
663
664	/*
665	 * Now make only the last 256Kb of the bitmap marked as free, so that
666	 * we end up with only the following ranges marked as free space:
667	 *
668	 * [128Mb + 128b, 128Mb + 256Kb[
669	 * [128Mb - 768Kb, 128Mb - 512Kb[
670	 */
671	ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
672	if (ret) {
673		test_msg("Failed to free part of bitmap space %d\n", ret);
674		return ret;
675	}
676
677	/* Confirm that only those 2 ranges are marked as free. */
678	if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
679		test_msg("Free space range missing\n");
680		return -ENOENT;
681	}
682	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
683		test_msg("Free space range missing\n");
684		return -ENOENT;
685	}
686
687	/*
688	 * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
689	 * as free anymore.
690	 */
691	if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
692		test_msg("Bitmap region not removed from space cache\n");
693		return -EINVAL;
694	}
695
696	/*
697	 * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
698	 * covered by the bitmap, isn't marked as free.
699	 */
700	if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
701		test_msg("Invalid bitmap region marked as free\n");
702		return -EINVAL;
703	}
704
705	/*
706	 * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
707	 * lets make sure the free space cache marks it as free in the bitmap,
708	 * and doesn't insert a new extent entry to represent this region.
709	 */
710	ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
711	if (ret) {
712		test_msg("Error adding free space: %d\n", ret);
713		return ret;
714	}
715	/* Confirm the region is marked as free. */
716	if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
717		test_msg("Bitmap region not marked as free\n");
718		return -ENOENT;
719	}
720
721	/*
722	 * Confirm that no new extent entries or bitmap entries were added to
723	 * the cache after adding that free space region.
724	 */
725	ret = check_num_extents_and_bitmaps(cache, 2, 1);
726	if (ret)
727		return ret;
728
729	/*
730	 * Now lets add a small free space region to the left of the previous
731	 * one, which is not contiguous with it and is part of the bitmap too.
732	 * The goal is to test that the bitmap entry space stealing doesn't
733	 * steal this space region.
734	 */
735	ret = btrfs_add_free_space(cache, SZ_32M, 8192);
736	if (ret) {
737		test_msg("Error adding free space: %d\n", ret);
738		return ret;
739	}
740
741	/*
742	 * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
743	 * expand the range covered by the existing extent entry that represents
744	 * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
745	 */
746	ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
747	if (ret) {
748		test_msg("Error adding free space: %d\n", ret);
749		return ret;
750	}
751	/* Confirm the region is marked as free. */
752	if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
753		test_msg("Extent region not marked as free\n");
754		return -ENOENT;
755	}
756
757	/*
758	 * Confirm that our extent entry didn't stole all free space from the
759	 * bitmap, because of the small 8Kb free space region.
760	 */
761	ret = check_num_extents_and_bitmaps(cache, 2, 1);
762	if (ret)
763		return ret;
764
765	/*
766	 * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
767	 * space. Without stealing bitmap free space into extent entry space,
768	 * we would have all this free space represented by 2 entries in the
769	 * cache:
770	 *
771	 * extent entry covering range: [128Mb, 128Mb + 256Kb[
772	 * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
773	 *
774	 * Attempting to allocate the whole free space (1Mb) would fail, because
775	 * we can't allocate from multiple entries.
776	 * With the bitmap free space stealing, we get a single extent entry
777	 * that represents the 1Mb free space, and therefore we're able to
778	 * allocate the whole free space at once.
779	 */
780	if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
781		test_msg("Expected region not marked as free\n");
782		return -ENOENT;
783	}
784
785	if (cache->free_space_ctl->free_space != (SZ_1M + 8192)) {
786		test_msg("Cache free space is not 1Mb + 8Kb\n");
787		return -EINVAL;
788	}
789
790	offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
791					    &max_extent_size);
792	if (offset != (SZ_128M - 768 * SZ_1K)) {
793		test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
 
794			 offset);
795		return -EINVAL;
796	}
797
798	/* All that remains is a 8Kb free space region in a bitmap. Confirm. */
 
 
 
799	ret = check_num_extents_and_bitmaps(cache, 1, 1);
800	if (ret)
801		return ret;
802
803	if (cache->free_space_ctl->free_space != 8192) {
804		test_msg("Cache free space is not 8Kb\n");
805		return -EINVAL;
806	}
807
808	offset = btrfs_find_space_for_alloc(cache,
809					    0, 8192, 0,
810					    &max_extent_size);
811	if (offset != SZ_32M) {
812		test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
813			 offset);
814		return -EINVAL;
815	}
816
817	ret = check_cache_empty(cache);
818	if (ret)
819		return ret;
820
821	cache->free_space_ctl->op = orig_free_space_ops;
822	__btrfs_remove_free_space_cache(cache->free_space_ctl);
823
824	return 0;
825}
826
827int btrfs_test_free_space_cache(void)
828{
829	struct btrfs_block_group_cache *cache;
 
830	struct btrfs_root *root = NULL;
831	int ret = -ENOMEM;
832
833	test_msg("Running btrfs free space cache tests\n");
 
 
 
 
 
834
835	cache = btrfs_alloc_dummy_block_group(1024 * 1024 * 1024);
 
 
 
 
 
 
836	if (!cache) {
837		test_msg("Couldn't run the tests\n");
 
838		return 0;
839	}
840
841	root = btrfs_alloc_dummy_root();
842	if (IS_ERR(root)) {
 
843		ret = PTR_ERR(root);
844		goto out;
845	}
846
847	root->fs_info = btrfs_alloc_dummy_fs_info();
848	if (!root->fs_info)
849		goto out;
850
851	root->fs_info->extent_root = root;
852	cache->fs_info = root->fs_info;
853
854	ret = test_extents(cache);
855	if (ret)
856		goto out;
857	ret = test_bitmaps(cache);
858	if (ret)
859		goto out;
860	ret = test_bitmaps_and_extents(cache);
861	if (ret)
862		goto out;
863
864	ret = test_steal_space_from_bitmap_to_extent(cache);
865out:
866	btrfs_free_dummy_block_group(cache);
867	btrfs_free_dummy_root(root);
868	test_msg("Free space cache tests finished\n");
869	return ret;
870}