Commit Graph

711 Commits

Author SHA1 Message Date
Miklos Szeredi e908619dbd fanotify: limit reporting of event with non-decodeable file handles
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 97ac489775f26acfd46a8a60c2f84ce7cc79fa4b
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed Oct 18 12:59:56 2023 +0300

    fanotify: limit reporting of event with non-decodeable file handles

    Commit a95aef69a740 ("fanotify: support reporting non-decodeable file
    handles") merged in v6.5-rc1, added the ability to use an fanotify group
    with FAN_REPORT_FID mode to watch filesystems that do not support nfs
    export, but do know how to encode non-decodeable file handles, with the
    newly introduced AT_HANDLE_FID flag.

    At the time that this commit was merged, there were no filesystems
    in-tree with those traits.

    Commit 16aac5ad1fa9 ("ovl: support encoding non-decodable file handles"),
    merged in v6.6-rc1, added this trait to overlayfs, thus allowing fanotify
    watching of overlayfs with FAN_REPORT_FID mode.

    In retrospect, allowing an fanotify filesystem/mount mark on such
    filesystem in FAN_REPORT_FID mode will result in getting events with
    file handles, without the ability to resolve the filesystem objects from
    those file handles (i.e. no open_by_handle_at() support).

    For v6.6, the safer option would be to allow this mode for inode marks
    only, where the caller has the opportunity to use name_to_handle_at() at
    the time of setting the mark. In the future we can revise this decision.

    Fixes: a95aef69a740 ("fanotify: support reporting non-decodeable file handles")
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Message-Id: <20231018100000.2453965-2-amir73il@gmail.com>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:19 +02:00
Miklos Szeredi b5ce01a2ab fanotify: delete useless parenthesis in FANOTIFY_INLINE_FH macro
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 1758cd2e95d31b308f29ae3828ae92c8b8d20466
Author: Alexey Dobriyan <adobriyan@gmail.com>
Date:   Tue Oct 10 14:44:35 2023 +0300

    fanotify: delete useless parenthesis in FANOTIFY_INLINE_FH macro

    Parenthesis around identifier name in declaration are useless.
    This is just "put every macro argument inside parenthesis" practice.

    Now "size" must be constant expression, but using comma expression in
    constant expression is useless too, therefore [] will guard "size"
    expression just as well as ().

    Also g++ is somewhat upset about these:

            fs/notify/fanotify/fanotify.h:278:28: warning: unnecessary parentheses in declaration of ‘object_fh’ [-Wparentheses]
              278 |         struct fanotify_fh (name);

    Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Message-Id: <633c251a-b548-4428-9e91-1cf8147d8c55@p183>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:19 +02:00
Miklos Szeredi a670a85f67 dnotify: Pass argument of fcntl_dirnotify as int
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit f4ae4081e5a871b4c4e2f802ccf73f8d69566073
Author: Luca Vizzarro <Luca.Vizzarro@arm.com>
Date:   Wed Feb 15 15:50:05 2023 +0000

    dnotify: Pass argument of fcntl_dirnotify as int

    The interface for fcntl expects the argument passed for the command
    F_DIRNOTIFY to be of type int. The current code wrongly treats it as
    a long. In order to avoid access to undefined bits, we should explicitly
    cast the argument to int.

    Cc: Jan Kara <jack@suse.cz>
    Cc: Amir Goldstein <amir73il@gmail.com>
    Cc: Alexander Viro <viro@zeniv.linux.org.uk>
    Cc: Christian Brauner <brauner@kernel.org>
    Cc: Jeff Layton <jlayton@kernel.org>
    Cc: Chuck Lever <chuck.lever@oracle.com>
    Cc: Kevin Brodsky <Kevin.Brodsky@arm.com>
    Cc: Vincenzo Frascino <Vincenzo.Frascino@arm.com>
    Cc: Szabolcs Nagy <Szabolcs.Nagy@arm.com>
    Cc: "Theodore Ts'o" <tytso@mit.edu>
    Cc: David Laight <David.Laight@ACULAB.com>
    Cc: Mark Rutland <Mark.Rutland@arm.com>
    Cc: linux-fsdevel@vger.kernel.org
    Cc: linux-morello@op-lists.linaro.org
    Acked-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Luca Vizzarro <Luca.Vizzarro@arm.com>
    Message-Id: <20230414152459.816046-6-Luca.Vizzarro@arm.com>
    Signed-off-by: Christian Brauner <brauner@kernel.org>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi ec686bb035 fanotify: disallow mount/sb marks on kernel internal pseudo fs
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 69562eb0bd3e6bb8e522a7b254334e0fb30dff0c
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Thu Jun 29 07:20:44 2023 +0300

    fanotify: disallow mount/sb marks on kernel internal pseudo fs

    Hopefully, nobody is trying to abuse mount/sb marks for watching all
    anonymous pipes/inodes.

    I cannot think of a good reason to allow this - it looks like an
    oversight that dated back to the original fanotify API.

    Link: https://lore.kernel.org/linux-fsdevel/20230628101132.kvchg544mczxv2pm@quack3/
    Fixes: 0ff21db9fc ("fanotify: hooks the fanotify_mark syscall to the vfsmount code")
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Reviewed-by: Christian Brauner <brauner@kernel.org>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Message-Id: <20230629042044.25723-1-amir73il@gmail.com>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 8f2a9dd407 fanotify: support reporting non-decodeable file handles
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit a95aef69a740f5a1c7d70f2b58552207edaef99a
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Tue May 2 15:48:17 2023 +0300

    fanotify: support reporting non-decodeable file handles

    fanotify users do not always need to decode the file handles reported
    with FAN_REPORT_FID.

    Relax the restriction that filesystem needs to support NFS export and
    allow reporting file handles from filesystems that only support ecoding
    unique file handles.

    Even filesystems that do not have export_operations at all can fallback
    to use the default FILEID_INO32_GEN encoding, but we use the existence
    of export_operations as an indication that the encoded file handles will
    be sufficiently unique and that user will be able to compare them to
    filesystem objects using AT_HANDLE_FID flag to name_to_handle_at(2).

    For filesystems that do not support NFS export, users will have to use
    the AT_HANDLE_FID of name_to_handle_at(2) if they want to compare the
    object in path to the object fid reported in an event.

    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Message-Id: <20230502124817.3070545-5-amir73il@gmail.com>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi a71f53085b inotify: Avoid reporting event with invalid wd
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit c915d8f5918bea7c3962b09b8884ca128bfd9b0c
Author: Jan Kara <jack@suse.cz>
Date:   Mon Apr 24 18:32:19 2023 +0200

    inotify: Avoid reporting event with invalid wd

    When inotify_freeing_mark() races with inotify_handle_inode_event() it
    can happen that inotify_handle_inode_event() sees that i_mark->wd got
    already reset to -1 and reports this value to userspace which can
    confuse the inotify listener. Avoid the problem by validating that wd is
    sensible (and pretend the mark got removed before the event got
    generated otherwise).

    CC: stable@vger.kernel.org
    Fixes: 7e790dd5fc ("inotify: fix error paths in inotify_update_watch")
    Message-Id: <20230424163219.9250-1-jack@suse.cz>
    Reported-by: syzbot+4a06d4373fd52f0b2f9c@syzkaller.appspotmail.com
    Reviewed-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi c1f761a893 fanotify: Remove obsoleted fanotify_event_has_path()
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 7a80bf902d2bc722b4477442ee772e8574603185
Author: Gaosheng Cui <cuigaosheng1@huawei.com>
Date:   Mon Sep 26 10:30:18 2022 +0800

    fanotify: Remove obsoleted fanotify_event_has_path()

    All uses of fanotify_event_has_path() have
    been removed since commit 9c61f3b560 ("fanotify: break up
    fanotify_alloc_event()"), now it is useless, so remove it.

    Link: https://lore.kernel.org/r/20220926023018.1505270-1-cuigaosheng1@huawei.com
    Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 3cab0a5cee fsnotify: remove unused declaration
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit f847c74d6e89f10926db58649a05b99237258691
Author: Gaosheng Cui <cuigaosheng1@huawei.com>
Date:   Fri Sep 9 11:38:28 2022 +0800

    fsnotify: remove unused declaration

    fsnotify_alloc_event_holder() and fsnotify_destroy_event_holder()
    has been removed since commit 7053aee26a ("fsnotify: do not share
    events between notification groups"), so remove it.

    Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
    Signed-off-by: Gaosheng Cui <cuigaosheng1@huawei.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 57aa63a8aa fsnotify: Fix comment typo
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit feee1ce45a5666bbdb08c5bb2f5f394047b1915b
Author: Xin Gao <gaoxin@cdjrlc.com>
Date:   Sat Jul 23 03:46:39 2022 +0800

    fsnotify: Fix comment typo

    The double `if' is duplicated in line 104, remove one.

    Signed-off-by: Xin Gao <gaoxin@cdjrlc.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220722194639.18545-1-gaoxin@cdjrlc.com

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi f9a9f95d06 fanotify: introduce FAN_MARK_IGNORE
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit e252f2ed1c8c6c3884ab5dd34e003ed21f1fe6e0
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed Jun 29 17:42:10 2022 +0300

    fanotify: introduce FAN_MARK_IGNORE

    This flag is a new way to configure ignore mask which allows adding and
    removing the event flags FAN_ONDIR and FAN_EVENT_ON_CHILD in ignore mask.

    The legacy FAN_MARK_IGNORED_MASK flag would always ignore events on
    directories and would ignore events on children depending on whether
    the FAN_EVENT_ON_CHILD flag was set in the (non ignored) mask.

    FAN_MARK_IGNORE can be used to ignore events on children without setting
    FAN_EVENT_ON_CHILD in the mark's mask and will not ignore events on
    directories unconditionally, only when FAN_ONDIR is set in ignore mask.

    The new behavior is non-downgradable.  After calling fanotify_mark() with
    FAN_MARK_IGNORE once, calling fanotify_mark() with FAN_MARK_IGNORED_MASK
    on the same object will return EEXIST error.

    Setting the event flags with FAN_MARK_IGNORE on a non-dir inode mark
    has no meaning and will return ENOTDIR error.

    The meaning of FAN_MARK_IGNORED_SURV_MODIFY is preserved with the new
    FAN_MARK_IGNORE flag, but with a few semantic differences:

    1. FAN_MARK_IGNORED_SURV_MODIFY is required for filesystem and mount
       marks and on an inode mark on a directory. Omitting this flag
       will return EINVAL or EISDIR error.

    2. An ignore mask on a non-directory inode that survives modify could
       never be downgraded to an ignore mask that does not survive modify.
       With new FAN_MARK_IGNORE semantics we make that rule explicit -
       trying to update a surviving ignore mask without the flag
       FAN_MARK_IGNORED_SURV_MODIFY will return EEXIST error.

    The conveniene macro FAN_MARK_IGNORE_SURV is added for
    (FAN_MARK_IGNORE | FAN_MARK_IGNORED_SURV_MODIFY), because the
    common case should use short constant names.

    Link: https://lore.kernel.org/r/20220629144210.2983229-4-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 439889396d fanotify: cleanups for fanotify_mark() input validations
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 8afd7215aa97f8868d033f6e1d01a276ab2d29c0
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed Jun 29 17:42:09 2022 +0300

    fanotify: cleanups for fanotify_mark() input validations

    Create helper fanotify_may_update_existing_mark() for checking for
    conflicts between existing mark flags and fanotify_mark() flags.

    Use variable mark_cmd to make the checks for mark command bits
    cleaner.

    Link: https://lore.kernel.org/r/20220629144210.2983229-3-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 199d513f96 fanotify: prepare for setting event flags in ignore mask
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 31a371e419c885e0f137ce70395356ba8639dc52
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed Jun 29 17:42:08 2022 +0300

    fanotify: prepare for setting event flags in ignore mask

    Setting flags FAN_ONDIR FAN_EVENT_ON_CHILD in ignore mask has no effect.
    The FAN_EVENT_ON_CHILD flag in mask implicitly applies to ignore mask and
    ignore mask is always implicitly applied to events on directories.

    Define a mark flag that replaces this legacy behavior with logic of
    applying the ignore mask according to event flags in ignore mask.

    Implement the new logic to prepare for supporting an ignore mask that
    ignores events on children and ignore mask that does not ignore events
    on directories.

    To emphasize the change in terminology, also rename ignored_mask mark
    member to ignore_mask and use accessors to get only the effective
    ignored events or the ignored events and flags.

    This change in terminology finally aligns with the "ignore mask"
    language in man pages and in most of the comments.

    Link: https://lore.kernel.org/r/20220629144210.2983229-2-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 486d460e27 fanotify: refine the validation checks on non-dir inode mask
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 8698e3bab4dd7968666e84e111d0bfd17c040e77
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Jun 27 20:47:19 2022 +0300

    fanotify: refine the validation checks on non-dir inode mask

    Commit ceaf69f8eadc ("fanotify: do not allow setting dirent events in
    mask of non-dir") added restrictions about setting dirent events in the
    mask of a non-dir inode mark, which does not make any sense.

    For backward compatibility, these restictions were added only to new
    (v5.17+) APIs.

    It also does not make any sense to set the flags FAN_EVENT_ON_CHILD or
    FAN_ONDIR in the mask of a non-dir inode.  Add these flags to the
    dir-only restriction of the new APIs as well.

    Move the check of the dir-only flags for new APIs into the helper
    fanotify_events_supported(), which is only called for FAN_MARK_ADD,
    because there is no need to error on an attempt to remove the dir-only
    flags from non-dir inode.

    Fixes: ceaf69f8eadc ("fanotify: do not allow setting dirent events in mask of non-dir")
    Link: https://lore.kernel.org/linux-fsdevel/20220627113224.kr2725conevh53u4@quack3.lan/
    Link: https://lore.kernel.org/r/20220627174719.2838175-1-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 7183386cfb fanotify: fix incorrect fmode_t casts
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit dccd855771b37820b6d976a99729c88259549f85
Author: Vasily Averin <vvs@openvz.org>
Date:   Sun May 22 15:08:02 2022 +0300

    fanotify: fix incorrect fmode_t casts

    Fixes sparce warnings:
    fs/notify/fanotify/fanotify_user.c:267:63: sparse:
     warning: restricted fmode_t degrades to integer
    fs/notify/fanotify/fanotify_user.c:1351:28: sparse:
     warning: restricted fmode_t degrades to integer

    FMODE_NONTIFY have bitwise fmode_t type and requires __force attribute
    for any casts.

    Signed-off-by: Vasily Averin <vvs@openvz.org>
    Reviewed-by: Christian Brauner (Microsoft) <brauner@kernel.org>
    Reviewed-by: Christoph Hellwig <hch@lst.de>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/9adfd6ac-1b89-791e-796b-49ada3293985@openvz.org

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:18 +02:00
Miklos Szeredi 5f652ad3f8 fsnotify: consistent behavior for parent not watching children
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit e730558adffb88a52e562db089e969ee9510184a
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed May 11 22:02:13 2022 +0300

    fsnotify: consistent behavior for parent not watching children

    The logic for handling events on child in groups that have a mark on
    the parent inode, but without FS_EVENT_ON_CHILD flag in the mask is
    duplicated in several places and inconsistent.

    Move the logic into the preparation of mark type iterator, so that the
    parent mark type will be excluded from all mark type iterations in that
    case.

    This results in several subtle changes of behavior, hopefully all
    desired changes of behavior, for example:

    - Group A has a mount mark with FS_MODIFY in mask
    - Group A has a mark with ignore mask that does not survive FS_MODIFY
      and does not watch children on directory D.
    - Group B has a mark with FS_MODIFY in mask that does watch children
      on directory D.
    - FS_MODIFY event on file D/foo should not clear the ignore mask of
      group A, but before this change it does

    And if group A ignore mask was set to survive FS_MODIFY:
    - FS_MODIFY event on file D/foo should be reported to group A on account
      of the mount mark, but before this change it is wrongly ignored

    Fixes: 2f02fd3fa1 ("fanotify: fix ignore mask logic for events on child and on dir")
    Reported-by: Jan Kara <jack@suse.com>
    Link: https://lore.kernel.org/linux-fsdevel/20220314113337.j7slrb5srxukztje@quack3.lan/
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220511190213.831646-3-amir73il@gmail.com

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 559f294da4 fsnotify: introduce mark type iterator
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 14362a2541797cf9df0e86fb12dcd7950baf566e
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed May 11 22:02:12 2022 +0300

    fsnotify: introduce mark type iterator

    fsnotify_foreach_iter_mark_type() is used to reduce boilerplate code
    of iterating all marks of a specific group interested in an event
    by consulting the iterator report_mask.

    Use an open coded version of that iterator in fsnotify_iter_next()
    that collects all marks of the current iteration group without
    consulting the iterator report_mask.

    At the moment, the two iterator variants are the same, but this
    decoupling will allow us to exclude some of the group's marks from
    reporting the event, for example for event on child and inode marks
    on parent did not request to watch events on children.

    Fixes: 2f02fd3fa1 ("fanotify: fix ignore mask logic for events on child and on dir")
    Reported-by: Jan Kara <jack@suse.com>
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220511190213.831646-2-amir73il@gmail.com

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 5dc492385e fanotify: do not allow setting dirent events in mask of non-dir
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit ceaf69f8eadcafb323392be88e7a5248c415d423
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Sat May 7 11:00:28 2022 +0300

    fanotify: do not allow setting dirent events in mask of non-dir

    Dirent events (create/delete/move) are only reported on watched
    directory inodes, but in fanotify as well as in legacy inotify, it was
    always allowed to set them on non-dir inode, which does not result in
    any meaningful outcome.

    Until kernel v5.17, dirent events in fanotify also differed from events
    "on child" (e.g. FAN_OPEN) in the information provided in the event.
    For example, FAN_OPEN could be set in the mask of a non-dir or the mask
    of its parent and event would report the fid of the child regardless of
    the marked object.
    By contrast, FAN_DELETE is not reported if the child is marked and the
    child fid was not reported in the events.

    Since kernel v5.17, with fanotify group flag FAN_REPORT_TARGET_FID, the
    fid of the child is reported with dirent events, like events "on child",
    which may create confusion for users expecting the same behavior as
    events "on child" when setting events in the mask on a child.

    The desired semantics of setting dirent events in the mask of a child
    are not clear, so for now, deny this action for a group initialized
    with flag FAN_REPORT_TARGET_FID and for the new event FAN_RENAME.
    We may relax this restriction in the future if we decide on the
    semantics and implement them.

    Fixes: d61fd650e9d2 ("fanotify: introduce group flag FAN_REPORT_TARGET_FID")
    Fixes: 8cc3b1ccd930 ("fanotify: wire up FAN_RENAME event")
    Link: https://lore.kernel.org/linux-fsdevel/20220505133057.zm5t6vumc4xdcnsg@quack3.lan/
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220507080028.219826-1-amir73il@gmail.com

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi a8a76ee318 fanotify: enable "evictable" inode marks
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 5f9d3bd520261fd7a850818c71809fd580e0f30c
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:27 2022 +0300

    fanotify: enable "evictable" inode marks

    Now that the direct reclaim path is handled we can enable evictable
    inode marks.

    Link: https://lore.kernel.org/r/20220422120327.3459282-17-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 1ebaae1d6b fanotify: use fsnotify group lock helpers
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit e79719a2ca5c61912c0493bc1367db52759cf6fd
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:26 2022 +0300

    fanotify: use fsnotify group lock helpers

    Direct reclaim from fanotify mark allocation context may try to evict
    inodes with evictable marks of the same group and hit this deadlock:

    [<0>] fsnotify_destroy_mark+0x1f/0x3a
    [<0>] fsnotify_destroy_marks+0x71/0xd9
    [<0>] __destroy_inode+0x24/0x7e
    [<0>] destroy_inode+0x2c/0x67
    [<0>] dispose_list+0x49/0x68
    [<0>] prune_icache_sb+0x5b/0x79
    [<0>] super_cache_scan+0x11c/0x16f
    [<0>] shrink_slab.constprop.0+0x23e/0x40f
    [<0>] shrink_node+0x218/0x3e7
    [<0>] do_try_to_free_pages+0x12a/0x2d2
    [<0>] try_to_free_pages+0x166/0x242
    [<0>] __alloc_pages_slowpath.constprop.0+0x30c/0x903
    [<0>] __alloc_pages+0xeb/0x1c7
    [<0>] cache_grow_begin+0x6f/0x31e
    [<0>] fallback_alloc+0xe0/0x12d
    [<0>] ____cache_alloc_node+0x15a/0x17e
    [<0>] kmem_cache_alloc_trace+0xa1/0x143
    [<0>] fanotify_add_mark+0xd5/0x2b2
    [<0>] do_fanotify_mark+0x566/0x5eb
    [<0>] __x64_sys_fanotify_mark+0x21/0x24
    [<0>] do_syscall_64+0x6d/0x80
    [<0>] entry_SYSCALL_64_after_hwframe+0x44/0xae

    Set the FSNOTIFY_GROUP_NOFS flag to prevent going into direct reclaim
    from allocations under fanotify group lock and use the safe group lock
    helpers.

    Link: https://lore.kernel.org/r/20220422120327.3459282-16-amir73il@gmail.com
    Suggested-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi a1e8487c75 fanotify: implement "evictable" inode marks
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 7d5e005d982527e4029b0139823d179986e34cdc
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:25 2022 +0300

    fanotify: implement "evictable" inode marks

    When an inode mark is created with flag FAN_MARK_EVICTABLE, it will not
    pin the marked inode to inode cache, so when inode is evicted from cache
    due to memory pressure, the mark will be lost.

    When an inode mark with flag FAN_MARK_EVICATBLE is updated without using
    this flag, the marked inode is pinned to inode cache.

    When an inode mark is updated with flag FAN_MARK_EVICTABLE but an
    existing mark already has the inode pinned, the mark update fails with
    error EEXIST.

    Evictable inode marks can be used to setup inode marks with ignored mask
    to suppress events from uninteresting files or directories in a lazy
    manner, upon receiving the first event, without having to iterate all
    the uninteresting files or directories before hand.

    The evictbale inode mark feature allows performing this lazy marks setup
    without exhausting the system memory with pinned inodes.

    This change does not enable the feature yet.

    Link: https://lore.kernel.org/linux-fsdevel/CAOQ4uxiRDpuS=2uA6+ZUM7yG9vVU-u212tkunBmSnP_u=mkv=Q@mail.gmail.com/
    Link: https://lore.kernel.org/r/20220422120327.3459282-15-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi e68d6a9284 fanotify: factor out helper fanotify_mark_update_flags()
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 8998d110835e3781ccd3f1ae061a590b4aaba911
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:24 2022 +0300

    fanotify: factor out helper fanotify_mark_update_flags()

    Handle FAN_MARK_IGNORED_SURV_MODIFY flag change in a helper that
    is called after updating the mark mask.

    Replace the added and removed return values and help variables with
    bool recalc return values and help variable, which makes the code a
    bit easier to follow.

    Rename flags argument to fan_flags to emphasize the difference from
    mark->flags.

    Link: https://lore.kernel.org/r/20220422120327.3459282-14-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 61c4c450b0 fanotify: create helper fanotify_mark_user_flags()
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 4adce25ccfff215939ee465b8c0aa70526d5c352
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:23 2022 +0300

    fanotify: create helper fanotify_mark_user_flags()

    To translate from fsnotify mark flags to user visible flags.

    Link: https://lore.kernel.org/r/20220422120327.3459282-13-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 280b354256 fsnotify: allow adding an inode mark without pinning inode
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit c3638b5b13740fa31762d414bbce8b7a694e582a
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:22 2022 +0300

    fsnotify: allow adding an inode mark without pinning inode

    fsnotify_add_mark() and variants implicitly take a reference on inode
    when attaching a mark to an inode.

    Make that behavior opt-out with the mark flag FSNOTIFY_MARK_FLAG_NO_IREF.

    Instead of taking the inode reference when attaching connector to inode
    and dropping the inode reference when detaching connector from inode,
    take the inode reference on attach of the first mark that wants to hold
    an inode reference and drop the inode reference on detach of the last
    mark that wants to hold an inode reference.

    Backends can "upgrade" an existing mark to take an inode reference, but
    cannot "downgrade" a mark with inode reference to release the refernce.

    This leaves the choice to the backend whether or not to pin the inode
    when adding an inode mark.

    This is intended to be used when adding a mark with ignored mask that is
    used for optimization in cases where group can afford getting unneeded
    events and reinstate the mark with ignored mask when inode is accessed
    again after being evicted.

    Link: https://lore.kernel.org/r/20220422120327.3459282-12-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi b429a7b880 dnotify: use fsnotify group lock helpers
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit aabb45fdcb31f00f1e7cae2bce83e83474a87c03
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:21 2022 +0300

    dnotify: use fsnotify group lock helpers

    Before commit 9542e6a643 ("nfsd: Containerise filecache laundrette")
    nfsd would close open files in direct reclaim context.  There is no
    guarantee that others memory shrinkers don't do the same and no
    guarantee that future shrinkers won't do that.

    For example, if overlayfs implements inode cache of fscache would
    keep open files to cached objects, inode shrinkers could end up closing
    open files to underlying fs.

    Direct reclaim from dnotify mark allocation context may try to close
    open files that have dnotify marks of the same group and hit a deadlock
    on mark_mutex.

    Set the FSNOTIFY_GROUP_NOFS flag to prevent going into direct reclaim
    from allocations under dnotify group lock and use the safe group lock
    helpers.

    Link: https://lore.kernel.org/r/20220422120327.3459282-11-amir73il@gmail.com
    Suggested-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 25bba4d6d6 inotify: use fsnotify group lock helpers
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 642054b87058019be36033f73c3e48ffff1915aa
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:18 2022 +0300

    inotify: use fsnotify group lock helpers

    inotify inode marks pin the inode so there is no need to set the
    FSNOTIFY_GROUP_NOFS flag.

    Link: https://lore.kernel.org/r/20220422120327.3459282-8-amir73il@gmail.com
    Suggested-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 4ff6d48116 fsnotify: create helpers for group mark_mutex lock
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 43b245a788e2d8f1bb742668a9bdace02fcb3e96
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:17 2022 +0300

    fsnotify: create helpers for group mark_mutex lock

    Create helpers to take and release the group mark_mutex lock.

    Define a flag FSNOTIFY_GROUP_NOFS in fsnotify_group that determines
    if the mark_mutex lock is fs reclaim safe or not.  If not safe, the
    lock helpers take the lock and disable direct fs reclaim.

    In that case we annotate the mutex with a different lockdep class to
    express to lockdep that an allocation of mark of an fs reclaim safe group
    may take the group lock of another "NOFS" group to evict inodes.

    For now, converted only the callers in common code and no backend
    defines the NOFS flag.  It is intended to be set by fanotify for
    evictable marks support.

    Link: https://lore.kernel.org/r/20220422120327.3459282-7-amir73il@gmail.com
    Suggested-by: Jan Kara <jack@suse.cz>
    Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 3c3a6f49ad fsnotify: make allow_dups a property of the group
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit f3010343d9e119da35ee864b3a28993bb5c78ed7
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:16 2022 +0300

    fsnotify: make allow_dups a property of the group

    Instead of passing the allow_dups argument to fsnotify_add_mark()
    as an argument, define the group flag FSNOTIFY_GROUP_DUPS to express
    the allow_dups behavior and set this behavior at group creation time
    for all calls of fsnotify_add_mark().

    Rename the allow_dups argument to generic add_flags argument for future
    use.

    Link: https://lore.kernel.org/r/20220422120327.3459282-6-amir73il@gmail.com
    Suggested-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi a1daca6d78 fsnotify: pass flags argument to fsnotify_alloc_group()
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 867a448d587e7fa845bceaf4ee1c632448f2a9fa
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:15 2022 +0300

    fsnotify: pass flags argument to fsnotify_alloc_group()

    Add flags argument to fsnotify_alloc_group(), define and use the flag
    FSNOTIFY_GROUP_USER in inotify and fanotify instead of the helper
    fsnotify_alloc_user_group() to indicate user allocation.

    Although the flag FSNOTIFY_GROUP_USER is currently not used after group
    allocation, we store the flags argument in the group struct for future
    use of other group flags.

    Link: https://lore.kernel.org/r/20220422120327.3459282-5-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:17 +02:00
Miklos Szeredi 0e98350acf fsnotify: fix wrong lockdep annotations
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 623af4f538b5df9b416e1b82f720af7371b4c771
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:14 2022 +0300

    fsnotify: fix wrong lockdep annotations

    Commit 6960b0d909 ("fsnotify: change locking order") changed some
    of the mark_mutex locks in direct reclaim path to use:
      mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);

    This change is explained:
     "...It uses nested locking to avoid deadlock in case we do the final
      iput() on an inode which still holds marks and thus would take the
      mutex again when calling fsnotify_inode_delete() in destroy_inode()."

    The problem is that the mutex_lock_nested() is not a nested lock at
    all. In fact, it has the opposite effect of preventing lockdep from
    warning about a very possible deadlock.

    Due to these wrong annotations, a deadlock that was introduced with
    nfsd filecache in kernel v5.4 went unnoticed in v5.4.y for over two
    years until it was reported recently by Khazhismel Kumykov, only to
    find out that the deadlock was already fixed in kernel v5.5.

    Fix the wrong lockdep annotations.

    Cc: Khazhismel Kumykov <khazhy@google.com>
    Fixes: 6960b0d909 ("fsnotify: change locking order")
    Link: https://lore.kernel.org/r/20220321112310.vpr7oxro2xkz5llh@quack3.lan/
    Link: https://lore.kernel.org/r/20220422120327.3459282-4-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 99c4b9f15d inotify: move control flags from mask to mark flags
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 38035c04f5865c4ef9597d6beed6a7178f90f64a
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Fri Apr 22 15:03:13 2022 +0300

    inotify: move control flags from mask to mark flags

    The inotify control flags in the mark mask (e.g. FS_IN_ONE_SHOT) are not
    relevant to object interest mask, so move them to the mark flags.

    This frees up some bits in the object interest mask.

    Link: https://lore.kernel.org/r/20220422120327.3459282-3-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 84c39f9e4f fsnotify: remove redundant parameter judgment
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit f92ca72b0263d601807bbd23ed25cbe6f4da89f4
Author: Bang Li <libang.linuxer@gmail.com>
Date:   Fri Mar 11 23:12:40 2022 +0800

    fsnotify: remove redundant parameter judgment

    iput() has already judged the incoming parameter, so there is no need to
    repeat the judgment here.

    Link: https://lore.kernel.org/r/20220311151240.62045-1-libang.linuxer@gmail.com
    Signed-off-by: Bang Li <libang.linuxer@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 588919520f fsnotify: optimize FS_MODIFY events with no ignored masks
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 04e317ba72d07901b03399b3d1525e83424df5b3
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed Feb 23 17:14:38 2022 +0200

    fsnotify: optimize FS_MODIFY events with no ignored masks

    fsnotify() treats FS_MODIFY events specially - it does not skip them
    even if the FS_MODIFY event does not apear in the object's fsnotify
    mask.  This is because send_to_group() checks if FS_MODIFY needs to
    clear ignored mask of marks.

    The common case is that an object does not have any mark with ignored
    mask and in particular, that it does not have a mark with ignored mask
    and without the FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY flag.

    Set FS_MODIFY in object's fsnotify mask during fsnotify_recalc_mask()
    if object has a mark with an ignored mask and without the
    FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY flag and remove the special
    treatment of FS_MODIFY in fsnotify(), so that FS_MODIFY events could
    be optimized in the common case.

    Call fsnotify_recalc_mask() from fanotify after adding or removing an
    ignored mask from a mark without FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY
    or when adding the FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY flag to a mark
    with ignored mask (the flag cannot be removed by fanotify uapi).

    Performance results for doing 10000000 write(2)s to tmpfs:

                                    vanilla         patched
    without notification mark       25.486+-1.054   24.965+-0.244
    with notification mark          30.111+-0.139   26.891+-1.355

    So we can see the overhead of notification subsystem has been
    drastically reduced.

    Link: https://lore.kernel.org/r/20220223151438.790268-3-amir73il@gmail.com
    Suggested-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi e28e5a56e7 fsnotify: fix merge with parent's ignored mask
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 4f0b903ded728c505850daf2914bfc08841f0ae6
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Wed Feb 23 17:14:37 2022 +0200

    fsnotify: fix merge with parent's ignored mask

    fsnotify_parent() does not consider the parent's mark at all unless
    the parent inode shows interest in events on children and in the
    specific event.

    So unless parent added an event to both its mark mask and ignored mask,
    the event will not be ignored.

    Fix this by declaring the interest of an object in an event when the
    event is in either a mark mask or ignored mask.

    Link: https://lore.kernel.org/r/20220223151438.790268-2-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi ed22601090 fanotify: remove variable set but not used
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 217663f101a56ef77f82273818253fff082bf503
Author: Yang Li <yang.lee@linux.alibaba.com>
Date:   Thu Jan 20 13:57:22 2022 +0100

    fanotify: remove variable set but not used

    The code that uses the pointer info has been removed in 7326e382c21e
    ("fanotify: report old and/or new parent+name in FAN_RENAME event").
    and fanotify_event_info() doesn't change 'event', so the declaration and
    assignment of info can be removed.

    Eliminate the following clang warning:
    fs/notify/fanotify/fanotify_user.c:161:24: warning: variable ‘info’ set
    but not used

    Reported-by: Abaci Robot <abaci@linux.alibaba.com>
    Signed-off-by: Yang Li <yang.lee@linux.alibaba.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 03833178b2 fanotify: wire up FAN_RENAME event
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 8cc3b1ccd930fe6971e1527f0c4f1bdc8cb56026
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:37 2021 +0200

    fanotify: wire up FAN_RENAME event

    FAN_RENAME is the successor of FAN_MOVED_FROM and FAN_MOVED_TO
    and can be used to get the old and new parent+name information in
    a single event.

    FAN_MOVED_FROM and FAN_MOVED_TO are still supported for backward
    compatibility, but it makes little sense to use them together with
    FAN_RENAME in the same group.

    FAN_RENAME uses special info type records to report the old and
    new parent+name, so reporting only old and new parent id is less
    useful and was not implemented.
    Therefore, FAN_REANAME requires a group with flag FAN_REPORT_NAME.

    Link: https://lore.kernel.org/r/20211129201537.1932819-12-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 941760ae94 fanotify: report old and/or new parent+name in FAN_RENAME event
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 7326e382c21e9c23c89c88369afdc90b82a14da8
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:36 2021 +0200

    fanotify: report old and/or new parent+name in FAN_RENAME event

    In the special case of FAN_RENAME event, we report old or new or both
    old and new parent+name.

    A single info record will be reported if either the old or new dir
    is watched and two records will be reported if both old and new dir
    (or their filesystem) are watched.

    The old and new parent+name are reported using new info record types
    FAN_EVENT_INFO_TYPE_{OLD,NEW}_DFID_NAME, so if a single info record
    is reported, it is clear to the application, to which dir entry the
    fid+name info is referring to.

    Link: https://lore.kernel.org/r/20211129201537.1932819-11-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 79e9d62076 fanotify: record either old name new name or both for FAN_RENAME
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 2bfbcccde6e7a787feabad4645f628f963fe0663
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:35 2021 +0200

    fanotify: record either old name new name or both for FAN_RENAME

    We do not want to report the dirfid+name of a directory whose
    inode/sb are not watched, because watcher may not have permissions
    to see the directory content.

    Use an internal iter_info to indicate to fanotify_alloc_event()
    which marks of this group are watching FAN_RENAME, so it can decide
    if we need to record only the old parent+name, new parent+name or both.

    Link: https://lore.kernel.org/r/20211129201537.1932819-10-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    [JK: Modified code to pass around only mask of mark types matching
    generated event]
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 9ae8738edf fanotify: record old and new parent and name in FAN_RENAME event
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 3982534ba5ce45e890b2f5ef5e7372c1accd14c7
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:34 2021 +0200

    fanotify: record old and new parent and name in FAN_RENAME event

    In the special case of FAN_RENAME event, we record both the old
    and new parent and name.

    Link: https://lore.kernel.org/r/20211129201537.1932819-9-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi ce9ab841d2 fanotify: support secondary dir fh and name in fanotify_info
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 3cf984e950c1c3f41d407ed31db33beb996be132
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:33 2021 +0200

    fanotify: support secondary dir fh and name in fanotify_info

    Allow storing a secondary dir fh and name tupple in fanotify_info.
    This will be used to store the new parent and name information in
    FAN_RENAME event.

    Link: https://lore.kernel.org/r/20211129201537.1932819-8-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 8d6ff843b1 fanotify: use helpers to parcel fanotify_info buffer
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 1a9515ac9e55e68d733bab81bd408463ab1e25b1
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:32 2021 +0200

    fanotify: use helpers to parcel fanotify_info buffer

    fanotify_info buffer is parceled into variable sized records, so the
    records must be written in order: dir_fh, file_fh, name.

    Use helpers to assert that order and make fanotify_alloc_name_event()
    a bit more generic to allow empty dir_fh record and to allow expanding
    to more records (i.e. name2) soon.

    Link: https://lore.kernel.org/r/20211129201537.1932819-7-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 00110b9372 fanotify: use macros to get the offset to fanotify_info buffer
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 2d9374f095136206a02eb0b6cd9ef94632c1e9f7
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:31 2021 +0200

    fanotify: use macros to get the offset to fanotify_info buffer

    The fanotify_info buffer contains up to two file handles and a name.
    Use macros to simplify the code that access the different items within
    the buffer.

    Add assertions to verify that stored fh len and name len do not overflow
    the u8 stored value in fanotify_info header.

    Remove the unused fanotify_info_len() helper.

    Link: https://lore.kernel.org/r/20211129201537.1932819-6-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:16 +02:00
Miklos Szeredi 273bff4256 fsnotify: generate FS_RENAME event with rich information
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit e54183fa7047c15819bc155f4c58501d9a9a3489
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:30 2021 +0200

    fsnotify: generate FS_RENAME event with rich information

    The dnotify FS_DN_RENAME event is used to request notification about
    a move within the same parent directory and was always coupled with
    the FS_MOVED_FROM event.

    Rename the FS_DN_RENAME event flag to FS_RENAME, decouple it from
    FS_MOVED_FROM and report it with the moved dentry instead of the moved
    inode, so it has the information about both old and new parent and name.

    Generate the FS_RENAME event regardless of same parent dir and apply
    the "same parent" rule in the generic fsnotify_handle_event() helper
    that is used to call backends with ->handle_inode_event() method
    (i.e. dnotify).  The ->handle_inode_event() method is not rich enough to
    report both old and new parent and name anyway.

    The enriched event is reported to fanotify over the ->handle_event()
    method with the old and new dir inode marks in marks array slots for
    ITER_TYPE_INODE and a new iter type slot ITER_TYPE_INODE2.

    The enriched event will be used for reporting old and new parent+name to
    fanotify groups with FAN_RENAME events.

    Link: https://lore.kernel.org/r/20211129201537.1932819-5-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi 95489b8324 fanotify: introduce group flag FAN_REPORT_TARGET_FID
JIRA: https://issues.redhat.com/browse/RHEL-102134
Conflicts: Due to excluded pidfd support

commit d61fd650e9d206a71fda789f02a1ced4b19944c4
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:29 2021 +0200

    fanotify: introduce group flag FAN_REPORT_TARGET_FID

    FAN_REPORT_FID is ambiguous in that it reports the fid of the child for
    some events and the fid of the parent for create/delete/move events.

    The new FAN_REPORT_TARGET_FID flag is an implicit request to report
    the fid of the target object of the operation (a.k.a the child inode)
    also in create/delete/move events in addition to the fid of the parent
    and the name of the child.

    To reduce the test matrix for uninteresting use cases, the new
    FAN_REPORT_TARGET_FID flag requires both FAN_REPORT_NAME and
    FAN_REPORT_FID.  The convenience macro FAN_REPORT_DFID_NAME_TARGET
    combines FAN_REPORT_TARGET_FID with all the required flags.

    Link: https://lore.kernel.org/r/20211129201537.1932819-4-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi ec8f784e76 fsnotify: separate mark iterator type from object type enum
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 1c9007d62bea6fd164285314f7553f73e5308863
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:28 2021 +0200

    fsnotify: separate mark iterator type from object type enum

    They are two different types that use the same enum, so this confusing.

    Use the object type to indicate the type of object mark is attached to
    and the iter type to indicate the type of watch.

    A group can have two different watches of the same object type (parent
    and child watches) that match the same event.

    Link: https://lore.kernel.org/r/20211129201537.1932819-3-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi 61b60473cd fsnotify: clarify object type argument
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit ad69cd9972e79aba103ba5365de0acd35770c265
Author: Amir Goldstein <amir73il@gmail.com>
Date:   Mon Nov 29 22:15:27 2021 +0200

    fsnotify: clarify object type argument

    In preparation for separating object type from iterator type, rename
    some 'type' arguments in functions to 'obj_type' and remove the unused
    interface to clear marks by object type mask.

    Link: https://lore.kernel.org/r/20211129201537.1932819-2-amir73il@gmail.com
    Signed-off-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi b0068d7f06 fanotify: Allow users to request FAN_FS_ERROR events
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 9709bd548f11a092d124698118013f66e1740f9b
Author: Gabriel Krisman Bertazi <krisman@collabora.com>
Date:   Mon Oct 25 16:27:43 2021 -0300

    fanotify: Allow users to request FAN_FS_ERROR events

    Wire up the FAN_FS_ERROR event in the fanotify_mark syscall, allowing
    user space to request the monitoring of FAN_FS_ERROR events.

    These events are limited to filesystem marks, so check it is the
    case in the syscall handler.

    Link: https://lore.kernel.org/r/20211025192746.66445-29-krisman@collabora.com
    Reviewed-by: Amir Goldstein <amir73il@gmail.com>
    Reviewed-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi b2dbe418dd fanotify: Emit generic error info for error event
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 130a3c742107acff985541c28360c8b40203559c
Author: Gabriel Krisman Bertazi <krisman@collabora.com>
Date:   Mon Oct 25 16:27:42 2021 -0300

    fanotify: Emit generic error info for error event

    The error info is a record sent to users on FAN_FS_ERROR events
    documenting the type of error.  It also carries an error count,
    documenting how many errors were observed since the last reporting.

    Link: https://lore.kernel.org/r/20211025192746.66445-28-krisman@collabora.com
    Reviewed-by: Amir Goldstein <amir73il@gmail.com>
    Reviewed-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi 59a3fe13c6 fanotify: Report fid info for file related file system errors
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 936d6a38be39177495af38497bf8da1c6128fa1b
Author: Gabriel Krisman Bertazi <krisman@collabora.com>
Date:   Mon Oct 25 16:27:41 2021 -0300

    fanotify: Report fid info for file related file system errors

    Plumb the pieces to add a FID report to error records.  Since all error
    event memory must be pre-allocated, we pre-allocate the maximum file
    handle size possible, such that it should always fit.

    For errors that don't expose a file handle, report it with an invalid
    FID. Internally we use zero-length FILEID_ROOT file handle for passing
    the information (which we report as zero-length FILEID_INVALID file
    handle to userspace) so we update the handle reporting code to deal with
    this case correctly.

    Link: https://lore.kernel.org/r/20211025192746.66445-27-krisman@collabora.com
    Link: https://lore.kernel.org/r/20211025192746.66445-25-krisman@collabora.com
    Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
    Reviewed-by: Amir Goldstein <amir73il@gmail.com>
    Reviewed-by: Jan Kara <jack@suse.cz>
    [Folded two patches into 2 to make series bisectable]
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi eccc09c8a8 fanotify: WARN_ON against too large file handles
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 572c28f27a269f88e2d8d7b6b1507f114d637337
Author: Gabriel Krisman Bertazi <krisman@collabora.com>
Date:   Mon Oct 25 16:27:40 2021 -0300

    fanotify: WARN_ON against too large file handles

    struct fanotify_error_event, at least, is preallocated and isn't able to
    to handle arbitrarily large file handles.  Future-proof the code by
    complaining loudly if a handle larger than MAX_HANDLE_SZ is ever found.

    Link: https://lore.kernel.org/r/20211025192746.66445-26-krisman@collabora.com
    Reviewed-by: Amir Goldstein <amir73il@gmail.com>
    Reviewed-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00
Miklos Szeredi 288b30d574 fanotify: Add helpers to decide whether to report FID/DFID
JIRA: https://issues.redhat.com/browse/RHEL-102134

commit 4bd5a5c8e6e5cd964e9738e6ef87f6c2cb453edf
Author: Gabriel Krisman Bertazi <krisman@collabora.com>
Date:   Mon Oct 25 16:27:38 2021 -0300

    fanotify: Add helpers to decide whether to report FID/DFID

    Now that there is an event that reports FID records even for a zeroed
    file handle, wrap the logic that deides whether to issue the records
    into helper functions.  This shouldn't have any impact on the code, but
    simplifies further patches.

    Link: https://lore.kernel.org/r/20211025192746.66445-24-krisman@collabora.com
    Reviewed-by: Jan Kara <jack@suse.cz>
    Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
    Reviewed-by: Amir Goldstein <amir73il@gmail.com>
    Signed-off-by: Jan Kara <jack@suse.cz>

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
2025-07-07 17:32:15 +02:00