From: Cloud User <rajeevm@hpe.com>
To: Yu Kuai <yukuai1@huaweicloud.com>,
ming.lei@redhat.com, hch@infradead.org, yukuai3@huawei.com,
rajeevm@hpe.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
yi.zhang@huawei.com, yangerkun@huawei.com,
johnny.chenyi@huawei.com
Subject: [PATCH 2/2] loop: use vfs_getattr_nosec for accurate file size
Date: Mon, 25 Aug 2025 22:17:41 +0000 [thread overview]
Message-ID: <20250825221741.3714488-3-rajeevm@hpe.com> (raw)
In-Reply-To: <20250825221741.3714488-1-rajeevm@hpe.com>
From: Rajeev Mishra <rajeevm@hpe.com>
In lo_calculate_size(), use vfs_getattr_nosec() for non-block files.
For block files, continue using i_size_read(). This improves size
accuracy on network and distributed filesystems.
Signed-off-by: Rajeev Mishra <rajeevm@hpe.com>
---
drivers/block/loop.c | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 0e1b9eb9db10..a6214ae40ef0 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -139,9 +139,24 @@ static int part_shift;
static loff_t lo_calculate_size(struct loop_device *lo, struct file *file)
{
+ struct kstat stat;
loff_t loopsize;
- /* Compute loopsize in bytes */
- loopsize = i_size_read(file->f_mapping->host);
+ int ret;
+
+ /*
+ * Get the accurate file size. This provides better results than
+ * cached inode data, particularly for network filesystems where
+ * metadata may be stale.
+ */
+ if (S_ISBLK(file->f_inode->i_mode)) {
+ loopsize = i_size_read(file->f_mapping->host);
+ } else {
+ ret = vfs_getattr_nosec(&file->f_path, &stat, STATX_SIZE, 0);
+ if (ret)
+ return 0;
+ loopsize = stat.size;
+ }
+
if (lo->lo_offset > 0)
loopsize -= lo->lo_offset;
/* offset is beyond i_size, weird but possible */
--
2.47.3
next prev parent reply other threads:[~2025-08-25 22:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-25 22:17 [PATCH v5] fixed the blok file statx issue Cloud User
2025-08-25 22:17 ` [PATCH 1/2] loop: Rename and merge get_size/get_loop_size to lo_calculate_size Cloud User
2025-08-25 22:17 ` Cloud User [this message]
2025-08-26 0:49 ` [PATCH v5] fixed the blok file statx issue Yu Kuai
2025-08-26 2:58 ` Cloud User
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=20250825221741.3714488-3-rajeevm@hpe.com \
--to=rajeevm@hpe.com \
--cc=hch@infradead.org \
--cc=johnny.chenyi@huawei.com \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ming.lei@redhat.com \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai1@huaweicloud.com \
--cc=yukuai3@huawei.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®