inotify.c 27 KB

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