inotify.c 26 KB

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