aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Mason <clm@fb.com>2015-10-12 19:24:15 -0400
committerChris Mason <clm@fb.com>2015-10-12 19:24:15 -0400
commit62fb50ab7c903357c92cef2f7677235b92ac575f (patch)
tree28f66d4e2c14b508b6b24795bfadf11aaada2d75
parent640926ffdda7e817965d3bfb5bbbc51598ccf49b (diff)
parent73416dab235e5ff030e3b3c61da0cd6ced324fc9 (diff)
Merge branch 'anand/sysfs-updates-v4.3-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux into for-linus-4.4
Signed-off-by: Chris Mason <clm@fb.com>
-rw-r--r--fs/btrfs/ctree.c6
-rw-r--r--fs/btrfs/ctree.h13
-rw-r--r--fs/btrfs/dev-replace.c36
-rw-r--r--fs/btrfs/disk-io.c69
-rw-r--r--fs/btrfs/disk-io.h2
-rw-r--r--fs/btrfs/extent-tree.c2
-rw-r--r--fs/btrfs/inode-item.c2
-rw-r--r--fs/btrfs/ioctl.c2
-rw-r--r--fs/btrfs/relocation.c2
-rw-r--r--fs/btrfs/root-tree.c4
-rw-r--r--fs/btrfs/super.c27
-rw-r--r--fs/btrfs/sysfs.c52
-rw-r--r--fs/btrfs/sysfs.h4
-rw-r--r--fs/btrfs/transaction.c2
-rw-r--r--fs/btrfs/tree-log.c8
-rw-r--r--fs/btrfs/volumes.c139
-rw-r--r--fs/btrfs/volumes.h5
17 files changed, 198 insertions, 177 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 5f745eadf77d..1063315fb387 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -1011,7 +1011,7 @@ static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
1011 return ret; 1011 return ret;
1012 if (refs == 0) { 1012 if (refs == 0) {
1013 ret = -EROFS; 1013 ret = -EROFS;
1014 btrfs_std_error(root->fs_info, ret); 1014 btrfs_std_error(root->fs_info, ret, NULL);
1015 return ret; 1015 return ret;
1016 } 1016 }
1017 } else { 1017 } else {
@@ -1927,7 +1927,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
1927 child = read_node_slot(root, mid, 0); 1927 child = read_node_slot(root, mid, 0);
1928 if (!child) { 1928 if (!child) {
1929 ret = -EROFS; 1929 ret = -EROFS;
1930 btrfs_std_error(root->fs_info, ret); 1930 btrfs_std_error(root->fs_info, ret, NULL);
1931 goto enospc; 1931 goto enospc;
1932 } 1932 }
1933 1933
@@ -2030,7 +2030,7 @@ static noinline int balance_level(struct btrfs_trans_handle *trans,
2030 */ 2030 */
2031 if (!left) { 2031 if (!left) {
2032 ret = -EROFS; 2032 ret = -EROFS;
2033 btrfs_std_error(root->fs_info, ret); 2033 btrfs_std_error(root->fs_info, ret, NULL);
2034 goto enospc; 2034 goto enospc;
2035 } 2035 }
2036 wret = balance_node_right(trans, root, mid, left); 2036 wret = balance_node_right(trans, root, mid, left);
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index bca42c5733a1..49bc792108b9 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -4004,8 +4004,8 @@ int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
4004/* sysfs.c */ 4004/* sysfs.c */
4005int btrfs_init_sysfs(void); 4005int btrfs_init_sysfs(void);
4006void btrfs_exit_sysfs(void); 4006void btrfs_exit_sysfs(void);
4007int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info); 4007int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info);
4008void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info); 4008void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info);
4009 4009
4010/* xattr.c */ 4010/* xattr.c */
4011ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size); 4011ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
@@ -4215,14 +4215,7 @@ do { \
4215 __LINE__, (errno)); \ 4215 __LINE__, (errno)); \
4216} while (0) 4216} while (0)
4217 4217
4218#define btrfs_std_error(fs_info, errno) \ 4218#define btrfs_std_error(fs_info, errno, fmt, args...) \
4219do { \
4220 if ((errno)) \
4221 __btrfs_std_error((fs_info), __func__, \
4222 __LINE__, (errno), NULL); \
4223} while (0)
4224
4225#define btrfs_error(fs_info, errno, fmt, args...) \
4226do { \ 4219do { \
4227 __btrfs_std_error((fs_info), __func__, __LINE__, \ 4220 __btrfs_std_error((fs_info), __func__, __LINE__, \
4228 (errno), fmt, ##args); \ 4221 (errno), fmt, ##args); \
diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index ede1c1ad0baf..a64ca942b63f 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -327,19 +327,6 @@ int btrfs_dev_replace_start(struct btrfs_root *root,
327 args->start.tgtdev_name[0] == '\0') 327 args->start.tgtdev_name[0] == '\0')
328 return -EINVAL; 328 return -EINVAL;
329 329
330 /*
331 * Here we commit the transaction to make sure commit_total_bytes
332 * of all the devices are updated.
333 */
334 trans = btrfs_attach_transaction(root);
335 if (!IS_ERR(trans)) {
336 ret = btrfs_commit_transaction(trans, root);
337 if (ret)
338 return ret;
339 } else if (PTR_ERR(trans) != -ENOENT) {
340 return PTR_ERR(trans);
341 }
342
343 /* the disk copy procedure reuses the scrub code */ 330 /* the disk copy procedure reuses the scrub code */
344 mutex_lock(&fs_info->volume_mutex); 331 mutex_lock(&fs_info->volume_mutex);
345 ret = btrfs_dev_replace_find_srcdev(root, args->start.srcdevid, 332 ret = btrfs_dev_replace_find_srcdev(root, args->start.srcdevid,
@@ -356,6 +343,19 @@ int btrfs_dev_replace_start(struct btrfs_root *root,
356 if (ret) 343 if (ret)
357 return ret; 344 return ret;
358 345
346 /*
347 * Here we commit the transaction to make sure commit_total_bytes
348 * of all the devices are updated.
349 */
350 trans = btrfs_attach_transaction(root);
351 if (!IS_ERR(trans)) {
352 ret = btrfs_commit_transaction(trans, root);
353 if (ret)
354 return ret;
355 } else if (PTR_ERR(trans) != -ENOENT) {
356 return PTR_ERR(trans);
357 }
358
359 btrfs_dev_replace_lock(dev_replace); 359 btrfs_dev_replace_lock(dev_replace);
360 switch (dev_replace->replace_state) { 360 switch (dev_replace->replace_state) {
361 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED: 361 case BTRFS_IOCTL_DEV_REPLACE_STATE_NEVER_STARTED:
@@ -375,10 +375,6 @@ int btrfs_dev_replace_start(struct btrfs_root *root,
375 WARN_ON(!tgt_device); 375 WARN_ON(!tgt_device);
376 dev_replace->tgtdev = tgt_device; 376 dev_replace->tgtdev = tgt_device;
377 377
378 ret = btrfs_kobj_add_device(tgt_device->fs_devices, tgt_device);
379 if (ret)
380 btrfs_err(root->fs_info, "kobj add dev failed %d\n", ret);
381
382 btrfs_info_in_rcu(root->fs_info, 378 btrfs_info_in_rcu(root->fs_info,
383 "dev_replace from %s (devid %llu) to %s started", 379 "dev_replace from %s (devid %llu) to %s started",
384 src_device->missing ? "<missing disk>" : 380 src_device->missing ? "<missing disk>" :
@@ -401,6 +397,10 @@ int btrfs_dev_replace_start(struct btrfs_root *root,
401 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR; 397 args->result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_ERROR;
402 btrfs_dev_replace_unlock(dev_replace); 398 btrfs_dev_replace_unlock(dev_replace);
403 399
400 ret = btrfs_sysfs_add_device_link(tgt_device->fs_devices, tgt_device);
401 if (ret)
402 btrfs_err(root->fs_info, "kobj add dev failed %d\n", ret);
403
404 btrfs_wait_ordered_roots(root->fs_info, -1); 404 btrfs_wait_ordered_roots(root->fs_info, -1);
405 405
406 /* force writing the updated state information to disk */ 406 /* force writing the updated state information to disk */
@@ -586,7 +586,7 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
586 mutex_unlock(&uuid_mutex); 586 mutex_unlock(&uuid_mutex);
587 587
588 /* replace the sysfs entry */ 588 /* replace the sysfs entry */
589 btrfs_kobj_rm_device(fs_info->fs_devices, src_device); 589 btrfs_sysfs_rm_device_link(fs_info->fs_devices, src_device);
590 btrfs_rm_dev_replace_free_srcdev(fs_info, src_device); 590 btrfs_rm_dev_replace_free_srcdev(fs_info, src_device);
591 591
592 /* write back the superblocks */ 592 /* write back the superblocks */
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 75f84e4c2033..7f934cccc98d 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -2375,7 +2375,7 @@ static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
2375 /* returns with log_tree_root freed on success */ 2375 /* returns with log_tree_root freed on success */
2376 ret = btrfs_recover_log_trees(log_tree_root); 2376 ret = btrfs_recover_log_trees(log_tree_root);
2377 if (ret) { 2377 if (ret) {
2378 btrfs_error(tree_root->fs_info, ret, 2378 btrfs_std_error(tree_root->fs_info, ret,
2379 "Failed to recover log tree"); 2379 "Failed to recover log tree");
2380 free_extent_buffer(log_tree_root->node); 2380 free_extent_buffer(log_tree_root->node);
2381 kfree(log_tree_root); 2381 kfree(log_tree_root);
@@ -2651,8 +2651,8 @@ int open_ctree(struct super_block *sb,
2651 * Read super block and check the signature bytes only 2651 * Read super block and check the signature bytes only
2652 */ 2652 */
2653 bh = btrfs_read_dev_super(fs_devices->latest_bdev); 2653 bh = btrfs_read_dev_super(fs_devices->latest_bdev);
2654 if (!bh) { 2654 if (IS_ERR(bh)) {
2655 err = -EINVAL; 2655 err = PTR_ERR(bh);
2656 goto fail_alloc; 2656 goto fail_alloc;
2657 } 2657 }
2658 2658
@@ -2935,7 +2935,7 @@ retry_root_backup:
2935 goto fail_fsdev_sysfs; 2935 goto fail_fsdev_sysfs;
2936 } 2936 }
2937 2937
2938 ret = btrfs_sysfs_add_one(fs_info); 2938 ret = btrfs_sysfs_add_mounted(fs_info);
2939 if (ret) { 2939 if (ret) {
2940 pr_err("BTRFS: failed to init sysfs interface: %d\n", ret); 2940 pr_err("BTRFS: failed to init sysfs interface: %d\n", ret);
2941 goto fail_fsdev_sysfs; 2941 goto fail_fsdev_sysfs;
@@ -3115,7 +3115,7 @@ fail_cleaner:
3115 filemap_write_and_wait(fs_info->btree_inode->i_mapping); 3115 filemap_write_and_wait(fs_info->btree_inode->i_mapping);
3116 3116
3117fail_sysfs: 3117fail_sysfs:
3118 btrfs_sysfs_remove_one(fs_info); 3118 btrfs_sysfs_remove_mounted(fs_info);
3119 3119
3120fail_fsdev_sysfs: 3120fail_fsdev_sysfs:
3121 btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3121 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
@@ -3190,6 +3190,37 @@ static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
3190 put_bh(bh); 3190 put_bh(bh);
3191} 3191}
3192 3192
3193int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
3194 struct buffer_head **bh_ret)
3195{
3196 struct buffer_head *bh;
3197 struct btrfs_super_block *super;
3198 u64 bytenr;
3199
3200 bytenr = btrfs_sb_offset(copy_num);
3201 if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
3202 return -EINVAL;
3203
3204 bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE);
3205 /*
3206 * If we fail to read from the underlying devices, as of now
3207 * the best option we have is to mark it EIO.
3208 */
3209 if (!bh)
3210 return -EIO;
3211
3212 super = (struct btrfs_super_block *)bh->b_data;
3213 if (btrfs_super_bytenr(super) != bytenr ||
3214 btrfs_super_magic(super) != BTRFS_MAGIC) {
3215 brelse(bh);
3216 return -EINVAL;
3217 }
3218
3219 *bh_ret = bh;
3220 return 0;
3221}
3222
3223
3193struct buffer_head *btrfs_read_dev_super(struct block_device *bdev) 3224struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3194{ 3225{
3195 struct buffer_head *bh; 3226 struct buffer_head *bh;
@@ -3197,7 +3228,7 @@ struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3197 struct btrfs_super_block *super; 3228 struct btrfs_super_block *super;
3198 int i; 3229 int i;
3199 u64 transid = 0; 3230 u64 transid = 0;
3200 u64 bytenr; 3231 int ret = -EINVAL;
3201 3232
3202 /* we would like to check all the supers, but that would make 3233 /* we would like to check all the supers, but that would make
3203 * a btrfs mount succeed after a mkfs from a different FS. 3234 * a btrfs mount succeed after a mkfs from a different FS.
@@ -3205,21 +3236,11 @@ struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3205 * later supers, using BTRFS_SUPER_MIRROR_MAX instead 3236 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
3206 */ 3237 */
3207 for (i = 0; i < 1; i++) { 3238 for (i = 0; i < 1; i++) {
3208 bytenr = btrfs_sb_offset(i); 3239 ret = btrfs_read_dev_one_super(bdev, i, &bh);
3209 if (bytenr + BTRFS_SUPER_INFO_SIZE >= 3240 if (ret)
3210 i_size_read(bdev->bd_inode))
3211 break;
3212 bh = __bread(bdev, bytenr / 4096,
3213 BTRFS_SUPER_INFO_SIZE);
3214 if (!bh)
3215 continue; 3241 continue;
3216 3242
3217 super = (struct btrfs_super_block *)bh->b_data; 3243 super = (struct btrfs_super_block *)bh->b_data;
3218 if (btrfs_super_bytenr(super) != bytenr ||
3219 btrfs_super_magic(super) != BTRFS_MAGIC) {
3220 brelse(bh);
3221 continue;
3222 }
3223 3244
3224 if (!latest || btrfs_super_generation(super) > transid) { 3245 if (!latest || btrfs_super_generation(super) > transid) {
3225 brelse(latest); 3246 brelse(latest);
@@ -3229,6 +3250,10 @@ struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
3229 brelse(bh); 3250 brelse(bh);
3230 } 3251 }
3231 } 3252 }
3253
3254 if (!latest)
3255 return ERR_PTR(ret);
3256
3232 return latest; 3257 return latest;
3233} 3258}
3234 3259
@@ -3547,7 +3572,7 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3547 if (ret) { 3572 if (ret) {
3548 mutex_unlock( 3573 mutex_unlock(
3549 &root->fs_info->fs_devices->device_list_mutex); 3574 &root->fs_info->fs_devices->device_list_mutex);
3550 btrfs_error(root->fs_info, ret, 3575 btrfs_std_error(root->fs_info, ret,
3551 "errors while submitting device barriers."); 3576 "errors while submitting device barriers.");
3552 return ret; 3577 return ret;
3553 } 3578 }
@@ -3587,7 +3612,7 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3587 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3612 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3588 3613
3589 /* FUA is masked off if unsupported and can't be the reason */ 3614 /* FUA is masked off if unsupported and can't be the reason */
3590 btrfs_error(root->fs_info, -EIO, 3615 btrfs_std_error(root->fs_info, -EIO,
3591 "%d errors while writing supers", total_errors); 3616 "%d errors while writing supers", total_errors);
3592 return -EIO; 3617 return -EIO;
3593 } 3618 }
@@ -3605,7 +3630,7 @@ static int write_all_supers(struct btrfs_root *root, int max_mirrors)
3605 } 3630 }
3606 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex); 3631 mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
3607 if (total_errors > max_errors) { 3632 if (total_errors > max_errors) {
3608 btrfs_error(root->fs_info, -EIO, 3633 btrfs_std_error(root->fs_info, -EIO,
3609 "%d errors while writing supers", total_errors); 3634 "%d errors while writing supers", total_errors);
3610 return -EIO; 3635 return -EIO;
3611 } 3636 }
@@ -3791,7 +3816,7 @@ void close_ctree(struct btrfs_root *root)
3791 percpu_counter_sum(&fs_info->delalloc_bytes)); 3816 percpu_counter_sum(&fs_info->delalloc_bytes));
3792 } 3817 }
3793 3818
3794 btrfs_sysfs_remove_one(fs_info); 3819 btrfs_sysfs_remove_mounted(fs_info);
3795 btrfs_sysfs_remove_fsid(fs_info->fs_devices); 3820 btrfs_sysfs_remove_fsid(fs_info->fs_devices);
3796 3821
3797 btrfs_free_fs_roots(fs_info); 3822 btrfs_free_fs_roots(fs_info);
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index bdfb479ea859..adeb31830b9c 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -60,6 +60,8 @@ void close_ctree(struct btrfs_root *root);
60int write_ctree_super(struct btrfs_trans_handle *trans, 60int write_ctree_super(struct btrfs_trans_handle *trans,
61 struct btrfs_root *root, int max_mirrors); 61 struct btrfs_root *root, int max_mirrors);
62struct buffer_head *btrfs_read_dev_super(struct block_device *bdev); 62struct buffer_head *btrfs_read_dev_super(struct block_device *bdev);
63int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
64 struct buffer_head **bh_ret);
63int btrfs_commit_super(struct btrfs_root *root); 65int btrfs_commit_super(struct btrfs_root *root);
64struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info, 66struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
65 u64 bytenr); 67 u64 bytenr);
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 4480a36b7c5b..92a5b4cad2ab 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -8694,7 +8694,7 @@ out:
8694 if (!for_reloc && root_dropped == false) 8694 if (!for_reloc && root_dropped == false)
8695 btrfs_add_dead_root(root); 8695 btrfs_add_dead_root(root);
8696 if (err && err != -EAGAIN) 8696 if (err && err != -EAGAIN)
8697 btrfs_std_error(root->fs_info, err); 8697 btrfs_std_error(root->fs_info, err, NULL);
8698 return err; 8698 return err;
8699} 8699}
8700 8700
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index 265e03c73f4d..be4d22a5022f 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -157,7 +157,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
157 */ 157 */
158 if (!btrfs_find_name_in_ext_backref(path, ref_objectid, 158 if (!btrfs_find_name_in_ext_backref(path, ref_objectid,
159 name, name_len, &extref)) { 159 name, name_len, &extref)) {
160 btrfs_std_error(root->fs_info, -ENOENT); 160 btrfs_std_error(root->fs_info, -ENOENT, NULL);
161 ret = -EROFS; 161 ret = -EROFS;
162 goto out; 162 goto out;
163 } 163 }
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 2e520c635709..80342d3fa5d2 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -4806,7 +4806,7 @@ static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg)
4806 /* update qgroup status and info */ 4806 /* update qgroup status and info */
4807 err = btrfs_run_qgroups(trans, root->fs_info); 4807 err = btrfs_run_qgroups(trans, root->fs_info);
4808 if (err < 0) 4808 if (err < 0)
4809 btrfs_error(root->fs_info, ret, 4809 btrfs_std_error(root->fs_info, ret,
4810 "failed to update qgroup status and info\n"); 4810 "failed to update qgroup status and info\n");
4811 err = btrfs_end_transaction(trans, root); 4811 err = btrfs_end_transaction(trans, root);
4812 if (err && !ret) 4812 if (err && !ret)
diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c
index 303babeef505..58ede0a56456 100644
--- a/fs/btrfs/relocation.c
+++ b/fs/btrfs/relocation.c
@@ -2418,7 +2418,7 @@ again:
2418 } 2418 }
2419out: 2419out:
2420 if (ret) { 2420 if (ret) {
2421 btrfs_std_error(root->fs_info, ret); 2421 btrfs_std_error(root->fs_info, ret, NULL);
2422 if (!list_empty(&reloc_roots)) 2422 if (!list_empty(&reloc_roots))
2423 free_reloc_roots(&reloc_roots); 2423 free_reloc_roots(&reloc_roots);
2424 2424
diff --git a/fs/btrfs/root-tree.c b/fs/btrfs/root-tree.c
index 42267d37bb94..bc9dcc53f66d 100644
--- a/fs/btrfs/root-tree.c
+++ b/fs/btrfs/root-tree.c
@@ -284,7 +284,7 @@ int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
284 trans = btrfs_join_transaction(tree_root); 284 trans = btrfs_join_transaction(tree_root);
285 if (IS_ERR(trans)) { 285 if (IS_ERR(trans)) {
286 err = PTR_ERR(trans); 286 err = PTR_ERR(trans);
287 btrfs_error(tree_root->fs_info, err, 287 btrfs_std_error(tree_root->fs_info, err,
288 "Failed to start trans to delete " 288 "Failed to start trans to delete "
289 "orphan item"); 289 "orphan item");
290 break; 290 break;
@@ -293,7 +293,7 @@ int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
293 root_key.objectid); 293 root_key.objectid);
294 btrfs_end_transaction(trans, tree_root); 294 btrfs_end_transaction(trans, tree_root);
295 if (err) { 295 if (err) {
296 btrfs_error(tree_root->fs_info, err, 296 btrfs_std_error(tree_root->fs_info, err,
297 "Failed to delete root orphan " 297 "Failed to delete root orphan "
298 "item"); 298 "item");
299 break; 299 break;
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 11d1eab9234d..b23d49daa1a2 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -130,7 +130,6 @@ static void btrfs_handle_error(struct btrfs_fs_info *fs_info)
130 } 130 }
131} 131}
132 132
133#ifdef CONFIG_PRINTK
134/* 133/*
135 * __btrfs_std_error decodes expected errors from the caller and 134 * __btrfs_std_error decodes expected errors from the caller and
136 * invokes the approciate error response. 135 * invokes the approciate error response.
@@ -140,7 +139,9 @@ void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
140 unsigned int line, int errno, const char *fmt, ...) 139 unsigned int line, int errno, const char *fmt, ...)
141{ 140{
142 struct super_block *sb = fs_info->sb; 141 struct super_block *sb = fs_info->sb;
142#ifdef CONFIG_PRINTK
143 const char *errstr; 143 const char *errstr;
144#endif
144 145
145 /* 146 /*
146 * Special case: if the error is EROFS, and we're already 147 * Special case: if the error is EROFS, and we're already
@@ -149,6 +150,7 @@ void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
149 if (errno == -EROFS && (sb->s_flags & MS_RDONLY)) 150 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
150 return; 151 return;
151 152
153#ifdef CONFIG_PRINTK
152 errstr = btrfs_decode_error(errno); 154 errstr = btrfs_decode_error(errno);
153 if (fmt) { 155 if (fmt) {
154 struct va_format vaf; 156 struct va_format vaf;
@@ -166,6 +168,7 @@ void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
166 printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n", 168 printk(KERN_CRIT "BTRFS: error (device %s) in %s:%d: errno=%d %s\n",
167 sb->s_id, function, line, errno, errstr); 169 sb->s_id, function, line, errno, errstr);
168 } 170 }
171#endif
169 172
170 /* Don't go through full error handling during mount */ 173 /* Don't go through full error handling during mount */
171 save_error_info(fs_info); 174 save_error_info(fs_info);
@@ -173,6 +176,7 @@ void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
173 btrfs_handle_error(fs_info); 176 btrfs_handle_error(fs_info);
174} 177}
175 178
179#ifdef CONFIG_PRINTK
176static const char * const logtypes[] = { 180static const char * const logtypes[] = {
177 "emergency", 181 "emergency",
178 "alert", 182 "alert",
@@ -212,27 +216,6 @@ void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
212 216
213 va_end(args); 217 va_end(args);
214} 218}
215
216#else
217
218void __btrfs_std_error(struct btrfs_fs_info *fs_info, const char *function,
219 unsigned int line, int errno, const char *fmt, ...)
220{
221 struct super_block *sb = fs_info->sb;
222
223 /*
224 * Special case: if the error is EROFS, and we're already
225 * under MS_RDONLY, then it is safe here.
226 */
227 if (errno == -EROFS && (sb->s_flags & MS_RDONLY))
228 return;
229
230 /* Don't go through full error handling during mount */
231 if (sb->s_flags & MS_BORN) {
232 save_error_info(fs_info);
233 btrfs_handle_error(fs_info);
234 }
235}
236#endif 219#endif
237 220
238/* 221/*
diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 603b0cc2b9bb..e0ac85949067 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -437,24 +437,24 @@ static const struct attribute *btrfs_attrs[] = {
437 NULL, 437 NULL,
438}; 438};
439 439
440static void btrfs_release_super_kobj(struct kobject *kobj) 440static void btrfs_release_fsid_kobj(struct kobject *kobj)
441{ 441{
442 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj); 442 struct btrfs_fs_devices *fs_devs = to_fs_devs(kobj);
443 443
444 memset(&fs_devs->super_kobj, 0, sizeof(struct kobject)); 444 memset(&fs_devs->fsid_kobj, 0, sizeof(struct kobject));
445 complete(&fs_devs->kobj_unregister); 445 complete(&fs_devs->kobj_unregister);
446} 446}
447 447
448static struct kobj_type btrfs_ktype = { 448static struct kobj_type btrfs_ktype = {
449 .sysfs_ops = &kobj_sysfs_ops, 449 .sysfs_ops = &kobj_sysfs_ops,
450 .release = btrfs_release_super_kobj, 450 .release = btrfs_release_fsid_kobj,
451}; 451};
452 452
453static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj) 453static inline struct btrfs_fs_devices *to_fs_devs(struct kobject *kobj)
454{ 454{
455 if (kobj->ktype != &btrfs_ktype) 455 if (kobj->ktype != &btrfs_ktype)
456 return NULL; 456 return NULL;
457 return container_of(kobj, struct btrfs_fs_devices, super_kobj); 457 return container_of(kobj, struct btrfs_fs_devices, fsid_kobj);
458} 458}
459 459
460static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj) 460static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
@@ -502,12 +502,12 @@ static int addrm_unknown_feature_attrs(struct btrfs_fs_info *fs_info, bool add)
502 attrs[0] = &fa->kobj_attr.attr; 502 attrs[0] = &fa->kobj_attr.attr;
503 if (add) { 503 if (add) {
504 int ret; 504 int ret;
505 ret = sysfs_merge_group(&fs_info->fs_devices->super_kobj, 505 ret = sysfs_merge_group(&fs_info->fs_devices->fsid_kobj,
506 &agroup); 506 &agroup);
507 if (ret) 507 if (ret)
508 return ret; 508 return ret;
509 } else 509 } else
510 sysfs_unmerge_group(&fs_info->fs_devices->super_kobj, 510 sysfs_unmerge_group(&fs_info->fs_devices->fsid_kobj,
511 &agroup); 511 &agroup);
512 } 512 }
513 513
@@ -523,9 +523,9 @@ static void __btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
523 fs_devs->device_dir_kobj = NULL; 523 fs_devs->device_dir_kobj = NULL;
524 } 524 }
525 525
526 if (fs_devs->super_kobj.state_initialized) { 526 if (fs_devs->fsid_kobj.state_initialized) {
527 kobject_del(&fs_devs->super_kobj); 527 kobject_del(&fs_devs->fsid_kobj);
528 kobject_put(&fs_devs->super_kobj); 528 kobject_put(&fs_devs->fsid_kobj);
529 wait_for_completion(&fs_devs->kobj_unregister); 529 wait_for_completion(&fs_devs->kobj_unregister);
530 } 530 }
531} 531}
@@ -545,7 +545,7 @@ void btrfs_sysfs_remove_fsid(struct btrfs_fs_devices *fs_devs)
545 } 545 }
546} 546}
547 547
548void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info) 548void btrfs_sysfs_remove_mounted(struct btrfs_fs_info *fs_info)
549{ 549{
550 btrfs_reset_fs_info_ptr(fs_info); 550 btrfs_reset_fs_info_ptr(fs_info);
551 551
@@ -555,9 +555,9 @@ void btrfs_sysfs_remove_one(struct btrfs_fs_info *fs_info)
555 kobject_put(fs_info->space_info_kobj); 555 kobject_put(fs_info->space_info_kobj);
556 } 556 }
557 addrm_unknown_feature_attrs(fs_info, false); 557 addrm_unknown_feature_attrs(fs_info, false);
558 sysfs_remove_group(&fs_info->fs_devices->super_kobj, &btrfs_feature_attr_group); 558 sysfs_remove_group(&fs_info->fs_devices->fsid_kobj, &btrfs_feature_attr_group);
559 sysfs_remove_files(&fs_info->fs_devices->super_kobj, btrfs_attrs); 559 sysfs_remove_files(&fs_info->fs_devices->fsid_kobj, btrfs_attrs);
560 btrfs_kobj_rm_device(fs_info->fs_devices, NULL); 560 btrfs_sysfs_rm_device_link(fs_info->fs_devices, NULL);
561} 561}
562 562
563const char * const btrfs_feature_set_names[3] = { 563const char * const btrfs_feature_set_names[3] = {
@@ -637,7 +637,7 @@ static void init_feature_attrs(void)
637 637
638/* when one_device is NULL, it removes all device links */ 638/* when one_device is NULL, it removes all device links */
639 639
640int btrfs_kobj_rm_device(struct btrfs_fs_devices *fs_devices, 640int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
641 struct btrfs_device *one_device) 641 struct btrfs_device *one_device)
642{ 642{
643 struct hd_struct *disk; 643 struct hd_struct *disk;
@@ -675,7 +675,7 @@ int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
675{ 675{
676 if (!fs_devs->device_dir_kobj) 676 if (!fs_devs->device_dir_kobj)
677 fs_devs->device_dir_kobj = kobject_create_and_add("devices", 677 fs_devs->device_dir_kobj = kobject_create_and_add("devices",
678 &fs_devs->super_kobj); 678 &fs_devs->fsid_kobj);
679 679
680 if (!fs_devs->device_dir_kobj) 680 if (!fs_devs->device_dir_kobj)
681 return -ENOMEM; 681 return -ENOMEM;
@@ -683,7 +683,7 @@ int btrfs_sysfs_add_device(struct btrfs_fs_devices *fs_devs)
683 return 0; 683 return 0;
684} 684}
685 685
686int btrfs_kobj_add_device(struct btrfs_fs_devices *fs_devices, 686int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
687 struct btrfs_device *one_device) 687 struct btrfs_device *one_device)
688{ 688{
689 int error = 0; 689 int error = 0;
@@ -730,31 +730,31 @@ int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
730 int error; 730 int error;
731 731
732 init_completion(&fs_devs->kobj_unregister); 732 init_completion(&fs_devs->kobj_unregister);
733 fs_devs->super_kobj.kset = btrfs_kset; 733 fs_devs->fsid_kobj.kset = btrfs_kset;
734 error = kobject_init_and_add(&fs_devs->super_kobj, 734 error = kobject_init_and_add(&fs_devs->fsid_kobj,
735 &btrfs_ktype, parent, "%pU", fs_devs->fsid); 735 &btrfs_ktype, parent, "%pU", fs_devs->fsid);
736 return error; 736 return error;
737} 737}
738 738
739int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info) 739int btrfs_sysfs_add_mounted(struct btrfs_fs_info *fs_info)
740{ 740{
741 int error; 741 int error;
742 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices; 742 struct btrfs_fs_devices *fs_devs = fs_info->fs_devices;
743 struct kobject *super_kobj = &fs_devs->super_kobj; 743 struct kobject *fsid_kobj = &fs_devs->fsid_kobj;
744 744
745 btrfs_set_fs_info_ptr(fs_info); 745 btrfs_set_fs_info_ptr(fs_info);
746 746
747 error = btrfs_kobj_add_device(fs_devs, NULL); 747 error = btrfs_sysfs_add_device_link(fs_devs, NULL);
748 if (error) 748 if (error)
749 return error; 749 return error;
750 750
751 error = sysfs_create_files(super_kobj, btrfs_attrs); 751 error = sysfs_create_files(fsid_kobj, btrfs_attrs);
752 if (error) { 752 if (error) {
753 btrfs_kobj_rm_device(fs_devs, NULL); 753 btrfs_sysfs_rm_device_link(fs_devs, NULL);
754 return error; 754 return error;
755 } 755 }
756 756
757 error = sysfs_create_group(super_kobj, 757 error = sysfs_create_group(fsid_kobj,
758 &btrfs_feature_attr_group); 758 &btrfs_feature_attr_group);
759 if (error) 759 if (error)
760 goto failure; 760 goto failure;
@@ -764,7 +764,7 @@ int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
764 goto failure; 764 goto failure;
765 765
766 fs_info->space_info_kobj = kobject_create_and_add("allocation", 766 fs_info->space_info_kobj = kobject_create_and_add("allocation",
767 super_kobj); 767 fsid_kobj);
768 if (!fs_info->space_info_kobj) { 768 if (!fs_info->space_info_kobj) {
769 error = -ENOMEM; 769 error = -ENOMEM;
770 goto failure; 770 goto failure;
@@ -776,7 +776,7 @@ int btrfs_sysfs_add_one(struct btrfs_fs_info *fs_info)
776 776
777 return 0; 777 return 0;
778failure: 778failure:
779 btrfs_sysfs_remove_one(fs_info); 779 btrfs_sysfs_remove_mounted(fs_info);
780 return error; 780 return error;
781} 781}
782 782
diff --git a/fs/btrfs/sysfs.h b/fs/btrfs/sysfs.h
index 6392527bcc15..9c09522125a6 100644
--- a/fs/btrfs/sysfs.h
+++ b/fs/btrfs/sysfs.h
@@ -82,9 +82,9 @@ char *btrfs_printable_features(enum btrfs_feature_set set, u64 flags);
82extern const char * const btrfs_feature_set_names[3]; 82extern const char * const btrfs_feature_set_names[3];
83extern struct kobj_type space_info_ktype; 83extern struct kobj_type space_info_ktype;
84extern struct kobj_type btrfs_raid_ktype; 84extern struct kobj_type btrfs_raid_ktype;
85int btrfs_kobj_add_device(struct btrfs_fs_devices *fs_devices, 85int btrfs_sysfs_add_device_link(struct btrfs_fs_devices *fs_devices,
86 struct btrfs_device *one_device); 86 struct btrfs_device *one_device);
87int btrfs_kobj_rm_device(struct btrfs_fs_devices *fs_devices, 87int btrfs_sysfs_rm_device_link(struct btrfs_fs_devices *fs_devices,
88 struct btrfs_device *one_device); 88 struct btrfs_device *one_device);
89int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs, 89int btrfs_sysfs_add_fsid(struct btrfs_fs_devices *fs_devs,
90 struct kobject *parent); 90 struct kobject *parent);
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index a5b06442f0bf..76354bb1312c 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -2136,7 +2136,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
2136 2136
2137 ret = btrfs_write_and_wait_transaction(trans, root); 2137 ret = btrfs_write_and_wait_transaction(trans, root);
2138 if (ret) { 2138 if (ret) {
2139 btrfs_error(root->fs_info, ret, 2139 btrfs_std_error(root->fs_info, ret,
2140 "Error while writing out transaction"); 2140 "Error while writing out transaction");
2141 mutex_unlock(&root->fs_info->tree_log_mutex); 2141 mutex_unlock(&root->fs_info->tree_log_mutex);
2142 goto scrub_continue; 2142 goto scrub_continue;
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index 1bbaace73383..c3f9a9c71f28 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -5314,7 +5314,7 @@ int btrfs_recover_log_trees(struct btrfs_root *log_root_tree)
5314 5314
5315 ret = walk_log_tree(trans, log_root_tree, &wc); 5315 ret = walk_log_tree(trans, log_root_tree, &wc);
5316 if (ret) { 5316 if (ret) {
5317 btrfs_error(fs_info, ret, "Failed to pin buffers while " 5317 btrfs_std_error(fs_info, ret, "Failed to pin buffers while "
5318 "recovering log root tree."); 5318 "recovering log root tree.");
5319 goto error; 5319 goto error;
5320 } 5320 }
@@ -5328,7 +5328,7 @@ again:
5328 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0); 5328 ret = btrfs_search_slot(NULL, log_root_tree, &key, path, 0, 0);
5329 5329
5330 if (ret < 0) { 5330 if (ret < 0) {
5331 btrfs_error(fs_info, ret, 5331 btrfs_std_error(fs_info, ret,
5332 "Couldn't find tree log root."); 5332 "Couldn't find tree log root.");
5333 goto error; 5333 goto error;
5334 } 5334 }
@@ -5346,7 +5346,7 @@ again:
5346 log = btrfs_read_fs_root(log_root_tree, &found_key); 5346 log = btrfs_read_fs_root(log_root_tree, &found_key);
5347 if (IS_ERR(log)) { 5347 if (IS_ERR(log)) {
5348 ret = PTR_ERR(log); 5348 ret = PTR_ERR(log);
5349 btrfs_error(fs_info, ret, 5349 btrfs_std_error(fs_info, ret,
5350 "Couldn't read tree log root."); 5350 "Couldn't read tree log root.");
5351 goto error; 5351 goto error;
5352 } 5352 }
@@ -5361,7 +5361,7 @@ again:
5361 free_extent_buffer(log->node); 5361 free_extent_buffer(log->node);
5362 free_extent_buffer(log->commit_root); 5362 free_extent_buffer(log->commit_root);
5363 kfree(log); 5363 kfree(log);
5364 btrfs_error(fs_info, ret, "Couldn't read target root " 5364 btrfs_std_error(fs_info, ret, "Couldn't read target root "
5365 "for tree log recovery."); 5365 "for tree log recovery.");
5366 goto error; 5366 goto error;
5367 } 5367 }
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 7ad310cc3efb..dd644d767782 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -198,7 +198,6 @@ btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
198 198
199 if (IS_ERR(*bdev)) { 199 if (IS_ERR(*bdev)) {
200 ret = PTR_ERR(*bdev); 200 ret = PTR_ERR(*bdev);
201 printk(KERN_INFO "BTRFS: open %s failed\n", device_path);
202 goto error; 201 goto error;
203 } 202 }
204 203
@@ -211,8 +210,8 @@ btrfs_get_bdev_and_sb(const char *device_path, fmode_t flags, void *holder,
211 } 210 }
212 invalidate_bdev(*bdev); 211 invalidate_bdev(*bdev);
213 *bh = btrfs_read_dev_super(*bdev); 212 *bh = btrfs_read_dev_super(*bdev);
214 if (!*bh) { 213 if (IS_ERR(*bh)) {
215 ret = -EINVAL; 214 ret = PTR_ERR(*bh);
216 blkdev_put(*bdev, flags); 215 blkdev_put(*bdev, flags);
217 goto error; 216 goto error;
218 } 217 }
@@ -765,36 +764,7 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
765 764
766 mutex_lock(&fs_devices->device_list_mutex); 765 mutex_lock(&fs_devices->device_list_mutex);
767 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) { 766 list_for_each_entry_safe(device, tmp, &fs_devices->devices, dev_list) {
768 struct btrfs_device *new_device; 767 btrfs_close_one_device(device);
769 struct rcu_string *name;
770
771 if (device->bdev)
772 fs_devices->open_devices--;
773
774 if (device->writeable &&
775 device->devid != BTRFS_DEV_REPLACE_DEVID) {
776 list_del_init(&device->dev_alloc_list);
777 fs_devices->rw_devices--;
778 }
779
780 if (device->missing)
781 fs_devices->missing_devices--;
782
783 new_device = btrfs_alloc_device(NULL, &device->devid,
784 device->uuid);
785 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
786
787 /* Safe because we are under uuid_mutex */
788 if (device->name) {
789 name = rcu_string_strdup(device->name->str, GFP_NOFS);
790 BUG_ON(!name); /* -ENOMEM */
791 rcu_assign_pointer(new_device->name, name);
792 }
793
794 list_replace_rcu(&device->dev_list, &new_device->dev_list);
795 new_device->fs_devices = device->fs_devices;
796
797 call_rcu(&device->rcu, free_device);
798 } 768 }
799 mutex_unlock(&fs_devices->device_list_mutex); 769 mutex_unlock(&fs_devices->device_list_mutex);
800 770
@@ -1402,7 +1372,7 @@ again:
1402 extent = btrfs_item_ptr(leaf, path->slots[0], 1372 extent = btrfs_item_ptr(leaf, path->slots[0],
1403 struct btrfs_dev_extent); 1373 struct btrfs_dev_extent);
1404 } else { 1374 } else {
1405 btrfs_error(root->fs_info, ret, "Slot search failed"); 1375 btrfs_std_error(root->fs_info, ret, "Slot search failed");
1406 goto out; 1376 goto out;
1407 } 1377 }
1408 1378
@@ -1410,7 +1380,7 @@ again:
1410 1380
1411 ret = btrfs_del_item(trans, root, path); 1381 ret = btrfs_del_item(trans, root, path);
1412 if (ret) { 1382 if (ret) {
1413 btrfs_error(root->fs_info, ret, 1383 btrfs_std_error(root->fs_info, ret,
1414 "Failed to remove dev extent item"); 1384 "Failed to remove dev extent item");
1415 } else { 1385 } else {
1416 trans->transaction->have_free_bgs = 1; 1386 trans->transaction->have_free_bgs = 1;
@@ -1801,7 +1771,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1801 if (device->bdev) { 1771 if (device->bdev) {
1802 device->fs_devices->open_devices--; 1772 device->fs_devices->open_devices--;
1803 /* remove sysfs entry */ 1773 /* remove sysfs entry */
1804 btrfs_kobj_rm_device(root->fs_info->fs_devices, device); 1774 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
1805 } 1775 }
1806 1776
1807 call_rcu(&device->rcu, free_device); 1777 call_rcu(&device->rcu, free_device);
@@ -1924,7 +1894,8 @@ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_fs_info *fs_info,
1924 if (srcdev->writeable) { 1894 if (srcdev->writeable) {
1925 fs_devices->rw_devices--; 1895 fs_devices->rw_devices--;
1926 /* zero out the old super if it is writable */ 1896 /* zero out the old super if it is writable */
1927 btrfs_scratch_superblock(srcdev); 1897 btrfs_scratch_superblocks(srcdev->bdev,
1898 rcu_str_deref(srcdev->name));
1928 } 1899 }
1929 1900
1930 if (srcdev->bdev) 1901 if (srcdev->bdev)
@@ -1971,10 +1942,11 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
1971 WARN_ON(!tgtdev); 1942 WARN_ON(!tgtdev);
1972 mutex_lock(&fs_info->fs_devices->device_list_mutex); 1943 mutex_lock(&fs_info->fs_devices->device_list_mutex);
1973 1944
1974 btrfs_kobj_rm_device(fs_info->fs_devices, tgtdev); 1945 btrfs_sysfs_rm_device_link(fs_info->fs_devices, tgtdev);
1975 1946
1976 if (tgtdev->bdev) { 1947 if (tgtdev->bdev) {
1977 btrfs_scratch_superblock(tgtdev); 1948 btrfs_scratch_superblocks(tgtdev->bdev,
1949 rcu_str_deref(tgtdev->name));
1978 fs_info->fs_devices->open_devices--; 1950 fs_info->fs_devices->open_devices--;
1979 } 1951 }
1980 fs_info->fs_devices->num_devices--; 1952 fs_info->fs_devices->num_devices--;
@@ -2041,10 +2013,8 @@ int btrfs_find_device_missing_or_by_path(struct btrfs_root *root,
2041 } 2013 }
2042 } 2014 }
2043 2015
2044 if (!*device) { 2016 if (!*device)
2045 btrfs_err(root->fs_info, "no missing device found"); 2017 return BTRFS_ERROR_DEV_MISSING_NOT_FOUND;
2046 return -ENOENT;
2047 }
2048 2018
2049 return 0; 2019 return 0;
2050 } else { 2020 } else {
@@ -2309,7 +2279,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2309 tmp + 1); 2279 tmp + 1);
2310 2280
2311 /* add sysfs device entry */ 2281 /* add sysfs device entry */
2312 btrfs_kobj_add_device(root->fs_info->fs_devices, device); 2282 btrfs_sysfs_add_device_link(root->fs_info->fs_devices, device);
2313 2283
2314 /* 2284 /*
2315 * we've got more storage, clear any full flags on the space 2285 * we've got more storage, clear any full flags on the space
@@ -2350,7 +2320,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2350 */ 2320 */
2351 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU", 2321 snprintf(fsid_buf, BTRFS_UUID_UNPARSED_SIZE, "%pU",
2352 root->fs_info->fsid); 2322 root->fs_info->fsid);
2353 if (kobject_rename(&root->fs_info->fs_devices->super_kobj, 2323 if (kobject_rename(&root->fs_info->fs_devices->fsid_kobj,
2354 fsid_buf)) 2324 fsid_buf))
2355 btrfs_warn(root->fs_info, 2325 btrfs_warn(root->fs_info,
2356 "sysfs: failed to create fsid for sprout"); 2326 "sysfs: failed to create fsid for sprout");
@@ -2369,7 +2339,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2369 2339
2370 ret = btrfs_relocate_sys_chunks(root); 2340 ret = btrfs_relocate_sys_chunks(root);
2371 if (ret < 0) 2341 if (ret < 0)
2372 btrfs_error(root->fs_info, ret, 2342 btrfs_std_error(root->fs_info, ret,
2373 "Failed to relocate sys chunks after " 2343 "Failed to relocate sys chunks after "
2374 "device initialization. This can be fixed " 2344 "device initialization. This can be fixed "
2375 "using the \"btrfs balance\" command."); 2345 "using the \"btrfs balance\" command.");
@@ -2389,7 +2359,7 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
2389error_trans: 2359error_trans:
2390 btrfs_end_transaction(trans, root); 2360 btrfs_end_transaction(trans, root);
2391 rcu_string_free(device->name); 2361 rcu_string_free(device->name);
2392 btrfs_kobj_rm_device(root->fs_info->fs_devices, device); 2362 btrfs_sysfs_rm_device_link(root->fs_info->fs_devices, device);
2393 kfree(device); 2363 kfree(device);
2394error: 2364error:
2395 blkdev_put(bdev, FMODE_EXCL); 2365 blkdev_put(bdev, FMODE_EXCL);
@@ -2614,7 +2584,7 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2614 if (ret < 0) 2584 if (ret < 0)
2615 goto out; 2585 goto out;
2616 else if (ret > 0) { /* Logic error or corruption */ 2586 else if (ret > 0) { /* Logic error or corruption */
2617 btrfs_error(root->fs_info, -ENOENT, 2587 btrfs_std_error(root->fs_info, -ENOENT,
2618 "Failed lookup while freeing chunk."); 2588 "Failed lookup while freeing chunk.");
2619 ret = -ENOENT; 2589 ret = -ENOENT;
2620 goto out; 2590 goto out;
@@ -2622,7 +2592,7 @@ static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
2622 2592
2623 ret = btrfs_del_item(trans, root, path); 2593 ret = btrfs_del_item(trans, root, path);
2624 if (ret < 0) 2594 if (ret < 0)
2625 btrfs_error(root->fs_info, ret, 2595 btrfs_std_error(root->fs_info, ret,
2626 "Failed to delete chunk item."); 2596 "Failed to delete chunk item.");
2627out: 2597out:
2628 btrfs_free_path(path); 2598 btrfs_free_path(path);
@@ -2807,7 +2777,7 @@ static int btrfs_relocate_chunk(struct btrfs_root *root, u64 chunk_offset)
2807 trans = btrfs_start_transaction(root, 0); 2777 trans = btrfs_start_transaction(root, 0);
2808 if (IS_ERR(trans)) { 2778 if (IS_ERR(trans)) {
2809 ret = PTR_ERR(trans); 2779 ret = PTR_ERR(trans);
2810 btrfs_std_error(root->fs_info, ret); 2780 btrfs_std_error(root->fs_info, ret, NULL);
2811 return ret; 2781 return ret;
2812 } 2782 }
2813 2783
@@ -3462,7 +3432,7 @@ static void __cancel_balance(struct btrfs_fs_info *fs_info)
3462 unset_balance_control(fs_info); 3432 unset_balance_control(fs_info);
3463 ret = del_balance_item(fs_info->tree_root); 3433 ret = del_balance_item(fs_info->tree_root);
3464 if (ret) 3434 if (ret)
3465 btrfs_std_error(fs_info, ret); 3435 btrfs_std_error(fs_info, ret, NULL);
3466 3436
3467 atomic_set(&fs_info->mutually_exclusive_operation_running, 0); 3437 atomic_set(&fs_info->mutually_exclusive_operation_running, 0);
3468} 3438}
@@ -6741,22 +6711,34 @@ int btrfs_get_dev_stats(struct btrfs_root *root,
6741 return 0; 6711 return 0;
6742} 6712}
6743 6713
6744int btrfs_scratch_superblock(struct btrfs_device *device) 6714void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path)
6745{ 6715{
6746 struct buffer_head *bh; 6716 struct buffer_head *bh;
6747 struct btrfs_super_block *disk_super; 6717 struct btrfs_super_block *disk_super;
6718 int copy_num;
6748 6719
6749 bh = btrfs_read_dev_super(device->bdev); 6720 if (!bdev)
6750 if (!bh) 6721 return;
6751 return -EINVAL;
6752 disk_super = (struct btrfs_super_block *)bh->b_data;
6753 6722
6754 memset(&disk_super->magic, 0, sizeof(disk_super->magic)); 6723 for (copy_num = 0; copy_num < BTRFS_SUPER_MIRROR_MAX;
6755 set_buffer_dirty(bh); 6724 copy_num++) {
6756 sync_dirty_buffer(bh);
6757 brelse(bh);
6758 6725
6759 return 0; 6726 if (btrfs_read_dev_one_super(bdev, copy_num, &bh))
6727 continue;
6728
6729 disk_super = (struct btrfs_super_block *)bh->b_data;
6730
6731 memset(&disk_super->magic, 0, sizeof(disk_super->magic));
6732 set_buffer_dirty(bh);
6733 sync_dirty_buffer(bh);
6734 brelse(bh);
6735 }
6736
6737 /* Notify udev that device has changed */
6738 btrfs_kobject_uevent(bdev, KOBJ_CHANGE);
6739
6740 /* Update ctime/mtime for device path for libblkid */
6741 update_dev_time(device_path);
6760} 6742}
6761 6743
6762/* 6744/*
@@ -6824,3 +6806,38 @@ void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info)
6824 fs_devices = fs_devices->seed; 6806 fs_devices = fs_devices->seed;
6825 } 6807 }
6826} 6808}
6809
6810void btrfs_close_one_device(struct btrfs_device *device)
6811{
6812 struct btrfs_fs_devices *fs_devices = device->fs_devices;
6813 struct btrfs_device *new_device;
6814 struct rcu_string *name;
6815
6816 if (device->bdev)
6817 fs_devices->open_devices--;
6818
6819 if (device->writeable &&
6820 device->devid != BTRFS_DEV_REPLACE_DEVID) {
6821 list_del_init(&device->dev_alloc_list);
6822 fs_devices->rw_devices--;
6823 }
6824
6825 if (device->missing)
6826 fs_devices->missing_devices--;
6827
6828 new_device = btrfs_alloc_device(NULL, &device->devid,
6829 device->uuid);
6830 BUG_ON(IS_ERR(new_device)); /* -ENOMEM */
6831
6832 /* Safe because we are under uuid_mutex */
6833 if (device->name) {
6834 name = rcu_string_strdup(device->name->str, GFP_NOFS);
6835 BUG_ON(!name); /* -ENOMEM */
6836 rcu_assign_pointer(new_device->name, name);
6837 }
6838
6839 list_replace_rcu(&device->dev_list, &new_device->dev_list);
6840 new_device->fs_devices = device->fs_devices;
6841
6842 call_rcu(&device->rcu, free_device);
6843}
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 2ca784a14e84..38450dc28837 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -256,7 +256,7 @@ struct btrfs_fs_devices {
256 256
257 struct btrfs_fs_info *fs_info; 257 struct btrfs_fs_info *fs_info;
258 /* sysfs kobjects */ 258 /* sysfs kobjects */
259 struct kobject super_kobj; 259 struct kobject fsid_kobj;
260 struct kobject *device_dir_kobj; 260 struct kobject *device_dir_kobj;
261 struct completion kobj_unregister; 261 struct completion kobj_unregister;
262}; 262};
@@ -474,7 +474,7 @@ void btrfs_destroy_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
474 struct btrfs_device *tgtdev); 474 struct btrfs_device *tgtdev);
475void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info, 475void btrfs_init_dev_replace_tgtdev_for_resume(struct btrfs_fs_info *fs_info,
476 struct btrfs_device *tgtdev); 476 struct btrfs_device *tgtdev);
477int btrfs_scratch_superblock(struct btrfs_device *device); 477void btrfs_scratch_superblocks(struct block_device *bdev, char *device_path);
478int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree, 478int btrfs_is_parity_mirror(struct btrfs_mapping_tree *map_tree,
479 u64 logical, u64 len, int mirror_num); 479 u64 logical, u64 len, int mirror_num);
480unsigned long btrfs_full_stripe_len(struct btrfs_root *root, 480unsigned long btrfs_full_stripe_len(struct btrfs_root *root,
@@ -547,5 +547,6 @@ static inline void unlock_chunks(struct btrfs_root *root)
547struct list_head *btrfs_get_fs_uuids(void); 547struct list_head *btrfs_get_fs_uuids(void);
548void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info); 548void btrfs_set_fs_info_ptr(struct btrfs_fs_info *fs_info);
549void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info); 549void btrfs_reset_fs_info_ptr(struct btrfs_fs_info *fs_info);
550void btrfs_close_one_device(struct btrfs_device *device);
550 551
551#endif 552#endif