inotify.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 <asm/ioctls.h>
  36. static atomic_t inotify_cookie;
  37. static atomic_t inotify_watches;
  38. static kmem_cache_t *watch_cachep;
  39. static kmem_cache_t *event_cachep;
  40. static struct vfsmount *inotify_mnt;
  41. /* these are configurable via /proc/sys/fs/inotify/ */
  42. int inotify_max_user_instances;
  43. int inotify_max_user_watches;
  44. int inotify_max_queued_events;
  45. /*
  46. * Lock ordering:
  47. *
  48. * dentry->d_lock (used to keep d_move() away from dentry->d_parent)
  49. * iprune_sem (synchronize shrink_icache_memory())
  50. * inode_lock (protects the super_block->s_inodes list)
  51. * inode->inotify_sem (protects inode->inotify_watches and watches->i_list)
  52. * inotify_dev->sem (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 semaphore 'sem'.
  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 semaphore sem; /* 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->sem 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->sem of the associated watch->dev.
  104. * i_list and mask are protected by inode->inotify_sem 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->sem.
  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->sem. 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->sem.
  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->sem.
  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->sem. 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. {
  325. int error;
  326. error = __user_walk(dirname, LOOKUP_FOLLOW, nd);
  327. if (error)
  328. return error;
  329. /* you can only watch an inode if you have read permissions on it */
  330. error = permission(nd->dentry->d_inode, MAY_READ, NULL);
  331. if (error)
  332. path_release(nd);
  333. return error;
  334. }
  335. /*
  336. * create_watch - creates a watch on the given device.
  337. *
  338. * Callers must hold dev->sem. Calls inotify_dev_get_wd() so may sleep.
  339. * Both 'dev' and 'inode' (by way of nameidata) need to be pinned.
  340. */
  341. static struct inotify_watch *create_watch(struct inotify_device *dev,
  342. u32 mask, struct inode *inode)
  343. {
  344. struct inotify_watch *watch;
  345. int ret;
  346. if (atomic_read(&dev->user->inotify_watches) >=
  347. inotify_max_user_watches)
  348. return ERR_PTR(-ENOSPC);
  349. watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
  350. if (unlikely(!watch))
  351. return ERR_PTR(-ENOMEM);
  352. ret = inotify_dev_get_wd(dev, watch);
  353. if (unlikely(ret)) {
  354. kmem_cache_free(watch_cachep, watch);
  355. return ERR_PTR(ret);
  356. }
  357. dev->last_wd = watch->wd;
  358. watch->mask = mask;
  359. atomic_set(&watch->count, 0);
  360. INIT_LIST_HEAD(&watch->d_list);
  361. INIT_LIST_HEAD(&watch->i_list);
  362. /* save a reference to device and bump the count to make it official */
  363. get_inotify_dev(dev);
  364. watch->dev = dev;
  365. /*
  366. * Save a reference to the inode and bump the ref count to make it
  367. * official. We hold a reference to nameidata, which makes this safe.
  368. */
  369. watch->inode = igrab(inode);
  370. /* bump our own count, corresponding to our entry in dev->watches */
  371. get_inotify_watch(watch);
  372. atomic_inc(&dev->user->inotify_watches);
  373. atomic_inc(&inotify_watches);
  374. return watch;
  375. }
  376. /*
  377. * inotify_find_dev - find the watch associated with the given inode and dev
  378. *
  379. * Callers must hold inode->inotify_sem.
  380. */
  381. static struct inotify_watch *inode_find_dev(struct inode *inode,
  382. struct inotify_device *dev)
  383. {
  384. struct inotify_watch *watch;
  385. list_for_each_entry(watch, &inode->inotify_watches, i_list) {
  386. if (watch->dev == dev)
  387. return watch;
  388. }
  389. return NULL;
  390. }
  391. /*
  392. * remove_watch_no_event - remove_watch() without the IN_IGNORED event.
  393. */
  394. static void remove_watch_no_event(struct inotify_watch *watch,
  395. struct inotify_device *dev)
  396. {
  397. list_del(&watch->i_list);
  398. list_del(&watch->d_list);
  399. atomic_dec(&dev->user->inotify_watches);
  400. atomic_dec(&inotify_watches);
  401. idr_remove(&dev->idr, watch->wd);
  402. put_inotify_watch(watch);
  403. }
  404. /*
  405. * remove_watch - Remove a watch from both the device and the inode. Sends
  406. * the IN_IGNORED event to the given device signifying that the inode is no
  407. * longer watched.
  408. *
  409. * Callers must hold both inode->inotify_sem and dev->sem. We drop a
  410. * reference to the inode before returning.
  411. *
  412. * The inode is not iput() so as to remain atomic. If the inode needs to be
  413. * iput(), the call returns one. Otherwise, it returns zero.
  414. */
  415. static void remove_watch(struct inotify_watch *watch,struct inotify_device *dev)
  416. {
  417. inotify_dev_queue_event(dev, watch, IN_IGNORED, 0, NULL);
  418. remove_watch_no_event(watch, dev);
  419. }
  420. /*
  421. * inotify_inode_watched - returns nonzero if there are watches on this inode
  422. * and zero otherwise. We call this lockless, we do not care if we race.
  423. */
  424. static inline int inotify_inode_watched(struct inode *inode)
  425. {
  426. return !list_empty(&inode->inotify_watches);
  427. }
  428. /* Kernel API */
  429. /**
  430. * inotify_inode_queue_event - queue an event to all watches on this inode
  431. * @inode: inode event is originating from
  432. * @mask: event mask describing this event
  433. * @cookie: cookie for synchronization, or zero
  434. * @name: filename, if any
  435. */
  436. void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,
  437. const char *name)
  438. {
  439. struct inotify_watch *watch, *next;
  440. if (!inotify_inode_watched(inode))
  441. return;
  442. down(&inode->inotify_sem);
  443. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  444. u32 watch_mask = watch->mask;
  445. if (watch_mask & mask) {
  446. struct inotify_device *dev = watch->dev;
  447. get_inotify_watch(watch);
  448. down(&dev->sem);
  449. inotify_dev_queue_event(dev, watch, mask, cookie, name);
  450. if (watch_mask & IN_ONESHOT)
  451. remove_watch_no_event(watch, dev);
  452. up(&dev->sem);
  453. put_inotify_watch(watch);
  454. }
  455. }
  456. up(&inode->inotify_sem);
  457. }
  458. EXPORT_SYMBOL_GPL(inotify_inode_queue_event);
  459. /**
  460. * inotify_dentry_parent_queue_event - queue an event to a dentry's parent
  461. * @dentry: the dentry in question, we queue against this dentry's parent
  462. * @mask: event mask describing this event
  463. * @cookie: cookie for synchronization, or zero
  464. * @name: filename, if any
  465. */
  466. void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,
  467. u32 cookie, const char *name)
  468. {
  469. struct dentry *parent;
  470. struct inode *inode;
  471. if (!atomic_read (&inotify_watches))
  472. return;
  473. spin_lock(&dentry->d_lock);
  474. parent = dentry->d_parent;
  475. inode = parent->d_inode;
  476. if (inotify_inode_watched(inode)) {
  477. dget(parent);
  478. spin_unlock(&dentry->d_lock);
  479. inotify_inode_queue_event(inode, mask, cookie, name);
  480. dput(parent);
  481. } else
  482. spin_unlock(&dentry->d_lock);
  483. }
  484. EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);
  485. /**
  486. * inotify_get_cookie - return a unique cookie for use in synchronizing events.
  487. */
  488. u32 inotify_get_cookie(void)
  489. {
  490. return atomic_inc_return(&inotify_cookie);
  491. }
  492. EXPORT_SYMBOL_GPL(inotify_get_cookie);
  493. /**
  494. * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.
  495. * @list: list of inodes being unmounted (sb->s_inodes)
  496. *
  497. * Called with inode_lock held, protecting the unmounting super block's list
  498. * of inodes, and with iprune_sem held, keeping shrink_icache_memory() at bay.
  499. * We temporarily drop inode_lock, however, and CAN block.
  500. */
  501. void inotify_unmount_inodes(struct list_head *list)
  502. {
  503. struct inode *inode, *next_i, *need_iput = NULL;
  504. list_for_each_entry_safe(inode, next_i, list, i_sb_list) {
  505. struct inotify_watch *watch, *next_w;
  506. struct inode *need_iput_tmp;
  507. struct list_head *watches;
  508. /*
  509. * If i_count is zero, the inode cannot have any watches and
  510. * doing an __iget/iput with MS_ACTIVE clear would actually
  511. * evict all inodes with zero i_count from icache which is
  512. * unnecessarily violent and may in fact be illegal to do.
  513. */
  514. if (!atomic_read(&inode->i_count))
  515. continue;
  516. /*
  517. * We cannot __iget() an inode in state I_CLEAR, I_FREEING, or
  518. * I_WILL_FREE which is fine because by that point the inode
  519. * cannot have any associated watches.
  520. */
  521. if (inode->i_state & (I_CLEAR | I_FREEING | I_WILL_FREE))
  522. continue;
  523. need_iput_tmp = need_iput;
  524. need_iput = NULL;
  525. /* In case the remove_watch() drops a reference. */
  526. if (inode != need_iput_tmp)
  527. __iget(inode);
  528. else
  529. need_iput_tmp = NULL;
  530. /* In case the dropping of a reference would nuke next_i. */
  531. if ((&next_i->i_sb_list != list) &&
  532. atomic_read(&next_i->i_count) &&
  533. !(next_i->i_state & (I_CLEAR | I_FREEING |
  534. I_WILL_FREE))) {
  535. __iget(next_i);
  536. need_iput = next_i;
  537. }
  538. /*
  539. * We can safely drop inode_lock here because we hold
  540. * references on both inode and next_i. Also no new inodes
  541. * will be added since the umount has begun. Finally,
  542. * iprune_sem keeps shrink_icache_memory() away.
  543. */
  544. spin_unlock(&inode_lock);
  545. if (need_iput_tmp)
  546. iput(need_iput_tmp);
  547. /* for each watch, send IN_UNMOUNT and then remove it */
  548. down(&inode->inotify_sem);
  549. watches = &inode->inotify_watches;
  550. list_for_each_entry_safe(watch, next_w, watches, i_list) {
  551. struct inotify_device *dev = watch->dev;
  552. down(&dev->sem);
  553. inotify_dev_queue_event(dev, watch, IN_UNMOUNT,0,NULL);
  554. remove_watch(watch, dev);
  555. up(&dev->sem);
  556. }
  557. up(&inode->inotify_sem);
  558. iput(inode);
  559. spin_lock(&inode_lock);
  560. }
  561. }
  562. EXPORT_SYMBOL_GPL(inotify_unmount_inodes);
  563. /**
  564. * inotify_inode_is_dead - an inode has been deleted, cleanup any watches
  565. * @inode: inode that is about to be removed
  566. */
  567. void inotify_inode_is_dead(struct inode *inode)
  568. {
  569. struct inotify_watch *watch, *next;
  570. down(&inode->inotify_sem);
  571. list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {
  572. struct inotify_device *dev = watch->dev;
  573. down(&dev->sem);
  574. remove_watch(watch, dev);
  575. up(&dev->sem);
  576. }
  577. up(&inode->inotify_sem);
  578. }
  579. EXPORT_SYMBOL_GPL(inotify_inode_is_dead);
  580. /* Device Interface */
  581. static unsigned int inotify_poll(struct file *file, poll_table *wait)
  582. {
  583. struct inotify_device *dev = file->private_data;
  584. int ret = 0;
  585. poll_wait(file, &dev->wq, wait);
  586. down(&dev->sem);
  587. if (!list_empty(&dev->events))
  588. ret = POLLIN | POLLRDNORM;
  589. up(&dev->sem);
  590. return ret;
  591. }
  592. static ssize_t inotify_read(struct file *file, char __user *buf,
  593. size_t count, loff_t *pos)
  594. {
  595. size_t event_size = sizeof (struct inotify_event);
  596. struct inotify_device *dev;
  597. char __user *start;
  598. int ret;
  599. DEFINE_WAIT(wait);
  600. start = buf;
  601. dev = file->private_data;
  602. while (1) {
  603. int events;
  604. prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
  605. down(&dev->sem);
  606. events = !list_empty(&dev->events);
  607. up(&dev->sem);
  608. if (events) {
  609. ret = 0;
  610. break;
  611. }
  612. if (file->f_flags & O_NONBLOCK) {
  613. ret = -EAGAIN;
  614. break;
  615. }
  616. if (signal_pending(current)) {
  617. ret = -EINTR;
  618. break;
  619. }
  620. schedule();
  621. }
  622. finish_wait(&dev->wq, &wait);
  623. if (ret)
  624. return ret;
  625. down(&dev->sem);
  626. while (1) {
  627. struct inotify_kernel_event *kevent;
  628. ret = buf - start;
  629. if (list_empty(&dev->events))
  630. break;
  631. kevent = inotify_dev_get_event(dev);
  632. if (event_size + kevent->event.len > count)
  633. break;
  634. if (copy_to_user(buf, &kevent->event, event_size)) {
  635. ret = -EFAULT;
  636. break;
  637. }
  638. buf += event_size;
  639. count -= event_size;
  640. if (kevent->name) {
  641. if (copy_to_user(buf, kevent->name, kevent->event.len)){
  642. ret = -EFAULT;
  643. break;
  644. }
  645. buf += kevent->event.len;
  646. count -= kevent->event.len;
  647. }
  648. remove_kevent(dev, kevent);
  649. }
  650. up(&dev->sem);
  651. return ret;
  652. }
  653. static int inotify_release(struct inode *ignored, struct file *file)
  654. {
  655. struct inotify_device *dev = file->private_data;
  656. /*
  657. * Destroy all of the watches on this device. Unfortunately, not very
  658. * pretty. We cannot do a simple iteration over the list, because we
  659. * do not know the inode until we iterate to the watch. But we need to
  660. * hold inode->inotify_sem before dev->sem. The following works.
  661. */
  662. while (1) {
  663. struct inotify_watch *watch;
  664. struct list_head *watches;
  665. struct inode *inode;
  666. down(&dev->sem);
  667. watches = &dev->watches;
  668. if (list_empty(watches)) {
  669. up(&dev->sem);
  670. break;
  671. }
  672. watch = list_entry(watches->next, struct inotify_watch, d_list);
  673. get_inotify_watch(watch);
  674. up(&dev->sem);
  675. inode = watch->inode;
  676. down(&inode->inotify_sem);
  677. down(&dev->sem);
  678. remove_watch_no_event(watch, dev);
  679. up(&dev->sem);
  680. up(&inode->inotify_sem);
  681. put_inotify_watch(watch);
  682. }
  683. /* destroy all of the events on this device */
  684. down(&dev->sem);
  685. while (!list_empty(&dev->events))
  686. inotify_dev_event_dequeue(dev);
  687. up(&dev->sem);
  688. /* free this device: the put matching the get in inotify_init() */
  689. put_inotify_dev(dev);
  690. return 0;
  691. }
  692. /*
  693. * inotify_ignore - remove a given wd from this inotify instance.
  694. *
  695. * Can sleep.
  696. */
  697. static int inotify_ignore(struct inotify_device *dev, s32 wd)
  698. {
  699. struct inotify_watch *watch;
  700. struct inode *inode;
  701. down(&dev->sem);
  702. watch = idr_find(&dev->idr, wd);
  703. if (unlikely(!watch)) {
  704. up(&dev->sem);
  705. return -EINVAL;
  706. }
  707. get_inotify_watch(watch);
  708. inode = watch->inode;
  709. up(&dev->sem);
  710. down(&inode->inotify_sem);
  711. down(&dev->sem);
  712. /* make sure that we did not race */
  713. watch = idr_find(&dev->idr, wd);
  714. if (likely(watch))
  715. remove_watch(watch, dev);
  716. up(&dev->sem);
  717. up(&inode->inotify_sem);
  718. put_inotify_watch(watch);
  719. return 0;
  720. }
  721. static long inotify_ioctl(struct file *file, unsigned int cmd,
  722. unsigned long arg)
  723. {
  724. struct inotify_device *dev;
  725. void __user *p;
  726. int ret = -ENOTTY;
  727. dev = file->private_data;
  728. p = (void __user *) arg;
  729. switch (cmd) {
  730. case FIONREAD:
  731. ret = put_user(dev->queue_size, (int __user *) p);
  732. break;
  733. }
  734. return ret;
  735. }
  736. static struct file_operations inotify_fops = {
  737. .poll = inotify_poll,
  738. .read = inotify_read,
  739. .release = inotify_release,
  740. .unlocked_ioctl = inotify_ioctl,
  741. .compat_ioctl = inotify_ioctl,
  742. };
  743. asmlinkage long sys_inotify_init(void)
  744. {
  745. struct inotify_device *dev;
  746. struct user_struct *user;
  747. struct file *filp;
  748. int fd, ret;
  749. fd = get_unused_fd();
  750. if (fd < 0)
  751. return fd;
  752. filp = get_empty_filp();
  753. if (!filp) {
  754. ret = -ENFILE;
  755. goto out_put_fd;
  756. }
  757. user = get_uid(current->user);
  758. if (unlikely(atomic_read(&user->inotify_devs) >=
  759. inotify_max_user_instances)) {
  760. ret = -EMFILE;
  761. goto out_free_uid;
  762. }
  763. dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
  764. if (unlikely(!dev)) {
  765. ret = -ENOMEM;
  766. goto out_free_uid;
  767. }
  768. filp->f_op = &inotify_fops;
  769. filp->f_vfsmnt = mntget(inotify_mnt);
  770. filp->f_dentry = dget(inotify_mnt->mnt_root);
  771. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  772. filp->f_mode = FMODE_READ;
  773. filp->f_flags = O_RDONLY;
  774. filp->private_data = dev;
  775. idr_init(&dev->idr);
  776. INIT_LIST_HEAD(&dev->events);
  777. INIT_LIST_HEAD(&dev->watches);
  778. init_waitqueue_head(&dev->wq);
  779. sema_init(&dev->sem, 1);
  780. dev->event_count = 0;
  781. dev->queue_size = 0;
  782. dev->max_events = inotify_max_queued_events;
  783. dev->user = user;
  784. dev->last_wd = 0;
  785. atomic_set(&dev->count, 0);
  786. get_inotify_dev(dev);
  787. atomic_inc(&user->inotify_devs);
  788. fd_install(fd, filp);
  789. return fd;
  790. out_free_uid:
  791. free_uid(user);
  792. put_filp(filp);
  793. out_put_fd:
  794. put_unused_fd(fd);
  795. return ret;
  796. }
  797. asmlinkage long sys_inotify_add_watch(int fd, const char __user *path, u32 mask)
  798. {
  799. struct inotify_watch *watch, *old;
  800. struct inode *inode;
  801. struct inotify_device *dev;
  802. struct nameidata nd;
  803. struct file *filp;
  804. int ret, fput_needed;
  805. int mask_add = 0;
  806. filp = fget_light(fd, &fput_needed);
  807. if (unlikely(!filp))
  808. return -EBADF;
  809. /* verify that this is indeed an inotify instance */
  810. if (unlikely(filp->f_op != &inotify_fops)) {
  811. ret = -EINVAL;
  812. goto fput_and_out;
  813. }
  814. ret = find_inode(path, &nd);
  815. if (unlikely(ret))
  816. goto fput_and_out;
  817. /* inode held in place by reference to nd; dev by fget on fd */
  818. inode = nd.dentry->d_inode;
  819. dev = filp->private_data;
  820. down(&inode->inotify_sem);
  821. down(&dev->sem);
  822. if (mask & IN_MASK_ADD)
  823. mask_add = 1;
  824. /* don't let user-space set invalid bits: we don't want flags set */
  825. mask &= IN_ALL_EVENTS;
  826. if (unlikely(!mask)) {
  827. ret = -EINVAL;
  828. goto out;
  829. }
  830. /*
  831. * Handle the case of re-adding a watch on an (inode,dev) pair that we
  832. * are already watching. We just update the mask and return its wd.
  833. */
  834. old = inode_find_dev(inode, dev);
  835. if (unlikely(old)) {
  836. if (mask_add)
  837. old->mask |= mask;
  838. else
  839. old->mask = mask;
  840. ret = old->wd;
  841. goto out;
  842. }
  843. watch = create_watch(dev, mask, inode);
  844. if (unlikely(IS_ERR(watch))) {
  845. ret = PTR_ERR(watch);
  846. goto out;
  847. }
  848. /* Add the watch to the device's and the inode's list */
  849. list_add(&watch->d_list, &dev->watches);
  850. list_add(&watch->i_list, &inode->inotify_watches);
  851. ret = watch->wd;
  852. out:
  853. up(&dev->sem);
  854. up(&inode->inotify_sem);
  855. path_release(&nd);
  856. fput_and_out:
  857. fput_light(filp, fput_needed);
  858. return ret;
  859. }
  860. asmlinkage long sys_inotify_rm_watch(int fd, u32 wd)
  861. {
  862. struct file *filp;
  863. struct inotify_device *dev;
  864. int ret, fput_needed;
  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 out;
  872. }
  873. dev = filp->private_data;
  874. ret = inotify_ignore(dev, wd);
  875. out:
  876. fput_light(filp, fput_needed);
  877. return ret;
  878. }
  879. static struct super_block *
  880. inotify_get_sb(struct file_system_type *fs_type, int flags,
  881. const char *dev_name, void *data)
  882. {
  883. return get_sb_pseudo(fs_type, "inotify", NULL, 0xBAD1DEA);
  884. }
  885. static struct file_system_type inotify_fs_type = {
  886. .name = "inotifyfs",
  887. .get_sb = inotify_get_sb,
  888. .kill_sb = kill_anon_super,
  889. };
  890. /*
  891. * inotify_setup - Our initialization function. Note that we cannnot return
  892. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  893. * must result in panic().
  894. */
  895. static int __init inotify_setup(void)
  896. {
  897. int ret;
  898. ret = register_filesystem(&inotify_fs_type);
  899. if (unlikely(ret))
  900. panic("inotify: register_filesystem returned %d!\n", ret);
  901. inotify_mnt = kern_mount(&inotify_fs_type);
  902. if (IS_ERR(inotify_mnt))
  903. panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
  904. inotify_max_queued_events = 16384;
  905. inotify_max_user_instances = 128;
  906. inotify_max_user_watches = 8192;
  907. atomic_set(&inotify_cookie, 0);
  908. atomic_set(&inotify_watches, 0);
  909. watch_cachep = kmem_cache_create("inotify_watch_cache",
  910. sizeof(struct inotify_watch),
  911. 0, SLAB_PANIC, NULL, NULL);
  912. event_cachep = kmem_cache_create("inotify_event_cache",
  913. sizeof(struct inotify_kernel_event),
  914. 0, SLAB_PANIC, NULL, NULL);
  915. return 0;
  916. }
  917. module_init(inotify_setup);