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
;
205 rsv
= trans
->block_rsv
;
206 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
208 /* 1 for slack space, 1 for updating the inode */
209 needed_bytes
= btrfs_calc_trunc_metadata_size(root
, 1) +
210 btrfs_calc_trans_metadata_size(root
, 1);
212 spin_lock(&trans
->block_rsv
->lock
);
213 if (trans
->block_rsv
->reserved
< needed_bytes
) {
214 spin_unlock(&trans
->block_rsv
->lock
);
215 trans
->block_rsv
= rsv
;
218 spin_unlock(&trans
->block_rsv
->lock
);
220 oldsize
= i_size_read(inode
);
221 btrfs_i_size_write(inode
, 0);
222 truncate_pagecache(inode
, oldsize
, 0);
225 * We don't need an orphan item because truncating the free space cache
226 * will never be split across transactions.
228 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
229 0, BTRFS_EXTENT_DATA_KEY
);
232 trans
->block_rsv
= rsv
;
237 ret
= btrfs_update_inode(trans
, root
, inode
);
238 trans
->block_rsv
= rsv
;
243 static int readahead_cache(struct inode
*inode
)
245 struct file_ra_state
*ra
;
246 unsigned long last_index
;
248 ra
= kzalloc(sizeof(*ra
), GFP_NOFS
);
252 file_ra_state_init(ra
, inode
->i_mapping
);
253 last_index
= (i_size_read(inode
) - 1) >> PAGE_CACHE_SHIFT
;
255 page_cache_sync_readahead(inode
->i_mapping
, ra
, NULL
, 0, last_index
);
266 struct btrfs_root
*root
;
270 unsigned check_crcs
:1;
273 static int io_ctl_init(struct io_ctl
*io_ctl
, struct inode
*inode
,
274 struct btrfs_root
*root
)
276 memset(io_ctl
, 0, sizeof(struct io_ctl
));
277 io_ctl
->num_pages
= (i_size_read(inode
) + PAGE_CACHE_SIZE
- 1) >>
279 io_ctl
->pages
= kzalloc(sizeof(struct page
*) * io_ctl
->num_pages
,
284 if (btrfs_ino(inode
) != BTRFS_FREE_INO_OBJECTID
)
285 io_ctl
->check_crcs
= 1;
289 static void io_ctl_free(struct io_ctl
*io_ctl
)
291 kfree(io_ctl
->pages
);
294 static void io_ctl_unmap_page(struct io_ctl
*io_ctl
)
297 kunmap(io_ctl
->page
);
303 static void io_ctl_map_page(struct io_ctl
*io_ctl
, int clear
)
305 WARN_ON(io_ctl
->cur
);
306 BUG_ON(io_ctl
->index
>= io_ctl
->num_pages
);
307 io_ctl
->page
= io_ctl
->pages
[io_ctl
->index
++];
308 io_ctl
->cur
= kmap(io_ctl
->page
);
309 io_ctl
->orig
= io_ctl
->cur
;
310 io_ctl
->size
= PAGE_CACHE_SIZE
;
312 memset(io_ctl
->cur
, 0, PAGE_CACHE_SIZE
);
315 static void io_ctl_drop_pages(struct io_ctl
*io_ctl
)
319 io_ctl_unmap_page(io_ctl
);
321 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
322 ClearPageChecked(io_ctl
->pages
[i
]);
323 unlock_page(io_ctl
->pages
[i
]);
324 page_cache_release(io_ctl
->pages
[i
]);
328 static int io_ctl_prepare_pages(struct io_ctl
*io_ctl
, struct inode
*inode
,
332 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
335 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
336 page
= find_or_create_page(inode
->i_mapping
, i
, mask
);
338 io_ctl_drop_pages(io_ctl
);
341 io_ctl
->pages
[i
] = page
;
342 if (uptodate
&& !PageUptodate(page
)) {
343 btrfs_readpage(NULL
, page
);
345 if (!PageUptodate(page
)) {
346 printk(KERN_ERR
"btrfs: error reading free "
348 io_ctl_drop_pages(io_ctl
);
354 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
355 clear_page_dirty_for_io(io_ctl
->pages
[i
]);
356 set_page_extent_mapped(io_ctl
->pages
[i
]);
362 static void io_ctl_set_generation(struct io_ctl
*io_ctl
, u64 generation
)
366 io_ctl_map_page(io_ctl
, 1);
369 * Skip the csum areas. If we don't check crcs then we just have a
370 * 64bit chunk at the front of the first page.
372 if (io_ctl
->check_crcs
) {
373 io_ctl
->cur
+= (sizeof(u32
) * io_ctl
->num_pages
);
374 io_ctl
->size
-= sizeof(u64
) + (sizeof(u32
) * io_ctl
->num_pages
);
376 io_ctl
->cur
+= sizeof(u64
);
377 io_ctl
->size
-= sizeof(u64
) * 2;
381 *val
= cpu_to_le64(generation
);
382 io_ctl
->cur
+= sizeof(u64
);
385 static int io_ctl_check_generation(struct io_ctl
*io_ctl
, u64 generation
)
390 * Skip the crc area. If we don't check crcs then we just have a 64bit
391 * chunk at the front of the first page.
393 if (io_ctl
->check_crcs
) {
394 io_ctl
->cur
+= sizeof(u32
) * io_ctl
->num_pages
;
395 io_ctl
->size
-= sizeof(u64
) +
396 (sizeof(u32
) * io_ctl
->num_pages
);
398 io_ctl
->cur
+= sizeof(u64
);
399 io_ctl
->size
-= sizeof(u64
) * 2;
403 if (le64_to_cpu(*gen
) != generation
) {
404 printk_ratelimited(KERN_ERR
"btrfs: space cache generation "
405 "(%Lu) does not match inode (%Lu)\n", *gen
,
407 io_ctl_unmap_page(io_ctl
);
410 io_ctl
->cur
+= sizeof(u64
);
414 static void io_ctl_set_crc(struct io_ctl
*io_ctl
, int index
)
420 if (!io_ctl
->check_crcs
) {
421 io_ctl_unmap_page(io_ctl
);
426 offset
= sizeof(u32
) * io_ctl
->num_pages
;;
428 crc
= btrfs_csum_data(io_ctl
->root
, io_ctl
->orig
+ offset
, crc
,
429 PAGE_CACHE_SIZE
- offset
);
430 btrfs_csum_final(crc
, (char *)&crc
);
431 io_ctl_unmap_page(io_ctl
);
432 tmp
= kmap(io_ctl
->pages
[0]);
435 kunmap(io_ctl
->pages
[0]);
438 static int io_ctl_check_crc(struct io_ctl
*io_ctl
, int index
)
444 if (!io_ctl
->check_crcs
) {
445 io_ctl_map_page(io_ctl
, 0);
450 offset
= sizeof(u32
) * io_ctl
->num_pages
;
452 tmp
= kmap(io_ctl
->pages
[0]);
455 kunmap(io_ctl
->pages
[0]);
457 io_ctl_map_page(io_ctl
, 0);
458 crc
= btrfs_csum_data(io_ctl
->root
, io_ctl
->orig
+ offset
, crc
,
459 PAGE_CACHE_SIZE
- offset
);
460 btrfs_csum_final(crc
, (char *)&crc
);
462 printk_ratelimited(KERN_ERR
"btrfs: csum mismatch on free "
464 io_ctl_unmap_page(io_ctl
);
471 static int io_ctl_add_entry(struct io_ctl
*io_ctl
, u64 offset
, u64 bytes
,
474 struct btrfs_free_space_entry
*entry
;
480 entry
->offset
= cpu_to_le64(offset
);
481 entry
->bytes
= cpu_to_le64(bytes
);
482 entry
->type
= (bitmap
) ? BTRFS_FREE_SPACE_BITMAP
:
483 BTRFS_FREE_SPACE_EXTENT
;
484 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
485 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
487 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
490 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
492 /* No more pages to map */
493 if (io_ctl
->index
>= io_ctl
->num_pages
)
496 /* map the next page */
497 io_ctl_map_page(io_ctl
, 1);
501 static int io_ctl_add_bitmap(struct io_ctl
*io_ctl
, void *bitmap
)
507 * If we aren't at the start of the current page, unmap this one and
508 * map the next one if there is any left.
510 if (io_ctl
->cur
!= io_ctl
->orig
) {
511 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
512 if (io_ctl
->index
>= io_ctl
->num_pages
)
514 io_ctl_map_page(io_ctl
, 0);
517 memcpy(io_ctl
->cur
, bitmap
, PAGE_CACHE_SIZE
);
518 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
519 if (io_ctl
->index
< io_ctl
->num_pages
)
520 io_ctl_map_page(io_ctl
, 0);
524 static void io_ctl_zero_remaining_pages(struct io_ctl
*io_ctl
)
527 * If we're not on the boundary we know we've modified the page and we
528 * need to crc the page.
530 if (io_ctl
->cur
!= io_ctl
->orig
)
531 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
533 io_ctl_unmap_page(io_ctl
);
535 while (io_ctl
->index
< io_ctl
->num_pages
) {
536 io_ctl_map_page(io_ctl
, 1);
537 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
541 static int io_ctl_read_entry(struct io_ctl
*io_ctl
,
542 struct btrfs_free_space
*entry
, u8
*type
)
544 struct btrfs_free_space_entry
*e
;
548 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
554 entry
->offset
= le64_to_cpu(e
->offset
);
555 entry
->bytes
= le64_to_cpu(e
->bytes
);
557 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
558 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
560 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
563 io_ctl_unmap_page(io_ctl
);
568 static int io_ctl_read_bitmap(struct io_ctl
*io_ctl
,
569 struct btrfs_free_space
*entry
)
573 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
577 memcpy(entry
->bitmap
, io_ctl
->cur
, PAGE_CACHE_SIZE
);
578 io_ctl_unmap_page(io_ctl
);
583 int __load_free_space_cache(struct btrfs_root
*root
, struct inode
*inode
,
584 struct btrfs_free_space_ctl
*ctl
,
585 struct btrfs_path
*path
, u64 offset
)
587 struct btrfs_free_space_header
*header
;
588 struct extent_buffer
*leaf
;
589 struct io_ctl io_ctl
;
590 struct btrfs_key key
;
591 struct btrfs_free_space
*e
, *n
;
592 struct list_head bitmaps
;
599 INIT_LIST_HEAD(&bitmaps
);
601 /* Nothing in the space cache, goodbye */
602 if (!i_size_read(inode
))
605 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
609 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
613 btrfs_release_path(path
);
619 leaf
= path
->nodes
[0];
620 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
621 struct btrfs_free_space_header
);
622 num_entries
= btrfs_free_space_entries(leaf
, header
);
623 num_bitmaps
= btrfs_free_space_bitmaps(leaf
, header
);
624 generation
= btrfs_free_space_generation(leaf
, header
);
625 btrfs_release_path(path
);
627 if (BTRFS_I(inode
)->generation
!= generation
) {
628 printk(KERN_ERR
"btrfs: free space inode generation (%llu) did"
629 " not match free space cache generation (%llu)\n",
630 (unsigned long long)BTRFS_I(inode
)->generation
,
631 (unsigned long long)generation
);
638 io_ctl_init(&io_ctl
, inode
, root
);
639 ret
= readahead_cache(inode
);
643 ret
= io_ctl_prepare_pages(&io_ctl
, inode
, 1);
647 ret
= io_ctl_check_crc(&io_ctl
, 0);
651 ret
= io_ctl_check_generation(&io_ctl
, generation
);
655 while (num_entries
) {
656 e
= kmem_cache_zalloc(btrfs_free_space_cachep
,
661 ret
= io_ctl_read_entry(&io_ctl
, e
, &type
);
663 kmem_cache_free(btrfs_free_space_cachep
, e
);
668 kmem_cache_free(btrfs_free_space_cachep
, e
);
672 if (type
== BTRFS_FREE_SPACE_EXTENT
) {
673 spin_lock(&ctl
->tree_lock
);
674 ret
= link_free_space(ctl
, e
);
675 spin_unlock(&ctl
->tree_lock
);
677 printk(KERN_ERR
"Duplicate entries in "
678 "free space cache, dumping\n");
679 kmem_cache_free(btrfs_free_space_cachep
, e
);
683 BUG_ON(!num_bitmaps
);
685 e
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
688 btrfs_free_space_cachep
, e
);
691 spin_lock(&ctl
->tree_lock
);
692 ret
= link_free_space(ctl
, e
);
693 ctl
->total_bitmaps
++;
694 ctl
->op
->recalc_thresholds(ctl
);
695 spin_unlock(&ctl
->tree_lock
);
697 printk(KERN_ERR
"Duplicate entries in "
698 "free space cache, dumping\n");
699 kmem_cache_free(btrfs_free_space_cachep
, e
);
702 list_add_tail(&e
->list
, &bitmaps
);
708 io_ctl_unmap_page(&io_ctl
);
711 * We add the bitmaps at the end of the entries in order that
712 * the bitmap entries are added to the cache.
714 list_for_each_entry_safe(e
, n
, &bitmaps
, list
) {
715 list_del_init(&e
->list
);
716 ret
= io_ctl_read_bitmap(&io_ctl
, e
);
721 io_ctl_drop_pages(&io_ctl
);
724 io_ctl_free(&io_ctl
);
727 io_ctl_drop_pages(&io_ctl
);
728 __btrfs_remove_free_space_cache(ctl
);
732 int load_free_space_cache(struct btrfs_fs_info
*fs_info
,
733 struct btrfs_block_group_cache
*block_group
)
735 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
736 struct btrfs_root
*root
= fs_info
->tree_root
;
738 struct btrfs_path
*path
;
741 u64 used
= btrfs_block_group_used(&block_group
->item
);
744 * If we're unmounting then just return, since this does a search on the
745 * normal root and not the commit root and we could deadlock.
747 if (btrfs_fs_closing(fs_info
))
751 * If this block group has been marked to be cleared for one reason or
752 * another then we can't trust the on disk cache, so just return.
754 spin_lock(&block_group
->lock
);
755 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
756 spin_unlock(&block_group
->lock
);
759 spin_unlock(&block_group
->lock
);
761 path
= btrfs_alloc_path();
765 inode
= lookup_free_space_inode(root
, block_group
, path
);
767 btrfs_free_path(path
);
771 /* We may have converted the inode and made the cache invalid. */
772 spin_lock(&block_group
->lock
);
773 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
774 spin_unlock(&block_group
->lock
);
777 spin_unlock(&block_group
->lock
);
779 ret
= __load_free_space_cache(fs_info
->tree_root
, inode
, ctl
,
780 path
, block_group
->key
.objectid
);
781 btrfs_free_path(path
);
785 spin_lock(&ctl
->tree_lock
);
786 matched
= (ctl
->free_space
== (block_group
->key
.offset
- used
-
787 block_group
->bytes_super
));
788 spin_unlock(&ctl
->tree_lock
);
791 __btrfs_remove_free_space_cache(ctl
);
792 printk(KERN_ERR
"block group %llu has an wrong amount of free "
793 "space\n", block_group
->key
.objectid
);
798 /* This cache is bogus, make sure it gets cleared */
799 spin_lock(&block_group
->lock
);
800 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
801 spin_unlock(&block_group
->lock
);
804 printk(KERN_ERR
"btrfs: failed to load free space cache "
805 "for block group %llu\n", block_group
->key
.objectid
);
813 * __btrfs_write_out_cache - write out cached info to an inode
814 * @root - the root the inode belongs to
815 * @ctl - the free space cache we are going to write out
816 * @block_group - the block_group for this cache if it belongs to a block_group
817 * @trans - the trans handle
818 * @path - the path to use
819 * @offset - the offset for the key we'll insert
821 * This function writes out a free space cache struct to disk for quick recovery
822 * on mount. This will return 0 if it was successfull in writing the cache out,
823 * and -1 if it was not.
825 int __btrfs_write_out_cache(struct btrfs_root
*root
, struct inode
*inode
,
826 struct btrfs_free_space_ctl
*ctl
,
827 struct btrfs_block_group_cache
*block_group
,
828 struct btrfs_trans_handle
*trans
,
829 struct btrfs_path
*path
, u64 offset
)
831 struct btrfs_free_space_header
*header
;
832 struct extent_buffer
*leaf
;
833 struct rb_node
*node
;
834 struct list_head
*pos
, *n
;
835 struct extent_state
*cached_state
= NULL
;
836 struct btrfs_free_cluster
*cluster
= NULL
;
837 struct extent_io_tree
*unpin
= NULL
;
838 struct io_ctl io_ctl
;
839 struct list_head bitmap_list
;
840 struct btrfs_key key
;
847 INIT_LIST_HEAD(&bitmap_list
);
849 if (!i_size_read(inode
))
852 io_ctl_init(&io_ctl
, inode
, root
);
854 /* Get the cluster for this block_group if it exists */
855 if (block_group
&& !list_empty(&block_group
->cluster_list
))
856 cluster
= list_entry(block_group
->cluster_list
.next
,
857 struct btrfs_free_cluster
,
861 * We shouldn't have switched the pinned extents yet so this is the
864 unpin
= root
->fs_info
->pinned_extents
;
866 /* Lock all pages first so we can lock the extent safely. */
867 io_ctl_prepare_pages(&io_ctl
, inode
, 0);
869 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
870 0, &cached_state
, GFP_NOFS
);
873 * When searching for pinned extents, we need to start at our start
877 start
= block_group
->key
.objectid
;
879 node
= rb_first(&ctl
->free_space_offset
);
880 if (!node
&& cluster
) {
881 node
= rb_first(&cluster
->root
);
885 /* Make sure we can fit our crcs into the first page */
886 if (io_ctl
.check_crcs
&&
887 (io_ctl
.num_pages
* sizeof(u32
)) >= PAGE_CACHE_SIZE
) {
892 io_ctl_set_generation(&io_ctl
, trans
->transid
);
894 /* Write out the extent entries */
896 struct btrfs_free_space
*e
;
898 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
901 ret
= io_ctl_add_entry(&io_ctl
, e
->offset
, e
->bytes
,
907 list_add_tail(&e
->list
, &bitmap_list
);
910 node
= rb_next(node
);
911 if (!node
&& cluster
) {
912 node
= rb_first(&cluster
->root
);
918 * We want to add any pinned extents to our free space cache
919 * so we don't leak the space
921 while (block_group
&& (start
< block_group
->key
.objectid
+
922 block_group
->key
.offset
)) {
923 ret
= find_first_extent_bit(unpin
, start
, &start
, &end
,
930 /* This pinned extent is out of our range */
931 if (start
>= block_group
->key
.objectid
+
932 block_group
->key
.offset
)
935 len
= block_group
->key
.objectid
+
936 block_group
->key
.offset
- start
;
937 len
= min(len
, end
+ 1 - start
);
940 ret
= io_ctl_add_entry(&io_ctl
, start
, len
, NULL
);
947 /* Write out the bitmaps */
948 list_for_each_safe(pos
, n
, &bitmap_list
) {
949 struct btrfs_free_space
*entry
=
950 list_entry(pos
, struct btrfs_free_space
, list
);
952 ret
= io_ctl_add_bitmap(&io_ctl
, entry
->bitmap
);
955 list_del_init(&entry
->list
);
958 /* Zero out the rest of the pages just to make sure */
959 io_ctl_zero_remaining_pages(&io_ctl
);
961 ret
= btrfs_dirty_pages(root
, inode
, io_ctl
.pages
, io_ctl
.num_pages
,
962 0, i_size_read(inode
), &cached_state
);
963 io_ctl_drop_pages(&io_ctl
);
964 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
965 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
971 ret
= filemap_write_and_wait(inode
->i_mapping
);
975 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
979 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
981 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, inode
->i_size
- 1,
982 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, NULL
,
986 leaf
= path
->nodes
[0];
988 struct btrfs_key found_key
;
989 BUG_ON(!path
->slots
[0]);
991 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
992 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
993 found_key
.offset
!= offset
) {
994 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0,
996 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0,
998 btrfs_release_path(path
);
1003 BTRFS_I(inode
)->generation
= trans
->transid
;
1004 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
1005 struct btrfs_free_space_header
);
1006 btrfs_set_free_space_entries(leaf
, header
, entries
);
1007 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
1008 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
1009 btrfs_mark_buffer_dirty(leaf
);
1010 btrfs_release_path(path
);
1014 io_ctl_free(&io_ctl
);
1016 invalidate_inode_pages2(inode
->i_mapping
);
1017 BTRFS_I(inode
)->generation
= 0;
1019 btrfs_update_inode(trans
, root
, inode
);
1023 list_for_each_safe(pos
, n
, &bitmap_list
) {
1024 struct btrfs_free_space
*entry
=
1025 list_entry(pos
, struct btrfs_free_space
, list
);
1026 list_del_init(&entry
->list
);
1028 io_ctl_drop_pages(&io_ctl
);
1029 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
1030 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
1034 int btrfs_write_out_cache(struct btrfs_root
*root
,
1035 struct btrfs_trans_handle
*trans
,
1036 struct btrfs_block_group_cache
*block_group
,
1037 struct btrfs_path
*path
)
1039 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1040 struct inode
*inode
;
1043 root
= root
->fs_info
->tree_root
;
1045 spin_lock(&block_group
->lock
);
1046 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
1047 spin_unlock(&block_group
->lock
);
1050 spin_unlock(&block_group
->lock
);
1052 inode
= lookup_free_space_inode(root
, block_group
, path
);
1056 ret
= __btrfs_write_out_cache(root
, inode
, ctl
, block_group
, trans
,
1057 path
, block_group
->key
.objectid
);
1059 spin_lock(&block_group
->lock
);
1060 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
1061 spin_unlock(&block_group
->lock
);
1064 printk(KERN_ERR
"btrfs: failed to write free space cace "
1065 "for block group %llu\n", block_group
->key
.objectid
);
1073 static inline unsigned long offset_to_bit(u64 bitmap_start
, u32 unit
,
1076 BUG_ON(offset
< bitmap_start
);
1077 offset
-= bitmap_start
;
1078 return (unsigned long)(div_u64(offset
, unit
));
1081 static inline unsigned long bytes_to_bits(u64 bytes
, u32 unit
)
1083 return (unsigned long)(div_u64(bytes
, unit
));
1086 static inline u64
offset_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1090 u64 bytes_per_bitmap
;
1092 bytes_per_bitmap
= BITS_PER_BITMAP
* ctl
->unit
;
1093 bitmap_start
= offset
- ctl
->start
;
1094 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
1095 bitmap_start
*= bytes_per_bitmap
;
1096 bitmap_start
+= ctl
->start
;
1098 return bitmap_start
;
1101 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
1102 struct rb_node
*node
, int bitmap
)
1104 struct rb_node
**p
= &root
->rb_node
;
1105 struct rb_node
*parent
= NULL
;
1106 struct btrfs_free_space
*info
;
1110 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
1112 if (offset
< info
->offset
) {
1114 } else if (offset
> info
->offset
) {
1115 p
= &(*p
)->rb_right
;
1118 * we could have a bitmap entry and an extent entry
1119 * share the same offset. If this is the case, we want
1120 * the extent entry to always be found first if we do a
1121 * linear search through the tree, since we want to have
1122 * the quickest allocation time, and allocating from an
1123 * extent is faster than allocating from a bitmap. So
1124 * if we're inserting a bitmap and we find an entry at
1125 * this offset, we want to go right, or after this entry
1126 * logically. If we are inserting an extent and we've
1127 * found a bitmap, we want to go left, or before
1135 p
= &(*p
)->rb_right
;
1137 if (!info
->bitmap
) {
1146 rb_link_node(node
, parent
, p
);
1147 rb_insert_color(node
, root
);
1153 * searches the tree for the given offset.
1155 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1156 * want a section that has at least bytes size and comes at or after the given
1159 static struct btrfs_free_space
*
1160 tree_search_offset(struct btrfs_free_space_ctl
*ctl
,
1161 u64 offset
, int bitmap_only
, int fuzzy
)
1163 struct rb_node
*n
= ctl
->free_space_offset
.rb_node
;
1164 struct btrfs_free_space
*entry
, *prev
= NULL
;
1166 /* find entry that is closest to the 'offset' */
1173 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1176 if (offset
< entry
->offset
)
1178 else if (offset
> entry
->offset
)
1191 * bitmap entry and extent entry may share same offset,
1192 * in that case, bitmap entry comes after extent entry.
1197 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1198 if (entry
->offset
!= offset
)
1201 WARN_ON(!entry
->bitmap
);
1204 if (entry
->bitmap
) {
1206 * if previous extent entry covers the offset,
1207 * we should return it instead of the bitmap entry
1209 n
= &entry
->offset_index
;
1214 prev
= rb_entry(n
, struct btrfs_free_space
,
1216 if (!prev
->bitmap
) {
1217 if (prev
->offset
+ prev
->bytes
> offset
)
1229 /* find last entry before the 'offset' */
1231 if (entry
->offset
> offset
) {
1232 n
= rb_prev(&entry
->offset_index
);
1234 entry
= rb_entry(n
, struct btrfs_free_space
,
1236 BUG_ON(entry
->offset
> offset
);
1245 if (entry
->bitmap
) {
1246 n
= &entry
->offset_index
;
1251 prev
= rb_entry(n
, struct btrfs_free_space
,
1253 if (!prev
->bitmap
) {
1254 if (prev
->offset
+ prev
->bytes
> offset
)
1259 if (entry
->offset
+ BITS_PER_BITMAP
* ctl
->unit
> offset
)
1261 } else if (entry
->offset
+ entry
->bytes
> offset
)
1268 if (entry
->bitmap
) {
1269 if (entry
->offset
+ BITS_PER_BITMAP
*
1273 if (entry
->offset
+ entry
->bytes
> offset
)
1277 n
= rb_next(&entry
->offset_index
);
1280 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1286 __unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1287 struct btrfs_free_space
*info
)
1289 rb_erase(&info
->offset_index
, &ctl
->free_space_offset
);
1290 ctl
->free_extents
--;
1293 static void unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1294 struct btrfs_free_space
*info
)
1296 __unlink_free_space(ctl
, info
);
1297 ctl
->free_space
-= info
->bytes
;
1300 static int link_free_space(struct btrfs_free_space_ctl
*ctl
,
1301 struct btrfs_free_space
*info
)
1305 BUG_ON(!info
->bitmap
&& !info
->bytes
);
1306 ret
= tree_insert_offset(&ctl
->free_space_offset
, info
->offset
,
1307 &info
->offset_index
, (info
->bitmap
!= NULL
));
1311 ctl
->free_space
+= info
->bytes
;
1312 ctl
->free_extents
++;
1316 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
1318 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1322 u64 size
= block_group
->key
.offset
;
1323 u64 bytes_per_bg
= BITS_PER_BITMAP
* block_group
->sectorsize
;
1324 int max_bitmaps
= div64_u64(size
+ bytes_per_bg
- 1, bytes_per_bg
);
1326 BUG_ON(ctl
->total_bitmaps
> max_bitmaps
);
1329 * The goal is to keep the total amount of memory used per 1gb of space
1330 * at or below 32k, so we need to adjust how much memory we allow to be
1331 * used by extent based free space tracking
1333 if (size
< 1024 * 1024 * 1024)
1334 max_bytes
= MAX_CACHE_BYTES_PER_GIG
;
1336 max_bytes
= MAX_CACHE_BYTES_PER_GIG
*
1337 div64_u64(size
, 1024 * 1024 * 1024);
1340 * we want to account for 1 more bitmap than what we have so we can make
1341 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1342 * we add more bitmaps.
1344 bitmap_bytes
= (ctl
->total_bitmaps
+ 1) * PAGE_CACHE_SIZE
;
1346 if (bitmap_bytes
>= max_bytes
) {
1347 ctl
->extents_thresh
= 0;
1352 * we want the extent entry threshold to always be at most 1/2 the maxw
1353 * bytes we can have, or whatever is less than that.
1355 extent_bytes
= max_bytes
- bitmap_bytes
;
1356 extent_bytes
= min_t(u64
, extent_bytes
, div64_u64(max_bytes
, 2));
1358 ctl
->extents_thresh
=
1359 div64_u64(extent_bytes
, (sizeof(struct btrfs_free_space
)));
1362 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1363 struct btrfs_free_space
*info
,
1364 u64 offset
, u64 bytes
)
1366 unsigned long start
, count
;
1368 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1369 count
= bytes_to_bits(bytes
, ctl
->unit
);
1370 BUG_ON(start
+ count
> BITS_PER_BITMAP
);
1372 bitmap_clear(info
->bitmap
, start
, count
);
1374 info
->bytes
-= bytes
;
1377 static void bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1378 struct btrfs_free_space
*info
, u64 offset
,
1381 __bitmap_clear_bits(ctl
, info
, offset
, bytes
);
1382 ctl
->free_space
-= bytes
;
1385 static void bitmap_set_bits(struct btrfs_free_space_ctl
*ctl
,
1386 struct btrfs_free_space
*info
, u64 offset
,
1389 unsigned long start
, count
;
1391 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1392 count
= bytes_to_bits(bytes
, ctl
->unit
);
1393 BUG_ON(start
+ count
> BITS_PER_BITMAP
);
1395 bitmap_set(info
->bitmap
, start
, count
);
1397 info
->bytes
+= bytes
;
1398 ctl
->free_space
+= bytes
;
1401 static int search_bitmap(struct btrfs_free_space_ctl
*ctl
,
1402 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1405 unsigned long found_bits
= 0;
1406 unsigned long bits
, i
;
1407 unsigned long next_zero
;
1409 i
= offset_to_bit(bitmap_info
->offset
, ctl
->unit
,
1410 max_t(u64
, *offset
, bitmap_info
->offset
));
1411 bits
= bytes_to_bits(*bytes
, ctl
->unit
);
1413 for (i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
);
1414 i
< BITS_PER_BITMAP
;
1415 i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1416 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1417 BITS_PER_BITMAP
, i
);
1418 if ((next_zero
- i
) >= bits
) {
1419 found_bits
= next_zero
- i
;
1426 *offset
= (u64
)(i
* ctl
->unit
) + bitmap_info
->offset
;
1427 *bytes
= (u64
)(found_bits
) * ctl
->unit
;
1434 static struct btrfs_free_space
*
1435 find_free_space(struct btrfs_free_space_ctl
*ctl
, u64
*offset
, u64
*bytes
)
1437 struct btrfs_free_space
*entry
;
1438 struct rb_node
*node
;
1441 if (!ctl
->free_space_offset
.rb_node
)
1444 entry
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, *offset
), 0, 1);
1448 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1449 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1450 if (entry
->bytes
< *bytes
)
1453 if (entry
->bitmap
) {
1454 ret
= search_bitmap(ctl
, entry
, offset
, bytes
);
1460 *offset
= entry
->offset
;
1461 *bytes
= entry
->bytes
;
1468 static void add_new_bitmap(struct btrfs_free_space_ctl
*ctl
,
1469 struct btrfs_free_space
*info
, u64 offset
)
1471 info
->offset
= offset_to_bitmap(ctl
, offset
);
1473 INIT_LIST_HEAD(&info
->list
);
1474 link_free_space(ctl
, info
);
1475 ctl
->total_bitmaps
++;
1477 ctl
->op
->recalc_thresholds(ctl
);
1480 static void free_bitmap(struct btrfs_free_space_ctl
*ctl
,
1481 struct btrfs_free_space
*bitmap_info
)
1483 unlink_free_space(ctl
, bitmap_info
);
1484 kfree(bitmap_info
->bitmap
);
1485 kmem_cache_free(btrfs_free_space_cachep
, bitmap_info
);
1486 ctl
->total_bitmaps
--;
1487 ctl
->op
->recalc_thresholds(ctl
);
1490 static noinline
int remove_from_bitmap(struct btrfs_free_space_ctl
*ctl
,
1491 struct btrfs_free_space
*bitmap_info
,
1492 u64
*offset
, u64
*bytes
)
1495 u64 search_start
, search_bytes
;
1499 end
= bitmap_info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
) - 1;
1502 * XXX - this can go away after a few releases.
1504 * since the only user of btrfs_remove_free_space is the tree logging
1505 * stuff, and the only way to test that is under crash conditions, we
1506 * want to have this debug stuff here just in case somethings not
1507 * working. Search the bitmap for the space we are trying to use to
1508 * make sure its actually there. If its not there then we need to stop
1509 * because something has gone wrong.
1511 search_start
= *offset
;
1512 search_bytes
= *bytes
;
1513 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1514 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
, &search_bytes
);
1515 BUG_ON(ret
< 0 || search_start
!= *offset
);
1517 if (*offset
> bitmap_info
->offset
&& *offset
+ *bytes
> end
) {
1518 bitmap_clear_bits(ctl
, bitmap_info
, *offset
, end
- *offset
+ 1);
1519 *bytes
-= end
- *offset
+ 1;
1521 } else if (*offset
>= bitmap_info
->offset
&& *offset
+ *bytes
<= end
) {
1522 bitmap_clear_bits(ctl
, bitmap_info
, *offset
, *bytes
);
1527 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1528 if (!bitmap_info
->bytes
)
1529 free_bitmap(ctl
, bitmap_info
);
1532 * no entry after this bitmap, but we still have bytes to
1533 * remove, so something has gone wrong.
1538 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1542 * if the next entry isn't a bitmap we need to return to let the
1543 * extent stuff do its work.
1545 if (!bitmap_info
->bitmap
)
1549 * Ok the next item is a bitmap, but it may not actually hold
1550 * the information for the rest of this free space stuff, so
1551 * look for it, and if we don't find it return so we can try
1552 * everything over again.
1554 search_start
= *offset
;
1555 search_bytes
= *bytes
;
1556 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
,
1558 if (ret
< 0 || search_start
!= *offset
)
1562 } else if (!bitmap_info
->bytes
)
1563 free_bitmap(ctl
, bitmap_info
);
1568 static u64
add_bytes_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1569 struct btrfs_free_space
*info
, u64 offset
,
1572 u64 bytes_to_set
= 0;
1575 end
= info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
);
1577 bytes_to_set
= min(end
- offset
, bytes
);
1579 bitmap_set_bits(ctl
, info
, offset
, bytes_to_set
);
1581 return bytes_to_set
;
1585 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
1586 struct btrfs_free_space
*info
)
1588 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1591 * If we are below the extents threshold then we can add this as an
1592 * extent, and don't have to deal with the bitmap
1594 if (ctl
->free_extents
< ctl
->extents_thresh
) {
1596 * If this block group has some small extents we don't want to
1597 * use up all of our free slots in the cache with them, we want
1598 * to reserve them to larger extents, however if we have plent
1599 * of cache left then go ahead an dadd them, no sense in adding
1600 * the overhead of a bitmap if we don't have to.
1602 if (info
->bytes
<= block_group
->sectorsize
* 4) {
1603 if (ctl
->free_extents
* 2 <= ctl
->extents_thresh
)
1611 * some block groups are so tiny they can't be enveloped by a bitmap, so
1612 * don't even bother to create a bitmap for this
1614 if (BITS_PER_BITMAP
* block_group
->sectorsize
>
1615 block_group
->key
.offset
)
1621 static struct btrfs_free_space_op free_space_op
= {
1622 .recalc_thresholds
= recalculate_thresholds
,
1623 .use_bitmap
= use_bitmap
,
1626 static int insert_into_bitmap(struct btrfs_free_space_ctl
*ctl
,
1627 struct btrfs_free_space
*info
)
1629 struct btrfs_free_space
*bitmap_info
;
1630 struct btrfs_block_group_cache
*block_group
= NULL
;
1632 u64 bytes
, offset
, bytes_added
;
1635 bytes
= info
->bytes
;
1636 offset
= info
->offset
;
1638 if (!ctl
->op
->use_bitmap(ctl
, info
))
1641 if (ctl
->op
== &free_space_op
)
1642 block_group
= ctl
->private;
1645 * Since we link bitmaps right into the cluster we need to see if we
1646 * have a cluster here, and if so and it has our bitmap we need to add
1647 * the free space to that bitmap.
1649 if (block_group
&& !list_empty(&block_group
->cluster_list
)) {
1650 struct btrfs_free_cluster
*cluster
;
1651 struct rb_node
*node
;
1652 struct btrfs_free_space
*entry
;
1654 cluster
= list_entry(block_group
->cluster_list
.next
,
1655 struct btrfs_free_cluster
,
1657 spin_lock(&cluster
->lock
);
1658 node
= rb_first(&cluster
->root
);
1660 spin_unlock(&cluster
->lock
);
1661 goto no_cluster_bitmap
;
1664 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1665 if (!entry
->bitmap
) {
1666 spin_unlock(&cluster
->lock
);
1667 goto no_cluster_bitmap
;
1670 if (entry
->offset
== offset_to_bitmap(ctl
, offset
)) {
1671 bytes_added
= add_bytes_to_bitmap(ctl
, entry
,
1673 bytes
-= bytes_added
;
1674 offset
+= bytes_added
;
1676 spin_unlock(&cluster
->lock
);
1684 bitmap_info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
1691 bytes_added
= add_bytes_to_bitmap(ctl
, bitmap_info
, offset
, bytes
);
1692 bytes
-= bytes_added
;
1693 offset
+= bytes_added
;
1703 if (info
&& info
->bitmap
) {
1704 add_new_bitmap(ctl
, info
, offset
);
1709 spin_unlock(&ctl
->tree_lock
);
1711 /* no pre-allocated info, allocate a new one */
1713 info
= kmem_cache_zalloc(btrfs_free_space_cachep
,
1716 spin_lock(&ctl
->tree_lock
);
1722 /* allocate the bitmap */
1723 info
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
1724 spin_lock(&ctl
->tree_lock
);
1725 if (!info
->bitmap
) {
1735 kfree(info
->bitmap
);
1736 kmem_cache_free(btrfs_free_space_cachep
, info
);
1742 static bool try_merge_free_space(struct btrfs_free_space_ctl
*ctl
,
1743 struct btrfs_free_space
*info
, bool update_stat
)
1745 struct btrfs_free_space
*left_info
;
1746 struct btrfs_free_space
*right_info
;
1747 bool merged
= false;
1748 u64 offset
= info
->offset
;
1749 u64 bytes
= info
->bytes
;
1752 * first we want to see if there is free space adjacent to the range we
1753 * are adding, if there is remove that struct and add a new one to
1754 * cover the entire range
1756 right_info
= tree_search_offset(ctl
, offset
+ bytes
, 0, 0);
1757 if (right_info
&& rb_prev(&right_info
->offset_index
))
1758 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
1759 struct btrfs_free_space
, offset_index
);
1761 left_info
= tree_search_offset(ctl
, offset
- 1, 0, 0);
1763 if (right_info
&& !right_info
->bitmap
) {
1765 unlink_free_space(ctl
, right_info
);
1767 __unlink_free_space(ctl
, right_info
);
1768 info
->bytes
+= right_info
->bytes
;
1769 kmem_cache_free(btrfs_free_space_cachep
, right_info
);
1773 if (left_info
&& !left_info
->bitmap
&&
1774 left_info
->offset
+ left_info
->bytes
== offset
) {
1776 unlink_free_space(ctl
, left_info
);
1778 __unlink_free_space(ctl
, left_info
);
1779 info
->offset
= left_info
->offset
;
1780 info
->bytes
+= left_info
->bytes
;
1781 kmem_cache_free(btrfs_free_space_cachep
, left_info
);
1788 int __btrfs_add_free_space(struct btrfs_free_space_ctl
*ctl
,
1789 u64 offset
, u64 bytes
)
1791 struct btrfs_free_space
*info
;
1794 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
1798 info
->offset
= offset
;
1799 info
->bytes
= bytes
;
1801 spin_lock(&ctl
->tree_lock
);
1803 if (try_merge_free_space(ctl
, info
, true))
1807 * There was no extent directly to the left or right of this new
1808 * extent then we know we're going to have to allocate a new extent, so
1809 * before we do that see if we need to drop this into a bitmap
1811 ret
= insert_into_bitmap(ctl
, info
);
1819 ret
= link_free_space(ctl
, info
);
1821 kmem_cache_free(btrfs_free_space_cachep
, info
);
1823 spin_unlock(&ctl
->tree_lock
);
1826 printk(KERN_CRIT
"btrfs: unable to add free space :%d\n", ret
);
1827 BUG_ON(ret
== -EEXIST
);
1833 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
1834 u64 offset
, u64 bytes
)
1836 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1837 struct btrfs_free_space
*info
;
1838 struct btrfs_free_space
*next_info
= NULL
;
1841 spin_lock(&ctl
->tree_lock
);
1844 info
= tree_search_offset(ctl
, offset
, 0, 0);
1847 * oops didn't find an extent that matched the space we wanted
1848 * to remove, look for a bitmap instead
1850 info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
1853 /* the tree logging code might be calling us before we
1854 * have fully loaded the free space rbtree for this
1855 * block group. So it is possible the entry won't
1856 * be in the rbtree yet at all. The caching code
1857 * will make sure not to put it in the rbtree if
1858 * the logging code has pinned it.
1864 if (info
->bytes
< bytes
&& rb_next(&info
->offset_index
)) {
1866 next_info
= rb_entry(rb_next(&info
->offset_index
),
1867 struct btrfs_free_space
,
1870 if (next_info
->bitmap
)
1871 end
= next_info
->offset
+
1872 BITS_PER_BITMAP
* ctl
->unit
- 1;
1874 end
= next_info
->offset
+ next_info
->bytes
;
1876 if (next_info
->bytes
< bytes
||
1877 next_info
->offset
> offset
|| offset
> end
) {
1878 printk(KERN_CRIT
"Found free space at %llu, size %llu,"
1879 " trying to use %llu\n",
1880 (unsigned long long)info
->offset
,
1881 (unsigned long long)info
->bytes
,
1882 (unsigned long long)bytes
);
1891 if (info
->bytes
== bytes
) {
1892 unlink_free_space(ctl
, info
);
1894 kfree(info
->bitmap
);
1895 ctl
->total_bitmaps
--;
1897 kmem_cache_free(btrfs_free_space_cachep
, info
);
1902 if (!info
->bitmap
&& info
->offset
== offset
) {
1903 unlink_free_space(ctl
, info
);
1904 info
->offset
+= bytes
;
1905 info
->bytes
-= bytes
;
1906 ret
= link_free_space(ctl
, info
);
1911 if (!info
->bitmap
&& info
->offset
<= offset
&&
1912 info
->offset
+ info
->bytes
>= offset
+ bytes
) {
1913 u64 old_start
= info
->offset
;
1915 * we're freeing space in the middle of the info,
1916 * this can happen during tree log replay
1918 * first unlink the old info and then
1919 * insert it again after the hole we're creating
1921 unlink_free_space(ctl
, info
);
1922 if (offset
+ bytes
< info
->offset
+ info
->bytes
) {
1923 u64 old_end
= info
->offset
+ info
->bytes
;
1925 info
->offset
= offset
+ bytes
;
1926 info
->bytes
= old_end
- info
->offset
;
1927 ret
= link_free_space(ctl
, info
);
1932 /* the hole we're creating ends at the end
1933 * of the info struct, just free the info
1935 kmem_cache_free(btrfs_free_space_cachep
, info
);
1937 spin_unlock(&ctl
->tree_lock
);
1939 /* step two, insert a new info struct to cover
1940 * anything before the hole
1942 ret
= btrfs_add_free_space(block_group
, old_start
,
1943 offset
- old_start
);
1948 ret
= remove_from_bitmap(ctl
, info
, &offset
, &bytes
);
1953 spin_unlock(&ctl
->tree_lock
);
1958 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
1961 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1962 struct btrfs_free_space
*info
;
1966 for (n
= rb_first(&ctl
->free_space_offset
); n
; n
= rb_next(n
)) {
1967 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1968 if (info
->bytes
>= bytes
)
1970 printk(KERN_CRIT
"entry offset %llu, bytes %llu, bitmap %s\n",
1971 (unsigned long long)info
->offset
,
1972 (unsigned long long)info
->bytes
,
1973 (info
->bitmap
) ? "yes" : "no");
1975 printk(KERN_INFO
"block group has cluster?: %s\n",
1976 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
1977 printk(KERN_INFO
"%d blocks of free space at or bigger than bytes is"
1981 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache
*block_group
)
1983 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1985 spin_lock_init(&ctl
->tree_lock
);
1986 ctl
->unit
= block_group
->sectorsize
;
1987 ctl
->start
= block_group
->key
.objectid
;
1988 ctl
->private = block_group
;
1989 ctl
->op
= &free_space_op
;
1992 * we only want to have 32k of ram per block group for keeping
1993 * track of free space, and if we pass 1/2 of that we want to
1994 * start converting things over to using bitmaps
1996 ctl
->extents_thresh
= ((1024 * 32) / 2) /
1997 sizeof(struct btrfs_free_space
);
2001 * for a given cluster, put all of its extents back into the free
2002 * space cache. If the block group passed doesn't match the block group
2003 * pointed to by the cluster, someone else raced in and freed the
2004 * cluster already. In that case, we just return without changing anything
2007 __btrfs_return_cluster_to_free_space(
2008 struct btrfs_block_group_cache
*block_group
,
2009 struct btrfs_free_cluster
*cluster
)
2011 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2012 struct btrfs_free_space
*entry
;
2013 struct rb_node
*node
;
2015 spin_lock(&cluster
->lock
);
2016 if (cluster
->block_group
!= block_group
)
2019 cluster
->block_group
= NULL
;
2020 cluster
->window_start
= 0;
2021 list_del_init(&cluster
->block_group_list
);
2023 node
= rb_first(&cluster
->root
);
2027 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2028 node
= rb_next(&entry
->offset_index
);
2029 rb_erase(&entry
->offset_index
, &cluster
->root
);
2031 bitmap
= (entry
->bitmap
!= NULL
);
2033 try_merge_free_space(ctl
, entry
, false);
2034 tree_insert_offset(&ctl
->free_space_offset
,
2035 entry
->offset
, &entry
->offset_index
, bitmap
);
2037 cluster
->root
= RB_ROOT
;
2040 spin_unlock(&cluster
->lock
);
2041 btrfs_put_block_group(block_group
);
2045 void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl
*ctl
)
2047 struct btrfs_free_space
*info
;
2048 struct rb_node
*node
;
2050 while ((node
= rb_last(&ctl
->free_space_offset
)) != NULL
) {
2051 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2052 if (!info
->bitmap
) {
2053 unlink_free_space(ctl
, info
);
2054 kmem_cache_free(btrfs_free_space_cachep
, info
);
2056 free_bitmap(ctl
, info
);
2058 if (need_resched()) {
2059 spin_unlock(&ctl
->tree_lock
);
2061 spin_lock(&ctl
->tree_lock
);
2066 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl
*ctl
)
2068 spin_lock(&ctl
->tree_lock
);
2069 __btrfs_remove_free_space_cache_locked(ctl
);
2070 spin_unlock(&ctl
->tree_lock
);
2073 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
2075 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2076 struct btrfs_free_cluster
*cluster
;
2077 struct list_head
*head
;
2079 spin_lock(&ctl
->tree_lock
);
2080 while ((head
= block_group
->cluster_list
.next
) !=
2081 &block_group
->cluster_list
) {
2082 cluster
= list_entry(head
, struct btrfs_free_cluster
,
2085 WARN_ON(cluster
->block_group
!= block_group
);
2086 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2087 if (need_resched()) {
2088 spin_unlock(&ctl
->tree_lock
);
2090 spin_lock(&ctl
->tree_lock
);
2093 __btrfs_remove_free_space_cache_locked(ctl
);
2094 spin_unlock(&ctl
->tree_lock
);
2098 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
2099 u64 offset
, u64 bytes
, u64 empty_size
)
2101 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2102 struct btrfs_free_space
*entry
= NULL
;
2103 u64 bytes_search
= bytes
+ empty_size
;
2106 spin_lock(&ctl
->tree_lock
);
2107 entry
= find_free_space(ctl
, &offset
, &bytes_search
);
2112 if (entry
->bitmap
) {
2113 bitmap_clear_bits(ctl
, entry
, offset
, bytes
);
2115 free_bitmap(ctl
, entry
);
2117 unlink_free_space(ctl
, entry
);
2118 entry
->offset
+= bytes
;
2119 entry
->bytes
-= bytes
;
2121 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2123 link_free_space(ctl
, entry
);
2127 spin_unlock(&ctl
->tree_lock
);
2133 * given a cluster, put all of its extents back into the free space
2134 * cache. If a block group is passed, this function will only free
2135 * a cluster that belongs to the passed block group.
2137 * Otherwise, it'll get a reference on the block group pointed to by the
2138 * cluster and remove the cluster from it.
2140 int btrfs_return_cluster_to_free_space(
2141 struct btrfs_block_group_cache
*block_group
,
2142 struct btrfs_free_cluster
*cluster
)
2144 struct btrfs_free_space_ctl
*ctl
;
2147 /* first, get a safe pointer to the block group */
2148 spin_lock(&cluster
->lock
);
2150 block_group
= cluster
->block_group
;
2152 spin_unlock(&cluster
->lock
);
2155 } else if (cluster
->block_group
!= block_group
) {
2156 /* someone else has already freed it don't redo their work */
2157 spin_unlock(&cluster
->lock
);
2160 atomic_inc(&block_group
->count
);
2161 spin_unlock(&cluster
->lock
);
2163 ctl
= block_group
->free_space_ctl
;
2165 /* now return any extents the cluster had on it */
2166 spin_lock(&ctl
->tree_lock
);
2167 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2168 spin_unlock(&ctl
->tree_lock
);
2170 /* finally drop our ref */
2171 btrfs_put_block_group(block_group
);
2175 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
2176 struct btrfs_free_cluster
*cluster
,
2177 struct btrfs_free_space
*entry
,
2178 u64 bytes
, u64 min_start
)
2180 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2182 u64 search_start
= cluster
->window_start
;
2183 u64 search_bytes
= bytes
;
2186 search_start
= min_start
;
2187 search_bytes
= bytes
;
2189 err
= search_bitmap(ctl
, entry
, &search_start
, &search_bytes
);
2194 __bitmap_clear_bits(ctl
, entry
, ret
, bytes
);
2200 * given a cluster, try to allocate 'bytes' from it, returns 0
2201 * if it couldn't find anything suitably large, or a logical disk offset
2202 * if things worked out
2204 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
2205 struct btrfs_free_cluster
*cluster
, u64 bytes
,
2208 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2209 struct btrfs_free_space
*entry
= NULL
;
2210 struct rb_node
*node
;
2213 spin_lock(&cluster
->lock
);
2214 if (bytes
> cluster
->max_size
)
2217 if (cluster
->block_group
!= block_group
)
2220 node
= rb_first(&cluster
->root
);
2224 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2226 if (entry
->bytes
< bytes
||
2227 (!entry
->bitmap
&& entry
->offset
< min_start
)) {
2228 node
= rb_next(&entry
->offset_index
);
2231 entry
= rb_entry(node
, struct btrfs_free_space
,
2236 if (entry
->bitmap
) {
2237 ret
= btrfs_alloc_from_bitmap(block_group
,
2238 cluster
, entry
, bytes
,
2241 node
= rb_next(&entry
->offset_index
);
2244 entry
= rb_entry(node
, struct btrfs_free_space
,
2249 ret
= entry
->offset
;
2251 entry
->offset
+= bytes
;
2252 entry
->bytes
-= bytes
;
2255 if (entry
->bytes
== 0)
2256 rb_erase(&entry
->offset_index
, &cluster
->root
);
2260 spin_unlock(&cluster
->lock
);
2265 spin_lock(&ctl
->tree_lock
);
2267 ctl
->free_space
-= bytes
;
2268 if (entry
->bytes
== 0) {
2269 ctl
->free_extents
--;
2270 if (entry
->bitmap
) {
2271 kfree(entry
->bitmap
);
2272 ctl
->total_bitmaps
--;
2273 ctl
->op
->recalc_thresholds(ctl
);
2275 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2278 spin_unlock(&ctl
->tree_lock
);
2283 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
2284 struct btrfs_free_space
*entry
,
2285 struct btrfs_free_cluster
*cluster
,
2286 u64 offset
, u64 bytes
, u64 min_bytes
)
2288 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2289 unsigned long next_zero
;
2291 unsigned long search_bits
;
2292 unsigned long total_bits
;
2293 unsigned long found_bits
;
2294 unsigned long start
= 0;
2295 unsigned long total_found
= 0;
2299 i
= offset_to_bit(entry
->offset
, block_group
->sectorsize
,
2300 max_t(u64
, offset
, entry
->offset
));
2301 search_bits
= bytes_to_bits(bytes
, block_group
->sectorsize
);
2302 total_bits
= bytes_to_bits(min_bytes
, block_group
->sectorsize
);
2306 for (i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
);
2307 i
< BITS_PER_BITMAP
;
2308 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
2309 next_zero
= find_next_zero_bit(entry
->bitmap
,
2310 BITS_PER_BITMAP
, i
);
2311 if (next_zero
- i
>= search_bits
) {
2312 found_bits
= next_zero
- i
;
2323 cluster
->max_size
= 0;
2327 total_found
+= found_bits
;
2329 if (cluster
->max_size
< found_bits
* block_group
->sectorsize
)
2330 cluster
->max_size
= found_bits
* block_group
->sectorsize
;
2332 if (total_found
< total_bits
) {
2333 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, next_zero
);
2334 if (i
- start
> total_bits
* 2) {
2336 cluster
->max_size
= 0;
2342 cluster
->window_start
= start
* block_group
->sectorsize
+
2344 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2345 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2346 &entry
->offset_index
, 1);
2353 * This searches the block group for just extents to fill the cluster with.
2356 setup_cluster_no_bitmap(struct btrfs_block_group_cache
*block_group
,
2357 struct btrfs_free_cluster
*cluster
,
2358 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2361 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2362 struct btrfs_free_space
*first
= NULL
;
2363 struct btrfs_free_space
*entry
= NULL
;
2364 struct btrfs_free_space
*prev
= NULL
;
2365 struct btrfs_free_space
*last
;
2366 struct rb_node
*node
;
2370 u64 max_gap
= 128 * 1024;
2372 entry
= tree_search_offset(ctl
, offset
, 0, 1);
2377 * We don't want bitmaps, so just move along until we find a normal
2380 while (entry
->bitmap
) {
2381 if (list_empty(&entry
->list
))
2382 list_add_tail(&entry
->list
, bitmaps
);
2383 node
= rb_next(&entry
->offset_index
);
2386 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2389 window_start
= entry
->offset
;
2390 window_free
= entry
->bytes
;
2391 max_extent
= entry
->bytes
;
2396 while (window_free
<= min_bytes
) {
2397 node
= rb_next(&entry
->offset_index
);
2400 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2402 if (entry
->bitmap
) {
2403 if (list_empty(&entry
->list
))
2404 list_add_tail(&entry
->list
, bitmaps
);
2409 * we haven't filled the empty size and the window is
2410 * very large. reset and try again
2412 if (entry
->offset
- (prev
->offset
+ prev
->bytes
) > max_gap
||
2413 entry
->offset
- window_start
> (min_bytes
* 2)) {
2415 window_start
= entry
->offset
;
2416 window_free
= entry
->bytes
;
2418 max_extent
= entry
->bytes
;
2421 window_free
+= entry
->bytes
;
2422 if (entry
->bytes
> max_extent
)
2423 max_extent
= entry
->bytes
;
2428 cluster
->window_start
= first
->offset
;
2430 node
= &first
->offset_index
;
2433 * now we've found our entries, pull them out of the free space
2434 * cache and put them into the cluster rbtree
2439 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2440 node
= rb_next(&entry
->offset_index
);
2444 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2445 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2446 &entry
->offset_index
, 0);
2448 } while (node
&& entry
!= last
);
2450 cluster
->max_size
= max_extent
;
2456 * This specifically looks for bitmaps that may work in the cluster, we assume
2457 * that we have already failed to find extents that will work.
2460 setup_cluster_bitmap(struct btrfs_block_group_cache
*block_group
,
2461 struct btrfs_free_cluster
*cluster
,
2462 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2465 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2466 struct btrfs_free_space
*entry
;
2468 u64 bitmap_offset
= offset_to_bitmap(ctl
, offset
);
2470 if (ctl
->total_bitmaps
== 0)
2474 * The bitmap that covers offset won't be in the list unless offset
2475 * is just its start offset.
2477 entry
= list_first_entry(bitmaps
, struct btrfs_free_space
, list
);
2478 if (entry
->offset
!= bitmap_offset
) {
2479 entry
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2480 if (entry
&& list_empty(&entry
->list
))
2481 list_add(&entry
->list
, bitmaps
);
2484 list_for_each_entry(entry
, bitmaps
, list
) {
2485 if (entry
->bytes
< min_bytes
)
2487 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
, offset
,
2494 * The bitmaps list has all the bitmaps that record free space
2495 * starting after offset, so no more search is required.
2501 * here we try to find a cluster of blocks in a block group. The goal
2502 * is to find at least bytes free and up to empty_size + bytes free.
2503 * We might not find them all in one contiguous area.
2505 * returns zero and sets up cluster if things worked out, otherwise
2506 * it returns -enospc
2508 int btrfs_find_space_cluster(struct btrfs_trans_handle
*trans
,
2509 struct btrfs_root
*root
,
2510 struct btrfs_block_group_cache
*block_group
,
2511 struct btrfs_free_cluster
*cluster
,
2512 u64 offset
, u64 bytes
, u64 empty_size
)
2514 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2515 struct btrfs_free_space
*entry
, *tmp
;
2520 /* for metadata, allow allocates with more holes */
2521 if (btrfs_test_opt(root
, SSD_SPREAD
)) {
2522 min_bytes
= bytes
+ empty_size
;
2523 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2525 * we want to do larger allocations when we are
2526 * flushing out the delayed refs, it helps prevent
2527 * making more work as we go along.
2529 if (trans
->transaction
->delayed_refs
.flushing
)
2530 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 1);
2532 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 4);
2534 min_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
2536 spin_lock(&ctl
->tree_lock
);
2539 * If we know we don't have enough space to make a cluster don't even
2540 * bother doing all the work to try and find one.
2542 if (ctl
->free_space
< min_bytes
) {
2543 spin_unlock(&ctl
->tree_lock
);
2547 spin_lock(&cluster
->lock
);
2549 /* someone already found a cluster, hooray */
2550 if (cluster
->block_group
) {
2555 ret
= setup_cluster_no_bitmap(block_group
, cluster
, &bitmaps
, offset
,
2558 ret
= setup_cluster_bitmap(block_group
, cluster
, &bitmaps
,
2559 offset
, bytes
, min_bytes
);
2561 /* Clear our temporary list */
2562 list_for_each_entry_safe(entry
, tmp
, &bitmaps
, list
)
2563 list_del_init(&entry
->list
);
2566 atomic_inc(&block_group
->count
);
2567 list_add_tail(&cluster
->block_group_list
,
2568 &block_group
->cluster_list
);
2569 cluster
->block_group
= block_group
;
2572 spin_unlock(&cluster
->lock
);
2573 spin_unlock(&ctl
->tree_lock
);
2579 * simple code to zero out a cluster
2581 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
2583 spin_lock_init(&cluster
->lock
);
2584 spin_lock_init(&cluster
->refill_lock
);
2585 cluster
->root
= RB_ROOT
;
2586 cluster
->max_size
= 0;
2587 INIT_LIST_HEAD(&cluster
->block_group_list
);
2588 cluster
->block_group
= NULL
;
2591 int btrfs_trim_block_group(struct btrfs_block_group_cache
*block_group
,
2592 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
)
2594 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2595 struct btrfs_free_space
*entry
= NULL
;
2596 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2598 u64 actually_trimmed
;
2603 while (start
< end
) {
2604 spin_lock(&ctl
->tree_lock
);
2606 if (ctl
->free_space
< minlen
) {
2607 spin_unlock(&ctl
->tree_lock
);
2611 entry
= tree_search_offset(ctl
, start
, 0, 1);
2613 entry
= tree_search_offset(ctl
,
2614 offset_to_bitmap(ctl
, start
),
2617 if (!entry
|| entry
->offset
>= end
) {
2618 spin_unlock(&ctl
->tree_lock
);
2622 if (entry
->bitmap
) {
2623 ret
= search_bitmap(ctl
, entry
, &start
, &bytes
);
2626 spin_unlock(&ctl
->tree_lock
);
2629 bytes
= min(bytes
, end
- start
);
2630 bitmap_clear_bits(ctl
, entry
, start
, bytes
);
2631 if (entry
->bytes
== 0)
2632 free_bitmap(ctl
, entry
);
2634 start
= entry
->offset
+ BITS_PER_BITMAP
*
2635 block_group
->sectorsize
;
2636 spin_unlock(&ctl
->tree_lock
);
2641 start
= entry
->offset
;
2642 bytes
= min(entry
->bytes
, end
- start
);
2643 unlink_free_space(ctl
, entry
);
2644 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2647 spin_unlock(&ctl
->tree_lock
);
2649 if (bytes
>= minlen
) {
2650 struct btrfs_space_info
*space_info
;
2653 space_info
= block_group
->space_info
;
2654 spin_lock(&space_info
->lock
);
2655 spin_lock(&block_group
->lock
);
2656 if (!block_group
->ro
) {
2657 block_group
->reserved
+= bytes
;
2658 space_info
->bytes_reserved
+= bytes
;
2661 spin_unlock(&block_group
->lock
);
2662 spin_unlock(&space_info
->lock
);
2664 ret
= btrfs_error_discard_extent(fs_info
->extent_root
,
2669 btrfs_add_free_space(block_group
, start
, bytes
);
2671 spin_lock(&space_info
->lock
);
2672 spin_lock(&block_group
->lock
);
2673 if (block_group
->ro
)
2674 space_info
->bytes_readonly
+= bytes
;
2675 block_group
->reserved
-= bytes
;
2676 space_info
->bytes_reserved
-= bytes
;
2677 spin_unlock(&space_info
->lock
);
2678 spin_unlock(&block_group
->lock
);
2683 *trimmed
+= actually_trimmed
;
2688 if (fatal_signal_pending(current
)) {
2700 * Find the left-most item in the cache tree, and then return the
2701 * smallest inode number in the item.
2703 * Note: the returned inode number may not be the smallest one in
2704 * the tree, if the left-most item is a bitmap.
2706 u64
btrfs_find_ino_for_alloc(struct btrfs_root
*fs_root
)
2708 struct btrfs_free_space_ctl
*ctl
= fs_root
->free_ino_ctl
;
2709 struct btrfs_free_space
*entry
= NULL
;
2712 spin_lock(&ctl
->tree_lock
);
2714 if (RB_EMPTY_ROOT(&ctl
->free_space_offset
))
2717 entry
= rb_entry(rb_first(&ctl
->free_space_offset
),
2718 struct btrfs_free_space
, offset_index
);
2720 if (!entry
->bitmap
) {
2721 ino
= entry
->offset
;
2723 unlink_free_space(ctl
, entry
);
2727 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2729 link_free_space(ctl
, entry
);
2735 ret
= search_bitmap(ctl
, entry
, &offset
, &count
);
2739 bitmap_clear_bits(ctl
, entry
, offset
, 1);
2740 if (entry
->bytes
== 0)
2741 free_bitmap(ctl
, entry
);
2744 spin_unlock(&ctl
->tree_lock
);
2749 struct inode
*lookup_free_ino_inode(struct btrfs_root
*root
,
2750 struct btrfs_path
*path
)
2752 struct inode
*inode
= NULL
;
2754 spin_lock(&root
->cache_lock
);
2755 if (root
->cache_inode
)
2756 inode
= igrab(root
->cache_inode
);
2757 spin_unlock(&root
->cache_lock
);
2761 inode
= __lookup_free_space_inode(root
, path
, 0);
2765 spin_lock(&root
->cache_lock
);
2766 if (!btrfs_fs_closing(root
->fs_info
))
2767 root
->cache_inode
= igrab(inode
);
2768 spin_unlock(&root
->cache_lock
);
2773 int create_free_ino_inode(struct btrfs_root
*root
,
2774 struct btrfs_trans_handle
*trans
,
2775 struct btrfs_path
*path
)
2777 return __create_free_space_inode(root
, trans
, path
,
2778 BTRFS_FREE_INO_OBJECTID
, 0);
2781 int load_free_ino_cache(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
2783 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
2784 struct btrfs_path
*path
;
2785 struct inode
*inode
;
2787 u64 root_gen
= btrfs_root_generation(&root
->root_item
);
2789 if (!btrfs_test_opt(root
, INODE_MAP_CACHE
))
2793 * If we're unmounting then just return, since this does a search on the
2794 * normal root and not the commit root and we could deadlock.
2796 if (btrfs_fs_closing(fs_info
))
2799 path
= btrfs_alloc_path();
2803 inode
= lookup_free_ino_inode(root
, path
);
2807 if (root_gen
!= BTRFS_I(inode
)->generation
)
2810 ret
= __load_free_space_cache(root
, inode
, ctl
, path
, 0);
2813 printk(KERN_ERR
"btrfs: failed to load free ino cache for "
2814 "root %llu\n", root
->root_key
.objectid
);
2818 btrfs_free_path(path
);
2822 int btrfs_write_out_ino_cache(struct btrfs_root
*root
,
2823 struct btrfs_trans_handle
*trans
,
2824 struct btrfs_path
*path
)
2826 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
2827 struct inode
*inode
;
2830 if (!btrfs_test_opt(root
, INODE_MAP_CACHE
))
2833 inode
= lookup_free_ino_inode(root
, path
);
2837 ret
= __btrfs_write_out_cache(root
, inode
, ctl
, NULL
, trans
, path
, 0);
2839 btrfs_delalloc_release_metadata(inode
, inode
->i_size
);
2841 printk(KERN_ERR
"btrfs: failed to write free ino cache "
2842 "for root %llu\n", root
->root_key
.objectid
);