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"
26 #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
27 #define MAX_CACHE_BYTES_PER_GIG SZ_64K
28 #define FORCE_EXTENT_THRESHOLD SZ_1M
30 struct btrfs_trim_range {
33 struct list_head list;
36 static int count_bitmap_extents(struct btrfs_free_space_ctl *ctl,
37 struct btrfs_free_space *bitmap_info);
38 static int link_free_space(struct btrfs_free_space_ctl *ctl,
39 struct btrfs_free_space *info);
40 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
41 struct btrfs_free_space *info);
42 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
43 struct btrfs_trans_handle *trans,
44 struct btrfs_io_ctl *io_ctl,
45 struct btrfs_path *path);
47 static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
48 struct btrfs_path *path,
51 struct btrfs_fs_info *fs_info = root->fs_info;
53 struct btrfs_key location;
54 struct btrfs_disk_key disk_key;
55 struct btrfs_free_space_header *header;
56 struct extent_buffer *leaf;
57 struct inode *inode = NULL;
61 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
65 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
69 btrfs_release_path(path);
70 return ERR_PTR(-ENOENT);
73 leaf = path->nodes[0];
74 header = btrfs_item_ptr(leaf, path->slots[0],
75 struct btrfs_free_space_header);
76 btrfs_free_space_key(leaf, header, &disk_key);
77 btrfs_disk_key_to_cpu(&location, &disk_key);
78 btrfs_release_path(path);
81 * We are often under a trans handle at this point, so we need to make
82 * sure NOFS is set to keep us from deadlocking.
84 nofs_flag = memalloc_nofs_save();
85 inode = btrfs_iget_path(fs_info->sb, location.objectid, root, path);
86 btrfs_release_path(path);
87 memalloc_nofs_restore(nofs_flag);
91 mapping_set_gfp_mask(inode->i_mapping,
92 mapping_gfp_constraint(inode->i_mapping,
93 ~(__GFP_FS | __GFP_HIGHMEM)));
98 struct inode *lookup_free_space_inode(struct btrfs_block_group *block_group,
99 struct btrfs_path *path)
101 struct btrfs_fs_info *fs_info = block_group->fs_info;
102 struct inode *inode = NULL;
103 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
105 spin_lock(&block_group->lock);
106 if (block_group->inode)
107 inode = igrab(block_group->inode);
108 spin_unlock(&block_group->lock);
112 inode = __lookup_free_space_inode(fs_info->tree_root, path,
117 spin_lock(&block_group->lock);
118 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
119 btrfs_info(fs_info, "Old style space inode found, converting.");
120 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
121 BTRFS_INODE_NODATACOW;
122 block_group->disk_cache_state = BTRFS_DC_CLEAR;
125 if (!block_group->iref) {
126 block_group->inode = igrab(inode);
127 block_group->iref = 1;
129 spin_unlock(&block_group->lock);
134 static int __create_free_space_inode(struct btrfs_root *root,
135 struct btrfs_trans_handle *trans,
136 struct btrfs_path *path,
139 struct btrfs_key key;
140 struct btrfs_disk_key disk_key;
141 struct btrfs_free_space_header *header;
142 struct btrfs_inode_item *inode_item;
143 struct extent_buffer *leaf;
144 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
147 ret = btrfs_insert_empty_inode(trans, root, path, ino);
151 /* We inline crc's for the free disk space cache */
152 if (ino != BTRFS_FREE_INO_OBJECTID)
153 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
155 leaf = path->nodes[0];
156 inode_item = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_inode_item);
158 btrfs_item_key(leaf, &disk_key, path->slots[0]);
159 memzero_extent_buffer(leaf, (unsigned long)inode_item,
160 sizeof(*inode_item));
161 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
162 btrfs_set_inode_size(leaf, inode_item, 0);
163 btrfs_set_inode_nbytes(leaf, inode_item, 0);
164 btrfs_set_inode_uid(leaf, inode_item, 0);
165 btrfs_set_inode_gid(leaf, inode_item, 0);
166 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
167 btrfs_set_inode_flags(leaf, inode_item, flags);
168 btrfs_set_inode_nlink(leaf, inode_item, 1);
169 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
170 btrfs_set_inode_block_group(leaf, inode_item, offset);
171 btrfs_mark_buffer_dirty(leaf);
172 btrfs_release_path(path);
174 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
177 ret = btrfs_insert_empty_item(trans, root, path, &key,
178 sizeof(struct btrfs_free_space_header));
180 btrfs_release_path(path);
184 leaf = path->nodes[0];
185 header = btrfs_item_ptr(leaf, path->slots[0],
186 struct btrfs_free_space_header);
187 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
188 btrfs_set_free_space_key(leaf, header, &disk_key);
189 btrfs_mark_buffer_dirty(leaf);
190 btrfs_release_path(path);
195 int create_free_space_inode(struct btrfs_trans_handle *trans,
196 struct btrfs_block_group *block_group,
197 struct btrfs_path *path)
202 ret = btrfs_find_free_objectid(trans->fs_info->tree_root, &ino);
206 return __create_free_space_inode(trans->fs_info->tree_root, trans, path,
207 ino, block_group->start);
210 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
211 struct btrfs_block_rsv *rsv)
216 /* 1 for slack space, 1 for updating the inode */
217 needed_bytes = btrfs_calc_insert_metadata_size(fs_info, 1) +
218 btrfs_calc_metadata_size(fs_info, 1);
220 spin_lock(&rsv->lock);
221 if (rsv->reserved < needed_bytes)
225 spin_unlock(&rsv->lock);
229 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
230 struct btrfs_block_group *block_group,
233 struct btrfs_root *root = BTRFS_I(inode)->root;
238 struct btrfs_path *path = btrfs_alloc_path();
245 mutex_lock(&trans->transaction->cache_write_mutex);
246 if (!list_empty(&block_group->io_list)) {
247 list_del_init(&block_group->io_list);
249 btrfs_wait_cache_io(trans, block_group, path);
250 btrfs_put_block_group(block_group);
254 * now that we've truncated the cache away, its no longer
257 spin_lock(&block_group->lock);
258 block_group->disk_cache_state = BTRFS_DC_CLEAR;
259 spin_unlock(&block_group->lock);
260 btrfs_free_path(path);
263 btrfs_i_size_write(BTRFS_I(inode), 0);
264 truncate_pagecache(inode, 0);
267 * We skip the throttling logic for free space cache inodes, so we don't
268 * need to check for -EAGAIN.
270 ret = btrfs_truncate_inode_items(trans, root, inode,
271 0, BTRFS_EXTENT_DATA_KEY);
275 ret = btrfs_update_inode(trans, root, inode);
279 mutex_unlock(&trans->transaction->cache_write_mutex);
281 btrfs_abort_transaction(trans, ret);
286 static void readahead_cache(struct inode *inode)
288 struct file_ra_state *ra;
289 unsigned long last_index;
291 ra = kzalloc(sizeof(*ra), GFP_NOFS);
295 file_ra_state_init(ra, inode->i_mapping);
296 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
298 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
303 static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
309 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
311 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
314 /* Make sure we can fit our crcs and generation into the first page */
315 if (write && check_crcs &&
316 (num_pages * sizeof(u32) + sizeof(u64)) > PAGE_SIZE)
319 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
321 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
325 io_ctl->num_pages = num_pages;
326 io_ctl->fs_info = btrfs_sb(inode->i_sb);
327 io_ctl->check_crcs = check_crcs;
328 io_ctl->inode = inode;
332 ALLOW_ERROR_INJECTION(io_ctl_init, ERRNO);
334 static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
336 kfree(io_ctl->pages);
337 io_ctl->pages = NULL;
340 static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
348 static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
350 ASSERT(io_ctl->index < io_ctl->num_pages);
351 io_ctl->page = io_ctl->pages[io_ctl->index++];
352 io_ctl->cur = page_address(io_ctl->page);
353 io_ctl->orig = io_ctl->cur;
354 io_ctl->size = PAGE_SIZE;
356 clear_page(io_ctl->cur);
359 static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
363 io_ctl_unmap_page(io_ctl);
365 for (i = 0; i < io_ctl->num_pages; i++) {
366 if (io_ctl->pages[i]) {
367 ClearPageChecked(io_ctl->pages[i]);
368 unlock_page(io_ctl->pages[i]);
369 put_page(io_ctl->pages[i]);
374 static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, bool uptodate)
377 struct inode *inode = io_ctl->inode;
378 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
381 for (i = 0; i < io_ctl->num_pages; i++) {
382 page = find_or_create_page(inode->i_mapping, i, mask);
384 io_ctl_drop_pages(io_ctl);
387 io_ctl->pages[i] = page;
388 if (uptodate && !PageUptodate(page)) {
389 btrfs_readpage(NULL, page);
391 if (page->mapping != inode->i_mapping) {
392 btrfs_err(BTRFS_I(inode)->root->fs_info,
393 "free space cache page truncated");
394 io_ctl_drop_pages(io_ctl);
397 if (!PageUptodate(page)) {
398 btrfs_err(BTRFS_I(inode)->root->fs_info,
399 "error reading free space cache");
400 io_ctl_drop_pages(io_ctl);
406 for (i = 0; i < io_ctl->num_pages; i++) {
407 clear_page_dirty_for_io(io_ctl->pages[i]);
408 set_page_extent_mapped(io_ctl->pages[i]);
414 static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
416 io_ctl_map_page(io_ctl, 1);
419 * Skip the csum areas. If we don't check crcs then we just have a
420 * 64bit chunk at the front of the first page.
422 if (io_ctl->check_crcs) {
423 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
424 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
426 io_ctl->cur += sizeof(u64);
427 io_ctl->size -= sizeof(u64) * 2;
430 put_unaligned_le64(generation, io_ctl->cur);
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;
451 cache_gen = get_unaligned_le64(io_ctl->cur);
452 if (cache_gen != generation) {
453 btrfs_err_rl(io_ctl->fs_info,
454 "space cache generation (%llu) does not match inode (%llu)",
455 cache_gen, generation);
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 put_unaligned_le64(offset, &entry->offset);
526 put_unaligned_le64(bytes, &entry->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 = get_unaligned_le64(&e->offset);
600 entry->bytes = get_unaligned_le64(&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, true);
736 ret = io_ctl_check_crc(&io_ctl, 0);
740 ret = io_ctl_check_generation(&io_ctl, generation);
744 while (num_entries) {
745 e = kmem_cache_zalloc(btrfs_free_space_cachep,
752 ret = io_ctl_read_entry(&io_ctl, e, &type);
754 kmem_cache_free(btrfs_free_space_cachep, e);
759 * Sync discard ensures that the free space cache is always
760 * trimmed. So when reading this in, the state should reflect
761 * that. We also do this for async as a stop gap for lack of
764 if (btrfs_test_opt(fs_info, DISCARD_SYNC) ||
765 btrfs_test_opt(fs_info, DISCARD_ASYNC))
766 e->trim_state = BTRFS_TRIM_STATE_TRIMMED;
770 kmem_cache_free(btrfs_free_space_cachep, e);
774 if (type == BTRFS_FREE_SPACE_EXTENT) {
775 spin_lock(&ctl->tree_lock);
776 ret = link_free_space(ctl, e);
777 spin_unlock(&ctl->tree_lock);
780 "Duplicate entries in free space cache, dumping");
781 kmem_cache_free(btrfs_free_space_cachep, e);
787 e->bitmap = kmem_cache_zalloc(
788 btrfs_free_space_bitmap_cachep, GFP_NOFS);
792 btrfs_free_space_cachep, e);
795 spin_lock(&ctl->tree_lock);
796 ret = link_free_space(ctl, e);
798 spin_unlock(&ctl->tree_lock);
800 "Duplicate entries in free space cache, dumping");
801 kmem_cache_free(btrfs_free_space_cachep, e);
804 ctl->total_bitmaps++;
805 ctl->op->recalc_thresholds(ctl);
806 spin_unlock(&ctl->tree_lock);
807 list_add_tail(&e->list, &bitmaps);
813 io_ctl_unmap_page(&io_ctl);
816 * We add the bitmaps at the end of the entries in order that
817 * the bitmap entries are added to the cache.
819 list_for_each_entry_safe(e, n, &bitmaps, list) {
820 list_del_init(&e->list);
821 ret = io_ctl_read_bitmap(&io_ctl, e);
824 e->bitmap_extents = count_bitmap_extents(ctl, e);
825 if (!btrfs_free_space_trimmed(e)) {
826 ctl->discardable_extents[BTRFS_STAT_CURR] +=
828 ctl->discardable_bytes[BTRFS_STAT_CURR] += e->bytes;
832 io_ctl_drop_pages(&io_ctl);
833 merge_space_tree(ctl);
836 btrfs_discard_update_discardable(ctl->private, ctl);
837 io_ctl_free(&io_ctl);
840 io_ctl_drop_pages(&io_ctl);
841 __btrfs_remove_free_space_cache(ctl);
845 int load_free_space_cache(struct btrfs_block_group *block_group)
847 struct btrfs_fs_info *fs_info = block_group->fs_info;
848 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
850 struct btrfs_path *path;
853 u64 used = block_group->used;
856 * If this block group has been marked to be cleared for one reason or
857 * another then we can't trust the on disk cache, so just return.
859 spin_lock(&block_group->lock);
860 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
861 spin_unlock(&block_group->lock);
864 spin_unlock(&block_group->lock);
866 path = btrfs_alloc_path();
869 path->search_commit_root = 1;
870 path->skip_locking = 1;
873 * We must pass a path with search_commit_root set to btrfs_iget in
874 * order to avoid a deadlock when allocating extents for the tree root.
876 * When we are COWing an extent buffer from the tree root, when looking
877 * for a free extent, at extent-tree.c:find_free_extent(), we can find
878 * block group without its free space cache loaded. When we find one
879 * we must load its space cache which requires reading its free space
880 * cache's inode item from the root tree. If this inode item is located
881 * in the same leaf that we started COWing before, then we end up in
882 * deadlock on the extent buffer (trying to read lock it when we
883 * previously write locked it).
885 * It's safe to read the inode item using the commit root because
886 * block groups, once loaded, stay in memory forever (until they are
887 * removed) as well as their space caches once loaded. New block groups
888 * once created get their ->cached field set to BTRFS_CACHE_FINISHED so
889 * we will never try to read their inode item while the fs is mounted.
891 inode = lookup_free_space_inode(block_group, path);
893 btrfs_free_path(path);
897 /* We may have converted the inode and made the cache invalid. */
898 spin_lock(&block_group->lock);
899 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
900 spin_unlock(&block_group->lock);
901 btrfs_free_path(path);
904 spin_unlock(&block_group->lock);
906 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
907 path, block_group->start);
908 btrfs_free_path(path);
912 spin_lock(&ctl->tree_lock);
913 matched = (ctl->free_space == (block_group->length - used -
914 block_group->bytes_super));
915 spin_unlock(&ctl->tree_lock);
918 __btrfs_remove_free_space_cache(ctl);
920 "block group %llu has wrong amount of free space",
926 /* This cache is bogus, make sure it gets cleared */
927 spin_lock(&block_group->lock);
928 block_group->disk_cache_state = BTRFS_DC_CLEAR;
929 spin_unlock(&block_group->lock);
933 "failed to load free space cache for block group %llu, rebuilding it now",
941 static noinline_for_stack
942 int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
943 struct btrfs_free_space_ctl *ctl,
944 struct btrfs_block_group *block_group,
945 int *entries, int *bitmaps,
946 struct list_head *bitmap_list)
949 struct btrfs_free_cluster *cluster = NULL;
950 struct btrfs_free_cluster *cluster_locked = NULL;
951 struct rb_node *node = rb_first(&ctl->free_space_offset);
952 struct btrfs_trim_range *trim_entry;
954 /* Get the cluster for this block_group if it exists */
955 if (block_group && !list_empty(&block_group->cluster_list)) {
956 cluster = list_entry(block_group->cluster_list.next,
957 struct btrfs_free_cluster,
961 if (!node && cluster) {
962 cluster_locked = cluster;
963 spin_lock(&cluster_locked->lock);
964 node = rb_first(&cluster->root);
968 /* Write out the extent entries */
970 struct btrfs_free_space *e;
972 e = rb_entry(node, struct btrfs_free_space, offset_index);
975 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
981 list_add_tail(&e->list, bitmap_list);
984 node = rb_next(node);
985 if (!node && cluster) {
986 node = rb_first(&cluster->root);
987 cluster_locked = cluster;
988 spin_lock(&cluster_locked->lock);
992 if (cluster_locked) {
993 spin_unlock(&cluster_locked->lock);
994 cluster_locked = NULL;
998 * Make sure we don't miss any range that was removed from our rbtree
999 * because trimming is running. Otherwise after a umount+mount (or crash
1000 * after committing the transaction) we would leak free space and get
1001 * an inconsistent free space cache report from fsck.
1003 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
1004 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
1005 trim_entry->bytes, NULL);
1014 spin_unlock(&cluster_locked->lock);
1018 static noinline_for_stack int
1019 update_cache_item(struct btrfs_trans_handle *trans,
1020 struct btrfs_root *root,
1021 struct inode *inode,
1022 struct btrfs_path *path, u64 offset,
1023 int entries, int bitmaps)
1025 struct btrfs_key key;
1026 struct btrfs_free_space_header *header;
1027 struct extent_buffer *leaf;
1030 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1031 key.offset = offset;
1034 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1036 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1037 EXTENT_DELALLOC, 0, 0, NULL);
1040 leaf = path->nodes[0];
1042 struct btrfs_key found_key;
1043 ASSERT(path->slots[0]);
1045 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1046 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1047 found_key.offset != offset) {
1048 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1049 inode->i_size - 1, EXTENT_DELALLOC, 0,
1051 btrfs_release_path(path);
1056 BTRFS_I(inode)->generation = trans->transid;
1057 header = btrfs_item_ptr(leaf, path->slots[0],
1058 struct btrfs_free_space_header);
1059 btrfs_set_free_space_entries(leaf, header, entries);
1060 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1061 btrfs_set_free_space_generation(leaf, header, trans->transid);
1062 btrfs_mark_buffer_dirty(leaf);
1063 btrfs_release_path(path);
1071 static noinline_for_stack int write_pinned_extent_entries(
1072 struct btrfs_trans_handle *trans,
1073 struct btrfs_block_group *block_group,
1074 struct btrfs_io_ctl *io_ctl,
1077 u64 start, extent_start, extent_end, len;
1078 struct extent_io_tree *unpin = NULL;
1085 * We want to add any pinned extents to our free space cache
1086 * so we don't leak the space
1088 * We shouldn't have switched the pinned extents yet so this is the
1091 unpin = &trans->transaction->pinned_extents;
1093 start = block_group->start;
1095 while (start < block_group->start + block_group->length) {
1096 ret = find_first_extent_bit(unpin, start,
1097 &extent_start, &extent_end,
1098 EXTENT_DIRTY, NULL);
1102 /* This pinned extent is out of our range */
1103 if (extent_start >= block_group->start + block_group->length)
1106 extent_start = max(extent_start, start);
1107 extent_end = min(block_group->start + block_group->length,
1109 len = extent_end - extent_start;
1112 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1122 static noinline_for_stack int
1123 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
1125 struct btrfs_free_space *entry, *next;
1128 /* Write out the bitmaps */
1129 list_for_each_entry_safe(entry, next, bitmap_list, list) {
1130 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1133 list_del_init(&entry->list);
1139 static int flush_dirty_cache(struct inode *inode)
1143 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1145 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1146 EXTENT_DELALLOC, 0, 0, NULL);
1151 static void noinline_for_stack
1152 cleanup_bitmap_list(struct list_head *bitmap_list)
1154 struct btrfs_free_space *entry, *next;
1156 list_for_each_entry_safe(entry, next, bitmap_list, list)
1157 list_del_init(&entry->list);
1160 static void noinline_for_stack
1161 cleanup_write_cache_enospc(struct inode *inode,
1162 struct btrfs_io_ctl *io_ctl,
1163 struct extent_state **cached_state)
1165 io_ctl_drop_pages(io_ctl);
1166 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1167 i_size_read(inode) - 1, cached_state);
1170 static int __btrfs_wait_cache_io(struct btrfs_root *root,
1171 struct btrfs_trans_handle *trans,
1172 struct btrfs_block_group *block_group,
1173 struct btrfs_io_ctl *io_ctl,
1174 struct btrfs_path *path, u64 offset)
1177 struct inode *inode = io_ctl->inode;
1182 /* Flush the dirty pages in the cache file. */
1183 ret = flush_dirty_cache(inode);
1187 /* Update the cache item to tell everyone this cache file is valid. */
1188 ret = update_cache_item(trans, root, inode, path, offset,
1189 io_ctl->entries, io_ctl->bitmaps);
1192 invalidate_inode_pages2(inode->i_mapping);
1193 BTRFS_I(inode)->generation = 0;
1195 btrfs_debug(root->fs_info,
1196 "failed to write free space cache for block group %llu error %d",
1197 block_group->start, ret);
1199 btrfs_update_inode(trans, root, inode);
1202 /* the dirty list is protected by the dirty_bgs_lock */
1203 spin_lock(&trans->transaction->dirty_bgs_lock);
1205 /* the disk_cache_state is protected by the block group lock */
1206 spin_lock(&block_group->lock);
1209 * only mark this as written if we didn't get put back on
1210 * the dirty list while waiting for IO. Otherwise our
1211 * cache state won't be right, and we won't get written again
1213 if (!ret && list_empty(&block_group->dirty_list))
1214 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1216 block_group->disk_cache_state = BTRFS_DC_ERROR;
1218 spin_unlock(&block_group->lock);
1219 spin_unlock(&trans->transaction->dirty_bgs_lock);
1220 io_ctl->inode = NULL;
1228 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1229 struct btrfs_trans_handle *trans,
1230 struct btrfs_io_ctl *io_ctl,
1231 struct btrfs_path *path)
1233 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1236 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1237 struct btrfs_block_group *block_group,
1238 struct btrfs_path *path)
1240 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1241 block_group, &block_group->io_ctl,
1242 path, block_group->start);
1246 * __btrfs_write_out_cache - write out cached info to an inode
1247 * @root - the root the inode belongs to
1248 * @ctl - the free space cache we are going to write out
1249 * @block_group - the block_group for this cache if it belongs to a block_group
1250 * @trans - the trans handle
1252 * This function writes out a free space cache struct to disk for quick recovery
1253 * on mount. This will return 0 if it was successful in writing the cache out,
1254 * or an errno if it was not.
1256 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1257 struct btrfs_free_space_ctl *ctl,
1258 struct btrfs_block_group *block_group,
1259 struct btrfs_io_ctl *io_ctl,
1260 struct btrfs_trans_handle *trans)
1262 struct extent_state *cached_state = NULL;
1263 LIST_HEAD(bitmap_list);
1269 if (!i_size_read(inode))
1272 WARN_ON(io_ctl->pages);
1273 ret = io_ctl_init(io_ctl, inode, 1);
1277 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1278 down_write(&block_group->data_rwsem);
1279 spin_lock(&block_group->lock);
1280 if (block_group->delalloc_bytes) {
1281 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1282 spin_unlock(&block_group->lock);
1283 up_write(&block_group->data_rwsem);
1284 BTRFS_I(inode)->generation = 0;
1289 spin_unlock(&block_group->lock);
1292 /* Lock all pages first so we can lock the extent safely. */
1293 ret = io_ctl_prepare_pages(io_ctl, false);
1297 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1300 io_ctl_set_generation(io_ctl, trans->transid);
1302 mutex_lock(&ctl->cache_writeout_mutex);
1303 /* Write out the extent entries in the free space cache */
1304 spin_lock(&ctl->tree_lock);
1305 ret = write_cache_extent_entries(io_ctl, ctl,
1306 block_group, &entries, &bitmaps,
1309 goto out_nospc_locked;
1312 * Some spaces that are freed in the current transaction are pinned,
1313 * they will be added into free space cache after the transaction is
1314 * committed, we shouldn't lose them.
1316 * If this changes while we are working we'll get added back to
1317 * the dirty list and redo it. No locking needed
1319 ret = write_pinned_extent_entries(trans, block_group, io_ctl, &entries);
1321 goto out_nospc_locked;
1324 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1325 * locked while doing it because a concurrent trim can be manipulating
1326 * or freeing the bitmap.
1328 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1329 spin_unlock(&ctl->tree_lock);
1330 mutex_unlock(&ctl->cache_writeout_mutex);
1334 /* Zero out the rest of the pages just to make sure */
1335 io_ctl_zero_remaining_pages(io_ctl);
1337 /* Everything is written out, now we dirty the pages in the file. */
1338 ret = btrfs_dirty_pages(BTRFS_I(inode), io_ctl->pages,
1339 io_ctl->num_pages, 0, i_size_read(inode),
1344 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1345 up_write(&block_group->data_rwsem);
1347 * Release the pages and unlock the extent, we will flush
1350 io_ctl_drop_pages(io_ctl);
1351 io_ctl_free(io_ctl);
1353 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1354 i_size_read(inode) - 1, &cached_state);
1357 * at this point the pages are under IO and we're happy,
1358 * The caller is responsible for waiting on them and updating
1359 * the cache and the inode
1361 io_ctl->entries = entries;
1362 io_ctl->bitmaps = bitmaps;
1364 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1371 cleanup_bitmap_list(&bitmap_list);
1372 spin_unlock(&ctl->tree_lock);
1373 mutex_unlock(&ctl->cache_writeout_mutex);
1376 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1379 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1380 up_write(&block_group->data_rwsem);
1383 io_ctl->inode = NULL;
1384 io_ctl_free(io_ctl);
1386 invalidate_inode_pages2(inode->i_mapping);
1387 BTRFS_I(inode)->generation = 0;
1389 btrfs_update_inode(trans, root, inode);
1395 int btrfs_write_out_cache(struct btrfs_trans_handle *trans,
1396 struct btrfs_block_group *block_group,
1397 struct btrfs_path *path)
1399 struct btrfs_fs_info *fs_info = trans->fs_info;
1400 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1401 struct inode *inode;
1404 spin_lock(&block_group->lock);
1405 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1406 spin_unlock(&block_group->lock);
1409 spin_unlock(&block_group->lock);
1411 inode = lookup_free_space_inode(block_group, path);
1415 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1416 block_group, &block_group->io_ctl, trans);
1418 btrfs_debug(fs_info,
1419 "failed to write free space cache for block group %llu error %d",
1420 block_group->start, ret);
1421 spin_lock(&block_group->lock);
1422 block_group->disk_cache_state = BTRFS_DC_ERROR;
1423 spin_unlock(&block_group->lock);
1425 block_group->io_ctl.inode = NULL;
1430 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1431 * to wait for IO and put the inode
1437 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1440 ASSERT(offset >= bitmap_start);
1441 offset -= bitmap_start;
1442 return (unsigned long)(div_u64(offset, unit));
1445 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1447 return (unsigned long)(div_u64(bytes, unit));
1450 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1454 u64 bytes_per_bitmap;
1456 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1457 bitmap_start = offset - ctl->start;
1458 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1459 bitmap_start *= bytes_per_bitmap;
1460 bitmap_start += ctl->start;
1462 return bitmap_start;
1465 static int tree_insert_offset(struct rb_root *root, u64 offset,
1466 struct rb_node *node, int bitmap)
1468 struct rb_node **p = &root->rb_node;
1469 struct rb_node *parent = NULL;
1470 struct btrfs_free_space *info;
1474 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1476 if (offset < info->offset) {
1478 } else if (offset > info->offset) {
1479 p = &(*p)->rb_right;
1482 * we could have a bitmap entry and an extent entry
1483 * share the same offset. If this is the case, we want
1484 * the extent entry to always be found first if we do a
1485 * linear search through the tree, since we want to have
1486 * the quickest allocation time, and allocating from an
1487 * extent is faster than allocating from a bitmap. So
1488 * if we're inserting a bitmap and we find an entry at
1489 * this offset, we want to go right, or after this entry
1490 * logically. If we are inserting an extent and we've
1491 * found a bitmap, we want to go left, or before
1499 p = &(*p)->rb_right;
1501 if (!info->bitmap) {
1510 rb_link_node(node, parent, p);
1511 rb_insert_color(node, root);
1517 * searches the tree for the given offset.
1519 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1520 * want a section that has at least bytes size and comes at or after the given
1523 static struct btrfs_free_space *
1524 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1525 u64 offset, int bitmap_only, int fuzzy)
1527 struct rb_node *n = ctl->free_space_offset.rb_node;
1528 struct btrfs_free_space *entry, *prev = NULL;
1530 /* find entry that is closest to the 'offset' */
1537 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1540 if (offset < entry->offset)
1542 else if (offset > entry->offset)
1555 * bitmap entry and extent entry may share same offset,
1556 * in that case, bitmap entry comes after extent entry.
1561 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1562 if (entry->offset != offset)
1565 WARN_ON(!entry->bitmap);
1568 if (entry->bitmap) {
1570 * if previous extent entry covers the offset,
1571 * we should return it instead of the bitmap entry
1573 n = rb_prev(&entry->offset_index);
1575 prev = rb_entry(n, struct btrfs_free_space,
1577 if (!prev->bitmap &&
1578 prev->offset + prev->bytes > offset)
1588 /* find last entry before the 'offset' */
1590 if (entry->offset > offset) {
1591 n = rb_prev(&entry->offset_index);
1593 entry = rb_entry(n, struct btrfs_free_space,
1595 ASSERT(entry->offset <= offset);
1604 if (entry->bitmap) {
1605 n = rb_prev(&entry->offset_index);
1607 prev = rb_entry(n, struct btrfs_free_space,
1609 if (!prev->bitmap &&
1610 prev->offset + prev->bytes > offset)
1613 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1615 } else if (entry->offset + entry->bytes > offset)
1622 if (entry->bitmap) {
1623 if (entry->offset + BITS_PER_BITMAP *
1627 if (entry->offset + entry->bytes > offset)
1631 n = rb_next(&entry->offset_index);
1634 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1640 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1641 struct btrfs_free_space *info)
1643 rb_erase(&info->offset_index, &ctl->free_space_offset);
1644 ctl->free_extents--;
1646 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
1647 ctl->discardable_extents[BTRFS_STAT_CURR]--;
1648 ctl->discardable_bytes[BTRFS_STAT_CURR] -= info->bytes;
1652 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1653 struct btrfs_free_space *info)
1655 __unlink_free_space(ctl, info);
1656 ctl->free_space -= info->bytes;
1659 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1660 struct btrfs_free_space *info)
1664 ASSERT(info->bytes || info->bitmap);
1665 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1666 &info->offset_index, (info->bitmap != NULL));
1670 if (!info->bitmap && !btrfs_free_space_trimmed(info)) {
1671 ctl->discardable_extents[BTRFS_STAT_CURR]++;
1672 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
1675 ctl->free_space += info->bytes;
1676 ctl->free_extents++;
1680 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1682 struct btrfs_block_group *block_group = ctl->private;
1686 u64 size = block_group->length;
1687 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1688 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1690 max_bitmaps = max_t(u64, max_bitmaps, 1);
1692 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1695 * We are trying to keep the total amount of memory used per 1GiB of
1696 * space to be MAX_CACHE_BYTES_PER_GIG. However, with a reclamation
1697 * mechanism of pulling extents >= FORCE_EXTENT_THRESHOLD out of
1698 * bitmaps, we may end up using more memory than this.
1701 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1703 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1705 bitmap_bytes = ctl->total_bitmaps * ctl->unit;
1708 * we want the extent entry threshold to always be at most 1/2 the max
1709 * bytes we can have, or whatever is less than that.
1711 extent_bytes = max_bytes - bitmap_bytes;
1712 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1714 ctl->extents_thresh =
1715 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1718 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1719 struct btrfs_free_space *info,
1720 u64 offset, u64 bytes)
1722 unsigned long start, count, end;
1723 int extent_delta = -1;
1725 start = offset_to_bit(info->offset, ctl->unit, offset);
1726 count = bytes_to_bits(bytes, ctl->unit);
1727 end = start + count;
1728 ASSERT(end <= BITS_PER_BITMAP);
1730 bitmap_clear(info->bitmap, start, count);
1732 info->bytes -= bytes;
1733 if (info->max_extent_size > ctl->unit)
1734 info->max_extent_size = 0;
1736 if (start && test_bit(start - 1, info->bitmap))
1739 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1742 info->bitmap_extents += extent_delta;
1743 if (!btrfs_free_space_trimmed(info)) {
1744 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
1745 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
1749 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1750 struct btrfs_free_space *info, u64 offset,
1753 __bitmap_clear_bits(ctl, info, offset, bytes);
1754 ctl->free_space -= bytes;
1757 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1758 struct btrfs_free_space *info, u64 offset,
1761 unsigned long start, count, end;
1762 int extent_delta = 1;
1764 start = offset_to_bit(info->offset, ctl->unit, offset);
1765 count = bytes_to_bits(bytes, ctl->unit);
1766 end = start + count;
1767 ASSERT(end <= BITS_PER_BITMAP);
1769 bitmap_set(info->bitmap, start, count);
1771 info->bytes += bytes;
1772 ctl->free_space += bytes;
1774 if (start && test_bit(start - 1, info->bitmap))
1777 if (end < BITS_PER_BITMAP && test_bit(end, info->bitmap))
1780 info->bitmap_extents += extent_delta;
1781 if (!btrfs_free_space_trimmed(info)) {
1782 ctl->discardable_extents[BTRFS_STAT_CURR] += extent_delta;
1783 ctl->discardable_bytes[BTRFS_STAT_CURR] += bytes;
1788 * If we can not find suitable extent, we will use bytes to record
1789 * the size of the max extent.
1791 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1792 struct btrfs_free_space *bitmap_info, u64 *offset,
1793 u64 *bytes, bool for_alloc)
1795 unsigned long found_bits = 0;
1796 unsigned long max_bits = 0;
1797 unsigned long bits, i;
1798 unsigned long next_zero;
1799 unsigned long extent_bits;
1802 * Skip searching the bitmap if we don't have a contiguous section that
1803 * is large enough for this allocation.
1806 bitmap_info->max_extent_size &&
1807 bitmap_info->max_extent_size < *bytes) {
1808 *bytes = bitmap_info->max_extent_size;
1812 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1813 max_t(u64, *offset, bitmap_info->offset));
1814 bits = bytes_to_bits(*bytes, ctl->unit);
1816 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1817 if (for_alloc && bits == 1) {
1821 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1822 BITS_PER_BITMAP, i);
1823 extent_bits = next_zero - i;
1824 if (extent_bits >= bits) {
1825 found_bits = extent_bits;
1827 } else if (extent_bits > max_bits) {
1828 max_bits = extent_bits;
1834 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1835 *bytes = (u64)(found_bits) * ctl->unit;
1839 *bytes = (u64)(max_bits) * ctl->unit;
1840 bitmap_info->max_extent_size = *bytes;
1844 static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1847 return entry->max_extent_size;
1848 return entry->bytes;
1851 /* Cache the size of the max extent in bytes */
1852 static struct btrfs_free_space *
1853 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1854 unsigned long align, u64 *max_extent_size)
1856 struct btrfs_free_space *entry;
1857 struct rb_node *node;
1862 if (!ctl->free_space_offset.rb_node)
1865 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1869 for (node = &entry->offset_index; node; node = rb_next(node)) {
1870 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1871 if (entry->bytes < *bytes) {
1872 *max_extent_size = max(get_max_extent_size(entry),
1877 /* make sure the space returned is big enough
1878 * to match our requested alignment
1880 if (*bytes >= align) {
1881 tmp = entry->offset - ctl->start + align - 1;
1882 tmp = div64_u64(tmp, align);
1883 tmp = tmp * align + ctl->start;
1884 align_off = tmp - entry->offset;
1887 tmp = entry->offset;
1890 if (entry->bytes < *bytes + align_off) {
1891 *max_extent_size = max(get_max_extent_size(entry),
1896 if (entry->bitmap) {
1899 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1906 max(get_max_extent_size(entry),
1913 *bytes = entry->bytes - align_off;
1920 static int count_bitmap_extents(struct btrfs_free_space_ctl *ctl,
1921 struct btrfs_free_space *bitmap_info)
1923 struct btrfs_block_group *block_group = ctl->private;
1924 u64 bytes = bitmap_info->bytes;
1925 unsigned int rs, re;
1928 if (!block_group || !bytes)
1931 bitmap_for_each_set_region(bitmap_info->bitmap, rs, re, 0,
1933 bytes -= (rs - re) * ctl->unit;
1943 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1944 struct btrfs_free_space *info, u64 offset)
1946 info->offset = offset_to_bitmap(ctl, offset);
1948 info->bitmap_extents = 0;
1949 INIT_LIST_HEAD(&info->list);
1950 link_free_space(ctl, info);
1951 ctl->total_bitmaps++;
1953 ctl->op->recalc_thresholds(ctl);
1956 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1957 struct btrfs_free_space *bitmap_info)
1960 * Normally when this is called, the bitmap is completely empty. However,
1961 * if we are blowing up the free space cache for one reason or another
1962 * via __btrfs_remove_free_space_cache(), then it may not be freed and
1963 * we may leave stats on the table.
1965 if (bitmap_info->bytes && !btrfs_free_space_trimmed(bitmap_info)) {
1966 ctl->discardable_extents[BTRFS_STAT_CURR] -=
1967 bitmap_info->bitmap_extents;
1968 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bitmap_info->bytes;
1971 unlink_free_space(ctl, bitmap_info);
1972 kmem_cache_free(btrfs_free_space_bitmap_cachep, bitmap_info->bitmap);
1973 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1974 ctl->total_bitmaps--;
1975 ctl->op->recalc_thresholds(ctl);
1978 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1979 struct btrfs_free_space *bitmap_info,
1980 u64 *offset, u64 *bytes)
1983 u64 search_start, search_bytes;
1987 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1990 * We need to search for bits in this bitmap. We could only cover some
1991 * of the extent in this bitmap thanks to how we add space, so we need
1992 * to search for as much as it as we can and clear that amount, and then
1993 * go searching for the next bit.
1995 search_start = *offset;
1996 search_bytes = ctl->unit;
1997 search_bytes = min(search_bytes, end - search_start + 1);
1998 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
2000 if (ret < 0 || search_start != *offset)
2003 /* We may have found more bits than what we need */
2004 search_bytes = min(search_bytes, *bytes);
2006 /* Cannot clear past the end of the bitmap */
2007 search_bytes = min(search_bytes, end - search_start + 1);
2009 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
2010 *offset += search_bytes;
2011 *bytes -= search_bytes;
2014 struct rb_node *next = rb_next(&bitmap_info->offset_index);
2015 if (!bitmap_info->bytes)
2016 free_bitmap(ctl, bitmap_info);
2019 * no entry after this bitmap, but we still have bytes to
2020 * remove, so something has gone wrong.
2025 bitmap_info = rb_entry(next, struct btrfs_free_space,
2029 * if the next entry isn't a bitmap we need to return to let the
2030 * extent stuff do its work.
2032 if (!bitmap_info->bitmap)
2036 * Ok the next item is a bitmap, but it may not actually hold
2037 * the information for the rest of this free space stuff, so
2038 * look for it, and if we don't find it return so we can try
2039 * everything over again.
2041 search_start = *offset;
2042 search_bytes = ctl->unit;
2043 ret = search_bitmap(ctl, bitmap_info, &search_start,
2044 &search_bytes, false);
2045 if (ret < 0 || search_start != *offset)
2049 } else if (!bitmap_info->bytes)
2050 free_bitmap(ctl, bitmap_info);
2055 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
2056 struct btrfs_free_space *info, u64 offset,
2057 u64 bytes, enum btrfs_trim_state trim_state)
2059 u64 bytes_to_set = 0;
2063 * This is a tradeoff to make bitmap trim state minimal. We mark the
2064 * whole bitmap untrimmed if at any point we add untrimmed regions.
2066 if (trim_state == BTRFS_TRIM_STATE_UNTRIMMED) {
2067 if (btrfs_free_space_trimmed(info)) {
2068 ctl->discardable_extents[BTRFS_STAT_CURR] +=
2069 info->bitmap_extents;
2070 ctl->discardable_bytes[BTRFS_STAT_CURR] += info->bytes;
2072 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2075 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
2077 bytes_to_set = min(end - offset, bytes);
2079 bitmap_set_bits(ctl, info, offset, bytes_to_set);
2082 * We set some bytes, we have no idea what the max extent size is
2085 info->max_extent_size = 0;
2087 return bytes_to_set;
2091 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2092 struct btrfs_free_space *info)
2094 struct btrfs_block_group *block_group = ctl->private;
2095 struct btrfs_fs_info *fs_info = block_group->fs_info;
2096 bool forced = false;
2098 #ifdef CONFIG_BTRFS_DEBUG
2099 if (btrfs_should_fragment_free_space(block_group))
2103 /* This is a way to reclaim large regions from the bitmaps. */
2104 if (!forced && info->bytes >= FORCE_EXTENT_THRESHOLD)
2108 * If we are below the extents threshold then we can add this as an
2109 * extent, and don't have to deal with the bitmap
2111 if (!forced && ctl->free_extents < ctl->extents_thresh) {
2113 * If this block group has some small extents we don't want to
2114 * use up all of our free slots in the cache with them, we want
2115 * to reserve them to larger extents, however if we have plenty
2116 * of cache left then go ahead an dadd them, no sense in adding
2117 * the overhead of a bitmap if we don't have to.
2119 if (info->bytes <= fs_info->sectorsize * 8) {
2120 if (ctl->free_extents * 3 <= ctl->extents_thresh)
2128 * The original block groups from mkfs can be really small, like 8
2129 * megabytes, so don't bother with a bitmap for those entries. However
2130 * some block groups can be smaller than what a bitmap would cover but
2131 * are still large enough that they could overflow the 32k memory limit,
2132 * so allow those block groups to still be allowed to have a bitmap
2135 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->length)
2141 static const struct btrfs_free_space_op free_space_op = {
2142 .recalc_thresholds = recalculate_thresholds,
2143 .use_bitmap = use_bitmap,
2146 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2147 struct btrfs_free_space *info)
2149 struct btrfs_free_space *bitmap_info;
2150 struct btrfs_block_group *block_group = NULL;
2152 u64 bytes, offset, bytes_added;
2153 enum btrfs_trim_state trim_state;
2156 bytes = info->bytes;
2157 offset = info->offset;
2158 trim_state = info->trim_state;
2160 if (!ctl->op->use_bitmap(ctl, info))
2163 if (ctl->op == &free_space_op)
2164 block_group = ctl->private;
2167 * Since we link bitmaps right into the cluster we need to see if we
2168 * have a cluster here, and if so and it has our bitmap we need to add
2169 * the free space to that bitmap.
2171 if (block_group && !list_empty(&block_group->cluster_list)) {
2172 struct btrfs_free_cluster *cluster;
2173 struct rb_node *node;
2174 struct btrfs_free_space *entry;
2176 cluster = list_entry(block_group->cluster_list.next,
2177 struct btrfs_free_cluster,
2179 spin_lock(&cluster->lock);
2180 node = rb_first(&cluster->root);
2182 spin_unlock(&cluster->lock);
2183 goto no_cluster_bitmap;
2186 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2187 if (!entry->bitmap) {
2188 spin_unlock(&cluster->lock);
2189 goto no_cluster_bitmap;
2192 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2193 bytes_added = add_bytes_to_bitmap(ctl, entry, offset,
2195 bytes -= bytes_added;
2196 offset += bytes_added;
2198 spin_unlock(&cluster->lock);
2206 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2213 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
2215 bytes -= bytes_added;
2216 offset += bytes_added;
2226 if (info && info->bitmap) {
2227 add_new_bitmap(ctl, info, offset);
2232 spin_unlock(&ctl->tree_lock);
2234 /* no pre-allocated info, allocate a new one */
2236 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2239 spin_lock(&ctl->tree_lock);
2245 /* allocate the bitmap */
2246 info->bitmap = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep,
2248 info->trim_state = BTRFS_TRIM_STATE_TRIMMED;
2249 spin_lock(&ctl->tree_lock);
2250 if (!info->bitmap) {
2260 kmem_cache_free(btrfs_free_space_bitmap_cachep,
2262 kmem_cache_free(btrfs_free_space_cachep, info);
2269 * Free space merging rules:
2270 * 1) Merge trimmed areas together
2271 * 2) Let untrimmed areas coalesce with trimmed areas
2272 * 3) Always pull neighboring regions from bitmaps
2274 * The above rules are for when we merge free space based on btrfs_trim_state.
2275 * Rules 2 and 3 are subtle because they are suboptimal, but are done for the
2276 * same reason: to promote larger extent regions which makes life easier for
2277 * find_free_extent(). Rule 2 enables coalescing based on the common path
2278 * being returning free space from btrfs_finish_extent_commit(). So when free
2279 * space is trimmed, it will prevent aggregating trimmed new region and
2280 * untrimmed regions in the rb_tree. Rule 3 is purely to obtain larger extents
2281 * and provide find_free_extent() with the largest extents possible hoping for
2284 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2285 struct btrfs_free_space *info, bool update_stat)
2287 struct btrfs_free_space *left_info = NULL;
2288 struct btrfs_free_space *right_info;
2289 bool merged = false;
2290 u64 offset = info->offset;
2291 u64 bytes = info->bytes;
2292 const bool is_trimmed = btrfs_free_space_trimmed(info);
2295 * first we want to see if there is free space adjacent to the range we
2296 * are adding, if there is remove that struct and add a new one to
2297 * cover the entire range
2299 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2300 if (right_info && rb_prev(&right_info->offset_index))
2301 left_info = rb_entry(rb_prev(&right_info->offset_index),
2302 struct btrfs_free_space, offset_index);
2303 else if (!right_info)
2304 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2306 /* See try_merge_free_space() comment. */
2307 if (right_info && !right_info->bitmap &&
2308 (!is_trimmed || btrfs_free_space_trimmed(right_info))) {
2310 unlink_free_space(ctl, right_info);
2312 __unlink_free_space(ctl, right_info);
2313 info->bytes += right_info->bytes;
2314 kmem_cache_free(btrfs_free_space_cachep, right_info);
2318 /* See try_merge_free_space() comment. */
2319 if (left_info && !left_info->bitmap &&
2320 left_info->offset + left_info->bytes == offset &&
2321 (!is_trimmed || btrfs_free_space_trimmed(left_info))) {
2323 unlink_free_space(ctl, left_info);
2325 __unlink_free_space(ctl, left_info);
2326 info->offset = left_info->offset;
2327 info->bytes += left_info->bytes;
2328 kmem_cache_free(btrfs_free_space_cachep, left_info);
2335 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2336 struct btrfs_free_space *info,
2339 struct btrfs_free_space *bitmap;
2342 const u64 end = info->offset + info->bytes;
2343 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2346 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2350 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2351 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2354 bytes = (j - i) * ctl->unit;
2355 info->bytes += bytes;
2357 /* See try_merge_free_space() comment. */
2358 if (!btrfs_free_space_trimmed(bitmap))
2359 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2362 bitmap_clear_bits(ctl, bitmap, end, bytes);
2364 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2367 free_bitmap(ctl, bitmap);
2372 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2373 struct btrfs_free_space *info,
2376 struct btrfs_free_space *bitmap;
2380 unsigned long prev_j;
2383 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2384 /* If we're on a boundary, try the previous logical bitmap. */
2385 if (bitmap_offset == info->offset) {
2386 if (info->offset == 0)
2388 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2391 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2395 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2397 prev_j = (unsigned long)-1;
2398 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2406 if (prev_j == (unsigned long)-1)
2407 bytes = (i + 1) * ctl->unit;
2409 bytes = (i - prev_j) * ctl->unit;
2411 info->offset -= bytes;
2412 info->bytes += bytes;
2414 /* See try_merge_free_space() comment. */
2415 if (!btrfs_free_space_trimmed(bitmap))
2416 info->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2419 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2421 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2424 free_bitmap(ctl, bitmap);
2430 * We prefer always to allocate from extent entries, both for clustered and
2431 * non-clustered allocation requests. So when attempting to add a new extent
2432 * entry, try to see if there's adjacent free space in bitmap entries, and if
2433 * there is, migrate that space from the bitmaps to the extent.
2434 * Like this we get better chances of satisfying space allocation requests
2435 * because we attempt to satisfy them based on a single cache entry, and never
2436 * on 2 or more entries - even if the entries represent a contiguous free space
2437 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2440 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2441 struct btrfs_free_space *info,
2445 * Only work with disconnected entries, as we can change their offset,
2446 * and must be extent entries.
2448 ASSERT(!info->bitmap);
2449 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2451 if (ctl->total_bitmaps > 0) {
2453 bool stole_front = false;
2455 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2456 if (ctl->total_bitmaps > 0)
2457 stole_front = steal_from_bitmap_to_front(ctl, info,
2460 if (stole_end || stole_front)
2461 try_merge_free_space(ctl, info, update_stat);
2465 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2466 struct btrfs_free_space_ctl *ctl,
2467 u64 offset, u64 bytes,
2468 enum btrfs_trim_state trim_state)
2470 struct btrfs_block_group *block_group = ctl->private;
2471 struct btrfs_free_space *info;
2473 u64 filter_bytes = bytes;
2475 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2479 info->offset = offset;
2480 info->bytes = bytes;
2481 info->trim_state = trim_state;
2482 RB_CLEAR_NODE(&info->offset_index);
2484 spin_lock(&ctl->tree_lock);
2486 if (try_merge_free_space(ctl, info, true))
2490 * There was no extent directly to the left or right of this new
2491 * extent then we know we're going to have to allocate a new extent, so
2492 * before we do that see if we need to drop this into a bitmap
2494 ret = insert_into_bitmap(ctl, info);
2503 * Only steal free space from adjacent bitmaps if we're sure we're not
2504 * going to add the new free space to existing bitmap entries - because
2505 * that would mean unnecessary work that would be reverted. Therefore
2506 * attempt to steal space from bitmaps if we're adding an extent entry.
2508 steal_from_bitmap(ctl, info, true);
2510 filter_bytes = max(filter_bytes, info->bytes);
2512 ret = link_free_space(ctl, info);
2514 kmem_cache_free(btrfs_free_space_cachep, info);
2516 btrfs_discard_update_discardable(block_group, ctl);
2517 spin_unlock(&ctl->tree_lock);
2520 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2521 ASSERT(ret != -EEXIST);
2524 if (trim_state != BTRFS_TRIM_STATE_TRIMMED) {
2525 btrfs_discard_check_filter(block_group, filter_bytes);
2526 btrfs_discard_queue_work(&fs_info->discard_ctl, block_group);
2532 int btrfs_add_free_space(struct btrfs_block_group *block_group,
2533 u64 bytenr, u64 size)
2535 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2537 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC))
2538 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2540 return __btrfs_add_free_space(block_group->fs_info,
2541 block_group->free_space_ctl,
2542 bytenr, size, trim_state);
2546 * This is a subtle distinction because when adding free space back in general,
2547 * we want it to be added as untrimmed for async. But in the case where we add
2548 * it on loading of a block group, we want to consider it trimmed.
2550 int btrfs_add_free_space_async_trimmed(struct btrfs_block_group *block_group,
2551 u64 bytenr, u64 size)
2553 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2555 if (btrfs_test_opt(block_group->fs_info, DISCARD_SYNC) ||
2556 btrfs_test_opt(block_group->fs_info, DISCARD_ASYNC))
2557 trim_state = BTRFS_TRIM_STATE_TRIMMED;
2559 return __btrfs_add_free_space(block_group->fs_info,
2560 block_group->free_space_ctl,
2561 bytenr, size, trim_state);
2564 int btrfs_remove_free_space(struct btrfs_block_group *block_group,
2565 u64 offset, u64 bytes)
2567 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2568 struct btrfs_free_space *info;
2570 bool re_search = false;
2572 spin_lock(&ctl->tree_lock);
2579 info = tree_search_offset(ctl, offset, 0, 0);
2582 * oops didn't find an extent that matched the space we wanted
2583 * to remove, look for a bitmap instead
2585 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2589 * If we found a partial bit of our free space in a
2590 * bitmap but then couldn't find the other part this may
2591 * be a problem, so WARN about it.
2599 if (!info->bitmap) {
2600 unlink_free_space(ctl, info);
2601 if (offset == info->offset) {
2602 u64 to_free = min(bytes, info->bytes);
2604 info->bytes -= to_free;
2605 info->offset += to_free;
2607 ret = link_free_space(ctl, info);
2610 kmem_cache_free(btrfs_free_space_cachep, info);
2617 u64 old_end = info->bytes + info->offset;
2619 info->bytes = offset - info->offset;
2620 ret = link_free_space(ctl, info);
2625 /* Not enough bytes in this entry to satisfy us */
2626 if (old_end < offset + bytes) {
2627 bytes -= old_end - offset;
2630 } else if (old_end == offset + bytes) {
2634 spin_unlock(&ctl->tree_lock);
2636 ret = __btrfs_add_free_space(block_group->fs_info, ctl,
2638 old_end - (offset + bytes),
2645 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2646 if (ret == -EAGAIN) {
2651 btrfs_discard_update_discardable(block_group, ctl);
2652 spin_unlock(&ctl->tree_lock);
2657 void btrfs_dump_free_space(struct btrfs_block_group *block_group,
2660 struct btrfs_fs_info *fs_info = block_group->fs_info;
2661 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2662 struct btrfs_free_space *info;
2666 spin_lock(&ctl->tree_lock);
2667 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2668 info = rb_entry(n, struct btrfs_free_space, offset_index);
2669 if (info->bytes >= bytes && !block_group->ro)
2671 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2672 info->offset, info->bytes,
2673 (info->bitmap) ? "yes" : "no");
2675 spin_unlock(&ctl->tree_lock);
2676 btrfs_info(fs_info, "block group has cluster?: %s",
2677 list_empty(&block_group->cluster_list) ? "no" : "yes");
2679 "%d blocks of free space at or bigger than bytes is", count);
2682 void btrfs_init_free_space_ctl(struct btrfs_block_group *block_group)
2684 struct btrfs_fs_info *fs_info = block_group->fs_info;
2685 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2687 spin_lock_init(&ctl->tree_lock);
2688 ctl->unit = fs_info->sectorsize;
2689 ctl->start = block_group->start;
2690 ctl->private = block_group;
2691 ctl->op = &free_space_op;
2692 INIT_LIST_HEAD(&ctl->trimming_ranges);
2693 mutex_init(&ctl->cache_writeout_mutex);
2696 * we only want to have 32k of ram per block group for keeping
2697 * track of free space, and if we pass 1/2 of that we want to
2698 * start converting things over to using bitmaps
2700 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2704 * for a given cluster, put all of its extents back into the free
2705 * space cache. If the block group passed doesn't match the block group
2706 * pointed to by the cluster, someone else raced in and freed the
2707 * cluster already. In that case, we just return without changing anything
2709 static void __btrfs_return_cluster_to_free_space(
2710 struct btrfs_block_group *block_group,
2711 struct btrfs_free_cluster *cluster)
2713 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2714 struct btrfs_free_space *entry;
2715 struct rb_node *node;
2717 spin_lock(&cluster->lock);
2718 if (cluster->block_group != block_group) {
2719 spin_unlock(&cluster->lock);
2723 cluster->block_group = NULL;
2724 cluster->window_start = 0;
2725 list_del_init(&cluster->block_group_list);
2727 node = rb_first(&cluster->root);
2731 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2732 node = rb_next(&entry->offset_index);
2733 rb_erase(&entry->offset_index, &cluster->root);
2734 RB_CLEAR_NODE(&entry->offset_index);
2736 bitmap = (entry->bitmap != NULL);
2738 /* Merging treats extents as if they were new */
2739 if (!btrfs_free_space_trimmed(entry)) {
2740 ctl->discardable_extents[BTRFS_STAT_CURR]--;
2741 ctl->discardable_bytes[BTRFS_STAT_CURR] -=
2745 try_merge_free_space(ctl, entry, false);
2746 steal_from_bitmap(ctl, entry, false);
2748 /* As we insert directly, update these statistics */
2749 if (!btrfs_free_space_trimmed(entry)) {
2750 ctl->discardable_extents[BTRFS_STAT_CURR]++;
2751 ctl->discardable_bytes[BTRFS_STAT_CURR] +=
2755 tree_insert_offset(&ctl->free_space_offset,
2756 entry->offset, &entry->offset_index, bitmap);
2758 cluster->root = RB_ROOT;
2759 spin_unlock(&cluster->lock);
2760 btrfs_put_block_group(block_group);
2763 static void __btrfs_remove_free_space_cache_locked(
2764 struct btrfs_free_space_ctl *ctl)
2766 struct btrfs_free_space *info;
2767 struct rb_node *node;
2769 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2770 info = rb_entry(node, struct btrfs_free_space, offset_index);
2771 if (!info->bitmap) {
2772 unlink_free_space(ctl, info);
2773 kmem_cache_free(btrfs_free_space_cachep, info);
2775 free_bitmap(ctl, info);
2778 cond_resched_lock(&ctl->tree_lock);
2782 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2784 spin_lock(&ctl->tree_lock);
2785 __btrfs_remove_free_space_cache_locked(ctl);
2787 btrfs_discard_update_discardable(ctl->private, ctl);
2788 spin_unlock(&ctl->tree_lock);
2791 void btrfs_remove_free_space_cache(struct btrfs_block_group *block_group)
2793 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2794 struct btrfs_free_cluster *cluster;
2795 struct list_head *head;
2797 spin_lock(&ctl->tree_lock);
2798 while ((head = block_group->cluster_list.next) !=
2799 &block_group->cluster_list) {
2800 cluster = list_entry(head, struct btrfs_free_cluster,
2803 WARN_ON(cluster->block_group != block_group);
2804 __btrfs_return_cluster_to_free_space(block_group, cluster);
2806 cond_resched_lock(&ctl->tree_lock);
2808 __btrfs_remove_free_space_cache_locked(ctl);
2809 btrfs_discard_update_discardable(block_group, ctl);
2810 spin_unlock(&ctl->tree_lock);
2815 * btrfs_is_free_space_trimmed - see if everything is trimmed
2816 * @block_group: block_group of interest
2818 * Walk @block_group's free space rb_tree to determine if everything is trimmed.
2820 bool btrfs_is_free_space_trimmed(struct btrfs_block_group *block_group)
2822 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2823 struct btrfs_free_space *info;
2824 struct rb_node *node;
2827 spin_lock(&ctl->tree_lock);
2828 node = rb_first(&ctl->free_space_offset);
2831 info = rb_entry(node, struct btrfs_free_space, offset_index);
2833 if (!btrfs_free_space_trimmed(info)) {
2838 node = rb_next(node);
2841 spin_unlock(&ctl->tree_lock);
2845 u64 btrfs_find_space_for_alloc(struct btrfs_block_group *block_group,
2846 u64 offset, u64 bytes, u64 empty_size,
2847 u64 *max_extent_size)
2849 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2850 struct btrfs_discard_ctl *discard_ctl =
2851 &block_group->fs_info->discard_ctl;
2852 struct btrfs_free_space *entry = NULL;
2853 u64 bytes_search = bytes + empty_size;
2856 u64 align_gap_len = 0;
2857 enum btrfs_trim_state align_gap_trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
2859 spin_lock(&ctl->tree_lock);
2860 entry = find_free_space(ctl, &offset, &bytes_search,
2861 block_group->full_stripe_len, max_extent_size);
2866 if (entry->bitmap) {
2867 bitmap_clear_bits(ctl, entry, offset, bytes);
2869 if (!btrfs_free_space_trimmed(entry))
2870 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
2873 free_bitmap(ctl, entry);
2875 unlink_free_space(ctl, entry);
2876 align_gap_len = offset - entry->offset;
2877 align_gap = entry->offset;
2878 align_gap_trim_state = entry->trim_state;
2880 if (!btrfs_free_space_trimmed(entry))
2881 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
2883 entry->offset = offset + bytes;
2884 WARN_ON(entry->bytes < bytes + align_gap_len);
2886 entry->bytes -= bytes + align_gap_len;
2888 kmem_cache_free(btrfs_free_space_cachep, entry);
2890 link_free_space(ctl, entry);
2893 btrfs_discard_update_discardable(block_group, ctl);
2894 spin_unlock(&ctl->tree_lock);
2897 __btrfs_add_free_space(block_group->fs_info, ctl,
2898 align_gap, align_gap_len,
2899 align_gap_trim_state);
2904 * given a cluster, put all of its extents back into the free space
2905 * cache. If a block group is passed, this function will only free
2906 * a cluster that belongs to the passed block group.
2908 * Otherwise, it'll get a reference on the block group pointed to by the
2909 * cluster and remove the cluster from it.
2911 void btrfs_return_cluster_to_free_space(
2912 struct btrfs_block_group *block_group,
2913 struct btrfs_free_cluster *cluster)
2915 struct btrfs_free_space_ctl *ctl;
2917 /* first, get a safe pointer to the block group */
2918 spin_lock(&cluster->lock);
2920 block_group = cluster->block_group;
2922 spin_unlock(&cluster->lock);
2925 } else if (cluster->block_group != block_group) {
2926 /* someone else has already freed it don't redo their work */
2927 spin_unlock(&cluster->lock);
2930 btrfs_get_block_group(block_group);
2931 spin_unlock(&cluster->lock);
2933 ctl = block_group->free_space_ctl;
2935 /* now return any extents the cluster had on it */
2936 spin_lock(&ctl->tree_lock);
2937 __btrfs_return_cluster_to_free_space(block_group, cluster);
2938 spin_unlock(&ctl->tree_lock);
2940 btrfs_discard_queue_work(&block_group->fs_info->discard_ctl, block_group);
2942 /* finally drop our ref */
2943 btrfs_put_block_group(block_group);
2946 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group *block_group,
2947 struct btrfs_free_cluster *cluster,
2948 struct btrfs_free_space *entry,
2949 u64 bytes, u64 min_start,
2950 u64 *max_extent_size)
2952 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2954 u64 search_start = cluster->window_start;
2955 u64 search_bytes = bytes;
2958 search_start = min_start;
2959 search_bytes = bytes;
2961 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2963 *max_extent_size = max(get_max_extent_size(entry),
2969 __bitmap_clear_bits(ctl, entry, ret, bytes);
2975 * given a cluster, try to allocate 'bytes' from it, returns 0
2976 * if it couldn't find anything suitably large, or a logical disk offset
2977 * if things worked out
2979 u64 btrfs_alloc_from_cluster(struct btrfs_block_group *block_group,
2980 struct btrfs_free_cluster *cluster, u64 bytes,
2981 u64 min_start, u64 *max_extent_size)
2983 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2984 struct btrfs_discard_ctl *discard_ctl =
2985 &block_group->fs_info->discard_ctl;
2986 struct btrfs_free_space *entry = NULL;
2987 struct rb_node *node;
2990 spin_lock(&cluster->lock);
2991 if (bytes > cluster->max_size)
2994 if (cluster->block_group != block_group)
2997 node = rb_first(&cluster->root);
3001 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3003 if (entry->bytes < bytes)
3004 *max_extent_size = max(get_max_extent_size(entry),
3007 if (entry->bytes < bytes ||
3008 (!entry->bitmap && entry->offset < min_start)) {
3009 node = rb_next(&entry->offset_index);
3012 entry = rb_entry(node, struct btrfs_free_space,
3017 if (entry->bitmap) {
3018 ret = btrfs_alloc_from_bitmap(block_group,
3019 cluster, entry, bytes,
3020 cluster->window_start,
3023 node = rb_next(&entry->offset_index);
3026 entry = rb_entry(node, struct btrfs_free_space,
3030 cluster->window_start += bytes;
3032 ret = entry->offset;
3034 entry->offset += bytes;
3035 entry->bytes -= bytes;
3041 spin_unlock(&cluster->lock);
3046 spin_lock(&ctl->tree_lock);
3048 if (!btrfs_free_space_trimmed(entry))
3049 atomic64_add(bytes, &discard_ctl->discard_bytes_saved);
3051 ctl->free_space -= bytes;
3052 if (!entry->bitmap && !btrfs_free_space_trimmed(entry))
3053 ctl->discardable_bytes[BTRFS_STAT_CURR] -= bytes;
3055 spin_lock(&cluster->lock);
3056 if (entry->bytes == 0) {
3057 rb_erase(&entry->offset_index, &cluster->root);
3058 ctl->free_extents--;
3059 if (entry->bitmap) {
3060 kmem_cache_free(btrfs_free_space_bitmap_cachep,
3062 ctl->total_bitmaps--;
3063 ctl->op->recalc_thresholds(ctl);
3064 } else if (!btrfs_free_space_trimmed(entry)) {
3065 ctl->discardable_extents[BTRFS_STAT_CURR]--;
3067 kmem_cache_free(btrfs_free_space_cachep, entry);
3070 spin_unlock(&cluster->lock);
3071 spin_unlock(&ctl->tree_lock);
3076 static int btrfs_bitmap_cluster(struct btrfs_block_group *block_group,
3077 struct btrfs_free_space *entry,
3078 struct btrfs_free_cluster *cluster,
3079 u64 offset, u64 bytes,
3080 u64 cont1_bytes, u64 min_bytes)
3082 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3083 unsigned long next_zero;
3085 unsigned long want_bits;
3086 unsigned long min_bits;
3087 unsigned long found_bits;
3088 unsigned long max_bits = 0;
3089 unsigned long start = 0;
3090 unsigned long total_found = 0;
3093 i = offset_to_bit(entry->offset, ctl->unit,
3094 max_t(u64, offset, entry->offset));
3095 want_bits = bytes_to_bits(bytes, ctl->unit);
3096 min_bits = bytes_to_bits(min_bytes, ctl->unit);
3099 * Don't bother looking for a cluster in this bitmap if it's heavily
3102 if (entry->max_extent_size &&
3103 entry->max_extent_size < cont1_bytes)
3107 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
3108 next_zero = find_next_zero_bit(entry->bitmap,
3109 BITS_PER_BITMAP, i);
3110 if (next_zero - i >= min_bits) {
3111 found_bits = next_zero - i;
3112 if (found_bits > max_bits)
3113 max_bits = found_bits;
3116 if (next_zero - i > max_bits)
3117 max_bits = next_zero - i;
3122 entry->max_extent_size = (u64)max_bits * ctl->unit;
3128 cluster->max_size = 0;
3131 total_found += found_bits;
3133 if (cluster->max_size < found_bits * ctl->unit)
3134 cluster->max_size = found_bits * ctl->unit;
3136 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
3141 cluster->window_start = start * ctl->unit + entry->offset;
3142 rb_erase(&entry->offset_index, &ctl->free_space_offset);
3143 ret = tree_insert_offset(&cluster->root, entry->offset,
3144 &entry->offset_index, 1);
3145 ASSERT(!ret); /* -EEXIST; Logic error */
3147 trace_btrfs_setup_cluster(block_group, cluster,
3148 total_found * ctl->unit, 1);
3153 * This searches the block group for just extents to fill the cluster with.
3154 * Try to find a cluster with at least bytes total bytes, at least one
3155 * extent of cont1_bytes, and other clusters of at least min_bytes.
3158 setup_cluster_no_bitmap(struct btrfs_block_group *block_group,
3159 struct btrfs_free_cluster *cluster,
3160 struct list_head *bitmaps, u64 offset, u64 bytes,
3161 u64 cont1_bytes, u64 min_bytes)
3163 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3164 struct btrfs_free_space *first = NULL;
3165 struct btrfs_free_space *entry = NULL;
3166 struct btrfs_free_space *last;
3167 struct rb_node *node;
3172 entry = tree_search_offset(ctl, offset, 0, 1);
3177 * We don't want bitmaps, so just move along until we find a normal
3180 while (entry->bitmap || entry->bytes < min_bytes) {
3181 if (entry->bitmap && list_empty(&entry->list))
3182 list_add_tail(&entry->list, bitmaps);
3183 node = rb_next(&entry->offset_index);
3186 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3189 window_free = entry->bytes;
3190 max_extent = entry->bytes;
3194 for (node = rb_next(&entry->offset_index); node;
3195 node = rb_next(&entry->offset_index)) {
3196 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3198 if (entry->bitmap) {
3199 if (list_empty(&entry->list))
3200 list_add_tail(&entry->list, bitmaps);
3204 if (entry->bytes < min_bytes)
3208 window_free += entry->bytes;
3209 if (entry->bytes > max_extent)
3210 max_extent = entry->bytes;
3213 if (window_free < bytes || max_extent < cont1_bytes)
3216 cluster->window_start = first->offset;
3218 node = &first->offset_index;
3221 * now we've found our entries, pull them out of the free space
3222 * cache and put them into the cluster rbtree
3227 entry = rb_entry(node, struct btrfs_free_space, offset_index);
3228 node = rb_next(&entry->offset_index);
3229 if (entry->bitmap || entry->bytes < min_bytes)
3232 rb_erase(&entry->offset_index, &ctl->free_space_offset);
3233 ret = tree_insert_offset(&cluster->root, entry->offset,
3234 &entry->offset_index, 0);
3235 total_size += entry->bytes;
3236 ASSERT(!ret); /* -EEXIST; Logic error */
3237 } while (node && entry != last);
3239 cluster->max_size = max_extent;
3240 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
3245 * This specifically looks for bitmaps that may work in the cluster, we assume
3246 * that we have already failed to find extents that will work.
3249 setup_cluster_bitmap(struct btrfs_block_group *block_group,
3250 struct btrfs_free_cluster *cluster,
3251 struct list_head *bitmaps, u64 offset, u64 bytes,
3252 u64 cont1_bytes, u64 min_bytes)
3254 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3255 struct btrfs_free_space *entry = NULL;
3257 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
3259 if (ctl->total_bitmaps == 0)
3263 * The bitmap that covers offset won't be in the list unless offset
3264 * is just its start offset.
3266 if (!list_empty(bitmaps))
3267 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3269 if (!entry || entry->offset != bitmap_offset) {
3270 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3271 if (entry && list_empty(&entry->list))
3272 list_add(&entry->list, bitmaps);
3275 list_for_each_entry(entry, bitmaps, list) {
3276 if (entry->bytes < bytes)
3278 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
3279 bytes, cont1_bytes, min_bytes);
3285 * The bitmaps list has all the bitmaps that record free space
3286 * starting after offset, so no more search is required.
3292 * here we try to find a cluster of blocks in a block group. The goal
3293 * is to find at least bytes+empty_size.
3294 * We might not find them all in one contiguous area.
3296 * returns zero and sets up cluster if things worked out, otherwise
3297 * it returns -enospc
3299 int btrfs_find_space_cluster(struct btrfs_block_group *block_group,
3300 struct btrfs_free_cluster *cluster,
3301 u64 offset, u64 bytes, u64 empty_size)
3303 struct btrfs_fs_info *fs_info = block_group->fs_info;
3304 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3305 struct btrfs_free_space *entry, *tmp;
3312 * Choose the minimum extent size we'll require for this
3313 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3314 * For metadata, allow allocates with smaller extents. For
3315 * data, keep it dense.
3317 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3318 cont1_bytes = min_bytes = bytes + empty_size;
3319 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3320 cont1_bytes = bytes;
3321 min_bytes = fs_info->sectorsize;
3323 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3324 min_bytes = fs_info->sectorsize;
3327 spin_lock(&ctl->tree_lock);
3330 * If we know we don't have enough space to make a cluster don't even
3331 * bother doing all the work to try and find one.
3333 if (ctl->free_space < bytes) {
3334 spin_unlock(&ctl->tree_lock);
3338 spin_lock(&cluster->lock);
3340 /* someone already found a cluster, hooray */
3341 if (cluster->block_group) {
3346 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3349 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3351 cont1_bytes, min_bytes);
3353 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3354 offset, bytes + empty_size,
3355 cont1_bytes, min_bytes);
3357 /* Clear our temporary list */
3358 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3359 list_del_init(&entry->list);
3362 btrfs_get_block_group(block_group);
3363 list_add_tail(&cluster->block_group_list,
3364 &block_group->cluster_list);
3365 cluster->block_group = block_group;
3367 trace_btrfs_failed_cluster_setup(block_group);
3370 spin_unlock(&cluster->lock);
3371 spin_unlock(&ctl->tree_lock);
3377 * simple code to zero out a cluster
3379 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3381 spin_lock_init(&cluster->lock);
3382 spin_lock_init(&cluster->refill_lock);
3383 cluster->root = RB_ROOT;
3384 cluster->max_size = 0;
3385 cluster->fragmented = false;
3386 INIT_LIST_HEAD(&cluster->block_group_list);
3387 cluster->block_group = NULL;
3390 static int do_trimming(struct btrfs_block_group *block_group,
3391 u64 *total_trimmed, u64 start, u64 bytes,
3392 u64 reserved_start, u64 reserved_bytes,
3393 enum btrfs_trim_state reserved_trim_state,
3394 struct btrfs_trim_range *trim_entry)
3396 struct btrfs_space_info *space_info = block_group->space_info;
3397 struct btrfs_fs_info *fs_info = block_group->fs_info;
3398 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3401 const u64 end = start + bytes;
3402 const u64 reserved_end = reserved_start + reserved_bytes;
3403 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
3406 spin_lock(&space_info->lock);
3407 spin_lock(&block_group->lock);
3408 if (!block_group->ro) {
3409 block_group->reserved += reserved_bytes;
3410 space_info->bytes_reserved += reserved_bytes;
3413 spin_unlock(&block_group->lock);
3414 spin_unlock(&space_info->lock);
3416 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3418 *total_trimmed += trimmed;
3419 trim_state = BTRFS_TRIM_STATE_TRIMMED;
3422 mutex_lock(&ctl->cache_writeout_mutex);
3423 if (reserved_start < start)
3424 __btrfs_add_free_space(fs_info, ctl, reserved_start,
3425 start - reserved_start,
3426 reserved_trim_state);
3427 if (start + bytes < reserved_start + reserved_bytes)
3428 __btrfs_add_free_space(fs_info, ctl, end, reserved_end - end,
3429 reserved_trim_state);
3430 __btrfs_add_free_space(fs_info, ctl, start, bytes, trim_state);
3431 list_del(&trim_entry->list);
3432 mutex_unlock(&ctl->cache_writeout_mutex);
3435 spin_lock(&space_info->lock);
3436 spin_lock(&block_group->lock);
3437 if (block_group->ro)
3438 space_info->bytes_readonly += reserved_bytes;
3439 block_group->reserved -= reserved_bytes;
3440 space_info->bytes_reserved -= reserved_bytes;
3441 spin_unlock(&block_group->lock);
3442 spin_unlock(&space_info->lock);
3449 * If @async is set, then we will trim 1 region and return.
3451 static int trim_no_bitmap(struct btrfs_block_group *block_group,
3452 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
3455 struct btrfs_discard_ctl *discard_ctl =
3456 &block_group->fs_info->discard_ctl;
3457 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3458 struct btrfs_free_space *entry;
3459 struct rb_node *node;
3463 enum btrfs_trim_state extent_trim_state;
3465 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
3467 while (start < end) {
3468 struct btrfs_trim_range trim_entry;
3470 mutex_lock(&ctl->cache_writeout_mutex);
3471 spin_lock(&ctl->tree_lock);
3473 if (ctl->free_space < minlen)
3476 entry = tree_search_offset(ctl, start, 0, 1);
3480 /* Skip bitmaps and if async, already trimmed entries */
3481 while (entry->bitmap ||
3482 (async && btrfs_free_space_trimmed(entry))) {
3483 node = rb_next(&entry->offset_index);
3486 entry = rb_entry(node, struct btrfs_free_space,
3490 if (entry->offset >= end)
3493 extent_start = entry->offset;
3494 extent_bytes = entry->bytes;
3495 extent_trim_state = entry->trim_state;
3497 start = entry->offset;
3498 bytes = entry->bytes;
3499 if (bytes < minlen) {
3500 spin_unlock(&ctl->tree_lock);
3501 mutex_unlock(&ctl->cache_writeout_mutex);
3504 unlink_free_space(ctl, entry);
3506 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3507 * If X < BTRFS_ASYNC_DISCARD_MIN_FILTER, we won't trim
3508 * X when we come back around. So trim it now.
3510 if (max_discard_size &&
3511 bytes >= (max_discard_size +
3512 BTRFS_ASYNC_DISCARD_MIN_FILTER)) {
3513 bytes = max_discard_size;
3514 extent_bytes = max_discard_size;
3515 entry->offset += max_discard_size;
3516 entry->bytes -= max_discard_size;
3517 link_free_space(ctl, entry);
3519 kmem_cache_free(btrfs_free_space_cachep, entry);
3522 start = max(start, extent_start);
3523 bytes = min(extent_start + extent_bytes, end) - start;
3524 if (bytes < minlen) {
3525 spin_unlock(&ctl->tree_lock);
3526 mutex_unlock(&ctl->cache_writeout_mutex);
3530 unlink_free_space(ctl, entry);
3531 kmem_cache_free(btrfs_free_space_cachep, entry);
3534 spin_unlock(&ctl->tree_lock);
3535 trim_entry.start = extent_start;
3536 trim_entry.bytes = extent_bytes;
3537 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3538 mutex_unlock(&ctl->cache_writeout_mutex);
3540 ret = do_trimming(block_group, total_trimmed, start, bytes,
3541 extent_start, extent_bytes, extent_trim_state,
3544 block_group->discard_cursor = start + bytes;
3549 block_group->discard_cursor = start;
3550 if (async && *total_trimmed)
3553 if (fatal_signal_pending(current)) {
3564 block_group->discard_cursor = btrfs_block_group_end(block_group);
3565 spin_unlock(&ctl->tree_lock);
3566 mutex_unlock(&ctl->cache_writeout_mutex);
3572 * If we break out of trimming a bitmap prematurely, we should reset the
3573 * trimming bit. In a rather contrieved case, it's possible to race here so
3574 * reset the state to BTRFS_TRIM_STATE_UNTRIMMED.
3576 * start = start of bitmap
3577 * end = near end of bitmap
3579 * Thread 1: Thread 2:
3580 * trim_bitmaps(start)
3582 * end_trimming_bitmap()
3583 * reset_trimming_bitmap()
3585 static void reset_trimming_bitmap(struct btrfs_free_space_ctl *ctl, u64 offset)
3587 struct btrfs_free_space *entry;
3589 spin_lock(&ctl->tree_lock);
3590 entry = tree_search_offset(ctl, offset, 1, 0);
3592 if (btrfs_free_space_trimmed(entry)) {
3593 ctl->discardable_extents[BTRFS_STAT_CURR] +=
3594 entry->bitmap_extents;
3595 ctl->discardable_bytes[BTRFS_STAT_CURR] += entry->bytes;
3597 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
3600 spin_unlock(&ctl->tree_lock);
3603 static void end_trimming_bitmap(struct btrfs_free_space_ctl *ctl,
3604 struct btrfs_free_space *entry)
3606 if (btrfs_free_space_trimming_bitmap(entry)) {
3607 entry->trim_state = BTRFS_TRIM_STATE_TRIMMED;
3608 ctl->discardable_extents[BTRFS_STAT_CURR] -=
3609 entry->bitmap_extents;
3610 ctl->discardable_bytes[BTRFS_STAT_CURR] -= entry->bytes;
3615 * If @async is set, then we will trim 1 region and return.
3617 static int trim_bitmaps(struct btrfs_block_group *block_group,
3618 u64 *total_trimmed, u64 start, u64 end, u64 minlen,
3619 u64 maxlen, bool async)
3621 struct btrfs_discard_ctl *discard_ctl =
3622 &block_group->fs_info->discard_ctl;
3623 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3624 struct btrfs_free_space *entry;
3628 u64 offset = offset_to_bitmap(ctl, start);
3629 const u64 max_discard_size = READ_ONCE(discard_ctl->max_discard_size);
3631 while (offset < end) {
3632 bool next_bitmap = false;
3633 struct btrfs_trim_range trim_entry;
3635 mutex_lock(&ctl->cache_writeout_mutex);
3636 spin_lock(&ctl->tree_lock);
3638 if (ctl->free_space < minlen) {
3639 block_group->discard_cursor =
3640 btrfs_block_group_end(block_group);
3641 spin_unlock(&ctl->tree_lock);
3642 mutex_unlock(&ctl->cache_writeout_mutex);
3646 entry = tree_search_offset(ctl, offset, 1, 0);
3648 * Bitmaps are marked trimmed lossily now to prevent constant
3649 * discarding of the same bitmap (the reason why we are bound
3650 * by the filters). So, retrim the block group bitmaps when we
3651 * are preparing to punt to the unused_bgs list. This uses
3652 * @minlen to determine if we are in BTRFS_DISCARD_INDEX_UNUSED
3653 * which is the only discard index which sets minlen to 0.
3655 if (!entry || (async && minlen && start == offset &&
3656 btrfs_free_space_trimmed(entry))) {
3657 spin_unlock(&ctl->tree_lock);
3658 mutex_unlock(&ctl->cache_writeout_mutex);
3664 * Async discard bitmap trimming begins at by setting the start
3665 * to be key.objectid and the offset_to_bitmap() aligns to the
3666 * start of the bitmap. This lets us know we are fully
3667 * scanning the bitmap rather than only some portion of it.
3669 if (start == offset)
3670 entry->trim_state = BTRFS_TRIM_STATE_TRIMMING;
3673 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3674 if (ret2 || start >= end) {
3676 * We lossily consider a bitmap trimmed if we only skip
3677 * over regions <= BTRFS_ASYNC_DISCARD_MIN_FILTER.
3679 if (ret2 && minlen <= BTRFS_ASYNC_DISCARD_MIN_FILTER)
3680 end_trimming_bitmap(ctl, entry);
3682 entry->trim_state = BTRFS_TRIM_STATE_UNTRIMMED;
3683 spin_unlock(&ctl->tree_lock);
3684 mutex_unlock(&ctl->cache_writeout_mutex);
3690 * We already trimmed a region, but are using the locking above
3691 * to reset the trim_state.
3693 if (async && *total_trimmed) {
3694 spin_unlock(&ctl->tree_lock);
3695 mutex_unlock(&ctl->cache_writeout_mutex);
3699 bytes = min(bytes, end - start);
3700 if (bytes < minlen || (async && maxlen && bytes > maxlen)) {
3701 spin_unlock(&ctl->tree_lock);
3702 mutex_unlock(&ctl->cache_writeout_mutex);
3707 * Let bytes = BTRFS_MAX_DISCARD_SIZE + X.
3708 * If X < @minlen, we won't trim X when we come back around.
3709 * So trim it now. We differ here from trimming extents as we
3710 * don't keep individual state per bit.
3714 bytes > (max_discard_size + minlen))
3715 bytes = max_discard_size;
3717 bitmap_clear_bits(ctl, entry, start, bytes);
3718 if (entry->bytes == 0)
3719 free_bitmap(ctl, entry);
3721 spin_unlock(&ctl->tree_lock);
3722 trim_entry.start = start;
3723 trim_entry.bytes = bytes;
3724 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3725 mutex_unlock(&ctl->cache_writeout_mutex);
3727 ret = do_trimming(block_group, total_trimmed, start, bytes,
3728 start, bytes, 0, &trim_entry);
3730 reset_trimming_bitmap(ctl, offset);
3731 block_group->discard_cursor =
3732 btrfs_block_group_end(block_group);
3737 offset += BITS_PER_BITMAP * ctl->unit;
3742 block_group->discard_cursor = start;
3744 if (fatal_signal_pending(current)) {
3745 if (start != offset)
3746 reset_trimming_bitmap(ctl, offset);
3755 block_group->discard_cursor = end;
3761 int btrfs_trim_block_group(struct btrfs_block_group *block_group,
3762 u64 *trimmed, u64 start, u64 end, u64 minlen)
3764 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3770 spin_lock(&block_group->lock);
3771 if (block_group->removed) {
3772 spin_unlock(&block_group->lock);
3775 btrfs_freeze_block_group(block_group);
3776 spin_unlock(&block_group->lock);
3778 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, false);
3782 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, 0, false);
3783 div64_u64_rem(end, BITS_PER_BITMAP * ctl->unit, &rem);
3784 /* If we ended in the middle of a bitmap, reset the trimming flag */
3786 reset_trimming_bitmap(ctl, offset_to_bitmap(ctl, end));
3788 btrfs_unfreeze_block_group(block_group);
3792 int btrfs_trim_block_group_extents(struct btrfs_block_group *block_group,
3793 u64 *trimmed, u64 start, u64 end, u64 minlen,
3800 spin_lock(&block_group->lock);
3801 if (block_group->removed) {
3802 spin_unlock(&block_group->lock);
3805 btrfs_freeze_block_group(block_group);
3806 spin_unlock(&block_group->lock);
3808 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen, async);
3809 btrfs_unfreeze_block_group(block_group);
3814 int btrfs_trim_block_group_bitmaps(struct btrfs_block_group *block_group,
3815 u64 *trimmed, u64 start, u64 end, u64 minlen,
3816 u64 maxlen, bool async)
3822 spin_lock(&block_group->lock);
3823 if (block_group->removed) {
3824 spin_unlock(&block_group->lock);
3827 btrfs_freeze_block_group(block_group);
3828 spin_unlock(&block_group->lock);
3830 ret = trim_bitmaps(block_group, trimmed, start, end, minlen, maxlen,
3833 btrfs_unfreeze_block_group(block_group);
3839 * Find the left-most item in the cache tree, and then return the
3840 * smallest inode number in the item.
3842 * Note: the returned inode number may not be the smallest one in
3843 * the tree, if the left-most item is a bitmap.
3845 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3847 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3848 struct btrfs_free_space *entry = NULL;
3851 spin_lock(&ctl->tree_lock);
3853 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3856 entry = rb_entry(rb_first(&ctl->free_space_offset),
3857 struct btrfs_free_space, offset_index);
3859 if (!entry->bitmap) {
3860 ino = entry->offset;
3862 unlink_free_space(ctl, entry);
3866 kmem_cache_free(btrfs_free_space_cachep, entry);
3868 link_free_space(ctl, entry);
3874 ret = search_bitmap(ctl, entry, &offset, &count, true);
3875 /* Logic error; Should be empty if it can't find anything */
3879 bitmap_clear_bits(ctl, entry, offset, 1);
3880 if (entry->bytes == 0)
3881 free_bitmap(ctl, entry);
3884 spin_unlock(&ctl->tree_lock);
3889 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3890 struct btrfs_path *path)
3892 struct inode *inode = NULL;
3894 spin_lock(&root->ino_cache_lock);
3895 if (root->ino_cache_inode)
3896 inode = igrab(root->ino_cache_inode);
3897 spin_unlock(&root->ino_cache_lock);
3901 inode = __lookup_free_space_inode(root, path, 0);
3905 spin_lock(&root->ino_cache_lock);
3906 if (!btrfs_fs_closing(root->fs_info))
3907 root->ino_cache_inode = igrab(inode);
3908 spin_unlock(&root->ino_cache_lock);
3913 int create_free_ino_inode(struct btrfs_root *root,
3914 struct btrfs_trans_handle *trans,
3915 struct btrfs_path *path)
3917 return __create_free_space_inode(root, trans, path,
3918 BTRFS_FREE_INO_OBJECTID, 0);
3921 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3923 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3924 struct btrfs_path *path;
3925 struct inode *inode;
3927 u64 root_gen = btrfs_root_generation(&root->root_item);
3929 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3933 * If we're unmounting then just return, since this does a search on the
3934 * normal root and not the commit root and we could deadlock.
3936 if (btrfs_fs_closing(fs_info))
3939 path = btrfs_alloc_path();
3943 inode = lookup_free_ino_inode(root, path);
3947 if (root_gen != BTRFS_I(inode)->generation)
3950 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3954 "failed to load free ino cache for root %llu",
3955 root->root_key.objectid);
3959 btrfs_free_path(path);
3963 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3964 struct btrfs_trans_handle *trans,
3965 struct btrfs_path *path,
3966 struct inode *inode)
3968 struct btrfs_fs_info *fs_info = root->fs_info;
3969 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3971 struct btrfs_io_ctl io_ctl;
3972 bool release_metadata = true;
3974 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3977 memset(&io_ctl, 0, sizeof(io_ctl));
3978 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3981 * At this point writepages() didn't error out, so our metadata
3982 * reservation is released when the writeback finishes, at
3983 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3984 * with or without an error.
3986 release_metadata = false;
3987 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3991 if (release_metadata)
3992 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3993 inode->i_size, true);
3994 btrfs_debug(fs_info,
3995 "failed to write free ino cache for root %llu error %d",
3996 root->root_key.objectid, ret);
4002 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4004 * Use this if you need to make a bitmap or extent entry specifically, it
4005 * doesn't do any of the merging that add_free_space does, this acts a lot like
4006 * how the free space cache loading stuff works, so you can get really weird
4009 int test_add_free_space_entry(struct btrfs_block_group *cache,
4010 u64 offset, u64 bytes, bool bitmap)
4012 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4013 struct btrfs_free_space *info = NULL, *bitmap_info;
4015 enum btrfs_trim_state trim_state = BTRFS_TRIM_STATE_TRIMMED;
4021 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
4027 spin_lock(&ctl->tree_lock);
4028 info->offset = offset;
4029 info->bytes = bytes;
4030 info->max_extent_size = 0;
4031 ret = link_free_space(ctl, info);
4032 spin_unlock(&ctl->tree_lock);
4034 kmem_cache_free(btrfs_free_space_cachep, info);
4039 map = kmem_cache_zalloc(btrfs_free_space_bitmap_cachep, GFP_NOFS);
4041 kmem_cache_free(btrfs_free_space_cachep, info);
4046 spin_lock(&ctl->tree_lock);
4047 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4052 add_new_bitmap(ctl, info, offset);
4057 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes,
4060 bytes -= bytes_added;
4061 offset += bytes_added;
4062 spin_unlock(&ctl->tree_lock);
4068 kmem_cache_free(btrfs_free_space_cachep, info);
4070 kmem_cache_free(btrfs_free_space_bitmap_cachep, map);
4075 * Checks to see if the given range is in the free space cache. This is really
4076 * just used to check the absence of space, so if there is free space in the
4077 * range at all we will return 1.
4079 int test_check_exists(struct btrfs_block_group *cache,
4080 u64 offset, u64 bytes)
4082 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
4083 struct btrfs_free_space *info;
4086 spin_lock(&ctl->tree_lock);
4087 info = tree_search_offset(ctl, offset, 0, 0);
4089 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
4097 u64 bit_off, bit_bytes;
4099 struct btrfs_free_space *tmp;
4102 bit_bytes = ctl->unit;
4103 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
4105 if (bit_off == offset) {
4108 } else if (bit_off > offset &&
4109 offset + bytes > bit_off) {
4115 n = rb_prev(&info->offset_index);
4117 tmp = rb_entry(n, struct btrfs_free_space,
4119 if (tmp->offset + tmp->bytes < offset)
4121 if (offset + bytes < tmp->offset) {
4122 n = rb_prev(&tmp->offset_index);
4129 n = rb_next(&info->offset_index);
4131 tmp = rb_entry(n, struct btrfs_free_space,
4133 if (offset + bytes < tmp->offset)
4135 if (tmp->offset + tmp->bytes < offset) {
4136 n = rb_next(&tmp->offset_index);
4147 if (info->offset == offset) {
4152 if (offset > info->offset && offset < info->offset + info->bytes)
4155 spin_unlock(&ctl->tree_lock);
4158 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */