2 * Copyright (C) 2007 Oracle. 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.
20 #include <linux/blkdev.h>
21 #include <linux/crc32c.h>
22 #include <linux/scatterlist.h>
23 #include <linux/swap.h>
24 #include <linux/radix-tree.h>
25 #include <linux/writeback.h>
26 #include <linux/buffer_head.h> // for block_sync_page
29 #include "transaction.h"
30 #include "btrfs_inode.h"
32 #include "print-tree.h"
35 static int check_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
)
37 if (extent_buffer_blocknr(buf
) != btrfs_header_blocknr(buf
)) {
38 printk(KERN_CRIT
"buf blocknr(buf) is %llu, header is %llu\n",
39 (unsigned long long)extent_buffer_blocknr(buf
),
40 (unsigned long long)btrfs_header_blocknr(buf
));
47 static struct extent_io_ops btree_extent_io_ops
;
49 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
50 u64 bytenr
, u32 blocksize
)
52 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
53 struct extent_buffer
*eb
;
54 eb
= find_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
55 bytenr
, blocksize
, GFP_NOFS
);
59 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
60 u64 bytenr
, u32 blocksize
)
62 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
63 struct extent_buffer
*eb
;
65 eb
= alloc_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
66 bytenr
, blocksize
, NULL
, GFP_NOFS
);
70 struct extent_map
*btree_get_extent(struct inode
*inode
, struct page
*page
,
71 size_t page_offset
, u64 start
, u64 len
,
74 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
75 struct extent_map
*em
;
79 spin_lock(&em_tree
->lock
);
80 em
= lookup_extent_mapping(em_tree
, start
, len
);
81 spin_unlock(&em_tree
->lock
);
85 em
= alloc_extent_map(GFP_NOFS
);
87 em
= ERR_PTR(-ENOMEM
);
91 em
->len
= i_size_read(inode
);
93 em
->bdev
= inode
->i_sb
->s_bdev
;
95 spin_lock(&em_tree
->lock
);
96 ret
= add_extent_mapping(em_tree
, em
);
97 spin_unlock(&em_tree
->lock
);
110 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
112 return crc32c(seed
, data
, len
);
115 void btrfs_csum_final(u32 crc
, char *result
)
117 *(__le32
*)result
= ~cpu_to_le32(crc
);
120 static int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
123 char result
[BTRFS_CRC32_SIZE
];
125 unsigned long cur_len
;
126 unsigned long offset
= BTRFS_CSUM_SIZE
;
127 char *map_token
= NULL
;
129 unsigned long map_start
;
130 unsigned long map_len
;
134 len
= buf
->len
- offset
;
136 err
= map_private_extent_buffer(buf
, offset
, 32,
138 &map_start
, &map_len
, KM_USER0
);
140 printk("failed to map extent buffer! %lu\n",
144 cur_len
= min(len
, map_len
- (offset
- map_start
));
145 crc
= btrfs_csum_data(root
, kaddr
+ offset
- map_start
,
149 unmap_extent_buffer(buf
, map_token
, KM_USER0
);
151 btrfs_csum_final(crc
, result
);
154 int from_this_trans
= 0;
156 if (root
->fs_info
->running_transaction
&&
157 btrfs_header_generation(buf
) ==
158 root
->fs_info
->running_transaction
->transid
)
161 /* FIXME, this is not good */
162 if (memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
165 memcpy(&found
, result
, BTRFS_CRC32_SIZE
);
167 read_extent_buffer(buf
, &val
, 0, BTRFS_CRC32_SIZE
);
169 printk("btrfs: %s checksum verify failed on %llu "
170 "wanted %X found %X from_this_trans %d "
172 root
->fs_info
->sb
->s_id
,
173 buf
->start
, val
, found
, from_this_trans
,
174 btrfs_header_level(buf
));
178 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
184 int csum_dirty_buffer(struct btrfs_root
*root
, struct page
*page
)
186 struct extent_io_tree
*tree
;
187 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
191 struct extent_buffer
*eb
;
192 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
194 if (page
->private == EXTENT_PAGE_PRIVATE
)
198 len
= page
->private >> 2;
202 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
203 read_extent_buffer_pages(tree
, eb
, start
+ PAGE_CACHE_SIZE
, 1,
205 btrfs_clear_buffer_defrag(eb
);
206 found_start
= btrfs_header_bytenr(eb
);
207 if (found_start
!= start
) {
208 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
209 start
, found_start
, len
);
213 if (eb
->first_page
!= page
) {
214 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
219 if (!PageUptodate(page
)) {
220 printk("csum not up to date page %lu\n", page
->index
);
224 found_level
= btrfs_header_level(eb
);
225 spin_lock(&root
->fs_info
->hash_lock
);
226 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
227 spin_unlock(&root
->fs_info
->hash_lock
);
228 csum_tree_block(root
, eb
, 0);
230 free_extent_buffer(eb
);
235 static int btree_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
237 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
239 csum_dirty_buffer(root
, page
);
243 static int btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
)
245 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
247 offset
= bio
->bi_sector
<< 9;
248 if (offset
== BTRFS_SUPER_INFO_OFFSET
) {
249 bio
->bi_bdev
= root
->fs_info
->sb
->s_bdev
;
253 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
);
256 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
258 struct extent_io_tree
*tree
;
259 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
260 return extent_write_full_page(tree
, page
, btree_get_extent
, wbc
);
263 static int btree_writepages(struct address_space
*mapping
,
264 struct writeback_control
*wbc
)
266 struct extent_io_tree
*tree
;
267 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
268 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
271 unsigned long thresh
= 96 * 1024 * 1024;
273 if (wbc
->for_kupdate
)
276 if (current_is_pdflush()) {
277 thresh
= 96 * 1024 * 1024;
279 thresh
= 8 * 1024 * 1024;
281 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
282 thresh
, EXTENT_DIRTY
);
283 if (num_dirty
< thresh
) {
287 return extent_writepages(tree
, mapping
, btree_get_extent
, wbc
);
290 int btree_readpage(struct file
*file
, struct page
*page
)
292 struct extent_io_tree
*tree
;
293 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
294 return extent_read_full_page(tree
, page
, btree_get_extent
);
297 static int btree_releasepage(struct page
*page
, gfp_t gfp_flags
)
299 struct extent_io_tree
*tree
;
300 struct extent_map_tree
*map
;
303 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
304 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
305 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
307 ClearPagePrivate(page
);
308 set_page_private(page
, 0);
309 page_cache_release(page
);
314 static void btree_invalidatepage(struct page
*page
, unsigned long offset
)
316 struct extent_io_tree
*tree
;
317 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
318 extent_invalidatepage(tree
, page
, offset
);
319 btree_releasepage(page
, GFP_NOFS
);
323 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
325 struct buffer_head
*bh
;
326 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
327 struct buffer_head
*head
;
328 if (!page_has_buffers(page
)) {
329 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
330 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
332 head
= page_buffers(page
);
335 if (buffer_dirty(bh
))
336 csum_tree_block(root
, bh
, 0);
337 bh
= bh
->b_this_page
;
338 } while (bh
!= head
);
339 return block_write_full_page(page
, btree_get_block
, wbc
);
343 static struct address_space_operations btree_aops
= {
344 .readpage
= btree_readpage
,
345 .writepage
= btree_writepage
,
346 .writepages
= btree_writepages
,
347 .releasepage
= btree_releasepage
,
348 .invalidatepage
= btree_invalidatepage
,
349 .sync_page
= block_sync_page
,
352 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
)
354 struct extent_buffer
*buf
= NULL
;
355 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
358 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
361 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
362 buf
, 0, 0, btree_get_extent
);
363 free_extent_buffer(buf
);
367 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
369 struct list_head
*list
;
370 struct list_head
*next
;
371 struct btrfs_device
*device
;
373 list
= &fs_info
->fs_devices
->devices
;
374 list_for_each(next
, list
) {
375 device
= list_entry(next
, struct btrfs_device
, dev_list
);
376 if (device
->bdev
&& device
->bdev
!= fs_info
->sb
->s_bdev
)
377 close_bdev_excl(device
->bdev
);
383 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
386 struct extent_buffer
*buf
= NULL
;
387 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
388 struct extent_io_tree
*io_tree
;
392 io_tree
= &BTRFS_I(btree_inode
)->io_tree
;
394 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
397 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
, buf
, 0, 1,
400 if (buf
->flags
& EXTENT_CSUM
)
403 end
= buf
->start
+ PAGE_CACHE_SIZE
- 1;
404 if (test_range_bit(io_tree
, buf
->start
, end
, EXTENT_CSUM
, 1)) {
405 buf
->flags
|= EXTENT_CSUM
;
409 lock_extent(io_tree
, buf
->start
, end
, GFP_NOFS
);
411 if (test_range_bit(io_tree
, buf
->start
, end
, EXTENT_CSUM
, 1)) {
412 buf
->flags
|= EXTENT_CSUM
;
416 ret
= csum_tree_block(root
, buf
, 1);
417 set_extent_bits(io_tree
, buf
->start
, end
, EXTENT_CSUM
, GFP_NOFS
);
418 buf
->flags
|= EXTENT_CSUM
;
421 unlock_extent(io_tree
, buf
->start
, end
, GFP_NOFS
);
425 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
426 struct extent_buffer
*buf
)
428 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
429 if (btrfs_header_generation(buf
) ==
430 root
->fs_info
->running_transaction
->transid
)
431 clear_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
,
436 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
437 struct extent_buffer
*buf
)
439 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
440 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode
)->io_tree
,
445 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
446 u32 stripesize
, struct btrfs_root
*root
,
447 struct btrfs_fs_info
*fs_info
,
452 root
->commit_root
= NULL
;
453 root
->sectorsize
= sectorsize
;
454 root
->nodesize
= nodesize
;
455 root
->leafsize
= leafsize
;
456 root
->stripesize
= stripesize
;
458 root
->track_dirty
= 0;
460 root
->fs_info
= fs_info
;
461 root
->objectid
= objectid
;
462 root
->last_trans
= 0;
463 root
->highest_inode
= 0;
464 root
->last_inode_alloc
= 0;
468 INIT_LIST_HEAD(&root
->dirty_list
);
469 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
470 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
471 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
472 memset(&root
->root_kobj
, 0, sizeof(root
->root_kobj
));
473 init_completion(&root
->kobj_unregister
);
474 root
->defrag_running
= 0;
475 root
->defrag_level
= 0;
476 root
->root_key
.objectid
= objectid
;
480 static int find_and_setup_root(struct btrfs_root
*tree_root
,
481 struct btrfs_fs_info
*fs_info
,
483 struct btrfs_root
*root
)
488 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
489 tree_root
->sectorsize
, tree_root
->stripesize
,
490 root
, fs_info
, objectid
);
491 ret
= btrfs_find_last_root(tree_root
, objectid
,
492 &root
->root_item
, &root
->root_key
);
495 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
496 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
502 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_fs_info
*fs_info
,
503 struct btrfs_key
*location
)
505 struct btrfs_root
*root
;
506 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
507 struct btrfs_path
*path
;
508 struct extent_buffer
*l
;
513 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
515 return ERR_PTR(-ENOMEM
);
516 if (location
->offset
== (u64
)-1) {
517 ret
= find_and_setup_root(tree_root
, fs_info
,
518 location
->objectid
, root
);
526 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
527 tree_root
->sectorsize
, tree_root
->stripesize
,
528 root
, fs_info
, location
->objectid
);
530 path
= btrfs_alloc_path();
532 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
539 read_extent_buffer(l
, &root
->root_item
,
540 btrfs_item_ptr_offset(l
, path
->slots
[0]),
541 sizeof(root
->root_item
));
542 memcpy(&root
->root_key
, location
, sizeof(*location
));
545 btrfs_release_path(root
, path
);
546 btrfs_free_path(path
);
551 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
552 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
557 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
559 root
->highest_inode
= highest_inode
;
560 root
->last_inode_alloc
= highest_inode
;
565 struct btrfs_root
*btrfs_lookup_fs_root(struct btrfs_fs_info
*fs_info
,
568 struct btrfs_root
*root
;
570 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
)
571 return fs_info
->tree_root
;
572 if (root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
573 return fs_info
->extent_root
;
575 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
576 (unsigned long)root_objectid
);
580 struct btrfs_root
*btrfs_read_fs_root_no_name(struct btrfs_fs_info
*fs_info
,
581 struct btrfs_key
*location
)
583 struct btrfs_root
*root
;
586 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
587 return fs_info
->tree_root
;
588 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
589 return fs_info
->extent_root
;
591 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
592 (unsigned long)location
->objectid
);
596 root
= btrfs_read_fs_root_no_radix(fs_info
, location
);
599 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
600 (unsigned long)root
->root_key
.objectid
,
603 free_extent_buffer(root
->node
);
607 ret
= btrfs_find_dead_roots(fs_info
->tree_root
,
608 root
->root_key
.objectid
, root
);
614 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
615 struct btrfs_key
*location
,
616 const char *name
, int namelen
)
618 struct btrfs_root
*root
;
621 root
= btrfs_read_fs_root_no_name(fs_info
, location
);
628 ret
= btrfs_set_root_name(root
, name
, namelen
);
630 free_extent_buffer(root
->node
);
635 ret
= btrfs_sysfs_add_root(root
);
637 free_extent_buffer(root
->node
);
646 static int add_hasher(struct btrfs_fs_info
*info
, char *type
) {
647 struct btrfs_hasher
*hasher
;
649 hasher
= kmalloc(sizeof(*hasher
), GFP_NOFS
);
652 hasher
->hash_tfm
= crypto_alloc_hash(type
, 0, CRYPTO_ALG_ASYNC
);
653 if (!hasher
->hash_tfm
) {
657 spin_lock(&info
->hash_lock
);
658 list_add(&hasher
->list
, &info
->hashers
);
659 spin_unlock(&info
->hash_lock
);
664 static int btrfs_congested_fn(void *congested_data
, int bdi_bits
)
666 struct btrfs_fs_info
*info
= (struct btrfs_fs_info
*)congested_data
;
668 struct list_head
*cur
;
669 struct btrfs_device
*device
;
670 struct backing_dev_info
*bdi
;
672 list_for_each(cur
, &info
->fs_devices
->devices
) {
673 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
674 bdi
= blk_get_backing_dev_info(device
->bdev
);
675 if (bdi
&& bdi_congested(bdi
, bdi_bits
)) {
683 void btrfs_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
685 struct list_head
*cur
;
686 struct btrfs_device
*device
;
687 struct btrfs_fs_info
*info
;
689 info
= (struct btrfs_fs_info
*)bdi
->unplug_io_data
;
690 list_for_each(cur
, &info
->fs_devices
->devices
) {
691 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
692 bdi
= blk_get_backing_dev_info(device
->bdev
);
693 if (bdi
->unplug_io_fn
) {
694 bdi
->unplug_io_fn(bdi
, page
);
699 static int setup_bdi(struct btrfs_fs_info
*info
, struct backing_dev_info
*bdi
)
702 bdi
->ra_pages
= default_backing_dev_info
.ra_pages
* 4;
704 bdi
->capabilities
= default_backing_dev_info
.capabilities
;
705 bdi
->unplug_io_fn
= btrfs_unplug_io_fn
;
706 bdi
->unplug_io_data
= info
;
707 bdi
->congested_fn
= btrfs_congested_fn
;
708 bdi
->congested_data
= info
;
712 struct btrfs_root
*open_ctree(struct super_block
*sb
,
713 struct btrfs_fs_devices
*fs_devices
)
720 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
722 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
724 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
726 struct btrfs_root
*chunk_root
= kmalloc(sizeof(struct btrfs_root
),
728 struct btrfs_root
*dev_root
= kmalloc(sizeof(struct btrfs_root
),
732 struct btrfs_super_block
*disk_super
;
734 if (!extent_root
|| !tree_root
|| !fs_info
) {
738 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
739 INIT_LIST_HEAD(&fs_info
->trans_list
);
740 INIT_LIST_HEAD(&fs_info
->dead_roots
);
741 INIT_LIST_HEAD(&fs_info
->hashers
);
742 spin_lock_init(&fs_info
->hash_lock
);
743 spin_lock_init(&fs_info
->delalloc_lock
);
744 spin_lock_init(&fs_info
->new_trans_lock
);
746 memset(&fs_info
->super_kobj
, 0, sizeof(fs_info
->super_kobj
));
747 init_completion(&fs_info
->kobj_unregister
);
748 sb_set_blocksize(sb
, 4096);
749 fs_info
->running_transaction
= NULL
;
750 fs_info
->last_trans_committed
= 0;
751 fs_info
->tree_root
= tree_root
;
752 fs_info
->extent_root
= extent_root
;
753 fs_info
->chunk_root
= chunk_root
;
754 fs_info
->dev_root
= dev_root
;
755 fs_info
->fs_devices
= fs_devices
;
756 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
757 INIT_LIST_HEAD(&fs_info
->space_info
);
758 btrfs_mapping_init(&fs_info
->mapping_tree
);
760 fs_info
->throttles
= 0;
761 fs_info
->mount_opt
= 0;
762 fs_info
->max_extent
= (u64
)-1;
763 fs_info
->max_inline
= 8192 * 1024;
764 fs_info
->delalloc_bytes
= 0;
765 setup_bdi(fs_info
, &fs_info
->bdi
);
766 fs_info
->btree_inode
= new_inode(sb
);
767 fs_info
->btree_inode
->i_ino
= 1;
768 fs_info
->btree_inode
->i_nlink
= 1;
769 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
770 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
771 fs_info
->btree_inode
->i_mapping
->backing_dev_info
= &fs_info
->bdi
;
773 extent_io_tree_init(&BTRFS_I(fs_info
->btree_inode
)->io_tree
,
774 fs_info
->btree_inode
->i_mapping
,
776 extent_map_tree_init(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
,
779 BTRFS_I(fs_info
->btree_inode
)->io_tree
.ops
= &btree_extent_io_ops
;
781 extent_io_tree_init(&fs_info
->free_space_cache
,
782 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
783 extent_io_tree_init(&fs_info
->block_group_cache
,
784 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
785 extent_io_tree_init(&fs_info
->pinned_extents
,
786 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
787 extent_io_tree_init(&fs_info
->pending_del
,
788 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
789 extent_io_tree_init(&fs_info
->extent_ins
,
790 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
791 fs_info
->do_barriers
= 1;
792 fs_info
->closing
= 0;
793 fs_info
->total_pinned
= 0;
794 fs_info
->last_alloc
= 0;
795 fs_info
->last_data_alloc
= 0;
796 fs_info
->extra_alloc_bits
= 0;
797 fs_info
->extra_data_alloc_bits
= 0;
799 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
800 INIT_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
, fs_info
);
802 INIT_DELAYED_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
);
804 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
805 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
806 sizeof(struct btrfs_key
));
807 insert_inode_hash(fs_info
->btree_inode
);
808 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
810 mutex_init(&fs_info
->trans_mutex
);
811 mutex_init(&fs_info
->fs_mutex
);
814 ret
= add_hasher(fs_info
, "crc32c");
816 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
821 __setup_root(4096, 4096, 4096, 4096, tree_root
,
822 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
824 fs_info
->sb_buffer
= read_tree_block(tree_root
,
825 BTRFS_SUPER_INFO_OFFSET
,
828 if (!fs_info
->sb_buffer
)
831 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
832 sizeof(fs_info
->super_copy
));
834 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
835 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
838 disk_super
= &fs_info
->super_copy
;
839 if (!btrfs_super_root(disk_super
))
842 if (btrfs_super_num_devices(disk_super
) != fs_devices
->num_devices
) {
843 printk("Btrfs: wanted %llu devices, but found %llu\n",
844 (unsigned long long)btrfs_super_num_devices(disk_super
),
845 (unsigned long long)fs_devices
->num_devices
);
848 nodesize
= btrfs_super_nodesize(disk_super
);
849 leafsize
= btrfs_super_leafsize(disk_super
);
850 sectorsize
= btrfs_super_sectorsize(disk_super
);
851 stripesize
= btrfs_super_stripesize(disk_super
);
852 tree_root
->nodesize
= nodesize
;
853 tree_root
->leafsize
= leafsize
;
854 tree_root
->sectorsize
= sectorsize
;
855 tree_root
->stripesize
= stripesize
;
856 sb_set_blocksize(sb
, sectorsize
);
858 i_size_write(fs_info
->btree_inode
,
859 btrfs_super_total_bytes(disk_super
));
861 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
862 sizeof(disk_super
->magic
))) {
863 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
867 mutex_lock(&fs_info
->fs_mutex
);
869 ret
= btrfs_read_sys_array(tree_root
);
872 blocksize
= btrfs_level_size(tree_root
,
873 btrfs_super_chunk_root_level(disk_super
));
875 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
876 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
878 chunk_root
->node
= read_tree_block(chunk_root
,
879 btrfs_super_chunk_root(disk_super
),
881 BUG_ON(!chunk_root
->node
);
883 ret
= btrfs_read_chunk_tree(chunk_root
);
886 blocksize
= btrfs_level_size(tree_root
,
887 btrfs_super_root_level(disk_super
));
890 tree_root
->node
= read_tree_block(tree_root
,
891 btrfs_super_root(disk_super
),
893 if (!tree_root
->node
)
897 ret
= find_and_setup_root(tree_root
, fs_info
,
898 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
901 extent_root
->track_dirty
= 1;
903 ret
= find_and_setup_root(tree_root
, fs_info
,
904 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
905 dev_root
->track_dirty
= 1;
908 goto fail_extent_root
;
910 btrfs_read_block_groups(extent_root
);
912 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
913 mutex_unlock(&fs_info
->fs_mutex
);
917 free_extent_buffer(extent_root
->node
);
919 mutex_unlock(&fs_info
->fs_mutex
);
920 free_extent_buffer(tree_root
->node
);
922 free_extent_buffer(fs_info
->sb_buffer
);
923 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
925 iput(fs_info
->btree_inode
);
927 close_all_devices(fs_info
);
930 bdi_destroy(&fs_info
->bdi
);
935 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
939 struct extent_buffer
*super
= root
->fs_info
->sb_buffer
;
940 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
941 struct super_block
*sb
= root
->fs_info
->sb
;
943 if (!btrfs_test_opt(root
, NOBARRIER
))
944 blkdev_issue_flush(sb
->s_bdev
, NULL
);
945 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, super
);
946 ret
= sync_page_range_nolock(btree_inode
, btree_inode
->i_mapping
,
947 super
->start
, super
->len
);
948 if (!btrfs_test_opt(root
, NOBARRIER
))
949 blkdev_issue_flush(sb
->s_bdev
, NULL
);
953 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
955 radix_tree_delete(&fs_info
->fs_roots_radix
,
956 (unsigned long)root
->root_key
.objectid
);
958 btrfs_sysfs_del_root(root
);
962 free_extent_buffer(root
->node
);
963 if (root
->commit_root
)
964 free_extent_buffer(root
->commit_root
);
971 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
974 struct btrfs_root
*gang
[8];
978 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
983 for (i
= 0; i
< ret
; i
++)
984 btrfs_free_fs_root(fs_info
, gang
[i
]);
989 int close_ctree(struct btrfs_root
*root
)
992 struct btrfs_trans_handle
*trans
;
993 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
995 fs_info
->closing
= 1;
996 btrfs_transaction_flush_work(root
);
997 mutex_lock(&fs_info
->fs_mutex
);
998 btrfs_defrag_dirty_roots(root
->fs_info
);
999 trans
= btrfs_start_transaction(root
, 1);
1000 ret
= btrfs_commit_transaction(trans
, root
);
1001 /* run commit again to drop the original snapshot */
1002 trans
= btrfs_start_transaction(root
, 1);
1003 btrfs_commit_transaction(trans
, root
);
1004 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
1006 write_ctree_super(NULL
, root
);
1007 mutex_unlock(&fs_info
->fs_mutex
);
1009 if (fs_info
->delalloc_bytes
) {
1010 printk("btrfs: at unmount delalloc count %Lu\n",
1011 fs_info
->delalloc_bytes
);
1013 if (fs_info
->extent_root
->node
)
1014 free_extent_buffer(fs_info
->extent_root
->node
);
1016 if (fs_info
->tree_root
->node
)
1017 free_extent_buffer(fs_info
->tree_root
->node
);
1019 if (root
->fs_info
->chunk_root
->node
);
1020 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
1022 if (root
->fs_info
->dev_root
->node
);
1023 free_extent_buffer(root
->fs_info
->dev_root
->node
);
1025 free_extent_buffer(fs_info
->sb_buffer
);
1027 btrfs_free_block_groups(root
->fs_info
);
1028 del_fs_roots(fs_info
);
1030 filemap_write_and_wait(fs_info
->btree_inode
->i_mapping
);
1032 extent_io_tree_empty_lru(&fs_info
->free_space_cache
);
1033 extent_io_tree_empty_lru(&fs_info
->block_group_cache
);
1034 extent_io_tree_empty_lru(&fs_info
->pinned_extents
);
1035 extent_io_tree_empty_lru(&fs_info
->pending_del
);
1036 extent_io_tree_empty_lru(&fs_info
->extent_ins
);
1037 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
1039 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
1041 iput(fs_info
->btree_inode
);
1043 while(!list_empty(&fs_info
->hashers
)) {
1044 struct btrfs_hasher
*hasher
;
1045 hasher
= list_entry(fs_info
->hashers
.next
, struct btrfs_hasher
,
1047 list_del(&hasher
->hashers
);
1048 crypto_free_hash(&fs_info
->hash_tfm
);
1052 close_all_devices(fs_info
);
1053 btrfs_mapping_tree_free(&fs_info
->mapping_tree
);
1054 bdi_destroy(&fs_info
->bdi
);
1056 kfree(fs_info
->extent_root
);
1057 kfree(fs_info
->tree_root
);
1058 kfree(fs_info
->chunk_root
);
1059 kfree(fs_info
->dev_root
);
1063 int btrfs_buffer_uptodate(struct extent_buffer
*buf
)
1065 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1066 return extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1069 int btrfs_set_buffer_uptodate(struct extent_buffer
*buf
)
1071 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1072 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
,
1076 void btrfs_mark_buffer_dirty(struct extent_buffer
*buf
)
1078 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1079 u64 transid
= btrfs_header_generation(buf
);
1080 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1082 if (transid
!= root
->fs_info
->generation
) {
1083 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
1084 (unsigned long long)buf
->start
,
1085 transid
, root
->fs_info
->generation
);
1088 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1091 void btrfs_throttle(struct btrfs_root
*root
)
1093 struct backing_dev_info
*bdi
;
1095 bdi
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
1096 if (root
->fs_info
->throttles
&& bdi_write_congested(bdi
)) {
1097 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1098 congestion_wait(WRITE
, HZ
/20);
1100 blk_congestion_wait(WRITE
, HZ
/20);
1105 void btrfs_btree_balance_dirty(struct btrfs_root
*root
, unsigned long nr
)
1107 balance_dirty_pages_ratelimited_nr(
1108 root
->fs_info
->btree_inode
->i_mapping
, 1);
1111 void btrfs_set_buffer_defrag(struct extent_buffer
*buf
)
1113 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1114 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1115 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1116 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, GFP_NOFS
);
1119 void btrfs_set_buffer_defrag_done(struct extent_buffer
*buf
)
1121 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1122 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1123 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1124 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG_DONE
,
1128 int btrfs_buffer_defrag(struct extent_buffer
*buf
)
1130 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1131 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1132 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1133 buf
->start
, buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, 0);
1136 int btrfs_buffer_defrag_done(struct extent_buffer
*buf
)
1138 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1139 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1140 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1141 buf
->start
, buf
->start
+ buf
->len
- 1,
1142 EXTENT_DEFRAG_DONE
, 0);
1145 int btrfs_clear_buffer_defrag_done(struct extent_buffer
*buf
)
1147 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1148 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1149 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1150 buf
->start
, buf
->start
+ buf
->len
- 1,
1151 EXTENT_DEFRAG_DONE
, GFP_NOFS
);
1154 int btrfs_clear_buffer_defrag(struct extent_buffer
*buf
)
1156 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1157 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1158 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1159 buf
->start
, buf
->start
+ buf
->len
- 1,
1160 EXTENT_DEFRAG
, GFP_NOFS
);
1163 int btrfs_read_buffer(struct extent_buffer
*buf
)
1165 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1166 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1167 return read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
1168 buf
, 0, 1, btree_get_extent
);
1171 static struct extent_io_ops btree_extent_io_ops
= {
1172 .writepage_io_hook
= btree_writepage_io_hook
,
1173 .submit_bio_hook
= btree_submit_bio_hook
,
1174 /* note we're sharing with inode.c for the merge bio hook */
1175 .merge_bio_hook
= btrfs_merge_bio_hook
,