diff mbox series

[01/10] btrfs: add a helper for opening a new device to add to the fs

Message ID 280fe61f3405e88a0a4a5ac0cacff3993f9f31ff.1663196746.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs: clean up zoned device helpers | expand

Commit Message

Josef Bacik Sept. 14, 2022, 11:07 p.m. UTC
We currently duplicate our device init code for adding a new device to a
file system and replacing an existing device in a file system.  These
two init functions are subtle-y different, however they both open the
disk and check it's zoned status to see if it's compatible.  Combine
this step into a single helper and use that helper from both init
functions.  The goal of this change is to move the zoned helper out of
zoned.h in order to avoid using helpers that aren't defined in zoned.h.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/dev-replace.c | 10 +---------
 fs/btrfs/volumes.c     | 28 +++++++++++++++++++++-------
 fs/btrfs/volumes.h     |  2 ++
 3 files changed, 24 insertions(+), 16 deletions(-)

Comments

Johannes Thumshirn Sept. 15, 2022, 1:54 p.m. UTC | #1
Looks good,
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Pankaj Raghav Sept. 21, 2022, 9:39 a.m. UTC | #2
>  	list_for_each_entry(device, &fs_devices->devices, dev_list) {
> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
> index 09c1434c3cae..ea76458d7c70 100644
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -2583,6 +2583,26 @@ static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
>  	return ret;
>  }
>  
> +struct block_device *btrfs_open_device_for_adding(struct btrfs_fs_info *fs_info,
> +						  const char *device_path)
> +{
> +	struct block_device *bdev;
> +
> +	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
> +				  fs_info->bdev_holder);
> +	if (IS_ERR(bdev))
> +		return bdev;
> +
> +	if (!btrfs_check_device_zone_type(fs_info, bdev)) {
> +		btrfs_err(fs_info,
> +			  "dev-replace: zoned type of target device mismatch with filesystem");

Shouldn't the `dev-replace` be removed as this helper is used from
dev-replace.c and volumes.c?

> +		blkdev_put(bdev, FMODE_EXCL);
> +		return ERR_PTR(-EINVAL);
> +	}
> +
> +	return bdev;
> +}
> +
>  int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
>  {
>  	struct btrfs_root *root = fs_info->dev_root;
> @@ -2602,16 +2622,10 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
>  	if (sb_rdonly(sb) && !fs_devices->seeding)
>  		return -EROFS;
>  
> -	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
> -				  fs_info->bdev_holder);
> +	bdev = btrfs_open_device_for_adding(fs_info, device_path);
diff mbox series

Patch

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index c6222057f655..8f5922ba0129 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -258,20 +258,12 @@  static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 		return -EINVAL;
 	}
 
-	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
-				  fs_info->bdev_holder);
+	bdev = btrfs_open_device_for_adding(fs_info, device_path);
 	if (IS_ERR(bdev)) {
 		btrfs_err(fs_info, "target device %s is invalid!", device_path);
 		return PTR_ERR(bdev);
 	}
 
-	if (!btrfs_check_device_zone_type(fs_info, bdev)) {
-		btrfs_err(fs_info,
-		"dev-replace: zoned type of target device mismatch with filesystem");
-		ret = -EINVAL;
-		goto error;
-	}
-
 	sync_blockdev(bdev);
 
 	list_for_each_entry(device, &fs_devices->devices, dev_list) {
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index 09c1434c3cae..ea76458d7c70 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2583,6 +2583,26 @@  static int btrfs_finish_sprout(struct btrfs_trans_handle *trans)
 	return ret;
 }
 
+struct block_device *btrfs_open_device_for_adding(struct btrfs_fs_info *fs_info,
+						  const char *device_path)
+{
+	struct block_device *bdev;
+
+	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
+				  fs_info->bdev_holder);
+	if (IS_ERR(bdev))
+		return bdev;
+
+	if (!btrfs_check_device_zone_type(fs_info, bdev)) {
+		btrfs_err(fs_info,
+			  "dev-replace: zoned type of target device mismatch with filesystem");
+		blkdev_put(bdev, FMODE_EXCL);
+		return ERR_PTR(-EINVAL);
+	}
+
+	return bdev;
+}
+
 int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path)
 {
 	struct btrfs_root *root = fs_info->dev_root;
@@ -2602,16 +2622,10 @@  int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_path
 	if (sb_rdonly(sb) && !fs_devices->seeding)
 		return -EROFS;
 
-	bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
-				  fs_info->bdev_holder);
+	bdev = btrfs_open_device_for_adding(fs_info, device_path);
 	if (IS_ERR(bdev))
 		return PTR_ERR(bdev);
 
-	if (!btrfs_check_device_zone_type(fs_info, bdev)) {
-		ret = -EINVAL;
-		goto error;
-	}
-
 	if (fs_devices->seeding) {
 		seeding_dev = true;
 		down_write(&sb->s_umount);
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 2bf7dbe739fd..60d74554edf2 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -757,4 +757,6 @@  int btrfs_verify_dev_extents(struct btrfs_fs_info *fs_info);
 bool btrfs_repair_one_zone(struct btrfs_fs_info *fs_info, u64 logical);
 unsigned long btrfs_chunk_item_size(int num_stripes);
 bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
+struct block_device *btrfs_open_device_for_adding(struct btrfs_fs_info *fs_info,
+						  const char *device_path);
 #endif