Linux userland API discussions
 help / color / mirror / Atom feed
* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
       [not found] ` <20261106122355.16885-1-lauri.gates@gmail.com>
@ 2026-11-06 15:20   ` Christoph Hellwig
  2026-11-06 20:40     ` Lauri Gates
  0 siblings, 1 reply; 8+ messages in thread
From: Christoph Hellwig @ 2026-11-06 15:20 UTC (permalink / raw)
  To: Lauri Gates
  Cc: Dave Chinner, linux-xfs, Carlos Maiolino, Christian Brauner,
	linux-api, linux-fsdevel, linux-btrfs, ocfs2-devel, linux-nfs,
	linux-cifs

On Fri, Nov 06, 2026 at 02:23:55PM +0200, Lauri Gates wrote:
> Mirroring XFS_DIFLAG2_REFLINK, the flag would mean "may have shared
> extents", which is all a du tool needs to decide whether to look
> closer. If it helps, I can write the patch.

Please do.  The main question are:

 - do we want it in fileattr_get or as a STATX_ATTR_ in statx.
 - how do we make the query reliable, and do we even need to?

Besides xfs, btrfs and ocfs2 also support reflinks, and nfs (and maybe
cifs?) can export the semanics remotely.  So we really need a way
to indicate if the file system could support reflinks to start with,
and then one that the file could, so we probably need two bits


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-06 15:20   ` Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock Christoph Hellwig
@ 2026-11-06 20:40     ` Lauri Gates
  2026-11-12 20:05       ` Christoph Hellwig
  0 siblings, 1 reply; 8+ messages in thread
From: Lauri Gates @ 2026-11-06 20:40 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Dave Chinner, linux-xfs, Carlos Maiolino, Christian Brauner,
	Darrick J. Wong, linux-api, linux-fsdevel, linux-btrfs,
	ocfs2-devel, linux-nfs, linux-cifs

On Fri, Nov 06, 2026 at 07:20:04AM -0800, Christoph Hellwig wrote:
> Please do.  The main question are:
>
>  - do we want it in fileattr_get or as a STATX_ATTR_ in statx.
>  - how do we make the query reliable, and do we even need to?

Thanks. For the newly Cc'd lists: the thread [1] started from du-like
tools that call FIEMAP on every file to count shared extents, which on
XFS serialises on the AGF buffer lock. Dave suggested a per-file "may
have shared extents" flag so such tools can skip FIEMAP on files that
cannot share, and Christoph asked for a patch. Before writing one I'd
like to settle the two questions above and the "two bits" question
below.

To see what the flag would buy, I emulated it: a walker that calls
statx() on every file and runs open + FIEMAP + close only on files
whose XFS_DIFLAG2_REFLINK was set, read beforehand with xfs_db -r. In
all 80 configurations (trees 0-100% reflinked, 1-24 threads, two
machines) it found the same shared files and extents as FIEMAP on
every file.

On placement, a STATX_ATTR_ bit (say STATX_ATTR_MAY_SHARE) looks like
the better fit to me, for two reasons, though I may well be missing
history here.

A du walk needs statx() for stx_blocks anyway, so a bit in
stx_attributes would cost it no extra syscall, while the path-based
file_getattr(2) adds a second call and lookup per file. Median of 5
runs, 100,000 files, warm cache, Ryzen 9 3900X on 6.17:

                               1 thread   24 threads
    statx()                      195 ms      11.4 ms
    statx() + file_getattr()     352 ms      19.9 ms

(An arm64 VM on 7.2 shows a smaller gap: statx() alone vs with
file_getattr() is 79 vs 97 ms on 1 thread, 8.9 vs 11.0 ms on 13.)

The other is that for a "may share" bit, clear is the unsafe answer,
so a filesystem that cannot answer should not look like one that
answers "not shared". As I understand it, stx_attributes_mask makes
that distinction, while struct file_attr has no validity mask today
(it is size-versioned, so one could be added), so through
file_getattr() btrfs would currently read as never shared. A spare
bit in stx_attributes adds no field to struct statx, which I hope
meets the concern Christian raised about spending statx space on the
case-folding RFC [2]. If fileattr is preferred, the bit could instead
mean "no shared extents", so that clear is the safe default, as it is
for FS_XFLAG_CASEFOLD.

> Besides xfs, btrfs and ocfs2 also support reflinks, and nfs (and maybe
> cifs?) can export the semanics remotely.  So we really need a way
> to indicate if the file system could support reflinks to start with,
> and then one that the file could, so we probably need two bits

For du, I think the mask could stand in for the "could support
reflinks" bit if it means "this filesystem can answer for this file":

  mask clear            not reported, fall back to FIEMAP
  mask set, bit clear   no shared extents
  mask set, bit set     may have shared extents

A filesystem that cannot share extents at all would set the mask and
leave the bit clear. If I read v7.4-rc1 right, only xfs, btrfs and
ocfs2 report shared extents, and all three implement
->remap_file_range, so the VFS might set the mask itself for files
whose filesystem lacks ->remap_file_range. What I could find per
filesystem:

 - xfs: XFS_DIFLAG2_REFLINK is set on both inodes in
   xfs_reflink_remap_prep() before any extent is remapped, so clear
   means no shared extents.
 - ocfs2: OCFS2_HAS_REFCOUNT_FL is set on both inodes before the
   remap and cleared only at zero clusters, which looks like the same
   meaning.
 - btrfs: I could not find a persisted per-inode reflink record
   (last_reflink_trans is in memory only and reset to last_trans when
   the inode is loaded), so btrfs would leave the bit out of the mask
   for now. btrfs folks, please correct me if I missed something.
 - nfs, cifs: I could not find a per-file attribute in NFSv4.2 or
   MS-FSCC that could carry it (space_freed comes closest, and nfsd
   does not implement it), so no mask bit there.

A filesystem-level "could reflink" bit might be a separate need:
nfsd encodes clone_blksize for every export and ksmbd advertises
FILE_SUPPORTS_BLOCK_REFCOUNTING unconditionally, I assume because
neither can ask the exported filesystem. Did you have nfsd and ksmbd
in mind for that bit?

On reliability, my tentative answer is that the bit does not need to
be exact for this use. A clear bit must never be wrong, and it never
was in my tests, including a survey of my desktop's XFS root (2.0M
files, flags read with xfs_db -r and matched by inode number). A
stale set bit costs one FIEMAP, so I would document it as "may have
shared extents"; since the contract is only "may", clearing it more
eagerly later would not change the uapi.

Stale bits do limit what it saves, though. The flag stays set when
the other copy is deleted or the file is overwritten; only truncation
to zero and FALLOC_FL_UNSHARE_RANGE (see the caveat below) clear it.
Ordinary copies set it: coreutils 9.4 cp and install, cat (which
copies with copy_file_range), Rust's std::fs::copy, and overlayfs
copy-up all reflink on XFS. On my desktop, leaving out benchmark
trees, 18% of inodes have the flag and 41% of those share nothing.
That leaves out ~/.rustup too: an earlier test reflink-copied it, so
all of its 132k inodes are flagged and none share; counting it, the
figures are 25% and 62%.

On real trees (24 threads, median of 5), statx() on every file, then
open + FIEMAP + close on every file, on flagged files, or on files
with shared extents only, the lower bound for a flag cleared when
sharing ends (/usr has no shared files):

                       statx only  every file  flagged  shared only
  /usr      224k files      28 ms      264 ms    29 ms       -
  ~/.local  372k files      63 ms      506 ms   487 ms    322 ms
  ~/.rustup 132k files      20 ms      319 ms   318 ms     21 ms

Would there be a reasonable point for XFS to clear the flag once
sharing ends? I don't know the code well enough to tell what that
would cost.

The caveat, which may be unintended or may be my misreading:
FALLOC_FL_UNSHARE_RANGE over a whole shared file does not clear the
flag on the first call. bpftrace on 7.2 shows
xfs_reflink_clear_inode_flag() entered and
xfs_reflink_inode_has_shared_extents() not, so it looks like
xfs_can_free_cowblocks() returned false, perhaps on I_DIRTY_PAGES
left by the unshare. A second call after sync clears the flag.

On Darrick's XFS_IOC_GETFSREFCOUNTS patch from your other mail:
thanks for the pointer. It gives a device-wide total without
privileges, which a du tool could show for the whole filesystem,
though not per directory.

If the statx direction sounds reasonable, I would be glad to put
together an RFC with the uapi bit, xfs and ocfs2 support, a man-pages
patch and an xfstest. This would be my first kernel patch, so
corrections on any of the above are very welcome.

[1] https://lore.kernel.org/linux-xfs/20261102193533.93690-1-lauri.gates@gmail.com/
[2] https://lore.kernel.org/all/20251006-zypressen-paarmal-4167375db973@brauner/

Thanks,
Lauri

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-06 20:40     ` Lauri Gates
@ 2026-11-12 20:05       ` Christoph Hellwig
  2026-11-17 16:22         ` Christian Brauner
  0 siblings, 1 reply; 8+ messages in thread
From: Christoph Hellwig @ 2026-11-12 20:05 UTC (permalink / raw)
  To: Lauri Gates
  Cc: Christoph Hellwig, Dave Chinner, linux-xfs, Carlos Maiolino,
	Christian Brauner, Darrick J. Wong, linux-api, linux-fsdevel,
	linux-btrfs, ocfs2-devel, linux-nfs, linux-cifs

On Fri, Nov 06, 2026 at 10:40:26PM +0200, Lauri Gates wrote:
> The other is that for a "may share" bit, clear is the unsafe answer,
> so a filesystem that cannot answer should not look like one that
> answers "not shared". As I understand it, stx_attributes_mask makes
> that distinction, while struct file_attr has no validity mask today
> (it is size-versioned, so one could be added), so through
> file_getattr() btrfs would currently read as never shared. A spare
> bit in stx_attributes adds no field to struct statx, which I hope
> meets the concern Christian raised about spending statx space on the
> case-folding RFC [2]. If fileattr is preferred, the bit could instead
> mean "no shared extents", so that clear is the safe default, as it is
> for FS_XFLAG_CASEFOLD.

statx sounds good to me, but I'd really like to hear from Christian.

> 
> > Besides xfs, btrfs and ocfs2 also support reflinks, and nfs (and maybe
> > cifs?) can export the semanics remotely.  So we really need a way
> > to indicate if the file system could support reflinks to start with,
> > and then one that the file could, so we probably need two bits
> 
> For du, I think the mask could stand in for the "could support
> reflinks" bit if it means "this filesystem can answer for this file":
> 
>   mask clear            not reported, fall back to FIEMAP
>   mask set, bit clear   no shared extents
>   mask set, bit set     may have shared extents
> 
> A filesystem that cannot share extents at all would set the mask and
> leave the bit clear.

Sounds good.

>  - nfs, cifs: I could not find a per-file attribute in NFSv4.2 or
>    MS-FSCC that could carry it (space_freed comes closest, and nfsd
>    does not implement it), so no mask bit there.

There's definitively nothing in NFS, speaking as the person who added
the clone support.  If your new Linux syscall API becomes widely useful,
we could add the equivalent to NFS in a new draft.

> A filesystem-level "could reflink" bit might be a separate need:
> nfsd encodes clone_blksize for every export and ksmbd advertises
> FILE_SUPPORTS_BLOCK_REFCOUNTING unconditionally, I assume because
> neither can ask the exported filesystem. Did you have nfsd and ksmbd
> in mind for that bit?

I just mean we need some way to discover it.  I think the three states
in statx should cover everything.

> On reliability, my tentative answer is that the bit does not need to
> be exact for this use. A clear bit must never be wrong,

Yes.

> Stale bits do limit what it saves, though. The flag stays set when
> the other copy is deleted or the file is overwritten; only truncation
> to zero and FALLOC_FL_UNSHARE_RANGE (see the caveat below) clear it.

Yes.

> Would there be a reasonable point for XFS to clear the flag once
> sharing ends? I don't know the code well enough to tell what that
> would cost.

It is hard to know that nothing is shared, because nothing in the
inode tracks the shared space, except for XFS_DIFLAG2_REFLINK only
the reflink btree shared if anyting is reflinked.   To efficiently
clear the bit in an exact way we'd need to track the number of
blocks or extents that are shared in the inode, which would be
an on-disk format change.  But if you run xfs_scrub with online
repair enable, the online repair can fix the value up as background
operation, which should be useful once that feature has matured a
bit and users enable it by default.

> 
> The caveat, which may be unintended or may be my misreading:
> FALLOC_FL_UNSHARE_RANGE over a whole shared file does not clear the
> flag on the first call. bpftrace on 7.2 shows
> xfs_reflink_clear_inode_flag() entered and
> xfs_reflink_inode_has_shared_extents() not, so it looks like
> xfs_can_free_cowblocks() returned false, perhaps on I_DIRTY_PAGES
> left by the unshare. A second call after sync clears the flag.

Interesting.  Maybe we need a sync before calling
xfs_reflink_inode_has_shared_extents?

> If the statx direction sounds reasonable, I would be glad to put
> together an RFC with the uapi bit, xfs and ocfs2 support, a man-pages
> patch and an xfstest. This would be my first kernel patch, so
> corrections on any of the above are very welcome.

Happy to help reviewing this if you take on a word.  Just as a warning
I'm a bit overloaded currently, so some replies might take longer than
I'd want.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-12 20:05       ` Christoph Hellwig
@ 2026-11-17 16:22         ` Christian Brauner
  2026-11-18 20:16           ` Dave Chinner
  0 siblings, 1 reply; 8+ messages in thread
From: Christian Brauner @ 2026-11-17 16:22 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Lauri Gates, Dave Chinner, linux-xfs, Carlos Maiolino,
	Christian Brauner, Darrick J. Wong, linux-api, linux-fsdevel,
	linux-btrfs, ocfs2-devel, linux-nfs, linux-cifs

On Thu, Nov 12, 2026 at 12:05:00PM -0800, Christoph Hellwig wrote:
> On Fri, Nov 06, 2026 at 10:40:26PM +0200, Lauri Gates wrote:
> > The other is that for a "may share" bit, clear is the unsafe answer,
> > so a filesystem that cannot answer should not look like one that
> > answers "not shared". As I understand it, stx_attributes_mask makes
> > that distinction, while struct file_attr has no validity mask today
> > (it is size-versioned, so one could be added), so through
> > file_getattr() btrfs would currently read as never shared. A spare
> > bit in stx_attributes adds no field to struct statx, which I hope
> > meets the concern Christian raised about spending statx space on the
> > case-folding RFC [2]. If fileattr is preferred, the bit could instead
> > mean "no shared extents", so that clear is the safe default, as it is
> > for FS_XFLAG_CASEFOLD.
> 
> statx sounds good to me, but I'd really like to hear from Christian.

I'm not fond of it mostly because it seems FIEMAP is a debug interface
and adding bits into statx() to make said debug interface quicker or
easier to use feels very wrong to me. So I'd like to hear a better
argument why we should do this. Otherwise, I think using file_attr as
suggested earlier is good enough.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-17 16:22         ` Christian Brauner
@ 2026-11-18 20:16           ` Dave Chinner
  2026-11-19  9:59             ` Lauri Gates
  0 siblings, 1 reply; 8+ messages in thread
From: Dave Chinner @ 2026-11-18 20:16 UTC (permalink / raw)
  To: Christian Brauner
  Cc: Christoph Hellwig, Lauri Gates, linux-xfs, Carlos Maiolino,
	Christian Brauner, Darrick J. Wong, linux-api, linux-fsdevel,
	linux-btrfs, ocfs2-devel, linux-nfs, linux-cifs

On Tue, Nov 17, 2026 at 05:22:25PM +0100, Christian Brauner wrote:
> On Thu, Nov 12, 2026 at 12:05:00PM -0800, Christoph Hellwig wrote:
> > On Fri, Nov 06, 2026 at 10:40:26PM +0200, Lauri Gates wrote:
> > > The other is that for a "may share" bit, clear is the unsafe answer,
> > > so a filesystem that cannot answer should not look like one that
> > > answers "not shared". As I understand it, stx_attributes_mask makes
> > > that distinction, while struct file_attr has no validity mask today
> > > (it is size-versioned, so one could be added), so through
> > > file_getattr() btrfs would currently read as never shared. A spare
> > > bit in stx_attributes adds no field to struct statx, which I hope
> > > meets the concern Christian raised about spending statx space on the
> > > case-folding RFC [2]. If fileattr is preferred, the bit could instead
> > > mean "no shared extents", so that clear is the safe default, as it is
> > > for FS_XFLAG_CASEFOLD.
> > 
> > statx sounds good to me, but I'd really like to hear from Christian.
> 
> I'm not fond of it mostly because it seems FIEMAP is a debug interface
> and adding bits into statx() to make said debug interface quicker or
> easier to use feels very wrong to me. So I'd like to hear a better
> argument why we should do this. Otherwise, I think using file_attr as
> suggested earlier is good enough.

I really don't care if it is exposed by statx or file_attr; checking
the flag through either interface will be orders of magnitude faster
than using FIEMAP to iterate extents on every file to find shared
extents. My concern with statx is coherency - it runs completely
unlocked and so can race with operations that add/remove the reflink
flag...

Cheers,

Dave.
-- 
Dave Chinner
dgc@kernel.org

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-18 20:16           ` Dave Chinner
@ 2026-11-19  9:59             ` Lauri Gates
  2026-11-19 11:00               ` koraynilay
  0 siblings, 1 reply; 8+ messages in thread
From: Lauri Gates @ 2026-11-19  9:59 UTC (permalink / raw)
  To: Dave Chinner
  Cc: Christian Brauner, Christoph Hellwig, linux-xfs, Carlos Maiolino,
	Christian Brauner, Darrick J. Wong, linux-api, linux-fsdevel,
	linux-btrfs, ocfs2-devel, linux-nfs, linux-cifs

On Wed, Nov 18, 2026 at 10:16PM +0200, Dave Chinner wrote:
> I really don't care if it is exposed by statx or file_attr; checking
> the flag through either interface will be orders of magnitude faster
> than using FIEMAP to iterate extents on every file to find shared
> extents. My concern with statx is coherency - it runs completely
> unlocked and so can race with operations that add/remove the reflink
> flag...

Thanks, Dave and Christian. Given Christian's preference and the
coherency point, I'll write the RFC against file_attr, once the
question at the end is settled. xfs_fileattr_get() already reads the
flags under XFS_ILOCK_SHARED, so the new flag gets that without extra
work. The answer can still go stale right after the call, as with any
stat result: a clone that lands after the read is missed, just as one
that lands after FIEMAP returns is missed today.

As I suggested on the 6th, the flag would take the inverted meaning,
so that an unset flag stays the safe answer: say FS_XFLAG_NOSHARED
("no extents shared with another file"), read-only, in
FS_XFLAG_RDONLY_MASK next to the case-folding flags. XFS would set it
when XFS_DIFLAG2_REFLINK is clear, and ocfs2 when
OCFS2_HAS_REFCOUNT_FL is clear. btrfs keeps no per-inode record and
would leave it unset, as would nfs and cifs, so their files read as
"may share" and a du tool falls back to FIEMAP there, as it does
today.

overlayfs needs care: ovl_fileattr_get() reads the upper inode, which
for a metacopy file has no data extents. The overlayfs patch would
take the flag from the lower data inode, and leave it unset ("may
share") while that inode is not yet known (the lazy lookup on
data-only lower layers).

Christian, one question on the shape within file_attr: would you take
the inverted flag, or would you rather keep a plain "may share" flag
and add a mask to struct file_attr that says which xflags the
filesystem reports?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-19  9:59             ` Lauri Gates
@ 2026-11-19 11:00               ` koraynilay
  2026-11-19 15:32                 ` Lauri Gates
  0 siblings, 1 reply; 8+ messages in thread
From: koraynilay @ 2026-11-19 11:00 UTC (permalink / raw)
  To: Lauri Gates, Dave Chinner
  Cc: Christian Brauner, Christoph Hellwig, linux-xfs, Carlos Maiolino,
	Christian Brauner, Darrick J. Wong, linux-api, linux-fsdevel,
	linux-btrfs, ocfs2-devel, linux-nfs, linux-cifs, Amir Goldstein

[-- Attachment #1: Type: text/plain, Size: 9733 bytes --]

On Thu Nov 19, 2026 at 10:59 AM CET, Lauri Gates wrote:
> On Wed, Nov 18, 2026 at 10:16PM +0200, Dave Chinner wrote:
>> I really don't care if it is exposed by statx or file_attr; checking
>> the flag through either interface will be orders of magnitude faster
>> than using FIEMAP to iterate extents on every file to find shared
>> extents. My concern with statx is coherency - it runs completely
>> unlocked and so can race with operations that add/remove the reflink
>> flag...
>
> Thanks, Dave and Christian. Given Christian's preference and the
> coherency point, I'll write the RFC against file_attr, once the
> question at the end is settled. xfs_fileattr_get() already reads the
> flags under XFS_ILOCK_SHARED, so the new flag gets that without extra
> work. The answer can still go stale right after the call, as with any
> stat result: a clone that lands after the read is missed, just as one
> that lands after FIEMAP returns is missed today.
>
> As I suggested on the 6th, the flag would take the inverted meaning,
> so that an unset flag stays the safe answer: say FS_XFLAG_NOSHARED
> ("no extents shared with another file"), read-only, in
> FS_XFLAG_RDONLY_MASK next to the case-folding flags.

Hello, I don't have any say in the FIEMAP issue, but I wanted to let you
know that, since also a mask was mentioned and your patch would be
touching the fileattr API, there is work going[1] on for adding a
fsx_xflags_mask that filesystem use[2][3] to advertise supported flags (on
get, like stx_attributes_mask) and users use to say what they want to
change (on set).
It doesn't seem like an FS_XFLAG_NOSHARED flag would touch this new
change, but I thought it was worth pointing out, especially because this
patch would also add FS_XFLAG_PROJID and a new FS_FILE_ATTR_XFLAGS_MASK
to have a way to include both legacy and newly added xflags, so
FS_XFLAG_NOSHARED would maybe also go there (correct me if I'm wrong).

> Christian, one question on the shape within file_attr: would you take
> the inverted flag, or would you rather keep a plain "may share" flag
> and add a mask to struct file_attr that says which xflags the
> filesystem reports?

And if Christian chooses the mask approach, then my patch[4] becomes
relevant.
(in [4] I put the patch that will be sent in v5 of [1], since in [1]
it's merged with the ptime change)

(For Amir who I just CC'd, [5] is the main link for this thread)

Thanks,
koraynilay

[1]: https://lore.kernel.org/all/20261115180343.576773-1-koray.fra@gmail.com/T/#u
[2]: https://lore.kernel.org/all/CAOQ4uxj82K4z+2OiY5W1g7NyWAFOFjqqWnZSS3prytW36j1LgA@mail.gmail.com/
[3]: https://lore.kernel.org/all/CAOQ4uxiuJ=eZu28FZAUz3rpqPKqCimPxJucOnYYuBowvD09_Cw@mail.gmail.com/
[5]: https://lore.kernel.org/linux-xfs/20261102193533.93690-1-lauri.gates@gmail.com/T/#u

[4]:
commit 0efe0de3b5cc2b767aa6e2bb9174eb05b0d818f1
Author: koraynilay <koray.fra@gmail.com>
Date:   Tue Nov 17 14:57:47 2026 +0100

    vfs: add xflags mask to file_attr API
    
    Add fa_xflags_mask to struct file_attr and equivalent fsx_xflags_mask
    to struct file_kattr.
    
    This lets filesystems advertise their support for specific xflags and
    users specify only what they want to change, behaving like
    statx.stx_attributes_mask:
    - on GET (file_getattr): each filesystem fills the mask according to
      its supported xflags, e.g. FS_XFLAG_COWEXTSIZE or FS_XFLAG_EXTSIZE,
      and provides the values in the corresponding struct fields;
    - on SET (file_setattr): userspace fills the mask according to which
      xflags it wants to change, VFS validates this and preserves xflags
      that were not specified in the mask;
    - if xflags_mask == 0, use legacy full-struct replacement to keep
      compatibility with older programs.
    
    Furthermore, add the missing FS_XFLAG_PROJID flag, to allow userspace to
    set the projid using file_[sg]etattr().
    
    Finally, in order to prevent the legacy FS_IOC_FSSETXATTR from accepting
    FS_XFLAG_PROJID, add an FS_FILE_ATTR_XFLAGS_MASK to support new xflags
    and use it in fileattr_to_file_attr()/file_attr_to_fileattr() to
    validate that the xflags passed in fa_xflags and fa_xflags_mask fields
    are valid.
    
    Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.7
    Signed-off-by: koraynilay <koray.fra@gmail.com>
---
 fs/file_attr.c           | 46 ++++++++++++++++++++++++++++++++++++++++++----
 include/linux/fileattr.h |  5 +++++
 include/uapi/linux/fs.h  |  3 +++
 3 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/fs/file_attr.c b/fs/file_attr.c
index 833e03810b24..56ea65f790f1 100644
--- a/fs/file_attr.c
+++ b/fs/file_attr.c
@@ -102,10 +102,18 @@ EXPORT_SYMBOL(vfs_fileattr_get);
 static void fileattr_to_file_attr(const struct file_kattr *fa,
 				  struct file_attr *fattr)
 {
-	__u32 mask = FS_XFLAGS_MASK;
+	/*
+	 * fsx_xflags_mask contains only flags added after the
+	 * file_[gs]etattr() API, so we need to tell userspace the filesystem
+	 * actually supports all legacy (FS_XFLAGS_MASK and FS_XFLAG_PROJID,
+	 * which lacked its own bit) and new advertised flags.
+	 */
+	__u32 mask = (FS_XFLAGS_MASK | FS_XFLAG_PROJID | fa->fsx_xflags_mask) &
+		     FS_FILE_ATTR_XFLAGS_MASK;
 
 	memset(fattr, 0, sizeof(struct file_attr));
 	fattr->fa_xflags = fa->fsx_xflags & mask;
+	fattr->fa_xflags_mask = mask;
 	fattr->fa_extsize = fa->fsx_extsize;
 	fattr->fa_nextents = fa->fsx_nextents;
 	fattr->fa_projid = fa->fsx_projid;
@@ -141,12 +149,17 @@ EXPORT_SYMBOL(copy_fsxattr_to_user);
 static int file_attr_to_fileattr(const struct file_attr *fattr,
 				 struct file_kattr *fa)
 {
-	__u64 mask = FS_XFLAGS_MASK;
+	__u64 mask = fattr->fa_xflags_mask ?: FS_XFLAGS_MASK;
+
+	if (fattr->fa_xflags_mask & ~FS_FILE_ATTR_XFLAGS_MASK)
+		return -EINVAL;
 
 	if (fattr->fa_xflags & ~mask)
 		return -EINVAL;
 
-	fileattr_fill_xflags(fa, fattr->fa_xflags & ~FS_XFLAG_RDONLY_MASK);
+	fileattr_fill_xflags(fa, fattr->fa_xflags & FS_XFLAGS_MASK &
+				 ~FS_XFLAG_RDONLY_MASK);
+	fa->fsx_xflags_mask = fattr->fa_xflags_mask;
 	fa->fsx_extsize = fattr->fa_extsize;
 	fa->fsx_projid = fattr->fa_projid;
 	fa->fsx_cowextsize = fattr->fa_cowextsize;
@@ -288,12 +301,37 @@ int vfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry,
 	if (!err) {
 		/* initialize missing bits from old_ma */
 		if (fa->flags_valid) {
-			fa->fsx_xflags |= old_ma.fsx_xflags & ~FS_XFLAG_COMMON;
+			/* FS_IOC_SETFLAGS */
+			fa->fsx_xflags_mask = 0;
+			fa->fsx_xflags |= (old_ma.fsx_xflags & FS_XFLAGS_MASK) &
+					  ~FS_XFLAG_COMMON;
 			fa->fsx_extsize = old_ma.fsx_extsize;
 			fa->fsx_nextents = old_ma.fsx_nextents;
 			fa->fsx_projid = old_ma.fsx_projid;
 			fa->fsx_cowextsize = old_ma.fsx_cowextsize;
 		} else {
+			/*
+			 * Preserve unmasked xflags when running file_setattr()
+			 * with a mask, which doesn't need a read-modify-write
+			 * so these attributes would be set to 0.
+			 *
+			 * With FS_IOC_FSSETXATTR or mask == 0 (legacy mode)
+			 * this is not needed as userspace does
+			 * read-modify-write already.
+			 */
+			if (fa->fsx_xflags_mask) {
+				fa->fsx_xflags |= (old_ma.fsx_xflags & FS_XFLAGS_MASK) &
+						  ~fa->fsx_xflags_mask;
+				fileattr_fill_xflags(fa, fa->fsx_xflags &
+						     ~FS_XFLAG_RDONLY_MASK);
+				if (!(fa->fsx_xflags_mask &
+				      (FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT)))
+					fa->fsx_extsize = old_ma.fsx_extsize;
+				if (!(fa->fsx_xflags_mask & FS_XFLAG_PROJID))
+					fa->fsx_projid = old_ma.fsx_projid;
+				if (!(fa->fsx_xflags_mask & FS_XFLAG_COWEXTSIZE))
+					fa->fsx_cowextsize = old_ma.fsx_cowextsize;
+			}
 			fa->flags |= old_ma.flags & ~FS_COMMON_FL;
 		}
 
diff --git a/include/linux/fileattr.h b/include/linux/fileattr.h
index 09e32b84e02a..40ecb29f6235 100644
--- a/include/linux/fileattr.h
+++ b/include/linux/fileattr.h
@@ -35,6 +35,10 @@
 	(FS_XFLAG_COMMON | FS_XFLAG_RDONLY_MASK | FS_XFLAG_VALUES_MASK | \
 	 FS_XFLAG_DIRONLY_MASK | FS_XFLAG_MISC_MASK)
 
+/* Flags only for fa_ fields */
+#define FS_FILE_ATTR_XFLAGS_MASK \
+	(FS_XFLAGS_MASK | FS_XFLAG_PROJID)
+
 /*
  * Merged interface for miscellaneous file attributes.  'flags' originates from
  * ext* and 'fsx_flags' from xfs.  There's some overlap between the two, which
@@ -45,6 +49,7 @@ struct file_kattr {
 	u32	flags;		/* flags (FS_IOC_GETFLAGS/FS_IOC_SETFLAGS) */
 	/* struct fsxattr: */
 	u32	fsx_xflags;	/* xflags field value (get/set) */
+	u32	fsx_xflags_mask; /* supported xflags (get) / flags to set (set) */
 	u32	fsx_extsize;	/* extsize field value (get/set)*/
 	u32	fsx_nextents;	/* nextents field value (get)	*/
 	u32	fsx_projid;	/* project identifier (get/set) */
diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index a46c33692aa2..b168f1b75126 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -229,6 +229,7 @@ struct file_attr {
 	__u32 fa_nextents;	/* nextents field value (get)   */
 	__u32 fa_projid;	/* project identifier (get/set) */
 	__u32 fa_cowextsize;	/* CoW extsize field value (get/set) */
+	__u32 fa_xflags_mask;   /* supported xflags (get) / flags to set (set) */
 };
 
 #define FILE_ATTR_SIZE_VER0 24
@@ -261,6 +262,8 @@ struct file_attr {
  */
 #define FS_XFLAG_CASEFOLD	0x00040000	/* case-insensitive lookups */
 #define FS_XFLAG_CASENONPRESERVING 0x00080000	/* case not preserved */
+
+#define FS_XFLAG_PROJID		0x00200000	/* project ID */
 #define FS_XFLAG_HASATTR	0x80000000	/* no DIFLAG for this	*/
 
 /* the read-only stuff doesn't really belong here, but any other place is

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock
  2026-11-19 11:00               ` koraynilay
@ 2026-11-19 15:32                 ` Lauri Gates
  0 siblings, 0 replies; 8+ messages in thread
From: Lauri Gates @ 2026-11-19 15:32 UTC (permalink / raw)
  To: koraynilay
  Cc: Dave Chinner, Christian Brauner, Christoph Hellwig, linux-xfs,
	Carlos Maiolino, Christian Brauner, Darrick J. Wong, linux-api,
	linux-fsdevel, linux-btrfs, ocfs2-devel, linux-nfs, linux-cifs,
	Amir Goldstein

On Thu, Nov 19, 2026 at 01:00PM +0200, koraynilay wrote:
> It doesn't seem like an FS_XFLAG_NOSHARED flag would touch this new
> change, but I thought it was worth pointing out, especially because this
> patch would also add FS_XFLAG_PROJID and a new FS_FILE_ATTR_XFLAGS_MASK
> to have a way to include both legacy and newly added xflags, so
> FS_XFLAG_NOSHARED would maybe also go there (correct me if I'm wrong).

Thanks, that is useful. I agree it would go there, whichever meaning
it ends up with: as a newly added flag its bit would be added to
FS_FILE_ATTR_XFLAGS_MASK next to FS_XFLAG_PROJID, since your
fileattr_to_file_attr() masks the value with it as well as the mask
bit.

Your mask also changes the question I asked Christian. With it, a
plain "may share" flag works the way stx_attributes_mask does: a
filesystem that sets the bit in fsx_xflags_mask answers, and one that
does not (btrfs today) reads as "not reported" rather than "not
shared". The inverted meaning was only there because file_attr had no
such mask.

One constraint for that case: the flag has to stay out of
FS_XFLAGS_MASK. As I read your fileattr_to_file_attr(), every bit in
FS_XFLAGS_MASK is reported as supported on every filesystem, so a
plain flag there would read as "supported, not shared" on btrfs.
Outside FS_XFLAGS_MASK the flag is also never seen through the legacy
FS_IOC_FSGETXATTR, which masks with FS_XFLAGS_MASK. That is fine,
since only file_getattr() needs to report it.

Christian, with this mask on the way, would you take a plain flag
(say FS_XFLAG_MAYSHARE, "may have extents shared with another file")
that filesystems advertise through fsx_xflags_mask? If so, I'll build
the RFC on top of koraynilay's series instead of using the inverted
FS_XFLAG_NOSHARED.

Thanks,
Lauri

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-11-19 15:32 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <auudz7Iwm2MrNA-b@dread>
     [not found] ` <20261106122355.16885-1-lauri.gates@gmail.com>
2026-11-06 15:20   ` Concurrent FIEMAP on reflinked files serialises on the AGF buffer lock Christoph Hellwig
2026-11-06 20:40     ` Lauri Gates
2026-11-12 20:05       ` Christoph Hellwig
2026-11-17 16:22         ` Christian Brauner
2026-11-18 20:16           ` Dave Chinner
2026-11-19  9:59             ` Lauri Gates
2026-11-19 11:00               ` koraynilay
2026-11-19 15:32                 ` Lauri Gates

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox