From: Rajeev Mishra <rajeevm@hpe.com>
To: axboe@kernel.dk, yukuai1@huaweicloud.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
dlemoal@kernel.org
Subject: Re: [PATCH v2] loop: use vfs_getattr_nosec() for accurate file size
Date: Tue, 12 Aug 2025 03:32:01 +0000 [thread overview]
Message-ID: <20250812033201.225425-1-rajeevm@hpe.com> (raw)
In-Reply-To: <a8041180-03f2-3342-b568-867b3f295239@huaweicloud.com>
Hi Kuai,
Thank you for the feedback on the v2 patch regarding error handling.
Yu mentioned:
> return 0 here is odd. Why not "return ret;" to propagate the error if any ?
I understand the concern about proper error propagation. However, there's a
type compatibility issue I'd like to discuss before implementing v3:
1. Current function signature: `static loff_t get_size(...)`
- Returns size as positive loff_t (unsigned 64-bit)
- All callers expect non-negative size values
2. vfs_getattr_nosec() error codes are negative integers (-ENOENT, -EIO, etc.)
- Returning `ret` would cast negative errors to huge positive numbers
- This could cause loop devices to appear as exabyte-sized
3. Current callers like loop_set_size() don't handle error checking
Would you prefer for v3:
a) Change function signature to `int get_size(..., loff_t *size)` and update all callers
b) Different approach?
diff with ret approach
diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index c418c47db76e..15117630c6c1 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -142,12 +142,13 @@ static int part_shift;
* @offset: offset into the backing file
* @sizelimit: user-specified size limit
* @file: the backing file
+ * @size: pointer to store the calculated size
*
* Calculate the effective size of the loop device
*
- * Returns: size in 512-byte sectors, or 0 if invalid
+ * Returns: 0 on success, negative error code on failure
*/
-static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
+static int get_size(loff_t offset, loff_t sizelimit, struct file *file, loff_t *size)
{
struct kstat stat;
loff_t loopsize;
@@ -159,7 +160,7 @@ static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
*/
ret = vfs_getattr_nosec(&file->f_path, &stat, STATX_SIZE, 0);
if (ret)
- return 0;
+ return ret;
loopsize = stat.size;
@@ -167,7 +168,7 @@ static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
loopsize -= offset;
/* offset is beyond i_size, weird but possible */
if (loopsize < 0)
- return 0;
+ return -EINVAL;
if (sizelimit > 0 && sizelimit < loopsize)
loopsize = sizelimit;
@@ -175,12 +176,20 @@ static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
* Unfortunately, if we want to do I/O on the device,
* the number of 512-byte sectors has to fit into a sector_t.
*/
- return loopsize >> 9;
+ *size = loopsize >> 9;
+ return 0;
}
static loff_t get_loop_size(struct loop_device *lo, struct file *file)
{
- return get_size(lo->lo_offset, lo->lo_sizelimit, file);
+ loff_t size;
+ int ret;
+
+ ret = get_size(lo->lo_offset, lo->lo_sizelimit, file, &size);
+ if (ret)
+ return 0; /* Fallback to 0 on error for backward compatibility */
+
+ return size;
}
I am happy to implement whichever direction you think is best.
Thanks,
Rajeev
next prev parent reply other threads:[~2025-08-12 3:41 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
2025-08-12 2:05 ` Yu Kuai
2025-08-12 3:32 ` Rajeev Mishra [this message]
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=20250812033201.225425-1-rajeevm@hpe.com \
--to=rajeevm@hpe.com \
--cc=axboe@kernel.dk \
--cc=dlemoal@kernel.org \
--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®