Re: [PATCH 2/3] vfs: add xflags mask to fileattr API

From: Amir Goldstein

Date: Wed Nov 25 2026 - 11:51:37 EST


On Wed, Nov 25, 2026 at 4:01 PM koraynilay <koray.fra@xxxxxxxxx> wrote:
>
> On Mon Nov 23, 2026 at 4:29 PM CET, Amir Goldstein wrote:
> > On Mon, Nov 23, 2026 at 2:58 PM koraynilay <koray.fra@xxxxxxxxx> wrote:
> >>
> >> On Mon Nov 23, 2026 at 2:24 PM CET, Amir Goldstein wrote:
> >> > On Sun, Nov 22, 2026 at 1:09 AM koraynilay <koray.fra@xxxxxxxxx> wrote:
> >> >>
> >> >> Add fa_xflags_rw_mask and fa_xflags_ro_mask to struct file_attr and
> >> >> equivalent masks to struct file_kattr.
> >> >>
> >> >> Also add has_masks selector to struct file_kattr so we can know when a
> >> >> user of this API (userspace or filesystem) supports them.
> >> >>
> >> >> This lets filesystems advertise their support for specific xflags, like
> >> >> statx.stx_attributes_mask does, and userspace can specify only which
> >> >> xflags it wants to change without needing a RMW cycle:
> >> >> - on GET: each filesystem that wants to support this new API fills the
> >> >> masks according to its supported and read-only xflags, then:
> >> >> * for boolean xflags: it fills fsx_xflags with the current on-disk value
> >> >> * for value xflags: it sets the xflag bit to 1 in fsx_xflags and the
> >> >> xflag value in the struct field;
> >> >> - on SET: userspace fills the rw_mask with the bits of the xflags it
> >> >> wants to change, then:
> >> >> * for boolean xflags: it provides the xflag value as a bit in fa_xflags
> >> >> * for value xflags: it sets the xflag bit to 1 in fa_xflags and then
> >> >> provides the value in the struct field; if the xflag bit is set to 0
> >> >> instead, this informs the kernel to unset/clear that value.
> >> >>
> >> >> Since fa_xflags_rw_mask means "I want to change the xflags specified in
> >> >> here", if userspace specifies fa_xflags_rw_mask = 0, only check for
> >> >> permissions and fileattr support without checking any xflags.
> >> >>
> >> >> Behaviour of older programs that already use file_[gs]etattr() is
> >> >> preserved by checking against FILE_ATTR_SIZE_VER*.
> >> >>
> >> >> Also add the missing FS_XFLAG_PROJID xflag, to allow userspace to set
> >> >> the projid using file_[gs]etattr() by leveraging the new masks.
> >> >>
> >> >> Finally, in order to prevent the legacy FS_IOC_FSSETXATTR from accepting
> >> >> FS_XFLAG_PROJID, add a FS_FILE_ATTR_XFLAGS_MASK to support new xflags
> >> >> and use it to validate/clamp both the _xflags fields and the masks.
> >> >>
> >> >> Assisted-by: Gemini:3.8-flash-high antigravity-cli-1.5.11
> >> >> Signed-off-by: koraynilay <koray.fra@xxxxxxxxx>
> >> >> ---
> > ...
> >> >> err = fileattr_set_prepare(inode, &old_ma, fa);
> >> >> @@ -424,6 +504,14 @@ SYSCALL_DEFINE5(file_getattr, int, dfd, const char __user *, filename,
> >> >> return error;
> >> >>
> >> >> fileattr_to_file_attr(&fa, &fattr);
> >> >> + /*
> >> >> + * Remove xflags added in or after VER1 so userspace programs compiled
> >> >> + * against VER0 don't try to write xflags they don't know about (since
> >> >> + * they do RMW).
> >> >> + */
> >> >> + if (usize < FILE_ATTR_SIZE_VER1)
> >> >> + fattr.fa_xflags &= FS_XFLAGS_MASK;
> >> >> +
> >> >
> >> > This looks odd.
> >> > old struct size with new flags should be -EINVAL not silently drop them
> >>
> >> Right, and the GET path should strip any new flags, so old programs
> >> doing RMW will never see them, correct?
> >>
> >
> > Yes, I think so.
> >
> >> >
> >> >> error = copy_struct_to_user(ufattr, usize, &fattr,
> >> >> sizeof(struct file_attr), NULL);
> >> >>
> >> >> @@ -460,10 +548,6 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
> >> >> if (error)
> >> >> return error;
> >> >>
> >> >> - error = file_attr_to_fileattr(&fattr, &fa);
> >> >> - if (error)
> >> >> - return error;
> >> >> -
> >> >> CLASS(filename_maybe_null, name)(filename, at_flags);
> >> >> if (!name && dfd >= 0) {
> >> >> CLASS(fd, f)(dfd);
> >> >> @@ -479,6 +563,30 @@ SYSCALL_DEFINE5(file_setattr, int, dfd, const char __user *, filename,
> >> >> return error;
> >> >> }
> >> >>
> >> >> + /*
> >> >> + * Userspace said "I don't want to change any flag", so be it I guess.
> >> >> + * Still check for fileattr support and permissions, also in case this
> >> >> + * is just a probe.
> >> >> + */
> >> >> + if (usize >= FILE_ATTR_SIZE_VER1 && fattr.fa_xflags_rw_mask == 0) {
> >> >> + struct inode *inode = d_inode(filepath.dentry);
> >> >
> >> >
> >> > No No this is not what this userspace said!
> >> >
> >> > This user space is the same old chattr programs recompiled with a new
> >> > fs.h header file
> >> > installed.
> >>
> >> Oh, right, I did not think about this case, I hope it doesn't break too
> >> much of the new code I changed already for v2 :(
> >>
> >> > When userspace does not set anything is masks it means "old API"
> >>
> >> And so a modern program using masks that passes rw_mask as 0 should be
> >> also interpreted as "old API", even if it kinda goes against the
> >> definition of rw_mask right?
> >
> > Yes, but it is not a problem to describe this backward compat exception:
> > When (rw_mask | ro_mask) == 0, it means "no mask" "set everything"
> > but then vfs will not accept new xflags that were not supported in v0
> > syscall.
>
> Uhm, since we have no way to know on the GET path if the caller is mask
> aware or not, on mask aware filesystems and legacy programs recompiled
> against VER1, file_getattr() will return rw_mask != 0, when the program
> does RMW, so calling file_setattr() with the old struct which has
> rw_mask != 0, vfs will think that the caller is mask aware.
>

True. This is a requirement from the design of v1
that RMW works correctly when the program does not change the masks.

> This causes problems when that legacy program changes the projid, since
> vfs will see rw_mask = PROJID but fsx_xflags will not have PROJID, vfs
> will clear it even tho the program wanted to set it to some value.
>

Yes, this is a specific issue with PROJID itself, not with the
entire masks API.


> Unless I'm missing something, there is no way for us to know if a caller
> of file_getattr() is a new mask aware program or just a recompiled legacy
> program.

Right.

>
> My robot proposed 2 fixes: either don't introduce FS_XFLAG_PROJID
> (which, eh, not the best imo)

Me neither. It's inconsistent IMO.

But it is inconsistent in that we want to have a way for chattr -p to say with
the new API "set only projid", so we must have the flag, but we
mostly need it for the mask, we don't really care about its value in
fsx_flags.

> or add a new flag to be used in at_flags,
> like AT_FILEATTR_MASKS, which would resolve all our legacy vs non-legacy
> problems, but it would be in *at*_flags, so doesn't really make sense
> semantically (but it would work well since file_getattr already rejects
> unknown at_flags, so it wouldn't cause problems for old programs).

I think this is an overkill as well.

We discussed this before.

Attributes that have X_value like fsx_projid have few types:
1. 0 means disabled (all existing values incl. projid)
2. 0 is a valid value but cannot be unset (btime)
3. 0 is a valid value and NULL/unset is also a valid value

Only for type 3 does can the interpretation of
(!XFLAG_X && !fsx_X) be considered as an "unset"
and anyway, this will require support in individual fs.

fileattr_set_prepare() already treats the existing values as:
fsx_X == 0 => clear XFLAG_X

And xfs returns EINVAL for !!(fsx_X) != !!(XFLAGX)

A simple solution is to fix the flag according to val in fileattr_set_prepare()
if rw_mask contains this flag:

if (fa->fsx_xflags_rw_mask & FS_XFLAG_PROJID) {
/* Possibly legacy chattr -p compiled with new headers unaware of PROJID */
if (fa->fsx_projid == 0)
fa->fsx_xflags &= ~FS_XFLAG_PROJID;
else
fa->fsx_xflags |= FS_XFLAG_PROJID;
}

Does that leave any problematic corner case?

Thanks,
Amir.