All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@infradead.org>
To: Rajeev Mishra <rajeevm@hpe.com>
Cc: axboe@kernel.dk, linux-block@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] loop: sync filesystem cache before getting file size in get_size()
Date: Sun, 10 Aug 2025 08:15:08 -0700	[thread overview]
Message-ID: <aJi3fFwlqb-SfHGg@infradead.org> (raw)
In-Reply-To: <20250807232522.192898-1-rajeevm@hpe.com>

On Thu, Aug 07, 2025 at 11:25:22PM +0000, Rajeev Mishra wrote:
> The get_size() function now uses vfs_getattr_nosec() with AT_STATX_SYNC_AS_STAT
> to ensure filesystem cache is synchronized before retrieving file size. This
> provides more accurate size information, especially when:
> 
> - The backing file size has been changed by another process
> - The file is on a network filesystem (NFS, CIFS, etc.)
> - The file is being modified concurrently
> - The most accurate size is needed for loop device setup
> 

Please wrap your commit messages at 73 (or apparently 75) lines.

Also 'syncing the cache' (what cache?) is at best an implementation
detail.  The VFS semantics simply are that you need a getattr to
retrieve the inode size, and the loop code fails to do this correctly.

> The implementation gracefully falls back to i_size_read() if vfs_getattr_nosec()
> fails, maintaining backward compatibility.

No need to fall back.  If vfs_getattr faills the file systems is
completely toast.

>  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
> + */

We don't really need a verbose kerneldoc for a static helper with 2
callers.

>  static loff_t get_size(loff_t offset, loff_t sizelimit, struct file *file)
>  {
> +	struct kstat stat;
>  	loff_t loopsize;
> +	int ret;
> +
> +	/*
> +	 * Get file attributes for validation. We use vfs_getattr() to ensure
> +	 * we have up-to-date file size information.
> +	 */

The comment seems a bit misleading or at least not to the point.

I'd say:

	/*
	 * File systems don't have to keep i_size in sync.  While local file
	 * systems typically keep it in sync, remote file system often do not.
	 * Go through ->getattr to retrieve the current value.
	 */

> +	ret = vfs_getattr_nosec(&file->f_path, &stat, STATX_SIZE, 
> +			        AT_STATX_SYNC_AS_STAT);

Most kernel callers just pass 0 instead of AT_STATX_SYNC_AS_STAT here,
which honestly is less confusing as I had to look up
AT_STATX_SYNC_AS_STAT first.

> +	if (ret) {
> +		/*
> +		 * If we can't get attributes, fall back to i_size_read()
> +		 * which should work for most cases.
> +		 */

As said above, there is no need to do this.  Adding a proper error
return chain here is better.


  parent reply	other threads:[~2025-08-10 15:15 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-07 23:25 [PATCH 1/1] loop: sync filesystem cache before getting file size in get_size() Rajeev Mishra
2025-08-08  2:47 ` Yu Kuai
     [not found]   ` <PH7PR84MB2079A6A4EFE799BCA7E5738CAA2FA@PH7PR84MB2079.NAMPRD84.PROD.OUTLOOK.COM>
2025-08-11  1:20     ` Yu Kuai
2025-08-10 15:15 ` Christoph Hellwig [this message]
2025-08-15  3:25 ` kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2025-08-08 18:59 Cloud User
2025-08-08 19:03 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=aJi3fFwlqb-SfHGg@infradead.org \
    --to=hch@infradead.org \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rajeevm@hpe.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.