Commit 5795b6f5 authored by Martijn Coenen's avatar Martijn Coenen Committed by Jens Axboe

loop: Factor out setting loop device size

This code is used repeatedly.
Signed-off-by: default avatarMartijn Coenen <maco@android.com>
Reviewed-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 083a6a50
...@@ -228,20 +228,35 @@ static void __loop_update_dio(struct loop_device *lo, bool dio) ...@@ -228,20 +228,35 @@ static void __loop_update_dio(struct loop_device *lo, bool dio)
blk_mq_unfreeze_queue(lo->lo_queue); 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 static void
figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit) figure_loop_size(struct loop_device *lo, loff_t offset, loff_t sizelimit)
{ {
loff_t size = get_size(offset, sizelimit, lo->lo_backing_file); loff_t size = get_size(offset, sizelimit, lo->lo_backing_file);
struct block_device *bdev = lo->lo_device;
if (lo->lo_offset != offset) if (lo->lo_offset != offset)
lo->lo_offset = offset; lo->lo_offset = offset;
if (lo->lo_sizelimit != sizelimit) if (lo->lo_sizelimit != sizelimit)
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); loop_set_size(lo, size);
/* let user-space know about the new size */
kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
} }
static inline int static inline int
...@@ -1034,11 +1049,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode, ...@@ -1034,11 +1049,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
loop_update_rotational(lo); loop_update_rotational(lo);
loop_update_dio(lo); loop_update_dio(lo);
set_capacity(lo->lo_disk, size);
bd_set_size(bdev, size << 9);
loop_sysfs_init(lo); loop_sysfs_init(lo);
/* let user-space know about the new size */ loop_set_size(lo, size);
kobject_uevent(&disk_to_dev(bdev->bd_disk)->kobj, KOBJ_CHANGE);
set_blocksize(bdev, S_ISBLK(inode->i_mode) ? set_blocksize(bdev, S_ISBLK(inode->i_mode) ?
block_size(inode->i_bdev) : PAGE_SIZE); block_size(inode->i_bdev) : PAGE_SIZE);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment