1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Red Hat. All rights reserved.
6 #include <linux/pagemap.h>
7 #include <linux/sched.h>
8 #include <linux/sched/signal.h>
9 #include <linux/slab.h>
10 #include <linux/math64.h>
11 #include <linux/ratelimit.h>
12 #include <linux/error-injection.h>
13 #include <linux/sched/mm.h>
15 #include "free-space-cache.h"
16 #include "transaction.h"
18 #include "extent_io.h"
19 #include "inode-map.h"
21 #include "space-info.h"
22 #include "delalloc-space.h"
23 #include "block-group.h"
25 #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
26 #define MAX_CACHE_BYTES_PER_GIG SZ_32K
28 struct btrfs_trim_range {
31 struct list_head list;
34 static int link_free_space(struct btrfs_free_space_ctl *ctl,
35 struct btrfs_free_space *info);
36 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
37 struct btrfs_free_space *info);
38 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
39 struct btrfs_trans_handle *trans,
40 struct btrfs_io_ctl *io_ctl,
41 struct btrfs_path *path);
43 static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
44 struct btrfs_path *path,
47 struct btrfs_fs_info *fs_info = root->fs_info;
49 struct btrfs_key location;
50 struct btrfs_disk_key disk_key;
51 struct btrfs_free_space_header *header;
52 struct extent_buffer *leaf;
53 struct inode *inode = NULL;
57 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
61 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
65 btrfs_release_path(path);
66 return ERR_PTR(-ENOENT);
69 leaf = path->nodes[0];
70 header = btrfs_item_ptr(leaf, path->slots[0],
71 struct btrfs_free_space_header);
72 btrfs_free_space_key(leaf, header, &disk_key);
73 btrfs_disk_key_to_cpu(&location, &disk_key);
74 btrfs_release_path(path);
77 * We are often under a trans handle at this point, so we need to make
78 * sure NOFS is set to keep us from deadlocking.
80 nofs_flag = memalloc_nofs_save();
81 inode = btrfs_iget_path(fs_info->sb, &location, root, NULL, path);
82 btrfs_release_path(path);
83 memalloc_nofs_restore(nofs_flag);
87 mapping_set_gfp_mask(inode->i_mapping,
88 mapping_gfp_constraint(inode->i_mapping,
89 ~(__GFP_FS | __GFP_HIGHMEM)));
94 struct inode *lookup_free_space_inode(
95 struct btrfs_block_group_cache *block_group,
96 struct btrfs_path *path)
98 struct btrfs_fs_info *fs_info = block_group->fs_info;
99 struct inode *inode = NULL;
100 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
102 spin_lock(&block_group->lock);
103 if (block_group->inode)
104 inode = igrab(block_group->inode);
105 spin_unlock(&block_group->lock);
109 inode = __lookup_free_space_inode(fs_info->tree_root, path,
110 block_group->key.objectid);
114 spin_lock(&block_group->lock);
115 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
116 btrfs_info(fs_info, "Old style space inode found, converting.");
117 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
118 BTRFS_INODE_NODATACOW;
119 block_group->disk_cache_state = BTRFS_DC_CLEAR;
122 if (!block_group->iref) {
123 block_group->inode = igrab(inode);
124 block_group->iref = 1;
126 spin_unlock(&block_group->lock);
131 static int __create_free_space_inode(struct btrfs_root *root,
132 struct btrfs_trans_handle *trans,
133 struct btrfs_path *path,
136 struct btrfs_key key;
137 struct btrfs_disk_key disk_key;
138 struct btrfs_free_space_header *header;
139 struct btrfs_inode_item *inode_item;
140 struct extent_buffer *leaf;
141 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
144 ret = btrfs_insert_empty_inode(trans, root, path, ino);
148 /* We inline crc's for the free disk space cache */
149 if (ino != BTRFS_FREE_INO_OBJECTID)
150 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
152 leaf = path->nodes[0];
153 inode_item = btrfs_item_ptr(leaf, path->slots[0],
154 struct btrfs_inode_item);
155 btrfs_item_key(leaf, &disk_key, path->slots[0]);
156 memzero_extent_buffer(leaf, (unsigned long)inode_item,
157 sizeof(*inode_item));
158 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
159 btrfs_set_inode_size(leaf, inode_item, 0);
160 btrfs_set_inode_nbytes(leaf, inode_item, 0);
161 btrfs_set_inode_uid(leaf, inode_item, 0);
162 btrfs_set_inode_gid(leaf, inode_item, 0);
163 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
164 btrfs_set_inode_flags(leaf, inode_item, flags);
165 btrfs_set_inode_nlink(leaf, inode_item, 1);
166 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
167 btrfs_set_inode_block_group(leaf, inode_item, offset);
168 btrfs_mark_buffer_dirty(leaf);
169 btrfs_release_path(path);
171 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
174 ret = btrfs_insert_empty_item(trans, root, path, &key,
175 sizeof(struct btrfs_free_space_header));
177 btrfs_release_path(path);
181 leaf = path->nodes[0];
182 header = btrfs_item_ptr(leaf, path->slots[0],
183 struct btrfs_free_space_header);
184 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
185 btrfs_set_free_space_key(leaf, header, &disk_key);
186 btrfs_mark_buffer_dirty(leaf);
187 btrfs_release_path(path);
192 int create_free_space_inode(struct btrfs_trans_handle *trans,
193 struct btrfs_block_group_cache *block_group,
194 struct btrfs_path *path)
199 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
203 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
204 ino, block_group->key.objectid);
207 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
208 struct btrfs_block_rsv *rsv)
213 /* 1 for slack space, 1 for updating the inode */
214 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
215 btrfs_calc_metadata_size(fs_info, 1);
217 spin_lock(&rsv->lock);
218 if (rsv->reserved < needed_bytes)
222 spin_unlock(&rsv->lock);
226 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
227 struct btrfs_block_group_cache *block_group,
230 struct btrfs_root *root = BTRFS_I(inode)->root;
235 struct btrfs_path *path = btrfs_alloc_path();
242 mutex_lock(&trans->transaction->cache_write_mutex);
243 if (!list_empty(&block_group->io_list)) {
244 list_del_init(&block_group->io_list);
246 btrfs_wait_cache_io(trans, block_group, path);
247 btrfs_put_block_group(block_group);
251 * now that we've truncated the cache away, its no longer
254 spin_lock(&block_group->lock);
255 block_group->disk_cache_state = BTRFS_DC_CLEAR;
256 spin_unlock(&block_group->lock);
257 btrfs_free_path(path);
260 btrfs_i_size_write(BTRFS_I(inode), 0);
261 truncate_pagecache(inode, 0);
264 * We skip the throttling logic for free space cache inodes, so we don't
265 * need to check for -EAGAIN.
267 ret = btrfs_truncate_inode_items(trans, root, inode,
268 0, BTRFS_EXTENT_DATA_KEY);
272 ret = btrfs_update_inode(trans, root, inode);
276 mutex_unlock(&trans->transaction->cache_write_mutex);
278 btrfs_abort_transaction(trans, ret);
283 static void readahead_cache(struct inode *inode)
285 struct file_ra_state *ra;
286 unsigned long last_index;
288 ra = kzalloc(sizeof(*ra), GFP_NOFS);
292 file_ra_state_init(ra, inode->i_mapping);
293 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
295 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
300 static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
306 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
308 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
311 /* Make sure we can fit our crcs and generation into the first page */
312 if (write && check_crcs &&
313 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
316 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
318 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
322 io_ctl->num_pages = num_pages;
323 io_ctl->fs_info = btrfs_sb(inode->i_sb);
324 io_ctl->check_crcs = check_crcs;
325 io_ctl->inode = inode;
329 ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
331 static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
333 kfree(io_ctl->pages);
334 io_ctl->pages = NULL;
337 static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
345 static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
347 ASSERT(io_ctl->index < io_ctl->num_pages);
348 io_ctl->page = io_ctl->pages[io_ctl->index++];
349 io_ctl->cur = page_address(io_ctl->page);
350 io_ctl->orig = io_ctl->cur;
351 io_ctl->size = PAGE_SIZE;
353 clear_page(io_ctl->cur);
356 static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
360 io_ctl_unmap_page(io_ctl);
362 for (i = 0; i < io_ctl->num_pages; i++) {
363 if (io_ctl->pages[i]) {
364 ClearPageChecked(io_ctl->pages[i]);
365 unlock_page(io_ctl->pages[i]);
366 put_page(io_ctl->pages[i]);
371 static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
375 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
378 for (i = 0; i < io_ctl->num_pages; i++) {
379 page = find_or_create_page(inode->i_mapping, i, mask);
381 io_ctl_drop_pages(io_ctl);
384 io_ctl->pages[i] = page;
385 if (uptodate && !PageUptodate(page)) {
386 btrfs_readpage(NULL, page);
388 if (page->mapping != inode->i_mapping) {
389 btrfs_err(BTRFS_I(inode)->root->fs_info,
390 "free space cache page truncated");
391 io_ctl_drop_pages(io_ctl);
394 if (!PageUptodate(page)) {
395 btrfs_err(BTRFS_I(inode)->root->fs_info,
396 "error reading free space cache");
397 io_ctl_drop_pages(io_ctl);
403 for (i = 0; i < io_ctl->num_pages; i++) {
404 clear_page_dirty_for_io(io_ctl->pages[i]);
405 set_page_extent_mapped(io_ctl->pages[i]);
411 static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
415 io_ctl_map_page(io_ctl, 1);
418 * Skip the csum areas. If we don't check crcs then we just have a
419 * 64bit chunk at the front of the first page.
421 if (io_ctl->check_crcs) {
422 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
423 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
425 io_ctl->cur += sizeof(u64);
426 io_ctl->size -= sizeof(u64) * 2;
430 *val = cpu_to_le64(generation);
431 io_ctl->cur += sizeof(u64);
434 static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
439 * Skip the crc area. If we don't check crcs then we just have a 64bit
440 * chunk at the front of the first page.
442 if (io_ctl->check_crcs) {
443 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
444 io_ctl->size -= sizeof(u64) +
445 (sizeof(u32) * io_ctl->num_pages);
447 io_ctl->cur += sizeof(u64);
448 io_ctl->size -= sizeof(u64) * 2;
452 if (le64_to_cpu(*gen) != generation) {
453 btrfs_err_rl(io_ctl->fs_info,
454 "space cache generation (%llu) does not match inode (%llu)",
456 io_ctl_unmap_page(io_ctl);
459 io_ctl->cur += sizeof(u64);
463 static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
469 if (!io_ctl->check_crcs) {
470 io_ctl_unmap_page(io_ctl);
475 offset = sizeof(u32) * io_ctl->num_pages;
477 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
478 btrfs_crc32c_final(crc, (u8 *)&crc);
479 io_ctl_unmap_page(io_ctl);
480 tmp = page_address(io_ctl->pages[0]);
485 static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
491 if (!io_ctl->check_crcs) {
492 io_ctl_map_page(io_ctl, 0);
497 offset = sizeof(u32) * io_ctl->num_pages;
499 tmp = page_address(io_ctl->pages[0]);
503 io_ctl_map_page(io_ctl, 0);
504 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
505 btrfs_crc32c_final(crc, (u8 *)&crc);
507 btrfs_err_rl(io_ctl->fs_info,
508 "csum mismatch on free space cache");
509 io_ctl_unmap_page(io_ctl);
516 static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
519 struct btrfs_free_space_entry *entry;
525 entry->offset = cpu_to_le64(offset);
526 entry->bytes = cpu_to_le64(bytes);
527 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
528 BTRFS_FREE_SPACE_EXTENT;
529 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
530 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
532 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
535 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
537 /* No more pages to map */
538 if (io_ctl->index >= io_ctl->num_pages)
541 /* map the next page */
542 io_ctl_map_page(io_ctl, 1);
546 static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
552 * If we aren't at the start of the current page, unmap this one and
553 * map the next one if there is any left.
555 if (io_ctl->cur != io_ctl->orig) {
556 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
557 if (io_ctl->index >= io_ctl->num_pages)
559 io_ctl_map_page(io_ctl, 0);
562 copy_page(io_ctl->cur, bitmap);
563 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
564 if (io_ctl->index < io_ctl->num_pages)
565 io_ctl_map_page(io_ctl, 0);
569 static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
572 * If we're not on the boundary we know we've modified the page and we
573 * need to crc the page.
575 if (io_ctl->cur != io_ctl->orig)
576 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
578 io_ctl_unmap_page(io_ctl);
580 while (io_ctl->index < io_ctl->num_pages) {
581 io_ctl_map_page(io_ctl, 1);
582 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
586 static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
587 struct btrfs_free_space *entry, u8 *type)
589 struct btrfs_free_space_entry *e;
593 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
599 entry->offset = le64_to_cpu(e->offset);
600 entry->bytes = le64_to_cpu(e->bytes);
602 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
603 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
605 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
608 io_ctl_unmap_page(io_ctl);
613 static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
614 struct btrfs_free_space *entry)
618 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
622 copy_page(entry->bitmap, io_ctl->cur);
623 io_ctl_unmap_page(io_ctl);
629 * Since we attach pinned extents after the fact we can have contiguous sections
630 * of free space that are split up in entries. This poses a problem with the
631 * tree logging stuff since it could have allocated across what appears to be 2
632 * entries since we would have merged the entries when adding the pinned extents
633 * back to the free space cache. So run through the space cache that we just
634 * loaded and merge contiguous entries. This will make the log replay stuff not
635 * blow up and it will make for nicer allocator behavior.
637 static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
639 struct btrfs_free_space *e, *prev = NULL;
643 spin_lock(&ctl->tree_lock);
644 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
645 e = rb_entry(n, struct btrfs_free_space, offset_index);
648 if (e->bitmap || prev->bitmap)
650 if (prev->offset + prev->bytes == e->offset) {
651 unlink_free_space(ctl, prev);
652 unlink_free_space(ctl, e);
653 prev->bytes += e->bytes;
654 kmem_cache_free(btrfs_free_space_cachep, e);
655 link_free_space(ctl, prev);
657 spin_unlock(&ctl->tree_lock);
663 spin_unlock(&ctl->tree_lock);
666 static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
667 struct btrfs_free_space_ctl *ctl,
668 struct btrfs_path *path, u64 offset)
670 struct btrfs_fs_info *fs_info = root->fs_info;
671 struct btrfs_free_space_header *header;
672 struct extent_buffer *leaf;
673 struct btrfs_io_ctl io_ctl;
674 struct btrfs_key key;
675 struct btrfs_free_space *e, *n;
683 /* Nothing in the space cache, goodbye */
684 if (!i_size_read(inode))
687 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
691 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
695 btrfs_release_path(path);
701 leaf = path->nodes[0];
702 header = btrfs_item_ptr(leaf, path->slots[0],
703 struct btrfs_free_space_header);
704 num_entries = btrfs_free_space_entries(leaf, header);
705 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
706 generation = btrfs_free_space_generation(leaf, header);
707 btrfs_release_path(path);
709 if (!BTRFS_I(inode)->generation) {
711 "the free space cache file (%llu) is invalid, skip it",
716 if (BTRFS_I(inode)->generation != generation) {
718 "free space inode generation (%llu) did not match free space cache generation (%llu)",
719 BTRFS_I(inode)->generation, generation);
726 ret = io_ctl_init(&io_ctl, inode, 0);
730 readahead_cache(inode);
732 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
736 ret = io_ctl_check_crc(&io_ctl, 0);
740 ret = io_ctl_check_generation(&io_ctl, generation);
744 while (num_entries) {
745 e = kmem_cache_zalloc(btrfs_free_space_cachep,
752 ret = io_ctl_read_entry(&io_ctl, e, &type);
754 kmem_cache_free(btrfs_free_space_cachep, e);
760 kmem_cache_free(btrfs_free_space_cachep, e);
764 if (type == BTRFS_FREE_SPACE_EXTENT) {
765 spin_lock(&ctl->tree_lock);
766 ret = link_free_space(ctl, e);
767 spin_unlock(&ctl->tree_lock);
770 "Duplicate entries in free space cache, dumping");
771 kmem_cache_free(btrfs_free_space_cachep, e);
777 e->bitmap = kmem_cache_zalloc(
778 btrfs_free_space_bitmap_cachep, GFP_NOFS);
782 btrfs_free_space_cachep, e);
785 spin_lock(&ctl->tree_lock);
786 ret = link_free_space(ctl, e);
788 spin_unlock(&ctl->tree_lock);
790 "Duplicate entries in free space cache, dumping");
791 kmem_cache_free(btrfs_free_space_cachep, e);
794 ctl->total_bitmaps++;
795 ctl->op->recalc_thresholds(ctl);
796 spin_unlock(&ctl->tree_lock);
797 list_add_tail(&e->list, &bitmaps);
803 io_ctl_unmap_page(&io_ctl);
806 * We add the bitmaps at the end of the entries in order that
807 * the bitmap entries are added to the cache.
809 list_for_each_entry_safe(e, n, &bitmaps, list) {
810 list_del_init(&e->list);
811 ret = io_ctl_read_bitmap(&io_ctl, e);
816 io_ctl_drop_pages(&io_ctl);
817 merge_space_tree(ctl);
820 io_ctl_free(&io_ctl);
823 io_ctl_drop_pages(&io_ctl);
824 __btrfs_remove_free_space_cache(ctl);
828 int load_free_space_cache(struct btrfs_block_group_cache *block_group)
830 struct btrfs_fs_info *fs_info = block_group->fs_info;
831 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
833 struct btrfs_path *path;
836 u64 used = btrfs_block_group_used(&block_group->item);
839 * If this block group has been marked to be cleared for one reason or
840 * another then we can't trust the on disk cache, so just return.
842 spin_lock(&block_group->lock);
843 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
844 spin_unlock(&block_group->lock);
847 spin_unlock(&block_group->lock);
849 path = btrfs_alloc_path();
852 path->search_commit_root = 1;
853 path->skip_locking = 1;
856 * We must pass a path with search_commit_root set to btrfs_iget in
857 * order to avoid a deadlock when allocating extents for the tree root.
859 * When we are COWing an extent buffer from the tree root, when looking
860 * for a free extent, at extent-tree.c:find_free_extent(), we can find
861 * block group without its free space cache loaded. When we find one
862 * we must load its space cache which requires reading its free space
863 * cache's inode item from the root tree. If this inode item is located
864 * in the same leaf that we started COWing before, then we end up in
865 * deadlock on the extent buffer (trying to read lock it when we
866 * previously write locked it).
868 * It's safe to read the inode item using the commit root because
869 * block groups, once loaded, stay in memory forever (until they are
870 * removed) as well as their space caches once loaded. New block groups
871 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
872 * we will never try to read their inode item while the fs is mounted.
874 inode = lookup_free_space_inode(block_group, path);
876 btrfs_free_path(path);
880 /* We may have converted the inode and made the cache invalid. */
881 spin_lock(&block_group->lock);
882 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
883 spin_unlock(&block_group->lock);
884 btrfs_free_path(path);
887 spin_unlock(&block_group->lock);
889 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
890 path, block_group->key.objectid);
891 btrfs_free_path(path);
895 spin_lock(&ctl->tree_lock);
896 matched = (ctl->free_space == (block_group->key.offset - used -
897 block_group->bytes_super));
898 spin_unlock(&ctl->tree_lock);
901 __btrfs_remove_free_space_cache(ctl);
903 "block group %llu has wrong amount of free space",
904 block_group->key.objectid);
909 /* This cache is bogus, make sure it gets cleared */
910 spin_lock(&block_group->lock);
911 block_group->disk_cache_state = BTRFS_DC_CLEAR;
912 spin_unlock(&block_group->lock);
916 "failed to load free space cache for block group %llu, rebuilding it now",
917 block_group->key.objectid);
924 static noinline_for_stack
925 int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
926 struct btrfs_free_space_ctl *ctl,
927 struct btrfs_block_group_cache *block_group,
928 int *entries, int *bitmaps,
929 struct list_head *bitmap_list)
932 struct btrfs_free_cluster *cluster = NULL;
933 struct btrfs_free_cluster *cluster_locked = NULL;
934 struct rb_node *node = rb_first(&ctl->free_space_offset);
935 struct btrfs_trim_range *trim_entry;
937 /* Get the cluster for this block_group if it exists */
938 if (block_group && !list_empty(&block_group->cluster_list)) {
939 cluster = list_entry(block_group->cluster_list.next,
940 struct btrfs_free_cluster,
944 if (!node && cluster) {
945 cluster_locked = cluster;
946 spin_lock(&cluster_locked->lock);
947 node = rb_first(&cluster->root);
951 /* Write out the extent entries */
953 struct btrfs_free_space *e;
955 e = rb_entry(node, struct btrfs_free_space, offset_index);
958 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
964 list_add_tail(&e->list, bitmap_list);
967 node = rb_next(node);
968 if (!node && cluster) {
969 node = rb_first(&cluster->root);
970 cluster_locked = cluster;
971 spin_lock(&cluster_locked->lock);
975 if (cluster_locked) {
976 spin_unlock(&cluster_locked->lock);
977 cluster_locked = NULL;
981 * Make sure we don't miss any range that was removed from our rbtree
982 * because trimming is running. Otherwise after a umount+mount (or crash
983 * after committing the transaction) we would leak free space and get
984 * an inconsistent free space cache report from fsck.
986 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
987 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
988 trim_entry->bytes, NULL);
997 spin_unlock(&cluster_locked->lock);
1001 static noinline_for_stack int
1002 update_cache_item(struct btrfs_trans_handle *trans,
1003 struct btrfs_root *root,
1004 struct inode *inode,
1005 struct btrfs_path *path, u64 offset,
1006 int entries, int bitmaps)
1008 struct btrfs_key key;
1009 struct btrfs_free_space_header *header;
1010 struct extent_buffer *leaf;
1013 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1014 key.offset = offset;
1017 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1019 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1020 EXTENT_DELALLOC, 0, 0, NULL);
1023 leaf = path->nodes[0];
1025 struct btrfs_key found_key;
1026 ASSERT(path->slots[0]);
1028 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1029 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1030 found_key.offset != offset) {
1031 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1032 inode->i_size - 1, EXTENT_DELALLOC, 0,
1034 btrfs_release_path(path);
1039 BTRFS_I(inode)->generation = trans->transid;
1040 header = btrfs_item_ptr(leaf, path->slots[0],
1041 struct btrfs_free_space_header);
1042 btrfs_set_free_space_entries(leaf, header, entries);
1043 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1044 btrfs_set_free_space_generation(leaf, header, trans->transid);
1045 btrfs_mark_buffer_dirty(leaf);
1046 btrfs_release_path(path);
1054 static noinline_for_stack int write_pinned_extent_entries(
1055 struct btrfs_block_group_cache *block_group,
1056 struct btrfs_io_ctl *io_ctl,
1059 u64 start, extent_start, extent_end, len;
1060 struct extent_io_tree *unpin = NULL;
1067 * We want to add any pinned extents to our free space cache
1068 * so we don't leak the space
1070 * We shouldn't have switched the pinned extents yet so this is the
1073 unpin = block_group->fs_info->pinned_extents;
1075 start = block_group->key.objectid;
1077 while (start < block_group->key.objectid + block_group->key.offset) {
1078 ret = find_first_extent_bit(unpin, start,
1079 &extent_start, &extent_end,
1080 EXTENT_DIRTY, NULL);
1084 /* This pinned extent is out of our range */
1085 if (extent_start >= block_group->key.objectid +
1086 block_group->key.offset)
1089 extent_start = max(extent_start, start);
1090 extent_end = min(block_group->key.objectid +
1091 block_group->key.offset, extent_end + 1);
1092 len = extent_end - extent_start;
1095 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1105 static noinline_for_stack int
1106 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
1108 struct btrfs_free_space *entry, *next;
1111 /* Write out the bitmaps */
1112 list_for_each_entry_safe(entry, next, bitmap_list, list) {
1113 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1116 list_del_init(&entry->list);
1122 static int flush_dirty_cache(struct inode *inode)
1126 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1128 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1129 EXTENT_DELALLOC, 0, 0, NULL);
1134 static void noinline_for_stack
1135 cleanup_bitmap_list(struct list_head *bitmap_list)
1137 struct btrfs_free_space *entry, *next;
1139 list_for_each_entry_safe(entry, next, bitmap_list, list)
1140 list_del_init(&entry->list);
1143 static void noinline_for_stack
1144 cleanup_write_cache_enospc(struct inode *inode,
1145 struct btrfs_io_ctl *io_ctl,
1146 struct extent_state **cached_state)
1148 io_ctl_drop_pages(io_ctl);
1149 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1150 i_size_read(inode) - 1, cached_state);
1153 static int __btrfs_wait_cache_io(struct btrfs_root *root,
1154 struct btrfs_trans_handle *trans,
1155 struct btrfs_block_group_cache *block_group,
1156 struct btrfs_io_ctl *io_ctl,
1157 struct btrfs_path *path, u64 offset)
1160 struct inode *inode = io_ctl->inode;
1165 /* Flush the dirty pages in the cache file. */
1166 ret = flush_dirty_cache(inode);
1170 /* Update the cache item to tell everyone this cache file is valid. */
1171 ret = update_cache_item(trans, root, inode, path, offset,
1172 io_ctl->entries, io_ctl->bitmaps);
1175 invalidate_inode_pages2(inode->i_mapping);
1176 BTRFS_I(inode)->generation = 0;
1179 btrfs_err(root->fs_info,
1180 "failed to write free space cache for block group %llu",
1181 block_group->key.objectid);
1185 btrfs_update_inode(trans, root, inode);
1188 /* the dirty list is protected by the dirty_bgs_lock */
1189 spin_lock(&trans->transaction->dirty_bgs_lock);
1191 /* the disk_cache_state is protected by the block group lock */
1192 spin_lock(&block_group->lock);
1195 * only mark this as written if we didn't get put back on
1196 * the dirty list while waiting for IO. Otherwise our
1197 * cache state won't be right, and we won't get written again
1199 if (!ret && list_empty(&block_group->dirty_list))
1200 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1202 block_group->disk_cache_state = BTRFS_DC_ERROR;
1204 spin_unlock(&block_group->lock);
1205 spin_unlock(&trans->transaction->dirty_bgs_lock);
1206 io_ctl->inode = NULL;
1214 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1215 struct btrfs_trans_handle *trans,
1216 struct btrfs_io_ctl *io_ctl,
1217 struct btrfs_path *path)
1219 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1222 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1223 struct btrfs_block_group_cache *block_group,
1224 struct btrfs_path *path)
1226 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1227 block_group, &block_group->io_ctl,
1228 path, block_group->key.objectid);
1232 * __btrfs_write_out_cache - write out cached info to an inode
1233 * @root - the root the inode belongs to
1234 * @ctl - the free space cache we are going to write out
1235 * @block_group - the block_group for this cache if it belongs to a block_group
1236 * @trans - the trans handle
1238 * This function writes out a free space cache struct to disk for quick recovery
1239 * on mount. This will return 0 if it was successful in writing the cache out,
1240 * or an errno if it was not.
1242 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1243 struct btrfs_free_space_ctl *ctl,
1244 struct btrfs_block_group_cache *block_group,
1245 struct btrfs_io_ctl *io_ctl,
1246 struct btrfs_trans_handle *trans)
1248 struct extent_state *cached_state = NULL;
1249 LIST_HEAD(bitmap_list);
1255 if (!i_size_read(inode))
1258 WARN_ON(io_ctl->pages);
1259 ret = io_ctl_init(io_ctl, inode, 1);
1263 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1264 down_write(&block_group->data_rwsem);
1265 spin_lock(&block_group->lock);
1266 if (block_group->delalloc_bytes) {
1267 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1268 spin_unlock(&block_group->lock);
1269 up_write(&block_group->data_rwsem);
1270 BTRFS_I(inode)->generation = 0;
1275 spin_unlock(&block_group->lock);
1278 /* Lock all pages first so we can lock the extent safely. */
1279 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1283 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1286 io_ctl_set_generation(io_ctl, trans->transid);
1288 mutex_lock(&ctl->cache_writeout_mutex);
1289 /* Write out the extent entries in the free space cache */
1290 spin_lock(&ctl->tree_lock);
1291 ret = write_cache_extent_entries(io_ctl, ctl,
1292 block_group, &entries, &bitmaps,
1295 goto out_nospc_locked;
1298 * Some spaces that are freed in the current transaction are pinned,
1299 * they will be added into free space cache after the transaction is
1300 * committed, we shouldn't lose them.
1302 * If this changes while we are working we'll get added back to
1303 * the dirty list and redo it. No locking needed
1305 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
1307 goto out_nospc_locked;
1310 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1311 * locked while doing it because a concurrent trim can be manipulating
1312 * or freeing the bitmap.
1314 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1315 spin_unlock(&ctl->tree_lock);
1316 mutex_unlock(&ctl->cache_writeout_mutex);
1320 /* Zero out the rest of the pages just to make sure */
1321 io_ctl_zero_remaining_pages(io_ctl);
1323 /* Everything is written out, now we dirty the pages in the file. */
1324 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1325 i_size_read(inode), &cached_state);
1329 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1330 up_write(&block_group->data_rwsem);
1332 * Release the pages and unlock the extent, we will flush
1335 io_ctl_drop_pages(io_ctl);
1336 io_ctl_free(io_ctl);
1338 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1339 i_size_read(inode) - 1, &cached_state);
1342 * at this point the pages are under IO and we're happy,
1343 * The caller is responsible for waiting on them and updating
1344 * the cache and the inode
1346 io_ctl->entries = entries;
1347 io_ctl->bitmaps = bitmaps;
1349 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1356 io_ctl->inode = NULL;
1357 io_ctl_free(io_ctl);
1359 invalidate_inode_pages2(inode->i_mapping);
1360 BTRFS_I(inode)->generation = 0;
1362 btrfs_update_inode(trans, root, inode);
1368 cleanup_bitmap_list(&bitmap_list);
1369 spin_unlock(&ctl->tree_lock);
1370 mutex_unlock(&ctl->cache_writeout_mutex);
1373 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1376 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1377 up_write(&block_group->data_rwsem);
1382 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
1383 struct btrfs_block_group_cache *block_group,
1384 struct btrfs_path *path)
1386 struct btrfs_fs_info *fs_info = trans->fs_info;
1387 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1388 struct inode *inode;
1391 spin_lock(&block_group->lock);
1392 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1393 spin_unlock(&block_group->lock);
1396 spin_unlock(&block_group->lock);
1398 inode = lookup_free_space_inode(block_group, path);
1402 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1403 block_group, &block_group->io_ctl, trans);
1407 "failed to write free space cache for block group %llu",
1408 block_group->key.objectid);
1410 spin_lock(&block_group->lock);
1411 block_group->disk_cache_state = BTRFS_DC_ERROR;
1412 spin_unlock(&block_group->lock);
1414 block_group->io_ctl.inode = NULL;
1419 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1420 * to wait for IO and put the inode
1426 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1429 ASSERT(offset >= bitmap_start);
1430 offset -= bitmap_start;
1431 return (unsigned long)(div_u64(offset, unit));
1434 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1436 return (unsigned long)(div_u64(bytes, unit));
1439 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1443 u64 bytes_per_bitmap;
1445 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1446 bitmap_start = offset - ctl->start;
1447 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1448 bitmap_start *= bytes_per_bitmap;
1449 bitmap_start += ctl->start;
1451 return bitmap_start;
1454 static int tree_insert_offset(struct rb_root *root, u64 offset,
1455 struct rb_node *node, int bitmap)
1457 struct rb_node **p = &root->rb_node;
1458 struct rb_node *parent = NULL;
1459 struct btrfs_free_space *info;
1463 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1465 if (offset < info->offset) {
1467 } else if (offset > info->offset) {
1468 p = &(*p)->rb_right;
1471 * we could have a bitmap entry and an extent entry
1472 * share the same offset. If this is the case, we want
1473 * the extent entry to always be found first if we do a
1474 * linear search through the tree, since we want to have
1475 * the quickest allocation time, and allocating from an
1476 * extent is faster than allocating from a bitmap. So
1477 * if we're inserting a bitmap and we find an entry at
1478 * this offset, we want to go right, or after this entry
1479 * logically. If we are inserting an extent and we've
1480 * found a bitmap, we want to go left, or before
1488 p = &(*p)->rb_right;
1490 if (!info->bitmap) {
1499 rb_link_node(node, parent, p);
1500 rb_insert_color(node, root);
1506 * searches the tree for the given offset.
1508 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1509 * want a section that has at least bytes size and comes at or after the given
1512 static struct btrfs_free_space *
1513 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1514 u64 offset, int bitmap_only, int fuzzy)
1516 struct rb_node *n = ctl->free_space_offset.rb_node;
1517 struct btrfs_free_space *entry, *prev = NULL;
1519 /* find entry that is closest to the 'offset' */
1526 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1529 if (offset < entry->offset)
1531 else if (offset > entry->offset)
1544 * bitmap entry and extent entry may share same offset,
1545 * in that case, bitmap entry comes after extent entry.
1550 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1551 if (entry->offset != offset)
1554 WARN_ON(!entry->bitmap);
1557 if (entry->bitmap) {
1559 * if previous extent entry covers the offset,
1560 * we should return it instead of the bitmap entry
1562 n = rb_prev(&entry->offset_index);
1564 prev = rb_entry(n, struct btrfs_free_space,
1566 if (!prev->bitmap &&
1567 prev->offset + prev->bytes > offset)
1577 /* find last entry before the 'offset' */
1579 if (entry->offset > offset) {
1580 n = rb_prev(&entry->offset_index);
1582 entry = rb_entry(n, struct btrfs_free_space,
1584 ASSERT(entry->offset <= offset);
1593 if (entry->bitmap) {
1594 n = rb_prev(&entry->offset_index);
1596 prev = rb_entry(n, struct btrfs_free_space,
1598 if (!prev->bitmap &&
1599 prev->offset + prev->bytes > offset)
1602 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1604 } else if (entry->offset + entry->bytes > offset)
1611 if (entry->bitmap) {
1612 if (entry->offset + BITS_PER_BITMAP *
1616 if (entry->offset + entry->bytes > offset)
1620 n = rb_next(&entry->offset_index);
1623 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1629 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1630 struct btrfs_free_space *info)
1632 rb_erase(&info->offset_index, &ctl->free_space_offset);
1633 ctl->free_extents--;
1636 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1637 struct btrfs_free_space *info)
1639 __unlink_free_space(ctl, info);
1640 ctl->free_space -= info->bytes;
1643 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1644 struct btrfs_free_space *info)
1648 ASSERT(info->bytes || info->bitmap);
1649 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1650 &info->offset_index, (info->bitmap != NULL));
1654 ctl->free_space += info->bytes;
1655 ctl->free_extents++;
1659 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1661 struct btrfs_block_group_cache *block_group = ctl->private;
1665 u64 size = block_group->key.offset;
1666 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1667 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1669 max_bitmaps = max_t(u64, max_bitmaps, 1);
1671 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1674 * The goal is to keep the total amount of memory used per 1gb of space
1675 * at or below 32k, so we need to adjust how much memory we allow to be
1676 * used by extent based free space tracking
1679 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1681 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1684 * we want to account for 1 more bitmap than what we have so we can make
1685 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1686 * we add more bitmaps.
1688 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
1690 if (bitmap_bytes >= max_bytes) {
1691 ctl->extents_thresh = 0;
1696 * we want the extent entry threshold to always be at most 1/2 the max
1697 * bytes we can have, or whatever is less than that.
1699 extent_bytes = max_bytes - bitmap_bytes;
1700 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1702 ctl->extents_thresh =
1703 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1706 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1707 struct btrfs_free_space *info,
1708 u64 offset, u64 bytes)
1710 unsigned long start, count;
1712 start = offset_to_bit(info->offset, ctl->unit, offset);
1713 count = bytes_to_bits(bytes, ctl->unit);
1714 ASSERT(start + count <= BITS_PER_BITMAP);
1716 bitmap_clear(info->bitmap, start, count);
1718 info->bytes -= bytes;
1719 if (info->max_extent_size > ctl->unit)
1720 info->max_extent_size = 0;
1723 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1724 struct btrfs_free_space *info, u64 offset,
1727 __bitmap_clear_bits(ctl, info, offset, bytes);
1728 ctl->free_space -= bytes;
1731 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1732 struct btrfs_free_space *info, u64 offset,
1735 unsigned long start, count;
1737 start = offset_to_bit(info->offset, ctl->unit, offset);
1738 count = bytes_to_bits(bytes, ctl->unit);
1739 ASSERT(start + count <= BITS_PER_BITMAP);
1741 bitmap_set(info->bitmap, start, count);
1743 info->bytes += bytes;
1744 ctl->free_space += bytes;
1748 * If we can not find suitable extent, we will use bytes to record
1749 * the size of the max extent.
1751 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1752 struct btrfs_free_space *bitmap_info, u64 *offset,
1753 u64 *bytes, bool for_alloc)
1755 unsigned long found_bits = 0;
1756 unsigned long max_bits = 0;
1757 unsigned long bits, i;
1758 unsigned long next_zero;
1759 unsigned long extent_bits;
1762 * Skip searching the bitmap if we don't have a contiguous section that
1763 * is large enough for this allocation.
1766 bitmap_info->max_extent_size &&
1767 bitmap_info->max_extent_size < *bytes) {
1768 *bytes = bitmap_info->max_extent_size;
1772 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1773 max_t(u64, *offset, bitmap_info->offset));
1774 bits = bytes_to_bits(*bytes, ctl->unit);
1776 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1777 if (for_alloc && bits == 1) {
1781 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1782 BITS_PER_BITMAP, i);
1783 extent_bits = next_zero - i;
1784 if (extent_bits >= bits) {
1785 found_bits = extent_bits;
1787 } else if (extent_bits > max_bits) {
1788 max_bits = extent_bits;
1794 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1795 *bytes = (u64)(found_bits) * ctl->unit;
1799 *bytes = (u64)(max_bits) * ctl->unit;
1800 bitmap_info->max_extent_size = *bytes;
1804 static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1807 return entry->max_extent_size;
1808 return entry->bytes;
1811 /* Cache the size of the max extent in bytes */
1812 static struct btrfs_free_space *
1813 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1814 unsigned long align, u64 *max_extent_size)
1816 struct btrfs_free_space *entry;
1817 struct rb_node *node;
1822 if (!ctl->free_space_offset.rb_node)
1825 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1829 for (node = &entry->offset_index; node; node = rb_next(node)) {
1830 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1831 if (entry->bytes < *bytes) {
1832 *max_extent_size = max(get_max_extent_size(entry),
1837 /* make sure the space returned is big enough
1838 * to match our requested alignment
1840 if (*bytes >= align) {
1841 tmp = entry->offset - ctl->start + align - 1;
1842 tmp = div64_u64(tmp, align);
1843 tmp = tmp * align + ctl->start;
1844 align_off = tmp - entry->offset;
1847 tmp = entry->offset;
1850 if (entry->bytes < *bytes + align_off) {
1851 *max_extent_size = max(get_max_extent_size(entry),
1856 if (entry->bitmap) {
1859 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1866 max(get_max_extent_size(entry),
1873 *bytes = entry->bytes - align_off;
1880 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1881 struct btrfs_free_space *info, u64 offset)
1883 info->offset = offset_to_bitmap(ctl, offset);
1885 INIT_LIST_HEAD(&info->list);
1886 link_free_space(ctl, info);
1887 ctl->total_bitmaps++;
1889 ctl->op->recalc_thresholds(ctl);
1892 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1893 struct btrfs_free_space *bitmap_info)
1895 unlink_free_space(ctl, bitmap_info);
1896 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
1897 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1898 ctl->total_bitmaps--;
1899 ctl->op->recalc_thresholds(ctl);
1902 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1903 struct btrfs_free_space *bitmap_info,
1904 u64 *offset, u64 *bytes)
1907 u64 search_start, search_bytes;
1911 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1914 * We need to search for bits in this bitmap. We could only cover some
1915 * of the extent in this bitmap thanks to how we add space, so we need
1916 * to search for as much as it as we can and clear that amount, and then
1917 * go searching for the next bit.
1919 search_start = *offset;
1920 search_bytes = ctl->unit;
1921 search_bytes = min(search_bytes, end - search_start + 1);
1922 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1924 if (ret < 0 || search_start != *offset)
1927 /* We may have found more bits than what we need */
1928 search_bytes = min(search_bytes, *bytes);
1930 /* Cannot clear past the end of the bitmap */
1931 search_bytes = min(search_bytes, end - search_start + 1);
1933 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1934 *offset += search_bytes;
1935 *bytes -= search_bytes;
1938 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1939 if (!bitmap_info->bytes)
1940 free_bitmap(ctl, bitmap_info);
1943 * no entry after this bitmap, but we still have bytes to
1944 * remove, so something has gone wrong.
1949 bitmap_info = rb_entry(next, struct btrfs_free_space,
1953 * if the next entry isn't a bitmap we need to return to let the
1954 * extent stuff do its work.
1956 if (!bitmap_info->bitmap)
1960 * Ok the next item is a bitmap, but it may not actually hold
1961 * the information for the rest of this free space stuff, so
1962 * look for it, and if we don't find it return so we can try
1963 * everything over again.
1965 search_start = *offset;
1966 search_bytes = ctl->unit;
1967 ret = search_bitmap(ctl, bitmap_info, &search_start,
1968 &search_bytes, false);
1969 if (ret < 0 || search_start != *offset)
1973 } else if (!bitmap_info->bytes)
1974 free_bitmap(ctl, bitmap_info);
1979 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1980 struct btrfs_free_space *info, u64 offset,
1983 u64 bytes_to_set = 0;
1986 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1988 bytes_to_set = min(end - offset, bytes);
1990 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1993 * We set some bytes, we have no idea what the max extent size is
1996 info->max_extent_size = 0;
1998 return bytes_to_set;
2002 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2003 struct btrfs_free_space *info)
2005 struct btrfs_block_group_cache *block_group = ctl->private;
2006 struct btrfs_fs_info *fs_info = block_group->fs_info;
2007 bool forced = false;
2009 #ifdef CONFIG_BTRFS_DEBUG
2010 if (btrfs_should_fragment_free_space(block_group))
2015 * If we are below the extents threshold then we can add this as an
2016 * extent, and don't have to deal with the bitmap
2018 if (!forced && ctl->free_extents < ctl->extents_thresh) {
2020 * If this block group has some small extents we don't want to
2021 * use up all of our free slots in the cache with them, we want
2022 * to reserve them to larger extents, however if we have plenty
2023 * of cache left then go ahead an dadd them, no sense in adding
2024 * the overhead of a bitmap if we don't have to.
2026 if (info->bytes <= fs_info->sectorsize * 4) {
2027 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2035 * The original block groups from mkfs can be really small, like 8
2036 * megabytes, so don't bother with a bitmap for those entries. However
2037 * some block groups can be smaller than what a bitmap would cover but
2038 * are still large enough that they could overflow the 32k memory limit,
2039 * so allow those block groups to still be allowed to have a bitmap
2042 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
2048 static const struct btrfs_free_space_op free_space_op = {
2049 .recalc_thresholds = recalculate_thresholds,
2050 .use_bitmap = use_bitmap,
2053 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2054 struct btrfs_free_space *info)
2056 struct btrfs_free_space *bitmap_info;
2057 struct btrfs_block_group_cache *block_group = NULL;
2059 u64 bytes, offset, bytes_added;
2062 bytes = info->bytes;
2063 offset = info->offset;
2065 if (!ctl->op->use_bitmap(ctl, info))
2068 if (ctl->op == &free_space_op)
2069 block_group = ctl->private;
2072 * Since we link bitmaps right into the cluster we need to see if we
2073 * have a cluster here, and if so and it has our bitmap we need to add
2074 * the free space to that bitmap.
2076 if (block_group && !list_empty(&block_group->cluster_list)) {
2077 struct btrfs_free_cluster *cluster;
2078 struct rb_node *node;
2079 struct btrfs_free_space *entry;
2081 cluster = list_entry(block_group->cluster_list.next,
2082 struct btrfs_free_cluster,
2084 spin_lock(&cluster->lock);
2085 node = rb_first(&cluster->root);
2087 spin_unlock(&cluster->lock);
2088 goto no_cluster_bitmap;
2091 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2092 if (!entry->bitmap) {
2093 spin_unlock(&cluster->lock);
2094 goto no_cluster_bitmap;
2097 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2098 bytes_added = add_bytes_to_bitmap(ctl, entry,
2100 bytes -= bytes_added;
2101 offset += bytes_added;
2103 spin_unlock(&cluster->lock);
2111 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2118 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2119 bytes -= bytes_added;
2120 offset += bytes_added;
2130 if (info && info->bitmap) {
2131 add_new_bitmap(ctl, info, offset);
2136 spin_unlock(&ctl->tree_lock);
2138 /* no pre-allocated info, allocate a new one */
2140 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2143 spin_lock(&ctl->tree_lock);
2149 /* allocate the bitmap */
2150 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2152 spin_lock(&ctl->tree_lock);
2153 if (!info->bitmap) {
2163 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2165 kmem_cache_free(btrfs_free_space_cachep, info);
2171 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2172 struct btrfs_free_space *info, bool update_stat)
2174 struct btrfs_free_space *left_info = NULL;
2175 struct btrfs_free_space *right_info;
2176 bool merged = false;
2177 u64 offset = info->offset;
2178 u64 bytes = info->bytes;
2181 * first we want to see if there is free space adjacent to the range we
2182 * are adding, if there is remove that struct and add a new one to
2183 * cover the entire range
2185 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2186 if (right_info && rb_prev(&right_info->offset_index))
2187 left_info = rb_entry(rb_prev(&right_info->offset_index),
2188 struct btrfs_free_space, offset_index);
2189 else if (!right_info)
2190 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2192 if (right_info && !right_info->bitmap) {
2194 unlink_free_space(ctl, right_info);
2196 __unlink_free_space(ctl, right_info);
2197 info->bytes += right_info->bytes;
2198 kmem_cache_free(btrfs_free_space_cachep, right_info);
2202 if (left_info && !left_info->bitmap &&
2203 left_info->offset + left_info->bytes == offset) {
2205 unlink_free_space(ctl, left_info);
2207 __unlink_free_space(ctl, left_info);
2208 info->offset = left_info->offset;
2209 info->bytes += left_info->bytes;
2210 kmem_cache_free(btrfs_free_space_cachep, left_info);
2217 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2218 struct btrfs_free_space *info,
2221 struct btrfs_free_space *bitmap;
2224 const u64 end = info->offset + info->bytes;
2225 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2228 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2232 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2233 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2236 bytes = (j - i) * ctl->unit;
2237 info->bytes += bytes;
2240 bitmap_clear_bits(ctl, bitmap, end, bytes);
2242 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2245 free_bitmap(ctl, bitmap);
2250 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2251 struct btrfs_free_space *info,
2254 struct btrfs_free_space *bitmap;
2258 unsigned long prev_j;
2261 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2262 /* If we're on a boundary, try the previous logical bitmap. */
2263 if (bitmap_offset == info->offset) {
2264 if (info->offset == 0)
2266 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2269 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2273 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2275 prev_j = (unsigned long)-1;
2276 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2284 if (prev_j == (unsigned long)-1)
2285 bytes = (i + 1) * ctl->unit;
2287 bytes = (i - prev_j) * ctl->unit;
2289 info->offset -= bytes;
2290 info->bytes += bytes;
2293 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2295 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2298 free_bitmap(ctl, bitmap);
2304 * We prefer always to allocate from extent entries, both for clustered and
2305 * non-clustered allocation requests. So when attempting to add a new extent
2306 * entry, try to see if there's adjacent free space in bitmap entries, and if
2307 * there is, migrate that space from the bitmaps to the extent.
2308 * Like this we get better chances of satisfying space allocation requests
2309 * because we attempt to satisfy them based on a single cache entry, and never
2310 * on 2 or more entries - even if the entries represent a contiguous free space
2311 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2314 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2315 struct btrfs_free_space *info,
2319 * Only work with disconnected entries, as we can change their offset,
2320 * and must be extent entries.
2322 ASSERT(!info->bitmap);
2323 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2325 if (ctl->total_bitmaps > 0) {
2327 bool stole_front = false;
2329 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2330 if (ctl->total_bitmaps > 0)
2331 stole_front = steal_from_bitmap_to_front(ctl, info,
2334 if (stole_end || stole_front)
2335 try_merge_free_space(ctl, info, update_stat);
2339 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2340 struct btrfs_free_space_ctl *ctl,
2341 u64 offset, u64 bytes)
2343 struct btrfs_free_space *info;
2346 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2350 info->offset = offset;
2351 info->bytes = bytes;
2352 RB_CLEAR_NODE(&info->offset_index);
2354 spin_lock(&ctl->tree_lock);
2356 if (try_merge_free_space(ctl, info, true))
2360 * There was no extent directly to the left or right of this new
2361 * extent then we know we're going to have to allocate a new extent, so
2362 * before we do that see if we need to drop this into a bitmap
2364 ret = insert_into_bitmap(ctl, info);
2373 * Only steal free space from adjacent bitmaps if we're sure we're not
2374 * going to add the new free space to existing bitmap entries - because
2375 * that would mean unnecessary work that would be reverted. Therefore
2376 * attempt to steal space from bitmaps if we're adding an extent entry.
2378 steal_from_bitmap(ctl, info, true);
2380 ret = link_free_space(ctl, info);
2382 kmem_cache_free(btrfs_free_space_cachep, info);
2384 spin_unlock(&ctl->tree_lock);
2387 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2388 ASSERT(ret != -EEXIST);
2394 int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
2395 u64 bytenr, u64 size)
2397 return __btrfs_add_free_space(block_group->fs_info,
2398 block_group->free_space_ctl,
2402 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2403 u64 offset, u64 bytes)
2405 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2406 struct btrfs_free_space *info;
2408 bool re_search = false;
2410 spin_lock(&ctl->tree_lock);
2417 info = tree_search_offset(ctl, offset, 0, 0);
2420 * oops didn't find an extent that matched the space we wanted
2421 * to remove, look for a bitmap instead
2423 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2427 * If we found a partial bit of our free space in a
2428 * bitmap but then couldn't find the other part this may
2429 * be a problem, so WARN about it.
2437 if (!info->bitmap) {
2438 unlink_free_space(ctl, info);
2439 if (offset == info->offset) {
2440 u64 to_free = min(bytes, info->bytes);
2442 info->bytes -= to_free;
2443 info->offset += to_free;
2445 ret = link_free_space(ctl, info);
2448 kmem_cache_free(btrfs_free_space_cachep, info);
2455 u64 old_end = info->bytes + info->offset;
2457 info->bytes = offset - info->offset;
2458 ret = link_free_space(ctl, info);
2463 /* Not enough bytes in this entry to satisfy us */
2464 if (old_end < offset + bytes) {
2465 bytes -= old_end - offset;
2468 } else if (old_end == offset + bytes) {
2472 spin_unlock(&ctl->tree_lock);
2474 ret = btrfs_add_free_space(block_group, offset + bytes,
2475 old_end - (offset + bytes));
2481 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2482 if (ret == -EAGAIN) {
2487 spin_unlock(&ctl->tree_lock);
2492 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2495 struct btrfs_fs_info *fs_info = block_group->fs_info;
2496 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2497 struct btrfs_free_space *info;
2501 spin_lock(&ctl->tree_lock);
2502 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2503 info = rb_entry(n, struct btrfs_free_space, offset_index);
2504 if (info->bytes >= bytes && !block_group->ro)
2506 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2507 info->offset, info->bytes,
2508 (info->bitmap) ? "yes" : "no");
2510 spin_unlock(&ctl->tree_lock);
2511 btrfs_info(fs_info, "block group has cluster?: %s",
2512 list_empty(&block_group->cluster_list) ? "no" : "yes");
2514 "%d blocks of free space at or bigger than bytes is", count);
2517 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
2519 struct btrfs_fs_info *fs_info = block_group->fs_info;
2520 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2522 spin_lock_init(&ctl->tree_lock);
2523 ctl->unit = fs_info->sectorsize;
2524 ctl->start = block_group->key.objectid;
2525 ctl->private = block_group;
2526 ctl->op = &free_space_op;
2527 INIT_LIST_HEAD(&ctl->trimming_ranges);
2528 mutex_init(&ctl->cache_writeout_mutex);
2531 * we only want to have 32k of ram per block group for keeping
2532 * track of free space, and if we pass 1/2 of that we want to
2533 * start converting things over to using bitmaps
2535 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2539 * for a given cluster, put all of its extents back into the free
2540 * space cache. If the block group passed doesn't match the block group
2541 * pointed to by the cluster, someone else raced in and freed the
2542 * cluster already. In that case, we just return without changing anything
2545 __btrfs_return_cluster_to_free_space(
2546 struct btrfs_block_group_cache *block_group,
2547 struct btrfs_free_cluster *cluster)
2549 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2550 struct btrfs_free_space *entry;
2551 struct rb_node *node;
2553 spin_lock(&cluster->lock);
2554 if (cluster->block_group != block_group)
2557 cluster->block_group = NULL;
2558 cluster->window_start = 0;
2559 list_del_init(&cluster->block_group_list);
2561 node = rb_first(&cluster->root);
2565 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2566 node = rb_next(&entry->offset_index);
2567 rb_erase(&entry->offset_index, &cluster->root);
2568 RB_CLEAR_NODE(&entry->offset_index);
2570 bitmap = (entry->bitmap != NULL);
2572 try_merge_free_space(ctl, entry, false);
2573 steal_from_bitmap(ctl, entry, false);
2575 tree_insert_offset(&ctl->free_space_offset,
2576 entry->offset, &entry->offset_index, bitmap);
2578 cluster->root = RB_ROOT;
2581 spin_unlock(&cluster->lock);
2582 btrfs_put_block_group(block_group);
2586 static void __btrfs_remove_free_space_cache_locked(
2587 struct btrfs_free_space_ctl *ctl)
2589 struct btrfs_free_space *info;
2590 struct rb_node *node;
2592 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2593 info = rb_entry(node, struct btrfs_free_space, offset_index);
2594 if (!info->bitmap) {
2595 unlink_free_space(ctl, info);
2596 kmem_cache_free(btrfs_free_space_cachep, info);
2598 free_bitmap(ctl, info);
2601 cond_resched_lock(&ctl->tree_lock);
2605 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2607 spin_lock(&ctl->tree_lock);
2608 __btrfs_remove_free_space_cache_locked(ctl);
2609 spin_unlock(&ctl->tree_lock);
2612 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2614 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2615 struct btrfs_free_cluster *cluster;
2616 struct list_head *head;
2618 spin_lock(&ctl->tree_lock);
2619 while ((head = block_group->cluster_list.next) !=
2620 &block_group->cluster_list) {
2621 cluster = list_entry(head, struct btrfs_free_cluster,
2624 WARN_ON(cluster->block_group != block_group);
2625 __btrfs_return_cluster_to_free_space(block_group, cluster);
2627 cond_resched_lock(&ctl->tree_lock);
2629 __btrfs_remove_free_space_cache_locked(ctl);
2630 spin_unlock(&ctl->tree_lock);
2634 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2635 u64 offset, u64 bytes, u64 empty_size,
2636 u64 *max_extent_size)
2638 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2639 struct btrfs_free_space *entry = NULL;
2640 u64 bytes_search = bytes + empty_size;
2643 u64 align_gap_len = 0;
2645 spin_lock(&ctl->tree_lock);
2646 entry = find_free_space(ctl, &offset, &bytes_search,
2647 block_group->full_stripe_len, max_extent_size);
2652 if (entry->bitmap) {
2653 bitmap_clear_bits(ctl, entry, offset, bytes);
2655 free_bitmap(ctl, entry);
2657 unlink_free_space(ctl, entry);
2658 align_gap_len = offset - entry->offset;
2659 align_gap = entry->offset;
2661 entry->offset = offset + bytes;
2662 WARN_ON(entry->bytes < bytes + align_gap_len);
2664 entry->bytes -= bytes + align_gap_len;
2666 kmem_cache_free(btrfs_free_space_cachep, entry);
2668 link_free_space(ctl, entry);
2671 spin_unlock(&ctl->tree_lock);
2674 __btrfs_add_free_space(block_group->fs_info, ctl,
2675 align_gap, align_gap_len);
2680 * given a cluster, put all of its extents back into the free space
2681 * cache. If a block group is passed, this function will only free
2682 * a cluster that belongs to the passed block group.
2684 * Otherwise, it'll get a reference on the block group pointed to by the
2685 * cluster and remove the cluster from it.
2687 int btrfs_return_cluster_to_free_space(
2688 struct btrfs_block_group_cache *block_group,
2689 struct btrfs_free_cluster *cluster)
2691 struct btrfs_free_space_ctl *ctl;
2694 /* first, get a safe pointer to the block group */
2695 spin_lock(&cluster->lock);
2697 block_group = cluster->block_group;
2699 spin_unlock(&cluster->lock);
2702 } else if (cluster->block_group != block_group) {
2703 /* someone else has already freed it don't redo their work */
2704 spin_unlock(&cluster->lock);
2707 atomic_inc(&block_group->count);
2708 spin_unlock(&cluster->lock);
2710 ctl = block_group->free_space_ctl;
2712 /* now return any extents the cluster had on it */
2713 spin_lock(&ctl->tree_lock);
2714 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2715 spin_unlock(&ctl->tree_lock);
2717 /* finally drop our ref */
2718 btrfs_put_block_group(block_group);
2722 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2723 struct btrfs_free_cluster *cluster,
2724 struct btrfs_free_space *entry,
2725 u64 bytes, u64 min_start,
2726 u64 *max_extent_size)
2728 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2730 u64 search_start = cluster->window_start;
2731 u64 search_bytes = bytes;
2734 search_start = min_start;
2735 search_bytes = bytes;
2737 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2739 *max_extent_size = max(get_max_extent_size(entry),
2745 __bitmap_clear_bits(ctl, entry, ret, bytes);
2751 * given a cluster, try to allocate 'bytes' from it, returns 0
2752 * if it couldn't find anything suitably large, or a logical disk offset
2753 * if things worked out
2755 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2756 struct btrfs_free_cluster *cluster, u64 bytes,
2757 u64 min_start, u64 *max_extent_size)
2759 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2760 struct btrfs_free_space *entry = NULL;
2761 struct rb_node *node;
2764 spin_lock(&cluster->lock);
2765 if (bytes > cluster->max_size)
2768 if (cluster->block_group != block_group)
2771 node = rb_first(&cluster->root);
2775 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2777 if (entry->bytes < bytes)
2778 *max_extent_size = max(get_max_extent_size(entry),
2781 if (entry->bytes < bytes ||
2782 (!entry->bitmap && entry->offset < min_start)) {
2783 node = rb_next(&entry->offset_index);
2786 entry = rb_entry(node, struct btrfs_free_space,
2791 if (entry->bitmap) {
2792 ret = btrfs_alloc_from_bitmap(block_group,
2793 cluster, entry, bytes,
2794 cluster->window_start,
2797 node = rb_next(&entry->offset_index);
2800 entry = rb_entry(node, struct btrfs_free_space,
2804 cluster->window_start += bytes;
2806 ret = entry->offset;
2808 entry->offset += bytes;
2809 entry->bytes -= bytes;
2812 if (entry->bytes == 0)
2813 rb_erase(&entry->offset_index, &cluster->root);
2817 spin_unlock(&cluster->lock);
2822 spin_lock(&ctl->tree_lock);
2824 ctl->free_space -= bytes;
2825 if (entry->bytes == 0) {
2826 ctl->free_extents--;
2827 if (entry->bitmap) {
2828 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2830 ctl->total_bitmaps--;
2831 ctl->op->recalc_thresholds(ctl);
2833 kmem_cache_free(btrfs_free_space_cachep, entry);
2836 spin_unlock(&ctl->tree_lock);
2841 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2842 struct btrfs_free_space *entry,
2843 struct btrfs_free_cluster *cluster,
2844 u64 offset, u64 bytes,
2845 u64 cont1_bytes, u64 min_bytes)
2847 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2848 unsigned long next_zero;
2850 unsigned long want_bits;
2851 unsigned long min_bits;
2852 unsigned long found_bits;
2853 unsigned long max_bits = 0;
2854 unsigned long start = 0;
2855 unsigned long total_found = 0;
2858 i = offset_to_bit(entry->offset, ctl->unit,
2859 max_t(u64, offset, entry->offset));
2860 want_bits = bytes_to_bits(bytes, ctl->unit);
2861 min_bits = bytes_to_bits(min_bytes, ctl->unit);
2864 * Don't bother looking for a cluster in this bitmap if it's heavily
2867 if (entry->max_extent_size &&
2868 entry->max_extent_size < cont1_bytes)
2872 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
2873 next_zero = find_next_zero_bit(entry->bitmap,
2874 BITS_PER_BITMAP, i);
2875 if (next_zero - i >= min_bits) {
2876 found_bits = next_zero - i;
2877 if (found_bits > max_bits)
2878 max_bits = found_bits;
2881 if (next_zero - i > max_bits)
2882 max_bits = next_zero - i;
2887 entry->max_extent_size = (u64)max_bits * ctl->unit;
2893 cluster->max_size = 0;
2896 total_found += found_bits;
2898 if (cluster->max_size < found_bits * ctl->unit)
2899 cluster->max_size = found_bits * ctl->unit;
2901 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2906 cluster->window_start = start * ctl->unit + entry->offset;
2907 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2908 ret = tree_insert_offset(&cluster->root, entry->offset,
2909 &entry->offset_index, 1);
2910 ASSERT(!ret); /* -EEXIST; Logic error */
2912 trace_btrfs_setup_cluster(block_group, cluster,
2913 total_found * ctl->unit, 1);
2918 * This searches the block group for just extents to fill the cluster with.
2919 * Try to find a cluster with at least bytes total bytes, at least one
2920 * extent of cont1_bytes, and other clusters of at least min_bytes.
2923 setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2924 struct btrfs_free_cluster *cluster,
2925 struct list_head *bitmaps, u64 offset, u64 bytes,
2926 u64 cont1_bytes, u64 min_bytes)
2928 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2929 struct btrfs_free_space *first = NULL;
2930 struct btrfs_free_space *entry = NULL;
2931 struct btrfs_free_space *last;
2932 struct rb_node *node;
2937 entry = tree_search_offset(ctl, offset, 0, 1);
2942 * We don't want bitmaps, so just move along until we find a normal
2945 while (entry->bitmap || entry->bytes < min_bytes) {
2946 if (entry->bitmap && list_empty(&entry->list))
2947 list_add_tail(&entry->list, bitmaps);
2948 node = rb_next(&entry->offset_index);
2951 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2954 window_free = entry->bytes;
2955 max_extent = entry->bytes;
2959 for (node = rb_next(&entry->offset_index); node;
2960 node = rb_next(&entry->offset_index)) {
2961 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2963 if (entry->bitmap) {
2964 if (list_empty(&entry->list))
2965 list_add_tail(&entry->list, bitmaps);
2969 if (entry->bytes < min_bytes)
2973 window_free += entry->bytes;
2974 if (entry->bytes > max_extent)
2975 max_extent = entry->bytes;
2978 if (window_free < bytes || max_extent < cont1_bytes)
2981 cluster->window_start = first->offset;
2983 node = &first->offset_index;
2986 * now we've found our entries, pull them out of the free space
2987 * cache and put them into the cluster rbtree
2992 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2993 node = rb_next(&entry->offset_index);
2994 if (entry->bitmap || entry->bytes < min_bytes)
2997 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2998 ret = tree_insert_offset(&cluster->root, entry->offset,
2999 &entry->offset_index, 0);
3000 total_size += entry->bytes;
3001 ASSERT(!ret); /* -EEXIST; Logic error */
3002 } while (node && entry != last);
3004 cluster->max_size = max_extent;
3005 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
3010 * This specifically looks for bitmaps that may work in the cluster, we assume
3011 * that we have already failed to find extents that will work.
3014 setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
3015 struct btrfs_free_cluster *cluster,
3016 struct list_head *bitmaps, u64 offset, u64 bytes,
3017 u64 cont1_bytes, u64 min_bytes)
3019 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3020 struct btrfs_free_space *entry = NULL;
3022 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
3024 if (ctl->total_bitmaps == 0)
3028 * The bitmap that covers offset won't be in the list unless offset
3029 * is just its start offset.
3031 if (!list_empty(bitmaps))
3032 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3034 if (!entry || entry->offset != bitmap_offset) {
3035 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3036 if (entry && list_empty(&entry->list))
3037 list_add(&entry->list, bitmaps);
3040 list_for_each_entry(entry, bitmaps, list) {
3041 if (entry->bytes < bytes)
3043 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
3044 bytes, cont1_bytes, min_bytes);
3050 * The bitmaps list has all the bitmaps that record free space
3051 * starting after offset, so no more search is required.
3057 * here we try to find a cluster of blocks in a block group. The goal
3058 * is to find at least bytes+empty_size.
3059 * We might not find them all in one contiguous area.
3061 * returns zero and sets up cluster if things worked out, otherwise
3062 * it returns -enospc
3064 int btrfs_find_space_cluster(struct btrfs_block_group_cache *block_group,
3065 struct btrfs_free_cluster *cluster,
3066 u64 offset, u64 bytes, u64 empty_size)
3068 struct btrfs_fs_info *fs_info = block_group->fs_info;
3069 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3070 struct btrfs_free_space *entry, *tmp;
3077 * Choose the minimum extent size we'll require for this
3078 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3079 * For metadata, allow allocates with smaller extents. For
3080 * data, keep it dense.
3082 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3083 cont1_bytes = min_bytes = bytes + empty_size;
3084 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3085 cont1_bytes = bytes;
3086 min_bytes = fs_info->sectorsize;
3088 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3089 min_bytes = fs_info->sectorsize;
3092 spin_lock(&ctl->tree_lock);
3095 * If we know we don't have enough space to make a cluster don't even
3096 * bother doing all the work to try and find one.
3098 if (ctl->free_space < bytes) {
3099 spin_unlock(&ctl->tree_lock);
3103 spin_lock(&cluster->lock);
3105 /* someone already found a cluster, hooray */
3106 if (cluster->block_group) {
3111 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3114 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3116 cont1_bytes, min_bytes);
3118 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3119 offset, bytes + empty_size,
3120 cont1_bytes, min_bytes);
3122 /* Clear our temporary list */
3123 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3124 list_del_init(&entry->list);
3127 atomic_inc(&block_group->count);
3128 list_add_tail(&cluster->block_group_list,
3129 &block_group->cluster_list);
3130 cluster->block_group = block_group;
3132 trace_btrfs_failed_cluster_setup(block_group);
3135 spin_unlock(&cluster->lock);
3136 spin_unlock(&ctl->tree_lock);
3142 * simple code to zero out a cluster
3144 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3146 spin_lock_init(&cluster->lock);
3147 spin_lock_init(&cluster->refill_lock);
3148 cluster->root = RB_ROOT;
3149 cluster->max_size = 0;
3150 cluster->fragmented = false;
3151 INIT_LIST_HEAD(&cluster->block_group_list);
3152 cluster->block_group = NULL;
3155 static int do_trimming(struct btrfs_block_group_cache *block_group,
3156 u64 *total_trimmed, u64 start, u64 bytes,
3157 u64 reserved_start, u64 reserved_bytes,
3158 struct btrfs_trim_range *trim_entry)
3160 struct btrfs_space_info *space_info = block_group->space_info;
3161 struct btrfs_fs_info *fs_info = block_group->fs_info;
3162 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3167 spin_lock(&space_info->lock);
3168 spin_lock(&block_group->lock);
3169 if (!block_group->ro) {
3170 block_group->reserved += reserved_bytes;
3171 space_info->bytes_reserved += reserved_bytes;
3174 spin_unlock(&block_group->lock);
3175 spin_unlock(&space_info->lock);
3177 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3179 *total_trimmed += trimmed;
3181 mutex_lock(&ctl->cache_writeout_mutex);
3182 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
3183 list_del(&trim_entry->list);
3184 mutex_unlock(&ctl->cache_writeout_mutex);
3187 spin_lock(&space_info->lock);
3188 spin_lock(&block_group->lock);
3189 if (block_group->ro)
3190 space_info->bytes_readonly += reserved_bytes;
3191 block_group->reserved -= reserved_bytes;
3192 space_info->bytes_reserved -= reserved_bytes;
3193 spin_unlock(&block_group->lock);
3194 spin_unlock(&space_info->lock);
3200 static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3201 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3203 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3204 struct btrfs_free_space *entry;
3205 struct rb_node *node;
3211 while (start < end) {
3212 struct btrfs_trim_range trim_entry;
3214 mutex_lock(&ctl->cache_writeout_mutex);
3215 spin_lock(&ctl->tree_lock);
3217 if (ctl->free_space < minlen) {
3218 spin_unlock(&ctl->tree_lock);
3219 mutex_unlock(&ctl->cache_writeout_mutex);
3223 entry = tree_search_offset(ctl, start, 0, 1);
3225 spin_unlock(&ctl->tree_lock);
3226 mutex_unlock(&ctl->cache_writeout_mutex);
3231 while (entry->bitmap) {
3232 node = rb_next(&entry->offset_index);
3234 spin_unlock(&ctl->tree_lock);
3235 mutex_unlock(&ctl->cache_writeout_mutex);
3238 entry = rb_entry(node, struct btrfs_free_space,
3242 if (entry->offset >= end) {
3243 spin_unlock(&ctl->tree_lock);
3244 mutex_unlock(&ctl->cache_writeout_mutex);
3248 extent_start = entry->offset;
3249 extent_bytes = entry->bytes;
3250 start = max(start, extent_start);
3251 bytes = min(extent_start + extent_bytes, end) - start;
3252 if (bytes < minlen) {
3253 spin_unlock(&ctl->tree_lock);
3254 mutex_unlock(&ctl->cache_writeout_mutex);
3258 unlink_free_space(ctl, entry);
3259 kmem_cache_free(btrfs_free_space_cachep, entry);
3261 spin_unlock(&ctl->tree_lock);
3262 trim_entry.start = extent_start;
3263 trim_entry.bytes = extent_bytes;
3264 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3265 mutex_unlock(&ctl->cache_writeout_mutex);
3267 ret = do_trimming(block_group, total_trimmed, start, bytes,
3268 extent_start, extent_bytes, &trim_entry);
3274 if (fatal_signal_pending(current)) {
3285 static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3286 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3288 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3289 struct btrfs_free_space *entry;
3293 u64 offset = offset_to_bitmap(ctl, start);
3295 while (offset < end) {
3296 bool next_bitmap = false;
3297 struct btrfs_trim_range trim_entry;
3299 mutex_lock(&ctl->cache_writeout_mutex);
3300 spin_lock(&ctl->tree_lock);
3302 if (ctl->free_space < minlen) {
3303 spin_unlock(&ctl->tree_lock);
3304 mutex_unlock(&ctl->cache_writeout_mutex);
3308 entry = tree_search_offset(ctl, offset, 1, 0);
3310 spin_unlock(&ctl->tree_lock);
3311 mutex_unlock(&ctl->cache_writeout_mutex);
3317 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3318 if (ret2 || start >= end) {
3319 spin_unlock(&ctl->tree_lock);
3320 mutex_unlock(&ctl->cache_writeout_mutex);
3325 bytes = min(bytes, end - start);
3326 if (bytes < minlen) {
3327 spin_unlock(&ctl->tree_lock);
3328 mutex_unlock(&ctl->cache_writeout_mutex);
3332 bitmap_clear_bits(ctl, entry, start, bytes);
3333 if (entry->bytes == 0)
3334 free_bitmap(ctl, entry);
3336 spin_unlock(&ctl->tree_lock);
3337 trim_entry.start = start;
3338 trim_entry.bytes = bytes;
3339 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3340 mutex_unlock(&ctl->cache_writeout_mutex);
3342 ret = do_trimming(block_group, total_trimmed, start, bytes,
3343 start, bytes, &trim_entry);
3348 offset += BITS_PER_BITMAP * ctl->unit;
3351 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3352 offset += BITS_PER_BITMAP * ctl->unit;
3355 if (fatal_signal_pending(current)) {
3366 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
3368 atomic_inc(&cache->trimming);
3371 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3373 struct btrfs_fs_info *fs_info = block_group->fs_info;
3374 struct extent_map_tree *em_tree;
3375 struct extent_map *em;
3378 spin_lock(&block_group->lock);
3379 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3380 block_group->removed);
3381 spin_unlock(&block_group->lock);
3384 mutex_lock(&fs_info->chunk_mutex);
3385 em_tree = &fs_info->mapping_tree;
3386 write_lock(&em_tree->lock);
3387 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3389 BUG_ON(!em); /* logic error, can't happen */
3390 remove_extent_mapping(em_tree, em);
3391 write_unlock(&em_tree->lock);
3392 mutex_unlock(&fs_info->chunk_mutex);
3394 /* once for us and once for the tree */
3395 free_extent_map(em);
3396 free_extent_map(em);
3399 * We've left one free space entry and other tasks trimming
3400 * this block group have left 1 entry each one. Free them.
3402 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3406 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3407 u64 *trimmed, u64 start, u64 end, u64 minlen)
3413 spin_lock(&block_group->lock);
3414 if (block_group->removed) {
3415 spin_unlock(&block_group->lock);
3418 btrfs_get_block_group_trimming(block_group);
3419 spin_unlock(&block_group->lock);
3421 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3425 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3427 btrfs_put_block_group_trimming(block_group);
3432 * Find the left-most item in the cache tree, and then return the
3433 * smallest inode number in the item.
3435 * Note: the returned inode number may not be the smallest one in
3436 * the tree, if the left-most item is a bitmap.
3438 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3440 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3441 struct btrfs_free_space *entry = NULL;
3444 spin_lock(&ctl->tree_lock);
3446 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3449 entry = rb_entry(rb_first(&ctl->free_space_offset),
3450 struct btrfs_free_space, offset_index);
3452 if (!entry->bitmap) {
3453 ino = entry->offset;
3455 unlink_free_space(ctl, entry);
3459 kmem_cache_free(btrfs_free_space_cachep, entry);
3461 link_free_space(ctl, entry);
3467 ret = search_bitmap(ctl, entry, &offset, &count, true);
3468 /* Logic error; Should be empty if it can't find anything */
3472 bitmap_clear_bits(ctl, entry, offset, 1);
3473 if (entry->bytes == 0)
3474 free_bitmap(ctl, entry);
3477 spin_unlock(&ctl->tree_lock);
3482 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3483 struct btrfs_path *path)
3485 struct inode *inode = NULL;
3487 spin_lock(&root->ino_cache_lock);
3488 if (root->ino_cache_inode)
3489 inode = igrab(root->ino_cache_inode);
3490 spin_unlock(&root->ino_cache_lock);
3494 inode = __lookup_free_space_inode(root, path, 0);
3498 spin_lock(&root->ino_cache_lock);
3499 if (!btrfs_fs_closing(root->fs_info))
3500 root->ino_cache_inode = igrab(inode);
3501 spin_unlock(&root->ino_cache_lock);
3506 int create_free_ino_inode(struct btrfs_root *root,
3507 struct btrfs_trans_handle *trans,
3508 struct btrfs_path *path)
3510 return __create_free_space_inode(root, trans, path,
3511 BTRFS_FREE_INO_OBJECTID, 0);
3514 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3516 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3517 struct btrfs_path *path;
3518 struct inode *inode;
3520 u64 root_gen = btrfs_root_generation(&root->root_item);
3522 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3526 * If we're unmounting then just return, since this does a search on the
3527 * normal root and not the commit root and we could deadlock.
3529 if (btrfs_fs_closing(fs_info))
3532 path = btrfs_alloc_path();
3536 inode = lookup_free_ino_inode(root, path);
3540 if (root_gen != BTRFS_I(inode)->generation)
3543 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3547 "failed to load free ino cache for root %llu",
3548 root->root_key.objectid);
3552 btrfs_free_path(path);
3556 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3557 struct btrfs_trans_handle *trans,
3558 struct btrfs_path *path,
3559 struct inode *inode)
3561 struct btrfs_fs_info *fs_info = root->fs_info;
3562 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3564 struct btrfs_io_ctl io_ctl;
3565 bool release_metadata = true;
3567 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3570 memset(&io_ctl, 0, sizeof(io_ctl));
3571 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3574 * At this point writepages() didn't error out, so our metadata
3575 * reservation is released when the writeback finishes, at
3576 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3577 * with or without an error.
3579 release_metadata = false;
3580 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3584 if (release_metadata)
3585 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3586 inode->i_size, true);
3589 "failed to write free ino cache for root %llu",
3590 root->root_key.objectid);
3597 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3599 * Use this if you need to make a bitmap or extent entry specifically, it
3600 * doesn't do any of the merging that add_free_space does, this acts a lot like
3601 * how the free space cache loading stuff works, so you can get really weird
3604 int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3605 u64 offset, u64 bytes, bool bitmap)
3607 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3608 struct btrfs_free_space *info = NULL, *bitmap_info;
3615 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3621 spin_lock(&ctl->tree_lock);
3622 info->offset = offset;
3623 info->bytes = bytes;
3624 info->max_extent_size = 0;
3625 ret = link_free_space(ctl, info);
3626 spin_unlock(&ctl->tree_lock);
3628 kmem_cache_free(btrfs_free_space_cachep, info);
3633 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
3635 kmem_cache_free(btrfs_free_space_cachep, info);
3640 spin_lock(&ctl->tree_lock);
3641 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3646 add_new_bitmap(ctl, info, offset);
3651 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3653 bytes -= bytes_added;
3654 offset += bytes_added;
3655 spin_unlock(&ctl->tree_lock);
3661 kmem_cache_free(btrfs_free_space_cachep, info);
3663 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
3668 * Checks to see if the given range is in the free space cache. This is really
3669 * just used to check the absence of space, so if there is free space in the
3670 * range at all we will return 1.
3672 int test_check_exists(struct btrfs_block_group_cache *cache,
3673 u64 offset, u64 bytes)
3675 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3676 struct btrfs_free_space *info;
3679 spin_lock(&ctl->tree_lock);
3680 info = tree_search_offset(ctl, offset, 0, 0);
3682 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3690 u64 bit_off, bit_bytes;
3692 struct btrfs_free_space *tmp;
3695 bit_bytes = ctl->unit;
3696 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
3698 if (bit_off == offset) {
3701 } else if (bit_off > offset &&
3702 offset + bytes > bit_off) {
3708 n = rb_prev(&info->offset_index);
3710 tmp = rb_entry(n, struct btrfs_free_space,
3712 if (tmp->offset + tmp->bytes < offset)
3714 if (offset + bytes < tmp->offset) {
3715 n = rb_prev(&tmp->offset_index);
3722 n = rb_next(&info->offset_index);
3724 tmp = rb_entry(n, struct btrfs_free_space,
3726 if (offset + bytes < tmp->offset)
3728 if (tmp->offset + tmp->bytes < offset) {
3729 n = rb_next(&tmp->offset_index);
3740 if (info->offset == offset) {
3745 if (offset > info->offset && offset < info->offset + info->bytes)
3748 spin_unlock(&ctl->tree_lock);
3751 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */