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, 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(struct btrfs_block_group *block_group,
95 struct btrfs_path *path)
97 struct btrfs_fs_info *fs_info = block_group->fs_info;
98 struct inode *inode = NULL;
99 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
101 spin_lock(&block_group->lock);
102 if (block_group->inode)
103 inode = igrab(block_group->inode);
104 spin_unlock(&block_group->lock);
108 inode = __lookup_free_space_inode(fs_info->tree_root, path,
113 spin_lock(&block_group->lock);
114 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
115 btrfs_info(fs_info, "Old style space inode found, converting.");
116 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
117 BTRFS_INODE_NODATACOW;
118 block_group->disk_cache_state = BTRFS_DC_CLEAR;
121 if (!block_group->iref) {
122 block_group->inode = igrab(inode);
123 block_group->iref = 1;
125 spin_unlock(&block_group->lock);
130 static int __create_free_space_inode(struct btrfs_root *root,
131 struct btrfs_trans_handle *trans,
132 struct btrfs_path *path,
135 struct btrfs_key key;
136 struct btrfs_disk_key disk_key;
137 struct btrfs_free_space_header *header;
138 struct btrfs_inode_item *inode_item;
139 struct extent_buffer *leaf;
140 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
143 ret = btrfs_insert_empty_inode(trans, root, path, ino);
147 /* We inline crc's for the free disk space cache */
148 if (ino != BTRFS_FREE_INO_OBJECTID)
149 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
151 leaf = path->nodes[0];
152 inode_item = btrfs_item_ptr(leaf, path->slots[0],
153 struct btrfs_inode_item);
154 btrfs_item_key(leaf, &disk_key, path->slots[0]);
155 memzero_extent_buffer(leaf, (unsigned long)inode_item,
156 sizeof(*inode_item));
157 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
158 btrfs_set_inode_size(leaf, inode_item, 0);
159 btrfs_set_inode_nbytes(leaf, inode_item, 0);
160 btrfs_set_inode_uid(leaf, inode_item, 0);
161 btrfs_set_inode_gid(leaf, inode_item, 0);
162 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
163 btrfs_set_inode_flags(leaf, inode_item, flags);
164 btrfs_set_inode_nlink(leaf, inode_item, 1);
165 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
166 btrfs_set_inode_block_group(leaf, inode_item, offset);
167 btrfs_mark_buffer_dirty(leaf);
168 btrfs_release_path(path);
170 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
173 ret = btrfs_insert_empty_item(trans, root, path, &key,
174 sizeof(struct btrfs_free_space_header));
176 btrfs_release_path(path);
180 leaf = path->nodes[0];
181 header = btrfs_item_ptr(leaf, path->slots[0],
182 struct btrfs_free_space_header);
183 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
184 btrfs_set_free_space_key(leaf, header, &disk_key);
185 btrfs_mark_buffer_dirty(leaf);
186 btrfs_release_path(path);
191 int create_free_space_inode(struct btrfs_trans_handle *trans,
192 struct btrfs_block_group *block_group,
193 struct btrfs_path *path)
198 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
202 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
203 ino, block_group->start);
206 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
207 struct btrfs_block_rsv *rsv)
212 /* 1 for slack space, 1 for updating the inode */
213 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
214 btrfs_calc_metadata_size(fs_info, 1);
216 spin_lock(&rsv->lock);
217 if (rsv->reserved < needed_bytes)
221 spin_unlock(&rsv->lock);
225 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
226 struct btrfs_block_group *block_group,
229 struct btrfs_root *root = BTRFS_I(inode)->root;
234 struct btrfs_path *path = btrfs_alloc_path();
241 mutex_lock(&trans->transaction->cache_write_mutex);
242 if (!list_empty(&block_group->io_list)) {
243 list_del_init(&block_group->io_list);
245 btrfs_wait_cache_io(trans, block_group, path);
246 btrfs_put_block_group(block_group);
250 * now that we've truncated the cache away, its no longer
253 spin_lock(&block_group->lock);
254 block_group->disk_cache_state = BTRFS_DC_CLEAR;
255 spin_unlock(&block_group->lock);
256 btrfs_free_path(path);
259 btrfs_i_size_write(BTRFS_I(inode), 0);
260 truncate_pagecache(inode, 0);
263 * We skip the throttling logic for free space cache inodes, so we don't
264 * need to check for -EAGAIN.
266 ret = btrfs_truncate_inode_items(trans, root, inode,
267 0, BTRFS_EXTENT_DATA_KEY);
271 ret = btrfs_update_inode(trans, root, inode);
275 mutex_unlock(&trans->transaction->cache_write_mutex);
277 btrfs_abort_transaction(trans, ret);
282 static void readahead_cache(struct inode *inode)
284 struct file_ra_state *ra;
285 unsigned long last_index;
287 ra = kzalloc(sizeof(*ra), GFP_NOFS);
291 file_ra_state_init(ra, inode->i_mapping);
292 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
294 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
299 static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
305 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
307 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
310 /* Make sure we can fit our crcs and generation into the first page */
311 if (write && check_crcs &&
312 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
315 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
317 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
321 io_ctl->num_pages = num_pages;
322 io_ctl->fs_info = btrfs_sb(inode->i_sb);
323 io_ctl->check_crcs = check_crcs;
324 io_ctl->inode = inode;
328 ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
330 static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
332 kfree(io_ctl->pages);
333 io_ctl->pages = NULL;
336 static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
344 static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
346 ASSERT(io_ctl->index < io_ctl->num_pages);
347 io_ctl->page = io_ctl->pages[io_ctl->index++];
348 io_ctl->cur = page_address(io_ctl->page);
349 io_ctl->orig = io_ctl->cur;
350 io_ctl->size = PAGE_SIZE;
352 clear_page(io_ctl->cur);
355 static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
359 io_ctl_unmap_page(io_ctl);
361 for (i = 0; i < io_ctl->num_pages; i++) {
362 if (io_ctl->pages[i]) {
363 ClearPageChecked(io_ctl->pages[i]);
364 unlock_page(io_ctl->pages[i]);
365 put_page(io_ctl->pages[i]);
370 static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
374 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
377 for (i = 0; i < io_ctl->num_pages; i++) {
378 page = find_or_create_page(inode->i_mapping, i, mask);
380 io_ctl_drop_pages(io_ctl);
383 io_ctl->pages[i] = page;
384 if (uptodate && !PageUptodate(page)) {
385 btrfs_readpage(NULL, page);
387 if (page->mapping != inode->i_mapping) {
388 btrfs_err(BTRFS_I(inode)->root->fs_info,
389 "free space cache page truncated");
390 io_ctl_drop_pages(io_ctl);
393 if (!PageUptodate(page)) {
394 btrfs_err(BTRFS_I(inode)->root->fs_info,
395 "error reading free space cache");
396 io_ctl_drop_pages(io_ctl);
402 for (i = 0; i < io_ctl->num_pages; i++) {
403 clear_page_dirty_for_io(io_ctl->pages[i]);
404 set_page_extent_mapped(io_ctl->pages[i]);
410 static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
414 io_ctl_map_page(io_ctl, 1);
417 * Skip the csum areas. If we don't check crcs then we just have a
418 * 64bit chunk at the front of the first page.
420 if (io_ctl->check_crcs) {
421 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
422 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
424 io_ctl->cur += sizeof(u64);
425 io_ctl->size -= sizeof(u64) * 2;
429 *val = cpu_to_le64(generation);
430 io_ctl->cur += sizeof(u64);
433 static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
438 * Skip the crc area. If we don't check crcs then we just have a 64bit
439 * chunk at the front of the first page.
441 if (io_ctl->check_crcs) {
442 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
443 io_ctl->size -= sizeof(u64) +
444 (sizeof(u32) * io_ctl->num_pages);
446 io_ctl->cur += sizeof(u64);
447 io_ctl->size -= sizeof(u64) * 2;
451 if (le64_to_cpu(*gen) != generation) {
452 btrfs_err_rl(io_ctl->fs_info,
453 "space cache generation (%llu) does not match inode (%llu)",
455 io_ctl_unmap_page(io_ctl);
458 io_ctl->cur += sizeof(u64);
462 static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
468 if (!io_ctl->check_crcs) {
469 io_ctl_unmap_page(io_ctl);
474 offset = sizeof(u32) * io_ctl->num_pages;
476 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
477 btrfs_crc32c_final(crc, (u8 *)&crc);
478 io_ctl_unmap_page(io_ctl);
479 tmp = page_address(io_ctl->pages[0]);
484 static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
490 if (!io_ctl->check_crcs) {
491 io_ctl_map_page(io_ctl, 0);
496 offset = sizeof(u32) * io_ctl->num_pages;
498 tmp = page_address(io_ctl->pages[0]);
502 io_ctl_map_page(io_ctl, 0);
503 crc = btrfs_crc32c(crc, io_ctl->orig + offset, PAGE_SIZE - offset);
504 btrfs_crc32c_final(crc, (u8 *)&crc);
506 btrfs_err_rl(io_ctl->fs_info,
507 "csum mismatch on free space cache");
508 io_ctl_unmap_page(io_ctl);
515 static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
518 struct btrfs_free_space_entry *entry;
524 entry->offset = cpu_to_le64(offset);
525 entry->bytes = cpu_to_le64(bytes);
526 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
527 BTRFS_FREE_SPACE_EXTENT;
528 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
529 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
531 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
534 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
536 /* No more pages to map */
537 if (io_ctl->index >= io_ctl->num_pages)
540 /* map the next page */
541 io_ctl_map_page(io_ctl, 1);
545 static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
551 * If we aren't at the start of the current page, unmap this one and
552 * map the next one if there is any left.
554 if (io_ctl->cur != io_ctl->orig) {
555 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
556 if (io_ctl->index >= io_ctl->num_pages)
558 io_ctl_map_page(io_ctl, 0);
561 copy_page(io_ctl->cur, bitmap);
562 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
563 if (io_ctl->index < io_ctl->num_pages)
564 io_ctl_map_page(io_ctl, 0);
568 static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
571 * If we're not on the boundary we know we've modified the page and we
572 * need to crc the page.
574 if (io_ctl->cur != io_ctl->orig)
575 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
577 io_ctl_unmap_page(io_ctl);
579 while (io_ctl->index < io_ctl->num_pages) {
580 io_ctl_map_page(io_ctl, 1);
581 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
585 static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
586 struct btrfs_free_space *entry, u8 *type)
588 struct btrfs_free_space_entry *e;
592 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
598 entry->offset = le64_to_cpu(e->offset);
599 entry->bytes = le64_to_cpu(e->bytes);
601 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
602 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
604 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
607 io_ctl_unmap_page(io_ctl);
612 static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
613 struct btrfs_free_space *entry)
617 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
621 copy_page(entry->bitmap, io_ctl->cur);
622 io_ctl_unmap_page(io_ctl);
628 * Since we attach pinned extents after the fact we can have contiguous sections
629 * of free space that are split up in entries. This poses a problem with the
630 * tree logging stuff since it could have allocated across what appears to be 2
631 * entries since we would have merged the entries when adding the pinned extents
632 * back to the free space cache. So run through the space cache that we just
633 * loaded and merge contiguous entries. This will make the log replay stuff not
634 * blow up and it will make for nicer allocator behavior.
636 static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
638 struct btrfs_free_space *e, *prev = NULL;
642 spin_lock(&ctl->tree_lock);
643 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
644 e = rb_entry(n, struct btrfs_free_space, offset_index);
647 if (e->bitmap || prev->bitmap)
649 if (prev->offset + prev->bytes == e->offset) {
650 unlink_free_space(ctl, prev);
651 unlink_free_space(ctl, e);
652 prev->bytes += e->bytes;
653 kmem_cache_free(btrfs_free_space_cachep, e);
654 link_free_space(ctl, prev);
656 spin_unlock(&ctl->tree_lock);
662 spin_unlock(&ctl->tree_lock);
665 static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
666 struct btrfs_free_space_ctl *ctl,
667 struct btrfs_path *path, u64 offset)
669 struct btrfs_fs_info *fs_info = root->fs_info;
670 struct btrfs_free_space_header *header;
671 struct extent_buffer *leaf;
672 struct btrfs_io_ctl io_ctl;
673 struct btrfs_key key;
674 struct btrfs_free_space *e, *n;
682 /* Nothing in the space cache, goodbye */
683 if (!i_size_read(inode))
686 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
690 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
694 btrfs_release_path(path);
700 leaf = path->nodes[0];
701 header = btrfs_item_ptr(leaf, path->slots[0],
702 struct btrfs_free_space_header);
703 num_entries = btrfs_free_space_entries(leaf, header);
704 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
705 generation = btrfs_free_space_generation(leaf, header);
706 btrfs_release_path(path);
708 if (!BTRFS_I(inode)->generation) {
710 "the free space cache file (%llu) is invalid, skip it",
715 if (BTRFS_I(inode)->generation != generation) {
717 "free space inode generation (%llu) did not match free space cache generation (%llu)",
718 BTRFS_I(inode)->generation, generation);
725 ret = io_ctl_init(&io_ctl, inode, 0);
729 readahead_cache(inode);
731 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
735 ret = io_ctl_check_crc(&io_ctl, 0);
739 ret = io_ctl_check_generation(&io_ctl, generation);
743 while (num_entries) {
744 e = kmem_cache_zalloc(btrfs_free_space_cachep,
749 ret = io_ctl_read_entry(&io_ctl, e, &type);
751 kmem_cache_free(btrfs_free_space_cachep, e);
756 kmem_cache_free(btrfs_free_space_cachep, e);
760 if (type == BTRFS_FREE_SPACE_EXTENT) {
761 spin_lock(&ctl->tree_lock);
762 ret = link_free_space(ctl, e);
763 spin_unlock(&ctl->tree_lock);
766 "Duplicate entries in free space cache, dumping");
767 kmem_cache_free(btrfs_free_space_cachep, e);
773 e->bitmap = kmem_cache_zalloc(
774 btrfs_free_space_bitmap_cachep, GFP_NOFS);
777 btrfs_free_space_cachep, e);
780 spin_lock(&ctl->tree_lock);
781 ret = link_free_space(ctl, e);
782 ctl->total_bitmaps++;
783 ctl->op->recalc_thresholds(ctl);
784 spin_unlock(&ctl->tree_lock);
787 "Duplicate entries in free space cache, dumping");
788 kmem_cache_free(btrfs_free_space_cachep, e);
791 list_add_tail(&e->list, &bitmaps);
797 io_ctl_unmap_page(&io_ctl);
800 * We add the bitmaps at the end of the entries in order that
801 * the bitmap entries are added to the cache.
803 list_for_each_entry_safe(e, n, &bitmaps, list) {
804 list_del_init(&e->list);
805 ret = io_ctl_read_bitmap(&io_ctl, e);
810 io_ctl_drop_pages(&io_ctl);
811 merge_space_tree(ctl);
814 io_ctl_free(&io_ctl);
817 io_ctl_drop_pages(&io_ctl);
818 __btrfs_remove_free_space_cache(ctl);
822 int load_free_space_cache(struct btrfs_block_group *block_group)
824 struct btrfs_fs_info *fs_info = block_group->fs_info;
825 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
827 struct btrfs_path *path;
830 u64 used = block_group->used;
833 * If this block group has been marked to be cleared for one reason or
834 * another then we can't trust the on disk cache, so just return.
836 spin_lock(&block_group->lock);
837 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
838 spin_unlock(&block_group->lock);
841 spin_unlock(&block_group->lock);
843 path = btrfs_alloc_path();
846 path->search_commit_root = 1;
847 path->skip_locking = 1;
850 * We must pass a path with search_commit_root set to btrfs_iget in
851 * order to avoid a deadlock when allocating extents for the tree root.
853 * When we are COWing an extent buffer from the tree root, when looking
854 * for a free extent, at extent-tree.c:find_free_extent(), we can find
855 * block group without its free space cache loaded. When we find one
856 * we must load its space cache which requires reading its free space
857 * cache's inode item from the root tree. If this inode item is located
858 * in the same leaf that we started COWing before, then we end up in
859 * deadlock on the extent buffer (trying to read lock it when we
860 * previously write locked it).
862 * It's safe to read the inode item using the commit root because
863 * block groups, once loaded, stay in memory forever (until they are
864 * removed) as well as their space caches once loaded. New block groups
865 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
866 * we will never try to read their inode item while the fs is mounted.
868 inode = lookup_free_space_inode(block_group, path);
870 btrfs_free_path(path);
874 /* We may have converted the inode and made the cache invalid. */
875 spin_lock(&block_group->lock);
876 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
877 spin_unlock(&block_group->lock);
878 btrfs_free_path(path);
881 spin_unlock(&block_group->lock);
883 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
884 path, block_group->start);
885 btrfs_free_path(path);
889 spin_lock(&ctl->tree_lock);
890 matched = (ctl->free_space == (block_group->length - used -
891 block_group->bytes_super));
892 spin_unlock(&ctl->tree_lock);
895 __btrfs_remove_free_space_cache(ctl);
897 "block group %llu has wrong amount of free space",
903 /* This cache is bogus, make sure it gets cleared */
904 spin_lock(&block_group->lock);
905 block_group->disk_cache_state = BTRFS_DC_CLEAR;
906 spin_unlock(&block_group->lock);
910 "failed to load free space cache for block group %llu, rebuilding it now",
918 static noinline_for_stack
919 int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
920 struct btrfs_free_space_ctl *ctl,
921 struct btrfs_block_group *block_group,
922 int *entries, int *bitmaps,
923 struct list_head *bitmap_list)
926 struct btrfs_free_cluster *cluster = NULL;
927 struct btrfs_free_cluster *cluster_locked = NULL;
928 struct rb_node *node = rb_first(&ctl->free_space_offset);
929 struct btrfs_trim_range *trim_entry;
931 /* Get the cluster for this block_group if it exists */
932 if (block_group && !list_empty(&block_group->cluster_list)) {
933 cluster = list_entry(block_group->cluster_list.next,
934 struct btrfs_free_cluster,
938 if (!node && cluster) {
939 cluster_locked = cluster;
940 spin_lock(&cluster_locked->lock);
941 node = rb_first(&cluster->root);
945 /* Write out the extent entries */
947 struct btrfs_free_space *e;
949 e = rb_entry(node, struct btrfs_free_space, offset_index);
952 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
958 list_add_tail(&e->list, bitmap_list);
961 node = rb_next(node);
962 if (!node && cluster) {
963 node = rb_first(&cluster->root);
964 cluster_locked = cluster;
965 spin_lock(&cluster_locked->lock);
969 if (cluster_locked) {
970 spin_unlock(&cluster_locked->lock);
971 cluster_locked = NULL;
975 * Make sure we don't miss any range that was removed from our rbtree
976 * because trimming is running. Otherwise after a umount+mount (or crash
977 * after committing the transaction) we would leak free space and get
978 * an inconsistent free space cache report from fsck.
980 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
981 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
982 trim_entry->bytes, NULL);
991 spin_unlock(&cluster_locked->lock);
995 static noinline_for_stack int
996 update_cache_item(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
999 struct btrfs_path *path, u64 offset,
1000 int entries, int bitmaps)
1002 struct btrfs_key key;
1003 struct btrfs_free_space_header *header;
1004 struct extent_buffer *leaf;
1007 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1008 key.offset = offset;
1011 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1013 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1014 EXTENT_DELALLOC, 0, 0, NULL);
1017 leaf = path->nodes[0];
1019 struct btrfs_key found_key;
1020 ASSERT(path->slots[0]);
1022 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1023 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1024 found_key.offset != offset) {
1025 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1026 inode->i_size - 1, EXTENT_DELALLOC, 0,
1028 btrfs_release_path(path);
1033 BTRFS_I(inode)->generation = trans->transid;
1034 header = btrfs_item_ptr(leaf, path->slots[0],
1035 struct btrfs_free_space_header);
1036 btrfs_set_free_space_entries(leaf, header, entries);
1037 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1038 btrfs_set_free_space_generation(leaf, header, trans->transid);
1039 btrfs_mark_buffer_dirty(leaf);
1040 btrfs_release_path(path);
1048 static noinline_for_stack int write_pinned_extent_entries(
1049 struct btrfs_block_group *block_group,
1050 struct btrfs_io_ctl *io_ctl,
1053 u64 start, extent_start, extent_end, len;
1054 struct extent_io_tree *unpin = NULL;
1061 * We want to add any pinned extents to our free space cache
1062 * so we don't leak the space
1064 * We shouldn't have switched the pinned extents yet so this is the
1067 unpin = block_group->fs_info->pinned_extents;
1069 start = block_group->start;
1071 while (start < block_group->start + block_group->length) {
1072 ret = find_first_extent_bit(unpin, start,
1073 &extent_start, &extent_end,
1074 EXTENT_DIRTY, NULL);
1078 /* This pinned extent is out of our range */
1079 if (extent_start >= block_group->start + block_group->length)
1082 extent_start = max(extent_start, start);
1083 extent_end = min(block_group->start + block_group->length,
1085 len = extent_end - extent_start;
1088 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1098 static noinline_for_stack int
1099 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
1101 struct btrfs_free_space *entry, *next;
1104 /* Write out the bitmaps */
1105 list_for_each_entry_safe(entry, next, bitmap_list, list) {
1106 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1109 list_del_init(&entry->list);
1115 static int flush_dirty_cache(struct inode *inode)
1119 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1121 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1122 EXTENT_DELALLOC, 0, 0, NULL);
1127 static void noinline_for_stack
1128 cleanup_bitmap_list(struct list_head *bitmap_list)
1130 struct btrfs_free_space *entry, *next;
1132 list_for_each_entry_safe(entry, next, bitmap_list, list)
1133 list_del_init(&entry->list);
1136 static void noinline_for_stack
1137 cleanup_write_cache_enospc(struct inode *inode,
1138 struct btrfs_io_ctl *io_ctl,
1139 struct extent_state **cached_state)
1141 io_ctl_drop_pages(io_ctl);
1142 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1143 i_size_read(inode) - 1, cached_state);
1146 static int __btrfs_wait_cache_io(struct btrfs_root *root,
1147 struct btrfs_trans_handle *trans,
1148 struct btrfs_block_group *block_group,
1149 struct btrfs_io_ctl *io_ctl,
1150 struct btrfs_path *path, u64 offset)
1153 struct inode *inode = io_ctl->inode;
1158 /* Flush the dirty pages in the cache file. */
1159 ret = flush_dirty_cache(inode);
1163 /* Update the cache item to tell everyone this cache file is valid. */
1164 ret = update_cache_item(trans, root, inode, path, offset,
1165 io_ctl->entries, io_ctl->bitmaps);
1167 io_ctl_free(io_ctl);
1169 invalidate_inode_pages2(inode->i_mapping);
1170 BTRFS_I(inode)->generation = 0;
1173 btrfs_err(root->fs_info,
1174 "failed to write free space cache for block group %llu",
1175 block_group->start);
1179 btrfs_update_inode(trans, root, inode);
1182 /* the dirty list is protected by the dirty_bgs_lock */
1183 spin_lock(&trans->transaction->dirty_bgs_lock);
1185 /* the disk_cache_state is protected by the block group lock */
1186 spin_lock(&block_group->lock);
1189 * only mark this as written if we didn't get put back on
1190 * the dirty list while waiting for IO. Otherwise our
1191 * cache state won't be right, and we won't get written again
1193 if (!ret && list_empty(&block_group->dirty_list))
1194 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1196 block_group->disk_cache_state = BTRFS_DC_ERROR;
1198 spin_unlock(&block_group->lock);
1199 spin_unlock(&trans->transaction->dirty_bgs_lock);
1200 io_ctl->inode = NULL;
1208 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1209 struct btrfs_trans_handle *trans,
1210 struct btrfs_io_ctl *io_ctl,
1211 struct btrfs_path *path)
1213 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1216 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1217 struct btrfs_block_group *block_group,
1218 struct btrfs_path *path)
1220 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1221 block_group, &block_group->io_ctl,
1222 path, block_group->start);
1226 * __btrfs_write_out_cache - write out cached info to an inode
1227 * @root - the root the inode belongs to
1228 * @ctl - the free space cache we are going to write out
1229 * @block_group - the block_group for this cache if it belongs to a block_group
1230 * @trans - the trans handle
1232 * This function writes out a free space cache struct to disk for quick recovery
1233 * on mount. This will return 0 if it was successful in writing the cache out,
1234 * or an errno if it was not.
1236 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1237 struct btrfs_free_space_ctl *ctl,
1238 struct btrfs_block_group *block_group,
1239 struct btrfs_io_ctl *io_ctl,
1240 struct btrfs_trans_handle *trans)
1242 struct extent_state *cached_state = NULL;
1243 LIST_HEAD(bitmap_list);
1249 if (!i_size_read(inode))
1252 WARN_ON(io_ctl->pages);
1253 ret = io_ctl_init(io_ctl, inode, 1);
1257 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1258 down_write(&block_group->data_rwsem);
1259 spin_lock(&block_group->lock);
1260 if (block_group->delalloc_bytes) {
1261 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1262 spin_unlock(&block_group->lock);
1263 up_write(&block_group->data_rwsem);
1264 BTRFS_I(inode)->generation = 0;
1269 spin_unlock(&block_group->lock);
1272 /* Lock all pages first so we can lock the extent safely. */
1273 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1277 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1280 io_ctl_set_generation(io_ctl, trans->transid);
1282 mutex_lock(&ctl->cache_writeout_mutex);
1283 /* Write out the extent entries in the free space cache */
1284 spin_lock(&ctl->tree_lock);
1285 ret = write_cache_extent_entries(io_ctl, ctl,
1286 block_group, &entries, &bitmaps,
1289 goto out_nospc_locked;
1292 * Some spaces that are freed in the current transaction are pinned,
1293 * they will be added into free space cache after the transaction is
1294 * committed, we shouldn't lose them.
1296 * If this changes while we are working we'll get added back to
1297 * the dirty list and redo it. No locking needed
1299 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
1301 goto out_nospc_locked;
1304 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1305 * locked while doing it because a concurrent trim can be manipulating
1306 * or freeing the bitmap.
1308 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1309 spin_unlock(&ctl->tree_lock);
1310 mutex_unlock(&ctl->cache_writeout_mutex);
1314 /* Zero out the rest of the pages just to make sure */
1315 io_ctl_zero_remaining_pages(io_ctl);
1317 /* Everything is written out, now we dirty the pages in the file. */
1318 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1319 i_size_read(inode), &cached_state);
1323 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1324 up_write(&block_group->data_rwsem);
1326 * Release the pages and unlock the extent, we will flush
1329 io_ctl_drop_pages(io_ctl);
1331 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1332 i_size_read(inode) - 1, &cached_state);
1335 * at this point the pages are under IO and we're happy,
1336 * The caller is responsible for waiting on them and updating the
1337 * the cache and the inode
1339 io_ctl->entries = entries;
1340 io_ctl->bitmaps = bitmaps;
1342 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1349 io_ctl->inode = NULL;
1350 io_ctl_free(io_ctl);
1352 invalidate_inode_pages2(inode->i_mapping);
1353 BTRFS_I(inode)->generation = 0;
1355 btrfs_update_inode(trans, root, inode);
1361 cleanup_bitmap_list(&bitmap_list);
1362 spin_unlock(&ctl->tree_lock);
1363 mutex_unlock(&ctl->cache_writeout_mutex);
1366 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1369 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1370 up_write(&block_group->data_rwsem);
1375 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
1376 struct btrfs_block_group *block_group,
1377 struct btrfs_path *path)
1379 struct btrfs_fs_info *fs_info = trans->fs_info;
1380 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1381 struct inode *inode;
1384 spin_lock(&block_group->lock);
1385 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1386 spin_unlock(&block_group->lock);
1389 spin_unlock(&block_group->lock);
1391 inode = lookup_free_space_inode(block_group, path);
1395 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1396 block_group, &block_group->io_ctl, trans);
1400 "failed to write free space cache for block group %llu",
1401 block_group->start);
1403 spin_lock(&block_group->lock);
1404 block_group->disk_cache_state = BTRFS_DC_ERROR;
1405 spin_unlock(&block_group->lock);
1407 block_group->io_ctl.inode = NULL;
1412 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1413 * to wait for IO and put the inode
1419 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1422 ASSERT(offset >= bitmap_start);
1423 offset -= bitmap_start;
1424 return (unsigned long)(div_u64(offset, unit));
1427 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1429 return (unsigned long)(div_u64(bytes, unit));
1432 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1436 u64 bytes_per_bitmap;
1438 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1439 bitmap_start = offset - ctl->start;
1440 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1441 bitmap_start *= bytes_per_bitmap;
1442 bitmap_start += ctl->start;
1444 return bitmap_start;
1447 static int tree_insert_offset(struct rb_root *root, u64 offset,
1448 struct rb_node *node, int bitmap)
1450 struct rb_node **p = &root->rb_node;
1451 struct rb_node *parent = NULL;
1452 struct btrfs_free_space *info;
1456 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1458 if (offset < info->offset) {
1460 } else if (offset > info->offset) {
1461 p = &(*p)->rb_right;
1464 * we could have a bitmap entry and an extent entry
1465 * share the same offset. If this is the case, we want
1466 * the extent entry to always be found first if we do a
1467 * linear search through the tree, since we want to have
1468 * the quickest allocation time, and allocating from an
1469 * extent is faster than allocating from a bitmap. So
1470 * if we're inserting a bitmap and we find an entry at
1471 * this offset, we want to go right, or after this entry
1472 * logically. If we are inserting an extent and we've
1473 * found a bitmap, we want to go left, or before
1481 p = &(*p)->rb_right;
1483 if (!info->bitmap) {
1492 rb_link_node(node, parent, p);
1493 rb_insert_color(node, root);
1499 * searches the tree for the given offset.
1501 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1502 * want a section that has at least bytes size and comes at or after the given
1505 static struct btrfs_free_space *
1506 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1507 u64 offset, int bitmap_only, int fuzzy)
1509 struct rb_node *n = ctl->free_space_offset.rb_node;
1510 struct btrfs_free_space *entry, *prev = NULL;
1512 /* find entry that is closest to the 'offset' */
1519 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1522 if (offset < entry->offset)
1524 else if (offset > entry->offset)
1537 * bitmap entry and extent entry may share same offset,
1538 * in that case, bitmap entry comes after extent entry.
1543 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1544 if (entry->offset != offset)
1547 WARN_ON(!entry->bitmap);
1550 if (entry->bitmap) {
1552 * if previous extent entry covers the offset,
1553 * we should return it instead of the bitmap entry
1555 n = rb_prev(&entry->offset_index);
1557 prev = rb_entry(n, struct btrfs_free_space,
1559 if (!prev->bitmap &&
1560 prev->offset + prev->bytes > offset)
1570 /* find last entry before the 'offset' */
1572 if (entry->offset > offset) {
1573 n = rb_prev(&entry->offset_index);
1575 entry = rb_entry(n, struct btrfs_free_space,
1577 ASSERT(entry->offset <= offset);
1586 if (entry->bitmap) {
1587 n = rb_prev(&entry->offset_index);
1589 prev = rb_entry(n, struct btrfs_free_space,
1591 if (!prev->bitmap &&
1592 prev->offset + prev->bytes > offset)
1595 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1597 } else if (entry->offset + entry->bytes > offset)
1604 if (entry->bitmap) {
1605 if (entry->offset + BITS_PER_BITMAP *
1609 if (entry->offset + entry->bytes > offset)
1613 n = rb_next(&entry->offset_index);
1616 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1622 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1623 struct btrfs_free_space *info)
1625 rb_erase(&info->offset_index, &ctl->free_space_offset);
1626 ctl->free_extents--;
1629 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1630 struct btrfs_free_space *info)
1632 __unlink_free_space(ctl, info);
1633 ctl->free_space -= info->bytes;
1636 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1637 struct btrfs_free_space *info)
1641 ASSERT(info->bytes || info->bitmap);
1642 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1643 &info->offset_index, (info->bitmap != NULL));
1647 ctl->free_space += info->bytes;
1648 ctl->free_extents++;
1652 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1654 struct btrfs_block_group *block_group = ctl->private;
1658 u64 size = block_group->length;
1659 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1660 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1662 max_bitmaps = max_t(u64, max_bitmaps, 1);
1664 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1667 * The goal is to keep the total amount of memory used per 1gb of space
1668 * at or below 32k, so we need to adjust how much memory we allow to be
1669 * used by extent based free space tracking
1672 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1674 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1677 * we want to account for 1 more bitmap than what we have so we can make
1678 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1679 * we add more bitmaps.
1681 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
1683 if (bitmap_bytes >= max_bytes) {
1684 ctl->extents_thresh = 0;
1689 * we want the extent entry threshold to always be at most 1/2 the max
1690 * bytes we can have, or whatever is less than that.
1692 extent_bytes = max_bytes - bitmap_bytes;
1693 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1695 ctl->extents_thresh =
1696 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1699 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1700 struct btrfs_free_space *info,
1701 u64 offset, u64 bytes)
1703 unsigned long start, count;
1705 start = offset_to_bit(info->offset, ctl->unit, offset);
1706 count = bytes_to_bits(bytes, ctl->unit);
1707 ASSERT(start + count <= BITS_PER_BITMAP);
1709 bitmap_clear(info->bitmap, start, count);
1711 info->bytes -= bytes;
1712 if (info->max_extent_size > ctl->unit)
1713 info->max_extent_size = 0;
1716 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1717 struct btrfs_free_space *info, u64 offset,
1720 __bitmap_clear_bits(ctl, info, offset, bytes);
1721 ctl->free_space -= bytes;
1724 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1725 struct btrfs_free_space *info, u64 offset,
1728 unsigned long start, count;
1730 start = offset_to_bit(info->offset, ctl->unit, offset);
1731 count = bytes_to_bits(bytes, ctl->unit);
1732 ASSERT(start + count <= BITS_PER_BITMAP);
1734 bitmap_set(info->bitmap, start, count);
1736 info->bytes += bytes;
1737 ctl->free_space += bytes;
1741 * If we can not find suitable extent, we will use bytes to record
1742 * the size of the max extent.
1744 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1745 struct btrfs_free_space *bitmap_info, u64 *offset,
1746 u64 *bytes, bool for_alloc)
1748 unsigned long found_bits = 0;
1749 unsigned long max_bits = 0;
1750 unsigned long bits, i;
1751 unsigned long next_zero;
1752 unsigned long extent_bits;
1755 * Skip searching the bitmap if we don't have a contiguous section that
1756 * is large enough for this allocation.
1759 bitmap_info->max_extent_size &&
1760 bitmap_info->max_extent_size < *bytes) {
1761 *bytes = bitmap_info->max_extent_size;
1765 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1766 max_t(u64, *offset, bitmap_info->offset));
1767 bits = bytes_to_bits(*bytes, ctl->unit);
1769 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1770 if (for_alloc && bits == 1) {
1774 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1775 BITS_PER_BITMAP, i);
1776 extent_bits = next_zero - i;
1777 if (extent_bits >= bits) {
1778 found_bits = extent_bits;
1780 } else if (extent_bits > max_bits) {
1781 max_bits = extent_bits;
1787 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1788 *bytes = (u64)(found_bits) * ctl->unit;
1792 *bytes = (u64)(max_bits) * ctl->unit;
1793 bitmap_info->max_extent_size = *bytes;
1797 static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1800 return entry->max_extent_size;
1801 return entry->bytes;
1804 /* Cache the size of the max extent in bytes */
1805 static struct btrfs_free_space *
1806 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1807 unsigned long align, u64 *max_extent_size)
1809 struct btrfs_free_space *entry;
1810 struct rb_node *node;
1815 if (!ctl->free_space_offset.rb_node)
1818 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1822 for (node = &entry->offset_index; node; node = rb_next(node)) {
1823 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1824 if (entry->bytes < *bytes) {
1825 *max_extent_size = max(get_max_extent_size(entry),
1830 /* make sure the space returned is big enough
1831 * to match our requested alignment
1833 if (*bytes >= align) {
1834 tmp = entry->offset - ctl->start + align - 1;
1835 tmp = div64_u64(tmp, align);
1836 tmp = tmp * align + ctl->start;
1837 align_off = tmp - entry->offset;
1840 tmp = entry->offset;
1843 if (entry->bytes < *bytes + align_off) {
1844 *max_extent_size = max(get_max_extent_size(entry),
1849 if (entry->bitmap) {
1852 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1859 max(get_max_extent_size(entry),
1866 *bytes = entry->bytes - align_off;
1873 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1874 struct btrfs_free_space *info, u64 offset)
1876 info->offset = offset_to_bitmap(ctl, offset);
1878 INIT_LIST_HEAD(&info->list);
1879 link_free_space(ctl, info);
1880 ctl->total_bitmaps++;
1882 ctl->op->recalc_thresholds(ctl);
1885 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1886 struct btrfs_free_space *bitmap_info)
1888 unlink_free_space(ctl, bitmap_info);
1889 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
1890 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1891 ctl->total_bitmaps--;
1892 ctl->op->recalc_thresholds(ctl);
1895 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1896 struct btrfs_free_space *bitmap_info,
1897 u64 *offset, u64 *bytes)
1900 u64 search_start, search_bytes;
1904 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1907 * We need to search for bits in this bitmap. We could only cover some
1908 * of the extent in this bitmap thanks to how we add space, so we need
1909 * to search for as much as it as we can and clear that amount, and then
1910 * go searching for the next bit.
1912 search_start = *offset;
1913 search_bytes = ctl->unit;
1914 search_bytes = min(search_bytes, end - search_start + 1);
1915 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1917 if (ret < 0 || search_start != *offset)
1920 /* We may have found more bits than what we need */
1921 search_bytes = min(search_bytes, *bytes);
1923 /* Cannot clear past the end of the bitmap */
1924 search_bytes = min(search_bytes, end - search_start + 1);
1926 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1927 *offset += search_bytes;
1928 *bytes -= search_bytes;
1931 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1932 if (!bitmap_info->bytes)
1933 free_bitmap(ctl, bitmap_info);
1936 * no entry after this bitmap, but we still have bytes to
1937 * remove, so something has gone wrong.
1942 bitmap_info = rb_entry(next, struct btrfs_free_space,
1946 * if the next entry isn't a bitmap we need to return to let the
1947 * extent stuff do its work.
1949 if (!bitmap_info->bitmap)
1953 * Ok the next item is a bitmap, but it may not actually hold
1954 * the information for the rest of this free space stuff, so
1955 * look for it, and if we don't find it return so we can try
1956 * everything over again.
1958 search_start = *offset;
1959 search_bytes = ctl->unit;
1960 ret = search_bitmap(ctl, bitmap_info, &search_start,
1961 &search_bytes, false);
1962 if (ret < 0 || search_start != *offset)
1966 } else if (!bitmap_info->bytes)
1967 free_bitmap(ctl, bitmap_info);
1972 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1973 struct btrfs_free_space *info, u64 offset,
1976 u64 bytes_to_set = 0;
1979 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1981 bytes_to_set = min(end - offset, bytes);
1983 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1986 * We set some bytes, we have no idea what the max extent size is
1989 info->max_extent_size = 0;
1991 return bytes_to_set;
1995 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1996 struct btrfs_free_space *info)
1998 struct btrfs_block_group *block_group = ctl->private;
1999 struct btrfs_fs_info *fs_info = block_group->fs_info;
2000 bool forced = false;
2002 #ifdef CONFIG_BTRFS_DEBUG
2003 if (btrfs_should_fragment_free_space(block_group))
2008 * If we are below the extents threshold then we can add this as an
2009 * extent, and don't have to deal with the bitmap
2011 if (!forced && ctl->free_extents < ctl->extents_thresh) {
2013 * If this block group has some small extents we don't want to
2014 * use up all of our free slots in the cache with them, we want
2015 * to reserve them to larger extents, however if we have plenty
2016 * of cache left then go ahead an dadd them, no sense in adding
2017 * the overhead of a bitmap if we don't have to.
2019 if (info->bytes <= fs_info->sectorsize * 4) {
2020 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2028 * The original block groups from mkfs can be really small, like 8
2029 * megabytes, so don't bother with a bitmap for those entries. However
2030 * some block groups can be smaller than what a bitmap would cover but
2031 * are still large enough that they could overflow the 32k memory limit,
2032 * so allow those block groups to still be allowed to have a bitmap
2035 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
2041 static const struct btrfs_free_space_op free_space_op = {
2042 .recalc_thresholds = recalculate_thresholds,
2043 .use_bitmap = use_bitmap,
2046 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2047 struct btrfs_free_space *info)
2049 struct btrfs_free_space *bitmap_info;
2050 struct btrfs_block_group *block_group = NULL;
2052 u64 bytes, offset, bytes_added;
2055 bytes = info->bytes;
2056 offset = info->offset;
2058 if (!ctl->op->use_bitmap(ctl, info))
2061 if (ctl->op == &free_space_op)
2062 block_group = ctl->private;
2065 * Since we link bitmaps right into the cluster we need to see if we
2066 * have a cluster here, and if so and it has our bitmap we need to add
2067 * the free space to that bitmap.
2069 if (block_group && !list_empty(&block_group->cluster_list)) {
2070 struct btrfs_free_cluster *cluster;
2071 struct rb_node *node;
2072 struct btrfs_free_space *entry;
2074 cluster = list_entry(block_group->cluster_list.next,
2075 struct btrfs_free_cluster,
2077 spin_lock(&cluster->lock);
2078 node = rb_first(&cluster->root);
2080 spin_unlock(&cluster->lock);
2081 goto no_cluster_bitmap;
2084 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2085 if (!entry->bitmap) {
2086 spin_unlock(&cluster->lock);
2087 goto no_cluster_bitmap;
2090 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2091 bytes_added = add_bytes_to_bitmap(ctl, entry,
2093 bytes -= bytes_added;
2094 offset += bytes_added;
2096 spin_unlock(&cluster->lock);
2104 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2111 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2112 bytes -= bytes_added;
2113 offset += bytes_added;
2123 if (info && info->bitmap) {
2124 add_new_bitmap(ctl, info, offset);
2129 spin_unlock(&ctl->tree_lock);
2131 /* no pre-allocated info, allocate a new one */
2133 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2136 spin_lock(&ctl->tree_lock);
2142 /* allocate the bitmap */
2143 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2145 spin_lock(&ctl->tree_lock);
2146 if (!info->bitmap) {
2156 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2158 kmem_cache_free(btrfs_free_space_cachep, info);
2164 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2165 struct btrfs_free_space *info, bool update_stat)
2167 struct btrfs_free_space *left_info;
2168 struct btrfs_free_space *right_info;
2169 bool merged = false;
2170 u64 offset = info->offset;
2171 u64 bytes = info->bytes;
2174 * first we want to see if there is free space adjacent to the range we
2175 * are adding, if there is remove that struct and add a new one to
2176 * cover the entire range
2178 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2179 if (right_info && rb_prev(&right_info->offset_index))
2180 left_info = rb_entry(rb_prev(&right_info->offset_index),
2181 struct btrfs_free_space, offset_index);
2183 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2185 if (right_info && !right_info->bitmap) {
2187 unlink_free_space(ctl, right_info);
2189 __unlink_free_space(ctl, right_info);
2190 info->bytes += right_info->bytes;
2191 kmem_cache_free(btrfs_free_space_cachep, right_info);
2195 if (left_info && !left_info->bitmap &&
2196 left_info->offset + left_info->bytes == offset) {
2198 unlink_free_space(ctl, left_info);
2200 __unlink_free_space(ctl, left_info);
2201 info->offset = left_info->offset;
2202 info->bytes += left_info->bytes;
2203 kmem_cache_free(btrfs_free_space_cachep, left_info);
2210 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2211 struct btrfs_free_space *info,
2214 struct btrfs_free_space *bitmap;
2217 const u64 end = info->offset + info->bytes;
2218 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2221 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2225 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2226 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2229 bytes = (j - i) * ctl->unit;
2230 info->bytes += bytes;
2233 bitmap_clear_bits(ctl, bitmap, end, bytes);
2235 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2238 free_bitmap(ctl, bitmap);
2243 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2244 struct btrfs_free_space *info,
2247 struct btrfs_free_space *bitmap;
2251 unsigned long prev_j;
2254 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2255 /* If we're on a boundary, try the previous logical bitmap. */
2256 if (bitmap_offset == info->offset) {
2257 if (info->offset == 0)
2259 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2262 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2266 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2268 prev_j = (unsigned long)-1;
2269 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2277 if (prev_j == (unsigned long)-1)
2278 bytes = (i + 1) * ctl->unit;
2280 bytes = (i - prev_j) * ctl->unit;
2282 info->offset -= bytes;
2283 info->bytes += bytes;
2286 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2288 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2291 free_bitmap(ctl, bitmap);
2297 * We prefer always to allocate from extent entries, both for clustered and
2298 * non-clustered allocation requests. So when attempting to add a new extent
2299 * entry, try to see if there's adjacent free space in bitmap entries, and if
2300 * there is, migrate that space from the bitmaps to the extent.
2301 * Like this we get better chances of satisfying space allocation requests
2302 * because we attempt to satisfy them based on a single cache entry, and never
2303 * on 2 or more entries - even if the entries represent a contiguous free space
2304 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2307 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2308 struct btrfs_free_space *info,
2312 * Only work with disconnected entries, as we can change their offset,
2313 * and must be extent entries.
2315 ASSERT(!info->bitmap);
2316 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2318 if (ctl->total_bitmaps > 0) {
2320 bool stole_front = false;
2322 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2323 if (ctl->total_bitmaps > 0)
2324 stole_front = steal_from_bitmap_to_front(ctl, info,
2327 if (stole_end || stole_front)
2328 try_merge_free_space(ctl, info, update_stat);
2332 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2333 struct btrfs_free_space_ctl *ctl,
2334 u64 offset, u64 bytes)
2336 struct btrfs_free_space *info;
2339 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2343 info->offset = offset;
2344 info->bytes = bytes;
2345 RB_CLEAR_NODE(&info->offset_index);
2347 spin_lock(&ctl->tree_lock);
2349 if (try_merge_free_space(ctl, info, true))
2353 * There was no extent directly to the left or right of this new
2354 * extent then we know we're going to have to allocate a new extent, so
2355 * before we do that see if we need to drop this into a bitmap
2357 ret = insert_into_bitmap(ctl, info);
2366 * Only steal free space from adjacent bitmaps if we're sure we're not
2367 * going to add the new free space to existing bitmap entries - because
2368 * that would mean unnecessary work that would be reverted. Therefore
2369 * attempt to steal space from bitmaps if we're adding an extent entry.
2371 steal_from_bitmap(ctl, info, true);
2373 ret = link_free_space(ctl, info);
2375 kmem_cache_free(btrfs_free_space_cachep, info);
2377 spin_unlock(&ctl->tree_lock);
2380 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2381 ASSERT(ret != -EEXIST);
2387 int btrfs_add_free_space(struct btrfs_block_group *block_group,
2388 u64 bytenr, u64 size)
2390 return __btrfs_add_free_space(block_group->fs_info,
2391 block_group->free_space_ctl,
2395 int btrfs_remove_free_space(struct btrfs_block_group *block_group,
2396 u64 offset, u64 bytes)
2398 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2399 struct btrfs_free_space *info;
2401 bool re_search = false;
2403 spin_lock(&ctl->tree_lock);
2410 info = tree_search_offset(ctl, offset, 0, 0);
2413 * oops didn't find an extent that matched the space we wanted
2414 * to remove, look for a bitmap instead
2416 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2420 * If we found a partial bit of our free space in a
2421 * bitmap but then couldn't find the other part this may
2422 * be a problem, so WARN about it.
2430 if (!info->bitmap) {
2431 unlink_free_space(ctl, info);
2432 if (offset == info->offset) {
2433 u64 to_free = min(bytes, info->bytes);
2435 info->bytes -= to_free;
2436 info->offset += to_free;
2438 ret = link_free_space(ctl, info);
2441 kmem_cache_free(btrfs_free_space_cachep, info);
2448 u64 old_end = info->bytes + info->offset;
2450 info->bytes = offset - info->offset;
2451 ret = link_free_space(ctl, info);
2456 /* Not enough bytes in this entry to satisfy us */
2457 if (old_end < offset + bytes) {
2458 bytes -= old_end - offset;
2461 } else if (old_end == offset + bytes) {
2465 spin_unlock(&ctl->tree_lock);
2467 ret = btrfs_add_free_space(block_group, offset + bytes,
2468 old_end - (offset + bytes));
2474 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2475 if (ret == -EAGAIN) {
2480 spin_unlock(&ctl->tree_lock);
2485 void btrfs_dump_free_space(struct btrfs_block_group *block_group,
2488 struct btrfs_fs_info *fs_info = block_group->fs_info;
2489 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2490 struct btrfs_free_space *info;
2494 spin_lock(&ctl->tree_lock);
2495 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2496 info = rb_entry(n, struct btrfs_free_space, offset_index);
2497 if (info->bytes >= bytes && !block_group->ro)
2499 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2500 info->offset, info->bytes,
2501 (info->bitmap) ? "yes" : "no");
2503 spin_unlock(&ctl->tree_lock);
2504 btrfs_info(fs_info, "block group has cluster?: %s",
2505 list_empty(&block_group->cluster_list) ? "no" : "yes");
2507 "%d blocks of free space at or bigger than bytes is", count);
2510 void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group)
2512 struct btrfs_fs_info *fs_info = block_group->fs_info;
2513 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2515 spin_lock_init(&ctl->tree_lock);
2516 ctl->unit = fs_info->sectorsize;
2517 ctl->start = block_group->start;
2518 ctl->private = block_group;
2519 ctl->op = &free_space_op;
2520 INIT_LIST_HEAD(&ctl->trimming_ranges);
2521 mutex_init(&ctl->cache_writeout_mutex);
2524 * we only want to have 32k of ram per block group for keeping
2525 * track of free space, and if we pass 1/2 of that we want to
2526 * start converting things over to using bitmaps
2528 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2532 * for a given cluster, put all of its extents back into the free
2533 * space cache. If the block group passed doesn't match the block group
2534 * pointed to by the cluster, someone else raced in and freed the
2535 * cluster already. In that case, we just return without changing anything
2538 __btrfs_return_cluster_to_free_space(
2539 struct btrfs_block_group *block_group,
2540 struct btrfs_free_cluster *cluster)
2542 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2543 struct btrfs_free_space *entry;
2544 struct rb_node *node;
2546 spin_lock(&cluster->lock);
2547 if (cluster->block_group != block_group)
2550 cluster->block_group = NULL;
2551 cluster->window_start = 0;
2552 list_del_init(&cluster->block_group_list);
2554 node = rb_first(&cluster->root);
2558 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2559 node = rb_next(&entry->offset_index);
2560 rb_erase(&entry->offset_index, &cluster->root);
2561 RB_CLEAR_NODE(&entry->offset_index);
2563 bitmap = (entry->bitmap != NULL);
2565 try_merge_free_space(ctl, entry, false);
2566 steal_from_bitmap(ctl, entry, false);
2568 tree_insert_offset(&ctl->free_space_offset,
2569 entry->offset, &entry->offset_index, bitmap);
2571 cluster->root = RB_ROOT;
2574 spin_unlock(&cluster->lock);
2575 btrfs_put_block_group(block_group);
2579 static void __btrfs_remove_free_space_cache_locked(
2580 struct btrfs_free_space_ctl *ctl)
2582 struct btrfs_free_space *info;
2583 struct rb_node *node;
2585 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2586 info = rb_entry(node, struct btrfs_free_space, offset_index);
2587 if (!info->bitmap) {
2588 unlink_free_space(ctl, info);
2589 kmem_cache_free(btrfs_free_space_cachep, info);
2591 free_bitmap(ctl, info);
2594 cond_resched_lock(&ctl->tree_lock);
2598 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2600 spin_lock(&ctl->tree_lock);
2601 __btrfs_remove_free_space_cache_locked(ctl);
2602 spin_unlock(&ctl->tree_lock);
2605 void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
2607 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2608 struct btrfs_free_cluster *cluster;
2609 struct list_head *head;
2611 spin_lock(&ctl->tree_lock);
2612 while ((head = block_group->cluster_list.next) !=
2613 &block_group->cluster_list) {
2614 cluster = list_entry(head, struct btrfs_free_cluster,
2617 WARN_ON(cluster->block_group != block_group);
2618 __btrfs_return_cluster_to_free_space(block_group, cluster);
2620 cond_resched_lock(&ctl->tree_lock);
2622 __btrfs_remove_free_space_cache_locked(ctl);
2623 spin_unlock(&ctl->tree_lock);
2627 u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
2628 u64 offset, u64 bytes, u64 empty_size,
2629 u64 *max_extent_size)
2631 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2632 struct btrfs_free_space *entry = NULL;
2633 u64 bytes_search = bytes + empty_size;
2636 u64 align_gap_len = 0;
2638 spin_lock(&ctl->tree_lock);
2639 entry = find_free_space(ctl, &offset, &bytes_search,
2640 block_group->full_stripe_len, max_extent_size);
2645 if (entry->bitmap) {
2646 bitmap_clear_bits(ctl, entry, offset, bytes);
2648 free_bitmap(ctl, entry);
2650 unlink_free_space(ctl, entry);
2651 align_gap_len = offset - entry->offset;
2652 align_gap = entry->offset;
2654 entry->offset = offset + bytes;
2655 WARN_ON(entry->bytes < bytes + align_gap_len);
2657 entry->bytes -= bytes + align_gap_len;
2659 kmem_cache_free(btrfs_free_space_cachep, entry);
2661 link_free_space(ctl, entry);
2664 spin_unlock(&ctl->tree_lock);
2667 __btrfs_add_free_space(block_group->fs_info, ctl,
2668 align_gap, align_gap_len);
2673 * given a cluster, put all of its extents back into the free space
2674 * cache. If a block group is passed, this function will only free
2675 * a cluster that belongs to the passed block group.
2677 * Otherwise, it'll get a reference on the block group pointed to by the
2678 * cluster and remove the cluster from it.
2680 int btrfs_return_cluster_to_free_space(
2681 struct btrfs_block_group *block_group,
2682 struct btrfs_free_cluster *cluster)
2684 struct btrfs_free_space_ctl *ctl;
2687 /* first, get a safe pointer to the block group */
2688 spin_lock(&cluster->lock);
2690 block_group = cluster->block_group;
2692 spin_unlock(&cluster->lock);
2695 } else if (cluster->block_group != block_group) {
2696 /* someone else has already freed it don't redo their work */
2697 spin_unlock(&cluster->lock);
2700 atomic_inc(&block_group->count);
2701 spin_unlock(&cluster->lock);
2703 ctl = block_group->free_space_ctl;
2705 /* now return any extents the cluster had on it */
2706 spin_lock(&ctl->tree_lock);
2707 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2708 spin_unlock(&ctl->tree_lock);
2710 /* finally drop our ref */
2711 btrfs_put_block_group(block_group);
2715 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
2716 struct btrfs_free_cluster *cluster,
2717 struct btrfs_free_space *entry,
2718 u64 bytes, u64 min_start,
2719 u64 *max_extent_size)
2721 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2723 u64 search_start = cluster->window_start;
2724 u64 search_bytes = bytes;
2727 search_start = min_start;
2728 search_bytes = bytes;
2730 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2732 *max_extent_size = max(get_max_extent_size(entry),
2738 __bitmap_clear_bits(ctl, entry, ret, bytes);
2744 * given a cluster, try to allocate 'bytes' from it, returns 0
2745 * if it couldn't find anything suitably large, or a logical disk offset
2746 * if things worked out
2748 u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
2749 struct btrfs_free_cluster *cluster, u64 bytes,
2750 u64 min_start, u64 *max_extent_size)
2752 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2753 struct btrfs_free_space *entry = NULL;
2754 struct rb_node *node;
2757 spin_lock(&cluster->lock);
2758 if (bytes > cluster->max_size)
2761 if (cluster->block_group != block_group)
2764 node = rb_first(&cluster->root);
2768 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2770 if (entry->bytes < bytes)
2771 *max_extent_size = max(get_max_extent_size(entry),
2774 if (entry->bytes < bytes ||
2775 (!entry->bitmap && entry->offset < min_start)) {
2776 node = rb_next(&entry->offset_index);
2779 entry = rb_entry(node, struct btrfs_free_space,
2784 if (entry->bitmap) {
2785 ret = btrfs_alloc_from_bitmap(block_group,
2786 cluster, entry, bytes,
2787 cluster->window_start,
2790 node = rb_next(&entry->offset_index);
2793 entry = rb_entry(node, struct btrfs_free_space,
2797 cluster->window_start += bytes;
2799 ret = entry->offset;
2801 entry->offset += bytes;
2802 entry->bytes -= bytes;
2805 if (entry->bytes == 0)
2806 rb_erase(&entry->offset_index, &cluster->root);
2810 spin_unlock(&cluster->lock);
2815 spin_lock(&ctl->tree_lock);
2817 ctl->free_space -= bytes;
2818 if (entry->bytes == 0) {
2819 ctl->free_extents--;
2820 if (entry->bitmap) {
2821 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2823 ctl->total_bitmaps--;
2824 ctl->op->recalc_thresholds(ctl);
2826 kmem_cache_free(btrfs_free_space_cachep, entry);
2829 spin_unlock(&ctl->tree_lock);
2834 static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
2835 struct btrfs_free_space *entry,
2836 struct btrfs_free_cluster *cluster,
2837 u64 offset, u64 bytes,
2838 u64 cont1_bytes, u64 min_bytes)
2840 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2841 unsigned long next_zero;
2843 unsigned long want_bits;
2844 unsigned long min_bits;
2845 unsigned long found_bits;
2846 unsigned long max_bits = 0;
2847 unsigned long start = 0;
2848 unsigned long total_found = 0;
2851 i = offset_to_bit(entry->offset, ctl->unit,
2852 max_t(u64, offset, entry->offset));
2853 want_bits = bytes_to_bits(bytes, ctl->unit);
2854 min_bits = bytes_to_bits(min_bytes, ctl->unit);
2857 * Don't bother looking for a cluster in this bitmap if it's heavily
2860 if (entry->max_extent_size &&
2861 entry->max_extent_size < cont1_bytes)
2865 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
2866 next_zero = find_next_zero_bit(entry->bitmap,
2867 BITS_PER_BITMAP, i);
2868 if (next_zero - i >= min_bits) {
2869 found_bits = next_zero - i;
2870 if (found_bits > max_bits)
2871 max_bits = found_bits;
2874 if (next_zero - i > max_bits)
2875 max_bits = next_zero - i;
2880 entry->max_extent_size = (u64)max_bits * ctl->unit;
2886 cluster->max_size = 0;
2889 total_found += found_bits;
2891 if (cluster->max_size < found_bits * ctl->unit)
2892 cluster->max_size = found_bits * ctl->unit;
2894 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2899 cluster->window_start = start * ctl->unit + entry->offset;
2900 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2901 ret = tree_insert_offset(&cluster->root, entry->offset,
2902 &entry->offset_index, 1);
2903 ASSERT(!ret); /* -EEXIST; Logic error */
2905 trace_btrfs_setup_cluster(block_group, cluster,
2906 total_found * ctl->unit, 1);
2911 * This searches the block group for just extents to fill the cluster with.
2912 * Try to find a cluster with at least bytes total bytes, at least one
2913 * extent of cont1_bytes, and other clusters of at least min_bytes.
2916 setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
2917 struct btrfs_free_cluster *cluster,
2918 struct list_head *bitmaps, u64 offset, u64 bytes,
2919 u64 cont1_bytes, u64 min_bytes)
2921 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2922 struct btrfs_free_space *first = NULL;
2923 struct btrfs_free_space *entry = NULL;
2924 struct btrfs_free_space *last;
2925 struct rb_node *node;
2930 entry = tree_search_offset(ctl, offset, 0, 1);
2935 * We don't want bitmaps, so just move along until we find a normal
2938 while (entry->bitmap || entry->bytes < min_bytes) {
2939 if (entry->bitmap && list_empty(&entry->list))
2940 list_add_tail(&entry->list, bitmaps);
2941 node = rb_next(&entry->offset_index);
2944 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2947 window_free = entry->bytes;
2948 max_extent = entry->bytes;
2952 for (node = rb_next(&entry->offset_index); node;
2953 node = rb_next(&entry->offset_index)) {
2954 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2956 if (entry->bitmap) {
2957 if (list_empty(&entry->list))
2958 list_add_tail(&entry->list, bitmaps);
2962 if (entry->bytes < min_bytes)
2966 window_free += entry->bytes;
2967 if (entry->bytes > max_extent)
2968 max_extent = entry->bytes;
2971 if (window_free < bytes || max_extent < cont1_bytes)
2974 cluster->window_start = first->offset;
2976 node = &first->offset_index;
2979 * now we've found our entries, pull them out of the free space
2980 * cache and put them into the cluster rbtree
2985 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2986 node = rb_next(&entry->offset_index);
2987 if (entry->bitmap || entry->bytes < min_bytes)
2990 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2991 ret = tree_insert_offset(&cluster->root, entry->offset,
2992 &entry->offset_index, 0);
2993 total_size += entry->bytes;
2994 ASSERT(!ret); /* -EEXIST; Logic error */
2995 } while (node && entry != last);
2997 cluster->max_size = max_extent;
2998 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
3003 * This specifically looks for bitmaps that may work in the cluster, we assume
3004 * that we have already failed to find extents that will work.
3007 setup_cluster_bitmap(struct btrfs_block_group *block_group,
3008 struct btrfs_free_cluster *cluster,
3009 struct list_head *bitmaps, u64 offset, u64 bytes,
3010 u64 cont1_bytes, u64 min_bytes)
3012 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3013 struct btrfs_free_space *entry = NULL;
3015 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
3017 if (ctl->total_bitmaps == 0)
3021 * The bitmap that covers offset won't be in the list unless offset
3022 * is just its start offset.
3024 if (!list_empty(bitmaps))
3025 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3027 if (!entry || entry->offset != bitmap_offset) {
3028 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3029 if (entry && list_empty(&entry->list))
3030 list_add(&entry->list, bitmaps);
3033 list_for_each_entry(entry, bitmaps, list) {
3034 if (entry->bytes < bytes)
3036 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
3037 bytes, cont1_bytes, min_bytes);
3043 * The bitmaps list has all the bitmaps that record free space
3044 * starting after offset, so no more search is required.
3050 * here we try to find a cluster of blocks in a block group. The goal
3051 * is to find at least bytes+empty_size.
3052 * We might not find them all in one contiguous area.
3054 * returns zero and sets up cluster if things worked out, otherwise
3055 * it returns -enospc
3057 int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
3058 struct btrfs_free_cluster *cluster,
3059 u64 offset, u64 bytes, u64 empty_size)
3061 struct btrfs_fs_info *fs_info = block_group->fs_info;
3062 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3063 struct btrfs_free_space *entry, *tmp;
3070 * Choose the minimum extent size we'll require for this
3071 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3072 * For metadata, allow allocates with smaller extents. For
3073 * data, keep it dense.
3075 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3076 cont1_bytes = min_bytes = bytes + empty_size;
3077 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3078 cont1_bytes = bytes;
3079 min_bytes = fs_info->sectorsize;
3081 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3082 min_bytes = fs_info->sectorsize;
3085 spin_lock(&ctl->tree_lock);
3088 * If we know we don't have enough space to make a cluster don't even
3089 * bother doing all the work to try and find one.
3091 if (ctl->free_space < bytes) {
3092 spin_unlock(&ctl->tree_lock);
3096 spin_lock(&cluster->lock);
3098 /* someone already found a cluster, hooray */
3099 if (cluster->block_group) {
3104 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3107 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3109 cont1_bytes, min_bytes);
3111 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3112 offset, bytes + empty_size,
3113 cont1_bytes, min_bytes);
3115 /* Clear our temporary list */
3116 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3117 list_del_init(&entry->list);
3120 atomic_inc(&block_group->count);
3121 list_add_tail(&cluster->block_group_list,
3122 &block_group->cluster_list);
3123 cluster->block_group = block_group;
3125 trace_btrfs_failed_cluster_setup(block_group);
3128 spin_unlock(&cluster->lock);
3129 spin_unlock(&ctl->tree_lock);
3135 * simple code to zero out a cluster
3137 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3139 spin_lock_init(&cluster->lock);
3140 spin_lock_init(&cluster->refill_lock);
3141 cluster->root = RB_ROOT;
3142 cluster->max_size = 0;
3143 cluster->fragmented = false;
3144 INIT_LIST_HEAD(&cluster->block_group_list);
3145 cluster->block_group = NULL;
3148 static int do_trimming(struct btrfs_block_group *block_group,
3149 u64 *total_trimmed, u64 start, u64 bytes,
3150 u64 reserved_start, u64 reserved_bytes,
3151 struct btrfs_trim_range *trim_entry)
3153 struct btrfs_space_info *space_info = block_group->space_info;
3154 struct btrfs_fs_info *fs_info = block_group->fs_info;
3155 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3160 spin_lock(&space_info->lock);
3161 spin_lock(&block_group->lock);
3162 if (!block_group->ro) {
3163 block_group->reserved += reserved_bytes;
3164 space_info->bytes_reserved += reserved_bytes;
3167 spin_unlock(&block_group->lock);
3168 spin_unlock(&space_info->lock);
3170 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3172 *total_trimmed += trimmed;
3174 mutex_lock(&ctl->cache_writeout_mutex);
3175 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
3176 list_del(&trim_entry->list);
3177 mutex_unlock(&ctl->cache_writeout_mutex);
3180 spin_lock(&space_info->lock);
3181 spin_lock(&block_group->lock);
3182 if (block_group->ro)
3183 space_info->bytes_readonly += reserved_bytes;
3184 block_group->reserved -= reserved_bytes;
3185 space_info->bytes_reserved -= reserved_bytes;
3186 spin_unlock(&block_group->lock);
3187 spin_unlock(&space_info->lock);
3193 static int trim_no_bitmap(struct btrfs_block_group *block_group,
3194 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3196 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3197 struct btrfs_free_space *entry;
3198 struct rb_node *node;
3204 while (start < end) {
3205 struct btrfs_trim_range trim_entry;
3207 mutex_lock(&ctl->cache_writeout_mutex);
3208 spin_lock(&ctl->tree_lock);
3210 if (ctl->free_space < minlen) {
3211 spin_unlock(&ctl->tree_lock);
3212 mutex_unlock(&ctl->cache_writeout_mutex);
3216 entry = tree_search_offset(ctl, start, 0, 1);
3218 spin_unlock(&ctl->tree_lock);
3219 mutex_unlock(&ctl->cache_writeout_mutex);
3224 while (entry->bitmap) {
3225 node = rb_next(&entry->offset_index);
3227 spin_unlock(&ctl->tree_lock);
3228 mutex_unlock(&ctl->cache_writeout_mutex);
3231 entry = rb_entry(node, struct btrfs_free_space,
3235 if (entry->offset >= end) {
3236 spin_unlock(&ctl->tree_lock);
3237 mutex_unlock(&ctl->cache_writeout_mutex);
3241 extent_start = entry->offset;
3242 extent_bytes = entry->bytes;
3243 start = max(start, extent_start);
3244 bytes = min(extent_start + extent_bytes, end) - start;
3245 if (bytes < minlen) {
3246 spin_unlock(&ctl->tree_lock);
3247 mutex_unlock(&ctl->cache_writeout_mutex);
3251 unlink_free_space(ctl, entry);
3252 kmem_cache_free(btrfs_free_space_cachep, entry);
3254 spin_unlock(&ctl->tree_lock);
3255 trim_entry.start = extent_start;
3256 trim_entry.bytes = extent_bytes;
3257 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3258 mutex_unlock(&ctl->cache_writeout_mutex);
3260 ret = do_trimming(block_group, total_trimmed, start, bytes,
3261 extent_start, extent_bytes, &trim_entry);
3267 if (fatal_signal_pending(current)) {
3278 static int trim_bitmaps(struct btrfs_block_group *block_group,
3279 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3281 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3282 struct btrfs_free_space *entry;
3286 u64 offset = offset_to_bitmap(ctl, start);
3288 while (offset < end) {
3289 bool next_bitmap = false;
3290 struct btrfs_trim_range trim_entry;
3292 mutex_lock(&ctl->cache_writeout_mutex);
3293 spin_lock(&ctl->tree_lock);
3295 if (ctl->free_space < minlen) {
3296 spin_unlock(&ctl->tree_lock);
3297 mutex_unlock(&ctl->cache_writeout_mutex);
3301 entry = tree_search_offset(ctl, offset, 1, 0);
3303 spin_unlock(&ctl->tree_lock);
3304 mutex_unlock(&ctl->cache_writeout_mutex);
3310 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3311 if (ret2 || start >= end) {
3312 spin_unlock(&ctl->tree_lock);
3313 mutex_unlock(&ctl->cache_writeout_mutex);
3318 bytes = min(bytes, end - start);
3319 if (bytes < minlen) {
3320 spin_unlock(&ctl->tree_lock);
3321 mutex_unlock(&ctl->cache_writeout_mutex);
3325 bitmap_clear_bits(ctl, entry, start, bytes);
3326 if (entry->bytes == 0)
3327 free_bitmap(ctl, entry);
3329 spin_unlock(&ctl->tree_lock);
3330 trim_entry.start = start;
3331 trim_entry.bytes = bytes;
3332 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3333 mutex_unlock(&ctl->cache_writeout_mutex);
3335 ret = do_trimming(block_group, total_trimmed, start, bytes,
3336 start, bytes, &trim_entry);
3341 offset += BITS_PER_BITMAP * ctl->unit;
3344 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3345 offset += BITS_PER_BITMAP * ctl->unit;
3348 if (fatal_signal_pending(current)) {
3359 void btrfs_get_block_group_trimming(struct btrfs_block_group *cache)
3361 atomic_inc(&cache->trimming);
3364 void btrfs_put_block_group_trimming(struct btrfs_block_group *block_group)
3366 struct btrfs_fs_info *fs_info = block_group->fs_info;
3367 struct extent_map_tree *em_tree;
3368 struct extent_map *em;
3371 spin_lock(&block_group->lock);
3372 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3373 block_group->removed);
3374 spin_unlock(&block_group->lock);
3377 mutex_lock(&fs_info->chunk_mutex);
3378 em_tree = &fs_info->mapping_tree;
3379 write_lock(&em_tree->lock);
3380 em = lookup_extent_mapping(em_tree, block_group->start,
3382 BUG_ON(!em); /* logic error, can't happen */
3383 remove_extent_mapping(em_tree, em);
3384 write_unlock(&em_tree->lock);
3385 mutex_unlock(&fs_info->chunk_mutex);
3387 /* once for us and once for the tree */
3388 free_extent_map(em);
3389 free_extent_map(em);
3392 * We've left one free space entry and other tasks trimming
3393 * this block group have left 1 entry each one. Free them.
3395 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3399 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
3400 u64 *trimmed, u64 start, u64 end, u64 minlen)
3406 spin_lock(&block_group->lock);
3407 if (block_group->removed) {
3408 spin_unlock(&block_group->lock);
3411 btrfs_get_block_group_trimming(block_group);
3412 spin_unlock(&block_group->lock);
3414 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3418 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3420 btrfs_put_block_group_trimming(block_group);
3425 * Find the left-most item in the cache tree, and then return the
3426 * smallest inode number in the item.
3428 * Note: the returned inode number may not be the smallest one in
3429 * the tree, if the left-most item is a bitmap.
3431 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3433 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3434 struct btrfs_free_space *entry = NULL;
3437 spin_lock(&ctl->tree_lock);
3439 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3442 entry = rb_entry(rb_first(&ctl->free_space_offset),
3443 struct btrfs_free_space, offset_index);
3445 if (!entry->bitmap) {
3446 ino = entry->offset;
3448 unlink_free_space(ctl, entry);
3452 kmem_cache_free(btrfs_free_space_cachep, entry);
3454 link_free_space(ctl, entry);
3460 ret = search_bitmap(ctl, entry, &offset, &count, true);
3461 /* Logic error; Should be empty if it can't find anything */
3465 bitmap_clear_bits(ctl, entry, offset, 1);
3466 if (entry->bytes == 0)
3467 free_bitmap(ctl, entry);
3470 spin_unlock(&ctl->tree_lock);
3475 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3476 struct btrfs_path *path)
3478 struct inode *inode = NULL;
3480 spin_lock(&root->ino_cache_lock);
3481 if (root->ino_cache_inode)
3482 inode = igrab(root->ino_cache_inode);
3483 spin_unlock(&root->ino_cache_lock);
3487 inode = __lookup_free_space_inode(root, path, 0);
3491 spin_lock(&root->ino_cache_lock);
3492 if (!btrfs_fs_closing(root->fs_info))
3493 root->ino_cache_inode = igrab(inode);
3494 spin_unlock(&root->ino_cache_lock);
3499 int create_free_ino_inode(struct btrfs_root *root,
3500 struct btrfs_trans_handle *trans,
3501 struct btrfs_path *path)
3503 return __create_free_space_inode(root, trans, path,
3504 BTRFS_FREE_INO_OBJECTID, 0);
3507 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3509 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3510 struct btrfs_path *path;
3511 struct inode *inode;
3513 u64 root_gen = btrfs_root_generation(&root->root_item);
3515 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3519 * If we're unmounting then just return, since this does a search on the
3520 * normal root and not the commit root and we could deadlock.
3522 if (btrfs_fs_closing(fs_info))
3525 path = btrfs_alloc_path();
3529 inode = lookup_free_ino_inode(root, path);
3533 if (root_gen != BTRFS_I(inode)->generation)
3536 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3540 "failed to load free ino cache for root %llu",
3541 root->root_key.objectid);
3545 btrfs_free_path(path);
3549 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3550 struct btrfs_trans_handle *trans,
3551 struct btrfs_path *path,
3552 struct inode *inode)
3554 struct btrfs_fs_info *fs_info = root->fs_info;
3555 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3557 struct btrfs_io_ctl io_ctl;
3558 bool release_metadata = true;
3560 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3563 memset(&io_ctl, 0, sizeof(io_ctl));
3564 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3567 * At this point writepages() didn't error out, so our metadata
3568 * reservation is released when the writeback finishes, at
3569 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3570 * with or without an error.
3572 release_metadata = false;
3573 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3577 if (release_metadata)
3578 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3579 inode->i_size, true);
3582 "failed to write free ino cache for root %llu",
3583 root->root_key.objectid);
3590 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3592 * Use this if you need to make a bitmap or extent entry specifically, it
3593 * doesn't do any of the merging that add_free_space does, this acts a lot like
3594 * how the free space cache loading stuff works, so you can get really weird
3597 int test_add_free_space_entry(struct btrfs_block_group *cache,
3598 u64 offset, u64 bytes, bool bitmap)
3600 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3601 struct btrfs_free_space *info = NULL, *bitmap_info;
3608 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3614 spin_lock(&ctl->tree_lock);
3615 info->offset = offset;
3616 info->bytes = bytes;
3617 info->max_extent_size = 0;
3618 ret = link_free_space(ctl, info);
3619 spin_unlock(&ctl->tree_lock);
3621 kmem_cache_free(btrfs_free_space_cachep, info);
3626 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
3628 kmem_cache_free(btrfs_free_space_cachep, info);
3633 spin_lock(&ctl->tree_lock);
3634 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3639 add_new_bitmap(ctl, info, offset);
3644 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3646 bytes -= bytes_added;
3647 offset += bytes_added;
3648 spin_unlock(&ctl->tree_lock);
3654 kmem_cache_free(btrfs_free_space_cachep, info);
3656 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
3661 * Checks to see if the given range is in the free space cache. This is really
3662 * just used to check the absence of space, so if there is free space in the
3663 * range at all we will return 1.
3665 int test_check_exists(struct btrfs_block_group *cache,
3666 u64 offset, u64 bytes)
3668 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3669 struct btrfs_free_space *info;
3672 spin_lock(&ctl->tree_lock);
3673 info = tree_search_offset(ctl, offset, 0, 0);
3675 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3683 u64 bit_off, bit_bytes;
3685 struct btrfs_free_space *tmp;
3688 bit_bytes = ctl->unit;
3689 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
3691 if (bit_off == offset) {
3694 } else if (bit_off > offset &&
3695 offset + bytes > bit_off) {
3701 n = rb_prev(&info->offset_index);
3703 tmp = rb_entry(n, struct btrfs_free_space,
3705 if (tmp->offset + tmp->bytes < offset)
3707 if (offset + bytes < tmp->offset) {
3708 n = rb_prev(&tmp->offset_index);
3715 n = rb_next(&info->offset_index);
3717 tmp = rb_entry(n, struct btrfs_free_space,
3719 if (offset + bytes < tmp->offset)
3721 if (tmp->offset + tmp->bytes < offset) {
3722 n = rb_next(&tmp->offset_index);
3733 if (info->offset == offset) {
3738 if (offset > info->offset && offset < info->offset + info->bytes)
3741 spin_unlock(&ctl->tree_lock);
3744 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */