notification.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. kfree(event->file_name);
  72. kmem_cache_free(fsnotify_event_cachep, event);
  73. }
  74. }
  75. struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
  76. {
  77. return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
  78. }
  79. void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
  80. {
  81. kmem_cache_free(fsnotify_event_holder_cachep, holder);
  82. }
  83. /*
  84. * check if 2 events contain the same information.
  85. */
  86. static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
  87. {
  88. if ((old->mask == new->mask) &&
  89. (old->to_tell == new->to_tell) &&
  90. (old->data_type == new->data_type)) {
  91. switch (old->data_type) {
  92. case (FSNOTIFY_EVENT_INODE):
  93. if (old->inode == new->inode)
  94. return true;
  95. break;
  96. case (FSNOTIFY_EVENT_PATH):
  97. if ((old->path.mnt == new->path.mnt) &&
  98. (old->path.dentry == new->path.dentry))
  99. return true;
  100. case (FSNOTIFY_EVENT_NONE):
  101. return true;
  102. };
  103. }
  104. return false;
  105. }
  106. /*
  107. * Add an event to the group notification queue. The group can later pull this
  108. * event off the queue to deal with. If the event is successfully added to the
  109. * group's notification queue, a reference is taken on event.
  110. */
  111. int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event)
  112. {
  113. struct fsnotify_event_holder *holder = NULL;
  114. struct list_head *list = &group->notification_list;
  115. struct fsnotify_event_holder *last_holder;
  116. struct fsnotify_event *last_event;
  117. /*
  118. * There is one fsnotify_event_holder embedded inside each fsnotify_event.
  119. * Check if we expect to be able to use that holder. If not alloc a new
  120. * holder.
  121. * For the overflow event it's possible that something will use the in
  122. * event holder before we get the lock so we may need to jump back and
  123. * alloc a new holder, this can't happen for most events...
  124. */
  125. if (!list_empty(&event->holder.event_list)) {
  126. alloc_holder:
  127. holder = fsnotify_alloc_event_holder();
  128. if (!holder)
  129. return -ENOMEM;
  130. }
  131. mutex_lock(&group->notification_mutex);
  132. if (group->q_len >= group->max_events)
  133. event = &q_overflow_event;
  134. spin_lock(&event->lock);
  135. if (list_empty(&event->holder.event_list)) {
  136. if (unlikely(holder))
  137. fsnotify_destroy_event_holder(holder);
  138. holder = &event->holder;
  139. } else if (unlikely(!holder)) {
  140. /* between the time we checked above and got the lock the in
  141. * event holder was used, go back and get a new one */
  142. spin_unlock(&event->lock);
  143. mutex_unlock(&group->notification_mutex);
  144. goto alloc_holder;
  145. }
  146. if (!list_empty(list)) {
  147. last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
  148. last_event = last_holder->event;
  149. if (event_compare(last_event, event)) {
  150. spin_unlock(&event->lock);
  151. mutex_unlock(&group->notification_mutex);
  152. if (holder != &event->holder)
  153. fsnotify_destroy_event_holder(holder);
  154. return 0;
  155. }
  156. }
  157. group->q_len++;
  158. holder->event = event;
  159. fsnotify_get_event(event);
  160. list_add_tail(&holder->event_list, list);
  161. spin_unlock(&event->lock);
  162. mutex_unlock(&group->notification_mutex);
  163. wake_up(&group->notification_waitq);
  164. return 0;
  165. }
  166. /*
  167. * Remove and return the first event from the notification list. There is a
  168. * reference held on this event since it was on the list. It is the responsibility
  169. * of the caller to drop this reference.
  170. */
  171. struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
  172. {
  173. struct fsnotify_event *event;
  174. struct fsnotify_event_holder *holder;
  175. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  176. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  177. event = holder->event;
  178. spin_lock(&event->lock);
  179. holder->event = NULL;
  180. list_del_init(&holder->event_list);
  181. spin_unlock(&event->lock);
  182. /* event == holder means we are referenced through the in event holder */
  183. if (holder != &event->holder)
  184. fsnotify_destroy_event_holder(holder);
  185. group->q_len--;
  186. return event;
  187. }
  188. /*
  189. * This will not remove the event, that must be done with fsnotify_remove_notify_event()
  190. */
  191. struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
  192. {
  193. struct fsnotify_event *event;
  194. struct fsnotify_event_holder *holder;
  195. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  196. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  197. event = holder->event;
  198. return event;
  199. }
  200. /*
  201. * Called when a group is being torn down to clean up any outstanding
  202. * event notifications.
  203. */
  204. void fsnotify_flush_notify(struct fsnotify_group *group)
  205. {
  206. struct fsnotify_event *event;
  207. mutex_lock(&group->notification_mutex);
  208. while (!fsnotify_notify_queue_is_empty(group)) {
  209. event = fsnotify_remove_notify_event(group);
  210. fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
  211. }
  212. mutex_unlock(&group->notification_mutex);
  213. }
  214. static void initialize_event(struct fsnotify_event *event)
  215. {
  216. event->holder.event = NULL;
  217. INIT_LIST_HEAD(&event->holder.event_list);
  218. atomic_set(&event->refcnt, 1);
  219. spin_lock_init(&event->lock);
  220. event->path.dentry = NULL;
  221. event->path.mnt = NULL;
  222. event->inode = NULL;
  223. event->data_type = FSNOTIFY_EVENT_NONE;
  224. event->to_tell = NULL;
  225. event->file_name = NULL;
  226. event->name_len = 0;
  227. }
  228. /*
  229. * fsnotify_create_event - Allocate a new event which will be sent to each
  230. * group's handle_event function if the group was interested in this
  231. * particular event.
  232. *
  233. * @to_tell the inode which is supposed to receive the event (sometimes a
  234. * parent of the inode to which the event happened.
  235. * @mask what actually happened.
  236. * @data pointer to the object which was actually affected
  237. * @data_type flag indication if the data is a file, path, inode, nothing...
  238. * @name the filename, if available
  239. */
  240. struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
  241. void *data, int data_type, const char *name)
  242. {
  243. struct fsnotify_event *event;
  244. event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
  245. if (!event)
  246. return NULL;
  247. initialize_event(event);
  248. if (name) {
  249. event->file_name = kstrdup(name, GFP_KERNEL);
  250. if (!event->file_name) {
  251. kmem_cache_free(fsnotify_event_cachep, event);
  252. return NULL;
  253. }
  254. event->name_len = strlen(event->file_name);
  255. }
  256. event->to_tell = to_tell;
  257. switch (data_type) {
  258. case FSNOTIFY_EVENT_FILE: {
  259. struct file *file = data;
  260. struct path *path = &file->f_path;
  261. event->path.dentry = path->dentry;
  262. event->path.mnt = path->mnt;
  263. path_get(&event->path);
  264. event->data_type = FSNOTIFY_EVENT_PATH;
  265. break;
  266. }
  267. case FSNOTIFY_EVENT_PATH: {
  268. struct path *path = data;
  269. event->path.dentry = path->dentry;
  270. event->path.mnt = path->mnt;
  271. path_get(&event->path);
  272. event->data_type = FSNOTIFY_EVENT_PATH;
  273. break;
  274. }
  275. case FSNOTIFY_EVENT_INODE:
  276. event->inode = data;
  277. event->data_type = FSNOTIFY_EVENT_INODE;
  278. break;
  279. case FSNOTIFY_EVENT_NONE:
  280. event->inode = NULL;
  281. event->path.dentry = NULL;
  282. event->path.mnt = NULL;
  283. break;
  284. default:
  285. BUG();
  286. }
  287. event->mask = mask;
  288. return event;
  289. }
  290. __init int fsnotify_notification_init(void)
  291. {
  292. fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
  293. fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
  294. initialize_event(&q_overflow_event);
  295. q_overflow_event.mask = FS_Q_OVERFLOW;
  296. return 0;
  297. }
  298. subsys_initcall(fsnotify_notification_init);