mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rajeev Mishra <rajeevm@hpe.com>
To: axboe@kernel.dk, yukuai1@huaweicloud.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	Rajeev Mishra <rajeevm@hpe.com>
Subject: [PATCH v4 2/2] loop: use vfs_getattr_nosec for accurate file size
Date: Wed, 13 Aug 2025 19:52:27 +0000	[thread overview]
Message-ID: <20250813195228.12021-3-rajeevm@hpe.com> (raw)
In-Reply-To: <20250813195228.12021-1-rajeevm@hpe.com>

- Use vfs_getattr_nosec() instead of i_size_read() in lo_calculate_size.
- Improves accuracy for network/distributed filesystems.

Signed-off-by: Rajeev Mishra <rajeevm@hpe.com>
---
 drivers/block/loop.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index 5faf8607dfb2..0a2703eda2c2 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -139,9 +139,20 @@ 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.
+	*/
+       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.43.7


  parent reply	other threads:[~2025-08-13 19:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-13 19:52 [PATCH v4 0/2] review of v3 loop: use vfs_getattr_nosec() Rajeev Mishra
2025-08-13 19:52 ` [PATCH v4 1/2] loop: Rename and merge get_size/get_loop_size to lo_calculate_size Rajeev Mishra
2025-08-14  6:38   ` Yu Kuai
2025-08-13 19:52 ` Rajeev Mishra [this message]
2025-08-14 19:10 Rajeev Mishra
2025-08-14 19:10 ` [PATCH v4 2/2] loop: use vfs_getattr_nosec for accurate file size Rajeev Mishra
2025-08-18  2:30   ` Yu Kuai
2025-08-20  4:55   ` kernel test robot
2025-08-20  7:17     ` Yu Kuai
2025-08-18 18:48 fixed commit message Rajeev Mishra
2025-08-18 18:48 ` [PATCH v4 2/2] loop: use vfs_getattr_nosec for accurate file size Rajeev Mishra

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=20250813195228.12021-3-rajeevm@hpe.com \
    --to=rajeevm@hpe.com \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --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®