[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <20250807232522.192898-1-rajeevm@hpe.com>
Date: Thu, 7 Aug 2025 23:25:22 +0000
From: Rajeev Mishra <rajeevm@....com>
To: axboe@...nel.dk
Cc: linux-block@...r.kernel.org, linux-kernel@...r.kernel.org,
Rajeev Mishra <rajeevm@....com>
Subject: [PATCH 1/1] loop: sync filesystem cache before getting file size in get_size()
The get_size() function now uses vfs_getattr_nosec() with AT_STATX_SYNC_AS_STAT
to ensure filesystem cache is synchronized before retrieving file size. This
provides more accurate size information, especially when:
- The backing file size has been changed by another process
- The file is on a network filesystem (NFS, CIFS, etc.)
- The file is being modified concurrently
- The most accurate size is needed for loop device setup
The implementation gracefully falls back to i_size_read() if vfs_getattr_nosec()
fails, maintaining backward compatibility.
Signed-off-by: Rajeev Mishra <rajeevm@....com>
---
drivers/block/loop.c | 31 +++++++++++++++++++++++++++++--
1 file changed, 29 insertions(+), 2 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 1b6ee91f8eb9..15d5edbc69ce 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -137,12 +137,39 @@ static void loop_global_unlock(struct loop_device *lo, bool global)
static int max_part;
static int part_shift;
+/**
+ * get_size - calculate the effective size of a loop device
+ * @offset: offset into the backing file
+ * @sizelimit: user-specified size limit
+ * @file: the backing file
+ *
+ * Calculate the effective size of the loop device
+ *
+ * Returns: size in 512-byte sectors, or 0 if invalid
+ */
static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
{
+ struct kstat stat;
loff_t loopsize;
+ int ret;
+
+ /*
+ * Get file attributes for validation. We use vfs_getattr() to ensure
+ * we have up-to-date file size information.
+ */
+ ret = vfs_getattr_nosec(&file->f_path, &stat, STATX_SIZE,
+ AT_STATX_SYNC_AS_STAT);
+ if (ret) {
+ /*
+ * If we can't get attributes, fall back to i_size_read()
+ * which should work for most cases.
+ */
+ loopsize = i_size_read(file->f_mapping->host);
+ } else {
+ /* Use the size from getattr for consistency */
+ loopsize = stat.size;
+ }
- /* Compute loopsize in bytes */
- loopsize = i_size_read(file->f_mapping->host);
if (offset > 0)
loopsize -= offset;
/* offset is beyond i_size, weird but possible */
--
2.43.5
Powered by blists - more mailing lists