aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2016-01-22 14:49:21 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2016-01-22 14:49:21 -0500
commit2101ae42899a14fe7caa73114e2161e778328661 (patch)
treea8ee78aabbcaa5c9549e8eda9883dad0347ddc43 /fs/btrfs
parent391f2a16b74b95da2f05a607f53213fc8ed24b8e (diff)
parenta6111d11b8b5364d02ea2e881c81ce39d004eb32 (diff)
Merge branch 'for-linus-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs
Pull more btrfs updates from Chris Mason: "These are mostly fixes that we've been testing, but also we grabbed and tested a few small cleanups that had been on the list for a while. Zhao Lei's patchset also fixes some early ENOSPC buglets" * 'for-linus-4.5' of git://git.kernel.org/pub/scm/linux/kernel/git/mason/linux-btrfs: (21 commits) btrfs: raid56: Use raid_write_end_io for scrub btrfs: Remove unnecessary ClearPageUptodate for raid56 btrfs: use rbio->nr_pages to reduce calculation btrfs: Use unified stripe_page's index calculation btrfs: Fix calculation of rbio->dbitmap's size calculation btrfs: Fix no_space in write and rm loop btrfs: merge functions for wait snapshot creation btrfs: delete unused argument in btrfs_copy_from_user btrfs: Use direct way to determine raid56 write/recover mode btrfs: Small cleanup for get index_srcdev loop btrfs: Enhance chunk validation check btrfs: Enhance super validation check Btrfs: fix deadlock running delayed iputs at transaction commit time Btrfs: fix typo in log message when starting a balance btrfs: remove duplicate const specifier btrfs: initialize the seq counter in struct btrfs_device Btrfs: clean up an error code in btrfs_init_space_info() btrfs: fix iterator with update error in backref.c Btrfs: fix output of compression message in btrfs_parse_options() Btrfs: Initialize btrfs_root->highest_objectid when loading tree root and subvolume roots ...
Diffstat (limited to 'fs/btrfs')
-rw-r--r--fs/btrfs/backref.c10
-rw-r--r--fs/btrfs/ctree.h3
-rw-r--r--fs/btrfs/dev-replace.c2
-rw-r--r--fs/btrfs/disk-io.c129
-rw-r--r--fs/btrfs/extent-tree.c37
-rw-r--r--fs/btrfs/extent_map.c2
-rw-r--r--fs/btrfs/extent_map.h10
-rw-r--r--fs/btrfs/file.c6
-rw-r--r--fs/btrfs/inode-map.c9
-rw-r--r--fs/btrfs/inode-map.h1
-rw-r--r--fs/btrfs/inode.c24
-rw-r--r--fs/btrfs/ioctl.c4
-rw-r--r--fs/btrfs/raid56.c100
-rw-r--r--fs/btrfs/scrub.c4
-rw-r--r--fs/btrfs/super.c29
-rw-r--r--fs/btrfs/volumes.c107
16 files changed, 264 insertions, 213 deletions
diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 08405a3da6b1..b90cd3776f8e 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -560,13 +560,13 @@ static int __add_missing_keys(struct btrfs_fs_info *fs_info,
560 */ 560 */
561static void __merge_refs(struct list_head *head, int mode) 561static void __merge_refs(struct list_head *head, int mode)
562{ 562{
563 struct __prelim_ref *ref1; 563 struct __prelim_ref *pos1;
564 564
565 list_for_each_entry(ref1, head, list) { 565 list_for_each_entry(pos1, head, list) {
566 struct __prelim_ref *ref2 = ref1, *tmp; 566 struct __prelim_ref *pos2 = pos1, *tmp;
567 567
568 list_for_each_entry_safe_continue(ref2, tmp, head, list) { 568 list_for_each_entry_safe_continue(pos2, tmp, head, list) {
569 struct __prelim_ref *xchg; 569 struct __prelim_ref *xchg, *ref1 = pos1, *ref2 = pos2;
570 struct extent_inode_elem *eie; 570 struct extent_inode_elem *eie;
571 571
572 if (!ref_for_same_block(ref1, ref2)) 572 if (!ref_for_same_block(ref1, ref2))
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 97ad9bbeb35d..bfe4a337fb4d 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1614,7 +1614,7 @@ struct btrfs_fs_info {
1614 1614
1615 spinlock_t delayed_iput_lock; 1615 spinlock_t delayed_iput_lock;
1616 struct list_head delayed_iputs; 1616 struct list_head delayed_iputs;
1617 struct rw_semaphore delayed_iput_sem; 1617 struct mutex cleaner_delayed_iput_mutex;
1618 1618
1619 /* this protects tree_mod_seq_list */ 1619 /* this protects tree_mod_seq_list */
1620 spinlock_t tree_mod_seq_lock; 1620 spinlock_t tree_mod_seq_lock;
@@ -3641,6 +3641,7 @@ int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
3641int __get_raid_index(u64 flags); 3641int __get_raid_index(u64 flags);
3642int btrfs_start_write_no_snapshoting(struct btrfs_root *root); 3642int btrfs_start_write_no_snapshoting(struct btrfs_root *root);
3643void btrfs_end_write_no_snapshoting(struct btrfs_root *root); 3643void btrfs_end_write_no_snapshoting(struct btrfs_root *root);
3644void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
3644void check_system_chunk(struct btrfs_trans_handle *trans, 3645void check_system_chunk(struct btrfs_trans_handle *trans,
3645 struct btrfs_root *root, 3646 struct btrfs_root *root,
3646 const u64 type); 3647 const u64 type);
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index 1e668fb7dd4c..cbb7dbfb3fff 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -614,7 +614,7 @@ static void btrfs_dev_replace_update_device_in_mapping_tree(
614 em = lookup_extent_mapping(em_tree, start, (u64)-1); 614 em = lookup_extent_mapping(em_tree, start, (u64)-1);
615 if (!em) 615 if (!em)
616 break; 616 break;
617 map = (struct map_lookup *)em->bdev; 617 map = em->map_lookup;
618 for (i = 0; i < map->num_stripes; i++) 618 for (i = 0; i < map->num_stripes; i++)
619 if (srcdev == map->stripes[i].dev) 619 if (srcdev == map->stripes[i].dev)
620 map->stripes[i].dev = tgtdev; 620 map->stripes[i].dev = tgtdev;
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index e99ccd6ffb2c..dd08e29f5117 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -55,6 +55,12 @@
55#include <asm/cpufeature.h> 55#include <asm/cpufeature.h>
56#endif 56#endif
57 57
58#define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
59 BTRFS_HEADER_FLAG_RELOC |\
60 BTRFS_SUPER_FLAG_ERROR |\
61 BTRFS_SUPER_FLAG_SEEDING |\
62 BTRFS_SUPER_FLAG_METADUMP)
63
58static const struct extent_io_ops btree_extent_io_ops; 64static const struct extent_io_ops btree_extent_io_ops;
59static void end_workqueue_fn(struct btrfs_work *work); 65static void end_workqueue_fn(struct btrfs_work *work);
60static void free_fs_root(struct btrfs_root *root); 66static void free_fs_root(struct btrfs_root *root);
@@ -1583,8 +1589,23 @@ int btrfs_init_fs_root(struct btrfs_root *root)
1583 ret = get_anon_bdev(&root->anon_dev); 1589 ret = get_anon_bdev(&root->anon_dev);
1584 if (ret) 1590 if (ret)
1585 goto free_writers; 1591 goto free_writers;
1592
1593 mutex_lock(&root->objectid_mutex);
1594 ret = btrfs_find_highest_objectid(root,
1595 &root->highest_objectid);
1596 if (ret) {
1597 mutex_unlock(&root->objectid_mutex);
1598 goto free_root_dev;
1599 }
1600
1601 ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
1602
1603 mutex_unlock(&root->objectid_mutex);
1604
1586 return 0; 1605 return 0;
1587 1606
1607free_root_dev:
1608 free_anon_bdev(root->anon_dev);
1588free_writers: 1609free_writers:
1589 btrfs_free_subvolume_writers(root->subv_writers); 1610 btrfs_free_subvolume_writers(root->subv_writers);
1590fail: 1611fail:
@@ -1786,7 +1807,10 @@ static int cleaner_kthread(void *arg)
1786 goto sleep; 1807 goto sleep;
1787 } 1808 }
1788 1809
1810 mutex_lock(&root->fs_info->cleaner_delayed_iput_mutex);
1789 btrfs_run_delayed_iputs(root); 1811 btrfs_run_delayed_iputs(root);
1812 mutex_unlock(&root->fs_info->cleaner_delayed_iput_mutex);
1813
1790 again = btrfs_clean_one_deleted_snapshot(root); 1814 again = btrfs_clean_one_deleted_snapshot(root);
1791 mutex_unlock(&root->fs_info->cleaner_mutex); 1815 mutex_unlock(&root->fs_info->cleaner_mutex);
1792 1816
@@ -2556,8 +2580,8 @@ int open_ctree(struct super_block *sb,
2556 mutex_init(&fs_info->delete_unused_bgs_mutex); 2580 mutex_init(&fs_info->delete_unused_bgs_mutex);
2557 mutex_init(&fs_info->reloc_mutex); 2581 mutex_init(&fs_info->reloc_mutex);
2558 mutex_init(&fs_info->delalloc_root_mutex); 2582 mutex_init(&fs_info->delalloc_root_mutex);
2583 mutex_init(&fs_info->cleaner_delayed_iput_mutex);
2559 seqlock_init(&fs_info->profiles_lock); 2584 seqlock_init(&fs_info->profiles_lock);
2560 init_rwsem(&fs_info->delayed_iput_sem);
2561 2585
2562 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots); 2586 INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
2563 INIT_LIST_HEAD(&fs_info->space_info); 2587 INIT_LIST_HEAD(&fs_info->space_info);
@@ -2742,26 +2766,6 @@ int open_ctree(struct super_block *sb,
2742 goto fail_alloc; 2766 goto fail_alloc;
2743 } 2767 }
2744 2768
2745 /*
2746 * Leafsize and nodesize were always equal, this is only a sanity check.
2747 */
2748 if (le32_to_cpu(disk_super->__unused_leafsize) !=
2749 btrfs_super_nodesize(disk_super)) {
2750 printk(KERN_ERR "BTRFS: couldn't mount because metadata "
2751 "blocksizes don't match. node %d leaf %d\n",
2752 btrfs_super_nodesize(disk_super),
2753 le32_to_cpu(disk_super->__unused_leafsize));
2754 err = -EINVAL;
2755 goto fail_alloc;
2756 }
2757 if (btrfs_super_nodesize(disk_super) > BTRFS_MAX_METADATA_BLOCKSIZE) {
2758 printk(KERN_ERR "BTRFS: couldn't mount because metadata "
2759 "blocksize (%d) was too large\n",
2760 btrfs_super_nodesize(disk_super));
2761 err = -EINVAL;
2762 goto fail_alloc;
2763 }
2764
2765 features = btrfs_super_incompat_flags(disk_super); 2769 features = btrfs_super_incompat_flags(disk_super);
2766 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF; 2770 features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
2767 if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO) 2771 if (tree_root->fs_info->compress_type == BTRFS_COMPRESS_LZO)
@@ -2833,17 +2837,6 @@ int open_ctree(struct super_block *sb,
2833 sb->s_blocksize = sectorsize; 2837 sb->s_blocksize = sectorsize;
2834 sb->s_blocksize_bits = blksize_bits(sectorsize); 2838 sb->s_blocksize_bits = blksize_bits(sectorsize);
2835 2839
2836 if (btrfs_super_magic(disk_super) != BTRFS_MAGIC) {
2837 printk(KERN_ERR "BTRFS: valid FS not found on %s\n", sb->s_id);
2838 goto fail_sb_buffer;
2839 }
2840
2841 if (sectorsize != PAGE_SIZE) {
2842 printk(KERN_ERR "BTRFS: incompatible sector size (%lu) "
2843 "found on %s\n", (unsigned long)sectorsize, sb->s_id);
2844 goto fail_sb_buffer;
2845 }
2846
2847 mutex_lock(&fs_info->chunk_mutex); 2840 mutex_lock(&fs_info->chunk_mutex);
2848 ret = btrfs_read_sys_array(tree_root); 2841 ret = btrfs_read_sys_array(tree_root);
2849 mutex_unlock(&fs_info->chunk_mutex); 2842 mutex_unlock(&fs_info->chunk_mutex);
@@ -2915,6 +2908,18 @@ retry_root_backup:
2915 tree_root->commit_root = btrfs_root_node(tree_root); 2908 tree_root->commit_root = btrfs_root_node(tree_root);
2916 btrfs_set_root_refs(&tree_root->root_item, 1); 2909 btrfs_set_root_refs(&tree_root->root_item, 1);
2917 2910
2911 mutex_lock(&tree_root->objectid_mutex);
2912 ret = btrfs_find_highest_objectid(tree_root,
2913 &tree_root->highest_objectid);
2914 if (ret) {
2915 mutex_unlock(&tree_root->objectid_mutex);
2916 goto recovery_tree_root;
2917 }
2918
2919 ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
2920
2921 mutex_unlock(&tree_root->objectid_mutex);
2922
2918 ret = btrfs_read_roots(fs_info, tree_root); 2923 ret = btrfs_read_roots(fs_info, tree_root);
2919 if (ret) 2924 if (ret)
2920 goto recovery_tree_root; 2925 goto recovery_tree_root;
@@ -4018,8 +4023,17 @@ static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
4018 int read_only) 4023 int read_only)
4019{ 4024{
4020 struct btrfs_super_block *sb = fs_info->super_copy; 4025 struct btrfs_super_block *sb = fs_info->super_copy;
4026 u64 nodesize = btrfs_super_nodesize(sb);
4027 u64 sectorsize = btrfs_super_sectorsize(sb);
4021 int ret = 0; 4028 int ret = 0;
4022 4029
4030 if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
4031 printk(KERN_ERR "BTRFS: no valid FS found\n");
4032 ret = -EINVAL;
4033 }
4034 if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP)
4035 printk(KERN_WARNING "BTRFS: unrecognized super flag: %llu\n",
4036 btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
4023 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) { 4037 if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
4024 printk(KERN_ERR "BTRFS: tree_root level too big: %d >= %d\n", 4038 printk(KERN_ERR "BTRFS: tree_root level too big: %d >= %d\n",
4025 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL); 4039 btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
@@ -4037,31 +4051,46 @@ static int btrfs_check_super_valid(struct btrfs_fs_info *fs_info,
4037 } 4051 }
4038 4052
4039 /* 4053 /*
4040 * The common minimum, we don't know if we can trust the nodesize/sectorsize 4054 * Check sectorsize and nodesize first, other check will need it.
4041 * items yet, they'll be verified later. Issue just a warning. 4055 * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
4042 */ 4056 */
4043 if (!IS_ALIGNED(btrfs_super_root(sb), 4096)) 4057 if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
4058 sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
4059 printk(KERN_ERR "BTRFS: invalid sectorsize %llu\n", sectorsize);
4060 ret = -EINVAL;
4061 }
4062 /* Only PAGE SIZE is supported yet */
4063 if (sectorsize != PAGE_CACHE_SIZE) {
4064 printk(KERN_ERR "BTRFS: sectorsize %llu not supported yet, only support %lu\n",
4065 sectorsize, PAGE_CACHE_SIZE);
4066 ret = -EINVAL;
4067 }
4068 if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
4069 nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
4070 printk(KERN_ERR "BTRFS: invalid nodesize %llu\n", nodesize);
4071 ret = -EINVAL;
4072 }
4073 if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
4074 printk(KERN_ERR "BTRFS: invalid leafsize %u, should be %llu\n",
4075 le32_to_cpu(sb->__unused_leafsize),
4076 nodesize);
4077 ret = -EINVAL;
4078 }
4079
4080 /* Root alignment check */
4081 if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
4044 printk(KERN_WARNING "BTRFS: tree_root block unaligned: %llu\n", 4082 printk(KERN_WARNING "BTRFS: tree_root block unaligned: %llu\n",
4045 btrfs_super_root(sb)); 4083 btrfs_super_root(sb));
4046 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), 4096)) 4084 ret = -EINVAL;
4085 }
4086 if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
4047 printk(KERN_WARNING "BTRFS: chunk_root block unaligned: %llu\n", 4087 printk(KERN_WARNING "BTRFS: chunk_root block unaligned: %llu\n",
4048 btrfs_super_chunk_root(sb)); 4088 btrfs_super_chunk_root(sb));
4049 if (!IS_ALIGNED(btrfs_super_log_root(sb), 4096))
4050 printk(KERN_WARNING "BTRFS: log_root block unaligned: %llu\n",
4051 btrfs_super_log_root(sb));
4052
4053 /*
4054 * Check the lower bound, the alignment and other constraints are
4055 * checked later.
4056 */
4057 if (btrfs_super_nodesize(sb) < 4096) {
4058 printk(KERN_ERR "BTRFS: nodesize too small: %u < 4096\n",
4059 btrfs_super_nodesize(sb));
4060 ret = -EINVAL; 4089 ret = -EINVAL;
4061 } 4090 }
4062 if (btrfs_super_sectorsize(sb) < 4096) { 4091 if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
4063 printk(KERN_ERR "BTRFS: sectorsize too small: %u < 4096\n", 4092 printk(KERN_WARNING "BTRFS: log_root block unaligned: %llu\n",
4064 btrfs_super_sectorsize(sb)); 4093 btrfs_super_log_root(sb));
4065 ret = -EINVAL; 4094 ret = -EINVAL;
4066 } 4095 }
4067 4096
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 60cc1399c64f..e2287c7c10be 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -4139,8 +4139,10 @@ commit_trans:
4139 !atomic_read(&root->fs_info->open_ioctl_trans)) { 4139 !atomic_read(&root->fs_info->open_ioctl_trans)) {
4140 need_commit--; 4140 need_commit--;
4141 4141
4142 if (need_commit > 0) 4142 if (need_commit > 0) {
4143 btrfs_start_delalloc_roots(fs_info, 0, -1);
4143 btrfs_wait_ordered_roots(fs_info, -1); 4144 btrfs_wait_ordered_roots(fs_info, -1);
4145 }
4144 4146
4145 trans = btrfs_join_transaction(root); 4147 trans = btrfs_join_transaction(root);
4146 if (IS_ERR(trans)) 4148 if (IS_ERR(trans))
@@ -4153,11 +4155,12 @@ commit_trans:
4153 if (ret) 4155 if (ret)
4154 return ret; 4156 return ret;
4155 /* 4157 /*
4156 * make sure that all running delayed iput are 4158 * The cleaner kthread might still be doing iput
4157 * done 4159 * operations. Wait for it to finish so that
4160 * more space is released.
4158 */ 4161 */
4159 down_write(&root->fs_info->delayed_iput_sem); 4162 mutex_lock(&root->fs_info->cleaner_delayed_iput_mutex);
4160 up_write(&root->fs_info->delayed_iput_sem); 4163 mutex_unlock(&root->fs_info->cleaner_delayed_iput_mutex);
4161 goto again; 4164 goto again;
4162 } else { 4165 } else {
4163 btrfs_end_transaction(trans, root); 4166 btrfs_end_transaction(trans, root);
@@ -10399,7 +10402,7 @@ btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info,
10399 * more device items and remove one chunk item), but this is done at 10402 * more device items and remove one chunk item), but this is done at
10400 * btrfs_remove_chunk() through a call to check_system_chunk(). 10403 * btrfs_remove_chunk() through a call to check_system_chunk().
10401 */ 10404 */
10402 map = (struct map_lookup *)em->bdev; 10405 map = em->map_lookup;
10403 num_items = 3 + map->num_stripes; 10406 num_items = 3 + map->num_stripes;
10404 free_extent_map(em); 10407 free_extent_map(em);
10405 10408
@@ -10586,7 +10589,7 @@ int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
10586 10589
10587 disk_super = fs_info->super_copy; 10590 disk_super = fs_info->super_copy;
10588 if (!btrfs_super_root(disk_super)) 10591 if (!btrfs_super_root(disk_super))
10589 return 1; 10592 return -EINVAL;
10590 10593
10591 features = btrfs_super_incompat_flags(disk_super); 10594 features = btrfs_super_incompat_flags(disk_super);
10592 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) 10595 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
@@ -10816,3 +10819,23 @@ int btrfs_start_write_no_snapshoting(struct btrfs_root *root)
10816 } 10819 }
10817 return 1; 10820 return 1;
10818} 10821}
10822
10823static int wait_snapshoting_atomic_t(atomic_t *a)
10824{
10825 schedule();
10826 return 0;
10827}
10828
10829void btrfs_wait_for_snapshot_creation(struct btrfs_root *root)
10830{
10831 while (true) {
10832 int ret;
10833
10834 ret = btrfs_start_write_no_snapshoting(root);
10835 if (ret)
10836 break;
10837 wait_on_atomic_t(&root->will_be_snapshoted,
10838 wait_snapshoting_atomic_t,
10839 TASK_UNINTERRUPTIBLE);
10840 }
10841}
diff --git a/fs/btrfs/extent_map.c b/fs/btrfs/extent_map.c
index 6a98bddd8f33..84fb56d5c018 100644
--- a/fs/btrfs/extent_map.c
+++ b/fs/btrfs/extent_map.c
@@ -76,7 +76,7 @@ void free_extent_map(struct extent_map *em)
76 WARN_ON(extent_map_in_tree(em)); 76 WARN_ON(extent_map_in_tree(em));
77 WARN_ON(!list_empty(&em->list)); 77 WARN_ON(!list_empty(&em->list));
78 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) 78 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
79 kfree(em->bdev); 79 kfree(em->map_lookup);
80 kmem_cache_free(extent_map_cache, em); 80 kmem_cache_free(extent_map_cache, em);
81 } 81 }
82} 82}
diff --git a/fs/btrfs/extent_map.h b/fs/btrfs/extent_map.h
index b2991fd8583e..eb8b8fae036b 100644
--- a/fs/btrfs/extent_map.h
+++ b/fs/btrfs/extent_map.h
@@ -32,7 +32,15 @@ struct extent_map {
32 u64 block_len; 32 u64 block_len;
33 u64 generation; 33 u64 generation;
34 unsigned long flags; 34 unsigned long flags;
35 struct block_device *bdev; 35 union {
36 struct block_device *bdev;
37
38 /*
39 * used for chunk mappings
40 * flags & EXTENT_FLAG_FS_MAPPING must be set
41 */
42 struct map_lookup *map_lookup;
43 };
36 atomic_t refs; 44 atomic_t refs;
37 unsigned int compress_type; 45 unsigned int compress_type;
38 struct list_head list; 46 struct list_head list;
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 83d7859d7619..9f5cc1e8e126 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -406,8 +406,7 @@ int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
406/* simple helper to fault in pages and copy. This should go away 406/* simple helper to fault in pages and copy. This should go away
407 * and be replaced with calls into generic code. 407 * and be replaced with calls into generic code.
408 */ 408 */
409static noinline int btrfs_copy_from_user(loff_t pos, int num_pages, 409static noinline int btrfs_copy_from_user(loff_t pos, size_t write_bytes,
410 size_t write_bytes,
411 struct page **prepared_pages, 410 struct page **prepared_pages,
412 struct iov_iter *i) 411 struct iov_iter *i)
413{ 412{
@@ -1588,8 +1587,7 @@ again:
1588 ret = 0; 1587 ret = 0;
1589 } 1588 }
1590 1589
1591 copied = btrfs_copy_from_user(pos, num_pages, 1590 copied = btrfs_copy_from_user(pos, write_bytes, pages, i);
1592 write_bytes, pages, i);
1593 1591
1594 /* 1592 /*
1595 * if we have trouble faulting in the pages, fall 1593 * if we have trouble faulting in the pages, fall
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index 8b57c17b3fb3..e50316c4af15 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -515,7 +515,7 @@ out:
515 return ret; 515 return ret;
516} 516}
517 517
518static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid) 518int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
519{ 519{
520 struct btrfs_path *path; 520 struct btrfs_path *path;
521 int ret; 521 int ret;
@@ -555,13 +555,6 @@ int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid)
555 int ret; 555 int ret;
556 mutex_lock(&root->objectid_mutex); 556 mutex_lock(&root->objectid_mutex);
557 557
558 if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
559 ret = btrfs_find_highest_objectid(root,
560 &root->highest_objectid);
561 if (ret)
562 goto out;
563 }
564
565 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) { 558 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
566 ret = -ENOSPC; 559 ret = -ENOSPC;
567 goto out; 560 goto out;
diff --git a/fs/btrfs/inode-map.h b/fs/btrfs/inode-map.h
index ddb347bfee23..c8e864b2d530 100644
--- a/fs/btrfs/inode-map.h
+++ b/fs/btrfs/inode-map.h
@@ -9,5 +9,6 @@ int btrfs_save_ino_cache(struct btrfs_root *root,
9 struct btrfs_trans_handle *trans); 9 struct btrfs_trans_handle *trans);
10 10
11int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid); 11int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid);
12int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid);
12 13
13#endif 14#endif
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 247830107686..1b79dc9b12e4 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -3134,7 +3134,6 @@ void btrfs_run_delayed_iputs(struct btrfs_root *root)
3134{ 3134{
3135 struct btrfs_fs_info *fs_info = root->fs_info; 3135 struct btrfs_fs_info *fs_info = root->fs_info;
3136 3136
3137 down_read(&fs_info->delayed_iput_sem);
3138 spin_lock(&fs_info->delayed_iput_lock); 3137 spin_lock(&fs_info->delayed_iput_lock);
3139 while (!list_empty(&fs_info->delayed_iputs)) { 3138 while (!list_empty(&fs_info->delayed_iputs)) {
3140 struct btrfs_inode *inode; 3139 struct btrfs_inode *inode;
@@ -3153,7 +3152,6 @@ void btrfs_run_delayed_iputs(struct btrfs_root *root)
3153 spin_lock(&fs_info->delayed_iput_lock); 3152 spin_lock(&fs_info->delayed_iput_lock);
3154 } 3153 }
3155 spin_unlock(&fs_info->delayed_iput_lock); 3154 spin_unlock(&fs_info->delayed_iput_lock);
3156 up_read(&root->fs_info->delayed_iput_sem);
3157} 3155}
3158 3156
3159/* 3157/*
@@ -4874,26 +4872,6 @@ next:
4874 return err; 4872 return err;
4875} 4873}
4876 4874
4877static int wait_snapshoting_atomic_t(atomic_t *a)
4878{
4879 schedule();
4880 return 0;
4881}
4882
4883static void wait_for_snapshot_creation(struct btrfs_root *root)
4884{
4885 while (true) {
4886 int ret;
4887
4888 ret = btrfs_start_write_no_snapshoting(root);
4889 if (ret)
4890 break;
4891 wait_on_atomic_t(&root->will_be_snapshoted,
4892 wait_snapshoting_atomic_t,
4893 TASK_UNINTERRUPTIBLE);
4894 }
4895}
4896
4897static int btrfs_setsize(struct inode *inode, struct iattr *attr) 4875static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4898{ 4876{
4899 struct btrfs_root *root = BTRFS_I(inode)->root; 4877 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -4925,7 +4903,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4925 * truncation, it must capture all writes that happened before 4903 * truncation, it must capture all writes that happened before
4926 * this truncation. 4904 * this truncation.
4927 */ 4905 */
4928 wait_for_snapshot_creation(root); 4906 btrfs_wait_for_snapshot_creation(root);
4929 ret = btrfs_cont_expand(inode, oldsize, newsize); 4907 ret = btrfs_cont_expand(inode, oldsize, newsize);
4930 if (ret) { 4908 if (ret) {
4931 btrfs_end_write_no_snapshoting(root); 4909 btrfs_end_write_no_snapshoting(root);
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 2a47a3148ec8..9028737ee9b5 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -568,6 +568,10 @@ static noinline int create_subvol(struct inode *dir,
568 goto fail; 568 goto fail;
569 } 569 }
570 570
571 mutex_lock(&new_root->objectid_mutex);
572 new_root->highest_objectid = new_dirid;
573 mutex_unlock(&new_root->objectid_mutex);
574
571 /* 575 /*
572 * insert the directory item 576 * insert the directory item
573 */ 577 */
diff --git a/fs/btrfs/raid56.c b/fs/btrfs/raid56.c
index 6d707545f775..55161369fab1 100644
--- a/fs/btrfs/raid56.c
+++ b/fs/btrfs/raid56.c
@@ -609,13 +609,28 @@ static int rbio_can_merge(struct btrfs_raid_bio *last,
609 return 1; 609 return 1;
610} 610}
611 611
612static int rbio_stripe_page_index(struct btrfs_raid_bio *rbio, int stripe,
613 int index)
614{
615 return stripe * rbio->stripe_npages + index;
616}
617
618/*
619 * these are just the pages from the rbio array, not from anything
620 * the FS sent down to us
621 */
622static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe,
623 int index)
624{
625 return rbio->stripe_pages[rbio_stripe_page_index(rbio, stripe, index)];
626}
627
612/* 628/*
613 * helper to index into the pstripe 629 * helper to index into the pstripe
614 */ 630 */
615static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index) 631static struct page *rbio_pstripe_page(struct btrfs_raid_bio *rbio, int index)
616{ 632{
617 index += (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT; 633 return rbio_stripe_page(rbio, rbio->nr_data, index);
618 return rbio->stripe_pages[index];
619} 634}
620 635
621/* 636/*
@@ -626,10 +641,7 @@ static struct page *rbio_qstripe_page(struct btrfs_raid_bio *rbio, int index)
626{ 641{
627 if (rbio->nr_data + 1 == rbio->real_stripes) 642 if (rbio->nr_data + 1 == rbio->real_stripes)
628 return NULL; 643 return NULL;
629 644 return rbio_stripe_page(rbio, rbio->nr_data + 1, index);
630 index += ((rbio->nr_data + 1) * rbio->stripe_len) >>
631 PAGE_CACHE_SHIFT;
632 return rbio->stripe_pages[index];
633} 645}
634 646
635/* 647/*
@@ -889,6 +901,7 @@ static void raid_write_end_io(struct bio *bio)
889{ 901{
890 struct btrfs_raid_bio *rbio = bio->bi_private; 902 struct btrfs_raid_bio *rbio = bio->bi_private;
891 int err = bio->bi_error; 903 int err = bio->bi_error;
904 int max_errors;
892 905
893 if (err) 906 if (err)
894 fail_bio_stripe(rbio, bio); 907 fail_bio_stripe(rbio, bio);
@@ -901,7 +914,9 @@ static void raid_write_end_io(struct bio *bio)
901 err = 0; 914 err = 0;
902 915
903 /* OK, we have read all the stripes we need to. */ 916 /* OK, we have read all the stripes we need to. */
904 if (atomic_read(&rbio->error) > rbio->bbio->max_errors) 917 max_errors = (rbio->operation == BTRFS_RBIO_PARITY_SCRUB) ?
918 0 : rbio->bbio->max_errors;
919 if (atomic_read(&rbio->error) > max_errors)
905 err = -EIO; 920 err = -EIO;
906 921
907 rbio_orig_end_io(rbio, err); 922 rbio_orig_end_io(rbio, err);
@@ -947,8 +962,7 @@ static struct page *page_in_rbio(struct btrfs_raid_bio *rbio,
947 */ 962 */
948static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes) 963static unsigned long rbio_nr_pages(unsigned long stripe_len, int nr_stripes)
949{ 964{
950 unsigned long nr = stripe_len * nr_stripes; 965 return DIV_ROUND_UP(stripe_len, PAGE_CACHE_SIZE) * nr_stripes;
951 return DIV_ROUND_UP(nr, PAGE_CACHE_SIZE);
952} 966}
953 967
954/* 968/*
@@ -966,8 +980,8 @@ static struct btrfs_raid_bio *alloc_rbio(struct btrfs_root *root,
966 void *p; 980 void *p;
967 981
968 rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2 + 982 rbio = kzalloc(sizeof(*rbio) + num_pages * sizeof(struct page *) * 2 +
969 DIV_ROUND_UP(stripe_npages, BITS_PER_LONG / 8), 983 DIV_ROUND_UP(stripe_npages, BITS_PER_LONG) *
970 GFP_NOFS); 984 sizeof(long), GFP_NOFS);
971 if (!rbio) 985 if (!rbio)
972 return ERR_PTR(-ENOMEM); 986 return ERR_PTR(-ENOMEM);
973 987
@@ -1021,18 +1035,17 @@ static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
1021 if (!page) 1035 if (!page)
1022 return -ENOMEM; 1036 return -ENOMEM;
1023 rbio->stripe_pages[i] = page; 1037 rbio->stripe_pages[i] = page;
1024 ClearPageUptodate(page);
1025 } 1038 }
1026 return 0; 1039 return 0;
1027} 1040}
1028 1041
1029/* allocate pages for just the p/q stripes */ 1042/* only allocate pages for p/q stripes */
1030static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio) 1043static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
1031{ 1044{
1032 int i; 1045 int i;
1033 struct page *page; 1046 struct page *page;
1034 1047
1035 i = (rbio->nr_data * rbio->stripe_len) >> PAGE_CACHE_SHIFT; 1048 i = rbio_stripe_page_index(rbio, rbio->nr_data, 0);
1036 1049
1037 for (; i < rbio->nr_pages; i++) { 1050 for (; i < rbio->nr_pages; i++) {
1038 if (rbio->stripe_pages[i]) 1051 if (rbio->stripe_pages[i])
@@ -1121,18 +1134,6 @@ static void validate_rbio_for_rmw(struct btrfs_raid_bio *rbio)
1121} 1134}
1122 1135
1123/* 1136/*
1124 * these are just the pages from the rbio array, not from anything
1125 * the FS sent down to us
1126 */
1127static struct page *rbio_stripe_page(struct btrfs_raid_bio *rbio, int stripe, int page)
1128{
1129 int index;
1130 index = stripe * (rbio->stripe_len >> PAGE_CACHE_SHIFT);
1131 index += page;
1132 return rbio->stripe_pages[index];
1133}
1134
1135/*
1136 * helper function to walk our bio list and populate the bio_pages array with 1137 * helper function to walk our bio list and populate the bio_pages array with
1137 * the result. This seems expensive, but it is faster than constantly 1138 * the result. This seems expensive, but it is faster than constantly
1138 * searching through the bio list as we setup the IO in finish_rmw or stripe 1139 * searching through the bio list as we setup the IO in finish_rmw or stripe
@@ -1175,7 +1176,6 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1175{ 1176{
1176 struct btrfs_bio *bbio = rbio->bbio; 1177 struct btrfs_bio *bbio = rbio->bbio;
1177 void *pointers[rbio->real_stripes]; 1178 void *pointers[rbio->real_stripes];
1178 int stripe_len = rbio->stripe_len;
1179 int nr_data = rbio->nr_data; 1179 int nr_data = rbio->nr_data;
1180 int stripe; 1180 int stripe;
1181 int pagenr; 1181 int pagenr;
@@ -1183,7 +1183,6 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1183 int q_stripe = -1; 1183 int q_stripe = -1;
1184 struct bio_list bio_list; 1184 struct bio_list bio_list;
1185 struct bio *bio; 1185 struct bio *bio;
1186 int pages_per_stripe = stripe_len >> PAGE_CACHE_SHIFT;
1187 int ret; 1186 int ret;
1188 1187
1189 bio_list_init(&bio_list); 1188 bio_list_init(&bio_list);
@@ -1226,7 +1225,7 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1226 else 1225 else
1227 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags); 1226 clear_bit(RBIO_CACHE_READY_BIT, &rbio->flags);
1228 1227
1229 for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) { 1228 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1230 struct page *p; 1229 struct page *p;
1231 /* first collect one page from each data stripe */ 1230 /* first collect one page from each data stripe */
1232 for (stripe = 0; stripe < nr_data; stripe++) { 1231 for (stripe = 0; stripe < nr_data; stripe++) {
@@ -1268,7 +1267,7 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1268 * everything else. 1267 * everything else.
1269 */ 1268 */
1270 for (stripe = 0; stripe < rbio->real_stripes; stripe++) { 1269 for (stripe = 0; stripe < rbio->real_stripes; stripe++) {
1271 for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) { 1270 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1272 struct page *page; 1271 struct page *page;
1273 if (stripe < rbio->nr_data) { 1272 if (stripe < rbio->nr_data) {
1274 page = page_in_rbio(rbio, stripe, pagenr, 1); 1273 page = page_in_rbio(rbio, stripe, pagenr, 1);
@@ -1292,7 +1291,7 @@ static noinline void finish_rmw(struct btrfs_raid_bio *rbio)
1292 if (!bbio->tgtdev_map[stripe]) 1291 if (!bbio->tgtdev_map[stripe])
1293 continue; 1292 continue;
1294 1293
1295 for (pagenr = 0; pagenr < pages_per_stripe; pagenr++) { 1294 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1296 struct page *page; 1295 struct page *page;
1297 if (stripe < rbio->nr_data) { 1296 if (stripe < rbio->nr_data) {
1298 page = page_in_rbio(rbio, stripe, pagenr, 1); 1297 page = page_in_rbio(rbio, stripe, pagenr, 1);
@@ -1506,7 +1505,6 @@ static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1506 int bios_to_read = 0; 1505 int bios_to_read = 0;
1507 struct bio_list bio_list; 1506 struct bio_list bio_list;
1508 int ret; 1507 int ret;
1509 int nr_pages = DIV_ROUND_UP(rbio->stripe_len, PAGE_CACHE_SIZE);
1510 int pagenr; 1508 int pagenr;
1511 int stripe; 1509 int stripe;
1512 struct bio *bio; 1510 struct bio *bio;
@@ -1525,7 +1523,7 @@ static int raid56_rmw_stripe(struct btrfs_raid_bio *rbio)
1525 * stripe 1523 * stripe
1526 */ 1524 */
1527 for (stripe = 0; stripe < rbio->nr_data; stripe++) { 1525 for (stripe = 0; stripe < rbio->nr_data; stripe++) {
1528 for (pagenr = 0; pagenr < nr_pages; pagenr++) { 1526 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1529 struct page *page; 1527 struct page *page;
1530 /* 1528 /*
1531 * we want to find all the pages missing from 1529 * we want to find all the pages missing from
@@ -1801,7 +1799,6 @@ static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
1801 int pagenr, stripe; 1799 int pagenr, stripe;
1802 void **pointers; 1800 void **pointers;
1803 int faila = -1, failb = -1; 1801 int faila = -1, failb = -1;
1804 int nr_pages = DIV_ROUND_UP(rbio->stripe_len, PAGE_CACHE_SIZE);
1805 struct page *page; 1802 struct page *page;
1806 int err; 1803 int err;
1807 int i; 1804 int i;
@@ -1824,7 +1821,7 @@ static void __raid_recover_end_io(struct btrfs_raid_bio *rbio)
1824 1821
1825 index_rbio_pages(rbio); 1822 index_rbio_pages(rbio);
1826 1823
1827 for (pagenr = 0; pagenr < nr_pages; pagenr++) { 1824 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
1828 /* 1825 /*
1829 * Now we just use bitmap to mark the horizontal stripes in 1826 * Now we just use bitmap to mark the horizontal stripes in
1830 * which we have data when doing parity scrub. 1827 * which we have data when doing parity scrub.
@@ -1935,7 +1932,7 @@ pstripe:
1935 * other endio functions will fiddle the uptodate bits 1932 * other endio functions will fiddle the uptodate bits
1936 */ 1933 */
1937 if (rbio->operation == BTRFS_RBIO_WRITE) { 1934 if (rbio->operation == BTRFS_RBIO_WRITE) {
1938 for (i = 0; i < nr_pages; i++) { 1935 for (i = 0; i < rbio->stripe_npages; i++) {
1939 if (faila != -1) { 1936 if (faila != -1) {
1940 page = rbio_stripe_page(rbio, faila, i); 1937 page = rbio_stripe_page(rbio, faila, i);
1941 SetPageUptodate(page); 1938 SetPageUptodate(page);
@@ -2031,7 +2028,6 @@ static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2031 int bios_to_read = 0; 2028 int bios_to_read = 0;
2032 struct bio_list bio_list; 2029 struct bio_list bio_list;
2033 int ret; 2030 int ret;
2034 int nr_pages = DIV_ROUND_UP(rbio->stripe_len, PAGE_CACHE_SIZE);
2035 int pagenr; 2031 int pagenr;
2036 int stripe; 2032 int stripe;
2037 struct bio *bio; 2033 struct bio *bio;
@@ -2055,7 +2051,7 @@ static int __raid56_parity_recover(struct btrfs_raid_bio *rbio)
2055 continue; 2051 continue;
2056 } 2052 }
2057 2053
2058 for (pagenr = 0; pagenr < nr_pages; pagenr++) { 2054 for (pagenr = 0; pagenr < rbio->stripe_npages; pagenr++) {
2059 struct page *p; 2055 struct page *p;
2060 2056
2061 /* 2057 /*
@@ -2279,37 +2275,11 @@ static int alloc_rbio_essential_pages(struct btrfs_raid_bio *rbio)
2279 if (!page) 2275 if (!page)
2280 return -ENOMEM; 2276 return -ENOMEM;
2281 rbio->stripe_pages[index] = page; 2277 rbio->stripe_pages[index] = page;
2282 ClearPageUptodate(page);
2283 } 2278 }
2284 } 2279 }
2285 return 0; 2280 return 0;
2286} 2281}
2287 2282
2288/*
2289 * end io function used by finish_rmw. When we finally
2290 * get here, we've written a full stripe
2291 */
2292static void raid_write_parity_end_io(struct bio *bio)
2293{
2294 struct btrfs_raid_bio *rbio = bio->bi_private;
2295 int err = bio->bi_error;
2296
2297 if (bio->bi_error)
2298 fail_bio_stripe(rbio, bio);
2299
2300 bio_put(bio);
2301
2302 if (!atomic_dec_and_test(&rbio->stripes_pending))
2303 return;
2304
2305 err = 0;
2306
2307 if (atomic_read(&rbio->error))
2308 err = -EIO;
2309
2310 rbio_orig_end_io(rbio, err);
2311}
2312
2313static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio, 2283static noinline void finish_parity_scrub(struct btrfs_raid_bio *rbio,
2314 int need_check) 2284 int need_check)
2315{ 2285{
@@ -2462,7 +2432,7 @@ submit_write:
2462 break; 2432 break;
2463 2433
2464 bio->bi_private = rbio; 2434 bio->bi_private = rbio;
2465 bio->bi_end_io = raid_write_parity_end_io; 2435 bio->bi_end_io = raid_write_end_io;
2466 submit_bio(WRITE, bio); 2436 submit_bio(WRITE, bio);
2467 } 2437 }
2468 return; 2438 return;
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 0c981ebe2acb..b1a68530e911 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2813,7 +2813,7 @@ out:
2813 2813
2814static inline int scrub_calc_parity_bitmap_len(int nsectors) 2814static inline int scrub_calc_parity_bitmap_len(int nsectors)
2815{ 2815{
2816 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * (BITS_PER_LONG / 8); 2816 return DIV_ROUND_UP(nsectors, BITS_PER_LONG) * sizeof(long);
2817} 2817}
2818 2818
2819static void scrub_parity_get(struct scrub_parity *sparity) 2819static void scrub_parity_get(struct scrub_parity *sparity)
@@ -3458,7 +3458,7 @@ static noinline_for_stack int scrub_chunk(struct scrub_ctx *sctx,
3458 return ret; 3458 return ret;
3459 } 3459 }
3460 3460
3461 map = (struct map_lookup *)em->bdev; 3461 map = em->map_lookup;
3462 if (em->start != chunk_offset) 3462 if (em->start != chunk_offset)
3463 goto out; 3463 goto out;
3464 3464
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 9b9eab6d048e..d41e09fe8e38 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -383,6 +383,9 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
383 int ret = 0; 383 int ret = 0;
384 char *compress_type; 384 char *compress_type;
385 bool compress_force = false; 385 bool compress_force = false;
386 enum btrfs_compression_type saved_compress_type;
387 bool saved_compress_force;
388 int no_compress = 0;
386 389
387 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 390 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
388 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) 391 if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE))
@@ -462,6 +465,10 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
462 /* Fallthrough */ 465 /* Fallthrough */
463 case Opt_compress: 466 case Opt_compress:
464 case Opt_compress_type: 467 case Opt_compress_type:
468 saved_compress_type = btrfs_test_opt(root, COMPRESS) ?
469 info->compress_type : BTRFS_COMPRESS_NONE;
470 saved_compress_force =
471 btrfs_test_opt(root, FORCE_COMPRESS);
465 if (token == Opt_compress || 472 if (token == Opt_compress ||
466 token == Opt_compress_force || 473 token == Opt_compress_force ||
467 strcmp(args[0].from, "zlib") == 0) { 474 strcmp(args[0].from, "zlib") == 0) {
@@ -470,6 +477,7 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
470 btrfs_set_opt(info->mount_opt, COMPRESS); 477 btrfs_set_opt(info->mount_opt, COMPRESS);
471 btrfs_clear_opt(info->mount_opt, NODATACOW); 478 btrfs_clear_opt(info->mount_opt, NODATACOW);
472 btrfs_clear_opt(info->mount_opt, NODATASUM); 479 btrfs_clear_opt(info->mount_opt, NODATASUM);
480 no_compress = 0;
473 } else if (strcmp(args[0].from, "lzo") == 0) { 481 } else if (strcmp(args[0].from, "lzo") == 0) {
474 compress_type = "lzo"; 482 compress_type = "lzo";
475 info->compress_type = BTRFS_COMPRESS_LZO; 483 info->compress_type = BTRFS_COMPRESS_LZO;
@@ -477,25 +485,21 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
477 btrfs_clear_opt(info->mount_opt, NODATACOW); 485 btrfs_clear_opt(info->mount_opt, NODATACOW);
478 btrfs_clear_opt(info->mount_opt, NODATASUM); 486 btrfs_clear_opt(info->mount_opt, NODATASUM);
479 btrfs_set_fs_incompat(info, COMPRESS_LZO); 487 btrfs_set_fs_incompat(info, COMPRESS_LZO);
488 no_compress = 0;
480 } else if (strncmp(args[0].from, "no", 2) == 0) { 489 } else if (strncmp(args[0].from, "no", 2) == 0) {
481 compress_type = "no"; 490 compress_type = "no";
482 btrfs_clear_opt(info->mount_opt, COMPRESS); 491 btrfs_clear_opt(info->mount_opt, COMPRESS);
483 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 492 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
484 compress_force = false; 493 compress_force = false;
494 no_compress++;
485 } else { 495 } else {
486 ret = -EINVAL; 496 ret = -EINVAL;
487 goto out; 497 goto out;
488 } 498 }
489 499
490 if (compress_force) { 500 if (compress_force) {
491 btrfs_set_and_info(root, FORCE_COMPRESS, 501 btrfs_set_opt(info->mount_opt, FORCE_COMPRESS);
492 "force %s compression",
493 compress_type);
494 } else { 502 } else {
495 if (!btrfs_test_opt(root, COMPRESS))
496 btrfs_info(root->fs_info,
497 "btrfs: use %s compression",
498 compress_type);
499 /* 503 /*
500 * If we remount from compress-force=xxx to 504 * If we remount from compress-force=xxx to
501 * compress=xxx, we need clear FORCE_COMPRESS 505 * compress=xxx, we need clear FORCE_COMPRESS
@@ -504,6 +508,17 @@ int btrfs_parse_options(struct btrfs_root *root, char *options)
504 */ 508 */
505 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS); 509 btrfs_clear_opt(info->mount_opt, FORCE_COMPRESS);
506 } 510 }
511 if ((btrfs_test_opt(root, COMPRESS) &&
512 (info->compress_type != saved_compress_type ||
513 compress_force != saved_compress_force)) ||
514 (!btrfs_test_opt(root, COMPRESS) &&
515 no_compress == 1)) {
516 btrfs_info(root->fs_info,
517 "%s %s compression",
518 (compress_force) ? "force" : "use",
519 compress_type);
520 }
521 compress_force = false;
507 break; 522 break;
508 case Opt_ssd: 523 case Opt_ssd:
509 btrfs_set_and_info(root, SSD, 524 btrfs_set_and_info(root, SSD,
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index c32abbca9d77..366b335946fa 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -108,7 +108,7 @@ const struct btrfs_raid_attr btrfs_raid_array[BTRFS_NR_RAID_TYPES] = {
108 }, 108 },
109}; 109};
110 110
111const u64 const btrfs_raid_group[BTRFS_NR_RAID_TYPES] = { 111const u64 btrfs_raid_group[BTRFS_NR_RAID_TYPES] = {
112 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10, 112 [BTRFS_RAID_RAID10] = BTRFS_BLOCK_GROUP_RAID10,
113 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1, 113 [BTRFS_RAID_RAID1] = BTRFS_BLOCK_GROUP_RAID1,
114 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP, 114 [BTRFS_RAID_DUP] = BTRFS_BLOCK_GROUP_DUP,
@@ -233,6 +233,7 @@ static struct btrfs_device *__alloc_device(void)
233 spin_lock_init(&dev->reada_lock); 233 spin_lock_init(&dev->reada_lock);
234 atomic_set(&dev->reada_in_flight, 0); 234 atomic_set(&dev->reada_in_flight, 0);
235 atomic_set(&dev->dev_stats_ccnt, 0); 235 atomic_set(&dev->dev_stats_ccnt, 0);
236 btrfs_device_data_ordered_init(dev);
236 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 237 INIT_RADIX_TREE(&dev->reada_zones, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
237 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM); 238 INIT_RADIX_TREE(&dev->reada_extents, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
238 239
@@ -1183,7 +1184,7 @@ again:
1183 struct map_lookup *map; 1184 struct map_lookup *map;
1184 int i; 1185 int i;
1185 1186
1186 map = (struct map_lookup *)em->bdev; 1187 map = em->map_lookup;
1187 for (i = 0; i < map->num_stripes; i++) { 1188 for (i = 0; i < map->num_stripes; i++) {
1188 u64 end; 1189 u64 end;
1189 1190
@@ -2755,7 +2756,7 @@ int btrfs_remove_chunk(struct btrfs_trans_handle *trans,
2755 free_extent_map(em); 2756 free_extent_map(em);
2756 return -EINVAL; 2757 return -EINVAL;
2757 } 2758 }
2758 map = (struct map_lookup *)em->bdev; 2759 map = em->map_lookup;
2759 lock_chunks(root->fs_info->chunk_root); 2760 lock_chunks(root->fs_info->chunk_root);
2760 check_system_chunk(trans, extent_root, map->type); 2761 check_system_chunk(trans, extent_root, map->type);
2761 unlock_chunks(root->fs_info->chunk_root); 2762 unlock_chunks(root->fs_info->chunk_root);
@@ -3751,7 +3752,7 @@ int btrfs_balance(struct btrfs_balance_control *bctl,
3751 if (btrfs_get_num_tolerated_disk_barrier_failures(bctl->meta.target) < 3752 if (btrfs_get_num_tolerated_disk_barrier_failures(bctl->meta.target) <
3752 btrfs_get_num_tolerated_disk_barrier_failures(bctl->data.target)) { 3753 btrfs_get_num_tolerated_disk_barrier_failures(bctl->data.target)) {
3753 btrfs_warn(fs_info, 3754 btrfs_warn(fs_info,
3754 "metatdata profile 0x%llx has lower redundancy than data profile 0x%llx", 3755 "metadata profile 0x%llx has lower redundancy than data profile 0x%llx",
3755 bctl->meta.target, bctl->data.target); 3756 bctl->meta.target, bctl->data.target);
3756 } 3757 }
3757 3758
@@ -4718,7 +4719,7 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4718 goto error; 4719 goto error;
4719 } 4720 }
4720 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); 4721 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4721 em->bdev = (struct block_device *)map; 4722 em->map_lookup = map;
4722 em->start = start; 4723 em->start = start;
4723 em->len = num_bytes; 4724 em->len = num_bytes;
4724 em->block_start = 0; 4725 em->block_start = 0;
@@ -4813,7 +4814,7 @@ int btrfs_finish_chunk_alloc(struct btrfs_trans_handle *trans,
4813 return -EINVAL; 4814 return -EINVAL;
4814 } 4815 }
4815 4816
4816 map = (struct map_lookup *)em->bdev; 4817 map = em->map_lookup;
4817 item_size = btrfs_chunk_item_size(map->num_stripes); 4818 item_size = btrfs_chunk_item_size(map->num_stripes);
4818 stripe_size = em->orig_block_len; 4819 stripe_size = em->orig_block_len;
4819 4820
@@ -4968,7 +4969,7 @@ int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
4968 if (!em) 4969 if (!em)
4969 return 1; 4970 return 1;
4970 4971
4971 map = (struct map_lookup *)em->bdev; 4972 map = em->map_lookup;
4972 for (i = 0; i < map->num_stripes; i++) { 4973 for (i = 0; i < map->num_stripes; i++) {
4973 if (map->stripes[i].dev->missing) { 4974 if (map->stripes[i].dev->missing) {
4974 miss_ndevs++; 4975 miss_ndevs++;
@@ -5048,7 +5049,7 @@ int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
5048 return 1; 5049 return 1;
5049 } 5050 }
5050 5051
5051 map = (struct map_lookup *)em->bdev; 5052 map = em->map_lookup;
5052 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1)) 5053 if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
5053 ret = map->num_stripes; 5054 ret = map->num_stripes;
5054 else if (map->type & BTRFS_BLOCK_GROUP_RAID10) 5055 else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
@@ -5084,7 +5085,7 @@ unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
5084 BUG_ON(!em); 5085 BUG_ON(!em);
5085 5086
5086 BUG_ON(em->start > logical || em->start + em->len < logical); 5087 BUG_ON(em->start > logical || em->start + em->len < logical);
5087 map = (struct map_lookup *)em->bdev; 5088 map = em->map_lookup;
5088 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) 5089 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5089 len = map->stripe_len * nr_data_stripes(map); 5090 len = map->stripe_len * nr_data_stripes(map);
5090 free_extent_map(em); 5091 free_extent_map(em);
@@ -5105,7 +5106,7 @@ int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
5105 BUG_ON(!em); 5106 BUG_ON(!em);
5106 5107
5107 BUG_ON(em->start > logical || em->start + em->len < logical); 5108 BUG_ON(em->start > logical || em->start + em->len < logical);
5108 map = (struct map_lookup *)em->bdev; 5109 map = em->map_lookup;
5109 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) 5110 if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK)
5110 ret = 1; 5111 ret = 1;
5111 free_extent_map(em); 5112 free_extent_map(em);
@@ -5264,7 +5265,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5264 return -EINVAL; 5265 return -EINVAL;
5265 } 5266 }
5266 5267
5267 map = (struct map_lookup *)em->bdev; 5268 map = em->map_lookup;
5268 offset = logical - em->start; 5269 offset = logical - em->start;
5269 5270
5270 stripe_len = map->stripe_len; 5271 stripe_len = map->stripe_len;
@@ -5378,35 +5379,33 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
5378 * target drive. 5379 * target drive.
5379 */ 5380 */
5380 for (i = 0; i < tmp_num_stripes; i++) { 5381 for (i = 0; i < tmp_num_stripes; i++) {
5381 if (tmp_bbio->stripes[i].dev->devid == srcdev_devid) { 5382 if (tmp_bbio->stripes[i].dev->devid != srcdev_devid)
5382 /* 5383 continue;
5383 * In case of DUP, in order to keep it 5384
5384 * simple, only add the mirror with the 5385 /*
5385 * lowest physical address 5386 * In case of DUP, in order to keep it simple, only add
5386 */ 5387 * the mirror with the lowest physical address
5387 if (found && 5388 */
5388 physical_of_found <= 5389 if (found &&
5389 tmp_bbio->stripes[i].physical) 5390 physical_of_found <= tmp_bbio->stripes[i].physical)
5390 continue; 5391 continue;
5391 index_srcdev = i; 5392
5392 found = 1; 5393 index_srcdev = i;
5393 physical_of_found = 5394 found = 1;
5394 tmp_bbio->stripes[i].physical; 5395 physical_of_found = tmp_bbio->stripes[i].physical;
5395 }
5396 } 5396 }
5397 5397
5398 if (found) { 5398 btrfs_put_bbio(tmp_bbio);
5399 mirror_num = index_srcdev + 1; 5399
5400 patch_the_first_stripe_for_dev_replace = 1; 5400 if (!found) {
5401 physical_to_patch_in_first_stripe = physical_of_found;
5402 } else {
5403 WARN_ON(1); 5401 WARN_ON(1);
5404 ret = -EIO; 5402 ret = -EIO;
5405 btrfs_put_bbio(tmp_bbio);
5406 goto out; 5403 goto out;
5407 } 5404 }
5408 5405
5409 btrfs_put_bbio(tmp_bbio); 5406 mirror_num = index_srcdev + 1;
5407 patch_the_first_stripe_for_dev_replace = 1;
5408 physical_to_patch_in_first_stripe = physical_of_found;
5410 } else if (mirror_num > map->num_stripes) { 5409 } else if (mirror_num > map->num_stripes) {
5411 mirror_num = 0; 5410 mirror_num = 0;
5412 } 5411 }
@@ -5806,7 +5805,7 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5806 free_extent_map(em); 5805 free_extent_map(em);
5807 return -EIO; 5806 return -EIO;
5808 } 5807 }
5809 map = (struct map_lookup *)em->bdev; 5808 map = em->map_lookup;
5810 5809
5811 length = em->len; 5810 length = em->len;
5812 rmap_len = map->stripe_len; 5811 rmap_len = map->stripe_len;
@@ -6069,7 +6068,8 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
6069 bbio->fs_info = root->fs_info; 6068 bbio->fs_info = root->fs_info;
6070 atomic_set(&bbio->stripes_pending, bbio->num_stripes); 6069 atomic_set(&bbio->stripes_pending, bbio->num_stripes);
6071 6070
6072 if (bbio->raid_map) { 6071 if ((bbio->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) &&
6072 ((rw & WRITE) || (mirror_num > 1))) {
6073 /* In this case, map_length has been set to the length of 6073 /* In this case, map_length has been set to the length of
6074 a single stripe; not the whole write */ 6074 a single stripe; not the whole write */
6075 if (rw & WRITE) { 6075 if (rw & WRITE) {
@@ -6210,6 +6210,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6210 struct extent_map *em; 6210 struct extent_map *em;
6211 u64 logical; 6211 u64 logical;
6212 u64 length; 6212 u64 length;
6213 u64 stripe_len;
6213 u64 devid; 6214 u64 devid;
6214 u8 uuid[BTRFS_UUID_SIZE]; 6215 u8 uuid[BTRFS_UUID_SIZE];
6215 int num_stripes; 6216 int num_stripes;
@@ -6218,6 +6219,37 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6218 6219
6219 logical = key->offset; 6220 logical = key->offset;
6220 length = btrfs_chunk_length(leaf, chunk); 6221 length = btrfs_chunk_length(leaf, chunk);
6222 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
6223 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6224 /* Validation check */
6225 if (!num_stripes) {
6226 btrfs_err(root->fs_info, "invalid chunk num_stripes: %u",
6227 num_stripes);
6228 return -EIO;
6229 }
6230 if (!IS_ALIGNED(logical, root->sectorsize)) {
6231 btrfs_err(root->fs_info,
6232 "invalid chunk logical %llu", logical);
6233 return -EIO;
6234 }
6235 if (!length || !IS_ALIGNED(length, root->sectorsize)) {
6236 btrfs_err(root->fs_info,
6237 "invalid chunk length %llu", length);
6238 return -EIO;
6239 }
6240 if (!is_power_of_2(stripe_len)) {
6241 btrfs_err(root->fs_info, "invalid chunk stripe length: %llu",
6242 stripe_len);
6243 return -EIO;
6244 }
6245 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6246 btrfs_chunk_type(leaf, chunk)) {
6247 btrfs_err(root->fs_info, "unrecognized chunk type: %llu",
6248 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
6249 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
6250 btrfs_chunk_type(leaf, chunk));
6251 return -EIO;
6252 }
6221 6253
6222 read_lock(&map_tree->map_tree.lock); 6254 read_lock(&map_tree->map_tree.lock);
6223 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1); 6255 em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
@@ -6234,7 +6266,6 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6234 em = alloc_extent_map(); 6266 em = alloc_extent_map();
6235 if (!em) 6267 if (!em)
6236 return -ENOMEM; 6268 return -ENOMEM;
6237 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
6238 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS); 6269 map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
6239 if (!map) { 6270 if (!map) {
6240 free_extent_map(em); 6271 free_extent_map(em);
@@ -6242,7 +6273,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
6242 } 6273 }
6243 6274
6244 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags); 6275 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
6245 em->bdev = (struct block_device *)map; 6276 em->map_lookup = map;
6246 em->start = logical; 6277 em->start = logical;
6247 em->len = length; 6278 em->len = length;
6248 em->orig_start = 0; 6279 em->orig_start = 0;
@@ -6944,7 +6975,7 @@ void btrfs_update_commit_device_bytes_used(struct btrfs_root *root,
6944 /* In order to kick the device replace finish process */ 6975 /* In order to kick the device replace finish process */
6945 lock_chunks(root); 6976 lock_chunks(root);
6946 list_for_each_entry(em, &transaction->pending_chunks, list) { 6977 list_for_each_entry(em, &transaction->pending_chunks, list) {
6947 map = (struct map_lookup *)em->bdev; 6978 map = em->map_lookup;
6948 6979
6949 for (i = 0; i < map->num_stripes; i++) { 6980 for (i = 0; i < map->num_stripes; i++) {
6950 dev = map->stripes[i].dev; 6981 dev = map->stripes[i].dev;