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 * Sync discard ensures that the free space cache is always
757 * trimmed. So when reading this in, the state should reflect
760 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
761 e->trim_state = BTRFS_TRIM_STATE_TRIMMED;
764 kmem_cache_free(btrfs_free_space_cachep, e);
768 if (type == BTRFS_FREE_SPACE_EXTENT) {
769 spin_lock(&ctl->tree_lock);
770 ret = link_free_space(ctl, e);
771 spin_unlock(&ctl->tree_lock);
774 "Duplicate entries in free space cache, dumping");
775 kmem_cache_free(btrfs_free_space_cachep, e);
781 e->bitmap = kmem_cache_zalloc(
782 btrfs_free_space_bitmap_cachep, GFP_NOFS);
785 btrfs_free_space_cachep, e);
788 spin_lock(&ctl->tree_lock);
789 ret = link_free_space(ctl, e);
790 ctl->total_bitmaps++;
791 ctl->op->recalc_thresholds(ctl);
792 spin_unlock(&ctl->tree_lock);
795 "Duplicate entries in free space cache, dumping");
796 kmem_cache_free(btrfs_free_space_cachep, e);
799 list_add_tail(&e->list, &bitmaps);
805 io_ctl_unmap_page(&io_ctl);
808 * We add the bitmaps at the end of the entries in order that
809 * the bitmap entries are added to the cache.
811 list_for_each_entry_safe(e, n, &bitmaps, list) {
812 list_del_init(&e->list);
813 ret = io_ctl_read_bitmap(&io_ctl, e);
818 io_ctl_drop_pages(&io_ctl);
819 merge_space_tree(ctl);
822 io_ctl_free(&io_ctl);
825 io_ctl_drop_pages(&io_ctl);
826 __btrfs_remove_free_space_cache(ctl);
830 int load_free_space_cache(struct btrfs_block_group *block_group)
832 struct btrfs_fs_info *fs_info = block_group->fs_info;
833 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
835 struct btrfs_path *path;
838 u64 used = block_group->used;
841 * If this block group has been marked to be cleared for one reason or
842 * another then we can't trust the on disk cache, so just return.
844 spin_lock(&block_group->lock);
845 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
846 spin_unlock(&block_group->lock);
849 spin_unlock(&block_group->lock);
851 path = btrfs_alloc_path();
854 path->search_commit_root = 1;
855 path->skip_locking = 1;
858 * We must pass a path with search_commit_root set to btrfs_iget in
859 * order to avoid a deadlock when allocating extents for the tree root.
861 * When we are COWing an extent buffer from the tree root, when looking
862 * for a free extent, at extent-tree.c:find_free_extent(), we can find
863 * block group without its free space cache loaded. When we find one
864 * we must load its space cache which requires reading its free space
865 * cache's inode item from the root tree. If this inode item is located
866 * in the same leaf that we started COWing before, then we end up in
867 * deadlock on the extent buffer (trying to read lock it when we
868 * previously write locked it).
870 * It's safe to read the inode item using the commit root because
871 * block groups, once loaded, stay in memory forever (until they are
872 * removed) as well as their space caches once loaded. New block groups
873 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
874 * we will never try to read their inode item while the fs is mounted.
876 inode = lookup_free_space_inode(block_group, path);
878 btrfs_free_path(path);
882 /* We may have converted the inode and made the cache invalid. */
883 spin_lock(&block_group->lock);
884 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
885 spin_unlock(&block_group->lock);
886 btrfs_free_path(path);
889 spin_unlock(&block_group->lock);
891 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
892 path, block_group->start);
893 btrfs_free_path(path);
897 spin_lock(&ctl->tree_lock);
898 matched = (ctl->free_space == (block_group->length - used -
899 block_group->bytes_super));
900 spin_unlock(&ctl->tree_lock);
903 __btrfs_remove_free_space_cache(ctl);
905 "block group %llu has wrong amount of free space",
911 /* This cache is bogus, make sure it gets cleared */
912 spin_lock(&block_group->lock);
913 block_group->disk_cache_state = BTRFS_DC_CLEAR;
914 spin_unlock(&block_group->lock);
918 "failed to load free space cache for block group %llu, rebuilding it now",
926 static noinline_for_stack
927 int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
928 struct btrfs_free_space_ctl *ctl,
929 struct btrfs_block_group *block_group,
930 int *entries, int *bitmaps,
931 struct list_head *bitmap_list)
934 struct btrfs_free_cluster *cluster = NULL;
935 struct btrfs_free_cluster *cluster_locked = NULL;
936 struct rb_node *node = rb_first(&ctl->free_space_offset);
937 struct btrfs_trim_range *trim_entry;
939 /* Get the cluster for this block_group if it exists */
940 if (block_group && !list_empty(&block_group->cluster_list)) {
941 cluster = list_entry(block_group->cluster_list.next,
942 struct btrfs_free_cluster,
946 if (!node && cluster) {
947 cluster_locked = cluster;
948 spin_lock(&cluster_locked->lock);
949 node = rb_first(&cluster->root);
953 /* Write out the extent entries */
955 struct btrfs_free_space *e;
957 e = rb_entry(node, struct btrfs_free_space, offset_index);
960 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
966 list_add_tail(&e->list, bitmap_list);
969 node = rb_next(node);
970 if (!node && cluster) {
971 node = rb_first(&cluster->root);
972 cluster_locked = cluster;
973 spin_lock(&cluster_locked->lock);
977 if (cluster_locked) {
978 spin_unlock(&cluster_locked->lock);
979 cluster_locked = NULL;
983 * Make sure we don't miss any range that was removed from our rbtree
984 * because trimming is running. Otherwise after a umount+mount (or crash
985 * after committing the transaction) we would leak free space and get
986 * an inconsistent free space cache report from fsck.
988 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
989 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
990 trim_entry->bytes, NULL);
999 spin_unlock(&cluster_locked->lock);
1003 static noinline_for_stack int
1004 update_cache_item(struct btrfs_trans_handle *trans,
1005 struct btrfs_root *root,
1006 struct inode *inode,
1007 struct btrfs_path *path, u64 offset,
1008 int entries, int bitmaps)
1010 struct btrfs_key key;
1011 struct btrfs_free_space_header *header;
1012 struct extent_buffer *leaf;
1015 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1016 key.offset = offset;
1019 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1021 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1022 EXTENT_DELALLOC, 0, 0, NULL);
1025 leaf = path->nodes[0];
1027 struct btrfs_key found_key;
1028 ASSERT(path->slots[0]);
1030 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1031 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1032 found_key.offset != offset) {
1033 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1034 inode->i_size - 1, EXTENT_DELALLOC, 0,
1036 btrfs_release_path(path);
1041 BTRFS_I(inode)->generation = trans->transid;
1042 header = btrfs_item_ptr(leaf, path->slots[0],
1043 struct btrfs_free_space_header);
1044 btrfs_set_free_space_entries(leaf, header, entries);
1045 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1046 btrfs_set_free_space_generation(leaf, header, trans->transid);
1047 btrfs_mark_buffer_dirty(leaf);
1048 btrfs_release_path(path);
1056 static noinline_for_stack int write_pinned_extent_entries(
1057 struct btrfs_block_group *block_group,
1058 struct btrfs_io_ctl *io_ctl,
1061 u64 start, extent_start, extent_end, len;
1062 struct extent_io_tree *unpin = NULL;
1069 * We want to add any pinned extents to our free space cache
1070 * so we don't leak the space
1072 * We shouldn't have switched the pinned extents yet so this is the
1075 unpin = block_group->fs_info->pinned_extents;
1077 start = block_group->start;
1079 while (start < block_group->start + block_group->length) {
1080 ret = find_first_extent_bit(unpin, start,
1081 &extent_start, &extent_end,
1082 EXTENT_DIRTY, NULL);
1086 /* This pinned extent is out of our range */
1087 if (extent_start >= block_group->start + block_group->length)
1090 extent_start = max(extent_start, start);
1091 extent_end = min(block_group->start + block_group->length,
1093 len = extent_end - extent_start;
1096 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1106 static noinline_for_stack int
1107 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
1109 struct btrfs_free_space *entry, *next;
1112 /* Write out the bitmaps */
1113 list_for_each_entry_safe(entry, next, bitmap_list, list) {
1114 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1117 list_del_init(&entry->list);
1123 static int flush_dirty_cache(struct inode *inode)
1127 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1129 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1130 EXTENT_DELALLOC, 0, 0, NULL);
1135 static void noinline_for_stack
1136 cleanup_bitmap_list(struct list_head *bitmap_list)
1138 struct btrfs_free_space *entry, *next;
1140 list_for_each_entry_safe(entry, next, bitmap_list, list)
1141 list_del_init(&entry->list);
1144 static void noinline_for_stack
1145 cleanup_write_cache_enospc(struct inode *inode,
1146 struct btrfs_io_ctl *io_ctl,
1147 struct extent_state **cached_state)
1149 io_ctl_drop_pages(io_ctl);
1150 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1151 i_size_read(inode) - 1, cached_state);
1154 static int __btrfs_wait_cache_io(struct btrfs_root *root,
1155 struct btrfs_trans_handle *trans,
1156 struct btrfs_block_group *block_group,
1157 struct btrfs_io_ctl *io_ctl,
1158 struct btrfs_path *path, u64 offset)
1161 struct inode *inode = io_ctl->inode;
1166 /* Flush the dirty pages in the cache file. */
1167 ret = flush_dirty_cache(inode);
1171 /* Update the cache item to tell everyone this cache file is valid. */
1172 ret = update_cache_item(trans, root, inode, path, offset,
1173 io_ctl->entries, io_ctl->bitmaps);
1175 io_ctl_free(io_ctl);
1177 invalidate_inode_pages2(inode->i_mapping);
1178 BTRFS_I(inode)->generation = 0;
1181 btrfs_err(root->fs_info,
1182 "failed to write free space cache for block group %llu",
1183 block_group->start);
1187 btrfs_update_inode(trans, root, inode);
1190 /* the dirty list is protected by the dirty_bgs_lock */
1191 spin_lock(&trans->transaction->dirty_bgs_lock);
1193 /* the disk_cache_state is protected by the block group lock */
1194 spin_lock(&block_group->lock);
1197 * only mark this as written if we didn't get put back on
1198 * the dirty list while waiting for IO. Otherwise our
1199 * cache state won't be right, and we won't get written again
1201 if (!ret && list_empty(&block_group->dirty_list))
1202 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1204 block_group->disk_cache_state = BTRFS_DC_ERROR;
1206 spin_unlock(&block_group->lock);
1207 spin_unlock(&trans->transaction->dirty_bgs_lock);
1208 io_ctl->inode = NULL;
1216 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1217 struct btrfs_trans_handle *trans,
1218 struct btrfs_io_ctl *io_ctl,
1219 struct btrfs_path *path)
1221 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1224 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1225 struct btrfs_block_group *block_group,
1226 struct btrfs_path *path)
1228 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1229 block_group, &block_group->io_ctl,
1230 path, block_group->start);
1234 * __btrfs_write_out_cache - write out cached info to an inode
1235 * @root - the root the inode belongs to
1236 * @ctl - the free space cache we are going to write out
1237 * @block_group - the block_group for this cache if it belongs to a block_group
1238 * @trans - the trans handle
1240 * This function writes out a free space cache struct to disk for quick recovery
1241 * on mount. This will return 0 if it was successful in writing the cache out,
1242 * or an errno if it was not.
1244 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1245 struct btrfs_free_space_ctl *ctl,
1246 struct btrfs_block_group *block_group,
1247 struct btrfs_io_ctl *io_ctl,
1248 struct btrfs_trans_handle *trans)
1250 struct extent_state *cached_state = NULL;
1251 LIST_HEAD(bitmap_list);
1257 if (!i_size_read(inode))
1260 WARN_ON(io_ctl->pages);
1261 ret = io_ctl_init(io_ctl, inode, 1);
1265 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1266 down_write(&block_group->data_rwsem);
1267 spin_lock(&block_group->lock);
1268 if (block_group->delalloc_bytes) {
1269 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1270 spin_unlock(&block_group->lock);
1271 up_write(&block_group->data_rwsem);
1272 BTRFS_I(inode)->generation = 0;
1277 spin_unlock(&block_group->lock);
1280 /* Lock all pages first so we can lock the extent safely. */
1281 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1285 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1288 io_ctl_set_generation(io_ctl, trans->transid);
1290 mutex_lock(&ctl->cache_writeout_mutex);
1291 /* Write out the extent entries in the free space cache */
1292 spin_lock(&ctl->tree_lock);
1293 ret = write_cache_extent_entries(io_ctl, ctl,
1294 block_group, &entries, &bitmaps,
1297 goto out_nospc_locked;
1300 * Some spaces that are freed in the current transaction are pinned,
1301 * they will be added into free space cache after the transaction is
1302 * committed, we shouldn't lose them.
1304 * If this changes while we are working we'll get added back to
1305 * the dirty list and redo it. No locking needed
1307 ret = write_pinned_extent_entries(block_group, io_ctl, &entries);
1309 goto out_nospc_locked;
1312 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1313 * locked while doing it because a concurrent trim can be manipulating
1314 * or freeing the bitmap.
1316 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1317 spin_unlock(&ctl->tree_lock);
1318 mutex_unlock(&ctl->cache_writeout_mutex);
1322 /* Zero out the rest of the pages just to make sure */
1323 io_ctl_zero_remaining_pages(io_ctl);
1325 /* Everything is written out, now we dirty the pages in the file. */
1326 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1327 i_size_read(inode), &cached_state);
1331 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1332 up_write(&block_group->data_rwsem);
1334 * Release the pages and unlock the extent, we will flush
1337 io_ctl_drop_pages(io_ctl);
1339 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1340 i_size_read(inode) - 1, &cached_state);
1343 * at this point the pages are under IO and we're happy,
1344 * The caller is responsible for waiting on them and updating the
1345 * the cache and the inode
1347 io_ctl->entries = entries;
1348 io_ctl->bitmaps = bitmaps;
1350 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1357 io_ctl->inode = NULL;
1358 io_ctl_free(io_ctl);
1360 invalidate_inode_pages2(inode->i_mapping);
1361 BTRFS_I(inode)->generation = 0;
1363 btrfs_update_inode(trans, root, inode);
1369 cleanup_bitmap_list(&bitmap_list);
1370 spin_unlock(&ctl->tree_lock);
1371 mutex_unlock(&ctl->cache_writeout_mutex);
1374 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1377 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1378 up_write(&block_group->data_rwsem);
1383 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
1384 struct btrfs_block_group *block_group,
1385 struct btrfs_path *path)
1387 struct btrfs_fs_info *fs_info = trans->fs_info;
1388 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1389 struct inode *inode;
1392 spin_lock(&block_group->lock);
1393 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1394 spin_unlock(&block_group->lock);
1397 spin_unlock(&block_group->lock);
1399 inode = lookup_free_space_inode(block_group, path);
1403 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1404 block_group, &block_group->io_ctl, trans);
1408 "failed to write free space cache for block group %llu",
1409 block_group->start);
1411 spin_lock(&block_group->lock);
1412 block_group->disk_cache_state = BTRFS_DC_ERROR;
1413 spin_unlock(&block_group->lock);
1415 block_group->io_ctl.inode = NULL;
1420 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1421 * to wait for IO and put the inode
1427 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1430 ASSERT(offset >= bitmap_start);
1431 offset -= bitmap_start;
1432 return (unsigned long)(div_u64(offset, unit));
1435 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1437 return (unsigned long)(div_u64(bytes, unit));
1440 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1444 u64 bytes_per_bitmap;
1446 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1447 bitmap_start = offset - ctl->start;
1448 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1449 bitmap_start *= bytes_per_bitmap;
1450 bitmap_start += ctl->start;
1452 return bitmap_start;
1455 static int tree_insert_offset(struct rb_root *root, u64 offset,
1456 struct rb_node *node, int bitmap)
1458 struct rb_node **p = &root->rb_node;
1459 struct rb_node *parent = NULL;
1460 struct btrfs_free_space *info;
1464 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1466 if (offset < info->offset) {
1468 } else if (offset > info->offset) {
1469 p = &(*p)->rb_right;
1472 * we could have a bitmap entry and an extent entry
1473 * share the same offset. If this is the case, we want
1474 * the extent entry to always be found first if we do a
1475 * linear search through the tree, since we want to have
1476 * the quickest allocation time, and allocating from an
1477 * extent is faster than allocating from a bitmap. So
1478 * if we're inserting a bitmap and we find an entry at
1479 * this offset, we want to go right, or after this entry
1480 * logically. If we are inserting an extent and we've
1481 * found a bitmap, we want to go left, or before
1489 p = &(*p)->rb_right;
1491 if (!info->bitmap) {
1500 rb_link_node(node, parent, p);
1501 rb_insert_color(node, root);
1507 * searches the tree for the given offset.
1509 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1510 * want a section that has at least bytes size and comes at or after the given
1513 static struct btrfs_free_space *
1514 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1515 u64 offset, int bitmap_only, int fuzzy)
1517 struct rb_node *n = ctl->free_space_offset.rb_node;
1518 struct btrfs_free_space *entry, *prev = NULL;
1520 /* find entry that is closest to the 'offset' */
1527 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1530 if (offset < entry->offset)
1532 else if (offset > entry->offset)
1545 * bitmap entry and extent entry may share same offset,
1546 * in that case, bitmap entry comes after extent entry.
1551 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1552 if (entry->offset != offset)
1555 WARN_ON(!entry->bitmap);
1558 if (entry->bitmap) {
1560 * if previous extent entry covers the offset,
1561 * we should return it instead of the bitmap entry
1563 n = rb_prev(&entry->offset_index);
1565 prev = rb_entry(n, struct btrfs_free_space,
1567 if (!prev->bitmap &&
1568 prev->offset + prev->bytes > offset)
1578 /* find last entry before the 'offset' */
1580 if (entry->offset > offset) {
1581 n = rb_prev(&entry->offset_index);
1583 entry = rb_entry(n, struct btrfs_free_space,
1585 ASSERT(entry->offset <= offset);
1594 if (entry->bitmap) {
1595 n = rb_prev(&entry->offset_index);
1597 prev = rb_entry(n, struct btrfs_free_space,
1599 if (!prev->bitmap &&
1600 prev->offset + prev->bytes > offset)
1603 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1605 } else if (entry->offset + entry->bytes > offset)
1612 if (entry->bitmap) {
1613 if (entry->offset + BITS_PER_BITMAP *
1617 if (entry->offset + entry->bytes > offset)
1621 n = rb_next(&entry->offset_index);
1624 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1630 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1631 struct btrfs_free_space *info)
1633 rb_erase(&info->offset_index, &ctl->free_space_offset);
1634 ctl->free_extents--;
1637 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1638 struct btrfs_free_space *info)
1640 __unlink_free_space(ctl, info);
1641 ctl->free_space -= info->bytes;
1644 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1645 struct btrfs_free_space *info)
1649 ASSERT(info->bytes || info->bitmap);
1650 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1651 &info->offset_index, (info->bitmap != NULL));
1655 ctl->free_space += info->bytes;
1656 ctl->free_extents++;
1660 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1662 struct btrfs_block_group *block_group = ctl->private;
1666 u64 size = block_group->length;
1667 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1668 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1670 max_bitmaps = max_t(u64, max_bitmaps, 1);
1672 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1675 * The goal is to keep the total amount of memory used per 1gb of space
1676 * at or below 32k, so we need to adjust how much memory we allow to be
1677 * used by extent based free space tracking
1680 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1682 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1685 * we want to account for 1 more bitmap than what we have so we can make
1686 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1687 * we add more bitmaps.
1689 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
1691 if (bitmap_bytes >= max_bytes) {
1692 ctl->extents_thresh = 0;
1697 * we want the extent entry threshold to always be at most 1/2 the max
1698 * bytes we can have, or whatever is less than that.
1700 extent_bytes = max_bytes - bitmap_bytes;
1701 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1703 ctl->extents_thresh =
1704 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1707 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1708 struct btrfs_free_space *info,
1709 u64 offset, u64 bytes)
1711 unsigned long start, count;
1713 start = offset_to_bit(info->offset, ctl->unit, offset);
1714 count = bytes_to_bits(bytes, ctl->unit);
1715 ASSERT(start + count <= BITS_PER_BITMAP);
1717 bitmap_clear(info->bitmap, start, count);
1719 info->bytes -= bytes;
1720 if (info->max_extent_size > ctl->unit)
1721 info->max_extent_size = 0;
1724 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1725 struct btrfs_free_space *info, u64 offset,
1728 __bitmap_clear_bits(ctl, info, offset, bytes);
1729 ctl->free_space -= bytes;
1732 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1733 struct btrfs_free_space *info, u64 offset,
1736 unsigned long start, count;
1738 start = offset_to_bit(info->offset, ctl->unit, offset);
1739 count = bytes_to_bits(bytes, ctl->unit);
1740 ASSERT(start + count <= BITS_PER_BITMAP);
1742 bitmap_set(info->bitmap, start, count);
1744 info->bytes += bytes;
1745 ctl->free_space += bytes;
1749 * If we can not find suitable extent, we will use bytes to record
1750 * the size of the max extent.
1752 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1753 struct btrfs_free_space *bitmap_info, u64 *offset,
1754 u64 *bytes, bool for_alloc)
1756 unsigned long found_bits = 0;
1757 unsigned long max_bits = 0;
1758 unsigned long bits, i;
1759 unsigned long next_zero;
1760 unsigned long extent_bits;
1763 * Skip searching the bitmap if we don't have a contiguous section that
1764 * is large enough for this allocation.
1767 bitmap_info->max_extent_size &&
1768 bitmap_info->max_extent_size < *bytes) {
1769 *bytes = bitmap_info->max_extent_size;
1773 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1774 max_t(u64, *offset, bitmap_info->offset));
1775 bits = bytes_to_bits(*bytes, ctl->unit);
1777 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1778 if (for_alloc && bits == 1) {
1782 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1783 BITS_PER_BITMAP, i);
1784 extent_bits = next_zero - i;
1785 if (extent_bits >= bits) {
1786 found_bits = extent_bits;
1788 } else if (extent_bits > max_bits) {
1789 max_bits = extent_bits;
1795 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1796 *bytes = (u64)(found_bits) * ctl->unit;
1800 *bytes = (u64)(max_bits) * ctl->unit;
1801 bitmap_info->max_extent_size = *bytes;
1805 static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1808 return entry->max_extent_size;
1809 return entry->bytes;
1812 /* Cache the size of the max extent in bytes */
1813 static struct btrfs_free_space *
1814 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1815 unsigned long align, u64 *max_extent_size)
1817 struct btrfs_free_space *entry;
1818 struct rb_node *node;
1823 if (!ctl->free_space_offset.rb_node)
1826 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1830 for (node = &entry->offset_index; node; node = rb_next(node)) {
1831 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1832 if (entry->bytes < *bytes) {
1833 *max_extent_size = max(get_max_extent_size(entry),
1838 /* make sure the space returned is big enough
1839 * to match our requested alignment
1841 if (*bytes >= align) {
1842 tmp = entry->offset - ctl->start + align - 1;
1843 tmp = div64_u64(tmp, align);
1844 tmp = tmp * align + ctl->start;
1845 align_off = tmp - entry->offset;
1848 tmp = entry->offset;
1851 if (entry->bytes < *bytes + align_off) {
1852 *max_extent_size = max(get_max_extent_size(entry),
1857 if (entry->bitmap) {
1860 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1867 max(get_max_extent_size(entry),
1874 *bytes = entry->bytes - align_off;
1881 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1882 struct btrfs_free_space *info, u64 offset)
1884 info->offset = offset_to_bitmap(ctl, offset);
1886 INIT_LIST_HEAD(&info->list);
1887 link_free_space(ctl, info);
1888 ctl->total_bitmaps++;
1890 ctl->op->recalc_thresholds(ctl);
1893 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1894 struct btrfs_free_space *bitmap_info)
1896 unlink_free_space(ctl, bitmap_info);
1897 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
1898 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1899 ctl->total_bitmaps--;
1900 ctl->op->recalc_thresholds(ctl);
1903 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1904 struct btrfs_free_space *bitmap_info,
1905 u64 *offset, u64 *bytes)
1908 u64 search_start, search_bytes;
1912 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1915 * We need to search for bits in this bitmap. We could only cover some
1916 * of the extent in this bitmap thanks to how we add space, so we need
1917 * to search for as much as it as we can and clear that amount, and then
1918 * go searching for the next bit.
1920 search_start = *offset;
1921 search_bytes = ctl->unit;
1922 search_bytes = min(search_bytes, end - search_start + 1);
1923 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1925 if (ret < 0 || search_start != *offset)
1928 /* We may have found more bits than what we need */
1929 search_bytes = min(search_bytes, *bytes);
1931 /* Cannot clear past the end of the bitmap */
1932 search_bytes = min(search_bytes, end - search_start + 1);
1934 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1935 *offset += search_bytes;
1936 *bytes -= search_bytes;
1939 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1940 if (!bitmap_info->bytes)
1941 free_bitmap(ctl, bitmap_info);
1944 * no entry after this bitmap, but we still have bytes to
1945 * remove, so something has gone wrong.
1950 bitmap_info = rb_entry(next, struct btrfs_free_space,
1954 * if the next entry isn't a bitmap we need to return to let the
1955 * extent stuff do its work.
1957 if (!bitmap_info->bitmap)
1961 * Ok the next item is a bitmap, but it may not actually hold
1962 * the information for the rest of this free space stuff, so
1963 * look for it, and if we don't find it return so we can try
1964 * everything over again.
1966 search_start = *offset;
1967 search_bytes = ctl->unit;
1968 ret = search_bitmap(ctl, bitmap_info, &search_start,
1969 &search_bytes, false);
1970 if (ret < 0 || search_start != *offset)
1974 } else if (!bitmap_info->bytes)
1975 free_bitmap(ctl, bitmap_info);
1980 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1981 struct btrfs_free_space *info, u64 offset,
1984 u64 bytes_to_set = 0;
1987 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1989 bytes_to_set = min(end - offset, bytes);
1991 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1994 * We set some bytes, we have no idea what the max extent size is
1997 info->max_extent_size = 0;
1999 return bytes_to_set;
2003 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2004 struct btrfs_free_space *info)
2006 struct btrfs_block_group *block_group = ctl->private;
2007 struct btrfs_fs_info *fs_info = block_group->fs_info;
2008 bool forced = false;
2010 #ifdef CONFIG_BTRFS_DEBUG
2011 if (btrfs_should_fragment_free_space(block_group))
2016 * If we are below the extents threshold then we can add this as an
2017 * extent, and don't have to deal with the bitmap
2019 if (!forced && ctl->free_extents < ctl->extents_thresh) {
2021 * If this block group has some small extents we don't want to
2022 * use up all of our free slots in the cache with them, we want
2023 * to reserve them to larger extents, however if we have plenty
2024 * of cache left then go ahead an dadd them, no sense in adding
2025 * the overhead of a bitmap if we don't have to.
2027 if (info->bytes <= fs_info->sectorsize * 4) {
2028 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2036 * The original block groups from mkfs can be really small, like 8
2037 * megabytes, so don't bother with a bitmap for those entries. However
2038 * some block groups can be smaller than what a bitmap would cover but
2039 * are still large enough that they could overflow the 32k memory limit,
2040 * so allow those block groups to still be allowed to have a bitmap
2043 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
2049 static const struct btrfs_free_space_op free_space_op = {
2050 .recalc_thresholds = recalculate_thresholds,
2051 .use_bitmap = use_bitmap,
2054 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2055 struct btrfs_free_space *info)
2057 struct btrfs_free_space *bitmap_info;
2058 struct btrfs_block_group *block_group = NULL;
2060 u64 bytes, offset, bytes_added;
2063 bytes = info->bytes;
2064 offset = info->offset;
2066 if (!ctl->op->use_bitmap(ctl, info))
2069 if (ctl->op == &free_space_op)
2070 block_group = ctl->private;
2073 * Since we link bitmaps right into the cluster we need to see if we
2074 * have a cluster here, and if so and it has our bitmap we need to add
2075 * the free space to that bitmap.
2077 if (block_group && !list_empty(&block_group->cluster_list)) {
2078 struct btrfs_free_cluster *cluster;
2079 struct rb_node *node;
2080 struct btrfs_free_space *entry;
2082 cluster = list_entry(block_group->cluster_list.next,
2083 struct btrfs_free_cluster,
2085 spin_lock(&cluster->lock);
2086 node = rb_first(&cluster->root);
2088 spin_unlock(&cluster->lock);
2089 goto no_cluster_bitmap;
2092 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2093 if (!entry->bitmap) {
2094 spin_unlock(&cluster->lock);
2095 goto no_cluster_bitmap;
2098 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2099 bytes_added = add_bytes_to_bitmap(ctl, entry,
2101 bytes -= bytes_added;
2102 offset += bytes_added;
2104 spin_unlock(&cluster->lock);
2112 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2119 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2120 bytes -= bytes_added;
2121 offset += bytes_added;
2131 if (info && info->bitmap) {
2132 add_new_bitmap(ctl, info, offset);
2137 spin_unlock(&ctl->tree_lock);
2139 /* no pre-allocated info, allocate a new one */
2141 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2144 spin_lock(&ctl->tree_lock);
2150 /* allocate the bitmap */
2151 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2153 spin_lock(&ctl->tree_lock);
2154 if (!info->bitmap) {
2164 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2166 kmem_cache_free(btrfs_free_space_cachep, info);
2173 * Free space merging rules:
2174 * 1) Merge trimmed areas together
2175 * 2) Let untrimmed areas coalesce with trimmed areas
2176 * 3) Always pull neighboring regions from bitmaps
2178 * The above rules are for when we merge free space based on btrfs_trim_state.
2179 * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
2180 * same reason: to promote larger extent regions which makes life easier for
2181 * find_free_extent(). Rule 2 enables coalescing based on the common path
2182 * being returning free space from btrfs_finish_extent_commit(). So when free
2183 * space is trimmed, it will prevent aggregating trimmed new region and
2184 * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
2185 * and provide find_free_extent() with the largest extents possible hoping for
2188 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2189 struct btrfs_free_space *info, bool update_stat)
2191 struct btrfs_free_space *left_info;
2192 struct btrfs_free_space *right_info;
2193 bool merged = false;
2194 u64 offset = info->offset;
2195 u64 bytes = info->bytes;
2196 const bool is_trimmed = btrfs_free_space_trimmed(info);
2199 * first we want to see if there is free space adjacent to the range we
2200 * are adding, if there is remove that struct and add a new one to
2201 * cover the entire range
2203 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2204 if (right_info && rb_prev(&right_info->offset_index))
2205 left_info = rb_entry(rb_prev(&right_info->offset_index),
2206 struct btrfs_free_space, offset_index);
2208 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2210 /* See try_merge_free_space() comment. */
2211 if (right_info && !right_info->bitmap &&
2212 (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
2214 unlink_free_space(ctl, right_info);
2216 __unlink_free_space(ctl, right_info);
2217 info->bytes += right_info->bytes;
2218 kmem_cache_free(btrfs_free_space_cachep, right_info);
2222 /* See try_merge_free_space() comment. */
2223 if (left_info && !left_info->bitmap &&
2224 left_info->offset + left_info->bytes == offset &&
2225 (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
2227 unlink_free_space(ctl, left_info);
2229 __unlink_free_space(ctl, left_info);
2230 info->offset = left_info->offset;
2231 info->bytes += left_info->bytes;
2232 kmem_cache_free(btrfs_free_space_cachep, left_info);
2239 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2240 struct btrfs_free_space *info,
2243 struct btrfs_free_space *bitmap;
2246 const u64 end = info->offset + info->bytes;
2247 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2250 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2254 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2255 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2258 bytes = (j - i) * ctl->unit;
2259 info->bytes += bytes;
2261 /* See try_merge_free_space() comment. */
2262 if (!btrfs_free_space_trimmed(bitmap))
2263 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2266 bitmap_clear_bits(ctl, bitmap, end, bytes);
2268 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2271 free_bitmap(ctl, bitmap);
2276 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2277 struct btrfs_free_space *info,
2280 struct btrfs_free_space *bitmap;
2284 unsigned long prev_j;
2287 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2288 /* If we're on a boundary, try the previous logical bitmap. */
2289 if (bitmap_offset == info->offset) {
2290 if (info->offset == 0)
2292 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2295 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2299 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2301 prev_j = (unsigned long)-1;
2302 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2310 if (prev_j == (unsigned long)-1)
2311 bytes = (i + 1) * ctl->unit;
2313 bytes = (i - prev_j) * ctl->unit;
2315 info->offset -= bytes;
2316 info->bytes += bytes;
2318 /* See try_merge_free_space() comment. */
2319 if (!btrfs_free_space_trimmed(bitmap))
2320 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2323 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2325 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2328 free_bitmap(ctl, bitmap);
2334 * We prefer always to allocate from extent entries, both for clustered and
2335 * non-clustered allocation requests. So when attempting to add a new extent
2336 * entry, try to see if there's adjacent free space in bitmap entries, and if
2337 * there is, migrate that space from the bitmaps to the extent.
2338 * Like this we get better chances of satisfying space allocation requests
2339 * because we attempt to satisfy them based on a single cache entry, and never
2340 * on 2 or more entries - even if the entries represent a contiguous free space
2341 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2344 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2345 struct btrfs_free_space *info,
2349 * Only work with disconnected entries, as we can change their offset,
2350 * and must be extent entries.
2352 ASSERT(!info->bitmap);
2353 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2355 if (ctl->total_bitmaps > 0) {
2357 bool stole_front = false;
2359 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2360 if (ctl->total_bitmaps > 0)
2361 stole_front = steal_from_bitmap_to_front(ctl, info,
2364 if (stole_end || stole_front)
2365 try_merge_free_space(ctl, info, update_stat);
2369 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2370 struct btrfs_free_space_ctl *ctl,
2371 u64 offset, u64 bytes,
2372 enum btrfs_trim_state trim_state)
2374 struct btrfs_free_space *info;
2377 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2381 info->offset = offset;
2382 info->bytes = bytes;
2383 info->trim_state = trim_state;
2384 RB_CLEAR_NODE(&info->offset_index);
2386 spin_lock(&ctl->tree_lock);
2388 if (try_merge_free_space(ctl, info, true))
2392 * There was no extent directly to the left or right of this new
2393 * extent then we know we're going to have to allocate a new extent, so
2394 * before we do that see if we need to drop this into a bitmap
2396 ret = insert_into_bitmap(ctl, info);
2405 * Only steal free space from adjacent bitmaps if we're sure we're not
2406 * going to add the new free space to existing bitmap entries - because
2407 * that would mean unnecessary work that would be reverted. Therefore
2408 * attempt to steal space from bitmaps if we're adding an extent entry.
2410 steal_from_bitmap(ctl, info, true);
2412 ret = link_free_space(ctl, info);
2414 kmem_cache_free(btrfs_free_space_cachep, info);
2416 spin_unlock(&ctl->tree_lock);
2419 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2420 ASSERT(ret != -EEXIST);
2426 int btrfs_add_free_space(struct btrfs_block_group *block_group,
2427 u64 bytenr, u64 size)
2429 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2431 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
2432 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2434 return __btrfs_add_free_space(block_group->fs_info,
2435 block_group->free_space_ctl,
2436 bytenr, size, trim_state);
2439 int btrfs_remove_free_space(struct btrfs_block_group *block_group,
2440 u64 offset, u64 bytes)
2442 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2443 struct btrfs_free_space *info;
2445 bool re_search = false;
2447 spin_lock(&ctl->tree_lock);
2454 info = tree_search_offset(ctl, offset, 0, 0);
2457 * oops didn't find an extent that matched the space we wanted
2458 * to remove, look for a bitmap instead
2460 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2464 * If we found a partial bit of our free space in a
2465 * bitmap but then couldn't find the other part this may
2466 * be a problem, so WARN about it.
2474 if (!info->bitmap) {
2475 unlink_free_space(ctl, info);
2476 if (offset == info->offset) {
2477 u64 to_free = min(bytes, info->bytes);
2479 info->bytes -= to_free;
2480 info->offset += to_free;
2482 ret = link_free_space(ctl, info);
2485 kmem_cache_free(btrfs_free_space_cachep, info);
2492 u64 old_end = info->bytes + info->offset;
2494 info->bytes = offset - info->offset;
2495 ret = link_free_space(ctl, info);
2500 /* Not enough bytes in this entry to satisfy us */
2501 if (old_end < offset + bytes) {
2502 bytes -= old_end - offset;
2505 } else if (old_end == offset + bytes) {
2509 spin_unlock(&ctl->tree_lock);
2511 ret = __btrfs_add_free_space(block_group->fs_info, ctl,
2513 old_end - (offset + bytes),
2520 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2521 if (ret == -EAGAIN) {
2526 spin_unlock(&ctl->tree_lock);
2531 void btrfs_dump_free_space(struct btrfs_block_group *block_group,
2534 struct btrfs_fs_info *fs_info = block_group->fs_info;
2535 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2536 struct btrfs_free_space *info;
2540 spin_lock(&ctl->tree_lock);
2541 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2542 info = rb_entry(n, struct btrfs_free_space, offset_index);
2543 if (info->bytes >= bytes && !block_group->ro)
2545 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2546 info->offset, info->bytes,
2547 (info->bitmap) ? "yes" : "no");
2549 spin_unlock(&ctl->tree_lock);
2550 btrfs_info(fs_info, "block group has cluster?: %s",
2551 list_empty(&block_group->cluster_list) ? "no" : "yes");
2553 "%d blocks of free space at or bigger than bytes is", count);
2556 void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group)
2558 struct btrfs_fs_info *fs_info = block_group->fs_info;
2559 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2561 spin_lock_init(&ctl->tree_lock);
2562 ctl->unit = fs_info->sectorsize;
2563 ctl->start = block_group->start;
2564 ctl->private = block_group;
2565 ctl->op = &free_space_op;
2566 INIT_LIST_HEAD(&ctl->trimming_ranges);
2567 mutex_init(&ctl->cache_writeout_mutex);
2570 * we only want to have 32k of ram per block group for keeping
2571 * track of free space, and if we pass 1/2 of that we want to
2572 * start converting things over to using bitmaps
2574 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2578 * for a given cluster, put all of its extents back into the free
2579 * space cache. If the block group passed doesn't match the block group
2580 * pointed to by the cluster, someone else raced in and freed the
2581 * cluster already. In that case, we just return without changing anything
2584 __btrfs_return_cluster_to_free_space(
2585 struct btrfs_block_group *block_group,
2586 struct btrfs_free_cluster *cluster)
2588 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2589 struct btrfs_free_space *entry;
2590 struct rb_node *node;
2592 spin_lock(&cluster->lock);
2593 if (cluster->block_group != block_group)
2596 cluster->block_group = NULL;
2597 cluster->window_start = 0;
2598 list_del_init(&cluster->block_group_list);
2600 node = rb_first(&cluster->root);
2604 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2605 node = rb_next(&entry->offset_index);
2606 rb_erase(&entry->offset_index, &cluster->root);
2607 RB_CLEAR_NODE(&entry->offset_index);
2609 bitmap = (entry->bitmap != NULL);
2611 try_merge_free_space(ctl, entry, false);
2612 steal_from_bitmap(ctl, entry, false);
2614 tree_insert_offset(&ctl->free_space_offset,
2615 entry->offset, &entry->offset_index, bitmap);
2617 cluster->root = RB_ROOT;
2620 spin_unlock(&cluster->lock);
2621 btrfs_put_block_group(block_group);
2625 static void __btrfs_remove_free_space_cache_locked(
2626 struct btrfs_free_space_ctl *ctl)
2628 struct btrfs_free_space *info;
2629 struct rb_node *node;
2631 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2632 info = rb_entry(node, struct btrfs_free_space, offset_index);
2633 if (!info->bitmap) {
2634 unlink_free_space(ctl, info);
2635 kmem_cache_free(btrfs_free_space_cachep, info);
2637 free_bitmap(ctl, info);
2640 cond_resched_lock(&ctl->tree_lock);
2644 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2646 spin_lock(&ctl->tree_lock);
2647 __btrfs_remove_free_space_cache_locked(ctl);
2648 spin_unlock(&ctl->tree_lock);
2651 void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
2653 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2654 struct btrfs_free_cluster *cluster;
2655 struct list_head *head;
2657 spin_lock(&ctl->tree_lock);
2658 while ((head = block_group->cluster_list.next) !=
2659 &block_group->cluster_list) {
2660 cluster = list_entry(head, struct btrfs_free_cluster,
2663 WARN_ON(cluster->block_group != block_group);
2664 __btrfs_return_cluster_to_free_space(block_group, cluster);
2666 cond_resched_lock(&ctl->tree_lock);
2668 __btrfs_remove_free_space_cache_locked(ctl);
2669 spin_unlock(&ctl->tree_lock);
2673 u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
2674 u64 offset, u64 bytes, u64 empty_size,
2675 u64 *max_extent_size)
2677 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2678 struct btrfs_free_space *entry = NULL;
2679 u64 bytes_search = bytes + empty_size;
2682 u64 align_gap_len = 0;
2683 enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2685 spin_lock(&ctl->tree_lock);
2686 entry = find_free_space(ctl, &offset, &bytes_search,
2687 block_group->full_stripe_len, max_extent_size);
2692 if (entry->bitmap) {
2693 bitmap_clear_bits(ctl, entry, offset, bytes);
2695 free_bitmap(ctl, entry);
2697 unlink_free_space(ctl, entry);
2698 align_gap_len = offset - entry->offset;
2699 align_gap = entry->offset;
2700 align_gap_trim_state = entry->trim_state;
2702 entry->offset = offset + bytes;
2703 WARN_ON(entry->bytes < bytes + align_gap_len);
2705 entry->bytes -= bytes + align_gap_len;
2707 kmem_cache_free(btrfs_free_space_cachep, entry);
2709 link_free_space(ctl, entry);
2712 spin_unlock(&ctl->tree_lock);
2715 __btrfs_add_free_space(block_group->fs_info, ctl,
2716 align_gap, align_gap_len,
2717 align_gap_trim_state);
2722 * given a cluster, put all of its extents back into the free space
2723 * cache. If a block group is passed, this function will only free
2724 * a cluster that belongs to the passed block group.
2726 * Otherwise, it'll get a reference on the block group pointed to by the
2727 * cluster and remove the cluster from it.
2729 int btrfs_return_cluster_to_free_space(
2730 struct btrfs_block_group *block_group,
2731 struct btrfs_free_cluster *cluster)
2733 struct btrfs_free_space_ctl *ctl;
2736 /* first, get a safe pointer to the block group */
2737 spin_lock(&cluster->lock);
2739 block_group = cluster->block_group;
2741 spin_unlock(&cluster->lock);
2744 } else if (cluster->block_group != block_group) {
2745 /* someone else has already freed it don't redo their work */
2746 spin_unlock(&cluster->lock);
2749 atomic_inc(&block_group->count);
2750 spin_unlock(&cluster->lock);
2752 ctl = block_group->free_space_ctl;
2754 /* now return any extents the cluster had on it */
2755 spin_lock(&ctl->tree_lock);
2756 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2757 spin_unlock(&ctl->tree_lock);
2759 /* finally drop our ref */
2760 btrfs_put_block_group(block_group);
2764 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
2765 struct btrfs_free_cluster *cluster,
2766 struct btrfs_free_space *entry,
2767 u64 bytes, u64 min_start,
2768 u64 *max_extent_size)
2770 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2772 u64 search_start = cluster->window_start;
2773 u64 search_bytes = bytes;
2776 search_start = min_start;
2777 search_bytes = bytes;
2779 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2781 *max_extent_size = max(get_max_extent_size(entry),
2787 __bitmap_clear_bits(ctl, entry, ret, bytes);
2793 * given a cluster, try to allocate 'bytes' from it, returns 0
2794 * if it couldn't find anything suitably large, or a logical disk offset
2795 * if things worked out
2797 u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
2798 struct btrfs_free_cluster *cluster, u64 bytes,
2799 u64 min_start, u64 *max_extent_size)
2801 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2802 struct btrfs_free_space *entry = NULL;
2803 struct rb_node *node;
2806 spin_lock(&cluster->lock);
2807 if (bytes > cluster->max_size)
2810 if (cluster->block_group != block_group)
2813 node = rb_first(&cluster->root);
2817 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2819 if (entry->bytes < bytes)
2820 *max_extent_size = max(get_max_extent_size(entry),
2823 if (entry->bytes < bytes ||
2824 (!entry->bitmap && entry->offset < min_start)) {
2825 node = rb_next(&entry->offset_index);
2828 entry = rb_entry(node, struct btrfs_free_space,
2833 if (entry->bitmap) {
2834 ret = btrfs_alloc_from_bitmap(block_group,
2835 cluster, entry, bytes,
2836 cluster->window_start,
2839 node = rb_next(&entry->offset_index);
2842 entry = rb_entry(node, struct btrfs_free_space,
2846 cluster->window_start += bytes;
2848 ret = entry->offset;
2850 entry->offset += bytes;
2851 entry->bytes -= bytes;
2854 if (entry->bytes == 0)
2855 rb_erase(&entry->offset_index, &cluster->root);
2859 spin_unlock(&cluster->lock);
2864 spin_lock(&ctl->tree_lock);
2866 ctl->free_space -= bytes;
2867 if (entry->bytes == 0) {
2868 ctl->free_extents--;
2869 if (entry->bitmap) {
2870 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2872 ctl->total_bitmaps--;
2873 ctl->op->recalc_thresholds(ctl);
2875 kmem_cache_free(btrfs_free_space_cachep, entry);
2878 spin_unlock(&ctl->tree_lock);
2883 static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
2884 struct btrfs_free_space *entry,
2885 struct btrfs_free_cluster *cluster,
2886 u64 offset, u64 bytes,
2887 u64 cont1_bytes, u64 min_bytes)
2889 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2890 unsigned long next_zero;
2892 unsigned long want_bits;
2893 unsigned long min_bits;
2894 unsigned long found_bits;
2895 unsigned long max_bits = 0;
2896 unsigned long start = 0;
2897 unsigned long total_found = 0;
2900 i = offset_to_bit(entry->offset, ctl->unit,
2901 max_t(u64, offset, entry->offset));
2902 want_bits = bytes_to_bits(bytes, ctl->unit);
2903 min_bits = bytes_to_bits(min_bytes, ctl->unit);
2906 * Don't bother looking for a cluster in this bitmap if it's heavily
2909 if (entry->max_extent_size &&
2910 entry->max_extent_size < cont1_bytes)
2914 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
2915 next_zero = find_next_zero_bit(entry->bitmap,
2916 BITS_PER_BITMAP, i);
2917 if (next_zero - i >= min_bits) {
2918 found_bits = next_zero - i;
2919 if (found_bits > max_bits)
2920 max_bits = found_bits;
2923 if (next_zero - i > max_bits)
2924 max_bits = next_zero - i;
2929 entry->max_extent_size = (u64)max_bits * ctl->unit;
2935 cluster->max_size = 0;
2938 total_found += found_bits;
2940 if (cluster->max_size < found_bits * ctl->unit)
2941 cluster->max_size = found_bits * ctl->unit;
2943 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2948 cluster->window_start = start * ctl->unit + entry->offset;
2949 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2950 ret = tree_insert_offset(&cluster->root, entry->offset,
2951 &entry->offset_index, 1);
2952 ASSERT(!ret); /* -EEXIST; Logic error */
2954 trace_btrfs_setup_cluster(block_group, cluster,
2955 total_found * ctl->unit, 1);
2960 * This searches the block group for just extents to fill the cluster with.
2961 * Try to find a cluster with at least bytes total bytes, at least one
2962 * extent of cont1_bytes, and other clusters of at least min_bytes.
2965 setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
2966 struct btrfs_free_cluster *cluster,
2967 struct list_head *bitmaps, u64 offset, u64 bytes,
2968 u64 cont1_bytes, u64 min_bytes)
2970 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2971 struct btrfs_free_space *first = NULL;
2972 struct btrfs_free_space *entry = NULL;
2973 struct btrfs_free_space *last;
2974 struct rb_node *node;
2979 entry = tree_search_offset(ctl, offset, 0, 1);
2984 * We don't want bitmaps, so just move along until we find a normal
2987 while (entry->bitmap || entry->bytes < min_bytes) {
2988 if (entry->bitmap && list_empty(&entry->list))
2989 list_add_tail(&entry->list, bitmaps);
2990 node = rb_next(&entry->offset_index);
2993 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2996 window_free = entry->bytes;
2997 max_extent = entry->bytes;
3001 for (node = rb_next(&entry->offset_index); node;
3002 node = rb_next(&entry->offset_index)) {
3003 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3005 if (entry->bitmap) {
3006 if (list_empty(&entry->list))
3007 list_add_tail(&entry->list, bitmaps);
3011 if (entry->bytes < min_bytes)
3015 window_free += entry->bytes;
3016 if (entry->bytes > max_extent)
3017 max_extent = entry->bytes;
3020 if (window_free < bytes || max_extent < cont1_bytes)
3023 cluster->window_start = first->offset;
3025 node = &first->offset_index;
3028 * now we've found our entries, pull them out of the free space
3029 * cache and put them into the cluster rbtree
3034 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3035 node = rb_next(&entry->offset_index);
3036 if (entry->bitmap || entry->bytes < min_bytes)
3039 rb_erase(&entry->offset_index, &ctl->free_space_offset);
3040 ret = tree_insert_offset(&cluster->root, entry->offset,
3041 &entry->offset_index, 0);
3042 total_size += entry->bytes;
3043 ASSERT(!ret); /* -EEXIST; Logic error */
3044 } while (node && entry != last);
3046 cluster->max_size = max_extent;
3047 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
3052 * This specifically looks for bitmaps that may work in the cluster, we assume
3053 * that we have already failed to find extents that will work.
3056 setup_cluster_bitmap(struct btrfs_block_group *block_group,
3057 struct btrfs_free_cluster *cluster,
3058 struct list_head *bitmaps, u64 offset, u64 bytes,
3059 u64 cont1_bytes, u64 min_bytes)
3061 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3062 struct btrfs_free_space *entry = NULL;
3064 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
3066 if (ctl->total_bitmaps == 0)
3070 * The bitmap that covers offset won't be in the list unless offset
3071 * is just its start offset.
3073 if (!list_empty(bitmaps))
3074 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3076 if (!entry || entry->offset != bitmap_offset) {
3077 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3078 if (entry && list_empty(&entry->list))
3079 list_add(&entry->list, bitmaps);
3082 list_for_each_entry(entry, bitmaps, list) {
3083 if (entry->bytes < bytes)
3085 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
3086 bytes, cont1_bytes, min_bytes);
3092 * The bitmaps list has all the bitmaps that record free space
3093 * starting after offset, so no more search is required.
3099 * here we try to find a cluster of blocks in a block group. The goal
3100 * is to find at least bytes+empty_size.
3101 * We might not find them all in one contiguous area.
3103 * returns zero and sets up cluster if things worked out, otherwise
3104 * it returns -enospc
3106 int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
3107 struct btrfs_free_cluster *cluster,
3108 u64 offset, u64 bytes, u64 empty_size)
3110 struct btrfs_fs_info *fs_info = block_group->fs_info;
3111 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3112 struct btrfs_free_space *entry, *tmp;
3119 * Choose the minimum extent size we'll require for this
3120 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3121 * For metadata, allow allocates with smaller extents. For
3122 * data, keep it dense.
3124 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3125 cont1_bytes = min_bytes = bytes + empty_size;
3126 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3127 cont1_bytes = bytes;
3128 min_bytes = fs_info->sectorsize;
3130 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3131 min_bytes = fs_info->sectorsize;
3134 spin_lock(&ctl->tree_lock);
3137 * If we know we don't have enough space to make a cluster don't even
3138 * bother doing all the work to try and find one.
3140 if (ctl->free_space < bytes) {
3141 spin_unlock(&ctl->tree_lock);
3145 spin_lock(&cluster->lock);
3147 /* someone already found a cluster, hooray */
3148 if (cluster->block_group) {
3153 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3156 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3158 cont1_bytes, min_bytes);
3160 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3161 offset, bytes + empty_size,
3162 cont1_bytes, min_bytes);
3164 /* Clear our temporary list */
3165 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3166 list_del_init(&entry->list);
3169 atomic_inc(&block_group->count);
3170 list_add_tail(&cluster->block_group_list,
3171 &block_group->cluster_list);
3172 cluster->block_group = block_group;
3174 trace_btrfs_failed_cluster_setup(block_group);
3177 spin_unlock(&cluster->lock);
3178 spin_unlock(&ctl->tree_lock);
3184 * simple code to zero out a cluster
3186 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3188 spin_lock_init(&cluster->lock);
3189 spin_lock_init(&cluster->refill_lock);
3190 cluster->root = RB_ROOT;
3191 cluster->max_size = 0;
3192 cluster->fragmented = false;
3193 INIT_LIST_HEAD(&cluster->block_group_list);
3194 cluster->block_group = NULL;
3197 static int do_trimming(struct btrfs_block_group *block_group,
3198 u64 *total_trimmed, u64 start, u64 bytes,
3199 u64 reserved_start, u64 reserved_bytes,
3200 struct btrfs_trim_range *trim_entry)
3202 struct btrfs_space_info *space_info = block_group->space_info;
3203 struct btrfs_fs_info *fs_info = block_group->fs_info;
3204 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3209 spin_lock(&space_info->lock);
3210 spin_lock(&block_group->lock);
3211 if (!block_group->ro) {
3212 block_group->reserved += reserved_bytes;
3213 space_info->bytes_reserved += reserved_bytes;
3216 spin_unlock(&block_group->lock);
3217 spin_unlock(&space_info->lock);
3219 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3221 *total_trimmed += trimmed;
3223 mutex_lock(&ctl->cache_writeout_mutex);
3224 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
3225 list_del(&trim_entry->list);
3226 mutex_unlock(&ctl->cache_writeout_mutex);
3229 spin_lock(&space_info->lock);
3230 spin_lock(&block_group->lock);
3231 if (block_group->ro)
3232 space_info->bytes_readonly += reserved_bytes;
3233 block_group->reserved -= reserved_bytes;
3234 space_info->bytes_reserved -= reserved_bytes;
3235 spin_unlock(&block_group->lock);
3236 spin_unlock(&space_info->lock);
3242 static int trim_no_bitmap(struct btrfs_block_group *block_group,
3243 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3245 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3246 struct btrfs_free_space *entry;
3247 struct rb_node *node;
3253 while (start < end) {
3254 struct btrfs_trim_range trim_entry;
3256 mutex_lock(&ctl->cache_writeout_mutex);
3257 spin_lock(&ctl->tree_lock);
3259 if (ctl->free_space < minlen) {
3260 spin_unlock(&ctl->tree_lock);
3261 mutex_unlock(&ctl->cache_writeout_mutex);
3265 entry = tree_search_offset(ctl, start, 0, 1);
3267 spin_unlock(&ctl->tree_lock);
3268 mutex_unlock(&ctl->cache_writeout_mutex);
3273 while (entry->bitmap) {
3274 node = rb_next(&entry->offset_index);
3276 spin_unlock(&ctl->tree_lock);
3277 mutex_unlock(&ctl->cache_writeout_mutex);
3280 entry = rb_entry(node, struct btrfs_free_space,
3284 if (entry->offset >= end) {
3285 spin_unlock(&ctl->tree_lock);
3286 mutex_unlock(&ctl->cache_writeout_mutex);
3290 extent_start = entry->offset;
3291 extent_bytes = entry->bytes;
3292 start = max(start, extent_start);
3293 bytes = min(extent_start + extent_bytes, end) - start;
3294 if (bytes < minlen) {
3295 spin_unlock(&ctl->tree_lock);
3296 mutex_unlock(&ctl->cache_writeout_mutex);
3300 unlink_free_space(ctl, entry);
3301 kmem_cache_free(btrfs_free_space_cachep, entry);
3303 spin_unlock(&ctl->tree_lock);
3304 trim_entry.start = extent_start;
3305 trim_entry.bytes = extent_bytes;
3306 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3307 mutex_unlock(&ctl->cache_writeout_mutex);
3309 ret = do_trimming(block_group, total_trimmed, start, bytes,
3310 extent_start, extent_bytes, &trim_entry);
3316 if (fatal_signal_pending(current)) {
3327 static int trim_bitmaps(struct btrfs_block_group *block_group,
3328 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3330 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3331 struct btrfs_free_space *entry;
3335 u64 offset = offset_to_bitmap(ctl, start);
3337 while (offset < end) {
3338 bool next_bitmap = false;
3339 struct btrfs_trim_range trim_entry;
3341 mutex_lock(&ctl->cache_writeout_mutex);
3342 spin_lock(&ctl->tree_lock);
3344 if (ctl->free_space < minlen) {
3345 spin_unlock(&ctl->tree_lock);
3346 mutex_unlock(&ctl->cache_writeout_mutex);
3350 entry = tree_search_offset(ctl, offset, 1, 0);
3352 spin_unlock(&ctl->tree_lock);
3353 mutex_unlock(&ctl->cache_writeout_mutex);
3359 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3360 if (ret2 || start >= end) {
3361 spin_unlock(&ctl->tree_lock);
3362 mutex_unlock(&ctl->cache_writeout_mutex);
3367 bytes = min(bytes, end - start);
3368 if (bytes < minlen) {
3369 spin_unlock(&ctl->tree_lock);
3370 mutex_unlock(&ctl->cache_writeout_mutex);
3374 bitmap_clear_bits(ctl, entry, start, bytes);
3375 if (entry->bytes == 0)
3376 free_bitmap(ctl, entry);
3378 spin_unlock(&ctl->tree_lock);
3379 trim_entry.start = start;
3380 trim_entry.bytes = bytes;
3381 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3382 mutex_unlock(&ctl->cache_writeout_mutex);
3384 ret = do_trimming(block_group, total_trimmed, start, bytes,
3385 start, bytes, &trim_entry);
3390 offset += BITS_PER_BITMAP * ctl->unit;
3393 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3394 offset += BITS_PER_BITMAP * ctl->unit;
3397 if (fatal_signal_pending(current)) {
3408 void btrfs_get_block_group_trimming(struct btrfs_block_group *cache)
3410 atomic_inc(&cache->trimming);
3413 void btrfs_put_block_group_trimming(struct btrfs_block_group *block_group)
3415 struct btrfs_fs_info *fs_info = block_group->fs_info;
3416 struct extent_map_tree *em_tree;
3417 struct extent_map *em;
3420 spin_lock(&block_group->lock);
3421 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3422 block_group->removed);
3423 spin_unlock(&block_group->lock);
3426 mutex_lock(&fs_info->chunk_mutex);
3427 em_tree = &fs_info->mapping_tree;
3428 write_lock(&em_tree->lock);
3429 em = lookup_extent_mapping(em_tree, block_group->start,
3431 BUG_ON(!em); /* logic error, can't happen */
3432 remove_extent_mapping(em_tree, em);
3433 write_unlock(&em_tree->lock);
3434 mutex_unlock(&fs_info->chunk_mutex);
3436 /* once for us and once for the tree */
3437 free_extent_map(em);
3438 free_extent_map(em);
3441 * We've left one free space entry and other tasks trimming
3442 * this block group have left 1 entry each one. Free them.
3444 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3448 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
3449 u64 *trimmed, u64 start, u64 end, u64 minlen)
3455 spin_lock(&block_group->lock);
3456 if (block_group->removed) {
3457 spin_unlock(&block_group->lock);
3460 btrfs_get_block_group_trimming(block_group);
3461 spin_unlock(&block_group->lock);
3463 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3467 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3469 btrfs_put_block_group_trimming(block_group);
3474 * Find the left-most item in the cache tree, and then return the
3475 * smallest inode number in the item.
3477 * Note: the returned inode number may not be the smallest one in
3478 * the tree, if the left-most item is a bitmap.
3480 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3482 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3483 struct btrfs_free_space *entry = NULL;
3486 spin_lock(&ctl->tree_lock);
3488 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3491 entry = rb_entry(rb_first(&ctl->free_space_offset),
3492 struct btrfs_free_space, offset_index);
3494 if (!entry->bitmap) {
3495 ino = entry->offset;
3497 unlink_free_space(ctl, entry);
3501 kmem_cache_free(btrfs_free_space_cachep, entry);
3503 link_free_space(ctl, entry);
3509 ret = search_bitmap(ctl, entry, &offset, &count, true);
3510 /* Logic error; Should be empty if it can't find anything */
3514 bitmap_clear_bits(ctl, entry, offset, 1);
3515 if (entry->bytes == 0)
3516 free_bitmap(ctl, entry);
3519 spin_unlock(&ctl->tree_lock);
3524 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3525 struct btrfs_path *path)
3527 struct inode *inode = NULL;
3529 spin_lock(&root->ino_cache_lock);
3530 if (root->ino_cache_inode)
3531 inode = igrab(root->ino_cache_inode);
3532 spin_unlock(&root->ino_cache_lock);
3536 inode = __lookup_free_space_inode(root, path, 0);
3540 spin_lock(&root->ino_cache_lock);
3541 if (!btrfs_fs_closing(root->fs_info))
3542 root->ino_cache_inode = igrab(inode);
3543 spin_unlock(&root->ino_cache_lock);
3548 int create_free_ino_inode(struct btrfs_root *root,
3549 struct btrfs_trans_handle *trans,
3550 struct btrfs_path *path)
3552 return __create_free_space_inode(root, trans, path,
3553 BTRFS_FREE_INO_OBJECTID, 0);
3556 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3558 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3559 struct btrfs_path *path;
3560 struct inode *inode;
3562 u64 root_gen = btrfs_root_generation(&root->root_item);
3564 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3568 * If we're unmounting then just return, since this does a search on the
3569 * normal root and not the commit root and we could deadlock.
3571 if (btrfs_fs_closing(fs_info))
3574 path = btrfs_alloc_path();
3578 inode = lookup_free_ino_inode(root, path);
3582 if (root_gen != BTRFS_I(inode)->generation)
3585 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3589 "failed to load free ino cache for root %llu",
3590 root->root_key.objectid);
3594 btrfs_free_path(path);
3598 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3599 struct btrfs_trans_handle *trans,
3600 struct btrfs_path *path,
3601 struct inode *inode)
3603 struct btrfs_fs_info *fs_info = root->fs_info;
3604 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3606 struct btrfs_io_ctl io_ctl;
3607 bool release_metadata = true;
3609 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3612 memset(&io_ctl, 0, sizeof(io_ctl));
3613 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3616 * At this point writepages() didn't error out, so our metadata
3617 * reservation is released when the writeback finishes, at
3618 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3619 * with or without an error.
3621 release_metadata = false;
3622 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3626 if (release_metadata)
3627 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3628 inode->i_size, true);
3631 "failed to write free ino cache for root %llu",
3632 root->root_key.objectid);
3639 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3641 * Use this if you need to make a bitmap or extent entry specifically, it
3642 * doesn't do any of the merging that add_free_space does, this acts a lot like
3643 * how the free space cache loading stuff works, so you can get really weird
3646 int test_add_free_space_entry(struct btrfs_block_group *cache,
3647 u64 offset, u64 bytes, bool bitmap)
3649 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3650 struct btrfs_free_space *info = NULL, *bitmap_info;
3657 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3663 spin_lock(&ctl->tree_lock);
3664 info->offset = offset;
3665 info->bytes = bytes;
3666 info->max_extent_size = 0;
3667 ret = link_free_space(ctl, info);
3668 spin_unlock(&ctl->tree_lock);
3670 kmem_cache_free(btrfs_free_space_cachep, info);
3675 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
3677 kmem_cache_free(btrfs_free_space_cachep, info);
3682 spin_lock(&ctl->tree_lock);
3683 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3688 add_new_bitmap(ctl, info, offset);
3693 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3695 bytes -= bytes_added;
3696 offset += bytes_added;
3697 spin_unlock(&ctl->tree_lock);
3703 kmem_cache_free(btrfs_free_space_cachep, info);
3705 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
3710 * Checks to see if the given range is in the free space cache. This is really
3711 * just used to check the absence of space, so if there is free space in the
3712 * range at all we will return 1.
3714 int test_check_exists(struct btrfs_block_group *cache,
3715 u64 offset, u64 bytes)
3717 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3718 struct btrfs_free_space *info;
3721 spin_lock(&ctl->tree_lock);
3722 info = tree_search_offset(ctl, offset, 0, 0);
3724 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3732 u64 bit_off, bit_bytes;
3734 struct btrfs_free_space *tmp;
3737 bit_bytes = ctl->unit;
3738 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
3740 if (bit_off == offset) {
3743 } else if (bit_off > offset &&
3744 offset + bytes > bit_off) {
3750 n = rb_prev(&info->offset_index);
3752 tmp = rb_entry(n, struct btrfs_free_space,
3754 if (tmp->offset + tmp->bytes < offset)
3756 if (offset + bytes < tmp->offset) {
3757 n = rb_prev(&tmp->offset_index);
3764 n = rb_next(&info->offset_index);
3766 tmp = rb_entry(n, struct btrfs_free_space,
3768 if (offset + bytes < tmp->offset)
3770 if (tmp->offset + tmp->bytes < offset) {
3771 n = rb_next(&tmp->offset_index);
3782 if (info->offset == offset) {
3787 if (offset > info->offset && offset < info->offset + info->bytes)
3790 spin_unlock(&ctl->tree_lock);
3793 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */