^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * fsnotify inode mark locking/lifetime/and refcnting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * REFCNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * The group->recnt and mark->refcnt tell how many "things" in the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * currently are referencing the objects. Both kind of objects typically will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * live inside the kernel with a refcnt of 2, one for its creation and one for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * the reference a group and a mark hold to each other.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * If you are holding the appropriate locks, you can take a reference and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * object itself is guaranteed to survive until the reference is dropped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * LOCKING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * There are 3 locks involved with fsnotify inode marks and they MUST be taken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * in order as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * group->mark_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * mark->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * mark->connector->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * group->mark_mutex protects the marks_list anchored inside a given group and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * each mark is hooked via the g_list. It also protects the groups private
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * data (i.e group limits).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * mark->lock protects the marks attributes like its masks and flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Furthermore it protects the access to a reference of the group that the mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * is assigned to as well as the access to a reference of the inode/vfsmount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * that is being watched by the mark.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * mark->connector->lock protects the list of marks anchored inside an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * inode / vfsmount and each mark is hooked via the i_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * A list of notification marks relating to inode / mnt is contained in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * fsnotify_mark_connector. That structure is alive as long as there are any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * marks in the list and is also protected by fsnotify_mark_srcu. A mark gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * detached from fsnotify_mark_connector when last reference to the mark is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * dropped. Thus having mark reference is enough to protect mark->connector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * pointer and to make sure fsnotify_mark_connector cannot disappear. Also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * because we remove mark from g_list before dropping mark reference associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * with that, any mark found through g_list is guaranteed to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * mark->connector set until we drop group->mark_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * LIFETIME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Inode marks survive between when they are added to an inode and when their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * refcnt==0. Marks are also protected by fsnotify_mark_srcu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The inode mark can be cleared for a number of different reasons including:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * - The inode is unlinked for the last time. (fsnotify_inode_remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * - The inode is being evicted from cache. (fsnotify_inode_delete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * - Something explicitly requests that it be removed. (fsnotify_destroy_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * - The fsnotify_group associated with the mark is going away and all such marks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * need to be cleaned up. (fsnotify_clear_marks_by_group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * This has the very interesting property of being able to run concurrently with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * any (or all) other directions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #include <linux/srcu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #include <linux/fsnotify_backend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #include "fsnotify.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct srcu_struct fsnotify_mark_srcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct kmem_cache *fsnotify_mark_connector_cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static DEFINE_SPINLOCK(destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static LIST_HEAD(destroy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static struct fsnotify_mark_connector *connector_destroy_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static void fsnotify_mark_destroy_workfn(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static void fsnotify_connector_destroy_workfn(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) static DECLARE_WORK(connector_reaper_work, fsnotify_connector_destroy_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) void fsnotify_get_mark(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) WARN_ON_ONCE(!refcount_read(&mark->refcnt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) refcount_inc(&mark->refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static __u32 *fsnotify_conn_mask_p(struct fsnotify_mark_connector *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return &fsnotify_conn_inode(conn)->i_fsnotify_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return &fsnotify_conn_mount(conn)->mnt_fsnotify_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) else if (conn->type == FSNOTIFY_OBJ_TYPE_SB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return &fsnotify_conn_sb(conn)->s_fsnotify_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (WARN_ON(!fsnotify_valid_obj_type(conn->type)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return *fsnotify_conn_mask_p(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) u32 new_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct fsnotify_mark *mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) assert_spin_locked(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* We can get detached connector here when inode is getting unlinked. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (!fsnotify_valid_obj_type(conn->type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) hlist_for_each_entry(mark, &conn->list, obj_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) new_mask |= mark->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) *fsnotify_conn_mask_p(conn) = new_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * Calculate mask of events for a list of marks. The caller must make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * connector and connector->obj cannot disappear under us. Callers achieve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * this by holding a mark->lock or mark->group->mark_mutex for a mark on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) spin_lock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) __fsnotify_recalc_mask(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) __fsnotify_update_child_dentry_flags(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) fsnotify_conn_inode(conn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* Free all connectors queued for freeing once SRCU period ends */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void fsnotify_connector_destroy_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct fsnotify_mark_connector *conn, *free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) spin_lock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) conn = connector_destroy_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) connector_destroy_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) spin_unlock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) synchronize_srcu(&fsnotify_mark_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) while (conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) free = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) conn = conn->destroy_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) kmem_cache_free(fsnotify_mark_connector_cachep, free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void *fsnotify_detach_connector_from_object(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct fsnotify_mark_connector *conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) unsigned int *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *type = conn->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (conn->type == FSNOTIFY_OBJ_TYPE_INODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) inode = fsnotify_conn_inode(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) inode->i_fsnotify_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) atomic_long_inc(&inode->i_sb->s_fsnotify_inode_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) } else if (conn->type == FSNOTIFY_OBJ_TYPE_VFSMOUNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) fsnotify_conn_mount(conn)->mnt_fsnotify_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) } else if (conn->type == FSNOTIFY_OBJ_TYPE_SB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fsnotify_conn_sb(conn)->s_fsnotify_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) rcu_assign_pointer(*(conn->obj), NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) conn->obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) conn->type = FSNOTIFY_OBJ_TYPE_DETACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static void fsnotify_final_mark_destroy(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct fsnotify_group *group = mark->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (WARN_ON_ONCE(!group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) group->ops->free_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) fsnotify_put_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* Drop object reference originally held by a connector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static void fsnotify_drop_object(unsigned int type, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (!objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* Currently only inode references are passed to be dropped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) if (WARN_ON_ONCE(type != FSNOTIFY_OBJ_TYPE_INODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) inode = objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (atomic_long_dec_and_test(&sb->s_fsnotify_inode_refs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) wake_up_var(&sb->s_fsnotify_inode_refs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) void fsnotify_put_mark(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) struct fsnotify_mark_connector *conn = READ_ONCE(mark->connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) void *objp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned int type = FSNOTIFY_OBJ_TYPE_DETACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) bool free_conn = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* Catch marks that were actually never attached to object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (!conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (refcount_dec_and_test(&mark->refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) fsnotify_final_mark_destroy(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * We have to be careful so that traversals of obj_list under lock can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * safely grab mark reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!refcount_dec_and_lock(&mark->refcnt, &conn->lock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) hlist_del_init_rcu(&mark->obj_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (hlist_empty(&conn->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) objp = fsnotify_detach_connector_from_object(conn, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) free_conn = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) __fsnotify_recalc_mask(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) WRITE_ONCE(mark->connector, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) fsnotify_drop_object(type, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (free_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) spin_lock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) conn->destroy_next = connector_destroy_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) connector_destroy_list = conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) spin_unlock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) queue_work(system_unbound_wq, &connector_reaper_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * Note that we didn't update flags telling whether inode cares about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * what's happening with children. We update these flags from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * __fsnotify_parent() lazily when next event happens on one of our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * children.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) spin_lock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) list_add(&mark->g_list, &destroy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) spin_unlock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) queue_delayed_work(system_unbound_wq, &reaper_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) FSNOTIFY_REAPER_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) EXPORT_SYMBOL_GPL(fsnotify_put_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * Get mark reference when we found the mark via lockless traversal of object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * list. Mark can be already removed from the list by now and on its way to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * destroyed once SRCU period ends.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * Also pin the group so it doesn't disappear under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static bool fsnotify_get_mark_safe(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (!mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (refcount_inc_not_zero(&mark->refcnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /* mark is attached, group is still alive then */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) atomic_inc(&mark->group->user_waits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) fsnotify_put_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * Puts marks and wakes up group destruction if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * Pairs with fsnotify_get_mark_safe()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static void fsnotify_put_mark_wake(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (mark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct fsnotify_group *group = mark->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) fsnotify_put_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * We abuse notification_waitq on group shutdown for waiting for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * all marks pinned when waiting for userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (atomic_dec_and_test(&group->user_waits) && group->shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) wake_up(&group->notification_waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) __releases(&fsnotify_mark_srcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) fsnotify_foreach_obj_type(type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* This can fail if mark is being removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (!fsnotify_get_mark_safe(iter_info->marks[type])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) __release(&fsnotify_mark_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * Now that both marks are pinned by refcount in the inode / vfsmount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * lists, we can drop SRCU lock, and safely resume the list iteration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * once userspace returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) srcu_read_unlock(&fsnotify_mark_srcu, iter_info->srcu_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) for (type--; type >= 0; type--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) fsnotify_put_mark_wake(iter_info->marks[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) __acquires(&fsnotify_mark_srcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) iter_info->srcu_idx = srcu_read_lock(&fsnotify_mark_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) fsnotify_foreach_obj_type(type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) fsnotify_put_mark_wake(iter_info->marks[type]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * Mark mark as detached, remove it from group list. Mark still stays in object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * list until its last reference is dropped. Note that we rely on mark being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * removed from group list before corresponding reference to it is dropped. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * particular we rely on mark->connector being valid while we hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * group->mark_mutex if we found the mark through g_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * Must be called with group->mark_mutex held. The caller must either hold
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * reference to the mark or be protected by fsnotify_mark_srcu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) void fsnotify_detach_mark(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct fsnotify_group *group = mark->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) WARN_ON_ONCE(!srcu_read_lock_held(&fsnotify_mark_srcu) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) refcount_read(&mark->refcnt) < 1 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) !!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) spin_lock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* something else already called this function on this mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) list_del_init(&mark->g_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) atomic_dec(&group->num_marks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* Drop mark reference acquired in fsnotify_add_mark_locked() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) fsnotify_put_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * Free fsnotify mark. The mark is actually only marked as being freed. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * freeing is actually happening only once last reference to the mark is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * dropped from a workqueue which first waits for srcu period end.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * Caller must have a reference to the mark or be protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * fsnotify_mark_srcu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) void fsnotify_free_mark(struct fsnotify_mark *mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct fsnotify_group *group = mark->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) spin_lock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /* something else already called this function on this mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * Some groups like to know that marks are being freed. This is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * callback to the group function to let it know that this mark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * is being freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (group->ops->freeing_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) group->ops->freeing_mark(mark, group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) void fsnotify_destroy_mark(struct fsnotify_mark *mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) struct fsnotify_group *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) fsnotify_detach_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) fsnotify_free_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * Sorting function for lists of fsnotify marks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) * Fanotify supports different notification classes (reflected as priority of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) * notification group). Events shall be passed to notification groups in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * decreasing priority order. To achieve this marks in notification lists for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * inodes and vfsmounts are sorted so that priorities of corresponding groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * are descending.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * Furthermore correct handling of the ignore mask requires processing inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * and vfsmount marks of each group together. Using the group address as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * further sort criterion provides a unique sorting order and thus we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * merge inode and vfsmount lists of marks in linear time and find groups
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * present in both lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * A return value of 1 signifies that b has priority over a.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * A return value of 0 signifies that the two marks have to be handled together.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * A return value of -1 signifies that a has priority over b.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (a == b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (!a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (!b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (a->priority < b->priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (a->priority > b->priority)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if (a < b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) static int fsnotify_attach_connector_to_object(fsnotify_connp_t *connp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) struct fsnotify_mark_connector *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) spin_lock_init(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) INIT_HLIST_HEAD(&conn->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) conn->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) conn->obj = connp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) /* Cache fsid of filesystem containing the object */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (fsid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) conn->fsid = *fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) conn->flags = FSNOTIFY_CONN_FLAG_HAS_FSID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) conn->fsid.val[0] = conn->fsid.val[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) conn->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (conn->type == FSNOTIFY_OBJ_TYPE_INODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) inode = igrab(fsnotify_conn_inode(conn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * cmpxchg() provides the barrier so that readers of *connp can see
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * only initialized structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (cmpxchg(connp, NULL, conn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) /* Someone else created list structure for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) kmem_cache_free(fsnotify_mark_connector_cachep, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * Get mark connector, make sure it is alive and return with its lock held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) * This is for users that get connector pointer from inode or mount. Users that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * hold reference to a mark on the list may directly lock connector->lock as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * they are sure list cannot go away under them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) static struct fsnotify_mark_connector *fsnotify_grab_connector(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) fsnotify_connp_t *connp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct fsnotify_mark_connector *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) idx = srcu_read_lock(&fsnotify_mark_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) conn = srcu_dereference(*connp, &fsnotify_mark_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) spin_lock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) if (conn->type == FSNOTIFY_OBJ_TYPE_DETACHED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) srcu_read_unlock(&fsnotify_mark_srcu, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) srcu_read_unlock(&fsnotify_mark_srcu, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * Add mark into proper place in given list of marks. These marks may be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * for the fsnotify backend to determine which event types should be delivered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * to which group and for which inodes. These marks are ordered according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * priority, highest number first, and then by the group's location in memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) static int fsnotify_add_mark_list(struct fsnotify_mark *mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) fsnotify_connp_t *connp, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) int allow_dups, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) struct fsnotify_mark *lmark, *last = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) struct fsnotify_mark_connector *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (WARN_ON(!fsnotify_valid_obj_type(type)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /* Backend is expected to check for zero fsid (e.g. tmpfs) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) if (fsid && WARN_ON_ONCE(!fsid->val[0] && !fsid->val[1]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) spin_lock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) conn = fsnotify_grab_connector(connp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (!conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) err = fsnotify_attach_connector_to_object(connp, type, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) } else if (fsid && !(conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) conn->fsid = *fsid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) /* Pairs with smp_rmb() in fanotify_get_fsid() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) conn->flags |= FSNOTIFY_CONN_FLAG_HAS_FSID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) } else if (fsid && (conn->flags & FSNOTIFY_CONN_FLAG_HAS_FSID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) (fsid->val[0] != conn->fsid.val[0] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) fsid->val[1] != conn->fsid.val[1])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) * Backend is expected to check for non uniform fsid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * (e.g. btrfs), but maybe we missed something?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * Only allow setting conn->fsid once to non zero fsid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * inotify and non-fid fanotify groups do not set nor test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * conn->fsid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) pr_warn_ratelimited("%s: fsid mismatch on object of type %u: "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) "%x.%x != %x.%x\n", __func__, conn->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) fsid->val[0], fsid->val[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) conn->fsid.val[0], conn->fsid.val[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) err = -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* is mark the first mark? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (hlist_empty(&conn->list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) hlist_add_head_rcu(&mark->obj_list, &conn->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) goto added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /* should mark be in the middle of the current list? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) hlist_for_each_entry(lmark, &conn->list, obj_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) last = lmark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if ((lmark->group == mark->group) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) (lmark->flags & FSNOTIFY_MARK_FLAG_ATTACHED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) !allow_dups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) err = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) cmp = fsnotify_compare_groups(lmark->group, mark->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (cmp >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) goto added;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) BUG_ON(last == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /* mark should be the last entry. last is the current last entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) hlist_add_behind_rcu(&mark->obj_list, &last->obj_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) added:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * Since connector is attached to object using cmpxchg() we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * guaranteed that connector initialization is fully visible by anyone
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * seeing mark->connector set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) WRITE_ONCE(mark->connector, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * Attach an initialized mark to a given group and fs object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * These marks may be used for the fsnotify backend to determine which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * event types should be delivered to which group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) fsnotify_connp_t *connp, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) int allow_dups, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct fsnotify_group *group = mark->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) BUG_ON(!mutex_is_locked(&group->mark_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) * LOCKING ORDER!!!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) * group->mark_mutex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) * mark->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) * mark->connector->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) spin_lock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) list_add(&mark->g_list, &group->marks_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) atomic_inc(&group->num_marks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) fsnotify_get_mark(mark); /* for g_list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) ret = fsnotify_add_mark_list(mark, connp, type, allow_dups, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (mark->mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) fsnotify_recalc_mask(mark->connector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) spin_lock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) mark->flags &= ~(FSNOTIFY_MARK_FLAG_ALIVE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) FSNOTIFY_MARK_FLAG_ATTACHED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) list_del_init(&mark->g_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) spin_unlock(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) atomic_dec(&group->num_marks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) fsnotify_put_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) int fsnotify_add_mark(struct fsnotify_mark *mark, fsnotify_connp_t *connp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) unsigned int type, int allow_dups, __kernel_fsid_t *fsid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) struct fsnotify_group *group = mark->group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) mutex_lock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) ret = fsnotify_add_mark_locked(mark, connp, type, allow_dups, fsid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) EXPORT_SYMBOL_GPL(fsnotify_add_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) * Given a list of marks, find the mark associated with given group. If found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) * take a reference to that mark and return it, else return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) struct fsnotify_group *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) struct fsnotify_mark_connector *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) struct fsnotify_mark *mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) conn = fsnotify_grab_connector(connp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) hlist_for_each_entry(mark, &conn->list, obj_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (mark->group == group &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) (mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) fsnotify_get_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) return mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) EXPORT_SYMBOL_GPL(fsnotify_find_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) /* Clear any marks in a group with given type mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) unsigned int type_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) struct fsnotify_mark *lmark, *mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) LIST_HEAD(to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) struct list_head *head = &to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) /* Skip selection step if we want to clear all marks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (type_mask == FSNOTIFY_OBJ_ALL_TYPES_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) head = &group->marks_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) goto clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * We have to be really careful here. Anytime we drop mark_mutex, e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * fsnotify_clear_marks_by_inode() can come and free marks. Even in our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) * to_free list so we have to use mark_mutex even when accessing that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) * list. And freeing mark requires us to drop mark_mutex. So we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) * reliably free only the first mark in the list. That's why we first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * move marks to free to to_free list in one go and then free marks in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * to_free list one by one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if ((1U << mark->connector->type) & type_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) list_move(&mark->g_list, &to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) clear:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (list_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) mark = list_first_entry(head, struct fsnotify_mark, g_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) fsnotify_get_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) fsnotify_detach_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) mutex_unlock(&group->mark_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) fsnotify_free_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) fsnotify_put_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) /* Destroy all marks attached to an object via connector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) void fsnotify_destroy_marks(fsnotify_connp_t *connp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct fsnotify_mark_connector *conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct fsnotify_mark *mark, *old_mark = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) conn = fsnotify_grab_connector(connp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (!conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) * We have to be careful since we can race with e.g.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) * fsnotify_clear_marks_by_group() and once we drop the conn->lock, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) * list can get modified. However we are holding mark reference and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) * thus our mark cannot be removed from obj_list so we can continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * iteration after regaining conn->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) hlist_for_each_entry(mark, &conn->list, obj_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) fsnotify_get_mark(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) if (old_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) fsnotify_put_mark(old_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) old_mark = mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) fsnotify_destroy_mark(mark, mark->group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) spin_lock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) * Detach list from object now so that we don't pin inode until all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) * mark references get dropped. It would lead to strange results such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) * as delaying inode deletion or blocking unmount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) objp = fsnotify_detach_connector_from_object(conn, &type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) spin_unlock(&conn->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (old_mark)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) fsnotify_put_mark(old_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) fsnotify_drop_object(type, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) * Nothing fancy, just initialize lists and locks and counters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) void fsnotify_init_mark(struct fsnotify_mark *mark,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) struct fsnotify_group *group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) memset(mark, 0, sizeof(*mark));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) spin_lock_init(&mark->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) refcount_set(&mark->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) fsnotify_get_group(group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) mark->group = group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) WRITE_ONCE(mark->connector, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) EXPORT_SYMBOL_GPL(fsnotify_init_mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * Destroy all marks in destroy_list, waits for SRCU period to finish before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * actually freeing marks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) static void fsnotify_mark_destroy_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct fsnotify_mark *mark, *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct list_head private_destroy_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) spin_lock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) /* exchange the list head */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) list_replace_init(&destroy_list, &private_destroy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) spin_unlock(&destroy_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) synchronize_srcu(&fsnotify_mark_srcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) list_del_init(&mark->g_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) fsnotify_final_mark_destroy(mark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) /* Wait for all marks queued for destruction to be actually destroyed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) void fsnotify_wait_marks_destroyed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) flush_delayed_work(&reaper_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) EXPORT_SYMBOL_GPL(fsnotify_wait_marks_destroyed);