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 (from_this_trans
== 0 &&
163 memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
166 memcpy(&found
, result
, BTRFS_CRC32_SIZE
);
168 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\n",
171 root
->fs_info
->sb
->s_id
,
172 buf
->start
, val
, found
, from_this_trans
);
176 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
182 int csum_dirty_buffer(struct btrfs_root
*root
, struct page
*page
)
184 struct extent_io_tree
*tree
;
185 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
189 struct extent_buffer
*eb
;
190 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
192 if (page
->private == EXTENT_PAGE_PRIVATE
)
196 len
= page
->private >> 2;
200 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
201 read_extent_buffer_pages(tree
, eb
, start
+ PAGE_CACHE_SIZE
, 1,
203 btrfs_clear_buffer_defrag(eb
);
204 found_start
= btrfs_header_bytenr(eb
);
205 if (found_start
!= start
) {
206 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
207 start
, found_start
, len
);
211 if (eb
->first_page
!= page
) {
212 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
217 if (!PageUptodate(page
)) {
218 printk("csum not up to date page %lu\n", page
->index
);
222 found_level
= btrfs_header_level(eb
);
223 csum_tree_block(root
, eb
, 0);
225 free_extent_buffer(eb
);
230 static int btree_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
232 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
234 csum_dirty_buffer(root
, page
);
238 static int btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
)
240 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
242 offset
= bio
->bi_sector
<< 9;
243 if (offset
== BTRFS_SUPER_INFO_OFFSET
) {
244 bio
->bi_bdev
= root
->fs_info
->sb
->s_bdev
;
248 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
);
251 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
253 struct extent_io_tree
*tree
;
254 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
255 return extent_write_full_page(tree
, page
, btree_get_extent
, wbc
);
258 static int btree_writepages(struct address_space
*mapping
,
259 struct writeback_control
*wbc
)
261 struct extent_io_tree
*tree
;
262 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
263 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
266 unsigned long thresh
= 96 * 1024 * 1024;
268 if (wbc
->for_kupdate
)
271 if (current_is_pdflush()) {
272 thresh
= 96 * 1024 * 1024;
274 thresh
= 8 * 1024 * 1024;
276 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
277 thresh
, EXTENT_DIRTY
);
278 if (num_dirty
< thresh
) {
282 return extent_writepages(tree
, mapping
, btree_get_extent
, wbc
);
285 int btree_readpage(struct file
*file
, struct page
*page
)
287 struct extent_io_tree
*tree
;
288 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
289 return extent_read_full_page(tree
, page
, btree_get_extent
);
292 static int btree_releasepage(struct page
*page
, gfp_t gfp_flags
)
294 struct extent_io_tree
*tree
;
295 struct extent_map_tree
*map
;
298 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
299 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
300 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
302 ClearPagePrivate(page
);
303 set_page_private(page
, 0);
304 page_cache_release(page
);
309 static void btree_invalidatepage(struct page
*page
, unsigned long offset
)
311 struct extent_io_tree
*tree
;
312 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
313 extent_invalidatepage(tree
, page
, offset
);
314 btree_releasepage(page
, GFP_NOFS
);
318 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
320 struct buffer_head
*bh
;
321 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
322 struct buffer_head
*head
;
323 if (!page_has_buffers(page
)) {
324 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
325 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
327 head
= page_buffers(page
);
330 if (buffer_dirty(bh
))
331 csum_tree_block(root
, bh
, 0);
332 bh
= bh
->b_this_page
;
333 } while (bh
!= head
);
334 return block_write_full_page(page
, btree_get_block
, wbc
);
338 static struct address_space_operations btree_aops
= {
339 .readpage
= btree_readpage
,
340 .writepage
= btree_writepage
,
341 .writepages
= btree_writepages
,
342 .releasepage
= btree_releasepage
,
343 .invalidatepage
= btree_invalidatepage
,
344 .sync_page
= block_sync_page
,
347 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
)
349 struct extent_buffer
*buf
= NULL
;
350 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
353 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
356 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
357 buf
, 0, 0, btree_get_extent
);
358 free_extent_buffer(buf
);
362 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
364 struct list_head
*list
;
365 struct list_head
*next
;
366 struct btrfs_device
*device
;
368 list
= &fs_info
->fs_devices
->devices
;
369 list_for_each(next
, list
) {
370 device
= list_entry(next
, struct btrfs_device
, dev_list
);
371 if (device
->bdev
&& device
->bdev
!= fs_info
->sb
->s_bdev
)
372 close_bdev_excl(device
->bdev
);
378 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
381 struct extent_buffer
*buf
= NULL
;
382 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
383 struct extent_io_tree
*io_tree
;
387 io_tree
= &BTRFS_I(btree_inode
)->io_tree
;
389 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
392 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
, buf
, 0, 1,
395 if (buf
->flags
& EXTENT_CSUM
)
398 end
= buf
->start
+ PAGE_CACHE_SIZE
- 1;
399 if (test_range_bit(io_tree
, buf
->start
, end
, EXTENT_CSUM
, 1)) {
400 buf
->flags
|= EXTENT_CSUM
;
404 lock_extent(io_tree
, buf
->start
, end
, GFP_NOFS
);
406 if (test_range_bit(io_tree
, buf
->start
, end
, EXTENT_CSUM
, 1)) {
407 buf
->flags
|= EXTENT_CSUM
;
411 ret
= csum_tree_block(root
, buf
, 1);
412 set_extent_bits(io_tree
, buf
->start
, end
, EXTENT_CSUM
, GFP_NOFS
);
413 buf
->flags
|= EXTENT_CSUM
;
416 unlock_extent(io_tree
, buf
->start
, end
, GFP_NOFS
);
420 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
421 struct extent_buffer
*buf
)
423 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
424 if (btrfs_header_generation(buf
) ==
425 root
->fs_info
->running_transaction
->transid
)
426 clear_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
,
431 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
432 struct extent_buffer
*buf
)
434 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
435 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode
)->io_tree
,
440 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
441 u32 stripesize
, struct btrfs_root
*root
,
442 struct btrfs_fs_info
*fs_info
,
447 root
->commit_root
= NULL
;
448 root
->sectorsize
= sectorsize
;
449 root
->nodesize
= nodesize
;
450 root
->leafsize
= leafsize
;
451 root
->stripesize
= stripesize
;
453 root
->track_dirty
= 0;
455 root
->fs_info
= fs_info
;
456 root
->objectid
= objectid
;
457 root
->last_trans
= 0;
458 root
->highest_inode
= 0;
459 root
->last_inode_alloc
= 0;
463 INIT_LIST_HEAD(&root
->dirty_list
);
464 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
465 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
466 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
467 memset(&root
->root_kobj
, 0, sizeof(root
->root_kobj
));
468 init_completion(&root
->kobj_unregister
);
469 root
->defrag_running
= 0;
470 root
->defrag_level
= 0;
471 root
->root_key
.objectid
= objectid
;
475 static int find_and_setup_root(struct btrfs_root
*tree_root
,
476 struct btrfs_fs_info
*fs_info
,
478 struct btrfs_root
*root
)
483 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
484 tree_root
->sectorsize
, tree_root
->stripesize
,
485 root
, fs_info
, objectid
);
486 ret
= btrfs_find_last_root(tree_root
, objectid
,
487 &root
->root_item
, &root
->root_key
);
490 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
491 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
497 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_fs_info
*fs_info
,
498 struct btrfs_key
*location
)
500 struct btrfs_root
*root
;
501 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
502 struct btrfs_path
*path
;
503 struct extent_buffer
*l
;
508 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
510 return ERR_PTR(-ENOMEM
);
511 if (location
->offset
== (u64
)-1) {
512 ret
= find_and_setup_root(tree_root
, fs_info
,
513 location
->objectid
, root
);
521 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
522 tree_root
->sectorsize
, tree_root
->stripesize
,
523 root
, fs_info
, location
->objectid
);
525 path
= btrfs_alloc_path();
527 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
534 read_extent_buffer(l
, &root
->root_item
,
535 btrfs_item_ptr_offset(l
, path
->slots
[0]),
536 sizeof(root
->root_item
));
537 memcpy(&root
->root_key
, location
, sizeof(*location
));
540 btrfs_release_path(root
, path
);
541 btrfs_free_path(path
);
546 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
547 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
552 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
554 root
->highest_inode
= highest_inode
;
555 root
->last_inode_alloc
= highest_inode
;
560 struct btrfs_root
*btrfs_lookup_fs_root(struct btrfs_fs_info
*fs_info
,
563 struct btrfs_root
*root
;
565 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
)
566 return fs_info
->tree_root
;
567 if (root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
568 return fs_info
->extent_root
;
570 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
571 (unsigned long)root_objectid
);
575 struct btrfs_root
*btrfs_read_fs_root_no_name(struct btrfs_fs_info
*fs_info
,
576 struct btrfs_key
*location
)
578 struct btrfs_root
*root
;
581 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
582 return fs_info
->tree_root
;
583 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
584 return fs_info
->extent_root
;
586 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
587 (unsigned long)location
->objectid
);
591 root
= btrfs_read_fs_root_no_radix(fs_info
, location
);
594 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
595 (unsigned long)root
->root_key
.objectid
,
598 free_extent_buffer(root
->node
);
602 ret
= btrfs_find_dead_roots(fs_info
->tree_root
,
603 root
->root_key
.objectid
, root
);
609 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
610 struct btrfs_key
*location
,
611 const char *name
, int namelen
)
613 struct btrfs_root
*root
;
616 root
= btrfs_read_fs_root_no_name(fs_info
, location
);
623 ret
= btrfs_set_root_name(root
, name
, namelen
);
625 free_extent_buffer(root
->node
);
630 ret
= btrfs_sysfs_add_root(root
);
632 free_extent_buffer(root
->node
);
641 static int add_hasher(struct btrfs_fs_info
*info
, char *type
) {
642 struct btrfs_hasher
*hasher
;
644 hasher
= kmalloc(sizeof(*hasher
), GFP_NOFS
);
647 hasher
->hash_tfm
= crypto_alloc_hash(type
, 0, CRYPTO_ALG_ASYNC
);
648 if (!hasher
->hash_tfm
) {
652 spin_lock(&info
->hash_lock
);
653 list_add(&hasher
->list
, &info
->hashers
);
654 spin_unlock(&info
->hash_lock
);
659 static int btrfs_congested_fn(void *congested_data
, int bdi_bits
)
661 struct btrfs_fs_info
*info
= (struct btrfs_fs_info
*)congested_data
;
663 struct list_head
*cur
;
664 struct btrfs_device
*device
;
665 struct backing_dev_info
*bdi
;
667 list_for_each(cur
, &info
->fs_devices
->devices
) {
668 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
669 bdi
= blk_get_backing_dev_info(device
->bdev
);
670 if (bdi
&& bdi_congested(bdi
, bdi_bits
)) {
678 void btrfs_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
680 struct list_head
*cur
;
681 struct btrfs_device
*device
;
682 struct btrfs_fs_info
*info
;
684 info
= (struct btrfs_fs_info
*)bdi
->unplug_io_data
;
685 list_for_each(cur
, &info
->fs_devices
->devices
) {
686 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
687 bdi
= blk_get_backing_dev_info(device
->bdev
);
688 if (bdi
->unplug_io_fn
) {
689 bdi
->unplug_io_fn(bdi
, page
);
694 static int setup_bdi(struct btrfs_fs_info
*info
, struct backing_dev_info
*bdi
)
697 bdi
->ra_pages
= default_backing_dev_info
.ra_pages
* 4;
699 bdi
->capabilities
= default_backing_dev_info
.capabilities
;
700 bdi
->unplug_io_fn
= btrfs_unplug_io_fn
;
701 bdi
->unplug_io_data
= info
;
702 bdi
->congested_fn
= btrfs_congested_fn
;
703 bdi
->congested_data
= info
;
707 struct btrfs_root
*open_ctree(struct super_block
*sb
,
708 struct btrfs_fs_devices
*fs_devices
)
715 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
717 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
719 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
721 struct btrfs_root
*chunk_root
= kmalloc(sizeof(struct btrfs_root
),
723 struct btrfs_root
*dev_root
= kmalloc(sizeof(struct btrfs_root
),
727 struct btrfs_super_block
*disk_super
;
729 if (!extent_root
|| !tree_root
|| !fs_info
) {
733 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
734 INIT_LIST_HEAD(&fs_info
->trans_list
);
735 INIT_LIST_HEAD(&fs_info
->dead_roots
);
736 INIT_LIST_HEAD(&fs_info
->hashers
);
737 spin_lock_init(&fs_info
->hash_lock
);
738 spin_lock_init(&fs_info
->delalloc_lock
);
739 spin_lock_init(&fs_info
->new_trans_lock
);
741 memset(&fs_info
->super_kobj
, 0, sizeof(fs_info
->super_kobj
));
742 init_completion(&fs_info
->kobj_unregister
);
743 sb_set_blocksize(sb
, 4096);
744 fs_info
->running_transaction
= NULL
;
745 fs_info
->last_trans_committed
= 0;
746 fs_info
->tree_root
= tree_root
;
747 fs_info
->extent_root
= extent_root
;
748 fs_info
->chunk_root
= chunk_root
;
749 fs_info
->dev_root
= dev_root
;
750 fs_info
->fs_devices
= fs_devices
;
751 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
752 INIT_LIST_HEAD(&fs_info
->space_info
);
753 btrfs_mapping_init(&fs_info
->mapping_tree
);
755 fs_info
->throttles
= 0;
756 fs_info
->mount_opt
= 0;
757 fs_info
->max_extent
= (u64
)-1;
758 fs_info
->max_inline
= 8192 * 1024;
759 fs_info
->delalloc_bytes
= 0;
760 setup_bdi(fs_info
, &fs_info
->bdi
);
761 fs_info
->btree_inode
= new_inode(sb
);
762 fs_info
->btree_inode
->i_ino
= 1;
763 fs_info
->btree_inode
->i_nlink
= 1;
764 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
765 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
766 fs_info
->btree_inode
->i_mapping
->backing_dev_info
= &fs_info
->bdi
;
768 extent_io_tree_init(&BTRFS_I(fs_info
->btree_inode
)->io_tree
,
769 fs_info
->btree_inode
->i_mapping
,
771 extent_map_tree_init(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
,
774 BTRFS_I(fs_info
->btree_inode
)->io_tree
.ops
= &btree_extent_io_ops
;
776 extent_io_tree_init(&fs_info
->free_space_cache
,
777 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
778 extent_io_tree_init(&fs_info
->block_group_cache
,
779 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
780 extent_io_tree_init(&fs_info
->pinned_extents
,
781 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
782 extent_io_tree_init(&fs_info
->pending_del
,
783 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
784 extent_io_tree_init(&fs_info
->extent_ins
,
785 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
786 fs_info
->do_barriers
= 1;
787 fs_info
->closing
= 0;
788 fs_info
->total_pinned
= 0;
789 fs_info
->last_alloc
= 0;
790 fs_info
->last_data_alloc
= 0;
791 fs_info
->extra_alloc_bits
= 0;
792 fs_info
->extra_data_alloc_bits
= 0;
794 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
795 INIT_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
, fs_info
);
797 INIT_DELAYED_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
);
799 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
800 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
801 sizeof(struct btrfs_key
));
802 insert_inode_hash(fs_info
->btree_inode
);
803 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
805 mutex_init(&fs_info
->trans_mutex
);
806 mutex_init(&fs_info
->fs_mutex
);
809 ret
= add_hasher(fs_info
, "crc32c");
811 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
816 __setup_root(4096, 4096, 4096, 4096, tree_root
,
817 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
819 fs_info
->sb_buffer
= read_tree_block(tree_root
,
820 BTRFS_SUPER_INFO_OFFSET
,
823 if (!fs_info
->sb_buffer
)
826 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
827 sizeof(fs_info
->super_copy
));
829 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
830 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
833 disk_super
= &fs_info
->super_copy
;
834 if (!btrfs_super_root(disk_super
))
837 if (btrfs_super_num_devices(disk_super
) != fs_devices
->num_devices
) {
838 printk("Btrfs: wanted %llu devices, but found %llu\n",
839 (unsigned long long)btrfs_super_num_devices(disk_super
),
840 (unsigned long long)fs_devices
->num_devices
);
843 nodesize
= btrfs_super_nodesize(disk_super
);
844 leafsize
= btrfs_super_leafsize(disk_super
);
845 sectorsize
= btrfs_super_sectorsize(disk_super
);
846 stripesize
= btrfs_super_stripesize(disk_super
);
847 tree_root
->nodesize
= nodesize
;
848 tree_root
->leafsize
= leafsize
;
849 tree_root
->sectorsize
= sectorsize
;
850 tree_root
->stripesize
= stripesize
;
851 sb_set_blocksize(sb
, sectorsize
);
853 i_size_write(fs_info
->btree_inode
,
854 btrfs_super_total_bytes(disk_super
));
856 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
857 sizeof(disk_super
->magic
))) {
858 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
862 mutex_lock(&fs_info
->fs_mutex
);
864 ret
= btrfs_read_sys_array(tree_root
);
867 blocksize
= btrfs_level_size(tree_root
,
868 btrfs_super_chunk_root_level(disk_super
));
870 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
871 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
873 chunk_root
->node
= read_tree_block(chunk_root
,
874 btrfs_super_chunk_root(disk_super
),
876 BUG_ON(!chunk_root
->node
);
878 ret
= btrfs_read_chunk_tree(chunk_root
);
881 blocksize
= btrfs_level_size(tree_root
,
882 btrfs_super_root_level(disk_super
));
885 tree_root
->node
= read_tree_block(tree_root
,
886 btrfs_super_root(disk_super
),
888 if (!tree_root
->node
)
892 ret
= find_and_setup_root(tree_root
, fs_info
,
893 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
896 extent_root
->track_dirty
= 1;
898 ret
= find_and_setup_root(tree_root
, fs_info
,
899 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
900 dev_root
->track_dirty
= 1;
903 goto fail_extent_root
;
905 btrfs_read_block_groups(extent_root
);
907 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
908 mutex_unlock(&fs_info
->fs_mutex
);
912 free_extent_buffer(extent_root
->node
);
914 mutex_unlock(&fs_info
->fs_mutex
);
915 free_extent_buffer(tree_root
->node
);
917 free_extent_buffer(fs_info
->sb_buffer
);
919 iput(fs_info
->btree_inode
);
921 close_all_devices(fs_info
);
928 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
932 struct extent_buffer
*super
= root
->fs_info
->sb_buffer
;
933 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
934 struct super_block
*sb
= root
->fs_info
->sb
;
936 if (!btrfs_test_opt(root
, NOBARRIER
))
937 blkdev_issue_flush(sb
->s_bdev
, NULL
);
938 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, super
);
939 ret
= sync_page_range_nolock(btree_inode
, btree_inode
->i_mapping
,
940 super
->start
, super
->len
);
941 if (!btrfs_test_opt(root
, NOBARRIER
))
942 blkdev_issue_flush(sb
->s_bdev
, NULL
);
946 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
948 radix_tree_delete(&fs_info
->fs_roots_radix
,
949 (unsigned long)root
->root_key
.objectid
);
951 btrfs_sysfs_del_root(root
);
955 free_extent_buffer(root
->node
);
956 if (root
->commit_root
)
957 free_extent_buffer(root
->commit_root
);
964 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
967 struct btrfs_root
*gang
[8];
971 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
976 for (i
= 0; i
< ret
; i
++)
977 btrfs_free_fs_root(fs_info
, gang
[i
]);
982 int close_ctree(struct btrfs_root
*root
)
985 struct btrfs_trans_handle
*trans
;
986 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
988 fs_info
->closing
= 1;
989 btrfs_transaction_flush_work(root
);
990 mutex_lock(&fs_info
->fs_mutex
);
991 btrfs_defrag_dirty_roots(root
->fs_info
);
992 trans
= btrfs_start_transaction(root
, 1);
993 ret
= btrfs_commit_transaction(trans
, root
);
994 /* run commit again to drop the original snapshot */
995 trans
= btrfs_start_transaction(root
, 1);
996 btrfs_commit_transaction(trans
, root
);
997 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
999 write_ctree_super(NULL
, root
);
1000 mutex_unlock(&fs_info
->fs_mutex
);
1002 if (fs_info
->delalloc_bytes
) {
1003 printk("btrfs: at unmount delalloc count %Lu\n",
1004 fs_info
->delalloc_bytes
);
1006 if (fs_info
->extent_root
->node
)
1007 free_extent_buffer(fs_info
->extent_root
->node
);
1009 if (fs_info
->tree_root
->node
)
1010 free_extent_buffer(fs_info
->tree_root
->node
);
1012 if (root
->fs_info
->chunk_root
->node
);
1013 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
1015 if (root
->fs_info
->dev_root
->node
);
1016 free_extent_buffer(root
->fs_info
->dev_root
->node
);
1018 free_extent_buffer(fs_info
->sb_buffer
);
1020 btrfs_free_block_groups(root
->fs_info
);
1021 del_fs_roots(fs_info
);
1023 filemap_write_and_wait(fs_info
->btree_inode
->i_mapping
);
1025 extent_io_tree_empty_lru(&fs_info
->free_space_cache
);
1026 extent_io_tree_empty_lru(&fs_info
->block_group_cache
);
1027 extent_io_tree_empty_lru(&fs_info
->pinned_extents
);
1028 extent_io_tree_empty_lru(&fs_info
->pending_del
);
1029 extent_io_tree_empty_lru(&fs_info
->extent_ins
);
1030 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
1032 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
1034 iput(fs_info
->btree_inode
);
1036 while(!list_empty(&fs_info
->hashers
)) {
1037 struct btrfs_hasher
*hasher
;
1038 hasher
= list_entry(fs_info
->hashers
.next
, struct btrfs_hasher
,
1040 list_del(&hasher
->hashers
);
1041 crypto_free_hash(&fs_info
->hash_tfm
);
1045 close_all_devices(fs_info
);
1046 btrfs_mapping_tree_free(&fs_info
->mapping_tree
);
1047 bdi_destroy(&fs_info
->bdi
);
1049 kfree(fs_info
->extent_root
);
1050 kfree(fs_info
->tree_root
);
1051 kfree(fs_info
->chunk_root
);
1052 kfree(fs_info
->dev_root
);
1056 int btrfs_buffer_uptodate(struct extent_buffer
*buf
)
1058 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1059 return extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1062 int btrfs_set_buffer_uptodate(struct extent_buffer
*buf
)
1064 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1065 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
,
1069 void btrfs_mark_buffer_dirty(struct extent_buffer
*buf
)
1071 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1072 u64 transid
= btrfs_header_generation(buf
);
1073 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1075 if (transid
!= root
->fs_info
->generation
) {
1076 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
1077 (unsigned long long)buf
->start
,
1078 transid
, root
->fs_info
->generation
);
1081 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1084 void btrfs_throttle(struct btrfs_root
*root
)
1086 struct backing_dev_info
*bdi
;
1088 bdi
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
1089 if (root
->fs_info
->throttles
&& bdi_write_congested(bdi
)) {
1090 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1091 congestion_wait(WRITE
, HZ
/20);
1093 blk_congestion_wait(WRITE
, HZ
/20);
1098 void btrfs_btree_balance_dirty(struct btrfs_root
*root
, unsigned long nr
)
1100 balance_dirty_pages_ratelimited_nr(
1101 root
->fs_info
->btree_inode
->i_mapping
, 1);
1104 void btrfs_set_buffer_defrag(struct extent_buffer
*buf
)
1106 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1107 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1108 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1109 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, GFP_NOFS
);
1112 void btrfs_set_buffer_defrag_done(struct extent_buffer
*buf
)
1114 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1115 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1116 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1117 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG_DONE
,
1121 int btrfs_buffer_defrag(struct extent_buffer
*buf
)
1123 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1124 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1125 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1126 buf
->start
, buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, 0);
1129 int btrfs_buffer_defrag_done(struct extent_buffer
*buf
)
1131 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1132 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1133 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1134 buf
->start
, buf
->start
+ buf
->len
- 1,
1135 EXTENT_DEFRAG_DONE
, 0);
1138 int btrfs_clear_buffer_defrag_done(struct extent_buffer
*buf
)
1140 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1141 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1142 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1143 buf
->start
, buf
->start
+ buf
->len
- 1,
1144 EXTENT_DEFRAG_DONE
, GFP_NOFS
);
1147 int btrfs_clear_buffer_defrag(struct extent_buffer
*buf
)
1149 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1150 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1151 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1152 buf
->start
, buf
->start
+ buf
->len
- 1,
1153 EXTENT_DEFRAG
, GFP_NOFS
);
1156 int btrfs_read_buffer(struct extent_buffer
*buf
)
1158 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1159 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1160 return read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
1161 buf
, 0, 1, btree_get_extent
);
1164 static struct extent_io_ops btree_extent_io_ops
= {
1165 .writepage_io_hook
= btree_writepage_io_hook
,
1166 .submit_bio_hook
= btree_submit_bio_hook
,
1167 /* note we're sharing with inode.c for the merge bio hook */
1168 .merge_bio_hook
= btrfs_merge_bio_hook
,