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"
31 #include "print-tree.h"
34 static int check_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
)
36 if (extent_buffer_blocknr(buf
) != btrfs_header_blocknr(buf
)) {
37 printk(KERN_CRIT
"buf blocknr(buf) is %llu, header is %llu\n",
38 (unsigned long long)extent_buffer_blocknr(buf
),
39 (unsigned long long)btrfs_header_blocknr(buf
));
46 static struct extent_map_ops btree_extent_map_ops
;
48 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
49 u64 bytenr
, u32 blocksize
)
51 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
52 struct extent_buffer
*eb
;
53 eb
= find_extent_buffer(&BTRFS_I(btree_inode
)->extent_tree
,
54 bytenr
, blocksize
, GFP_NOFS
);
58 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
59 u64 bytenr
, u32 blocksize
)
61 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
62 struct extent_buffer
*eb
;
64 eb
= alloc_extent_buffer(&BTRFS_I(btree_inode
)->extent_tree
,
65 bytenr
, blocksize
, NULL
, GFP_NOFS
);
69 struct extent_map
*btree_get_extent(struct inode
*inode
, struct page
*page
,
70 size_t page_offset
, u64 start
, u64 end
,
73 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
74 struct extent_map
*em
;
78 em
= lookup_extent_mapping(em_tree
, start
, end
);
82 em
= alloc_extent_map(GFP_NOFS
);
84 em
= ERR_PTR(-ENOMEM
);
88 em
->end
= (i_size_read(inode
) & ~((u64
)PAGE_CACHE_SIZE
-1)) - 1;
90 em
->block_end
= em
->end
;
91 em
->bdev
= inode
->i_sb
->s_bdev
;
92 ret
= add_extent_mapping(em_tree
, em
);
104 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
106 return crc32c(seed
, data
, len
);
109 void btrfs_csum_final(u32 crc
, char *result
)
111 *(__le32
*)result
= ~cpu_to_le32(crc
);
114 static int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
117 char result
[BTRFS_CRC32_SIZE
];
119 unsigned long cur_len
;
120 unsigned long offset
= BTRFS_CSUM_SIZE
;
121 char *map_token
= NULL
;
123 unsigned long map_start
;
124 unsigned long map_len
;
128 len
= buf
->len
- offset
;
130 err
= map_private_extent_buffer(buf
, offset
, 32,
132 &map_start
, &map_len
, KM_USER0
);
134 printk("failed to map extent buffer! %lu\n",
138 cur_len
= min(len
, map_len
- (offset
- map_start
));
139 crc
= btrfs_csum_data(root
, kaddr
+ offset
- map_start
,
143 unmap_extent_buffer(buf
, map_token
, KM_USER0
);
145 btrfs_csum_final(crc
, result
);
148 int from_this_trans
= 0;
150 if (root
->fs_info
->running_transaction
&&
151 btrfs_header_generation(buf
) ==
152 root
->fs_info
->running_transaction
->transid
)
155 /* FIXME, this is not good */
156 if (from_this_trans
== 0 &&
157 memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
160 memcpy(&found
, result
, BTRFS_CRC32_SIZE
);
162 read_extent_buffer(buf
, &val
, 0, BTRFS_CRC32_SIZE
);
163 printk("btrfs: %s checksum verify failed on %llu "
164 "wanted %X found %X from_this_trans %d\n",
165 root
->fs_info
->sb
->s_id
,
166 buf
->start
, val
, found
, from_this_trans
);
170 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
176 int csum_dirty_buffer(struct btrfs_root
*root
, struct page
*page
)
178 struct extent_map_tree
*tree
;
179 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
183 struct extent_buffer
*eb
;
184 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
186 if (page
->private == EXTENT_PAGE_PRIVATE
)
190 len
= page
->private >> 2;
194 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
195 read_extent_buffer_pages(tree
, eb
, start
+ PAGE_CACHE_SIZE
, 1);
196 found_start
= btrfs_header_bytenr(eb
);
197 if (found_start
!= start
) {
198 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
199 start
, found_start
, len
);
203 if (eb
->first_page
!= page
) {
204 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
209 if (!PageUptodate(page
)) {
210 printk("csum not up to date page %lu\n", page
->index
);
214 found_level
= btrfs_header_level(eb
);
215 csum_tree_block(root
, eb
, 0);
217 free_extent_buffer(eb
);
222 static int btree_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
224 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
226 csum_dirty_buffer(root
, page
);
230 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
232 struct extent_map_tree
*tree
;
233 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
234 return extent_write_full_page(tree
, page
, btree_get_extent
, wbc
);
237 static int btree_writepages(struct address_space
*mapping
,
238 struct writeback_control
*wbc
)
240 struct extent_map_tree
*tree
;
241 tree
= &BTRFS_I(mapping
->host
)->extent_tree
;
242 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
245 unsigned long thresh
= 96 * 1024 * 1024;
247 if (wbc
->for_kupdate
)
250 if (current_is_pdflush()) {
251 thresh
= 96 * 1024 * 1024;
253 thresh
= 8 * 1024 * 1024;
255 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
256 thresh
, EXTENT_DIRTY
);
257 if (num_dirty
< thresh
) {
261 return extent_writepages(tree
, mapping
, btree_get_extent
, wbc
);
264 int btree_readpage(struct file
*file
, struct page
*page
)
266 struct extent_map_tree
*tree
;
267 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
268 return extent_read_full_page(tree
, page
, btree_get_extent
);
271 static int btree_releasepage(struct page
*page
, gfp_t unused_gfp_flags
)
273 struct extent_map_tree
*tree
;
276 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
277 ret
= try_release_extent_mapping(tree
, page
);
279 ClearPagePrivate(page
);
280 set_page_private(page
, 0);
281 page_cache_release(page
);
286 static void btree_invalidatepage(struct page
*page
, unsigned long offset
)
288 struct extent_map_tree
*tree
;
289 tree
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
290 extent_invalidatepage(tree
, page
, offset
);
291 btree_releasepage(page
, GFP_NOFS
);
295 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
297 struct buffer_head
*bh
;
298 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
299 struct buffer_head
*head
;
300 if (!page_has_buffers(page
)) {
301 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
302 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
304 head
= page_buffers(page
);
307 if (buffer_dirty(bh
))
308 csum_tree_block(root
, bh
, 0);
309 bh
= bh
->b_this_page
;
310 } while (bh
!= head
);
311 return block_write_full_page(page
, btree_get_block
, wbc
);
315 static struct address_space_operations btree_aops
= {
316 .readpage
= btree_readpage
,
317 .writepage
= btree_writepage
,
318 .writepages
= btree_writepages
,
319 .releasepage
= btree_releasepage
,
320 .invalidatepage
= btree_invalidatepage
,
321 .sync_page
= block_sync_page
,
324 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
)
326 struct extent_buffer
*buf
= NULL
;
327 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
330 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
333 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->extent_tree
,
335 free_extent_buffer(buf
);
339 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
342 struct extent_buffer
*buf
= NULL
;
343 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
344 struct extent_map_tree
*extent_tree
;
348 extent_tree
= &BTRFS_I(btree_inode
)->extent_tree
;
350 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
353 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->extent_tree
,
356 if (buf
->flags
& EXTENT_CSUM
)
359 end
= buf
->start
+ PAGE_CACHE_SIZE
- 1;
360 if (test_range_bit(extent_tree
, buf
->start
, end
, EXTENT_CSUM
, 1)) {
361 buf
->flags
|= EXTENT_CSUM
;
365 lock_extent(extent_tree
, buf
->start
, end
, GFP_NOFS
);
367 if (test_range_bit(extent_tree
, buf
->start
, end
, EXTENT_CSUM
, 1)) {
368 buf
->flags
|= EXTENT_CSUM
;
372 ret
= csum_tree_block(root
, buf
, 1);
373 set_extent_bits(extent_tree
, buf
->start
, end
, EXTENT_CSUM
, GFP_NOFS
);
374 buf
->flags
|= EXTENT_CSUM
;
377 unlock_extent(extent_tree
, buf
->start
, end
, GFP_NOFS
);
381 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
382 struct extent_buffer
*buf
)
384 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
385 if (btrfs_header_generation(buf
) ==
386 root
->fs_info
->running_transaction
->transid
)
387 clear_extent_buffer_dirty(&BTRFS_I(btree_inode
)->extent_tree
,
392 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
393 struct extent_buffer
*buf
)
395 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
396 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode
)->extent_tree
,
401 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
402 u32 stripesize
, struct btrfs_root
*root
,
403 struct btrfs_fs_info
*fs_info
,
408 root
->commit_root
= NULL
;
409 root
->sectorsize
= sectorsize
;
410 root
->nodesize
= nodesize
;
411 root
->leafsize
= leafsize
;
412 root
->stripesize
= stripesize
;
414 root
->fs_info
= fs_info
;
415 root
->objectid
= objectid
;
416 root
->last_trans
= 0;
417 root
->highest_inode
= 0;
418 root
->last_inode_alloc
= 0;
421 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
422 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
423 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
424 memset(&root
->root_kobj
, 0, sizeof(root
->root_kobj
));
425 init_completion(&root
->kobj_unregister
);
426 root
->defrag_running
= 0;
427 root
->defrag_level
= 0;
428 root
->root_key
.objectid
= objectid
;
432 static int find_and_setup_root(struct btrfs_root
*tree_root
,
433 struct btrfs_fs_info
*fs_info
,
435 struct btrfs_root
*root
)
440 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
441 tree_root
->sectorsize
, tree_root
->stripesize
,
442 root
, fs_info
, objectid
);
443 ret
= btrfs_find_last_root(tree_root
, objectid
,
444 &root
->root_item
, &root
->root_key
);
447 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
448 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
454 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_fs_info
*fs_info
,
455 struct btrfs_key
*location
)
457 struct btrfs_root
*root
;
458 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
459 struct btrfs_path
*path
;
460 struct extent_buffer
*l
;
465 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
467 return ERR_PTR(-ENOMEM
);
468 if (location
->offset
== (u64
)-1) {
469 ret
= find_and_setup_root(tree_root
, fs_info
,
470 location
->objectid
, root
);
478 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
479 tree_root
->sectorsize
, tree_root
->stripesize
,
480 root
, fs_info
, location
->objectid
);
482 path
= btrfs_alloc_path();
484 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
491 read_extent_buffer(l
, &root
->root_item
,
492 btrfs_item_ptr_offset(l
, path
->slots
[0]),
493 sizeof(root
->root_item
));
494 memcpy(&root
->root_key
, location
, sizeof(*location
));
497 btrfs_release_path(root
, path
);
498 btrfs_free_path(path
);
503 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
504 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
509 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
511 root
->highest_inode
= highest_inode
;
512 root
->last_inode_alloc
= highest_inode
;
517 struct btrfs_root
*btrfs_lookup_fs_root(struct btrfs_fs_info
*fs_info
,
520 struct btrfs_root
*root
;
522 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
)
523 return fs_info
->tree_root
;
524 if (root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
525 return fs_info
->extent_root
;
527 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
528 (unsigned long)root_objectid
);
532 struct btrfs_root
*btrfs_read_fs_root_no_name(struct btrfs_fs_info
*fs_info
,
533 struct btrfs_key
*location
)
535 struct btrfs_root
*root
;
538 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
539 return fs_info
->tree_root
;
540 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
541 return fs_info
->extent_root
;
543 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
544 (unsigned long)location
->objectid
);
548 root
= btrfs_read_fs_root_no_radix(fs_info
, location
);
551 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
552 (unsigned long)root
->root_key
.objectid
,
555 free_extent_buffer(root
->node
);
559 ret
= btrfs_find_dead_roots(fs_info
->tree_root
,
560 root
->root_key
.objectid
, root
);
566 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
567 struct btrfs_key
*location
,
568 const char *name
, int namelen
)
570 struct btrfs_root
*root
;
573 root
= btrfs_read_fs_root_no_name(fs_info
, location
);
580 ret
= btrfs_set_root_name(root
, name
, namelen
);
582 free_extent_buffer(root
->node
);
587 ret
= btrfs_sysfs_add_root(root
);
589 free_extent_buffer(root
->node
);
598 static int add_hasher(struct btrfs_fs_info
*info
, char *type
) {
599 struct btrfs_hasher
*hasher
;
601 hasher
= kmalloc(sizeof(*hasher
), GFP_NOFS
);
604 hasher
->hash_tfm
= crypto_alloc_hash(type
, 0, CRYPTO_ALG_ASYNC
);
605 if (!hasher
->hash_tfm
) {
609 spin_lock(&info
->hash_lock
);
610 list_add(&hasher
->list
, &info
->hashers
);
611 spin_unlock(&info
->hash_lock
);
615 struct btrfs_root
*open_ctree(struct super_block
*sb
)
622 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
624 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
626 struct btrfs_fs_info
*fs_info
= kmalloc(sizeof(*fs_info
),
630 struct btrfs_super_block
*disk_super
;
632 if (!extent_root
|| !tree_root
|| !fs_info
) {
636 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
637 INIT_LIST_HEAD(&fs_info
->trans_list
);
638 INIT_LIST_HEAD(&fs_info
->dead_roots
);
639 INIT_LIST_HEAD(&fs_info
->hashers
);
640 spin_lock_init(&fs_info
->hash_lock
);
641 spin_lock_init(&fs_info
->delalloc_lock
);
642 spin_lock_init(&fs_info
->new_trans_lock
);
644 memset(&fs_info
->super_kobj
, 0, sizeof(fs_info
->super_kobj
));
645 init_completion(&fs_info
->kobj_unregister
);
646 sb_set_blocksize(sb
, 4096);
647 fs_info
->running_transaction
= NULL
;
648 fs_info
->last_trans_committed
= 0;
649 fs_info
->tree_root
= tree_root
;
650 fs_info
->extent_root
= extent_root
;
652 fs_info
->throttles
= 0;
653 fs_info
->mount_opt
= 0;
654 fs_info
->max_extent
= (u64
)-1;
655 fs_info
->delalloc_bytes
= 0;
656 fs_info
->btree_inode
= new_inode(sb
);
657 fs_info
->btree_inode
->i_ino
= 1;
658 fs_info
->btree_inode
->i_nlink
= 1;
659 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
660 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
661 extent_map_tree_init(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
,
662 fs_info
->btree_inode
->i_mapping
,
664 BTRFS_I(fs_info
->btree_inode
)->extent_tree
.ops
= &btree_extent_map_ops
;
666 extent_map_tree_init(&fs_info
->free_space_cache
,
667 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
668 extent_map_tree_init(&fs_info
->block_group_cache
,
669 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
670 extent_map_tree_init(&fs_info
->pinned_extents
,
671 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
672 extent_map_tree_init(&fs_info
->pending_del
,
673 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
674 extent_map_tree_init(&fs_info
->extent_ins
,
675 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
676 fs_info
->do_barriers
= 1;
677 fs_info
->closing
= 0;
678 fs_info
->total_pinned
= 0;
679 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
680 INIT_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
, fs_info
);
682 INIT_DELAYED_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
);
684 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
685 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
686 sizeof(struct btrfs_key
));
687 insert_inode_hash(fs_info
->btree_inode
);
688 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
690 mutex_init(&fs_info
->trans_mutex
);
691 mutex_init(&fs_info
->fs_mutex
);
694 ret
= add_hasher(fs_info
, "crc32c");
696 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
701 __setup_root(512, 512, 512, 512, tree_root
,
702 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
704 fs_info
->sb_buffer
= read_tree_block(tree_root
,
705 BTRFS_SUPER_INFO_OFFSET
,
708 if (!fs_info
->sb_buffer
)
711 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
712 sizeof(fs_info
->super_copy
));
714 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
715 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
717 disk_super
= &fs_info
->super_copy
;
718 if (!btrfs_super_root(disk_super
))
721 nodesize
= btrfs_super_nodesize(disk_super
);
722 leafsize
= btrfs_super_leafsize(disk_super
);
723 sectorsize
= btrfs_super_sectorsize(disk_super
);
724 stripesize
= btrfs_super_stripesize(disk_super
);
725 tree_root
->nodesize
= nodesize
;
726 tree_root
->leafsize
= leafsize
;
727 tree_root
->sectorsize
= sectorsize
;
728 tree_root
->stripesize
= stripesize
;
729 sb_set_blocksize(sb
, sectorsize
);
731 i_size_write(fs_info
->btree_inode
,
732 btrfs_super_total_bytes(disk_super
));
734 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
735 sizeof(disk_super
->magic
))) {
736 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
740 blocksize
= btrfs_level_size(tree_root
,
741 btrfs_super_root_level(disk_super
));
743 tree_root
->node
= read_tree_block(tree_root
,
744 btrfs_super_root(disk_super
),
746 if (!tree_root
->node
)
749 mutex_lock(&fs_info
->fs_mutex
);
751 ret
= find_and_setup_root(tree_root
, fs_info
,
752 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
754 mutex_unlock(&fs_info
->fs_mutex
);
758 btrfs_read_block_groups(extent_root
);
760 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
761 mutex_unlock(&fs_info
->fs_mutex
);
765 free_extent_buffer(tree_root
->node
);
767 free_extent_buffer(fs_info
->sb_buffer
);
769 iput(fs_info
->btree_inode
);
777 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
781 struct extent_buffer
*super
= root
->fs_info
->sb_buffer
;
782 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
783 struct super_block
*sb
= root
->fs_info
->sb
;
785 if (!btrfs_test_opt(root
, NOBARRIER
))
786 blkdev_issue_flush(sb
->s_bdev
, NULL
);
787 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->extent_tree
, super
);
788 ret
= sync_page_range_nolock(btree_inode
, btree_inode
->i_mapping
,
789 super
->start
, super
->len
);
790 if (!btrfs_test_opt(root
, NOBARRIER
))
791 blkdev_issue_flush(sb
->s_bdev
, NULL
);
795 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
797 radix_tree_delete(&fs_info
->fs_roots_radix
,
798 (unsigned long)root
->root_key
.objectid
);
800 btrfs_sysfs_del_root(root
);
804 free_extent_buffer(root
->node
);
805 if (root
->commit_root
)
806 free_extent_buffer(root
->commit_root
);
813 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
816 struct btrfs_root
*gang
[8];
820 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
825 for (i
= 0; i
< ret
; i
++)
826 btrfs_free_fs_root(fs_info
, gang
[i
]);
831 int close_ctree(struct btrfs_root
*root
)
834 struct btrfs_trans_handle
*trans
;
835 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
837 fs_info
->closing
= 1;
838 btrfs_transaction_flush_work(root
);
839 mutex_lock(&fs_info
->fs_mutex
);
840 btrfs_defrag_dirty_roots(root
->fs_info
);
841 trans
= btrfs_start_transaction(root
, 1);
842 ret
= btrfs_commit_transaction(trans
, root
);
843 /* run commit again to drop the original snapshot */
844 trans
= btrfs_start_transaction(root
, 1);
845 btrfs_commit_transaction(trans
, root
);
846 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
848 write_ctree_super(NULL
, root
);
849 mutex_unlock(&fs_info
->fs_mutex
);
851 if (fs_info
->extent_root
->node
)
852 free_extent_buffer(fs_info
->extent_root
->node
);
854 if (fs_info
->tree_root
->node
)
855 free_extent_buffer(fs_info
->tree_root
->node
);
857 free_extent_buffer(fs_info
->sb_buffer
);
859 btrfs_free_block_groups(root
->fs_info
);
860 del_fs_roots(fs_info
);
862 filemap_write_and_wait(fs_info
->btree_inode
->i_mapping
);
864 extent_map_tree_empty_lru(&fs_info
->free_space_cache
);
865 extent_map_tree_empty_lru(&fs_info
->block_group_cache
);
866 extent_map_tree_empty_lru(&fs_info
->pinned_extents
);
867 extent_map_tree_empty_lru(&fs_info
->pending_del
);
868 extent_map_tree_empty_lru(&fs_info
->extent_ins
);
869 extent_map_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
);
871 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
873 iput(fs_info
->btree_inode
);
875 while(!list_empty(&fs_info
->hashers
)) {
876 struct btrfs_hasher
*hasher
;
877 hasher
= list_entry(fs_info
->hashers
.next
, struct btrfs_hasher
,
879 list_del(&hasher
->hashers
);
880 crypto_free_hash(&fs_info
->hash_tfm
);
884 kfree(fs_info
->extent_root
);
885 kfree(fs_info
->tree_root
);
889 int btrfs_buffer_uptodate(struct extent_buffer
*buf
)
891 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
892 return extent_buffer_uptodate(&BTRFS_I(btree_inode
)->extent_tree
, buf
);
895 int btrfs_set_buffer_uptodate(struct extent_buffer
*buf
)
897 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
898 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode
)->extent_tree
,
902 void btrfs_mark_buffer_dirty(struct extent_buffer
*buf
)
904 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
905 u64 transid
= btrfs_header_generation(buf
);
906 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
908 if (transid
!= root
->fs_info
->generation
) {
909 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
910 (unsigned long long)buf
->start
,
911 transid
, root
->fs_info
->generation
);
914 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->extent_tree
, buf
);
917 void btrfs_throttle(struct btrfs_root
*root
)
919 struct backing_dev_info
*bdi
;
921 bdi
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
922 if (root
->fs_info
->throttles
&& bdi_write_congested(bdi
))
923 congestion_wait(WRITE
, HZ
/20);
926 void btrfs_btree_balance_dirty(struct btrfs_root
*root
, unsigned long nr
)
928 balance_dirty_pages_ratelimited_nr(
929 root
->fs_info
->btree_inode
->i_mapping
, 1);
932 void btrfs_set_buffer_defrag(struct extent_buffer
*buf
)
934 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
935 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
936 set_extent_bits(&BTRFS_I(btree_inode
)->extent_tree
, buf
->start
,
937 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, GFP_NOFS
);
940 void btrfs_set_buffer_defrag_done(struct extent_buffer
*buf
)
942 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
943 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
944 set_extent_bits(&BTRFS_I(btree_inode
)->extent_tree
, buf
->start
,
945 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG_DONE
,
949 int btrfs_buffer_defrag(struct extent_buffer
*buf
)
951 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
952 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
953 return test_range_bit(&BTRFS_I(btree_inode
)->extent_tree
,
954 buf
->start
, buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, 0);
957 int btrfs_buffer_defrag_done(struct extent_buffer
*buf
)
959 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
960 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
961 return test_range_bit(&BTRFS_I(btree_inode
)->extent_tree
,
962 buf
->start
, buf
->start
+ buf
->len
- 1,
963 EXTENT_DEFRAG_DONE
, 0);
966 int btrfs_clear_buffer_defrag_done(struct extent_buffer
*buf
)
968 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
969 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
970 return clear_extent_bits(&BTRFS_I(btree_inode
)->extent_tree
,
971 buf
->start
, buf
->start
+ buf
->len
- 1,
972 EXTENT_DEFRAG_DONE
, GFP_NOFS
);
975 int btrfs_clear_buffer_defrag(struct extent_buffer
*buf
)
977 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
978 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
979 return clear_extent_bits(&BTRFS_I(btree_inode
)->extent_tree
,
980 buf
->start
, buf
->start
+ buf
->len
- 1,
981 EXTENT_DEFRAG
, GFP_NOFS
);
984 int btrfs_read_buffer(struct extent_buffer
*buf
)
986 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
987 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
988 return read_extent_buffer_pages(&BTRFS_I(btree_inode
)->extent_tree
,
992 static struct extent_map_ops btree_extent_map_ops
= {
993 .writepage_io_hook
= btree_writepage_io_hook
,