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,
750 ret = io_ctl_read_entry(&io_ctl, e, &type);
752 kmem_cache_free(btrfs_free_space_cachep, e);
757 kmem_cache_free(btrfs_free_space_cachep, e);
761 if (type == BTRFS_FREE_SPACE_EXTENT) {
762 spin_lock(&ctl->tree_lock);
763 ret = link_free_space(ctl, e);
764 spin_unlock(&ctl->tree_lock);
767 "Duplicate entries in free space cache, dumping");
768 kmem_cache_free(btrfs_free_space_cachep, e);
774 e->bitmap = kmem_cache_zalloc(
775 btrfs_free_space_bitmap_cachep, GFP_NOFS);
778 btrfs_free_space_cachep, e);
781 spin_lock(&ctl->tree_lock);
782 ret = link_free_space(ctl, e);
783 ctl->total_bitmaps++;
784 ctl->op->recalc_thresholds(ctl);
785 spin_unlock(&ctl->tree_lock);
788 "Duplicate entries in free space cache, dumping");
789 kmem_cache_free(btrfs_free_space_cachep, e);
792 list_add_tail(&e->list, &bitmaps);
798 io_ctl_unmap_page(&io_ctl);
801 * We add the bitmaps at the end of the entries in order that
802 * the bitmap entries are added to the cache.
804 list_for_each_entry_safe(e, n, &bitmaps, list) {
805 list_del_init(&e->list);
806 ret = io_ctl_read_bitmap(&io_ctl, e);
811 io_ctl_drop_pages(&io_ctl);
812 merge_space_tree(ctl);
815 io_ctl_free(&io_ctl);
818 io_ctl_drop_pages(&io_ctl);
819 __btrfs_remove_free_space_cache(ctl);
823 int load_free_space_cache(struct btrfs_block_group_cache *block_group)
825 struct btrfs_fs_info *fs_info = block_group->fs_info;
826 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
828 struct btrfs_path *path;
831 u64 used = btrfs_block_group_used(&block_group->item);
834 * If this block group has been marked to be cleared for one reason or
835 * another then we can't trust the on disk cache, so just return.
837 spin_lock(&block_group->lock);
838 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
839 spin_unlock(&block_group->lock);
842 spin_unlock(&block_group->lock);
844 path = btrfs_alloc_path();
847 path->search_commit_root = 1;
848 path->skip_locking = 1;
851 * We must pass a path with search_commit_root set to btrfs_iget in
852 * order to avoid a deadlock when allocating extents for the tree root.
854 * When we are COWing an extent buffer from the tree root, when looking
855 * for a free extent, at extent-tree.c:find_free_extent(), we can find
856 * block group without its free space cache loaded. When we find one
857 * we must load its space cache which requires reading its free space
858 * cache's inode item from the root tree. If this inode item is located
859 * in the same leaf that we started COWing before, then we end up in
860 * deadlock on the extent buffer (trying to read lock it when we
861 * previously write locked it).
863 * It's safe to read the inode item using the commit root because
864 * block groups, once loaded, stay in memory forever (until they are
865 * removed) as well as their space caches once loaded. New block groups
866 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
867 * we will never try to read their inode item while the fs is mounted.
869 inode = lookup_free_space_inode(block_group, path);
871 btrfs_free_path(path);
875 /* We may have converted the inode and made the cache invalid. */
876 spin_lock(&block_group->lock);
877 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
878 spin_unlock(&block_group->lock);
879 btrfs_free_path(path);
882 spin_unlock(&block_group->lock);
884 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
885 path, block_group->key.objectid);
886 btrfs_free_path(path);
890 spin_lock(&ctl->tree_lock);
891 matched = (ctl->free_space == (block_group->key.offset - used -
892 block_group->bytes_super));
893 spin_unlock(&ctl->tree_lock);
896 __btrfs_remove_free_space_cache(ctl);
898 "block group %llu has wrong amount of free space",
899 block_group->key.objectid);
904 /* This cache is bogus, make sure it gets cleared */
905 spin_lock(&block_group->lock);
906 block_group->disk_cache_state = BTRFS_DC_CLEAR;
907 spin_unlock(&block_group->lock);
911 "failed to load free space cache for block group %llu, rebuilding it now",
912 block_group->key.objectid);
919 static noinline_for_stack
920 int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
921 struct btrfs_free_space_ctl *ctl,
922 struct btrfs_block_group_cache *block_group,
923 int *entries, int *bitmaps,
924 struct list_head *bitmap_list)
927 struct btrfs_free_cluster *cluster = NULL;
928 struct btrfs_free_cluster *cluster_locked = NULL;
929 struct rb_node *node = rb_first(&ctl->free_space_offset);
930 struct btrfs_trim_range *trim_entry;
932 /* Get the cluster for this block_group if it exists */
933 if (block_group && !list_empty(&block_group->cluster_list)) {
934 cluster = list_entry(block_group->cluster_list.next,
935 struct btrfs_free_cluster,
939 if (!node && cluster) {
940 cluster_locked = cluster;
941 spin_lock(&cluster_locked->lock);
942 node = rb_first(&cluster->root);
946 /* Write out the extent entries */
948 struct btrfs_free_space *e;
950 e = rb_entry(node, struct btrfs_free_space, offset_index);
953 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
959 list_add_tail(&e->list, bitmap_list);
962 node = rb_next(node);
963 if (!node && cluster) {
964 node = rb_first(&cluster->root);
965 cluster_locked = cluster;
966 spin_lock(&cluster_locked->lock);
970 if (cluster_locked) {
971 spin_unlock(&cluster_locked->lock);
972 cluster_locked = NULL;
976 * Make sure we don't miss any range that was removed from our rbtree
977 * because trimming is running. Otherwise after a umount+mount (or crash
978 * after committing the transaction) we would leak free space and get
979 * an inconsistent free space cache report from fsck.
981 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
982 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
983 trim_entry->bytes, NULL);
992 spin_unlock(&cluster_locked->lock);
996 static noinline_for_stack int
997 update_cache_item(struct btrfs_trans_handle *trans,
998 struct btrfs_root *root,
1000 struct btrfs_path *path, u64 offset,
1001 int entries, int bitmaps)
1003 struct btrfs_key key;
1004 struct btrfs_free_space_header *header;
1005 struct extent_buffer *leaf;
1008 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1009 key.offset = offset;
1012 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1014 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1015 EXTENT_DELALLOC, 0, 0, NULL);
1018 leaf = path->nodes[0];
1020 struct btrfs_key found_key;
1021 ASSERT(path->slots[0]);
1023 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1024 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1025 found_key.offset != offset) {
1026 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1027 inode->i_size - 1, EXTENT_DELALLOC, 0,
1029 btrfs_release_path(path);
1034 BTRFS_I(inode)->generation = trans->transid;
1035 header = btrfs_item_ptr(leaf, path->slots[0],
1036 struct btrfs_free_space_header);
1037 btrfs_set_free_space_entries(leaf, header, entries);
1038 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1039 btrfs_set_free_space_generation(leaf, header, trans->transid);
1040 btrfs_mark_buffer_dirty(leaf);
1041 btrfs_release_path(path);
1049 static noinline_for_stack int write_pinned_extent_entries(
1050 struct btrfs_block_group_cache *block_group,
1051 struct btrfs_io_ctl *io_ctl,
1054 u64 start, extent_start, extent_end, len;
1055 struct extent_io_tree *unpin = NULL;
1062 * We want to add any pinned extents to our free space cache
1063 * so we don't leak the space
1065 * We shouldn't have switched the pinned extents yet so this is the
1068 unpin = block_group->fs_info->pinned_extents;
1070 start = block_group->key.objectid;
1072 while (start < block_group->key.objectid + block_group->key.offset) {
1073 ret = find_first_extent_bit(unpin, start,
1074 &extent_start, &extent_end,
1075 EXTENT_DIRTY, NULL);
1079 /* This pinned extent is out of our range */
1080 if (extent_start >= block_group->key.objectid +
1081 block_group->key.offset)
1084 extent_start = max(extent_start, start);
1085 extent_end = min(block_group->key.objectid +
1086 block_group->key.offset, extent_end + 1);
1087 len = extent_end - extent_start;
1090 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1100 static noinline_for_stack int
1101 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
1103 struct btrfs_free_space *entry, *next;
1106 /* Write out the bitmaps */
1107 list_for_each_entry_safe(entry, next, bitmap_list, list) {
1108 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1111 list_del_init(&entry->list);
1117 static int flush_dirty_cache(struct inode *inode)
1121 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1123 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1124 EXTENT_DELALLOC, 0, 0, NULL);
1129 static void noinline_for_stack
1130 cleanup_bitmap_list(struct list_head *bitmap_list)
1132 struct btrfs_free_space *entry, *next;
1134 list_for_each_entry_safe(entry, next, bitmap_list, list)
1135 list_del_init(&entry->list);
1138 static void noinline_for_stack
1139 cleanup_write_cache_enospc(struct inode *inode,
1140 struct btrfs_io_ctl *io_ctl,
1141 struct extent_state **cached_state)
1143 io_ctl_drop_pages(io_ctl);
1144 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1145 i_size_read(inode) - 1, cached_state);
1148 static int __btrfs_wait_cache_io(struct btrfs_root *root,
1149 struct btrfs_trans_handle *trans,
1150 struct btrfs_block_group_cache *block_group,
1151 struct btrfs_io_ctl *io_ctl,
1152 struct btrfs_path *path, u64 offset)
1155 struct inode *inode = io_ctl->inode;
1160 /* Flush the dirty pages in the cache file. */
1161 ret = flush_dirty_cache(inode);
1165 /* Update the cache item to tell everyone this cache file is valid. */
1166 ret = update_cache_item(trans, root, inode, path, offset,
1167 io_ctl->entries, io_ctl->bitmaps);
1169 io_ctl_free(io_ctl);
1171 invalidate_inode_pages2(inode->i_mapping);
1172 BTRFS_I(inode)->generation = 0;
1175 btrfs_err(root->fs_info,
1176 "failed to write free space cache for block group %llu",
1177 block_group->key.objectid);
1181 btrfs_update_inode(trans, root, inode);
1184 /* the dirty list is protected by the dirty_bgs_lock */
1185 spin_lock(&trans->transaction->dirty_bgs_lock);
1187 /* the disk_cache_state is protected by the block group lock */
1188 spin_lock(&block_group->lock);
1191 * only mark this as written if we didn't get put back on
1192 * the dirty list while waiting for IO. Otherwise our
1193 * cache state won't be right, and we won't get written again
1195 if (!ret && list_empty(&block_group->dirty_list))
1196 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1198 block_group->disk_cache_state = BTRFS_DC_ERROR;
1200 spin_unlock(&block_group->lock);
1201 spin_unlock(&trans->transaction->dirty_bgs_lock);
1202 io_ctl->inode = NULL;
1210 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1211 struct btrfs_trans_handle *trans,
1212 struct btrfs_io_ctl *io_ctl,
1213 struct btrfs_path *path)
1215 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1218 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1219 struct btrfs_block_group_cache *block_group,
1220 struct btrfs_path *path)
1222 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1223 block_group, &block_group->io_ctl,
1224 path, block_group->key.objectid);
1228 * __btrfs_write_out_cache - write out cached info to an inode
1229 * @root - the root the inode belongs to
1230 * @ctl - the free space cache we are going to write out
1231 * @block_group - the block_group for this cache if it belongs to a block_group
1232 * @trans - the trans handle
1234 * This function writes out a free space cache struct to disk for quick recovery
1235 * on mount. This will return 0 if it was successful in writing the cache out,
1236 * or an errno if it was not.
1238 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1239 struct btrfs_free_space_ctl *ctl,
1240 struct btrfs_block_group_cache *block_group,
1241 struct btrfs_io_ctl *io_ctl,
1242 struct btrfs_trans_handle *trans)
1244 struct extent_state *cached_state = NULL;
1245 LIST_HEAD(bitmap_list);
1251 if (!i_size_read(inode))
1254 WARN_ON(io_ctl->pages);
1255 ret = io_ctl_init(io_ctl, inode, 1);
1259 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1260 down_write(&block_group->data_rwsem);
1261 spin_lock(&block_group->lock);
1262 if (block_group->delalloc_bytes) {
1263 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1264 spin_unlock(&block_group->lock);
1265 up_write(&block_group->data_rwsem);
1266 BTRFS_I(inode)->generation = 0;
1271 spin_unlock(&block_group->lock);
1274 /* Lock all pages first so we can lock the extent safely. */
1275 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1279 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1282 io_ctl_set_generation(io_ctl, trans->transid);
1284 mutex_lock(&ctl->cache_writeout_mutex);
1285 /* Write out the extent entries in the free space cache */
1286 spin_lock(&ctl->tree_lock);
1287 ret = write_cache_extent_entries(io_ctl, ctl,
1288 block_group, &entries, &bitmaps,
1291 goto out_nospc_locked;
1294 * Some spaces that are freed in the current transaction are pinned,
1295 * they will be added into free space cache after the transaction is
1296 * committed, we shouldn't lose them.
1298 * If this changes while we are working we'll get added back to
1299 * the dirty list and redo it. No locking needed
1301 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
1303 goto out_nospc_locked;
1306 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1307 * locked while doing it because a concurrent trim can be manipulating
1308 * or freeing the bitmap.
1310 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1311 spin_unlock(&ctl->tree_lock);
1312 mutex_unlock(&ctl->cache_writeout_mutex);
1316 /* Zero out the rest of the pages just to make sure */
1317 io_ctl_zero_remaining_pages(io_ctl);
1319 /* Everything is written out, now we dirty the pages in the file. */
1320 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1321 i_size_read(inode), &cached_state);
1325 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1326 up_write(&block_group->data_rwsem);
1328 * Release the pages and unlock the extent, we will flush
1331 io_ctl_drop_pages(io_ctl);
1333 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1334 i_size_read(inode) - 1, &cached_state);
1337 * at this point the pages are under IO and we're happy,
1338 * The caller is responsible for waiting on them and updating the
1339 * the cache and the inode
1341 io_ctl->entries = entries;
1342 io_ctl->bitmaps = bitmaps;
1344 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1351 io_ctl->inode = NULL;
1352 io_ctl_free(io_ctl);
1354 invalidate_inode_pages2(inode->i_mapping);
1355 BTRFS_I(inode)->generation = 0;
1357 btrfs_update_inode(trans, root, inode);
1363 cleanup_bitmap_list(&bitmap_list);
1364 spin_unlock(&ctl->tree_lock);
1365 mutex_unlock(&ctl->cache_writeout_mutex);
1368 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1371 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1372 up_write(&block_group->data_rwsem);
1377 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
1378 struct btrfs_block_group_cache *block_group,
1379 struct btrfs_path *path)
1381 struct btrfs_fs_info *fs_info = trans->fs_info;
1382 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1383 struct inode *inode;
1386 spin_lock(&block_group->lock);
1387 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1388 spin_unlock(&block_group->lock);
1391 spin_unlock(&block_group->lock);
1393 inode = lookup_free_space_inode(block_group, path);
1397 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1398 block_group, &block_group->io_ctl, trans);
1402 "failed to write free space cache for block group %llu",
1403 block_group->key.objectid);
1405 spin_lock(&block_group->lock);
1406 block_group->disk_cache_state = BTRFS_DC_ERROR;
1407 spin_unlock(&block_group->lock);
1409 block_group->io_ctl.inode = NULL;
1414 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1415 * to wait for IO and put the inode
1421 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1424 ASSERT(offset >= bitmap_start);
1425 offset -= bitmap_start;
1426 return (unsigned long)(div_u64(offset, unit));
1429 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1431 return (unsigned long)(div_u64(bytes, unit));
1434 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1438 u64 bytes_per_bitmap;
1440 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1441 bitmap_start = offset - ctl->start;
1442 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1443 bitmap_start *= bytes_per_bitmap;
1444 bitmap_start += ctl->start;
1446 return bitmap_start;
1449 static int tree_insert_offset(struct rb_root *root, u64 offset,
1450 struct rb_node *node, int bitmap)
1452 struct rb_node **p = &root->rb_node;
1453 struct rb_node *parent = NULL;
1454 struct btrfs_free_space *info;
1458 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1460 if (offset < info->offset) {
1462 } else if (offset > info->offset) {
1463 p = &(*p)->rb_right;
1466 * we could have a bitmap entry and an extent entry
1467 * share the same offset. If this is the case, we want
1468 * the extent entry to always be found first if we do a
1469 * linear search through the tree, since we want to have
1470 * the quickest allocation time, and allocating from an
1471 * extent is faster than allocating from a bitmap. So
1472 * if we're inserting a bitmap and we find an entry at
1473 * this offset, we want to go right, or after this entry
1474 * logically. If we are inserting an extent and we've
1475 * found a bitmap, we want to go left, or before
1483 p = &(*p)->rb_right;
1485 if (!info->bitmap) {
1494 rb_link_node(node, parent, p);
1495 rb_insert_color(node, root);
1501 * searches the tree for the given offset.
1503 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1504 * want a section that has at least bytes size and comes at or after the given
1507 static struct btrfs_free_space *
1508 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1509 u64 offset, int bitmap_only, int fuzzy)
1511 struct rb_node *n = ctl->free_space_offset.rb_node;
1512 struct btrfs_free_space *entry, *prev = NULL;
1514 /* find entry that is closest to the 'offset' */
1521 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1524 if (offset < entry->offset)
1526 else if (offset > entry->offset)
1539 * bitmap entry and extent entry may share same offset,
1540 * in that case, bitmap entry comes after extent entry.
1545 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1546 if (entry->offset != offset)
1549 WARN_ON(!entry->bitmap);
1552 if (entry->bitmap) {
1554 * if previous extent entry covers the offset,
1555 * we should return it instead of the bitmap entry
1557 n = rb_prev(&entry->offset_index);
1559 prev = rb_entry(n, struct btrfs_free_space,
1561 if (!prev->bitmap &&
1562 prev->offset + prev->bytes > offset)
1572 /* find last entry before the 'offset' */
1574 if (entry->offset > offset) {
1575 n = rb_prev(&entry->offset_index);
1577 entry = rb_entry(n, struct btrfs_free_space,
1579 ASSERT(entry->offset <= offset);
1588 if (entry->bitmap) {
1589 n = rb_prev(&entry->offset_index);
1591 prev = rb_entry(n, struct btrfs_free_space,
1593 if (!prev->bitmap &&
1594 prev->offset + prev->bytes > offset)
1597 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1599 } else if (entry->offset + entry->bytes > offset)
1606 if (entry->bitmap) {
1607 if (entry->offset + BITS_PER_BITMAP *
1611 if (entry->offset + entry->bytes > offset)
1615 n = rb_next(&entry->offset_index);
1618 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1624 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1625 struct btrfs_free_space *info)
1627 rb_erase(&info->offset_index, &ctl->free_space_offset);
1628 ctl->free_extents--;
1631 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1632 struct btrfs_free_space *info)
1634 __unlink_free_space(ctl, info);
1635 ctl->free_space -= info->bytes;
1638 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1639 struct btrfs_free_space *info)
1643 ASSERT(info->bytes || info->bitmap);
1644 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1645 &info->offset_index, (info->bitmap != NULL));
1649 ctl->free_space += info->bytes;
1650 ctl->free_extents++;
1654 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1656 struct btrfs_block_group_cache *block_group = ctl->private;
1660 u64 size = block_group->key.offset;
1661 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1662 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1664 max_bitmaps = max_t(u64, max_bitmaps, 1);
1666 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1669 * The goal is to keep the total amount of memory used per 1gb of space
1670 * at or below 32k, so we need to adjust how much memory we allow to be
1671 * used by extent based free space tracking
1674 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1676 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1679 * we want to account for 1 more bitmap than what we have so we can make
1680 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1681 * we add more bitmaps.
1683 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
1685 if (bitmap_bytes >= max_bytes) {
1686 ctl->extents_thresh = 0;
1691 * we want the extent entry threshold to always be at most 1/2 the max
1692 * bytes we can have, or whatever is less than that.
1694 extent_bytes = max_bytes - bitmap_bytes;
1695 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1697 ctl->extents_thresh =
1698 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1701 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1702 struct btrfs_free_space *info,
1703 u64 offset, u64 bytes)
1705 unsigned long start, count;
1707 start = offset_to_bit(info->offset, ctl->unit, offset);
1708 count = bytes_to_bits(bytes, ctl->unit);
1709 ASSERT(start + count <= BITS_PER_BITMAP);
1711 bitmap_clear(info->bitmap, start, count);
1713 info->bytes -= bytes;
1714 if (info->max_extent_size > ctl->unit)
1715 info->max_extent_size = 0;
1718 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1719 struct btrfs_free_space *info, u64 offset,
1722 __bitmap_clear_bits(ctl, info, offset, bytes);
1723 ctl->free_space -= bytes;
1726 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1727 struct btrfs_free_space *info, u64 offset,
1730 unsigned long start, count;
1732 start = offset_to_bit(info->offset, ctl->unit, offset);
1733 count = bytes_to_bits(bytes, ctl->unit);
1734 ASSERT(start + count <= BITS_PER_BITMAP);
1736 bitmap_set(info->bitmap, start, count);
1738 info->bytes += bytes;
1739 ctl->free_space += bytes;
1743 * If we can not find suitable extent, we will use bytes to record
1744 * the size of the max extent.
1746 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1747 struct btrfs_free_space *bitmap_info, u64 *offset,
1748 u64 *bytes, bool for_alloc)
1750 unsigned long found_bits = 0;
1751 unsigned long max_bits = 0;
1752 unsigned long bits, i;
1753 unsigned long next_zero;
1754 unsigned long extent_bits;
1757 * Skip searching the bitmap if we don't have a contiguous section that
1758 * is large enough for this allocation.
1761 bitmap_info->max_extent_size &&
1762 bitmap_info->max_extent_size < *bytes) {
1763 *bytes = bitmap_info->max_extent_size;
1767 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1768 max_t(u64, *offset, bitmap_info->offset));
1769 bits = bytes_to_bits(*bytes, ctl->unit);
1771 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1772 if (for_alloc && bits == 1) {
1776 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1777 BITS_PER_BITMAP, i);
1778 extent_bits = next_zero - i;
1779 if (extent_bits >= bits) {
1780 found_bits = extent_bits;
1782 } else if (extent_bits > max_bits) {
1783 max_bits = extent_bits;
1789 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1790 *bytes = (u64)(found_bits) * ctl->unit;
1794 *bytes = (u64)(max_bits) * ctl->unit;
1795 bitmap_info->max_extent_size = *bytes;
1799 static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1802 return entry->max_extent_size;
1803 return entry->bytes;
1806 /* Cache the size of the max extent in bytes */
1807 static struct btrfs_free_space *
1808 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1809 unsigned long align, u64 *max_extent_size)
1811 struct btrfs_free_space *entry;
1812 struct rb_node *node;
1817 if (!ctl->free_space_offset.rb_node)
1820 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1824 for (node = &entry->offset_index; node; node = rb_next(node)) {
1825 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1826 if (entry->bytes < *bytes) {
1827 *max_extent_size = max(get_max_extent_size(entry),
1832 /* make sure the space returned is big enough
1833 * to match our requested alignment
1835 if (*bytes >= align) {
1836 tmp = entry->offset - ctl->start + align - 1;
1837 tmp = div64_u64(tmp, align);
1838 tmp = tmp * align + ctl->start;
1839 align_off = tmp - entry->offset;
1842 tmp = entry->offset;
1845 if (entry->bytes < *bytes + align_off) {
1846 *max_extent_size = max(get_max_extent_size(entry),
1851 if (entry->bitmap) {
1854 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1861 max(get_max_extent_size(entry),
1868 *bytes = entry->bytes - align_off;
1875 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1876 struct btrfs_free_space *info, u64 offset)
1878 info->offset = offset_to_bitmap(ctl, offset);
1880 INIT_LIST_HEAD(&info->list);
1881 link_free_space(ctl, info);
1882 ctl->total_bitmaps++;
1884 ctl->op->recalc_thresholds(ctl);
1887 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1888 struct btrfs_free_space *bitmap_info)
1890 unlink_free_space(ctl, bitmap_info);
1891 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
1892 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1893 ctl->total_bitmaps--;
1894 ctl->op->recalc_thresholds(ctl);
1897 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1898 struct btrfs_free_space *bitmap_info,
1899 u64 *offset, u64 *bytes)
1902 u64 search_start, search_bytes;
1906 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1909 * We need to search for bits in this bitmap. We could only cover some
1910 * of the extent in this bitmap thanks to how we add space, so we need
1911 * to search for as much as it as we can and clear that amount, and then
1912 * go searching for the next bit.
1914 search_start = *offset;
1915 search_bytes = ctl->unit;
1916 search_bytes = min(search_bytes, end - search_start + 1);
1917 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1919 if (ret < 0 || search_start != *offset)
1922 /* We may have found more bits than what we need */
1923 search_bytes = min(search_bytes, *bytes);
1925 /* Cannot clear past the end of the bitmap */
1926 search_bytes = min(search_bytes, end - search_start + 1);
1928 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1929 *offset += search_bytes;
1930 *bytes -= search_bytes;
1933 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1934 if (!bitmap_info->bytes)
1935 free_bitmap(ctl, bitmap_info);
1938 * no entry after this bitmap, but we still have bytes to
1939 * remove, so something has gone wrong.
1944 bitmap_info = rb_entry(next, struct btrfs_free_space,
1948 * if the next entry isn't a bitmap we need to return to let the
1949 * extent stuff do its work.
1951 if (!bitmap_info->bitmap)
1955 * Ok the next item is a bitmap, but it may not actually hold
1956 * the information for the rest of this free space stuff, so
1957 * look for it, and if we don't find it return so we can try
1958 * everything over again.
1960 search_start = *offset;
1961 search_bytes = ctl->unit;
1962 ret = search_bitmap(ctl, bitmap_info, &search_start,
1963 &search_bytes, false);
1964 if (ret < 0 || search_start != *offset)
1968 } else if (!bitmap_info->bytes)
1969 free_bitmap(ctl, bitmap_info);
1974 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1975 struct btrfs_free_space *info, u64 offset,
1978 u64 bytes_to_set = 0;
1981 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1983 bytes_to_set = min(end - offset, bytes);
1985 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1988 * We set some bytes, we have no idea what the max extent size is
1991 info->max_extent_size = 0;
1993 return bytes_to_set;
1997 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1998 struct btrfs_free_space *info)
2000 struct btrfs_block_group_cache *block_group = ctl->private;
2001 struct btrfs_fs_info *fs_info = block_group->fs_info;
2002 bool forced = false;
2004 #ifdef CONFIG_BTRFS_DEBUG
2005 if (btrfs_should_fragment_free_space(block_group))
2010 * If we are below the extents threshold then we can add this as an
2011 * extent, and don't have to deal with the bitmap
2013 if (!forced && ctl->free_extents < ctl->extents_thresh) {
2015 * If this block group has some small extents we don't want to
2016 * use up all of our free slots in the cache with them, we want
2017 * to reserve them to larger extents, however if we have plenty
2018 * of cache left then go ahead an dadd them, no sense in adding
2019 * the overhead of a bitmap if we don't have to.
2021 if (info->bytes <= fs_info->sectorsize * 4) {
2022 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2030 * The original block groups from mkfs can be really small, like 8
2031 * megabytes, so don't bother with a bitmap for those entries. However
2032 * some block groups can be smaller than what a bitmap would cover but
2033 * are still large enough that they could overflow the 32k memory limit,
2034 * so allow those block groups to still be allowed to have a bitmap
2037 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
2043 static const struct btrfs_free_space_op free_space_op = {
2044 .recalc_thresholds = recalculate_thresholds,
2045 .use_bitmap = use_bitmap,
2048 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2049 struct btrfs_free_space *info)
2051 struct btrfs_free_space *bitmap_info;
2052 struct btrfs_block_group_cache *block_group = NULL;
2054 u64 bytes, offset, bytes_added;
2057 bytes = info->bytes;
2058 offset = info->offset;
2060 if (!ctl->op->use_bitmap(ctl, info))
2063 if (ctl->op == &free_space_op)
2064 block_group = ctl->private;
2067 * Since we link bitmaps right into the cluster we need to see if we
2068 * have a cluster here, and if so and it has our bitmap we need to add
2069 * the free space to that bitmap.
2071 if (block_group && !list_empty(&block_group->cluster_list)) {
2072 struct btrfs_free_cluster *cluster;
2073 struct rb_node *node;
2074 struct btrfs_free_space *entry;
2076 cluster = list_entry(block_group->cluster_list.next,
2077 struct btrfs_free_cluster,
2079 spin_lock(&cluster->lock);
2080 node = rb_first(&cluster->root);
2082 spin_unlock(&cluster->lock);
2083 goto no_cluster_bitmap;
2086 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2087 if (!entry->bitmap) {
2088 spin_unlock(&cluster->lock);
2089 goto no_cluster_bitmap;
2092 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2093 bytes_added = add_bytes_to_bitmap(ctl, entry,
2095 bytes -= bytes_added;
2096 offset += bytes_added;
2098 spin_unlock(&cluster->lock);
2106 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2113 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2114 bytes -= bytes_added;
2115 offset += bytes_added;
2125 if (info && info->bitmap) {
2126 add_new_bitmap(ctl, info, offset);
2131 spin_unlock(&ctl->tree_lock);
2133 /* no pre-allocated info, allocate a new one */
2135 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2138 spin_lock(&ctl->tree_lock);
2144 /* allocate the bitmap */
2145 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2147 spin_lock(&ctl->tree_lock);
2148 if (!info->bitmap) {
2158 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2160 kmem_cache_free(btrfs_free_space_cachep, info);
2166 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2167 struct btrfs_free_space *info, bool update_stat)
2169 struct btrfs_free_space *left_info;
2170 struct btrfs_free_space *right_info;
2171 bool merged = false;
2172 u64 offset = info->offset;
2173 u64 bytes = info->bytes;
2176 * first we want to see if there is free space adjacent to the range we
2177 * are adding, if there is remove that struct and add a new one to
2178 * cover the entire range
2180 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2181 if (right_info && rb_prev(&right_info->offset_index))
2182 left_info = rb_entry(rb_prev(&right_info->offset_index),
2183 struct btrfs_free_space, offset_index);
2185 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2187 if (right_info && !right_info->bitmap) {
2189 unlink_free_space(ctl, right_info);
2191 __unlink_free_space(ctl, right_info);
2192 info->bytes += right_info->bytes;
2193 kmem_cache_free(btrfs_free_space_cachep, right_info);
2197 if (left_info && !left_info->bitmap &&
2198 left_info->offset + left_info->bytes == offset) {
2200 unlink_free_space(ctl, left_info);
2202 __unlink_free_space(ctl, left_info);
2203 info->offset = left_info->offset;
2204 info->bytes += left_info->bytes;
2205 kmem_cache_free(btrfs_free_space_cachep, left_info);
2212 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2213 struct btrfs_free_space *info,
2216 struct btrfs_free_space *bitmap;
2219 const u64 end = info->offset + info->bytes;
2220 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2223 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2227 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2228 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2231 bytes = (j - i) * ctl->unit;
2232 info->bytes += bytes;
2235 bitmap_clear_bits(ctl, bitmap, end, bytes);
2237 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2240 free_bitmap(ctl, bitmap);
2245 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2246 struct btrfs_free_space *info,
2249 struct btrfs_free_space *bitmap;
2253 unsigned long prev_j;
2256 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2257 /* If we're on a boundary, try the previous logical bitmap. */
2258 if (bitmap_offset == info->offset) {
2259 if (info->offset == 0)
2261 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2264 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2268 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2270 prev_j = (unsigned long)-1;
2271 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2279 if (prev_j == (unsigned long)-1)
2280 bytes = (i + 1) * ctl->unit;
2282 bytes = (i - prev_j) * ctl->unit;
2284 info->offset -= bytes;
2285 info->bytes += bytes;
2288 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2290 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2293 free_bitmap(ctl, bitmap);
2299 * We prefer always to allocate from extent entries, both for clustered and
2300 * non-clustered allocation requests. So when attempting to add a new extent
2301 * entry, try to see if there's adjacent free space in bitmap entries, and if
2302 * there is, migrate that space from the bitmaps to the extent.
2303 * Like this we get better chances of satisfying space allocation requests
2304 * because we attempt to satisfy them based on a single cache entry, and never
2305 * on 2 or more entries - even if the entries represent a contiguous free space
2306 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2309 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2310 struct btrfs_free_space *info,
2314 * Only work with disconnected entries, as we can change their offset,
2315 * and must be extent entries.
2317 ASSERT(!info->bitmap);
2318 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2320 if (ctl->total_bitmaps > 0) {
2322 bool stole_front = false;
2324 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2325 if (ctl->total_bitmaps > 0)
2326 stole_front = steal_from_bitmap_to_front(ctl, info,
2329 if (stole_end || stole_front)
2330 try_merge_free_space(ctl, info, update_stat);
2334 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2335 struct btrfs_free_space_ctl *ctl,
2336 u64 offset, u64 bytes)
2338 struct btrfs_free_space *info;
2341 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2345 info->offset = offset;
2346 info->bytes = bytes;
2347 RB_CLEAR_NODE(&info->offset_index);
2349 spin_lock(&ctl->tree_lock);
2351 if (try_merge_free_space(ctl, info, true))
2355 * There was no extent directly to the left or right of this new
2356 * extent then we know we're going to have to allocate a new extent, so
2357 * before we do that see if we need to drop this into a bitmap
2359 ret = insert_into_bitmap(ctl, info);
2368 * Only steal free space from adjacent bitmaps if we're sure we're not
2369 * going to add the new free space to existing bitmap entries - because
2370 * that would mean unnecessary work that would be reverted. Therefore
2371 * attempt to steal space from bitmaps if we're adding an extent entry.
2373 steal_from_bitmap(ctl, info, true);
2375 ret = link_free_space(ctl, info);
2377 kmem_cache_free(btrfs_free_space_cachep, info);
2379 spin_unlock(&ctl->tree_lock);
2382 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2383 ASSERT(ret != -EEXIST);
2389 int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
2390 u64 bytenr, u64 size)
2392 return __btrfs_add_free_space(block_group->fs_info,
2393 block_group->free_space_ctl,
2397 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2398 u64 offset, u64 bytes)
2400 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2401 struct btrfs_free_space *info;
2403 bool re_search = false;
2405 spin_lock(&ctl->tree_lock);
2412 info = tree_search_offset(ctl, offset, 0, 0);
2415 * oops didn't find an extent that matched the space we wanted
2416 * to remove, look for a bitmap instead
2418 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2422 * If we found a partial bit of our free space in a
2423 * bitmap but then couldn't find the other part this may
2424 * be a problem, so WARN about it.
2432 if (!info->bitmap) {
2433 unlink_free_space(ctl, info);
2434 if (offset == info->offset) {
2435 u64 to_free = min(bytes, info->bytes);
2437 info->bytes -= to_free;
2438 info->offset += to_free;
2440 ret = link_free_space(ctl, info);
2443 kmem_cache_free(btrfs_free_space_cachep, info);
2450 u64 old_end = info->bytes + info->offset;
2452 info->bytes = offset - info->offset;
2453 ret = link_free_space(ctl, info);
2458 /* Not enough bytes in this entry to satisfy us */
2459 if (old_end < offset + bytes) {
2460 bytes -= old_end - offset;
2463 } else if (old_end == offset + bytes) {
2467 spin_unlock(&ctl->tree_lock);
2469 ret = btrfs_add_free_space(block_group, offset + bytes,
2470 old_end - (offset + bytes));
2476 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2477 if (ret == -EAGAIN) {
2482 spin_unlock(&ctl->tree_lock);
2487 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2490 struct btrfs_fs_info *fs_info = block_group->fs_info;
2491 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2492 struct btrfs_free_space *info;
2496 spin_lock(&ctl->tree_lock);
2497 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2498 info = rb_entry(n, struct btrfs_free_space, offset_index);
2499 if (info->bytes >= bytes && !block_group->ro)
2501 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2502 info->offset, info->bytes,
2503 (info->bitmap) ? "yes" : "no");
2505 spin_unlock(&ctl->tree_lock);
2506 btrfs_info(fs_info, "block group has cluster?: %s",
2507 list_empty(&block_group->cluster_list) ? "no" : "yes");
2509 "%d blocks of free space at or bigger than bytes is", count);
2512 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
2514 struct btrfs_fs_info *fs_info = block_group->fs_info;
2515 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2517 spin_lock_init(&ctl->tree_lock);
2518 ctl->unit = fs_info->sectorsize;
2519 ctl->start = block_group->key.objectid;
2520 ctl->private = block_group;
2521 ctl->op = &free_space_op;
2522 INIT_LIST_HEAD(&ctl->trimming_ranges);
2523 mutex_init(&ctl->cache_writeout_mutex);
2526 * we only want to have 32k of ram per block group for keeping
2527 * track of free space, and if we pass 1/2 of that we want to
2528 * start converting things over to using bitmaps
2530 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2534 * for a given cluster, put all of its extents back into the free
2535 * space cache. If the block group passed doesn't match the block group
2536 * pointed to by the cluster, someone else raced in and freed the
2537 * cluster already. In that case, we just return without changing anything
2540 __btrfs_return_cluster_to_free_space(
2541 struct btrfs_block_group_cache *block_group,
2542 struct btrfs_free_cluster *cluster)
2544 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2545 struct btrfs_free_space *entry;
2546 struct rb_node *node;
2548 spin_lock(&cluster->lock);
2549 if (cluster->block_group != block_group)
2552 cluster->block_group = NULL;
2553 cluster->window_start = 0;
2554 list_del_init(&cluster->block_group_list);
2556 node = rb_first(&cluster->root);
2560 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2561 node = rb_next(&entry->offset_index);
2562 rb_erase(&entry->offset_index, &cluster->root);
2563 RB_CLEAR_NODE(&entry->offset_index);
2565 bitmap = (entry->bitmap != NULL);
2567 try_merge_free_space(ctl, entry, false);
2568 steal_from_bitmap(ctl, entry, false);
2570 tree_insert_offset(&ctl->free_space_offset,
2571 entry->offset, &entry->offset_index, bitmap);
2573 cluster->root = RB_ROOT;
2576 spin_unlock(&cluster->lock);
2577 btrfs_put_block_group(block_group);
2581 static void __btrfs_remove_free_space_cache_locked(
2582 struct btrfs_free_space_ctl *ctl)
2584 struct btrfs_free_space *info;
2585 struct rb_node *node;
2587 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2588 info = rb_entry(node, struct btrfs_free_space, offset_index);
2589 if (!info->bitmap) {
2590 unlink_free_space(ctl, info);
2591 kmem_cache_free(btrfs_free_space_cachep, info);
2593 free_bitmap(ctl, info);
2596 cond_resched_lock(&ctl->tree_lock);
2600 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2602 spin_lock(&ctl->tree_lock);
2603 __btrfs_remove_free_space_cache_locked(ctl);
2604 spin_unlock(&ctl->tree_lock);
2607 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2609 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2610 struct btrfs_free_cluster *cluster;
2611 struct list_head *head;
2613 spin_lock(&ctl->tree_lock);
2614 while ((head = block_group->cluster_list.next) !=
2615 &block_group->cluster_list) {
2616 cluster = list_entry(head, struct btrfs_free_cluster,
2619 WARN_ON(cluster->block_group != block_group);
2620 __btrfs_return_cluster_to_free_space(block_group, cluster);
2622 cond_resched_lock(&ctl->tree_lock);
2624 __btrfs_remove_free_space_cache_locked(ctl);
2625 spin_unlock(&ctl->tree_lock);
2629 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2630 u64 offset, u64 bytes, u64 empty_size,
2631 u64 *max_extent_size)
2633 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2634 struct btrfs_free_space *entry = NULL;
2635 u64 bytes_search = bytes + empty_size;
2638 u64 align_gap_len = 0;
2640 spin_lock(&ctl->tree_lock);
2641 entry = find_free_space(ctl, &offset, &bytes_search,
2642 block_group->full_stripe_len, max_extent_size);
2647 if (entry->bitmap) {
2648 bitmap_clear_bits(ctl, entry, offset, bytes);
2650 free_bitmap(ctl, entry);
2652 unlink_free_space(ctl, entry);
2653 align_gap_len = offset - entry->offset;
2654 align_gap = entry->offset;
2656 entry->offset = offset + bytes;
2657 WARN_ON(entry->bytes < bytes + align_gap_len);
2659 entry->bytes -= bytes + align_gap_len;
2661 kmem_cache_free(btrfs_free_space_cachep, entry);
2663 link_free_space(ctl, entry);
2666 spin_unlock(&ctl->tree_lock);
2669 __btrfs_add_free_space(block_group->fs_info, ctl,
2670 align_gap, align_gap_len);
2675 * given a cluster, put all of its extents back into the free space
2676 * cache. If a block group is passed, this function will only free
2677 * a cluster that belongs to the passed block group.
2679 * Otherwise, it'll get a reference on the block group pointed to by the
2680 * cluster and remove the cluster from it.
2682 int btrfs_return_cluster_to_free_space(
2683 struct btrfs_block_group_cache *block_group,
2684 struct btrfs_free_cluster *cluster)
2686 struct btrfs_free_space_ctl *ctl;
2689 /* first, get a safe pointer to the block group */
2690 spin_lock(&cluster->lock);
2692 block_group = cluster->block_group;
2694 spin_unlock(&cluster->lock);
2697 } else if (cluster->block_group != block_group) {
2698 /* someone else has already freed it don't redo their work */
2699 spin_unlock(&cluster->lock);
2702 atomic_inc(&block_group->count);
2703 spin_unlock(&cluster->lock);
2705 ctl = block_group->free_space_ctl;
2707 /* now return any extents the cluster had on it */
2708 spin_lock(&ctl->tree_lock);
2709 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2710 spin_unlock(&ctl->tree_lock);
2712 /* finally drop our ref */
2713 btrfs_put_block_group(block_group);
2717 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2718 struct btrfs_free_cluster *cluster,
2719 struct btrfs_free_space *entry,
2720 u64 bytes, u64 min_start,
2721 u64 *max_extent_size)
2723 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2725 u64 search_start = cluster->window_start;
2726 u64 search_bytes = bytes;
2729 search_start = min_start;
2730 search_bytes = bytes;
2732 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2734 *max_extent_size = max(get_max_extent_size(entry),
2740 __bitmap_clear_bits(ctl, entry, ret, bytes);
2746 * given a cluster, try to allocate 'bytes' from it, returns 0
2747 * if it couldn't find anything suitably large, or a logical disk offset
2748 * if things worked out
2750 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2751 struct btrfs_free_cluster *cluster, u64 bytes,
2752 u64 min_start, u64 *max_extent_size)
2754 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2755 struct btrfs_free_space *entry = NULL;
2756 struct rb_node *node;
2759 spin_lock(&cluster->lock);
2760 if (bytes > cluster->max_size)
2763 if (cluster->block_group != block_group)
2766 node = rb_first(&cluster->root);
2770 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2772 if (entry->bytes < bytes)
2773 *max_extent_size = max(get_max_extent_size(entry),
2776 if (entry->bytes < bytes ||
2777 (!entry->bitmap && entry->offset < min_start)) {
2778 node = rb_next(&entry->offset_index);
2781 entry = rb_entry(node, struct btrfs_free_space,
2786 if (entry->bitmap) {
2787 ret = btrfs_alloc_from_bitmap(block_group,
2788 cluster, entry, bytes,
2789 cluster->window_start,
2792 node = rb_next(&entry->offset_index);
2795 entry = rb_entry(node, struct btrfs_free_space,
2799 cluster->window_start += bytes;
2801 ret = entry->offset;
2803 entry->offset += bytes;
2804 entry->bytes -= bytes;
2807 if (entry->bytes == 0)
2808 rb_erase(&entry->offset_index, &cluster->root);
2812 spin_unlock(&cluster->lock);
2817 spin_lock(&ctl->tree_lock);
2819 ctl->free_space -= bytes;
2820 if (entry->bytes == 0) {
2821 ctl->free_extents--;
2822 if (entry->bitmap) {
2823 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2825 ctl->total_bitmaps--;
2826 ctl->op->recalc_thresholds(ctl);
2828 kmem_cache_free(btrfs_free_space_cachep, entry);
2831 spin_unlock(&ctl->tree_lock);
2836 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2837 struct btrfs_free_space *entry,
2838 struct btrfs_free_cluster *cluster,
2839 u64 offset, u64 bytes,
2840 u64 cont1_bytes, u64 min_bytes)
2842 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2843 unsigned long next_zero;
2845 unsigned long want_bits;
2846 unsigned long min_bits;
2847 unsigned long found_bits;
2848 unsigned long max_bits = 0;
2849 unsigned long start = 0;
2850 unsigned long total_found = 0;
2853 i = offset_to_bit(entry->offset, ctl->unit,
2854 max_t(u64, offset, entry->offset));
2855 want_bits = bytes_to_bits(bytes, ctl->unit);
2856 min_bits = bytes_to_bits(min_bytes, ctl->unit);
2859 * Don't bother looking for a cluster in this bitmap if it's heavily
2862 if (entry->max_extent_size &&
2863 entry->max_extent_size < cont1_bytes)
2867 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
2868 next_zero = find_next_zero_bit(entry->bitmap,
2869 BITS_PER_BITMAP, i);
2870 if (next_zero - i >= min_bits) {
2871 found_bits = next_zero - i;
2872 if (found_bits > max_bits)
2873 max_bits = found_bits;
2876 if (next_zero - i > max_bits)
2877 max_bits = next_zero - i;
2882 entry->max_extent_size = (u64)max_bits * ctl->unit;
2888 cluster->max_size = 0;
2891 total_found += found_bits;
2893 if (cluster->max_size < found_bits * ctl->unit)
2894 cluster->max_size = found_bits * ctl->unit;
2896 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2901 cluster->window_start = start * ctl->unit + entry->offset;
2902 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2903 ret = tree_insert_offset(&cluster->root, entry->offset,
2904 &entry->offset_index, 1);
2905 ASSERT(!ret); /* -EEXIST; Logic error */
2907 trace_btrfs_setup_cluster(block_group, cluster,
2908 total_found * ctl->unit, 1);
2913 * This searches the block group for just extents to fill the cluster with.
2914 * Try to find a cluster with at least bytes total bytes, at least one
2915 * extent of cont1_bytes, and other clusters of at least min_bytes.
2918 setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2919 struct btrfs_free_cluster *cluster,
2920 struct list_head *bitmaps, u64 offset, u64 bytes,
2921 u64 cont1_bytes, u64 min_bytes)
2923 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2924 struct btrfs_free_space *first = NULL;
2925 struct btrfs_free_space *entry = NULL;
2926 struct btrfs_free_space *last;
2927 struct rb_node *node;
2932 entry = tree_search_offset(ctl, offset, 0, 1);
2937 * We don't want bitmaps, so just move along until we find a normal
2940 while (entry->bitmap || entry->bytes < min_bytes) {
2941 if (entry->bitmap && list_empty(&entry->list))
2942 list_add_tail(&entry->list, bitmaps);
2943 node = rb_next(&entry->offset_index);
2946 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2949 window_free = entry->bytes;
2950 max_extent = entry->bytes;
2954 for (node = rb_next(&entry->offset_index); node;
2955 node = rb_next(&entry->offset_index)) {
2956 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2958 if (entry->bitmap) {
2959 if (list_empty(&entry->list))
2960 list_add_tail(&entry->list, bitmaps);
2964 if (entry->bytes < min_bytes)
2968 window_free += entry->bytes;
2969 if (entry->bytes > max_extent)
2970 max_extent = entry->bytes;
2973 if (window_free < bytes || max_extent < cont1_bytes)
2976 cluster->window_start = first->offset;
2978 node = &first->offset_index;
2981 * now we've found our entries, pull them out of the free space
2982 * cache and put them into the cluster rbtree
2987 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2988 node = rb_next(&entry->offset_index);
2989 if (entry->bitmap || entry->bytes < min_bytes)
2992 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2993 ret = tree_insert_offset(&cluster->root, entry->offset,
2994 &entry->offset_index, 0);
2995 total_size += entry->bytes;
2996 ASSERT(!ret); /* -EEXIST; Logic error */
2997 } while (node && entry != last);
2999 cluster->max_size = max_extent;
3000 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
3005 * This specifically looks for bitmaps that may work in the cluster, we assume
3006 * that we have already failed to find extents that will work.
3009 setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
3010 struct btrfs_free_cluster *cluster,
3011 struct list_head *bitmaps, u64 offset, u64 bytes,
3012 u64 cont1_bytes, u64 min_bytes)
3014 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3015 struct btrfs_free_space *entry = NULL;
3017 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
3019 if (ctl->total_bitmaps == 0)
3023 * The bitmap that covers offset won't be in the list unless offset
3024 * is just its start offset.
3026 if (!list_empty(bitmaps))
3027 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3029 if (!entry || entry->offset != bitmap_offset) {
3030 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3031 if (entry && list_empty(&entry->list))
3032 list_add(&entry->list, bitmaps);
3035 list_for_each_entry(entry, bitmaps, list) {
3036 if (entry->bytes < bytes)
3038 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
3039 bytes, cont1_bytes, min_bytes);
3045 * The bitmaps list has all the bitmaps that record free space
3046 * starting after offset, so no more search is required.
3052 * here we try to find a cluster of blocks in a block group. The goal
3053 * is to find at least bytes+empty_size.
3054 * We might not find them all in one contiguous area.
3056 * returns zero and sets up cluster if things worked out, otherwise
3057 * it returns -enospc
3059 int btrfs_find_space_cluster(struct btrfs_block_group_cache *block_group,
3060 struct btrfs_free_cluster *cluster,
3061 u64 offset, u64 bytes, u64 empty_size)
3063 struct btrfs_fs_info *fs_info = block_group->fs_info;
3064 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3065 struct btrfs_free_space *entry, *tmp;
3072 * Choose the minimum extent size we'll require for this
3073 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3074 * For metadata, allow allocates with smaller extents. For
3075 * data, keep it dense.
3077 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3078 cont1_bytes = min_bytes = bytes + empty_size;
3079 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3080 cont1_bytes = bytes;
3081 min_bytes = fs_info->sectorsize;
3083 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3084 min_bytes = fs_info->sectorsize;
3087 spin_lock(&ctl->tree_lock);
3090 * If we know we don't have enough space to make a cluster don't even
3091 * bother doing all the work to try and find one.
3093 if (ctl->free_space < bytes) {
3094 spin_unlock(&ctl->tree_lock);
3098 spin_lock(&cluster->lock);
3100 /* someone already found a cluster, hooray */
3101 if (cluster->block_group) {
3106 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3109 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3111 cont1_bytes, min_bytes);
3113 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3114 offset, bytes + empty_size,
3115 cont1_bytes, min_bytes);
3117 /* Clear our temporary list */
3118 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3119 list_del_init(&entry->list);
3122 atomic_inc(&block_group->count);
3123 list_add_tail(&cluster->block_group_list,
3124 &block_group->cluster_list);
3125 cluster->block_group = block_group;
3127 trace_btrfs_failed_cluster_setup(block_group);
3130 spin_unlock(&cluster->lock);
3131 spin_unlock(&ctl->tree_lock);
3137 * simple code to zero out a cluster
3139 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3141 spin_lock_init(&cluster->lock);
3142 spin_lock_init(&cluster->refill_lock);
3143 cluster->root = RB_ROOT;
3144 cluster->max_size = 0;
3145 cluster->fragmented = false;
3146 INIT_LIST_HEAD(&cluster->block_group_list);
3147 cluster->block_group = NULL;
3150 static int do_trimming(struct btrfs_block_group_cache *block_group,
3151 u64 *total_trimmed, u64 start, u64 bytes,
3152 u64 reserved_start, u64 reserved_bytes,
3153 struct btrfs_trim_range *trim_entry)
3155 struct btrfs_space_info *space_info = block_group->space_info;
3156 struct btrfs_fs_info *fs_info = block_group->fs_info;
3157 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3162 spin_lock(&space_info->lock);
3163 spin_lock(&block_group->lock);
3164 if (!block_group->ro) {
3165 block_group->reserved += reserved_bytes;
3166 space_info->bytes_reserved += reserved_bytes;
3169 spin_unlock(&block_group->lock);
3170 spin_unlock(&space_info->lock);
3172 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3174 *total_trimmed += trimmed;
3176 mutex_lock(&ctl->cache_writeout_mutex);
3177 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
3178 list_del(&trim_entry->list);
3179 mutex_unlock(&ctl->cache_writeout_mutex);
3182 spin_lock(&space_info->lock);
3183 spin_lock(&block_group->lock);
3184 if (block_group->ro)
3185 space_info->bytes_readonly += reserved_bytes;
3186 block_group->reserved -= reserved_bytes;
3187 space_info->bytes_reserved -= reserved_bytes;
3188 spin_unlock(&block_group->lock);
3189 spin_unlock(&space_info->lock);
3195 static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3196 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3198 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3199 struct btrfs_free_space *entry;
3200 struct rb_node *node;
3206 while (start < end) {
3207 struct btrfs_trim_range trim_entry;
3209 mutex_lock(&ctl->cache_writeout_mutex);
3210 spin_lock(&ctl->tree_lock);
3212 if (ctl->free_space < minlen) {
3213 spin_unlock(&ctl->tree_lock);
3214 mutex_unlock(&ctl->cache_writeout_mutex);
3218 entry = tree_search_offset(ctl, start, 0, 1);
3220 spin_unlock(&ctl->tree_lock);
3221 mutex_unlock(&ctl->cache_writeout_mutex);
3226 while (entry->bitmap) {
3227 node = rb_next(&entry->offset_index);
3229 spin_unlock(&ctl->tree_lock);
3230 mutex_unlock(&ctl->cache_writeout_mutex);
3233 entry = rb_entry(node, struct btrfs_free_space,
3237 if (entry->offset >= end) {
3238 spin_unlock(&ctl->tree_lock);
3239 mutex_unlock(&ctl->cache_writeout_mutex);
3243 extent_start = entry->offset;
3244 extent_bytes = entry->bytes;
3245 start = max(start, extent_start);
3246 bytes = min(extent_start + extent_bytes, end) - start;
3247 if (bytes < minlen) {
3248 spin_unlock(&ctl->tree_lock);
3249 mutex_unlock(&ctl->cache_writeout_mutex);
3253 unlink_free_space(ctl, entry);
3254 kmem_cache_free(btrfs_free_space_cachep, entry);
3256 spin_unlock(&ctl->tree_lock);
3257 trim_entry.start = extent_start;
3258 trim_entry.bytes = extent_bytes;
3259 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3260 mutex_unlock(&ctl->cache_writeout_mutex);
3262 ret = do_trimming(block_group, total_trimmed, start, bytes,
3263 extent_start, extent_bytes, &trim_entry);
3269 if (fatal_signal_pending(current)) {
3280 static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3281 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3283 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3284 struct btrfs_free_space *entry;
3288 u64 offset = offset_to_bitmap(ctl, start);
3290 while (offset < end) {
3291 bool next_bitmap = false;
3292 struct btrfs_trim_range trim_entry;
3294 mutex_lock(&ctl->cache_writeout_mutex);
3295 spin_lock(&ctl->tree_lock);
3297 if (ctl->free_space < minlen) {
3298 spin_unlock(&ctl->tree_lock);
3299 mutex_unlock(&ctl->cache_writeout_mutex);
3303 entry = tree_search_offset(ctl, offset, 1, 0);
3305 spin_unlock(&ctl->tree_lock);
3306 mutex_unlock(&ctl->cache_writeout_mutex);
3312 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3313 if (ret2 || start >= end) {
3314 spin_unlock(&ctl->tree_lock);
3315 mutex_unlock(&ctl->cache_writeout_mutex);
3320 bytes = min(bytes, end - start);
3321 if (bytes < minlen) {
3322 spin_unlock(&ctl->tree_lock);
3323 mutex_unlock(&ctl->cache_writeout_mutex);
3327 bitmap_clear_bits(ctl, entry, start, bytes);
3328 if (entry->bytes == 0)
3329 free_bitmap(ctl, entry);
3331 spin_unlock(&ctl->tree_lock);
3332 trim_entry.start = start;
3333 trim_entry.bytes = bytes;
3334 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3335 mutex_unlock(&ctl->cache_writeout_mutex);
3337 ret = do_trimming(block_group, total_trimmed, start, bytes,
3338 start, bytes, &trim_entry);
3343 offset += BITS_PER_BITMAP * ctl->unit;
3346 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3347 offset += BITS_PER_BITMAP * ctl->unit;
3350 if (fatal_signal_pending(current)) {
3361 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
3363 atomic_inc(&cache->trimming);
3366 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3368 struct btrfs_fs_info *fs_info = block_group->fs_info;
3369 struct extent_map_tree *em_tree;
3370 struct extent_map *em;
3373 spin_lock(&block_group->lock);
3374 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3375 block_group->removed);
3376 spin_unlock(&block_group->lock);
3379 mutex_lock(&fs_info->chunk_mutex);
3380 em_tree = &fs_info->mapping_tree;
3381 write_lock(&em_tree->lock);
3382 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3384 BUG_ON(!em); /* logic error, can't happen */
3385 remove_extent_mapping(em_tree, em);
3386 write_unlock(&em_tree->lock);
3387 mutex_unlock(&fs_info->chunk_mutex);
3389 /* once for us and once for the tree */
3390 free_extent_map(em);
3391 free_extent_map(em);
3394 * We've left one free space entry and other tasks trimming
3395 * this block group have left 1 entry each one. Free them.
3397 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3401 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3402 u64 *trimmed, u64 start, u64 end, u64 minlen)
3408 spin_lock(&block_group->lock);
3409 if (block_group->removed) {
3410 spin_unlock(&block_group->lock);
3413 btrfs_get_block_group_trimming(block_group);
3414 spin_unlock(&block_group->lock);
3416 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3420 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3422 btrfs_put_block_group_trimming(block_group);
3427 * Find the left-most item in the cache tree, and then return the
3428 * smallest inode number in the item.
3430 * Note: the returned inode number may not be the smallest one in
3431 * the tree, if the left-most item is a bitmap.
3433 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3435 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3436 struct btrfs_free_space *entry = NULL;
3439 spin_lock(&ctl->tree_lock);
3441 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3444 entry = rb_entry(rb_first(&ctl->free_space_offset),
3445 struct btrfs_free_space, offset_index);
3447 if (!entry->bitmap) {
3448 ino = entry->offset;
3450 unlink_free_space(ctl, entry);
3454 kmem_cache_free(btrfs_free_space_cachep, entry);
3456 link_free_space(ctl, entry);
3462 ret = search_bitmap(ctl, entry, &offset, &count, true);
3463 /* Logic error; Should be empty if it can't find anything */
3467 bitmap_clear_bits(ctl, entry, offset, 1);
3468 if (entry->bytes == 0)
3469 free_bitmap(ctl, entry);
3472 spin_unlock(&ctl->tree_lock);
3477 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3478 struct btrfs_path *path)
3480 struct inode *inode = NULL;
3482 spin_lock(&root->ino_cache_lock);
3483 if (root->ino_cache_inode)
3484 inode = igrab(root->ino_cache_inode);
3485 spin_unlock(&root->ino_cache_lock);
3489 inode = __lookup_free_space_inode(root, path, 0);
3493 spin_lock(&root->ino_cache_lock);
3494 if (!btrfs_fs_closing(root->fs_info))
3495 root->ino_cache_inode = igrab(inode);
3496 spin_unlock(&root->ino_cache_lock);
3501 int create_free_ino_inode(struct btrfs_root *root,
3502 struct btrfs_trans_handle *trans,
3503 struct btrfs_path *path)
3505 return __create_free_space_inode(root, trans, path,
3506 BTRFS_FREE_INO_OBJECTID, 0);
3509 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3511 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3512 struct btrfs_path *path;
3513 struct inode *inode;
3515 u64 root_gen = btrfs_root_generation(&root->root_item);
3517 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3521 * If we're unmounting then just return, since this does a search on the
3522 * normal root and not the commit root and we could deadlock.
3524 if (btrfs_fs_closing(fs_info))
3527 path = btrfs_alloc_path();
3531 inode = lookup_free_ino_inode(root, path);
3535 if (root_gen != BTRFS_I(inode)->generation)
3538 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3542 "failed to load free ino cache for root %llu",
3543 root->root_key.objectid);
3547 btrfs_free_path(path);
3551 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3552 struct btrfs_trans_handle *trans,
3553 struct btrfs_path *path,
3554 struct inode *inode)
3556 struct btrfs_fs_info *fs_info = root->fs_info;
3557 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3559 struct btrfs_io_ctl io_ctl;
3560 bool release_metadata = true;
3562 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3565 memset(&io_ctl, 0, sizeof(io_ctl));
3566 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3569 * At this point writepages() didn't error out, so our metadata
3570 * reservation is released when the writeback finishes, at
3571 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3572 * with or without an error.
3574 release_metadata = false;
3575 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3579 if (release_metadata)
3580 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3581 inode->i_size, true);
3584 "failed to write free ino cache for root %llu",
3585 root->root_key.objectid);
3592 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3594 * Use this if you need to make a bitmap or extent entry specifically, it
3595 * doesn't do any of the merging that add_free_space does, this acts a lot like
3596 * how the free space cache loading stuff works, so you can get really weird
3599 int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3600 u64 offset, u64 bytes, bool bitmap)
3602 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3603 struct btrfs_free_space *info = NULL, *bitmap_info;
3610 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3616 spin_lock(&ctl->tree_lock);
3617 info->offset = offset;
3618 info->bytes = bytes;
3619 info->max_extent_size = 0;
3620 ret = link_free_space(ctl, info);
3621 spin_unlock(&ctl->tree_lock);
3623 kmem_cache_free(btrfs_free_space_cachep, info);
3628 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
3630 kmem_cache_free(btrfs_free_space_cachep, info);
3635 spin_lock(&ctl->tree_lock);
3636 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3641 add_new_bitmap(ctl, info, offset);
3646 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3648 bytes -= bytes_added;
3649 offset += bytes_added;
3650 spin_unlock(&ctl->tree_lock);
3656 kmem_cache_free(btrfs_free_space_cachep, info);
3658 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
3663 * Checks to see if the given range is in the free space cache. This is really
3664 * just used to check the absence of space, so if there is free space in the
3665 * range at all we will return 1.
3667 int test_check_exists(struct btrfs_block_group_cache *cache,
3668 u64 offset, u64 bytes)
3670 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3671 struct btrfs_free_space *info;
3674 spin_lock(&ctl->tree_lock);
3675 info = tree_search_offset(ctl, offset, 0, 0);
3677 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3685 u64 bit_off, bit_bytes;
3687 struct btrfs_free_space *tmp;
3690 bit_bytes = ctl->unit;
3691 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
3693 if (bit_off == offset) {
3696 } else if (bit_off > offset &&
3697 offset + bytes > bit_off) {
3703 n = rb_prev(&info->offset_index);
3705 tmp = rb_entry(n, struct btrfs_free_space,
3707 if (tmp->offset + tmp->bytes < offset)
3709 if (offset + bytes < tmp->offset) {
3710 n = rb_prev(&tmp->offset_index);
3717 n = rb_next(&info->offset_index);
3719 tmp = rb_entry(n, struct btrfs_free_space,
3721 if (offset + bytes < tmp->offset)
3723 if (tmp->offset + tmp->bytes < offset) {
3724 n = rb_next(&tmp->offset_index);
3735 if (info->offset == offset) {
3740 if (offset > info->offset && offset < info->offset + info->bytes)
3743 spin_unlock(&ctl->tree_lock);
3746 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */