1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Red Hat. All rights reserved.
6 #include <linux/pagemap.h>
7 #include <linux/sched.h>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
10 #include <linux/math64.h>
11 #include <linux/ratelimit.h>
12 #include <linux/error-injection.h>
13 #include <linux/sched/mm.h>
15 #include "free-space-cache.h"
16 #include "transaction.h"
18 #include "extent_io.h"
19 #include "inode-map.h"
22 #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
23 #define MAX_CACHE_BYTES_PER_GIG SZ_32K
25 struct btrfs_trim_range
{
28 struct list_head list
;
31 static int link_free_space(struct btrfs_free_space_ctl
*ctl
,
32 struct btrfs_free_space
*info
);
33 static void unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
34 struct btrfs_free_space
*info
);
35 static int btrfs_wait_cache_io_root(struct btrfs_root
*root
,
36 struct btrfs_trans_handle
*trans
,
37 struct btrfs_io_ctl
*io_ctl
,
38 struct btrfs_path
*path
);
40 static struct inode
*__lookup_free_space_inode(struct btrfs_root
*root
,
41 struct btrfs_path
*path
,
44 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
46 struct btrfs_key location
;
47 struct btrfs_disk_key disk_key
;
48 struct btrfs_free_space_header
*header
;
49 struct extent_buffer
*leaf
;
50 struct inode
*inode
= NULL
;
54 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
58 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
62 btrfs_release_path(path
);
63 return ERR_PTR(-ENOENT
);
66 leaf
= path
->nodes
[0];
67 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
68 struct btrfs_free_space_header
);
69 btrfs_free_space_key(leaf
, header
, &disk_key
);
70 btrfs_disk_key_to_cpu(&location
, &disk_key
);
71 btrfs_release_path(path
);
74 * We are often under a trans handle at this point, so we need to make
75 * sure NOFS is set to keep us from deadlocking.
77 nofs_flag
= memalloc_nofs_save();
78 inode
= btrfs_iget_path(fs_info
->sb
, &location
, root
, NULL
, path
);
79 btrfs_release_path(path
);
80 memalloc_nofs_restore(nofs_flag
);
84 mapping_set_gfp_mask(inode
->i_mapping
,
85 mapping_gfp_constraint(inode
->i_mapping
,
86 ~(__GFP_FS
| __GFP_HIGHMEM
)));
91 struct inode
*lookup_free_space_inode(struct btrfs_fs_info
*fs_info
,
92 struct btrfs_block_group_cache
93 *block_group
, struct btrfs_path
*path
)
95 struct inode
*inode
= NULL
;
96 u32 flags
= BTRFS_INODE_NODATASUM
| BTRFS_INODE_NODATACOW
;
98 spin_lock(&block_group
->lock
);
99 if (block_group
->inode
)
100 inode
= igrab(block_group
->inode
);
101 spin_unlock(&block_group
->lock
);
105 inode
= __lookup_free_space_inode(fs_info
->tree_root
, path
,
106 block_group
->key
.objectid
);
110 spin_lock(&block_group
->lock
);
111 if (!((BTRFS_I(inode
)->flags
& flags
) == flags
)) {
112 btrfs_info(fs_info
, "Old style space inode found, converting.");
113 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
|
114 BTRFS_INODE_NODATACOW
;
115 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
118 if (!block_group
->iref
) {
119 block_group
->inode
= igrab(inode
);
120 block_group
->iref
= 1;
122 spin_unlock(&block_group
->lock
);
127 static int __create_free_space_inode(struct btrfs_root
*root
,
128 struct btrfs_trans_handle
*trans
,
129 struct btrfs_path
*path
,
132 struct btrfs_key key
;
133 struct btrfs_disk_key disk_key
;
134 struct btrfs_free_space_header
*header
;
135 struct btrfs_inode_item
*inode_item
;
136 struct extent_buffer
*leaf
;
137 u64 flags
= BTRFS_INODE_NOCOMPRESS
| BTRFS_INODE_PREALLOC
;
140 ret
= btrfs_insert_empty_inode(trans
, root
, path
, ino
);
144 /* We inline crc's for the free disk space cache */
145 if (ino
!= BTRFS_FREE_INO_OBJECTID
)
146 flags
|= BTRFS_INODE_NODATASUM
| BTRFS_INODE_NODATACOW
;
148 leaf
= path
->nodes
[0];
149 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
150 struct btrfs_inode_item
);
151 btrfs_item_key(leaf
, &disk_key
, path
->slots
[0]);
152 memzero_extent_buffer(leaf
, (unsigned long)inode_item
,
153 sizeof(*inode_item
));
154 btrfs_set_inode_generation(leaf
, inode_item
, trans
->transid
);
155 btrfs_set_inode_size(leaf
, inode_item
, 0);
156 btrfs_set_inode_nbytes(leaf
, inode_item
, 0);
157 btrfs_set_inode_uid(leaf
, inode_item
, 0);
158 btrfs_set_inode_gid(leaf
, inode_item
, 0);
159 btrfs_set_inode_mode(leaf
, inode_item
, S_IFREG
| 0600);
160 btrfs_set_inode_flags(leaf
, inode_item
, flags
);
161 btrfs_set_inode_nlink(leaf
, inode_item
, 1);
162 btrfs_set_inode_transid(leaf
, inode_item
, trans
->transid
);
163 btrfs_set_inode_block_group(leaf
, inode_item
, offset
);
164 btrfs_mark_buffer_dirty(leaf
);
165 btrfs_release_path(path
);
167 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
170 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
171 sizeof(struct btrfs_free_space_header
));
173 btrfs_release_path(path
);
177 leaf
= path
->nodes
[0];
178 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
179 struct btrfs_free_space_header
);
180 memzero_extent_buffer(leaf
, (unsigned long)header
, sizeof(*header
));
181 btrfs_set_free_space_key(leaf
, header
, &disk_key
);
182 btrfs_mark_buffer_dirty(leaf
);
183 btrfs_release_path(path
);
188 int create_free_space_inode(struct btrfs_fs_info
*fs_info
,
189 struct btrfs_trans_handle
*trans
,
190 struct btrfs_block_group_cache
*block_group
,
191 struct btrfs_path
*path
)
196 ret
= btrfs_find_free_objectid(fs_info
->tree_root
, &ino
);
200 return __create_free_space_inode(fs_info
->tree_root
, trans
, path
, ino
,
201 block_group
->key
.objectid
);
204 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info
*fs_info
,
205 struct btrfs_block_rsv
*rsv
)
210 /* 1 for slack space, 1 for updating the inode */
211 needed_bytes
= btrfs_calc_trunc_metadata_size(fs_info
, 1) +
212 btrfs_calc_trans_metadata_size(fs_info
, 1);
214 spin_lock(&rsv
->lock
);
215 if (rsv
->reserved
< needed_bytes
)
219 spin_unlock(&rsv
->lock
);
223 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle
*trans
,
224 struct btrfs_block_group_cache
*block_group
,
227 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
232 struct btrfs_path
*path
= btrfs_alloc_path();
239 mutex_lock(&trans
->transaction
->cache_write_mutex
);
240 if (!list_empty(&block_group
->io_list
)) {
241 list_del_init(&block_group
->io_list
);
243 btrfs_wait_cache_io(trans
, block_group
, path
);
244 btrfs_put_block_group(block_group
);
248 * now that we've truncated the cache away, its no longer
251 spin_lock(&block_group
->lock
);
252 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
253 spin_unlock(&block_group
->lock
);
254 btrfs_free_path(path
);
257 btrfs_i_size_write(BTRFS_I(inode
), 0);
258 truncate_pagecache(inode
, 0);
261 * We skip the throttling logic for free space cache inodes, so we don't
262 * need to check for -EAGAIN.
264 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
265 0, BTRFS_EXTENT_DATA_KEY
);
269 ret
= btrfs_update_inode(trans
, root
, inode
);
273 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
275 btrfs_abort_transaction(trans
, ret
);
280 static void readahead_cache(struct inode
*inode
)
282 struct file_ra_state
*ra
;
283 unsigned long last_index
;
285 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
289 file_ra_state_init(ra
, inode
->i_mapping
);
290 last_index
= (i_size_read(inode
) - 1) >> PAGE_SHIFT
;
292 page_cache_sync_readahead(inode
->i_mapping
, ra
, NULL
, 0, last_index
);
297 static int io_ctl_init(struct btrfs_io_ctl
*io_ctl
, struct inode
*inode
,
303 num_pages
= DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
);
305 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FREE_INO_OBJECTID
)
308 /* Make sure we can fit our crcs and generation into the first page */
309 if (write
&& check_crcs
&&
310 (num_pages
* sizeof(u32
) + sizeof(u64
)) > PAGE_SIZE
)
313 memset(io_ctl
, 0, sizeof(struct btrfs_io_ctl
));
315 io_ctl
->pages
= kcalloc(num_pages
, sizeof(struct page
*), GFP_NOFS
);
319 io_ctl
->num_pages
= num_pages
;
320 io_ctl
->fs_info
= btrfs_sb(inode
->i_sb
);
321 io_ctl
->check_crcs
= check_crcs
;
322 io_ctl
->inode
= inode
;
326 ALLOW_ERROR_INJECTION(io_ctl_init
, ERRNO
);
328 static void io_ctl_free(struct btrfs_io_ctl
*io_ctl
)
330 kfree(io_ctl
->pages
);
331 io_ctl
->pages
= NULL
;
334 static void io_ctl_unmap_page(struct btrfs_io_ctl
*io_ctl
)
342 static void io_ctl_map_page(struct btrfs_io_ctl
*io_ctl
, int clear
)
344 ASSERT(io_ctl
->index
< io_ctl
->num_pages
);
345 io_ctl
->page
= io_ctl
->pages
[io_ctl
->index
++];
346 io_ctl
->cur
= page_address(io_ctl
->page
);
347 io_ctl
->orig
= io_ctl
->cur
;
348 io_ctl
->size
= PAGE_SIZE
;
350 clear_page(io_ctl
->cur
);
353 static void io_ctl_drop_pages(struct btrfs_io_ctl
*io_ctl
)
357 io_ctl_unmap_page(io_ctl
);
359 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
360 if (io_ctl
->pages
[i
]) {
361 ClearPageChecked(io_ctl
->pages
[i
]);
362 unlock_page(io_ctl
->pages
[i
]);
363 put_page(io_ctl
->pages
[i
]);
368 static int io_ctl_prepare_pages(struct btrfs_io_ctl
*io_ctl
, struct inode
*inode
,
372 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
375 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
376 page
= find_or_create_page(inode
->i_mapping
, i
, mask
);
378 io_ctl_drop_pages(io_ctl
);
381 io_ctl
->pages
[i
] = page
;
382 if (uptodate
&& !PageUptodate(page
)) {
383 btrfs_readpage(NULL
, page
);
385 if (page
->mapping
!= inode
->i_mapping
) {
386 btrfs_err(BTRFS_I(inode
)->root
->fs_info
,
387 "free space cache page truncated");
388 io_ctl_drop_pages(io_ctl
);
391 if (!PageUptodate(page
)) {
392 btrfs_err(BTRFS_I(inode
)->root
->fs_info
,
393 "error reading free space cache");
394 io_ctl_drop_pages(io_ctl
);
400 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
401 clear_page_dirty_for_io(io_ctl
->pages
[i
]);
402 set_page_extent_mapped(io_ctl
->pages
[i
]);
408 static void io_ctl_set_generation(struct btrfs_io_ctl
*io_ctl
, u64 generation
)
412 io_ctl_map_page(io_ctl
, 1);
415 * Skip the csum areas. If we don't check crcs then we just have a
416 * 64bit chunk at the front of the first page.
418 if (io_ctl
->check_crcs
) {
419 io_ctl
->cur
+= (sizeof(u32
) * io_ctl
->num_pages
);
420 io_ctl
->size
-= sizeof(u64
) + (sizeof(u32
) * io_ctl
->num_pages
);
422 io_ctl
->cur
+= sizeof(u64
);
423 io_ctl
->size
-= sizeof(u64
) * 2;
427 *val
= cpu_to_le64(generation
);
428 io_ctl
->cur
+= sizeof(u64
);
431 static int io_ctl_check_generation(struct btrfs_io_ctl
*io_ctl
, u64 generation
)
436 * Skip the crc area. If we don't check crcs then we just have a 64bit
437 * chunk at the front of the first page.
439 if (io_ctl
->check_crcs
) {
440 io_ctl
->cur
+= sizeof(u32
) * io_ctl
->num_pages
;
441 io_ctl
->size
-= sizeof(u64
) +
442 (sizeof(u32
) * io_ctl
->num_pages
);
444 io_ctl
->cur
+= sizeof(u64
);
445 io_ctl
->size
-= sizeof(u64
) * 2;
449 if (le64_to_cpu(*gen
) != generation
) {
450 btrfs_err_rl(io_ctl
->fs_info
,
451 "space cache generation (%llu) does not match inode (%llu)",
453 io_ctl_unmap_page(io_ctl
);
456 io_ctl
->cur
+= sizeof(u64
);
460 static void io_ctl_set_crc(struct btrfs_io_ctl
*io_ctl
, int index
)
466 if (!io_ctl
->check_crcs
) {
467 io_ctl_unmap_page(io_ctl
);
472 offset
= sizeof(u32
) * io_ctl
->num_pages
;
474 crc
= btrfs_csum_data(io_ctl
->orig
+ offset
, crc
,
476 btrfs_csum_final(crc
, (u8
*)&crc
);
477 io_ctl_unmap_page(io_ctl
);
478 tmp
= page_address(io_ctl
->pages
[0]);
483 static int io_ctl_check_crc(struct btrfs_io_ctl
*io_ctl
, int index
)
489 if (!io_ctl
->check_crcs
) {
490 io_ctl_map_page(io_ctl
, 0);
495 offset
= sizeof(u32
) * io_ctl
->num_pages
;
497 tmp
= page_address(io_ctl
->pages
[0]);
501 io_ctl_map_page(io_ctl
, 0);
502 crc
= btrfs_csum_data(io_ctl
->orig
+ offset
, crc
,
504 btrfs_csum_final(crc
, (u8
*)&crc
);
506 btrfs_err_rl(io_ctl
->fs_info
,
507 "csum mismatch on free space cache");
508 io_ctl_unmap_page(io_ctl
);
515 static int io_ctl_add_entry(struct btrfs_io_ctl
*io_ctl
, u64 offset
, u64 bytes
,
518 struct btrfs_free_space_entry
*entry
;
524 entry
->offset
= cpu_to_le64(offset
);
525 entry
->bytes
= cpu_to_le64(bytes
);
526 entry
->type
= (bitmap
) ? BTRFS_FREE_SPACE_BITMAP
:
527 BTRFS_FREE_SPACE_EXTENT
;
528 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
529 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
531 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
534 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
536 /* No more pages to map */
537 if (io_ctl
->index
>= io_ctl
->num_pages
)
540 /* map the next page */
541 io_ctl_map_page(io_ctl
, 1);
545 static int io_ctl_add_bitmap(struct btrfs_io_ctl
*io_ctl
, void *bitmap
)
551 * If we aren't at the start of the current page, unmap this one and
552 * map the next one if there is any left.
554 if (io_ctl
->cur
!= io_ctl
->orig
) {
555 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
556 if (io_ctl
->index
>= io_ctl
->num_pages
)
558 io_ctl_map_page(io_ctl
, 0);
561 copy_page(io_ctl
->cur
, bitmap
);
562 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
563 if (io_ctl
->index
< io_ctl
->num_pages
)
564 io_ctl_map_page(io_ctl
, 0);
568 static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl
*io_ctl
)
571 * If we're not on the boundary we know we've modified the page and we
572 * need to crc the page.
574 if (io_ctl
->cur
!= io_ctl
->orig
)
575 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
577 io_ctl_unmap_page(io_ctl
);
579 while (io_ctl
->index
< io_ctl
->num_pages
) {
580 io_ctl_map_page(io_ctl
, 1);
581 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
585 static int io_ctl_read_entry(struct btrfs_io_ctl
*io_ctl
,
586 struct btrfs_free_space
*entry
, u8
*type
)
588 struct btrfs_free_space_entry
*e
;
592 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
598 entry
->offset
= le64_to_cpu(e
->offset
);
599 entry
->bytes
= le64_to_cpu(e
->bytes
);
601 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
602 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
604 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
607 io_ctl_unmap_page(io_ctl
);
612 static int io_ctl_read_bitmap(struct btrfs_io_ctl
*io_ctl
,
613 struct btrfs_free_space
*entry
)
617 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
621 copy_page(entry
->bitmap
, io_ctl
->cur
);
622 io_ctl_unmap_page(io_ctl
);
628 * Since we attach pinned extents after the fact we can have contiguous sections
629 * of free space that are split up in entries. This poses a problem with the
630 * tree logging stuff since it could have allocated across what appears to be 2
631 * entries since we would have merged the entries when adding the pinned extents
632 * back to the free space cache. So run through the space cache that we just
633 * loaded and merge contiguous entries. This will make the log replay stuff not
634 * blow up and it will make for nicer allocator behavior.
636 static void merge_space_tree(struct btrfs_free_space_ctl
*ctl
)
638 struct btrfs_free_space
*e
, *prev
= NULL
;
642 spin_lock(&ctl
->tree_lock
);
643 for (n
= rb_first(&ctl
->free_space_offset
); n
; n
= rb_next(n
)) {
644 e
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
647 if (e
->bitmap
|| prev
->bitmap
)
649 if (prev
->offset
+ prev
->bytes
== e
->offset
) {
650 unlink_free_space(ctl
, prev
);
651 unlink_free_space(ctl
, e
);
652 prev
->bytes
+= e
->bytes
;
653 kmem_cache_free(btrfs_free_space_cachep
, e
);
654 link_free_space(ctl
, prev
);
656 spin_unlock(&ctl
->tree_lock
);
662 spin_unlock(&ctl
->tree_lock
);
665 static int __load_free_space_cache(struct btrfs_root
*root
, struct inode
*inode
,
666 struct btrfs_free_space_ctl
*ctl
,
667 struct btrfs_path
*path
, u64 offset
)
669 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
670 struct btrfs_free_space_header
*header
;
671 struct extent_buffer
*leaf
;
672 struct btrfs_io_ctl io_ctl
;
673 struct btrfs_key key
;
674 struct btrfs_free_space
*e
, *n
;
682 /* Nothing in the space cache, goodbye */
683 if (!i_size_read(inode
))
686 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
690 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
694 btrfs_release_path(path
);
700 leaf
= path
->nodes
[0];
701 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
702 struct btrfs_free_space_header
);
703 num_entries
= btrfs_free_space_entries(leaf
, header
);
704 num_bitmaps
= btrfs_free_space_bitmaps(leaf
, header
);
705 generation
= btrfs_free_space_generation(leaf
, header
);
706 btrfs_release_path(path
);
708 if (!BTRFS_I(inode
)->generation
) {
710 "the free space cache file (%llu) is invalid, skip it",
715 if (BTRFS_I(inode
)->generation
!= generation
) {
717 "free space inode generation (%llu) did not match free space cache generation (%llu)",
718 BTRFS_I(inode
)->generation
, generation
);
725 ret
= io_ctl_init(&io_ctl
, inode
, 0);
729 readahead_cache(inode
);
731 ret
= io_ctl_prepare_pages(&io_ctl
, inode
, 1);
735 ret
= io_ctl_check_crc(&io_ctl
, 0);
739 ret
= io_ctl_check_generation(&io_ctl
, generation
);
743 while (num_entries
) {
744 e
= kmem_cache_zalloc(btrfs_free_space_cachep
,
749 ret
= io_ctl_read_entry(&io_ctl
, e
, &type
);
751 kmem_cache_free(btrfs_free_space_cachep
, e
);
756 kmem_cache_free(btrfs_free_space_cachep
, e
);
760 if (type
== BTRFS_FREE_SPACE_EXTENT
) {
761 spin_lock(&ctl
->tree_lock
);
762 ret
= link_free_space(ctl
, e
);
763 spin_unlock(&ctl
->tree_lock
);
766 "Duplicate entries in free space cache, dumping");
767 kmem_cache_free(btrfs_free_space_cachep
, e
);
773 e
->bitmap
= kmem_cache_zalloc(
774 btrfs_free_space_bitmap_cachep
, GFP_NOFS
);
777 btrfs_free_space_cachep
, e
);
780 spin_lock(&ctl
->tree_lock
);
781 ret
= link_free_space(ctl
, e
);
782 ctl
->total_bitmaps
++;
783 ctl
->op
->recalc_thresholds(ctl
);
784 spin_unlock(&ctl
->tree_lock
);
787 "Duplicate entries in free space cache, dumping");
788 kmem_cache_free(btrfs_free_space_cachep
, e
);
791 list_add_tail(&e
->list
, &bitmaps
);
797 io_ctl_unmap_page(&io_ctl
);
800 * We add the bitmaps at the end of the entries in order that
801 * the bitmap entries are added to the cache.
803 list_for_each_entry_safe(e
, n
, &bitmaps
, list
) {
804 list_del_init(&e
->list
);
805 ret
= io_ctl_read_bitmap(&io_ctl
, e
);
810 io_ctl_drop_pages(&io_ctl
);
811 merge_space_tree(ctl
);
814 io_ctl_free(&io_ctl
);
817 io_ctl_drop_pages(&io_ctl
);
818 __btrfs_remove_free_space_cache(ctl
);
822 int load_free_space_cache(struct btrfs_fs_info
*fs_info
,
823 struct btrfs_block_group_cache
*block_group
)
825 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
827 struct btrfs_path
*path
;
830 u64 used
= btrfs_block_group_used(&block_group
->item
);
833 * If this block group has been marked to be cleared for one reason or
834 * another then we can't trust the on disk cache, so just return.
836 spin_lock(&block_group
->lock
);
837 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
838 spin_unlock(&block_group
->lock
);
841 spin_unlock(&block_group
->lock
);
843 path
= btrfs_alloc_path();
846 path
->search_commit_root
= 1;
847 path
->skip_locking
= 1;
850 * We must pass a path with search_commit_root set to btrfs_iget in
851 * order to avoid a deadlock when allocating extents for the tree root.
853 * When we are COWing an extent buffer from the tree root, when looking
854 * for a free extent, at extent-tree.c:find_free_extent(), we can find
855 * block group without its free space cache loaded. When we find one
856 * we must load its space cache which requires reading its free space
857 * cache's inode item from the root tree. If this inode item is located
858 * in the same leaf that we started COWing before, then we end up in
859 * deadlock on the extent buffer (trying to read lock it when we
860 * previously write locked it).
862 * It's safe to read the inode item using the commit root because
863 * block groups, once loaded, stay in memory forever (until they are
864 * removed) as well as their space caches once loaded. New block groups
865 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
866 * we will never try to read their inode item while the fs is mounted.
868 inode
= lookup_free_space_inode(fs_info
, block_group
, path
);
870 btrfs_free_path(path
);
874 /* We may have converted the inode and made the cache invalid. */
875 spin_lock(&block_group
->lock
);
876 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
877 spin_unlock(&block_group
->lock
);
878 btrfs_free_path(path
);
881 spin_unlock(&block_group
->lock
);
883 ret
= __load_free_space_cache(fs_info
->tree_root
, inode
, ctl
,
884 path
, block_group
->key
.objectid
);
885 btrfs_free_path(path
);
889 spin_lock(&ctl
->tree_lock
);
890 matched
= (ctl
->free_space
== (block_group
->key
.offset
- used
-
891 block_group
->bytes_super
));
892 spin_unlock(&ctl
->tree_lock
);
895 __btrfs_remove_free_space_cache(ctl
);
897 "block group %llu has wrong amount of free space",
898 block_group
->key
.objectid
);
903 /* This cache is bogus, make sure it gets cleared */
904 spin_lock(&block_group
->lock
);
905 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
906 spin_unlock(&block_group
->lock
);
910 "failed to load free space cache for block group %llu, rebuilding it now",
911 block_group
->key
.objectid
);
918 static noinline_for_stack
919 int write_cache_extent_entries(struct btrfs_io_ctl
*io_ctl
,
920 struct btrfs_free_space_ctl
*ctl
,
921 struct btrfs_block_group_cache
*block_group
,
922 int *entries
, int *bitmaps
,
923 struct list_head
*bitmap_list
)
926 struct btrfs_free_cluster
*cluster
= NULL
;
927 struct btrfs_free_cluster
*cluster_locked
= NULL
;
928 struct rb_node
*node
= rb_first(&ctl
->free_space_offset
);
929 struct btrfs_trim_range
*trim_entry
;
931 /* Get the cluster for this block_group if it exists */
932 if (block_group
&& !list_empty(&block_group
->cluster_list
)) {
933 cluster
= list_entry(block_group
->cluster_list
.next
,
934 struct btrfs_free_cluster
,
938 if (!node
&& cluster
) {
939 cluster_locked
= cluster
;
940 spin_lock(&cluster_locked
->lock
);
941 node
= rb_first(&cluster
->root
);
945 /* Write out the extent entries */
947 struct btrfs_free_space
*e
;
949 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
952 ret
= io_ctl_add_entry(io_ctl
, e
->offset
, e
->bytes
,
958 list_add_tail(&e
->list
, bitmap_list
);
961 node
= rb_next(node
);
962 if (!node
&& cluster
) {
963 node
= rb_first(&cluster
->root
);
964 cluster_locked
= cluster
;
965 spin_lock(&cluster_locked
->lock
);
969 if (cluster_locked
) {
970 spin_unlock(&cluster_locked
->lock
);
971 cluster_locked
= NULL
;
975 * Make sure we don't miss any range that was removed from our rbtree
976 * because trimming is running. Otherwise after a umount+mount (or crash
977 * after committing the transaction) we would leak free space and get
978 * an inconsistent free space cache report from fsck.
980 list_for_each_entry(trim_entry
, &ctl
->trimming_ranges
, list
) {
981 ret
= io_ctl_add_entry(io_ctl
, trim_entry
->start
,
982 trim_entry
->bytes
, NULL
);
991 spin_unlock(&cluster_locked
->lock
);
995 static noinline_for_stack
int
996 update_cache_item(struct btrfs_trans_handle
*trans
,
997 struct btrfs_root
*root
,
999 struct btrfs_path
*path
, u64 offset
,
1000 int entries
, int bitmaps
)
1002 struct btrfs_key key
;
1003 struct btrfs_free_space_header
*header
;
1004 struct extent_buffer
*leaf
;
1007 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
1008 key
.offset
= offset
;
1011 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
1013 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, inode
->i_size
- 1,
1014 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, NULL
);
1017 leaf
= path
->nodes
[0];
1019 struct btrfs_key found_key
;
1020 ASSERT(path
->slots
[0]);
1022 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1023 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
1024 found_key
.offset
!= offset
) {
1025 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0,
1027 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0,
1029 btrfs_release_path(path
);
1034 BTRFS_I(inode
)->generation
= trans
->transid
;
1035 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
1036 struct btrfs_free_space_header
);
1037 btrfs_set_free_space_entries(leaf
, header
, entries
);
1038 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
1039 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
1040 btrfs_mark_buffer_dirty(leaf
);
1041 btrfs_release_path(path
);
1049 static noinline_for_stack
int
1050 write_pinned_extent_entries(struct btrfs_fs_info
*fs_info
,
1051 struct btrfs_block_group_cache
*block_group
,
1052 struct btrfs_io_ctl
*io_ctl
,
1055 u64 start
, extent_start
, extent_end
, len
;
1056 struct extent_io_tree
*unpin
= NULL
;
1063 * We want to add any pinned extents to our free space cache
1064 * so we don't leak the space
1066 * We shouldn't have switched the pinned extents yet so this is the
1069 unpin
= fs_info
->pinned_extents
;
1071 start
= block_group
->key
.objectid
;
1073 while (start
< block_group
->key
.objectid
+ block_group
->key
.offset
) {
1074 ret
= find_first_extent_bit(unpin
, start
,
1075 &extent_start
, &extent_end
,
1076 EXTENT_DIRTY
, NULL
);
1080 /* This pinned extent is out of our range */
1081 if (extent_start
>= block_group
->key
.objectid
+
1082 block_group
->key
.offset
)
1085 extent_start
= max(extent_start
, start
);
1086 extent_end
= min(block_group
->key
.objectid
+
1087 block_group
->key
.offset
, extent_end
+ 1);
1088 len
= extent_end
- extent_start
;
1091 ret
= io_ctl_add_entry(io_ctl
, extent_start
, len
, NULL
);
1101 static noinline_for_stack
int
1102 write_bitmap_entries(struct btrfs_io_ctl
*io_ctl
, struct list_head
*bitmap_list
)
1104 struct btrfs_free_space
*entry
, *next
;
1107 /* Write out the bitmaps */
1108 list_for_each_entry_safe(entry
, next
, bitmap_list
, list
) {
1109 ret
= io_ctl_add_bitmap(io_ctl
, entry
->bitmap
);
1112 list_del_init(&entry
->list
);
1118 static int flush_dirty_cache(struct inode
*inode
)
1122 ret
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1124 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, inode
->i_size
- 1,
1125 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, NULL
);
1130 static void noinline_for_stack
1131 cleanup_bitmap_list(struct list_head
*bitmap_list
)
1133 struct btrfs_free_space
*entry
, *next
;
1135 list_for_each_entry_safe(entry
, next
, bitmap_list
, list
)
1136 list_del_init(&entry
->list
);
1139 static void noinline_for_stack
1140 cleanup_write_cache_enospc(struct inode
*inode
,
1141 struct btrfs_io_ctl
*io_ctl
,
1142 struct extent_state
**cached_state
)
1144 io_ctl_drop_pages(io_ctl
);
1145 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
1146 i_size_read(inode
) - 1, cached_state
);
1149 static int __btrfs_wait_cache_io(struct btrfs_root
*root
,
1150 struct btrfs_trans_handle
*trans
,
1151 struct btrfs_block_group_cache
*block_group
,
1152 struct btrfs_io_ctl
*io_ctl
,
1153 struct btrfs_path
*path
, u64 offset
)
1156 struct inode
*inode
= io_ctl
->inode
;
1161 /* Flush the dirty pages in the cache file. */
1162 ret
= flush_dirty_cache(inode
);
1166 /* Update the cache item to tell everyone this cache file is valid. */
1167 ret
= update_cache_item(trans
, root
, inode
, path
, offset
,
1168 io_ctl
->entries
, io_ctl
->bitmaps
);
1170 io_ctl_free(io_ctl
);
1172 invalidate_inode_pages2(inode
->i_mapping
);
1173 BTRFS_I(inode
)->generation
= 0;
1176 btrfs_err(root
->fs_info
,
1177 "failed to write free space cache for block group %llu",
1178 block_group
->key
.objectid
);
1182 btrfs_update_inode(trans
, root
, inode
);
1185 /* the dirty list is protected by the dirty_bgs_lock */
1186 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
1188 /* the disk_cache_state is protected by the block group lock */
1189 spin_lock(&block_group
->lock
);
1192 * only mark this as written if we didn't get put back on
1193 * the dirty list while waiting for IO. Otherwise our
1194 * cache state won't be right, and we won't get written again
1196 if (!ret
&& list_empty(&block_group
->dirty_list
))
1197 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
1199 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
1201 spin_unlock(&block_group
->lock
);
1202 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
1203 io_ctl
->inode
= NULL
;
1211 static int btrfs_wait_cache_io_root(struct btrfs_root
*root
,
1212 struct btrfs_trans_handle
*trans
,
1213 struct btrfs_io_ctl
*io_ctl
,
1214 struct btrfs_path
*path
)
1216 return __btrfs_wait_cache_io(root
, trans
, NULL
, io_ctl
, path
, 0);
1219 int btrfs_wait_cache_io(struct btrfs_trans_handle
*trans
,
1220 struct btrfs_block_group_cache
*block_group
,
1221 struct btrfs_path
*path
)
1223 return __btrfs_wait_cache_io(block_group
->fs_info
->tree_root
, trans
,
1224 block_group
, &block_group
->io_ctl
,
1225 path
, block_group
->key
.objectid
);
1229 * __btrfs_write_out_cache - write out cached info to an inode
1230 * @root - the root the inode belongs to
1231 * @ctl - the free space cache we are going to write out
1232 * @block_group - the block_group for this cache if it belongs to a block_group
1233 * @trans - the trans handle
1235 * This function writes out a free space cache struct to disk for quick recovery
1236 * on mount. This will return 0 if it was successful in writing the cache out,
1237 * or an errno if it was not.
1239 static int __btrfs_write_out_cache(struct btrfs_root
*root
, struct inode
*inode
,
1240 struct btrfs_free_space_ctl
*ctl
,
1241 struct btrfs_block_group_cache
*block_group
,
1242 struct btrfs_io_ctl
*io_ctl
,
1243 struct btrfs_trans_handle
*trans
)
1245 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1246 struct extent_state
*cached_state
= NULL
;
1247 LIST_HEAD(bitmap_list
);
1253 if (!i_size_read(inode
))
1256 WARN_ON(io_ctl
->pages
);
1257 ret
= io_ctl_init(io_ctl
, inode
, 1);
1261 if (block_group
&& (block_group
->flags
& BTRFS_BLOCK_GROUP_DATA
)) {
1262 down_write(&block_group
->data_rwsem
);
1263 spin_lock(&block_group
->lock
);
1264 if (block_group
->delalloc_bytes
) {
1265 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
1266 spin_unlock(&block_group
->lock
);
1267 up_write(&block_group
->data_rwsem
);
1268 BTRFS_I(inode
)->generation
= 0;
1273 spin_unlock(&block_group
->lock
);
1276 /* Lock all pages first so we can lock the extent safely. */
1277 ret
= io_ctl_prepare_pages(io_ctl
, inode
, 0);
1281 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
1284 io_ctl_set_generation(io_ctl
, trans
->transid
);
1286 mutex_lock(&ctl
->cache_writeout_mutex
);
1287 /* Write out the extent entries in the free space cache */
1288 spin_lock(&ctl
->tree_lock
);
1289 ret
= write_cache_extent_entries(io_ctl
, ctl
,
1290 block_group
, &entries
, &bitmaps
,
1293 goto out_nospc_locked
;
1296 * Some spaces that are freed in the current transaction are pinned,
1297 * they will be added into free space cache after the transaction is
1298 * committed, we shouldn't lose them.
1300 * If this changes while we are working we'll get added back to
1301 * the dirty list and redo it. No locking needed
1303 ret
= write_pinned_extent_entries(fs_info
, block_group
,
1306 goto out_nospc_locked
;
1309 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1310 * locked while doing it because a concurrent trim can be manipulating
1311 * or freeing the bitmap.
1313 ret
= write_bitmap_entries(io_ctl
, &bitmap_list
);
1314 spin_unlock(&ctl
->tree_lock
);
1315 mutex_unlock(&ctl
->cache_writeout_mutex
);
1319 /* Zero out the rest of the pages just to make sure */
1320 io_ctl_zero_remaining_pages(io_ctl
);
1322 /* Everything is written out, now we dirty the pages in the file. */
1323 ret
= btrfs_dirty_pages(inode
, io_ctl
->pages
, io_ctl
->num_pages
, 0,
1324 i_size_read(inode
), &cached_state
);
1328 if (block_group
&& (block_group
->flags
& BTRFS_BLOCK_GROUP_DATA
))
1329 up_write(&block_group
->data_rwsem
);
1331 * Release the pages and unlock the extent, we will flush
1334 io_ctl_drop_pages(io_ctl
);
1336 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
1337 i_size_read(inode
) - 1, &cached_state
);
1340 * at this point the pages are under IO and we're happy,
1341 * The caller is responsible for waiting on them and updating the
1342 * the cache and the inode
1344 io_ctl
->entries
= entries
;
1345 io_ctl
->bitmaps
= bitmaps
;
1347 ret
= btrfs_fdatawrite_range(inode
, 0, (u64
)-1);
1354 io_ctl
->inode
= NULL
;
1355 io_ctl_free(io_ctl
);
1357 invalidate_inode_pages2(inode
->i_mapping
);
1358 BTRFS_I(inode
)->generation
= 0;
1360 btrfs_update_inode(trans
, root
, inode
);
1366 cleanup_bitmap_list(&bitmap_list
);
1367 spin_unlock(&ctl
->tree_lock
);
1368 mutex_unlock(&ctl
->cache_writeout_mutex
);
1371 cleanup_write_cache_enospc(inode
, io_ctl
, &cached_state
);
1374 if (block_group
&& (block_group
->flags
& BTRFS_BLOCK_GROUP_DATA
))
1375 up_write(&block_group
->data_rwsem
);
1380 int btrfs_write_out_cache(struct btrfs_fs_info
*fs_info
,
1381 struct btrfs_trans_handle
*trans
,
1382 struct btrfs_block_group_cache
*block_group
,
1383 struct btrfs_path
*path
)
1385 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1386 struct inode
*inode
;
1389 spin_lock(&block_group
->lock
);
1390 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
1391 spin_unlock(&block_group
->lock
);
1394 spin_unlock(&block_group
->lock
);
1396 inode
= lookup_free_space_inode(fs_info
, block_group
, path
);
1400 ret
= __btrfs_write_out_cache(fs_info
->tree_root
, inode
, ctl
,
1401 block_group
, &block_group
->io_ctl
, trans
);
1405 "failed to write free space cache for block group %llu",
1406 block_group
->key
.objectid
);
1408 spin_lock(&block_group
->lock
);
1409 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
1410 spin_unlock(&block_group
->lock
);
1412 block_group
->io_ctl
.inode
= NULL
;
1417 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1418 * to wait for IO and put the inode
1424 static inline unsigned long offset_to_bit(u64 bitmap_start
, u32 unit
,
1427 ASSERT(offset
>= bitmap_start
);
1428 offset
-= bitmap_start
;
1429 return (unsigned long)(div_u64(offset
, unit
));
1432 static inline unsigned long bytes_to_bits(u64 bytes
, u32 unit
)
1434 return (unsigned long)(div_u64(bytes
, unit
));
1437 static inline u64
offset_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1441 u64 bytes_per_bitmap
;
1443 bytes_per_bitmap
= BITS_PER_BITMAP
* ctl
->unit
;
1444 bitmap_start
= offset
- ctl
->start
;
1445 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
1446 bitmap_start
*= bytes_per_bitmap
;
1447 bitmap_start
+= ctl
->start
;
1449 return bitmap_start
;
1452 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
1453 struct rb_node
*node
, int bitmap
)
1455 struct rb_node
**p
= &root
->rb_node
;
1456 struct rb_node
*parent
= NULL
;
1457 struct btrfs_free_space
*info
;
1461 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
1463 if (offset
< info
->offset
) {
1465 } else if (offset
> info
->offset
) {
1466 p
= &(*p
)->rb_right
;
1469 * we could have a bitmap entry and an extent entry
1470 * share the same offset. If this is the case, we want
1471 * the extent entry to always be found first if we do a
1472 * linear search through the tree, since we want to have
1473 * the quickest allocation time, and allocating from an
1474 * extent is faster than allocating from a bitmap. So
1475 * if we're inserting a bitmap and we find an entry at
1476 * this offset, we want to go right, or after this entry
1477 * logically. If we are inserting an extent and we've
1478 * found a bitmap, we want to go left, or before
1486 p
= &(*p
)->rb_right
;
1488 if (!info
->bitmap
) {
1497 rb_link_node(node
, parent
, p
);
1498 rb_insert_color(node
, root
);
1504 * searches the tree for the given offset.
1506 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1507 * want a section that has at least bytes size and comes at or after the given
1510 static struct btrfs_free_space
*
1511 tree_search_offset(struct btrfs_free_space_ctl
*ctl
,
1512 u64 offset
, int bitmap_only
, int fuzzy
)
1514 struct rb_node
*n
= ctl
->free_space_offset
.rb_node
;
1515 struct btrfs_free_space
*entry
, *prev
= NULL
;
1517 /* find entry that is closest to the 'offset' */
1524 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1527 if (offset
< entry
->offset
)
1529 else if (offset
> entry
->offset
)
1542 * bitmap entry and extent entry may share same offset,
1543 * in that case, bitmap entry comes after extent entry.
1548 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1549 if (entry
->offset
!= offset
)
1552 WARN_ON(!entry
->bitmap
);
1555 if (entry
->bitmap
) {
1557 * if previous extent entry covers the offset,
1558 * we should return it instead of the bitmap entry
1560 n
= rb_prev(&entry
->offset_index
);
1562 prev
= rb_entry(n
, struct btrfs_free_space
,
1564 if (!prev
->bitmap
&&
1565 prev
->offset
+ prev
->bytes
> offset
)
1575 /* find last entry before the 'offset' */
1577 if (entry
->offset
> offset
) {
1578 n
= rb_prev(&entry
->offset_index
);
1580 entry
= rb_entry(n
, struct btrfs_free_space
,
1582 ASSERT(entry
->offset
<= offset
);
1591 if (entry
->bitmap
) {
1592 n
= rb_prev(&entry
->offset_index
);
1594 prev
= rb_entry(n
, struct btrfs_free_space
,
1596 if (!prev
->bitmap
&&
1597 prev
->offset
+ prev
->bytes
> offset
)
1600 if (entry
->offset
+ BITS_PER_BITMAP
* ctl
->unit
> offset
)
1602 } else if (entry
->offset
+ entry
->bytes
> offset
)
1609 if (entry
->bitmap
) {
1610 if (entry
->offset
+ BITS_PER_BITMAP
*
1614 if (entry
->offset
+ entry
->bytes
> offset
)
1618 n
= rb_next(&entry
->offset_index
);
1621 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1627 __unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1628 struct btrfs_free_space
*info
)
1630 rb_erase(&info
->offset_index
, &ctl
->free_space_offset
);
1631 ctl
->free_extents
--;
1634 static void unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1635 struct btrfs_free_space
*info
)
1637 __unlink_free_space(ctl
, info
);
1638 ctl
->free_space
-= info
->bytes
;
1641 static int link_free_space(struct btrfs_free_space_ctl
*ctl
,
1642 struct btrfs_free_space
*info
)
1646 ASSERT(info
->bytes
|| info
->bitmap
);
1647 ret
= tree_insert_offset(&ctl
->free_space_offset
, info
->offset
,
1648 &info
->offset_index
, (info
->bitmap
!= NULL
));
1652 ctl
->free_space
+= info
->bytes
;
1653 ctl
->free_extents
++;
1657 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
1659 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1663 u64 size
= block_group
->key
.offset
;
1664 u64 bytes_per_bg
= BITS_PER_BITMAP
* ctl
->unit
;
1665 u64 max_bitmaps
= div64_u64(size
+ bytes_per_bg
- 1, bytes_per_bg
);
1667 max_bitmaps
= max_t(u64
, max_bitmaps
, 1);
1669 ASSERT(ctl
->total_bitmaps
<= max_bitmaps
);
1672 * The goal is to keep the total amount of memory used per 1gb of space
1673 * at or below 32k, so we need to adjust how much memory we allow to be
1674 * used by extent based free space tracking
1677 max_bytes
= MAX_CACHE_BYTES_PER_GIG
;
1679 max_bytes
= MAX_CACHE_BYTES_PER_GIG
* div_u64(size
, SZ_1G
);
1682 * we want to account for 1 more bitmap than what we have so we can make
1683 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1684 * we add more bitmaps.
1686 bitmap_bytes
= (ctl
->total_bitmaps
+ 1) * ctl
->unit
;
1688 if (bitmap_bytes
>= max_bytes
) {
1689 ctl
->extents_thresh
= 0;
1694 * we want the extent entry threshold to always be at most 1/2 the max
1695 * bytes we can have, or whatever is less than that.
1697 extent_bytes
= max_bytes
- bitmap_bytes
;
1698 extent_bytes
= min_t(u64
, extent_bytes
, max_bytes
>> 1);
1700 ctl
->extents_thresh
=
1701 div_u64(extent_bytes
, sizeof(struct btrfs_free_space
));
1704 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1705 struct btrfs_free_space
*info
,
1706 u64 offset
, u64 bytes
)
1708 unsigned long start
, count
;
1710 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1711 count
= bytes_to_bits(bytes
, ctl
->unit
);
1712 ASSERT(start
+ count
<= BITS_PER_BITMAP
);
1714 bitmap_clear(info
->bitmap
, start
, count
);
1716 info
->bytes
-= bytes
;
1717 if (info
->max_extent_size
> ctl
->unit
)
1718 info
->max_extent_size
= 0;
1721 static void bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1722 struct btrfs_free_space
*info
, u64 offset
,
1725 __bitmap_clear_bits(ctl
, info
, offset
, bytes
);
1726 ctl
->free_space
-= bytes
;
1729 static void bitmap_set_bits(struct btrfs_free_space_ctl
*ctl
,
1730 struct btrfs_free_space
*info
, u64 offset
,
1733 unsigned long start
, count
;
1735 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1736 count
= bytes_to_bits(bytes
, ctl
->unit
);
1737 ASSERT(start
+ count
<= BITS_PER_BITMAP
);
1739 bitmap_set(info
->bitmap
, start
, count
);
1741 info
->bytes
+= bytes
;
1742 ctl
->free_space
+= bytes
;
1746 * If we can not find suitable extent, we will use bytes to record
1747 * the size of the max extent.
1749 static int search_bitmap(struct btrfs_free_space_ctl
*ctl
,
1750 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1751 u64
*bytes
, bool for_alloc
)
1753 unsigned long found_bits
= 0;
1754 unsigned long max_bits
= 0;
1755 unsigned long bits
, i
;
1756 unsigned long next_zero
;
1757 unsigned long extent_bits
;
1760 * Skip searching the bitmap if we don't have a contiguous section that
1761 * is large enough for this allocation.
1764 bitmap_info
->max_extent_size
&&
1765 bitmap_info
->max_extent_size
< *bytes
) {
1766 *bytes
= bitmap_info
->max_extent_size
;
1770 i
= offset_to_bit(bitmap_info
->offset
, ctl
->unit
,
1771 max_t(u64
, *offset
, bitmap_info
->offset
));
1772 bits
= bytes_to_bits(*bytes
, ctl
->unit
);
1774 for_each_set_bit_from(i
, bitmap_info
->bitmap
, BITS_PER_BITMAP
) {
1775 if (for_alloc
&& bits
== 1) {
1779 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1780 BITS_PER_BITMAP
, i
);
1781 extent_bits
= next_zero
- i
;
1782 if (extent_bits
>= bits
) {
1783 found_bits
= extent_bits
;
1785 } else if (extent_bits
> max_bits
) {
1786 max_bits
= extent_bits
;
1792 *offset
= (u64
)(i
* ctl
->unit
) + bitmap_info
->offset
;
1793 *bytes
= (u64
)(found_bits
) * ctl
->unit
;
1797 *bytes
= (u64
)(max_bits
) * ctl
->unit
;
1798 bitmap_info
->max_extent_size
= *bytes
;
1802 static inline u64
get_max_extent_size(struct btrfs_free_space
*entry
)
1805 return entry
->max_extent_size
;
1806 return entry
->bytes
;
1809 /* Cache the size of the max extent in bytes */
1810 static struct btrfs_free_space
*
1811 find_free_space(struct btrfs_free_space_ctl
*ctl
, u64
*offset
, u64
*bytes
,
1812 unsigned long align
, u64
*max_extent_size
)
1814 struct btrfs_free_space
*entry
;
1815 struct rb_node
*node
;
1820 if (!ctl
->free_space_offset
.rb_node
)
1823 entry
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, *offset
), 0, 1);
1827 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1828 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1829 if (entry
->bytes
< *bytes
) {
1830 *max_extent_size
= max(get_max_extent_size(entry
),
1835 /* make sure the space returned is big enough
1836 * to match our requested alignment
1838 if (*bytes
>= align
) {
1839 tmp
= entry
->offset
- ctl
->start
+ align
- 1;
1840 tmp
= div64_u64(tmp
, align
);
1841 tmp
= tmp
* align
+ ctl
->start
;
1842 align_off
= tmp
- entry
->offset
;
1845 tmp
= entry
->offset
;
1848 if (entry
->bytes
< *bytes
+ align_off
) {
1849 *max_extent_size
= max(get_max_extent_size(entry
),
1854 if (entry
->bitmap
) {
1857 ret
= search_bitmap(ctl
, entry
, &tmp
, &size
, true);
1864 max(get_max_extent_size(entry
),
1871 *bytes
= entry
->bytes
- align_off
;
1878 static void add_new_bitmap(struct btrfs_free_space_ctl
*ctl
,
1879 struct btrfs_free_space
*info
, u64 offset
)
1881 info
->offset
= offset_to_bitmap(ctl
, offset
);
1883 INIT_LIST_HEAD(&info
->list
);
1884 link_free_space(ctl
, info
);
1885 ctl
->total_bitmaps
++;
1887 ctl
->op
->recalc_thresholds(ctl
);
1890 static void free_bitmap(struct btrfs_free_space_ctl
*ctl
,
1891 struct btrfs_free_space
*bitmap_info
)
1893 unlink_free_space(ctl
, bitmap_info
);
1894 kmem_cache_free(btrfs_free_space_bitmap_cachep
, bitmap_info
->bitmap
);
1895 kmem_cache_free(btrfs_free_space_cachep
, bitmap_info
);
1896 ctl
->total_bitmaps
--;
1897 ctl
->op
->recalc_thresholds(ctl
);
1900 static noinline
int remove_from_bitmap(struct btrfs_free_space_ctl
*ctl
,
1901 struct btrfs_free_space
*bitmap_info
,
1902 u64
*offset
, u64
*bytes
)
1905 u64 search_start
, search_bytes
;
1909 end
= bitmap_info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
) - 1;
1912 * We need to search for bits in this bitmap. We could only cover some
1913 * of the extent in this bitmap thanks to how we add space, so we need
1914 * to search for as much as it as we can and clear that amount, and then
1915 * go searching for the next bit.
1917 search_start
= *offset
;
1918 search_bytes
= ctl
->unit
;
1919 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1920 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
, &search_bytes
,
1922 if (ret
< 0 || search_start
!= *offset
)
1925 /* We may have found more bits than what we need */
1926 search_bytes
= min(search_bytes
, *bytes
);
1928 /* Cannot clear past the end of the bitmap */
1929 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1931 bitmap_clear_bits(ctl
, bitmap_info
, search_start
, search_bytes
);
1932 *offset
+= search_bytes
;
1933 *bytes
-= search_bytes
;
1936 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1937 if (!bitmap_info
->bytes
)
1938 free_bitmap(ctl
, bitmap_info
);
1941 * no entry after this bitmap, but we still have bytes to
1942 * remove, so something has gone wrong.
1947 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1951 * if the next entry isn't a bitmap we need to return to let the
1952 * extent stuff do its work.
1954 if (!bitmap_info
->bitmap
)
1958 * Ok the next item is a bitmap, but it may not actually hold
1959 * the information for the rest of this free space stuff, so
1960 * look for it, and if we don't find it return so we can try
1961 * everything over again.
1963 search_start
= *offset
;
1964 search_bytes
= ctl
->unit
;
1965 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
,
1966 &search_bytes
, false);
1967 if (ret
< 0 || search_start
!= *offset
)
1971 } else if (!bitmap_info
->bytes
)
1972 free_bitmap(ctl
, bitmap_info
);
1977 static u64
add_bytes_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1978 struct btrfs_free_space
*info
, u64 offset
,
1981 u64 bytes_to_set
= 0;
1984 end
= info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
);
1986 bytes_to_set
= min(end
- offset
, bytes
);
1988 bitmap_set_bits(ctl
, info
, offset
, bytes_to_set
);
1991 * We set some bytes, we have no idea what the max extent size is
1994 info
->max_extent_size
= 0;
1996 return bytes_to_set
;
2000 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
2001 struct btrfs_free_space
*info
)
2003 struct btrfs_block_group_cache
*block_group
= ctl
->private;
2004 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2005 bool forced
= false;
2007 #ifdef CONFIG_BTRFS_DEBUG
2008 if (btrfs_should_fragment_free_space(block_group
))
2013 * If we are below the extents threshold then we can add this as an
2014 * extent, and don't have to deal with the bitmap
2016 if (!forced
&& ctl
->free_extents
< ctl
->extents_thresh
) {
2018 * If this block group has some small extents we don't want to
2019 * use up all of our free slots in the cache with them, we want
2020 * to reserve them to larger extents, however if we have plenty
2021 * of cache left then go ahead an dadd them, no sense in adding
2022 * the overhead of a bitmap if we don't have to.
2024 if (info
->bytes
<= fs_info
->sectorsize
* 4) {
2025 if (ctl
->free_extents
* 2 <= ctl
->extents_thresh
)
2033 * The original block groups from mkfs can be really small, like 8
2034 * megabytes, so don't bother with a bitmap for those entries. However
2035 * some block groups can be smaller than what a bitmap would cover but
2036 * are still large enough that they could overflow the 32k memory limit,
2037 * so allow those block groups to still be allowed to have a bitmap
2040 if (((BITS_PER_BITMAP
* ctl
->unit
) >> 1) > block_group
->key
.offset
)
2046 static const struct btrfs_free_space_op free_space_op
= {
2047 .recalc_thresholds
= recalculate_thresholds
,
2048 .use_bitmap
= use_bitmap
,
2051 static int insert_into_bitmap(struct btrfs_free_space_ctl
*ctl
,
2052 struct btrfs_free_space
*info
)
2054 struct btrfs_free_space
*bitmap_info
;
2055 struct btrfs_block_group_cache
*block_group
= NULL
;
2057 u64 bytes
, offset
, bytes_added
;
2060 bytes
= info
->bytes
;
2061 offset
= info
->offset
;
2063 if (!ctl
->op
->use_bitmap(ctl
, info
))
2066 if (ctl
->op
== &free_space_op
)
2067 block_group
= ctl
->private;
2070 * Since we link bitmaps right into the cluster we need to see if we
2071 * have a cluster here, and if so and it has our bitmap we need to add
2072 * the free space to that bitmap.
2074 if (block_group
&& !list_empty(&block_group
->cluster_list
)) {
2075 struct btrfs_free_cluster
*cluster
;
2076 struct rb_node
*node
;
2077 struct btrfs_free_space
*entry
;
2079 cluster
= list_entry(block_group
->cluster_list
.next
,
2080 struct btrfs_free_cluster
,
2082 spin_lock(&cluster
->lock
);
2083 node
= rb_first(&cluster
->root
);
2085 spin_unlock(&cluster
->lock
);
2086 goto no_cluster_bitmap
;
2089 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2090 if (!entry
->bitmap
) {
2091 spin_unlock(&cluster
->lock
);
2092 goto no_cluster_bitmap
;
2095 if (entry
->offset
== offset_to_bitmap(ctl
, offset
)) {
2096 bytes_added
= add_bytes_to_bitmap(ctl
, entry
,
2098 bytes
-= bytes_added
;
2099 offset
+= bytes_added
;
2101 spin_unlock(&cluster
->lock
);
2109 bitmap_info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
2116 bytes_added
= add_bytes_to_bitmap(ctl
, bitmap_info
, offset
, bytes
);
2117 bytes
-= bytes_added
;
2118 offset
+= bytes_added
;
2128 if (info
&& info
->bitmap
) {
2129 add_new_bitmap(ctl
, info
, offset
);
2134 spin_unlock(&ctl
->tree_lock
);
2136 /* no pre-allocated info, allocate a new one */
2138 info
= kmem_cache_zalloc(btrfs_free_space_cachep
,
2141 spin_lock(&ctl
->tree_lock
);
2147 /* allocate the bitmap */
2148 info
->bitmap
= kmem_cache_zalloc(btrfs_free_space_bitmap_cachep
,
2150 spin_lock(&ctl
->tree_lock
);
2151 if (!info
->bitmap
) {
2161 kmem_cache_free(btrfs_free_space_bitmap_cachep
,
2163 kmem_cache_free(btrfs_free_space_cachep
, info
);
2169 static bool try_merge_free_space(struct btrfs_free_space_ctl
*ctl
,
2170 struct btrfs_free_space
*info
, bool update_stat
)
2172 struct btrfs_free_space
*left_info
;
2173 struct btrfs_free_space
*right_info
;
2174 bool merged
= false;
2175 u64 offset
= info
->offset
;
2176 u64 bytes
= info
->bytes
;
2179 * first we want to see if there is free space adjacent to the range we
2180 * are adding, if there is remove that struct and add a new one to
2181 * cover the entire range
2183 right_info
= tree_search_offset(ctl
, offset
+ bytes
, 0, 0);
2184 if (right_info
&& rb_prev(&right_info
->offset_index
))
2185 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
2186 struct btrfs_free_space
, offset_index
);
2188 left_info
= tree_search_offset(ctl
, offset
- 1, 0, 0);
2190 if (right_info
&& !right_info
->bitmap
) {
2192 unlink_free_space(ctl
, right_info
);
2194 __unlink_free_space(ctl
, right_info
);
2195 info
->bytes
+= right_info
->bytes
;
2196 kmem_cache_free(btrfs_free_space_cachep
, right_info
);
2200 if (left_info
&& !left_info
->bitmap
&&
2201 left_info
->offset
+ left_info
->bytes
== offset
) {
2203 unlink_free_space(ctl
, left_info
);
2205 __unlink_free_space(ctl
, left_info
);
2206 info
->offset
= left_info
->offset
;
2207 info
->bytes
+= left_info
->bytes
;
2208 kmem_cache_free(btrfs_free_space_cachep
, left_info
);
2215 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl
*ctl
,
2216 struct btrfs_free_space
*info
,
2219 struct btrfs_free_space
*bitmap
;
2222 const u64 end
= info
->offset
+ info
->bytes
;
2223 const u64 bitmap_offset
= offset_to_bitmap(ctl
, end
);
2226 bitmap
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2230 i
= offset_to_bit(bitmap
->offset
, ctl
->unit
, end
);
2231 j
= find_next_zero_bit(bitmap
->bitmap
, BITS_PER_BITMAP
, i
);
2234 bytes
= (j
- i
) * ctl
->unit
;
2235 info
->bytes
+= bytes
;
2238 bitmap_clear_bits(ctl
, bitmap
, end
, bytes
);
2240 __bitmap_clear_bits(ctl
, bitmap
, end
, bytes
);
2243 free_bitmap(ctl
, bitmap
);
2248 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl
*ctl
,
2249 struct btrfs_free_space
*info
,
2252 struct btrfs_free_space
*bitmap
;
2256 unsigned long prev_j
;
2259 bitmap_offset
= offset_to_bitmap(ctl
, info
->offset
);
2260 /* If we're on a boundary, try the previous logical bitmap. */
2261 if (bitmap_offset
== info
->offset
) {
2262 if (info
->offset
== 0)
2264 bitmap_offset
= offset_to_bitmap(ctl
, info
->offset
- 1);
2267 bitmap
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2271 i
= offset_to_bit(bitmap
->offset
, ctl
->unit
, info
->offset
) - 1;
2273 prev_j
= (unsigned long)-1;
2274 for_each_clear_bit_from(j
, bitmap
->bitmap
, BITS_PER_BITMAP
) {
2282 if (prev_j
== (unsigned long)-1)
2283 bytes
= (i
+ 1) * ctl
->unit
;
2285 bytes
= (i
- prev_j
) * ctl
->unit
;
2287 info
->offset
-= bytes
;
2288 info
->bytes
+= bytes
;
2291 bitmap_clear_bits(ctl
, bitmap
, info
->offset
, bytes
);
2293 __bitmap_clear_bits(ctl
, bitmap
, info
->offset
, bytes
);
2296 free_bitmap(ctl
, bitmap
);
2302 * We prefer always to allocate from extent entries, both for clustered and
2303 * non-clustered allocation requests. So when attempting to add a new extent
2304 * entry, try to see if there's adjacent free space in bitmap entries, and if
2305 * there is, migrate that space from the bitmaps to the extent.
2306 * Like this we get better chances of satisfying space allocation requests
2307 * because we attempt to satisfy them based on a single cache entry, and never
2308 * on 2 or more entries - even if the entries represent a contiguous free space
2309 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2312 static void steal_from_bitmap(struct btrfs_free_space_ctl
*ctl
,
2313 struct btrfs_free_space
*info
,
2317 * Only work with disconnected entries, as we can change their offset,
2318 * and must be extent entries.
2320 ASSERT(!info
->bitmap
);
2321 ASSERT(RB_EMPTY_NODE(&info
->offset_index
));
2323 if (ctl
->total_bitmaps
> 0) {
2325 bool stole_front
= false;
2327 stole_end
= steal_from_bitmap_to_end(ctl
, info
, update_stat
);
2328 if (ctl
->total_bitmaps
> 0)
2329 stole_front
= steal_from_bitmap_to_front(ctl
, info
,
2332 if (stole_end
|| stole_front
)
2333 try_merge_free_space(ctl
, info
, update_stat
);
2337 int __btrfs_add_free_space(struct btrfs_fs_info
*fs_info
,
2338 struct btrfs_free_space_ctl
*ctl
,
2339 u64 offset
, u64 bytes
)
2341 struct btrfs_free_space
*info
;
2344 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
2348 info
->offset
= offset
;
2349 info
->bytes
= bytes
;
2350 RB_CLEAR_NODE(&info
->offset_index
);
2352 spin_lock(&ctl
->tree_lock
);
2354 if (try_merge_free_space(ctl
, info
, true))
2358 * There was no extent directly to the left or right of this new
2359 * extent then we know we're going to have to allocate a new extent, so
2360 * before we do that see if we need to drop this into a bitmap
2362 ret
= insert_into_bitmap(ctl
, info
);
2371 * Only steal free space from adjacent bitmaps if we're sure we're not
2372 * going to add the new free space to existing bitmap entries - because
2373 * that would mean unnecessary work that would be reverted. Therefore
2374 * attempt to steal space from bitmaps if we're adding an extent entry.
2376 steal_from_bitmap(ctl
, info
, true);
2378 ret
= link_free_space(ctl
, info
);
2380 kmem_cache_free(btrfs_free_space_cachep
, info
);
2382 spin_unlock(&ctl
->tree_lock
);
2385 btrfs_crit(fs_info
, "unable to add free space :%d", ret
);
2386 ASSERT(ret
!= -EEXIST
);
2392 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
2393 u64 offset
, u64 bytes
)
2395 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2396 struct btrfs_free_space
*info
;
2398 bool re_search
= false;
2400 spin_lock(&ctl
->tree_lock
);
2407 info
= tree_search_offset(ctl
, offset
, 0, 0);
2410 * oops didn't find an extent that matched the space we wanted
2411 * to remove, look for a bitmap instead
2413 info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
2417 * If we found a partial bit of our free space in a
2418 * bitmap but then couldn't find the other part this may
2419 * be a problem, so WARN about it.
2427 if (!info
->bitmap
) {
2428 unlink_free_space(ctl
, info
);
2429 if (offset
== info
->offset
) {
2430 u64 to_free
= min(bytes
, info
->bytes
);
2432 info
->bytes
-= to_free
;
2433 info
->offset
+= to_free
;
2435 ret
= link_free_space(ctl
, info
);
2438 kmem_cache_free(btrfs_free_space_cachep
, info
);
2445 u64 old_end
= info
->bytes
+ info
->offset
;
2447 info
->bytes
= offset
- info
->offset
;
2448 ret
= link_free_space(ctl
, info
);
2453 /* Not enough bytes in this entry to satisfy us */
2454 if (old_end
< offset
+ bytes
) {
2455 bytes
-= old_end
- offset
;
2458 } else if (old_end
== offset
+ bytes
) {
2462 spin_unlock(&ctl
->tree_lock
);
2464 ret
= btrfs_add_free_space(block_group
, offset
+ bytes
,
2465 old_end
- (offset
+ bytes
));
2471 ret
= remove_from_bitmap(ctl
, info
, &offset
, &bytes
);
2472 if (ret
== -EAGAIN
) {
2477 spin_unlock(&ctl
->tree_lock
);
2482 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
2485 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2486 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2487 struct btrfs_free_space
*info
;
2491 spin_lock(&ctl
->tree_lock
);
2492 for (n
= rb_first(&ctl
->free_space_offset
); n
; n
= rb_next(n
)) {
2493 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
2494 if (info
->bytes
>= bytes
&& !block_group
->ro
)
2496 btrfs_crit(fs_info
, "entry offset %llu, bytes %llu, bitmap %s",
2497 info
->offset
, info
->bytes
,
2498 (info
->bitmap
) ? "yes" : "no");
2500 spin_unlock(&ctl
->tree_lock
);
2501 btrfs_info(fs_info
, "block group has cluster?: %s",
2502 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
2504 "%d blocks of free space at or bigger than bytes is", count
);
2507 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache
*block_group
)
2509 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2510 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2512 spin_lock_init(&ctl
->tree_lock
);
2513 ctl
->unit
= fs_info
->sectorsize
;
2514 ctl
->start
= block_group
->key
.objectid
;
2515 ctl
->private = block_group
;
2516 ctl
->op
= &free_space_op
;
2517 INIT_LIST_HEAD(&ctl
->trimming_ranges
);
2518 mutex_init(&ctl
->cache_writeout_mutex
);
2521 * we only want to have 32k of ram per block group for keeping
2522 * track of free space, and if we pass 1/2 of that we want to
2523 * start converting things over to using bitmaps
2525 ctl
->extents_thresh
= (SZ_32K
/ 2) / sizeof(struct btrfs_free_space
);
2529 * for a given cluster, put all of its extents back into the free
2530 * space cache. If the block group passed doesn't match the block group
2531 * pointed to by the cluster, someone else raced in and freed the
2532 * cluster already. In that case, we just return without changing anything
2535 __btrfs_return_cluster_to_free_space(
2536 struct btrfs_block_group_cache
*block_group
,
2537 struct btrfs_free_cluster
*cluster
)
2539 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2540 struct btrfs_free_space
*entry
;
2541 struct rb_node
*node
;
2543 spin_lock(&cluster
->lock
);
2544 if (cluster
->block_group
!= block_group
)
2547 cluster
->block_group
= NULL
;
2548 cluster
->window_start
= 0;
2549 list_del_init(&cluster
->block_group_list
);
2551 node
= rb_first(&cluster
->root
);
2555 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2556 node
= rb_next(&entry
->offset_index
);
2557 rb_erase(&entry
->offset_index
, &cluster
->root
);
2558 RB_CLEAR_NODE(&entry
->offset_index
);
2560 bitmap
= (entry
->bitmap
!= NULL
);
2562 try_merge_free_space(ctl
, entry
, false);
2563 steal_from_bitmap(ctl
, entry
, false);
2565 tree_insert_offset(&ctl
->free_space_offset
,
2566 entry
->offset
, &entry
->offset_index
, bitmap
);
2568 cluster
->root
= RB_ROOT
;
2571 spin_unlock(&cluster
->lock
);
2572 btrfs_put_block_group(block_group
);
2576 static void __btrfs_remove_free_space_cache_locked(
2577 struct btrfs_free_space_ctl
*ctl
)
2579 struct btrfs_free_space
*info
;
2580 struct rb_node
*node
;
2582 while ((node
= rb_last(&ctl
->free_space_offset
)) != NULL
) {
2583 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2584 if (!info
->bitmap
) {
2585 unlink_free_space(ctl
, info
);
2586 kmem_cache_free(btrfs_free_space_cachep
, info
);
2588 free_bitmap(ctl
, info
);
2591 cond_resched_lock(&ctl
->tree_lock
);
2595 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl
*ctl
)
2597 spin_lock(&ctl
->tree_lock
);
2598 __btrfs_remove_free_space_cache_locked(ctl
);
2599 spin_unlock(&ctl
->tree_lock
);
2602 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
2604 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2605 struct btrfs_free_cluster
*cluster
;
2606 struct list_head
*head
;
2608 spin_lock(&ctl
->tree_lock
);
2609 while ((head
= block_group
->cluster_list
.next
) !=
2610 &block_group
->cluster_list
) {
2611 cluster
= list_entry(head
, struct btrfs_free_cluster
,
2614 WARN_ON(cluster
->block_group
!= block_group
);
2615 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2617 cond_resched_lock(&ctl
->tree_lock
);
2619 __btrfs_remove_free_space_cache_locked(ctl
);
2620 spin_unlock(&ctl
->tree_lock
);
2624 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
2625 u64 offset
, u64 bytes
, u64 empty_size
,
2626 u64
*max_extent_size
)
2628 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2629 struct btrfs_free_space
*entry
= NULL
;
2630 u64 bytes_search
= bytes
+ empty_size
;
2633 u64 align_gap_len
= 0;
2635 spin_lock(&ctl
->tree_lock
);
2636 entry
= find_free_space(ctl
, &offset
, &bytes_search
,
2637 block_group
->full_stripe_len
, max_extent_size
);
2642 if (entry
->bitmap
) {
2643 bitmap_clear_bits(ctl
, entry
, offset
, bytes
);
2645 free_bitmap(ctl
, entry
);
2647 unlink_free_space(ctl
, entry
);
2648 align_gap_len
= offset
- entry
->offset
;
2649 align_gap
= entry
->offset
;
2651 entry
->offset
= offset
+ bytes
;
2652 WARN_ON(entry
->bytes
< bytes
+ align_gap_len
);
2654 entry
->bytes
-= bytes
+ align_gap_len
;
2656 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2658 link_free_space(ctl
, entry
);
2661 spin_unlock(&ctl
->tree_lock
);
2664 __btrfs_add_free_space(block_group
->fs_info
, ctl
,
2665 align_gap
, align_gap_len
);
2670 * given a cluster, put all of its extents back into the free space
2671 * cache. If a block group is passed, this function will only free
2672 * a cluster that belongs to the passed block group.
2674 * Otherwise, it'll get a reference on the block group pointed to by the
2675 * cluster and remove the cluster from it.
2677 int btrfs_return_cluster_to_free_space(
2678 struct btrfs_block_group_cache
*block_group
,
2679 struct btrfs_free_cluster
*cluster
)
2681 struct btrfs_free_space_ctl
*ctl
;
2684 /* first, get a safe pointer to the block group */
2685 spin_lock(&cluster
->lock
);
2687 block_group
= cluster
->block_group
;
2689 spin_unlock(&cluster
->lock
);
2692 } else if (cluster
->block_group
!= block_group
) {
2693 /* someone else has already freed it don't redo their work */
2694 spin_unlock(&cluster
->lock
);
2697 atomic_inc(&block_group
->count
);
2698 spin_unlock(&cluster
->lock
);
2700 ctl
= block_group
->free_space_ctl
;
2702 /* now return any extents the cluster had on it */
2703 spin_lock(&ctl
->tree_lock
);
2704 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2705 spin_unlock(&ctl
->tree_lock
);
2707 /* finally drop our ref */
2708 btrfs_put_block_group(block_group
);
2712 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
2713 struct btrfs_free_cluster
*cluster
,
2714 struct btrfs_free_space
*entry
,
2715 u64 bytes
, u64 min_start
,
2716 u64
*max_extent_size
)
2718 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2720 u64 search_start
= cluster
->window_start
;
2721 u64 search_bytes
= bytes
;
2724 search_start
= min_start
;
2725 search_bytes
= bytes
;
2727 err
= search_bitmap(ctl
, entry
, &search_start
, &search_bytes
, true);
2729 *max_extent_size
= max(get_max_extent_size(entry
),
2735 __bitmap_clear_bits(ctl
, entry
, ret
, bytes
);
2741 * given a cluster, try to allocate 'bytes' from it, returns 0
2742 * if it couldn't find anything suitably large, or a logical disk offset
2743 * if things worked out
2745 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
2746 struct btrfs_free_cluster
*cluster
, u64 bytes
,
2747 u64 min_start
, u64
*max_extent_size
)
2749 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2750 struct btrfs_free_space
*entry
= NULL
;
2751 struct rb_node
*node
;
2754 spin_lock(&cluster
->lock
);
2755 if (bytes
> cluster
->max_size
)
2758 if (cluster
->block_group
!= block_group
)
2761 node
= rb_first(&cluster
->root
);
2765 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2767 if (entry
->bytes
< bytes
)
2768 *max_extent_size
= max(get_max_extent_size(entry
),
2771 if (entry
->bytes
< bytes
||
2772 (!entry
->bitmap
&& entry
->offset
< min_start
)) {
2773 node
= rb_next(&entry
->offset_index
);
2776 entry
= rb_entry(node
, struct btrfs_free_space
,
2781 if (entry
->bitmap
) {
2782 ret
= btrfs_alloc_from_bitmap(block_group
,
2783 cluster
, entry
, bytes
,
2784 cluster
->window_start
,
2787 node
= rb_next(&entry
->offset_index
);
2790 entry
= rb_entry(node
, struct btrfs_free_space
,
2794 cluster
->window_start
+= bytes
;
2796 ret
= entry
->offset
;
2798 entry
->offset
+= bytes
;
2799 entry
->bytes
-= bytes
;
2802 if (entry
->bytes
== 0)
2803 rb_erase(&entry
->offset_index
, &cluster
->root
);
2807 spin_unlock(&cluster
->lock
);
2812 spin_lock(&ctl
->tree_lock
);
2814 ctl
->free_space
-= bytes
;
2815 if (entry
->bytes
== 0) {
2816 ctl
->free_extents
--;
2817 if (entry
->bitmap
) {
2818 kmem_cache_free(btrfs_free_space_bitmap_cachep
,
2820 ctl
->total_bitmaps
--;
2821 ctl
->op
->recalc_thresholds(ctl
);
2823 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2826 spin_unlock(&ctl
->tree_lock
);
2831 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
2832 struct btrfs_free_space
*entry
,
2833 struct btrfs_free_cluster
*cluster
,
2834 u64 offset
, u64 bytes
,
2835 u64 cont1_bytes
, u64 min_bytes
)
2837 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2838 unsigned long next_zero
;
2840 unsigned long want_bits
;
2841 unsigned long min_bits
;
2842 unsigned long found_bits
;
2843 unsigned long max_bits
= 0;
2844 unsigned long start
= 0;
2845 unsigned long total_found
= 0;
2848 i
= offset_to_bit(entry
->offset
, ctl
->unit
,
2849 max_t(u64
, offset
, entry
->offset
));
2850 want_bits
= bytes_to_bits(bytes
, ctl
->unit
);
2851 min_bits
= bytes_to_bits(min_bytes
, ctl
->unit
);
2854 * Don't bother looking for a cluster in this bitmap if it's heavily
2857 if (entry
->max_extent_size
&&
2858 entry
->max_extent_size
< cont1_bytes
)
2862 for_each_set_bit_from(i
, entry
->bitmap
, BITS_PER_BITMAP
) {
2863 next_zero
= find_next_zero_bit(entry
->bitmap
,
2864 BITS_PER_BITMAP
, i
);
2865 if (next_zero
- i
>= min_bits
) {
2866 found_bits
= next_zero
- i
;
2867 if (found_bits
> max_bits
)
2868 max_bits
= found_bits
;
2871 if (next_zero
- i
> max_bits
)
2872 max_bits
= next_zero
- i
;
2877 entry
->max_extent_size
= (u64
)max_bits
* ctl
->unit
;
2883 cluster
->max_size
= 0;
2886 total_found
+= found_bits
;
2888 if (cluster
->max_size
< found_bits
* ctl
->unit
)
2889 cluster
->max_size
= found_bits
* ctl
->unit
;
2891 if (total_found
< want_bits
|| cluster
->max_size
< cont1_bytes
) {
2896 cluster
->window_start
= start
* ctl
->unit
+ entry
->offset
;
2897 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2898 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2899 &entry
->offset_index
, 1);
2900 ASSERT(!ret
); /* -EEXIST; Logic error */
2902 trace_btrfs_setup_cluster(block_group
, cluster
,
2903 total_found
* ctl
->unit
, 1);
2908 * This searches the block group for just extents to fill the cluster with.
2909 * Try to find a cluster with at least bytes total bytes, at least one
2910 * extent of cont1_bytes, and other clusters of at least min_bytes.
2913 setup_cluster_no_bitmap(struct btrfs_block_group_cache
*block_group
,
2914 struct btrfs_free_cluster
*cluster
,
2915 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2916 u64 cont1_bytes
, u64 min_bytes
)
2918 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2919 struct btrfs_free_space
*first
= NULL
;
2920 struct btrfs_free_space
*entry
= NULL
;
2921 struct btrfs_free_space
*last
;
2922 struct rb_node
*node
;
2927 entry
= tree_search_offset(ctl
, offset
, 0, 1);
2932 * We don't want bitmaps, so just move along until we find a normal
2935 while (entry
->bitmap
|| entry
->bytes
< min_bytes
) {
2936 if (entry
->bitmap
&& list_empty(&entry
->list
))
2937 list_add_tail(&entry
->list
, bitmaps
);
2938 node
= rb_next(&entry
->offset_index
);
2941 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2944 window_free
= entry
->bytes
;
2945 max_extent
= entry
->bytes
;
2949 for (node
= rb_next(&entry
->offset_index
); node
;
2950 node
= rb_next(&entry
->offset_index
)) {
2951 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2953 if (entry
->bitmap
) {
2954 if (list_empty(&entry
->list
))
2955 list_add_tail(&entry
->list
, bitmaps
);
2959 if (entry
->bytes
< min_bytes
)
2963 window_free
+= entry
->bytes
;
2964 if (entry
->bytes
> max_extent
)
2965 max_extent
= entry
->bytes
;
2968 if (window_free
< bytes
|| max_extent
< cont1_bytes
)
2971 cluster
->window_start
= first
->offset
;
2973 node
= &first
->offset_index
;
2976 * now we've found our entries, pull them out of the free space
2977 * cache and put them into the cluster rbtree
2982 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2983 node
= rb_next(&entry
->offset_index
);
2984 if (entry
->bitmap
|| entry
->bytes
< min_bytes
)
2987 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2988 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2989 &entry
->offset_index
, 0);
2990 total_size
+= entry
->bytes
;
2991 ASSERT(!ret
); /* -EEXIST; Logic error */
2992 } while (node
&& entry
!= last
);
2994 cluster
->max_size
= max_extent
;
2995 trace_btrfs_setup_cluster(block_group
, cluster
, total_size
, 0);
3000 * This specifically looks for bitmaps that may work in the cluster, we assume
3001 * that we have already failed to find extents that will work.
3004 setup_cluster_bitmap(struct btrfs_block_group_cache
*block_group
,
3005 struct btrfs_free_cluster
*cluster
,
3006 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
3007 u64 cont1_bytes
, u64 min_bytes
)
3009 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3010 struct btrfs_free_space
*entry
= NULL
;
3012 u64 bitmap_offset
= offset_to_bitmap(ctl
, offset
);
3014 if (ctl
->total_bitmaps
== 0)
3018 * The bitmap that covers offset won't be in the list unless offset
3019 * is just its start offset.
3021 if (!list_empty(bitmaps
))
3022 entry
= list_first_entry(bitmaps
, struct btrfs_free_space
, list
);
3024 if (!entry
|| entry
->offset
!= bitmap_offset
) {
3025 entry
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
3026 if (entry
&& list_empty(&entry
->list
))
3027 list_add(&entry
->list
, bitmaps
);
3030 list_for_each_entry(entry
, bitmaps
, list
) {
3031 if (entry
->bytes
< bytes
)
3033 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
, offset
,
3034 bytes
, cont1_bytes
, min_bytes
);
3040 * The bitmaps list has all the bitmaps that record free space
3041 * starting after offset, so no more search is required.
3047 * here we try to find a cluster of blocks in a block group. The goal
3048 * is to find at least bytes+empty_size.
3049 * We might not find them all in one contiguous area.
3051 * returns zero and sets up cluster if things worked out, otherwise
3052 * it returns -enospc
3054 int btrfs_find_space_cluster(struct btrfs_fs_info
*fs_info
,
3055 struct btrfs_block_group_cache
*block_group
,
3056 struct btrfs_free_cluster
*cluster
,
3057 u64 offset
, u64 bytes
, u64 empty_size
)
3059 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3060 struct btrfs_free_space
*entry
, *tmp
;
3067 * Choose the minimum extent size we'll require for this
3068 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3069 * For metadata, allow allocates with smaller extents. For
3070 * data, keep it dense.
3072 if (btrfs_test_opt(fs_info
, SSD_SPREAD
)) {
3073 cont1_bytes
= min_bytes
= bytes
+ empty_size
;
3074 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
3075 cont1_bytes
= bytes
;
3076 min_bytes
= fs_info
->sectorsize
;
3078 cont1_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
3079 min_bytes
= fs_info
->sectorsize
;
3082 spin_lock(&ctl
->tree_lock
);
3085 * If we know we don't have enough space to make a cluster don't even
3086 * bother doing all the work to try and find one.
3088 if (ctl
->free_space
< bytes
) {
3089 spin_unlock(&ctl
->tree_lock
);
3093 spin_lock(&cluster
->lock
);
3095 /* someone already found a cluster, hooray */
3096 if (cluster
->block_group
) {
3101 trace_btrfs_find_cluster(block_group
, offset
, bytes
, empty_size
,
3104 ret
= setup_cluster_no_bitmap(block_group
, cluster
, &bitmaps
, offset
,
3106 cont1_bytes
, min_bytes
);
3108 ret
= setup_cluster_bitmap(block_group
, cluster
, &bitmaps
,
3109 offset
, bytes
+ empty_size
,
3110 cont1_bytes
, min_bytes
);
3112 /* Clear our temporary list */
3113 list_for_each_entry_safe(entry
, tmp
, &bitmaps
, list
)
3114 list_del_init(&entry
->list
);
3117 atomic_inc(&block_group
->count
);
3118 list_add_tail(&cluster
->block_group_list
,
3119 &block_group
->cluster_list
);
3120 cluster
->block_group
= block_group
;
3122 trace_btrfs_failed_cluster_setup(block_group
);
3125 spin_unlock(&cluster
->lock
);
3126 spin_unlock(&ctl
->tree_lock
);
3132 * simple code to zero out a cluster
3134 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
3136 spin_lock_init(&cluster
->lock
);
3137 spin_lock_init(&cluster
->refill_lock
);
3138 cluster
->root
= RB_ROOT
;
3139 cluster
->max_size
= 0;
3140 cluster
->fragmented
= false;
3141 INIT_LIST_HEAD(&cluster
->block_group_list
);
3142 cluster
->block_group
= NULL
;
3145 static int do_trimming(struct btrfs_block_group_cache
*block_group
,
3146 u64
*total_trimmed
, u64 start
, u64 bytes
,
3147 u64 reserved_start
, u64 reserved_bytes
,
3148 struct btrfs_trim_range
*trim_entry
)
3150 struct btrfs_space_info
*space_info
= block_group
->space_info
;
3151 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
3152 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3157 spin_lock(&space_info
->lock
);
3158 spin_lock(&block_group
->lock
);
3159 if (!block_group
->ro
) {
3160 block_group
->reserved
+= reserved_bytes
;
3161 space_info
->bytes_reserved
+= reserved_bytes
;
3164 spin_unlock(&block_group
->lock
);
3165 spin_unlock(&space_info
->lock
);
3167 ret
= btrfs_discard_extent(fs_info
, start
, bytes
, &trimmed
);
3169 *total_trimmed
+= trimmed
;
3171 mutex_lock(&ctl
->cache_writeout_mutex
);
3172 btrfs_add_free_space(block_group
, reserved_start
, reserved_bytes
);
3173 list_del(&trim_entry
->list
);
3174 mutex_unlock(&ctl
->cache_writeout_mutex
);
3177 spin_lock(&space_info
->lock
);
3178 spin_lock(&block_group
->lock
);
3179 if (block_group
->ro
)
3180 space_info
->bytes_readonly
+= reserved_bytes
;
3181 block_group
->reserved
-= reserved_bytes
;
3182 space_info
->bytes_reserved
-= reserved_bytes
;
3183 spin_unlock(&space_info
->lock
);
3184 spin_unlock(&block_group
->lock
);
3190 static int trim_no_bitmap(struct btrfs_block_group_cache
*block_group
,
3191 u64
*total_trimmed
, u64 start
, u64 end
, u64 minlen
)
3193 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3194 struct btrfs_free_space
*entry
;
3195 struct rb_node
*node
;
3201 while (start
< end
) {
3202 struct btrfs_trim_range trim_entry
;
3204 mutex_lock(&ctl
->cache_writeout_mutex
);
3205 spin_lock(&ctl
->tree_lock
);
3207 if (ctl
->free_space
< minlen
) {
3208 spin_unlock(&ctl
->tree_lock
);
3209 mutex_unlock(&ctl
->cache_writeout_mutex
);
3213 entry
= tree_search_offset(ctl
, start
, 0, 1);
3215 spin_unlock(&ctl
->tree_lock
);
3216 mutex_unlock(&ctl
->cache_writeout_mutex
);
3221 while (entry
->bitmap
) {
3222 node
= rb_next(&entry
->offset_index
);
3224 spin_unlock(&ctl
->tree_lock
);
3225 mutex_unlock(&ctl
->cache_writeout_mutex
);
3228 entry
= rb_entry(node
, struct btrfs_free_space
,
3232 if (entry
->offset
>= end
) {
3233 spin_unlock(&ctl
->tree_lock
);
3234 mutex_unlock(&ctl
->cache_writeout_mutex
);
3238 extent_start
= entry
->offset
;
3239 extent_bytes
= entry
->bytes
;
3240 start
= max(start
, extent_start
);
3241 bytes
= min(extent_start
+ extent_bytes
, end
) - start
;
3242 if (bytes
< minlen
) {
3243 spin_unlock(&ctl
->tree_lock
);
3244 mutex_unlock(&ctl
->cache_writeout_mutex
);
3248 unlink_free_space(ctl
, entry
);
3249 kmem_cache_free(btrfs_free_space_cachep
, entry
);
3251 spin_unlock(&ctl
->tree_lock
);
3252 trim_entry
.start
= extent_start
;
3253 trim_entry
.bytes
= extent_bytes
;
3254 list_add_tail(&trim_entry
.list
, &ctl
->trimming_ranges
);
3255 mutex_unlock(&ctl
->cache_writeout_mutex
);
3257 ret
= do_trimming(block_group
, total_trimmed
, start
, bytes
,
3258 extent_start
, extent_bytes
, &trim_entry
);
3264 if (fatal_signal_pending(current
)) {
3275 static int trim_bitmaps(struct btrfs_block_group_cache
*block_group
,
3276 u64
*total_trimmed
, u64 start
, u64 end
, u64 minlen
)
3278 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3279 struct btrfs_free_space
*entry
;
3283 u64 offset
= offset_to_bitmap(ctl
, start
);
3285 while (offset
< end
) {
3286 bool next_bitmap
= false;
3287 struct btrfs_trim_range trim_entry
;
3289 mutex_lock(&ctl
->cache_writeout_mutex
);
3290 spin_lock(&ctl
->tree_lock
);
3292 if (ctl
->free_space
< minlen
) {
3293 spin_unlock(&ctl
->tree_lock
);
3294 mutex_unlock(&ctl
->cache_writeout_mutex
);
3298 entry
= tree_search_offset(ctl
, offset
, 1, 0);
3300 spin_unlock(&ctl
->tree_lock
);
3301 mutex_unlock(&ctl
->cache_writeout_mutex
);
3307 ret2
= search_bitmap(ctl
, entry
, &start
, &bytes
, false);
3308 if (ret2
|| start
>= end
) {
3309 spin_unlock(&ctl
->tree_lock
);
3310 mutex_unlock(&ctl
->cache_writeout_mutex
);
3315 bytes
= min(bytes
, end
- start
);
3316 if (bytes
< minlen
) {
3317 spin_unlock(&ctl
->tree_lock
);
3318 mutex_unlock(&ctl
->cache_writeout_mutex
);
3322 bitmap_clear_bits(ctl
, entry
, start
, bytes
);
3323 if (entry
->bytes
== 0)
3324 free_bitmap(ctl
, entry
);
3326 spin_unlock(&ctl
->tree_lock
);
3327 trim_entry
.start
= start
;
3328 trim_entry
.bytes
= bytes
;
3329 list_add_tail(&trim_entry
.list
, &ctl
->trimming_ranges
);
3330 mutex_unlock(&ctl
->cache_writeout_mutex
);
3332 ret
= do_trimming(block_group
, total_trimmed
, start
, bytes
,
3333 start
, bytes
, &trim_entry
);
3338 offset
+= BITS_PER_BITMAP
* ctl
->unit
;
3341 if (start
>= offset
+ BITS_PER_BITMAP
* ctl
->unit
)
3342 offset
+= BITS_PER_BITMAP
* ctl
->unit
;
3345 if (fatal_signal_pending(current
)) {
3356 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache
*cache
)
3358 atomic_inc(&cache
->trimming
);
3361 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache
*block_group
)
3363 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
3364 struct extent_map_tree
*em_tree
;
3365 struct extent_map
*em
;
3368 spin_lock(&block_group
->lock
);
3369 cleanup
= (atomic_dec_and_test(&block_group
->trimming
) &&
3370 block_group
->removed
);
3371 spin_unlock(&block_group
->lock
);
3374 mutex_lock(&fs_info
->chunk_mutex
);
3375 em_tree
= &fs_info
->mapping_tree
.map_tree
;
3376 write_lock(&em_tree
->lock
);
3377 em
= lookup_extent_mapping(em_tree
, block_group
->key
.objectid
,
3379 BUG_ON(!em
); /* logic error, can't happen */
3381 * remove_extent_mapping() will delete us from the pinned_chunks
3382 * list, which is protected by the chunk mutex.
3384 remove_extent_mapping(em_tree
, em
);
3385 write_unlock(&em_tree
->lock
);
3386 mutex_unlock(&fs_info
->chunk_mutex
);
3388 /* once for us and once for the tree */
3389 free_extent_map(em
);
3390 free_extent_map(em
);
3393 * We've left one free space entry and other tasks trimming
3394 * this block group have left 1 entry each one. Free them.
3396 __btrfs_remove_free_space_cache(block_group
->free_space_ctl
);
3400 int btrfs_trim_block_group(struct btrfs_block_group_cache
*block_group
,
3401 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
)
3407 spin_lock(&block_group
->lock
);
3408 if (block_group
->removed
) {
3409 spin_unlock(&block_group
->lock
);
3412 btrfs_get_block_group_trimming(block_group
);
3413 spin_unlock(&block_group
->lock
);
3415 ret
= trim_no_bitmap(block_group
, trimmed
, start
, end
, minlen
);
3419 ret
= trim_bitmaps(block_group
, trimmed
, start
, end
, minlen
);
3421 btrfs_put_block_group_trimming(block_group
);
3426 * Find the left-most item in the cache tree, and then return the
3427 * smallest inode number in the item.
3429 * Note: the returned inode number may not be the smallest one in
3430 * the tree, if the left-most item is a bitmap.
3432 u64
btrfs_find_ino_for_alloc(struct btrfs_root
*fs_root
)
3434 struct btrfs_free_space_ctl
*ctl
= fs_root
->free_ino_ctl
;
3435 struct btrfs_free_space
*entry
= NULL
;
3438 spin_lock(&ctl
->tree_lock
);
3440 if (RB_EMPTY_ROOT(&ctl
->free_space_offset
))
3443 entry
= rb_entry(rb_first(&ctl
->free_space_offset
),
3444 struct btrfs_free_space
, offset_index
);
3446 if (!entry
->bitmap
) {
3447 ino
= entry
->offset
;
3449 unlink_free_space(ctl
, entry
);
3453 kmem_cache_free(btrfs_free_space_cachep
, entry
);
3455 link_free_space(ctl
, entry
);
3461 ret
= search_bitmap(ctl
, entry
, &offset
, &count
, true);
3462 /* Logic error; Should be empty if it can't find anything */
3466 bitmap_clear_bits(ctl
, entry
, offset
, 1);
3467 if (entry
->bytes
== 0)
3468 free_bitmap(ctl
, entry
);
3471 spin_unlock(&ctl
->tree_lock
);
3476 struct inode
*lookup_free_ino_inode(struct btrfs_root
*root
,
3477 struct btrfs_path
*path
)
3479 struct inode
*inode
= NULL
;
3481 spin_lock(&root
->ino_cache_lock
);
3482 if (root
->ino_cache_inode
)
3483 inode
= igrab(root
->ino_cache_inode
);
3484 spin_unlock(&root
->ino_cache_lock
);
3488 inode
= __lookup_free_space_inode(root
, path
, 0);
3492 spin_lock(&root
->ino_cache_lock
);
3493 if (!btrfs_fs_closing(root
->fs_info
))
3494 root
->ino_cache_inode
= igrab(inode
);
3495 spin_unlock(&root
->ino_cache_lock
);
3500 int create_free_ino_inode(struct btrfs_root
*root
,
3501 struct btrfs_trans_handle
*trans
,
3502 struct btrfs_path
*path
)
3504 return __create_free_space_inode(root
, trans
, path
,
3505 BTRFS_FREE_INO_OBJECTID
, 0);
3508 int load_free_ino_cache(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
3510 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
3511 struct btrfs_path
*path
;
3512 struct inode
*inode
;
3514 u64 root_gen
= btrfs_root_generation(&root
->root_item
);
3516 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
3520 * If we're unmounting then just return, since this does a search on the
3521 * normal root and not the commit root and we could deadlock.
3523 if (btrfs_fs_closing(fs_info
))
3526 path
= btrfs_alloc_path();
3530 inode
= lookup_free_ino_inode(root
, path
);
3534 if (root_gen
!= BTRFS_I(inode
)->generation
)
3537 ret
= __load_free_space_cache(root
, inode
, ctl
, path
, 0);
3541 "failed to load free ino cache for root %llu",
3542 root
->root_key
.objectid
);
3546 btrfs_free_path(path
);
3550 int btrfs_write_out_ino_cache(struct btrfs_root
*root
,
3551 struct btrfs_trans_handle
*trans
,
3552 struct btrfs_path
*path
,
3553 struct inode
*inode
)
3555 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3556 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
3558 struct btrfs_io_ctl io_ctl
;
3559 bool release_metadata
= true;
3561 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
3564 memset(&io_ctl
, 0, sizeof(io_ctl
));
3565 ret
= __btrfs_write_out_cache(root
, inode
, ctl
, NULL
, &io_ctl
, trans
);
3568 * At this point writepages() didn't error out, so our metadata
3569 * reservation is released when the writeback finishes, at
3570 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3571 * with or without an error.
3573 release_metadata
= false;
3574 ret
= btrfs_wait_cache_io_root(root
, trans
, &io_ctl
, path
);
3578 if (release_metadata
)
3579 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
3580 inode
->i_size
, true);
3583 "failed to write free ino cache for root %llu",
3584 root
->root_key
.objectid
);
3591 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3593 * Use this if you need to make a bitmap or extent entry specifically, it
3594 * doesn't do any of the merging that add_free_space does, this acts a lot like
3595 * how the free space cache loading stuff works, so you can get really weird
3598 int test_add_free_space_entry(struct btrfs_block_group_cache
*cache
,
3599 u64 offset
, u64 bytes
, bool bitmap
)
3601 struct btrfs_free_space_ctl
*ctl
= cache
->free_space_ctl
;
3602 struct btrfs_free_space
*info
= NULL
, *bitmap_info
;
3609 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
3615 spin_lock(&ctl
->tree_lock
);
3616 info
->offset
= offset
;
3617 info
->bytes
= bytes
;
3618 info
->max_extent_size
= 0;
3619 ret
= link_free_space(ctl
, info
);
3620 spin_unlock(&ctl
->tree_lock
);
3622 kmem_cache_free(btrfs_free_space_cachep
, info
);
3627 map
= kmem_cache_zalloc(btrfs_free_space_bitmap_cachep
, GFP_NOFS
);
3629 kmem_cache_free(btrfs_free_space_cachep
, info
);
3634 spin_lock(&ctl
->tree_lock
);
3635 bitmap_info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
3640 add_new_bitmap(ctl
, info
, offset
);
3645 bytes_added
= add_bytes_to_bitmap(ctl
, bitmap_info
, offset
, bytes
);
3647 bytes
-= bytes_added
;
3648 offset
+= bytes_added
;
3649 spin_unlock(&ctl
->tree_lock
);
3655 kmem_cache_free(btrfs_free_space_cachep
, info
);
3657 kmem_cache_free(btrfs_free_space_bitmap_cachep
, map
);
3662 * Checks to see if the given range is in the free space cache. This is really
3663 * just used to check the absence of space, so if there is free space in the
3664 * range at all we will return 1.
3666 int test_check_exists(struct btrfs_block_group_cache
*cache
,
3667 u64 offset
, u64 bytes
)
3669 struct btrfs_free_space_ctl
*ctl
= cache
->free_space_ctl
;
3670 struct btrfs_free_space
*info
;
3673 spin_lock(&ctl
->tree_lock
);
3674 info
= tree_search_offset(ctl
, offset
, 0, 0);
3676 info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
3684 u64 bit_off
, bit_bytes
;
3686 struct btrfs_free_space
*tmp
;
3689 bit_bytes
= ctl
->unit
;
3690 ret
= search_bitmap(ctl
, info
, &bit_off
, &bit_bytes
, false);
3692 if (bit_off
== offset
) {
3695 } else if (bit_off
> offset
&&
3696 offset
+ bytes
> bit_off
) {
3702 n
= rb_prev(&info
->offset_index
);
3704 tmp
= rb_entry(n
, struct btrfs_free_space
,
3706 if (tmp
->offset
+ tmp
->bytes
< offset
)
3708 if (offset
+ bytes
< tmp
->offset
) {
3709 n
= rb_prev(&tmp
->offset_index
);
3716 n
= rb_next(&info
->offset_index
);
3718 tmp
= rb_entry(n
, struct btrfs_free_space
,
3720 if (offset
+ bytes
< tmp
->offset
)
3722 if (tmp
->offset
+ tmp
->bytes
< offset
) {
3723 n
= rb_next(&tmp
->offset_index
);
3734 if (info
->offset
== offset
) {
3739 if (offset
> info
->offset
&& offset
< info
->offset
+ info
->bytes
)
3742 spin_unlock(&ctl
->tree_lock
);
3745 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */