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.
19 #include <linux/version.h>
21 #include <linux/blkdev.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
27 #include <linux/workqueue.h>
28 #include <linux/kthread.h>
29 # include <linux/freezer.h>
33 #include "transaction.h"
34 #include "btrfs_inode.h"
36 #include "print-tree.h"
37 #include "async-thread.h"
39 #include "ref-cache.h"
43 static int check_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
)
45 if (extent_buffer_blocknr(buf
) != btrfs_header_blocknr(buf
)) {
46 printk(KERN_CRIT
"buf blocknr(buf) is %llu, header is %llu\n",
47 (unsigned long long)extent_buffer_blocknr(buf
),
48 (unsigned long long)btrfs_header_blocknr(buf
));
55 static struct extent_io_ops btree_extent_io_ops
;
56 static void end_workqueue_fn(struct btrfs_work
*work
);
62 struct btrfs_fs_info
*info
;
65 struct list_head list
;
66 struct btrfs_work work
;
69 struct async_submit_bio
{
72 struct list_head list
;
73 extent_submit_bio_hook_t
*submit_bio_hook
;
76 struct btrfs_work work
;
79 struct extent_map
*btree_get_extent(struct inode
*inode
, struct page
*page
,
80 size_t page_offset
, u64 start
, u64 len
,
83 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
84 struct extent_map
*em
;
87 spin_lock(&em_tree
->lock
);
88 em
= lookup_extent_mapping(em_tree
, start
, len
);
91 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
92 spin_unlock(&em_tree
->lock
);
95 spin_unlock(&em_tree
->lock
);
97 em
= alloc_extent_map(GFP_NOFS
);
99 em
= ERR_PTR(-ENOMEM
);
105 em
->bdev
= BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
;
107 spin_lock(&em_tree
->lock
);
108 ret
= add_extent_mapping(em_tree
, em
);
109 if (ret
== -EEXIST
) {
110 u64 failed_start
= em
->start
;
111 u64 failed_len
= em
->len
;
113 printk("failed to insert %Lu %Lu -> %Lu into tree\n",
114 em
->start
, em
->len
, em
->block_start
);
116 em
= lookup_extent_mapping(em_tree
, start
, len
);
118 printk("after failing, found %Lu %Lu %Lu\n",
119 em
->start
, em
->len
, em
->block_start
);
122 em
= lookup_extent_mapping(em_tree
, failed_start
,
125 printk("double failure lookup gives us "
126 "%Lu %Lu -> %Lu\n", em
->start
,
127 em
->len
, em
->block_start
);
136 spin_unlock(&em_tree
->lock
);
144 u32
btrfs_csum_data(struct btrfs_root
*root
, char *data
, u32 seed
, size_t len
)
146 return btrfs_crc32c(seed
, data
, len
);
149 void btrfs_csum_final(u32 crc
, char *result
)
151 *(__le32
*)result
= ~cpu_to_le32(crc
);
154 static int csum_tree_block(struct btrfs_root
*root
, struct extent_buffer
*buf
,
157 char result
[BTRFS_CRC32_SIZE
];
159 unsigned long cur_len
;
160 unsigned long offset
= BTRFS_CSUM_SIZE
;
161 char *map_token
= NULL
;
163 unsigned long map_start
;
164 unsigned long map_len
;
168 len
= buf
->len
- offset
;
170 err
= map_private_extent_buffer(buf
, offset
, 32,
172 &map_start
, &map_len
, KM_USER0
);
174 printk("failed to map extent buffer! %lu\n",
178 cur_len
= min(len
, map_len
- (offset
- map_start
));
179 crc
= btrfs_csum_data(root
, kaddr
+ offset
- map_start
,
183 unmap_extent_buffer(buf
, map_token
, KM_USER0
);
185 btrfs_csum_final(crc
, result
);
188 /* FIXME, this is not good */
189 if (memcmp_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
)) {
192 memcpy(&found
, result
, BTRFS_CRC32_SIZE
);
194 read_extent_buffer(buf
, &val
, 0, BTRFS_CRC32_SIZE
);
195 printk("btrfs: %s checksum verify failed on %llu "
196 "wanted %X found %X level %d\n",
197 root
->fs_info
->sb
->s_id
,
198 buf
->start
, val
, found
, btrfs_header_level(buf
));
202 write_extent_buffer(buf
, result
, 0, BTRFS_CRC32_SIZE
);
207 static int verify_parent_transid(struct extent_io_tree
*io_tree
,
208 struct extent_buffer
*eb
, u64 parent_transid
)
212 if (!parent_transid
|| btrfs_header_generation(eb
) == parent_transid
)
215 lock_extent(io_tree
, eb
->start
, eb
->start
+ eb
->len
- 1, GFP_NOFS
);
216 if (extent_buffer_uptodate(io_tree
, eb
) &&
217 btrfs_header_generation(eb
) == parent_transid
) {
221 printk("parent transid verify failed on %llu wanted %llu found %llu\n",
222 (unsigned long long)eb
->start
,
223 (unsigned long long)parent_transid
,
224 (unsigned long long)btrfs_header_generation(eb
));
226 clear_extent_buffer_uptodate(io_tree
, eb
);
228 unlock_extent(io_tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
234 static int btree_read_extent_buffer_pages(struct btrfs_root
*root
,
235 struct extent_buffer
*eb
,
236 u64 start
, u64 parent_transid
)
238 struct extent_io_tree
*io_tree
;
243 io_tree
= &BTRFS_I(root
->fs_info
->btree_inode
)->io_tree
;
245 ret
= read_extent_buffer_pages(io_tree
, eb
, start
, 1,
246 btree_get_extent
, mirror_num
);
248 !verify_parent_transid(io_tree
, eb
, parent_transid
))
250 printk("read extent buffer pages failed with ret %d mirror no %d\n", ret
, mirror_num
);
251 num_copies
= btrfs_num_copies(&root
->fs_info
->mapping_tree
,
257 if (mirror_num
> num_copies
)
263 int csum_dirty_buffer(struct btrfs_root
*root
, struct page
*page
)
265 struct extent_io_tree
*tree
;
266 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
270 struct extent_buffer
*eb
;
273 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
275 if (page
->private == EXTENT_PAGE_PRIVATE
)
279 len
= page
->private >> 2;
283 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
284 ret
= btree_read_extent_buffer_pages(root
, eb
, start
+ PAGE_CACHE_SIZE
,
285 btrfs_header_generation(eb
));
287 found_start
= btrfs_header_bytenr(eb
);
288 if (found_start
!= start
) {
289 printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
290 start
, found_start
, len
);
294 if (eb
->first_page
!= page
) {
295 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
300 if (!PageUptodate(page
)) {
301 printk("csum not up to date page %lu\n", page
->index
);
305 found_level
= btrfs_header_level(eb
);
307 csum_tree_block(root
, eb
, 0);
309 free_extent_buffer(eb
);
314 int btree_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
315 struct extent_state
*state
)
317 struct extent_io_tree
*tree
;
321 struct extent_buffer
*eb
;
322 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
325 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
326 if (page
->private == EXTENT_PAGE_PRIVATE
)
330 len
= page
->private >> 2;
334 eb
= alloc_extent_buffer(tree
, start
, len
, page
, GFP_NOFS
);
336 found_start
= btrfs_header_bytenr(eb
);
337 if (found_start
!= start
) {
338 printk("bad tree block start %llu %llu\n",
339 (unsigned long long)found_start
,
340 (unsigned long long)eb
->start
);
344 if (eb
->first_page
!= page
) {
345 printk("bad first page %lu %lu\n", eb
->first_page
->index
,
351 if (memcmp_extent_buffer(eb
, root
->fs_info
->fsid
,
352 (unsigned long)btrfs_header_fsid(eb
),
354 printk("bad fsid on block %Lu\n", eb
->start
);
358 found_level
= btrfs_header_level(eb
);
360 ret
= csum_tree_block(root
, eb
, 1);
364 end
= min_t(u64
, eb
->len
, PAGE_CACHE_SIZE
);
365 end
= eb
->start
+ end
- 1;
367 free_extent_buffer(eb
);
372 static void end_workqueue_bio(struct bio
*bio
, int err
)
374 struct end_io_wq
*end_io_wq
= bio
->bi_private
;
375 struct btrfs_fs_info
*fs_info
;
377 fs_info
= end_io_wq
->info
;
378 end_io_wq
->error
= err
;
379 end_io_wq
->work
.func
= end_workqueue_fn
;
380 end_io_wq
->work
.flags
= 0;
381 if (bio
->bi_rw
& (1 << BIO_RW
))
382 btrfs_queue_worker(&fs_info
->endio_write_workers
,
385 btrfs_queue_worker(&fs_info
->endio_workers
, &end_io_wq
->work
);
388 int btrfs_bio_wq_end_io(struct btrfs_fs_info
*info
, struct bio
*bio
,
391 struct end_io_wq
*end_io_wq
;
392 end_io_wq
= kmalloc(sizeof(*end_io_wq
), GFP_NOFS
);
396 end_io_wq
->private = bio
->bi_private
;
397 end_io_wq
->end_io
= bio
->bi_end_io
;
398 end_io_wq
->info
= info
;
399 end_io_wq
->error
= 0;
400 end_io_wq
->bio
= bio
;
401 end_io_wq
->metadata
= metadata
;
403 bio
->bi_private
= end_io_wq
;
404 bio
->bi_end_io
= end_workqueue_bio
;
408 unsigned long btrfs_async_submit_limit(struct btrfs_fs_info
*info
)
410 unsigned long limit
= min_t(unsigned long,
411 info
->workers
.max_workers
,
412 info
->fs_devices
->open_devices
);
416 int btrfs_congested_async(struct btrfs_fs_info
*info
, int iodone
)
418 return atomic_read(&info
->nr_async_bios
) >
419 btrfs_async_submit_limit(info
);
422 static void run_one_async_submit(struct btrfs_work
*work
)
424 struct btrfs_fs_info
*fs_info
;
425 struct async_submit_bio
*async
;
428 async
= container_of(work
, struct async_submit_bio
, work
);
429 fs_info
= BTRFS_I(async
->inode
)->root
->fs_info
;
431 limit
= btrfs_async_submit_limit(fs_info
);
432 limit
= limit
* 2 / 3;
434 atomic_dec(&fs_info
->nr_async_submits
);
436 if (atomic_read(&fs_info
->nr_async_submits
) < limit
&&
437 waitqueue_active(&fs_info
->async_submit_wait
))
438 wake_up(&fs_info
->async_submit_wait
);
440 async
->submit_bio_hook(async
->inode
, async
->rw
, async
->bio
,
445 int btrfs_wq_submit_bio(struct btrfs_fs_info
*fs_info
, struct inode
*inode
,
446 int rw
, struct bio
*bio
, int mirror_num
,
447 extent_submit_bio_hook_t
*submit_bio_hook
)
449 struct async_submit_bio
*async
;
450 int limit
= btrfs_async_submit_limit(fs_info
);
452 async
= kmalloc(sizeof(*async
), GFP_NOFS
);
456 async
->inode
= inode
;
459 async
->mirror_num
= mirror_num
;
460 async
->submit_bio_hook
= submit_bio_hook
;
461 async
->work
.func
= run_one_async_submit
;
462 async
->work
.flags
= 0;
463 atomic_inc(&fs_info
->nr_async_submits
);
464 btrfs_queue_worker(&fs_info
->workers
, &async
->work
);
466 if (atomic_read(&fs_info
->nr_async_submits
) > limit
) {
467 wait_event_timeout(fs_info
->async_submit_wait
,
468 (atomic_read(&fs_info
->nr_async_submits
) < limit
),
471 wait_event_timeout(fs_info
->async_submit_wait
,
472 (atomic_read(&fs_info
->nr_async_bios
) < limit
),
478 static int btree_csum_one_bio(struct bio
*bio
)
480 struct bio_vec
*bvec
= bio
->bi_io_vec
;
482 struct btrfs_root
*root
;
484 WARN_ON(bio
->bi_vcnt
<= 0);
485 while(bio_index
< bio
->bi_vcnt
) {
486 root
= BTRFS_I(bvec
->bv_page
->mapping
->host
)->root
;
487 csum_dirty_buffer(root
, bvec
->bv_page
);
494 static int __btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
497 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
501 offset
= bio
->bi_sector
<< 9;
504 * when we're called for a write, we're already in the async
505 * submission context. Just jump into btrfs_map_bio
507 if (rw
& (1 << BIO_RW
)) {
508 btree_csum_one_bio(bio
);
509 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
,
514 * called for a read, do the setup so that checksum validation
515 * can happen in the async kernel threads
517 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 1);
520 return btrfs_map_bio(BTRFS_I(inode
)->root
, rw
, bio
, mirror_num
, 1);
523 static int btree_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
527 * kthread helpers are used to submit writes so that checksumming
528 * can happen in parallel across all CPUs
530 if (!(rw
& (1 << BIO_RW
))) {
531 return __btree_submit_bio_hook(inode
, rw
, bio
, mirror_num
);
533 return btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
534 inode
, rw
, bio
, mirror_num
,
535 __btree_submit_bio_hook
);
538 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
540 struct extent_io_tree
*tree
;
541 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
543 if (current
->flags
& PF_MEMALLOC
) {
544 redirty_page_for_writepage(wbc
, page
);
548 return extent_write_full_page(tree
, page
, btree_get_extent
, wbc
);
551 static int btree_writepages(struct address_space
*mapping
,
552 struct writeback_control
*wbc
)
554 struct extent_io_tree
*tree
;
555 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
556 if (wbc
->sync_mode
== WB_SYNC_NONE
) {
559 unsigned long thresh
= 32 * 1024 * 1024;
561 if (wbc
->for_kupdate
)
564 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
565 thresh
, EXTENT_DIRTY
);
566 if (num_dirty
< thresh
) {
570 return extent_writepages(tree
, mapping
, btree_get_extent
, wbc
);
573 int btree_readpage(struct file
*file
, struct page
*page
)
575 struct extent_io_tree
*tree
;
576 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
577 return extent_read_full_page(tree
, page
, btree_get_extent
);
580 static int btree_releasepage(struct page
*page
, gfp_t gfp_flags
)
582 struct extent_io_tree
*tree
;
583 struct extent_map_tree
*map
;
586 if (PageWriteback(page
) || PageDirty(page
))
589 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
590 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
592 ret
= try_release_extent_state(map
, tree
, page
, gfp_flags
);
597 ret
= try_release_extent_buffer(tree
, page
);
599 ClearPagePrivate(page
);
600 set_page_private(page
, 0);
601 page_cache_release(page
);
607 static void btree_invalidatepage(struct page
*page
, unsigned long offset
)
609 struct extent_io_tree
*tree
;
610 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
611 extent_invalidatepage(tree
, page
, offset
);
612 btree_releasepage(page
, GFP_NOFS
);
613 if (PagePrivate(page
)) {
614 printk("warning page private not zero on page %Lu\n",
616 ClearPagePrivate(page
);
617 set_page_private(page
, 0);
618 page_cache_release(page
);
623 static int btree_writepage(struct page
*page
, struct writeback_control
*wbc
)
625 struct buffer_head
*bh
;
626 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
627 struct buffer_head
*head
;
628 if (!page_has_buffers(page
)) {
629 create_empty_buffers(page
, root
->fs_info
->sb
->s_blocksize
,
630 (1 << BH_Dirty
)|(1 << BH_Uptodate
));
632 head
= page_buffers(page
);
635 if (buffer_dirty(bh
))
636 csum_tree_block(root
, bh
, 0);
637 bh
= bh
->b_this_page
;
638 } while (bh
!= head
);
639 return block_write_full_page(page
, btree_get_block
, wbc
);
643 static struct address_space_operations btree_aops
= {
644 .readpage
= btree_readpage
,
645 .writepage
= btree_writepage
,
646 .writepages
= btree_writepages
,
647 .releasepage
= btree_releasepage
,
648 .invalidatepage
= btree_invalidatepage
,
649 .sync_page
= block_sync_page
,
652 int readahead_tree_block(struct btrfs_root
*root
, u64 bytenr
, u32 blocksize
,
655 struct extent_buffer
*buf
= NULL
;
656 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
659 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
662 read_extent_buffer_pages(&BTRFS_I(btree_inode
)->io_tree
,
663 buf
, 0, 0, btree_get_extent
, 0);
664 free_extent_buffer(buf
);
668 struct extent_buffer
*btrfs_find_tree_block(struct btrfs_root
*root
,
669 u64 bytenr
, u32 blocksize
)
671 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
672 struct extent_buffer
*eb
;
673 eb
= find_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
674 bytenr
, blocksize
, GFP_NOFS
);
678 struct extent_buffer
*btrfs_find_create_tree_block(struct btrfs_root
*root
,
679 u64 bytenr
, u32 blocksize
)
681 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
682 struct extent_buffer
*eb
;
684 eb
= alloc_extent_buffer(&BTRFS_I(btree_inode
)->io_tree
,
685 bytenr
, blocksize
, NULL
, GFP_NOFS
);
690 int btrfs_write_tree_block(struct extent_buffer
*buf
)
692 return btrfs_fdatawrite_range(buf
->first_page
->mapping
, buf
->start
,
693 buf
->start
+ buf
->len
- 1, WB_SYNC_ALL
);
696 int btrfs_wait_tree_block_writeback(struct extent_buffer
*buf
)
698 return btrfs_wait_on_page_writeback_range(buf
->first_page
->mapping
,
699 buf
->start
, buf
->start
+ buf
->len
-1);
702 struct extent_buffer
*read_tree_block(struct btrfs_root
*root
, u64 bytenr
,
703 u32 blocksize
, u64 parent_transid
)
705 struct extent_buffer
*buf
= NULL
;
706 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
707 struct extent_io_tree
*io_tree
;
710 io_tree
= &BTRFS_I(btree_inode
)->io_tree
;
712 buf
= btrfs_find_create_tree_block(root
, bytenr
, blocksize
);
716 ret
= btree_read_extent_buffer_pages(root
, buf
, 0, parent_transid
);
719 buf
->flags
|= EXTENT_UPTODATE
;
727 int clean_tree_block(struct btrfs_trans_handle
*trans
, struct btrfs_root
*root
,
728 struct extent_buffer
*buf
)
730 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
731 if (btrfs_header_generation(buf
) ==
732 root
->fs_info
->running_transaction
->transid
) {
733 WARN_ON(!btrfs_tree_locked(buf
));
734 clear_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
,
740 static int __setup_root(u32 nodesize
, u32 leafsize
, u32 sectorsize
,
741 u32 stripesize
, struct btrfs_root
*root
,
742 struct btrfs_fs_info
*fs_info
,
747 root
->commit_root
= NULL
;
748 root
->ref_tree
= NULL
;
749 root
->sectorsize
= sectorsize
;
750 root
->nodesize
= nodesize
;
751 root
->leafsize
= leafsize
;
752 root
->stripesize
= stripesize
;
754 root
->track_dirty
= 0;
756 root
->fs_info
= fs_info
;
757 root
->objectid
= objectid
;
758 root
->last_trans
= 0;
759 root
->highest_inode
= 0;
760 root
->last_inode_alloc
= 0;
764 INIT_LIST_HEAD(&root
->dirty_list
);
765 INIT_LIST_HEAD(&root
->orphan_list
);
766 INIT_LIST_HEAD(&root
->dead_list
);
767 spin_lock_init(&root
->node_lock
);
768 spin_lock_init(&root
->list_lock
);
769 mutex_init(&root
->objectid_mutex
);
770 mutex_init(&root
->log_mutex
);
771 extent_io_tree_init(&root
->dirty_log_pages
,
772 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
774 btrfs_leaf_ref_tree_init(&root
->ref_tree_struct
);
775 root
->ref_tree
= &root
->ref_tree_struct
;
777 memset(&root
->root_key
, 0, sizeof(root
->root_key
));
778 memset(&root
->root_item
, 0, sizeof(root
->root_item
));
779 memset(&root
->defrag_progress
, 0, sizeof(root
->defrag_progress
));
780 memset(&root
->root_kobj
, 0, sizeof(root
->root_kobj
));
781 root
->defrag_trans_start
= fs_info
->generation
;
782 init_completion(&root
->kobj_unregister
);
783 root
->defrag_running
= 0;
784 root
->defrag_level
= 0;
785 root
->root_key
.objectid
= objectid
;
789 static int find_and_setup_root(struct btrfs_root
*tree_root
,
790 struct btrfs_fs_info
*fs_info
,
792 struct btrfs_root
*root
)
797 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
798 tree_root
->sectorsize
, tree_root
->stripesize
,
799 root
, fs_info
, objectid
);
800 ret
= btrfs_find_last_root(tree_root
, objectid
,
801 &root
->root_item
, &root
->root_key
);
804 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
805 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
811 int btrfs_free_log_root_tree(struct btrfs_trans_handle
*trans
,
812 struct btrfs_fs_info
*fs_info
)
814 struct extent_buffer
*eb
;
815 struct btrfs_root
*log_root_tree
= fs_info
->log_root_tree
;
824 ret
= find_first_extent_bit(&log_root_tree
->dirty_log_pages
,
825 0, &start
, &end
, EXTENT_DIRTY
);
829 clear_extent_dirty(&log_root_tree
->dirty_log_pages
,
830 start
, end
, GFP_NOFS
);
832 eb
= fs_info
->log_root_tree
->node
;
834 WARN_ON(btrfs_header_level(eb
) != 0);
835 WARN_ON(btrfs_header_nritems(eb
) != 0);
837 ret
= btrfs_free_reserved_extent(fs_info
->tree_root
,
841 free_extent_buffer(eb
);
842 kfree(fs_info
->log_root_tree
);
843 fs_info
->log_root_tree
= NULL
;
847 int btrfs_init_log_root_tree(struct btrfs_trans_handle
*trans
,
848 struct btrfs_fs_info
*fs_info
)
850 struct btrfs_root
*root
;
851 struct btrfs_root
*tree_root
= fs_info
->tree_root
;
853 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
857 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
858 tree_root
->sectorsize
, tree_root
->stripesize
,
859 root
, fs_info
, BTRFS_TREE_LOG_OBJECTID
);
861 root
->root_key
.objectid
= BTRFS_TREE_LOG_OBJECTID
;
862 root
->root_key
.type
= BTRFS_ROOT_ITEM_KEY
;
863 root
->root_key
.offset
= BTRFS_TREE_LOG_OBJECTID
;
866 root
->node
= btrfs_alloc_free_block(trans
, root
, root
->leafsize
,
867 0, BTRFS_TREE_LOG_OBJECTID
,
868 trans
->transid
, 0, 0, 0);
870 btrfs_set_header_nritems(root
->node
, 0);
871 btrfs_set_header_level(root
->node
, 0);
872 btrfs_set_header_bytenr(root
->node
, root
->node
->start
);
873 btrfs_set_header_generation(root
->node
, trans
->transid
);
874 btrfs_set_header_owner(root
->node
, BTRFS_TREE_LOG_OBJECTID
);
876 write_extent_buffer(root
->node
, root
->fs_info
->fsid
,
877 (unsigned long)btrfs_header_fsid(root
->node
),
879 btrfs_mark_buffer_dirty(root
->node
);
880 btrfs_tree_unlock(root
->node
);
881 fs_info
->log_root_tree
= root
;
885 struct btrfs_root
*btrfs_read_fs_root_no_radix(struct btrfs_root
*tree_root
,
886 struct btrfs_key
*location
)
888 struct btrfs_root
*root
;
889 struct btrfs_fs_info
*fs_info
= tree_root
->fs_info
;
890 struct btrfs_path
*path
;
891 struct extent_buffer
*l
;
896 root
= kzalloc(sizeof(*root
), GFP_NOFS
);
898 return ERR_PTR(-ENOMEM
);
899 if (location
->offset
== (u64
)-1) {
900 ret
= find_and_setup_root(tree_root
, fs_info
,
901 location
->objectid
, root
);
909 __setup_root(tree_root
->nodesize
, tree_root
->leafsize
,
910 tree_root
->sectorsize
, tree_root
->stripesize
,
911 root
, fs_info
, location
->objectid
);
913 path
= btrfs_alloc_path();
915 ret
= btrfs_search_slot(NULL
, tree_root
, location
, path
, 0, 0);
922 read_extent_buffer(l
, &root
->root_item
,
923 btrfs_item_ptr_offset(l
, path
->slots
[0]),
924 sizeof(root
->root_item
));
925 memcpy(&root
->root_key
, location
, sizeof(*location
));
928 btrfs_release_path(root
, path
);
929 btrfs_free_path(path
);
934 blocksize
= btrfs_level_size(root
, btrfs_root_level(&root
->root_item
));
935 root
->node
= read_tree_block(root
, btrfs_root_bytenr(&root
->root_item
),
939 if (location
->objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
941 ret
= btrfs_find_highest_inode(root
, &highest_inode
);
943 root
->highest_inode
= highest_inode
;
944 root
->last_inode_alloc
= highest_inode
;
950 struct btrfs_root
*btrfs_lookup_fs_root(struct btrfs_fs_info
*fs_info
,
953 struct btrfs_root
*root
;
955 if (root_objectid
== BTRFS_ROOT_TREE_OBJECTID
)
956 return fs_info
->tree_root
;
957 if (root_objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
958 return fs_info
->extent_root
;
960 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
961 (unsigned long)root_objectid
);
965 struct btrfs_root
*btrfs_read_fs_root_no_name(struct btrfs_fs_info
*fs_info
,
966 struct btrfs_key
*location
)
968 struct btrfs_root
*root
;
971 if (location
->objectid
== BTRFS_ROOT_TREE_OBJECTID
)
972 return fs_info
->tree_root
;
973 if (location
->objectid
== BTRFS_EXTENT_TREE_OBJECTID
)
974 return fs_info
->extent_root
;
975 if (location
->objectid
== BTRFS_CHUNK_TREE_OBJECTID
)
976 return fs_info
->chunk_root
;
977 if (location
->objectid
== BTRFS_DEV_TREE_OBJECTID
)
978 return fs_info
->dev_root
;
980 root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
981 (unsigned long)location
->objectid
);
985 root
= btrfs_read_fs_root_no_radix(fs_info
->tree_root
, location
);
988 ret
= radix_tree_insert(&fs_info
->fs_roots_radix
,
989 (unsigned long)root
->root_key
.objectid
,
992 free_extent_buffer(root
->node
);
996 ret
= btrfs_find_dead_roots(fs_info
->tree_root
,
997 root
->root_key
.objectid
, root
);
1003 struct btrfs_root
*btrfs_read_fs_root(struct btrfs_fs_info
*fs_info
,
1004 struct btrfs_key
*location
,
1005 const char *name
, int namelen
)
1007 struct btrfs_root
*root
;
1010 root
= btrfs_read_fs_root_no_name(fs_info
, location
);
1017 ret
= btrfs_set_root_name(root
, name
, namelen
);
1019 free_extent_buffer(root
->node
);
1021 return ERR_PTR(ret
);
1024 ret
= btrfs_sysfs_add_root(root
);
1026 free_extent_buffer(root
->node
);
1029 return ERR_PTR(ret
);
1035 static int add_hasher(struct btrfs_fs_info
*info
, char *type
) {
1036 struct btrfs_hasher
*hasher
;
1038 hasher
= kmalloc(sizeof(*hasher
), GFP_NOFS
);
1041 hasher
->hash_tfm
= crypto_alloc_hash(type
, 0, CRYPTO_ALG_ASYNC
);
1042 if (!hasher
->hash_tfm
) {
1046 spin_lock(&info
->hash_lock
);
1047 list_add(&hasher
->list
, &info
->hashers
);
1048 spin_unlock(&info
->hash_lock
);
1053 static int btrfs_congested_fn(void *congested_data
, int bdi_bits
)
1055 struct btrfs_fs_info
*info
= (struct btrfs_fs_info
*)congested_data
;
1057 struct list_head
*cur
;
1058 struct btrfs_device
*device
;
1059 struct backing_dev_info
*bdi
;
1061 if ((bdi_bits
& (1 << BDI_write_congested
)) &&
1062 btrfs_congested_async(info
, 0))
1065 list_for_each(cur
, &info
->fs_devices
->devices
) {
1066 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1069 bdi
= blk_get_backing_dev_info(device
->bdev
);
1070 if (bdi
&& bdi_congested(bdi
, bdi_bits
)) {
1079 * this unplugs every device on the box, and it is only used when page
1082 static void __unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
1084 struct list_head
*cur
;
1085 struct btrfs_device
*device
;
1086 struct btrfs_fs_info
*info
;
1088 info
= (struct btrfs_fs_info
*)bdi
->unplug_io_data
;
1089 list_for_each(cur
, &info
->fs_devices
->devices
) {
1090 device
= list_entry(cur
, struct btrfs_device
, dev_list
);
1091 bdi
= blk_get_backing_dev_info(device
->bdev
);
1092 if (bdi
->unplug_io_fn
) {
1093 bdi
->unplug_io_fn(bdi
, page
);
1098 void btrfs_unplug_io_fn(struct backing_dev_info
*bdi
, struct page
*page
)
1100 struct inode
*inode
;
1101 struct extent_map_tree
*em_tree
;
1102 struct extent_map
*em
;
1103 struct address_space
*mapping
;
1106 /* the generic O_DIRECT read code does this */
1108 __unplug_io_fn(bdi
, page
);
1113 * page->mapping may change at any time. Get a consistent copy
1114 * and use that for everything below
1117 mapping
= page
->mapping
;
1121 inode
= mapping
->host
;
1122 offset
= page_offset(page
);
1124 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1125 spin_lock(&em_tree
->lock
);
1126 em
= lookup_extent_mapping(em_tree
, offset
, PAGE_CACHE_SIZE
);
1127 spin_unlock(&em_tree
->lock
);
1129 __unplug_io_fn(bdi
, page
);
1133 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
1134 free_extent_map(em
);
1135 __unplug_io_fn(bdi
, page
);
1138 offset
= offset
- em
->start
;
1139 btrfs_unplug_page(&BTRFS_I(inode
)->root
->fs_info
->mapping_tree
,
1140 em
->block_start
+ offset
, page
);
1141 free_extent_map(em
);
1144 static int setup_bdi(struct btrfs_fs_info
*info
, struct backing_dev_info
*bdi
)
1147 bdi
->ra_pages
= default_backing_dev_info
.ra_pages
;
1149 bdi
->capabilities
= default_backing_dev_info
.capabilities
;
1150 bdi
->unplug_io_fn
= btrfs_unplug_io_fn
;
1151 bdi
->unplug_io_data
= info
;
1152 bdi
->congested_fn
= btrfs_congested_fn
;
1153 bdi
->congested_data
= info
;
1157 static int bio_ready_for_csum(struct bio
*bio
)
1163 struct extent_io_tree
*io_tree
= NULL
;
1164 struct btrfs_fs_info
*info
= NULL
;
1165 struct bio_vec
*bvec
;
1169 bio_for_each_segment(bvec
, bio
, i
) {
1170 page
= bvec
->bv_page
;
1171 if (page
->private == EXTENT_PAGE_PRIVATE
) {
1172 length
+= bvec
->bv_len
;
1175 if (!page
->private) {
1176 length
+= bvec
->bv_len
;
1179 length
= bvec
->bv_len
;
1180 buf_len
= page
->private >> 2;
1181 start
= page_offset(page
) + bvec
->bv_offset
;
1182 io_tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1183 info
= BTRFS_I(page
->mapping
->host
)->root
->fs_info
;
1185 /* are we fully contained in this bio? */
1186 if (buf_len
<= length
)
1189 ret
= extent_range_uptodate(io_tree
, start
+ length
,
1190 start
+ buf_len
- 1);
1197 * called by the kthread helper functions to finally call the bio end_io
1198 * functions. This is where read checksum verification actually happens
1200 static void end_workqueue_fn(struct btrfs_work
*work
)
1203 struct end_io_wq
*end_io_wq
;
1204 struct btrfs_fs_info
*fs_info
;
1207 end_io_wq
= container_of(work
, struct end_io_wq
, work
);
1208 bio
= end_io_wq
->bio
;
1209 fs_info
= end_io_wq
->info
;
1211 /* metadata bios are special because the whole tree block must
1212 * be checksummed at once. This makes sure the entire block is in
1213 * ram and up to date before trying to verify things. For
1214 * blocksize <= pagesize, it is basically a noop
1216 if (end_io_wq
->metadata
&& !bio_ready_for_csum(bio
)) {
1217 btrfs_queue_worker(&fs_info
->endio_workers
,
1221 error
= end_io_wq
->error
;
1222 bio
->bi_private
= end_io_wq
->private;
1223 bio
->bi_end_io
= end_io_wq
->end_io
;
1225 bio_endio(bio
, error
);
1228 static int cleaner_kthread(void *arg
)
1230 struct btrfs_root
*root
= arg
;
1234 if (root
->fs_info
->closing
)
1237 vfs_check_frozen(root
->fs_info
->sb
, SB_FREEZE_WRITE
);
1238 mutex_lock(&root
->fs_info
->cleaner_mutex
);
1239 btrfs_clean_old_snapshots(root
);
1240 mutex_unlock(&root
->fs_info
->cleaner_mutex
);
1242 if (freezing(current
)) {
1246 if (root
->fs_info
->closing
)
1248 set_current_state(TASK_INTERRUPTIBLE
);
1250 __set_current_state(TASK_RUNNING
);
1252 } while (!kthread_should_stop());
1256 static int transaction_kthread(void *arg
)
1258 struct btrfs_root
*root
= arg
;
1259 struct btrfs_trans_handle
*trans
;
1260 struct btrfs_transaction
*cur
;
1262 unsigned long delay
;
1267 if (root
->fs_info
->closing
)
1271 vfs_check_frozen(root
->fs_info
->sb
, SB_FREEZE_WRITE
);
1272 mutex_lock(&root
->fs_info
->transaction_kthread_mutex
);
1274 if (root
->fs_info
->total_ref_cache_size
> 20 * 1024 * 1024) {
1275 printk("btrfs: total reference cache size %Lu\n",
1276 root
->fs_info
->total_ref_cache_size
);
1279 mutex_lock(&root
->fs_info
->trans_mutex
);
1280 cur
= root
->fs_info
->running_transaction
;
1282 mutex_unlock(&root
->fs_info
->trans_mutex
);
1286 now
= get_seconds();
1287 if (now
< cur
->start_time
|| now
- cur
->start_time
< 30) {
1288 mutex_unlock(&root
->fs_info
->trans_mutex
);
1292 mutex_unlock(&root
->fs_info
->trans_mutex
);
1293 trans
= btrfs_start_transaction(root
, 1);
1294 ret
= btrfs_commit_transaction(trans
, root
);
1296 wake_up_process(root
->fs_info
->cleaner_kthread
);
1297 mutex_unlock(&root
->fs_info
->transaction_kthread_mutex
);
1299 if (freezing(current
)) {
1302 if (root
->fs_info
->closing
)
1304 set_current_state(TASK_INTERRUPTIBLE
);
1305 schedule_timeout(delay
);
1306 __set_current_state(TASK_RUNNING
);
1308 } while (!kthread_should_stop());
1312 struct btrfs_root
*open_ctree(struct super_block
*sb
,
1313 struct btrfs_fs_devices
*fs_devices
,
1321 struct buffer_head
*bh
;
1322 struct btrfs_root
*extent_root
= kzalloc(sizeof(struct btrfs_root
),
1324 struct btrfs_root
*tree_root
= kzalloc(sizeof(struct btrfs_root
),
1326 struct btrfs_fs_info
*fs_info
= kzalloc(sizeof(*fs_info
),
1328 struct btrfs_root
*chunk_root
= kzalloc(sizeof(struct btrfs_root
),
1330 struct btrfs_root
*dev_root
= kzalloc(sizeof(struct btrfs_root
),
1332 struct btrfs_root
*log_tree_root
;
1337 struct btrfs_super_block
*disk_super
;
1339 if (!extent_root
|| !tree_root
|| !fs_info
) {
1343 INIT_RADIX_TREE(&fs_info
->fs_roots_radix
, GFP_NOFS
);
1344 INIT_LIST_HEAD(&fs_info
->trans_list
);
1345 INIT_LIST_HEAD(&fs_info
->dead_roots
);
1346 INIT_LIST_HEAD(&fs_info
->hashers
);
1347 INIT_LIST_HEAD(&fs_info
->delalloc_inodes
);
1348 spin_lock_init(&fs_info
->hash_lock
);
1349 spin_lock_init(&fs_info
->delalloc_lock
);
1350 spin_lock_init(&fs_info
->new_trans_lock
);
1351 spin_lock_init(&fs_info
->ref_cache_lock
);
1353 init_completion(&fs_info
->kobj_unregister
);
1354 fs_info
->tree_root
= tree_root
;
1355 fs_info
->extent_root
= extent_root
;
1356 fs_info
->chunk_root
= chunk_root
;
1357 fs_info
->dev_root
= dev_root
;
1358 fs_info
->fs_devices
= fs_devices
;
1359 INIT_LIST_HEAD(&fs_info
->dirty_cowonly_roots
);
1360 INIT_LIST_HEAD(&fs_info
->space_info
);
1361 btrfs_mapping_init(&fs_info
->mapping_tree
);
1362 atomic_set(&fs_info
->nr_async_submits
, 0);
1363 atomic_set(&fs_info
->nr_async_bios
, 0);
1364 atomic_set(&fs_info
->throttles
, 0);
1365 atomic_set(&fs_info
->throttle_gen
, 0);
1367 fs_info
->max_extent
= (u64
)-1;
1368 fs_info
->max_inline
= 8192 * 1024;
1369 setup_bdi(fs_info
, &fs_info
->bdi
);
1370 fs_info
->btree_inode
= new_inode(sb
);
1371 fs_info
->btree_inode
->i_ino
= 1;
1372 fs_info
->btree_inode
->i_nlink
= 1;
1373 fs_info
->thread_pool_size
= min(num_online_cpus() + 2, 8);
1375 INIT_LIST_HEAD(&fs_info
->ordered_extents
);
1376 spin_lock_init(&fs_info
->ordered_extent_lock
);
1378 sb
->s_blocksize
= 4096;
1379 sb
->s_blocksize_bits
= blksize_bits(4096);
1382 * we set the i_size on the btree inode to the max possible int.
1383 * the real end of the address space is determined by all of
1384 * the devices in the system
1386 fs_info
->btree_inode
->i_size
= OFFSET_MAX
;
1387 fs_info
->btree_inode
->i_mapping
->a_ops
= &btree_aops
;
1388 fs_info
->btree_inode
->i_mapping
->backing_dev_info
= &fs_info
->bdi
;
1390 extent_io_tree_init(&BTRFS_I(fs_info
->btree_inode
)->io_tree
,
1391 fs_info
->btree_inode
->i_mapping
,
1393 extent_map_tree_init(&BTRFS_I(fs_info
->btree_inode
)->extent_tree
,
1396 BTRFS_I(fs_info
->btree_inode
)->io_tree
.ops
= &btree_extent_io_ops
;
1398 spin_lock_init(&fs_info
->block_group_cache_lock
);
1399 fs_info
->block_group_cache_tree
.rb_node
= NULL
;
1401 extent_io_tree_init(&fs_info
->pinned_extents
,
1402 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1403 extent_io_tree_init(&fs_info
->pending_del
,
1404 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1405 extent_io_tree_init(&fs_info
->extent_ins
,
1406 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1407 fs_info
->do_barriers
= 1;
1409 extent_io_tree_init(&fs_info
->reloc_mapping_tree
,
1410 fs_info
->btree_inode
->i_mapping
, GFP_NOFS
);
1411 INIT_LIST_HEAD(&fs_info
->dead_reloc_roots
);
1412 btrfs_leaf_ref_tree_init(&fs_info
->reloc_ref_tree
);
1413 btrfs_leaf_ref_tree_init(&fs_info
->shared_ref_tree
);
1415 BTRFS_I(fs_info
->btree_inode
)->root
= tree_root
;
1416 memset(&BTRFS_I(fs_info
->btree_inode
)->location
, 0,
1417 sizeof(struct btrfs_key
));
1418 insert_inode_hash(fs_info
->btree_inode
);
1420 mutex_init(&fs_info
->trans_mutex
);
1421 mutex_init(&fs_info
->tree_log_mutex
);
1422 mutex_init(&fs_info
->drop_mutex
);
1423 mutex_init(&fs_info
->alloc_mutex
);
1424 mutex_init(&fs_info
->chunk_mutex
);
1425 mutex_init(&fs_info
->transaction_kthread_mutex
);
1426 mutex_init(&fs_info
->cleaner_mutex
);
1427 mutex_init(&fs_info
->volume_mutex
);
1428 mutex_init(&fs_info
->tree_reloc_mutex
);
1429 init_waitqueue_head(&fs_info
->transaction_throttle
);
1430 init_waitqueue_head(&fs_info
->transaction_wait
);
1431 init_waitqueue_head(&fs_info
->async_submit_wait
);
1432 init_waitqueue_head(&fs_info
->tree_log_wait
);
1433 atomic_set(&fs_info
->tree_log_commit
, 0);
1434 atomic_set(&fs_info
->tree_log_writers
, 0);
1435 fs_info
->tree_log_transid
= 0;
1438 ret
= add_hasher(fs_info
, "crc32c");
1440 printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
1445 __setup_root(4096, 4096, 4096, 4096, tree_root
,
1446 fs_info
, BTRFS_ROOT_TREE_OBJECTID
);
1449 bh
= __bread(fs_devices
->latest_bdev
,
1450 BTRFS_SUPER_INFO_OFFSET
/ 4096, 4096);
1454 memcpy(&fs_info
->super_copy
, bh
->b_data
, sizeof(fs_info
->super_copy
));
1457 memcpy(fs_info
->fsid
, fs_info
->super_copy
.fsid
, BTRFS_FSID_SIZE
);
1459 disk_super
= &fs_info
->super_copy
;
1460 if (!btrfs_super_root(disk_super
))
1461 goto fail_sb_buffer
;
1463 err
= btrfs_parse_options(tree_root
, options
);
1465 goto fail_sb_buffer
;
1468 * we need to start all the end_io workers up front because the
1469 * queue work function gets called at interrupt time, and so it
1470 * cannot dynamically grow.
1472 btrfs_init_workers(&fs_info
->workers
, "worker",
1473 fs_info
->thread_pool_size
);
1474 btrfs_init_workers(&fs_info
->submit_workers
, "submit",
1475 min_t(u64
, fs_devices
->num_devices
,
1476 fs_info
->thread_pool_size
));
1478 /* a higher idle thresh on the submit workers makes it much more
1479 * likely that bios will be send down in a sane order to the
1482 fs_info
->submit_workers
.idle_thresh
= 64;
1484 /* fs_info->workers is responsible for checksumming file data
1485 * blocks and metadata. Using a larger idle thresh allows each
1486 * worker thread to operate on things in roughly the order they
1487 * were sent by the writeback daemons, improving overall locality
1488 * of the IO going down the pipe.
1490 fs_info
->workers
.idle_thresh
= 128;
1492 btrfs_init_workers(&fs_info
->fixup_workers
, "fixup", 1);
1493 btrfs_init_workers(&fs_info
->endio_workers
, "endio",
1494 fs_info
->thread_pool_size
);
1495 btrfs_init_workers(&fs_info
->endio_write_workers
, "endio-write",
1496 fs_info
->thread_pool_size
);
1499 * endios are largely parallel and should have a very
1502 fs_info
->endio_workers
.idle_thresh
= 4;
1503 fs_info
->endio_write_workers
.idle_thresh
= 64;
1505 btrfs_start_workers(&fs_info
->workers
, 1);
1506 btrfs_start_workers(&fs_info
->submit_workers
, 1);
1507 btrfs_start_workers(&fs_info
->fixup_workers
, 1);
1508 btrfs_start_workers(&fs_info
->endio_workers
, fs_info
->thread_pool_size
);
1509 btrfs_start_workers(&fs_info
->endio_write_workers
,
1510 fs_info
->thread_pool_size
);
1513 if (btrfs_super_num_devices(disk_super
) > fs_devices
->open_devices
) {
1514 printk("Btrfs: wanted %llu devices, but found %llu\n",
1515 (unsigned long long)btrfs_super_num_devices(disk_super
),
1516 (unsigned long long)fs_devices
->open_devices
);
1517 if (btrfs_test_opt(tree_root
, DEGRADED
))
1518 printk("continuing in degraded mode\n");
1520 goto fail_sb_buffer
;
1524 fs_info
->bdi
.ra_pages
*= btrfs_super_num_devices(disk_super
);
1526 nodesize
= btrfs_super_nodesize(disk_super
);
1527 leafsize
= btrfs_super_leafsize(disk_super
);
1528 sectorsize
= btrfs_super_sectorsize(disk_super
);
1529 stripesize
= btrfs_super_stripesize(disk_super
);
1530 tree_root
->nodesize
= nodesize
;
1531 tree_root
->leafsize
= leafsize
;
1532 tree_root
->sectorsize
= sectorsize
;
1533 tree_root
->stripesize
= stripesize
;
1535 sb
->s_blocksize
= sectorsize
;
1536 sb
->s_blocksize_bits
= blksize_bits(sectorsize
);
1538 if (strncmp((char *)(&disk_super
->magic
), BTRFS_MAGIC
,
1539 sizeof(disk_super
->magic
))) {
1540 printk("btrfs: valid FS not found on %s\n", sb
->s_id
);
1541 goto fail_sb_buffer
;
1544 mutex_lock(&fs_info
->chunk_mutex
);
1545 ret
= btrfs_read_sys_array(tree_root
);
1546 mutex_unlock(&fs_info
->chunk_mutex
);
1548 printk("btrfs: failed to read the system array on %s\n",
1550 goto fail_sys_array
;
1553 blocksize
= btrfs_level_size(tree_root
,
1554 btrfs_super_chunk_root_level(disk_super
));
1556 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
1557 chunk_root
, fs_info
, BTRFS_CHUNK_TREE_OBJECTID
);
1559 chunk_root
->node
= read_tree_block(chunk_root
,
1560 btrfs_super_chunk_root(disk_super
),
1562 BUG_ON(!chunk_root
->node
);
1564 read_extent_buffer(chunk_root
->node
, fs_info
->chunk_tree_uuid
,
1565 (unsigned long)btrfs_header_chunk_tree_uuid(chunk_root
->node
),
1568 mutex_lock(&fs_info
->chunk_mutex
);
1569 ret
= btrfs_read_chunk_tree(chunk_root
);
1570 mutex_unlock(&fs_info
->chunk_mutex
);
1573 btrfs_close_extra_devices(fs_devices
);
1575 blocksize
= btrfs_level_size(tree_root
,
1576 btrfs_super_root_level(disk_super
));
1579 tree_root
->node
= read_tree_block(tree_root
,
1580 btrfs_super_root(disk_super
),
1582 if (!tree_root
->node
)
1583 goto fail_sb_buffer
;
1586 ret
= find_and_setup_root(tree_root
, fs_info
,
1587 BTRFS_EXTENT_TREE_OBJECTID
, extent_root
);
1589 goto fail_tree_root
;
1590 extent_root
->track_dirty
= 1;
1592 ret
= find_and_setup_root(tree_root
, fs_info
,
1593 BTRFS_DEV_TREE_OBJECTID
, dev_root
);
1594 dev_root
->track_dirty
= 1;
1597 goto fail_extent_root
;
1599 btrfs_read_block_groups(extent_root
);
1601 fs_info
->generation
= btrfs_super_generation(disk_super
) + 1;
1602 fs_info
->data_alloc_profile
= (u64
)-1;
1603 fs_info
->metadata_alloc_profile
= (u64
)-1;
1604 fs_info
->system_alloc_profile
= fs_info
->metadata_alloc_profile
;
1605 fs_info
->cleaner_kthread
= kthread_run(cleaner_kthread
, tree_root
,
1607 if (!fs_info
->cleaner_kthread
)
1608 goto fail_extent_root
;
1610 fs_info
->transaction_kthread
= kthread_run(transaction_kthread
,
1612 "btrfs-transaction");
1613 if (!fs_info
->transaction_kthread
)
1616 if (btrfs_super_log_root(disk_super
) != 0) {
1618 u64 bytenr
= btrfs_super_log_root(disk_super
);
1621 btrfs_level_size(tree_root
,
1622 btrfs_super_log_root_level(disk_super
));
1624 log_tree_root
= kzalloc(sizeof(struct btrfs_root
),
1627 __setup_root(nodesize
, leafsize
, sectorsize
, stripesize
,
1628 log_tree_root
, fs_info
, BTRFS_TREE_LOG_OBJECTID
);
1630 log_tree_root
->node
= read_tree_block(tree_root
, bytenr
,
1632 ret
= btrfs_recover_log_trees(log_tree_root
);
1636 ret
= btrfs_cleanup_reloc_trees(tree_root
);
1639 fs_info
->last_trans_committed
= btrfs_super_generation(disk_super
);
1643 kthread_stop(fs_info
->cleaner_kthread
);
1645 free_extent_buffer(extent_root
->node
);
1647 free_extent_buffer(tree_root
->node
);
1650 btrfs_stop_workers(&fs_info
->fixup_workers
);
1651 btrfs_stop_workers(&fs_info
->workers
);
1652 btrfs_stop_workers(&fs_info
->endio_workers
);
1653 btrfs_stop_workers(&fs_info
->endio_write_workers
);
1654 btrfs_stop_workers(&fs_info
->submit_workers
);
1656 iput(fs_info
->btree_inode
);
1658 btrfs_close_devices(fs_info
->fs_devices
);
1659 btrfs_mapping_tree_free(&fs_info
->mapping_tree
);
1663 bdi_destroy(&fs_info
->bdi
);
1665 return ERR_PTR(err
);
1668 static void btrfs_end_buffer_write_sync(struct buffer_head
*bh
, int uptodate
)
1670 char b
[BDEVNAME_SIZE
];
1673 set_buffer_uptodate(bh
);
1675 if (!buffer_eopnotsupp(bh
) && printk_ratelimit()) {
1676 printk(KERN_WARNING
"lost page write due to "
1677 "I/O error on %s\n",
1678 bdevname(bh
->b_bdev
, b
));
1680 /* note, we dont' set_buffer_write_io_error because we have
1681 * our own ways of dealing with the IO errors
1683 clear_buffer_uptodate(bh
);
1689 int write_all_supers(struct btrfs_root
*root
)
1691 struct list_head
*cur
;
1692 struct list_head
*head
= &root
->fs_info
->fs_devices
->devices
;
1693 struct btrfs_device
*dev
;
1694 struct btrfs_super_block
*sb
;
1695 struct btrfs_dev_item
*dev_item
;
1696 struct buffer_head
*bh
;
1700 int total_errors
= 0;
1704 max_errors
= btrfs_super_num_devices(&root
->fs_info
->super_copy
) - 1;
1705 do_barriers
= !btrfs_test_opt(root
, NOBARRIER
);
1707 sb
= &root
->fs_info
->super_for_commit
;
1708 dev_item
= &sb
->dev_item
;
1709 list_for_each(cur
, head
) {
1710 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
1715 if (!dev
->in_fs_metadata
)
1718 btrfs_set_stack_device_type(dev_item
, dev
->type
);
1719 btrfs_set_stack_device_id(dev_item
, dev
->devid
);
1720 btrfs_set_stack_device_total_bytes(dev_item
, dev
->total_bytes
);
1721 btrfs_set_stack_device_bytes_used(dev_item
, dev
->bytes_used
);
1722 btrfs_set_stack_device_io_align(dev_item
, dev
->io_align
);
1723 btrfs_set_stack_device_io_width(dev_item
, dev
->io_width
);
1724 btrfs_set_stack_device_sector_size(dev_item
, dev
->sector_size
);
1725 memcpy(dev_item
->uuid
, dev
->uuid
, BTRFS_UUID_SIZE
);
1726 flags
= btrfs_super_flags(sb
);
1727 btrfs_set_super_flags(sb
, flags
| BTRFS_HEADER_FLAG_WRITTEN
);
1731 crc
= btrfs_csum_data(root
, (char *)sb
+ BTRFS_CSUM_SIZE
, crc
,
1732 BTRFS_SUPER_INFO_SIZE
- BTRFS_CSUM_SIZE
);
1733 btrfs_csum_final(crc
, sb
->csum
);
1735 bh
= __getblk(dev
->bdev
, BTRFS_SUPER_INFO_OFFSET
/ 4096,
1736 BTRFS_SUPER_INFO_SIZE
);
1738 memcpy(bh
->b_data
, sb
, BTRFS_SUPER_INFO_SIZE
);
1739 dev
->pending_io
= bh
;
1742 set_buffer_uptodate(bh
);
1744 bh
->b_end_io
= btrfs_end_buffer_write_sync
;
1746 if (do_barriers
&& dev
->barriers
) {
1747 ret
= submit_bh(WRITE_BARRIER
, bh
);
1748 if (ret
== -EOPNOTSUPP
) {
1749 printk("btrfs: disabling barriers on dev %s\n",
1751 set_buffer_uptodate(bh
);
1755 ret
= submit_bh(WRITE
, bh
);
1758 ret
= submit_bh(WRITE
, bh
);
1763 if (total_errors
> max_errors
) {
1764 printk("btrfs: %d errors while writing supers\n", total_errors
);
1769 list_for_each(cur
, head
) {
1770 dev
= list_entry(cur
, struct btrfs_device
, dev_list
);
1773 if (!dev
->in_fs_metadata
)
1776 BUG_ON(!dev
->pending_io
);
1777 bh
= dev
->pending_io
;
1779 if (!buffer_uptodate(dev
->pending_io
)) {
1780 if (do_barriers
&& dev
->barriers
) {
1781 printk("btrfs: disabling barriers on dev %s\n",
1783 set_buffer_uptodate(bh
);
1787 ret
= submit_bh(WRITE
, bh
);
1790 if (!buffer_uptodate(bh
))
1797 dev
->pending_io
= NULL
;
1800 if (total_errors
> max_errors
) {
1801 printk("btrfs: %d errors while writing supers\n", total_errors
);
1807 int write_ctree_super(struct btrfs_trans_handle
*trans
, struct btrfs_root
1812 ret
= write_all_supers(root
);
1816 int btrfs_free_fs_root(struct btrfs_fs_info
*fs_info
, struct btrfs_root
*root
)
1818 radix_tree_delete(&fs_info
->fs_roots_radix
,
1819 (unsigned long)root
->root_key
.objectid
);
1821 btrfs_sysfs_del_root(root
);
1825 free_extent_buffer(root
->node
);
1826 if (root
->commit_root
)
1827 free_extent_buffer(root
->commit_root
);
1834 static int del_fs_roots(struct btrfs_fs_info
*fs_info
)
1837 struct btrfs_root
*gang
[8];
1841 ret
= radix_tree_gang_lookup(&fs_info
->fs_roots_radix
,
1846 for (i
= 0; i
< ret
; i
++)
1847 btrfs_free_fs_root(fs_info
, gang
[i
]);
1852 int close_ctree(struct btrfs_root
*root
)
1855 struct btrfs_trans_handle
*trans
;
1856 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1858 fs_info
->closing
= 1;
1861 kthread_stop(root
->fs_info
->transaction_kthread
);
1862 kthread_stop(root
->fs_info
->cleaner_kthread
);
1864 btrfs_clean_old_snapshots(root
);
1865 trans
= btrfs_start_transaction(root
, 1);
1866 ret
= btrfs_commit_transaction(trans
, root
);
1867 /* run commit again to drop the original snapshot */
1868 trans
= btrfs_start_transaction(root
, 1);
1869 btrfs_commit_transaction(trans
, root
);
1870 ret
= btrfs_write_and_wait_transaction(NULL
, root
);
1873 write_ctree_super(NULL
, root
);
1875 if (fs_info
->delalloc_bytes
) {
1876 printk("btrfs: at unmount delalloc count %Lu\n",
1877 fs_info
->delalloc_bytes
);
1879 if (fs_info
->total_ref_cache_size
) {
1880 printk("btrfs: at umount reference cache size %Lu\n",
1881 fs_info
->total_ref_cache_size
);
1884 if (fs_info
->extent_root
->node
)
1885 free_extent_buffer(fs_info
->extent_root
->node
);
1887 if (fs_info
->tree_root
->node
)
1888 free_extent_buffer(fs_info
->tree_root
->node
);
1890 if (root
->fs_info
->chunk_root
->node
);
1891 free_extent_buffer(root
->fs_info
->chunk_root
->node
);
1893 if (root
->fs_info
->dev_root
->node
);
1894 free_extent_buffer(root
->fs_info
->dev_root
->node
);
1896 btrfs_free_block_groups(root
->fs_info
);
1897 fs_info
->closing
= 2;
1898 del_fs_roots(fs_info
);
1900 filemap_write_and_wait(fs_info
->btree_inode
->i_mapping
);
1902 truncate_inode_pages(fs_info
->btree_inode
->i_mapping
, 0);
1904 btrfs_stop_workers(&fs_info
->fixup_workers
);
1905 btrfs_stop_workers(&fs_info
->workers
);
1906 btrfs_stop_workers(&fs_info
->endio_workers
);
1907 btrfs_stop_workers(&fs_info
->endio_write_workers
);
1908 btrfs_stop_workers(&fs_info
->submit_workers
);
1910 iput(fs_info
->btree_inode
);
1912 while(!list_empty(&fs_info
->hashers
)) {
1913 struct btrfs_hasher
*hasher
;
1914 hasher
= list_entry(fs_info
->hashers
.next
, struct btrfs_hasher
,
1916 list_del(&hasher
->hashers
);
1917 crypto_free_hash(&fs_info
->hash_tfm
);
1921 btrfs_close_devices(fs_info
->fs_devices
);
1922 btrfs_mapping_tree_free(&fs_info
->mapping_tree
);
1924 bdi_destroy(&fs_info
->bdi
);
1926 kfree(fs_info
->extent_root
);
1927 kfree(fs_info
->tree_root
);
1928 kfree(fs_info
->chunk_root
);
1929 kfree(fs_info
->dev_root
);
1933 int btrfs_buffer_uptodate(struct extent_buffer
*buf
, u64 parent_transid
)
1936 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1938 ret
= extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1942 ret
= verify_parent_transid(&BTRFS_I(btree_inode
)->io_tree
, buf
,
1947 int btrfs_set_buffer_uptodate(struct extent_buffer
*buf
)
1949 struct inode
*btree_inode
= buf
->first_page
->mapping
->host
;
1950 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode
)->io_tree
,
1954 void btrfs_mark_buffer_dirty(struct extent_buffer
*buf
)
1956 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1957 u64 transid
= btrfs_header_generation(buf
);
1958 struct inode
*btree_inode
= root
->fs_info
->btree_inode
;
1960 WARN_ON(!btrfs_tree_locked(buf
));
1961 if (transid
!= root
->fs_info
->generation
) {
1962 printk(KERN_CRIT
"transid mismatch buffer %llu, found %Lu running %Lu\n",
1963 (unsigned long long)buf
->start
,
1964 transid
, root
->fs_info
->generation
);
1967 set_extent_buffer_dirty(&BTRFS_I(btree_inode
)->io_tree
, buf
);
1970 void btrfs_btree_balance_dirty(struct btrfs_root
*root
, unsigned long nr
)
1973 * looks as though older kernels can get into trouble with
1974 * this code, they end up stuck in balance_dirty_pages forever
1976 struct extent_io_tree
*tree
;
1979 unsigned long thresh
= 96 * 1024 * 1024;
1980 tree
= &BTRFS_I(root
->fs_info
->btree_inode
)->io_tree
;
1982 if (current_is_pdflush() || current
->flags
& PF_MEMALLOC
)
1985 num_dirty
= count_range_bits(tree
, &start
, (u64
)-1,
1986 thresh
, EXTENT_DIRTY
);
1987 if (num_dirty
> thresh
) {
1988 balance_dirty_pages_ratelimited_nr(
1989 root
->fs_info
->btree_inode
->i_mapping
, 1);
1994 int btrfs_read_buffer(struct extent_buffer
*buf
, u64 parent_transid
)
1996 struct btrfs_root
*root
= BTRFS_I(buf
->first_page
->mapping
->host
)->root
;
1998 ret
= btree_read_extent_buffer_pages(root
, buf
, 0, parent_transid
);
2000 buf
->flags
|= EXTENT_UPTODATE
;
2005 int btree_lock_page_hook(struct page
*page
)
2007 struct inode
*inode
= page
->mapping
->host
;
2008 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2009 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2010 struct extent_buffer
*eb
;
2012 u64 bytenr
= page_offset(page
);
2014 if (page
->private == EXTENT_PAGE_PRIVATE
)
2017 len
= page
->private >> 2;
2018 eb
= find_extent_buffer(io_tree
, bytenr
, len
, GFP_NOFS
);
2022 btrfs_tree_lock(eb
);
2023 spin_lock(&root
->fs_info
->hash_lock
);
2024 btrfs_set_header_flag(eb
, BTRFS_HEADER_FLAG_WRITTEN
);
2025 spin_unlock(&root
->fs_info
->hash_lock
);
2026 btrfs_tree_unlock(eb
);
2027 free_extent_buffer(eb
);
2033 static struct extent_io_ops btree_extent_io_ops
= {
2034 .write_cache_pages_lock_hook
= btree_lock_page_hook
,
2035 .readpage_end_io_hook
= btree_readpage_end_io_hook
,
2036 .submit_bio_hook
= btree_submit_bio_hook
,
2037 /* note we're sharing with inode.c for the merge bio hook */
2038 .merge_bio_hook
= btrfs_merge_bio_hook
,