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 12:42:44 +0900 [thread overview]
Message-ID: <34624336-331d-4047-822f-8091098eeebc@kernel.org> (raw)
In-Reply-To: <20250812033201.225425-1-rajeevm@hpe.com>
On 8/12/25 12:32 PM, Rajeev Mishra wrote:
> 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)
Since loff_t is "long long", so a signed type, I would keep this interface and
add a negative error check in the 2 call sites for get_size(). That is simpler.
> +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
--
Damien Le Moal
Western Digital Research
next prev parent reply other threads:[~2025-08-12 3:45 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
2025-08-12 3:42 ` Damien Le Moal [this message]
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=34624336-331d-4047-822f-8091098eeebc@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®