This is a note to let you know that I've just added the patch titled loop: Factor out setting loop device size to the 4.19-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: loop-factor-out-setting-loop-device-size.patch and it can be found in the queue-4.19 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From stable+bounces-25713-greg=kroah.com@xxxxxxxxxxxxxxx Fri Mar 1 02:33:42 2024 From: Genjian <zhanggenjian@xxxxxxx> Date: Fri, 1 Mar 2024 09:30:23 +0800 Subject: loop: Factor out setting loop device size To: stable@xxxxxxxxxxxxxxx Cc: axboe@xxxxxxxxx, stable@xxxxxxxxxx, linux-block@xxxxxxxxxxxxxxx, linux-kernel@xxxxxxxxxxxxxxx, zhanggenjian123@xxxxxxxxx, Martijn Coenen <maco@xxxxxxxxxxx>, Christoph Hellwig <hch@xxxxxx>, Genjian Zhang <zhanggenjian@xxxxxxxxxx> Message-ID: <20240301013028.2293831-5-zhanggenjian@xxxxxxx> From: Martijn Coenen <maco@xxxxxxxxxxx> [ Upstream commit 5795b6f5607f7e4db62ddea144727780cb351a9b ] This code is used repeatedly. Signed-off-by: Martijn Coenen <maco@xxxxxxxxxxx> Reviewed-by: Christoph Hellwig <hch@xxxxxx> Signed-off-by: Jens Axboe <axboe@xxxxxxxxx> Signed-off-by: Genjian Zhang <zhanggenjian@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- drivers/block/loop.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -225,20 +225,35 @@ static void __loop_update_dio(struct loo 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, eg using loop_validate_size() + */ +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); +} + static void figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) { loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); - struct block_device *bdev = lo->lo_device; if (lo->lo_offset != offset) lo->lo_offset = offset; if (lo->lo_sizelimit != sizelimit) lo->lo_sizelimit = sizelimit; - set_capacity(lo->lo_disk, size); - bd_set_size(bdev, (loff_t)get_capacity(bdev->bd_disk) << 9); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + + loop_set_size(lo, size); } static inline int @@ -992,11 +1007,8 @@ static int loop_set_fd(struct loop_devic blk_queue_write_cache(lo->lo_queue, true, false); loop_update_dio(lo); - set_capacity(lo->lo_disk, size); - bd_set_size(bdev, size << 9); loop_sysfs_init(lo); - /* let user-space know about the new size */ - kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE); + loop_set_size(lo, size); set_blocksize(bdev, S_ISBLK(inode->i_mode) ? block_size(inode->i_bdev) : PAGE_SIZE); Patches currently in stable-queue which might be from kroah.com@xxxxxxxxxxxxxxx are queue-4.19/loop-factor-out-configuring-loop-from-status.patch queue-4.19/loop-call-loop_config_discard-only-after-new-config-is-applied.patch queue-4.19/loop-refactor-loop_set_status-size-calculation.patch queue-4.19/loop-factor-out-setting-loop-device-size.patch queue-4.19/loop-check-for-overflow-while-configuring-loop.patch queue-4.19/loop-properly-observe-rotational-flag-of-underlying-device.patch queue-4.19/loop-remove-sector_t-truncation-checks.patch queue-4.19/revert-loop-check-for-overflow-while-configuring-loop.patch queue-4.19/loop-loop_set_status_from_info-check-before-assignment.patch