Support fanotify FAN_REPORT_FID on all filesystems
Subsystems (3)
Amir Goldstein
reviewerv2 cover Support fanotify FAN_REPORT_FID on all filesystems
Jan,
In the vfs fanotify update for v6.7-rc1 [1], we considerably increased the amount of filesystems that can setup inode marks with FAN_REPORT_FID:
- NFS export is no longer required for setting up inode marks
- All the simple fs gained a non-zero fsid
This leaves the following in-tree filesystems where inode marks with FAN_REPORT_FID cannot be set:
- nfs, fuse, afs, coda (zero fsid)
- btrfs non-root subvol (fsid not a unique identifier of sb)
This patch set takes care of these remaining cases, by allowing inode marks, as long as all inode marks in the group are contained to the same filesystem and same fsid (i.e. subvol).
I've written some basic sanity tests [2] and a man-page update draft [3]. The LTP tests excersize the new code by running tests that did not run before on ntfs-3g fuse filesystem and skipping the test cases with mount and sb marks.
I've also tested fsnotifywait --recursive on fuse and on btrfs subvol. It works as expected - if tree travesal crosses filesystem or subvol boundary, setting the subdir mark fails with -EXDEV.
Thanks,
Amir.
[1] https://lore.kernel.org/linux-fsdevel/20231107-vfs-fsid-5037e344d215@brauner/
[2] https://github.com/amir73il/ltp/commits/fanotify_fsid
[3] https://github.com/amir73il/man-pages/commits/fanotify_fsid
Changes since v1:
- Add missing fsnotify_put_mark()
- Improve readablity of fanotify_test_fsid()
- Rename fanotify_check_mark_fsid() => fanotify_set_mark_fsid()
- Remove fanotify_mark_fsid() wrapper
Amir Goldstein (2):
fanotify: store fsid in mark instead of in connector
fanotify: allow "weak" fsid when watching a single filesystem
fs/notify/fanotify/fanotify.c | 34 ++------ fs/notify/fanotify/fanotify.h | 16 ++++ fs/notify/fanotify/fanotify_user.c | 124 ++++++++++++++++++++++++----- fs/notify/mark.c | 52 ++---------- include/linux/fsnotify_backend.h | 14 ++-- 5 files changed, 140 insertions(+), 100 deletions(-)
Signature · 1 line
2.34.1
Amir Goldstein
reviewerPATCH v2 1/2 fanotify: store fsid in mark instead of in connector
Some filesystems like fuse and nfs have zero or non-unique fsid. We would like to avoid reporting ambiguous fsid in events, so we need to avoid marking objects with same fsid and different sb.
To make this easier to enforce, store the fsid in the marks of the group instead of in the shared conenctor.
--- fs/notify/fanotify/fanotify.c | 19 +++-------- fs/notify/fanotify/fanotify.h | 10 ++++++ fs/notify/fanotify/fanotify_user.c | 18 ++++++++--- fs/notify/mark.c | 52 +++++------------------------- include/linux/fsnotify_backend.h | 13 +++----- 5 files changed, 42 insertions(+), 70 deletions(-)
fs/notify/fanotify/fanotify.c +5 -14
@@ -838,9 +838,8 @@ static struct fanotify_event *fanotify_alloc_event( }/* -* Get cached fsid of the filesystem containing the object from any connector. -* All connectors are supposed to have the same fsid, but we do not verify that -* here. +* Get cached fsid of the filesystem containing the object from any mark. +* All marks are supposed to have the same fsid, but we do not verify that here. */ static __kernel_fsid_t fanotify_get_fsid (struct fsnotify_iter_info *iter_info) {
@@ -849,17 +848,9 @@ static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)__kernel_fsid_t fsid = {}; fsnotify_foreach_iter_mark_type(iter_info, mark, type) { -struct fsnotify_mark_connector *conn; - - conn =READ_ONCE (mark->connector); -/* Mark is just getting destroyed or created? */ -if (!conn) -continue ; -if (!(conn->flags &FSNOTIFY_CONN_FLAG_HAS_FSID )) +if (!(mark->flags &FSNOTIFY_MARK_FLAG_HAS_FSID ))continue ; -/* Pairs with smp_wmb() in fsnotify_add_mark_list() */ -smp_rmb (); - fsid = conn->fsid; + fsid =FANOTIFY_MARK (mark)->fsid;if (WARN_ON_ONCE (!fsid.val[0 ] && !fsid.val[1 ]))continue ;return fsid;
@@ -1068,7 +1059,7 @@ static void fanotify_freeing_mark(struct fsnotify_mark *mark,static void fanotify_free_mark (struct fsnotify_mark *fsn_mark) { -kmem_cache_free (fanotify_mark_cache, fsn_mark); +kmem_cache_free (fanotify_mark_cache,FANOTIFY_MARK (fsn_mark)); }const struct fsnotify_ops fanotify_fsnotify_ops = {
fs/notify/fanotify/fanotify.h +10 -0
@@ -489,6 +489,16 @@ static inline unsigned int fanotify_event_hash_bucket(return event->hash &FANOTIFY_HTABLE_MASK ; } +struct fanotify_mark { +struct fsnotify_mark fsn_mark; +__kernel_fsid_t fsid; +}; + +static inline struct fanotify_mark *FANOTIFY_MARK (struct fsnotify_mark *mark) +{ + return container_of(mark, struct fanotify_mark, fsn_mark); +} +static inline unsigned int fanotify_mark_user_flags (struct fsnotify_mark *mark) {unsigned int mflags =0 ;
fs/notify/fanotify/fanotify_user.c +14 -4
@@ -1199,6 +1199,7 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, __kernel_fsid_t *fsid) {struct ucounts *ucounts = group->fanotify_data.ucounts; +struct fanotify_mark *fan_mark;struct fsnotify_mark *mark;int ret;
@@ -1211,17 +1212,26 @@ static struct fsnotify_mark *fanotify_add_new_mark(struct fsnotify_group *group, !inc_ucount(ucounts->ns, ucounts->uid, UCOUNT_FANOTIFY_MARKS))return ERR_PTR (-ENOSPC ); - mark =kmem_cache_alloc (fanotify_mark_cache,GFP_KERNEL ); -if (!mark) { + fan_mark =kmem_cache_alloc (fanotify_mark_cache,GFP_KERNEL ); +if (!fan_mark) { ret = -ENOMEM ; goto out_dec_ucounts; } + mark = &fan_mark->fsn_mark;fsnotify_init_mark (mark, group);if (fan_flags &FAN_MARK_EVICTABLE ) mark->flags |=FSNOTIFY_MARK_FLAG_NO_IREF ; - ret =fsnotify_add_mark_locked (mark, connp, obj_type,0 , fsid); +/* Cache fsid of filesystem containing the marked object */ +if (fsid) { + fan_mark->fsid = *fsid; + mark->flags |=FSNOTIFY_MARK_FLAG_HAS_FSID ; + }else { + fan_mark->fsid.val[0 ] = fan_mark->fsid.val[1 ] =0 ; + } + + ret =fsnotify_add_mark_locked (mark, connp, obj_type,0 );if (ret) {fsnotify_put_mark (mark); goto out_dec_ucounts;
@@ -1935,7 +1945,7 @@ static int __init fanotify_user_setup(void)BUILD_BUG_ON (HWEIGHT32 (FANOTIFY_INIT_FLAGS ) !=12 );BUILD_BUG_ON (HWEIGHT32 (FANOTIFY_MARK_FLAGS ) !=11 ); - fanotify_mark_cache =KMEM_CACHE (fsnotify_mark, + fanotify_mark_cache =KMEM_CACHE (fanotify_mark,SLAB_PANIC |SLAB_ACCOUNT ); fanotify_fid_event_cachep =KMEM_CACHE (fanotify_fid_event,SLAB_PANIC );
fs/notify/mark.c +8 -44
107 lines not on this page · raw message
include/linux/fsnotify_backend.h +5 -8
@@ -472,10 +472,8 @@ typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;struct fsnotify_mark_connector {spinlock_t lock;unsigned short type;/* Type of object [lock] */ -#define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01#define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02unsigned short flags;/* flags [lock] */ -__kernel_fsid_t fsid;/* fsid of filesystem containing object */ union {/* Object pointer [lock] */ fsnotify_connp_t *obj;
@@ -530,6 +528,7 @@ struct fsnotify_mark {#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100#define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200#define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400 +#define FSNOTIFY_MARK_FLAG_HAS_FSID 0x0800unsigned int flags;/* flags [mark->lock] */ };
@@ -763,11 +762,10 @@ extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,/* attach the mark to the object */ extern int fsnotify_add_mark (struct fsnotify_mark *mark,fsnotify_connp_t *connp,unsigned int obj_type, -int add_flags,__kernel_fsid_t *fsid); +int add_flags);extern int fsnotify_add_mark_locked (struct fsnotify_mark *mark,fsnotify_connp_t *connp, -unsigned int obj_type,int add_flags, -__kernel_fsid_t *fsid); +unsigned int obj_type,int add_flags);/* attach the mark to the inode */ static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
@@ -775,15 +773,14 @@ static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark, int add_flags) {return fsnotify_add_mark (mark, &inode->i_fsnotify_marks, -FSNOTIFY_OBJ_TYPE_INODE , add_flags,NULL ); +FSNOTIFY_OBJ_TYPE_INODE , add_flags); }static inline int fsnotify_add_inode_mark_locked (struct fsnotify_mark *mark,struct inode *inode,int add_flags) {return fsnotify_add_mark_locked (mark, &inode->i_fsnotify_marks, -FSNOTIFY_OBJ_TYPE_INODE , add_flags, -NULL ); +FSNOTIFY_OBJ_TYPE_INODE , add_flags); }/* given a group and a mark, flag mark to be freed when all references are dropped */
Signature · 1 line
2.34.1
Amir Goldstein
reviewerPATCH v2 2/2 fanotify: allow "weak" fsid when watching a single filesystem
In reply to Amir GoldsteinSo far, fanotify returns -ENODEV or -EXDEV when trying to set a mark on a filesystem with a "weak" fsid, namely, zero fsid (e.g. fuse), or non-uniform fsid (e.g. btrfs non-root subvol).
When group is watching inodes all from the same filesystem (or subvol), allow adding inode marks with "weak" fsid, because there is no ambiguity regarding which filesystem reports the event.
The first mark added to a group determines if this group is single or multi filesystem, depending on the fsid at the path of the added mark.
If the first mark added has a "strong" fsid, marks with "weak" fsid cannot be added and vice versa.
If the first mark added has a "weak" fsid, following marks must have the same "weak" fsid and the same sb as the first mark.
--- fs/notify/fanotify/fanotify.c | 15 +--- fs/notify/fanotify/fanotify.h | 6 ++ fs/notify/fanotify/fanotify_user.c | 112 +++++++++++++++++++++++------ include/linux/fsnotify_backend.h | 1 + 4 files changed, 101 insertions(+), 33 deletions(-)
fs/notify/fanotify/fanotify.c +3 -12
@@ -29,12 +29,6 @@ static unsigned int fanotify_hash_path(const struct path *path)hash_ptr (path->mnt,FANOTIFY_EVENT_HASH_BITS ); } -static inline bool fanotify_fsid_equal (__kernel_fsid_t *fsid1, -__kernel_fsid_t *fsid2) -{ -return fsid1->val[0 ] == fsid2->val[0 ] && fsid1->val[1 ] == fsid2->val[1 ]; -} -static unsigned int fanotify_hash_fsid (__kernel_fsid_t *fsid) { return hash_32(fsid->val[0], FANOTIFY_EVENT_HASH_BITS) ^
@@ -851,7 +845,8 @@ static __kernel_fsid_t fanotify_get_fsid(struct fsnotify_iter_info *iter_info)if (!(mark->flags &FSNOTIFY_MARK_FLAG_HAS_FSID ))continue ; fsid =FANOTIFY_MARK (mark)->fsid; -if (WARN_ON_ONCE (!fsid.val[0 ] && !fsid.val[1 ])) +if (!(mark->flags &FSNOTIFY_MARK_FLAG_WEAK_FSID ) && +WARN_ON_ONCE (!fsid.val[0 ] && !fsid.val[1 ]))continue ;return fsid; }
@@ -933,12 +928,8 @@ static int fanotify_handle_event(struct fsnotify_group *group, u32 mask,return 0 ; } -if (FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS )) { +if (FAN_GROUP_FLAG (group,FANOTIFY_FID_BITS )) fsid =fanotify_get_fsid (iter_info); -/* Racing with mark destruction or creation? */ -if (!fsid.val[0 ] && !fsid.val[1 ]) -return 0 ; - } event =fanotify_alloc_event (group, mask, data, data_type, dir, file_name, &fsid, match_mask);
fs/notify/fanotify/fanotify.h +6 -0
@@ -499,6 +499,12 @@ static inline struct fanotify_mark *FANOTIFY_MARK(struct fsnotify_mark *mark) return container_of(mark, struct fanotify_mark, fsn_mark); } +static inline bool fanotify_fsid_equal (__kernel_fsid_t *fsid1, +__kernel_fsid_t *fsid2) +{ +return fsid1->val[0 ] == fsid2->val[0 ] && fsid1->val[1 ] == fsid2->val[1 ]; +} +static inline unsigned int fanotify_mark_user_flags (struct fsnotify_mark *mark) {unsigned int mflags =0 ;
fs/notify/fanotify/fanotify_user.c +91 -21
202 lines not on this page · raw message
include/linux/fsnotify_backend.h +1 -0
@@ -529,6 +529,7 @@ struct fsnotify_mark {#define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200#define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400#define FSNOTIFY_MARK_FLAG_HAS_FSID 0x0800 +#define FSNOTIFY_MARK_FLAG_WEAK_FSID 0x1000unsigned int flags;/* flags [mark->lock] */ };
Signature · 1 line
2.34.1
Jan Kara
maintainerRe: [PATCH v2 2/2] fanotify: allow "weak" fsid when watching a single filesystem
Quoted 11 lines · Amir Goldstein
So far, fanotify returns
-ENODEVor-EXDEVwhen trying to set a mark on a filesystem with a "weak" fsid, namely, zero fsid (e.g. fuse), or non-uniform fsid (e.g. btrfs non-root subvol).When group is watching inodes all from the same filesystem (or subvol), allow adding inode marks with "weak" fsid, because there is no ambiguity regarding which filesystem reports the event.
The first mark added to a group determines if this group is single or multi filesystem, depending on the fsid at the path of the added mark.
Quoted 7 lines
If the first mark added has a "strong" fsid, marks with "weak" fsid cannot be added and vice versa.
If the first mark added has a "weak" fsid, following marks must have the same "weak" fsid and the same sb as the first mark.
Signed-off-by Amir Goldstein <[email protected]>
Yep, this is good. Can you please repost the whole series so that b4 can easily pick it up from the list ;)? Thanks!
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Amir Goldstein
reviewerRe: [PATCH v2 2/2] fanotify: allow "weak" fsid when watching a single filesystem
Quoted 16 lines · Jan Kara
Quoted 14 lines · Amir Goldstein
So far, fanotify returns
-ENODEVor-EXDEVwhen trying to set a mark on a filesystem with a "weak" fsid, namely, zero fsid (e.g. fuse), or non-uniform fsid (e.g. btrfs non-root subvol).When group is watching inodes all from the same filesystem (or subvol), allow adding inode marks with "weak" fsid, because there is no ambiguity regarding which filesystem reports the event.
The first mark added to a group determines if this group is single or multi filesystem, depending on the fsid at the path of the added mark.
If the first mark added has a "strong" fsid, marks with "weak" fsid cannot be added and vice versa.
Quoted 7 lines
Quoted 4 lines
If the first mark added has a "weak" fsid, following marks must have the same "weak" fsid and the same sb as the first mark.
Signed-off-by Amir Goldstein <[email protected]>Yep, this is good. Can you please repost the whole series so that b4 can easily pick it up from the list ;)? Thanks!
hmm. I posted all but they did not hit the list :-/ now reposted.
Thanks,
Amir.
Amir Goldstein
reviewerRe: [PATCH v2 2/2] fanotify: allow "weak" fsid when watching a single filesystem
Quoted 21 lines · Amir Goldstein
Quoted 19 lines · Jan Kara
Quoted 17 lines · Amir Goldstein
So far, fanotify returns
-ENODEVor-EXDEVwhen trying to set a mark on a filesystem with a "weak" fsid, namely, zero fsid (e.g. fuse), or non-uniform fsid (e.g. btrfs non-root subvol).When group is watching inodes all from the same filesystem (or subvol), allow adding inode marks with "weak" fsid, because there is no ambiguity regarding which filesystem reports the event.
The first mark added to a group determines if this group is single or multi filesystem, depending on the fsid at the path of the added mark.
If the first mark added has a "strong" fsid, marks with "weak" fsid cannot be added and vice versa.
If the first mark added has a "weak" fsid, following marks must have the same "weak" fsid and the same sb as the first mark.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Signed-off-by Amir Goldstein <[email protected]>Yep, this is good. Can you please repost the whole series so that b4 can easily pick it up from the list ;)? Thanks!
hmm. I posted all but they did not hit the list :-/ now reposted.
damnit! only the first REPOST patch hit the list,
but the first v2 patch now trickled.
You can get it from github if you prefer:
https://github.com/amir73il/linux/commits/fanotify_fsid
Thanks,
Amir.
Jan Kara
maintainerHi!
Quoted 16 lines · Amir Goldstein
Jan,
In the vfs fanotify update for v6.7-rc1 [1], we considerably increased the amount of filesystems that can setup inode marks with
FAN_REPORT_FID:
- NFS export is no longer required for setting up inode marks
- All the simple fs gained a non-zero fsidThis leaves the following in-tree filesystems where inode marks with
FAN_REPORT_FIDcannot be set:
- nfs, fuse, afs, coda (zero fsid)
- btrfs non-root subvol (fsid not a unique identifier of sb)This patch set takes care of these remaining cases, by allowing inode marks, as long as all inode marks in the group are contained to the same filesystem and same fsid (i.e. subvol).
Quoted 8 lines
I've written some basic sanity tests [2] and a man-page update draft [3]. The LTP tests excersize the new code by running tests that did not run before on ntfs-3g fuse filesystem and skipping the test cases with mount and sb marks.
I've also tested fsnotifywait --recursive on fuse and on btrfs subvol. It works as expected - if tree travesal crosses filesystem or subvol boundary, setting the subdir mark fails with
-EXDEV.
Now all the patches have arrived. Queued into my tree.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR