loop: Consolidate size calculation logic into lo_calculate_size()
authorRajeev Mishra <rajeevm@hpe.com>
Mon, 18 Aug 2025 18:48:20 +0000 (18:48 +0000)
committerJens Axboe <axboe@kernel.dk>
Mon, 18 Aug 2025 19:10:25 +0000 (13:10 -0600)
Renamed get_size to lo_calculate_size and merged the logic from get_size
and get_loop_size into a single function. Update all callers to use
lo_calculate_size. This is done in preparation for improving the size
detection logic.

Signed-off-by: Rajeev Mishra <rajeevm@hpe.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Link: https://lore.kernel.org/r/20250818184821.115033-2-rajeevm@hpe.com
[axboe: massage commit message]
Signed-off-by: Jens Axboe <axboe@kernel.dk>
drivers/block/loop.c

index 1b6ee91f8eb987071c2e8730cfdcc0ea65ec7f04..0e1b9eb9db10600382222d70703d285bbe233f6a 100644 (file)
@@ -137,20 +137,18 @@ static void loop_global_unlock(struct loop_device *lo, bool global)
 static int max_part;
 static int part_shift;
 
-static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
+static loff_t lo_calculate_size(struct loop_device *lo, struct file *file)
 {
        loff_t loopsize;
-
        /* Compute loopsize in bytes */
        loopsize = i_size_read(file->f_mapping->host);
-       if (offset > 0)
-               loopsize -= offset;
+       if (lo->lo_offset > 0)
+               loopsize -= lo->lo_offset;
        /* offset is beyond i_size, weird but possible */
        if (loopsize < 0)
                return 0;
-
-       if (sizelimit > 0 && sizelimit < loopsize)
-               loopsize = sizelimit;
+       if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
+               loopsize = lo->lo_sizelimit;
        /*
         * Unfortunately, if we want to do I/O on the device,
         * the number of 512-byte sectors has to fit into a sector_t.
@@ -158,11 +156,6 @@ static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
        return loopsize >> 9;
 }
 
-static loff_t get_loop_size(struct loop_device *lo, struct file *file)
-{
-       return get_size(lo->lo_offset, lo->lo_sizelimit, file);
-}
-
 /*
  * We support direct I/O only if lo_offset is aligned with the logical I/O size
  * of backing device, and the logical block size of loop is bigger than that of
@@ -569,7 +562,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
        error = -EINVAL;
 
        /* size of the new backing store needs to be the same */
-       if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
+       if (lo_calculate_size(lo, file) != lo_calculate_size(lo, old_file))
                goto out_err;
 
        /*
@@ -1063,7 +1056,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
        loop_update_dio(lo);
        loop_sysfs_init(lo);
 
-       size = get_loop_size(lo, file);
+       size = lo_calculate_size(lo, file);
        loop_set_size(lo, size);
 
        /* Order wrt reading lo_state in loop_validate_file(). */
@@ -1255,8 +1248,7 @@ out_unfreeze:
        if (partscan)
                clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
        if (!err && size_changed) {
-               loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
-                                          lo->lo_backing_file);
+               loff_t new_size = lo_calculate_size(lo, lo->lo_backing_file);
                loop_set_size(lo, new_size);
        }
 out_unlock:
@@ -1399,7 +1391,7 @@ static int loop_set_capacity(struct loop_device *lo)
        if (unlikely(lo->lo_state != Lo_bound))
                return -ENXIO;
 
-       size = get_loop_size(lo, lo->lo_backing_file);
+       size = lo_calculate_size(lo, lo->lo_backing_file);
        loop_set_size(lo, size);
 
        return 0;