inotify.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. * fs/inotify.c - inode-based file event notifications
  3. *
  4. * Authors:
  5. * John McCutchan <ttb@tentacle.dhs.org>
  6. * Robert Love <rml@novell.com>
  7. *
  8. * Copyright (C) 2005 John McCutchan
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/idr.h>
  25. #include <linux/slab.h>
  26. #include <linux/fs.h>
  27. #include <linux/file.h>
  28. #include <linux/mount.h>
  29. #include <linux/namei.h>
  30. #include <linux/poll.h>
  31. #include <linux/init.h>
  32. #include <linux/list.h>
  33. #include <linux/writeback.h>
  34. #include <linux/inotify.h>
  35. #include <linux/syscalls.h>
  36. #include <asm/ioctls.h>
  37. static atomic_t inotify_cookie;
  38. static kmem_cache_t *watch_cachep __read_mostly;
  39. static kmem_cache_t *event_cachep __read_mostly;
  40. static struct vfsmount *inotify_mnt __read_mostly;
  41. /* these are configurable via /proc/sys/fs/inotify/ */
  42. int inotify_max_user_instances __read_mostly;
  43. int inotify_max_user_watches __read_mostly;
  44. int inotify_max_queued_events __read_mostly;
  45. /*
  46. * Lock ordering:
  47. *
  48. * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
  49. * iprune_mutex (synchronize shrink_icache_memory())
  50. * inode_lock (protects the super_block->s_inodes list)
  51. * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)
  52. * inotify_dev->mutex (protects inotify_device and watches->d_list)
  53. */
  54. /*
  55. * Lifetimes of the three main data structures--inotify_device, inode, and
  56. * inotify_watch--are managed by reference count.
  57. *
  58. * inotify_device: Lifetime is from inotify_init() until release. Additional
  59. * references can bump the count via get_inotify_dev() and drop the count via
  60. * put_inotify_dev().
  61. *
  62. * inotify_watch: Lifetime is from create_watch() to destory_watch().
  63. * Additional references can bump the count via get_inotify_watch() and drop
  64. * the count via put_inotify_watch().
  65. *
  66. * inode: Pinned so long as the inode is associated with a watch, from
  67. * create_watch() to put_inotify_watch().
  68. */
  69. /*
  70. * struct inotify_device - represents an inotify instance
  71. *
  72. * This structure is protected by the mutex 'mutex'.
  73. */
  74. struct inotify_device {
  75. wait_queue_head_t wq; /* wait queue for i/o */
  76. struct idr idr; /* idr mapping wd -> watch */
  77. struct mutex mutex; /* protects this bad boy */
  78. struct list_head events; /* list of queued events */
  79. struct list_head watches; /* list of watches */
  80. atomic_t count; /* reference count */
  81. struct user_struct *user; /* user who opened this dev */
  82. unsigned int queue_size; /* size of the queue (bytes) */
  83. unsigned int event_count; /* number of pending events */
  84. unsigned int max_events; /* maximum number of events */
  85. u32 last_wd; /* the last wd allocated */
  86. };
  87. /*
  88. * struct inotify_kernel_event - An inotify event, originating from a watch and
  89. * queued for user-space. A list of these is attached to each instance of the
  90. * device. In read(), this list is walked and all events that can fit in the
  91. * buffer are returned.
  92. *
  93. * Protected by dev->mutex of the device in which we are queued.
  94. */
  95. struct inotify_kernel_event {
  96. struct inotify_event event; /* the user-space event */
  97. struct list_head list; /* entry in inotify_device's list */
  98. char *name; /* filename, if any */
  99. };
  100. /*
  101. * struct inotify_watch - represents a watch request on a specific inode
  102. *
  103. * d_list is protected by dev->mutex of the associated watch->dev.
  104. * i_list and mask are protected by inode->inotify_mutex of the associated inode.
  105. * dev, inode, and wd are never written to once the watch is created.
  106. */
  107. struct inotify_watch {
  108. struct list_head d_list; /* entry in inotify_device's list */
  109. struct list_head i_list; /* entry in inode's list */
  110. atomic_t count; /* reference count */
  111. struct inotify_device *dev; /* associated device */
  112. struct inode *inode; /* associated inode */
  113. s32 wd; /* watch descriptor */
  114. u32 mask; /* event mask for this watch */
  115. };
  116. #ifdef CONFIG_SYSCTL
  117. #include <linux/sysctl.h>
  118. static int zero;
  119. ctl_table inotify_table[] = {
  120. {
  121. .ctl_name = INOTIFY_MAX_USER_INSTANCES,
  122. .procname = "max_user_instances",
  123. .data = &inotify_max_user_instances,
  124. .maxlen = sizeof(int),
  125. .mode = 0644,
  126. .proc_handler = &proc_dointvec_minmax,
  127. .strategy = &sysctl_intvec,
  128. .extra1 = &zero,
  129. },
  130. {
  131. .ctl_name = INOTIFY_MAX_USER_WATCHES,
  132. .procname = "max_user_watches",
  133. .data = &inotify_max_user_watches,
  134. .maxlen = sizeof(int),
  135. .mode = 0644,
  136. .proc_handler = &proc_dointvec_minmax,
  137. .strategy = &sysctl_intvec,
  138. .extra1 = &zero,
  139. },
  140. {
  141. .ctl_name = INOTIFY_MAX_QUEUED_EVENTS,
  142. .procname = "max_queued_events",
  143. .data = &inotify_max_queued_events,
  144. .maxlen = sizeof(int),
  145. .mode = 0644,
  146. .proc_handler = &proc_dointvec_minmax,
  147. .strategy = &sysctl_intvec,
  148. .extra1 = &zero
  149. },
  150. { .ctl_name = 0 }
  151. };
  152. #endif /* CONFIG_SYSCTL */
  153. static inline void get_inotify_dev(struct inotify_device *dev)
  154. {
  155. atomic_inc(&dev->count);
  156. }
  157. static inline void put_inotify_dev(struct inotify_device *dev)
  158. {
  159. if (atomic_dec_and_test(&dev->count)) {
  160. atomic_dec(&dev->user->inotify_devs);
  161. free_uid(dev->user);
  162. idr_destroy(&dev->idr);
  163. kfree(dev);
  164. }
  165. }
  166. static inline void get_inotify_watch(struct inotify_watch *watch)
  167. {
  168. atomic_inc(&watch->count);
  169. }
  170. /*
  171. * put_inotify_watch - decrements the ref count on a given watch. cleans up
  172. * the watch and its references if the count reaches zero.
  173. */
  174. static inline void put_inotify_watch(struct inotify_watch *watch)
  175. {
  176. if (atomic_dec_and_test(&watch->count)) {
  177. put_inotify_dev(watch->dev);
  178. iput(watch->inode);
  179. kmem_cache_free(watch_cachep, watch);
  180. }
  181. }
  182. /*
  183. * kernel_event - create a new kernel event with the given parameters
  184. *
  185. * This function can sleep.
  186. */
  187. static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie,
  188. const char *name)
  189. {
  190. struct inotify_kernel_event *kevent;
  191. kevent = kmem_cache_alloc(event_cachep, GFP_KERNEL);
  192. if (unlikely(!kevent))
  193. return NULL;
  194. /* we hand this out to user-space, so zero it just in case */
  195. memset(&kevent->event, 0, sizeof(struct inotify_event));
  196. kevent->event.wd = wd;
  197. kevent->event.mask = mask;
  198. kevent->event.cookie = cookie;
  199. INIT_LIST_HEAD(&kevent->list);
  200. if (name) {
  201. size_t len, rem, event_size = sizeof(struct inotify_event);
  202. /*
  203. * We need to pad the filename so as to properly align an
  204. * array of inotify_event structures. Because the structure is
  205. * small and the common case is a small filename, we just round
  206. * up to the next multiple of the structure's sizeof. This is
  207. * simple and safe for all architectures.
  208. */
  209. len = strlen(name) + 1;
  210. rem = event_size - len;
  211. if (len > event_size) {
  212. rem = event_size - (len % event_size);
  213. if (len % event_size == 0)
  214. rem = 0;
  215. }
  216. kevent->name = kmalloc(len + rem, GFP_KERNEL);
  217. if (unlikely(!kevent->name)) {
  218. kmem_cache_free(event_cachep, kevent);
  219. return NULL;
  220. }
  221. memcpy(kevent->name, name, len);
  222. if (rem)
  223. memset(kevent->name + len, 0, rem);
  224. kevent->event.len = len + rem;
  225. } else {
  226. kevent->event.len = 0;
  227. kevent->name = NULL;
  228. }
  229. return kevent;
  230. }
  231. /*
  232. * inotify_dev_get_event - return the next event in the given dev's queue
  233. *
  234. * Caller must hold dev->mutex.
  235. */
  236. static inline struct inotify_kernel_event *
  237. inotify_dev_get_event(struct inotify_device *dev)
  238. {
  239. return list_entry(dev->events.next, struct inotify_kernel_event, list);
  240. }
  241. /*
  242. * inotify_dev_queue_event - add a new event to the given device
  243. *
  244. * Caller must hold dev->mutex. Can sleep (calls kernel_event()).
  245. */
  246. static void inotify_dev_queue_event(struct inotify_device *dev,
  247. struct inotify_watch *watch, u32 mask,
  248. u32 cookie, const char *name)
  249. {
  250. struct inotify_kernel_event *kevent, *last;
  251. /* coalescing: drop this event if it is a dupe of the previous */
  252. last = inotify_dev_get_event(dev);
  253. if (last && last->event.mask == mask && last->event.wd == watch->wd &&
  254. last->event.cookie == cookie) {
  255. const char *lastname = last->name;
  256. if (!name && !lastname)
  257. return;
  258. if (name && lastname && !strcmp(lastname, name))
  259. return;
  260. }
  261. /* the queue overflowed and we already sent the Q_OVERFLOW event */
  262. if (unlikely(dev->event_count > dev->max_events))
  263. return;
  264. /* if the queue overflows, we need to notify user space */
  265. if (unlikely(dev->event_count == dev->max_events))
  266. kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL);
  267. else
  268. kevent = kernel_event(watch->wd, mask, cookie, name);
  269. if (unlikely(!kevent))
  270. return;
  271. /* queue the event and wake up anyone waiting */
  272. dev->event_count++;
  273. dev->queue_size += sizeof(struct inotify_event) + kevent->event.len;
  274. list_add_tail(&kevent->list, &dev->events);
  275. wake_up_interruptible(&dev->wq);
  276. }
  277. /*
  278. * remove_kevent - cleans up and ultimately frees the given kevent
  279. *
  280. * Caller must hold dev->mutex.
  281. */
  282. static void remove_kevent(struct inotify_device *dev,
  283. struct inotify_kernel_event *kevent)
  284. {
  285. list_del(&kevent->list);
  286. dev->event_count--;
  287. dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len;
  288. kfree(kevent->name);
  289. kmem_cache_free(event_cachep, kevent);
  290. }
  291. /*
  292. * inotify_dev_event_dequeue - destroy an event on the given device
  293. *
  294. * Caller must hold dev->mutex.
  295. */
  296. static void inotify_dev_event_dequeue(struct inotify_device *dev)
  297. {
  298. if (!list_empty(&dev->events)) {
  299. struct inotify_kernel_event *kevent;
  300. kevent = inotify_dev_get_event(dev);
  301. remove_kevent(dev, kevent);
  302. }
  303. }
  304. /*
  305. * inotify_dev_get_wd - returns the next WD for use by the given dev
  306. *
  307. * Callers must hold dev->mutex. This function can sleep.
  308. */
  309. static int inotify_dev_get_wd(struct inotify_device *dev,
  310. struct inotify_watch *watch)
  311. {
  312. int ret;
  313. do {
  314. if (unlikely(!idr_pre_get(&dev->idr, GFP_KERNEL)))
  315. return -ENOSPC;
  316. ret = idr_get_new_above(&dev->idr, watch, dev->last_wd+1, &watch->wd);
  317. } while (ret == -EAGAIN);
  318. return ret;
  319. }
  320. /*
  321. * find_inode - resolve a user-given path to a specific inode and return a nd
  322. */
  323. static int find_inode(const char __user *dirname, struct nameidata *nd,
  324. unsigned flags)
  325. {
  326. int error;
  327. error = __user_walk(dirname, flags, nd);
  328. if (error)
  329. return error;
  330. /* you can only watch an inode if you have read permissions on it */
  331. error = vfs_permission(nd, MAY_READ);
  332. if (error)
  333. path_release(nd);
  334. return error;
  335. }
  336. /*
  337. * inotify_inode_watched - returns nonzero if there are watches on this inode
  338. * and zero otherwise. We call this lockless, we do not care if we race.
  339. */
  340. static inline int inotify_inode_watched(struct inode *inode)
  341. {
  342. return !list_empty(&inode->inotify_watches);
  343. }
  344. /*
  345. * Get child dentry flag into synch with parent inode.
  346. * Flag should always be clear for negative dentrys.
  347. */
  348. static void set_dentry_child_flags(struct inode *inode, int watched)
  349. {
  350. struct dentry *alias;
  351. spin_lock(&dcache_lock);
  352. list_for_each_entry(alias, &inode->i_dentry, d_alias) {
  353. struct dentry *child;
  354. list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {
  355. if (!child->d_inode) {
  356. WARN_ON(child->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
  357. continue;
  358. }
  359. spin_lock(&child->d_lock);
  360. if (watched) {
  361. WARN_ON(child->d_flags &
  362. DCACHE_INOTIFY_PARENT_WATCHED);
  363. child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  364. } else {
  365. WARN_ON(!(child->d_flags &
  366. DCACHE_INOTIFY_PARENT_WATCHED));
  367. child->d_flags&=~DCACHE_INOTIFY_PARENT_WATCHED;
  368. }
  369. spin_unlock(&child->d_lock);
  370. }
  371. }
  372. spin_unlock(&dcache_lock);
  373. }
  374. /*
  375. * create_watch - creates a watch on the given device.
  376. *
  377. * Callers must hold dev->mutex. Calls inotify_dev_get_wd() so may sleep.
  378. * Both 'dev' and 'inode' (by way of nameidata) need to be pinned.
  379. */
  380. static struct inotify_watch *create_watch(struct inotify_device *dev,
  381. u32 mask, struct inode *inode)
  382. {
  383. struct inotify_watch *watch;
  384. int ret;
  385. if (atomic_read(&dev->user->inotify_watches) >=
  386. inotify_max_user_watches)
  387. return ERR_PTR(-ENOSPC);
  388. watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
  389. if (unlikely(!watch))
  390. return ERR_PTR(-ENOMEM);
  391. ret = inotify_dev_get_wd(dev, watch);
  392. if (unlikely(ret)) {
  393. kmem_cache_free(watch_cachep, watch);
  394. return ERR_PTR(ret);
  395. }
  396. dev->last_wd = watch->wd;
  397. watch->mask = mask;
  398. atomic_set(&watch->count, 0);
  399. INIT_LIST_HEAD(&watch->d_list);
  400. INIT_LIST_HEAD(&watch->i_list);
  401. /* save a reference to device and bump the count to make it official */
  402. get_inotify_dev(dev);
  403. watch->dev = dev;
  404. /*
  405. * Save a reference to the inode and bump the ref count to make it
  406. * official. We hold a reference to nameidata, which makes this safe.
  407. */
  408. watch->inode = igrab(inode);
  409. /* bump our own count, corresponding to our entry in dev->watches */
  410. get_inotify_watch(watch);
  411. atomic_inc(&dev->user->inotify_watches);
  412. return watch;
  413. }
  414. /*
  415. * inotify_find_dev - find the watch associated with the given inode and dev
  416. *
  417. * Callers must hold inode->inotify_mutex.
  418. */
  419. static struct inotify_watch *inode_find_dev(struct inode *inode,
  420. struct inotify_device *dev)
  421. {
  422. struct inotify_watch *watch;
  423. list_for_each_entry(watch, &inode->inotify_watches, i_list) {
  424. if (watch->dev == dev)
  425. return watch;
  426. }
  427. return NULL;
  428. }
  429. /*
  430. * remove_watch_no_event - remove_watch() without the IN_IGNORED event.
  431. */
  432. static void remove_watch_no_event(struct inotify_watch *watch,
  433. struct inotify_device *dev)
  434. {
  435. list_del(&watch->i_list);
  436. list_del(&watch->d_list);
  437. if (!inotify_inode_watched(watch->inode))
  438. set_dentry_child_flags(watch->inode, 0);
  439. atomic_dec(&dev->user->inotify_watches);
  440. idr_remove(&dev->idr, watch->wd);
  441. put_inotify_watch(watch);
  442. }
  443. /*
  444. * remove_watch - Remove a watch from both the device and the inode. Sends
  445. * the IN_IGNORED event to the given device signifying that the inode is no
  446. * longer watched.
  447. *
  448. * Callers must hold both inode->inotify_mutex and dev->mutex. We drop a
  449. * reference to the inode before returning.
  450. *
  451. * The inode is not iput() so as to remain atomic. If the inode needs to be
  452. * iput(), the call returns one. Otherwise, it returns zero.
  453. */
  454. static void remove_watch(struct inotify_watch *watch,struct inotify_device *dev)
  455. {
  456. inotify_dev_queue_event(dev, watch, IN_IGNORED, 0, NULL);
  457. remove_watch_no_event(watch, dev);
  458. }
  459. /* Kernel API */
  460. /*
  461. * inotify_d_instantiate - instantiate dcache entry for inode
  462. */
  463. void inotify_d_instantiate(struct dentry *entry, struct inode *inode)
  464. {
  465. struct dentry *parent;
  466. if (!inode)
  467. return;
  468. WARN_ON(entry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED);
  469. spin_lock(&entry->d_lock);
  470. parent = entry->d_parent;
  471. if (parent->d_inode && inotify_inode_watched(parent->d_inode))
  472. entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  473. spin_unlock(&entry->d_lock);
  474. }
  475. /*
  476. * inotify_d_move - dcache entry has been moved
  477. */
  478. void inotify_d_move(struct dentry *entry)
  479. {
  480. struct dentry *parent;
  481. parent = entry->d_parent;
  482. if (inotify_inode_watched(parent->d_inode))
  483. entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;
  484. else
  485. entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
  486. }
  487. /**
  488. * inotify_inode_queue_event - queue an event to all watches on this inode
  489. * @inode: inode event is originating from
  490. * @mask: event mask describing this event
  491. * @cookie: cookie for synchronization, or zero
  492. * @name: filename, if any
  493. */
  494. void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
  495. const char *name)
  496. {
  497. struct inotify_watch *watch, *next;
  498. if (!inotify_inode_watched(inode))
  499. return;
  500. mutex_lock(&inode->inotify_mutex);
  501. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  502. u32 watch_mask = watch->mask;
  503. if (watch_mask & mask) {
  504. struct inotify_device *dev = watch->dev;
  505. get_inotify_watch(watch);
  506. mutex_lock(&dev->mutex);
  507. inotify_dev_queue_event(dev, watch, mask, cookie, name);
  508. if (watch_mask & IN_ONESHOT)
  509. remove_watch_no_event(watch, dev);
  510. mutex_unlock(&dev->mutex);
  511. put_inotify_watch(watch);
  512. }
  513. }
  514. mutex_unlock(&inode->inotify_mutex);
  515. }
  516. EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
  517. /**
  518. * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
  519. * @dentry: the dentry in question, we queue against this dentry's parent
  520. * @mask: event mask describing this event
  521. * @cookie: cookie for synchronization, or zero
  522. * @name: filename, if any
  523. */
  524. void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
  525. u32 cookie, const char *name)
  526. {
  527. struct dentry *parent;
  528. struct inode *inode;
  529. if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))
  530. return;
  531. spin_lock(&dentry->d_lock);
  532. parent = dentry->d_parent;
  533. inode = parent->d_inode;
  534. if (inotify_inode_watched(inode)) {
  535. dget(parent);
  536. spin_unlock(&dentry->d_lock);
  537. inotify_inode_queue_event(inode, mask, cookie, name);
  538. dput(parent);
  539. } else
  540. spin_unlock(&dentry->d_lock);
  541. }
  542. EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
  543. /**
  544. * inotify_get_cookie - return a unique cookie for use in synchronizing events.
  545. */
  546. u32 inotify_get_cookie(void)
  547. {
  548. return atomic_inc_return(&inotify_cookie);
  549. }
  550. EXPORT_SYMBOL_GPL(inotify_get_cookie);
  551. /**
  552. * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  553. * @list: list of inodes being unmounted (sb->s_inodes)
  554. *
  555. * Called with inode_lock held, protecting the unmounting super block's list
  556. * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.
  557. * We temporarily drop inode_lock, however, and CAN block.
  558. */
  559. void inotify_unmount_inodes(struct list_head *list)
  560. {
  561. struct inode *inode, *next_i, *need_iput = NULL;
  562. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  563. struct inotify_watch *watch, *next_w;
  564. struct inode *need_iput_tmp;
  565. struct list_head *watches;
  566. /*
  567. * If i_count is zero, the inode cannot have any watches and
  568. * doing an __iget/iput with MS_ACTIVE clear would actually
  569. * evict all inodes with zero i_count from icache which is
  570. * unnecessarily violent and may in fact be illegal to do.
  571. */
  572. if (!atomic_read(&inode->i_count))
  573. continue;
  574. /*
  575. * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
  576. * I_WILL_FREE which is fine because by that point the inode
  577. * cannot have any associated watches.
  578. */
  579. if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
  580. continue;
  581. need_iput_tmp = need_iput;
  582. need_iput = NULL;
  583. /* In case the remove_watch() drops a reference. */
  584. if (inode != need_iput_tmp)
  585. __iget(inode);
  586. else
  587. need_iput_tmp = NULL;
  588. /* In case the dropping of a reference would nuke next_i. */
  589. if ((&next_i->i_sb_list != list) &&
  590. atomic_read(&next_i->i_count) &&
  591. !(next_i->i_state & (I_CLEAR | I_FREEING |
  592. I_WILL_FREE))) {
  593. __iget(next_i);
  594. need_iput = next_i;
  595. }
  596. /*
  597. * We can safely drop inode_lock here because we hold
  598. * references on both inode and next_i. Also no new inodes
  599. * will be added since the umount has begun. Finally,
  600. * iprune_mutex keeps shrink_icache_memory() away.
  601. */
  602. spin_unlock(&inode_lock);
  603. if (need_iput_tmp)
  604. iput(need_iput_tmp);
  605. /* for each watch, send IN_UNMOUNT and then remove it */
  606. mutex_lock(&inode->inotify_mutex);
  607. watches = &inode->inotify_watches;
  608. list_for_each_entry_safe(watch, next_w, watches, i_list) {
  609. struct inotify_device *dev = watch->dev;
  610. mutex_lock(&dev->mutex);
  611. inotify_dev_queue_event(dev, watch, IN_UNMOUNT,0,NULL);
  612. remove_watch(watch, dev);
  613. mutex_unlock(&dev->mutex);
  614. }
  615. mutex_unlock(&inode->inotify_mutex);
  616. iput(inode);
  617. spin_lock(&inode_lock);
  618. }
  619. }
  620. EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
  621. /**
  622. * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
  623. * @inode: inode that is about to be removed
  624. */
  625. void inotify_inode_is_dead(struct inode *inode)
  626. {
  627. struct inotify_watch *watch, *next;
  628. mutex_lock(&inode->inotify_mutex);
  629. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  630. struct inotify_device *dev = watch->dev;
  631. mutex_lock(&dev->mutex);
  632. remove_watch(watch, dev);
  633. mutex_unlock(&dev->mutex);
  634. }
  635. mutex_unlock(&inode->inotify_mutex);
  636. }
  637. EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
  638. /* Device Interface */
  639. static unsigned int inotify_poll(struct file *file, poll_table *wait)
  640. {
  641. struct inotify_device *dev = file->private_data;
  642. int ret = 0;
  643. poll_wait(file, &dev->wq, wait);
  644. mutex_lock(&dev->mutex);
  645. if (!list_empty(&dev->events))
  646. ret = POLLIN | POLLRDNORM;
  647. mutex_unlock(&dev->mutex);
  648. return ret;
  649. }
  650. static ssize_t inotify_read(struct file *file, char __user *buf,
  651. size_t count, loff_t *pos)
  652. {
  653. size_t event_size = sizeof (struct inotify_event);
  654. struct inotify_device *dev;
  655. char __user *start;
  656. int ret;
  657. DEFINE_WAIT(wait);
  658. start = buf;
  659. dev = file->private_data;
  660. while (1) {
  661. int events;
  662. prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
  663. mutex_lock(&dev->mutex);
  664. events = !list_empty(&dev->events);
  665. mutex_unlock(&dev->mutex);
  666. if (events) {
  667. ret = 0;
  668. break;
  669. }
  670. if (file->f_flags & O_NONBLOCK) {
  671. ret = -EAGAIN;
  672. break;
  673. }
  674. if (signal_pending(current)) {
  675. ret = -EINTR;
  676. break;
  677. }
  678. schedule();
  679. }
  680. finish_wait(&dev->wq, &wait);
  681. if (ret)
  682. return ret;
  683. mutex_lock(&dev->mutex);
  684. while (1) {
  685. struct inotify_kernel_event *kevent;
  686. ret = buf - start;
  687. if (list_empty(&dev->events))
  688. break;
  689. kevent = inotify_dev_get_event(dev);
  690. if (event_size + kevent->event.len > count)
  691. break;
  692. if (copy_to_user(buf, &kevent->event, event_size)) {
  693. ret = -EFAULT;
  694. break;
  695. }
  696. buf += event_size;
  697. count -= event_size;
  698. if (kevent->name) {
  699. if (copy_to_user(buf, kevent->name, kevent->event.len)){
  700. ret = -EFAULT;
  701. break;
  702. }
  703. buf += kevent->event.len;
  704. count -= kevent->event.len;
  705. }
  706. remove_kevent(dev, kevent);
  707. }
  708. mutex_unlock(&dev->mutex);
  709. return ret;
  710. }
  711. static int inotify_release(struct inode *ignored, struct file *file)
  712. {
  713. struct inotify_device *dev = file->private_data;
  714. /*
  715. * Destroy all of the watches on this device. Unfortunately, not very
  716. * pretty. We cannot do a simple iteration over the list, because we
  717. * do not know the inode until we iterate to the watch. But we need to
  718. * hold inode->inotify_mutex before dev->mutex. The following works.
  719. */
  720. while (1) {
  721. struct inotify_watch *watch;
  722. struct list_head *watches;
  723. struct inode *inode;
  724. mutex_lock(&dev->mutex);
  725. watches = &dev->watches;
  726. if (list_empty(watches)) {
  727. mutex_unlock(&dev->mutex);
  728. break;
  729. }
  730. watch = list_entry(watches->next, struct inotify_watch, d_list);
  731. get_inotify_watch(watch);
  732. mutex_unlock(&dev->mutex);
  733. inode = watch->inode;
  734. mutex_lock(&inode->inotify_mutex);
  735. mutex_lock(&dev->mutex);
  736. remove_watch_no_event(watch, dev);
  737. mutex_unlock(&dev->mutex);
  738. mutex_unlock(&inode->inotify_mutex);
  739. put_inotify_watch(watch);
  740. }
  741. /* destroy all of the events on this device */
  742. mutex_lock(&dev->mutex);
  743. while (!list_empty(&dev->events))
  744. inotify_dev_event_dequeue(dev);
  745. mutex_unlock(&dev->mutex);
  746. /* free this device: the put matching the get in inotify_init() */
  747. put_inotify_dev(dev);
  748. return 0;
  749. }
  750. /*
  751. * inotify_ignore - remove a given wd from this inotify instance.
  752. *
  753. * Can sleep.
  754. */
  755. static int inotify_ignore(struct inotify_device *dev, s32 wd)
  756. {
  757. struct inotify_watch *watch;
  758. struct inode *inode;
  759. mutex_lock(&dev->mutex);
  760. watch = idr_find(&dev->idr, wd);
  761. if (unlikely(!watch)) {
  762. mutex_unlock(&dev->mutex);
  763. return -EINVAL;
  764. }
  765. get_inotify_watch(watch);
  766. inode = watch->inode;
  767. mutex_unlock(&dev->mutex);
  768. mutex_lock(&inode->inotify_mutex);
  769. mutex_lock(&dev->mutex);
  770. /* make sure that we did not race */
  771. watch = idr_find(&dev->idr, wd);
  772. if (likely(watch))
  773. remove_watch(watch, dev);
  774. mutex_unlock(&dev->mutex);
  775. mutex_unlock(&inode->inotify_mutex);
  776. put_inotify_watch(watch);
  777. return 0;
  778. }
  779. static long inotify_ioctl(struct file *file, unsigned int cmd,
  780. unsigned long arg)
  781. {
  782. struct inotify_device *dev;
  783. void __user *p;
  784. int ret = -ENOTTY;
  785. dev = file->private_data;
  786. p = (void __user *) arg;
  787. switch (cmd) {
  788. case FIONREAD:
  789. ret = put_user(dev->queue_size, (int __user *) p);
  790. break;
  791. }
  792. return ret;
  793. }
  794. static const struct file_operations inotify_fops = {
  795. .poll = inotify_poll,
  796. .read = inotify_read,
  797. .release = inotify_release,
  798. .unlocked_ioctl = inotify_ioctl,
  799. .compat_ioctl = inotify_ioctl,
  800. };
  801. asmlinkage long sys_inotify_init(void)
  802. {
  803. struct inotify_device *dev;
  804. struct user_struct *user;
  805. struct file *filp;
  806. int fd, ret;
  807. fd = get_unused_fd();
  808. if (fd < 0)
  809. return fd;
  810. filp = get_empty_filp();
  811. if (!filp) {
  812. ret = -ENFILE;
  813. goto out_put_fd;
  814. }
  815. user = get_uid(current->user);
  816. if (unlikely(atomic_read(&user->inotify_devs) >=
  817. inotify_max_user_instances)) {
  818. ret = -EMFILE;
  819. goto out_free_uid;
  820. }
  821. dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
  822. if (unlikely(!dev)) {
  823. ret = -ENOMEM;
  824. goto out_free_uid;
  825. }
  826. filp->f_op = &inotify_fops;
  827. filp->f_vfsmnt = mntget(inotify_mnt);
  828. filp->f_dentry = dget(inotify_mnt->mnt_root);
  829. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  830. filp->f_mode = FMODE_READ;
  831. filp->f_flags = O_RDONLY;
  832. filp->private_data = dev;
  833. idr_init(&dev->idr);
  834. INIT_LIST_HEAD(&dev->events);
  835. INIT_LIST_HEAD(&dev->watches);
  836. init_waitqueue_head(&dev->wq);
  837. mutex_init(&dev->mutex);
  838. dev->event_count = 0;
  839. dev->queue_size = 0;
  840. dev->max_events = inotify_max_queued_events;
  841. dev->user = user;
  842. dev->last_wd = 0;
  843. atomic_set(&dev->count, 0);
  844. get_inotify_dev(dev);
  845. atomic_inc(&user->inotify_devs);
  846. fd_install(fd, filp);
  847. return fd;
  848. out_free_uid:
  849. free_uid(user);
  850. put_filp(filp);
  851. out_put_fd:
  852. put_unused_fd(fd);
  853. return ret;
  854. }
  855. asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
  856. {
  857. struct inotify_watch *watch, *old;
  858. struct inode *inode;
  859. struct inotify_device *dev;
  860. struct nameidata nd;
  861. struct file *filp;
  862. int ret, fput_needed;
  863. int mask_add = 0;
  864. unsigned flags = 0;
  865. filp = fget_light(fd, &fput_needed);
  866. if (unlikely(!filp))
  867. return -EBADF;
  868. /* verify that this is indeed an inotify instance */
  869. if (unlikely(filp->f_op != &inotify_fops)) {
  870. ret = -EINVAL;
  871. goto fput_and_out;
  872. }
  873. if (!(mask & IN_DONT_FOLLOW))
  874. flags |= LOOKUP_FOLLOW;
  875. if (mask & IN_ONLYDIR)
  876. flags |= LOOKUP_DIRECTORY;
  877. ret = find_inode(path, &nd, flags);
  878. if (unlikely(ret))
  879. goto fput_and_out;
  880. /* inode held in place by reference to nd; dev by fget on fd */
  881. inode = nd.dentry->d_inode;
  882. dev = filp->private_data;
  883. mutex_lock(&inode->inotify_mutex);
  884. mutex_lock(&dev->mutex);
  885. if (mask & IN_MASK_ADD)
  886. mask_add = 1;
  887. /* don't let user-space set invalid bits: we don't want flags set */
  888. mask &= IN_ALL_EVENTS | IN_ONESHOT;
  889. if (unlikely(!mask)) {
  890. ret = -EINVAL;
  891. goto out;
  892. }
  893. /*
  894. * Handle the case of re-adding a watch on an (inode,dev) pair that we
  895. * are already watching. We just update the mask and return its wd.
  896. */
  897. old = inode_find_dev(inode, dev);
  898. if (unlikely(old)) {
  899. if (mask_add)
  900. old->mask |= mask;
  901. else
  902. old->mask = mask;
  903. ret = old->wd;
  904. goto out;
  905. }
  906. watch = create_watch(dev, mask, inode);
  907. if (unlikely(IS_ERR(watch))) {
  908. ret = PTR_ERR(watch);
  909. goto out;
  910. }
  911. if (!inotify_inode_watched(inode))
  912. set_dentry_child_flags(inode, 1);
  913. /* Add the watch to the device's and the inode's list */
  914. list_add(&watch->d_list, &dev->watches);
  915. list_add(&watch->i_list, &inode->inotify_watches);
  916. ret = watch->wd;
  917. out:
  918. mutex_unlock(&dev->mutex);
  919. mutex_unlock(&inode->inotify_mutex);
  920. path_release(&nd);
  921. fput_and_out:
  922. fput_light(filp, fput_needed);
  923. return ret;
  924. }
  925. asmlinkage long sys_inotify_rm_watch(int fd, u32 wd)
  926. {
  927. struct file *filp;
  928. struct inotify_device *dev;
  929. int ret, fput_needed;
  930. filp = fget_light(fd, &fput_needed);
  931. if (unlikely(!filp))
  932. return -EBADF;
  933. /* verify that this is indeed an inotify instance */
  934. if (unlikely(filp->f_op != &inotify_fops)) {
  935. ret = -EINVAL;
  936. goto out;
  937. }
  938. dev = filp->private_data;
  939. ret = inotify_ignore(dev, wd);
  940. out:
  941. fput_light(filp, fput_needed);
  942. return ret;
  943. }
  944. static struct super_block *
  945. inotify_get_sb(struct file_system_type *fs_type, int flags,
  946. const char *dev_name, void *data)
  947. {
  948. return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA);
  949. }
  950. static struct file_system_type inotify_fs_type = {
  951. .name = "inotifyfs",
  952. .get_sb = inotify_get_sb,
  953. .kill_sb = kill_anon_super,
  954. };
  955. /*
  956. * inotify_setup - Our initialization function. Note that we cannnot return
  957. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  958. * must result in panic().
  959. */
  960. static int __init inotify_setup(void)
  961. {
  962. int ret;
  963. ret = register_filesystem(&inotify_fs_type);
  964. if (unlikely(ret))
  965. panic("inotify: register_filesystem returned %d!\n", ret);
  966. inotify_mnt = kern_mount(&inotify_fs_type);
  967. if (IS_ERR(inotify_mnt))
  968. panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
  969. inotify_max_queued_events = 16384;
  970. inotify_max_user_instances = 128;
  971. inotify_max_user_watches = 8192;
  972. atomic_set(&inotify_cookie, 0);
  973. watch_cachep = kmem_cache_create("inotify_watch_cache",
  974. sizeof(struct inotify_watch),
  975. 0, SLAB_PANIC, NULL, NULL);
  976. event_cachep = kmem_cache_create("inotify_event_cache",
  977. sizeof(struct inotify_kernel_event),
  978. 0, SLAB_PANIC, NULL, NULL);
  979. return 0;
  980. }
  981. module_init(inotify_setup);