All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kyle Ambroff-Kao <kyle@ambroffkao.com>
To: linux-btrfs@vger.kernel.org
Cc: Kyle Ambroff-Kao <kyle@ambroffkao.com>
Subject: [PATCH 1/1] btrfs: Allow replacing device with a smaller one if possible
Date: Sun,  8 Dec 2019 01:30:45 -0800	[thread overview]
Message-ID: <20191208093045.43433-2-kyle@ambroffkao.com> (raw)
In-Reply-To: <20191208093045.43433-1-kyle@ambroffkao.com>

As long as the target device has enough capacity for the total bytes
of the source device, allow the replacement to occur.

Just changing the size validation isn't enough though, since the
rest of the replacement code just assumes that the source device is
identical to the target device. The current code just blindly
copies the total disk size from the source to the target.

A btrfs resize <devid>:max could be performed, but we might as well
just set the disk size for the new target device correctly in the
first place before initiating a scrub, which is what this patch does.

When initializing the target device, the size in bytes is calculated
in the same way that btrfs_init_new_device does it.

When the replace operation completes, btrfs_dev_replace_finishing no
longer clobbers the target device size with the source device size.

Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=112741
Signed-off-by: Kyle Ambroff-Kao <kyle@ambroffkao.com>
---
 fs/btrfs/dev-replace.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/dev-replace.c b/fs/btrfs/dev-replace.c
index f639dde2a679..6a7a83ccab56 100644
--- a/fs/btrfs/dev-replace.c
+++ b/fs/btrfs/dev-replace.c
@@ -216,7 +216,7 @@ static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 
 
 	if (i_size_read(bdev->bd_inode) <
-	    btrfs_device_get_total_bytes(srcdev)) {
+	    btrfs_device_get_bytes_used(srcdev)) {
 		btrfs_err(fs_info,
 			  "target device is smaller than source device!");
 		ret = -EINVAL;
@@ -243,8 +243,10 @@ static int btrfs_init_dev_replace_tgtdev(struct btrfs_fs_info *fs_info,
 	device->io_width = fs_info->sectorsize;
 	device->io_align = fs_info->sectorsize;
 	device->sector_size = fs_info->sectorsize;
-	device->total_bytes = btrfs_device_get_total_bytes(srcdev);
-	device->disk_total_bytes = btrfs_device_get_disk_total_bytes(srcdev);
+	device->total_bytes = round_down(
+		i_size_read(bdev->bd_inode),
+		fs_info->sectorsize);
+	device->disk_total_bytes = device->total_bytes;
 	device->bytes_used = btrfs_device_get_bytes_used(srcdev);
 	device->commit_total_bytes = srcdev->commit_total_bytes;
 	device->commit_bytes_used = device->bytes_used;
@@ -671,9 +673,6 @@ static int btrfs_dev_replace_finishing(struct btrfs_fs_info *fs_info,
 	memcpy(uuid_tmp, tgt_device->uuid, sizeof(uuid_tmp));
 	memcpy(tgt_device->uuid, src_device->uuid, sizeof(tgt_device->uuid));
 	memcpy(src_device->uuid, uuid_tmp, sizeof(src_device->uuid));
-	btrfs_device_set_total_bytes(tgt_device, src_device->total_bytes);
-	btrfs_device_set_disk_total_bytes(tgt_device,
-					  src_device->disk_total_bytes);
 	btrfs_device_set_bytes_used(tgt_device, src_device->bytes_used);
 	tgt_device->commit_bytes_used = src_device->bytes_used;
 
-- 
2.20.1


  reply	other threads:[~2019-12-08  9:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-08  9:30 [PATCH 0/1] Support replacing a device with smaller one Kyle Ambroff-Kao
2019-12-08  9:30 ` Kyle Ambroff-Kao [this message]
2019-12-09 10:26   ` [PATCH 1/1] btrfs: Allow replacing device with a smaller one if possible Filipe Manana
2019-12-09 19:22     ` Kyle Ambroff-Kao
2019-12-13 15:48     ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191208093045.43433-2-kyle@ambroffkao.com \
    --to=kyle@ambroffkao.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.