0001
0002
0003
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
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
0035
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
0046
0047
0048
0049
0050 void fsnotify_destroy_group(struct fsnotify_group *group)
0051 {
0052
0053
0054
0055
0056
0057
0058 fsnotify_group_stop_queueing(group);
0059
0060
0061 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_ANY);
0062
0063
0064
0065
0066
0067
0068 wait_event(group->notification_waitq, !atomic_read(&group->user_waits));
0069
0070
0071
0072
0073
0074
0075
0076 fsnotify_wait_marks_destroyed();
0077
0078
0079
0080
0081
0082
0083
0084 fsnotify_flush_notify(group);
0085
0086
0087
0088
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
0098
0099 void fsnotify_get_group(struct fsnotify_group *group)
0100 {
0101 refcount_inc(&group->refcnt);
0102 }
0103
0104
0105
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
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
0141
0142
0143
0144
0145
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
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 }