inotify_user.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /*
  2. * fs/inotify_user.c - inotify support for userspace
  3. *
  4. * Authors:
  5. * John McCutchan <ttb@tentacle.dhs.org>
  6. * Robert Love <rml@novell.com>
  7. *
  8. * Copyright (C) 2005 John McCutchan
  9. * Copyright 2006 Hewlett-Packard Development Company, L.P.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2, or (at your option) any
  14. * later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/fs.h>
  25. #include <linux/file.h>
  26. #include <linux/mount.h>
  27. #include <linux/namei.h>
  28. #include <linux/poll.h>
  29. #include <linux/init.h>
  30. #include <linux/list.h>
  31. #include <linux/inotify.h>
  32. #include <linux/syscalls.h>
  33. #include <linux/magic.h>
  34. #include <asm/ioctls.h>
  35. static struct kmem_cache *watch_cachep __read_mostly;
  36. static struct kmem_cache *event_cachep __read_mostly;
  37. static struct vfsmount *inotify_mnt __read_mostly;
  38. /* these are configurable via /proc/sys/fs/inotify/ */
  39. static int inotify_max_user_instances __read_mostly;
  40. static int inotify_max_user_watches __read_mostly;
  41. static int inotify_max_queued_events __read_mostly;
  42. /*
  43. * Lock ordering:
  44. *
  45. * inotify_dev->up_mutex (ensures we don't re-add the same watch)
  46. * inode->inotify_mutex (protects inode's watch list)
  47. * inotify_handle->mutex (protects inotify_handle's watch list)
  48. * inotify_dev->ev_mutex (protects device's event queue)
  49. */
  50. /*
  51. * Lifetimes of the main data structures:
  52. *
  53. * inotify_device: Lifetime is managed by reference count, from
  54. * sys_inotify_init() until release. Additional references can bump the count
  55. * via get_inotify_dev() and drop the count via put_inotify_dev().
  56. *
  57. * inotify_user_watch: Lifetime is from create_watch() to the receipt of an
  58. * IN_IGNORED event from inotify, or when using IN_ONESHOT, to receipt of the
  59. * first event, or to inotify_destroy().
  60. */
  61. /*
  62. * struct inotify_device - represents an inotify instance
  63. *
  64. * This structure is protected by the mutex 'mutex'.
  65. */
  66. struct inotify_device {
  67. wait_queue_head_t wq; /* wait queue for i/o */
  68. struct mutex ev_mutex; /* protects event queue */
  69. struct mutex up_mutex; /* synchronizes watch updates */
  70. struct list_head events; /* list of queued events */
  71. struct user_struct *user; /* user who opened this dev */
  72. struct inotify_handle *ih; /* inotify handle */
  73. struct fasync_struct *fa; /* async notification */
  74. atomic_t count; /* reference count */
  75. unsigned int queue_size; /* size of the queue (bytes) */
  76. unsigned int event_count; /* number of pending events */
  77. unsigned int max_events; /* maximum number of events */
  78. };
  79. /*
  80. * struct inotify_kernel_event - An inotify event, originating from a watch and
  81. * queued for user-space. A list of these is attached to each instance of the
  82. * device. In read(), this list is walked and all events that can fit in the
  83. * buffer are returned.
  84. *
  85. * Protected by dev->ev_mutex of the device in which we are queued.
  86. */
  87. struct inotify_kernel_event {
  88. struct inotify_event event; /* the user-space event */
  89. struct list_head list; /* entry in inotify_device's list */
  90. char *name; /* filename, if any */
  91. };
  92. /*
  93. * struct inotify_user_watch - our version of an inotify_watch, we add
  94. * a reference to the associated inotify_device.
  95. */
  96. struct inotify_user_watch {
  97. struct inotify_device *dev; /* associated device */
  98. struct inotify_watch wdata; /* inotify watch data */
  99. };
  100. #ifdef CONFIG_SYSCTL
  101. #include <linux/sysctl.h>
  102. static int zero;
  103. ctl_table inotify_table[] = {
  104. {
  105. .ctl_name = INOTIFY_MAX_USER_INSTANCES,
  106. .procname = "max_user_instances",
  107. .data = &inotify_max_user_instances,
  108. .maxlen = sizeof(int),
  109. .mode = 0644,
  110. .proc_handler = &proc_dointvec_minmax,
  111. .strategy = &sysctl_intvec,
  112. .extra1 = &zero,
  113. },
  114. {
  115. .ctl_name = INOTIFY_MAX_USER_WATCHES,
  116. .procname = "max_user_watches",
  117. .data = &inotify_max_user_watches,
  118. .maxlen = sizeof(int),
  119. .mode = 0644,
  120. .proc_handler = &proc_dointvec_minmax,
  121. .strategy = &sysctl_intvec,
  122. .extra1 = &zero,
  123. },
  124. {
  125. .ctl_name = INOTIFY_MAX_QUEUED_EVENTS,
  126. .procname = "max_queued_events",
  127. .data = &inotify_max_queued_events,
  128. .maxlen = sizeof(int),
  129. .mode = 0644,
  130. .proc_handler = &proc_dointvec_minmax,
  131. .strategy = &sysctl_intvec,
  132. .extra1 = &zero
  133. },
  134. { .ctl_name = 0 }
  135. };
  136. #endif /* CONFIG_SYSCTL */
  137. static inline void get_inotify_dev(struct inotify_device *dev)
  138. {
  139. atomic_inc(&dev->count);
  140. }
  141. static inline void put_inotify_dev(struct inotify_device *dev)
  142. {
  143. if (atomic_dec_and_test(&dev->count)) {
  144. atomic_dec(&dev->user->inotify_devs);
  145. free_uid(dev->user);
  146. kfree(dev);
  147. }
  148. }
  149. /*
  150. * free_inotify_user_watch - cleans up the watch and its references
  151. */
  152. static void free_inotify_user_watch(struct inotify_watch *w)
  153. {
  154. struct inotify_user_watch *watch;
  155. struct inotify_device *dev;
  156. watch = container_of(w, struct inotify_user_watch, wdata);
  157. dev = watch->dev;
  158. atomic_dec(&dev->user->inotify_watches);
  159. put_inotify_dev(dev);
  160. kmem_cache_free(watch_cachep, watch);
  161. }
  162. /*
  163. * kernel_event - create a new kernel event with the given parameters
  164. *
  165. * This function can sleep.
  166. */
  167. static struct inotify_kernel_event * kernel_event(s32 wd, u32 mask, u32 cookie,
  168. const char *name)
  169. {
  170. struct inotify_kernel_event *kevent;
  171. kevent = kmem_cache_alloc(event_cachep, GFP_NOFS);
  172. if (unlikely(!kevent))
  173. return NULL;
  174. /* we hand this out to user-space, so zero it just in case */
  175. memset(&kevent->event, 0, sizeof(struct inotify_event));
  176. kevent->event.wd = wd;
  177. kevent->event.mask = mask;
  178. kevent->event.cookie = cookie;
  179. INIT_LIST_HEAD(&kevent->list);
  180. if (name) {
  181. size_t len, rem, event_size = sizeof(struct inotify_event);
  182. /*
  183. * We need to pad the filename so as to properly align an
  184. * array of inotify_event structures. Because the structure is
  185. * small and the common case is a small filename, we just round
  186. * up to the next multiple of the structure's sizeof. This is
  187. * simple and safe for all architectures.
  188. */
  189. len = strlen(name) + 1;
  190. rem = event_size - len;
  191. if (len > event_size) {
  192. rem = event_size - (len % event_size);
  193. if (len % event_size == 0)
  194. rem = 0;
  195. }
  196. kevent->name = kmalloc(len + rem, GFP_KERNEL);
  197. if (unlikely(!kevent->name)) {
  198. kmem_cache_free(event_cachep, kevent);
  199. return NULL;
  200. }
  201. memcpy(kevent->name, name, len);
  202. if (rem)
  203. memset(kevent->name + len, 0, rem);
  204. kevent->event.len = len + rem;
  205. } else {
  206. kevent->event.len = 0;
  207. kevent->name = NULL;
  208. }
  209. return kevent;
  210. }
  211. /*
  212. * inotify_dev_get_event - return the next event in the given dev's queue
  213. *
  214. * Caller must hold dev->ev_mutex.
  215. */
  216. static inline struct inotify_kernel_event *
  217. inotify_dev_get_event(struct inotify_device *dev)
  218. {
  219. return list_entry(dev->events.next, struct inotify_kernel_event, list);
  220. }
  221. /*
  222. * inotify_dev_get_last_event - return the last event in the given dev's queue
  223. *
  224. * Caller must hold dev->ev_mutex.
  225. */
  226. static inline struct inotify_kernel_event *
  227. inotify_dev_get_last_event(struct inotify_device *dev)
  228. {
  229. if (list_empty(&dev->events))
  230. return NULL;
  231. return list_entry(dev->events.prev, struct inotify_kernel_event, list);
  232. }
  233. /*
  234. * inotify_dev_queue_event - event handler registered with core inotify, adds
  235. * a new event to the given device
  236. *
  237. * Can sleep (calls kernel_event()).
  238. */
  239. static void inotify_dev_queue_event(struct inotify_watch *w, u32 wd, u32 mask,
  240. u32 cookie, const char *name,
  241. struct inode *ignored)
  242. {
  243. struct inotify_user_watch *watch;
  244. struct inotify_device *dev;
  245. struct inotify_kernel_event *kevent, *last;
  246. watch = container_of(w, struct inotify_user_watch, wdata);
  247. dev = watch->dev;
  248. mutex_lock(&dev->ev_mutex);
  249. /* we can safely put the watch as we don't reference it while
  250. * generating the event
  251. */
  252. if (mask & IN_IGNORED || w->mask & IN_ONESHOT)
  253. put_inotify_watch(w); /* final put */
  254. /* coalescing: drop this event if it is a dupe of the previous */
  255. last = inotify_dev_get_last_event(dev);
  256. if (last && last->event.mask == mask && last->event.wd == wd &&
  257. last->event.cookie == cookie) {
  258. const char *lastname = last->name;
  259. if (!name && !lastname)
  260. goto out;
  261. if (name && lastname && !strcmp(lastname, name))
  262. goto out;
  263. }
  264. /* the queue overflowed and we already sent the Q_OVERFLOW event */
  265. if (unlikely(dev->event_count > dev->max_events))
  266. goto out;
  267. /* if the queue overflows, we need to notify user space */
  268. if (unlikely(dev->event_count == dev->max_events))
  269. kevent = kernel_event(-1, IN_Q_OVERFLOW, cookie, NULL);
  270. else
  271. kevent = kernel_event(wd, mask, cookie, name);
  272. if (unlikely(!kevent))
  273. goto out;
  274. /* queue the event and wake up anyone waiting */
  275. dev->event_count++;
  276. dev->queue_size += sizeof(struct inotify_event) + kevent->event.len;
  277. list_add_tail(&kevent->list, &dev->events);
  278. wake_up_interruptible(&dev->wq);
  279. kill_fasync(&dev->fa, SIGIO, POLL_IN);
  280. out:
  281. mutex_unlock(&dev->ev_mutex);
  282. }
  283. /*
  284. * remove_kevent - cleans up the given kevent
  285. *
  286. * Caller must hold dev->ev_mutex.
  287. */
  288. static void remove_kevent(struct inotify_device *dev,
  289. struct inotify_kernel_event *kevent)
  290. {
  291. list_del(&kevent->list);
  292. dev->event_count--;
  293. dev->queue_size -= sizeof(struct inotify_event) + kevent->event.len;
  294. }
  295. /*
  296. * free_kevent - frees the given kevent.
  297. */
  298. static void free_kevent(struct inotify_kernel_event *kevent)
  299. {
  300. kfree(kevent->name);
  301. kmem_cache_free(event_cachep, kevent);
  302. }
  303. /*
  304. * inotify_dev_event_dequeue - destroy an event on the given device
  305. *
  306. * Caller must hold dev->ev_mutex.
  307. */
  308. static void inotify_dev_event_dequeue(struct inotify_device *dev)
  309. {
  310. if (!list_empty(&dev->events)) {
  311. struct inotify_kernel_event *kevent;
  312. kevent = inotify_dev_get_event(dev);
  313. remove_kevent(dev, kevent);
  314. free_kevent(kevent);
  315. }
  316. }
  317. /*
  318. * find_inode - resolve a user-given path to a specific inode
  319. */
  320. static int find_inode(const char __user *dirname, struct path *path,
  321. unsigned flags)
  322. {
  323. int error;
  324. error = user_path_at(AT_FDCWD, dirname, flags, path);
  325. if (error)
  326. return error;
  327. /* you can only watch an inode if you have read permissions on it */
  328. error = inode_permission(path->dentry->d_inode, MAY_READ);
  329. if (error)
  330. path_put(path);
  331. return error;
  332. }
  333. /*
  334. * create_watch - creates a watch on the given device.
  335. *
  336. * Callers must hold dev->up_mutex.
  337. */
  338. static int create_watch(struct inotify_device *dev, struct inode *inode,
  339. u32 mask)
  340. {
  341. struct inotify_user_watch *watch;
  342. int ret;
  343. if (atomic_read(&dev->user->inotify_watches) >=
  344. inotify_max_user_watches)
  345. return -ENOSPC;
  346. watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
  347. if (unlikely(!watch))
  348. return -ENOMEM;
  349. /* save a reference to device and bump the count to make it official */
  350. get_inotify_dev(dev);
  351. watch->dev = dev;
  352. atomic_inc(&dev->user->inotify_watches);
  353. inotify_init_watch(&watch->wdata);
  354. ret = inotify_add_watch(dev->ih, &watch->wdata, inode, mask);
  355. if (ret < 0)
  356. free_inotify_user_watch(&watch->wdata);
  357. return ret;
  358. }
  359. /* Device Interface */
  360. static unsigned int inotify_poll(struct file *file, poll_table *wait)
  361. {
  362. struct inotify_device *dev = file->private_data;
  363. int ret = 0;
  364. poll_wait(file, &dev->wq, wait);
  365. mutex_lock(&dev->ev_mutex);
  366. if (!list_empty(&dev->events))
  367. ret = POLLIN | POLLRDNORM;
  368. mutex_unlock(&dev->ev_mutex);
  369. return ret;
  370. }
  371. /*
  372. * Get an inotify_kernel_event if one exists and is small
  373. * enough to fit in "count". Return an error pointer if
  374. * not large enough.
  375. *
  376. * Called with the device ev_mutex held.
  377. */
  378. static struct inotify_kernel_event *get_one_event(struct inotify_device *dev,
  379. size_t count)
  380. {
  381. size_t event_size = sizeof(struct inotify_event);
  382. struct inotify_kernel_event *kevent;
  383. if (list_empty(&dev->events))
  384. return NULL;
  385. kevent = inotify_dev_get_event(dev);
  386. if (kevent->name)
  387. event_size += kevent->event.len;
  388. if (event_size > count)
  389. return ERR_PTR(-EINVAL);
  390. remove_kevent(dev, kevent);
  391. return kevent;
  392. }
  393. /*
  394. * Copy an event to user space, returning how much we copied.
  395. *
  396. * We already checked that the event size is smaller than the
  397. * buffer we had in "get_one_event()" above.
  398. */
  399. static ssize_t copy_event_to_user(struct inotify_kernel_event *kevent,
  400. char __user *buf)
  401. {
  402. size_t event_size = sizeof(struct inotify_event);
  403. if (copy_to_user(buf, &kevent->event, event_size))
  404. return -EFAULT;
  405. if (kevent->name) {
  406. buf += event_size;
  407. if (copy_to_user(buf, kevent->name, kevent->event.len))
  408. return -EFAULT;
  409. event_size += kevent->event.len;
  410. }
  411. return event_size;
  412. }
  413. static ssize_t inotify_read(struct file *file, char __user *buf,
  414. size_t count, loff_t *pos)
  415. {
  416. struct inotify_device *dev;
  417. char __user *start;
  418. int ret;
  419. DEFINE_WAIT(wait);
  420. start = buf;
  421. dev = file->private_data;
  422. while (1) {
  423. struct inotify_kernel_event *kevent;
  424. prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
  425. mutex_lock(&dev->ev_mutex);
  426. kevent = get_one_event(dev, count);
  427. mutex_unlock(&dev->ev_mutex);
  428. if (kevent) {
  429. ret = PTR_ERR(kevent);
  430. if (IS_ERR(kevent))
  431. break;
  432. ret = copy_event_to_user(kevent, buf);
  433. free_kevent(kevent);
  434. if (ret < 0)
  435. break;
  436. buf += ret;
  437. count -= ret;
  438. continue;
  439. }
  440. ret = -EAGAIN;
  441. if (file->f_flags & O_NONBLOCK)
  442. break;
  443. ret = -EINTR;
  444. if (signal_pending(current))
  445. break;
  446. if (start != buf)
  447. break;
  448. schedule();
  449. }
  450. finish_wait(&dev->wq, &wait);
  451. if (start != buf && ret != -EFAULT)
  452. ret = buf - start;
  453. return ret;
  454. }
  455. static int inotify_fasync(int fd, struct file *file, int on)
  456. {
  457. struct inotify_device *dev = file->private_data;
  458. return fasync_helper(fd, file, on, &dev->fa) >= 0 ? 0 : -EIO;
  459. }
  460. static int inotify_release(struct inode *ignored, struct file *file)
  461. {
  462. struct inotify_device *dev = file->private_data;
  463. inotify_destroy(dev->ih);
  464. /* destroy all of the events on this device */
  465. mutex_lock(&dev->ev_mutex);
  466. while (!list_empty(&dev->events))
  467. inotify_dev_event_dequeue(dev);
  468. mutex_unlock(&dev->ev_mutex);
  469. /* free this device: the put matching the get in inotify_init() */
  470. put_inotify_dev(dev);
  471. return 0;
  472. }
  473. static long inotify_ioctl(struct file *file, unsigned int cmd,
  474. unsigned long arg)
  475. {
  476. struct inotify_device *dev;
  477. void __user *p;
  478. int ret = -ENOTTY;
  479. dev = file->private_data;
  480. p = (void __user *) arg;
  481. switch (cmd) {
  482. case FIONREAD:
  483. ret = put_user(dev->queue_size, (int __user *) p);
  484. break;
  485. }
  486. return ret;
  487. }
  488. static const struct file_operations inotify_fops = {
  489. .poll = inotify_poll,
  490. .read = inotify_read,
  491. .fasync = inotify_fasync,
  492. .release = inotify_release,
  493. .unlocked_ioctl = inotify_ioctl,
  494. .compat_ioctl = inotify_ioctl,
  495. };
  496. static const struct inotify_operations inotify_user_ops = {
  497. .handle_event = inotify_dev_queue_event,
  498. .destroy_watch = free_inotify_user_watch,
  499. };
  500. SYSCALL_DEFINE1(inotify_init1, int, flags)
  501. {
  502. struct inotify_device *dev;
  503. struct inotify_handle *ih;
  504. struct user_struct *user;
  505. struct file *filp;
  506. int fd, ret;
  507. /* Check the IN_* constants for consistency. */
  508. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  509. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  510. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  511. return -EINVAL;
  512. fd = get_unused_fd_flags(flags & O_CLOEXEC);
  513. if (fd < 0)
  514. return fd;
  515. filp = get_empty_filp();
  516. if (!filp) {
  517. ret = -ENFILE;
  518. goto out_put_fd;
  519. }
  520. user = get_current_user();
  521. if (unlikely(atomic_read(&user->inotify_devs) >=
  522. inotify_max_user_instances)) {
  523. ret = -EMFILE;
  524. goto out_free_uid;
  525. }
  526. dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
  527. if (unlikely(!dev)) {
  528. ret = -ENOMEM;
  529. goto out_free_uid;
  530. }
  531. ih = inotify_init(&inotify_user_ops);
  532. if (IS_ERR(ih)) {
  533. ret = PTR_ERR(ih);
  534. goto out_free_dev;
  535. }
  536. dev->ih = ih;
  537. dev->fa = NULL;
  538. filp->f_op = &inotify_fops;
  539. filp->f_path.mnt = mntget(inotify_mnt);
  540. filp->f_path.dentry = dget(inotify_mnt->mnt_root);
  541. filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
  542. filp->f_mode = FMODE_READ;
  543. filp->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  544. filp->private_data = dev;
  545. INIT_LIST_HEAD(&dev->events);
  546. init_waitqueue_head(&dev->wq);
  547. mutex_init(&dev->ev_mutex);
  548. mutex_init(&dev->up_mutex);
  549. dev->event_count = 0;
  550. dev->queue_size = 0;
  551. dev->max_events = inotify_max_queued_events;
  552. dev->user = user;
  553. atomic_set(&dev->count, 0);
  554. get_inotify_dev(dev);
  555. atomic_inc(&user->inotify_devs);
  556. fd_install(fd, filp);
  557. return fd;
  558. out_free_dev:
  559. kfree(dev);
  560. out_free_uid:
  561. free_uid(user);
  562. put_filp(filp);
  563. out_put_fd:
  564. put_unused_fd(fd);
  565. return ret;
  566. }
  567. SYSCALL_DEFINE0(inotify_init)
  568. {
  569. return sys_inotify_init1(0);
  570. }
  571. SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
  572. u32, mask)
  573. {
  574. struct inode *inode;
  575. struct inotify_device *dev;
  576. struct path path;
  577. struct file *filp;
  578. int ret, fput_needed;
  579. unsigned flags = 0;
  580. filp = fget_light(fd, &fput_needed);
  581. if (unlikely(!filp))
  582. return -EBADF;
  583. /* verify that this is indeed an inotify instance */
  584. if (unlikely(filp->f_op != &inotify_fops)) {
  585. ret = -EINVAL;
  586. goto fput_and_out;
  587. }
  588. if (!(mask & IN_DONT_FOLLOW))
  589. flags |= LOOKUP_FOLLOW;
  590. if (mask & IN_ONLYDIR)
  591. flags |= LOOKUP_DIRECTORY;
  592. ret = find_inode(pathname, &path, flags);
  593. if (unlikely(ret))
  594. goto fput_and_out;
  595. /* inode held in place by reference to path; dev by fget on fd */
  596. inode = path.dentry->d_inode;
  597. dev = filp->private_data;
  598. mutex_lock(&dev->up_mutex);
  599. ret = inotify_find_update_watch(dev->ih, inode, mask);
  600. if (ret == -ENOENT)
  601. ret = create_watch(dev, inode, mask);
  602. mutex_unlock(&dev->up_mutex);
  603. path_put(&path);
  604. fput_and_out:
  605. fput_light(filp, fput_needed);
  606. return ret;
  607. }
  608. SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
  609. {
  610. struct file *filp;
  611. struct inotify_device *dev;
  612. int ret, fput_needed;
  613. filp = fget_light(fd, &fput_needed);
  614. if (unlikely(!filp))
  615. return -EBADF;
  616. /* verify that this is indeed an inotify instance */
  617. if (unlikely(filp->f_op != &inotify_fops)) {
  618. ret = -EINVAL;
  619. goto out;
  620. }
  621. dev = filp->private_data;
  622. /* we free our watch data when we get IN_IGNORED */
  623. ret = inotify_rm_wd(dev->ih, wd);
  624. out:
  625. fput_light(filp, fput_needed);
  626. return ret;
  627. }
  628. static int
  629. inotify_get_sb(struct file_system_type *fs_type, int flags,
  630. const char *dev_name, void *data, struct vfsmount *mnt)
  631. {
  632. return get_sb_pseudo(fs_type, "inotify", NULL,
  633. INOTIFYFS_SUPER_MAGIC, mnt);
  634. }
  635. static struct file_system_type inotify_fs_type = {
  636. .name = "inotifyfs",
  637. .get_sb = inotify_get_sb,
  638. .kill_sb = kill_anon_super,
  639. };
  640. /*
  641. * inotify_user_setup - Our initialization function. Note that we cannnot return
  642. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  643. * must result in panic().
  644. */
  645. static int __init inotify_user_setup(void)
  646. {
  647. int ret;
  648. ret = register_filesystem(&inotify_fs_type);
  649. if (unlikely(ret))
  650. panic("inotify: register_filesystem returned %d!\n", ret);
  651. inotify_mnt = kern_mount(&inotify_fs_type);
  652. if (IS_ERR(inotify_mnt))
  653. panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
  654. inotify_max_queued_events = 16384;
  655. inotify_max_user_instances = 128;
  656. inotify_max_user_watches = 8192;
  657. watch_cachep = kmem_cache_create("inotify_watch_cache",
  658. sizeof(struct inotify_user_watch),
  659. 0, SLAB_PANIC, NULL);
  660. event_cachep = kmem_cache_create("inotify_event_cache",
  661. sizeof(struct inotify_kernel_event),
  662. 0, SLAB_PANIC, NULL);
  663. return 0;
  664. }
  665. module_init(inotify_user_setup);