notification.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2, or (at your option)
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; see the file COPYING. If not, write to
  16. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /*
  19. * Basic idea behind the notification queue: An fsnotify group (like inotify)
  20. * sends the userspace notification about events asyncronously some time after
  21. * the event happened. When inotify gets an event it will need to add that
  22. * event to the group notify queue. Since a single event might need to be on
  23. * multiple group's notification queues we can't add the event directly to each
  24. * queue and instead add a small "event_holder" to each queue. This event_holder
  25. * has a pointer back to the original event. Since the majority of events are
  26. * going to end up on one, and only one, notification queue we embed one
  27. * event_holder into each event. This means we have a single allocation instead
  28. * of always needing two. If the embedded event_holder is already in use by
  29. * another group a new event_holder (from fsnotify_event_holder_cachep) will be
  30. * allocated and used.
  31. */
  32. #include <linux/fs.h>
  33. #include <linux/init.h>
  34. #include <linux/kernel.h>
  35. #include <linux/list.h>
  36. #include <linux/mount.h>
  37. #include <linux/mutex.h>
  38. #include <linux/namei.h>
  39. #include <linux/path.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <asm/atomic.h>
  43. #include <linux/fsnotify_backend.h>
  44. #include "fsnotify.h"
  45. static struct kmem_cache *fsnotify_event_cachep;
  46. static struct kmem_cache *fsnotify_event_holder_cachep;
  47. /*
  48. * This is a magic event we send when the q is too full. Since it doesn't
  49. * hold real event information we just keep one system wide and use it any time
  50. * it is needed. It's refcnt is set 1 at kernel init time and will never
  51. * get set to 0 so it will never get 'freed'
  52. */
  53. static struct fsnotify_event q_overflow_event;
  54. /* return true if the notify queue is empty, false otherwise */
  55. bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
  56. {
  57. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  58. return list_empty(&group->notification_list) ? true : false;
  59. }
  60. void fsnotify_get_event(struct fsnotify_event *event)
  61. {
  62. atomic_inc(&event->refcnt);
  63. }
  64. void fsnotify_put_event(struct fsnotify_event *event)
  65. {
  66. if (!event)
  67. return;
  68. if (atomic_dec_and_test(&event->refcnt)) {
  69. if (event->data_type == FSNOTIFY_EVENT_PATH)
  70. path_put(&event->path);
  71. kmem_cache_free(fsnotify_event_cachep, event);
  72. }
  73. }
  74. struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
  75. {
  76. return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
  77. }
  78. void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
  79. {
  80. kmem_cache_free(fsnotify_event_holder_cachep, holder);
  81. }
  82. /*
  83. * check if 2 events contain the same information.
  84. */
  85. static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
  86. {
  87. if ((old->mask == new->mask) &&
  88. (old->to_tell == new->to_tell) &&
  89. (old->data_type == new->data_type)) {
  90. switch (old->data_type) {
  91. case (FSNOTIFY_EVENT_INODE):
  92. if (old->inode == new->inode)
  93. return true;
  94. break;
  95. case (FSNOTIFY_EVENT_PATH):
  96. if ((old->path.mnt == new->path.mnt) &&
  97. (old->path.dentry == new->path.dentry))
  98. return true;
  99. case (FSNOTIFY_EVENT_NONE):
  100. return true;
  101. };
  102. }
  103. return false;
  104. }
  105. /*
  106. * Add an event to the group notification queue. The group can later pull this
  107. * event off the queue to deal with. If the event is successfully added to the
  108. * group's notification queue, a reference is taken on event.
  109. */
  110. int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event)
  111. {
  112. struct fsnotify_event_holder *holder = NULL;
  113. struct list_head *list = &group->notification_list;
  114. struct fsnotify_event_holder *last_holder;
  115. struct fsnotify_event *last_event;
  116. /*
  117. * There is one fsnotify_event_holder embedded inside each fsnotify_event.
  118. * Check if we expect to be able to use that holder. If not alloc a new
  119. * holder.
  120. * For the overflow event it's possible that something will use the in
  121. * event holder before we get the lock so we may need to jump back and
  122. * alloc a new holder, this can't happen for most events...
  123. */
  124. if (!list_empty(&event->holder.event_list)) {
  125. alloc_holder:
  126. holder = fsnotify_alloc_event_holder();
  127. if (!holder)
  128. return -ENOMEM;
  129. }
  130. mutex_lock(&group->notification_mutex);
  131. if (group->q_len >= group->max_events)
  132. event = &q_overflow_event;
  133. spin_lock(&event->lock);
  134. if (list_empty(&event->holder.event_list)) {
  135. if (unlikely(holder))
  136. fsnotify_destroy_event_holder(holder);
  137. holder = &event->holder;
  138. } else if (unlikely(!holder)) {
  139. /* between the time we checked above and got the lock the in
  140. * event holder was used, go back and get a new one */
  141. spin_unlock(&event->lock);
  142. mutex_unlock(&group->notification_mutex);
  143. goto alloc_holder;
  144. }
  145. if (!list_empty(list)) {
  146. last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
  147. last_event = last_holder->event;
  148. if (event_compare(last_event, event)) {
  149. spin_unlock(&event->lock);
  150. mutex_unlock(&group->notification_mutex);
  151. if (holder != &event->holder)
  152. fsnotify_destroy_event_holder(holder);
  153. return 0;
  154. }
  155. }
  156. group->q_len++;
  157. holder->event = event;
  158. fsnotify_get_event(event);
  159. list_add_tail(&holder->event_list, list);
  160. spin_unlock(&event->lock);
  161. mutex_unlock(&group->notification_mutex);
  162. wake_up(&group->notification_waitq);
  163. return 0;
  164. }
  165. /*
  166. * Remove and return the first event from the notification list. There is a
  167. * reference held on this event since it was on the list. It is the responsibility
  168. * of the caller to drop this reference.
  169. */
  170. struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
  171. {
  172. struct fsnotify_event *event;
  173. struct fsnotify_event_holder *holder;
  174. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  175. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  176. event = holder->event;
  177. spin_lock(&event->lock);
  178. holder->event = NULL;
  179. list_del_init(&holder->event_list);
  180. spin_unlock(&event->lock);
  181. /* event == holder means we are referenced through the in event holder */
  182. if (holder != &event->holder)
  183. fsnotify_destroy_event_holder(holder);
  184. group->q_len--;
  185. return event;
  186. }
  187. /*
  188. * This will not remove the event, that must be done with fsnotify_remove_notify_event()
  189. */
  190. struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
  191. {
  192. struct fsnotify_event *event;
  193. struct fsnotify_event_holder *holder;
  194. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  195. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  196. event = holder->event;
  197. return event;
  198. }
  199. /*
  200. * Called when a group is being torn down to clean up any outstanding
  201. * event notifications.
  202. */
  203. void fsnotify_flush_notify(struct fsnotify_group *group)
  204. {
  205. struct fsnotify_event *event;
  206. mutex_lock(&group->notification_mutex);
  207. while (!fsnotify_notify_queue_is_empty(group)) {
  208. event = fsnotify_remove_notify_event(group);
  209. fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
  210. }
  211. mutex_unlock(&group->notification_mutex);
  212. }
  213. static void initialize_event(struct fsnotify_event *event)
  214. {
  215. event->holder.event = NULL;
  216. INIT_LIST_HEAD(&event->holder.event_list);
  217. atomic_set(&event->refcnt, 1);
  218. spin_lock_init(&event->lock);
  219. event->path.dentry = NULL;
  220. event->path.mnt = NULL;
  221. event->inode = NULL;
  222. event->data_type = FSNOTIFY_EVENT_NONE;
  223. event->to_tell = NULL;
  224. }
  225. /*
  226. * fsnotify_create_event - Allocate a new event which will be sent to each
  227. * group's handle_event function if the group was interested in this
  228. * particular event.
  229. *
  230. * @to_tell the inode which is supposed to receive the event (sometimes a
  231. * parent of the inode to which the event happened.
  232. * @mask what actually happened.
  233. * @data pointer to the object which was actually affected
  234. * @data_type flag indication if the data is a file, path, inode, nothing...
  235. */
  236. struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
  237. void *data, int data_type)
  238. {
  239. struct fsnotify_event *event;
  240. event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
  241. if (!event)
  242. return NULL;
  243. initialize_event(event);
  244. event->to_tell = to_tell;
  245. switch (data_type) {
  246. case FSNOTIFY_EVENT_FILE: {
  247. struct file *file = data;
  248. struct path *path = &file->f_path;
  249. event->path.dentry = path->dentry;
  250. event->path.mnt = path->mnt;
  251. path_get(&event->path);
  252. event->data_type = FSNOTIFY_EVENT_PATH;
  253. break;
  254. }
  255. case FSNOTIFY_EVENT_PATH: {
  256. struct path *path = data;
  257. event->path.dentry = path->dentry;
  258. event->path.mnt = path->mnt;
  259. path_get(&event->path);
  260. event->data_type = FSNOTIFY_EVENT_PATH;
  261. break;
  262. }
  263. case FSNOTIFY_EVENT_INODE:
  264. event->inode = data;
  265. event->data_type = FSNOTIFY_EVENT_INODE;
  266. break;
  267. case FSNOTIFY_EVENT_NONE:
  268. event->inode = NULL;
  269. event->path.dentry = NULL;
  270. event->path.mnt = NULL;
  271. break;
  272. default:
  273. BUG();
  274. }
  275. event->mask = mask;
  276. return event;
  277. }
  278. __init int fsnotify_notification_init(void)
  279. {
  280. fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
  281. fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
  282. initialize_event(&q_overflow_event);
  283. q_overflow_event.mask = FS_Q_OVERFLOW;
  284. return 0;
  285. }
  286. subsys_initcall(fsnotify_notification_init);