inotify.c 26 KB

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