inotify.c 26 KB

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