notification.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 asynchronously 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 <linux/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. pr_debug("%s: event=%p\n", __func__, event);
  81. if (event->data_type == FSNOTIFY_EVENT_PATH)
  82. path_put(&event->path);
  83. BUG_ON(!list_empty(&event->private_data_list));
  84. kfree(event->file_name);
  85. put_pid(event->tgid);
  86. kmem_cache_free(fsnotify_event_cachep, event);
  87. }
  88. }
  89. struct fsnotify_event_holder *fsnotify_alloc_event_holder(void)
  90. {
  91. return kmem_cache_alloc(fsnotify_event_holder_cachep, GFP_KERNEL);
  92. }
  93. void fsnotify_destroy_event_holder(struct fsnotify_event_holder *holder)
  94. {
  95. if (holder)
  96. kmem_cache_free(fsnotify_event_holder_cachep, holder);
  97. }
  98. /*
  99. * Find the private data that the group previously attached to this event when
  100. * the group added the event to the notification queue (fsnotify_add_notify_event)
  101. */
  102. struct fsnotify_event_private_data *fsnotify_remove_priv_from_event(struct fsnotify_group *group, struct fsnotify_event *event)
  103. {
  104. struct fsnotify_event_private_data *lpriv;
  105. struct fsnotify_event_private_data *priv = NULL;
  106. assert_spin_locked(&event->lock);
  107. list_for_each_entry(lpriv, &event->private_data_list, event_list) {
  108. if (lpriv->group == group) {
  109. priv = lpriv;
  110. list_del(&priv->event_list);
  111. break;
  112. }
  113. }
  114. return priv;
  115. }
  116. /*
  117. * Add an event to the group notification queue. The group can later pull this
  118. * event off the queue to deal with. If the event is successfully added to the
  119. * group's notification queue, a reference is taken on event.
  120. */
  121. struct fsnotify_event *fsnotify_add_notify_event(struct fsnotify_group *group, struct fsnotify_event *event,
  122. struct fsnotify_event_private_data *priv,
  123. struct fsnotify_event *(*merge)(struct list_head *,
  124. struct fsnotify_event *))
  125. {
  126. struct fsnotify_event *return_event = NULL;
  127. struct fsnotify_event_holder *holder = NULL;
  128. struct list_head *list = &group->notification_list;
  129. pr_debug("%s: group=%p event=%p priv=%p\n", __func__, group, event, priv);
  130. /*
  131. * There is one fsnotify_event_holder embedded inside each fsnotify_event.
  132. * Check if we expect to be able to use that holder. If not alloc a new
  133. * holder.
  134. * For the overflow event it's possible that something will use the in
  135. * event holder before we get the lock so we may need to jump back and
  136. * alloc a new holder, this can't happen for most events...
  137. */
  138. if (!list_empty(&event->holder.event_list)) {
  139. alloc_holder:
  140. holder = fsnotify_alloc_event_holder();
  141. if (!holder)
  142. return ERR_PTR(-ENOMEM);
  143. }
  144. mutex_lock(&group->notification_mutex);
  145. if (group->q_len >= group->max_events) {
  146. event = q_overflow_event;
  147. /*
  148. * we need to return the overflow event
  149. * which means we need a ref
  150. */
  151. fsnotify_get_event(event);
  152. return_event = event;
  153. /* sorry, no private data on the overflow event */
  154. priv = NULL;
  155. }
  156. if (!list_empty(list) && merge) {
  157. struct fsnotify_event *tmp;
  158. tmp = merge(list, event);
  159. if (tmp) {
  160. mutex_unlock(&group->notification_mutex);
  161. if (return_event)
  162. fsnotify_put_event(return_event);
  163. if (holder != &event->holder)
  164. fsnotify_destroy_event_holder(holder);
  165. return tmp;
  166. }
  167. }
  168. spin_lock(&event->lock);
  169. if (list_empty(&event->holder.event_list)) {
  170. if (unlikely(holder))
  171. fsnotify_destroy_event_holder(holder);
  172. holder = &event->holder;
  173. } else if (unlikely(!holder)) {
  174. /* between the time we checked above and got the lock the in
  175. * event holder was used, go back and get a new one */
  176. spin_unlock(&event->lock);
  177. mutex_unlock(&group->notification_mutex);
  178. if (return_event) {
  179. fsnotify_put_event(return_event);
  180. return_event = NULL;
  181. }
  182. goto alloc_holder;
  183. }
  184. group->q_len++;
  185. holder->event = event;
  186. fsnotify_get_event(event);
  187. list_add_tail(&holder->event_list, list);
  188. if (priv)
  189. list_add_tail(&priv->event_list, &event->private_data_list);
  190. spin_unlock(&event->lock);
  191. mutex_unlock(&group->notification_mutex);
  192. wake_up(&group->notification_waitq);
  193. kill_fasync(&group->fsn_fa, SIGIO, POLL_IN);
  194. return return_event;
  195. }
  196. /*
  197. * Remove and return the first event from the notification list. There is a
  198. * reference held on this event since it was on the list. It is the responsibility
  199. * of the caller to drop this reference.
  200. */
  201. struct fsnotify_event *fsnotify_remove_notify_event(struct fsnotify_group *group)
  202. {
  203. struct fsnotify_event *event;
  204. struct fsnotify_event_holder *holder;
  205. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  206. pr_debug("%s: group=%p\n", __func__, group);
  207. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  208. event = holder->event;
  209. spin_lock(&event->lock);
  210. holder->event = NULL;
  211. list_del_init(&holder->event_list);
  212. spin_unlock(&event->lock);
  213. /* event == holder means we are referenced through the in event holder */
  214. if (holder != &event->holder)
  215. fsnotify_destroy_event_holder(holder);
  216. group->q_len--;
  217. return event;
  218. }
  219. /*
  220. * This will not remove the event, that must be done with fsnotify_remove_notify_event()
  221. */
  222. struct fsnotify_event *fsnotify_peek_notify_event(struct fsnotify_group *group)
  223. {
  224. struct fsnotify_event *event;
  225. struct fsnotify_event_holder *holder;
  226. BUG_ON(!mutex_is_locked(&group->notification_mutex));
  227. holder = list_first_entry(&group->notification_list, struct fsnotify_event_holder, event_list);
  228. event = holder->event;
  229. return event;
  230. }
  231. /*
  232. * Called when a group is being torn down to clean up any outstanding
  233. * event notifications.
  234. */
  235. void fsnotify_flush_notify(struct fsnotify_group *group)
  236. {
  237. struct fsnotify_event *event;
  238. struct fsnotify_event_private_data *priv;
  239. mutex_lock(&group->notification_mutex);
  240. while (!fsnotify_notify_queue_is_empty(group)) {
  241. event = fsnotify_remove_notify_event(group);
  242. /* if they don't implement free_event_priv they better not have attached any */
  243. if (group->ops->free_event_priv) {
  244. spin_lock(&event->lock);
  245. priv = fsnotify_remove_priv_from_event(group, event);
  246. spin_unlock(&event->lock);
  247. if (priv)
  248. group->ops->free_event_priv(priv);
  249. }
  250. fsnotify_put_event(event); /* matches fsnotify_add_notify_event */
  251. }
  252. mutex_unlock(&group->notification_mutex);
  253. }
  254. static void initialize_event(struct fsnotify_event *event)
  255. {
  256. INIT_LIST_HEAD(&event->holder.event_list);
  257. atomic_set(&event->refcnt, 1);
  258. spin_lock_init(&event->lock);
  259. INIT_LIST_HEAD(&event->private_data_list);
  260. }
  261. /*
  262. * Caller damn well better be holding whatever mutex is protecting the
  263. * old_holder->event_list and the new_event must be a clean event which
  264. * cannot be found anywhere else in the kernel.
  265. */
  266. int fsnotify_replace_event(struct fsnotify_event_holder *old_holder,
  267. struct fsnotify_event *new_event)
  268. {
  269. struct fsnotify_event *old_event = old_holder->event;
  270. struct fsnotify_event_holder *new_holder = &new_event->holder;
  271. enum event_spinlock_class {
  272. SPINLOCK_OLD,
  273. SPINLOCK_NEW,
  274. };
  275. pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, new_event);
  276. /*
  277. * if the new_event's embedded holder is in use someone
  278. * screwed up and didn't give us a clean new event.
  279. */
  280. BUG_ON(!list_empty(&new_holder->event_list));
  281. spin_lock_nested(&old_event->lock, SPINLOCK_OLD);
  282. spin_lock_nested(&new_event->lock, SPINLOCK_NEW);
  283. new_holder->event = new_event;
  284. list_replace_init(&old_holder->event_list, &new_holder->event_list);
  285. spin_unlock(&new_event->lock);
  286. spin_unlock(&old_event->lock);
  287. /* event == holder means we are referenced through the in event holder */
  288. if (old_holder != &old_event->holder)
  289. fsnotify_destroy_event_holder(old_holder);
  290. fsnotify_get_event(new_event); /* on the list take reference */
  291. fsnotify_put_event(old_event); /* off the list, drop reference */
  292. return 0;
  293. }
  294. struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
  295. {
  296. struct fsnotify_event *event;
  297. event = kmem_cache_alloc(fsnotify_event_cachep, GFP_KERNEL);
  298. if (!event)
  299. return NULL;
  300. pr_debug("%s: old_event=%p new_event=%p\n", __func__, old_event, event);
  301. memcpy(event, old_event, sizeof(*event));
  302. initialize_event(event);
  303. if (event->name_len) {
  304. event->file_name = kstrdup(old_event->file_name, GFP_KERNEL);
  305. if (!event->file_name) {
  306. kmem_cache_free(fsnotify_event_cachep, event);
  307. return NULL;
  308. }
  309. }
  310. event->tgid = get_pid(old_event->tgid);
  311. if (event->data_type == FSNOTIFY_EVENT_PATH)
  312. path_get(&event->path);
  313. return event;
  314. }
  315. /*
  316. * fsnotify_create_event - Allocate a new event which will be sent to each
  317. * group's handle_event function if the group was interested in this
  318. * particular event.
  319. *
  320. * @to_tell the inode which is supposed to receive the event (sometimes a
  321. * parent of the inode to which the event happened.
  322. * @mask what actually happened.
  323. * @data pointer to the object which was actually affected
  324. * @data_type flag indication if the data is a file, path, inode, nothing...
  325. * @name the filename, if available
  326. */
  327. struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask, void *data,
  328. int data_type, const unsigned char *name,
  329. u32 cookie, gfp_t gfp)
  330. {
  331. struct fsnotify_event *event;
  332. event = kmem_cache_zalloc(fsnotify_event_cachep, gfp);
  333. if (!event)
  334. return NULL;
  335. pr_debug("%s: event=%p to_tell=%p mask=%x data=%p data_type=%d\n",
  336. __func__, event, to_tell, mask, data, data_type);
  337. initialize_event(event);
  338. if (name) {
  339. event->file_name = kstrdup(name, gfp);
  340. if (!event->file_name) {
  341. kmem_cache_free(fsnotify_event_cachep, event);
  342. return NULL;
  343. }
  344. event->name_len = strlen(event->file_name);
  345. }
  346. event->tgid = get_pid(task_tgid(current));
  347. event->sync_cookie = cookie;
  348. event->to_tell = to_tell;
  349. event->data_type = data_type;
  350. switch (data_type) {
  351. case FSNOTIFY_EVENT_PATH: {
  352. struct path *path = data;
  353. event->path.dentry = path->dentry;
  354. event->path.mnt = path->mnt;
  355. path_get(&event->path);
  356. break;
  357. }
  358. case FSNOTIFY_EVENT_INODE:
  359. event->inode = data;
  360. break;
  361. case FSNOTIFY_EVENT_NONE:
  362. event->inode = NULL;
  363. event->path.dentry = NULL;
  364. event->path.mnt = NULL;
  365. break;
  366. default:
  367. BUG();
  368. }
  369. event->mask = mask;
  370. return event;
  371. }
  372. static __init int fsnotify_notification_init(void)
  373. {
  374. fsnotify_event_cachep = KMEM_CACHE(fsnotify_event, SLAB_PANIC);
  375. fsnotify_event_holder_cachep = KMEM_CACHE(fsnotify_event_holder, SLAB_PANIC);
  376. q_overflow_event = fsnotify_create_event(NULL, FS_Q_OVERFLOW, NULL,
  377. FSNOTIFY_EVENT_NONE, NULL, 0,
  378. GFP_KERNEL);
  379. if (!q_overflow_event)
  380. panic("unable to allocate fsnotify q_overflow_event\n");
  381. return 0;
  382. }
  383. subsys_initcall(fsnotify_notification_init);