Re: [PATCH v2 1/5] loop: Refactor size calculation

From: Christoph Hellwig
Date: Wed Apr 22 2020 - 13:39:58 EST


On Wed, Apr 22, 2020 at 12:06:32PM +0200, Martijn Coenen wrote:
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index f1754262fc94..4f5c765c73d8 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -228,23 +228,42 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
> blk_mq_unfreeze_queue(lo->lo_queue);
> }
>
> +/**
> + * loop_set_size - sets device size and notifies userspace
> + * @lo: struct loop_device to set the size for
> + * @size: new size of the loop device
> + *
> + * Callers must validate that the size passed into this function fits into
> + * a sector_t.
> + */
> +static void loop_set_size(struct loop_device *lo, loff_t size)
> +{
> + struct block_device *bdev = lo->lo_device;
> +
> + set_capacity(lo->lo_disk, size);
> + bd_set_size(bdev, size << SECTOR_SHIFT);
> + /* let user-space know about the new size */
> + kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);

I think this should use set_capacity_revalidate_and_notify, although
that probably should be a separate patch.

Also I'm having a bit of a hard time following all the different changes
(even if the all look good). Maybe at least the loop_set_size
factoring should be split into another separate prep patch.

> +/**
> + * loop_update_size - updates device size from the backing file
> + * @lo: struct loop_device to update the size for
> + *
> + * Recomputes the device size from the backing file, and updates the device
> + * with the new size.
> + */
> static int
> -figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
> +loop_update_size(struct loop_device *lo)
> {
> + loff_t size = get_loop_size(lo, lo->lo_backing_file);
> sector_t x = (sector_t)size;
>
> if (unlikely((loff_t)x != size))
> return -EFBIG;
> +
> + loop_set_size(lo, size);
> +
> return 0;
> }

With all the other nice helper, I think this can actually be open coded
in the caller. Another useful helper would be one that checks for the
size truncation, as that is duplicated in a few places.