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 and generation into the first page */
304 if (write && check_crcs &&
305 (num_pages * sizeof(u32) + sizeof(u64)) > 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 copy_page(io_ctl->cur, bitmap);
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 copy_page(entry->bitmap, io_ctl->cur);
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 = root->fs_info;
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;
1130 /* Flush the dirty pages in the cache file. */
1131 ret = flush_dirty_cache(inode);
1135 /* Update the cache item to tell everyone this cache file is valid. */
1136 ret = update_cache_item(trans, root, inode, path, offset,
1137 io_ctl->entries, io_ctl->bitmaps);
1139 io_ctl_free(io_ctl);
1141 invalidate_inode_pages2(inode->i_mapping);
1142 BTRFS_I(inode)->generation = 0;
1145 btrfs_err(root->fs_info,
1146 "failed to write free space cache for block group %llu",
1147 block_group->key.objectid);
1151 btrfs_update_inode(trans, root, inode);
1154 /* the dirty list is protected by the dirty_bgs_lock */
1155 spin_lock(&trans->transaction->dirty_bgs_lock);
1157 /* the disk_cache_state is protected by the block group lock */
1158 spin_lock(&block_group->lock);
1161 * only mark this as written if we didn't get put back on
1162 * the dirty list while waiting for IO. Otherwise our
1163 * cache state won't be right, and we won't get written again
1165 if (!ret && list_empty(&block_group->dirty_list))
1166 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1168 block_group->disk_cache_state = BTRFS_DC_ERROR;
1170 spin_unlock(&block_group->lock);
1171 spin_unlock(&trans->transaction->dirty_bgs_lock);
1172 io_ctl->inode = NULL;
1180 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1181 struct btrfs_trans_handle *trans,
1182 struct btrfs_io_ctl *io_ctl,
1183 struct btrfs_path *path)
1185 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1188 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1189 struct btrfs_block_group_cache *block_group,
1190 struct btrfs_path *path)
1192 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1193 block_group, &block_group->io_ctl,
1194 path, block_group->key.objectid);
1198 * __btrfs_write_out_cache - write out cached info to an inode
1199 * @root - the root the inode belongs to
1200 * @ctl - the free space cache we are going to write out
1201 * @block_group - the block_group for this cache if it belongs to a block_group
1202 * @trans - the trans handle
1204 * This function writes out a free space cache struct to disk for quick recovery
1205 * on mount. This will return 0 if it was successful in writing the cache out,
1206 * or an errno if it was not.
1208 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1209 struct btrfs_free_space_ctl *ctl,
1210 struct btrfs_block_group_cache *block_group,
1211 struct btrfs_io_ctl *io_ctl,
1212 struct btrfs_trans_handle *trans)
1214 struct btrfs_fs_info *fs_info = root->fs_info;
1215 struct extent_state *cached_state = NULL;
1216 LIST_HEAD(bitmap_list);
1222 if (!i_size_read(inode))
1225 WARN_ON(io_ctl->pages);
1226 ret = io_ctl_init(io_ctl, inode, 1);
1230 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1231 down_write(&block_group->data_rwsem);
1232 spin_lock(&block_group->lock);
1233 if (block_group->delalloc_bytes) {
1234 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1235 spin_unlock(&block_group->lock);
1236 up_write(&block_group->data_rwsem);
1237 BTRFS_I(inode)->generation = 0;
1242 spin_unlock(&block_group->lock);
1245 /* Lock all pages first so we can lock the extent safely. */
1246 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1250 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1253 io_ctl_set_generation(io_ctl, trans->transid);
1255 mutex_lock(&ctl->cache_writeout_mutex);
1256 /* Write out the extent entries in the free space cache */
1257 spin_lock(&ctl->tree_lock);
1258 ret = write_cache_extent_entries(io_ctl, ctl,
1259 block_group, &entries, &bitmaps,
1262 goto out_nospc_locked;
1265 * Some spaces that are freed in the current transaction are pinned,
1266 * they will be added into free space cache after the transaction is
1267 * committed, we shouldn't lose them.
1269 * If this changes while we are working we'll get added back to
1270 * the dirty list and redo it. No locking needed
1272 ret = write_pinned_extent_entries(fs_info, block_group,
1275 goto out_nospc_locked;
1278 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1279 * locked while doing it because a concurrent trim can be manipulating
1280 * or freeing the bitmap.
1282 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1283 spin_unlock(&ctl->tree_lock);
1284 mutex_unlock(&ctl->cache_writeout_mutex);
1288 /* Zero out the rest of the pages just to make sure */
1289 io_ctl_zero_remaining_pages(io_ctl);
1291 /* Everything is written out, now we dirty the pages in the file. */
1292 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1293 i_size_read(inode), &cached_state);
1297 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1298 up_write(&block_group->data_rwsem);
1300 * Release the pages and unlock the extent, we will flush
1303 io_ctl_drop_pages(io_ctl);
1305 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1306 i_size_read(inode) - 1, &cached_state);
1309 * at this point the pages are under IO and we're happy,
1310 * The caller is responsible for waiting on them and updating the
1311 * the cache and the inode
1313 io_ctl->entries = entries;
1314 io_ctl->bitmaps = bitmaps;
1316 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1323 io_ctl->inode = NULL;
1324 io_ctl_free(io_ctl);
1326 invalidate_inode_pages2(inode->i_mapping);
1327 BTRFS_I(inode)->generation = 0;
1329 btrfs_update_inode(trans, root, inode);
1335 cleanup_bitmap_list(&bitmap_list);
1336 spin_unlock(&ctl->tree_lock);
1337 mutex_unlock(&ctl->cache_writeout_mutex);
1340 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1343 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1344 up_write(&block_group->data_rwsem);
1349 int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
1350 struct btrfs_trans_handle *trans,
1351 struct btrfs_block_group_cache *block_group,
1352 struct btrfs_path *path)
1354 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1355 struct inode *inode;
1358 spin_lock(&block_group->lock);
1359 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1360 spin_unlock(&block_group->lock);
1363 spin_unlock(&block_group->lock);
1365 inode = lookup_free_space_inode(fs_info, block_group, path);
1369 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1370 block_group, &block_group->io_ctl, trans);
1374 "failed to write free space cache for block group %llu",
1375 block_group->key.objectid);
1377 spin_lock(&block_group->lock);
1378 block_group->disk_cache_state = BTRFS_DC_ERROR;
1379 spin_unlock(&block_group->lock);
1381 block_group->io_ctl.inode = NULL;
1386 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1387 * to wait for IO and put the inode
1393 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1396 ASSERT(offset >= bitmap_start);
1397 offset -= bitmap_start;
1398 return (unsigned long)(div_u64(offset, unit));
1401 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1403 return (unsigned long)(div_u64(bytes, unit));
1406 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1410 u64 bytes_per_bitmap;
1412 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1413 bitmap_start = offset - ctl->start;
1414 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1415 bitmap_start *= bytes_per_bitmap;
1416 bitmap_start += ctl->start;
1418 return bitmap_start;
1421 static int tree_insert_offset(struct rb_root *root, u64 offset,
1422 struct rb_node *node, int bitmap)
1424 struct rb_node **p = &root->rb_node;
1425 struct rb_node *parent = NULL;
1426 struct btrfs_free_space *info;
1430 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1432 if (offset < info->offset) {
1434 } else if (offset > info->offset) {
1435 p = &(*p)->rb_right;
1438 * we could have a bitmap entry and an extent entry
1439 * share the same offset. If this is the case, we want
1440 * the extent entry to always be found first if we do a
1441 * linear search through the tree, since we want to have
1442 * the quickest allocation time, and allocating from an
1443 * extent is faster than allocating from a bitmap. So
1444 * if we're inserting a bitmap and we find an entry at
1445 * this offset, we want to go right, or after this entry
1446 * logically. If we are inserting an extent and we've
1447 * found a bitmap, we want to go left, or before
1455 p = &(*p)->rb_right;
1457 if (!info->bitmap) {
1466 rb_link_node(node, parent, p);
1467 rb_insert_color(node, root);
1473 * searches the tree for the given offset.
1475 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1476 * want a section that has at least bytes size and comes at or after the given
1479 static struct btrfs_free_space *
1480 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1481 u64 offset, int bitmap_only, int fuzzy)
1483 struct rb_node *n = ctl->free_space_offset.rb_node;
1484 struct btrfs_free_space *entry, *prev = NULL;
1486 /* find entry that is closest to the 'offset' */
1493 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1496 if (offset < entry->offset)
1498 else if (offset > entry->offset)
1511 * bitmap entry and extent entry may share same offset,
1512 * in that case, bitmap entry comes after extent entry.
1517 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1518 if (entry->offset != offset)
1521 WARN_ON(!entry->bitmap);
1524 if (entry->bitmap) {
1526 * if previous extent entry covers the offset,
1527 * we should return it instead of the bitmap entry
1529 n = rb_prev(&entry->offset_index);
1531 prev = rb_entry(n, struct btrfs_free_space,
1533 if (!prev->bitmap &&
1534 prev->offset + prev->bytes > offset)
1544 /* find last entry before the 'offset' */
1546 if (entry->offset > offset) {
1547 n = rb_prev(&entry->offset_index);
1549 entry = rb_entry(n, struct btrfs_free_space,
1551 ASSERT(entry->offset <= offset);
1560 if (entry->bitmap) {
1561 n = rb_prev(&entry->offset_index);
1563 prev = rb_entry(n, struct btrfs_free_space,
1565 if (!prev->bitmap &&
1566 prev->offset + prev->bytes > offset)
1569 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1571 } else if (entry->offset + entry->bytes > offset)
1578 if (entry->bitmap) {
1579 if (entry->offset + BITS_PER_BITMAP *
1583 if (entry->offset + entry->bytes > offset)
1587 n = rb_next(&entry->offset_index);
1590 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1596 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1597 struct btrfs_free_space *info)
1599 rb_erase(&info->offset_index, &ctl->free_space_offset);
1600 ctl->free_extents--;
1603 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1604 struct btrfs_free_space *info)
1606 __unlink_free_space(ctl, info);
1607 ctl->free_space -= info->bytes;
1610 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1611 struct btrfs_free_space *info)
1615 ASSERT(info->bytes || info->bitmap);
1616 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1617 &info->offset_index, (info->bitmap != NULL));
1621 ctl->free_space += info->bytes;
1622 ctl->free_extents++;
1626 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1628 struct btrfs_block_group_cache *block_group = ctl->private;
1632 u64 size = block_group->key.offset;
1633 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1634 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1636 max_bitmaps = max_t(u64, max_bitmaps, 1);
1638 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1641 * The goal is to keep the total amount of memory used per 1gb of space
1642 * at or below 32k, so we need to adjust how much memory we allow to be
1643 * used by extent based free space tracking
1646 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1648 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1651 * we want to account for 1 more bitmap than what we have so we can make
1652 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1653 * we add more bitmaps.
1655 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
1657 if (bitmap_bytes >= max_bytes) {
1658 ctl->extents_thresh = 0;
1663 * we want the extent entry threshold to always be at most 1/2 the max
1664 * bytes we can have, or whatever is less than that.
1666 extent_bytes = max_bytes - bitmap_bytes;
1667 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1669 ctl->extents_thresh =
1670 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1673 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1674 struct btrfs_free_space *info,
1675 u64 offset, u64 bytes)
1677 unsigned long start, count;
1679 start = offset_to_bit(info->offset, ctl->unit, offset);
1680 count = bytes_to_bits(bytes, ctl->unit);
1681 ASSERT(start + count <= BITS_PER_BITMAP);
1683 bitmap_clear(info->bitmap, start, count);
1685 info->bytes -= bytes;
1688 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1689 struct btrfs_free_space *info, u64 offset,
1692 __bitmap_clear_bits(ctl, info, offset, bytes);
1693 ctl->free_space -= bytes;
1696 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1697 struct btrfs_free_space *info, u64 offset,
1700 unsigned long start, count;
1702 start = offset_to_bit(info->offset, ctl->unit, offset);
1703 count = bytes_to_bits(bytes, ctl->unit);
1704 ASSERT(start + count <= BITS_PER_BITMAP);
1706 bitmap_set(info->bitmap, start, count);
1708 info->bytes += bytes;
1709 ctl->free_space += bytes;
1713 * If we can not find suitable extent, we will use bytes to record
1714 * the size of the max extent.
1716 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1717 struct btrfs_free_space *bitmap_info, u64 *offset,
1718 u64 *bytes, bool for_alloc)
1720 unsigned long found_bits = 0;
1721 unsigned long max_bits = 0;
1722 unsigned long bits, i;
1723 unsigned long next_zero;
1724 unsigned long extent_bits;
1727 * Skip searching the bitmap if we don't have a contiguous section that
1728 * is large enough for this allocation.
1731 bitmap_info->max_extent_size &&
1732 bitmap_info->max_extent_size < *bytes) {
1733 *bytes = bitmap_info->max_extent_size;
1737 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1738 max_t(u64, *offset, bitmap_info->offset));
1739 bits = bytes_to_bits(*bytes, ctl->unit);
1741 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1742 if (for_alloc && bits == 1) {
1746 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1747 BITS_PER_BITMAP, i);
1748 extent_bits = next_zero - i;
1749 if (extent_bits >= bits) {
1750 found_bits = extent_bits;
1752 } else if (extent_bits > max_bits) {
1753 max_bits = extent_bits;
1759 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1760 *bytes = (u64)(found_bits) * ctl->unit;
1764 *bytes = (u64)(max_bits) * ctl->unit;
1765 bitmap_info->max_extent_size = *bytes;
1769 /* Cache the size of the max extent in bytes */
1770 static struct btrfs_free_space *
1771 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1772 unsigned long align, u64 *max_extent_size)
1774 struct btrfs_free_space *entry;
1775 struct rb_node *node;
1780 if (!ctl->free_space_offset.rb_node)
1783 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1787 for (node = &entry->offset_index; node; node = rb_next(node)) {
1788 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1789 if (entry->bytes < *bytes) {
1790 if (entry->bytes > *max_extent_size)
1791 *max_extent_size = entry->bytes;
1795 /* make sure the space returned is big enough
1796 * to match our requested alignment
1798 if (*bytes >= align) {
1799 tmp = entry->offset - ctl->start + align - 1;
1800 tmp = div64_u64(tmp, align);
1801 tmp = tmp * align + ctl->start;
1802 align_off = tmp - entry->offset;
1805 tmp = entry->offset;
1808 if (entry->bytes < *bytes + align_off) {
1809 if (entry->bytes > *max_extent_size)
1810 *max_extent_size = entry->bytes;
1814 if (entry->bitmap) {
1817 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1822 } else if (size > *max_extent_size) {
1823 *max_extent_size = size;
1829 *bytes = entry->bytes - align_off;
1836 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1837 struct btrfs_free_space *info, u64 offset)
1839 info->offset = offset_to_bitmap(ctl, offset);
1841 INIT_LIST_HEAD(&info->list);
1842 link_free_space(ctl, info);
1843 ctl->total_bitmaps++;
1845 ctl->op->recalc_thresholds(ctl);
1848 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1849 struct btrfs_free_space *bitmap_info)
1851 unlink_free_space(ctl, bitmap_info);
1852 kfree(bitmap_info->bitmap);
1853 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1854 ctl->total_bitmaps--;
1855 ctl->op->recalc_thresholds(ctl);
1858 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1859 struct btrfs_free_space *bitmap_info,
1860 u64 *offset, u64 *bytes)
1863 u64 search_start, search_bytes;
1867 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1870 * We need to search for bits in this bitmap. We could only cover some
1871 * of the extent in this bitmap thanks to how we add space, so we need
1872 * to search for as much as it as we can and clear that amount, and then
1873 * go searching for the next bit.
1875 search_start = *offset;
1876 search_bytes = ctl->unit;
1877 search_bytes = min(search_bytes, end - search_start + 1);
1878 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1880 if (ret < 0 || search_start != *offset)
1883 /* We may have found more bits than what we need */
1884 search_bytes = min(search_bytes, *bytes);
1886 /* Cannot clear past the end of the bitmap */
1887 search_bytes = min(search_bytes, end - search_start + 1);
1889 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1890 *offset += search_bytes;
1891 *bytes -= search_bytes;
1894 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1895 if (!bitmap_info->bytes)
1896 free_bitmap(ctl, bitmap_info);
1899 * no entry after this bitmap, but we still have bytes to
1900 * remove, so something has gone wrong.
1905 bitmap_info = rb_entry(next, struct btrfs_free_space,
1909 * if the next entry isn't a bitmap we need to return to let the
1910 * extent stuff do its work.
1912 if (!bitmap_info->bitmap)
1916 * Ok the next item is a bitmap, but it may not actually hold
1917 * the information for the rest of this free space stuff, so
1918 * look for it, and if we don't find it return so we can try
1919 * everything over again.
1921 search_start = *offset;
1922 search_bytes = ctl->unit;
1923 ret = search_bitmap(ctl, bitmap_info, &search_start,
1924 &search_bytes, false);
1925 if (ret < 0 || search_start != *offset)
1929 } else if (!bitmap_info->bytes)
1930 free_bitmap(ctl, bitmap_info);
1935 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1936 struct btrfs_free_space *info, u64 offset,
1939 u64 bytes_to_set = 0;
1942 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1944 bytes_to_set = min(end - offset, bytes);
1946 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1949 * We set some bytes, we have no idea what the max extent size is
1952 info->max_extent_size = 0;
1954 return bytes_to_set;
1958 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1959 struct btrfs_free_space *info)
1961 struct btrfs_block_group_cache *block_group = ctl->private;
1962 struct btrfs_fs_info *fs_info = block_group->fs_info;
1963 bool forced = false;
1965 #ifdef CONFIG_BTRFS_DEBUG
1966 if (btrfs_should_fragment_free_space(block_group))
1971 * If we are below the extents threshold then we can add this as an
1972 * extent, and don't have to deal with the bitmap
1974 if (!forced && ctl->free_extents < ctl->extents_thresh) {
1976 * If this block group has some small extents we don't want to
1977 * use up all of our free slots in the cache with them, we want
1978 * to reserve them to larger extents, however if we have plenty
1979 * of cache left then go ahead an dadd them, no sense in adding
1980 * the overhead of a bitmap if we don't have to.
1982 if (info->bytes <= fs_info->sectorsize * 4) {
1983 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1991 * The original block groups from mkfs can be really small, like 8
1992 * megabytes, so don't bother with a bitmap for those entries. However
1993 * some block groups can be smaller than what a bitmap would cover but
1994 * are still large enough that they could overflow the 32k memory limit,
1995 * so allow those block groups to still be allowed to have a bitmap
1998 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
2004 static const struct btrfs_free_space_op free_space_op = {
2005 .recalc_thresholds = recalculate_thresholds,
2006 .use_bitmap = use_bitmap,
2009 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2010 struct btrfs_free_space *info)
2012 struct btrfs_free_space *bitmap_info;
2013 struct btrfs_block_group_cache *block_group = NULL;
2015 u64 bytes, offset, bytes_added;
2018 bytes = info->bytes;
2019 offset = info->offset;
2021 if (!ctl->op->use_bitmap(ctl, info))
2024 if (ctl->op == &free_space_op)
2025 block_group = ctl->private;
2028 * Since we link bitmaps right into the cluster we need to see if we
2029 * have a cluster here, and if so and it has our bitmap we need to add
2030 * the free space to that bitmap.
2032 if (block_group && !list_empty(&block_group->cluster_list)) {
2033 struct btrfs_free_cluster *cluster;
2034 struct rb_node *node;
2035 struct btrfs_free_space *entry;
2037 cluster = list_entry(block_group->cluster_list.next,
2038 struct btrfs_free_cluster,
2040 spin_lock(&cluster->lock);
2041 node = rb_first(&cluster->root);
2043 spin_unlock(&cluster->lock);
2044 goto no_cluster_bitmap;
2047 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2048 if (!entry->bitmap) {
2049 spin_unlock(&cluster->lock);
2050 goto no_cluster_bitmap;
2053 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2054 bytes_added = add_bytes_to_bitmap(ctl, entry,
2056 bytes -= bytes_added;
2057 offset += bytes_added;
2059 spin_unlock(&cluster->lock);
2067 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2074 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2075 bytes -= bytes_added;
2076 offset += bytes_added;
2086 if (info && info->bitmap) {
2087 add_new_bitmap(ctl, info, offset);
2092 spin_unlock(&ctl->tree_lock);
2094 /* no pre-allocated info, allocate a new one */
2096 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2099 spin_lock(&ctl->tree_lock);
2105 /* allocate the bitmap */
2106 info->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
2107 spin_lock(&ctl->tree_lock);
2108 if (!info->bitmap) {
2118 kfree(info->bitmap);
2119 kmem_cache_free(btrfs_free_space_cachep, info);
2125 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2126 struct btrfs_free_space *info, bool update_stat)
2128 struct btrfs_free_space *left_info;
2129 struct btrfs_free_space *right_info;
2130 bool merged = false;
2131 u64 offset = info->offset;
2132 u64 bytes = info->bytes;
2135 * first we want to see if there is free space adjacent to the range we
2136 * are adding, if there is remove that struct and add a new one to
2137 * cover the entire range
2139 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2140 if (right_info && rb_prev(&right_info->offset_index))
2141 left_info = rb_entry(rb_prev(&right_info->offset_index),
2142 struct btrfs_free_space, offset_index);
2144 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2146 if (right_info && !right_info->bitmap) {
2148 unlink_free_space(ctl, right_info);
2150 __unlink_free_space(ctl, right_info);
2151 info->bytes += right_info->bytes;
2152 kmem_cache_free(btrfs_free_space_cachep, right_info);
2156 if (left_info && !left_info->bitmap &&
2157 left_info->offset + left_info->bytes == offset) {
2159 unlink_free_space(ctl, left_info);
2161 __unlink_free_space(ctl, left_info);
2162 info->offset = left_info->offset;
2163 info->bytes += left_info->bytes;
2164 kmem_cache_free(btrfs_free_space_cachep, left_info);
2171 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2172 struct btrfs_free_space *info,
2175 struct btrfs_free_space *bitmap;
2178 const u64 end = info->offset + info->bytes;
2179 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2182 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2186 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2187 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2190 bytes = (j - i) * ctl->unit;
2191 info->bytes += bytes;
2194 bitmap_clear_bits(ctl, bitmap, end, bytes);
2196 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2199 free_bitmap(ctl, bitmap);
2204 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2205 struct btrfs_free_space *info,
2208 struct btrfs_free_space *bitmap;
2212 unsigned long prev_j;
2215 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2216 /* If we're on a boundary, try the previous logical bitmap. */
2217 if (bitmap_offset == info->offset) {
2218 if (info->offset == 0)
2220 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2223 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2227 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2229 prev_j = (unsigned long)-1;
2230 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2238 if (prev_j == (unsigned long)-1)
2239 bytes = (i + 1) * ctl->unit;
2241 bytes = (i - prev_j) * ctl->unit;
2243 info->offset -= bytes;
2244 info->bytes += bytes;
2247 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2249 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2252 free_bitmap(ctl, bitmap);
2258 * We prefer always to allocate from extent entries, both for clustered and
2259 * non-clustered allocation requests. So when attempting to add a new extent
2260 * entry, try to see if there's adjacent free space in bitmap entries, and if
2261 * there is, migrate that space from the bitmaps to the extent.
2262 * Like this we get better chances of satisfying space allocation requests
2263 * because we attempt to satisfy them based on a single cache entry, and never
2264 * on 2 or more entries - even if the entries represent a contiguous free space
2265 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2268 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2269 struct btrfs_free_space *info,
2273 * Only work with disconnected entries, as we can change their offset,
2274 * and must be extent entries.
2276 ASSERT(!info->bitmap);
2277 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2279 if (ctl->total_bitmaps > 0) {
2281 bool stole_front = false;
2283 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2284 if (ctl->total_bitmaps > 0)
2285 stole_front = steal_from_bitmap_to_front(ctl, info,
2288 if (stole_end || stole_front)
2289 try_merge_free_space(ctl, info, update_stat);
2293 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2294 struct btrfs_free_space_ctl *ctl,
2295 u64 offset, u64 bytes)
2297 struct btrfs_free_space *info;
2300 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2304 info->offset = offset;
2305 info->bytes = bytes;
2306 RB_CLEAR_NODE(&info->offset_index);
2308 spin_lock(&ctl->tree_lock);
2310 if (try_merge_free_space(ctl, info, true))
2314 * There was no extent directly to the left or right of this new
2315 * extent then we know we're going to have to allocate a new extent, so
2316 * before we do that see if we need to drop this into a bitmap
2318 ret = insert_into_bitmap(ctl, info);
2327 * Only steal free space from adjacent bitmaps if we're sure we're not
2328 * going to add the new free space to existing bitmap entries - because
2329 * that would mean unnecessary work that would be reverted. Therefore
2330 * attempt to steal space from bitmaps if we're adding an extent entry.
2332 steal_from_bitmap(ctl, info, true);
2334 ret = link_free_space(ctl, info);
2336 kmem_cache_free(btrfs_free_space_cachep, info);
2338 spin_unlock(&ctl->tree_lock);
2341 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2342 ASSERT(ret != -EEXIST);
2348 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2349 u64 offset, u64 bytes)
2351 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2352 struct btrfs_free_space *info;
2354 bool re_search = false;
2356 spin_lock(&ctl->tree_lock);
2363 info = tree_search_offset(ctl, offset, 0, 0);
2366 * oops didn't find an extent that matched the space we wanted
2367 * to remove, look for a bitmap instead
2369 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2373 * If we found a partial bit of our free space in a
2374 * bitmap but then couldn't find the other part this may
2375 * be a problem, so WARN about it.
2383 if (!info->bitmap) {
2384 unlink_free_space(ctl, info);
2385 if (offset == info->offset) {
2386 u64 to_free = min(bytes, info->bytes);
2388 info->bytes -= to_free;
2389 info->offset += to_free;
2391 ret = link_free_space(ctl, info);
2394 kmem_cache_free(btrfs_free_space_cachep, info);
2401 u64 old_end = info->bytes + info->offset;
2403 info->bytes = offset - info->offset;
2404 ret = link_free_space(ctl, info);
2409 /* Not enough bytes in this entry to satisfy us */
2410 if (old_end < offset + bytes) {
2411 bytes -= old_end - offset;
2414 } else if (old_end == offset + bytes) {
2418 spin_unlock(&ctl->tree_lock);
2420 ret = btrfs_add_free_space(block_group, offset + bytes,
2421 old_end - (offset + bytes));
2427 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2428 if (ret == -EAGAIN) {
2433 spin_unlock(&ctl->tree_lock);
2438 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2441 struct btrfs_fs_info *fs_info = block_group->fs_info;
2442 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2443 struct btrfs_free_space *info;
2447 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2448 info = rb_entry(n, struct btrfs_free_space, offset_index);
2449 if (info->bytes >= bytes && !block_group->ro)
2451 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2452 info->offset, info->bytes,
2453 (info->bitmap) ? "yes" : "no");
2455 btrfs_info(fs_info, "block group has cluster?: %s",
2456 list_empty(&block_group->cluster_list) ? "no" : "yes");
2458 "%d blocks of free space at or bigger than bytes is", count);
2461 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
2463 struct btrfs_fs_info *fs_info = block_group->fs_info;
2464 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2466 spin_lock_init(&ctl->tree_lock);
2467 ctl->unit = fs_info->sectorsize;
2468 ctl->start = block_group->key.objectid;
2469 ctl->private = block_group;
2470 ctl->op = &free_space_op;
2471 INIT_LIST_HEAD(&ctl->trimming_ranges);
2472 mutex_init(&ctl->cache_writeout_mutex);
2475 * we only want to have 32k of ram per block group for keeping
2476 * track of free space, and if we pass 1/2 of that we want to
2477 * start converting things over to using bitmaps
2479 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2483 * for a given cluster, put all of its extents back into the free
2484 * space cache. If the block group passed doesn't match the block group
2485 * pointed to by the cluster, someone else raced in and freed the
2486 * cluster already. In that case, we just return without changing anything
2489 __btrfs_return_cluster_to_free_space(
2490 struct btrfs_block_group_cache *block_group,
2491 struct btrfs_free_cluster *cluster)
2493 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2494 struct btrfs_free_space *entry;
2495 struct rb_node *node;
2497 spin_lock(&cluster->lock);
2498 if (cluster->block_group != block_group)
2501 cluster->block_group = NULL;
2502 cluster->window_start = 0;
2503 list_del_init(&cluster->block_group_list);
2505 node = rb_first(&cluster->root);
2509 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2510 node = rb_next(&entry->offset_index);
2511 rb_erase(&entry->offset_index, &cluster->root);
2512 RB_CLEAR_NODE(&entry->offset_index);
2514 bitmap = (entry->bitmap != NULL);
2516 try_merge_free_space(ctl, entry, false);
2517 steal_from_bitmap(ctl, entry, false);
2519 tree_insert_offset(&ctl->free_space_offset,
2520 entry->offset, &entry->offset_index, bitmap);
2522 cluster->root = RB_ROOT;
2525 spin_unlock(&cluster->lock);
2526 btrfs_put_block_group(block_group);
2530 static void __btrfs_remove_free_space_cache_locked(
2531 struct btrfs_free_space_ctl *ctl)
2533 struct btrfs_free_space *info;
2534 struct rb_node *node;
2536 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2537 info = rb_entry(node, struct btrfs_free_space, offset_index);
2538 if (!info->bitmap) {
2539 unlink_free_space(ctl, info);
2540 kmem_cache_free(btrfs_free_space_cachep, info);
2542 free_bitmap(ctl, info);
2545 cond_resched_lock(&ctl->tree_lock);
2549 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2551 spin_lock(&ctl->tree_lock);
2552 __btrfs_remove_free_space_cache_locked(ctl);
2553 spin_unlock(&ctl->tree_lock);
2556 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2558 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2559 struct btrfs_free_cluster *cluster;
2560 struct list_head *head;
2562 spin_lock(&ctl->tree_lock);
2563 while ((head = block_group->cluster_list.next) !=
2564 &block_group->cluster_list) {
2565 cluster = list_entry(head, struct btrfs_free_cluster,
2568 WARN_ON(cluster->block_group != block_group);
2569 __btrfs_return_cluster_to_free_space(block_group, cluster);
2571 cond_resched_lock(&ctl->tree_lock);
2573 __btrfs_remove_free_space_cache_locked(ctl);
2574 spin_unlock(&ctl->tree_lock);
2578 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2579 u64 offset, u64 bytes, u64 empty_size,
2580 u64 *max_extent_size)
2582 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2583 struct btrfs_free_space *entry = NULL;
2584 u64 bytes_search = bytes + empty_size;
2587 u64 align_gap_len = 0;
2589 spin_lock(&ctl->tree_lock);
2590 entry = find_free_space(ctl, &offset, &bytes_search,
2591 block_group->full_stripe_len, max_extent_size);
2596 if (entry->bitmap) {
2597 bitmap_clear_bits(ctl, entry, offset, bytes);
2599 free_bitmap(ctl, entry);
2601 unlink_free_space(ctl, entry);
2602 align_gap_len = offset - entry->offset;
2603 align_gap = entry->offset;
2605 entry->offset = offset + bytes;
2606 WARN_ON(entry->bytes < bytes + align_gap_len);
2608 entry->bytes -= bytes + align_gap_len;
2610 kmem_cache_free(btrfs_free_space_cachep, entry);
2612 link_free_space(ctl, entry);
2615 spin_unlock(&ctl->tree_lock);
2618 __btrfs_add_free_space(block_group->fs_info, ctl,
2619 align_gap, align_gap_len);
2624 * given a cluster, put all of its extents back into the free space
2625 * cache. If a block group is passed, this function will only free
2626 * a cluster that belongs to the passed block group.
2628 * Otherwise, it'll get a reference on the block group pointed to by the
2629 * cluster and remove the cluster from it.
2631 int btrfs_return_cluster_to_free_space(
2632 struct btrfs_block_group_cache *block_group,
2633 struct btrfs_free_cluster *cluster)
2635 struct btrfs_free_space_ctl *ctl;
2638 /* first, get a safe pointer to the block group */
2639 spin_lock(&cluster->lock);
2641 block_group = cluster->block_group;
2643 spin_unlock(&cluster->lock);
2646 } else if (cluster->block_group != block_group) {
2647 /* someone else has already freed it don't redo their work */
2648 spin_unlock(&cluster->lock);
2651 atomic_inc(&block_group->count);
2652 spin_unlock(&cluster->lock);
2654 ctl = block_group->free_space_ctl;
2656 /* now return any extents the cluster had on it */
2657 spin_lock(&ctl->tree_lock);
2658 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2659 spin_unlock(&ctl->tree_lock);
2661 /* finally drop our ref */
2662 btrfs_put_block_group(block_group);
2666 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2667 struct btrfs_free_cluster *cluster,
2668 struct btrfs_free_space *entry,
2669 u64 bytes, u64 min_start,
2670 u64 *max_extent_size)
2672 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2674 u64 search_start = cluster->window_start;
2675 u64 search_bytes = bytes;
2678 search_start = min_start;
2679 search_bytes = bytes;
2681 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2683 if (search_bytes > *max_extent_size)
2684 *max_extent_size = search_bytes;
2689 __bitmap_clear_bits(ctl, entry, ret, bytes);
2695 * given a cluster, try to allocate 'bytes' from it, returns 0
2696 * if it couldn't find anything suitably large, or a logical disk offset
2697 * if things worked out
2699 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2700 struct btrfs_free_cluster *cluster, u64 bytes,
2701 u64 min_start, u64 *max_extent_size)
2703 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2704 struct btrfs_free_space *entry = NULL;
2705 struct rb_node *node;
2708 spin_lock(&cluster->lock);
2709 if (bytes > cluster->max_size)
2712 if (cluster->block_group != block_group)
2715 node = rb_first(&cluster->root);
2719 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2721 if (entry->bytes < bytes && entry->bytes > *max_extent_size)
2722 *max_extent_size = entry->bytes;
2724 if (entry->bytes < bytes ||
2725 (!entry->bitmap && entry->offset < min_start)) {
2726 node = rb_next(&entry->offset_index);
2729 entry = rb_entry(node, struct btrfs_free_space,
2734 if (entry->bitmap) {
2735 ret = btrfs_alloc_from_bitmap(block_group,
2736 cluster, entry, bytes,
2737 cluster->window_start,
2740 node = rb_next(&entry->offset_index);
2743 entry = rb_entry(node, struct btrfs_free_space,
2747 cluster->window_start += bytes;
2749 ret = entry->offset;
2751 entry->offset += bytes;
2752 entry->bytes -= bytes;
2755 if (entry->bytes == 0)
2756 rb_erase(&entry->offset_index, &cluster->root);
2760 spin_unlock(&cluster->lock);
2765 spin_lock(&ctl->tree_lock);
2767 ctl->free_space -= bytes;
2768 if (entry->bytes == 0) {
2769 ctl->free_extents--;
2770 if (entry->bitmap) {
2771 kfree(entry->bitmap);
2772 ctl->total_bitmaps--;
2773 ctl->op->recalc_thresholds(ctl);
2775 kmem_cache_free(btrfs_free_space_cachep, entry);
2778 spin_unlock(&ctl->tree_lock);
2783 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2784 struct btrfs_free_space *entry,
2785 struct btrfs_free_cluster *cluster,
2786 u64 offset, u64 bytes,
2787 u64 cont1_bytes, u64 min_bytes)
2789 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2790 unsigned long next_zero;
2792 unsigned long want_bits;
2793 unsigned long min_bits;
2794 unsigned long found_bits;
2795 unsigned long max_bits = 0;
2796 unsigned long start = 0;
2797 unsigned long total_found = 0;
2800 i = offset_to_bit(entry->offset, ctl->unit,
2801 max_t(u64, offset, entry->offset));
2802 want_bits = bytes_to_bits(bytes, ctl->unit);
2803 min_bits = bytes_to_bits(min_bytes, ctl->unit);
2806 * Don't bother looking for a cluster in this bitmap if it's heavily
2809 if (entry->max_extent_size &&
2810 entry->max_extent_size < cont1_bytes)
2814 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
2815 next_zero = find_next_zero_bit(entry->bitmap,
2816 BITS_PER_BITMAP, i);
2817 if (next_zero - i >= min_bits) {
2818 found_bits = next_zero - i;
2819 if (found_bits > max_bits)
2820 max_bits = found_bits;
2823 if (next_zero - i > max_bits)
2824 max_bits = next_zero - i;
2829 entry->max_extent_size = (u64)max_bits * ctl->unit;
2835 cluster->max_size = 0;
2838 total_found += found_bits;
2840 if (cluster->max_size < found_bits * ctl->unit)
2841 cluster->max_size = found_bits * ctl->unit;
2843 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2848 cluster->window_start = start * ctl->unit + entry->offset;
2849 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2850 ret = tree_insert_offset(&cluster->root, entry->offset,
2851 &entry->offset_index, 1);
2852 ASSERT(!ret); /* -EEXIST; Logic error */
2854 trace_btrfs_setup_cluster(block_group, cluster,
2855 total_found * ctl->unit, 1);
2860 * This searches the block group for just extents to fill the cluster with.
2861 * Try to find a cluster with at least bytes total bytes, at least one
2862 * extent of cont1_bytes, and other clusters of at least min_bytes.
2865 setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2866 struct btrfs_free_cluster *cluster,
2867 struct list_head *bitmaps, u64 offset, u64 bytes,
2868 u64 cont1_bytes, u64 min_bytes)
2870 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2871 struct btrfs_free_space *first = NULL;
2872 struct btrfs_free_space *entry = NULL;
2873 struct btrfs_free_space *last;
2874 struct rb_node *node;
2879 entry = tree_search_offset(ctl, offset, 0, 1);
2884 * We don't want bitmaps, so just move along until we find a normal
2887 while (entry->bitmap || entry->bytes < min_bytes) {
2888 if (entry->bitmap && list_empty(&entry->list))
2889 list_add_tail(&entry->list, bitmaps);
2890 node = rb_next(&entry->offset_index);
2893 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2896 window_free = entry->bytes;
2897 max_extent = entry->bytes;
2901 for (node = rb_next(&entry->offset_index); node;
2902 node = rb_next(&entry->offset_index)) {
2903 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2905 if (entry->bitmap) {
2906 if (list_empty(&entry->list))
2907 list_add_tail(&entry->list, bitmaps);
2911 if (entry->bytes < min_bytes)
2915 window_free += entry->bytes;
2916 if (entry->bytes > max_extent)
2917 max_extent = entry->bytes;
2920 if (window_free < bytes || max_extent < cont1_bytes)
2923 cluster->window_start = first->offset;
2925 node = &first->offset_index;
2928 * now we've found our entries, pull them out of the free space
2929 * cache and put them into the cluster rbtree
2934 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2935 node = rb_next(&entry->offset_index);
2936 if (entry->bitmap || entry->bytes < min_bytes)
2939 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2940 ret = tree_insert_offset(&cluster->root, entry->offset,
2941 &entry->offset_index, 0);
2942 total_size += entry->bytes;
2943 ASSERT(!ret); /* -EEXIST; Logic error */
2944 } while (node && entry != last);
2946 cluster->max_size = max_extent;
2947 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
2952 * This specifically looks for bitmaps that may work in the cluster, we assume
2953 * that we have already failed to find extents that will work.
2956 setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2957 struct btrfs_free_cluster *cluster,
2958 struct list_head *bitmaps, u64 offset, u64 bytes,
2959 u64 cont1_bytes, u64 min_bytes)
2961 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2962 struct btrfs_free_space *entry = NULL;
2964 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
2966 if (ctl->total_bitmaps == 0)
2970 * The bitmap that covers offset won't be in the list unless offset
2971 * is just its start offset.
2973 if (!list_empty(bitmaps))
2974 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2976 if (!entry || entry->offset != bitmap_offset) {
2977 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2978 if (entry && list_empty(&entry->list))
2979 list_add(&entry->list, bitmaps);
2982 list_for_each_entry(entry, bitmaps, list) {
2983 if (entry->bytes < bytes)
2985 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2986 bytes, cont1_bytes, min_bytes);
2992 * The bitmaps list has all the bitmaps that record free space
2993 * starting after offset, so no more search is required.
2999 * here we try to find a cluster of blocks in a block group. The goal
3000 * is to find at least bytes+empty_size.
3001 * We might not find them all in one contiguous area.
3003 * returns zero and sets up cluster if things worked out, otherwise
3004 * it returns -enospc
3006 int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
3007 struct btrfs_block_group_cache *block_group,
3008 struct btrfs_free_cluster *cluster,
3009 u64 offset, u64 bytes, u64 empty_size)
3011 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3012 struct btrfs_free_space *entry, *tmp;
3019 * Choose the minimum extent size we'll require for this
3020 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3021 * For metadata, allow allocates with smaller extents. For
3022 * data, keep it dense.
3024 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3025 cont1_bytes = min_bytes = bytes + empty_size;
3026 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3027 cont1_bytes = bytes;
3028 min_bytes = fs_info->sectorsize;
3030 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3031 min_bytes = fs_info->sectorsize;
3034 spin_lock(&ctl->tree_lock);
3037 * If we know we don't have enough space to make a cluster don't even
3038 * bother doing all the work to try and find one.
3040 if (ctl->free_space < bytes) {
3041 spin_unlock(&ctl->tree_lock);
3045 spin_lock(&cluster->lock);
3047 /* someone already found a cluster, hooray */
3048 if (cluster->block_group) {
3053 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3056 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3058 cont1_bytes, min_bytes);
3060 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3061 offset, bytes + empty_size,
3062 cont1_bytes, min_bytes);
3064 /* Clear our temporary list */
3065 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3066 list_del_init(&entry->list);
3069 atomic_inc(&block_group->count);
3070 list_add_tail(&cluster->block_group_list,
3071 &block_group->cluster_list);
3072 cluster->block_group = block_group;
3074 trace_btrfs_failed_cluster_setup(block_group);
3077 spin_unlock(&cluster->lock);
3078 spin_unlock(&ctl->tree_lock);
3084 * simple code to zero out a cluster
3086 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3088 spin_lock_init(&cluster->lock);
3089 spin_lock_init(&cluster->refill_lock);
3090 cluster->root = RB_ROOT;
3091 cluster->max_size = 0;
3092 cluster->fragmented = false;
3093 INIT_LIST_HEAD(&cluster->block_group_list);
3094 cluster->block_group = NULL;
3097 static int do_trimming(struct btrfs_block_group_cache *block_group,
3098 u64 *total_trimmed, u64 start, u64 bytes,
3099 u64 reserved_start, u64 reserved_bytes,
3100 struct btrfs_trim_range *trim_entry)
3102 struct btrfs_space_info *space_info = block_group->space_info;
3103 struct btrfs_fs_info *fs_info = block_group->fs_info;
3104 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3109 spin_lock(&space_info->lock);
3110 spin_lock(&block_group->lock);
3111 if (!block_group->ro) {
3112 block_group->reserved += reserved_bytes;
3113 space_info->bytes_reserved += reserved_bytes;
3116 spin_unlock(&block_group->lock);
3117 spin_unlock(&space_info->lock);
3119 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3121 *total_trimmed += trimmed;
3123 mutex_lock(&ctl->cache_writeout_mutex);
3124 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
3125 list_del(&trim_entry->list);
3126 mutex_unlock(&ctl->cache_writeout_mutex);
3129 spin_lock(&space_info->lock);
3130 spin_lock(&block_group->lock);
3131 if (block_group->ro)
3132 space_info->bytes_readonly += reserved_bytes;
3133 block_group->reserved -= reserved_bytes;
3134 space_info->bytes_reserved -= reserved_bytes;
3135 spin_unlock(&space_info->lock);
3136 spin_unlock(&block_group->lock);
3142 static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3143 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3145 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3146 struct btrfs_free_space *entry;
3147 struct rb_node *node;
3153 while (start < end) {
3154 struct btrfs_trim_range trim_entry;
3156 mutex_lock(&ctl->cache_writeout_mutex);
3157 spin_lock(&ctl->tree_lock);
3159 if (ctl->free_space < minlen) {
3160 spin_unlock(&ctl->tree_lock);
3161 mutex_unlock(&ctl->cache_writeout_mutex);
3165 entry = tree_search_offset(ctl, start, 0, 1);
3167 spin_unlock(&ctl->tree_lock);
3168 mutex_unlock(&ctl->cache_writeout_mutex);
3173 while (entry->bitmap) {
3174 node = rb_next(&entry->offset_index);
3176 spin_unlock(&ctl->tree_lock);
3177 mutex_unlock(&ctl->cache_writeout_mutex);
3180 entry = rb_entry(node, struct btrfs_free_space,
3184 if (entry->offset >= end) {
3185 spin_unlock(&ctl->tree_lock);
3186 mutex_unlock(&ctl->cache_writeout_mutex);
3190 extent_start = entry->offset;
3191 extent_bytes = entry->bytes;
3192 start = max(start, extent_start);
3193 bytes = min(extent_start + extent_bytes, end) - start;
3194 if (bytes < minlen) {
3195 spin_unlock(&ctl->tree_lock);
3196 mutex_unlock(&ctl->cache_writeout_mutex);
3200 unlink_free_space(ctl, entry);
3201 kmem_cache_free(btrfs_free_space_cachep, entry);
3203 spin_unlock(&ctl->tree_lock);
3204 trim_entry.start = extent_start;
3205 trim_entry.bytes = extent_bytes;
3206 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3207 mutex_unlock(&ctl->cache_writeout_mutex);
3209 ret = do_trimming(block_group, total_trimmed, start, bytes,
3210 extent_start, extent_bytes, &trim_entry);
3216 if (fatal_signal_pending(current)) {
3227 static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3228 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3230 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3231 struct btrfs_free_space *entry;
3235 u64 offset = offset_to_bitmap(ctl, start);
3237 while (offset < end) {
3238 bool next_bitmap = false;
3239 struct btrfs_trim_range trim_entry;
3241 mutex_lock(&ctl->cache_writeout_mutex);
3242 spin_lock(&ctl->tree_lock);
3244 if (ctl->free_space < minlen) {
3245 spin_unlock(&ctl->tree_lock);
3246 mutex_unlock(&ctl->cache_writeout_mutex);
3250 entry = tree_search_offset(ctl, offset, 1, 0);
3252 spin_unlock(&ctl->tree_lock);
3253 mutex_unlock(&ctl->cache_writeout_mutex);
3259 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3260 if (ret2 || start >= end) {
3261 spin_unlock(&ctl->tree_lock);
3262 mutex_unlock(&ctl->cache_writeout_mutex);
3267 bytes = min(bytes, end - start);
3268 if (bytes < minlen) {
3269 spin_unlock(&ctl->tree_lock);
3270 mutex_unlock(&ctl->cache_writeout_mutex);
3274 bitmap_clear_bits(ctl, entry, start, bytes);
3275 if (entry->bytes == 0)
3276 free_bitmap(ctl, entry);
3278 spin_unlock(&ctl->tree_lock);
3279 trim_entry.start = start;
3280 trim_entry.bytes = bytes;
3281 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3282 mutex_unlock(&ctl->cache_writeout_mutex);
3284 ret = do_trimming(block_group, total_trimmed, start, bytes,
3285 start, bytes, &trim_entry);
3290 offset += BITS_PER_BITMAP * ctl->unit;
3293 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3294 offset += BITS_PER_BITMAP * ctl->unit;
3297 if (fatal_signal_pending(current)) {
3308 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
3310 atomic_inc(&cache->trimming);
3313 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3315 struct btrfs_fs_info *fs_info = block_group->fs_info;
3316 struct extent_map_tree *em_tree;
3317 struct extent_map *em;
3320 spin_lock(&block_group->lock);
3321 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3322 block_group->removed);
3323 spin_unlock(&block_group->lock);
3326 mutex_lock(&fs_info->chunk_mutex);
3327 em_tree = &fs_info->mapping_tree.map_tree;
3328 write_lock(&em_tree->lock);
3329 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3331 BUG_ON(!em); /* logic error, can't happen */
3333 * remove_extent_mapping() will delete us from the pinned_chunks
3334 * list, which is protected by the chunk mutex.
3336 remove_extent_mapping(em_tree, em);
3337 write_unlock(&em_tree->lock);
3338 mutex_unlock(&fs_info->chunk_mutex);
3340 /* once for us and once for the tree */
3341 free_extent_map(em);
3342 free_extent_map(em);
3345 * We've left one free space entry and other tasks trimming
3346 * this block group have left 1 entry each one. Free them.
3348 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3352 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3353 u64 *trimmed, u64 start, u64 end, u64 minlen)
3359 spin_lock(&block_group->lock);
3360 if (block_group->removed) {
3361 spin_unlock(&block_group->lock);
3364 btrfs_get_block_group_trimming(block_group);
3365 spin_unlock(&block_group->lock);
3367 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3371 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3373 btrfs_put_block_group_trimming(block_group);
3378 * Find the left-most item in the cache tree, and then return the
3379 * smallest inode number in the item.
3381 * Note: the returned inode number may not be the smallest one in
3382 * the tree, if the left-most item is a bitmap.
3384 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3386 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3387 struct btrfs_free_space *entry = NULL;
3390 spin_lock(&ctl->tree_lock);
3392 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3395 entry = rb_entry(rb_first(&ctl->free_space_offset),
3396 struct btrfs_free_space, offset_index);
3398 if (!entry->bitmap) {
3399 ino = entry->offset;
3401 unlink_free_space(ctl, entry);
3405 kmem_cache_free(btrfs_free_space_cachep, entry);
3407 link_free_space(ctl, entry);
3413 ret = search_bitmap(ctl, entry, &offset, &count, true);
3414 /* Logic error; Should be empty if it can't find anything */
3418 bitmap_clear_bits(ctl, entry, offset, 1);
3419 if (entry->bytes == 0)
3420 free_bitmap(ctl, entry);
3423 spin_unlock(&ctl->tree_lock);
3428 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3429 struct btrfs_path *path)
3431 struct inode *inode = NULL;
3433 spin_lock(&root->ino_cache_lock);
3434 if (root->ino_cache_inode)
3435 inode = igrab(root->ino_cache_inode);
3436 spin_unlock(&root->ino_cache_lock);
3440 inode = __lookup_free_space_inode(root, path, 0);
3444 spin_lock(&root->ino_cache_lock);
3445 if (!btrfs_fs_closing(root->fs_info))
3446 root->ino_cache_inode = igrab(inode);
3447 spin_unlock(&root->ino_cache_lock);
3452 int create_free_ino_inode(struct btrfs_root *root,
3453 struct btrfs_trans_handle *trans,
3454 struct btrfs_path *path)
3456 return __create_free_space_inode(root, trans, path,
3457 BTRFS_FREE_INO_OBJECTID, 0);
3460 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3462 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3463 struct btrfs_path *path;
3464 struct inode *inode;
3466 u64 root_gen = btrfs_root_generation(&root->root_item);
3468 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3472 * If we're unmounting then just return, since this does a search on the
3473 * normal root and not the commit root and we could deadlock.
3475 if (btrfs_fs_closing(fs_info))
3478 path = btrfs_alloc_path();
3482 inode = lookup_free_ino_inode(root, path);
3486 if (root_gen != BTRFS_I(inode)->generation)
3489 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3493 "failed to load free ino cache for root %llu",
3494 root->root_key.objectid);
3498 btrfs_free_path(path);
3502 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3503 struct btrfs_trans_handle *trans,
3504 struct btrfs_path *path,
3505 struct inode *inode)
3507 struct btrfs_fs_info *fs_info = root->fs_info;
3508 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3510 struct btrfs_io_ctl io_ctl;
3511 bool release_metadata = true;
3513 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3516 memset(&io_ctl, 0, sizeof(io_ctl));
3517 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3520 * At this point writepages() didn't error out, so our metadata
3521 * reservation is released when the writeback finishes, at
3522 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3523 * with or without an error.
3525 release_metadata = false;
3526 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3530 if (release_metadata)
3531 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3532 inode->i_size, true);
3535 "failed to write free ino cache for root %llu",
3536 root->root_key.objectid);
3543 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3545 * Use this if you need to make a bitmap or extent entry specifically, it
3546 * doesn't do any of the merging that add_free_space does, this acts a lot like
3547 * how the free space cache loading stuff works, so you can get really weird
3550 int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3551 u64 offset, u64 bytes, bool bitmap)
3553 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3554 struct btrfs_free_space *info = NULL, *bitmap_info;
3561 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3567 spin_lock(&ctl->tree_lock);
3568 info->offset = offset;
3569 info->bytes = bytes;
3570 info->max_extent_size = 0;
3571 ret = link_free_space(ctl, info);
3572 spin_unlock(&ctl->tree_lock);
3574 kmem_cache_free(btrfs_free_space_cachep, info);
3579 map = kzalloc(PAGE_SIZE, GFP_NOFS);
3581 kmem_cache_free(btrfs_free_space_cachep, info);
3586 spin_lock(&ctl->tree_lock);
3587 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3592 add_new_bitmap(ctl, info, offset);
3597 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3599 bytes -= bytes_added;
3600 offset += bytes_added;
3601 spin_unlock(&ctl->tree_lock);
3607 kmem_cache_free(btrfs_free_space_cachep, info);
3614 * Checks to see if the given range is in the free space cache. This is really
3615 * just used to check the absence of space, so if there is free space in the
3616 * range at all we will return 1.
3618 int test_check_exists(struct btrfs_block_group_cache *cache,
3619 u64 offset, u64 bytes)
3621 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3622 struct btrfs_free_space *info;
3625 spin_lock(&ctl->tree_lock);
3626 info = tree_search_offset(ctl, offset, 0, 0);
3628 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3636 u64 bit_off, bit_bytes;
3638 struct btrfs_free_space *tmp;
3641 bit_bytes = ctl->unit;
3642 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
3644 if (bit_off == offset) {
3647 } else if (bit_off > offset &&
3648 offset + bytes > bit_off) {
3654 n = rb_prev(&info->offset_index);
3656 tmp = rb_entry(n, struct btrfs_free_space,
3658 if (tmp->offset + tmp->bytes < offset)
3660 if (offset + bytes < tmp->offset) {
3661 n = rb_prev(&tmp->offset_index);
3668 n = rb_next(&info->offset_index);
3670 tmp = rb_entry(n, struct btrfs_free_space,
3672 if (offset + bytes < tmp->offset)
3674 if (tmp->offset + tmp->bytes < offset) {
3675 n = rb_next(&tmp->offset_index);
3686 if (info->offset == offset) {
3691 if (offset > info->offset && offset < info->offset + info->bytes)
3694 spin_unlock(&ctl->tree_lock);
3697 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */