Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

loop: use vfs_getattr_nosec for accurate file size

Use vfs_getattr_nosec() in lo_calculate_size() for getting the file
size, rather than just read the cached inode size via i_size_read().
This provides better results than cached inode data, particularly for
network filesystems where metadata may be stale.

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

authored by

Rajeev Mishra and committed by
Jens Axboe
47b71abd 8aa5a3b6

+13 -2
+13 -2
drivers/block/loop.c
··· 139 139 140 140 static loff_t lo_calculate_size(struct loop_device *lo, struct file *file) 141 141 { 142 + struct kstat stat; 142 143 loff_t loopsize; 143 - /* Compute loopsize in bytes */ 144 - loopsize = i_size_read(file->f_mapping->host); 144 + int ret; 145 + 146 + /* 147 + * Get the accurate file size. This provides better results than 148 + * cached inode data, particularly for network filesystems where 149 + * metadata may be stale. 150 + */ 151 + ret = vfs_getattr_nosec(&file->f_path, &stat, STATX_SIZE, 0); 152 + if (ret) 153 + return 0; 154 + 155 + loopsize = stat.size; 145 156 if (lo->lo_offset > 0) 146 157 loopsize -= lo->lo_offset; 147 158 /* offset is beyond i_size, weird but possible */