2 * Copyright (C) 2008 Red Hat. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/pagemap.h>
20 #include <linux/sched.h>
21 #include <linux/sched/signal.h>
22 #include <linux/slab.h>
23 #include <linux/math64.h>
24 #include <linux/ratelimit.h>
25 #include <linux/sched/mm.h>
27 #include "free-space-cache.h"
28 #include "transaction.h"
30 #include "extent_io.h"
31 #include "inode-map.h"
34 #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
35 #define MAX_CACHE_BYTES_PER_GIG SZ_32K
37 struct btrfs_trim_range {
40 struct list_head list;
43 static int link_free_space(struct btrfs_free_space_ctl *ctl,
44 struct btrfs_free_space *info);
45 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
46 struct btrfs_free_space *info);
47 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
48 struct btrfs_trans_handle *trans,
49 struct btrfs_io_ctl *io_ctl,
50 struct btrfs_path *path);
52 static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
53 struct btrfs_path *path,
56 struct btrfs_fs_info *fs_info = root->fs_info;
58 struct btrfs_key location;
59 struct btrfs_disk_key disk_key;
60 struct btrfs_free_space_header *header;
61 struct extent_buffer *leaf;
62 struct inode *inode = NULL;
66 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
70 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 btrfs_release_path(path);
75 return ERR_PTR(-ENOENT);
78 leaf = path->nodes[0];
79 header = btrfs_item_ptr(leaf, path->slots[0],
80 struct btrfs_free_space_header);
81 btrfs_free_space_key(leaf, header, &disk_key);
82 btrfs_disk_key_to_cpu(&location, &disk_key);
83 btrfs_release_path(path);
86 * We are often under a trans handle at this point, so we need to make
87 * sure NOFS is set to keep us from deadlocking.
89 nofs_flag = memalloc_nofs_save();
90 inode = btrfs_iget(fs_info->sb, &location, root, NULL);
91 memalloc_nofs_restore(nofs_flag);
94 if (is_bad_inode(inode)) {
96 return ERR_PTR(-ENOENT);
99 mapping_set_gfp_mask(inode->i_mapping,
100 mapping_gfp_constraint(inode->i_mapping,
101 ~(__GFP_FS | __GFP_HIGHMEM)));
106 struct inode *lookup_free_space_inode(struct btrfs_fs_info *fs_info,
107 struct btrfs_block_group_cache
108 *block_group, struct btrfs_path *path)
110 struct inode *inode = NULL;
111 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
113 spin_lock(&block_group->lock);
114 if (block_group->inode)
115 inode = igrab(block_group->inode);
116 spin_unlock(&block_group->lock);
120 inode = __lookup_free_space_inode(fs_info->tree_root, path,
121 block_group->key.objectid);
125 spin_lock(&block_group->lock);
126 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
127 btrfs_info(fs_info, "Old style space inode found, converting.");
128 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
129 BTRFS_INODE_NODATACOW;
130 block_group->disk_cache_state = BTRFS_DC_CLEAR;
133 if (!block_group->iref) {
134 block_group->inode = igrab(inode);
135 block_group->iref = 1;
137 spin_unlock(&block_group->lock);
142 static int __create_free_space_inode(struct btrfs_root *root,
143 struct btrfs_trans_handle *trans,
144 struct btrfs_path *path,
147 struct btrfs_key key;
148 struct btrfs_disk_key disk_key;
149 struct btrfs_free_space_header *header;
150 struct btrfs_inode_item *inode_item;
151 struct extent_buffer *leaf;
152 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
155 ret = btrfs_insert_empty_inode(trans, root, path, ino);
159 /* We inline crc's for the free disk space cache */
160 if (ino != BTRFS_FREE_INO_OBJECTID)
161 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
163 leaf = path->nodes[0];
164 inode_item = btrfs_item_ptr(leaf, path->slots[0],
165 struct btrfs_inode_item);
166 btrfs_item_key(leaf, &disk_key, path->slots[0]);
167 memzero_extent_buffer(leaf, (unsigned long)inode_item,
168 sizeof(*inode_item));
169 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
170 btrfs_set_inode_size(leaf, inode_item, 0);
171 btrfs_set_inode_nbytes(leaf, inode_item, 0);
172 btrfs_set_inode_uid(leaf, inode_item, 0);
173 btrfs_set_inode_gid(leaf, inode_item, 0);
174 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
175 btrfs_set_inode_flags(leaf, inode_item, flags);
176 btrfs_set_inode_nlink(leaf, inode_item, 1);
177 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
178 btrfs_set_inode_block_group(leaf, inode_item, offset);
179 btrfs_mark_buffer_dirty(leaf);
180 btrfs_release_path(path);
182 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
185 ret = btrfs_insert_empty_item(trans, root, path, &key,
186 sizeof(struct btrfs_free_space_header));
188 btrfs_release_path(path);
192 leaf = path->nodes[0];
193 header = btrfs_item_ptr(leaf, path->slots[0],
194 struct btrfs_free_space_header);
195 memzero_extent_buffer(leaf, (unsigned long)header, sizeof(*header));
196 btrfs_set_free_space_key(leaf, header, &disk_key);
197 btrfs_mark_buffer_dirty(leaf);
198 btrfs_release_path(path);
203 int create_free_space_inode(struct btrfs_fs_info *fs_info,
204 struct btrfs_trans_handle *trans,
205 struct btrfs_block_group_cache *block_group,
206 struct btrfs_path *path)
211 ret = btrfs_find_free_objectid(fs_info->tree_root, &ino);
215 return __create_free_space_inode(fs_info->tree_root, trans, path, ino,
216 block_group->key.objectid);
219 int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
220 struct btrfs_block_rsv *rsv)
225 /* 1 for slack space, 1 for updating the inode */
226 needed_bytes = btrfs_calc_trunc_metadata_size(fs_info, 1) +
227 btrfs_calc_trans_metadata_size(fs_info, 1);
229 spin_lock(&rsv->lock);
230 if (rsv->reserved < needed_bytes)
234 spin_unlock(&rsv->lock);
238 int btrfs_truncate_free_space_cache(struct btrfs_trans_handle *trans,
239 struct btrfs_block_group_cache *block_group,
242 struct btrfs_root *root = BTRFS_I(inode)->root;
247 struct btrfs_path *path = btrfs_alloc_path();
254 mutex_lock(&trans->transaction->cache_write_mutex);
255 if (!list_empty(&block_group->io_list)) {
256 list_del_init(&block_group->io_list);
258 btrfs_wait_cache_io(trans, block_group, path);
259 btrfs_put_block_group(block_group);
263 * now that we've truncated the cache away, its no longer
266 spin_lock(&block_group->lock);
267 block_group->disk_cache_state = BTRFS_DC_CLEAR;
268 spin_unlock(&block_group->lock);
269 btrfs_free_path(path);
272 btrfs_i_size_write(BTRFS_I(inode), 0);
273 truncate_pagecache(inode, 0);
276 * We don't need an orphan item because truncating the free space cache
277 * will never be split across transactions.
278 * We don't need to check for -EAGAIN because we're a free space
281 ret = btrfs_truncate_inode_items(trans, root, inode,
282 0, BTRFS_EXTENT_DATA_KEY);
286 ret = btrfs_update_inode(trans, root, inode);
290 mutex_unlock(&trans->transaction->cache_write_mutex);
292 btrfs_abort_transaction(trans, ret);
297 static void readahead_cache(struct inode *inode)
299 struct file_ra_state *ra;
300 unsigned long last_index;
302 ra = kzalloc(sizeof(*ra), GFP_NOFS);
306 file_ra_state_init(ra, inode->i_mapping);
307 last_index = (i_size_read(inode) - 1) >> PAGE_SHIFT;
309 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
314 static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
320 num_pages = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
322 if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FREE_INO_OBJECTID)
325 /* Make sure we can fit our crcs into the first page */
326 if (write && check_crcs &&
327 (num_pages * sizeof(u32)) >= PAGE_SIZE)
330 memset(io_ctl, 0, sizeof(struct btrfs_io_ctl));
332 io_ctl->pages = kcalloc(num_pages, sizeof(struct page *), GFP_NOFS);
336 io_ctl->num_pages = num_pages;
337 io_ctl->fs_info = btrfs_sb(inode->i_sb);
338 io_ctl->check_crcs = check_crcs;
339 io_ctl->inode = inode;
344 static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
346 kfree(io_ctl->pages);
347 io_ctl->pages = NULL;
350 static void io_ctl_unmap_page(struct btrfs_io_ctl *io_ctl)
358 static void io_ctl_map_page(struct btrfs_io_ctl *io_ctl, int clear)
360 ASSERT(io_ctl->index < io_ctl->num_pages);
361 io_ctl->page = io_ctl->pages[io_ctl->index++];
362 io_ctl->cur = page_address(io_ctl->page);
363 io_ctl->orig = io_ctl->cur;
364 io_ctl->size = PAGE_SIZE;
366 clear_page(io_ctl->cur);
369 static void io_ctl_drop_pages(struct btrfs_io_ctl *io_ctl)
373 io_ctl_unmap_page(io_ctl);
375 for (i = 0; i < io_ctl->num_pages; i++) {
376 if (io_ctl->pages[i]) {
377 ClearPageChecked(io_ctl->pages[i]);
378 unlock_page(io_ctl->pages[i]);
379 put_page(io_ctl->pages[i]);
384 static int io_ctl_prepare_pages(struct btrfs_io_ctl *io_ctl, struct inode *inode,
388 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
391 for (i = 0; i < io_ctl->num_pages; i++) {
392 page = find_or_create_page(inode->i_mapping, i, mask);
394 io_ctl_drop_pages(io_ctl);
397 io_ctl->pages[i] = page;
398 if (uptodate && !PageUptodate(page)) {
399 btrfs_readpage(NULL, page);
401 if (page->mapping != inode->i_mapping) {
402 btrfs_err(BTRFS_I(inode)->root->fs_info,
403 "free space cache page truncated");
404 io_ctl_drop_pages(io_ctl);
407 if (!PageUptodate(page)) {
408 btrfs_err(BTRFS_I(inode)->root->fs_info,
409 "error reading free space cache");
410 io_ctl_drop_pages(io_ctl);
416 for (i = 0; i < io_ctl->num_pages; i++) {
417 clear_page_dirty_for_io(io_ctl->pages[i]);
418 set_page_extent_mapped(io_ctl->pages[i]);
424 static void io_ctl_set_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
428 io_ctl_map_page(io_ctl, 1);
431 * Skip the csum areas. If we don't check crcs then we just have a
432 * 64bit chunk at the front of the first page.
434 if (io_ctl->check_crcs) {
435 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
436 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
438 io_ctl->cur += sizeof(u64);
439 io_ctl->size -= sizeof(u64) * 2;
443 *val = cpu_to_le64(generation);
444 io_ctl->cur += sizeof(u64);
447 static int io_ctl_check_generation(struct btrfs_io_ctl *io_ctl, u64 generation)
452 * Skip the crc area. If we don't check crcs then we just have a 64bit
453 * chunk at the front of the first page.
455 if (io_ctl->check_crcs) {
456 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
457 io_ctl->size -= sizeof(u64) +
458 (sizeof(u32) * io_ctl->num_pages);
460 io_ctl->cur += sizeof(u64);
461 io_ctl->size -= sizeof(u64) * 2;
465 if (le64_to_cpu(*gen) != generation) {
466 btrfs_err_rl(io_ctl->fs_info,
467 "space cache generation (%llu) does not match inode (%llu)",
469 io_ctl_unmap_page(io_ctl);
472 io_ctl->cur += sizeof(u64);
476 static void io_ctl_set_crc(struct btrfs_io_ctl *io_ctl, int index)
482 if (!io_ctl->check_crcs) {
483 io_ctl_unmap_page(io_ctl);
488 offset = sizeof(u32) * io_ctl->num_pages;
490 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
492 btrfs_csum_final(crc, (u8 *)&crc);
493 io_ctl_unmap_page(io_ctl);
494 tmp = page_address(io_ctl->pages[0]);
499 static int io_ctl_check_crc(struct btrfs_io_ctl *io_ctl, int index)
505 if (!io_ctl->check_crcs) {
506 io_ctl_map_page(io_ctl, 0);
511 offset = sizeof(u32) * io_ctl->num_pages;
513 tmp = page_address(io_ctl->pages[0]);
517 io_ctl_map_page(io_ctl, 0);
518 crc = btrfs_csum_data(io_ctl->orig + offset, crc,
520 btrfs_csum_final(crc, (u8 *)&crc);
522 btrfs_err_rl(io_ctl->fs_info,
523 "csum mismatch on free space cache");
524 io_ctl_unmap_page(io_ctl);
531 static int io_ctl_add_entry(struct btrfs_io_ctl *io_ctl, u64 offset, u64 bytes,
534 struct btrfs_free_space_entry *entry;
540 entry->offset = cpu_to_le64(offset);
541 entry->bytes = cpu_to_le64(bytes);
542 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
543 BTRFS_FREE_SPACE_EXTENT;
544 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
545 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
547 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
550 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
552 /* No more pages to map */
553 if (io_ctl->index >= io_ctl->num_pages)
556 /* map the next page */
557 io_ctl_map_page(io_ctl, 1);
561 static int io_ctl_add_bitmap(struct btrfs_io_ctl *io_ctl, void *bitmap)
567 * If we aren't at the start of the current page, unmap this one and
568 * map the next one if there is any left.
570 if (io_ctl->cur != io_ctl->orig) {
571 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
572 if (io_ctl->index >= io_ctl->num_pages)
574 io_ctl_map_page(io_ctl, 0);
577 memcpy(io_ctl->cur, bitmap, PAGE_SIZE);
578 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
579 if (io_ctl->index < io_ctl->num_pages)
580 io_ctl_map_page(io_ctl, 0);
584 static void io_ctl_zero_remaining_pages(struct btrfs_io_ctl *io_ctl)
587 * If we're not on the boundary we know we've modified the page and we
588 * need to crc the page.
590 if (io_ctl->cur != io_ctl->orig)
591 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
593 io_ctl_unmap_page(io_ctl);
595 while (io_ctl->index < io_ctl->num_pages) {
596 io_ctl_map_page(io_ctl, 1);
597 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
601 static int io_ctl_read_entry(struct btrfs_io_ctl *io_ctl,
602 struct btrfs_free_space *entry, u8 *type)
604 struct btrfs_free_space_entry *e;
608 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
614 entry->offset = le64_to_cpu(e->offset);
615 entry->bytes = le64_to_cpu(e->bytes);
617 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
618 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
620 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
623 io_ctl_unmap_page(io_ctl);
628 static int io_ctl_read_bitmap(struct btrfs_io_ctl *io_ctl,
629 struct btrfs_free_space *entry)
633 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
637 memcpy(entry->bitmap, io_ctl->cur, PAGE_SIZE);
638 io_ctl_unmap_page(io_ctl);
644 * Since we attach pinned extents after the fact we can have contiguous sections
645 * of free space that are split up in entries. This poses a problem with the
646 * tree logging stuff since it could have allocated across what appears to be 2
647 * entries since we would have merged the entries when adding the pinned extents
648 * back to the free space cache. So run through the space cache that we just
649 * loaded and merge contiguous entries. This will make the log replay stuff not
650 * blow up and it will make for nicer allocator behavior.
652 static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
654 struct btrfs_free_space *e, *prev = NULL;
658 spin_lock(&ctl->tree_lock);
659 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
660 e = rb_entry(n, struct btrfs_free_space, offset_index);
663 if (e->bitmap || prev->bitmap)
665 if (prev->offset + prev->bytes == e->offset) {
666 unlink_free_space(ctl, prev);
667 unlink_free_space(ctl, e);
668 prev->bytes += e->bytes;
669 kmem_cache_free(btrfs_free_space_cachep, e);
670 link_free_space(ctl, prev);
672 spin_unlock(&ctl->tree_lock);
678 spin_unlock(&ctl->tree_lock);
681 static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
682 struct btrfs_free_space_ctl *ctl,
683 struct btrfs_path *path, u64 offset)
685 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
686 struct btrfs_free_space_header *header;
687 struct extent_buffer *leaf;
688 struct btrfs_io_ctl io_ctl;
689 struct btrfs_key key;
690 struct btrfs_free_space *e, *n;
698 /* Nothing in the space cache, goodbye */
699 if (!i_size_read(inode))
702 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
706 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
710 btrfs_release_path(path);
716 leaf = path->nodes[0];
717 header = btrfs_item_ptr(leaf, path->slots[0],
718 struct btrfs_free_space_header);
719 num_entries = btrfs_free_space_entries(leaf, header);
720 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
721 generation = btrfs_free_space_generation(leaf, header);
722 btrfs_release_path(path);
724 if (!BTRFS_I(inode)->generation) {
726 "the free space cache file (%llu) is invalid, skip it",
731 if (BTRFS_I(inode)->generation != generation) {
733 "free space inode generation (%llu) did not match free space cache generation (%llu)",
734 BTRFS_I(inode)->generation, generation);
741 ret = io_ctl_init(&io_ctl, inode, 0);
745 readahead_cache(inode);
747 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
751 ret = io_ctl_check_crc(&io_ctl, 0);
755 ret = io_ctl_check_generation(&io_ctl, generation);
759 while (num_entries) {
760 e = kmem_cache_zalloc(btrfs_free_space_cachep,
767 ret = io_ctl_read_entry(&io_ctl, e, &type);
769 kmem_cache_free(btrfs_free_space_cachep, e);
775 kmem_cache_free(btrfs_free_space_cachep, e);
779 if (type == BTRFS_FREE_SPACE_EXTENT) {
780 spin_lock(&ctl->tree_lock);
781 ret = link_free_space(ctl, e);
782 spin_unlock(&ctl->tree_lock);
785 "Duplicate entries in free space cache, dumping");
786 kmem_cache_free(btrfs_free_space_cachep, e);
792 e->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
796 btrfs_free_space_cachep, e);
799 spin_lock(&ctl->tree_lock);
800 ret = link_free_space(ctl, e);
801 ctl->total_bitmaps++;
802 ctl->op->recalc_thresholds(ctl);
803 spin_unlock(&ctl->tree_lock);
806 "Duplicate entries in free space cache, dumping");
807 kmem_cache_free(btrfs_free_space_cachep, e);
810 list_add_tail(&e->list, &bitmaps);
816 io_ctl_unmap_page(&io_ctl);
819 * We add the bitmaps at the end of the entries in order that
820 * the bitmap entries are added to the cache.
822 list_for_each_entry_safe(e, n, &bitmaps, list) {
823 list_del_init(&e->list);
824 ret = io_ctl_read_bitmap(&io_ctl, e);
829 io_ctl_drop_pages(&io_ctl);
830 merge_space_tree(ctl);
833 io_ctl_free(&io_ctl);
836 io_ctl_drop_pages(&io_ctl);
837 __btrfs_remove_free_space_cache(ctl);
841 int load_free_space_cache(struct btrfs_fs_info *fs_info,
842 struct btrfs_block_group_cache *block_group)
844 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
846 struct btrfs_path *path;
849 u64 used = btrfs_block_group_used(&block_group->item);
852 * If this block group has been marked to be cleared for one reason or
853 * another then we can't trust the on disk cache, so just return.
855 spin_lock(&block_group->lock);
856 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
857 spin_unlock(&block_group->lock);
860 spin_unlock(&block_group->lock);
862 path = btrfs_alloc_path();
865 path->search_commit_root = 1;
866 path->skip_locking = 1;
868 inode = lookup_free_space_inode(fs_info, block_group, path);
870 btrfs_free_path(path);
874 /* We may have converted the inode and made the cache invalid. */
875 spin_lock(&block_group->lock);
876 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
877 spin_unlock(&block_group->lock);
878 btrfs_free_path(path);
881 spin_unlock(&block_group->lock);
883 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
884 path, block_group->key.objectid);
885 btrfs_free_path(path);
889 spin_lock(&ctl->tree_lock);
890 matched = (ctl->free_space == (block_group->key.offset - used -
891 block_group->bytes_super));
892 spin_unlock(&ctl->tree_lock);
895 __btrfs_remove_free_space_cache(ctl);
897 "block group %llu has wrong amount of free space",
898 block_group->key.objectid);
903 /* This cache is bogus, make sure it gets cleared */
904 spin_lock(&block_group->lock);
905 block_group->disk_cache_state = BTRFS_DC_CLEAR;
906 spin_unlock(&block_group->lock);
910 "failed to load free space cache for block group %llu, rebuilding it now",
911 block_group->key.objectid);
918 static noinline_for_stack
919 int write_cache_extent_entries(struct btrfs_io_ctl *io_ctl,
920 struct btrfs_free_space_ctl *ctl,
921 struct btrfs_block_group_cache *block_group,
922 int *entries, int *bitmaps,
923 struct list_head *bitmap_list)
926 struct btrfs_free_cluster *cluster = NULL;
927 struct btrfs_free_cluster *cluster_locked = NULL;
928 struct rb_node *node = rb_first(&ctl->free_space_offset);
929 struct btrfs_trim_range *trim_entry;
931 /* Get the cluster for this block_group if it exists */
932 if (block_group && !list_empty(&block_group->cluster_list)) {
933 cluster = list_entry(block_group->cluster_list.next,
934 struct btrfs_free_cluster,
938 if (!node && cluster) {
939 cluster_locked = cluster;
940 spin_lock(&cluster_locked->lock);
941 node = rb_first(&cluster->root);
945 /* Write out the extent entries */
947 struct btrfs_free_space *e;
949 e = rb_entry(node, struct btrfs_free_space, offset_index);
952 ret = io_ctl_add_entry(io_ctl, e->offset, e->bytes,
958 list_add_tail(&e->list, bitmap_list);
961 node = rb_next(node);
962 if (!node && cluster) {
963 node = rb_first(&cluster->root);
964 cluster_locked = cluster;
965 spin_lock(&cluster_locked->lock);
969 if (cluster_locked) {
970 spin_unlock(&cluster_locked->lock);
971 cluster_locked = NULL;
975 * Make sure we don't miss any range that was removed from our rbtree
976 * because trimming is running. Otherwise after a umount+mount (or crash
977 * after committing the transaction) we would leak free space and get
978 * an inconsistent free space cache report from fsck.
980 list_for_each_entry(trim_entry, &ctl->trimming_ranges, list) {
981 ret = io_ctl_add_entry(io_ctl, trim_entry->start,
982 trim_entry->bytes, NULL);
991 spin_unlock(&cluster_locked->lock);
995 static noinline_for_stack int
996 update_cache_item(struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
999 struct btrfs_path *path, u64 offset,
1000 int entries, int bitmaps)
1002 struct btrfs_key key;
1003 struct btrfs_free_space_header *header;
1004 struct extent_buffer *leaf;
1007 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
1008 key.offset = offset;
1011 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1013 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1014 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1018 leaf = path->nodes[0];
1020 struct btrfs_key found_key;
1021 ASSERT(path->slots[0]);
1023 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1024 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
1025 found_key.offset != offset) {
1026 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
1028 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
1030 btrfs_release_path(path);
1035 BTRFS_I(inode)->generation = trans->transid;
1036 header = btrfs_item_ptr(leaf, path->slots[0],
1037 struct btrfs_free_space_header);
1038 btrfs_set_free_space_entries(leaf, header, entries);
1039 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1040 btrfs_set_free_space_generation(leaf, header, trans->transid);
1041 btrfs_mark_buffer_dirty(leaf);
1042 btrfs_release_path(path);
1050 static noinline_for_stack int
1051 write_pinned_extent_entries(struct btrfs_fs_info *fs_info,
1052 struct btrfs_block_group_cache *block_group,
1053 struct btrfs_io_ctl *io_ctl,
1056 u64 start, extent_start, extent_end, len;
1057 struct extent_io_tree *unpin = NULL;
1064 * We want to add any pinned extents to our free space cache
1065 * so we don't leak the space
1067 * We shouldn't have switched the pinned extents yet so this is the
1070 unpin = fs_info->pinned_extents;
1072 start = block_group->key.objectid;
1074 while (start < block_group->key.objectid + block_group->key.offset) {
1075 ret = find_first_extent_bit(unpin, start,
1076 &extent_start, &extent_end,
1077 EXTENT_DIRTY, NULL);
1081 /* This pinned extent is out of our range */
1082 if (extent_start >= block_group->key.objectid +
1083 block_group->key.offset)
1086 extent_start = max(extent_start, start);
1087 extent_end = min(block_group->key.objectid +
1088 block_group->key.offset, extent_end + 1);
1089 len = extent_end - extent_start;
1092 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1102 static noinline_for_stack int
1103 write_bitmap_entries(struct btrfs_io_ctl *io_ctl, struct list_head *bitmap_list)
1105 struct btrfs_free_space *entry, *next;
1108 /* Write out the bitmaps */
1109 list_for_each_entry_safe(entry, next, bitmap_list, list) {
1110 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1113 list_del_init(&entry->list);
1119 static int flush_dirty_cache(struct inode *inode)
1123 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1125 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1126 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1132 static void noinline_for_stack
1133 cleanup_bitmap_list(struct list_head *bitmap_list)
1135 struct btrfs_free_space *entry, *next;
1137 list_for_each_entry_safe(entry, next, bitmap_list, list)
1138 list_del_init(&entry->list);
1141 static void noinline_for_stack
1142 cleanup_write_cache_enospc(struct inode *inode,
1143 struct btrfs_io_ctl *io_ctl,
1144 struct extent_state **cached_state)
1146 io_ctl_drop_pages(io_ctl);
1147 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1148 i_size_read(inode) - 1, cached_state,
1152 static int __btrfs_wait_cache_io(struct btrfs_root *root,
1153 struct btrfs_trans_handle *trans,
1154 struct btrfs_block_group_cache *block_group,
1155 struct btrfs_io_ctl *io_ctl,
1156 struct btrfs_path *path, u64 offset)
1159 struct inode *inode = io_ctl->inode;
1160 struct btrfs_fs_info *fs_info;
1165 fs_info = btrfs_sb(inode->i_sb);
1167 /* Flush the dirty pages in the cache file. */
1168 ret = flush_dirty_cache(inode);
1172 /* Update the cache item to tell everyone this cache file is valid. */
1173 ret = update_cache_item(trans, root, inode, path, offset,
1174 io_ctl->entries, io_ctl->bitmaps);
1177 invalidate_inode_pages2(inode->i_mapping);
1178 BTRFS_I(inode)->generation = 0;
1182 "failed to write free space cache for block group %llu",
1183 block_group->key.objectid);
1187 btrfs_update_inode(trans, root, inode);
1190 /* the dirty list is protected by the dirty_bgs_lock */
1191 spin_lock(&trans->transaction->dirty_bgs_lock);
1193 /* the disk_cache_state is protected by the block group lock */
1194 spin_lock(&block_group->lock);
1197 * only mark this as written if we didn't get put back on
1198 * the dirty list while waiting for IO. Otherwise our
1199 * cache state won't be right, and we won't get written again
1201 if (!ret && list_empty(&block_group->dirty_list))
1202 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1204 block_group->disk_cache_state = BTRFS_DC_ERROR;
1206 spin_unlock(&block_group->lock);
1207 spin_unlock(&trans->transaction->dirty_bgs_lock);
1208 io_ctl->inode = NULL;
1216 static int btrfs_wait_cache_io_root(struct btrfs_root *root,
1217 struct btrfs_trans_handle *trans,
1218 struct btrfs_io_ctl *io_ctl,
1219 struct btrfs_path *path)
1221 return __btrfs_wait_cache_io(root, trans, NULL, io_ctl, path, 0);
1224 int btrfs_wait_cache_io(struct btrfs_trans_handle *trans,
1225 struct btrfs_block_group_cache *block_group,
1226 struct btrfs_path *path)
1228 return __btrfs_wait_cache_io(block_group->fs_info->tree_root, trans,
1229 block_group, &block_group->io_ctl,
1230 path, block_group->key.objectid);
1234 * __btrfs_write_out_cache - write out cached info to an inode
1235 * @root - the root the inode belongs to
1236 * @ctl - the free space cache we are going to write out
1237 * @block_group - the block_group for this cache if it belongs to a block_group
1238 * @trans - the trans handle
1240 * This function writes out a free space cache struct to disk for quick recovery
1241 * on mount. This will return 0 if it was successful in writing the cache out,
1242 * or an errno if it was not.
1244 static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1245 struct btrfs_free_space_ctl *ctl,
1246 struct btrfs_block_group_cache *block_group,
1247 struct btrfs_io_ctl *io_ctl,
1248 struct btrfs_trans_handle *trans)
1250 struct btrfs_fs_info *fs_info = root->fs_info;
1251 struct extent_state *cached_state = NULL;
1252 LIST_HEAD(bitmap_list);
1258 if (!i_size_read(inode))
1261 WARN_ON(io_ctl->pages);
1262 ret = io_ctl_init(io_ctl, inode, 1);
1266 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1267 down_write(&block_group->data_rwsem);
1268 spin_lock(&block_group->lock);
1269 if (block_group->delalloc_bytes) {
1270 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1271 spin_unlock(&block_group->lock);
1272 up_write(&block_group->data_rwsem);
1273 BTRFS_I(inode)->generation = 0;
1278 spin_unlock(&block_group->lock);
1281 /* Lock all pages first so we can lock the extent safely. */
1282 ret = io_ctl_prepare_pages(io_ctl, inode, 0);
1286 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1289 io_ctl_set_generation(io_ctl, trans->transid);
1291 mutex_lock(&ctl->cache_writeout_mutex);
1292 /* Write out the extent entries in the free space cache */
1293 spin_lock(&ctl->tree_lock);
1294 ret = write_cache_extent_entries(io_ctl, ctl,
1295 block_group, &entries, &bitmaps,
1298 goto out_nospc_locked;
1301 * Some spaces that are freed in the current transaction are pinned,
1302 * they will be added into free space cache after the transaction is
1303 * committed, we shouldn't lose them.
1305 * If this changes while we are working we'll get added back to
1306 * the dirty list and redo it. No locking needed
1308 ret = write_pinned_extent_entries(fs_info, block_group,
1311 goto out_nospc_locked;
1314 * At last, we write out all the bitmaps and keep cache_writeout_mutex
1315 * locked while doing it because a concurrent trim can be manipulating
1316 * or freeing the bitmap.
1318 ret = write_bitmap_entries(io_ctl, &bitmap_list);
1319 spin_unlock(&ctl->tree_lock);
1320 mutex_unlock(&ctl->cache_writeout_mutex);
1324 /* Zero out the rest of the pages just to make sure */
1325 io_ctl_zero_remaining_pages(io_ctl);
1327 /* Everything is written out, now we dirty the pages in the file. */
1328 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1329 i_size_read(inode), &cached_state);
1333 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1334 up_write(&block_group->data_rwsem);
1336 * Release the pages and unlock the extent, we will flush
1339 io_ctl_drop_pages(io_ctl);
1340 io_ctl_free(io_ctl);
1342 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1343 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1346 * at this point the pages are under IO and we're happy,
1347 * The caller is responsible for waiting on them and updating the
1348 * the cache and the inode
1350 io_ctl->entries = entries;
1351 io_ctl->bitmaps = bitmaps;
1353 ret = btrfs_fdatawrite_range(inode, 0, (u64)-1);
1360 io_ctl->inode = NULL;
1361 io_ctl_free(io_ctl);
1363 invalidate_inode_pages2(inode->i_mapping);
1364 BTRFS_I(inode)->generation = 0;
1366 btrfs_update_inode(trans, root, inode);
1372 cleanup_bitmap_list(&bitmap_list);
1373 spin_unlock(&ctl->tree_lock);
1374 mutex_unlock(&ctl->cache_writeout_mutex);
1377 cleanup_write_cache_enospc(inode, io_ctl, &cached_state);
1380 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1381 up_write(&block_group->data_rwsem);
1386 int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
1387 struct btrfs_trans_handle *trans,
1388 struct btrfs_block_group_cache *block_group,
1389 struct btrfs_path *path)
1391 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1392 struct inode *inode;
1395 spin_lock(&block_group->lock);
1396 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1397 spin_unlock(&block_group->lock);
1400 spin_unlock(&block_group->lock);
1402 inode = lookup_free_space_inode(fs_info, block_group, path);
1406 ret = __btrfs_write_out_cache(fs_info->tree_root, inode, ctl,
1407 block_group, &block_group->io_ctl, trans);
1411 "failed to write free space cache for block group %llu",
1412 block_group->key.objectid);
1414 spin_lock(&block_group->lock);
1415 block_group->disk_cache_state = BTRFS_DC_ERROR;
1416 spin_unlock(&block_group->lock);
1418 block_group->io_ctl.inode = NULL;
1423 * if ret == 0 the caller is expected to call btrfs_wait_cache_io
1424 * to wait for IO and put the inode
1430 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1433 ASSERT(offset >= bitmap_start);
1434 offset -= bitmap_start;
1435 return (unsigned long)(div_u64(offset, unit));
1438 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1440 return (unsigned long)(div_u64(bytes, unit));
1443 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1447 u64 bytes_per_bitmap;
1449 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1450 bitmap_start = offset - ctl->start;
1451 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1452 bitmap_start *= bytes_per_bitmap;
1453 bitmap_start += ctl->start;
1455 return bitmap_start;
1458 static int tree_insert_offset(struct rb_root *root, u64 offset,
1459 struct rb_node *node, int bitmap)
1461 struct rb_node **p = &root->rb_node;
1462 struct rb_node *parent = NULL;
1463 struct btrfs_free_space *info;
1467 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1469 if (offset < info->offset) {
1471 } else if (offset > info->offset) {
1472 p = &(*p)->rb_right;
1475 * we could have a bitmap entry and an extent entry
1476 * share the same offset. If this is the case, we want
1477 * the extent entry to always be found first if we do a
1478 * linear search through the tree, since we want to have
1479 * the quickest allocation time, and allocating from an
1480 * extent is faster than allocating from a bitmap. So
1481 * if we're inserting a bitmap and we find an entry at
1482 * this offset, we want to go right, or after this entry
1483 * logically. If we are inserting an extent and we've
1484 * found a bitmap, we want to go left, or before
1492 p = &(*p)->rb_right;
1494 if (!info->bitmap) {
1503 rb_link_node(node, parent, p);
1504 rb_insert_color(node, root);
1510 * searches the tree for the given offset.
1512 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1513 * want a section that has at least bytes size and comes at or after the given
1516 static struct btrfs_free_space *
1517 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1518 u64 offset, int bitmap_only, int fuzzy)
1520 struct rb_node *n = ctl->free_space_offset.rb_node;
1521 struct btrfs_free_space *entry, *prev = NULL;
1523 /* find entry that is closest to the 'offset' */
1530 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1533 if (offset < entry->offset)
1535 else if (offset > entry->offset)
1548 * bitmap entry and extent entry may share same offset,
1549 * in that case, bitmap entry comes after extent entry.
1554 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1555 if (entry->offset != offset)
1558 WARN_ON(!entry->bitmap);
1561 if (entry->bitmap) {
1563 * if previous extent entry covers the offset,
1564 * we should return it instead of the bitmap entry
1566 n = rb_prev(&entry->offset_index);
1568 prev = rb_entry(n, struct btrfs_free_space,
1570 if (!prev->bitmap &&
1571 prev->offset + prev->bytes > offset)
1581 /* find last entry before the 'offset' */
1583 if (entry->offset > offset) {
1584 n = rb_prev(&entry->offset_index);
1586 entry = rb_entry(n, struct btrfs_free_space,
1588 ASSERT(entry->offset <= offset);
1597 if (entry->bitmap) {
1598 n = rb_prev(&entry->offset_index);
1600 prev = rb_entry(n, struct btrfs_free_space,
1602 if (!prev->bitmap &&
1603 prev->offset + prev->bytes > offset)
1606 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1608 } else if (entry->offset + entry->bytes > offset)
1615 if (entry->bitmap) {
1616 if (entry->offset + BITS_PER_BITMAP *
1620 if (entry->offset + entry->bytes > offset)
1624 n = rb_next(&entry->offset_index);
1627 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1633 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1634 struct btrfs_free_space *info)
1636 rb_erase(&info->offset_index, &ctl->free_space_offset);
1637 ctl->free_extents--;
1640 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1641 struct btrfs_free_space *info)
1643 __unlink_free_space(ctl, info);
1644 ctl->free_space -= info->bytes;
1647 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1648 struct btrfs_free_space *info)
1652 ASSERT(info->bytes || info->bitmap);
1653 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1654 &info->offset_index, (info->bitmap != NULL));
1658 ctl->free_space += info->bytes;
1659 ctl->free_extents++;
1663 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1665 struct btrfs_block_group_cache *block_group = ctl->private;
1669 u64 size = block_group->key.offset;
1670 u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
1671 u64 max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1673 max_bitmaps = max_t(u64, max_bitmaps, 1);
1675 ASSERT(ctl->total_bitmaps <= max_bitmaps);
1678 * The goal is to keep the total amount of memory used per 1gb of space
1679 * at or below 32k, so we need to adjust how much memory we allow to be
1680 * used by extent based free space tracking
1683 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1685 max_bytes = MAX_CACHE_BYTES_PER_GIG * div_u64(size, SZ_1G);
1688 * we want to account for 1 more bitmap than what we have so we can make
1689 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1690 * we add more bitmaps.
1692 bitmap_bytes = (ctl->total_bitmaps + 1) * ctl->unit;
1694 if (bitmap_bytes >= max_bytes) {
1695 ctl->extents_thresh = 0;
1700 * we want the extent entry threshold to always be at most 1/2 the max
1701 * bytes we can have, or whatever is less than that.
1703 extent_bytes = max_bytes - bitmap_bytes;
1704 extent_bytes = min_t(u64, extent_bytes, max_bytes >> 1);
1706 ctl->extents_thresh =
1707 div_u64(extent_bytes, sizeof(struct btrfs_free_space));
1710 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1711 struct btrfs_free_space *info,
1712 u64 offset, u64 bytes)
1714 unsigned long start, count;
1716 start = offset_to_bit(info->offset, ctl->unit, offset);
1717 count = bytes_to_bits(bytes, ctl->unit);
1718 ASSERT(start + count <= BITS_PER_BITMAP);
1720 bitmap_clear(info->bitmap, start, count);
1722 info->bytes -= bytes;
1723 if (info->max_extent_size > ctl->unit)
1724 info->max_extent_size = 0;
1727 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1728 struct btrfs_free_space *info, u64 offset,
1731 __bitmap_clear_bits(ctl, info, offset, bytes);
1732 ctl->free_space -= bytes;
1735 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1736 struct btrfs_free_space *info, u64 offset,
1739 unsigned long start, count;
1741 start = offset_to_bit(info->offset, ctl->unit, offset);
1742 count = bytes_to_bits(bytes, ctl->unit);
1743 ASSERT(start + count <= BITS_PER_BITMAP);
1745 bitmap_set(info->bitmap, start, count);
1747 info->bytes += bytes;
1748 ctl->free_space += bytes;
1752 * If we can not find suitable extent, we will use bytes to record
1753 * the size of the max extent.
1755 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1756 struct btrfs_free_space *bitmap_info, u64 *offset,
1757 u64 *bytes, bool for_alloc)
1759 unsigned long found_bits = 0;
1760 unsigned long max_bits = 0;
1761 unsigned long bits, i;
1762 unsigned long next_zero;
1763 unsigned long extent_bits;
1766 * Skip searching the bitmap if we don't have a contiguous section that
1767 * is large enough for this allocation.
1770 bitmap_info->max_extent_size &&
1771 bitmap_info->max_extent_size < *bytes) {
1772 *bytes = bitmap_info->max_extent_size;
1776 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1777 max_t(u64, *offset, bitmap_info->offset));
1778 bits = bytes_to_bits(*bytes, ctl->unit);
1780 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
1781 if (for_alloc && bits == 1) {
1785 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1786 BITS_PER_BITMAP, i);
1787 extent_bits = next_zero - i;
1788 if (extent_bits >= bits) {
1789 found_bits = extent_bits;
1791 } else if (extent_bits > max_bits) {
1792 max_bits = extent_bits;
1798 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1799 *bytes = (u64)(found_bits) * ctl->unit;
1803 *bytes = (u64)(max_bits) * ctl->unit;
1804 bitmap_info->max_extent_size = *bytes;
1808 static inline u64 get_max_extent_size(struct btrfs_free_space *entry)
1811 return entry->max_extent_size;
1812 return entry->bytes;
1815 /* Cache the size of the max extent in bytes */
1816 static struct btrfs_free_space *
1817 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes,
1818 unsigned long align, u64 *max_extent_size)
1820 struct btrfs_free_space *entry;
1821 struct rb_node *node;
1826 if (!ctl->free_space_offset.rb_node)
1829 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1833 for (node = &entry->offset_index; node; node = rb_next(node)) {
1834 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1835 if (entry->bytes < *bytes) {
1836 *max_extent_size = max(get_max_extent_size(entry),
1841 /* make sure the space returned is big enough
1842 * to match our requested alignment
1844 if (*bytes >= align) {
1845 tmp = entry->offset - ctl->start + align - 1;
1846 tmp = div64_u64(tmp, align);
1847 tmp = tmp * align + ctl->start;
1848 align_off = tmp - entry->offset;
1851 tmp = entry->offset;
1854 if (entry->bytes < *bytes + align_off) {
1855 *max_extent_size = max(get_max_extent_size(entry),
1860 if (entry->bitmap) {
1863 ret = search_bitmap(ctl, entry, &tmp, &size, true);
1870 max(get_max_extent_size(entry),
1877 *bytes = entry->bytes - align_off;
1884 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1885 struct btrfs_free_space *info, u64 offset)
1887 info->offset = offset_to_bitmap(ctl, offset);
1889 INIT_LIST_HEAD(&info->list);
1890 link_free_space(ctl, info);
1891 ctl->total_bitmaps++;
1893 ctl->op->recalc_thresholds(ctl);
1896 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1897 struct btrfs_free_space *bitmap_info)
1899 unlink_free_space(ctl, bitmap_info);
1900 kfree(bitmap_info->bitmap);
1901 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1902 ctl->total_bitmaps--;
1903 ctl->op->recalc_thresholds(ctl);
1906 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1907 struct btrfs_free_space *bitmap_info,
1908 u64 *offset, u64 *bytes)
1911 u64 search_start, search_bytes;
1915 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1918 * We need to search for bits in this bitmap. We could only cover some
1919 * of the extent in this bitmap thanks to how we add space, so we need
1920 * to search for as much as it as we can and clear that amount, and then
1921 * go searching for the next bit.
1923 search_start = *offset;
1924 search_bytes = ctl->unit;
1925 search_bytes = min(search_bytes, end - search_start + 1);
1926 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes,
1928 if (ret < 0 || search_start != *offset)
1931 /* We may have found more bits than what we need */
1932 search_bytes = min(search_bytes, *bytes);
1934 /* Cannot clear past the end of the bitmap */
1935 search_bytes = min(search_bytes, end - search_start + 1);
1937 bitmap_clear_bits(ctl, bitmap_info, search_start, search_bytes);
1938 *offset += search_bytes;
1939 *bytes -= search_bytes;
1942 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1943 if (!bitmap_info->bytes)
1944 free_bitmap(ctl, bitmap_info);
1947 * no entry after this bitmap, but we still have bytes to
1948 * remove, so something has gone wrong.
1953 bitmap_info = rb_entry(next, struct btrfs_free_space,
1957 * if the next entry isn't a bitmap we need to return to let the
1958 * extent stuff do its work.
1960 if (!bitmap_info->bitmap)
1964 * Ok the next item is a bitmap, but it may not actually hold
1965 * the information for the rest of this free space stuff, so
1966 * look for it, and if we don't find it return so we can try
1967 * everything over again.
1969 search_start = *offset;
1970 search_bytes = ctl->unit;
1971 ret = search_bitmap(ctl, bitmap_info, &search_start,
1972 &search_bytes, false);
1973 if (ret < 0 || search_start != *offset)
1977 } else if (!bitmap_info->bytes)
1978 free_bitmap(ctl, bitmap_info);
1983 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1984 struct btrfs_free_space *info, u64 offset,
1987 u64 bytes_to_set = 0;
1990 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1992 bytes_to_set = min(end - offset, bytes);
1994 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1997 * We set some bytes, we have no idea what the max extent size is
2000 info->max_extent_size = 0;
2002 return bytes_to_set;
2006 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
2007 struct btrfs_free_space *info)
2009 struct btrfs_block_group_cache *block_group = ctl->private;
2010 struct btrfs_fs_info *fs_info = block_group->fs_info;
2011 bool forced = false;
2013 #ifdef CONFIG_BTRFS_DEBUG
2014 if (btrfs_should_fragment_free_space(block_group))
2019 * If we are below the extents threshold then we can add this as an
2020 * extent, and don't have to deal with the bitmap
2022 if (!forced && ctl->free_extents < ctl->extents_thresh) {
2024 * If this block group has some small extents we don't want to
2025 * use up all of our free slots in the cache with them, we want
2026 * to reserve them to larger extents, however if we have plenty
2027 * of cache left then go ahead an dadd them, no sense in adding
2028 * the overhead of a bitmap if we don't have to.
2030 if (info->bytes <= fs_info->sectorsize * 4) {
2031 if (ctl->free_extents * 2 <= ctl->extents_thresh)
2039 * The original block groups from mkfs can be really small, like 8
2040 * megabytes, so don't bother with a bitmap for those entries. However
2041 * some block groups can be smaller than what a bitmap would cover but
2042 * are still large enough that they could overflow the 32k memory limit,
2043 * so allow those block groups to still be allowed to have a bitmap
2046 if (((BITS_PER_BITMAP * ctl->unit) >> 1) > block_group->key.offset)
2052 static const struct btrfs_free_space_op free_space_op = {
2053 .recalc_thresholds = recalculate_thresholds,
2054 .use_bitmap = use_bitmap,
2057 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
2058 struct btrfs_free_space *info)
2060 struct btrfs_free_space *bitmap_info;
2061 struct btrfs_block_group_cache *block_group = NULL;
2063 u64 bytes, offset, bytes_added;
2066 bytes = info->bytes;
2067 offset = info->offset;
2069 if (!ctl->op->use_bitmap(ctl, info))
2072 if (ctl->op == &free_space_op)
2073 block_group = ctl->private;
2076 * Since we link bitmaps right into the cluster we need to see if we
2077 * have a cluster here, and if so and it has our bitmap we need to add
2078 * the free space to that bitmap.
2080 if (block_group && !list_empty(&block_group->cluster_list)) {
2081 struct btrfs_free_cluster *cluster;
2082 struct rb_node *node;
2083 struct btrfs_free_space *entry;
2085 cluster = list_entry(block_group->cluster_list.next,
2086 struct btrfs_free_cluster,
2088 spin_lock(&cluster->lock);
2089 node = rb_first(&cluster->root);
2091 spin_unlock(&cluster->lock);
2092 goto no_cluster_bitmap;
2095 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2096 if (!entry->bitmap) {
2097 spin_unlock(&cluster->lock);
2098 goto no_cluster_bitmap;
2101 if (entry->offset == offset_to_bitmap(ctl, offset)) {
2102 bytes_added = add_bytes_to_bitmap(ctl, entry,
2104 bytes -= bytes_added;
2105 offset += bytes_added;
2107 spin_unlock(&cluster->lock);
2115 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2122 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
2123 bytes -= bytes_added;
2124 offset += bytes_added;
2134 if (info && info->bitmap) {
2135 add_new_bitmap(ctl, info, offset);
2140 spin_unlock(&ctl->tree_lock);
2142 /* no pre-allocated info, allocate a new one */
2144 info = kmem_cache_zalloc(btrfs_free_space_cachep,
2147 spin_lock(&ctl->tree_lock);
2153 /* allocate the bitmap */
2154 info->bitmap = kzalloc(PAGE_SIZE, GFP_NOFS);
2155 spin_lock(&ctl->tree_lock);
2156 if (!info->bitmap) {
2166 kfree(info->bitmap);
2167 kmem_cache_free(btrfs_free_space_cachep, info);
2173 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
2174 struct btrfs_free_space *info, bool update_stat)
2176 struct btrfs_free_space *left_info = NULL;
2177 struct btrfs_free_space *right_info;
2178 bool merged = false;
2179 u64 offset = info->offset;
2180 u64 bytes = info->bytes;
2183 * first we want to see if there is free space adjacent to the range we
2184 * are adding, if there is remove that struct and add a new one to
2185 * cover the entire range
2187 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
2188 if (right_info && rb_prev(&right_info->offset_index))
2189 left_info = rb_entry(rb_prev(&right_info->offset_index),
2190 struct btrfs_free_space, offset_index);
2191 else if (!right_info)
2192 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
2194 if (right_info && !right_info->bitmap) {
2196 unlink_free_space(ctl, right_info);
2198 __unlink_free_space(ctl, right_info);
2199 info->bytes += right_info->bytes;
2200 kmem_cache_free(btrfs_free_space_cachep, right_info);
2204 if (left_info && !left_info->bitmap &&
2205 left_info->offset + left_info->bytes == offset) {
2207 unlink_free_space(ctl, left_info);
2209 __unlink_free_space(ctl, left_info);
2210 info->offset = left_info->offset;
2211 info->bytes += left_info->bytes;
2212 kmem_cache_free(btrfs_free_space_cachep, left_info);
2219 static bool steal_from_bitmap_to_end(struct btrfs_free_space_ctl *ctl,
2220 struct btrfs_free_space *info,
2223 struct btrfs_free_space *bitmap;
2226 const u64 end = info->offset + info->bytes;
2227 const u64 bitmap_offset = offset_to_bitmap(ctl, end);
2230 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2234 i = offset_to_bit(bitmap->offset, ctl->unit, end);
2235 j = find_next_zero_bit(bitmap->bitmap, BITS_PER_BITMAP, i);
2238 bytes = (j - i) * ctl->unit;
2239 info->bytes += bytes;
2242 bitmap_clear_bits(ctl, bitmap, end, bytes);
2244 __bitmap_clear_bits(ctl, bitmap, end, bytes);
2247 free_bitmap(ctl, bitmap);
2252 static bool steal_from_bitmap_to_front(struct btrfs_free_space_ctl *ctl,
2253 struct btrfs_free_space *info,
2256 struct btrfs_free_space *bitmap;
2260 unsigned long prev_j;
2263 bitmap_offset = offset_to_bitmap(ctl, info->offset);
2264 /* If we're on a boundary, try the previous logical bitmap. */
2265 if (bitmap_offset == info->offset) {
2266 if (info->offset == 0)
2268 bitmap_offset = offset_to_bitmap(ctl, info->offset - 1);
2271 bitmap = tree_search_offset(ctl, bitmap_offset, 1, 0);
2275 i = offset_to_bit(bitmap->offset, ctl->unit, info->offset) - 1;
2277 prev_j = (unsigned long)-1;
2278 for_each_clear_bit_from(j, bitmap->bitmap, BITS_PER_BITMAP) {
2286 if (prev_j == (unsigned long)-1)
2287 bytes = (i + 1) * ctl->unit;
2289 bytes = (i - prev_j) * ctl->unit;
2291 info->offset -= bytes;
2292 info->bytes += bytes;
2295 bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2297 __bitmap_clear_bits(ctl, bitmap, info->offset, bytes);
2300 free_bitmap(ctl, bitmap);
2306 * We prefer always to allocate from extent entries, both for clustered and
2307 * non-clustered allocation requests. So when attempting to add a new extent
2308 * entry, try to see if there's adjacent free space in bitmap entries, and if
2309 * there is, migrate that space from the bitmaps to the extent.
2310 * Like this we get better chances of satisfying space allocation requests
2311 * because we attempt to satisfy them based on a single cache entry, and never
2312 * on 2 or more entries - even if the entries represent a contiguous free space
2313 * region (e.g. 1 extent entry + 1 bitmap entry starting where the extent entry
2316 static void steal_from_bitmap(struct btrfs_free_space_ctl *ctl,
2317 struct btrfs_free_space *info,
2321 * Only work with disconnected entries, as we can change their offset,
2322 * and must be extent entries.
2324 ASSERT(!info->bitmap);
2325 ASSERT(RB_EMPTY_NODE(&info->offset_index));
2327 if (ctl->total_bitmaps > 0) {
2329 bool stole_front = false;
2331 stole_end = steal_from_bitmap_to_end(ctl, info, update_stat);
2332 if (ctl->total_bitmaps > 0)
2333 stole_front = steal_from_bitmap_to_front(ctl, info,
2336 if (stole_end || stole_front)
2337 try_merge_free_space(ctl, info, update_stat);
2341 int __btrfs_add_free_space(struct btrfs_fs_info *fs_info,
2342 struct btrfs_free_space_ctl *ctl,
2343 u64 offset, u64 bytes)
2345 struct btrfs_free_space *info;
2348 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
2352 info->offset = offset;
2353 info->bytes = bytes;
2354 RB_CLEAR_NODE(&info->offset_index);
2356 spin_lock(&ctl->tree_lock);
2358 if (try_merge_free_space(ctl, info, true))
2362 * There was no extent directly to the left or right of this new
2363 * extent then we know we're going to have to allocate a new extent, so
2364 * before we do that see if we need to drop this into a bitmap
2366 ret = insert_into_bitmap(ctl, info);
2375 * Only steal free space from adjacent bitmaps if we're sure we're not
2376 * going to add the new free space to existing bitmap entries - because
2377 * that would mean unnecessary work that would be reverted. Therefore
2378 * attempt to steal space from bitmaps if we're adding an extent entry.
2380 steal_from_bitmap(ctl, info, true);
2382 ret = link_free_space(ctl, info);
2384 kmem_cache_free(btrfs_free_space_cachep, info);
2386 spin_unlock(&ctl->tree_lock);
2389 btrfs_crit(fs_info, "unable to add free space :%d", ret);
2390 ASSERT(ret != -EEXIST);
2396 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
2397 u64 offset, u64 bytes)
2399 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2400 struct btrfs_free_space *info;
2402 bool re_search = false;
2404 spin_lock(&ctl->tree_lock);
2411 info = tree_search_offset(ctl, offset, 0, 0);
2414 * oops didn't find an extent that matched the space we wanted
2415 * to remove, look for a bitmap instead
2417 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
2421 * If we found a partial bit of our free space in a
2422 * bitmap but then couldn't find the other part this may
2423 * be a problem, so WARN about it.
2431 if (!info->bitmap) {
2432 unlink_free_space(ctl, info);
2433 if (offset == info->offset) {
2434 u64 to_free = min(bytes, info->bytes);
2436 info->bytes -= to_free;
2437 info->offset += to_free;
2439 ret = link_free_space(ctl, info);
2442 kmem_cache_free(btrfs_free_space_cachep, info);
2449 u64 old_end = info->bytes + info->offset;
2451 info->bytes = offset - info->offset;
2452 ret = link_free_space(ctl, info);
2457 /* Not enough bytes in this entry to satisfy us */
2458 if (old_end < offset + bytes) {
2459 bytes -= old_end - offset;
2462 } else if (old_end == offset + bytes) {
2466 spin_unlock(&ctl->tree_lock);
2468 ret = btrfs_add_free_space(block_group, offset + bytes,
2469 old_end - (offset + bytes));
2475 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
2476 if (ret == -EAGAIN) {
2481 spin_unlock(&ctl->tree_lock);
2486 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2489 struct btrfs_fs_info *fs_info = block_group->fs_info;
2490 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2491 struct btrfs_free_space *info;
2495 spin_lock(&ctl->tree_lock);
2496 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
2497 info = rb_entry(n, struct btrfs_free_space, offset_index);
2498 if (info->bytes >= bytes && !block_group->ro)
2500 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2501 info->offset, info->bytes,
2502 (info->bitmap) ? "yes" : "no");
2504 spin_unlock(&ctl->tree_lock);
2505 btrfs_info(fs_info, "block group has cluster?: %s",
2506 list_empty(&block_group->cluster_list) ? "no" : "yes");
2508 "%d blocks of free space at or bigger than bytes is", count);
2511 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
2513 struct btrfs_fs_info *fs_info = block_group->fs_info;
2514 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2516 spin_lock_init(&ctl->tree_lock);
2517 ctl->unit = fs_info->sectorsize;
2518 ctl->start = block_group->key.objectid;
2519 ctl->private = block_group;
2520 ctl->op = &free_space_op;
2521 INIT_LIST_HEAD(&ctl->trimming_ranges);
2522 mutex_init(&ctl->cache_writeout_mutex);
2525 * we only want to have 32k of ram per block group for keeping
2526 * track of free space, and if we pass 1/2 of that we want to
2527 * start converting things over to using bitmaps
2529 ctl->extents_thresh = (SZ_32K / 2) / sizeof(struct btrfs_free_space);
2533 * for a given cluster, put all of its extents back into the free
2534 * space cache. If the block group passed doesn't match the block group
2535 * pointed to by the cluster, someone else raced in and freed the
2536 * cluster already. In that case, we just return without changing anything
2539 __btrfs_return_cluster_to_free_space(
2540 struct btrfs_block_group_cache *block_group,
2541 struct btrfs_free_cluster *cluster)
2543 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2544 struct btrfs_free_space *entry;
2545 struct rb_node *node;
2547 spin_lock(&cluster->lock);
2548 if (cluster->block_group != block_group)
2551 cluster->block_group = NULL;
2552 cluster->window_start = 0;
2553 list_del_init(&cluster->block_group_list);
2555 node = rb_first(&cluster->root);
2559 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2560 node = rb_next(&entry->offset_index);
2561 rb_erase(&entry->offset_index, &cluster->root);
2562 RB_CLEAR_NODE(&entry->offset_index);
2564 bitmap = (entry->bitmap != NULL);
2566 try_merge_free_space(ctl, entry, false);
2567 steal_from_bitmap(ctl, entry, false);
2569 tree_insert_offset(&ctl->free_space_offset,
2570 entry->offset, &entry->offset_index, bitmap);
2572 cluster->root = RB_ROOT;
2575 spin_unlock(&cluster->lock);
2576 btrfs_put_block_group(block_group);
2580 static void __btrfs_remove_free_space_cache_locked(
2581 struct btrfs_free_space_ctl *ctl)
2583 struct btrfs_free_space *info;
2584 struct rb_node *node;
2586 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2587 info = rb_entry(node, struct btrfs_free_space, offset_index);
2588 if (!info->bitmap) {
2589 unlink_free_space(ctl, info);
2590 kmem_cache_free(btrfs_free_space_cachep, info);
2592 free_bitmap(ctl, info);
2595 cond_resched_lock(&ctl->tree_lock);
2599 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2601 spin_lock(&ctl->tree_lock);
2602 __btrfs_remove_free_space_cache_locked(ctl);
2603 spin_unlock(&ctl->tree_lock);
2606 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2608 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2609 struct btrfs_free_cluster *cluster;
2610 struct list_head *head;
2612 spin_lock(&ctl->tree_lock);
2613 while ((head = block_group->cluster_list.next) !=
2614 &block_group->cluster_list) {
2615 cluster = list_entry(head, struct btrfs_free_cluster,
2618 WARN_ON(cluster->block_group != block_group);
2619 __btrfs_return_cluster_to_free_space(block_group, cluster);
2621 cond_resched_lock(&ctl->tree_lock);
2623 __btrfs_remove_free_space_cache_locked(ctl);
2624 spin_unlock(&ctl->tree_lock);
2628 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2629 u64 offset, u64 bytes, u64 empty_size,
2630 u64 *max_extent_size)
2632 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2633 struct btrfs_free_space *entry = NULL;
2634 u64 bytes_search = bytes + empty_size;
2637 u64 align_gap_len = 0;
2639 spin_lock(&ctl->tree_lock);
2640 entry = find_free_space(ctl, &offset, &bytes_search,
2641 block_group->full_stripe_len, max_extent_size);
2646 if (entry->bitmap) {
2647 bitmap_clear_bits(ctl, entry, offset, bytes);
2649 free_bitmap(ctl, entry);
2651 unlink_free_space(ctl, entry);
2652 align_gap_len = offset - entry->offset;
2653 align_gap = entry->offset;
2655 entry->offset = offset + bytes;
2656 WARN_ON(entry->bytes < bytes + align_gap_len);
2658 entry->bytes -= bytes + align_gap_len;
2660 kmem_cache_free(btrfs_free_space_cachep, entry);
2662 link_free_space(ctl, entry);
2665 spin_unlock(&ctl->tree_lock);
2668 __btrfs_add_free_space(block_group->fs_info, ctl,
2669 align_gap, align_gap_len);
2674 * given a cluster, put all of its extents back into the free space
2675 * cache. If a block group is passed, this function will only free
2676 * a cluster that belongs to the passed block group.
2678 * Otherwise, it'll get a reference on the block group pointed to by the
2679 * cluster and remove the cluster from it.
2681 int btrfs_return_cluster_to_free_space(
2682 struct btrfs_block_group_cache *block_group,
2683 struct btrfs_free_cluster *cluster)
2685 struct btrfs_free_space_ctl *ctl;
2688 /* first, get a safe pointer to the block group */
2689 spin_lock(&cluster->lock);
2691 block_group = cluster->block_group;
2693 spin_unlock(&cluster->lock);
2696 } else if (cluster->block_group != block_group) {
2697 /* someone else has already freed it don't redo their work */
2698 spin_unlock(&cluster->lock);
2701 atomic_inc(&block_group->count);
2702 spin_unlock(&cluster->lock);
2704 ctl = block_group->free_space_ctl;
2706 /* now return any extents the cluster had on it */
2707 spin_lock(&ctl->tree_lock);
2708 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2709 spin_unlock(&ctl->tree_lock);
2711 /* finally drop our ref */
2712 btrfs_put_block_group(block_group);
2716 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2717 struct btrfs_free_cluster *cluster,
2718 struct btrfs_free_space *entry,
2719 u64 bytes, u64 min_start,
2720 u64 *max_extent_size)
2722 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2724 u64 search_start = cluster->window_start;
2725 u64 search_bytes = bytes;
2728 search_start = min_start;
2729 search_bytes = bytes;
2731 err = search_bitmap(ctl, entry, &search_start, &search_bytes, true);
2733 *max_extent_size = max(get_max_extent_size(entry),
2739 __bitmap_clear_bits(ctl, entry, ret, bytes);
2745 * given a cluster, try to allocate 'bytes' from it, returns 0
2746 * if it couldn't find anything suitably large, or a logical disk offset
2747 * if things worked out
2749 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2750 struct btrfs_free_cluster *cluster, u64 bytes,
2751 u64 min_start, u64 *max_extent_size)
2753 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2754 struct btrfs_free_space *entry = NULL;
2755 struct rb_node *node;
2758 spin_lock(&cluster->lock);
2759 if (bytes > cluster->max_size)
2762 if (cluster->block_group != block_group)
2765 node = rb_first(&cluster->root);
2769 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2771 if (entry->bytes < bytes)
2772 *max_extent_size = max(get_max_extent_size(entry),
2775 if (entry->bytes < bytes ||
2776 (!entry->bitmap && entry->offset < min_start)) {
2777 node = rb_next(&entry->offset_index);
2780 entry = rb_entry(node, struct btrfs_free_space,
2785 if (entry->bitmap) {
2786 ret = btrfs_alloc_from_bitmap(block_group,
2787 cluster, entry, bytes,
2788 cluster->window_start,
2791 node = rb_next(&entry->offset_index);
2794 entry = rb_entry(node, struct btrfs_free_space,
2798 cluster->window_start += bytes;
2800 ret = entry->offset;
2802 entry->offset += bytes;
2803 entry->bytes -= bytes;
2806 if (entry->bytes == 0)
2807 rb_erase(&entry->offset_index, &cluster->root);
2811 spin_unlock(&cluster->lock);
2816 spin_lock(&ctl->tree_lock);
2818 ctl->free_space -= bytes;
2819 if (entry->bytes == 0) {
2820 ctl->free_extents--;
2821 if (entry->bitmap) {
2822 kfree(entry->bitmap);
2823 ctl->total_bitmaps--;
2824 ctl->op->recalc_thresholds(ctl);
2826 kmem_cache_free(btrfs_free_space_cachep, entry);
2829 spin_unlock(&ctl->tree_lock);
2834 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2835 struct btrfs_free_space *entry,
2836 struct btrfs_free_cluster *cluster,
2837 u64 offset, u64 bytes,
2838 u64 cont1_bytes, u64 min_bytes)
2840 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2841 unsigned long next_zero;
2843 unsigned long want_bits;
2844 unsigned long min_bits;
2845 unsigned long found_bits;
2846 unsigned long max_bits = 0;
2847 unsigned long start = 0;
2848 unsigned long total_found = 0;
2851 i = offset_to_bit(entry->offset, ctl->unit,
2852 max_t(u64, offset, entry->offset));
2853 want_bits = bytes_to_bits(bytes, ctl->unit);
2854 min_bits = bytes_to_bits(min_bytes, ctl->unit);
2857 * Don't bother looking for a cluster in this bitmap if it's heavily
2860 if (entry->max_extent_size &&
2861 entry->max_extent_size < cont1_bytes)
2865 for_each_set_bit_from(i, entry->bitmap, BITS_PER_BITMAP) {
2866 next_zero = find_next_zero_bit(entry->bitmap,
2867 BITS_PER_BITMAP, i);
2868 if (next_zero - i >= min_bits) {
2869 found_bits = next_zero - i;
2870 if (found_bits > max_bits)
2871 max_bits = found_bits;
2874 if (next_zero - i > max_bits)
2875 max_bits = next_zero - i;
2880 entry->max_extent_size = (u64)max_bits * ctl->unit;
2886 cluster->max_size = 0;
2889 total_found += found_bits;
2891 if (cluster->max_size < found_bits * ctl->unit)
2892 cluster->max_size = found_bits * ctl->unit;
2894 if (total_found < want_bits || cluster->max_size < cont1_bytes) {
2899 cluster->window_start = start * ctl->unit + entry->offset;
2900 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2901 ret = tree_insert_offset(&cluster->root, entry->offset,
2902 &entry->offset_index, 1);
2903 ASSERT(!ret); /* -EEXIST; Logic error */
2905 trace_btrfs_setup_cluster(block_group, cluster,
2906 total_found * ctl->unit, 1);
2911 * This searches the block group for just extents to fill the cluster with.
2912 * Try to find a cluster with at least bytes total bytes, at least one
2913 * extent of cont1_bytes, and other clusters of at least min_bytes.
2916 setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2917 struct btrfs_free_cluster *cluster,
2918 struct list_head *bitmaps, u64 offset, u64 bytes,
2919 u64 cont1_bytes, u64 min_bytes)
2921 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2922 struct btrfs_free_space *first = NULL;
2923 struct btrfs_free_space *entry = NULL;
2924 struct btrfs_free_space *last;
2925 struct rb_node *node;
2930 entry = tree_search_offset(ctl, offset, 0, 1);
2935 * We don't want bitmaps, so just move along until we find a normal
2938 while (entry->bitmap || entry->bytes < min_bytes) {
2939 if (entry->bitmap && list_empty(&entry->list))
2940 list_add_tail(&entry->list, bitmaps);
2941 node = rb_next(&entry->offset_index);
2944 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2947 window_free = entry->bytes;
2948 max_extent = entry->bytes;
2952 for (node = rb_next(&entry->offset_index); node;
2953 node = rb_next(&entry->offset_index)) {
2954 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2956 if (entry->bitmap) {
2957 if (list_empty(&entry->list))
2958 list_add_tail(&entry->list, bitmaps);
2962 if (entry->bytes < min_bytes)
2966 window_free += entry->bytes;
2967 if (entry->bytes > max_extent)
2968 max_extent = entry->bytes;
2971 if (window_free < bytes || max_extent < cont1_bytes)
2974 cluster->window_start = first->offset;
2976 node = &first->offset_index;
2979 * now we've found our entries, pull them out of the free space
2980 * cache and put them into the cluster rbtree
2985 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2986 node = rb_next(&entry->offset_index);
2987 if (entry->bitmap || entry->bytes < min_bytes)
2990 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2991 ret = tree_insert_offset(&cluster->root, entry->offset,
2992 &entry->offset_index, 0);
2993 total_size += entry->bytes;
2994 ASSERT(!ret); /* -EEXIST; Logic error */
2995 } while (node && entry != last);
2997 cluster->max_size = max_extent;
2998 trace_btrfs_setup_cluster(block_group, cluster, total_size, 0);
3003 * This specifically looks for bitmaps that may work in the cluster, we assume
3004 * that we have already failed to find extents that will work.
3007 setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
3008 struct btrfs_free_cluster *cluster,
3009 struct list_head *bitmaps, u64 offset, u64 bytes,
3010 u64 cont1_bytes, u64 min_bytes)
3012 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3013 struct btrfs_free_space *entry = NULL;
3015 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
3017 if (ctl->total_bitmaps == 0)
3021 * The bitmap that covers offset won't be in the list unless offset
3022 * is just its start offset.
3024 if (!list_empty(bitmaps))
3025 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
3027 if (!entry || entry->offset != bitmap_offset) {
3028 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
3029 if (entry && list_empty(&entry->list))
3030 list_add(&entry->list, bitmaps);
3033 list_for_each_entry(entry, bitmaps, list) {
3034 if (entry->bytes < bytes)
3036 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
3037 bytes, cont1_bytes, min_bytes);
3043 * The bitmaps list has all the bitmaps that record free space
3044 * starting after offset, so no more search is required.
3050 * here we try to find a cluster of blocks in a block group. The goal
3051 * is to find at least bytes+empty_size.
3052 * We might not find them all in one contiguous area.
3054 * returns zero and sets up cluster if things worked out, otherwise
3055 * it returns -enospc
3057 int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
3058 struct btrfs_block_group_cache *block_group,
3059 struct btrfs_free_cluster *cluster,
3060 u64 offset, u64 bytes, u64 empty_size)
3062 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3063 struct btrfs_free_space *entry, *tmp;
3070 * Choose the minimum extent size we'll require for this
3071 * cluster. For SSD_SPREAD, don't allow any fragmentation.
3072 * For metadata, allow allocates with smaller extents. For
3073 * data, keep it dense.
3075 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3076 cont1_bytes = min_bytes = bytes + empty_size;
3077 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3078 cont1_bytes = bytes;
3079 min_bytes = fs_info->sectorsize;
3081 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3082 min_bytes = fs_info->sectorsize;
3085 spin_lock(&ctl->tree_lock);
3088 * If we know we don't have enough space to make a cluster don't even
3089 * bother doing all the work to try and find one.
3091 if (ctl->free_space < bytes) {
3092 spin_unlock(&ctl->tree_lock);
3096 spin_lock(&cluster->lock);
3098 /* someone already found a cluster, hooray */
3099 if (cluster->block_group) {
3104 trace_btrfs_find_cluster(block_group, offset, bytes, empty_size,
3107 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
3109 cont1_bytes, min_bytes);
3111 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
3112 offset, bytes + empty_size,
3113 cont1_bytes, min_bytes);
3115 /* Clear our temporary list */
3116 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
3117 list_del_init(&entry->list);
3120 atomic_inc(&block_group->count);
3121 list_add_tail(&cluster->block_group_list,
3122 &block_group->cluster_list);
3123 cluster->block_group = block_group;
3125 trace_btrfs_failed_cluster_setup(block_group);
3128 spin_unlock(&cluster->lock);
3129 spin_unlock(&ctl->tree_lock);
3135 * simple code to zero out a cluster
3137 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
3139 spin_lock_init(&cluster->lock);
3140 spin_lock_init(&cluster->refill_lock);
3141 cluster->root = RB_ROOT;
3142 cluster->max_size = 0;
3143 cluster->fragmented = false;
3144 INIT_LIST_HEAD(&cluster->block_group_list);
3145 cluster->block_group = NULL;
3148 static int do_trimming(struct btrfs_block_group_cache *block_group,
3149 u64 *total_trimmed, u64 start, u64 bytes,
3150 u64 reserved_start, u64 reserved_bytes,
3151 struct btrfs_trim_range *trim_entry)
3153 struct btrfs_space_info *space_info = block_group->space_info;
3154 struct btrfs_fs_info *fs_info = block_group->fs_info;
3155 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3160 spin_lock(&space_info->lock);
3161 spin_lock(&block_group->lock);
3162 if (!block_group->ro) {
3163 block_group->reserved += reserved_bytes;
3164 space_info->bytes_reserved += reserved_bytes;
3167 spin_unlock(&block_group->lock);
3168 spin_unlock(&space_info->lock);
3170 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3172 *total_trimmed += trimmed;
3174 mutex_lock(&ctl->cache_writeout_mutex);
3175 btrfs_add_free_space(block_group, reserved_start, reserved_bytes);
3176 list_del(&trim_entry->list);
3177 mutex_unlock(&ctl->cache_writeout_mutex);
3180 spin_lock(&space_info->lock);
3181 spin_lock(&block_group->lock);
3182 if (block_group->ro)
3183 space_info->bytes_readonly += reserved_bytes;
3184 block_group->reserved -= reserved_bytes;
3185 space_info->bytes_reserved -= reserved_bytes;
3186 spin_unlock(&space_info->lock);
3187 spin_unlock(&block_group->lock);
3193 static int trim_no_bitmap(struct btrfs_block_group_cache *block_group,
3194 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3196 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3197 struct btrfs_free_space *entry;
3198 struct rb_node *node;
3204 while (start < end) {
3205 struct btrfs_trim_range trim_entry;
3207 mutex_lock(&ctl->cache_writeout_mutex);
3208 spin_lock(&ctl->tree_lock);
3210 if (ctl->free_space < minlen) {
3211 spin_unlock(&ctl->tree_lock);
3212 mutex_unlock(&ctl->cache_writeout_mutex);
3216 entry = tree_search_offset(ctl, start, 0, 1);
3218 spin_unlock(&ctl->tree_lock);
3219 mutex_unlock(&ctl->cache_writeout_mutex);
3224 while (entry->bitmap) {
3225 node = rb_next(&entry->offset_index);
3227 spin_unlock(&ctl->tree_lock);
3228 mutex_unlock(&ctl->cache_writeout_mutex);
3231 entry = rb_entry(node, struct btrfs_free_space,
3235 if (entry->offset >= end) {
3236 spin_unlock(&ctl->tree_lock);
3237 mutex_unlock(&ctl->cache_writeout_mutex);
3241 extent_start = entry->offset;
3242 extent_bytes = entry->bytes;
3243 start = max(start, extent_start);
3244 bytes = min(extent_start + extent_bytes, end) - start;
3245 if (bytes < minlen) {
3246 spin_unlock(&ctl->tree_lock);
3247 mutex_unlock(&ctl->cache_writeout_mutex);
3251 unlink_free_space(ctl, entry);
3252 kmem_cache_free(btrfs_free_space_cachep, entry);
3254 spin_unlock(&ctl->tree_lock);
3255 trim_entry.start = extent_start;
3256 trim_entry.bytes = extent_bytes;
3257 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3258 mutex_unlock(&ctl->cache_writeout_mutex);
3260 ret = do_trimming(block_group, total_trimmed, start, bytes,
3261 extent_start, extent_bytes, &trim_entry);
3267 if (fatal_signal_pending(current)) {
3278 static int trim_bitmaps(struct btrfs_block_group_cache *block_group,
3279 u64 *total_trimmed, u64 start, u64 end, u64 minlen)
3281 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3282 struct btrfs_free_space *entry;
3286 u64 offset = offset_to_bitmap(ctl, start);
3288 while (offset < end) {
3289 bool next_bitmap = false;
3290 struct btrfs_trim_range trim_entry;
3292 mutex_lock(&ctl->cache_writeout_mutex);
3293 spin_lock(&ctl->tree_lock);
3295 if (ctl->free_space < minlen) {
3296 spin_unlock(&ctl->tree_lock);
3297 mutex_unlock(&ctl->cache_writeout_mutex);
3301 entry = tree_search_offset(ctl, offset, 1, 0);
3303 spin_unlock(&ctl->tree_lock);
3304 mutex_unlock(&ctl->cache_writeout_mutex);
3310 ret2 = search_bitmap(ctl, entry, &start, &bytes, false);
3311 if (ret2 || start >= end) {
3312 spin_unlock(&ctl->tree_lock);
3313 mutex_unlock(&ctl->cache_writeout_mutex);
3318 bytes = min(bytes, end - start);
3319 if (bytes < minlen) {
3320 spin_unlock(&ctl->tree_lock);
3321 mutex_unlock(&ctl->cache_writeout_mutex);
3325 bitmap_clear_bits(ctl, entry, start, bytes);
3326 if (entry->bytes == 0)
3327 free_bitmap(ctl, entry);
3329 spin_unlock(&ctl->tree_lock);
3330 trim_entry.start = start;
3331 trim_entry.bytes = bytes;
3332 list_add_tail(&trim_entry.list, &ctl->trimming_ranges);
3333 mutex_unlock(&ctl->cache_writeout_mutex);
3335 ret = do_trimming(block_group, total_trimmed, start, bytes,
3336 start, bytes, &trim_entry);
3341 offset += BITS_PER_BITMAP * ctl->unit;
3344 if (start >= offset + BITS_PER_BITMAP * ctl->unit)
3345 offset += BITS_PER_BITMAP * ctl->unit;
3348 if (fatal_signal_pending(current)) {
3359 void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
3361 atomic_inc(&cache->trimming);
3364 void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3366 struct btrfs_fs_info *fs_info = block_group->fs_info;
3367 struct extent_map_tree *em_tree;
3368 struct extent_map *em;
3371 spin_lock(&block_group->lock);
3372 cleanup = (atomic_dec_and_test(&block_group->trimming) &&
3373 block_group->removed);
3374 spin_unlock(&block_group->lock);
3377 mutex_lock(&fs_info->chunk_mutex);
3378 em_tree = &fs_info->mapping_tree.map_tree;
3379 write_lock(&em_tree->lock);
3380 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3382 BUG_ON(!em); /* logic error, can't happen */
3384 * remove_extent_mapping() will delete us from the pinned_chunks
3385 * list, which is protected by the chunk mutex.
3387 remove_extent_mapping(em_tree, em);
3388 write_unlock(&em_tree->lock);
3389 mutex_unlock(&fs_info->chunk_mutex);
3391 /* once for us and once for the tree */
3392 free_extent_map(em);
3393 free_extent_map(em);
3396 * We've left one free space entry and other tasks trimming
3397 * this block group have left 1 entry each one. Free them.
3399 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
3403 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
3404 u64 *trimmed, u64 start, u64 end, u64 minlen)
3410 spin_lock(&block_group->lock);
3411 if (block_group->removed) {
3412 spin_unlock(&block_group->lock);
3415 btrfs_get_block_group_trimming(block_group);
3416 spin_unlock(&block_group->lock);
3418 ret = trim_no_bitmap(block_group, trimmed, start, end, minlen);
3422 ret = trim_bitmaps(block_group, trimmed, start, end, minlen);
3424 btrfs_put_block_group_trimming(block_group);
3429 * Find the left-most item in the cache tree, and then return the
3430 * smallest inode number in the item.
3432 * Note: the returned inode number may not be the smallest one in
3433 * the tree, if the left-most item is a bitmap.
3435 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
3437 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
3438 struct btrfs_free_space *entry = NULL;
3441 spin_lock(&ctl->tree_lock);
3443 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
3446 entry = rb_entry(rb_first(&ctl->free_space_offset),
3447 struct btrfs_free_space, offset_index);
3449 if (!entry->bitmap) {
3450 ino = entry->offset;
3452 unlink_free_space(ctl, entry);
3456 kmem_cache_free(btrfs_free_space_cachep, entry);
3458 link_free_space(ctl, entry);
3464 ret = search_bitmap(ctl, entry, &offset, &count, true);
3465 /* Logic error; Should be empty if it can't find anything */
3469 bitmap_clear_bits(ctl, entry, offset, 1);
3470 if (entry->bytes == 0)
3471 free_bitmap(ctl, entry);
3474 spin_unlock(&ctl->tree_lock);
3479 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
3480 struct btrfs_path *path)
3482 struct inode *inode = NULL;
3484 spin_lock(&root->ino_cache_lock);
3485 if (root->ino_cache_inode)
3486 inode = igrab(root->ino_cache_inode);
3487 spin_unlock(&root->ino_cache_lock);
3491 inode = __lookup_free_space_inode(root, path, 0);
3495 spin_lock(&root->ino_cache_lock);
3496 if (!btrfs_fs_closing(root->fs_info))
3497 root->ino_cache_inode = igrab(inode);
3498 spin_unlock(&root->ino_cache_lock);
3503 int create_free_ino_inode(struct btrfs_root *root,
3504 struct btrfs_trans_handle *trans,
3505 struct btrfs_path *path)
3507 return __create_free_space_inode(root, trans, path,
3508 BTRFS_FREE_INO_OBJECTID, 0);
3511 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3513 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3514 struct btrfs_path *path;
3515 struct inode *inode;
3517 u64 root_gen = btrfs_root_generation(&root->root_item);
3519 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3523 * If we're unmounting then just return, since this does a search on the
3524 * normal root and not the commit root and we could deadlock.
3526 if (btrfs_fs_closing(fs_info))
3529 path = btrfs_alloc_path();
3533 inode = lookup_free_ino_inode(root, path);
3537 if (root_gen != BTRFS_I(inode)->generation)
3540 ret = __load_free_space_cache(root, inode, ctl, path, 0);
3544 "failed to load free ino cache for root %llu",
3545 root->root_key.objectid);
3549 btrfs_free_path(path);
3553 int btrfs_write_out_ino_cache(struct btrfs_root *root,
3554 struct btrfs_trans_handle *trans,
3555 struct btrfs_path *path,
3556 struct inode *inode)
3558 struct btrfs_fs_info *fs_info = root->fs_info;
3559 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3561 struct btrfs_io_ctl io_ctl;
3562 bool release_metadata = true;
3564 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3567 memset(&io_ctl, 0, sizeof(io_ctl));
3568 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, &io_ctl, trans);
3571 * At this point writepages() didn't error out, so our metadata
3572 * reservation is released when the writeback finishes, at
3573 * inode.c:btrfs_finish_ordered_io(), regardless of it finishing
3574 * with or without an error.
3576 release_metadata = false;
3577 ret = btrfs_wait_cache_io_root(root, trans, &io_ctl, path);
3581 if (release_metadata)
3582 btrfs_delalloc_release_metadata(BTRFS_I(inode),
3586 "failed to write free ino cache for root %llu",
3587 root->root_key.objectid);
3594 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3596 * Use this if you need to make a bitmap or extent entry specifically, it
3597 * doesn't do any of the merging that add_free_space does, this acts a lot like
3598 * how the free space cache loading stuff works, so you can get really weird
3601 int test_add_free_space_entry(struct btrfs_block_group_cache *cache,
3602 u64 offset, u64 bytes, bool bitmap)
3604 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3605 struct btrfs_free_space *info = NULL, *bitmap_info;
3612 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
3618 spin_lock(&ctl->tree_lock);
3619 info->offset = offset;
3620 info->bytes = bytes;
3621 info->max_extent_size = 0;
3622 ret = link_free_space(ctl, info);
3623 spin_unlock(&ctl->tree_lock);
3625 kmem_cache_free(btrfs_free_space_cachep, info);
3630 map = kzalloc(PAGE_SIZE, GFP_NOFS);
3632 kmem_cache_free(btrfs_free_space_cachep, info);
3637 spin_lock(&ctl->tree_lock);
3638 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3643 add_new_bitmap(ctl, info, offset);
3648 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
3650 bytes -= bytes_added;
3651 offset += bytes_added;
3652 spin_unlock(&ctl->tree_lock);
3658 kmem_cache_free(btrfs_free_space_cachep, info);
3665 * Checks to see if the given range is in the free space cache. This is really
3666 * just used to check the absence of space, so if there is free space in the
3667 * range at all we will return 1.
3669 int test_check_exists(struct btrfs_block_group_cache *cache,
3670 u64 offset, u64 bytes)
3672 struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
3673 struct btrfs_free_space *info;
3676 spin_lock(&ctl->tree_lock);
3677 info = tree_search_offset(ctl, offset, 0, 0);
3679 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
3687 u64 bit_off, bit_bytes;
3689 struct btrfs_free_space *tmp;
3692 bit_bytes = ctl->unit;
3693 ret = search_bitmap(ctl, info, &bit_off, &bit_bytes, false);
3695 if (bit_off == offset) {
3698 } else if (bit_off > offset &&
3699 offset + bytes > bit_off) {
3705 n = rb_prev(&info->offset_index);
3707 tmp = rb_entry(n, struct btrfs_free_space,
3709 if (tmp->offset + tmp->bytes < offset)
3711 if (offset + bytes < tmp->offset) {
3712 n = rb_prev(&tmp->offset_index);
3719 n = rb_next(&info->offset_index);
3721 tmp = rb_entry(n, struct btrfs_free_space,
3723 if (offset + bytes < tmp->offset)
3725 if (tmp->offset + tmp->bytes < offset) {
3726 n = rb_next(&tmp->offset_index);
3737 if (info->offset == offset) {
3742 if (offset > info->offset && offset < info->offset + info->bytes)
3745 spin_unlock(&ctl->tree_lock);
3748 #endif /* CONFIG_BTRFS_FS_RUN_SANITY_TESTS */