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/scatterlist.h>
22 #include <linux/swap.h>
23 #include <linux/radix-tree.h>
24 #include <linux/writeback.h>
25 #include <linux/buffer_head.h> // for block_sync_page
26 #include <linux/workqueue.h>
30 #include "transaction.h"
31 #include "btrfs_inode.h"
33 #include "print-tree.h"
36 static int check_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
)
38 if (extent_buffer_blocknr(buf
) != btrfs_header_blocknr(buf
)) {
39 printk(KERN_CRIT
"buf blocknr(buf) is %llu, header is %llu\n",
40 (unsigned long long)extent_buffer_blocknr(buf
),
41 (unsigned long long)btrfs_header_blocknr(buf
));
48 static struct extent_io_ops btree_extent_io_ops
;
49 static struct workqueue_struct
*end_io_workqueue
;
50 static struct workqueue_struct
*async_submit_workqueue
;
56 struct btrfs_fs_info
*info
;
59 struct list_head list
;
62 struct async_submit_bio
{
65 struct list_head list
;
66 extent_submit_bio_hook_t
*submit_bio_hook
;
71 struct extent_map
*btree_get_extent(struct inode
*inode
, struct page
*page
,
72 size_t page_offset
, u64 start
, u64 len
,
75 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
76 struct extent_map
*em
;
80 spin_lock(&em_tree
->lock
);
81 em
= lookup_extent_mapping(em_tree
, start
, len
);
82 spin_unlock(&em_tree
->lock
);
86 em
= alloc_extent_map(GFP_NOFS
);
88 em
= ERR_PTR(-ENOMEM
);
92 em
->len
= i_size_read(inode
);
94 em
->bdev
= inode
->i_sb
->s_bdev
;
96 spin_lock(&em_tree
->lock
);
97 ret
= add_extent_mapping(em_tree
, em
);
98 spin_unlock(&em_tree
->lock
);
100 if (ret
== -EEXIST
) {
111 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
113 return btrfs_crc32c(seed
, data
, len
);
116 void btrfs_csum_final(u32 crc
, char *result
)
118 *(__le32
*)result
= ~cpu_to_le32(crc
);
121 static int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
124 char result
[BTRFS_CRC32_SIZE
];
126 unsigned long cur_len
;
127 unsigned long offset
= BTRFS_CSUM_SIZE
;
128 char *map_token
= NULL
;
130 unsigned long map_start
;
131 unsigned long map_len
;
135 len
= buf
->len
- offset
;
137 err
= map_private_extent_buffer(buf
, offset
, 32,
139 &map_start
, &map_len
, KM_USER0
);
141 printk("failed to map extent buffer! %lu\n",
145 cur_len
= min(len
, map_len
- (offset
- map_start
));
146 crc
= btrfs_csum_data(root
, kaddr
+ offset
- map_start
,
150 unmap_extent_buffer(buf
, map_token
, KM_USER0
);
152 btrfs_csum_final(crc
, result
);
155 int from_this_trans
= 0;
157 if (root
->fs_info
->running_transaction
&&
158 btrfs_header_generation(buf
) ==
159 root
->fs_info
->running_transaction
->transid
)
162 /* FIXME, this is not good */
163 if (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 "
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
);
183 static int btree_read_extent_buffer_pages(struct btrfs_root
*root
,
184 struct extent_buffer
*eb
,
187 struct extent_io_tree
*io_tree
;
192 io_tree
= &BTRFS_I(root
->fs_info
->btree_inode
)->io_tree
;
194 ret
= read_extent_buffer_pages(io_tree
, eb
, start
, 1,
195 btree_get_extent
, mirror_num
);
198 printk("good read %Lu mirror %d total %d\n", eb
->start
, mirror_num
, num_copies
);
201 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
203 printk("failed to read %Lu mirror %d total %d\n", eb
->start
, mirror_num
, num_copies
);
204 if (num_copies
== 1) {
205 printk("reading %Lu failed only one copy\n", eb
->start
);
209 if (mirror_num
> num_copies
) {
210 printk("bailing at mirror %d of %d\n", mirror_num
, num_copies
);
214 printk("read extent buffer page last\n");
218 int csum_dirty_buffer(struct btrfs_root
*root
, struct page
*page
)
220 struct extent_io_tree
*tree
;
221 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
225 struct extent_buffer
*eb
;
228 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
230 if (page
->private == EXTENT_PAGE_PRIVATE
)
234 len
= page
->private >> 2;
238 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
239 ret
= btree_read_extent_buffer_pages(root
, eb
, start
+ PAGE_CACHE_SIZE
);
241 btrfs_clear_buffer_defrag(eb
);
242 found_start
= btrfs_header_bytenr(eb
);
243 if (found_start
!= start
) {
244 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
245 start
, found_start
, len
);
249 if (eb
->first_page
!= page
) {
250 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
255 if (!PageUptodate(page
)) {
256 printk("csum not up to date page %lu\n", page
->index
);
260 found_level
= btrfs_header_level(eb
);
261 spin_lock(&root
->fs_info
->hash_lock
);
262 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
263 spin_unlock(&root
->fs_info
->hash_lock
);
264 csum_tree_block(root
, eb
, 0);
266 free_extent_buffer(eb
);
271 static int btree_writepage_io_hook(struct page
*page
, u64 start
, u64 end
)
273 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
275 csum_dirty_buffer(root
, page
);
279 int btree_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
280 struct extent_state
*state
)
282 struct extent_io_tree
*tree
;
286 struct extent_buffer
*eb
;
287 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
290 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
291 if (page
->private == EXTENT_PAGE_PRIVATE
)
295 len
= page
->private >> 2;
299 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
301 btrfs_clear_buffer_defrag(eb
);
302 found_start
= btrfs_header_bytenr(eb
);
303 if (found_start
!= start
) {
304 printk("bad start on %Lu found %Lu\n", eb
->start
, found_start
);
308 if (eb
->first_page
!= page
) {
309 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
315 found_level
= btrfs_header_level(eb
);
317 ret
= csum_tree_block(root
, eb
, 1);
321 end
= min_t(u64
, eb
->len
, PAGE_CACHE_SIZE
);
322 end
= eb
->start
+ end
- 1;
323 release_extent_buffer_tail_pages(eb
);
325 free_extent_buffer(eb
);
330 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
331 static void end_workqueue_bio(struct bio
*bio
, int err
)
333 static int end_workqueue_bio(struct bio
*bio
,
334 unsigned int bytes_done
, int err
)
337 struct end_io_wq
*end_io_wq
= bio
->bi_private
;
338 struct btrfs_fs_info
*fs_info
;
341 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
346 fs_info
= end_io_wq
->info
;
347 spin_lock_irqsave(&fs_info
->end_io_work_lock
, flags
);
348 end_io_wq
->error
= err
;
349 list_add_tail(&end_io_wq
->list
, &fs_info
->end_io_work_list
);
350 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
, flags
);
351 queue_work(end_io_workqueue
, &fs_info
->end_io_work
);
353 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
358 int btrfs_bio_wq_end_io(struct btrfs_fs_info
*info
, struct bio
*bio
,
361 struct end_io_wq
*end_io_wq
;
362 end_io_wq
= kmalloc(sizeof(*end_io_wq
), GFP_NOFS
);
366 end_io_wq
->private = bio
->bi_private
;
367 end_io_wq
->end_io
= bio
->bi_end_io
;
368 end_io_wq
->info
= info
;
369 end_io_wq
->error
= 0;
370 end_io_wq
->bio
= bio
;
371 end_io_wq
->metadata
= metadata
;
373 bio
->bi_private
= end_io_wq
;
374 bio
->bi_end_io
= end_workqueue_bio
;
378 int btrfs_wq_submit_bio(struct btrfs_fs_info
*fs_info
, struct inode
*inode
,
379 int rw
, struct bio
*bio
, int mirror_num
,
380 extent_submit_bio_hook_t
*submit_bio_hook
)
382 struct async_submit_bio
*async
;
385 * inline writerback should stay inline, only hop to the async
386 * queue if we're pdflush
388 if (!current_is_pdflush())
389 return submit_bio_hook(inode
, rw
, bio
, mirror_num
);
391 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
395 async
->inode
= inode
;
398 async
->mirror_num
= mirror_num
;
399 async
->submit_bio_hook
= submit_bio_hook
;
401 spin_lock(&fs_info
->async_submit_work_lock
);
402 list_add_tail(&async
->list
, &fs_info
->async_submit_work_list
);
403 spin_unlock(&fs_info
->async_submit_work_lock
);
405 queue_work(async_submit_workqueue
, &fs_info
->async_submit_work
);
409 static int __btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
412 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
416 offset
= bio
->bi_sector
<< 9;
418 if (rw
& (1 << BIO_RW
)) {
419 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
, mirror_num
);
422 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 1);
425 if (offset
== BTRFS_SUPER_INFO_OFFSET
) {
426 bio
->bi_bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
430 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
, mirror_num
);
433 static int btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
436 if (!(rw
& (1 << BIO_RW
))) {
437 return __btree_submit_bio_hook(inode
, rw
, bio
, mirror_num
);
439 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
440 inode
, rw
, bio
, mirror_num
,
441 __btree_submit_bio_hook
);
444 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
446 struct extent_io_tree
*tree
;
447 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
448 return extent_write_full_page(tree
, page
, btree_get_extent
, wbc
);
451 static int btree_writepages(struct address_space
*mapping
,
452 struct writeback_control
*wbc
)
454 struct extent_io_tree
*tree
;
455 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
456 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
459 unsigned long thresh
= 96 * 1024 * 1024;
461 if (wbc
->for_kupdate
)
464 if (current_is_pdflush()) {
465 thresh
= 96 * 1024 * 1024;
467 thresh
= 8 * 1024 * 1024;
469 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
470 thresh
, EXTENT_DIRTY
);
471 if (num_dirty
< thresh
) {
475 return extent_writepages(tree
, mapping
, btree_get_extent
, wbc
);
478 int btree_readpage(struct file
*file
, struct page
*page
)
480 struct extent_io_tree
*tree
;
481 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
482 return extent_read_full_page(tree
, page
, btree_get_extent
);
485 static int btree_releasepage(struct page
*page
, gfp_t gfp_flags
)
487 struct extent_io_tree
*tree
;
488 struct extent_map_tree
*map
;
491 if (page_count(page
) > 3) {
492 /* once for page->private, once for the caller, once
493 * once for the page cache
497 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
498 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
499 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
501 invalidate_extent_lru(tree
, page_offset(page
), PAGE_CACHE_SIZE
);
502 ClearPagePrivate(page
);
503 set_page_private(page
, 0);
504 page_cache_release(page
);
509 static void btree_invalidatepage(struct page
*page
, unsigned long offset
)
511 struct extent_io_tree
*tree
;
512 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
513 extent_invalidatepage(tree
, page
, offset
);
514 btree_releasepage(page
, GFP_NOFS
);
518 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
520 struct buffer_head
*bh
;
521 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
522 struct buffer_head
*head
;
523 if (!page_has_buffers(page
)) {
524 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
525 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
527 head
= page_buffers(page
);
530 if (buffer_dirty(bh
))
531 csum_tree_block(root
, bh
, 0);
532 bh
= bh
->b_this_page
;
533 } while (bh
!= head
);
534 return block_write_full_page(page
, btree_get_block
, wbc
);
538 static struct address_space_operations btree_aops
= {
539 .readpage
= btree_readpage
,
540 .writepage
= btree_writepage
,
541 .writepages
= btree_writepages
,
542 .releasepage
= btree_releasepage
,
543 .invalidatepage
= btree_invalidatepage
,
544 .sync_page
= block_sync_page
,
547 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
)
549 struct extent_buffer
*buf
= NULL
;
550 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
553 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
556 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
557 buf
, 0, 0, btree_get_extent
, 0);
558 free_extent_buffer(buf
);
562 static int close_all_devices(struct btrfs_fs_info
*fs_info
)
564 struct list_head
*list
;
565 struct list_head
*next
;
566 struct btrfs_device
*device
;
568 list
= &fs_info
->fs_devices
->devices
;
569 list_for_each(next
, list
) {
570 device
= list_entry(next
, struct btrfs_device
, dev_list
);
571 if (device
->bdev
&& device
->bdev
!= fs_info
->sb
->s_bdev
)
572 close_bdev_excl(device
->bdev
);
578 int btrfs_verify_block_csum(struct btrfs_root
*root
,
579 struct extent_buffer
*buf
)
581 return btrfs_buffer_uptodate(buf
);
584 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
585 u64 bytenr
, u32 blocksize
)
587 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
588 struct extent_buffer
*eb
;
589 eb
= find_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
590 bytenr
, blocksize
, GFP_NOFS
);
594 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
595 u64 bytenr
, u32 blocksize
)
597 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
598 struct extent_buffer
*eb
;
600 eb
= alloc_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
601 bytenr
, blocksize
, NULL
, GFP_NOFS
);
606 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
609 struct extent_buffer
*buf
= NULL
;
610 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
611 struct extent_io_tree
*io_tree
;
614 io_tree
= &BTRFS_I(btree_inode
)->io_tree
;
616 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
620 ret
= btree_read_extent_buffer_pages(root
, buf
, 0);
623 buf
->flags
|= EXTENT_UPTODATE
;
629 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
630 struct extent_buffer
*buf
)
632 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
633 if (btrfs_header_generation(buf
) ==
634 root
->fs_info
->running_transaction
->transid
)
635 clear_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
,
640 int wait_on_tree_block_writeback(struct btrfs_root
*root
,
641 struct extent_buffer
*buf
)
643 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
644 wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode
)->io_tree
,
649 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
650 u32 stripesize
, struct btrfs_root
*root
,
651 struct btrfs_fs_info
*fs_info
,
656 root
->commit_root
= NULL
;
657 root
->sectorsize
= sectorsize
;
658 root
->nodesize
= nodesize
;
659 root
->leafsize
= leafsize
;
660 root
->stripesize
= stripesize
;
662 root
->track_dirty
= 0;
664 root
->fs_info
= fs_info
;
665 root
->objectid
= objectid
;
666 root
->last_trans
= 0;
667 root
->highest_inode
= 0;
668 root
->last_inode_alloc
= 0;
672 INIT_LIST_HEAD(&root
->dirty_list
);
673 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
674 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
675 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
676 memset(&root
->root_kobj
, 0, sizeof(root
->root_kobj
));
677 init_completion(&root
->kobj_unregister
);
678 root
->defrag_running
= 0;
679 root
->defrag_level
= 0;
680 root
->root_key
.objectid
= objectid
;
684 static int find_and_setup_root(struct btrfs_root
*tree_root
,
685 struct btrfs_fs_info
*fs_info
,
687 struct btrfs_root
*root
)
692 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
693 tree_root
->sectorsize
, tree_root
->stripesize
,
694 root
, fs_info
, objectid
);
695 ret
= btrfs_find_last_root(tree_root
, objectid
,
696 &root
->root_item
, &root
->root_key
);
699 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
700 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
706 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_fs_info
*fs_info
,
707 struct btrfs_key
*location
)
709 struct btrfs_root
*root
;
710 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
711 struct btrfs_path
*path
;
712 struct extent_buffer
*l
;
717 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
719 return ERR_PTR(-ENOMEM
);
720 if (location
->offset
== (u64
)-1) {
721 ret
= find_and_setup_root(tree_root
, fs_info
,
722 location
->objectid
, root
);
730 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
731 tree_root
->sectorsize
, tree_root
->stripesize
,
732 root
, fs_info
, location
->objectid
);
734 path
= btrfs_alloc_path();
736 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
743 read_extent_buffer(l
, &root
->root_item
,
744 btrfs_item_ptr_offset(l
, path
->slots
[0]),
745 sizeof(root
->root_item
));
746 memcpy(&root
->root_key
, location
, sizeof(*location
));
749 btrfs_release_path(root
, path
);
750 btrfs_free_path(path
);
755 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
756 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
761 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
763 root
->highest_inode
= highest_inode
;
764 root
->last_inode_alloc
= highest_inode
;
769 struct btrfs_root
*btrfs_lookup_fs_root(struct btrfs_fs_info
*fs_info
,
772 struct btrfs_root
*root
;
774 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
)
775 return fs_info
->tree_root
;
776 if (root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
777 return fs_info
->extent_root
;
779 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
780 (unsigned long)root_objectid
);
784 struct btrfs_root
*btrfs_read_fs_root_no_name(struct btrfs_fs_info
*fs_info
,
785 struct btrfs_key
*location
)
787 struct btrfs_root
*root
;
790 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
791 return fs_info
->tree_root
;
792 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
793 return fs_info
->extent_root
;
795 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
796 (unsigned long)location
->objectid
);
800 root
= btrfs_read_fs_root_no_radix(fs_info
, location
);
803 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
804 (unsigned long)root
->root_key
.objectid
,
807 free_extent_buffer(root
->node
);
811 ret
= btrfs_find_dead_roots(fs_info
->tree_root
,
812 root
->root_key
.objectid
, root
);
818 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
819 struct btrfs_key
*location
,
820 const char *name
, int namelen
)
822 struct btrfs_root
*root
;
825 root
= btrfs_read_fs_root_no_name(fs_info
, location
);
832 ret
= btrfs_set_root_name(root
, name
, namelen
);
834 free_extent_buffer(root
->node
);
839 ret
= btrfs_sysfs_add_root(root
);
841 free_extent_buffer(root
->node
);
850 static int add_hasher(struct btrfs_fs_info
*info
, char *type
) {
851 struct btrfs_hasher
*hasher
;
853 hasher
= kmalloc(sizeof(*hasher
), GFP_NOFS
);
856 hasher
->hash_tfm
= crypto_alloc_hash(type
, 0, CRYPTO_ALG_ASYNC
);
857 if (!hasher
->hash_tfm
) {
861 spin_lock(&info
->hash_lock
);
862 list_add(&hasher
->list
, &info
->hashers
);
863 spin_unlock(&info
->hash_lock
);
868 static int btrfs_congested_fn(void *congested_data
, int bdi_bits
)
870 struct btrfs_fs_info
*info
= (struct btrfs_fs_info
*)congested_data
;
872 struct list_head
*cur
;
873 struct btrfs_device
*device
;
874 struct backing_dev_info
*bdi
;
876 list_for_each(cur
, &info
->fs_devices
->devices
) {
877 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
878 bdi
= blk_get_backing_dev_info(device
->bdev
);
879 if (bdi
&& bdi_congested(bdi
, bdi_bits
)) {
887 void btrfs_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
889 struct list_head
*cur
;
890 struct btrfs_device
*device
;
891 struct btrfs_fs_info
*info
;
893 info
= (struct btrfs_fs_info
*)bdi
->unplug_io_data
;
894 list_for_each(cur
, &info
->fs_devices
->devices
) {
895 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
896 bdi
= blk_get_backing_dev_info(device
->bdev
);
897 if (bdi
->unplug_io_fn
) {
898 bdi
->unplug_io_fn(bdi
, page
);
903 static int setup_bdi(struct btrfs_fs_info
*info
, struct backing_dev_info
*bdi
)
905 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
908 bdi
->ra_pages
= default_backing_dev_info
.ra_pages
* 4;
910 bdi
->capabilities
= default_backing_dev_info
.capabilities
;
911 bdi
->unplug_io_fn
= btrfs_unplug_io_fn
;
912 bdi
->unplug_io_data
= info
;
913 bdi
->congested_fn
= btrfs_congested_fn
;
914 bdi
->congested_data
= info
;
918 static int bio_ready_for_csum(struct bio
*bio
)
924 struct extent_io_tree
*io_tree
= NULL
;
925 struct btrfs_fs_info
*info
= NULL
;
926 struct bio_vec
*bvec
;
930 bio_for_each_segment(bvec
, bio
, i
) {
931 page
= bvec
->bv_page
;
932 if (page
->private == EXTENT_PAGE_PRIVATE
) {
933 length
+= bvec
->bv_len
;
936 if (!page
->private) {
937 length
+= bvec
->bv_len
;
940 length
= bvec
->bv_len
;
941 buf_len
= page
->private >> 2;
942 start
= page_offset(page
) + bvec
->bv_offset
;
943 io_tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
944 info
= BTRFS_I(page
->mapping
->host
)->root
->fs_info
;
946 /* are we fully contained in this bio? */
947 if (buf_len
<= length
)
950 ret
= extent_range_uptodate(io_tree
, start
+ length
,
951 start
+ buf_len
- 1);
957 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
958 static void btrfs_end_io_csum(void *p
)
960 static void btrfs_end_io_csum(struct work_struct
*work
)
963 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
964 struct btrfs_fs_info
*fs_info
= p
;
966 struct btrfs_fs_info
*fs_info
= container_of(work
,
967 struct btrfs_fs_info
,
971 struct end_io_wq
*end_io_wq
;
973 struct list_head
*next
;
978 spin_lock_irqsave(&fs_info
->end_io_work_lock
, flags
);
979 if (list_empty(&fs_info
->end_io_work_list
)) {
980 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
,
984 next
= fs_info
->end_io_work_list
.next
;
986 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
, flags
);
988 end_io_wq
= list_entry(next
, struct end_io_wq
, list
);
990 bio
= end_io_wq
->bio
;
991 if (end_io_wq
->metadata
&& !bio_ready_for_csum(bio
)) {
992 spin_lock_irqsave(&fs_info
->end_io_work_lock
, flags
);
993 was_empty
= list_empty(&fs_info
->end_io_work_list
);
994 list_add_tail(&end_io_wq
->list
,
995 &fs_info
->end_io_work_list
);
996 spin_unlock_irqrestore(&fs_info
->end_io_work_lock
,
1002 error
= end_io_wq
->error
;
1003 bio
->bi_private
= end_io_wq
->private;
1004 bio
->bi_end_io
= end_io_wq
->end_io
;
1006 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1007 bio_endio(bio
, bio
->bi_size
, error
);
1009 bio_endio(bio
, error
);
1014 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1015 static void btrfs_async_submit_work(void *p
)
1017 static void btrfs_async_submit_work(struct work_struct
*work
)
1020 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1021 struct btrfs_fs_info
*fs_info
= p
;
1023 struct btrfs_fs_info
*fs_info
= container_of(work
,
1024 struct btrfs_fs_info
,
1027 struct async_submit_bio
*async
;
1028 struct list_head
*next
;
1031 spin_lock(&fs_info
->async_submit_work_lock
);
1032 if (list_empty(&fs_info
->async_submit_work_list
)) {
1033 spin_unlock(&fs_info
->async_submit_work_lock
);
1036 next
= fs_info
->async_submit_work_list
.next
;
1038 spin_unlock(&fs_info
->async_submit_work_lock
);
1040 async
= list_entry(next
, struct async_submit_bio
, list
);
1041 async
->submit_bio_hook(async
->inode
, async
->rw
, async
->bio
,
1047 struct btrfs_root
*open_ctree(struct super_block
*sb
,
1048 struct btrfs_fs_devices
*fs_devices
)
1055 struct btrfs_root
*extent_root
= kmalloc(sizeof(struct btrfs_root
),
1057 struct btrfs_root
*tree_root
= kmalloc(sizeof(struct btrfs_root
),
1059 struct btrfs_fs_info
*fs_info
= kzalloc(sizeof(*fs_info
),
1061 struct btrfs_root
*chunk_root
= kmalloc(sizeof(struct btrfs_root
),
1063 struct btrfs_root
*dev_root
= kmalloc(sizeof(struct btrfs_root
),
1067 struct btrfs_super_block
*disk_super
;
1069 if (!extent_root
|| !tree_root
|| !fs_info
) {
1073 end_io_workqueue
= create_workqueue("btrfs-end-io");
1074 BUG_ON(!end_io_workqueue
);
1075 async_submit_workqueue
= create_workqueue("btrfs-async-submit");
1077 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
1078 INIT_LIST_HEAD(&fs_info
->trans_list
);
1079 INIT_LIST_HEAD(&fs_info
->dead_roots
);
1080 INIT_LIST_HEAD(&fs_info
->hashers
);
1081 INIT_LIST_HEAD(&fs_info
->end_io_work_list
);
1082 INIT_LIST_HEAD(&fs_info
->async_submit_work_list
);
1083 spin_lock_init(&fs_info
->hash_lock
);
1084 spin_lock_init(&fs_info
->end_io_work_lock
);
1085 spin_lock_init(&fs_info
->async_submit_work_lock
);
1086 spin_lock_init(&fs_info
->delalloc_lock
);
1087 spin_lock_init(&fs_info
->new_trans_lock
);
1089 init_completion(&fs_info
->kobj_unregister
);
1090 sb_set_blocksize(sb
, BTRFS_SUPER_INFO_SIZE
);
1091 fs_info
->tree_root
= tree_root
;
1092 fs_info
->extent_root
= extent_root
;
1093 fs_info
->chunk_root
= chunk_root
;
1094 fs_info
->dev_root
= dev_root
;
1095 fs_info
->fs_devices
= fs_devices
;
1096 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
1097 INIT_LIST_HEAD(&fs_info
->space_info
);
1098 btrfs_mapping_init(&fs_info
->mapping_tree
);
1100 fs_info
->max_extent
= (u64
)-1;
1101 fs_info
->max_inline
= 8192 * 1024;
1102 setup_bdi(fs_info
, &fs_info
->bdi
);
1103 fs_info
->btree_inode
= new_inode(sb
);
1104 fs_info
->btree_inode
->i_ino
= 1;
1105 fs_info
->btree_inode
->i_nlink
= 1;
1106 fs_info
->btree_inode
->i_size
= sb
->s_bdev
->bd_inode
->i_size
;
1107 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
1108 fs_info
->btree_inode
->i_mapping
->backing_dev_info
= &fs_info
->bdi
;
1110 extent_io_tree_init(&BTRFS_I(fs_info
->btree_inode
)->io_tree
,
1111 fs_info
->btree_inode
->i_mapping
,
1113 extent_map_tree_init(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
,
1116 BTRFS_I(fs_info
->btree_inode
)->io_tree
.ops
= &btree_extent_io_ops
;
1118 extent_io_tree_init(&fs_info
->free_space_cache
,
1119 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1120 extent_io_tree_init(&fs_info
->block_group_cache
,
1121 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1122 extent_io_tree_init(&fs_info
->pinned_extents
,
1123 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1124 extent_io_tree_init(&fs_info
->pending_del
,
1125 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1126 extent_io_tree_init(&fs_info
->extent_ins
,
1127 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1128 fs_info
->do_barriers
= 1;
1130 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1131 INIT_WORK(&fs_info
->end_io_work
, btrfs_end_io_csum
, fs_info
);
1132 INIT_WORK(&fs_info
->async_submit_work
, btrfs_async_submit_work
,
1134 INIT_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
, fs_info
);
1136 INIT_WORK(&fs_info
->end_io_work
, btrfs_end_io_csum
);
1137 INIT_WORK(&fs_info
->async_submit_work
, btrfs_async_submit_work
);
1138 INIT_DELAYED_WORK(&fs_info
->trans_work
, btrfs_transaction_cleaner
);
1140 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
1141 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
1142 sizeof(struct btrfs_key
));
1143 insert_inode_hash(fs_info
->btree_inode
);
1144 mapping_set_gfp_mask(fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1146 mutex_init(&fs_info
->trans_mutex
);
1147 mutex_init(&fs_info
->fs_mutex
);
1150 ret
= add_hasher(fs_info
, "crc32c");
1152 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1157 __setup_root(4096, 4096, 4096, 4096, tree_root
,
1158 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
1160 fs_info
->sb_buffer
= read_tree_block(tree_root
,
1161 BTRFS_SUPER_INFO_OFFSET
,
1164 if (!fs_info
->sb_buffer
)
1167 read_extent_buffer(fs_info
->sb_buffer
, &fs_info
->super_copy
, 0,
1168 sizeof(fs_info
->super_copy
));
1170 read_extent_buffer(fs_info
->sb_buffer
, fs_info
->fsid
,
1171 (unsigned long)btrfs_super_fsid(fs_info
->sb_buffer
),
1174 disk_super
= &fs_info
->super_copy
;
1175 if (!btrfs_super_root(disk_super
))
1176 goto fail_sb_buffer
;
1178 if (btrfs_super_num_devices(disk_super
) != fs_devices
->num_devices
) {
1179 printk("Btrfs: wanted %llu devices, but found %llu\n",
1180 (unsigned long long)btrfs_super_num_devices(disk_super
),
1181 (unsigned long long)fs_devices
->num_devices
);
1182 goto fail_sb_buffer
;
1184 nodesize
= btrfs_super_nodesize(disk_super
);
1185 leafsize
= btrfs_super_leafsize(disk_super
);
1186 sectorsize
= btrfs_super_sectorsize(disk_super
);
1187 stripesize
= btrfs_super_stripesize(disk_super
);
1188 tree_root
->nodesize
= nodesize
;
1189 tree_root
->leafsize
= leafsize
;
1190 tree_root
->sectorsize
= sectorsize
;
1191 tree_root
->stripesize
= stripesize
;
1192 sb_set_blocksize(sb
, sectorsize
);
1194 i_size_write(fs_info
->btree_inode
,
1195 btrfs_super_total_bytes(disk_super
));
1197 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
1198 sizeof(disk_super
->magic
))) {
1199 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
1200 goto fail_sb_buffer
;
1203 mutex_lock(&fs_info
->fs_mutex
);
1205 ret
= btrfs_read_sys_array(tree_root
);
1208 blocksize
= btrfs_level_size(tree_root
,
1209 btrfs_super_chunk_root_level(disk_super
));
1211 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
1212 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
1214 chunk_root
->node
= read_tree_block(chunk_root
,
1215 btrfs_super_chunk_root(disk_super
),
1217 BUG_ON(!chunk_root
->node
);
1219 read_extent_buffer(chunk_root
->node
, fs_info
->chunk_tree_uuid
,
1220 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root
->node
),
1223 ret
= btrfs_read_chunk_tree(chunk_root
);
1226 blocksize
= btrfs_level_size(tree_root
,
1227 btrfs_super_root_level(disk_super
));
1230 tree_root
->node
= read_tree_block(tree_root
,
1231 btrfs_super_root(disk_super
),
1233 if (!tree_root
->node
)
1234 goto fail_sb_buffer
;
1237 ret
= find_and_setup_root(tree_root
, fs_info
,
1238 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
1240 goto fail_tree_root
;
1241 extent_root
->track_dirty
= 1;
1243 ret
= find_and_setup_root(tree_root
, fs_info
,
1244 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
1245 dev_root
->track_dirty
= 1;
1248 goto fail_extent_root
;
1250 btrfs_read_block_groups(extent_root
);
1252 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
1253 fs_info
->data_alloc_profile
= (u64
)-1;
1254 fs_info
->metadata_alloc_profile
= (u64
)-1;
1255 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
1257 mutex_unlock(&fs_info
->fs_mutex
);
1261 free_extent_buffer(extent_root
->node
);
1263 mutex_unlock(&fs_info
->fs_mutex
);
1264 free_extent_buffer(tree_root
->node
);
1266 free_extent_buffer(fs_info
->sb_buffer
);
1267 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
1269 iput(fs_info
->btree_inode
);
1271 close_all_devices(fs_info
);
1274 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1275 bdi_destroy(&fs_info
->bdi
);
1278 return ERR_PTR(err
);
1281 static void btrfs_end_buffer_write_sync(struct buffer_head
*bh
, int uptodate
)
1283 char b
[BDEVNAME_SIZE
];
1286 set_buffer_uptodate(bh
);
1288 if (!buffer_eopnotsupp(bh
) && printk_ratelimit()) {
1289 printk(KERN_WARNING
"lost page write due to "
1290 "I/O error on %s\n",
1291 bdevname(bh
->b_bdev
, b
));
1293 set_buffer_write_io_error(bh
);
1294 clear_buffer_uptodate(bh
);
1300 int write_all_supers(struct btrfs_root
*root
)
1302 struct list_head
*cur
;
1303 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
1304 struct btrfs_device
*dev
;
1305 struct extent_buffer
*sb
;
1306 struct btrfs_dev_item
*dev_item
;
1307 struct buffer_head
*bh
;
1311 do_barriers
= !btrfs_test_opt(root
, NOBARRIER
);
1313 sb
= root
->fs_info
->sb_buffer
;
1314 dev_item
= (struct btrfs_dev_item
*)offsetof(struct btrfs_super_block
,
1316 list_for_each(cur
, head
) {
1317 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
1318 btrfs_set_device_type(sb
, dev_item
, dev
->type
);
1319 btrfs_set_device_id(sb
, dev_item
, dev
->devid
);
1320 btrfs_set_device_total_bytes(sb
, dev_item
, dev
->total_bytes
);
1321 btrfs_set_device_bytes_used(sb
, dev_item
, dev
->bytes_used
);
1322 btrfs_set_device_io_align(sb
, dev_item
, dev
->io_align
);
1323 btrfs_set_device_io_width(sb
, dev_item
, dev
->io_width
);
1324 btrfs_set_device_sector_size(sb
, dev_item
, dev
->sector_size
);
1325 write_extent_buffer(sb
, dev
->uuid
,
1326 (unsigned long)btrfs_device_uuid(dev_item
),
1329 btrfs_set_header_flag(sb
, BTRFS_HEADER_FLAG_WRITTEN
);
1330 csum_tree_block(root
, sb
, 0);
1332 bh
= __getblk(dev
->bdev
, BTRFS_SUPER_INFO_OFFSET
/
1333 root
->fs_info
->sb
->s_blocksize
,
1334 BTRFS_SUPER_INFO_SIZE
);
1336 read_extent_buffer(sb
, bh
->b_data
, 0, BTRFS_SUPER_INFO_SIZE
);
1337 dev
->pending_io
= bh
;
1340 set_buffer_uptodate(bh
);
1342 bh
->b_end_io
= btrfs_end_buffer_write_sync
;
1344 if (do_barriers
&& dev
->barriers
) {
1345 ret
= submit_bh(WRITE_BARRIER
, bh
);
1346 if (ret
== -EOPNOTSUPP
) {
1347 printk("btrfs: disabling barriers on dev %s\n",
1349 set_buffer_uptodate(bh
);
1353 ret
= submit_bh(WRITE
, bh
);
1356 ret
= submit_bh(WRITE
, bh
);
1361 list_for_each(cur
, head
) {
1362 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
1363 BUG_ON(!dev
->pending_io
);
1364 bh
= dev
->pending_io
;
1366 if (!buffer_uptodate(dev
->pending_io
)) {
1367 if (do_barriers
&& dev
->barriers
) {
1368 printk("btrfs: disabling barriers on dev %s\n",
1370 set_buffer_uptodate(bh
);
1374 ret
= submit_bh(WRITE
, bh
);
1377 BUG_ON(!buffer_uptodate(bh
));
1383 dev
->pending_io
= NULL
;
1389 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
1394 ret
= write_all_supers(root
);
1396 if (!btrfs_test_opt(root
, NOBARRIER
))
1397 blkdev_issue_flush(sb
->s_bdev
, NULL
);
1398 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, super
);
1399 ret
= sync_page_range_nolock(btree_inode
, btree_inode
->i_mapping
,
1400 super
->start
, super
->len
);
1401 if (!btrfs_test_opt(root
, NOBARRIER
))
1402 blkdev_issue_flush(sb
->s_bdev
, NULL
);
1407 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
1409 radix_tree_delete(&fs_info
->fs_roots_radix
,
1410 (unsigned long)root
->root_key
.objectid
);
1412 btrfs_sysfs_del_root(root
);
1416 free_extent_buffer(root
->node
);
1417 if (root
->commit_root
)
1418 free_extent_buffer(root
->commit_root
);
1425 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
1428 struct btrfs_root
*gang
[8];
1432 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
1437 for (i
= 0; i
< ret
; i
++)
1438 btrfs_free_fs_root(fs_info
, gang
[i
]);
1443 int close_ctree(struct btrfs_root
*root
)
1446 struct btrfs_trans_handle
*trans
;
1447 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1449 fs_info
->closing
= 1;
1450 btrfs_transaction_flush_work(root
);
1451 mutex_lock(&fs_info
->fs_mutex
);
1452 btrfs_defrag_dirty_roots(root
->fs_info
);
1453 trans
= btrfs_start_transaction(root
, 1);
1454 ret
= btrfs_commit_transaction(trans
, root
);
1455 /* run commit again to drop the original snapshot */
1456 trans
= btrfs_start_transaction(root
, 1);
1457 btrfs_commit_transaction(trans
, root
);
1458 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
1460 write_ctree_super(NULL
, root
);
1461 mutex_unlock(&fs_info
->fs_mutex
);
1463 if (fs_info
->delalloc_bytes
) {
1464 printk("btrfs: at unmount delalloc count %Lu\n",
1465 fs_info
->delalloc_bytes
);
1467 if (fs_info
->extent_root
->node
)
1468 free_extent_buffer(fs_info
->extent_root
->node
);
1470 if (fs_info
->tree_root
->node
)
1471 free_extent_buffer(fs_info
->tree_root
->node
);
1473 if (root
->fs_info
->chunk_root
->node
);
1474 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
1476 if (root
->fs_info
->dev_root
->node
);
1477 free_extent_buffer(root
->fs_info
->dev_root
->node
);
1479 free_extent_buffer(fs_info
->sb_buffer
);
1481 btrfs_free_block_groups(root
->fs_info
);
1482 del_fs_roots(fs_info
);
1484 filemap_write_and_wait(fs_info
->btree_inode
->i_mapping
);
1486 extent_io_tree_empty_lru(&fs_info
->free_space_cache
);
1487 extent_io_tree_empty_lru(&fs_info
->block_group_cache
);
1488 extent_io_tree_empty_lru(&fs_info
->pinned_extents
);
1489 extent_io_tree_empty_lru(&fs_info
->pending_del
);
1490 extent_io_tree_empty_lru(&fs_info
->extent_ins
);
1491 extent_io_tree_empty_lru(&BTRFS_I(fs_info
->btree_inode
)->io_tree
);
1493 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
1494 flush_workqueue(end_io_workqueue
);
1495 destroy_workqueue(end_io_workqueue
);
1497 flush_workqueue(async_submit_workqueue
);
1498 destroy_workqueue(async_submit_workqueue
);
1500 iput(fs_info
->btree_inode
);
1502 while(!list_empty(&fs_info
->hashers
)) {
1503 struct btrfs_hasher
*hasher
;
1504 hasher
= list_entry(fs_info
->hashers
.next
, struct btrfs_hasher
,
1506 list_del(&hasher
->hashers
);
1507 crypto_free_hash(&fs_info
->hash_tfm
);
1511 close_all_devices(fs_info
);
1512 btrfs_mapping_tree_free(&fs_info
->mapping_tree
);
1514 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,23)
1515 bdi_destroy(&fs_info
->bdi
);
1518 kfree(fs_info
->extent_root
);
1519 kfree(fs_info
->tree_root
);
1520 kfree(fs_info
->chunk_root
);
1521 kfree(fs_info
->dev_root
);
1525 int btrfs_buffer_uptodate(struct extent_buffer
*buf
)
1527 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1528 return extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1531 int btrfs_set_buffer_uptodate(struct extent_buffer
*buf
)
1533 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1534 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
,
1538 void btrfs_mark_buffer_dirty(struct extent_buffer
*buf
)
1540 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1541 u64 transid
= btrfs_header_generation(buf
);
1542 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1544 if (transid
!= root
->fs_info
->generation
) {
1545 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
1546 (unsigned long long)buf
->start
,
1547 transid
, root
->fs_info
->generation
);
1550 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1553 void btrfs_throttle(struct btrfs_root
*root
)
1555 struct backing_dev_info
*bdi
;
1557 bdi
= root
->fs_info
->sb
->s_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
1558 if (root
->fs_info
->throttles
&& bdi_write_congested(bdi
)) {
1559 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1560 congestion_wait(WRITE
, HZ
/20);
1562 blk_congestion_wait(WRITE
, HZ
/20);
1567 void btrfs_btree_balance_dirty(struct btrfs_root
*root
, unsigned long nr
)
1569 balance_dirty_pages_ratelimited_nr(
1570 root
->fs_info
->btree_inode
->i_mapping
, 1);
1573 void btrfs_set_buffer_defrag(struct extent_buffer
*buf
)
1575 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1576 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1577 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1578 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, GFP_NOFS
);
1581 void btrfs_set_buffer_defrag_done(struct extent_buffer
*buf
)
1583 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1584 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1585 set_extent_bits(&BTRFS_I(btree_inode
)->io_tree
, buf
->start
,
1586 buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG_DONE
,
1590 int btrfs_buffer_defrag(struct extent_buffer
*buf
)
1592 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1593 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1594 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1595 buf
->start
, buf
->start
+ buf
->len
- 1, EXTENT_DEFRAG
, 0);
1598 int btrfs_buffer_defrag_done(struct extent_buffer
*buf
)
1600 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1601 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1602 return test_range_bit(&BTRFS_I(btree_inode
)->io_tree
,
1603 buf
->start
, buf
->start
+ buf
->len
- 1,
1604 EXTENT_DEFRAG_DONE
, 0);
1607 int btrfs_clear_buffer_defrag_done(struct extent_buffer
*buf
)
1609 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1610 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1611 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1612 buf
->start
, buf
->start
+ buf
->len
- 1,
1613 EXTENT_DEFRAG_DONE
, GFP_NOFS
);
1616 int btrfs_clear_buffer_defrag(struct extent_buffer
*buf
)
1618 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1619 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1620 return clear_extent_bits(&BTRFS_I(btree_inode
)->io_tree
,
1621 buf
->start
, buf
->start
+ buf
->len
- 1,
1622 EXTENT_DEFRAG
, GFP_NOFS
);
1625 int btrfs_read_buffer(struct extent_buffer
*buf
)
1627 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1629 ret
= btree_read_extent_buffer_pages(root
, buf
, 0);
1631 buf
->flags
|= EXTENT_UPTODATE
;
1636 static struct extent_io_ops btree_extent_io_ops
= {
1637 .writepage_io_hook
= btree_writepage_io_hook
,
1638 .readpage_end_io_hook
= btree_readpage_end_io_hook
,
1639 .submit_bio_hook
= btree_submit_bio_hook
,
1640 /* note we're sharing with inode.c for the merge bio hook */
1641 .merge_bio_hook
= btrfs_merge_bio_hook
,