notification.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/module.h>
  37. #include <linux/mount.h>
  38. #include <linux/mutex.h>
  39. #include <linux/namei.h>
  40. #include <linux/path.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include <asm/atomic.h>
  44. #include <linux/fsnotify_backend.h>
  45. #include "fsnotify.h"
  46. static struct kmem_cache *fsnotify_event_cachep;
  47. static struct kmem_cache *fsnotify_event_holder_cachep;
  48. /*
  49. * This is a magic event we send when the q is too full. Since it doesn't
  50. * hold real event information we just keep one system wide and use it any time
  51. * it is needed. It's refcnt is set 1 at kernel init time and will never
  52. * get set to 0 so it will never get 'freed'
  53. */
  54. static struct fsnotify_event q_overflow_event;
  55. static atomic_t fsnotify_sync_cookie = ATOMIC_INIT(0);
  56. /**
  57. * fsnotify_get_cookie - return a unique cookie for use in synchronizing events.
  58. * Called from fsnotify_move, which is inlined into filesystem modules.
  59. */
  60. u32 fsnotify_get_cookie(void)
  61. {
  62. return atomic_inc_return(&fsnotify_sync_cookie);
  63. }
  64. EXPORT_SYMBOL_GPL(fsnotify_get_cookie);
  65. /* return true if the notify queue is empty, false otherwise */
  66. bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
  67. {
  68. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  69. return list_empty(&group->notification_list) ? true : false;
  70. }
  71. void fsnotify_get_event(struct fsnotify_event *event)
  72. {
  73. atomic_inc(&event->refcnt);
  74. }
  75. void fsnotify_put_event(struct fsnotify_event *event)
  76. {
  77. if (!event)
  78. return;
  79. if (atomic_dec_and_test(&event->refcnt)) {
  80. if (event->data_type == FSNOTIFY_EVENT_PATH)
  81. path_put(&event->path);
  82. BUG_ON(!list_empty(&event->private_data_list));
  83. kfree(event->file_name);
  84. kmem_cache_free(fsnotify_event_cachep, event);
  85. }
  86. }
  87. struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
  88. {
  89. return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
  90. }
  91. void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
  92. {
  93. kmem_cache_free(fsnotify_event_holder_cachep, holder);
  94. }
  95. /*
  96. * Find the private data that the group previously attached to this event when
  97. * the group added the event to the notification queue (fsnotify_add_notify_event)
  98. */
  99. struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event)
  100. {
  101. struct fsnotify_event_private_data *lpriv;
  102. struct fsnotify_event_private_data *priv = NULL;
  103. assert_spin_locked(&event->lock);
  104. list_for_each_entry(lpriv, &event->private_data_list, event_list) {
  105. if (lpriv->group == group) {
  106. priv = lpriv;
  107. list_del(&priv->event_list);
  108. break;
  109. }
  110. }
  111. return priv;
  112. }
  113. /*
  114. * Check if 2 events contain the same information. We do not compare private data
  115. * but at this moment that isn't a problem for any know fsnotify listeners.
  116. */
  117. static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new)
  118. {
  119. if ((old->mask == new->mask) &&
  120. (old->to_tell == new->to_tell) &&
  121. (old->data_type == new->data_type) &&
  122. (old->name_len == new->name_len)) {
  123. switch (old->data_type) {
  124. case (FSNOTIFY_EVENT_INODE):
  125. /* remember, after old was put on the wait_q we aren't
  126. * allowed to look at the inode any more, only thing
  127. * left to check was if the file_name is the same */
  128. if (!old->name_len ||
  129. !strcmp(old->file_name, new->file_name))
  130. return true;
  131. break;
  132. case (FSNOTIFY_EVENT_PATH):
  133. if ((old->path.mnt == new->path.mnt) &&
  134. (old->path.dentry == new->path.dentry))
  135. return true;
  136. break;
  137. case (FSNOTIFY_EVENT_NONE):
  138. if (old->mask & FS_Q_OVERFLOW)
  139. return true;
  140. else if (old->mask & FS_IN_IGNORED)
  141. return false;
  142. return false;
  143. };
  144. }
  145. return false;
  146. }
  147. /*
  148. * Add an event to the group notification queue. The group can later pull this
  149. * event off the queue to deal with. If the event is successfully added to the
  150. * group's notification queue, a reference is taken on event.
  151. */
  152. int fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
  153. struct fsnotify_event_private_data *priv)
  154. {
  155. struct fsnotify_event_holder *holder = NULL;
  156. struct list_head *list = &group->notification_list;
  157. struct fsnotify_event_holder *last_holder;
  158. struct fsnotify_event *last_event;
  159. int ret = 0;
  160. /*
  161. * There is one fsnotify_event_holder embedded inside each fsnotify_event.
  162. * Check if we expect to be able to use that holder. If not alloc a new
  163. * holder.
  164. * For the overflow event it's possible that something will use the in
  165. * event holder before we get the lock so we may need to jump back and
  166. * alloc a new holder, this can't happen for most events...
  167. */
  168. if (!list_empty(&event->holder.event_list)) {
  169. alloc_holder:
  170. holder = fsnotify_alloc_event_holder();
  171. if (!holder)
  172. return -ENOMEM;
  173. }
  174. mutex_lock(&group->notification_mutex);
  175. if (group->q_len >= group->max_events) {
  176. event = &q_overflow_event;
  177. ret = -EOVERFLOW;
  178. /* sorry, no private data on the overflow event */
  179. priv = NULL;
  180. }
  181. spin_lock(&event->lock);
  182. if (list_empty(&event->holder.event_list)) {
  183. if (unlikely(holder))
  184. fsnotify_destroy_event_holder(holder);
  185. holder = &event->holder;
  186. } else if (unlikely(!holder)) {
  187. /* between the time we checked above and got the lock the in
  188. * event holder was used, go back and get a new one */
  189. spin_unlock(&event->lock);
  190. mutex_unlock(&group->notification_mutex);
  191. goto alloc_holder;
  192. }
  193. if (!list_empty(list)) {
  194. last_holder = list_entry(list->prev, struct fsnotify_event_holder, event_list);
  195. last_event = last_holder->event;
  196. if (event_compare(last_event, event)) {
  197. spin_unlock(&event->lock);
  198. mutex_unlock(&group->notification_mutex);
  199. if (holder != &event->holder)
  200. fsnotify_destroy_event_holder(holder);
  201. return -EEXIST;
  202. }
  203. }
  204. group->q_len++;
  205. holder->event = event;
  206. fsnotify_get_event(event);
  207. list_add_tail(&holder->event_list, list);
  208. if (priv)
  209. list_add_tail(&priv->event_list, &event->private_data_list);
  210. spin_unlock(&event->lock);
  211. mutex_unlock(&group->notification_mutex);
  212. wake_up(&group->notification_waitq);
  213. return ret;
  214. }
  215. /*
  216. * Remove and return the first event from the notification list. There is a
  217. * reference held on this event since it was on the list. It is the responsibility
  218. * of the caller to drop this reference.
  219. */
  220. struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
  221. {
  222. struct fsnotify_event *event;
  223. struct fsnotify_event_holder *holder;
  224. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  225. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  226. event = holder->event;
  227. spin_lock(&event->lock);
  228. holder->event = NULL;
  229. list_del_init(&holder->event_list);
  230. spin_unlock(&event->lock);
  231. /* event == holder means we are referenced through the in event holder */
  232. if (holder != &event->holder)
  233. fsnotify_destroy_event_holder(holder);
  234. group->q_len--;
  235. return event;
  236. }
  237. /*
  238. * This will not remove the event, that must be done with fsnotify_remove_notify_event()
  239. */
  240. struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
  241. {
  242. struct fsnotify_event *event;
  243. struct fsnotify_event_holder *holder;
  244. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  245. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  246. event = holder->event;
  247. return event;
  248. }
  249. /*
  250. * Called when a group is being torn down to clean up any outstanding
  251. * event notifications.
  252. */
  253. void fsnotify_flush_notify(struct fsnotify_group *group)
  254. {
  255. struct fsnotify_event *event;
  256. struct fsnotify_event_private_data *priv;
  257. mutex_lock(&group->notification_mutex);
  258. while (!fsnotify_notify_queue_is_empty(group)) {
  259. event = fsnotify_remove_notify_event(group);
  260. /* if they don't implement free_event_priv they better not have attached any */
  261. if (group->ops->free_event_priv) {
  262. spin_lock(&event->lock);
  263. priv = fsnotify_remove_priv_from_event(group, event);
  264. spin_unlock(&event->lock);
  265. if (priv)
  266. group->ops->free_event_priv(priv);
  267. }
  268. fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
  269. }
  270. mutex_unlock(&group->notification_mutex);
  271. }
  272. static void initialize_event(struct fsnotify_event *event)
  273. {
  274. event->holder.event = NULL;
  275. INIT_LIST_HEAD(&event->holder.event_list);
  276. atomic_set(&event->refcnt, 1);
  277. spin_lock_init(&event->lock);
  278. event->path.dentry = NULL;
  279. event->path.mnt = NULL;
  280. event->inode = NULL;
  281. event->data_type = FSNOTIFY_EVENT_NONE;
  282. INIT_LIST_HEAD(&event->private_data_list);
  283. event->to_tell = NULL;
  284. event->file_name = NULL;
  285. event->name_len = 0;
  286. event->sync_cookie = 0;
  287. }
  288. /*
  289. * fsnotify_create_event - Allocate a new event which will be sent to each
  290. * group's handle_event function if the group was interested in this
  291. * particular event.
  292. *
  293. * @to_tell the inode which is supposed to receive the event (sometimes a
  294. * parent of the inode to which the event happened.
  295. * @mask what actually happened.
  296. * @data pointer to the object which was actually affected
  297. * @data_type flag indication if the data is a file, path, inode, nothing...
  298. * @name the filename, if available
  299. */
  300. struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
  301. int data_type, const char *name, u32 cookie,
  302. gfp_t gfp)
  303. {
  304. struct fsnotify_event *event;
  305. event = kmem_cache_alloc(fsnotify_event_cachep, gfp);
  306. if (!event)
  307. return NULL;
  308. initialize_event(event);
  309. if (name) {
  310. event->file_name = kstrdup(name, gfp);
  311. if (!event->file_name) {
  312. kmem_cache_free(fsnotify_event_cachep, event);
  313. return NULL;
  314. }
  315. event->name_len = strlen(event->file_name);
  316. }
  317. event->sync_cookie = cookie;
  318. event->to_tell = to_tell;
  319. switch (data_type) {
  320. case FSNOTIFY_EVENT_FILE: {
  321. struct file *file = data;
  322. struct path *path = &file->f_path;
  323. event->path.dentry = path->dentry;
  324. event->path.mnt = path->mnt;
  325. path_get(&event->path);
  326. event->data_type = FSNOTIFY_EVENT_PATH;
  327. break;
  328. }
  329. case FSNOTIFY_EVENT_PATH: {
  330. struct path *path = data;
  331. event->path.dentry = path->dentry;
  332. event->path.mnt = path->mnt;
  333. path_get(&event->path);
  334. event->data_type = FSNOTIFY_EVENT_PATH;
  335. break;
  336. }
  337. case FSNOTIFY_EVENT_INODE:
  338. event->inode = data;
  339. event->data_type = FSNOTIFY_EVENT_INODE;
  340. break;
  341. case FSNOTIFY_EVENT_NONE:
  342. event->inode = NULL;
  343. event->path.dentry = NULL;
  344. event->path.mnt = NULL;
  345. break;
  346. default:
  347. BUG();
  348. }
  349. event->mask = mask;
  350. return event;
  351. }
  352. __init int fsnotify_notification_init(void)
  353. {
  354. fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
  355. fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
  356. initialize_event(&q_overflow_event);
  357. q_overflow_event.mask = FS_Q_OVERFLOW;
  358. return 0;
  359. }
  360. subsys_initcall(fsnotify_notification_init);