inotify.c 27 KB

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