aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeff Mahoney <jeffm@suse.com>2016-06-22 18:54:23 -0400
committerDavid Sterba <dsterba@suse.com>2016-12-06 10:06:59 -0500
commit0b246afa62b0cf5b09d078121f543135f28492ad (patch)
tree200ad296d09f1b2f5329658c8de81bc625007ace
parent6202df6921494f29308307e0ae6f567c2ab2ba19 (diff)
btrfs: root->fs_info cleanup, add fs_info convenience variables
In routines where someptr->fs_info is referenced multiple times, we introduce a convenience variable. This makes the code considerably more readable. Signed-off-by: Jeff Mahoney <jeffm@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
-rw-r--r--fs/btrfs/check-integrity.c63
-rw-r--r--fs/btrfs/compression.c28
-rw-r--r--fs/btrfs/ctree.c255
-rw-r--r--fs/btrfs/ctree.h14
-rw-r--r--fs/btrfs/delayed-inode.c37
-rw-r--r--fs/btrfs/dev-replace.c46
-rw-r--r--fs/btrfs/dir-item.c12
-rw-r--r--fs/btrfs/disk-io.c268
-rw-r--r--fs/btrfs/export.c10
-rw-r--r--fs/btrfs/extent-tree.c751
-rw-r--r--fs/btrfs/extent_io.c25
-rw-r--r--fs/btrfs/file-item.c114
-rw-r--r--fs/btrfs/file.c132
-rw-r--r--fs/btrfs/free-space-cache.c89
-rw-r--r--fs/btrfs/free-space-tree.c29
-rw-r--r--fs/btrfs/inode-item.c3
-rw-r--r--fs/btrfs/inode-map.c20
-rw-r--r--fs/btrfs/inode.c494
-rw-r--r--fs/btrfs/ioctl.c424
-rw-r--r--fs/btrfs/ordered-data.c34
-rw-r--r--fs/btrfs/print-tree.c8
-rw-r--r--fs/btrfs/qgroup.c42
-rw-r--r--fs/btrfs/raid56.c42
-rw-r--r--fs/btrfs/reada.c9
-rw-r--r--fs/btrfs/relocation.c128
-rw-r--r--fs/btrfs/root-tree.c15
-rw-r--r--fs/btrfs/scrub.c80
-rw-r--r--fs/btrfs/send.c17
-rw-r--r--fs/btrfs/super.c112
-rw-r--r--fs/btrfs/transaction.c331
-rw-r--r--fs/btrfs/tree-log.c117
-rw-r--r--fs/btrfs/uuid-tree.c7
-rw-r--r--fs/btrfs/volumes.c511
33 files changed, 2251 insertions, 2016 deletions
diff --git a/fs/btrfs/check-integrity.c b/fs/btrfs/check-integrity.c
index a6243ccd9800..137883cce13c 100644
--- a/fs/btrfs/check-integrity.c
+++ b/fs/btrfs/check-integrity.c
@@ -646,11 +646,12 @@ static struct btrfsic_dev_state *btrfsic_dev_state_hashtable_lookup(
646static int btrfsic_process_superblock(struct btrfsic_state *state, 646static int btrfsic_process_superblock(struct btrfsic_state *state,
647 struct btrfs_fs_devices *fs_devices) 647 struct btrfs_fs_devices *fs_devices)
648{ 648{
649 int ret = 0; 649 struct btrfs_fs_info *fs_info = state->fs_info;
650 struct btrfs_super_block *selected_super; 650 struct btrfs_super_block *selected_super;
651 struct list_head *dev_head = &fs_devices->devices; 651 struct list_head *dev_head = &fs_devices->devices;
652 struct btrfs_device *device; 652 struct btrfs_device *device;
653 struct btrfsic_dev_state *selected_dev_state = NULL; 653 struct btrfsic_dev_state *selected_dev_state = NULL;
654 int ret = 0;
654 int pass; 655 int pass;
655 656
656 BUG_ON(NULL == state); 657 BUG_ON(NULL == state);
@@ -716,9 +717,8 @@ static int btrfsic_process_superblock(struct btrfsic_state *state,
716 break; 717 break;
717 } 718 }
718 719
719 num_copies = 720 num_copies = btrfs_num_copies(fs_info, next_bytenr,
720 btrfs_num_copies(state->fs_info, 721 state->metablock_size);
721 next_bytenr, state->metablock_size);
722 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) 722 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
723 pr_info("num_copies(log_bytenr=%llu) = %d\n", 723 pr_info("num_copies(log_bytenr=%llu) = %d\n",
724 next_bytenr, num_copies); 724 next_bytenr, num_copies);
@@ -783,6 +783,7 @@ static int btrfsic_process_superblock_dev_mirror(
783 struct btrfsic_dev_state **selected_dev_state, 783 struct btrfsic_dev_state **selected_dev_state,
784 struct btrfs_super_block *selected_super) 784 struct btrfs_super_block *selected_super)
785{ 785{
786 struct btrfs_fs_info *fs_info = state->fs_info;
786 struct btrfs_super_block *super_tmp; 787 struct btrfs_super_block *super_tmp;
787 u64 dev_bytenr; 788 u64 dev_bytenr;
788 struct buffer_head *bh; 789 struct buffer_head *bh;
@@ -832,7 +833,7 @@ static int btrfsic_process_superblock_dev_mirror(
832 superblock_tmp->never_written = 0; 833 superblock_tmp->never_written = 0;
833 superblock_tmp->mirror_num = 1 + superblock_mirror_num; 834 superblock_tmp->mirror_num = 1 + superblock_mirror_num;
834 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE) 835 if (state->print_mask & BTRFSIC_PRINT_MASK_SUPERBLOCK_WRITE)
835 btrfs_info_in_rcu(device->fs_info, 836 btrfs_info_in_rcu(fs_info,
836 "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)", 837 "new initial S-block (bdev %p, %s) @%llu (%s/%llu/%d)",
837 superblock_bdev, 838 superblock_bdev,
838 rcu_str_deref(device->name), dev_bytenr, 839 rcu_str_deref(device->name), dev_bytenr,
@@ -887,9 +888,8 @@ static int btrfsic_process_superblock_dev_mirror(
887 break; 888 break;
888 } 889 }
889 890
890 num_copies = 891 num_copies = btrfs_num_copies(fs_info, next_bytenr,
891 btrfs_num_copies(state->fs_info, 892 state->metablock_size);
892 next_bytenr, state->metablock_size);
893 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) 893 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
894 pr_info("num_copies(log_bytenr=%llu) = %d\n", 894 pr_info("num_copies(log_bytenr=%llu) = %d\n",
895 next_bytenr, num_copies); 895 next_bytenr, num_copies);
@@ -1254,6 +1254,7 @@ static int btrfsic_create_link_to_next_block(
1254 struct btrfs_disk_key *disk_key, 1254 struct btrfs_disk_key *disk_key,
1255 u64 parent_generation) 1255 u64 parent_generation)
1256{ 1256{
1257 struct btrfs_fs_info *fs_info = state->fs_info;
1257 struct btrfsic_block *next_block = NULL; 1258 struct btrfsic_block *next_block = NULL;
1258 int ret; 1259 int ret;
1259 struct btrfsic_block_link *l; 1260 struct btrfsic_block_link *l;
@@ -1262,9 +1263,8 @@ static int btrfsic_create_link_to_next_block(
1262 1263
1263 *next_blockp = NULL; 1264 *next_blockp = NULL;
1264 if (0 == *num_copiesp) { 1265 if (0 == *num_copiesp) {
1265 *num_copiesp = 1266 *num_copiesp = btrfs_num_copies(fs_info, next_bytenr,
1266 btrfs_num_copies(state->fs_info, 1267 state->metablock_size);
1267 next_bytenr, state->metablock_size);
1268 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) 1268 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1269 pr_info("num_copies(log_bytenr=%llu) = %d\n", 1269 pr_info("num_copies(log_bytenr=%llu) = %d\n",
1270 next_bytenr, *num_copiesp); 1270 next_bytenr, *num_copiesp);
@@ -1390,13 +1390,14 @@ static int btrfsic_handle_extent_data(
1390 struct btrfsic_block_data_ctx *block_ctx, 1390 struct btrfsic_block_data_ctx *block_ctx,
1391 u32 item_offset, int force_iodone_flag) 1391 u32 item_offset, int force_iodone_flag)
1392{ 1392{
1393 int ret; 1393 struct btrfs_fs_info *fs_info = state->fs_info;
1394 struct btrfs_file_extent_item file_extent_item; 1394 struct btrfs_file_extent_item file_extent_item;
1395 u64 file_extent_item_offset; 1395 u64 file_extent_item_offset;
1396 u64 next_bytenr; 1396 u64 next_bytenr;
1397 u64 num_bytes; 1397 u64 num_bytes;
1398 u64 generation; 1398 u64 generation;
1399 struct btrfsic_block_link *l; 1399 struct btrfsic_block_link *l;
1400 int ret;
1400 1401
1401 file_extent_item_offset = offsetof(struct btrfs_leaf, items) + 1402 file_extent_item_offset = offsetof(struct btrfs_leaf, items) +
1402 item_offset; 1403 item_offset;
@@ -1456,9 +1457,8 @@ static int btrfsic_handle_extent_data(
1456 else 1457 else
1457 chunk_len = num_bytes; 1458 chunk_len = num_bytes;
1458 1459
1459 num_copies = 1460 num_copies = btrfs_num_copies(fs_info, next_bytenr,
1460 btrfs_num_copies(state->fs_info, 1461 state->datablock_size);
1461 next_bytenr, state->datablock_size);
1462 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) 1462 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
1463 pr_info("num_copies(log_bytenr=%llu) = %d\n", 1463 pr_info("num_copies(log_bytenr=%llu) = %d\n",
1464 next_bytenr, num_copies); 1464 next_bytenr, num_copies);
@@ -1533,13 +1533,14 @@ static int btrfsic_map_block(struct btrfsic_state *state, u64 bytenr, u32 len,
1533 struct btrfsic_block_data_ctx *block_ctx_out, 1533 struct btrfsic_block_data_ctx *block_ctx_out,
1534 int mirror_num) 1534 int mirror_num)
1535{ 1535{
1536 struct btrfs_fs_info *fs_info = state->fs_info;
1536 int ret; 1537 int ret;
1537 u64 length; 1538 u64 length;
1538 struct btrfs_bio *multi = NULL; 1539 struct btrfs_bio *multi = NULL;
1539 struct btrfs_device *device; 1540 struct btrfs_device *device;
1540 1541
1541 length = len; 1542 length = len;
1542 ret = btrfs_map_block(state->fs_info, BTRFS_MAP_READ, 1543 ret = btrfs_map_block(fs_info, BTRFS_MAP_READ,
1543 bytenr, &length, &multi, mirror_num); 1544 bytenr, &length, &multi, mirror_num);
1544 1545
1545 if (ret) { 1546 if (ret) {
@@ -1731,6 +1732,7 @@ static void btrfsic_dump_database(struct btrfsic_state *state)
1731static int btrfsic_test_for_metadata(struct btrfsic_state *state, 1732static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1732 char **datav, unsigned int num_pages) 1733 char **datav, unsigned int num_pages)
1733{ 1734{
1735 struct btrfs_fs_info *fs_info = state->fs_info;
1734 struct btrfs_header *h; 1736 struct btrfs_header *h;
1735 u8 csum[BTRFS_CSUM_SIZE]; 1737 u8 csum[BTRFS_CSUM_SIZE];
1736 u32 crc = ~(u32)0; 1738 u32 crc = ~(u32)0;
@@ -1741,7 +1743,7 @@ static int btrfsic_test_for_metadata(struct btrfsic_state *state,
1741 num_pages = state->metablock_size >> PAGE_SHIFT; 1743 num_pages = state->metablock_size >> PAGE_SHIFT;
1742 h = (struct btrfs_header *)datav[0]; 1744 h = (struct btrfs_header *)datav[0];
1743 1745
1744 if (memcmp(h->fsid, state->fs_info->fsid, BTRFS_UUID_SIZE)) 1746 if (memcmp(h->fsid, fs_info->fsid, BTRFS_UUID_SIZE))
1745 return 1; 1747 return 1;
1746 1748
1747 for (i = 0; i < num_pages; i++) { 1749 for (i = 0; i < num_pages; i++) {
@@ -2202,6 +2204,7 @@ static int btrfsic_process_written_superblock(
2202 struct btrfsic_block *const superblock, 2204 struct btrfsic_block *const superblock,
2203 struct btrfs_super_block *const super_hdr) 2205 struct btrfs_super_block *const super_hdr)
2204{ 2206{
2207 struct btrfs_fs_info *fs_info = state->fs_info;
2205 int pass; 2208 int pass;
2206 2209
2207 superblock->generation = btrfs_super_generation(super_hdr); 2210 superblock->generation = btrfs_super_generation(super_hdr);
@@ -2275,9 +2278,8 @@ static int btrfsic_process_written_superblock(
2275 break; 2278 break;
2276 } 2279 }
2277 2280
2278 num_copies = 2281 num_copies = btrfs_num_copies(fs_info, next_bytenr,
2279 btrfs_num_copies(state->fs_info, 2282 BTRFS_SUPER_INFO_SIZE);
2280 next_bytenr, BTRFS_SUPER_INFO_SIZE);
2281 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES) 2283 if (state->print_mask & BTRFSIC_PRINT_MASK_NUM_COPIES)
2282 pr_info("num_copies(log_bytenr=%llu) = %d\n", 2284 pr_info("num_copies(log_bytenr=%llu) = %d\n",
2283 next_bytenr, num_copies); 2285 next_bytenr, num_copies);
@@ -2699,14 +2701,14 @@ static void btrfsic_cmp_log_and_dev_bytenr(struct btrfsic_state *state,
2699 struct btrfsic_dev_state *dev_state, 2701 struct btrfsic_dev_state *dev_state,
2700 u64 dev_bytenr) 2702 u64 dev_bytenr)
2701{ 2703{
2704 struct btrfs_fs_info *fs_info = state->fs_info;
2705 struct btrfsic_block_data_ctx block_ctx;
2702 int num_copies; 2706 int num_copies;
2703 int mirror_num; 2707 int mirror_num;
2704 int ret;
2705 struct btrfsic_block_data_ctx block_ctx;
2706 int match = 0; 2708 int match = 0;
2709 int ret;
2707 2710
2708 num_copies = btrfs_num_copies(state->fs_info, 2711 num_copies = btrfs_num_copies(fs_info, bytenr, state->metablock_size);
2709 bytenr, state->metablock_size);
2710 2712
2711 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) { 2713 for (mirror_num = 1; mirror_num <= num_copies; mirror_num++) {
2712 ret = btrfsic_map_block(state, bytenr, state->metablock_size, 2714 ret = btrfsic_map_block(state, bytenr, state->metablock_size,
@@ -2909,16 +2911,17 @@ int btrfsic_mount(struct btrfs_root *root,
2909 int ret; 2911 int ret;
2910 struct btrfsic_state *state; 2912 struct btrfsic_state *state;
2911 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;
2912 struct btrfs_device *device; 2915 struct btrfs_device *device;
2913 2916
2914 if (root->fs_info->nodesize & ((u64)PAGE_SIZE - 1)) { 2917 if (fs_info->nodesize & ((u64)PAGE_SIZE - 1)) {
2915 pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n", 2918 pr_info("btrfsic: cannot handle nodesize %d not being a multiple of PAGE_SIZE %ld!\n",
2916 root->fs_info->nodesize, PAGE_SIZE); 2919 fs_info->nodesize, PAGE_SIZE);
2917 return -1; 2920 return -1;
2918 } 2921 }
2919 if (root->fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) { 2922 if (fs_info->sectorsize & ((u64)PAGE_SIZE - 1)) {
2920 pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n", 2923 pr_info("btrfsic: cannot handle sectorsize %d not being a multiple of PAGE_SIZE %ld!\n",
2921 root->fs_info->sectorsize, PAGE_SIZE); 2924 fs_info->sectorsize, PAGE_SIZE);
2922 return -1; 2925 return -1;
2923 } 2926 }
2924 state = kzalloc(sizeof(*state), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); 2927 state = kzalloc(sizeof(*state), GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
@@ -2940,8 +2943,8 @@ int btrfsic_mount(struct btrfs_root *root,
2940 state->print_mask = print_mask; 2943 state->print_mask = print_mask;
2941 state->include_extent_data = including_extent_data; 2944 state->include_extent_data = including_extent_data;
2942 state->csum_size = 0; 2945 state->csum_size = 0;
2943 state->metablock_size = root->fs_info->nodesize; 2946 state->metablock_size = fs_info->nodesize;
2944 state->datablock_size = root->fs_info->sectorsize; 2947 state->datablock_size = fs_info->sectorsize;
2945 INIT_LIST_HEAD(&state->all_blocks_list); 2948 INIT_LIST_HEAD(&state->all_blocks_list);
2946 btrfsic_block_hashtable_init(&state->block_hashtable); 2949 btrfsic_block_hashtable_init(&state->block_hashtable);
2947 btrfsic_block_link_hashtable_init(&state->block_link_hashtable); 2950 btrfsic_block_link_hashtable_init(&state->block_link_hashtable);
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index e4a8c3a085db..750bae4a46da 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -88,10 +88,11 @@ static int btrfs_decompress_bio(int type, struct page **pages_in,
88static inline int compressed_bio_size(struct btrfs_root *root, 88static inline int compressed_bio_size(struct btrfs_root *root,
89 unsigned long disk_size) 89 unsigned long disk_size)
90{ 90{
91 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 91 struct btrfs_fs_info *fs_info = root->fs_info;
92 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
92 93
93 return sizeof(struct compressed_bio) + 94 return sizeof(struct compressed_bio) +
94 (DIV_ROUND_UP(disk_size, root->fs_info->sectorsize)) * csum_size; 95 (DIV_ROUND_UP(disk_size, fs_info->sectorsize)) * csum_size;
95} 96}
96 97
97static struct bio *compressed_bio_alloc(struct block_device *bdev, 98static struct bio *compressed_bio_alloc(struct block_device *bdev,
@@ -328,6 +329,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
328 struct page **compressed_pages, 329 struct page **compressed_pages,
329 unsigned long nr_pages) 330 unsigned long nr_pages)
330{ 331{
332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
331 struct bio *bio = NULL; 333 struct bio *bio = NULL;
332 struct btrfs_root *root = BTRFS_I(inode)->root; 334 struct btrfs_root *root = BTRFS_I(inode)->root;
333 struct compressed_bio *cb; 335 struct compressed_bio *cb;
@@ -355,7 +357,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
355 cb->orig_bio = NULL; 357 cb->orig_bio = NULL;
356 cb->nr_pages = nr_pages; 358 cb->nr_pages = nr_pages;
357 359
358 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 360 bdev = fs_info->fs_devices->latest_bdev;
359 361
360 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS); 362 bio = compressed_bio_alloc(bdev, first_byte, GFP_NOFS);
361 if (!bio) { 363 if (!bio) {
@@ -391,8 +393,8 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
391 * freed before we're done setting it up 393 * freed before we're done setting it up
392 */ 394 */
393 atomic_inc(&cb->pending_bios); 395 atomic_inc(&cb->pending_bios);
394 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 396 ret = btrfs_bio_wq_end_io(fs_info, bio,
395 BTRFS_WQ_ENDIO_DATA); 397 BTRFS_WQ_ENDIO_DATA);
396 BUG_ON(ret); /* -ENOMEM */ 398 BUG_ON(ret); /* -ENOMEM */
397 399
398 if (!skip_sum) { 400 if (!skip_sum) {
@@ -417,7 +419,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
417 bio_add_page(bio, page, PAGE_SIZE, 0); 419 bio_add_page(bio, page, PAGE_SIZE, 0);
418 } 420 }
419 if (bytes_left < PAGE_SIZE) { 421 if (bytes_left < PAGE_SIZE) {
420 btrfs_info(BTRFS_I(inode)->root->fs_info, 422 btrfs_info(fs_info,
421 "bytes left %lu compress len %lu nr %lu", 423 "bytes left %lu compress len %lu nr %lu",
422 bytes_left, cb->compressed_len, cb->nr_pages); 424 bytes_left, cb->compressed_len, cb->nr_pages);
423 } 425 }
@@ -427,7 +429,7 @@ int btrfs_submit_compressed_write(struct inode *inode, u64 start,
427 } 429 }
428 bio_get(bio); 430 bio_get(bio);
429 431
430 ret = btrfs_bio_wq_end_io(root->fs_info, bio, BTRFS_WQ_ENDIO_DATA); 432 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
431 BUG_ON(ret); /* -ENOMEM */ 433 BUG_ON(ret); /* -ENOMEM */
432 434
433 if (!skip_sum) { 435 if (!skip_sum) {
@@ -575,6 +577,7 @@ next:
575int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, 577int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
576 int mirror_num, unsigned long bio_flags) 578 int mirror_num, unsigned long bio_flags)
577{ 579{
580 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
578 struct extent_io_tree *tree; 581 struct extent_io_tree *tree;
579 struct extent_map_tree *em_tree; 582 struct extent_map_tree *em_tree;
580 struct compressed_bio *cb; 583 struct compressed_bio *cb;
@@ -634,7 +637,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
634 if (!cb->compressed_pages) 637 if (!cb->compressed_pages)
635 goto fail1; 638 goto fail1;
636 639
637 bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 640 bdev = fs_info->fs_devices->latest_bdev;
638 641
639 for (pg_index = 0; pg_index < nr_pages; pg_index++) { 642 for (pg_index = 0; pg_index < nr_pages; pg_index++) {
640 cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS | 643 cb->compressed_pages[pg_index] = alloc_page(GFP_NOFS |
@@ -678,8 +681,8 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
678 PAGE_SIZE) { 681 PAGE_SIZE) {
679 bio_get(comp_bio); 682 bio_get(comp_bio);
680 683
681 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 684 ret = btrfs_bio_wq_end_io(fs_info, comp_bio,
682 BTRFS_WQ_ENDIO_DATA); 685 BTRFS_WQ_ENDIO_DATA);
683 BUG_ON(ret); /* -ENOMEM */ 686 BUG_ON(ret); /* -ENOMEM */
684 687
685 /* 688 /*
@@ -696,7 +699,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
696 BUG_ON(ret); /* -ENOMEM */ 699 BUG_ON(ret); /* -ENOMEM */
697 } 700 }
698 sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size, 701 sums += DIV_ROUND_UP(comp_bio->bi_iter.bi_size,
699 root->fs_info->sectorsize); 702 fs_info->sectorsize);
700 703
701 ret = btrfs_map_bio(root, comp_bio, mirror_num, 0); 704 ret = btrfs_map_bio(root, comp_bio, mirror_num, 0);
702 if (ret) { 705 if (ret) {
@@ -719,8 +722,7 @@ int btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,
719 } 722 }
720 bio_get(comp_bio); 723 bio_get(comp_bio);
721 724
722 ret = btrfs_bio_wq_end_io(root->fs_info, comp_bio, 725 ret = btrfs_bio_wq_end_io(fs_info, comp_bio, BTRFS_WQ_ENDIO_DATA);
723 BTRFS_WQ_ENDIO_DATA);
724 BUG_ON(ret); /* -ENOMEM */ 726 BUG_ON(ret); /* -ENOMEM */
725 727
726 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { 728 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 431b150a823a..b29c8d82e741 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -212,21 +212,23 @@ static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
212 */ 212 */
213static void add_root_to_dirty_list(struct btrfs_root *root) 213static void add_root_to_dirty_list(struct btrfs_root *root)
214{ 214{
215 struct btrfs_fs_info *fs_info = root->fs_info;
216
215 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || 217 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
216 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) 218 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
217 return; 219 return;
218 220
219 spin_lock(&root->fs_info->trans_lock); 221 spin_lock(&fs_info->trans_lock);
220 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { 222 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
221 /* Want the extent tree to be the last on the list */ 223 /* Want the extent tree to be the last on the list */
222 if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID) 224 if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
223 list_move_tail(&root->dirty_list, 225 list_move_tail(&root->dirty_list,
224 &root->fs_info->dirty_cowonly_roots); 226 &fs_info->dirty_cowonly_roots);
225 else 227 else
226 list_move(&root->dirty_list, 228 list_move(&root->dirty_list,
227 &root->fs_info->dirty_cowonly_roots); 229 &fs_info->dirty_cowonly_roots);
228 } 230 }
229 spin_unlock(&root->fs_info->trans_lock); 231 spin_unlock(&fs_info->trans_lock);
230} 232}
231 233
232/* 234/*
@@ -239,13 +241,14 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
239 struct extent_buffer *buf, 241 struct extent_buffer *buf,
240 struct extent_buffer **cow_ret, u64 new_root_objectid) 242 struct extent_buffer **cow_ret, u64 new_root_objectid)
241{ 243{
244 struct btrfs_fs_info *fs_info = root->fs_info;
242 struct extent_buffer *cow; 245 struct extent_buffer *cow;
243 int ret = 0; 246 int ret = 0;
244 int level; 247 int level;
245 struct btrfs_disk_key disk_key; 248 struct btrfs_disk_key disk_key;
246 249
247 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 250 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
248 trans->transid != root->fs_info->running_transaction->transid); 251 trans->transid != fs_info->running_transaction->transid);
249 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 252 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
250 trans->transid != root->last_trans); 253 trans->transid != root->last_trans);
251 254
@@ -271,7 +274,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
271 else 274 else
272 btrfs_set_header_owner(cow, new_root_objectid); 275 btrfs_set_header_owner(cow, new_root_objectid);
273 276
274 write_extent_buffer_fsid(cow, root->fs_info->fsid); 277 write_extent_buffer_fsid(cow, fs_info->fsid);
275 278
276 WARN_ON(btrfs_header_generation(buf) > trans->transid); 279 WARN_ON(btrfs_header_generation(buf) > trans->transid);
277 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) 280 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
@@ -977,6 +980,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
977 struct extent_buffer *cow, 980 struct extent_buffer *cow,
978 int *last_ref) 981 int *last_ref)
979{ 982{
983 struct btrfs_fs_info *fs_info = root->fs_info;
980 u64 refs; 984 u64 refs;
981 u64 owner; 985 u64 owner;
982 u64 flags; 986 u64 flags;
@@ -1008,7 +1012,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
1008 return ret; 1012 return ret;
1009 if (refs == 0) { 1013 if (refs == 0) {
1010 ret = -EROFS; 1014 ret = -EROFS;
1011 btrfs_handle_fs_error(root->fs_info, ret, NULL); 1015 btrfs_handle_fs_error(fs_info, ret, NULL);
1012 return ret; 1016 return ret;
1013 } 1017 }
1014 } else { 1018 } else {
@@ -1069,7 +1073,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
1069 ret = btrfs_dec_ref(trans, root, buf, 1); 1073 ret = btrfs_dec_ref(trans, root, buf, 1);
1070 BUG_ON(ret); /* -ENOMEM */ 1074 BUG_ON(ret); /* -ENOMEM */
1071 } 1075 }
1072 clean_tree_block(trans, root->fs_info, buf); 1076 clean_tree_block(trans, fs_info, buf);
1073 *last_ref = 1; 1077 *last_ref = 1;
1074 } 1078 }
1075 return 0; 1079 return 0;
@@ -1094,6 +1098,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1094 struct extent_buffer **cow_ret, 1098 struct extent_buffer **cow_ret,
1095 u64 search_start, u64 empty_size) 1099 u64 search_start, u64 empty_size)
1096{ 1100{
1101 struct btrfs_fs_info *fs_info = root->fs_info;
1097 struct btrfs_disk_key disk_key; 1102 struct btrfs_disk_key disk_key;
1098 struct extent_buffer *cow; 1103 struct extent_buffer *cow;
1099 int level, ret; 1104 int level, ret;
@@ -1107,7 +1112,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1107 btrfs_assert_tree_locked(buf); 1112 btrfs_assert_tree_locked(buf);
1108 1113
1109 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 1114 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1110 trans->transid != root->fs_info->running_transaction->transid); 1115 trans->transid != fs_info->running_transaction->transid);
1111 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 1116 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1112 trans->transid != root->last_trans); 1117 trans->transid != root->last_trans);
1113 1118
@@ -1140,7 +1145,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1140 else 1145 else
1141 btrfs_set_header_owner(cow, root->root_key.objectid); 1146 btrfs_set_header_owner(cow, root->root_key.objectid);
1142 1147
1143 write_extent_buffer_fsid(cow, root->fs_info->fsid); 1148 write_extent_buffer_fsid(cow, fs_info->fsid);
1144 1149
1145 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); 1150 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
1146 if (ret) { 1151 if (ret) {
@@ -1172,7 +1177,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1172 add_root_to_dirty_list(root); 1177 add_root_to_dirty_list(root);
1173 } else { 1178 } else {
1174 WARN_ON(trans->transid != btrfs_header_generation(parent)); 1179 WARN_ON(trans->transid != btrfs_header_generation(parent));
1175 tree_mod_log_insert_key(root->fs_info, parent, parent_slot, 1180 tree_mod_log_insert_key(fs_info, parent, parent_slot,
1176 MOD_LOG_KEY_REPLACE, GFP_NOFS); 1181 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1177 btrfs_set_node_blockptr(parent, parent_slot, 1182 btrfs_set_node_blockptr(parent, parent_slot,
1178 cow->start); 1183 cow->start);
@@ -1180,7 +1185,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
1180 trans->transid); 1185 trans->transid);
1181 btrfs_mark_buffer_dirty(parent); 1186 btrfs_mark_buffer_dirty(parent);
1182 if (last_ref) { 1187 if (last_ref) {
1183 ret = tree_mod_log_free_eb(root->fs_info, buf); 1188 ret = tree_mod_log_free_eb(fs_info, buf);
1184 if (ret) { 1189 if (ret) {
1185 btrfs_abort_transaction(trans, ret); 1190 btrfs_abort_transaction(trans, ret);
1186 return ret; 1191 return ret;
@@ -1400,6 +1405,7 @@ tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1400static inline struct extent_buffer * 1405static inline struct extent_buffer *
1401get_old_root(struct btrfs_root *root, u64 time_seq) 1406get_old_root(struct btrfs_root *root, u64 time_seq)
1402{ 1407{
1408 struct btrfs_fs_info *fs_info = root->fs_info;
1403 struct tree_mod_elem *tm; 1409 struct tree_mod_elem *tm;
1404 struct extent_buffer *eb = NULL; 1410 struct extent_buffer *eb = NULL;
1405 struct extent_buffer *eb_root; 1411 struct extent_buffer *eb_root;
@@ -1409,7 +1415,7 @@ get_old_root(struct btrfs_root *root, u64 time_seq)
1409 u64 logical; 1415 u64 logical;
1410 1416
1411 eb_root = btrfs_read_lock_root_node(root); 1417 eb_root = btrfs_read_lock_root_node(root);
1412 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq); 1418 tm = __tree_mod_log_oldest_root(fs_info, eb_root, time_seq);
1413 if (!tm) 1419 if (!tm)
1414 return eb_root; 1420 return eb_root;
1415 1421
@@ -1421,7 +1427,7 @@ get_old_root(struct btrfs_root *root, u64 time_seq)
1421 logical = eb_root->start; 1427 logical = eb_root->start;
1422 } 1428 }
1423 1429
1424 tm = tree_mod_log_search(root->fs_info, logical, time_seq); 1430 tm = tree_mod_log_search(fs_info, logical, time_seq);
1425 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { 1431 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1426 btrfs_tree_read_unlock(eb_root); 1432 btrfs_tree_read_unlock(eb_root);
1427 free_extent_buffer(eb_root); 1433 free_extent_buffer(eb_root);
@@ -1429,8 +1435,9 @@ get_old_root(struct btrfs_root *root, u64 time_seq)
1429 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { 1435 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1430 if (!IS_ERR(old)) 1436 if (!IS_ERR(old))
1431 free_extent_buffer(old); 1437 free_extent_buffer(old);
1432 btrfs_warn(root->fs_info, 1438 btrfs_warn(fs_info,
1433 "failed to read tree block %llu from get_old_root", logical); 1439 "failed to read tree block %llu from get_old_root",
1440 logical);
1434 } else { 1441 } else {
1435 eb = btrfs_clone_extent_buffer(old); 1442 eb = btrfs_clone_extent_buffer(old);
1436 free_extent_buffer(old); 1443 free_extent_buffer(old);
@@ -1438,7 +1445,7 @@ get_old_root(struct btrfs_root *root, u64 time_seq)
1438 } else if (old_root) { 1445 } else if (old_root) {
1439 btrfs_tree_read_unlock(eb_root); 1446 btrfs_tree_read_unlock(eb_root);
1440 free_extent_buffer(eb_root); 1447 free_extent_buffer(eb_root);
1441 eb = alloc_dummy_extent_buffer(root->fs_info, logical); 1448 eb = alloc_dummy_extent_buffer(fs_info, logical);
1442 } else { 1449 } else {
1443 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK); 1450 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
1444 eb = btrfs_clone_extent_buffer(eb_root); 1451 eb = btrfs_clone_extent_buffer(eb_root);
@@ -1458,10 +1465,10 @@ get_old_root(struct btrfs_root *root, u64 time_seq)
1458 btrfs_set_header_generation(eb, old_generation); 1465 btrfs_set_header_generation(eb, old_generation);
1459 } 1466 }
1460 if (tm) 1467 if (tm)
1461 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm); 1468 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
1462 else 1469 else
1463 WARN_ON(btrfs_header_level(eb) != 0); 1470 WARN_ON(btrfs_header_level(eb) != 0);
1464 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)); 1471 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1465 1472
1466 return eb; 1473 return eb;
1467} 1474}
@@ -1523,17 +1530,18 @@ noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
1523 struct extent_buffer *parent, int parent_slot, 1530 struct extent_buffer *parent, int parent_slot,
1524 struct extent_buffer **cow_ret) 1531 struct extent_buffer **cow_ret)
1525{ 1532{
1533 struct btrfs_fs_info *fs_info = root->fs_info;
1526 u64 search_start; 1534 u64 search_start;
1527 int ret; 1535 int ret;
1528 1536
1529 if (trans->transaction != root->fs_info->running_transaction) 1537 if (trans->transaction != fs_info->running_transaction)
1530 WARN(1, KERN_CRIT "trans %llu running %llu\n", 1538 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1531 trans->transid, 1539 trans->transid,
1532 root->fs_info->running_transaction->transid); 1540 fs_info->running_transaction->transid);
1533 1541
1534 if (trans->transid != root->fs_info->generation) 1542 if (trans->transid != fs_info->generation)
1535 WARN(1, KERN_CRIT "trans %llu running %llu\n", 1543 WARN(1, KERN_CRIT "trans %llu running %llu\n",
1536 trans->transid, root->fs_info->generation); 1544 trans->transid, fs_info->generation);
1537 1545
1538 if (!should_cow_block(trans, root, buf)) { 1546 if (!should_cow_block(trans, root, buf)) {
1539 trans->dirty = true; 1547 trans->dirty = true;
@@ -1610,6 +1618,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1610 int start_slot, u64 *last_ret, 1618 int start_slot, u64 *last_ret,
1611 struct btrfs_key *progress) 1619 struct btrfs_key *progress)
1612{ 1620{
1621 struct btrfs_fs_info *fs_info = root->fs_info;
1613 struct extent_buffer *cur; 1622 struct extent_buffer *cur;
1614 u64 blocknr; 1623 u64 blocknr;
1615 u64 gen; 1624 u64 gen;
@@ -1628,11 +1637,11 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1628 1637
1629 parent_level = btrfs_header_level(parent); 1638 parent_level = btrfs_header_level(parent);
1630 1639
1631 WARN_ON(trans->transaction != root->fs_info->running_transaction); 1640 WARN_ON(trans->transaction != fs_info->running_transaction);
1632 WARN_ON(trans->transid != root->fs_info->generation); 1641 WARN_ON(trans->transid != fs_info->generation);
1633 1642
1634 parent_nritems = btrfs_header_nritems(parent); 1643 parent_nritems = btrfs_header_nritems(parent);
1635 blocksize = root->fs_info->nodesize; 1644 blocksize = fs_info->nodesize;
1636 end_slot = parent_nritems - 1; 1645 end_slot = parent_nritems - 1;
1637 1646
1638 if (parent_nritems <= 1) 1647 if (parent_nritems <= 1)
@@ -1666,7 +1675,7 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1666 continue; 1675 continue;
1667 } 1676 }
1668 1677
1669 cur = find_extent_buffer(root->fs_info, blocknr); 1678 cur = find_extent_buffer(fs_info, blocknr);
1670 if (cur) 1679 if (cur)
1671 uptodate = btrfs_buffer_uptodate(cur, gen, 0); 1680 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
1672 else 1681 else
@@ -1711,7 +1720,6 @@ int btrfs_realloc_node(struct btrfs_trans_handle *trans,
1711 return err; 1720 return err;
1712} 1721}
1713 1722
1714
1715/* 1723/*
1716 * search for key in the extent_buffer. The items start at offset p, 1724 * search for key in the extent_buffer. The items start at offset p,
1717 * and they are item_size apart. There are 'max' items in p. 1725 * and they are item_size apart. There are 'max' items in p.
@@ -1865,6 +1873,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1865 struct btrfs_root *root, 1873 struct btrfs_root *root,
1866 struct btrfs_path *path, int level) 1874 struct btrfs_path *path, int level)
1867{ 1875{
1876 struct btrfs_fs_info *fs_info = root->fs_info;
1868 struct extent_buffer *right = NULL; 1877 struct extent_buffer *right = NULL;
1869 struct extent_buffer *mid; 1878 struct extent_buffer *mid;
1870 struct extent_buffer *left = NULL; 1879 struct extent_buffer *left = NULL;
@@ -1905,7 +1914,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1905 child = read_node_slot(root, mid, 0); 1914 child = read_node_slot(root, mid, 0);
1906 if (IS_ERR(child)) { 1915 if (IS_ERR(child)) {
1907 ret = PTR_ERR(child); 1916 ret = PTR_ERR(child);
1908 btrfs_handle_fs_error(root->fs_info, ret, NULL); 1917 btrfs_handle_fs_error(fs_info, ret, NULL);
1909 goto enospc; 1918 goto enospc;
1910 } 1919 }
1911 1920
@@ -1926,7 +1935,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1926 1935
1927 path->locks[level] = 0; 1936 path->locks[level] = 0;
1928 path->nodes[level] = NULL; 1937 path->nodes[level] = NULL;
1929 clean_tree_block(trans, root->fs_info, mid); 1938 clean_tree_block(trans, fs_info, mid);
1930 btrfs_tree_unlock(mid); 1939 btrfs_tree_unlock(mid);
1931 /* once for the path */ 1940 /* once for the path */
1932 free_extent_buffer(mid); 1941 free_extent_buffer(mid);
@@ -1938,7 +1947,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1938 return 0; 1947 return 0;
1939 } 1948 }
1940 if (btrfs_header_nritems(mid) > 1949 if (btrfs_header_nritems(mid) >
1941 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) / 4) 1950 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
1942 return 0; 1951 return 0;
1943 1952
1944 left = read_node_slot(root, parent, pslot - 1); 1953 left = read_node_slot(root, parent, pslot - 1);
@@ -1987,7 +1996,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1987 if (wret < 0 && wret != -ENOSPC) 1996 if (wret < 0 && wret != -ENOSPC)
1988 ret = wret; 1997 ret = wret;
1989 if (btrfs_header_nritems(right) == 0) { 1998 if (btrfs_header_nritems(right) == 0) {
1990 clean_tree_block(trans, root->fs_info, right); 1999 clean_tree_block(trans, fs_info, right);
1991 btrfs_tree_unlock(right); 2000 btrfs_tree_unlock(right);
1992 del_ptr(root, path, level + 1, pslot + 1); 2001 del_ptr(root, path, level + 1, pslot + 1);
1993 root_sub_used(root, right->len); 2002 root_sub_used(root, right->len);
@@ -1997,7 +2006,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1997 } else { 2006 } else {
1998 struct btrfs_disk_key right_key; 2007 struct btrfs_disk_key right_key;
1999 btrfs_node_key(right, &right_key, 0); 2008 btrfs_node_key(right, &right_key, 0);
2000 tree_mod_log_set_node_key(root->fs_info, parent, 2009 tree_mod_log_set_node_key(fs_info, parent,
2001 pslot + 1, 0); 2010 pslot + 1, 0);
2002 btrfs_set_node_key(parent, &right_key, pslot + 1); 2011 btrfs_set_node_key(parent, &right_key, pslot + 1);
2003 btrfs_mark_buffer_dirty(parent); 2012 btrfs_mark_buffer_dirty(parent);
@@ -2015,7 +2024,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
2015 */ 2024 */
2016 if (!left) { 2025 if (!left) {
2017 ret = -EROFS; 2026 ret = -EROFS;
2018 btrfs_handle_fs_error(root->fs_info, ret, NULL); 2027 btrfs_handle_fs_error(fs_info, ret, NULL);
2019 goto enospc; 2028 goto enospc;
2020 } 2029 }
2021 wret = balance_node_right(trans, root, mid, left); 2030 wret = balance_node_right(trans, root, mid, left);
@@ -2031,7 +2040,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
2031 BUG_ON(wret == 1); 2040 BUG_ON(wret == 1);
2032 } 2041 }
2033 if (btrfs_header_nritems(mid) == 0) { 2042 if (btrfs_header_nritems(mid) == 0) {
2034 clean_tree_block(trans, root->fs_info, mid); 2043 clean_tree_block(trans, fs_info, mid);
2035 btrfs_tree_unlock(mid); 2044 btrfs_tree_unlock(mid);
2036 del_ptr(root, path, level + 1, pslot); 2045 del_ptr(root, path, level + 1, pslot);
2037 root_sub_used(root, mid->len); 2046 root_sub_used(root, mid->len);
@@ -2042,8 +2051,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
2042 /* update the parent key to reflect our changes */ 2051 /* update the parent key to reflect our changes */
2043 struct btrfs_disk_key mid_key; 2052 struct btrfs_disk_key mid_key;
2044 btrfs_node_key(mid, &mid_key, 0); 2053 btrfs_node_key(mid, &mid_key, 0);
2045 tree_mod_log_set_node_key(root->fs_info, parent, 2054 tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
2046 pslot, 0);
2047 btrfs_set_node_key(parent, &mid_key, pslot); 2055 btrfs_set_node_key(parent, &mid_key, pslot);
2048 btrfs_mark_buffer_dirty(parent); 2056 btrfs_mark_buffer_dirty(parent);
2049 } 2057 }
@@ -2090,6 +2098,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2090 struct btrfs_root *root, 2098 struct btrfs_root *root,
2091 struct btrfs_path *path, int level) 2099 struct btrfs_path *path, int level)
2092{ 2100{
2101 struct btrfs_fs_info *fs_info = root->fs_info;
2093 struct extent_buffer *right = NULL; 2102 struct extent_buffer *right = NULL;
2094 struct extent_buffer *mid; 2103 struct extent_buffer *mid;
2095 struct extent_buffer *left = NULL; 2104 struct extent_buffer *left = NULL;
@@ -2125,7 +2134,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2125 btrfs_set_lock_blocking(left); 2134 btrfs_set_lock_blocking(left);
2126 2135
2127 left_nr = btrfs_header_nritems(left); 2136 left_nr = btrfs_header_nritems(left);
2128 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) { 2137 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2129 wret = 1; 2138 wret = 1;
2130 } else { 2139 } else {
2131 ret = btrfs_cow_block(trans, root, left, parent, 2140 ret = btrfs_cow_block(trans, root, left, parent,
@@ -2143,8 +2152,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2143 struct btrfs_disk_key disk_key; 2152 struct btrfs_disk_key disk_key;
2144 orig_slot += left_nr; 2153 orig_slot += left_nr;
2145 btrfs_node_key(mid, &disk_key, 0); 2154 btrfs_node_key(mid, &disk_key, 0);
2146 tree_mod_log_set_node_key(root->fs_info, parent, 2155 tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
2147 pslot, 0);
2148 btrfs_set_node_key(parent, &disk_key, pslot); 2156 btrfs_set_node_key(parent, &disk_key, pslot);
2149 btrfs_mark_buffer_dirty(parent); 2157 btrfs_mark_buffer_dirty(parent);
2150 if (btrfs_header_nritems(left) > orig_slot) { 2158 if (btrfs_header_nritems(left) > orig_slot) {
@@ -2179,7 +2187,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2179 btrfs_set_lock_blocking(right); 2187 btrfs_set_lock_blocking(right);
2180 2188
2181 right_nr = btrfs_header_nritems(right); 2189 right_nr = btrfs_header_nritems(right);
2182 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 1) { 2190 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
2183 wret = 1; 2191 wret = 1;
2184 } else { 2192 } else {
2185 ret = btrfs_cow_block(trans, root, right, 2193 ret = btrfs_cow_block(trans, root, right,
@@ -2198,7 +2206,7 @@ static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
2198 struct btrfs_disk_key disk_key; 2206 struct btrfs_disk_key disk_key;
2199 2207
2200 btrfs_node_key(right, &disk_key, 0); 2208 btrfs_node_key(right, &disk_key, 0);
2201 tree_mod_log_set_node_key(root->fs_info, parent, 2209 tree_mod_log_set_node_key(fs_info, parent,
2202 pslot + 1, 0); 2210 pslot + 1, 0);
2203 btrfs_set_node_key(parent, &disk_key, pslot + 1); 2211 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2204 btrfs_mark_buffer_dirty(parent); 2212 btrfs_mark_buffer_dirty(parent);
@@ -2230,6 +2238,7 @@ static void reada_for_search(struct btrfs_root *root,
2230 struct btrfs_path *path, 2238 struct btrfs_path *path,
2231 int level, int slot, u64 objectid) 2239 int level, int slot, u64 objectid)
2232{ 2240{
2241 struct btrfs_fs_info *fs_info = root->fs_info;
2233 struct extent_buffer *node; 2242 struct extent_buffer *node;
2234 struct btrfs_disk_key disk_key; 2243 struct btrfs_disk_key disk_key;
2235 u32 nritems; 2244 u32 nritems;
@@ -2250,8 +2259,8 @@ static void reada_for_search(struct btrfs_root *root,
2250 node = path->nodes[level]; 2259 node = path->nodes[level];
2251 2260
2252 search = btrfs_node_blockptr(node, slot); 2261 search = btrfs_node_blockptr(node, slot);
2253 blocksize = root->fs_info->nodesize; 2262 blocksize = fs_info->nodesize;
2254 eb = find_extent_buffer(root->fs_info, search); 2263 eb = find_extent_buffer(fs_info, search);
2255 if (eb) { 2264 if (eb) {
2256 free_extent_buffer(eb); 2265 free_extent_buffer(eb);
2257 return; 2266 return;
@@ -2292,6 +2301,7 @@ static void reada_for_search(struct btrfs_root *root,
2292static noinline void reada_for_balance(struct btrfs_root *root, 2301static noinline void reada_for_balance(struct btrfs_root *root,
2293 struct btrfs_path *path, int level) 2302 struct btrfs_path *path, int level)
2294{ 2303{
2304 struct btrfs_fs_info *fs_info = root->fs_info;
2295 int slot; 2305 int slot;
2296 int nritems; 2306 int nritems;
2297 struct extent_buffer *parent; 2307 struct extent_buffer *parent;
@@ -2310,7 +2320,7 @@ static noinline void reada_for_balance(struct btrfs_root *root,
2310 if (slot > 0) { 2320 if (slot > 0) {
2311 block1 = btrfs_node_blockptr(parent, slot - 1); 2321 block1 = btrfs_node_blockptr(parent, slot - 1);
2312 gen = btrfs_node_ptr_generation(parent, slot - 1); 2322 gen = btrfs_node_ptr_generation(parent, slot - 1);
2313 eb = find_extent_buffer(root->fs_info, block1); 2323 eb = find_extent_buffer(fs_info, block1);
2314 /* 2324 /*
2315 * if we get -eagain from btrfs_buffer_uptodate, we 2325 * if we get -eagain from btrfs_buffer_uptodate, we
2316 * don't want to return eagain here. That will loop 2326 * don't want to return eagain here. That will loop
@@ -2323,7 +2333,7 @@ static noinline void reada_for_balance(struct btrfs_root *root,
2323 if (slot + 1 < nritems) { 2333 if (slot + 1 < nritems) {
2324 block2 = btrfs_node_blockptr(parent, slot + 1); 2334 block2 = btrfs_node_blockptr(parent, slot + 1);
2325 gen = btrfs_node_ptr_generation(parent, slot + 1); 2335 gen = btrfs_node_ptr_generation(parent, slot + 1);
2326 eb = find_extent_buffer(root->fs_info, block2); 2336 eb = find_extent_buffer(fs_info, block2);
2327 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) 2337 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
2328 block2 = 0; 2338 block2 = 0;
2329 free_extent_buffer(eb); 2339 free_extent_buffer(eb);
@@ -2432,6 +2442,7 @@ read_block_for_search(struct btrfs_trans_handle *trans,
2432 struct extent_buffer **eb_ret, int level, int slot, 2442 struct extent_buffer **eb_ret, int level, int slot,
2433 struct btrfs_key *key, u64 time_seq) 2443 struct btrfs_key *key, u64 time_seq)
2434{ 2444{
2445 struct btrfs_fs_info *fs_info = root->fs_info;
2435 u64 blocknr; 2446 u64 blocknr;
2436 u64 gen; 2447 u64 gen;
2437 struct extent_buffer *b = *eb_ret; 2448 struct extent_buffer *b = *eb_ret;
@@ -2441,7 +2452,7 @@ read_block_for_search(struct btrfs_trans_handle *trans,
2441 blocknr = btrfs_node_blockptr(b, slot); 2452 blocknr = btrfs_node_blockptr(b, slot);
2442 gen = btrfs_node_ptr_generation(b, slot); 2453 gen = btrfs_node_ptr_generation(b, slot);
2443 2454
2444 tmp = find_extent_buffer(root->fs_info, blocknr); 2455 tmp = find_extent_buffer(fs_info, blocknr);
2445 if (tmp) { 2456 if (tmp) {
2446 /* first we do an atomic uptodate check */ 2457 /* first we do an atomic uptodate check */
2447 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { 2458 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
@@ -2517,9 +2528,11 @@ setup_nodes_for_search(struct btrfs_trans_handle *trans,
2517 struct extent_buffer *b, int level, int ins_len, 2528 struct extent_buffer *b, int level, int ins_len,
2518 int *write_lock_level) 2529 int *write_lock_level)
2519{ 2530{
2531 struct btrfs_fs_info *fs_info = root->fs_info;
2520 int ret; 2532 int ret;
2533
2521 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= 2534 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2522 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3) { 2535 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
2523 int sret; 2536 int sret;
2524 2537
2525 if (*write_lock_level < level + 1) { 2538 if (*write_lock_level < level + 1) {
@@ -2540,7 +2553,7 @@ setup_nodes_for_search(struct btrfs_trans_handle *trans,
2540 } 2553 }
2541 b = p->nodes[level]; 2554 b = p->nodes[level];
2542 } else if (ins_len < 0 && btrfs_header_nritems(b) < 2555 } else if (ins_len < 0 && btrfs_header_nritems(b) <
2543 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) / 2) { 2556 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
2544 int sret; 2557 int sret;
2545 2558
2546 if (*write_lock_level < level + 1) { 2559 if (*write_lock_level < level + 1) {
@@ -2659,6 +2672,7 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2659 *root, struct btrfs_key *key, struct btrfs_path *p, int 2672 *root, struct btrfs_key *key, struct btrfs_path *p, int
2660 ins_len, int cow) 2673 ins_len, int cow)
2661{ 2674{
2675 struct btrfs_fs_info *fs_info = root->fs_info;
2662 struct extent_buffer *b; 2676 struct extent_buffer *b;
2663 int slot; 2677 int slot;
2664 int ret; 2678 int ret;
@@ -2714,12 +2728,12 @@ again:
2714 * so we always do read locks 2728 * so we always do read locks
2715 */ 2729 */
2716 if (p->need_commit_sem) 2730 if (p->need_commit_sem)
2717 down_read(&root->fs_info->commit_root_sem); 2731 down_read(&fs_info->commit_root_sem);
2718 b = root->commit_root; 2732 b = root->commit_root;
2719 extent_buffer_get(b); 2733 extent_buffer_get(b);
2720 level = btrfs_header_level(b); 2734 level = btrfs_header_level(b);
2721 if (p->need_commit_sem) 2735 if (p->need_commit_sem)
2722 up_read(&root->fs_info->commit_root_sem); 2736 up_read(&fs_info->commit_root_sem);
2723 if (!p->skip_locking) 2737 if (!p->skip_locking)
2724 btrfs_tree_read_lock(b); 2738 btrfs_tree_read_lock(b);
2725 } else { 2739 } else {
@@ -2942,6 +2956,7 @@ done:
2942int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key, 2956int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2943 struct btrfs_path *p, u64 time_seq) 2957 struct btrfs_path *p, u64 time_seq)
2944{ 2958{
2959 struct btrfs_fs_info *fs_info = root->fs_info;
2945 struct extent_buffer *b; 2960 struct extent_buffer *b;
2946 int slot; 2961 int slot;
2947 int ret; 2962 int ret;
@@ -3016,7 +3031,7 @@ again:
3016 btrfs_clear_path_blocking(p, b, 3031 btrfs_clear_path_blocking(p, b,
3017 BTRFS_READ_LOCK); 3032 BTRFS_READ_LOCK);
3018 } 3033 }
3019 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq); 3034 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3020 if (!b) { 3035 if (!b) {
3021 ret = -ENOMEM; 3036 ret = -ENOMEM;
3022 goto done; 3037 goto done;
@@ -3186,6 +3201,7 @@ static int push_node_left(struct btrfs_trans_handle *trans,
3186 struct btrfs_root *root, struct extent_buffer *dst, 3201 struct btrfs_root *root, struct extent_buffer *dst,
3187 struct extent_buffer *src, int empty) 3202 struct extent_buffer *src, int empty)
3188{ 3203{
3204 struct btrfs_fs_info *fs_info = root->fs_info;
3189 int push_items = 0; 3205 int push_items = 0;
3190 int src_nritems; 3206 int src_nritems;
3191 int dst_nritems; 3207 int dst_nritems;
@@ -3193,7 +3209,7 @@ static int push_node_left(struct btrfs_trans_handle *trans,
3193 3209
3194 src_nritems = btrfs_header_nritems(src); 3210 src_nritems = btrfs_header_nritems(src);
3195 dst_nritems = btrfs_header_nritems(dst); 3211 dst_nritems = btrfs_header_nritems(dst);
3196 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems; 3212 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3197 WARN_ON(btrfs_header_generation(src) != trans->transid); 3213 WARN_ON(btrfs_header_generation(src) != trans->transid);
3198 WARN_ON(btrfs_header_generation(dst) != trans->transid); 3214 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3199 3215
@@ -3218,7 +3234,7 @@ static int push_node_left(struct btrfs_trans_handle *trans,
3218 } else 3234 } else
3219 push_items = min(src_nritems - 8, push_items); 3235 push_items = min(src_nritems - 8, push_items);
3220 3236
3221 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0, 3237 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
3222 push_items); 3238 push_items);
3223 if (ret) { 3239 if (ret) {
3224 btrfs_abort_transaction(trans, ret); 3240 btrfs_abort_transaction(trans, ret);
@@ -3261,6 +3277,7 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
3261 struct extent_buffer *dst, 3277 struct extent_buffer *dst,
3262 struct extent_buffer *src) 3278 struct extent_buffer *src)
3263{ 3279{
3280 struct btrfs_fs_info *fs_info = root->fs_info;
3264 int push_items = 0; 3281 int push_items = 0;
3265 int max_push; 3282 int max_push;
3266 int src_nritems; 3283 int src_nritems;
@@ -3272,7 +3289,7 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
3272 3289
3273 src_nritems = btrfs_header_nritems(src); 3290 src_nritems = btrfs_header_nritems(src);
3274 dst_nritems = btrfs_header_nritems(dst); 3291 dst_nritems = btrfs_header_nritems(dst);
3275 push_items = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - dst_nritems; 3292 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
3276 if (push_items <= 0) 3293 if (push_items <= 0)
3277 return 1; 3294 return 1;
3278 3295
@@ -3287,13 +3304,13 @@ static int balance_node_right(struct btrfs_trans_handle *trans,
3287 if (max_push < push_items) 3304 if (max_push < push_items)
3288 push_items = max_push; 3305 push_items = max_push;
3289 3306
3290 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems); 3307 tree_mod_log_eb_move(fs_info, dst, push_items, 0, dst_nritems);
3291 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), 3308 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3292 btrfs_node_key_ptr_offset(0), 3309 btrfs_node_key_ptr_offset(0),
3293 (dst_nritems) * 3310 (dst_nritems) *
3294 sizeof(struct btrfs_key_ptr)); 3311 sizeof(struct btrfs_key_ptr));
3295 3312
3296 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0, 3313 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
3297 src_nritems - push_items, push_items); 3314 src_nritems - push_items, push_items);
3298 if (ret) { 3315 if (ret) {
3299 btrfs_abort_transaction(trans, ret); 3316 btrfs_abort_transaction(trans, ret);
@@ -3324,6 +3341,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3324 struct btrfs_root *root, 3341 struct btrfs_root *root,
3325 struct btrfs_path *path, int level) 3342 struct btrfs_path *path, int level)
3326{ 3343{
3344 struct btrfs_fs_info *fs_info = root->fs_info;
3327 u64 lower_gen; 3345 u64 lower_gen;
3328 struct extent_buffer *lower; 3346 struct extent_buffer *lower;
3329 struct extent_buffer *c; 3347 struct extent_buffer *c;
@@ -3344,7 +3362,7 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3344 if (IS_ERR(c)) 3362 if (IS_ERR(c))
3345 return PTR_ERR(c); 3363 return PTR_ERR(c);
3346 3364
3347 root_add_used(root, root->fs_info->nodesize); 3365 root_add_used(root, fs_info->nodesize);
3348 3366
3349 memzero_extent_buffer(c, 0, sizeof(struct btrfs_header)); 3367 memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
3350 btrfs_set_header_nritems(c, 1); 3368 btrfs_set_header_nritems(c, 1);
@@ -3354,8 +3372,8 @@ static noinline int insert_new_root(struct btrfs_trans_handle *trans,
3354 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); 3372 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
3355 btrfs_set_header_owner(c, root->root_key.objectid); 3373 btrfs_set_header_owner(c, root->root_key.objectid);
3356 3374
3357 write_extent_buffer_fsid(c, root->fs_info->fsid); 3375 write_extent_buffer_fsid(c, fs_info->fsid);
3358 write_extent_buffer_chunk_tree_uuid(c, root->fs_info->chunk_tree_uuid); 3376 write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
3359 3377
3360 btrfs_set_node_key(c, &lower_key, 0); 3378 btrfs_set_node_key(c, &lower_key, 0);
3361 btrfs_set_node_blockptr(c, 0, lower->start); 3379 btrfs_set_node_blockptr(c, 0, lower->start);
@@ -3393,6 +3411,7 @@ static void insert_ptr(struct btrfs_trans_handle *trans,
3393 struct btrfs_disk_key *key, u64 bytenr, 3411 struct btrfs_disk_key *key, u64 bytenr,
3394 int slot, int level) 3412 int slot, int level)
3395{ 3413{
3414 struct btrfs_fs_info *fs_info = root->fs_info;
3396 struct extent_buffer *lower; 3415 struct extent_buffer *lower;
3397 int nritems; 3416 int nritems;
3398 int ret; 3417 int ret;
@@ -3402,10 +3421,10 @@ static void insert_ptr(struct btrfs_trans_handle *trans,
3402 lower = path->nodes[level]; 3421 lower = path->nodes[level];
3403 nritems = btrfs_header_nritems(lower); 3422 nritems = btrfs_header_nritems(lower);
3404 BUG_ON(slot > nritems); 3423 BUG_ON(slot > nritems);
3405 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)); 3424 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
3406 if (slot != nritems) { 3425 if (slot != nritems) {
3407 if (level) 3426 if (level)
3408 tree_mod_log_eb_move(root->fs_info, lower, slot + 1, 3427 tree_mod_log_eb_move(fs_info, lower, slot + 1,
3409 slot, nritems - slot); 3428 slot, nritems - slot);
3410 memmove_extent_buffer(lower, 3429 memmove_extent_buffer(lower,
3411 btrfs_node_key_ptr_offset(slot + 1), 3430 btrfs_node_key_ptr_offset(slot + 1),
@@ -3413,7 +3432,7 @@ static void insert_ptr(struct btrfs_trans_handle *trans,
3413 (nritems - slot) * sizeof(struct btrfs_key_ptr)); 3432 (nritems - slot) * sizeof(struct btrfs_key_ptr));
3414 } 3433 }
3415 if (level) { 3434 if (level) {
3416 ret = tree_mod_log_insert_key(root->fs_info, lower, slot, 3435 ret = tree_mod_log_insert_key(fs_info, lower, slot,
3417 MOD_LOG_KEY_ADD, GFP_NOFS); 3436 MOD_LOG_KEY_ADD, GFP_NOFS);
3418 BUG_ON(ret < 0); 3437 BUG_ON(ret < 0);
3419 } 3438 }
@@ -3438,6 +3457,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3438 struct btrfs_root *root, 3457 struct btrfs_root *root,
3439 struct btrfs_path *path, int level) 3458 struct btrfs_path *path, int level)
3440{ 3459{
3460 struct btrfs_fs_info *fs_info = root->fs_info;
3441 struct extent_buffer *c; 3461 struct extent_buffer *c;
3442 struct extent_buffer *split; 3462 struct extent_buffer *split;
3443 struct btrfs_disk_key disk_key; 3463 struct btrfs_disk_key disk_key;
@@ -3465,7 +3485,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3465 ret = push_nodes_for_insert(trans, root, path, level); 3485 ret = push_nodes_for_insert(trans, root, path, level);
3466 c = path->nodes[level]; 3486 c = path->nodes[level];
3467 if (!ret && btrfs_header_nritems(c) < 3487 if (!ret && btrfs_header_nritems(c) <
3468 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - 3) 3488 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
3469 return 0; 3489 return 0;
3470 if (ret < 0) 3490 if (ret < 0)
3471 return ret; 3491 return ret;
@@ -3480,7 +3500,7 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3480 if (IS_ERR(split)) 3500 if (IS_ERR(split))
3481 return PTR_ERR(split); 3501 return PTR_ERR(split);
3482 3502
3483 root_add_used(root, root->fs_info->nodesize); 3503 root_add_used(root, fs_info->nodesize);
3484 3504
3485 memzero_extent_buffer(split, 0, sizeof(struct btrfs_header)); 3505 memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
3486 btrfs_set_header_level(split, btrfs_header_level(c)); 3506 btrfs_set_header_level(split, btrfs_header_level(c));
@@ -3488,12 +3508,10 @@ static noinline int split_node(struct btrfs_trans_handle *trans,
3488 btrfs_set_header_generation(split, trans->transid); 3508 btrfs_set_header_generation(split, trans->transid);
3489 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); 3509 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
3490 btrfs_set_header_owner(split, root->root_key.objectid); 3510 btrfs_set_header_owner(split, root->root_key.objectid);
3491 write_extent_buffer_fsid(split, root->fs_info->fsid); 3511 write_extent_buffer_fsid(split, fs_info->fsid);
3492 write_extent_buffer_chunk_tree_uuid(split, 3512 write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
3493 root->fs_info->chunk_tree_uuid);
3494 3513
3495 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0, 3514 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
3496 mid, c_nritems - mid);
3497 if (ret) { 3515 if (ret) {
3498 btrfs_abort_transaction(trans, ret); 3516 btrfs_abort_transaction(trans, ret);
3499 return ret; 3517 return ret;
@@ -3560,15 +3578,17 @@ static int leaf_space_used(struct extent_buffer *l, int start, int nr)
3560noinline int btrfs_leaf_free_space(struct btrfs_root *root, 3578noinline int btrfs_leaf_free_space(struct btrfs_root *root,
3561 struct extent_buffer *leaf) 3579 struct extent_buffer *leaf)
3562{ 3580{
3581 struct btrfs_fs_info *fs_info = root->fs_info;
3563 int nritems = btrfs_header_nritems(leaf); 3582 int nritems = btrfs_header_nritems(leaf);
3564 int ret; 3583 int ret;
3565 ret = BTRFS_LEAF_DATA_SIZE(root->fs_info) - leaf_space_used(leaf, 0, nritems); 3584
3585 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
3566 if (ret < 0) { 3586 if (ret < 0) {
3567 btrfs_crit(root->fs_info, 3587 btrfs_crit(fs_info,
3568 "leaf free space ret %d, leaf data size %lu, used %d nritems %d", 3588 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3569 ret, 3589 ret,
3570 (unsigned long) BTRFS_LEAF_DATA_SIZE(root->fs_info), 3590 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3571 leaf_space_used(leaf, 0, nritems), nritems); 3591 leaf_space_used(leaf, 0, nritems), nritems);
3572 } 3592 }
3573 return ret; 3593 return ret;
3574} 3594}
@@ -3585,6 +3605,7 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3585 int free_space, u32 left_nritems, 3605 int free_space, u32 left_nritems,
3586 u32 min_slot) 3606 u32 min_slot)
3587{ 3607{
3608 struct btrfs_fs_info *fs_info = root->fs_info;
3588 struct extent_buffer *left = path->nodes[0]; 3609 struct extent_buffer *left = path->nodes[0];
3589 struct extent_buffer *upper = path->nodes[1]; 3610 struct extent_buffer *upper = path->nodes[1];
3590 struct btrfs_map_token token; 3611 struct btrfs_map_token token;
@@ -3654,11 +3675,11 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3654 memmove_extent_buffer(right, 3675 memmove_extent_buffer(right,
3655 btrfs_leaf_data(right) + data_end - push_space, 3676 btrfs_leaf_data(right) + data_end - push_space,
3656 btrfs_leaf_data(right) + data_end, 3677 btrfs_leaf_data(right) + data_end,
3657 BTRFS_LEAF_DATA_SIZE(root->fs_info) - data_end); 3678 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
3658 3679
3659 /* copy from the left data area */ 3680 /* copy from the left data area */
3660 copy_extent_buffer(right, left, btrfs_leaf_data(right) + 3681 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
3661 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space, 3682 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3662 btrfs_leaf_data(left) + leaf_data_end(root, left), 3683 btrfs_leaf_data(left) + leaf_data_end(root, left),
3663 push_space); 3684 push_space);
3664 3685
@@ -3674,7 +3695,7 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3674 /* update the item pointers */ 3695 /* update the item pointers */
3675 right_nritems += push_items; 3696 right_nritems += push_items;
3676 btrfs_set_header_nritems(right, right_nritems); 3697 btrfs_set_header_nritems(right, right_nritems);
3677 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info); 3698 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3678 for (i = 0; i < right_nritems; i++) { 3699 for (i = 0; i < right_nritems; i++) {
3679 item = btrfs_item_nr(i); 3700 item = btrfs_item_nr(i);
3680 push_space -= btrfs_token_item_size(right, item, &token); 3701 push_space -= btrfs_token_item_size(right, item, &token);
@@ -3687,7 +3708,7 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3687 if (left_nritems) 3708 if (left_nritems)
3688 btrfs_mark_buffer_dirty(left); 3709 btrfs_mark_buffer_dirty(left);
3689 else 3710 else
3690 clean_tree_block(trans, root->fs_info, left); 3711 clean_tree_block(trans, fs_info, left);
3691 3712
3692 btrfs_mark_buffer_dirty(right); 3713 btrfs_mark_buffer_dirty(right);
3693 3714
@@ -3699,7 +3720,7 @@ static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3699 if (path->slots[0] >= left_nritems) { 3720 if (path->slots[0] >= left_nritems) {
3700 path->slots[0] -= left_nritems; 3721 path->slots[0] -= left_nritems;
3701 if (btrfs_header_nritems(path->nodes[0]) == 0) 3722 if (btrfs_header_nritems(path->nodes[0]) == 0)
3702 clean_tree_block(trans, root->fs_info, path->nodes[0]); 3723 clean_tree_block(trans, fs_info, path->nodes[0]);
3703 btrfs_tree_unlock(path->nodes[0]); 3724 btrfs_tree_unlock(path->nodes[0]);
3704 free_extent_buffer(path->nodes[0]); 3725 free_extent_buffer(path->nodes[0]);
3705 path->nodes[0] = right; 3726 path->nodes[0] = right;
@@ -3814,6 +3835,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3814 int free_space, u32 right_nritems, 3835 int free_space, u32 right_nritems,
3815 u32 max_slot) 3836 u32 max_slot)
3816{ 3837{
3838 struct btrfs_fs_info *fs_info = root->fs_info;
3817 struct btrfs_disk_key disk_key; 3839 struct btrfs_disk_key disk_key;
3818 struct extent_buffer *right = path->nodes[0]; 3840 struct extent_buffer *right = path->nodes[0];
3819 int i; 3841 int i;
@@ -3870,7 +3892,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3870 btrfs_item_nr_offset(0), 3892 btrfs_item_nr_offset(0),
3871 push_items * sizeof(struct btrfs_item)); 3893 push_items * sizeof(struct btrfs_item));
3872 3894
3873 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info) - 3895 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
3874 btrfs_item_offset_nr(right, push_items - 1); 3896 btrfs_item_offset_nr(right, push_items - 1);
3875 3897
3876 copy_extent_buffer(left, right, btrfs_leaf_data(left) + 3898 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
@@ -3889,7 +3911,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3889 3911
3890 ioff = btrfs_token_item_offset(left, item, &token); 3912 ioff = btrfs_token_item_offset(left, item, &token);
3891 btrfs_set_token_item_offset(left, item, 3913 btrfs_set_token_item_offset(left, item,
3892 ioff - (BTRFS_LEAF_DATA_SIZE(root->fs_info) - old_left_item_size), 3914 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
3893 &token); 3915 &token);
3894 } 3916 }
3895 btrfs_set_header_nritems(left, old_left_nritems + push_items); 3917 btrfs_set_header_nritems(left, old_left_nritems + push_items);
@@ -3903,7 +3925,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3903 push_space = btrfs_item_offset_nr(right, push_items - 1) - 3925 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3904 leaf_data_end(root, right); 3926 leaf_data_end(root, right);
3905 memmove_extent_buffer(right, btrfs_leaf_data(right) + 3927 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3906 BTRFS_LEAF_DATA_SIZE(root->fs_info) - push_space, 3928 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
3907 btrfs_leaf_data(right) + 3929 btrfs_leaf_data(right) +
3908 leaf_data_end(root, right), push_space); 3930 leaf_data_end(root, right), push_space);
3909 3931
@@ -3914,7 +3936,7 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3914 } 3936 }
3915 right_nritems -= push_items; 3937 right_nritems -= push_items;
3916 btrfs_set_header_nritems(right, right_nritems); 3938 btrfs_set_header_nritems(right, right_nritems);
3917 push_space = BTRFS_LEAF_DATA_SIZE(root->fs_info); 3939 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
3918 for (i = 0; i < right_nritems; i++) { 3940 for (i = 0; i < right_nritems; i++) {
3919 item = btrfs_item_nr(i); 3941 item = btrfs_item_nr(i);
3920 3942
@@ -3927,10 +3949,10 @@ static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3927 if (right_nritems) 3949 if (right_nritems)
3928 btrfs_mark_buffer_dirty(right); 3950 btrfs_mark_buffer_dirty(right);
3929 else 3951 else
3930 clean_tree_block(trans, root->fs_info, right); 3952 clean_tree_block(trans, fs_info, right);
3931 3953
3932 btrfs_item_key(right, &disk_key, 0); 3954 btrfs_item_key(right, &disk_key, 0);
3933 fixup_low_keys(root->fs_info, path, &disk_key, 1); 3955 fixup_low_keys(fs_info, path, &disk_key, 1);
3934 3956
3935 /* then fixup the leaf pointer in the path */ 3957 /* then fixup the leaf pointer in the path */
3936 if (path->slots[0] < push_items) { 3958 if (path->slots[0] < push_items) {
@@ -4036,6 +4058,7 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4036 struct extent_buffer *right, 4058 struct extent_buffer *right,
4037 int slot, int mid, int nritems) 4059 int slot, int mid, int nritems)
4038{ 4060{
4061 struct btrfs_fs_info *fs_info = root->fs_info;
4039 int data_copy_size; 4062 int data_copy_size;
4040 int rt_data_off; 4063 int rt_data_off;
4041 int i; 4064 int i;
@@ -4053,12 +4076,11 @@ static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4053 nritems * sizeof(struct btrfs_item)); 4076 nritems * sizeof(struct btrfs_item));
4054 4077
4055 copy_extent_buffer(right, l, 4078 copy_extent_buffer(right, l,
4056 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root->fs_info) - 4079 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(fs_info) -
4057 data_copy_size, btrfs_leaf_data(l) + 4080 data_copy_size, btrfs_leaf_data(l) +
4058 leaf_data_end(root, l), data_copy_size); 4081 leaf_data_end(root, l), data_copy_size);
4059 4082
4060 rt_data_off = BTRFS_LEAF_DATA_SIZE(root->fs_info) - 4083 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
4061 btrfs_item_end_nr(l, mid);
4062 4084
4063 for (i = 0; i < nritems; i++) { 4085 for (i = 0; i < nritems; i++) {
4064 struct btrfs_item *item = btrfs_item_nr(i); 4086 struct btrfs_item *item = btrfs_item_nr(i);
@@ -4181,7 +4203,7 @@ static noinline int split_leaf(struct btrfs_trans_handle *trans,
4181 l = path->nodes[0]; 4203 l = path->nodes[0];
4182 slot = path->slots[0]; 4204 slot = path->slots[0];
4183 if (extend && data_size + btrfs_item_size_nr(l, slot) + 4205 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4184 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root->fs_info)) 4206 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
4185 return -EOVERFLOW; 4207 return -EOVERFLOW;
4186 4208
4187 /* first try to make some room by pushing left and right */ 4209 /* first try to make some room by pushing left and right */
@@ -4223,14 +4245,14 @@ again:
4223 if (mid <= slot) { 4245 if (mid <= slot) {
4224 if (nritems == 1 || 4246 if (nritems == 1 ||
4225 leaf_space_used(l, mid, nritems - mid) + data_size > 4247 leaf_space_used(l, mid, nritems - mid) + data_size >
4226 BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 4248 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4227 if (slot >= nritems) { 4249 if (slot >= nritems) {
4228 split = 0; 4250 split = 0;
4229 } else { 4251 } else {
4230 mid = slot; 4252 mid = slot;
4231 if (mid != nritems && 4253 if (mid != nritems &&
4232 leaf_space_used(l, mid, nritems - mid) + 4254 leaf_space_used(l, mid, nritems - mid) +
4233 data_size > BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 4255 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4234 if (data_size && !tried_avoid_double) 4256 if (data_size && !tried_avoid_double)
4235 goto push_for_double; 4257 goto push_for_double;
4236 split = 2; 4258 split = 2;
@@ -4239,7 +4261,7 @@ again:
4239 } 4261 }
4240 } else { 4262 } else {
4241 if (leaf_space_used(l, 0, mid) + data_size > 4263 if (leaf_space_used(l, 0, mid) + data_size >
4242 BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 4264 BTRFS_LEAF_DATA_SIZE(fs_info)) {
4243 if (!extend && data_size && slot == 0) { 4265 if (!extend && data_size && slot == 0) {
4244 split = 0; 4266 split = 0;
4245 } else if ((extend || !data_size) && slot == 0) { 4267 } else if ((extend || !data_size) && slot == 0) {
@@ -4248,7 +4270,7 @@ again:
4248 mid = slot; 4270 mid = slot;
4249 if (mid != nritems && 4271 if (mid != nritems &&
4250 leaf_space_used(l, mid, nritems - mid) + 4272 leaf_space_used(l, mid, nritems - mid) +
4251 data_size > BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 4273 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
4252 if (data_size && !tried_avoid_double) 4274 if (data_size && !tried_avoid_double)
4253 goto push_for_double; 4275 goto push_for_double;
4254 split = 2; 4276 split = 2;
@@ -4267,7 +4289,7 @@ again:
4267 if (IS_ERR(right)) 4289 if (IS_ERR(right))
4268 return PTR_ERR(right); 4290 return PTR_ERR(right);
4269 4291
4270 root_add_used(root, root->fs_info->nodesize); 4292 root_add_used(root, fs_info->nodesize);
4271 4293
4272 memzero_extent_buffer(right, 0, sizeof(struct btrfs_header)); 4294 memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
4273 btrfs_set_header_bytenr(right, right->start); 4295 btrfs_set_header_bytenr(right, right->start);
@@ -4539,6 +4561,7 @@ int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4539void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path, 4561void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4540 u32 new_size, int from_end) 4562 u32 new_size, int from_end)
4541{ 4563{
4564 struct btrfs_fs_info *fs_info = root->fs_info;
4542 int slot; 4565 int slot;
4543 struct extent_buffer *leaf; 4566 struct extent_buffer *leaf;
4544 struct btrfs_item *item; 4567 struct btrfs_item *item;
@@ -4619,7 +4642,7 @@ void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4619 btrfs_set_disk_key_offset(&disk_key, offset + size_diff); 4642 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4620 btrfs_set_item_key(leaf, &disk_key, slot); 4643 btrfs_set_item_key(leaf, &disk_key, slot);
4621 if (slot == 0) 4644 if (slot == 0)
4622 fixup_low_keys(root->fs_info, path, &disk_key, 1); 4645 fixup_low_keys(fs_info, path, &disk_key, 1);
4623 } 4646 }
4624 4647
4625 item = btrfs_item_nr(slot); 4648 item = btrfs_item_nr(slot);
@@ -4638,6 +4661,7 @@ void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
4638void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path, 4661void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
4639 u32 data_size) 4662 u32 data_size)
4640{ 4663{
4664 struct btrfs_fs_info *fs_info = root->fs_info;
4641 int slot; 4665 int slot;
4642 struct extent_buffer *leaf; 4666 struct extent_buffer *leaf;
4643 struct btrfs_item *item; 4667 struct btrfs_item *item;
@@ -4665,8 +4689,8 @@ void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
4665 BUG_ON(slot < 0); 4689 BUG_ON(slot < 0);
4666 if (slot >= nritems) { 4690 if (slot >= nritems) {
4667 btrfs_print_leaf(root, leaf); 4691 btrfs_print_leaf(root, leaf);
4668 btrfs_crit(root->fs_info, "slot %d too large, nritems %d", 4692 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4669 slot, nritems); 4693 slot, nritems);
4670 BUG_ON(1); 4694 BUG_ON(1);
4671 } 4695 }
4672 4696
@@ -4709,6 +4733,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4709 struct btrfs_key *cpu_key, u32 *data_size, 4733 struct btrfs_key *cpu_key, u32 *data_size,
4710 u32 total_data, u32 total_size, int nr) 4734 u32 total_data, u32 total_size, int nr)
4711{ 4735{
4736 struct btrfs_fs_info *fs_info = root->fs_info;
4712 struct btrfs_item *item; 4737 struct btrfs_item *item;
4713 int i; 4738 int i;
4714 u32 nritems; 4739 u32 nritems;
@@ -4720,7 +4745,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4720 4745
4721 if (path->slots[0] == 0) { 4746 if (path->slots[0] == 0) {
4722 btrfs_cpu_key_to_disk(&disk_key, cpu_key); 4747 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
4723 fixup_low_keys(root->fs_info, path, &disk_key, 1); 4748 fixup_low_keys(fs_info, path, &disk_key, 1);
4724 } 4749 }
4725 btrfs_unlock_up_safe(path, 1); 4750 btrfs_unlock_up_safe(path, 1);
4726 4751
@@ -4734,8 +4759,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4734 4759
4735 if (btrfs_leaf_free_space(root, leaf) < total_size) { 4760 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4736 btrfs_print_leaf(root, leaf); 4761 btrfs_print_leaf(root, leaf);
4737 btrfs_crit(root->fs_info, 4762 btrfs_crit(fs_info, "not enough freespace need %u have %d",
4738 "not enough freespace need %u have %d",
4739 total_size, btrfs_leaf_free_space(root, leaf)); 4763 total_size, btrfs_leaf_free_space(root, leaf));
4740 BUG(); 4764 BUG();
4741 } 4765 }
@@ -4745,8 +4769,7 @@ void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
4745 4769
4746 if (old_data < data_end) { 4770 if (old_data < data_end) {
4747 btrfs_print_leaf(root, leaf); 4771 btrfs_print_leaf(root, leaf);
4748 btrfs_crit(root->fs_info, 4772 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
4749 "slot %d old_data %d data_end %d",
4750 slot, old_data, data_end); 4773 slot, old_data, data_end);
4751 BUG_ON(1); 4774 BUG_ON(1);
4752 } 4775 }
@@ -4864,6 +4887,7 @@ int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4864static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, 4887static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4865 int level, int slot) 4888 int level, int slot)
4866{ 4889{
4890 struct btrfs_fs_info *fs_info = root->fs_info;
4867 struct extent_buffer *parent = path->nodes[level]; 4891 struct extent_buffer *parent = path->nodes[level];
4868 u32 nritems; 4892 u32 nritems;
4869 int ret; 4893 int ret;
@@ -4871,7 +4895,7 @@ static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4871 nritems = btrfs_header_nritems(parent); 4895 nritems = btrfs_header_nritems(parent);
4872 if (slot != nritems - 1) { 4896 if (slot != nritems - 1) {
4873 if (level) 4897 if (level)
4874 tree_mod_log_eb_move(root->fs_info, parent, slot, 4898 tree_mod_log_eb_move(fs_info, parent, slot,
4875 slot + 1, nritems - slot - 1); 4899 slot + 1, nritems - slot - 1);
4876 memmove_extent_buffer(parent, 4900 memmove_extent_buffer(parent,
4877 btrfs_node_key_ptr_offset(slot), 4901 btrfs_node_key_ptr_offset(slot),
@@ -4879,7 +4903,7 @@ static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4879 sizeof(struct btrfs_key_ptr) * 4903 sizeof(struct btrfs_key_ptr) *
4880 (nritems - slot - 1)); 4904 (nritems - slot - 1));
4881 } else if (level) { 4905 } else if (level) {
4882 ret = tree_mod_log_insert_key(root->fs_info, parent, slot, 4906 ret = tree_mod_log_insert_key(fs_info, parent, slot,
4883 MOD_LOG_KEY_REMOVE, GFP_NOFS); 4907 MOD_LOG_KEY_REMOVE, GFP_NOFS);
4884 BUG_ON(ret < 0); 4908 BUG_ON(ret < 0);
4885 } 4909 }
@@ -4894,7 +4918,7 @@ static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4894 struct btrfs_disk_key disk_key; 4918 struct btrfs_disk_key disk_key;
4895 4919
4896 btrfs_node_key(parent, &disk_key, 0); 4920 btrfs_node_key(parent, &disk_key, 0);
4897 fixup_low_keys(root->fs_info, path, &disk_key, level + 1); 4921 fixup_low_keys(fs_info, path, &disk_key, level + 1);
4898 } 4922 }
4899 btrfs_mark_buffer_dirty(parent); 4923 btrfs_mark_buffer_dirty(parent);
4900} 4924}
@@ -4936,6 +4960,7 @@ static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4936int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, 4960int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4937 struct btrfs_path *path, int slot, int nr) 4961 struct btrfs_path *path, int slot, int nr)
4938{ 4962{
4963 struct btrfs_fs_info *fs_info = root->fs_info;
4939 struct extent_buffer *leaf; 4964 struct extent_buffer *leaf;
4940 struct btrfs_item *item; 4965 struct btrfs_item *item;
4941 u32 last_off; 4966 u32 last_off;
@@ -4987,7 +5012,7 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4987 btrfs_set_header_level(leaf, 0); 5012 btrfs_set_header_level(leaf, 0);
4988 } else { 5013 } else {
4989 btrfs_set_path_blocking(path); 5014 btrfs_set_path_blocking(path);
4990 clean_tree_block(trans, root->fs_info, leaf); 5015 clean_tree_block(trans, fs_info, leaf);
4991 btrfs_del_leaf(trans, root, path, leaf); 5016 btrfs_del_leaf(trans, root, path, leaf);
4992 } 5017 }
4993 } else { 5018 } else {
@@ -4996,11 +5021,11 @@ int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4996 struct btrfs_disk_key disk_key; 5021 struct btrfs_disk_key disk_key;
4997 5022
4998 btrfs_item_key(leaf, &disk_key, 0); 5023 btrfs_item_key(leaf, &disk_key, 0);
4999 fixup_low_keys(root->fs_info, path, &disk_key, 1); 5024 fixup_low_keys(fs_info, path, &disk_key, 1);
5000 } 5025 }
5001 5026
5002 /* delete the leaf if it is mostly empty */ 5027 /* delete the leaf if it is mostly empty */
5003 if (used < BTRFS_LEAF_DATA_SIZE(root->fs_info) / 3) { 5028 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
5004 /* push_leaf_left fixes the path. 5029 /* push_leaf_left fixes the path.
5005 * make sure the path still points to our leaf 5030 * make sure the path still points to our leaf
5006 * for possible call to del_ptr below 5031 * for possible call to del_ptr below
@@ -5337,6 +5362,7 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5337 struct btrfs_root *right_root, 5362 struct btrfs_root *right_root,
5338 btrfs_changed_cb_t changed_cb, void *ctx) 5363 btrfs_changed_cb_t changed_cb, void *ctx)
5339{ 5364{
5365 struct btrfs_fs_info *fs_info = left_root->fs_info;
5340 int ret; 5366 int ret;
5341 int cmp; 5367 int cmp;
5342 struct btrfs_path *left_path = NULL; 5368 struct btrfs_path *left_path = NULL;
@@ -5368,10 +5394,9 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5368 goto out; 5394 goto out;
5369 } 5395 }
5370 5396
5371 tmp_buf = kmalloc(left_root->fs_info->nodesize, 5397 tmp_buf = kmalloc(fs_info->nodesize, GFP_KERNEL | __GFP_NOWARN);
5372 GFP_KERNEL | __GFP_NOWARN);
5373 if (!tmp_buf) { 5398 if (!tmp_buf) {
5374 tmp_buf = vmalloc(left_root->fs_info->nodesize); 5399 tmp_buf = vmalloc(fs_info->nodesize);
5375 if (!tmp_buf) { 5400 if (!tmp_buf) {
5376 ret = -ENOMEM; 5401 ret = -ENOMEM;
5377 goto out; 5402 goto out;
@@ -5419,7 +5444,7 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5419 * the right if possible or go up and right. 5444 * the right if possible or go up and right.
5420 */ 5445 */
5421 5446
5422 down_read(&left_root->fs_info->commit_root_sem); 5447 down_read(&fs_info->commit_root_sem);
5423 left_level = btrfs_header_level(left_root->commit_root); 5448 left_level = btrfs_header_level(left_root->commit_root);
5424 left_root_level = left_level; 5449 left_root_level = left_level;
5425 left_path->nodes[left_level] = left_root->commit_root; 5450 left_path->nodes[left_level] = left_root->commit_root;
@@ -5429,7 +5454,7 @@ int btrfs_compare_trees(struct btrfs_root *left_root,
5429 right_root_level = right_level; 5454 right_root_level = right_level;
5430 right_path->nodes[right_level] = right_root->commit_root; 5455 right_path->nodes[right_level] = right_root->commit_root;
5431 extent_buffer_get(right_path->nodes[right_level]); 5456 extent_buffer_get(right_path->nodes[right_level]);
5432 up_read(&left_root->fs_info->commit_root_sem); 5457 up_read(&fs_info->commit_root_sem);
5433 5458
5434 if (left_level == 0) 5459 if (left_level == 0)
5435 btrfs_item_key_to_cpu(left_path->nodes[left_level], 5460 btrfs_item_key_to_cpu(left_path->nodes[left_level],
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 19b6bb2f2368..860103020ac9 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1352,9 +1352,11 @@ static inline int
1352btrfs_should_fragment_free_space(struct btrfs_root *root, 1352btrfs_should_fragment_free_space(struct btrfs_root *root,
1353 struct btrfs_block_group_cache *block_group) 1353 struct btrfs_block_group_cache *block_group)
1354{ 1354{
1355 return (btrfs_test_opt(root->fs_info, FRAGMENT_METADATA) && 1355 struct btrfs_fs_info *fs_info = root->fs_info;
1356
1357 return (btrfs_test_opt(fs_info, FRAGMENT_METADATA) &&
1356 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) || 1358 block_group->flags & BTRFS_BLOCK_GROUP_METADATA) ||
1357 (btrfs_test_opt(root->fs_info, FRAGMENT_DATA) && 1359 (btrfs_test_opt(fs_info, FRAGMENT_DATA) &&
1358 block_group->flags & BTRFS_BLOCK_GROUP_DATA); 1360 block_group->flags & BTRFS_BLOCK_GROUP_DATA);
1359} 1361}
1360#endif 1362#endif
@@ -2312,10 +2314,11 @@ static inline unsigned long btrfs_leaf_data(struct extent_buffer *l)
2312static inline unsigned int leaf_data_end(struct btrfs_root *root, 2314static inline unsigned int leaf_data_end(struct btrfs_root *root,
2313 struct extent_buffer *leaf) 2315 struct extent_buffer *leaf)
2314{ 2316{
2317 struct btrfs_fs_info *fs_info = root->fs_info;
2315 u32 nr = btrfs_header_nritems(leaf); 2318 u32 nr = btrfs_header_nritems(leaf);
2316 2319
2317 if (nr == 0) 2320 if (nr == 0)
2318 return BTRFS_LEAF_DATA_SIZE(root->fs_info); 2321 return BTRFS_LEAF_DATA_SIZE(fs_info);
2319 return btrfs_item_offset_nr(leaf, nr - 1); 2322 return btrfs_item_offset_nr(leaf, nr - 1);
2320} 2323}
2321 2324
@@ -2905,8 +2908,9 @@ static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
2905 */ 2908 */
2906static inline int btrfs_need_cleaner_sleep(struct btrfs_root *root) 2909static inline int btrfs_need_cleaner_sleep(struct btrfs_root *root)
2907{ 2910{
2908 return (root->fs_info->sb->s_flags & MS_RDONLY || 2911 struct btrfs_fs_info *fs_info = root->fs_info;
2909 btrfs_fs_closing(root->fs_info)); 2912
2913 return (fs_info->sb->s_flags & MS_RDONLY || btrfs_fs_closing(fs_info));
2910} 2914}
2911 2915
2912static inline void free_fs_info(struct btrfs_fs_info *fs_info) 2916static inline void free_fs_info(struct btrfs_fs_info *fs_info)
diff --git a/fs/btrfs/delayed-inode.c b/fs/btrfs/delayed-inode.c
index d4e07816fee0..d7d5eb989f7d 100644
--- a/fs/btrfs/delayed-inode.c
+++ b/fs/btrfs/delayed-inode.c
@@ -538,6 +538,7 @@ static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
538 struct btrfs_root *root, 538 struct btrfs_root *root,
539 struct btrfs_delayed_item *item) 539 struct btrfs_delayed_item *item)
540{ 540{
541 struct btrfs_fs_info *fs_info = root->fs_info;
541 struct btrfs_block_rsv *src_rsv; 542 struct btrfs_block_rsv *src_rsv;
542 struct btrfs_block_rsv *dst_rsv; 543 struct btrfs_block_rsv *dst_rsv;
543 u64 num_bytes; 544 u64 num_bytes;
@@ -547,12 +548,12 @@ static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
547 return 0; 548 return 0;
548 549
549 src_rsv = trans->block_rsv; 550 src_rsv = trans->block_rsv;
550 dst_rsv = &root->fs_info->delayed_block_rsv; 551 dst_rsv = &fs_info->delayed_block_rsv;
551 552
552 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 553 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
553 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); 554 ret = btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
554 if (!ret) { 555 if (!ret) {
555 trace_btrfs_space_reservation(root->fs_info, "delayed_item", 556 trace_btrfs_space_reservation(fs_info, "delayed_item",
556 item->key.objectid, 557 item->key.objectid,
557 num_bytes, 1); 558 num_bytes, 1);
558 item->bytes_reserved = num_bytes; 559 item->bytes_reserved = num_bytes;
@@ -564,13 +565,14 @@ static int btrfs_delayed_item_reserve_metadata(struct btrfs_trans_handle *trans,
564static void btrfs_delayed_item_release_metadata(struct btrfs_root *root, 565static void btrfs_delayed_item_release_metadata(struct btrfs_root *root,
565 struct btrfs_delayed_item *item) 566 struct btrfs_delayed_item *item)
566{ 567{
568 struct btrfs_fs_info *fs_info = root->fs_info;
567 struct btrfs_block_rsv *rsv; 569 struct btrfs_block_rsv *rsv;
568 570
569 if (!item->bytes_reserved) 571 if (!item->bytes_reserved)
570 return; 572 return;
571 573
572 rsv = &root->fs_info->delayed_block_rsv; 574 rsv = &fs_info->delayed_block_rsv;
573 trace_btrfs_space_reservation(root->fs_info, "delayed_item", 575 trace_btrfs_space_reservation(fs_info, "delayed_item",
574 item->key.objectid, item->bytes_reserved, 576 item->key.objectid, item->bytes_reserved,
575 0); 577 0);
576 btrfs_block_rsv_release(root, rsv, 578 btrfs_block_rsv_release(root, rsv,
@@ -583,6 +585,7 @@ static int btrfs_delayed_inode_reserve_metadata(
583 struct inode *inode, 585 struct inode *inode,
584 struct btrfs_delayed_node *node) 586 struct btrfs_delayed_node *node)
585{ 587{
588 struct btrfs_fs_info *fs_info = root->fs_info;
586 struct btrfs_block_rsv *src_rsv; 589 struct btrfs_block_rsv *src_rsv;
587 struct btrfs_block_rsv *dst_rsv; 590 struct btrfs_block_rsv *dst_rsv;
588 u64 num_bytes; 591 u64 num_bytes;
@@ -590,9 +593,9 @@ static int btrfs_delayed_inode_reserve_metadata(
590 bool release = false; 593 bool release = false;
591 594
592 src_rsv = trans->block_rsv; 595 src_rsv = trans->block_rsv;
593 dst_rsv = &root->fs_info->delayed_block_rsv; 596 dst_rsv = &fs_info->delayed_block_rsv;
594 597
595 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 598 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
596 599
597 /* 600 /*
598 * If our block_rsv is the delalloc block reserve then check and see if 601 * If our block_rsv is the delalloc block reserve then check and see if
@@ -640,7 +643,7 @@ static int btrfs_delayed_inode_reserve_metadata(
640 ret = -ENOSPC; 643 ret = -ENOSPC;
641 if (!ret) { 644 if (!ret) {
642 node->bytes_reserved = num_bytes; 645 node->bytes_reserved = num_bytes;
643 trace_btrfs_space_reservation(root->fs_info, 646 trace_btrfs_space_reservation(fs_info,
644 "delayed_inode", 647 "delayed_inode",
645 btrfs_ino(inode), 648 btrfs_ino(inode),
646 num_bytes, 1); 649 num_bytes, 1);
@@ -664,13 +667,13 @@ static int btrfs_delayed_inode_reserve_metadata(
664 * how block rsvs. work. 667 * how block rsvs. work.
665 */ 668 */
666 if (!ret) { 669 if (!ret) {
667 trace_btrfs_space_reservation(root->fs_info, "delayed_inode", 670 trace_btrfs_space_reservation(fs_info, "delayed_inode",
668 btrfs_ino(inode), num_bytes, 1); 671 btrfs_ino(inode), num_bytes, 1);
669 node->bytes_reserved = num_bytes; 672 node->bytes_reserved = num_bytes;
670 } 673 }
671 674
672 if (release) { 675 if (release) {
673 trace_btrfs_space_reservation(root->fs_info, "delalloc", 676 trace_btrfs_space_reservation(fs_info, "delalloc",
674 btrfs_ino(inode), num_bytes, 0); 677 btrfs_ino(inode), num_bytes, 0);
675 btrfs_block_rsv_release(root, src_rsv, num_bytes); 678 btrfs_block_rsv_release(root, src_rsv, num_bytes);
676 } 679 }
@@ -681,13 +684,14 @@ static int btrfs_delayed_inode_reserve_metadata(
681static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root, 684static void btrfs_delayed_inode_release_metadata(struct btrfs_root *root,
682 struct btrfs_delayed_node *node) 685 struct btrfs_delayed_node *node)
683{ 686{
687 struct btrfs_fs_info *fs_info = root->fs_info;
684 struct btrfs_block_rsv *rsv; 688 struct btrfs_block_rsv *rsv;
685 689
686 if (!node->bytes_reserved) 690 if (!node->bytes_reserved)
687 return; 691 return;
688 692
689 rsv = &root->fs_info->delayed_block_rsv; 693 rsv = &fs_info->delayed_block_rsv;
690 trace_btrfs_space_reservation(root->fs_info, "delayed_inode", 694 trace_btrfs_space_reservation(fs_info, "delayed_inode",
691 node->inode_id, node->bytes_reserved, 0); 695 node->inode_id, node->bytes_reserved, 0);
692 btrfs_block_rsv_release(root, rsv, 696 btrfs_block_rsv_release(root, rsv,
693 node->bytes_reserved); 697 node->bytes_reserved);
@@ -1140,6 +1144,7 @@ __btrfs_commit_inode_delayed_items(struct btrfs_trans_handle *trans,
1140static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans, 1144static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1141 struct btrfs_root *root, int nr) 1145 struct btrfs_root *root, int nr)
1142{ 1146{
1147 struct btrfs_fs_info *fs_info = root->fs_info;
1143 struct btrfs_delayed_root *delayed_root; 1148 struct btrfs_delayed_root *delayed_root;
1144 struct btrfs_delayed_node *curr_node, *prev_node; 1149 struct btrfs_delayed_node *curr_node, *prev_node;
1145 struct btrfs_path *path; 1150 struct btrfs_path *path;
@@ -1156,7 +1161,7 @@ static int __btrfs_run_delayed_items(struct btrfs_trans_handle *trans,
1156 path->leave_spinning = 1; 1161 path->leave_spinning = 1;
1157 1162
1158 block_rsv = trans->block_rsv; 1163 block_rsv = trans->block_rsv;
1159 trans->block_rsv = &root->fs_info->delayed_block_rsv; 1164 trans->block_rsv = &fs_info->delayed_block_rsv;
1160 1165
1161 delayed_root = btrfs_get_delayed_root(root); 1166 delayed_root = btrfs_get_delayed_root(root);
1162 1167
@@ -1860,6 +1865,7 @@ release_node:
1860 1865
1861int btrfs_delayed_delete_inode_ref(struct inode *inode) 1866int btrfs_delayed_delete_inode_ref(struct inode *inode)
1862{ 1867{
1868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1863 struct btrfs_delayed_node *delayed_node; 1869 struct btrfs_delayed_node *delayed_node;
1864 1870
1865 /* 1871 /*
@@ -1867,8 +1873,7 @@ int btrfs_delayed_delete_inode_ref(struct inode *inode)
1867 * leads to enospc problems. This means we also can't do 1873 * leads to enospc problems. This means we also can't do
1868 * delayed inode refs 1874 * delayed inode refs
1869 */ 1875 */
1870 if (test_bit(BTRFS_FS_LOG_RECOVERING, 1876 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
1871 &BTRFS_I(inode)->root->fs_info->flags))
1872 return -EAGAIN; 1877 return -EAGAIN;
1873 1878
1874 delayed_node = btrfs_get_or_create_delayed_node(inode); 1879 delayed_node = btrfs_get_or_create_delayed_node(inode);
@@ -1895,7 +1900,7 @@ int btrfs_delayed_delete_inode_ref(struct inode *inode)
1895 1900
1896 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags); 1901 set_bit(BTRFS_DELAYED_NODE_DEL_IREF, &delayed_node->flags);
1897 delayed_node->count++; 1902 delayed_node->count++;
1898 atomic_inc(&BTRFS_I(inode)->root->fs_info->delayed_root->items); 1903 atomic_inc(&fs_info->delayed_root->items);
1899release_node: 1904release_node:
1900 mutex_unlock(&delayed_node->mutex); 1905 mutex_unlock(&delayed_node->mutex);
1901 btrfs_release_delayed_node(delayed_node); 1906 btrfs_release_delayed_node(delayed_node);
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 05169ef30596..c6558ed93301 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -142,7 +142,7 @@ no_valid_dev_replace_entry_found:
142 * missing 142 * missing
143 */ 143 */
144 if (!dev_replace->srcdev && 144 if (!dev_replace->srcdev &&
145 !btrfs_test_opt(dev_root->fs_info, DEGRADED)) { 145 !btrfs_test_opt(fs_info, DEGRADED)) {
146 ret = -EIO; 146 ret = -EIO;
147 btrfs_warn(fs_info, 147 btrfs_warn(fs_info,
148 "cannot mount because device replace operation is ongoing and"); 148 "cannot mount because device replace operation is ongoing and");
@@ -151,7 +151,7 @@ no_valid_dev_replace_entry_found:
151 src_devid); 151 src_devid);
152 } 152 }
153 if (!dev_replace->tgtdev && 153 if (!dev_replace->tgtdev &&
154 !btrfs_test_opt(dev_root->fs_info, DEGRADED)) { 154 !btrfs_test_opt(fs_info, DEGRADED)) {
155 ret = -EIO; 155 ret = -EIO;
156 btrfs_warn(fs_info, 156 btrfs_warn(fs_info,
157 "cannot mount because device replace operation is ongoing and"); 157 "cannot mount because device replace operation is ongoing and");
@@ -387,7 +387,7 @@ int btrfs_dev_replace_start(struct btrfs_root *root, char *tgtdev_name,
387 if (ret) 387 if (ret)
388 btrfs_err(fs_info, "kobj add dev failed %d", ret); 388 btrfs_err(fs_info, "kobj add dev failed %d", ret);
389 389
390 btrfs_wait_ordered_roots(root->fs_info, -1, 0, (u64)-1); 390 btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
391 391
392 /* force writing the updated state information to disk */ 392 /* force writing the updated state information to disk */
393 trans = btrfs_start_transaction(root, 0); 393 trans = btrfs_start_transaction(root, 0);
@@ -501,12 +501,12 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
501 * flush all outstanding I/O and inode extent mappings before the 501 * flush all outstanding I/O and inode extent mappings before the
502 * copy operation is declared as being finished 502 * copy operation is declared as being finished
503 */ 503 */
504 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1); 504 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
505 if (ret) { 505 if (ret) {
506 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount); 506 mutex_unlock(&dev_replace->lock_finishing_cancel_unmount);
507 return ret; 507 return ret;
508 } 508 }
509 btrfs_wait_ordered_roots(root->fs_info, -1, 0, (u64)-1); 509 btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1);
510 510
511 trans = btrfs_start_transaction(root, 0); 511 trans = btrfs_start_transaction(root, 0);
512 if (IS_ERR(trans)) { 512 if (IS_ERR(trans)) {
@@ -518,8 +518,8 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
518 518
519 mutex_lock(&uuid_mutex); 519 mutex_lock(&uuid_mutex);
520 /* keep away write_all_supers() during the finishing procedure */ 520 /* keep away write_all_supers() during the finishing procedure */
521 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 521 mutex_lock(&fs_info->fs_devices->device_list_mutex);
522 mutex_lock(&root->fs_info->chunk_mutex); 522 mutex_lock(&fs_info->chunk_mutex);
523 btrfs_dev_replace_lock(dev_replace, 1); 523 btrfs_dev_replace_lock(dev_replace, 1);
524 dev_replace->replace_state = 524 dev_replace->replace_state =
525 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED 525 scrub_ret ? BTRFS_IOCTL_DEV_REPLACE_STATE_CANCELED
@@ -535,15 +535,15 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
535 src_device, 535 src_device,
536 tgt_device); 536 tgt_device);
537 } else { 537 } else {
538 btrfs_err_in_rcu(root->fs_info, 538 btrfs_err_in_rcu(fs_info,
539 "btrfs_scrub_dev(%s, %llu, %s) failed %d", 539 "btrfs_scrub_dev(%s, %llu, %s) failed %d",
540 src_device->missing ? "<missing disk>" : 540 src_device->missing ? "<missing disk>" :
541 rcu_str_deref(src_device->name), 541 rcu_str_deref(src_device->name),
542 src_device->devid, 542 src_device->devid,
543 rcu_str_deref(tgt_device->name), scrub_ret); 543 rcu_str_deref(tgt_device->name), scrub_ret);
544 btrfs_dev_replace_unlock(dev_replace, 1); 544 btrfs_dev_replace_unlock(dev_replace, 1);
545 mutex_unlock(&root->fs_info->chunk_mutex); 545 mutex_unlock(&fs_info->chunk_mutex);
546 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 546 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
547 mutex_unlock(&uuid_mutex); 547 mutex_unlock(&uuid_mutex);
548 if (tgt_device) 548 if (tgt_device)
549 btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device); 549 btrfs_destroy_dev_replace_tgtdev(fs_info, tgt_device);
@@ -552,12 +552,12 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
552 return scrub_ret; 552 return scrub_ret;
553 } 553 }
554 554
555 btrfs_info_in_rcu(root->fs_info, 555 btrfs_info_in_rcu(fs_info,
556 "dev_replace from %s (devid %llu) to %s finished", 556 "dev_replace from %s (devid %llu) to %s finished",
557 src_device->missing ? "<missing disk>" : 557 src_device->missing ? "<missing disk>" :
558 rcu_str_deref(src_device->name), 558 rcu_str_deref(src_device->name),
559 src_device->devid, 559 src_device->devid,
560 rcu_str_deref(tgt_device->name)); 560 rcu_str_deref(tgt_device->name));
561 tgt_device->is_tgtdev_for_dev_replace = 0; 561 tgt_device->is_tgtdev_for_dev_replace = 0;
562 tgt_device->devid = src_device->devid; 562 tgt_device->devid = src_device->devid;
563 src_device->devid = BTRFS_DEV_REPLACE_DEVID; 563 src_device->devid = BTRFS_DEV_REPLACE_DEVID;
@@ -592,8 +592,8 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
592 * superblock is scratched out so that it is no longer marked to 592 * superblock is scratched out so that it is no longer marked to
593 * belong to this filesystem. 593 * belong to this filesystem.
594 */ 594 */
595 mutex_unlock(&root->fs_info->chunk_mutex); 595 mutex_unlock(&fs_info->chunk_mutex);
596 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 596 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
597 mutex_unlock(&uuid_mutex); 597 mutex_unlock(&uuid_mutex);
598 598
599 /* replace the sysfs entry */ 599 /* replace the sysfs entry */
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index 79233ab69ae5..128f8ab20d41 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -451,12 +451,12 @@ int verify_dir_item(struct btrfs_root *root,
451 struct extent_buffer *leaf, 451 struct extent_buffer *leaf,
452 struct btrfs_dir_item *dir_item) 452 struct btrfs_dir_item *dir_item)
453{ 453{
454 struct btrfs_fs_info *fs_info = root->fs_info;
454 u16 namelen = BTRFS_NAME_LEN; 455 u16 namelen = BTRFS_NAME_LEN;
455 u8 type = btrfs_dir_type(leaf, dir_item); 456 u8 type = btrfs_dir_type(leaf, dir_item);
456 457
457 if (type >= BTRFS_FT_MAX) { 458 if (type >= BTRFS_FT_MAX) {
458 btrfs_crit(root->fs_info, "invalid dir item type: %d", 459 btrfs_crit(fs_info, "invalid dir item type: %d", (int)type);
459 (int)type);
460 return 1; 460 return 1;
461 } 461 }
462 462
@@ -464,16 +464,16 @@ int verify_dir_item(struct btrfs_root *root,
464 namelen = XATTR_NAME_MAX; 464 namelen = XATTR_NAME_MAX;
465 465
466 if (btrfs_dir_name_len(leaf, dir_item) > namelen) { 466 if (btrfs_dir_name_len(leaf, dir_item) > namelen) {
467 btrfs_crit(root->fs_info, "invalid dir item name len: %u", 467 btrfs_crit(fs_info, "invalid dir item name len: %u",
468 (unsigned)btrfs_dir_data_len(leaf, dir_item)); 468 (unsigned)btrfs_dir_data_len(leaf, dir_item));
469 return 1; 469 return 1;
470 } 470 }
471 471
472 /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */ 472 /* BTRFS_MAX_XATTR_SIZE is the same for all dir items */
473 if ((btrfs_dir_data_len(leaf, dir_item) + 473 if ((btrfs_dir_data_len(leaf, dir_item) +
474 btrfs_dir_name_len(leaf, dir_item)) > BTRFS_MAX_XATTR_SIZE(root->fs_info)) { 474 btrfs_dir_name_len(leaf, dir_item)) >
475 btrfs_crit(root->fs_info, 475 BTRFS_MAX_XATTR_SIZE(fs_info)) {
476 "invalid dir item name + data len: %u + %u", 476 btrfs_crit(fs_info, "invalid dir item name + data len: %u + %u",
477 (unsigned)btrfs_dir_name_len(leaf, dir_item), 477 (unsigned)btrfs_dir_name_len(leaf, dir_item),
478 (unsigned)btrfs_dir_data_len(leaf, dir_item)); 478 (unsigned)btrfs_dir_data_len(leaf, dir_item));
479 return 1; 479 return 1;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 5abf3afa6ea5..02ba794d171e 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -224,6 +224,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
224 struct page *page, size_t pg_offset, u64 start, u64 len, 224 struct page *page, size_t pg_offset, u64 start, u64 len,
225 int create) 225 int create)
226{ 226{
227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
227 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 228 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
228 struct extent_map *em; 229 struct extent_map *em;
229 int ret; 230 int ret;
@@ -231,8 +232,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
231 read_lock(&em_tree->lock); 232 read_lock(&em_tree->lock);
232 em = lookup_extent_mapping(em_tree, start, len); 233 em = lookup_extent_mapping(em_tree, start, len);
233 if (em) { 234 if (em) {
234 em->bdev = 235 em->bdev = fs_info->fs_devices->latest_bdev;
235 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
236 read_unlock(&em_tree->lock); 236 read_unlock(&em_tree->lock);
237 goto out; 237 goto out;
238 } 238 }
@@ -247,7 +247,7 @@ static struct extent_map *btree_get_extent(struct inode *inode,
247 em->len = (u64)-1; 247 em->len = (u64)-1;
248 em->block_len = (u64)-1; 248 em->block_len = (u64)-1;
249 em->block_start = 0; 249 em->block_start = 0;
250 em->bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 250 em->bdev = fs_info->fs_devices->latest_bdev;
251 251
252 write_lock(&em_tree->lock); 252 write_lock(&em_tree->lock);
253 ret = add_extent_mapping(em_tree, em, 0); 253 ret = add_extent_mapping(em_tree, em, 0);
@@ -444,6 +444,7 @@ static int btree_read_extent_buffer_pages(struct btrfs_root *root,
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;
447 struct extent_io_tree *io_tree; 448 struct extent_io_tree *io_tree;
448 int failed = 0; 449 int failed = 0;
449 int ret; 450 int ret;
@@ -452,7 +453,7 @@ static int btree_read_extent_buffer_pages(struct btrfs_root *root,
452 int failed_mirror = 0; 453 int failed_mirror = 0;
453 454
454 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); 455 clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
455 io_tree = &BTRFS_I(root->fs_info->btree_inode)->io_tree; 456 io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
456 while (1) { 457 while (1) {
457 ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE, 458 ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE,
458 btree_get_extent, mirror_num); 459 btree_get_extent, mirror_num);
@@ -472,7 +473,7 @@ static int btree_read_extent_buffer_pages(struct btrfs_root *root,
472 if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags)) 473 if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags))
473 break; 474 break;
474 475
475 num_copies = btrfs_num_copies(root->fs_info, 476 num_copies = btrfs_num_copies(fs_info,
476 eb->start, eb->len); 477 eb->start, eb->len);
477 if (num_copies == 1) 478 if (num_copies == 1)
478 break; 479 break;
@@ -545,15 +546,16 @@ static int check_tree_block_fsid(struct btrfs_fs_info *fs_info,
545 return ret; 546 return ret;
546} 547}
547 548
548#define CORRUPT(reason, eb, root, slot) \ 549#define CORRUPT(reason, eb, root, slot) \
549 btrfs_crit(root->fs_info, "corrupt %s, %s: block=%llu," \ 550 btrfs_crit(root->fs_info, \
550 " root=%llu, slot=%d", \ 551 "corrupt %s, %s: block=%llu, root=%llu, slot=%d", \
551 btrfs_header_level(eb) == 0 ? "leaf" : "node",\ 552 btrfs_header_level(eb) == 0 ? "leaf" : "node", \
552 reason, btrfs_header_bytenr(eb), root->objectid, slot) 553 reason, btrfs_header_bytenr(eb), root->objectid, slot)
553 554
554static noinline int check_leaf(struct btrfs_root *root, 555static noinline int check_leaf(struct btrfs_root *root,
555 struct extent_buffer *leaf) 556 struct extent_buffer *leaf)
556{ 557{
558 struct btrfs_fs_info *fs_info = root->fs_info;
557 struct btrfs_key key; 559 struct btrfs_key key;
558 struct btrfs_key leaf_key; 560 struct btrfs_key leaf_key;
559 u32 nritems = btrfs_header_nritems(leaf); 561 u32 nritems = btrfs_header_nritems(leaf);
@@ -566,7 +568,7 @@ static noinline int check_leaf(struct btrfs_root *root,
566 key.type = BTRFS_ROOT_ITEM_KEY; 568 key.type = BTRFS_ROOT_ITEM_KEY;
567 key.offset = (u64)-1; 569 key.offset = (u64)-1;
568 570
569 check_root = btrfs_get_fs_root(root->fs_info, &key, false); 571 check_root = btrfs_get_fs_root(fs_info, &key, false);
570 /* 572 /*
571 * The only reason we also check NULL here is that during 573 * The only reason we also check NULL here is that during
572 * open_ctree() some roots has not yet been set up. 574 * open_ctree() some roots has not yet been set up.
@@ -585,7 +587,7 @@ static noinline int check_leaf(struct btrfs_root *root,
585 587
586 /* Check the 0 item */ 588 /* Check the 0 item */
587 if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) != 589 if (btrfs_item_offset_nr(leaf, 0) + btrfs_item_size_nr(leaf, 0) !=
588 BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 590 BTRFS_LEAF_DATA_SIZE(fs_info)) {
589 CORRUPT("invalid item offset size pair", leaf, root, 0); 591 CORRUPT("invalid item offset size pair", leaf, root, 0);
590 return -EIO; 592 return -EIO;
591 } 593 }
@@ -624,7 +626,7 @@ static noinline int check_leaf(struct btrfs_root *root,
624 * all point outside of the leaf. 626 * all point outside of the leaf.
625 */ 627 */
626 if (btrfs_item_end_nr(leaf, slot) > 628 if (btrfs_item_end_nr(leaf, slot) >
627 BTRFS_LEAF_DATA_SIZE(root->fs_info)) { 629 BTRFS_LEAF_DATA_SIZE(fs_info)) {
628 CORRUPT("slot end outside of leaf", leaf, root, slot); 630 CORRUPT("slot end outside of leaf", leaf, root, slot);
629 return -EIO; 631 return -EIO;
630 } 632 }
@@ -1004,6 +1006,7 @@ static int btree_submit_bio_hook(struct inode *inode, struct bio *bio,
1004 int mirror_num, unsigned long bio_flags, 1006 int mirror_num, unsigned long bio_flags,
1005 u64 bio_offset) 1007 u64 bio_offset)
1006{ 1008{
1009 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1007 int async = check_async_write(inode, bio_flags); 1010 int async = check_async_write(inode, bio_flags);
1008 int ret; 1011 int ret;
1009 1012
@@ -1012,8 +1015,8 @@ static int btree_submit_bio_hook(struct inode *inode, struct bio *bio,
1012 * called for a read, do the setup so that checksum validation 1015 * called for a read, do the setup so that checksum validation
1013 * can happen in the async kernel threads 1016 * can happen in the async kernel threads
1014 */ 1017 */
1015 ret = btrfs_bio_wq_end_io(BTRFS_I(inode)->root->fs_info, 1018 ret = btrfs_bio_wq_end_io(fs_info, bio,
1016 bio, BTRFS_WQ_ENDIO_METADATA); 1019 BTRFS_WQ_ENDIO_METADATA);
1017 if (ret) 1020 if (ret)
1018 goto out_w_error; 1021 goto out_w_error;
1019 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 0); 1022 ret = btrfs_map_bio(BTRFS_I(inode)->root, bio, mirror_num, 0);
@@ -1027,8 +1030,7 @@ static int btree_submit_bio_hook(struct inode *inode, struct bio *bio,
1027 * kthread helpers are used to submit writes so that 1030 * kthread helpers are used to submit writes so that
1028 * checksumming can happen in parallel across all CPUs 1031 * checksumming can happen in parallel across all CPUs
1029 */ 1032 */
1030 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 1033 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num, 0,
1031 inode, bio, mirror_num, 0,
1032 bio_offset, 1034 bio_offset,
1033 __btree_submit_bio_start, 1035 __btree_submit_bio_start,
1034 __btree_submit_bio_done); 1036 __btree_submit_bio_done);
@@ -1194,9 +1196,11 @@ int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr,
1194struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root, 1196struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
1195 u64 bytenr) 1197 u64 bytenr)
1196{ 1198{
1197 if (btrfs_is_testing(root->fs_info)) 1199 struct btrfs_fs_info *fs_info = root->fs_info;
1198 return alloc_test_extent_buffer(root->fs_info, bytenr); 1200
1199 return alloc_extent_buffer(root->fs_info, bytenr); 1201 if (btrfs_is_testing(fs_info))
1202 return alloc_test_extent_buffer(fs_info, bytenr);
1203 return alloc_extent_buffer(fs_info, bytenr);
1200} 1204}
1201 1205
1202 1206
@@ -1493,7 +1497,7 @@ static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
1493 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID); 1497 btrfs_set_header_owner(leaf, BTRFS_TREE_LOG_OBJECTID);
1494 root->node = leaf; 1498 root->node = leaf;
1495 1499
1496 write_extent_buffer_fsid(root->node, root->fs_info->fsid); 1500 write_extent_buffer_fsid(root->node, fs_info->fsid);
1497 btrfs_mark_buffer_dirty(root->node); 1501 btrfs_mark_buffer_dirty(root->node);
1498 btrfs_tree_unlock(root->node); 1502 btrfs_tree_unlock(root->node);
1499 return root; 1503 return root;
@@ -1515,10 +1519,11 @@ int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
1515int btrfs_add_log_tree(struct btrfs_trans_handle *trans, 1519int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1516 struct btrfs_root *root) 1520 struct btrfs_root *root)
1517{ 1521{
1522 struct btrfs_fs_info *fs_info = root->fs_info;
1518 struct btrfs_root *log_root; 1523 struct btrfs_root *log_root;
1519 struct btrfs_inode_item *inode_item; 1524 struct btrfs_inode_item *inode_item;
1520 1525
1521 log_root = alloc_log_tree(trans, root->fs_info); 1526 log_root = alloc_log_tree(trans, fs_info);
1522 if (IS_ERR(log_root)) 1527 if (IS_ERR(log_root))
1523 return PTR_ERR(log_root); 1528 return PTR_ERR(log_root);
1524 1529
@@ -1530,7 +1535,7 @@ int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
1530 btrfs_set_stack_inode_size(inode_item, 3); 1535 btrfs_set_stack_inode_size(inode_item, 3);
1531 btrfs_set_stack_inode_nlink(inode_item, 1); 1536 btrfs_set_stack_inode_nlink(inode_item, 1);
1532 btrfs_set_stack_inode_nbytes(inode_item, 1537 btrfs_set_stack_inode_nbytes(inode_item,
1533 root->fs_info->nodesize); 1538 fs_info->nodesize);
1534 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 1539 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
1535 1540
1536 btrfs_set_root_node(&log_root->root_item, log_root->node); 1541 btrfs_set_root_node(&log_root->root_item, log_root->node);
@@ -1828,6 +1833,7 @@ static void end_workqueue_fn(struct btrfs_work *work)
1828static int cleaner_kthread(void *arg) 1833static int cleaner_kthread(void *arg)
1829{ 1834{
1830 struct btrfs_root *root = arg; 1835 struct btrfs_root *root = arg;
1836 struct btrfs_fs_info *fs_info = root->fs_info;
1831 int again; 1837 int again;
1832 struct btrfs_trans_handle *trans; 1838 struct btrfs_trans_handle *trans;
1833 1839
@@ -1842,10 +1848,10 @@ static int cleaner_kthread(void *arg)
1842 * Do not do anything if we might cause open_ctree() to block 1848 * Do not do anything if we might cause open_ctree() to block
1843 * before we have finished mounting the filesystem. 1849 * before we have finished mounting the filesystem.
1844 */ 1850 */
1845 if (!test_bit(BTRFS_FS_OPEN, &root->fs_info->flags)) 1851 if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
1846 goto sleep; 1852 goto sleep;
1847 1853
1848 if (!mutex_trylock(&root->fs_info->cleaner_mutex)) 1854 if (!mutex_trylock(&fs_info->cleaner_mutex))
1849 goto sleep; 1855 goto sleep;
1850 1856
1851 /* 1857 /*
@@ -1853,22 +1859,22 @@ static int cleaner_kthread(void *arg)
1853 * during the above check and trylock. 1859 * during the above check and trylock.
1854 */ 1860 */
1855 if (btrfs_need_cleaner_sleep(root)) { 1861 if (btrfs_need_cleaner_sleep(root)) {
1856 mutex_unlock(&root->fs_info->cleaner_mutex); 1862 mutex_unlock(&fs_info->cleaner_mutex);
1857 goto sleep; 1863 goto sleep;
1858 } 1864 }
1859 1865
1860 mutex_lock(&root->fs_info->cleaner_delayed_iput_mutex); 1866 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
1861 btrfs_run_delayed_iputs(root); 1867 btrfs_run_delayed_iputs(root);
1862 mutex_unlock(&root->fs_info->cleaner_delayed_iput_mutex); 1868 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
1863 1869
1864 again = btrfs_clean_one_deleted_snapshot(root); 1870 again = btrfs_clean_one_deleted_snapshot(root);
1865 mutex_unlock(&root->fs_info->cleaner_mutex); 1871 mutex_unlock(&fs_info->cleaner_mutex);
1866 1872
1867 /* 1873 /*
1868 * The defragger has dealt with the R/O remount and umount, 1874 * The defragger has dealt with the R/O remount and umount,
1869 * needn't do anything special here. 1875 * needn't do anything special here.
1870 */ 1876 */
1871 btrfs_run_defrag_inodes(root->fs_info); 1877 btrfs_run_defrag_inodes(fs_info);
1872 1878
1873 /* 1879 /*
1874 * Acquires fs_info->delete_unused_bgs_mutex to avoid racing 1880 * Acquires fs_info->delete_unused_bgs_mutex to avoid racing
@@ -1878,7 +1884,7 @@ static int cleaner_kthread(void *arg)
1878 * can't hold, nor need to, fs_info->cleaner_mutex when deleting 1884 * can't hold, nor need to, fs_info->cleaner_mutex when deleting
1879 * unused block groups. 1885 * unused block groups.
1880 */ 1886 */
1881 btrfs_delete_unused_bgs(root->fs_info); 1887 btrfs_delete_unused_bgs(fs_info);
1882sleep: 1888sleep:
1883 if (!again) { 1889 if (!again) {
1884 set_current_state(TASK_INTERRUPTIBLE); 1890 set_current_state(TASK_INTERRUPTIBLE);
@@ -1902,7 +1908,7 @@ sleep:
1902 trans = btrfs_attach_transaction(root); 1908 trans = btrfs_attach_transaction(root);
1903 if (IS_ERR(trans)) { 1909 if (IS_ERR(trans)) {
1904 if (PTR_ERR(trans) != -ENOENT) 1910 if (PTR_ERR(trans) != -ENOENT)
1905 btrfs_err(root->fs_info, 1911 btrfs_err(fs_info,
1906 "cleaner transaction attach returned %ld", 1912 "cleaner transaction attach returned %ld",
1907 PTR_ERR(trans)); 1913 PTR_ERR(trans));
1908 } else { 1914 } else {
@@ -1910,7 +1916,7 @@ sleep:
1910 1916
1911 ret = btrfs_commit_transaction(trans, root); 1917 ret = btrfs_commit_transaction(trans, root);
1912 if (ret) 1918 if (ret)
1913 btrfs_err(root->fs_info, 1919 btrfs_err(fs_info,
1914 "cleaner open transaction commit returned %d", 1920 "cleaner open transaction commit returned %d",
1915 ret); 1921 ret);
1916 } 1922 }
@@ -1921,6 +1927,7 @@ sleep:
1921static int transaction_kthread(void *arg) 1927static int transaction_kthread(void *arg)
1922{ 1928{
1923 struct btrfs_root *root = arg; 1929 struct btrfs_root *root = arg;
1930 struct btrfs_fs_info *fs_info = root->fs_info;
1924 struct btrfs_trans_handle *trans; 1931 struct btrfs_trans_handle *trans;
1925 struct btrfs_transaction *cur; 1932 struct btrfs_transaction *cur;
1926 u64 transid; 1933 u64 transid;
@@ -1930,26 +1937,26 @@ static int transaction_kthread(void *arg)
1930 1937
1931 do { 1938 do {
1932 cannot_commit = false; 1939 cannot_commit = false;
1933 delay = HZ * root->fs_info->commit_interval; 1940 delay = HZ * fs_info->commit_interval;
1934 mutex_lock(&root->fs_info->transaction_kthread_mutex); 1941 mutex_lock(&fs_info->transaction_kthread_mutex);
1935 1942
1936 spin_lock(&root->fs_info->trans_lock); 1943 spin_lock(&fs_info->trans_lock);
1937 cur = root->fs_info->running_transaction; 1944 cur = fs_info->running_transaction;
1938 if (!cur) { 1945 if (!cur) {
1939 spin_unlock(&root->fs_info->trans_lock); 1946 spin_unlock(&fs_info->trans_lock);
1940 goto sleep; 1947 goto sleep;
1941 } 1948 }
1942 1949
1943 now = get_seconds(); 1950 now = get_seconds();
1944 if (cur->state < TRANS_STATE_BLOCKED && 1951 if (cur->state < TRANS_STATE_BLOCKED &&
1945 (now < cur->start_time || 1952 (now < cur->start_time ||
1946 now - cur->start_time < root->fs_info->commit_interval)) { 1953 now - cur->start_time < fs_info->commit_interval)) {
1947 spin_unlock(&root->fs_info->trans_lock); 1954 spin_unlock(&fs_info->trans_lock);
1948 delay = HZ * 5; 1955 delay = HZ * 5;
1949 goto sleep; 1956 goto sleep;
1950 } 1957 }
1951 transid = cur->transid; 1958 transid = cur->transid;
1952 spin_unlock(&root->fs_info->trans_lock); 1959 spin_unlock(&fs_info->trans_lock);
1953 1960
1954 /* If the file system is aborted, this will always fail. */ 1961 /* If the file system is aborted, this will always fail. */
1955 trans = btrfs_attach_transaction(root); 1962 trans = btrfs_attach_transaction(root);
@@ -1964,15 +1971,15 @@ static int transaction_kthread(void *arg)
1964 btrfs_end_transaction(trans, root); 1971 btrfs_end_transaction(trans, root);
1965 } 1972 }
1966sleep: 1973sleep:
1967 wake_up_process(root->fs_info->cleaner_kthread); 1974 wake_up_process(fs_info->cleaner_kthread);
1968 mutex_unlock(&root->fs_info->transaction_kthread_mutex); 1975 mutex_unlock(&fs_info->transaction_kthread_mutex);
1969 1976
1970 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR, 1977 if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
1971 &root->fs_info->fs_state))) 1978 &fs_info->fs_state)))
1972 btrfs_cleanup_transaction(root); 1979 btrfs_cleanup_transaction(root);
1973 set_current_state(TASK_INTERRUPTIBLE); 1980 set_current_state(TASK_INTERRUPTIBLE);
1974 if (!kthread_should_stop() && 1981 if (!kthread_should_stop() &&
1975 (!btrfs_transaction_blocked(root->fs_info) || 1982 (!btrfs_transaction_blocked(fs_info) ||
1976 cannot_commit)) 1983 cannot_commit))
1977 schedule_timeout(delay); 1984 schedule_timeout(delay);
1978 __set_current_state(TASK_RUNNING); 1985 __set_current_state(TASK_RUNNING);
@@ -2464,8 +2471,8 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2464 /* returns with log_tree_root freed on success */ 2471 /* returns with log_tree_root freed on success */
2465 ret = btrfs_recover_log_trees(log_tree_root); 2472 ret = btrfs_recover_log_trees(log_tree_root);
2466 if (ret) { 2473 if (ret) {
2467 btrfs_handle_fs_error(tree_root->fs_info, ret, 2474 btrfs_handle_fs_error(fs_info, ret,
2468 "Failed to recover log tree"); 2475 "Failed to recover log tree");
2469 free_extent_buffer(log_tree_root->node); 2476 free_extent_buffer(log_tree_root->node);
2470 kfree(log_tree_root); 2477 kfree(log_tree_root);
2471 return ret; 2478 return ret;
@@ -2830,7 +2837,7 @@ int open_ctree(struct super_block *sb,
2830 2837
2831 features = btrfs_super_incompat_flags(disk_super); 2838 features = btrfs_super_incompat_flags(disk_super);
2832 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 2839 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
2833 if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2840 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
2834 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; 2841 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
2835 2842
2836 if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA) 2843 if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
@@ -3059,8 +3066,8 @@ retry_root_backup:
3059 if (IS_ERR(fs_info->transaction_kthread)) 3066 if (IS_ERR(fs_info->transaction_kthread))
3060 goto fail_cleaner; 3067 goto fail_cleaner;
3061 3068
3062 if (!btrfs_test_opt(tree_root->fs_info, SSD) && 3069 if (!btrfs_test_opt(fs_info, SSD) &&
3063 !btrfs_test_opt(tree_root->fs_info, NOSSD) && 3070 !btrfs_test_opt(fs_info, NOSSD) &&
3064 !fs_info->fs_devices->rotating) { 3071 !fs_info->fs_devices->rotating) {
3065 btrfs_info(fs_info, "detected SSD devices, enabling SSD mode"); 3072 btrfs_info(fs_info, "detected SSD devices, enabling SSD mode");
3066 btrfs_set_opt(fs_info->mount_opt, SSD); 3073 btrfs_set_opt(fs_info->mount_opt, SSD);
@@ -3073,9 +3080,9 @@ retry_root_backup:
3073 btrfs_apply_pending_changes(fs_info); 3080 btrfs_apply_pending_changes(fs_info);
3074 3081
3075#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 3082#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3076 if (btrfs_test_opt(tree_root->fs_info, CHECK_INTEGRITY)) { 3083 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
3077 ret = btrfsic_mount(tree_root, fs_devices, 3084 ret = btrfsic_mount(tree_root, fs_devices,
3078 btrfs_test_opt(tree_root->fs_info, 3085 btrfs_test_opt(fs_info,
3079 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ? 3086 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
3080 1 : 0, 3087 1 : 0,
3081 fs_info->check_integrity_print_mask); 3088 fs_info->check_integrity_print_mask);
@@ -3091,7 +3098,7 @@ retry_root_backup:
3091 3098
3092 /* do not make disk changes in broken FS or nologreplay is given */ 3099 /* do not make disk changes in broken FS or nologreplay is given */
3093 if (btrfs_super_log_root(disk_super) != 0 && 3100 if (btrfs_super_log_root(disk_super) != 0 &&
3094 !btrfs_test_opt(tree_root->fs_info, NOLOGREPLAY)) { 3101 !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
3095 ret = btrfs_replay_log(fs_info, fs_devices); 3102 ret = btrfs_replay_log(fs_info, fs_devices);
3096 if (ret) { 3103 if (ret) {
3097 err = ret; 3104 err = ret;
@@ -3152,7 +3159,7 @@ retry_root_backup:
3152 } 3159 }
3153 } 3160 }
3154 3161
3155 if (btrfs_test_opt(tree_root->fs_info, FREE_SPACE_TREE) && 3162 if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
3156 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) { 3163 !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
3157 btrfs_info(fs_info, "creating free space tree"); 3164 btrfs_info(fs_info, "creating free space tree");
3158 ret = btrfs_create_free_space_tree(fs_info); 3165 ret = btrfs_create_free_space_tree(fs_info);
@@ -3198,7 +3205,7 @@ retry_root_backup:
3198 close_ctree(fs_info); 3205 close_ctree(fs_info);
3199 return ret; 3206 return ret;
3200 } 3207 }
3201 } else if (btrfs_test_opt(tree_root->fs_info, RESCAN_UUID_TREE) || 3208 } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
3202 fs_info->generation != 3209 fs_info->generation !=
3203 btrfs_super_uuid_tree_generation(disk_super)) { 3210 btrfs_super_uuid_tree_generation(disk_super)) {
3204 btrfs_info(fs_info, "checking UUID tree"); 3211 btrfs_info(fs_info, "checking UUID tree");
@@ -3274,7 +3281,7 @@ fail:
3274 return err; 3281 return err;
3275 3282
3276recovery_tree_root: 3283recovery_tree_root:
3277 if (!btrfs_test_opt(tree_root->fs_info, USEBACKUPROOT)) 3284 if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
3278 goto fail_tree_roots; 3285 goto fail_tree_roots;
3279 3286
3280 free_root_pointers(fs_info, 0); 3287 free_root_pointers(fs_info, 0);
@@ -3680,6 +3687,7 @@ int btrfs_calc_num_tolerated_disk_barrier_failures(
3680 3687
3681static int write_all_supers(struct btrfs_root *root, int max_mirrors) 3688static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3682{ 3689{
3690 struct btrfs_fs_info *fs_info = root->fs_info;
3683 struct list_head *head; 3691 struct list_head *head;
3684 struct btrfs_device *dev; 3692 struct btrfs_device *dev;
3685 struct btrfs_super_block *sb; 3693 struct btrfs_super_block *sb;
@@ -3690,23 +3698,23 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3690 int total_errors = 0; 3698 int total_errors = 0;
3691 u64 flags; 3699 u64 flags;
3692 3700
3693 do_barriers = !btrfs_test_opt(root->fs_info, NOBARRIER); 3701 do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
3694 backup_super_roots(root->fs_info); 3702 backup_super_roots(fs_info);
3695 3703
3696 sb = root->fs_info->super_for_commit; 3704 sb = fs_info->super_for_commit;
3697 dev_item = &sb->dev_item; 3705 dev_item = &sb->dev_item;
3698 3706
3699 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 3707 mutex_lock(&fs_info->fs_devices->device_list_mutex);
3700 head = &root->fs_info->fs_devices->devices; 3708 head = &fs_info->fs_devices->devices;
3701 max_errors = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 3709 max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
3702 3710
3703 if (do_barriers) { 3711 if (do_barriers) {
3704 ret = barrier_all_devices(root->fs_info); 3712 ret = barrier_all_devices(fs_info);
3705 if (ret) { 3713 if (ret) {
3706 mutex_unlock( 3714 mutex_unlock(
3707 &root->fs_info->fs_devices->device_list_mutex); 3715 &fs_info->fs_devices->device_list_mutex);
3708 btrfs_handle_fs_error(root->fs_info, ret, 3716 btrfs_handle_fs_error(fs_info, ret,
3709 "errors while submitting device barriers."); 3717 "errors while submitting device barriers.");
3710 return ret; 3718 return ret;
3711 } 3719 }
3712 } 3720 }
@@ -3740,13 +3748,14 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3740 total_errors++; 3748 total_errors++;
3741 } 3749 }
3742 if (total_errors > max_errors) { 3750 if (total_errors > max_errors) {
3743 btrfs_err(root->fs_info, "%d errors while writing supers", 3751 btrfs_err(fs_info, "%d errors while writing supers",
3744 total_errors); 3752 total_errors);
3745 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3753 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3746 3754
3747 /* FUA is masked off if unsupported and can't be the reason */ 3755 /* FUA is masked off if unsupported and can't be the reason */
3748 btrfs_handle_fs_error(root->fs_info, -EIO, 3756 btrfs_handle_fs_error(fs_info, -EIO,
3749 "%d errors while writing supers", total_errors); 3757 "%d errors while writing supers",
3758 total_errors);
3750 return -EIO; 3759 return -EIO;
3751 } 3760 }
3752 3761
@@ -3761,10 +3770,11 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3761 if (ret) 3770 if (ret)
3762 total_errors++; 3771 total_errors++;
3763 } 3772 }
3764 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3773 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
3765 if (total_errors > max_errors) { 3774 if (total_errors > max_errors) {
3766 btrfs_handle_fs_error(root->fs_info, -EIO, 3775 btrfs_handle_fs_error(fs_info, -EIO,
3767 "%d errors while writing supers", total_errors); 3776 "%d errors while writing supers",
3777 total_errors);
3768 return -EIO; 3778 return -EIO;
3769 } 3779 }
3770 return 0; 3780 return 0;
@@ -3884,14 +3894,14 @@ int btrfs_commit_super(struct btrfs_fs_info *fs_info)
3884 struct btrfs_root *root = fs_info->tree_root; 3894 struct btrfs_root *root = fs_info->tree_root;
3885 struct btrfs_trans_handle *trans; 3895 struct btrfs_trans_handle *trans;
3886 3896
3887 mutex_lock(&root->fs_info->cleaner_mutex); 3897 mutex_lock(&fs_info->cleaner_mutex);
3888 btrfs_run_delayed_iputs(root); 3898 btrfs_run_delayed_iputs(root);
3889 mutex_unlock(&root->fs_info->cleaner_mutex); 3899 mutex_unlock(&fs_info->cleaner_mutex);
3890 wake_up_process(root->fs_info->cleaner_kthread); 3900 wake_up_process(fs_info->cleaner_kthread);
3891 3901
3892 /* wait until ongoing cleanup work done */ 3902 /* wait until ongoing cleanup work done */
3893 down_write(&root->fs_info->cleanup_work_sem); 3903 down_write(&fs_info->cleanup_work_sem);
3894 up_write(&root->fs_info->cleanup_work_sem); 3904 up_write(&fs_info->cleanup_work_sem);
3895 3905
3896 trans = btrfs_join_transaction(root); 3906 trans = btrfs_join_transaction(root);
3897 if (IS_ERR(trans)) 3907 if (IS_ERR(trans))
@@ -3936,7 +3946,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
3936 * block groups queued for removal, the deletion will be 3946 * block groups queued for removal, the deletion will be
3937 * skipped when we quit the cleaner thread. 3947 * skipped when we quit the cleaner thread.
3938 */ 3948 */
3939 btrfs_delete_unused_bgs(root->fs_info); 3949 btrfs_delete_unused_bgs(fs_info);
3940 3950
3941 ret = btrfs_commit_super(fs_info); 3951 ret = btrfs_commit_super(fs_info);
3942 if (ret) 3952 if (ret)
@@ -3980,7 +3990,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
3980 iput(fs_info->btree_inode); 3990 iput(fs_info->btree_inode);
3981 3991
3982#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 3992#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
3983 if (btrfs_test_opt(root->fs_info, CHECK_INTEGRITY)) 3993 if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
3984 btrfsic_unmount(root, fs_info->fs_devices); 3994 btrfsic_unmount(root, fs_info->fs_devices);
3985#endif 3995#endif
3986 3996
@@ -3998,7 +4008,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
3998 __btrfs_free_block_rsv(root->orphan_block_rsv); 4008 __btrfs_free_block_rsv(root->orphan_block_rsv);
3999 root->orphan_block_rsv = NULL; 4009 root->orphan_block_rsv = NULL;
4000 4010
4001 lock_chunks(root->fs_info); 4011 lock_chunks(fs_info);
4002 while (!list_empty(&fs_info->pinned_chunks)) { 4012 while (!list_empty(&fs_info->pinned_chunks)) {
4003 struct extent_map *em; 4013 struct extent_map *em;
4004 4014
@@ -4007,7 +4017,7 @@ void close_ctree(struct btrfs_fs_info *fs_info)
4007 list_del_init(&em->list); 4017 list_del_init(&em->list);
4008 free_extent_map(em); 4018 free_extent_map(em);
4009 } 4019 }
4010 unlock_chunks(root->fs_info); 4020 unlock_chunks(fs_info);
4011} 4021}
4012 4022
4013int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid, 4023int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
@@ -4029,6 +4039,7 @@ int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
4029 4039
4030void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 4040void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4031{ 4041{
4042 struct btrfs_fs_info *fs_info;
4032 struct btrfs_root *root; 4043 struct btrfs_root *root;
4033 u64 transid = btrfs_header_generation(buf); 4044 u64 transid = btrfs_header_generation(buf);
4034 int was_dirty; 4045 int was_dirty;
@@ -4043,15 +4054,16 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4043 return; 4054 return;
4044#endif 4055#endif
4045 root = BTRFS_I(buf->pages[0]->mapping->host)->root; 4056 root = BTRFS_I(buf->pages[0]->mapping->host)->root;
4057 fs_info = root->fs_info;
4046 btrfs_assert_tree_locked(buf); 4058 btrfs_assert_tree_locked(buf);
4047 if (transid != root->fs_info->generation) 4059 if (transid != fs_info->generation)
4048 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n", 4060 WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
4049 buf->start, transid, root->fs_info->generation); 4061 buf->start, transid, fs_info->generation);
4050 was_dirty = set_extent_buffer_dirty(buf); 4062 was_dirty = set_extent_buffer_dirty(buf);
4051 if (!was_dirty) 4063 if (!was_dirty)
4052 __percpu_counter_add(&root->fs_info->dirty_metadata_bytes, 4064 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
4053 buf->len, 4065 buf->len,
4054 root->fs_info->dirty_metadata_batch); 4066 fs_info->dirty_metadata_batch);
4055#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 4067#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
4056 if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) { 4068 if (btrfs_header_level(buf) == 0 && check_leaf(root, buf)) {
4057 btrfs_print_leaf(root, buf); 4069 btrfs_print_leaf(root, buf);
@@ -4063,6 +4075,7 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
4063static void __btrfs_btree_balance_dirty(struct btrfs_root *root, 4075static void __btrfs_btree_balance_dirty(struct btrfs_root *root,
4064 int flush_delayed) 4076 int flush_delayed)
4065{ 4077{
4078 struct btrfs_fs_info *fs_info = root->fs_info;
4066 /* 4079 /*
4067 * looks as though older kernels can get into trouble with 4080 * looks as though older kernels can get into trouble with
4068 * this code, they end up stuck in balance_dirty_pages forever 4081 * this code, they end up stuck in balance_dirty_pages forever
@@ -4075,11 +4088,10 @@ static void __btrfs_btree_balance_dirty(struct btrfs_root *root,
4075 if (flush_delayed) 4088 if (flush_delayed)
4076 btrfs_balance_delayed_items(root); 4089 btrfs_balance_delayed_items(root);
4077 4090
4078 ret = percpu_counter_compare(&root->fs_info->dirty_metadata_bytes, 4091 ret = percpu_counter_compare(&fs_info->dirty_metadata_bytes,
4079 BTRFS_DIRTY_METADATA_THRESH); 4092 BTRFS_DIRTY_METADATA_THRESH);
4080 if (ret > 0) { 4093 if (ret > 0) {
4081 balance_dirty_pages_ratelimited( 4094 balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
4082 root->fs_info->btree_inode->i_mapping);
4083 } 4095 }
4084} 4096}
4085 4097
@@ -4249,12 +4261,14 @@ static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
4249 4261
4250static void btrfs_error_commit_super(struct btrfs_root *root) 4262static void btrfs_error_commit_super(struct btrfs_root *root)
4251{ 4263{
4252 mutex_lock(&root->fs_info->cleaner_mutex); 4264 struct btrfs_fs_info *fs_info = root->fs_info;
4265
4266 mutex_lock(&fs_info->cleaner_mutex);
4253 btrfs_run_delayed_iputs(root); 4267 btrfs_run_delayed_iputs(root);
4254 mutex_unlock(&root->fs_info->cleaner_mutex); 4268 mutex_unlock(&fs_info->cleaner_mutex);
4255 4269
4256 down_write(&root->fs_info->cleanup_work_sem); 4270 down_write(&fs_info->cleanup_work_sem);
4257 up_write(&root->fs_info->cleanup_work_sem); 4271 up_write(&fs_info->cleanup_work_sem);
4258 4272
4259 /* cleanup FS via transaction */ 4273 /* cleanup FS via transaction */
4260 btrfs_cleanup_transaction(root); 4274 btrfs_cleanup_transaction(root);
@@ -4302,6 +4316,7 @@ static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
4302static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans, 4316static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4303 struct btrfs_root *root) 4317 struct btrfs_root *root)
4304{ 4318{
4319 struct btrfs_fs_info *fs_info = root->fs_info;
4305 struct rb_node *node; 4320 struct rb_node *node;
4306 struct btrfs_delayed_ref_root *delayed_refs; 4321 struct btrfs_delayed_ref_root *delayed_refs;
4307 struct btrfs_delayed_ref_node *ref; 4322 struct btrfs_delayed_ref_node *ref;
@@ -4312,7 +4327,7 @@ static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
4312 spin_lock(&delayed_refs->lock); 4327 spin_lock(&delayed_refs->lock);
4313 if (atomic_read(&delayed_refs->num_entries) == 0) { 4328 if (atomic_read(&delayed_refs->num_entries) == 0) {
4314 spin_unlock(&delayed_refs->lock); 4329 spin_unlock(&delayed_refs->lock);
4315 btrfs_info(root->fs_info, "delayed_refs has NO entry"); 4330 btrfs_info(fs_info, "delayed_refs has NO entry");
4316 return ret; 4331 return ret;
4317 } 4332 }
4318 4333
@@ -4425,6 +4440,7 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
4425 struct extent_io_tree *dirty_pages, 4440 struct extent_io_tree *dirty_pages,
4426 int mark) 4441 int mark)
4427{ 4442{
4443 struct btrfs_fs_info *fs_info = root->fs_info;
4428 int ret; 4444 int ret;
4429 struct extent_buffer *eb; 4445 struct extent_buffer *eb;
4430 u64 start = 0; 4446 u64 start = 0;
@@ -4438,8 +4454,8 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
4438 4454
4439 clear_extent_bits(dirty_pages, start, end, mark); 4455 clear_extent_bits(dirty_pages, start, end, mark);
4440 while (start <= end) { 4456 while (start <= end) {
4441 eb = find_extent_buffer(root->fs_info, start); 4457 eb = find_extent_buffer(fs_info, start);
4442 start += root->fs_info->nodesize; 4458 start += fs_info->nodesize;
4443 if (!eb) 4459 if (!eb)
4444 continue; 4460 continue;
4445 wait_on_extent_buffer_writeback(eb); 4461 wait_on_extent_buffer_writeback(eb);
@@ -4457,6 +4473,7 @@ static int btrfs_destroy_marked_extents(struct btrfs_root *root,
4457static int btrfs_destroy_pinned_extent(struct btrfs_root *root, 4473static int btrfs_destroy_pinned_extent(struct btrfs_root *root,
4458 struct extent_io_tree *pinned_extents) 4474 struct extent_io_tree *pinned_extents)
4459{ 4475{
4476 struct btrfs_fs_info *fs_info = root->fs_info;
4460 struct extent_io_tree *unpin; 4477 struct extent_io_tree *unpin;
4461 u64 start; 4478 u64 start;
4462 u64 end; 4479 u64 end;
@@ -4477,10 +4494,10 @@ again:
4477 } 4494 }
4478 4495
4479 if (loop) { 4496 if (loop) {
4480 if (unpin == &root->fs_info->freed_extents[0]) 4497 if (unpin == &fs_info->freed_extents[0])
4481 unpin = &root->fs_info->freed_extents[1]; 4498 unpin = &fs_info->freed_extents[1];
4482 else 4499 else
4483 unpin = &root->fs_info->freed_extents[0]; 4500 unpin = &fs_info->freed_extents[0];
4484 loop = false; 4501 loop = false;
4485 goto again; 4502 goto again;
4486 } 4503 }
@@ -4505,6 +4522,7 @@ static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
4505void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans, 4522void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4506 struct btrfs_root *root) 4523 struct btrfs_root *root)
4507{ 4524{
4525 struct btrfs_fs_info *fs_info = root->fs_info;
4508 struct btrfs_block_group_cache *cache; 4526 struct btrfs_block_group_cache *cache;
4509 4527
4510 spin_lock(&cur_trans->dirty_bgs_lock); 4528 spin_lock(&cur_trans->dirty_bgs_lock);
@@ -4513,8 +4531,7 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4513 struct btrfs_block_group_cache, 4531 struct btrfs_block_group_cache,
4514 dirty_list); 4532 dirty_list);
4515 if (!cache) { 4533 if (!cache) {
4516 btrfs_err(root->fs_info, 4534 btrfs_err(fs_info, "orphan block group dirty_bgs list");
4517 "orphan block group dirty_bgs list");
4518 spin_unlock(&cur_trans->dirty_bgs_lock); 4535 spin_unlock(&cur_trans->dirty_bgs_lock);
4519 return; 4536 return;
4520 } 4537 }
@@ -4542,8 +4559,7 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4542 struct btrfs_block_group_cache, 4559 struct btrfs_block_group_cache,
4543 io_list); 4560 io_list);
4544 if (!cache) { 4561 if (!cache) {
4545 btrfs_err(root->fs_info, 4562 btrfs_err(fs_info, "orphan block group on io_bgs list");
4546 "orphan block group on io_bgs list");
4547 return; 4563 return;
4548 } 4564 }
4549 4565
@@ -4558,6 +4574,7 @@ void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
4558void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans, 4574void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4559 struct btrfs_root *root) 4575 struct btrfs_root *root)
4560{ 4576{
4577 struct btrfs_fs_info *fs_info = root->fs_info;
4561 btrfs_cleanup_dirty_bgs(cur_trans, root); 4578 btrfs_cleanup_dirty_bgs(cur_trans, root);
4562 ASSERT(list_empty(&cur_trans->dirty_bgs)); 4579 ASSERT(list_empty(&cur_trans->dirty_bgs));
4563 ASSERT(list_empty(&cur_trans->io_bgs)); 4580 ASSERT(list_empty(&cur_trans->io_bgs));
@@ -4565,10 +4582,10 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4565 btrfs_destroy_delayed_refs(cur_trans, root); 4582 btrfs_destroy_delayed_refs(cur_trans, root);
4566 4583
4567 cur_trans->state = TRANS_STATE_COMMIT_START; 4584 cur_trans->state = TRANS_STATE_COMMIT_START;
4568 wake_up(&root->fs_info->transaction_blocked_wait); 4585 wake_up(&fs_info->transaction_blocked_wait);
4569 4586
4570 cur_trans->state = TRANS_STATE_UNBLOCKED; 4587 cur_trans->state = TRANS_STATE_UNBLOCKED;
4571 wake_up(&root->fs_info->transaction_wait); 4588 wake_up(&fs_info->transaction_wait);
4572 4589
4573 btrfs_destroy_delayed_inodes(root); 4590 btrfs_destroy_delayed_inodes(root);
4574 btrfs_assert_delayed_root_empty(root); 4591 btrfs_assert_delayed_root_empty(root);
@@ -4576,7 +4593,7 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4576 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages, 4593 btrfs_destroy_marked_extents(root, &cur_trans->dirty_pages,
4577 EXTENT_DIRTY); 4594 EXTENT_DIRTY);
4578 btrfs_destroy_pinned_extent(root, 4595 btrfs_destroy_pinned_extent(root,
4579 root->fs_info->pinned_extents); 4596 fs_info->pinned_extents);
4580 4597
4581 cur_trans->state =TRANS_STATE_COMPLETED; 4598 cur_trans->state =TRANS_STATE_COMPLETED;
4582 wake_up(&cur_trans->commit_wait); 4599 wake_up(&cur_trans->commit_wait);
@@ -4589,25 +4606,26 @@ void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
4589 4606
4590static int btrfs_cleanup_transaction(struct btrfs_root *root) 4607static int btrfs_cleanup_transaction(struct btrfs_root *root)
4591{ 4608{
4609 struct btrfs_fs_info *fs_info = root->fs_info;
4592 struct btrfs_transaction *t; 4610 struct btrfs_transaction *t;
4593 4611
4594 mutex_lock(&root->fs_info->transaction_kthread_mutex); 4612 mutex_lock(&fs_info->transaction_kthread_mutex);
4595 4613
4596 spin_lock(&root->fs_info->trans_lock); 4614 spin_lock(&fs_info->trans_lock);
4597 while (!list_empty(&root->fs_info->trans_list)) { 4615 while (!list_empty(&fs_info->trans_list)) {
4598 t = list_first_entry(&root->fs_info->trans_list, 4616 t = list_first_entry(&fs_info->trans_list,
4599 struct btrfs_transaction, list); 4617 struct btrfs_transaction, list);
4600 if (t->state >= TRANS_STATE_COMMIT_START) { 4618 if (t->state >= TRANS_STATE_COMMIT_START) {
4601 atomic_inc(&t->use_count); 4619 atomic_inc(&t->use_count);
4602 spin_unlock(&root->fs_info->trans_lock); 4620 spin_unlock(&fs_info->trans_lock);
4603 btrfs_wait_for_commit(root, t->transid); 4621 btrfs_wait_for_commit(root, t->transid);
4604 btrfs_put_transaction(t); 4622 btrfs_put_transaction(t);
4605 spin_lock(&root->fs_info->trans_lock); 4623 spin_lock(&fs_info->trans_lock);
4606 continue; 4624 continue;
4607 } 4625 }
4608 if (t == root->fs_info->running_transaction) { 4626 if (t == fs_info->running_transaction) {
4609 t->state = TRANS_STATE_COMMIT_DOING; 4627 t->state = TRANS_STATE_COMMIT_DOING;
4610 spin_unlock(&root->fs_info->trans_lock); 4628 spin_unlock(&fs_info->trans_lock);
4611 /* 4629 /*
4612 * We wait for 0 num_writers since we don't hold a trans 4630 * We wait for 0 num_writers since we don't hold a trans
4613 * handle open currently for this transaction. 4631 * handle open currently for this transaction.
@@ -4615,27 +4633,27 @@ static int btrfs_cleanup_transaction(struct btrfs_root *root)
4615 wait_event(t->writer_wait, 4633 wait_event(t->writer_wait,
4616 atomic_read(&t->num_writers) == 0); 4634 atomic_read(&t->num_writers) == 0);
4617 } else { 4635 } else {
4618 spin_unlock(&root->fs_info->trans_lock); 4636 spin_unlock(&fs_info->trans_lock);
4619 } 4637 }
4620 btrfs_cleanup_one_transaction(t, root); 4638 btrfs_cleanup_one_transaction(t, root);
4621 4639
4622 spin_lock(&root->fs_info->trans_lock); 4640 spin_lock(&fs_info->trans_lock);
4623 if (t == root->fs_info->running_transaction) 4641 if (t == fs_info->running_transaction)
4624 root->fs_info->running_transaction = NULL; 4642 fs_info->running_transaction = NULL;
4625 list_del_init(&t->list); 4643 list_del_init(&t->list);
4626 spin_unlock(&root->fs_info->trans_lock); 4644 spin_unlock(&fs_info->trans_lock);
4627 4645
4628 btrfs_put_transaction(t); 4646 btrfs_put_transaction(t);
4629 trace_btrfs_transaction_commit(root); 4647 trace_btrfs_transaction_commit(root);
4630 spin_lock(&root->fs_info->trans_lock); 4648 spin_lock(&fs_info->trans_lock);
4631 } 4649 }
4632 spin_unlock(&root->fs_info->trans_lock); 4650 spin_unlock(&fs_info->trans_lock);
4633 btrfs_destroy_all_ordered_extents(root->fs_info); 4651 btrfs_destroy_all_ordered_extents(fs_info);
4634 btrfs_destroy_delayed_inodes(root); 4652 btrfs_destroy_delayed_inodes(root);
4635 btrfs_assert_delayed_root_empty(root); 4653 btrfs_assert_delayed_root_empty(root);
4636 btrfs_destroy_pinned_extent(root, root->fs_info->pinned_extents); 4654 btrfs_destroy_pinned_extent(root, fs_info->pinned_extents);
4637 btrfs_destroy_all_delalloc_inodes(root->fs_info); 4655 btrfs_destroy_all_delalloc_inodes(fs_info);
4638 mutex_unlock(&root->fs_info->transaction_kthread_mutex); 4656 mutex_unlock(&fs_info->transaction_kthread_mutex);
4639 4657
4640 return 0; 4658 return 0;
4641} 4659}
diff --git a/fs/btrfs/export.c b/fs/btrfs/export.c
index 2513a7f53334..340d90751263 100644
--- a/fs/btrfs/export.c
+++ b/fs/btrfs/export.c
@@ -153,6 +153,7 @@ static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
153static struct dentry *btrfs_get_parent(struct dentry *child) 153static struct dentry *btrfs_get_parent(struct dentry *child)
154{ 154{
155 struct inode *dir = d_inode(child); 155 struct inode *dir = d_inode(child);
156 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
156 struct btrfs_root *root = BTRFS_I(dir)->root; 157 struct btrfs_root *root = BTRFS_I(dir)->root;
157 struct btrfs_path *path; 158 struct btrfs_path *path;
158 struct extent_buffer *leaf; 159 struct extent_buffer *leaf;
@@ -169,7 +170,7 @@ static struct dentry *btrfs_get_parent(struct dentry *child)
169 key.objectid = root->root_key.objectid; 170 key.objectid = root->root_key.objectid;
170 key.type = BTRFS_ROOT_BACKREF_KEY; 171 key.type = BTRFS_ROOT_BACKREF_KEY;
171 key.offset = (u64)-1; 172 key.offset = (u64)-1;
172 root = root->fs_info->tree_root; 173 root = fs_info->tree_root;
173 } else { 174 } else {
174 key.objectid = btrfs_ino(dir); 175 key.objectid = btrfs_ino(dir);
175 key.type = BTRFS_INODE_REF_KEY; 176 key.type = BTRFS_INODE_REF_KEY;
@@ -205,13 +206,13 @@ static struct dentry *btrfs_get_parent(struct dentry *child)
205 btrfs_free_path(path); 206 btrfs_free_path(path);
206 207
207 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) { 208 if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
208 return btrfs_get_dentry(root->fs_info->sb, key.objectid, 209 return btrfs_get_dentry(fs_info->sb, key.objectid,
209 found_key.offset, 0, 0); 210 found_key.offset, 0, 0);
210 } 211 }
211 212
212 key.type = BTRFS_INODE_ITEM_KEY; 213 key.type = BTRFS_INODE_ITEM_KEY;
213 key.offset = 0; 214 key.offset = 0;
214 return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL)); 215 return d_obtain_alias(btrfs_iget(fs_info->sb, &key, root, NULL));
215fail: 216fail:
216 btrfs_free_path(path); 217 btrfs_free_path(path);
217 return ERR_PTR(ret); 218 return ERR_PTR(ret);
@@ -222,6 +223,7 @@ static int btrfs_get_name(struct dentry *parent, char *name,
222{ 223{
223 struct inode *inode = d_inode(child); 224 struct inode *inode = d_inode(child);
224 struct inode *dir = d_inode(parent); 225 struct inode *dir = d_inode(parent);
226 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
225 struct btrfs_path *path; 227 struct btrfs_path *path;
226 struct btrfs_root *root = BTRFS_I(dir)->root; 228 struct btrfs_root *root = BTRFS_I(dir)->root;
227 struct btrfs_inode_ref *iref; 229 struct btrfs_inode_ref *iref;
@@ -250,7 +252,7 @@ static int btrfs_get_name(struct dentry *parent, char *name,
250 key.objectid = BTRFS_I(inode)->root->root_key.objectid; 252 key.objectid = BTRFS_I(inode)->root->root_key.objectid;
251 key.type = BTRFS_ROOT_BACKREF_KEY; 253 key.type = BTRFS_ROOT_BACKREF_KEY;
252 key.offset = (u64)-1; 254 key.offset = (u64)-1;
253 root = root->fs_info->tree_root; 255 root = fs_info->tree_root;
254 } else { 256 } else {
255 key.objectid = ino; 257 key.objectid = ino;
256 key.offset = btrfs_ino(dir); 258 key.offset = btrfs_ino(dir);
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 2e395d46ba9c..03512c6f4964 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -226,10 +226,11 @@ block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
226static int add_excluded_extent(struct btrfs_root *root, 226static int add_excluded_extent(struct btrfs_root *root,
227 u64 start, u64 num_bytes) 227 u64 start, u64 num_bytes)
228{ 228{
229 struct btrfs_fs_info *fs_info = root->fs_info;
229 u64 end = start + num_bytes - 1; 230 u64 end = start + num_bytes - 1;
230 set_extent_bits(&root->fs_info->freed_extents[0], 231 set_extent_bits(&fs_info->freed_extents[0],
231 start, end, EXTENT_UPTODATE); 232 start, end, EXTENT_UPTODATE);
232 set_extent_bits(&root->fs_info->freed_extents[1], 233 set_extent_bits(&fs_info->freed_extents[1],
233 start, end, EXTENT_UPTODATE); 234 start, end, EXTENT_UPTODATE);
234 return 0; 235 return 0;
235} 236}
@@ -237,20 +238,22 @@ static int add_excluded_extent(struct btrfs_root *root,
237static void free_excluded_extents(struct btrfs_root *root, 238static void free_excluded_extents(struct btrfs_root *root,
238 struct btrfs_block_group_cache *cache) 239 struct btrfs_block_group_cache *cache)
239{ 240{
241 struct btrfs_fs_info *fs_info = root->fs_info;
240 u64 start, end; 242 u64 start, end;
241 243
242 start = cache->key.objectid; 244 start = cache->key.objectid;
243 end = start + cache->key.offset - 1; 245 end = start + cache->key.offset - 1;
244 246
245 clear_extent_bits(&root->fs_info->freed_extents[0], 247 clear_extent_bits(&fs_info->freed_extents[0],
246 start, end, EXTENT_UPTODATE); 248 start, end, EXTENT_UPTODATE);
247 clear_extent_bits(&root->fs_info->freed_extents[1], 249 clear_extent_bits(&fs_info->freed_extents[1],
248 start, end, EXTENT_UPTODATE); 250 start, end, EXTENT_UPTODATE);
249} 251}
250 252
251static int exclude_super_stripes(struct btrfs_root *root, 253static int exclude_super_stripes(struct btrfs_root *root,
252 struct btrfs_block_group_cache *cache) 254 struct btrfs_block_group_cache *cache)
253{ 255{
256 struct btrfs_fs_info *fs_info = root->fs_info;
254 u64 bytenr; 257 u64 bytenr;
255 u64 *logical; 258 u64 *logical;
256 int stripe_len; 259 int stripe_len;
@@ -267,7 +270,7 @@ static int exclude_super_stripes(struct btrfs_root *root,
267 270
268 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 271 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
269 bytenr = btrfs_sb_offset(i); 272 bytenr = btrfs_sb_offset(i);
270 ret = btrfs_rmap_block(root->fs_info, cache->key.objectid, 273 ret = btrfs_rmap_block(fs_info, cache->key.objectid,
271 bytenr, 0, &logical, &nr, &stripe_len); 274 bytenr, 0, &logical, &nr, &stripe_len);
272 if (ret) 275 if (ret)
273 return ret; 276 return ret;
@@ -332,10 +335,12 @@ static void put_caching_control(struct btrfs_caching_control *ctl)
332static void fragment_free_space(struct btrfs_root *root, 335static void fragment_free_space(struct btrfs_root *root,
333 struct btrfs_block_group_cache *block_group) 336 struct btrfs_block_group_cache *block_group)
334{ 337{
338 struct btrfs_fs_info *fs_info = root->fs_info;
339
335 u64 start = block_group->key.objectid; 340 u64 start = block_group->key.objectid;
336 u64 len = block_group->key.offset; 341 u64 len = block_group->key.offset;
337 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? 342 u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ?
338 root->fs_info->nodesize : root->fs_info->sectorsize; 343 fs_info->nodesize : fs_info->sectorsize;
339 u64 step = chunk << 1; 344 u64 step = chunk << 1;
340 345
341 while (len > chunk) { 346 while (len > chunk) {
@@ -394,9 +399,9 @@ u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
394 399
395static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) 400static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
396{ 401{
397 struct btrfs_block_group_cache *block_group; 402 struct btrfs_block_group_cache *block_group = caching_ctl->block_group;
398 struct btrfs_fs_info *fs_info; 403 struct btrfs_fs_info *fs_info = block_group->fs_info;
399 struct btrfs_root *extent_root; 404 struct btrfs_root *extent_root = fs_info->extent_root;
400 struct btrfs_path *path; 405 struct btrfs_path *path;
401 struct extent_buffer *leaf; 406 struct extent_buffer *leaf;
402 struct btrfs_key key; 407 struct btrfs_key key;
@@ -406,10 +411,6 @@ static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl)
406 int ret; 411 int ret;
407 bool wakeup = true; 412 bool wakeup = true;
408 413
409 block_group = caching_ctl->block_group;
410 fs_info = block_group->fs_info;
411 extent_root = fs_info->extent_root;
412
413 path = btrfs_alloc_path(); 414 path = btrfs_alloc_path();
414 if (!path) 415 if (!path)
415 return -ENOMEM; 416 return -ENOMEM;
@@ -780,6 +781,7 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
780/* simple helper to search for an existing data extent at a given offset */ 781/* simple helper to search for an existing data extent at a given offset */
781int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len) 782int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len)
782{ 783{
784 struct btrfs_fs_info *fs_info = root->fs_info;
783 int ret; 785 int ret;
784 struct btrfs_key key; 786 struct btrfs_key key;
785 struct btrfs_path *path; 787 struct btrfs_path *path;
@@ -791,8 +793,7 @@ int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len)
791 key.objectid = start; 793 key.objectid = start;
792 key.offset = len; 794 key.offset = len;
793 key.type = BTRFS_EXTENT_ITEM_KEY; 795 key.type = BTRFS_EXTENT_ITEM_KEY;
794 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, 796 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
795 0, 0);
796 btrfs_free_path(path); 797 btrfs_free_path(path);
797 return ret; 798 return ret;
798} 799}
@@ -810,6 +811,7 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
810 struct btrfs_root *root, u64 bytenr, 811 struct btrfs_root *root, u64 bytenr,
811 u64 offset, int metadata, u64 *refs, u64 *flags) 812 u64 offset, int metadata, u64 *refs, u64 *flags)
812{ 813{
814 struct btrfs_fs_info *fs_info = root->fs_info;
813 struct btrfs_delayed_ref_head *head; 815 struct btrfs_delayed_ref_head *head;
814 struct btrfs_delayed_ref_root *delayed_refs; 816 struct btrfs_delayed_ref_root *delayed_refs;
815 struct btrfs_path *path; 817 struct btrfs_path *path;
@@ -825,8 +827,8 @@ int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
825 * If we don't have skinny metadata, don't bother doing anything 827 * If we don't have skinny metadata, don't bother doing anything
826 * different 828 * different
827 */ 829 */
828 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) { 830 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
829 offset = root->fs_info->nodesize; 831 offset = fs_info->nodesize;
830 metadata = 0; 832 metadata = 0;
831 } 833 }
832 834
@@ -847,8 +849,7 @@ search_again:
847 else 849 else
848 key.type = BTRFS_EXTENT_ITEM_KEY; 850 key.type = BTRFS_EXTENT_ITEM_KEY;
849 851
850 ret = btrfs_search_slot(trans, root->fs_info->extent_root, 852 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
851 &key, path, 0, 0);
852 if (ret < 0) 853 if (ret < 0)
853 goto out_free; 854 goto out_free;
854 855
@@ -859,7 +860,7 @@ search_again:
859 path->slots[0]); 860 path->slots[0]);
860 if (key.objectid == bytenr && 861 if (key.objectid == bytenr &&
861 key.type == BTRFS_EXTENT_ITEM_KEY && 862 key.type == BTRFS_EXTENT_ITEM_KEY &&
862 key.offset == root->fs_info->nodesize) 863 key.offset == fs_info->nodesize)
863 ret = 0; 864 ret = 0;
864 } 865 }
865 } 866 }
@@ -1540,6 +1541,7 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1540 u64 parent, u64 root_objectid, 1541 u64 parent, u64 root_objectid,
1541 u64 owner, u64 offset, int insert) 1542 u64 owner, u64 offset, int insert)
1542{ 1543{
1544 struct btrfs_fs_info *fs_info = root->fs_info;
1543 struct btrfs_key key; 1545 struct btrfs_key key;
1544 struct extent_buffer *leaf; 1546 struct extent_buffer *leaf;
1545 struct btrfs_extent_item *ei; 1547 struct btrfs_extent_item *ei;
@@ -1553,8 +1555,7 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1553 int want; 1555 int want;
1554 int ret; 1556 int ret;
1555 int err = 0; 1557 int err = 0;
1556 bool skinny_metadata = btrfs_fs_incompat(root->fs_info, 1558 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
1557 SKINNY_METADATA);
1558 1559
1559 key.objectid = bytenr; 1560 key.objectid = bytenr;
1560 key.type = BTRFS_EXTENT_ITEM_KEY; 1561 key.type = BTRFS_EXTENT_ITEM_KEY;
@@ -2025,6 +2026,7 @@ static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
2025int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, 2026int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
2026 u64 num_bytes, u64 *actual_bytes) 2027 u64 num_bytes, u64 *actual_bytes)
2027{ 2028{
2029 struct btrfs_fs_info *fs_info = root->fs_info;
2028 int ret; 2030 int ret;
2029 u64 discarded_bytes = 0; 2031 u64 discarded_bytes = 0;
2030 struct btrfs_bio *bbio = NULL; 2032 struct btrfs_bio *bbio = NULL;
@@ -2034,10 +2036,10 @@ int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
2034 * Avoid races with device replace and make sure our bbio has devices 2036 * Avoid races with device replace and make sure our bbio has devices
2035 * associated to its stripes that don't go away while we are discarding. 2037 * associated to its stripes that don't go away while we are discarding.
2036 */ 2038 */
2037 btrfs_bio_counter_inc_blocked(root->fs_info); 2039 btrfs_bio_counter_inc_blocked(fs_info);
2038 /* Tell the block device(s) that the sectors can be discarded */ 2040 /* Tell the block device(s) that the sectors can be discarded */
2039 ret = btrfs_map_block(root->fs_info, BTRFS_MAP_DISCARD, 2041 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes,
2040 bytenr, &num_bytes, &bbio, 0); 2042 &bbio, 0);
2041 /* Error condition is -ENOMEM */ 2043 /* Error condition is -ENOMEM */
2042 if (!ret) { 2044 if (!ret) {
2043 struct btrfs_bio_stripe *stripe = bbio->stripes; 2045 struct btrfs_bio_stripe *stripe = bbio->stripes;
@@ -2067,7 +2069,7 @@ int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
2067 } 2069 }
2068 btrfs_put_bbio(bbio); 2070 btrfs_put_bbio(bbio);
2069 } 2071 }
2070 btrfs_bio_counter_dec(root->fs_info); 2072 btrfs_bio_counter_dec(fs_info);
2071 2073
2072 if (actual_bytes) 2074 if (actual_bytes)
2073 *actual_bytes = discarded_bytes; 2075 *actual_bytes = discarded_bytes;
@@ -2154,7 +2156,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
2154 path->reada = READA_FORWARD; 2156 path->reada = READA_FORWARD;
2155 path->leave_spinning = 1; 2157 path->leave_spinning = 1;
2156 /* now insert the actual backref */ 2158 /* now insert the actual backref */
2157 ret = insert_extent_backref(trans, root->fs_info->extent_root, 2159 ret = insert_extent_backref(trans, fs_info->extent_root,
2158 path, bytenr, parent, root_objectid, 2160 path, bytenr, parent, root_objectid,
2159 owner, offset, refs_to_add); 2161 owner, offset, refs_to_add);
2160 if (ret) 2162 if (ret)
@@ -2170,6 +2172,7 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2170 struct btrfs_delayed_extent_op *extent_op, 2172 struct btrfs_delayed_extent_op *extent_op,
2171 int insert_reserved) 2173 int insert_reserved)
2172{ 2174{
2175 struct btrfs_fs_info *fs_info = root->fs_info;
2173 int ret = 0; 2176 int ret = 0;
2174 struct btrfs_delayed_data_ref *ref; 2177 struct btrfs_delayed_data_ref *ref;
2175 struct btrfs_key ins; 2178 struct btrfs_key ins;
@@ -2182,7 +2185,7 @@ static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2182 ins.type = BTRFS_EXTENT_ITEM_KEY; 2185 ins.type = BTRFS_EXTENT_ITEM_KEY;
2183 2186
2184 ref = btrfs_delayed_node_to_data_ref(node); 2187 ref = btrfs_delayed_node_to_data_ref(node);
2185 trace_run_delayed_data_ref(root->fs_info, node, ref, node->action); 2188 trace_run_delayed_data_ref(fs_info, node, ref, node->action);
2186 2189
2187 if (node->type == BTRFS_SHARED_DATA_REF_KEY) 2190 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2188 parent = ref->parent; 2191 parent = ref->parent;
@@ -2234,6 +2237,7 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2234 struct btrfs_delayed_ref_node *node, 2237 struct btrfs_delayed_ref_node *node,
2235 struct btrfs_delayed_extent_op *extent_op) 2238 struct btrfs_delayed_extent_op *extent_op)
2236{ 2239{
2240 struct btrfs_fs_info *fs_info = root->fs_info;
2237 struct btrfs_key key; 2241 struct btrfs_key key;
2238 struct btrfs_path *path; 2242 struct btrfs_path *path;
2239 struct btrfs_extent_item *ei; 2243 struct btrfs_extent_item *ei;
@@ -2246,7 +2250,7 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2246 if (trans->aborted) 2250 if (trans->aborted)
2247 return 0; 2251 return 0;
2248 2252
2249 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) 2253 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2250 metadata = 0; 2254 metadata = 0;
2251 2255
2252 path = btrfs_alloc_path(); 2256 path = btrfs_alloc_path();
@@ -2266,8 +2270,7 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2266again: 2270again:
2267 path->reada = READA_FORWARD; 2271 path->reada = READA_FORWARD;
2268 path->leave_spinning = 1; 2272 path->leave_spinning = 1;
2269 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, 2273 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
2270 path, 0, 1);
2271 if (ret < 0) { 2274 if (ret < 0) {
2272 err = ret; 2275 err = ret;
2273 goto out; 2276 goto out;
@@ -2302,7 +2305,7 @@ again:
2302 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 2305 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2303#ifdef BTRFS_COMPAT_EXTENT_TREE_V0 2306#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2304 if (item_size < sizeof(*ei)) { 2307 if (item_size < sizeof(*ei)) {
2305 ret = convert_extent_item_v0(trans, root->fs_info->extent_root, 2308 ret = convert_extent_item_v0(trans, fs_info->extent_root,
2306 path, (u64)-1, 0); 2309 path, (u64)-1, 0);
2307 if (ret < 0) { 2310 if (ret < 0) {
2308 err = ret; 2311 err = ret;
@@ -2328,16 +2331,16 @@ static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2328 struct btrfs_delayed_extent_op *extent_op, 2331 struct btrfs_delayed_extent_op *extent_op,
2329 int insert_reserved) 2332 int insert_reserved)
2330{ 2333{
2334 struct btrfs_fs_info *fs_info = root->fs_info;
2331 int ret = 0; 2335 int ret = 0;
2332 struct btrfs_delayed_tree_ref *ref; 2336 struct btrfs_delayed_tree_ref *ref;
2333 struct btrfs_key ins; 2337 struct btrfs_key ins;
2334 u64 parent = 0; 2338 u64 parent = 0;
2335 u64 ref_root = 0; 2339 u64 ref_root = 0;
2336 bool skinny_metadata = btrfs_fs_incompat(root->fs_info, 2340 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
2337 SKINNY_METADATA);
2338 2341
2339 ref = btrfs_delayed_node_to_tree_ref(node); 2342 ref = btrfs_delayed_node_to_tree_ref(node);
2340 trace_run_delayed_tree_ref(root->fs_info, node, ref, node->action); 2343 trace_run_delayed_tree_ref(fs_info, node, ref, node->action);
2341 2344
2342 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) 2345 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2343 parent = ref->parent; 2346 parent = ref->parent;
@@ -2388,6 +2391,7 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2388 struct btrfs_delayed_extent_op *extent_op, 2391 struct btrfs_delayed_extent_op *extent_op,
2389 int insert_reserved) 2392 int insert_reserved)
2390{ 2393{
2394 struct btrfs_fs_info *fs_info = root->fs_info;
2391 int ret = 0; 2395 int ret = 0;
2392 2396
2393 if (trans->aborted) { 2397 if (trans->aborted) {
@@ -2407,22 +2411,20 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2407 */ 2411 */
2408 BUG_ON(extent_op); 2412 BUG_ON(extent_op);
2409 head = btrfs_delayed_node_to_head(node); 2413 head = btrfs_delayed_node_to_head(node);
2410 trace_run_delayed_ref_head(root->fs_info, node, head, 2414 trace_run_delayed_ref_head(fs_info, node, head, node->action);
2411 node->action);
2412 2415
2413 if (insert_reserved) { 2416 if (insert_reserved) {
2414 btrfs_pin_extent(root, node->bytenr, 2417 btrfs_pin_extent(root, node->bytenr,
2415 node->num_bytes, 1); 2418 node->num_bytes, 1);
2416 if (head->is_data) { 2419 if (head->is_data) {
2417 ret = btrfs_del_csums(trans, root->fs_info, 2420 ret = btrfs_del_csums(trans, fs_info,
2418 node->bytenr, 2421 node->bytenr,
2419 node->num_bytes); 2422 node->num_bytes);
2420 } 2423 }
2421 } 2424 }
2422 2425
2423 /* Also free its reserved qgroup space */ 2426 /* Also free its reserved qgroup space */
2424 btrfs_qgroup_free_delayed_ref(root->fs_info, 2427 btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root,
2425 head->qgroup_ref_root,
2426 head->qgroup_reserved); 2428 head->qgroup_reserved);
2427 return ret; 2429 return ret;
2428 } 2430 }
@@ -2748,18 +2750,19 @@ static u64 find_middle(struct rb_root *root)
2748 2750
2749static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads) 2751static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2750{ 2752{
2753 struct btrfs_fs_info *fs_info = root->fs_info;
2751 u64 num_bytes; 2754 u64 num_bytes;
2752 2755
2753 num_bytes = heads * (sizeof(struct btrfs_extent_item) + 2756 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2754 sizeof(struct btrfs_extent_inline_ref)); 2757 sizeof(struct btrfs_extent_inline_ref));
2755 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) 2758 if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA))
2756 num_bytes += heads * sizeof(struct btrfs_tree_block_info); 2759 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2757 2760
2758 /* 2761 /*
2759 * We don't ever fill up leaves all the way so multiply by 2 just to be 2762 * We don't ever fill up leaves all the way so multiply by 2 just to be
2760 * closer to what we're really going to want to use. 2763 * closer to what we're really going to want to use.
2761 */ 2764 */
2762 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root->fs_info)); 2765 return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info));
2763} 2766}
2764 2767
2765/* 2768/*
@@ -2768,14 +2771,15 @@ static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2768 */ 2771 */
2769u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes) 2772u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
2770{ 2773{
2774 struct btrfs_fs_info *fs_info = root->fs_info;
2771 u64 csum_size; 2775 u64 csum_size;
2772 u64 num_csums_per_leaf; 2776 u64 num_csums_per_leaf;
2773 u64 num_csums; 2777 u64 num_csums;
2774 2778
2775 csum_size = BTRFS_MAX_ITEM_SIZE(root->fs_info); 2779 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
2776 num_csums_per_leaf = div64_u64(csum_size, 2780 num_csums_per_leaf = div64_u64(csum_size,
2777 (u64)btrfs_super_csum_size(root->fs_info->super_copy)); 2781 (u64)btrfs_super_csum_size(fs_info->super_copy));
2778 num_csums = div64_u64(csum_bytes, root->fs_info->sectorsize); 2782 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
2779 num_csums += num_csums_per_leaf - 1; 2783 num_csums += num_csums_per_leaf - 1;
2780 num_csums = div64_u64(num_csums, num_csums_per_leaf); 2784 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2781 return num_csums; 2785 return num_csums;
@@ -2784,6 +2788,7 @@ u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes)
2784int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, 2788int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2785 struct btrfs_root *root) 2789 struct btrfs_root *root)
2786{ 2790{
2791 struct btrfs_fs_info *fs_info = root->fs_info;
2787 struct btrfs_block_rsv *global_rsv; 2792 struct btrfs_block_rsv *global_rsv;
2788 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; 2793 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2789 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; 2794 u64 csum_bytes = trans->transaction->delayed_refs.pending_csums;
@@ -2791,15 +2796,15 @@ int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2791 u64 num_bytes, num_dirty_bgs_bytes; 2796 u64 num_bytes, num_dirty_bgs_bytes;
2792 int ret = 0; 2797 int ret = 0;
2793 2798
2794 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 2799 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
2795 num_heads = heads_to_leaves(root, num_heads); 2800 num_heads = heads_to_leaves(root, num_heads);
2796 if (num_heads > 1) 2801 if (num_heads > 1)
2797 num_bytes += (num_heads - 1) * root->fs_info->nodesize; 2802 num_bytes += (num_heads - 1) * fs_info->nodesize;
2798 num_bytes <<= 1; 2803 num_bytes <<= 1;
2799 num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * root->fs_info->nodesize; 2804 num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * fs_info->nodesize;
2800 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 2805 num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info,
2801 num_dirty_bgs); 2806 num_dirty_bgs);
2802 global_rsv = &root->fs_info->global_block_rsv; 2807 global_rsv = &fs_info->global_block_rsv;
2803 2808
2804 /* 2809 /*
2805 * If we can't allocate any more chunks lets make sure we have _lots_ of 2810 * If we can't allocate any more chunks lets make sure we have _lots_ of
@@ -2892,6 +2897,7 @@ done:
2892int btrfs_async_run_delayed_refs(struct btrfs_root *root, 2897int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2893 unsigned long count, u64 transid, int wait) 2898 unsigned long count, u64 transid, int wait)
2894{ 2899{
2900 struct btrfs_fs_info *fs_info = root->fs_info;
2895 struct async_delayed_refs *async; 2901 struct async_delayed_refs *async;
2896 int ret; 2902 int ret;
2897 2903
@@ -2899,7 +2905,7 @@ int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2899 if (!async) 2905 if (!async)
2900 return -ENOMEM; 2906 return -ENOMEM;
2901 2907
2902 async->root = root->fs_info->tree_root; 2908 async->root = fs_info->tree_root;
2903 async->count = count; 2909 async->count = count;
2904 async->error = 0; 2910 async->error = 0;
2905 async->transid = transid; 2911 async->transid = transid;
@@ -2912,7 +2918,7 @@ int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2912 btrfs_init_work(&async->work, btrfs_extent_refs_helper, 2918 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
2913 delayed_ref_async_start, NULL, NULL); 2919 delayed_ref_async_start, NULL, NULL);
2914 2920
2915 btrfs_queue_work(root->fs_info->extent_workers, &async->work); 2921 btrfs_queue_work(fs_info->extent_workers, &async->work);
2916 2922
2917 if (wait) { 2923 if (wait) {
2918 wait_for_completion(&async->wait); 2924 wait_for_completion(&async->wait);
@@ -2936,6 +2942,7 @@ int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2936int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 2942int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2937 struct btrfs_root *root, unsigned long count) 2943 struct btrfs_root *root, unsigned long count)
2938{ 2944{
2945 struct btrfs_fs_info *fs_info = root->fs_info;
2939 struct rb_node *node; 2946 struct rb_node *node;
2940 struct btrfs_delayed_ref_root *delayed_refs; 2947 struct btrfs_delayed_ref_root *delayed_refs;
2941 struct btrfs_delayed_ref_head *head; 2948 struct btrfs_delayed_ref_head *head;
@@ -2947,11 +2954,11 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2947 if (trans->aborted) 2954 if (trans->aborted)
2948 return 0; 2955 return 0;
2949 2956
2950 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &root->fs_info->flags)) 2957 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
2951 return 0; 2958 return 0;
2952 2959
2953 if (root == root->fs_info->extent_root) 2960 if (root == fs_info->extent_root)
2954 root = root->fs_info->tree_root; 2961 root = fs_info->tree_root;
2955 2962
2956 delayed_refs = &trans->transaction->delayed_refs; 2963 delayed_refs = &trans->transaction->delayed_refs;
2957 if (count == 0) 2964 if (count == 0)
@@ -3019,6 +3026,7 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
3019 u64 bytenr, u64 num_bytes, u64 flags, 3026 u64 bytenr, u64 num_bytes, u64 flags,
3020 int level, int is_data) 3027 int level, int is_data)
3021{ 3028{
3029 struct btrfs_fs_info *fs_info = root->fs_info;
3022 struct btrfs_delayed_extent_op *extent_op; 3030 struct btrfs_delayed_extent_op *extent_op;
3023 int ret; 3031 int ret;
3024 3032
@@ -3032,7 +3040,7 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
3032 extent_op->is_data = is_data ? true : false; 3040 extent_op->is_data = is_data ? true : false;
3033 extent_op->level = level; 3041 extent_op->level = level;
3034 3042
3035 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr, 3043 ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr,
3036 num_bytes, extent_op); 3044 num_bytes, extent_op);
3037 if (ret) 3045 if (ret)
3038 btrfs_free_delayed_extent_op(extent_op); 3046 btrfs_free_delayed_extent_op(extent_op);
@@ -3106,7 +3114,8 @@ static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
3106 struct btrfs_path *path, 3114 struct btrfs_path *path,
3107 u64 objectid, u64 offset, u64 bytenr) 3115 u64 objectid, u64 offset, u64 bytenr)
3108{ 3116{
3109 struct btrfs_root *extent_root = root->fs_info->extent_root; 3117 struct btrfs_fs_info *fs_info = root->fs_info;
3118 struct btrfs_root *extent_root = fs_info->extent_root;
3110 struct extent_buffer *leaf; 3119 struct extent_buffer *leaf;
3111 struct btrfs_extent_data_ref *ref; 3120 struct btrfs_extent_data_ref *ref;
3112 struct btrfs_extent_inline_ref *iref; 3121 struct btrfs_extent_inline_ref *iref;
@@ -3213,6 +3222,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3213 struct extent_buffer *buf, 3222 struct extent_buffer *buf,
3214 int full_backref, int inc) 3223 int full_backref, int inc)
3215{ 3224{
3225 struct btrfs_fs_info *fs_info = root->fs_info;
3216 u64 bytenr; 3226 u64 bytenr;
3217 u64 num_bytes; 3227 u64 num_bytes;
3218 u64 parent; 3228 u64 parent;
@@ -3227,7 +3237,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3227 u64, u64, u64, u64, u64, u64); 3237 u64, u64, u64, u64, u64, u64);
3228 3238
3229 3239
3230 if (btrfs_is_testing(root->fs_info)) 3240 if (btrfs_is_testing(fs_info))
3231 return 0; 3241 return 0;
3232 3242
3233 ref_root = btrfs_header_owner(buf); 3243 ref_root = btrfs_header_owner(buf);
@@ -3270,7 +3280,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3270 goto fail; 3280 goto fail;
3271 } else { 3281 } else {
3272 bytenr = btrfs_node_blockptr(buf, i); 3282 bytenr = btrfs_node_blockptr(buf, i);
3273 num_bytes = root->fs_info->nodesize; 3283 num_bytes = fs_info->nodesize;
3274 ret = process_func(trans, root, bytenr, num_bytes, 3284 ret = process_func(trans, root, bytenr, num_bytes,
3275 parent, ref_root, level - 1, 0); 3285 parent, ref_root, level - 1, 0);
3276 if (ret) 3286 if (ret)
@@ -3300,7 +3310,8 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
3300 struct btrfs_block_group_cache *cache) 3310 struct btrfs_block_group_cache *cache)
3301{ 3311{
3302 int ret; 3312 int ret;
3303 struct btrfs_root *extent_root = root->fs_info->extent_root; 3313 struct btrfs_fs_info *fs_info = root->fs_info;
3314 struct btrfs_root *extent_root = fs_info->extent_root;
3304 unsigned long bi; 3315 unsigned long bi;
3305 struct extent_buffer *leaf; 3316 struct extent_buffer *leaf;
3306 3317
@@ -3325,19 +3336,18 @@ static struct btrfs_block_group_cache *
3325next_block_group(struct btrfs_root *root, 3336next_block_group(struct btrfs_root *root,
3326 struct btrfs_block_group_cache *cache) 3337 struct btrfs_block_group_cache *cache)
3327{ 3338{
3339 struct btrfs_fs_info *fs_info = root->fs_info;
3328 struct rb_node *node; 3340 struct rb_node *node;
3329 3341
3330 spin_lock(&root->fs_info->block_group_cache_lock); 3342 spin_lock(&fs_info->block_group_cache_lock);
3331 3343
3332 /* If our block group was removed, we need a full search. */ 3344 /* If our block group was removed, we need a full search. */
3333 if (RB_EMPTY_NODE(&cache->cache_node)) { 3345 if (RB_EMPTY_NODE(&cache->cache_node)) {
3334 const u64 next_bytenr = cache->key.objectid + cache->key.offset; 3346 const u64 next_bytenr = cache->key.objectid + cache->key.offset;
3335 3347
3336 spin_unlock(&root->fs_info->block_group_cache_lock); 3348 spin_unlock(&fs_info->block_group_cache_lock);
3337 btrfs_put_block_group(cache); 3349 btrfs_put_block_group(cache);
3338 cache = btrfs_lookup_first_block_group(root->fs_info, 3350 cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache;
3339 next_bytenr);
3340 return cache;
3341 } 3351 }
3342 node = rb_next(&cache->cache_node); 3352 node = rb_next(&cache->cache_node);
3343 btrfs_put_block_group(cache); 3353 btrfs_put_block_group(cache);
@@ -3347,7 +3357,7 @@ next_block_group(struct btrfs_root *root,
3347 btrfs_get_block_group(cache); 3357 btrfs_get_block_group(cache);
3348 } else 3358 } else
3349 cache = NULL; 3359 cache = NULL;
3350 spin_unlock(&root->fs_info->block_group_cache_lock); 3360 spin_unlock(&fs_info->block_group_cache_lock);
3351 return cache; 3361 return cache;
3352} 3362}
3353 3363
@@ -3355,7 +3365,8 @@ static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3355 struct btrfs_trans_handle *trans, 3365 struct btrfs_trans_handle *trans,
3356 struct btrfs_path *path) 3366 struct btrfs_path *path)
3357{ 3367{
3358 struct btrfs_root *root = block_group->fs_info->tree_root; 3368 struct btrfs_fs_info *fs_info = block_group->fs_info;
3369 struct btrfs_root *root = fs_info->tree_root;
3359 struct inode *inode = NULL; 3370 struct inode *inode = NULL;
3360 u64 alloc_hint = 0; 3371 u64 alloc_hint = 0;
3361 int dcs = BTRFS_DC_ERROR; 3372 int dcs = BTRFS_DC_ERROR;
@@ -3429,7 +3440,7 @@ again:
3429 3440
3430 if (i_size_read(inode) > 0) { 3441 if (i_size_read(inode) > 0) {
3431 ret = btrfs_check_trunc_cache_free_space(root, 3442 ret = btrfs_check_trunc_cache_free_space(root,
3432 &root->fs_info->global_block_rsv); 3443 &fs_info->global_block_rsv);
3433 if (ret) 3444 if (ret)
3434 goto out_put; 3445 goto out_put;
3435 3446
@@ -3440,7 +3451,7 @@ again:
3440 3451
3441 spin_lock(&block_group->lock); 3452 spin_lock(&block_group->lock);
3442 if (block_group->cached != BTRFS_CACHE_FINISHED || 3453 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3443 !btrfs_test_opt(root->fs_info, SPACE_CACHE)) { 3454 !btrfs_test_opt(fs_info, SPACE_CACHE)) {
3444 /* 3455 /*
3445 * don't bother trying to write stuff out _if_ 3456 * don't bother trying to write stuff out _if_
3446 * a) we're not cached, 3457 * a) we're not cached,
@@ -3511,12 +3522,13 @@ out:
3511int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, 3522int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
3512 struct btrfs_root *root) 3523 struct btrfs_root *root)
3513{ 3524{
3525 struct btrfs_fs_info *fs_info = root->fs_info;
3514 struct btrfs_block_group_cache *cache, *tmp; 3526 struct btrfs_block_group_cache *cache, *tmp;
3515 struct btrfs_transaction *cur_trans = trans->transaction; 3527 struct btrfs_transaction *cur_trans = trans->transaction;
3516 struct btrfs_path *path; 3528 struct btrfs_path *path;
3517 3529
3518 if (list_empty(&cur_trans->dirty_bgs) || 3530 if (list_empty(&cur_trans->dirty_bgs) ||
3519 !btrfs_test_opt(root->fs_info, SPACE_CACHE)) 3531 !btrfs_test_opt(fs_info, SPACE_CACHE))
3520 return 0; 3532 return 0;
3521 3533
3522 path = btrfs_alloc_path(); 3534 path = btrfs_alloc_path();
@@ -3549,6 +3561,7 @@ int btrfs_setup_space_cache(struct btrfs_trans_handle *trans,
3549int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, 3561int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans,
3550 struct btrfs_root *root) 3562 struct btrfs_root *root)
3551{ 3563{
3564 struct btrfs_fs_info *fs_info = root->fs_info;
3552 struct btrfs_block_group_cache *cache; 3565 struct btrfs_block_group_cache *cache;
3553 struct btrfs_transaction *cur_trans = trans->transaction; 3566 struct btrfs_transaction *cur_trans = trans->transaction;
3554 int ret = 0; 3567 int ret = 0;
@@ -3622,7 +3635,7 @@ again:
3622 3635
3623 if (cache->disk_cache_state == BTRFS_DC_SETUP) { 3636 if (cache->disk_cache_state == BTRFS_DC_SETUP) {
3624 cache->io_ctl.inode = NULL; 3637 cache->io_ctl.inode = NULL;
3625 ret = btrfs_write_out_cache(root->fs_info, trans, 3638 ret = btrfs_write_out_cache(fs_info, trans,
3626 cache, path); 3639 cache, path);
3627 if (ret == 0 && cache->io_ctl.inode) { 3640 if (ret == 0 && cache->io_ctl.inode) {
3628 num_started++; 3641 num_started++;
@@ -3712,6 +3725,7 @@ again:
3712int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, 3725int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3713 struct btrfs_root *root) 3726 struct btrfs_root *root)
3714{ 3727{
3728 struct btrfs_fs_info *fs_info = root->fs_info;
3715 struct btrfs_block_group_cache *cache; 3729 struct btrfs_block_group_cache *cache;
3716 struct btrfs_transaction *cur_trans = trans->transaction; 3730 struct btrfs_transaction *cur_trans = trans->transaction;
3717 int ret = 0; 3731 int ret = 0;
@@ -3775,7 +3789,7 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3775 3789
3776 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { 3790 if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) {
3777 cache->io_ctl.inode = NULL; 3791 cache->io_ctl.inode = NULL;
3778 ret = btrfs_write_out_cache(root->fs_info, trans, 3792 ret = btrfs_write_out_cache(fs_info, trans,
3779 cache, path); 3793 cache, path);
3780 if (ret == 0 && cache->io_ctl.inode) { 3794 if (ret == 0 && cache->io_ctl.inode) {
3781 num_started++; 3795 num_started++;
@@ -3836,10 +3850,11 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3836 3850
3837int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) 3851int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3838{ 3852{
3853 struct btrfs_fs_info *fs_info = root->fs_info;
3839 struct btrfs_block_group_cache *block_group; 3854 struct btrfs_block_group_cache *block_group;
3840 int readonly = 0; 3855 int readonly = 0;
3841 3856
3842 block_group = btrfs_lookup_block_group(root->fs_info, bytenr); 3857 block_group = btrfs_lookup_block_group(fs_info, bytenr);
3843 if (!block_group || block_group->ro) 3858 if (!block_group || block_group->ro)
3844 readonly = 1; 3859 readonly = 1;
3845 if (block_group) 3860 if (block_group)
@@ -4050,7 +4065,8 @@ static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
4050 */ 4065 */
4051static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) 4066static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
4052{ 4067{
4053 u64 num_devices = root->fs_info->fs_devices->rw_devices; 4068 struct btrfs_fs_info *fs_info = root->fs_info;
4069 u64 num_devices = fs_info->fs_devices->rw_devices;
4054 u64 target; 4070 u64 target;
4055 u64 raid_type; 4071 u64 raid_type;
4056 u64 allowed = 0; 4072 u64 allowed = 0;
@@ -4059,16 +4075,16 @@ static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
4059 * see if restripe for this chunk_type is in progress, if so 4075 * see if restripe for this chunk_type is in progress, if so
4060 * try to reduce to the target profile 4076 * try to reduce to the target profile
4061 */ 4077 */
4062 spin_lock(&root->fs_info->balance_lock); 4078 spin_lock(&fs_info->balance_lock);
4063 target = get_restripe_target(root->fs_info, flags); 4079 target = get_restripe_target(fs_info, flags);
4064 if (target) { 4080 if (target) {
4065 /* pick target profile only if it's already available */ 4081 /* pick target profile only if it's already available */
4066 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { 4082 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
4067 spin_unlock(&root->fs_info->balance_lock); 4083 spin_unlock(&fs_info->balance_lock);
4068 return extended_to_chunk(target); 4084 return extended_to_chunk(target);
4069 } 4085 }
4070 } 4086 }
4071 spin_unlock(&root->fs_info->balance_lock); 4087 spin_unlock(&fs_info->balance_lock);
4072 4088
4073 /* First, mask out the RAID levels which aren't possible */ 4089 /* First, mask out the RAID levels which aren't possible */
4074 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { 4090 for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
@@ -4095,32 +4111,34 @@ static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
4095 4111
4096static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags) 4112static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
4097{ 4113{
4114 struct btrfs_fs_info *fs_info = root->fs_info;
4098 unsigned seq; 4115 unsigned seq;
4099 u64 flags; 4116 u64 flags;
4100 4117
4101 do { 4118 do {
4102 flags = orig_flags; 4119 flags = orig_flags;
4103 seq = read_seqbegin(&root->fs_info->profiles_lock); 4120 seq = read_seqbegin(&fs_info->profiles_lock);
4104 4121
4105 if (flags & BTRFS_BLOCK_GROUP_DATA) 4122 if (flags & BTRFS_BLOCK_GROUP_DATA)
4106 flags |= root->fs_info->avail_data_alloc_bits; 4123 flags |= fs_info->avail_data_alloc_bits;
4107 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 4124 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
4108 flags |= root->fs_info->avail_system_alloc_bits; 4125 flags |= fs_info->avail_system_alloc_bits;
4109 else if (flags & BTRFS_BLOCK_GROUP_METADATA) 4126 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
4110 flags |= root->fs_info->avail_metadata_alloc_bits; 4127 flags |= fs_info->avail_metadata_alloc_bits;
4111 } while (read_seqretry(&root->fs_info->profiles_lock, seq)); 4128 } while (read_seqretry(&fs_info->profiles_lock, seq));
4112 4129
4113 return btrfs_reduce_alloc_profile(root, flags); 4130 return btrfs_reduce_alloc_profile(root, flags);
4114} 4131}
4115 4132
4116u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) 4133u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
4117{ 4134{
4135 struct btrfs_fs_info *fs_info = root->fs_info;
4118 u64 flags; 4136 u64 flags;
4119 u64 ret; 4137 u64 ret;
4120 4138
4121 if (data) 4139 if (data)
4122 flags = BTRFS_BLOCK_GROUP_DATA; 4140 flags = BTRFS_BLOCK_GROUP_DATA;
4123 else if (root == root->fs_info->chunk_root) 4141 else if (root == fs_info->chunk_root)
4124 flags = BTRFS_BLOCK_GROUP_SYSTEM; 4142 flags = BTRFS_BLOCK_GROUP_SYSTEM;
4125 else 4143 else
4126 flags = BTRFS_BLOCK_GROUP_METADATA; 4144 flags = BTRFS_BLOCK_GROUP_METADATA;
@@ -4140,7 +4158,7 @@ int btrfs_alloc_data_chunk_ondemand(struct inode *inode, u64 bytes)
4140 int have_pinned_space; 4158 int have_pinned_space;
4141 4159
4142 /* make sure bytes are sectorsize aligned */ 4160 /* make sure bytes are sectorsize aligned */
4143 bytes = ALIGN(bytes, root->fs_info->sectorsize); 4161 bytes = ALIGN(bytes, fs_info->sectorsize);
4144 4162
4145 if (btrfs_is_free_space_inode(inode)) { 4163 if (btrfs_is_free_space_inode(inode)) {
4146 need_commit = 0; 4164 need_commit = 0;
@@ -4186,7 +4204,7 @@ alloc:
4186 if (IS_ERR(trans)) 4204 if (IS_ERR(trans))
4187 return PTR_ERR(trans); 4205 return PTR_ERR(trans);
4188 4206
4189 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 4207 ret = do_chunk_alloc(trans, fs_info->extent_root,
4190 alloc_target, 4208 alloc_target,
4191 CHUNK_ALLOC_NO_FORCE); 4209 CHUNK_ALLOC_NO_FORCE);
4192 btrfs_end_transaction(trans, root); 4210 btrfs_end_transaction(trans, root);
@@ -4218,12 +4236,13 @@ alloc:
4218 /* commit the current transaction and try again */ 4236 /* commit the current transaction and try again */
4219commit_trans: 4237commit_trans:
4220 if (need_commit && 4238 if (need_commit &&
4221 !atomic_read(&root->fs_info->open_ioctl_trans)) { 4239 !atomic_read(&fs_info->open_ioctl_trans)) {
4222 need_commit--; 4240 need_commit--;
4223 4241
4224 if (need_commit > 0) { 4242 if (need_commit > 0) {
4225 btrfs_start_delalloc_roots(fs_info, 0, -1); 4243 btrfs_start_delalloc_roots(fs_info, 0, -1);
4226 btrfs_wait_ordered_roots(fs_info, -1, 0, (u64)-1); 4244 btrfs_wait_ordered_roots(fs_info, -1, 0,
4245 (u64)-1);
4227 } 4246 }
4228 4247
4229 trans = btrfs_join_transaction(root); 4248 trans = btrfs_join_transaction(root);
@@ -4241,21 +4260,21 @@ commit_trans:
4241 * operations. Wait for it to finish so that 4260 * operations. Wait for it to finish so that
4242 * more space is released. 4261 * more space is released.
4243 */ 4262 */
4244 mutex_lock(&root->fs_info->cleaner_delayed_iput_mutex); 4263 mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
4245 mutex_unlock(&root->fs_info->cleaner_delayed_iput_mutex); 4264 mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
4246 goto again; 4265 goto again;
4247 } else { 4266 } else {
4248 btrfs_end_transaction(trans, root); 4267 btrfs_end_transaction(trans, root);
4249 } 4268 }
4250 } 4269 }
4251 4270
4252 trace_btrfs_space_reservation(root->fs_info, 4271 trace_btrfs_space_reservation(fs_info,
4253 "space_info:enospc", 4272 "space_info:enospc",
4254 data_sinfo->flags, bytes, 1); 4273 data_sinfo->flags, bytes, 1);
4255 return -ENOSPC; 4274 return -ENOSPC;
4256 } 4275 }
4257 data_sinfo->bytes_may_use += bytes; 4276 data_sinfo->bytes_may_use += bytes;
4258 trace_btrfs_space_reservation(root->fs_info, "space_info", 4277 trace_btrfs_space_reservation(fs_info, "space_info",
4259 data_sinfo->flags, bytes, 1); 4278 data_sinfo->flags, bytes, 1);
4260 spin_unlock(&data_sinfo->lock); 4279 spin_unlock(&data_sinfo->lock);
4261 4280
@@ -4269,13 +4288,13 @@ commit_trans:
4269 */ 4288 */
4270int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len) 4289int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len)
4271{ 4290{
4272 struct btrfs_root *root = BTRFS_I(inode)->root; 4291 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4273 int ret; 4292 int ret;
4274 4293
4275 /* align the range */ 4294 /* align the range */
4276 len = round_up(start + len, root->fs_info->sectorsize) - 4295 len = round_up(start + len, fs_info->sectorsize) -
4277 round_down(start, root->fs_info->sectorsize); 4296 round_down(start, fs_info->sectorsize);
4278 start = round_down(start, root->fs_info->sectorsize); 4297 start = round_down(start, fs_info->sectorsize);
4279 4298
4280 ret = btrfs_alloc_data_chunk_ondemand(inode, len); 4299 ret = btrfs_alloc_data_chunk_ondemand(inode, len);
4281 if (ret < 0) 4300 if (ret < 0)
@@ -4299,21 +4318,21 @@ int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len)
4299void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, 4318void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start,
4300 u64 len) 4319 u64 len)
4301{ 4320{
4302 struct btrfs_root *root = BTRFS_I(inode)->root; 4321 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4303 struct btrfs_space_info *data_sinfo; 4322 struct btrfs_space_info *data_sinfo;
4304 4323
4305 /* Make sure the range is aligned to sectorsize */ 4324 /* Make sure the range is aligned to sectorsize */
4306 len = round_up(start + len, root->fs_info->sectorsize) - 4325 len = round_up(start + len, fs_info->sectorsize) -
4307 round_down(start, root->fs_info->sectorsize); 4326 round_down(start, fs_info->sectorsize);
4308 start = round_down(start, root->fs_info->sectorsize); 4327 start = round_down(start, fs_info->sectorsize);
4309 4328
4310 data_sinfo = root->fs_info->data_sinfo; 4329 data_sinfo = fs_info->data_sinfo;
4311 spin_lock(&data_sinfo->lock); 4330 spin_lock(&data_sinfo->lock);
4312 if (WARN_ON(data_sinfo->bytes_may_use < len)) 4331 if (WARN_ON(data_sinfo->bytes_may_use < len))
4313 data_sinfo->bytes_may_use = 0; 4332 data_sinfo->bytes_may_use = 0;
4314 else 4333 else
4315 data_sinfo->bytes_may_use -= len; 4334 data_sinfo->bytes_may_use -= len;
4316 trace_btrfs_space_reservation(root->fs_info, "space_info", 4335 trace_btrfs_space_reservation(fs_info, "space_info",
4317 data_sinfo->flags, len, 0); 4336 data_sinfo->flags, len, 0);
4318 spin_unlock(&data_sinfo->lock); 4337 spin_unlock(&data_sinfo->lock);
4319} 4338}
@@ -4359,7 +4378,8 @@ static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
4359static int should_alloc_chunk(struct btrfs_root *root, 4378static int should_alloc_chunk(struct btrfs_root *root,
4360 struct btrfs_space_info *sinfo, int force) 4379 struct btrfs_space_info *sinfo, int force)
4361{ 4380{
4362 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; 4381 struct btrfs_fs_info *fs_info = root->fs_info;
4382 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4363 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; 4383 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
4364 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; 4384 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
4365 u64 thresh; 4385 u64 thresh;
@@ -4380,7 +4400,7 @@ static int should_alloc_chunk(struct btrfs_root *root,
4380 * about 1% of the FS size. 4400 * about 1% of the FS size.
4381 */ 4401 */
4382 if (force == CHUNK_ALLOC_LIMITED) { 4402 if (force == CHUNK_ALLOC_LIMITED) {
4383 thresh = btrfs_super_total_bytes(root->fs_info->super_copy); 4403 thresh = btrfs_super_total_bytes(fs_info->super_copy);
4384 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); 4404 thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1));
4385 4405
4386 if (num_bytes - num_allocated < thresh) 4406 if (num_bytes - num_allocated < thresh)
@@ -4394,13 +4414,14 @@ static int should_alloc_chunk(struct btrfs_root *root,
4394 4414
4395static u64 get_profile_num_devs(struct btrfs_root *root, u64 type) 4415static u64 get_profile_num_devs(struct btrfs_root *root, u64 type)
4396{ 4416{
4417 struct btrfs_fs_info *fs_info = root->fs_info;
4397 u64 num_dev; 4418 u64 num_dev;
4398 4419
4399 if (type & (BTRFS_BLOCK_GROUP_RAID10 | 4420 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
4400 BTRFS_BLOCK_GROUP_RAID0 | 4421 BTRFS_BLOCK_GROUP_RAID0 |
4401 BTRFS_BLOCK_GROUP_RAID5 | 4422 BTRFS_BLOCK_GROUP_RAID5 |
4402 BTRFS_BLOCK_GROUP_RAID6)) 4423 BTRFS_BLOCK_GROUP_RAID6))
4403 num_dev = root->fs_info->fs_devices->rw_devices; 4424 num_dev = fs_info->fs_devices->rw_devices;
4404 else if (type & BTRFS_BLOCK_GROUP_RAID1) 4425 else if (type & BTRFS_BLOCK_GROUP_RAID1)
4405 num_dev = 2; 4426 num_dev = 2;
4406 else 4427 else
@@ -4418,6 +4439,7 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4418 struct btrfs_root *root, 4439 struct btrfs_root *root,
4419 u64 type) 4440 u64 type)
4420{ 4441{
4442 struct btrfs_fs_info *fs_info = root->fs_info;
4421 struct btrfs_space_info *info; 4443 struct btrfs_space_info *info;
4422 u64 left; 4444 u64 left;
4423 u64 thresh; 4445 u64 thresh;
@@ -4428,9 +4450,9 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4428 * Needed because we can end up allocating a system chunk and for an 4450 * Needed because we can end up allocating a system chunk and for an
4429 * atomic and race free space reservation in the chunk block reserve. 4451 * atomic and race free space reservation in the chunk block reserve.
4430 */ 4452 */
4431 ASSERT(mutex_is_locked(&root->fs_info->chunk_mutex)); 4453 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
4432 4454
4433 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 4455 info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4434 spin_lock(&info->lock); 4456 spin_lock(&info->lock);
4435 left = info->total_bytes - info->bytes_used - info->bytes_pinned - 4457 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
4436 info->bytes_reserved - info->bytes_readonly - 4458 info->bytes_reserved - info->bytes_readonly -
@@ -4440,19 +4462,19 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4440 num_devs = get_profile_num_devs(root, type); 4462 num_devs = get_profile_num_devs(root, type);
4441 4463
4442 /* num_devs device items to update and 1 chunk item to add or remove */ 4464 /* num_devs device items to update and 1 chunk item to add or remove */
4443 thresh = btrfs_calc_trunc_metadata_size(root->fs_info, num_devs) + 4465 thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) +
4444 btrfs_calc_trans_metadata_size(root->fs_info, 1); 4466 btrfs_calc_trans_metadata_size(fs_info, 1);
4445 4467
4446 if (left < thresh && btrfs_test_opt(root->fs_info, ENOSPC_DEBUG)) { 4468 if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4447 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu", 4469 btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu",
4448 left, thresh, type); 4470 left, thresh, type);
4449 dump_space_info(root->fs_info, info, 0, 0); 4471 dump_space_info(fs_info, info, 0, 0);
4450 } 4472 }
4451 4473
4452 if (left < thresh) { 4474 if (left < thresh) {
4453 u64 flags; 4475 u64 flags;
4454 4476
4455 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0); 4477 flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
4456 /* 4478 /*
4457 * Ignore failure to create system chunk. We might end up not 4479 * Ignore failure to create system chunk. We might end up not
4458 * needing it, as we might not need to COW all nodes/leafs from 4480 * needing it, as we might not need to COW all nodes/leafs from
@@ -4463,8 +4485,8 @@ void check_system_chunk(struct btrfs_trans_handle *trans,
4463 } 4485 }
4464 4486
4465 if (!ret) { 4487 if (!ret) {
4466 ret = btrfs_block_rsv_add(root->fs_info->chunk_root, 4488 ret = btrfs_block_rsv_add(fs_info->chunk_root,
4467 &root->fs_info->chunk_block_rsv, 4489 &fs_info->chunk_block_rsv,
4468 thresh, BTRFS_RESERVE_NO_FLUSH); 4490 thresh, BTRFS_RESERVE_NO_FLUSH);
4469 if (!ret) 4491 if (!ret)
4470 trans->chunk_bytes_reserved += thresh; 4492 trans->chunk_bytes_reserved += thresh;
@@ -4492,10 +4514,9 @@ static int do_chunk_alloc(struct btrfs_trans_handle *trans,
4492 if (trans->allocating_chunk) 4514 if (trans->allocating_chunk)
4493 return -ENOSPC; 4515 return -ENOSPC;
4494 4516
4495 space_info = __find_space_info(extent_root->fs_info, flags); 4517 space_info = __find_space_info(fs_info, flags);
4496 if (!space_info) { 4518 if (!space_info) {
4497 ret = update_space_info(extent_root->fs_info, flags, 4519 ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info);
4498 0, 0, 0, &space_info);
4499 BUG_ON(ret); /* -ENOMEM */ 4520 BUG_ON(ret); /* -ENOMEM */
4500 } 4521 }
4501 BUG_ON(!space_info); /* Logic error */ 4522 BUG_ON(!space_info); /* Logic error */
@@ -4607,7 +4628,8 @@ static int can_overcommit(struct btrfs_root *root,
4607 struct btrfs_space_info *space_info, u64 bytes, 4628 struct btrfs_space_info *space_info, u64 bytes,
4608 enum btrfs_reserve_flush_enum flush) 4629 enum btrfs_reserve_flush_enum flush)
4609{ 4630{
4610 struct btrfs_block_rsv *global_rsv; 4631 struct btrfs_fs_info *fs_info = root->fs_info;
4632 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4611 u64 profile; 4633 u64 profile;
4612 u64 space_size; 4634 u64 space_size;
4613 u64 avail; 4635 u64 avail;
@@ -4617,8 +4639,6 @@ static int can_overcommit(struct btrfs_root *root,
4617 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA) 4639 if (space_info->flags & BTRFS_BLOCK_GROUP_DATA)
4618 return 0; 4640 return 0;
4619 4641
4620 BUG_ON(root->fs_info == NULL);
4621 global_rsv = &root->fs_info->global_block_rsv;
4622 profile = btrfs_get_alloc_profile(root, 0); 4642 profile = btrfs_get_alloc_profile(root, 0);
4623 used = space_info->bytes_used + space_info->bytes_reserved + 4643 used = space_info->bytes_used + space_info->bytes_reserved +
4624 space_info->bytes_pinned + space_info->bytes_readonly; 4644 space_info->bytes_pinned + space_info->bytes_readonly;
@@ -4637,9 +4657,9 @@ static int can_overcommit(struct btrfs_root *root,
4637 4657
4638 used += space_info->bytes_may_use; 4658 used += space_info->bytes_may_use;
4639 4659
4640 spin_lock(&root->fs_info->free_chunk_lock); 4660 spin_lock(&fs_info->free_chunk_lock);
4641 avail = root->fs_info->free_chunk_space; 4661 avail = fs_info->free_chunk_space;
4642 spin_unlock(&root->fs_info->free_chunk_lock); 4662 spin_unlock(&fs_info->free_chunk_lock);
4643 4663
4644 /* 4664 /*
4645 * If we have dup, raid1 or raid10 then only half of the free 4665 * If we have dup, raid1 or raid10 then only half of the free
@@ -4670,7 +4690,8 @@ static int can_overcommit(struct btrfs_root *root,
4670static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, 4690static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
4671 unsigned long nr_pages, int nr_items) 4691 unsigned long nr_pages, int nr_items)
4672{ 4692{
4673 struct super_block *sb = root->fs_info->sb; 4693 struct btrfs_fs_info *fs_info = root->fs_info;
4694 struct super_block *sb = fs_info->sb;
4674 4695
4675 if (down_read_trylock(&sb->s_umount)) { 4696 if (down_read_trylock(&sb->s_umount)) {
4676 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); 4697 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
@@ -4683,10 +4704,9 @@ static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
4683 * the filesystem is readonly(all dirty pages are written to 4704 * the filesystem is readonly(all dirty pages are written to
4684 * the disk). 4705 * the disk).
4685 */ 4706 */
4686 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items); 4707 btrfs_start_delalloc_roots(fs_info, 0, nr_items);
4687 if (!current->journal_info) 4708 if (!current->journal_info)
4688 btrfs_wait_ordered_roots(root->fs_info, nr_items, 4709 btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1);
4689 0, (u64)-1);
4690 } 4710 }
4691} 4711}
4692 4712
@@ -4710,6 +4730,7 @@ static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4710static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, 4730static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4711 bool wait_ordered) 4731 bool wait_ordered)
4712{ 4732{
4733 struct btrfs_fs_info *fs_info = root->fs_info;
4713 struct btrfs_block_rsv *block_rsv; 4734 struct btrfs_block_rsv *block_rsv;
4714 struct btrfs_space_info *space_info; 4735 struct btrfs_space_info *space_info;
4715 struct btrfs_trans_handle *trans; 4736 struct btrfs_trans_handle *trans;
@@ -4726,17 +4747,16 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4726 to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM; 4747 to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM;
4727 4748
4728 trans = (struct btrfs_trans_handle *)current->journal_info; 4749 trans = (struct btrfs_trans_handle *)current->journal_info;
4729 block_rsv = &root->fs_info->delalloc_block_rsv; 4750 block_rsv = &fs_info->delalloc_block_rsv;
4730 space_info = block_rsv->space_info; 4751 space_info = block_rsv->space_info;
4731 4752
4732 delalloc_bytes = percpu_counter_sum_positive( 4753 delalloc_bytes = percpu_counter_sum_positive(
4733 &root->fs_info->delalloc_bytes); 4754 &fs_info->delalloc_bytes);
4734 if (delalloc_bytes == 0) { 4755 if (delalloc_bytes == 0) {
4735 if (trans) 4756 if (trans)
4736 return; 4757 return;
4737 if (wait_ordered) 4758 if (wait_ordered)
4738 btrfs_wait_ordered_roots(root->fs_info, items, 4759 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
4739 0, (u64)-1);
4740 return; 4760 return;
4741 } 4761 }
4742 4762
@@ -4749,7 +4769,7 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4749 * We need to wait for the async pages to actually start before 4769 * We need to wait for the async pages to actually start before
4750 * we do anything. 4770 * we do anything.
4751 */ 4771 */
4752 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages); 4772 max_reclaim = atomic_read(&fs_info->async_delalloc_pages);
4753 if (!max_reclaim) 4773 if (!max_reclaim)
4754 goto skip_async; 4774 goto skip_async;
4755 4775
@@ -4758,8 +4778,8 @@ static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4758 else 4778 else
4759 max_reclaim -= nr_pages; 4779 max_reclaim -= nr_pages;
4760 4780
4761 wait_event(root->fs_info->async_submit_wait, 4781 wait_event(fs_info->async_submit_wait,
4762 atomic_read(&root->fs_info->async_delalloc_pages) <= 4782 atomic_read(&fs_info->async_delalloc_pages) <=
4763 (int)max_reclaim); 4783 (int)max_reclaim);
4764skip_async: 4784skip_async:
4765 if (!trans) 4785 if (!trans)
@@ -4780,15 +4800,14 @@ skip_async:
4780 4800
4781 loops++; 4801 loops++;
4782 if (wait_ordered && !trans) { 4802 if (wait_ordered && !trans) {
4783 btrfs_wait_ordered_roots(root->fs_info, items, 4803 btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1);
4784 0, (u64)-1);
4785 } else { 4804 } else {
4786 time_left = schedule_timeout_killable(1); 4805 time_left = schedule_timeout_killable(1);
4787 if (time_left) 4806 if (time_left)
4788 break; 4807 break;
4789 } 4808 }
4790 delalloc_bytes = percpu_counter_sum_positive( 4809 delalloc_bytes = percpu_counter_sum_positive(
4791 &root->fs_info->delalloc_bytes); 4810 &fs_info->delalloc_bytes);
4792 } 4811 }
4793} 4812}
4794 4813
@@ -4806,7 +4825,8 @@ static int may_commit_transaction(struct btrfs_root *root,
4806 struct btrfs_space_info *space_info, 4825 struct btrfs_space_info *space_info,
4807 u64 bytes, int force) 4826 u64 bytes, int force)
4808{ 4827{
4809 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; 4828 struct btrfs_fs_info *fs_info = root->fs_info;
4829 struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv;
4810 struct btrfs_trans_handle *trans; 4830 struct btrfs_trans_handle *trans;
4811 4831
4812 trans = (struct btrfs_trans_handle *)current->journal_info; 4832 trans = (struct btrfs_trans_handle *)current->journal_info;
@@ -4855,6 +4875,7 @@ static int flush_space(struct btrfs_root *root,
4855 struct btrfs_space_info *space_info, u64 num_bytes, 4875 struct btrfs_space_info *space_info, u64 num_bytes,
4856 u64 orig_bytes, int state) 4876 u64 orig_bytes, int state)
4857{ 4877{
4878 struct btrfs_fs_info *fs_info = root->fs_info;
4858 struct btrfs_trans_handle *trans; 4879 struct btrfs_trans_handle *trans;
4859 int nr; 4880 int nr;
4860 int ret = 0; 4881 int ret = 0;
@@ -4886,7 +4907,7 @@ static int flush_space(struct btrfs_root *root,
4886 ret = PTR_ERR(trans); 4907 ret = PTR_ERR(trans);
4887 break; 4908 break;
4888 } 4909 }
4889 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 4910 ret = do_chunk_alloc(trans, fs_info->extent_root,
4890 btrfs_get_alloc_profile(root, 0), 4911 btrfs_get_alloc_profile(root, 0),
4891 CHUNK_ALLOC_NO_FORCE); 4912 CHUNK_ALLOC_NO_FORCE);
4892 btrfs_end_transaction(trans, root); 4913 btrfs_end_transaction(trans, root);
@@ -4901,7 +4922,7 @@ static int flush_space(struct btrfs_root *root,
4901 break; 4922 break;
4902 } 4923 }
4903 4924
4904 trace_btrfs_flush_space(root->fs_info, space_info->flags, num_bytes, 4925 trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes,
4905 orig_bytes, state, ret); 4926 orig_bytes, state, ret);
4906 return ret; 4927 return ret;
4907} 4928}
@@ -4947,6 +4968,7 @@ btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4947static inline int need_do_async_reclaim(struct btrfs_space_info *space_info, 4968static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4948 struct btrfs_root *root, u64 used) 4969 struct btrfs_root *root, u64 used)
4949{ 4970{
4971 struct btrfs_fs_info *fs_info = root->fs_info;
4950 u64 thresh = div_factor_fine(space_info->total_bytes, 98); 4972 u64 thresh = div_factor_fine(space_info->total_bytes, 98);
4951 4973
4952 /* If we're just plain full then async reclaim just slows us down. */ 4974 /* If we're just plain full then async reclaim just slows us down. */
@@ -4956,9 +4978,8 @@ static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4956 if (!btrfs_calc_reclaim_metadata_size(root, space_info)) 4978 if (!btrfs_calc_reclaim_metadata_size(root, space_info))
4957 return 0; 4979 return 0;
4958 4980
4959 return (used >= thresh && !btrfs_fs_closing(root->fs_info) && 4981 return (used >= thresh && !btrfs_fs_closing(fs_info) &&
4960 !test_bit(BTRFS_FS_STATE_REMOUNTING, 4982 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4961 &root->fs_info->fs_state));
4962} 4983}
4963 4984
4964static void wake_all_tickets(struct list_head *head) 4985static void wake_all_tickets(struct list_head *head)
@@ -5138,6 +5159,7 @@ static int __reserve_metadata_bytes(struct btrfs_root *root,
5138 u64 orig_bytes, 5159 u64 orig_bytes,
5139 enum btrfs_reserve_flush_enum flush) 5160 enum btrfs_reserve_flush_enum flush)
5140{ 5161{
5162 struct btrfs_fs_info *fs_info = root->fs_info;
5141 struct reserve_ticket ticket; 5163 struct reserve_ticket ticket;
5142 u64 used; 5164 u64 used;
5143 int ret = 0; 5165 int ret = 0;
@@ -5158,15 +5180,13 @@ static int __reserve_metadata_bytes(struct btrfs_root *root,
5158 */ 5180 */
5159 if (used + orig_bytes <= space_info->total_bytes) { 5181 if (used + orig_bytes <= space_info->total_bytes) {
5160 space_info->bytes_may_use += orig_bytes; 5182 space_info->bytes_may_use += orig_bytes;
5161 trace_btrfs_space_reservation(root->fs_info, "space_info", 5183 trace_btrfs_space_reservation(fs_info, "space_info",
5162 space_info->flags, orig_bytes, 5184 space_info->flags, orig_bytes, 1);
5163 1);
5164 ret = 0; 5185 ret = 0;
5165 } else if (can_overcommit(root, space_info, orig_bytes, flush)) { 5186 } else if (can_overcommit(root, space_info, orig_bytes, flush)) {
5166 space_info->bytes_may_use += orig_bytes; 5187 space_info->bytes_may_use += orig_bytes;
5167 trace_btrfs_space_reservation(root->fs_info, "space_info", 5188 trace_btrfs_space_reservation(fs_info, "space_info",
5168 space_info->flags, orig_bytes, 5189 space_info->flags, orig_bytes, 1);
5169 1);
5170 ret = 0; 5190 ret = 0;
5171 } 5191 }
5172 5192
@@ -5185,7 +5205,7 @@ static int __reserve_metadata_bytes(struct btrfs_root *root,
5185 list_add_tail(&ticket.list, &space_info->tickets); 5205 list_add_tail(&ticket.list, &space_info->tickets);
5186 if (!space_info->flush) { 5206 if (!space_info->flush) {
5187 space_info->flush = 1; 5207 space_info->flush = 1;
5188 trace_btrfs_trigger_flush(root->fs_info, 5208 trace_btrfs_trigger_flush(fs_info,
5189 space_info->flags, 5209 space_info->flags,
5190 orig_bytes, flush, 5210 orig_bytes, flush,
5191 "enospc"); 5211 "enospc");
@@ -5203,15 +5223,13 @@ static int __reserve_metadata_bytes(struct btrfs_root *root,
5203 * which means we won't have fs_info->fs_root set, so don't do 5223 * which means we won't have fs_info->fs_root set, so don't do
5204 * the async reclaim as we will panic. 5224 * the async reclaim as we will panic.
5205 */ 5225 */
5206 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags) && 5226 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) &&
5207 need_do_async_reclaim(space_info, root, used) && 5227 need_do_async_reclaim(space_info, root, used) &&
5208 !work_busy(&root->fs_info->async_reclaim_work)) { 5228 !work_busy(&fs_info->async_reclaim_work)) {
5209 trace_btrfs_trigger_flush(root->fs_info, 5229 trace_btrfs_trigger_flush(fs_info, space_info->flags,
5210 space_info->flags, 5230 orig_bytes, flush, "preempt");
5211 orig_bytes, flush,
5212 "preempt");
5213 queue_work(system_unbound_wq, 5231 queue_work(system_unbound_wq,
5214 &root->fs_info->async_reclaim_work); 5232 &fs_info->async_reclaim_work);
5215 } 5233 }
5216 } 5234 }
5217 spin_unlock(&space_info->lock); 5235 spin_unlock(&space_info->lock);
@@ -5219,19 +5237,19 @@ static int __reserve_metadata_bytes(struct btrfs_root *root,
5219 return ret; 5237 return ret;
5220 5238
5221 if (flush == BTRFS_RESERVE_FLUSH_ALL) 5239 if (flush == BTRFS_RESERVE_FLUSH_ALL)
5222 return wait_reserve_ticket(root->fs_info, space_info, &ticket, 5240 return wait_reserve_ticket(fs_info, space_info, &ticket,
5223 orig_bytes); 5241 orig_bytes);
5224 5242
5225 ret = 0; 5243 ret = 0;
5226 priority_reclaim_metadata_space(root->fs_info, space_info, &ticket); 5244 priority_reclaim_metadata_space(fs_info, space_info, &ticket);
5227 spin_lock(&space_info->lock); 5245 spin_lock(&space_info->lock);
5228 if (ticket.bytes) { 5246 if (ticket.bytes) {
5229 if (ticket.bytes < orig_bytes) { 5247 if (ticket.bytes < orig_bytes) {
5230 u64 num_bytes = orig_bytes - ticket.bytes; 5248 u64 num_bytes = orig_bytes - ticket.bytes;
5231 space_info->bytes_may_use -= num_bytes; 5249 space_info->bytes_may_use -= num_bytes;
5232 trace_btrfs_space_reservation(root->fs_info, 5250 trace_btrfs_space_reservation(fs_info, "space_info",
5233 "space_info", space_info->flags, 5251 space_info->flags,
5234 num_bytes, 0); 5252 num_bytes, 0);
5235 5253
5236 } 5254 }
5237 list_del_init(&ticket.list); 5255 list_del_init(&ticket.list);
@@ -5261,22 +5279,20 @@ static int reserve_metadata_bytes(struct btrfs_root *root,
5261 u64 orig_bytes, 5279 u64 orig_bytes,
5262 enum btrfs_reserve_flush_enum flush) 5280 enum btrfs_reserve_flush_enum flush)
5263{ 5281{
5282 struct btrfs_fs_info *fs_info = root->fs_info;
5283 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5264 int ret; 5284 int ret;
5265 5285
5266 ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes, 5286 ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes,
5267 flush); 5287 flush);
5268 if (ret == -ENOSPC && 5288 if (ret == -ENOSPC &&
5269 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { 5289 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
5270 struct btrfs_block_rsv *global_rsv =
5271 &root->fs_info->global_block_rsv;
5272
5273 if (block_rsv != global_rsv && 5290 if (block_rsv != global_rsv &&
5274 !block_rsv_use_bytes(global_rsv, orig_bytes)) 5291 !block_rsv_use_bytes(global_rsv, orig_bytes))
5275 ret = 0; 5292 ret = 0;
5276 } 5293 }
5277 if (ret == -ENOSPC) 5294 if (ret == -ENOSPC)
5278 trace_btrfs_space_reservation(root->fs_info, 5295 trace_btrfs_space_reservation(fs_info, "space_info:enospc",
5279 "space_info:enospc",
5280 block_rsv->space_info->flags, 5296 block_rsv->space_info->flags,
5281 orig_bytes, 1); 5297 orig_bytes, 1);
5282 return ret; 5298 return ret;
@@ -5286,18 +5302,19 @@ static struct btrfs_block_rsv *get_block_rsv(
5286 const struct btrfs_trans_handle *trans, 5302 const struct btrfs_trans_handle *trans,
5287 const struct btrfs_root *root) 5303 const struct btrfs_root *root)
5288{ 5304{
5305 struct btrfs_fs_info *fs_info = root->fs_info;
5289 struct btrfs_block_rsv *block_rsv = NULL; 5306 struct btrfs_block_rsv *block_rsv = NULL;
5290 5307
5291 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 5308 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
5292 (root == root->fs_info->csum_root && trans->adding_csums) || 5309 (root == fs_info->csum_root && trans->adding_csums) ||
5293 (root == root->fs_info->uuid_root)) 5310 (root == fs_info->uuid_root))
5294 block_rsv = trans->block_rsv; 5311 block_rsv = trans->block_rsv;
5295 5312
5296 if (!block_rsv) 5313 if (!block_rsv)
5297 block_rsv = root->block_rsv; 5314 block_rsv = root->block_rsv;
5298 5315
5299 if (!block_rsv) 5316 if (!block_rsv)
5300 block_rsv = &root->fs_info->empty_block_rsv; 5317 block_rsv = &fs_info->empty_block_rsv;
5301 5318
5302 return block_rsv; 5319 return block_rsv;
5303} 5320}
@@ -5619,12 +5636,13 @@ void btrfs_block_rsv_release(struct btrfs_root *root,
5619 struct btrfs_block_rsv *block_rsv, 5636 struct btrfs_block_rsv *block_rsv,
5620 u64 num_bytes) 5637 u64 num_bytes)
5621{ 5638{
5622 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; 5639 struct btrfs_fs_info *fs_info = root->fs_info;
5640 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5641
5623 if (global_rsv == block_rsv || 5642 if (global_rsv == block_rsv ||
5624 block_rsv->space_info != global_rsv->space_info) 5643 block_rsv->space_info != global_rsv->space_info)
5625 global_rsv = NULL; 5644 global_rsv = NULL;
5626 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv, 5645 block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes);
5627 num_bytes);
5628} 5646}
5629 5647
5630static void update_global_block_rsv(struct btrfs_fs_info *fs_info) 5648static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
@@ -5721,13 +5739,15 @@ static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
5721void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, 5739void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
5722 struct btrfs_root *root) 5740 struct btrfs_root *root)
5723{ 5741{
5742 struct btrfs_fs_info *fs_info = root->fs_info;
5743
5724 if (!trans->block_rsv) 5744 if (!trans->block_rsv)
5725 return; 5745 return;
5726 5746
5727 if (!trans->bytes_reserved) 5747 if (!trans->bytes_reserved)
5728 return; 5748 return;
5729 5749
5730 trace_btrfs_space_reservation(root->fs_info, "transaction", 5750 trace_btrfs_space_reservation(fs_info, "transaction",
5731 trans->transid, trans->bytes_reserved, 0); 5751 trans->transid, trans->bytes_reserved, 0);
5732 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); 5752 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
5733 trans->bytes_reserved = 0; 5753 trans->bytes_reserved = 0;
@@ -5755,6 +5775,7 @@ void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans)
5755int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, 5775int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
5756 struct inode *inode) 5776 struct inode *inode)
5757{ 5777{
5778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5758 struct btrfs_root *root = BTRFS_I(inode)->root; 5779 struct btrfs_root *root = BTRFS_I(inode)->root;
5759 /* 5780 /*
5760 * We always use trans->block_rsv here as we will have reserved space 5781 * We always use trans->block_rsv here as we will have reserved space
@@ -5770,17 +5791,20 @@ int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
5770 * added it, so this takes the reservation so we can release it later 5791 * added it, so this takes the reservation so we can release it later
5771 * when we are truly done with the orphan item. 5792 * when we are truly done with the orphan item.
5772 */ 5793 */
5773 u64 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 5794 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5774 trace_btrfs_space_reservation(root->fs_info, "orphan", 5795
5796 trace_btrfs_space_reservation(fs_info, "orphan",
5775 btrfs_ino(inode), num_bytes, 1); 5797 btrfs_ino(inode), num_bytes, 1);
5776 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); 5798 return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1);
5777} 5799}
5778 5800
5779void btrfs_orphan_release_metadata(struct inode *inode) 5801void btrfs_orphan_release_metadata(struct inode *inode)
5780{ 5802{
5803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5781 struct btrfs_root *root = BTRFS_I(inode)->root; 5804 struct btrfs_root *root = BTRFS_I(inode)->root;
5782 u64 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); 5805 u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1);
5783 trace_btrfs_space_reservation(root->fs_info, "orphan", 5806
5807 trace_btrfs_space_reservation(fs_info, "orphan",
5784 btrfs_ino(inode), num_bytes, 0); 5808 btrfs_ino(inode), num_bytes, 0);
5785 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); 5809 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
5786} 5810}
@@ -5807,11 +5831,12 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5807{ 5831{
5808 u64 num_bytes; 5832 u64 num_bytes;
5809 int ret; 5833 int ret;
5810 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; 5834 struct btrfs_fs_info *fs_info = root->fs_info;
5835 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5811 5836
5812 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags)) { 5837 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
5813 /* One for parent inode, two for dir entries */ 5838 /* One for parent inode, two for dir entries */
5814 num_bytes = 3 * root->fs_info->nodesize; 5839 num_bytes = 3 * fs_info->nodesize;
5815 ret = btrfs_qgroup_reserve_meta(root, num_bytes); 5840 ret = btrfs_qgroup_reserve_meta(root, num_bytes);
5816 if (ret) 5841 if (ret)
5817 return ret; 5842 return ret;
@@ -5821,8 +5846,8 @@ int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
5821 5846
5822 *qgroup_reserved = num_bytes; 5847 *qgroup_reserved = num_bytes;
5823 5848
5824 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, items); 5849 num_bytes = btrfs_calc_trans_metadata_size(fs_info, items);
5825 rsv->space_info = __find_space_info(root->fs_info, 5850 rsv->space_info = __find_space_info(fs_info,
5826 BTRFS_BLOCK_GROUP_METADATA); 5851 BTRFS_BLOCK_GROUP_METADATA);
5827 ret = btrfs_block_rsv_add(root, rsv, num_bytes, 5852 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5828 BTRFS_RESERVE_FLUSH_ALL); 5853 BTRFS_RESERVE_FLUSH_ALL);
@@ -5906,6 +5931,7 @@ static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
5906static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, 5931static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5907 int reserve) 5932 int reserve)
5908{ 5933{
5934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5909 struct btrfs_root *root = BTRFS_I(inode)->root; 5935 struct btrfs_root *root = BTRFS_I(inode)->root;
5910 u64 old_csums, num_csums; 5936 u64 old_csums, num_csums;
5911 5937
@@ -5925,17 +5951,17 @@ static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5925 return 0; 5951 return 0;
5926 5952
5927 if (reserve) 5953 if (reserve)
5928 return btrfs_calc_trans_metadata_size(root->fs_info, 5954 return btrfs_calc_trans_metadata_size(fs_info,
5929 num_csums - old_csums); 5955 num_csums - old_csums);
5930 5956
5931 return btrfs_calc_trans_metadata_size(root->fs_info, 5957 return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums);
5932 old_csums - num_csums);
5933} 5958}
5934 5959
5935int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) 5960int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5936{ 5961{
5962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5937 struct btrfs_root *root = BTRFS_I(inode)->root; 5963 struct btrfs_root *root = BTRFS_I(inode)->root;
5938 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; 5964 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
5939 u64 to_reserve = 0; 5965 u64 to_reserve = 0;
5940 u64 csum_bytes; 5966 u64 csum_bytes;
5941 unsigned nr_extents = 0; 5967 unsigned nr_extents = 0;
@@ -5962,13 +5988,13 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5962 } 5988 }
5963 5989
5964 if (flush != BTRFS_RESERVE_NO_FLUSH && 5990 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5965 btrfs_transaction_in_commit(root->fs_info)) 5991 btrfs_transaction_in_commit(fs_info))
5966 schedule_timeout(1); 5992 schedule_timeout(1);
5967 5993
5968 if (delalloc_lock) 5994 if (delalloc_lock)
5969 mutex_lock(&BTRFS_I(inode)->delalloc_mutex); 5995 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5970 5996
5971 num_bytes = ALIGN(num_bytes, root->fs_info->sectorsize); 5997 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
5972 5998
5973 spin_lock(&BTRFS_I(inode)->lock); 5999 spin_lock(&BTRFS_I(inode)->lock);
5974 nr_extents = (unsigned)div64_u64(num_bytes + 6000 nr_extents = (unsigned)div64_u64(num_bytes +
@@ -5983,15 +6009,14 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5983 BTRFS_I(inode)->reserved_extents; 6009 BTRFS_I(inode)->reserved_extents;
5984 6010
5985 /* We always want to reserve a slot for updating the inode. */ 6011 /* We always want to reserve a slot for updating the inode. */
5986 to_reserve = btrfs_calc_trans_metadata_size(root->fs_info, 6012 to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1);
5987 nr_extents + 1);
5988 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); 6013 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
5989 csum_bytes = BTRFS_I(inode)->csum_bytes; 6014 csum_bytes = BTRFS_I(inode)->csum_bytes;
5990 spin_unlock(&BTRFS_I(inode)->lock); 6015 spin_unlock(&BTRFS_I(inode)->lock);
5991 6016
5992 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags)) { 6017 if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) {
5993 ret = btrfs_qgroup_reserve_meta(root, 6018 ret = btrfs_qgroup_reserve_meta(root,
5994 nr_extents * root->fs_info->nodesize); 6019 nr_extents * fs_info->nodesize);
5995 if (ret) 6020 if (ret)
5996 goto out_fail; 6021 goto out_fail;
5997 } 6022 }
@@ -5999,14 +6024,14 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5999 ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush); 6024 ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush);
6000 if (unlikely(ret)) { 6025 if (unlikely(ret)) {
6001 btrfs_qgroup_free_meta(root, 6026 btrfs_qgroup_free_meta(root,
6002 nr_extents * root->fs_info->nodesize); 6027 nr_extents * fs_info->nodesize);
6003 goto out_fail; 6028 goto out_fail;
6004 } 6029 }
6005 6030
6006 spin_lock(&BTRFS_I(inode)->lock); 6031 spin_lock(&BTRFS_I(inode)->lock);
6007 if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, 6032 if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
6008 &BTRFS_I(inode)->runtime_flags)) { 6033 &BTRFS_I(inode)->runtime_flags)) {
6009 to_reserve -= btrfs_calc_trans_metadata_size(root->fs_info, 1); 6034 to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1);
6010 release_extra = true; 6035 release_extra = true;
6011 } 6036 }
6012 BTRFS_I(inode)->reserved_extents += nr_extents; 6037 BTRFS_I(inode)->reserved_extents += nr_extents;
@@ -6016,11 +6041,11 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
6016 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); 6041 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
6017 6042
6018 if (to_reserve) 6043 if (to_reserve)
6019 trace_btrfs_space_reservation(root->fs_info, "delalloc", 6044 trace_btrfs_space_reservation(fs_info, "delalloc",
6020 btrfs_ino(inode), to_reserve, 1); 6045 btrfs_ino(inode), to_reserve, 1);
6021 if (release_extra) 6046 if (release_extra)
6022 btrfs_block_rsv_release(root, block_rsv, 6047 btrfs_block_rsv_release(root, block_rsv,
6023 btrfs_calc_trans_metadata_size(root->fs_info, 1)); 6048 btrfs_calc_trans_metadata_size(fs_info, 1));
6024 return 0; 6049 return 0;
6025 6050
6026out_fail: 6051out_fail:
@@ -6075,12 +6100,11 @@ out_fail:
6075 } 6100 }
6076 spin_unlock(&BTRFS_I(inode)->lock); 6101 spin_unlock(&BTRFS_I(inode)->lock);
6077 if (dropped) 6102 if (dropped)
6078 to_free += btrfs_calc_trans_metadata_size(root->fs_info, 6103 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
6079 dropped);
6080 6104
6081 if (to_free) { 6105 if (to_free) {
6082 btrfs_block_rsv_release(root, block_rsv, to_free); 6106 btrfs_block_rsv_release(root, block_rsv, to_free);
6083 trace_btrfs_space_reservation(root->fs_info, "delalloc", 6107 trace_btrfs_space_reservation(fs_info, "delalloc",
6084 btrfs_ino(inode), to_free, 0); 6108 btrfs_ino(inode), to_free, 0);
6085 } 6109 }
6086 if (delalloc_lock) 6110 if (delalloc_lock)
@@ -6099,11 +6123,12 @@ out_fail:
6099 */ 6123 */
6100void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) 6124void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
6101{ 6125{
6126 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6102 struct btrfs_root *root = BTRFS_I(inode)->root; 6127 struct btrfs_root *root = BTRFS_I(inode)->root;
6103 u64 to_free = 0; 6128 u64 to_free = 0;
6104 unsigned dropped; 6129 unsigned dropped;
6105 6130
6106 num_bytes = ALIGN(num_bytes, root->fs_info->sectorsize); 6131 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
6107 spin_lock(&BTRFS_I(inode)->lock); 6132 spin_lock(&BTRFS_I(inode)->lock);
6108 dropped = drop_outstanding_extent(inode, num_bytes); 6133 dropped = drop_outstanding_extent(inode, num_bytes);
6109 6134
@@ -6111,17 +6136,15 @@ void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
6111 to_free = calc_csum_metadata_size(inode, num_bytes, 0); 6136 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
6112 spin_unlock(&BTRFS_I(inode)->lock); 6137 spin_unlock(&BTRFS_I(inode)->lock);
6113 if (dropped > 0) 6138 if (dropped > 0)
6114 to_free += btrfs_calc_trans_metadata_size(root->fs_info, 6139 to_free += btrfs_calc_trans_metadata_size(fs_info, dropped);
6115 dropped);
6116 6140
6117 if (btrfs_is_testing(root->fs_info)) 6141 if (btrfs_is_testing(fs_info))
6118 return; 6142 return;
6119 6143
6120 trace_btrfs_space_reservation(root->fs_info, "delalloc", 6144 trace_btrfs_space_reservation(fs_info, "delalloc",
6121 btrfs_ino(inode), to_free, 0); 6145 btrfs_ino(inode), to_free, 0);
6122 6146
6123 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, 6147 btrfs_block_rsv_release(root, &fs_info->delalloc_block_rsv, to_free);
6124 to_free);
6125} 6148}
6126 6149
6127/** 6150/**
@@ -6251,7 +6274,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
6251 spin_unlock(&cache->lock); 6274 spin_unlock(&cache->lock);
6252 spin_unlock(&cache->space_info->lock); 6275 spin_unlock(&cache->space_info->lock);
6253 6276
6254 trace_btrfs_space_reservation(root->fs_info, "pinned", 6277 trace_btrfs_space_reservation(info, "pinned",
6255 cache->space_info->flags, 6278 cache->space_info->flags,
6256 num_bytes, 1); 6279 num_bytes, 1);
6257 set_extent_dirty(info->pinned_extents, 6280 set_extent_dirty(info->pinned_extents,
@@ -6293,17 +6316,18 @@ static int update_block_group(struct btrfs_trans_handle *trans,
6293 6316
6294static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) 6317static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
6295{ 6318{
6319 struct btrfs_fs_info *fs_info = root->fs_info;
6296 struct btrfs_block_group_cache *cache; 6320 struct btrfs_block_group_cache *cache;
6297 u64 bytenr; 6321 u64 bytenr;
6298 6322
6299 spin_lock(&root->fs_info->block_group_cache_lock); 6323 spin_lock(&fs_info->block_group_cache_lock);
6300 bytenr = root->fs_info->first_logical_byte; 6324 bytenr = fs_info->first_logical_byte;
6301 spin_unlock(&root->fs_info->block_group_cache_lock); 6325 spin_unlock(&fs_info->block_group_cache_lock);
6302 6326
6303 if (bytenr < (u64)-1) 6327 if (bytenr < (u64)-1)
6304 return bytenr; 6328 return bytenr;
6305 6329
6306 cache = btrfs_lookup_first_block_group(root->fs_info, search_start); 6330 cache = btrfs_lookup_first_block_group(fs_info, search_start);
6307 if (!cache) 6331 if (!cache)
6308 return 0; 6332 return 0;
6309 6333
@@ -6317,6 +6341,8 @@ static int pin_down_extent(struct btrfs_root *root,
6317 struct btrfs_block_group_cache *cache, 6341 struct btrfs_block_group_cache *cache,
6318 u64 bytenr, u64 num_bytes, int reserved) 6342 u64 bytenr, u64 num_bytes, int reserved)
6319{ 6343{
6344 struct btrfs_fs_info *fs_info = cache->fs_info;
6345
6320 spin_lock(&cache->space_info->lock); 6346 spin_lock(&cache->space_info->lock);
6321 spin_lock(&cache->lock); 6347 spin_lock(&cache->lock);
6322 cache->pinned += num_bytes; 6348 cache->pinned += num_bytes;
@@ -6328,9 +6354,9 @@ static int pin_down_extent(struct btrfs_root *root,
6328 spin_unlock(&cache->lock); 6354 spin_unlock(&cache->lock);
6329 spin_unlock(&cache->space_info->lock); 6355 spin_unlock(&cache->space_info->lock);
6330 6356
6331 trace_btrfs_space_reservation(root->fs_info, "pinned", 6357 trace_btrfs_space_reservation(fs_info, "pinned",
6332 cache->space_info->flags, num_bytes, 1); 6358 cache->space_info->flags, num_bytes, 1);
6333 set_extent_dirty(root->fs_info->pinned_extents, bytenr, 6359 set_extent_dirty(fs_info->pinned_extents, bytenr,
6334 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); 6360 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
6335 return 0; 6361 return 0;
6336} 6362}
@@ -6341,9 +6367,10 @@ static int pin_down_extent(struct btrfs_root *root,
6341int btrfs_pin_extent(struct btrfs_root *root, 6367int btrfs_pin_extent(struct btrfs_root *root,
6342 u64 bytenr, u64 num_bytes, int reserved) 6368 u64 bytenr, u64 num_bytes, int reserved)
6343{ 6369{
6370 struct btrfs_fs_info *fs_info = root->fs_info;
6344 struct btrfs_block_group_cache *cache; 6371 struct btrfs_block_group_cache *cache;
6345 6372
6346 cache = btrfs_lookup_block_group(root->fs_info, bytenr); 6373 cache = btrfs_lookup_block_group(fs_info, bytenr);
6347 BUG_ON(!cache); /* Logic error */ 6374 BUG_ON(!cache); /* Logic error */
6348 6375
6349 pin_down_extent(root, cache, bytenr, num_bytes, reserved); 6376 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
@@ -6358,10 +6385,11 @@ int btrfs_pin_extent(struct btrfs_root *root,
6358int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, 6385int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
6359 u64 bytenr, u64 num_bytes) 6386 u64 bytenr, u64 num_bytes)
6360{ 6387{
6388 struct btrfs_fs_info *fs_info = root->fs_info;
6361 struct btrfs_block_group_cache *cache; 6389 struct btrfs_block_group_cache *cache;
6362 int ret; 6390 int ret;
6363 6391
6364 cache = btrfs_lookup_block_group(root->fs_info, bytenr); 6392 cache = btrfs_lookup_block_group(fs_info, bytenr);
6365 if (!cache) 6393 if (!cache)
6366 return -EINVAL; 6394 return -EINVAL;
6367 6395
@@ -6383,11 +6411,12 @@ int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
6383 6411
6384static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes) 6412static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
6385{ 6413{
6414 struct btrfs_fs_info *fs_info = root->fs_info;
6386 int ret; 6415 int ret;
6387 struct btrfs_block_group_cache *block_group; 6416 struct btrfs_block_group_cache *block_group;
6388 struct btrfs_caching_control *caching_ctl; 6417 struct btrfs_caching_control *caching_ctl;
6389 6418
6390 block_group = btrfs_lookup_block_group(root->fs_info, start); 6419 block_group = btrfs_lookup_block_group(fs_info, start);
6391 if (!block_group) 6420 if (!block_group)
6392 return -EINVAL; 6421 return -EINVAL;
6393 6422
@@ -6615,8 +6644,9 @@ static struct btrfs_free_cluster *
6615fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info, 6644fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info,
6616 u64 *empty_cluster) 6645 u64 *empty_cluster)
6617{ 6646{
6647 struct btrfs_fs_info *fs_info = root->fs_info;
6618 struct btrfs_free_cluster *ret = NULL; 6648 struct btrfs_free_cluster *ret = NULL;
6619 bool ssd = btrfs_test_opt(root->fs_info, SSD); 6649 bool ssd = btrfs_test_opt(fs_info, SSD);
6620 6650
6621 *empty_cluster = 0; 6651 *empty_cluster = 0;
6622 if (btrfs_mixed_space_info(space_info)) 6652 if (btrfs_mixed_space_info(space_info))
@@ -6625,11 +6655,11 @@ fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info,
6625 if (ssd) 6655 if (ssd)
6626 *empty_cluster = SZ_2M; 6656 *empty_cluster = SZ_2M;
6627 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { 6657 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
6628 ret = &root->fs_info->meta_alloc_cluster; 6658 ret = &fs_info->meta_alloc_cluster;
6629 if (!ssd) 6659 if (!ssd)
6630 *empty_cluster = SZ_64K; 6660 *empty_cluster = SZ_64K;
6631 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) { 6661 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) {
6632 ret = &root->fs_info->data_alloc_cluster; 6662 ret = &fs_info->data_alloc_cluster;
6633 } 6663 }
6634 6664
6635 return ret; 6665 return ret;
@@ -6761,7 +6791,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
6761 break; 6791 break;
6762 } 6792 }
6763 6793
6764 if (btrfs_test_opt(root->fs_info, DISCARD)) 6794 if (btrfs_test_opt(fs_info, DISCARD))
6765 ret = btrfs_discard_extent(root, start, 6795 ret = btrfs_discard_extent(root, start,
6766 end + 1 - start, NULL); 6796 end + 1 - start, NULL);
6767 6797
@@ -6847,8 +6877,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
6847 u64 bytenr = node->bytenr; 6877 u64 bytenr = node->bytenr;
6848 u64 num_bytes = node->num_bytes; 6878 u64 num_bytes = node->num_bytes;
6849 int last_ref = 0; 6879 int last_ref = 0;
6850 bool skinny_metadata = btrfs_fs_incompat(root->fs_info, 6880 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
6851 SKINNY_METADATA);
6852 6881
6853 path = btrfs_alloc_path(); 6882 path = btrfs_alloc_path();
6854 if (!path) 6883 if (!path)
@@ -7048,7 +7077,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
7048 goto out; 7077 goto out;
7049 } 7078 }
7050 } 7079 }
7051 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid, 7080 add_pinned_bytes(info, -num_bytes, owner_objectid,
7052 root_objectid); 7081 root_objectid);
7053 } else { 7082 } else {
7054 if (found_extent) { 7083 if (found_extent) {
@@ -7080,15 +7109,13 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
7080 } 7109 }
7081 } 7110 }
7082 7111
7083 ret = add_to_free_space_tree(trans, root->fs_info, bytenr, 7112 ret = add_to_free_space_tree(trans, info, bytenr, num_bytes);
7084 num_bytes);
7085 if (ret) { 7113 if (ret) {
7086 btrfs_abort_transaction(trans, ret); 7114 btrfs_abort_transaction(trans, ret);
7087 goto out; 7115 goto out;
7088 } 7116 }
7089 7117
7090 ret = update_block_group(trans, root->fs_info, bytenr, 7118 ret = update_block_group(trans, info, bytenr, num_bytes, 0);
7091 num_bytes, 0);
7092 if (ret) { 7119 if (ret) {
7093 btrfs_abort_transaction(trans, ret); 7120 btrfs_abort_transaction(trans, ret);
7094 goto out; 7121 goto out;
@@ -7178,15 +7205,17 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7178 struct extent_buffer *buf, 7205 struct extent_buffer *buf,
7179 u64 parent, int last_ref) 7206 u64 parent, int last_ref)
7180{ 7207{
7208 struct btrfs_fs_info *fs_info = root->fs_info;
7181 int pin = 1; 7209 int pin = 1;
7182 int ret; 7210 int ret;
7183 7211
7184 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 7212 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
7185 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, 7213 ret = btrfs_add_delayed_tree_ref(fs_info, trans,
7186 buf->start, buf->len, 7214 buf->start, buf->len,
7187 parent, root->root_key.objectid, 7215 parent,
7188 btrfs_header_level(buf), 7216 root->root_key.objectid,
7189 BTRFS_DROP_DELAYED_REF, NULL); 7217 btrfs_header_level(buf),
7218 BTRFS_DROP_DELAYED_REF, NULL);
7190 BUG_ON(ret); /* -ENOMEM */ 7219 BUG_ON(ret); /* -ENOMEM */
7191 } 7220 }
7192 7221
@@ -7202,7 +7231,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7202 goto out; 7231 goto out;
7203 } 7232 }
7204 7233
7205 cache = btrfs_lookup_block_group(root->fs_info, buf->start); 7234 cache = btrfs_lookup_block_group(fs_info, buf->start);
7206 7235
7207 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 7236 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
7208 pin_down_extent(root, cache, buf->start, buf->len, 1); 7237 pin_down_extent(root, cache, buf->start, buf->len, 1);
@@ -7220,8 +7249,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
7220 } 7249 }
7221out: 7250out:
7222 if (pin) 7251 if (pin)
7223 add_pinned_bytes(root->fs_info, buf->len, 7252 add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf),
7224 btrfs_header_level(buf),
7225 root->root_key.objectid); 7253 root->root_key.objectid);
7226 7254
7227 /* 7255 /*
@@ -7242,7 +7270,7 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7242 if (btrfs_is_testing(fs_info)) 7270 if (btrfs_is_testing(fs_info))
7243 return 0; 7271 return 0;
7244 7272
7245 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid); 7273 add_pinned_bytes(fs_info, num_bytes, owner, root_objectid);
7246 7274
7247 /* 7275 /*
7248 * tree log blocks never actually go into the extent allocation 7276 * tree log blocks never actually go into the extent allocation
@@ -7442,8 +7470,9 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7442 u64 hint_byte, struct btrfs_key *ins, 7470 u64 hint_byte, struct btrfs_key *ins,
7443 u64 flags, int delalloc) 7471 u64 flags, int delalloc)
7444{ 7472{
7473 struct btrfs_fs_info *fs_info = orig_root->fs_info;
7445 int ret = 0; 7474 int ret = 0;
7446 struct btrfs_root *root = orig_root->fs_info->extent_root; 7475 struct btrfs_root *root = fs_info->extent_root;
7447 struct btrfs_free_cluster *last_ptr = NULL; 7476 struct btrfs_free_cluster *last_ptr = NULL;
7448 struct btrfs_block_group_cache *block_group = NULL; 7477 struct btrfs_block_group_cache *block_group = NULL;
7449 u64 search_start = 0; 7478 u64 search_start = 0;
@@ -7459,16 +7488,16 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7459 bool orig_have_caching_bg = false; 7488 bool orig_have_caching_bg = false;
7460 bool full_search = false; 7489 bool full_search = false;
7461 7490
7462 WARN_ON(num_bytes < root->fs_info->sectorsize); 7491 WARN_ON(num_bytes < fs_info->sectorsize);
7463 ins->type = BTRFS_EXTENT_ITEM_KEY; 7492 ins->type = BTRFS_EXTENT_ITEM_KEY;
7464 ins->objectid = 0; 7493 ins->objectid = 0;
7465 ins->offset = 0; 7494 ins->offset = 0;
7466 7495
7467 trace_find_free_extent(orig_root, num_bytes, empty_size, flags); 7496 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
7468 7497
7469 space_info = __find_space_info(root->fs_info, flags); 7498 space_info = __find_space_info(fs_info, flags);
7470 if (!space_info) { 7499 if (!space_info) {
7471 btrfs_err(root->fs_info, "No space info for %llu", flags); 7500 btrfs_err(fs_info, "No space info for %llu", flags);
7472 return -ENOSPC; 7501 return -ENOSPC;
7473 } 7502 }
7474 7503
@@ -7515,8 +7544,7 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
7515 search_start = max(search_start, first_logical_byte(root, 0)); 7544 search_start = max(search_start, first_logical_byte(root, 0));
7516 search_start = max(search_start, hint_byte); 7545 search_start = max(search_start, hint_byte);
7517 if (search_start == hint_byte) { 7546 if (search_start == hint_byte) {
7518 block_group = btrfs_lookup_block_group(root->fs_info, 7547 block_group = btrfs_lookup_block_group(fs_info, search_start);
7519 search_start);
7520 /* 7548 /*
7521 * we don't want to use the block group if it doesn't match our 7549 * we don't want to use the block group if it doesn't match our
7522 * allocation bits, or if its not cached. 7550 * allocation bits, or if its not cached.
@@ -7769,7 +7797,7 @@ unclustered_alloc:
7769 goto loop; 7797 goto loop;
7770 } 7798 }
7771checks: 7799checks:
7772 search_start = ALIGN(offset, root->fs_info->stripesize); 7800 search_start = ALIGN(offset, fs_info->stripesize);
7773 7801
7774 /* move on to the next group */ 7802 /* move on to the next group */
7775 if (search_start + num_bytes > 7803 if (search_start + num_bytes >
@@ -7968,7 +7996,7 @@ int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
7968 7996
7969 flags = btrfs_get_alloc_profile(root, is_data); 7997 flags = btrfs_get_alloc_profile(root, is_data);
7970again: 7998again:
7971 WARN_ON(num_bytes < root->fs_info->sectorsize); 7999 WARN_ON(num_bytes < fs_info->sectorsize);
7972 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size, 8000 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
7973 hint_byte, ins, flags, delalloc); 8001 hint_byte, ins, flags, delalloc);
7974 if (!ret && !is_data) { 8002 if (!ret && !is_data) {
@@ -7977,7 +8005,7 @@ again:
7977 if (!final_tried && ins->offset) { 8005 if (!final_tried && ins->offset) {
7978 num_bytes = min(num_bytes >> 1, ins->offset); 8006 num_bytes = min(num_bytes >> 1, ins->offset);
7979 num_bytes = round_down(num_bytes, 8007 num_bytes = round_down(num_bytes,
7980 root->fs_info->sectorsize); 8008 fs_info->sectorsize);
7981 num_bytes = max(num_bytes, min_alloc_size); 8009 num_bytes = max(num_bytes, min_alloc_size);
7982 ram_bytes = num_bytes; 8010 ram_bytes = num_bytes;
7983 if (num_bytes == min_alloc_size) 8011 if (num_bytes == min_alloc_size)
@@ -7987,7 +8015,7 @@ again:
7987 struct btrfs_space_info *sinfo; 8015 struct btrfs_space_info *sinfo;
7988 8016
7989 sinfo = __find_space_info(fs_info, flags); 8017 sinfo = __find_space_info(fs_info, flags);
7990 btrfs_err(root->fs_info, 8018 btrfs_err(fs_info,
7991 "allocation failed flags %llu, wanted %llu", 8019 "allocation failed flags %llu, wanted %llu",
7992 flags, num_bytes); 8020 flags, num_bytes);
7993 if (sinfo) 8021 if (sinfo)
@@ -8002,20 +8030,21 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
8002 u64 start, u64 len, 8030 u64 start, u64 len,
8003 int pin, int delalloc) 8031 int pin, int delalloc)
8004{ 8032{
8033 struct btrfs_fs_info *fs_info = root->fs_info;
8005 struct btrfs_block_group_cache *cache; 8034 struct btrfs_block_group_cache *cache;
8006 int ret = 0; 8035 int ret = 0;
8007 8036
8008 cache = btrfs_lookup_block_group(root->fs_info, start); 8037 cache = btrfs_lookup_block_group(fs_info, start);
8009 if (!cache) { 8038 if (!cache) {
8010 btrfs_err(root->fs_info, "Unable to find block group for %llu", 8039 btrfs_err(fs_info, "Unable to find block group for %llu",
8011 start); 8040 start);
8012 return -ENOSPC; 8041 return -ENOSPC;
8013 } 8042 }
8014 8043
8015 if (pin) 8044 if (pin)
8016 pin_down_extent(root, cache, start, len, 1); 8045 pin_down_extent(root, cache, start, len, 1);
8017 else { 8046 else {
8018 if (btrfs_test_opt(root->fs_info, DISCARD)) 8047 if (btrfs_test_opt(fs_info, DISCARD))
8019 ret = btrfs_discard_extent(root, start, len, NULL); 8048 ret = btrfs_discard_extent(root, start, len, NULL);
8020 btrfs_add_free_space(cache, start, len); 8049 btrfs_add_free_space(cache, start, len);
8021 btrfs_free_reserved_bytes(cache, len, delalloc); 8050 btrfs_free_reserved_bytes(cache, len, delalloc);
@@ -8130,8 +8159,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8130 struct extent_buffer *leaf; 8159 struct extent_buffer *leaf;
8131 u32 size = sizeof(*extent_item) + sizeof(*iref); 8160 u32 size = sizeof(*extent_item) + sizeof(*iref);
8132 u64 num_bytes = ins->offset; 8161 u64 num_bytes = ins->offset;
8133 bool skinny_metadata = btrfs_fs_incompat(root->fs_info, 8162 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
8134 SKINNY_METADATA);
8135 8163
8136 if (!skinny_metadata) 8164 if (!skinny_metadata)
8137 size += sizeof(*block_info); 8165 size += sizeof(*block_info);
@@ -8139,7 +8167,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8139 path = btrfs_alloc_path(); 8167 path = btrfs_alloc_path();
8140 if (!path) { 8168 if (!path) {
8141 btrfs_free_and_pin_reserved_extent(root, ins->objectid, 8169 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
8142 root->fs_info->nodesize); 8170 fs_info->nodesize);
8143 return -ENOMEM; 8171 return -ENOMEM;
8144 } 8172 }
8145 8173
@@ -8149,7 +8177,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8149 if (ret) { 8177 if (ret) {
8150 btrfs_free_path(path); 8178 btrfs_free_path(path);
8151 btrfs_free_and_pin_reserved_extent(root, ins->objectid, 8179 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
8152 root->fs_info->nodesize); 8180 fs_info->nodesize);
8153 return ret; 8181 return ret;
8154 } 8182 }
8155 8183
@@ -8163,7 +8191,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8163 8191
8164 if (skinny_metadata) { 8192 if (skinny_metadata) {
8165 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); 8193 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
8166 num_bytes = root->fs_info->nodesize; 8194 num_bytes = fs_info->nodesize;
8167 } else { 8195 } else {
8168 block_info = (struct btrfs_tree_block_info *)(extent_item + 1); 8196 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
8169 btrfs_set_tree_block_key(leaf, block_info, key); 8197 btrfs_set_tree_block_key(leaf, block_info, key);
@@ -8199,7 +8227,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
8199 } 8227 }
8200 8228
8201 trace_btrfs_reserved_extent_alloc(root, ins->objectid, 8229 trace_btrfs_reserved_extent_alloc(root, ins->objectid,
8202 root->fs_info->nodesize); 8230 fs_info->nodesize);
8203 return ret; 8231 return ret;
8204} 8232}
8205 8233
@@ -8209,11 +8237,12 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
8209 u64 offset, u64 ram_bytes, 8237 u64 offset, u64 ram_bytes,
8210 struct btrfs_key *ins) 8238 struct btrfs_key *ins)
8211{ 8239{
8240 struct btrfs_fs_info *fs_info = root->fs_info;
8212 int ret; 8241 int ret;
8213 8242
8214 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); 8243 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
8215 8244
8216 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, 8245 ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid,
8217 ins->offset, 0, 8246 ins->offset, 0,
8218 root_objectid, owner, offset, 8247 root_objectid, owner, offset,
8219 ram_bytes, BTRFS_ADD_DELAYED_EXTENT, 8248 ram_bytes, BTRFS_ADD_DELAYED_EXTENT,
@@ -8231,6 +8260,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8231 u64 root_objectid, u64 owner, u64 offset, 8260 u64 root_objectid, u64 owner, u64 offset,
8232 struct btrfs_key *ins) 8261 struct btrfs_key *ins)
8233{ 8262{
8263 struct btrfs_fs_info *fs_info = root->fs_info;
8234 int ret; 8264 int ret;
8235 struct btrfs_block_group_cache *block_group; 8265 struct btrfs_block_group_cache *block_group;
8236 struct btrfs_space_info *space_info; 8266 struct btrfs_space_info *space_info;
@@ -8239,13 +8269,13 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
8239 * Mixed block groups will exclude before processing the log so we only 8269 * Mixed block groups will exclude before processing the log so we only
8240 * need to do the exclude dance if this fs isn't mixed. 8270 * need to do the exclude dance if this fs isn't mixed.
8241 */ 8271 */
8242 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) { 8272 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
8243 ret = __exclude_logged_extent(root, ins->objectid, ins->offset); 8273 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
8244 if (ret) 8274 if (ret)
8245 return ret; 8275 return ret;
8246 } 8276 }
8247 8277
8248 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); 8278 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
8249 if (!block_group) 8279 if (!block_group)
8250 return -EINVAL; 8280 return -EINVAL;
8251 8281
@@ -8267,6 +8297,7 @@ static struct extent_buffer *
8267btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, 8297btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
8268 u64 bytenr, int level) 8298 u64 bytenr, int level)
8269{ 8299{
8300 struct btrfs_fs_info *fs_info = root->fs_info;
8270 struct extent_buffer *buf; 8301 struct extent_buffer *buf;
8271 8302
8272 buf = btrfs_find_create_tree_block(root, bytenr); 8303 buf = btrfs_find_create_tree_block(root, bytenr);
@@ -8276,7 +8307,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
8276 btrfs_set_header_generation(buf, trans->transid); 8307 btrfs_set_header_generation(buf, trans->transid);
8277 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); 8308 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
8278 btrfs_tree_lock(buf); 8309 btrfs_tree_lock(buf);
8279 clean_tree_block(trans, root->fs_info, buf); 8310 clean_tree_block(trans, fs_info, buf);
8280 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); 8311 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
8281 8312
8282 btrfs_set_lock_blocking(buf); 8313 btrfs_set_lock_blocking(buf);
@@ -8308,8 +8339,9 @@ static struct btrfs_block_rsv *
8308use_block_rsv(struct btrfs_trans_handle *trans, 8339use_block_rsv(struct btrfs_trans_handle *trans,
8309 struct btrfs_root *root, u32 blocksize) 8340 struct btrfs_root *root, u32 blocksize)
8310{ 8341{
8342 struct btrfs_fs_info *fs_info = root->fs_info;
8311 struct btrfs_block_rsv *block_rsv; 8343 struct btrfs_block_rsv *block_rsv;
8312 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; 8344 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
8313 int ret; 8345 int ret;
8314 bool global_updated = false; 8346 bool global_updated = false;
8315 8347
@@ -8327,11 +8359,11 @@ again:
8327 8359
8328 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { 8360 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
8329 global_updated = true; 8361 global_updated = true;
8330 update_global_block_rsv(root->fs_info); 8362 update_global_block_rsv(fs_info);
8331 goto again; 8363 goto again;
8332 } 8364 }
8333 8365
8334 if (btrfs_test_opt(root->fs_info, ENOSPC_DEBUG)) { 8366 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
8335 static DEFINE_RATELIMIT_STATE(_rs, 8367 static DEFINE_RATELIMIT_STATE(_rs,
8336 DEFAULT_RATELIMIT_INTERVAL * 10, 8368 DEFAULT_RATELIMIT_INTERVAL * 10,
8337 /*DEFAULT_RATELIMIT_BURST*/ 1); 8369 /*DEFAULT_RATELIMIT_BURST*/ 1);
@@ -8375,18 +8407,18 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
8375 struct btrfs_disk_key *key, int level, 8407 struct btrfs_disk_key *key, int level,
8376 u64 hint, u64 empty_size) 8408 u64 hint, u64 empty_size)
8377{ 8409{
8410 struct btrfs_fs_info *fs_info = root->fs_info;
8378 struct btrfs_key ins; 8411 struct btrfs_key ins;
8379 struct btrfs_block_rsv *block_rsv; 8412 struct btrfs_block_rsv *block_rsv;
8380 struct extent_buffer *buf; 8413 struct extent_buffer *buf;
8381 struct btrfs_delayed_extent_op *extent_op; 8414 struct btrfs_delayed_extent_op *extent_op;
8382 u64 flags = 0; 8415 u64 flags = 0;
8383 int ret; 8416 int ret;
8384 u32 blocksize = root->fs_info->nodesize; 8417 u32 blocksize = fs_info->nodesize;
8385 bool skinny_metadata = btrfs_fs_incompat(root->fs_info, 8418 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
8386 SKINNY_METADATA);
8387 8419
8388#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS 8420#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8389 if (btrfs_is_testing(root->fs_info)) { 8421 if (btrfs_is_testing(fs_info)) {
8390 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, 8422 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
8391 level); 8423 level);
8392 if (!IS_ERR(buf)) 8424 if (!IS_ERR(buf))
@@ -8433,7 +8465,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
8433 extent_op->is_data = false; 8465 extent_op->is_data = false;
8434 extent_op->level = level; 8466 extent_op->level = level;
8435 8467
8436 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, 8468 ret = btrfs_add_delayed_tree_ref(fs_info, trans,
8437 ins.objectid, ins.offset, 8469 ins.objectid, ins.offset,
8438 parent, root_objectid, level, 8470 parent, root_objectid, level,
8439 BTRFS_ADD_DELAYED_EXTENT, 8471 BTRFS_ADD_DELAYED_EXTENT,
@@ -8450,7 +8482,7 @@ out_free_buf:
8450out_free_reserved: 8482out_free_reserved:
8451 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0); 8483 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0);
8452out_unuse: 8484out_unuse:
8453 unuse_block_rsv(root->fs_info, block_rsv, blocksize); 8485 unuse_block_rsv(fs_info, block_rsv, blocksize);
8454 return ERR_PTR(ret); 8486 return ERR_PTR(ret);
8455} 8487}
8456 8488
@@ -8476,6 +8508,7 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8476 struct walk_control *wc, 8508 struct walk_control *wc,
8477 struct btrfs_path *path) 8509 struct btrfs_path *path)
8478{ 8510{
8511 struct btrfs_fs_info *fs_info = root->fs_info;
8479 u64 bytenr; 8512 u64 bytenr;
8480 u64 generation; 8513 u64 generation;
8481 u64 refs; 8514 u64 refs;
@@ -8493,7 +8526,7 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
8493 } else { 8526 } else {
8494 wc->reada_count = wc->reada_count * 3 / 2; 8527 wc->reada_count = wc->reada_count * 3 / 2;
8495 wc->reada_count = min_t(int, wc->reada_count, 8528 wc->reada_count = min_t(int, wc->reada_count,
8496 BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)); 8529 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
8497 } 8530 }
8498 8531
8499 eb = path->nodes[wc->level]; 8532 eb = path->nodes[wc->level];
@@ -8644,6 +8677,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8644 struct btrfs_path *path, 8677 struct btrfs_path *path,
8645 struct walk_control *wc, int *lookup_info) 8678 struct walk_control *wc, int *lookup_info)
8646{ 8679{
8680 struct btrfs_fs_info *fs_info = root->fs_info;
8647 u64 bytenr; 8681 u64 bytenr;
8648 u64 generation; 8682 u64 generation;
8649 u64 parent; 8683 u64 parent;
@@ -8669,9 +8703,9 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8669 } 8703 }
8670 8704
8671 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); 8705 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
8672 blocksize = root->fs_info->nodesize; 8706 blocksize = fs_info->nodesize;
8673 8707
8674 next = find_extent_buffer(root->fs_info, bytenr); 8708 next = find_extent_buffer(fs_info, bytenr);
8675 if (!next) { 8709 if (!next) {
8676 next = btrfs_find_create_tree_block(root, bytenr); 8710 next = btrfs_find_create_tree_block(root, bytenr);
8677 if (IS_ERR(next)) 8711 if (IS_ERR(next))
@@ -8691,7 +8725,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
8691 goto out_unlock; 8725 goto out_unlock;
8692 8726
8693 if (unlikely(wc->refs[level - 1] == 0)) { 8727 if (unlikely(wc->refs[level - 1] == 0)) {
8694 btrfs_err(root->fs_info, "Missing references."); 8728 btrfs_err(fs_info, "Missing references.");
8695 ret = -EIO; 8729 ret = -EIO;
8696 goto out_unlock; 8730 goto out_unlock;
8697 } 8731 }
@@ -8781,7 +8815,7 @@ skip:
8781 ret = btrfs_qgroup_trace_subtree(trans, root, next, 8815 ret = btrfs_qgroup_trace_subtree(trans, root, next,
8782 generation, level - 1); 8816 generation, level - 1);
8783 if (ret) { 8817 if (ret) {
8784 btrfs_err_rl(root->fs_info, 8818 btrfs_err_rl(fs_info,
8785 "Error %d accounting shared subtree. Quota is out of sync, rescan required.", 8819 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
8786 ret); 8820 ret);
8787 } 8821 }
@@ -8819,6 +8853,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8819 struct btrfs_path *path, 8853 struct btrfs_path *path,
8820 struct walk_control *wc) 8854 struct walk_control *wc)
8821{ 8855{
8856 struct btrfs_fs_info *fs_info = root->fs_info;
8822 int ret; 8857 int ret;
8823 int level = wc->level; 8858 int level = wc->level;
8824 struct extent_buffer *eb = path->nodes[level]; 8859 struct extent_buffer *eb = path->nodes[level];
@@ -8878,7 +8913,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8878 BUG_ON(ret); /* -ENOMEM */ 8913 BUG_ON(ret); /* -ENOMEM */
8879 ret = btrfs_qgroup_trace_leaf_items(trans, root, eb); 8914 ret = btrfs_qgroup_trace_leaf_items(trans, root, eb);
8880 if (ret) { 8915 if (ret) {
8881 btrfs_err_rl(root->fs_info, 8916 btrfs_err_rl(fs_info,
8882 "error %d accounting leaf items. Quota is out of sync, rescan required.", 8917 "error %d accounting leaf items. Quota is out of sync, rescan required.",
8883 ret); 8918 ret);
8884 } 8919 }
@@ -8890,7 +8925,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
8890 btrfs_set_lock_blocking(eb); 8925 btrfs_set_lock_blocking(eb);
8891 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; 8926 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8892 } 8927 }
8893 clean_tree_block(trans, root->fs_info, eb); 8928 clean_tree_block(trans, fs_info, eb);
8894 } 8929 }
8895 8930
8896 if (eb == root->node) { 8931 if (eb == root->node) {
@@ -9094,7 +9129,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
9094 wc->update_ref = update_ref; 9129 wc->update_ref = update_ref;
9095 wc->keep_locks = 0; 9130 wc->keep_locks = 0;
9096 wc->for_reloc = for_reloc; 9131 wc->for_reloc = for_reloc;
9097 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info); 9132 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
9098 9133
9099 while (1) { 9134 while (1) {
9100 9135
@@ -9219,6 +9254,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9219 struct extent_buffer *node, 9254 struct extent_buffer *node,
9220 struct extent_buffer *parent) 9255 struct extent_buffer *parent)
9221{ 9256{
9257 struct btrfs_fs_info *fs_info = root->fs_info;
9222 struct btrfs_path *path; 9258 struct btrfs_path *path;
9223 struct walk_control *wc; 9259 struct walk_control *wc;
9224 int level; 9260 int level;
@@ -9258,7 +9294,7 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
9258 wc->update_ref = 0; 9294 wc->update_ref = 0;
9259 wc->keep_locks = 1; 9295 wc->keep_locks = 1;
9260 wc->for_reloc = 1; 9296 wc->for_reloc = 1;
9261 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root->fs_info); 9297 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
9262 9298
9263 while (1) { 9299 while (1) {
9264 wret = walk_down_tree(trans, root, path, wc); 9300 wret = walk_down_tree(trans, root, path, wc);
@@ -9377,6 +9413,7 @@ int btrfs_inc_block_group_ro(struct btrfs_root *root,
9377 struct btrfs_block_group_cache *cache) 9413 struct btrfs_block_group_cache *cache)
9378 9414
9379{ 9415{
9416 struct btrfs_fs_info *fs_info = root->fs_info;
9380 struct btrfs_trans_handle *trans; 9417 struct btrfs_trans_handle *trans;
9381 u64 alloc_flags; 9418 u64 alloc_flags;
9382 int ret; 9419 int ret;
@@ -9391,11 +9428,11 @@ again:
9391 * block groups cache has started writing. If it already started, 9428 * block groups cache has started writing. If it already started,
9392 * back off and let this transaction commit 9429 * back off and let this transaction commit
9393 */ 9430 */
9394 mutex_lock(&root->fs_info->ro_block_group_mutex); 9431 mutex_lock(&fs_info->ro_block_group_mutex);
9395 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { 9432 if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) {
9396 u64 transid = trans->transid; 9433 u64 transid = trans->transid;
9397 9434
9398 mutex_unlock(&root->fs_info->ro_block_group_mutex); 9435 mutex_unlock(&fs_info->ro_block_group_mutex);
9399 btrfs_end_transaction(trans, root); 9436 btrfs_end_transaction(trans, root);
9400 9437
9401 ret = btrfs_wait_for_commit(root, transid); 9438 ret = btrfs_wait_for_commit(root, transid);
@@ -9408,7 +9445,7 @@ again:
9408 * if we are changing raid levels, try to allocate a corresponding 9445 * if we are changing raid levels, try to allocate a corresponding
9409 * block group with the new raid level. 9446 * block group with the new raid level.
9410 */ 9447 */
9411 alloc_flags = update_block_group_flags(root->fs_info, cache->flags); 9448 alloc_flags = update_block_group_flags(fs_info, cache->flags);
9412 if (alloc_flags != cache->flags) { 9449 if (alloc_flags != cache->flags) {
9413 ret = do_chunk_alloc(trans, root, alloc_flags, 9450 ret = do_chunk_alloc(trans, root, alloc_flags,
9414 CHUNK_ALLOC_FORCE); 9451 CHUNK_ALLOC_FORCE);
@@ -9434,13 +9471,12 @@ again:
9434 ret = inc_block_group_ro(cache, 0); 9471 ret = inc_block_group_ro(cache, 0);
9435out: 9472out:
9436 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { 9473 if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
9437 alloc_flags = update_block_group_flags(root->fs_info, 9474 alloc_flags = update_block_group_flags(fs_info, cache->flags);
9438 cache->flags); 9475 lock_chunks(fs_info);
9439 lock_chunks(root->fs_info);
9440 check_system_chunk(trans, root, alloc_flags); 9476 check_system_chunk(trans, root, alloc_flags);
9441 unlock_chunks(root->fs_info); 9477 unlock_chunks(fs_info);
9442 } 9478 }
9443 mutex_unlock(&root->fs_info->ro_block_group_mutex); 9479 mutex_unlock(&fs_info->ro_block_group_mutex);
9444 9480
9445 btrfs_end_transaction(trans, root); 9481 btrfs_end_transaction(trans, root);
9446 return ret; 9482 return ret;
@@ -9527,7 +9563,7 @@ int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
9527 struct btrfs_root *root = fs_info->extent_root; 9563 struct btrfs_root *root = fs_info->extent_root;
9528 struct btrfs_block_group_cache *block_group; 9564 struct btrfs_block_group_cache *block_group;
9529 struct btrfs_space_info *space_info; 9565 struct btrfs_space_info *space_info;
9530 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 9566 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
9531 struct btrfs_device *device; 9567 struct btrfs_device *device;
9532 struct btrfs_trans_handle *trans; 9568 struct btrfs_trans_handle *trans;
9533 u64 min_free; 9569 u64 min_free;
@@ -9539,14 +9575,14 @@ int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
9539 int full = 0; 9575 int full = 0;
9540 int ret = 0; 9576 int ret = 0;
9541 9577
9542 debug = btrfs_test_opt(root->fs_info, ENOSPC_DEBUG); 9578 debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG);
9543 9579
9544 block_group = btrfs_lookup_block_group(root->fs_info, bytenr); 9580 block_group = btrfs_lookup_block_group(fs_info, bytenr);
9545 9581
9546 /* odd, couldn't find the block group, leave it alone */ 9582 /* odd, couldn't find the block group, leave it alone */
9547 if (!block_group) { 9583 if (!block_group) {
9548 if (debug) 9584 if (debug)
9549 btrfs_warn(root->fs_info, 9585 btrfs_warn(fs_info,
9550 "can't find block group for bytenr %llu", 9586 "can't find block group for bytenr %llu",
9551 bytenr); 9587 bytenr);
9552 return -1; 9588 return -1;
@@ -9596,7 +9632,7 @@ int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
9596 * 3: raid0 9632 * 3: raid0
9597 * 4: single 9633 * 4: single
9598 */ 9634 */
9599 target = get_restripe_target(root->fs_info, block_group->flags); 9635 target = get_restripe_target(fs_info, block_group->flags);
9600 if (target) { 9636 if (target) {
9601 index = __get_raid_index(extended_to_chunk(target)); 9637 index = __get_raid_index(extended_to_chunk(target));
9602 } else { 9638 } else {
@@ -9606,9 +9642,9 @@ int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
9606 */ 9642 */
9607 if (full) { 9643 if (full) {
9608 if (debug) 9644 if (debug)
9609 btrfs_warn(root->fs_info, 9645 btrfs_warn(fs_info,
9610 "no space to alloc new chunk for block group %llu", 9646 "no space to alloc new chunk for block group %llu",
9611 block_group->key.objectid); 9647 block_group->key.objectid);
9612 goto out; 9648 goto out;
9613 } 9649 }
9614 9650
@@ -9636,7 +9672,7 @@ int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
9636 goto out; 9672 goto out;
9637 } 9673 }
9638 9674
9639 mutex_lock(&root->fs_info->chunk_mutex); 9675 mutex_lock(&fs_info->chunk_mutex);
9640 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { 9676 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
9641 u64 dev_offset; 9677 u64 dev_offset;
9642 9678
@@ -9658,10 +9694,10 @@ int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr)
9658 } 9694 }
9659 } 9695 }
9660 if (debug && ret == -1) 9696 if (debug && ret == -1)
9661 btrfs_warn(root->fs_info, 9697 btrfs_warn(fs_info,
9662 "no space to allocate a new chunk for block group %llu", 9698 "no space to allocate a new chunk for block group %llu",
9663 block_group->key.objectid); 9699 block_group->key.objectid);
9664 mutex_unlock(&root->fs_info->chunk_mutex); 9700 mutex_unlock(&fs_info->chunk_mutex);
9665 btrfs_end_transaction(trans, root); 9701 btrfs_end_transaction(trans, root);
9666out: 9702out:
9667 btrfs_put_block_group(block_group); 9703 btrfs_put_block_group(block_group);
@@ -9706,7 +9742,7 @@ static int find_first_block_group(struct btrfs_fs_info *fs_info,
9706 found_key.offset); 9742 found_key.offset);
9707 read_unlock(&em_tree->lock); 9743 read_unlock(&em_tree->lock);
9708 if (!em) { 9744 if (!em) {
9709 btrfs_err(root->fs_info, 9745 btrfs_err(fs_info,
9710 "logical %llu len %llu found bg but no related chunk", 9746 "logical %llu len %llu found bg but no related chunk",
9711 found_key.objectid, found_key.offset); 9747 found_key.objectid, found_key.offset);
9712 ret = -ENOENT; 9748 ret = -ENOENT;
@@ -9898,6 +9934,7 @@ out_err:
9898static struct btrfs_block_group_cache * 9934static struct btrfs_block_group_cache *
9899btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) 9935btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
9900{ 9936{
9937 struct btrfs_fs_info *fs_info = root->fs_info;
9901 struct btrfs_block_group_cache *cache; 9938 struct btrfs_block_group_cache *cache;
9902 9939
9903 cache = kzalloc(sizeof(*cache), GFP_NOFS); 9940 cache = kzalloc(sizeof(*cache), GFP_NOFS);
@@ -9915,11 +9952,10 @@ btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
9915 cache->key.offset = size; 9952 cache->key.offset = size;
9916 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; 9953 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9917 9954
9918 cache->sectorsize = root->fs_info->sectorsize; 9955 cache->sectorsize = fs_info->sectorsize;
9919 cache->fs_info = root->fs_info; 9956 cache->fs_info = fs_info;
9920 cache->full_stripe_len = btrfs_full_stripe_len(root, 9957 cache->full_stripe_len = btrfs_full_stripe_len(root,
9921 &root->fs_info->mapping_tree, 9958 &fs_info->mapping_tree, start);
9922 start);
9923 set_free_space_tree_thresholds(cache); 9959 set_free_space_tree_thresholds(cache);
9924 9960
9925 atomic_set(&cache->count, 1); 9961 atomic_set(&cache->count, 1);
@@ -9964,11 +10000,11 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
9964 return -ENOMEM; 10000 return -ENOMEM;
9965 path->reada = READA_FORWARD; 10001 path->reada = READA_FORWARD;
9966 10002
9967 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 10003 cache_gen = btrfs_super_cache_generation(info->super_copy);
9968 if (btrfs_test_opt(root->fs_info, SPACE_CACHE) && 10004 if (btrfs_test_opt(info, SPACE_CACHE) &&
9969 btrfs_super_generation(root->fs_info->super_copy) != cache_gen) 10005 btrfs_super_generation(info->super_copy) != cache_gen)
9970 need_clear = 1; 10006 need_clear = 1;
9971 if (btrfs_test_opt(root->fs_info, CLEAR_CACHE)) 10007 if (btrfs_test_opt(info, CLEAR_CACHE))
9972 need_clear = 1; 10008 need_clear = 1;
9973 10009
9974 while (1) { 10010 while (1) {
@@ -9999,7 +10035,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
9999 * b) Setting 'dirty flag' makes sure that we flush 10035 * b) Setting 'dirty flag' makes sure that we flush
10000 * the new space cache info onto disk. 10036 * the new space cache info onto disk.
10001 */ 10037 */
10002 if (btrfs_test_opt(root->fs_info, SPACE_CACHE)) 10038 if (btrfs_test_opt(info, SPACE_CACHE))
10003 cache->disk_cache_state = BTRFS_DC_CLEAR; 10039 cache->disk_cache_state = BTRFS_DC_CLEAR;
10004 } 10040 }
10005 10041
@@ -10050,21 +10086,21 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10050 } else if (btrfs_block_group_used(&cache->item) == 0) { 10086 } else if (btrfs_block_group_used(&cache->item) == 0) {
10051 cache->last_byte_to_unpin = (u64)-1; 10087 cache->last_byte_to_unpin = (u64)-1;
10052 cache->cached = BTRFS_CACHE_FINISHED; 10088 cache->cached = BTRFS_CACHE_FINISHED;
10053 add_new_free_space(cache, root->fs_info, 10089 add_new_free_space(cache, info,
10054 found_key.objectid, 10090 found_key.objectid,
10055 found_key.objectid + 10091 found_key.objectid +
10056 found_key.offset); 10092 found_key.offset);
10057 free_excluded_extents(root, cache); 10093 free_excluded_extents(root, cache);
10058 } 10094 }
10059 10095
10060 ret = btrfs_add_block_group_cache(root->fs_info, cache); 10096 ret = btrfs_add_block_group_cache(info, cache);
10061 if (ret) { 10097 if (ret) {
10062 btrfs_remove_free_space_cache(cache); 10098 btrfs_remove_free_space_cache(cache);
10063 btrfs_put_block_group(cache); 10099 btrfs_put_block_group(cache);
10064 goto error; 10100 goto error;
10065 } 10101 }
10066 10102
10067 trace_btrfs_add_block_group(root->fs_info, cache, 0); 10103 trace_btrfs_add_block_group(info, cache, 0);
10068 ret = update_space_info(info, cache->flags, found_key.offset, 10104 ret = update_space_info(info, cache->flags, found_key.offset,
10069 btrfs_block_group_used(&cache->item), 10105 btrfs_block_group_used(&cache->item),
10070 cache->bytes_super, &space_info); 10106 cache->bytes_super, &space_info);
@@ -10083,7 +10119,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10083 10119
10084 __link_block_group(space_info, cache); 10120 __link_block_group(space_info, cache);
10085 10121
10086 set_avail_alloc_bits(root->fs_info, cache->flags); 10122 set_avail_alloc_bits(info, cache->flags);
10087 if (btrfs_chunk_readonly(root, cache->key.objectid)) { 10123 if (btrfs_chunk_readonly(root, cache->key.objectid)) {
10088 inc_block_group_ro(cache, 1); 10124 inc_block_group_ro(cache, 1);
10089 } else if (btrfs_block_group_used(&cache->item) == 0) { 10125 } else if (btrfs_block_group_used(&cache->item) == 0) {
@@ -10098,7 +10134,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
10098 } 10134 }
10099 } 10135 }
10100 10136
10101 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { 10137 list_for_each_entry_rcu(space_info, &info->space_info, list) {
10102 if (!(get_alloc_profile(root, space_info->flags) & 10138 if (!(get_alloc_profile(root, space_info->flags) &
10103 (BTRFS_BLOCK_GROUP_RAID10 | 10139 (BTRFS_BLOCK_GROUP_RAID10 |
10104 BTRFS_BLOCK_GROUP_RAID1 | 10140 BTRFS_BLOCK_GROUP_RAID1 |
@@ -10130,8 +10166,9 @@ error:
10130void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, 10166void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
10131 struct btrfs_root *root) 10167 struct btrfs_root *root)
10132{ 10168{
10169 struct btrfs_fs_info *fs_info = root->fs_info;
10133 struct btrfs_block_group_cache *block_group, *tmp; 10170 struct btrfs_block_group_cache *block_group, *tmp;
10134 struct btrfs_root *extent_root = root->fs_info->extent_root; 10171 struct btrfs_root *extent_root = fs_info->extent_root;
10135 struct btrfs_block_group_item item; 10172 struct btrfs_block_group_item item;
10136 struct btrfs_key key; 10173 struct btrfs_key key;
10137 int ret = 0; 10174 int ret = 0;
@@ -10151,11 +10188,11 @@ void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
10151 sizeof(item)); 10188 sizeof(item));
10152 if (ret) 10189 if (ret)
10153 btrfs_abort_transaction(trans, ret); 10190 btrfs_abort_transaction(trans, ret);
10154 ret = btrfs_finish_chunk_alloc(trans, extent_root->fs_info, 10191 ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid,
10155 key.objectid, key.offset); 10192 key.offset);
10156 if (ret) 10193 if (ret)
10157 btrfs_abort_transaction(trans, ret); 10194 btrfs_abort_transaction(trans, ret);
10158 add_block_group_free_space(trans, root->fs_info, block_group); 10195 add_block_group_free_space(trans, fs_info, block_group);
10159 /* already aborted the transaction if it failed. */ 10196 /* already aborted the transaction if it failed. */
10160next: 10197next:
10161 list_del_init(&block_group->bg_list); 10198 list_del_init(&block_group->bg_list);
@@ -10168,12 +10205,11 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10168 u64 type, u64 chunk_objectid, u64 chunk_offset, 10205 u64 type, u64 chunk_objectid, u64 chunk_offset,
10169 u64 size) 10206 u64 size)
10170{ 10207{
10171 int ret; 10208 struct btrfs_fs_info *fs_info = root->fs_info;
10172 struct btrfs_root *extent_root;
10173 struct btrfs_block_group_cache *cache; 10209 struct btrfs_block_group_cache *cache;
10174 extent_root = root->fs_info->extent_root; 10210 int ret;
10175 10211
10176 btrfs_set_log_full_commit(root->fs_info, trans); 10212 btrfs_set_log_full_commit(fs_info, trans);
10177 10213
10178 cache = btrfs_create_block_group_cache(root, chunk_offset, size); 10214 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
10179 if (!cache) 10215 if (!cache)
@@ -10198,8 +10234,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10198 return ret; 10234 return ret;
10199 } 10235 }
10200 10236
10201 add_new_free_space(cache, root->fs_info, chunk_offset, 10237 add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size);
10202 chunk_offset + size);
10203 10238
10204 free_excluded_extents(root, cache); 10239 free_excluded_extents(root, cache);
10205 10240
@@ -10216,7 +10251,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10216 * assigned to our block group, but don't update its counters just yet. 10251 * assigned to our block group, but don't update its counters just yet.
10217 * We want our bg to be added to the rbtree with its ->space_info set. 10252 * We want our bg to be added to the rbtree with its ->space_info set.
10218 */ 10253 */
10219 ret = update_space_info(root->fs_info, cache->flags, 0, 0, 0, 10254 ret = update_space_info(fs_info, cache->flags, 0, 0, 0,
10220 &cache->space_info); 10255 &cache->space_info);
10221 if (ret) { 10256 if (ret) {
10222 btrfs_remove_free_space_cache(cache); 10257 btrfs_remove_free_space_cache(cache);
@@ -10224,7 +10259,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10224 return ret; 10259 return ret;
10225 } 10260 }
10226 10261
10227 ret = btrfs_add_block_group_cache(root->fs_info, cache); 10262 ret = btrfs_add_block_group_cache(fs_info, cache);
10228 if (ret) { 10263 if (ret) {
10229 btrfs_remove_free_space_cache(cache); 10264 btrfs_remove_free_space_cache(cache);
10230 btrfs_put_block_group(cache); 10265 btrfs_put_block_group(cache);
@@ -10235,26 +10270,26 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
10235 * Now that our block group has its ->space_info set and is inserted in 10270 * Now that our block group has its ->space_info set and is inserted in
10236 * the rbtree, update the space info's counters. 10271 * the rbtree, update the space info's counters.
10237 */ 10272 */
10238 trace_btrfs_add_block_group(root->fs_info, cache, 1); 10273 trace_btrfs_add_block_group(fs_info, cache, 1);
10239 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, 10274 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
10240 cache->bytes_super, &cache->space_info); 10275 cache->bytes_super, &cache->space_info);
10241 if (ret) { 10276 if (ret) {
10242 btrfs_remove_free_space_cache(cache); 10277 btrfs_remove_free_space_cache(cache);
10243 spin_lock(&root->fs_info->block_group_cache_lock); 10278 spin_lock(&fs_info->block_group_cache_lock);
10244 rb_erase(&cache->cache_node, 10279 rb_erase(&cache->cache_node,
10245 &root->fs_info->block_group_cache_tree); 10280 &fs_info->block_group_cache_tree);
10246 RB_CLEAR_NODE(&cache->cache_node); 10281 RB_CLEAR_NODE(&cache->cache_node);
10247 spin_unlock(&root->fs_info->block_group_cache_lock); 10282 spin_unlock(&fs_info->block_group_cache_lock);
10248 btrfs_put_block_group(cache); 10283 btrfs_put_block_group(cache);
10249 return ret; 10284 return ret;
10250 } 10285 }
10251 update_global_block_rsv(root->fs_info); 10286 update_global_block_rsv(fs_info);
10252 10287
10253 __link_block_group(cache->space_info, cache); 10288 __link_block_group(cache->space_info, cache);
10254 10289
10255 list_add_tail(&cache->bg_list, &trans->new_bgs); 10290 list_add_tail(&cache->bg_list, &trans->new_bgs);
10256 10291
10257 set_avail_alloc_bits(extent_root->fs_info, type); 10292 set_avail_alloc_bits(fs_info, type);
10258 return 0; 10293 return 0;
10259} 10294}
10260 10295
@@ -10281,7 +10316,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10281 struct btrfs_path *path; 10316 struct btrfs_path *path;
10282 struct btrfs_block_group_cache *block_group; 10317 struct btrfs_block_group_cache *block_group;
10283 struct btrfs_free_cluster *cluster; 10318 struct btrfs_free_cluster *cluster;
10284 struct btrfs_root *tree_root = root->fs_info->tree_root; 10319 struct btrfs_root *tree_root = fs_info->tree_root;
10285 struct btrfs_key key; 10320 struct btrfs_key key;
10286 struct inode *inode; 10321 struct inode *inode;
10287 struct kobject *kobj = NULL; 10322 struct kobject *kobj = NULL;
@@ -10311,7 +10346,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10311 factor = 1; 10346 factor = 1;
10312 10347
10313 /* make sure this block group isn't part of an allocation cluster */ 10348 /* make sure this block group isn't part of an allocation cluster */
10314 cluster = &root->fs_info->data_alloc_cluster; 10349 cluster = &fs_info->data_alloc_cluster;
10315 spin_lock(&cluster->refill_lock); 10350 spin_lock(&cluster->refill_lock);
10316 btrfs_return_cluster_to_free_space(block_group, cluster); 10351 btrfs_return_cluster_to_free_space(block_group, cluster);
10317 spin_unlock(&cluster->refill_lock); 10352 spin_unlock(&cluster->refill_lock);
@@ -10320,7 +10355,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10320 * make sure this block group isn't part of a metadata 10355 * make sure this block group isn't part of a metadata
10321 * allocation cluster 10356 * allocation cluster
10322 */ 10357 */
10323 cluster = &root->fs_info->meta_alloc_cluster; 10358 cluster = &fs_info->meta_alloc_cluster;
10324 spin_lock(&cluster->refill_lock); 10359 spin_lock(&cluster->refill_lock);
10325 btrfs_return_cluster_to_free_space(block_group, cluster); 10360 btrfs_return_cluster_to_free_space(block_group, cluster);
10326 spin_unlock(&cluster->refill_lock); 10361 spin_unlock(&cluster->refill_lock);
@@ -10400,14 +10435,14 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10400 btrfs_release_path(path); 10435 btrfs_release_path(path);
10401 } 10436 }
10402 10437
10403 spin_lock(&root->fs_info->block_group_cache_lock); 10438 spin_lock(&fs_info->block_group_cache_lock);
10404 rb_erase(&block_group->cache_node, 10439 rb_erase(&block_group->cache_node,
10405 &root->fs_info->block_group_cache_tree); 10440 &fs_info->block_group_cache_tree);
10406 RB_CLEAR_NODE(&block_group->cache_node); 10441 RB_CLEAR_NODE(&block_group->cache_node);
10407 10442
10408 if (root->fs_info->first_logical_byte == block_group->key.objectid) 10443 if (fs_info->first_logical_byte == block_group->key.objectid)
10409 root->fs_info->first_logical_byte = (u64)-1; 10444 fs_info->first_logical_byte = (u64)-1;
10410 spin_unlock(&root->fs_info->block_group_cache_lock); 10445 spin_unlock(&fs_info->block_group_cache_lock);
10411 10446
10412 down_write(&block_group->space_info->groups_sem); 10447 down_write(&block_group->space_info->groups_sem);
10413 /* 10448 /*
@@ -10418,7 +10453,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10418 if (list_empty(&block_group->space_info->block_groups[index])) { 10453 if (list_empty(&block_group->space_info->block_groups[index])) {
10419 kobj = block_group->space_info->block_group_kobjs[index]; 10454 kobj = block_group->space_info->block_group_kobjs[index];
10420 block_group->space_info->block_group_kobjs[index] = NULL; 10455 block_group->space_info->block_group_kobjs[index] = NULL;
10421 clear_avail_alloc_bits(root->fs_info, block_group->flags); 10456 clear_avail_alloc_bits(fs_info, block_group->flags);
10422 } 10457 }
10423 up_write(&block_group->space_info->groups_sem); 10458 up_write(&block_group->space_info->groups_sem);
10424 if (kobj) { 10459 if (kobj) {
@@ -10431,12 +10466,12 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10431 if (block_group->cached == BTRFS_CACHE_STARTED) 10466 if (block_group->cached == BTRFS_CACHE_STARTED)
10432 wait_block_group_cache_done(block_group); 10467 wait_block_group_cache_done(block_group);
10433 if (block_group->has_caching_ctl) { 10468 if (block_group->has_caching_ctl) {
10434 down_write(&root->fs_info->commit_root_sem); 10469 down_write(&fs_info->commit_root_sem);
10435 if (!caching_ctl) { 10470 if (!caching_ctl) {
10436 struct btrfs_caching_control *ctl; 10471 struct btrfs_caching_control *ctl;
10437 10472
10438 list_for_each_entry(ctl, 10473 list_for_each_entry(ctl,
10439 &root->fs_info->caching_block_groups, list) 10474 &fs_info->caching_block_groups, list)
10440 if (ctl->block_group == block_group) { 10475 if (ctl->block_group == block_group) {
10441 caching_ctl = ctl; 10476 caching_ctl = ctl;
10442 atomic_inc(&caching_ctl->count); 10477 atomic_inc(&caching_ctl->count);
@@ -10445,7 +10480,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10445 } 10480 }
10446 if (caching_ctl) 10481 if (caching_ctl)
10447 list_del_init(&caching_ctl->list); 10482 list_del_init(&caching_ctl->list);
10448 up_write(&root->fs_info->commit_root_sem); 10483 up_write(&fs_info->commit_root_sem);
10449 if (caching_ctl) { 10484 if (caching_ctl) {
10450 /* Once for the caching bgs list and once for us. */ 10485 /* Once for the caching bgs list and once for us. */
10451 put_caching_control(caching_ctl); 10486 put_caching_control(caching_ctl);
@@ -10466,7 +10501,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10466 spin_lock(&block_group->space_info->lock); 10501 spin_lock(&block_group->space_info->lock);
10467 list_del_init(&block_group->ro_list); 10502 list_del_init(&block_group->ro_list);
10468 10503
10469 if (btrfs_test_opt(root->fs_info, ENOSPC_DEBUG)) { 10504 if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
10470 WARN_ON(block_group->space_info->total_bytes 10505 WARN_ON(block_group->space_info->total_bytes
10471 < block_group->key.offset); 10506 < block_group->key.offset);
10472 WARN_ON(block_group->space_info->bytes_readonly 10507 WARN_ON(block_group->space_info->bytes_readonly
@@ -10482,7 +10517,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10482 10517
10483 memcpy(&key, &block_group->key, sizeof(key)); 10518 memcpy(&key, &block_group->key, sizeof(key));
10484 10519
10485 lock_chunks(root->fs_info); 10520 lock_chunks(fs_info);
10486 if (!list_empty(&em->list)) { 10521 if (!list_empty(&em->list)) {
10487 /* We're in the transaction->pending_chunks list. */ 10522 /* We're in the transaction->pending_chunks list. */
10488 free_extent_map(em); 10523 free_extent_map(em);
@@ -10530,14 +10565,14 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10530 * sees the em, either in the pending_chunks list or in the 10565 * sees the em, either in the pending_chunks list or in the
10531 * pinned_chunks list. 10566 * pinned_chunks list.
10532 */ 10567 */
10533 list_move_tail(&em->list, &root->fs_info->pinned_chunks); 10568 list_move_tail(&em->list, &fs_info->pinned_chunks);
10534 } 10569 }
10535 spin_unlock(&block_group->lock); 10570 spin_unlock(&block_group->lock);
10536 10571
10537 if (remove_em) { 10572 if (remove_em) {
10538 struct extent_map_tree *em_tree; 10573 struct extent_map_tree *em_tree;
10539 10574
10540 em_tree = &root->fs_info->mapping_tree.map_tree; 10575 em_tree = &fs_info->mapping_tree.map_tree;
10541 write_lock(&em_tree->lock); 10576 write_lock(&em_tree->lock);
10542 /* 10577 /*
10543 * The em might be in the pending_chunks list, so make sure the 10578 * The em might be in the pending_chunks list, so make sure the
@@ -10550,9 +10585,9 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
10550 free_extent_map(em); 10585 free_extent_map(em);
10551 } 10586 }
10552 10587
10553 unlock_chunks(root->fs_info); 10588 unlock_chunks(fs_info);
10554 10589
10555 ret = remove_block_group_free_space(trans, root->fs_info, block_group); 10590 ret = remove_block_group_free_space(trans, fs_info, block_group);
10556 if (ret) 10591 if (ret)
10557 goto out; 10592 goto out;
10558 10593
@@ -10734,7 +10769,7 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
10734 spin_unlock(&space_info->lock); 10769 spin_unlock(&space_info->lock);
10735 10770
10736 /* DISCARD can flip during remount */ 10771 /* DISCARD can flip during remount */
10737 trimming = btrfs_test_opt(root->fs_info, DISCARD); 10772 trimming = btrfs_test_opt(fs_info, DISCARD);
10738 10773
10739 /* Implicit trim during transaction commit. */ 10774 /* Implicit trim during transaction commit. */
10740 if (trimming) 10775 if (trimming)
@@ -10970,8 +11005,8 @@ int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
10970 cache = next_block_group(fs_info->tree_root, cache); 11005 cache = next_block_group(fs_info->tree_root, cache);
10971 } 11006 }
10972 11007
10973 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 11008 mutex_lock(&fs_info->fs_devices->device_list_mutex);
10974 devices = &root->fs_info->fs_devices->alloc_list; 11009 devices = &fs_info->fs_devices->alloc_list;
10975 list_for_each_entry(device, devices, dev_alloc_list) { 11010 list_for_each_entry(device, devices, dev_alloc_list) {
10976 ret = btrfs_trim_free_extents(device, range->minlen, 11011 ret = btrfs_trim_free_extents(device, range->minlen,
10977 &group_trimmed); 11012 &group_trimmed);
@@ -10980,7 +11015,7 @@ int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
10980 11015
10981 trimmed += group_trimmed; 11016 trimmed += group_trimmed;
10982 } 11017 }
10983 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 11018 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
10984 11019
10985 range->len = trimmed; 11020 range->len = trimmed;
10986 return ret; 11021 return ret;
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 3747af693e78..8df72ded9018 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -2070,17 +2070,18 @@ int repair_io_failure(struct inode *inode, u64 start, u64 length, u64 logical,
2070int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb, 2070int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2071 int mirror_num) 2071 int mirror_num)
2072{ 2072{
2073 struct btrfs_fs_info *fs_info = root->fs_info;
2073 u64 start = eb->start; 2074 u64 start = eb->start;
2074 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len); 2075 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
2075 int ret = 0; 2076 int ret = 0;
2076 2077
2077 if (root->fs_info->sb->s_flags & MS_RDONLY) 2078 if (fs_info->sb->s_flags & MS_RDONLY)
2078 return -EROFS; 2079 return -EROFS;
2079 2080
2080 for (i = 0; i < num_pages; i++) { 2081 for (i = 0; i < num_pages; i++) {
2081 struct page *p = eb->pages[i]; 2082 struct page *p = eb->pages[i];
2082 2083
2083 ret = repair_io_failure(root->fs_info->btree_inode, start, 2084 ret = repair_io_failure(fs_info->btree_inode, start,
2084 PAGE_SIZE, start, p, 2085 PAGE_SIZE, start, p,
2085 start - page_offset(p), mirror_num); 2086 start - page_offset(p), mirror_num);
2086 if (ret) 2087 if (ret)
@@ -2341,6 +2342,7 @@ struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2341 struct page *page, int pg_offset, int icsum, 2342 struct page *page, int pg_offset, int icsum,
2342 bio_end_io_t *endio_func, void *data) 2343 bio_end_io_t *endio_func, void *data)
2343{ 2344{
2345 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2344 struct bio *bio; 2346 struct bio *bio;
2345 struct btrfs_io_bio *btrfs_failed_bio; 2347 struct btrfs_io_bio *btrfs_failed_bio;
2346 struct btrfs_io_bio *btrfs_bio; 2348 struct btrfs_io_bio *btrfs_bio;
@@ -2351,13 +2353,12 @@ struct bio *btrfs_create_repair_bio(struct inode *inode, struct bio *failed_bio,
2351 2353
2352 bio->bi_end_io = endio_func; 2354 bio->bi_end_io = endio_func;
2353 bio->bi_iter.bi_sector = failrec->logical >> 9; 2355 bio->bi_iter.bi_sector = failrec->logical >> 9;
2354 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev; 2356 bio->bi_bdev = fs_info->fs_devices->latest_bdev;
2355 bio->bi_iter.bi_size = 0; 2357 bio->bi_iter.bi_size = 0;
2356 bio->bi_private = data; 2358 bio->bi_private = data;
2357 2359
2358 btrfs_failed_bio = btrfs_io_bio(failed_bio); 2360 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2359 if (btrfs_failed_bio->csum) { 2361 if (btrfs_failed_bio->csum) {
2360 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2361 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy); 2362 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2362 2363
2363 btrfs_bio = btrfs_io_bio(bio); 2364 btrfs_bio = btrfs_io_bio(bio);
@@ -2476,6 +2477,8 @@ static void end_bio_extent_writepage(struct bio *bio)
2476 2477
2477 bio_for_each_segment_all(bvec, bio, i) { 2478 bio_for_each_segment_all(bvec, bio, i) {
2478 struct page *page = bvec->bv_page; 2479 struct page *page = bvec->bv_page;
2480 struct inode *inode = page->mapping->host;
2481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2479 2482
2480 /* We always issue full-page reads, but if some block 2483 /* We always issue full-page reads, but if some block
2481 * in a page fails to read, blk_update_request() will 2484 * in a page fails to read, blk_update_request() will
@@ -2484,11 +2487,11 @@ static void end_bio_extent_writepage(struct bio *bio)
2484 * if they don't add up to a full page. */ 2487 * if they don't add up to a full page. */
2485 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) { 2488 if (bvec->bv_offset || bvec->bv_len != PAGE_SIZE) {
2486 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE) 2489 if (bvec->bv_offset + bvec->bv_len != PAGE_SIZE)
2487 btrfs_err(BTRFS_I(page->mapping->host)->root->fs_info, 2490 btrfs_err(fs_info,
2488 "partial page write in btrfs with offset %u and length %u", 2491 "partial page write in btrfs with offset %u and length %u",
2489 bvec->bv_offset, bvec->bv_len); 2492 bvec->bv_offset, bvec->bv_len);
2490 else 2493 else
2491 btrfs_info(BTRFS_I(page->mapping->host)->root->fs_info, 2494 btrfs_info(fs_info,
2492 "incomplete page write in btrfs with offset %u and length %u", 2495 "incomplete page write in btrfs with offset %u and length %u",
2493 bvec->bv_offset, bvec->bv_len); 2496 bvec->bv_offset, bvec->bv_len);
2494 } 2497 }
@@ -5789,6 +5792,7 @@ static void copy_pages(struct page *dst_page, struct page *src_page,
5789void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, 5792void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5790 unsigned long src_offset, unsigned long len) 5793 unsigned long src_offset, unsigned long len)
5791{ 5794{
5795 struct btrfs_fs_info *fs_info = dst->fs_info;
5792 size_t cur; 5796 size_t cur;
5793 size_t dst_off_in_page; 5797 size_t dst_off_in_page;
5794 size_t src_off_in_page; 5798 size_t src_off_in_page;
@@ -5797,13 +5801,13 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5797 unsigned long src_i; 5801 unsigned long src_i;
5798 5802
5799 if (src_offset + len > dst->len) { 5803 if (src_offset + len > dst->len) {
5800 btrfs_err(dst->fs_info, 5804 btrfs_err(fs_info,
5801 "memmove bogus src_offset %lu move len %lu dst len %lu", 5805 "memmove bogus src_offset %lu move len %lu dst len %lu",
5802 src_offset, len, dst->len); 5806 src_offset, len, dst->len);
5803 BUG_ON(1); 5807 BUG_ON(1);
5804 } 5808 }
5805 if (dst_offset + len > dst->len) { 5809 if (dst_offset + len > dst->len) {
5806 btrfs_err(dst->fs_info, 5810 btrfs_err(fs_info,
5807 "memmove bogus dst_offset %lu move len %lu dst len %lu", 5811 "memmove bogus dst_offset %lu move len %lu dst len %lu",
5808 dst_offset, len, dst->len); 5812 dst_offset, len, dst->len);
5809 BUG_ON(1); 5813 BUG_ON(1);
@@ -5835,6 +5839,7 @@ void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5835void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset, 5839void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5836 unsigned long src_offset, unsigned long len) 5840 unsigned long src_offset, unsigned long len)
5837{ 5841{
5842 struct btrfs_fs_info *fs_info = dst->fs_info;
5838 size_t cur; 5843 size_t cur;
5839 size_t dst_off_in_page; 5844 size_t dst_off_in_page;
5840 size_t src_off_in_page; 5845 size_t src_off_in_page;
@@ -5845,13 +5850,13 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5845 unsigned long src_i; 5850 unsigned long src_i;
5846 5851
5847 if (src_offset + len > dst->len) { 5852 if (src_offset + len > dst->len) {
5848 btrfs_err(dst->fs_info, 5853 btrfs_err(fs_info,
5849 "memmove bogus src_offset %lu move len %lu len %lu", 5854 "memmove bogus src_offset %lu move len %lu len %lu",
5850 src_offset, len, dst->len); 5855 src_offset, len, dst->len);
5851 BUG_ON(1); 5856 BUG_ON(1);
5852 } 5857 }
5853 if (dst_offset + len > dst->len) { 5858 if (dst_offset + len > dst->len) {
5854 btrfs_err(dst->fs_info, 5859 btrfs_err(fs_info,
5855 "memmove bogus dst_offset %lu move len %lu len %lu", 5860 "memmove bogus dst_offset %lu move len %lu len %lu",
5856 dst_offset, len, dst->len); 5861 dst_offset, len, dst->len);
5857 BUG_ON(1); 5862 BUG_ON(1);
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 5349e8e9bbaa..672a36554363 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -90,13 +90,14 @@ btrfs_lookup_csum(struct btrfs_trans_handle *trans,
90 struct btrfs_path *path, 90 struct btrfs_path *path,
91 u64 bytenr, int cow) 91 u64 bytenr, int cow)
92{ 92{
93 struct btrfs_fs_info *fs_info = root->fs_info;
93 int ret; 94 int ret;
94 struct btrfs_key file_key; 95 struct btrfs_key file_key;
95 struct btrfs_key found_key; 96 struct btrfs_key found_key;
96 struct btrfs_csum_item *item; 97 struct btrfs_csum_item *item;
97 struct extent_buffer *leaf; 98 struct extent_buffer *leaf;
98 u64 csum_offset = 0; 99 u64 csum_offset = 0;
99 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 100 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
100 int csums_in_item; 101 int csums_in_item;
101 102
102 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; 103 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
@@ -116,7 +117,7 @@ btrfs_lookup_csum(struct btrfs_trans_handle *trans,
116 goto fail; 117 goto fail;
117 118
118 csum_offset = (bytenr - found_key.offset) >> 119 csum_offset = (bytenr - found_key.offset) >>
119 root->fs_info->sb->s_blocksize_bits; 120 fs_info->sb->s_blocksize_bits;
120 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); 121 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
121 csums_in_item /= csum_size; 122 csums_in_item /= csum_size;
122 123
@@ -163,6 +164,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
163 struct inode *inode, struct bio *bio, 164 struct inode *inode, struct bio *bio,
164 u64 logical_offset, u32 *dst, int dio) 165 u64 logical_offset, u32 *dst, int dio)
165{ 166{
167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
166 struct bio_vec *bvec; 168 struct bio_vec *bvec;
167 struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio); 169 struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
168 struct btrfs_csum_item *item = NULL; 170 struct btrfs_csum_item *item = NULL;
@@ -177,7 +179,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
177 u32 diff; 179 u32 diff;
178 int nblocks; 180 int nblocks;
179 int count = 0, i; 181 int count = 0, i;
180 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 182 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
181 183
182 path = btrfs_alloc_path(); 184 path = btrfs_alloc_path();
183 if (!path) 185 if (!path)
@@ -241,7 +243,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
241 243
242 if (item) 244 if (item)
243 btrfs_release_path(path); 245 btrfs_release_path(path);
244 item = btrfs_lookup_csum(NULL, root->fs_info->csum_root, 246 item = btrfs_lookup_csum(NULL, fs_info->csum_root,
245 path, disk_bytenr, 0); 247 path, disk_bytenr, 0);
246 if (IS_ERR(item)) { 248 if (IS_ERR(item)) {
247 count = 1; 249 count = 1;
@@ -249,10 +251,10 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
249 if (BTRFS_I(inode)->root->root_key.objectid == 251 if (BTRFS_I(inode)->root->root_key.objectid ==
250 BTRFS_DATA_RELOC_TREE_OBJECTID) { 252 BTRFS_DATA_RELOC_TREE_OBJECTID) {
251 set_extent_bits(io_tree, offset, 253 set_extent_bits(io_tree, offset,
252 offset + root->fs_info->sectorsize - 1, 254 offset + fs_info->sectorsize - 1,
253 EXTENT_NODATASUM); 255 EXTENT_NODATASUM);
254 } else { 256 } else {
255 btrfs_info_rl(BTRFS_I(inode)->root->fs_info, 257 btrfs_info_rl(fs_info,
256 "no csum found for inode %llu start %llu", 258 "no csum found for inode %llu start %llu",
257 btrfs_ino(inode), offset); 259 btrfs_ino(inode), offset);
258 } 260 }
@@ -268,7 +270,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
268 path->slots[0]); 270 path->slots[0]);
269 item_last_offset = item_start_offset + 271 item_last_offset = item_start_offset +
270 (item_size / csum_size) * 272 (item_size / csum_size) *
271 root->fs_info->sectorsize; 273 fs_info->sectorsize;
272 item = btrfs_item_ptr(path->nodes[0], path->slots[0], 274 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
273 struct btrfs_csum_item); 275 struct btrfs_csum_item);
274 } 276 }
@@ -277,7 +279,7 @@ static int __btrfs_lookup_bio_sums(struct btrfs_root *root,
277 * a single leaf so it will also fit inside a u32 279 * a single leaf so it will also fit inside a u32
278 */ 280 */
279 diff = disk_bytenr - item_start_offset; 281 diff = disk_bytenr - item_start_offset;
280 diff = diff / root->fs_info->sectorsize; 282 diff = diff / fs_info->sectorsize;
281 diff = diff * csum_size; 283 diff = diff * csum_size;
282 count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >> 284 count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >>
283 inode->i_sb->s_blocksize_bits); 285 inode->i_sb->s_blocksize_bits);
@@ -289,9 +291,9 @@ found:
289 nblocks -= count; 291 nblocks -= count;
290next: 292next:
291 while (count--) { 293 while (count--) {
292 disk_bytenr += root->fs_info->sectorsize; 294 disk_bytenr += fs_info->sectorsize;
293 offset += root->fs_info->sectorsize; 295 offset += fs_info->sectorsize;
294 page_bytes_left -= root->fs_info->sectorsize; 296 page_bytes_left -= fs_info->sectorsize;
295 if (!page_bytes_left) 297 if (!page_bytes_left)
296 break; /* move to next bio */ 298 break; /* move to next bio */
297 } 299 }
@@ -317,6 +319,7 @@ int btrfs_lookup_bio_sums_dio(struct btrfs_root *root, struct inode *inode,
317int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, 319int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
318 struct list_head *list, int search_commit) 320 struct list_head *list, int search_commit)
319{ 321{
322 struct btrfs_fs_info *fs_info = root->fs_info;
320 struct btrfs_key key; 323 struct btrfs_key key;
321 struct btrfs_path *path; 324 struct btrfs_path *path;
322 struct extent_buffer *leaf; 325 struct extent_buffer *leaf;
@@ -327,10 +330,10 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
327 int ret; 330 int ret;
328 size_t size; 331 size_t size;
329 u64 csum_end; 332 u64 csum_end;
330 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 333 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
331 334
332 ASSERT(IS_ALIGNED(start, root->fs_info->sectorsize) && 335 ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
333 IS_ALIGNED(end + 1, root->fs_info->sectorsize)); 336 IS_ALIGNED(end + 1, fs_info->sectorsize));
334 337
335 path = btrfs_alloc_path(); 338 path = btrfs_alloc_path();
336 if (!path) 339 if (!path)
@@ -355,7 +358,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
355 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && 358 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
356 key.type == BTRFS_EXTENT_CSUM_KEY) { 359 key.type == BTRFS_EXTENT_CSUM_KEY) {
357 offset = (start - key.offset) >> 360 offset = (start - key.offset) >>
358 root->fs_info->sb->s_blocksize_bits; 361 fs_info->sb->s_blocksize_bits;
359 if (offset * csum_size < 362 if (offset * csum_size <
360 btrfs_item_size_nr(leaf, path->slots[0] - 1)) 363 btrfs_item_size_nr(leaf, path->slots[0] - 1))
361 path->slots[0]--; 364 path->slots[0]--;
@@ -383,7 +386,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
383 start = key.offset; 386 start = key.offset;
384 387
385 size = btrfs_item_size_nr(leaf, path->slots[0]); 388 size = btrfs_item_size_nr(leaf, path->slots[0]);
386 csum_end = key.offset + (size / csum_size) * root->fs_info->sectorsize; 389 csum_end = key.offset + (size / csum_size) * fs_info->sectorsize;
387 if (csum_end <= start) { 390 if (csum_end <= start) {
388 path->slots[0]++; 391 path->slots[0]++;
389 continue; 392 continue;
@@ -394,9 +397,8 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
394 struct btrfs_csum_item); 397 struct btrfs_csum_item);
395 while (start < csum_end) { 398 while (start < csum_end) {
396 size = min_t(size_t, csum_end - start, 399 size = min_t(size_t, csum_end - start,
397 MAX_ORDERED_SUM_BYTES(root->fs_info)); 400 MAX_ORDERED_SUM_BYTES(fs_info));
398 sums = kzalloc(btrfs_ordered_sum_size(root->fs_info, 401 sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
399 size),
400 GFP_NOFS); 402 GFP_NOFS);
401 if (!sums) { 403 if (!sums) {
402 ret = -ENOMEM; 404 ret = -ENOMEM;
@@ -407,16 +409,16 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
407 sums->len = (int)size; 409 sums->len = (int)size;
408 410
409 offset = (start - key.offset) >> 411 offset = (start - key.offset) >>
410 root->fs_info->sb->s_blocksize_bits; 412 fs_info->sb->s_blocksize_bits;
411 offset *= csum_size; 413 offset *= csum_size;
412 size >>= root->fs_info->sb->s_blocksize_bits; 414 size >>= fs_info->sb->s_blocksize_bits;
413 415
414 read_extent_buffer(path->nodes[0], 416 read_extent_buffer(path->nodes[0],
415 sums->sums, 417 sums->sums,
416 ((unsigned long)item) + offset, 418 ((unsigned long)item) + offset,
417 csum_size * size); 419 csum_size * size);
418 420
419 start += root->fs_info->sectorsize * size; 421 start += fs_info->sectorsize * size;
420 list_add_tail(&sums->list, &tmplist); 422 list_add_tail(&sums->list, &tmplist);
421 } 423 }
422 path->slots[0]++; 424 path->slots[0]++;
@@ -437,6 +439,7 @@ fail:
437int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, 439int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
438 struct bio *bio, u64 file_start, int contig) 440 struct bio *bio, u64 file_start, int contig)
439{ 441{
442 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
440 struct btrfs_ordered_sum *sums; 443 struct btrfs_ordered_sum *sums;
441 struct btrfs_ordered_extent *ordered = NULL; 444 struct btrfs_ordered_extent *ordered = NULL;
442 char *data; 445 char *data;
@@ -449,8 +452,7 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
449 u64 offset; 452 u64 offset;
450 453
451 WARN_ON(bio->bi_vcnt <= 0); 454 WARN_ON(bio->bi_vcnt <= 0);
452 sums = kzalloc(btrfs_ordered_sum_size(root->fs_info, 455 sums = kzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size),
453 bio->bi_iter.bi_size),
454 GFP_NOFS); 456 GFP_NOFS);
455 if (!sums) 457 if (!sums)
456 return -ENOMEM; 458 return -ENOMEM;
@@ -477,9 +479,9 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
477 479
478 data = kmap_atomic(bvec->bv_page); 480 data = kmap_atomic(bvec->bv_page);
479 481
480 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, 482 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info,
481 bvec->bv_len + root->fs_info->sectorsize 483 bvec->bv_len + fs_info->sectorsize
482 - 1); 484 - 1);
483 485
484 for (i = 0; i < nr_sectors; i++) { 486 for (i = 0; i < nr_sectors; i++) {
485 if (offset >= ordered->file_offset + ordered->len || 487 if (offset >= ordered->file_offset + ordered->len ||
@@ -494,8 +496,8 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
494 496
495 bytes_left = bio->bi_iter.bi_size - total_bytes; 497 bytes_left = bio->bi_iter.bi_size - total_bytes;
496 498
497 sums = kzalloc(btrfs_ordered_sum_size(root->fs_info, bytes_left), 499 sums = kzalloc(btrfs_ordered_sum_size(fs_info, bytes_left),
498 GFP_NOFS); 500 GFP_NOFS);
499 BUG_ON(!sums); /* -ENOMEM */ 501 BUG_ON(!sums); /* -ENOMEM */
500 sums->len = bytes_left; 502 sums->len = bytes_left;
501 ordered = btrfs_lookup_ordered_extent(inode, 503 ordered = btrfs_lookup_ordered_extent(inode,
@@ -511,15 +513,15 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
511 sums->sums[index] = ~(u32)0; 513 sums->sums[index] = ~(u32)0;
512 sums->sums[index] 514 sums->sums[index]
513 = btrfs_csum_data(data + bvec->bv_offset 515 = btrfs_csum_data(data + bvec->bv_offset
514 + (i * root->fs_info->sectorsize), 516 + (i * fs_info->sectorsize),
515 sums->sums[index], 517 sums->sums[index],
516 root->fs_info->sectorsize); 518 fs_info->sectorsize);
517 btrfs_csum_final(sums->sums[index], 519 btrfs_csum_final(sums->sums[index],
518 (char *)(sums->sums + index)); 520 (char *)(sums->sums + index));
519 index++; 521 index++;
520 offset += root->fs_info->sectorsize; 522 offset += fs_info->sectorsize;
521 this_sum_bytes += root->fs_info->sectorsize; 523 this_sum_bytes += fs_info->sectorsize;
522 total_bytes += root->fs_info->sectorsize; 524 total_bytes += fs_info->sectorsize;
523 } 525 }
524 526
525 kunmap_atomic(data); 527 kunmap_atomic(data);
@@ -546,15 +548,16 @@ static noinline void truncate_one_csum(struct btrfs_root *root,
546 struct btrfs_key *key, 548 struct btrfs_key *key,
547 u64 bytenr, u64 len) 549 u64 bytenr, u64 len)
548{ 550{
551 struct btrfs_fs_info *fs_info = root->fs_info;
549 struct extent_buffer *leaf; 552 struct extent_buffer *leaf;
550 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 553 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
551 u64 csum_end; 554 u64 csum_end;
552 u64 end_byte = bytenr + len; 555 u64 end_byte = bytenr + len;
553 u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits; 556 u32 blocksize_bits = fs_info->sb->s_blocksize_bits;
554 557
555 leaf = path->nodes[0]; 558 leaf = path->nodes[0];
556 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; 559 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
557 csum_end <<= root->fs_info->sb->s_blocksize_bits; 560 csum_end <<= fs_info->sb->s_blocksize_bits;
558 csum_end += key->offset; 561 csum_end += key->offset;
559 562
560 if (key->offset < bytenr && csum_end <= end_byte) { 563 if (key->offset < bytenr && csum_end <= end_byte) {
@@ -581,7 +584,7 @@ static noinline void truncate_one_csum(struct btrfs_root *root,
581 btrfs_truncate_item(root, path, new_size, 0); 584 btrfs_truncate_item(root, path, new_size, 0);
582 585
583 key->offset = end_byte; 586 key->offset = end_byte;
584 btrfs_set_item_key_safe(root->fs_info, path, key); 587 btrfs_set_item_key_safe(fs_info, path, key);
585 } else { 588 } else {
586 BUG(); 589 BUG();
587 } 590 }
@@ -601,8 +604,8 @@ int btrfs_del_csums(struct btrfs_trans_handle *trans,
601 u64 csum_end; 604 u64 csum_end;
602 struct extent_buffer *leaf; 605 struct extent_buffer *leaf;
603 int ret; 606 int ret;
604 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 607 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
605 int blocksize_bits = root->fs_info->sb->s_blocksize_bits; 608 int blocksize_bits = fs_info->sb->s_blocksize_bits;
606 609
607 path = btrfs_alloc_path(); 610 path = btrfs_alloc_path();
608 if (!path) 611 if (!path)
@@ -711,6 +714,7 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
711 struct btrfs_root *root, 714 struct btrfs_root *root,
712 struct btrfs_ordered_sum *sums) 715 struct btrfs_ordered_sum *sums)
713{ 716{
717 struct btrfs_fs_info *fs_info = root->fs_info;
714 struct btrfs_key file_key; 718 struct btrfs_key file_key;
715 struct btrfs_key found_key; 719 struct btrfs_key found_key;
716 struct btrfs_path *path; 720 struct btrfs_path *path;
@@ -726,7 +730,7 @@ int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
726 int index = 0; 730 int index = 0;
727 int found_next; 731 int found_next;
728 int ret; 732 int ret;
729 u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy); 733 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
730 734
731 path = btrfs_alloc_path(); 735 path = btrfs_alloc_path();
732 if (!path) 736 if (!path)
@@ -759,7 +763,7 @@ again:
759 leaf = path->nodes[0]; 763 leaf = path->nodes[0];
760 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 764 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
761 if ((item_size / csum_size) >= 765 if ((item_size / csum_size) >=
762 MAX_CSUM_ITEMS(root->fs_info, csum_size)) { 766 MAX_CSUM_ITEMS(fs_info, csum_size)) {
763 /* already at max size, make a new one */ 767 /* already at max size, make a new one */
764 goto insert; 768 goto insert;
765 } 769 }
@@ -805,11 +809,11 @@ again:
805 leaf = path->nodes[0]; 809 leaf = path->nodes[0];
806 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 810 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
807 csum_offset = (bytenr - found_key.offset) >> 811 csum_offset = (bytenr - found_key.offset) >>
808 root->fs_info->sb->s_blocksize_bits; 812 fs_info->sb->s_blocksize_bits;
809 813
810 if (found_key.type != BTRFS_EXTENT_CSUM_KEY || 814 if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
811 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || 815 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
812 csum_offset >= MAX_CSUM_ITEMS(root->fs_info, csum_size)) { 816 csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
813 goto insert; 817 goto insert;
814 } 818 }
815 819
@@ -827,12 +831,13 @@ again:
827 free_space = btrfs_leaf_free_space(root, leaf) - 831 free_space = btrfs_leaf_free_space(root, leaf) -
828 sizeof(struct btrfs_item) - csum_size; 832 sizeof(struct btrfs_item) - csum_size;
829 tmp = sums->len - total_bytes; 833 tmp = sums->len - total_bytes;
830 tmp >>= root->fs_info->sb->s_blocksize_bits; 834 tmp >>= fs_info->sb->s_blocksize_bits;
831 WARN_ON(tmp < 1); 835 WARN_ON(tmp < 1);
832 836
833 extend_nr = max_t(int, 1, (int)tmp); 837 extend_nr = max_t(int, 1, (int)tmp);
834 diff = (csum_offset + extend_nr) * csum_size; 838 diff = (csum_offset + extend_nr) * csum_size;
835 diff = min(diff, MAX_CSUM_ITEMS(root->fs_info, csum_size) * csum_size); 839 diff = min(diff,
840 MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
836 841
837 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); 842 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
838 diff = min(free_space, diff); 843 diff = min(free_space, diff);
@@ -851,12 +856,12 @@ insert:
851 u64 tmp; 856 u64 tmp;
852 857
853 tmp = sums->len - total_bytes; 858 tmp = sums->len - total_bytes;
854 tmp >>= root->fs_info->sb->s_blocksize_bits; 859 tmp >>= fs_info->sb->s_blocksize_bits;
855 tmp = min(tmp, (next_offset - file_key.offset) >> 860 tmp = min(tmp, (next_offset - file_key.offset) >>
856 root->fs_info->sb->s_blocksize_bits); 861 fs_info->sb->s_blocksize_bits);
857 862
858 tmp = max((u64)1, tmp); 863 tmp = max((u64)1, tmp);
859 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root->fs_info, csum_size)); 864 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(fs_info, csum_size));
860 ins_size = csum_size * tmp; 865 ins_size = csum_size * tmp;
861 } else { 866 } else {
862 ins_size = csum_size; 867 ins_size = csum_size;
@@ -878,7 +883,7 @@ csum:
878 csum_offset * csum_size); 883 csum_offset * csum_size);
879found: 884found:
880 ins_size = (u32)(sums->len - total_bytes) >> 885 ins_size = (u32)(sums->len - total_bytes) >>
881 root->fs_info->sb->s_blocksize_bits; 886 fs_info->sb->s_blocksize_bits;
882 ins_size *= csum_size; 887 ins_size *= csum_size;
883 ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item, 888 ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
884 ins_size); 889 ins_size);
@@ -886,7 +891,7 @@ found:
886 ins_size); 891 ins_size);
887 892
888 ins_size /= csum_size; 893 ins_size /= csum_size;
889 total_bytes += ins_size * root->fs_info->sectorsize; 894 total_bytes += ins_size * fs_info->sectorsize;
890 index += ins_size; 895 index += ins_size;
891 896
892 btrfs_mark_buffer_dirty(path->nodes[0]); 897 btrfs_mark_buffer_dirty(path->nodes[0]);
@@ -909,6 +914,7 @@ void btrfs_extent_item_to_extent_map(struct inode *inode,
909 const bool new_inline, 914 const bool new_inline,
910 struct extent_map *em) 915 struct extent_map *em)
911{ 916{
917 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
912 struct btrfs_root *root = BTRFS_I(inode)->root; 918 struct btrfs_root *root = BTRFS_I(inode)->root;
913 struct extent_buffer *leaf = path->nodes[0]; 919 struct extent_buffer *leaf = path->nodes[0];
914 const int slot = path->slots[0]; 920 const int slot = path->slots[0];
@@ -918,7 +924,7 @@ void btrfs_extent_item_to_extent_map(struct inode *inode,
918 u8 type = btrfs_file_extent_type(leaf, fi); 924 u8 type = btrfs_file_extent_type(leaf, fi);
919 int compress_type = btrfs_file_extent_compression(leaf, fi); 925 int compress_type = btrfs_file_extent_compression(leaf, fi);
920 926
921 em->bdev = root->fs_info->fs_devices->latest_bdev; 927 em->bdev = fs_info->fs_devices->latest_bdev;
922 btrfs_item_key_to_cpu(leaf, &key, slot); 928 btrfs_item_key_to_cpu(leaf, &key, slot);
923 extent_start = key.offset; 929 extent_start = key.offset;
924 930
@@ -930,7 +936,7 @@ void btrfs_extent_item_to_extent_map(struct inode *inode,
930 size_t size; 936 size_t size;
931 size = btrfs_file_extent_inline_len(leaf, slot, fi); 937 size = btrfs_file_extent_inline_len(leaf, slot, fi);
932 extent_end = ALIGN(extent_start + size, 938 extent_end = ALIGN(extent_start + size,
933 root->fs_info->sectorsize); 939 fs_info->sectorsize);
934 } 940 }
935 941
936 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi); 942 em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
@@ -973,7 +979,7 @@ void btrfs_extent_item_to_extent_map(struct inode *inode,
973 em->compress_type = compress_type; 979 em->compress_type = compress_type;
974 } 980 }
975 } else { 981 } else {
976 btrfs_err(root->fs_info, 982 btrfs_err(fs_info,
977 "unknown file extent item type %d, inode %llu, offset %llu, root %llu", 983 "unknown file extent item type %d, inode %llu, offset %llu, root %llu",
978 type, btrfs_ino(inode), extent_start, 984 type, btrfs_ino(inode), extent_start,
979 root->root_key.objectid); 985 root->root_key.objectid);
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 008670e3c98a..d49d8eadf517 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -95,13 +95,13 @@ static int __compare_inode_defrag(struct inode_defrag *defrag1,
95static int __btrfs_add_inode_defrag(struct inode *inode, 95static int __btrfs_add_inode_defrag(struct inode *inode,
96 struct inode_defrag *defrag) 96 struct inode_defrag *defrag)
97{ 97{
98 struct btrfs_root *root = BTRFS_I(inode)->root; 98 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
99 struct inode_defrag *entry; 99 struct inode_defrag *entry;
100 struct rb_node **p; 100 struct rb_node **p;
101 struct rb_node *parent = NULL; 101 struct rb_node *parent = NULL;
102 int ret; 102 int ret;
103 103
104 p = &root->fs_info->defrag_inodes.rb_node; 104 p = &fs_info->defrag_inodes.rb_node;
105 while (*p) { 105 while (*p) {
106 parent = *p; 106 parent = *p;
107 entry = rb_entry(parent, struct inode_defrag, rb_node); 107 entry = rb_entry(parent, struct inode_defrag, rb_node);
@@ -125,16 +125,18 @@ static int __btrfs_add_inode_defrag(struct inode *inode,
125 } 125 }
126 set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags); 126 set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
127 rb_link_node(&defrag->rb_node, parent, p); 127 rb_link_node(&defrag->rb_node, parent, p);
128 rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes); 128 rb_insert_color(&defrag->rb_node, &fs_info->defrag_inodes);
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_root *root)
133{ 133{
134 if (!btrfs_test_opt(root->fs_info, AUTO_DEFRAG)) 134 struct btrfs_fs_info *fs_info = root->fs_info;
135
136 if (!btrfs_test_opt(fs_info, AUTO_DEFRAG))
135 return 0; 137 return 0;
136 138
137 if (btrfs_fs_closing(root->fs_info)) 139 if (btrfs_fs_closing(fs_info))
138 return 0; 140 return 0;
139 141
140 return 1; 142 return 1;
@@ -147,6 +149,7 @@ static inline int __need_auto_defrag(struct btrfs_root *root)
147int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans, 149int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
148 struct inode *inode) 150 struct inode *inode)
149{ 151{
152 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
150 struct btrfs_root *root = BTRFS_I(inode)->root; 153 struct btrfs_root *root = BTRFS_I(inode)->root;
151 struct inode_defrag *defrag; 154 struct inode_defrag *defrag;
152 u64 transid; 155 u64 transid;
@@ -171,7 +174,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
171 defrag->transid = transid; 174 defrag->transid = transid;
172 defrag->root = root->root_key.objectid; 175 defrag->root = root->root_key.objectid;
173 176
174 spin_lock(&root->fs_info->defrag_inodes_lock); 177 spin_lock(&fs_info->defrag_inodes_lock);
175 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) { 178 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags)) {
176 /* 179 /*
177 * If we set IN_DEFRAG flag and evict the inode from memory, 180 * If we set IN_DEFRAG flag and evict the inode from memory,
@@ -184,7 +187,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
184 } else { 187 } else {
185 kmem_cache_free(btrfs_inode_defrag_cachep, defrag); 188 kmem_cache_free(btrfs_inode_defrag_cachep, defrag);
186 } 189 }
187 spin_unlock(&root->fs_info->defrag_inodes_lock); 190 spin_unlock(&fs_info->defrag_inodes_lock);
188 return 0; 191 return 0;
189} 192}
190 193
@@ -196,6 +199,7 @@ int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
196static void btrfs_requeue_inode_defrag(struct inode *inode, 199static void btrfs_requeue_inode_defrag(struct inode *inode,
197 struct inode_defrag *defrag) 200 struct inode_defrag *defrag)
198{ 201{
202 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
199 struct btrfs_root *root = BTRFS_I(inode)->root; 203 struct btrfs_root *root = BTRFS_I(inode)->root;
200 int ret; 204 int ret;
201 205
@@ -206,9 +210,9 @@ static void btrfs_requeue_inode_defrag(struct inode *inode,
206 * Here we don't check the IN_DEFRAG flag, because we need merge 210 * Here we don't check the IN_DEFRAG flag, because we need merge
207 * them together. 211 * them together.
208 */ 212 */
209 spin_lock(&root->fs_info->defrag_inodes_lock); 213 spin_lock(&fs_info->defrag_inodes_lock);
210 ret = __btrfs_add_inode_defrag(inode, defrag); 214 ret = __btrfs_add_inode_defrag(inode, defrag);
211 spin_unlock(&root->fs_info->defrag_inodes_lock); 215 spin_unlock(&fs_info->defrag_inodes_lock);
212 if (ret) 216 if (ret)
213 goto out; 217 goto out;
214 return; 218 return;
@@ -489,6 +493,7 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
489 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)
491{ 495{
496 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
492 int err = 0; 497 int err = 0;
493 int i; 498 int i;
494 u64 num_bytes; 499 u64 num_bytes;
@@ -497,9 +502,9 @@ int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
497 u64 end_pos = pos + write_bytes; 502 u64 end_pos = pos + write_bytes;
498 loff_t isize = i_size_read(inode); 503 loff_t isize = i_size_read(inode);
499 504
500 start_pos = pos & ~((u64) root->fs_info->sectorsize - 1); 505 start_pos = pos & ~((u64) fs_info->sectorsize - 1);
501 num_bytes = round_up(write_bytes + pos - start_pos, 506 num_bytes = round_up(write_bytes + pos - start_pos,
502 root->fs_info->sectorsize); 507 fs_info->sectorsize);
503 508
504 end_of_last_block = start_pos + num_bytes - 1; 509 end_of_last_block = start_pos + num_bytes - 1;
505 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block, 510 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
@@ -696,6 +701,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
696 u32 extent_item_size, 701 u32 extent_item_size,
697 int *key_inserted) 702 int *key_inserted)
698{ 703{
704 struct btrfs_fs_info *fs_info = root->fs_info;
699 struct extent_buffer *leaf; 705 struct extent_buffer *leaf;
700 struct btrfs_file_extent_item *fi; 706 struct btrfs_file_extent_item *fi;
701 struct btrfs_key key; 707 struct btrfs_key key;
@@ -724,7 +730,7 @@ int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
724 modify_tree = 0; 730 modify_tree = 0;
725 731
726 update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 732 update_refs = (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
727 root == root->fs_info->tree_root); 733 root == fs_info->tree_root);
728 while (1) { 734 while (1) {
729 recow = 0; 735 recow = 0;
730 ret = btrfs_lookup_file_extent(trans, root, path, ino, 736 ret = btrfs_lookup_file_extent(trans, root, path, ino,
@@ -881,7 +887,7 @@ next_slot:
881 887
882 memcpy(&new_key, &key, sizeof(new_key)); 888 memcpy(&new_key, &key, sizeof(new_key));
883 new_key.offset = end; 889 new_key.offset = end;
884 btrfs_set_item_key_safe(root->fs_info, path, &new_key); 890 btrfs_set_item_key_safe(fs_info, path, &new_key);
885 891
886 extent_offset += end - key.offset; 892 extent_offset += end - key.offset;
887 btrfs_set_file_extent_offset(leaf, fi, extent_offset); 893 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
@@ -936,7 +942,7 @@ delete_extent_item:
936 inode_sub_bytes(inode, 942 inode_sub_bytes(inode,
937 extent_end - key.offset); 943 extent_end - key.offset);
938 extent_end = ALIGN(extent_end, 944 extent_end = ALIGN(extent_end,
939 root->fs_info->sectorsize); 945 fs_info->sectorsize);
940 } else if (update_refs && disk_bytenr > 0) { 946 } else if (update_refs && disk_bytenr > 0) {
941 ret = btrfs_free_extent(trans, root, 947 ret = btrfs_free_extent(trans, root,
942 disk_bytenr, num_bytes, 0, 948 disk_bytenr, num_bytes, 0,
@@ -1082,6 +1088,7 @@ static int extent_mergeable(struct extent_buffer *leaf, int slot,
1082int btrfs_mark_extent_written(struct btrfs_trans_handle *trans, 1088int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
1083 struct inode *inode, u64 start, u64 end) 1089 struct inode *inode, u64 start, u64 end)
1084{ 1090{
1091 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1085 struct btrfs_root *root = BTRFS_I(inode)->root; 1092 struct btrfs_root *root = BTRFS_I(inode)->root;
1086 struct extent_buffer *leaf; 1093 struct extent_buffer *leaf;
1087 struct btrfs_path *path; 1094 struct btrfs_path *path;
@@ -1151,7 +1158,7 @@ again:
1151 ino, bytenr, orig_offset, 1158 ino, bytenr, orig_offset,
1152 &other_start, &other_end)) { 1159 &other_start, &other_end)) {
1153 new_key.offset = end; 1160 new_key.offset = end;
1154 btrfs_set_item_key_safe(root->fs_info, path, &new_key); 1161 btrfs_set_item_key_safe(fs_info, path, &new_key);
1155 fi = btrfs_item_ptr(leaf, path->slots[0], 1162 fi = btrfs_item_ptr(leaf, path->slots[0],
1156 struct btrfs_file_extent_item); 1163 struct btrfs_file_extent_item);
1157 btrfs_set_file_extent_generation(leaf, fi, 1164 btrfs_set_file_extent_generation(leaf, fi,
@@ -1185,7 +1192,7 @@ again:
1185 trans->transid); 1192 trans->transid);
1186 path->slots[0]++; 1193 path->slots[0]++;
1187 new_key.offset = start; 1194 new_key.offset = start;
1188 btrfs_set_item_key_safe(root->fs_info, path, &new_key); 1195 btrfs_set_item_key_safe(fs_info, path, &new_key);
1189 1196
1190 fi = btrfs_item_ptr(leaf, path->slots[0], 1197 fi = btrfs_item_ptr(leaf, path->slots[0],
1191 struct btrfs_file_extent_item); 1198 struct btrfs_file_extent_item);
@@ -1418,16 +1425,16 @@ lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
1418 u64 *lockstart, u64 *lockend, 1425 u64 *lockstart, u64 *lockend,
1419 struct extent_state **cached_state) 1426 struct extent_state **cached_state)
1420{ 1427{
1421 struct btrfs_root *root = BTRFS_I(inode)->root; 1428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1422 u64 start_pos; 1429 u64 start_pos;
1423 u64 last_pos; 1430 u64 last_pos;
1424 int i; 1431 int i;
1425 int ret = 0; 1432 int ret = 0;
1426 1433
1427 start_pos = round_down(pos, root->fs_info->sectorsize); 1434 start_pos = round_down(pos, fs_info->sectorsize);
1428 last_pos = start_pos 1435 last_pos = start_pos
1429 + round_up(pos + write_bytes - start_pos, 1436 + round_up(pos + write_bytes - start_pos,
1430 root->fs_info->sectorsize) - 1; 1437 fs_info->sectorsize) - 1;
1431 1438
1432 if (start_pos < inode->i_size) { 1439 if (start_pos < inode->i_size) {
1433 struct btrfs_ordered_extent *ordered; 1440 struct btrfs_ordered_extent *ordered;
@@ -1474,6 +1481,7 @@ lock_and_cleanup_extent_if_need(struct inode *inode, struct page **pages,
1474static noinline int check_can_nocow(struct inode *inode, loff_t pos, 1481static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1475 size_t *write_bytes) 1482 size_t *write_bytes)
1476{ 1483{
1484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1477 struct btrfs_root *root = BTRFS_I(inode)->root; 1485 struct btrfs_root *root = BTRFS_I(inode)->root;
1478 struct btrfs_ordered_extent *ordered; 1486 struct btrfs_ordered_extent *ordered;
1479 u64 lockstart, lockend; 1487 u64 lockstart, lockend;
@@ -1484,9 +1492,9 @@ static noinline int check_can_nocow(struct inode *inode, loff_t pos,
1484 if (!ret) 1492 if (!ret)
1485 return -ENOSPC; 1493 return -ENOSPC;
1486 1494
1487 lockstart = round_down(pos, root->fs_info->sectorsize); 1495 lockstart = round_down(pos, fs_info->sectorsize);
1488 lockend = round_up(pos + *write_bytes, 1496 lockend = round_up(pos + *write_bytes,
1489 root->fs_info->sectorsize) - 1; 1497 fs_info->sectorsize) - 1;
1490 1498
1491 while (1) { 1499 while (1) {
1492 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend); 1500 lock_extent(&BTRFS_I(inode)->io_tree, lockstart, lockend);
@@ -1520,8 +1528,8 @@ static noinline ssize_t __btrfs_buffered_write(struct file *file,
1520 loff_t pos) 1528 loff_t pos)
1521{ 1529{
1522 struct inode *inode = file_inode(file); 1530 struct inode *inode = file_inode(file);
1531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1523 struct btrfs_root *root = BTRFS_I(inode)->root; 1532 struct btrfs_root *root = BTRFS_I(inode)->root;
1524 struct btrfs_fs_info *fs_info = root->fs_info;
1525 struct page **pages = NULL; 1533 struct page **pages = NULL;
1526 struct extent_state *cached_state = NULL; 1534 struct extent_state *cached_state = NULL;
1527 u64 release_bytes = 0; 1535 u64 release_bytes = 0;
@@ -1633,12 +1641,10 @@ again:
1633 1641
1634 copied = btrfs_copy_from_user(pos, write_bytes, pages, i); 1642 copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
1635 1643
1636 num_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, 1644 num_sectors = BTRFS_BYTES_TO_BLKS(fs_info, reserve_bytes);
1637 reserve_bytes);
1638 dirty_sectors = round_up(copied + sector_offset, 1645 dirty_sectors = round_up(copied + sector_offset,
1639 root->fs_info->sectorsize); 1646 fs_info->sectorsize);
1640 dirty_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, 1647 dirty_sectors = BTRFS_BYTES_TO_BLKS(fs_info, dirty_sectors);
1641 dirty_sectors);
1642 1648
1643 /* 1649 /*
1644 * if we have trouble faulting in the pages, fall 1650 * if we have trouble faulting in the pages, fall
@@ -1666,11 +1672,9 @@ again:
1666 * managed to copy. 1672 * managed to copy.
1667 */ 1673 */
1668 if (num_sectors > dirty_sectors) { 1674 if (num_sectors > dirty_sectors) {
1669
1670 /* release everything except the sectors we dirtied */ 1675 /* release everything except the sectors we dirtied */
1671 release_bytes -= dirty_sectors << 1676 release_bytes -= dirty_sectors <<
1672 root->fs_info->sb->s_blocksize_bits; 1677 fs_info->sb->s_blocksize_bits;
1673
1674 if (copied > 0) { 1678 if (copied > 0) {
1675 spin_lock(&BTRFS_I(inode)->lock); 1679 spin_lock(&BTRFS_I(inode)->lock);
1676 BTRFS_I(inode)->outstanding_extents++; 1680 BTRFS_I(inode)->outstanding_extents++;
@@ -1683,7 +1687,7 @@ again:
1683 u64 __pos; 1687 u64 __pos;
1684 1688
1685 __pos = round_down(pos, 1689 __pos = round_down(pos,
1686 root->fs_info->sectorsize) + 1690 fs_info->sectorsize) +
1687 (dirty_pages << PAGE_SHIFT); 1691 (dirty_pages << PAGE_SHIFT);
1688 btrfs_delalloc_release_space(inode, __pos, 1692 btrfs_delalloc_release_space(inode, __pos,
1689 release_bytes); 1693 release_bytes);
@@ -1691,7 +1695,7 @@ again:
1691 } 1695 }
1692 1696
1693 release_bytes = round_up(copied + sector_offset, 1697 release_bytes = round_up(copied + sector_offset,
1694 root->fs_info->sectorsize); 1698 fs_info->sectorsize);
1695 1699
1696 if (copied > 0) 1700 if (copied > 0)
1697 ret = btrfs_dirty_pages(root, inode, pages, 1701 ret = btrfs_dirty_pages(root, inode, pages,
@@ -1712,9 +1716,9 @@ again:
1712 1716
1713 if (only_release_metadata && copied > 0) { 1717 if (only_release_metadata && copied > 0) {
1714 lockstart = round_down(pos, 1718 lockstart = round_down(pos,
1715 root->fs_info->sectorsize); 1719 fs_info->sectorsize);
1716 lockend = round_up(pos + copied, 1720 lockend = round_up(pos + copied,
1717 root->fs_info->sectorsize) - 1; 1721 fs_info->sectorsize) - 1;
1718 1722
1719 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, 1723 set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
1720 lockend, EXTENT_NORESERVE, NULL, 1724 lockend, EXTENT_NORESERVE, NULL,
@@ -1727,7 +1731,7 @@ again:
1727 cond_resched(); 1731 cond_resched();
1728 1732
1729 balance_dirty_pages_ratelimited(inode->i_mapping); 1733 balance_dirty_pages_ratelimited(inode->i_mapping);
1730 if (dirty_pages < (root->fs_info->nodesize >> PAGE_SHIFT) + 1) 1734 if (dirty_pages < (fs_info->nodesize >> PAGE_SHIFT) + 1)
1731 btrfs_btree_balance_dirty(root); 1735 btrfs_btree_balance_dirty(root);
1732 1736
1733 pos += copied; 1737 pos += copied;
@@ -1742,7 +1746,7 @@ again:
1742 btrfs_delalloc_release_metadata(inode, release_bytes); 1746 btrfs_delalloc_release_metadata(inode, release_bytes);
1743 } else { 1747 } else {
1744 btrfs_delalloc_release_space(inode, 1748 btrfs_delalloc_release_space(inode,
1745 round_down(pos, root->fs_info->sectorsize), 1749 round_down(pos, fs_info->sectorsize),
1746 release_bytes); 1750 release_bytes);
1747 } 1751 }
1748 } 1752 }
@@ -1813,6 +1817,7 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1813{ 1817{
1814 struct file *file = iocb->ki_filp; 1818 struct file *file = iocb->ki_filp;
1815 struct inode *inode = file_inode(file); 1819 struct inode *inode = file_inode(file);
1820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1816 struct btrfs_root *root = BTRFS_I(inode)->root; 1821 struct btrfs_root *root = BTRFS_I(inode)->root;
1817 u64 start_pos; 1822 u64 start_pos;
1818 u64 end_pos; 1823 u64 end_pos;
@@ -1844,7 +1849,7 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1844 * although we have opened a file as writable, we have 1849 * although we have opened a file as writable, we have
1845 * to stop this write operation to ensure FS consistency. 1850 * to stop this write operation to ensure FS consistency.
1846 */ 1851 */
1847 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 1852 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
1848 inode_unlock(inode); 1853 inode_unlock(inode);
1849 err = -EROFS; 1854 err = -EROFS;
1850 goto out; 1855 goto out;
@@ -1860,18 +1865,18 @@ static ssize_t btrfs_file_write_iter(struct kiocb *iocb,
1860 1865
1861 pos = iocb->ki_pos; 1866 pos = iocb->ki_pos;
1862 count = iov_iter_count(from); 1867 count = iov_iter_count(from);
1863 start_pos = round_down(pos, root->fs_info->sectorsize); 1868 start_pos = round_down(pos, fs_info->sectorsize);
1864 oldsize = i_size_read(inode); 1869 oldsize = i_size_read(inode);
1865 if (start_pos > oldsize) { 1870 if (start_pos > oldsize) {
1866 /* Expand hole size to cover write data, preventing empty gap */ 1871 /* Expand hole size to cover write data, preventing empty gap */
1867 end_pos = round_up(pos + count, 1872 end_pos = round_up(pos + count,
1868 root->fs_info->sectorsize); 1873 fs_info->sectorsize);
1869 err = btrfs_cont_expand(inode, oldsize, end_pos); 1874 err = btrfs_cont_expand(inode, oldsize, end_pos);
1870 if (err) { 1875 if (err) {
1871 inode_unlock(inode); 1876 inode_unlock(inode);
1872 goto out; 1877 goto out;
1873 } 1878 }
1874 if (start_pos > round_up(oldsize, root->fs_info->sectorsize)) 1879 if (start_pos > round_up(oldsize, fs_info->sectorsize))
1875 clean_page = 1; 1880 clean_page = 1;
1876 } 1881 }
1877 1882
@@ -1951,6 +1956,7 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
1951{ 1956{
1952 struct dentry *dentry = file_dentry(file); 1957 struct dentry *dentry = file_dentry(file);
1953 struct inode *inode = d_inode(dentry); 1958 struct inode *inode = d_inode(dentry);
1959 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1954 struct btrfs_root *root = BTRFS_I(inode)->root; 1960 struct btrfs_root *root = BTRFS_I(inode)->root;
1955 struct btrfs_trans_handle *trans; 1961 struct btrfs_trans_handle *trans;
1956 struct btrfs_log_ctx ctx; 1962 struct btrfs_log_ctx ctx;
@@ -2061,12 +2067,12 @@ int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
2061 * commit does not start nor waits for ordered extents to complete. 2067 * commit does not start nor waits for ordered extents to complete.
2062 */ 2068 */
2063 smp_mb(); 2069 smp_mb();
2064 if (btrfs_inode_in_log(inode, root->fs_info->generation) || 2070 if (btrfs_inode_in_log(inode, fs_info->generation) ||
2065 (full_sync && BTRFS_I(inode)->last_trans <= 2071 (full_sync && BTRFS_I(inode)->last_trans <=
2066 root->fs_info->last_trans_committed) || 2072 fs_info->last_trans_committed) ||
2067 (!btrfs_have_ordered_extents_in_range(inode, start, len) && 2073 (!btrfs_have_ordered_extents_in_range(inode, start, len) &&
2068 BTRFS_I(inode)->last_trans 2074 BTRFS_I(inode)->last_trans
2069 <= root->fs_info->last_trans_committed)) { 2075 <= fs_info->last_trans_committed)) {
2070 /* 2076 /*
2071 * We've had everything committed since the last time we were 2077 * We've had everything committed since the last time we were
2072 * modified so clear this flag in case it was set for whatever 2078 * modified so clear this flag in case it was set for whatever
@@ -2224,6 +2230,7 @@ static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
2224static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode, 2230static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2225 struct btrfs_path *path, u64 offset, u64 end) 2231 struct btrfs_path *path, u64 offset, u64 end)
2226{ 2232{
2233 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2227 struct btrfs_root *root = BTRFS_I(inode)->root; 2234 struct btrfs_root *root = BTRFS_I(inode)->root;
2228 struct extent_buffer *leaf; 2235 struct extent_buffer *leaf;
2229 struct btrfs_file_extent_item *fi; 2236 struct btrfs_file_extent_item *fi;
@@ -2232,7 +2239,7 @@ static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2232 struct btrfs_key key; 2239 struct btrfs_key key;
2233 int ret; 2240 int ret;
2234 2241
2235 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) 2242 if (btrfs_fs_incompat(fs_info, NO_HOLES))
2236 goto out; 2243 goto out;
2237 2244
2238 key.objectid = btrfs_ino(inode); 2245 key.objectid = btrfs_ino(inode);
@@ -2270,7 +2277,7 @@ static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
2270 u64 num_bytes; 2277 u64 num_bytes;
2271 2278
2272 key.offset = offset; 2279 key.offset = offset;
2273 btrfs_set_item_key_safe(root->fs_info, path, &key); 2280 btrfs_set_item_key_safe(fs_info, path, &key);
2274 fi = btrfs_item_ptr(leaf, path->slots[0], 2281 fi = btrfs_item_ptr(leaf, path->slots[0],
2275 struct btrfs_file_extent_item); 2282 struct btrfs_file_extent_item);
2276 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end - 2283 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
@@ -2306,7 +2313,7 @@ out:
2306 hole_em->block_start = EXTENT_MAP_HOLE; 2313 hole_em->block_start = EXTENT_MAP_HOLE;
2307 hole_em->block_len = 0; 2314 hole_em->block_len = 0;
2308 hole_em->orig_block_len = 0; 2315 hole_em->orig_block_len = 0;
2309 hole_em->bdev = root->fs_info->fs_devices->latest_bdev; 2316 hole_em->bdev = fs_info->fs_devices->latest_bdev;
2310 hole_em->compress_type = BTRFS_COMPRESS_NONE; 2317 hole_em->compress_type = BTRFS_COMPRESS_NONE;
2311 hole_em->generation = trans->transid; 2318 hole_em->generation = trans->transid;
2312 2319
@@ -2358,6 +2365,7 @@ static int find_first_non_hole(struct inode *inode, u64 *start, u64 *len)
2358 2365
2359static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) 2366static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2360{ 2367{
2368 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2361 struct btrfs_root *root = BTRFS_I(inode)->root; 2369 struct btrfs_root *root = BTRFS_I(inode)->root;
2362 struct extent_state *cached_state = NULL; 2370 struct extent_state *cached_state = NULL;
2363 struct btrfs_path *path; 2371 struct btrfs_path *path;
@@ -2369,13 +2377,13 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2369 u64 tail_len; 2377 u64 tail_len;
2370 u64 orig_start = offset; 2378 u64 orig_start = offset;
2371 u64 cur_offset; 2379 u64 cur_offset;
2372 u64 min_size = btrfs_calc_trunc_metadata_size(root->fs_info, 1); 2380 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
2373 u64 drop_end; 2381 u64 drop_end;
2374 int ret = 0; 2382 int ret = 0;
2375 int err = 0; 2383 int err = 0;
2376 unsigned int rsv_count; 2384 unsigned int rsv_count;
2377 bool same_block; 2385 bool same_block;
2378 bool no_holes = btrfs_fs_incompat(root->fs_info, NO_HOLES); 2386 bool no_holes = btrfs_fs_incompat(fs_info, NO_HOLES);
2379 u64 ino_size; 2387 u64 ino_size;
2380 bool truncated_block = false; 2388 bool truncated_block = false;
2381 bool updated_inode = false; 2389 bool updated_inode = false;
@@ -2385,7 +2393,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2385 return ret; 2393 return ret;
2386 2394
2387 inode_lock(inode); 2395 inode_lock(inode);
2388 ino_size = round_up(inode->i_size, root->fs_info->sectorsize); 2396 ino_size = round_up(inode->i_size, fs_info->sectorsize);
2389 ret = find_first_non_hole(inode, &offset, &len); 2397 ret = find_first_non_hole(inode, &offset, &len);
2390 if (ret < 0) 2398 if (ret < 0)
2391 goto out_only_mutex; 2399 goto out_only_mutex;
@@ -2398,8 +2406,8 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2398 lockstart = round_up(offset, btrfs_inode_sectorsize(inode)); 2406 lockstart = round_up(offset, btrfs_inode_sectorsize(inode));
2399 lockend = round_down(offset + len, 2407 lockend = round_down(offset + len,
2400 btrfs_inode_sectorsize(inode)) - 1; 2408 btrfs_inode_sectorsize(inode)) - 1;
2401 same_block = (BTRFS_BYTES_TO_BLKS(root->fs_info, offset)) 2409 same_block = (BTRFS_BYTES_TO_BLKS(fs_info, offset))
2402 == (BTRFS_BYTES_TO_BLKS(root->fs_info, offset + len - 1)); 2410 == (BTRFS_BYTES_TO_BLKS(fs_info, offset + len - 1));
2403 /* 2411 /*
2404 * We needn't truncate any block which is beyond the end of the file 2412 * We needn't truncate any block which is beyond the end of the file
2405 * because we are sure there is no data there. 2413 * because we are sure there is no data there.
@@ -2408,7 +2416,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2408 * Only do this if we are in the same block and we aren't doing the 2416 * Only do this if we are in the same block and we aren't doing the
2409 * entire block. 2417 * entire block.
2410 */ 2418 */
2411 if (same_block && len < root->fs_info->sectorsize) { 2419 if (same_block && len < fs_info->sectorsize) {
2412 if (offset < ino_size) { 2420 if (offset < ino_size) {
2413 truncated_block = true; 2421 truncated_block = true;
2414 ret = btrfs_truncate_block(inode, offset, len, 0); 2422 ret = btrfs_truncate_block(inode, offset, len, 0);
@@ -2516,7 +2524,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2516 ret = -ENOMEM; 2524 ret = -ENOMEM;
2517 goto out_free; 2525 goto out_free;
2518 } 2526 }
2519 rsv->size = btrfs_calc_trunc_metadata_size(root->fs_info, 1); 2527 rsv->size = btrfs_calc_trunc_metadata_size(fs_info, 1);
2520 rsv->failfast = 1; 2528 rsv->failfast = 1;
2521 2529
2522 /* 2530 /*
@@ -2531,7 +2539,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2531 goto out_free; 2539 goto out_free;
2532 } 2540 }
2533 2541
2534 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, 2542 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
2535 min_size, 0); 2543 min_size, 0);
2536 BUG_ON(ret); 2544 BUG_ON(ret);
2537 trans->block_rsv = rsv; 2545 trans->block_rsv = rsv;
@@ -2545,7 +2553,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2545 if (ret != -ENOSPC) 2553 if (ret != -ENOSPC)
2546 break; 2554 break;
2547 2555
2548 trans->block_rsv = &root->fs_info->trans_block_rsv; 2556 trans->block_rsv = &fs_info->trans_block_rsv;
2549 2557
2550 if (cur_offset < drop_end && cur_offset < ino_size) { 2558 if (cur_offset < drop_end && cur_offset < ino_size) {
2551 ret = fill_holes(trans, inode, path, cur_offset, 2559 ret = fill_holes(trans, inode, path, cur_offset,
@@ -2581,7 +2589,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2581 break; 2589 break;
2582 } 2590 }
2583 2591
2584 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, 2592 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
2585 rsv, min_size, 0); 2593 rsv, min_size, 0);
2586 BUG_ON(ret); /* shouldn't happen */ 2594 BUG_ON(ret); /* shouldn't happen */
2587 trans->block_rsv = rsv; 2595 trans->block_rsv = rsv;
@@ -2600,7 +2608,7 @@ static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
2600 goto out_trans; 2608 goto out_trans;
2601 } 2609 }
2602 2610
2603 trans->block_rsv = &root->fs_info->trans_block_rsv; 2611 trans->block_rsv = &fs_info->trans_block_rsv;
2604 /* 2612 /*
2605 * If we are using the NO_HOLES feature we might have had already an 2613 * If we are using the NO_HOLES feature we might have had already an
2606 * hole that overlaps a part of the region [lockstart, lockend] and 2614 * hole that overlaps a part of the region [lockstart, lockend] and
@@ -2636,7 +2644,7 @@ out_trans:
2636 inode_inc_iversion(inode); 2644 inode_inc_iversion(inode);
2637 inode->i_mtime = inode->i_ctime = current_time(inode); 2645 inode->i_mtime = inode->i_ctime = current_time(inode);
2638 2646
2639 trans->block_rsv = &root->fs_info->trans_block_rsv; 2647 trans->block_rsv = &fs_info->trans_block_rsv;
2640 ret = btrfs_update_inode(trans, root, inode); 2648 ret = btrfs_update_inode(trans, root, inode);
2641 updated_inode = true; 2649 updated_inode = true;
2642 btrfs_end_transaction(trans, root); 2650 btrfs_end_transaction(trans, root);
@@ -2922,7 +2930,7 @@ out:
2922 2930
2923static int find_desired_extent(struct inode *inode, loff_t *offset, int whence) 2931static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
2924{ 2932{
2925 struct btrfs_root *root = BTRFS_I(inode)->root; 2933 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2926 struct extent_map *em = NULL; 2934 struct extent_map *em = NULL;
2927 struct extent_state *cached_state = NULL; 2935 struct extent_state *cached_state = NULL;
2928 u64 lockstart; 2936 u64 lockstart;
@@ -2940,11 +2948,11 @@ static int find_desired_extent(struct inode *inode, loff_t *offset, int whence)
2940 */ 2948 */
2941 start = max_t(loff_t, 0, *offset); 2949 start = max_t(loff_t, 0, *offset);
2942 2950
2943 lockstart = round_down(start, root->fs_info->sectorsize); 2951 lockstart = round_down(start, fs_info->sectorsize);
2944 lockend = round_up(i_size_read(inode), 2952 lockend = round_up(i_size_read(inode),
2945 root->fs_info->sectorsize); 2953 fs_info->sectorsize);
2946 if (lockend <= lockstart) 2954 if (lockend <= lockstart)
2947 lockend = lockstart + root->fs_info->sectorsize; 2955 lockend = lockstart + fs_info->sectorsize;
2948 lockend--; 2956 lockend--;
2949 len = lockend - lockstart + 1; 2957 len = lockend - lockstart + 1;
2950 2958
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 842461792d4e..e93763673b05 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -47,6 +47,7 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
47 struct btrfs_path *path, 47 struct btrfs_path *path,
48 u64 offset) 48 u64 offset)
49{ 49{
50 struct btrfs_fs_info *fs_info = root->fs_info;
50 struct btrfs_key key; 51 struct btrfs_key key;
51 struct btrfs_key location; 52 struct btrfs_key location;
52 struct btrfs_disk_key disk_key; 53 struct btrfs_disk_key disk_key;
@@ -74,7 +75,7 @@ static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
74 btrfs_disk_key_to_cpu(&location, &disk_key); 75 btrfs_disk_key_to_cpu(&location, &disk_key);
75 btrfs_release_path(path); 76 btrfs_release_path(path);
76 77
77 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL); 78 inode = btrfs_iget(fs_info->sb, &location, root, NULL);
78 if (IS_ERR(inode)) 79 if (IS_ERR(inode))
79 return inode; 80 return inode;
80 if (is_bad_inode(inode)) { 81 if (is_bad_inode(inode)) {
@@ -94,6 +95,7 @@ struct inode *lookup_free_space_inode(struct btrfs_root *root,
94 *block_group, struct btrfs_path *path) 95 *block_group, struct btrfs_path *path)
95{ 96{
96 struct inode *inode = NULL; 97 struct inode *inode = NULL;
98 struct btrfs_fs_info *fs_info = root->fs_info;
97 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW; 99 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
98 100
99 spin_lock(&block_group->lock); 101 spin_lock(&block_group->lock);
@@ -110,8 +112,7 @@ struct inode *lookup_free_space_inode(struct btrfs_root *root,
110 112
111 spin_lock(&block_group->lock); 113 spin_lock(&block_group->lock);
112 if (!((BTRFS_I(inode)->flags & flags) == flags)) { 114 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
113 btrfs_info(root->fs_info, 115 btrfs_info(fs_info, "Old style space inode found, converting.");
114 "Old style space inode found, converting.");
115 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM | 116 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
116 BTRFS_INODE_NODATACOW; 117 BTRFS_INODE_NODATACOW;
117 block_group->disk_cache_state = BTRFS_DC_CLEAR; 118 block_group->disk_cache_state = BTRFS_DC_CLEAR;
@@ -206,12 +207,13 @@ int create_free_space_inode(struct btrfs_root *root,
206int btrfs_check_trunc_cache_free_space(struct btrfs_root *root, 207int btrfs_check_trunc_cache_free_space(struct btrfs_root *root,
207 struct btrfs_block_rsv *rsv) 208 struct btrfs_block_rsv *rsv)
208{ 209{
210 struct btrfs_fs_info *fs_info = root->fs_info;
209 u64 needed_bytes; 211 u64 needed_bytes;
210 int ret; 212 int ret;
211 213
212 /* 1 for slack space, 1 for updating the inode */ 214 /* 1 for slack space, 1 for updating the inode */
213 needed_bytes = btrfs_calc_trunc_metadata_size(root->fs_info, 1) + 215 needed_bytes = btrfs_calc_trunc_metadata_size(fs_info, 1) +
214 btrfs_calc_trans_metadata_size(root->fs_info, 1); 216 btrfs_calc_trans_metadata_size(fs_info, 1);
215 217
216 spin_lock(&rsv->lock); 218 spin_lock(&rsv->lock);
217 if (rsv->reserved < needed_bytes) 219 if (rsv->reserved < needed_bytes)
@@ -667,6 +669,7 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
667 struct btrfs_free_space_ctl *ctl, 669 struct btrfs_free_space_ctl *ctl,
668 struct btrfs_path *path, u64 offset) 670 struct btrfs_path *path, u64 offset)
669{ 671{
672 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
670 struct btrfs_free_space_header *header; 673 struct btrfs_free_space_header *header;
671 struct extent_buffer *leaf; 674 struct extent_buffer *leaf;
672 struct btrfs_io_ctl io_ctl; 675 struct btrfs_io_ctl io_ctl;
@@ -706,16 +709,16 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
706 btrfs_release_path(path); 709 btrfs_release_path(path);
707 710
708 if (!BTRFS_I(inode)->generation) { 711 if (!BTRFS_I(inode)->generation) {
709 btrfs_info(root->fs_info, 712 btrfs_info(fs_info,
710 "The free space cache file (%llu) is invalid. skip it\n", 713 "The free space cache file (%llu) is invalid. skip it\n",
711 offset); 714 offset);
712 return 0; 715 return 0;
713 } 716 }
714 717
715 if (BTRFS_I(inode)->generation != generation) { 718 if (BTRFS_I(inode)->generation != generation) {
716 btrfs_err(root->fs_info, 719 btrfs_err(fs_info,
717 "free space inode generation (%llu) did not match free space cache generation (%llu)", 720 "free space inode generation (%llu) did not match free space cache generation (%llu)",
718 BTRFS_I(inode)->generation, generation); 721 BTRFS_I(inode)->generation, generation);
719 return 0; 722 return 0;
720 } 723 }
721 724
@@ -764,7 +767,7 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
764 ret = link_free_space(ctl, e); 767 ret = link_free_space(ctl, e);
765 spin_unlock(&ctl->tree_lock); 768 spin_unlock(&ctl->tree_lock);
766 if (ret) { 769 if (ret) {
767 btrfs_err(root->fs_info, 770 btrfs_err(fs_info,
768 "Duplicate entries in free space cache, dumping"); 771 "Duplicate entries in free space cache, dumping");
769 kmem_cache_free(btrfs_free_space_cachep, e); 772 kmem_cache_free(btrfs_free_space_cachep, e);
770 goto free_cache; 773 goto free_cache;
@@ -784,7 +787,7 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
784 ctl->op->recalc_thresholds(ctl); 787 ctl->op->recalc_thresholds(ctl);
785 spin_unlock(&ctl->tree_lock); 788 spin_unlock(&ctl->tree_lock);
786 if (ret) { 789 if (ret) {
787 btrfs_err(root->fs_info, 790 btrfs_err(fs_info,
788 "Duplicate entries in free space cache, dumping"); 791 "Duplicate entries in free space cache, dumping");
789 kmem_cache_free(btrfs_free_space_cachep, e); 792 kmem_cache_free(btrfs_free_space_cachep, e);
790 goto free_cache; 793 goto free_cache;
@@ -1036,6 +1039,7 @@ write_pinned_extent_entries(struct btrfs_root *root,
1036 struct btrfs_io_ctl *io_ctl, 1039 struct btrfs_io_ctl *io_ctl,
1037 int *entries) 1040 int *entries)
1038{ 1041{
1042 struct btrfs_fs_info *fs_info;
1039 u64 start, extent_start, extent_end, len; 1043 u64 start, extent_start, extent_end, len;
1040 struct extent_io_tree *unpin = NULL; 1044 struct extent_io_tree *unpin = NULL;
1041 int ret; 1045 int ret;
@@ -1043,6 +1047,8 @@ write_pinned_extent_entries(struct btrfs_root *root,
1043 if (!block_group) 1047 if (!block_group)
1044 return 0; 1048 return 0;
1045 1049
1050 fs_info = block_group->fs_info;
1051
1046 /* 1052 /*
1047 * We want to add any pinned extents to our free space cache 1053 * We want to add any pinned extents to our free space cache
1048 * so we don't leak the space 1054 * so we don't leak the space
@@ -1050,7 +1056,7 @@ write_pinned_extent_entries(struct btrfs_root *root,
1050 * We shouldn't have switched the pinned extents yet so this is the 1056 * We shouldn't have switched the pinned extents yet so this is the
1051 * right one 1057 * right one
1052 */ 1058 */
1053 unpin = root->fs_info->pinned_extents; 1059 unpin = fs_info->pinned_extents;
1054 1060
1055 start = block_group->key.objectid; 1061 start = block_group->key.objectid;
1056 1062
@@ -1141,12 +1147,15 @@ int btrfs_wait_cache_io(struct btrfs_root *root,
1141{ 1147{
1142 int ret; 1148 int ret;
1143 struct inode *inode = io_ctl->inode; 1149 struct inode *inode = io_ctl->inode;
1150 struct btrfs_fs_info *fs_info;
1144 1151
1145 if (!inode) 1152 if (!inode)
1146 return 0; 1153 return 0;
1147 1154
1155 fs_info = btrfs_sb(inode->i_sb);
1156
1148 if (block_group) 1157 if (block_group)
1149 root = root->fs_info->tree_root; 1158 root = fs_info->tree_root;
1150 1159
1151 /* Flush the dirty pages in the cache file. */ 1160 /* Flush the dirty pages in the cache file. */
1152 ret = flush_dirty_cache(inode); 1161 ret = flush_dirty_cache(inode);
@@ -1163,9 +1172,9 @@ out:
1163 BTRFS_I(inode)->generation = 0; 1172 BTRFS_I(inode)->generation = 0;
1164 if (block_group) { 1173 if (block_group) {
1165#ifdef DEBUG 1174#ifdef DEBUG
1166 btrfs_err(root->fs_info, 1175 btrfs_err(fs_info,
1167 "failed to write free space cache for block group %llu", 1176 "failed to write free space cache for block group %llu",
1168 block_group->key.objectid); 1177 block_group->key.objectid);
1169#endif 1178#endif
1170 } 1179 }
1171 } 1180 }
@@ -1376,9 +1385,9 @@ int btrfs_write_out_cache(struct btrfs_fs_info *fs_info,
1376 path, block_group->key.objectid); 1385 path, block_group->key.objectid);
1377 if (ret) { 1386 if (ret) {
1378#ifdef DEBUG 1387#ifdef DEBUG
1379 btrfs_err(root->fs_info, 1388 btrfs_err(fs_info,
1380 "failed to write free space cache for block group %llu", 1389 "failed to write free space cache for block group %llu",
1381 block_group->key.objectid); 1390 block_group->key.objectid);
1382#endif 1391#endif
1383 spin_lock(&block_group->lock); 1392 spin_lock(&block_group->lock);
1384 block_group->disk_cache_state = BTRFS_DC_ERROR; 1393 block_group->disk_cache_state = BTRFS_DC_ERROR;
@@ -1965,11 +1974,11 @@ static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1965 struct btrfs_free_space *info) 1974 struct btrfs_free_space *info)
1966{ 1975{
1967 struct btrfs_block_group_cache *block_group = ctl->private; 1976 struct btrfs_block_group_cache *block_group = ctl->private;
1977 struct btrfs_fs_info *fs_info = block_group->fs_info;
1968 bool forced = false; 1978 bool forced = false;
1969 1979
1970#ifdef CONFIG_BTRFS_DEBUG 1980#ifdef CONFIG_BTRFS_DEBUG
1971 if (btrfs_should_fragment_free_space(block_group->fs_info->extent_root, 1981 if (btrfs_should_fragment_free_space(fs_info->extent_root, block_group))
1972 block_group))
1973 forced = true; 1982 forced = true;
1974#endif 1983#endif
1975 1984
@@ -1985,7 +1994,7 @@ static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1985 * of cache left then go ahead an dadd them, no sense in adding 1994 * of cache left then go ahead an dadd them, no sense in adding
1986 * the overhead of a bitmap if we don't have to. 1995 * the overhead of a bitmap if we don't have to.
1987 */ 1996 */
1988 if (info->bytes <= block_group->fs_info->sectorsize * 4) { 1997 if (info->bytes <= fs_info->sectorsize * 4) {
1989 if (ctl->free_extents * 2 <= ctl->extents_thresh) 1998 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1990 return false; 1999 return false;
1991 } else { 2000 } else {
@@ -2444,6 +2453,7 @@ out:
2444void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group, 2453void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2445 u64 bytes) 2454 u64 bytes)
2446{ 2455{
2456 struct btrfs_fs_info *fs_info = block_group->fs_info;
2447 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 2457 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2448 struct btrfs_free_space *info; 2458 struct btrfs_free_space *info;
2449 struct rb_node *n; 2459 struct rb_node *n;
@@ -2453,23 +2463,23 @@ void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
2453 info = rb_entry(n, struct btrfs_free_space, offset_index); 2463 info = rb_entry(n, struct btrfs_free_space, offset_index);
2454 if (info->bytes >= bytes && !block_group->ro) 2464 if (info->bytes >= bytes && !block_group->ro)
2455 count++; 2465 count++;
2456 btrfs_crit(block_group->fs_info, 2466 btrfs_crit(fs_info, "entry offset %llu, bytes %llu, bitmap %s",
2457 "entry offset %llu, bytes %llu, bitmap %s",
2458 info->offset, info->bytes, 2467 info->offset, info->bytes,
2459 (info->bitmap) ? "yes" : "no"); 2468 (info->bitmap) ? "yes" : "no");
2460 } 2469 }
2461 btrfs_info(block_group->fs_info, "block group has cluster?: %s", 2470 btrfs_info(fs_info, "block group has cluster?: %s",
2462 list_empty(&block_group->cluster_list) ? "no" : "yes"); 2471 list_empty(&block_group->cluster_list) ? "no" : "yes");
2463 btrfs_info(block_group->fs_info, 2472 btrfs_info(fs_info,
2464 "%d blocks of free space at or bigger than bytes is", count); 2473 "%d blocks of free space at or bigger than bytes is", count);
2465} 2474}
2466 2475
2467void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group) 2476void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
2468{ 2477{
2478 struct btrfs_fs_info *fs_info = block_group->fs_info;
2469 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 2479 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
2470 2480
2471 spin_lock_init(&ctl->tree_lock); 2481 spin_lock_init(&ctl->tree_lock);
2472 ctl->unit = block_group->fs_info->sectorsize; 2482 ctl->unit = fs_info->sectorsize;
2473 ctl->start = block_group->key.objectid; 2483 ctl->start = block_group->key.objectid;
2474 ctl->private = block_group; 2484 ctl->private = block_group;
2475 ctl->op = &free_space_op; 2485 ctl->op = &free_space_op;
@@ -3014,6 +3024,7 @@ int btrfs_find_space_cluster(struct btrfs_root *root,
3014 u64 offset, u64 bytes, u64 empty_size) 3024 u64 offset, u64 bytes, u64 empty_size)
3015{ 3025{
3016 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; 3026 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
3027 struct btrfs_fs_info *fs_info = block_group->fs_info;
3017 struct btrfs_free_space *entry, *tmp; 3028 struct btrfs_free_space *entry, *tmp;
3018 LIST_HEAD(bitmaps); 3029 LIST_HEAD(bitmaps);
3019 u64 min_bytes; 3030 u64 min_bytes;
@@ -3026,14 +3037,14 @@ int btrfs_find_space_cluster(struct btrfs_root *root,
3026 * For metadata, allow allocates with smaller extents. For 3037 * For metadata, allow allocates with smaller extents. For
3027 * data, keep it dense. 3038 * data, keep it dense.
3028 */ 3039 */
3029 if (btrfs_test_opt(root->fs_info, SSD_SPREAD)) { 3040 if (btrfs_test_opt(fs_info, SSD_SPREAD)) {
3030 cont1_bytes = min_bytes = bytes + empty_size; 3041 cont1_bytes = min_bytes = bytes + empty_size;
3031 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { 3042 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
3032 cont1_bytes = bytes; 3043 cont1_bytes = bytes;
3033 min_bytes = block_group->fs_info->sectorsize; 3044 min_bytes = fs_info->sectorsize;
3034 } else { 3045 } else {
3035 cont1_bytes = max(bytes, (bytes + empty_size) >> 2); 3046 cont1_bytes = max(bytes, (bytes + empty_size) >> 2);
3036 min_bytes = block_group->fs_info->sectorsize; 3047 min_bytes = fs_info->sectorsize;
3037 } 3048 }
3038 3049
3039 spin_lock(&ctl->tree_lock); 3050 spin_lock(&ctl->tree_lock);
@@ -3318,6 +3329,7 @@ void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache)
3318 3329
3319void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group) 3330void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3320{ 3331{
3332 struct btrfs_fs_info *fs_info = block_group->fs_info;
3321 struct extent_map_tree *em_tree; 3333 struct extent_map_tree *em_tree;
3322 struct extent_map *em; 3334 struct extent_map *em;
3323 bool cleanup; 3335 bool cleanup;
@@ -3328,8 +3340,8 @@ void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3328 spin_unlock(&block_group->lock); 3340 spin_unlock(&block_group->lock);
3329 3341
3330 if (cleanup) { 3342 if (cleanup) {
3331 lock_chunks(block_group->fs_info); 3343 lock_chunks(fs_info);
3332 em_tree = &block_group->fs_info->mapping_tree.map_tree; 3344 em_tree = &fs_info->mapping_tree.map_tree;
3333 write_lock(&em_tree->lock); 3345 write_lock(&em_tree->lock);
3334 em = lookup_extent_mapping(em_tree, block_group->key.objectid, 3346 em = lookup_extent_mapping(em_tree, block_group->key.objectid,
3335 1); 3347 1);
@@ -3340,7 +3352,7 @@ void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *block_group)
3340 */ 3352 */
3341 remove_extent_mapping(em_tree, em); 3353 remove_extent_mapping(em_tree, em);
3342 write_unlock(&em_tree->lock); 3354 write_unlock(&em_tree->lock);
3343 unlock_chunks(block_group->fs_info); 3355 unlock_chunks(fs_info);
3344 3356
3345 /* once for us and once for the tree */ 3357 /* once for us and once for the tree */
3346 free_extent_map(em); 3358 free_extent_map(em);
@@ -3470,7 +3482,7 @@ int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
3470 int ret = 0; 3482 int ret = 0;
3471 u64 root_gen = btrfs_root_generation(&root->root_item); 3483 u64 root_gen = btrfs_root_generation(&root->root_item);
3472 3484
3473 if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE)) 3485 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3474 return 0; 3486 return 0;
3475 3487
3476 /* 3488 /*
@@ -3509,12 +3521,13 @@ int btrfs_write_out_ino_cache(struct btrfs_root *root,
3509 struct btrfs_path *path, 3521 struct btrfs_path *path,
3510 struct inode *inode) 3522 struct inode *inode)
3511{ 3523{
3524 struct btrfs_fs_info *fs_info = root->fs_info;
3512 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; 3525 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
3513 int ret; 3526 int ret;
3514 struct btrfs_io_ctl io_ctl; 3527 struct btrfs_io_ctl io_ctl;
3515 bool release_metadata = true; 3528 bool release_metadata = true;
3516 3529
3517 if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE)) 3530 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
3518 return 0; 3531 return 0;
3519 3532
3520 memset(&io_ctl, 0, sizeof(io_ctl)); 3533 memset(&io_ctl, 0, sizeof(io_ctl));
@@ -3535,9 +3548,9 @@ int btrfs_write_out_ino_cache(struct btrfs_root *root,
3535 if (release_metadata) 3548 if (release_metadata)
3536 btrfs_delalloc_release_metadata(inode, inode->i_size); 3549 btrfs_delalloc_release_metadata(inode, inode->i_size);
3537#ifdef DEBUG 3550#ifdef DEBUG
3538 btrfs_err(root->fs_info, 3551 btrfs_err(fs_info,
3539 "failed to write free ino cache for root %llu", 3552 "failed to write free ino cache for root %llu",
3540 root->root_key.objectid); 3553 root->root_key.objectid);
3541#endif 3554#endif
3542 } 3555 }
3543 3556
diff --git a/fs/btrfs/free-space-tree.c b/fs/btrfs/free-space-tree.c
index 9dfb535d8378..2a08f518353b 100644
--- a/fs/btrfs/free-space-tree.c
+++ b/fs/btrfs/free-space-tree.c
@@ -189,7 +189,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
189 int ret; 189 int ret;
190 190
191 bitmap_size = free_space_bitmap_size(block_group->key.offset, 191 bitmap_size = free_space_bitmap_size(block_group->key.offset,
192 block_group->fs_info->sectorsize); 192 fs_info->sectorsize);
193 bitmap = alloc_bitmap(bitmap_size); 193 bitmap = alloc_bitmap(bitmap_size);
194 if (!bitmap) { 194 if (!bitmap) {
195 ret = -ENOMEM; 195 ret = -ENOMEM;
@@ -227,9 +227,9 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
227 ASSERT(found_key.objectid + found_key.offset <= end); 227 ASSERT(found_key.objectid + found_key.offset <= end);
228 228
229 first = div_u64(found_key.objectid - start, 229 first = div_u64(found_key.objectid - start,
230 block_group->fs_info->sectorsize); 230 fs_info->sectorsize);
231 last = div_u64(found_key.objectid + found_key.offset - start, 231 last = div_u64(found_key.objectid + found_key.offset - start,
232 block_group->fs_info->sectorsize); 232 fs_info->sectorsize);
233 le_bitmap_set(bitmap, first, last - first); 233 le_bitmap_set(bitmap, first, last - first);
234 234
235 extent_count++; 235 extent_count++;
@@ -270,7 +270,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
270 } 270 }
271 271
272 bitmap_cursor = bitmap; 272 bitmap_cursor = bitmap;
273 bitmap_range = block_group->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS; 273 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
274 i = start; 274 i = start;
275 while (i < end) { 275 while (i < end) {
276 unsigned long ptr; 276 unsigned long ptr;
@@ -279,7 +279,7 @@ int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
279 279
280 extent_size = min(end - i, bitmap_range); 280 extent_size = min(end - i, bitmap_range);
281 data_size = free_space_bitmap_size(extent_size, 281 data_size = free_space_bitmap_size(extent_size,
282 block_group->fs_info->sectorsize); 282 fs_info->sectorsize);
283 283
284 key.objectid = i; 284 key.objectid = i;
285 key.type = BTRFS_FREE_SPACE_BITMAP_KEY; 285 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
@@ -330,7 +330,7 @@ int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
330 int ret; 330 int ret;
331 331
332 bitmap_size = free_space_bitmap_size(block_group->key.offset, 332 bitmap_size = free_space_bitmap_size(block_group->key.offset,
333 block_group->fs_info->sectorsize); 333 fs_info->sectorsize);
334 bitmap = alloc_bitmap(bitmap_size); 334 bitmap = alloc_bitmap(bitmap_size);
335 if (!bitmap) { 335 if (!bitmap) {
336 ret = -ENOMEM; 336 ret = -ENOMEM;
@@ -370,11 +370,11 @@ int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
370 ASSERT(found_key.objectid + found_key.offset <= end); 370 ASSERT(found_key.objectid + found_key.offset <= end);
371 371
372 bitmap_pos = div_u64(found_key.objectid - start, 372 bitmap_pos = div_u64(found_key.objectid - start,
373 block_group->fs_info->sectorsize * 373 fs_info->sectorsize *
374 BITS_PER_BYTE); 374 BITS_PER_BYTE);
375 bitmap_cursor = bitmap + bitmap_pos; 375 bitmap_cursor = bitmap + bitmap_pos;
376 data_size = free_space_bitmap_size(found_key.offset, 376 data_size = free_space_bitmap_size(found_key.offset,
377 block_group->fs_info->sectorsize); 377 fs_info->sectorsize);
378 378
379 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1); 379 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
380 read_extent_buffer(leaf, bitmap_cursor, ptr, 380 read_extent_buffer(leaf, bitmap_cursor, ptr,
@@ -425,7 +425,7 @@ int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
425 extent_count++; 425 extent_count++;
426 } 426 }
427 prev_bit = bit; 427 prev_bit = bit;
428 offset += block_group->fs_info->sectorsize; 428 offset += fs_info->sectorsize;
429 bitnr++; 429 bitnr++;
430 } 430 }
431 if (prev_bit == 1) { 431 if (prev_bit == 1) {
@@ -526,6 +526,7 @@ static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
526 struct btrfs_path *path, u64 *start, u64 *size, 526 struct btrfs_path *path, u64 *start, u64 *size,
527 int bit) 527 int bit)
528{ 528{
529 struct btrfs_fs_info *fs_info = block_group->fs_info;
529 struct extent_buffer *leaf; 530 struct extent_buffer *leaf;
530 struct btrfs_key key; 531 struct btrfs_key key;
531 u64 end = *start + *size; 532 u64 end = *start + *size;
@@ -545,10 +546,8 @@ static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
545 end = found_end; 546 end = found_end;
546 547
547 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); 548 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
548 first = div_u64(*start - found_start, 549 first = div_u64(*start - found_start, fs_info->sectorsize);
549 block_group->fs_info->sectorsize); 550 last = div_u64(end - found_start, fs_info->sectorsize);
550 last = div_u64(end - found_start,
551 block_group->fs_info->sectorsize);
552 if (bit) 551 if (bit)
553 extent_buffer_bitmap_set(leaf, ptr, first, last - first); 552 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
554 else 553 else
@@ -1270,7 +1269,7 @@ int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1270 list_del(&free_space_root->dirty_list); 1269 list_del(&free_space_root->dirty_list);
1271 1270
1272 btrfs_tree_lock(free_space_root->node); 1271 btrfs_tree_lock(free_space_root->node);
1273 clean_tree_block(trans, tree_root->fs_info, free_space_root->node); 1272 clean_tree_block(trans, fs_info, free_space_root->node);
1274 btrfs_tree_unlock(free_space_root->node); 1273 btrfs_tree_unlock(free_space_root->node);
1275 btrfs_free_tree_block(trans, free_space_root, free_space_root->node, 1274 btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
1276 0, 1); 1275 0, 1);
@@ -1476,7 +1475,7 @@ static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1476 extent_count++; 1475 extent_count++;
1477 } 1476 }
1478 prev_bit = bit; 1477 prev_bit = bit;
1479 offset += block_group->fs_info->sectorsize; 1478 offset += fs_info->sectorsize;
1480 } 1479 }
1481 } 1480 }
1482 if (prev_bit == 1) { 1481 if (prev_bit == 1) {
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index b8acc07ac6c2..47270a3c9649 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -328,6 +328,7 @@ int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
328 const char *name, int name_len, 328 const char *name, int name_len,
329 u64 inode_objectid, u64 ref_objectid, u64 index) 329 u64 inode_objectid, u64 ref_objectid, u64 index)
330{ 330{
331 struct btrfs_fs_info *fs_info = root->fs_info;
331 struct btrfs_path *path; 332 struct btrfs_path *path;
332 struct btrfs_key key; 333 struct btrfs_key key;
333 struct btrfs_inode_ref *ref; 334 struct btrfs_inode_ref *ref;
@@ -384,7 +385,7 @@ out:
384 btrfs_free_path(path); 385 btrfs_free_path(path);
385 386
386 if (ret == -EMLINK) { 387 if (ret == -EMLINK) {
387 struct btrfs_super_block *disk_super = root->fs_info->super_copy; 388 struct btrfs_super_block *disk_super = fs_info->super_copy;
388 /* We ran out of space in the ref array. Need to 389 /* We ran out of space in the ref array. Need to
389 * add an extended ref. */ 390 * add an extended ref. */
390 if (btrfs_super_incompat_flags(disk_super) 391 if (btrfs_super_incompat_flags(disk_super)
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index 0c4926728b6a..79921f6fb8e5 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -38,7 +38,7 @@ static int caching_kthread(void *data)
38 int slot; 38 int slot;
39 int ret; 39 int ret;
40 40
41 if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE)) 41 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
42 return 0; 42 return 0;
43 43
44 path = btrfs_alloc_path(); 44 path = btrfs_alloc_path();
@@ -180,7 +180,7 @@ static void start_caching(struct btrfs_root *root)
180 if (IS_ERR(tsk)) { 180 if (IS_ERR(tsk)) {
181 btrfs_warn(fs_info, "failed to start inode caching task"); 181 btrfs_warn(fs_info, "failed to start inode caching task");
182 btrfs_clear_pending_and_info(fs_info, INODE_MAP_CACHE, 182 btrfs_clear_pending_and_info(fs_info, INODE_MAP_CACHE,
183 "disabling inode map caching"); 183 "disabling inode map caching");
184 } 184 }
185} 185}
186 186
@@ -395,6 +395,7 @@ void btrfs_init_free_ino_ctl(struct btrfs_root *root)
395int btrfs_save_ino_cache(struct btrfs_root *root, 395int btrfs_save_ino_cache(struct btrfs_root *root,
396 struct btrfs_trans_handle *trans) 396 struct btrfs_trans_handle *trans)
397{ 397{
398 struct btrfs_fs_info *fs_info = root->fs_info;
398 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl; 399 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
399 struct btrfs_path *path; 400 struct btrfs_path *path;
400 struct inode *inode; 401 struct inode *inode;
@@ -415,7 +416,7 @@ int btrfs_save_ino_cache(struct btrfs_root *root,
415 if (btrfs_root_refs(&root->root_item) == 0) 416 if (btrfs_root_refs(&root->root_item) == 0)
416 return 0; 417 return 0;
417 418
418 if (!btrfs_test_opt(root->fs_info, INODE_MAP_CACHE)) 419 if (!btrfs_test_opt(fs_info, INODE_MAP_CACHE))
419 return 0; 420 return 0;
420 421
421 path = btrfs_alloc_path(); 422 path = btrfs_alloc_path();
@@ -423,7 +424,7 @@ int btrfs_save_ino_cache(struct btrfs_root *root,
423 return -ENOMEM; 424 return -ENOMEM;
424 425
425 rsv = trans->block_rsv; 426 rsv = trans->block_rsv;
426 trans->block_rsv = &root->fs_info->trans_block_rsv; 427 trans->block_rsv = &fs_info->trans_block_rsv;
427 428
428 num_bytes = trans->bytes_reserved; 429 num_bytes = trans->bytes_reserved;
429 /* 430 /*
@@ -433,15 +434,14 @@ int btrfs_save_ino_cache(struct btrfs_root *root,
433 * 1 item for free space object 434 * 1 item for free space object
434 * 3 items for pre-allocation 435 * 3 items for pre-allocation
435 */ 436 */
436 trans->bytes_reserved = btrfs_calc_trans_metadata_size(root->fs_info, 437 trans->bytes_reserved = btrfs_calc_trans_metadata_size(fs_info, 10);
437 10);
438 ret = btrfs_block_rsv_add(root, trans->block_rsv, 438 ret = btrfs_block_rsv_add(root, trans->block_rsv,
439 trans->bytes_reserved, 439 trans->bytes_reserved,
440 BTRFS_RESERVE_NO_FLUSH); 440 BTRFS_RESERVE_NO_FLUSH);
441 if (ret) 441 if (ret)
442 goto out; 442 goto out;
443 trace_btrfs_space_reservation(root->fs_info, "ino_cache", 443 trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
444 trans->transid, trans->bytes_reserved, 1); 444 trans->bytes_reserved, 1);
445again: 445again:
446 inode = lookup_free_ino_inode(root, path); 446 inode = lookup_free_ino_inode(root, path);
447 if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) { 447 if (IS_ERR(inode) && (PTR_ERR(inode) != -ENOENT || retry)) {
@@ -507,8 +507,8 @@ again:
507out_put: 507out_put:
508 iput(inode); 508 iput(inode);
509out_release: 509out_release:
510 trace_btrfs_space_reservation(root->fs_info, "ino_cache", 510 trace_btrfs_space_reservation(fs_info, "ino_cache", trans->transid,
511 trans->transid, 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(root, trans->block_rsv, trans->bytes_reserved);
513out: 513out:
514 trans->block_rsv = rsv; 514 trans->block_rsv = rsv;
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index d0a236f5fddc..677762e0dcab 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -249,11 +249,12 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
249 int compress_type, 249 int compress_type,
250 struct page **compressed_pages) 250 struct page **compressed_pages)
251{ 251{
252 struct btrfs_fs_info *fs_info = root->fs_info;
252 struct btrfs_trans_handle *trans; 253 struct btrfs_trans_handle *trans;
253 u64 isize = i_size_read(inode); 254 u64 isize = i_size_read(inode);
254 u64 actual_end = min(end + 1, isize); 255 u64 actual_end = min(end + 1, isize);
255 u64 inline_len = actual_end - start; 256 u64 inline_len = actual_end - start;
256 u64 aligned_end = ALIGN(end, root->fs_info->sectorsize); 257 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
257 u64 data_len = inline_len; 258 u64 data_len = inline_len;
258 int ret; 259 int ret;
259 struct btrfs_path *path; 260 struct btrfs_path *path;
@@ -264,12 +265,12 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
264 data_len = compressed_size; 265 data_len = compressed_size;
265 266
266 if (start > 0 || 267 if (start > 0 ||
267 actual_end > root->fs_info->sectorsize || 268 actual_end > fs_info->sectorsize ||
268 data_len > BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info) || 269 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
269 (!compressed_size && 270 (!compressed_size &&
270 (actual_end & (root->fs_info->sectorsize - 1)) == 0) || 271 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
271 end + 1 < isize || 272 end + 1 < isize ||
272 data_len > root->fs_info->max_inline) { 273 data_len > fs_info->max_inline) {
273 return 1; 274 return 1;
274 } 275 }
275 276
@@ -282,7 +283,7 @@ static noinline int cow_file_range_inline(struct btrfs_root *root,
282 btrfs_free_path(path); 283 btrfs_free_path(path);
283 return PTR_ERR(trans); 284 return PTR_ERR(trans);
284 } 285 }
285 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 286 trans->block_rsv = &fs_info->delalloc_block_rsv;
286 287
287 if (compressed_size && compressed_pages) 288 if (compressed_size && compressed_pages)
288 extent_item_size = btrfs_file_extent_calc_inline_size( 289 extent_item_size = btrfs_file_extent_calc_inline_size(
@@ -372,15 +373,15 @@ static noinline int add_async_extent(struct async_cow *cow,
372 373
373static inline int inode_need_compress(struct inode *inode) 374static inline int inode_need_compress(struct inode *inode)
374{ 375{
375 struct btrfs_root *root = BTRFS_I(inode)->root; 376 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
376 377
377 /* force compress */ 378 /* force compress */
378 if (btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) 379 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
379 return 1; 380 return 1;
380 /* bad compression ratios */ 381 /* bad compression ratios */
381 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) 382 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
382 return 0; 383 return 0;
383 if (btrfs_test_opt(root->fs_info, COMPRESS) || 384 if (btrfs_test_opt(fs_info, COMPRESS) ||
384 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || 385 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
385 BTRFS_I(inode)->force_compress) 386 BTRFS_I(inode)->force_compress)
386 return 1; 387 return 1;
@@ -410,9 +411,10 @@ static noinline void compress_file_range(struct inode *inode,
410 struct async_cow *async_cow, 411 struct async_cow *async_cow,
411 int *num_added) 412 int *num_added)
412{ 413{
414 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
413 struct btrfs_root *root = BTRFS_I(inode)->root; 415 struct btrfs_root *root = BTRFS_I(inode)->root;
414 u64 num_bytes; 416 u64 num_bytes;
415 u64 blocksize = root->fs_info->sectorsize; 417 u64 blocksize = fs_info->sectorsize;
416 u64 actual_end; 418 u64 actual_end;
417 u64 isize = i_size_read(inode); 419 u64 isize = i_size_read(inode);
418 int ret = 0; 420 int ret = 0;
@@ -425,7 +427,7 @@ static noinline void compress_file_range(struct inode *inode,
425 unsigned long max_uncompressed = SZ_128K; 427 unsigned long max_uncompressed = SZ_128K;
426 int i; 428 int i;
427 int will_compress; 429 int will_compress;
428 int compress_type = root->fs_info->compress_type; 430 int compress_type = fs_info->compress_type;
429 int redirty = 0; 431 int redirty = 0;
430 432
431 /* if this is a small write inside eof, kick off a defrag */ 433 /* if this is a small write inside eof, kick off a defrag */
@@ -624,7 +626,7 @@ cont:
624 nr_pages_ret = 0; 626 nr_pages_ret = 0;
625 627
626 /* flag the file so we don't compress in the future */ 628 /* flag the file so we don't compress in the future */
627 if (!btrfs_test_opt(root->fs_info, FORCE_COMPRESS) && 629 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
628 !(BTRFS_I(inode)->force_compress)) { 630 !(BTRFS_I(inode)->force_compress)) {
629 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; 631 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
630 } 632 }
@@ -682,6 +684,7 @@ static void free_async_extent_pages(struct async_extent *async_extent)
682static noinline void submit_compressed_extents(struct inode *inode, 684static noinline void submit_compressed_extents(struct inode *inode,
683 struct async_cow *async_cow) 685 struct async_cow *async_cow)
684{ 686{
687 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
685 struct async_extent *async_extent; 688 struct async_extent *async_extent;
686 u64 alloc_hint = 0; 689 u64 alloc_hint = 0;
687 struct btrfs_key ins; 690 struct btrfs_key ins;
@@ -794,7 +797,7 @@ retry:
794 em->block_len = ins.offset; 797 em->block_len = ins.offset;
795 em->orig_block_len = ins.offset; 798 em->orig_block_len = ins.offset;
796 em->ram_bytes = async_extent->ram_size; 799 em->ram_bytes = async_extent->ram_size;
797 em->bdev = root->fs_info->fs_devices->latest_bdev; 800 em->bdev = fs_info->fs_devices->latest_bdev;
798 em->compress_type = async_extent->compress_type; 801 em->compress_type = async_extent->compress_type;
799 set_bit(EXTENT_FLAG_PINNED, &em->flags); 802 set_bit(EXTENT_FLAG_PINNED, &em->flags);
800 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags); 803 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
@@ -829,7 +832,7 @@ retry:
829 async_extent->ram_size - 1, 0); 832 async_extent->ram_size - 1, 0);
830 goto out_free_reserve; 833 goto out_free_reserve;
831 } 834 }
832 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); 835 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
833 836
834 /* 837 /*
835 * clear dirty, set writeback and unlock the pages. 838 * clear dirty, set writeback and unlock the pages.
@@ -870,7 +873,7 @@ retry:
870 } 873 }
871 return; 874 return;
872out_free_reserve: 875out_free_reserve:
873 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); 876 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
874 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 877 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
875out_free: 878out_free:
876 extent_clear_unlock_delalloc(inode, async_extent->start, 879 extent_clear_unlock_delalloc(inode, async_extent->start,
@@ -939,13 +942,14 @@ static noinline int cow_file_range(struct inode *inode,
939 int *page_started, unsigned long *nr_written, 942 int *page_started, unsigned long *nr_written,
940 int unlock, struct btrfs_dedupe_hash *hash) 943 int unlock, struct btrfs_dedupe_hash *hash)
941{ 944{
945 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
942 struct btrfs_root *root = BTRFS_I(inode)->root; 946 struct btrfs_root *root = BTRFS_I(inode)->root;
943 u64 alloc_hint = 0; 947 u64 alloc_hint = 0;
944 u64 num_bytes; 948 u64 num_bytes;
945 unsigned long ram_size; 949 unsigned long ram_size;
946 u64 disk_num_bytes; 950 u64 disk_num_bytes;
947 u64 cur_alloc_size; 951 u64 cur_alloc_size;
948 u64 blocksize = root->fs_info->sectorsize; 952 u64 blocksize = fs_info->sectorsize;
949 struct btrfs_key ins; 953 struct btrfs_key ins;
950 struct extent_map *em; 954 struct extent_map *em;
951 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 955 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
@@ -989,7 +993,7 @@ static noinline int cow_file_range(struct inode *inode,
989 } 993 }
990 994
991 BUG_ON(disk_num_bytes > 995 BUG_ON(disk_num_bytes >
992 btrfs_super_total_bytes(root->fs_info->super_copy)); 996 btrfs_super_total_bytes(fs_info->super_copy));
993 997
994 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes); 998 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
995 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0); 999 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
@@ -999,8 +1003,7 @@ static noinline int cow_file_range(struct inode *inode,
999 1003
1000 cur_alloc_size = disk_num_bytes; 1004 cur_alloc_size = disk_num_bytes;
1001 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size, 1005 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1002 root->fs_info->sectorsize, 0, 1006 fs_info->sectorsize, 0, alloc_hint,
1003 alloc_hint,
1004 &ins, 1, 1); 1007 &ins, 1, 1);
1005 if (ret < 0) 1008 if (ret < 0)
1006 goto out_unlock; 1009 goto out_unlock;
@@ -1021,7 +1024,7 @@ static noinline int cow_file_range(struct inode *inode,
1021 em->block_len = ins.offset; 1024 em->block_len = ins.offset;
1022 em->orig_block_len = ins.offset; 1025 em->orig_block_len = ins.offset;
1023 em->ram_bytes = ram_size; 1026 em->ram_bytes = ram_size;
1024 em->bdev = root->fs_info->fs_devices->latest_bdev; 1027 em->bdev = fs_info->fs_devices->latest_bdev;
1025 set_bit(EXTENT_FLAG_PINNED, &em->flags); 1028 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1026 em->generation = -1; 1029 em->generation = -1;
1027 1030
@@ -1053,7 +1056,7 @@ static noinline int cow_file_range(struct inode *inode,
1053 goto out_drop_extent_cache; 1056 goto out_drop_extent_cache;
1054 } 1057 }
1055 1058
1056 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); 1059 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1057 1060
1058 if (disk_num_bytes < cur_alloc_size) 1061 if (disk_num_bytes < cur_alloc_size)
1059 break; 1062 break;
@@ -1084,7 +1087,7 @@ out:
1084out_drop_extent_cache: 1087out_drop_extent_cache:
1085 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0); 1088 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1086out_reserve: 1089out_reserve:
1087 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); 1090 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1088 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 1091 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
1089out_unlock: 1092out_unlock:
1090 extent_clear_unlock_delalloc(inode, start, end, delalloc_end, 1093 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
@@ -1119,6 +1122,7 @@ static noinline void async_cow_start(struct btrfs_work *work)
1119 */ 1122 */
1120static noinline void async_cow_submit(struct btrfs_work *work) 1123static noinline void async_cow_submit(struct btrfs_work *work)
1121{ 1124{
1125 struct btrfs_fs_info *fs_info;
1122 struct async_cow *async_cow; 1126 struct async_cow *async_cow;
1123 struct btrfs_root *root; 1127 struct btrfs_root *root;
1124 unsigned long nr_pages; 1128 unsigned long nr_pages;
@@ -1126,16 +1130,17 @@ static noinline void async_cow_submit(struct btrfs_work *work)
1126 async_cow = container_of(work, struct async_cow, work); 1130 async_cow = container_of(work, struct async_cow, work);
1127 1131
1128 root = async_cow->root; 1132 root = async_cow->root;
1133 fs_info = root->fs_info;
1129 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >> 1134 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1130 PAGE_SHIFT; 1135 PAGE_SHIFT;
1131 1136
1132 /* 1137 /*
1133 * atomic_sub_return implies a barrier for waitqueue_active 1138 * atomic_sub_return implies a barrier for waitqueue_active
1134 */ 1139 */
1135 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) < 1140 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1136 5 * SZ_1M && 1141 5 * SZ_1M &&
1137 waitqueue_active(&root->fs_info->async_submit_wait)) 1142 waitqueue_active(&fs_info->async_submit_wait))
1138 wake_up(&root->fs_info->async_submit_wait); 1143 wake_up(&fs_info->async_submit_wait);
1139 1144
1140 if (async_cow->inode) 1145 if (async_cow->inode)
1141 submit_compressed_extents(async_cow->inode, async_cow); 1146 submit_compressed_extents(async_cow->inode, async_cow);
@@ -1154,6 +1159,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1154 u64 start, u64 end, int *page_started, 1159 u64 start, u64 end, int *page_started,
1155 unsigned long *nr_written) 1160 unsigned long *nr_written)
1156{ 1161{
1162 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1157 struct async_cow *async_cow; 1163 struct async_cow *async_cow;
1158 struct btrfs_root *root = BTRFS_I(inode)->root; 1164 struct btrfs_root *root = BTRFS_I(inode)->root;
1159 unsigned long nr_pages; 1165 unsigned long nr_pages;
@@ -1171,7 +1177,7 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1171 async_cow->start = start; 1177 async_cow->start = start;
1172 1178
1173 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS && 1179 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1174 !btrfs_test_opt(root->fs_info, FORCE_COMPRESS)) 1180 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1175 cur_end = end; 1181 cur_end = end;
1176 else 1182 else
1177 cur_end = min(end, start + SZ_512K - 1); 1183 cur_end = min(end, start + SZ_512K - 1);
@@ -1186,22 +1192,21 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1186 1192
1187 nr_pages = (cur_end - start + PAGE_SIZE) >> 1193 nr_pages = (cur_end - start + PAGE_SIZE) >>
1188 PAGE_SHIFT; 1194 PAGE_SHIFT;
1189 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages); 1195 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1190 1196
1191 btrfs_queue_work(root->fs_info->delalloc_workers, 1197 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1192 &async_cow->work);
1193 1198
1194 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) { 1199 if (atomic_read(&fs_info->async_delalloc_pages) > limit) {
1195 wait_event(root->fs_info->async_submit_wait, 1200 wait_event(fs_info->async_submit_wait,
1196 (atomic_read(&root->fs_info->async_delalloc_pages) < 1201 (atomic_read(&fs_info->async_delalloc_pages) <
1197 limit)); 1202 limit));
1198 } 1203 }
1199 1204
1200 while (atomic_read(&root->fs_info->async_submit_draining) && 1205 while (atomic_read(&fs_info->async_submit_draining) &&
1201 atomic_read(&root->fs_info->async_delalloc_pages)) { 1206 atomic_read(&fs_info->async_delalloc_pages)) {
1202 wait_event(root->fs_info->async_submit_wait, 1207 wait_event(fs_info->async_submit_wait,
1203 (atomic_read(&root->fs_info->async_delalloc_pages) == 1208 (atomic_read(&fs_info->async_delalloc_pages) ==
1204 0)); 1209 0));
1205 } 1210 }
1206 1211
1207 *nr_written += nr_pages; 1212 *nr_written += nr_pages;
@@ -1214,11 +1219,12 @@ static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1214static noinline int csum_exist_in_range(struct btrfs_root *root, 1219static noinline int csum_exist_in_range(struct btrfs_root *root,
1215 u64 bytenr, u64 num_bytes) 1220 u64 bytenr, u64 num_bytes)
1216{ 1221{
1222 struct btrfs_fs_info *fs_info = root->fs_info;
1217 int ret; 1223 int ret;
1218 struct btrfs_ordered_sum *sums; 1224 struct btrfs_ordered_sum *sums;
1219 LIST_HEAD(list); 1225 LIST_HEAD(list);
1220 1226
1221 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr, 1227 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1222 bytenr + num_bytes - 1, &list, 0); 1228 bytenr + num_bytes - 1, &list, 0);
1223 if (ret == 0 && list_empty(&list)) 1229 if (ret == 0 && list_empty(&list))
1224 return 0; 1230 return 0;
@@ -1243,6 +1249,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1243 u64 start, u64 end, int *page_started, int force, 1249 u64 start, u64 end, int *page_started, int force,
1244 unsigned long *nr_written) 1250 unsigned long *nr_written)
1245{ 1251{
1252 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1246 struct btrfs_root *root = BTRFS_I(inode)->root; 1253 struct btrfs_root *root = BTRFS_I(inode)->root;
1247 struct btrfs_trans_handle *trans; 1254 struct btrfs_trans_handle *trans;
1248 struct extent_buffer *leaf; 1255 struct extent_buffer *leaf;
@@ -1298,7 +1305,7 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1298 return PTR_ERR(trans); 1305 return PTR_ERR(trans);
1299 } 1306 }
1300 1307
1301 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1308 trans->block_rsv = &fs_info->delalloc_block_rsv;
1302 1309
1303 cow_start = (u64)-1; 1310 cow_start = (u64)-1;
1304 cur_offset = start; 1311 cur_offset = start;
@@ -1399,8 +1406,7 @@ next_slot:
1399 */ 1406 */
1400 if (csum_exist_in_range(root, disk_bytenr, num_bytes)) 1407 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1401 goto out_check; 1408 goto out_check;
1402 if (!btrfs_inc_nocow_writers(root->fs_info, 1409 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1403 disk_bytenr))
1404 goto out_check; 1410 goto out_check;
1405 nocow = 1; 1411 nocow = 1;
1406 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { 1412 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
@@ -1408,7 +1414,7 @@ next_slot:
1408 btrfs_file_extent_inline_len(leaf, 1414 btrfs_file_extent_inline_len(leaf,
1409 path->slots[0], fi); 1415 path->slots[0], fi);
1410 extent_end = ALIGN(extent_end, 1416 extent_end = ALIGN(extent_end,
1411 root->fs_info->sectorsize); 1417 fs_info->sectorsize);
1412 } else { 1418 } else {
1413 BUG_ON(1); 1419 BUG_ON(1);
1414 } 1420 }
@@ -1418,8 +1424,7 @@ out_check:
1418 if (!nolock && nocow) 1424 if (!nolock && nocow)
1419 btrfs_end_write_no_snapshoting(root); 1425 btrfs_end_write_no_snapshoting(root);
1420 if (nocow) 1426 if (nocow)
1421 btrfs_dec_nocow_writers(root->fs_info, 1427 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1422 disk_bytenr);
1423 goto next_slot; 1428 goto next_slot;
1424 } 1429 }
1425 if (!nocow) { 1430 if (!nocow) {
@@ -1442,7 +1447,7 @@ out_check:
1442 if (!nolock && nocow) 1447 if (!nolock && nocow)
1443 btrfs_end_write_no_snapshoting(root); 1448 btrfs_end_write_no_snapshoting(root);
1444 if (nocow) 1449 if (nocow)
1445 btrfs_dec_nocow_writers(root->fs_info, 1450 btrfs_dec_nocow_writers(fs_info,
1446 disk_bytenr); 1451 disk_bytenr);
1447 goto error; 1452 goto error;
1448 } 1453 }
@@ -1462,7 +1467,7 @@ out_check:
1462 em->block_start = disk_bytenr; 1467 em->block_start = disk_bytenr;
1463 em->orig_block_len = disk_num_bytes; 1468 em->orig_block_len = disk_num_bytes;
1464 em->ram_bytes = ram_bytes; 1469 em->ram_bytes = ram_bytes;
1465 em->bdev = root->fs_info->fs_devices->latest_bdev; 1470 em->bdev = fs_info->fs_devices->latest_bdev;
1466 em->mod_start = em->start; 1471 em->mod_start = em->start;
1467 em->mod_len = em->len; 1472 em->mod_len = em->len;
1468 set_bit(EXTENT_FLAG_PINNED, &em->flags); 1473 set_bit(EXTENT_FLAG_PINNED, &em->flags);
@@ -1487,7 +1492,7 @@ out_check:
1487 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr, 1492 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1488 num_bytes, num_bytes, type); 1493 num_bytes, num_bytes, type);
1489 if (nocow) 1494 if (nocow)
1490 btrfs_dec_nocow_writers(root->fs_info, disk_bytenr); 1495 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1491 BUG_ON(ret); /* -ENOMEM */ 1496 BUG_ON(ret); /* -ENOMEM */
1492 1497
1493 if (root->root_key.objectid == 1498 if (root->root_key.objectid ==
@@ -1694,6 +1699,8 @@ static void btrfs_merge_extent_hook(struct inode *inode,
1694static void btrfs_add_delalloc_inodes(struct btrfs_root *root, 1699static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1695 struct inode *inode) 1700 struct inode *inode)
1696{ 1701{
1702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1703
1697 spin_lock(&root->delalloc_lock); 1704 spin_lock(&root->delalloc_lock);
1698 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { 1705 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1699 list_add_tail(&BTRFS_I(inode)->delalloc_inodes, 1706 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
@@ -1702,11 +1709,11 @@ static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1702 &BTRFS_I(inode)->runtime_flags); 1709 &BTRFS_I(inode)->runtime_flags);
1703 root->nr_delalloc_inodes++; 1710 root->nr_delalloc_inodes++;
1704 if (root->nr_delalloc_inodes == 1) { 1711 if (root->nr_delalloc_inodes == 1) {
1705 spin_lock(&root->fs_info->delalloc_root_lock); 1712 spin_lock(&fs_info->delalloc_root_lock);
1706 BUG_ON(!list_empty(&root->delalloc_root)); 1713 BUG_ON(!list_empty(&root->delalloc_root));
1707 list_add_tail(&root->delalloc_root, 1714 list_add_tail(&root->delalloc_root,
1708 &root->fs_info->delalloc_roots); 1715 &fs_info->delalloc_roots);
1709 spin_unlock(&root->fs_info->delalloc_root_lock); 1716 spin_unlock(&fs_info->delalloc_root_lock);
1710 } 1717 }
1711 } 1718 }
1712 spin_unlock(&root->delalloc_lock); 1719 spin_unlock(&root->delalloc_lock);
@@ -1715,6 +1722,8 @@ static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1715static void btrfs_del_delalloc_inode(struct btrfs_root *root, 1722static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1716 struct inode *inode) 1723 struct inode *inode)
1717{ 1724{
1725 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1726
1718 spin_lock(&root->delalloc_lock); 1727 spin_lock(&root->delalloc_lock);
1719 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) { 1728 if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1720 list_del_init(&BTRFS_I(inode)->delalloc_inodes); 1729 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
@@ -1722,10 +1731,10 @@ static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1722 &BTRFS_I(inode)->runtime_flags); 1731 &BTRFS_I(inode)->runtime_flags);
1723 root->nr_delalloc_inodes--; 1732 root->nr_delalloc_inodes--;
1724 if (!root->nr_delalloc_inodes) { 1733 if (!root->nr_delalloc_inodes) {
1725 spin_lock(&root->fs_info->delalloc_root_lock); 1734 spin_lock(&fs_info->delalloc_root_lock);
1726 BUG_ON(list_empty(&root->delalloc_root)); 1735 BUG_ON(list_empty(&root->delalloc_root));
1727 list_del_init(&root->delalloc_root); 1736 list_del_init(&root->delalloc_root);
1728 spin_unlock(&root->fs_info->delalloc_root_lock); 1737 spin_unlock(&fs_info->delalloc_root_lock);
1729 } 1738 }
1730 } 1739 }
1731 spin_unlock(&root->delalloc_lock); 1740 spin_unlock(&root->delalloc_lock);
@@ -1740,6 +1749,8 @@ static void btrfs_set_bit_hook(struct inode *inode,
1740 struct extent_state *state, unsigned *bits) 1749 struct extent_state *state, unsigned *bits)
1741{ 1750{
1742 1751
1752 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1753
1743 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC)) 1754 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1744 WARN_ON(1); 1755 WARN_ON(1);
1745 /* 1756 /*
@@ -1761,11 +1772,11 @@ static void btrfs_set_bit_hook(struct inode *inode,
1761 } 1772 }
1762 1773
1763 /* For sanity tests */ 1774 /* For sanity tests */
1764 if (btrfs_is_testing(root->fs_info)) 1775 if (btrfs_is_testing(fs_info))
1765 return; 1776 return;
1766 1777
1767 __percpu_counter_add(&root->fs_info->delalloc_bytes, len, 1778 __percpu_counter_add(&fs_info->delalloc_bytes, len,
1768 root->fs_info->delalloc_batch); 1779 fs_info->delalloc_batch);
1769 spin_lock(&BTRFS_I(inode)->lock); 1780 spin_lock(&BTRFS_I(inode)->lock);
1770 BTRFS_I(inode)->delalloc_bytes += len; 1781 BTRFS_I(inode)->delalloc_bytes += len;
1771 if (*bits & EXTENT_DEFRAG) 1782 if (*bits & EXTENT_DEFRAG)
@@ -1784,6 +1795,7 @@ static void btrfs_clear_bit_hook(struct inode *inode,
1784 struct extent_state *state, 1795 struct extent_state *state,
1785 unsigned *bits) 1796 unsigned *bits)
1786{ 1797{
1798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1787 u64 len = state->end + 1 - state->start; 1799 u64 len = state->end + 1 - state->start;
1788 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, 1800 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1789 BTRFS_MAX_EXTENT_SIZE); 1801 BTRFS_MAX_EXTENT_SIZE);
@@ -1816,11 +1828,11 @@ static void btrfs_clear_bit_hook(struct inode *inode,
1816 * error. 1828 * error.
1817 */ 1829 */
1818 if (*bits & EXTENT_DO_ACCOUNTING && 1830 if (*bits & EXTENT_DO_ACCOUNTING &&
1819 root != root->fs_info->tree_root) 1831 root != fs_info->tree_root)
1820 btrfs_delalloc_release_metadata(inode, len); 1832 btrfs_delalloc_release_metadata(inode, len);
1821 1833
1822 /* For sanity tests. */ 1834 /* For sanity tests. */
1823 if (btrfs_is_testing(root->fs_info)) 1835 if (btrfs_is_testing(fs_info))
1824 return; 1836 return;
1825 1837
1826 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID 1838 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
@@ -1830,8 +1842,8 @@ static void btrfs_clear_bit_hook(struct inode *inode,
1830 btrfs_free_reserved_data_space_noquota(inode, 1842 btrfs_free_reserved_data_space_noquota(inode,
1831 state->start, len); 1843 state->start, len);
1832 1844
1833 __percpu_counter_add(&root->fs_info->delalloc_bytes, -len, 1845 __percpu_counter_add(&fs_info->delalloc_bytes, -len,
1834 root->fs_info->delalloc_batch); 1846 fs_info->delalloc_batch);
1835 spin_lock(&BTRFS_I(inode)->lock); 1847 spin_lock(&BTRFS_I(inode)->lock);
1836 BTRFS_I(inode)->delalloc_bytes -= len; 1848 BTRFS_I(inode)->delalloc_bytes -= len;
1837 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 && 1849 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
@@ -1854,7 +1866,8 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1854 size_t size, struct bio *bio, 1866 size_t size, struct bio *bio,
1855 unsigned long bio_flags) 1867 unsigned long bio_flags)
1856{ 1868{
1857 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; 1869 struct inode *inode = page->mapping->host;
1870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1858 u64 logical = (u64)bio->bi_iter.bi_sector << 9; 1871 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1859 u64 length = 0; 1872 u64 length = 0;
1860 u64 map_length; 1873 u64 map_length;
@@ -1865,8 +1878,8 @@ int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1865 1878
1866 length = bio->bi_iter.bi_size; 1879 length = bio->bi_iter.bi_size;
1867 map_length = length; 1880 map_length = length;
1868 ret = btrfs_map_block(root->fs_info, btrfs_op(bio), logical, 1881 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1869 &map_length, NULL, 0); 1882 NULL, 0);
1870 if (ret < 0) 1883 if (ret < 0)
1871 return ret; 1884 return ret;
1872 if (map_length < length + size) 1885 if (map_length < length + size)
@@ -1925,6 +1938,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1925 int mirror_num, unsigned long bio_flags, 1938 int mirror_num, unsigned long bio_flags,
1926 u64 bio_offset) 1939 u64 bio_offset)
1927{ 1940{
1941 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1928 struct btrfs_root *root = BTRFS_I(inode)->root; 1942 struct btrfs_root *root = BTRFS_I(inode)->root;
1929 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA; 1943 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1930 int ret = 0; 1944 int ret = 0;
@@ -1937,7 +1951,7 @@ static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1937 metadata = BTRFS_WQ_ENDIO_FREE_SPACE; 1951 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1938 1952
1939 if (bio_op(bio) != REQ_OP_WRITE) { 1953 if (bio_op(bio) != REQ_OP_WRITE) {
1940 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata); 1954 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
1941 if (ret) 1955 if (ret)
1942 goto out; 1956 goto out;
1943 1957
@@ -1957,11 +1971,10 @@ static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1957 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) 1971 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1958 goto mapit; 1972 goto mapit;
1959 /* we're doing a write, do the async checksumming */ 1973 /* we're doing a write, do the async checksumming */
1960 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, 1974 ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1961 inode, bio, mirror_num, 1975 bio_flags, bio_offset,
1962 bio_flags, bio_offset, 1976 __btrfs_submit_bio_start,
1963 __btrfs_submit_bio_start, 1977 __btrfs_submit_bio_done);
1964 __btrfs_submit_bio_done);
1965 goto out; 1978 goto out;
1966 } else if (!skip_sum) { 1979 } else if (!skip_sum) {
1967 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0); 1980 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
@@ -2091,8 +2104,8 @@ out_page:
2091static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) 2104static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2092{ 2105{
2093 struct inode *inode = page->mapping->host; 2106 struct inode *inode = page->mapping->host;
2107 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2094 struct btrfs_writepage_fixup *fixup; 2108 struct btrfs_writepage_fixup *fixup;
2095 struct btrfs_root *root = BTRFS_I(inode)->root;
2096 2109
2097 /* this page is properly in the ordered list */ 2110 /* this page is properly in the ordered list */
2098 if (TestClearPagePrivate2(page)) 2111 if (TestClearPagePrivate2(page))
@@ -2110,7 +2123,7 @@ static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2110 btrfs_init_work(&fixup->work, btrfs_fixup_helper, 2123 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2111 btrfs_writepage_fixup_worker, NULL, NULL); 2124 btrfs_writepage_fixup_worker, NULL, NULL);
2112 fixup->page = page; 2125 fixup->page = page;
2113 btrfs_queue_work(root->fs_info->fixup_workers, &fixup->work); 2126 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2114 return -EBUSY; 2127 return -EBUSY;
2115} 2128}
2116 2129
@@ -2294,7 +2307,6 @@ static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2294 void *ctx) 2307 void *ctx)
2295{ 2308{
2296 struct btrfs_file_extent_item *extent; 2309 struct btrfs_file_extent_item *extent;
2297 struct btrfs_fs_info *fs_info;
2298 struct old_sa_defrag_extent *old = ctx; 2310 struct old_sa_defrag_extent *old = ctx;
2299 struct new_sa_defrag_extent *new = old->new; 2311 struct new_sa_defrag_extent *new = old->new;
2300 struct btrfs_path *path = new->path; 2312 struct btrfs_path *path = new->path;
@@ -2303,6 +2315,7 @@ static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2303 struct sa_defrag_extent_backref *backref; 2315 struct sa_defrag_extent_backref *backref;
2304 struct extent_buffer *leaf; 2316 struct extent_buffer *leaf;
2305 struct inode *inode = new->inode; 2317 struct inode *inode = new->inode;
2318 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2306 int slot; 2319 int slot;
2307 int ret; 2320 int ret;
2308 u64 extent_offset; 2321 u64 extent_offset;
@@ -2316,7 +2329,6 @@ static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2316 key.type = BTRFS_ROOT_ITEM_KEY; 2329 key.type = BTRFS_ROOT_ITEM_KEY;
2317 key.offset = (u64)-1; 2330 key.offset = (u64)-1;
2318 2331
2319 fs_info = BTRFS_I(inode)->root->fs_info;
2320 root = btrfs_read_fs_root_no_name(fs_info, &key); 2332 root = btrfs_read_fs_root_no_name(fs_info, &key);
2321 if (IS_ERR(root)) { 2333 if (IS_ERR(root)) {
2322 if (PTR_ERR(root) == -ENOENT) 2334 if (PTR_ERR(root) == -ENOENT)
@@ -2414,7 +2426,7 @@ out:
2414static noinline bool record_extent_backrefs(struct btrfs_path *path, 2426static noinline bool record_extent_backrefs(struct btrfs_path *path,
2415 struct new_sa_defrag_extent *new) 2427 struct new_sa_defrag_extent *new)
2416{ 2428{
2417 struct btrfs_fs_info *fs_info = BTRFS_I(new->inode)->root->fs_info; 2429 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2418 struct old_sa_defrag_extent *old, *tmp; 2430 struct old_sa_defrag_extent *old, *tmp;
2419 int ret; 2431 int ret;
2420 2432
@@ -2472,13 +2484,12 @@ static noinline int relink_extent_backref(struct btrfs_path *path,
2472 struct btrfs_file_extent_item *item; 2484 struct btrfs_file_extent_item *item;
2473 struct btrfs_ordered_extent *ordered; 2485 struct btrfs_ordered_extent *ordered;
2474 struct btrfs_trans_handle *trans; 2486 struct btrfs_trans_handle *trans;
2475 struct btrfs_fs_info *fs_info;
2476 struct btrfs_root *root; 2487 struct btrfs_root *root;
2477 struct btrfs_key key; 2488 struct btrfs_key key;
2478 struct extent_buffer *leaf; 2489 struct extent_buffer *leaf;
2479 struct old_sa_defrag_extent *old = backref->old; 2490 struct old_sa_defrag_extent *old = backref->old;
2480 struct new_sa_defrag_extent *new = old->new; 2491 struct new_sa_defrag_extent *new = old->new;
2481 struct inode *src_inode = new->inode; 2492 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2482 struct inode *inode; 2493 struct inode *inode;
2483 struct extent_state *cached = NULL; 2494 struct extent_state *cached = NULL;
2484 int ret = 0; 2495 int ret = 0;
@@ -2499,7 +2510,6 @@ static noinline int relink_extent_backref(struct btrfs_path *path,
2499 key.type = BTRFS_ROOT_ITEM_KEY; 2510 key.type = BTRFS_ROOT_ITEM_KEY;
2500 key.offset = (u64)-1; 2511 key.offset = (u64)-1;
2501 2512
2502 fs_info = BTRFS_I(src_inode)->root->fs_info;
2503 index = srcu_read_lock(&fs_info->subvol_srcu); 2513 index = srcu_read_lock(&fs_info->subvol_srcu);
2504 2514
2505 root = btrfs_read_fs_root_no_name(fs_info, &key); 2515 root = btrfs_read_fs_root_no_name(fs_info, &key);
@@ -2680,6 +2690,7 @@ static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2680 2690
2681static void relink_file_extents(struct new_sa_defrag_extent *new) 2691static void relink_file_extents(struct new_sa_defrag_extent *new)
2682{ 2692{
2693 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2683 struct btrfs_path *path; 2694 struct btrfs_path *path;
2684 struct sa_defrag_extent_backref *backref; 2695 struct sa_defrag_extent_backref *backref;
2685 struct sa_defrag_extent_backref *prev = NULL; 2696 struct sa_defrag_extent_backref *prev = NULL;
@@ -2726,14 +2737,15 @@ static void relink_file_extents(struct new_sa_defrag_extent *new)
2726out: 2737out:
2727 free_sa_defrag_extent(new); 2738 free_sa_defrag_extent(new);
2728 2739
2729 atomic_dec(&root->fs_info->defrag_running); 2740 atomic_dec(&fs_info->defrag_running);
2730 wake_up(&root->fs_info->transaction_wait); 2741 wake_up(&fs_info->transaction_wait);
2731} 2742}
2732 2743
2733static struct new_sa_defrag_extent * 2744static struct new_sa_defrag_extent *
2734record_old_file_extents(struct inode *inode, 2745record_old_file_extents(struct inode *inode,
2735 struct btrfs_ordered_extent *ordered) 2746 struct btrfs_ordered_extent *ordered)
2736{ 2747{
2748 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2737 struct btrfs_root *root = BTRFS_I(inode)->root; 2749 struct btrfs_root *root = BTRFS_I(inode)->root;
2738 struct btrfs_path *path; 2750 struct btrfs_path *path;
2739 struct btrfs_key key; 2751 struct btrfs_key key;
@@ -2832,7 +2844,7 @@ next:
2832 } 2844 }
2833 2845
2834 btrfs_free_path(path); 2846 btrfs_free_path(path);
2835 atomic_inc(&root->fs_info->defrag_running); 2847 atomic_inc(&fs_info->defrag_running);
2836 2848
2837 return new; 2849 return new;
2838 2850
@@ -2846,9 +2858,10 @@ out_kfree:
2846static void btrfs_release_delalloc_bytes(struct btrfs_root *root, 2858static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2847 u64 start, u64 len) 2859 u64 start, u64 len)
2848{ 2860{
2861 struct btrfs_fs_info *fs_info = root->fs_info;
2849 struct btrfs_block_group_cache *cache; 2862 struct btrfs_block_group_cache *cache;
2850 2863
2851 cache = btrfs_lookup_block_group(root->fs_info, start); 2864 cache = btrfs_lookup_block_group(fs_info, start);
2852 ASSERT(cache); 2865 ASSERT(cache);
2853 2866
2854 spin_lock(&cache->lock); 2867 spin_lock(&cache->lock);
@@ -2865,6 +2878,7 @@ static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2865static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent) 2878static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2866{ 2879{
2867 struct inode *inode = ordered_extent->inode; 2880 struct inode *inode = ordered_extent->inode;
2881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2868 struct btrfs_root *root = BTRFS_I(inode)->root; 2882 struct btrfs_root *root = BTRFS_I(inode)->root;
2869 struct btrfs_trans_handle *trans = NULL; 2883 struct btrfs_trans_handle *trans = NULL;
2870 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 2884 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
@@ -2915,7 +2929,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2915 trans = NULL; 2929 trans = NULL;
2916 goto out; 2930 goto out;
2917 } 2931 }
2918 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 2932 trans->block_rsv = &fs_info->delalloc_block_rsv;
2919 ret = btrfs_update_inode_fallback(trans, root, inode); 2933 ret = btrfs_update_inode_fallback(trans, root, inode);
2920 if (ret) /* -ENOMEM or corruption */ 2934 if (ret) /* -ENOMEM or corruption */
2921 btrfs_abort_transaction(trans, ret); 2935 btrfs_abort_transaction(trans, ret);
@@ -2950,7 +2964,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2950 goto out_unlock; 2964 goto out_unlock;
2951 } 2965 }
2952 2966
2953 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 2967 trans->block_rsv = &fs_info->delalloc_block_rsv;
2954 2968
2955 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) 2969 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2956 compress_type = ordered_extent->compress_type; 2970 compress_type = ordered_extent->compress_type;
@@ -2961,7 +2975,7 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2961 ordered_extent->file_offset + 2975 ordered_extent->file_offset +
2962 logical_len); 2976 logical_len);
2963 } else { 2977 } else {
2964 BUG_ON(root == root->fs_info->tree_root); 2978 BUG_ON(root == fs_info->tree_root);
2965 ret = insert_reserved_file_extent(trans, inode, 2979 ret = insert_reserved_file_extent(trans, inode,
2966 ordered_extent->file_offset, 2980 ordered_extent->file_offset,
2967 ordered_extent->start, 2981 ordered_extent->start,
@@ -2997,7 +3011,7 @@ out_unlock:
2997 ordered_extent->file_offset + 3011 ordered_extent->file_offset +
2998 ordered_extent->len - 1, &cached_state, GFP_NOFS); 3012 ordered_extent->len - 1, &cached_state, GFP_NOFS);
2999out: 3013out:
3000 if (root != root->fs_info->tree_root) 3014 if (root != fs_info->tree_root)
3001 btrfs_delalloc_release_metadata(inode, ordered_extent->len); 3015 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
3002 if (trans) 3016 if (trans)
3003 btrfs_end_transaction(trans, root); 3017 btrfs_end_transaction(trans, root);
@@ -3039,7 +3053,7 @@ out:
3039 if (new) { 3053 if (new) {
3040 if (ret) { 3054 if (ret) {
3041 free_sa_defrag_extent(new); 3055 free_sa_defrag_extent(new);
3042 atomic_dec(&root->fs_info->defrag_running); 3056 atomic_dec(&fs_info->defrag_running);
3043 } else { 3057 } else {
3044 relink_file_extents(new); 3058 relink_file_extents(new);
3045 } 3059 }
@@ -3064,7 +3078,7 @@ static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
3064 struct extent_state *state, int uptodate) 3078 struct extent_state *state, int uptodate)
3065{ 3079{
3066 struct inode *inode = page->mapping->host; 3080 struct inode *inode = page->mapping->host;
3067 struct btrfs_root *root = BTRFS_I(inode)->root; 3081 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3068 struct btrfs_ordered_extent *ordered_extent = NULL; 3082 struct btrfs_ordered_extent *ordered_extent = NULL;
3069 struct btrfs_workqueue *wq; 3083 struct btrfs_workqueue *wq;
3070 btrfs_work_func_t func; 3084 btrfs_work_func_t func;
@@ -3077,10 +3091,10 @@ static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
3077 return 0; 3091 return 0;
3078 3092
3079 if (btrfs_is_free_space_inode(inode)) { 3093 if (btrfs_is_free_space_inode(inode)) {
3080 wq = root->fs_info->endio_freespace_worker; 3094 wq = fs_info->endio_freespace_worker;
3081 func = btrfs_freespace_write_helper; 3095 func = btrfs_freespace_write_helper;
3082 } else { 3096 } else {
3083 wq = root->fs_info->endio_write_workers; 3097 wq = fs_info->endio_write_workers;
3084 func = btrfs_endio_write_helper; 3098 func = btrfs_endio_write_helper;
3085 } 3099 }
3086 3100
@@ -3157,7 +3171,7 @@ static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3157 3171
3158void btrfs_add_delayed_iput(struct inode *inode) 3172void btrfs_add_delayed_iput(struct inode *inode)
3159{ 3173{
3160 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; 3174 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3161 struct btrfs_inode *binode = BTRFS_I(inode); 3175 struct btrfs_inode *binode = BTRFS_I(inode);
3162 3176
3163 if (atomic_add_unless(&inode->i_count, -1, 1)) 3177 if (atomic_add_unless(&inode->i_count, -1, 1))
@@ -3205,6 +3219,7 @@ void btrfs_run_delayed_iputs(struct btrfs_root *root)
3205void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans, 3219void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3206 struct btrfs_root *root) 3220 struct btrfs_root *root)
3207{ 3221{
3222 struct btrfs_fs_info *fs_info = root->fs_info;
3208 struct btrfs_block_rsv *block_rsv; 3223 struct btrfs_block_rsv *block_rsv;
3209 int ret; 3224 int ret;
3210 3225
@@ -3229,7 +3244,7 @@ void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3229 3244
3230 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) && 3245 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
3231 btrfs_root_refs(&root->root_item) > 0) { 3246 btrfs_root_refs(&root->root_item) > 0) {
3232 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root, 3247 ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
3233 root->root_key.objectid); 3248 root->root_key.objectid);
3234 if (ret) 3249 if (ret)
3235 btrfs_abort_transaction(trans, ret); 3250 btrfs_abort_transaction(trans, ret);
@@ -3253,6 +3268,7 @@ void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3253 */ 3268 */
3254int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) 3269int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3255{ 3270{
3271 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3256 struct btrfs_root *root = BTRFS_I(inode)->root; 3272 struct btrfs_root *root = BTRFS_I(inode)->root;
3257 struct btrfs_block_rsv *block_rsv = NULL; 3273 struct btrfs_block_rsv *block_rsv = NULL;
3258 int reserve = 0; 3274 int reserve = 0;
@@ -3332,7 +3348,7 @@ int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3332 3348
3333 /* insert an orphan item to track subvolume contains orphan files */ 3349 /* insert an orphan item to track subvolume contains orphan files */
3334 if (insert >= 2) { 3350 if (insert >= 2) {
3335 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root, 3351 ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
3336 root->root_key.objectid); 3352 root->root_key.objectid);
3337 if (ret && ret != -EEXIST) { 3353 if (ret && ret != -EEXIST) {
3338 btrfs_abort_transaction(trans, ret); 3354 btrfs_abort_transaction(trans, ret);
@@ -3383,6 +3399,7 @@ static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3383 */ 3399 */
3384int btrfs_orphan_cleanup(struct btrfs_root *root) 3400int btrfs_orphan_cleanup(struct btrfs_root *root)
3385{ 3401{
3402 struct btrfs_fs_info *fs_info = root->fs_info;
3386 struct btrfs_path *path; 3403 struct btrfs_path *path;
3387 struct extent_buffer *leaf; 3404 struct extent_buffer *leaf;
3388 struct btrfs_key key, found_key; 3405 struct btrfs_key key, found_key;
@@ -3442,8 +3459,8 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
3442 */ 3459 */
3443 3460
3444 if (found_key.offset == last_objectid) { 3461 if (found_key.offset == last_objectid) {
3445 btrfs_err(root->fs_info, 3462 btrfs_err(fs_info,
3446 "Error removing orphan entry, stopping orphan cleanup"); 3463 "Error removing orphan entry, stopping orphan cleanup");
3447 ret = -EINVAL; 3464 ret = -EINVAL;
3448 goto out; 3465 goto out;
3449 } 3466 }
@@ -3453,12 +3470,12 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
3453 found_key.objectid = found_key.offset; 3470 found_key.objectid = found_key.offset;
3454 found_key.type = BTRFS_INODE_ITEM_KEY; 3471 found_key.type = BTRFS_INODE_ITEM_KEY;
3455 found_key.offset = 0; 3472 found_key.offset = 0;
3456 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL); 3473 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
3457 ret = PTR_ERR_OR_ZERO(inode); 3474 ret = PTR_ERR_OR_ZERO(inode);
3458 if (ret && ret != -ENOENT) 3475 if (ret && ret != -ENOENT)
3459 goto out; 3476 goto out;
3460 3477
3461 if (ret == -ENOENT && root == root->fs_info->tree_root) { 3478 if (ret == -ENOENT && root == fs_info->tree_root) {
3462 struct btrfs_root *dead_root; 3479 struct btrfs_root *dead_root;
3463 struct btrfs_fs_info *fs_info = root->fs_info; 3480 struct btrfs_fs_info *fs_info = root->fs_info;
3464 int is_dead_root = 0; 3481 int is_dead_root = 0;
@@ -3500,8 +3517,8 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
3500 ret = PTR_ERR(trans); 3517 ret = PTR_ERR(trans);
3501 goto out; 3518 goto out;
3502 } 3519 }
3503 btrfs_debug(root->fs_info, "auto deleting %Lu", 3520 btrfs_debug(fs_info, "auto deleting %Lu",
3504 found_key.objectid); 3521 found_key.objectid);
3505 ret = btrfs_del_orphan_item(trans, root, 3522 ret = btrfs_del_orphan_item(trans, root,
3506 found_key.objectid); 3523 found_key.objectid);
3507 btrfs_end_transaction(trans, root); 3524 btrfs_end_transaction(trans, root);
@@ -3569,14 +3586,13 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
3569 } 3586 }
3570 3587
3571 if (nr_unlink) 3588 if (nr_unlink)
3572 btrfs_debug(root->fs_info, "unlinked %d orphans", nr_unlink); 3589 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3573 if (nr_truncate) 3590 if (nr_truncate)
3574 btrfs_debug(root->fs_info, "truncated %d orphans", nr_truncate); 3591 btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
3575 3592
3576out: 3593out:
3577 if (ret) 3594 if (ret)
3578 btrfs_err(root->fs_info, 3595 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3579 "could not do orphan cleanup %d", ret);
3580 btrfs_free_path(path); 3596 btrfs_free_path(path);
3581 return ret; 3597 return ret;
3582} 3598}
@@ -3655,6 +3671,7 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3655 */ 3671 */
3656static int btrfs_read_locked_inode(struct inode *inode) 3672static int btrfs_read_locked_inode(struct inode *inode)
3657{ 3673{
3674 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3658 struct btrfs_path *path; 3675 struct btrfs_path *path;
3659 struct extent_buffer *leaf; 3676 struct extent_buffer *leaf;
3660 struct btrfs_inode_item *inode_item; 3677 struct btrfs_inode_item *inode_item;
@@ -3735,7 +3752,7 @@ cache_index:
3735 * This is required for both inode re-read from disk and delayed inode 3752 * This is required for both inode re-read from disk and delayed inode
3736 * in delayed_nodes_tree. 3753 * in delayed_nodes_tree.
3737 */ 3754 */
3738 if (BTRFS_I(inode)->last_trans == root->fs_info->generation) 3755 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3739 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, 3756 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3740 &BTRFS_I(inode)->runtime_flags); 3757 &BTRFS_I(inode)->runtime_flags);
3741 3758
@@ -3801,7 +3818,7 @@ cache_acl:
3801 path->slots[0] = first_xattr_slot; 3818 path->slots[0] = first_xattr_slot;
3802 ret = btrfs_load_inode_props(inode, path); 3819 ret = btrfs_load_inode_props(inode, path);
3803 if (ret) 3820 if (ret)
3804 btrfs_err(root->fs_info, 3821 btrfs_err(fs_info,
3805 "error loading props for ino %llu (root %llu): %d", 3822 "error loading props for ino %llu (root %llu): %d",
3806 btrfs_ino(inode), 3823 btrfs_ino(inode),
3807 root->root_key.objectid, ret); 3824 root->root_key.objectid, ret);
@@ -3820,7 +3837,7 @@ cache_acl:
3820 break; 3837 break;
3821 case S_IFDIR: 3838 case S_IFDIR:
3822 inode->i_fop = &btrfs_dir_file_operations; 3839 inode->i_fop = &btrfs_dir_file_operations;
3823 if (root == root->fs_info->tree_root) 3840 if (root == fs_info->tree_root)
3824 inode->i_op = &btrfs_dir_ro_inode_operations; 3841 inode->i_op = &btrfs_dir_ro_inode_operations;
3825 else 3842 else
3826 inode->i_op = &btrfs_dir_inode_operations; 3843 inode->i_op = &btrfs_dir_inode_operations;
@@ -3938,6 +3955,7 @@ failed:
3938noinline int btrfs_update_inode(struct btrfs_trans_handle *trans, 3955noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3939 struct btrfs_root *root, struct inode *inode) 3956 struct btrfs_root *root, struct inode *inode)
3940{ 3957{
3958 struct btrfs_fs_info *fs_info = root->fs_info;
3941 int ret; 3959 int ret;
3942 3960
3943 /* 3961 /*
@@ -3949,7 +3967,7 @@ noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3949 */ 3967 */
3950 if (!btrfs_is_free_space_inode(inode) 3968 if (!btrfs_is_free_space_inode(inode)
3951 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID 3969 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3952 && !test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { 3970 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
3953 btrfs_update_root_times(trans, root); 3971 btrfs_update_root_times(trans, root);
3954 3972
3955 ret = btrfs_delayed_update_inode(trans, root, inode); 3973 ret = btrfs_delayed_update_inode(trans, root, inode);
@@ -3983,6 +4001,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3983 struct inode *dir, struct inode *inode, 4001 struct inode *dir, struct inode *inode,
3984 const char *name, int name_len) 4002 const char *name, int name_len)
3985{ 4003{
4004 struct btrfs_fs_info *fs_info = root->fs_info;
3986 struct btrfs_path *path; 4005 struct btrfs_path *path;
3987 int ret = 0; 4006 int ret = 0;
3988 struct extent_buffer *leaf; 4007 struct extent_buffer *leaf;
@@ -4037,7 +4056,7 @@ static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4037 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino, 4056 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4038 dir_ino, &index); 4057 dir_ino, &index);
4039 if (ret) { 4058 if (ret) {
4040 btrfs_info(root->fs_info, 4059 btrfs_info(fs_info,
4041 "failed to delete reference to %.*s, inode %llu parent %llu", 4060 "failed to delete reference to %.*s, inode %llu parent %llu",
4042 name_len, name, ino, dir_ino); 4061 name_len, name, ino, dir_ino);
4043 btrfs_abort_transaction(trans, ret); 4062 btrfs_abort_transaction(trans, ret);
@@ -4149,6 +4168,7 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4149 struct inode *dir, u64 objectid, 4168 struct inode *dir, u64 objectid,
4150 const char *name, int name_len) 4169 const char *name, int name_len)
4151{ 4170{
4171 struct btrfs_fs_info *fs_info = root->fs_info;
4152 struct btrfs_path *path; 4172 struct btrfs_path *path;
4153 struct extent_buffer *leaf; 4173 struct extent_buffer *leaf;
4154 struct btrfs_dir_item *di; 4174 struct btrfs_dir_item *di;
@@ -4181,9 +4201,9 @@ int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4181 } 4201 }
4182 btrfs_release_path(path); 4202 btrfs_release_path(path);
4183 4203
4184 ret = btrfs_del_root_ref(trans, root->fs_info, 4204 ret = btrfs_del_root_ref(trans, fs_info, objectid,
4185 objectid, root->root_key.objectid, 4205 root->root_key.objectid, dir_ino,
4186 dir_ino, &index, name, name_len); 4206 &index, name, name_len);
4187 if (ret < 0) { 4207 if (ret < 0) {
4188 if (ret != -ENOENT) { 4208 if (ret != -ENOENT) {
4189 btrfs_abort_transaction(trans, ret); 4209 btrfs_abort_transaction(trans, ret);
@@ -4285,6 +4305,7 @@ static int truncate_space_check(struct btrfs_trans_handle *trans,
4285 struct btrfs_root *root, 4305 struct btrfs_root *root,
4286 u64 bytes_deleted) 4306 u64 bytes_deleted)
4287{ 4307{
4308 struct btrfs_fs_info *fs_info = root->fs_info;
4288 int ret; 4309 int ret;
4289 4310
4290 /* 4311 /*
@@ -4292,11 +4313,11 @@ static int truncate_space_check(struct btrfs_trans_handle *trans,
4292 * intend to use this reservation at all. 4313 * intend to use this reservation at all.
4293 */ 4314 */
4294 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted); 4315 bytes_deleted = btrfs_csum_bytes_to_leaves(root, bytes_deleted);
4295 bytes_deleted *= root->fs_info->nodesize; 4316 bytes_deleted *= fs_info->nodesize;
4296 ret = btrfs_block_rsv_add(root, &root->fs_info->trans_block_rsv, 4317 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
4297 bytes_deleted, BTRFS_RESERVE_NO_FLUSH); 4318 bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4298 if (!ret) { 4319 if (!ret) {
4299 trace_btrfs_space_reservation(root->fs_info, "transaction", 4320 trace_btrfs_space_reservation(fs_info, "transaction",
4300 trans->transid, 4321 trans->transid,
4301 bytes_deleted, 1); 4322 bytes_deleted, 1);
4302 trans->bytes_reserved += bytes_deleted; 4323 trans->bytes_reserved += bytes_deleted;
@@ -4363,6 +4384,7 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4363 struct inode *inode, 4384 struct inode *inode,
4364 u64 new_size, u32 min_type) 4385 u64 new_size, u32 min_type)
4365{ 4386{
4387 struct btrfs_fs_info *fs_info = root->fs_info;
4366 struct btrfs_path *path; 4388 struct btrfs_path *path;
4367 struct extent_buffer *leaf; 4389 struct extent_buffer *leaf;
4368 struct btrfs_file_extent_item *fi; 4390 struct btrfs_file_extent_item *fi;
@@ -4408,9 +4430,9 @@ int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4408 * extent just the way it is. 4430 * extent just the way it is.
4409 */ 4431 */
4410 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 4432 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4411 root == root->fs_info->tree_root) 4433 root == fs_info->tree_root)
4412 btrfs_drop_extent_cache(inode, ALIGN(new_size, 4434 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4413 root->fs_info->sectorsize), 4435 fs_info->sectorsize),
4414 (u64)-1, 0); 4436 (u64)-1, 0);
4415 4437
4416 /* 4438 /*
@@ -4510,7 +4532,7 @@ search_again:
4510 btrfs_file_extent_num_bytes(leaf, fi); 4532 btrfs_file_extent_num_bytes(leaf, fi);
4511 extent_num_bytes = ALIGN(new_size - 4533 extent_num_bytes = ALIGN(new_size -
4512 found_key.offset, 4534 found_key.offset,
4513 root->fs_info->sectorsize); 4535 fs_info->sectorsize);
4514 btrfs_set_file_extent_num_bytes(leaf, fi, 4536 btrfs_set_file_extent_num_bytes(leaf, fi,
4515 extent_num_bytes); 4537 extent_num_bytes);
4516 num_dec = (orig_num_bytes - 4538 num_dec = (orig_num_bytes -
@@ -4597,7 +4619,7 @@ delete:
4597 4619
4598 if (found_extent && 4620 if (found_extent &&
4599 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 4621 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4600 root == root->fs_info->tree_root)) { 4622 root == fs_info->tree_root)) {
4601 btrfs_set_path_blocking(path); 4623 btrfs_set_path_blocking(path);
4602 bytes_deleted += extent_num_bytes; 4624 bytes_deleted += extent_num_bytes;
4603 ret = btrfs_free_extent(trans, root, extent_start, 4625 ret = btrfs_free_extent(trans, root, extent_start,
@@ -4699,13 +4721,13 @@ error:
4699int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len, 4721int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4700 int front) 4722 int front)
4701{ 4723{
4724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4702 struct address_space *mapping = inode->i_mapping; 4725 struct address_space *mapping = inode->i_mapping;
4703 struct btrfs_root *root = BTRFS_I(inode)->root;
4704 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 4726 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4705 struct btrfs_ordered_extent *ordered; 4727 struct btrfs_ordered_extent *ordered;
4706 struct extent_state *cached_state = NULL; 4728 struct extent_state *cached_state = NULL;
4707 char *kaddr; 4729 char *kaddr;
4708 u32 blocksize = root->fs_info->sectorsize; 4730 u32 blocksize = fs_info->sectorsize;
4709 pgoff_t index = from >> PAGE_SHIFT; 4731 pgoff_t index = from >> PAGE_SHIFT;
4710 unsigned offset = from & (blocksize - 1); 4732 unsigned offset = from & (blocksize - 1);
4711 struct page *page; 4733 struct page *page;
@@ -4809,6 +4831,7 @@ out:
4809static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode, 4831static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4810 u64 offset, u64 len) 4832 u64 offset, u64 len)
4811{ 4833{
4834 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4812 struct btrfs_trans_handle *trans; 4835 struct btrfs_trans_handle *trans;
4813 int ret; 4836 int ret;
4814 4837
@@ -4816,8 +4839,8 @@ static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4816 * Still need to make sure the inode looks like it's been updated so 4839 * Still need to make sure the inode looks like it's been updated so
4817 * that any holes get logged if we fsync. 4840 * that any holes get logged if we fsync.
4818 */ 4841 */
4819 if (btrfs_fs_incompat(root->fs_info, NO_HOLES)) { 4842 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4820 BTRFS_I(inode)->last_trans = root->fs_info->generation; 4843 BTRFS_I(inode)->last_trans = fs_info->generation;
4821 BTRFS_I(inode)->last_sub_trans = root->log_transid; 4844 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4822 BTRFS_I(inode)->last_log_commit = root->last_log_commit; 4845 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4823 return 0; 4846 return 0;
@@ -4857,13 +4880,14 @@ static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4857 */ 4880 */
4858int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size) 4881int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4859{ 4882{
4883 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4860 struct btrfs_root *root = BTRFS_I(inode)->root; 4884 struct btrfs_root *root = BTRFS_I(inode)->root;
4861 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 4885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4862 struct extent_map *em = NULL; 4886 struct extent_map *em = NULL;
4863 struct extent_state *cached_state = NULL; 4887 struct extent_state *cached_state = NULL;
4864 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 4888 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4865 u64 hole_start = ALIGN(oldsize, root->fs_info->sectorsize); 4889 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4866 u64 block_end = ALIGN(size, root->fs_info->sectorsize); 4890 u64 block_end = ALIGN(size, fs_info->sectorsize);
4867 u64 last_byte; 4891 u64 last_byte;
4868 u64 cur_offset; 4892 u64 cur_offset;
4869 u64 hole_size; 4893 u64 hole_size;
@@ -4906,7 +4930,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4906 break; 4930 break;
4907 } 4931 }
4908 last_byte = min(extent_map_end(em), block_end); 4932 last_byte = min(extent_map_end(em), block_end);
4909 last_byte = ALIGN(last_byte, root->fs_info->sectorsize); 4933 last_byte = ALIGN(last_byte, fs_info->sectorsize);
4910 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) { 4934 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4911 struct extent_map *hole_em; 4935 struct extent_map *hole_em;
4912 hole_size = last_byte - cur_offset; 4936 hole_size = last_byte - cur_offset;
@@ -4931,9 +4955,9 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4931 hole_em->block_len = 0; 4955 hole_em->block_len = 0;
4932 hole_em->orig_block_len = 0; 4956 hole_em->orig_block_len = 0;
4933 hole_em->ram_bytes = hole_size; 4957 hole_em->ram_bytes = hole_size;
4934 hole_em->bdev = root->fs_info->fs_devices->latest_bdev; 4958 hole_em->bdev = fs_info->fs_devices->latest_bdev;
4935 hole_em->compress_type = BTRFS_COMPRESS_NONE; 4959 hole_em->compress_type = BTRFS_COMPRESS_NONE;
4936 hole_em->generation = root->fs_info->generation; 4960 hole_em->generation = fs_info->generation;
4937 4961
4938 while (1) { 4962 while (1) {
4939 write_lock(&em_tree->lock); 4963 write_lock(&em_tree->lock);
@@ -5203,6 +5227,7 @@ static void evict_inode_truncate_pages(struct inode *inode)
5203 5227
5204void btrfs_evict_inode(struct inode *inode) 5228void btrfs_evict_inode(struct inode *inode)
5205{ 5229{
5230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5206 struct btrfs_trans_handle *trans; 5231 struct btrfs_trans_handle *trans;
5207 struct btrfs_root *root = BTRFS_I(inode)->root; 5232 struct btrfs_root *root = BTRFS_I(inode)->root;
5208 struct btrfs_block_rsv *rsv, *global_rsv; 5233 struct btrfs_block_rsv *rsv, *global_rsv;
@@ -5217,7 +5242,7 @@ void btrfs_evict_inode(struct inode *inode)
5217 return; 5242 return;
5218 } 5243 }
5219 5244
5220 min_size = btrfs_calc_trunc_metadata_size(root->fs_info, 1); 5245 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
5221 5246
5222 evict_inode_truncate_pages(inode); 5247 evict_inode_truncate_pages(inode);
5223 5248
@@ -5237,7 +5262,7 @@ void btrfs_evict_inode(struct inode *inode)
5237 5262
5238 btrfs_free_io_failure_record(inode, 0, (u64)-1); 5263 btrfs_free_io_failure_record(inode, 0, (u64)-1);
5239 5264
5240 if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) { 5265 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
5241 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, 5266 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
5242 &BTRFS_I(inode)->runtime_flags)); 5267 &BTRFS_I(inode)->runtime_flags));
5243 goto no_delete; 5268 goto no_delete;
@@ -5262,7 +5287,7 @@ void btrfs_evict_inode(struct inode *inode)
5262 } 5287 }
5263 rsv->size = min_size; 5288 rsv->size = min_size;
5264 rsv->failfast = 1; 5289 rsv->failfast = 1;
5265 global_rsv = &root->fs_info->global_block_rsv; 5290 global_rsv = &fs_info->global_block_rsv;
5266 5291
5267 btrfs_i_size_write(inode, 0); 5292 btrfs_i_size_write(inode, 0);
5268 5293
@@ -5296,9 +5321,9 @@ void btrfs_evict_inode(struct inode *inode)
5296 * steal_from_global == 3: abandon all hope! 5321 * steal_from_global == 3: abandon all hope!
5297 */ 5322 */
5298 if (steal_from_global > 2) { 5323 if (steal_from_global > 2) {
5299 btrfs_warn(root->fs_info, 5324 btrfs_warn(fs_info,
5300 "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",
5301 ret); 5326 ret);
5302 btrfs_orphan_del(NULL, inode); 5327 btrfs_orphan_del(NULL, inode);
5303 btrfs_free_block_rsv(root, rsv); 5328 btrfs_free_block_rsv(root, rsv);
5304 goto no_delete; 5329 goto no_delete;
@@ -5347,7 +5372,7 @@ void btrfs_evict_inode(struct inode *inode)
5347 if (ret != -ENOSPC && ret != -EAGAIN) 5372 if (ret != -ENOSPC && ret != -EAGAIN)
5348 break; 5373 break;
5349 5374
5350 trans->block_rsv = &root->fs_info->trans_block_rsv; 5375 trans->block_rsv = &fs_info->trans_block_rsv;
5351 btrfs_end_transaction(trans, root); 5376 btrfs_end_transaction(trans, root);
5352 trans = NULL; 5377 trans = NULL;
5353 btrfs_btree_balance_dirty(root); 5378 btrfs_btree_balance_dirty(root);
@@ -5366,8 +5391,8 @@ void btrfs_evict_inode(struct inode *inode)
5366 btrfs_orphan_del(NULL, inode); 5391 btrfs_orphan_del(NULL, inode);
5367 } 5392 }
5368 5393
5369 trans->block_rsv = &root->fs_info->trans_block_rsv; 5394 trans->block_rsv = &fs_info->trans_block_rsv;
5370 if (!(root == root->fs_info->tree_root || 5395 if (!(root == fs_info->tree_root ||
5371 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)) 5396 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
5372 btrfs_return_ino(root, btrfs_ino(inode)); 5397 btrfs_return_ino(root, btrfs_ino(inode));
5373 5398
@@ -5424,6 +5449,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
5424 struct btrfs_key *location, 5449 struct btrfs_key *location,
5425 struct btrfs_root **sub_root) 5450 struct btrfs_root **sub_root)
5426{ 5451{
5452 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5427 struct btrfs_path *path; 5453 struct btrfs_path *path;
5428 struct btrfs_root *new_root; 5454 struct btrfs_root *new_root;
5429 struct btrfs_root_ref *ref; 5455 struct btrfs_root_ref *ref;
@@ -5443,8 +5469,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
5443 key.type = BTRFS_ROOT_REF_KEY; 5469 key.type = BTRFS_ROOT_REF_KEY;
5444 key.offset = location->objectid; 5470 key.offset = location->objectid;
5445 5471
5446 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, path, 5472 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5447 0, 0);
5448 if (ret) { 5473 if (ret) {
5449 if (ret < 0) 5474 if (ret < 0)
5450 err = ret; 5475 err = ret;
@@ -5465,7 +5490,7 @@ static int fixup_tree_root_location(struct btrfs_root *root,
5465 5490
5466 btrfs_release_path(path); 5491 btrfs_release_path(path);
5467 5492
5468 new_root = btrfs_read_fs_root_no_name(root->fs_info, location); 5493 new_root = btrfs_read_fs_root_no_name(fs_info, location);
5469 if (IS_ERR(new_root)) { 5494 if (IS_ERR(new_root)) {
5470 err = PTR_ERR(new_root); 5495 err = PTR_ERR(new_root);
5471 goto out; 5496 goto out;
@@ -5519,6 +5544,7 @@ static void inode_tree_add(struct inode *inode)
5519 5544
5520static void inode_tree_del(struct inode *inode) 5545static void inode_tree_del(struct inode *inode)
5521{ 5546{
5547 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5522 struct btrfs_root *root = BTRFS_I(inode)->root; 5548 struct btrfs_root *root = BTRFS_I(inode)->root;
5523 int empty = 0; 5549 int empty = 0;
5524 5550
@@ -5531,7 +5557,7 @@ static void inode_tree_del(struct inode *inode)
5531 spin_unlock(&root->inode_lock); 5557 spin_unlock(&root->inode_lock);
5532 5558
5533 if (empty && btrfs_root_refs(&root->root_item) == 0) { 5559 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5534 synchronize_srcu(&root->fs_info->subvol_srcu); 5560 synchronize_srcu(&fs_info->subvol_srcu);
5535 spin_lock(&root->inode_lock); 5561 spin_lock(&root->inode_lock);
5536 empty = RB_EMPTY_ROOT(&root->inode_tree); 5562 empty = RB_EMPTY_ROOT(&root->inode_tree);
5537 spin_unlock(&root->inode_lock); 5563 spin_unlock(&root->inode_lock);
@@ -5542,13 +5568,14 @@ static void inode_tree_del(struct inode *inode)
5542 5568
5543void btrfs_invalidate_inodes(struct btrfs_root *root) 5569void btrfs_invalidate_inodes(struct btrfs_root *root)
5544{ 5570{
5571 struct btrfs_fs_info *fs_info = root->fs_info;
5545 struct rb_node *node; 5572 struct rb_node *node;
5546 struct rb_node *prev; 5573 struct rb_node *prev;
5547 struct btrfs_inode *entry; 5574 struct btrfs_inode *entry;
5548 struct inode *inode; 5575 struct inode *inode;
5549 u64 objectid = 0; 5576 u64 objectid = 0;
5550 5577
5551 if (!test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 5578 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
5552 WARN_ON(btrfs_root_refs(&root->root_item) != 0); 5579 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
5553 5580
5554 spin_lock(&root->inode_lock); 5581 spin_lock(&root->inode_lock);
@@ -5696,6 +5723,7 @@ static struct inode *new_simple_dir(struct super_block *s,
5696 5723
5697struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry) 5724struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5698{ 5725{
5726 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5699 struct inode *inode; 5727 struct inode *inode;
5700 struct btrfs_root *root = BTRFS_I(dir)->root; 5728 struct btrfs_root *root = BTRFS_I(dir)->root;
5701 struct btrfs_root *sub_root = root; 5729 struct btrfs_root *sub_root = root;
@@ -5720,7 +5748,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5720 5748
5721 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY); 5749 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5722 5750
5723 index = srcu_read_lock(&root->fs_info->subvol_srcu); 5751 index = srcu_read_lock(&fs_info->subvol_srcu);
5724 ret = fixup_tree_root_location(root, dir, dentry, 5752 ret = fixup_tree_root_location(root, dir, dentry,
5725 &location, &sub_root); 5753 &location, &sub_root);
5726 if (ret < 0) { 5754 if (ret < 0) {
@@ -5731,13 +5759,13 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5731 } else { 5759 } else {
5732 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL); 5760 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
5733 } 5761 }
5734 srcu_read_unlock(&root->fs_info->subvol_srcu, index); 5762 srcu_read_unlock(&fs_info->subvol_srcu, index);
5735 5763
5736 if (!IS_ERR(inode) && root != sub_root) { 5764 if (!IS_ERR(inode) && root != sub_root) {
5737 down_read(&root->fs_info->cleanup_work_sem); 5765 down_read(&fs_info->cleanup_work_sem);
5738 if (!(inode->i_sb->s_flags & MS_RDONLY)) 5766 if (!(inode->i_sb->s_flags & MS_RDONLY))
5739 ret = btrfs_orphan_cleanup(sub_root); 5767 ret = btrfs_orphan_cleanup(sub_root);
5740 up_read(&root->fs_info->cleanup_work_sem); 5768 up_read(&fs_info->cleanup_work_sem);
5741 if (ret) { 5769 if (ret) {
5742 iput(inode); 5770 iput(inode);
5743 inode = ERR_PTR(ret); 5771 inode = ERR_PTR(ret);
@@ -6109,6 +6137,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6109 u64 ref_objectid, u64 objectid, 6137 u64 ref_objectid, u64 objectid,
6110 umode_t mode, u64 *index) 6138 umode_t mode, u64 *index)
6111{ 6139{
6140 struct btrfs_fs_info *fs_info = root->fs_info;
6112 struct inode *inode; 6141 struct inode *inode;
6113 struct btrfs_inode_item *inode_item; 6142 struct btrfs_inode_item *inode_item;
6114 struct btrfs_key *location; 6143 struct btrfs_key *location;
@@ -6124,7 +6153,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6124 if (!path) 6153 if (!path)
6125 return ERR_PTR(-ENOMEM); 6154 return ERR_PTR(-ENOMEM);
6126 6155
6127 inode = new_inode(root->fs_info->sb); 6156 inode = new_inode(fs_info->sb);
6128 if (!inode) { 6157 if (!inode) {
6129 btrfs_free_path(path); 6158 btrfs_free_path(path);
6130 return ERR_PTR(-ENOMEM); 6159 return ERR_PTR(-ENOMEM);
@@ -6237,9 +6266,9 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6237 btrfs_inherit_iflags(inode, dir); 6266 btrfs_inherit_iflags(inode, dir);
6238 6267
6239 if (S_ISREG(mode)) { 6268 if (S_ISREG(mode)) {
6240 if (btrfs_test_opt(root->fs_info, NODATASUM)) 6269 if (btrfs_test_opt(fs_info, NODATASUM))
6241 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; 6270 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6242 if (btrfs_test_opt(root->fs_info, NODATACOW)) 6271 if (btrfs_test_opt(fs_info, NODATACOW))
6243 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW | 6272 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6244 BTRFS_INODE_NODATASUM; 6273 BTRFS_INODE_NODATASUM;
6245 } 6274 }
@@ -6253,7 +6282,7 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6253 6282
6254 ret = btrfs_inode_inherit_props(trans, inode, dir); 6283 ret = btrfs_inode_inherit_props(trans, inode, dir);
6255 if (ret) 6284 if (ret)
6256 btrfs_err(root->fs_info, 6285 btrfs_err(fs_info,
6257 "error inheriting props for ino %llu (root %llu): %d", 6286 "error inheriting props for ino %llu (root %llu): %d",
6258 btrfs_ino(inode), root->root_key.objectid, ret); 6287 btrfs_ino(inode), root->root_key.objectid, ret);
6259 6288
@@ -6284,6 +6313,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
6284 struct inode *parent_inode, struct inode *inode, 6313 struct inode *parent_inode, struct inode *inode,
6285 const char *name, int name_len, int add_backref, u64 index) 6314 const char *name, int name_len, int add_backref, u64 index)
6286{ 6315{
6316 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6287 int ret = 0; 6317 int ret = 0;
6288 struct btrfs_key key; 6318 struct btrfs_key key;
6289 struct btrfs_root *root = BTRFS_I(parent_inode)->root; 6319 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
@@ -6299,9 +6329,9 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,
6299 } 6329 }
6300 6330
6301 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { 6331 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6302 ret = btrfs_add_root_ref(trans, root->fs_info, 6332 ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6303 key.objectid, root->root_key.objectid, 6333 root->root_key.objectid, parent_ino,
6304 parent_ino, index, name, name_len); 6334 index, name, name_len);
6305 } else if (add_backref) { 6335 } else if (add_backref) {
6306 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino, 6336 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6307 parent_ino, index); 6337 parent_ino, index);
@@ -6335,9 +6365,9 @@ fail_dir_item:
6335 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) { 6365 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6336 u64 local_index; 6366 u64 local_index;
6337 int err; 6367 int err;
6338 err = btrfs_del_root_ref(trans, root->fs_info, 6368 err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6339 key.objectid, root->root_key.objectid, 6369 root->root_key.objectid, parent_ino,
6340 parent_ino, &local_index, name, name_len); 6370 &local_index, name, name_len);
6341 6371
6342 } else if (add_backref) { 6372 } else if (add_backref) {
6343 u64 local_index; 6373 u64 local_index;
@@ -6761,6 +6791,7 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
6761 size_t pg_offset, u64 start, u64 len, 6791 size_t pg_offset, u64 start, u64 len,
6762 int create) 6792 int create)
6763{ 6793{
6794 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6764 int ret; 6795 int ret;
6765 int err = 0; 6796 int err = 0;
6766 u64 extent_start = 0; 6797 u64 extent_start = 0;
@@ -6782,7 +6813,7 @@ again:
6782 read_lock(&em_tree->lock); 6813 read_lock(&em_tree->lock);
6783 em = lookup_extent_mapping(em_tree, start, len); 6814 em = lookup_extent_mapping(em_tree, start, len);
6784 if (em) 6815 if (em)
6785 em->bdev = root->fs_info->fs_devices->latest_bdev; 6816 em->bdev = fs_info->fs_devices->latest_bdev;
6786 read_unlock(&em_tree->lock); 6817 read_unlock(&em_tree->lock);
6787 6818
6788 if (em) { 6819 if (em) {
@@ -6798,7 +6829,7 @@ again:
6798 err = -ENOMEM; 6829 err = -ENOMEM;
6799 goto out; 6830 goto out;
6800 } 6831 }
6801 em->bdev = root->fs_info->fs_devices->latest_bdev; 6832 em->bdev = fs_info->fs_devices->latest_bdev;
6802 em->start = EXTENT_MAP_HOLE; 6833 em->start = EXTENT_MAP_HOLE;
6803 em->orig_start = EXTENT_MAP_HOLE; 6834 em->orig_start = EXTENT_MAP_HOLE;
6804 em->len = (u64)-1; 6835 em->len = (u64)-1;
@@ -6858,7 +6889,7 @@ again:
6858 size_t size; 6889 size_t size;
6859 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item); 6890 size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6860 extent_end = ALIGN(extent_start + size, 6891 extent_end = ALIGN(extent_start + size,
6861 root->fs_info->sectorsize); 6892 fs_info->sectorsize);
6862 } 6893 }
6863next: 6894next:
6864 if (start >= extent_end) { 6895 if (start >= extent_end) {
@@ -6907,7 +6938,7 @@ next:
6907 copy_size = min_t(u64, PAGE_SIZE - pg_offset, 6938 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6908 size - extent_offset); 6939 size - extent_offset);
6909 em->start = extent_start + extent_offset; 6940 em->start = extent_start + extent_offset;
6910 em->len = ALIGN(copy_size, root->fs_info->sectorsize); 6941 em->len = ALIGN(copy_size, fs_info->sectorsize);
6911 em->orig_block_len = em->len; 6942 em->orig_block_len = em->len;
6912 em->orig_start = em->start; 6943 em->orig_start = em->start;
6913 ptr = btrfs_file_extent_inline_start(item) + extent_offset; 6944 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
@@ -6966,7 +6997,7 @@ not_found_em:
6966insert: 6997insert:
6967 btrfs_release_path(path); 6998 btrfs_release_path(path);
6968 if (em->start > start || extent_map_end(em) <= start) { 6999 if (em->start > start || extent_map_end(em) <= start) {
6969 btrfs_err(root->fs_info, 7000 btrfs_err(fs_info,
6970 "bad extent! em: [%llu %llu] passed [%llu %llu]", 7001 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6971 em->start, em->len, start, len); 7002 em->start, em->len, start, len);
6972 err = -EIO; 7003 err = -EIO;
@@ -7206,6 +7237,7 @@ static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7206static struct extent_map *btrfs_new_extent_direct(struct inode *inode, 7237static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7207 u64 start, u64 len) 7238 u64 start, u64 len)
7208{ 7239{
7240 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7209 struct btrfs_root *root = BTRFS_I(inode)->root; 7241 struct btrfs_root *root = BTRFS_I(inode)->root;
7210 struct extent_map *em; 7242 struct extent_map *em;
7211 struct btrfs_key ins; 7243 struct btrfs_key ins;
@@ -7213,7 +7245,7 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7213 int ret; 7245 int ret;
7214 7246
7215 alloc_hint = get_extent_allocation_hint(inode, start, len); 7247 alloc_hint = get_extent_allocation_hint(inode, start, len);
7216 ret = btrfs_reserve_extent(root, len, len, root->fs_info->sectorsize, 7248 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7217 0, alloc_hint, &ins, 1, 1); 7249 0, alloc_hint, &ins, 1, 1);
7218 if (ret) 7250 if (ret)
7219 return ERR_PTR(ret); 7251 return ERR_PTR(ret);
@@ -7221,7 +7253,7 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7221 em = btrfs_create_dio_extent(inode, start, ins.offset, start, 7253 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7222 ins.objectid, ins.offset, ins.offset, 7254 ins.objectid, ins.offset, ins.offset,
7223 ins.offset, 0); 7255 ins.offset, 0);
7224 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); 7256 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7225 if (IS_ERR(em)) 7257 if (IS_ERR(em))
7226 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1); 7258 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
7227 7259
@@ -7596,8 +7628,8 @@ static void adjust_dio_outstanding_extents(struct inode *inode,
7596static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock, 7628static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7597 struct buffer_head *bh_result, int create) 7629 struct buffer_head *bh_result, int create)
7598{ 7630{
7631 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7599 struct extent_map *em; 7632 struct extent_map *em;
7600 struct btrfs_root *root = BTRFS_I(inode)->root;
7601 struct extent_state *cached_state = NULL; 7633 struct extent_state *cached_state = NULL;
7602 struct btrfs_dio_data *dio_data = NULL; 7634 struct btrfs_dio_data *dio_data = NULL;
7603 u64 start = iblock << inode->i_blkbits; 7635 u64 start = iblock << inode->i_blkbits;
@@ -7609,7 +7641,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7609 if (create) 7641 if (create)
7610 unlock_bits |= EXTENT_DIRTY; 7642 unlock_bits |= EXTENT_DIRTY;
7611 else 7643 else
7612 len = min_t(u64, len, root->fs_info->sectorsize); 7644 len = min_t(u64, len, fs_info->sectorsize);
7613 7645
7614 lockstart = start; 7646 lockstart = start;
7615 lockend = start + len - 1; 7647 lockend = start + len - 1;
@@ -7698,14 +7730,14 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7698 7730
7699 if (can_nocow_extent(inode, start, &len, &orig_start, 7731 if (can_nocow_extent(inode, start, &len, &orig_start,
7700 &orig_block_len, &ram_bytes) == 1 && 7732 &orig_block_len, &ram_bytes) == 1 &&
7701 btrfs_inc_nocow_writers(root->fs_info, block_start)) { 7733 btrfs_inc_nocow_writers(fs_info, block_start)) {
7702 struct extent_map *em2; 7734 struct extent_map *em2;
7703 7735
7704 em2 = btrfs_create_dio_extent(inode, start, len, 7736 em2 = btrfs_create_dio_extent(inode, start, len,
7705 orig_start, block_start, 7737 orig_start, block_start,
7706 len, orig_block_len, 7738 len, orig_block_len,
7707 ram_bytes, type); 7739 ram_bytes, type);
7708 btrfs_dec_nocow_writers(root->fs_info, block_start); 7740 btrfs_dec_nocow_writers(fs_info, block_start);
7709 if (type == BTRFS_ORDERED_PREALLOC) { 7741 if (type == BTRFS_ORDERED_PREALLOC) {
7710 free_extent_map(em); 7742 free_extent_map(em);
7711 em = em2; 7743 em = em2;
@@ -7731,7 +7763,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7731 * give it a chance to use preallocated space. 7763 * give it a chance to use preallocated space.
7732 */ 7764 */
7733 len = min_t(u64, bh_result->b_size, em->len - (start - em->start)); 7765 len = min_t(u64, bh_result->b_size, em->len - (start - em->start));
7734 len = ALIGN(len, root->fs_info->sectorsize); 7766 len = ALIGN(len, fs_info->sectorsize);
7735 free_extent_map(em); 7767 free_extent_map(em);
7736 em = btrfs_new_extent_direct(inode, start, len); 7768 em = btrfs_new_extent_direct(inode, start, len);
7737 if (IS_ERR(em)) { 7769 if (IS_ERR(em)) {
@@ -8142,7 +8174,7 @@ static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8142 const u64 bytes, 8174 const u64 bytes,
8143 const int uptodate) 8175 const int uptodate)
8144{ 8176{
8145 struct btrfs_root *root = BTRFS_I(inode)->root; 8177 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8146 struct btrfs_ordered_extent *ordered = NULL; 8178 struct btrfs_ordered_extent *ordered = NULL;
8147 u64 ordered_offset = offset; 8179 u64 ordered_offset = offset;
8148 u64 ordered_bytes = bytes; 8180 u64 ordered_bytes = bytes;
@@ -8158,8 +8190,7 @@ again:
8158 8190
8159 btrfs_init_work(&ordered->work, btrfs_endio_write_helper, 8191 btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8160 finish_ordered_fn, NULL, NULL); 8192 finish_ordered_fn, NULL, NULL);
8161 btrfs_queue_work(root->fs_info->endio_write_workers, 8193 btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
8162 &ordered->work);
8163out_test: 8194out_test:
8164 /* 8195 /*
8165 * our bio might span multiple ordered extents. If we haven't 8196 * our bio might span multiple ordered extents. If we haven't
@@ -8285,6 +8316,7 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8285 u64 file_offset, int skip_sum, 8316 u64 file_offset, int skip_sum,
8286 int async_submit) 8317 int async_submit)
8287{ 8318{
8319 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8288 struct btrfs_dio_private *dip = bio->bi_private; 8320 struct btrfs_dio_private *dip = bio->bi_private;
8289 bool write = bio_op(bio) == REQ_OP_WRITE; 8321 bool write = bio_op(bio) == REQ_OP_WRITE;
8290 struct btrfs_root *root = BTRFS_I(inode)->root; 8322 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -8296,8 +8328,7 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8296 bio_get(bio); 8328 bio_get(bio);
8297 8329
8298 if (!write) { 8330 if (!write) {
8299 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 8331 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8300 BTRFS_WQ_ENDIO_DATA);
8301 if (ret) 8332 if (ret)
8302 goto err; 8333 goto err;
8303 } 8334 }
@@ -8306,10 +8337,10 @@ static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8306 goto map; 8337 goto map;
8307 8338
8308 if (write && async_submit) { 8339 if (write && async_submit) {
8309 ret = btrfs_wq_submit_bio(root->fs_info, 8340 ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8310 inode, bio, 0, 0, file_offset, 8341 file_offset,
8311 __btrfs_submit_bio_start_direct_io, 8342 __btrfs_submit_bio_start_direct_io,
8312 __btrfs_submit_bio_done); 8343 __btrfs_submit_bio_done);
8313 goto err; 8344 goto err;
8314 } else if (write) { 8345 } else if (write) {
8315 /* 8346 /*
@@ -8336,6 +8367,7 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8336 int skip_sum) 8367 int skip_sum)
8337{ 8368{
8338 struct inode *inode = dip->inode; 8369 struct inode *inode = dip->inode;
8370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8339 struct btrfs_root *root = BTRFS_I(inode)->root; 8371 struct btrfs_root *root = BTRFS_I(inode)->root;
8340 struct bio *bio; 8372 struct bio *bio;
8341 struct bio *orig_bio = dip->orig_bio; 8373 struct bio *orig_bio = dip->orig_bio;
@@ -8344,15 +8376,15 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8344 u64 file_offset = dip->logical_offset; 8376 u64 file_offset = dip->logical_offset;
8345 u64 submit_len = 0; 8377 u64 submit_len = 0;
8346 u64 map_length; 8378 u64 map_length;
8347 u32 blocksize = root->fs_info->sectorsize; 8379 u32 blocksize = fs_info->sectorsize;
8348 int async_submit = 0; 8380 int async_submit = 0;
8349 int nr_sectors; 8381 int nr_sectors;
8350 int ret; 8382 int ret;
8351 int i, j; 8383 int i, j;
8352 8384
8353 map_length = orig_bio->bi_iter.bi_size; 8385 map_length = orig_bio->bi_iter.bi_size;
8354 ret = btrfs_map_block(root->fs_info, btrfs_op(orig_bio), 8386 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8355 start_sector << 9, &map_length, NULL, 0); 8387 &map_length, NULL, 0);
8356 if (ret) 8388 if (ret)
8357 return -EIO; 8389 return -EIO;
8358 8390
@@ -8379,7 +8411,7 @@ static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8379 atomic_inc(&dip->pending_bios); 8411 atomic_inc(&dip->pending_bios);
8380 8412
8381 bio_for_each_segment_all(bvec, orig_bio, j) { 8413 bio_for_each_segment_all(bvec, orig_bio, j) {
8382 nr_sectors = BTRFS_BYTES_TO_BLKS(root->fs_info, bvec->bv_len); 8414 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8383 i = 0; 8415 i = 0;
8384next_block: 8416next_block:
8385 if (unlikely(map_length < submit_len + blocksize || 8417 if (unlikely(map_length < submit_len + blocksize ||
@@ -8417,7 +8449,7 @@ next_block:
8417 btrfs_io_bio(bio)->logical = file_offset; 8449 btrfs_io_bio(bio)->logical = file_offset;
8418 8450
8419 map_length = orig_bio->bi_iter.bi_size; 8451 map_length = orig_bio->bi_iter.bi_size;
8420 ret = btrfs_map_block(root->fs_info, btrfs_op(orig_bio), 8452 ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8421 start_sector << 9, 8453 start_sector << 9,
8422 &map_length, NULL, 0); 8454 &map_length, NULL, 0);
8423 if (ret) { 8455 if (ret) {
@@ -8566,9 +8598,10 @@ free_ordered:
8566static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb, 8598static ssize_t check_direct_IO(struct btrfs_root *root, struct kiocb *iocb,
8567 const struct iov_iter *iter, loff_t offset) 8599 const struct iov_iter *iter, loff_t offset)
8568{ 8600{
8601 struct btrfs_fs_info *fs_info = root->fs_info;
8569 int seg; 8602 int seg;
8570 int i; 8603 int i;
8571 unsigned int blocksize_mask = root->fs_info->sectorsize - 1; 8604 unsigned int blocksize_mask = fs_info->sectorsize - 1;
8572 ssize_t retval = -EINVAL; 8605 ssize_t retval = -EINVAL;
8573 8606
8574 if (offset & blocksize_mask) 8607 if (offset & blocksize_mask)
@@ -8600,7 +8633,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8600{ 8633{
8601 struct file *file = iocb->ki_filp; 8634 struct file *file = iocb->ki_filp;
8602 struct inode *inode = file->f_mapping->host; 8635 struct inode *inode = file->f_mapping->host;
8603 struct btrfs_root *root = BTRFS_I(inode)->root; 8636 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8604 struct btrfs_dio_data dio_data = { 0 }; 8637 struct btrfs_dio_data dio_data = { 0 };
8605 loff_t offset = iocb->ki_pos; 8638 loff_t offset = iocb->ki_pos;
8606 size_t count = 0; 8639 size_t count = 0;
@@ -8650,7 +8683,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8650 * originally calculated. Abuse current->journal_info for this. 8683 * originally calculated. Abuse current->journal_info for this.
8651 */ 8684 */
8652 dio_data.reserve = round_up(count, 8685 dio_data.reserve = round_up(count,
8653 root->fs_info->sectorsize); 8686 fs_info->sectorsize);
8654 dio_data.unsubmitted_oe_range_start = (u64)offset; 8687 dio_data.unsubmitted_oe_range_start = (u64)offset;
8655 dio_data.unsubmitted_oe_range_end = (u64)offset; 8688 dio_data.unsubmitted_oe_range_end = (u64)offset;
8656 current->journal_info = &dio_data; 8689 current->journal_info = &dio_data;
@@ -8662,7 +8695,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8662 } 8695 }
8663 8696
8664 ret = __blockdev_direct_IO(iocb, inode, 8697 ret = __blockdev_direct_IO(iocb, inode,
8665 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev, 8698 fs_info->fs_devices->latest_bdev,
8666 iter, btrfs_get_blocks_direct, NULL, 8699 iter, btrfs_get_blocks_direct, NULL,
8667 btrfs_submit_direct, flags); 8700 btrfs_submit_direct, flags);
8668 if (iov_iter_rw(iter) == WRITE) { 8701 if (iov_iter_rw(iter) == WRITE) {
@@ -8921,7 +8954,7 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
8921{ 8954{
8922 struct page *page = vmf->page; 8955 struct page *page = vmf->page;
8923 struct inode *inode = file_inode(vma->vm_file); 8956 struct inode *inode = file_inode(vma->vm_file);
8924 struct btrfs_root *root = BTRFS_I(inode)->root; 8957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8925 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 8958 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8926 struct btrfs_ordered_extent *ordered; 8959 struct btrfs_ordered_extent *ordered;
8927 struct extent_state *cached_state = NULL; 8960 struct extent_state *cached_state = NULL;
@@ -8997,7 +9030,7 @@ again:
8997 9030
8998 if (page->index == ((size - 1) >> PAGE_SHIFT)) { 9031 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
8999 reserved_space = round_up(size - page_start, 9032 reserved_space = round_up(size - page_start,
9000 root->fs_info->sectorsize); 9033 fs_info->sectorsize);
9001 if (reserved_space < PAGE_SIZE) { 9034 if (reserved_space < PAGE_SIZE) {
9002 end = page_start + reserved_space - 1; 9035 end = page_start + reserved_space - 1;
9003 spin_lock(&BTRFS_I(inode)->lock); 9036 spin_lock(&BTRFS_I(inode)->lock);
@@ -9046,7 +9079,7 @@ again:
9046 set_page_dirty(page); 9079 set_page_dirty(page);
9047 SetPageUptodate(page); 9080 SetPageUptodate(page);
9048 9081
9049 BTRFS_I(inode)->last_trans = root->fs_info->generation; 9082 BTRFS_I(inode)->last_trans = fs_info->generation;
9050 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid; 9083 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
9051 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit; 9084 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
9052 9085
@@ -9067,13 +9100,14 @@ out_noreserve:
9067 9100
9068static int btrfs_truncate(struct inode *inode) 9101static int btrfs_truncate(struct inode *inode)
9069{ 9102{
9103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9070 struct btrfs_root *root = BTRFS_I(inode)->root; 9104 struct btrfs_root *root = BTRFS_I(inode)->root;
9071 struct btrfs_block_rsv *rsv; 9105 struct btrfs_block_rsv *rsv;
9072 int ret = 0; 9106 int ret = 0;
9073 int err = 0; 9107 int err = 0;
9074 struct btrfs_trans_handle *trans; 9108 struct btrfs_trans_handle *trans;
9075 u64 mask = root->fs_info->sectorsize - 1; 9109 u64 mask = fs_info->sectorsize - 1;
9076 u64 min_size = btrfs_calc_trunc_metadata_size(root->fs_info, 1); 9110 u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
9077 9111
9078 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask), 9112 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9079 (u64)-1); 9113 (u64)-1);
@@ -9133,7 +9167,7 @@ static int btrfs_truncate(struct inode *inode)
9133 } 9167 }
9134 9168
9135 /* Migrate the slack space for the truncate to our reserve */ 9169 /* Migrate the slack space for the truncate to our reserve */
9136 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv, 9170 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
9137 min_size, 0); 9171 min_size, 0);
9138 BUG_ON(ret); 9172 BUG_ON(ret);
9139 9173
@@ -9156,7 +9190,7 @@ static int btrfs_truncate(struct inode *inode)
9156 break; 9190 break;
9157 } 9191 }
9158 9192
9159 trans->block_rsv = &root->fs_info->trans_block_rsv; 9193 trans->block_rsv = &fs_info->trans_block_rsv;
9160 ret = btrfs_update_inode(trans, root, inode); 9194 ret = btrfs_update_inode(trans, root, inode);
9161 if (ret) { 9195 if (ret) {
9162 err = ret; 9196 err = ret;
@@ -9173,7 +9207,7 @@ static int btrfs_truncate(struct inode *inode)
9173 break; 9207 break;
9174 } 9208 }
9175 9209
9176 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, 9210 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
9177 rsv, min_size, 0); 9211 rsv, min_size, 0);
9178 BUG_ON(ret); /* shouldn't happen */ 9212 BUG_ON(ret); /* shouldn't happen */
9179 trans->block_rsv = rsv; 9213 trans->block_rsv = rsv;
@@ -9187,7 +9221,7 @@ static int btrfs_truncate(struct inode *inode)
9187 } 9221 }
9188 9222
9189 if (trans) { 9223 if (trans) {
9190 trans->block_rsv = &root->fs_info->trans_block_rsv; 9224 trans->block_rsv = &fs_info->trans_block_rsv;
9191 ret = btrfs_update_inode(trans, root, inode); 9225 ret = btrfs_update_inode(trans, root, inode);
9192 if (ret && !err) 9226 if (ret && !err)
9193 err = ret; 9227 err = ret;
@@ -9312,6 +9346,7 @@ static void btrfs_i_callback(struct rcu_head *head)
9312 9346
9313void btrfs_destroy_inode(struct inode *inode) 9347void btrfs_destroy_inode(struct inode *inode)
9314{ 9348{
9349 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9315 struct btrfs_ordered_extent *ordered; 9350 struct btrfs_ordered_extent *ordered;
9316 struct btrfs_root *root = BTRFS_I(inode)->root; 9351 struct btrfs_root *root = BTRFS_I(inode)->root;
9317 9352
@@ -9333,8 +9368,8 @@ void btrfs_destroy_inode(struct inode *inode)
9333 9368
9334 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM, 9369 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9335 &BTRFS_I(inode)->runtime_flags)) { 9370 &BTRFS_I(inode)->runtime_flags)) {
9336 btrfs_info(root->fs_info, "inode %llu still on the orphan list", 9371 btrfs_info(fs_info, "inode %llu still on the orphan list",
9337 btrfs_ino(inode)); 9372 btrfs_ino(inode));
9338 atomic_dec(&root->orphan_inodes); 9373 atomic_dec(&root->orphan_inodes);
9339 } 9374 }
9340 9375
@@ -9343,7 +9378,7 @@ void btrfs_destroy_inode(struct inode *inode)
9343 if (!ordered) 9378 if (!ordered)
9344 break; 9379 break;
9345 else { 9380 else {
9346 btrfs_err(root->fs_info, 9381 btrfs_err(fs_info,
9347 "found ordered extent %llu %llu on inode cleanup", 9382 "found ordered extent %llu %llu on inode cleanup",
9348 ordered->file_offset, ordered->len); 9383 ordered->file_offset, ordered->len);
9349 btrfs_remove_ordered_extent(inode, ordered); 9384 btrfs_remove_ordered_extent(inode, ordered);
@@ -9455,6 +9490,7 @@ static int btrfs_rename_exchange(struct inode *old_dir,
9455 struct inode *new_dir, 9490 struct inode *new_dir,
9456 struct dentry *new_dentry) 9491 struct dentry *new_dentry)
9457{ 9492{
9493 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9458 struct btrfs_trans_handle *trans; 9494 struct btrfs_trans_handle *trans;
9459 struct btrfs_root *root = BTRFS_I(old_dir)->root; 9495 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9460 struct btrfs_root *dest = BTRFS_I(new_dir)->root; 9496 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
@@ -9477,9 +9513,9 @@ static int btrfs_rename_exchange(struct inode *old_dir,
9477 9513
9478 /* close the race window with snapshot create/destroy ioctl */ 9514 /* close the race window with snapshot create/destroy ioctl */
9479 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) 9515 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9480 down_read(&root->fs_info->subvol_sem); 9516 down_read(&fs_info->subvol_sem);
9481 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) 9517 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9482 down_read(&dest->fs_info->subvol_sem); 9518 down_read(&fs_info->subvol_sem);
9483 9519
9484 /* 9520 /*
9485 * We want to reserve the absolute worst case amount of items. So if 9521 * We want to reserve the absolute worst case amount of items. So if
@@ -9512,7 +9548,7 @@ static int btrfs_rename_exchange(struct inode *old_dir,
9512 /* Reference for the source. */ 9548 /* Reference for the source. */
9513 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) { 9549 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9514 /* force full log commit if subvolume involved. */ 9550 /* force full log commit if subvolume involved. */
9515 btrfs_set_log_full_commit(root->fs_info, trans); 9551 btrfs_set_log_full_commit(fs_info, trans);
9516 } else { 9552 } else {
9517 btrfs_pin_log_trans(root); 9553 btrfs_pin_log_trans(root);
9518 root_log_pinned = true; 9554 root_log_pinned = true;
@@ -9528,7 +9564,7 @@ static int btrfs_rename_exchange(struct inode *old_dir,
9528 /* And now for the dest. */ 9564 /* And now for the dest. */
9529 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) { 9565 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9530 /* force full log commit if subvolume involved. */ 9566 /* force full log commit if subvolume involved. */
9531 btrfs_set_log_full_commit(dest->fs_info, trans); 9567 btrfs_set_log_full_commit(fs_info, trans);
9532 } else { 9568 } else {
9533 btrfs_pin_log_trans(dest); 9569 btrfs_pin_log_trans(dest);
9534 dest_log_pinned = true; 9570 dest_log_pinned = true;
@@ -9642,12 +9678,12 @@ out_fail:
9642 * allow the tasks to sync it. 9678 * allow the tasks to sync it.
9643 */ 9679 */
9644 if (ret && (root_log_pinned || dest_log_pinned)) { 9680 if (ret && (root_log_pinned || dest_log_pinned)) {
9645 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || 9681 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9646 btrfs_inode_in_log(new_dir, root->fs_info->generation) || 9682 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9647 btrfs_inode_in_log(old_inode, root->fs_info->generation) || 9683 btrfs_inode_in_log(old_inode, fs_info->generation) ||
9648 (new_inode && 9684 (new_inode &&
9649 btrfs_inode_in_log(new_inode, root->fs_info->generation))) 9685 btrfs_inode_in_log(new_inode, fs_info->generation)))
9650 btrfs_set_log_full_commit(root->fs_info, trans); 9686 btrfs_set_log_full_commit(fs_info, trans);
9651 9687
9652 if (root_log_pinned) { 9688 if (root_log_pinned) {
9653 btrfs_end_log_trans(root); 9689 btrfs_end_log_trans(root);
@@ -9661,9 +9697,9 @@ out_fail:
9661 ret = btrfs_end_transaction(trans, root); 9697 ret = btrfs_end_transaction(trans, root);
9662out_notrans: 9698out_notrans:
9663 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) 9699 if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9664 up_read(&dest->fs_info->subvol_sem); 9700 up_read(&fs_info->subvol_sem);
9665 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) 9701 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9666 up_read(&root->fs_info->subvol_sem); 9702 up_read(&fs_info->subvol_sem);
9667 9703
9668 return ret; 9704 return ret;
9669} 9705}
@@ -9723,6 +9759,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9723 struct inode *new_dir, struct dentry *new_dentry, 9759 struct inode *new_dir, struct dentry *new_dentry,
9724 unsigned int flags) 9760 unsigned int flags)
9725{ 9761{
9762 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9726 struct btrfs_trans_handle *trans; 9763 struct btrfs_trans_handle *trans;
9727 unsigned int trans_num_items; 9764 unsigned int trans_num_items;
9728 struct btrfs_root *root = BTRFS_I(old_dir)->root; 9765 struct btrfs_root *root = BTRFS_I(old_dir)->root;
@@ -9779,7 +9816,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9779 9816
9780 /* close the racy window with snapshot create/destroy ioctl */ 9817 /* close the racy window with snapshot create/destroy ioctl */
9781 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) 9818 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9782 down_read(&root->fs_info->subvol_sem); 9819 down_read(&fs_info->subvol_sem);
9783 /* 9820 /*
9784 * We want to reserve the absolute worst case amount of items. So if 9821 * We want to reserve the absolute worst case amount of items. So if
9785 * both inodes are subvols and we need to unlink them then that would 9822 * both inodes are subvols and we need to unlink them then that would
@@ -9810,7 +9847,7 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9810 BTRFS_I(old_inode)->dir_index = 0ULL; 9847 BTRFS_I(old_inode)->dir_index = 0ULL;
9811 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) { 9848 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9812 /* force full log commit if subvolume involved. */ 9849 /* force full log commit if subvolume involved. */
9813 btrfs_set_log_full_commit(root->fs_info, trans); 9850 btrfs_set_log_full_commit(fs_info, trans);
9814 } else { 9851 } else {
9815 btrfs_pin_log_trans(root); 9852 btrfs_pin_log_trans(root);
9816 log_pinned = true; 9853 log_pinned = true;
@@ -9917,12 +9954,12 @@ out_fail:
9917 * allow the tasks to sync it. 9954 * allow the tasks to sync it.
9918 */ 9955 */
9919 if (ret && log_pinned) { 9956 if (ret && log_pinned) {
9920 if (btrfs_inode_in_log(old_dir, root->fs_info->generation) || 9957 if (btrfs_inode_in_log(old_dir, fs_info->generation) ||
9921 btrfs_inode_in_log(new_dir, root->fs_info->generation) || 9958 btrfs_inode_in_log(new_dir, fs_info->generation) ||
9922 btrfs_inode_in_log(old_inode, root->fs_info->generation) || 9959 btrfs_inode_in_log(old_inode, fs_info->generation) ||
9923 (new_inode && 9960 (new_inode &&
9924 btrfs_inode_in_log(new_inode, root->fs_info->generation))) 9961 btrfs_inode_in_log(new_inode, fs_info->generation)))
9925 btrfs_set_log_full_commit(root->fs_info, trans); 9962 btrfs_set_log_full_commit(fs_info, trans);
9926 9963
9927 btrfs_end_log_trans(root); 9964 btrfs_end_log_trans(root);
9928 log_pinned = false; 9965 log_pinned = false;
@@ -9930,7 +9967,7 @@ out_fail:
9930 btrfs_end_transaction(trans, root); 9967 btrfs_end_transaction(trans, root);
9931out_notrans: 9968out_notrans:
9932 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) 9969 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9933 up_read(&root->fs_info->subvol_sem); 9970 up_read(&fs_info->subvol_sem);
9934 9971
9935 return ret; 9972 return ret;
9936} 9973}
@@ -10065,9 +10102,10 @@ out:
10065 10102
10066int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput) 10103int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10067{ 10104{
10105 struct btrfs_fs_info *fs_info = root->fs_info;
10068 int ret; 10106 int ret;
10069 10107
10070 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 10108 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10071 return -EROFS; 10109 return -EROFS;
10072 10110
10073 ret = __start_delalloc_inodes(root, delay_iput, -1); 10111 ret = __start_delalloc_inodes(root, delay_iput, -1);
@@ -10078,14 +10116,14 @@ int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10078 * we have to make sure the IO is actually started and that 10116 * we have to make sure the IO is actually started and that
10079 * ordered extents get created before we return 10117 * ordered extents get created before we return
10080 */ 10118 */
10081 atomic_inc(&root->fs_info->async_submit_draining); 10119 atomic_inc(&fs_info->async_submit_draining);
10082 while (atomic_read(&root->fs_info->nr_async_submits) || 10120 while (atomic_read(&fs_info->nr_async_submits) ||
10083 atomic_read(&root->fs_info->async_delalloc_pages)) { 10121 atomic_read(&fs_info->async_delalloc_pages)) {
10084 wait_event(root->fs_info->async_submit_wait, 10122 wait_event(fs_info->async_submit_wait,
10085 (atomic_read(&root->fs_info->nr_async_submits) == 0 && 10123 (atomic_read(&fs_info->nr_async_submits) == 0 &&
10086 atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 10124 atomic_read(&fs_info->async_delalloc_pages) == 0));
10087 } 10125 }
10088 atomic_dec(&root->fs_info->async_submit_draining); 10126 atomic_dec(&fs_info->async_submit_draining);
10089 return ret; 10127 return ret;
10090} 10128}
10091 10129
@@ -10148,6 +10186,7 @@ out:
10148static int btrfs_symlink(struct inode *dir, struct dentry *dentry, 10186static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10149 const char *symname) 10187 const char *symname)
10150{ 10188{
10189 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10151 struct btrfs_trans_handle *trans; 10190 struct btrfs_trans_handle *trans;
10152 struct btrfs_root *root = BTRFS_I(dir)->root; 10191 struct btrfs_root *root = BTRFS_I(dir)->root;
10153 struct btrfs_path *path; 10192 struct btrfs_path *path;
@@ -10164,7 +10203,7 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10164 struct extent_buffer *leaf; 10203 struct extent_buffer *leaf;
10165 10204
10166 name_len = strlen(symname); 10205 name_len = strlen(symname);
10167 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info)) 10206 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
10168 return -ENAMETOOLONG; 10207 return -ENAMETOOLONG;
10169 10208
10170 /* 10209 /*
@@ -10277,6 +10316,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10277 loff_t actual_len, u64 *alloc_hint, 10316 loff_t actual_len, u64 *alloc_hint,
10278 struct btrfs_trans_handle *trans) 10317 struct btrfs_trans_handle *trans)
10279{ 10318{
10319 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
10280 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 10320 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10281 struct extent_map *em; 10321 struct extent_map *em;
10282 struct btrfs_root *root = BTRFS_I(inode)->root; 10322 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -10316,7 +10356,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10316 btrfs_end_transaction(trans, root); 10356 btrfs_end_transaction(trans, root);
10317 break; 10357 break;
10318 } 10358 }
10319 btrfs_dec_block_group_reservations(root->fs_info, ins.objectid); 10359 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10320 10360
10321 last_alloc = ins.offset; 10361 last_alloc = ins.offset;
10322 ret = insert_reserved_file_extent(trans, inode, 10362 ret = insert_reserved_file_extent(trans, inode,
@@ -10350,7 +10390,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10350 em->block_len = ins.offset; 10390 em->block_len = ins.offset;
10351 em->orig_block_len = ins.offset; 10391 em->orig_block_len = ins.offset;
10352 em->ram_bytes = ins.offset; 10392 em->ram_bytes = ins.offset;
10353 em->bdev = root->fs_info->fs_devices->latest_bdev; 10393 em->bdev = fs_info->fs_devices->latest_bdev;
10354 set_bit(EXTENT_FLAG_PREALLOC, &em->flags); 10394 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10355 em->generation = trans->transid; 10395 em->generation = trans->transid;
10356 10396
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 062f67ac1335..d00e4d3c1baf 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -215,6 +215,7 @@ static int check_flags(unsigned int flags)
215static int btrfs_ioctl_setflags(struct file *file, void __user *arg) 215static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
216{ 216{
217 struct inode *inode = file_inode(file); 217 struct inode *inode = file_inode(file);
218 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
218 struct btrfs_inode *ip = BTRFS_I(inode); 219 struct btrfs_inode *ip = BTRFS_I(inode);
219 struct btrfs_root *root = ip->root; 220 struct btrfs_root *root = ip->root;
220 struct btrfs_trans_handle *trans; 221 struct btrfs_trans_handle *trans;
@@ -324,7 +325,7 @@ static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
324 ip->flags |= BTRFS_INODE_COMPRESS; 325 ip->flags |= BTRFS_INODE_COMPRESS;
325 ip->flags &= ~BTRFS_INODE_NOCOMPRESS; 326 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
326 327
327 if (root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 328 if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
328 comp = "lzo"; 329 comp = "lzo";
329 else 330 else
330 comp = "zlib"; 331 comp = "zlib";
@@ -373,7 +374,8 @@ static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
373 374
374static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) 375static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
375{ 376{
376 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); 377 struct inode *inode = file_inode(file);
378 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
377 struct btrfs_device *device; 379 struct btrfs_device *device;
378 struct request_queue *q; 380 struct request_queue *q;
379 struct fstrim_range range; 381 struct fstrim_range range;
@@ -436,6 +438,7 @@ static noinline int create_subvol(struct inode *dir,
436 u64 *async_transid, 438 u64 *async_transid,
437 struct btrfs_qgroup_inherit *inherit) 439 struct btrfs_qgroup_inherit *inherit)
438{ 440{
441 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
439 struct btrfs_trans_handle *trans; 442 struct btrfs_trans_handle *trans;
440 struct btrfs_key key; 443 struct btrfs_key key;
441 struct btrfs_root_item *root_item; 444 struct btrfs_root_item *root_item;
@@ -458,7 +461,7 @@ static noinline int create_subvol(struct inode *dir,
458 if (!root_item) 461 if (!root_item)
459 return -ENOMEM; 462 return -ENOMEM;
460 463
461 ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); 464 ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid);
462 if (ret) 465 if (ret)
463 goto fail_free; 466 goto fail_free;
464 467
@@ -491,7 +494,7 @@ static noinline int create_subvol(struct inode *dir,
491 trans->block_rsv = &block_rsv; 494 trans->block_rsv = &block_rsv;
492 trans->bytes_reserved = block_rsv.size; 495 trans->bytes_reserved = block_rsv.size;
493 496
494 ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); 497 ret = btrfs_qgroup_inherit(trans, fs_info, 0, objectid, inherit);
495 if (ret) 498 if (ret)
496 goto fail; 499 goto fail;
497 500
@@ -507,9 +510,8 @@ static noinline int create_subvol(struct inode *dir,
507 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); 510 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
508 btrfs_set_header_owner(leaf, objectid); 511 btrfs_set_header_owner(leaf, objectid);
509 512
510 write_extent_buffer_fsid(leaf, root->fs_info->fsid); 513 write_extent_buffer_fsid(leaf, fs_info->fsid);
511 write_extent_buffer_chunk_tree_uuid(leaf, 514 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
512 root->fs_info->chunk_tree_uuid);
513 btrfs_mark_buffer_dirty(leaf); 515 btrfs_mark_buffer_dirty(leaf);
514 516
515 inode_item = &root_item->inode; 517 inode_item = &root_item->inode;
@@ -517,7 +519,7 @@ static noinline int create_subvol(struct inode *dir,
517 btrfs_set_stack_inode_size(inode_item, 3); 519 btrfs_set_stack_inode_size(inode_item, 3);
518 btrfs_set_stack_inode_nlink(inode_item, 1); 520 btrfs_set_stack_inode_nlink(inode_item, 1);
519 btrfs_set_stack_inode_nbytes(inode_item, 521 btrfs_set_stack_inode_nbytes(inode_item,
520 root->fs_info->nodesize); 522 fs_info->nodesize);
521 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); 523 btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
522 524
523 btrfs_set_root_flags(root_item, 0); 525 btrfs_set_root_flags(root_item, 0);
@@ -550,13 +552,13 @@ static noinline int create_subvol(struct inode *dir,
550 key.objectid = objectid; 552 key.objectid = objectid;
551 key.offset = 0; 553 key.offset = 0;
552 key.type = BTRFS_ROOT_ITEM_KEY; 554 key.type = BTRFS_ROOT_ITEM_KEY;
553 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, 555 ret = btrfs_insert_root(trans, fs_info->tree_root, &key,
554 root_item); 556 root_item);
555 if (ret) 557 if (ret)
556 goto fail; 558 goto fail;
557 559
558 key.offset = (u64)-1; 560 key.offset = (u64)-1;
559 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); 561 new_root = btrfs_read_fs_root_no_name(fs_info, &key);
560 if (IS_ERR(new_root)) { 562 if (IS_ERR(new_root)) {
561 ret = PTR_ERR(new_root); 563 ret = PTR_ERR(new_root);
562 btrfs_abort_transaction(trans, ret); 564 btrfs_abort_transaction(trans, ret);
@@ -597,12 +599,12 @@ static noinline int create_subvol(struct inode *dir,
597 ret = btrfs_update_inode(trans, root, dir); 599 ret = btrfs_update_inode(trans, root, dir);
598 BUG_ON(ret); 600 BUG_ON(ret);
599 601
600 ret = btrfs_add_root_ref(trans, root->fs_info, 602 ret = btrfs_add_root_ref(trans, fs_info,
601 objectid, root->root_key.objectid, 603 objectid, root->root_key.objectid,
602 btrfs_ino(dir), index, name, namelen); 604 btrfs_ino(dir), index, name, namelen);
603 BUG_ON(ret); 605 BUG_ON(ret);
604 606
605 ret = btrfs_uuid_tree_add(trans, root->fs_info, root_item->uuid, 607 ret = btrfs_uuid_tree_add(trans, fs_info, root_item->uuid,
606 BTRFS_UUID_KEY_SUBVOL, objectid); 608 BTRFS_UUID_KEY_SUBVOL, objectid);
607 if (ret) 609 if (ret)
608 btrfs_abort_transaction(trans, ret); 610 btrfs_abort_transaction(trans, ret);
@@ -659,6 +661,7 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
659 u64 *async_transid, bool readonly, 661 u64 *async_transid, bool readonly,
660 struct btrfs_qgroup_inherit *inherit) 662 struct btrfs_qgroup_inherit *inherit)
661{ 663{
664 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
662 struct inode *inode; 665 struct inode *inode;
663 struct btrfs_pending_snapshot *pending_snapshot; 666 struct btrfs_pending_snapshot *pending_snapshot;
664 struct btrfs_trans_handle *trans; 667 struct btrfs_trans_handle *trans;
@@ -718,19 +721,19 @@ static int create_snapshot(struct btrfs_root *root, struct inode *dir,
718 goto fail; 721 goto fail;
719 } 722 }
720 723
721 spin_lock(&root->fs_info->trans_lock); 724 spin_lock(&fs_info->trans_lock);
722 list_add(&pending_snapshot->list, 725 list_add(&pending_snapshot->list,
723 &trans->transaction->pending_snapshots); 726 &trans->transaction->pending_snapshots);
724 spin_unlock(&root->fs_info->trans_lock); 727 spin_unlock(&fs_info->trans_lock);
725 if (async_transid) { 728 if (async_transid) {
726 *async_transid = trans->transid; 729 *async_transid = trans->transid;
727 ret = btrfs_commit_transaction_async(trans, 730 ret = btrfs_commit_transaction_async(trans,
728 root->fs_info->extent_root, 1); 731 fs_info->extent_root, 1);
729 if (ret) 732 if (ret)
730 ret = btrfs_commit_transaction(trans, root); 733 ret = btrfs_commit_transaction(trans, root);
731 } else { 734 } else {
732 ret = btrfs_commit_transaction(trans, 735 ret = btrfs_commit_transaction(trans,
733 root->fs_info->extent_root); 736 fs_info->extent_root);
734 } 737 }
735 if (ret) 738 if (ret)
736 goto fail; 739 goto fail;
@@ -839,7 +842,8 @@ static noinline int btrfs_mksubvol(struct path *parent,
839 u64 *async_transid, bool readonly, 842 u64 *async_transid, bool readonly,
840 struct btrfs_qgroup_inherit *inherit) 843 struct btrfs_qgroup_inherit *inherit)
841{ 844{
842 struct inode *dir = d_inode(parent->dentry); 845 struct inode *dir = d_inode(parent->dentry);
846 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
843 struct dentry *dentry; 847 struct dentry *dentry;
844 int error; 848 int error;
845 849
@@ -866,7 +870,7 @@ static noinline int btrfs_mksubvol(struct path *parent,
866 if (error) 870 if (error)
867 goto out_dput; 871 goto out_dput;
868 872
869 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 873 down_read(&fs_info->subvol_sem);
870 874
871 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) 875 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
872 goto out_up_read; 876 goto out_up_read;
@@ -881,7 +885,7 @@ static noinline int btrfs_mksubvol(struct path *parent,
881 if (!error) 885 if (!error)
882 fsnotify_mkdir(dir, dentry); 886 fsnotify_mkdir(dir, dentry);
883out_up_read: 887out_up_read:
884 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); 888 up_read(&fs_info->subvol_sem);
885out_dput: 889out_dput:
886 dput(dentry); 890 dput(dentry);
887out_unlock: 891out_unlock:
@@ -1265,6 +1269,7 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
1265 struct btrfs_ioctl_defrag_range_args *range, 1269 struct btrfs_ioctl_defrag_range_args *range,
1266 u64 newer_than, unsigned long max_to_defrag) 1270 u64 newer_than, unsigned long max_to_defrag)
1267{ 1271{
1272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1268 struct btrfs_root *root = BTRFS_I(inode)->root; 1273 struct btrfs_root *root = BTRFS_I(inode)->root;
1269 struct file_ra_state *ra = NULL; 1274 struct file_ra_state *ra = NULL;
1270 unsigned long last_index; 1275 unsigned long last_index;
@@ -1362,8 +1367,8 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
1362 if (!(inode->i_sb->s_flags & MS_ACTIVE)) 1367 if (!(inode->i_sb->s_flags & MS_ACTIVE))
1363 break; 1368 break;
1364 1369
1365 if (btrfs_defrag_cancelled(root->fs_info)) { 1370 if (btrfs_defrag_cancelled(fs_info)) {
1366 btrfs_debug(root->fs_info, "defrag_file cancelled"); 1371 btrfs_debug(fs_info, "defrag_file cancelled");
1367 ret = -EAGAIN; 1372 ret = -EAGAIN;
1368 break; 1373 break;
1369 } 1374 }
@@ -1451,18 +1456,18 @@ int btrfs_defrag_file(struct inode *inode, struct file *file,
1451 * we have to make sure the IO is actually started and that 1456 * we have to make sure the IO is actually started and that
1452 * ordered extents get created before we return 1457 * ordered extents get created before we return
1453 */ 1458 */
1454 atomic_inc(&root->fs_info->async_submit_draining); 1459 atomic_inc(&fs_info->async_submit_draining);
1455 while (atomic_read(&root->fs_info->nr_async_submits) || 1460 while (atomic_read(&fs_info->nr_async_submits) ||
1456 atomic_read(&root->fs_info->async_delalloc_pages)) { 1461 atomic_read(&fs_info->async_delalloc_pages)) {
1457 wait_event(root->fs_info->async_submit_wait, 1462 wait_event(fs_info->async_submit_wait,
1458 (atomic_read(&root->fs_info->nr_async_submits) == 0 && 1463 (atomic_read(&fs_info->nr_async_submits) == 0 &&
1459 atomic_read(&root->fs_info->async_delalloc_pages) == 0)); 1464 atomic_read(&fs_info->async_delalloc_pages) == 0));
1460 } 1465 }
1461 atomic_dec(&root->fs_info->async_submit_draining); 1466 atomic_dec(&fs_info->async_submit_draining);
1462 } 1467 }
1463 1468
1464 if (range->compress_type == BTRFS_COMPRESS_LZO) { 1469 if (range->compress_type == BTRFS_COMPRESS_LZO) {
1465 btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); 1470 btrfs_set_fs_incompat(fs_info, COMPRESS_LZO);
1466 } 1471 }
1467 1472
1468 ret = defrag_count; 1473 ret = defrag_count;
@@ -1482,10 +1487,12 @@ out_ra:
1482static noinline int btrfs_ioctl_resize(struct file *file, 1487static noinline int btrfs_ioctl_resize(struct file *file,
1483 void __user *arg) 1488 void __user *arg)
1484{ 1489{
1490 struct inode *inode = file_inode(file);
1491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1485 u64 new_size; 1492 u64 new_size;
1486 u64 old_size; 1493 u64 old_size;
1487 u64 devid = 1; 1494 u64 devid = 1;
1488 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 1495 struct btrfs_root *root = BTRFS_I(inode)->root;
1489 struct btrfs_ioctl_vol_args *vol_args; 1496 struct btrfs_ioctl_vol_args *vol_args;
1490 struct btrfs_trans_handle *trans; 1497 struct btrfs_trans_handle *trans;
1491 struct btrfs_device *device = NULL; 1498 struct btrfs_device *device = NULL;
@@ -1502,13 +1509,12 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1502 if (ret) 1509 if (ret)
1503 return ret; 1510 return ret;
1504 1511
1505 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 1512 if (atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
1506 1)) {
1507 mnt_drop_write_file(file); 1513 mnt_drop_write_file(file);
1508 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 1514 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
1509 } 1515 }
1510 1516
1511 mutex_lock(&root->fs_info->volume_mutex); 1517 mutex_lock(&fs_info->volume_mutex);
1512 vol_args = memdup_user(arg, sizeof(*vol_args)); 1518 vol_args = memdup_user(arg, sizeof(*vol_args));
1513 if (IS_ERR(vol_args)) { 1519 if (IS_ERR(vol_args)) {
1514 ret = PTR_ERR(vol_args); 1520 ret = PTR_ERR(vol_args);
@@ -1530,19 +1536,19 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1530 ret = -EINVAL; 1536 ret = -EINVAL;
1531 goto out_free; 1537 goto out_free;
1532 } 1538 }
1533 btrfs_info(root->fs_info, "resizing devid %llu", devid); 1539 btrfs_info(fs_info, "resizing devid %llu", devid);
1534 } 1540 }
1535 1541
1536 device = btrfs_find_device(root->fs_info, devid, NULL, NULL); 1542 device = btrfs_find_device(fs_info, devid, NULL, NULL);
1537 if (!device) { 1543 if (!device) {
1538 btrfs_info(root->fs_info, "resizer unable to find device %llu", 1544 btrfs_info(fs_info, "resizer unable to find device %llu",
1539 devid); 1545 devid);
1540 ret = -ENODEV; 1546 ret = -ENODEV;
1541 goto out_free; 1547 goto out_free;
1542 } 1548 }
1543 1549
1544 if (!device->writeable) { 1550 if (!device->writeable) {
1545 btrfs_info(root->fs_info, 1551 btrfs_info(fs_info,
1546 "resizer unable to apply on readonly device %llu", 1552 "resizer unable to apply on readonly device %llu",
1547 devid); 1553 devid);
1548 ret = -EPERM; 1554 ret = -EPERM;
@@ -1596,11 +1602,11 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1596 goto out_free; 1602 goto out_free;
1597 } 1603 }
1598 1604
1599 new_size = div_u64(new_size, root->fs_info->sectorsize); 1605 new_size = div_u64(new_size, fs_info->sectorsize);
1600 new_size *= root->fs_info->sectorsize; 1606 new_size *= fs_info->sectorsize;
1601 1607
1602 btrfs_info_in_rcu(root->fs_info, "new size for %s is %llu", 1608 btrfs_info_in_rcu(fs_info, "new size for %s is %llu",
1603 rcu_str_deref(device->name), new_size); 1609 rcu_str_deref(device->name), new_size);
1604 1610
1605 if (new_size > old_size) { 1611 if (new_size > old_size) {
1606 trans = btrfs_start_transaction(root, 0); 1612 trans = btrfs_start_transaction(root, 0);
@@ -1617,8 +1623,8 @@ static noinline int btrfs_ioctl_resize(struct file *file,
1617out_free: 1623out_free:
1618 kfree(vol_args); 1624 kfree(vol_args);
1619out: 1625out:
1620 mutex_unlock(&root->fs_info->volume_mutex); 1626 mutex_unlock(&fs_info->volume_mutex);
1621 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 1627 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
1622 mnt_drop_write_file(file); 1628 mnt_drop_write_file(file);
1623 return ret; 1629 return ret;
1624} 1630}
@@ -1771,6 +1777,7 @@ static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1771 void __user *arg) 1777 void __user *arg)
1772{ 1778{
1773 struct inode *inode = file_inode(file); 1779 struct inode *inode = file_inode(file);
1780 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1774 struct btrfs_root *root = BTRFS_I(inode)->root; 1781 struct btrfs_root *root = BTRFS_I(inode)->root;
1775 int ret = 0; 1782 int ret = 0;
1776 u64 flags = 0; 1783 u64 flags = 0;
@@ -1778,10 +1785,10 @@ static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1778 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) 1785 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID)
1779 return -EINVAL; 1786 return -EINVAL;
1780 1787
1781 down_read(&root->fs_info->subvol_sem); 1788 down_read(&fs_info->subvol_sem);
1782 if (btrfs_root_readonly(root)) 1789 if (btrfs_root_readonly(root))
1783 flags |= BTRFS_SUBVOL_RDONLY; 1790 flags |= BTRFS_SUBVOL_RDONLY;
1784 up_read(&root->fs_info->subvol_sem); 1791 up_read(&fs_info->subvol_sem);
1785 1792
1786 if (copy_to_user(arg, &flags, sizeof(flags))) 1793 if (copy_to_user(arg, &flags, sizeof(flags)))
1787 ret = -EFAULT; 1794 ret = -EFAULT;
@@ -1793,6 +1800,7 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1793 void __user *arg) 1800 void __user *arg)
1794{ 1801{
1795 struct inode *inode = file_inode(file); 1802 struct inode *inode = file_inode(file);
1803 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1796 struct btrfs_root *root = BTRFS_I(inode)->root; 1804 struct btrfs_root *root = BTRFS_I(inode)->root;
1797 struct btrfs_trans_handle *trans; 1805 struct btrfs_trans_handle *trans;
1798 u64 root_flags; 1806 u64 root_flags;
@@ -1826,7 +1834,7 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1826 goto out_drop_write; 1834 goto out_drop_write;
1827 } 1835 }
1828 1836
1829 down_write(&root->fs_info->subvol_sem); 1837 down_write(&fs_info->subvol_sem);
1830 1838
1831 /* nothing to do */ 1839 /* nothing to do */
1832 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) 1840 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
@@ -1848,9 +1856,9 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1848 spin_unlock(&root->root_item_lock); 1856 spin_unlock(&root->root_item_lock);
1849 } else { 1857 } else {
1850 spin_unlock(&root->root_item_lock); 1858 spin_unlock(&root->root_item_lock);
1851 btrfs_warn(root->fs_info, 1859 btrfs_warn(fs_info,
1852 "Attempt to set subvolume %llu read-write during send", 1860 "Attempt to set subvolume %llu read-write during send",
1853 root->root_key.objectid); 1861 root->root_key.objectid);
1854 ret = -EPERM; 1862 ret = -EPERM;
1855 goto out_drop_sem; 1863 goto out_drop_sem;
1856 } 1864 }
@@ -1862,7 +1870,7 @@ static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1862 goto out_reset; 1870 goto out_reset;
1863 } 1871 }
1864 1872
1865 ret = btrfs_update_root(trans, root->fs_info->tree_root, 1873 ret = btrfs_update_root(trans, fs_info->tree_root,
1866 &root->root_key, &root->root_item); 1874 &root->root_key, &root->root_item);
1867 1875
1868 btrfs_commit_transaction(trans, root); 1876 btrfs_commit_transaction(trans, root);
@@ -1870,7 +1878,7 @@ out_reset:
1870 if (ret) 1878 if (ret)
1871 btrfs_set_root_flags(&root->root_item, root_flags); 1879 btrfs_set_root_flags(&root->root_item, root_flags);
1872out_drop_sem: 1880out_drop_sem:
1873 up_write(&root->fs_info->subvol_sem); 1881 up_write(&fs_info->subvol_sem);
1874out_drop_write: 1882out_drop_write:
1875 mnt_drop_write_file(file); 1883 mnt_drop_write_file(file);
1876out: 1884out:
@@ -1882,6 +1890,7 @@ out:
1882 */ 1890 */
1883static noinline int may_destroy_subvol(struct btrfs_root *root) 1891static noinline int may_destroy_subvol(struct btrfs_root *root)
1884{ 1892{
1893 struct btrfs_fs_info *fs_info = root->fs_info;
1885 struct btrfs_path *path; 1894 struct btrfs_path *path;
1886 struct btrfs_dir_item *di; 1895 struct btrfs_dir_item *di;
1887 struct btrfs_key key; 1896 struct btrfs_key key;
@@ -1893,14 +1902,14 @@ static noinline int may_destroy_subvol(struct btrfs_root *root)
1893 return -ENOMEM; 1902 return -ENOMEM;
1894 1903
1895 /* Make sure this root isn't set as the default subvol */ 1904 /* Make sure this root isn't set as the default subvol */
1896 dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 1905 dir_id = btrfs_super_root_dir(fs_info->super_copy);
1897 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path, 1906 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
1898 dir_id, "default", 7, 0); 1907 dir_id, "default", 7, 0);
1899 if (di && !IS_ERR(di)) { 1908 if (di && !IS_ERR(di)) {
1900 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); 1909 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
1901 if (key.objectid == root->root_key.objectid) { 1910 if (key.objectid == root->root_key.objectid) {
1902 ret = -EPERM; 1911 ret = -EPERM;
1903 btrfs_err(root->fs_info, 1912 btrfs_err(fs_info,
1904 "deleting default subvolume %llu is not allowed", 1913 "deleting default subvolume %llu is not allowed",
1905 key.objectid); 1914 key.objectid);
1906 goto out; 1915 goto out;
@@ -1912,8 +1921,7 @@ static noinline int may_destroy_subvol(struct btrfs_root *root)
1912 key.type = BTRFS_ROOT_REF_KEY; 1921 key.type = BTRFS_ROOT_REF_KEY;
1913 key.offset = (u64)-1; 1922 key.offset = (u64)-1;
1914 1923
1915 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, 1924 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
1916 &key, path, 0, 0);
1917 if (ret < 0) 1925 if (ret < 0)
1918 goto out; 1926 goto out;
1919 BUG_ON(ret == 0); 1927 BUG_ON(ret == 0);
@@ -2084,10 +2092,10 @@ static noinline int search_ioctl(struct inode *inode,
2084 size_t *buf_size, 2092 size_t *buf_size,
2085 char __user *ubuf) 2093 char __user *ubuf)
2086{ 2094{
2095 struct btrfs_fs_info *info = btrfs_sb(inode->i_sb);
2087 struct btrfs_root *root; 2096 struct btrfs_root *root;
2088 struct btrfs_key key; 2097 struct btrfs_key key;
2089 struct btrfs_path *path; 2098 struct btrfs_path *path;
2090 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
2091 int ret; 2099 int ret;
2092 int num_found = 0; 2100 int num_found = 0;
2093 unsigned long sk_offset = 0; 2101 unsigned long sk_offset = 0;
@@ -2350,6 +2358,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2350 void __user *arg) 2358 void __user *arg)
2351{ 2359{
2352 struct dentry *parent = file->f_path.dentry; 2360 struct dentry *parent = file->f_path.dentry;
2361 struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb);
2353 struct dentry *dentry; 2362 struct dentry *dentry;
2354 struct inode *dir = d_inode(parent); 2363 struct inode *dir = d_inode(parent);
2355 struct inode *inode; 2364 struct inode *inode;
@@ -2415,7 +2424,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2415 * rmdir(2). 2424 * rmdir(2).
2416 */ 2425 */
2417 err = -EPERM; 2426 err = -EPERM;
2418 if (!btrfs_test_opt(root->fs_info, USER_SUBVOL_RM_ALLOWED)) 2427 if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED))
2419 goto out_dput; 2428 goto out_dput;
2420 2429
2421 /* 2430 /*
@@ -2459,14 +2468,14 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2459 spin_unlock(&dest->root_item_lock); 2468 spin_unlock(&dest->root_item_lock);
2460 } else { 2469 } else {
2461 spin_unlock(&dest->root_item_lock); 2470 spin_unlock(&dest->root_item_lock);
2462 btrfs_warn(root->fs_info, 2471 btrfs_warn(fs_info,
2463 "Attempt to delete subvolume %llu during send", 2472 "Attempt to delete subvolume %llu during send",
2464 dest->root_key.objectid); 2473 dest->root_key.objectid);
2465 err = -EPERM; 2474 err = -EPERM;
2466 goto out_unlock_inode; 2475 goto out_unlock_inode;
2467 } 2476 }
2468 2477
2469 down_write(&root->fs_info->subvol_sem); 2478 down_write(&fs_info->subvol_sem);
2470 2479
2471 err = may_destroy_subvol(dest); 2480 err = may_destroy_subvol(dest);
2472 if (err) 2481 if (err)
@@ -2511,7 +2520,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2511 2520
2512 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) { 2521 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
2513 ret = btrfs_insert_orphan_item(trans, 2522 ret = btrfs_insert_orphan_item(trans,
2514 root->fs_info->tree_root, 2523 fs_info->tree_root,
2515 dest->root_key.objectid); 2524 dest->root_key.objectid);
2516 if (ret) { 2525 if (ret) {
2517 btrfs_abort_transaction(trans, ret); 2526 btrfs_abort_transaction(trans, ret);
@@ -2520,7 +2529,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2520 } 2529 }
2521 } 2530 }
2522 2531
2523 ret = btrfs_uuid_tree_rem(trans, root->fs_info, dest->root_item.uuid, 2532 ret = btrfs_uuid_tree_rem(trans, fs_info, dest->root_item.uuid,
2524 BTRFS_UUID_KEY_SUBVOL, 2533 BTRFS_UUID_KEY_SUBVOL,
2525 dest->root_key.objectid); 2534 dest->root_key.objectid);
2526 if (ret && ret != -ENOENT) { 2535 if (ret && ret != -ENOENT) {
@@ -2529,7 +2538,7 @@ static noinline int btrfs_ioctl_snap_destroy(struct file *file,
2529 goto out_end_trans; 2538 goto out_end_trans;
2530 } 2539 }
2531 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) { 2540 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
2532 ret = btrfs_uuid_tree_rem(trans, root->fs_info, 2541 ret = btrfs_uuid_tree_rem(trans, fs_info,
2533 dest->root_item.received_uuid, 2542 dest->root_item.received_uuid,
2534 BTRFS_UUID_KEY_RECEIVED_SUBVOL, 2543 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
2535 dest->root_key.objectid); 2544 dest->root_key.objectid);
@@ -2550,7 +2559,7 @@ out_end_trans:
2550out_release: 2559out_release:
2551 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); 2560 btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved);
2552out_up_write: 2561out_up_write:
2553 up_write(&root->fs_info->subvol_sem); 2562 up_write(&fs_info->subvol_sem);
2554 if (err) { 2563 if (err) {
2555 spin_lock(&dest->root_item_lock); 2564 spin_lock(&dest->root_item_lock);
2556 root_flags = btrfs_root_flags(&dest->root_item); 2565 root_flags = btrfs_root_flags(&dest->root_item);
@@ -2654,18 +2663,17 @@ out:
2654 2663
2655static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) 2664static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2656{ 2665{
2666 struct btrfs_fs_info *fs_info = root->fs_info;
2657 struct btrfs_ioctl_vol_args *vol_args; 2667 struct btrfs_ioctl_vol_args *vol_args;
2658 int ret; 2668 int ret;
2659 2669
2660 if (!capable(CAP_SYS_ADMIN)) 2670 if (!capable(CAP_SYS_ADMIN))
2661 return -EPERM; 2671 return -EPERM;
2662 2672
2663 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2673 if (atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1))
2664 1)) {
2665 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2674 return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2666 }
2667 2675
2668 mutex_lock(&root->fs_info->volume_mutex); 2676 mutex_lock(&fs_info->volume_mutex);
2669 vol_args = memdup_user(arg, sizeof(*vol_args)); 2677 vol_args = memdup_user(arg, sizeof(*vol_args));
2670 if (IS_ERR(vol_args)) { 2678 if (IS_ERR(vol_args)) {
2671 ret = PTR_ERR(vol_args); 2679 ret = PTR_ERR(vol_args);
@@ -2676,18 +2684,20 @@ static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
2676 ret = btrfs_init_new_device(root->fs_info, vol_args->name); 2684 ret = btrfs_init_new_device(root->fs_info, vol_args->name);
2677 2685
2678 if (!ret) 2686 if (!ret)
2679 btrfs_info(root->fs_info, "disk added %s",vol_args->name); 2687 btrfs_info(fs_info, "disk added %s", vol_args->name);
2680 2688
2681 kfree(vol_args); 2689 kfree(vol_args);
2682out: 2690out:
2683 mutex_unlock(&root->fs_info->volume_mutex); 2691 mutex_unlock(&fs_info->volume_mutex);
2684 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2692 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
2685 return ret; 2693 return ret;
2686} 2694}
2687 2695
2688static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) 2696static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
2689{ 2697{
2690 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2698 struct inode *inode = file_inode(file);
2699 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2700 struct btrfs_root *root = BTRFS_I(inode)->root;
2691 struct btrfs_ioctl_vol_args_v2 *vol_args; 2701 struct btrfs_ioctl_vol_args_v2 *vol_args;
2692 int ret; 2702 int ret;
2693 2703
@@ -2708,28 +2718,27 @@ static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg)
2708 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) 2718 if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED)
2709 return -EOPNOTSUPP; 2719 return -EOPNOTSUPP;
2710 2720
2711 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2721 if (atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
2712 1)) {
2713 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2722 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2714 goto out; 2723 goto out;
2715 } 2724 }
2716 2725
2717 mutex_lock(&root->fs_info->volume_mutex); 2726 mutex_lock(&fs_info->volume_mutex);
2718 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { 2727 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) {
2719 ret = btrfs_rm_device(root, NULL, vol_args->devid); 2728 ret = btrfs_rm_device(root, NULL, vol_args->devid);
2720 } else { 2729 } else {
2721 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; 2730 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
2722 ret = btrfs_rm_device(root, vol_args->name, 0); 2731 ret = btrfs_rm_device(root, vol_args->name, 0);
2723 } 2732 }
2724 mutex_unlock(&root->fs_info->volume_mutex); 2733 mutex_unlock(&fs_info->volume_mutex);
2725 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2734 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
2726 2735
2727 if (!ret) { 2736 if (!ret) {
2728 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) 2737 if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID)
2729 btrfs_info(root->fs_info, "device deleted: id %llu", 2738 btrfs_info(fs_info, "device deleted: id %llu",
2730 vol_args->devid); 2739 vol_args->devid);
2731 else 2740 else
2732 btrfs_info(root->fs_info, "device deleted: %s", 2741 btrfs_info(fs_info, "device deleted: %s",
2733 vol_args->name); 2742 vol_args->name);
2734 } 2743 }
2735out: 2744out:
@@ -2741,7 +2750,9 @@ err_drop:
2741 2750
2742static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) 2751static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2743{ 2752{
2744 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 2753 struct inode *inode = file_inode(file);
2754 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2755 struct btrfs_root *root = BTRFS_I(inode)->root;
2745 struct btrfs_ioctl_vol_args *vol_args; 2756 struct btrfs_ioctl_vol_args *vol_args;
2746 int ret; 2757 int ret;
2747 2758
@@ -2752,8 +2763,7 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2752 if (ret) 2763 if (ret)
2753 return ret; 2764 return ret;
2754 2765
2755 if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, 2766 if (atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) {
2756 1)) {
2757 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 2767 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
2758 goto out_drop_write; 2768 goto out_drop_write;
2759 } 2769 }
@@ -2765,15 +2775,15 @@ static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg)
2765 } 2775 }
2766 2776
2767 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; 2777 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
2768 mutex_lock(&root->fs_info->volume_mutex); 2778 mutex_lock(&fs_info->volume_mutex);
2769 ret = btrfs_rm_device(root, vol_args->name, 0); 2779 ret = btrfs_rm_device(root, vol_args->name, 0);
2770 mutex_unlock(&root->fs_info->volume_mutex); 2780 mutex_unlock(&fs_info->volume_mutex);
2771 2781
2772 if (!ret) 2782 if (!ret)
2773 btrfs_info(root->fs_info, "disk deleted %s",vol_args->name); 2783 btrfs_info(fs_info, "disk deleted %s", vol_args->name);
2774 kfree(vol_args); 2784 kfree(vol_args);
2775out: 2785out:
2776 atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); 2786 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
2777out_drop_write: 2787out_drop_write:
2778 mnt_drop_write_file(file); 2788 mnt_drop_write_file(file);
2779 2789
@@ -2782,9 +2792,10 @@ out_drop_write:
2782 2792
2783static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) 2793static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2784{ 2794{
2795 struct btrfs_fs_info *fs_info = root->fs_info;
2785 struct btrfs_ioctl_fs_info_args *fi_args; 2796 struct btrfs_ioctl_fs_info_args *fi_args;
2786 struct btrfs_device *device; 2797 struct btrfs_device *device;
2787 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2798 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2788 int ret = 0; 2799 int ret = 0;
2789 2800
2790 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); 2801 fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL);
@@ -2793,7 +2804,7 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2793 2804
2794 mutex_lock(&fs_devices->device_list_mutex); 2805 mutex_lock(&fs_devices->device_list_mutex);
2795 fi_args->num_devices = fs_devices->num_devices; 2806 fi_args->num_devices = fs_devices->num_devices;
2796 memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); 2807 memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid));
2797 2808
2798 list_for_each_entry(device, &fs_devices->devices, dev_list) { 2809 list_for_each_entry(device, &fs_devices->devices, dev_list) {
2799 if (device->devid > fi_args->max_id) 2810 if (device->devid > fi_args->max_id)
@@ -2801,9 +2812,9 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2801 } 2812 }
2802 mutex_unlock(&fs_devices->device_list_mutex); 2813 mutex_unlock(&fs_devices->device_list_mutex);
2803 2814
2804 fi_args->nodesize = root->fs_info->super_copy->nodesize; 2815 fi_args->nodesize = fs_info->super_copy->nodesize;
2805 fi_args->sectorsize = root->fs_info->super_copy->sectorsize; 2816 fi_args->sectorsize = fs_info->super_copy->sectorsize;
2806 fi_args->clone_alignment = root->fs_info->super_copy->sectorsize; 2817 fi_args->clone_alignment = fs_info->super_copy->sectorsize;
2807 2818
2808 if (copy_to_user(arg, fi_args, sizeof(*fi_args))) 2819 if (copy_to_user(arg, fi_args, sizeof(*fi_args)))
2809 ret = -EFAULT; 2820 ret = -EFAULT;
@@ -2814,9 +2825,10 @@ static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg)
2814 2825
2815static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) 2826static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2816{ 2827{
2828 struct btrfs_fs_info *fs_info = root->fs_info;
2817 struct btrfs_ioctl_dev_info_args *di_args; 2829 struct btrfs_ioctl_dev_info_args *di_args;
2818 struct btrfs_device *dev; 2830 struct btrfs_device *dev;
2819 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2831 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2820 int ret = 0; 2832 int ret = 0;
2821 char *s_uuid = NULL; 2833 char *s_uuid = NULL;
2822 2834
@@ -2828,7 +2840,7 @@ static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg)
2828 s_uuid = di_args->uuid; 2840 s_uuid = di_args->uuid;
2829 2841
2830 mutex_lock(&fs_devices->device_list_mutex); 2842 mutex_lock(&fs_devices->device_list_mutex);
2831 dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); 2843 dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL);
2832 2844
2833 if (!dev) { 2845 if (!dev) {
2834 ret = -ENODEV; 2846 ret = -ENODEV;
@@ -3403,9 +3415,10 @@ static int clone_copy_inline_extent(struct inode *src,
3403 const u64 size, 3415 const u64 size,
3404 char *inline_data) 3416 char *inline_data)
3405{ 3417{
3418 struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb);
3406 struct btrfs_root *root = BTRFS_I(dst)->root; 3419 struct btrfs_root *root = BTRFS_I(dst)->root;
3407 const u64 aligned_end = ALIGN(new_key->offset + datal, 3420 const u64 aligned_end = ALIGN(new_key->offset + datal,
3408 root->fs_info->sectorsize); 3421 fs_info->sectorsize);
3409 int ret; 3422 int ret;
3410 struct btrfs_key key; 3423 struct btrfs_key key;
3411 3424
@@ -3526,6 +3539,7 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
3526 const u64 off, const u64 olen, const u64 olen_aligned, 3539 const u64 off, const u64 olen, const u64 olen_aligned,
3527 const u64 destoff, int no_time_update) 3540 const u64 destoff, int no_time_update)
3528{ 3541{
3542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3529 struct btrfs_root *root = BTRFS_I(inode)->root; 3543 struct btrfs_root *root = BTRFS_I(inode)->root;
3530 struct btrfs_path *path = NULL; 3544 struct btrfs_path *path = NULL;
3531 struct extent_buffer *leaf; 3545 struct extent_buffer *leaf;
@@ -3539,10 +3553,9 @@ static int btrfs_clone(struct inode *src, struct inode *inode,
3539 u64 last_dest_end = destoff; 3553 u64 last_dest_end = destoff;
3540 3554
3541 ret = -ENOMEM; 3555 ret = -ENOMEM;
3542 buf = kmalloc(root->fs_info->nodesize, 3556 buf = kmalloc(fs_info->nodesize, GFP_KERNEL | __GFP_NOWARN);
3543 GFP_KERNEL | __GFP_NOWARN);
3544 if (!buf) { 3557 if (!buf) {
3545 buf = vmalloc(root->fs_info->nodesize); 3558 buf = vmalloc(fs_info->nodesize);
3546 if (!buf) 3559 if (!buf)
3547 return ret; 3560 return ret;
3548 } 3561 }
@@ -3800,7 +3813,7 @@ process_slot:
3800 btrfs_release_path(path); 3813 btrfs_release_path(path);
3801 3814
3802 last_dest_end = ALIGN(new_key.offset + datal, 3815 last_dest_end = ALIGN(new_key.offset + datal,
3803 root->fs_info->sectorsize); 3816 fs_info->sectorsize);
3804 ret = clone_finish_inode_update(trans, inode, 3817 ret = clone_finish_inode_update(trans, inode,
3805 last_dest_end, 3818 last_dest_end,
3806 destoff, olen, 3819 destoff, olen,
@@ -3861,10 +3874,11 @@ static noinline int btrfs_clone_files(struct file *file, struct file *file_src,
3861{ 3874{
3862 struct inode *inode = file_inode(file); 3875 struct inode *inode = file_inode(file);
3863 struct inode *src = file_inode(file_src); 3876 struct inode *src = file_inode(file_src);
3877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3864 struct btrfs_root *root = BTRFS_I(inode)->root; 3878 struct btrfs_root *root = BTRFS_I(inode)->root;
3865 int ret; 3879 int ret;
3866 u64 len = olen; 3880 u64 len = olen;
3867 u64 bs = root->fs_info->sb->s_blocksize; 3881 u64 bs = fs_info->sb->s_blocksize;
3868 int same_inode = src == inode; 3882 int same_inode = src == inode;
3869 3883
3870 /* 3884 /*
@@ -4005,6 +4019,7 @@ int btrfs_clone_file_range(struct file *src_file, loff_t off,
4005static long btrfs_ioctl_trans_start(struct file *file) 4019static long btrfs_ioctl_trans_start(struct file *file)
4006{ 4020{
4007 struct inode *inode = file_inode(file); 4021 struct inode *inode = file_inode(file);
4022 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4008 struct btrfs_root *root = BTRFS_I(inode)->root; 4023 struct btrfs_root *root = BTRFS_I(inode)->root;
4009 struct btrfs_trans_handle *trans; 4024 struct btrfs_trans_handle *trans;
4010 int ret; 4025 int ret;
@@ -4025,7 +4040,7 @@ static long btrfs_ioctl_trans_start(struct file *file)
4025 if (ret) 4040 if (ret)
4026 goto out; 4041 goto out;
4027 4042
4028 atomic_inc(&root->fs_info->open_ioctl_trans); 4043 atomic_inc(&fs_info->open_ioctl_trans);
4029 4044
4030 ret = -ENOMEM; 4045 ret = -ENOMEM;
4031 trans = btrfs_start_ioctl_transaction(root); 4046 trans = btrfs_start_ioctl_transaction(root);
@@ -4036,7 +4051,7 @@ static long btrfs_ioctl_trans_start(struct file *file)
4036 return 0; 4051 return 0;
4037 4052
4038out_drop: 4053out_drop:
4039 atomic_dec(&root->fs_info->open_ioctl_trans); 4054 atomic_dec(&fs_info->open_ioctl_trans);
4040 mnt_drop_write_file(file); 4055 mnt_drop_write_file(file);
4041out: 4056out:
4042 return ret; 4057 return ret;
@@ -4045,6 +4060,7 @@ out:
4045static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) 4060static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4046{ 4061{
4047 struct inode *inode = file_inode(file); 4062 struct inode *inode = file_inode(file);
4063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4048 struct btrfs_root *root = BTRFS_I(inode)->root; 4064 struct btrfs_root *root = BTRFS_I(inode)->root;
4049 struct btrfs_root *new_root; 4065 struct btrfs_root *new_root;
4050 struct btrfs_dir_item *di; 4066 struct btrfs_dir_item *di;
@@ -4075,7 +4091,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4075 location.type = BTRFS_ROOT_ITEM_KEY; 4091 location.type = BTRFS_ROOT_ITEM_KEY;
4076 location.offset = (u64)-1; 4092 location.offset = (u64)-1;
4077 4093
4078 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); 4094 new_root = btrfs_read_fs_root_no_name(fs_info, &location);
4079 if (IS_ERR(new_root)) { 4095 if (IS_ERR(new_root)) {
4080 ret = PTR_ERR(new_root); 4096 ret = PTR_ERR(new_root);
4081 goto out; 4097 goto out;
@@ -4095,13 +4111,13 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4095 goto out; 4111 goto out;
4096 } 4112 }
4097 4113
4098 dir_id = btrfs_super_root_dir(root->fs_info->super_copy); 4114 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4099 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, 4115 di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path,
4100 dir_id, "default", 7, 1); 4116 dir_id, "default", 7, 1);
4101 if (IS_ERR_OR_NULL(di)) { 4117 if (IS_ERR_OR_NULL(di)) {
4102 btrfs_free_path(path); 4118 btrfs_free_path(path);
4103 btrfs_end_transaction(trans, root); 4119 btrfs_end_transaction(trans, root);
4104 btrfs_err(new_root->fs_info, 4120 btrfs_err(fs_info,
4105 "Umm, you don't have the default diritem, this isn't going to work"); 4121 "Umm, you don't have the default diritem, this isn't going to work");
4106 ret = -ENOENT; 4122 ret = -ENOENT;
4107 goto out; 4123 goto out;
@@ -4112,7 +4128,7 @@ static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
4112 btrfs_mark_buffer_dirty(path->nodes[0]); 4128 btrfs_mark_buffer_dirty(path->nodes[0]);
4113 btrfs_free_path(path); 4129 btrfs_free_path(path);
4114 4130
4115 btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); 4131 btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL);
4116 btrfs_end_transaction(trans, root); 4132 btrfs_end_transaction(trans, root);
4117out: 4133out:
4118 mnt_drop_write_file(file); 4134 mnt_drop_write_file(file);
@@ -4137,6 +4153,7 @@ void btrfs_get_block_group_info(struct list_head *groups_list,
4137 4153
4138static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) 4154static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
4139{ 4155{
4156 struct btrfs_fs_info *fs_info = root->fs_info;
4140 struct btrfs_ioctl_space_args space_args; 4157 struct btrfs_ioctl_space_args space_args;
4141 struct btrfs_ioctl_space_info space; 4158 struct btrfs_ioctl_space_info space;
4142 struct btrfs_ioctl_space_info *dest; 4159 struct btrfs_ioctl_space_info *dest;
@@ -4163,7 +4180,7 @@ static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
4163 4180
4164 info = NULL; 4181 info = NULL;
4165 rcu_read_lock(); 4182 rcu_read_lock();
4166 list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 4183 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4167 list) { 4184 list) {
4168 if (tmp->flags == types[i]) { 4185 if (tmp->flags == types[i]) {
4169 info = tmp; 4186 info = tmp;
@@ -4219,7 +4236,7 @@ static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
4219 4236
4220 info = NULL; 4237 info = NULL;
4221 rcu_read_lock(); 4238 rcu_read_lock();
4222 list_for_each_entry_rcu(tmp, &root->fs_info->space_info, 4239 list_for_each_entry_rcu(tmp, &fs_info->space_info,
4223 list) { 4240 list) {
4224 if (tmp->flags == types[i]) { 4241 if (tmp->flags == types[i]) {
4225 info = tmp; 4242 info = tmp;
@@ -4250,7 +4267,7 @@ static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
4250 * Add global block reserve 4267 * Add global block reserve
4251 */ 4268 */
4252 if (slot_count) { 4269 if (slot_count) {
4253 struct btrfs_block_rsv *block_rsv = &root->fs_info->global_block_rsv; 4270 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4254 4271
4255 spin_lock(&block_rsv->lock); 4272 spin_lock(&block_rsv->lock);
4256 space.total_bytes = block_rsv->size; 4273 space.total_bytes = block_rsv->size;
@@ -4345,7 +4362,7 @@ static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root,
4345 4362
4346static long btrfs_ioctl_scrub(struct file *file, void __user *arg) 4363static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4347{ 4364{
4348 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4365 struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb);
4349 struct btrfs_ioctl_scrub_args *sa; 4366 struct btrfs_ioctl_scrub_args *sa;
4350 int ret; 4367 int ret;
4351 4368
@@ -4362,7 +4379,7 @@ static long btrfs_ioctl_scrub(struct file *file, void __user *arg)
4362 goto out; 4379 goto out;
4363 } 4380 }
4364 4381
4365 ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, 4382 ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end,
4366 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, 4383 &sa->progress, sa->flags & BTRFS_SCRUB_READONLY,
4367 0); 4384 0);
4368 4385
@@ -4432,6 +4449,7 @@ static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root,
4432 4449
4433static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) 4450static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4434{ 4451{
4452 struct btrfs_fs_info *fs_info = root->fs_info;
4435 struct btrfs_ioctl_dev_replace_args *p; 4453 struct btrfs_ioctl_dev_replace_args *p;
4436 int ret; 4454 int ret;
4437 4455
@@ -4444,27 +4462,25 @@ static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg)
4444 4462
4445 switch (p->cmd) { 4463 switch (p->cmd) {
4446 case BTRFS_IOCTL_DEV_REPLACE_CMD_START: 4464 case BTRFS_IOCTL_DEV_REPLACE_CMD_START:
4447 if (root->fs_info->sb->s_flags & MS_RDONLY) { 4465 if (fs_info->sb->s_flags & MS_RDONLY) {
4448 ret = -EROFS; 4466 ret = -EROFS;
4449 goto out; 4467 goto out;
4450 } 4468 }
4451 if (atomic_xchg( 4469 if (atomic_xchg(
4452 &root->fs_info->mutually_exclusive_operation_running, 4470 &fs_info->mutually_exclusive_operation_running, 1)) {
4453 1)) {
4454 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; 4471 ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS;
4455 } else { 4472 } else {
4456 ret = btrfs_dev_replace_by_ioctl(root, p); 4473 ret = btrfs_dev_replace_by_ioctl(root, p);
4457 atomic_set( 4474 atomic_set(
4458 &root->fs_info->mutually_exclusive_operation_running, 4475 &fs_info->mutually_exclusive_operation_running, 0);
4459 0);
4460 } 4476 }
4461 break; 4477 break;
4462 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: 4478 case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS:
4463 btrfs_dev_replace_status(root->fs_info, p); 4479 btrfs_dev_replace_status(fs_info, p);
4464 ret = 0; 4480 ret = 0;
4465 break; 4481 break;
4466 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: 4482 case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL:
4467 ret = btrfs_dev_replace_cancel(root->fs_info, p); 4483 ret = btrfs_dev_replace_cancel(fs_info, p);
4468 break; 4484 break;
4469 default: 4485 default:
4470 ret = -EINVAL; 4486 ret = -EINVAL;
@@ -4785,14 +4801,16 @@ out:
4785 4801
4786static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) 4802static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd)
4787{ 4803{
4804 struct btrfs_fs_info *fs_info = root->fs_info;
4805
4788 if (!capable(CAP_SYS_ADMIN)) 4806 if (!capable(CAP_SYS_ADMIN))
4789 return -EPERM; 4807 return -EPERM;
4790 4808
4791 switch (cmd) { 4809 switch (cmd) {
4792 case BTRFS_BALANCE_CTL_PAUSE: 4810 case BTRFS_BALANCE_CTL_PAUSE:
4793 return btrfs_pause_balance(root->fs_info); 4811 return btrfs_pause_balance(fs_info);
4794 case BTRFS_BALANCE_CTL_CANCEL: 4812 case BTRFS_BALANCE_CTL_CANCEL:
4795 return btrfs_cancel_balance(root->fs_info); 4813 return btrfs_cancel_balance(fs_info);
4796 } 4814 }
4797 4815
4798 return -EINVAL; 4816 return -EINVAL;
@@ -4833,7 +4851,8 @@ out:
4833 4851
4834static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) 4852static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4835{ 4853{
4836 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4854 struct inode *inode = file_inode(file);
4855 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4837 struct btrfs_ioctl_quota_ctl_args *sa; 4856 struct btrfs_ioctl_quota_ctl_args *sa;
4838 struct btrfs_trans_handle *trans = NULL; 4857 struct btrfs_trans_handle *trans = NULL;
4839 int ret; 4858 int ret;
@@ -4852,8 +4871,8 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4852 goto drop_write; 4871 goto drop_write;
4853 } 4872 }
4854 4873
4855 down_write(&root->fs_info->subvol_sem); 4874 down_write(&fs_info->subvol_sem);
4856 trans = btrfs_start_transaction(root->fs_info->tree_root, 2); 4875 trans = btrfs_start_transaction(fs_info->tree_root, 2);
4857 if (IS_ERR(trans)) { 4876 if (IS_ERR(trans)) {
4858 ret = PTR_ERR(trans); 4877 ret = PTR_ERR(trans);
4859 goto out; 4878 goto out;
@@ -4861,22 +4880,22 @@ static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg)
4861 4880
4862 switch (sa->cmd) { 4881 switch (sa->cmd) {
4863 case BTRFS_QUOTA_CTL_ENABLE: 4882 case BTRFS_QUOTA_CTL_ENABLE:
4864 ret = btrfs_quota_enable(trans, root->fs_info); 4883 ret = btrfs_quota_enable(trans, fs_info);
4865 break; 4884 break;
4866 case BTRFS_QUOTA_CTL_DISABLE: 4885 case BTRFS_QUOTA_CTL_DISABLE:
4867 ret = btrfs_quota_disable(trans, root->fs_info); 4886 ret = btrfs_quota_disable(trans, fs_info);
4868 break; 4887 break;
4869 default: 4888 default:
4870 ret = -EINVAL; 4889 ret = -EINVAL;
4871 break; 4890 break;
4872 } 4891 }
4873 4892
4874 err = btrfs_commit_transaction(trans, root->fs_info->tree_root); 4893 err = btrfs_commit_transaction(trans, fs_info->tree_root);
4875 if (err && !ret) 4894 if (err && !ret)
4876 ret = err; 4895 ret = err;
4877out: 4896out:
4878 kfree(sa); 4897 kfree(sa);
4879 up_write(&root->fs_info->subvol_sem); 4898 up_write(&fs_info->subvol_sem);
4880drop_write: 4899drop_write:
4881 mnt_drop_write_file(file); 4900 mnt_drop_write_file(file);
4882 return ret; 4901 return ret;
@@ -4884,7 +4903,9 @@ drop_write:
4884 4903
4885static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) 4904static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4886{ 4905{
4887 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4906 struct inode *inode = file_inode(file);
4907 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4908 struct btrfs_root *root = BTRFS_I(inode)->root;
4888 struct btrfs_ioctl_qgroup_assign_args *sa; 4909 struct btrfs_ioctl_qgroup_assign_args *sa;
4889 struct btrfs_trans_handle *trans; 4910 struct btrfs_trans_handle *trans;
4890 int ret; 4911 int ret;
@@ -4911,18 +4932,18 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4911 4932
4912 /* FIXME: check if the IDs really exist */ 4933 /* FIXME: check if the IDs really exist */
4913 if (sa->assign) { 4934 if (sa->assign) {
4914 ret = btrfs_add_qgroup_relation(trans, root->fs_info, 4935 ret = btrfs_add_qgroup_relation(trans, fs_info,
4915 sa->src, sa->dst); 4936 sa->src, sa->dst);
4916 } else { 4937 } else {
4917 ret = btrfs_del_qgroup_relation(trans, root->fs_info, 4938 ret = btrfs_del_qgroup_relation(trans, fs_info,
4918 sa->src, sa->dst); 4939 sa->src, sa->dst);
4919 } 4940 }
4920 4941
4921 /* update qgroup status and info */ 4942 /* update qgroup status and info */
4922 err = btrfs_run_qgroups(trans, root->fs_info); 4943 err = btrfs_run_qgroups(trans, fs_info);
4923 if (err < 0) 4944 if (err < 0)
4924 btrfs_handle_fs_error(root->fs_info, err, 4945 btrfs_handle_fs_error(fs_info, err,
4925 "failed to update qgroup status and info"); 4946 "failed to update qgroup status and info");
4926 err = btrfs_end_transaction(trans, root); 4947 err = btrfs_end_transaction(trans, root);
4927 if (err && !ret) 4948 if (err && !ret)
4928 ret = err; 4949 ret = err;
@@ -4936,7 +4957,9 @@ drop_write:
4936 4957
4937static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) 4958static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4938{ 4959{
4939 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 4960 struct inode *inode = file_inode(file);
4961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4962 struct btrfs_root *root = BTRFS_I(inode)->root;
4940 struct btrfs_ioctl_qgroup_create_args *sa; 4963 struct btrfs_ioctl_qgroup_create_args *sa;
4941 struct btrfs_trans_handle *trans; 4964 struct btrfs_trans_handle *trans;
4942 int ret; 4965 int ret;
@@ -4968,9 +4991,9 @@ static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg)
4968 4991
4969 /* FIXME: check if the IDs really exist */ 4992 /* FIXME: check if the IDs really exist */
4970 if (sa->create) { 4993 if (sa->create) {
4971 ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid); 4994 ret = btrfs_create_qgroup(trans, fs_info, sa->qgroupid);
4972 } else { 4995 } else {
4973 ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); 4996 ret = btrfs_remove_qgroup(trans, fs_info, sa->qgroupid);
4974 } 4997 }
4975 4998
4976 err = btrfs_end_transaction(trans, root); 4999 err = btrfs_end_transaction(trans, root);
@@ -4986,7 +5009,9 @@ drop_write:
4986 5009
4987static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) 5010static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
4988{ 5011{
4989 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5012 struct inode *inode = file_inode(file);
5013 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5014 struct btrfs_root *root = BTRFS_I(inode)->root;
4990 struct btrfs_ioctl_qgroup_limit_args *sa; 5015 struct btrfs_ioctl_qgroup_limit_args *sa;
4991 struct btrfs_trans_handle *trans; 5016 struct btrfs_trans_handle *trans;
4992 int ret; 5017 int ret;
@@ -5019,7 +5044,7 @@ static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg)
5019 } 5044 }
5020 5045
5021 /* FIXME: check if the IDs really exist */ 5046 /* FIXME: check if the IDs really exist */
5022 ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); 5047 ret = btrfs_limit_qgroup(trans, fs_info, qgroupid, &sa->lim);
5023 5048
5024 err = btrfs_end_transaction(trans, root); 5049 err = btrfs_end_transaction(trans, root);
5025 if (err && !ret) 5050 if (err && !ret)
@@ -5034,7 +5059,8 @@ drop_write:
5034 5059
5035static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) 5060static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5036{ 5061{
5037 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5062 struct inode *inode = file_inode(file);
5063 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5038 struct btrfs_ioctl_quota_rescan_args *qsa; 5064 struct btrfs_ioctl_quota_rescan_args *qsa;
5039 int ret; 5065 int ret;
5040 5066
@@ -5056,7 +5082,7 @@ static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg)
5056 goto out; 5082 goto out;
5057 } 5083 }
5058 5084
5059 ret = btrfs_qgroup_rescan(root->fs_info); 5085 ret = btrfs_qgroup_rescan(fs_info);
5060 5086
5061out: 5087out:
5062 kfree(qsa); 5088 kfree(qsa);
@@ -5067,7 +5093,8 @@ drop_write:
5067 5093
5068static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) 5094static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5069{ 5095{
5070 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5096 struct inode *inode = file_inode(file);
5097 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5071 struct btrfs_ioctl_quota_rescan_args *qsa; 5098 struct btrfs_ioctl_quota_rescan_args *qsa;
5072 int ret = 0; 5099 int ret = 0;
5073 5100
@@ -5078,9 +5105,9 @@ static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5078 if (!qsa) 5105 if (!qsa)
5079 return -ENOMEM; 5106 return -ENOMEM;
5080 5107
5081 if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { 5108 if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) {
5082 qsa->flags = 1; 5109 qsa->flags = 1;
5083 qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; 5110 qsa->progress = fs_info->qgroup_rescan_progress.objectid;
5084 } 5111 }
5085 5112
5086 if (copy_to_user(arg, qsa, sizeof(*qsa))) 5113 if (copy_to_user(arg, qsa, sizeof(*qsa)))
@@ -5092,18 +5119,20 @@ static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg)
5092 5119
5093static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) 5120static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg)
5094{ 5121{
5095 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5122 struct inode *inode = file_inode(file);
5123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5096 5124
5097 if (!capable(CAP_SYS_ADMIN)) 5125 if (!capable(CAP_SYS_ADMIN))
5098 return -EPERM; 5126 return -EPERM;
5099 5127
5100 return btrfs_qgroup_wait_for_completion(root->fs_info, true); 5128 return btrfs_qgroup_wait_for_completion(fs_info, true);
5101} 5129}
5102 5130
5103static long _btrfs_ioctl_set_received_subvol(struct file *file, 5131static long _btrfs_ioctl_set_received_subvol(struct file *file,
5104 struct btrfs_ioctl_received_subvol_args *sa) 5132 struct btrfs_ioctl_received_subvol_args *sa)
5105{ 5133{
5106 struct inode *inode = file_inode(file); 5134 struct inode *inode = file_inode(file);
5135 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5107 struct btrfs_root *root = BTRFS_I(inode)->root; 5136 struct btrfs_root *root = BTRFS_I(inode)->root;
5108 struct btrfs_root_item *root_item = &root->root_item; 5137 struct btrfs_root_item *root_item = &root->root_item;
5109 struct btrfs_trans_handle *trans; 5138 struct btrfs_trans_handle *trans;
@@ -5118,7 +5147,7 @@ static long _btrfs_ioctl_set_received_subvol(struct file *file,
5118 if (ret < 0) 5147 if (ret < 0)
5119 return ret; 5148 return ret;
5120 5149
5121 down_write(&root->fs_info->subvol_sem); 5150 down_write(&fs_info->subvol_sem);
5122 5151
5123 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { 5152 if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) {
5124 ret = -EINVAL; 5153 ret = -EINVAL;
@@ -5149,8 +5178,7 @@ static long _btrfs_ioctl_set_received_subvol(struct file *file,
5149 BTRFS_UUID_SIZE); 5178 BTRFS_UUID_SIZE);
5150 if (received_uuid_changed && 5179 if (received_uuid_changed &&
5151 !btrfs_is_empty_uuid(root_item->received_uuid)) 5180 !btrfs_is_empty_uuid(root_item->received_uuid))
5152 btrfs_uuid_tree_rem(trans, root->fs_info, 5181 btrfs_uuid_tree_rem(trans, fs_info, root_item->received_uuid,
5153 root_item->received_uuid,
5154 BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5182 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5155 root->root_key.objectid); 5183 root->root_key.objectid);
5156 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); 5184 memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE);
@@ -5161,14 +5189,14 @@ static long _btrfs_ioctl_set_received_subvol(struct file *file,
5161 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); 5189 btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec);
5162 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); 5190 btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec);
5163 5191
5164 ret = btrfs_update_root(trans, root->fs_info->tree_root, 5192 ret = btrfs_update_root(trans, fs_info->tree_root,
5165 &root->root_key, &root->root_item); 5193 &root->root_key, &root->root_item);
5166 if (ret < 0) { 5194 if (ret < 0) {
5167 btrfs_end_transaction(trans, root); 5195 btrfs_end_transaction(trans, root);
5168 goto out; 5196 goto out;
5169 } 5197 }
5170 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { 5198 if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) {
5171 ret = btrfs_uuid_tree_add(trans, root->fs_info, sa->uuid, 5199 ret = btrfs_uuid_tree_add(trans, fs_info, sa->uuid,
5172 BTRFS_UUID_KEY_RECEIVED_SUBVOL, 5200 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
5173 root->root_key.objectid); 5201 root->root_key.objectid);
5174 if (ret < 0 && ret != -EEXIST) { 5202 if (ret < 0 && ret != -EEXIST) {
@@ -5183,7 +5211,7 @@ static long _btrfs_ioctl_set_received_subvol(struct file *file,
5183 } 5211 }
5184 5212
5185out: 5213out:
5186 up_write(&root->fs_info->subvol_sem); 5214 up_write(&fs_info->subvol_sem);
5187 mnt_drop_write_file(file); 5215 mnt_drop_write_file(file);
5188 return ret; 5216 return ret;
5189} 5217}
@@ -5265,20 +5293,22 @@ out:
5265 5293
5266static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) 5294static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5267{ 5295{
5268 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5296 struct inode *inode = file_inode(file);
5297 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5269 size_t len; 5298 size_t len;
5270 int ret; 5299 int ret;
5271 char label[BTRFS_LABEL_SIZE]; 5300 char label[BTRFS_LABEL_SIZE];
5272 5301
5273 spin_lock(&root->fs_info->super_lock); 5302 spin_lock(&fs_info->super_lock);
5274 memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); 5303 memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE);
5275 spin_unlock(&root->fs_info->super_lock); 5304 spin_unlock(&fs_info->super_lock);
5276 5305
5277 len = strnlen(label, BTRFS_LABEL_SIZE); 5306 len = strnlen(label, BTRFS_LABEL_SIZE);
5278 5307
5279 if (len == BTRFS_LABEL_SIZE) { 5308 if (len == BTRFS_LABEL_SIZE) {
5280 btrfs_warn(root->fs_info, 5309 btrfs_warn(fs_info,
5281 "label is too long, return the first %zu bytes", --len); 5310 "label is too long, return the first %zu bytes",
5311 --len);
5282 } 5312 }
5283 5313
5284 ret = copy_to_user(arg, label, len); 5314 ret = copy_to_user(arg, label, len);
@@ -5288,8 +5318,10 @@ static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg)
5288 5318
5289static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) 5319static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5290{ 5320{
5291 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5321 struct inode *inode = file_inode(file);
5292 struct btrfs_super_block *super_block = root->fs_info->super_copy; 5322 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5323 struct btrfs_root *root = BTRFS_I(inode)->root;
5324 struct btrfs_super_block *super_block = fs_info->super_copy;
5293 struct btrfs_trans_handle *trans; 5325 struct btrfs_trans_handle *trans;
5294 char label[BTRFS_LABEL_SIZE]; 5326 char label[BTRFS_LABEL_SIZE];
5295 int ret; 5327 int ret;
@@ -5301,7 +5333,7 @@ static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5301 return -EFAULT; 5333 return -EFAULT;
5302 5334
5303 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { 5335 if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) {
5304 btrfs_err(root->fs_info, 5336 btrfs_err(fs_info,
5305 "unable to set label with more than %d bytes", 5337 "unable to set label with more than %d bytes",
5306 BTRFS_LABEL_SIZE - 1); 5338 BTRFS_LABEL_SIZE - 1);
5307 return -EINVAL; 5339 return -EINVAL;
@@ -5317,9 +5349,9 @@ static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg)
5317 goto out_unlock; 5349 goto out_unlock;
5318 } 5350 }
5319 5351
5320 spin_lock(&root->fs_info->super_lock); 5352 spin_lock(&fs_info->super_lock);
5321 strcpy(super_block->label, label); 5353 strcpy(super_block->label, label);
5322 spin_unlock(&root->fs_info->super_lock); 5354 spin_unlock(&fs_info->super_lock);
5323 ret = btrfs_commit_transaction(trans, root); 5355 ret = btrfs_commit_transaction(trans, root);
5324 5356
5325out_unlock: 5357out_unlock:
@@ -5348,8 +5380,9 @@ int btrfs_ioctl_get_supported_features(void __user *arg)
5348 5380
5349static int btrfs_ioctl_get_features(struct file *file, void __user *arg) 5381static int btrfs_ioctl_get_features(struct file *file, void __user *arg)
5350{ 5382{
5351 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5383 struct inode *inode = file_inode(file);
5352 struct btrfs_super_block *super_block = root->fs_info->super_copy; 5384 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5385 struct btrfs_super_block *super_block = fs_info->super_copy;
5353 struct btrfs_ioctl_feature_flags features; 5386 struct btrfs_ioctl_feature_flags features;
5354 5387
5355 features.compat_flags = btrfs_super_compat_flags(super_block); 5388 features.compat_flags = btrfs_super_compat_flags(super_block);
@@ -5367,6 +5400,7 @@ static int check_feature_bits(struct btrfs_root *root,
5367 u64 change_mask, u64 flags, u64 supported_flags, 5400 u64 change_mask, u64 flags, u64 supported_flags,
5368 u64 safe_set, u64 safe_clear) 5401 u64 safe_set, u64 safe_clear)
5369{ 5402{
5403 struct btrfs_fs_info *fs_info = root->fs_info;
5370 const char *type = btrfs_feature_set_names[set]; 5404 const char *type = btrfs_feature_set_names[set];
5371 char *names; 5405 char *names;
5372 u64 disallowed, unsupported; 5406 u64 disallowed, unsupported;
@@ -5377,14 +5411,14 @@ static int check_feature_bits(struct btrfs_root *root,
5377 if (unsupported) { 5411 if (unsupported) {
5378 names = btrfs_printable_features(set, unsupported); 5412 names = btrfs_printable_features(set, unsupported);
5379 if (names) { 5413 if (names) {
5380 btrfs_warn(root->fs_info, 5414 btrfs_warn(fs_info,
5381 "this kernel does not support the %s feature bit%s", 5415 "this kernel does not support the %s feature bit%s",
5382 names, strchr(names, ',') ? "s" : ""); 5416 names, strchr(names, ',') ? "s" : "");
5383 kfree(names); 5417 kfree(names);
5384 } else 5418 } else
5385 btrfs_warn(root->fs_info, 5419 btrfs_warn(fs_info,
5386 "this kernel does not support %s bits 0x%llx", 5420 "this kernel does not support %s bits 0x%llx",
5387 type, unsupported); 5421 type, unsupported);
5388 return -EOPNOTSUPP; 5422 return -EOPNOTSUPP;
5389 } 5423 }
5390 5424
@@ -5392,14 +5426,14 @@ static int check_feature_bits(struct btrfs_root *root,
5392 if (disallowed) { 5426 if (disallowed) {
5393 names = btrfs_printable_features(set, disallowed); 5427 names = btrfs_printable_features(set, disallowed);
5394 if (names) { 5428 if (names) {
5395 btrfs_warn(root->fs_info, 5429 btrfs_warn(fs_info,
5396 "can't set the %s feature bit%s while mounted", 5430 "can't set the %s feature bit%s while mounted",
5397 names, strchr(names, ',') ? "s" : ""); 5431 names, strchr(names, ',') ? "s" : "");
5398 kfree(names); 5432 kfree(names);
5399 } else 5433 } else
5400 btrfs_warn(root->fs_info, 5434 btrfs_warn(fs_info,
5401 "can't set %s bits 0x%llx while mounted", 5435 "can't set %s bits 0x%llx while mounted",
5402 type, disallowed); 5436 type, disallowed);
5403 return -EPERM; 5437 return -EPERM;
5404 } 5438 }
5405 5439
@@ -5407,14 +5441,14 @@ static int check_feature_bits(struct btrfs_root *root,
5407 if (disallowed) { 5441 if (disallowed) {
5408 names = btrfs_printable_features(set, disallowed); 5442 names = btrfs_printable_features(set, disallowed);
5409 if (names) { 5443 if (names) {
5410 btrfs_warn(root->fs_info, 5444 btrfs_warn(fs_info,
5411 "can't clear the %s feature bit%s while mounted", 5445 "can't clear the %s feature bit%s while mounted",
5412 names, strchr(names, ',') ? "s" : ""); 5446 names, strchr(names, ',') ? "s" : "");
5413 kfree(names); 5447 kfree(names);
5414 } else 5448 } else
5415 btrfs_warn(root->fs_info, 5449 btrfs_warn(fs_info,
5416 "can't clear %s bits 0x%llx while mounted", 5450 "can't clear %s bits 0x%llx while mounted",
5417 type, disallowed); 5451 type, disallowed);
5418 return -EPERM; 5452 return -EPERM;
5419 } 5453 }
5420 5454
@@ -5429,8 +5463,10 @@ check_feature_bits(root, FEAT_##mask_base, change_mask, flags, \
5429 5463
5430static int btrfs_ioctl_set_features(struct file *file, void __user *arg) 5464static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5431{ 5465{
5432 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5466 struct inode *inode = file_inode(file);
5433 struct btrfs_super_block *super_block = root->fs_info->super_copy; 5467 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5468 struct btrfs_root *root = BTRFS_I(inode)->root;
5469 struct btrfs_super_block *super_block = fs_info->super_copy;
5434 struct btrfs_ioctl_feature_flags flags[2]; 5470 struct btrfs_ioctl_feature_flags flags[2];
5435 struct btrfs_trans_handle *trans; 5471 struct btrfs_trans_handle *trans;
5436 u64 newflags; 5472 u64 newflags;
@@ -5472,7 +5508,7 @@ static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5472 goto out_drop_write; 5508 goto out_drop_write;
5473 } 5509 }
5474 5510
5475 spin_lock(&root->fs_info->super_lock); 5511 spin_lock(&fs_info->super_lock);
5476 newflags = btrfs_super_compat_flags(super_block); 5512 newflags = btrfs_super_compat_flags(super_block);
5477 newflags |= flags[0].compat_flags & flags[1].compat_flags; 5513 newflags |= flags[0].compat_flags & flags[1].compat_flags;
5478 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); 5514 newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags);
@@ -5487,7 +5523,7 @@ static int btrfs_ioctl_set_features(struct file *file, void __user *arg)
5487 newflags |= flags[0].incompat_flags & flags[1].incompat_flags; 5523 newflags |= flags[0].incompat_flags & flags[1].incompat_flags;
5488 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); 5524 newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags);
5489 btrfs_set_super_incompat_flags(super_block, newflags); 5525 btrfs_set_super_incompat_flags(super_block, newflags);
5490 spin_unlock(&root->fs_info->super_lock); 5526 spin_unlock(&fs_info->super_lock);
5491 5527
5492 ret = btrfs_commit_transaction(trans, root); 5528 ret = btrfs_commit_transaction(trans, root);
5493out_drop_write: 5529out_drop_write:
@@ -5499,7 +5535,9 @@ out_drop_write:
5499long btrfs_ioctl(struct file *file, unsigned int 5535long btrfs_ioctl(struct file *file, unsigned int
5500 cmd, unsigned long arg) 5536 cmd, unsigned long arg)
5501{ 5537{
5502 struct btrfs_root *root = BTRFS_I(file_inode(file))->root; 5538 struct inode *inode = file_inode(file);
5539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5540 struct btrfs_root *root = BTRFS_I(inode)->root;
5503 void __user *argp = (void __user *)arg; 5541 void __user *argp = (void __user *)arg;
5504 5542
5505 switch (cmd) { 5543 switch (cmd) {
@@ -5564,16 +5602,16 @@ long btrfs_ioctl(struct file *file, unsigned int
5564 case BTRFS_IOC_SYNC: { 5602 case BTRFS_IOC_SYNC: {
5565 int ret; 5603 int ret;
5566 5604
5567 ret = btrfs_start_delalloc_roots(root->fs_info, 0, -1); 5605 ret = btrfs_start_delalloc_roots(fs_info, 0, -1);
5568 if (ret) 5606 if (ret)
5569 return ret; 5607 return ret;
5570 ret = btrfs_sync_fs(file_inode(file)->i_sb, 1); 5608 ret = btrfs_sync_fs(inode->i_sb, 1);
5571 /* 5609 /*
5572 * The transaction thread may want to do more work, 5610 * The transaction thread may want to do more work,
5573 * namely it pokes the cleaner kthread that will start 5611 * namely it pokes the cleaner kthread that will start
5574 * processing uncleaned subvols. 5612 * processing uncleaned subvols.
5575 */ 5613 */
5576 wake_up_process(root->fs_info->transaction_kthread); 5614 wake_up_process(fs_info->transaction_kthread);
5577 return ret; 5615 return ret;
5578 } 5616 }
5579 case BTRFS_IOC_START_SYNC: 5617 case BTRFS_IOC_START_SYNC:
diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index 763bdfca4c2b..041c3326d109 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -186,6 +186,7 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
186 u64 start, u64 len, u64 disk_len, 186 u64 start, u64 len, u64 disk_len,
187 int type, int dio, int compress_type) 187 int type, int dio, int compress_type)
188{ 188{
189 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
189 struct btrfs_root *root = BTRFS_I(inode)->root; 190 struct btrfs_root *root = BTRFS_I(inode)->root;
190 struct btrfs_ordered_inode_tree *tree; 191 struct btrfs_ordered_inode_tree *tree;
191 struct rb_node *node; 192 struct rb_node *node;
@@ -234,11 +235,10 @@ static int __btrfs_add_ordered_extent(struct inode *inode, u64 file_offset,
234 &root->ordered_extents); 235 &root->ordered_extents);
235 root->nr_ordered_extents++; 236 root->nr_ordered_extents++;
236 if (root->nr_ordered_extents == 1) { 237 if (root->nr_ordered_extents == 1) {
237 spin_lock(&root->fs_info->ordered_root_lock); 238 spin_lock(&fs_info->ordered_root_lock);
238 BUG_ON(!list_empty(&root->ordered_root)); 239 BUG_ON(!list_empty(&root->ordered_root));
239 list_add_tail(&root->ordered_root, 240 list_add_tail(&root->ordered_root, &fs_info->ordered_roots);
240 &root->fs_info->ordered_roots); 241 spin_unlock(&fs_info->ordered_root_lock);
241 spin_unlock(&root->fs_info->ordered_root_lock);
242 } 242 }
243 spin_unlock(&root->ordered_extent_lock); 243 spin_unlock(&root->ordered_extent_lock);
244 244
@@ -303,6 +303,7 @@ int btrfs_dec_test_first_ordered_pending(struct inode *inode,
303 struct btrfs_ordered_extent **cached, 303 struct btrfs_ordered_extent **cached,
304 u64 *file_offset, u64 io_size, int uptodate) 304 u64 *file_offset, u64 io_size, int uptodate)
305{ 305{
306 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
306 struct btrfs_ordered_inode_tree *tree; 307 struct btrfs_ordered_inode_tree *tree;
307 struct rb_node *node; 308 struct rb_node *node;
308 struct btrfs_ordered_extent *entry = NULL; 309 struct btrfs_ordered_extent *entry = NULL;
@@ -331,14 +332,14 @@ int btrfs_dec_test_first_ordered_pending(struct inode *inode,
331 entry->len); 332 entry->len);
332 *file_offset = dec_end; 333 *file_offset = dec_end;
333 if (dec_start > dec_end) { 334 if (dec_start > dec_end) {
334 btrfs_crit(BTRFS_I(inode)->root->fs_info, 335 btrfs_crit(fs_info, "bad ordering dec_start %llu end %llu",
335 "bad ordering dec_start %llu end %llu", dec_start, dec_end); 336 dec_start, dec_end);
336 } 337 }
337 to_dec = dec_end - dec_start; 338 to_dec = dec_end - dec_start;
338 if (to_dec > entry->bytes_left) { 339 if (to_dec > entry->bytes_left) {
339 btrfs_crit(BTRFS_I(inode)->root->fs_info, 340 btrfs_crit(fs_info,
340 "bad ordered accounting left %llu size %llu", 341 "bad ordered accounting left %llu size %llu",
341 entry->bytes_left, to_dec); 342 entry->bytes_left, to_dec);
342 } 343 }
343 entry->bytes_left -= to_dec; 344 entry->bytes_left -= to_dec;
344 if (!uptodate) 345 if (!uptodate)
@@ -588,6 +589,7 @@ void btrfs_put_ordered_extent(struct btrfs_ordered_extent *entry)
588void btrfs_remove_ordered_extent(struct inode *inode, 589void btrfs_remove_ordered_extent(struct inode *inode,
589 struct btrfs_ordered_extent *entry) 590 struct btrfs_ordered_extent *entry)
590{ 591{
592 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
591 struct btrfs_ordered_inode_tree *tree; 593 struct btrfs_ordered_inode_tree *tree;
592 struct btrfs_root *root = BTRFS_I(inode)->root; 594 struct btrfs_root *root = BTRFS_I(inode)->root;
593 struct rb_node *node; 595 struct rb_node *node;
@@ -618,11 +620,11 @@ void btrfs_remove_ordered_extent(struct inode *inode,
618 * lock, so be nice and check if trans is set, but ASSERT() so 620 * lock, so be nice and check if trans is set, but ASSERT() so
619 * if it isn't set a developer will notice. 621 * if it isn't set a developer will notice.
620 */ 622 */
621 spin_lock(&root->fs_info->trans_lock); 623 spin_lock(&fs_info->trans_lock);
622 trans = root->fs_info->running_transaction; 624 trans = fs_info->running_transaction;
623 if (trans) 625 if (trans)
624 atomic_inc(&trans->use_count); 626 atomic_inc(&trans->use_count);
625 spin_unlock(&root->fs_info->trans_lock); 627 spin_unlock(&fs_info->trans_lock);
626 628
627 ASSERT(trans); 629 ASSERT(trans);
628 if (trans) { 630 if (trans) {
@@ -639,10 +641,10 @@ void btrfs_remove_ordered_extent(struct inode *inode,
639 trace_btrfs_ordered_extent_remove(inode, entry); 641 trace_btrfs_ordered_extent_remove(inode, entry);
640 642
641 if (!root->nr_ordered_extents) { 643 if (!root->nr_ordered_extents) {
642 spin_lock(&root->fs_info->ordered_root_lock); 644 spin_lock(&fs_info->ordered_root_lock);
643 BUG_ON(list_empty(&root->ordered_root)); 645 BUG_ON(list_empty(&root->ordered_root));
644 list_del_init(&root->ordered_root); 646 list_del_init(&root->ordered_root);
645 spin_unlock(&root->fs_info->ordered_root_lock); 647 spin_unlock(&fs_info->ordered_root_lock);
646 } 648 }
647 spin_unlock(&root->ordered_extent_lock); 649 spin_unlock(&root->ordered_extent_lock);
648 wake_up(&entry->wait); 650 wake_up(&entry->wait);
@@ -664,6 +666,7 @@ static void btrfs_run_ordered_extent_work(struct btrfs_work *work)
664int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr, 666int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr,
665 const u64 range_start, const u64 range_len) 667 const u64 range_start, const u64 range_len)
666{ 668{
669 struct btrfs_fs_info *fs_info = root->fs_info;
667 LIST_HEAD(splice); 670 LIST_HEAD(splice);
668 LIST_HEAD(skipped); 671 LIST_HEAD(skipped);
669 LIST_HEAD(works); 672 LIST_HEAD(works);
@@ -694,8 +697,7 @@ int btrfs_wait_ordered_extents(struct btrfs_root *root, int nr,
694 btrfs_flush_delalloc_helper, 697 btrfs_flush_delalloc_helper,
695 btrfs_run_ordered_extent_work, NULL, NULL); 698 btrfs_run_ordered_extent_work, NULL, NULL);
696 list_add_tail(&ordered->work_list, &works); 699 list_add_tail(&ordered->work_list, &works);
697 btrfs_queue_work(root->fs_info->flush_workers, 700 btrfs_queue_work(fs_info->flush_workers, &ordered->flush_work);
698 &ordered->flush_work);
699 701
700 cond_resched(); 702 cond_resched();
701 spin_lock(&root->ordered_extent_lock); 703 spin_lock(&root->ordered_extent_lock);
diff --git a/fs/btrfs/print-tree.c b/fs/btrfs/print-tree.c
index 57c403b205a5..3251a0dd03a2 100644
--- a/fs/btrfs/print-tree.c
+++ b/fs/btrfs/print-tree.c
@@ -163,6 +163,7 @@ static void print_uuid_item(struct extent_buffer *l, unsigned long offset,
163 163
164void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l) 164void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
165{ 165{
166 struct btrfs_fs_info *fs_info = root->fs_info;
166 int i; 167 int i;
167 u32 type, nr; 168 u32 type, nr;
168 struct btrfs_item *item; 169 struct btrfs_item *item;
@@ -182,7 +183,7 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
182 183
183 nr = btrfs_header_nritems(l); 184 nr = btrfs_header_nritems(l);
184 185
185 btrfs_info(root->fs_info, "leaf %llu total ptrs %d free space %d", 186 btrfs_info(fs_info, "leaf %llu total ptrs %d free space %d",
186 btrfs_header_bytenr(l), nr, btrfs_leaf_free_space(root, l)); 187 btrfs_header_bytenr(l), nr, btrfs_leaf_free_space(root, l));
187 for (i = 0 ; i < nr ; i++) { 188 for (i = 0 ; i < nr ; i++) {
188 item = btrfs_item_nr(i); 189 item = btrfs_item_nr(i);
@@ -316,6 +317,7 @@ void btrfs_print_leaf(struct btrfs_root *root, struct extent_buffer *l)
316 317
317void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c) 318void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
318{ 319{
320 struct btrfs_fs_info *fs_info = root->fs_info;
319 int i; u32 nr; 321 int i; u32 nr;
320 struct btrfs_key key; 322 struct btrfs_key key;
321 int level; 323 int level;
@@ -328,10 +330,10 @@ void btrfs_print_tree(struct btrfs_root *root, struct extent_buffer *c)
328 btrfs_print_leaf(root, c); 330 btrfs_print_leaf(root, c);
329 return; 331 return;
330 } 332 }
331 btrfs_info(root->fs_info, 333 btrfs_info(fs_info,
332 "node %llu level %d total ptrs %d free spc %u", 334 "node %llu level %d total ptrs %d free spc %u",
333 btrfs_header_bytenr(c), level, nr, 335 btrfs_header_bytenr(c), level, nr,
334 (u32)BTRFS_NODEPTRS_PER_BLOCK(root->fs_info) - nr); 336 (u32)BTRFS_NODEPTRS_PER_BLOCK(fs_info) - nr);
335 for (i = 0; i < nr; i++) { 337 for (i = 0; i < nr; i++) {
336 btrfs_node_key_to_cpu(c, &key, i); 338 btrfs_node_key_to_cpu(c, &key, i);
337 pr_info("\tkey %d (%llu %u %llu) block %llu\n", 339 pr_info("\tkey %d (%llu %u %llu) block %llu\n",
diff --git a/fs/btrfs/qgroup.c b/fs/btrfs/qgroup.c
index 1fd9bef8cf3b..eb389b1452ae 100644
--- a/fs/btrfs/qgroup.c
+++ b/fs/btrfs/qgroup.c
@@ -1019,7 +1019,7 @@ int btrfs_quota_disable(struct btrfs_trans_handle *trans,
1019 list_del(&quota_root->dirty_list); 1019 list_del(&quota_root->dirty_list);
1020 1020
1021 btrfs_tree_lock(quota_root->node); 1021 btrfs_tree_lock(quota_root->node);
1022 clean_tree_block(trans, tree_root->fs_info, quota_root->node); 1022 clean_tree_block(trans, fs_info, quota_root->node);
1023 btrfs_tree_unlock(quota_root->node); 1023 btrfs_tree_unlock(quota_root->node);
1024 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1); 1024 btrfs_free_tree_block(trans, quota_root, quota_root->node, 0, 1);
1025 1025
@@ -1192,7 +1192,7 @@ int btrfs_add_qgroup_relation(struct btrfs_trans_handle *trans,
1192 } 1192 }
1193 1193
1194 spin_lock(&fs_info->qgroup_lock); 1194 spin_lock(&fs_info->qgroup_lock);
1195 ret = add_relation_rb(quota_root->fs_info, src, dst); 1195 ret = add_relation_rb(fs_info, src, dst);
1196 if (ret < 0) { 1196 if (ret < 0) {
1197 spin_unlock(&fs_info->qgroup_lock); 1197 spin_unlock(&fs_info->qgroup_lock);
1198 goto out; 1198 goto out;
@@ -1340,7 +1340,7 @@ int btrfs_remove_qgroup(struct btrfs_trans_handle *trans,
1340 } 1340 }
1341 1341
1342 spin_lock(&fs_info->qgroup_lock); 1342 spin_lock(&fs_info->qgroup_lock);
1343 del_qgroup_rb(quota_root->fs_info, qgroupid); 1343 del_qgroup_rb(fs_info, qgroupid);
1344 spin_unlock(&fs_info->qgroup_lock); 1344 spin_unlock(&fs_info->qgroup_lock);
1345out: 1345out:
1346 mutex_unlock(&fs_info->qgroup_ioctl_lock); 1346 mutex_unlock(&fs_info->qgroup_ioctl_lock);
@@ -1521,6 +1521,7 @@ int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1521 struct btrfs_root *root, 1521 struct btrfs_root *root,
1522 struct extent_buffer *eb) 1522 struct extent_buffer *eb)
1523{ 1523{
1524 struct btrfs_fs_info *fs_info = root->fs_info;
1524 int nr = btrfs_header_nritems(eb); 1525 int nr = btrfs_header_nritems(eb);
1525 int i, extent_type, ret; 1526 int i, extent_type, ret;
1526 struct btrfs_key key; 1527 struct btrfs_key key;
@@ -1528,7 +1529,7 @@ int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1528 u64 bytenr, num_bytes; 1529 u64 bytenr, num_bytes;
1529 1530
1530 /* We can be called directly from walk_up_proc() */ 1531 /* We can be called directly from walk_up_proc() */
1531 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags)) 1532 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1532 return 0; 1533 return 0;
1533 1534
1534 for (i = 0; i < nr; i++) { 1535 for (i = 0; i < nr; i++) {
@@ -1550,8 +1551,8 @@ int btrfs_qgroup_trace_leaf_items(struct btrfs_trans_handle *trans,
1550 1551
1551 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi); 1552 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
1552 1553
1553 ret = btrfs_qgroup_trace_extent(trans, root->fs_info, 1554 ret = btrfs_qgroup_trace_extent(trans, fs_info, bytenr,
1554 bytenr, num_bytes, GFP_NOFS); 1555 num_bytes, GFP_NOFS);
1555 if (ret) 1556 if (ret)
1556 return ret; 1557 return ret;
1557 } 1558 }
@@ -1625,6 +1626,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1625 struct extent_buffer *root_eb, 1626 struct extent_buffer *root_eb,
1626 u64 root_gen, int root_level) 1627 u64 root_gen, int root_level)
1627{ 1628{
1629 struct btrfs_fs_info *fs_info = root->fs_info;
1628 int ret = 0; 1630 int ret = 0;
1629 int level; 1631 int level;
1630 struct extent_buffer *eb = root_eb; 1632 struct extent_buffer *eb = root_eb;
@@ -1633,7 +1635,7 @@ int btrfs_qgroup_trace_subtree(struct btrfs_trans_handle *trans,
1633 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL); 1635 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
1634 BUG_ON(root_eb == NULL); 1636 BUG_ON(root_eb == NULL);
1635 1637
1636 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags)) 1638 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags))
1637 return 0; 1639 return 0;
1638 1640
1639 if (!extent_buffer_uptodate(root_eb)) { 1641 if (!extent_buffer_uptodate(root_eb)) {
@@ -1698,9 +1700,10 @@ walk_down:
1698 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); 1700 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1699 path->locks[level] = BTRFS_READ_LOCK_BLOCKING; 1701 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
1700 1702
1701 ret = btrfs_qgroup_trace_extent(trans, 1703 ret = btrfs_qgroup_trace_extent(trans, fs_info,
1702 root->fs_info, child_bytenr, 1704 child_bytenr,
1703 root->fs_info->nodesize, GFP_NOFS); 1705 fs_info->nodesize,
1706 GFP_NOFS);
1704 if (ret) 1707 if (ret)
1705 goto out; 1708 goto out;
1706 } 1709 }
@@ -2170,7 +2173,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2170 } 2173 }
2171 2174
2172 rcu_read_lock(); 2175 rcu_read_lock();
2173 level_size = srcroot->fs_info->nodesize; 2176 level_size = fs_info->nodesize;
2174 rcu_read_unlock(); 2177 rcu_read_unlock();
2175 } 2178 }
2176 2179
@@ -2254,8 +2257,7 @@ int btrfs_qgroup_inherit(struct btrfs_trans_handle *trans,
2254 i_qgroups = (u64 *)(inherit + 1); 2257 i_qgroups = (u64 *)(inherit + 1);
2255 for (i = 0; i < inherit->num_qgroups; ++i) { 2258 for (i = 0; i < inherit->num_qgroups; ++i) {
2256 if (*i_qgroups) { 2259 if (*i_qgroups) {
2257 ret = add_relation_rb(quota_root->fs_info, objectid, 2260 ret = add_relation_rb(fs_info, objectid, *i_qgroups);
2258 *i_qgroups);
2259 if (ret) 2261 if (ret)
2260 goto unlock; 2262 goto unlock;
2261 } 2263 }
@@ -2897,13 +2899,14 @@ int btrfs_qgroup_release_data(struct inode *inode, u64 start, u64 len)
2897 2899
2898int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes) 2900int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes)
2899{ 2901{
2902 struct btrfs_fs_info *fs_info = root->fs_info;
2900 int ret; 2903 int ret;
2901 2904
2902 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) || 2905 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
2903 !is_fstree(root->objectid) || num_bytes == 0) 2906 !is_fstree(root->objectid) || num_bytes == 0)
2904 return 0; 2907 return 0;
2905 2908
2906 BUG_ON(num_bytes != round_down(num_bytes, root->fs_info->nodesize)); 2909 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
2907 ret = qgroup_reserve(root, num_bytes); 2910 ret = qgroup_reserve(root, num_bytes);
2908 if (ret < 0) 2911 if (ret < 0)
2909 return ret; 2912 return ret;
@@ -2913,9 +2916,10 @@ int btrfs_qgroup_reserve_meta(struct btrfs_root *root, int num_bytes)
2913 2916
2914void btrfs_qgroup_free_meta_all(struct btrfs_root *root) 2917void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
2915{ 2918{
2919 struct btrfs_fs_info *fs_info = root->fs_info;
2916 int reserved; 2920 int reserved;
2917 2921
2918 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) || 2922 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
2919 !is_fstree(root->objectid)) 2923 !is_fstree(root->objectid))
2920 return; 2924 return;
2921 2925
@@ -2927,11 +2931,13 @@ void btrfs_qgroup_free_meta_all(struct btrfs_root *root)
2927 2931
2928void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes) 2932void btrfs_qgroup_free_meta(struct btrfs_root *root, int num_bytes)
2929{ 2933{
2930 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &root->fs_info->flags) || 2934 struct btrfs_fs_info *fs_info = root->fs_info;
2935
2936 if (!test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags) ||
2931 !is_fstree(root->objectid)) 2937 !is_fstree(root->objectid))
2932 return; 2938 return;
2933 2939
2934 BUG_ON(num_bytes != round_down(num_bytes, root->fs_info->nodesize)); 2940 BUG_ON(num_bytes != round_down(num_bytes, fs_info->nodesize));
2935 WARN_ON(atomic_read(&root->qgroup_meta_rsv) < num_bytes); 2941 WARN_ON(atomic_read(&root->qgroup_meta_rsv) < num_bytes);
2936 atomic_sub(num_bytes, &root->qgroup_meta_rsv); 2942 atomic_sub(num_bytes, &root->qgroup_meta_rsv);
2937 qgroup_free(root, num_bytes); 2943 qgroup_free(root, num_bytes);
diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
index 6e9e21da23e7..9a67346b48f5 100644
--- a/fs/btrfs/raid56.c
+++ b/fs/btrfs/raid56.c
@@ -1478,11 +1478,8 @@ cleanup:
1478 1478
1479static void async_rmw_stripe(struct btrfs_raid_bio *rbio) 1479static void async_rmw_stripe(struct btrfs_raid_bio *rbio)
1480{ 1480{
1481 btrfs_init_work(&rbio->work, btrfs_rmw_helper, 1481 btrfs_init_work(&rbio->work, btrfs_rmw_helper, rmw_work, NULL, NULL);
1482 rmw_work, NULL, NULL); 1482 btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
1483
1484 btrfs_queue_work(rbio->fs_info->rmw_workers,
1485 &rbio->work);
1486} 1483}
1487 1484
1488static void async_read_rebuild(struct btrfs_raid_bio *rbio) 1485static void async_read_rebuild(struct btrfs_raid_bio *rbio)
@@ -1490,8 +1487,7 @@ static void async_read_rebuild(struct btrfs_raid_bio *rbio)
1490 btrfs_init_work(&rbio->work, btrfs_rmw_helper, 1487 btrfs_init_work(&rbio->work, btrfs_rmw_helper,
1491 read_rebuild_work, NULL, NULL); 1488 read_rebuild_work, NULL, NULL);
1492 1489
1493 btrfs_queue_work(rbio->fs_info->rmw_workers, 1490 btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
1494 &rbio->work);
1495} 1491}
1496 1492
1497/* 1493/*
@@ -1573,8 +1569,7 @@ static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1573 bio->bi_end_io = raid_rmw_end_io; 1569 bio->bi_end_io = raid_rmw_end_io;
1574 bio_set_op_attrs(bio, REQ_OP_READ, 0); 1570 bio_set_op_attrs(bio, REQ_OP_READ, 0);
1575 1571
1576 btrfs_bio_wq_end_io(rbio->fs_info, bio, 1572 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
1577 BTRFS_WQ_ENDIO_RAID56);
1578 1573
1579 submit_bio(bio); 1574 submit_bio(bio);
1580 } 1575 }
@@ -1742,6 +1737,7 @@ static void btrfs_raid_unplug(struct blk_plug_cb *cb, bool from_schedule)
1742int raid56_parity_write(struct btrfs_root *root, struct bio *bio, 1737int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
1743 struct btrfs_bio *bbio, u64 stripe_len) 1738 struct btrfs_bio *bbio, u64 stripe_len)
1744{ 1739{
1740 struct btrfs_fs_info *fs_info = root->fs_info;
1745 struct btrfs_raid_bio *rbio; 1741 struct btrfs_raid_bio *rbio;
1746 struct btrfs_plug_cb *plug = NULL; 1742 struct btrfs_plug_cb *plug = NULL;
1747 struct blk_plug_cb *cb; 1743 struct blk_plug_cb *cb;
@@ -1756,7 +1752,7 @@ int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
1756 rbio->bio_list_bytes = bio->bi_iter.bi_size; 1752 rbio->bio_list_bytes = bio->bi_iter.bi_size;
1757 rbio->operation = BTRFS_RBIO_WRITE; 1753 rbio->operation = BTRFS_RBIO_WRITE;
1758 1754
1759 btrfs_bio_counter_inc_noblocked(root->fs_info); 1755 btrfs_bio_counter_inc_noblocked(fs_info);
1760 rbio->generic_bio_cnt = 1; 1756 rbio->generic_bio_cnt = 1;
1761 1757
1762 /* 1758 /*
@@ -1766,16 +1762,15 @@ int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
1766 if (rbio_is_full(rbio)) { 1762 if (rbio_is_full(rbio)) {
1767 ret = full_stripe_write(rbio); 1763 ret = full_stripe_write(rbio);
1768 if (ret) 1764 if (ret)
1769 btrfs_bio_counter_dec(root->fs_info); 1765 btrfs_bio_counter_dec(fs_info);
1770 return ret; 1766 return ret;
1771 } 1767 }
1772 1768
1773 cb = blk_check_plugged(btrfs_raid_unplug, root->fs_info, 1769 cb = blk_check_plugged(btrfs_raid_unplug, fs_info, sizeof(*plug));
1774 sizeof(*plug));
1775 if (cb) { 1770 if (cb) {
1776 plug = container_of(cb, struct btrfs_plug_cb, cb); 1771 plug = container_of(cb, struct btrfs_plug_cb, cb);
1777 if (!plug->info) { 1772 if (!plug->info) {
1778 plug->info = root->fs_info; 1773 plug->info = fs_info;
1779 INIT_LIST_HEAD(&plug->rbio_list); 1774 INIT_LIST_HEAD(&plug->rbio_list);
1780 } 1775 }
1781 list_add_tail(&rbio->plug_list, &plug->rbio_list); 1776 list_add_tail(&rbio->plug_list, &plug->rbio_list);
@@ -1783,7 +1778,7 @@ int raid56_parity_write(struct btrfs_root *root, struct bio *bio,
1783 } else { 1778 } else {
1784 ret = __raid56_parity_write(rbio); 1779 ret = __raid56_parity_write(rbio);
1785 if (ret) 1780 if (ret)
1786 btrfs_bio_counter_dec(root->fs_info); 1781 btrfs_bio_counter_dec(fs_info);
1787 } 1782 }
1788 return ret; 1783 return ret;
1789} 1784}
@@ -2098,8 +2093,7 @@ static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2098 bio->bi_end_io = raid_recover_end_io; 2093 bio->bi_end_io = raid_recover_end_io;
2099 bio_set_op_attrs(bio, REQ_OP_READ, 0); 2094 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2100 2095
2101 btrfs_bio_wq_end_io(rbio->fs_info, bio, 2096 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
2102 BTRFS_WQ_ENDIO_RAID56);
2103 2097
2104 submit_bio(bio); 2098 submit_bio(bio);
2105 } 2099 }
@@ -2123,6 +2117,7 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,
2123 struct btrfs_bio *bbio, u64 stripe_len, 2117 struct btrfs_bio *bbio, u64 stripe_len,
2124 int mirror_num, int generic_io) 2118 int mirror_num, int generic_io)
2125{ 2119{
2120 struct btrfs_fs_info *fs_info = root->fs_info;
2126 struct btrfs_raid_bio *rbio; 2121 struct btrfs_raid_bio *rbio;
2127 int ret; 2122 int ret;
2128 2123
@@ -2139,7 +2134,7 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,
2139 2134
2140 rbio->faila = find_logical_bio_stripe(rbio, bio); 2135 rbio->faila = find_logical_bio_stripe(rbio, bio);
2141 if (rbio->faila == -1) { 2136 if (rbio->faila == -1) {
2142 btrfs_warn(root->fs_info, 2137 btrfs_warn(fs_info,
2143 "%s could not find the bad stripe in raid56 so that we cannot recover any more (bio has logical %llu len %llu, bbio has map_type %llu)", 2138 "%s could not find the bad stripe in raid56 so that we cannot recover any more (bio has logical %llu len %llu, bbio has map_type %llu)",
2144 __func__, (u64)bio->bi_iter.bi_sector << 9, 2139 __func__, (u64)bio->bi_iter.bi_sector << 9,
2145 (u64)bio->bi_iter.bi_size, bbio->map_type); 2140 (u64)bio->bi_iter.bi_size, bbio->map_type);
@@ -2150,7 +2145,7 @@ int raid56_parity_recover(struct btrfs_root *root, struct bio *bio,
2150 } 2145 }
2151 2146
2152 if (generic_io) { 2147 if (generic_io) {
2153 btrfs_bio_counter_inc_noblocked(root->fs_info); 2148 btrfs_bio_counter_inc_noblocked(fs_info);
2154 rbio->generic_bio_cnt = 1; 2149 rbio->generic_bio_cnt = 1;
2155 } else { 2150 } else {
2156 btrfs_get_bbio(bbio); 2151 btrfs_get_bbio(bbio);
@@ -2213,6 +2208,7 @@ raid56_parity_alloc_scrub_rbio(struct btrfs_root *root, struct bio *bio,
2213 struct btrfs_device *scrub_dev, 2208 struct btrfs_device *scrub_dev,
2214 unsigned long *dbitmap, int stripe_nsectors) 2209 unsigned long *dbitmap, int stripe_nsectors)
2215{ 2210{
2211 struct btrfs_fs_info *fs_info = root->fs_info;
2216 struct btrfs_raid_bio *rbio; 2212 struct btrfs_raid_bio *rbio;
2217 int i; 2213 int i;
2218 2214
@@ -2235,7 +2231,7 @@ raid56_parity_alloc_scrub_rbio(struct btrfs_root *root, struct bio *bio,
2235 } 2231 }
2236 2232
2237 /* Now we just support the sectorsize equals to page size */ 2233 /* Now we just support the sectorsize equals to page size */
2238 ASSERT(root->fs_info->sectorsize == PAGE_SIZE); 2234 ASSERT(fs_info->sectorsize == PAGE_SIZE);
2239 ASSERT(rbio->stripe_npages == stripe_nsectors); 2235 ASSERT(rbio->stripe_npages == stripe_nsectors);
2240 bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors); 2236 bitmap_copy(rbio->dbitmap, dbitmap, stripe_nsectors);
2241 2237
@@ -2617,8 +2613,7 @@ static void raid56_parity_scrub_stripe(struct btrfs_raid_bio *rbio)
2617 bio->bi_end_io = raid56_parity_scrub_end_io; 2613 bio->bi_end_io = raid56_parity_scrub_end_io;
2618 bio_set_op_attrs(bio, REQ_OP_READ, 0); 2614 bio_set_op_attrs(bio, REQ_OP_READ, 0);
2619 2615
2620 btrfs_bio_wq_end_io(rbio->fs_info, bio, 2616 btrfs_bio_wq_end_io(rbio->fs_info, bio, BTRFS_WQ_ENDIO_RAID56);
2621 BTRFS_WQ_ENDIO_RAID56);
2622 2617
2623 submit_bio(bio); 2618 submit_bio(bio);
2624 } 2619 }
@@ -2646,8 +2641,7 @@ static void async_scrub_parity(struct btrfs_raid_bio *rbio)
2646 btrfs_init_work(&rbio->work, btrfs_rmw_helper, 2641 btrfs_init_work(&rbio->work, btrfs_rmw_helper,
2647 scrub_parity_work, NULL, NULL); 2642 scrub_parity_work, NULL, NULL);
2648 2643
2649 btrfs_queue_work(rbio->fs_info->rmw_workers, 2644 btrfs_queue_work(rbio->fs_info->rmw_workers, &rbio->work);
2650 &rbio->work);
2651} 2645}
2652 2646
2653void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio) 2647void raid56_parity_submit_scrub_rbio(struct btrfs_raid_bio *rbio)
diff --git a/fs/btrfs/reada.c b/fs/btrfs/reada.c
index c51292abbd03..8d36fb457594 100644
--- a/fs/btrfs/reada.c
+++ b/fs/btrfs/reada.c
@@ -335,7 +335,7 @@ static struct reada_extent *reada_find_extent(struct btrfs_root *root,
335 if (!re) 335 if (!re)
336 return NULL; 336 return NULL;
337 337
338 blocksize = root->fs_info->nodesize; 338 blocksize = fs_info->nodesize;
339 re->logical = logical; 339 re->logical = logical;
340 re->top = *top; 340 re->top = *top;
341 INIT_LIST_HEAD(&re->extctl); 341 INIT_LIST_HEAD(&re->extctl);
@@ -352,7 +352,7 @@ static struct reada_extent *reada_find_extent(struct btrfs_root *root,
352 goto error; 352 goto error;
353 353
354 if (bbio->num_stripes > BTRFS_MAX_MIRRORS) { 354 if (bbio->num_stripes > BTRFS_MAX_MIRRORS) {
355 btrfs_err(root->fs_info, 355 btrfs_err(fs_info,
356 "readahead: more than %d copies not supported", 356 "readahead: more than %d copies not supported",
357 BTRFS_MAX_MIRRORS); 357 BTRFS_MAX_MIRRORS);
358 goto error; 358 goto error;
@@ -950,11 +950,10 @@ int btrfs_reada_wait(void *handle)
950 reada_start_machine(fs_info); 950 reada_start_machine(fs_info);
951 wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0, 951 wait_event_timeout(rc->wait, atomic_read(&rc->elems) == 0,
952 5 * HZ); 952 5 * HZ);
953 dump_devs(rc->root->fs_info, 953 dump_devs(fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
954 atomic_read(&rc->elems) < 10 ? 1 : 0);
955 } 954 }
956 955
957 dump_devs(rc->root->fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0); 956 dump_devs(fs_info, atomic_read(&rc->elems) < 10 ? 1 : 0);
958 957
959 kref_put(&rc->refcnt, reada_control_release); 958 kref_put(&rc->refcnt, reada_control_release);
960 959
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index a51559138850..bc6ccd3a6051 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -1288,9 +1288,10 @@ fail:
1288 */ 1288 */
1289static int __must_check __add_reloc_root(struct btrfs_root *root) 1289static int __must_check __add_reloc_root(struct btrfs_root *root)
1290{ 1290{
1291 struct btrfs_fs_info *fs_info = root->fs_info;
1291 struct rb_node *rb_node; 1292 struct rb_node *rb_node;
1292 struct mapping_node *node; 1293 struct mapping_node *node;
1293 struct reloc_control *rc = root->fs_info->reloc_ctl; 1294 struct reloc_control *rc = fs_info->reloc_ctl;
1294 1295
1295 node = kmalloc(sizeof(*node), GFP_NOFS); 1296 node = kmalloc(sizeof(*node), GFP_NOFS);
1296 if (!node) 1297 if (!node)
@@ -1304,7 +1305,7 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
1304 node->bytenr, &node->rb_node); 1305 node->bytenr, &node->rb_node);
1305 spin_unlock(&rc->reloc_root_tree.lock); 1306 spin_unlock(&rc->reloc_root_tree.lock);
1306 if (rb_node) { 1307 if (rb_node) {
1307 btrfs_panic(root->fs_info, -EEXIST, 1308 btrfs_panic(fs_info, -EEXIST,
1308 "Duplicate root found for start=%llu while inserting into relocation tree", 1309 "Duplicate root found for start=%llu while inserting into relocation tree",
1309 node->bytenr); 1310 node->bytenr);
1310 kfree(node); 1311 kfree(node);
@@ -1321,9 +1322,10 @@ static int __must_check __add_reloc_root(struct btrfs_root *root)
1321 */ 1322 */
1322static void __del_reloc_root(struct btrfs_root *root) 1323static void __del_reloc_root(struct btrfs_root *root)
1323{ 1324{
1325 struct btrfs_fs_info *fs_info = root->fs_info;
1324 struct rb_node *rb_node; 1326 struct rb_node *rb_node;
1325 struct mapping_node *node = NULL; 1327 struct mapping_node *node = NULL;
1326 struct reloc_control *rc = root->fs_info->reloc_ctl; 1328 struct reloc_control *rc = fs_info->reloc_ctl;
1327 1329
1328 spin_lock(&rc->reloc_root_tree.lock); 1330 spin_lock(&rc->reloc_root_tree.lock);
1329 rb_node = tree_search(&rc->reloc_root_tree.rb_root, 1331 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
@@ -1338,9 +1340,9 @@ static void __del_reloc_root(struct btrfs_root *root)
1338 return; 1340 return;
1339 BUG_ON((struct btrfs_root *)node->data != root); 1341 BUG_ON((struct btrfs_root *)node->data != root);
1340 1342
1341 spin_lock(&root->fs_info->trans_lock); 1343 spin_lock(&fs_info->trans_lock);
1342 list_del_init(&root->root_list); 1344 list_del_init(&root->root_list);
1343 spin_unlock(&root->fs_info->trans_lock); 1345 spin_unlock(&fs_info->trans_lock);
1344 kfree(node); 1346 kfree(node);
1345} 1347}
1346 1348
@@ -1350,9 +1352,10 @@ static void __del_reloc_root(struct btrfs_root *root)
1350 */ 1352 */
1351static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr) 1353static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1352{ 1354{
1355 struct btrfs_fs_info *fs_info = root->fs_info;
1353 struct rb_node *rb_node; 1356 struct rb_node *rb_node;
1354 struct mapping_node *node = NULL; 1357 struct mapping_node *node = NULL;
1355 struct reloc_control *rc = root->fs_info->reloc_ctl; 1358 struct reloc_control *rc = fs_info->reloc_ctl;
1356 1359
1357 spin_lock(&rc->reloc_root_tree.lock); 1360 spin_lock(&rc->reloc_root_tree.lock);
1358 rb_node = tree_search(&rc->reloc_root_tree.rb_root, 1361 rb_node = tree_search(&rc->reloc_root_tree.rb_root,
@@ -1380,6 +1383,7 @@ static int __update_reloc_root(struct btrfs_root *root, u64 new_bytenr)
1380static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans, 1383static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1381 struct btrfs_root *root, u64 objectid) 1384 struct btrfs_root *root, u64 objectid)
1382{ 1385{
1386 struct btrfs_fs_info *fs_info = root->fs_info;
1383 struct btrfs_root *reloc_root; 1387 struct btrfs_root *reloc_root;
1384 struct extent_buffer *eb; 1388 struct extent_buffer *eb;
1385 struct btrfs_root_item *root_item; 1389 struct btrfs_root_item *root_item;
@@ -1437,12 +1441,12 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1437 btrfs_tree_unlock(eb); 1441 btrfs_tree_unlock(eb);
1438 free_extent_buffer(eb); 1442 free_extent_buffer(eb);
1439 1443
1440 ret = btrfs_insert_root(trans, root->fs_info->tree_root, 1444 ret = btrfs_insert_root(trans, fs_info->tree_root,
1441 &root_key, root_item); 1445 &root_key, root_item);
1442 BUG_ON(ret); 1446 BUG_ON(ret);
1443 kfree(root_item); 1447 kfree(root_item);
1444 1448
1445 reloc_root = btrfs_read_fs_root(root->fs_info->tree_root, &root_key); 1449 reloc_root = btrfs_read_fs_root(fs_info->tree_root, &root_key);
1446 BUG_ON(IS_ERR(reloc_root)); 1450 BUG_ON(IS_ERR(reloc_root));
1447 reloc_root->last_trans = trans->transid; 1451 reloc_root->last_trans = trans->transid;
1448 return reloc_root; 1452 return reloc_root;
@@ -1455,8 +1459,9 @@ static struct btrfs_root *create_reloc_root(struct btrfs_trans_handle *trans,
1455int btrfs_init_reloc_root(struct btrfs_trans_handle *trans, 1459int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1456 struct btrfs_root *root) 1460 struct btrfs_root *root)
1457{ 1461{
1462 struct btrfs_fs_info *fs_info = root->fs_info;
1458 struct btrfs_root *reloc_root; 1463 struct btrfs_root *reloc_root;
1459 struct reloc_control *rc = root->fs_info->reloc_ctl; 1464 struct reloc_control *rc = fs_info->reloc_ctl;
1460 struct btrfs_block_rsv *rsv; 1465 struct btrfs_block_rsv *rsv;
1461 int clear_rsv = 0; 1466 int clear_rsv = 0;
1462 int ret; 1467 int ret;
@@ -1492,6 +1497,7 @@ int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
1492int btrfs_update_reloc_root(struct btrfs_trans_handle *trans, 1497int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1493 struct btrfs_root *root) 1498 struct btrfs_root *root)
1494{ 1499{
1500 struct btrfs_fs_info *fs_info = root->fs_info;
1495 struct btrfs_root *reloc_root; 1501 struct btrfs_root *reloc_root;
1496 struct btrfs_root_item *root_item; 1502 struct btrfs_root_item *root_item;
1497 int ret; 1503 int ret;
@@ -1502,7 +1508,7 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1502 reloc_root = root->reloc_root; 1508 reloc_root = root->reloc_root;
1503 root_item = &reloc_root->root_item; 1509 root_item = &reloc_root->root_item;
1504 1510
1505 if (root->fs_info->reloc_ctl->merge_reloc_tree && 1511 if (fs_info->reloc_ctl->merge_reloc_tree &&
1506 btrfs_root_refs(root_item) == 0) { 1512 btrfs_root_refs(root_item) == 0) {
1507 root->reloc_root = NULL; 1513 root->reloc_root = NULL;
1508 __del_reloc_root(reloc_root); 1514 __del_reloc_root(reloc_root);
@@ -1514,7 +1520,7 @@ int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
1514 reloc_root->commit_root = btrfs_root_node(reloc_root); 1520 reloc_root->commit_root = btrfs_root_node(reloc_root);
1515 } 1521 }
1516 1522
1517 ret = btrfs_update_root(trans, root->fs_info->tree_root, 1523 ret = btrfs_update_root(trans, fs_info->tree_root,
1518 &reloc_root->root_key, root_item); 1524 &reloc_root->root_key, root_item);
1519 BUG_ON(ret); 1525 BUG_ON(ret);
1520 1526
@@ -1642,6 +1648,7 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
1642 struct btrfs_root *root, 1648 struct btrfs_root *root,
1643 struct extent_buffer *leaf) 1649 struct extent_buffer *leaf)
1644{ 1650{
1651 struct btrfs_fs_info *fs_info = root->fs_info;
1645 struct btrfs_key key; 1652 struct btrfs_key key;
1646 struct btrfs_file_extent_item *fi; 1653 struct btrfs_file_extent_item *fi;
1647 struct inode *inode = NULL; 1654 struct inode *inode = NULL;
@@ -1698,8 +1705,8 @@ int replace_file_extents(struct btrfs_trans_handle *trans,
1698 end = key.offset + 1705 end = key.offset +
1699 btrfs_file_extent_num_bytes(leaf, fi); 1706 btrfs_file_extent_num_bytes(leaf, fi);
1700 WARN_ON(!IS_ALIGNED(key.offset, 1707 WARN_ON(!IS_ALIGNED(key.offset,
1701 root->fs_info->sectorsize)); 1708 fs_info->sectorsize));
1702 WARN_ON(!IS_ALIGNED(end, root->fs_info->sectorsize)); 1709 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
1703 end--; 1710 end--;
1704 ret = try_lock_extent(&BTRFS_I(inode)->io_tree, 1711 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
1705 key.offset, end); 1712 key.offset, end);
@@ -1777,6 +1784,7 @@ int replace_path(struct btrfs_trans_handle *trans,
1777 struct btrfs_path *path, struct btrfs_key *next_key, 1784 struct btrfs_path *path, struct btrfs_key *next_key,
1778 int lowest_level, int max_level) 1785 int lowest_level, int max_level)
1779{ 1786{
1787 struct btrfs_fs_info *fs_info = dest->fs_info;
1780 struct extent_buffer *eb; 1788 struct extent_buffer *eb;
1781 struct extent_buffer *parent; 1789 struct extent_buffer *parent;
1782 struct btrfs_key key; 1790 struct btrfs_key key;
@@ -1834,7 +1842,7 @@ again:
1834 btrfs_node_key_to_cpu(parent, next_key, slot + 1); 1842 btrfs_node_key_to_cpu(parent, next_key, slot + 1);
1835 1843
1836 old_bytenr = btrfs_node_blockptr(parent, slot); 1844 old_bytenr = btrfs_node_blockptr(parent, slot);
1837 blocksize = dest->fs_info->nodesize; 1845 blocksize = fs_info->nodesize;
1838 old_ptr_gen = btrfs_node_ptr_generation(parent, slot); 1846 old_ptr_gen = btrfs_node_ptr_generation(parent, slot);
1839 1847
1840 if (level <= max_level) { 1848 if (level <= max_level) {
@@ -2061,6 +2069,7 @@ static int invalidate_extent_cache(struct btrfs_root *root,
2061 struct btrfs_key *min_key, 2069 struct btrfs_key *min_key,
2062 struct btrfs_key *max_key) 2070 struct btrfs_key *max_key)
2063{ 2071{
2072 struct btrfs_fs_info *fs_info = root->fs_info;
2064 struct inode *inode = NULL; 2073 struct inode *inode = NULL;
2065 u64 objectid; 2074 u64 objectid;
2066 u64 start, end; 2075 u64 start, end;
@@ -2095,7 +2104,7 @@ static int invalidate_extent_cache(struct btrfs_root *root,
2095 start = 0; 2104 start = 0;
2096 else { 2105 else {
2097 start = min_key->offset; 2106 start = min_key->offset;
2098 WARN_ON(!IS_ALIGNED(start, root->fs_info->sectorsize)); 2107 WARN_ON(!IS_ALIGNED(start, fs_info->sectorsize));
2099 } 2108 }
2100 } else { 2109 } else {
2101 start = 0; 2110 start = 0;
@@ -2110,7 +2119,7 @@ static int invalidate_extent_cache(struct btrfs_root *root,
2110 if (max_key->offset == 0) 2119 if (max_key->offset == 0)
2111 continue; 2120 continue;
2112 end = max_key->offset; 2121 end = max_key->offset;
2113 WARN_ON(!IS_ALIGNED(end, root->fs_info->sectorsize)); 2122 WARN_ON(!IS_ALIGNED(end, fs_info->sectorsize));
2114 end--; 2123 end--;
2115 } 2124 }
2116 } else { 2125 } else {
@@ -2150,6 +2159,7 @@ static int find_next_key(struct btrfs_path *path, int level,
2150static noinline_for_stack int merge_reloc_root(struct reloc_control *rc, 2159static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2151 struct btrfs_root *root) 2160 struct btrfs_root *root)
2152{ 2161{
2162 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2153 LIST_HEAD(inode_list); 2163 LIST_HEAD(inode_list);
2154 struct btrfs_key key; 2164 struct btrfs_key key;
2155 struct btrfs_key next_key; 2165 struct btrfs_key next_key;
@@ -2198,7 +2208,7 @@ static noinline_for_stack int merge_reloc_root(struct reloc_control *rc,
2198 btrfs_unlock_up_safe(path, 0); 2208 btrfs_unlock_up_safe(path, 0);
2199 } 2209 }
2200 2210
2201 min_reserved = root->fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; 2211 min_reserved = fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2202 memset(&next_key, 0, sizeof(next_key)); 2212 memset(&next_key, 0, sizeof(next_key));
2203 2213
2204 while (1) { 2214 while (1) {
@@ -2304,16 +2314,17 @@ static noinline_for_stack
2304int prepare_to_merge(struct reloc_control *rc, int err) 2314int prepare_to_merge(struct reloc_control *rc, int err)
2305{ 2315{
2306 struct btrfs_root *root = rc->extent_root; 2316 struct btrfs_root *root = rc->extent_root;
2317 struct btrfs_fs_info *fs_info = root->fs_info;
2307 struct btrfs_root *reloc_root; 2318 struct btrfs_root *reloc_root;
2308 struct btrfs_trans_handle *trans; 2319 struct btrfs_trans_handle *trans;
2309 LIST_HEAD(reloc_roots); 2320 LIST_HEAD(reloc_roots);
2310 u64 num_bytes = 0; 2321 u64 num_bytes = 0;
2311 int ret; 2322 int ret;
2312 2323
2313 mutex_lock(&root->fs_info->reloc_mutex); 2324 mutex_lock(&fs_info->reloc_mutex);
2314 rc->merging_rsv_size += root->fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2; 2325 rc->merging_rsv_size += fs_info->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2315 rc->merging_rsv_size += rc->nodes_relocated * 2; 2326 rc->merging_rsv_size += rc->nodes_relocated * 2;
2316 mutex_unlock(&root->fs_info->reloc_mutex); 2327 mutex_unlock(&fs_info->reloc_mutex);
2317 2328
2318again: 2329again:
2319 if (!err) { 2330 if (!err) {
@@ -2348,8 +2359,7 @@ again:
2348 struct btrfs_root, root_list); 2359 struct btrfs_root, root_list);
2349 list_del_init(&reloc_root->root_list); 2360 list_del_init(&reloc_root->root_list);
2350 2361
2351 root = read_fs_root(reloc_root->fs_info, 2362 root = read_fs_root(fs_info, reloc_root->root_key.offset);
2352 reloc_root->root_key.offset);
2353 BUG_ON(IS_ERR(root)); 2363 BUG_ON(IS_ERR(root));
2354 BUG_ON(root->reloc_root != reloc_root); 2364 BUG_ON(root->reloc_root != reloc_root);
2355 2365
@@ -2392,6 +2402,7 @@ void free_reloc_roots(struct list_head *list)
2392static noinline_for_stack 2402static noinline_for_stack
2393void merge_reloc_roots(struct reloc_control *rc) 2403void merge_reloc_roots(struct reloc_control *rc)
2394{ 2404{
2405 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2395 struct btrfs_root *root; 2406 struct btrfs_root *root;
2396 struct btrfs_root *reloc_root; 2407 struct btrfs_root *reloc_root;
2397 u64 last_snap; 2408 u64 last_snap;
@@ -2409,9 +2420,9 @@ again:
2409 * adding their roots to the list while we are 2420 * adding their roots to the list while we are
2410 * doing this splice 2421 * doing this splice
2411 */ 2422 */
2412 mutex_lock(&root->fs_info->reloc_mutex); 2423 mutex_lock(&fs_info->reloc_mutex);
2413 list_splice_init(&rc->reloc_roots, &reloc_roots); 2424 list_splice_init(&rc->reloc_roots, &reloc_roots);
2414 mutex_unlock(&root->fs_info->reloc_mutex); 2425 mutex_unlock(&fs_info->reloc_mutex);
2415 2426
2416 while (!list_empty(&reloc_roots)) { 2427 while (!list_empty(&reloc_roots)) {
2417 found = 1; 2428 found = 1;
@@ -2419,7 +2430,7 @@ again:
2419 struct btrfs_root, root_list); 2430 struct btrfs_root, root_list);
2420 2431
2421 if (btrfs_root_refs(&reloc_root->root_item) > 0) { 2432 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
2422 root = read_fs_root(reloc_root->fs_info, 2433 root = read_fs_root(fs_info,
2423 reloc_root->root_key.offset); 2434 reloc_root->root_key.offset);
2424 BUG_ON(IS_ERR(root)); 2435 BUG_ON(IS_ERR(root));
2425 BUG_ON(root->reloc_root != reloc_root); 2436 BUG_ON(root->reloc_root != reloc_root);
@@ -2458,14 +2469,14 @@ again:
2458 } 2469 }
2459out: 2470out:
2460 if (ret) { 2471 if (ret) {
2461 btrfs_handle_fs_error(root->fs_info, ret, NULL); 2472 btrfs_handle_fs_error(fs_info, ret, NULL);
2462 if (!list_empty(&reloc_roots)) 2473 if (!list_empty(&reloc_roots))
2463 free_reloc_roots(&reloc_roots); 2474 free_reloc_roots(&reloc_roots);
2464 2475
2465 /* new reloc root may be added */ 2476 /* new reloc root may be added */
2466 mutex_lock(&root->fs_info->reloc_mutex); 2477 mutex_lock(&fs_info->reloc_mutex);
2467 list_splice_init(&rc->reloc_roots, &reloc_roots); 2478 list_splice_init(&rc->reloc_roots, &reloc_roots);
2468 mutex_unlock(&root->fs_info->reloc_mutex); 2479 mutex_unlock(&fs_info->reloc_mutex);
2469 if (!list_empty(&reloc_roots)) 2480 if (!list_empty(&reloc_roots))
2470 free_reloc_roots(&reloc_roots); 2481 free_reloc_roots(&reloc_roots);
2471 } 2482 }
@@ -2487,12 +2498,13 @@ static void free_block_list(struct rb_root *blocks)
2487static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans, 2498static int record_reloc_root_in_trans(struct btrfs_trans_handle *trans,
2488 struct btrfs_root *reloc_root) 2499 struct btrfs_root *reloc_root)
2489{ 2500{
2501 struct btrfs_fs_info *fs_info = reloc_root->fs_info;
2490 struct btrfs_root *root; 2502 struct btrfs_root *root;
2491 2503
2492 if (reloc_root->last_trans == trans->transid) 2504 if (reloc_root->last_trans == trans->transid)
2493 return 0; 2505 return 0;
2494 2506
2495 root = read_fs_root(reloc_root->fs_info, reloc_root->root_key.offset); 2507 root = read_fs_root(fs_info, reloc_root->root_key.offset);
2496 BUG_ON(IS_ERR(root)); 2508 BUG_ON(IS_ERR(root));
2497 BUG_ON(root->reloc_root != reloc_root); 2509 BUG_ON(root->reloc_root != reloc_root);
2498 2510
@@ -2602,6 +2614,7 @@ static noinline_for_stack
2602u64 calcu_metadata_size(struct reloc_control *rc, 2614u64 calcu_metadata_size(struct reloc_control *rc,
2603 struct backref_node *node, int reserve) 2615 struct backref_node *node, int reserve)
2604{ 2616{
2617 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
2605 struct backref_node *next = node; 2618 struct backref_node *next = node;
2606 struct backref_edge *edge; 2619 struct backref_edge *edge;
2607 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1]; 2620 struct backref_edge *edges[BTRFS_MAX_LEVEL - 1];
@@ -2616,7 +2629,7 @@ u64 calcu_metadata_size(struct reloc_control *rc,
2616 if (next->processed && (reserve || next != node)) 2629 if (next->processed && (reserve || next != node))
2617 break; 2630 break;
2618 2631
2619 num_bytes += rc->extent_root->fs_info->nodesize; 2632 num_bytes += fs_info->nodesize;
2620 2633
2621 if (list_empty(&next->upper)) 2634 if (list_empty(&next->upper))
2622 break; 2635 break;
@@ -3131,7 +3144,7 @@ static noinline_for_stack
3131int setup_extent_mapping(struct inode *inode, u64 start, u64 end, 3144int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3132 u64 block_start) 3145 u64 block_start)
3133{ 3146{
3134 struct btrfs_root *root = BTRFS_I(inode)->root; 3147 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3135 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; 3148 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3136 struct extent_map *em; 3149 struct extent_map *em;
3137 int ret = 0; 3150 int ret = 0;
@@ -3144,7 +3157,7 @@ int setup_extent_mapping(struct inode *inode, u64 start, u64 end,
3144 em->len = end + 1 - start; 3157 em->len = end + 1 - start;
3145 em->block_len = em->len; 3158 em->block_len = em->len;
3146 em->block_start = block_start; 3159 em->block_start = block_start;
3147 em->bdev = root->fs_info->fs_devices->latest_bdev; 3160 em->bdev = fs_info->fs_devices->latest_bdev;
3148 set_bit(EXTENT_FLAG_PINNED, &em->flags); 3161 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3149 3162
3150 lock_extent(&BTRFS_I(inode)->io_tree, start, end); 3163 lock_extent(&BTRFS_I(inode)->io_tree, start, end);
@@ -3419,11 +3432,11 @@ static int __add_tree_block(struct reloc_control *rc,
3419 u64 bytenr, u32 blocksize, 3432 u64 bytenr, u32 blocksize,
3420 struct rb_root *blocks) 3433 struct rb_root *blocks)
3421{ 3434{
3435 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3422 struct btrfs_path *path; 3436 struct btrfs_path *path;
3423 struct btrfs_key key; 3437 struct btrfs_key key;
3424 int ret; 3438 int ret;
3425 bool skinny = btrfs_fs_incompat(rc->extent_root->fs_info, 3439 bool skinny = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
3426 SKINNY_METADATA);
3427 3440
3428 if (tree_block_processed(bytenr, rc)) 3441 if (tree_block_processed(bytenr, rc))
3429 return 0; 3442 return 0;
@@ -3556,6 +3569,7 @@ static int find_data_references(struct reloc_control *rc,
3556 struct btrfs_extent_data_ref *ref, 3569 struct btrfs_extent_data_ref *ref,
3557 struct rb_root *blocks) 3570 struct rb_root *blocks)
3558{ 3571{
3572 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3559 struct btrfs_path *path; 3573 struct btrfs_path *path;
3560 struct tree_block *block; 3574 struct tree_block *block;
3561 struct btrfs_root *root; 3575 struct btrfs_root *root;
@@ -3582,8 +3596,7 @@ static int find_data_references(struct reloc_control *rc,
3582 * it and redo the search. 3596 * it and redo the search.
3583 */ 3597 */
3584 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) { 3598 if (ref_root == BTRFS_ROOT_TREE_OBJECTID) {
3585 ret = delete_block_group_cache(rc->extent_root->fs_info, 3599 ret = delete_block_group_cache(fs_info, rc->block_group,
3586 rc->block_group,
3587 NULL, ref_objectid); 3600 NULL, ref_objectid);
3588 if (ret != -ENOENT) 3601 if (ret != -ENOENT)
3589 return ret; 3602 return ret;
@@ -3595,7 +3608,7 @@ static int find_data_references(struct reloc_control *rc,
3595 return -ENOMEM; 3608 return -ENOMEM;
3596 path->reada = READA_FORWARD; 3609 path->reada = READA_FORWARD;
3597 3610
3598 root = read_fs_root(rc->extent_root->fs_info, ref_root); 3611 root = read_fs_root(fs_info, ref_root);
3599 if (IS_ERR(root)) { 3612 if (IS_ERR(root)) {
3600 err = PTR_ERR(root); 3613 err = PTR_ERR(root);
3601 goto out; 3614 goto out;
@@ -3821,6 +3834,7 @@ static noinline_for_stack
3821int find_next_extent(struct reloc_control *rc, struct btrfs_path *path, 3834int find_next_extent(struct reloc_control *rc, struct btrfs_path *path,
3822 struct btrfs_key *extent_key) 3835 struct btrfs_key *extent_key)
3823{ 3836{
3837 struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3824 struct btrfs_key key; 3838 struct btrfs_key key;
3825 struct extent_buffer *leaf; 3839 struct extent_buffer *leaf;
3826 u64 start, end, last; 3840 u64 start, end, last;
@@ -3872,7 +3886,7 @@ next:
3872 } 3886 }
3873 3887
3874 if (key.type == BTRFS_METADATA_ITEM_KEY && 3888 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3875 key.objectid + rc->extent_root->fs_info->nodesize <= 3889 key.objectid + fs_info->nodesize <=
3876 rc->search_start) { 3890 rc->search_start) {
3877 path->slots[0]++; 3891 path->slots[0]++;
3878 goto next; 3892 goto next;
@@ -3890,7 +3904,7 @@ next:
3890 rc->search_start = key.objectid + key.offset; 3904 rc->search_start = key.objectid + key.offset;
3891 else 3905 else
3892 rc->search_start = key.objectid + 3906 rc->search_start = key.objectid +
3893 rc->extent_root->fs_info->nodesize; 3907 fs_info->nodesize;
3894 memcpy(extent_key, &key, sizeof(key)); 3908 memcpy(extent_key, &key, sizeof(key));
3895 return 0; 3909 return 0;
3896 } 3910 }
@@ -4233,7 +4247,7 @@ struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
4233 key.objectid = objectid; 4247 key.objectid = objectid;
4234 key.type = BTRFS_INODE_ITEM_KEY; 4248 key.type = BTRFS_INODE_ITEM_KEY;
4235 key.offset = 0; 4249 key.offset = 0;
4236 inode = btrfs_iget(root->fs_info->sb, &key, root, NULL); 4250 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
4237 BUG_ON(IS_ERR(inode) || is_bad_inode(inode)); 4251 BUG_ON(IS_ERR(inode) || is_bad_inode(inode));
4238 BTRFS_I(inode)->index_cnt = group->key.objectid; 4252 BTRFS_I(inode)->index_cnt = group->key.objectid;
4239 4253
@@ -4360,7 +4374,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
4360 goto out; 4374 goto out;
4361 } 4375 }
4362 4376
4363 describe_relocation(extent_root->fs_info, rc->block_group); 4377 describe_relocation(fs_info, rc->block_group);
4364 4378
4365 btrfs_wait_block_group_reservations(rc->block_group); 4379 btrfs_wait_block_group_reservations(rc->block_group);
4366 btrfs_wait_nocow_writers(rc->block_group); 4380 btrfs_wait_nocow_writers(rc->block_group);
@@ -4380,8 +4394,7 @@ int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start)
4380 if (rc->extents_found == 0) 4394 if (rc->extents_found == 0)
4381 break; 4395 break;
4382 4396
4383 btrfs_info(extent_root->fs_info, "found %llu extents", 4397 btrfs_info(fs_info, "found %llu extents", rc->extents_found);
4384 rc->extents_found);
4385 4398
4386 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) { 4399 if (rc->stage == MOVE_DATA_EXTENTS && rc->found_file_extent) {
4387 ret = btrfs_wait_ordered_range(rc->data_inode, 0, 4400 ret = btrfs_wait_ordered_range(rc->data_inode, 0,
@@ -4410,10 +4423,11 @@ out:
4410 4423
4411static noinline_for_stack int mark_garbage_root(struct btrfs_root *root) 4424static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4412{ 4425{
4426 struct btrfs_fs_info *fs_info = root->fs_info;
4413 struct btrfs_trans_handle *trans; 4427 struct btrfs_trans_handle *trans;
4414 int ret, err; 4428 int ret, err;
4415 4429
4416 trans = btrfs_start_transaction(root->fs_info->tree_root, 0); 4430 trans = btrfs_start_transaction(fs_info->tree_root, 0);
4417 if (IS_ERR(trans)) 4431 if (IS_ERR(trans))
4418 return PTR_ERR(trans); 4432 return PTR_ERR(trans);
4419 4433
@@ -4421,10 +4435,10 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4421 sizeof(root->root_item.drop_progress)); 4435 sizeof(root->root_item.drop_progress));
4422 root->root_item.drop_level = 0; 4436 root->root_item.drop_level = 0;
4423 btrfs_set_root_refs(&root->root_item, 0); 4437 btrfs_set_root_refs(&root->root_item, 0);
4424 ret = btrfs_update_root(trans, root->fs_info->tree_root, 4438 ret = btrfs_update_root(trans, fs_info->tree_root,
4425 &root->root_key, &root->root_item); 4439 &root->root_key, &root->root_item);
4426 4440
4427 err = btrfs_end_transaction(trans, root->fs_info->tree_root); 4441 err = btrfs_end_transaction(trans, fs_info->tree_root);
4428 if (err) 4442 if (err)
4429 return err; 4443 return err;
4430 return ret; 4444 return ret;
@@ -4438,6 +4452,7 @@ static noinline_for_stack int mark_garbage_root(struct btrfs_root *root)
4438 */ 4452 */
4439int btrfs_recover_relocation(struct btrfs_root *root) 4453int btrfs_recover_relocation(struct btrfs_root *root)
4440{ 4454{
4455 struct btrfs_fs_info *fs_info = root->fs_info;
4441 LIST_HEAD(reloc_roots); 4456 LIST_HEAD(reloc_roots);
4442 struct btrfs_key key; 4457 struct btrfs_key key;
4443 struct btrfs_root *fs_root; 4458 struct btrfs_root *fs_root;
@@ -4459,7 +4474,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4459 key.offset = (u64)-1; 4474 key.offset = (u64)-1;
4460 4475
4461 while (1) { 4476 while (1) {
4462 ret = btrfs_search_slot(NULL, root->fs_info->tree_root, &key, 4477 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key,
4463 path, 0, 0); 4478 path, 0, 0);
4464 if (ret < 0) { 4479 if (ret < 0) {
4465 err = ret; 4480 err = ret;
@@ -4487,7 +4502,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4487 list_add(&reloc_root->root_list, &reloc_roots); 4502 list_add(&reloc_root->root_list, &reloc_roots);
4488 4503
4489 if (btrfs_root_refs(&reloc_root->root_item) > 0) { 4504 if (btrfs_root_refs(&reloc_root->root_item) > 0) {
4490 fs_root = read_fs_root(root->fs_info, 4505 fs_root = read_fs_root(fs_info,
4491 reloc_root->root_key.offset); 4506 reloc_root->root_key.offset);
4492 if (IS_ERR(fs_root)) { 4507 if (IS_ERR(fs_root)) {
4493 ret = PTR_ERR(fs_root); 4508 ret = PTR_ERR(fs_root);
@@ -4513,13 +4528,13 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4513 if (list_empty(&reloc_roots)) 4528 if (list_empty(&reloc_roots))
4514 goto out; 4529 goto out;
4515 4530
4516 rc = alloc_reloc_control(root->fs_info); 4531 rc = alloc_reloc_control(fs_info);
4517 if (!rc) { 4532 if (!rc) {
4518 err = -ENOMEM; 4533 err = -ENOMEM;
4519 goto out; 4534 goto out;
4520 } 4535 }
4521 4536
4522 rc->extent_root = root->fs_info->extent_root; 4537 rc->extent_root = fs_info->extent_root;
4523 4538
4524 set_reloc_control(rc); 4539 set_reloc_control(rc);
4525 4540
@@ -4543,8 +4558,7 @@ int btrfs_recover_relocation(struct btrfs_root *root)
4543 continue; 4558 continue;
4544 } 4559 }
4545 4560
4546 fs_root = read_fs_root(root->fs_info, 4561 fs_root = read_fs_root(fs_info, reloc_root->root_key.offset);
4547 reloc_root->root_key.offset);
4548 if (IS_ERR(fs_root)) { 4562 if (IS_ERR(fs_root)) {
4549 err = PTR_ERR(fs_root); 4563 err = PTR_ERR(fs_root);
4550 goto out_free; 4564 goto out_free;
@@ -4579,8 +4593,7 @@ out:
4579 4593
4580 if (err == 0) { 4594 if (err == 0) {
4581 /* cleanup orphan inode in data relocation tree */ 4595 /* cleanup orphan inode in data relocation tree */
4582 fs_root = read_fs_root(root->fs_info, 4596 fs_root = read_fs_root(fs_info, BTRFS_DATA_RELOC_TREE_OBJECTID);
4583 BTRFS_DATA_RELOC_TREE_OBJECTID);
4584 if (IS_ERR(fs_root)) 4597 if (IS_ERR(fs_root))
4585 err = PTR_ERR(fs_root); 4598 err = PTR_ERR(fs_root);
4586 else 4599 else
@@ -4597,9 +4610,9 @@ out:
4597 */ 4610 */
4598int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len) 4611int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4599{ 4612{
4613 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4600 struct btrfs_ordered_sum *sums; 4614 struct btrfs_ordered_sum *sums;
4601 struct btrfs_ordered_extent *ordered; 4615 struct btrfs_ordered_extent *ordered;
4602 struct btrfs_root *root = BTRFS_I(inode)->root;
4603 int ret; 4616 int ret;
4604 u64 disk_bytenr; 4617 u64 disk_bytenr;
4605 u64 new_bytenr; 4618 u64 new_bytenr;
@@ -4609,7 +4622,7 @@ int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
4609 BUG_ON(ordered->file_offset != file_pos || ordered->len != len); 4622 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
4610 4623
4611 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt; 4624 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4612 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr, 4625 ret = btrfs_lookup_csums_range(fs_info->csum_root, disk_bytenr,
4613 disk_bytenr + len - 1, &list, 0); 4626 disk_bytenr + len - 1, &list, 0);
4614 if (ret) 4627 if (ret)
4615 goto out; 4628 goto out;
@@ -4644,13 +4657,14 @@ int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root, struct extent_buffer *buf, 4657 struct btrfs_root *root, struct extent_buffer *buf,
4645 struct extent_buffer *cow) 4658 struct extent_buffer *cow)
4646{ 4659{
4660 struct btrfs_fs_info *fs_info = root->fs_info;
4647 struct reloc_control *rc; 4661 struct reloc_control *rc;
4648 struct backref_node *node; 4662 struct backref_node *node;
4649 int first_cow = 0; 4663 int first_cow = 0;
4650 int level; 4664 int level;
4651 int ret = 0; 4665 int ret = 0;
4652 4666
4653 rc = root->fs_info->reloc_ctl; 4667 rc = fs_info->reloc_ctl;
4654 if (!rc) 4668 if (!rc)
4655 return 0; 4669 return 0;
4656 4670
diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 60e711ae044a..f7f6cb7d9a62 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -132,6 +132,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
132 *root, struct btrfs_key *key, struct btrfs_root_item 132 *root, struct btrfs_key *key, struct btrfs_root_item
133 *item) 133 *item)
134{ 134{
135 struct btrfs_fs_info *fs_info = root->fs_info;
135 struct btrfs_path *path; 136 struct btrfs_path *path;
136 struct extent_buffer *l; 137 struct extent_buffer *l;
137 int ret; 138 int ret;
@@ -151,8 +152,7 @@ int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
151 152
152 if (ret != 0) { 153 if (ret != 0) {
153 btrfs_print_leaf(root, path->nodes[0]); 154 btrfs_print_leaf(root, path->nodes[0]);
154 btrfs_crit(root->fs_info, 155 btrfs_crit(fs_info, "unable to update root key %llu %u %llu",
155 "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);
158 } 158 }
@@ -228,7 +228,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
228 int ret; 228 int ret;
229 bool can_recover = true; 229 bool can_recover = true;
230 230
231 if (tree_root->fs_info->sb->s_flags & MS_RDONLY) 231 if (fs_info->sb->s_flags & MS_RDONLY)
232 can_recover = false; 232 can_recover = false;
233 233
234 path = btrfs_alloc_path(); 234 path = btrfs_alloc_path();
@@ -276,8 +276,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
276 * in turn reads and inserts fs roots while doing backref 276 * in turn reads and inserts fs roots while doing backref
277 * walking. 277 * walking.
278 */ 278 */
279 root = btrfs_lookup_fs_root(tree_root->fs_info, 279 root = btrfs_lookup_fs_root(fs_info, root_key.objectid);
280 root_key.objectid);
281 if (root) { 280 if (root) {
282 WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, 281 WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
283 &root->state)); 282 &root->state));
@@ -298,7 +297,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
298 trans = btrfs_join_transaction(tree_root); 297 trans = btrfs_join_transaction(tree_root);
299 if (IS_ERR(trans)) { 298 if (IS_ERR(trans)) {
300 err = PTR_ERR(trans); 299 err = PTR_ERR(trans);
301 btrfs_handle_fs_error(tree_root->fs_info, err, 300 btrfs_handle_fs_error(fs_info, err,
302 "Failed to start trans to delete orphan item"); 301 "Failed to start trans to delete orphan item");
303 break; 302 break;
304 } 303 }
@@ -306,7 +305,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
306 root_key.objectid); 305 root_key.objectid);
307 btrfs_end_transaction(trans, tree_root); 306 btrfs_end_transaction(trans, tree_root);
308 if (err) { 307 if (err) {
309 btrfs_handle_fs_error(tree_root->fs_info, err, 308 btrfs_handle_fs_error(fs_info, err,
310 "Failed to delete root orphan item"); 309 "Failed to delete root orphan item");
311 break; 310 break;
312 } 311 }
@@ -321,7 +320,7 @@ int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
321 320
322 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state); 321 set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
323 322
324 err = btrfs_insert_fs_root(root->fs_info, root); 323 err = btrfs_insert_fs_root(fs_info, root);
325 if (err) { 324 if (err) {
326 BUG_ON(err == -EEXIST); 325 BUG_ON(err == -EEXIST);
327 btrfs_free_fs_root(root); 326 btrfs_free_fs_root(root);
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 6834ff8dc0ab..8aef00b0ff84 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -489,8 +489,8 @@ struct scrub_ctx *scrub_setup_ctx(struct btrfs_device *dev, int is_dev_replace)
489 sctx->bios[i]->next_free = -1; 489 sctx->bios[i]->next_free = -1;
490 } 490 }
491 sctx->first_free = 0; 491 sctx->first_free = 0;
492 sctx->nodesize = dev->fs_info->nodesize; 492 sctx->nodesize = fs_info->nodesize;
493 sctx->sectorsize = dev->fs_info->sectorsize; 493 sctx->sectorsize = fs_info->sectorsize;
494 atomic_set(&sctx->bios_in_flight, 0); 494 atomic_set(&sctx->bios_in_flight, 0);
495 atomic_set(&sctx->workers_pending, 0); 495 atomic_set(&sctx->workers_pending, 0);
496 atomic_set(&sctx->cancel_req, 0); 496 atomic_set(&sctx->cancel_req, 0);
@@ -789,6 +789,7 @@ out:
789 789
790static void scrub_fixup_nodatasum(struct btrfs_work *work) 790static void scrub_fixup_nodatasum(struct btrfs_work *work)
791{ 791{
792 struct btrfs_fs_info *fs_info;
792 int ret; 793 int ret;
793 struct scrub_fixup_nodatasum *fixup; 794 struct scrub_fixup_nodatasum *fixup;
794 struct scrub_ctx *sctx; 795 struct scrub_ctx *sctx;
@@ -798,6 +799,7 @@ static void scrub_fixup_nodatasum(struct btrfs_work *work)
798 799
799 fixup = container_of(work, struct scrub_fixup_nodatasum, work); 800 fixup = container_of(work, struct scrub_fixup_nodatasum, work);
800 sctx = fixup->sctx; 801 sctx = fixup->sctx;
802 fs_info = fixup->root->fs_info;
801 803
802 path = btrfs_alloc_path(); 804 path = btrfs_alloc_path();
803 if (!path) { 805 if (!path) {
@@ -823,9 +825,8 @@ static void scrub_fixup_nodatasum(struct btrfs_work *work)
823 * (once it's finished) and rewrite the failed sector if a good copy 825 * (once it's finished) and rewrite the failed sector if a good copy
824 * can be found. 826 * can be found.
825 */ 827 */
826 ret = iterate_inodes_from_logical(fixup->logical, fixup->root->fs_info, 828 ret = iterate_inodes_from_logical(fixup->logical, fs_info, path,
827 path, scrub_fixup_readpage, 829 scrub_fixup_readpage, fixup);
828 fixup);
829 if (ret < 0) { 830 if (ret < 0) {
830 uncorrectable = 1; 831 uncorrectable = 1;
831 goto out; 832 goto out;
@@ -843,9 +844,9 @@ out:
843 spin_lock(&sctx->stat_lock); 844 spin_lock(&sctx->stat_lock);
844 ++sctx->stat.uncorrectable_errors; 845 ++sctx->stat.uncorrectable_errors;
845 spin_unlock(&sctx->stat_lock); 846 spin_unlock(&sctx->stat_lock);
846 btrfs_dev_replace_stats_inc(&sctx->fs_info->dev_replace. 847 btrfs_dev_replace_stats_inc(
847 num_uncorrectable_read_errors); 848 &fs_info->dev_replace.num_uncorrectable_read_errors);
848 btrfs_err_rl_in_rcu(sctx->fs_info, 849 btrfs_err_rl_in_rcu(fs_info,
849 "unable to fixup (nodatasum) error at logical %llu on dev %s", 850 "unable to fixup (nodatasum) error at logical %llu on dev %s",
850 fixup->logical, rcu_str_deref(fixup->dev->name)); 851 fixup->logical, rcu_str_deref(fixup->dev->name));
851 } 852 }
@@ -1176,8 +1177,7 @@ nodatasum_case:
1176 if (scrub_write_page_to_dev_replace(sblock_other, 1177 if (scrub_write_page_to_dev_replace(sblock_other,
1177 page_num) != 0) { 1178 page_num) != 0) {
1178 btrfs_dev_replace_stats_inc( 1179 btrfs_dev_replace_stats_inc(
1179 &sctx->fs_info->dev_replace. 1180 &fs_info->dev_replace.num_write_errors);
1180 num_write_errors);
1181 success = 0; 1181 success = 0;
1182 } 1182 }
1183 } else if (sblock_other) { 1183 } else if (sblock_other) {
@@ -1563,6 +1563,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1563{ 1563{
1564 struct scrub_page *page_bad = sblock_bad->pagev[page_num]; 1564 struct scrub_page *page_bad = sblock_bad->pagev[page_num];
1565 struct scrub_page *page_good = sblock_good->pagev[page_num]; 1565 struct scrub_page *page_good = sblock_good->pagev[page_num];
1566 struct btrfs_fs_info *fs_info = sblock_bad->sctx->fs_info;
1566 1567
1567 BUG_ON(page_bad->page == NULL); 1568 BUG_ON(page_bad->page == NULL);
1568 BUG_ON(page_good->page == NULL); 1569 BUG_ON(page_good->page == NULL);
@@ -1572,7 +1573,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1572 int ret; 1573 int ret;
1573 1574
1574 if (!page_bad->dev->bdev) { 1575 if (!page_bad->dev->bdev) {
1575 btrfs_warn_rl(sblock_bad->sctx->fs_info, 1576 btrfs_warn_rl(fs_info,
1576 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected"); 1577 "scrub_repair_page_from_good_copy(bdev == NULL) is unexpected");
1577 return -EIO; 1578 return -EIO;
1578 } 1579 }
@@ -1594,8 +1595,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1594 btrfs_dev_stat_inc_and_print(page_bad->dev, 1595 btrfs_dev_stat_inc_and_print(page_bad->dev,
1595 BTRFS_DEV_STAT_WRITE_ERRS); 1596 BTRFS_DEV_STAT_WRITE_ERRS);
1596 btrfs_dev_replace_stats_inc( 1597 btrfs_dev_replace_stats_inc(
1597 &sblock_bad->sctx->fs_info-> 1598 &fs_info->dev_replace.num_write_errors);
1598 dev_replace.num_write_errors);
1599 bio_put(bio); 1599 bio_put(bio);
1600 return -EIO; 1600 return -EIO;
1601 } 1601 }
@@ -1607,6 +1607,7 @@ static int scrub_repair_page_from_good_copy(struct scrub_block *sblock_bad,
1607 1607
1608static void scrub_write_block_to_dev_replace(struct scrub_block *sblock) 1608static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1609{ 1609{
1610 struct btrfs_fs_info *fs_info = sblock->sctx->fs_info;
1610 int page_num; 1611 int page_num;
1611 1612
1612 /* 1613 /*
@@ -1622,8 +1623,7 @@ static void scrub_write_block_to_dev_replace(struct scrub_block *sblock)
1622 ret = scrub_write_page_to_dev_replace(sblock, page_num); 1623 ret = scrub_write_page_to_dev_replace(sblock, page_num);
1623 if (ret) 1624 if (ret)
1624 btrfs_dev_replace_stats_inc( 1625 btrfs_dev_replace_stats_inc(
1625 &sblock->sctx->fs_info->dev_replace. 1626 &fs_info->dev_replace.num_write_errors);
1626 num_write_errors);
1627 } 1627 }
1628} 1628}
1629 1629
@@ -1857,8 +1857,7 @@ static int scrub_checksum_tree_block(struct scrub_block *sblock)
1857{ 1857{
1858 struct scrub_ctx *sctx = sblock->sctx; 1858 struct scrub_ctx *sctx = sblock->sctx;
1859 struct btrfs_header *h; 1859 struct btrfs_header *h;
1860 struct btrfs_root *root = sctx->fs_info->dev_root; 1860 struct btrfs_fs_info *fs_info = sctx->fs_info;
1861 struct btrfs_fs_info *fs_info = root->fs_info;
1862 u8 calculated_csum[BTRFS_CSUM_SIZE]; 1861 u8 calculated_csum[BTRFS_CSUM_SIZE];
1863 u8 on_disk_csum[BTRFS_CSUM_SIZE]; 1862 u8 on_disk_csum[BTRFS_CSUM_SIZE];
1864 struct page *page; 1863 struct page *page;
@@ -2138,6 +2137,7 @@ static void scrub_missing_raid56_worker(struct btrfs_work *work)
2138{ 2137{
2139 struct scrub_block *sblock = container_of(work, struct scrub_block, work); 2138 struct scrub_block *sblock = container_of(work, struct scrub_block, work);
2140 struct scrub_ctx *sctx = sblock->sctx; 2139 struct scrub_ctx *sctx = sblock->sctx;
2140 struct btrfs_fs_info *fs_info = sctx->fs_info;
2141 u64 logical; 2141 u64 logical;
2142 struct btrfs_device *dev; 2142 struct btrfs_device *dev;
2143 2143
@@ -2151,14 +2151,14 @@ static void scrub_missing_raid56_worker(struct btrfs_work *work)
2151 spin_lock(&sctx->stat_lock); 2151 spin_lock(&sctx->stat_lock);
2152 sctx->stat.read_errors++; 2152 sctx->stat.read_errors++;
2153 spin_unlock(&sctx->stat_lock); 2153 spin_unlock(&sctx->stat_lock);
2154 btrfs_err_rl_in_rcu(sctx->fs_info, 2154 btrfs_err_rl_in_rcu(fs_info,
2155 "IO error rebuilding logical %llu for dev %s", 2155 "IO error rebuilding logical %llu for dev %s",
2156 logical, rcu_str_deref(dev->name)); 2156 logical, rcu_str_deref(dev->name));
2157 } else if (sblock->header_error || sblock->checksum_error) { 2157 } else if (sblock->header_error || sblock->checksum_error) {
2158 spin_lock(&sctx->stat_lock); 2158 spin_lock(&sctx->stat_lock);
2159 sctx->stat.uncorrectable_errors++; 2159 sctx->stat.uncorrectable_errors++;
2160 spin_unlock(&sctx->stat_lock); 2160 spin_unlock(&sctx->stat_lock);
2161 btrfs_err_rl_in_rcu(sctx->fs_info, 2161 btrfs_err_rl_in_rcu(fs_info,
2162 "failed to rebuild valid logical %llu for dev %s", 2162 "failed to rebuild valid logical %llu for dev %s",
2163 logical, rcu_str_deref(dev->name)); 2163 logical, rcu_str_deref(dev->name));
2164 } else { 2164 } else {
@@ -2749,6 +2749,7 @@ static void scrub_parity_bio_endio_worker(struct btrfs_work *work)
2749static void scrub_parity_bio_endio(struct bio *bio) 2749static void scrub_parity_bio_endio(struct bio *bio)
2750{ 2750{
2751 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private; 2751 struct scrub_parity *sparity = (struct scrub_parity *)bio->bi_private;
2752 struct btrfs_fs_info *fs_info = sparity->sctx->fs_info;
2752 2753
2753 if (bio->bi_error) 2754 if (bio->bi_error)
2754 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap, 2755 bitmap_or(sparity->ebitmap, sparity->ebitmap, sparity->dbitmap,
@@ -2758,14 +2759,14 @@ static void scrub_parity_bio_endio(struct bio *bio)
2758 2759
2759 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper, 2760 btrfs_init_work(&sparity->work, btrfs_scrubparity_helper,
2760 scrub_parity_bio_endio_worker, NULL, NULL); 2761 scrub_parity_bio_endio_worker, NULL, NULL);
2761 btrfs_queue_work(sparity->sctx->fs_info->scrub_parity_workers, 2762 btrfs_queue_work(fs_info->scrub_parity_workers, &sparity->work);
2762 &sparity->work);
2763} 2763}
2764 2764
2765static void scrub_parity_check_and_repair(struct scrub_parity *sparity) 2765static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2766{ 2766{
2767 struct scrub_ctx *sctx = sparity->sctx; 2767 struct scrub_ctx *sctx = sparity->sctx;
2768 struct btrfs_root *dev_root = sctx->fs_info->dev_root; 2768 struct btrfs_fs_info *fs_info = sctx->fs_info;
2769 struct btrfs_root *dev_root = fs_info->dev_root;
2769 struct bio *bio; 2770 struct bio *bio;
2770 struct btrfs_raid_bio *rbio; 2771 struct btrfs_raid_bio *rbio;
2771 struct scrub_page *spage; 2772 struct scrub_page *spage;
@@ -2778,8 +2779,7 @@ static void scrub_parity_check_and_repair(struct scrub_parity *sparity)
2778 goto out; 2779 goto out;
2779 2780
2780 length = sparity->logic_end - sparity->logic_start; 2781 length = sparity->logic_end - sparity->logic_start;
2781 ret = btrfs_map_sblock(sctx->fs_info, BTRFS_MAP_WRITE, 2782 ret = btrfs_map_sblock(fs_info, BTRFS_MAP_WRITE, sparity->logic_start,
2782 sparity->logic_start,
2783 &length, &bbio, 0, 1); 2783 &length, &bbio, 0, 1);
2784 if (ret || !bbio || !bbio->raid_map) 2784 if (ret || !bbio || !bbio->raid_map)
2785 goto bbio_out; 2785 goto bbio_out;
@@ -2866,7 +2866,7 @@ static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2866 int extent_mirror_num; 2866 int extent_mirror_num;
2867 int stop_loop = 0; 2867 int stop_loop = 0;
2868 2868
2869 nsectors = div_u64(map->stripe_len, root->fs_info->sectorsize); 2869 nsectors = div_u64(map->stripe_len, fs_info->sectorsize);
2870 bitmap_len = scrub_calc_parity_bitmap_len(nsectors); 2870 bitmap_len = scrub_calc_parity_bitmap_len(nsectors);
2871 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len, 2871 sparity = kzalloc(sizeof(struct scrub_parity) + 2 * bitmap_len,
2872 GFP_NOFS); 2872 GFP_NOFS);
@@ -2937,7 +2937,7 @@ static noinline_for_stack int scrub_raid56_parity(struct scrub_ctx *sctx,
2937 goto next; 2937 goto next;
2938 2938
2939 if (key.type == BTRFS_METADATA_ITEM_KEY) 2939 if (key.type == BTRFS_METADATA_ITEM_KEY)
2940 bytes = root->fs_info->nodesize; 2940 bytes = fs_info->nodesize;
2941 else 2941 else
2942 bytes = key.offset; 2942 bytes = key.offset;
2943 2943
@@ -3290,7 +3290,7 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
3290 goto next; 3290 goto next;
3291 3291
3292 if (key.type == BTRFS_METADATA_ITEM_KEY) 3292 if (key.type == BTRFS_METADATA_ITEM_KEY)
3293 bytes = root->fs_info->nodesize; 3293 bytes = fs_info->nodesize;
3294 else 3294 else
3295 bytes = key.offset; 3295 bytes = key.offset;
3296 3296
@@ -3497,8 +3497,8 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
3497{ 3497{
3498 struct btrfs_dev_extent *dev_extent = NULL; 3498 struct btrfs_dev_extent *dev_extent = NULL;
3499 struct btrfs_path *path; 3499 struct btrfs_path *path;
3500 struct btrfs_root *root = sctx->fs_info->dev_root; 3500 struct btrfs_fs_info *fs_info = sctx->fs_info;
3501 struct btrfs_fs_info *fs_info = root->fs_info; 3501 struct btrfs_root *root = fs_info->dev_root;
3502 u64 length; 3502 u64 length;
3503 u64 chunk_offset; 3503 u64 chunk_offset;
3504 int ret = 0; 3504 int ret = 0;
@@ -3747,16 +3747,16 @@ static noinline_for_stack int scrub_supers(struct scrub_ctx *sctx,
3747 u64 bytenr; 3747 u64 bytenr;
3748 u64 gen; 3748 u64 gen;
3749 int ret; 3749 int ret;
3750 struct btrfs_root *root = sctx->fs_info->dev_root; 3750 struct btrfs_fs_info *fs_info = sctx->fs_info;
3751 3751
3752 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 3752 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3753 return -EIO; 3753 return -EIO;
3754 3754
3755 /* Seed devices of a new filesystem has their own generation. */ 3755 /* Seed devices of a new filesystem has their own generation. */
3756 if (scrub_dev->fs_devices != root->fs_info->fs_devices) 3756 if (scrub_dev->fs_devices != fs_info->fs_devices)
3757 gen = scrub_dev->generation; 3757 gen = scrub_dev->generation;
3758 else 3758 else
3759 gen = root->fs_info->last_trans_committed; 3759 gen = fs_info->last_trans_committed;
3760 3760
3761 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 3761 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
3762 bytenr = btrfs_sb_offset(i); 3762 bytenr = btrfs_sb_offset(i);
@@ -4052,16 +4052,17 @@ int btrfs_scrub_cancel_dev(struct btrfs_fs_info *fs_info,
4052int btrfs_scrub_progress(struct btrfs_root *root, u64 devid, 4052int btrfs_scrub_progress(struct btrfs_root *root, u64 devid,
4053 struct btrfs_scrub_progress *progress) 4053 struct btrfs_scrub_progress *progress)
4054{ 4054{
4055 struct btrfs_fs_info *fs_info = root->fs_info;
4055 struct btrfs_device *dev; 4056 struct btrfs_device *dev;
4056 struct scrub_ctx *sctx = NULL; 4057 struct scrub_ctx *sctx = NULL;
4057 4058
4058 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 4059 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4059 dev = btrfs_find_device(root->fs_info, devid, NULL, NULL); 4060 dev = btrfs_find_device(fs_info, devid, NULL, NULL);
4060 if (dev) 4061 if (dev)
4061 sctx = dev->scrub_device; 4062 sctx = dev->scrub_device;
4062 if (sctx) 4063 if (sctx)
4063 memcpy(progress, &sctx->stat, sizeof(*progress)); 4064 memcpy(progress, &sctx->stat, sizeof(*progress));
4064 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 4065 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4065 4066
4066 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV; 4067 return dev ? (sctx ? 0 : -ENOTCONN) : -ENODEV;
4067} 4068}
@@ -4171,20 +4172,17 @@ static void copy_nocow_pages_worker(struct btrfs_work *work)
4171 struct scrub_copy_nocow_ctx *nocow_ctx = 4172 struct scrub_copy_nocow_ctx *nocow_ctx =
4172 container_of(work, struct scrub_copy_nocow_ctx, work); 4173 container_of(work, struct scrub_copy_nocow_ctx, work);
4173 struct scrub_ctx *sctx = nocow_ctx->sctx; 4174 struct scrub_ctx *sctx = nocow_ctx->sctx;
4175 struct btrfs_fs_info *fs_info = sctx->fs_info;
4176 struct btrfs_root *root = fs_info->extent_root;
4174 u64 logical = nocow_ctx->logical; 4177 u64 logical = nocow_ctx->logical;
4175 u64 len = nocow_ctx->len; 4178 u64 len = nocow_ctx->len;
4176 int mirror_num = nocow_ctx->mirror_num; 4179 int mirror_num = nocow_ctx->mirror_num;
4177 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace; 4180 u64 physical_for_dev_replace = nocow_ctx->physical_for_dev_replace;
4178 int ret; 4181 int ret;
4179 struct btrfs_trans_handle *trans = NULL; 4182 struct btrfs_trans_handle *trans = NULL;
4180 struct btrfs_fs_info *fs_info;
4181 struct btrfs_path *path; 4183 struct btrfs_path *path;
4182 struct btrfs_root *root;
4183 int not_written = 0; 4184 int not_written = 0;
4184 4185
4185 fs_info = sctx->fs_info;
4186 root = fs_info->extent_root;
4187
4188 path = btrfs_alloc_path(); 4186 path = btrfs_alloc_path();
4189 if (!path) { 4187 if (!path) {
4190 spin_lock(&sctx->stat_lock); 4188 spin_lock(&sctx->stat_lock);
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index 78845cc81dd6..399b36b2a182 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -1431,9 +1431,9 @@ static int find_extent_clone(struct send_ctx *sctx,
1431 extent_item_pos = logical - found_key.objectid; 1431 extent_item_pos = logical - found_key.objectid;
1432 else 1432 else
1433 extent_item_pos = 0; 1433 extent_item_pos = 0;
1434 ret = iterate_extent_inodes(fs_info, 1434 ret = iterate_extent_inodes(fs_info, found_key.objectid,
1435 found_key.objectid, extent_item_pos, 1, 1435 extent_item_pos, 1, __iterate_backrefs,
1436 __iterate_backrefs, backref_ctx); 1436 backref_ctx);
1437 1437
1438 if (ret < 0) 1438 if (ret < 0)
1439 goto out; 1439 goto out;
@@ -6137,17 +6137,17 @@ static void btrfs_root_dec_send_in_progress(struct btrfs_root* root)
6137 */ 6137 */
6138 if (root->send_in_progress < 0) 6138 if (root->send_in_progress < 0)
6139 btrfs_err(root->fs_info, 6139 btrfs_err(root->fs_info,
6140 "send_in_progres unbalanced %d root %llu", 6140 "send_in_progres unbalanced %d root %llu",
6141 root->send_in_progress, root->root_key.objectid); 6141 root->send_in_progress, root->root_key.objectid);
6142 spin_unlock(&root->root_item_lock); 6142 spin_unlock(&root->root_item_lock);
6143} 6143}
6144 6144
6145long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_) 6145long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6146{ 6146{
6147 int ret = 0; 6147 int ret = 0;
6148 struct btrfs_root *send_root; 6148 struct btrfs_root *send_root = BTRFS_I(file_inode(mnt_file))->root;
6149 struct btrfs_fs_info *fs_info = send_root->fs_info;
6149 struct btrfs_root *clone_root; 6150 struct btrfs_root *clone_root;
6150 struct btrfs_fs_info *fs_info;
6151 struct btrfs_ioctl_send_args *arg = NULL; 6151 struct btrfs_ioctl_send_args *arg = NULL;
6152 struct btrfs_key key; 6152 struct btrfs_key key;
6153 struct send_ctx *sctx = NULL; 6153 struct send_ctx *sctx = NULL;
@@ -6161,9 +6161,6 @@ long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_)
6161 if (!capable(CAP_SYS_ADMIN)) 6161 if (!capable(CAP_SYS_ADMIN))
6162 return -EPERM; 6162 return -EPERM;
6163 6163
6164 send_root = BTRFS_I(file_inode(mnt_file))->root;
6165 fs_info = send_root->fs_info;
6166
6167 /* 6164 /*
6168 * The subvolume must remain read-only during send, protect against 6165 * The subvolume must remain read-only during send, protect against
6169 * making it RW. This also protects against deletion. 6166 * making it RW. This also protects against deletion.
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index c91a51f1e8f2..fa2f33fe8c6c 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -411,8 +411,8 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
411 bool saved_compress_force; 411 bool saved_compress_force;
412 int no_compress = 0; 412 int no_compress = 0;
413 413
414 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 414 cache_gen = btrfs_super_cache_generation(info->super_copy);
415 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) 415 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE))
416 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE); 416 btrfs_set_opt(info->mount_opt, FREE_SPACE_TREE);
417 else if (cache_gen) 417 else if (cache_gen)
418 btrfs_set_opt(info->mount_opt, SPACE_CACHE); 418 btrfs_set_opt(info->mount_opt, SPACE_CACHE);
@@ -442,7 +442,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
442 token = match_token(p, tokens, args); 442 token = match_token(p, tokens, args);
443 switch (token) { 443 switch (token) {
444 case Opt_degraded: 444 case Opt_degraded:
445 btrfs_info(root->fs_info, "allowing degraded mounts"); 445 btrfs_info(info, "allowing degraded mounts");
446 btrfs_set_opt(info->mount_opt, DEGRADED); 446 btrfs_set_opt(info->mount_opt, DEGRADED);
447 break; 447 break;
448 case Opt_subvol: 448 case Opt_subvol:
@@ -461,11 +461,10 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
461 case Opt_datasum: 461 case Opt_datasum:
462 if (btrfs_test_opt(info, NODATASUM)) { 462 if (btrfs_test_opt(info, NODATASUM)) {
463 if (btrfs_test_opt(info, NODATACOW)) 463 if (btrfs_test_opt(info, NODATACOW))
464 btrfs_info(root->fs_info, 464 btrfs_info(info,
465 "setting datasum, datacow enabled"); 465 "setting datasum, datacow enabled");
466 else 466 else
467 btrfs_info(root->fs_info, 467 btrfs_info(info, "setting datasum");
468 "setting datasum");
469 } 468 }
470 btrfs_clear_opt(info->mount_opt, NODATACOW); 469 btrfs_clear_opt(info->mount_opt, NODATACOW);
471 btrfs_clear_opt(info->mount_opt, NODATASUM); 470 btrfs_clear_opt(info->mount_opt, NODATASUM);
@@ -474,11 +473,10 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
474 if (!btrfs_test_opt(info, NODATACOW)) { 473 if (!btrfs_test_opt(info, NODATACOW)) {
475 if (!btrfs_test_opt(info, COMPRESS) || 474 if (!btrfs_test_opt(info, COMPRESS) ||
476 !btrfs_test_opt(info, FORCE_COMPRESS)) { 475 !btrfs_test_opt(info, FORCE_COMPRESS)) {
477 btrfs_info(root->fs_info, 476 btrfs_info(info,
478 "setting nodatacow, compression disabled"); 477 "setting nodatacow, compression disabled");
479 } else { 478 } else {
480 btrfs_info(root->fs_info, 479 btrfs_info(info, "setting nodatacow");
481 "setting nodatacow");
482 } 480 }
483 } 481 }
484 btrfs_clear_opt(info->mount_opt, COMPRESS); 482 btrfs_clear_opt(info->mount_opt, COMPRESS);
@@ -545,8 +543,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
545 compress_force != saved_compress_force)) || 543 compress_force != saved_compress_force)) ||
546 (!btrfs_test_opt(info, COMPRESS) && 544 (!btrfs_test_opt(info, COMPRESS) &&
547 no_compress == 1)) { 545 no_compress == 1)) {
548 btrfs_info(root->fs_info, 546 btrfs_info(info, "%s %s compression",
549 "%s %s compression",
550 (compress_force) ? "force" : "use", 547 (compress_force) ? "force" : "use",
551 compress_type); 548 compress_type);
552 } 549 }
@@ -594,10 +591,10 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
594 if (info->max_inline) { 591 if (info->max_inline) {
595 info->max_inline = min_t(u64, 592 info->max_inline = min_t(u64,
596 info->max_inline, 593 info->max_inline,
597 root->fs_info->sectorsize); 594 info->sectorsize);
598 } 595 }
599 btrfs_info(root->fs_info, "max_inline at %llu", 596 btrfs_info(info, "max_inline at %llu",
600 info->max_inline); 597 info->max_inline);
601 } else { 598 } else {
602 ret = -ENOMEM; 599 ret = -ENOMEM;
603 goto out; 600 goto out;
@@ -610,8 +607,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
610 info->alloc_start = memparse(num, NULL); 607 info->alloc_start = memparse(num, NULL);
611 mutex_unlock(&info->chunk_mutex); 608 mutex_unlock(&info->chunk_mutex);
612 kfree(num); 609 kfree(num);
613 btrfs_info(root->fs_info, 610 btrfs_info(info, "allocations start at %llu",
614 "allocations start at %llu",
615 info->alloc_start); 611 info->alloc_start);
616 } else { 612 } else {
617 ret = -ENOMEM; 613 ret = -ENOMEM;
@@ -620,16 +616,15 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
620 break; 616 break;
621 case Opt_acl: 617 case Opt_acl:
622#ifdef CONFIG_BTRFS_FS_POSIX_ACL 618#ifdef CONFIG_BTRFS_FS_POSIX_ACL
623 root->fs_info->sb->s_flags |= MS_POSIXACL; 619 info->sb->s_flags |= MS_POSIXACL;
624 break; 620 break;
625#else 621#else
626 btrfs_err(root->fs_info, 622 btrfs_err(info, "support for ACL not compiled in!");
627 "support for ACL not compiled in!");
628 ret = -EINVAL; 623 ret = -EINVAL;
629 goto out; 624 goto out;
630#endif 625#endif
631 case Opt_noacl: 626 case Opt_noacl:
632 root->fs_info->sb->s_flags &= ~MS_POSIXACL; 627 info->sb->s_flags &= ~MS_POSIXACL;
633 break; 628 break;
634 case Opt_notreelog: 629 case Opt_notreelog:
635 btrfs_set_and_info(info, NOTREELOG, 630 btrfs_set_and_info(info, NOTREELOG,
@@ -658,8 +653,8 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
658 goto out; 653 goto out;
659 } else if (intarg >= 0) { 654 } else if (intarg >= 0) {
660 info->metadata_ratio = intarg; 655 info->metadata_ratio = intarg;
661 btrfs_info(root->fs_info, "metadata ratio %d", 656 btrfs_info(info, "metadata ratio %d",
662 info->metadata_ratio); 657 info->metadata_ratio);
663 } else { 658 } else {
664 ret = -EINVAL; 659 ret = -EINVAL;
665 goto out; 660 goto out;
@@ -677,15 +672,14 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
677 case Opt_space_cache_version: 672 case Opt_space_cache_version:
678 if (token == Opt_space_cache || 673 if (token == Opt_space_cache ||
679 strcmp(args[0].from, "v1") == 0) { 674 strcmp(args[0].from, "v1") == 0) {
680 btrfs_clear_opt(root->fs_info->mount_opt, 675 btrfs_clear_opt(info->mount_opt,
681 FREE_SPACE_TREE); 676 FREE_SPACE_TREE);
682 btrfs_set_and_info(info, SPACE_CACHE, 677 btrfs_set_and_info(info, SPACE_CACHE,
683 "enabling disk space caching"); 678 "enabling disk space caching");
684 } else if (strcmp(args[0].from, "v2") == 0) { 679 } else if (strcmp(args[0].from, "v2") == 0) {
685 btrfs_clear_opt(root->fs_info->mount_opt, 680 btrfs_clear_opt(info->mount_opt,
686 SPACE_CACHE); 681 SPACE_CACHE);
687 btrfs_set_and_info(info, 682 btrfs_set_and_info(info, FREE_SPACE_TREE,
688 FREE_SPACE_TREE,
689 "enabling free space tree"); 683 "enabling free space tree");
690 } else { 684 } else {
691 ret = -EINVAL; 685 ret = -EINVAL;
@@ -697,14 +691,12 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
697 break; 691 break;
698 case Opt_no_space_cache: 692 case Opt_no_space_cache:
699 if (btrfs_test_opt(info, SPACE_CACHE)) { 693 if (btrfs_test_opt(info, SPACE_CACHE)) {
700 btrfs_clear_and_info(info, 694 btrfs_clear_and_info(info, SPACE_CACHE,
701 SPACE_CACHE, 695 "disabling disk space caching");
702 "disabling disk space caching");
703 } 696 }
704 if (btrfs_test_opt(info, FREE_SPACE_TREE)) { 697 if (btrfs_test_opt(info, FREE_SPACE_TREE)) {
705 btrfs_clear_and_info(info, 698 btrfs_clear_and_info(info, FREE_SPACE_TREE,
706 FREE_SPACE_TREE, 699 "disabling free space tree");
707 "disabling free space tree");
708 } 700 }
709 break; 701 break;
710 case Opt_inode_cache: 702 case Opt_inode_cache:
@@ -737,10 +729,10 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
737 "disabling auto defrag"); 729 "disabling auto defrag");
738 break; 730 break;
739 case Opt_recovery: 731 case Opt_recovery:
740 btrfs_warn(root->fs_info, 732 btrfs_warn(info,
741 "'recovery' is deprecated, use 'usebackuproot' instead"); 733 "'recovery' is deprecated, use 'usebackuproot' instead");
742 case Opt_usebackuproot: 734 case Opt_usebackuproot:
743 btrfs_info(root->fs_info, 735 btrfs_info(info,
744 "trying to use backup root at mount time"); 736 "trying to use backup root at mount time");
745 btrfs_set_opt(info->mount_opt, USEBACKUPROOT); 737 btrfs_set_opt(info->mount_opt, USEBACKUPROOT);
746 break; 738 break;
@@ -749,14 +741,14 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
749 break; 741 break;
750#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY 742#ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
751 case Opt_check_integrity_including_extent_data: 743 case Opt_check_integrity_including_extent_data:
752 btrfs_info(root->fs_info, 744 btrfs_info(info,
753 "enabling check integrity including extent data"); 745 "enabling check integrity including extent data");
754 btrfs_set_opt(info->mount_opt, 746 btrfs_set_opt(info->mount_opt,
755 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA); 747 CHECK_INTEGRITY_INCLUDING_EXTENT_DATA);
756 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 748 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
757 break; 749 break;
758 case Opt_check_integrity: 750 case Opt_check_integrity:
759 btrfs_info(root->fs_info, "enabling check integrity"); 751 btrfs_info(info, "enabling check integrity");
760 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY); 752 btrfs_set_opt(info->mount_opt, CHECK_INTEGRITY);
761 break; 753 break;
762 case Opt_check_integrity_print_mask: 754 case Opt_check_integrity_print_mask:
@@ -765,7 +757,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
765 goto out; 757 goto out;
766 } else if (intarg >= 0) { 758 } else if (intarg >= 0) {
767 info->check_integrity_print_mask = intarg; 759 info->check_integrity_print_mask = intarg;
768 btrfs_info(root->fs_info, 760 btrfs_info(info,
769 "check_integrity_print_mask 0x%x", 761 "check_integrity_print_mask 0x%x",
770 info->check_integrity_print_mask); 762 info->check_integrity_print_mask);
771 } else { 763 } else {
@@ -777,8 +769,8 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
777 case Opt_check_integrity_including_extent_data: 769 case Opt_check_integrity_including_extent_data:
778 case Opt_check_integrity: 770 case Opt_check_integrity:
779 case Opt_check_integrity_print_mask: 771 case Opt_check_integrity_print_mask:
780 btrfs_err(root->fs_info, 772 btrfs_err(info,
781 "support for check_integrity* not compiled in!"); 773 "support for check_integrity* not compiled in!");
782 ret = -EINVAL; 774 ret = -EINVAL;
783 goto out; 775 goto out;
784#endif 776#endif
@@ -798,20 +790,19 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
798 intarg = 0; 790 intarg = 0;
799 ret = match_int(&args[0], &intarg); 791 ret = match_int(&args[0], &intarg);
800 if (ret < 0) { 792 if (ret < 0) {
801 btrfs_err(root->fs_info, 793 btrfs_err(info, "invalid commit interval");
802 "invalid commit interval");
803 ret = -EINVAL; 794 ret = -EINVAL;
804 goto out; 795 goto out;
805 } 796 }
806 if (intarg > 0) { 797 if (intarg > 0) {
807 if (intarg > 300) { 798 if (intarg > 300) {
808 btrfs_warn(root->fs_info, 799 btrfs_warn(info,
809 "excessive commit interval %d", 800 "excessive commit interval %d",
810 intarg); 801 intarg);
811 } 802 }
812 info->commit_interval = intarg; 803 info->commit_interval = intarg;
813 } else { 804 } else {
814 btrfs_info(root->fs_info, 805 btrfs_info(info,
815 "using default commit interval %ds", 806 "using default commit interval %ds",
816 BTRFS_DEFAULT_COMMIT_INTERVAL); 807 BTRFS_DEFAULT_COMMIT_INTERVAL);
817 info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL; 808 info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
@@ -819,23 +810,22 @@ int btrfs_parse_options(struct btrfs_root *root, char *options,
819 break; 810 break;
820#ifdef CONFIG_BTRFS_DEBUG 811#ifdef CONFIG_BTRFS_DEBUG
821 case Opt_fragment_all: 812 case Opt_fragment_all:
822 btrfs_info(root->fs_info, "fragmenting all space"); 813 btrfs_info(info, "fragmenting all space");
823 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 814 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
824 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA); 815 btrfs_set_opt(info->mount_opt, FRAGMENT_METADATA);
825 break; 816 break;
826 case Opt_fragment_metadata: 817 case Opt_fragment_metadata:
827 btrfs_info(root->fs_info, "fragmenting metadata"); 818 btrfs_info(info, "fragmenting metadata");
828 btrfs_set_opt(info->mount_opt, 819 btrfs_set_opt(info->mount_opt,
829 FRAGMENT_METADATA); 820 FRAGMENT_METADATA);
830 break; 821 break;
831 case Opt_fragment_data: 822 case Opt_fragment_data:
832 btrfs_info(root->fs_info, "fragmenting data"); 823 btrfs_info(info, "fragmenting data");
833 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA); 824 btrfs_set_opt(info->mount_opt, FRAGMENT_DATA);
834 break; 825 break;
835#endif 826#endif
836 case Opt_err: 827 case Opt_err:
837 btrfs_info(root->fs_info, 828 btrfs_info(info, "unrecognized mount option '%s'", p);
838 "unrecognized mount option '%s'", p);
839 ret = -EINVAL; 829 ret = -EINVAL;
840 goto out; 830 goto out;
841 default: 831 default:
@@ -847,22 +837,22 @@ check:
847 * Extra check for current option against current flag 837 * Extra check for current option against current flag
848 */ 838 */
849 if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) { 839 if (btrfs_test_opt(info, NOLOGREPLAY) && !(new_flags & MS_RDONLY)) {
850 btrfs_err(root->fs_info, 840 btrfs_err(info,
851 "nologreplay must be used with ro mount option"); 841 "nologreplay must be used with ro mount option");
852 ret = -EINVAL; 842 ret = -EINVAL;
853 } 843 }
854out: 844out:
855 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE) && 845 if (btrfs_fs_compat_ro(info, FREE_SPACE_TREE) &&
856 !btrfs_test_opt(info, FREE_SPACE_TREE) && 846 !btrfs_test_opt(info, FREE_SPACE_TREE) &&
857 !btrfs_test_opt(info, CLEAR_CACHE)) { 847 !btrfs_test_opt(info, CLEAR_CACHE)) {
858 btrfs_err(root->fs_info, "cannot disable free space tree"); 848 btrfs_err(info, "cannot disable free space tree");
859 ret = -EINVAL; 849 ret = -EINVAL;
860 850
861 } 851 }
862 if (!ret && btrfs_test_opt(info, SPACE_CACHE)) 852 if (!ret && btrfs_test_opt(info, SPACE_CACHE))
863 btrfs_info(root->fs_info, "disk space caching is enabled"); 853 btrfs_info(info, "disk space caching is enabled");
864 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE)) 854 if (!ret && btrfs_test_opt(info, FREE_SPACE_TREE))
865 btrfs_info(root->fs_info, "using free space tree"); 855 btrfs_info(info, "using free space tree");
866 kfree(orig); 856 kfree(orig);
867 return ret; 857 return ret;
868} 858}
@@ -1223,7 +1213,6 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
1223static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry) 1213static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1224{ 1214{
1225 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb); 1215 struct btrfs_fs_info *info = btrfs_sb(dentry->d_sb);
1226 struct btrfs_root *root = info->tree_root;
1227 char *compress_type; 1216 char *compress_type;
1228 1217
1229 if (btrfs_test_opt(info, DEGRADED)) 1218 if (btrfs_test_opt(info, DEGRADED))
@@ -1265,7 +1254,7 @@ static int btrfs_show_options(struct seq_file *seq, struct dentry *dentry)
1265 seq_puts(seq, ",flushoncommit"); 1254 seq_puts(seq, ",flushoncommit");
1266 if (btrfs_test_opt(info, DISCARD)) 1255 if (btrfs_test_opt(info, DISCARD))
1267 seq_puts(seq, ",discard"); 1256 seq_puts(seq, ",discard");
1268 if (!(root->fs_info->sb->s_flags & MS_POSIXACL)) 1257 if (!(info->sb->s_flags & MS_POSIXACL))
1269 seq_puts(seq, ",noacl"); 1258 seq_puts(seq, ",noacl");
1270 if (btrfs_test_opt(info, SPACE_CACHE)) 1259 if (btrfs_test_opt(info, SPACE_CACHE))
1271 seq_puts(seq, ",space_cache"); 1260 seq_puts(seq, ",space_cache");
@@ -1788,7 +1777,7 @@ static int btrfs_remount(struct super_block *sb, int *flags, char *data)
1788 if (ret) 1777 if (ret)
1789 goto restore; 1778 goto restore;
1790 } else { 1779 } else {
1791 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 1780 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
1792 btrfs_err(fs_info, 1781 btrfs_err(fs_info,
1793 "Remounting read-write after error is not allowed"); 1782 "Remounting read-write after error is not allowed");
1794 ret = -EINVAL; 1783 ret = -EINVAL;
@@ -2246,9 +2235,10 @@ static long btrfs_control_ioctl(struct file *file, unsigned int cmd,
2246static int btrfs_freeze(struct super_block *sb) 2235static int btrfs_freeze(struct super_block *sb)
2247{ 2236{
2248 struct btrfs_trans_handle *trans; 2237 struct btrfs_trans_handle *trans;
2249 struct btrfs_root *root = btrfs_sb(sb)->tree_root; 2238 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
2239 struct btrfs_root *root = fs_info->tree_root;
2250 2240
2251 root->fs_info->fs_frozen = 1; 2241 fs_info->fs_frozen = 1;
2252 /* 2242 /*
2253 * We don't need a barrier here, we'll wait for any transaction that 2243 * We don't need a barrier here, we'll wait for any transaction that
2254 * could be in progress on other threads (and do delayed iputs that 2244 * could be in progress on other threads (and do delayed iputs that
@@ -2267,9 +2257,7 @@ static int btrfs_freeze(struct super_block *sb)
2267 2257
2268static int btrfs_unfreeze(struct super_block *sb) 2258static int btrfs_unfreeze(struct super_block *sb)
2269{ 2259{
2270 struct btrfs_root *root = btrfs_sb(sb)->tree_root; 2260 btrfs_sb(sb)->fs_frozen = 0;
2271
2272 root->fs_info->fs_frozen = 0;
2273 return 0; 2261 return 0;
2274} 2262}
2275 2263
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index bec5aa0e94e2..7fa8a6a9d07e 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -314,9 +314,11 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans,
314 struct btrfs_root *root, 314 struct btrfs_root *root,
315 int force) 315 int force)
316{ 316{
317 struct btrfs_fs_info *fs_info = root->fs_info;
318
317 if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) && 319 if ((test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
318 root->last_trans < trans->transid) || force) { 320 root->last_trans < trans->transid) || force) {
319 WARN_ON(root == root->fs_info->extent_root); 321 WARN_ON(root == fs_info->extent_root);
320 WARN_ON(root->commit_root != root->node); 322 WARN_ON(root->commit_root != root->node);
321 323
322 /* 324 /*
@@ -331,15 +333,15 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans,
331 */ 333 */
332 smp_wmb(); 334 smp_wmb();
333 335
334 spin_lock(&root->fs_info->fs_roots_radix_lock); 336 spin_lock(&fs_info->fs_roots_radix_lock);
335 if (root->last_trans == trans->transid && !force) { 337 if (root->last_trans == trans->transid && !force) {
336 spin_unlock(&root->fs_info->fs_roots_radix_lock); 338 spin_unlock(&fs_info->fs_roots_radix_lock);
337 return 0; 339 return 0;
338 } 340 }
339 radix_tree_tag_set(&root->fs_info->fs_roots_radix, 341 radix_tree_tag_set(&fs_info->fs_roots_radix,
340 (unsigned long)root->root_key.objectid, 342 (unsigned long)root->root_key.objectid,
341 BTRFS_ROOT_TRANS_TAG); 343 BTRFS_ROOT_TRANS_TAG);
342 spin_unlock(&root->fs_info->fs_roots_radix_lock); 344 spin_unlock(&fs_info->fs_roots_radix_lock);
343 root->last_trans = trans->transid; 345 root->last_trans = trans->transid;
344 346
345 /* this is pretty tricky. We don't want to 347 /* this is pretty tricky. We don't want to
@@ -372,6 +374,7 @@ static int record_root_in_trans(struct btrfs_trans_handle *trans,
372void btrfs_add_dropped_root(struct btrfs_trans_handle *trans, 374void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
373 struct btrfs_root *root) 375 struct btrfs_root *root)
374{ 376{
377 struct btrfs_fs_info *fs_info = root->fs_info;
375 struct btrfs_transaction *cur_trans = trans->transaction; 378 struct btrfs_transaction *cur_trans = trans->transaction;
376 379
377 /* Add ourselves to the transaction dropped list */ 380 /* Add ourselves to the transaction dropped list */
@@ -380,16 +383,18 @@ void btrfs_add_dropped_root(struct btrfs_trans_handle *trans,
380 spin_unlock(&cur_trans->dropped_roots_lock); 383 spin_unlock(&cur_trans->dropped_roots_lock);
381 384
382 /* Make sure we don't try to update the root at commit time */ 385 /* Make sure we don't try to update the root at commit time */
383 spin_lock(&root->fs_info->fs_roots_radix_lock); 386 spin_lock(&fs_info->fs_roots_radix_lock);
384 radix_tree_tag_clear(&root->fs_info->fs_roots_radix, 387 radix_tree_tag_clear(&fs_info->fs_roots_radix,
385 (unsigned long)root->root_key.objectid, 388 (unsigned long)root->root_key.objectid,
386 BTRFS_ROOT_TRANS_TAG); 389 BTRFS_ROOT_TRANS_TAG);
387 spin_unlock(&root->fs_info->fs_roots_radix_lock); 390 spin_unlock(&fs_info->fs_roots_radix_lock);
388} 391}
389 392
390int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans, 393int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
391 struct btrfs_root *root) 394 struct btrfs_root *root)
392{ 395{
396 struct btrfs_fs_info *fs_info = root->fs_info;
397
393 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) 398 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state))
394 return 0; 399 return 0;
395 400
@@ -402,9 +407,9 @@ int btrfs_record_root_in_trans(struct btrfs_trans_handle *trans,
402 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state)) 407 !test_bit(BTRFS_ROOT_IN_TRANS_SETUP, &root->state))
403 return 0; 408 return 0;
404 409
405 mutex_lock(&root->fs_info->reloc_mutex); 410 mutex_lock(&fs_info->reloc_mutex);
406 record_root_in_trans(trans, root, 0); 411 record_root_in_trans(trans, root, 0);
407 mutex_unlock(&root->fs_info->reloc_mutex); 412 mutex_unlock(&fs_info->reloc_mutex);
408 413
409 return 0; 414 return 0;
410} 415}
@@ -422,33 +427,36 @@ static inline int is_transaction_blocked(struct btrfs_transaction *trans)
422 */ 427 */
423static void wait_current_trans(struct btrfs_root *root) 428static void wait_current_trans(struct btrfs_root *root)
424{ 429{
430 struct btrfs_fs_info *fs_info = root->fs_info;
425 struct btrfs_transaction *cur_trans; 431 struct btrfs_transaction *cur_trans;
426 432
427 spin_lock(&root->fs_info->trans_lock); 433 spin_lock(&fs_info->trans_lock);
428 cur_trans = root->fs_info->running_transaction; 434 cur_trans = fs_info->running_transaction;
429 if (cur_trans && is_transaction_blocked(cur_trans)) { 435 if (cur_trans && is_transaction_blocked(cur_trans)) {
430 atomic_inc(&cur_trans->use_count); 436 atomic_inc(&cur_trans->use_count);
431 spin_unlock(&root->fs_info->trans_lock); 437 spin_unlock(&fs_info->trans_lock);
432 438
433 wait_event(root->fs_info->transaction_wait, 439 wait_event(fs_info->transaction_wait,
434 cur_trans->state >= TRANS_STATE_UNBLOCKED || 440 cur_trans->state >= TRANS_STATE_UNBLOCKED ||
435 cur_trans->aborted); 441 cur_trans->aborted);
436 btrfs_put_transaction(cur_trans); 442 btrfs_put_transaction(cur_trans);
437 } else { 443 } else {
438 spin_unlock(&root->fs_info->trans_lock); 444 spin_unlock(&fs_info->trans_lock);
439 } 445 }
440} 446}
441 447
442static int may_wait_transaction(struct btrfs_root *root, int type) 448static int may_wait_transaction(struct btrfs_root *root, int type)
443{ 449{
444 if (test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) 450 struct btrfs_fs_info *fs_info = root->fs_info;
451
452 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
445 return 0; 453 return 0;
446 454
447 if (type == TRANS_USERSPACE) 455 if (type == TRANS_USERSPACE)
448 return 1; 456 return 1;
449 457
450 if (type == TRANS_START && 458 if (type == TRANS_START &&
451 !atomic_read(&root->fs_info->open_ioctl_trans)) 459 !atomic_read(&fs_info->open_ioctl_trans))
452 return 1; 460 return 1;
453 461
454 return 0; 462 return 0;
@@ -456,7 +464,9 @@ static int may_wait_transaction(struct btrfs_root *root, int type)
456 464
457static inline bool need_reserve_reloc_root(struct btrfs_root *root) 465static inline bool need_reserve_reloc_root(struct btrfs_root *root)
458{ 466{
459 if (!root->fs_info->reloc_ctl || 467 struct btrfs_fs_info *fs_info = root->fs_info;
468
469 if (!fs_info->reloc_ctl ||
460 !test_bit(BTRFS_ROOT_REF_COWS, &root->state) || 470 !test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
461 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || 471 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
462 root->reloc_root) 472 root->reloc_root)
@@ -469,6 +479,8 @@ static struct btrfs_trans_handle *
469start_transaction(struct btrfs_root *root, unsigned int num_items, 479start_transaction(struct btrfs_root *root, unsigned int num_items,
470 unsigned int type, enum btrfs_reserve_flush_enum flush) 480 unsigned int type, enum btrfs_reserve_flush_enum flush)
471{ 481{
482 struct btrfs_fs_info *fs_info = root->fs_info;
483
472 struct btrfs_trans_handle *h; 484 struct btrfs_trans_handle *h;
473 struct btrfs_transaction *cur_trans; 485 struct btrfs_transaction *cur_trans;
474 u64 num_bytes = 0; 486 u64 num_bytes = 0;
@@ -479,7 +491,7 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
479 /* Send isn't supposed to start transactions. */ 491 /* Send isn't supposed to start transactions. */
480 ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB); 492 ASSERT(current->journal_info != BTRFS_SEND_TRANS_STUB);
481 493
482 if (test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) 494 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
483 return ERR_PTR(-EROFS); 495 return ERR_PTR(-EROFS);
484 496
485 if (current->journal_info) { 497 if (current->journal_info) {
@@ -496,24 +508,22 @@ start_transaction(struct btrfs_root *root, unsigned int num_items,
496 * Do the reservation before we join the transaction so we can do all 508 * Do the reservation before we join the transaction so we can do all
497 * the appropriate flushing if need be. 509 * the appropriate flushing if need be.
498 */ 510 */
499 if (num_items > 0 && root != root->fs_info->chunk_root) { 511 if (num_items > 0 && root != fs_info->chunk_root) {
500 qgroup_reserved = num_items * root->fs_info->nodesize; 512 qgroup_reserved = num_items * fs_info->nodesize;
501 ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved); 513 ret = btrfs_qgroup_reserve_meta(root, qgroup_reserved);
502 if (ret) 514 if (ret)
503 return ERR_PTR(ret); 515 return ERR_PTR(ret);
504 516
505 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, 517 num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
506 num_items);
507 /* 518 /*
508 * Do the reservation for the relocation root creation 519 * Do the reservation for the relocation root creation
509 */ 520 */
510 if (need_reserve_reloc_root(root)) { 521 if (need_reserve_reloc_root(root)) {
511 num_bytes += root->fs_info->nodesize; 522 num_bytes += fs_info->nodesize;
512 reloc_reserved = true; 523 reloc_reserved = true;
513 } 524 }
514 525
515 ret = btrfs_block_rsv_add(root, 526 ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
516 &root->fs_info->trans_block_rsv,
517 num_bytes, flush); 527 num_bytes, flush);
518 if (ret) 528 if (ret)
519 goto reserve_fail; 529 goto reserve_fail;
@@ -536,7 +546,7 @@ again:
536 * transaction and commit it, so we needn't do sb_start_intwrite(). 546 * transaction and commit it, so we needn't do sb_start_intwrite().
537 */ 547 */
538 if (type & __TRANS_FREEZABLE) 548 if (type & __TRANS_FREEZABLE)
539 sb_start_intwrite(root->fs_info->sb); 549 sb_start_intwrite(fs_info->sb);
540 550
541 if (may_wait_transaction(root, type)) 551 if (may_wait_transaction(root, type))
542 wait_current_trans(root); 552 wait_current_trans(root);
@@ -553,7 +563,7 @@ again:
553 if (ret < 0) 563 if (ret < 0)
554 goto join_fail; 564 goto join_fail;
555 565
556 cur_trans = root->fs_info->running_transaction; 566 cur_trans = fs_info->running_transaction;
557 567
558 h->transid = cur_trans->transid; 568 h->transid = cur_trans->transid;
559 h->transaction = cur_trans; 569 h->transaction = cur_trans;
@@ -575,9 +585,9 @@ again:
575 } 585 }
576 586
577 if (num_bytes) { 587 if (num_bytes) {
578 trace_btrfs_space_reservation(root->fs_info, "transaction", 588 trace_btrfs_space_reservation(fs_info, "transaction",
579 h->transid, num_bytes, 1); 589 h->transid, num_bytes, 1);
580 h->block_rsv = &root->fs_info->trans_block_rsv; 590 h->block_rsv = &fs_info->trans_block_rsv;
581 h->bytes_reserved = num_bytes; 591 h->bytes_reserved = num_bytes;
582 h->reloc_reserved = reloc_reserved; 592 h->reloc_reserved = reloc_reserved;
583 } 593 }
@@ -591,11 +601,11 @@ got_it:
591 601
592join_fail: 602join_fail:
593 if (type & __TRANS_FREEZABLE) 603 if (type & __TRANS_FREEZABLE)
594 sb_end_intwrite(root->fs_info->sb); 604 sb_end_intwrite(fs_info->sb);
595 kmem_cache_free(btrfs_trans_handle_cachep, h); 605 kmem_cache_free(btrfs_trans_handle_cachep, h);
596alloc_fail: 606alloc_fail:
597 if (num_bytes) 607 if (num_bytes)
598 btrfs_block_rsv_release(root, &root->fs_info->trans_block_rsv, 608 btrfs_block_rsv_release(root, &fs_info->trans_block_rsv,
599 num_bytes); 609 num_bytes);
600reserve_fail: 610reserve_fail:
601 btrfs_qgroup_free_meta(root, qgroup_reserved); 611 btrfs_qgroup_free_meta(root, qgroup_reserved);
@@ -613,6 +623,7 @@ struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
613 unsigned int num_items, 623 unsigned int num_items,
614 int min_factor) 624 int min_factor)
615{ 625{
626 struct btrfs_fs_info *fs_info = root->fs_info;
616 struct btrfs_trans_handle *trans; 627 struct btrfs_trans_handle *trans;
617 u64 num_bytes; 628 u64 num_bytes;
618 int ret; 629 int ret;
@@ -625,19 +636,17 @@ struct btrfs_trans_handle *btrfs_start_transaction_fallback_global_rsv(
625 if (IS_ERR(trans)) 636 if (IS_ERR(trans))
626 return trans; 637 return trans;
627 638
628 num_bytes = btrfs_calc_trans_metadata_size(root->fs_info, num_items); 639 num_bytes = btrfs_calc_trans_metadata_size(fs_info, num_items);
629 ret = btrfs_cond_migrate_bytes(root->fs_info, 640 ret = btrfs_cond_migrate_bytes(fs_info, &fs_info->trans_block_rsv,
630 &root->fs_info->trans_block_rsv, 641 num_bytes, min_factor);
631 num_bytes,
632 min_factor);
633 if (ret) { 642 if (ret) {
634 btrfs_end_transaction(trans, root); 643 btrfs_end_transaction(trans, root);
635 return ERR_PTR(ret); 644 return ERR_PTR(ret);
636 } 645 }
637 646
638 trans->block_rsv = &root->fs_info->trans_block_rsv; 647 trans->block_rsv = &fs_info->trans_block_rsv;
639 trans->bytes_reserved = num_bytes; 648 trans->bytes_reserved = num_bytes;
640 trace_btrfs_space_reservation(root->fs_info, "transaction", 649 trace_btrfs_space_reservation(fs_info, "transaction",
641 trans->transid, num_bytes, 1); 650 trans->transid, num_bytes, 1);
642 651
643 return trans; 652 return trans;
@@ -717,16 +726,17 @@ static noinline void wait_for_commit(struct btrfs_root *root,
717 726
718int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid) 727int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
719{ 728{
729 struct btrfs_fs_info *fs_info = root->fs_info;
720 struct btrfs_transaction *cur_trans = NULL, *t; 730 struct btrfs_transaction *cur_trans = NULL, *t;
721 int ret = 0; 731 int ret = 0;
722 732
723 if (transid) { 733 if (transid) {
724 if (transid <= root->fs_info->last_trans_committed) 734 if (transid <= fs_info->last_trans_committed)
725 goto out; 735 goto out;
726 736
727 /* find specified transaction */ 737 /* find specified transaction */
728 spin_lock(&root->fs_info->trans_lock); 738 spin_lock(&fs_info->trans_lock);
729 list_for_each_entry(t, &root->fs_info->trans_list, list) { 739 list_for_each_entry(t, &fs_info->trans_list, list) {
730 if (t->transid == transid) { 740 if (t->transid == transid) {
731 cur_trans = t; 741 cur_trans = t;
732 atomic_inc(&cur_trans->use_count); 742 atomic_inc(&cur_trans->use_count);
@@ -738,21 +748,21 @@ int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
738 break; 748 break;
739 } 749 }
740 } 750 }
741 spin_unlock(&root->fs_info->trans_lock); 751 spin_unlock(&fs_info->trans_lock);
742 752
743 /* 753 /*
744 * The specified transaction doesn't exist, or we 754 * The specified transaction doesn't exist, or we
745 * raced with btrfs_commit_transaction 755 * raced with btrfs_commit_transaction
746 */ 756 */
747 if (!cur_trans) { 757 if (!cur_trans) {
748 if (transid > root->fs_info->last_trans_committed) 758 if (transid > fs_info->last_trans_committed)
749 ret = -EINVAL; 759 ret = -EINVAL;
750 goto out; 760 goto out;
751 } 761 }
752 } else { 762 } else {
753 /* find newest transaction that is committing | committed */ 763 /* find newest transaction that is committing | committed */
754 spin_lock(&root->fs_info->trans_lock); 764 spin_lock(&fs_info->trans_lock);
755 list_for_each_entry_reverse(t, &root->fs_info->trans_list, 765 list_for_each_entry_reverse(t, &fs_info->trans_list,
756 list) { 766 list) {
757 if (t->state >= TRANS_STATE_COMMIT_START) { 767 if (t->state >= TRANS_STATE_COMMIT_START) {
758 if (t->state == TRANS_STATE_COMPLETED) 768 if (t->state == TRANS_STATE_COMPLETED)
@@ -762,7 +772,7 @@ int btrfs_wait_for_commit(struct btrfs_root *root, u64 transid)
762 break; 772 break;
763 } 773 }
764 } 774 }
765 spin_unlock(&root->fs_info->trans_lock); 775 spin_unlock(&fs_info->trans_lock);
766 if (!cur_trans) 776 if (!cur_trans)
767 goto out; /* nothing committing|committed */ 777 goto out; /* nothing committing|committed */
768 } 778 }
@@ -775,18 +785,22 @@ out:
775 785
776void btrfs_throttle(struct btrfs_root *root) 786void btrfs_throttle(struct btrfs_root *root)
777{ 787{
778 if (!atomic_read(&root->fs_info->open_ioctl_trans)) 788 struct btrfs_fs_info *fs_info = root->fs_info;
789
790 if (!atomic_read(&fs_info->open_ioctl_trans))
779 wait_current_trans(root); 791 wait_current_trans(root);
780} 792}
781 793
782static int should_end_transaction(struct btrfs_trans_handle *trans, 794static int should_end_transaction(struct btrfs_trans_handle *trans,
783 struct btrfs_root *root) 795 struct btrfs_root *root)
784{ 796{
785 if (root->fs_info->global_block_rsv.space_info->full && 797 struct btrfs_fs_info *fs_info = root->fs_info;
798
799 if (fs_info->global_block_rsv.space_info->full &&
786 btrfs_check_space_for_delayed_refs(trans, root)) 800 btrfs_check_space_for_delayed_refs(trans, root))
787 return 1; 801 return 1;
788 802
789 return !!btrfs_block_rsv_check(root, &root->fs_info->global_block_rsv, 5); 803 return !!btrfs_block_rsv_check(root, &fs_info->global_block_rsv, 5);
790} 804}
791 805
792int btrfs_should_end_transaction(struct btrfs_trans_handle *trans, 806int btrfs_should_end_transaction(struct btrfs_trans_handle *trans,
@@ -858,7 +872,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
858 872
859 btrfs_trans_release_chunk_metadata(trans); 873 btrfs_trans_release_chunk_metadata(trans);
860 874
861 if (lock && !atomic_read(&root->fs_info->open_ioctl_trans) && 875 if (lock && !atomic_read(&info->open_ioctl_trans) &&
862 should_end_transaction(trans, root) && 876 should_end_transaction(trans, root) &&
863 ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) { 877 ACCESS_ONCE(cur_trans->state) == TRANS_STATE_RUNNING) {
864 spin_lock(&info->trans_lock); 878 spin_lock(&info->trans_lock);
@@ -875,7 +889,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
875 } 889 }
876 890
877 if (trans->type & __TRANS_FREEZABLE) 891 if (trans->type & __TRANS_FREEZABLE)
878 sb_end_intwrite(root->fs_info->sb); 892 sb_end_intwrite(info->sb);
879 893
880 WARN_ON(cur_trans != info->running_transaction); 894 WARN_ON(cur_trans != info->running_transaction);
881 WARN_ON(atomic_read(&cur_trans->num_writers) < 1); 895 WARN_ON(atomic_read(&cur_trans->num_writers) < 1);
@@ -897,7 +911,7 @@ static int __btrfs_end_transaction(struct btrfs_trans_handle *trans,
897 btrfs_run_delayed_iputs(root); 911 btrfs_run_delayed_iputs(root);
898 912
899 if (trans->aborted || 913 if (trans->aborted ||
900 test_bit(BTRFS_FS_STATE_ERROR, &root->fs_info->fs_state)) { 914 test_bit(BTRFS_FS_STATE_ERROR, &info->fs_state)) {
901 wake_up_process(info->transaction_kthread); 915 wake_up_process(info->transaction_kthread);
902 err = -EIO; 916 err = -EIO;
903 } 917 }
@@ -933,7 +947,8 @@ int btrfs_write_marked_extents(struct btrfs_root *root,
933{ 947{
934 int err = 0; 948 int err = 0;
935 int werr = 0; 949 int werr = 0;
936 struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 950 struct btrfs_fs_info *fs_info = root->fs_info;
951 struct address_space *mapping = fs_info->btree_inode->i_mapping;
937 struct extent_state *cached_state = NULL; 952 struct extent_state *cached_state = NULL;
938 u64 start = 0; 953 u64 start = 0;
939 u64 end; 954 u64 end;
@@ -987,7 +1002,8 @@ int btrfs_wait_marked_extents(struct btrfs_root *root,
987{ 1002{
988 int err = 0; 1003 int err = 0;
989 int werr = 0; 1004 int werr = 0;
990 struct address_space *mapping = root->fs_info->btree_inode->i_mapping; 1005 struct btrfs_fs_info *fs_info = root->fs_info;
1006 struct address_space *mapping = fs_info->btree_inode->i_mapping;
991 struct extent_state *cached_state = NULL; 1007 struct extent_state *cached_state = NULL;
992 u64 start = 0; 1008 u64 start = 0;
993 u64 end; 1009 u64 end;
@@ -1022,17 +1038,14 @@ int btrfs_wait_marked_extents(struct btrfs_root *root,
1022 1038
1023 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { 1039 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1024 if ((mark & EXTENT_DIRTY) && 1040 if ((mark & EXTENT_DIRTY) &&
1025 test_and_clear_bit(BTRFS_FS_LOG1_ERR, 1041 test_and_clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags))
1026 &root->fs_info->flags))
1027 errors = true; 1042 errors = true;
1028 1043
1029 if ((mark & EXTENT_NEW) && 1044 if ((mark & EXTENT_NEW) &&
1030 test_and_clear_bit(BTRFS_FS_LOG2_ERR, 1045 test_and_clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags))
1031 &root->fs_info->flags))
1032 errors = true; 1046 errors = true;
1033 } else { 1047 } else {
1034 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, 1048 if (test_and_clear_bit(BTRFS_FS_BTREE_ERR, &fs_info->flags))
1035 &root->fs_info->flags))
1036 errors = true; 1049 errors = true;
1037 } 1050 }
1038 1051
@@ -1095,7 +1108,8 @@ static int update_cowonly_root(struct btrfs_trans_handle *trans,
1095 int ret; 1108 int ret;
1096 u64 old_root_bytenr; 1109 u64 old_root_bytenr;
1097 u64 old_root_used; 1110 u64 old_root_used;
1098 struct btrfs_root *tree_root = root->fs_info->tree_root; 1111 struct btrfs_fs_info *fs_info = root->fs_info;
1112 struct btrfs_root *tree_root = fs_info->tree_root;
1099 1113
1100 old_root_used = btrfs_root_used(&root->root_item); 1114 old_root_used = btrfs_root_used(&root->root_item);
1101 1115
@@ -1148,13 +1162,13 @@ static noinline int commit_cowonly_roots(struct btrfs_trans_handle *trans,
1148 if (ret) 1162 if (ret)
1149 return ret; 1163 return ret;
1150 1164
1151 ret = btrfs_run_dev_stats(trans, root->fs_info); 1165 ret = btrfs_run_dev_stats(trans, fs_info);
1152 if (ret) 1166 if (ret)
1153 return ret; 1167 return ret;
1154 ret = btrfs_run_dev_replace(trans, root->fs_info); 1168 ret = btrfs_run_dev_replace(trans, fs_info);
1155 if (ret) 1169 if (ret)
1156 return ret; 1170 return ret;
1157 ret = btrfs_run_qgroups(trans, root->fs_info); 1171 ret = btrfs_run_qgroups(trans, fs_info);
1158 if (ret) 1172 if (ret)
1159 return ret; 1173 return ret;
1160 1174
@@ -1210,10 +1224,12 @@ again:
1210 */ 1224 */
1211void btrfs_add_dead_root(struct btrfs_root *root) 1225void btrfs_add_dead_root(struct btrfs_root *root)
1212{ 1226{
1213 spin_lock(&root->fs_info->trans_lock); 1227 struct btrfs_fs_info *fs_info = root->fs_info;
1228
1229 spin_lock(&fs_info->trans_lock);
1214 if (list_empty(&root->root_list)) 1230 if (list_empty(&root->root_list))
1215 list_add_tail(&root->root_list, &root->fs_info->dead_roots); 1231 list_add_tail(&root->root_list, &fs_info->dead_roots);
1216 spin_unlock(&root->fs_info->trans_lock); 1232 spin_unlock(&fs_info->trans_lock);
1217} 1233}
1218 1234
1219/* 1235/*
@@ -1462,7 +1478,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1462 rsv = trans->block_rsv; 1478 rsv = trans->block_rsv;
1463 trans->block_rsv = &pending->block_rsv; 1479 trans->block_rsv = &pending->block_rsv;
1464 trans->bytes_reserved = trans->block_rsv->reserved; 1480 trans->bytes_reserved = trans->block_rsv->reserved;
1465 trace_btrfs_space_reservation(root->fs_info, "transaction", 1481 trace_btrfs_space_reservation(fs_info, "transaction",
1466 trans->transid, 1482 trans->transid,
1467 trans->bytes_reserved, 1); 1483 trans->bytes_reserved, 1);
1468 dentry = pending->dentry; 1484 dentry = pending->dentry;
@@ -1582,7 +1598,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans,
1582 } 1598 }
1583 1599
1584 key.offset = (u64)-1; 1600 key.offset = (u64)-1;
1585 pending->snap = btrfs_read_fs_root_no_name(root->fs_info, &key); 1601 pending->snap = btrfs_read_fs_root_no_name(fs_info, &key);
1586 if (IS_ERR(pending->snap)) { 1602 if (IS_ERR(pending->snap)) {
1587 ret = PTR_ERR(pending->snap); 1603 ret = PTR_ERR(pending->snap);
1588 btrfs_abort_transaction(trans, ret); 1604 btrfs_abort_transaction(trans, ret);
@@ -1692,23 +1708,24 @@ static noinline int create_pending_snapshots(struct btrfs_trans_handle *trans,
1692 1708
1693static void update_super_roots(struct btrfs_root *root) 1709static void update_super_roots(struct btrfs_root *root)
1694{ 1710{
1711 struct btrfs_fs_info *fs_info = root->fs_info;
1695 struct btrfs_root_item *root_item; 1712 struct btrfs_root_item *root_item;
1696 struct btrfs_super_block *super; 1713 struct btrfs_super_block *super;
1697 1714
1698 super = root->fs_info->super_copy; 1715 super = fs_info->super_copy;
1699 1716
1700 root_item = &root->fs_info->chunk_root->root_item; 1717 root_item = &fs_info->chunk_root->root_item;
1701 super->chunk_root = root_item->bytenr; 1718 super->chunk_root = root_item->bytenr;
1702 super->chunk_root_generation = root_item->generation; 1719 super->chunk_root_generation = root_item->generation;
1703 super->chunk_root_level = root_item->level; 1720 super->chunk_root_level = root_item->level;
1704 1721
1705 root_item = &root->fs_info->tree_root->root_item; 1722 root_item = &fs_info->tree_root->root_item;
1706 super->root = root_item->bytenr; 1723 super->root = root_item->bytenr;
1707 super->generation = root_item->generation; 1724 super->generation = root_item->generation;
1708 super->root_level = root_item->level; 1725 super->root_level = root_item->level;
1709 if (btrfs_test_opt(root->fs_info, SPACE_CACHE)) 1726 if (btrfs_test_opt(fs_info, SPACE_CACHE))
1710 super->cache_generation = root_item->generation; 1727 super->cache_generation = root_item->generation;
1711 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &root->fs_info->flags)) 1728 if (test_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags))
1712 super->uuid_tree_generation = root_item->generation; 1729 super->uuid_tree_generation = root_item->generation;
1713} 1730}
1714 1731
@@ -1794,6 +1811,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1794 struct btrfs_root *root, 1811 struct btrfs_root *root,
1795 int wait_for_unblock) 1812 int wait_for_unblock)
1796{ 1813{
1814 struct btrfs_fs_info *fs_info = root->fs_info;
1797 struct btrfs_async_commit *ac; 1815 struct btrfs_async_commit *ac;
1798 struct btrfs_transaction *cur_trans; 1816 struct btrfs_transaction *cur_trans;
1799 1817
@@ -1821,7 +1839,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1821 * async commit thread will be the one to unlock it. 1839 * async commit thread will be the one to unlock it.
1822 */ 1840 */
1823 if (ac->newtrans->type & __TRANS_FREEZABLE) 1841 if (ac->newtrans->type & __TRANS_FREEZABLE)
1824 __sb_writers_release(root->fs_info->sb, SB_FREEZE_FS); 1842 __sb_writers_release(fs_info->sb, SB_FREEZE_FS);
1825 1843
1826 schedule_work(&ac->work); 1844 schedule_work(&ac->work);
1827 1845
@@ -1842,6 +1860,7 @@ int btrfs_commit_transaction_async(struct btrfs_trans_handle *trans,
1842static void cleanup_transaction(struct btrfs_trans_handle *trans, 1860static void cleanup_transaction(struct btrfs_trans_handle *trans,
1843 struct btrfs_root *root, int err) 1861 struct btrfs_root *root, int err)
1844{ 1862{
1863 struct btrfs_fs_info *fs_info = root->fs_info;
1845 struct btrfs_transaction *cur_trans = trans->transaction; 1864 struct btrfs_transaction *cur_trans = trans->transaction;
1846 DEFINE_WAIT(wait); 1865 DEFINE_WAIT(wait);
1847 1866
@@ -1849,7 +1868,7 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,
1849 1868
1850 btrfs_abort_transaction(trans, err); 1869 btrfs_abort_transaction(trans, err);
1851 1870
1852 spin_lock(&root->fs_info->trans_lock); 1871 spin_lock(&fs_info->trans_lock);
1853 1872
1854 /* 1873 /*
1855 * If the transaction is removed from the list, it means this 1874 * If the transaction is removed from the list, it means this
@@ -1859,25 +1878,25 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,
1859 BUG_ON(list_empty(&cur_trans->list)); 1878 BUG_ON(list_empty(&cur_trans->list));
1860 1879
1861 list_del_init(&cur_trans->list); 1880 list_del_init(&cur_trans->list);
1862 if (cur_trans == root->fs_info->running_transaction) { 1881 if (cur_trans == fs_info->running_transaction) {
1863 cur_trans->state = TRANS_STATE_COMMIT_DOING; 1882 cur_trans->state = TRANS_STATE_COMMIT_DOING;
1864 spin_unlock(&root->fs_info->trans_lock); 1883 spin_unlock(&fs_info->trans_lock);
1865 wait_event(cur_trans->writer_wait, 1884 wait_event(cur_trans->writer_wait,
1866 atomic_read(&cur_trans->num_writers) == 1); 1885 atomic_read(&cur_trans->num_writers) == 1);
1867 1886
1868 spin_lock(&root->fs_info->trans_lock); 1887 spin_lock(&fs_info->trans_lock);
1869 } 1888 }
1870 spin_unlock(&root->fs_info->trans_lock); 1889 spin_unlock(&fs_info->trans_lock);
1871 1890
1872 btrfs_cleanup_one_transaction(trans->transaction, root); 1891 btrfs_cleanup_one_transaction(trans->transaction, root);
1873 1892
1874 spin_lock(&root->fs_info->trans_lock); 1893 spin_lock(&fs_info->trans_lock);
1875 if (cur_trans == root->fs_info->running_transaction) 1894 if (cur_trans == fs_info->running_transaction)
1876 root->fs_info->running_transaction = NULL; 1895 fs_info->running_transaction = NULL;
1877 spin_unlock(&root->fs_info->trans_lock); 1896 spin_unlock(&fs_info->trans_lock);
1878 1897
1879 if (trans->type & __TRANS_FREEZABLE) 1898 if (trans->type & __TRANS_FREEZABLE)
1880 sb_end_intwrite(root->fs_info->sb); 1899 sb_end_intwrite(fs_info->sb);
1881 btrfs_put_transaction(cur_trans); 1900 btrfs_put_transaction(cur_trans);
1882 btrfs_put_transaction(cur_trans); 1901 btrfs_put_transaction(cur_trans);
1883 1902
@@ -1885,7 +1904,7 @@ static void cleanup_transaction(struct btrfs_trans_handle *trans,
1885 1904
1886 if (current->journal_info == trans) 1905 if (current->journal_info == trans)
1887 current->journal_info = NULL; 1906 current->journal_info = NULL;
1888 btrfs_scrub_cancel(root->fs_info); 1907 btrfs_scrub_cancel(fs_info);
1889 1908
1890 kmem_cache_free(btrfs_trans_handle_cachep, trans); 1909 kmem_cache_free(btrfs_trans_handle_cachep, trans);
1891} 1910}
@@ -1913,6 +1932,7 @@ btrfs_wait_pending_ordered(struct btrfs_transaction *cur_trans)
1913int btrfs_commit_transaction(struct btrfs_trans_handle *trans, 1932int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1914 struct btrfs_root *root) 1933 struct btrfs_root *root)
1915{ 1934{
1935 struct btrfs_fs_info *fs_info = root->fs_info;
1916 struct btrfs_transaction *cur_trans = trans->transaction; 1936 struct btrfs_transaction *cur_trans = trans->transaction;
1917 struct btrfs_transaction *prev_trans = NULL; 1937 struct btrfs_transaction *prev_trans = NULL;
1918 int ret; 1938 int ret;
@@ -1970,11 +1990,11 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1970 * hurt to have more than one go through, but there's no 1990 * hurt to have more than one go through, but there's no
1971 * real advantage to it either. 1991 * real advantage to it either.
1972 */ 1992 */
1973 mutex_lock(&root->fs_info->ro_block_group_mutex); 1993 mutex_lock(&fs_info->ro_block_group_mutex);
1974 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN, 1994 if (!test_and_set_bit(BTRFS_TRANS_DIRTY_BG_RUN,
1975 &cur_trans->flags)) 1995 &cur_trans->flags))
1976 run_it = 1; 1996 run_it = 1;
1977 mutex_unlock(&root->fs_info->ro_block_group_mutex); 1997 mutex_unlock(&fs_info->ro_block_group_mutex);
1978 1998
1979 if (run_it) 1999 if (run_it)
1980 ret = btrfs_start_dirty_block_groups(trans, root); 2000 ret = btrfs_start_dirty_block_groups(trans, root);
@@ -1984,9 +2004,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
1984 return ret; 2004 return ret;
1985 } 2005 }
1986 2006
1987 spin_lock(&root->fs_info->trans_lock); 2007 spin_lock(&fs_info->trans_lock);
1988 if (cur_trans->state >= TRANS_STATE_COMMIT_START) { 2008 if (cur_trans->state >= TRANS_STATE_COMMIT_START) {
1989 spin_unlock(&root->fs_info->trans_lock); 2009 spin_unlock(&fs_info->trans_lock);
1990 atomic_inc(&cur_trans->use_count); 2010 atomic_inc(&cur_trans->use_count);
1991 ret = btrfs_end_transaction(trans, root); 2011 ret = btrfs_end_transaction(trans, root);
1992 2012
@@ -2001,14 +2021,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2001 } 2021 }
2002 2022
2003 cur_trans->state = TRANS_STATE_COMMIT_START; 2023 cur_trans->state = TRANS_STATE_COMMIT_START;
2004 wake_up(&root->fs_info->transaction_blocked_wait); 2024 wake_up(&fs_info->transaction_blocked_wait);
2005 2025
2006 if (cur_trans->list.prev != &root->fs_info->trans_list) { 2026 if (cur_trans->list.prev != &fs_info->trans_list) {
2007 prev_trans = list_entry(cur_trans->list.prev, 2027 prev_trans = list_entry(cur_trans->list.prev,
2008 struct btrfs_transaction, list); 2028 struct btrfs_transaction, list);
2009 if (prev_trans->state != TRANS_STATE_COMPLETED) { 2029 if (prev_trans->state != TRANS_STATE_COMPLETED) {
2010 atomic_inc(&prev_trans->use_count); 2030 atomic_inc(&prev_trans->use_count);
2011 spin_unlock(&root->fs_info->trans_lock); 2031 spin_unlock(&fs_info->trans_lock);
2012 2032
2013 wait_for_commit(root, prev_trans); 2033 wait_for_commit(root, prev_trans);
2014 ret = prev_trans->aborted; 2034 ret = prev_trans->aborted;
@@ -2017,15 +2037,15 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2017 if (ret) 2037 if (ret)
2018 goto cleanup_transaction; 2038 goto cleanup_transaction;
2019 } else { 2039 } else {
2020 spin_unlock(&root->fs_info->trans_lock); 2040 spin_unlock(&fs_info->trans_lock);
2021 } 2041 }
2022 } else { 2042 } else {
2023 spin_unlock(&root->fs_info->trans_lock); 2043 spin_unlock(&fs_info->trans_lock);
2024 } 2044 }
2025 2045
2026 extwriter_counter_dec(cur_trans, trans->type); 2046 extwriter_counter_dec(cur_trans, trans->type);
2027 2047
2028 ret = btrfs_start_delalloc_flush(root->fs_info); 2048 ret = btrfs_start_delalloc_flush(fs_info);
2029 if (ret) 2049 if (ret)
2030 goto cleanup_transaction; 2050 goto cleanup_transaction;
2031 2051
@@ -2041,7 +2061,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2041 if (ret) 2061 if (ret)
2042 goto cleanup_transaction; 2062 goto cleanup_transaction;
2043 2063
2044 btrfs_wait_delalloc_flush(root->fs_info); 2064 btrfs_wait_delalloc_flush(fs_info);
2045 2065
2046 btrfs_wait_pending_ordered(cur_trans); 2066 btrfs_wait_pending_ordered(cur_trans);
2047 2067
@@ -2051,9 +2071,9 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2051 * commit the transaction. We could have started a join before setting 2071 * commit the transaction. We could have started a join before setting
2052 * COMMIT_DOING so make sure to wait for num_writers to == 1 again. 2072 * COMMIT_DOING so make sure to wait for num_writers to == 1 again.
2053 */ 2073 */
2054 spin_lock(&root->fs_info->trans_lock); 2074 spin_lock(&fs_info->trans_lock);
2055 cur_trans->state = TRANS_STATE_COMMIT_DOING; 2075 cur_trans->state = TRANS_STATE_COMMIT_DOING;
2056 spin_unlock(&root->fs_info->trans_lock); 2076 spin_unlock(&fs_info->trans_lock);
2057 wait_event(cur_trans->writer_wait, 2077 wait_event(cur_trans->writer_wait,
2058 atomic_read(&cur_trans->num_writers) == 1); 2078 atomic_read(&cur_trans->num_writers) == 1);
2059 2079
@@ -2067,16 +2087,16 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2067 * the balancing code from coming in and moving 2087 * the balancing code from coming in and moving
2068 * extents around in the middle of the commit 2088 * extents around in the middle of the commit
2069 */ 2089 */
2070 mutex_lock(&root->fs_info->reloc_mutex); 2090 mutex_lock(&fs_info->reloc_mutex);
2071 2091
2072 /* 2092 /*
2073 * We needn't worry about the delayed items because we will 2093 * We needn't worry about the delayed items because we will
2074 * deal with them in create_pending_snapshot(), which is the 2094 * deal with them in create_pending_snapshot(), which is the
2075 * core function of the snapshot creation. 2095 * core function of the snapshot creation.
2076 */ 2096 */
2077 ret = create_pending_snapshots(trans, root->fs_info); 2097 ret = create_pending_snapshots(trans, fs_info);
2078 if (ret) { 2098 if (ret) {
2079 mutex_unlock(&root->fs_info->reloc_mutex); 2099 mutex_unlock(&fs_info->reloc_mutex);
2080 goto scrub_continue; 2100 goto scrub_continue;
2081 } 2101 }
2082 2102
@@ -2092,20 +2112,20 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2092 */ 2112 */
2093 ret = btrfs_run_delayed_items(trans, root); 2113 ret = btrfs_run_delayed_items(trans, root);
2094 if (ret) { 2114 if (ret) {
2095 mutex_unlock(&root->fs_info->reloc_mutex); 2115 mutex_unlock(&fs_info->reloc_mutex);
2096 goto scrub_continue; 2116 goto scrub_continue;
2097 } 2117 }
2098 2118
2099 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1); 2119 ret = btrfs_run_delayed_refs(trans, root, (unsigned long)-1);
2100 if (ret) { 2120 if (ret) {
2101 mutex_unlock(&root->fs_info->reloc_mutex); 2121 mutex_unlock(&fs_info->reloc_mutex);
2102 goto scrub_continue; 2122 goto scrub_continue;
2103 } 2123 }
2104 2124
2105 /* Reocrd old roots for later qgroup accounting */ 2125 /* Reocrd old roots for later qgroup accounting */
2106 ret = btrfs_qgroup_prepare_account_extents(trans, root->fs_info); 2126 ret = btrfs_qgroup_prepare_account_extents(trans, fs_info);
2107 if (ret) { 2127 if (ret) {
2108 mutex_unlock(&root->fs_info->reloc_mutex); 2128 mutex_unlock(&fs_info->reloc_mutex);
2109 goto scrub_continue; 2129 goto scrub_continue;
2110 } 2130 }
2111 2131
@@ -2130,12 +2150,12 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2130 * from now until after the super is written, we avoid races 2150 * from now until after the super is written, we avoid races
2131 * with the tree-log code. 2151 * with the tree-log code.
2132 */ 2152 */
2133 mutex_lock(&root->fs_info->tree_log_mutex); 2153 mutex_lock(&fs_info->tree_log_mutex);
2134 2154
2135 ret = commit_fs_roots(trans, root->fs_info); 2155 ret = commit_fs_roots(trans, fs_info);
2136 if (ret) { 2156 if (ret) {
2137 mutex_unlock(&root->fs_info->tree_log_mutex); 2157 mutex_unlock(&fs_info->tree_log_mutex);
2138 mutex_unlock(&root->fs_info->reloc_mutex); 2158 mutex_unlock(&fs_info->reloc_mutex);
2139 goto scrub_continue; 2159 goto scrub_continue;
2140 } 2160 }
2141 2161
@@ -2143,28 +2163,28 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2143 * Since the transaction is done, we can apply the pending changes 2163 * Since the transaction is done, we can apply the pending changes
2144 * before the next transaction. 2164 * before the next transaction.
2145 */ 2165 */
2146 btrfs_apply_pending_changes(root->fs_info); 2166 btrfs_apply_pending_changes(fs_info);
2147 2167
2148 /* commit_fs_roots gets rid of all the tree log roots, it is now 2168 /* commit_fs_roots gets rid of all the tree log roots, it is now
2149 * safe to free the root of tree log roots 2169 * safe to free the root of tree log roots
2150 */ 2170 */
2151 btrfs_free_log_root_tree(trans, root->fs_info); 2171 btrfs_free_log_root_tree(trans, fs_info);
2152 2172
2153 /* 2173 /*
2154 * Since fs roots are all committed, we can get a quite accurate 2174 * Since fs roots are all committed, we can get a quite accurate
2155 * new_roots. So let's do quota accounting. 2175 * new_roots. So let's do quota accounting.
2156 */ 2176 */
2157 ret = btrfs_qgroup_account_extents(trans, root->fs_info); 2177 ret = btrfs_qgroup_account_extents(trans, fs_info);
2158 if (ret < 0) { 2178 if (ret < 0) {
2159 mutex_unlock(&root->fs_info->tree_log_mutex); 2179 mutex_unlock(&fs_info->tree_log_mutex);
2160 mutex_unlock(&root->fs_info->reloc_mutex); 2180 mutex_unlock(&fs_info->reloc_mutex);
2161 goto scrub_continue; 2181 goto scrub_continue;
2162 } 2182 }
2163 2183
2164 ret = commit_cowonly_roots(trans, root); 2184 ret = commit_cowonly_roots(trans, root);
2165 if (ret) { 2185 if (ret) {
2166 mutex_unlock(&root->fs_info->tree_log_mutex); 2186 mutex_unlock(&fs_info->tree_log_mutex);
2167 mutex_unlock(&root->fs_info->reloc_mutex); 2187 mutex_unlock(&fs_info->reloc_mutex);
2168 goto scrub_continue; 2188 goto scrub_continue;
2169 } 2189 }
2170 2190
@@ -2174,64 +2194,64 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2174 */ 2194 */
2175 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) { 2195 if (unlikely(ACCESS_ONCE(cur_trans->aborted))) {
2176 ret = cur_trans->aborted; 2196 ret = cur_trans->aborted;
2177 mutex_unlock(&root->fs_info->tree_log_mutex); 2197 mutex_unlock(&fs_info->tree_log_mutex);
2178 mutex_unlock(&root->fs_info->reloc_mutex); 2198 mutex_unlock(&fs_info->reloc_mutex);
2179 goto scrub_continue; 2199 goto scrub_continue;
2180 } 2200 }
2181 2201
2182 btrfs_prepare_extent_commit(trans, root); 2202 btrfs_prepare_extent_commit(trans, root);
2183 2203
2184 cur_trans = root->fs_info->running_transaction; 2204 cur_trans = fs_info->running_transaction;
2185 2205
2186 btrfs_set_root_node(&root->fs_info->tree_root->root_item, 2206 btrfs_set_root_node(&fs_info->tree_root->root_item,
2187 root->fs_info->tree_root->node); 2207 fs_info->tree_root->node);
2188 list_add_tail(&root->fs_info->tree_root->dirty_list, 2208 list_add_tail(&fs_info->tree_root->dirty_list,
2189 &cur_trans->switch_commits); 2209 &cur_trans->switch_commits);
2190 2210
2191 btrfs_set_root_node(&root->fs_info->chunk_root->root_item, 2211 btrfs_set_root_node(&fs_info->chunk_root->root_item,
2192 root->fs_info->chunk_root->node); 2212 fs_info->chunk_root->node);
2193 list_add_tail(&root->fs_info->chunk_root->dirty_list, 2213 list_add_tail(&fs_info->chunk_root->dirty_list,
2194 &cur_trans->switch_commits); 2214 &cur_trans->switch_commits);
2195 2215
2196 switch_commit_roots(cur_trans, root->fs_info); 2216 switch_commit_roots(cur_trans, fs_info);
2197 2217
2198 assert_qgroups_uptodate(trans); 2218 assert_qgroups_uptodate(trans);
2199 ASSERT(list_empty(&cur_trans->dirty_bgs)); 2219 ASSERT(list_empty(&cur_trans->dirty_bgs));
2200 ASSERT(list_empty(&cur_trans->io_bgs)); 2220 ASSERT(list_empty(&cur_trans->io_bgs));
2201 update_super_roots(root); 2221 update_super_roots(root);
2202 2222
2203 btrfs_set_super_log_root(root->fs_info->super_copy, 0); 2223 btrfs_set_super_log_root(fs_info->super_copy, 0);
2204 btrfs_set_super_log_root_level(root->fs_info->super_copy, 0); 2224 btrfs_set_super_log_root_level(fs_info->super_copy, 0);
2205 memcpy(root->fs_info->super_for_commit, root->fs_info->super_copy, 2225 memcpy(fs_info->super_for_commit, fs_info->super_copy,
2206 sizeof(*root->fs_info->super_copy)); 2226 sizeof(*fs_info->super_copy));
2207 2227
2208 btrfs_update_commit_device_size(root->fs_info); 2228 btrfs_update_commit_device_size(fs_info);
2209 btrfs_update_commit_device_bytes_used(root, cur_trans); 2229 btrfs_update_commit_device_bytes_used(root, cur_trans);
2210 2230
2211 clear_bit(BTRFS_FS_LOG1_ERR, &root->fs_info->flags); 2231 clear_bit(BTRFS_FS_LOG1_ERR, &fs_info->flags);
2212 clear_bit(BTRFS_FS_LOG2_ERR, &root->fs_info->flags); 2232 clear_bit(BTRFS_FS_LOG2_ERR, &fs_info->flags);
2213 2233
2214 btrfs_trans_release_chunk_metadata(trans); 2234 btrfs_trans_release_chunk_metadata(trans);
2215 2235
2216 spin_lock(&root->fs_info->trans_lock); 2236 spin_lock(&fs_info->trans_lock);
2217 cur_trans->state = TRANS_STATE_UNBLOCKED; 2237 cur_trans->state = TRANS_STATE_UNBLOCKED;
2218 root->fs_info->running_transaction = NULL; 2238 fs_info->running_transaction = NULL;
2219 spin_unlock(&root->fs_info->trans_lock); 2239 spin_unlock(&fs_info->trans_lock);
2220 mutex_unlock(&root->fs_info->reloc_mutex); 2240 mutex_unlock(&fs_info->reloc_mutex);
2221 2241
2222 wake_up(&root->fs_info->transaction_wait); 2242 wake_up(&fs_info->transaction_wait);
2223 2243
2224 ret = btrfs_write_and_wait_transaction(trans, root); 2244 ret = btrfs_write_and_wait_transaction(trans, root);
2225 if (ret) { 2245 if (ret) {
2226 btrfs_handle_fs_error(root->fs_info, ret, 2246 btrfs_handle_fs_error(fs_info, ret,
2227 "Error while writing out transaction"); 2247 "Error while writing out transaction");
2228 mutex_unlock(&root->fs_info->tree_log_mutex); 2248 mutex_unlock(&fs_info->tree_log_mutex);
2229 goto scrub_continue; 2249 goto scrub_continue;
2230 } 2250 }
2231 2251
2232 ret = write_ctree_super(trans, root, 0); 2252 ret = write_ctree_super(trans, root, 0);
2233 if (ret) { 2253 if (ret) {
2234 mutex_unlock(&root->fs_info->tree_log_mutex); 2254 mutex_unlock(&fs_info->tree_log_mutex);
2235 goto scrub_continue; 2255 goto scrub_continue;
2236 } 2256 }
2237 2257
@@ -2239,14 +2259,14 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2239 * the super is written, we can safely allow the tree-loggers 2259 * the super is written, we can safely allow the tree-loggers
2240 * to go about their business 2260 * to go about their business
2241 */ 2261 */
2242 mutex_unlock(&root->fs_info->tree_log_mutex); 2262 mutex_unlock(&fs_info->tree_log_mutex);
2243 2263
2244 btrfs_finish_extent_commit(trans, root); 2264 btrfs_finish_extent_commit(trans, root);
2245 2265
2246 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags)) 2266 if (test_bit(BTRFS_TRANS_HAVE_FREE_BGS, &cur_trans->flags))
2247 btrfs_clear_space_info_full(root->fs_info); 2267 btrfs_clear_space_info_full(fs_info);
2248 2268
2249 root->fs_info->last_trans_committed = cur_trans->transid; 2269 fs_info->last_trans_committed = cur_trans->transid;
2250 /* 2270 /*
2251 * We needn't acquire the lock here because there is no other task 2271 * We needn't acquire the lock here because there is no other task
2252 * which can change it. 2272 * which can change it.
@@ -2254,15 +2274,15 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2254 cur_trans->state = TRANS_STATE_COMPLETED; 2274 cur_trans->state = TRANS_STATE_COMPLETED;
2255 wake_up(&cur_trans->commit_wait); 2275 wake_up(&cur_trans->commit_wait);
2256 2276
2257 spin_lock(&root->fs_info->trans_lock); 2277 spin_lock(&fs_info->trans_lock);
2258 list_del_init(&cur_trans->list); 2278 list_del_init(&cur_trans->list);
2259 spin_unlock(&root->fs_info->trans_lock); 2279 spin_unlock(&fs_info->trans_lock);
2260 2280
2261 btrfs_put_transaction(cur_trans); 2281 btrfs_put_transaction(cur_trans);
2262 btrfs_put_transaction(cur_trans); 2282 btrfs_put_transaction(cur_trans);
2263 2283
2264 if (trans->type & __TRANS_FREEZABLE) 2284 if (trans->type & __TRANS_FREEZABLE)
2265 sb_end_intwrite(root->fs_info->sb); 2285 sb_end_intwrite(fs_info->sb);
2266 2286
2267 trace_btrfs_transaction_commit(root); 2287 trace_btrfs_transaction_commit(root);
2268 2288
@@ -2277,9 +2297,8 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2277 * If fs has been frozen, we can not handle delayed iputs, otherwise 2297 * If fs has been frozen, we can not handle delayed iputs, otherwise
2278 * it'll result in deadlock about SB_FREEZE_FS. 2298 * it'll result in deadlock about SB_FREEZE_FS.
2279 */ 2299 */
2280 if (current != root->fs_info->transaction_kthread && 2300 if (current != fs_info->transaction_kthread &&
2281 current != root->fs_info->cleaner_kthread && 2301 current != fs_info->cleaner_kthread && !fs_info->fs_frozen)
2282 !root->fs_info->fs_frozen)
2283 btrfs_run_delayed_iputs(root); 2302 btrfs_run_delayed_iputs(root);
2284 2303
2285 return ret; 2304 return ret;
@@ -2290,7 +2309,7 @@ cleanup_transaction:
2290 btrfs_trans_release_metadata(trans, root); 2309 btrfs_trans_release_metadata(trans, root);
2291 btrfs_trans_release_chunk_metadata(trans); 2310 btrfs_trans_release_chunk_metadata(trans);
2292 trans->block_rsv = NULL; 2311 trans->block_rsv = NULL;
2293 btrfs_warn(root->fs_info, "Skipping commit of aborted transaction."); 2312 btrfs_warn(fs_info, "Skipping commit of aborted transaction.");
2294 if (current->journal_info == trans) 2313 if (current->journal_info == trans)
2295 current->journal_info = NULL; 2314 current->journal_info = NULL;
2296 cleanup_transaction(trans, root, ret); 2315 cleanup_transaction(trans, root, ret);
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index ad3ae2ef0cd4..dcb225e6a1c7 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -142,12 +142,13 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root, 142 struct btrfs_root *root,
143 struct btrfs_log_ctx *ctx) 143 struct btrfs_log_ctx *ctx)
144{ 144{
145 struct btrfs_fs_info *fs_info = root->fs_info;
145 int ret = 0; 146 int ret = 0;
146 147
147 mutex_lock(&root->log_mutex); 148 mutex_lock(&root->log_mutex);
148 149
149 if (root->log_root) { 150 if (root->log_root) {
150 if (btrfs_need_log_full_commit(root->fs_info, trans)) { 151 if (btrfs_need_log_full_commit(fs_info, trans)) {
151 ret = -EAGAIN; 152 ret = -EAGAIN;
152 goto out; 153 goto out;
153 } 154 }
@@ -159,10 +160,10 @@ static int start_log_trans(struct btrfs_trans_handle *trans,
159 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state); 160 set_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state);
160 } 161 }
161 } else { 162 } else {
162 mutex_lock(&root->fs_info->tree_log_mutex); 163 mutex_lock(&fs_info->tree_log_mutex);
163 if (!root->fs_info->log_root_tree) 164 if (!fs_info->log_root_tree)
164 ret = btrfs_init_log_root_tree(trans, root->fs_info); 165 ret = btrfs_init_log_root_tree(trans, fs_info);
165 mutex_unlock(&root->fs_info->tree_log_mutex); 166 mutex_unlock(&fs_info->tree_log_mutex);
166 if (ret) 167 if (ret)
167 goto out; 168 goto out;
168 169
@@ -292,20 +293,21 @@ static int process_one_buffer(struct btrfs_root *log,
292 struct extent_buffer *eb, 293 struct extent_buffer *eb,
293 struct walk_control *wc, u64 gen) 294 struct walk_control *wc, u64 gen)
294{ 295{
296 struct btrfs_fs_info *fs_info = log->fs_info;
295 int ret = 0; 297 int ret = 0;
296 298
297 /* 299 /*
298 * If this fs is mixed then we need to be able to process the leaves to 300 * If this fs is mixed then we need to be able to process the leaves to
299 * pin down any logged extents, so we have to read the block. 301 * pin down any logged extents, so we have to read the block.
300 */ 302 */
301 if (btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) { 303 if (btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
302 ret = btrfs_read_buffer(eb, gen); 304 ret = btrfs_read_buffer(eb, gen);
303 if (ret) 305 if (ret)
304 return ret; 306 return ret;
305 } 307 }
306 308
307 if (wc->pin) 309 if (wc->pin)
308 ret = btrfs_pin_extent_for_log_replay(log->fs_info->extent_root, 310 ret = btrfs_pin_extent_for_log_replay(fs_info->extent_root,
309 eb->start, eb->len); 311 eb->start, eb->len);
310 312
311 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) { 313 if (!ret && btrfs_buffer_uptodate(eb, gen, 0)) {
@@ -582,6 +584,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
582 struct extent_buffer *eb, int slot, 584 struct extent_buffer *eb, int slot,
583 struct btrfs_key *key) 585 struct btrfs_key *key)
584{ 586{
587 struct btrfs_fs_info *fs_info = root->fs_info;
585 int found_type; 588 int found_type;
586 u64 extent_end; 589 u64 extent_end;
587 u64 start = key->offset; 590 u64 start = key->offset;
@@ -609,7 +612,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
609 size = btrfs_file_extent_inline_len(eb, slot, item); 612 size = btrfs_file_extent_inline_len(eb, slot, item);
610 nbytes = btrfs_file_extent_ram_bytes(eb, item); 613 nbytes = btrfs_file_extent_ram_bytes(eb, item);
611 extent_end = ALIGN(start + size, 614 extent_end = ALIGN(start + size,
612 root->fs_info->sectorsize); 615 fs_info->sectorsize);
613 } else { 616 } else {
614 ret = 0; 617 ret = 0;
615 goto out; 618 goto out;
@@ -690,7 +693,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
690 * as the owner of the file extent changed from log tree 693 * as the owner of the file extent changed from log tree
691 * (doesn't affect qgroup) to fs/file tree(affects qgroup) 694 * (doesn't affect qgroup) to fs/file tree(affects qgroup)
692 */ 695 */
693 ret = btrfs_qgroup_trace_extent(trans, root->fs_info, 696 ret = btrfs_qgroup_trace_extent(trans, fs_info,
694 btrfs_file_extent_disk_bytenr(eb, item), 697 btrfs_file_extent_disk_bytenr(eb, item),
695 btrfs_file_extent_disk_num_bytes(eb, item), 698 btrfs_file_extent_disk_num_bytes(eb, item),
696 GFP_NOFS); 699 GFP_NOFS);
@@ -797,14 +800,12 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans,
797 struct btrfs_ordered_sum, 800 struct btrfs_ordered_sum,
798 list); 801 list);
799 if (!ret) 802 if (!ret)
800 ret = btrfs_del_csums(trans, 803 ret = btrfs_del_csums(trans, fs_info,
801 root->fs_info,
802 sums->bytenr, 804 sums->bytenr,
803 sums->len); 805 sums->len);
804 if (!ret) 806 if (!ret)
805 ret = btrfs_csum_file_blocks(trans, 807 ret = btrfs_csum_file_blocks(trans,
806 root->fs_info->csum_root, 808 fs_info->csum_root, sums);
807 sums);
808 list_del(&sums->list); 809 list_del(&sums->list);
809 kfree(sums); 810 kfree(sums);
810 } 811 }
@@ -2408,6 +2409,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2408 struct btrfs_path *path, int *level, 2409 struct btrfs_path *path, int *level,
2409 struct walk_control *wc) 2410 struct walk_control *wc)
2410{ 2411{
2412 struct btrfs_fs_info *fs_info = root->fs_info;
2411 u64 root_owner; 2413 u64 root_owner;
2412 u64 bytenr; 2414 u64 bytenr;
2413 u64 ptr_gen; 2415 u64 ptr_gen;
@@ -2433,7 +2435,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2433 2435
2434 bytenr = btrfs_node_blockptr(cur, path->slots[*level]); 2436 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2435 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]); 2437 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2436 blocksize = root->fs_info->nodesize; 2438 blocksize = fs_info->nodesize;
2437 2439
2438 parent = path->nodes[*level]; 2440 parent = path->nodes[*level];
2439 root_owner = btrfs_header_owner(parent); 2441 root_owner = btrfs_header_owner(parent);
@@ -2460,8 +2462,7 @@ static noinline int walk_down_log_tree(struct btrfs_trans_handle *trans,
2460 if (trans) { 2462 if (trans) {
2461 btrfs_tree_lock(next); 2463 btrfs_tree_lock(next);
2462 btrfs_set_lock_blocking(next); 2464 btrfs_set_lock_blocking(next);
2463 clean_tree_block(trans, root->fs_info, 2465 clean_tree_block(trans, fs_info, next);
2464 next);
2465 btrfs_wait_tree_block_writeback(next); 2466 btrfs_wait_tree_block_writeback(next);
2466 btrfs_tree_unlock(next); 2467 btrfs_tree_unlock(next);
2467 } 2468 }
@@ -2506,6 +2507,7 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2506 struct btrfs_path *path, int *level, 2507 struct btrfs_path *path, int *level,
2507 struct walk_control *wc) 2508 struct walk_control *wc)
2508{ 2509{
2510 struct btrfs_fs_info *fs_info = root->fs_info;
2509 u64 root_owner; 2511 u64 root_owner;
2510 int i; 2512 int i;
2511 int slot; 2513 int slot;
@@ -2539,8 +2541,7 @@ static noinline int walk_up_log_tree(struct btrfs_trans_handle *trans,
2539 if (trans) { 2541 if (trans) {
2540 btrfs_tree_lock(next); 2542 btrfs_tree_lock(next);
2541 btrfs_set_lock_blocking(next); 2543 btrfs_set_lock_blocking(next);
2542 clean_tree_block(trans, root->fs_info, 2544 clean_tree_block(trans, fs_info, next);
2543 next);
2544 btrfs_wait_tree_block_writeback(next); 2545 btrfs_wait_tree_block_writeback(next);
2545 btrfs_tree_unlock(next); 2546 btrfs_tree_unlock(next);
2546 } 2547 }
@@ -2642,14 +2643,15 @@ out:
2642static int update_log_root(struct btrfs_trans_handle *trans, 2643static int update_log_root(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *log) 2644 struct btrfs_root *log)
2644{ 2645{
2646 struct btrfs_fs_info *fs_info = log->fs_info;
2645 int ret; 2647 int ret;
2646 2648
2647 if (log->log_transid == 1) { 2649 if (log->log_transid == 1) {
2648 /* insert root item on the first sync */ 2650 /* insert root item on the first sync */
2649 ret = btrfs_insert_root(trans, log->fs_info->log_root_tree, 2651 ret = btrfs_insert_root(trans, fs_info->log_root_tree,
2650 &log->root_key, &log->root_item); 2652 &log->root_key, &log->root_item);
2651 } else { 2653 } else {
2652 ret = btrfs_update_root(trans, log->fs_info->log_root_tree, 2654 ret = btrfs_update_root(trans, fs_info->log_root_tree,
2653 &log->root_key, &log->root_item); 2655 &log->root_key, &log->root_item);
2654 } 2656 }
2655 return ret; 2657 return ret;
@@ -2743,8 +2745,9 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2743 int index2; 2745 int index2;
2744 int mark; 2746 int mark;
2745 int ret; 2747 int ret;
2748 struct btrfs_fs_info *fs_info = root->fs_info;
2746 struct btrfs_root *log = root->log_root; 2749 struct btrfs_root *log = root->log_root;
2747 struct btrfs_root *log_root_tree = root->fs_info->log_root_tree; 2750 struct btrfs_root *log_root_tree = fs_info->log_root_tree;
2748 int log_transid = 0; 2751 int log_transid = 0;
2749 struct btrfs_log_ctx root_log_ctx; 2752 struct btrfs_log_ctx root_log_ctx;
2750 struct blk_plug plug; 2753 struct blk_plug plug;
@@ -2772,7 +2775,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2772 while (1) { 2775 while (1) {
2773 int batch = atomic_read(&root->log_batch); 2776 int batch = atomic_read(&root->log_batch);
2774 /* when we're on an ssd, just kick the log commit out */ 2777 /* when we're on an ssd, just kick the log commit out */
2775 if (!btrfs_test_opt(root->fs_info, SSD) && 2778 if (!btrfs_test_opt(fs_info, SSD) &&
2776 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) { 2779 test_bit(BTRFS_ROOT_MULTI_LOG_TASKS, &root->state)) {
2777 mutex_unlock(&root->log_mutex); 2780 mutex_unlock(&root->log_mutex);
2778 schedule_timeout_uninterruptible(1); 2781 schedule_timeout_uninterruptible(1);
@@ -2784,7 +2787,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2784 } 2787 }
2785 2788
2786 /* bail out if we need to do a full commit */ 2789 /* bail out if we need to do a full commit */
2787 if (btrfs_need_log_full_commit(root->fs_info, trans)) { 2790 if (btrfs_need_log_full_commit(fs_info, trans)) {
2788 ret = -EAGAIN; 2791 ret = -EAGAIN;
2789 btrfs_free_logged_extents(log, log_transid); 2792 btrfs_free_logged_extents(log, log_transid);
2790 mutex_unlock(&root->log_mutex); 2793 mutex_unlock(&root->log_mutex);
@@ -2805,7 +2808,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2805 blk_finish_plug(&plug); 2808 blk_finish_plug(&plug);
2806 btrfs_abort_transaction(trans, ret); 2809 btrfs_abort_transaction(trans, ret);
2807 btrfs_free_logged_extents(log, log_transid); 2810 btrfs_free_logged_extents(log, log_transid);
2808 btrfs_set_log_full_commit(root->fs_info, trans); 2811 btrfs_set_log_full_commit(fs_info, trans);
2809 mutex_unlock(&root->log_mutex); 2812 mutex_unlock(&root->log_mutex);
2810 goto out; 2813 goto out;
2811 } 2814 }
@@ -2850,7 +2853,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2850 list_del_init(&root_log_ctx.list); 2853 list_del_init(&root_log_ctx.list);
2851 2854
2852 blk_finish_plug(&plug); 2855 blk_finish_plug(&plug);
2853 btrfs_set_log_full_commit(root->fs_info, trans); 2856 btrfs_set_log_full_commit(fs_info, trans);
2854 2857
2855 if (ret != -ENOSPC) { 2858 if (ret != -ENOSPC) {
2856 btrfs_abort_transaction(trans, ret); 2859 btrfs_abort_transaction(trans, ret);
@@ -2899,7 +2902,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2899 * now that we've moved on to the tree of log tree roots, 2902 * now that we've moved on to the tree of log tree roots,
2900 * check the full commit flag again 2903 * check the full commit flag again
2901 */ 2904 */
2902 if (btrfs_need_log_full_commit(root->fs_info, trans)) { 2905 if (btrfs_need_log_full_commit(fs_info, trans)) {
2903 blk_finish_plug(&plug); 2906 blk_finish_plug(&plug);
2904 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark); 2907 btrfs_wait_marked_extents(log, &log->dirty_log_pages, mark);
2905 btrfs_free_logged_extents(log, log_transid); 2908 btrfs_free_logged_extents(log, log_transid);
@@ -2913,7 +2916,7 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2913 EXTENT_DIRTY | EXTENT_NEW); 2916 EXTENT_DIRTY | EXTENT_NEW);
2914 blk_finish_plug(&plug); 2917 blk_finish_plug(&plug);
2915 if (ret) { 2918 if (ret) {
2916 btrfs_set_log_full_commit(root->fs_info, trans); 2919 btrfs_set_log_full_commit(fs_info, trans);
2917 btrfs_abort_transaction(trans, ret); 2920 btrfs_abort_transaction(trans, ret);
2918 btrfs_free_logged_extents(log, log_transid); 2921 btrfs_free_logged_extents(log, log_transid);
2919 mutex_unlock(&log_root_tree->log_mutex); 2922 mutex_unlock(&log_root_tree->log_mutex);
@@ -2925,17 +2928,17 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2925 &log_root_tree->dirty_log_pages, 2928 &log_root_tree->dirty_log_pages,
2926 EXTENT_NEW | EXTENT_DIRTY); 2929 EXTENT_NEW | EXTENT_DIRTY);
2927 if (ret) { 2930 if (ret) {
2928 btrfs_set_log_full_commit(root->fs_info, trans); 2931 btrfs_set_log_full_commit(fs_info, trans);
2929 btrfs_free_logged_extents(log, log_transid); 2932 btrfs_free_logged_extents(log, log_transid);
2930 mutex_unlock(&log_root_tree->log_mutex); 2933 mutex_unlock(&log_root_tree->log_mutex);
2931 goto out_wake_log_root; 2934 goto out_wake_log_root;
2932 } 2935 }
2933 btrfs_wait_logged_extents(trans, log, log_transid); 2936 btrfs_wait_logged_extents(trans, log, log_transid);
2934 2937
2935 btrfs_set_super_log_root(root->fs_info->super_for_commit, 2938 btrfs_set_super_log_root(fs_info->super_for_commit,
2936 log_root_tree->node->start); 2939 log_root_tree->node->start);
2937 btrfs_set_super_log_root_level(root->fs_info->super_for_commit, 2940 btrfs_set_super_log_root_level(fs_info->super_for_commit,
2938 btrfs_header_level(log_root_tree->node)); 2941 btrfs_header_level(log_root_tree->node));
2939 2942
2940 log_root_tree->log_transid++; 2943 log_root_tree->log_transid++;
2941 mutex_unlock(&log_root_tree->log_mutex); 2944 mutex_unlock(&log_root_tree->log_mutex);
@@ -2947,9 +2950,9 @@ int btrfs_sync_log(struct btrfs_trans_handle *trans,
2947 * the running transaction open, so a full commit can't hop 2950 * the running transaction open, so a full commit can't hop
2948 * in and cause problems either. 2951 * in and cause problems either.
2949 */ 2952 */
2950 ret = write_ctree_super(trans, root->fs_info->tree_root, 1); 2953 ret = write_ctree_super(trans, fs_info->tree_root, 1);
2951 if (ret) { 2954 if (ret) {
2952 btrfs_set_log_full_commit(root->fs_info, trans); 2955 btrfs_set_log_full_commit(fs_info, trans);
2953 btrfs_abort_transaction(trans, ret); 2956 btrfs_abort_transaction(trans, ret);
2954 goto out_wake_log_root; 2957 goto out_wake_log_root;
2955 } 2958 }
@@ -3183,6 +3186,7 @@ int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3183 const char *name, int name_len, 3186 const char *name, int name_len,
3184 struct inode *inode, u64 dirid) 3187 struct inode *inode, u64 dirid)
3185{ 3188{
3189 struct btrfs_fs_info *fs_info = root->fs_info;
3186 struct btrfs_root *log; 3190 struct btrfs_root *log;
3187 u64 index; 3191 u64 index;
3188 int ret; 3192 int ret;
@@ -3200,7 +3204,7 @@ int btrfs_del_inode_ref_in_log(struct btrfs_trans_handle *trans,
3200 dirid, &index); 3204 dirid, &index);
3201 mutex_unlock(&BTRFS_I(inode)->log_mutex); 3205 mutex_unlock(&BTRFS_I(inode)->log_mutex);
3202 if (ret == -ENOSPC) { 3206 if (ret == -ENOSPC) {
3203 btrfs_set_log_full_commit(root->fs_info, trans); 3207 btrfs_set_log_full_commit(fs_info, trans);
3204 ret = 0; 3208 ret = 0;
3205 } else if (ret < 0 && ret != -ENOENT) 3209 } else if (ret < 0 && ret != -ENOENT)
3206 btrfs_abort_transaction(trans, ret); 3210 btrfs_abort_transaction(trans, ret);
@@ -3607,6 +3611,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
3607 int start_slot, int nr, int inode_only, 3611 int start_slot, int nr, int inode_only,
3608 u64 logged_isize) 3612 u64 logged_isize)
3609{ 3613{
3614 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3610 unsigned long src_offset; 3615 unsigned long src_offset;
3611 unsigned long dst_offset; 3616 unsigned long dst_offset;
3612 struct btrfs_root *log = BTRFS_I(inode)->root->log_root; 3617 struct btrfs_root *log = BTRFS_I(inode)->root->log_root;
@@ -3717,7 +3722,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
3717 } 3722 }
3718 3723
3719 ret = btrfs_lookup_csums_range( 3724 ret = btrfs_lookup_csums_range(
3720 log->fs_info->csum_root, 3725 fs_info->csum_root,
3721 ds + cs, ds + cs + cl - 1, 3726 ds + cs, ds + cs + cl - 1,
3722 &ordered_sums, 0); 3727 &ordered_sums, 0);
3723 if (ret) { 3728 if (ret) {
@@ -3790,7 +3795,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans,
3790 src_path->slots[0], 3795 src_path->slots[0],
3791 extent); 3796 extent);
3792 *last_extent = ALIGN(key.offset + len, 3797 *last_extent = ALIGN(key.offset + len,
3793 log->fs_info->sectorsize); 3798 fs_info->sectorsize);
3794 } else { 3799 } else {
3795 len = btrfs_file_extent_num_bytes(src, extent); 3800 len = btrfs_file_extent_num_bytes(src, extent);
3796 *last_extent = key.offset + len; 3801 *last_extent = key.offset + len;
@@ -3854,7 +3859,7 @@ fill_holes:
3854 BTRFS_FILE_EXTENT_INLINE) { 3859 BTRFS_FILE_EXTENT_INLINE) {
3855 len = btrfs_file_extent_inline_len(src, i, extent); 3860 len = btrfs_file_extent_inline_len(src, i, extent);
3856 extent_end = ALIGN(key.offset + len, 3861 extent_end = ALIGN(key.offset + len,
3857 log->fs_info->sectorsize); 3862 fs_info->sectorsize);
3858 } else { 3863 } else {
3859 len = btrfs_file_extent_num_bytes(src, extent); 3864 len = btrfs_file_extent_num_bytes(src, extent);
3860 extent_end = key.offset + len; 3865 extent_end = key.offset + len;
@@ -3904,6 +3909,7 @@ static int wait_ordered_extents(struct btrfs_trans_handle *trans,
3904 const struct list_head *logged_list, 3909 const struct list_head *logged_list,
3905 bool *ordered_io_error) 3910 bool *ordered_io_error)
3906{ 3911{
3912 struct btrfs_fs_info *fs_info = root->fs_info;
3907 struct btrfs_ordered_extent *ordered; 3913 struct btrfs_ordered_extent *ordered;
3908 struct btrfs_root *log = root->log_root; 3914 struct btrfs_root *log = root->log_root;
3909 u64 mod_start = em->mod_start; 3915 u64 mod_start = em->mod_start;
@@ -4020,7 +4026,7 @@ static int wait_ordered_extents(struct btrfs_trans_handle *trans,
4020 } 4026 }
4021 4027
4022 /* block start is already adjusted for the file extent offset. */ 4028 /* block start is already adjusted for the file extent offset. */
4023 ret = btrfs_lookup_csums_range(log->fs_info->csum_root, 4029 ret = btrfs_lookup_csums_range(fs_info->csum_root,
4024 em->block_start + csum_offset, 4030 em->block_start + csum_offset,
4025 em->block_start + csum_offset + 4031 em->block_start + csum_offset +
4026 csum_len - 1, &ordered_sums, 0); 4032 csum_len - 1, &ordered_sums, 0);
@@ -4363,6 +4369,7 @@ static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4363 struct inode *inode, 4369 struct inode *inode,
4364 struct btrfs_path *path) 4370 struct btrfs_path *path)
4365{ 4371{
4372 struct btrfs_fs_info *fs_info = root->fs_info;
4366 int ret; 4373 int ret;
4367 struct btrfs_key key; 4374 struct btrfs_key key;
4368 u64 hole_start; 4375 u64 hole_start;
@@ -4372,7 +4379,7 @@ static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4372 const u64 ino = btrfs_ino(inode); 4379 const u64 ino = btrfs_ino(inode);
4373 const u64 i_size = i_size_read(inode); 4380 const u64 i_size = i_size_read(inode);
4374 4381
4375 if (!btrfs_fs_incompat(root->fs_info, NO_HOLES)) 4382 if (!btrfs_fs_incompat(fs_info, NO_HOLES))
4376 return 0; 4383 return 0;
4377 4384
4378 key.objectid = ino; 4385 key.objectid = ino;
@@ -4429,7 +4436,7 @@ static int btrfs_log_trailing_hole(struct btrfs_trans_handle *trans,
4429 if (hole_size == 0) 4436 if (hole_size == 0)
4430 return 0; 4437 return 0;
4431 4438
4432 hole_size = ALIGN(hole_size, root->fs_info->sectorsize); 4439 hole_size = ALIGN(hole_size, fs_info->sectorsize);
4433 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0, 4440 ret = btrfs_insert_file_extent(trans, log, ino, hole_start, 0, 0,
4434 hole_size, 0, hole_size, 0, 0, 0); 4441 hole_size, 0, hole_size, 0, 0, 0);
4435 return ret; 4442 return ret;
@@ -4587,6 +4594,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4587 const loff_t end, 4594 const loff_t end,
4588 struct btrfs_log_ctx *ctx) 4595 struct btrfs_log_ctx *ctx)
4589{ 4596{
4597 struct btrfs_fs_info *fs_info = root->fs_info;
4590 struct btrfs_path *path; 4598 struct btrfs_path *path;
4591 struct btrfs_path *dst_path; 4599 struct btrfs_path *dst_path;
4592 struct btrfs_key min_key; 4600 struct btrfs_key min_key;
@@ -4639,7 +4647,7 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
4639 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items). 4647 * fixup (create temporary BTRFS_TREE_LOG_FIXUP_OBJECTID items).
4640 */ 4648 */
4641 if (S_ISDIR(inode->i_mode) || 4649 if (S_ISDIR(inode->i_mode) ||
4642 BTRFS_I(inode)->generation > root->fs_info->last_trans_committed) 4650 BTRFS_I(inode)->generation > fs_info->last_trans_committed)
4643 ret = btrfs_commit_inode_delayed_items(trans, inode); 4651 ret = btrfs_commit_inode_delayed_items(trans, inode);
4644 else 4652 else
4645 ret = btrfs_commit_inode_delayed_inode(inode); 4653 ret = btrfs_commit_inode_delayed_inode(inode);
@@ -4776,7 +4784,7 @@ again:
4776 inode_key.objectid = other_ino; 4784 inode_key.objectid = other_ino;
4777 inode_key.type = BTRFS_INODE_ITEM_KEY; 4785 inode_key.type = BTRFS_INODE_ITEM_KEY;
4778 inode_key.offset = 0; 4786 inode_key.offset = 0;
4779 other_inode = btrfs_iget(root->fs_info->sb, 4787 other_inode = btrfs_iget(fs_info->sb,
4780 &inode_key, root, 4788 &inode_key, root,
4781 NULL); 4789 NULL);
4782 /* 4790 /*
@@ -5140,6 +5148,7 @@ static int log_new_dir_dentries(struct btrfs_trans_handle *trans,
5140 struct inode *start_inode, 5148 struct inode *start_inode,
5141 struct btrfs_log_ctx *ctx) 5149 struct btrfs_log_ctx *ctx)
5142{ 5150{
5151 struct btrfs_fs_info *fs_info = root->fs_info;
5143 struct btrfs_root *log = root->log_root; 5152 struct btrfs_root *log = root->log_root;
5144 struct btrfs_path *path; 5153 struct btrfs_path *path;
5145 LIST_HEAD(dir_list); 5154 LIST_HEAD(dir_list);
@@ -5207,8 +5216,7 @@ process_leaf:
5207 if (di_key.type == BTRFS_ROOT_ITEM_KEY) 5216 if (di_key.type == BTRFS_ROOT_ITEM_KEY)
5208 continue; 5217 continue;
5209 5218
5210 di_inode = btrfs_iget(root->fs_info->sb, &di_key, 5219 di_inode = btrfs_iget(fs_info->sb, &di_key, root, NULL);
5211 root, NULL);
5212 if (IS_ERR(di_inode)) { 5220 if (IS_ERR(di_inode)) {
5213 ret = PTR_ERR(di_inode); 5221 ret = PTR_ERR(di_inode);
5214 goto next_dir_inode; 5222 goto next_dir_inode;
@@ -5270,6 +5278,7 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5270 struct inode *inode, 5278 struct inode *inode,
5271 struct btrfs_log_ctx *ctx) 5279 struct btrfs_log_ctx *ctx)
5272{ 5280{
5281 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5273 int ret; 5282 int ret;
5274 struct btrfs_path *path; 5283 struct btrfs_path *path;
5275 struct btrfs_key key; 5284 struct btrfs_key key;
@@ -5334,7 +5343,7 @@ static int btrfs_log_all_parents(struct btrfs_trans_handle *trans,
5334 cur_offset = item_size; 5343 cur_offset = item_size;
5335 } 5344 }
5336 5345
5337 dir_inode = btrfs_iget(root->fs_info->sb, &inode_key, 5346 dir_inode = btrfs_iget(fs_info->sb, &inode_key,
5338 root, NULL); 5347 root, NULL);
5339 /* If parent inode was deleted, skip it. */ 5348 /* If parent inode was deleted, skip it. */
5340 if (IS_ERR(dir_inode)) 5349 if (IS_ERR(dir_inode))
@@ -5376,17 +5385,18 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5376 int exists_only, 5385 int exists_only,
5377 struct btrfs_log_ctx *ctx) 5386 struct btrfs_log_ctx *ctx)
5378{ 5387{
5388 struct btrfs_fs_info *fs_info = root->fs_info;
5379 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL; 5389 int inode_only = exists_only ? LOG_INODE_EXISTS : LOG_INODE_ALL;
5380 struct super_block *sb; 5390 struct super_block *sb;
5381 struct dentry *old_parent = NULL; 5391 struct dentry *old_parent = NULL;
5382 int ret = 0; 5392 int ret = 0;
5383 u64 last_committed = root->fs_info->last_trans_committed; 5393 u64 last_committed = fs_info->last_trans_committed;
5384 bool log_dentries = false; 5394 bool log_dentries = false;
5385 struct inode *orig_inode = inode; 5395 struct inode *orig_inode = inode;
5386 5396
5387 sb = inode->i_sb; 5397 sb = inode->i_sb;
5388 5398
5389 if (btrfs_test_opt(root->fs_info, NOTREELOG)) { 5399 if (btrfs_test_opt(fs_info, NOTREELOG)) {
5390 ret = 1; 5400 ret = 1;
5391 goto end_no_trans; 5401 goto end_no_trans;
5392 } 5402 }
@@ -5395,8 +5405,8 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5395 * The prev transaction commit doesn't complete, we need do 5405 * The prev transaction commit doesn't complete, we need do
5396 * full commit by ourselves. 5406 * full commit by ourselves.
5397 */ 5407 */
5398 if (root->fs_info->last_trans_log_full_commit > 5408 if (fs_info->last_trans_log_full_commit >
5399 root->fs_info->last_trans_committed) { 5409 fs_info->last_trans_committed) {
5400 ret = 1; 5410 ret = 1;
5401 goto end_no_trans; 5411 goto end_no_trans;
5402 } 5412 }
@@ -5517,7 +5527,7 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans,
5517end_trans: 5527end_trans:
5518 dput(old_parent); 5528 dput(old_parent);
5519 if (ret < 0) { 5529 if (ret < 0) {
5520 btrfs_set_log_full_commit(root->fs_info, trans); 5530 btrfs_set_log_full_commit(fs_info, trans);
5521 ret = 1; 5531 ret = 1;
5522 } 5532 }
5523 5533
@@ -5788,6 +5798,7 @@ int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5788 struct inode *inode, struct inode *old_dir, 5798 struct inode *inode, struct inode *old_dir,
5789 struct dentry *parent) 5799 struct dentry *parent)
5790{ 5800{
5801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5791 struct btrfs_root * root = BTRFS_I(inode)->root; 5802 struct btrfs_root * root = BTRFS_I(inode)->root;
5792 5803
5793 /* 5804 /*
@@ -5802,9 +5813,9 @@ int btrfs_log_new_name(struct btrfs_trans_handle *trans,
5802 * from hasn't been logged, we don't need to log it 5813 * from hasn't been logged, we don't need to log it
5803 */ 5814 */
5804 if (BTRFS_I(inode)->logged_trans <= 5815 if (BTRFS_I(inode)->logged_trans <=
5805 root->fs_info->last_trans_committed && 5816 fs_info->last_trans_committed &&
5806 (!old_dir || BTRFS_I(old_dir)->logged_trans <= 5817 (!old_dir || BTRFS_I(old_dir)->logged_trans <=
5807 root->fs_info->last_trans_committed)) 5818 fs_info->last_trans_committed))
5808 return 0; 5819 return 0;
5809 5820
5810 return btrfs_log_inode_parent(trans, root, inode, parent, 0, 5821 return btrfs_log_inode_parent(trans, root, inode, parent, 0,
diff --git a/fs/btrfs/uuid-tree.c b/fs/btrfs/uuid-tree.c
index e63846edb923..4464e80bb5ef 100644
--- a/fs/btrfs/uuid-tree.c
+++ b/fs/btrfs/uuid-tree.c
@@ -187,8 +187,8 @@ int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
187 187
188 ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1); 188 ret = btrfs_search_slot(trans, uuid_root, &key, path, -1, 1);
189 if (ret < 0) { 189 if (ret < 0) {
190 btrfs_warn(uuid_root->fs_info, 190 btrfs_warn(fs_info, "error %d while searching for uuid item!",
191 "error %d while searching for uuid item!", ret); 191 ret);
192 goto out; 192 goto out;
193 } 193 }
194 if (ret > 0) { 194 if (ret > 0) {
@@ -201,8 +201,7 @@ int btrfs_uuid_tree_rem(struct btrfs_trans_handle *trans,
201 offset = btrfs_item_ptr_offset(eb, slot); 201 offset = btrfs_item_ptr_offset(eb, slot);
202 item_size = btrfs_item_size_nr(eb, slot); 202 item_size = btrfs_item_size_nr(eb, slot);
203 if (!IS_ALIGNED(item_size, sizeof(u64))) { 203 if (!IS_ALIGNED(item_size, sizeof(u64))) {
204 btrfs_warn(uuid_root->fs_info, 204 btrfs_warn(fs_info, "uuid item with illegal size %lu!",
205 "uuid item with illegal size %lu!",
206 (unsigned long)item_size); 205 (unsigned long)item_size);
207 ret = -ENOENT; 206 ret = -ENOENT;
208 goto out; 207 goto out;
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 953582883034..f983d258bf5c 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -343,9 +343,9 @@ static void requeue_list(struct btrfs_pending_bios *pending_bios,
343 */ 343 */
344static noinline void run_scheduled_bios(struct btrfs_device *device) 344static noinline void run_scheduled_bios(struct btrfs_device *device)
345{ 345{
346 struct btrfs_fs_info *fs_info = device->fs_info;
346 struct bio *pending; 347 struct bio *pending;
347 struct backing_dev_info *bdi; 348 struct backing_dev_info *bdi;
348 struct btrfs_fs_info *fs_info;
349 struct btrfs_pending_bios *pending_bios; 349 struct btrfs_pending_bios *pending_bios;
350 struct bio *tail; 350 struct bio *tail;
351 struct bio *cur; 351 struct bio *cur;
@@ -367,7 +367,6 @@ static noinline void run_scheduled_bios(struct btrfs_device *device)
367 blk_start_plug(&plug); 367 blk_start_plug(&plug);
368 368
369 bdi = blk_get_backing_dev_info(device->bdev); 369 bdi = blk_get_backing_dev_info(device->bdev);
370 fs_info = device->fs_info;
371 limit = btrfs_async_submit_limit(fs_info); 370 limit = btrfs_async_submit_limit(fs_info);
372 limit = limit * 2 / 3; 371 limit = limit * 2 / 3;
373 372
@@ -1338,7 +1337,8 @@ int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1338 struct btrfs_device *device, u64 num_bytes, 1337 struct btrfs_device *device, u64 num_bytes,
1339 u64 search_start, u64 *start, u64 *len) 1338 u64 search_start, u64 *start, u64 *len)
1340{ 1339{
1341 struct btrfs_root *root = device->fs_info->dev_root; 1340 struct btrfs_fs_info *fs_info = device->fs_info;
1341 struct btrfs_root *root = fs_info->dev_root;
1342 struct btrfs_key key; 1342 struct btrfs_key key;
1343 struct btrfs_dev_extent *dev_extent; 1343 struct btrfs_dev_extent *dev_extent;
1344 struct btrfs_path *path; 1344 struct btrfs_path *path;
@@ -1357,7 +1357,7 @@ int find_free_dev_extent_start(struct btrfs_transaction *transaction,
1357 * used by the boot loader (grub for example), so we make sure to start 1357 * used by the boot loader (grub for example), so we make sure to start
1358 * at an offset of at least 1MB. 1358 * at an offset of at least 1MB.
1359 */ 1359 */
1360 min_search_start = max(root->fs_info->alloc_start, 1024ull * 1024); 1360 min_search_start = max(fs_info->alloc_start, 1024ull * 1024);
1361 search_start = max(search_start, min_search_start); 1361 search_start = max(search_start, min_search_start);
1362 1362
1363 path = btrfs_alloc_path(); 1363 path = btrfs_alloc_path();
@@ -1508,7 +1508,8 @@ static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
1508 struct btrfs_device *device, 1508 struct btrfs_device *device,
1509 u64 start, u64 *dev_extent_len) 1509 u64 start, u64 *dev_extent_len)
1510{ 1510{
1511 struct btrfs_root *root = device->fs_info->dev_root; 1511 struct btrfs_fs_info *fs_info = device->fs_info;
1512 struct btrfs_root *root = fs_info->dev_root;
1512 int ret; 1513 int ret;
1513 struct btrfs_path *path; 1514 struct btrfs_path *path;
1514 struct btrfs_key key; 1515 struct btrfs_key key;
@@ -1544,7 +1545,7 @@ again:
1544 extent = btrfs_item_ptr(leaf, path->slots[0], 1545 extent = btrfs_item_ptr(leaf, path->slots[0],
1545 struct btrfs_dev_extent); 1546 struct btrfs_dev_extent);
1546 } else { 1547 } else {
1547 btrfs_handle_fs_error(root->fs_info, ret, "Slot search failed"); 1548 btrfs_handle_fs_error(fs_info, ret, "Slot search failed");
1548 goto out; 1549 goto out;
1549 } 1550 }
1550 1551
@@ -1552,8 +1553,8 @@ again:
1552 1553
1553 ret = btrfs_del_item(trans, root, path); 1554 ret = btrfs_del_item(trans, root, path);
1554 if (ret) { 1555 if (ret) {
1555 btrfs_handle_fs_error(root->fs_info, ret, 1556 btrfs_handle_fs_error(fs_info, ret,
1556 "Failed to remove dev extent item"); 1557 "Failed to remove dev extent item");
1557 } else { 1558 } else {
1558 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags); 1559 set_bit(BTRFS_TRANS_HAVE_FREE_BGS, &trans->transaction->flags);
1559 } 1560 }
@@ -1569,7 +1570,8 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1569{ 1570{
1570 int ret; 1571 int ret;
1571 struct btrfs_path *path; 1572 struct btrfs_path *path;
1572 struct btrfs_root *root = device->fs_info->dev_root; 1573 struct btrfs_fs_info *fs_info = device->fs_info;
1574 struct btrfs_root *root = fs_info->dev_root;
1573 struct btrfs_dev_extent *extent; 1575 struct btrfs_dev_extent *extent;
1574 struct extent_buffer *leaf; 1576 struct extent_buffer *leaf;
1575 struct btrfs_key key; 1577 struct btrfs_key key;
@@ -1595,8 +1597,7 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
1595 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid); 1597 btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
1596 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset); 1598 btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
1597 1599
1598 write_extent_buffer_chunk_tree_uuid(leaf, 1600 write_extent_buffer_chunk_tree_uuid(leaf, fs_info->chunk_tree_uuid);
1599 root->fs_info->chunk_tree_uuid);
1600 1601
1601 btrfs_set_dev_extent_length(leaf, extent, num_bytes); 1602 btrfs_set_dev_extent_length(leaf, extent, num_bytes);
1602 btrfs_mark_buffer_dirty(leaf); 1603 btrfs_mark_buffer_dirty(leaf);
@@ -1712,7 +1713,7 @@ static int btrfs_add_device(struct btrfs_trans_handle *trans,
1712 ptr = btrfs_device_uuid(dev_item); 1713 ptr = btrfs_device_uuid(dev_item);
1713 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE); 1714 write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
1714 ptr = btrfs_device_fsid(dev_item); 1715 ptr = btrfs_device_fsid(dev_item);
1715 write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE); 1716 write_extent_buffer(leaf, fs_info->fsid, ptr, BTRFS_UUID_SIZE);
1716 btrfs_mark_buffer_dirty(leaf); 1717 btrfs_mark_buffer_dirty(leaf);
1717 1718
1718 ret = 0; 1719 ret = 0;
@@ -1853,6 +1854,7 @@ void btrfs_assign_next_active_device(struct btrfs_fs_info *fs_info,
1853 1854
1854int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid) 1855int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1855{ 1856{
1857 struct btrfs_fs_info *fs_info = root->fs_info;
1856 struct btrfs_device *device; 1858 struct btrfs_device *device;
1857 struct btrfs_fs_devices *cur_devices; 1859 struct btrfs_fs_devices *cur_devices;
1858 u64 num_devices; 1860 u64 num_devices;
@@ -1861,15 +1863,15 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1861 1863
1862 mutex_lock(&uuid_mutex); 1864 mutex_lock(&uuid_mutex);
1863 1865
1864 num_devices = root->fs_info->fs_devices->num_devices; 1866 num_devices = fs_info->fs_devices->num_devices;
1865 btrfs_dev_replace_lock(&root->fs_info->dev_replace, 0); 1867 btrfs_dev_replace_lock(&fs_info->dev_replace, 0);
1866 if (btrfs_dev_replace_is_ongoing(&root->fs_info->dev_replace)) { 1868 if (btrfs_dev_replace_is_ongoing(&fs_info->dev_replace)) {
1867 WARN_ON(num_devices < 1); 1869 WARN_ON(num_devices < 1);
1868 num_devices--; 1870 num_devices--;
1869 } 1871 }
1870 btrfs_dev_replace_unlock(&root->fs_info->dev_replace, 0); 1872 btrfs_dev_replace_unlock(&fs_info->dev_replace, 0);
1871 1873
1872 ret = btrfs_check_raid_min_devices(root->fs_info, num_devices - 1); 1874 ret = btrfs_check_raid_min_devices(fs_info, num_devices - 1);
1873 if (ret) 1875 if (ret)
1874 goto out; 1876 goto out;
1875 1877
@@ -1883,16 +1885,16 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1883 goto out; 1885 goto out;
1884 } 1886 }
1885 1887
1886 if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) { 1888 if (device->writeable && fs_info->fs_devices->rw_devices == 1) {
1887 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE; 1889 ret = BTRFS_ERROR_DEV_ONLY_WRITABLE;
1888 goto out; 1890 goto out;
1889 } 1891 }
1890 1892
1891 if (device->writeable) { 1893 if (device->writeable) {
1892 lock_chunks(root->fs_info); 1894 lock_chunks(fs_info);
1893 list_del_init(&device->dev_alloc_list); 1895 list_del_init(&device->dev_alloc_list);
1894 device->fs_devices->rw_devices--; 1896 device->fs_devices->rw_devices--;
1895 unlock_chunks(root->fs_info); 1897 unlock_chunks(fs_info);
1896 clear_super = true; 1898 clear_super = true;
1897 } 1899 }
1898 1900
@@ -1907,12 +1909,12 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1907 * counter although write_all_supers() is not locked out. This 1909 * counter although write_all_supers() is not locked out. This
1908 * could give a filesystem state which requires a degraded mount. 1910 * could give a filesystem state which requires a degraded mount.
1909 */ 1911 */
1910 ret = btrfs_rm_dev_item(root->fs_info, device); 1912 ret = btrfs_rm_dev_item(fs_info, device);
1911 if (ret) 1913 if (ret)
1912 goto error_undo; 1914 goto error_undo;
1913 1915
1914 device->in_fs_metadata = 0; 1916 device->in_fs_metadata = 0;
1915 btrfs_scrub_cancel_dev(root->fs_info, device); 1917 btrfs_scrub_cancel_dev(fs_info, device);
1916 1918
1917 /* 1919 /*
1918 * the device list mutex makes sure that we don't change 1920 * the device list mutex makes sure that we don't change
@@ -1925,7 +1927,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1925 */ 1927 */
1926 1928
1927 cur_devices = device->fs_devices; 1929 cur_devices = device->fs_devices;
1928 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 1930 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1929 list_del_rcu(&device->dev_list); 1931 list_del_rcu(&device->dev_list);
1930 1932
1931 device->fs_devices->num_devices--; 1933 device->fs_devices->num_devices--;
@@ -1934,17 +1936,17 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1934 if (device->missing) 1936 if (device->missing)
1935 device->fs_devices->missing_devices--; 1937 device->fs_devices->missing_devices--;
1936 1938
1937 btrfs_assign_next_active_device(root->fs_info, device, NULL); 1939 btrfs_assign_next_active_device(fs_info, device, NULL);
1938 1940
1939 if (device->bdev) { 1941 if (device->bdev) {
1940 device->fs_devices->open_devices--; 1942 device->fs_devices->open_devices--;
1941 /* remove sysfs entry */ 1943 /* remove sysfs entry */
1942 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); 1944 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
1943 } 1945 }
1944 1946
1945 num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1; 1947 num_devices = btrfs_super_num_devices(fs_info->super_copy) - 1;
1946 btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices); 1948 btrfs_set_super_num_devices(fs_info->super_copy, num_devices);
1947 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 1949 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
1948 1950
1949 /* 1951 /*
1950 * at this point, the device is zero sized and detached from 1952 * at this point, the device is zero sized and detached from
@@ -1959,7 +1961,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1959 1961
1960 if (cur_devices->open_devices == 0) { 1962 if (cur_devices->open_devices == 0) {
1961 struct btrfs_fs_devices *fs_devices; 1963 struct btrfs_fs_devices *fs_devices;
1962 fs_devices = root->fs_info->fs_devices; 1964 fs_devices = fs_info->fs_devices;
1963 while (fs_devices) { 1965 while (fs_devices) {
1964 if (fs_devices->seed == cur_devices) { 1966 if (fs_devices->seed == cur_devices) {
1965 fs_devices->seed = cur_devices->seed; 1967 fs_devices->seed = cur_devices->seed;
@@ -1972,8 +1974,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path, u64 devid)
1972 free_fs_devices(cur_devices); 1974 free_fs_devices(cur_devices);
1973 } 1975 }
1974 1976
1975 root->fs_info->num_tolerated_disk_barrier_failures = 1977 fs_info->num_tolerated_disk_barrier_failures =
1976 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); 1978 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
1977 1979
1978out: 1980out:
1979 mutex_unlock(&uuid_mutex); 1981 mutex_unlock(&uuid_mutex);
@@ -1981,11 +1983,11 @@ out:
1981 1983
1982error_undo: 1984error_undo:
1983 if (device->writeable) { 1985 if (device->writeable) {
1984 lock_chunks(root->fs_info); 1986 lock_chunks(fs_info);
1985 list_add(&device->dev_alloc_list, 1987 list_add(&device->dev_alloc_list,
1986 &root->fs_info->fs_devices->alloc_list); 1988 &fs_info->fs_devices->alloc_list);
1987 device->fs_devices->rw_devices++; 1989 device->fs_devices->rw_devices++;
1988 unlock_chunks(root->fs_info); 1990 unlock_chunks(fs_info);
1989 } 1991 }
1990 goto out; 1992 goto out;
1991} 1993}
@@ -2093,6 +2095,7 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
2093static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path, 2095static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2094 struct btrfs_device **device) 2096 struct btrfs_device **device)
2095{ 2097{
2098 struct btrfs_fs_info *fs_info = root->fs_info;
2096 int ret = 0; 2099 int ret = 0;
2097 struct btrfs_super_block *disk_super; 2100 struct btrfs_super_block *disk_super;
2098 u64 devid; 2101 u64 devid;
@@ -2102,14 +2105,13 @@ static int btrfs_find_device_by_path(struct btrfs_root *root, char *device_path,
2102 2105
2103 *device = NULL; 2106 *device = NULL;
2104 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ, 2107 ret = btrfs_get_bdev_and_sb(device_path, FMODE_READ,
2105 root->fs_info->bdev_holder, 0, &bdev, &bh); 2108 fs_info->bdev_holder, 0, &bdev, &bh);
2106 if (ret) 2109 if (ret)
2107 return ret; 2110 return ret;
2108 disk_super = (struct btrfs_super_block *)bh->b_data; 2111 disk_super = (struct btrfs_super_block *)bh->b_data;
2109 devid = btrfs_stack_device_id(&disk_super->dev_item); 2112 devid = btrfs_stack_device_id(&disk_super->dev_item);
2110 dev_uuid = disk_super->dev_item.uuid; 2113 dev_uuid = disk_super->dev_item.uuid;
2111 *device = btrfs_find_device(root->fs_info, devid, dev_uuid, 2114 *device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2112 disk_super->fsid);
2113 brelse(bh); 2115 brelse(bh);
2114 if (!*device) 2116 if (!*device)
2115 ret = -ENOENT; 2117 ret = -ENOENT;
@@ -2121,12 +2123,14 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2121 char *device_path, 2123 char *device_path,
2122 struct btrfs_device **device) 2124 struct btrfs_device **device)
2123{ 2125{
2126 struct btrfs_fs_info *fs_info = root->fs_info;
2127
2124 *device = NULL; 2128 *device = NULL;
2125 if (strcmp(device_path, "missing") == 0) { 2129 if (strcmp(device_path, "missing") == 0) {
2126 struct list_head *devices; 2130 struct list_head *devices;
2127 struct btrfs_device *tmp; 2131 struct btrfs_device *tmp;
2128 2132
2129 devices = &root->fs_info->fs_devices->devices; 2133 devices = &fs_info->fs_devices->devices;
2130 /* 2134 /*
2131 * It is safe to read the devices since the volume_mutex 2135 * It is safe to read the devices since the volume_mutex
2132 * is held by the caller. 2136 * is held by the caller.
@@ -2154,12 +2158,12 @@ int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid,
2154 char *devpath, 2158 char *devpath,
2155 struct btrfs_device **device) 2159 struct btrfs_device **device)
2156{ 2160{
2161 struct btrfs_fs_info *fs_info = root->fs_info;
2157 int ret; 2162 int ret;
2158 2163
2159 if (devid) { 2164 if (devid) {
2160 ret = 0; 2165 ret = 0;
2161 *device = btrfs_find_device(root->fs_info, devid, NULL, 2166 *device = btrfs_find_device(fs_info, devid, NULL, NULL);
2162 NULL);
2163 if (!*device) 2167 if (!*device)
2164 ret = -ENOENT; 2168 ret = -ENOENT;
2165 } else { 2169 } else {
@@ -2177,10 +2181,11 @@ int btrfs_find_device_by_devspec(struct btrfs_root *root, u64 devid,
2177 */ 2181 */
2178static int btrfs_prepare_sprout(struct btrfs_root *root) 2182static int btrfs_prepare_sprout(struct btrfs_root *root)
2179{ 2183{
2180 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2184 struct btrfs_fs_info *fs_info = root->fs_info;
2185 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2181 struct btrfs_fs_devices *old_devices; 2186 struct btrfs_fs_devices *old_devices;
2182 struct btrfs_fs_devices *seed_devices; 2187 struct btrfs_fs_devices *seed_devices;
2183 struct btrfs_super_block *disk_super = root->fs_info->super_copy; 2188 struct btrfs_super_block *disk_super = fs_info->super_copy;
2184 struct btrfs_device *device; 2189 struct btrfs_device *device;
2185 u64 super_flags; 2190 u64 super_flags;
2186 2191
@@ -2206,15 +2211,15 @@ static int btrfs_prepare_sprout(struct btrfs_root *root)
2206 INIT_LIST_HEAD(&seed_devices->alloc_list); 2211 INIT_LIST_HEAD(&seed_devices->alloc_list);
2207 mutex_init(&seed_devices->device_list_mutex); 2212 mutex_init(&seed_devices->device_list_mutex);
2208 2213
2209 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2214 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2210 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices, 2215 list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
2211 synchronize_rcu); 2216 synchronize_rcu);
2212 list_for_each_entry(device, &seed_devices->devices, dev_list) 2217 list_for_each_entry(device, &seed_devices->devices, dev_list)
2213 device->fs_devices = seed_devices; 2218 device->fs_devices = seed_devices;
2214 2219
2215 lock_chunks(root->fs_info); 2220 lock_chunks(fs_info);
2216 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list); 2221 list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
2217 unlock_chunks(root->fs_info); 2222 unlock_chunks(fs_info);
2218 2223
2219 fs_devices->seeding = 0; 2224 fs_devices->seeding = 0;
2220 fs_devices->num_devices = 0; 2225 fs_devices->num_devices = 0;
@@ -2224,9 +2229,9 @@ static int btrfs_prepare_sprout(struct btrfs_root *root)
2224 fs_devices->seed = seed_devices; 2229 fs_devices->seed = seed_devices;
2225 2230
2226 generate_random_uuid(fs_devices->fsid); 2231 generate_random_uuid(fs_devices->fsid);
2227 memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); 2232 memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2228 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE); 2233 memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2229 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 2234 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2230 2235
2231 super_flags = btrfs_super_flags(disk_super) & 2236 super_flags = btrfs_super_flags(disk_super) &
2232 ~BTRFS_SUPER_FLAG_SEEDING; 2237 ~BTRFS_SUPER_FLAG_SEEDING;
@@ -2291,8 +2296,7 @@ next_slot:
2291 BTRFS_UUID_SIZE); 2296 BTRFS_UUID_SIZE);
2292 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), 2297 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
2293 BTRFS_UUID_SIZE); 2298 BTRFS_UUID_SIZE);
2294 device = btrfs_find_device(root->fs_info, devid, dev_uuid, 2299 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
2295 fs_uuid);
2296 BUG_ON(!device); /* Logic error */ 2300 BUG_ON(!device); /* Logic error */
2297 2301
2298 if (device->fs_devices->seeding) { 2302 if (device->fs_devices->seeding) {
@@ -2318,21 +2322,21 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2318 struct btrfs_device *device; 2322 struct btrfs_device *device;
2319 struct block_device *bdev; 2323 struct block_device *bdev;
2320 struct list_head *devices; 2324 struct list_head *devices;
2321 struct super_block *sb = root->fs_info->sb; 2325 struct super_block *sb = fs_info->sb;
2322 struct rcu_string *name; 2326 struct rcu_string *name;
2323 u64 tmp; 2327 u64 tmp;
2324 int seeding_dev = 0; 2328 int seeding_dev = 0;
2325 int ret = 0; 2329 int ret = 0;
2326 2330
2327 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) 2331 if ((sb->s_flags & MS_RDONLY) && !fs_info->fs_devices->seeding)
2328 return -EROFS; 2332 return -EROFS;
2329 2333
2330 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL, 2334 bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
2331 root->fs_info->bdev_holder); 2335 fs_info->bdev_holder);
2332 if (IS_ERR(bdev)) 2336 if (IS_ERR(bdev))
2333 return PTR_ERR(bdev); 2337 return PTR_ERR(bdev);
2334 2338
2335 if (root->fs_info->fs_devices->seeding) { 2339 if (fs_info->fs_devices->seeding) {
2336 seeding_dev = 1; 2340 seeding_dev = 1;
2337 down_write(&sb->s_umount); 2341 down_write(&sb->s_umount);
2338 mutex_lock(&uuid_mutex); 2342 mutex_lock(&uuid_mutex);
@@ -2340,20 +2344,20 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2340 2344
2341 filemap_write_and_wait(bdev->bd_inode->i_mapping); 2345 filemap_write_and_wait(bdev->bd_inode->i_mapping);
2342 2346
2343 devices = &root->fs_info->fs_devices->devices; 2347 devices = &fs_info->fs_devices->devices;
2344 2348
2345 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2349 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2346 list_for_each_entry(device, devices, dev_list) { 2350 list_for_each_entry(device, devices, dev_list) {
2347 if (device->bdev == bdev) { 2351 if (device->bdev == bdev) {
2348 ret = -EEXIST; 2352 ret = -EEXIST;
2349 mutex_unlock( 2353 mutex_unlock(
2350 &root->fs_info->fs_devices->device_list_mutex); 2354 &fs_info->fs_devices->device_list_mutex);
2351 goto error; 2355 goto error;
2352 } 2356 }
2353 } 2357 }
2354 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 2358 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2355 2359
2356 device = btrfs_alloc_device(root->fs_info, NULL, NULL); 2360 device = btrfs_alloc_device(fs_info, NULL, NULL);
2357 if (IS_ERR(device)) { 2361 if (IS_ERR(device)) {
2358 /* we can safely leave the fs_devices entry around */ 2362 /* we can safely leave the fs_devices entry around */
2359 ret = PTR_ERR(device); 2363 ret = PTR_ERR(device);
@@ -2381,9 +2385,9 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2381 device->can_discard = 1; 2385 device->can_discard = 1;
2382 device->writeable = 1; 2386 device->writeable = 1;
2383 device->generation = trans->transid; 2387 device->generation = trans->transid;
2384 device->io_width = root->fs_info->sectorsize; 2388 device->io_width = fs_info->sectorsize;
2385 device->io_align = root->fs_info->sectorsize; 2389 device->io_align = fs_info->sectorsize;
2386 device->sector_size = root->fs_info->sectorsize; 2390 device->sector_size = fs_info->sectorsize;
2387 device->total_bytes = i_size_read(bdev->bd_inode); 2391 device->total_bytes = i_size_read(bdev->bd_inode);
2388 device->disk_total_bytes = device->total_bytes; 2392 device->disk_total_bytes = device->total_bytes;
2389 device->commit_total_bytes = device->total_bytes; 2393 device->commit_total_bytes = device->total_bytes;
@@ -2401,57 +2405,56 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2401 BUG_ON(ret); /* -ENOMEM */ 2405 BUG_ON(ret); /* -ENOMEM */
2402 } 2406 }
2403 2407
2404 device->fs_devices = root->fs_info->fs_devices; 2408 device->fs_devices = fs_info->fs_devices;
2405 2409
2406 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2410 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2407 lock_chunks(root->fs_info); 2411 lock_chunks(fs_info);
2408 list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices); 2412 list_add_rcu(&device->dev_list, &fs_info->fs_devices->devices);
2409 list_add(&device->dev_alloc_list, 2413 list_add(&device->dev_alloc_list,
2410 &root->fs_info->fs_devices->alloc_list); 2414 &fs_info->fs_devices->alloc_list);
2411 root->fs_info->fs_devices->num_devices++; 2415 fs_info->fs_devices->num_devices++;
2412 root->fs_info->fs_devices->open_devices++; 2416 fs_info->fs_devices->open_devices++;
2413 root->fs_info->fs_devices->rw_devices++; 2417 fs_info->fs_devices->rw_devices++;
2414 root->fs_info->fs_devices->total_devices++; 2418 fs_info->fs_devices->total_devices++;
2415 root->fs_info->fs_devices->total_rw_bytes += device->total_bytes; 2419 fs_info->fs_devices->total_rw_bytes += device->total_bytes;
2416 2420
2417 spin_lock(&root->fs_info->free_chunk_lock); 2421 spin_lock(&fs_info->free_chunk_lock);
2418 root->fs_info->free_chunk_space += device->total_bytes; 2422 fs_info->free_chunk_space += device->total_bytes;
2419 spin_unlock(&root->fs_info->free_chunk_lock); 2423 spin_unlock(&fs_info->free_chunk_lock);
2420 2424
2421 if (!blk_queue_nonrot(bdev_get_queue(bdev))) 2425 if (!blk_queue_nonrot(bdev_get_queue(bdev)))
2422 root->fs_info->fs_devices->rotating = 1; 2426 fs_info->fs_devices->rotating = 1;
2423 2427
2424 tmp = btrfs_super_total_bytes(root->fs_info->super_copy); 2428 tmp = btrfs_super_total_bytes(fs_info->super_copy);
2425 btrfs_set_super_total_bytes(root->fs_info->super_copy, 2429 btrfs_set_super_total_bytes(fs_info->super_copy,
2426 tmp + device->total_bytes); 2430 tmp + device->total_bytes);
2427 2431
2428 tmp = btrfs_super_num_devices(root->fs_info->super_copy); 2432 tmp = btrfs_super_num_devices(fs_info->super_copy);
2429 btrfs_set_super_num_devices(root->fs_info->super_copy, 2433 btrfs_set_super_num_devices(fs_info->super_copy, tmp + 1);
2430 tmp + 1);
2431 2434
2432 /* add sysfs device entry */ 2435 /* add sysfs device entry */
2433 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device); 2436 btrfs_sysfs_add_device_link(fs_info->fs_devices, device);
2434 2437
2435 /* 2438 /*
2436 * we've got more storage, clear any full flags on the space 2439 * we've got more storage, clear any full flags on the space
2437 * infos 2440 * infos
2438 */ 2441 */
2439 btrfs_clear_space_info_full(root->fs_info); 2442 btrfs_clear_space_info_full(fs_info);
2440 2443
2441 unlock_chunks(root->fs_info); 2444 unlock_chunks(fs_info);
2442 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 2445 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2443 2446
2444 if (seeding_dev) { 2447 if (seeding_dev) {
2445 lock_chunks(root->fs_info); 2448 lock_chunks(fs_info);
2446 ret = init_first_rw_device(trans, root, device); 2449 ret = init_first_rw_device(trans, root, device);
2447 unlock_chunks(root->fs_info); 2450 unlock_chunks(fs_info);
2448 if (ret) { 2451 if (ret) {
2449 btrfs_abort_transaction(trans, ret); 2452 btrfs_abort_transaction(trans, ret);
2450 goto error_trans; 2453 goto error_trans;
2451 } 2454 }
2452 } 2455 }
2453 2456
2454 ret = btrfs_add_device(trans, root->fs_info, device); 2457 ret = btrfs_add_device(trans, fs_info, device);
2455 if (ret) { 2458 if (ret) {
2456 btrfs_abort_transaction(trans, ret); 2459 btrfs_abort_transaction(trans, ret);
2457 goto error_trans; 2460 goto error_trans;
@@ -2460,7 +2463,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2460 if (seeding_dev) { 2463 if (seeding_dev) {
2461 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE]; 2464 char fsid_buf[BTRFS_UUID_UNPARSED_SIZE];
2462 2465
2463 ret = btrfs_finish_sprout(trans, root->fs_info); 2466 ret = btrfs_finish_sprout(trans, fs_info);
2464 if (ret) { 2467 if (ret) {
2465 btrfs_abort_transaction(trans, ret); 2468 btrfs_abort_transaction(trans, ret);
2466 goto error_trans; 2469 goto error_trans;
@@ -2470,15 +2473,14 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2470 * so rename the fsid on the sysfs 2473 * so rename the fsid on the sysfs
2471 */ 2474 */
2472 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", 2475 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2473 root->fs_info->fsid); 2476 fs_info->fsid);
2474 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj, 2477 if (kobject_rename(&fs_info->fs_devices->fsid_kobj, fsid_buf))
2475 fsid_buf)) 2478 btrfs_warn(fs_info,
2476 btrfs_warn(root->fs_info, 2479 "sysfs: failed to create fsid for sprout");
2477 "sysfs: failed to create fsid for sprout");
2478 } 2480 }
2479 2481
2480 root->fs_info->num_tolerated_disk_barrier_failures = 2482 fs_info->num_tolerated_disk_barrier_failures =
2481 btrfs_calc_num_tolerated_disk_barrier_failures(root->fs_info); 2483 btrfs_calc_num_tolerated_disk_barrier_failures(fs_info);
2482 ret = btrfs_commit_transaction(trans, root); 2484 ret = btrfs_commit_transaction(trans, root);
2483 2485
2484 if (seeding_dev) { 2486 if (seeding_dev) {
@@ -2490,7 +2492,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2490 2492
2491 ret = btrfs_relocate_sys_chunks(root); 2493 ret = btrfs_relocate_sys_chunks(root);
2492 if (ret < 0) 2494 if (ret < 0)
2493 btrfs_handle_fs_error(root->fs_info, ret, 2495 btrfs_handle_fs_error(fs_info, ret,
2494 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command."); 2496 "Failed to relocate sys chunks after device initialization. This can be fixed using the \"btrfs balance\" command.");
2495 trans = btrfs_attach_transaction(root); 2497 trans = btrfs_attach_transaction(root);
2496 if (IS_ERR(trans)) { 2498 if (IS_ERR(trans)) {
@@ -2508,7 +2510,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, char *device_path)
2508error_trans: 2510error_trans:
2509 btrfs_end_transaction(trans, root); 2511 btrfs_end_transaction(trans, root);
2510 rcu_string_free(device->name); 2512 rcu_string_free(device->name);
2511 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device); 2513 btrfs_sysfs_rm_device_link(fs_info->fs_devices, device);
2512 kfree(device); 2514 kfree(device);
2513error: 2515error:
2514 blkdev_put(bdev, FMODE_EXCL); 2516 blkdev_put(bdev, FMODE_EXCL);
@@ -2584,12 +2586,12 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2584 q = bdev_get_queue(bdev); 2586 q = bdev_get_queue(bdev);
2585 if (blk_queue_discard(q)) 2587 if (blk_queue_discard(q))
2586 device->can_discard = 1; 2588 device->can_discard = 1;
2587 mutex_lock(&root->fs_info->fs_devices->device_list_mutex); 2589 mutex_lock(&fs_info->fs_devices->device_list_mutex);
2588 device->writeable = 1; 2590 device->writeable = 1;
2589 device->generation = 0; 2591 device->generation = 0;
2590 device->io_width = root->fs_info->sectorsize; 2592 device->io_width = fs_info->sectorsize;
2591 device->io_align = root->fs_info->sectorsize; 2593 device->io_align = fs_info->sectorsize;
2592 device->sector_size = root->fs_info->sectorsize; 2594 device->sector_size = fs_info->sectorsize;
2593 device->total_bytes = btrfs_device_get_total_bytes(srcdev); 2595 device->total_bytes = btrfs_device_get_total_bytes(srcdev);
2594 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev); 2596 device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
2595 device->bytes_used = btrfs_device_get_bytes_used(srcdev); 2597 device->bytes_used = btrfs_device_get_bytes_used(srcdev);
@@ -2607,7 +2609,7 @@ int btrfs_init_dev_replace_tgtdev(struct btrfs_root *root, char *device_path,
2607 list_add(&device->dev_list, &fs_info->fs_devices->devices); 2609 list_add(&device->dev_list, &fs_info->fs_devices->devices);
2608 fs_info->fs_devices->num_devices++; 2610 fs_info->fs_devices->num_devices++;
2609 fs_info->fs_devices->open_devices++; 2611 fs_info->fs_devices->open_devices++;
2610 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 2612 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
2611 2613
2612 *device_out = device; 2614 *device_out = device;
2613 return ret; 2615 return ret;
@@ -2635,13 +2637,11 @@ static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
2635{ 2637{
2636 int ret; 2638 int ret;
2637 struct btrfs_path *path; 2639 struct btrfs_path *path;
2638 struct btrfs_root *root; 2640 struct btrfs_root *root = device->fs_info->chunk_root;
2639 struct btrfs_dev_item *dev_item; 2641 struct btrfs_dev_item *dev_item;
2640 struct extent_buffer *leaf; 2642 struct extent_buffer *leaf;
2641 struct btrfs_key key; 2643 struct btrfs_key key;
2642 2644
2643 root = device->fs_info->chunk_root;
2644
2645 path = btrfs_alloc_path(); 2645 path = btrfs_alloc_path();
2646 if (!path) 2646 if (!path)
2647 return -ENOMEM; 2647 return -ENOMEM;
@@ -2681,7 +2681,8 @@ out:
2681int btrfs_grow_device(struct btrfs_trans_handle *trans, 2681int btrfs_grow_device(struct btrfs_trans_handle *trans,
2682 struct btrfs_device *device, u64 new_size) 2682 struct btrfs_device *device, u64 new_size)
2683{ 2683{
2684 struct btrfs_super_block *super_copy = device->fs_info->super_copy; 2684 struct btrfs_fs_info *fs_info = device->fs_info;
2685 struct btrfs_super_block *super_copy = fs_info->super_copy;
2685 struct btrfs_fs_devices *fs_devices; 2686 struct btrfs_fs_devices *fs_devices;
2686 u64 old_total; 2687 u64 old_total;
2687 u64 diff; 2688 u64 diff;
@@ -2689,17 +2690,17 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans,
2689 if (!device->writeable) 2690 if (!device->writeable)
2690 return -EACCES; 2691 return -EACCES;
2691 2692
2692 lock_chunks(device->fs_info); 2693 lock_chunks(fs_info);
2693 old_total = btrfs_super_total_bytes(super_copy); 2694 old_total = btrfs_super_total_bytes(super_copy);
2694 diff = new_size - device->total_bytes; 2695 diff = new_size - device->total_bytes;
2695 2696
2696 if (new_size <= device->total_bytes || 2697 if (new_size <= device->total_bytes ||
2697 device->is_tgtdev_for_dev_replace) { 2698 device->is_tgtdev_for_dev_replace) {
2698 unlock_chunks(device->fs_info); 2699 unlock_chunks(fs_info);
2699 return -EINVAL; 2700 return -EINVAL;
2700 } 2701 }
2701 2702
2702 fs_devices = device->fs_info->fs_devices; 2703 fs_devices = fs_info->fs_devices;
2703 2704
2704 btrfs_set_super_total_bytes(super_copy, old_total + diff); 2705 btrfs_set_super_total_bytes(super_copy, old_total + diff);
2705 device->fs_devices->total_rw_bytes += diff; 2706 device->fs_devices->total_rw_bytes += diff;
@@ -2710,7 +2711,7 @@ int btrfs_grow_device(struct btrfs_trans_handle *trans,
2710 if (list_empty(&device->resized_list)) 2711 if (list_empty(&device->resized_list))
2711 list_add_tail(&device->resized_list, 2712 list_add_tail(&device->resized_list,
2712 &fs_devices->resized_devices); 2713 &fs_devices->resized_devices);
2713 unlock_chunks(device->fs_info); 2714 unlock_chunks(fs_info);
2714 2715
2715 return btrfs_update_device(trans, device); 2716 return btrfs_update_device(trans, device);
2716} 2717}
@@ -2736,16 +2737,16 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2736 if (ret < 0) 2737 if (ret < 0)
2737 goto out; 2738 goto out;
2738 else if (ret > 0) { /* Logic error or corruption */ 2739 else if (ret > 0) { /* Logic error or corruption */
2739 btrfs_handle_fs_error(root->fs_info, -ENOENT, 2740 btrfs_handle_fs_error(fs_info, -ENOENT,
2740 "Failed lookup while freeing chunk."); 2741 "Failed lookup while freeing chunk.");
2741 ret = -ENOENT; 2742 ret = -ENOENT;
2742 goto out; 2743 goto out;
2743 } 2744 }
2744 2745
2745 ret = btrfs_del_item(trans, root, path); 2746 ret = btrfs_del_item(trans, root, path);
2746 if (ret < 0) 2747 if (ret < 0)
2747 btrfs_handle_fs_error(root->fs_info, ret, 2748 btrfs_handle_fs_error(fs_info, ret,
2748 "Failed to delete chunk item."); 2749 "Failed to delete chunk item.");
2749out: 2750out:
2750 btrfs_free_path(path); 2751 btrfs_free_path(path);
2751 return ret; 2752 return ret;
@@ -2754,8 +2755,7 @@ out:
2754static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info, 2755static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info,
2755 u64 chunk_objectid, u64 chunk_offset) 2756 u64 chunk_objectid, u64 chunk_offset)
2756{ 2757{
2757 struct btrfs_root *root = fs_info->chunk_root; 2758 struct btrfs_super_block *super_copy = fs_info->super_copy;
2758 struct btrfs_super_block *super_copy = root->fs_info->super_copy;
2759 struct btrfs_disk_key *disk_key; 2759 struct btrfs_disk_key *disk_key;
2760 struct btrfs_chunk *chunk; 2760 struct btrfs_chunk *chunk;
2761 u8 *ptr; 2761 u8 *ptr;
@@ -2766,7 +2766,7 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info,
2766 u32 cur; 2766 u32 cur;
2767 struct btrfs_key key; 2767 struct btrfs_key key;
2768 2768
2769 lock_chunks(root->fs_info); 2769 lock_chunks(fs_info);
2770 array_size = btrfs_super_sys_array_size(super_copy); 2770 array_size = btrfs_super_sys_array_size(super_copy);
2771 2771
2772 ptr = super_copy->sys_chunk_array; 2772 ptr = super_copy->sys_chunk_array;
@@ -2796,22 +2796,21 @@ static int btrfs_del_sys_chunk(struct btrfs_fs_info *fs_info,
2796 cur += len; 2796 cur += len;
2797 } 2797 }
2798 } 2798 }
2799 unlock_chunks(root->fs_info); 2799 unlock_chunks(fs_info);
2800 return ret; 2800 return ret;
2801} 2801}
2802 2802
2803int btrfs_remove_chunk(struct btrfs_trans_handle *trans, 2803int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2804 struct btrfs_fs_info *fs_info, u64 chunk_offset) 2804 struct btrfs_fs_info *fs_info, u64 chunk_offset)
2805{ 2805{
2806 struct btrfs_root *root = fs_info->chunk_root;
2807 struct extent_map_tree *em_tree; 2806 struct extent_map_tree *em_tree;
2808 struct extent_map *em; 2807 struct extent_map *em;
2809 struct btrfs_root *extent_root = root->fs_info->extent_root; 2808 struct btrfs_root *extent_root = fs_info->extent_root;
2810 struct map_lookup *map; 2809 struct map_lookup *map;
2811 u64 dev_extent_len = 0; 2810 u64 dev_extent_len = 0;
2812 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; 2811 u64 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
2813 int i, ret = 0; 2812 int i, ret = 0;
2814 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 2813 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
2815 2814
2816 em_tree = &fs_info->mapping_tree.map_tree; 2815 em_tree = &fs_info->mapping_tree.map_tree;
2817 2816
@@ -2854,14 +2853,14 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2854 } 2853 }
2855 2854
2856 if (device->bytes_used > 0) { 2855 if (device->bytes_used > 0) {
2857 lock_chunks(root->fs_info); 2856 lock_chunks(fs_info);
2858 btrfs_device_set_bytes_used(device, 2857 btrfs_device_set_bytes_used(device,
2859 device->bytes_used - dev_extent_len); 2858 device->bytes_used - dev_extent_len);
2860 spin_lock(&root->fs_info->free_chunk_lock); 2859 spin_lock(&fs_info->free_chunk_lock);
2861 root->fs_info->free_chunk_space += dev_extent_len; 2860 fs_info->free_chunk_space += dev_extent_len;
2862 spin_unlock(&root->fs_info->free_chunk_lock); 2861 spin_unlock(&fs_info->free_chunk_lock);
2863 btrfs_clear_space_info_full(root->fs_info); 2862 btrfs_clear_space_info_full(fs_info);
2864 unlock_chunks(root->fs_info); 2863 unlock_chunks(fs_info);
2865 } 2864 }
2866 2865
2867 if (map->stripes[i].dev) { 2866 if (map->stripes[i].dev) {
@@ -2875,8 +2874,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2875 } 2874 }
2876 mutex_unlock(&fs_devices->device_list_mutex); 2875 mutex_unlock(&fs_devices->device_list_mutex);
2877 2876
2878 ret = btrfs_free_chunk(trans, root->fs_info, chunk_objectid, 2877 ret = btrfs_free_chunk(trans, fs_info, chunk_objectid, chunk_offset);
2879 chunk_offset);
2880 if (ret) { 2878 if (ret) {
2881 btrfs_abort_transaction(trans, ret); 2879 btrfs_abort_transaction(trans, ret);
2882 goto out; 2880 goto out;
@@ -2924,15 +2922,15 @@ static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2924 * we release the path used to search the chunk/dev tree and before 2922 * we release the path used to search the chunk/dev tree and before
2925 * the current task acquires this mutex and calls us. 2923 * the current task acquires this mutex and calls us.
2926 */ 2924 */
2927 ASSERT(mutex_is_locked(&root->fs_info->delete_unused_bgs_mutex)); 2925 ASSERT(mutex_is_locked(&fs_info->delete_unused_bgs_mutex));
2928 2926
2929 ret = btrfs_can_relocate(root->fs_info, chunk_offset); 2927 ret = btrfs_can_relocate(fs_info, chunk_offset);
2930 if (ret) 2928 if (ret)
2931 return -ENOSPC; 2929 return -ENOSPC;
2932 2930
2933 /* step one, relocate all the extents inside this chunk */ 2931 /* step one, relocate all the extents inside this chunk */
2934 btrfs_scrub_pause(root); 2932 btrfs_scrub_pause(root);
2935 ret = btrfs_relocate_block_group(root->fs_info, chunk_offset); 2933 ret = btrfs_relocate_block_group(fs_info, chunk_offset);
2936 btrfs_scrub_continue(root); 2934 btrfs_scrub_continue(root);
2937 if (ret) 2935 if (ret)
2938 return ret; 2936 return ret;
@@ -2956,7 +2954,8 @@ static int btrfs_relocate_chunk(struct btrfs_fs_info *fs_info, u64 chunk_offset)
2956 2954
2957static int btrfs_relocate_sys_chunks(struct btrfs_root *root) 2955static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
2958{ 2956{
2959 struct btrfs_root *chunk_root = root->fs_info->chunk_root; 2957 struct btrfs_fs_info *fs_info = root->fs_info;
2958 struct btrfs_root *chunk_root = fs_info->chunk_root;
2960 struct btrfs_path *path; 2959 struct btrfs_path *path;
2961 struct extent_buffer *leaf; 2960 struct extent_buffer *leaf;
2962 struct btrfs_chunk *chunk; 2961 struct btrfs_chunk *chunk;
@@ -2977,10 +2976,10 @@ again:
2977 key.type = BTRFS_CHUNK_ITEM_KEY; 2976 key.type = BTRFS_CHUNK_ITEM_KEY;
2978 2977
2979 while (1) { 2978 while (1) {
2980 mutex_lock(&root->fs_info->delete_unused_bgs_mutex); 2979 mutex_lock(&fs_info->delete_unused_bgs_mutex);
2981 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0); 2980 ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
2982 if (ret < 0) { 2981 if (ret < 0) {
2983 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 2982 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2984 goto error; 2983 goto error;
2985 } 2984 }
2986 BUG_ON(ret == 0); /* Corruption */ 2985 BUG_ON(ret == 0); /* Corruption */
@@ -2988,7 +2987,7 @@ again:
2988 ret = btrfs_previous_item(chunk_root, path, key.objectid, 2987 ret = btrfs_previous_item(chunk_root, path, key.objectid,
2989 key.type); 2988 key.type);
2990 if (ret) 2989 if (ret)
2991 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 2990 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
2992 if (ret < 0) 2991 if (ret < 0)
2993 goto error; 2992 goto error;
2994 if (ret > 0) 2993 if (ret > 0)
@@ -3003,14 +3002,13 @@ again:
3003 btrfs_release_path(path); 3002 btrfs_release_path(path);
3004 3003
3005 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) { 3004 if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
3006 ret = btrfs_relocate_chunk(root->fs_info, 3005 ret = btrfs_relocate_chunk(fs_info, found_key.offset);
3007 found_key.offset);
3008 if (ret == -ENOSPC) 3006 if (ret == -ENOSPC)
3009 failed++; 3007 failed++;
3010 else 3008 else
3011 BUG_ON(ret); 3009 BUG_ON(ret);
3012 } 3010 }
3013 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 3011 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
3014 3012
3015 if (found_key.offset == 0) 3013 if (found_key.offset == 0)
3016 break; 3014 break;
@@ -3375,7 +3373,8 @@ static int should_balance_chunk(struct btrfs_root *root,
3375 struct extent_buffer *leaf, 3373 struct extent_buffer *leaf,
3376 struct btrfs_chunk *chunk, u64 chunk_offset) 3374 struct btrfs_chunk *chunk, u64 chunk_offset)
3377{ 3375{
3378 struct btrfs_balance_control *bctl = root->fs_info->balance_ctl; 3376 struct btrfs_fs_info *fs_info = root->fs_info;
3377 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3379 struct btrfs_balance_args *bargs = NULL; 3378 struct btrfs_balance_args *bargs = NULL;
3380 u64 chunk_type = btrfs_chunk_type(leaf, chunk); 3379 u64 chunk_type = btrfs_chunk_type(leaf, chunk);
3381 3380
@@ -3400,10 +3399,10 @@ static int should_balance_chunk(struct btrfs_root *root,
3400 3399
3401 /* usage filter */ 3400 /* usage filter */
3402 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) && 3401 if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
3403 chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) { 3402 chunk_usage_filter(fs_info, chunk_offset, bargs)) {
3404 return 0; 3403 return 0;
3405 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) && 3404 } else if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) &&
3406 chunk_usage_range_filter(bctl->fs_info, chunk_offset, bargs)) { 3405 chunk_usage_range_filter(fs_info, chunk_offset, bargs)) {
3407 return 0; 3406 return 0;
3408 } 3407 }
3409 3408
@@ -4356,8 +4355,9 @@ int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
4356 */ 4355 */
4357int btrfs_shrink_device(struct btrfs_device *device, u64 new_size) 4356int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4358{ 4357{
4358 struct btrfs_fs_info *fs_info = device->fs_info;
4359 struct btrfs_root *root = fs_info->dev_root;
4359 struct btrfs_trans_handle *trans; 4360 struct btrfs_trans_handle *trans;
4360 struct btrfs_root *root = device->fs_info->dev_root;
4361 struct btrfs_dev_extent *dev_extent = NULL; 4361 struct btrfs_dev_extent *dev_extent = NULL;
4362 struct btrfs_path *path; 4362 struct btrfs_path *path;
4363 u64 length; 4363 u64 length;
@@ -4369,7 +4369,7 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4369 bool checked_pending_chunks = false; 4369 bool checked_pending_chunks = false;
4370 struct extent_buffer *l; 4370 struct extent_buffer *l;
4371 struct btrfs_key key; 4371 struct btrfs_key key;
4372 struct btrfs_super_block *super_copy = root->fs_info->super_copy; 4372 struct btrfs_super_block *super_copy = fs_info->super_copy;
4373 u64 old_total = btrfs_super_total_bytes(super_copy); 4373 u64 old_total = btrfs_super_total_bytes(super_copy);
4374 u64 old_size = btrfs_device_get_total_bytes(device); 4374 u64 old_size = btrfs_device_get_total_bytes(device);
4375 u64 diff = old_size - new_size; 4375 u64 diff = old_size - new_size;
@@ -4383,16 +4383,16 @@ int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
4383 4383
4384 path->reada = READA_FORWARD; 4384 path->reada = READA_FORWARD;
4385 4385
4386 lock_chunks(root->fs_info); 4386 lock_chunks(fs_info);
4387 4387
4388 btrfs_device_set_total_bytes(device, new_size); 4388 btrfs_device_set_total_bytes(device, new_size);
4389 if (device->writeable) { 4389 if (device->writeable) {
4390 device->fs_devices->total_rw_bytes -= diff; 4390 device->fs_devices->total_rw_bytes -= diff;
4391 spin_lock(&root->fs_info->free_chunk_lock); 4391 spin_lock(&fs_info->free_chunk_lock);
4392 root->fs_info->free_chunk_space -= diff; 4392 fs_info->free_chunk_space -= diff;
4393 spin_unlock(&root->fs_info->free_chunk_lock); 4393 spin_unlock(&fs_info->free_chunk_lock);
4394 } 4394 }
4395 unlock_chunks(root->fs_info); 4395 unlock_chunks(fs_info);
4396 4396
4397again: 4397again:
4398 key.objectid = device->devid; 4398 key.objectid = device->devid;
@@ -4400,16 +4400,16 @@ again:
4400 key.type = BTRFS_DEV_EXTENT_KEY; 4400 key.type = BTRFS_DEV_EXTENT_KEY;
4401 4401
4402 do { 4402 do {
4403 mutex_lock(&root->fs_info->delete_unused_bgs_mutex); 4403 mutex_lock(&fs_info->delete_unused_bgs_mutex);
4404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 4404 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4405 if (ret < 0) { 4405 if (ret < 0) {
4406 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 4406 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4407 goto done; 4407 goto done;
4408 } 4408 }
4409 4409
4410 ret = btrfs_previous_item(root, path, 0, key.type); 4410 ret = btrfs_previous_item(root, path, 0, key.type);
4411 if (ret) 4411 if (ret)
4412 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 4412 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4413 if (ret < 0) 4413 if (ret < 0)
4414 goto done; 4414 goto done;
4415 if (ret) { 4415 if (ret) {
@@ -4423,7 +4423,7 @@ again:
4423 btrfs_item_key_to_cpu(l, &key, path->slots[0]); 4423 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
4424 4424
4425 if (key.objectid != device->devid) { 4425 if (key.objectid != device->devid) {
4426 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 4426 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4427 btrfs_release_path(path); 4427 btrfs_release_path(path);
4428 break; 4428 break;
4429 } 4429 }
@@ -4432,7 +4432,7 @@ again:
4432 length = btrfs_dev_extent_length(l, dev_extent); 4432 length = btrfs_dev_extent_length(l, dev_extent);
4433 4433
4434 if (key.offset + length <= new_size) { 4434 if (key.offset + length <= new_size) {
4435 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 4435 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4436 btrfs_release_path(path); 4436 btrfs_release_path(path);
4437 break; 4437 break;
4438 } 4438 }
@@ -4440,8 +4440,8 @@ again:
4440 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent); 4440 chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
4441 btrfs_release_path(path); 4441 btrfs_release_path(path);
4442 4442
4443 ret = btrfs_relocate_chunk(root->fs_info, chunk_offset); 4443 ret = btrfs_relocate_chunk(fs_info, chunk_offset);
4444 mutex_unlock(&root->fs_info->delete_unused_bgs_mutex); 4444 mutex_unlock(&fs_info->delete_unused_bgs_mutex);
4445 if (ret && ret != -ENOSPC) 4445 if (ret && ret != -ENOSPC)
4446 goto done; 4446 goto done;
4447 if (ret == -ENOSPC) 4447 if (ret == -ENOSPC)
@@ -4464,7 +4464,7 @@ again:
4464 goto done; 4464 goto done;
4465 } 4465 }
4466 4466
4467 lock_chunks(root->fs_info); 4467 lock_chunks(fs_info);
4468 4468
4469 /* 4469 /*
4470 * We checked in the above loop all device extents that were already in 4470 * We checked in the above loop all device extents that were already in
@@ -4484,7 +4484,7 @@ again:
4484 4484
4485 if (contains_pending_extent(trans->transaction, device, 4485 if (contains_pending_extent(trans->transaction, device,
4486 &start, len)) { 4486 &start, len)) {
4487 unlock_chunks(root->fs_info); 4487 unlock_chunks(fs_info);
4488 checked_pending_chunks = true; 4488 checked_pending_chunks = true;
4489 failed = 0; 4489 failed = 0;
4490 retried = false; 4490 retried = false;
@@ -4498,11 +4498,11 @@ again:
4498 btrfs_device_set_disk_total_bytes(device, new_size); 4498 btrfs_device_set_disk_total_bytes(device, new_size);
4499 if (list_empty(&device->resized_list)) 4499 if (list_empty(&device->resized_list))
4500 list_add_tail(&device->resized_list, 4500 list_add_tail(&device->resized_list,
4501 &root->fs_info->fs_devices->resized_devices); 4501 &fs_info->fs_devices->resized_devices);
4502 4502
4503 WARN_ON(diff > old_total); 4503 WARN_ON(diff > old_total);
4504 btrfs_set_super_total_bytes(super_copy, old_total - diff); 4504 btrfs_set_super_total_bytes(super_copy, old_total - diff);
4505 unlock_chunks(root->fs_info); 4505 unlock_chunks(fs_info);
4506 4506
4507 /* Now btrfs_update_device() will change the on-disk size. */ 4507 /* Now btrfs_update_device() will change the on-disk size. */
4508 ret = btrfs_update_device(trans, device); 4508 ret = btrfs_update_device(trans, device);
@@ -4510,14 +4510,14 @@ again:
4510done: 4510done:
4511 btrfs_free_path(path); 4511 btrfs_free_path(path);
4512 if (ret) { 4512 if (ret) {
4513 lock_chunks(root->fs_info); 4513 lock_chunks(fs_info);
4514 btrfs_device_set_total_bytes(device, old_size); 4514 btrfs_device_set_total_bytes(device, old_size);
4515 if (device->writeable) 4515 if (device->writeable)
4516 device->fs_devices->total_rw_bytes += diff; 4516 device->fs_devices->total_rw_bytes += diff;
4517 spin_lock(&root->fs_info->free_chunk_lock); 4517 spin_lock(&fs_info->free_chunk_lock);
4518 root->fs_info->free_chunk_space += diff; 4518 fs_info->free_chunk_space += diff;
4519 spin_unlock(&root->fs_info->free_chunk_lock); 4519 spin_unlock(&fs_info->free_chunk_lock);
4520 unlock_chunks(root->fs_info); 4520 unlock_chunks(fs_info);
4521 } 4521 }
4522 return ret; 4522 return ret;
4523} 4523}
@@ -4526,16 +4526,17 @@ static int btrfs_add_system_chunk(struct btrfs_root *root,
4526 struct btrfs_key *key, 4526 struct btrfs_key *key,
4527 struct btrfs_chunk *chunk, int item_size) 4527 struct btrfs_chunk *chunk, int item_size)
4528{ 4528{
4529 struct btrfs_super_block *super_copy = root->fs_info->super_copy; 4529 struct btrfs_fs_info *fs_info = root->fs_info;
4530 struct btrfs_super_block *super_copy = fs_info->super_copy;
4530 struct btrfs_disk_key disk_key; 4531 struct btrfs_disk_key disk_key;
4531 u32 array_size; 4532 u32 array_size;
4532 u8 *ptr; 4533 u8 *ptr;
4533 4534
4534 lock_chunks(root->fs_info); 4535 lock_chunks(fs_info);
4535 array_size = btrfs_super_sys_array_size(super_copy); 4536 array_size = btrfs_super_sys_array_size(super_copy);
4536 if (array_size + item_size + sizeof(disk_key) 4537 if (array_size + item_size + sizeof(disk_key)
4537 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) { 4538 > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
4538 unlock_chunks(root->fs_info); 4539 unlock_chunks(fs_info);
4539 return -EFBIG; 4540 return -EFBIG;
4540 } 4541 }
4541 4542
@@ -4546,7 +4547,7 @@ static int btrfs_add_system_chunk(struct btrfs_root *root,
4546 memcpy(ptr, chunk, item_size); 4547 memcpy(ptr, chunk, item_size);
4547 item_size += sizeof(disk_key); 4548 item_size += sizeof(disk_key);
4548 btrfs_set_super_sys_array_size(super_copy, array_size + item_size); 4549 btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
4549 unlock_chunks(root->fs_info); 4550 unlock_chunks(fs_info);
4550 4551
4551 return 0; 4552 return 0;
4552} 4553}
@@ -4838,7 +4839,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4838 em->block_len = em->len; 4839 em->block_len = em->len;
4839 em->orig_block_len = stripe_size; 4840 em->orig_block_len = stripe_size;
4840 4841
4841 em_tree = &extent_root->fs_info->mapping_tree.map_tree; 4842 em_tree = &info->mapping_tree.map_tree;
4842 write_lock(&em_tree->lock); 4843 write_lock(&em_tree->lock);
4843 ret = add_extent_mapping(em_tree, em, 0); 4844 ret = add_extent_mapping(em_tree, em, 0);
4844 if (!ret) { 4845 if (!ret) {
@@ -4862,13 +4863,12 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4862 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes); 4863 btrfs_device_set_bytes_used(map->stripes[i].dev, num_bytes);
4863 } 4864 }
4864 4865
4865 spin_lock(&extent_root->fs_info->free_chunk_lock); 4866 spin_lock(&info->free_chunk_lock);
4866 extent_root->fs_info->free_chunk_space -= (stripe_size * 4867 info->free_chunk_space -= (stripe_size * map->num_stripes);
4867 map->num_stripes); 4868 spin_unlock(&info->free_chunk_lock);
4868 spin_unlock(&extent_root->fs_info->free_chunk_lock);
4869 4869
4870 free_extent_map(em); 4870 free_extent_map(em);
4871 check_raid56_incompat_flag(extent_root->fs_info, type); 4871 check_raid56_incompat_flag(info, type);
4872 4872
4873 kfree(devices_info); 4873 kfree(devices_info);
4874 return 0; 4874 return 0;
@@ -4908,20 +4908,19 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4908 int i = 0; 4908 int i = 0;
4909 int ret = 0; 4909 int ret = 0;
4910 4910
4911 em_tree = &extent_root->fs_info->mapping_tree.map_tree; 4911 em_tree = &fs_info->mapping_tree.map_tree;
4912 read_lock(&em_tree->lock); 4912 read_lock(&em_tree->lock);
4913 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size); 4913 em = lookup_extent_mapping(em_tree, chunk_offset, chunk_size);
4914 read_unlock(&em_tree->lock); 4914 read_unlock(&em_tree->lock);
4915 4915
4916 if (!em) { 4916 if (!em) {
4917 btrfs_crit(extent_root->fs_info, 4917 btrfs_crit(fs_info, "unable to find logical %Lu len %Lu",
4918 "unable to find logical %Lu len %Lu",
4919 chunk_offset, chunk_size); 4918 chunk_offset, chunk_size);
4920 return -EINVAL; 4919 return -EINVAL;
4921 } 4920 }
4922 4921
4923 if (em->start != chunk_offset || em->len != chunk_size) { 4922 if (em->start != chunk_offset || em->len != chunk_size) {
4924 btrfs_crit(extent_root->fs_info, 4923 btrfs_crit(fs_info,
4925 "found a bad mapping, wanted %Lu-%Lu, found %Lu-%Lu", 4924 "found a bad mapping, wanted %Lu-%Lu, found %Lu-%Lu",
4926 chunk_offset, chunk_size, em->start, em->len); 4925 chunk_offset, chunk_size, em->start, em->len);
4927 free_extent_map(em); 4926 free_extent_map(em);
@@ -4945,7 +4944,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4945 * at any time during that final phase of the device replace operation 4944 * at any time during that final phase of the device replace operation
4946 * (dev-replace.c:btrfs_dev_replace_finishing()). 4945 * (dev-replace.c:btrfs_dev_replace_finishing()).
4947 */ 4946 */
4948 mutex_lock(&chunk_root->fs_info->fs_devices->device_list_mutex); 4947 mutex_lock(&fs_info->fs_devices->device_list_mutex);
4949 for (i = 0; i < map->num_stripes; i++) { 4948 for (i = 0; i < map->num_stripes; i++) {
4950 device = map->stripes[i].dev; 4949 device = map->stripes[i].dev;
4951 dev_offset = map->stripes[i].physical; 4950 dev_offset = map->stripes[i].physical;
@@ -4962,7 +4961,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4962 break; 4961 break;
4963 } 4962 }
4964 if (ret) { 4963 if (ret) {
4965 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex); 4964 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4966 goto out; 4965 goto out;
4967 } 4966 }
4968 4967
@@ -4976,7 +4975,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4976 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE); 4975 memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
4977 stripe++; 4976 stripe++;
4978 } 4977 }
4979 mutex_unlock(&chunk_root->fs_info->fs_devices->device_list_mutex); 4978 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
4980 4979
4981 btrfs_set_stack_chunk_length(chunk, chunk_size); 4980 btrfs_set_stack_chunk_length(chunk, chunk_size);
4982 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid); 4981 btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
@@ -4985,8 +4984,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4985 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes); 4984 btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
4986 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len); 4985 btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
4987 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len); 4986 btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
4988 btrfs_set_stack_chunk_sector_size(chunk, 4987 btrfs_set_stack_chunk_sector_size(chunk, fs_info->sectorsize);
4989 extent_root->fs_info->sectorsize);
4990 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes); 4988 btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
4991 4989
4992 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID; 4990 key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
@@ -5019,10 +5017,11 @@ out:
5019int btrfs_alloc_chunk(struct btrfs_trans_handle *trans, 5017int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
5020 struct btrfs_root *extent_root, u64 type) 5018 struct btrfs_root *extent_root, u64 type)
5021{ 5019{
5020 struct btrfs_fs_info *fs_info = extent_root->fs_info;
5022 u64 chunk_offset; 5021 u64 chunk_offset;
5023 5022
5024 ASSERT(mutex_is_locked(&extent_root->fs_info->chunk_mutex)); 5023 ASSERT(mutex_is_locked(&fs_info->chunk_mutex));
5025 chunk_offset = find_next_chunk(extent_root->fs_info); 5024 chunk_offset = find_next_chunk(fs_info);
5026 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type); 5025 return __btrfs_alloc_chunk(trans, extent_root, chunk_offset, type);
5027} 5026}
5028 5027
@@ -5044,7 +5043,7 @@ static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
5044 if (ret) 5043 if (ret)
5045 return ret; 5044 return ret;
5046 5045
5047 sys_chunk_offset = find_next_chunk(root->fs_info); 5046 sys_chunk_offset = find_next_chunk(fs_info);
5048 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0); 5047 alloc_profile = btrfs_get_alloc_profile(fs_info->chunk_root, 0);
5049 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset, 5048 ret = __btrfs_alloc_chunk(trans, extent_root, sys_chunk_offset,
5050 alloc_profile); 5049 alloc_profile);
@@ -5071,9 +5070,10 @@ static inline int btrfs_chunk_max_errors(struct map_lookup *map)
5071 5070
5072int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset) 5071int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
5073{ 5072{
5073 struct btrfs_fs_info *fs_info = root->fs_info;
5074 struct extent_map *em; 5074 struct extent_map *em;
5075 struct map_lookup *map; 5075 struct map_lookup *map;
5076 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; 5076 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
5077 int readonly = 0; 5077 int readonly = 0;
5078 int miss_ndevs = 0; 5078 int miss_ndevs = 0;
5079 int i; 5079 int i;
@@ -5189,10 +5189,11 @@ unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
5189 struct btrfs_mapping_tree *map_tree, 5189 struct btrfs_mapping_tree *map_tree,
5190 u64 logical) 5190 u64 logical)
5191{ 5191{
5192 struct btrfs_fs_info *fs_info = root->fs_info;
5192 struct extent_map *em; 5193 struct extent_map *em;
5193 struct map_lookup *map; 5194 struct map_lookup *map;
5194 struct extent_map_tree *em_tree = &map_tree->map_tree; 5195 struct extent_map_tree *em_tree = &map_tree->map_tree;
5195 unsigned long len = root->fs_info->sectorsize; 5196 unsigned long len = fs_info->sectorsize;
5196 5197
5197 read_lock(&em_tree->lock); 5198 read_lock(&em_tree->lock);
5198 em = lookup_extent_mapping(em_tree, logical, len); 5199 em = lookup_extent_mapping(em_tree, logical, len);
@@ -6078,6 +6079,7 @@ static noinline void btrfs_schedule_bio(struct btrfs_root *root,
6078 struct btrfs_device *device, 6079 struct btrfs_device *device,
6079 struct bio *bio) 6080 struct bio *bio)
6080{ 6081{
6082 struct btrfs_fs_info *fs_info = device->fs_info;
6081 int should_queue = 1; 6083 int should_queue = 1;
6082 struct btrfs_pending_bios *pending_bios; 6084 struct btrfs_pending_bios *pending_bios;
6083 6085
@@ -6100,7 +6102,7 @@ static noinline void btrfs_schedule_bio(struct btrfs_root *root,
6100 * made progress against dirty pages when we've really just put it 6102 * made progress against dirty pages when we've really just put it
6101 * on a queue for later 6103 * on a queue for later
6102 */ 6104 */
6103 atomic_inc(&root->fs_info->nr_async_bios); 6105 atomic_inc(&fs_info->nr_async_bios);
6104 WARN_ON(bio->bi_next); 6106 WARN_ON(bio->bi_next);
6105 bio->bi_next = NULL; 6107 bio->bi_next = NULL;
6106 6108
@@ -6122,8 +6124,7 @@ static noinline void btrfs_schedule_bio(struct btrfs_root *root,
6122 spin_unlock(&device->io_lock); 6124 spin_unlock(&device->io_lock);
6123 6125
6124 if (should_queue) 6126 if (should_queue)
6125 btrfs_queue_work(root->fs_info->submit_workers, 6127 btrfs_queue_work(fs_info->submit_workers, &device->work);
6126 &device->work);
6127} 6128}
6128 6129
6129static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio, 6130static void submit_stripe_bio(struct btrfs_root *root, struct btrfs_bio *bbio,
@@ -6178,6 +6179,7 @@ static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
6178int btrfs_map_bio(struct btrfs_root *root, struct bio *bio, 6179int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6179 int mirror_num, int async_submit) 6180 int mirror_num, int async_submit)
6180{ 6181{
6182 struct btrfs_fs_info *fs_info = root->fs_info;
6181 struct btrfs_device *dev; 6183 struct btrfs_device *dev;
6182 struct bio *first_bio = bio; 6184 struct bio *first_bio = bio;
6183 u64 logical = (u64)bio->bi_iter.bi_sector << 9; 6185 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
@@ -6191,11 +6193,11 @@ int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6191 length = bio->bi_iter.bi_size; 6193 length = bio->bi_iter.bi_size;
6192 map_length = length; 6194 map_length = length;
6193 6195
6194 btrfs_bio_counter_inc_blocked(root->fs_info); 6196 btrfs_bio_counter_inc_blocked(fs_info);
6195 ret = __btrfs_map_block(root->fs_info, bio_op(bio), logical, 6197 ret = __btrfs_map_block(fs_info, bio_op(bio), logical,
6196 &map_length, &bbio, mirror_num, 1); 6198 &map_length, &bbio, mirror_num, 1);
6197 if (ret) { 6199 if (ret) {
6198 btrfs_bio_counter_dec(root->fs_info); 6200 btrfs_bio_counter_dec(fs_info);
6199 return ret; 6201 return ret;
6200 } 6202 }
6201 6203
@@ -6203,7 +6205,7 @@ int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6203 bbio->orig_bio = first_bio; 6205 bbio->orig_bio = first_bio;
6204 bbio->private = first_bio->bi_private; 6206 bbio->private = first_bio->bi_private;
6205 bbio->end_io = first_bio->bi_end_io; 6207 bbio->end_io = first_bio->bi_end_io;
6206 bbio->fs_info = root->fs_info; 6208 bbio->fs_info = fs_info;
6207 atomic_set(&bbio->stripes_pending, bbio->num_stripes); 6209 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6208 6210
6209 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) && 6211 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
@@ -6217,12 +6219,12 @@ int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6217 mirror_num, 1); 6219 mirror_num, 1);
6218 } 6220 }
6219 6221
6220 btrfs_bio_counter_dec(root->fs_info); 6222 btrfs_bio_counter_dec(fs_info);
6221 return ret; 6223 return ret;
6222 } 6224 }
6223 6225
6224 if (map_length < length) { 6226 if (map_length < length) {
6225 btrfs_crit(root->fs_info, 6227 btrfs_crit(fs_info,
6226 "mapping failed logical %llu bio len %llu len %llu", 6228 "mapping failed logical %llu bio len %llu len %llu",
6227 logical, length, map_length); 6229 logical, length, map_length);
6228 BUG(); 6230 BUG();
@@ -6246,7 +6248,7 @@ int btrfs_map_bio(struct btrfs_root *root, struct bio *bio,
6246 bbio->stripes[dev_nr].physical, dev_nr, 6248 bbio->stripes[dev_nr].physical, dev_nr,
6247 async_submit); 6249 async_submit);
6248 } 6250 }
6249 btrfs_bio_counter_dec(root->fs_info); 6251 btrfs_bio_counter_dec(fs_info);
6250 return 0; 6252 return 0;
6251} 6253}
6252 6254
@@ -6346,6 +6348,7 @@ static int btrfs_check_chunk_valid(struct btrfs_root *root,
6346 struct extent_buffer *leaf, 6348 struct extent_buffer *leaf,
6347 struct btrfs_chunk *chunk, u64 logical) 6349 struct btrfs_chunk *chunk, u64 logical)
6348{ 6350{
6351 struct btrfs_fs_info *fs_info = root->fs_info;
6349 u64 length; 6352 u64 length;
6350 u64 stripe_len; 6353 u64 stripe_len;
6351 u16 num_stripes; 6354 u16 num_stripes;
@@ -6359,33 +6362,31 @@ static int btrfs_check_chunk_valid(struct btrfs_root *root,
6359 type = btrfs_chunk_type(leaf, chunk); 6362 type = btrfs_chunk_type(leaf, chunk);
6360 6363
6361 if (!num_stripes) { 6364 if (!num_stripes) {
6362 btrfs_err(root->fs_info, "invalid chunk num_stripes: %u", 6365 btrfs_err(fs_info, "invalid chunk num_stripes: %u",
6363 num_stripes); 6366 num_stripes);
6364 return -EIO; 6367 return -EIO;
6365 } 6368 }
6366 if (!IS_ALIGNED(logical, root->fs_info->sectorsize)) { 6369 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
6367 btrfs_err(root->fs_info, 6370 btrfs_err(fs_info, "invalid chunk logical %llu", logical);
6368 "invalid chunk logical %llu", logical);
6369 return -EIO; 6371 return -EIO;
6370 } 6372 }
6371 if (btrfs_chunk_sector_size(leaf, chunk) != root->fs_info->sectorsize) { 6373 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
6372 btrfs_err(root->fs_info, "invalid chunk sectorsize %u", 6374 btrfs_err(fs_info, "invalid chunk sectorsize %u",
6373 btrfs_chunk_sector_size(leaf, chunk)); 6375 btrfs_chunk_sector_size(leaf, chunk));
6374 return -EIO; 6376 return -EIO;
6375 } 6377 }
6376 if (!length || !IS_ALIGNED(length, root->fs_info->sectorsize)) { 6378 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
6377 btrfs_err(root->fs_info, 6379 btrfs_err(fs_info, "invalid chunk length %llu", length);
6378 "invalid chunk length %llu", length);
6379 return -EIO; 6380 return -EIO;
6380 } 6381 }
6381 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { 6382 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
6382 btrfs_err(root->fs_info, "invalid chunk stripe length: %llu", 6383 btrfs_err(fs_info, "invalid chunk stripe length: %llu",
6383 stripe_len); 6384 stripe_len);
6384 return -EIO; 6385 return -EIO;
6385 } 6386 }
6386 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & 6387 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6387 type) { 6388 type) {
6388 btrfs_err(root->fs_info, "unrecognized chunk type: %llu", 6389 btrfs_err(fs_info, "unrecognized chunk type: %llu",
6389 ~(BTRFS_BLOCK_GROUP_TYPE_MASK | 6390 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6390 BTRFS_BLOCK_GROUP_PROFILE_MASK) & 6391 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6391 btrfs_chunk_type(leaf, chunk)); 6392 btrfs_chunk_type(leaf, chunk));
@@ -6398,7 +6399,7 @@ static int btrfs_check_chunk_valid(struct btrfs_root *root,
6398 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || 6399 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
6399 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && 6400 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
6400 num_stripes != 1)) { 6401 num_stripes != 1)) {
6401 btrfs_err(root->fs_info, 6402 btrfs_err(fs_info,
6402 "invalid num_stripes:sub_stripes %u:%u for profile %llu", 6403 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
6403 num_stripes, sub_stripes, 6404 num_stripes, sub_stripes,
6404 type & BTRFS_BLOCK_GROUP_PROFILE_MASK); 6405 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
@@ -6412,7 +6413,8 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6412 struct extent_buffer *leaf, 6413 struct extent_buffer *leaf,
6413 struct btrfs_chunk *chunk) 6414 struct btrfs_chunk *chunk)
6414{ 6415{
6415 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree; 6416 struct btrfs_fs_info *fs_info = root->fs_info;
6417 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
6416 struct map_lookup *map; 6418 struct map_lookup *map;
6417 struct extent_map *em; 6419 struct extent_map *em;
6418 u64 logical; 6420 u64 logical;
@@ -6476,23 +6478,22 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6476 read_extent_buffer(leaf, uuid, (unsigned long) 6478 read_extent_buffer(leaf, uuid, (unsigned long)
6477 btrfs_stripe_dev_uuid_nr(chunk, i), 6479 btrfs_stripe_dev_uuid_nr(chunk, i),
6478 BTRFS_UUID_SIZE); 6480 BTRFS_UUID_SIZE);
6479 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, 6481 map->stripes[i].dev = btrfs_find_device(fs_info, devid,
6480 uuid, NULL); 6482 uuid, NULL);
6481 if (!map->stripes[i].dev && 6483 if (!map->stripes[i].dev &&
6482 !btrfs_test_opt(root->fs_info, DEGRADED)) { 6484 !btrfs_test_opt(fs_info, DEGRADED)) {
6483 free_extent_map(em); 6485 free_extent_map(em);
6484 return -EIO; 6486 return -EIO;
6485 } 6487 }
6486 if (!map->stripes[i].dev) { 6488 if (!map->stripes[i].dev) {
6487 map->stripes[i].dev = 6489 map->stripes[i].dev =
6488 add_missing_dev(root, root->fs_info->fs_devices, 6490 add_missing_dev(root, fs_info->fs_devices,
6489 devid, uuid); 6491 devid, uuid);
6490 if (!map->stripes[i].dev) { 6492 if (!map->stripes[i].dev) {
6491 free_extent_map(em); 6493 free_extent_map(em);
6492 return -EIO; 6494 return -EIO;
6493 } 6495 }
6494 btrfs_warn(root->fs_info, 6496 btrfs_warn(fs_info, "devid %llu uuid %pU is missing",
6495 "devid %llu uuid %pU is missing",
6496 devid, uuid); 6497 devid, uuid);
6497 } 6498 }
6498 map->stripes[i].dev->in_fs_metadata = 1; 6499 map->stripes[i].dev->in_fs_metadata = 1;
@@ -6533,12 +6534,13 @@ static void fill_device_from_item(struct extent_buffer *leaf,
6533static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root, 6534static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6534 u8 *fsid) 6535 u8 *fsid)
6535{ 6536{
6537 struct btrfs_fs_info *fs_info = root->fs_info;
6536 struct btrfs_fs_devices *fs_devices; 6538 struct btrfs_fs_devices *fs_devices;
6537 int ret; 6539 int ret;
6538 6540
6539 BUG_ON(!mutex_is_locked(&uuid_mutex)); 6541 BUG_ON(!mutex_is_locked(&uuid_mutex));
6540 6542
6541 fs_devices = root->fs_info->fs_devices->seed; 6543 fs_devices = fs_info->fs_devices->seed;
6542 while (fs_devices) { 6544 while (fs_devices) {
6543 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) 6545 if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE))
6544 return fs_devices; 6546 return fs_devices;
@@ -6548,7 +6550,7 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6548 6550
6549 fs_devices = find_fsid(fsid); 6551 fs_devices = find_fsid(fsid);
6550 if (!fs_devices) { 6552 if (!fs_devices) {
6551 if (!btrfs_test_opt(root->fs_info, DEGRADED)) 6553 if (!btrfs_test_opt(fs_info, DEGRADED))
6552 return ERR_PTR(-ENOENT); 6554 return ERR_PTR(-ENOENT);
6553 6555
6554 fs_devices = alloc_fs_devices(fsid); 6556 fs_devices = alloc_fs_devices(fsid);
@@ -6565,7 +6567,7 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6565 return fs_devices; 6567 return fs_devices;
6566 6568
6567 ret = __btrfs_open_devices(fs_devices, FMODE_READ, 6569 ret = __btrfs_open_devices(fs_devices, FMODE_READ,
6568 root->fs_info->bdev_holder); 6570 fs_info->bdev_holder);
6569 if (ret) { 6571 if (ret) {
6570 free_fs_devices(fs_devices); 6572 free_fs_devices(fs_devices);
6571 fs_devices = ERR_PTR(ret); 6573 fs_devices = ERR_PTR(ret);
@@ -6579,8 +6581,8 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_root *root,
6579 goto out; 6581 goto out;
6580 } 6582 }
6581 6583
6582 fs_devices->seed = root->fs_info->fs_devices->seed; 6584 fs_devices->seed = fs_info->fs_devices->seed;
6583 root->fs_info->fs_devices->seed = fs_devices; 6585 fs_info->fs_devices->seed = fs_devices;
6584out: 6586out:
6585 return fs_devices; 6587 return fs_devices;
6586} 6588}
@@ -6589,7 +6591,8 @@ static int read_one_dev(struct btrfs_root *root,
6589 struct extent_buffer *leaf, 6591 struct extent_buffer *leaf,
6590 struct btrfs_dev_item *dev_item) 6592 struct btrfs_dev_item *dev_item)
6591{ 6593{
6592 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 6594 struct btrfs_fs_info *fs_info = root->fs_info;
6595 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
6593 struct btrfs_device *device; 6596 struct btrfs_device *device;
6594 u64 devid; 6597 u64 devid;
6595 int ret; 6598 int ret;
@@ -6602,24 +6605,24 @@ static int read_one_dev(struct btrfs_root *root,
6602 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item), 6605 read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
6603 BTRFS_UUID_SIZE); 6606 BTRFS_UUID_SIZE);
6604 6607
6605 if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) { 6608 if (memcmp(fs_uuid, fs_info->fsid, BTRFS_UUID_SIZE)) {
6606 fs_devices = open_seed_devices(root, fs_uuid); 6609 fs_devices = open_seed_devices(root, fs_uuid);
6607 if (IS_ERR(fs_devices)) 6610 if (IS_ERR(fs_devices))
6608 return PTR_ERR(fs_devices); 6611 return PTR_ERR(fs_devices);
6609 } 6612 }
6610 6613
6611 device = btrfs_find_device(root->fs_info, devid, dev_uuid, fs_uuid); 6614 device = btrfs_find_device(fs_info, devid, dev_uuid, fs_uuid);
6612 if (!device) { 6615 if (!device) {
6613 if (!btrfs_test_opt(root->fs_info, DEGRADED)) 6616 if (!btrfs_test_opt(fs_info, DEGRADED))
6614 return -EIO; 6617 return -EIO;
6615 6618
6616 device = add_missing_dev(root, fs_devices, devid, dev_uuid); 6619 device = add_missing_dev(root, fs_devices, devid, dev_uuid);
6617 if (!device) 6620 if (!device)
6618 return -ENOMEM; 6621 return -ENOMEM;
6619 btrfs_warn(root->fs_info, "devid %llu uuid %pU missing", 6622 btrfs_warn(fs_info, "devid %llu uuid %pU missing",
6620 devid, dev_uuid); 6623 devid, dev_uuid);
6621 } else { 6624 } else {
6622 if (!device->bdev && !btrfs_test_opt(root->fs_info, DEGRADED)) 6625 if (!device->bdev && !btrfs_test_opt(fs_info, DEGRADED))
6623 return -EIO; 6626 return -EIO;
6624 6627
6625 if(!device->bdev && !device->missing) { 6628 if(!device->bdev && !device->missing) {
@@ -6648,7 +6651,7 @@ static int read_one_dev(struct btrfs_root *root,
6648 } 6651 }
6649 } 6652 }
6650 6653
6651 if (device->fs_devices != root->fs_info->fs_devices) { 6654 if (device->fs_devices != fs_info->fs_devices) {
6652 BUG_ON(device->writeable); 6655 BUG_ON(device->writeable);
6653 if (device->generation != 6656 if (device->generation !=
6654 btrfs_device_generation(leaf, dev_item)) 6657 btrfs_device_generation(leaf, dev_item))
@@ -6659,10 +6662,10 @@ static int read_one_dev(struct btrfs_root *root,
6659 device->in_fs_metadata = 1; 6662 device->in_fs_metadata = 1;
6660 if (device->writeable && !device->is_tgtdev_for_dev_replace) { 6663 if (device->writeable && !device->is_tgtdev_for_dev_replace) {
6661 device->fs_devices->total_rw_bytes += device->total_bytes; 6664 device->fs_devices->total_rw_bytes += device->total_bytes;
6662 spin_lock(&root->fs_info->free_chunk_lock); 6665 spin_lock(&fs_info->free_chunk_lock);
6663 root->fs_info->free_chunk_space += device->total_bytes - 6666 fs_info->free_chunk_space += device->total_bytes -
6664 device->bytes_used; 6667 device->bytes_used;
6665 spin_unlock(&root->fs_info->free_chunk_lock); 6668 spin_unlock(&fs_info->free_chunk_lock);
6666 } 6669 }
6667 ret = 0; 6670 ret = 0;
6668 return ret; 6671 return ret;
@@ -6685,7 +6688,7 @@ int btrfs_read_sys_array(struct btrfs_fs_info *fs_info)
6685 u64 type; 6688 u64 type;
6686 struct btrfs_key key; 6689 struct btrfs_key key;
6687 6690
6688 ASSERT(BTRFS_SUPER_INFO_SIZE <= root->fs_info->nodesize); 6691 ASSERT(BTRFS_SUPER_INFO_SIZE <= fs_info->nodesize);
6689 /* 6692 /*
6690 * This will create extent buffer of nodesize, superblock size is 6693 * This will create extent buffer of nodesize, superblock size is
6691 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will 6694 * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
@@ -6804,7 +6807,7 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
6804 return -ENOMEM; 6807 return -ENOMEM;
6805 6808
6806 mutex_lock(&uuid_mutex); 6809 mutex_lock(&uuid_mutex);
6807 lock_chunks(root->fs_info); 6810 lock_chunks(fs_info);
6808 6811
6809 /* 6812 /*
6810 * Read all device items, and then all the chunk items. All 6813 * Read all device items, and then all the chunk items. All
@@ -6852,26 +6855,26 @@ int btrfs_read_chunk_tree(struct btrfs_fs_info *fs_info)
6852 * After loading chunk tree, we've got all device information, 6855 * After loading chunk tree, we've got all device information,
6853 * do another round of validation checks. 6856 * do another round of validation checks.
6854 */ 6857 */
6855 if (total_dev != root->fs_info->fs_devices->total_devices) { 6858 if (total_dev != fs_info->fs_devices->total_devices) {
6856 btrfs_err(root->fs_info, 6859 btrfs_err(fs_info,
6857 "super_num_devices %llu mismatch with num_devices %llu found here", 6860 "super_num_devices %llu mismatch with num_devices %llu found here",
6858 btrfs_super_num_devices(root->fs_info->super_copy), 6861 btrfs_super_num_devices(fs_info->super_copy),
6859 total_dev); 6862 total_dev);
6860 ret = -EINVAL; 6863 ret = -EINVAL;
6861 goto error; 6864 goto error;
6862 } 6865 }
6863 if (btrfs_super_total_bytes(root->fs_info->super_copy) < 6866 if (btrfs_super_total_bytes(fs_info->super_copy) <
6864 root->fs_info->fs_devices->total_rw_bytes) { 6867 fs_info->fs_devices->total_rw_bytes) {
6865 btrfs_err(root->fs_info, 6868 btrfs_err(fs_info,
6866 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu", 6869 "super_total_bytes %llu mismatch with fs_devices total_rw_bytes %llu",
6867 btrfs_super_total_bytes(root->fs_info->super_copy), 6870 btrfs_super_total_bytes(fs_info->super_copy),
6868 root->fs_info->fs_devices->total_rw_bytes); 6871 fs_info->fs_devices->total_rw_bytes);
6869 ret = -EINVAL; 6872 ret = -EINVAL;
6870 goto error; 6873 goto error;
6871 } 6874 }
6872 ret = 0; 6875 ret = 0;
6873error: 6876error:
6874 unlock_chunks(root->fs_info); 6877 unlock_chunks(fs_info);
6875 mutex_unlock(&uuid_mutex); 6878 mutex_unlock(&uuid_mutex);
6876 6879
6877 btrfs_free_path(path); 6880 btrfs_free_path(path);
@@ -6982,7 +6985,7 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6982 BUG_ON(!path); 6985 BUG_ON(!path);
6983 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1); 6986 ret = btrfs_search_slot(trans, dev_root, &key, path, -1, 1);
6984 if (ret < 0) { 6987 if (ret < 0) {
6985 btrfs_warn_in_rcu(dev_root->fs_info, 6988 btrfs_warn_in_rcu(fs_info,
6986 "error %d while searching for dev_stats item for device %s", 6989 "error %d while searching for dev_stats item for device %s",
6987 ret, rcu_str_deref(device->name)); 6990 ret, rcu_str_deref(device->name));
6988 goto out; 6991 goto out;
@@ -6993,7 +6996,7 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans,
6993 /* need to delete old one and insert a new one */ 6996 /* need to delete old one and insert a new one */
6994 ret = btrfs_del_item(trans, dev_root, path); 6997 ret = btrfs_del_item(trans, dev_root, path);
6995 if (ret != 0) { 6998 if (ret != 0) {
6996 btrfs_warn_in_rcu(dev_root->fs_info, 6999 btrfs_warn_in_rcu(fs_info,
6997 "delete too small dev_stats item for device %s failed %d", 7000 "delete too small dev_stats item for device %s failed %d",
6998 rcu_str_deref(device->name), ret); 7001 rcu_str_deref(device->name), ret);
6999 goto out; 7002 goto out;
@@ -7007,7 +7010,7 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans,
7007 ret = btrfs_insert_empty_item(trans, dev_root, path, 7010 ret = btrfs_insert_empty_item(trans, dev_root, path,
7008 &key, sizeof(*ptr)); 7011 &key, sizeof(*ptr));
7009 if (ret < 0) { 7012 if (ret < 0) {
7010 btrfs_warn_in_rcu(dev_root->fs_info, 7013 btrfs_warn_in_rcu(fs_info,
7011 "insert dev_stats item for device %s failed %d", 7014 "insert dev_stats item for device %s failed %d",
7012 rcu_str_deref(device->name), ret); 7015 rcu_str_deref(device->name), ret);
7013 goto out; 7016 goto out;
@@ -7095,21 +7098,20 @@ static void btrfs_dev_stat_print_on_load(struct btrfs_device *dev)
7095int btrfs_get_dev_stats(struct btrfs_root *root, 7098int btrfs_get_dev_stats(struct btrfs_root *root,
7096 struct btrfs_ioctl_get_dev_stats *stats) 7099 struct btrfs_ioctl_get_dev_stats *stats)
7097{ 7100{
7101 struct btrfs_fs_info *fs_info = root->fs_info;
7098 struct btrfs_device *dev; 7102 struct btrfs_device *dev;
7099 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; 7103 struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
7100 int i; 7104 int i;
7101 7105
7102 mutex_lock(&fs_devices->device_list_mutex); 7106 mutex_lock(&fs_devices->device_list_mutex);
7103 dev = btrfs_find_device(root->fs_info, stats->devid, NULL, NULL); 7107 dev = btrfs_find_device(fs_info, stats->devid, NULL, NULL);
7104 mutex_unlock(&fs_devices->device_list_mutex); 7108 mutex_unlock(&fs_devices->device_list_mutex);
7105 7109
7106 if (!dev) { 7110 if (!dev) {
7107 btrfs_warn(root->fs_info, 7111 btrfs_warn(fs_info, "get dev_stats failed, device not found");
7108 "get dev_stats failed, device not found");
7109 return -ENODEV; 7112 return -ENODEV;
7110 } else if (!dev->dev_stats_valid) { 7113 } else if (!dev->dev_stats_valid) {
7111 btrfs_warn(root->fs_info, 7114 btrfs_warn(fs_info, "get dev_stats failed, not yet valid");
7112 "get dev_stats failed, not yet valid");
7113 return -ENODEV; 7115 return -ENODEV;
7114 } else if (stats->flags & BTRFS_DEV_STATS_RESET) { 7116 } else if (stats->flags & BTRFS_DEV_STATS_RESET) {
7115 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) { 7117 for (i = 0; i < BTRFS_DEV_STAT_VALUES_MAX; i++) {
@@ -7186,6 +7188,7 @@ void btrfs_update_commit_device_size(struct btrfs_fs_info *fs_info)
7186void btrfs_update_commit_device_bytes_used(struct btrfs_root *root, 7188void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
7187 struct btrfs_transaction *transaction) 7189 struct btrfs_transaction *transaction)
7188{ 7190{
7191 struct btrfs_fs_info *fs_info = root->fs_info;
7189 struct extent_map *em; 7192 struct extent_map *em;
7190 struct map_lookup *map; 7193 struct map_lookup *map;
7191 struct btrfs_device *dev; 7194 struct btrfs_device *dev;
@@ -7195,7 +7198,7 @@ void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
7195 return; 7198 return;
7196 7199
7197 /* In order to kick the device replace finish process */ 7200 /* In order to kick the device replace finish process */
7198 lock_chunks(root->fs_info); 7201 lock_chunks(fs_info);
7199 list_for_each_entry(em, &transaction->pending_chunks, list) { 7202 list_for_each_entry(em, &transaction->pending_chunks, list) {
7200 map = em->map_lookup; 7203 map = em->map_lookup;
7201 7204
@@ -7204,7 +7207,7 @@ void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
7204 dev->commit_bytes_used = dev->bytes_used; 7207 dev->commit_bytes_used = dev->bytes_used;
7205 } 7208 }
7206 } 7209 }
7207 unlock_chunks(root->fs_info); 7210 unlock_chunks(fs_info);
7208} 7211}
7209 7212
7210void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info) 7213void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info)