From: Damien Le Moal <dlemoal@kernel.org>
To: Rajeev Mishra <rajeevm@hpe.com>,
axboe@kernel.dk, yukuai1@huaweicloud.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] loop: use vfs_getattr_nosec() for accurate file size
Date: Tue, 12 Aug 2025 10:40:21 +0900 [thread overview]
Message-ID: <45b33232-9089-4df4-b9f8-c843cb3d2d07@kernel.org> (raw)
In-Reply-To: <20250811190303.222802-1-rajeevm@hpe.com>
On 8/12/25 4:03 AM, Rajeev Mishra wrote:
> The get_size() function now uses vfs_getattr_nosec() instead of
> i_size_read() to obtain file size information. This provides more
> accurate results for network filesystems where cached metadata
> may be stale, ensuring the loop device reflects the current file
> size rather than potentially outdated cached values.
>
> Signed-off-by: Rajeev Mishra <rajeevm@hpe.com>
> ---
> drivers/block/loop.c | 24 ++++++++++++++++++++++--
> 1 file changed, 22 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/block/loop.c b/drivers/block/loop.c
> index 1b6ee91f8eb9..c418c47db76e 100644
> --- a/drivers/block/loop.c
> +++ b/drivers/block/loop.c
> @@ -137,12 +137,32 @@ 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 the accurate file size. This will prevent caching
> + * issue that occurs at filesystem layer.
> + */
> + ret = vfs_getattr_nosec(&file->f_path, &stat, STATX_SIZE, 0);
> + if (ret)
> + return 0;
return 0 here is odd. Why not "return ret;" to propagate the error if any ?
An error may come from the underlying FS inode->i_op->getattr().
> +
> + 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 */
--
Damien Le Moal
Western Digital Research
next prev parent reply other threads:[~2025-08-12 1:43 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-11 19:03 Rajeev Mishra
2025-08-12 1:40 ` Damien Le Moal [this message]
2025-08-12 2:05 ` Yu Kuai
2025-08-12 3:32 ` Rajeev Mishra
2025-08-12 3:42 ` Damien Le Moal
2025-08-12 4:28 ` Al Viro
2025-08-12 5:17 ` Damien Le Moal
2025-08-12 8:37 ` Christoph Hellwig
2025-08-12 9:34 ` Yu Kuai
2025-08-12 8:39 ` Christoph Hellwig
2025-08-12 4:25 ` Al Viro
2025-08-12 8:39 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=45b33232-9089-4df4-b9f8-c843cb3d2d07@kernel.org \
--to=dlemoal@kernel.org \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rajeevm@hpe.com \
--cc=yukuai1@huaweicloud.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®