Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
0004  */
0005 
0006 #include <linux/list.h>
0007 #include <linux/mutex.h>
0008 #include <linux/slab.h>
0009 #include <linux/srcu.h>
0010 #include <linux/rculist.h>
0011 #include <linux/wait.h>
0012 #include <linux/memcontrol.h>
0013 
0014 #include <linux/fsnotify_backend.h>
0015 #include "fsnotify.h"
0016 
0017 #include <linux/atomic.h>
0018 
0019 /*
0020  * Final freeing of a group
0021  */
0022 static void fsnotify_final_destroy_group(struct fsnotify_group *group)
0023 {
0024     if (group->ops->free_group_priv)
0025         group->ops->free_group_priv(group);
0026 
0027     mem_cgroup_put(group->memcg);
0028     mutex_destroy(&group->mark_mutex);
0029 
0030     kfree(group);
0031 }
0032 
0033 /*
0034  * Stop queueing new events for this group. Once this function returns
0035  * fsnotify_add_event() will not add any new events to the group's queue.
0036  */
0037 void fsnotify_group_stop_queueing(struct fsnotify_group *group)
0038 {
0039     spin_lock(&group->notification_lock);
0040     group->shutdown = true;
0041     spin_unlock(&group->notification_lock);
0042 }
0043 
0044 /*
0045  * Trying to get rid of a group. Remove all marks, flush all events and release
0046  * the group reference.
0047  * Note that another thread calling fsnotify_clear_marks_by_group() may still
0048  * hold a ref to the group.
0049  */
0050 void fsnotify_destroy_group(struct fsnotify_group *group)
0051 {
0052     /*
0053      * Stop queueing new events. The code below is careful enough to not
0054      * require this but fanotify needs to stop queuing events even before
0055      * fsnotify_destroy_group() is called and this makes the other callers
0056      * of fsnotify_destroy_group() to see the same behavior.
0057      */
0058     fsnotify_group_stop_queueing(group);
0059 
0060     /* Clear all marks for this group and queue them for destruction */
0061     fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_ANY);
0062 
0063     /*
0064      * Some marks can still be pinned when waiting for response from
0065      * userspace. Wait for those now. fsnotify_prepare_user_wait() will
0066      * not succeed now so this wait is race-free.
0067      */
0068     wait_event(group->notification_waitq, !atomic_read(&group->user_waits));
0069 
0070     /*
0071      * Wait until all marks get really destroyed. We could actually destroy
0072      * them ourselves instead of waiting for worker to do it, however that
0073      * would be racy as worker can already be processing some marks before
0074      * we even entered fsnotify_destroy_group().
0075      */
0076     fsnotify_wait_marks_destroyed();
0077 
0078     /*
0079      * Since we have waited for fsnotify_mark_srcu in
0080      * fsnotify_mark_destroy_list() there can be no outstanding event
0081      * notification against this group. So clearing the notification queue
0082      * of all events is reliable now.
0083      */
0084     fsnotify_flush_notify(group);
0085 
0086     /*
0087      * Destroy overflow event (we cannot use fsnotify_destroy_event() as
0088      * that deliberately ignores overflow events.
0089      */
0090     if (group->overflow_event)
0091         group->ops->free_event(group, group->overflow_event);
0092 
0093     fsnotify_put_group(group);
0094 }
0095 
0096 /*
0097  * Get reference to a group.
0098  */
0099 void fsnotify_get_group(struct fsnotify_group *group)
0100 {
0101     refcount_inc(&group->refcnt);
0102 }
0103 
0104 /*
0105  * Drop a reference to a group.  Free it if it's through.
0106  */
0107 void fsnotify_put_group(struct fsnotify_group *group)
0108 {
0109     if (refcount_dec_and_test(&group->refcnt))
0110         fsnotify_final_destroy_group(group);
0111 }
0112 EXPORT_SYMBOL_GPL(fsnotify_put_group);
0113 
0114 static struct fsnotify_group *__fsnotify_alloc_group(
0115                 const struct fsnotify_ops *ops,
0116                 int flags, gfp_t gfp)
0117 {
0118     static struct lock_class_key nofs_marks_lock;
0119     struct fsnotify_group *group;
0120 
0121     group = kzalloc(sizeof(struct fsnotify_group), gfp);
0122     if (!group)
0123         return ERR_PTR(-ENOMEM);
0124 
0125     /* set to 0 when there a no external references to this group */
0126     refcount_set(&group->refcnt, 1);
0127     atomic_set(&group->user_waits, 0);
0128 
0129     spin_lock_init(&group->notification_lock);
0130     INIT_LIST_HEAD(&group->notification_list);
0131     init_waitqueue_head(&group->notification_waitq);
0132     group->max_events = UINT_MAX;
0133 
0134     mutex_init(&group->mark_mutex);
0135     INIT_LIST_HEAD(&group->marks_list);
0136 
0137     group->ops = ops;
0138     group->flags = flags;
0139     /*
0140      * For most backends, eviction of inode with a mark is not expected,
0141      * because marks hold a refcount on the inode against eviction.
0142      *
0143      * Use a different lockdep class for groups that support evictable
0144      * inode marks, because with evictable marks, mark_mutex is NOT
0145      * fs-reclaim safe - the mutex is taken when evicting inodes.
0146      */
0147     if (flags & FSNOTIFY_GROUP_NOFS)
0148         lockdep_set_class(&group->mark_mutex, &nofs_marks_lock);
0149 
0150     return group;
0151 }
0152 
0153 /*
0154  * Create a new fsnotify_group and hold a reference for the group returned.
0155  */
0156 struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops,
0157                         int flags)
0158 {
0159     gfp_t gfp = (flags & FSNOTIFY_GROUP_USER) ? GFP_KERNEL_ACCOUNT :
0160                             GFP_KERNEL;
0161 
0162     return __fsnotify_alloc_group(ops, flags, gfp);
0163 }
0164 EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
0165 
0166 int fsnotify_fasync(int fd, struct file *file, int on)
0167 {
0168     struct fsnotify_group *group = file->private_data;
0169 
0170     return fasync_helper(fd, file, on, &group->fsn_fa) >= 0 ? 0 : -EIO;
0171 }