aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Mahoney <jeffm@suse.com>2016-06-22 18:54:24 -0400
committerDavid Sterba <dsterba@suse.com>2016-12-06 10:06:59 -0500
commit2ff7e61e0d30ff166a2ae94575526bffe11fd1a8 (patch)
treea2b8aba1d10c010cc247f3e0866dcbd627e852f3
parentafdb571890615059ed4f0625209b379aff6cb08d (diff)
btrfs: take an fs_info directly when the root is not used otherwise
There are loads of functions in btrfs that accept a root parameter but only use it to obtain an fs_info pointer. Let's convert those to just accept an fs_info pointer directly. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--fs/btrfs/backref.c6
-rw-r--r--fs/btrfs/check-integrity.c10
-rw-r--r--fs/btrfs/check-integrity.h5
-rw-r--r--fs/btrfs/compression.c28
-rw-r--r--fs/btrfs/ctree.c231
-rw-r--r--fs/btrfs/ctree.h131
-rw-r--r--fs/btrfs/delayed-inode.c84
-rw-r--r--fs/btrfs/delayed-inode.h15
-rw-r--r--fs/btrfs/dev-replace.c12
-rw-r--r--fs/btrfs/dev-replace.h4
-rw-r--r--fs/btrfs/dir-item.c31
-rw-r--r--fs/btrfs/disk-io.c186
-rw-r--r--fs/btrfs/disk-io.h23
-rw-r--r--fs/btrfs/extent-tree.c541
-rw-r--r--fs/btrfs/extent_io.c8
-rw-r--r--fs/btrfs/extent_io.h4
-rw-r--r--fs/btrfs/file-item.c32
-rw-r--r--fs/btrfs/file.c47
-rw-r--r--fs/btrfs/free-space-cache.c24
-rw-r--r--fs/btrfs/free-space-cache.h4
-rw-r--r--fs/btrfs/inode-item.c8
-rw-r--r--fs/btrfs/inode-map.c3
-rw-r--r--fs/btrfs/inode.c186
-rw-r--r--fs/btrfs/ioctl.c108
-rw-r--r--fs/btrfs/print-tree.c15
-rw-r--r--fs/btrfs/print-tree.h4
-rw-r--r--fs/btrfs/props.c5
-rw-r--r--fs/btrfs/qgroup.c14
-rw-r--r--fs/btrfs/qgroup.h2
-rw-r--r--fs/btrfs/raid56.c26
-rw-r--r--fs/btrfs/raid56.h8
-rw-r--r--fs/btrfs/reada.c8
-rw-r--r--fs/btrfs/relocation.c78
-rw-r--r--fs/btrfs/root-tree.c2
-rw-r--r--fs/btrfs/scrub.c21
-rw-r--r--fs/btrfs/send.c5
-rw-r--r--fs/btrfs/super.c5
-rw-r--r--fs/btrfs/transaction.c172
-rw-r--r--fs/btrfs/transaction.h6
-rw-r--r--fs/btrfs/tree-log.c58
-rw-r--r--fs/btrfs/uuid-tree.c4
-rw-r--r--fs/btrfs/volumes.c159
-rw-r--r--fs/btrfs/volumes.h22
-rw-r--r--fs/btrfs/xattr.c17
44 files changed, 1119 insertions, 1243 deletions
diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 45ef41f247b1..4577c028333a 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -788,8 +788,7 @@ static int __add_missing_keys(struct btrfs_fs_info *fs_info,
788 if (ref->key_for_search.type) 788 if (ref->key_for_search.type)
789 continue; 789 continue;
790 BUG_ON(!ref->wanted_disk_byte); 790 BUG_ON(!ref->wanted_disk_byte);
791 eb = read_tree_block(fs_info->tree_root, ref->wanted_disk_byte, 791 eb = read_tree_block(fs_info, ref->wanted_disk_byte, 0);
792 0);
793 if (IS_ERR(eb)) { 792 if (IS_ERR(eb)) {
794 return PTR_ERR(eb); 793 return PTR_ERR(eb);
795 } else if (!extent_buffer_uptodate(eb)) { 794 } else if (!extent_buffer_uptodate(eb)) {
@@ -1405,8 +1404,7 @@ again:
1405 ref->level == 0) { 1404 ref->level == 0) {
1406 struct extent_buffer *eb; 1405 struct extent_buffer *eb;
1407 1406
1408 eb = read_tree_block(fs_info->extent_root, 1407 eb = read_tree_block(fs_info, ref->parent, 0);
1409 ref->parent, 0);
1410 if (IS_ERR(eb)) { 1408 if (IS_ERR(eb)) {
1411 ret = PTR_ERR(eb); 1409 ret = PTR_ERR(eb);
1412 goto out; 1410 goto out;
diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c
index 137883cce13c..ab14c2e635ca 100644
--- a/fs/btrfs/check-integrity.c
+++ b/fs/btrfs/check-integrity.c
@@ -2904,14 +2904,13 @@ int btrfsic_submit_bio_wait(struct bio *bio)
2904 return submit_bio_wait(bio); 2904 return submit_bio_wait(bio);
2905} 2905}
2906 2906
2907int btrfsic_mount(struct btrfs_root *root, 2907int btrfsic_mount(struct btrfs_fs_info *fs_info,
2908 struct btrfs_fs_devices *fs_devices, 2908 struct btrfs_fs_devices *fs_devices,
2909 int including_extent_data, u32 print_mask) 2909 int including_extent_data, u32 print_mask)
2910{ 2910{
2911 int ret; 2911 int ret;
2912 struct btrfsic_state *state; 2912 struct btrfsic_state *state;
2913 struct list_head *dev_head = &fs_devices->devices; 2913 struct list_head *dev_head = &fs_devices->devices;
2914 struct btrfs_fs_info *fs_info = root->fs_info;
2915 struct btrfs_device *device; 2914 struct btrfs_device *device;
2916 2915
2917 if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) { 2916 if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
@@ -2939,7 +2938,7 @@ int btrfsic_mount(struct btrfs_root *root,
2939 btrfsic_is_initialized = 1; 2938 btrfsic_is_initialized = 1;
2940 } 2939 }
2941 mutex_lock(&btrfsic_mutex); 2940 mutex_lock(&btrfsic_mutex);
2942 state->fs_info = root->fs_info; 2941 state->fs_info = fs_info;
2943 state->print_mask = print_mask; 2942 state->print_mask = print_mask;
2944 state->include_extent_data = including_extent_data; 2943 state->include_extent_data = including_extent_data;
2945 state->csum_size = 0; 2944 state->csum_size = 0;
@@ -2977,7 +2976,7 @@ int btrfsic_mount(struct btrfs_root *root,
2977 ret = btrfsic_process_superblock(state, fs_devices); 2976 ret = btrfsic_process_superblock(state, fs_devices);
2978 if (0 != ret) { 2977 if (0 != ret) {
2979 mutex_unlock(&btrfsic_mutex); 2978 mutex_unlock(&btrfsic_mutex);
2980 btrfsic_unmount(root, fs_devices); 2979 btrfsic_unmount(fs_devices);
2981 return ret; 2980 return ret;
2982 } 2981 }
2983 2982
@@ -2990,8 +2989,7 @@ int btrfsic_mount(struct btrfs_root *root,
2990 return 0; 2989 return 0;
2991} 2990}
2992 2991
2993void btrfsic_unmount(struct btrfs_root *root, 2992void btrfsic_unmount(struct btrfs_fs_devices *fs_devices)
2994 struct btrfs_fs_devices *fs_devices)
2995{ 2993{
2996 struct btrfsic_block *b_all, *tmp_all; 2994 struct btrfsic_block *b_all, *tmp_all;
2997 struct btrfsic_state *state; 2995 struct btrfsic_state *state;
diff --git a/fs/btrfs/check-integrity.h b/fs/btrfs/check-integrity.h
index f78dff1c7e86..2de58a99ee92 100644
--- a/fs/btrfs/check-integrity.h
+++ b/fs/btrfs/check-integrity.h
@@ -29,10 +29,9 @@ int btrfsic_submit_bio_wait(struct bio *bio);
29#define btrfsic_submit_bio_wait submit_bio_wait 29#define btrfsic_submit_bio_wait submit_bio_wait
30#endif 30#endif
31 31
32int btrfsic_mount(struct btrfs_root *root, 32int btrfsic_mount(struct btrfs_fs_info *fs_info,
33 struct btrfs_fs_devices *fs_devices, 33 struct btrfs_fs_devices *fs_devices,
34 int including_extent_data, u32 print_mask); 34 int including_extent_data, u32 print_mask);
35void btrfsic_unmount(struct btrfs_root *root, 35void btrfsic_unmount(struct btrfs_fs_devices *fs_devices);
36 struct btrfs_fs_devices *fs_devices);
37 36
38#endif 37#endif
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index 750bae4a46da..7f390849343b 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -85,10 +85,9 @@ static int btrfs_decompress_bio(int type, struct page **pages_in,
85 u64 disk_start, struct bio *orig_bio, 85 u64 disk_start, struct bio *orig_bio,
86 size_t srclen); 86 size_t srclen);
87 87
88static inline int compressed_bio_size(struct btrfs_root *root, 88static inline int compressed_bio_size(struct btrfs_fs_info *fs_info,
89 unsigned long disk_size) 89 unsigned long disk_size)
90{ 90{
91 struct btrfs_fs_info *fs_info = root->fs_info;
92 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 91 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
93 92
94 return sizeof(struct compressed_bio) + 93 return sizeof(struct compressed_bio) +
@@ -331,7 +330,6 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
331{ 330{
332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 331 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
333 struct bio *bio = NULL; 332 struct bio *bio = NULL;
334 struct btrfs_root *root = BTRFS_I(inode)->root;
335 struct compressed_bio *cb; 333 struct compressed_bio *cb;
336 unsigned long bytes_left; 334 unsigned long bytes_left;
337 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 335 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
@@ -343,7 +341,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
343 int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM; 341 int skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
344 342
345 WARN_ON(start & ((u64)PAGE_SIZE - 1)); 343 WARN_ON(start & ((u64)PAGE_SIZE - 1));
346 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS); 344 cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
347 if (!cb) 345 if (!cb)
348 return -ENOMEM; 346 return -ENOMEM;
349 atomic_set(&cb->pending_bios, 0); 347 atomic_set(&cb->pending_bios, 0);
@@ -398,12 +396,11 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
398 BUG_ON(ret); /* -ENOMEM */ 396 BUG_ON(ret); /* -ENOMEM */
399 397
400 if (!skip_sum) { 398 if (!skip_sum) {
401 ret = btrfs_csum_one_bio(root, inode, bio, 399 ret = btrfs_csum_one_bio(inode, bio, start, 1);
402 start, 1);
403 BUG_ON(ret); /* -ENOMEM */ 400 BUG_ON(ret); /* -ENOMEM */
404 } 401 }
405 402
406 ret = btrfs_map_bio(root, bio, 0, 1); 403 ret = btrfs_map_bio(fs_info, bio, 0, 1);
407 if (ret) { 404 if (ret) {
408 bio->bi_error = ret; 405 bio->bi_error = ret;
409 bio_endio(bio); 406 bio_endio(bio);
@@ -433,11 +430,11 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
433 BUG_ON(ret); /* -ENOMEM */ 430 BUG_ON(ret); /* -ENOMEM */
434 431
435 if (!skip_sum) { 432 if (!skip_sum) {
436 ret = btrfs_csum_one_bio(root, inode, bio, start, 1); 433 ret = btrfs_csum_one_bio(inode, bio, start, 1);
437 BUG_ON(ret); /* -ENOMEM */ 434 BUG_ON(ret); /* -ENOMEM */
438 } 435 }
439 436
440 ret = btrfs_map_bio(root, bio, 0, 1); 437 ret = btrfs_map_bio(fs_info, bio, 0, 1);
441 if (ret) { 438 if (ret) {
442 bio->bi_error = ret; 439 bio->bi_error = ret;
443 bio_endio(bio); 440 bio_endio(bio);
@@ -581,7 +578,6 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
581 struct extent_io_tree *tree; 578 struct extent_io_tree *tree;
582 struct extent_map_tree *em_tree; 579 struct extent_map_tree *em_tree;
583 struct compressed_bio *cb; 580 struct compressed_bio *cb;
584 struct btrfs_root *root = BTRFS_I(inode)->root;
585 unsigned long compressed_len; 581 unsigned long compressed_len;
586 unsigned long nr_pages; 582 unsigned long nr_pages;
587 unsigned long pg_index; 583 unsigned long pg_index;
@@ -609,7 +605,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
609 return -EIO; 605 return -EIO;
610 606
611 compressed_len = em->block_len; 607 compressed_len = em->block_len;
612 cb = kmalloc(compressed_bio_size(root, compressed_len), GFP_NOFS); 608 cb = kmalloc(compressed_bio_size(fs_info, compressed_len), GFP_NOFS);
613 if (!cb) 609 if (!cb)
614 goto out; 610 goto out;
615 611
@@ -694,14 +690,14 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
694 atomic_inc(&cb->pending_bios); 690 atomic_inc(&cb->pending_bios);
695 691
696 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 692 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
697 ret = btrfs_lookup_bio_sums(root, inode, 693 ret = btrfs_lookup_bio_sums(inode, comp_bio,
698 comp_bio, sums); 694 sums);
699 BUG_ON(ret); /* -ENOMEM */ 695 BUG_ON(ret); /* -ENOMEM */
700 } 696 }
701 sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size, 697 sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
702 fs_info->sectorsize); 698 fs_info->sectorsize);
703 699
704 ret = btrfs_map_bio(root, comp_bio, mirror_num, 0); 700 ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0);
705 if (ret) { 701 if (ret) {
706 comp_bio->bi_error = ret; 702 comp_bio->bi_error = ret;
707 bio_endio(comp_bio); 703 bio_endio(comp_bio);
@@ -726,11 +722,11 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
726 BUG_ON(ret); /* -ENOMEM */ 722 BUG_ON(ret); /* -ENOMEM */
727 723
728 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 724 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
729 ret = btrfs_lookup_bio_sums(root, inode, comp_bio, sums); 725 ret = btrfs_lookup_bio_sums(inode, comp_bio, sums);
730 BUG_ON(ret); /* -ENOMEM */ 726 BUG_ON(ret); /* -ENOMEM */
731 } 727 }
732 728
733 ret = btrfs_map_bio(root, comp_bio, mirror_num, 0); 729 ret = btrfs_map_bio(fs_info, comp_bio, mirror_num, 0);
734 if (ret) { 730 if (ret) {
735 comp_bio->bi_error = ret; 731 comp_bio->bi_error = ret;
736 bio_endio(comp_bio); 732 bio_endio(comp_bio);
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index b29c8d82e741..a426dc822d4d 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -32,10 +32,11 @@ static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
32 *root, struct btrfs_key *ins_key, 32 *root, struct btrfs_key *ins_key,
33 struct btrfs_path *path, int data_size, int extend); 33 struct btrfs_path *path, int data_size, int extend);
34static int push_node_left(struct btrfs_trans_handle *trans, 34static int push_node_left(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root, struct extent_buffer *dst, 35 struct btrfs_fs_info *fs_info,
36 struct extent_buffer *dst,
36 struct extent_buffer *src, int empty); 37 struct extent_buffer *src, int empty);
37static int balance_node_right(struct btrfs_trans_handle *trans, 38static int balance_node_right(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root, 39 struct btrfs_fs_info *fs_info,
39 struct extent_buffer *dst_buf, 40 struct extent_buffer *dst_buf,
40 struct extent_buffer *src_buf); 41 struct extent_buffer *src_buf);
41static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 42static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
@@ -1005,7 +1006,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
1005 */ 1006 */
1006 1007
1007 if (btrfs_block_can_be_shared(root, buf)) { 1008 if (btrfs_block_can_be_shared(root, buf)) {
1008 ret = btrfs_lookup_extent_info(trans, root, buf->start, 1009 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
1009 btrfs_header_level(buf), 1, 1010 btrfs_header_level(buf), 1,
1010 &refs, &flags); 1011 &refs, &flags);
1011 if (ret) 1012 if (ret)
@@ -1055,7 +1056,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
1055 if (new_flags != 0) { 1056 if (new_flags != 0) {
1056 int level = btrfs_header_level(buf); 1057 int level = btrfs_header_level(buf);
1057 1058
1058 ret = btrfs_set_disk_extent_flags(trans, root, 1059 ret = btrfs_set_disk_extent_flags(trans, fs_info,
1059 buf->start, 1060 buf->start,
1060 buf->len, 1061 buf->len,
1061 new_flags, level, 0); 1062 new_flags, level, 0);
@@ -1431,7 +1432,7 @@ get_old_root(struct btrfs_root *root, u64 time_seq)
1431 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 1432 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1432 btrfs_tree_read_unlock(eb_root); 1433 btrfs_tree_read_unlock(eb_root);
1433 free_extent_buffer(eb_root); 1434 free_extent_buffer(eb_root);
1434 old = read_tree_block(root, logical, 0); 1435 old = read_tree_block(fs_info, logical, 0);
1435 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { 1436 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1436 if (!IS_ERR(old)) 1437 if (!IS_ERR(old))
1437 free_extent_buffer(old); 1438 free_extent_buffer(old);
@@ -1682,7 +1683,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1682 uptodate = 0; 1683 uptodate = 0;
1683 if (!cur || !uptodate) { 1684 if (!cur || !uptodate) {
1684 if (!cur) { 1685 if (!cur) {
1685 cur = read_tree_block(root, blocknr, gen); 1686 cur = read_tree_block(fs_info, blocknr, gen);
1686 if (IS_ERR(cur)) { 1687 if (IS_ERR(cur)) {
1687 return PTR_ERR(cur); 1688 return PTR_ERR(cur);
1688 } else if (!extent_buffer_uptodate(cur)) { 1689 } else if (!extent_buffer_uptodate(cur)) {
@@ -1843,8 +1844,9 @@ static void root_sub_used(struct btrfs_root *root, u32 size)
1843/* given a node and slot number, this reads the blocks it points to. The 1844/* given a node and slot number, this reads the blocks it points to. The
1844 * extent buffer is returned with a reference taken (but unlocked). 1845 * extent buffer is returned with a reference taken (but unlocked).
1845 */ 1846 */
1846static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root, 1847static noinline struct extent_buffer *
1847 struct extent_buffer *parent, int slot) 1848read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1849 int slot)
1848{ 1850{
1849 int level = btrfs_header_level(parent); 1851 int level = btrfs_header_level(parent);
1850 struct extent_buffer *eb; 1852 struct extent_buffer *eb;
@@ -1854,7 +1856,7 @@ static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
1854 1856
1855 BUG_ON(level == 0); 1857 BUG_ON(level == 0);
1856 1858
1857 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot), 1859 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
1858 btrfs_node_ptr_generation(parent, slot)); 1860 btrfs_node_ptr_generation(parent, slot));
1859 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { 1861 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1860 free_extent_buffer(eb); 1862 free_extent_buffer(eb);
@@ -1911,7 +1913,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1911 return 0; 1913 return 0;
1912 1914
1913 /* promote the child to a root */ 1915 /* promote the child to a root */
1914 child = read_node_slot(root, mid, 0); 1916 child = read_node_slot(fs_info, mid, 0);
1915 if (IS_ERR(child)) { 1917 if (IS_ERR(child)) {
1916 ret = PTR_ERR(child); 1918 ret = PTR_ERR(child);
1917 btrfs_handle_fs_error(fs_info, ret, NULL); 1919 btrfs_handle_fs_error(fs_info, ret, NULL);
@@ -1950,7 +1952,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1950 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) 1952 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1951 return 0; 1953 return 0;
1952 1954
1953 left = read_node_slot(root, parent, pslot - 1); 1955 left = read_node_slot(fs_info, parent, pslot - 1);
1954 if (IS_ERR(left)) 1956 if (IS_ERR(left))
1955 left = NULL; 1957 left = NULL;
1956 1958
@@ -1965,7 +1967,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1965 } 1967 }
1966 } 1968 }
1967 1969
1968 right = read_node_slot(root, parent, pslot + 1); 1970 right = read_node_slot(fs_info, parent, pslot + 1);
1969 if (IS_ERR(right)) 1971 if (IS_ERR(right))
1970 right = NULL; 1972 right = NULL;
1971 1973
@@ -1983,7 +1985,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1983 /* first, try to make some room in the middle buffer */ 1985 /* first, try to make some room in the middle buffer */
1984 if (left) { 1986 if (left) {
1985 orig_slot += btrfs_header_nritems(left); 1987 orig_slot += btrfs_header_nritems(left);
1986 wret = push_node_left(trans, root, left, mid, 1); 1988 wret = push_node_left(trans, fs_info, left, mid, 1);
1987 if (wret < 0) 1989 if (wret < 0)
1988 ret = wret; 1990 ret = wret;
1989 } 1991 }
@@ -1992,7 +1994,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1992 * then try to empty the right most buffer into the middle 1994 * then try to empty the right most buffer into the middle
1993 */ 1995 */
1994 if (right) { 1996 if (right) {
1995 wret = push_node_left(trans, root, mid, right, 1); 1997 wret = push_node_left(trans, fs_info, mid, right, 1);
1996 if (wret < 0 && wret != -ENOSPC) 1998 if (wret < 0 && wret != -ENOSPC)
1997 ret = wret; 1999 ret = wret;
1998 if (btrfs_header_nritems(right) == 0) { 2000 if (btrfs_header_nritems(right) == 0) {
@@ -2027,13 +2029,13 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
2027 btrfs_handle_fs_error(fs_info, ret, NULL); 2029 btrfs_handle_fs_error(fs_info, ret, NULL);
2028 goto enospc; 2030 goto enospc;
2029 } 2031 }
2030 wret = balance_node_right(trans, root, mid, left); 2032 wret = balance_node_right(trans, fs_info, mid, left);
2031 if (wret < 0) { 2033 if (wret < 0) {
2032 ret = wret; 2034 ret = wret;
2033 goto enospc; 2035 goto enospc;
2034 } 2036 }
2035 if (wret == 1) { 2037 if (wret == 1) {
2036 wret = push_node_left(trans, root, left, mid, 1); 2038 wret = push_node_left(trans, fs_info, left, mid, 1);
2037 if (wret < 0) 2039 if (wret < 0)
2038 ret = wret; 2040 ret = wret;
2039 } 2041 }
@@ -2122,7 +2124,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2122 if (!parent) 2124 if (!parent)
2123 return 1; 2125 return 1;
2124 2126
2125 left = read_node_slot(root, parent, pslot - 1); 2127 left = read_node_slot(fs_info, parent, pslot - 1);
2126 if (IS_ERR(left)) 2128 if (IS_ERR(left))
2127 left = NULL; 2129 left = NULL;
2128 2130
@@ -2142,7 +2144,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2142 if (ret) 2144 if (ret)
2143 wret = 1; 2145 wret = 1;
2144 else { 2146 else {
2145 wret = push_node_left(trans, root, 2147 wret = push_node_left(trans, fs_info,
2146 left, mid, 0); 2148 left, mid, 0);
2147 } 2149 }
2148 } 2150 }
@@ -2173,7 +2175,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2173 btrfs_tree_unlock(left); 2175 btrfs_tree_unlock(left);
2174 free_extent_buffer(left); 2176 free_extent_buffer(left);
2175 } 2177 }
2176 right = read_node_slot(root, parent, pslot + 1); 2178 right = read_node_slot(fs_info, parent, pslot + 1);
2177 if (IS_ERR(right)) 2179 if (IS_ERR(right))
2178 right = NULL; 2180 right = NULL;
2179 2181
@@ -2196,7 +2198,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2196 if (ret) 2198 if (ret)
2197 wret = 1; 2199 wret = 1;
2198 else { 2200 else {
2199 wret = balance_node_right(trans, root, 2201 wret = balance_node_right(trans, fs_info,
2200 right, mid); 2202 right, mid);
2201 } 2203 }
2202 } 2204 }
@@ -2234,11 +2236,10 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2234 * readahead one full node of leaves, finding things that are close 2236 * readahead one full node of leaves, finding things that are close
2235 * to the block in 'slot', and triggering ra on them. 2237 * to the block in 'slot', and triggering ra on them.
2236 */ 2238 */
2237static void reada_for_search(struct btrfs_root *root, 2239static void reada_for_search(struct btrfs_fs_info *fs_info,
2238 struct btrfs_path *path, 2240 struct btrfs_path *path,
2239 int level, int slot, u64 objectid) 2241 int level, int slot, u64 objectid)
2240{ 2242{
2241 struct btrfs_fs_info *fs_info = root->fs_info;
2242 struct extent_buffer *node; 2243 struct extent_buffer *node;
2243 struct btrfs_disk_key disk_key; 2244 struct btrfs_disk_key disk_key;
2244 u32 nritems; 2245 u32 nritems;
@@ -2289,7 +2290,7 @@ static void reada_for_search(struct btrfs_root *root,
2289 search = btrfs_node_blockptr(node, nr); 2290 search = btrfs_node_blockptr(node, nr);
2290 if ((search <= target && target - search <= 65536) || 2291 if ((search <= target && target - search <= 65536) ||
2291 (search > target && search - target <= 65536)) { 2292 (search > target && search - target <= 65536)) {
2292 readahead_tree_block(root, search); 2293 readahead_tree_block(fs_info, search);
2293 nread += blocksize; 2294 nread += blocksize;
2294 } 2295 }
2295 nscan++; 2296 nscan++;
@@ -2298,10 +2299,9 @@ static void reada_for_search(struct btrfs_root *root,
2298 } 2299 }
2299} 2300}
2300 2301
2301static noinline void reada_for_balance(struct btrfs_root *root, 2302static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
2302 struct btrfs_path *path, int level) 2303 struct btrfs_path *path, int level)
2303{ 2304{
2304 struct btrfs_fs_info *fs_info = root->fs_info;
2305 int slot; 2305 int slot;
2306 int nritems; 2306 int nritems;
2307 struct extent_buffer *parent; 2307 struct extent_buffer *parent;
@@ -2340,9 +2340,9 @@ static noinline void reada_for_balance(struct btrfs_root *root,
2340 } 2340 }
2341 2341
2342 if (block1) 2342 if (block1)
2343 readahead_tree_block(root, block1); 2343 readahead_tree_block(fs_info, block1);
2344 if (block2) 2344 if (block2)
2345 readahead_tree_block(root, block2); 2345 readahead_tree_block(fs_info, block2);
2346} 2346}
2347 2347
2348 2348
@@ -2491,12 +2491,12 @@ read_block_for_search(struct btrfs_trans_handle *trans,
2491 2491
2492 free_extent_buffer(tmp); 2492 free_extent_buffer(tmp);
2493 if (p->reada != READA_NONE) 2493 if (p->reada != READA_NONE)
2494 reada_for_search(root, p, level, slot, key->objectid); 2494 reada_for_search(fs_info, p, level, slot, key->objectid);
2495 2495
2496 btrfs_release_path(p); 2496 btrfs_release_path(p);
2497 2497
2498 ret = -EAGAIN; 2498 ret = -EAGAIN;
2499 tmp = read_tree_block(root, blocknr, 0); 2499 tmp = read_tree_block(fs_info, blocknr, 0);
2500 if (!IS_ERR(tmp)) { 2500 if (!IS_ERR(tmp)) {
2501 /* 2501 /*
2502 * If the read above didn't mark this buffer up to date, 2502 * If the read above didn't mark this buffer up to date,
@@ -2542,7 +2542,7 @@ setup_nodes_for_search(struct btrfs_trans_handle *trans,
2542 } 2542 }
2543 2543
2544 btrfs_set_path_blocking(p); 2544 btrfs_set_path_blocking(p);
2545 reada_for_balance(root, p, level); 2545 reada_for_balance(fs_info, p, level);
2546 sret = split_node(trans, root, p, level); 2546 sret = split_node(trans, root, p, level);
2547 btrfs_clear_path_blocking(p, NULL, 0); 2547 btrfs_clear_path_blocking(p, NULL, 0);
2548 2548
@@ -2563,7 +2563,7 @@ setup_nodes_for_search(struct btrfs_trans_handle *trans,
2563 } 2563 }
2564 2564
2565 btrfs_set_path_blocking(p); 2565 btrfs_set_path_blocking(p);
2566 reada_for_balance(root, p, level); 2566 reada_for_balance(fs_info, p, level);
2567 sret = balance_level(trans, root, p, level); 2567 sret = balance_level(trans, root, p, level);
2568 btrfs_clear_path_blocking(p, NULL, 0); 2568 btrfs_clear_path_blocking(p, NULL, 0);
2569 2569
@@ -2905,7 +2905,7 @@ cow_done:
2905 } else { 2905 } else {
2906 p->slots[level] = slot; 2906 p->slots[level] = slot;
2907 if (ins_len > 0 && 2907 if (ins_len > 0 &&
2908 btrfs_leaf_free_space(root, b) < ins_len) { 2908 btrfs_leaf_free_space(fs_info, b) < ins_len) {
2909 if (write_lock_level < 1) { 2909 if (write_lock_level < 1) {
2910 write_lock_level = 1; 2910 write_lock_level = 1;
2911 btrfs_release_path(p); 2911 btrfs_release_path(p);
@@ -3198,10 +3198,10 @@ void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3198 * error, and > 0 if there was no room in the left hand block. 3198 * error, and > 0 if there was no room in the left hand block.
3199 */ 3199 */
3200static int push_node_left(struct btrfs_trans_handle *trans, 3200static int push_node_left(struct btrfs_trans_handle *trans,
3201 struct btrfs_root *root, struct extent_buffer *dst, 3201 struct btrfs_fs_info *fs_info,
3202 struct extent_buffer *dst,
3202 struct extent_buffer *src, int empty) 3203 struct extent_buffer *src, int empty)
3203{ 3204{
3204 struct btrfs_fs_info *fs_info = root->fs_info;
3205 int push_items = 0; 3205 int push_items = 0;
3206 int src_nritems; 3206 int src_nritems;
3207 int dst_nritems; 3207 int dst_nritems;
@@ -3273,11 +3273,10 @@ static int push_node_left(struct btrfs_trans_handle *trans,
3273 * this will only push up to 1/2 the contents of the left node over 3273 * this will only push up to 1/2 the contents of the left node over
3274 */ 3274 */
3275static int balance_node_right(struct btrfs_trans_handle *trans, 3275static int balance_node_right(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root, 3276 struct btrfs_fs_info *fs_info,
3277 struct extent_buffer *dst, 3277 struct extent_buffer *dst,
3278 struct extent_buffer *src) 3278 struct extent_buffer *src)
3279{ 3279{
3280 struct btrfs_fs_info *fs_info = root->fs_info;
3281 int push_items = 0; 3280 int push_items = 0;
3282 int max_push; 3281 int max_push;
3283 int src_nritems; 3282 int src_nritems;
@@ -3407,11 +3406,10 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3407 * blocknr is the block the key points to. 3406 * blocknr is the block the key points to.
3408 */ 3407 */
3409static void insert_ptr(struct btrfs_trans_handle *trans, 3408static void insert_ptr(struct btrfs_trans_handle *trans,
3410 struct btrfs_root *root, struct btrfs_path *path, 3409 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
3411 struct btrfs_disk_key *key, u64 bytenr, 3410 struct btrfs_disk_key *key, u64 bytenr,
3412 int slot, int level) 3411 int slot, int level)
3413{ 3412{
3414 struct btrfs_fs_info *fs_info = root->fs_info;
3415 struct extent_buffer *lower; 3413 struct extent_buffer *lower;
3416 int nritems; 3414 int nritems;
3417 int ret; 3415 int ret;
@@ -3527,7 +3525,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3527 btrfs_mark_buffer_dirty(c); 3525 btrfs_mark_buffer_dirty(c);
3528 btrfs_mark_buffer_dirty(split); 3526 btrfs_mark_buffer_dirty(split);
3529 3527
3530 insert_ptr(trans, root, path, &disk_key, split->start, 3528 insert_ptr(trans, fs_info, path, &disk_key, split->start,
3531 path->slots[level + 1] + 1, level + 1); 3529 path->slots[level + 1] + 1, level + 1);
3532 3530
3533 if (path->slots[level] >= mid) { 3531 if (path->slots[level] >= mid) {
@@ -3575,10 +3573,9 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3575 * the start of the leaf data. IOW, how much room 3573 * the start of the leaf data. IOW, how much room
3576 * the leaf has left for both items and data 3574 * the leaf has left for both items and data
3577 */ 3575 */
3578noinline int btrfs_leaf_free_space(struct btrfs_root *root, 3576noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
3579 struct extent_buffer *leaf) 3577 struct extent_buffer *leaf)
3580{ 3578{
3581 struct btrfs_fs_info *fs_info = root->fs_info;
3582 int nritems = btrfs_header_nritems(leaf); 3579 int nritems = btrfs_header_nritems(leaf);
3583 int ret; 3580 int ret;
3584 3581
@@ -3598,14 +3595,13 @@ noinline int btrfs_leaf_free_space(struct btrfs_root *root,
3598 * right. We'll push up to and including min_slot, but no lower 3595 * right. We'll push up to and including min_slot, but no lower
3599 */ 3596 */
3600static noinline int __push_leaf_right(struct btrfs_trans_handle *trans, 3597static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3601 struct btrfs_root *root, 3598 struct btrfs_fs_info *fs_info,
3602 struct btrfs_path *path, 3599 struct btrfs_path *path,
3603 int data_size, int empty, 3600 int data_size, int empty,
3604 struct extent_buffer *right, 3601 struct extent_buffer *right,
3605 int free_space, u32 left_nritems, 3602 int free_space, u32 left_nritems,
3606 u32 min_slot) 3603 u32 min_slot)
3607{ 3604{
3608 struct btrfs_fs_info *fs_info = root->fs_info;
3609 struct extent_buffer *left = path->nodes[0]; 3605 struct extent_buffer *left = path->nodes[0];
3610 struct extent_buffer *upper = path->nodes[1]; 3606 struct extent_buffer *upper = path->nodes[1];
3611 struct btrfs_map_token token; 3607 struct btrfs_map_token token;
@@ -3639,7 +3635,7 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3639 if (path->slots[0] > i) 3635 if (path->slots[0] > i)
3640 break; 3636 break;
3641 if (path->slots[0] == i) { 3637 if (path->slots[0] == i) {
3642 int space = btrfs_leaf_free_space(root, left); 3638 int space = btrfs_leaf_free_space(fs_info, left);
3643 if (space + push_space * 2 > free_space) 3639 if (space + push_space * 2 > free_space)
3644 break; 3640 break;
3645 } 3641 }
@@ -3668,10 +3664,10 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3668 right_nritems = btrfs_header_nritems(right); 3664 right_nritems = btrfs_header_nritems(right);
3669 3665
3670 push_space = btrfs_item_end_nr(left, left_nritems - push_items); 3666 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
3671 push_space -= leaf_data_end(root, left); 3667 push_space -= leaf_data_end(fs_info, left);
3672 3668
3673 /* make room in the right data area */ 3669 /* make room in the right data area */
3674 data_end = leaf_data_end(root, right); 3670 data_end = leaf_data_end(fs_info, right);
3675 memmove_extent_buffer(right, 3671 memmove_extent_buffer(right,
3676 btrfs_leaf_data(right) + data_end - push_space, 3672 btrfs_leaf_data(right) + data_end - push_space,
3677 btrfs_leaf_data(right) + data_end, 3673 btrfs_leaf_data(right) + data_end,
@@ -3680,7 +3676,7 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3680 /* copy from the left data area */ 3676 /* copy from the left data area */
3681 copy_extent_buffer(right, left, btrfs_leaf_data(right) + 3677 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
3682 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 3678 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3683 btrfs_leaf_data(left) + leaf_data_end(root, left), 3679 btrfs_leaf_data(left) + leaf_data_end(fs_info, left),
3684 push_space); 3680 push_space);
3685 3681
3686 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), 3682 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
@@ -3752,6 +3748,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3752 int min_data_size, int data_size, 3748 int min_data_size, int data_size,
3753 int empty, u32 min_slot) 3749 int empty, u32 min_slot)
3754{ 3750{
3751 struct btrfs_fs_info *fs_info = root->fs_info;
3755 struct extent_buffer *left = path->nodes[0]; 3752 struct extent_buffer *left = path->nodes[0];
3756 struct extent_buffer *right; 3753 struct extent_buffer *right;
3757 struct extent_buffer *upper; 3754 struct extent_buffer *upper;
@@ -3770,7 +3767,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3770 3767
3771 btrfs_assert_tree_locked(path->nodes[1]); 3768 btrfs_assert_tree_locked(path->nodes[1]);
3772 3769
3773 right = read_node_slot(root, upper, slot + 1); 3770 right = read_node_slot(fs_info, upper, slot + 1);
3774 /* 3771 /*
3775 * slot + 1 is not valid or we fail to read the right node, 3772 * slot + 1 is not valid or we fail to read the right node,
3776 * no big deal, just return. 3773 * no big deal, just return.
@@ -3781,7 +3778,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3781 btrfs_tree_lock(right); 3778 btrfs_tree_lock(right);
3782 btrfs_set_lock_blocking(right); 3779 btrfs_set_lock_blocking(right);
3783 3780
3784 free_space = btrfs_leaf_free_space(root, right); 3781 free_space = btrfs_leaf_free_space(fs_info, right);
3785 if (free_space < data_size) 3782 if (free_space < data_size)
3786 goto out_unlock; 3783 goto out_unlock;
3787 3784
@@ -3791,7 +3788,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3791 if (ret) 3788 if (ret)
3792 goto out_unlock; 3789 goto out_unlock;
3793 3790
3794 free_space = btrfs_leaf_free_space(root, right); 3791 free_space = btrfs_leaf_free_space(fs_info, right);
3795 if (free_space < data_size) 3792 if (free_space < data_size)
3796 goto out_unlock; 3793 goto out_unlock;
3797 3794
@@ -3812,7 +3809,7 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
3812 return 0; 3809 return 0;
3813 } 3810 }
3814 3811
3815 return __push_leaf_right(trans, root, path, min_data_size, empty, 3812 return __push_leaf_right(trans, fs_info, path, min_data_size, empty,
3816 right, free_space, left_nritems, min_slot); 3813 right, free_space, left_nritems, min_slot);
3817out_unlock: 3814out_unlock:
3818 btrfs_tree_unlock(right); 3815 btrfs_tree_unlock(right);
@@ -3829,13 +3826,12 @@ out_unlock:
3829 * items 3826 * items
3830 */ 3827 */
3831static noinline int __push_leaf_left(struct btrfs_trans_handle *trans, 3828static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3832 struct btrfs_root *root, 3829 struct btrfs_fs_info *fs_info,
3833 struct btrfs_path *path, int data_size, 3830 struct btrfs_path *path, int data_size,
3834 int empty, struct extent_buffer *left, 3831 int empty, struct extent_buffer *left,
3835 int free_space, u32 right_nritems, 3832 int free_space, u32 right_nritems,
3836 u32 max_slot) 3833 u32 max_slot)
3837{ 3834{
3838 struct btrfs_fs_info *fs_info = root->fs_info;
3839 struct btrfs_disk_key disk_key; 3835 struct btrfs_disk_key disk_key;
3840 struct extent_buffer *right = path->nodes[0]; 3836 struct extent_buffer *right = path->nodes[0];
3841 int i; 3837 int i;
@@ -3863,7 +3859,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3863 if (path->slots[0] < i) 3859 if (path->slots[0] < i)
3864 break; 3860 break;
3865 if (path->slots[0] == i) { 3861 if (path->slots[0] == i) {
3866 int space = btrfs_leaf_free_space(root, right); 3862 int space = btrfs_leaf_free_space(fs_info, right);
3867 if (space + push_space * 2 > free_space) 3863 if (space + push_space * 2 > free_space)
3868 break; 3864 break;
3869 } 3865 }
@@ -3896,7 +3892,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3896 btrfs_item_offset_nr(right, push_items - 1); 3892 btrfs_item_offset_nr(right, push_items - 1);
3897 3893
3898 copy_extent_buffer(left, right, btrfs_leaf_data(left) + 3894 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
3899 leaf_data_end(root, left) - push_space, 3895 leaf_data_end(fs_info, left) - push_space,
3900 btrfs_leaf_data(right) + 3896 btrfs_leaf_data(right) +
3901 btrfs_item_offset_nr(right, push_items - 1), 3897 btrfs_item_offset_nr(right, push_items - 1),
3902 push_space); 3898 push_space);
@@ -3923,11 +3919,11 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3923 3919
3924 if (push_items < right_nritems) { 3920 if (push_items < right_nritems) {
3925 push_space = btrfs_item_offset_nr(right, push_items - 1) - 3921 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3926 leaf_data_end(root, right); 3922 leaf_data_end(fs_info, right);
3927 memmove_extent_buffer(right, btrfs_leaf_data(right) + 3923 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3928 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, 3924 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3929 btrfs_leaf_data(right) + 3925 btrfs_leaf_data(right) +
3930 leaf_data_end(root, right), push_space); 3926 leaf_data_end(fs_info, right), push_space);
3931 3927
3932 memmove_extent_buffer(right, btrfs_item_nr_offset(0), 3928 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
3933 btrfs_item_nr_offset(push_items), 3929 btrfs_item_nr_offset(push_items),
@@ -3986,6 +3982,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
3986 *root, struct btrfs_path *path, int min_data_size, 3982 *root, struct btrfs_path *path, int min_data_size,
3987 int data_size, int empty, u32 max_slot) 3983 int data_size, int empty, u32 max_slot)
3988{ 3984{
3985 struct btrfs_fs_info *fs_info = root->fs_info;
3989 struct extent_buffer *right = path->nodes[0]; 3986 struct extent_buffer *right = path->nodes[0];
3990 struct extent_buffer *left; 3987 struct extent_buffer *left;
3991 int slot; 3988 int slot;
@@ -4005,7 +4002,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
4005 4002
4006 btrfs_assert_tree_locked(path->nodes[1]); 4003 btrfs_assert_tree_locked(path->nodes[1]);
4007 4004
4008 left = read_node_slot(root, path->nodes[1], slot - 1); 4005 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
4009 /* 4006 /*
4010 * slot - 1 is not valid or we fail to read the left node, 4007 * slot - 1 is not valid or we fail to read the left node,
4011 * no big deal, just return. 4008 * no big deal, just return.
@@ -4016,7 +4013,7 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
4016 btrfs_tree_lock(left); 4013 btrfs_tree_lock(left);
4017 btrfs_set_lock_blocking(left); 4014 btrfs_set_lock_blocking(left);
4018 4015
4019 free_space = btrfs_leaf_free_space(root, left); 4016 free_space = btrfs_leaf_free_space(fs_info, left);
4020 if (free_space < data_size) { 4017 if (free_space < data_size) {
4021 ret = 1; 4018 ret = 1;
4022 goto out; 4019 goto out;
@@ -4032,13 +4029,13 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
4032 goto out; 4029 goto out;
4033 } 4030 }
4034 4031
4035 free_space = btrfs_leaf_free_space(root, left); 4032 free_space = btrfs_leaf_free_space(fs_info, left);
4036 if (free_space < data_size) { 4033 if (free_space < data_size) {
4037 ret = 1; 4034 ret = 1;
4038 goto out; 4035 goto out;
4039 } 4036 }
4040 4037
4041 return __push_leaf_left(trans, root, path, min_data_size, 4038 return __push_leaf_left(trans, fs_info, path, min_data_size,
4042 empty, left, free_space, right_nritems, 4039 empty, left, free_space, right_nritems,
4043 max_slot); 4040 max_slot);
4044out: 4041out:
@@ -4052,13 +4049,12 @@ out:
4052 * available for the resulting leaf level of the path. 4049 * available for the resulting leaf level of the path.
4053 */ 4050 */
4054static noinline void copy_for_split(struct btrfs_trans_handle *trans, 4051static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4055 struct btrfs_root *root, 4052 struct btrfs_fs_info *fs_info,
4056 struct btrfs_path *path, 4053 struct btrfs_path *path,
4057 struct extent_buffer *l, 4054 struct extent_buffer *l,
4058 struct extent_buffer *right, 4055 struct extent_buffer *right,
4059 int slot, int mid, int nritems) 4056 int slot, int mid, int nritems)
4060{ 4057{
4061 struct btrfs_fs_info *fs_info = root->fs_info;
4062 int data_copy_size; 4058 int data_copy_size;
4063 int rt_data_off; 4059 int rt_data_off;
4064 int i; 4060 int i;
@@ -4069,7 +4065,7 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4069 4065
4070 nritems = nritems - mid; 4066 nritems = nritems - mid;
4071 btrfs_set_header_nritems(right, nritems); 4067 btrfs_set_header_nritems(right, nritems);
4072 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l); 4068 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
4073 4069
4074 copy_extent_buffer(right, l, btrfs_item_nr_offset(0), 4070 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4075 btrfs_item_nr_offset(mid), 4071 btrfs_item_nr_offset(mid),
@@ -4078,7 +4074,7 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4078 copy_extent_buffer(right, l, 4074 copy_extent_buffer(right, l,
4079 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(fs_info) - 4075 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(fs_info) -
4080 data_copy_size, btrfs_leaf_data(l) + 4076 data_copy_size, btrfs_leaf_data(l) +
4081 leaf_data_end(root, l), data_copy_size); 4077 leaf_data_end(fs_info, l), data_copy_size);
4082 4078
4083 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid); 4079 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4084 4080
@@ -4093,7 +4089,7 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4093 4089
4094 btrfs_set_header_nritems(l, mid); 4090 btrfs_set_header_nritems(l, mid);
4095 btrfs_item_key(right, &disk_key, 0); 4091 btrfs_item_key(right, &disk_key, 0);
4096 insert_ptr(trans, root, path, &disk_key, right->start, 4092 insert_ptr(trans, fs_info, path, &disk_key, right->start,
4097 path->slots[1] + 1, 1); 4093 path->slots[1] + 1, 1);
4098 4094
4099 btrfs_mark_buffer_dirty(right); 4095 btrfs_mark_buffer_dirty(right);
@@ -4129,6 +4125,7 @@ static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4129 struct btrfs_path *path, 4125 struct btrfs_path *path,
4130 int data_size) 4126 int data_size)
4131{ 4127{
4128 struct btrfs_fs_info *fs_info = root->fs_info;
4132 int ret; 4129 int ret;
4133 int progress = 0; 4130 int progress = 0;
4134 int slot; 4131 int slot;
@@ -4137,7 +4134,7 @@ static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4137 4134
4138 slot = path->slots[0]; 4135 slot = path->slots[0];
4139 if (slot < btrfs_header_nritems(path->nodes[0])) 4136 if (slot < btrfs_header_nritems(path->nodes[0]))
4140 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]); 4137 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
4141 4138
4142 /* 4139 /*
4143 * try to push all the items after our slot into the 4140 * try to push all the items after our slot into the
@@ -4158,7 +4155,7 @@ static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4158 if (path->slots[0] == 0 || path->slots[0] == nritems) 4155 if (path->slots[0] == 0 || path->slots[0] == nritems)
4159 return 0; 4156 return 0;
4160 4157
4161 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 4158 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4162 return 0; 4159 return 0;
4163 4160
4164 /* try to push all the items before our slot into the next leaf */ 4161 /* try to push all the items before our slot into the next leaf */
@@ -4211,7 +4208,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,
4211 int space_needed = data_size; 4208 int space_needed = data_size;
4212 4209
4213 if (slot < btrfs_header_nritems(l)) 4210 if (slot < btrfs_header_nritems(l))
4214 space_needed -= btrfs_leaf_free_space(root, l); 4211 space_needed -= btrfs_leaf_free_space(fs_info, l);
4215 4212
4216 wret = push_leaf_right(trans, root, path, space_needed, 4213 wret = push_leaf_right(trans, root, path, space_needed,
4217 space_needed, 0, 0); 4214 space_needed, 0, 0);
@@ -4226,7 +4223,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,
4226 l = path->nodes[0]; 4223 l = path->nodes[0];
4227 4224
4228 /* did the pushes work? */ 4225 /* did the pushes work? */
4229 if (btrfs_leaf_free_space(root, l) >= data_size) 4226 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
4230 return 0; 4227 return 0;
4231 } 4228 }
4232 4229
@@ -4303,8 +4300,8 @@ again:
4303 if (split == 0) { 4300 if (split == 0) {
4304 if (mid <= slot) { 4301 if (mid <= slot) {
4305 btrfs_set_header_nritems(right, 0); 4302 btrfs_set_header_nritems(right, 0);
4306 insert_ptr(trans, root, path, &disk_key, right->start, 4303 insert_ptr(trans, fs_info, path, &disk_key,
4307 path->slots[1] + 1, 1); 4304 right->start, path->slots[1] + 1, 1);
4308 btrfs_tree_unlock(path->nodes[0]); 4305 btrfs_tree_unlock(path->nodes[0]);
4309 free_extent_buffer(path->nodes[0]); 4306 free_extent_buffer(path->nodes[0]);
4310 path->nodes[0] = right; 4307 path->nodes[0] = right;
@@ -4312,8 +4309,8 @@ again:
4312 path->slots[1] += 1; 4309 path->slots[1] += 1;
4313 } else { 4310 } else {
4314 btrfs_set_header_nritems(right, 0); 4311 btrfs_set_header_nritems(right, 0);
4315 insert_ptr(trans, root, path, &disk_key, right->start, 4312 insert_ptr(trans, fs_info, path, &disk_key,
4316 path->slots[1], 1); 4313 right->start, path->slots[1], 1);
4317 btrfs_tree_unlock(path->nodes[0]); 4314 btrfs_tree_unlock(path->nodes[0]);
4318 free_extent_buffer(path->nodes[0]); 4315 free_extent_buffer(path->nodes[0]);
4319 path->nodes[0] = right; 4316 path->nodes[0] = right;
@@ -4329,7 +4326,7 @@ again:
4329 return ret; 4326 return ret;
4330 } 4327 }
4331 4328
4332 copy_for_split(trans, root, path, l, right, slot, mid, nritems); 4329 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
4333 4330
4334 if (split == 2) { 4331 if (split == 2) {
4335 BUG_ON(num_doubles != 0); 4332 BUG_ON(num_doubles != 0);
@@ -4342,7 +4339,7 @@ again:
4342push_for_double: 4339push_for_double:
4343 push_for_double_split(trans, root, path, data_size); 4340 push_for_double_split(trans, root, path, data_size);
4344 tried_avoid_double = 1; 4341 tried_avoid_double = 1;
4345 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size) 4342 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
4346 return 0; 4343 return 0;
4347 goto again; 4344 goto again;
4348} 4345}
@@ -4351,6 +4348,7 @@ static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4351 struct btrfs_root *root, 4348 struct btrfs_root *root,
4352 struct btrfs_path *path, int ins_len) 4349 struct btrfs_path *path, int ins_len)
4353{ 4350{
4351 struct btrfs_fs_info *fs_info = root->fs_info;
4354 struct btrfs_key key; 4352 struct btrfs_key key;
4355 struct extent_buffer *leaf; 4353 struct extent_buffer *leaf;
4356 struct btrfs_file_extent_item *fi; 4354 struct btrfs_file_extent_item *fi;
@@ -4364,7 +4362,7 @@ static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4364 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && 4362 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4365 key.type != BTRFS_EXTENT_CSUM_KEY); 4363 key.type != BTRFS_EXTENT_CSUM_KEY);
4366 4364
4367 if (btrfs_leaf_free_space(root, leaf) >= ins_len) 4365 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
4368 return 0; 4366 return 0;
4369 4367
4370 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 4368 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
@@ -4391,7 +4389,7 @@ static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4391 goto err; 4389 goto err;
4392 4390
4393 /* the leaf has changed, it now has room. return now */ 4391 /* the leaf has changed, it now has room. return now */
4394 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len) 4392 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
4395 goto err; 4393 goto err;
4396 4394
4397 if (key.type == BTRFS_EXTENT_DATA_KEY) { 4395 if (key.type == BTRFS_EXTENT_DATA_KEY) {
@@ -4415,7 +4413,7 @@ err:
4415} 4413}
4416 4414
4417static noinline int split_item(struct btrfs_trans_handle *trans, 4415static noinline int split_item(struct btrfs_trans_handle *trans,
4418 struct btrfs_root *root, 4416 struct btrfs_fs_info *fs_info,
4419 struct btrfs_path *path, 4417 struct btrfs_path *path,
4420 struct btrfs_key *new_key, 4418 struct btrfs_key *new_key,
4421 unsigned long split_offset) 4419 unsigned long split_offset)
@@ -4431,7 +4429,7 @@ static noinline int split_item(struct btrfs_trans_handle *trans,
4431 struct btrfs_disk_key disk_key; 4429 struct btrfs_disk_key disk_key;
4432 4430
4433 leaf = path->nodes[0]; 4431 leaf = path->nodes[0];
4434 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item)); 4432 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
4435 4433
4436 btrfs_set_path_blocking(path); 4434 btrfs_set_path_blocking(path);
4437 4435
@@ -4480,7 +4478,7 @@ static noinline int split_item(struct btrfs_trans_handle *trans,
4480 item_size - split_offset); 4478 item_size - split_offset);
4481 btrfs_mark_buffer_dirty(leaf); 4479 btrfs_mark_buffer_dirty(leaf);
4482 4480
4483 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0); 4481 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
4484 kfree(buf); 4482 kfree(buf);
4485 return 0; 4483 return 0;
4486} 4484}
@@ -4512,7 +4510,7 @@ int btrfs_split_item(struct btrfs_trans_handle *trans,
4512 if (ret) 4510 if (ret)
4513 return ret; 4511 return ret;
4514 4512
4515 ret = split_item(trans, root, path, new_key, split_offset); 4513 ret = split_item(trans, root->fs_info, path, new_key, split_offset);
4516 return ret; 4514 return ret;
4517} 4515}
4518 4516
@@ -4558,10 +4556,9 @@ int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4558 * off the end of the item or if we shift the item to chop bytes off 4556 * off the end of the item or if we shift the item to chop bytes off
4559 * the front. 4557 * the front.
4560 */ 4558 */
4561void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, 4559void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4562 u32 new_size, int from_end) 4560 struct btrfs_path *path, u32 new_size, int from_end)
4563{ 4561{
4564 struct btrfs_fs_info *fs_info = root->fs_info;
4565 int slot; 4562 int slot;
4566 struct extent_buffer *leaf; 4563 struct extent_buffer *leaf;
4567 struct btrfs_item *item; 4564 struct btrfs_item *item;
@@ -4583,7 +4580,7 @@ void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4583 return; 4580 return;
4584 4581
4585 nritems = btrfs_header_nritems(leaf); 4582 nritems = btrfs_header_nritems(leaf);
4586 data_end = leaf_data_end(root, leaf); 4583 data_end = leaf_data_end(fs_info, leaf);
4587 4584
4588 old_data_start = btrfs_item_offset_nr(leaf, slot); 4585 old_data_start = btrfs_item_offset_nr(leaf, slot);
4589 4586
@@ -4649,8 +4646,8 @@ void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4649 btrfs_set_item_size(leaf, item, new_size); 4646 btrfs_set_item_size(leaf, item, new_size);
4650 btrfs_mark_buffer_dirty(leaf); 4647 btrfs_mark_buffer_dirty(leaf);
4651 4648
4652 if (btrfs_leaf_free_space(root, leaf) < 0) { 4649 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4653 btrfs_print_leaf(root, leaf); 4650 btrfs_print_leaf(fs_info, leaf);
4654 BUG(); 4651 BUG();
4655 } 4652 }
4656} 4653}
@@ -4658,10 +4655,9 @@ void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4658/* 4655/*
4659 * make the item pointed to by the path bigger, data_size is the added size. 4656 * make the item pointed to by the path bigger, data_size is the added size.
4660 */ 4657 */
4661void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, 4658void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
4662 u32 data_size) 4659 u32 data_size)
4663{ 4660{
4664 struct btrfs_fs_info *fs_info = root->fs_info;
4665 int slot; 4661 int slot;
4666 struct extent_buffer *leaf; 4662 struct extent_buffer *leaf;
4667 struct btrfs_item *item; 4663 struct btrfs_item *item;
@@ -4677,10 +4673,10 @@ void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
4677 leaf = path->nodes[0]; 4673 leaf = path->nodes[0];
4678 4674
4679 nritems = btrfs_header_nritems(leaf); 4675 nritems = btrfs_header_nritems(leaf);
4680 data_end = leaf_data_end(root, leaf); 4676 data_end = leaf_data_end(fs_info, leaf);
4681 4677
4682 if (btrfs_leaf_free_space(root, leaf) < data_size) { 4678 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
4683 btrfs_print_leaf(root, leaf); 4679 btrfs_print_leaf(fs_info, leaf);
4684 BUG(); 4680 BUG();
4685 } 4681 }
4686 slot = path->slots[0]; 4682 slot = path->slots[0];
@@ -4688,7 +4684,7 @@ void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
4688 4684
4689 BUG_ON(slot < 0); 4685 BUG_ON(slot < 0);
4690 if (slot >= nritems) { 4686 if (slot >= nritems) {
4691 btrfs_print_leaf(root, leaf); 4687 btrfs_print_leaf(fs_info, leaf);
4692 btrfs_crit(fs_info, "slot %d too large, nritems %d", 4688 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4693 slot, nritems); 4689 slot, nritems);
4694 BUG_ON(1); 4690 BUG_ON(1);
@@ -4718,8 +4714,8 @@ void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
4718 btrfs_set_item_size(leaf, item, old_size + data_size); 4714 btrfs_set_item_size(leaf, item, old_size + data_size);
4719 btrfs_mark_buffer_dirty(leaf); 4715 btrfs_mark_buffer_dirty(leaf);
4720 4716
4721 if (btrfs_leaf_free_space(root, leaf) < 0) { 4717 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4722 btrfs_print_leaf(root, leaf); 4718 btrfs_print_leaf(fs_info, leaf);
4723 BUG(); 4719 BUG();
4724 } 4720 }
4725} 4721}
@@ -4755,12 +4751,12 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4755 slot = path->slots[0]; 4751 slot = path->slots[0];
4756 4752
4757 nritems = btrfs_header_nritems(leaf); 4753 nritems = btrfs_header_nritems(leaf);
4758 data_end = leaf_data_end(root, leaf); 4754 data_end = leaf_data_end(fs_info, leaf);
4759 4755
4760 if (btrfs_leaf_free_space(root, leaf) < total_size) { 4756 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
4761 btrfs_print_leaf(root, leaf); 4757 btrfs_print_leaf(fs_info, leaf);
4762 btrfs_crit(fs_info, "not enough freespace need %u have %d", 4758 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4763 total_size, btrfs_leaf_free_space(root, leaf)); 4759 total_size, btrfs_leaf_free_space(fs_info, leaf));
4764 BUG(); 4760 BUG();
4765 } 4761 }
4766 4762
@@ -4768,7 +4764,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4768 unsigned int old_data = btrfs_item_end_nr(leaf, slot); 4764 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4769 4765
4770 if (old_data < data_end) { 4766 if (old_data < data_end) {
4771 btrfs_print_leaf(root, leaf); 4767 btrfs_print_leaf(fs_info, leaf);
4772 btrfs_crit(fs_info, "slot %d old_data %d data_end %d", 4768 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4773 slot, old_data, data_end); 4769 slot, old_data, data_end);
4774 BUG_ON(1); 4770 BUG_ON(1);
@@ -4811,8 +4807,8 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4811 btrfs_set_header_nritems(leaf, nritems + nr); 4807 btrfs_set_header_nritems(leaf, nritems + nr);
4812 btrfs_mark_buffer_dirty(leaf); 4808 btrfs_mark_buffer_dirty(leaf);
4813 4809
4814 if (btrfs_leaf_free_space(root, leaf) < 0) { 4810 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
4815 btrfs_print_leaf(root, leaf); 4811 btrfs_print_leaf(fs_info, leaf);
4816 BUG(); 4812 BUG();
4817 } 4813 }
4818} 4814}
@@ -4982,7 +4978,7 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4982 nritems = btrfs_header_nritems(leaf); 4978 nritems = btrfs_header_nritems(leaf);
4983 4979
4984 if (slot + nr != nritems) { 4980 if (slot + nr != nritems) {
4985 int data_end = leaf_data_end(root, leaf); 4981 int data_end = leaf_data_end(fs_info, leaf);
4986 4982
4987 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) + 4983 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4988 data_end + dsize, 4984 data_end + dsize,
@@ -5145,6 +5141,7 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
5145 struct btrfs_path *path, 5141 struct btrfs_path *path,
5146 u64 min_trans) 5142 u64 min_trans)
5147{ 5143{
5144 struct btrfs_fs_info *fs_info = root->fs_info;
5148 struct extent_buffer *cur; 5145 struct extent_buffer *cur;
5149 struct btrfs_key found_key; 5146 struct btrfs_key found_key;
5150 int slot; 5147 int slot;
@@ -5221,7 +5218,7 @@ find_next_key:
5221 goto out; 5218 goto out;
5222 } 5219 }
5223 btrfs_set_path_blocking(path); 5220 btrfs_set_path_blocking(path);
5224 cur = read_node_slot(root, cur, slot); 5221 cur = read_node_slot(fs_info, cur, slot);
5225 if (IS_ERR(cur)) { 5222 if (IS_ERR(cur)) {
5226 ret = PTR_ERR(cur); 5223 ret = PTR_ERR(cur);
5227 goto out; 5224 goto out;
@@ -5244,14 +5241,14 @@ out:
5244 return ret; 5241 return ret;
5245} 5242}
5246 5243
5247static int tree_move_down(struct btrfs_root *root, 5244static int tree_move_down(struct btrfs_fs_info *fs_info,
5248 struct btrfs_path *path, 5245 struct btrfs_path *path,
5249 int *level, int root_level) 5246 int *level, int root_level)
5250{ 5247{
5251 struct extent_buffer *eb; 5248 struct extent_buffer *eb;
5252 5249
5253 BUG_ON(*level == 0); 5250 BUG_ON(*level == 0);
5254 eb = read_node_slot(root, path->nodes[*level], path->slots[*level]); 5251 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
5255 if (IS_ERR(eb)) 5252 if (IS_ERR(eb))
5256 return PTR_ERR(eb); 5253 return PTR_ERR(eb);
5257 5254
@@ -5261,7 +5258,7 @@ static int tree_move_down(struct btrfs_root *root,
5261 return 0; 5258 return 0;
5262} 5259}
5263 5260
5264static int tree_move_next_or_upnext(struct btrfs_root *root, 5261static int tree_move_next_or_upnext(struct btrfs_fs_info *fs_info,
5265 struct btrfs_path *path, 5262 struct btrfs_path *path,
5266 int *level, int root_level) 5263 int *level, int root_level)
5267{ 5264{
@@ -5292,7 +5289,7 @@ static int tree_move_next_or_upnext(struct btrfs_root *root,
5292 * Returns 1 if it had to move up and next. 0 is returned if it moved only next 5289 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5293 * or down. 5290 * or down.
5294 */ 5291 */
5295static int tree_advance(struct btrfs_root *root, 5292static int tree_advance(struct btrfs_fs_info *fs_info,
5296 struct btrfs_path *path, 5293 struct btrfs_path *path,
5297 int *level, int root_level, 5294 int *level, int root_level,
5298 int allow_down, 5295 int allow_down,
@@ -5301,9 +5298,10 @@ static int tree_advance(struct btrfs_root *root,
5301 int ret; 5298 int ret;
5302 5299
5303 if (*level == 0 || !allow_down) { 5300 if (*level == 0 || !allow_down) {
5304 ret = tree_move_next_or_upnext(root, path, level, root_level); 5301 ret = tree_move_next_or_upnext(fs_info, path, level,
5302 root_level);
5305 } else { 5303 } else {
5306 ret = tree_move_down(root, path, level, root_level); 5304 ret = tree_move_down(fs_info, path, level, root_level);
5307 } 5305 }
5308 if (ret >= 0) { 5306 if (ret >= 0) {
5309 if (*level == 0) 5307 if (*level == 0)
@@ -5316,8 +5314,7 @@ static int tree_advance(struct btrfs_root *root,
5316 return ret; 5314 return ret;
5317} 5315}
5318 5316
5319static int tree_compare_item(struct btrfs_root *left_root, 5317static int tree_compare_item(struct btrfs_path *left_path,
5320 struct btrfs_path *left_path,
5321 struct btrfs_path *right_path, 5318 struct btrfs_path *right_path,
5322 char *tmp_buf) 5319 char *tmp_buf)
5323{ 5320{
@@ -5474,7 +5471,7 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5474 5471
5475 while (1) { 5472 while (1) {
5476 if (advance_left && !left_end_reached) { 5473 if (advance_left && !left_end_reached) {
5477 ret = tree_advance(left_root, left_path, &left_level, 5474 ret = tree_advance(fs_info, left_path, &left_level,
5478 left_root_level, 5475 left_root_level,
5479 advance_left != ADVANCE_ONLY_NEXT, 5476 advance_left != ADVANCE_ONLY_NEXT,
5480 &left_key); 5477 &left_key);
@@ -5485,7 +5482,7 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5485 advance_left = 0; 5482 advance_left = 0;
5486 } 5483 }
5487 if (advance_right && !right_end_reached) { 5484 if (advance_right && !right_end_reached) {
5488 ret = tree_advance(right_root, right_path, &right_level, 5485 ret = tree_advance(fs_info, right_path, &right_level,
5489 right_root_level, 5486 right_root_level,
5490 advance_right != ADVANCE_ONLY_NEXT, 5487 advance_right != ADVANCE_ONLY_NEXT,
5491 &right_key); 5488 &right_key);
@@ -5549,8 +5546,8 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5549 enum btrfs_compare_tree_result result; 5546 enum btrfs_compare_tree_result result;
5550 5547
5551 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); 5548 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
5552 ret = tree_compare_item(left_root, left_path, 5549 ret = tree_compare_item(left_path, right_path,
5553 right_path, tmp_buf); 5550 tmp_buf);
5554 if (ret) 5551 if (ret)
5555 result = BTRFS_COMPARE_TREE_CHANGED; 5552 result = BTRFS_COMPARE_TREE_CHANGED;
5556 else 5553 else
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 860103020ac9..fc1864acb368 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1349,10 +1349,9 @@ static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
1349 1349
1350#ifdef CONFIG_BTRFS_DEBUG 1350#ifdef CONFIG_BTRFS_DEBUG
1351static inline int 1351static inline int
1352btrfs_should_fragment_free_space(struct btrfs_root *root, 1352btrfs_should_fragment_free_space(struct btrfs_block_group_cache *block_group)
1353 struct btrfs_block_group_cache *block_group)
1354{ 1353{
1355 struct btrfs_fs_info *fs_info = root->fs_info; 1354 struct btrfs_fs_info *fs_info = block_group->fs_info;
1356 1355
1357 return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) && 1356 return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
1358 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) || 1357 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
@@ -2311,10 +2310,9 @@ static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
2311 * this returns the address of the start of the last item, 2310 * this returns the address of the start of the last item,
2312 * which is the stop of the leaf data stack 2311 * which is the stop of the leaf data stack
2313 */ 2312 */
2314static inline unsigned int leaf_data_end(struct btrfs_root *root, 2313static inline unsigned int leaf_data_end(struct btrfs_fs_info *fs_info,
2315 struct extent_buffer *leaf) 2314 struct extent_buffer *leaf)
2316{ 2315{
2317 struct btrfs_fs_info *fs_info = root->fs_info;
2318 u32 nr = btrfs_header_nritems(leaf); 2316 u32 nr = btrfs_header_nritems(leaf);
2319 2317
2320 if (nr == 0) 2318 if (nr == 0)
@@ -2536,7 +2534,7 @@ static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
2536 2534
2537/* extent-tree.c */ 2535/* extent-tree.c */
2538 2536
2539u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes); 2537u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes);
2540 2538
2541static inline u64 btrfs_calc_trans_metadata_size(struct btrfs_fs_info *fs_info, 2539static inline u64 btrfs_calc_trans_metadata_size(struct btrfs_fs_info *fs_info,
2542 unsigned num_items) 2540 unsigned num_items)
@@ -2555,9 +2553,9 @@ static inline u64 btrfs_calc_trunc_metadata_size(struct btrfs_fs_info *fs_info,
2555} 2553}
2556 2554
2557int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, 2555int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2558 struct btrfs_root *root); 2556 struct btrfs_fs_info *fs_info);
2559int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, 2557int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2560 struct btrfs_root *root); 2558 struct btrfs_fs_info *fs_info);
2561void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, 2559void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info,
2562 const u64 start); 2560 const u64 start);
2563void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg); 2561void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg);
@@ -2566,18 +2564,18 @@ void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr);
2566void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg); 2564void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg);
2567void btrfs_put_block_group(struct btrfs_block_group_cache *cache); 2565void btrfs_put_block_group(struct btrfs_block_group_cache *cache);
2568int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 2566int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2569 struct btrfs_root *root, unsigned long count); 2567 struct btrfs_fs_info *fs_info, unsigned long count);
2570int btrfs_async_run_delayed_refs(struct btrfs_root *root, 2568int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
2571 unsigned long count, u64 transid, int wait); 2569 unsigned long count, u64 transid, int wait);
2572int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len); 2570int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
2573int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, 2571int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
2574 struct btrfs_root *root, u64 bytenr, 2572 struct btrfs_fs_info *fs_info, u64 bytenr,
2575 u64 offset, int metadata, u64 *refs, u64 *flags); 2573 u64 offset, int metadata, u64 *refs, u64 *flags);
2576int btrfs_pin_extent(struct btrfs_root *root, 2574int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2577 u64 bytenr, u64 num, int reserved); 2575 u64 bytenr, u64 num, int reserved);
2578int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, 2576int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
2579 u64 bytenr, u64 num_bytes); 2577 u64 bytenr, u64 num_bytes);
2580int btrfs_exclude_logged_extents(struct btrfs_root *root, 2578int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
2581 struct extent_buffer *eb); 2579 struct extent_buffer *eb);
2582int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, 2580int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2583 struct btrfs_root *root, 2581 struct btrfs_root *root,
@@ -2598,12 +2596,11 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
2598 struct extent_buffer *buf, 2596 struct extent_buffer *buf,
2599 u64 parent, int last_ref); 2597 u64 parent, int last_ref);
2600int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, 2598int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
2601 struct btrfs_root *root,
2602 u64 root_objectid, u64 owner, 2599 u64 root_objectid, u64 owner,
2603 u64 offset, u64 ram_bytes, 2600 u64 offset, u64 ram_bytes,
2604 struct btrfs_key *ins); 2601 struct btrfs_key *ins);
2605int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, 2602int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
2606 struct btrfs_root *root, 2603 struct btrfs_fs_info *fs_info,
2607 u64 root_objectid, u64 owner, u64 offset, 2604 u64 root_objectid, u64 owner, u64 offset,
2608 struct btrfs_key *ins); 2605 struct btrfs_key *ins);
2609int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes, 2606int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
@@ -2614,39 +2611,39 @@ int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2614int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2611int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2615 struct extent_buffer *buf, int full_backref); 2612 struct extent_buffer *buf, int full_backref);
2616int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, 2613int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2617 struct btrfs_root *root, 2614 struct btrfs_fs_info *fs_info,
2618 u64 bytenr, u64 num_bytes, u64 flags, 2615 u64 bytenr, u64 num_bytes, u64 flags,
2619 int level, int is_data); 2616 int level, int is_data);
2620int btrfs_free_extent(struct btrfs_trans_handle *trans, 2617int btrfs_free_extent(struct btrfs_trans_handle *trans,
2621 struct btrfs_root *root, 2618 struct btrfs_fs_info *fs_info,
2622 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, 2619 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
2623 u64 owner, u64 offset); 2620 u64 owner, u64 offset);
2624 2621
2625int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len, 2622int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
2626 int delalloc); 2623 u64 start, u64 len, int delalloc);
2627int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, 2624int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
2628 u64 start, u64 len); 2625 u64 start, u64 len);
2629void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 2626void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
2630 struct btrfs_root *root); 2627 struct btrfs_fs_info *fs_info);
2631int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, 2628int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2632 struct btrfs_root *root); 2629 struct btrfs_fs_info *fs_info);
2633int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 2630int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2634 struct btrfs_root *root, 2631 struct btrfs_fs_info *fs_info,
2635 u64 bytenr, u64 num_bytes, u64 parent, 2632 u64 bytenr, u64 num_bytes, u64 parent,
2636 u64 root_objectid, u64 owner, u64 offset); 2633 u64 root_objectid, u64 owner, u64 offset);
2637 2634
2638int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, 2635int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
2639 struct btrfs_root *root); 2636 struct btrfs_fs_info *fs_info);
2640int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, 2637int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2641 struct btrfs_root *root); 2638 struct btrfs_fs_info *fs_info);
2642int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, 2639int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *root); 2640 struct btrfs_fs_info *fs_info);
2644int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr); 2641int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);
2645int btrfs_free_block_groups(struct btrfs_fs_info *info); 2642int btrfs_free_block_groups(struct btrfs_fs_info *info);
2646int btrfs_read_block_groups(struct btrfs_fs_info *info); 2643int btrfs_read_block_groups(struct btrfs_fs_info *info);
2647int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr); 2644int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr);
2648int btrfs_make_block_group(struct btrfs_trans_handle *trans, 2645int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2649 struct btrfs_root *root, u64 bytes_used, 2646 struct btrfs_fs_info *fs_info, u64 bytes_used,
2650 u64 type, u64 chunk_objectid, u64 chunk_offset, 2647 u64 type, u64 chunk_objectid, u64 chunk_offset,
2651 u64 size); 2648 u64 size);
2652struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( 2649struct btrfs_trans_handle *btrfs_start_trans_remove_block_group(
@@ -2659,7 +2656,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info);
2659void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache); 2656void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache);
2660void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *cache); 2657void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *cache);
2661void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, 2658void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
2662 struct btrfs_root *root); 2659 struct btrfs_fs_info *fs_info);
2663u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data); 2660u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data);
2664void btrfs_clear_space_info_full(struct btrfs_fs_info *info); 2661void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
2665 2662
@@ -2689,7 +2686,7 @@ void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len);
2689void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, 2686void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
2690 u64 len); 2687 u64 len);
2691void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, 2688void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root); 2689 struct btrfs_fs_info *fs_info);
2693void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans); 2690void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans);
2694int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, 2691int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
2695 struct inode *inode); 2692 struct inode *inode);
@@ -2698,7 +2695,7 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
2698 struct btrfs_block_rsv *rsv, 2695 struct btrfs_block_rsv *rsv,
2699 int nitems, 2696 int nitems,
2700 u64 *qgroup_reserved, bool use_global_rsv); 2697 u64 *qgroup_reserved, bool use_global_rsv);
2701void btrfs_subvolume_release_metadata(struct btrfs_root *root, 2698void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
2702 struct btrfs_block_rsv *rsv, 2699 struct btrfs_block_rsv *rsv,
2703 u64 qgroup_reserved); 2700 u64 qgroup_reserved);
2704int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes); 2701int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes);
@@ -2706,16 +2703,15 @@ void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes);
2706int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len); 2703int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len);
2707void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len); 2704void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len);
2708void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type); 2705void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type);
2709struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, 2706struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
2710 unsigned short type); 2707 unsigned short type);
2711void btrfs_free_block_rsv(struct btrfs_root *root, 2708void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
2712 struct btrfs_block_rsv *rsv); 2709 struct btrfs_block_rsv *rsv);
2713void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv); 2710void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv);
2714int btrfs_block_rsv_add(struct btrfs_root *root, 2711int btrfs_block_rsv_add(struct btrfs_root *root,
2715 struct btrfs_block_rsv *block_rsv, u64 num_bytes, 2712 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
2716 enum btrfs_reserve_flush_enum flush); 2713 enum btrfs_reserve_flush_enum flush);
2717int btrfs_block_rsv_check(struct btrfs_root *root, 2714int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor);
2718 struct btrfs_block_rsv *block_rsv, int min_factor);
2719int btrfs_block_rsv_refill(struct btrfs_root *root, 2715int btrfs_block_rsv_refill(struct btrfs_root *root,
2720 struct btrfs_block_rsv *block_rsv, u64 min_reserved, 2716 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
2721 enum btrfs_reserve_flush_enum flush); 2717 enum btrfs_reserve_flush_enum flush);
@@ -2725,22 +2721,21 @@ int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
2725int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, 2721int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
2726 struct btrfs_block_rsv *dest, u64 num_bytes, 2722 struct btrfs_block_rsv *dest, u64 num_bytes,
2727 int min_factor); 2723 int min_factor);
2728void btrfs_block_rsv_release(struct btrfs_root *root, 2724void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
2729 struct btrfs_block_rsv *block_rsv, 2725 struct btrfs_block_rsv *block_rsv,
2730 u64 num_bytes); 2726 u64 num_bytes);
2731int btrfs_inc_block_group_ro(struct btrfs_root *root, 2727int btrfs_inc_block_group_ro(struct btrfs_root *root,
2732 struct btrfs_block_group_cache *cache); 2728 struct btrfs_block_group_cache *cache);
2733void btrfs_dec_block_group_ro(struct btrfs_root *root, 2729void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache);
2734 struct btrfs_block_group_cache *cache);
2735void btrfs_put_block_group_cache(struct btrfs_fs_info *info); 2730void btrfs_put_block_group_cache(struct btrfs_fs_info *info);
2736u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo); 2731u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
2737int btrfs_error_unpin_extent_range(struct btrfs_root *root, 2732int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
2738 u64 start, u64 end); 2733 u64 start, u64 end);
2739int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, 2734int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2740 u64 num_bytes, u64 *actual_bytes); 2735 u64 num_bytes, u64 *actual_bytes);
2741int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, 2736int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
2742 struct btrfs_root *root, u64 type); 2737 struct btrfs_fs_info *fs_info, u64 type);
2743int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range); 2738int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
2744 2739
2745int btrfs_init_space_info(struct btrfs_fs_info *fs_info); 2740int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
2746int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans, 2741int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
@@ -2750,8 +2745,7 @@ int btrfs_start_write_no_snapshoting(struct btrfs_root *root);
2750void btrfs_end_write_no_snapshoting(struct btrfs_root *root); 2745void btrfs_end_write_no_snapshoting(struct btrfs_root *root);
2751void btrfs_wait_for_snapshot_creation(struct btrfs_root *root); 2746void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
2752void check_system_chunk(struct btrfs_trans_handle *trans, 2747void check_system_chunk(struct btrfs_trans_handle *trans,
2753 struct btrfs_root *root, 2748 struct btrfs_fs_info *fs_info, const u64 type);
2754 const u64 type);
2755u64 add_new_free_space(struct btrfs_block_group_cache *block_group, 2749u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
2756 struct btrfs_fs_info *info, u64 start, u64 end); 2750 struct btrfs_fs_info *info, u64 start, u64 end);
2757 2751
@@ -2801,10 +2795,10 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
2801 struct extent_buffer **cow_ret, u64 new_root_objectid); 2795 struct extent_buffer **cow_ret, u64 new_root_objectid);
2802int btrfs_block_can_be_shared(struct btrfs_root *root, 2796int btrfs_block_can_be_shared(struct btrfs_root *root,
2803 struct extent_buffer *buf); 2797 struct extent_buffer *buf);
2804void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, 2798void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
2805 u32 data_size); 2799 u32 data_size);
2806void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, 2800void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
2807 u32 new_size, int from_end); 2801 struct btrfs_path *path, u32 new_size, int from_end);
2808int btrfs_split_item(struct btrfs_trans_handle *trans, 2802int btrfs_split_item(struct btrfs_trans_handle *trans,
2809 struct btrfs_root *root, 2803 struct btrfs_root *root,
2810 struct btrfs_path *path, 2804 struct btrfs_path *path,
@@ -2880,7 +2874,8 @@ static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
2880{ 2874{
2881 return btrfs_next_old_item(root, p, 0); 2875 return btrfs_next_old_item(root, p, 0);
2882} 2876}
2883int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf); 2877int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
2878 struct extent_buffer *leaf);
2884int __must_check btrfs_drop_snapshot(struct btrfs_root *root, 2879int __must_check btrfs_drop_snapshot(struct btrfs_root *root,
2885 struct btrfs_block_rsv *block_rsv, 2880 struct btrfs_block_rsv *block_rsv,
2886 int update_ref, int for_reloc); 2881 int update_ref, int for_reloc);
@@ -2906,11 +2901,9 @@ static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
2906 * anything except sleeping. This function is used to check the status of 2901 * anything except sleeping. This function is used to check the status of
2907 * the fs. 2902 * the fs.
2908 */ 2903 */
2909static inline int btrfs_need_cleaner_sleep(struct btrfs_root *root) 2904static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
2910{ 2905{
2911 struct btrfs_fs_info *fs_info = root->fs_info; 2906 return fs_info->sb->s_flags & MS_RDONLY || btrfs_fs_closing(fs_info);
2912
2913 return (fs_info->sb->s_flags & MS_RDONLY || btrfs_fs_closing(fs_info));
2914} 2907}
2915 2908
2916static inline void free_fs_info(struct btrfs_fs_info *fs_info) 2909static inline void free_fs_info(struct btrfs_fs_info *fs_info)
@@ -3013,10 +3006,10 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
3013 struct btrfs_path *path, u64 dir, 3006 struct btrfs_path *path, u64 dir,
3014 const char *name, u16 name_len, 3007 const char *name, u16 name_len,
3015 int mod); 3008 int mod);
3016int verify_dir_item(struct btrfs_root *root, 3009int verify_dir_item(struct btrfs_fs_info *fs_info,
3017 struct extent_buffer *leaf, 3010 struct extent_buffer *leaf,
3018 struct btrfs_dir_item *dir_item); 3011 struct btrfs_dir_item *dir_item);
3019struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, 3012struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
3020 struct btrfs_path *path, 3013 struct btrfs_path *path,
3021 const char *name, 3014 const char *name,
3022 int name_len); 3015 int name_len);
@@ -3061,10 +3054,9 @@ int btrfs_find_name_in_ext_backref(struct btrfs_path *path,
3061struct btrfs_dio_private; 3054struct btrfs_dio_private;
3062int btrfs_del_csums(struct btrfs_trans_handle *trans, 3055int btrfs_del_csums(struct btrfs_trans_handle *trans,
3063 struct btrfs_fs_info *fs_info, u64 bytenr, u64 len); 3056 struct btrfs_fs_info *fs_info, u64 bytenr, u64 len);
3064int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, 3057int btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst);
3065 struct bio *bio, u32 *dst); 3058int btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio,
3066int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode, 3059 u64 logical_offset);
3067 struct bio *bio, u64 logical_offset);
3068int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, 3060int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
3069 struct btrfs_root *root, 3061 struct btrfs_root *root,
3070 u64 objectid, u64 pos, 3062 u64 objectid, u64 pos,
@@ -3078,8 +3070,8 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
3078int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, 3070int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
3079 struct btrfs_root *root, 3071 struct btrfs_root *root,
3080 struct btrfs_ordered_sum *sums); 3072 struct btrfs_ordered_sum *sums);
3081int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, 3073int btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
3082 struct bio *bio, u64 file_start, int contig); 3074 u64 file_start, int contig);
3083int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 3075int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
3084 struct list_head *list, int search_commit); 3076 struct list_head *list, int search_commit);
3085void btrfs_extent_item_to_extent_map(struct inode *inode, 3077void btrfs_extent_item_to_extent_map(struct inode *inode,
@@ -3182,7 +3174,7 @@ void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3182int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size); 3174int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size);
3183void btrfs_invalidate_inodes(struct btrfs_root *root); 3175void btrfs_invalidate_inodes(struct btrfs_root *root);
3184void btrfs_add_delayed_iput(struct inode *inode); 3176void btrfs_add_delayed_iput(struct inode *inode);
3185void btrfs_run_delayed_iputs(struct btrfs_root *root); 3177void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
3186int btrfs_prealloc_file_range(struct inode *inode, int mode, 3178int btrfs_prealloc_file_range(struct inode *inode, int mode,
3187 u64 start, u64 num_bytes, u64 min_size, 3179 u64 start, u64 num_bytes, u64 min_size,
3188 loff_t actual_len, u64 *alloc_hint); 3180 loff_t actual_len, u64 *alloc_hint);
@@ -3236,9 +3228,8 @@ int btrfs_drop_extents(struct btrfs_trans_handle *trans,
3236int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, 3228int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
3237 struct inode *inode, u64 start, u64 end); 3229 struct inode *inode, u64 start, u64 end);
3238int btrfs_release_file(struct inode *inode, struct file *file); 3230int btrfs_release_file(struct inode *inode, struct file *file);
3239int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode, 3231int btrfs_dirty_pages(struct inode *inode, struct page **pages,
3240 struct page **pages, size_t num_pages, 3232 size_t num_pages, loff_t pos, size_t write_bytes,
3241 loff_t pos, size_t write_bytes,
3242 struct extent_state **cached); 3233 struct extent_state **cached);
3243int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end); 3234int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
3244ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in, 3235ssize_t btrfs_copy_file_range(struct file *file_in, loff_t pos_in,
@@ -3261,7 +3252,7 @@ void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
3261ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size); 3252ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
3262 3253
3263/* super.c */ 3254/* super.c */
3264int btrfs_parse_options(struct btrfs_root *root, char *options, 3255int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
3265 unsigned long new_flags); 3256 unsigned long new_flags);
3266int btrfs_sync_fs(struct super_block *sb, int wait); 3257int btrfs_sync_fs(struct super_block *sb, int wait);
3267 3258
@@ -3637,12 +3628,12 @@ int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
3637int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start, 3628int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3638 u64 end, struct btrfs_scrub_progress *progress, 3629 u64 end, struct btrfs_scrub_progress *progress,
3639 int readonly, int is_dev_replace); 3630 int readonly, int is_dev_replace);
3640void btrfs_scrub_pause(struct btrfs_root *root); 3631void btrfs_scrub_pause(struct btrfs_fs_info *fs_info);
3641void btrfs_scrub_continue(struct btrfs_root *root); 3632void btrfs_scrub_continue(struct btrfs_fs_info *fs_info);
3642int btrfs_scrub_cancel(struct btrfs_fs_info *info); 3633int btrfs_scrub_cancel(struct btrfs_fs_info *info);
3643int btrfs_scrub_cancel_dev(struct btrfs_fs_info *info, 3634int btrfs_scrub_cancel_dev(struct btrfs_fs_info *info,
3644 struct btrfs_device *dev); 3635 struct btrfs_device *dev);
3645int btrfs_scrub_progress(struct btrfs_root *root, u64 devid, 3636int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
3646 struct btrfs_scrub_progress *progress); 3637 struct btrfs_scrub_progress *progress);
3647 3638
3648/* dev-replace.c */ 3639/* dev-replace.c */
diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c
index c8ffceb2aff9..33ed79b8d6cc 100644
--- a/fs/btrfs/delayed-inode.c
+++ b/fs/btrfs/delayed-inode.c
@@ -529,10 +529,9 @@ static struct btrfs_delayed_item *__btrfs_next_delayed_item(
529} 529}
530 530
531static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans, 531static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root, 532 struct btrfs_fs_info *fs_info,
533 struct btrfs_delayed_item *item) 533 struct btrfs_delayed_item *item)
534{ 534{
535 struct btrfs_fs_info *fs_info = root->fs_info;
536 struct btrfs_block_rsv *src_rsv; 535 struct btrfs_block_rsv *src_rsv;
537 struct btrfs_block_rsv *dst_rsv; 536 struct btrfs_block_rsv *dst_rsv;
538 u64 num_bytes; 537 u64 num_bytes;
@@ -556,10 +555,9 @@ static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
556 return ret; 555 return ret;
557} 556}
558 557
559static void btrfs_delayed_item_release_metadata(struct btrfs_root *root, 558static void btrfs_delayed_item_release_metadata(struct btrfs_fs_info *fs_info,
560 struct btrfs_delayed_item *item) 559 struct btrfs_delayed_item *item)
561{ 560{
562 struct btrfs_fs_info *fs_info = root->fs_info;
563 struct btrfs_block_rsv *rsv; 561 struct btrfs_block_rsv *rsv;
564 562
565 if (!item->bytes_reserved) 563 if (!item->bytes_reserved)
@@ -569,7 +567,7 @@ static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
569 trace_btrfs_space_reservation(fs_info, "delayed_item", 567 trace_btrfs_space_reservation(fs_info, "delayed_item",
570 item->key.objectid, item->bytes_reserved, 568 item->key.objectid, item->bytes_reserved,
571 0); 569 0);
572 btrfs_block_rsv_release(root, rsv, 570 btrfs_block_rsv_release(fs_info, rsv,
573 item->bytes_reserved); 571 item->bytes_reserved);
574} 572}
575 573
@@ -669,16 +667,15 @@ static int btrfs_delayed_inode_reserve_metadata(
669 if (release) { 667 if (release) {
670 trace_btrfs_space_reservation(fs_info, "delalloc", 668 trace_btrfs_space_reservation(fs_info, "delalloc",
671 btrfs_ino(inode), num_bytes, 0); 669 btrfs_ino(inode), num_bytes, 0);
672 btrfs_block_rsv_release(root, src_rsv, num_bytes); 670 btrfs_block_rsv_release(fs_info, src_rsv, num_bytes);
673 } 671 }
674 672
675 return ret; 673 return ret;
676} 674}
677 675
678static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root, 676static void btrfs_delayed_inode_release_metadata(struct btrfs_fs_info *fs_info,
679 struct btrfs_delayed_node *node) 677 struct btrfs_delayed_node *node)
680{ 678{
681 struct btrfs_fs_info *fs_info = root->fs_info;
682 struct btrfs_block_rsv *rsv; 679 struct btrfs_block_rsv *rsv;
683 680
684 if (!node->bytes_reserved) 681 if (!node->bytes_reserved)
@@ -687,7 +684,7 @@ static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
687 rsv = &fs_info->delayed_block_rsv; 684 rsv = &fs_info->delayed_block_rsv;
688 trace_btrfs_space_reservation(fs_info, "delayed_inode", 685 trace_btrfs_space_reservation(fs_info, "delayed_inode",
689 node->inode_id, node->bytes_reserved, 0); 686 node->inode_id, node->bytes_reserved, 0);
690 btrfs_block_rsv_release(root, rsv, 687 btrfs_block_rsv_release(fs_info, rsv,
691 node->bytes_reserved); 688 node->bytes_reserved);
692 node->bytes_reserved = 0; 689 node->bytes_reserved = 0;
693} 690}
@@ -700,6 +697,7 @@ static int btrfs_batch_insert_items(struct btrfs_root *root,
700 struct btrfs_path *path, 697 struct btrfs_path *path,
701 struct btrfs_delayed_item *item) 698 struct btrfs_delayed_item *item)
702{ 699{
700 struct btrfs_fs_info *fs_info = root->fs_info;
703 struct btrfs_delayed_item *curr, *next; 701 struct btrfs_delayed_item *curr, *next;
704 int free_space; 702 int free_space;
705 int total_data_size = 0, total_size = 0; 703 int total_data_size = 0, total_size = 0;
@@ -716,7 +714,7 @@ static int btrfs_batch_insert_items(struct btrfs_root *root,
716 BUG_ON(!path->nodes[0]); 714 BUG_ON(!path->nodes[0]);
717 715
718 leaf = path->nodes[0]; 716 leaf = path->nodes[0];
719 free_space = btrfs_leaf_free_space(root, leaf); 717 free_space = btrfs_leaf_free_space(fs_info, leaf);
720 INIT_LIST_HEAD(&head); 718 INIT_LIST_HEAD(&head);
721 719
722 next = item; 720 next = item;
@@ -789,7 +787,7 @@ static int btrfs_batch_insert_items(struct btrfs_root *root,
789 curr->data_len); 787 curr->data_len);
790 slot++; 788 slot++;
791 789
792 btrfs_delayed_item_release_metadata(root, curr); 790 btrfs_delayed_item_release_metadata(fs_info, curr);
793 791
794 list_del(&curr->tree_list); 792 list_del(&curr->tree_list);
795 btrfs_release_delayed_item(curr); 793 btrfs_release_delayed_item(curr);
@@ -811,6 +809,7 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
811 struct btrfs_path *path, 809 struct btrfs_path *path,
812 struct btrfs_delayed_item *delayed_item) 810 struct btrfs_delayed_item *delayed_item)
813{ 811{
812 struct btrfs_fs_info *fs_info = root->fs_info;
814 struct extent_buffer *leaf; 813 struct extent_buffer *leaf;
815 char *ptr; 814 char *ptr;
816 int ret; 815 int ret;
@@ -828,7 +827,7 @@ static int btrfs_insert_delayed_item(struct btrfs_trans_handle *trans,
828 delayed_item->data_len); 827 delayed_item->data_len);
829 btrfs_mark_buffer_dirty(leaf); 828 btrfs_mark_buffer_dirty(leaf);
830 829
831 btrfs_delayed_item_release_metadata(root, delayed_item); 830 btrfs_delayed_item_release_metadata(fs_info, delayed_item);
832 return 0; 831 return 0;
833} 832}
834 833
@@ -880,6 +879,7 @@ static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
880 struct btrfs_path *path, 879 struct btrfs_path *path,
881 struct btrfs_delayed_item *item) 880 struct btrfs_delayed_item *item)
882{ 881{
882 struct btrfs_fs_info *fs_info = root->fs_info;
883 struct btrfs_delayed_item *curr, *next; 883 struct btrfs_delayed_item *curr, *next;
884 struct extent_buffer *leaf; 884 struct extent_buffer *leaf;
885 struct btrfs_key key; 885 struct btrfs_key key;
@@ -929,7 +929,7 @@ static int btrfs_batch_delete_items(struct btrfs_trans_handle *trans,
929 goto out; 929 goto out;
930 930
931 list_for_each_entry_safe(curr, next, &head, tree_list) { 931 list_for_each_entry_safe(curr, next, &head, tree_list) {
932 btrfs_delayed_item_release_metadata(root, curr); 932 btrfs_delayed_item_release_metadata(fs_info, curr);
933 list_del(&curr->tree_list); 933 list_del(&curr->tree_list);
934 btrfs_release_delayed_item(curr); 934 btrfs_release_delayed_item(curr);
935 } 935 }
@@ -1015,6 +1015,7 @@ static int __btrfs_update_delayed_inode(struct btrfs_trans_handle *trans,
1015 struct btrfs_path *path, 1015 struct btrfs_path *path,
1016 struct btrfs_delayed_node *node) 1016 struct btrfs_delayed_node *node)
1017{ 1017{
1018 struct btrfs_fs_info *fs_info = root->fs_info;
1018 struct btrfs_key key; 1019 struct btrfs_key key;
1019 struct btrfs_inode_item *inode_item; 1020 struct btrfs_inode_item *inode_item;
1020 struct extent_buffer *leaf; 1021 struct extent_buffer *leaf;
@@ -1071,7 +1072,7 @@ out:
1071no_iref: 1072no_iref:
1072 btrfs_release_path(path); 1073 btrfs_release_path(path);
1073err_out: 1074err_out:
1074 btrfs_delayed_inode_release_metadata(root, node); 1075 btrfs_delayed_inode_release_metadata(fs_info, node);
1075 btrfs_release_delayed_inode(node); 1076 btrfs_release_delayed_inode(node);
1076 1077
1077 return ret; 1078 return ret;
@@ -1136,9 +1137,8 @@ __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1136 * outstanding delayed items cleaned up. 1137 * outstanding delayed items cleaned up.
1137 */ 1138 */
1138static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, 1139static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1139 struct btrfs_root *root, int nr) 1140 struct btrfs_fs_info *fs_info, int nr)
1140{ 1141{
1141 struct btrfs_fs_info *fs_info = root->fs_info;
1142 struct btrfs_delayed_root *delayed_root; 1142 struct btrfs_delayed_root *delayed_root;
1143 struct btrfs_delayed_node *curr_node, *prev_node; 1143 struct btrfs_delayed_node *curr_node, *prev_node;
1144 struct btrfs_path *path; 1144 struct btrfs_path *path;
@@ -1184,15 +1184,15 @@ static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1184} 1184}
1185 1185
1186int btrfs_run_delayed_items(struct btrfs_trans_handle *trans, 1186int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1187 struct btrfs_root *root) 1187 struct btrfs_fs_info *fs_info)
1188{ 1188{
1189 return __btrfs_run_delayed_items(trans, root, -1); 1189 return __btrfs_run_delayed_items(trans, fs_info, -1);
1190} 1190}
1191 1191
1192int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, 1192int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
1193 struct btrfs_root *root, int nr) 1193 struct btrfs_fs_info *fs_info, int nr)
1194{ 1194{
1195 return __btrfs_run_delayed_items(trans, root, nr); 1195 return __btrfs_run_delayed_items(trans, fs_info, nr);
1196} 1196}
1197 1197
1198int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, 1198int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
@@ -1235,6 +1235,7 @@ int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1235 1235
1236int btrfs_commit_inode_delayed_inode(struct inode *inode) 1236int btrfs_commit_inode_delayed_inode(struct inode *inode)
1237{ 1237{
1238 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1238 struct btrfs_trans_handle *trans; 1239 struct btrfs_trans_handle *trans;
1239 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode); 1240 struct btrfs_delayed_node *delayed_node = btrfs_get_delayed_node(inode);
1240 struct btrfs_path *path; 1241 struct btrfs_path *path;
@@ -1266,7 +1267,7 @@ int btrfs_commit_inode_delayed_inode(struct inode *inode)
1266 path->leave_spinning = 1; 1267 path->leave_spinning = 1;
1267 1268
1268 block_rsv = trans->block_rsv; 1269 block_rsv = trans->block_rsv;
1269 trans->block_rsv = &delayed_node->root->fs_info->delayed_block_rsv; 1270 trans->block_rsv = &fs_info->delayed_block_rsv;
1270 1271
1271 mutex_lock(&delayed_node->mutex); 1272 mutex_lock(&delayed_node->mutex);
1272 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) 1273 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags))
@@ -1280,7 +1281,7 @@ int btrfs_commit_inode_delayed_inode(struct inode *inode)
1280 trans->block_rsv = block_rsv; 1281 trans->block_rsv = block_rsv;
1281trans_out: 1282trans_out:
1282 btrfs_end_transaction(trans, delayed_node->root); 1283 btrfs_end_transaction(trans, delayed_node->root);
1283 btrfs_btree_balance_dirty(delayed_node->root); 1284 btrfs_btree_balance_dirty(fs_info);
1284out: 1285out:
1285 btrfs_release_delayed_node(delayed_node); 1286 btrfs_release_delayed_node(delayed_node);
1286 1287
@@ -1345,7 +1346,7 @@ again:
1345 1346
1346 trans->block_rsv = block_rsv; 1347 trans->block_rsv = block_rsv;
1347 btrfs_end_transaction(trans, root); 1348 btrfs_end_transaction(trans, root);
1348 btrfs_btree_balance_dirty_nodelay(root); 1349 btrfs_btree_balance_dirty_nodelay(root->fs_info);
1349 1350
1350release_path: 1351release_path:
1351 btrfs_release_path(path); 1352 btrfs_release_path(path);
@@ -1402,12 +1403,9 @@ static int could_end_wait(struct btrfs_delayed_root *delayed_root, int seq)
1402 return 0; 1403 return 0;
1403} 1404}
1404 1405
1405void btrfs_balance_delayed_items(struct btrfs_root *root) 1406void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info)
1406{ 1407{
1407 struct btrfs_delayed_root *delayed_root; 1408 struct btrfs_delayed_root *delayed_root = fs_info->delayed_root;
1408 struct btrfs_fs_info *fs_info = root->fs_info;
1409
1410 delayed_root = fs_info->delayed_root;
1411 1409
1412 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND) 1410 if (atomic_read(&delayed_root->items) < BTRFS_DELAYED_BACKGROUND)
1413 return; 1411 return;
@@ -1432,8 +1430,9 @@ void btrfs_balance_delayed_items(struct btrfs_root *root)
1432 1430
1433/* Will return 0 or -ENOMEM */ 1431/* Will return 0 or -ENOMEM */
1434int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, 1432int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1435 struct btrfs_root *root, const char *name, 1433 struct btrfs_fs_info *fs_info,
1436 int name_len, struct inode *dir, 1434 const char *name, int name_len,
1435 struct inode *dir,
1437 struct btrfs_disk_key *disk_key, u8 type, 1436 struct btrfs_disk_key *disk_key, u8 type,
1438 u64 index) 1437 u64 index)
1439{ 1438{
@@ -1464,7 +1463,7 @@ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1464 btrfs_set_stack_dir_type(dir_item, type); 1463 btrfs_set_stack_dir_type(dir_item, type);
1465 memcpy((char *)(dir_item + 1), name, name_len); 1464 memcpy((char *)(dir_item + 1), name, name_len);
1466 1465
1467 ret = btrfs_delayed_item_reserve_metadata(trans, root, delayed_item); 1466 ret = btrfs_delayed_item_reserve_metadata(trans, fs_info, delayed_item);
1468 /* 1467 /*
1469 * we have reserved enough space when we start a new transaction, 1468 * we have reserved enough space when we start a new transaction,
1470 * so reserving metadata failure is impossible 1469 * so reserving metadata failure is impossible
@@ -1475,7 +1474,7 @@ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
1475 mutex_lock(&delayed_node->mutex); 1474 mutex_lock(&delayed_node->mutex);
1476 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item); 1475 ret = __btrfs_add_delayed_insertion_item(delayed_node, delayed_item);
1477 if (unlikely(ret)) { 1476 if (unlikely(ret)) {
1478 btrfs_err(root->fs_info, 1477 btrfs_err(fs_info,
1479 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", 1478 "err add delayed dir index item(name: %.*s) into the insertion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1480 name_len, name, delayed_node->root->objectid, 1479 name_len, name, delayed_node->root->objectid,
1481 delayed_node->inode_id, ret); 1480 delayed_node->inode_id, ret);
@@ -1488,7 +1487,7 @@ release_node:
1488 return ret; 1487 return ret;
1489} 1488}
1490 1489
1491static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root, 1490static int btrfs_delete_delayed_insertion_item(struct btrfs_fs_info *fs_info,
1492 struct btrfs_delayed_node *node, 1491 struct btrfs_delayed_node *node,
1493 struct btrfs_key *key) 1492 struct btrfs_key *key)
1494{ 1493{
@@ -1501,15 +1500,15 @@ static int btrfs_delete_delayed_insertion_item(struct btrfs_root *root,
1501 return 1; 1500 return 1;
1502 } 1501 }
1503 1502
1504 btrfs_delayed_item_release_metadata(root, item); 1503 btrfs_delayed_item_release_metadata(fs_info, item);
1505 btrfs_release_delayed_item(item); 1504 btrfs_release_delayed_item(item);
1506 mutex_unlock(&node->mutex); 1505 mutex_unlock(&node->mutex);
1507 return 0; 1506 return 0;
1508} 1507}
1509 1508
1510int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, 1509int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1511 struct btrfs_root *root, struct inode *dir, 1510 struct btrfs_fs_info *fs_info,
1512 u64 index) 1511 struct inode *dir, u64 index)
1513{ 1512{
1514 struct btrfs_delayed_node *node; 1513 struct btrfs_delayed_node *node;
1515 struct btrfs_delayed_item *item; 1514 struct btrfs_delayed_item *item;
@@ -1524,7 +1523,7 @@ int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1524 item_key.type = BTRFS_DIR_INDEX_KEY; 1523 item_key.type = BTRFS_DIR_INDEX_KEY;
1525 item_key.offset = index; 1524 item_key.offset = index;
1526 1525
1527 ret = btrfs_delete_delayed_insertion_item(root, node, &item_key); 1526 ret = btrfs_delete_delayed_insertion_item(fs_info, node, &item_key);
1528 if (!ret) 1527 if (!ret)
1529 goto end; 1528 goto end;
1530 1529
@@ -1536,7 +1535,7 @@ int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1536 1535
1537 item->key = item_key; 1536 item->key = item_key;
1538 1537
1539 ret = btrfs_delayed_item_reserve_metadata(trans, root, item); 1538 ret = btrfs_delayed_item_reserve_metadata(trans, fs_info, item);
1540 /* 1539 /*
1541 * we have reserved enough space when we start a new transaction, 1540 * we have reserved enough space when we start a new transaction,
1542 * so reserving metadata failure is impossible. 1541 * so reserving metadata failure is impossible.
@@ -1546,7 +1545,7 @@ int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
1546 mutex_lock(&node->mutex); 1545 mutex_lock(&node->mutex);
1547 ret = __btrfs_add_delayed_deletion_item(node, item); 1546 ret = __btrfs_add_delayed_deletion_item(node, item);
1548 if (unlikely(ret)) { 1547 if (unlikely(ret)) {
1549 btrfs_err(root->fs_info, 1548 btrfs_err(fs_info,
1550 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)", 1549 "err add delayed dir index item(index: %llu) into the deletion tree of the delayed node(root id: %llu, inode id: %llu, errno: %d)",
1551 index, node->root->objectid, node->inode_id, ret); 1550 index, node->root->objectid, node->inode_id, ret);
1552 BUG(); 1551 BUG();
@@ -1902,12 +1901,13 @@ release_node:
1902static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node) 1901static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1903{ 1902{
1904 struct btrfs_root *root = delayed_node->root; 1903 struct btrfs_root *root = delayed_node->root;
1904 struct btrfs_fs_info *fs_info = root->fs_info;
1905 struct btrfs_delayed_item *curr_item, *prev_item; 1905 struct btrfs_delayed_item *curr_item, *prev_item;
1906 1906
1907 mutex_lock(&delayed_node->mutex); 1907 mutex_lock(&delayed_node->mutex);
1908 curr_item = __btrfs_first_delayed_insertion_item(delayed_node); 1908 curr_item = __btrfs_first_delayed_insertion_item(delayed_node);
1909 while (curr_item) { 1909 while (curr_item) {
1910 btrfs_delayed_item_release_metadata(root, curr_item); 1910 btrfs_delayed_item_release_metadata(fs_info, curr_item);
1911 prev_item = curr_item; 1911 prev_item = curr_item;
1912 curr_item = __btrfs_next_delayed_item(prev_item); 1912 curr_item = __btrfs_next_delayed_item(prev_item);
1913 btrfs_release_delayed_item(prev_item); 1913 btrfs_release_delayed_item(prev_item);
@@ -1915,7 +1915,7 @@ static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1915 1915
1916 curr_item = __btrfs_first_delayed_deletion_item(delayed_node); 1916 curr_item = __btrfs_first_delayed_deletion_item(delayed_node);
1917 while (curr_item) { 1917 while (curr_item) {
1918 btrfs_delayed_item_release_metadata(root, curr_item); 1918 btrfs_delayed_item_release_metadata(fs_info, curr_item);
1919 prev_item = curr_item; 1919 prev_item = curr_item;
1920 curr_item = __btrfs_next_delayed_item(prev_item); 1920 curr_item = __btrfs_next_delayed_item(prev_item);
1921 btrfs_release_delayed_item(prev_item); 1921 btrfs_release_delayed_item(prev_item);
@@ -1925,7 +1925,7 @@ static void __btrfs_kill_delayed_node(struct btrfs_delayed_node *delayed_node)
1925 btrfs_release_delayed_iref(delayed_node); 1925 btrfs_release_delayed_iref(delayed_node);
1926 1926
1927 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) { 1927 if (test_bit(BTRFS_DELAYED_NODE_INODE_DIRTY, &delayed_node->flags)) {
1928 btrfs_delayed_inode_release_metadata(root, delayed_node); 1928 btrfs_delayed_inode_release_metadata(fs_info, delayed_node);
1929 btrfs_release_delayed_inode(delayed_node); 1929 btrfs_release_delayed_inode(delayed_node);
1930 } 1930 }
1931 mutex_unlock(&delayed_node->mutex); 1931 mutex_unlock(&delayed_node->mutex);
diff --git a/fs/btrfs/delayed-inode.h b/fs/btrfs/delayed-inode.h
index 7320d72f7b9c..8a2bf5e3e4cf 100644
--- a/fs/btrfs/delayed-inode.h
+++ b/fs/btrfs/delayed-inode.h
@@ -99,23 +99,24 @@ static inline void btrfs_init_delayed_root(
99} 99}
100 100
101int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, 101int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,
102 struct btrfs_root *root, const char *name, 102 struct btrfs_fs_info *fs_info,
103 int name_len, struct inode *dir, 103 const char *name, int name_len,
104 struct inode *dir,
104 struct btrfs_disk_key *disk_key, u8 type, 105 struct btrfs_disk_key *disk_key, u8 type,
105 u64 index); 106 u64 index);
106 107
107int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans, 108int btrfs_delete_delayed_dir_index(struct btrfs_trans_handle *trans,
108 struct btrfs_root *root, struct inode *dir, 109 struct btrfs_fs_info *fs_info,
109 u64 index); 110 struct inode *dir, u64 index);
110 111
111int btrfs_inode_delayed_dir_index_count(struct inode *inode); 112int btrfs_inode_delayed_dir_index_count(struct inode *inode);
112 113
113int btrfs_run_delayed_items(struct btrfs_trans_handle *trans, 114int btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
114 struct btrfs_root *root); 115 struct btrfs_fs_info *fs_info);
115int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans, 116int btrfs_run_delayed_items_nr(struct btrfs_trans_handle *trans,
116 struct btrfs_root *root, int nr); 117 struct btrfs_fs_info *fs_info, int nr);
117 118
118void btrfs_balance_delayed_items(struct btrfs_root *root); 119void btrfs_balance_delayed_items(struct btrfs_fs_info *fs_info);
119 120
120int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans, 121int btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
121 struct inode *inode); 122 struct inode *inode);
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index c6558ed93301..1b545885f7b1 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -304,11 +304,11 @@ void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info)
304 dev_replace->cursor_left_last_write_of_item; 304 dev_replace->cursor_left_last_write_of_item;
305} 305}
306 306
307int btrfs_dev_replace_start(struct btrfs_root *root, char *tgtdev_name, 307int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, char *tgtdev_name,
308 u64 srcdevid, char *srcdev_name, int read_src) 308 u64 srcdevid, char *srcdev_name, int read_src)
309{ 309{
310 struct btrfs_root *root = fs_info->dev_root;
310 struct btrfs_trans_handle *trans; 311 struct btrfs_trans_handle *trans;
311 struct btrfs_fs_info *fs_info = root->fs_info;
312 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace; 312 struct btrfs_dev_replace *dev_replace = &fs_info->dev_replace;
313 int ret; 313 int ret;
314 struct btrfs_device *tgt_device = NULL; 314 struct btrfs_device *tgt_device = NULL;
@@ -316,14 +316,14 @@ int btrfs_dev_replace_start(struct btrfs_root *root, char *tgtdev_name,
316 316
317 /* the disk copy procedure reuses the scrub code */ 317 /* the disk copy procedure reuses the scrub code */
318 mutex_lock(&fs_info->volume_mutex); 318 mutex_lock(&fs_info->volume_mutex);
319 ret = btrfs_find_device_by_devspec(root, srcdevid, 319 ret = btrfs_find_device_by_devspec(fs_info, srcdevid,
320 srcdev_name, &src_device); 320 srcdev_name, &src_device);
321 if (ret) { 321 if (ret) {
322 mutex_unlock(&fs_info->volume_mutex); 322 mutex_unlock(&fs_info->volume_mutex);
323 return ret; 323 return ret;
324 } 324 }
325 325
326 ret = btrfs_init_dev_replace_tgtdev(root, tgtdev_name, 326 ret = btrfs_init_dev_replace_tgtdev(fs_info, tgtdev_name,
327 src_device, &tgt_device); 327 src_device, &tgt_device);
328 mutex_unlock(&fs_info->volume_mutex); 328 mutex_unlock(&fs_info->volume_mutex);
329 if (ret) 329 if (ret)
@@ -422,7 +422,7 @@ leave:
422 return ret; 422 return ret;
423} 423}
424 424
425int btrfs_dev_replace_by_ioctl(struct btrfs_root *root, 425int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
426 struct btrfs_ioctl_dev_replace_args *args) 426 struct btrfs_ioctl_dev_replace_args *args)
427{ 427{
428 int ret; 428 int ret;
@@ -439,7 +439,7 @@ int btrfs_dev_replace_by_ioctl(struct btrfs_root *root,
439 args->start.tgtdev_name[0] == '\0') 439 args->start.tgtdev_name[0] == '\0')
440 return -EINVAL; 440 return -EINVAL;
441 441
442 ret = btrfs_dev_replace_start(root, args->start.tgtdev_name, 442 ret = btrfs_dev_replace_start(fs_info, args->start.tgtdev_name,
443 args->start.srcdevid, 443 args->start.srcdevid,
444 args->start.srcdev_name, 444 args->start.srcdev_name,
445 args->start.cont_reading_from_srcdev_mode); 445 args->start.cont_reading_from_srcdev_mode);
diff --git a/fs/btrfs/dev-replace.h b/fs/btrfs/dev-replace.h
index e922b42d91df..54ea12bda15b 100644
--- a/fs/btrfs/dev-replace.h
+++ b/fs/btrfs/dev-replace.h
@@ -25,9 +25,9 @@ int btrfs_init_dev_replace(struct btrfs_fs_info *fs_info);
25int btrfs_run_dev_replace(struct btrfs_trans_handle *trans, 25int btrfs_run_dev_replace(struct btrfs_trans_handle *trans,
26 struct btrfs_fs_info *fs_info); 26 struct btrfs_fs_info *fs_info);
27void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info); 27void btrfs_after_dev_replace_commit(struct btrfs_fs_info *fs_info);
28int btrfs_dev_replace_by_ioctl(struct btrfs_root *root, 28int btrfs_dev_replace_by_ioctl(struct btrfs_fs_info *fs_info,
29 struct btrfs_ioctl_dev_replace_args *args); 29 struct btrfs_ioctl_dev_replace_args *args);
30int btrfs_dev_replace_start(struct btrfs_root *root, char *tgtdev_name, 30int btrfs_dev_replace_start(struct btrfs_fs_info *fs_info, char *tgtdev_name,
31 u64 srcdevid, char *srcdev_name, int read_src); 31 u64 srcdevid, char *srcdev_name, int read_src);
32void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info, 32void btrfs_dev_replace_status(struct btrfs_fs_info *fs_info,
33 struct btrfs_ioctl_dev_replace_args *args); 33 struct btrfs_ioctl_dev_replace_args *args);
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index 128f8ab20d41..b039fe0c751a 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -38,6 +38,7 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
38 const char *name, 38 const char *name,
39 int name_len) 39 int name_len)
40{ 40{
41 struct btrfs_fs_info *fs_info = root->fs_info;
41 int ret; 42 int ret;
42 char *ptr; 43 char *ptr;
43 struct btrfs_item *item; 44 struct btrfs_item *item;
@@ -46,10 +47,10 @@ static struct btrfs_dir_item *insert_with_overflow(struct btrfs_trans_handle
46 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); 47 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
47 if (ret == -EEXIST) { 48 if (ret == -EEXIST) {
48 struct btrfs_dir_item *di; 49 struct btrfs_dir_item *di;
49 di = btrfs_match_dir_item_name(root, path, name, name_len); 50 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
50 if (di) 51 if (di)
51 return ERR_PTR(-EEXIST); 52 return ERR_PTR(-EEXIST);
52 btrfs_extend_item(root, path, data_size); 53 btrfs_extend_item(fs_info, path, data_size);
53 } else if (ret < 0) 54 } else if (ret < 0)
54 return ERR_PTR(ret); 55 return ERR_PTR(ret);
55 WARN_ON(ret > 0); 56 WARN_ON(ret > 0);
@@ -172,8 +173,9 @@ second_insert:
172 } 173 }
173 btrfs_release_path(path); 174 btrfs_release_path(path);
174 175
175 ret2 = btrfs_insert_delayed_dir_index(trans, root, name, name_len, dir, 176 ret2 = btrfs_insert_delayed_dir_index(trans, root->fs_info, name,
176 &disk_key, type, index); 177 name_len, dir, &disk_key, type,
178 index);
177out_free: 179out_free:
178 btrfs_free_path(path); 180 btrfs_free_path(path);
179 if (ret) 181 if (ret)
@@ -210,7 +212,7 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
210 if (ret > 0) 212 if (ret > 0)
211 return NULL; 213 return NULL;
212 214
213 return btrfs_match_dir_item_name(root, path, name, name_len); 215 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
214} 216}
215 217
216int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir, 218int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
@@ -246,7 +248,7 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
246 } 248 }
247 249
248 /* we found an item, look for our name in the item */ 250 /* we found an item, look for our name in the item */
249 di = btrfs_match_dir_item_name(root, path, name, name_len); 251 di = btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
250 if (di) { 252 if (di) {
251 /* our exact name was found */ 253 /* our exact name was found */
252 ret = -EEXIST; 254 ret = -EEXIST;
@@ -301,7 +303,7 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
301 return ERR_PTR(ret); 303 return ERR_PTR(ret);
302 if (ret > 0) 304 if (ret > 0)
303 return ERR_PTR(-ENOENT); 305 return ERR_PTR(-ENOENT);
304 return btrfs_match_dir_item_name(root, path, name, name_len); 306 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
305} 307}
306 308
307struct btrfs_dir_item * 309struct btrfs_dir_item *
@@ -342,7 +344,8 @@ btrfs_search_dir_index_item(struct btrfs_root *root,
342 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY) 344 if (key.objectid != dirid || key.type != BTRFS_DIR_INDEX_KEY)
343 break; 345 break;
344 346
345 di = btrfs_match_dir_item_name(root, path, name, name_len); 347 di = btrfs_match_dir_item_name(root->fs_info, path,
348 name, name_len);
346 if (di) 349 if (di)
347 return di; 350 return di;
348 351
@@ -371,7 +374,7 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
371 if (ret > 0) 374 if (ret > 0)
372 return NULL; 375 return NULL;
373 376
374 return btrfs_match_dir_item_name(root, path, name, name_len); 377 return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
375} 378}
376 379
377/* 380/*
@@ -379,7 +382,7 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
379 * this walks through all the entries in a dir item and finds one 382 * this walks through all the entries in a dir item and finds one
380 * for a specific name. 383 * for a specific name.
381 */ 384 */
382struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root, 385struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
383 struct btrfs_path *path, 386 struct btrfs_path *path,
384 const char *name, int name_len) 387 const char *name, int name_len)
385{ 388{
@@ -392,7 +395,7 @@ struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_root *root,
392 395
393 leaf = path->nodes[0]; 396 leaf = path->nodes[0];
394 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item); 397 dir_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dir_item);
395 if (verify_dir_item(root, leaf, dir_item)) 398 if (verify_dir_item(fs_info, leaf, dir_item))
396 return NULL; 399 return NULL;
397 400
398 total_len = btrfs_item_size_nr(leaf, path->slots[0]); 401 total_len = btrfs_item_size_nr(leaf, path->slots[0]);
@@ -442,16 +445,16 @@ int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
442 start = btrfs_item_ptr_offset(leaf, path->slots[0]); 445 start = btrfs_item_ptr_offset(leaf, path->slots[0]);
443 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, 446 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
444 item_len - (ptr + sub_item_len - start)); 447 item_len - (ptr + sub_item_len - start));
445 btrfs_truncate_item(root, path, item_len - sub_item_len, 1); 448 btrfs_truncate_item(root->fs_info, path,
449 item_len - sub_item_len, 1);
446 } 450 }
447 return ret; 451 return ret;
448} 452}
449 453
450int verify_dir_item(struct btrfs_root *root, 454int verify_dir_item(struct btrfs_fs_info *fs_info,
451 struct extent_buffer *leaf, 455 struct extent_buffer *leaf,
452 struct btrfs_dir_item *dir_item) 456 struct btrfs_dir_item *dir_item)
453{ 457{
454 struct btrfs_fs_info *fs_info = root->fs_info;
455 u16 namelen = BTRFS_NAME_LEN; 458 u16 namelen = BTRFS_NAME_LEN;
456 u8 type = btrfs_dir_type(leaf, dir_item); 459 u8 type = btrfs_dir_type(leaf, dir_item);
457 460
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 5f7d283933b4..3ce36b526ebe 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -68,15 +68,15 @@ static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
68 int read_only); 68 int read_only);
69static void btrfs_destroy_ordered_extents(struct btrfs_root *root); 69static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
70static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 70static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
71 struct btrfs_root *root); 71 struct btrfs_fs_info *fs_info);
72static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root); 72static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
73static int btrfs_destroy_marked_extents(struct btrfs_root *root, 73static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
74 struct extent_io_tree *dirty_pages, 74 struct extent_io_tree *dirty_pages,
75 int mark); 75 int mark);
76static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 76static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
77 struct extent_io_tree *pinned_extents); 77 struct extent_io_tree *pinned_extents);
78static int btrfs_cleanup_transaction(struct btrfs_root *root); 78static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
79static void btrfs_error_commit_super(struct btrfs_root *root); 79static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
80 80
81/* 81/*
82 * btrfs_end_io_wq structs are used to do processing in task context when an IO 82 * btrfs_end_io_wq structs are used to do processing in task context when an IO
@@ -440,11 +440,10 @@ static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
440 * helper to read a given tree block, doing retries as required when 440 * helper to read a given tree block, doing retries as required when
441 * the checksums don't match and we have alternate mirrors to try. 441 * the checksums don't match and we have alternate mirrors to try.
442 */ 442 */
443static int btree_read_extent_buffer_pages(struct btrfs_root *root, 443static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info,
444 struct extent_buffer *eb, 444 struct extent_buffer *eb,
445 u64 parent_transid) 445 u64 parent_transid)
446{ 446{
447 struct btrfs_fs_info *fs_info = root->fs_info;
448 struct extent_io_tree *io_tree; 447 struct extent_io_tree *io_tree;
449 int failed = 0; 448 int failed = 0;
450 int ret; 449 int ret;
@@ -492,7 +491,7 @@ static int btree_read_extent_buffer_pages(struct btrfs_root *root,
492 } 491 }
493 492
494 if (failed && !ret && failed_mirror) 493 if (failed && !ret && failed_mirror)
495 repair_eb_io_failure(root, eb, failed_mirror); 494 repair_eb_io_failure(fs_info, eb, failed_mirror);
496 495
497 return ret; 496 return ret;
498} 497}
@@ -983,7 +982,7 @@ static int __btree_submit_bio_done(struct inode *inode, struct bio *bio,
983 * when we're called for a write, we're already in the async 982 * when we're called for a write, we're already in the async
984 * submission context. Just jump into btrfs_map_bio 983 * submission context. Just jump into btrfs_map_bio
985 */ 984 */
986 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 1); 985 ret = btrfs_map_bio(btrfs_sb(inode->i_sb), bio, mirror_num, 1);
987 if (ret) { 986 if (ret) {
988 bio->bi_error = ret; 987 bio->bi_error = ret;
989 bio_endio(bio); 988 bio_endio(bio);
@@ -1019,12 +1018,12 @@ static int btree_submit_bio_hook(struct inode *inode, struct bio *bio,
1019 BTRFS_WQ_ENDIO_METADATA); 1018 BTRFS_WQ_ENDIO_METADATA);
1020 if (ret) 1019 if (ret)
1021 goto out_w_error; 1020 goto out_w_error;
1022 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 0); 1021 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
1023 } else if (!async) { 1022 } else if (!async) {
1024 ret = btree_csum_one_bio(bio); 1023 ret = btree_csum_one_bio(bio);
1025 if (ret) 1024 if (ret)
1026 goto out_w_error; 1025 goto out_w_error;
1027 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 0); 1026 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
1028 } else { 1027 } else {
1029 /* 1028 /*
1030 * kthread helpers are used to submit writes so that 1029 * kthread helpers are used to submit writes so that
@@ -1148,12 +1147,12 @@ static const struct address_space_operations btree_aops = {
1148 .set_page_dirty = btree_set_page_dirty, 1147 .set_page_dirty = btree_set_page_dirty,
1149}; 1148};
1150 1149
1151void readahead_tree_block(struct btrfs_root *root, u64 bytenr) 1150void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr)
1152{ 1151{
1153 struct extent_buffer *buf = NULL; 1152 struct extent_buffer *buf = NULL;
1154 struct inode *btree_inode = root->fs_info->btree_inode; 1153 struct inode *btree_inode = fs_info->btree_inode;
1155 1154
1156 buf = btrfs_find_create_tree_block(root, bytenr); 1155 buf = btrfs_find_create_tree_block(fs_info, bytenr);
1157 if (IS_ERR(buf)) 1156 if (IS_ERR(buf))
1158 return; 1157 return;
1159 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, 1158 read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
@@ -1161,15 +1160,15 @@ void readahead_tree_block(struct btrfs_root *root, u64 bytenr)
1161 free_extent_buffer(buf); 1160 free_extent_buffer(buf);
1162} 1161}
1163 1162
1164int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, 1163int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
1165 int mirror_num, struct extent_buffer **eb) 1164 int mirror_num, struct extent_buffer **eb)
1166{ 1165{
1167 struct extent_buffer *buf = NULL; 1166 struct extent_buffer *buf = NULL;
1168 struct inode *btree_inode = root->fs_info->btree_inode; 1167 struct inode *btree_inode = fs_info->btree_inode;
1169 struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree; 1168 struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
1170 int ret; 1169 int ret;
1171 1170
1172 buf = btrfs_find_create_tree_block(root, bytenr); 1171 buf = btrfs_find_create_tree_block(fs_info, bytenr);
1173 if (IS_ERR(buf)) 1172 if (IS_ERR(buf))
1174 return 0; 1173 return 0;
1175 1174
@@ -1193,11 +1192,10 @@ int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr,
1193 return 0; 1192 return 0;
1194} 1193}
1195 1194
1196struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 1195struct extent_buffer *btrfs_find_create_tree_block(
1197 u64 bytenr) 1196 struct btrfs_fs_info *fs_info,
1197 u64 bytenr)
1198{ 1198{
1199 struct btrfs_fs_info *fs_info = root->fs_info;
1200
1201 if (btrfs_is_testing(fs_info)) 1199 if (btrfs_is_testing(fs_info))
1202 return alloc_test_extent_buffer(fs_info, bytenr); 1200 return alloc_test_extent_buffer(fs_info, bytenr);
1203 return alloc_extent_buffer(fs_info, bytenr); 1201 return alloc_extent_buffer(fs_info, bytenr);
@@ -1216,17 +1214,17 @@ int btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
1216 buf->start, buf->start + buf->len - 1); 1214 buf->start, buf->start + buf->len - 1);
1217} 1215}
1218 1216
1219struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 1217struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
1220 u64 parent_transid) 1218 u64 parent_transid)
1221{ 1219{
1222 struct extent_buffer *buf = NULL; 1220 struct extent_buffer *buf = NULL;
1223 int ret; 1221 int ret;
1224 1222
1225 buf = btrfs_find_create_tree_block(root, bytenr); 1223 buf = btrfs_find_create_tree_block(fs_info, bytenr);
1226 if (IS_ERR(buf)) 1224 if (IS_ERR(buf))
1227 return buf; 1225 return buf;
1228 1226
1229 ret = btree_read_extent_buffer_pages(root, buf, parent_transid); 1227 ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid);
1230 if (ret) { 1228 if (ret) {
1231 free_extent_buffer(buf); 1229 free_extent_buffer(buf);
1232 return ERR_PTR(ret); 1230 return ERR_PTR(ret);
@@ -1578,7 +1576,8 @@ static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
1578 } 1576 }
1579 1577
1580 generation = btrfs_root_generation(&root->root_item); 1578 generation = btrfs_root_generation(&root->root_item);
1581 root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item), 1579 root->node = read_tree_block(fs_info,
1580 btrfs_root_bytenr(&root->root_item),
1582 generation); 1581 generation);
1583 if (IS_ERR(root->node)) { 1582 if (IS_ERR(root->node)) {
1584 ret = PTR_ERR(root->node); 1583 ret = PTR_ERR(root->node);
@@ -1841,7 +1840,7 @@ static int cleaner_kthread(void *arg)
1841 again = 0; 1840 again = 0;
1842 1841
1843 /* Make the cleaner go to sleep early. */ 1842 /* Make the cleaner go to sleep early. */
1844 if (btrfs_need_cleaner_sleep(root)) 1843 if (btrfs_need_cleaner_sleep(fs_info))
1845 goto sleep; 1844 goto sleep;
1846 1845
1847 /* 1846 /*
@@ -1858,13 +1857,13 @@ static int cleaner_kthread(void *arg)
1858 * Avoid the problem that we change the status of the fs 1857 * Avoid the problem that we change the status of the fs
1859 * during the above check and trylock. 1858 * during the above check and trylock.
1860 */ 1859 */
1861 if (btrfs_need_cleaner_sleep(root)) { 1860 if (btrfs_need_cleaner_sleep(fs_info)) {
1862 mutex_unlock(&fs_info->cleaner_mutex); 1861 mutex_unlock(&fs_info->cleaner_mutex);
1863 goto sleep; 1862 goto sleep;
1864 } 1863 }
1865 1864
1866 mutex_lock(&fs_info->cleaner_delayed_iput_mutex); 1865 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
1867 btrfs_run_delayed_iputs(root); 1866 btrfs_run_delayed_iputs(fs_info);
1868 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); 1867 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
1869 1868
1870 again = btrfs_clean_one_deleted_snapshot(root); 1869 again = btrfs_clean_one_deleted_snapshot(root);
@@ -1976,7 +1975,7 @@ sleep:
1976 1975
1977 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 1976 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
1978 &fs_info->fs_state))) 1977 &fs_info->fs_state)))
1979 btrfs_cleanup_transaction(root); 1978 btrfs_cleanup_transaction(fs_info);
1980 set_current_state(TASK_INTERRUPTIBLE); 1979 set_current_state(TASK_INTERRUPTIBLE);
1981 if (!kthread_should_stop() && 1980 if (!kthread_should_stop() &&
1982 (!btrfs_transaction_blocked(fs_info) || 1981 (!btrfs_transaction_blocked(fs_info) ||
@@ -2266,8 +2265,7 @@ void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
2266 2265
2267 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) { 2266 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
2268 btrfs_free_log_root_tree(NULL, fs_info); 2267 btrfs_free_log_root_tree(NULL, fs_info);
2269 btrfs_destroy_pinned_extent(fs_info->tree_root, 2268 btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
2270 fs_info->pinned_extents);
2271 } 2269 }
2272} 2270}
2273 2271
@@ -2295,30 +2293,29 @@ static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
2295 2293
2296static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info) 2294static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
2297{ 2295{
2298 fs_info->btree_inode->i_ino = BTRFS_BTREE_INODE_OBJECTID; 2296 struct inode *inode = fs_info->btree_inode;
2299 set_nlink(fs_info->btree_inode, 1); 2297
2298 inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
2299 set_nlink(inode, 1);
2300 /* 2300 /*
2301 * we set the i_size on the btree inode to the max possible int. 2301 * we set the i_size on the btree inode to the max possible int.
2302 * the real end of the address space is determined by all of 2302 * the real end of the address space is determined by all of
2303 * the devices in the system 2303 * the devices in the system
2304 */ 2304 */
2305 fs_info->btree_inode->i_size = OFFSET_MAX; 2305 inode->i_size = OFFSET_MAX;
2306 fs_info->btree_inode->i_mapping->a_ops = &btree_aops; 2306 inode->i_mapping->a_ops = &btree_aops;
2307 2307
2308 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 2308 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
2309 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 2309 extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode->i_mapping);
2310 fs_info->btree_inode->i_mapping); 2310 BTRFS_I(inode)->io_tree.track_uptodate = 0;
2311 BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0; 2311 extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
2312 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree);
2313 2312
2314 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 2313 BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops;
2315 2314
2316 BTRFS_I(fs_info->btree_inode)->root = fs_info->tree_root; 2315 BTRFS_I(inode)->root = fs_info->tree_root;
2317 memset(&BTRFS_I(fs_info->btree_inode)->location, 0, 2316 memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key));
2318 sizeof(struct btrfs_key)); 2317 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
2319 set_bit(BTRFS_INODE_DUMMY, 2318 btrfs_insert_inode_hash(inode);
2320 &BTRFS_I(fs_info->btree_inode)->runtime_flags);
2321 btrfs_insert_inode_hash(fs_info->btree_inode);
2322} 2319}
2323 2320
2324static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info) 2321static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
@@ -2439,7 +2436,6 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2439 struct btrfs_fs_devices *fs_devices) 2436 struct btrfs_fs_devices *fs_devices)
2440{ 2437{
2441 int ret; 2438 int ret;
2442 struct btrfs_root *tree_root = fs_info->tree_root;
2443 struct btrfs_root *log_tree_root; 2439 struct btrfs_root *log_tree_root;
2444 struct btrfs_super_block *disk_super = fs_info->super_copy; 2440 struct btrfs_super_block *disk_super = fs_info->super_copy;
2445 u64 bytenr = btrfs_super_log_root(disk_super); 2441 u64 bytenr = btrfs_super_log_root(disk_super);
@@ -2455,8 +2451,8 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2455 2451
2456 __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID); 2452 __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
2457 2453
2458 log_tree_root->node = read_tree_block(tree_root, bytenr, 2454 log_tree_root->node = read_tree_block(fs_info, bytenr,
2459 fs_info->generation + 1); 2455 fs_info->generation + 1);
2460 if (IS_ERR(log_tree_root->node)) { 2456 if (IS_ERR(log_tree_root->node)) {
2461 btrfs_warn(fs_info, "failed to read log tree"); 2457 btrfs_warn(fs_info, "failed to read log tree");
2462 ret = PTR_ERR(log_tree_root->node); 2458 ret = PTR_ERR(log_tree_root->node);
@@ -2819,7 +2815,7 @@ int open_ctree(struct super_block *sb,
2819 */ 2815 */
2820 fs_info->compress_type = BTRFS_COMPRESS_ZLIB; 2816 fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
2821 2817
2822 ret = btrfs_parse_options(tree_root, options, sb->s_flags); 2818 ret = btrfs_parse_options(fs_info, options, sb->s_flags);
2823 if (ret) { 2819 if (ret) {
2824 err = ret; 2820 err = ret;
2825 goto fail_alloc; 2821 goto fail_alloc;
@@ -2920,7 +2916,7 @@ int open_ctree(struct super_block *sb,
2920 2916
2921 __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID); 2917 __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
2922 2918
2923 chunk_root->node = read_tree_block(chunk_root, 2919 chunk_root->node = read_tree_block(fs_info,
2924 btrfs_super_chunk_root(disk_super), 2920 btrfs_super_chunk_root(disk_super),
2925 generation); 2921 generation);
2926 if (IS_ERR(chunk_root->node) || 2922 if (IS_ERR(chunk_root->node) ||
@@ -2957,7 +2953,7 @@ int open_ctree(struct super_block *sb,
2957retry_root_backup: 2953retry_root_backup:
2958 generation = btrfs_super_generation(disk_super); 2954 generation = btrfs_super_generation(disk_super);
2959 2955
2960 tree_root->node = read_tree_block(tree_root, 2956 tree_root->node = read_tree_block(fs_info,
2961 btrfs_super_root(disk_super), 2957 btrfs_super_root(disk_super),
2962 generation); 2958 generation);
2963 if (IS_ERR(tree_root->node) || 2959 if (IS_ERR(tree_root->node) ||
@@ -3081,7 +3077,7 @@ retry_root_backup:
3081 3077
3082#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 3078#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3083 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) { 3079 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
3084 ret = btrfsic_mount(tree_root, fs_devices, 3080 ret = btrfsic_mount(fs_info, fs_devices,
3085 btrfs_test_opt(fs_info, 3081 btrfs_test_opt(fs_info,
3086 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 3082 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
3087 1 : 0, 3083 1 : 0,
@@ -3233,7 +3229,7 @@ fail_qgroup:
3233 btrfs_free_qgroup_config(fs_info); 3229 btrfs_free_qgroup_config(fs_info);
3234fail_trans_kthread: 3230fail_trans_kthread:
3235 kthread_stop(fs_info->transaction_kthread); 3231 kthread_stop(fs_info->transaction_kthread);
3236 btrfs_cleanup_transaction(fs_info->tree_root); 3232 btrfs_cleanup_transaction(fs_info);
3237 btrfs_free_fs_roots(fs_info); 3233 btrfs_free_fs_roots(fs_info);
3238fail_cleaner: 3234fail_cleaner:
3239 kthread_stop(fs_info->cleaner_kthread); 3235 kthread_stop(fs_info->cleaner_kthread);
@@ -3685,9 +3681,8 @@ int btrfs_calc_num_tolerated_disk_barrier_failures(
3685 return num_tolerated_disk_barrier_failures; 3681 return num_tolerated_disk_barrier_failures;
3686} 3682}
3687 3683
3688static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3684static int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
3689{ 3685{
3690 struct btrfs_fs_info *fs_info = root->fs_info;
3691 struct list_head *head; 3686 struct list_head *head;
3692 struct btrfs_device *dev; 3687 struct btrfs_device *dev;
3693 struct btrfs_super_block *sb; 3688 struct btrfs_super_block *sb;
@@ -3781,9 +3776,9 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3781} 3776}
3782 3777
3783int write_ctree_super(struct btrfs_trans_handle *trans, 3778int write_ctree_super(struct btrfs_trans_handle *trans,
3784 struct btrfs_root *root, int max_mirrors) 3779 struct btrfs_fs_info *fs_info, int max_mirrors)
3785{ 3780{
3786 return write_all_supers(root, max_mirrors); 3781 return write_all_supers(fs_info, max_mirrors);
3787} 3782}
3788 3783
3789/* Drop a fs root from the radix tree and free it. */ 3784/* Drop a fs root from the radix tree and free it. */
@@ -3819,7 +3814,7 @@ static void free_fs_root(struct btrfs_root *root)
3819{ 3814{
3820 iput(root->ino_cache_inode); 3815 iput(root->ino_cache_inode);
3821 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree)); 3816 WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
3822 btrfs_free_block_rsv(root, root->orphan_block_rsv); 3817 btrfs_free_block_rsv(root->fs_info, root->orphan_block_rsv);
3823 root->orphan_block_rsv = NULL; 3818 root->orphan_block_rsv = NULL;
3824 if (root->anon_dev) 3819 if (root->anon_dev)
3825 free_anon_bdev(root->anon_dev); 3820 free_anon_bdev(root->anon_dev);
@@ -3895,7 +3890,7 @@ int btrfs_commit_super(struct btrfs_fs_info *fs_info)
3895 struct btrfs_trans_handle *trans; 3890 struct btrfs_trans_handle *trans;
3896 3891
3897 mutex_lock(&fs_info->cleaner_mutex); 3892 mutex_lock(&fs_info->cleaner_mutex);
3898 btrfs_run_delayed_iputs(root); 3893 btrfs_run_delayed_iputs(fs_info);
3899 mutex_unlock(&fs_info->cleaner_mutex); 3894 mutex_unlock(&fs_info->cleaner_mutex);
3900 wake_up_process(fs_info->cleaner_kthread); 3895 wake_up_process(fs_info->cleaner_kthread);
3901 3896
@@ -3954,7 +3949,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
3954 } 3949 }
3955 3950
3956 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) 3951 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3957 btrfs_error_commit_super(root); 3952 btrfs_error_commit_super(fs_info);
3958 3953
3959 kthread_stop(fs_info->transaction_kthread); 3954 kthread_stop(fs_info->transaction_kthread);
3960 kthread_stop(fs_info->cleaner_kthread); 3955 kthread_stop(fs_info->cleaner_kthread);
@@ -3991,7 +3986,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
3991 3986
3992#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 3987#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3993 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) 3988 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
3994 btrfsic_unmount(root, fs_info->fs_devices); 3989 btrfsic_unmount(fs_info->fs_devices);
3995#endif 3990#endif
3996 3991
3997 btrfs_close_devices(fs_info->fs_devices); 3992 btrfs_close_devices(fs_info->fs_devices);
@@ -4066,16 +4061,15 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4066 fs_info->dirty_metadata_batch); 4061 fs_info->dirty_metadata_batch);
4067#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4062#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4068 if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) { 4063 if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) {
4069 btrfs_print_leaf(root, buf); 4064 btrfs_print_leaf(fs_info, buf);
4070 ASSERT(0); 4065 ASSERT(0);
4071 } 4066 }
4072#endif 4067#endif
4073} 4068}
4074 4069
4075static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 4070static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
4076 int flush_delayed) 4071 int flush_delayed)
4077{ 4072{
4078 struct btrfs_fs_info *fs_info = root->fs_info;
4079 /* 4073 /*
4080 * looks as though older kernels can get into trouble with 4074 * looks as though older kernels can get into trouble with
4081 * this code, they end up stuck in balance_dirty_pages forever 4075 * this code, they end up stuck in balance_dirty_pages forever
@@ -4086,7 +4080,7 @@ static void __btrfs_btree_balance_dirty(struct btrfs_root *root,
4086 return; 4080 return;
4087 4081
4088 if (flush_delayed) 4082 if (flush_delayed)
4089 btrfs_balance_delayed_items(root); 4083 btrfs_balance_delayed_items(fs_info);
4090 4084
4091 ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes, 4085 ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4092 BTRFS_DIRTY_METADATA_THRESH); 4086 BTRFS_DIRTY_METADATA_THRESH);
@@ -4095,20 +4089,22 @@ static void __btrfs_btree_balance_dirty(struct btrfs_root *root,
4095 } 4089 }
4096} 4090}
4097 4091
4098void btrfs_btree_balance_dirty(struct btrfs_root *root) 4092void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
4099{ 4093{
4100 __btrfs_btree_balance_dirty(root, 1); 4094 __btrfs_btree_balance_dirty(fs_info, 1);
4101} 4095}
4102 4096
4103void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root) 4097void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
4104{ 4098{
4105 __btrfs_btree_balance_dirty(root, 0); 4099 __btrfs_btree_balance_dirty(fs_info, 0);
4106} 4100}
4107 4101
4108int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 4102int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
4109{ 4103{
4110 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 4104 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
4111 return btree_read_extent_buffer_pages(root, buf, parent_transid); 4105 struct btrfs_fs_info *fs_info = root->fs_info;
4106
4107 return btree_read_extent_buffer_pages(fs_info, buf, parent_transid);
4112} 4108}
4113 4109
4114static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info, 4110static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
@@ -4259,19 +4255,17 @@ static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
4259 return ret; 4255 return ret;
4260} 4256}
4261 4257
4262static void btrfs_error_commit_super(struct btrfs_root *root) 4258static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
4263{ 4259{
4264 struct btrfs_fs_info *fs_info = root->fs_info;
4265
4266 mutex_lock(&fs_info->cleaner_mutex); 4260 mutex_lock(&fs_info->cleaner_mutex);
4267 btrfs_run_delayed_iputs(root); 4261 btrfs_run_delayed_iputs(fs_info);
4268 mutex_unlock(&fs_info->cleaner_mutex); 4262 mutex_unlock(&fs_info->cleaner_mutex);
4269 4263
4270 down_write(&fs_info->cleanup_work_sem); 4264 down_write(&fs_info->cleanup_work_sem);
4271 up_write(&fs_info->cleanup_work_sem); 4265 up_write(&fs_info->cleanup_work_sem);
4272 4266
4273 /* cleanup FS via transaction */ 4267 /* cleanup FS via transaction */
4274 btrfs_cleanup_transaction(root); 4268 btrfs_cleanup_transaction(fs_info);
4275} 4269}
4276 4270
4277static void btrfs_destroy_ordered_extents(struct btrfs_root *root) 4271static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
@@ -4314,9 +4308,8 @@ static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4314} 4308}
4315 4309
4316static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 4310static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4317 struct btrfs_root *root) 4311 struct btrfs_fs_info *fs_info)
4318{ 4312{
4319 struct btrfs_fs_info *fs_info = root->fs_info;
4320 struct rb_node *node; 4313 struct rb_node *node;
4321 struct btrfs_delayed_ref_root *delayed_refs; 4314 struct btrfs_delayed_ref_root *delayed_refs;
4322 struct btrfs_delayed_ref_node *ref; 4315 struct btrfs_delayed_ref_node *ref;
@@ -4372,7 +4365,7 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4372 mutex_unlock(&head->mutex); 4365 mutex_unlock(&head->mutex);
4373 4366
4374 if (pin_bytes) 4367 if (pin_bytes)
4375 btrfs_pin_extent(root, head->node.bytenr, 4368 btrfs_pin_extent(fs_info, head->node.bytenr,
4376 head->node.num_bytes, 1); 4369 head->node.num_bytes, 1);
4377 btrfs_put_delayed_ref(&head->node); 4370 btrfs_put_delayed_ref(&head->node);
4378 cond_resched(); 4371 cond_resched();
@@ -4436,11 +4429,10 @@ static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
4436 spin_unlock(&fs_info->delalloc_root_lock); 4429 spin_unlock(&fs_info->delalloc_root_lock);
4437} 4430}
4438 4431
4439static int btrfs_destroy_marked_extents(struct btrfs_root *root, 4432static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
4440 struct extent_io_tree *dirty_pages, 4433 struct extent_io_tree *dirty_pages,
4441 int mark) 4434 int mark)
4442{ 4435{
4443 struct btrfs_fs_info *fs_info = root->fs_info;
4444 int ret; 4436 int ret;
4445 struct extent_buffer *eb; 4437 struct extent_buffer *eb;
4446 u64 start = 0; 4438 u64 start = 0;
@@ -4470,10 +4462,9 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
4470 return ret; 4462 return ret;
4471} 4463}
4472 4464
4473static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 4465static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
4474 struct extent_io_tree *pinned_extents) 4466 struct extent_io_tree *pinned_extents)
4475{ 4467{
4476 struct btrfs_fs_info *fs_info = root->fs_info;
4477 struct extent_io_tree *unpin; 4468 struct extent_io_tree *unpin;
4478 u64 start; 4469 u64 start;
4479 u64 end; 4470 u64 end;
@@ -4489,7 +4480,7 @@ again:
4489 break; 4480 break;
4490 4481
4491 clear_extent_dirty(unpin, start, end); 4482 clear_extent_dirty(unpin, start, end);
4492 btrfs_error_unpin_extent_range(root, start, end); 4483 btrfs_error_unpin_extent_range(fs_info, start, end);
4493 cond_resched(); 4484 cond_resched();
4494 } 4485 }
4495 4486
@@ -4520,9 +4511,8 @@ static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
4520} 4511}
4521 4512
4522void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 4513void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4523 struct btrfs_root *root) 4514 struct btrfs_fs_info *fs_info)
4524{ 4515{
4525 struct btrfs_fs_info *fs_info = root->fs_info;
4526 struct btrfs_block_group_cache *cache; 4516 struct btrfs_block_group_cache *cache;
4527 4517
4528 spin_lock(&cur_trans->dirty_bgs_lock); 4518 spin_lock(&cur_trans->dirty_bgs_lock);
@@ -4572,14 +4562,13 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4572} 4562}
4573 4563
4574void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 4564void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4575 struct btrfs_root *root) 4565 struct btrfs_fs_info *fs_info)
4576{ 4566{
4577 struct btrfs_fs_info *fs_info = root->fs_info; 4567 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
4578 btrfs_cleanup_dirty_bgs(cur_trans, root);
4579 ASSERT(list_empty(&cur_trans->dirty_bgs)); 4568 ASSERT(list_empty(&cur_trans->dirty_bgs));
4580 ASSERT(list_empty(&cur_trans->io_bgs)); 4569 ASSERT(list_empty(&cur_trans->io_bgs));
4581 4570
4582 btrfs_destroy_delayed_refs(cur_trans, root); 4571 btrfs_destroy_delayed_refs(cur_trans, fs_info);
4583 4572
4584 cur_trans->state = TRANS_STATE_COMMIT_START; 4573 cur_trans->state = TRANS_STATE_COMMIT_START;
4585 wake_up(&fs_info->transaction_blocked_wait); 4574 wake_up(&fs_info->transaction_blocked_wait);
@@ -4590,9 +4579,9 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4590 btrfs_destroy_delayed_inodes(fs_info); 4579 btrfs_destroy_delayed_inodes(fs_info);
4591 btrfs_assert_delayed_root_empty(fs_info); 4580 btrfs_assert_delayed_root_empty(fs_info);
4592 4581
4593 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 4582 btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
4594 EXTENT_DIRTY); 4583 EXTENT_DIRTY);
4595 btrfs_destroy_pinned_extent(root, 4584 btrfs_destroy_pinned_extent(fs_info,
4596 fs_info->pinned_extents); 4585 fs_info->pinned_extents);
4597 4586
4598 cur_trans->state =TRANS_STATE_COMPLETED; 4587 cur_trans->state =TRANS_STATE_COMPLETED;
@@ -4604,9 +4593,8 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4604 */ 4593 */
4605} 4594}
4606 4595
4607static int btrfs_cleanup_transaction(struct btrfs_root *root) 4596static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
4608{ 4597{
4609 struct btrfs_fs_info *fs_info = root->fs_info;
4610 struct btrfs_transaction *t; 4598 struct btrfs_transaction *t;
4611 4599
4612 mutex_lock(&fs_info->transaction_kthread_mutex); 4600 mutex_lock(&fs_info->transaction_kthread_mutex);
@@ -4618,7 +4606,7 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root)
4618 if (t->state >= TRANS_STATE_COMMIT_START) { 4606 if (t->state >= TRANS_STATE_COMMIT_START) {
4619 atomic_inc(&t->use_count); 4607 atomic_inc(&t->use_count);
4620 spin_unlock(&fs_info->trans_lock); 4608 spin_unlock(&fs_info->trans_lock);
4621 btrfs_wait_for_commit(root, t->transid); 4609 btrfs_wait_for_commit(fs_info, t->transid);
4622 btrfs_put_transaction(t); 4610 btrfs_put_transaction(t);
4623 spin_lock(&fs_info->trans_lock); 4611 spin_lock(&fs_info->trans_lock);
4624 continue; 4612 continue;
@@ -4635,7 +4623,7 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root)
4635 } else { 4623 } else {
4636 spin_unlock(&fs_info->trans_lock); 4624 spin_unlock(&fs_info->trans_lock);
4637 } 4625 }
4638 btrfs_cleanup_one_transaction(t, root); 4626 btrfs_cleanup_one_transaction(t, fs_info);
4639 4627
4640 spin_lock(&fs_info->trans_lock); 4628 spin_lock(&fs_info->trans_lock);
4641 if (t == fs_info->running_transaction) 4629 if (t == fs_info->running_transaction)
@@ -4644,14 +4632,14 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root)
4644 spin_unlock(&fs_info->trans_lock); 4632 spin_unlock(&fs_info->trans_lock);
4645 4633
4646 btrfs_put_transaction(t); 4634 btrfs_put_transaction(t);
4647 trace_btrfs_transaction_commit(root); 4635 trace_btrfs_transaction_commit(fs_info->tree_root);
4648 spin_lock(&fs_info->trans_lock); 4636 spin_lock(&fs_info->trans_lock);
4649 } 4637 }
4650 spin_unlock(&fs_info->trans_lock); 4638 spin_unlock(&fs_info->trans_lock);
4651 btrfs_destroy_all_ordered_extents(fs_info); 4639 btrfs_destroy_all_ordered_extents(fs_info);
4652 btrfs_destroy_delayed_inodes(fs_info); 4640 btrfs_destroy_delayed_inodes(fs_info);
4653 btrfs_assert_delayed_root_empty(fs_info); 4641 btrfs_assert_delayed_root_empty(fs_info);
4654 btrfs_destroy_pinned_extent(root, fs_info->pinned_extents); 4642 btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
4655 btrfs_destroy_all_delalloc_inodes(fs_info); 4643 btrfs_destroy_all_delalloc_inodes(fs_info);
4656 mutex_unlock(&fs_info->transaction_kthread_mutex); 4644 mutex_unlock(&fs_info->transaction_kthread_mutex);
4657 4645
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index b7f971190922..44dcd9af6b7c 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -44,13 +44,14 @@ static inline u64 btrfs_sb_offset(int mirror)
44struct btrfs_device; 44struct btrfs_device;
45struct btrfs_fs_devices; 45struct btrfs_fs_devices;
46 46
47struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr, 47struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info,
48 u64 parent_transid); 48 u64 bytenr, u64 parent_transid);
49void readahead_tree_block(struct btrfs_root *root, u64 bytenr); 49void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr);
50int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, 50int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
51 int mirror_num, struct extent_buffer **eb); 51 int mirror_num, struct extent_buffer **eb);
52struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 52struct extent_buffer *btrfs_find_create_tree_block(
53 u64 bytenr); 53 struct btrfs_fs_info *fs_info,
54 u64 bytenr);
54void clean_tree_block(struct btrfs_trans_handle *trans, 55void clean_tree_block(struct btrfs_trans_handle *trans,
55 struct btrfs_fs_info *fs_info, struct extent_buffer *buf); 56 struct btrfs_fs_info *fs_info, struct extent_buffer *buf);
56int open_ctree(struct super_block *sb, 57int open_ctree(struct super_block *sb,
@@ -58,7 +59,7 @@ int open_ctree(struct super_block *sb,
58 char *options); 59 char *options);
59void close_ctree(struct btrfs_fs_info *fs_info); 60void close_ctree(struct btrfs_fs_info *fs_info);
60int write_ctree_super(struct btrfs_trans_handle *trans, 61int write_ctree_super(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root, int max_mirrors); 62 struct btrfs_fs_info *fs_info, int max_mirrors);
62struct buffer_head *btrfs_read_dev_super(struct block_device *bdev); 63struct buffer_head *btrfs_read_dev_super(struct block_device *bdev);
63int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num, 64int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
64 struct buffer_head **bh_ret); 65 struct buffer_head **bh_ret);
@@ -83,8 +84,8 @@ btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
83} 84}
84 85
85int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info); 86int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info);
86void btrfs_btree_balance_dirty(struct btrfs_root *root); 87void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info);
87void btrfs_btree_balance_dirty_nodelay(struct btrfs_root *root); 88void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info);
88void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info, 89void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
89 struct btrfs_root *root); 90 struct btrfs_root *root);
90void btrfs_free_fs_root(struct btrfs_root *root); 91void btrfs_free_fs_root(struct btrfs_root *root);
@@ -134,9 +135,9 @@ int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
134int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 135int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root); 136 struct btrfs_root *root);
136void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *trans, 137void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *trans,
137 struct btrfs_root *root); 138 struct btrfs_fs_info *fs_info);
138void btrfs_cleanup_one_transaction(struct btrfs_transaction *trans, 139void btrfs_cleanup_one_transaction(struct btrfs_transaction *trans,
139 struct btrfs_root *root); 140 struct btrfs_fs_info *fs_info);
140struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans, 141struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
141 struct btrfs_fs_info *fs_info, 142 struct btrfs_fs_info *fs_info,
142 u64 objectid); 143 u64 objectid);
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index d0c5d5d92d1c..0360e973378d 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -64,7 +64,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
64 struct btrfs_fs_info *fs_info, u64 bytenr, 64 struct btrfs_fs_info *fs_info, u64 bytenr,
65 u64 num_bytes, int alloc); 65 u64 num_bytes, int alloc);
66static int __btrfs_free_extent(struct btrfs_trans_handle *trans, 66static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
67 struct btrfs_root *root, 67 struct btrfs_fs_info *fs_info,
68 struct btrfs_delayed_ref_node *node, u64 parent, 68 struct btrfs_delayed_ref_node *node, u64 parent,
69 u64 root_objectid, u64 owner_objectid, 69 u64 root_objectid, u64 owner_objectid,
70 u64 owner_offset, int refs_to_drop, 70 u64 owner_offset, int refs_to_drop,
@@ -73,17 +73,17 @@ static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
73 struct extent_buffer *leaf, 73 struct extent_buffer *leaf,
74 struct btrfs_extent_item *ei); 74 struct btrfs_extent_item *ei);
75static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, 75static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
76 struct btrfs_root *root, 76 struct btrfs_fs_info *fs_info,
77 u64 parent, u64 root_objectid, 77 u64 parent, u64 root_objectid,
78 u64 flags, u64 owner, u64 offset, 78 u64 flags, u64 owner, u64 offset,
79 struct btrfs_key *ins, int ref_mod); 79 struct btrfs_key *ins, int ref_mod);
80static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, 80static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
81 struct btrfs_root *root, 81 struct btrfs_fs_info *fs_info,
82 u64 parent, u64 root_objectid, 82 u64 parent, u64 root_objectid,
83 u64 flags, struct btrfs_disk_key *key, 83 u64 flags, struct btrfs_disk_key *key,
84 int level, struct btrfs_key *ins); 84 int level, struct btrfs_key *ins);
85static int do_chunk_alloc(struct btrfs_trans_handle *trans, 85static int do_chunk_alloc(struct btrfs_trans_handle *trans,
86 struct btrfs_root *extent_root, u64 flags, 86 struct btrfs_fs_info *fs_info, u64 flags,
87 int force); 87 int force);
88static int find_next_key(struct btrfs_path *path, int level, 88static int find_next_key(struct btrfs_path *path, int level,
89 struct btrfs_key *key); 89 struct btrfs_key *key);
@@ -96,8 +96,6 @@ static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
96 u64 num_bytes, int delalloc); 96 u64 num_bytes, int delalloc);
97static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, 97static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
98 u64 num_bytes); 98 u64 num_bytes);
99int btrfs_pin_extent(struct btrfs_root *root,
100 u64 bytenr, u64 num_bytes, int reserved);
101static int __reserve_metadata_bytes(struct btrfs_root *root, 99static int __reserve_metadata_bytes(struct btrfs_root *root,
102 struct btrfs_space_info *space_info, 100 struct btrfs_space_info *space_info,
103 u64 orig_bytes, 101 u64 orig_bytes,
@@ -223,10 +221,9 @@ block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
223 return ret; 221 return ret;
224} 222}
225 223
226static int add_excluded_extent(struct btrfs_root *root, 224static int add_excluded_extent(struct btrfs_fs_info *fs_info,
227 u64 start, u64 num_bytes) 225 u64 start, u64 num_bytes)
228{ 226{
229 struct btrfs_fs_info *fs_info = root->fs_info;
230 u64 end = start + num_bytes - 1; 227 u64 end = start + num_bytes - 1;
231 set_extent_bits(&fs_info->freed_extents[0], 228 set_extent_bits(&fs_info->freed_extents[0],
232 start, end, EXTENT_UPTODATE); 229 start, end, EXTENT_UPTODATE);
@@ -235,10 +232,9 @@ static int add_excluded_extent(struct btrfs_root *root,
235 return 0; 232 return 0;
236} 233}
237 234
238static void free_excluded_extents(struct btrfs_root *root, 235static void free_excluded_extents(struct btrfs_fs_info *fs_info,
239 struct btrfs_block_group_cache *cache) 236 struct btrfs_block_group_cache *cache)
240{ 237{
241 struct btrfs_fs_info *fs_info = root->fs_info;
242 u64 start, end; 238 u64 start, end;
243 239
244 start = cache->key.objectid; 240 start = cache->key.objectid;
@@ -250,10 +246,9 @@ static void free_excluded_extents(struct btrfs_root *root,
250 start, end, EXTENT_UPTODATE); 246 start, end, EXTENT_UPTODATE);
251} 247}
252 248
253static int exclude_super_stripes(struct btrfs_root *root, 249static int exclude_super_stripes(struct btrfs_fs_info *fs_info,
254 struct btrfs_block_group_cache *cache) 250 struct btrfs_block_group_cache *cache)
255{ 251{
256 struct btrfs_fs_info *fs_info = root->fs_info;
257 u64 bytenr; 252 u64 bytenr;
258 u64 *logical; 253 u64 *logical;
259 int stripe_len; 254 int stripe_len;
@@ -262,7 +257,7 @@ static int exclude_super_stripes(struct btrfs_root *root,
262 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { 257 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
263 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; 258 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
264 cache->bytes_super += stripe_len; 259 cache->bytes_super += stripe_len;
265 ret = add_excluded_extent(root, cache->key.objectid, 260 ret = add_excluded_extent(fs_info, cache->key.objectid,
266 stripe_len); 261 stripe_len);
267 if (ret) 262 if (ret)
268 return ret; 263 return ret;
@@ -296,7 +291,7 @@ static int exclude_super_stripes(struct btrfs_root *root,
296 } 291 }
297 292
298 cache->bytes_super += len; 293 cache->bytes_super += len;
299 ret = add_excluded_extent(root, start, len); 294 ret = add_excluded_extent(fs_info, start, len);
300 if (ret) { 295 if (ret) {
301 kfree(logical); 296 kfree(logical);
302 return ret; 297 return ret;
@@ -332,11 +327,9 @@ static void put_caching_control(struct btrfs_caching_control *ctl)
332} 327}
333 328
334#ifdef CONFIG_BTRFS_DEBUG 329#ifdef CONFIG_BTRFS_DEBUG
335static void fragment_free_space(struct btrfs_root *root, 330static void fragment_free_space(struct btrfs_block_group_cache *block_group)
336 struct btrfs_block_group_cache *block_group)
337{ 331{
338 struct btrfs_fs_info *fs_info = root->fs_info; 332 struct btrfs_fs_info *fs_info = block_group->fs_info;
339
340 u64 start = block_group->key.objectid; 333 u64 start = block_group->key.objectid;
341 u64 len = block_group->key.offset; 334 u64 len = block_group->key.offset;
342 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? 335 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
@@ -423,7 +416,7 @@ static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
423 * allocate from this block group until we've had a chance to fragment 416 * allocate from this block group until we've had a chance to fragment
424 * the free space. 417 * the free space.
425 */ 418 */
426 if (btrfs_should_fragment_free_space(extent_root, block_group)) 419 if (btrfs_should_fragment_free_space(block_group))
427 wakeup = false; 420 wakeup = false;
428#endif 421#endif
429 /* 422 /*
@@ -562,7 +555,7 @@ static noinline void caching_thread(struct btrfs_work *work)
562 spin_unlock(&block_group->lock); 555 spin_unlock(&block_group->lock);
563 556
564#ifdef CONFIG_BTRFS_DEBUG 557#ifdef CONFIG_BTRFS_DEBUG
565 if (btrfs_should_fragment_free_space(extent_root, block_group)) { 558 if (btrfs_should_fragment_free_space(block_group)) {
566 u64 bytes_used; 559 u64 bytes_used;
567 560
568 spin_lock(&block_group->space_info->lock); 561 spin_lock(&block_group->space_info->lock);
@@ -572,14 +565,14 @@ static noinline void caching_thread(struct btrfs_work *work)
572 block_group->space_info->bytes_used += bytes_used >> 1; 565 block_group->space_info->bytes_used += bytes_used >> 1;
573 spin_unlock(&block_group->lock); 566 spin_unlock(&block_group->lock);
574 spin_unlock(&block_group->space_info->lock); 567 spin_unlock(&block_group->space_info->lock);
575 fragment_free_space(extent_root, block_group); 568 fragment_free_space(block_group);
576 } 569 }
577#endif 570#endif
578 571
579 caching_ctl->progress = (u64)-1; 572 caching_ctl->progress = (u64)-1;
580 573
581 up_read(&fs_info->commit_root_sem); 574 up_read(&fs_info->commit_root_sem);
582 free_excluded_extents(fs_info->extent_root, block_group); 575 free_excluded_extents(fs_info, block_group);
583 mutex_unlock(&caching_ctl->mutex); 576 mutex_unlock(&caching_ctl->mutex);
584 577
585 wake_up(&caching_ctl->wait); 578 wake_up(&caching_ctl->wait);
@@ -669,8 +662,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
669 spin_unlock(&cache->lock); 662 spin_unlock(&cache->lock);
670#ifdef CONFIG_BTRFS_DEBUG 663#ifdef CONFIG_BTRFS_DEBUG
671 if (ret == 1 && 664 if (ret == 1 &&
672 btrfs_should_fragment_free_space(fs_info->extent_root, 665 btrfs_should_fragment_free_space(cache)) {
673 cache)) {
674 u64 bytes_used; 666 u64 bytes_used;
675 667
676 spin_lock(&cache->space_info->lock); 668 spin_lock(&cache->space_info->lock);
@@ -680,7 +672,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
680 cache->space_info->bytes_used += bytes_used >> 1; 672 cache->space_info->bytes_used += bytes_used >> 1;
681 spin_unlock(&cache->lock); 673 spin_unlock(&cache->lock);
682 spin_unlock(&cache->space_info->lock); 674 spin_unlock(&cache->space_info->lock);
683 fragment_free_space(fs_info->extent_root, cache); 675 fragment_free_space(cache);
684 } 676 }
685#endif 677#endif
686 mutex_unlock(&caching_ctl->mutex); 678 mutex_unlock(&caching_ctl->mutex);
@@ -688,7 +680,7 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
688 wake_up(&caching_ctl->wait); 680 wake_up(&caching_ctl->wait);
689 if (ret == 1) { 681 if (ret == 1) {
690 put_caching_control(caching_ctl); 682 put_caching_control(caching_ctl);
691 free_excluded_extents(fs_info->extent_root, cache); 683 free_excluded_extents(fs_info, cache);
692 return 0; 684 return 0;
693 } 685 }
694 } else { 686 } else {
@@ -779,9 +771,8 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
779} 771}
780 772
781/* simple helper to search for an existing data extent at a given offset */ 773/* simple helper to search for an existing data extent at a given offset */
782int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len) 774int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
783{ 775{
784 struct btrfs_fs_info *fs_info = root->fs_info;
785 int ret; 776 int ret;
786 struct btrfs_key key; 777 struct btrfs_key key;
787 struct btrfs_path *path; 778 struct btrfs_path *path;
@@ -808,10 +799,9 @@ int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len)
808 * the delayed refs are not processed. 799 * the delayed refs are not processed.
809 */ 800 */
810int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, 801int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
811 struct btrfs_root *root, u64 bytenr, 802 struct btrfs_fs_info *fs_info, u64 bytenr,
812 u64 offset, int metadata, u64 *refs, u64 *flags) 803 u64 offset, int metadata, u64 *refs, u64 *flags)
813{ 804{
814 struct btrfs_fs_info *fs_info = root->fs_info;
815 struct btrfs_delayed_ref_head *head; 805 struct btrfs_delayed_ref_head *head;
816 struct btrfs_delayed_ref_root *delayed_refs; 806 struct btrfs_delayed_ref_root *delayed_refs;
817 struct btrfs_path *path; 807 struct btrfs_path *path;
@@ -1102,7 +1092,7 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
1102 return ret; 1092 return ret;
1103 BUG_ON(ret); /* Corruption */ 1093 BUG_ON(ret); /* Corruption */
1104 1094
1105 btrfs_extend_item(root, path, new_size); 1095 btrfs_extend_item(root->fs_info, path, new_size);
1106 1096
1107 leaf = path->nodes[0]; 1097 leaf = path->nodes[0];
1108 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1098 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
@@ -1749,7 +1739,7 @@ void setup_inline_extent_backref(struct btrfs_root *root,
1749 type = extent_ref_type(parent, owner); 1739 type = extent_ref_type(parent, owner);
1750 size = btrfs_extent_inline_ref_size(type); 1740 size = btrfs_extent_inline_ref_size(type);
1751 1741
1752 btrfs_extend_item(root, path, size); 1742 btrfs_extend_item(root->fs_info, path, size);
1753 1743
1754 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1744 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1755 refs = btrfs_extent_refs(leaf, ei); 1745 refs = btrfs_extent_refs(leaf, ei);
@@ -1876,7 +1866,7 @@ void update_inline_extent_backref(struct btrfs_root *root,
1876 memmove_extent_buffer(leaf, ptr, ptr + size, 1866 memmove_extent_buffer(leaf, ptr, ptr + size,
1877 end - ptr - size); 1867 end - ptr - size);
1878 item_size -= size; 1868 item_size -= size;
1879 btrfs_truncate_item(root, path, item_size, 1); 1869 btrfs_truncate_item(root->fs_info, path, item_size, 1);
1880 } 1870 }
1881 btrfs_mark_buffer_dirty(leaf); 1871 btrfs_mark_buffer_dirty(leaf);
1882} 1872}
@@ -2023,10 +2013,9 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2023 return ret; 2013 return ret;
2024} 2014}
2025 2015
2026int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, 2016int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
2027 u64 num_bytes, u64 *actual_bytes) 2017 u64 num_bytes, u64 *actual_bytes)
2028{ 2018{
2029 struct btrfs_fs_info *fs_info = root->fs_info;
2030 int ret; 2019 int ret;
2031 u64 discarded_bytes = 0; 2020 u64 discarded_bytes = 0;
2032 struct btrfs_bio *bbio = NULL; 2021 struct btrfs_bio *bbio = NULL;
@@ -2082,12 +2071,11 @@ int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
2082 2071
2083/* Can return -ENOMEM */ 2072/* Can return -ENOMEM */
2084int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 2073int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2085 struct btrfs_root *root, 2074 struct btrfs_fs_info *fs_info,
2086 u64 bytenr, u64 num_bytes, u64 parent, 2075 u64 bytenr, u64 num_bytes, u64 parent,
2087 u64 root_objectid, u64 owner, u64 offset) 2076 u64 root_objectid, u64 owner, u64 offset)
2088{ 2077{
2089 int ret; 2078 int ret;
2090 struct btrfs_fs_info *fs_info = root->fs_info;
2091 2079
2092 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && 2080 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
2093 root_objectid == BTRFS_TREE_LOG_OBJECTID); 2081 root_objectid == BTRFS_TREE_LOG_OBJECTID);
@@ -2107,13 +2095,12 @@ int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2107} 2095}
2108 2096
2109static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 2097static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2110 struct btrfs_root *root, 2098 struct btrfs_fs_info *fs_info,
2111 struct btrfs_delayed_ref_node *node, 2099 struct btrfs_delayed_ref_node *node,
2112 u64 parent, u64 root_objectid, 2100 u64 parent, u64 root_objectid,
2113 u64 owner, u64 offset, int refs_to_add, 2101 u64 owner, u64 offset, int refs_to_add,
2114 struct btrfs_delayed_extent_op *extent_op) 2102 struct btrfs_delayed_extent_op *extent_op)
2115{ 2103{
2116 struct btrfs_fs_info *fs_info = root->fs_info;
2117 struct btrfs_path *path; 2104 struct btrfs_path *path;
2118 struct extent_buffer *leaf; 2105 struct extent_buffer *leaf;
2119 struct btrfs_extent_item *item; 2106 struct btrfs_extent_item *item;
@@ -2167,12 +2154,11 @@ out:
2167} 2154}
2168 2155
2169static int run_delayed_data_ref(struct btrfs_trans_handle *trans, 2156static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root, 2157 struct btrfs_fs_info *fs_info,
2171 struct btrfs_delayed_ref_node *node, 2158 struct btrfs_delayed_ref_node *node,
2172 struct btrfs_delayed_extent_op *extent_op, 2159 struct btrfs_delayed_extent_op *extent_op,
2173 int insert_reserved) 2160 int insert_reserved)
2174{ 2161{
2175 struct btrfs_fs_info *fs_info = root->fs_info;
2176 int ret = 0; 2162 int ret = 0;
2177 struct btrfs_delayed_data_ref *ref; 2163 struct btrfs_delayed_data_ref *ref;
2178 struct btrfs_key ins; 2164 struct btrfs_key ins;
@@ -2194,17 +2180,17 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2194 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { 2180 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2195 if (extent_op) 2181 if (extent_op)
2196 flags |= extent_op->flags_to_set; 2182 flags |= extent_op->flags_to_set;
2197 ret = alloc_reserved_file_extent(trans, root, 2183 ret = alloc_reserved_file_extent(trans, fs_info,
2198 parent, ref_root, flags, 2184 parent, ref_root, flags,
2199 ref->objectid, ref->offset, 2185 ref->objectid, ref->offset,
2200 &ins, node->ref_mod); 2186 &ins, node->ref_mod);
2201 } else if (node->action == BTRFS_ADD_DELAYED_REF) { 2187 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2202 ret = __btrfs_inc_extent_ref(trans, root, node, parent, 2188 ret = __btrfs_inc_extent_ref(trans, fs_info, node, parent,
2203 ref_root, ref->objectid, 2189 ref_root, ref->objectid,
2204 ref->offset, node->ref_mod, 2190 ref->offset, node->ref_mod,
2205 extent_op); 2191 extent_op);
2206 } else if (node->action == BTRFS_DROP_DELAYED_REF) { 2192 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2207 ret = __btrfs_free_extent(trans, root, node, parent, 2193 ret = __btrfs_free_extent(trans, fs_info, node, parent,
2208 ref_root, ref->objectid, 2194 ref_root, ref->objectid,
2209 ref->offset, node->ref_mod, 2195 ref->offset, node->ref_mod,
2210 extent_op); 2196 extent_op);
@@ -2233,11 +2219,10 @@ static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2233} 2219}
2234 2220
2235static int run_delayed_extent_op(struct btrfs_trans_handle *trans, 2221static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2236 struct btrfs_root *root, 2222 struct btrfs_fs_info *fs_info,
2237 struct btrfs_delayed_ref_node *node, 2223 struct btrfs_delayed_ref_node *node,
2238 struct btrfs_delayed_extent_op *extent_op) 2224 struct btrfs_delayed_extent_op *extent_op)
2239{ 2225{
2240 struct btrfs_fs_info *fs_info = root->fs_info;
2241 struct btrfs_key key; 2226 struct btrfs_key key;
2242 struct btrfs_path *path; 2227 struct btrfs_path *path;
2243 struct btrfs_extent_item *ei; 2228 struct btrfs_extent_item *ei;
@@ -2326,12 +2311,11 @@ out:
2326} 2311}
2327 2312
2328static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, 2313static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2329 struct btrfs_root *root, 2314 struct btrfs_fs_info *fs_info,
2330 struct btrfs_delayed_ref_node *node, 2315 struct btrfs_delayed_ref_node *node,
2331 struct btrfs_delayed_extent_op *extent_op, 2316 struct btrfs_delayed_extent_op *extent_op,
2332 int insert_reserved) 2317 int insert_reserved)
2333{ 2318{
2334 struct btrfs_fs_info *fs_info = root->fs_info;
2335 int ret = 0; 2319 int ret = 0;
2336 struct btrfs_delayed_tree_ref *ref; 2320 struct btrfs_delayed_tree_ref *ref;
2337 struct btrfs_key ins; 2321 struct btrfs_key ins;
@@ -2356,7 +2340,7 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2356 } 2340 }
2357 2341
2358 if (node->ref_mod != 1) { 2342 if (node->ref_mod != 1) {
2359 btrfs_err(root->fs_info, 2343 btrfs_err(fs_info,
2360 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", 2344 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
2361 node->bytenr, node->ref_mod, node->action, ref_root, 2345 node->bytenr, node->ref_mod, node->action, ref_root,
2362 parent); 2346 parent);
@@ -2364,18 +2348,18 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2364 } 2348 }
2365 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { 2349 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2366 BUG_ON(!extent_op || !extent_op->update_flags); 2350 BUG_ON(!extent_op || !extent_op->update_flags);
2367 ret = alloc_reserved_tree_block(trans, root, 2351 ret = alloc_reserved_tree_block(trans, fs_info,
2368 parent, ref_root, 2352 parent, ref_root,
2369 extent_op->flags_to_set, 2353 extent_op->flags_to_set,
2370 &extent_op->key, 2354 &extent_op->key,
2371 ref->level, &ins); 2355 ref->level, &ins);
2372 } else if (node->action == BTRFS_ADD_DELAYED_REF) { 2356 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2373 ret = __btrfs_inc_extent_ref(trans, root, node, 2357 ret = __btrfs_inc_extent_ref(trans, fs_info, node,
2374 parent, ref_root, 2358 parent, ref_root,
2375 ref->level, 0, 1, 2359 ref->level, 0, 1,
2376 extent_op); 2360 extent_op);
2377 } else if (node->action == BTRFS_DROP_DELAYED_REF) { 2361 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2378 ret = __btrfs_free_extent(trans, root, node, 2362 ret = __btrfs_free_extent(trans, fs_info, node,
2379 parent, ref_root, 2363 parent, ref_root,
2380 ref->level, 0, 1, extent_op); 2364 ref->level, 0, 1, extent_op);
2381 } else { 2365 } else {
@@ -2386,17 +2370,16 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2386 2370
2387/* helper function to actually process a single delayed ref entry */ 2371/* helper function to actually process a single delayed ref entry */
2388static int run_one_delayed_ref(struct btrfs_trans_handle *trans, 2372static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2389 struct btrfs_root *root, 2373 struct btrfs_fs_info *fs_info,
2390 struct btrfs_delayed_ref_node *node, 2374 struct btrfs_delayed_ref_node *node,
2391 struct btrfs_delayed_extent_op *extent_op, 2375 struct btrfs_delayed_extent_op *extent_op,
2392 int insert_reserved) 2376 int insert_reserved)
2393{ 2377{
2394 struct btrfs_fs_info *fs_info = root->fs_info;
2395 int ret = 0; 2378 int ret = 0;
2396 2379
2397 if (trans->aborted) { 2380 if (trans->aborted) {
2398 if (insert_reserved) 2381 if (insert_reserved)
2399 btrfs_pin_extent(root, node->bytenr, 2382 btrfs_pin_extent(fs_info, node->bytenr,
2400 node->num_bytes, 1); 2383 node->num_bytes, 1);
2401 return 0; 2384 return 0;
2402 } 2385 }
@@ -2414,7 +2397,7 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2414 trace_run_delayed_ref_head(fs_info, node, head, node->action); 2397 trace_run_delayed_ref_head(fs_info, node, head, node->action);
2415 2398
2416 if (insert_reserved) { 2399 if (insert_reserved) {
2417 btrfs_pin_extent(root, node->bytenr, 2400 btrfs_pin_extent(fs_info, node->bytenr,
2418 node->num_bytes, 1); 2401 node->num_bytes, 1);
2419 if (head->is_data) { 2402 if (head->is_data) {
2420 ret = btrfs_del_csums(trans, fs_info, 2403 ret = btrfs_del_csums(trans, fs_info,
@@ -2431,11 +2414,11 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2431 2414
2432 if (node->type == BTRFS_TREE_BLOCK_REF_KEY || 2415 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2433 node->type == BTRFS_SHARED_BLOCK_REF_KEY) 2416 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2434 ret = run_delayed_tree_ref(trans, root, node, extent_op, 2417 ret = run_delayed_tree_ref(trans, fs_info, node, extent_op,
2435 insert_reserved); 2418 insert_reserved);
2436 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || 2419 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2437 node->type == BTRFS_SHARED_DATA_REF_KEY) 2420 node->type == BTRFS_SHARED_DATA_REF_KEY)
2438 ret = run_delayed_data_ref(trans, root, node, extent_op, 2421 ret = run_delayed_data_ref(trans, fs_info, node, extent_op,
2439 insert_reserved); 2422 insert_reserved);
2440 else 2423 else
2441 BUG(); 2424 BUG();
@@ -2471,14 +2454,13 @@ select_delayed_ref(struct btrfs_delayed_ref_head *head)
2471 * Returns -ENOMEM or -EIO on failure and will abort the transaction. 2454 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2472 */ 2455 */
2473static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 2456static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2474 struct btrfs_root *root, 2457 struct btrfs_fs_info *fs_info,
2475 unsigned long nr) 2458 unsigned long nr)
2476{ 2459{
2477 struct btrfs_delayed_ref_root *delayed_refs; 2460 struct btrfs_delayed_ref_root *delayed_refs;
2478 struct btrfs_delayed_ref_node *ref; 2461 struct btrfs_delayed_ref_node *ref;
2479 struct btrfs_delayed_ref_head *locked_ref = NULL; 2462 struct btrfs_delayed_ref_head *locked_ref = NULL;
2480 struct btrfs_delayed_extent_op *extent_op; 2463 struct btrfs_delayed_extent_op *extent_op;
2481 struct btrfs_fs_info *fs_info = root->fs_info;
2482 ktime_t start = ktime_get(); 2464 ktime_t start = ktime_get();
2483 int ret; 2465 int ret;
2484 unsigned long count = 0; 2466 unsigned long count = 0;
@@ -2577,7 +2559,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2577 2559
2578 if (extent_op) { 2560 if (extent_op) {
2579 spin_unlock(&locked_ref->lock); 2561 spin_unlock(&locked_ref->lock);
2580 ret = run_delayed_extent_op(trans, root, 2562 ret = run_delayed_extent_op(trans, fs_info,
2581 ref, extent_op); 2563 ref, extent_op);
2582 btrfs_free_delayed_extent_op(extent_op); 2564 btrfs_free_delayed_extent_op(extent_op);
2583 2565
@@ -2647,7 +2629,7 @@ static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2647 } 2629 }
2648 spin_unlock(&locked_ref->lock); 2630 spin_unlock(&locked_ref->lock);
2649 2631
2650 ret = run_one_delayed_ref(trans, root, ref, extent_op, 2632 ret = run_one_delayed_ref(trans, fs_info, ref, extent_op,
2651 must_insert_reserved); 2633 must_insert_reserved);
2652 2634
2653 btrfs_free_delayed_extent_op(extent_op); 2635 btrfs_free_delayed_extent_op(extent_op);
@@ -2748,9 +2730,8 @@ static u64 find_middle(struct rb_root *root)
2748} 2730}
2749#endif 2731#endif
2750 2732
2751static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads) 2733static inline u64 heads_to_leaves(struct btrfs_fs_info *fs_info, u64 heads)
2752{ 2734{
2753 struct btrfs_fs_info *fs_info = root->fs_info;
2754 u64 num_bytes; 2735 u64 num_bytes;
2755 2736
2756 num_bytes = heads * (sizeof(struct btrfs_extent_item) + 2737 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
@@ -2769,9 +2750,8 @@ static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2769 * Takes the number of bytes to be csumm'ed and figures out how many leaves it 2750 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2770 * would require to store the csums for that many bytes. 2751 * would require to store the csums for that many bytes.
2771 */ 2752 */
2772u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes) 2753u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
2773{ 2754{
2774 struct btrfs_fs_info *fs_info = root->fs_info;
2775 u64 csum_size; 2755 u64 csum_size;
2776 u64 num_csums_per_leaf; 2756 u64 num_csums_per_leaf;
2777 u64 num_csums; 2757 u64 num_csums;
@@ -2786,9 +2766,8 @@ u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
2786} 2766}
2787 2767
2788int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, 2768int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2789 struct btrfs_root *root) 2769 struct btrfs_fs_info *fs_info)
2790{ 2770{
2791 struct btrfs_fs_info *fs_info = root->fs_info;
2792 struct btrfs_block_rsv *global_rsv; 2771 struct btrfs_block_rsv *global_rsv;
2793 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; 2772 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2794 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; 2773 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
@@ -2797,11 +2776,12 @@ int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2797 int ret = 0; 2776 int ret = 0;
2798 2777
2799 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); 2778 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
2800 num_heads = heads_to_leaves(root, num_heads); 2779 num_heads = heads_to_leaves(fs_info, num_heads);
2801 if (num_heads > 1) 2780 if (num_heads > 1)
2802 num_bytes += (num_heads - 1) * fs_info->nodesize; 2781 num_bytes += (num_heads - 1) * fs_info->nodesize;
2803 num_bytes <<= 1; 2782 num_bytes <<= 1;
2804 num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * fs_info->nodesize; 2783 num_bytes += btrfs_csum_bytes_to_leaves(fs_info, csum_bytes) *
2784 fs_info->nodesize;
2805 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info, 2785 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
2806 num_dirty_bgs); 2786 num_dirty_bgs);
2807 global_rsv = &fs_info->global_block_rsv; 2787 global_rsv = &fs_info->global_block_rsv;
@@ -2823,9 +2803,8 @@ int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2823} 2803}
2824 2804
2825int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, 2805int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2826 struct btrfs_root *root) 2806 struct btrfs_fs_info *fs_info)
2827{ 2807{
2828 struct btrfs_fs_info *fs_info = root->fs_info;
2829 u64 num_entries = 2808 u64 num_entries =
2830 atomic_read(&trans->transaction->delayed_refs.num_entries); 2809 atomic_read(&trans->transaction->delayed_refs.num_entries);
2831 u64 avg_runtime; 2810 u64 avg_runtime;
@@ -2839,7 +2818,7 @@ int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2839 if (val >= NSEC_PER_SEC / 2) 2818 if (val >= NSEC_PER_SEC / 2)
2840 return 2; 2819 return 2;
2841 2820
2842 return btrfs_check_space_for_delayed_refs(trans, root); 2821 return btrfs_check_space_for_delayed_refs(trans, fs_info);
2843} 2822}
2844 2823
2845struct async_delayed_refs { 2824struct async_delayed_refs {
@@ -2852,16 +2831,21 @@ struct async_delayed_refs {
2852 struct btrfs_work work; 2831 struct btrfs_work work;
2853}; 2832};
2854 2833
2834static inline struct async_delayed_refs *
2835to_async_delayed_refs(struct btrfs_work *work)
2836{
2837 return container_of(work, struct async_delayed_refs, work);
2838}
2839
2855static void delayed_ref_async_start(struct btrfs_work *work) 2840static void delayed_ref_async_start(struct btrfs_work *work)
2856{ 2841{
2857 struct async_delayed_refs *async; 2842 struct async_delayed_refs *async = to_async_delayed_refs(work);
2858 struct btrfs_trans_handle *trans; 2843 struct btrfs_trans_handle *trans;
2844 struct btrfs_fs_info *fs_info = async->root->fs_info;
2859 int ret; 2845 int ret;
2860 2846
2861 async = container_of(work, struct async_delayed_refs, work);
2862
2863 /* if the commit is already started, we don't need to wait here */ 2847 /* if the commit is already started, we don't need to wait here */
2864 if (btrfs_transaction_blocked(async->root->fs_info)) 2848 if (btrfs_transaction_blocked(fs_info))
2865 goto done; 2849 goto done;
2866 2850
2867 trans = btrfs_join_transaction(async->root); 2851 trans = btrfs_join_transaction(async->root);
@@ -2880,7 +2864,7 @@ static void delayed_ref_async_start(struct btrfs_work *work)
2880 if (trans->transid > async->transid) 2864 if (trans->transid > async->transid)
2881 goto end; 2865 goto end;
2882 2866
2883 ret = btrfs_run_delayed_refs(trans, async->root, async->count); 2867 ret = btrfs_run_delayed_refs(trans, fs_info, async->count);
2884 if (ret) 2868 if (ret)
2885 async->error = ret; 2869 async->error = ret;
2886end: 2870end:
@@ -2894,10 +2878,9 @@ done:
2894 kfree(async); 2878 kfree(async);
2895} 2879}
2896 2880
2897int btrfs_async_run_delayed_refs(struct btrfs_root *root, 2881int btrfs_async_run_delayed_refs(struct btrfs_fs_info *fs_info,
2898 unsigned long count, u64 transid, int wait) 2882 unsigned long count, u64 transid, int wait)
2899{ 2883{
2900 struct btrfs_fs_info *fs_info = root->fs_info;
2901 struct async_delayed_refs *async; 2884 struct async_delayed_refs *async;
2902 int ret; 2885 int ret;
2903 2886
@@ -2940,9 +2923,8 @@ int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2940 * Returns <0 on error and aborts the transaction 2923 * Returns <0 on error and aborts the transaction
2941 */ 2924 */
2942int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 2925int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2943 struct btrfs_root *root, unsigned long count) 2926 struct btrfs_fs_info *fs_info, unsigned long count)
2944{ 2927{
2945 struct btrfs_fs_info *fs_info = root->fs_info;
2946 struct rb_node *node; 2928 struct rb_node *node;
2947 struct btrfs_delayed_ref_root *delayed_refs; 2929 struct btrfs_delayed_ref_root *delayed_refs;
2948 struct btrfs_delayed_ref_head *head; 2930 struct btrfs_delayed_ref_head *head;
@@ -2957,9 +2939,6 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2957 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) 2939 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
2958 return 0; 2940 return 0;
2959 2941
2960 if (root == fs_info->extent_root)
2961 root = fs_info->tree_root;
2962
2963 delayed_refs = &trans->transaction->delayed_refs; 2942 delayed_refs = &trans->transaction->delayed_refs;
2964 if (count == 0) 2943 if (count == 0)
2965 count = atomic_read(&delayed_refs->num_entries) * 2; 2944 count = atomic_read(&delayed_refs->num_entries) * 2;
@@ -2969,7 +2948,7 @@ again:
2969 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); 2948 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2970#endif 2949#endif
2971 trans->can_flush_pending_bgs = false; 2950 trans->can_flush_pending_bgs = false;
2972 ret = __btrfs_run_delayed_refs(trans, root, count); 2951 ret = __btrfs_run_delayed_refs(trans, fs_info, count);
2973 if (ret < 0) { 2952 if (ret < 0) {
2974 btrfs_abort_transaction(trans, ret); 2953 btrfs_abort_transaction(trans, ret);
2975 return ret; 2954 return ret;
@@ -2977,7 +2956,7 @@ again:
2977 2956
2978 if (run_all) { 2957 if (run_all) {
2979 if (!list_empty(&trans->new_bgs)) 2958 if (!list_empty(&trans->new_bgs))
2980 btrfs_create_pending_block_groups(trans, root); 2959 btrfs_create_pending_block_groups(trans, fs_info);
2981 2960
2982 spin_lock(&delayed_refs->lock); 2961 spin_lock(&delayed_refs->lock);
2983 node = rb_first(&delayed_refs->href_root); 2962 node = rb_first(&delayed_refs->href_root);
@@ -3022,11 +3001,10 @@ out:
3022} 3001}
3023 3002
3024int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, 3003int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
3025 struct btrfs_root *root, 3004 struct btrfs_fs_info *fs_info,
3026 u64 bytenr, u64 num_bytes, u64 flags, 3005 u64 bytenr, u64 num_bytes, u64 flags,
3027 int level, int is_data) 3006 int level, int is_data)
3028{ 3007{
3029 struct btrfs_fs_info *fs_info = root->fs_info;
3030 struct btrfs_delayed_extent_op *extent_op; 3008 struct btrfs_delayed_extent_op *extent_op;
3031 int ret; 3009 int ret;
3032 3010
@@ -3233,7 +3211,8 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3233 int i; 3211 int i;
3234 int level; 3212 int level;
3235 int ret = 0; 3213 int ret = 0;
3236 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, 3214 int (*process_func)(struct btrfs_trans_handle *,
3215 struct btrfs_fs_info *,
3237 u64, u64, u64, u64, u64, u64); 3216 u64, u64, u64, u64, u64, u64);
3238 3217
3239 3218
@@ -3273,7 +3252,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3273 3252
3274 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); 3253 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3275 key.offset -= btrfs_file_extent_offset(buf, fi); 3254 key.offset -= btrfs_file_extent_offset(buf, fi);
3276 ret = process_func(trans, root, bytenr, num_bytes, 3255 ret = process_func(trans, fs_info, bytenr, num_bytes,
3277 parent, ref_root, key.objectid, 3256 parent, ref_root, key.objectid,
3278 key.offset); 3257 key.offset);
3279 if (ret) 3258 if (ret)
@@ -3281,7 +3260,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3281 } else { 3260 } else {
3282 bytenr = btrfs_node_blockptr(buf, i); 3261 bytenr = btrfs_node_blockptr(buf, i);
3283 num_bytes = fs_info->nodesize; 3262 num_bytes = fs_info->nodesize;
3284 ret = process_func(trans, root, bytenr, num_bytes, 3263 ret = process_func(trans, fs_info, bytenr, num_bytes,
3285 parent, ref_root, level - 1, 0); 3264 parent, ref_root, level - 1, 0);
3286 if (ret) 3265 if (ret)
3287 goto fail; 3266 goto fail;
@@ -3305,12 +3284,11 @@ int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3305} 3284}
3306 3285
3307static int write_one_cache_group(struct btrfs_trans_handle *trans, 3286static int write_one_cache_group(struct btrfs_trans_handle *trans,
3308 struct btrfs_root *root, 3287 struct btrfs_fs_info *fs_info,
3309 struct btrfs_path *path, 3288 struct btrfs_path *path,
3310 struct btrfs_block_group_cache *cache) 3289 struct btrfs_block_group_cache *cache)
3311{ 3290{
3312 int ret; 3291 int ret;
3313 struct btrfs_fs_info *fs_info = root->fs_info;
3314 struct btrfs_root *extent_root = fs_info->extent_root; 3292 struct btrfs_root *extent_root = fs_info->extent_root;
3315 unsigned long bi; 3293 unsigned long bi;
3316 struct extent_buffer *leaf; 3294 struct extent_buffer *leaf;
@@ -3333,10 +3311,9 @@ fail:
3333} 3311}
3334 3312
3335static struct btrfs_block_group_cache * 3313static struct btrfs_block_group_cache *
3336next_block_group(struct btrfs_root *root, 3314next_block_group(struct btrfs_fs_info *fs_info,
3337 struct btrfs_block_group_cache *cache) 3315 struct btrfs_block_group_cache *cache)
3338{ 3316{
3339 struct btrfs_fs_info *fs_info = root->fs_info;
3340 struct rb_node *node; 3317 struct rb_node *node;
3341 3318
3342 spin_lock(&fs_info->block_group_cache_lock); 3319 spin_lock(&fs_info->block_group_cache_lock);
@@ -3439,7 +3416,7 @@ again:
3439 WARN_ON(ret); 3416 WARN_ON(ret);
3440 3417
3441 if (i_size_read(inode) > 0) { 3418 if (i_size_read(inode) > 0) {
3442 ret = btrfs_check_trunc_cache_free_space(root, 3419 ret = btrfs_check_trunc_cache_free_space(fs_info,
3443 &fs_info->global_block_rsv); 3420 &fs_info->global_block_rsv);
3444 if (ret) 3421 if (ret)
3445 goto out_put; 3422 goto out_put;
@@ -3520,9 +3497,8 @@ out:
3520} 3497}
3521 3498
3522int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, 3499int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
3523 struct btrfs_root *root) 3500 struct btrfs_fs_info *fs_info)
3524{ 3501{
3525 struct btrfs_fs_info *fs_info = root->fs_info;
3526 struct btrfs_block_group_cache *cache, *tmp; 3502 struct btrfs_block_group_cache *cache, *tmp;
3527 struct btrfs_transaction *cur_trans = trans->transaction; 3503 struct btrfs_transaction *cur_trans = trans->transaction;
3528 struct btrfs_path *path; 3504 struct btrfs_path *path;
@@ -3559,9 +3535,8 @@ int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
3559 * we're still allowing others to join the commit. 3535 * we're still allowing others to join the commit.
3560 */ 3536 */
3561int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, 3537int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
3562 struct btrfs_root *root) 3538 struct btrfs_fs_info *fs_info)
3563{ 3539{
3564 struct btrfs_fs_info *fs_info = root->fs_info;
3565 struct btrfs_block_group_cache *cache; 3540 struct btrfs_block_group_cache *cache;
3566 struct btrfs_transaction *cur_trans = trans->transaction; 3541 struct btrfs_transaction *cur_trans = trans->transaction;
3567 int ret = 0; 3542 int ret = 0;
@@ -3585,7 +3560,7 @@ again:
3585 * make sure all the block groups on our dirty list actually 3560 * make sure all the block groups on our dirty list actually
3586 * exist 3561 * exist
3587 */ 3562 */
3588 btrfs_create_pending_block_groups(trans, root); 3563 btrfs_create_pending_block_groups(trans, fs_info);
3589 3564
3590 if (!path) { 3565 if (!path) {
3591 path = btrfs_alloc_path(); 3566 path = btrfs_alloc_path();
@@ -3653,7 +3628,8 @@ again:
3653 } 3628 }
3654 } 3629 }
3655 if (!ret) { 3630 if (!ret) {
3656 ret = write_one_cache_group(trans, root, path, cache); 3631 ret = write_one_cache_group(trans, fs_info,
3632 path, cache);
3657 /* 3633 /*
3658 * Our block group might still be attached to the list 3634 * Our block group might still be attached to the list
3659 * of new block groups in the transaction handle of some 3635 * of new block groups in the transaction handle of some
@@ -3698,7 +3674,7 @@ again:
3698 * go through delayed refs for all the stuff we've just kicked off 3674 * go through delayed refs for all the stuff we've just kicked off
3699 * and then loop back (just once) 3675 * and then loop back (just once)
3700 */ 3676 */
3701 ret = btrfs_run_delayed_refs(trans, root, 0); 3677 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
3702 if (!ret && loops == 0) { 3678 if (!ret && loops == 0) {
3703 loops++; 3679 loops++;
3704 spin_lock(&cur_trans->dirty_bgs_lock); 3680 spin_lock(&cur_trans->dirty_bgs_lock);
@@ -3713,7 +3689,7 @@ again:
3713 } 3689 }
3714 spin_unlock(&cur_trans->dirty_bgs_lock); 3690 spin_unlock(&cur_trans->dirty_bgs_lock);
3715 } else if (ret < 0) { 3691 } else if (ret < 0) {
3716 btrfs_cleanup_dirty_bgs(cur_trans, root); 3692 btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
3717 } 3693 }
3718 3694
3719 btrfs_free_path(path); 3695 btrfs_free_path(path);
@@ -3721,9 +3697,8 @@ again:
3721} 3697}
3722 3698
3723int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, 3699int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3724 struct btrfs_root *root) 3700 struct btrfs_fs_info *fs_info)
3725{ 3701{
3726 struct btrfs_fs_info *fs_info = root->fs_info;
3727 struct btrfs_block_group_cache *cache; 3702 struct btrfs_block_group_cache *cache;
3728 struct btrfs_transaction *cur_trans = trans->transaction; 3703 struct btrfs_transaction *cur_trans = trans->transaction;
3729 int ret = 0; 3704 int ret = 0;
@@ -3781,7 +3756,8 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3781 cache_save_setup(cache, trans, path); 3756 cache_save_setup(cache, trans, path);
3782 3757
3783 if (!ret) 3758 if (!ret)
3784 ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1); 3759 ret = btrfs_run_delayed_refs(trans, fs_info,
3760 (unsigned long) -1);
3785 3761
3786 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { 3762 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3787 cache->io_ctl.inode = NULL; 3763 cache->io_ctl.inode = NULL;
@@ -3800,7 +3776,8 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3800 } 3776 }
3801 } 3777 }
3802 if (!ret) { 3778 if (!ret) {
3803 ret = write_one_cache_group(trans, root, path, cache); 3779 ret = write_one_cache_group(trans, fs_info,
3780 path, cache);
3804 /* 3781 /*
3805 * One of the free space endio workers might have 3782 * One of the free space endio workers might have
3806 * created a new block group while updating a free space 3783 * created a new block group while updating a free space
@@ -3817,8 +3794,8 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3817 if (ret == -ENOENT) { 3794 if (ret == -ENOENT) {
3818 wait_event(cur_trans->writer_wait, 3795 wait_event(cur_trans->writer_wait,
3819 atomic_read(&cur_trans->num_writers) == 1); 3796 atomic_read(&cur_trans->num_writers) == 1);
3820 ret = write_one_cache_group(trans, root, path, 3797 ret = write_one_cache_group(trans, fs_info,
3821 cache); 3798 path, cache);
3822 } 3799 }
3823 if (ret) 3800 if (ret)
3824 btrfs_abort_transaction(trans, ret); 3801 btrfs_abort_transaction(trans, ret);
@@ -3843,9 +3820,8 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3843 return ret; 3820 return ret;
3844} 3821}
3845 3822
3846int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) 3823int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
3847{ 3824{
3848 struct btrfs_fs_info *fs_info = root->fs_info;
3849 struct btrfs_block_group_cache *block_group; 3825 struct btrfs_block_group_cache *block_group;
3850 int readonly = 0; 3826 int readonly = 0;
3851 3827
@@ -4058,9 +4034,8 @@ static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4058 * progress (either running or paused) picks the target profile (if it's 4034 * progress (either running or paused) picks the target profile (if it's
4059 * already available), otherwise falls back to plain reducing. 4035 * already available), otherwise falls back to plain reducing.
4060 */ 4036 */
4061static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) 4037static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags)
4062{ 4038{
4063 struct btrfs_fs_info *fs_info = root->fs_info;
4064 u64 num_devices = fs_info->fs_devices->rw_devices; 4039 u64 num_devices = fs_info->fs_devices->rw_devices;
4065 u64 target; 4040 u64 target;
4066 u64 raid_type; 4041 u64 raid_type;
@@ -4104,9 +4079,8 @@ static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
4104 return extended_to_chunk(flags | allowed); 4079 return extended_to_chunk(flags | allowed);
4105} 4080}
4106 4081
4107static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags) 4082static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags)
4108{ 4083{
4109 struct btrfs_fs_info *fs_info = root->fs_info;
4110 unsigned seq; 4084 unsigned seq;
4111 u64 flags; 4085 u64 flags;
4112 4086
@@ -4122,7 +4096,7 @@ static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
4122 flags |= fs_info->avail_metadata_alloc_bits; 4096 flags |= fs_info->avail_metadata_alloc_bits;
4123 } while (read_seqretry(&fs_info->profiles_lock, seq)); 4097 } while (read_seqretry(&fs_info->profiles_lock, seq));
4124 4098
4125 return btrfs_reduce_alloc_profile(root, flags); 4099 return btrfs_reduce_alloc_profile(fs_info, flags);
4126} 4100}
4127 4101
4128u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) 4102u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
@@ -4138,7 +4112,7 @@ u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
4138 else 4112 else
4139 flags = BTRFS_BLOCK_GROUP_METADATA; 4113 flags = BTRFS_BLOCK_GROUP_METADATA;
4140 4114
4141 ret = get_alloc_profile(root, flags); 4115 ret = get_alloc_profile(fs_info, flags);
4142 return ret; 4116 return ret;
4143} 4117}
4144 4118
@@ -4199,8 +4173,7 @@ alloc:
4199 if (IS_ERR(trans)) 4173 if (IS_ERR(trans))
4200 return PTR_ERR(trans); 4174 return PTR_ERR(trans);
4201 4175
4202 ret = do_chunk_alloc(trans, fs_info->extent_root, 4176 ret = do_chunk_alloc(trans, fs_info, alloc_target,
4203 alloc_target,
4204 CHUNK_ALLOC_NO_FORCE); 4177 CHUNK_ALLOC_NO_FORCE);
4205 btrfs_end_transaction(trans, root); 4178 btrfs_end_transaction(trans, root);
4206 if (ret < 0) { 4179 if (ret < 0) {
@@ -4370,10 +4343,9 @@ static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4370 return (global->size << 1); 4343 return (global->size << 1);
4371} 4344}
4372 4345
4373static int should_alloc_chunk(struct btrfs_root *root, 4346static int should_alloc_chunk(struct btrfs_fs_info *fs_info,
4374 struct btrfs_space_info *sinfo, int force) 4347 struct btrfs_space_info *sinfo, int force)
4375{ 4348{
4376 struct btrfs_fs_info *fs_info = root->fs_info;
4377 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; 4349 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4378 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; 4350 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
4379 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; 4351 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
@@ -4407,9 +4379,8 @@ static int should_alloc_chunk(struct btrfs_root *root,
4407 return 1; 4379 return 1;
4408} 4380}
4409 4381
4410static u64 get_profile_num_devs(struct btrfs_root *root, u64 type) 4382static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type)
4411{ 4383{
4412 struct btrfs_fs_info *fs_info = root->fs_info;
4413 u64 num_dev; 4384 u64 num_dev;
4414 4385
4415 if (type & (BTRFS_BLOCK_GROUP_RAID10 | 4386 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
@@ -4431,10 +4402,8 @@ static u64 get_profile_num_devs(struct btrfs_root *root, u64 type)
4431 * removing a chunk. 4402 * removing a chunk.
4432 */ 4403 */
4433void check_system_chunk(struct btrfs_trans_handle *trans, 4404void check_system_chunk(struct btrfs_trans_handle *trans,
4434 struct btrfs_root *root, 4405 struct btrfs_fs_info *fs_info, u64 type)
4435 u64 type)
4436{ 4406{
4437 struct btrfs_fs_info *fs_info = root->fs_info;
4438 struct btrfs_space_info *info; 4407 struct btrfs_space_info *info;
4439 u64 left; 4408 u64 left;
4440 u64 thresh; 4409 u64 thresh;
@@ -4454,7 +4423,7 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4454 info->bytes_may_use; 4423 info->bytes_may_use;
4455 spin_unlock(&info->lock); 4424 spin_unlock(&info->lock);
4456 4425
4457 num_devs = get_profile_num_devs(root, type); 4426 num_devs = get_profile_num_devs(fs_info, type);
4458 4427
4459 /* num_devs device items to update and 1 chunk item to add or remove */ 4428 /* num_devs device items to update and 1 chunk item to add or remove */
4460 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) + 4429 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
@@ -4476,7 +4445,7 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4476 * the paths we visit in the chunk tree (they were already COWed 4445 * the paths we visit in the chunk tree (they were already COWed
4477 * or created in the current transaction for example). 4446 * or created in the current transaction for example).
4478 */ 4447 */
4479 ret = btrfs_alloc_chunk(trans, root, flags); 4448 ret = btrfs_alloc_chunk(trans, fs_info, flags);
4480 } 4449 }
4481 4450
4482 if (!ret) { 4451 if (!ret) {
@@ -4498,10 +4467,9 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4498 * - return errors including -ENOSPC otherwise. 4467 * - return errors including -ENOSPC otherwise.
4499 */ 4468 */
4500static int do_chunk_alloc(struct btrfs_trans_handle *trans, 4469static int do_chunk_alloc(struct btrfs_trans_handle *trans,
4501 struct btrfs_root *extent_root, u64 flags, int force) 4470 struct btrfs_fs_info *fs_info, u64 flags, int force)
4502{ 4471{
4503 struct btrfs_space_info *space_info; 4472 struct btrfs_space_info *space_info;
4504 struct btrfs_fs_info *fs_info = extent_root->fs_info;
4505 int wait_for_alloc = 0; 4473 int wait_for_alloc = 0;
4506 int ret = 0; 4474 int ret = 0;
4507 4475
@@ -4521,7 +4489,7 @@ again:
4521 if (force < space_info->force_alloc) 4489 if (force < space_info->force_alloc)
4522 force = space_info->force_alloc; 4490 force = space_info->force_alloc;
4523 if (space_info->full) { 4491 if (space_info->full) {
4524 if (should_alloc_chunk(extent_root, space_info, force)) 4492 if (should_alloc_chunk(fs_info, space_info, force))
4525 ret = -ENOSPC; 4493 ret = -ENOSPC;
4526 else 4494 else
4527 ret = 0; 4495 ret = 0;
@@ -4529,7 +4497,7 @@ again:
4529 return ret; 4497 return ret;
4530 } 4498 }
4531 4499
4532 if (!should_alloc_chunk(extent_root, space_info, force)) { 4500 if (!should_alloc_chunk(fs_info, space_info, force)) {
4533 spin_unlock(&space_info->lock); 4501 spin_unlock(&space_info->lock);
4534 return 0; 4502 return 0;
4535 } else if (space_info->chunk_alloc) { 4503 } else if (space_info->chunk_alloc) {
@@ -4579,9 +4547,9 @@ again:
4579 * Check if we have enough space in SYSTEM chunk because we may need 4547 * Check if we have enough space in SYSTEM chunk because we may need
4580 * to update devices. 4548 * to update devices.
4581 */ 4549 */
4582 check_system_chunk(trans, extent_root, flags); 4550 check_system_chunk(trans, fs_info, flags);
4583 4551
4584 ret = btrfs_alloc_chunk(trans, extent_root, flags); 4552 ret = btrfs_alloc_chunk(trans, fs_info, flags);
4585 trans->allocating_chunk = false; 4553 trans->allocating_chunk = false;
4586 4554
4587 spin_lock(&space_info->lock); 4555 spin_lock(&space_info->lock);
@@ -4613,7 +4581,7 @@ out:
4613 */ 4581 */
4614 if (trans->can_flush_pending_bgs && 4582 if (trans->can_flush_pending_bgs &&
4615 trans->chunk_bytes_reserved >= (u64)SZ_2M) { 4583 trans->chunk_bytes_reserved >= (u64)SZ_2M) {
4616 btrfs_create_pending_block_groups(trans, extent_root); 4584 btrfs_create_pending_block_groups(trans, fs_info);
4617 btrfs_trans_release_chunk_metadata(trans); 4585 btrfs_trans_release_chunk_metadata(trans);
4618 } 4586 }
4619 return ret; 4587 return ret;
@@ -4682,10 +4650,9 @@ static int can_overcommit(struct btrfs_root *root,
4682 return 0; 4650 return 0;
4683} 4651}
4684 4652
4685static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, 4653static void btrfs_writeback_inodes_sb_nr(struct btrfs_fs_info *fs_info,
4686 unsigned long nr_pages, int nr_items) 4654 unsigned long nr_pages, int nr_items)
4687{ 4655{
4688 struct btrfs_fs_info *fs_info = root->fs_info;
4689 struct super_block *sb = fs_info->sb; 4656 struct super_block *sb = fs_info->sb;
4690 4657
4691 if (down_read_trylock(&sb->s_umount)) { 4658 if (down_read_trylock(&sb->s_umount)) {
@@ -4705,12 +4672,13 @@ static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
4705 } 4672 }
4706} 4673}
4707 4674
4708static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim) 4675static inline int calc_reclaim_items_nr(struct btrfs_fs_info *fs_info,
4676 u64 to_reclaim)
4709{ 4677{
4710 u64 bytes; 4678 u64 bytes;
4711 int nr; 4679 int nr;
4712 4680
4713 bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 4681 bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
4714 nr = (int)div64_u64(to_reclaim, bytes); 4682 nr = (int)div64_u64(to_reclaim, bytes);
4715 if (!nr) 4683 if (!nr)
4716 nr = 1; 4684 nr = 1;
@@ -4738,7 +4706,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4738 enum btrfs_reserve_flush_enum flush; 4706 enum btrfs_reserve_flush_enum flush;
4739 4707
4740 /* Calc the number of the pages we need flush for space reservation */ 4708 /* Calc the number of the pages we need flush for space reservation */
4741 items = calc_reclaim_items_nr(root, to_reclaim); 4709 items = calc_reclaim_items_nr(fs_info, to_reclaim);
4742 to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM; 4710 to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM;
4743 4711
4744 trans = (struct btrfs_trans_handle *)current->journal_info; 4712 trans = (struct btrfs_trans_handle *)current->journal_info;
@@ -4759,7 +4727,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4759 while (delalloc_bytes && loops < 3) { 4727 while (delalloc_bytes && loops < 3) {
4760 max_reclaim = min(delalloc_bytes, to_reclaim); 4728 max_reclaim = min(delalloc_bytes, to_reclaim);
4761 nr_pages = max_reclaim >> PAGE_SHIFT; 4729 nr_pages = max_reclaim >> PAGE_SHIFT;
4762 btrfs_writeback_inodes_sb_nr(root, nr_pages, items); 4730 btrfs_writeback_inodes_sb_nr(fs_info, nr_pages, items);
4763 /* 4731 /*
4764 * We need to wait for the async pages to actually start before 4732 * We need to wait for the async pages to actually start before
4765 * we do anything. 4733 * we do anything.
@@ -4879,7 +4847,7 @@ static int flush_space(struct btrfs_root *root,
4879 case FLUSH_DELAYED_ITEMS_NR: 4847 case FLUSH_DELAYED_ITEMS_NR:
4880 case FLUSH_DELAYED_ITEMS: 4848 case FLUSH_DELAYED_ITEMS:
4881 if (state == FLUSH_DELAYED_ITEMS_NR) 4849 if (state == FLUSH_DELAYED_ITEMS_NR)
4882 nr = calc_reclaim_items_nr(root, num_bytes) * 2; 4850 nr = calc_reclaim_items_nr(fs_info, num_bytes) * 2;
4883 else 4851 else
4884 nr = -1; 4852 nr = -1;
4885 4853
@@ -4888,7 +4856,7 @@ static int flush_space(struct btrfs_root *root,
4888 ret = PTR_ERR(trans); 4856 ret = PTR_ERR(trans);
4889 break; 4857 break;
4890 } 4858 }
4891 ret = btrfs_run_delayed_items_nr(trans, root, nr); 4859 ret = btrfs_run_delayed_items_nr(trans, fs_info, nr);
4892 btrfs_end_transaction(trans, root); 4860 btrfs_end_transaction(trans, root);
4893 break; 4861 break;
4894 case FLUSH_DELALLOC: 4862 case FLUSH_DELALLOC:
@@ -4902,7 +4870,7 @@ static int flush_space(struct btrfs_root *root,
4902 ret = PTR_ERR(trans); 4870 ret = PTR_ERR(trans);
4903 break; 4871 break;
4904 } 4872 }
4905 ret = do_chunk_alloc(trans, fs_info->extent_root, 4873 ret = do_chunk_alloc(trans, fs_info,
4906 btrfs_get_alloc_profile(root, 0), 4874 btrfs_get_alloc_profile(root, 0),
4907 CHUNK_ALLOC_NO_FORCE); 4875 CHUNK_ALLOC_NO_FORCE);
4908 btrfs_end_transaction(trans, root); 4876 btrfs_end_transaction(trans, root);
@@ -5531,11 +5499,10 @@ void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
5531 rsv->type = type; 5499 rsv->type = type;
5532} 5500}
5533 5501
5534struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, 5502struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_fs_info *fs_info,
5535 unsigned short type) 5503 unsigned short type)
5536{ 5504{
5537 struct btrfs_block_rsv *block_rsv; 5505 struct btrfs_block_rsv *block_rsv;
5538 struct btrfs_fs_info *fs_info = root->fs_info;
5539 5506
5540 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); 5507 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
5541 if (!block_rsv) 5508 if (!block_rsv)
@@ -5547,12 +5514,12 @@ struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
5547 return block_rsv; 5514 return block_rsv;
5548} 5515}
5549 5516
5550void btrfs_free_block_rsv(struct btrfs_root *root, 5517void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
5551 struct btrfs_block_rsv *rsv) 5518 struct btrfs_block_rsv *rsv)
5552{ 5519{
5553 if (!rsv) 5520 if (!rsv)
5554 return; 5521 return;
5555 btrfs_block_rsv_release(root, rsv, (u64)-1); 5522 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
5556 kfree(rsv); 5523 kfree(rsv);
5557} 5524}
5558 5525
@@ -5579,8 +5546,7 @@ int btrfs_block_rsv_add(struct btrfs_root *root,
5579 return ret; 5546 return ret;
5580} 5547}
5581 5548
5582int btrfs_block_rsv_check(struct btrfs_root *root, 5549int btrfs_block_rsv_check(struct btrfs_block_rsv *block_rsv, int min_factor)
5583 struct btrfs_block_rsv *block_rsv, int min_factor)
5584{ 5550{
5585 u64 num_bytes = 0; 5551 u64 num_bytes = 0;
5586 int ret = -ENOSPC; 5552 int ret = -ENOSPC;
@@ -5627,11 +5593,10 @@ int btrfs_block_rsv_refill(struct btrfs_root *root,
5627 return ret; 5593 return ret;
5628} 5594}
5629 5595
5630void btrfs_block_rsv_release(struct btrfs_root *root, 5596void btrfs_block_rsv_release(struct btrfs_fs_info *fs_info,
5631 struct btrfs_block_rsv *block_rsv, 5597 struct btrfs_block_rsv *block_rsv,
5632 u64 num_bytes) 5598 u64 num_bytes)
5633{ 5599{
5634 struct btrfs_fs_info *fs_info = root->fs_info;
5635 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; 5600 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5636 5601
5637 if (global_rsv == block_rsv || 5602 if (global_rsv == block_rsv ||
@@ -5732,10 +5697,8 @@ static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5732} 5697}
5733 5698
5734void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, 5699void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
5735 struct btrfs_root *root) 5700 struct btrfs_fs_info *fs_info)
5736{ 5701{
5737 struct btrfs_fs_info *fs_info = root->fs_info;
5738
5739 if (!trans->block_rsv) 5702 if (!trans->block_rsv)
5740 return; 5703 return;
5741 5704
@@ -5744,7 +5707,8 @@ void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
5744 5707
5745 trace_btrfs_space_reservation(fs_info, "transaction", 5708 trace_btrfs_space_reservation(fs_info, "transaction",
5746 trans->transid, trans->bytes_reserved, 0); 5709 trans->transid, trans->bytes_reserved, 0);
5747 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); 5710 btrfs_block_rsv_release(fs_info, trans->block_rsv,
5711 trans->bytes_reserved);
5748 trans->bytes_reserved = 0; 5712 trans->bytes_reserved = 0;
5749} 5713}
5750 5714
@@ -5801,7 +5765,7 @@ void btrfs_orphan_release_metadata(struct inode *inode)
5801 5765
5802 trace_btrfs_space_reservation(fs_info, "orphan", 5766 trace_btrfs_space_reservation(fs_info, "orphan",
5803 btrfs_ino(inode), num_bytes, 0); 5767 btrfs_ino(inode), num_bytes, 0);
5804 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); 5768 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv, num_bytes);
5805} 5769}
5806 5770
5807/* 5771/*
@@ -5856,11 +5820,11 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5856 return ret; 5820 return ret;
5857} 5821}
5858 5822
5859void btrfs_subvolume_release_metadata(struct btrfs_root *root, 5823void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
5860 struct btrfs_block_rsv *rsv, 5824 struct btrfs_block_rsv *rsv,
5861 u64 qgroup_reserved) 5825 u64 qgroup_reserved)
5862{ 5826{
5863 btrfs_block_rsv_release(root, rsv, (u64)-1); 5827 btrfs_block_rsv_release(fs_info, rsv, (u64)-1);
5864} 5828}
5865 5829
5866/** 5830/**
@@ -5927,19 +5891,20 @@ static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5927 int reserve) 5891 int reserve)
5928{ 5892{
5929 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 5893 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5930 struct btrfs_root *root = BTRFS_I(inode)->root;
5931 u64 old_csums, num_csums; 5894 u64 old_csums, num_csums;
5932 5895
5933 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && 5896 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5934 BTRFS_I(inode)->csum_bytes == 0) 5897 BTRFS_I(inode)->csum_bytes == 0)
5935 return 0; 5898 return 0;
5936 5899
5937 old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes); 5900 old_csums = btrfs_csum_bytes_to_leaves(fs_info,
5901 BTRFS_I(inode)->csum_bytes);
5938 if (reserve) 5902 if (reserve)
5939 BTRFS_I(inode)->csum_bytes += num_bytes; 5903 BTRFS_I(inode)->csum_bytes += num_bytes;
5940 else 5904 else
5941 BTRFS_I(inode)->csum_bytes -= num_bytes; 5905 BTRFS_I(inode)->csum_bytes -= num_bytes;
5942 num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes); 5906 num_csums = btrfs_csum_bytes_to_leaves(fs_info,
5907 BTRFS_I(inode)->csum_bytes);
5943 5908
5944 /* No change, no need to reserve more */ 5909 /* No change, no need to reserve more */
5945 if (old_csums == num_csums) 5910 if (old_csums == num_csums)
@@ -6039,7 +6004,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
6039 trace_btrfs_space_reservation(fs_info, "delalloc", 6004 trace_btrfs_space_reservation(fs_info, "delalloc",
6040 btrfs_ino(inode), to_reserve, 1); 6005 btrfs_ino(inode), to_reserve, 1);
6041 if (release_extra) 6006 if (release_extra)
6042 btrfs_block_rsv_release(root, block_rsv, 6007 btrfs_block_rsv_release(fs_info, block_rsv,
6043 btrfs_calc_trans_metadata_size(fs_info, 1)); 6008 btrfs_calc_trans_metadata_size(fs_info, 1));
6044 return 0; 6009 return 0;
6045 6010
@@ -6098,7 +6063,7 @@ out_fail:
6098 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); 6063 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
6099 6064
6100 if (to_free) { 6065 if (to_free) {
6101 btrfs_block_rsv_release(root, block_rsv, to_free); 6066 btrfs_block_rsv_release(fs_info, block_rsv, to_free);
6102 trace_btrfs_space_reservation(fs_info, "delalloc", 6067 trace_btrfs_space_reservation(fs_info, "delalloc",
6103 btrfs_ino(inode), to_free, 0); 6068 btrfs_ino(inode), to_free, 0);
6104 } 6069 }
@@ -6119,7 +6084,6 @@ out_fail:
6119void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) 6084void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
6120{ 6085{
6121 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 6086 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6122 struct btrfs_root *root = BTRFS_I(inode)->root;
6123 u64 to_free = 0; 6087 u64 to_free = 0;
6124 unsigned dropped; 6088 unsigned dropped;
6125 6089
@@ -6139,7 +6103,7 @@ void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
6139 trace_btrfs_space_reservation(fs_info, "delalloc", 6103 trace_btrfs_space_reservation(fs_info, "delalloc",
6140 btrfs_ino(inode), to_free, 0); 6104 btrfs_ino(inode), to_free, 0);
6141 6105
6142 btrfs_block_rsv_release(root, &fs_info->delalloc_block_rsv, to_free); 6106 btrfs_block_rsv_release(fs_info, &fs_info->delalloc_block_rsv, to_free);
6143} 6107}
6144 6108
6145/** 6109/**
@@ -6309,9 +6273,8 @@ static int update_block_group(struct btrfs_trans_handle *trans,
6309 return 0; 6273 return 0;
6310} 6274}
6311 6275
6312static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) 6276static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
6313{ 6277{
6314 struct btrfs_fs_info *fs_info = root->fs_info;
6315 struct btrfs_block_group_cache *cache; 6278 struct btrfs_block_group_cache *cache;
6316 u64 bytenr; 6279 u64 bytenr;
6317 6280
@@ -6332,12 +6295,10 @@ static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
6332 return bytenr; 6295 return bytenr;
6333} 6296}
6334 6297
6335static int pin_down_extent(struct btrfs_root *root, 6298static int pin_down_extent(struct btrfs_fs_info *fs_info,
6336 struct btrfs_block_group_cache *cache, 6299 struct btrfs_block_group_cache *cache,
6337 u64 bytenr, u64 num_bytes, int reserved) 6300 u64 bytenr, u64 num_bytes, int reserved)
6338{ 6301{
6339 struct btrfs_fs_info *fs_info = cache->fs_info;
6340
6341 spin_lock(&cache->space_info->lock); 6302 spin_lock(&cache->space_info->lock);
6342 spin_lock(&cache->lock); 6303 spin_lock(&cache->lock);
6343 cache->pinned += num_bytes; 6304 cache->pinned += num_bytes;
@@ -6359,16 +6320,15 @@ static int pin_down_extent(struct btrfs_root *root,
6359/* 6320/*
6360 * this function must be called within transaction 6321 * this function must be called within transaction
6361 */ 6322 */
6362int btrfs_pin_extent(struct btrfs_root *root, 6323int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
6363 u64 bytenr, u64 num_bytes, int reserved) 6324 u64 bytenr, u64 num_bytes, int reserved)
6364{ 6325{
6365 struct btrfs_fs_info *fs_info = root->fs_info;
6366 struct btrfs_block_group_cache *cache; 6326 struct btrfs_block_group_cache *cache;
6367 6327
6368 cache = btrfs_lookup_block_group(fs_info, bytenr); 6328 cache = btrfs_lookup_block_group(fs_info, bytenr);
6369 BUG_ON(!cache); /* Logic error */ 6329 BUG_ON(!cache); /* Logic error */
6370 6330
6371 pin_down_extent(root, cache, bytenr, num_bytes, reserved); 6331 pin_down_extent(fs_info, cache, bytenr, num_bytes, reserved);
6372 6332
6373 btrfs_put_block_group(cache); 6333 btrfs_put_block_group(cache);
6374 return 0; 6334 return 0;
@@ -6377,10 +6337,9 @@ int btrfs_pin_extent(struct btrfs_root *root,
6377/* 6337/*
6378 * this function must be called within transaction 6338 * this function must be called within transaction
6379 */ 6339 */
6380int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, 6340int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
6381 u64 bytenr, u64 num_bytes) 6341 u64 bytenr, u64 num_bytes)
6382{ 6342{
6383 struct btrfs_fs_info *fs_info = root->fs_info;
6384 struct btrfs_block_group_cache *cache; 6343 struct btrfs_block_group_cache *cache;
6385 int ret; 6344 int ret;
6386 6345
@@ -6396,7 +6355,7 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
6396 */ 6355 */
6397 cache_block_group(cache, 1); 6356 cache_block_group(cache, 1);
6398 6357
6399 pin_down_extent(root, cache, bytenr, num_bytes, 0); 6358 pin_down_extent(fs_info, cache, bytenr, num_bytes, 0);
6400 6359
6401 /* remove us from the free space cache (if we're there at all) */ 6360 /* remove us from the free space cache (if we're there at all) */
6402 ret = btrfs_remove_free_space(cache, bytenr, num_bytes); 6361 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
@@ -6404,9 +6363,9 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
6404 return ret; 6363 return ret;
6405} 6364}
6406 6365
6407static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes) 6366static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
6367 u64 start, u64 num_bytes)
6408{ 6368{
6409 struct btrfs_fs_info *fs_info = root->fs_info;
6410 int ret; 6369 int ret;
6411 struct btrfs_block_group_cache *block_group; 6370 struct btrfs_block_group_cache *block_group;
6412 struct btrfs_caching_control *caching_ctl; 6371 struct btrfs_caching_control *caching_ctl;
@@ -6426,7 +6385,7 @@ static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_b
6426 mutex_lock(&caching_ctl->mutex); 6385 mutex_lock(&caching_ctl->mutex);
6427 6386
6428 if (start >= caching_ctl->progress) { 6387 if (start >= caching_ctl->progress) {
6429 ret = add_excluded_extent(root, start, num_bytes); 6388 ret = add_excluded_extent(fs_info, start, num_bytes);
6430 } else if (start + num_bytes <= caching_ctl->progress) { 6389 } else if (start + num_bytes <= caching_ctl->progress) {
6431 ret = btrfs_remove_free_space(block_group, 6390 ret = btrfs_remove_free_space(block_group,
6432 start, num_bytes); 6391 start, num_bytes);
@@ -6440,7 +6399,7 @@ static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_b
6440 num_bytes = (start + num_bytes) - 6399 num_bytes = (start + num_bytes) -
6441 caching_ctl->progress; 6400 caching_ctl->progress;
6442 start = caching_ctl->progress; 6401 start = caching_ctl->progress;
6443 ret = add_excluded_extent(root, start, num_bytes); 6402 ret = add_excluded_extent(fs_info, start, num_bytes);
6444 } 6403 }
6445out_lock: 6404out_lock:
6446 mutex_unlock(&caching_ctl->mutex); 6405 mutex_unlock(&caching_ctl->mutex);
@@ -6450,7 +6409,7 @@ out_lock:
6450 return ret; 6409 return ret;
6451} 6410}
6452 6411
6453int btrfs_exclude_logged_extents(struct btrfs_root *log, 6412int btrfs_exclude_logged_extents(struct btrfs_fs_info *fs_info,
6454 struct extent_buffer *eb) 6413 struct extent_buffer *eb)
6455{ 6414{
6456 struct btrfs_file_extent_item *item; 6415 struct btrfs_file_extent_item *item;
@@ -6458,7 +6417,7 @@ int btrfs_exclude_logged_extents(struct btrfs_root *log,
6458 int found_type; 6417 int found_type;
6459 int i; 6418 int i;
6460 6419
6461 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) 6420 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
6462 return 0; 6421 return 0;
6463 6422
6464 for (i = 0; i < btrfs_header_nritems(eb); i++) { 6423 for (i = 0; i < btrfs_header_nritems(eb); i++) {
@@ -6473,7 +6432,7 @@ int btrfs_exclude_logged_extents(struct btrfs_root *log,
6473 continue; 6432 continue;
6474 key.objectid = btrfs_file_extent_disk_bytenr(eb, item); 6433 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
6475 key.offset = btrfs_file_extent_disk_num_bytes(eb, item); 6434 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
6476 __exclude_logged_extent(log, key.objectid, key.offset); 6435 __exclude_logged_extent(fs_info, key.objectid, key.offset);
6477 } 6436 }
6478 6437
6479 return 0; 6438 return 0;
@@ -6600,9 +6559,8 @@ static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6600 return ret; 6559 return ret;
6601} 6560}
6602void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 6561void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
6603 struct btrfs_root *root) 6562 struct btrfs_fs_info *fs_info)
6604{ 6563{
6605 struct btrfs_fs_info *fs_info = root->fs_info;
6606 struct btrfs_caching_control *next; 6564 struct btrfs_caching_control *next;
6607 struct btrfs_caching_control *caching_ctl; 6565 struct btrfs_caching_control *caching_ctl;
6608 struct btrfs_block_group_cache *cache; 6566 struct btrfs_block_group_cache *cache;
@@ -6636,10 +6594,9 @@ void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
6636 * what it should be based on the mount options. 6594 * what it should be based on the mount options.
6637 */ 6595 */
6638static struct btrfs_free_cluster * 6596static struct btrfs_free_cluster *
6639fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info, 6597fetch_cluster_info(struct btrfs_fs_info *fs_info,
6640 u64 *empty_cluster) 6598 struct btrfs_space_info *space_info, u64 *empty_cluster)
6641{ 6599{
6642 struct btrfs_fs_info *fs_info = root->fs_info;
6643 struct btrfs_free_cluster *ret = NULL; 6600 struct btrfs_free_cluster *ret = NULL;
6644 bool ssd = btrfs_test_opt(fs_info, SSD); 6601 bool ssd = btrfs_test_opt(fs_info, SSD);
6645 6602
@@ -6660,10 +6617,10 @@ fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info,
6660 return ret; 6617 return ret;
6661} 6618}
6662 6619
6663static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end, 6620static int unpin_extent_range(struct btrfs_fs_info *fs_info,
6621 u64 start, u64 end,
6664 const bool return_free_space) 6622 const bool return_free_space)
6665{ 6623{
6666 struct btrfs_fs_info *fs_info = root->fs_info;
6667 struct btrfs_block_group_cache *cache = NULL; 6624 struct btrfs_block_group_cache *cache = NULL;
6668 struct btrfs_space_info *space_info; 6625 struct btrfs_space_info *space_info;
6669 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; 6626 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
@@ -6683,7 +6640,7 @@ static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
6683 cache = btrfs_lookup_block_group(fs_info, start); 6640 cache = btrfs_lookup_block_group(fs_info, start);
6684 BUG_ON(!cache); /* Logic error */ 6641 BUG_ON(!cache); /* Logic error */
6685 6642
6686 cluster = fetch_cluster_info(root, 6643 cluster = fetch_cluster_info(fs_info,
6687 cache->space_info, 6644 cache->space_info,
6688 &empty_cluster); 6645 &empty_cluster);
6689 empty_cluster <<= 1; 6646 empty_cluster <<= 1;
@@ -6762,9 +6719,8 @@ static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
6762} 6719}
6763 6720
6764int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, 6721int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
6765 struct btrfs_root *root) 6722 struct btrfs_fs_info *fs_info)
6766{ 6723{
6767 struct btrfs_fs_info *fs_info = root->fs_info;
6768 struct btrfs_block_group_cache *block_group, *tmp; 6724 struct btrfs_block_group_cache *block_group, *tmp;
6769 struct list_head *deleted_bgs; 6725 struct list_head *deleted_bgs;
6770 struct extent_io_tree *unpin; 6726 struct extent_io_tree *unpin;
@@ -6787,11 +6743,11 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
6787 } 6743 }
6788 6744
6789 if (btrfs_test_opt(fs_info, DISCARD)) 6745 if (btrfs_test_opt(fs_info, DISCARD))
6790 ret = btrfs_discard_extent(root, start, 6746 ret = btrfs_discard_extent(fs_info, start,
6791 end + 1 - start, NULL); 6747 end + 1 - start, NULL);
6792 6748
6793 clear_extent_dirty(unpin, start, end); 6749 clear_extent_dirty(unpin, start, end);
6794 unpin_extent_range(root, start, end, true); 6750 unpin_extent_range(fs_info, start, end, true);
6795 mutex_unlock(&fs_info->unused_bg_unpin_mutex); 6751 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
6796 cond_resched(); 6752 cond_resched();
6797 } 6753 }
@@ -6807,7 +6763,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
6807 6763
6808 ret = -EROFS; 6764 ret = -EROFS;
6809 if (!trans->aborted) 6765 if (!trans->aborted)
6810 ret = btrfs_discard_extent(root, 6766 ret = btrfs_discard_extent(fs_info,
6811 block_group->key.objectid, 6767 block_group->key.objectid,
6812 block_group->key.offset, 6768 block_group->key.offset,
6813 &trimmed); 6769 &trimmed);
@@ -6849,7 +6805,7 @@ static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
6849 6805
6850 6806
6851static int __btrfs_free_extent(struct btrfs_trans_handle *trans, 6807static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6852 struct btrfs_root *root, 6808 struct btrfs_fs_info *info,
6853 struct btrfs_delayed_ref_node *node, u64 parent, 6809 struct btrfs_delayed_ref_node *node, u64 parent,
6854 u64 root_objectid, u64 owner_objectid, 6810 u64 root_objectid, u64 owner_objectid,
6855 u64 owner_offset, int refs_to_drop, 6811 u64 owner_offset, int refs_to_drop,
@@ -6857,7 +6813,6 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6857{ 6813{
6858 struct btrfs_key key; 6814 struct btrfs_key key;
6859 struct btrfs_path *path; 6815 struct btrfs_path *path;
6860 struct btrfs_fs_info *info = root->fs_info;
6861 struct btrfs_root *extent_root = info->extent_root; 6816 struct btrfs_root *extent_root = info->extent_root;
6862 struct extent_buffer *leaf; 6817 struct extent_buffer *leaf;
6863 struct btrfs_extent_item *ei; 6818 struct btrfs_extent_item *ei;
@@ -6969,8 +6924,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6969 "umm, got %d back from search, was looking for %llu", 6924 "umm, got %d back from search, was looking for %llu",
6970 ret, bytenr); 6925 ret, bytenr);
6971 if (ret > 0) 6926 if (ret > 0)
6972 btrfs_print_leaf(extent_root, 6927 btrfs_print_leaf(info, path->nodes[0]);
6973 path->nodes[0]);
6974 } 6928 }
6975 if (ret < 0) { 6929 if (ret < 0) {
6976 btrfs_abort_transaction(trans, ret); 6930 btrfs_abort_transaction(trans, ret);
@@ -6979,7 +6933,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6979 extent_slot = path->slots[0]; 6933 extent_slot = path->slots[0];
6980 } 6934 }
6981 } else if (WARN_ON(ret == -ENOENT)) { 6935 } else if (WARN_ON(ret == -ENOENT)) {
6982 btrfs_print_leaf(extent_root, path->nodes[0]); 6936 btrfs_print_leaf(info, path->nodes[0]);
6983 btrfs_err(info, 6937 btrfs_err(info,
6984 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", 6938 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
6985 bytenr, parent, root_objectid, owner_objectid, 6939 bytenr, parent, root_objectid, owner_objectid,
@@ -7016,7 +6970,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
7016 btrfs_err(info, 6970 btrfs_err(info,
7017 "umm, got %d back from search, was looking for %llu", 6971 "umm, got %d back from search, was looking for %llu",
7018 ret, bytenr); 6972 ret, bytenr);
7019 btrfs_print_leaf(extent_root, path->nodes[0]); 6973 btrfs_print_leaf(info, path->nodes[0]);
7020 } 6974 }
7021 if (ret < 0) { 6975 if (ret < 0) {
7022 btrfs_abort_transaction(trans, ret); 6976 btrfs_abort_transaction(trans, ret);
@@ -7130,7 +7084,7 @@ out:
7130 * removes it from the tree. 7084 * removes it from the tree.
7131 */ 7085 */
7132static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, 7086static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
7133 struct btrfs_root *root, u64 bytenr) 7087 u64 bytenr)
7134{ 7088{
7135 struct btrfs_delayed_ref_head *head; 7089 struct btrfs_delayed_ref_head *head;
7136 struct btrfs_delayed_ref_root *delayed_refs; 7090 struct btrfs_delayed_ref_root *delayed_refs;
@@ -7221,7 +7175,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7221 struct btrfs_block_group_cache *cache; 7175 struct btrfs_block_group_cache *cache;
7222 7176
7223 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 7177 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
7224 ret = check_ref_cleanup(trans, root, buf->start); 7178 ret = check_ref_cleanup(trans, buf->start);
7225 if (!ret) 7179 if (!ret)
7226 goto out; 7180 goto out;
7227 } 7181 }
@@ -7229,7 +7183,8 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7229 cache = btrfs_lookup_block_group(fs_info, buf->start); 7183 cache = btrfs_lookup_block_group(fs_info, buf->start);
7230 7184
7231 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 7185 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
7232 pin_down_extent(root, cache, buf->start, buf->len, 1); 7186 pin_down_extent(fs_info, cache, buf->start,
7187 buf->len, 1);
7233 btrfs_put_block_group(cache); 7188 btrfs_put_block_group(cache);
7234 goto out; 7189 goto out;
7235 } 7190 }
@@ -7255,12 +7210,12 @@ out:
7255} 7210}
7256 7211
7257/* Can return -ENOMEM */ 7212/* Can return -ENOMEM */
7258int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, 7213int btrfs_free_extent(struct btrfs_trans_handle *trans,
7214 struct btrfs_fs_info *fs_info,
7259 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, 7215 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
7260 u64 owner, u64 offset) 7216 u64 owner, u64 offset)
7261{ 7217{
7262 int ret; 7218 int ret;
7263 struct btrfs_fs_info *fs_info = root->fs_info;
7264 7219
7265 if (btrfs_is_testing(fs_info)) 7220 if (btrfs_is_testing(fs_info))
7266 return 0; 7221 return 0;
@@ -7274,7 +7229,7 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7274 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { 7229 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
7275 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); 7230 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
7276 /* unlocks the pinned mutex */ 7231 /* unlocks the pinned mutex */
7277 btrfs_pin_extent(root, bytenr, num_bytes, 1); 7232 btrfs_pin_extent(fs_info, bytenr, num_bytes, 1);
7278 ret = 0; 7233 ret = 0;
7279 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { 7234 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
7280 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, 7235 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
@@ -7519,7 +7474,7 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7519 spin_unlock(&space_info->lock); 7474 spin_unlock(&space_info->lock);
7520 } 7475 }
7521 7476
7522 last_ptr = fetch_cluster_info(orig_root, space_info, &empty_cluster); 7477 last_ptr = fetch_cluster_info(fs_info, space_info, &empty_cluster);
7523 if (last_ptr) { 7478 if (last_ptr) {
7524 spin_lock(&last_ptr->lock); 7479 spin_lock(&last_ptr->lock);
7525 if (last_ptr->block_group) 7480 if (last_ptr->block_group)
@@ -7536,7 +7491,7 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7536 spin_unlock(&last_ptr->lock); 7491 spin_unlock(&last_ptr->lock);
7537 } 7492 }
7538 7493
7539 search_start = max(search_start, first_logical_byte(root, 0)); 7494 search_start = max(search_start, first_logical_byte(fs_info, 0));
7540 search_start = max(search_start, hint_byte); 7495 search_start = max(search_start, hint_byte);
7541 if (search_start == hint_byte) { 7496 if (search_start == hint_byte) {
7542 block_group = btrfs_lookup_block_group(fs_info, search_start); 7497 block_group = btrfs_lookup_block_group(fs_info, search_start);
@@ -7703,7 +7658,7 @@ refill_cluster:
7703 block_group->full_stripe_len); 7658 block_group->full_stripe_len);
7704 7659
7705 /* allocate a cluster in this block group */ 7660 /* allocate a cluster in this block group */
7706 ret = btrfs_find_space_cluster(root, block_group, 7661 ret = btrfs_find_space_cluster(fs_info, block_group,
7707 last_ptr, search_start, 7662 last_ptr, search_start,
7708 num_bytes, 7663 num_bytes,
7709 aligned_cluster); 7664 aligned_cluster);
@@ -7879,7 +7834,7 @@ loop:
7879 goto out; 7834 goto out;
7880 } 7835 }
7881 7836
7882 ret = do_chunk_alloc(trans, root, flags, 7837 ret = do_chunk_alloc(trans, fs_info, flags,
7883 CHUNK_ALLOC_FORCE); 7838 CHUNK_ALLOC_FORCE);
7884 7839
7885 /* 7840 /*
@@ -8021,11 +7976,10 @@ again:
8021 return ret; 7976 return ret;
8022} 7977}
8023 7978
8024static int __btrfs_free_reserved_extent(struct btrfs_root *root, 7979static int __btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
8025 u64 start, u64 len, 7980 u64 start, u64 len,
8026 int pin, int delalloc) 7981 int pin, int delalloc)
8027{ 7982{
8028 struct btrfs_fs_info *fs_info = root->fs_info;
8029 struct btrfs_block_group_cache *cache; 7983 struct btrfs_block_group_cache *cache;
8030 int ret = 0; 7984 int ret = 0;
8031 7985
@@ -8037,10 +7991,10 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
8037 } 7991 }
8038 7992
8039 if (pin) 7993 if (pin)
8040 pin_down_extent(root, cache, start, len, 1); 7994 pin_down_extent(fs_info, cache, start, len, 1);
8041 else { 7995 else {
8042 if (btrfs_test_opt(fs_info, DISCARD)) 7996 if (btrfs_test_opt(fs_info, DISCARD))
8043 ret = btrfs_discard_extent(root, start, len, NULL); 7997 ret = btrfs_discard_extent(fs_info, start, len, NULL);
8044 btrfs_add_free_space(cache, start, len); 7998 btrfs_add_free_space(cache, start, len);
8045 btrfs_free_reserved_bytes(cache, len, delalloc); 7999 btrfs_free_reserved_bytes(cache, len, delalloc);
8046 trace_btrfs_reserved_extent_free(fs_info, start, len); 8000 trace_btrfs_reserved_extent_free(fs_info, start, len);
@@ -8050,26 +8004,25 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
8050 return ret; 8004 return ret;
8051} 8005}
8052 8006
8053int btrfs_free_reserved_extent(struct btrfs_root *root, 8007int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
8054 u64 start, u64 len, int delalloc) 8008 u64 start, u64 len, int delalloc)
8055{ 8009{
8056 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc); 8010 return __btrfs_free_reserved_extent(fs_info, start, len, 0, delalloc);
8057} 8011}
8058 8012
8059int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, 8013int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
8060 u64 start, u64 len) 8014 u64 start, u64 len)
8061{ 8015{
8062 return __btrfs_free_reserved_extent(root, start, len, 1, 0); 8016 return __btrfs_free_reserved_extent(fs_info, start, len, 1, 0);
8063} 8017}
8064 8018
8065static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, 8019static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
8066 struct btrfs_root *root, 8020 struct btrfs_fs_info *fs_info,
8067 u64 parent, u64 root_objectid, 8021 u64 parent, u64 root_objectid,
8068 u64 flags, u64 owner, u64 offset, 8022 u64 flags, u64 owner, u64 offset,
8069 struct btrfs_key *ins, int ref_mod) 8023 struct btrfs_key *ins, int ref_mod)
8070{ 8024{
8071 int ret; 8025 int ret;
8072 struct btrfs_fs_info *fs_info = root->fs_info;
8073 struct btrfs_extent_item *extent_item; 8026 struct btrfs_extent_item *extent_item;
8074 struct btrfs_extent_inline_ref *iref; 8027 struct btrfs_extent_inline_ref *iref;
8075 struct btrfs_path *path; 8028 struct btrfs_path *path;
@@ -8139,13 +8092,12 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
8139} 8092}
8140 8093
8141static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, 8094static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8142 struct btrfs_root *root, 8095 struct btrfs_fs_info *fs_info,
8143 u64 parent, u64 root_objectid, 8096 u64 parent, u64 root_objectid,
8144 u64 flags, struct btrfs_disk_key *key, 8097 u64 flags, struct btrfs_disk_key *key,
8145 int level, struct btrfs_key *ins) 8098 int level, struct btrfs_key *ins)
8146{ 8099{
8147 int ret; 8100 int ret;
8148 struct btrfs_fs_info *fs_info = root->fs_info;
8149 struct btrfs_extent_item *extent_item; 8101 struct btrfs_extent_item *extent_item;
8150 struct btrfs_tree_block_info *block_info; 8102 struct btrfs_tree_block_info *block_info;
8151 struct btrfs_extent_inline_ref *iref; 8103 struct btrfs_extent_inline_ref *iref;
@@ -8160,7 +8112,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8160 8112
8161 path = btrfs_alloc_path(); 8113 path = btrfs_alloc_path();
8162 if (!path) { 8114 if (!path) {
8163 btrfs_free_and_pin_reserved_extent(root, ins->objectid, 8115 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
8164 fs_info->nodesize); 8116 fs_info->nodesize);
8165 return -ENOMEM; 8117 return -ENOMEM;
8166 } 8118 }
@@ -8170,7 +8122,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8170 ins, size); 8122 ins, size);
8171 if (ret) { 8123 if (ret) {
8172 btrfs_free_path(path); 8124 btrfs_free_path(path);
8173 btrfs_free_and_pin_reserved_extent(root, ins->objectid, 8125 btrfs_free_and_pin_reserved_extent(fs_info, ins->objectid,
8174 fs_info->nodesize); 8126 fs_info->nodesize);
8175 return ret; 8127 return ret;
8176 } 8128 }
@@ -8226,12 +8178,11 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8226} 8178}
8227 8179
8228int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, 8180int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
8229 struct btrfs_root *root,
8230 u64 root_objectid, u64 owner, 8181 u64 root_objectid, u64 owner,
8231 u64 offset, u64 ram_bytes, 8182 u64 offset, u64 ram_bytes,
8232 struct btrfs_key *ins) 8183 struct btrfs_key *ins)
8233{ 8184{
8234 struct btrfs_fs_info *fs_info = root->fs_info; 8185 struct btrfs_fs_info *fs_info = trans->fs_info;
8235 int ret; 8186 int ret;
8236 8187
8237 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); 8188 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
@@ -8250,11 +8201,10 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
8250 * space cache bits as well 8201 * space cache bits as well
8251 */ 8202 */
8252int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, 8203int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8253 struct btrfs_root *root, 8204 struct btrfs_fs_info *fs_info,
8254 u64 root_objectid, u64 owner, u64 offset, 8205 u64 root_objectid, u64 owner, u64 offset,
8255 struct btrfs_key *ins) 8206 struct btrfs_key *ins)
8256{ 8207{
8257 struct btrfs_fs_info *fs_info = root->fs_info;
8258 int ret; 8208 int ret;
8259 struct btrfs_block_group_cache *block_group; 8209 struct btrfs_block_group_cache *block_group;
8260 struct btrfs_space_info *space_info; 8210 struct btrfs_space_info *space_info;
@@ -8264,7 +8214,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8264 * need to do the exclude dance if this fs isn't mixed. 8214 * need to do the exclude dance if this fs isn't mixed.
8265 */ 8215 */
8266 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { 8216 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
8267 ret = __exclude_logged_extent(root, ins->objectid, ins->offset); 8217 ret = __exclude_logged_extent(fs_info, ins->objectid,
8218 ins->offset);
8268 if (ret) 8219 if (ret)
8269 return ret; 8220 return ret;
8270 } 8221 }
@@ -8281,7 +8232,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8281 spin_unlock(&block_group->lock); 8232 spin_unlock(&block_group->lock);
8282 spin_unlock(&space_info->lock); 8233 spin_unlock(&space_info->lock);
8283 8234
8284 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 8235 ret = alloc_reserved_file_extent(trans, fs_info, 0, root_objectid,
8285 0, owner, offset, ins, 1); 8236 0, owner, offset, ins, 1);
8286 btrfs_put_block_group(block_group); 8237 btrfs_put_block_group(block_group);
8287 return ret; 8238 return ret;
@@ -8294,7 +8245,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
8294 struct btrfs_fs_info *fs_info = root->fs_info; 8245 struct btrfs_fs_info *fs_info = root->fs_info;
8295 struct extent_buffer *buf; 8246 struct extent_buffer *buf;
8296 8247
8297 buf = btrfs_find_create_tree_block(root, bytenr); 8248 buf = btrfs_find_create_tree_block(fs_info, bytenr);
8298 if (IS_ERR(buf)) 8249 if (IS_ERR(buf))
8299 return buf; 8250 return buf;
8300 8251
@@ -8474,7 +8425,7 @@ out_free_delayed:
8474out_free_buf: 8425out_free_buf:
8475 free_extent_buffer(buf); 8426 free_extent_buffer(buf);
8476out_free_reserved: 8427out_free_reserved:
8477 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0); 8428 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
8478out_unuse: 8429out_unuse:
8479 unuse_block_rsv(fs_info, block_rsv, blocksize); 8430 unuse_block_rsv(fs_info, block_rsv, blocksize);
8480 return ERR_PTR(ret); 8431 return ERR_PTR(ret);
@@ -8542,7 +8493,7 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8542 continue; 8493 continue;
8543 8494
8544 /* We don't lock the tree block, it's OK to be racy here */ 8495 /* We don't lock the tree block, it's OK to be racy here */
8545 ret = btrfs_lookup_extent_info(trans, root, bytenr, 8496 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
8546 wc->level - 1, 1, &refs, 8497 wc->level - 1, 1, &refs,
8547 &flags); 8498 &flags);
8548 /* We don't care about errors in readahead. */ 8499 /* We don't care about errors in readahead. */
@@ -8571,7 +8522,7 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8571 continue; 8522 continue;
8572 } 8523 }
8573reada: 8524reada:
8574 readahead_tree_block(root, bytenr); 8525 readahead_tree_block(fs_info, bytenr);
8575 nread++; 8526 nread++;
8576 } 8527 }
8577 wc->reada_slot = slot; 8528 wc->reada_slot = slot;
@@ -8590,6 +8541,7 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8590 struct btrfs_path *path, 8541 struct btrfs_path *path,
8591 struct walk_control *wc, int lookup_info) 8542 struct walk_control *wc, int lookup_info)
8592{ 8543{
8544 struct btrfs_fs_info *fs_info = root->fs_info;
8593 int level = wc->level; 8545 int level = wc->level;
8594 struct extent_buffer *eb = path->nodes[level]; 8546 struct extent_buffer *eb = path->nodes[level];
8595 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; 8547 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
@@ -8607,7 +8559,7 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8607 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || 8559 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
8608 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { 8560 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
8609 BUG_ON(!path->locks[level]); 8561 BUG_ON(!path->locks[level]);
8610 ret = btrfs_lookup_extent_info(trans, root, 8562 ret = btrfs_lookup_extent_info(trans, fs_info,
8611 eb->start, level, 1, 8563 eb->start, level, 1,
8612 &wc->refs[level], 8564 &wc->refs[level],
8613 &wc->flags[level]); 8565 &wc->flags[level]);
@@ -8635,7 +8587,7 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
8635 BUG_ON(ret); /* -ENOMEM */ 8587 BUG_ON(ret); /* -ENOMEM */
8636 ret = btrfs_dec_ref(trans, root, eb, 0); 8588 ret = btrfs_dec_ref(trans, root, eb, 0);
8637 BUG_ON(ret); /* -ENOMEM */ 8589 BUG_ON(ret); /* -ENOMEM */
8638 ret = btrfs_set_disk_extent_flags(trans, root, eb->start, 8590 ret = btrfs_set_disk_extent_flags(trans, fs_info, eb->start,
8639 eb->len, flag, 8591 eb->len, flag,
8640 btrfs_header_level(eb), 0); 8592 btrfs_header_level(eb), 0);
8641 BUG_ON(ret); /* -ENOMEM */ 8593 BUG_ON(ret); /* -ENOMEM */
@@ -8701,7 +8653,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8701 8653
8702 next = find_extent_buffer(fs_info, bytenr); 8654 next = find_extent_buffer(fs_info, bytenr);
8703 if (!next) { 8655 if (!next) {
8704 next = btrfs_find_create_tree_block(root, bytenr); 8656 next = btrfs_find_create_tree_block(fs_info, bytenr);
8705 if (IS_ERR(next)) 8657 if (IS_ERR(next))
8706 return PTR_ERR(next); 8658 return PTR_ERR(next);
8707 8659
@@ -8712,7 +8664,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8712 btrfs_tree_lock(next); 8664 btrfs_tree_lock(next);
8713 btrfs_set_lock_blocking(next); 8665 btrfs_set_lock_blocking(next);
8714 8666
8715 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1, 8667 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
8716 &wc->refs[level - 1], 8668 &wc->refs[level - 1],
8717 &wc->flags[level - 1]); 8669 &wc->flags[level - 1]);
8718 if (ret < 0) 8670 if (ret < 0)
@@ -8761,7 +8713,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8761 if (!next) { 8713 if (!next) {
8762 if (reada && level == 1) 8714 if (reada && level == 1)
8763 reada_walk_down(trans, root, wc, path); 8715 reada_walk_down(trans, root, wc, path);
8764 next = read_tree_block(root, bytenr, generation); 8716 next = read_tree_block(fs_info, bytenr, generation);
8765 if (IS_ERR(next)) { 8717 if (IS_ERR(next)) {
8766 return PTR_ERR(next); 8718 return PTR_ERR(next);
8767 } else if (!extent_buffer_uptodate(next)) { 8719 } else if (!extent_buffer_uptodate(next)) {
@@ -8814,8 +8766,9 @@ skip:
8814 ret); 8766 ret);
8815 } 8767 }
8816 } 8768 }
8817 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, 8769 ret = btrfs_free_extent(trans, fs_info, bytenr, blocksize,
8818 root->root_key.objectid, level - 1, 0); 8770 parent, root->root_key.objectid,
8771 level - 1, 0);
8819 if (ret) 8772 if (ret)
8820 goto out_unlock; 8773 goto out_unlock;
8821 } 8774 }
@@ -8877,7 +8830,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8877 btrfs_set_lock_blocking(eb); 8830 btrfs_set_lock_blocking(eb);
8878 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; 8831 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8879 8832
8880 ret = btrfs_lookup_extent_info(trans, root, 8833 ret = btrfs_lookup_extent_info(trans, fs_info,
8881 eb->start, level, 1, 8834 eb->start, level, 1,
8882 &wc->refs[level], 8835 &wc->refs[level],
8883 &wc->flags[level]); 8836 &wc->flags[level]);
@@ -8905,7 +8858,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8905 else 8858 else
8906 ret = btrfs_dec_ref(trans, root, eb, 0); 8859 ret = btrfs_dec_ref(trans, root, eb, 0);
8907 BUG_ON(ret); /* -ENOMEM */ 8860 BUG_ON(ret); /* -ENOMEM */
8908 ret = btrfs_qgroup_trace_leaf_items(trans, root, eb); 8861 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, eb);
8909 if (ret) { 8862 if (ret) {
8910 btrfs_err_rl(fs_info, 8863 btrfs_err_rl(fs_info,
8911 "error %d accounting leaf items. Quota is out of sync, rescan required.", 8864 "error %d accounting leaf items. Quota is out of sync, rescan required.",
@@ -9097,7 +9050,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
9097 btrfs_set_lock_blocking(path->nodes[level]); 9050 btrfs_set_lock_blocking(path->nodes[level]);
9098 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; 9051 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
9099 9052
9100 ret = btrfs_lookup_extent_info(trans, root, 9053 ret = btrfs_lookup_extent_info(trans, fs_info,
9101 path->nodes[level]->start, 9054 path->nodes[level]->start,
9102 level, 1, &wc->refs[level], 9055 level, 1, &wc->refs[level],
9103 &wc->flags[level]); 9056 &wc->flags[level]);
@@ -9154,7 +9107,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
9154 9107
9155 BUG_ON(wc->level == 0); 9108 BUG_ON(wc->level == 0);
9156 if (btrfs_should_end_transaction(trans, tree_root) || 9109 if (btrfs_should_end_transaction(trans, tree_root) ||
9157 (!for_reloc && btrfs_need_cleaner_sleep(root))) { 9110 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
9158 ret = btrfs_update_root(trans, tree_root, 9111 ret = btrfs_update_root(trans, tree_root,
9159 &root->root_key, 9112 &root->root_key,
9160 root_item); 9113 root_item);
@@ -9165,7 +9118,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
9165 } 9118 }
9166 9119
9167 btrfs_end_transaction_throttle(trans, tree_root); 9120 btrfs_end_transaction_throttle(trans, tree_root);
9168 if (!for_reloc && btrfs_need_cleaner_sleep(root)) { 9121 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
9169 btrfs_debug(fs_info, 9122 btrfs_debug(fs_info,
9170 "drop snapshot early exit"); 9123 "drop snapshot early exit");
9171 err = -EAGAIN; 9124 err = -EAGAIN;
@@ -9429,7 +9382,7 @@ again:
9429 mutex_unlock(&fs_info->ro_block_group_mutex); 9382 mutex_unlock(&fs_info->ro_block_group_mutex);
9430 btrfs_end_transaction(trans, root); 9383 btrfs_end_transaction(trans, root);
9431 9384
9432 ret = btrfs_wait_for_commit(root, transid); 9385 ret = btrfs_wait_for_commit(fs_info, transid);
9433 if (ret) 9386 if (ret)
9434 return ret; 9387 return ret;
9435 goto again; 9388 goto again;
@@ -9441,7 +9394,7 @@ again:
9441 */ 9394 */
9442 alloc_flags = update_block_group_flags(fs_info, cache->flags); 9395 alloc_flags = update_block_group_flags(fs_info, cache->flags);
9443 if (alloc_flags != cache->flags) { 9396 if (alloc_flags != cache->flags) {
9444 ret = do_chunk_alloc(trans, root, alloc_flags, 9397 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
9445 CHUNK_ALLOC_FORCE); 9398 CHUNK_ALLOC_FORCE);
9446 /* 9399 /*
9447 * ENOSPC is allowed here, we may have enough space 9400 * ENOSPC is allowed here, we may have enough space
@@ -9457,8 +9410,8 @@ again:
9457 ret = inc_block_group_ro(cache, 0); 9410 ret = inc_block_group_ro(cache, 0);
9458 if (!ret) 9411 if (!ret)
9459 goto out; 9412 goto out;
9460 alloc_flags = get_alloc_profile(root, cache->space_info->flags); 9413 alloc_flags = get_alloc_profile(fs_info, cache->space_info->flags);
9461 ret = do_chunk_alloc(trans, root, alloc_flags, 9414 ret = do_chunk_alloc(trans, fs_info, alloc_flags,
9462 CHUNK_ALLOC_FORCE); 9415 CHUNK_ALLOC_FORCE);
9463 if (ret < 0) 9416 if (ret < 0)
9464 goto out; 9417 goto out;
@@ -9467,7 +9420,7 @@ out:
9467 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { 9420 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
9468 alloc_flags = update_block_group_flags(fs_info, cache->flags); 9421 alloc_flags = update_block_group_flags(fs_info, cache->flags);
9469 lock_chunks(fs_info); 9422 lock_chunks(fs_info);
9470 check_system_chunk(trans, root, alloc_flags); 9423 check_system_chunk(trans, fs_info, alloc_flags);
9471 unlock_chunks(fs_info); 9424 unlock_chunks(fs_info);
9472 } 9425 }
9473 mutex_unlock(&fs_info->ro_block_group_mutex); 9426 mutex_unlock(&fs_info->ro_block_group_mutex);
@@ -9477,11 +9430,11 @@ out:
9477} 9430}
9478 9431
9479int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, 9432int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
9480 struct btrfs_root *root, u64 type) 9433 struct btrfs_fs_info *fs_info, u64 type)
9481{ 9434{
9482 u64 alloc_flags = get_alloc_profile(root, type); 9435 u64 alloc_flags = get_alloc_profile(fs_info, type);
9483 return do_chunk_alloc(trans, root, alloc_flags, 9436
9484 CHUNK_ALLOC_FORCE); 9437 return do_chunk_alloc(trans, fs_info, alloc_flags, CHUNK_ALLOC_FORCE);
9485} 9438}
9486 9439
9487/* 9440/*
@@ -9525,8 +9478,7 @@ u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
9525 return free_bytes; 9478 return free_bytes;
9526} 9479}
9527 9480
9528void btrfs_dec_block_group_ro(struct btrfs_root *root, 9481void btrfs_dec_block_group_ro(struct btrfs_block_group_cache *cache)
9529 struct btrfs_block_group_cache *cache)
9530{ 9482{
9531 struct btrfs_space_info *sinfo = cache->space_info; 9483 struct btrfs_space_info *sinfo = cache->space_info;
9532 u64 num_bytes; 9484 u64 num_bytes;
@@ -9766,8 +9718,7 @@ void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
9766 if (block_group->iref) 9718 if (block_group->iref)
9767 break; 9719 break;
9768 spin_unlock(&block_group->lock); 9720 spin_unlock(&block_group->lock);
9769 block_group = next_block_group(info->tree_root, 9721 block_group = next_block_group(info, block_group);
9770 block_group);
9771 } 9722 }
9772 if (!block_group) { 9723 if (!block_group) {
9773 if (last == 0) 9724 if (last == 0)
@@ -9835,7 +9786,7 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
9835 */ 9786 */
9836 if (block_group->cached == BTRFS_CACHE_NO || 9787 if (block_group->cached == BTRFS_CACHE_NO ||
9837 block_group->cached == BTRFS_CACHE_ERROR) 9788 block_group->cached == BTRFS_CACHE_ERROR)
9838 free_excluded_extents(info->extent_root, block_group); 9789 free_excluded_extents(info, block_group);
9839 9790
9840 btrfs_remove_free_space_cache(block_group); 9791 btrfs_remove_free_space_cache(block_group);
9841 ASSERT(list_empty(&block_group->dirty_list)); 9792 ASSERT(list_empty(&block_group->dirty_list));
@@ -9926,9 +9877,9 @@ out_err:
9926} 9877}
9927 9878
9928static struct btrfs_block_group_cache * 9879static struct btrfs_block_group_cache *
9929btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) 9880btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
9881 u64 start, u64 size)
9930{ 9882{
9931 struct btrfs_fs_info *fs_info = root->fs_info;
9932 struct btrfs_block_group_cache *cache; 9883 struct btrfs_block_group_cache *cache;
9933 9884
9934 cache = kzalloc(sizeof(*cache), GFP_NOFS); 9885 cache = kzalloc(sizeof(*cache), GFP_NOFS);
@@ -9948,8 +9899,9 @@ btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
9948 9899
9949 cache->sectorsize = fs_info->sectorsize; 9900 cache->sectorsize = fs_info->sectorsize;
9950 cache->fs_info = fs_info; 9901 cache->fs_info = fs_info;
9951 cache->full_stripe_len = btrfs_full_stripe_len(root, 9902 cache->full_stripe_len = btrfs_full_stripe_len(fs_info,
9952 &fs_info->mapping_tree, start); 9903 &fs_info->mapping_tree,
9904 start);
9953 set_free_space_tree_thresholds(cache); 9905 set_free_space_tree_thresholds(cache);
9954 9906
9955 atomic_set(&cache->count, 1); 9907 atomic_set(&cache->count, 1);
@@ -9970,7 +9922,6 @@ btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
9970 9922
9971int btrfs_read_block_groups(struct btrfs_fs_info *info) 9923int btrfs_read_block_groups(struct btrfs_fs_info *info)
9972{ 9924{
9973 struct btrfs_root *root = info->extent_root;
9974 struct btrfs_path *path; 9925 struct btrfs_path *path;
9975 int ret; 9926 int ret;
9976 struct btrfs_block_group_cache *cache; 9927 struct btrfs_block_group_cache *cache;
@@ -10011,7 +9962,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10011 leaf = path->nodes[0]; 9962 leaf = path->nodes[0];
10012 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 9963 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
10013 9964
10014 cache = btrfs_create_block_group_cache(root, found_key.objectid, 9965 cache = btrfs_create_block_group_cache(info, found_key.objectid,
10015 found_key.offset); 9966 found_key.offset);
10016 if (!cache) { 9967 if (!cache) {
10017 ret = -ENOMEM; 9968 ret = -ENOMEM;
@@ -10055,13 +10006,13 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10055 * info has super bytes accounted for, otherwise we'll think 10006 * info has super bytes accounted for, otherwise we'll think
10056 * we have more space than we actually do. 10007 * we have more space than we actually do.
10057 */ 10008 */
10058 ret = exclude_super_stripes(root, cache); 10009 ret = exclude_super_stripes(info, cache);
10059 if (ret) { 10010 if (ret) {
10060 /* 10011 /*
10061 * We may have excluded something, so call this just in 10012 * We may have excluded something, so call this just in
10062 * case. 10013 * case.
10063 */ 10014 */
10064 free_excluded_extents(root, cache); 10015 free_excluded_extents(info, cache);
10065 btrfs_put_block_group(cache); 10016 btrfs_put_block_group(cache);
10066 goto error; 10017 goto error;
10067 } 10018 }
@@ -10076,7 +10027,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10076 if (found_key.offset == btrfs_block_group_used(&cache->item)) { 10027 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
10077 cache->last_byte_to_unpin = (u64)-1; 10028 cache->last_byte_to_unpin = (u64)-1;
10078 cache->cached = BTRFS_CACHE_FINISHED; 10029 cache->cached = BTRFS_CACHE_FINISHED;
10079 free_excluded_extents(root, cache); 10030 free_excluded_extents(info, cache);
10080 } else if (btrfs_block_group_used(&cache->item) == 0) { 10031 } else if (btrfs_block_group_used(&cache->item) == 0) {
10081 cache->last_byte_to_unpin = (u64)-1; 10032 cache->last_byte_to_unpin = (u64)-1;
10082 cache->cached = BTRFS_CACHE_FINISHED; 10033 cache->cached = BTRFS_CACHE_FINISHED;
@@ -10084,7 +10035,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10084 found_key.objectid, 10035 found_key.objectid,
10085 found_key.objectid + 10036 found_key.objectid +
10086 found_key.offset); 10037 found_key.offset);
10087 free_excluded_extents(root, cache); 10038 free_excluded_extents(info, cache);
10088 } 10039 }
10089 10040
10090 ret = btrfs_add_block_group_cache(info, cache); 10041 ret = btrfs_add_block_group_cache(info, cache);
@@ -10114,7 +10065,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10114 __link_block_group(space_info, cache); 10065 __link_block_group(space_info, cache);
10115 10066
10116 set_avail_alloc_bits(info, cache->flags); 10067 set_avail_alloc_bits(info, cache->flags);
10117 if (btrfs_chunk_readonly(root, cache->key.objectid)) { 10068 if (btrfs_chunk_readonly(info, cache->key.objectid)) {
10118 inc_block_group_ro(cache, 1); 10069 inc_block_group_ro(cache, 1);
10119 } else if (btrfs_block_group_used(&cache->item) == 0) { 10070 } else if (btrfs_block_group_used(&cache->item) == 0) {
10120 spin_lock(&info->unused_bgs_lock); 10071 spin_lock(&info->unused_bgs_lock);
@@ -10129,7 +10080,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10129 } 10080 }
10130 10081
10131 list_for_each_entry_rcu(space_info, &info->space_info, list) { 10082 list_for_each_entry_rcu(space_info, &info->space_info, list) {
10132 if (!(get_alloc_profile(root, space_info->flags) & 10083 if (!(get_alloc_profile(info, space_info->flags) &
10133 (BTRFS_BLOCK_GROUP_RAID10 | 10084 (BTRFS_BLOCK_GROUP_RAID10 |
10134 BTRFS_BLOCK_GROUP_RAID1 | 10085 BTRFS_BLOCK_GROUP_RAID1 |
10135 BTRFS_BLOCK_GROUP_RAID5 | 10086 BTRFS_BLOCK_GROUP_RAID5 |
@@ -10158,9 +10109,8 @@ error:
10158} 10109}
10159 10110
10160void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, 10111void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
10161 struct btrfs_root *root) 10112 struct btrfs_fs_info *fs_info)
10162{ 10113{
10163 struct btrfs_fs_info *fs_info = root->fs_info;
10164 struct btrfs_block_group_cache *block_group, *tmp; 10114 struct btrfs_block_group_cache *block_group, *tmp;
10165 struct btrfs_root *extent_root = fs_info->extent_root; 10115 struct btrfs_root *extent_root = fs_info->extent_root;
10166 struct btrfs_block_group_item item; 10116 struct btrfs_block_group_item item;
@@ -10195,17 +10145,16 @@ next:
10195} 10145}
10196 10146
10197int btrfs_make_block_group(struct btrfs_trans_handle *trans, 10147int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10198 struct btrfs_root *root, u64 bytes_used, 10148 struct btrfs_fs_info *fs_info, u64 bytes_used,
10199 u64 type, u64 chunk_objectid, u64 chunk_offset, 10149 u64 type, u64 chunk_objectid, u64 chunk_offset,
10200 u64 size) 10150 u64 size)
10201{ 10151{
10202 struct btrfs_fs_info *fs_info = root->fs_info;
10203 struct btrfs_block_group_cache *cache; 10152 struct btrfs_block_group_cache *cache;
10204 int ret; 10153 int ret;
10205 10154
10206 btrfs_set_log_full_commit(fs_info, trans); 10155 btrfs_set_log_full_commit(fs_info, trans);
10207 10156
10208 cache = btrfs_create_block_group_cache(root, chunk_offset, size); 10157 cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size);
10209 if (!cache) 10158 if (!cache)
10210 return -ENOMEM; 10159 return -ENOMEM;
10211 10160
@@ -10217,27 +10166,27 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10217 cache->last_byte_to_unpin = (u64)-1; 10166 cache->last_byte_to_unpin = (u64)-1;
10218 cache->cached = BTRFS_CACHE_FINISHED; 10167 cache->cached = BTRFS_CACHE_FINISHED;
10219 cache->needs_free_space = 1; 10168 cache->needs_free_space = 1;
10220 ret = exclude_super_stripes(root, cache); 10169 ret = exclude_super_stripes(fs_info, cache);
10221 if (ret) { 10170 if (ret) {
10222 /* 10171 /*
10223 * We may have excluded something, so call this just in 10172 * We may have excluded something, so call this just in
10224 * case. 10173 * case.
10225 */ 10174 */
10226 free_excluded_extents(root, cache); 10175 free_excluded_extents(fs_info, cache);
10227 btrfs_put_block_group(cache); 10176 btrfs_put_block_group(cache);
10228 return ret; 10177 return ret;
10229 } 10178 }
10230 10179
10231 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size); 10180 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
10232 10181
10233 free_excluded_extents(root, cache); 10182 free_excluded_extents(fs_info, cache);
10234 10183
10235#ifdef CONFIG_BTRFS_DEBUG 10184#ifdef CONFIG_BTRFS_DEBUG
10236 if (btrfs_should_fragment_free_space(root, cache)) { 10185 if (btrfs_should_fragment_free_space(cache)) {
10237 u64 new_bytes_used = size - bytes_used; 10186 u64 new_bytes_used = size - bytes_used;
10238 10187
10239 bytes_used += new_bytes_used >> 1; 10188 bytes_used += new_bytes_used >> 1;
10240 fragment_free_space(root, cache); 10189 fragment_free_space(cache);
10241 } 10190 }
10242#endif 10191#endif
10243 /* 10192 /*
@@ -10328,7 +10277,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10328 * Free the reserved super bytes from this block group before 10277 * Free the reserved super bytes from this block group before
10329 * remove it. 10278 * remove it.
10330 */ 10279 */
10331 free_excluded_extents(root, block_group); 10280 free_excluded_extents(fs_info, block_group);
10332 10281
10333 memcpy(&key, &block_group->key, sizeof(key)); 10282 memcpy(&key, &block_group->key, sizeof(key));
10334 index = get_block_group_index(block_group); 10283 index = get_block_group_index(block_group);
@@ -10708,7 +10657,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10708 trans = btrfs_start_trans_remove_block_group(fs_info, 10657 trans = btrfs_start_trans_remove_block_group(fs_info,
10709 block_group->key.objectid); 10658 block_group->key.objectid);
10710 if (IS_ERR(trans)) { 10659 if (IS_ERR(trans)) {
10711 btrfs_dec_block_group_ro(root, block_group); 10660 btrfs_dec_block_group_ro(block_group);
10712 ret = PTR_ERR(trans); 10661 ret = PTR_ERR(trans);
10713 goto next; 10662 goto next;
10714 } 10663 }
@@ -10735,14 +10684,14 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10735 EXTENT_DIRTY); 10684 EXTENT_DIRTY);
10736 if (ret) { 10685 if (ret) {
10737 mutex_unlock(&fs_info->unused_bg_unpin_mutex); 10686 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
10738 btrfs_dec_block_group_ro(root, block_group); 10687 btrfs_dec_block_group_ro(block_group);
10739 goto end_trans; 10688 goto end_trans;
10740 } 10689 }
10741 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, 10690 ret = clear_extent_bits(&fs_info->freed_extents[1], start, end,
10742 EXTENT_DIRTY); 10691 EXTENT_DIRTY);
10743 if (ret) { 10692 if (ret) {
10744 mutex_unlock(&fs_info->unused_bg_unpin_mutex); 10693 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
10745 btrfs_dec_block_group_ro(root, block_group); 10694 btrfs_dec_block_group_ro(block_group);
10746 goto end_trans; 10695 goto end_trans;
10747 } 10696 }
10748 mutex_unlock(&fs_info->unused_bg_unpin_mutex); 10697 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
@@ -10845,9 +10794,10 @@ out:
10845 return ret; 10794 return ret;
10846} 10795}
10847 10796
10848int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) 10797int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
10798 u64 start, u64 end)
10849{ 10799{
10850 return unpin_extent_range(root, start, end, false); 10800 return unpin_extent_range(fs_info, start, end, false);
10851} 10801}
10852 10802
10853/* 10803/*
@@ -10937,9 +10887,8 @@ static int btrfs_trim_free_extents(struct btrfs_device *device,
10937 return ret; 10887 return ret;
10938} 10888}
10939 10889
10940int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) 10890int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
10941{ 10891{
10942 struct btrfs_fs_info *fs_info = root->fs_info;
10943 struct btrfs_block_group_cache *cache = NULL; 10892 struct btrfs_block_group_cache *cache = NULL;
10944 struct btrfs_device *device; 10893 struct btrfs_device *device;
10945 struct list_head *devices; 10894 struct list_head *devices;
@@ -10994,7 +10943,7 @@ int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
10994 } 10943 }
10995 } 10944 }
10996 10945
10997 cache = next_block_group(fs_info->tree_root, cache); 10946 cache = next_block_group(fs_info, cache);
10998 } 10947 }
10999 10948
11000 mutex_lock(&fs_info->fs_devices->device_list_mutex); 10949 mutex_lock(&fs_info->fs_devices->device_list_mutex);
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 8df72ded9018..3b148e5fcc56 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2067,10 +2067,9 @@ int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
2067 return 0; 2067 return 0;
2068} 2068}
2069 2069
2070int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, 2070int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
2071 int mirror_num) 2071 struct extent_buffer *eb, int mirror_num)
2072{ 2072{
2073 struct btrfs_fs_info *fs_info = root->fs_info;
2074 u64 start = eb->start; 2073 u64 start = eb->start;
2075 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); 2074 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2076 int ret = 0; 2075 int ret = 0;
@@ -3753,8 +3752,7 @@ static noinline_for_stack int write_one_eb(struct extent_buffer *eb,
3753 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0 3752 * header 0 1 2 .. N ... data_N .. data_2 data_1 data_0
3754 */ 3753 */
3755 start = btrfs_item_nr_offset(nritems); 3754 start = btrfs_item_nr_offset(nritems);
3756 end = btrfs_leaf_data(eb) + 3755 end = btrfs_leaf_data(eb) + leaf_data_end(fs_info, eb);
3757 leaf_data_end(fs_info->tree_root, eb);
3758 memzero_extent_buffer(eb, start, end - start); 3756 memzero_extent_buffer(eb, start, end - start);
3759 } 3757 }
3760 3758
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index f786156bd7e2..17f9ce479ed7 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -457,8 +457,8 @@ int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
457int clean_io_failure(struct inode *inode, u64 start, struct page *page, 457int clean_io_failure(struct inode *inode, u64 start, struct page *page,
458 unsigned int pg_offset); 458 unsigned int pg_offset);
459void end_extent_writepage(struct page *page, int err, u64 start, u64 end); 459void end_extent_writepage(struct page *page, int err, u64 start, u64 end);
460int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, 460int repair_eb_io_failure(struct btrfs_fs_info *fs_info,
461 int mirror_num); 461 struct extent_buffer *eb, int mirror_num);
462 462
463/* 463/*
464 * When IO fails, either with EIO or csum verification fails, we 464 * When IO fails, either with EIO or csum verification fails, we
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 672a36554363..e97e322c28f0 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -160,8 +160,7 @@ static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err)
160 kfree(bio->csum_allocated); 160 kfree(bio->csum_allocated);
161} 161}
162 162
163static int __btrfs_lookup_bio_sums(struct btrfs_root *root, 163static int __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio,
164 struct inode *inode, struct bio *bio,
165 u64 logical_offset, u32 *dst, int dio) 164 u64 logical_offset, u32 *dst, int dio)
166{ 165{
167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 166 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
@@ -304,16 +303,14 @@ next:
304 return 0; 303 return 0;
305} 304}
306 305
307int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, 306int btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst)
308 struct bio *bio, u32 *dst)
309{ 307{
310 return __btrfs_lookup_bio_sums(root, inode, bio, 0, dst, 0); 308 return __btrfs_lookup_bio_sums(inode, bio, 0, dst, 0);
311} 309}
312 310
313int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode, 311int btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio, u64 offset)
314 struct bio *bio, u64 offset)
315{ 312{
316 return __btrfs_lookup_bio_sums(root, inode, bio, offset, NULL, 1); 313 return __btrfs_lookup_bio_sums(inode, bio, offset, NULL, 1);
317} 314}
318 315
319int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 316int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
@@ -436,8 +433,8 @@ fail:
436 return ret; 433 return ret;
437} 434}
438 435
439int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, 436int btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
440 struct bio *bio, u64 file_start, int contig) 437 u64 file_start, int contig)
441{ 438{
442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 439 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
443 struct btrfs_ordered_sum *sums; 440 struct btrfs_ordered_sum *sums;
@@ -543,12 +540,11 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
543 * This calls btrfs_truncate_item with the correct args based on the 540 * This calls btrfs_truncate_item with the correct args based on the
544 * overlap, and fixes up the key as required. 541 * overlap, and fixes up the key as required.
545 */ 542 */
546static noinline void truncate_one_csum(struct btrfs_root *root, 543static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
547 struct btrfs_path *path, 544 struct btrfs_path *path,
548 struct btrfs_key *key, 545 struct btrfs_key *key,
549 u64 bytenr, u64 len) 546 u64 bytenr, u64 len)
550{ 547{
551 struct btrfs_fs_info *fs_info = root->fs_info;
552 struct extent_buffer *leaf; 548 struct extent_buffer *leaf;
553 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 549 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
554 u64 csum_end; 550 u64 csum_end;
@@ -569,7 +565,7 @@ static noinline void truncate_one_csum(struct btrfs_root *root,
569 */ 565 */
570 u32 new_size = (bytenr - key->offset) >> blocksize_bits; 566 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
571 new_size *= csum_size; 567 new_size *= csum_size;
572 btrfs_truncate_item(root, path, new_size, 1); 568 btrfs_truncate_item(fs_info, path, new_size, 1);
573 } else if (key->offset >= bytenr && csum_end > end_byte && 569 } else if (key->offset >= bytenr && csum_end > end_byte &&
574 end_byte > key->offset) { 570 end_byte > key->offset) {
575 /* 571 /*
@@ -581,7 +577,7 @@ static noinline void truncate_one_csum(struct btrfs_root *root,
581 u32 new_size = (csum_end - end_byte) >> blocksize_bits; 577 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
582 new_size *= csum_size; 578 new_size *= csum_size;
583 579
584 btrfs_truncate_item(root, path, new_size, 0); 580 btrfs_truncate_item(fs_info, path, new_size, 0);
585 581
586 key->offset = end_byte; 582 key->offset = end_byte;
587 btrfs_set_item_key_safe(fs_info, path, key); 583 btrfs_set_item_key_safe(fs_info, path, key);
@@ -698,7 +694,7 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
698 694
699 key.offset = end_byte - 1; 695 key.offset = end_byte - 1;
700 } else { 696 } else {
701 truncate_one_csum(root, path, &key, bytenr, len); 697 truncate_one_csum(fs_info, path, &key, bytenr, len);
702 if (key.offset < bytenr) 698 if (key.offset < bytenr)
703 break; 699 break;
704 } 700 }
@@ -824,11 +820,11 @@ again:
824 u32 diff; 820 u32 diff;
825 u32 free_space; 821 u32 free_space;
826 822
827 if (btrfs_leaf_free_space(root, leaf) < 823 if (btrfs_leaf_free_space(fs_info, leaf) <
828 sizeof(struct btrfs_item) + csum_size * 2) 824 sizeof(struct btrfs_item) + csum_size * 2)
829 goto insert; 825 goto insert;
830 826
831 free_space = btrfs_leaf_free_space(root, leaf) - 827 free_space = btrfs_leaf_free_space(fs_info, leaf) -
832 sizeof(struct btrfs_item) - csum_size; 828 sizeof(struct btrfs_item) - csum_size;
833 tmp = sums->len - total_bytes; 829 tmp = sums->len - total_bytes;
834 tmp >>= fs_info->sb->s_blocksize_bits; 830 tmp >>= fs_info->sb->s_blocksize_bits;
@@ -844,7 +840,7 @@ again:
844 diff /= csum_size; 840 diff /= csum_size;
845 diff *= csum_size; 841 diff *= csum_size;
846 842
847 btrfs_extend_item(root, path, diff); 843 btrfs_extend_item(fs_info, path, diff);
848 ret = 0; 844 ret = 0;
849 goto csum; 845 goto csum;
850 } 846 }
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index d49d8eadf517..2d3b93dd9c2c 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -129,10 +129,8 @@ static int __btrfs_add_inode_defrag(struct inode *inode,
129 return 0; 129 return 0;
130} 130}
131 131
132static inline int __need_auto_defrag(struct btrfs_root *root) 132static inline int __need_auto_defrag(struct btrfs_fs_info *fs_info)
133{ 133{
134 struct btrfs_fs_info *fs_info = root->fs_info;
135
136 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG)) 134 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
137 return 0; 135 return 0;
138 136
@@ -155,7 +153,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
155 u64 transid; 153 u64 transid;
156 int ret; 154 int ret;
157 155
158 if (!__need_auto_defrag(root)) 156 if (!__need_auto_defrag(fs_info))
159 return 0; 157 return 0;
160 158
161 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) 159 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
@@ -200,10 +198,9 @@ static void btrfs_requeue_inode_defrag(struct inode *inode,
200 struct inode_defrag *defrag) 198 struct inode_defrag *defrag)
201{ 199{
202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 200 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
203 struct btrfs_root *root = BTRFS_I(inode)->root;
204 int ret; 201 int ret;
205 202
206 if (!__need_auto_defrag(root)) 203 if (!__need_auto_defrag(fs_info))
207 goto out; 204 goto out;
208 205
209 /* 206 /*
@@ -376,7 +373,7 @@ int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
376 &fs_info->fs_state)) 373 &fs_info->fs_state))
377 break; 374 break;
378 375
379 if (!__need_auto_defrag(fs_info->tree_root)) 376 if (!__need_auto_defrag(fs_info))
380 break; 377 break;
381 378
382 /* find an inode to defrag */ 379 /* find an inode to defrag */
@@ -488,10 +485,9 @@ static void btrfs_drop_pages(struct page **pages, size_t num_pages)
488 * this also makes the decision about creating an inline extent vs 485 * this also makes the decision about creating an inline extent vs
489 * doing real data extents, marking pages dirty and delalloc as required. 486 * doing real data extents, marking pages dirty and delalloc as required.
490 */ 487 */
491int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode, 488int btrfs_dirty_pages(struct inode *inode, struct page **pages,
492 struct page **pages, size_t num_pages, 489 size_t num_pages, loff_t pos, size_t write_bytes,
493 loff_t pos, size_t write_bytes, 490 struct extent_state **cached)
494 struct extent_state **cached)
495{ 491{
496 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 492 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
497 int err = 0; 493 int err = 0;
@@ -860,7 +856,7 @@ next_slot:
860 btrfs_mark_buffer_dirty(leaf); 856 btrfs_mark_buffer_dirty(leaf);
861 857
862 if (update_refs && disk_bytenr > 0) { 858 if (update_refs && disk_bytenr > 0) {
863 ret = btrfs_inc_extent_ref(trans, root, 859 ret = btrfs_inc_extent_ref(trans, fs_info,
864 disk_bytenr, num_bytes, 0, 860 disk_bytenr, num_bytes, 0,
865 root->root_key.objectid, 861 root->root_key.objectid,
866 new_key.objectid, 862 new_key.objectid,
@@ -944,7 +940,7 @@ delete_extent_item:
944 extent_end = ALIGN(extent_end, 940 extent_end = ALIGN(extent_end,
945 fs_info->sectorsize); 941 fs_info->sectorsize);
946 } else if (update_refs && disk_bytenr > 0) { 942 } else if (update_refs && disk_bytenr > 0) {
947 ret = btrfs_free_extent(trans, root, 943 ret = btrfs_free_extent(trans, fs_info,
948 disk_bytenr, num_bytes, 0, 944 disk_bytenr, num_bytes, 0,
949 root->root_key.objectid, 945 root->root_key.objectid,
950 key.objectid, key.offset - 946 key.objectid, key.offset -
@@ -1001,7 +997,7 @@ delete_extent_item:
1001 if (!ret && replace_extent && leafs_visited == 1 && 997 if (!ret && replace_extent && leafs_visited == 1 &&
1002 (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING || 998 (path->locks[0] == BTRFS_WRITE_LOCK_BLOCKING ||
1003 path->locks[0] == BTRFS_WRITE_LOCK) && 999 path->locks[0] == BTRFS_WRITE_LOCK) &&
1004 btrfs_leaf_free_space(root, leaf) >= 1000 btrfs_leaf_free_space(fs_info, leaf) >=
1005 sizeof(struct btrfs_item) + extent_item_size) { 1001 sizeof(struct btrfs_item) + extent_item_size) {
1006 1002
1007 key.objectid = ino; 1003 key.objectid = ino;
@@ -1238,8 +1234,8 @@ again:
1238 extent_end - split); 1234 extent_end - split);
1239 btrfs_mark_buffer_dirty(leaf); 1235 btrfs_mark_buffer_dirty(leaf);
1240 1236
1241 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, 1237 ret = btrfs_inc_extent_ref(trans, fs_info, bytenr, num_bytes,
1242 root->root_key.objectid, 1238 0, root->root_key.objectid,
1243 ino, orig_offset); 1239 ino, orig_offset);
1244 if (ret) { 1240 if (ret) {
1245 btrfs_abort_transaction(trans, ret); 1241 btrfs_abort_transaction(trans, ret);
@@ -1272,7 +1268,7 @@ again:
1272 extent_end = other_end; 1268 extent_end = other_end;
1273 del_slot = path->slots[0] + 1; 1269 del_slot = path->slots[0] + 1;
1274 del_nr++; 1270 del_nr++;
1275 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 1271 ret = btrfs_free_extent(trans, fs_info, bytenr, num_bytes,
1276 0, root->root_key.objectid, 1272 0, root->root_key.objectid,
1277 ino, orig_offset); 1273 ino, orig_offset);
1278 if (ret) { 1274 if (ret) {
@@ -1292,7 +1288,7 @@ again:
1292 key.offset = other_start; 1288 key.offset = other_start;
1293 del_slot = path->slots[0]; 1289 del_slot = path->slots[0];
1294 del_nr++; 1290 del_nr++;
1295 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 1291 ret = btrfs_free_extent(trans, fs_info, bytenr, num_bytes,
1296 0, root->root_key.objectid, 1292 0, root->root_key.objectid,
1297 ino, orig_offset); 1293 ino, orig_offset);
1298 if (ret) { 1294 if (ret) {
@@ -1698,9 +1694,8 @@ again:
1698 fs_info->sectorsize); 1694 fs_info->sectorsize);
1699 1695
1700 if (copied > 0) 1696 if (copied > 0)
1701 ret = btrfs_dirty_pages(root, inode, pages, 1697 ret = btrfs_dirty_pages(inode, pages, dirty_pages,
1702 dirty_pages, pos, copied, 1698 pos, copied, NULL);
1703 NULL);
1704 if (need_unlock) 1699 if (need_unlock)
1705 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 1700 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1706 lockstart, lockend, &cached_state, 1701 lockstart, lockend, &cached_state,
@@ -1732,7 +1727,7 @@ again:
1732 1727
1733 balance_dirty_pages_ratelimited(inode->i_mapping); 1728 balance_dirty_pages_ratelimited(inode->i_mapping);
1734 if (dirty_pages < (fs_info->nodesize >> PAGE_SHIFT) + 1) 1729 if (dirty_pages < (fs_info->nodesize >> PAGE_SHIFT) + 1)
1735 btrfs_btree_balance_dirty(root); 1730 btrfs_btree_balance_dirty(fs_info);
1736 1731
1737 pos += copied; 1732 pos += copied;
1738 num_written += copied; 1733 num_written += copied;
@@ -2519,7 +2514,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2519 goto out; 2514 goto out;
2520 } 2515 }
2521 2516
2522 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); 2517 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
2523 if (!rsv) { 2518 if (!rsv) {
2524 ret = -ENOMEM; 2519 ret = -ENOMEM;
2525 goto out_free; 2520 goto out_free;
@@ -2580,7 +2575,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2580 } 2575 }
2581 2576
2582 btrfs_end_transaction(trans, root); 2577 btrfs_end_transaction(trans, root);
2583 btrfs_btree_balance_dirty(root); 2578 btrfs_btree_balance_dirty(fs_info);
2584 2579
2585 trans = btrfs_start_transaction(root, rsv_count); 2580 trans = btrfs_start_transaction(root, rsv_count);
2586 if (IS_ERR(trans)) { 2581 if (IS_ERR(trans)) {
@@ -2648,10 +2643,10 @@ out_trans:
2648 ret = btrfs_update_inode(trans, root, inode); 2643 ret = btrfs_update_inode(trans, root, inode);
2649 updated_inode = true; 2644 updated_inode = true;
2650 btrfs_end_transaction(trans, root); 2645 btrfs_end_transaction(trans, root);
2651 btrfs_btree_balance_dirty(root); 2646 btrfs_btree_balance_dirty(fs_info);
2652out_free: 2647out_free:
2653 btrfs_free_path(path); 2648 btrfs_free_path(path);
2654 btrfs_free_block_rsv(root, rsv); 2649 btrfs_free_block_rsv(fs_info, rsv);
2655out: 2650out:
2656 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend, 2651 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2657 &cached_state, GFP_NOFS); 2652 &cached_state, GFP_NOFS);
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index ab7e2b97f49e..2e8445e4ffa3 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -208,10 +208,9 @@ int create_free_space_inode(struct btrfs_root *root,
208 block_group->key.objectid); 208 block_group->key.objectid);
209} 209}
210 210
211int btrfs_check_trunc_cache_free_space(struct btrfs_root *root, 211int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
212 struct btrfs_block_rsv *rsv) 212 struct btrfs_block_rsv *rsv)
213{ 213{
214 struct btrfs_fs_info *fs_info = root->fs_info;
215 u64 needed_bytes; 214 u64 needed_bytes;
216 int ret; 215 int ret;
217 216
@@ -1036,12 +1035,11 @@ fail:
1036} 1035}
1037 1036
1038static noinline_for_stack int 1037static noinline_for_stack int
1039write_pinned_extent_entries(struct btrfs_root *root, 1038write_pinned_extent_entries(struct btrfs_fs_info *fs_info,
1040 struct btrfs_block_group_cache *block_group, 1039 struct btrfs_block_group_cache *block_group,
1041 struct btrfs_io_ctl *io_ctl, 1040 struct btrfs_io_ctl *io_ctl,
1042 int *entries) 1041 int *entries)
1043{ 1042{
1044 struct btrfs_fs_info *fs_info;
1045 u64 start, extent_start, extent_end, len; 1043 u64 start, extent_start, extent_end, len;
1046 struct extent_io_tree *unpin = NULL; 1044 struct extent_io_tree *unpin = NULL;
1047 int ret; 1045 int ret;
@@ -1049,8 +1047,6 @@ write_pinned_extent_entries(struct btrfs_root *root,
1049 if (!block_group) 1047 if (!block_group)
1050 return 0; 1048 return 0;
1051 1049
1052 fs_info = block_group->fs_info;
1053
1054 /* 1050 /*
1055 * We want to add any pinned extents to our free space cache 1051 * We want to add any pinned extents to our free space cache
1056 * so we don't leak the space 1052 * so we don't leak the space
@@ -1243,6 +1239,7 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1243 struct btrfs_trans_handle *trans, 1239 struct btrfs_trans_handle *trans,
1244 struct btrfs_path *path, u64 offset) 1240 struct btrfs_path *path, u64 offset)
1245{ 1241{
1242 struct btrfs_fs_info *fs_info = root->fs_info;
1246 struct extent_state *cached_state = NULL; 1243 struct extent_state *cached_state = NULL;
1247 LIST_HEAD(bitmap_list); 1244 LIST_HEAD(bitmap_list);
1248 int entries = 0; 1245 int entries = 0;
@@ -1300,7 +1297,8 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1300 * If this changes while we are working we'll get added back to 1297 * If this changes while we are working we'll get added back to
1301 * the dirty list and redo it. No locking needed 1298 * the dirty list and redo it. No locking needed
1302 */ 1299 */
1303 ret = write_pinned_extent_entries(root, block_group, io_ctl, &entries); 1300 ret = write_pinned_extent_entries(fs_info, block_group,
1301 io_ctl, &entries);
1304 if (ret) 1302 if (ret)
1305 goto out_nospc_locked; 1303 goto out_nospc_locked;
1306 1304
@@ -1319,8 +1317,8 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1319 io_ctl_zero_remaining_pages(io_ctl); 1317 io_ctl_zero_remaining_pages(io_ctl);
1320 1318
1321 /* Everything is written out, now we dirty the pages in the file. */ 1319 /* Everything is written out, now we dirty the pages in the file. */
1322 ret = btrfs_dirty_pages(root, inode, io_ctl->pages, io_ctl->num_pages, 1320 ret = btrfs_dirty_pages(inode, io_ctl->pages, io_ctl->num_pages, 0,
1323 0, i_size_read(inode), &cached_state); 1321 i_size_read(inode), &cached_state);
1324 if (ret) 1322 if (ret)
1325 goto out_nospc; 1323 goto out_nospc;
1326 1324
@@ -1994,7 +1992,7 @@ static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1994 bool forced = false; 1992 bool forced = false;
1995 1993
1996#ifdef CONFIG_BTRFS_DEBUG 1994#ifdef CONFIG_BTRFS_DEBUG
1997 if (btrfs_should_fragment_free_space(fs_info->extent_root, block_group)) 1995 if (btrfs_should_fragment_free_space(block_group))
1998 forced = true; 1996 forced = true;
1999#endif 1997#endif
2000 1998
@@ -3034,13 +3032,12 @@ setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
3034 * returns zero and sets up cluster if things worked out, otherwise 3032 * returns zero and sets up cluster if things worked out, otherwise
3035 * it returns -enospc 3033 * it returns -enospc
3036 */ 3034 */
3037int btrfs_find_space_cluster(struct btrfs_root *root, 3035int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
3038 struct btrfs_block_group_cache *block_group, 3036 struct btrfs_block_group_cache *block_group,
3039 struct btrfs_free_cluster *cluster, 3037 struct btrfs_free_cluster *cluster,
3040 u64 offset, u64 bytes, u64 empty_size) 3038 u64 offset, u64 bytes, u64 empty_size)
3041{ 3039{
3042 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 3040 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3043 struct btrfs_fs_info *fs_info = block_group->fs_info;
3044 struct btrfs_free_space *entry, *tmp; 3041 struct btrfs_free_space *entry, *tmp;
3045 LIST_HEAD(bitmaps); 3042 LIST_HEAD(bitmaps);
3046 u64 min_bytes; 3043 u64 min_bytes;
@@ -3148,8 +3145,7 @@ static int do_trimming(struct btrfs_block_group_cache *block_group,
3148 spin_unlock(&block_group->lock); 3145 spin_unlock(&block_group->lock);
3149 spin_unlock(&space_info->lock); 3146 spin_unlock(&space_info->lock);
3150 3147
3151 ret = btrfs_discard_extent(fs_info->extent_root, 3148 ret = btrfs_discard_extent(fs_info, start, bytes, &trimmed);
3152 start, bytes, &trimmed);
3153 if (!ret) 3149 if (!ret)
3154 *total_trimmed += trimmed; 3150 *total_trimmed += trimmed;
3155 3151
diff --git a/fs/btrfs/free-space-cache.h b/fs/btrfs/free-space-cache.h
index f39ba850b566..6f3c025a2c6c 100644
--- a/fs/btrfs/free-space-cache.h
+++ b/fs/btrfs/free-space-cache.h
@@ -59,7 +59,7 @@ int create_free_space_inode(struct btrfs_root *root,
59 struct btrfs_block_group_cache *block_group, 59 struct btrfs_block_group_cache *block_group,
60 struct btrfs_path *path); 60 struct btrfs_path *path);
61 61
62int btrfs_check_trunc_cache_free_space(struct btrfs_root *root, 62int btrfs_check_trunc_cache_free_space(struct btrfs_fs_info *fs_info,
63 struct btrfs_block_rsv *rsv); 63 struct btrfs_block_rsv *rsv);
64int btrfs_truncate_free_space_cache(struct btrfs_root *root, 64int btrfs_truncate_free_space_cache(struct btrfs_root *root,
65 struct btrfs_trans_handle *trans, 65 struct btrfs_trans_handle *trans,
@@ -109,7 +109,7 @@ u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
109u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root); 109u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root);
110void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group, 110void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
111 u64 bytes); 111 u64 bytes);
112int btrfs_find_space_cluster(struct btrfs_root *root, 112int btrfs_find_space_cluster(struct btrfs_fs_info *fs_info,
113 struct btrfs_block_group_cache *block_group, 113 struct btrfs_block_group_cache *block_group,
114 struct btrfs_free_cluster *cluster, 114 struct btrfs_free_cluster *cluster,
115 u64 offset, u64 bytes, u64 empty_size); 115 u64 offset, u64 bytes, u64 empty_size);
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index 47270a3c9649..39c968f80157 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -182,7 +182,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
182 memmove_extent_buffer(leaf, ptr, ptr + del_len, 182 memmove_extent_buffer(leaf, ptr, ptr + del_len,
183 item_size - (ptr + del_len - item_start)); 183 item_size - (ptr + del_len - item_start));
184 184
185 btrfs_truncate_item(root, path, item_size - del_len, 1); 185 btrfs_truncate_item(root->fs_info, path, item_size - del_len, 1);
186 186
187out: 187out:
188 btrfs_free_path(path); 188 btrfs_free_path(path);
@@ -245,7 +245,7 @@ int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
245 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]); 245 item_start = btrfs_item_ptr_offset(leaf, path->slots[0]);
246 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len, 246 memmove_extent_buffer(leaf, ptr, ptr + sub_item_len,
247 item_size - (ptr + sub_item_len - item_start)); 247 item_size - (ptr + sub_item_len - item_start));
248 btrfs_truncate_item(root, path, item_size - sub_item_len, 1); 248 btrfs_truncate_item(root->fs_info, path, item_size - sub_item_len, 1);
249out: 249out:
250 btrfs_free_path(path); 250 btrfs_free_path(path);
251 251
@@ -297,7 +297,7 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
297 name, name_len, NULL)) 297 name, name_len, NULL))
298 goto out; 298 goto out;
299 299
300 btrfs_extend_item(root, path, ins_len); 300 btrfs_extend_item(root->fs_info, path, ins_len);
301 ret = 0; 301 ret = 0;
302 } 302 }
303 if (ret < 0) 303 if (ret < 0)
@@ -355,7 +355,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
355 goto out; 355 goto out;
356 356
357 old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]); 357 old_size = btrfs_item_size_nr(path->nodes[0], path->slots[0]);
358 btrfs_extend_item(root, path, ins_len); 358 btrfs_extend_item(fs_info, path, ins_len);
359 ref = btrfs_item_ptr(path->nodes[0], path->slots[0], 359 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
360 struct btrfs_inode_ref); 360 struct btrfs_inode_ref);
361 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size); 361 ref = (struct btrfs_inode_ref *)((unsigned long)ref + old_size);
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index 79921f6fb8e5..144b119ff43f 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -509,7 +509,8 @@ out_put:
509out_release: 509out_release:
510 trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid, 510 trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
511 trans->bytes_reserved, 0); 511 trans->bytes_reserved, 0);
512 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); 512 btrfs_block_rsv_release(fs_info, trans->block_rsv,
513 trans->bytes_reserved);
513out: 514out:
514 trans->block_rsv = rsv; 515 trans->block_rsv = rsv;
515 trans->bytes_reserved = num_bytes; 516 trans->bytes_reserved = num_bytes;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 677762e0dcab..5fa8aeb23e94 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -874,7 +874,7 @@ retry:
874 return; 874 return;
875out_free_reserve: 875out_free_reserve:
876 btrfs_dec_block_group_reservations(fs_info, ins.objectid); 876 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
877 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 877 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
878out_free: 878out_free:
879 extent_clear_unlock_delalloc(inode, async_extent->start, 879 extent_clear_unlock_delalloc(inode, async_extent->start,
880 async_extent->start + 880 async_extent->start +
@@ -1088,7 +1088,7 @@ out_drop_extent_cache:
1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); 1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1089out_reserve: 1089out_reserve:
1090 btrfs_dec_block_group_reservations(fs_info, ins.objectid); 1090 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1091 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 1091 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1092out_unlock: 1092out_unlock:
1093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end, 1093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1094 locked_page, 1094 locked_page,
@@ -1216,10 +1216,9 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1216 return 0; 1216 return 0;
1217} 1217}
1218 1218
1219static noinline int csum_exist_in_range(struct btrfs_root *root, 1219static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1220 u64 bytenr, u64 num_bytes) 1220 u64 bytenr, u64 num_bytes)
1221{ 1221{
1222 struct btrfs_fs_info *fs_info = root->fs_info;
1223 int ret; 1222 int ret;
1224 struct btrfs_ordered_sum *sums; 1223 struct btrfs_ordered_sum *sums;
1225 LIST_HEAD(list); 1224 LIST_HEAD(list);
@@ -1381,7 +1380,7 @@ next_slot:
1381 goto out_check; 1380 goto out_check;
1382 if (extent_type == BTRFS_FILE_EXTENT_REG && !force) 1381 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1383 goto out_check; 1382 goto out_check;
1384 if (btrfs_extent_readonly(root, disk_bytenr)) 1383 if (btrfs_extent_readonly(fs_info, disk_bytenr))
1385 goto out_check; 1384 goto out_check;
1386 if (btrfs_cross_ref_exist(trans, root, ino, 1385 if (btrfs_cross_ref_exist(trans, root, ino,
1387 found_key.offset - 1386 found_key.offset -
@@ -1404,7 +1403,8 @@ next_slot:
1404 * this ensure that csum for a given extent are 1403 * this ensure that csum for a given extent are
1405 * either valid or do not exist. 1404 * either valid or do not exist.
1406 */ 1405 */
1407 if (csum_exist_in_range(root, disk_bytenr, num_bytes)) 1406 if (csum_exist_in_range(fs_info, disk_bytenr,
1407 num_bytes))
1408 goto out_check; 1408 goto out_check;
1409 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) 1409 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1410 goto out_check; 1410 goto out_check;
@@ -1899,10 +1899,9 @@ static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1899 int mirror_num, unsigned long bio_flags, 1899 int mirror_num, unsigned long bio_flags,
1900 u64 bio_offset) 1900 u64 bio_offset)
1901{ 1901{
1902 struct btrfs_root *root = BTRFS_I(inode)->root;
1903 int ret = 0; 1902 int ret = 0;
1904 1903
1905 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); 1904 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1906 BUG_ON(ret); /* -ENOMEM */ 1905 BUG_ON(ret); /* -ENOMEM */
1907 return 0; 1906 return 0;
1908} 1907}
@@ -1919,10 +1918,10 @@ static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
1919 int mirror_num, unsigned long bio_flags, 1918 int mirror_num, unsigned long bio_flags,
1920 u64 bio_offset) 1919 u64 bio_offset)
1921{ 1920{
1922 struct btrfs_root *root = BTRFS_I(inode)->root; 1921 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1923 int ret; 1922 int ret;
1924 1923
1925 ret = btrfs_map_bio(root, bio, mirror_num, 1); 1924 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1926 if (ret) { 1925 if (ret) {
1927 bio->bi_error = ret; 1926 bio->bi_error = ret;
1928 bio_endio(bio); 1927 bio_endio(bio);
@@ -1961,7 +1960,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1961 bio_flags); 1960 bio_flags);
1962 goto out; 1961 goto out;
1963 } else if (!skip_sum) { 1962 } else if (!skip_sum) {
1964 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL); 1963 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
1965 if (ret) 1964 if (ret)
1966 goto out; 1965 goto out;
1967 } 1966 }
@@ -1977,13 +1976,13 @@ static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1977 __btrfs_submit_bio_done); 1976 __btrfs_submit_bio_done);
1978 goto out; 1977 goto out;
1979 } else if (!skip_sum) { 1978 } else if (!skip_sum) {
1980 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); 1979 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1981 if (ret) 1980 if (ret)
1982 goto out; 1981 goto out;
1983 } 1982 }
1984 1983
1985mapit: 1984mapit:
1986 ret = btrfs_map_bio(root, bio, mirror_num, 0); 1985 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
1987 1986
1988out: 1987out:
1989 if (ret < 0) { 1988 if (ret < 0) {
@@ -2194,10 +2193,9 @@ static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2194 ins.objectid = disk_bytenr; 2193 ins.objectid = disk_bytenr;
2195 ins.offset = disk_num_bytes; 2194 ins.offset = disk_num_bytes;
2196 ins.type = BTRFS_EXTENT_ITEM_KEY; 2195 ins.type = BTRFS_EXTENT_ITEM_KEY;
2197 ret = btrfs_alloc_reserved_file_extent(trans, root, 2196 ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2198 root->root_key.objectid, 2197 btrfs_ino(inode), file_pos,
2199 btrfs_ino(inode), file_pos, 2198 ram_bytes, &ins);
2200 ram_bytes, &ins);
2201 /* 2199 /*
2202 * Release the reserved range from inode dirty range map, as it is 2200 * Release the reserved range from inode dirty range map, as it is
2203 * already moved into delayed_ref_head 2201 * already moved into delayed_ref_head
@@ -2654,7 +2652,7 @@ again:
2654 inode_add_bytes(inode, len); 2652 inode_add_bytes(inode, len);
2655 btrfs_release_path(path); 2653 btrfs_release_path(path);
2656 2654
2657 ret = btrfs_inc_extent_ref(trans, root, new->bytenr, 2655 ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
2658 new->disk_len, 0, 2656 new->disk_len, 0,
2659 backref->root_id, backref->inum, 2657 backref->root_id, backref->inum,
2660 new->file_pos); /* start - extent_offset */ 2658 new->file_pos); /* start - extent_offset */
@@ -2855,10 +2853,9 @@ out_kfree:
2855 return NULL; 2853 return NULL;
2856} 2854}
2857 2855
2858static void btrfs_release_delalloc_bytes(struct btrfs_root *root, 2856static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2859 u64 start, u64 len) 2857 u64 start, u64 len)
2860{ 2858{
2861 struct btrfs_fs_info *fs_info = root->fs_info;
2862 struct btrfs_block_group_cache *cache; 2859 struct btrfs_block_group_cache *cache;
2863 2860
2864 cache = btrfs_lookup_block_group(fs_info, start); 2861 cache = btrfs_lookup_block_group(fs_info, start);
@@ -2984,7 +2981,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2984 compress_type, 0, 0, 2981 compress_type, 0, 0,
2985 BTRFS_FILE_EXTENT_REG); 2982 BTRFS_FILE_EXTENT_REG);
2986 if (!ret) 2983 if (!ret)
2987 btrfs_release_delalloc_bytes(root, 2984 btrfs_release_delalloc_bytes(fs_info,
2988 ordered_extent->start, 2985 ordered_extent->start,
2989 ordered_extent->disk_len); 2986 ordered_extent->disk_len);
2990 } 2987 }
@@ -3038,7 +3035,8 @@ out:
3038 if ((ret || !logical_len) && 3035 if ((ret || !logical_len) &&
3039 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && 3036 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3040 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) 3037 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3041 btrfs_free_reserved_extent(root, ordered_extent->start, 3038 btrfs_free_reserved_extent(fs_info,
3039 ordered_extent->start,
3042 ordered_extent->disk_len, 1); 3040 ordered_extent->disk_len, 1);
3043 } 3041 }
3044 3042
@@ -3187,9 +3185,8 @@ void btrfs_add_delayed_iput(struct inode *inode)
3187 spin_unlock(&fs_info->delayed_iput_lock); 3185 spin_unlock(&fs_info->delayed_iput_lock);
3188} 3186}
3189 3187
3190void btrfs_run_delayed_iputs(struct btrfs_root *root) 3188void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3191{ 3189{
3192 struct btrfs_fs_info *fs_info = root->fs_info;
3193 3190
3194 spin_lock(&fs_info->delayed_iput_lock); 3191 spin_lock(&fs_info->delayed_iput_lock);
3195 while (!list_empty(&fs_info->delayed_iputs)) { 3192 while (!list_empty(&fs_info->delayed_iputs)) {
@@ -3255,7 +3252,7 @@ void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3255 3252
3256 if (block_rsv) { 3253 if (block_rsv) {
3257 WARN_ON(block_rsv->size > 0); 3254 WARN_ON(block_rsv->size > 0);
3258 btrfs_free_block_rsv(root, block_rsv); 3255 btrfs_free_block_rsv(fs_info, block_rsv);
3259 } 3256 }
3260} 3257}
3261 3258
@@ -3276,7 +3273,8 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3276 int ret; 3273 int ret;
3277 3274
3278 if (!root->orphan_block_rsv) { 3275 if (!root->orphan_block_rsv) {
3279 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); 3276 block_rsv = btrfs_alloc_block_rsv(fs_info,
3277 BTRFS_BLOCK_RSV_TEMP);
3280 if (!block_rsv) 3278 if (!block_rsv)
3281 return -ENOMEM; 3279 return -ENOMEM;
3282 } 3280 }
@@ -3285,7 +3283,7 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3285 if (!root->orphan_block_rsv) { 3283 if (!root->orphan_block_rsv) {
3286 root->orphan_block_rsv = block_rsv; 3284 root->orphan_block_rsv = block_rsv;
3287 } else if (block_rsv) { 3285 } else if (block_rsv) {
3288 btrfs_free_block_rsv(root, block_rsv); 3286 btrfs_free_block_rsv(fs_info, block_rsv);
3289 block_rsv = NULL; 3287 block_rsv = NULL;
3290 } 3288 }
3291 3289
@@ -3575,7 +3573,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
3575 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE; 3573 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3576 3574
3577 if (root->orphan_block_rsv) 3575 if (root->orphan_block_rsv)
3578 btrfs_block_rsv_release(root, root->orphan_block_rsv, 3576 btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
3579 (u64)-1); 3577 (u64)-1);
3580 3578
3581 if (root->orphan_block_rsv || 3579 if (root->orphan_block_rsv ||
@@ -4063,7 +4061,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4063 goto err; 4061 goto err;
4064 } 4062 }
4065skip_backref: 4063skip_backref:
4066 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); 4064 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
4067 if (ret) { 4065 if (ret) {
4068 btrfs_abort_transaction(trans, ret); 4066 btrfs_abort_transaction(trans, ret);
4069 goto err; 4067 goto err;
@@ -4159,7 +4157,7 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4159 4157
4160out: 4158out:
4161 btrfs_end_transaction(trans, root); 4159 btrfs_end_transaction(trans, root);
4162 btrfs_btree_balance_dirty(root); 4160 btrfs_btree_balance_dirty(root->fs_info);
4163 return ret; 4161 return ret;
4164} 4162}
4165 4163
@@ -4227,7 +4225,7 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4227 } 4225 }
4228 btrfs_release_path(path); 4226 btrfs_release_path(path);
4229 4227
4230 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index); 4228 ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
4231 if (ret) { 4229 if (ret) {
4232 btrfs_abort_transaction(trans, ret); 4230 btrfs_abort_transaction(trans, ret);
4233 goto out; 4231 goto out;
@@ -4296,7 +4294,7 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4296 } 4294 }
4297out: 4295out:
4298 btrfs_end_transaction(trans, root); 4296 btrfs_end_transaction(trans, root);
4299 btrfs_btree_balance_dirty(root); 4297 btrfs_btree_balance_dirty(root->fs_info);
4300 4298
4301 return err; 4299 return err;
4302} 4300}
@@ -4312,7 +4310,7 @@ static int truncate_space_check(struct btrfs_trans_handle *trans,
4312 * This is only used to apply pressure to the enospc system, we don't 4310 * This is only used to apply pressure to the enospc system, we don't
4313 * intend to use this reservation at all. 4311 * intend to use this reservation at all.
4314 */ 4312 */
4315 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); 4313 bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
4316 bytes_deleted *= fs_info->nodesize; 4314 bytes_deleted *= fs_info->nodesize;
4317 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv, 4315 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
4318 bytes_deleted, BTRFS_RESERVE_NO_FLUSH); 4316 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
@@ -4360,7 +4358,7 @@ static int truncate_inline_extent(struct inode *inode,
4360 4358
4361 btrfs_set_file_extent_ram_bytes(leaf, fi, size); 4359 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4362 size = btrfs_file_extent_calc_inline_size(size); 4360 size = btrfs_file_extent_calc_inline_size(size);
4363 btrfs_truncate_item(root, path, size, 1); 4361 btrfs_truncate_item(root->fs_info, path, size, 1);
4364 4362
4365 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 4363 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4366 inode_sub_bytes(inode, item_end + 1 - new_size); 4364 inode_sub_bytes(inode, item_end + 1 - new_size);
@@ -4622,13 +4620,13 @@ delete:
4622 root == fs_info->tree_root)) { 4620 root == fs_info->tree_root)) {
4623 btrfs_set_path_blocking(path); 4621 btrfs_set_path_blocking(path);
4624 bytes_deleted += extent_num_bytes; 4622 bytes_deleted += extent_num_bytes;
4625 ret = btrfs_free_extent(trans, root, extent_start, 4623 ret = btrfs_free_extent(trans, fs_info, extent_start,
4626 extent_num_bytes, 0, 4624 extent_num_bytes, 0,
4627 btrfs_header_owner(leaf), 4625 btrfs_header_owner(leaf),
4628 ino, extent_offset); 4626 ino, extent_offset);
4629 BUG_ON(ret); 4627 BUG_ON(ret);
4630 if (btrfs_should_throttle_delayed_refs(trans, root)) 4628 if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4631 btrfs_async_run_delayed_refs(root, 4629 btrfs_async_run_delayed_refs(fs_info,
4632 trans->delayed_ref_updates * 2, 4630 trans->delayed_ref_updates * 2,
4633 trans->transid, 0); 4631 trans->transid, 0);
4634 if (be_nice) { 4632 if (be_nice) {
@@ -4637,9 +4635,8 @@ delete:
4637 should_end = 1; 4635 should_end = 1;
4638 } 4636 }
4639 if (btrfs_should_throttle_delayed_refs(trans, 4637 if (btrfs_should_throttle_delayed_refs(trans,
4640 root)) { 4638 fs_info))
4641 should_throttle = 1; 4639 should_throttle = 1;
4642 }
4643 } 4640 }
4644 } 4641 }
4645 4642
@@ -4664,7 +4661,9 @@ delete:
4664 unsigned long updates = trans->delayed_ref_updates; 4661 unsigned long updates = trans->delayed_ref_updates;
4665 if (updates) { 4662 if (updates) {
4666 trans->delayed_ref_updates = 0; 4663 trans->delayed_ref_updates = 0;
4667 ret = btrfs_run_delayed_refs(trans, root, updates * 2); 4664 ret = btrfs_run_delayed_refs(trans,
4665 fs_info,
4666 updates * 2);
4668 if (ret && !err) 4667 if (ret && !err)
4669 err = ret; 4668 err = ret;
4670 } 4669 }
@@ -4699,7 +4698,8 @@ error:
4699 unsigned long updates = trans->delayed_ref_updates; 4698 unsigned long updates = trans->delayed_ref_updates;
4700 if (updates) { 4699 if (updates) {
4701 trans->delayed_ref_updates = 0; 4700 trans->delayed_ref_updates = 0;
4702 ret = btrfs_run_delayed_refs(trans, root, updates * 2); 4701 ret = btrfs_run_delayed_refs(trans, fs_info,
4702 updates * 2);
4703 if (ret && !err) 4703 if (ret && !err)
4704 err = ret; 4704 err = ret;
4705 } 4705 }
@@ -5280,7 +5280,7 @@ void btrfs_evict_inode(struct inode *inode)
5280 goto no_delete; 5280 goto no_delete;
5281 } 5281 }
5282 5282
5283 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); 5283 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5284 if (!rsv) { 5284 if (!rsv) {
5285 btrfs_orphan_del(NULL, inode); 5285 btrfs_orphan_del(NULL, inode);
5286 goto no_delete; 5286 goto no_delete;
@@ -5325,14 +5325,14 @@ void btrfs_evict_inode(struct inode *inode)
5325 "Could not get space for a delete, will truncate on mount %d", 5325 "Could not get space for a delete, will truncate on mount %d",
5326 ret); 5326 ret);
5327 btrfs_orphan_del(NULL, inode); 5327 btrfs_orphan_del(NULL, inode);
5328 btrfs_free_block_rsv(root, rsv); 5328 btrfs_free_block_rsv(fs_info, rsv);
5329 goto no_delete; 5329 goto no_delete;
5330 } 5330 }
5331 5331
5332 trans = btrfs_join_transaction(root); 5332 trans = btrfs_join_transaction(root);
5333 if (IS_ERR(trans)) { 5333 if (IS_ERR(trans)) {
5334 btrfs_orphan_del(NULL, inode); 5334 btrfs_orphan_del(NULL, inode);
5335 btrfs_free_block_rsv(root, rsv); 5335 btrfs_free_block_rsv(fs_info, rsv);
5336 goto no_delete; 5336 goto no_delete;
5337 } 5337 }
5338 5338
@@ -5342,7 +5342,7 @@ void btrfs_evict_inode(struct inode *inode)
5342 * again. 5342 * again.
5343 */ 5343 */
5344 if (steal_from_global) { 5344 if (steal_from_global) {
5345 if (!btrfs_check_space_for_delayed_refs(trans, root)) 5345 if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
5346 ret = btrfs_block_rsv_migrate(global_rsv, rsv, 5346 ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5347 min_size, 0); 5347 min_size, 0);
5348 else 5348 else
@@ -5358,7 +5358,7 @@ void btrfs_evict_inode(struct inode *inode)
5358 ret = btrfs_commit_transaction(trans, root); 5358 ret = btrfs_commit_transaction(trans, root);
5359 if (ret) { 5359 if (ret) {
5360 btrfs_orphan_del(NULL, inode); 5360 btrfs_orphan_del(NULL, inode);
5361 btrfs_free_block_rsv(root, rsv); 5361 btrfs_free_block_rsv(fs_info, rsv);
5362 goto no_delete; 5362 goto no_delete;
5363 } 5363 }
5364 continue; 5364 continue;
@@ -5375,10 +5375,10 @@ void btrfs_evict_inode(struct inode *inode)
5375 trans->block_rsv = &fs_info->trans_block_rsv; 5375 trans->block_rsv = &fs_info->trans_block_rsv;
5376 btrfs_end_transaction(trans, root); 5376 btrfs_end_transaction(trans, root);
5377 trans = NULL; 5377 trans = NULL;
5378 btrfs_btree_balance_dirty(root); 5378 btrfs_btree_balance_dirty(fs_info);
5379 } 5379 }
5380 5380
5381 btrfs_free_block_rsv(root, rsv); 5381 btrfs_free_block_rsv(fs_info, rsv);
5382 5382
5383 /* 5383 /*
5384 * Errors here aren't a big deal, it just means we leave orphan items 5384 * Errors here aren't a big deal, it just means we leave orphan items
@@ -5397,7 +5397,7 @@ void btrfs_evict_inode(struct inode *inode)
5397 btrfs_return_ino(root, btrfs_ino(inode)); 5397 btrfs_return_ino(root, btrfs_ino(inode));
5398 5398
5399 btrfs_end_transaction(trans, root); 5399 btrfs_end_transaction(trans, root);
5400 btrfs_btree_balance_dirty(root); 5400 btrfs_btree_balance_dirty(fs_info);
5401no_delete: 5401no_delete:
5402 btrfs_remove_delayed_node(inode); 5402 btrfs_remove_delayed_node(inode);
5403 clear_inode(inode); 5403 clear_inode(inode);
@@ -5443,13 +5443,12 @@ out_err:
5443 * needs to be changed to reflect the root directory of the tree root. This 5443 * needs to be changed to reflect the root directory of the tree root. This
5444 * is kind of like crossing a mount point. 5444 * is kind of like crossing a mount point.
5445 */ 5445 */
5446static int fixup_tree_root_location(struct btrfs_root *root, 5446static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5447 struct inode *dir, 5447 struct inode *dir,
5448 struct dentry *dentry, 5448 struct dentry *dentry,
5449 struct btrfs_key *location, 5449 struct btrfs_key *location,
5450 struct btrfs_root **sub_root) 5450 struct btrfs_root **sub_root)
5451{ 5451{
5452 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5453 struct btrfs_path *path; 5452 struct btrfs_path *path;
5454 struct btrfs_root *new_root; 5453 struct btrfs_root *new_root;
5455 struct btrfs_root_ref *ref; 5454 struct btrfs_root_ref *ref;
@@ -5749,7 +5748,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5749 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); 5748 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5750 5749
5751 index = srcu_read_lock(&fs_info->subvol_srcu); 5750 index = srcu_read_lock(&fs_info->subvol_srcu);
5752 ret = fixup_tree_root_location(root, dir, dentry, 5751 ret = fixup_tree_root_location(fs_info, dir, dentry,
5753 &location, &sub_root); 5752 &location, &sub_root);
5754 if (ret < 0) { 5753 if (ret < 0) {
5755 if (ret != -ENOENT) 5754 if (ret != -ENOENT)
@@ -5822,6 +5821,7 @@ unsigned char btrfs_filetype_table[] = {
5822static int btrfs_real_readdir(struct file *file, struct dir_context *ctx) 5821static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5823{ 5822{
5824 struct inode *inode = file_inode(file); 5823 struct inode *inode = file_inode(file);
5824 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5825 struct btrfs_root *root = BTRFS_I(inode)->root; 5825 struct btrfs_root *root = BTRFS_I(inode)->root;
5826 struct btrfs_item *item; 5826 struct btrfs_item *item;
5827 struct btrfs_dir_item *di; 5827 struct btrfs_dir_item *di;
@@ -5889,7 +5889,7 @@ static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5889 ctx->pos = found_key.offset; 5889 ctx->pos = found_key.offset;
5890 5890
5891 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 5891 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5892 if (verify_dir_item(root, leaf, di)) 5892 if (verify_dir_item(fs_info, leaf, di))
5893 goto next; 5893 goto next;
5894 5894
5895 name_len = btrfs_dir_name_len(leaf, di); 5895 name_len = btrfs_dir_name_len(leaf, di);
@@ -5988,6 +5988,7 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5988 */ 5988 */
5989static int btrfs_dirty_inode(struct inode *inode) 5989static int btrfs_dirty_inode(struct inode *inode)
5990{ 5990{
5991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5991 struct btrfs_root *root = BTRFS_I(inode)->root; 5992 struct btrfs_root *root = BTRFS_I(inode)->root;
5992 struct btrfs_trans_handle *trans; 5993 struct btrfs_trans_handle *trans;
5993 int ret; 5994 int ret;
@@ -6011,7 +6012,7 @@ static int btrfs_dirty_inode(struct inode *inode)
6011 } 6012 }
6012 btrfs_end_transaction(trans, root); 6013 btrfs_end_transaction(trans, root);
6013 if (BTRFS_I(inode)->delayed_node) 6014 if (BTRFS_I(inode)->delayed_node)
6014 btrfs_balance_delayed_items(root); 6015 btrfs_balance_delayed_items(fs_info);
6015 6016
6016 return ret; 6017 return ret;
6017} 6018}
@@ -6394,6 +6395,7 @@ static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6394static int btrfs_mknod(struct inode *dir, struct dentry *dentry, 6395static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6395 umode_t mode, dev_t rdev) 6396 umode_t mode, dev_t rdev)
6396{ 6397{
6398 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6397 struct btrfs_trans_handle *trans; 6399 struct btrfs_trans_handle *trans;
6398 struct btrfs_root *root = BTRFS_I(dir)->root; 6400 struct btrfs_root *root = BTRFS_I(dir)->root;
6399 struct inode *inode = NULL; 6401 struct inode *inode = NULL;
@@ -6447,8 +6449,8 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6447 6449
6448out_unlock: 6450out_unlock:
6449 btrfs_end_transaction(trans, root); 6451 btrfs_end_transaction(trans, root);
6450 btrfs_balance_delayed_items(root); 6452 btrfs_balance_delayed_items(fs_info);
6451 btrfs_btree_balance_dirty(root); 6453 btrfs_btree_balance_dirty(fs_info);
6452 if (drop_inode) { 6454 if (drop_inode) {
6453 inode_dec_link_count(inode); 6455 inode_dec_link_count(inode);
6454 iput(inode); 6456 iput(inode);
@@ -6465,6 +6467,7 @@ out_unlock_inode:
6465static int btrfs_create(struct inode *dir, struct dentry *dentry, 6467static int btrfs_create(struct inode *dir, struct dentry *dentry,
6466 umode_t mode, bool excl) 6468 umode_t mode, bool excl)
6467{ 6469{
6470 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6468 struct btrfs_trans_handle *trans; 6471 struct btrfs_trans_handle *trans;
6469 struct btrfs_root *root = BTRFS_I(dir)->root; 6472 struct btrfs_root *root = BTRFS_I(dir)->root;
6470 struct inode *inode = NULL; 6473 struct inode *inode = NULL;
@@ -6526,8 +6529,8 @@ out_unlock:
6526 inode_dec_link_count(inode); 6529 inode_dec_link_count(inode);
6527 iput(inode); 6530 iput(inode);
6528 } 6531 }
6529 btrfs_balance_delayed_items(root); 6532 btrfs_balance_delayed_items(fs_info);
6530 btrfs_btree_balance_dirty(root); 6533 btrfs_btree_balance_dirty(fs_info);
6531 return err; 6534 return err;
6532 6535
6533out_unlock_inode: 6536out_unlock_inode:
@@ -6542,6 +6545,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6542 struct btrfs_trans_handle *trans = NULL; 6545 struct btrfs_trans_handle *trans = NULL;
6543 struct btrfs_root *root = BTRFS_I(dir)->root; 6546 struct btrfs_root *root = BTRFS_I(dir)->root;
6544 struct inode *inode = d_inode(old_dentry); 6547 struct inode *inode = d_inode(old_dentry);
6548 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6545 u64 index; 6549 u64 index;
6546 int err; 6550 int err;
6547 int drop_inode = 0; 6551 int drop_inode = 0;
@@ -6599,7 +6603,7 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6599 btrfs_log_new_name(trans, inode, NULL, parent); 6603 btrfs_log_new_name(trans, inode, NULL, parent);
6600 } 6604 }
6601 6605
6602 btrfs_balance_delayed_items(root); 6606 btrfs_balance_delayed_items(fs_info);
6603fail: 6607fail:
6604 if (trans) 6608 if (trans)
6605 btrfs_end_transaction(trans, root); 6609 btrfs_end_transaction(trans, root);
@@ -6607,12 +6611,13 @@ fail:
6607 inode_dec_link_count(inode); 6611 inode_dec_link_count(inode);
6608 iput(inode); 6612 iput(inode);
6609 } 6613 }
6610 btrfs_btree_balance_dirty(root); 6614 btrfs_btree_balance_dirty(fs_info);
6611 return err; 6615 return err;
6612} 6616}
6613 6617
6614static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 6618static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6615{ 6619{
6620 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6616 struct inode *inode = NULL; 6621 struct inode *inode = NULL;
6617 struct btrfs_trans_handle *trans; 6622 struct btrfs_trans_handle *trans;
6618 struct btrfs_root *root = BTRFS_I(dir)->root; 6623 struct btrfs_root *root = BTRFS_I(dir)->root;
@@ -6675,8 +6680,8 @@ out_fail:
6675 inode_dec_link_count(inode); 6680 inode_dec_link_count(inode);
6676 iput(inode); 6681 iput(inode);
6677 } 6682 }
6678 btrfs_balance_delayed_items(root); 6683 btrfs_balance_delayed_items(fs_info);
6679 btrfs_btree_balance_dirty(root); 6684 btrfs_btree_balance_dirty(fs_info);
6680 return err; 6685 return err;
6681 6686
6682out_fail_inode: 6687out_fail_inode:
@@ -7255,7 +7260,8 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7255 ins.offset, 0); 7260 ins.offset, 0);
7256 btrfs_dec_block_group_reservations(fs_info, ins.objectid); 7261 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7257 if (IS_ERR(em)) 7262 if (IS_ERR(em))
7258 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 7263 btrfs_free_reserved_extent(fs_info, ins.objectid,
7264 ins.offset, 1);
7259 7265
7260 return em; 7266 return em;
7261} 7267}
@@ -7268,6 +7274,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7268 u64 *orig_start, u64 *orig_block_len, 7274 u64 *orig_start, u64 *orig_block_len,
7269 u64 *ram_bytes) 7275 u64 *ram_bytes)
7270{ 7276{
7277 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7271 struct btrfs_trans_handle *trans; 7278 struct btrfs_trans_handle *trans;
7272 struct btrfs_path *path; 7279 struct btrfs_path *path;
7273 int ret; 7280 int ret;
@@ -7348,7 +7355,7 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7348 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 7355 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7349 } 7356 }
7350 7357
7351 if (btrfs_extent_readonly(root, disk_bytenr)) 7358 if (btrfs_extent_readonly(fs_info, disk_bytenr))
7352 goto out; 7359 goto out;
7353 7360
7354 num_bytes = min(offset + *len, extent_end) - offset; 7361 num_bytes = min(offset + *len, extent_end) - offset;
@@ -7393,8 +7400,8 @@ noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7393 */ 7400 */
7394 disk_bytenr += backref_offset; 7401 disk_bytenr += backref_offset;
7395 disk_bytenr += offset - key.offset; 7402 disk_bytenr += offset - key.offset;
7396 if (csum_exist_in_range(root, disk_bytenr, num_bytes)) 7403 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7397 goto out; 7404 goto out;
7398 /* 7405 /*
7399 * all of the above have passed, it is safe to overwrite this extent 7406 * all of the above have passed, it is safe to overwrite this extent
7400 * without cow 7407 * without cow
@@ -7832,19 +7839,18 @@ err:
7832static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio, 7839static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7833 int mirror_num) 7840 int mirror_num)
7834{ 7841{
7835 struct btrfs_root *root = BTRFS_I(inode)->root; 7842 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7836 int ret; 7843 int ret;
7837 7844
7838 BUG_ON(bio_op(bio) == REQ_OP_WRITE); 7845 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
7839 7846
7840 bio_get(bio); 7847 bio_get(bio);
7841 7848
7842 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 7849 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
7843 BTRFS_WQ_ENDIO_DIO_REPAIR);
7844 if (ret) 7850 if (ret)
7845 goto err; 7851 goto err;
7846 7852
7847 ret = btrfs_map_bio(root, bio, mirror_num, 0); 7853 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
7848err: 7854err:
7849 bio_put(bio); 7855 bio_put(bio);
7850 return ret; 7856 return ret;
@@ -8225,8 +8231,7 @@ static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
8225 unsigned long bio_flags, u64 offset) 8231 unsigned long bio_flags, u64 offset)
8226{ 8232{
8227 int ret; 8233 int ret;
8228 struct btrfs_root *root = BTRFS_I(inode)->root; 8234 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
8229 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
8230 BUG_ON(ret); /* -ENOMEM */ 8235 BUG_ON(ret); /* -ENOMEM */
8231 return 0; 8236 return 0;
8232} 8237}
@@ -8280,8 +8285,7 @@ static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8280 return bio; 8285 return bio;
8281} 8286}
8282 8287
8283static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root, 8288static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
8284 struct inode *inode,
8285 struct btrfs_dio_private *dip, 8289 struct btrfs_dio_private *dip,
8286 struct bio *bio, 8290 struct bio *bio,
8287 u64 file_offset) 8291 u64 file_offset)
@@ -8296,7 +8300,7 @@ static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root *root,
8296 * contention. 8300 * contention.
8297 */ 8301 */
8298 if (dip->logical_offset == file_offset) { 8302 if (dip->logical_offset == file_offset) {
8299 ret = btrfs_lookup_bio_sums_dio(root, inode, dip->orig_bio, 8303 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
8300 file_offset); 8304 file_offset);
8301 if (ret) 8305 if (ret)
8302 return ret; 8306 return ret;
@@ -8319,7 +8323,6 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8319 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 8323 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8320 struct btrfs_dio_private *dip = bio->bi_private; 8324 struct btrfs_dio_private *dip = bio->bi_private;
8321 bool write = bio_op(bio) == REQ_OP_WRITE; 8325 bool write = bio_op(bio) == REQ_OP_WRITE;
8322 struct btrfs_root *root = BTRFS_I(inode)->root;
8323 int ret; 8326 int ret;
8324 8327
8325 if (async_submit) 8328 if (async_submit)
@@ -8347,17 +8350,17 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8347 * If we aren't doing async submit, calculate the csum of the 8350 * If we aren't doing async submit, calculate the csum of the
8348 * bio now. 8351 * bio now.
8349 */ 8352 */
8350 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1); 8353 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
8351 if (ret) 8354 if (ret)
8352 goto err; 8355 goto err;
8353 } else { 8356 } else {
8354 ret = btrfs_lookup_and_bind_dio_csum(root, inode, dip, bio, 8357 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
8355 file_offset); 8358 file_offset);
8356 if (ret) 8359 if (ret)
8357 goto err; 8360 goto err;
8358 } 8361 }
8359map: 8362map:
8360 ret = btrfs_map_bio(root, bio, 0, async_submit); 8363 ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
8361err: 8364err:
8362 bio_put(bio); 8365 bio_put(bio);
8363 return ret; 8366 return ret;
@@ -8595,10 +8598,10 @@ free_ordered:
8595 kfree(dip); 8598 kfree(dip);
8596} 8599}
8597 8600
8598static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, 8601static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8599 const struct iov_iter *iter, loff_t offset) 8602 struct kiocb *iocb,
8603 const struct iov_iter *iter, loff_t offset)
8600{ 8604{
8601 struct btrfs_fs_info *fs_info = root->fs_info;
8602 int seg; 8605 int seg;
8603 int i; 8606 int i;
8604 unsigned int blocksize_mask = fs_info->sectorsize - 1; 8607 unsigned int blocksize_mask = fs_info->sectorsize - 1;
@@ -8642,7 +8645,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8642 bool relock = false; 8645 bool relock = false;
8643 ssize_t ret; 8646 ssize_t ret;
8644 8647
8645 if (check_direct_IO(BTRFS_I(inode)->root, iocb, iter, offset)) 8648 if (check_direct_IO(fs_info, iocb, iter, offset))
8646 return 0; 8649 return 0;
8647 8650
8648 inode_dio_begin(inode); 8651 inode_dio_begin(inode);
@@ -9150,7 +9153,7 @@ static int btrfs_truncate(struct inode *inode)
9150 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for 9153 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9151 * updating the inode. 9154 * updating the inode.
9152 */ 9155 */
9153 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP); 9156 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
9154 if (!rsv) 9157 if (!rsv)
9155 return -ENOMEM; 9158 return -ENOMEM;
9156 rsv->size = min_size; 9159 rsv->size = min_size;
@@ -9198,7 +9201,7 @@ static int btrfs_truncate(struct inode *inode)
9198 } 9201 }
9199 9202
9200 btrfs_end_transaction(trans, root); 9203 btrfs_end_transaction(trans, root);
9201 btrfs_btree_balance_dirty(root); 9204 btrfs_btree_balance_dirty(fs_info);
9202 9205
9203 trans = btrfs_start_transaction(root, 2); 9206 trans = btrfs_start_transaction(root, 2);
9204 if (IS_ERR(trans)) { 9207 if (IS_ERR(trans)) {
@@ -9227,10 +9230,10 @@ static int btrfs_truncate(struct inode *inode)
9227 err = ret; 9230 err = ret;
9228 9231
9229 ret = btrfs_end_transaction(trans, root); 9232 ret = btrfs_end_transaction(trans, root);
9230 btrfs_btree_balance_dirty(root); 9233 btrfs_btree_balance_dirty(fs_info);
9231 } 9234 }
9232out: 9235out:
9233 btrfs_free_block_rsv(root, rsv); 9236 btrfs_free_block_rsv(fs_info, rsv);
9234 9237
9235 if (ret && !err) 9238 if (ret && !err)
9236 err = ret; 9239 err = ret;
@@ -10302,7 +10305,7 @@ out_unlock:
10302 inode_dec_link_count(inode); 10305 inode_dec_link_count(inode);
10303 iput(inode); 10306 iput(inode);
10304 } 10307 }
10305 btrfs_btree_balance_dirty(root); 10308 btrfs_btree_balance_dirty(fs_info);
10306 return err; 10309 return err;
10307 10310
10308out_unlock_inode: 10311out_unlock_inode:
@@ -10365,7 +10368,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10365 ins.offset, 0, 0, 0, 10368 ins.offset, 0, 0, 0,
10366 BTRFS_FILE_EXTENT_PREALLOC); 10369 BTRFS_FILE_EXTENT_PREALLOC);
10367 if (ret) { 10370 if (ret) {
10368 btrfs_free_reserved_extent(root, ins.objectid, 10371 btrfs_free_reserved_extent(fs_info, ins.objectid,
10369 ins.offset, 0); 10372 ins.offset, 0);
10370 btrfs_abort_transaction(trans, ret); 10373 btrfs_abort_transaction(trans, ret);
10371 if (own_trans) 10374 if (own_trans)
@@ -10482,6 +10485,7 @@ static int btrfs_permission(struct inode *inode, int mask)
10482 10485
10483static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 10486static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10484{ 10487{
10488 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10485 struct btrfs_trans_handle *trans; 10489 struct btrfs_trans_handle *trans;
10486 struct btrfs_root *root = BTRFS_I(dir)->root; 10490 struct btrfs_root *root = BTRFS_I(dir)->root;
10487 struct inode *inode = NULL; 10491 struct inode *inode = NULL;
@@ -10541,8 +10545,8 @@ out:
10541 btrfs_end_transaction(trans, root); 10545 btrfs_end_transaction(trans, root);
10542 if (ret) 10546 if (ret)
10543 iput(inode); 10547 iput(inode);
10544 btrfs_balance_delayed_items(root); 10548 btrfs_balance_delayed_items(fs_info);
10545 btrfs_btree_balance_dirty(root); 10549 btrfs_btree_balance_dirty(fs_info);
10546 return ret; 10550 return ret;
10547 10551
10548out_inode: 10552out_inode:
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index d00e4d3c1baf..256af12d0ff0 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -411,7 +411,7 @@ static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
411 411
412 range.len = min(range.len, total_bytes - range.start); 412 range.len = min(range.len, total_bytes - range.start);
413 range.minlen = max(range.minlen, minlen); 413 range.minlen = max(range.minlen, minlen);
414 ret = btrfs_trim_fs(fs_info->tree_root, &range); 414 ret = btrfs_trim_fs(fs_info, &range);
415 if (ret < 0) 415 if (ret < 0)
416 return ret; 416 return ret;
417 417
@@ -487,7 +487,7 @@ static noinline int create_subvol(struct inode *dir,
487 trans = btrfs_start_transaction(root, 0); 487 trans = btrfs_start_transaction(root, 0);
488 if (IS_ERR(trans)) { 488 if (IS_ERR(trans)) {
489 ret = PTR_ERR(trans); 489 ret = PTR_ERR(trans);
490 btrfs_subvolume_release_metadata(root, &block_rsv, 490 btrfs_subvolume_release_metadata(fs_info, &block_rsv,
491 qgroup_reserved); 491 qgroup_reserved);
492 goto fail_free; 492 goto fail_free;
493 } 493 }
@@ -613,7 +613,7 @@ fail:
613 kfree(root_item); 613 kfree(root_item);
614 trans->block_rsv = NULL; 614 trans->block_rsv = NULL;
615 trans->bytes_reserved = 0; 615 trans->bytes_reserved = 0;
616 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 616 btrfs_subvolume_release_metadata(fs_info, &block_rsv, qgroup_reserved);
617 617
618 if (async_transid) { 618 if (async_transid) {
619 *async_transid = trans->transid; 619 *async_transid = trans->transid;
@@ -755,7 +755,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
755 d_instantiate(dentry, inode); 755 d_instantiate(dentry, inode);
756 ret = 0; 756 ret = 0;
757fail: 757fail:
758 btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, 758 btrfs_subvolume_release_metadata(fs_info,
759 &pending_snapshot->block_rsv, 759 &pending_snapshot->block_rsv,
760 pending_snapshot->qgroup_reserved); 760 pending_snapshot->qgroup_reserved);
761dec_and_free: 761dec_and_free:
@@ -2557,7 +2557,7 @@ out_end_trans:
2557 err = ret; 2557 err = ret;
2558 inode->i_flags |= S_DEAD; 2558 inode->i_flags |= S_DEAD;
2559out_release: 2559out_release:
2560 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 2560 btrfs_subvolume_release_metadata(fs_info, &block_rsv, qgroup_reserved);
2561out_up_write: 2561out_up_write:
2562 up_write(&fs_info->subvol_sem); 2562 up_write(&fs_info->subvol_sem);
2563 if (err) { 2563 if (err) {
@@ -2661,9 +2661,8 @@ out:
2661 return ret; 2661 return ret;
2662} 2662}
2663 2663
2664static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2664static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg)
2665{ 2665{
2666 struct btrfs_fs_info *fs_info = root->fs_info;
2667 struct btrfs_ioctl_vol_args *vol_args; 2666 struct btrfs_ioctl_vol_args *vol_args;
2668 int ret; 2667 int ret;
2669 2668
@@ -2681,7 +2680,7 @@ static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2681 } 2680 }
2682 2681
2683 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2682 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2684 ret = btrfs_init_new_device(root->fs_info, vol_args->name); 2683 ret = btrfs_init_new_device(fs_info, vol_args->name);
2685 2684
2686 if (!ret) 2685 if (!ret)
2687 btrfs_info(fs_info, "disk added %s", vol_args->name); 2686 btrfs_info(fs_info, "disk added %s", vol_args->name);
@@ -2697,7 +2696,6 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
2697{ 2696{
2698 struct inode *inode = file_inode(file); 2697 struct inode *inode = file_inode(file);
2699 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2698 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2700 struct btrfs_root *root = BTRFS_I(inode)->root;
2701 struct btrfs_ioctl_vol_args_v2 *vol_args; 2699 struct btrfs_ioctl_vol_args_v2 *vol_args;
2702 int ret; 2700 int ret;
2703 2701
@@ -2725,10 +2723,10 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
2725 2723
2726 mutex_lock(&fs_info->volume_mutex); 2724 mutex_lock(&fs_info->volume_mutex);
2727 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 2725 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
2728 ret = btrfs_rm_device(root, NULL, vol_args->devid); 2726 ret = btrfs_rm_device(fs_info, NULL, vol_args->devid);
2729 } else { 2727 } else {
2730 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 2728 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
2731 ret = btrfs_rm_device(root, vol_args->name, 0); 2729 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
2732 } 2730 }
2733 mutex_unlock(&fs_info->volume_mutex); 2731 mutex_unlock(&fs_info->volume_mutex);
2734 atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 2732 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
@@ -2752,7 +2750,6 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2752{ 2750{
2753 struct inode *inode = file_inode(file); 2751 struct inode *inode = file_inode(file);
2754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2755 struct btrfs_root *root = BTRFS_I(inode)->root;
2756 struct btrfs_ioctl_vol_args *vol_args; 2753 struct btrfs_ioctl_vol_args *vol_args;
2757 int ret; 2754 int ret;
2758 2755
@@ -2776,7 +2773,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2776 2773
2777 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2774 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2778 mutex_lock(&fs_info->volume_mutex); 2775 mutex_lock(&fs_info->volume_mutex);
2779 ret = btrfs_rm_device(root, vol_args->name, 0); 2776 ret = btrfs_rm_device(fs_info, vol_args->name, 0);
2780 mutex_unlock(&fs_info->volume_mutex); 2777 mutex_unlock(&fs_info->volume_mutex);
2781 2778
2782 if (!ret) 2779 if (!ret)
@@ -2790,9 +2787,9 @@ out_drop_write:
2790 return ret; 2787 return ret;
2791} 2788}
2792 2789
2793static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2790static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info,
2791 void __user *arg)
2794{ 2792{
2795 struct btrfs_fs_info *fs_info = root->fs_info;
2796 struct btrfs_ioctl_fs_info_args *fi_args; 2793 struct btrfs_ioctl_fs_info_args *fi_args;
2797 struct btrfs_device *device; 2794 struct btrfs_device *device;
2798 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2795 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
@@ -2823,9 +2820,9 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2823 return ret; 2820 return ret;
2824} 2821}
2825 2822
2826static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2823static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info,
2824 void __user *arg)
2827{ 2825{
2828 struct btrfs_fs_info *fs_info = root->fs_info;
2829 struct btrfs_ioctl_dev_info_args *di_args; 2826 struct btrfs_ioctl_dev_info_args *di_args;
2830 struct btrfs_device *dev; 2827 struct btrfs_device *dev;
2831 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2828 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
@@ -3750,7 +3747,8 @@ process_slot:
3750 3747
3751 if (disko) { 3748 if (disko) {
3752 inode_add_bytes(inode, datal); 3749 inode_add_bytes(inode, datal);
3753 ret = btrfs_inc_extent_ref(trans, root, 3750 ret = btrfs_inc_extent_ref(trans,
3751 fs_info,
3754 disko, diskl, 0, 3752 disko, diskl, 0,
3755 root->root_key.objectid, 3753 root->root_key.objectid,
3756 btrfs_ino(inode), 3754 btrfs_ino(inode),
@@ -4151,9 +4149,9 @@ void btrfs_get_block_group_info(struct list_head *groups_list,
4151 } 4149 }
4152} 4150}
4153 4151
4154static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 4152static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info,
4153 void __user *arg)
4155{ 4154{
4156 struct btrfs_fs_info *fs_info = root->fs_info;
4157 struct btrfs_ioctl_space_args space_args; 4155 struct btrfs_ioctl_space_args space_args;
4158 struct btrfs_ioctl_space_info space; 4156 struct btrfs_ioctl_space_info space;
4159 struct btrfs_ioctl_space_info *dest; 4157 struct btrfs_ioctl_space_info *dest;
@@ -4346,7 +4344,7 @@ out:
4346 return 0; 4344 return 0;
4347} 4345}
4348 4346
4349static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, 4347static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info,
4350 void __user *argp) 4348 void __user *argp)
4351{ 4349{
4352 u64 transid; 4350 u64 transid;
@@ -4357,7 +4355,7 @@ static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4357 } else { 4355 } else {
4358 transid = 0; /* current trans */ 4356 transid = 0; /* current trans */
4359 } 4357 }
4360 return btrfs_wait_for_commit(root, transid); 4358 return btrfs_wait_for_commit(fs_info, transid);
4361} 4359}
4362 4360
4363static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4361static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
@@ -4393,15 +4391,15 @@ out:
4393 return ret; 4391 return ret;
4394} 4392}
4395 4393
4396static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) 4394static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info)
4397{ 4395{
4398 if (!capable(CAP_SYS_ADMIN)) 4396 if (!capable(CAP_SYS_ADMIN))
4399 return -EPERM; 4397 return -EPERM;
4400 4398
4401 return btrfs_scrub_cancel(root->fs_info); 4399 return btrfs_scrub_cancel(fs_info);
4402} 4400}
4403 4401
4404static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, 4402static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info,
4405 void __user *arg) 4403 void __user *arg)
4406{ 4404{
4407 struct btrfs_ioctl_scrub_args *sa; 4405 struct btrfs_ioctl_scrub_args *sa;
@@ -4414,7 +4412,7 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4414 if (IS_ERR(sa)) 4412 if (IS_ERR(sa))
4415 return PTR_ERR(sa); 4413 return PTR_ERR(sa);
4416 4414
4417 ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); 4415 ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress);
4418 4416
4419 if (copy_to_user(arg, sa, sizeof(*sa))) 4417 if (copy_to_user(arg, sa, sizeof(*sa)))
4420 ret = -EFAULT; 4418 ret = -EFAULT;
@@ -4423,7 +4421,7 @@ static long btrfs_ioctl_scrub_progress(struct btrfs_root *root,
4423 return ret; 4421 return ret;
4424} 4422}
4425 4423
4426static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, 4424static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info,
4427 void __user *arg) 4425 void __user *arg)
4428{ 4426{
4429 struct btrfs_ioctl_get_dev_stats *sa; 4427 struct btrfs_ioctl_get_dev_stats *sa;
@@ -4438,7 +4436,7 @@ static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
4438 return -EPERM; 4436 return -EPERM;
4439 } 4437 }
4440 4438
4441 ret = btrfs_get_dev_stats(root, sa); 4439 ret = btrfs_get_dev_stats(fs_info, sa);
4442 4440
4443 if (copy_to_user(arg, sa, sizeof(*sa))) 4441 if (copy_to_user(arg, sa, sizeof(*sa)))
4444 ret = -EFAULT; 4442 ret = -EFAULT;
@@ -4447,9 +4445,9 @@ static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
4447 return ret; 4445 return ret;
4448} 4446}
4449 4447
4450static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 4448static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,
4449 void __user *arg)
4451{ 4450{
4452 struct btrfs_fs_info *fs_info = root->fs_info;
4453 struct btrfs_ioctl_dev_replace_args *p; 4451 struct btrfs_ioctl_dev_replace_args *p;
4454 int ret; 4452 int ret;
4455 4453
@@ -4470,7 +4468,7 @@ static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4470 &fs_info->mutually_exclusive_operation_running, 1)) { 4468 &fs_info->mutually_exclusive_operation_running, 1)) {
4471 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4469 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4472 } else { 4470 } else {
4473 ret = btrfs_dev_replace_by_ioctl(root, p); 4471 ret = btrfs_dev_replace_by_ioctl(fs_info, p);
4474 atomic_set( 4472 atomic_set(
4475 &fs_info->mutually_exclusive_operation_running, 0); 4473 &fs_info->mutually_exclusive_operation_running, 0);
4476 } 4474 }
@@ -4573,7 +4571,7 @@ static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx)
4573 return 0; 4571 return 0;
4574} 4572}
4575 4573
4576static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, 4574static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info,
4577 void __user *arg) 4575 void __user *arg)
4578{ 4576{
4579 int ret = 0; 4577 int ret = 0;
@@ -4603,7 +4601,7 @@ static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root,
4603 goto out; 4601 goto out;
4604 } 4602 }
4605 4603
4606 ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, 4604 ret = iterate_inodes_from_logical(loi->logical, fs_info, path,
4607 build_ino_list, inodes); 4605 build_ino_list, inodes);
4608 if (ret == -EINVAL) 4606 if (ret == -EINVAL)
4609 ret = -ENOENT; 4607 ret = -ENOENT;
@@ -4799,10 +4797,8 @@ out:
4799 return ret; 4797 return ret;
4800} 4798}
4801 4799
4802static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4800static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd)
4803{ 4801{
4804 struct btrfs_fs_info *fs_info = root->fs_info;
4805
4806 if (!capable(CAP_SYS_ADMIN)) 4802 if (!capable(CAP_SYS_ADMIN))
4807 return -EPERM; 4803 return -EPERM;
4808 4804
@@ -4816,10 +4812,9 @@ static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4816 return -EINVAL; 4812 return -EINVAL;
4817} 4813}
4818 4814
4819static long btrfs_ioctl_balance_progress(struct btrfs_root *root, 4815static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info,
4820 void __user *arg) 4816 void __user *arg)
4821{ 4817{
4822 struct btrfs_fs_info *fs_info = root->fs_info;
4823 struct btrfs_ioctl_balance_args *bargs; 4818 struct btrfs_ioctl_balance_args *bargs;
4824 int ret = 0; 4819 int ret = 0;
4825 4820
@@ -5395,12 +5390,11 @@ static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5395 return 0; 5390 return 0;
5396} 5391}
5397 5392
5398static int check_feature_bits(struct btrfs_root *root, 5393static int check_feature_bits(struct btrfs_fs_info *fs_info,
5399 enum btrfs_feature_set set, 5394 enum btrfs_feature_set set,
5400 u64 change_mask, u64 flags, u64 supported_flags, 5395 u64 change_mask, u64 flags, u64 supported_flags,
5401 u64 safe_set, u64 safe_clear) 5396 u64 safe_set, u64 safe_clear)
5402{ 5397{
5403 struct btrfs_fs_info *fs_info = root->fs_info;
5404 const char *type = btrfs_feature_set_names[set]; 5398 const char *type = btrfs_feature_set_names[set];
5405 char *names; 5399 char *names;
5406 u64 disallowed, unsupported; 5400 u64 disallowed, unsupported;
@@ -5455,8 +5449,8 @@ static int check_feature_bits(struct btrfs_root *root,
5455 return 0; 5449 return 0;
5456} 5450}
5457 5451
5458#define check_feature(root, change_mask, flags, mask_base) \ 5452#define check_feature(fs_info, change_mask, flags, mask_base) \
5459check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \ 5453check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \
5460 BTRFS_FEATURE_ ## mask_base ## _SUPP, \ 5454 BTRFS_FEATURE_ ## mask_base ## _SUPP, \
5461 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ 5455 BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \
5462 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) 5456 BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR)
@@ -5483,17 +5477,17 @@ static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5483 !flags[0].incompat_flags) 5477 !flags[0].incompat_flags)
5484 return 0; 5478 return 0;
5485 5479
5486 ret = check_feature(root, flags[0].compat_flags, 5480 ret = check_feature(fs_info, flags[0].compat_flags,
5487 flags[1].compat_flags, COMPAT); 5481 flags[1].compat_flags, COMPAT);
5488 if (ret) 5482 if (ret)
5489 return ret; 5483 return ret;
5490 5484
5491 ret = check_feature(root, flags[0].compat_ro_flags, 5485 ret = check_feature(fs_info, flags[0].compat_ro_flags,
5492 flags[1].compat_ro_flags, COMPAT_RO); 5486 flags[1].compat_ro_flags, COMPAT_RO);
5493 if (ret) 5487 if (ret)
5494 return ret; 5488 return ret;
5495 5489
5496 ret = check_feature(root, flags[0].incompat_flags, 5490 ret = check_feature(fs_info, flags[0].incompat_flags,
5497 flags[1].incompat_flags, INCOMPAT); 5491 flags[1].incompat_flags, INCOMPAT);
5498 if (ret) 5492 if (ret)
5499 return ret; 5493 return ret;
@@ -5572,15 +5566,15 @@ long btrfs_ioctl(struct file *file, unsigned int
5572 case BTRFS_IOC_RESIZE: 5566 case BTRFS_IOC_RESIZE:
5573 return btrfs_ioctl_resize(file, argp); 5567 return btrfs_ioctl_resize(file, argp);
5574 case BTRFS_IOC_ADD_DEV: 5568 case BTRFS_IOC_ADD_DEV:
5575 return btrfs_ioctl_add_dev(root, argp); 5569 return btrfs_ioctl_add_dev(fs_info, argp);
5576 case BTRFS_IOC_RM_DEV: 5570 case BTRFS_IOC_RM_DEV:
5577 return btrfs_ioctl_rm_dev(file, argp); 5571 return btrfs_ioctl_rm_dev(file, argp);
5578 case BTRFS_IOC_RM_DEV_V2: 5572 case BTRFS_IOC_RM_DEV_V2:
5579 return btrfs_ioctl_rm_dev_v2(file, argp); 5573 return btrfs_ioctl_rm_dev_v2(file, argp);
5580 case BTRFS_IOC_FS_INFO: 5574 case BTRFS_IOC_FS_INFO:
5581 return btrfs_ioctl_fs_info(root, argp); 5575 return btrfs_ioctl_fs_info(fs_info, argp);
5582 case BTRFS_IOC_DEV_INFO: 5576 case BTRFS_IOC_DEV_INFO:
5583 return btrfs_ioctl_dev_info(root, argp); 5577 return btrfs_ioctl_dev_info(fs_info, argp);
5584 case BTRFS_IOC_BALANCE: 5578 case BTRFS_IOC_BALANCE:
5585 return btrfs_ioctl_balance(file, NULL); 5579 return btrfs_ioctl_balance(file, NULL);
5586 case BTRFS_IOC_TRANS_START: 5580 case BTRFS_IOC_TRANS_START:
@@ -5596,9 +5590,9 @@ long btrfs_ioctl(struct file *file, unsigned int
5596 case BTRFS_IOC_INO_PATHS: 5590 case BTRFS_IOC_INO_PATHS:
5597 return btrfs_ioctl_ino_to_path(root, argp); 5591 return btrfs_ioctl_ino_to_path(root, argp);
5598 case BTRFS_IOC_LOGICAL_INO: 5592 case BTRFS_IOC_LOGICAL_INO:
5599 return btrfs_ioctl_logical_to_ino(root, argp); 5593 return btrfs_ioctl_logical_to_ino(fs_info, argp);
5600 case BTRFS_IOC_SPACE_INFO: 5594 case BTRFS_IOC_SPACE_INFO:
5601 return btrfs_ioctl_space_info(root, argp); 5595 return btrfs_ioctl_space_info(fs_info, argp);
5602 case BTRFS_IOC_SYNC: { 5596 case BTRFS_IOC_SYNC: {
5603 int ret; 5597 int ret;
5604 5598
@@ -5617,19 +5611,19 @@ long btrfs_ioctl(struct file *file, unsigned int
5617 case BTRFS_IOC_START_SYNC: 5611 case BTRFS_IOC_START_SYNC:
5618 return btrfs_ioctl_start_sync(root, argp); 5612 return btrfs_ioctl_start_sync(root, argp);
5619 case BTRFS_IOC_WAIT_SYNC: 5613 case BTRFS_IOC_WAIT_SYNC:
5620 return btrfs_ioctl_wait_sync(root, argp); 5614 return btrfs_ioctl_wait_sync(fs_info, argp);
5621 case BTRFS_IOC_SCRUB: 5615 case BTRFS_IOC_SCRUB:
5622 return btrfs_ioctl_scrub(file, argp); 5616 return btrfs_ioctl_scrub(file, argp);
5623 case BTRFS_IOC_SCRUB_CANCEL: 5617 case BTRFS_IOC_SCRUB_CANCEL:
5624 return btrfs_ioctl_scrub_cancel(root, argp); 5618 return btrfs_ioctl_scrub_cancel(fs_info);
5625 case BTRFS_IOC_SCRUB_PROGRESS: 5619 case BTRFS_IOC_SCRUB_PROGRESS:
5626 return btrfs_ioctl_scrub_progress(root, argp); 5620 return btrfs_ioctl_scrub_progress(fs_info, argp);
5627 case BTRFS_IOC_BALANCE_V2: 5621 case BTRFS_IOC_BALANCE_V2:
5628 return btrfs_ioctl_balance(file, argp); 5622 return btrfs_ioctl_balance(file, argp);
5629 case BTRFS_IOC_BALANCE_CTL: 5623 case BTRFS_IOC_BALANCE_CTL:
5630 return btrfs_ioctl_balance_ctl(root, arg); 5624 return btrfs_ioctl_balance_ctl(fs_info, arg);
5631 case BTRFS_IOC_BALANCE_PROGRESS: 5625 case BTRFS_IOC_BALANCE_PROGRESS:
5632 return btrfs_ioctl_balance_progress(root, argp); 5626 return btrfs_ioctl_balance_progress(fs_info, argp);
5633 case BTRFS_IOC_SET_RECEIVED_SUBVOL: 5627 case BTRFS_IOC_SET_RECEIVED_SUBVOL:
5634 return btrfs_ioctl_set_received_subvol(file, argp); 5628 return btrfs_ioctl_set_received_subvol(file, argp);
5635#ifdef CONFIG_64BIT 5629#ifdef CONFIG_64BIT
@@ -5639,7 +5633,7 @@ long btrfs_ioctl(struct file *file, unsigned int
5639 case BTRFS_IOC_SEND: 5633 case BTRFS_IOC_SEND:
5640 return btrfs_ioctl_send(file, argp); 5634 return btrfs_ioctl_send(file, argp);
5641 case BTRFS_IOC_GET_DEV_STATS: 5635 case BTRFS_IOC_GET_DEV_STATS:
5642 return btrfs_ioctl_get_dev_stats(root, argp); 5636 return btrfs_ioctl_get_dev_stats(fs_info, argp);
5643 case BTRFS_IOC_QUOTA_CTL: 5637 case BTRFS_IOC_QUOTA_CTL:
5644 return btrfs_ioctl_quota_ctl(file, argp); 5638 return btrfs_ioctl_quota_ctl(file, argp);
5645 case BTRFS_IOC_QGROUP_ASSIGN: 5639 case BTRFS_IOC_QGROUP_ASSIGN:
@@ -5655,7 +5649,7 @@ long btrfs_ioctl(struct file *file, unsigned int
5655 case BTRFS_IOC_QUOTA_RESCAN_WAIT: 5649 case BTRFS_IOC_QUOTA_RESCAN_WAIT:
5656 return btrfs_ioctl_quota_rescan_wait(file, argp); 5650 return btrfs_ioctl_quota_rescan_wait(file, argp);
5657 case BTRFS_IOC_DEV_REPLACE: 5651 case BTRFS_IOC_DEV_REPLACE:
5658 return btrfs_ioctl_dev_replace(root, argp); 5652 return btrfs_ioctl_dev_replace(fs_info, argp);
5659 case BTRFS_IOC_GET_FSLABEL: 5653 case BTRFS_IOC_GET_FSLABEL:
5660 return btrfs_ioctl_get_fslabel(file, argp); 5654 return btrfs_ioctl_get_fslabel(file, argp);
5661 case BTRFS_IOC_SET_FSLABEL: 5655 case BTRFS_IOC_SET_FSLABEL:
diff --git a/fs/btrfs/print-tree.c b/fs/btrfs/print-tree.c
index 3251a0dd03a2..cdafbf92ef0c 100644
--- a/fs/btrfs/print-tree.c
+++ b/fs/btrfs/print-tree.c
@@ -161,9 +161,8 @@ static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
161 } 161 }
162} 162}
163 163
164void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l) 164void btrfs_print_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *l)
165{ 165{
166 struct btrfs_fs_info *fs_info = root->fs_info;
167 int i; 166 int i;
168 u32 type, nr; 167 u32 type, nr;
169 struct btrfs_item *item; 168 struct btrfs_item *item;
@@ -184,7 +183,8 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
184 nr = btrfs_header_nritems(l); 183 nr = btrfs_header_nritems(l);
185 184
186 btrfs_info(fs_info, "leaf %llu total ptrs %d free space %d", 185 btrfs_info(fs_info, "leaf %llu total ptrs %d free space %d",
187 btrfs_header_bytenr(l), nr, btrfs_leaf_free_space(root, l)); 186 btrfs_header_bytenr(l), nr,
187 btrfs_leaf_free_space(fs_info, l));
188 for (i = 0 ; i < nr ; i++) { 188 for (i = 0 ; i < nr ; i++) {
189 item = btrfs_item_nr(i); 189 item = btrfs_item_nr(i);
190 btrfs_item_key_to_cpu(l, &key, i); 190 btrfs_item_key_to_cpu(l, &key, i);
@@ -315,9 +315,8 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
315 } 315 }
316} 316}
317 317
318void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c) 318void btrfs_print_tree(struct btrfs_fs_info *fs_info, struct extent_buffer *c)
319{ 319{
320 struct btrfs_fs_info *fs_info = root->fs_info;
321 int i; u32 nr; 320 int i; u32 nr;
322 struct btrfs_key key; 321 struct btrfs_key key;
323 int level; 322 int level;
@@ -327,7 +326,7 @@ void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
327 nr = btrfs_header_nritems(c); 326 nr = btrfs_header_nritems(c);
328 level = btrfs_header_level(c); 327 level = btrfs_header_level(c);
329 if (level == 0) { 328 if (level == 0) {
330 btrfs_print_leaf(root, c); 329 btrfs_print_leaf(fs_info, c);
331 return; 330 return;
332 } 331 }
333 btrfs_info(fs_info, 332 btrfs_info(fs_info,
@@ -341,7 +340,7 @@ void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
341 btrfs_node_blockptr(c, i)); 340 btrfs_node_blockptr(c, i));
342 } 341 }
343 for (i = 0; i < nr; i++) { 342 for (i = 0; i < nr; i++) {
344 struct extent_buffer *next = read_tree_block(root, 343 struct extent_buffer *next = read_tree_block(fs_info,
345 btrfs_node_blockptr(c, i), 344 btrfs_node_blockptr(c, i),
346 btrfs_node_ptr_generation(c, i)); 345 btrfs_node_ptr_generation(c, i));
347 if (IS_ERR(next)) { 346 if (IS_ERR(next)) {
@@ -357,7 +356,7 @@ void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
357 if (btrfs_header_level(next) != 356 if (btrfs_header_level(next) !=
358 level - 1) 357 level - 1)
359 BUG(); 358 BUG();
360 btrfs_print_tree(root, next); 359 btrfs_print_tree(fs_info, next);
361 free_extent_buffer(next); 360 free_extent_buffer(next);
362 } 361 }
363} 362}
diff --git a/fs/btrfs/print-tree.h b/fs/btrfs/print-tree.h
index 7faddfacc5bd..4f2e0ea0e95a 100644
--- a/fs/btrfs/print-tree.h
+++ b/fs/btrfs/print-tree.h
@@ -18,6 +18,6 @@
18 18
19#ifndef __PRINT_TREE_ 19#ifndef __PRINT_TREE_
20#define __PRINT_TREE_ 20#define __PRINT_TREE_
21void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l); 21void btrfs_print_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *l);
22void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c); 22void btrfs_print_tree(struct btrfs_fs_info *fs_info, struct extent_buffer *c);
23#endif 23#endif
diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index f4a58761ae4e..f2621e330954 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -301,6 +301,7 @@ static int inherit_props(struct btrfs_trans_handle *trans,
301 struct inode *parent) 301 struct inode *parent)
302{ 302{
303 struct btrfs_root *root = BTRFS_I(inode)->root; 303 struct btrfs_root *root = BTRFS_I(inode)->root;
304 struct btrfs_fs_info *fs_info = root->fs_info;
304 int ret; 305 int ret;
305 int i; 306 int i;
306 307
@@ -320,14 +321,14 @@ static int inherit_props(struct btrfs_trans_handle *trans,
320 if (!value) 321 if (!value)
321 continue; 322 continue;
322 323
323 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 324 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
324 ret = btrfs_block_rsv_add(root, trans->block_rsv, 325 ret = btrfs_block_rsv_add(root, trans->block_rsv,
325 num_bytes, BTRFS_RESERVE_NO_FLUSH); 326 num_bytes, BTRFS_RESERVE_NO_FLUSH);
326 if (ret) 327 if (ret)
327 goto out; 328 goto out;
328 ret = __btrfs_set_prop(trans, inode, h->xattr_name, 329 ret = __btrfs_set_prop(trans, inode, h->xattr_name,
329 value, strlen(value), 0); 330 value, strlen(value), 0);
330 btrfs_block_rsv_release(root, trans->block_rsv, num_bytes); 331 btrfs_block_rsv_release(fs_info, trans->block_rsv, num_bytes);
331 if (ret) 332 if (ret)
332 goto out; 333 goto out;
333 } 334 }
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index eb389b1452ae..135bb7986dfb 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -1509,8 +1509,7 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
1509 record->old_roots = NULL; 1509 record->old_roots = NULL;
1510 1510
1511 spin_lock(&delayed_refs->lock); 1511 spin_lock(&delayed_refs->lock);
1512 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, 1512 ret = btrfs_qgroup_trace_extent_nolock(fs_info, delayed_refs, record);
1513 record);
1514 spin_unlock(&delayed_refs->lock); 1513 spin_unlock(&delayed_refs->lock);
1515 if (ret > 0) 1514 if (ret > 0)
1516 kfree(record); 1515 kfree(record);
@@ -1518,10 +1517,9 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
1518} 1517}
1519 1518
1520int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans, 1519int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1521 struct btrfs_root *root, 1520 struct btrfs_fs_info *fs_info,
1522 struct extent_buffer *eb) 1521 struct extent_buffer *eb)
1523{ 1522{
1524 struct btrfs_fs_info *fs_info = root->fs_info;
1525 int nr = btrfs_header_nritems(eb); 1523 int nr = btrfs_header_nritems(eb);
1526 int i, extent_type, ret; 1524 int i, extent_type, ret;
1527 struct btrfs_key key; 1525 struct btrfs_key key;
@@ -1645,7 +1643,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1645 } 1643 }
1646 1644
1647 if (root_level == 0) { 1645 if (root_level == 0) {
1648 ret = btrfs_qgroup_trace_leaf_items(trans, root, root_eb); 1646 ret = btrfs_qgroup_trace_leaf_items(trans, fs_info, root_eb);
1649 goto out; 1647 goto out;
1650 } 1648 }
1651 1649
@@ -1683,7 +1681,7 @@ walk_down:
1683 child_bytenr = btrfs_node_blockptr(eb, parent_slot); 1681 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
1684 child_gen = btrfs_node_ptr_generation(eb, parent_slot); 1682 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
1685 1683
1686 eb = read_tree_block(root, child_bytenr, child_gen); 1684 eb = read_tree_block(fs_info, child_bytenr, child_gen);
1687 if (IS_ERR(eb)) { 1685 if (IS_ERR(eb)) {
1688 ret = PTR_ERR(eb); 1686 ret = PTR_ERR(eb);
1689 goto out; 1687 goto out;
@@ -1709,8 +1707,8 @@ walk_down:
1709 } 1707 }
1710 1708
1711 if (level == 0) { 1709 if (level == 0) {
1712 ret = btrfs_qgroup_trace_leaf_items(trans, root, 1710 ret = btrfs_qgroup_trace_leaf_items(trans,fs_info,
1713 path->nodes[level]); 1711 path->nodes[level]);
1714 if (ret) 1712 if (ret)
1715 goto out; 1713 goto out;
1716 1714
diff --git a/fs/btrfs/qgroup.h b/fs/btrfs/qgroup.h
index 99c879dbedc1..416ae8e1d23c 100644
--- a/fs/btrfs/qgroup.h
+++ b/fs/btrfs/qgroup.h
@@ -129,7 +129,7 @@ int btrfs_qgroup_trace_extent(struct btrfs_trans_handle *trans,
129 * Return <0 for error(ENOMEM) 129 * Return <0 for error(ENOMEM)
130 */ 130 */
131int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans, 131int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
132 struct btrfs_root *root, 132 struct btrfs_fs_info *fs_info,
133 struct extent_buffer *eb); 133 struct extent_buffer *eb);
134/* 134/*
135 * Inform qgroup to trace a whole subtree, including all its child tree 135 * Inform qgroup to trace a whole subtree, including all its child tree
diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
index 9a67346b48f5..d2a9a1ee5361 100644
--- a/fs/btrfs/raid56.c
+++ b/fs/btrfs/raid56.c
@@ -969,8 +969,9 @@ static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
969 * allocation and initial setup for the btrfs_raid_bio. Not 969 * allocation and initial setup for the btrfs_raid_bio. Not
970 * this does not allocate any pages for rbio->pages. 970 * this does not allocate any pages for rbio->pages.
971 */ 971 */
972static struct btrfs_raid_bio *alloc_rbio(struct btrfs_root *root, 972static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
973 struct btrfs_bio *bbio, u64 stripe_len) 973 struct btrfs_bio *bbio,
974 u64 stripe_len)
974{ 975{
975 struct btrfs_raid_bio *rbio; 976 struct btrfs_raid_bio *rbio;
976 int nr_data = 0; 977 int nr_data = 0;
@@ -991,7 +992,7 @@ static struct btrfs_raid_bio *alloc_rbio(struct btrfs_root *root,
991 INIT_LIST_HEAD(&rbio->stripe_cache); 992 INIT_LIST_HEAD(&rbio->stripe_cache);
992 INIT_LIST_HEAD(&rbio->hash_list); 993 INIT_LIST_HEAD(&rbio->hash_list);
993 rbio->bbio = bbio; 994 rbio->bbio = bbio;
994 rbio->fs_info = root->fs_info; 995 rbio->fs_info = fs_info;
995 rbio->stripe_len = stripe_len; 996 rbio->stripe_len = stripe_len;
996 rbio->nr_pages = num_pages; 997 rbio->nr_pages = num_pages;
997 rbio->real_stripes = real_stripes; 998 rbio->real_stripes = real_stripes;
@@ -1734,16 +1735,15 @@ static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
1734/* 1735/*
1735 * our main entry point for writes from the rest of the FS. 1736 * our main entry point for writes from the rest of the FS.
1736 */ 1737 */
1737int raid56_parity_write(struct btrfs_root *root, struct bio *bio, 1738int raid56_parity_write(struct btrfs_fs_info *fs_info, struct bio *bio,
1738 struct btrfs_bio *bbio, u64 stripe_len) 1739 struct btrfs_bio *bbio, u64 stripe_len)
1739{ 1740{
1740 struct btrfs_fs_info *fs_info = root->fs_info;
1741 struct btrfs_raid_bio *rbio; 1741 struct btrfs_raid_bio *rbio;
1742 struct btrfs_plug_cb *plug = NULL; 1742 struct btrfs_plug_cb *plug = NULL;
1743 struct blk_plug_cb *cb; 1743 struct blk_plug_cb *cb;
1744 int ret; 1744 int ret;
1745 1745
1746 rbio = alloc_rbio(root, bbio, stripe_len); 1746 rbio = alloc_rbio(fs_info, bbio, stripe_len);
1747 if (IS_ERR(rbio)) { 1747 if (IS_ERR(rbio)) {
1748 btrfs_put_bbio(bbio); 1748 btrfs_put_bbio(bbio);
1749 return PTR_ERR(rbio); 1749 return PTR_ERR(rbio);
@@ -2113,15 +2113,14 @@ cleanup:
2113 * so we assume the bio they send down corresponds to a failed part 2113 * so we assume the bio they send down corresponds to a failed part
2114 * of the drive. 2114 * of the drive.
2115 */ 2115 */
2116int raid56_parity_recover(struct btrfs_root *root, struct bio *bio, 2116int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
2117 struct btrfs_bio *bbio, u64 stripe_len, 2117 struct btrfs_bio *bbio, u64 stripe_len,
2118 int mirror_num, int generic_io) 2118 int mirror_num, int generic_io)
2119{ 2119{
2120 struct btrfs_fs_info *fs_info = root->fs_info;
2121 struct btrfs_raid_bio *rbio; 2120 struct btrfs_raid_bio *rbio;
2122 int ret; 2121 int ret;
2123 2122
2124 rbio = alloc_rbio(root, bbio, stripe_len); 2123 rbio = alloc_rbio(fs_info, bbio, stripe_len);
2125 if (IS_ERR(rbio)) { 2124 if (IS_ERR(rbio)) {
2126 if (generic_io) 2125 if (generic_io)
2127 btrfs_put_bbio(bbio); 2126 btrfs_put_bbio(bbio);
@@ -2203,16 +2202,15 @@ static void read_rebuild_work(struct btrfs_work *work)
2203 */ 2202 */
2204 2203
2205struct btrfs_raid_bio * 2204struct btrfs_raid_bio *
2206raid56_parity_alloc_scrub_rbio(struct btrfs_root *root, struct bio *bio, 2205raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
2207 struct btrfs_bio *bbio, u64 stripe_len, 2206 struct btrfs_bio *bbio, u64 stripe_len,
2208 struct btrfs_device *scrub_dev, 2207 struct btrfs_device *scrub_dev,
2209 unsigned long *dbitmap, int stripe_nsectors) 2208 unsigned long *dbitmap, int stripe_nsectors)
2210{ 2209{
2211 struct btrfs_fs_info *fs_info = root->fs_info;
2212 struct btrfs_raid_bio *rbio; 2210 struct btrfs_raid_bio *rbio;
2213 int i; 2211 int i;
2214 2212
2215 rbio = alloc_rbio(root, bbio, stripe_len); 2213 rbio = alloc_rbio(fs_info, bbio, stripe_len);
2216 if (IS_ERR(rbio)) 2214 if (IS_ERR(rbio))
2217 return NULL; 2215 return NULL;
2218 bio_list_add(&rbio->bio_list, bio); 2216 bio_list_add(&rbio->bio_list, bio);
@@ -2653,12 +2651,12 @@ void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
2653/* The following code is used for dev replace of a missing RAID 5/6 device. */ 2651/* The following code is used for dev replace of a missing RAID 5/6 device. */
2654 2652
2655struct btrfs_raid_bio * 2653struct btrfs_raid_bio *
2656raid56_alloc_missing_rbio(struct btrfs_root *root, struct bio *bio, 2654raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
2657 struct btrfs_bio *bbio, u64 length) 2655 struct btrfs_bio *bbio, u64 length)
2658{ 2656{
2659 struct btrfs_raid_bio *rbio; 2657 struct btrfs_raid_bio *rbio;
2660 2658
2661 rbio = alloc_rbio(root, bbio, length); 2659 rbio = alloc_rbio(fs_info, bbio, length);
2662 if (IS_ERR(rbio)) 2660 if (IS_ERR(rbio))
2663 return NULL; 2661 return NULL;
2664 2662
diff --git a/fs/btrfs/raid56.h b/fs/btrfs/raid56.h
index 8b694699d502..4ee4fe346838 100644
--- a/fs/btrfs/raid56.h
+++ b/fs/btrfs/raid56.h
@@ -42,24 +42,24 @@ static inline int nr_data_stripes(struct map_lookup *map)
42struct btrfs_raid_bio; 42struct btrfs_raid_bio;
43struct btrfs_device; 43struct btrfs_device;
44 44
45int raid56_parity_recover(struct btrfs_root *root, struct bio *bio, 45int raid56_parity_recover(struct btrfs_fs_info *fs_info, struct bio *bio,
46 struct btrfs_bio *bbio, u64 stripe_len, 46 struct btrfs_bio *bbio, u64 stripe_len,
47 int mirror_num, int generic_io); 47 int mirror_num, int generic_io);
48int raid56_parity_write(struct btrfs_root *root, struct bio *bio, 48int raid56_parity_write(struct btrfs_fs_info *fs_info, struct bio *bio,
49 struct btrfs_bio *bbio, u64 stripe_len); 49 struct btrfs_bio *bbio, u64 stripe_len);
50 50
51void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page, 51void raid56_add_scrub_pages(struct btrfs_raid_bio *rbio, struct page *page,
52 u64 logical); 52 u64 logical);
53 53
54struct btrfs_raid_bio * 54struct btrfs_raid_bio *
55raid56_parity_alloc_scrub_rbio(struct btrfs_root *root, struct bio *bio, 55raid56_parity_alloc_scrub_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
56 struct btrfs_bio *bbio, u64 stripe_len, 56 struct btrfs_bio *bbio, u64 stripe_len,
57 struct btrfs_device *scrub_dev, 57 struct btrfs_device *scrub_dev,
58 unsigned long *dbitmap, int stripe_nsectors); 58 unsigned long *dbitmap, int stripe_nsectors);
59void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio); 59void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio);
60 60
61struct btrfs_raid_bio * 61struct btrfs_raid_bio *
62raid56_alloc_missing_rbio(struct btrfs_root *root, struct bio *bio, 62raid56_alloc_missing_rbio(struct btrfs_fs_info *fs_info, struct bio *bio,
63 struct btrfs_bio *bbio, u64 length); 63 struct btrfs_bio *bbio, u64 length);
64void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio); 64void raid56_submit_missing_rbio(struct btrfs_raid_bio *rbio);
65 65
diff --git a/fs/btrfs/reada.c b/fs/btrfs/reada.c
index 8d36fb457594..e88bca87f5d2 100644
--- a/fs/btrfs/reada.c
+++ b/fs/btrfs/reada.c
@@ -303,14 +303,13 @@ static struct reada_zone *reada_find_zone(struct btrfs_fs_info *fs_info,
303 return zone; 303 return zone;
304} 304}
305 305
306static struct reada_extent *reada_find_extent(struct btrfs_root *root, 306static struct reada_extent *reada_find_extent(struct btrfs_fs_info *fs_info,
307 u64 logical, 307 u64 logical,
308 struct btrfs_key *top) 308 struct btrfs_key *top)
309{ 309{
310 int ret; 310 int ret;
311 struct reada_extent *re = NULL; 311 struct reada_extent *re = NULL;
312 struct reada_extent *re_exist = NULL; 312 struct reada_extent *re_exist = NULL;
313 struct btrfs_fs_info *fs_info = root->fs_info;
314 struct btrfs_bio *bbio = NULL; 313 struct btrfs_bio *bbio = NULL;
315 struct btrfs_device *dev; 314 struct btrfs_device *dev;
316 struct btrfs_device *prev_dev; 315 struct btrfs_device *prev_dev;
@@ -549,7 +548,7 @@ static int reada_add_block(struct reada_control *rc, u64 logical,
549 struct reada_extctl *rec; 548 struct reada_extctl *rec;
550 549
551 /* takes one ref */ 550 /* takes one ref */
552 re = reada_find_extent(fs_info->tree_root, logical, top); 551 re = reada_find_extent(fs_info, logical, top);
553 if (!re) 552 if (!re)
554 return -1; 553 return -1;
555 554
@@ -705,8 +704,7 @@ static int reada_start_machine_dev(struct btrfs_fs_info *fs_info,
705 logical = re->logical; 704 logical = re->logical;
706 705
707 atomic_inc(&dev->reada_in_flight); 706 atomic_inc(&dev->reada_in_flight);
708 ret = reada_tree_block_flagged(fs_info->extent_root, logical, 707 ret = reada_tree_block_flagged(fs_info, logical, mirror_num, &eb);
709 mirror_num, &eb);
710 if (ret) 708 if (ret)
711 __readahead_hook(fs_info, re, NULL, ret); 709 __readahead_hook(fs_info, re, NULL, ret);
712 else if (eb) 710 else if (eb)
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index bc6ccd3a6051..341a099c4967 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -1734,7 +1734,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
1734 dirty = 1; 1734 dirty = 1;
1735 1735
1736 key.offset -= btrfs_file_extent_offset(leaf, fi); 1736 key.offset -= btrfs_file_extent_offset(leaf, fi);
1737 ret = btrfs_inc_extent_ref(trans, root, new_bytenr, 1737 ret = btrfs_inc_extent_ref(trans, fs_info, new_bytenr,
1738 num_bytes, parent, 1738 num_bytes, parent,
1739 btrfs_header_owner(leaf), 1739 btrfs_header_owner(leaf),
1740 key.objectid, key.offset); 1740 key.objectid, key.offset);
@@ -1743,7 +1743,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
1743 break; 1743 break;
1744 } 1744 }
1745 1745
1746 ret = btrfs_free_extent(trans, root, bytenr, num_bytes, 1746 ret = btrfs_free_extent(trans, fs_info, bytenr, num_bytes,
1747 parent, btrfs_header_owner(leaf), 1747 parent, btrfs_header_owner(leaf),
1748 key.objectid, key.offset); 1748 key.objectid, key.offset);
1749 if (ret) { 1749 if (ret) {
@@ -1868,7 +1868,7 @@ again:
1868 break; 1868 break;
1869 } 1869 }
1870 1870
1871 eb = read_tree_block(dest, old_bytenr, old_ptr_gen); 1871 eb = read_tree_block(fs_info, old_bytenr, old_ptr_gen);
1872 if (IS_ERR(eb)) { 1872 if (IS_ERR(eb)) {
1873 ret = PTR_ERR(eb); 1873 ret = PTR_ERR(eb);
1874 break; 1874 break;
@@ -1944,21 +1944,21 @@ again:
1944 path->slots[level], old_ptr_gen); 1944 path->slots[level], old_ptr_gen);
1945 btrfs_mark_buffer_dirty(path->nodes[level]); 1945 btrfs_mark_buffer_dirty(path->nodes[level]);
1946 1946
1947 ret = btrfs_inc_extent_ref(trans, src, old_bytenr, blocksize, 1947 ret = btrfs_inc_extent_ref(trans, fs_info, old_bytenr,
1948 path->nodes[level]->start, 1948 blocksize, path->nodes[level]->start,
1949 src->root_key.objectid, level - 1, 0); 1949 src->root_key.objectid, level - 1, 0);
1950 BUG_ON(ret); 1950 BUG_ON(ret);
1951 ret = btrfs_inc_extent_ref(trans, dest, new_bytenr, blocksize, 1951 ret = btrfs_inc_extent_ref(trans, fs_info, new_bytenr,
1952 0, dest->root_key.objectid, level - 1, 1952 blocksize, 0, dest->root_key.objectid,
1953 0); 1953 level - 1, 0);
1954 BUG_ON(ret); 1954 BUG_ON(ret);
1955 1955
1956 ret = btrfs_free_extent(trans, src, new_bytenr, blocksize, 1956 ret = btrfs_free_extent(trans, fs_info, new_bytenr, blocksize,
1957 path->nodes[level]->start, 1957 path->nodes[level]->start,
1958 src->root_key.objectid, level - 1, 0); 1958 src->root_key.objectid, level - 1, 0);
1959 BUG_ON(ret); 1959 BUG_ON(ret);
1960 1960
1961 ret = btrfs_free_extent(trans, dest, old_bytenr, blocksize, 1961 ret = btrfs_free_extent(trans, fs_info, old_bytenr, blocksize,
1962 0, dest->root_key.objectid, level - 1, 1962 0, dest->root_key.objectid, level - 1,
1963 0); 1963 0);
1964 BUG_ON(ret); 1964 BUG_ON(ret);
@@ -2017,6 +2017,7 @@ static noinline_for_stack
2017int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path, 2017int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
2018 int *level) 2018 int *level)
2019{ 2019{
2020 struct btrfs_fs_info *fs_info = root->fs_info;
2020 struct extent_buffer *eb = NULL; 2021 struct extent_buffer *eb = NULL;
2021 int i; 2022 int i;
2022 u64 bytenr; 2023 u64 bytenr;
@@ -2047,7 +2048,7 @@ int walk_down_reloc_tree(struct btrfs_root *root, struct btrfs_path *path,
2047 } 2048 }
2048 2049
2049 bytenr = btrfs_node_blockptr(eb, path->slots[i]); 2050 bytenr = btrfs_node_blockptr(eb, path->slots[i]);
2050 eb = read_tree_block(root, bytenr, ptr_gen); 2051 eb = read_tree_block(fs_info, bytenr, ptr_gen);
2051 if (IS_ERR(eb)) { 2052 if (IS_ERR(eb)) {
2052 return PTR_ERR(eb); 2053 return PTR_ERR(eb);
2053 } else if (!extent_buffer_uptodate(eb)) { 2054 } else if (!extent_buffer_uptodate(eb)) {
@@ -2272,7 +2273,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2272 btrfs_end_transaction_throttle(trans, root); 2273 btrfs_end_transaction_throttle(trans, root);
2273 trans = NULL; 2274 trans = NULL;
2274 2275
2275 btrfs_btree_balance_dirty(root); 2276 btrfs_btree_balance_dirty(fs_info);
2276 2277
2277 if (replaced && rc->stage == UPDATE_DATA_PTRS) 2278 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2278 invalidate_extent_cache(root, &key, &next_key); 2279 invalidate_extent_cache(root, &key, &next_key);
@@ -2302,7 +2303,7 @@ out:
2302 if (trans) 2303 if (trans)
2303 btrfs_end_transaction_throttle(trans, root); 2304 btrfs_end_transaction_throttle(trans, root);
2304 2305
2305 btrfs_btree_balance_dirty(root); 2306 btrfs_btree_balance_dirty(fs_info);
2306 2307
2307 if (replaced && rc->stage == UPDATE_DATA_PTRS) 2308 if (replaced && rc->stage == UPDATE_DATA_PTRS)
2308 invalidate_extent_cache(root, &key, &next_key); 2309 invalidate_extent_cache(root, &key, &next_key);
@@ -2338,16 +2339,16 @@ again:
2338 trans = btrfs_join_transaction(rc->extent_root); 2339 trans = btrfs_join_transaction(rc->extent_root);
2339 if (IS_ERR(trans)) { 2340 if (IS_ERR(trans)) {
2340 if (!err) 2341 if (!err)
2341 btrfs_block_rsv_release(rc->extent_root, 2342 btrfs_block_rsv_release(fs_info, rc->block_rsv,
2342 rc->block_rsv, num_bytes); 2343 num_bytes);
2343 return PTR_ERR(trans); 2344 return PTR_ERR(trans);
2344 } 2345 }
2345 2346
2346 if (!err) { 2347 if (!err) {
2347 if (num_bytes != rc->merging_rsv_size) { 2348 if (num_bytes != rc->merging_rsv_size) {
2348 btrfs_end_transaction(trans, rc->extent_root); 2349 btrfs_end_transaction(trans, rc->extent_root);
2349 btrfs_block_rsv_release(rc->extent_root, 2350 btrfs_block_rsv_release(fs_info, rc->block_rsv,
2350 rc->block_rsv, num_bytes); 2351 num_bytes);
2351 goto again; 2352 goto again;
2352 } 2353 }
2353 } 2354 }
@@ -2698,6 +2699,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
2698 struct btrfs_key *key, 2699 struct btrfs_key *key,
2699 struct btrfs_path *path, int lowest) 2700 struct btrfs_path *path, int lowest)
2700{ 2701{
2702 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2701 struct backref_node *upper; 2703 struct backref_node *upper;
2702 struct backref_edge *edge; 2704 struct backref_edge *edge;
2703 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; 2705 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
@@ -2780,7 +2782,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
2780 2782
2781 blocksize = root->fs_info->nodesize; 2783 blocksize = root->fs_info->nodesize;
2782 generation = btrfs_node_ptr_generation(upper->eb, slot); 2784 generation = btrfs_node_ptr_generation(upper->eb, slot);
2783 eb = read_tree_block(root, bytenr, generation); 2785 eb = read_tree_block(fs_info, bytenr, generation);
2784 if (IS_ERR(eb)) { 2786 if (IS_ERR(eb)) {
2785 err = PTR_ERR(eb); 2787 err = PTR_ERR(eb);
2786 goto next; 2788 goto next;
@@ -2809,7 +2811,7 @@ static int do_relocation(struct btrfs_trans_handle *trans,
2809 trans->transid); 2811 trans->transid);
2810 btrfs_mark_buffer_dirty(upper->eb); 2812 btrfs_mark_buffer_dirty(upper->eb);
2811 2813
2812 ret = btrfs_inc_extent_ref(trans, root, 2814 ret = btrfs_inc_extent_ref(trans, root->fs_info,
2813 node->eb->start, blocksize, 2815 node->eb->start, blocksize,
2814 upper->eb->start, 2816 upper->eb->start,
2815 btrfs_header_owner(upper->eb), 2817 btrfs_header_owner(upper->eb),
@@ -2939,14 +2941,13 @@ static int tree_block_processed(u64 bytenr, struct reloc_control *rc)
2939 return 0; 2941 return 0;
2940} 2942}
2941 2943
2942static int get_tree_block_key(struct reloc_control *rc, 2944static int get_tree_block_key(struct btrfs_fs_info *fs_info,
2943 struct tree_block *block) 2945 struct tree_block *block)
2944{ 2946{
2945 struct extent_buffer *eb; 2947 struct extent_buffer *eb;
2946 2948
2947 BUG_ON(block->key_ready); 2949 BUG_ON(block->key_ready);
2948 eb = read_tree_block(rc->extent_root, block->bytenr, 2950 eb = read_tree_block(fs_info, block->bytenr, block->key.offset);
2949 block->key.offset);
2950 if (IS_ERR(eb)) { 2951 if (IS_ERR(eb)) {
2951 return PTR_ERR(eb); 2952 return PTR_ERR(eb);
2952 } else if (!extent_buffer_uptodate(eb)) { 2953 } else if (!extent_buffer_uptodate(eb)) {
@@ -3025,6 +3026,7 @@ static noinline_for_stack
3025int relocate_tree_blocks(struct btrfs_trans_handle *trans, 3026int relocate_tree_blocks(struct btrfs_trans_handle *trans,
3026 struct reloc_control *rc, struct rb_root *blocks) 3027 struct reloc_control *rc, struct rb_root *blocks)
3027{ 3028{
3029 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3028 struct backref_node *node; 3030 struct backref_node *node;
3029 struct btrfs_path *path; 3031 struct btrfs_path *path;
3030 struct tree_block *block; 3032 struct tree_block *block;
@@ -3042,7 +3044,7 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans,
3042 while (rb_node) { 3044 while (rb_node) {
3043 block = rb_entry(rb_node, struct tree_block, rb_node); 3045 block = rb_entry(rb_node, struct tree_block, rb_node);
3044 if (!block->key_ready) 3046 if (!block->key_ready)
3045 readahead_tree_block(rc->extent_root, block->bytenr); 3047 readahead_tree_block(fs_info, block->bytenr);
3046 rb_node = rb_next(rb_node); 3048 rb_node = rb_next(rb_node);
3047 } 3049 }
3048 3050
@@ -3050,7 +3052,7 @@ int relocate_tree_blocks(struct btrfs_trans_handle *trans,
3050 while (rb_node) { 3052 while (rb_node) {
3051 block = rb_entry(rb_node, struct tree_block, rb_node); 3053 block = rb_entry(rb_node, struct tree_block, rb_node);
3052 if (!block->key_ready) { 3054 if (!block->key_ready) {
3053 err = get_tree_block_key(rc, block); 3055 err = get_tree_block_key(fs_info, block);
3054 if (err) 3056 if (err)
3055 goto out_free_path; 3057 goto out_free_path;
3056 } 3058 }
@@ -3178,6 +3180,7 @@ int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3178static int relocate_file_extent_cluster(struct inode *inode, 3180static int relocate_file_extent_cluster(struct inode *inode,
3179 struct file_extent_cluster *cluster) 3181 struct file_extent_cluster *cluster)
3180{ 3182{
3183 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3181 u64 page_start; 3184 u64 page_start;
3182 u64 page_end; 3185 u64 page_end;
3183 u64 offset = BTRFS_I(inode)->index_cnt; 3186 u64 offset = BTRFS_I(inode)->index_cnt;
@@ -3273,7 +3276,7 @@ static int relocate_file_extent_cluster(struct inode *inode,
3273 3276
3274 index++; 3277 index++;
3275 balance_dirty_pages_ratelimited(inode->i_mapping); 3278 balance_dirty_pages_ratelimited(inode->i_mapping);
3276 btrfs_throttle(BTRFS_I(inode)->root); 3279 btrfs_throttle(fs_info);
3277 } 3280 }
3278 WARN_ON(nr != cluster->nr); 3281 WARN_ON(nr != cluster->nr);
3279out: 3282out:
@@ -3502,7 +3505,7 @@ static int block_use_full_backref(struct reloc_control *rc,
3502 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV) 3505 btrfs_header_backref_rev(eb) < BTRFS_MIXED_BACKREF_REV)
3503 return 1; 3506 return 1;
3504 3507
3505 ret = btrfs_lookup_extent_info(NULL, rc->extent_root, 3508 ret = btrfs_lookup_extent_info(NULL, rc->extent_root->fs_info,
3506 eb->start, btrfs_header_level(eb), 1, 3509 eb->start, btrfs_header_level(eb), 1,
3507 NULL, &flags); 3510 NULL, &flags);
3508 BUG_ON(ret); 3511 BUG_ON(ret);
@@ -3539,7 +3542,7 @@ static int delete_block_group_cache(struct btrfs_fs_info *fs_info,
3539 } 3542 }
3540 3543
3541truncate: 3544truncate:
3542 ret = btrfs_check_trunc_cache_free_space(root, 3545 ret = btrfs_check_trunc_cache_free_space(fs_info,
3543 &fs_info->global_block_rsv); 3546 &fs_info->global_block_rsv);
3544 if (ret) 3547 if (ret)
3545 goto out; 3548 goto out;
@@ -3553,7 +3556,7 @@ truncate:
3553 ret = btrfs_truncate_free_space_cache(root, trans, block_group, inode); 3556 ret = btrfs_truncate_free_space_cache(root, trans, block_group, inode);
3554 3557
3555 btrfs_end_transaction(trans, root); 3558 btrfs_end_transaction(trans, root);
3556 btrfs_btree_balance_dirty(root); 3559 btrfs_btree_balance_dirty(fs_info);
3557out: 3560out:
3558 iput(inode); 3561 iput(inode);
3559 return ret; 3562 return ret;
@@ -3951,7 +3954,7 @@ int prepare_to_relocate(struct reloc_control *rc)
3951 struct btrfs_trans_handle *trans; 3954 struct btrfs_trans_handle *trans;
3952 int ret; 3955 int ret;
3953 3956
3954 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root, 3957 rc->block_rsv = btrfs_alloc_block_rsv(rc->extent_root->fs_info,
3955 BTRFS_BLOCK_RSV_TEMP); 3958 BTRFS_BLOCK_RSV_TEMP);
3956 if (!rc->block_rsv) 3959 if (!rc->block_rsv)
3957 return -ENOMEM; 3960 return -ENOMEM;
@@ -3989,6 +3992,7 @@ int prepare_to_relocate(struct reloc_control *rc)
3989 3992
3990static noinline_for_stack int relocate_block_group(struct reloc_control *rc) 3993static noinline_for_stack int relocate_block_group(struct reloc_control *rc)
3991{ 3994{
3995 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3992 struct rb_root blocks = RB_ROOT; 3996 struct rb_root blocks = RB_ROOT;
3993 struct btrfs_key key; 3997 struct btrfs_key key;
3994 struct btrfs_trans_handle *trans = NULL; 3998 struct btrfs_trans_handle *trans = NULL;
@@ -4118,7 +4122,7 @@ restart:
4118 } 4122 }
4119 4123
4120 btrfs_end_transaction_throttle(trans, rc->extent_root); 4124 btrfs_end_transaction_throttle(trans, rc->extent_root);
4121 btrfs_btree_balance_dirty(rc->extent_root); 4125 btrfs_btree_balance_dirty(fs_info);
4122 trans = NULL; 4126 trans = NULL;
4123 4127
4124 if (rc->stage == MOVE_DATA_EXTENTS && 4128 if (rc->stage == MOVE_DATA_EXTENTS &&
@@ -4133,7 +4137,7 @@ restart:
4133 } 4137 }
4134 } 4138 }
4135 if (trans && progress && err == -ENOSPC) { 4139 if (trans && progress && err == -ENOSPC) {
4136 ret = btrfs_force_chunk_alloc(trans, rc->extent_root, 4140 ret = btrfs_force_chunk_alloc(trans, fs_info,
4137 rc->block_group->flags); 4141 rc->block_group->flags);
4138 if (ret == 1) { 4142 if (ret == 1) {
4139 err = 0; 4143 err = 0;
@@ -4147,7 +4151,7 @@ restart:
4147 4151
4148 if (trans) { 4152 if (trans) {
4149 btrfs_end_transaction_throttle(trans, rc->extent_root); 4153 btrfs_end_transaction_throttle(trans, rc->extent_root);
4150 btrfs_btree_balance_dirty(rc->extent_root); 4154 btrfs_btree_balance_dirty(fs_info);
4151 } 4155 }
4152 4156
4153 if (!err) { 4157 if (!err) {
@@ -4161,7 +4165,7 @@ restart:
4161 set_reloc_control(rc); 4165 set_reloc_control(rc);
4162 4166
4163 backref_cache_cleanup(&rc->backref_cache); 4167 backref_cache_cleanup(&rc->backref_cache);
4164 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1); 4168 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1);
4165 4169
4166 err = prepare_to_merge(rc, err); 4170 err = prepare_to_merge(rc, err);
4167 4171
@@ -4169,7 +4173,7 @@ restart:
4169 4173
4170 rc->merge_reloc_tree = 0; 4174 rc->merge_reloc_tree = 0;
4171 unset_reloc_control(rc); 4175 unset_reloc_control(rc);
4172 btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1); 4176 btrfs_block_rsv_release(fs_info, rc->block_rsv, (u64)-1);
4173 4177
4174 /* get rid of pinned extents */ 4178 /* get rid of pinned extents */
4175 trans = btrfs_join_transaction(rc->extent_root); 4179 trans = btrfs_join_transaction(rc->extent_root);
@@ -4179,7 +4183,7 @@ restart:
4179 } 4183 }
4180 btrfs_commit_transaction(trans, rc->extent_root); 4184 btrfs_commit_transaction(trans, rc->extent_root);
4181out_free: 4185out_free:
4182 btrfs_free_block_rsv(rc->extent_root, rc->block_rsv); 4186 btrfs_free_block_rsv(fs_info, rc->block_rsv);
4183 btrfs_free_path(path); 4187 btrfs_free_path(path);
4184 return err; 4188 return err;
4185} 4189}
@@ -4254,7 +4258,7 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4254 err = btrfs_orphan_add(trans, inode); 4258 err = btrfs_orphan_add(trans, inode);
4255out: 4259out:
4256 btrfs_end_transaction(trans, root); 4260 btrfs_end_transaction(trans, root);
4257 btrfs_btree_balance_dirty(root); 4261 btrfs_btree_balance_dirty(fs_info);
4258 if (err) { 4262 if (err) {
4259 if (inode) 4263 if (inode)
4260 iput(inode); 4264 iput(inode);
@@ -4414,7 +4418,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
4414 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0); 4418 WARN_ON(btrfs_block_group_used(&rc->block_group->item) > 0);
4415out: 4419out:
4416 if (err && rw) 4420 if (err && rw)
4417 btrfs_dec_block_group_ro(extent_root, rc->block_group); 4421 btrfs_dec_block_group_ro(rc->block_group);
4418 iput(rc->data_inode); 4422 iput(rc->data_inode);
4419 btrfs_put_block_group(rc->block_group); 4423 btrfs_put_block_group(rc->block_group);
4420 kfree(rc); 4424 kfree(rc);
diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index f7f6cb7d9a62..87728ff39622 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -151,7 +151,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
151 } 151 }
152 152
153 if (ret != 0) { 153 if (ret != 0) {
154 btrfs_print_leaf(root, path->nodes[0]); 154 btrfs_print_leaf(fs_info, path->nodes[0]);
155 btrfs_crit(fs_info, "unable to update root key %llu %u %llu", 155 btrfs_crit(fs_info, "unable to update root key %llu %u %llu",
156 key->objectid, key->type, key->offset); 156 key->objectid, key->type, key->offset);
157 BUG_ON(1); 157 BUG_ON(1);
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 8aef00b0ff84..09fb2fb0848e 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -1450,7 +1450,7 @@ static int scrub_submit_raid56_bio_wait(struct btrfs_fs_info *fs_info,
1450 bio->bi_private = &done; 1450 bio->bi_private = &done;
1451 bio->bi_end_io = scrub_bio_wait_endio; 1451 bio->bi_end_io = scrub_bio_wait_endio;
1452 1452
1453 ret = raid56_parity_recover(fs_info->fs_root, bio, page->recover->bbio, 1453 ret = raid56_parity_recover(fs_info, bio, page->recover->bbio,
1454 page->recover->map_length, 1454 page->recover->map_length,
1455 page->mirror_num, 0); 1455 page->mirror_num, 0);
1456 if (ret) 1456 if (ret)
@@ -2181,7 +2181,6 @@ static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2181{ 2181{
2182 struct scrub_ctx *sctx = sblock->sctx; 2182 struct scrub_ctx *sctx = sblock->sctx;
2183 struct btrfs_fs_info *fs_info = sctx->fs_info; 2183 struct btrfs_fs_info *fs_info = sctx->fs_info;
2184 struct btrfs_root *dev_root = fs_info->dev_root;
2185 u64 length = sblock->page_count * PAGE_SIZE; 2184 u64 length = sblock->page_count * PAGE_SIZE;
2186 u64 logical = sblock->pagev[0]->logical; 2185 u64 logical = sblock->pagev[0]->logical;
2187 struct btrfs_bio *bbio = NULL; 2186 struct btrfs_bio *bbio = NULL;
@@ -2214,7 +2213,7 @@ static void scrub_missing_raid56_pages(struct scrub_block *sblock)
2214 bio->bi_private = sblock; 2213 bio->bi_private = sblock;
2215 bio->bi_end_io = scrub_missing_raid56_end_io; 2214 bio->bi_end_io = scrub_missing_raid56_end_io;
2216 2215
2217 rbio = raid56_alloc_missing_rbio(dev_root, bio, bbio, length); 2216 rbio = raid56_alloc_missing_rbio(fs_info, bio, bbio, length);
2218 if (!rbio) 2217 if (!rbio)
2219 goto rbio_out; 2218 goto rbio_out;
2220 2219
@@ -2766,7 +2765,6 @@ static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2766{ 2765{
2767 struct scrub_ctx *sctx = sparity->sctx; 2766 struct scrub_ctx *sctx = sparity->sctx;
2768 struct btrfs_fs_info *fs_info = sctx->fs_info; 2767 struct btrfs_fs_info *fs_info = sctx->fs_info;
2769 struct btrfs_root *dev_root = fs_info->dev_root;
2770 struct bio *bio; 2768 struct bio *bio;
2771 struct btrfs_raid_bio *rbio; 2769 struct btrfs_raid_bio *rbio;
2772 struct scrub_page *spage; 2770 struct scrub_page *spage;
@@ -2792,7 +2790,7 @@ static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2792 bio->bi_private = sparity; 2790 bio->bi_private = sparity;
2793 bio->bi_end_io = scrub_parity_bio_endio; 2791 bio->bi_end_io = scrub_parity_bio_endio;
2794 2792
2795 rbio = raid56_parity_alloc_scrub_rbio(dev_root, bio, bbio, 2793 rbio = raid56_parity_alloc_scrub_rbio(fs_info, bio, bbio,
2796 length, sparity->scrub_dev, 2794 length, sparity->scrub_dev,
2797 sparity->dbitmap, 2795 sparity->dbitmap,
2798 sparity->nsectors); 2796 sparity->nsectors);
@@ -3694,7 +3692,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
3694 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1); 3692 btrfs_dev_replace_unlock(&fs_info->dev_replace, 1);
3695 3693
3696 if (ro_set) 3694 if (ro_set)
3697 btrfs_dec_block_group_ro(root, cache); 3695 btrfs_dec_block_group_ro(cache);
3698 3696
3699 /* 3697 /*
3700 * We might have prevented the cleaner kthread from deleting 3698 * We might have prevented the cleaner kthread from deleting
@@ -3980,10 +3978,8 @@ int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
3980 return ret; 3978 return ret;
3981} 3979}
3982 3980
3983void btrfs_scrub_pause(struct btrfs_root *root) 3981void btrfs_scrub_pause(struct btrfs_fs_info *fs_info)
3984{ 3982{
3985 struct btrfs_fs_info *fs_info = root->fs_info;
3986
3987 mutex_lock(&fs_info->scrub_lock); 3983 mutex_lock(&fs_info->scrub_lock);
3988 atomic_inc(&fs_info->scrub_pause_req); 3984 atomic_inc(&fs_info->scrub_pause_req);
3989 while (atomic_read(&fs_info->scrubs_paused) != 3985 while (atomic_read(&fs_info->scrubs_paused) !=
@@ -3997,10 +3993,8 @@ void btrfs_scrub_pause(struct btrfs_root *root)
3997 mutex_unlock(&fs_info->scrub_lock); 3993 mutex_unlock(&fs_info->scrub_lock);
3998} 3994}
3999 3995
4000void btrfs_scrub_continue(struct btrfs_root *root) 3996void btrfs_scrub_continue(struct btrfs_fs_info *fs_info)
4001{ 3997{
4002 struct btrfs_fs_info *fs_info = root->fs_info;
4003
4004 atomic_dec(&fs_info->scrub_pause_req); 3998 atomic_dec(&fs_info->scrub_pause_req);
4005 wake_up(&fs_info->scrub_pause_wait); 3999 wake_up(&fs_info->scrub_pause_wait);
4006} 4000}
@@ -4049,10 +4043,9 @@ int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4049 return 0; 4043 return 0;
4050} 4044}
4051 4045
4052int btrfs_scrub_progress(struct btrfs_root *root, u64 devid, 4046int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
4053 struct btrfs_scrub_progress *progress) 4047 struct btrfs_scrub_progress *progress)
4054{ 4048{
4055 struct btrfs_fs_info *fs_info = root->fs_info;
4056 struct btrfs_device *dev; 4049 struct btrfs_device *dev;
4057 struct scrub_ctx *sctx = NULL; 4050 struct scrub_ctx *sctx = NULL;
4058 4051
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index 399b36b2a182..1df3b8798323 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -3435,6 +3435,7 @@ static int wait_for_dest_dir_move(struct send_ctx *sctx,
3435 struct recorded_ref *parent_ref, 3435 struct recorded_ref *parent_ref,
3436 const bool is_orphan) 3436 const bool is_orphan)
3437{ 3437{
3438 struct btrfs_fs_info *fs_info = sctx->parent_root->fs_info;
3438 struct btrfs_path *path; 3439 struct btrfs_path *path;
3439 struct btrfs_key key; 3440 struct btrfs_key key;
3440 struct btrfs_key di_key; 3441 struct btrfs_key di_key;
@@ -3463,8 +3464,8 @@ static int wait_for_dest_dir_move(struct send_ctx *sctx,
3463 goto out; 3464 goto out;
3464 } 3465 }
3465 3466
3466 di = btrfs_match_dir_item_name(sctx->parent_root, path, 3467 di = btrfs_match_dir_item_name(fs_info, path, parent_ref->name,
3467 parent_ref->name, parent_ref->name_len); 3468 parent_ref->name_len);
3468 if (!di) { 3469 if (!di) {
3469 ret = 0; 3470 ret = 0;
3470 goto out; 3471 goto out;
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index fa2f33fe8c6c..3021b0f32e53 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -396,10 +396,9 @@ static const match_table_t tokens = {
396 * reading in a new superblock is parsed here. 396 * reading in a new superblock is parsed here.
397 * XXX JDM: This needs to be cleaned up for remount. 397 * XXX JDM: This needs to be cleaned up for remount.
398 */ 398 */
399int btrfs_parse_options(struct btrfs_root *root, char *options, 399int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
400 unsigned long new_flags) 400 unsigned long new_flags)
401{ 401{
402 struct btrfs_fs_info *info = root->fs_info;
403 substring_t args[MAX_OPT_ARGS]; 402 substring_t args[MAX_OPT_ARGS];
404 char *p, *num, *orig = NULL; 403 char *p, *num, *orig = NULL;
405 u64 cache_gen; 404 u64 cache_gen;
@@ -1733,7 +1732,7 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1733 } 1732 }
1734 } 1733 }
1735 1734
1736 ret = btrfs_parse_options(root, data, *flags); 1735 ret = btrfs_parse_options(fs_info, data, *flags);
1737 if (ret) { 1736 if (ret) {
1738 ret = -EINVAL; 1737 ret = -EINVAL;
1739 goto restore; 1738 goto restore;
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 56eeecf4ecde..8667a991888f 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -184,10 +184,10 @@ static inline int extwriter_counter_read(struct btrfs_transaction *trans)
184/* 184/*
185 * either allocate a new transaction or hop into the existing one 185 * either allocate a new transaction or hop into the existing one
186 */ 186 */
187static noinline int join_transaction(struct btrfs_root *root, unsigned int type) 187static noinline int join_transaction(struct btrfs_fs_info *fs_info,
188 unsigned int type)
188{ 189{
189 struct btrfs_transaction *cur_trans; 190 struct btrfs_transaction *cur_trans;
190 struct btrfs_fs_info *fs_info = root->fs_info;
191 191
192 spin_lock(&fs_info->trans_lock); 192 spin_lock(&fs_info->trans_lock);
193loop: 193loop:
@@ -425,9 +425,8 @@ static inline int is_transaction_blocked(struct btrfs_transaction *trans)
425 * when this is done, it is safe to start a new transaction, but the current 425 * when this is done, it is safe to start a new transaction, but the current
426 * transaction might not be fully on disk. 426 * transaction might not be fully on disk.
427 */ 427 */
428static void wait_current_trans(struct btrfs_root *root) 428static void wait_current_trans(struct btrfs_fs_info *fs_info)
429{ 429{
430 struct btrfs_fs_info *fs_info = root->fs_info;
431 struct btrfs_transaction *cur_trans; 430 struct btrfs_transaction *cur_trans;
432 431
433 spin_lock(&fs_info->trans_lock); 432 spin_lock(&fs_info->trans_lock);
@@ -445,10 +444,8 @@ static void wait_current_trans(struct btrfs_root *root)
445 } 444 }
446} 445}
447 446
448static int may_wait_transaction(struct btrfs_root *root, int type) 447static int may_wait_transaction(struct btrfs_fs_info *fs_info, int type)
449{ 448{
450 struct btrfs_fs_info *fs_info = root->fs_info;
451
452 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) 449 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
453 return 0; 450 return 0;
454 451
@@ -548,13 +545,13 @@ again:
548 if (type & __TRANS_FREEZABLE) 545 if (type & __TRANS_FREEZABLE)
549 sb_start_intwrite(fs_info->sb); 546 sb_start_intwrite(fs_info->sb);
550 547
551 if (may_wait_transaction(root, type)) 548 if (may_wait_transaction(fs_info, type))
552 wait_current_trans(root); 549 wait_current_trans(fs_info);
553 550
554 do { 551 do {
555 ret = join_transaction(root, type); 552 ret = join_transaction(fs_info, type);
556 if (ret == -EBUSY) { 553 if (ret == -EBUSY) {
557 wait_current_trans(root); 554 wait_current_trans(fs_info);
558 if (unlikely(type == TRANS_ATTACH)) 555 if (unlikely(type == TRANS_ATTACH))
559 ret = -ENOENT; 556 ret = -ENOENT;
560 } 557 }
@@ -578,7 +575,7 @@ again:
578 575
579 smp_mb(); 576 smp_mb();
580 if (cur_trans->state >= TRANS_STATE_BLOCKED && 577 if (cur_trans->state >= TRANS_STATE_BLOCKED &&
581 may_wait_transaction(root, type)) { 578 may_wait_transaction(fs_info, type)) {
582 current->journal_info = h; 579 current->journal_info = h;
583 btrfs_commit_transaction(h, root); 580 btrfs_commit_transaction(h, root);
584 goto again; 581 goto again;
@@ -605,7 +602,7 @@ join_fail:
605 kmem_cache_free(btrfs_trans_handle_cachep, h); 602 kmem_cache_free(btrfs_trans_handle_cachep, h);
606alloc_fail: 603alloc_fail:
607 if (num_bytes) 604 if (num_bytes)
608 btrfs_block_rsv_release(root, &fs_info->trans_block_rsv, 605 btrfs_block_rsv_release(fs_info, &fs_info->trans_block_rsv,
609 num_bytes); 606 num_bytes);
610reserve_fail: 607reserve_fail:
611 btrfs_qgroup_free_meta(root, qgroup_reserved); 608 btrfs_qgroup_free_meta(root, qgroup_reserved);
@@ -712,21 +709,19 @@ btrfs_attach_transaction_barrier(struct btrfs_root *root)
712 trans = start_transaction(root, 0, TRANS_ATTACH, 709 trans = start_transaction(root, 0, TRANS_ATTACH,
713 BTRFS_RESERVE_NO_FLUSH); 710 BTRFS_RESERVE_NO_FLUSH);
714 if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT) 711 if (IS_ERR(trans) && PTR_ERR(trans) == -ENOENT)
715 btrfs_wait_for_commit(root, 0); 712 btrfs_wait_for_commit(root->fs_info, 0);
716 713
717 return trans; 714 return trans;
718} 715}
719 716
720/* wait for a transaction commit to be fully complete */ 717/* wait for a transaction commit to be fully complete */
721static noinline void wait_for_commit(struct btrfs_root *root, 718static noinline void wait_for_commit(struct btrfs_transaction *commit)
722 struct btrfs_transaction *commit)
723{ 719{
724 wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED); 720 wait_event(commit->commit_wait, commit->state == TRANS_STATE_COMPLETED);
725} 721}
726 722
727int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 723int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid)
728{ 724{
729 struct btrfs_fs_info *fs_info = root->fs_info;
730 struct btrfs_transaction *cur_trans = NULL, *t; 725 struct btrfs_transaction *cur_trans = NULL, *t;
731 int ret = 0; 726 int ret = 0;
732 727
@@ -777,35 +772,33 @@ int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
777 goto out; /* nothing committing|committed */ 772 goto out; /* nothing committing|committed */
778 } 773 }
779 774
780 wait_for_commit(root, cur_trans); 775 wait_for_commit(cur_trans);
781 btrfs_put_transaction(cur_trans); 776 btrfs_put_transaction(cur_trans);
782out: 777out:
783 return ret; 778 return ret;
784} 779}
785 780
786void btrfs_throttle(struct btrfs_root *root) 781void btrfs_throttle(struct btrfs_fs_info *fs_info)
787{ 782{
788 struct btrfs_fs_info *fs_info = root->fs_info;
789
790 if (!atomic_read(&fs_info->open_ioctl_trans)) 783 if (!atomic_read(&fs_info->open_ioctl_trans))
791 wait_current_trans(root); 784 wait_current_trans(fs_info);
792} 785}
793 786
794static int should_end_transaction(struct btrfs_trans_handle *trans, 787static int should_end_transaction(struct btrfs_trans_handle *trans)
795 struct btrfs_root *root)
796{ 788{
797 struct btrfs_fs_info *fs_info = root->fs_info; 789 struct btrfs_fs_info *fs_info = trans->fs_info;
798 790
799 if (fs_info->global_block_rsv.space_info->full && 791 if (fs_info->global_block_rsv.space_info->full &&
800 btrfs_check_space_for_delayed_refs(trans, root)) 792 btrfs_check_space_for_delayed_refs(trans, fs_info))
801 return 1; 793 return 1;
802 794
803 return !!btrfs_block_rsv_check(root, &fs_info->global_block_rsv, 5); 795 return !!btrfs_block_rsv_check(&fs_info->global_block_rsv, 5);
804} 796}
805 797
806int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 798int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
807 struct btrfs_root *root) 799 struct btrfs_root *root)
808{ 800{
801 struct btrfs_fs_info *fs_info = root->fs_info;
809 struct btrfs_transaction *cur_trans = trans->transaction; 802 struct btrfs_transaction *cur_trans = trans->transaction;
810 int updates; 803 int updates;
811 int err; 804 int err;
@@ -818,12 +811,12 @@ int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
818 updates = trans->delayed_ref_updates; 811 updates = trans->delayed_ref_updates;
819 trans->delayed_ref_updates = 0; 812 trans->delayed_ref_updates = 0;
820 if (updates) { 813 if (updates) {
821 err = btrfs_run_delayed_refs(trans, root, updates * 2); 814 err = btrfs_run_delayed_refs(trans, fs_info, updates * 2);
822 if (err) /* Error code will also eval true */ 815 if (err) /* Error code will also eval true */
823 return err; 816 return err;
824 } 817 }
825 818
826 return should_end_transaction(trans, root); 819 return should_end_transaction(trans);
827} 820}
828 821
829static int __btrfs_end_transaction(struct btrfs_trans_handle *trans, 822static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
@@ -843,16 +836,16 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
843 return 0; 836 return 0;
844 } 837 }
845 838
846 btrfs_trans_release_metadata(trans, root); 839 btrfs_trans_release_metadata(trans, info);
847 trans->block_rsv = NULL; 840 trans->block_rsv = NULL;
848 841
849 if (!list_empty(&trans->new_bgs)) 842 if (!list_empty(&trans->new_bgs))
850 btrfs_create_pending_block_groups(trans, root); 843 btrfs_create_pending_block_groups(trans, info);
851 844
852 trans->delayed_ref_updates = 0; 845 trans->delayed_ref_updates = 0;
853 if (!trans->sync) { 846 if (!trans->sync) {
854 must_run_delayed_refs = 847 must_run_delayed_refs =
855 btrfs_should_throttle_delayed_refs(trans, root); 848 btrfs_should_throttle_delayed_refs(trans, info);
856 cur = max_t(unsigned long, cur, 32); 849 cur = max_t(unsigned long, cur, 32);
857 850
858 /* 851 /*
@@ -864,16 +857,16 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
864 must_run_delayed_refs = 2; 857 must_run_delayed_refs = 2;
865 } 858 }
866 859
867 btrfs_trans_release_metadata(trans, root); 860 btrfs_trans_release_metadata(trans, info);
868 trans->block_rsv = NULL; 861 trans->block_rsv = NULL;
869 862
870 if (!list_empty(&trans->new_bgs)) 863 if (!list_empty(&trans->new_bgs))
871 btrfs_create_pending_block_groups(trans, root); 864 btrfs_create_pending_block_groups(trans, info);
872 865
873 btrfs_trans_release_chunk_metadata(trans); 866 btrfs_trans_release_chunk_metadata(trans);
874 867
875 if (lock && !atomic_read(&info->open_ioctl_trans) && 868 if (lock && !atomic_read(&info->open_ioctl_trans) &&
876 should_end_transaction(trans, root) && 869 should_end_transaction(trans) &&
877 ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 870 ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
878 spin_lock(&info->trans_lock); 871 spin_lock(&info->trans_lock);
879 if (cur_trans->state == TRANS_STATE_RUNNING) 872 if (cur_trans->state == TRANS_STATE_RUNNING)
@@ -908,7 +901,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
908 current->journal_info = NULL; 901 current->journal_info = NULL;
909 902
910 if (throttle) 903 if (throttle)
911 btrfs_run_delayed_iputs(root); 904 btrfs_run_delayed_iputs(info);
912 905
913 if (trans->aborted || 906 if (trans->aborted ||
914 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) { 907 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
@@ -919,7 +912,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
919 912
920 kmem_cache_free(btrfs_trans_handle_cachep, trans); 913 kmem_cache_free(btrfs_trans_handle_cachep, trans);
921 if (must_run_delayed_refs) { 914 if (must_run_delayed_refs) {
922 btrfs_async_run_delayed_refs(root, cur, transid, 915 btrfs_async_run_delayed_refs(info, cur, transid,
923 must_run_delayed_refs == 1); 916 must_run_delayed_refs == 1);
924 } 917 }
925 return err; 918 return err;
@@ -942,12 +935,11 @@ int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
942 * them in one of two extent_io trees. This is used to make sure all of 935 * them in one of two extent_io trees. This is used to make sure all of
943 * those extents are sent to disk but does not wait on them 936 * those extents are sent to disk but does not wait on them
944 */ 937 */
945int btrfs_write_marked_extents(struct btrfs_root *root, 938int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
946 struct extent_io_tree *dirty_pages, int mark) 939 struct extent_io_tree *dirty_pages, int mark)
947{ 940{
948 int err = 0; 941 int err = 0;
949 int werr = 0; 942 int werr = 0;
950 struct btrfs_fs_info *fs_info = root->fs_info;
951 struct address_space *mapping = fs_info->btree_inode->i_mapping; 943 struct address_space *mapping = fs_info->btree_inode->i_mapping;
952 struct extent_state *cached_state = NULL; 944 struct extent_state *cached_state = NULL;
953 u64 start = 0; 945 u64 start = 0;
@@ -1068,7 +1060,7 @@ static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
1068 struct blk_plug plug; 1060 struct blk_plug plug;
1069 1061
1070 blk_start_plug(&plug); 1062 blk_start_plug(&plug);
1071 ret = btrfs_write_marked_extents(root, dirty_pages, mark); 1063 ret = btrfs_write_marked_extents(root->fs_info, dirty_pages, mark);
1072 blk_finish_plug(&plug); 1064 blk_finish_plug(&plug);
1073 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark); 1065 ret2 = btrfs_wait_marked_extents(root, dirty_pages, mark);
1074 1066
@@ -1080,7 +1072,7 @@ static int btrfs_write_and_wait_marked_extents(struct btrfs_root *root,
1080} 1072}
1081 1073
1082static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans, 1074static int btrfs_write_and_wait_transaction(struct btrfs_trans_handle *trans,
1083 struct btrfs_root *root) 1075 struct btrfs_root *root)
1084{ 1076{
1085 int ret; 1077 int ret;
1086 1078
@@ -1140,9 +1132,8 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
1140 * to clean up the delayed refs. 1132 * to clean up the delayed refs.
1141 */ 1133 */
1142static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans, 1134static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
1143 struct btrfs_root *root) 1135 struct btrfs_fs_info *fs_info)
1144{ 1136{
1145 struct btrfs_fs_info *fs_info = root->fs_info;
1146 struct list_head *dirty_bgs = &trans->transaction->dirty_bgs; 1137 struct list_head *dirty_bgs = &trans->transaction->dirty_bgs;
1147 struct list_head *io_bgs = &trans->transaction->io_bgs; 1138 struct list_head *io_bgs = &trans->transaction->io_bgs;
1148 struct list_head *next; 1139 struct list_head *next;
@@ -1158,7 +1149,7 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
1158 if (ret) 1149 if (ret)
1159 return ret; 1150 return ret;
1160 1151
1161 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1152 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1162 if (ret) 1153 if (ret)
1163 return ret; 1154 return ret;
1164 1155
@@ -1172,16 +1163,17 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
1172 if (ret) 1163 if (ret)
1173 return ret; 1164 return ret;
1174 1165
1175 ret = btrfs_setup_space_cache(trans, root); 1166 ret = btrfs_setup_space_cache(trans, fs_info);
1176 if (ret) 1167 if (ret)
1177 return ret; 1168 return ret;
1178 1169
1179 /* run_qgroups might have added some more refs */ 1170 /* run_qgroups might have added some more refs */
1180 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1171 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1181 if (ret) 1172 if (ret)
1182 return ret; 1173 return ret;
1183again: 1174again:
1184 while (!list_empty(&fs_info->dirty_cowonly_roots)) { 1175 while (!list_empty(&fs_info->dirty_cowonly_roots)) {
1176 struct btrfs_root *root;
1185 next = fs_info->dirty_cowonly_roots.next; 1177 next = fs_info->dirty_cowonly_roots.next;
1186 list_del_init(next); 1178 list_del_init(next);
1187 root = list_entry(next, struct btrfs_root, dirty_list); 1179 root = list_entry(next, struct btrfs_root, dirty_list);
@@ -1193,16 +1185,16 @@ again:
1193 ret = update_cowonly_root(trans, root); 1185 ret = update_cowonly_root(trans, root);
1194 if (ret) 1186 if (ret)
1195 return ret; 1187 return ret;
1196 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1188 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1197 if (ret) 1189 if (ret)
1198 return ret; 1190 return ret;
1199 } 1191 }
1200 1192
1201 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) { 1193 while (!list_empty(dirty_bgs) || !list_empty(io_bgs)) {
1202 ret = btrfs_write_dirty_block_groups(trans, root); 1194 ret = btrfs_write_dirty_block_groups(trans, fs_info);
1203 if (ret) 1195 if (ret)
1204 return ret; 1196 return ret;
1205 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1197 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1206 if (ret) 1198 if (ret)
1207 return ret; 1199 return ret;
1208 } 1200 }
@@ -1309,7 +1301,7 @@ int btrfs_defrag_root(struct btrfs_root *root)
1309 ret = btrfs_defrag_leaves(trans, root); 1301 ret = btrfs_defrag_leaves(trans, root);
1310 1302
1311 btrfs_end_transaction(trans, root); 1303 btrfs_end_transaction(trans, root);
1312 btrfs_btree_balance_dirty(info->tree_root); 1304 btrfs_btree_balance_dirty(info);
1313 cond_resched(); 1305 cond_resched();
1314 1306
1315 if (btrfs_fs_closing(info) || ret != -EAGAIN) 1307 if (btrfs_fs_closing(info) || ret != -EAGAIN)
@@ -1388,7 +1380,7 @@ static int qgroup_account_snapshot(struct btrfs_trans_handle *trans,
1388 * like chunk and root tree, as they won't affect qgroup. 1380 * like chunk and root tree, as they won't affect qgroup.
1389 * And we don't write super to avoid half committed status. 1381 * And we don't write super to avoid half committed status.
1390 */ 1382 */
1391 ret = commit_cowonly_roots(trans, src); 1383 ret = commit_cowonly_roots(trans, fs_info);
1392 if (ret) 1384 if (ret)
1393 goto out; 1385 goto out;
1394 switch_commit_roots(trans->transaction, fs_info); 1386 switch_commit_roots(trans->transaction, fs_info);
@@ -1515,7 +1507,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1515 * otherwise we corrupt the FS during 1507 * otherwise we corrupt the FS during
1516 * snapshot 1508 * snapshot
1517 */ 1509 */
1518 ret = btrfs_run_delayed_items(trans, root); 1510 ret = btrfs_run_delayed_items(trans, fs_info);
1519 if (ret) { /* Transaction aborted */ 1511 if (ret) { /* Transaction aborted */
1520 btrfs_abort_transaction(trans, ret); 1512 btrfs_abort_transaction(trans, ret);
1521 goto fail; 1513 goto fail;
@@ -1611,7 +1603,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1611 goto fail; 1603 goto fail;
1612 } 1604 }
1613 1605
1614 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1606 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1615 if (ret) { 1607 if (ret) {
1616 btrfs_abort_transaction(trans, ret); 1608 btrfs_abort_transaction(trans, ret);
1617 goto fail; 1609 goto fail;
@@ -1665,7 +1657,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1665 } 1657 }
1666 } 1658 }
1667 1659
1668 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 1660 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
1669 if (ret) { 1661 if (ret) {
1670 btrfs_abort_transaction(trans, ret); 1662 btrfs_abort_transaction(trans, ret);
1671 goto fail; 1663 goto fail;
@@ -1706,9 +1698,8 @@ static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1706 return ret; 1698 return ret;
1707} 1699}
1708 1700
1709static void update_super_roots(struct btrfs_root *root) 1701static void update_super_roots(struct btrfs_fs_info *fs_info)
1710{ 1702{
1711 struct btrfs_fs_info *fs_info = root->fs_info;
1712 struct btrfs_root_item *root_item; 1703 struct btrfs_root_item *root_item;
1713 struct btrfs_super_block *super; 1704 struct btrfs_super_block *super;
1714 1705
@@ -1759,24 +1750,23 @@ int btrfs_transaction_blocked(struct btrfs_fs_info *info)
1759 * wait for the current transaction commit to start and block subsequent 1750 * wait for the current transaction commit to start and block subsequent
1760 * transaction joins 1751 * transaction joins
1761 */ 1752 */
1762static void wait_current_trans_commit_start(struct btrfs_root *root, 1753static void wait_current_trans_commit_start(struct btrfs_fs_info *fs_info,
1763 struct btrfs_transaction *trans) 1754 struct btrfs_transaction *trans)
1764{ 1755{
1765 wait_event(root->fs_info->transaction_blocked_wait, 1756 wait_event(fs_info->transaction_blocked_wait,
1766 trans->state >= TRANS_STATE_COMMIT_START || 1757 trans->state >= TRANS_STATE_COMMIT_START || trans->aborted);
1767 trans->aborted);
1768} 1758}
1769 1759
1770/* 1760/*
1771 * wait for the current transaction to start and then become unblocked. 1761 * wait for the current transaction to start and then become unblocked.
1772 * caller holds ref. 1762 * caller holds ref.
1773 */ 1763 */
1774static void wait_current_trans_commit_start_and_unblock(struct btrfs_root *root, 1764static void wait_current_trans_commit_start_and_unblock(
1775 struct btrfs_transaction *trans) 1765 struct btrfs_fs_info *fs_info,
1766 struct btrfs_transaction *trans)
1776{ 1767{
1777 wait_event(root->fs_info->transaction_wait, 1768 wait_event(fs_info->transaction_wait,
1778 trans->state >= TRANS_STATE_UNBLOCKED || 1769 trans->state >= TRANS_STATE_UNBLOCKED || trans->aborted);
1779 trans->aborted);
1780} 1770}
1781 1771
1782/* 1772/*
@@ -1845,9 +1835,9 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1845 1835
1846 /* wait for transaction to start and unblock */ 1836 /* wait for transaction to start and unblock */
1847 if (wait_for_unblock) 1837 if (wait_for_unblock)
1848 wait_current_trans_commit_start_and_unblock(root, cur_trans); 1838 wait_current_trans_commit_start_and_unblock(fs_info, cur_trans);
1849 else 1839 else
1850 wait_current_trans_commit_start(root, cur_trans); 1840 wait_current_trans_commit_start(fs_info, cur_trans);
1851 1841
1852 if (current->journal_info == trans) 1842 if (current->journal_info == trans)
1853 current->journal_info = NULL; 1843 current->journal_info = NULL;
@@ -1888,7 +1878,7 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,
1888 } 1878 }
1889 spin_unlock(&fs_info->trans_lock); 1879 spin_unlock(&fs_info->trans_lock);
1890 1880
1891 btrfs_cleanup_one_transaction(trans->transaction, root); 1881 btrfs_cleanup_one_transaction(trans->transaction, fs_info);
1892 1882
1893 spin_lock(&fs_info->trans_lock); 1883 spin_lock(&fs_info->trans_lock);
1894 if (cur_trans == fs_info->running_transaction) 1884 if (cur_trans == fs_info->running_transaction)
@@ -1947,13 +1937,13 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1947 /* make a pass through all the delayed refs we have so far 1937 /* make a pass through all the delayed refs we have so far
1948 * any runnings procs may add more while we are here 1938 * any runnings procs may add more while we are here
1949 */ 1939 */
1950 ret = btrfs_run_delayed_refs(trans, root, 0); 1940 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
1951 if (ret) { 1941 if (ret) {
1952 btrfs_end_transaction(trans, root); 1942 btrfs_end_transaction(trans, root);
1953 return ret; 1943 return ret;
1954 } 1944 }
1955 1945
1956 btrfs_trans_release_metadata(trans, root); 1946 btrfs_trans_release_metadata(trans, fs_info);
1957 trans->block_rsv = NULL; 1947 trans->block_rsv = NULL;
1958 1948
1959 cur_trans = trans->transaction; 1949 cur_trans = trans->transaction;
@@ -1966,9 +1956,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1966 smp_wmb(); 1956 smp_wmb();
1967 1957
1968 if (!list_empty(&trans->new_bgs)) 1958 if (!list_empty(&trans->new_bgs))
1969 btrfs_create_pending_block_groups(trans, root); 1959 btrfs_create_pending_block_groups(trans, fs_info);
1970 1960
1971 ret = btrfs_run_delayed_refs(trans, root, 0); 1961 ret = btrfs_run_delayed_refs(trans, fs_info, 0);
1972 if (ret) { 1962 if (ret) {
1973 btrfs_end_transaction(trans, root); 1963 btrfs_end_transaction(trans, root);
1974 return ret; 1964 return ret;
@@ -1997,7 +1987,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1997 mutex_unlock(&fs_info->ro_block_group_mutex); 1987 mutex_unlock(&fs_info->ro_block_group_mutex);
1998 1988
1999 if (run_it) 1989 if (run_it)
2000 ret = btrfs_start_dirty_block_groups(trans, root); 1990 ret = btrfs_start_dirty_block_groups(trans, fs_info);
2001 } 1991 }
2002 if (ret) { 1992 if (ret) {
2003 btrfs_end_transaction(trans, root); 1993 btrfs_end_transaction(trans, root);
@@ -2010,7 +2000,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2010 atomic_inc(&cur_trans->use_count); 2000 atomic_inc(&cur_trans->use_count);
2011 ret = btrfs_end_transaction(trans, root); 2001 ret = btrfs_end_transaction(trans, root);
2012 2002
2013 wait_for_commit(root, cur_trans); 2003 wait_for_commit(cur_trans);
2014 2004
2015 if (unlikely(cur_trans->aborted)) 2005 if (unlikely(cur_trans->aborted))
2016 ret = cur_trans->aborted; 2006 ret = cur_trans->aborted;
@@ -2030,7 +2020,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2030 atomic_inc(&prev_trans->use_count); 2020 atomic_inc(&prev_trans->use_count);
2031 spin_unlock(&fs_info->trans_lock); 2021 spin_unlock(&fs_info->trans_lock);
2032 2022
2033 wait_for_commit(root, prev_trans); 2023 wait_for_commit(prev_trans);
2034 ret = prev_trans->aborted; 2024 ret = prev_trans->aborted;
2035 2025
2036 btrfs_put_transaction(prev_trans); 2026 btrfs_put_transaction(prev_trans);
@@ -2049,7 +2039,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2049 if (ret) 2039 if (ret)
2050 goto cleanup_transaction; 2040 goto cleanup_transaction;
2051 2041
2052 ret = btrfs_run_delayed_items(trans, root); 2042 ret = btrfs_run_delayed_items(trans, fs_info);
2053 if (ret) 2043 if (ret)
2054 goto cleanup_transaction; 2044 goto cleanup_transaction;
2055 2045
@@ -2057,7 +2047,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2057 extwriter_counter_read(cur_trans) == 0); 2047 extwriter_counter_read(cur_trans) == 0);
2058 2048
2059 /* some pending stuffs might be added after the previous flush. */ 2049 /* some pending stuffs might be added after the previous flush. */
2060 ret = btrfs_run_delayed_items(trans, root); 2050 ret = btrfs_run_delayed_items(trans, fs_info);
2061 if (ret) 2051 if (ret)
2062 goto cleanup_transaction; 2052 goto cleanup_transaction;
2063 2053
@@ -2065,7 +2055,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2065 2055
2066 btrfs_wait_pending_ordered(cur_trans); 2056 btrfs_wait_pending_ordered(cur_trans);
2067 2057
2068 btrfs_scrub_pause(root); 2058 btrfs_scrub_pause(fs_info);
2069 /* 2059 /*
2070 * Ok now we need to make sure to block out any other joins while we 2060 * Ok now we need to make sure to block out any other joins while we
2071 * commit the transaction. We could have started a join before setting 2061 * commit the transaction. We could have started a join before setting
@@ -2110,13 +2100,13 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2110 * because all the tree which are snapshoted will be forced to COW 2100 * because all the tree which are snapshoted will be forced to COW
2111 * the nodes and leaves. 2101 * the nodes and leaves.
2112 */ 2102 */
2113 ret = btrfs_run_delayed_items(trans, root); 2103 ret = btrfs_run_delayed_items(trans, fs_info);
2114 if (ret) { 2104 if (ret) {
2115 mutex_unlock(&fs_info->reloc_mutex); 2105 mutex_unlock(&fs_info->reloc_mutex);
2116 goto scrub_continue; 2106 goto scrub_continue;
2117 } 2107 }
2118 2108
2119 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 2109 ret = btrfs_run_delayed_refs(trans, fs_info, (unsigned long)-1);
2120 if (ret) { 2110 if (ret) {
2121 mutex_unlock(&fs_info->reloc_mutex); 2111 mutex_unlock(&fs_info->reloc_mutex);
2122 goto scrub_continue; 2112 goto scrub_continue;
@@ -2181,7 +2171,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2181 goto scrub_continue; 2171 goto scrub_continue;
2182 } 2172 }
2183 2173
2184 ret = commit_cowonly_roots(trans, root); 2174 ret = commit_cowonly_roots(trans, fs_info);
2185 if (ret) { 2175 if (ret) {
2186 mutex_unlock(&fs_info->tree_log_mutex); 2176 mutex_unlock(&fs_info->tree_log_mutex);
2187 mutex_unlock(&fs_info->reloc_mutex); 2177 mutex_unlock(&fs_info->reloc_mutex);
@@ -2199,7 +2189,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2199 goto scrub_continue; 2189 goto scrub_continue;
2200 } 2190 }
2201 2191
2202 btrfs_prepare_extent_commit(trans, root); 2192 btrfs_prepare_extent_commit(trans, fs_info);
2203 2193
2204 cur_trans = fs_info->running_transaction; 2194 cur_trans = fs_info->running_transaction;
2205 2195
@@ -2218,7 +2208,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2218 assert_qgroups_uptodate(trans); 2208 assert_qgroups_uptodate(trans);
2219 ASSERT(list_empty(&cur_trans->dirty_bgs)); 2209 ASSERT(list_empty(&cur_trans->dirty_bgs));
2220 ASSERT(list_empty(&cur_trans->io_bgs)); 2210 ASSERT(list_empty(&cur_trans->io_bgs));
2221 update_super_roots(root); 2211 update_super_roots(fs_info);
2222 2212
2223 btrfs_set_super_log_root(fs_info->super_copy, 0); 2213 btrfs_set_super_log_root(fs_info->super_copy, 0);
2224 btrfs_set_super_log_root_level(fs_info->super_copy, 0); 2214 btrfs_set_super_log_root_level(fs_info->super_copy, 0);
@@ -2226,7 +2216,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2226 sizeof(*fs_info->super_copy)); 2216 sizeof(*fs_info->super_copy));
2227 2217
2228 btrfs_update_commit_device_size(fs_info); 2218 btrfs_update_commit_device_size(fs_info);
2229 btrfs_update_commit_device_bytes_used(root, cur_trans); 2219 btrfs_update_commit_device_bytes_used(fs_info, cur_trans);
2230 2220
2231 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags); 2221 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2232 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags); 2222 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
@@ -2249,7 +2239,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2249 goto scrub_continue; 2239 goto scrub_continue;
2250 } 2240 }
2251 2241
2252 ret = write_ctree_super(trans, root, 0); 2242 ret = write_ctree_super(trans, fs_info, 0);
2253 if (ret) { 2243 if (ret) {
2254 mutex_unlock(&fs_info->tree_log_mutex); 2244 mutex_unlock(&fs_info->tree_log_mutex);
2255 goto scrub_continue; 2245 goto scrub_continue;
@@ -2261,7 +2251,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2261 */ 2251 */
2262 mutex_unlock(&fs_info->tree_log_mutex); 2252 mutex_unlock(&fs_info->tree_log_mutex);
2263 2253
2264 btrfs_finish_extent_commit(trans, root); 2254 btrfs_finish_extent_commit(trans, fs_info);
2265 2255
2266 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 2256 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2267 btrfs_clear_space_info_full(fs_info); 2257 btrfs_clear_space_info_full(fs_info);
@@ -2286,7 +2276,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2286 2276
2287 trace_btrfs_transaction_commit(root); 2277 trace_btrfs_transaction_commit(root);
2288 2278
2289 btrfs_scrub_continue(root); 2279 btrfs_scrub_continue(fs_info);
2290 2280
2291 if (current->journal_info == trans) 2281 if (current->journal_info == trans)
2292 current->journal_info = NULL; 2282 current->journal_info = NULL;
@@ -2299,14 +2289,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2299 */ 2289 */
2300 if (current != fs_info->transaction_kthread && 2290 if (current != fs_info->transaction_kthread &&
2301 current != fs_info->cleaner_kthread && !fs_info->fs_frozen) 2291 current != fs_info->cleaner_kthread && !fs_info->fs_frozen)
2302 btrfs_run_delayed_iputs(root); 2292 btrfs_run_delayed_iputs(fs_info);
2303 2293
2304 return ret; 2294 return ret;
2305 2295
2306scrub_continue: 2296scrub_continue:
2307 btrfs_scrub_continue(root); 2297 btrfs_scrub_continue(fs_info);
2308cleanup_transaction: 2298cleanup_transaction:
2309 btrfs_trans_release_metadata(trans, root); 2299 btrfs_trans_release_metadata(trans, fs_info);
2310 btrfs_trans_release_chunk_metadata(trans); 2300 btrfs_trans_release_chunk_metadata(trans);
2311 trans->block_rsv = NULL; 2301 trans->block_rsv = NULL;
2312 btrfs_warn(fs_info, "Skipping commit of aborted transaction."); 2302 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
index 6cf0d37d4f76..b005371a62db 100644
--- a/fs/btrfs/transaction.h
+++ b/fs/btrfs/transaction.h
@@ -202,7 +202,7 @@ struct btrfs_trans_handle *btrfs_attach_transaction(struct btrfs_root *root);
202struct btrfs_trans_handle *btrfs_attach_transaction_barrier( 202struct btrfs_trans_handle *btrfs_attach_transaction_barrier(
203 struct btrfs_root *root); 203 struct btrfs_root *root);
204struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root); 204struct btrfs_trans_handle *btrfs_start_ioctl_transaction(struct btrfs_root *root);
205int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid); 205int btrfs_wait_for_commit(struct btrfs_fs_info *fs_info, u64 transid);
206 206
207void btrfs_add_dead_root(struct btrfs_root *root); 207void btrfs_add_dead_root(struct btrfs_root *root);
208int btrfs_defrag_root(struct btrfs_root *root); 208int btrfs_defrag_root(struct btrfs_root *root);
@@ -216,10 +216,10 @@ int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans,
216 struct btrfs_root *root); 216 struct btrfs_root *root);
217int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 217int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root); 218 struct btrfs_root *root);
219void btrfs_throttle(struct btrfs_root *root); 219void btrfs_throttle(struct btrfs_fs_info *fs_info);
220int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 220int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
221 struct btrfs_root *root); 221 struct btrfs_root *root);
222int btrfs_write_marked_extents(struct btrfs_root *root, 222int btrfs_write_marked_extents(struct btrfs_fs_info *fs_info,
223 struct extent_io_tree *dirty_pages, int mark); 223 struct extent_io_tree *dirty_pages, int mark);
224int btrfs_wait_marked_extents(struct btrfs_root *root, 224int btrfs_wait_marked_extents(struct btrfs_root *root,
225 struct extent_io_tree *dirty_pages, int mark); 225 struct extent_io_tree *dirty_pages, int mark);
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index dcb225e6a1c7..0cde002bbd0a 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -307,12 +307,12 @@ static int process_one_buffer(struct btrfs_root *log,
307 } 307 }
308 308
309 if (wc->pin) 309 if (wc->pin)
310 ret = btrfs_pin_extent_for_log_replay(fs_info->extent_root, 310 ret = btrfs_pin_extent_for_log_replay(fs_info, eb->start,
311 eb->start, eb->len); 311 eb->len);
312 312
313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { 313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
314 if (wc->pin && btrfs_header_level(eb) == 0) 314 if (wc->pin && btrfs_header_level(eb) == 0)
315 ret = btrfs_exclude_logged_extents(log, eb); 315 ret = btrfs_exclude_logged_extents(fs_info, eb);
316 if (wc->write) 316 if (wc->write)
317 btrfs_write_tree_block(eb); 317 btrfs_write_tree_block(eb);
318 if (wc->wait) 318 if (wc->wait)
@@ -341,6 +341,7 @@ static noinline int overwrite_item(struct btrfs_trans_handle *trans,
341 struct extent_buffer *eb, int slot, 341 struct extent_buffer *eb, int slot,
342 struct btrfs_key *key) 342 struct btrfs_key *key)
343{ 343{
344 struct btrfs_fs_info *fs_info = root->fs_info;
344 int ret; 345 int ret;
345 u32 item_size; 346 u32 item_size;
346 u64 saved_i_size = 0; 347 u64 saved_i_size = 0;
@@ -461,9 +462,9 @@ insert:
461 found_size = btrfs_item_size_nr(path->nodes[0], 462 found_size = btrfs_item_size_nr(path->nodes[0],
462 path->slots[0]); 463 path->slots[0]);
463 if (found_size > item_size) 464 if (found_size > item_size)
464 btrfs_truncate_item(root, path, item_size, 1); 465 btrfs_truncate_item(fs_info, path, item_size, 1);
465 else if (found_size < item_size) 466 else if (found_size < item_size)
466 btrfs_extend_item(root, path, 467 btrfs_extend_item(fs_info, path,
467 item_size - found_size); 468 item_size - found_size);
468 } else if (ret) { 469 } else if (ret) {
469 return ret; 470 return ret;
@@ -708,10 +709,10 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
708 * is this extent already allocated in the extent 709 * is this extent already allocated in the extent
709 * allocation tree? If so, just add a reference 710 * allocation tree? If so, just add a reference
710 */ 711 */
711 ret = btrfs_lookup_data_extent(root, ins.objectid, 712 ret = btrfs_lookup_data_extent(fs_info, ins.objectid,
712 ins.offset); 713 ins.offset);
713 if (ret == 0) { 714 if (ret == 0) {
714 ret = btrfs_inc_extent_ref(trans, root, 715 ret = btrfs_inc_extent_ref(trans, fs_info,
715 ins.objectid, ins.offset, 716 ins.objectid, ins.offset,
716 0, root->root_key.objectid, 717 0, root->root_key.objectid,
717 key->objectid, offset); 718 key->objectid, offset);
@@ -723,7 +724,8 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
723 * allocation tree 724 * allocation tree
724 */ 725 */
725 ret = btrfs_alloc_logged_file_extent(trans, 726 ret = btrfs_alloc_logged_file_extent(trans,
726 root, root->root_key.objectid, 727 fs_info,
728 root->root_key.objectid,
727 key->objectid, offset, &ins); 729 key->objectid, offset, &ins);
728 if (ret) 730 if (ret)
729 goto out; 731 goto out;
@@ -843,6 +845,7 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
843 struct inode *dir, 845 struct inode *dir,
844 struct btrfs_dir_item *di) 846 struct btrfs_dir_item *di)
845{ 847{
848 struct btrfs_fs_info *fs_info = root->fs_info;
846 struct inode *inode; 849 struct inode *inode;
847 char *name; 850 char *name;
848 int name_len; 851 int name_len;
@@ -875,7 +878,7 @@ static noinline int drop_one_dir_item(struct btrfs_trans_handle *trans,
875 if (ret) 878 if (ret)
876 goto out; 879 goto out;
877 else 880 else
878 ret = btrfs_run_delayed_items(trans, root); 881 ret = btrfs_run_delayed_items(trans, fs_info);
879out: 882out:
880 kfree(name); 883 kfree(name);
881 iput(inode); 884 iput(inode);
@@ -993,6 +996,7 @@ static inline int __add_inode_ref(struct btrfs_trans_handle *trans,
993 u64 ref_index, char *name, int namelen, 996 u64 ref_index, char *name, int namelen,
994 int *search_done) 997 int *search_done)
995{ 998{
999 struct btrfs_fs_info *fs_info = root->fs_info;
996 int ret; 1000 int ret;
997 char *victim_name; 1001 char *victim_name;
998 int victim_name_len; 1002 int victim_name_len;
@@ -1051,7 +1055,7 @@ again:
1051 kfree(victim_name); 1055 kfree(victim_name);
1052 if (ret) 1056 if (ret)
1053 return ret; 1057 return ret;
1054 ret = btrfs_run_delayed_items(trans, root); 1058 ret = btrfs_run_delayed_items(trans, fs_info);
1055 if (ret) 1059 if (ret)
1056 return ret; 1060 return ret;
1057 *search_done = 1; 1061 *search_done = 1;
@@ -1122,7 +1126,8 @@ again:
1122 victim_name_len); 1126 victim_name_len);
1123 if (!ret) 1127 if (!ret)
1124 ret = btrfs_run_delayed_items( 1128 ret = btrfs_run_delayed_items(
1125 trans, root); 1129 trans,
1130 fs_info);
1126 } 1131 }
1127 iput(victim_parent); 1132 iput(victim_parent);
1128 kfree(victim_name); 1133 kfree(victim_name);
@@ -1813,6 +1818,7 @@ static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1813 struct extent_buffer *eb, int slot, 1818 struct extent_buffer *eb, int slot,
1814 struct btrfs_key *key) 1819 struct btrfs_key *key)
1815{ 1820{
1821 struct btrfs_fs_info *fs_info = root->fs_info;
1816 int ret = 0; 1822 int ret = 0;
1817 u32 item_size = btrfs_item_size_nr(eb, slot); 1823 u32 item_size = btrfs_item_size_nr(eb, slot);
1818 struct btrfs_dir_item *di; 1824 struct btrfs_dir_item *di;
@@ -1825,7 +1831,7 @@ static noinline int replay_one_dir_item(struct btrfs_trans_handle *trans,
1825 ptr_end = ptr + item_size; 1831 ptr_end = ptr + item_size;
1826 while (ptr < ptr_end) { 1832 while (ptr < ptr_end) {
1827 di = (struct btrfs_dir_item *)ptr; 1833 di = (struct btrfs_dir_item *)ptr;
1828 if (verify_dir_item(root, eb, di)) 1834 if (verify_dir_item(fs_info, eb, di))
1829 return -EIO; 1835 return -EIO;
1830 name_len = btrfs_dir_name_len(eb, di); 1836 name_len = btrfs_dir_name_len(eb, di);
1831 ret = replay_one_name(trans, root, path, eb, di, key); 1837 ret = replay_one_name(trans, root, path, eb, di, key);
@@ -1980,6 +1986,7 @@ static noinline int check_item_in_log(struct btrfs_trans_handle *trans,
1980 struct inode *dir, 1986 struct inode *dir,
1981 struct btrfs_key *dir_key) 1987 struct btrfs_key *dir_key)
1982{ 1988{
1989 struct btrfs_fs_info *fs_info = root->fs_info;
1983 int ret; 1990 int ret;
1984 struct extent_buffer *eb; 1991 struct extent_buffer *eb;
1985 int slot; 1992 int slot;
@@ -2001,7 +2008,7 @@ again:
2001 ptr_end = ptr + item_size; 2008 ptr_end = ptr + item_size;
2002 while (ptr < ptr_end) { 2009 while (ptr < ptr_end) {
2003 di = (struct btrfs_dir_item *)ptr; 2010 di = (struct btrfs_dir_item *)ptr;
2004 if (verify_dir_item(root, eb, di)) { 2011 if (verify_dir_item(fs_info, eb, di)) {
2005 ret = -EIO; 2012 ret = -EIO;
2006 goto out; 2013 goto out;
2007 } 2014 }
@@ -2048,7 +2055,7 @@ again:
2048 ret = btrfs_unlink_inode(trans, root, dir, inode, 2055 ret = btrfs_unlink_inode(trans, root, dir, inode,
2049 name, name_len); 2056 name, name_len);
2050 if (!ret) 2057 if (!ret)
2051 ret = btrfs_run_delayed_items(trans, root); 2058 ret = btrfs_run_delayed_items(trans, fs_info);
2052 kfree(name); 2059 kfree(name);
2053 iput(inode); 2060 iput(inode);
2054 if (ret) 2061 if (ret)
@@ -2440,7 +2447,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2440 parent = path->nodes[*level]; 2447 parent = path->nodes[*level];
2441 root_owner = btrfs_header_owner(parent); 2448 root_owner = btrfs_header_owner(parent);
2442 2449
2443 next = btrfs_find_create_tree_block(root, bytenr); 2450 next = btrfs_find_create_tree_block(fs_info, bytenr);
2444 if (IS_ERR(next)) 2451 if (IS_ERR(next))
2445 return PTR_ERR(next); 2452 return PTR_ERR(next);
2446 2453
@@ -2469,8 +2476,9 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2469 2476
2470 WARN_ON(root_owner != 2477 WARN_ON(root_owner !=
2471 BTRFS_TREE_LOG_OBJECTID); 2478 BTRFS_TREE_LOG_OBJECTID);
2472 ret = btrfs_free_and_pin_reserved_extent(root, 2479 ret = btrfs_free_and_pin_reserved_extent(
2473 bytenr, blocksize); 2480 fs_info, bytenr,
2481 blocksize);
2474 if (ret) { 2482 if (ret) {
2475 free_extent_buffer(next); 2483 free_extent_buffer(next);
2476 return ret; 2484 return ret;
@@ -2547,7 +2555,8 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2547 } 2555 }
2548 2556
2549 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID); 2557 WARN_ON(root_owner != BTRFS_TREE_LOG_OBJECTID);
2550 ret = btrfs_free_and_pin_reserved_extent(root, 2558 ret = btrfs_free_and_pin_reserved_extent(
2559 fs_info,
2551 path->nodes[*level]->start, 2560 path->nodes[*level]->start,
2552 path->nodes[*level]->len); 2561 path->nodes[*level]->len);
2553 if (ret) 2562 if (ret)
@@ -2569,6 +2578,7 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2569static int walk_log_tree(struct btrfs_trans_handle *trans, 2578static int walk_log_tree(struct btrfs_trans_handle *trans,
2570 struct btrfs_root *log, struct walk_control *wc) 2579 struct btrfs_root *log, struct walk_control *wc)
2571{ 2580{
2581 struct btrfs_fs_info *fs_info = log->fs_info;
2572 int ret = 0; 2582 int ret = 0;
2573 int wret; 2583 int wret;
2574 int level; 2584 int level;
@@ -2617,15 +2627,15 @@ static int walk_log_tree(struct btrfs_trans_handle *trans,
2617 if (trans) { 2627 if (trans) {
2618 btrfs_tree_lock(next); 2628 btrfs_tree_lock(next);
2619 btrfs_set_lock_blocking(next); 2629 btrfs_set_lock_blocking(next);
2620 clean_tree_block(trans, log->fs_info, next); 2630 clean_tree_block(trans, fs_info, next);
2621 btrfs_wait_tree_block_writeback(next); 2631 btrfs_wait_tree_block_writeback(next);
2622 btrfs_tree_unlock(next); 2632 btrfs_tree_unlock(next);
2623 } 2633 }
2624 2634
2625 WARN_ON(log->root_key.objectid != 2635 WARN_ON(log->root_key.objectid !=
2626 BTRFS_TREE_LOG_OBJECTID); 2636 BTRFS_TREE_LOG_OBJECTID);
2627 ret = btrfs_free_and_pin_reserved_extent(log, next->start, 2637 ret = btrfs_free_and_pin_reserved_extent(fs_info,
2628 next->len); 2638 next->start, next->len);
2629 if (ret) 2639 if (ret)
2630 goto out; 2640 goto out;
2631 } 2641 }
@@ -2803,7 +2813,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2803 * wait for them until later. 2813 * wait for them until later.
2804 */ 2814 */
2805 blk_start_plug(&plug); 2815 blk_start_plug(&plug);
2806 ret = btrfs_write_marked_extents(log, &log->dirty_log_pages, mark); 2816 ret = btrfs_write_marked_extents(fs_info, &log->dirty_log_pages, mark);
2807 if (ret) { 2817 if (ret) {
2808 blk_finish_plug(&plug); 2818 blk_finish_plug(&plug);
2809 btrfs_abort_transaction(trans, ret); 2819 btrfs_abort_transaction(trans, ret);
@@ -2911,7 +2921,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2911 goto out_wake_log_root; 2921 goto out_wake_log_root;
2912 } 2922 }
2913 2923
2914 ret = btrfs_write_marked_extents(log_root_tree, 2924 ret = btrfs_write_marked_extents(fs_info,
2915 &log_root_tree->dirty_log_pages, 2925 &log_root_tree->dirty_log_pages,
2916 EXTENT_DIRTY | EXTENT_NEW); 2926 EXTENT_DIRTY | EXTENT_NEW);
2917 blk_finish_plug(&plug); 2927 blk_finish_plug(&plug);
@@ -2950,7 +2960,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2950 * the running transaction open, so a full commit can't hop 2960 * the running transaction open, so a full commit can't hop
2951 * in and cause problems either. 2961 * in and cause problems either.
2952 */ 2962 */
2953 ret = write_ctree_super(trans, fs_info->tree_root, 1); 2963 ret = write_ctree_super(trans, fs_info, 1);
2954 if (ret) { 2964 if (ret) {
2955 btrfs_set_log_full_commit(fs_info, trans); 2965 btrfs_set_log_full_commit(fs_info, trans);
2956 btrfs_abort_transaction(trans, ret); 2966 btrfs_abort_transaction(trans, ret);
diff --git a/fs/btrfs/uuid-tree.c b/fs/btrfs/uuid-tree.c
index 4464e80bb5ef..0a211d4ce8e0 100644
--- a/fs/btrfs/uuid-tree.c
+++ b/fs/btrfs/uuid-tree.c
@@ -133,7 +133,7 @@ int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans,
133 * An item with that type already exists. 133 * An item with that type already exists.
134 * Extend the item and store the new subid at the end. 134 * Extend the item and store the new subid at the end.
135 */ 135 */
136 btrfs_extend_item(uuid_root, path, sizeof(subid_le)); 136 btrfs_extend_item(fs_info, path, sizeof(subid_le));
137 eb = path->nodes[0]; 137 eb = path->nodes[0];
138 slot = path->slots[0]; 138 slot = path->slots[0];
139 offset = btrfs_item_ptr_offset(eb, slot); 139 offset = btrfs_item_ptr_offset(eb, slot);
@@ -231,7 +231,7 @@ int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
231 move_src = offset + sizeof(subid); 231 move_src = offset + sizeof(subid);
232 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot)); 232 move_len = item_size - (move_src - btrfs_item_ptr_offset(eb, slot));
233 memmove_extent_buffer(eb, move_dst, move_src, move_len); 233 memmove_extent_buffer(eb, move_dst, move_src, move_len);
234 btrfs_truncate_item(uuid_root, path, item_size - sizeof(subid), 1); 234 btrfs_truncate_item(fs_info, path, item_size - sizeof(subid), 1);
235 235
236out: 236out:
237 btrfs_free_path(path); 237 btrfs_free_path(path);
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index f983d258bf5c..e7127bd61485 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -134,9 +134,9 @@ const int btrfs_raid_mindev_error[BTRFS_NR_RAID_TYPES] = {
134}; 134};
135 135
136static int init_first_rw_device(struct btrfs_trans_handle *trans, 136static int init_first_rw_device(struct btrfs_trans_handle *trans,
137 struct btrfs_root *root, 137 struct btrfs_fs_info *fs_info,
138 struct btrfs_device *device); 138 struct btrfs_device *device);
139static int btrfs_relocate_sys_chunks(struct btrfs_root *root); 139static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info);
140static void __btrfs_reset_dev_stats(struct btrfs_device *dev); 140static void __btrfs_reset_dev_stats(struct btrfs_device *dev);
141static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev); 141static void btrfs_dev_stat_print_on_error(struct btrfs_device *dev);
142static void btrfs_dev_stat_print_on_load(struct btrfs_device *device); 142static void btrfs_dev_stat_print_on_load(struct btrfs_device *device);
@@ -1852,9 +1852,8 @@ void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1852 fs_info->fs_devices->latest_bdev = next_device->bdev; 1852 fs_info->fs_devices->latest_bdev = next_device->bdev;
1853} 1853}
1854 1854
1855int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) 1855int btrfs_rm_device(struct btrfs_fs_info *fs_info, char *device_path, u64 devid)
1856{ 1856{
1857 struct btrfs_fs_info *fs_info = root->fs_info;
1858 struct btrfs_device *device; 1857 struct btrfs_device *device;
1859 struct btrfs_fs_devices *cur_devices; 1858 struct btrfs_fs_devices *cur_devices;
1860 u64 num_devices; 1859 u64 num_devices;
@@ -1875,8 +1874,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1875 if (ret) 1874 if (ret)
1876 goto out; 1875 goto out;
1877 1876
1878 ret = btrfs_find_device_by_devspec(root, devid, device_path, 1877 ret = btrfs_find_device_by_devspec(fs_info, devid, device_path,
1879 &device); 1878 &device);
1880 if (ret) 1879 if (ret)
1881 goto out; 1880 goto out;
1882 1881
@@ -2092,10 +2091,10 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2092 call_rcu(&tgtdev->rcu, free_device); 2091 call_rcu(&tgtdev->rcu, free_device);
2093} 2092}
2094 2093
2095static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, 2094static int btrfs_find_device_by_path(struct btrfs_fs_info *fs_info,
2095 char *device_path,
2096 struct btrfs_device **device) 2096 struct btrfs_device **device)
2097{ 2097{
2098 struct btrfs_fs_info *fs_info = root->fs_info;
2099 int ret = 0; 2098 int ret = 0;
2100 struct btrfs_super_block *disk_super; 2099 struct btrfs_super_block *disk_super;
2101 u64 devid; 2100 u64 devid;
@@ -2119,12 +2118,10 @@ static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2119 return ret; 2118 return ret;
2120} 2119}
2121 2120
2122int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, 2121int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
2123 char *device_path, 2122 char *device_path,
2124 struct btrfs_device **device) 2123 struct btrfs_device **device)
2125{ 2124{
2126 struct btrfs_fs_info *fs_info = root->fs_info;
2127
2128 *device = NULL; 2125 *device = NULL;
2129 if (strcmp(device_path, "missing") == 0) { 2126 if (strcmp(device_path, "missing") == 0) {
2130 struct list_head *devices; 2127 struct list_head *devices;
@@ -2147,18 +2144,16 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2147 2144
2148 return 0; 2145 return 0;
2149 } else { 2146 } else {
2150 return btrfs_find_device_by_path(root, device_path, device); 2147 return btrfs_find_device_by_path(fs_info, device_path, device);
2151 } 2148 }
2152} 2149}
2153 2150
2154/* 2151/*
2155 * Lookup a device given by device id, or the path if the id is 0. 2152 * Lookup a device given by device id, or the path if the id is 0.
2156 */ 2153 */
2157int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid, 2154int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
2158 char *devpath, 2155 char *devpath, struct btrfs_device **device)
2159 struct btrfs_device **device)
2160{ 2156{
2161 struct btrfs_fs_info *fs_info = root->fs_info;
2162 int ret; 2157 int ret;
2163 2158
2164 if (devid) { 2159 if (devid) {
@@ -2170,7 +2165,7 @@ int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid,
2170 if (!devpath || !devpath[0]) 2165 if (!devpath || !devpath[0])
2171 return -EINVAL; 2166 return -EINVAL;
2172 2167
2173 ret = btrfs_find_device_missing_or_by_path(root, devpath, 2168 ret = btrfs_find_device_missing_or_by_path(fs_info, devpath,
2174 device); 2169 device);
2175 } 2170 }
2176 return ret; 2171 return ret;
@@ -2179,9 +2174,8 @@ int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid,
2179/* 2174/*
2180 * does all the dirty work required for changing file system's UUID. 2175 * does all the dirty work required for changing file system's UUID.
2181 */ 2176 */
2182static int btrfs_prepare_sprout(struct btrfs_root *root) 2177static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
2183{ 2178{
2184 struct btrfs_fs_info *fs_info = root->fs_info;
2185 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 2179 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2186 struct btrfs_fs_devices *old_devices; 2180 struct btrfs_fs_devices *old_devices;
2187 struct btrfs_fs_devices *seed_devices; 2181 struct btrfs_fs_devices *seed_devices;
@@ -2401,7 +2395,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2401 2395
2402 if (seeding_dev) { 2396 if (seeding_dev) {
2403 sb->s_flags &= ~MS_RDONLY; 2397 sb->s_flags &= ~MS_RDONLY;
2404 ret = btrfs_prepare_sprout(root); 2398 ret = btrfs_prepare_sprout(fs_info);
2405 BUG_ON(ret); /* -ENOMEM */ 2399 BUG_ON(ret); /* -ENOMEM */
2406 } 2400 }
2407 2401
@@ -2446,7 +2440,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2446 2440
2447 if (seeding_dev) { 2441 if (seeding_dev) {
2448 lock_chunks(fs_info); 2442 lock_chunks(fs_info);
2449 ret = init_first_rw_device(trans, root, device); 2443 ret = init_first_rw_device(trans, fs_info, device);
2450 unlock_chunks(fs_info); 2444 unlock_chunks(fs_info);
2451 if (ret) { 2445 if (ret) {
2452 btrfs_abort_transaction(trans, ret); 2446 btrfs_abort_transaction(trans, ret);
@@ -2490,7 +2484,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2490 if (ret) /* transaction commit */ 2484 if (ret) /* transaction commit */
2491 return ret; 2485 return ret;
2492 2486
2493 ret = btrfs_relocate_sys_chunks(root); 2487 ret = btrfs_relocate_sys_chunks(fs_info);
2494 if (ret < 0) 2488 if (ret < 0)
2495 btrfs_handle_fs_error(fs_info, ret, 2489 btrfs_handle_fs_error(fs_info, ret,
2496 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); 2490 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
@@ -2521,14 +2515,14 @@ error:
2521 return ret; 2515 return ret;
2522} 2516}
2523 2517
2524int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, 2518int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2519 char *device_path,
2525 struct btrfs_device *srcdev, 2520 struct btrfs_device *srcdev,
2526 struct btrfs_device **device_out) 2521 struct btrfs_device **device_out)
2527{ 2522{
2528 struct request_queue *q; 2523 struct request_queue *q;
2529 struct btrfs_device *device; 2524 struct btrfs_device *device;
2530 struct block_device *bdev; 2525 struct block_device *bdev;
2531 struct btrfs_fs_info *fs_info = root->fs_info;
2532 struct list_head *devices; 2526 struct list_head *devices;
2533 struct rcu_string *name; 2527 struct rcu_string *name;
2534 u64 devid = BTRFS_DEV_REPLACE_DEVID; 2528 u64 devid = BTRFS_DEV_REPLACE_DEVID;
@@ -2805,7 +2799,6 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2805{ 2799{
2806 struct extent_map_tree *em_tree; 2800 struct extent_map_tree *em_tree;
2807 struct extent_map *em; 2801 struct extent_map *em;
2808 struct btrfs_root *extent_root = fs_info->extent_root;
2809 struct map_lookup *map; 2802 struct map_lookup *map;
2810 u64 dev_extent_len = 0; 2803 u64 dev_extent_len = 0;
2811 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; 2804 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
@@ -2832,7 +2825,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2832 } 2825 }
2833 map = em->map_lookup; 2826 map = em->map_lookup;
2834 lock_chunks(fs_info); 2827 lock_chunks(fs_info);
2835 check_system_chunk(trans, extent_root, map->type); 2828 check_system_chunk(trans, fs_info, map->type);
2836 unlock_chunks(fs_info); 2829 unlock_chunks(fs_info);
2837 2830
2838 /* 2831 /*
@@ -2929,9 +2922,9 @@ static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2929 return -ENOSPC; 2922 return -ENOSPC;
2930 2923
2931 /* step one, relocate all the extents inside this chunk */ 2924 /* step one, relocate all the extents inside this chunk */
2932 btrfs_scrub_pause(root); 2925 btrfs_scrub_pause(fs_info);
2933 ret = btrfs_relocate_block_group(fs_info, chunk_offset); 2926 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2934 btrfs_scrub_continue(root); 2927 btrfs_scrub_continue(fs_info);
2935 if (ret) 2928 if (ret)
2936 return ret; 2929 return ret;
2937 2930
@@ -2952,9 +2945,8 @@ static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2952 return ret; 2945 return ret;
2953} 2946}
2954 2947
2955static int btrfs_relocate_sys_chunks(struct btrfs_root *root) 2948static int btrfs_relocate_sys_chunks(struct btrfs_fs_info *fs_info)
2956{ 2949{
2957 struct btrfs_fs_info *fs_info = root->fs_info;
2958 struct btrfs_root *chunk_root = fs_info->chunk_root; 2950 struct btrfs_root *chunk_root = fs_info->chunk_root;
2959 struct btrfs_path *path; 2951 struct btrfs_path *path;
2960 struct extent_buffer *leaf; 2952 struct extent_buffer *leaf;
@@ -3369,11 +3361,10 @@ static int chunk_soft_convert_filter(u64 chunk_type,
3369 return 0; 3361 return 0;
3370} 3362}
3371 3363
3372static int should_balance_chunk(struct btrfs_root *root, 3364static int should_balance_chunk(struct btrfs_fs_info *fs_info,
3373 struct extent_buffer *leaf, 3365 struct extent_buffer *leaf,
3374 struct btrfs_chunk *chunk, u64 chunk_offset) 3366 struct btrfs_chunk *chunk, u64 chunk_offset)
3375{ 3367{
3376 struct btrfs_fs_info *fs_info = root->fs_info;
3377 struct btrfs_balance_control *bctl = fs_info->balance_ctl; 3368 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3378 struct btrfs_balance_args *bargs = NULL; 3369 struct btrfs_balance_args *bargs = NULL;
3379 u64 chunk_type = btrfs_chunk_type(leaf, chunk); 3370 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
@@ -3607,7 +3598,7 @@ again:
3607 spin_unlock(&fs_info->balance_lock); 3598 spin_unlock(&fs_info->balance_lock);
3608 } 3599 }
3609 3600
3610 ret = should_balance_chunk(chunk_root, leaf, chunk, 3601 ret = should_balance_chunk(fs_info, leaf, chunk,
3611 found_key.offset); 3602 found_key.offset);
3612 3603
3613 btrfs_release_path(path); 3604 btrfs_release_path(path);
@@ -3660,7 +3651,7 @@ again:
3660 goto error; 3651 goto error;
3661 } 3652 }
3662 3653
3663 ret = btrfs_force_chunk_alloc(trans, chunk_root, 3654 ret = btrfs_force_chunk_alloc(trans, fs_info,
3664 BTRFS_BLOCK_GROUP_DATA); 3655 BTRFS_BLOCK_GROUP_DATA);
3665 btrfs_end_transaction(trans, chunk_root); 3656 btrfs_end_transaction(trans, chunk_root);
3666 if (ret < 0) { 3657 if (ret < 0) {
@@ -4522,11 +4513,10 @@ done:
4522 return ret; 4513 return ret;
4523} 4514}
4524 4515
4525static int btrfs_add_system_chunk(struct btrfs_root *root, 4516static int btrfs_add_system_chunk(struct btrfs_fs_info *fs_info,
4526 struct btrfs_key *key, 4517 struct btrfs_key *key,
4527 struct btrfs_chunk *chunk, int item_size) 4518 struct btrfs_chunk *chunk, int item_size)
4528{ 4519{
4529 struct btrfs_fs_info *fs_info = root->fs_info;
4530 struct btrfs_super_block *super_copy = fs_info->super_copy; 4520 struct btrfs_super_block *super_copy = fs_info->super_copy;
4531 struct btrfs_disk_key disk_key; 4521 struct btrfs_disk_key disk_key;
4532 u32 array_size; 4522 u32 array_size;
@@ -4595,10 +4585,10 @@ static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
4595 / sizeof(struct btrfs_stripe) + 1) 4585 / sizeof(struct btrfs_stripe) + 1)
4596 4586
4597static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans, 4587static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4598 struct btrfs_root *extent_root, u64 start, 4588 struct btrfs_fs_info *fs_info, u64 start,
4599 u64 type) 4589 u64 type)
4600{ 4590{
4601 struct btrfs_fs_info *info = extent_root->fs_info; 4591 struct btrfs_fs_info *info = trans->fs_info;
4602 struct btrfs_fs_devices *fs_devices = info->fs_devices; 4592 struct btrfs_fs_devices *fs_devices = info->fs_devices;
4603 struct list_head *cur; 4593 struct list_head *cur;
4604 struct map_lookup *map = NULL; 4594 struct map_lookup *map = NULL;
@@ -4852,7 +4842,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4852 goto error; 4842 goto error;
4853 } 4843 }
4854 4844
4855 ret = btrfs_make_block_group(trans, extent_root, 0, type, 4845 ret = btrfs_make_block_group(trans, info, 0, type,
4856 BTRFS_FIRST_CHUNK_TREE_OBJECTID, 4846 BTRFS_FIRST_CHUNK_TREE_OBJECTID,
4857 start, num_bytes); 4847 start, num_bytes);
4858 if (ret) 4848 if (ret)
@@ -4997,8 +4987,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4997 * TODO: Cleanup of inserted chunk root in case of 4987 * TODO: Cleanup of inserted chunk root in case of
4998 * failure. 4988 * failure.
4999 */ 4989 */
5000 ret = btrfs_add_system_chunk(chunk_root, &key, chunk, 4990 ret = btrfs_add_system_chunk(fs_info, &key, chunk, item_size);
5001 item_size);
5002 } 4991 }
5003 4992
5004out: 4993out:
@@ -5015,37 +5004,34 @@ out:
5015 * bootstrap process of adding storage to a seed btrfs. 5004 * bootstrap process of adding storage to a seed btrfs.
5016 */ 5005 */
5017int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, 5006int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
5018 struct btrfs_root *extent_root, u64 type) 5007 struct btrfs_fs_info *fs_info, u64 type)
5019{ 5008{
5020 struct btrfs_fs_info *fs_info = extent_root->fs_info;
5021 u64 chunk_offset; 5009 u64 chunk_offset;
5022 5010
5023 ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); 5011 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
5024 chunk_offset = find_next_chunk(fs_info); 5012 chunk_offset = find_next_chunk(fs_info);
5025 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); 5013 return __btrfs_alloc_chunk(trans, fs_info, chunk_offset, type);
5026} 5014}
5027 5015
5028static noinline int init_first_rw_device(struct btrfs_trans_handle *trans, 5016static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
5029 struct btrfs_root *root, 5017 struct btrfs_fs_info *fs_info,
5030 struct btrfs_device *device) 5018 struct btrfs_device *device)
5031{ 5019{
5020 struct btrfs_root *extent_root = fs_info->extent_root;
5032 u64 chunk_offset; 5021 u64 chunk_offset;
5033 u64 sys_chunk_offset; 5022 u64 sys_chunk_offset;
5034 u64 alloc_profile; 5023 u64 alloc_profile;
5035 struct btrfs_fs_info *fs_info = root->fs_info;
5036 struct btrfs_root *extent_root = fs_info->extent_root;
5037 int ret; 5024 int ret;
5038 5025
5039 chunk_offset = find_next_chunk(fs_info); 5026 chunk_offset = find_next_chunk(fs_info);
5040 alloc_profile = btrfs_get_alloc_profile(extent_root, 0); 5027 alloc_profile = btrfs_get_alloc_profile(extent_root, 0);
5041 ret = __btrfs_alloc_chunk(trans, extent_root, chunk_offset, 5028 ret = __btrfs_alloc_chunk(trans, fs_info, chunk_offset, alloc_profile);
5042 alloc_profile);
5043 if (ret) 5029 if (ret)
5044 return ret; 5030 return ret;
5045 5031
5046 sys_chunk_offset = find_next_chunk(fs_info); 5032 sys_chunk_offset = find_next_chunk(fs_info);
5047 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); 5033 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
5048 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, 5034 ret = __btrfs_alloc_chunk(trans, fs_info, sys_chunk_offset,
5049 alloc_profile); 5035 alloc_profile);
5050 return ret; 5036 return ret;
5051} 5037}
@@ -5068,9 +5054,8 @@ static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5068 return max_errors; 5054 return max_errors;
5069} 5055}
5070 5056
5071int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) 5057int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset)
5072{ 5058{
5073 struct btrfs_fs_info *fs_info = root->fs_info;
5074 struct extent_map *em; 5059 struct extent_map *em;
5075 struct map_lookup *map; 5060 struct map_lookup *map;
5076 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; 5061 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
@@ -5185,11 +5170,10 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5185 return ret; 5170 return ret;
5186} 5171}
5187 5172
5188unsigned long btrfs_full_stripe_len(struct btrfs_root *root, 5173unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
5189 struct btrfs_mapping_tree *map_tree, 5174 struct btrfs_mapping_tree *map_tree,
5190 u64 logical) 5175 u64 logical)
5191{ 5176{
5192 struct btrfs_fs_info *fs_info = root->fs_info;
5193 struct extent_map *em; 5177 struct extent_map *em;
5194 struct map_lookup *map; 5178 struct map_lookup *map;
5195 struct extent_map_tree *em_tree = &map_tree->map_tree; 5179 struct extent_map_tree *em_tree = &map_tree->map_tree;
@@ -6075,8 +6059,7 @@ static void btrfs_end_bio(struct bio *bio)
6075 * This will add one bio to the pending list for a device and make sure 6059 * This will add one bio to the pending list for a device and make sure
6076 * the work struct is scheduled. 6060 * the work struct is scheduled.
6077 */ 6061 */
6078static noinline void btrfs_schedule_bio(struct btrfs_root *root, 6062static noinline void btrfs_schedule_bio(struct btrfs_device *device,
6079 struct btrfs_device *device,
6080 struct bio *bio) 6063 struct bio *bio)
6081{ 6064{
6082 struct btrfs_fs_info *fs_info = device->fs_info; 6065 struct btrfs_fs_info *fs_info = device->fs_info;
@@ -6127,11 +6110,11 @@ static noinline void btrfs_schedule_bio(struct btrfs_root *root,
6127 btrfs_queue_work(fs_info->submit_workers, &device->work); 6110 btrfs_queue_work(fs_info->submit_workers, &device->work);
6128} 6111}
6129 6112
6130static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, 6113static void submit_stripe_bio(struct btrfs_bio *bbio, struct bio *bio,
6131 struct bio *bio, u64 physical, int dev_nr, 6114 u64 physical, int dev_nr, int async)
6132 int async)
6133{ 6115{
6134 struct btrfs_device *dev = bbio->stripes[dev_nr].dev; 6116 struct btrfs_device *dev = bbio->stripes[dev_nr].dev;
6117 struct btrfs_fs_info *fs_info = bbio->fs_info;
6135 6118
6136 bio->bi_private = bbio; 6119 bio->bi_private = bbio;
6137 btrfs_io_bio(bio)->stripe_index = dev_nr; 6120 btrfs_io_bio(bio)->stripe_index = dev_nr;
@@ -6154,10 +6137,10 @@ static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
6154#endif 6137#endif
6155 bio->bi_bdev = dev->bdev; 6138 bio->bi_bdev = dev->bdev;
6156 6139
6157 btrfs_bio_counter_inc_noblocked(root->fs_info); 6140 btrfs_bio_counter_inc_noblocked(fs_info);
6158 6141
6159 if (async) 6142 if (async)
6160 btrfs_schedule_bio(root, dev, bio); 6143 btrfs_schedule_bio(dev, bio);
6161 else 6144 else
6162 btrfsic_submit_bio(bio); 6145 btrfsic_submit_bio(bio);
6163} 6146}
@@ -6176,10 +6159,9 @@ static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6176 } 6159 }
6177} 6160}
6178 6161
6179int btrfs_map_bio(struct btrfs_root *root, struct bio *bio, 6162int btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
6180 int mirror_num, int async_submit) 6163 int mirror_num, int async_submit)
6181{ 6164{
6182 struct btrfs_fs_info *fs_info = root->fs_info;
6183 struct btrfs_device *dev; 6165 struct btrfs_device *dev;
6184 struct bio *first_bio = bio; 6166 struct bio *first_bio = bio;
6185 u64 logical = (u64)bio->bi_iter.bi_sector << 9; 6167 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
@@ -6213,10 +6195,11 @@ int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6213 /* In this case, map_length has been set to the length of 6195 /* In this case, map_length has been set to the length of
6214 a single stripe; not the whole write */ 6196 a single stripe; not the whole write */
6215 if (bio_op(bio) == REQ_OP_WRITE) { 6197 if (bio_op(bio) == REQ_OP_WRITE) {
6216 ret = raid56_parity_write(root, bio, bbio, map_length); 6198 ret = raid56_parity_write(fs_info, bio, bbio,
6199 map_length);
6217 } else { 6200 } else {
6218 ret = raid56_parity_recover(root, bio, bbio, map_length, 6201 ret = raid56_parity_recover(fs_info, bio, bbio,
6219 mirror_num, 1); 6202 map_length, mirror_num, 1);
6220 } 6203 }
6221 6204
6222 btrfs_bio_counter_dec(fs_info); 6205 btrfs_bio_counter_dec(fs_info);
@@ -6244,9 +6227,8 @@ int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6244 } else 6227 } else
6245 bio = first_bio; 6228 bio = first_bio;
6246 6229
6247 submit_stripe_bio(root, bbio, bio, 6230 submit_stripe_bio(bbio, bio, bbio->stripes[dev_nr].physical,
6248 bbio->stripes[dev_nr].physical, dev_nr, 6231 dev_nr, async_submit);
6249 async_submit);
6250 } 6232 }
6251 btrfs_bio_counter_dec(fs_info); 6233 btrfs_bio_counter_dec(fs_info);
6252 return 0; 6234 return 0;
@@ -6272,8 +6254,7 @@ struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
6272 return NULL; 6254 return NULL;
6273} 6255}
6274 6256
6275static struct btrfs_device *add_missing_dev(struct btrfs_root *root, 6257static struct btrfs_device *add_missing_dev(struct btrfs_fs_devices *fs_devices,
6276 struct btrfs_fs_devices *fs_devices,
6277 u64 devid, u8 *dev_uuid) 6258 u64 devid, u8 *dev_uuid)
6278{ 6259{
6279 struct btrfs_device *device; 6260 struct btrfs_device *device;
@@ -6344,11 +6325,10 @@ struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
6344} 6325}
6345 6326
6346/* Return -EIO if any error, otherwise return 0. */ 6327/* Return -EIO if any error, otherwise return 0. */
6347static int btrfs_check_chunk_valid(struct btrfs_root *root, 6328static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
6348 struct extent_buffer *leaf, 6329 struct extent_buffer *leaf,
6349 struct btrfs_chunk *chunk, u64 logical) 6330 struct btrfs_chunk *chunk, u64 logical)
6350{ 6331{
6351 struct btrfs_fs_info *fs_info = root->fs_info;
6352 u64 length; 6332 u64 length;
6353 u64 stripe_len; 6333 u64 stripe_len;
6354 u16 num_stripes; 6334 u16 num_stripes;
@@ -6409,11 +6389,10 @@ static int btrfs_check_chunk_valid(struct btrfs_root *root,
6409 return 0; 6389 return 0;
6410} 6390}
6411 6391
6412static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key, 6392static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key,
6413 struct extent_buffer *leaf, 6393 struct extent_buffer *leaf,
6414 struct btrfs_chunk *chunk) 6394 struct btrfs_chunk *chunk)
6415{ 6395{
6416 struct btrfs_fs_info *fs_info = root->fs_info;
6417 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree; 6396 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6418 struct map_lookup *map; 6397 struct map_lookup *map;
6419 struct extent_map *em; 6398 struct extent_map *em;
@@ -6431,7 +6410,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6431 stripe_len = btrfs_chunk_stripe_len(leaf, chunk); 6410 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6432 num_stripes = btrfs_chunk_num_stripes(leaf, chunk); 6411 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6433 6412
6434 ret = btrfs_check_chunk_valid(root, leaf, chunk, logical); 6413 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, logical);
6435 if (ret) 6414 if (ret)
6436 return ret; 6415 return ret;
6437 6416
@@ -6487,8 +6466,8 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6487 } 6466 }
6488 if (!map->stripes[i].dev) { 6467 if (!map->stripes[i].dev) {
6489 map->stripes[i].dev = 6468 map->stripes[i].dev =
6490 add_missing_dev(root, fs_info->fs_devices, 6469 add_missing_dev(fs_info->fs_devices, devid,
6491 devid, uuid); 6470 uuid);
6492 if (!map->stripes[i].dev) { 6471 if (!map->stripes[i].dev) {
6493 free_extent_map(em); 6472 free_extent_map(em);
6494 return -EIO; 6473 return -EIO;
@@ -6531,10 +6510,9 @@ static void fill_device_from_item(struct extent_buffer *leaf,
6531 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); 6510 read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
6532} 6511}
6533 6512
6534static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, 6513static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
6535 u8 *fsid) 6514 u8 *fsid)
6536{ 6515{
6537 struct btrfs_fs_info *fs_info = root->fs_info;
6538 struct btrfs_fs_devices *fs_devices; 6516 struct btrfs_fs_devices *fs_devices;
6539 int ret; 6517 int ret;
6540 6518
@@ -6587,11 +6565,10 @@ out:
6587 return fs_devices; 6565 return fs_devices;
6588} 6566}
6589 6567
6590static int read_one_dev(struct btrfs_root *root, 6568static int read_one_dev(struct btrfs_fs_info *fs_info,
6591 struct extent_buffer *leaf, 6569 struct extent_buffer *leaf,
6592 struct btrfs_dev_item *dev_item) 6570 struct btrfs_dev_item *dev_item)
6593{ 6571{
6594 struct btrfs_fs_info *fs_info = root->fs_info;
6595 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 6572 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6596 struct btrfs_device *device; 6573 struct btrfs_device *device;
6597 u64 devid; 6574 u64 devid;
@@ -6606,7 +6583,7 @@ static int read_one_dev(struct btrfs_root *root,
6606 BTRFS_UUID_SIZE); 6583 BTRFS_UUID_SIZE);
6607 6584
6608 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_UUID_SIZE)) { 6585 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_UUID_SIZE)) {
6609 fs_devices = open_seed_devices(root, fs_uuid); 6586 fs_devices = open_seed_devices(fs_info, fs_uuid);
6610 if (IS_ERR(fs_devices)) 6587 if (IS_ERR(fs_devices))
6611 return PTR_ERR(fs_devices); 6588 return PTR_ERR(fs_devices);
6612 } 6589 }
@@ -6616,7 +6593,7 @@ static int read_one_dev(struct btrfs_root *root,
6616 if (!btrfs_test_opt(fs_info, DEGRADED)) 6593 if (!btrfs_test_opt(fs_info, DEGRADED))
6617 return -EIO; 6594 return -EIO;
6618 6595
6619 device = add_missing_dev(root, fs_devices, devid, dev_uuid); 6596 device = add_missing_dev(fs_devices, devid, dev_uuid);
6620 if (!device) 6597 if (!device)
6621 return -ENOMEM; 6598 return -ENOMEM;
6622 btrfs_warn(fs_info, "devid %llu uuid %pU missing", 6599 btrfs_warn(fs_info, "devid %llu uuid %pU missing",
@@ -6694,7 +6671,7 @@ int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
6694 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will 6671 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
6695 * overallocate but we can keep it as-is, only the first page is used. 6672 * overallocate but we can keep it as-is, only the first page is used.
6696 */ 6673 */
6697 sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET); 6674 sb = btrfs_find_create_tree_block(fs_info, BTRFS_SUPER_INFO_OFFSET);
6698 if (IS_ERR(sb)) 6675 if (IS_ERR(sb))
6699 return PTR_ERR(sb); 6676 return PTR_ERR(sb);
6700 set_extent_buffer_uptodate(sb); 6677 set_extent_buffer_uptodate(sb);
@@ -6765,7 +6742,7 @@ int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
6765 if (cur_offset + len > array_size) 6742 if (cur_offset + len > array_size)
6766 goto out_short_read; 6743 goto out_short_read;
6767 6744
6768 ret = read_one_chunk(root, &key, sb, chunk); 6745 ret = read_one_chunk(fs_info, &key, sb, chunk);
6769 if (ret) 6746 if (ret)
6770 break; 6747 break;
6771 } else { 6748 } else {
@@ -6837,14 +6814,14 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
6837 struct btrfs_dev_item *dev_item; 6814 struct btrfs_dev_item *dev_item;
6838 dev_item = btrfs_item_ptr(leaf, slot, 6815 dev_item = btrfs_item_ptr(leaf, slot,
6839 struct btrfs_dev_item); 6816 struct btrfs_dev_item);
6840 ret = read_one_dev(root, leaf, dev_item); 6817 ret = read_one_dev(fs_info, leaf, dev_item);
6841 if (ret) 6818 if (ret)
6842 goto error; 6819 goto error;
6843 total_dev++; 6820 total_dev++;
6844 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) { 6821 } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
6845 struct btrfs_chunk *chunk; 6822 struct btrfs_chunk *chunk;
6846 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); 6823 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
6847 ret = read_one_chunk(root, &found_key, leaf, chunk); 6824 ret = read_one_chunk(fs_info, &found_key, leaf, chunk);
6848 if (ret) 6825 if (ret)
6849 goto error; 6826 goto error;
6850 } 6827 }
@@ -7095,10 +7072,9 @@ static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7095 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS)); 7072 btrfs_dev_stat_read(dev, BTRFS_DEV_STAT_GENERATION_ERRS));
7096} 7073}
7097 7074
7098int btrfs_get_dev_stats(struct btrfs_root *root, 7075int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
7099 struct btrfs_ioctl_get_dev_stats *stats) 7076 struct btrfs_ioctl_get_dev_stats *stats)
7100{ 7077{
7101 struct btrfs_fs_info *fs_info = root->fs_info;
7102 struct btrfs_device *dev; 7078 struct btrfs_device *dev;
7103 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; 7079 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7104 int i; 7080 int i;
@@ -7185,10 +7161,9 @@ void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7185} 7161}
7186 7162
7187/* Must be invoked during the transaction commit */ 7163/* Must be invoked during the transaction commit */
7188void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, 7164void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
7189 struct btrfs_transaction *transaction) 7165 struct btrfs_transaction *transaction)
7190{ 7166{
7191 struct btrfs_fs_info *fs_info = root->fs_info;
7192 struct extent_map *em; 7167 struct extent_map *em;
7193 struct map_lookup *map; 7168 struct map_lookup *map;
7194 struct btrfs_device *dev; 7169 struct btrfs_device *dev;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 724d18ca7ff0..18b4449fa5a5 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -408,10 +408,10 @@ int btrfs_rmap_block(struct btrfs_fs_info *fs_info,
408int btrfs_read_sys_array(struct btrfs_fs_info *fs_info); 408int btrfs_read_sys_array(struct btrfs_fs_info *fs_info);
409int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info); 409int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info);
410int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, 410int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
411 struct btrfs_root *extent_root, u64 type); 411 struct btrfs_fs_info *fs_info, u64 type);
412void btrfs_mapping_init(struct btrfs_mapping_tree *tree); 412void btrfs_mapping_init(struct btrfs_mapping_tree *tree);
413void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree); 413void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree);
414int btrfs_map_bio(struct btrfs_root *root, struct bio *bio, 414int btrfs_map_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
415 int mirror_num, int async_submit); 415 int mirror_num, int async_submit);
416int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, 416int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
417 fmode_t flags, void *holder); 417 fmode_t flags, void *holder);
@@ -421,16 +421,17 @@ int btrfs_close_devices(struct btrfs_fs_devices *fs_devices);
421void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step); 421void btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices, int step);
422void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info, 422void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
423 struct btrfs_device *device, struct btrfs_device *this_dev); 423 struct btrfs_device *device, struct btrfs_device *this_dev);
424int btrfs_find_device_missing_or_by_path(struct btrfs_root *root, 424int btrfs_find_device_missing_or_by_path(struct btrfs_fs_info *fs_info,
425 char *device_path, 425 char *device_path,
426 struct btrfs_device **device); 426 struct btrfs_device **device);
427int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid, 427int btrfs_find_device_by_devspec(struct btrfs_fs_info *fs_info, u64 devid,
428 char *devpath, 428 char *devpath,
429 struct btrfs_device **device); 429 struct btrfs_device **device);
430struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info, 430struct btrfs_device *btrfs_alloc_device(struct btrfs_fs_info *fs_info,
431 const u64 *devid, 431 const u64 *devid,
432 const u8 *uuid); 432 const u8 *uuid);
433int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid); 433int btrfs_rm_device(struct btrfs_fs_info *fs_info,
434 char *device_path, u64 devid);
434void btrfs_cleanup_fs_uuids(void); 435void btrfs_cleanup_fs_uuids(void);
435int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len); 436int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len);
436int btrfs_grow_device(struct btrfs_trans_handle *trans, 437int btrfs_grow_device(struct btrfs_trans_handle *trans,
@@ -439,7 +440,8 @@ struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
439 u8 *uuid, u8 *fsid); 440 u8 *uuid, u8 *fsid);
440int btrfs_shrink_device(struct btrfs_device *device, u64 new_size); 441int btrfs_shrink_device(struct btrfs_device *device, u64 new_size);
441int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *path); 442int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *path);
442int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path, 443int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
444 char *device_path,
443 struct btrfs_device *srcdev, 445 struct btrfs_device *srcdev,
444 struct btrfs_device **device_out); 446 struct btrfs_device **device_out);
445int btrfs_balance(struct btrfs_balance_control *bctl, 447int btrfs_balance(struct btrfs_balance_control *bctl,
@@ -450,7 +452,7 @@ int btrfs_pause_balance(struct btrfs_fs_info *fs_info);
450int btrfs_cancel_balance(struct btrfs_fs_info *fs_info); 452int btrfs_cancel_balance(struct btrfs_fs_info *fs_info);
451int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info); 453int btrfs_create_uuid_tree(struct btrfs_fs_info *fs_info);
452int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info); 454int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info);
453int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset); 455int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset);
454int find_free_dev_extent_start(struct btrfs_transaction *transaction, 456int find_free_dev_extent_start(struct btrfs_transaction *transaction,
455 struct btrfs_device *device, u64 num_bytes, 457 struct btrfs_device *device, u64 num_bytes,
456 u64 search_start, u64 *start, u64 *max_avail); 458 u64 search_start, u64 *start, u64 *max_avail);
@@ -458,7 +460,7 @@ int find_free_dev_extent(struct btrfs_trans_handle *trans,
458 struct btrfs_device *device, u64 num_bytes, 460 struct btrfs_device *device, u64 num_bytes,
459 u64 *start, u64 *max_avail); 461 u64 *start, u64 *max_avail);
460void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index); 462void btrfs_dev_stat_inc_and_print(struct btrfs_device *dev, int index);
461int btrfs_get_dev_stats(struct btrfs_root *root, 463int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
462 struct btrfs_ioctl_get_dev_stats *stats); 464 struct btrfs_ioctl_get_dev_stats *stats);
463void btrfs_init_devices_late(struct btrfs_fs_info *fs_info); 465void btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
464int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info); 466int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
@@ -475,7 +477,7 @@ void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
475void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path); 477void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path);
476int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, 478int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
477 u64 logical, u64 len, int mirror_num); 479 u64 logical, u64 len, int mirror_num);
478unsigned long btrfs_full_stripe_len(struct btrfs_root *root, 480unsigned long btrfs_full_stripe_len(struct btrfs_fs_info *fs_info,
479 struct btrfs_mapping_tree *map_tree, 481 struct btrfs_mapping_tree *map_tree,
480 u64 logical); 482 u64 logical);
481int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans, 483int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
@@ -529,7 +531,7 @@ static inline void btrfs_dev_stat_reset(struct btrfs_device *dev,
529} 531}
530 532
531void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info); 533void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info);
532void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, 534void btrfs_update_commit_device_bytes_used(struct btrfs_fs_info *fs_info,
533 struct btrfs_transaction *transaction); 535 struct btrfs_transaction *transaction);
534 536
535static inline void lock_chunks(struct btrfs_fs_info *fs_info) 537static inline void lock_chunks(struct btrfs_fs_info *fs_info)
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
index b29557482ada..7dfd2f06eb55 100644
--- a/fs/btrfs/xattr.c
+++ b/fs/btrfs/xattr.c
@@ -94,6 +94,7 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
94{ 94{
95 struct btrfs_dir_item *di = NULL; 95 struct btrfs_dir_item *di = NULL;
96 struct btrfs_root *root = BTRFS_I(inode)->root; 96 struct btrfs_root *root = BTRFS_I(inode)->root;
97 struct btrfs_fs_info *fs_info = root->fs_info;
97 struct btrfs_path *path; 98 struct btrfs_path *path;
98 size_t name_len = strlen(name); 99 size_t name_len = strlen(name);
99 int ret = 0; 100 int ret = 0;
@@ -149,14 +150,14 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
149 */ 150 */
150 ret = 0; 151 ret = 0;
151 btrfs_assert_tree_locked(path->nodes[0]); 152 btrfs_assert_tree_locked(path->nodes[0]);
152 di = btrfs_match_dir_item_name(root, path, name, name_len); 153 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
153 if (!di && !(flags & XATTR_REPLACE)) { 154 if (!di && !(flags & XATTR_REPLACE)) {
154 ret = -ENOSPC; 155 ret = -ENOSPC;
155 goto out; 156 goto out;
156 } 157 }
157 } else if (ret == -EEXIST) { 158 } else if (ret == -EEXIST) {
158 ret = 0; 159 ret = 0;
159 di = btrfs_match_dir_item_name(root, path, name, name_len); 160 di = btrfs_match_dir_item_name(fs_info, path, name, name_len);
160 ASSERT(di); /* logic error */ 161 ASSERT(di); /* logic error */
161 } else if (ret) { 162 } else if (ret) {
162 goto out; 163 goto out;
@@ -185,7 +186,7 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
185 char *ptr; 186 char *ptr;
186 187
187 if (size > old_data_len) { 188 if (size > old_data_len) {
188 if (btrfs_leaf_free_space(root, leaf) < 189 if (btrfs_leaf_free_space(fs_info, leaf) <
189 (size - old_data_len)) { 190 (size - old_data_len)) {
190 ret = -ENOSPC; 191 ret = -ENOSPC;
191 goto out; 192 goto out;
@@ -195,16 +196,17 @@ static int do_setxattr(struct btrfs_trans_handle *trans,
195 if (old_data_len + name_len + sizeof(*di) == item_size) { 196 if (old_data_len + name_len + sizeof(*di) == item_size) {
196 /* No other xattrs packed in the same leaf item. */ 197 /* No other xattrs packed in the same leaf item. */
197 if (size > old_data_len) 198 if (size > old_data_len)
198 btrfs_extend_item(root, path, 199 btrfs_extend_item(fs_info, path,
199 size - old_data_len); 200 size - old_data_len);
200 else if (size < old_data_len) 201 else if (size < old_data_len)
201 btrfs_truncate_item(root, path, data_size, 1); 202 btrfs_truncate_item(fs_info, path,
203 data_size, 1);
202 } else { 204 } else {
203 /* There are other xattrs packed in the same item. */ 205 /* There are other xattrs packed in the same item. */
204 ret = btrfs_delete_one_dir_name(trans, root, path, di); 206 ret = btrfs_delete_one_dir_name(trans, root, path, di);
205 if (ret) 207 if (ret)
206 goto out; 208 goto out;
207 btrfs_extend_item(root, path, data_size); 209 btrfs_extend_item(fs_info, path, data_size);
208 } 210 }
209 211
210 item = btrfs_item_nr(slot); 212 item = btrfs_item_nr(slot);
@@ -265,6 +267,7 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
265{ 267{
266 struct btrfs_key key; 268 struct btrfs_key key;
267 struct inode *inode = d_inode(dentry); 269 struct inode *inode = d_inode(dentry);
270 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
268 struct btrfs_root *root = BTRFS_I(inode)->root; 271 struct btrfs_root *root = BTRFS_I(inode)->root;
269 struct btrfs_path *path; 272 struct btrfs_path *path;
270 int ret = 0; 273 int ret = 0;
@@ -333,7 +336,7 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
333 u32 this_len = sizeof(*di) + name_len + data_len; 336 u32 this_len = sizeof(*di) + name_len + data_len;
334 unsigned long name_ptr = (unsigned long)(di + 1); 337 unsigned long name_ptr = (unsigned long)(di + 1);
335 338
336 if (verify_dir_item(root, leaf, di)) { 339 if (verify_dir_item(fs_info, leaf, di)) {
337 ret = -EIO; 340 ret = -EIO;
338 goto err; 341 goto err;
339 } 342 }