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 if (io_ctl
->pages
[i
]) {
323 ClearPageChecked(io_ctl
->pages
[i
]);
324 unlock_page(io_ctl
->pages
[i
]);
325 page_cache_release(io_ctl
->pages
[i
]);
330 static int io_ctl_prepare_pages(struct io_ctl
*io_ctl
, struct inode
*inode
,
334 gfp_t mask
= btrfs_alloc_write_mask(inode
->i_mapping
);
337 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
338 page
= find_or_create_page(inode
->i_mapping
, i
, mask
);
340 io_ctl_drop_pages(io_ctl
);
343 io_ctl
->pages
[i
] = page
;
344 if (uptodate
&& !PageUptodate(page
)) {
345 btrfs_readpage(NULL
, page
);
347 if (!PageUptodate(page
)) {
348 printk(KERN_ERR
"btrfs: error reading free "
350 io_ctl_drop_pages(io_ctl
);
356 for (i
= 0; i
< io_ctl
->num_pages
; i
++) {
357 clear_page_dirty_for_io(io_ctl
->pages
[i
]);
358 set_page_extent_mapped(io_ctl
->pages
[i
]);
364 static void io_ctl_set_generation(struct io_ctl
*io_ctl
, u64 generation
)
368 io_ctl_map_page(io_ctl
, 1);
371 * Skip the csum areas. If we don't check crcs then we just have a
372 * 64bit chunk at the front of the first page.
374 if (io_ctl
->check_crcs
) {
375 io_ctl
->cur
+= (sizeof(u32
) * io_ctl
->num_pages
);
376 io_ctl
->size
-= sizeof(u64
) + (sizeof(u32
) * io_ctl
->num_pages
);
378 io_ctl
->cur
+= sizeof(u64
);
379 io_ctl
->size
-= sizeof(u64
) * 2;
383 *val
= cpu_to_le64(generation
);
384 io_ctl
->cur
+= sizeof(u64
);
387 static int io_ctl_check_generation(struct io_ctl
*io_ctl
, u64 generation
)
392 * Skip the crc area. If we don't check crcs then we just have a 64bit
393 * chunk at the front of the first page.
395 if (io_ctl
->check_crcs
) {
396 io_ctl
->cur
+= sizeof(u32
) * io_ctl
->num_pages
;
397 io_ctl
->size
-= sizeof(u64
) +
398 (sizeof(u32
) * io_ctl
->num_pages
);
400 io_ctl
->cur
+= sizeof(u64
);
401 io_ctl
->size
-= sizeof(u64
) * 2;
405 if (le64_to_cpu(*gen
) != generation
) {
406 printk_ratelimited(KERN_ERR
"btrfs: space cache generation "
407 "(%Lu) does not match inode (%Lu)\n", *gen
,
409 io_ctl_unmap_page(io_ctl
);
412 io_ctl
->cur
+= sizeof(u64
);
416 static void io_ctl_set_crc(struct io_ctl
*io_ctl
, int index
)
422 if (!io_ctl
->check_crcs
) {
423 io_ctl_unmap_page(io_ctl
);
428 offset
= sizeof(u32
) * io_ctl
->num_pages
;
430 crc
= btrfs_csum_data(io_ctl
->root
, io_ctl
->orig
+ offset
, crc
,
431 PAGE_CACHE_SIZE
- offset
);
432 btrfs_csum_final(crc
, (char *)&crc
);
433 io_ctl_unmap_page(io_ctl
);
434 tmp
= kmap(io_ctl
->pages
[0]);
437 kunmap(io_ctl
->pages
[0]);
440 static int io_ctl_check_crc(struct io_ctl
*io_ctl
, int index
)
446 if (!io_ctl
->check_crcs
) {
447 io_ctl_map_page(io_ctl
, 0);
452 offset
= sizeof(u32
) * io_ctl
->num_pages
;
454 tmp
= kmap(io_ctl
->pages
[0]);
457 kunmap(io_ctl
->pages
[0]);
459 io_ctl_map_page(io_ctl
, 0);
460 crc
= btrfs_csum_data(io_ctl
->root
, io_ctl
->orig
+ offset
, crc
,
461 PAGE_CACHE_SIZE
- offset
);
462 btrfs_csum_final(crc
, (char *)&crc
);
464 printk_ratelimited(KERN_ERR
"btrfs: csum mismatch on free "
466 io_ctl_unmap_page(io_ctl
);
473 static int io_ctl_add_entry(struct io_ctl
*io_ctl
, u64 offset
, u64 bytes
,
476 struct btrfs_free_space_entry
*entry
;
482 entry
->offset
= cpu_to_le64(offset
);
483 entry
->bytes
= cpu_to_le64(bytes
);
484 entry
->type
= (bitmap
) ? BTRFS_FREE_SPACE_BITMAP
:
485 BTRFS_FREE_SPACE_EXTENT
;
486 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
487 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
489 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
492 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
494 /* No more pages to map */
495 if (io_ctl
->index
>= io_ctl
->num_pages
)
498 /* map the next page */
499 io_ctl_map_page(io_ctl
, 1);
503 static int io_ctl_add_bitmap(struct io_ctl
*io_ctl
, void *bitmap
)
509 * If we aren't at the start of the current page, unmap this one and
510 * map the next one if there is any left.
512 if (io_ctl
->cur
!= io_ctl
->orig
) {
513 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
514 if (io_ctl
->index
>= io_ctl
->num_pages
)
516 io_ctl_map_page(io_ctl
, 0);
519 memcpy(io_ctl
->cur
, bitmap
, PAGE_CACHE_SIZE
);
520 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
521 if (io_ctl
->index
< io_ctl
->num_pages
)
522 io_ctl_map_page(io_ctl
, 0);
526 static void io_ctl_zero_remaining_pages(struct io_ctl
*io_ctl
)
529 * If we're not on the boundary we know we've modified the page and we
530 * need to crc the page.
532 if (io_ctl
->cur
!= io_ctl
->orig
)
533 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
535 io_ctl_unmap_page(io_ctl
);
537 while (io_ctl
->index
< io_ctl
->num_pages
) {
538 io_ctl_map_page(io_ctl
, 1);
539 io_ctl_set_crc(io_ctl
, io_ctl
->index
- 1);
543 static int io_ctl_read_entry(struct io_ctl
*io_ctl
,
544 struct btrfs_free_space
*entry
, u8
*type
)
546 struct btrfs_free_space_entry
*e
;
550 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
556 entry
->offset
= le64_to_cpu(e
->offset
);
557 entry
->bytes
= le64_to_cpu(e
->bytes
);
559 io_ctl
->cur
+= sizeof(struct btrfs_free_space_entry
);
560 io_ctl
->size
-= sizeof(struct btrfs_free_space_entry
);
562 if (io_ctl
->size
>= sizeof(struct btrfs_free_space_entry
))
565 io_ctl_unmap_page(io_ctl
);
570 static int io_ctl_read_bitmap(struct io_ctl
*io_ctl
,
571 struct btrfs_free_space
*entry
)
575 ret
= io_ctl_check_crc(io_ctl
, io_ctl
->index
);
579 memcpy(entry
->bitmap
, io_ctl
->cur
, PAGE_CACHE_SIZE
);
580 io_ctl_unmap_page(io_ctl
);
585 int __load_free_space_cache(struct btrfs_root
*root
, struct inode
*inode
,
586 struct btrfs_free_space_ctl
*ctl
,
587 struct btrfs_path
*path
, u64 offset
)
589 struct btrfs_free_space_header
*header
;
590 struct extent_buffer
*leaf
;
591 struct io_ctl io_ctl
;
592 struct btrfs_key key
;
593 struct btrfs_free_space
*e
, *n
;
594 struct list_head bitmaps
;
601 INIT_LIST_HEAD(&bitmaps
);
603 /* Nothing in the space cache, goodbye */
604 if (!i_size_read(inode
))
607 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
611 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
615 btrfs_release_path(path
);
621 leaf
= path
->nodes
[0];
622 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
623 struct btrfs_free_space_header
);
624 num_entries
= btrfs_free_space_entries(leaf
, header
);
625 num_bitmaps
= btrfs_free_space_bitmaps(leaf
, header
);
626 generation
= btrfs_free_space_generation(leaf
, header
);
627 btrfs_release_path(path
);
629 if (BTRFS_I(inode
)->generation
!= generation
) {
630 printk(KERN_ERR
"btrfs: free space inode generation (%llu) did"
631 " not match free space cache generation (%llu)\n",
632 (unsigned long long)BTRFS_I(inode
)->generation
,
633 (unsigned long long)generation
);
640 ret
= io_ctl_init(&io_ctl
, inode
, root
);
644 ret
= readahead_cache(inode
);
648 ret
= io_ctl_prepare_pages(&io_ctl
, inode
, 1);
652 ret
= io_ctl_check_crc(&io_ctl
, 0);
656 ret
= io_ctl_check_generation(&io_ctl
, generation
);
660 while (num_entries
) {
661 e
= kmem_cache_zalloc(btrfs_free_space_cachep
,
666 ret
= io_ctl_read_entry(&io_ctl
, e
, &type
);
668 kmem_cache_free(btrfs_free_space_cachep
, e
);
673 kmem_cache_free(btrfs_free_space_cachep
, e
);
677 if (type
== BTRFS_FREE_SPACE_EXTENT
) {
678 spin_lock(&ctl
->tree_lock
);
679 ret
= link_free_space(ctl
, e
);
680 spin_unlock(&ctl
->tree_lock
);
682 printk(KERN_ERR
"Duplicate entries in "
683 "free space cache, dumping\n");
684 kmem_cache_free(btrfs_free_space_cachep
, e
);
688 BUG_ON(!num_bitmaps
);
690 e
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
693 btrfs_free_space_cachep
, e
);
696 spin_lock(&ctl
->tree_lock
);
697 ret
= link_free_space(ctl
, e
);
698 ctl
->total_bitmaps
++;
699 ctl
->op
->recalc_thresholds(ctl
);
700 spin_unlock(&ctl
->tree_lock
);
702 printk(KERN_ERR
"Duplicate entries in "
703 "free space cache, dumping\n");
704 kmem_cache_free(btrfs_free_space_cachep
, e
);
707 list_add_tail(&e
->list
, &bitmaps
);
713 io_ctl_unmap_page(&io_ctl
);
716 * We add the bitmaps at the end of the entries in order that
717 * the bitmap entries are added to the cache.
719 list_for_each_entry_safe(e
, n
, &bitmaps
, list
) {
720 list_del_init(&e
->list
);
721 ret
= io_ctl_read_bitmap(&io_ctl
, e
);
726 io_ctl_drop_pages(&io_ctl
);
729 io_ctl_free(&io_ctl
);
732 io_ctl_drop_pages(&io_ctl
);
733 __btrfs_remove_free_space_cache(ctl
);
737 int load_free_space_cache(struct btrfs_fs_info
*fs_info
,
738 struct btrfs_block_group_cache
*block_group
)
740 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
741 struct btrfs_root
*root
= fs_info
->tree_root
;
743 struct btrfs_path
*path
;
746 u64 used
= btrfs_block_group_used(&block_group
->item
);
749 * If we're unmounting then just return, since this does a search on the
750 * normal root and not the commit root and we could deadlock.
752 if (btrfs_fs_closing(fs_info
))
756 * If this block group has been marked to be cleared for one reason or
757 * another then we can't trust the on disk cache, so just return.
759 spin_lock(&block_group
->lock
);
760 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
761 spin_unlock(&block_group
->lock
);
764 spin_unlock(&block_group
->lock
);
766 path
= btrfs_alloc_path();
770 inode
= lookup_free_space_inode(root
, block_group
, path
);
772 btrfs_free_path(path
);
776 /* We may have converted the inode and made the cache invalid. */
777 spin_lock(&block_group
->lock
);
778 if (block_group
->disk_cache_state
!= BTRFS_DC_WRITTEN
) {
779 spin_unlock(&block_group
->lock
);
780 btrfs_free_path(path
);
783 spin_unlock(&block_group
->lock
);
785 ret
= __load_free_space_cache(fs_info
->tree_root
, inode
, ctl
,
786 path
, block_group
->key
.objectid
);
787 btrfs_free_path(path
);
791 spin_lock(&ctl
->tree_lock
);
792 matched
= (ctl
->free_space
== (block_group
->key
.offset
- used
-
793 block_group
->bytes_super
));
794 spin_unlock(&ctl
->tree_lock
);
797 __btrfs_remove_free_space_cache(ctl
);
798 printk(KERN_ERR
"block group %llu has an wrong amount of free "
799 "space\n", block_group
->key
.objectid
);
804 /* This cache is bogus, make sure it gets cleared */
805 spin_lock(&block_group
->lock
);
806 block_group
->disk_cache_state
= BTRFS_DC_CLEAR
;
807 spin_unlock(&block_group
->lock
);
810 printk(KERN_ERR
"btrfs: failed to load free space cache "
811 "for block group %llu\n", block_group
->key
.objectid
);
819 * __btrfs_write_out_cache - write out cached info to an inode
820 * @root - the root the inode belongs to
821 * @ctl - the free space cache we are going to write out
822 * @block_group - the block_group for this cache if it belongs to a block_group
823 * @trans - the trans handle
824 * @path - the path to use
825 * @offset - the offset for the key we'll insert
827 * This function writes out a free space cache struct to disk for quick recovery
828 * on mount. This will return 0 if it was successfull in writing the cache out,
829 * and -1 if it was not.
831 int __btrfs_write_out_cache(struct btrfs_root
*root
, struct inode
*inode
,
832 struct btrfs_free_space_ctl
*ctl
,
833 struct btrfs_block_group_cache
*block_group
,
834 struct btrfs_trans_handle
*trans
,
835 struct btrfs_path
*path
, u64 offset
)
837 struct btrfs_free_space_header
*header
;
838 struct extent_buffer
*leaf
;
839 struct rb_node
*node
;
840 struct list_head
*pos
, *n
;
841 struct extent_state
*cached_state
= NULL
;
842 struct btrfs_free_cluster
*cluster
= NULL
;
843 struct extent_io_tree
*unpin
= NULL
;
844 struct io_ctl io_ctl
;
845 struct list_head bitmap_list
;
846 struct btrfs_key key
;
847 u64 start
, extent_start
, extent_end
, len
;
853 INIT_LIST_HEAD(&bitmap_list
);
855 if (!i_size_read(inode
))
858 ret
= io_ctl_init(&io_ctl
, inode
, root
);
862 /* Get the cluster for this block_group if it exists */
863 if (block_group
&& !list_empty(&block_group
->cluster_list
))
864 cluster
= list_entry(block_group
->cluster_list
.next
,
865 struct btrfs_free_cluster
,
868 /* Lock all pages first so we can lock the extent safely. */
869 io_ctl_prepare_pages(&io_ctl
, inode
, 0);
871 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, 0, i_size_read(inode
) - 1,
872 0, &cached_state
, GFP_NOFS
);
874 node
= rb_first(&ctl
->free_space_offset
);
875 if (!node
&& cluster
) {
876 node
= rb_first(&cluster
->root
);
880 /* Make sure we can fit our crcs into the first page */
881 if (io_ctl
.check_crcs
&&
882 (io_ctl
.num_pages
* sizeof(u32
)) >= PAGE_CACHE_SIZE
) {
887 io_ctl_set_generation(&io_ctl
, trans
->transid
);
889 /* Write out the extent entries */
891 struct btrfs_free_space
*e
;
893 e
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
896 ret
= io_ctl_add_entry(&io_ctl
, e
->offset
, e
->bytes
,
902 list_add_tail(&e
->list
, &bitmap_list
);
905 node
= rb_next(node
);
906 if (!node
&& cluster
) {
907 node
= rb_first(&cluster
->root
);
913 * We want to add any pinned extents to our free space cache
914 * so we don't leak the space
918 * We shouldn't have switched the pinned extents yet so this is the
921 unpin
= root
->fs_info
->pinned_extents
;
924 start
= block_group
->key
.objectid
;
926 while (block_group
&& (start
< block_group
->key
.objectid
+
927 block_group
->key
.offset
)) {
928 ret
= find_first_extent_bit(unpin
, start
,
929 &extent_start
, &extent_end
,
936 /* This pinned extent is out of our range */
937 if (extent_start
>= block_group
->key
.objectid
+
938 block_group
->key
.offset
)
941 extent_start
= max(extent_start
, start
);
942 extent_end
= min(block_group
->key
.objectid
+
943 block_group
->key
.offset
, extent_end
+ 1);
944 len
= extent_end
- extent_start
;
947 ret
= io_ctl_add_entry(&io_ctl
, extent_start
, len
, NULL
);
954 /* Write out the bitmaps */
955 list_for_each_safe(pos
, n
, &bitmap_list
) {
956 struct btrfs_free_space
*entry
=
957 list_entry(pos
, struct btrfs_free_space
, list
);
959 ret
= io_ctl_add_bitmap(&io_ctl
, entry
->bitmap
);
962 list_del_init(&entry
->list
);
965 /* Zero out the rest of the pages just to make sure */
966 io_ctl_zero_remaining_pages(&io_ctl
);
968 ret
= btrfs_dirty_pages(root
, inode
, io_ctl
.pages
, io_ctl
.num_pages
,
969 0, i_size_read(inode
), &cached_state
);
970 io_ctl_drop_pages(&io_ctl
);
971 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
972 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
978 ret
= filemap_write_and_wait(inode
->i_mapping
);
982 key
.objectid
= BTRFS_FREE_SPACE_OBJECTID
;
986 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
988 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0, inode
->i_size
- 1,
989 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, NULL
,
993 leaf
= path
->nodes
[0];
995 struct btrfs_key found_key
;
996 BUG_ON(!path
->slots
[0]);
998 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
999 if (found_key
.objectid
!= BTRFS_FREE_SPACE_OBJECTID
||
1000 found_key
.offset
!= offset
) {
1001 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, 0,
1003 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0,
1005 btrfs_release_path(path
);
1010 BTRFS_I(inode
)->generation
= trans
->transid
;
1011 header
= btrfs_item_ptr(leaf
, path
->slots
[0],
1012 struct btrfs_free_space_header
);
1013 btrfs_set_free_space_entries(leaf
, header
, entries
);
1014 btrfs_set_free_space_bitmaps(leaf
, header
, bitmaps
);
1015 btrfs_set_free_space_generation(leaf
, header
, trans
->transid
);
1016 btrfs_mark_buffer_dirty(leaf
);
1017 btrfs_release_path(path
);
1021 io_ctl_free(&io_ctl
);
1023 invalidate_inode_pages2(inode
->i_mapping
);
1024 BTRFS_I(inode
)->generation
= 0;
1026 btrfs_update_inode(trans
, root
, inode
);
1030 list_for_each_safe(pos
, n
, &bitmap_list
) {
1031 struct btrfs_free_space
*entry
=
1032 list_entry(pos
, struct btrfs_free_space
, list
);
1033 list_del_init(&entry
->list
);
1035 io_ctl_drop_pages(&io_ctl
);
1036 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, 0,
1037 i_size_read(inode
) - 1, &cached_state
, GFP_NOFS
);
1041 int btrfs_write_out_cache(struct btrfs_root
*root
,
1042 struct btrfs_trans_handle
*trans
,
1043 struct btrfs_block_group_cache
*block_group
,
1044 struct btrfs_path
*path
)
1046 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1047 struct inode
*inode
;
1050 root
= root
->fs_info
->tree_root
;
1052 spin_lock(&block_group
->lock
);
1053 if (block_group
->disk_cache_state
< BTRFS_DC_SETUP
) {
1054 spin_unlock(&block_group
->lock
);
1057 spin_unlock(&block_group
->lock
);
1059 inode
= lookup_free_space_inode(root
, block_group
, path
);
1063 ret
= __btrfs_write_out_cache(root
, inode
, ctl
, block_group
, trans
,
1064 path
, block_group
->key
.objectid
);
1066 spin_lock(&block_group
->lock
);
1067 block_group
->disk_cache_state
= BTRFS_DC_ERROR
;
1068 spin_unlock(&block_group
->lock
);
1071 printk(KERN_ERR
"btrfs: failed to write free space cace "
1072 "for block group %llu\n", block_group
->key
.objectid
);
1080 static inline unsigned long offset_to_bit(u64 bitmap_start
, u32 unit
,
1083 BUG_ON(offset
< bitmap_start
);
1084 offset
-= bitmap_start
;
1085 return (unsigned long)(div_u64(offset
, unit
));
1088 static inline unsigned long bytes_to_bits(u64 bytes
, u32 unit
)
1090 return (unsigned long)(div_u64(bytes
, unit
));
1093 static inline u64
offset_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1097 u64 bytes_per_bitmap
;
1099 bytes_per_bitmap
= BITS_PER_BITMAP
* ctl
->unit
;
1100 bitmap_start
= offset
- ctl
->start
;
1101 bitmap_start
= div64_u64(bitmap_start
, bytes_per_bitmap
);
1102 bitmap_start
*= bytes_per_bitmap
;
1103 bitmap_start
+= ctl
->start
;
1105 return bitmap_start
;
1108 static int tree_insert_offset(struct rb_root
*root
, u64 offset
,
1109 struct rb_node
*node
, int bitmap
)
1111 struct rb_node
**p
= &root
->rb_node
;
1112 struct rb_node
*parent
= NULL
;
1113 struct btrfs_free_space
*info
;
1117 info
= rb_entry(parent
, struct btrfs_free_space
, offset_index
);
1119 if (offset
< info
->offset
) {
1121 } else if (offset
> info
->offset
) {
1122 p
= &(*p
)->rb_right
;
1125 * we could have a bitmap entry and an extent entry
1126 * share the same offset. If this is the case, we want
1127 * the extent entry to always be found first if we do a
1128 * linear search through the tree, since we want to have
1129 * the quickest allocation time, and allocating from an
1130 * extent is faster than allocating from a bitmap. So
1131 * if we're inserting a bitmap and we find an entry at
1132 * this offset, we want to go right, or after this entry
1133 * logically. If we are inserting an extent and we've
1134 * found a bitmap, we want to go left, or before
1142 p
= &(*p
)->rb_right
;
1144 if (!info
->bitmap
) {
1153 rb_link_node(node
, parent
, p
);
1154 rb_insert_color(node
, root
);
1160 * searches the tree for the given offset.
1162 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1163 * want a section that has at least bytes size and comes at or after the given
1166 static struct btrfs_free_space
*
1167 tree_search_offset(struct btrfs_free_space_ctl
*ctl
,
1168 u64 offset
, int bitmap_only
, int fuzzy
)
1170 struct rb_node
*n
= ctl
->free_space_offset
.rb_node
;
1171 struct btrfs_free_space
*entry
, *prev
= NULL
;
1173 /* find entry that is closest to the 'offset' */
1180 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1183 if (offset
< entry
->offset
)
1185 else if (offset
> entry
->offset
)
1198 * bitmap entry and extent entry may share same offset,
1199 * in that case, bitmap entry comes after extent entry.
1204 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1205 if (entry
->offset
!= offset
)
1208 WARN_ON(!entry
->bitmap
);
1211 if (entry
->bitmap
) {
1213 * if previous extent entry covers the offset,
1214 * we should return it instead of the bitmap entry
1216 n
= &entry
->offset_index
;
1221 prev
= rb_entry(n
, struct btrfs_free_space
,
1223 if (!prev
->bitmap
) {
1224 if (prev
->offset
+ prev
->bytes
> offset
)
1236 /* find last entry before the 'offset' */
1238 if (entry
->offset
> offset
) {
1239 n
= rb_prev(&entry
->offset_index
);
1241 entry
= rb_entry(n
, struct btrfs_free_space
,
1243 BUG_ON(entry
->offset
> offset
);
1252 if (entry
->bitmap
) {
1253 n
= &entry
->offset_index
;
1258 prev
= rb_entry(n
, struct btrfs_free_space
,
1260 if (!prev
->bitmap
) {
1261 if (prev
->offset
+ prev
->bytes
> offset
)
1266 if (entry
->offset
+ BITS_PER_BITMAP
* ctl
->unit
> offset
)
1268 } else if (entry
->offset
+ entry
->bytes
> offset
)
1275 if (entry
->bitmap
) {
1276 if (entry
->offset
+ BITS_PER_BITMAP
*
1280 if (entry
->offset
+ entry
->bytes
> offset
)
1284 n
= rb_next(&entry
->offset_index
);
1287 entry
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1293 __unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1294 struct btrfs_free_space
*info
)
1296 rb_erase(&info
->offset_index
, &ctl
->free_space_offset
);
1297 ctl
->free_extents
--;
1300 static void unlink_free_space(struct btrfs_free_space_ctl
*ctl
,
1301 struct btrfs_free_space
*info
)
1303 __unlink_free_space(ctl
, info
);
1304 ctl
->free_space
-= info
->bytes
;
1307 static int link_free_space(struct btrfs_free_space_ctl
*ctl
,
1308 struct btrfs_free_space
*info
)
1312 BUG_ON(!info
->bitmap
&& !info
->bytes
);
1313 ret
= tree_insert_offset(&ctl
->free_space_offset
, info
->offset
,
1314 &info
->offset_index
, (info
->bitmap
!= NULL
));
1318 ctl
->free_space
+= info
->bytes
;
1319 ctl
->free_extents
++;
1323 static void recalculate_thresholds(struct btrfs_free_space_ctl
*ctl
)
1325 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1329 u64 size
= block_group
->key
.offset
;
1330 u64 bytes_per_bg
= BITS_PER_BITMAP
* block_group
->sectorsize
;
1331 int max_bitmaps
= div64_u64(size
+ bytes_per_bg
- 1, bytes_per_bg
);
1333 BUG_ON(ctl
->total_bitmaps
> max_bitmaps
);
1336 * The goal is to keep the total amount of memory used per 1gb of space
1337 * at or below 32k, so we need to adjust how much memory we allow to be
1338 * used by extent based free space tracking
1340 if (size
< 1024 * 1024 * 1024)
1341 max_bytes
= MAX_CACHE_BYTES_PER_GIG
;
1343 max_bytes
= MAX_CACHE_BYTES_PER_GIG
*
1344 div64_u64(size
, 1024 * 1024 * 1024);
1347 * we want to account for 1 more bitmap than what we have so we can make
1348 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1349 * we add more bitmaps.
1351 bitmap_bytes
= (ctl
->total_bitmaps
+ 1) * PAGE_CACHE_SIZE
;
1353 if (bitmap_bytes
>= max_bytes
) {
1354 ctl
->extents_thresh
= 0;
1359 * we want the extent entry threshold to always be at most 1/2 the maxw
1360 * bytes we can have, or whatever is less than that.
1362 extent_bytes
= max_bytes
- bitmap_bytes
;
1363 extent_bytes
= min_t(u64
, extent_bytes
, div64_u64(max_bytes
, 2));
1365 ctl
->extents_thresh
=
1366 div64_u64(extent_bytes
, (sizeof(struct btrfs_free_space
)));
1369 static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1370 struct btrfs_free_space
*info
,
1371 u64 offset
, u64 bytes
)
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_clear(info
->bitmap
, start
, count
);
1381 info
->bytes
-= bytes
;
1384 static void bitmap_clear_bits(struct btrfs_free_space_ctl
*ctl
,
1385 struct btrfs_free_space
*info
, u64 offset
,
1388 __bitmap_clear_bits(ctl
, info
, offset
, bytes
);
1389 ctl
->free_space
-= bytes
;
1392 static void bitmap_set_bits(struct btrfs_free_space_ctl
*ctl
,
1393 struct btrfs_free_space
*info
, u64 offset
,
1396 unsigned long start
, count
;
1398 start
= offset_to_bit(info
->offset
, ctl
->unit
, offset
);
1399 count
= bytes_to_bits(bytes
, ctl
->unit
);
1400 BUG_ON(start
+ count
> BITS_PER_BITMAP
);
1402 bitmap_set(info
->bitmap
, start
, count
);
1404 info
->bytes
+= bytes
;
1405 ctl
->free_space
+= bytes
;
1408 static int search_bitmap(struct btrfs_free_space_ctl
*ctl
,
1409 struct btrfs_free_space
*bitmap_info
, u64
*offset
,
1412 unsigned long found_bits
= 0;
1413 unsigned long bits
, i
;
1414 unsigned long next_zero
;
1416 i
= offset_to_bit(bitmap_info
->offset
, ctl
->unit
,
1417 max_t(u64
, *offset
, bitmap_info
->offset
));
1418 bits
= bytes_to_bits(*bytes
, ctl
->unit
);
1420 for (i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
);
1421 i
< BITS_PER_BITMAP
;
1422 i
= find_next_bit(bitmap_info
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
1423 next_zero
= find_next_zero_bit(bitmap_info
->bitmap
,
1424 BITS_PER_BITMAP
, i
);
1425 if ((next_zero
- i
) >= bits
) {
1426 found_bits
= next_zero
- i
;
1433 *offset
= (u64
)(i
* ctl
->unit
) + bitmap_info
->offset
;
1434 *bytes
= (u64
)(found_bits
) * ctl
->unit
;
1441 static struct btrfs_free_space
*
1442 find_free_space(struct btrfs_free_space_ctl
*ctl
, u64
*offset
, u64
*bytes
)
1444 struct btrfs_free_space
*entry
;
1445 struct rb_node
*node
;
1448 if (!ctl
->free_space_offset
.rb_node
)
1451 entry
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, *offset
), 0, 1);
1455 for (node
= &entry
->offset_index
; node
; node
= rb_next(node
)) {
1456 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1457 if (entry
->bytes
< *bytes
)
1460 if (entry
->bitmap
) {
1461 ret
= search_bitmap(ctl
, entry
, offset
, bytes
);
1467 *offset
= entry
->offset
;
1468 *bytes
= entry
->bytes
;
1475 static void add_new_bitmap(struct btrfs_free_space_ctl
*ctl
,
1476 struct btrfs_free_space
*info
, u64 offset
)
1478 info
->offset
= offset_to_bitmap(ctl
, offset
);
1480 INIT_LIST_HEAD(&info
->list
);
1481 link_free_space(ctl
, info
);
1482 ctl
->total_bitmaps
++;
1484 ctl
->op
->recalc_thresholds(ctl
);
1487 static void free_bitmap(struct btrfs_free_space_ctl
*ctl
,
1488 struct btrfs_free_space
*bitmap_info
)
1490 unlink_free_space(ctl
, bitmap_info
);
1491 kfree(bitmap_info
->bitmap
);
1492 kmem_cache_free(btrfs_free_space_cachep
, bitmap_info
);
1493 ctl
->total_bitmaps
--;
1494 ctl
->op
->recalc_thresholds(ctl
);
1497 static noinline
int remove_from_bitmap(struct btrfs_free_space_ctl
*ctl
,
1498 struct btrfs_free_space
*bitmap_info
,
1499 u64
*offset
, u64
*bytes
)
1502 u64 search_start
, search_bytes
;
1506 end
= bitmap_info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
) - 1;
1509 * XXX - this can go away after a few releases.
1511 * since the only user of btrfs_remove_free_space is the tree logging
1512 * stuff, and the only way to test that is under crash conditions, we
1513 * want to have this debug stuff here just in case somethings not
1514 * working. Search the bitmap for the space we are trying to use to
1515 * make sure its actually there. If its not there then we need to stop
1516 * because something has gone wrong.
1518 search_start
= *offset
;
1519 search_bytes
= *bytes
;
1520 search_bytes
= min(search_bytes
, end
- search_start
+ 1);
1521 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
, &search_bytes
);
1522 BUG_ON(ret
< 0 || search_start
!= *offset
);
1524 if (*offset
> bitmap_info
->offset
&& *offset
+ *bytes
> end
) {
1525 bitmap_clear_bits(ctl
, bitmap_info
, *offset
, end
- *offset
+ 1);
1526 *bytes
-= end
- *offset
+ 1;
1528 } else if (*offset
>= bitmap_info
->offset
&& *offset
+ *bytes
<= end
) {
1529 bitmap_clear_bits(ctl
, bitmap_info
, *offset
, *bytes
);
1534 struct rb_node
*next
= rb_next(&bitmap_info
->offset_index
);
1535 if (!bitmap_info
->bytes
)
1536 free_bitmap(ctl
, bitmap_info
);
1539 * no entry after this bitmap, but we still have bytes to
1540 * remove, so something has gone wrong.
1545 bitmap_info
= rb_entry(next
, struct btrfs_free_space
,
1549 * if the next entry isn't a bitmap we need to return to let the
1550 * extent stuff do its work.
1552 if (!bitmap_info
->bitmap
)
1556 * Ok the next item is a bitmap, but it may not actually hold
1557 * the information for the rest of this free space stuff, so
1558 * look for it, and if we don't find it return so we can try
1559 * everything over again.
1561 search_start
= *offset
;
1562 search_bytes
= *bytes
;
1563 ret
= search_bitmap(ctl
, bitmap_info
, &search_start
,
1565 if (ret
< 0 || search_start
!= *offset
)
1569 } else if (!bitmap_info
->bytes
)
1570 free_bitmap(ctl
, bitmap_info
);
1575 static u64
add_bytes_to_bitmap(struct btrfs_free_space_ctl
*ctl
,
1576 struct btrfs_free_space
*info
, u64 offset
,
1579 u64 bytes_to_set
= 0;
1582 end
= info
->offset
+ (u64
)(BITS_PER_BITMAP
* ctl
->unit
);
1584 bytes_to_set
= min(end
- offset
, bytes
);
1586 bitmap_set_bits(ctl
, info
, offset
, bytes_to_set
);
1588 return bytes_to_set
;
1592 static bool use_bitmap(struct btrfs_free_space_ctl
*ctl
,
1593 struct btrfs_free_space
*info
)
1595 struct btrfs_block_group_cache
*block_group
= ctl
->private;
1598 * If we are below the extents threshold then we can add this as an
1599 * extent, and don't have to deal with the bitmap
1601 if (ctl
->free_extents
< ctl
->extents_thresh
) {
1603 * If this block group has some small extents we don't want to
1604 * use up all of our free slots in the cache with them, we want
1605 * to reserve them to larger extents, however if we have plent
1606 * of cache left then go ahead an dadd them, no sense in adding
1607 * the overhead of a bitmap if we don't have to.
1609 if (info
->bytes
<= block_group
->sectorsize
* 4) {
1610 if (ctl
->free_extents
* 2 <= ctl
->extents_thresh
)
1618 * some block groups are so tiny they can't be enveloped by a bitmap, so
1619 * don't even bother to create a bitmap for this
1621 if (BITS_PER_BITMAP
* block_group
->sectorsize
>
1622 block_group
->key
.offset
)
1628 static struct btrfs_free_space_op free_space_op
= {
1629 .recalc_thresholds
= recalculate_thresholds
,
1630 .use_bitmap
= use_bitmap
,
1633 static int insert_into_bitmap(struct btrfs_free_space_ctl
*ctl
,
1634 struct btrfs_free_space
*info
)
1636 struct btrfs_free_space
*bitmap_info
;
1637 struct btrfs_block_group_cache
*block_group
= NULL
;
1639 u64 bytes
, offset
, bytes_added
;
1642 bytes
= info
->bytes
;
1643 offset
= info
->offset
;
1645 if (!ctl
->op
->use_bitmap(ctl
, info
))
1648 if (ctl
->op
== &free_space_op
)
1649 block_group
= ctl
->private;
1652 * Since we link bitmaps right into the cluster we need to see if we
1653 * have a cluster here, and if so and it has our bitmap we need to add
1654 * the free space to that bitmap.
1656 if (block_group
&& !list_empty(&block_group
->cluster_list
)) {
1657 struct btrfs_free_cluster
*cluster
;
1658 struct rb_node
*node
;
1659 struct btrfs_free_space
*entry
;
1661 cluster
= list_entry(block_group
->cluster_list
.next
,
1662 struct btrfs_free_cluster
,
1664 spin_lock(&cluster
->lock
);
1665 node
= rb_first(&cluster
->root
);
1667 spin_unlock(&cluster
->lock
);
1668 goto no_cluster_bitmap
;
1671 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
1672 if (!entry
->bitmap
) {
1673 spin_unlock(&cluster
->lock
);
1674 goto no_cluster_bitmap
;
1677 if (entry
->offset
== offset_to_bitmap(ctl
, offset
)) {
1678 bytes_added
= add_bytes_to_bitmap(ctl
, entry
,
1680 bytes
-= bytes_added
;
1681 offset
+= bytes_added
;
1683 spin_unlock(&cluster
->lock
);
1691 bitmap_info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
1698 bytes_added
= add_bytes_to_bitmap(ctl
, bitmap_info
, offset
, bytes
);
1699 bytes
-= bytes_added
;
1700 offset
+= bytes_added
;
1710 if (info
&& info
->bitmap
) {
1711 add_new_bitmap(ctl
, info
, offset
);
1716 spin_unlock(&ctl
->tree_lock
);
1718 /* no pre-allocated info, allocate a new one */
1720 info
= kmem_cache_zalloc(btrfs_free_space_cachep
,
1723 spin_lock(&ctl
->tree_lock
);
1729 /* allocate the bitmap */
1730 info
->bitmap
= kzalloc(PAGE_CACHE_SIZE
, GFP_NOFS
);
1731 spin_lock(&ctl
->tree_lock
);
1732 if (!info
->bitmap
) {
1742 kfree(info
->bitmap
);
1743 kmem_cache_free(btrfs_free_space_cachep
, info
);
1749 static bool try_merge_free_space(struct btrfs_free_space_ctl
*ctl
,
1750 struct btrfs_free_space
*info
, bool update_stat
)
1752 struct btrfs_free_space
*left_info
;
1753 struct btrfs_free_space
*right_info
;
1754 bool merged
= false;
1755 u64 offset
= info
->offset
;
1756 u64 bytes
= info
->bytes
;
1759 * first we want to see if there is free space adjacent to the range we
1760 * are adding, if there is remove that struct and add a new one to
1761 * cover the entire range
1763 right_info
= tree_search_offset(ctl
, offset
+ bytes
, 0, 0);
1764 if (right_info
&& rb_prev(&right_info
->offset_index
))
1765 left_info
= rb_entry(rb_prev(&right_info
->offset_index
),
1766 struct btrfs_free_space
, offset_index
);
1768 left_info
= tree_search_offset(ctl
, offset
- 1, 0, 0);
1770 if (right_info
&& !right_info
->bitmap
) {
1772 unlink_free_space(ctl
, right_info
);
1774 __unlink_free_space(ctl
, right_info
);
1775 info
->bytes
+= right_info
->bytes
;
1776 kmem_cache_free(btrfs_free_space_cachep
, right_info
);
1780 if (left_info
&& !left_info
->bitmap
&&
1781 left_info
->offset
+ left_info
->bytes
== offset
) {
1783 unlink_free_space(ctl
, left_info
);
1785 __unlink_free_space(ctl
, left_info
);
1786 info
->offset
= left_info
->offset
;
1787 info
->bytes
+= left_info
->bytes
;
1788 kmem_cache_free(btrfs_free_space_cachep
, left_info
);
1795 int __btrfs_add_free_space(struct btrfs_free_space_ctl
*ctl
,
1796 u64 offset
, u64 bytes
)
1798 struct btrfs_free_space
*info
;
1801 info
= kmem_cache_zalloc(btrfs_free_space_cachep
, GFP_NOFS
);
1805 info
->offset
= offset
;
1806 info
->bytes
= bytes
;
1808 spin_lock(&ctl
->tree_lock
);
1810 if (try_merge_free_space(ctl
, info
, true))
1814 * There was no extent directly to the left or right of this new
1815 * extent then we know we're going to have to allocate a new extent, so
1816 * before we do that see if we need to drop this into a bitmap
1818 ret
= insert_into_bitmap(ctl
, info
);
1826 ret
= link_free_space(ctl
, info
);
1828 kmem_cache_free(btrfs_free_space_cachep
, info
);
1830 spin_unlock(&ctl
->tree_lock
);
1833 printk(KERN_CRIT
"btrfs: unable to add free space :%d\n", ret
);
1834 BUG_ON(ret
== -EEXIST
);
1840 int btrfs_remove_free_space(struct btrfs_block_group_cache
*block_group
,
1841 u64 offset
, u64 bytes
)
1843 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1844 struct btrfs_free_space
*info
;
1845 struct btrfs_free_space
*next_info
= NULL
;
1848 spin_lock(&ctl
->tree_lock
);
1851 info
= tree_search_offset(ctl
, offset
, 0, 0);
1854 * oops didn't find an extent that matched the space we wanted
1855 * to remove, look for a bitmap instead
1857 info
= tree_search_offset(ctl
, offset_to_bitmap(ctl
, offset
),
1860 /* the tree logging code might be calling us before we
1861 * have fully loaded the free space rbtree for this
1862 * block group. So it is possible the entry won't
1863 * be in the rbtree yet at all. The caching code
1864 * will make sure not to put it in the rbtree if
1865 * the logging code has pinned it.
1871 if (info
->bytes
< bytes
&& rb_next(&info
->offset_index
)) {
1873 next_info
= rb_entry(rb_next(&info
->offset_index
),
1874 struct btrfs_free_space
,
1877 if (next_info
->bitmap
)
1878 end
= next_info
->offset
+
1879 BITS_PER_BITMAP
* ctl
->unit
- 1;
1881 end
= next_info
->offset
+ next_info
->bytes
;
1883 if (next_info
->bytes
< bytes
||
1884 next_info
->offset
> offset
|| offset
> end
) {
1885 printk(KERN_CRIT
"Found free space at %llu, size %llu,"
1886 " trying to use %llu\n",
1887 (unsigned long long)info
->offset
,
1888 (unsigned long long)info
->bytes
,
1889 (unsigned long long)bytes
);
1898 if (info
->bytes
== bytes
) {
1899 unlink_free_space(ctl
, info
);
1901 kfree(info
->bitmap
);
1902 ctl
->total_bitmaps
--;
1904 kmem_cache_free(btrfs_free_space_cachep
, info
);
1909 if (!info
->bitmap
&& info
->offset
== offset
) {
1910 unlink_free_space(ctl
, info
);
1911 info
->offset
+= bytes
;
1912 info
->bytes
-= bytes
;
1913 ret
= link_free_space(ctl
, info
);
1918 if (!info
->bitmap
&& info
->offset
<= offset
&&
1919 info
->offset
+ info
->bytes
>= offset
+ bytes
) {
1920 u64 old_start
= info
->offset
;
1922 * we're freeing space in the middle of the info,
1923 * this can happen during tree log replay
1925 * first unlink the old info and then
1926 * insert it again after the hole we're creating
1928 unlink_free_space(ctl
, info
);
1929 if (offset
+ bytes
< info
->offset
+ info
->bytes
) {
1930 u64 old_end
= info
->offset
+ info
->bytes
;
1932 info
->offset
= offset
+ bytes
;
1933 info
->bytes
= old_end
- info
->offset
;
1934 ret
= link_free_space(ctl
, info
);
1939 /* the hole we're creating ends at the end
1940 * of the info struct, just free the info
1942 kmem_cache_free(btrfs_free_space_cachep
, info
);
1944 spin_unlock(&ctl
->tree_lock
);
1946 /* step two, insert a new info struct to cover
1947 * anything before the hole
1949 ret
= btrfs_add_free_space(block_group
, old_start
,
1950 offset
- old_start
);
1955 ret
= remove_from_bitmap(ctl
, info
, &offset
, &bytes
);
1960 spin_unlock(&ctl
->tree_lock
);
1965 void btrfs_dump_free_space(struct btrfs_block_group_cache
*block_group
,
1968 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1969 struct btrfs_free_space
*info
;
1973 for (n
= rb_first(&ctl
->free_space_offset
); n
; n
= rb_next(n
)) {
1974 info
= rb_entry(n
, struct btrfs_free_space
, offset_index
);
1975 if (info
->bytes
>= bytes
)
1977 printk(KERN_CRIT
"entry offset %llu, bytes %llu, bitmap %s\n",
1978 (unsigned long long)info
->offset
,
1979 (unsigned long long)info
->bytes
,
1980 (info
->bitmap
) ? "yes" : "no");
1982 printk(KERN_INFO
"block group has cluster?: %s\n",
1983 list_empty(&block_group
->cluster_list
) ? "no" : "yes");
1984 printk(KERN_INFO
"%d blocks of free space at or bigger than bytes is"
1988 void btrfs_init_free_space_ctl(struct btrfs_block_group_cache
*block_group
)
1990 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
1992 spin_lock_init(&ctl
->tree_lock
);
1993 ctl
->unit
= block_group
->sectorsize
;
1994 ctl
->start
= block_group
->key
.objectid
;
1995 ctl
->private = block_group
;
1996 ctl
->op
= &free_space_op
;
1999 * we only want to have 32k of ram per block group for keeping
2000 * track of free space, and if we pass 1/2 of that we want to
2001 * start converting things over to using bitmaps
2003 ctl
->extents_thresh
= ((1024 * 32) / 2) /
2004 sizeof(struct btrfs_free_space
);
2008 * for a given cluster, put all of its extents back into the free
2009 * space cache. If the block group passed doesn't match the block group
2010 * pointed to by the cluster, someone else raced in and freed the
2011 * cluster already. In that case, we just return without changing anything
2014 __btrfs_return_cluster_to_free_space(
2015 struct btrfs_block_group_cache
*block_group
,
2016 struct btrfs_free_cluster
*cluster
)
2018 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2019 struct btrfs_free_space
*entry
;
2020 struct rb_node
*node
;
2022 spin_lock(&cluster
->lock
);
2023 if (cluster
->block_group
!= block_group
)
2026 cluster
->block_group
= NULL
;
2027 cluster
->window_start
= 0;
2028 list_del_init(&cluster
->block_group_list
);
2030 node
= rb_first(&cluster
->root
);
2034 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2035 node
= rb_next(&entry
->offset_index
);
2036 rb_erase(&entry
->offset_index
, &cluster
->root
);
2038 bitmap
= (entry
->bitmap
!= NULL
);
2040 try_merge_free_space(ctl
, entry
, false);
2041 tree_insert_offset(&ctl
->free_space_offset
,
2042 entry
->offset
, &entry
->offset_index
, bitmap
);
2044 cluster
->root
= RB_ROOT
;
2047 spin_unlock(&cluster
->lock
);
2048 btrfs_put_block_group(block_group
);
2052 void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl
*ctl
)
2054 struct btrfs_free_space
*info
;
2055 struct rb_node
*node
;
2057 while ((node
= rb_last(&ctl
->free_space_offset
)) != NULL
) {
2058 info
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2059 if (!info
->bitmap
) {
2060 unlink_free_space(ctl
, info
);
2061 kmem_cache_free(btrfs_free_space_cachep
, info
);
2063 free_bitmap(ctl
, info
);
2065 if (need_resched()) {
2066 spin_unlock(&ctl
->tree_lock
);
2068 spin_lock(&ctl
->tree_lock
);
2073 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl
*ctl
)
2075 spin_lock(&ctl
->tree_lock
);
2076 __btrfs_remove_free_space_cache_locked(ctl
);
2077 spin_unlock(&ctl
->tree_lock
);
2080 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache
*block_group
)
2082 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2083 struct btrfs_free_cluster
*cluster
;
2084 struct list_head
*head
;
2086 spin_lock(&ctl
->tree_lock
);
2087 while ((head
= block_group
->cluster_list
.next
) !=
2088 &block_group
->cluster_list
) {
2089 cluster
= list_entry(head
, struct btrfs_free_cluster
,
2092 WARN_ON(cluster
->block_group
!= block_group
);
2093 __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2094 if (need_resched()) {
2095 spin_unlock(&ctl
->tree_lock
);
2097 spin_lock(&ctl
->tree_lock
);
2100 __btrfs_remove_free_space_cache_locked(ctl
);
2101 spin_unlock(&ctl
->tree_lock
);
2105 u64
btrfs_find_space_for_alloc(struct btrfs_block_group_cache
*block_group
,
2106 u64 offset
, u64 bytes
, u64 empty_size
)
2108 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2109 struct btrfs_free_space
*entry
= NULL
;
2110 u64 bytes_search
= bytes
+ empty_size
;
2113 spin_lock(&ctl
->tree_lock
);
2114 entry
= find_free_space(ctl
, &offset
, &bytes_search
);
2119 if (entry
->bitmap
) {
2120 bitmap_clear_bits(ctl
, entry
, offset
, bytes
);
2122 free_bitmap(ctl
, entry
);
2124 unlink_free_space(ctl
, entry
);
2125 entry
->offset
+= bytes
;
2126 entry
->bytes
-= bytes
;
2128 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2130 link_free_space(ctl
, entry
);
2134 spin_unlock(&ctl
->tree_lock
);
2140 * given a cluster, put all of its extents back into the free space
2141 * cache. If a block group is passed, this function will only free
2142 * a cluster that belongs to the passed block group.
2144 * Otherwise, it'll get a reference on the block group pointed to by the
2145 * cluster and remove the cluster from it.
2147 int btrfs_return_cluster_to_free_space(
2148 struct btrfs_block_group_cache
*block_group
,
2149 struct btrfs_free_cluster
*cluster
)
2151 struct btrfs_free_space_ctl
*ctl
;
2154 /* first, get a safe pointer to the block group */
2155 spin_lock(&cluster
->lock
);
2157 block_group
= cluster
->block_group
;
2159 spin_unlock(&cluster
->lock
);
2162 } else if (cluster
->block_group
!= block_group
) {
2163 /* someone else has already freed it don't redo their work */
2164 spin_unlock(&cluster
->lock
);
2167 atomic_inc(&block_group
->count
);
2168 spin_unlock(&cluster
->lock
);
2170 ctl
= block_group
->free_space_ctl
;
2172 /* now return any extents the cluster had on it */
2173 spin_lock(&ctl
->tree_lock
);
2174 ret
= __btrfs_return_cluster_to_free_space(block_group
, cluster
);
2175 spin_unlock(&ctl
->tree_lock
);
2177 /* finally drop our ref */
2178 btrfs_put_block_group(block_group
);
2182 static u64
btrfs_alloc_from_bitmap(struct btrfs_block_group_cache
*block_group
,
2183 struct btrfs_free_cluster
*cluster
,
2184 struct btrfs_free_space
*entry
,
2185 u64 bytes
, u64 min_start
)
2187 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2189 u64 search_start
= cluster
->window_start
;
2190 u64 search_bytes
= bytes
;
2193 search_start
= min_start
;
2194 search_bytes
= bytes
;
2196 err
= search_bitmap(ctl
, entry
, &search_start
, &search_bytes
);
2201 __bitmap_clear_bits(ctl
, entry
, ret
, bytes
);
2207 * given a cluster, try to allocate 'bytes' from it, returns 0
2208 * if it couldn't find anything suitably large, or a logical disk offset
2209 * if things worked out
2211 u64
btrfs_alloc_from_cluster(struct btrfs_block_group_cache
*block_group
,
2212 struct btrfs_free_cluster
*cluster
, u64 bytes
,
2215 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2216 struct btrfs_free_space
*entry
= NULL
;
2217 struct rb_node
*node
;
2220 spin_lock(&cluster
->lock
);
2221 if (bytes
> cluster
->max_size
)
2224 if (cluster
->block_group
!= block_group
)
2227 node
= rb_first(&cluster
->root
);
2231 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2233 if (entry
->bytes
< bytes
||
2234 (!entry
->bitmap
&& entry
->offset
< min_start
)) {
2235 node
= rb_next(&entry
->offset_index
);
2238 entry
= rb_entry(node
, struct btrfs_free_space
,
2243 if (entry
->bitmap
) {
2244 ret
= btrfs_alloc_from_bitmap(block_group
,
2245 cluster
, entry
, bytes
,
2246 cluster
->window_start
);
2248 node
= rb_next(&entry
->offset_index
);
2251 entry
= rb_entry(node
, struct btrfs_free_space
,
2255 cluster
->window_start
+= bytes
;
2257 ret
= entry
->offset
;
2259 entry
->offset
+= bytes
;
2260 entry
->bytes
-= bytes
;
2263 if (entry
->bytes
== 0)
2264 rb_erase(&entry
->offset_index
, &cluster
->root
);
2268 spin_unlock(&cluster
->lock
);
2273 spin_lock(&ctl
->tree_lock
);
2275 ctl
->free_space
-= bytes
;
2276 if (entry
->bytes
== 0) {
2277 ctl
->free_extents
--;
2278 if (entry
->bitmap
) {
2279 kfree(entry
->bitmap
);
2280 ctl
->total_bitmaps
--;
2281 ctl
->op
->recalc_thresholds(ctl
);
2283 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2286 spin_unlock(&ctl
->tree_lock
);
2291 static int btrfs_bitmap_cluster(struct btrfs_block_group_cache
*block_group
,
2292 struct btrfs_free_space
*entry
,
2293 struct btrfs_free_cluster
*cluster
,
2294 u64 offset
, u64 bytes
,
2295 u64 cont1_bytes
, u64 min_bytes
)
2297 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2298 unsigned long next_zero
;
2300 unsigned long want_bits
;
2301 unsigned long min_bits
;
2302 unsigned long found_bits
;
2303 unsigned long start
= 0;
2304 unsigned long total_found
= 0;
2307 i
= offset_to_bit(entry
->offset
, block_group
->sectorsize
,
2308 max_t(u64
, offset
, entry
->offset
));
2309 want_bits
= bytes_to_bits(bytes
, block_group
->sectorsize
);
2310 min_bits
= bytes_to_bits(min_bytes
, block_group
->sectorsize
);
2314 for (i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
);
2315 i
< BITS_PER_BITMAP
;
2316 i
= find_next_bit(entry
->bitmap
, BITS_PER_BITMAP
, i
+ 1)) {
2317 next_zero
= find_next_zero_bit(entry
->bitmap
,
2318 BITS_PER_BITMAP
, i
);
2319 if (next_zero
- i
>= min_bits
) {
2320 found_bits
= next_zero
- i
;
2331 cluster
->max_size
= 0;
2334 total_found
+= found_bits
;
2336 if (cluster
->max_size
< found_bits
* block_group
->sectorsize
)
2337 cluster
->max_size
= found_bits
* block_group
->sectorsize
;
2339 if (total_found
< want_bits
|| cluster
->max_size
< cont1_bytes
) {
2344 cluster
->window_start
= start
* block_group
->sectorsize
+
2346 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2347 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2348 &entry
->offset_index
, 1);
2351 trace_btrfs_setup_cluster(block_group
, cluster
,
2352 total_found
* block_group
->sectorsize
, 1);
2357 * This searches the block group for just extents to fill the cluster with.
2358 * Try to find a cluster with at least bytes total bytes, at least one
2359 * extent of cont1_bytes, and other clusters of at least min_bytes.
2362 setup_cluster_no_bitmap(struct btrfs_block_group_cache
*block_group
,
2363 struct btrfs_free_cluster
*cluster
,
2364 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2365 u64 cont1_bytes
, u64 min_bytes
)
2367 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2368 struct btrfs_free_space
*first
= NULL
;
2369 struct btrfs_free_space
*entry
= NULL
;
2370 struct btrfs_free_space
*last
;
2371 struct rb_node
*node
;
2377 entry
= tree_search_offset(ctl
, offset
, 0, 1);
2382 * We don't want bitmaps, so just move along until we find a normal
2385 while (entry
->bitmap
|| entry
->bytes
< min_bytes
) {
2386 if (entry
->bitmap
&& list_empty(&entry
->list
))
2387 list_add_tail(&entry
->list
, bitmaps
);
2388 node
= rb_next(&entry
->offset_index
);
2391 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2394 window_start
= entry
->offset
;
2395 window_free
= entry
->bytes
;
2396 max_extent
= entry
->bytes
;
2400 for (node
= rb_next(&entry
->offset_index
); node
;
2401 node
= rb_next(&entry
->offset_index
)) {
2402 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2404 if (entry
->bitmap
) {
2405 if (list_empty(&entry
->list
))
2406 list_add_tail(&entry
->list
, bitmaps
);
2410 if (entry
->bytes
< min_bytes
)
2414 window_free
+= entry
->bytes
;
2415 if (entry
->bytes
> max_extent
)
2416 max_extent
= entry
->bytes
;
2419 if (window_free
< bytes
|| max_extent
< cont1_bytes
)
2422 cluster
->window_start
= first
->offset
;
2424 node
= &first
->offset_index
;
2427 * now we've found our entries, pull them out of the free space
2428 * cache and put them into the cluster rbtree
2433 entry
= rb_entry(node
, struct btrfs_free_space
, offset_index
);
2434 node
= rb_next(&entry
->offset_index
);
2435 if (entry
->bitmap
|| entry
->bytes
< min_bytes
)
2438 rb_erase(&entry
->offset_index
, &ctl
->free_space_offset
);
2439 ret
= tree_insert_offset(&cluster
->root
, entry
->offset
,
2440 &entry
->offset_index
, 0);
2441 total_size
+= entry
->bytes
;
2443 } while (node
&& entry
!= last
);
2445 cluster
->max_size
= max_extent
;
2446 trace_btrfs_setup_cluster(block_group
, cluster
, total_size
, 0);
2451 * This specifically looks for bitmaps that may work in the cluster, we assume
2452 * that we have already failed to find extents that will work.
2455 setup_cluster_bitmap(struct btrfs_block_group_cache
*block_group
,
2456 struct btrfs_free_cluster
*cluster
,
2457 struct list_head
*bitmaps
, u64 offset
, u64 bytes
,
2458 u64 cont1_bytes
, u64 min_bytes
)
2460 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2461 struct btrfs_free_space
*entry
;
2463 u64 bitmap_offset
= offset_to_bitmap(ctl
, offset
);
2465 if (ctl
->total_bitmaps
== 0)
2469 * The bitmap that covers offset won't be in the list unless offset
2470 * is just its start offset.
2472 entry
= list_first_entry(bitmaps
, struct btrfs_free_space
, list
);
2473 if (entry
->offset
!= bitmap_offset
) {
2474 entry
= tree_search_offset(ctl
, bitmap_offset
, 1, 0);
2475 if (entry
&& list_empty(&entry
->list
))
2476 list_add(&entry
->list
, bitmaps
);
2479 list_for_each_entry(entry
, bitmaps
, list
) {
2480 if (entry
->bytes
< bytes
)
2482 ret
= btrfs_bitmap_cluster(block_group
, entry
, cluster
, offset
,
2483 bytes
, cont1_bytes
, min_bytes
);
2489 * The bitmaps list has all the bitmaps that record free space
2490 * starting after offset, so no more search is required.
2496 * here we try to find a cluster of blocks in a block group. The goal
2497 * is to find at least bytes+empty_size.
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 btrfs_free_space
*entry
, *tmp
;
2517 * Choose the minimum extent size we'll require for this
2518 * cluster. For SSD_SPREAD, don't allow any fragmentation.
2519 * For metadata, allow allocates with smaller extents. For
2520 * data, keep it dense.
2522 if (btrfs_test_opt(root
, SSD_SPREAD
)) {
2523 cont1_bytes
= min_bytes
= bytes
+ empty_size
;
2524 } else if (block_group
->flags
& BTRFS_BLOCK_GROUP_METADATA
) {
2525 cont1_bytes
= bytes
;
2526 min_bytes
= block_group
->sectorsize
;
2528 cont1_bytes
= max(bytes
, (bytes
+ empty_size
) >> 2);
2529 min_bytes
= block_group
->sectorsize
;
2532 spin_lock(&ctl
->tree_lock
);
2535 * If we know we don't have enough space to make a cluster don't even
2536 * bother doing all the work to try and find one.
2538 if (ctl
->free_space
< bytes
) {
2539 spin_unlock(&ctl
->tree_lock
);
2543 spin_lock(&cluster
->lock
);
2545 /* someone already found a cluster, hooray */
2546 if (cluster
->block_group
) {
2551 trace_btrfs_find_cluster(block_group
, offset
, bytes
, empty_size
,
2554 INIT_LIST_HEAD(&bitmaps
);
2555 ret
= setup_cluster_no_bitmap(block_group
, cluster
, &bitmaps
, offset
,
2557 cont1_bytes
, min_bytes
);
2559 ret
= setup_cluster_bitmap(block_group
, cluster
, &bitmaps
,
2560 offset
, bytes
+ empty_size
,
2561 cont1_bytes
, min_bytes
);
2563 /* Clear our temporary list */
2564 list_for_each_entry_safe(entry
, tmp
, &bitmaps
, list
)
2565 list_del_init(&entry
->list
);
2568 atomic_inc(&block_group
->count
);
2569 list_add_tail(&cluster
->block_group_list
,
2570 &block_group
->cluster_list
);
2571 cluster
->block_group
= block_group
;
2573 trace_btrfs_failed_cluster_setup(block_group
);
2576 spin_unlock(&cluster
->lock
);
2577 spin_unlock(&ctl
->tree_lock
);
2583 * simple code to zero out a cluster
2585 void btrfs_init_free_cluster(struct btrfs_free_cluster
*cluster
)
2587 spin_lock_init(&cluster
->lock
);
2588 spin_lock_init(&cluster
->refill_lock
);
2589 cluster
->root
= RB_ROOT
;
2590 cluster
->max_size
= 0;
2591 INIT_LIST_HEAD(&cluster
->block_group_list
);
2592 cluster
->block_group
= NULL
;
2595 static int do_trimming(struct btrfs_block_group_cache
*block_group
,
2596 u64
*total_trimmed
, u64 start
, u64 bytes
,
2597 u64 reserved_start
, u64 reserved_bytes
)
2599 struct btrfs_space_info
*space_info
= block_group
->space_info
;
2600 struct btrfs_fs_info
*fs_info
= block_group
->fs_info
;
2605 spin_lock(&space_info
->lock
);
2606 spin_lock(&block_group
->lock
);
2607 if (!block_group
->ro
) {
2608 block_group
->reserved
+= reserved_bytes
;
2609 space_info
->bytes_reserved
+= reserved_bytes
;
2612 spin_unlock(&block_group
->lock
);
2613 spin_unlock(&space_info
->lock
);
2615 ret
= btrfs_error_discard_extent(fs_info
->extent_root
,
2616 start
, bytes
, &trimmed
);
2618 *total_trimmed
+= trimmed
;
2620 btrfs_add_free_space(block_group
, reserved_start
, reserved_bytes
);
2623 spin_lock(&space_info
->lock
);
2624 spin_lock(&block_group
->lock
);
2625 if (block_group
->ro
)
2626 space_info
->bytes_readonly
+= reserved_bytes
;
2627 block_group
->reserved
-= reserved_bytes
;
2628 space_info
->bytes_reserved
-= reserved_bytes
;
2629 spin_unlock(&space_info
->lock
);
2630 spin_unlock(&block_group
->lock
);
2636 static int trim_no_bitmap(struct btrfs_block_group_cache
*block_group
,
2637 u64
*total_trimmed
, u64 start
, u64 end
, u64 minlen
)
2639 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2640 struct btrfs_free_space
*entry
;
2641 struct rb_node
*node
;
2647 while (start
< end
) {
2648 spin_lock(&ctl
->tree_lock
);
2650 if (ctl
->free_space
< minlen
) {
2651 spin_unlock(&ctl
->tree_lock
);
2655 entry
= tree_search_offset(ctl
, start
, 0, 1);
2657 spin_unlock(&ctl
->tree_lock
);
2662 while (entry
->bitmap
) {
2663 node
= rb_next(&entry
->offset_index
);
2665 spin_unlock(&ctl
->tree_lock
);
2668 entry
= rb_entry(node
, struct btrfs_free_space
,
2672 if (entry
->offset
>= end
) {
2673 spin_unlock(&ctl
->tree_lock
);
2677 extent_start
= entry
->offset
;
2678 extent_bytes
= entry
->bytes
;
2679 start
= max(start
, extent_start
);
2680 bytes
= min(extent_start
+ extent_bytes
, end
) - start
;
2681 if (bytes
< minlen
) {
2682 spin_unlock(&ctl
->tree_lock
);
2686 unlink_free_space(ctl
, entry
);
2687 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2689 spin_unlock(&ctl
->tree_lock
);
2691 ret
= do_trimming(block_group
, total_trimmed
, start
, bytes
,
2692 extent_start
, extent_bytes
);
2698 if (fatal_signal_pending(current
)) {
2709 static int trim_bitmaps(struct btrfs_block_group_cache
*block_group
,
2710 u64
*total_trimmed
, u64 start
, u64 end
, u64 minlen
)
2712 struct btrfs_free_space_ctl
*ctl
= block_group
->free_space_ctl
;
2713 struct btrfs_free_space
*entry
;
2717 u64 offset
= offset_to_bitmap(ctl
, start
);
2719 while (offset
< end
) {
2720 bool next_bitmap
= false;
2722 spin_lock(&ctl
->tree_lock
);
2724 if (ctl
->free_space
< minlen
) {
2725 spin_unlock(&ctl
->tree_lock
);
2729 entry
= tree_search_offset(ctl
, offset
, 1, 0);
2731 spin_unlock(&ctl
->tree_lock
);
2737 ret2
= search_bitmap(ctl
, entry
, &start
, &bytes
);
2738 if (ret2
|| start
>= end
) {
2739 spin_unlock(&ctl
->tree_lock
);
2744 bytes
= min(bytes
, end
- start
);
2745 if (bytes
< minlen
) {
2746 spin_unlock(&ctl
->tree_lock
);
2750 bitmap_clear_bits(ctl
, entry
, start
, bytes
);
2751 if (entry
->bytes
== 0)
2752 free_bitmap(ctl
, entry
);
2754 spin_unlock(&ctl
->tree_lock
);
2756 ret
= do_trimming(block_group
, total_trimmed
, start
, bytes
,
2762 offset
+= BITS_PER_BITMAP
* ctl
->unit
;
2765 if (start
>= offset
+ BITS_PER_BITMAP
* ctl
->unit
)
2766 offset
+= BITS_PER_BITMAP
* ctl
->unit
;
2769 if (fatal_signal_pending(current
)) {
2780 int btrfs_trim_block_group(struct btrfs_block_group_cache
*block_group
,
2781 u64
*trimmed
, u64 start
, u64 end
, u64 minlen
)
2787 ret
= trim_no_bitmap(block_group
, trimmed
, start
, end
, minlen
);
2791 ret
= trim_bitmaps(block_group
, trimmed
, start
, end
, minlen
);
2797 * Find the left-most item in the cache tree, and then return the
2798 * smallest inode number in the item.
2800 * Note: the returned inode number may not be the smallest one in
2801 * the tree, if the left-most item is a bitmap.
2803 u64
btrfs_find_ino_for_alloc(struct btrfs_root
*fs_root
)
2805 struct btrfs_free_space_ctl
*ctl
= fs_root
->free_ino_ctl
;
2806 struct btrfs_free_space
*entry
= NULL
;
2809 spin_lock(&ctl
->tree_lock
);
2811 if (RB_EMPTY_ROOT(&ctl
->free_space_offset
))
2814 entry
= rb_entry(rb_first(&ctl
->free_space_offset
),
2815 struct btrfs_free_space
, offset_index
);
2817 if (!entry
->bitmap
) {
2818 ino
= entry
->offset
;
2820 unlink_free_space(ctl
, entry
);
2824 kmem_cache_free(btrfs_free_space_cachep
, entry
);
2826 link_free_space(ctl
, entry
);
2832 ret
= search_bitmap(ctl
, entry
, &offset
, &count
);
2836 bitmap_clear_bits(ctl
, entry
, offset
, 1);
2837 if (entry
->bytes
== 0)
2838 free_bitmap(ctl
, entry
);
2841 spin_unlock(&ctl
->tree_lock
);
2846 struct inode
*lookup_free_ino_inode(struct btrfs_root
*root
,
2847 struct btrfs_path
*path
)
2849 struct inode
*inode
= NULL
;
2851 spin_lock(&root
->cache_lock
);
2852 if (root
->cache_inode
)
2853 inode
= igrab(root
->cache_inode
);
2854 spin_unlock(&root
->cache_lock
);
2858 inode
= __lookup_free_space_inode(root
, path
, 0);
2862 spin_lock(&root
->cache_lock
);
2863 if (!btrfs_fs_closing(root
->fs_info
))
2864 root
->cache_inode
= igrab(inode
);
2865 spin_unlock(&root
->cache_lock
);
2870 int create_free_ino_inode(struct btrfs_root
*root
,
2871 struct btrfs_trans_handle
*trans
,
2872 struct btrfs_path
*path
)
2874 return __create_free_space_inode(root
, trans
, path
,
2875 BTRFS_FREE_INO_OBJECTID
, 0);
2878 int load_free_ino_cache(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
2880 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
2881 struct btrfs_path
*path
;
2882 struct inode
*inode
;
2884 u64 root_gen
= btrfs_root_generation(&root
->root_item
);
2886 if (!btrfs_test_opt(root
, INODE_MAP_CACHE
))
2890 * If we're unmounting then just return, since this does a search on the
2891 * normal root and not the commit root and we could deadlock.
2893 if (btrfs_fs_closing(fs_info
))
2896 path
= btrfs_alloc_path();
2900 inode
= lookup_free_ino_inode(root
, path
);
2904 if (root_gen
!= BTRFS_I(inode
)->generation
)
2907 ret
= __load_free_space_cache(root
, inode
, ctl
, path
, 0);
2910 printk(KERN_ERR
"btrfs: failed to load free ino cache for "
2911 "root %llu\n", root
->root_key
.objectid
);
2915 btrfs_free_path(path
);
2919 int btrfs_write_out_ino_cache(struct btrfs_root
*root
,
2920 struct btrfs_trans_handle
*trans
,
2921 struct btrfs_path
*path
)
2923 struct btrfs_free_space_ctl
*ctl
= root
->free_ino_ctl
;
2924 struct inode
*inode
;
2927 if (!btrfs_test_opt(root
, INODE_MAP_CACHE
))
2930 inode
= lookup_free_ino_inode(root
, path
);
2934 ret
= __btrfs_write_out_cache(root
, inode
, ctl
, NULL
, trans
, path
, 0);
2936 btrfs_delalloc_release_metadata(inode
, inode
->i_size
);
2938 printk(KERN_ERR
"btrfs: failed to write free ino cache "
2939 "for root %llu\n", root
->root_key
.objectid
);