inotify.c 27 KB

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