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/slab.h>
22 #include <linux/math64.h>
23 #include <linux/ratelimit.h>
25 #include "free-space-cache.h"
26 #include "transaction.h"
28 #include "extent_io.h"
29 #include "inode-map.h"
31 #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
32 #define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
34 static int link_free_space(struct btrfs_free_space_ctl *ctl,
35 struct btrfs_free_space *info);
37 static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
38 struct btrfs_path *path,
42 struct btrfs_key location;
43 struct btrfs_disk_key disk_key;
44 struct btrfs_free_space_header *header;
45 struct extent_buffer *leaf;
46 struct inode *inode = NULL;
49 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
53 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
57 btrfs_release_path(path);
58 return ERR_PTR(-ENOENT);
61 leaf = path->nodes[0];
62 header = btrfs_item_ptr(leaf, path->slots[0],
63 struct btrfs_free_space_header);
64 btrfs_free_space_key(leaf, header, &disk_key);
65 btrfs_disk_key_to_cpu(&location, &disk_key);
66 btrfs_release_path(path);
68 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
70 return ERR_PTR(-ENOENT);
73 if (is_bad_inode(inode)) {
75 return ERR_PTR(-ENOENT);
78 inode->i_mapping->flags &= ~__GFP_FS;
83 struct inode *lookup_free_space_inode(struct btrfs_root *root,
84 struct btrfs_block_group_cache
85 *block_group, struct btrfs_path *path)
87 struct inode *inode = NULL;
88 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
90 spin_lock(&block_group->lock);
91 if (block_group->inode)
92 inode = igrab(block_group->inode);
93 spin_unlock(&block_group->lock);
97 inode = __lookup_free_space_inode(root, path,
98 block_group->key.objectid);
102 spin_lock(&block_group->lock);
103 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
104 printk(KERN_INFO "Old style space inode found, converting.\n");
105 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
106 BTRFS_INODE_NODATACOW;
107 block_group->disk_cache_state = BTRFS_DC_CLEAR;
110 if (!block_group->iref) {
111 block_group->inode = igrab(inode);
112 block_group->iref = 1;
114 spin_unlock(&block_group->lock);
119 int __create_free_space_inode(struct btrfs_root *root,
120 struct btrfs_trans_handle *trans,
121 struct btrfs_path *path, u64 ino, u64 offset)
123 struct btrfs_key key;
124 struct btrfs_disk_key disk_key;
125 struct btrfs_free_space_header *header;
126 struct btrfs_inode_item *inode_item;
127 struct extent_buffer *leaf;
128 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
131 ret = btrfs_insert_empty_inode(trans, root, path, ino);
135 /* We inline crc's for the free disk space cache */
136 if (ino != BTRFS_FREE_INO_OBJECTID)
137 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
139 leaf = path->nodes[0];
140 inode_item = btrfs_item_ptr(leaf, path->slots[0],
141 struct btrfs_inode_item);
142 btrfs_item_key(leaf, &disk_key, path->slots[0]);
143 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
144 sizeof(*inode_item));
145 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
146 btrfs_set_inode_size(leaf, inode_item, 0);
147 btrfs_set_inode_nbytes(leaf, inode_item, 0);
148 btrfs_set_inode_uid(leaf, inode_item, 0);
149 btrfs_set_inode_gid(leaf, inode_item, 0);
150 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
151 btrfs_set_inode_flags(leaf, inode_item, flags);
152 btrfs_set_inode_nlink(leaf, inode_item, 1);
153 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
154 btrfs_set_inode_block_group(leaf, inode_item, offset);
155 btrfs_mark_buffer_dirty(leaf);
156 btrfs_release_path(path);
158 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 sizeof(struct btrfs_free_space_header));
165 btrfs_release_path(path);
168 leaf = path->nodes[0];
169 header = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_free_space_header);
171 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
172 btrfs_set_free_space_key(leaf, header, &disk_key);
173 btrfs_mark_buffer_dirty(leaf);
174 btrfs_release_path(path);
179 int create_free_space_inode(struct btrfs_root *root,
180 struct btrfs_trans_handle *trans,
181 struct btrfs_block_group_cache *block_group,
182 struct btrfs_path *path)
187 ret = btrfs_find_free_objectid(root, &ino);
191 return __create_free_space_inode(root, trans, path, ino,
192 block_group->key.objectid);
195 int btrfs_truncate_free_space_cache(struct btrfs_root *root,
196 struct btrfs_trans_handle *trans,
197 struct btrfs_path *path,
200 struct btrfs_block_rsv *rsv;
204 rsv = trans->block_rsv;
205 trans->block_rsv = root->orphan_block_rsv;
206 ret = btrfs_block_rsv_check(root, root->orphan_block_rsv, 5);
210 oldsize = i_size_read(inode);
211 btrfs_i_size_write(inode, 0);
212 truncate_pagecache(inode, oldsize, 0);
215 * We don't need an orphan item because truncating the free space cache
216 * will never be split across transactions.
218 ret = btrfs_truncate_inode_items(trans, root, inode,
219 0, BTRFS_EXTENT_DATA_KEY);
221 trans->block_rsv = rsv;
227 ret = btrfs_update_inode(trans, root, inode);
231 static int readahead_cache(struct inode *inode)
233 struct file_ra_state *ra;
234 unsigned long last_index;
236 ra = kzalloc(sizeof(*ra), GFP_NOFS);
240 file_ra_state_init(ra, inode->i_mapping);
241 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
243 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
254 struct btrfs_root *root;
258 unsigned check_crcs:1;
261 static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
262 struct btrfs_root *root)
264 memset(io_ctl, 0, sizeof(struct io_ctl));
265 io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
267 io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages,
272 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
273 io_ctl->check_crcs = 1;
277 static void io_ctl_free(struct io_ctl *io_ctl)
279 kfree(io_ctl->pages);
282 static void io_ctl_unmap_page(struct io_ctl *io_ctl)
285 kunmap(io_ctl->page);
291 static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
293 WARN_ON(io_ctl->cur);
294 BUG_ON(io_ctl->index >= io_ctl->num_pages);
295 io_ctl->page = io_ctl->pages[io_ctl->index++];
296 io_ctl->cur = kmap(io_ctl->page);
297 io_ctl->orig = io_ctl->cur;
298 io_ctl->size = PAGE_CACHE_SIZE;
300 memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
303 static void io_ctl_drop_pages(struct io_ctl *io_ctl)
307 io_ctl_unmap_page(io_ctl);
309 for (i = 0; i < io_ctl->num_pages; i++) {
310 ClearPageChecked(io_ctl->pages[i]);
311 unlock_page(io_ctl->pages[i]);
312 page_cache_release(io_ctl->pages[i]);
316 static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode,
320 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
323 for (i = 0; i < io_ctl->num_pages; i++) {
324 page = find_or_create_page(inode->i_mapping, i, mask);
326 io_ctl_drop_pages(io_ctl);
329 io_ctl->pages[i] = page;
330 if (uptodate && !PageUptodate(page)) {
331 btrfs_readpage(NULL, page);
333 if (!PageUptodate(page)) {
334 printk(KERN_ERR "btrfs: error reading free "
336 io_ctl_drop_pages(io_ctl);
345 static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
349 io_ctl_map_page(io_ctl, 1);
352 * Skip the csum areas. If we don't check crcs then we just have a
353 * 64bit chunk at the front of the first page.
355 if (io_ctl->check_crcs) {
356 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
357 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
359 io_ctl->cur += sizeof(u64);
360 io_ctl->size -= sizeof(u64) * 2;
364 *val = cpu_to_le64(generation);
365 io_ctl->cur += sizeof(u64);
368 static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
373 * Skip the crc area. If we don't check crcs then we just have a 64bit
374 * chunk at the front of the first page.
376 if (io_ctl->check_crcs) {
377 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
378 io_ctl->size -= sizeof(u64) +
379 (sizeof(u32) * io_ctl->num_pages);
381 io_ctl->cur += sizeof(u64);
382 io_ctl->size -= sizeof(u64) * 2;
386 if (le64_to_cpu(*gen) != generation) {
387 printk_ratelimited(KERN_ERR "btrfs: space cache generation "
388 "(%Lu) does not match inode (%Lu)\n", *gen,
390 io_ctl_unmap_page(io_ctl);
393 io_ctl->cur += sizeof(u64);
397 static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
403 if (!io_ctl->check_crcs) {
404 io_ctl_unmap_page(io_ctl);
409 offset = sizeof(u32) * io_ctl->num_pages;;
411 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
412 PAGE_CACHE_SIZE - offset);
413 btrfs_csum_final(crc, (char *)&crc);
414 io_ctl_unmap_page(io_ctl);
415 tmp = kmap(io_ctl->pages[0]);
418 kunmap(io_ctl->pages[0]);
421 static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
427 if (!io_ctl->check_crcs) {
428 io_ctl_map_page(io_ctl, 0);
433 offset = sizeof(u32) * io_ctl->num_pages;
435 tmp = kmap(io_ctl->pages[0]);
438 kunmap(io_ctl->pages[0]);
440 io_ctl_map_page(io_ctl, 0);
441 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
442 PAGE_CACHE_SIZE - offset);
443 btrfs_csum_final(crc, (char *)&crc);
445 printk_ratelimited(KERN_ERR "btrfs: csum mismatch on free "
447 io_ctl_unmap_page(io_ctl);
454 static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
457 struct btrfs_free_space_entry *entry;
463 entry->offset = cpu_to_le64(offset);
464 entry->bytes = cpu_to_le64(bytes);
465 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
466 BTRFS_FREE_SPACE_EXTENT;
467 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
468 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
470 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
473 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
475 /* No more pages to map */
476 if (io_ctl->index >= io_ctl->num_pages)
479 /* map the next page */
480 io_ctl_map_page(io_ctl, 1);
484 static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
490 * If we aren't at the start of the current page, unmap this one and
491 * map the next one if there is any left.
493 if (io_ctl->cur != io_ctl->orig) {
494 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
495 if (io_ctl->index >= io_ctl->num_pages)
497 io_ctl_map_page(io_ctl, 0);
500 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
501 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
502 if (io_ctl->index < io_ctl->num_pages)
503 io_ctl_map_page(io_ctl, 0);
507 static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
510 * If we're not on the boundary we know we've modified the page and we
511 * need to crc the page.
513 if (io_ctl->cur != io_ctl->orig)
514 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
516 io_ctl_unmap_page(io_ctl);
518 while (io_ctl->index < io_ctl->num_pages) {
519 io_ctl_map_page(io_ctl, 1);
520 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
524 static int io_ctl_read_entry(struct io_ctl *io_ctl,
525 struct btrfs_free_space *entry, u8 *type)
527 struct btrfs_free_space_entry *e;
530 entry->offset = le64_to_cpu(e->offset);
531 entry->bytes = le64_to_cpu(e->bytes);
533 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
534 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
536 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
539 io_ctl_unmap_page(io_ctl);
541 if (io_ctl->index >= io_ctl->num_pages)
544 return io_ctl_check_crc(io_ctl, io_ctl->index);
547 static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
548 struct btrfs_free_space *entry)
552 if (io_ctl->cur && io_ctl->cur != io_ctl->orig)
553 io_ctl_unmap_page(io_ctl);
555 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
559 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
560 io_ctl_unmap_page(io_ctl);
565 int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
566 struct btrfs_free_space_ctl *ctl,
567 struct btrfs_path *path, u64 offset)
569 struct btrfs_free_space_header *header;
570 struct extent_buffer *leaf;
571 struct io_ctl io_ctl;
572 struct btrfs_key key;
573 struct btrfs_free_space *e, *n;
574 struct list_head bitmaps;
581 INIT_LIST_HEAD(&bitmaps);
583 /* Nothing in the space cache, goodbye */
584 if (!i_size_read(inode))
587 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
595 btrfs_release_path(path);
601 leaf = path->nodes[0];
602 header = btrfs_item_ptr(leaf, path->slots[0],
603 struct btrfs_free_space_header);
604 num_entries = btrfs_free_space_entries(leaf, header);
605 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
606 generation = btrfs_free_space_generation(leaf, header);
607 btrfs_release_path(path);
609 if (BTRFS_I(inode)->generation != generation) {
610 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
611 " not match free space cache generation (%llu)\n",
612 (unsigned long long)BTRFS_I(inode)->generation,
613 (unsigned long long)generation);
620 io_ctl_init(&io_ctl, inode, root);
621 ret = readahead_cache(inode);
625 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
629 ret = io_ctl_check_crc(&io_ctl, 0);
633 ret = io_ctl_check_generation(&io_ctl, generation);
637 while (num_entries) {
638 e = kmem_cache_zalloc(btrfs_free_space_cachep,
643 ret = io_ctl_read_entry(&io_ctl, e, &type);
645 kmem_cache_free(btrfs_free_space_cachep, e);
650 kmem_cache_free(btrfs_free_space_cachep, e);
654 if (type == BTRFS_FREE_SPACE_EXTENT) {
655 spin_lock(&ctl->tree_lock);
656 ret = link_free_space(ctl, e);
657 spin_unlock(&ctl->tree_lock);
659 printk(KERN_ERR "Duplicate entries in "
660 "free space cache, dumping\n");
661 kmem_cache_free(btrfs_free_space_cachep, e);
665 BUG_ON(!num_bitmaps);
667 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
670 btrfs_free_space_cachep, e);
673 spin_lock(&ctl->tree_lock);
674 ret = link_free_space(ctl, e);
675 ctl->total_bitmaps++;
676 ctl->op->recalc_thresholds(ctl);
677 spin_unlock(&ctl->tree_lock);
679 printk(KERN_ERR "Duplicate entries in "
680 "free space cache, dumping\n");
681 kmem_cache_free(btrfs_free_space_cachep, e);
684 list_add_tail(&e->list, &bitmaps);
691 * We add the bitmaps at the end of the entries in order that
692 * the bitmap entries are added to the cache.
694 list_for_each_entry_safe(e, n, &bitmaps, list) {
695 list_del_init(&e->list);
696 ret = io_ctl_read_bitmap(&io_ctl, e);
701 io_ctl_drop_pages(&io_ctl);
704 io_ctl_free(&io_ctl);
707 io_ctl_drop_pages(&io_ctl);
708 __btrfs_remove_free_space_cache(ctl);
712 int load_free_space_cache(struct btrfs_fs_info *fs_info,
713 struct btrfs_block_group_cache *block_group)
715 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
716 struct btrfs_root *root = fs_info->tree_root;
718 struct btrfs_path *path;
721 u64 used = btrfs_block_group_used(&block_group->item);
724 * If we're unmounting then just return, since this does a search on the
725 * normal root and not the commit root and we could deadlock.
727 if (btrfs_fs_closing(fs_info))
731 * If this block group has been marked to be cleared for one reason or
732 * another then we can't trust the on disk cache, so just return.
734 spin_lock(&block_group->lock);
735 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
736 spin_unlock(&block_group->lock);
739 spin_unlock(&block_group->lock);
741 path = btrfs_alloc_path();
745 inode = lookup_free_space_inode(root, block_group, path);
747 btrfs_free_path(path);
751 /* We may have converted the inode and made the cache invalid. */
752 spin_lock(&block_group->lock);
753 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
754 spin_unlock(&block_group->lock);
757 spin_unlock(&block_group->lock);
759 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
760 path, block_group->key.objectid);
761 btrfs_free_path(path);
765 spin_lock(&ctl->tree_lock);
766 matched = (ctl->free_space == (block_group->key.offset - used -
767 block_group->bytes_super));
768 spin_unlock(&ctl->tree_lock);
771 __btrfs_remove_free_space_cache(ctl);
772 printk(KERN_ERR "block group %llu has an wrong amount of free "
773 "space\n", block_group->key.objectid);
778 /* This cache is bogus, make sure it gets cleared */
779 spin_lock(&block_group->lock);
780 block_group->disk_cache_state = BTRFS_DC_CLEAR;
781 spin_unlock(&block_group->lock);
784 printk(KERN_ERR "btrfs: failed to load free space cache "
785 "for block group %llu\n", block_group->key.objectid);
793 * __btrfs_write_out_cache - write out cached info to an inode
794 * @root - the root the inode belongs to
795 * @ctl - the free space cache we are going to write out
796 * @block_group - the block_group for this cache if it belongs to a block_group
797 * @trans - the trans handle
798 * @path - the path to use
799 * @offset - the offset for the key we'll insert
801 * This function writes out a free space cache struct to disk for quick recovery
802 * on mount. This will return 0 if it was successfull in writing the cache out,
803 * and -1 if it was not.
805 int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
806 struct btrfs_free_space_ctl *ctl,
807 struct btrfs_block_group_cache *block_group,
808 struct btrfs_trans_handle *trans,
809 struct btrfs_path *path, u64 offset)
811 struct btrfs_free_space_header *header;
812 struct extent_buffer *leaf;
813 struct rb_node *node;
814 struct list_head *pos, *n;
815 struct extent_state *cached_state = NULL;
816 struct btrfs_free_cluster *cluster = NULL;
817 struct extent_io_tree *unpin = NULL;
818 struct io_ctl io_ctl;
819 struct list_head bitmap_list;
820 struct btrfs_key key;
827 INIT_LIST_HEAD(&bitmap_list);
829 if (!i_size_read(inode))
832 filemap_write_and_wait(inode->i_mapping);
833 btrfs_wait_ordered_range(inode, inode->i_size &
834 ~(root->sectorsize - 1), (u64)-1);
836 io_ctl_init(&io_ctl, inode, root);
838 /* Get the cluster for this block_group if it exists */
839 if (block_group && !list_empty(&block_group->cluster_list))
840 cluster = list_entry(block_group->cluster_list.next,
841 struct btrfs_free_cluster,
845 * We shouldn't have switched the pinned extents yet so this is the
848 unpin = root->fs_info->pinned_extents;
850 /* Lock all pages first so we can lock the extent safely. */
851 io_ctl_prepare_pages(&io_ctl, inode, 0);
853 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
854 0, &cached_state, GFP_NOFS);
857 * When searching for pinned extents, we need to start at our start
861 start = block_group->key.objectid;
863 node = rb_first(&ctl->free_space_offset);
864 if (!node && cluster) {
865 node = rb_first(&cluster->root);
869 /* Make sure we can fit our crcs into the first page */
870 if (io_ctl.check_crcs &&
871 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
876 io_ctl_set_generation(&io_ctl, trans->transid);
878 /* Write out the extent entries */
880 struct btrfs_free_space *e;
882 e = rb_entry(node, struct btrfs_free_space, offset_index);
885 ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes,
891 list_add_tail(&e->list, &bitmap_list);
894 node = rb_next(node);
895 if (!node && cluster) {
896 node = rb_first(&cluster->root);
902 * We want to add any pinned extents to our free space cache
903 * so we don't leak the space
905 while (block_group && (start < block_group->key.objectid +
906 block_group->key.offset)) {
907 ret = find_first_extent_bit(unpin, start, &start, &end,
914 /* This pinned extent is out of our range */
915 if (start >= block_group->key.objectid +
916 block_group->key.offset)
919 len = block_group->key.objectid +
920 block_group->key.offset - start;
921 len = min(len, end + 1 - start);
924 ret = io_ctl_add_entry(&io_ctl, start, len, NULL);
931 /* Write out the bitmaps */
932 list_for_each_safe(pos, n, &bitmap_list) {
933 struct btrfs_free_space *entry =
934 list_entry(pos, struct btrfs_free_space, list);
936 ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap);
939 list_del_init(&entry->list);
942 /* Zero out the rest of the pages just to make sure */
943 io_ctl_zero_remaining_pages(&io_ctl);
945 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
946 0, i_size_read(inode), &cached_state);
947 io_ctl_drop_pages(&io_ctl);
948 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
949 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
955 ret = filemap_write_and_wait(inode->i_mapping);
959 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
963 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
965 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
966 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
970 leaf = path->nodes[0];
972 struct btrfs_key found_key;
973 BUG_ON(!path->slots[0]);
975 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
976 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
977 found_key.offset != offset) {
978 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
980 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
982 btrfs_release_path(path);
987 BTRFS_I(inode)->generation = trans->transid;
988 header = btrfs_item_ptr(leaf, path->slots[0],
989 struct btrfs_free_space_header);
990 btrfs_set_free_space_entries(leaf, header, entries);
991 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
992 btrfs_set_free_space_generation(leaf, header, trans->transid);
993 btrfs_mark_buffer_dirty(leaf);
994 btrfs_release_path(path);
998 io_ctl_free(&io_ctl);
1000 invalidate_inode_pages2(inode->i_mapping);
1001 BTRFS_I(inode)->generation = 0;
1003 btrfs_update_inode(trans, root, inode);
1007 list_for_each_safe(pos, n, &bitmap_list) {
1008 struct btrfs_free_space *entry =
1009 list_entry(pos, struct btrfs_free_space, list);
1010 list_del_init(&entry->list);
1012 io_ctl_drop_pages(&io_ctl);
1013 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1014 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1018 int btrfs_write_out_cache(struct btrfs_root *root,
1019 struct btrfs_trans_handle *trans,
1020 struct btrfs_block_group_cache *block_group,
1021 struct btrfs_path *path)
1023 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1024 struct inode *inode;
1027 root = root->fs_info->tree_root;
1029 spin_lock(&block_group->lock);
1030 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1031 spin_unlock(&block_group->lock);
1034 spin_unlock(&block_group->lock);
1036 inode = lookup_free_space_inode(root, block_group, path);
1040 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1041 path, block_group->key.objectid);
1043 spin_lock(&block_group->lock);
1044 block_group->disk_cache_state = BTRFS_DC_ERROR;
1045 spin_unlock(&block_group->lock);
1048 printk(KERN_ERR "btrfs: failed to write free space cace "
1049 "for block group %llu\n", block_group->key.objectid);
1057 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
1060 BUG_ON(offset < bitmap_start);
1061 offset -= bitmap_start;
1062 return (unsigned long)(div_u64(offset, unit));
1065 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
1067 return (unsigned long)(div_u64(bytes, unit));
1070 static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
1074 u64 bytes_per_bitmap;
1076 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1077 bitmap_start = offset - ctl->start;
1078 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1079 bitmap_start *= bytes_per_bitmap;
1080 bitmap_start += ctl->start;
1082 return bitmap_start;
1085 static int tree_insert_offset(struct rb_root *root, u64 offset,
1086 struct rb_node *node, int bitmap)
1088 struct rb_node **p = &root->rb_node;
1089 struct rb_node *parent = NULL;
1090 struct btrfs_free_space *info;
1094 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1096 if (offset < info->offset) {
1098 } else if (offset > info->offset) {
1099 p = &(*p)->rb_right;
1102 * we could have a bitmap entry and an extent entry
1103 * share the same offset. If this is the case, we want
1104 * the extent entry to always be found first if we do a
1105 * linear search through the tree, since we want to have
1106 * the quickest allocation time, and allocating from an
1107 * extent is faster than allocating from a bitmap. So
1108 * if we're inserting a bitmap and we find an entry at
1109 * this offset, we want to go right, or after this entry
1110 * logically. If we are inserting an extent and we've
1111 * found a bitmap, we want to go left, or before
1119 p = &(*p)->rb_right;
1121 if (!info->bitmap) {
1130 rb_link_node(node, parent, p);
1131 rb_insert_color(node, root);
1137 * searches the tree for the given offset.
1139 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1140 * want a section that has at least bytes size and comes at or after the given
1143 static struct btrfs_free_space *
1144 tree_search_offset(struct btrfs_free_space_ctl *ctl,
1145 u64 offset, int bitmap_only, int fuzzy)
1147 struct rb_node *n = ctl->free_space_offset.rb_node;
1148 struct btrfs_free_space *entry, *prev = NULL;
1150 /* find entry that is closest to the 'offset' */
1157 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1160 if (offset < entry->offset)
1162 else if (offset > entry->offset)
1175 * bitmap entry and extent entry may share same offset,
1176 * in that case, bitmap entry comes after extent entry.
1181 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1182 if (entry->offset != offset)
1185 WARN_ON(!entry->bitmap);
1188 if (entry->bitmap) {
1190 * if previous extent entry covers the offset,
1191 * we should return it instead of the bitmap entry
1193 n = &entry->offset_index;
1198 prev = rb_entry(n, struct btrfs_free_space,
1200 if (!prev->bitmap) {
1201 if (prev->offset + prev->bytes > offset)
1213 /* find last entry before the 'offset' */
1215 if (entry->offset > offset) {
1216 n = rb_prev(&entry->offset_index);
1218 entry = rb_entry(n, struct btrfs_free_space,
1220 BUG_ON(entry->offset > offset);
1229 if (entry->bitmap) {
1230 n = &entry->offset_index;
1235 prev = rb_entry(n, struct btrfs_free_space,
1237 if (!prev->bitmap) {
1238 if (prev->offset + prev->bytes > offset)
1243 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
1245 } else if (entry->offset + entry->bytes > offset)
1252 if (entry->bitmap) {
1253 if (entry->offset + BITS_PER_BITMAP *
1257 if (entry->offset + entry->bytes > offset)
1261 n = rb_next(&entry->offset_index);
1264 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1270 __unlink_free_space(struct btrfs_free_space_ctl *ctl,
1271 struct btrfs_free_space *info)
1273 rb_erase(&info->offset_index, &ctl->free_space_offset);
1274 ctl->free_extents--;
1277 static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
1278 struct btrfs_free_space *info)
1280 __unlink_free_space(ctl, info);
1281 ctl->free_space -= info->bytes;
1284 static int link_free_space(struct btrfs_free_space_ctl *ctl,
1285 struct btrfs_free_space *info)
1289 BUG_ON(!info->bitmap && !info->bytes);
1290 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
1291 &info->offset_index, (info->bitmap != NULL));
1295 ctl->free_space += info->bytes;
1296 ctl->free_extents++;
1300 static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
1302 struct btrfs_block_group_cache *block_group = ctl->private;
1306 u64 size = block_group->key.offset;
1307 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1308 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1310 BUG_ON(ctl->total_bitmaps > max_bitmaps);
1313 * The goal is to keep the total amount of memory used per 1gb of space
1314 * at or below 32k, so we need to adjust how much memory we allow to be
1315 * used by extent based free space tracking
1317 if (size < 1024 * 1024 * 1024)
1318 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1320 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1321 div64_u64(size, 1024 * 1024 * 1024);
1324 * we want to account for 1 more bitmap than what we have so we can make
1325 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1326 * we add more bitmaps.
1328 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
1330 if (bitmap_bytes >= max_bytes) {
1331 ctl->extents_thresh = 0;
1336 * we want the extent entry threshold to always be at most 1/2 the maxw
1337 * bytes we can have, or whatever is less than that.
1339 extent_bytes = max_bytes - bitmap_bytes;
1340 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1342 ctl->extents_thresh =
1343 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
1346 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1347 struct btrfs_free_space *info,
1348 u64 offset, u64 bytes)
1350 unsigned long start, count;
1352 start = offset_to_bit(info->offset, ctl->unit, offset);
1353 count = bytes_to_bits(bytes, ctl->unit);
1354 BUG_ON(start + count > BITS_PER_BITMAP);
1356 bitmap_clear(info->bitmap, start, count);
1358 info->bytes -= bytes;
1361 static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1362 struct btrfs_free_space *info, u64 offset,
1365 __bitmap_clear_bits(ctl, info, offset, bytes);
1366 ctl->free_space -= bytes;
1369 static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
1370 struct btrfs_free_space *info, u64 offset,
1373 unsigned long start, count;
1375 start = offset_to_bit(info->offset, ctl->unit, offset);
1376 count = bytes_to_bits(bytes, ctl->unit);
1377 BUG_ON(start + count > BITS_PER_BITMAP);
1379 bitmap_set(info->bitmap, start, count);
1381 info->bytes += bytes;
1382 ctl->free_space += bytes;
1385 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
1386 struct btrfs_free_space *bitmap_info, u64 *offset,
1389 unsigned long found_bits = 0;
1390 unsigned long bits, i;
1391 unsigned long next_zero;
1393 i = offset_to_bit(bitmap_info->offset, ctl->unit,
1394 max_t(u64, *offset, bitmap_info->offset));
1395 bits = bytes_to_bits(*bytes, ctl->unit);
1397 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1398 i < BITS_PER_BITMAP;
1399 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1400 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1401 BITS_PER_BITMAP, i);
1402 if ((next_zero - i) >= bits) {
1403 found_bits = next_zero - i;
1410 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1411 *bytes = (u64)(found_bits) * ctl->unit;
1418 static struct btrfs_free_space *
1419 find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
1421 struct btrfs_free_space *entry;
1422 struct rb_node *node;
1425 if (!ctl->free_space_offset.rb_node)
1428 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
1432 for (node = &entry->offset_index; node; node = rb_next(node)) {
1433 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1434 if (entry->bytes < *bytes)
1437 if (entry->bitmap) {
1438 ret = search_bitmap(ctl, entry, offset, bytes);
1444 *offset = entry->offset;
1445 *bytes = entry->bytes;
1452 static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
1453 struct btrfs_free_space *info, u64 offset)
1455 info->offset = offset_to_bitmap(ctl, offset);
1457 link_free_space(ctl, info);
1458 ctl->total_bitmaps++;
1460 ctl->op->recalc_thresholds(ctl);
1463 static void free_bitmap(struct btrfs_free_space_ctl *ctl,
1464 struct btrfs_free_space *bitmap_info)
1466 unlink_free_space(ctl, bitmap_info);
1467 kfree(bitmap_info->bitmap);
1468 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
1469 ctl->total_bitmaps--;
1470 ctl->op->recalc_thresholds(ctl);
1473 static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
1474 struct btrfs_free_space *bitmap_info,
1475 u64 *offset, u64 *bytes)
1478 u64 search_start, search_bytes;
1482 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
1485 * XXX - this can go away after a few releases.
1487 * since the only user of btrfs_remove_free_space is the tree logging
1488 * stuff, and the only way to test that is under crash conditions, we
1489 * want to have this debug stuff here just in case somethings not
1490 * working. Search the bitmap for the space we are trying to use to
1491 * make sure its actually there. If its not there then we need to stop
1492 * because something has gone wrong.
1494 search_start = *offset;
1495 search_bytes = *bytes;
1496 search_bytes = min(search_bytes, end - search_start + 1);
1497 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
1498 BUG_ON(ret < 0 || search_start != *offset);
1500 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
1501 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
1502 *bytes -= end - *offset + 1;
1504 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
1505 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
1510 struct rb_node *next = rb_next(&bitmap_info->offset_index);
1511 if (!bitmap_info->bytes)
1512 free_bitmap(ctl, bitmap_info);
1515 * no entry after this bitmap, but we still have bytes to
1516 * remove, so something has gone wrong.
1521 bitmap_info = rb_entry(next, struct btrfs_free_space,
1525 * if the next entry isn't a bitmap we need to return to let the
1526 * extent stuff do its work.
1528 if (!bitmap_info->bitmap)
1532 * Ok the next item is a bitmap, but it may not actually hold
1533 * the information for the rest of this free space stuff, so
1534 * look for it, and if we don't find it return so we can try
1535 * everything over again.
1537 search_start = *offset;
1538 search_bytes = *bytes;
1539 ret = search_bitmap(ctl, bitmap_info, &search_start,
1541 if (ret < 0 || search_start != *offset)
1545 } else if (!bitmap_info->bytes)
1546 free_bitmap(ctl, bitmap_info);
1551 static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1552 struct btrfs_free_space *info, u64 offset,
1555 u64 bytes_to_set = 0;
1558 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1560 bytes_to_set = min(end - offset, bytes);
1562 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1564 return bytes_to_set;
1568 static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1569 struct btrfs_free_space *info)
1571 struct btrfs_block_group_cache *block_group = ctl->private;
1574 * If we are below the extents threshold then we can add this as an
1575 * extent, and don't have to deal with the bitmap
1577 if (ctl->free_extents < ctl->extents_thresh) {
1579 * If this block group has some small extents we don't want to
1580 * use up all of our free slots in the cache with them, we want
1581 * to reserve them to larger extents, however if we have plent
1582 * of cache left then go ahead an dadd them, no sense in adding
1583 * the overhead of a bitmap if we don't have to.
1585 if (info->bytes <= block_group->sectorsize * 4) {
1586 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1594 * some block groups are so tiny they can't be enveloped by a bitmap, so
1595 * don't even bother to create a bitmap for this
1597 if (BITS_PER_BITMAP * block_group->sectorsize >
1598 block_group->key.offset)
1604 static struct btrfs_free_space_op free_space_op = {
1605 .recalc_thresholds = recalculate_thresholds,
1606 .use_bitmap = use_bitmap,
1609 static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1610 struct btrfs_free_space *info)
1612 struct btrfs_free_space *bitmap_info;
1613 struct btrfs_block_group_cache *block_group = NULL;
1615 u64 bytes, offset, bytes_added;
1618 bytes = info->bytes;
1619 offset = info->offset;
1621 if (!ctl->op->use_bitmap(ctl, info))
1624 if (ctl->op == &free_space_op)
1625 block_group = ctl->private;
1628 * Since we link bitmaps right into the cluster we need to see if we
1629 * have a cluster here, and if so and it has our bitmap we need to add
1630 * the free space to that bitmap.
1632 if (block_group && !list_empty(&block_group->cluster_list)) {
1633 struct btrfs_free_cluster *cluster;
1634 struct rb_node *node;
1635 struct btrfs_free_space *entry;
1637 cluster = list_entry(block_group->cluster_list.next,
1638 struct btrfs_free_cluster,
1640 spin_lock(&cluster->lock);
1641 node = rb_first(&cluster->root);
1643 spin_unlock(&cluster->lock);
1644 goto no_cluster_bitmap;
1647 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1648 if (!entry->bitmap) {
1649 spin_unlock(&cluster->lock);
1650 goto no_cluster_bitmap;
1653 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1654 bytes_added = add_bytes_to_bitmap(ctl, entry,
1656 bytes -= bytes_added;
1657 offset += bytes_added;
1659 spin_unlock(&cluster->lock);
1667 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1674 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1675 bytes -= bytes_added;
1676 offset += bytes_added;
1686 if (info && info->bitmap) {
1687 add_new_bitmap(ctl, info, offset);
1692 spin_unlock(&ctl->tree_lock);
1694 /* no pre-allocated info, allocate a new one */
1696 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1699 spin_lock(&ctl->tree_lock);
1705 /* allocate the bitmap */
1706 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1707 spin_lock(&ctl->tree_lock);
1708 if (!info->bitmap) {
1718 kfree(info->bitmap);
1719 kmem_cache_free(btrfs_free_space_cachep, info);
1725 static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
1726 struct btrfs_free_space *info, bool update_stat)
1728 struct btrfs_free_space *left_info;
1729 struct btrfs_free_space *right_info;
1730 bool merged = false;
1731 u64 offset = info->offset;
1732 u64 bytes = info->bytes;
1735 * first we want to see if there is free space adjacent to the range we
1736 * are adding, if there is remove that struct and add a new one to
1737 * cover the entire range
1739 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
1740 if (right_info && rb_prev(&right_info->offset_index))
1741 left_info = rb_entry(rb_prev(&right_info->offset_index),
1742 struct btrfs_free_space, offset_index);
1744 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
1746 if (right_info && !right_info->bitmap) {
1748 unlink_free_space(ctl, right_info);
1750 __unlink_free_space(ctl, right_info);
1751 info->bytes += right_info->bytes;
1752 kmem_cache_free(btrfs_free_space_cachep, right_info);
1756 if (left_info && !left_info->bitmap &&
1757 left_info->offset + left_info->bytes == offset) {
1759 unlink_free_space(ctl, left_info);
1761 __unlink_free_space(ctl, left_info);
1762 info->offset = left_info->offset;
1763 info->bytes += left_info->bytes;
1764 kmem_cache_free(btrfs_free_space_cachep, left_info);
1771 int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1772 u64 offset, u64 bytes)
1774 struct btrfs_free_space *info;
1777 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
1781 info->offset = offset;
1782 info->bytes = bytes;
1784 spin_lock(&ctl->tree_lock);
1786 if (try_merge_free_space(ctl, info, true))
1790 * There was no extent directly to the left or right of this new
1791 * extent then we know we're going to have to allocate a new extent, so
1792 * before we do that see if we need to drop this into a bitmap
1794 ret = insert_into_bitmap(ctl, info);
1802 ret = link_free_space(ctl, info);
1804 kmem_cache_free(btrfs_free_space_cachep, info);
1806 spin_unlock(&ctl->tree_lock);
1809 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
1810 BUG_ON(ret == -EEXIST);
1816 int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1817 u64 offset, u64 bytes)
1819 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1820 struct btrfs_free_space *info;
1821 struct btrfs_free_space *next_info = NULL;
1824 spin_lock(&ctl->tree_lock);
1827 info = tree_search_offset(ctl, offset, 0, 0);
1830 * oops didn't find an extent that matched the space we wanted
1831 * to remove, look for a bitmap instead
1833 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
1841 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1843 next_info = rb_entry(rb_next(&info->offset_index),
1844 struct btrfs_free_space,
1847 if (next_info->bitmap)
1848 end = next_info->offset +
1849 BITS_PER_BITMAP * ctl->unit - 1;
1851 end = next_info->offset + next_info->bytes;
1853 if (next_info->bytes < bytes ||
1854 next_info->offset > offset || offset > end) {
1855 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1856 " trying to use %llu\n",
1857 (unsigned long long)info->offset,
1858 (unsigned long long)info->bytes,
1859 (unsigned long long)bytes);
1868 if (info->bytes == bytes) {
1869 unlink_free_space(ctl, info);
1871 kfree(info->bitmap);
1872 ctl->total_bitmaps--;
1874 kmem_cache_free(btrfs_free_space_cachep, info);
1878 if (!info->bitmap && info->offset == offset) {
1879 unlink_free_space(ctl, info);
1880 info->offset += bytes;
1881 info->bytes -= bytes;
1882 link_free_space(ctl, info);
1886 if (!info->bitmap && info->offset <= offset &&
1887 info->offset + info->bytes >= offset + bytes) {
1888 u64 old_start = info->offset;
1890 * we're freeing space in the middle of the info,
1891 * this can happen during tree log replay
1893 * first unlink the old info and then
1894 * insert it again after the hole we're creating
1896 unlink_free_space(ctl, info);
1897 if (offset + bytes < info->offset + info->bytes) {
1898 u64 old_end = info->offset + info->bytes;
1900 info->offset = offset + bytes;
1901 info->bytes = old_end - info->offset;
1902 ret = link_free_space(ctl, info);
1907 /* the hole we're creating ends at the end
1908 * of the info struct, just free the info
1910 kmem_cache_free(btrfs_free_space_cachep, info);
1912 spin_unlock(&ctl->tree_lock);
1914 /* step two, insert a new info struct to cover
1915 * anything before the hole
1917 ret = btrfs_add_free_space(block_group, old_start,
1918 offset - old_start);
1923 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
1928 spin_unlock(&ctl->tree_lock);
1933 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1936 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1937 struct btrfs_free_space *info;
1941 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
1942 info = rb_entry(n, struct btrfs_free_space, offset_index);
1943 if (info->bytes >= bytes)
1945 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
1946 (unsigned long long)info->offset,
1947 (unsigned long long)info->bytes,
1948 (info->bitmap) ? "yes" : "no");
1950 printk(KERN_INFO "block group has cluster?: %s\n",
1951 list_empty(&block_group->cluster_list) ? "no" : "yes");
1952 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1956 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
1958 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1960 spin_lock_init(&ctl->tree_lock);
1961 ctl->unit = block_group->sectorsize;
1962 ctl->start = block_group->key.objectid;
1963 ctl->private = block_group;
1964 ctl->op = &free_space_op;
1967 * we only want to have 32k of ram per block group for keeping
1968 * track of free space, and if we pass 1/2 of that we want to
1969 * start converting things over to using bitmaps
1971 ctl->extents_thresh = ((1024 * 32) / 2) /
1972 sizeof(struct btrfs_free_space);
1976 * for a given cluster, put all of its extents back into the free
1977 * space cache. If the block group passed doesn't match the block group
1978 * pointed to by the cluster, someone else raced in and freed the
1979 * cluster already. In that case, we just return without changing anything
1982 __btrfs_return_cluster_to_free_space(
1983 struct btrfs_block_group_cache *block_group,
1984 struct btrfs_free_cluster *cluster)
1986 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1987 struct btrfs_free_space *entry;
1988 struct rb_node *node;
1990 spin_lock(&cluster->lock);
1991 if (cluster->block_group != block_group)
1994 cluster->block_group = NULL;
1995 cluster->window_start = 0;
1996 list_del_init(&cluster->block_group_list);
1998 node = rb_first(&cluster->root);
2002 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2003 node = rb_next(&entry->offset_index);
2004 rb_erase(&entry->offset_index, &cluster->root);
2006 bitmap = (entry->bitmap != NULL);
2008 try_merge_free_space(ctl, entry, false);
2009 tree_insert_offset(&ctl->free_space_offset,
2010 entry->offset, &entry->offset_index, bitmap);
2012 cluster->root = RB_ROOT;
2015 spin_unlock(&cluster->lock);
2016 btrfs_put_block_group(block_group);
2020 void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
2022 struct btrfs_free_space *info;
2023 struct rb_node *node;
2025 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2026 info = rb_entry(node, struct btrfs_free_space, offset_index);
2027 if (!info->bitmap) {
2028 unlink_free_space(ctl, info);
2029 kmem_cache_free(btrfs_free_space_cachep, info);
2031 free_bitmap(ctl, info);
2033 if (need_resched()) {
2034 spin_unlock(&ctl->tree_lock);
2036 spin_lock(&ctl->tree_lock);
2041 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2043 spin_lock(&ctl->tree_lock);
2044 __btrfs_remove_free_space_cache_locked(ctl);
2045 spin_unlock(&ctl->tree_lock);
2048 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2050 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2051 struct btrfs_free_cluster *cluster;
2052 struct list_head *head;
2054 spin_lock(&ctl->tree_lock);
2055 while ((head = block_group->cluster_list.next) !=
2056 &block_group->cluster_list) {
2057 cluster = list_entry(head, struct btrfs_free_cluster,
2060 WARN_ON(cluster->block_group != block_group);
2061 __btrfs_return_cluster_to_free_space(block_group, cluster);
2062 if (need_resched()) {
2063 spin_unlock(&ctl->tree_lock);
2065 spin_lock(&ctl->tree_lock);
2068 __btrfs_remove_free_space_cache_locked(ctl);
2069 spin_unlock(&ctl->tree_lock);
2073 u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2074 u64 offset, u64 bytes, u64 empty_size)
2076 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2077 struct btrfs_free_space *entry = NULL;
2078 u64 bytes_search = bytes + empty_size;
2081 spin_lock(&ctl->tree_lock);
2082 entry = find_free_space(ctl, &offset, &bytes_search);
2087 if (entry->bitmap) {
2088 bitmap_clear_bits(ctl, entry, offset, bytes);
2090 free_bitmap(ctl, entry);
2092 unlink_free_space(ctl, entry);
2093 entry->offset += bytes;
2094 entry->bytes -= bytes;
2096 kmem_cache_free(btrfs_free_space_cachep, entry);
2098 link_free_space(ctl, entry);
2102 spin_unlock(&ctl->tree_lock);
2108 * given a cluster, put all of its extents back into the free space
2109 * cache. If a block group is passed, this function will only free
2110 * a cluster that belongs to the passed block group.
2112 * Otherwise, it'll get a reference on the block group pointed to by the
2113 * cluster and remove the cluster from it.
2115 int btrfs_return_cluster_to_free_space(
2116 struct btrfs_block_group_cache *block_group,
2117 struct btrfs_free_cluster *cluster)
2119 struct btrfs_free_space_ctl *ctl;
2122 /* first, get a safe pointer to the block group */
2123 spin_lock(&cluster->lock);
2125 block_group = cluster->block_group;
2127 spin_unlock(&cluster->lock);
2130 } else if (cluster->block_group != block_group) {
2131 /* someone else has already freed it don't redo their work */
2132 spin_unlock(&cluster->lock);
2135 atomic_inc(&block_group->count);
2136 spin_unlock(&cluster->lock);
2138 ctl = block_group->free_space_ctl;
2140 /* now return any extents the cluster had on it */
2141 spin_lock(&ctl->tree_lock);
2142 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
2143 spin_unlock(&ctl->tree_lock);
2145 /* finally drop our ref */
2146 btrfs_put_block_group(block_group);
2150 static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2151 struct btrfs_free_cluster *cluster,
2152 struct btrfs_free_space *entry,
2153 u64 bytes, u64 min_start)
2155 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2157 u64 search_start = cluster->window_start;
2158 u64 search_bytes = bytes;
2161 search_start = min_start;
2162 search_bytes = bytes;
2164 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
2169 __bitmap_clear_bits(ctl, entry, ret, bytes);
2175 * given a cluster, try to allocate 'bytes' from it, returns 0
2176 * if it couldn't find anything suitably large, or a logical disk offset
2177 * if things worked out
2179 u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2180 struct btrfs_free_cluster *cluster, u64 bytes,
2183 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2184 struct btrfs_free_space *entry = NULL;
2185 struct rb_node *node;
2188 spin_lock(&cluster->lock);
2189 if (bytes > cluster->max_size)
2192 if (cluster->block_group != block_group)
2195 node = rb_first(&cluster->root);
2199 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2201 if (entry->bytes < bytes ||
2202 (!entry->bitmap && entry->offset < min_start)) {
2203 node = rb_next(&entry->offset_index);
2206 entry = rb_entry(node, struct btrfs_free_space,
2211 if (entry->bitmap) {
2212 ret = btrfs_alloc_from_bitmap(block_group,
2213 cluster, entry, bytes,
2216 node = rb_next(&entry->offset_index);
2219 entry = rb_entry(node, struct btrfs_free_space,
2224 ret = entry->offset;
2226 entry->offset += bytes;
2227 entry->bytes -= bytes;
2230 if (entry->bytes == 0)
2231 rb_erase(&entry->offset_index, &cluster->root);
2235 spin_unlock(&cluster->lock);
2240 spin_lock(&ctl->tree_lock);
2242 ctl->free_space -= bytes;
2243 if (entry->bytes == 0) {
2244 ctl->free_extents--;
2245 if (entry->bitmap) {
2246 kfree(entry->bitmap);
2247 ctl->total_bitmaps--;
2248 ctl->op->recalc_thresholds(ctl);
2250 kmem_cache_free(btrfs_free_space_cachep, entry);
2253 spin_unlock(&ctl->tree_lock);
2258 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2259 struct btrfs_free_space *entry,
2260 struct btrfs_free_cluster *cluster,
2261 u64 offset, u64 bytes, u64 min_bytes)
2263 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2264 unsigned long next_zero;
2266 unsigned long search_bits;
2267 unsigned long total_bits;
2268 unsigned long found_bits;
2269 unsigned long start = 0;
2270 unsigned long total_found = 0;
2274 i = offset_to_bit(entry->offset, block_group->sectorsize,
2275 max_t(u64, offset, entry->offset));
2276 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2277 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
2281 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2282 i < BITS_PER_BITMAP;
2283 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2284 next_zero = find_next_zero_bit(entry->bitmap,
2285 BITS_PER_BITMAP, i);
2286 if (next_zero - i >= search_bits) {
2287 found_bits = next_zero - i;
2301 total_found += found_bits;
2303 if (cluster->max_size < found_bits * block_group->sectorsize)
2304 cluster->max_size = found_bits * block_group->sectorsize;
2306 if (total_found < total_bits) {
2307 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2308 if (i - start > total_bits * 2) {
2310 cluster->max_size = 0;
2316 cluster->window_start = start * block_group->sectorsize +
2318 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2319 ret = tree_insert_offset(&cluster->root, entry->offset,
2320 &entry->offset_index, 1);
2327 * This searches the block group for just extents to fill the cluster with.
2330 setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2331 struct btrfs_free_cluster *cluster,
2332 struct list_head *bitmaps, u64 offset, u64 bytes,
2335 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2336 struct btrfs_free_space *first = NULL;
2337 struct btrfs_free_space *entry = NULL;
2338 struct btrfs_free_space *prev = NULL;
2339 struct btrfs_free_space *last;
2340 struct rb_node *node;
2344 u64 max_gap = 128 * 1024;
2346 entry = tree_search_offset(ctl, offset, 0, 1);
2351 * We don't want bitmaps, so just move along until we find a normal
2354 while (entry->bitmap) {
2355 if (list_empty(&entry->list))
2356 list_add_tail(&entry->list, bitmaps);
2357 node = rb_next(&entry->offset_index);
2360 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2363 window_start = entry->offset;
2364 window_free = entry->bytes;
2365 max_extent = entry->bytes;
2370 while (window_free <= min_bytes) {
2371 node = rb_next(&entry->offset_index);
2374 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2376 if (entry->bitmap) {
2377 if (list_empty(&entry->list))
2378 list_add_tail(&entry->list, bitmaps);
2383 * we haven't filled the empty size and the window is
2384 * very large. reset and try again
2386 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2387 entry->offset - window_start > (min_bytes * 2)) {
2389 window_start = entry->offset;
2390 window_free = entry->bytes;
2392 max_extent = entry->bytes;
2395 window_free += entry->bytes;
2396 if (entry->bytes > max_extent)
2397 max_extent = entry->bytes;
2402 cluster->window_start = first->offset;
2404 node = &first->offset_index;
2407 * now we've found our entries, pull them out of the free space
2408 * cache and put them into the cluster rbtree
2413 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2414 node = rb_next(&entry->offset_index);
2418 rb_erase(&entry->offset_index, &ctl->free_space_offset);
2419 ret = tree_insert_offset(&cluster->root, entry->offset,
2420 &entry->offset_index, 0);
2422 } while (node && entry != last);
2424 cluster->max_size = max_extent;
2430 * This specifically looks for bitmaps that may work in the cluster, we assume
2431 * that we have already failed to find extents that will work.
2434 setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2435 struct btrfs_free_cluster *cluster,
2436 struct list_head *bitmaps, u64 offset, u64 bytes,
2439 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2440 struct btrfs_free_space *entry;
2441 struct rb_node *node;
2444 if (ctl->total_bitmaps == 0)
2448 * First check our cached list of bitmaps and see if there is an entry
2449 * here that will work.
2451 list_for_each_entry(entry, bitmaps, list) {
2452 if (entry->bytes < min_bytes)
2454 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2461 * If we do have entries on our list and we are here then we didn't find
2462 * anything, so go ahead and get the next entry after the last entry in
2463 * this list and start the search from there.
2465 if (!list_empty(bitmaps)) {
2466 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2468 node = rb_next(&entry->offset_index);
2471 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2475 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
2480 node = &entry->offset_index;
2482 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2483 node = rb_next(&entry->offset_index);
2486 if (entry->bytes < min_bytes)
2488 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2490 } while (ret && node);
2496 * here we try to find a cluster of blocks in a block group. The goal
2497 * is to find at least bytes free and up to empty_size + bytes free.
2498 * We might not find them all in one contiguous area.
2500 * returns zero and sets up cluster if things worked out, otherwise
2501 * it returns -enospc
2503 int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
2504 struct btrfs_root *root,
2505 struct btrfs_block_group_cache *block_group,
2506 struct btrfs_free_cluster *cluster,
2507 u64 offset, u64 bytes, u64 empty_size)
2509 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2510 struct list_head bitmaps;
2511 struct btrfs_free_space *entry, *tmp;
2515 /* for metadata, allow allocates with more holes */
2516 if (btrfs_test_opt(root, SSD_SPREAD)) {
2517 min_bytes = bytes + empty_size;
2518 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
2520 * we want to do larger allocations when we are
2521 * flushing out the delayed refs, it helps prevent
2522 * making more work as we go along.
2524 if (trans->transaction->delayed_refs.flushing)
2525 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2527 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2529 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2531 spin_lock(&ctl->tree_lock);
2534 * If we know we don't have enough space to make a cluster don't even
2535 * bother doing all the work to try and find one.
2537 if (ctl->free_space < min_bytes) {
2538 spin_unlock(&ctl->tree_lock);
2542 spin_lock(&cluster->lock);
2544 /* someone already found a cluster, hooray */
2545 if (cluster->block_group) {
2550 INIT_LIST_HEAD(&bitmaps);
2551 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2554 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2555 offset, bytes, min_bytes);
2557 /* Clear our temporary list */
2558 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2559 list_del_init(&entry->list);
2562 atomic_inc(&block_group->count);
2563 list_add_tail(&cluster->block_group_list,
2564 &block_group->cluster_list);
2565 cluster->block_group = block_group;
2568 spin_unlock(&cluster->lock);
2569 spin_unlock(&ctl->tree_lock);
2575 * simple code to zero out a cluster
2577 void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2579 spin_lock_init(&cluster->lock);
2580 spin_lock_init(&cluster->refill_lock);
2581 cluster->root = RB_ROOT;
2582 cluster->max_size = 0;
2583 INIT_LIST_HEAD(&cluster->block_group_list);
2584 cluster->block_group = NULL;
2587 int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2588 u64 *trimmed, u64 start, u64 end, u64 minlen)
2590 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2591 struct btrfs_free_space *entry = NULL;
2592 struct btrfs_fs_info *fs_info = block_group->fs_info;
2594 u64 actually_trimmed;
2599 while (start < end) {
2600 spin_lock(&ctl->tree_lock);
2602 if (ctl->free_space < minlen) {
2603 spin_unlock(&ctl->tree_lock);
2607 entry = tree_search_offset(ctl, start, 0, 1);
2609 entry = tree_search_offset(ctl,
2610 offset_to_bitmap(ctl, start),
2613 if (!entry || entry->offset >= end) {
2614 spin_unlock(&ctl->tree_lock);
2618 if (entry->bitmap) {
2619 ret = search_bitmap(ctl, entry, &start, &bytes);
2622 spin_unlock(&ctl->tree_lock);
2625 bytes = min(bytes, end - start);
2626 bitmap_clear_bits(ctl, entry, start, bytes);
2627 if (entry->bytes == 0)
2628 free_bitmap(ctl, entry);
2630 start = entry->offset + BITS_PER_BITMAP *
2631 block_group->sectorsize;
2632 spin_unlock(&ctl->tree_lock);
2637 start = entry->offset;
2638 bytes = min(entry->bytes, end - start);
2639 unlink_free_space(ctl, entry);
2640 kmem_cache_free(btrfs_free_space_cachep, entry);
2643 spin_unlock(&ctl->tree_lock);
2645 if (bytes >= minlen) {
2646 struct btrfs_space_info *space_info;
2649 space_info = block_group->space_info;
2650 spin_lock(&space_info->lock);
2651 spin_lock(&block_group->lock);
2652 if (!block_group->ro) {
2653 block_group->reserved += bytes;
2654 space_info->bytes_reserved += bytes;
2657 spin_unlock(&block_group->lock);
2658 spin_unlock(&space_info->lock);
2660 ret = btrfs_error_discard_extent(fs_info->extent_root,
2665 btrfs_add_free_space(block_group, start, bytes);
2667 spin_lock(&space_info->lock);
2668 spin_lock(&block_group->lock);
2669 if (block_group->ro)
2670 space_info->bytes_readonly += bytes;
2671 block_group->reserved -= bytes;
2672 space_info->bytes_reserved -= bytes;
2673 spin_unlock(&space_info->lock);
2674 spin_unlock(&block_group->lock);
2679 *trimmed += actually_trimmed;
2684 if (fatal_signal_pending(current)) {
2696 * Find the left-most item in the cache tree, and then return the
2697 * smallest inode number in the item.
2699 * Note: the returned inode number may not be the smallest one in
2700 * the tree, if the left-most item is a bitmap.
2702 u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2704 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2705 struct btrfs_free_space *entry = NULL;
2708 spin_lock(&ctl->tree_lock);
2710 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2713 entry = rb_entry(rb_first(&ctl->free_space_offset),
2714 struct btrfs_free_space, offset_index);
2716 if (!entry->bitmap) {
2717 ino = entry->offset;
2719 unlink_free_space(ctl, entry);
2723 kmem_cache_free(btrfs_free_space_cachep, entry);
2725 link_free_space(ctl, entry);
2731 ret = search_bitmap(ctl, entry, &offset, &count);
2735 bitmap_clear_bits(ctl, entry, offset, 1);
2736 if (entry->bytes == 0)
2737 free_bitmap(ctl, entry);
2740 spin_unlock(&ctl->tree_lock);
2745 struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2746 struct btrfs_path *path)
2748 struct inode *inode = NULL;
2750 spin_lock(&root->cache_lock);
2751 if (root->cache_inode)
2752 inode = igrab(root->cache_inode);
2753 spin_unlock(&root->cache_lock);
2757 inode = __lookup_free_space_inode(root, path, 0);
2761 spin_lock(&root->cache_lock);
2762 if (!btrfs_fs_closing(root->fs_info))
2763 root->cache_inode = igrab(inode);
2764 spin_unlock(&root->cache_lock);
2769 int create_free_ino_inode(struct btrfs_root *root,
2770 struct btrfs_trans_handle *trans,
2771 struct btrfs_path *path)
2773 return __create_free_space_inode(root, trans, path,
2774 BTRFS_FREE_INO_OBJECTID, 0);
2777 int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2779 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2780 struct btrfs_path *path;
2781 struct inode *inode;
2783 u64 root_gen = btrfs_root_generation(&root->root_item);
2785 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2789 * If we're unmounting then just return, since this does a search on the
2790 * normal root and not the commit root and we could deadlock.
2792 if (btrfs_fs_closing(fs_info))
2795 path = btrfs_alloc_path();
2799 inode = lookup_free_ino_inode(root, path);
2803 if (root_gen != BTRFS_I(inode)->generation)
2806 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2809 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2810 "root %llu\n", root->root_key.objectid);
2814 btrfs_free_path(path);
2818 int btrfs_write_out_ino_cache(struct btrfs_root *root,
2819 struct btrfs_trans_handle *trans,
2820 struct btrfs_path *path)
2822 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2823 struct inode *inode;
2826 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2829 inode = lookup_free_ino_inode(root, path);
2833 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
2835 btrfs_delalloc_release_metadata(inode, inode->i_size);
2837 printk(KERN_ERR "btrfs: failed to write free ino cache "
2838 "for root %llu\n", root->root_key.objectid);