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>
14 #include "free-space-cache.h"
15 #include "transaction.h"
17 #include "extent_io.h"
18 #include "inode-map.h"
21 #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
22 #define MAX_CACHE_BYTES_PER_GIG SZ_32K
24 struct btrfs_trim_range
{
27 struct list_head list
;
30 static int link_free_space(struct btrfs_free_space_ctl
*ctl
,
31 struct btrfs_free_space
*info
);
32 static void unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
33 struct btrfs_free_space
*info
);
34 static int btrfs_wait_cache_io_root(struct btrfs_root
*root
,
35 struct btrfs_trans_handle
*trans
,
36 struct btrfs_io_ctl
*io_ctl
,
37 struct btrfs_path
*path
);
39 static struct inode
*__lookup_free_space_inode(struct btrfs_root
*root
,
40 struct btrfs_path
*path
,
43 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
45 struct btrfs_key location
;
46 struct btrfs_disk_key disk_key
;
47 struct btrfs_free_space_header
*header
;
48 struct extent_buffer
*leaf
;
49 struct inode
*inode
= NULL
;
52 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
56 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
60 btrfs_release_path(path
);
61 return ERR_PTR(-ENOENT
);
64 leaf
= path
->nodes
[0];
65 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
66 struct btrfs_free_space_header
);
67 btrfs_free_space_key(leaf
, header
, &disk_key
);
68 btrfs_disk_key_to_cpu(&location
, &disk_key
);
69 btrfs_release_path(path
);
71 inode
= btrfs_iget(fs_info
->sb
, &location
, root
, NULL
);
74 if (is_bad_inode(inode
)) {
76 return ERR_PTR(-ENOENT
);
79 mapping_set_gfp_mask(inode
->i_mapping
,
80 mapping_gfp_constraint(inode
->i_mapping
,
81 ~(__GFP_FS
| __GFP_HIGHMEM
)));
86 struct inode
*lookup_free_space_inode(struct btrfs_fs_info
*fs_info
,
87 struct btrfs_block_group_cache
88 *block_group
, struct btrfs_path
*path
)
90 struct inode
*inode
= NULL
;
91 u32 flags
= BTRFS_INODE_NODATASUM
| BTRFS_INODE_NODATACOW
;
93 spin_lock(&block_group
->lock
);
94 if (block_group
->inode
)
95 inode
= igrab(block_group
->inode
);
96 spin_unlock(&block_group
->lock
);
100 inode
= __lookup_free_space_inode(fs_info
->tree_root
, path
,
101 block_group
->key
.objectid
);
105 spin_lock(&block_group
->lock
);
106 if (!((BTRFS_I(inode
)->flags
& flags
) == flags
)) {
107 btrfs_info(fs_info
, "Old style space inode found, converting.");
108 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
|
109 BTRFS_INODE_NODATACOW
;
110 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
113 if (!block_group
->iref
) {
114 block_group
->inode
= igrab(inode
);
115 block_group
->iref
= 1;
117 spin_unlock(&block_group
->lock
);
122 static int __create_free_space_inode(struct btrfs_root
*root
,
123 struct btrfs_trans_handle
*trans
,
124 struct btrfs_path
*path
,
127 struct btrfs_key key
;
128 struct btrfs_disk_key disk_key
;
129 struct btrfs_free_space_header
*header
;
130 struct btrfs_inode_item
*inode_item
;
131 struct extent_buffer
*leaf
;
132 u64 flags
= BTRFS_INODE_NOCOMPRESS
| BTRFS_INODE_PREALLOC
;
135 ret
= btrfs_insert_empty_inode(trans
, root
, path
, ino
);
139 /* We inline crc's for the free disk space cache */
140 if (ino
!= BTRFS_FREE_INO_OBJECTID
)
141 flags
|= BTRFS_INODE_NODATASUM
| BTRFS_INODE_NODATACOW
;
143 leaf
= path
->nodes
[0];
144 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
145 struct btrfs_inode_item
);
146 btrfs_item_key(leaf
, &disk_key
, path
->slots
[0]);
147 memzero_extent_buffer(leaf
, (unsigned long)inode_item
,
148 sizeof(*inode_item
));
149 btrfs_set_inode_generation(leaf
, inode_item
, trans
->transid
);
150 btrfs_set_inode_size(leaf
, inode_item
, 0);
151 btrfs_set_inode_nbytes(leaf
, inode_item
, 0);
152 btrfs_set_inode_uid(leaf
, inode_item
, 0);
153 btrfs_set_inode_gid(leaf
, inode_item
, 0);
154 btrfs_set_inode_mode(leaf
, inode_item
, S_IFREG
| 0600);
155 btrfs_set_inode_flags(leaf
, inode_item
, flags
);
156 btrfs_set_inode_nlink(leaf
, inode_item
, 1);
157 btrfs_set_inode_transid(leaf
, inode_item
, trans
->transid
);
158 btrfs_set_inode_block_group(leaf
, inode_item
, offset
);
159 btrfs_mark_buffer_dirty(leaf
);
160 btrfs_release_path(path
);
162 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
165 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
166 sizeof(struct btrfs_free_space_header
));
168 btrfs_release_path(path
);
172 leaf
= path
->nodes
[0];
173 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
174 struct btrfs_free_space_header
);
175 memzero_extent_buffer(leaf
, (unsigned long)header
, sizeof(*header
));
176 btrfs_set_free_space_key(leaf
, header
, &disk_key
);
177 btrfs_mark_buffer_dirty(leaf
);
178 btrfs_release_path(path
);
183 int create_free_space_inode(struct btrfs_fs_info
*fs_info
,
184 struct btrfs_trans_handle
*trans
,
185 struct btrfs_block_group_cache
*block_group
,
186 struct btrfs_path
*path
)
191 ret
= btrfs_find_free_objectid(fs_info
->tree_root
, &ino
);
195 return __create_free_space_inode(fs_info
->tree_root
, trans
, path
, ino
,
196 block_group
->key
.objectid
);
199 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info
*fs_info
,
200 struct btrfs_block_rsv
*rsv
)
205 /* 1 for slack space, 1 for updating the inode */
206 needed_bytes
= btrfs_calc_trunc_metadata_size(fs_info
, 1) +
207 btrfs_calc_trans_metadata_size(fs_info
, 1);
209 spin_lock(&rsv
->lock
);
210 if (rsv
->reserved
< needed_bytes
)
214 spin_unlock(&rsv
->lock
);
218 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle
*trans
,
219 struct btrfs_block_group_cache
*block_group
,
222 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
227 struct btrfs_path
*path
= btrfs_alloc_path();
234 mutex_lock(&trans
->transaction
->cache_write_mutex
);
235 if (!list_empty(&block_group
->io_list
)) {
236 list_del_init(&block_group
->io_list
);
238 btrfs_wait_cache_io(trans
, block_group
, path
);
239 btrfs_put_block_group(block_group
);
243 * now that we've truncated the cache away, its no longer
246 spin_lock(&block_group
->lock
);
247 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
248 spin_unlock(&block_group
->lock
);
249 btrfs_free_path(path
);
252 btrfs_i_size_write(BTRFS_I(inode
), 0);
253 truncate_pagecache(inode
, 0);
256 * We skip the throttling logic for free space cache inodes, so we don't
257 * need to check for -EAGAIN.
259 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
260 0, BTRFS_EXTENT_DATA_KEY
);
264 ret
= btrfs_update_inode(trans
, root
, inode
);
268 mutex_unlock(&trans
->transaction
->cache_write_mutex
);
270 btrfs_abort_transaction(trans
, ret
);
275 static void readahead_cache(struct inode
*inode
)
277 struct file_ra_state
*ra
;
278 unsigned long last_index
;
280 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
284 file_ra_state_init(ra
, inode
->i_mapping
);
285 last_index
= (i_size_read(inode
) - 1) >> PAGE_SHIFT
;
287 page_cache_sync_readahead(inode
->i_mapping
, ra
, NULL
, 0, last_index
);
292 static int io_ctl_init(struct btrfs_io_ctl
*io_ctl
, struct inode
*inode
,
298 num_pages
= DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
);
300 if (btrfs_ino(BTRFS_I(inode
)) != BTRFS_FREE_INO_OBJECTID
)
303 /* Make sure we can fit our crcs into the first page */
304 if (write
&& check_crcs
&&
305 (num_pages
* sizeof(u32
)) >= PAGE_SIZE
)
308 memset(io_ctl
, 0, sizeof(struct btrfs_io_ctl
));
310 io_ctl
->pages
= kcalloc(num_pages
, sizeof(struct page
*), GFP_NOFS
);
314 io_ctl
->num_pages
= num_pages
;
315 io_ctl
->fs_info
= btrfs_sb(inode
->i_sb
);
316 io_ctl
->check_crcs
= check_crcs
;
317 io_ctl
->inode
= inode
;
321 ALLOW_ERROR_INJECTION(io_ctl_init
, ERRNO
);
323 static void io_ctl_free(struct btrfs_io_ctl
*io_ctl
)
325 kfree(io_ctl
->pages
);
326 io_ctl
->pages
= NULL
;
329 static void io_ctl_unmap_page(struct btrfs_io_ctl
*io_ctl
)
337 static void io_ctl_map_page(struct btrfs_io_ctl
*io_ctl
, int clear
)
339 ASSERT(io_ctl
->index
< io_ctl
->num_pages
);
340 io_ctl
->page
= io_ctl
->pages
[io_ctl
->index
++];
341 io_ctl
->cur
= page_address(io_ctl
->page
);
342 io_ctl
->orig
= io_ctl
->cur
;
343 io_ctl
->size
= PAGE_SIZE
;
345 clear_page(io_ctl
->cur
);
348 static void io_ctl_drop_pages(struct btrfs_io_ctl
*io_ctl
)
352 io_ctl_unmap_page(io_ctl
);
354 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
355 if (io_ctl
->pages
[i
]) {
356 ClearPageChecked(io_ctl
->pages
[i
]);
357 unlock_page(io_ctl
->pages
[i
]);
358 put_page(io_ctl
->pages
[i
]);
363 static int io_ctl_prepare_pages(struct btrfs_io_ctl
*io_ctl
, struct inode
*inode
,
367 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
370 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
371 page
= find_or_create_page(inode
->i_mapping
, i
, mask
);
373 io_ctl_drop_pages(io_ctl
);
376 io_ctl
->pages
[i
] = page
;
377 if (uptodate
&& !PageUptodate(page
)) {
378 btrfs_readpage(NULL
, page
);
380 if (!PageUptodate(page
)) {
381 btrfs_err(BTRFS_I(inode
)->root
->fs_info
,
382 "error reading free space cache");
383 io_ctl_drop_pages(io_ctl
);
389 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
390 clear_page_dirty_for_io(io_ctl
->pages
[i
]);
391 set_page_extent_mapped(io_ctl
->pages
[i
]);
397 static void io_ctl_set_generation(struct btrfs_io_ctl
*io_ctl
, u64 generation
)
401 io_ctl_map_page(io_ctl
, 1);
404 * Skip the csum areas. If we don't check crcs then we just have a
405 * 64bit chunk at the front of the first page.
407 if (io_ctl
->check_crcs
) {
408 io_ctl
->cur
+= (sizeof(u32
) * io_ctl
->num_pages
);
409 io_ctl
->size
-= sizeof(u64
) + (sizeof(u32
) * io_ctl
->num_pages
);
411 io_ctl
->cur
+= sizeof(u64
);
412 io_ctl
->size
-= sizeof(u64
) * 2;
416 *val
= cpu_to_le64(generation
);
417 io_ctl
->cur
+= sizeof(u64
);
420 static int io_ctl_check_generation(struct btrfs_io_ctl
*io_ctl
, u64 generation
)
425 * Skip the crc area. If we don't check crcs then we just have a 64bit
426 * chunk at the front of the first page.
428 if (io_ctl
->check_crcs
) {
429 io_ctl
->cur
+= sizeof(u32
) * io_ctl
->num_pages
;
430 io_ctl
->size
-= sizeof(u64
) +
431 (sizeof(u32
) * io_ctl
->num_pages
);
433 io_ctl
->cur
+= sizeof(u64
);
434 io_ctl
->size
-= sizeof(u64
) * 2;
438 if (le64_to_cpu(*gen
) != generation
) {
439 btrfs_err_rl(io_ctl
->fs_info
,
440 "space cache generation (%llu) does not match inode (%llu)",
442 io_ctl_unmap_page(io_ctl
);
445 io_ctl
->cur
+= sizeof(u64
);
449 static void io_ctl_set_crc(struct btrfs_io_ctl
*io_ctl
, int index
)
455 if (!io_ctl
->check_crcs
) {
456 io_ctl_unmap_page(io_ctl
);
461 offset
= sizeof(u32
) * io_ctl
->num_pages
;
463 crc
= btrfs_csum_data(io_ctl
->orig
+ offset
, crc
,
465 btrfs_csum_final(crc
, (u8
*)&crc
);
466 io_ctl_unmap_page(io_ctl
);
467 tmp
= page_address(io_ctl
->pages
[0]);
472 static int io_ctl_check_crc(struct btrfs_io_ctl
*io_ctl
, int index
)
478 if (!io_ctl
->check_crcs
) {
479 io_ctl_map_page(io_ctl
, 0);
484 offset
= sizeof(u32
) * io_ctl
->num_pages
;
486 tmp
= page_address(io_ctl
->pages
[0]);
490 io_ctl_map_page(io_ctl
, 0);
491 crc
= btrfs_csum_data(io_ctl
->orig
+ offset
, crc
,
493 btrfs_csum_final(crc
, (u8
*)&crc
);
495 btrfs_err_rl(io_ctl
->fs_info
,
496 "csum mismatch on free space cache");
497 io_ctl_unmap_page(io_ctl
);
504 static int io_ctl_add_entry(struct btrfs_io_ctl
*io_ctl
, u64 offset
, u64 bytes
,
507 struct btrfs_free_space_entry
*entry
;
513 entry
->offset
= cpu_to_le64(offset
);
514 entry
->bytes
= cpu_to_le64(bytes
);
515 entry
->type
= (bitmap
) ? BTRFS_FREE_SPACE_BITMAP
:
516 BTRFS_FREE_SPACE_EXTENT
;
517 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
518 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
520 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
523 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
525 /* No more pages to map */
526 if (io_ctl
->index
>= io_ctl
->num_pages
)
529 /* map the next page */
530 io_ctl_map_page(io_ctl
, 1);
534 static int io_ctl_add_bitmap(struct btrfs_io_ctl
*io_ctl
, void *bitmap
)
540 * If we aren't at the start of the current page, unmap this one and
541 * map the next one if there is any left.
543 if (io_ctl
->cur
!= io_ctl
->orig
) {
544 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
545 if (io_ctl
->index
>= io_ctl
->num_pages
)
547 io_ctl_map_page(io_ctl
, 0);
550 memcpy(io_ctl
->cur
, bitmap
, PAGE_SIZE
);
551 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
552 if (io_ctl
->index
< io_ctl
->num_pages
)
553 io_ctl_map_page(io_ctl
, 0);
557 static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl
*io_ctl
)
560 * If we're not on the boundary we know we've modified the page and we
561 * need to crc the page.
563 if (io_ctl
->cur
!= io_ctl
->orig
)
564 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
566 io_ctl_unmap_page(io_ctl
);
568 while (io_ctl
->index
< io_ctl
->num_pages
) {
569 io_ctl_map_page(io_ctl
, 1);
570 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
574 static int io_ctl_read_entry(struct btrfs_io_ctl
*io_ctl
,
575 struct btrfs_free_space
*entry
, u8
*type
)
577 struct btrfs_free_space_entry
*e
;
581 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
587 entry
->offset
= le64_to_cpu(e
->offset
);
588 entry
->bytes
= le64_to_cpu(e
->bytes
);
590 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
591 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
593 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
596 io_ctl_unmap_page(io_ctl
);
601 static int io_ctl_read_bitmap(struct btrfs_io_ctl
*io_ctl
,
602 struct btrfs_free_space
*entry
)
606 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
610 memcpy(entry
->bitmap
, io_ctl
->cur
, PAGE_SIZE
);
611 io_ctl_unmap_page(io_ctl
);
617 * Since we attach pinned extents after the fact we can have contiguous sections
618 * of free space that are split up in entries. This poses a problem with the
619 * tree logging stuff since it could have allocated across what appears to be 2
620 * entries since we would have merged the entries when adding the pinned extents
621 * back to the free space cache. So run through the space cache that we just
622 * loaded and merge contiguous entries. This will make the log replay stuff not
623 * blow up and it will make for nicer allocator behavior.
625 static void merge_space_tree(struct btrfs_free_space_ctl
*ctl
)
627 struct btrfs_free_space
*e
, *prev
= NULL
;
631 spin_lock(&ctl
->tree_lock
);
632 for (n
= rb_first(&ctl
->free_space_offset
); n
; n
= rb_next(n
)) {
633 e
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
636 if (e
->bitmap
|| prev
->bitmap
)
638 if (prev
->offset
+ prev
->bytes
== e
->offset
) {
639 unlink_free_space(ctl
, prev
);
640 unlink_free_space(ctl
, e
);
641 prev
->bytes
+= e
->bytes
;
642 kmem_cache_free(btrfs_free_space_cachep
, e
);
643 link_free_space(ctl
, prev
);
645 spin_unlock(&ctl
->tree_lock
);
651 spin_unlock(&ctl
->tree_lock
);
654 static int __load_free_space_cache(struct btrfs_root
*root
, struct inode
*inode
,
655 struct btrfs_free_space_ctl
*ctl
,
656 struct btrfs_path
*path
, u64 offset
)
658 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
659 struct btrfs_free_space_header
*header
;
660 struct extent_buffer
*leaf
;
661 struct btrfs_io_ctl io_ctl
;
662 struct btrfs_key key
;
663 struct btrfs_free_space
*e
, *n
;
671 /* Nothing in the space cache, goodbye */
672 if (!i_size_read(inode
))
675 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
679 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
683 btrfs_release_path(path
);
689 leaf
= path
->nodes
[0];
690 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
691 struct btrfs_free_space_header
);
692 num_entries
= btrfs_free_space_entries(leaf
, header
);
693 num_bitmaps
= btrfs_free_space_bitmaps(leaf
, header
);
694 generation
= btrfs_free_space_generation(leaf
, header
);
695 btrfs_release_path(path
);
697 if (!BTRFS_I(inode
)->generation
) {
699 "the free space cache file (%llu) is invalid, skip it",
704 if (BTRFS_I(inode
)->generation
!= generation
) {
706 "free space inode generation (%llu) did not match free space cache generation (%llu)",
707 BTRFS_I(inode
)->generation
, generation
);
714 ret
= io_ctl_init(&io_ctl
, inode
, 0);
718 readahead_cache(inode
);
720 ret
= io_ctl_prepare_pages(&io_ctl
, inode
, 1);
724 ret
= io_ctl_check_crc(&io_ctl
, 0);
728 ret
= io_ctl_check_generation(&io_ctl
, generation
);
732 while (num_entries
) {
733 e
= kmem_cache_zalloc(btrfs_free_space_cachep
,
738 ret
= io_ctl_read_entry(&io_ctl
, e
, &type
);
740 kmem_cache_free(btrfs_free_space_cachep
, e
);
745 kmem_cache_free(btrfs_free_space_cachep
, e
);
749 if (type
== BTRFS_FREE_SPACE_EXTENT
) {
750 spin_lock(&ctl
->tree_lock
);
751 ret
= link_free_space(ctl
, e
);
752 spin_unlock(&ctl
->tree_lock
);
755 "Duplicate entries in free space cache, dumping");
756 kmem_cache_free(btrfs_free_space_cachep
, e
);
762 e
->bitmap
= kzalloc(PAGE_SIZE
, GFP_NOFS
);
765 btrfs_free_space_cachep
, e
);
768 spin_lock(&ctl
->tree_lock
);
769 ret
= link_free_space(ctl
, e
);
770 ctl
->total_bitmaps
++;
771 ctl
->op
->recalc_thresholds(ctl
);
772 spin_unlock(&ctl
->tree_lock
);
775 "Duplicate entries in free space cache, dumping");
776 kmem_cache_free(btrfs_free_space_cachep
, e
);
779 list_add_tail(&e
->list
, &bitmaps
);
785 io_ctl_unmap_page(&io_ctl
);
788 * We add the bitmaps at the end of the entries in order that
789 * the bitmap entries are added to the cache.
791 list_for_each_entry_safe(e
, n
, &bitmaps
, list
) {
792 list_del_init(&e
->list
);
793 ret
= io_ctl_read_bitmap(&io_ctl
, e
);
798 io_ctl_drop_pages(&io_ctl
);
799 merge_space_tree(ctl
);
802 io_ctl_free(&io_ctl
);
805 io_ctl_drop_pages(&io_ctl
);
806 __btrfs_remove_free_space_cache(ctl
);
810 int load_free_space_cache(struct btrfs_fs_info
*fs_info
,
811 struct btrfs_block_group_cache
*block_group
)
813 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
815 struct btrfs_path
*path
;
818 u64 used
= btrfs_block_group_used(&block_group
->item
);
821 * If this block group has been marked to be cleared for one reason or
822 * another then we can't trust the on disk cache, so just return.
824 spin_lock(&block_group
->lock
);
825 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
826 spin_unlock(&block_group
->lock
);
829 spin_unlock(&block_group
->lock
);
831 path
= btrfs_alloc_path();
834 path
->search_commit_root
= 1;
835 path
->skip_locking
= 1;
837 inode
= lookup_free_space_inode(fs_info
, block_group
, path
);
839 btrfs_free_path(path
);
843 /* We may have converted the inode and made the cache invalid. */
844 spin_lock(&block_group
->lock
);
845 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
846 spin_unlock(&block_group
->lock
);
847 btrfs_free_path(path
);
850 spin_unlock(&block_group
->lock
);
852 ret
= __load_free_space_cache(fs_info
->tree_root
, inode
, ctl
,
853 path
, block_group
->key
.objectid
);
854 btrfs_free_path(path
);
858 spin_lock(&ctl
->tree_lock
);
859 matched
= (ctl
->free_space
== (block_group
->key
.offset
- used
-
860 block_group
->bytes_super
));
861 spin_unlock(&ctl
->tree_lock
);
864 __btrfs_remove_free_space_cache(ctl
);
866 "block group %llu has wrong amount of free space",
867 block_group
->key
.objectid
);
872 /* This cache is bogus, make sure it gets cleared */
873 spin_lock(&block_group
->lock
);
874 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
875 spin_unlock(&block_group
->lock
);
879 "failed to load free space cache for block group %llu, rebuilding it now",
880 block_group
->key
.objectid
);
887 static noinline_for_stack
888 int write_cache_extent_entries(struct btrfs_io_ctl
*io_ctl
,
889 struct btrfs_free_space_ctl
*ctl
,
890 struct btrfs_block_group_cache
*block_group
,
891 int *entries
, int *bitmaps
,
892 struct list_head
*bitmap_list
)
895 struct btrfs_free_cluster
*cluster
= NULL
;
896 struct btrfs_free_cluster
*cluster_locked
= NULL
;
897 struct rb_node
*node
= rb_first(&ctl
->free_space_offset
);
898 struct btrfs_trim_range
*trim_entry
;
900 /* Get the cluster for this block_group if it exists */
901 if (block_group
&& !list_empty(&block_group
->cluster_list
)) {
902 cluster
= list_entry(block_group
->cluster_list
.next
,
903 struct btrfs_free_cluster
,
907 if (!node
&& cluster
) {
908 cluster_locked
= cluster
;
909 spin_lock(&cluster_locked
->lock
);
910 node
= rb_first(&cluster
->root
);
914 /* Write out the extent entries */
916 struct btrfs_free_space
*e
;
918 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
921 ret
= io_ctl_add_entry(io_ctl
, e
->offset
, e
->bytes
,
927 list_add_tail(&e
->list
, bitmap_list
);
930 node
= rb_next(node
);
931 if (!node
&& cluster
) {
932 node
= rb_first(&cluster
->root
);
933 cluster_locked
= cluster
;
934 spin_lock(&cluster_locked
->lock
);
938 if (cluster_locked
) {
939 spin_unlock(&cluster_locked
->lock
);
940 cluster_locked
= NULL
;
944 * Make sure we don't miss any range that was removed from our rbtree
945 * because trimming is running. Otherwise after a umount+mount (or crash
946 * after committing the transaction) we would leak free space and get
947 * an inconsistent free space cache report from fsck.
949 list_for_each_entry(trim_entry
, &ctl
->trimming_ranges
, list
) {
950 ret
= io_ctl_add_entry(io_ctl
, trim_entry
->start
,
951 trim_entry
->bytes
, NULL
);
960 spin_unlock(&cluster_locked
->lock
);
964 static noinline_for_stack
int
965 update_cache_item(struct btrfs_trans_handle
*trans
,
966 struct btrfs_root
*root
,
968 struct btrfs_path
*path
, u64 offset
,
969 int entries
, int bitmaps
)
971 struct btrfs_key key
;
972 struct btrfs_free_space_header
*header
;
973 struct extent_buffer
*leaf
;
976 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
980 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
982 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, inode
->i_size
- 1,
983 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, NULL
);
986 leaf
= path
->nodes
[0];
988 struct btrfs_key found_key
;
989 ASSERT(path
->slots
[0]);
991 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
992 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
993 found_key
.offset
!= offset
) {
994 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0,
996 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0,
998 btrfs_release_path(path
);
1003 BTRFS_I(inode
)->generation
= trans
->transid
;
1004 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
1005 struct btrfs_free_space_header
);
1006 btrfs_set_free_space_entries(leaf
, header
, entries
);
1007 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
1008 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
1009 btrfs_mark_buffer_dirty(leaf
);
1010 btrfs_release_path(path
);
1018 static noinline_for_stack
int
1019 write_pinned_extent_entries(struct btrfs_fs_info
*fs_info
,
1020 struct btrfs_block_group_cache
*block_group
,
1021 struct btrfs_io_ctl
*io_ctl
,
1024 u64 start
, extent_start
, extent_end
, len
;
1025 struct extent_io_tree
*unpin
= NULL
;
1032 * We want to add any pinned extents to our free space cache
1033 * so we don't leak the space
1035 * We shouldn't have switched the pinned extents yet so this is the
1038 unpin
= fs_info
->pinned_extents
;
1040 start
= block_group
->key
.objectid
;
1042 while (start
< block_group
->key
.objectid
+ block_group
->key
.offset
) {
1043 ret
= find_first_extent_bit(unpin
, start
,
1044 &extent_start
, &extent_end
,
1045 EXTENT_DIRTY
, NULL
);
1049 /* This pinned extent is out of our range */
1050 if (extent_start
>= block_group
->key
.objectid
+
1051 block_group
->key
.offset
)
1054 extent_start
= max(extent_start
, start
);
1055 extent_end
= min(block_group
->key
.objectid
+
1056 block_group
->key
.offset
, extent_end
+ 1);
1057 len
= extent_end
- extent_start
;
1060 ret
= io_ctl_add_entry(io_ctl
, extent_start
, len
, NULL
);
1070 static noinline_for_stack
int
1071 write_bitmap_entries(struct btrfs_io_ctl
*io_ctl
, struct list_head
*bitmap_list
)
1073 struct btrfs_free_space
*entry
, *next
;
1076 /* Write out the bitmaps */
1077 list_for_each_entry_safe(entry
, next
, bitmap_list
, list
) {
1078 ret
= io_ctl_add_bitmap(io_ctl
, entry
->bitmap
);
1081 list_del_init(&entry
->list
);
1087 static int flush_dirty_cache(struct inode
*inode
)
1091 ret
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
1093 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, inode
->i_size
- 1,
1094 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, NULL
);
1099 static void noinline_for_stack
1100 cleanup_bitmap_list(struct list_head
*bitmap_list
)
1102 struct btrfs_free_space
*entry
, *next
;
1104 list_for_each_entry_safe(entry
, next
, bitmap_list
, list
)
1105 list_del_init(&entry
->list
);
1108 static void noinline_for_stack
1109 cleanup_write_cache_enospc(struct inode
*inode
,
1110 struct btrfs_io_ctl
*io_ctl
,
1111 struct extent_state
**cached_state
)
1113 io_ctl_drop_pages(io_ctl
);
1114 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
1115 i_size_read(inode
) - 1, cached_state
);
1118 static int __btrfs_wait_cache_io(struct btrfs_root
*root
,
1119 struct btrfs_trans_handle
*trans
,
1120 struct btrfs_block_group_cache
*block_group
,
1121 struct btrfs_io_ctl
*io_ctl
,
1122 struct btrfs_path
*path
, u64 offset
)
1125 struct inode
*inode
= io_ctl
->inode
;
1126 struct btrfs_fs_info
*fs_info
;
1131 fs_info
= btrfs_sb(inode
->i_sb
);
1133 /* Flush the dirty pages in the cache file. */
1134 ret
= flush_dirty_cache(inode
);
1138 /* Update the cache item to tell everyone this cache file is valid. */
1139 ret
= update_cache_item(trans
, root
, inode
, path
, offset
,
1140 io_ctl
->entries
, io_ctl
->bitmaps
);
1142 io_ctl_free(io_ctl
);
1144 invalidate_inode_pages2(inode
->i_mapping
);
1145 BTRFS_I(inode
)->generation
= 0;
1149 "failed to write free space cache for block group %llu",
1150 block_group
->key
.objectid
);
1154 btrfs_update_inode(trans
, root
, inode
);
1157 /* the dirty list is protected by the dirty_bgs_lock */
1158 spin_lock(&trans
->transaction
->dirty_bgs_lock
);
1160 /* the disk_cache_state is protected by the block group lock */
1161 spin_lock(&block_group
->lock
);
1164 * only mark this as written if we didn't get put back on
1165 * the dirty list while waiting for IO. Otherwise our
1166 * cache state won't be right, and we won't get written again
1168 if (!ret
&& list_empty(&block_group
->dirty_list
))
1169 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
1171 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
1173 spin_unlock(&block_group
->lock
);
1174 spin_unlock(&trans
->transaction
->dirty_bgs_lock
);
1175 io_ctl
->inode
= NULL
;
1183 static int btrfs_wait_cache_io_root(struct btrfs_root
*root
,
1184 struct btrfs_trans_handle
*trans
,
1185 struct btrfs_io_ctl
*io_ctl
,
1186 struct btrfs_path
*path
)
1188 return __btrfs_wait_cache_io(root
, trans
, NULL
, io_ctl
, path
, 0);
1191 int btrfs_wait_cache_io(struct btrfs_trans_handle
*trans
,
1192 struct btrfs_block_group_cache
*block_group
,
1193 struct btrfs_path
*path
)
1195 return __btrfs_wait_cache_io(block_group
->fs_info
->tree_root
, trans
,
1196 block_group
, &block_group
->io_ctl
,
1197 path
, block_group
->key
.objectid
);
1201 * __btrfs_write_out_cache - write out cached info to an inode
1202 * @root - the root the inode belongs to
1203 * @ctl - the free space cache we are going to write out
1204 * @block_group - the block_group for this cache if it belongs to a block_group
1205 * @trans - the trans handle
1207 * This function writes out a free space cache struct to disk for quick recovery
1208 * on mount. This will return 0 if it was successful in writing the cache out,
1209 * or an errno if it was not.
1211 static int __btrfs_write_out_cache(struct btrfs_root
*root
, struct inode
*inode
,
1212 struct btrfs_free_space_ctl
*ctl
,
1213 struct btrfs_block_group_cache
*block_group
,
1214 struct btrfs_io_ctl
*io_ctl
,
1215 struct btrfs_trans_handle
*trans
)
1217 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1218 struct extent_state
*cached_state
= NULL
;
1219 LIST_HEAD(bitmap_list
);
1225 if (!i_size_read(inode
))
1228 WARN_ON(io_ctl
->pages
);
1229 ret
= io_ctl_init(io_ctl
, inode
, 1);
1233 if (block_group
&& (block_group
->flags
& BTRFS_BLOCK_GROUP_DATA
)) {
1234 down_write(&block_group
->data_rwsem
);
1235 spin_lock(&block_group
->lock
);
1236 if (block_group
->delalloc_bytes
) {
1237 block_group
->disk_cache_state
= BTRFS_DC_WRITTEN
;
1238 spin_unlock(&block_group
->lock
);
1239 up_write(&block_group
->data_rwsem
);
1240 BTRFS_I(inode
)->generation
= 0;
1245 spin_unlock(&block_group
->lock
);
1248 /* Lock all pages first so we can lock the extent safely. */
1249 ret
= io_ctl_prepare_pages(io_ctl
, inode
, 0);
1253 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
1256 io_ctl_set_generation(io_ctl
, trans
->transid
);
1258 mutex_lock(&ctl
->cache_writeout_mutex
);
1259 /* Write out the extent entries in the free space cache */
1260 spin_lock(&ctl
->tree_lock
);
1261 ret
= write_cache_extent_entries(io_ctl
, ctl
,
1262 block_group
, &entries
, &bitmaps
,
1265 goto out_nospc_locked
;
1268 * Some spaces that are freed in the current transaction are pinned,
1269 * they will be added into free space cache after the transaction is
1270 * committed, we shouldn't lose them.
1272 * If this changes while we are working we'll get added back to
1273 * the dirty list and redo it. No locking needed
1275 ret
= write_pinned_extent_entries(fs_info
, block_group
,
1278 goto out_nospc_locked
;
1281 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1282 * locked while doing it because a concurrent trim can be manipulating
1283 * or freeing the bitmap.
1285 ret
= write_bitmap_entries(io_ctl
, &bitmap_list
);
1286 spin_unlock(&ctl
->tree_lock
);
1287 mutex_unlock(&ctl
->cache_writeout_mutex
);
1291 /* Zero out the rest of the pages just to make sure */
1292 io_ctl_zero_remaining_pages(io_ctl
);
1294 /* Everything is written out, now we dirty the pages in the file. */
1295 ret
= btrfs_dirty_pages(inode
, io_ctl
->pages
, io_ctl
->num_pages
, 0,
1296 i_size_read(inode
), &cached_state
);
1300 if (block_group
&& (block_group
->flags
& BTRFS_BLOCK_GROUP_DATA
))
1301 up_write(&block_group
->data_rwsem
);
1303 * Release the pages and unlock the extent, we will flush
1306 io_ctl_drop_pages(io_ctl
);
1308 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
1309 i_size_read(inode
) - 1, &cached_state
);
1312 * at this point the pages are under IO and we're happy,
1313 * The caller is responsible for waiting on them and updating the
1314 * the cache and the inode
1316 io_ctl
->entries
= entries
;
1317 io_ctl
->bitmaps
= bitmaps
;
1319 ret
= btrfs_fdatawrite_range(inode
, 0, (u64
)-1);
1326 io_ctl
->inode
= NULL
;
1327 io_ctl_free(io_ctl
);
1329 invalidate_inode_pages2(inode
->i_mapping
);
1330 BTRFS_I(inode
)->generation
= 0;
1332 btrfs_update_inode(trans
, root
, inode
);
1338 cleanup_bitmap_list(&bitmap_list
);
1339 spin_unlock(&ctl
->tree_lock
);
1340 mutex_unlock(&ctl
->cache_writeout_mutex
);
1343 cleanup_write_cache_enospc(inode
, io_ctl
, &cached_state
);
1346 if (block_group
&& (block_group
->flags
& BTRFS_BLOCK_GROUP_DATA
))
1347 up_write(&block_group
->data_rwsem
);
1352 int btrfs_write_out_cache(struct btrfs_fs_info
*fs_info
,
1353 struct btrfs_trans_handle
*trans
,
1354 struct btrfs_block_group_cache
*block_group
,
1355 struct btrfs_path
*path
)
1357 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1358 struct inode
*inode
;
1361 spin_lock(&block_group
->lock
);
1362 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
1363 spin_unlock(&block_group
->lock
);
1366 spin_unlock(&block_group
->lock
);
1368 inode
= lookup_free_space_inode(fs_info
, block_group
, path
);
1372 ret
= __btrfs_write_out_cache(fs_info
->tree_root
, inode
, ctl
,
1373 block_group
, &block_group
->io_ctl
, trans
);
1377 "failed to write free space cache for block group %llu",
1378 block_group
->key
.objectid
);
1380 spin_lock(&block_group
->lock
);
1381 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
1382 spin_unlock(&block_group
->lock
);
1384 block_group
->io_ctl
.inode
= NULL
;
1389 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1390 * to wait for IO and put the inode
1396 static inline unsigned long offset_to_bit(u64 bitmap_start
, u32 unit
,
1399 ASSERT(offset
>= bitmap_start
);
1400 offset
-= bitmap_start
;
1401 return (unsigned long)(div_u64(offset
, unit
));
1404 static inline unsigned long bytes_to_bits(u64 bytes
, u32 unit
)
1406 return (unsigned long)(div_u64(bytes
, unit
));
1409 static inline u64
offset_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1413 u64 bytes_per_bitmap
;
1415 bytes_per_bitmap
= BITS_PER_BITMAP
* ctl
->unit
;
1416 bitmap_start
= offset
- ctl
->start
;
1417 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
1418 bitmap_start
*= bytes_per_bitmap
;
1419 bitmap_start
+= ctl
->start
;
1421 return bitmap_start
;
1424 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
1425 struct rb_node
*node
, int bitmap
)
1427 struct rb_node
**p
= &root
->rb_node
;
1428 struct rb_node
*parent
= NULL
;
1429 struct btrfs_free_space
*info
;
1433 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
1435 if (offset
< info
->offset
) {
1437 } else if (offset
> info
->offset
) {
1438 p
= &(*p
)->rb_right
;
1441 * we could have a bitmap entry and an extent entry
1442 * share the same offset. If this is the case, we want
1443 * the extent entry to always be found first if we do a
1444 * linear search through the tree, since we want to have
1445 * the quickest allocation time, and allocating from an
1446 * extent is faster than allocating from a bitmap. So
1447 * if we're inserting a bitmap and we find an entry at
1448 * this offset, we want to go right, or after this entry
1449 * logically. If we are inserting an extent and we've
1450 * found a bitmap, we want to go left, or before
1458 p
= &(*p
)->rb_right
;
1460 if (!info
->bitmap
) {
1469 rb_link_node(node
, parent
, p
);
1470 rb_insert_color(node
, root
);
1476 * searches the tree for the given offset.
1478 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1479 * want a section that has at least bytes size and comes at or after the given
1482 static struct btrfs_free_space
*
1483 tree_search_offset(struct btrfs_free_space_ctl
*ctl
,
1484 u64 offset
, int bitmap_only
, int fuzzy
)
1486 struct rb_node
*n
= ctl
->free_space_offset
.rb_node
;
1487 struct btrfs_free_space
*entry
, *prev
= NULL
;
1489 /* find entry that is closest to the 'offset' */
1496 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1499 if (offset
< entry
->offset
)
1501 else if (offset
> entry
->offset
)
1514 * bitmap entry and extent entry may share same offset,
1515 * in that case, bitmap entry comes after extent entry.
1520 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1521 if (entry
->offset
!= offset
)
1524 WARN_ON(!entry
->bitmap
);
1527 if (entry
->bitmap
) {
1529 * if previous extent entry covers the offset,
1530 * we should return it instead of the bitmap entry
1532 n
= rb_prev(&entry
->offset_index
);
1534 prev
= rb_entry(n
, struct btrfs_free_space
,
1536 if (!prev
->bitmap
&&
1537 prev
->offset
+ prev
->bytes
> offset
)
1547 /* find last entry before the 'offset' */
1549 if (entry
->offset
> offset
) {
1550 n
= rb_prev(&entry
->offset_index
);
1552 entry
= rb_entry(n
, struct btrfs_free_space
,
1554 ASSERT(entry
->offset
<= offset
);
1563 if (entry
->bitmap
) {
1564 n
= rb_prev(&entry
->offset_index
);
1566 prev
= rb_entry(n
, struct btrfs_free_space
,
1568 if (!prev
->bitmap
&&
1569 prev
->offset
+ prev
->bytes
> offset
)
1572 if (entry
->offset
+ BITS_PER_BITMAP
* ctl
->unit
> offset
)
1574 } else if (entry
->offset
+ entry
->bytes
> offset
)
1581 if (entry
->bitmap
) {
1582 if (entry
->offset
+ BITS_PER_BITMAP
*
1586 if (entry
->offset
+ entry
->bytes
> offset
)
1590 n
= rb_next(&entry
->offset_index
);
1593 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1599 __unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1600 struct btrfs_free_space
*info
)
1602 rb_erase(&info
->offset_index
, &ctl
->free_space_offset
);
1603 ctl
->free_extents
--;
1606 static void unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1607 struct btrfs_free_space
*info
)
1609 __unlink_free_space(ctl
, info
);
1610 ctl
->free_space
-= info
->bytes
;
1613 static int link_free_space(struct btrfs_free_space_ctl
*ctl
,
1614 struct btrfs_free_space
*info
)
1618 ASSERT(info
->bytes
|| info
->bitmap
);
1619 ret
= tree_insert_offset(&ctl
->free_space_offset
, info
->offset
,
1620 &info
->offset_index
, (info
->bitmap
!= NULL
));
1624 ctl
->free_space
+= info
->bytes
;
1625 ctl
->free_extents
++;
1629 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
1631 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1635 u64 size
= block_group
->key
.offset
;
1636 u64 bytes_per_bg
= BITS_PER_BITMAP
* ctl
->unit
;
1637 u64 max_bitmaps
= div64_u64(size
+ bytes_per_bg
- 1, bytes_per_bg
);
1639 max_bitmaps
= max_t(u64
, max_bitmaps
, 1);
1641 ASSERT(ctl
->total_bitmaps
<= max_bitmaps
);
1644 * The goal is to keep the total amount of memory used per 1gb of space
1645 * at or below 32k, so we need to adjust how much memory we allow to be
1646 * used by extent based free space tracking
1649 max_bytes
= MAX_CACHE_BYTES_PER_GIG
;
1651 max_bytes
= MAX_CACHE_BYTES_PER_GIG
* div_u64(size
, SZ_1G
);
1654 * we want to account for 1 more bitmap than what we have so we can make
1655 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1656 * we add more bitmaps.
1658 bitmap_bytes
= (ctl
->total_bitmaps
+ 1) * ctl
->unit
;
1660 if (bitmap_bytes
>= max_bytes
) {
1661 ctl
->extents_thresh
= 0;
1666 * we want the extent entry threshold to always be at most 1/2 the max
1667 * bytes we can have, or whatever is less than that.
1669 extent_bytes
= max_bytes
- bitmap_bytes
;
1670 extent_bytes
= min_t(u64
, extent_bytes
, max_bytes
>> 1);
1672 ctl
->extents_thresh
=
1673 div_u64(extent_bytes
, sizeof(struct btrfs_free_space
));
1676 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1677 struct btrfs_free_space
*info
,
1678 u64 offset
, u64 bytes
)
1680 unsigned long start
, count
;
1682 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1683 count
= bytes_to_bits(bytes
, ctl
->unit
);
1684 ASSERT(start
+ count
<= BITS_PER_BITMAP
);
1686 bitmap_clear(info
->bitmap
, start
, count
);
1688 info
->bytes
-= bytes
;
1691 static void bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1692 struct btrfs_free_space
*info
, u64 offset
,
1695 __bitmap_clear_bits(ctl
, info
, offset
, bytes
);
1696 ctl
->free_space
-= bytes
;
1699 static void bitmap_set_bits(struct btrfs_free_space_ctl
*ctl
,
1700 struct btrfs_free_space
*info
, u64 offset
,
1703 unsigned long start
, count
;
1705 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1706 count
= bytes_to_bits(bytes
, ctl
->unit
);
1707 ASSERT(start
+ count
<= BITS_PER_BITMAP
);
1709 bitmap_set(info
->bitmap
, start
, count
);
1711 info
->bytes
+= bytes
;
1712 ctl
->free_space
+= bytes
;
1716 * If we can not find suitable extent, we will use bytes to record
1717 * the size of the max extent.
1719 static int search_bitmap(struct btrfs_free_space_ctl
*ctl
,
1720 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1721 u64
*bytes
, bool for_alloc
)
1723 unsigned long found_bits
= 0;
1724 unsigned long max_bits
= 0;
1725 unsigned long bits
, i
;
1726 unsigned long next_zero
;
1727 unsigned long extent_bits
;
1730 * Skip searching the bitmap if we don't have a contiguous section that
1731 * is large enough for this allocation.
1734 bitmap_info
->max_extent_size
&&
1735 bitmap_info
->max_extent_size
< *bytes
) {
1736 *bytes
= bitmap_info
->max_extent_size
;
1740 i
= offset_to_bit(bitmap_info
->offset
, ctl
->unit
,
1741 max_t(u64
, *offset
, bitmap_info
->offset
));
1742 bits
= bytes_to_bits(*bytes
, ctl
->unit
);
1744 for_each_set_bit_from(i
, bitmap_info
->bitmap
, BITS_PER_BITMAP
) {
1745 if (for_alloc
&& bits
== 1) {
1749 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1750 BITS_PER_BITMAP
, i
);
1751 extent_bits
= next_zero
- i
;
1752 if (extent_bits
>= bits
) {
1753 found_bits
= extent_bits
;
1755 } else if (extent_bits
> max_bits
) {
1756 max_bits
= extent_bits
;
1762 *offset
= (u64
)(i
* ctl
->unit
) + bitmap_info
->offset
;
1763 *bytes
= (u64
)(found_bits
) * ctl
->unit
;
1767 *bytes
= (u64
)(max_bits
) * ctl
->unit
;
1768 bitmap_info
->max_extent_size
= *bytes
;
1772 /* Cache the size of the max extent in bytes */
1773 static struct btrfs_free_space
*
1774 find_free_space(struct btrfs_free_space_ctl
*ctl
, u64
*offset
, u64
*bytes
,
1775 unsigned long align
, u64
*max_extent_size
)
1777 struct btrfs_free_space
*entry
;
1778 struct rb_node
*node
;
1783 if (!ctl
->free_space_offset
.rb_node
)
1786 entry
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, *offset
), 0, 1);
1790 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1791 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1792 if (entry
->bytes
< *bytes
) {
1793 if (entry
->bytes
> *max_extent_size
)
1794 *max_extent_size
= entry
->bytes
;
1798 /* make sure the space returned is big enough
1799 * to match our requested alignment
1801 if (*bytes
>= align
) {
1802 tmp
= entry
->offset
- ctl
->start
+ align
- 1;
1803 tmp
= div64_u64(tmp
, align
);
1804 tmp
= tmp
* align
+ ctl
->start
;
1805 align_off
= tmp
- entry
->offset
;
1808 tmp
= entry
->offset
;
1811 if (entry
->bytes
< *bytes
+ align_off
) {
1812 if (entry
->bytes
> *max_extent_size
)
1813 *max_extent_size
= entry
->bytes
;
1817 if (entry
->bitmap
) {
1820 ret
= search_bitmap(ctl
, entry
, &tmp
, &size
, true);
1825 } else if (size
> *max_extent_size
) {
1826 *max_extent_size
= size
;
1832 *bytes
= entry
->bytes
- align_off
;
1839 static void add_new_bitmap(struct btrfs_free_space_ctl
*ctl
,
1840 struct btrfs_free_space
*info
, u64 offset
)
1842 info
->offset
= offset_to_bitmap(ctl
, offset
);
1844 INIT_LIST_HEAD(&info
->list
);
1845 link_free_space(ctl
, info
);
1846 ctl
->total_bitmaps
++;
1848 ctl
->op
->recalc_thresholds(ctl
);
1851 static void free_bitmap(struct btrfs_free_space_ctl
*ctl
,
1852 struct btrfs_free_space
*bitmap_info
)
1854 unlink_free_space(ctl
, bitmap_info
);
1855 kfree(bitmap_info
->bitmap
);
1856 kmem_cache_free(btrfs_free_space_cachep
, bitmap_info
);
1857 ctl
->total_bitmaps
--;
1858 ctl
->op
->recalc_thresholds(ctl
);
1861 static noinline
int remove_from_bitmap(struct btrfs_free_space_ctl
*ctl
,
1862 struct btrfs_free_space
*bitmap_info
,
1863 u64
*offset
, u64
*bytes
)
1866 u64 search_start
, search_bytes
;
1870 end
= bitmap_info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
) - 1;
1873 * We need to search for bits in this bitmap. We could only cover some
1874 * of the extent in this bitmap thanks to how we add space, so we need
1875 * to search for as much as it as we can and clear that amount, and then
1876 * go searching for the next bit.
1878 search_start
= *offset
;
1879 search_bytes
= ctl
->unit
;
1880 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1881 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
, &search_bytes
,
1883 if (ret
< 0 || search_start
!= *offset
)
1886 /* We may have found more bits than what we need */
1887 search_bytes
= min(search_bytes
, *bytes
);
1889 /* Cannot clear past the end of the bitmap */
1890 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1892 bitmap_clear_bits(ctl
, bitmap_info
, search_start
, search_bytes
);
1893 *offset
+= search_bytes
;
1894 *bytes
-= search_bytes
;
1897 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1898 if (!bitmap_info
->bytes
)
1899 free_bitmap(ctl
, bitmap_info
);
1902 * no entry after this bitmap, but we still have bytes to
1903 * remove, so something has gone wrong.
1908 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1912 * if the next entry isn't a bitmap we need to return to let the
1913 * extent stuff do its work.
1915 if (!bitmap_info
->bitmap
)
1919 * Ok the next item is a bitmap, but it may not actually hold
1920 * the information for the rest of this free space stuff, so
1921 * look for it, and if we don't find it return so we can try
1922 * everything over again.
1924 search_start
= *offset
;
1925 search_bytes
= ctl
->unit
;
1926 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
,
1927 &search_bytes
, false);
1928 if (ret
< 0 || search_start
!= *offset
)
1932 } else if (!bitmap_info
->bytes
)
1933 free_bitmap(ctl
, bitmap_info
);
1938 static u64
add_bytes_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1939 struct btrfs_free_space
*info
, u64 offset
,
1942 u64 bytes_to_set
= 0;
1945 end
= info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
);
1947 bytes_to_set
= min(end
- offset
, bytes
);
1949 bitmap_set_bits(ctl
, info
, offset
, bytes_to_set
);
1952 * We set some bytes, we have no idea what the max extent size is
1955 info
->max_extent_size
= 0;
1957 return bytes_to_set
;
1961 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
1962 struct btrfs_free_space
*info
)
1964 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1965 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
1966 bool forced
= false;
1968 #ifdef CONFIG_BTRFS_DEBUG
1969 if (btrfs_should_fragment_free_space(block_group
))
1974 * If we are below the extents threshold then we can add this as an
1975 * extent, and don't have to deal with the bitmap
1977 if (!forced
&& ctl
->free_extents
< ctl
->extents_thresh
) {
1979 * If this block group has some small extents we don't want to
1980 * use up all of our free slots in the cache with them, we want
1981 * to reserve them to larger extents, however if we have plenty
1982 * of cache left then go ahead an dadd them, no sense in adding
1983 * the overhead of a bitmap if we don't have to.
1985 if (info
->bytes
<= fs_info
->sectorsize
* 4) {
1986 if (ctl
->free_extents
* 2 <= ctl
->extents_thresh
)
1994 * The original block groups from mkfs can be really small, like 8
1995 * megabytes, so don't bother with a bitmap for those entries. However
1996 * some block groups can be smaller than what a bitmap would cover but
1997 * are still large enough that they could overflow the 32k memory limit,
1998 * so allow those block groups to still be allowed to have a bitmap
2001 if (((BITS_PER_BITMAP
* ctl
->unit
) >> 1) > block_group
->key
.offset
)
2007 static const struct btrfs_free_space_op free_space_op
= {
2008 .recalc_thresholds
= recalculate_thresholds
,
2009 .use_bitmap
= use_bitmap
,
2012 static int insert_into_bitmap(struct btrfs_free_space_ctl
*ctl
,
2013 struct btrfs_free_space
*info
)
2015 struct btrfs_free_space
*bitmap_info
;
2016 struct btrfs_block_group_cache
*block_group
= NULL
;
2018 u64 bytes
, offset
, bytes_added
;
2021 bytes
= info
->bytes
;
2022 offset
= info
->offset
;
2024 if (!ctl
->op
->use_bitmap(ctl
, info
))
2027 if (ctl
->op
== &free_space_op
)
2028 block_group
= ctl
->private;
2031 * Since we link bitmaps right into the cluster we need to see if we
2032 * have a cluster here, and if so and it has our bitmap we need to add
2033 * the free space to that bitmap.
2035 if (block_group
&& !list_empty(&block_group
->cluster_list
)) {
2036 struct btrfs_free_cluster
*cluster
;
2037 struct rb_node
*node
;
2038 struct btrfs_free_space
*entry
;
2040 cluster
= list_entry(block_group
->cluster_list
.next
,
2041 struct btrfs_free_cluster
,
2043 spin_lock(&cluster
->lock
);
2044 node
= rb_first(&cluster
->root
);
2046 spin_unlock(&cluster
->lock
);
2047 goto no_cluster_bitmap
;
2050 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2051 if (!entry
->bitmap
) {
2052 spin_unlock(&cluster
->lock
);
2053 goto no_cluster_bitmap
;
2056 if (entry
->offset
== offset_to_bitmap(ctl
, offset
)) {
2057 bytes_added
= add_bytes_to_bitmap(ctl
, entry
,
2059 bytes
-= bytes_added
;
2060 offset
+= bytes_added
;
2062 spin_unlock(&cluster
->lock
);
2070 bitmap_info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
2077 bytes_added
= add_bytes_to_bitmap(ctl
, bitmap_info
, offset
, bytes
);
2078 bytes
-= bytes_added
;
2079 offset
+= bytes_added
;
2089 if (info
&& info
->bitmap
) {
2090 add_new_bitmap(ctl
, info
, offset
);
2095 spin_unlock(&ctl
->tree_lock
);
2097 /* no pre-allocated info, allocate a new one */
2099 info
= kmem_cache_zalloc(btrfs_free_space_cachep
,
2102 spin_lock(&ctl
->tree_lock
);
2108 /* allocate the bitmap */
2109 info
->bitmap
= kzalloc(PAGE_SIZE
, GFP_NOFS
);
2110 spin_lock(&ctl
->tree_lock
);
2111 if (!info
->bitmap
) {
2121 kfree(info
->bitmap
);
2122 kmem_cache_free(btrfs_free_space_cachep
, info
);
2128 static bool try_merge_free_space(struct btrfs_free_space_ctl
*ctl
,
2129 struct btrfs_free_space
*info
, bool update_stat
)
2131 struct btrfs_free_space
*left_info
;
2132 struct btrfs_free_space
*right_info
;
2133 bool merged
= false;
2134 u64 offset
= info
->offset
;
2135 u64 bytes
= info
->bytes
;
2138 * first we want to see if there is free space adjacent to the range we
2139 * are adding, if there is remove that struct and add a new one to
2140 * cover the entire range
2142 right_info
= tree_search_offset(ctl
, offset
+ bytes
, 0, 0);
2143 if (right_info
&& rb_prev(&right_info
->offset_index
))
2144 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
2145 struct btrfs_free_space
, offset_index
);
2147 left_info
= tree_search_offset(ctl
, offset
- 1, 0, 0);
2149 if (right_info
&& !right_info
->bitmap
) {
2151 unlink_free_space(ctl
, right_info
);
2153 __unlink_free_space(ctl
, right_info
);
2154 info
->bytes
+= right_info
->bytes
;
2155 kmem_cache_free(btrfs_free_space_cachep
, right_info
);
2159 if (left_info
&& !left_info
->bitmap
&&
2160 left_info
->offset
+ left_info
->bytes
== offset
) {
2162 unlink_free_space(ctl
, left_info
);
2164 __unlink_free_space(ctl
, left_info
);
2165 info
->offset
= left_info
->offset
;
2166 info
->bytes
+= left_info
->bytes
;
2167 kmem_cache_free(btrfs_free_space_cachep
, left_info
);
2174 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl
*ctl
,
2175 struct btrfs_free_space
*info
,
2178 struct btrfs_free_space
*bitmap
;
2181 const u64 end
= info
->offset
+ info
->bytes
;
2182 const u64 bitmap_offset
= offset_to_bitmap(ctl
, end
);
2185 bitmap
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2189 i
= offset_to_bit(bitmap
->offset
, ctl
->unit
, end
);
2190 j
= find_next_zero_bit(bitmap
->bitmap
, BITS_PER_BITMAP
, i
);
2193 bytes
= (j
- i
) * ctl
->unit
;
2194 info
->bytes
+= bytes
;
2197 bitmap_clear_bits(ctl
, bitmap
, end
, bytes
);
2199 __bitmap_clear_bits(ctl
, bitmap
, end
, bytes
);
2202 free_bitmap(ctl
, bitmap
);
2207 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl
*ctl
,
2208 struct btrfs_free_space
*info
,
2211 struct btrfs_free_space
*bitmap
;
2215 unsigned long prev_j
;
2218 bitmap_offset
= offset_to_bitmap(ctl
, info
->offset
);
2219 /* If we're on a boundary, try the previous logical bitmap. */
2220 if (bitmap_offset
== info
->offset
) {
2221 if (info
->offset
== 0)
2223 bitmap_offset
= offset_to_bitmap(ctl
, info
->offset
- 1);
2226 bitmap
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2230 i
= offset_to_bit(bitmap
->offset
, ctl
->unit
, info
->offset
) - 1;
2232 prev_j
= (unsigned long)-1;
2233 for_each_clear_bit_from(j
, bitmap
->bitmap
, BITS_PER_BITMAP
) {
2241 if (prev_j
== (unsigned long)-1)
2242 bytes
= (i
+ 1) * ctl
->unit
;
2244 bytes
= (i
- prev_j
) * ctl
->unit
;
2246 info
->offset
-= bytes
;
2247 info
->bytes
+= bytes
;
2250 bitmap_clear_bits(ctl
, bitmap
, info
->offset
, bytes
);
2252 __bitmap_clear_bits(ctl
, bitmap
, info
->offset
, bytes
);
2255 free_bitmap(ctl
, bitmap
);
2261 * We prefer always to allocate from extent entries, both for clustered and
2262 * non-clustered allocation requests. So when attempting to add a new extent
2263 * entry, try to see if there's adjacent free space in bitmap entries, and if
2264 * there is, migrate that space from the bitmaps to the extent.
2265 * Like this we get better chances of satisfying space allocation requests
2266 * because we attempt to satisfy them based on a single cache entry, and never
2267 * on 2 or more entries - even if the entries represent a contiguous free space
2268 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2271 static void steal_from_bitmap(struct btrfs_free_space_ctl
*ctl
,
2272 struct btrfs_free_space
*info
,
2276 * Only work with disconnected entries, as we can change their offset,
2277 * and must be extent entries.
2279 ASSERT(!info
->bitmap
);
2280 ASSERT(RB_EMPTY_NODE(&info
->offset_index
));
2282 if (ctl
->total_bitmaps
> 0) {
2284 bool stole_front
= false;
2286 stole_end
= steal_from_bitmap_to_end(ctl
, info
, update_stat
);
2287 if (ctl
->total_bitmaps
> 0)
2288 stole_front
= steal_from_bitmap_to_front(ctl
, info
,
2291 if (stole_end
|| stole_front
)
2292 try_merge_free_space(ctl
, info
, update_stat
);
2296 int __btrfs_add_free_space(struct btrfs_fs_info
*fs_info
,
2297 struct btrfs_free_space_ctl
*ctl
,
2298 u64 offset
, u64 bytes
)
2300 struct btrfs_free_space
*info
;
2303 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
2307 info
->offset
= offset
;
2308 info
->bytes
= bytes
;
2309 RB_CLEAR_NODE(&info
->offset_index
);
2311 spin_lock(&ctl
->tree_lock
);
2313 if (try_merge_free_space(ctl
, info
, true))
2317 * There was no extent directly to the left or right of this new
2318 * extent then we know we're going to have to allocate a new extent, so
2319 * before we do that see if we need to drop this into a bitmap
2321 ret
= insert_into_bitmap(ctl
, info
);
2330 * Only steal free space from adjacent bitmaps if we're sure we're not
2331 * going to add the new free space to existing bitmap entries - because
2332 * that would mean unnecessary work that would be reverted. Therefore
2333 * attempt to steal space from bitmaps if we're adding an extent entry.
2335 steal_from_bitmap(ctl
, info
, true);
2337 ret
= link_free_space(ctl
, info
);
2339 kmem_cache_free(btrfs_free_space_cachep
, info
);
2341 spin_unlock(&ctl
->tree_lock
);
2344 btrfs_crit(fs_info
, "unable to add free space :%d", ret
);
2345 ASSERT(ret
!= -EEXIST
);
2351 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
2352 u64 offset
, u64 bytes
)
2354 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2355 struct btrfs_free_space
*info
;
2357 bool re_search
= false;
2359 spin_lock(&ctl
->tree_lock
);
2366 info
= tree_search_offset(ctl
, offset
, 0, 0);
2369 * oops didn't find an extent that matched the space we wanted
2370 * to remove, look for a bitmap instead
2372 info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
2376 * If we found a partial bit of our free space in a
2377 * bitmap but then couldn't find the other part this may
2378 * be a problem, so WARN about it.
2386 if (!info
->bitmap
) {
2387 unlink_free_space(ctl
, info
);
2388 if (offset
== info
->offset
) {
2389 u64 to_free
= min(bytes
, info
->bytes
);
2391 info
->bytes
-= to_free
;
2392 info
->offset
+= to_free
;
2394 ret
= link_free_space(ctl
, info
);
2397 kmem_cache_free(btrfs_free_space_cachep
, info
);
2404 u64 old_end
= info
->bytes
+ info
->offset
;
2406 info
->bytes
= offset
- info
->offset
;
2407 ret
= link_free_space(ctl
, info
);
2412 /* Not enough bytes in this entry to satisfy us */
2413 if (old_end
< offset
+ bytes
) {
2414 bytes
-= old_end
- offset
;
2417 } else if (old_end
== offset
+ bytes
) {
2421 spin_unlock(&ctl
->tree_lock
);
2423 ret
= btrfs_add_free_space(block_group
, offset
+ bytes
,
2424 old_end
- (offset
+ bytes
));
2430 ret
= remove_from_bitmap(ctl
, info
, &offset
, &bytes
);
2431 if (ret
== -EAGAIN
) {
2436 spin_unlock(&ctl
->tree_lock
);
2441 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
2444 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2445 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2446 struct btrfs_free_space
*info
;
2450 for (n
= rb_first(&ctl
->free_space_offset
); n
; n
= rb_next(n
)) {
2451 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
2452 if (info
->bytes
>= bytes
&& !block_group
->ro
)
2454 btrfs_crit(fs_info
, "entry offset %llu, bytes %llu, bitmap %s",
2455 info
->offset
, info
->bytes
,
2456 (info
->bitmap
) ? "yes" : "no");
2458 btrfs_info(fs_info
, "block group has cluster?: %s",
2459 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
2461 "%d blocks of free space at or bigger than bytes is", count
);
2464 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache
*block_group
)
2466 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2467 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2469 spin_lock_init(&ctl
->tree_lock
);
2470 ctl
->unit
= fs_info
->sectorsize
;
2471 ctl
->start
= block_group
->key
.objectid
;
2472 ctl
->private = block_group
;
2473 ctl
->op
= &free_space_op
;
2474 INIT_LIST_HEAD(&ctl
->trimming_ranges
);
2475 mutex_init(&ctl
->cache_writeout_mutex
);
2478 * we only want to have 32k of ram per block group for keeping
2479 * track of free space, and if we pass 1/2 of that we want to
2480 * start converting things over to using bitmaps
2482 ctl
->extents_thresh
= (SZ_32K
/ 2) / sizeof(struct btrfs_free_space
);
2486 * for a given cluster, put all of its extents back into the free
2487 * space cache. If the block group passed doesn't match the block group
2488 * pointed to by the cluster, someone else raced in and freed the
2489 * cluster already. In that case, we just return without changing anything
2492 __btrfs_return_cluster_to_free_space(
2493 struct btrfs_block_group_cache
*block_group
,
2494 struct btrfs_free_cluster
*cluster
)
2496 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2497 struct btrfs_free_space
*entry
;
2498 struct rb_node
*node
;
2500 spin_lock(&cluster
->lock
);
2501 if (cluster
->block_group
!= block_group
)
2504 cluster
->block_group
= NULL
;
2505 cluster
->window_start
= 0;
2506 list_del_init(&cluster
->block_group_list
);
2508 node
= rb_first(&cluster
->root
);
2512 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2513 node
= rb_next(&entry
->offset_index
);
2514 rb_erase(&entry
->offset_index
, &cluster
->root
);
2515 RB_CLEAR_NODE(&entry
->offset_index
);
2517 bitmap
= (entry
->bitmap
!= NULL
);
2519 try_merge_free_space(ctl
, entry
, false);
2520 steal_from_bitmap(ctl
, entry
, false);
2522 tree_insert_offset(&ctl
->free_space_offset
,
2523 entry
->offset
, &entry
->offset_index
, bitmap
);
2525 cluster
->root
= RB_ROOT
;
2528 spin_unlock(&cluster
->lock
);
2529 btrfs_put_block_group(block_group
);
2533 static void __btrfs_remove_free_space_cache_locked(
2534 struct btrfs_free_space_ctl
*ctl
)
2536 struct btrfs_free_space
*info
;
2537 struct rb_node
*node
;
2539 while ((node
= rb_last(&ctl
->free_space_offset
)) != NULL
) {
2540 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2541 if (!info
->bitmap
) {
2542 unlink_free_space(ctl
, info
);
2543 kmem_cache_free(btrfs_free_space_cachep
, info
);
2545 free_bitmap(ctl
, info
);
2548 cond_resched_lock(&ctl
->tree_lock
);
2552 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl
*ctl
)
2554 spin_lock(&ctl
->tree_lock
);
2555 __btrfs_remove_free_space_cache_locked(ctl
);
2556 spin_unlock(&ctl
->tree_lock
);
2559 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
2561 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2562 struct btrfs_free_cluster
*cluster
;
2563 struct list_head
*head
;
2565 spin_lock(&ctl
->tree_lock
);
2566 while ((head
= block_group
->cluster_list
.next
) !=
2567 &block_group
->cluster_list
) {
2568 cluster
= list_entry(head
, struct btrfs_free_cluster
,
2571 WARN_ON(cluster
->block_group
!= block_group
);
2572 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2574 cond_resched_lock(&ctl
->tree_lock
);
2576 __btrfs_remove_free_space_cache_locked(ctl
);
2577 spin_unlock(&ctl
->tree_lock
);
2581 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
2582 u64 offset
, u64 bytes
, u64 empty_size
,
2583 u64
*max_extent_size
)
2585 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2586 struct btrfs_free_space
*entry
= NULL
;
2587 u64 bytes_search
= bytes
+ empty_size
;
2590 u64 align_gap_len
= 0;
2592 spin_lock(&ctl
->tree_lock
);
2593 entry
= find_free_space(ctl
, &offset
, &bytes_search
,
2594 block_group
->full_stripe_len
, max_extent_size
);
2599 if (entry
->bitmap
) {
2600 bitmap_clear_bits(ctl
, entry
, offset
, bytes
);
2602 free_bitmap(ctl
, entry
);
2604 unlink_free_space(ctl
, entry
);
2605 align_gap_len
= offset
- entry
->offset
;
2606 align_gap
= entry
->offset
;
2608 entry
->offset
= offset
+ bytes
;
2609 WARN_ON(entry
->bytes
< bytes
+ align_gap_len
);
2611 entry
->bytes
-= bytes
+ align_gap_len
;
2613 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2615 link_free_space(ctl
, entry
);
2618 spin_unlock(&ctl
->tree_lock
);
2621 __btrfs_add_free_space(block_group
->fs_info
, ctl
,
2622 align_gap
, align_gap_len
);
2627 * given a cluster, put all of its extents back into the free space
2628 * cache. If a block group is passed, this function will only free
2629 * a cluster that belongs to the passed block group.
2631 * Otherwise, it'll get a reference on the block group pointed to by the
2632 * cluster and remove the cluster from it.
2634 int btrfs_return_cluster_to_free_space(
2635 struct btrfs_block_group_cache
*block_group
,
2636 struct btrfs_free_cluster
*cluster
)
2638 struct btrfs_free_space_ctl
*ctl
;
2641 /* first, get a safe pointer to the block group */
2642 spin_lock(&cluster
->lock
);
2644 block_group
= cluster
->block_group
;
2646 spin_unlock(&cluster
->lock
);
2649 } else if (cluster
->block_group
!= block_group
) {
2650 /* someone else has already freed it don't redo their work */
2651 spin_unlock(&cluster
->lock
);
2654 atomic_inc(&block_group
->count
);
2655 spin_unlock(&cluster
->lock
);
2657 ctl
= block_group
->free_space_ctl
;
2659 /* now return any extents the cluster had on it */
2660 spin_lock(&ctl
->tree_lock
);
2661 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2662 spin_unlock(&ctl
->tree_lock
);
2664 /* finally drop our ref */
2665 btrfs_put_block_group(block_group
);
2669 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
2670 struct btrfs_free_cluster
*cluster
,
2671 struct btrfs_free_space
*entry
,
2672 u64 bytes
, u64 min_start
,
2673 u64
*max_extent_size
)
2675 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2677 u64 search_start
= cluster
->window_start
;
2678 u64 search_bytes
= bytes
;
2681 search_start
= min_start
;
2682 search_bytes
= bytes
;
2684 err
= search_bitmap(ctl
, entry
, &search_start
, &search_bytes
, true);
2686 if (search_bytes
> *max_extent_size
)
2687 *max_extent_size
= search_bytes
;
2692 __bitmap_clear_bits(ctl
, entry
, ret
, bytes
);
2698 * given a cluster, try to allocate 'bytes' from it, returns 0
2699 * if it couldn't find anything suitably large, or a logical disk offset
2700 * if things worked out
2702 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
2703 struct btrfs_free_cluster
*cluster
, u64 bytes
,
2704 u64 min_start
, u64
*max_extent_size
)
2706 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2707 struct btrfs_free_space
*entry
= NULL
;
2708 struct rb_node
*node
;
2711 spin_lock(&cluster
->lock
);
2712 if (bytes
> cluster
->max_size
)
2715 if (cluster
->block_group
!= block_group
)
2718 node
= rb_first(&cluster
->root
);
2722 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2724 if (entry
->bytes
< bytes
&& entry
->bytes
> *max_extent_size
)
2725 *max_extent_size
= entry
->bytes
;
2727 if (entry
->bytes
< bytes
||
2728 (!entry
->bitmap
&& entry
->offset
< min_start
)) {
2729 node
= rb_next(&entry
->offset_index
);
2732 entry
= rb_entry(node
, struct btrfs_free_space
,
2737 if (entry
->bitmap
) {
2738 ret
= btrfs_alloc_from_bitmap(block_group
,
2739 cluster
, entry
, bytes
,
2740 cluster
->window_start
,
2743 node
= rb_next(&entry
->offset_index
);
2746 entry
= rb_entry(node
, struct btrfs_free_space
,
2750 cluster
->window_start
+= bytes
;
2752 ret
= entry
->offset
;
2754 entry
->offset
+= bytes
;
2755 entry
->bytes
-= bytes
;
2758 if (entry
->bytes
== 0)
2759 rb_erase(&entry
->offset_index
, &cluster
->root
);
2763 spin_unlock(&cluster
->lock
);
2768 spin_lock(&ctl
->tree_lock
);
2770 ctl
->free_space
-= bytes
;
2771 if (entry
->bytes
== 0) {
2772 ctl
->free_extents
--;
2773 if (entry
->bitmap
) {
2774 kfree(entry
->bitmap
);
2775 ctl
->total_bitmaps
--;
2776 ctl
->op
->recalc_thresholds(ctl
);
2778 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2781 spin_unlock(&ctl
->tree_lock
);
2786 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
2787 struct btrfs_free_space
*entry
,
2788 struct btrfs_free_cluster
*cluster
,
2789 u64 offset
, u64 bytes
,
2790 u64 cont1_bytes
, u64 min_bytes
)
2792 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2793 unsigned long next_zero
;
2795 unsigned long want_bits
;
2796 unsigned long min_bits
;
2797 unsigned long found_bits
;
2798 unsigned long max_bits
= 0;
2799 unsigned long start
= 0;
2800 unsigned long total_found
= 0;
2803 i
= offset_to_bit(entry
->offset
, ctl
->unit
,
2804 max_t(u64
, offset
, entry
->offset
));
2805 want_bits
= bytes_to_bits(bytes
, ctl
->unit
);
2806 min_bits
= bytes_to_bits(min_bytes
, ctl
->unit
);
2809 * Don't bother looking for a cluster in this bitmap if it's heavily
2812 if (entry
->max_extent_size
&&
2813 entry
->max_extent_size
< cont1_bytes
)
2817 for_each_set_bit_from(i
, entry
->bitmap
, BITS_PER_BITMAP
) {
2818 next_zero
= find_next_zero_bit(entry
->bitmap
,
2819 BITS_PER_BITMAP
, i
);
2820 if (next_zero
- i
>= min_bits
) {
2821 found_bits
= next_zero
- i
;
2822 if (found_bits
> max_bits
)
2823 max_bits
= found_bits
;
2826 if (next_zero
- i
> max_bits
)
2827 max_bits
= next_zero
- i
;
2832 entry
->max_extent_size
= (u64
)max_bits
* ctl
->unit
;
2838 cluster
->max_size
= 0;
2841 total_found
+= found_bits
;
2843 if (cluster
->max_size
< found_bits
* ctl
->unit
)
2844 cluster
->max_size
= found_bits
* ctl
->unit
;
2846 if (total_found
< want_bits
|| cluster
->max_size
< cont1_bytes
) {
2851 cluster
->window_start
= start
* ctl
->unit
+ entry
->offset
;
2852 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2853 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2854 &entry
->offset_index
, 1);
2855 ASSERT(!ret
); /* -EEXIST; Logic error */
2857 trace_btrfs_setup_cluster(block_group
, cluster
,
2858 total_found
* ctl
->unit
, 1);
2863 * This searches the block group for just extents to fill the cluster with.
2864 * Try to find a cluster with at least bytes total bytes, at least one
2865 * extent of cont1_bytes, and other clusters of at least min_bytes.
2868 setup_cluster_no_bitmap(struct btrfs_block_group_cache
*block_group
,
2869 struct btrfs_free_cluster
*cluster
,
2870 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2871 u64 cont1_bytes
, u64 min_bytes
)
2873 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2874 struct btrfs_free_space
*first
= NULL
;
2875 struct btrfs_free_space
*entry
= NULL
;
2876 struct btrfs_free_space
*last
;
2877 struct rb_node
*node
;
2882 entry
= tree_search_offset(ctl
, offset
, 0, 1);
2887 * We don't want bitmaps, so just move along until we find a normal
2890 while (entry
->bitmap
|| entry
->bytes
< min_bytes
) {
2891 if (entry
->bitmap
&& list_empty(&entry
->list
))
2892 list_add_tail(&entry
->list
, bitmaps
);
2893 node
= rb_next(&entry
->offset_index
);
2896 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2899 window_free
= entry
->bytes
;
2900 max_extent
= entry
->bytes
;
2904 for (node
= rb_next(&entry
->offset_index
); node
;
2905 node
= rb_next(&entry
->offset_index
)) {
2906 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2908 if (entry
->bitmap
) {
2909 if (list_empty(&entry
->list
))
2910 list_add_tail(&entry
->list
, bitmaps
);
2914 if (entry
->bytes
< min_bytes
)
2918 window_free
+= entry
->bytes
;
2919 if (entry
->bytes
> max_extent
)
2920 max_extent
= entry
->bytes
;
2923 if (window_free
< bytes
|| max_extent
< cont1_bytes
)
2926 cluster
->window_start
= first
->offset
;
2928 node
= &first
->offset_index
;
2931 * now we've found our entries, pull them out of the free space
2932 * cache and put them into the cluster rbtree
2937 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2938 node
= rb_next(&entry
->offset_index
);
2939 if (entry
->bitmap
|| entry
->bytes
< min_bytes
)
2942 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2943 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2944 &entry
->offset_index
, 0);
2945 total_size
+= entry
->bytes
;
2946 ASSERT(!ret
); /* -EEXIST; Logic error */
2947 } while (node
&& entry
!= last
);
2949 cluster
->max_size
= max_extent
;
2950 trace_btrfs_setup_cluster(block_group
, cluster
, total_size
, 0);
2955 * This specifically looks for bitmaps that may work in the cluster, we assume
2956 * that we have already failed to find extents that will work.
2959 setup_cluster_bitmap(struct btrfs_block_group_cache
*block_group
,
2960 struct btrfs_free_cluster
*cluster
,
2961 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2962 u64 cont1_bytes
, u64 min_bytes
)
2964 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2965 struct btrfs_free_space
*entry
= NULL
;
2967 u64 bitmap_offset
= offset_to_bitmap(ctl
, offset
);
2969 if (ctl
->total_bitmaps
== 0)
2973 * The bitmap that covers offset won't be in the list unless offset
2974 * is just its start offset.
2976 if (!list_empty(bitmaps
))
2977 entry
= list_first_entry(bitmaps
, struct btrfs_free_space
, list
);
2979 if (!entry
|| entry
->offset
!= bitmap_offset
) {
2980 entry
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2981 if (entry
&& list_empty(&entry
->list
))
2982 list_add(&entry
->list
, bitmaps
);
2985 list_for_each_entry(entry
, bitmaps
, list
) {
2986 if (entry
->bytes
< bytes
)
2988 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
, offset
,
2989 bytes
, cont1_bytes
, min_bytes
);
2995 * The bitmaps list has all the bitmaps that record free space
2996 * starting after offset, so no more search is required.
3002 * here we try to find a cluster of blocks in a block group. The goal
3003 * is to find at least bytes+empty_size.
3004 * We might not find them all in one contiguous area.
3006 * returns zero and sets up cluster if things worked out, otherwise
3007 * it returns -enospc
3009 int btrfs_find_space_cluster(struct btrfs_fs_info
*fs_info
,
3010 struct btrfs_block_group_cache
*block_group
,
3011 struct btrfs_free_cluster
*cluster
,
3012 u64 offset
, u64 bytes
, u64 empty_size
)
3014 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3015 struct btrfs_free_space
*entry
, *tmp
;
3022 * Choose the minimum extent size we'll require for this
3023 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3024 * For metadata, allow allocates with smaller extents. For
3025 * data, keep it dense.
3027 if (btrfs_test_opt(fs_info
, SSD_SPREAD
)) {
3028 cont1_bytes
= min_bytes
= bytes
+ empty_size
;
3029 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
3030 cont1_bytes
= bytes
;
3031 min_bytes
= fs_info
->sectorsize
;
3033 cont1_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
3034 min_bytes
= fs_info
->sectorsize
;
3037 spin_lock(&ctl
->tree_lock
);
3040 * If we know we don't have enough space to make a cluster don't even
3041 * bother doing all the work to try and find one.
3043 if (ctl
->free_space
< bytes
) {
3044 spin_unlock(&ctl
->tree_lock
);
3048 spin_lock(&cluster
->lock
);
3050 /* someone already found a cluster, hooray */
3051 if (cluster
->block_group
) {
3056 trace_btrfs_find_cluster(block_group
, offset
, bytes
, empty_size
,
3059 ret
= setup_cluster_no_bitmap(block_group
, cluster
, &bitmaps
, offset
,
3061 cont1_bytes
, min_bytes
);
3063 ret
= setup_cluster_bitmap(block_group
, cluster
, &bitmaps
,
3064 offset
, bytes
+ empty_size
,
3065 cont1_bytes
, min_bytes
);
3067 /* Clear our temporary list */
3068 list_for_each_entry_safe(entry
, tmp
, &bitmaps
, list
)
3069 list_del_init(&entry
->list
);
3072 atomic_inc(&block_group
->count
);
3073 list_add_tail(&cluster
->block_group_list
,
3074 &block_group
->cluster_list
);
3075 cluster
->block_group
= block_group
;
3077 trace_btrfs_failed_cluster_setup(block_group
);
3080 spin_unlock(&cluster
->lock
);
3081 spin_unlock(&ctl
->tree_lock
);
3087 * simple code to zero out a cluster
3089 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
3091 spin_lock_init(&cluster
->lock
);
3092 spin_lock_init(&cluster
->refill_lock
);
3093 cluster
->root
= RB_ROOT
;
3094 cluster
->max_size
= 0;
3095 cluster
->fragmented
= false;
3096 INIT_LIST_HEAD(&cluster
->block_group_list
);
3097 cluster
->block_group
= NULL
;
3100 static int do_trimming(struct btrfs_block_group_cache
*block_group
,
3101 u64
*total_trimmed
, u64 start
, u64 bytes
,
3102 u64 reserved_start
, u64 reserved_bytes
,
3103 struct btrfs_trim_range
*trim_entry
)
3105 struct btrfs_space_info
*space_info
= block_group
->space_info
;
3106 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
3107 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3112 spin_lock(&space_info
->lock
);
3113 spin_lock(&block_group
->lock
);
3114 if (!block_group
->ro
) {
3115 block_group
->reserved
+= reserved_bytes
;
3116 space_info
->bytes_reserved
+= reserved_bytes
;
3119 spin_unlock(&block_group
->lock
);
3120 spin_unlock(&space_info
->lock
);
3122 ret
= btrfs_discard_extent(fs_info
, start
, bytes
, &trimmed
);
3124 *total_trimmed
+= trimmed
;
3126 mutex_lock(&ctl
->cache_writeout_mutex
);
3127 btrfs_add_free_space(block_group
, reserved_start
, reserved_bytes
);
3128 list_del(&trim_entry
->list
);
3129 mutex_unlock(&ctl
->cache_writeout_mutex
);
3132 spin_lock(&space_info
->lock
);
3133 spin_lock(&block_group
->lock
);
3134 if (block_group
->ro
)
3135 space_info
->bytes_readonly
+= reserved_bytes
;
3136 block_group
->reserved
-= reserved_bytes
;
3137 space_info
->bytes_reserved
-= reserved_bytes
;
3138 spin_unlock(&space_info
->lock
);
3139 spin_unlock(&block_group
->lock
);
3145 static int trim_no_bitmap(struct btrfs_block_group_cache
*block_group
,
3146 u64
*total_trimmed
, u64 start
, u64 end
, u64 minlen
)
3148 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3149 struct btrfs_free_space
*entry
;
3150 struct rb_node
*node
;
3156 while (start
< end
) {
3157 struct btrfs_trim_range trim_entry
;
3159 mutex_lock(&ctl
->cache_writeout_mutex
);
3160 spin_lock(&ctl
->tree_lock
);
3162 if (ctl
->free_space
< minlen
) {
3163 spin_unlock(&ctl
->tree_lock
);
3164 mutex_unlock(&ctl
->cache_writeout_mutex
);
3168 entry
= tree_search_offset(ctl
, start
, 0, 1);
3170 spin_unlock(&ctl
->tree_lock
);
3171 mutex_unlock(&ctl
->cache_writeout_mutex
);
3176 while (entry
->bitmap
) {
3177 node
= rb_next(&entry
->offset_index
);
3179 spin_unlock(&ctl
->tree_lock
);
3180 mutex_unlock(&ctl
->cache_writeout_mutex
);
3183 entry
= rb_entry(node
, struct btrfs_free_space
,
3187 if (entry
->offset
>= end
) {
3188 spin_unlock(&ctl
->tree_lock
);
3189 mutex_unlock(&ctl
->cache_writeout_mutex
);
3193 extent_start
= entry
->offset
;
3194 extent_bytes
= entry
->bytes
;
3195 start
= max(start
, extent_start
);
3196 bytes
= min(extent_start
+ extent_bytes
, end
) - start
;
3197 if (bytes
< minlen
) {
3198 spin_unlock(&ctl
->tree_lock
);
3199 mutex_unlock(&ctl
->cache_writeout_mutex
);
3203 unlink_free_space(ctl
, entry
);
3204 kmem_cache_free(btrfs_free_space_cachep
, entry
);
3206 spin_unlock(&ctl
->tree_lock
);
3207 trim_entry
.start
= extent_start
;
3208 trim_entry
.bytes
= extent_bytes
;
3209 list_add_tail(&trim_entry
.list
, &ctl
->trimming_ranges
);
3210 mutex_unlock(&ctl
->cache_writeout_mutex
);
3212 ret
= do_trimming(block_group
, total_trimmed
, start
, bytes
,
3213 extent_start
, extent_bytes
, &trim_entry
);
3219 if (fatal_signal_pending(current
)) {
3230 static int trim_bitmaps(struct btrfs_block_group_cache
*block_group
,
3231 u64
*total_trimmed
, u64 start
, u64 end
, u64 minlen
)
3233 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
3234 struct btrfs_free_space
*entry
;
3238 u64 offset
= offset_to_bitmap(ctl
, start
);
3240 while (offset
< end
) {
3241 bool next_bitmap
= false;
3242 struct btrfs_trim_range trim_entry
;
3244 mutex_lock(&ctl
->cache_writeout_mutex
);
3245 spin_lock(&ctl
->tree_lock
);
3247 if (ctl
->free_space
< minlen
) {
3248 spin_unlock(&ctl
->tree_lock
);
3249 mutex_unlock(&ctl
->cache_writeout_mutex
);
3253 entry
= tree_search_offset(ctl
, offset
, 1, 0);
3255 spin_unlock(&ctl
->tree_lock
);
3256 mutex_unlock(&ctl
->cache_writeout_mutex
);
3262 ret2
= search_bitmap(ctl
, entry
, &start
, &bytes
, false);
3263 if (ret2
|| start
>= end
) {
3264 spin_unlock(&ctl
->tree_lock
);
3265 mutex_unlock(&ctl
->cache_writeout_mutex
);
3270 bytes
= min(bytes
, end
- start
);
3271 if (bytes
< minlen
) {
3272 spin_unlock(&ctl
->tree_lock
);
3273 mutex_unlock(&ctl
->cache_writeout_mutex
);
3277 bitmap_clear_bits(ctl
, entry
, start
, bytes
);
3278 if (entry
->bytes
== 0)
3279 free_bitmap(ctl
, entry
);
3281 spin_unlock(&ctl
->tree_lock
);
3282 trim_entry
.start
= start
;
3283 trim_entry
.bytes
= bytes
;
3284 list_add_tail(&trim_entry
.list
, &ctl
->trimming_ranges
);
3285 mutex_unlock(&ctl
->cache_writeout_mutex
);
3287 ret
= do_trimming(block_group
, total_trimmed
, start
, bytes
,
3288 start
, bytes
, &trim_entry
);
3293 offset
+= BITS_PER_BITMAP
* ctl
->unit
;
3296 if (start
>= offset
+ BITS_PER_BITMAP
* ctl
->unit
)
3297 offset
+= BITS_PER_BITMAP
* ctl
->unit
;
3300 if (fatal_signal_pending(current
)) {
3311 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache
*cache
)
3313 atomic_inc(&cache
->trimming
);
3316 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache
*block_group
)
3318 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
3319 struct extent_map_tree
*em_tree
;
3320 struct extent_map
*em
;
3323 spin_lock(&block_group
->lock
);
3324 cleanup
= (atomic_dec_and_test(&block_group
->trimming
) &&
3325 block_group
->removed
);
3326 spin_unlock(&block_group
->lock
);
3329 mutex_lock(&fs_info
->chunk_mutex
);
3330 em_tree
= &fs_info
->mapping_tree
.map_tree
;
3331 write_lock(&em_tree
->lock
);
3332 em
= lookup_extent_mapping(em_tree
, block_group
->key
.objectid
,
3334 BUG_ON(!em
); /* logic error, can't happen */
3336 * remove_extent_mapping() will delete us from the pinned_chunks
3337 * list, which is protected by the chunk mutex.
3339 remove_extent_mapping(em_tree
, em
);
3340 write_unlock(&em_tree
->lock
);
3341 mutex_unlock(&fs_info
->chunk_mutex
);
3343 /* once for us and once for the tree */
3344 free_extent_map(em
);
3345 free_extent_map(em
);
3348 * We've left one free space entry and other tasks trimming
3349 * this block group have left 1 entry each one. Free them.
3351 __btrfs_remove_free_space_cache(block_group
->free_space_ctl
);
3355 int btrfs_trim_block_group(struct btrfs_block_group_cache
*block_group
,
3356 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
)
3362 spin_lock(&block_group
->lock
);
3363 if (block_group
->removed
) {
3364 spin_unlock(&block_group
->lock
);
3367 btrfs_get_block_group_trimming(block_group
);
3368 spin_unlock(&block_group
->lock
);
3370 ret
= trim_no_bitmap(block_group
, trimmed
, start
, end
, minlen
);
3374 ret
= trim_bitmaps(block_group
, trimmed
, start
, end
, minlen
);
3376 btrfs_put_block_group_trimming(block_group
);
3381 * Find the left-most item in the cache tree, and then return the
3382 * smallest inode number in the item.
3384 * Note: the returned inode number may not be the smallest one in
3385 * the tree, if the left-most item is a bitmap.
3387 u64
btrfs_find_ino_for_alloc(struct btrfs_root
*fs_root
)
3389 struct btrfs_free_space_ctl
*ctl
= fs_root
->free_ino_ctl
;
3390 struct btrfs_free_space
*entry
= NULL
;
3393 spin_lock(&ctl
->tree_lock
);
3395 if (RB_EMPTY_ROOT(&ctl
->free_space_offset
))
3398 entry
= rb_entry(rb_first(&ctl
->free_space_offset
),
3399 struct btrfs_free_space
, offset_index
);
3401 if (!entry
->bitmap
) {
3402 ino
= entry
->offset
;
3404 unlink_free_space(ctl
, entry
);
3408 kmem_cache_free(btrfs_free_space_cachep
, entry
);
3410 link_free_space(ctl
, entry
);
3416 ret
= search_bitmap(ctl
, entry
, &offset
, &count
, true);
3417 /* Logic error; Should be empty if it can't find anything */
3421 bitmap_clear_bits(ctl
, entry
, offset
, 1);
3422 if (entry
->bytes
== 0)
3423 free_bitmap(ctl
, entry
);
3426 spin_unlock(&ctl
->tree_lock
);
3431 struct inode
*lookup_free_ino_inode(struct btrfs_root
*root
,
3432 struct btrfs_path
*path
)
3434 struct inode
*inode
= NULL
;
3436 spin_lock(&root
->ino_cache_lock
);
3437 if (root
->ino_cache_inode
)
3438 inode
= igrab(root
->ino_cache_inode
);
3439 spin_unlock(&root
->ino_cache_lock
);
3443 inode
= __lookup_free_space_inode(root
, path
, 0);
3447 spin_lock(&root
->ino_cache_lock
);
3448 if (!btrfs_fs_closing(root
->fs_info
))
3449 root
->ino_cache_inode
= igrab(inode
);
3450 spin_unlock(&root
->ino_cache_lock
);
3455 int create_free_ino_inode(struct btrfs_root
*root
,
3456 struct btrfs_trans_handle
*trans
,
3457 struct btrfs_path
*path
)
3459 return __create_free_space_inode(root
, trans
, path
,
3460 BTRFS_FREE_INO_OBJECTID
, 0);
3463 int load_free_ino_cache(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
3465 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
3466 struct btrfs_path
*path
;
3467 struct inode
*inode
;
3469 u64 root_gen
= btrfs_root_generation(&root
->root_item
);
3471 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
3475 * If we're unmounting then just return, since this does a search on the
3476 * normal root and not the commit root and we could deadlock.
3478 if (btrfs_fs_closing(fs_info
))
3481 path
= btrfs_alloc_path();
3485 inode
= lookup_free_ino_inode(root
, path
);
3489 if (root_gen
!= BTRFS_I(inode
)->generation
)
3492 ret
= __load_free_space_cache(root
, inode
, ctl
, path
, 0);
3496 "failed to load free ino cache for root %llu",
3497 root
->root_key
.objectid
);
3501 btrfs_free_path(path
);
3505 int btrfs_write_out_ino_cache(struct btrfs_root
*root
,
3506 struct btrfs_trans_handle
*trans
,
3507 struct btrfs_path
*path
,
3508 struct inode
*inode
)
3510 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3511 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
3513 struct btrfs_io_ctl io_ctl
;
3514 bool release_metadata
= true;
3516 if (!btrfs_test_opt(fs_info
, INODE_MAP_CACHE
))
3519 memset(&io_ctl
, 0, sizeof(io_ctl
));
3520 ret
= __btrfs_write_out_cache(root
, inode
, ctl
, NULL
, &io_ctl
, trans
);
3523 * At this point writepages() didn't error out, so our metadata
3524 * reservation is released when the writeback finishes, at
3525 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3526 * with or without an error.
3528 release_metadata
= false;
3529 ret
= btrfs_wait_cache_io_root(root
, trans
, &io_ctl
, path
);
3533 if (release_metadata
)
3534 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
3535 inode
->i_size
, true);
3538 "failed to write free ino cache for root %llu",
3539 root
->root_key
.objectid
);
3546 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3548 * Use this if you need to make a bitmap or extent entry specifically, it
3549 * doesn't do any of the merging that add_free_space does, this acts a lot like
3550 * how the free space cache loading stuff works, so you can get really weird
3553 int test_add_free_space_entry(struct btrfs_block_group_cache
*cache
,
3554 u64 offset
, u64 bytes
, bool bitmap
)
3556 struct btrfs_free_space_ctl
*ctl
= cache
->free_space_ctl
;
3557 struct btrfs_free_space
*info
= NULL
, *bitmap_info
;
3564 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
3570 spin_lock(&ctl
->tree_lock
);
3571 info
->offset
= offset
;
3572 info
->bytes
= bytes
;
3573 info
->max_extent_size
= 0;
3574 ret
= link_free_space(ctl
, info
);
3575 spin_unlock(&ctl
->tree_lock
);
3577 kmem_cache_free(btrfs_free_space_cachep
, info
);
3582 map
= kzalloc(PAGE_SIZE
, GFP_NOFS
);
3584 kmem_cache_free(btrfs_free_space_cachep
, info
);
3589 spin_lock(&ctl
->tree_lock
);
3590 bitmap_info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
3595 add_new_bitmap(ctl
, info
, offset
);
3600 bytes_added
= add_bytes_to_bitmap(ctl
, bitmap_info
, offset
, bytes
);
3602 bytes
-= bytes_added
;
3603 offset
+= bytes_added
;
3604 spin_unlock(&ctl
->tree_lock
);
3610 kmem_cache_free(btrfs_free_space_cachep
, info
);
3617 * Checks to see if the given range is in the free space cache. This is really
3618 * just used to check the absence of space, so if there is free space in the
3619 * range at all we will return 1.
3621 int test_check_exists(struct btrfs_block_group_cache
*cache
,
3622 u64 offset
, u64 bytes
)
3624 struct btrfs_free_space_ctl
*ctl
= cache
->free_space_ctl
;
3625 struct btrfs_free_space
*info
;
3628 spin_lock(&ctl
->tree_lock
);
3629 info
= tree_search_offset(ctl
, offset
, 0, 0);
3631 info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
3639 u64 bit_off
, bit_bytes
;
3641 struct btrfs_free_space
*tmp
;
3644 bit_bytes
= ctl
->unit
;
3645 ret
= search_bitmap(ctl
, info
, &bit_off
, &bit_bytes
, false);
3647 if (bit_off
== offset
) {
3650 } else if (bit_off
> offset
&&
3651 offset
+ bytes
> bit_off
) {
3657 n
= rb_prev(&info
->offset_index
);
3659 tmp
= rb_entry(n
, struct btrfs_free_space
,
3661 if (tmp
->offset
+ tmp
->bytes
< offset
)
3663 if (offset
+ bytes
< tmp
->offset
) {
3664 n
= rb_prev(&tmp
->offset_index
);
3671 n
= rb_next(&info
->offset_index
);
3673 tmp
= rb_entry(n
, struct btrfs_free_space
,
3675 if (offset
+ bytes
< tmp
->offset
)
3677 if (tmp
->offset
+ tmp
->bytes
< offset
) {
3678 n
= rb_next(&tmp
->offset_index
);
3689 if (info
->offset
== offset
) {
3694 if (offset
> info
->offset
&& offset
< info
->offset
+ info
->bytes
)
3697 spin_unlock(&ctl
->tree_lock
);
3700 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */