[PATCH] fs: send fsnotify_xattr()/IN_ATTRIB from vfs_fileattr_set()/chattr(1)

From: Ahelenia Ziemiańska
Date: Mon Dec 08 2025 - 17:21:24 EST


Currently it seems impossible to observe these changes to the file's
attributes. It's useful to be able to do this to see when the file
becomes immutable, for example, so emit IN_ATTRIB via fsnotify_xattr(),
like when changing other inode attributes.

Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@xxxxxxxxxxxxxxxxxx>
---
Given:
#include <sys/inotify.h>
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
int main() {
int fd = inotify_init();
inotify_add_watch(fd, ".", IN_ATTRIB);
char buf[sizeof(struct inotify_event) + NAME_MAX + 1];
for (;;) {
ssize_t rd = read(fd, buf, sizeof(buf));
struct inotify_event *ev = buf, *end = buf + rd;
while (ev < end) {
printf("%x\t%s\n", ev->mask, ev->name);
ev = (char *)(ev + 1) + ev->len;
}
}
}

Before:
sh-5.2# ./test &
[1] 255
sh-5.2# chmod -x test
4 test
sh-5.2# setfattr -n user.name -v value test
4 test
sh-5.2# chattr -i test
sh-5.2#

After:
sh-5.2# ./test &
[1] 280
sh-5.2# chmod -x test
4 test
sh-5.2# setfattr -n user.name -v value test
4 test
sh-5.2# chattr -i test
4 test
sh-5.2#

fs/file_attr.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/fs/file_attr.c b/fs/file_attr.c
index 4c4916632f11..13cdb31a3e94 100644
--- a/fs/file_attr.c
+++ b/fs/file_attr.c
@@ -2,6 +2,7 @@
#include <linux/fs.h>
#include <linux/security.h>
#include <linux/fscrypt.h>
+#include <linux/fsnotify.h>
#include <linux/fileattr.h>
#include <linux/export.h>
#include <linux/syscalls.h>
@@ -298,6 +299,7 @@ int vfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry,
err = inode->i_op->fileattr_set(idmap, dentry, fa);
if (err)
goto out;
+ fsnotify_xattr(dentry);
}

out:
--
2.39.5

Attachment: signature.asc
Description: PGP signature