Skip to content

Commit 8aa5a3b

Browse files
Rajeev Mishraaxboe
authored andcommitted
loop: Consolidate size calculation logic into lo_calculate_size()
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 <[email protected]> Reviewed-by: Yu Kuai <[email protected]> Link: https://lore.kernel.org/r/[email protected] [axboe: massage commit message] Signed-off-by: Jens Axboe <[email protected]>
1 parent 7242169 commit 8aa5a3b

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

drivers/block/loop.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -137,32 +137,25 @@ static void loop_global_unlock(struct loop_device *lo, bool global)
137137
static int max_part;
138138
static int part_shift;
139139

140-
static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
140+
static loff_t lo_calculate_size(struct loop_device *lo, struct file *file)
141141
{
142142
loff_t loopsize;
143-
144143
/* Compute loopsize in bytes */
145144
loopsize = i_size_read(file->f_mapping->host);
146-
if (offset > 0)
147-
loopsize -= offset;
145+
if (lo->lo_offset > 0)
146+
loopsize -= lo->lo_offset;
148147
/* offset is beyond i_size, weird but possible */
149148
if (loopsize < 0)
150149
return 0;
151-
152-
if (sizelimit > 0 && sizelimit < loopsize)
153-
loopsize = sizelimit;
150+
if (lo->lo_sizelimit > 0 && lo->lo_sizelimit < loopsize)
151+
loopsize = lo->lo_sizelimit;
154152
/*
155153
* Unfortunately, if we want to do I/O on the device,
156154
* the number of 512-byte sectors has to fit into a sector_t.
157155
*/
158156
return loopsize >> 9;
159157
}
160158

161-
static loff_t get_loop_size(struct loop_device *lo, struct file *file)
162-
{
163-
return get_size(lo->lo_offset, lo->lo_sizelimit, file);
164-
}
165-
166159
/*
167160
* We support direct I/O only if lo_offset is aligned with the logical I/O size
168161
* 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,
569562
error = -EINVAL;
570563

571564
/* size of the new backing store needs to be the same */
572-
if (get_loop_size(lo, file) != get_loop_size(lo, old_file))
565+
if (lo_calculate_size(lo, file) != lo_calculate_size(lo, old_file))
573566
goto out_err;
574567

575568
/*
@@ -1063,7 +1056,7 @@ static int loop_configure(struct loop_device *lo, blk_mode_t mode,
10631056
loop_update_dio(lo);
10641057
loop_sysfs_init(lo);
10651058

1066-
size = get_loop_size(lo, file);
1059+
size = lo_calculate_size(lo, file);
10671060
loop_set_size(lo, size);
10681061

10691062
/* Order wrt reading lo_state in loop_validate_file(). */
@@ -1255,8 +1248,7 @@ loop_set_status(struct loop_device *lo, const struct loop_info64 *info)
12551248
if (partscan)
12561249
clear_bit(GD_SUPPRESS_PART_SCAN, &lo->lo_disk->state);
12571250
if (!err && size_changed) {
1258-
loff_t new_size = get_size(lo->lo_offset, lo->lo_sizelimit,
1259-
lo->lo_backing_file);
1251+
loff_t new_size = lo_calculate_size(lo, lo->lo_backing_file);
12601252
loop_set_size(lo, new_size);
12611253
}
12621254
out_unlock:
@@ -1399,7 +1391,7 @@ static int loop_set_capacity(struct loop_device *lo)
13991391
if (unlikely(lo->lo_state != Lo_bound))
14001392
return -ENXIO;
14011393

1402-
size = get_loop_size(lo, lo->lo_backing_file);
1394+
size = lo_calculate_size(lo, lo->lo_backing_file);
14031395
loop_set_size(lo, size);
14041396

14051397
return 0;

0 commit comments

Comments
 (0)