[PATCH 1/1] btrfs: Allow replacing device with a smaller one if possible

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx>
---
 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




[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux