inotify_user.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. atomic_t count; /* reference count */
  72. struct user_struct *user; /* user who opened this dev */
  73. struct inotify_handle *ih; /* inotify handle */
  74. struct fasync_struct *fa; /* async notification */
  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 and ultimately frees 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. kfree(kevent->name);
  295. kmem_cache_free(event_cachep, kevent);
  296. }
  297. /*
  298. * inotify_dev_event_dequeue - destroy an event on the given device
  299. *
  300. * Caller must hold dev->ev_mutex.
  301. */
  302. static void inotify_dev_event_dequeue(struct inotify_device *dev)
  303. {
  304. if (!list_empty(&dev->events)) {
  305. struct inotify_kernel_event *kevent;
  306. kevent = inotify_dev_get_event(dev);
  307. remove_kevent(dev, kevent);
  308. }
  309. }
  310. /*
  311. * find_inode - resolve a user-given path to a specific inode
  312. */
  313. static int find_inode(const char __user *dirname, struct path *path,
  314. unsigned flags)
  315. {
  316. int error;
  317. error = user_path_at(AT_FDCWD, dirname, flags, path);
  318. if (error)
  319. return error;
  320. /* you can only watch an inode if you have read permissions on it */
  321. error = inode_permission(path->dentry->d_inode, MAY_READ);
  322. if (error)
  323. path_put(path);
  324. return error;
  325. }
  326. /*
  327. * create_watch - creates a watch on the given device.
  328. *
  329. * Callers must hold dev->up_mutex.
  330. */
  331. static int create_watch(struct inotify_device *dev, struct inode *inode,
  332. u32 mask)
  333. {
  334. struct inotify_user_watch *watch;
  335. int ret;
  336. if (atomic_read(&dev->user->inotify_watches) >=
  337. inotify_max_user_watches)
  338. return -ENOSPC;
  339. watch = kmem_cache_alloc(watch_cachep, GFP_KERNEL);
  340. if (unlikely(!watch))
  341. return -ENOMEM;
  342. /* save a reference to device and bump the count to make it official */
  343. get_inotify_dev(dev);
  344. watch->dev = dev;
  345. atomic_inc(&dev->user->inotify_watches);
  346. inotify_init_watch(&watch->wdata);
  347. ret = inotify_add_watch(dev->ih, &watch->wdata, inode, mask);
  348. if (ret < 0)
  349. free_inotify_user_watch(&watch->wdata);
  350. return ret;
  351. }
  352. /* Device Interface */
  353. static unsigned int inotify_poll(struct file *file, poll_table *wait)
  354. {
  355. struct inotify_device *dev = file->private_data;
  356. int ret = 0;
  357. poll_wait(file, &dev->wq, wait);
  358. mutex_lock(&dev->ev_mutex);
  359. if (!list_empty(&dev->events))
  360. ret = POLLIN | POLLRDNORM;
  361. mutex_unlock(&dev->ev_mutex);
  362. return ret;
  363. }
  364. static ssize_t inotify_read(struct file *file, char __user *buf,
  365. size_t count, loff_t *pos)
  366. {
  367. size_t event_size = sizeof (struct inotify_event);
  368. struct inotify_device *dev;
  369. char __user *start;
  370. int ret;
  371. DEFINE_WAIT(wait);
  372. start = buf;
  373. dev = file->private_data;
  374. while (1) {
  375. int events;
  376. prepare_to_wait(&dev->wq, &wait, TASK_INTERRUPTIBLE);
  377. mutex_lock(&dev->ev_mutex);
  378. events = !list_empty(&dev->events);
  379. mutex_unlock(&dev->ev_mutex);
  380. if (events) {
  381. ret = 0;
  382. break;
  383. }
  384. if (file->f_flags & O_NONBLOCK) {
  385. ret = -EAGAIN;
  386. break;
  387. }
  388. if (signal_pending(current)) {
  389. ret = -EINTR;
  390. break;
  391. }
  392. schedule();
  393. }
  394. finish_wait(&dev->wq, &wait);
  395. if (ret)
  396. return ret;
  397. mutex_lock(&dev->ev_mutex);
  398. while (1) {
  399. struct inotify_kernel_event *kevent;
  400. ret = buf - start;
  401. if (list_empty(&dev->events))
  402. break;
  403. kevent = inotify_dev_get_event(dev);
  404. if (event_size + kevent->event.len > count) {
  405. if (ret == 0 && count > 0) {
  406. /*
  407. * could not get a single event because we
  408. * didn't have enough buffer space.
  409. */
  410. ret = -EINVAL;
  411. }
  412. break;
  413. }
  414. if (copy_to_user(buf, &kevent->event, event_size)) {
  415. ret = -EFAULT;
  416. break;
  417. }
  418. buf += event_size;
  419. count -= event_size;
  420. if (kevent->name) {
  421. if (copy_to_user(buf, kevent->name, kevent->event.len)){
  422. ret = -EFAULT;
  423. break;
  424. }
  425. buf += kevent->event.len;
  426. count -= kevent->event.len;
  427. }
  428. remove_kevent(dev, kevent);
  429. }
  430. mutex_unlock(&dev->ev_mutex);
  431. return ret;
  432. }
  433. static int inotify_fasync(int fd, struct file *file, int on)
  434. {
  435. struct inotify_device *dev = file->private_data;
  436. return fasync_helper(fd, file, on, &dev->fa) >= 0 ? 0 : -EIO;
  437. }
  438. static int inotify_release(struct inode *ignored, struct file *file)
  439. {
  440. struct inotify_device *dev = file->private_data;
  441. inotify_destroy(dev->ih);
  442. /* destroy all of the events on this device */
  443. mutex_lock(&dev->ev_mutex);
  444. while (!list_empty(&dev->events))
  445. inotify_dev_event_dequeue(dev);
  446. mutex_unlock(&dev->ev_mutex);
  447. if (file->f_flags & FASYNC)
  448. inotify_fasync(-1, file, 0);
  449. /* free this device: the put matching the get in inotify_init() */
  450. put_inotify_dev(dev);
  451. return 0;
  452. }
  453. static long inotify_ioctl(struct file *file, unsigned int cmd,
  454. unsigned long arg)
  455. {
  456. struct inotify_device *dev;
  457. void __user *p;
  458. int ret = -ENOTTY;
  459. dev = file->private_data;
  460. p = (void __user *) arg;
  461. switch (cmd) {
  462. case FIONREAD:
  463. ret = put_user(dev->queue_size, (int __user *) p);
  464. break;
  465. }
  466. return ret;
  467. }
  468. static const struct file_operations inotify_fops = {
  469. .poll = inotify_poll,
  470. .read = inotify_read,
  471. .fasync = inotify_fasync,
  472. .release = inotify_release,
  473. .unlocked_ioctl = inotify_ioctl,
  474. .compat_ioctl = inotify_ioctl,
  475. };
  476. static const struct inotify_operations inotify_user_ops = {
  477. .handle_event = inotify_dev_queue_event,
  478. .destroy_watch = free_inotify_user_watch,
  479. };
  480. asmlinkage long sys_inotify_init1(int flags)
  481. {
  482. struct inotify_device *dev;
  483. struct inotify_handle *ih;
  484. struct user_struct *user;
  485. struct file *filp;
  486. int fd, ret;
  487. /* Check the IN_* constants for consistency. */
  488. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  489. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  490. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  491. return -EINVAL;
  492. fd = get_unused_fd_flags(flags & O_CLOEXEC);
  493. if (fd < 0)
  494. return fd;
  495. filp = get_empty_filp();
  496. if (!filp) {
  497. ret = -ENFILE;
  498. goto out_put_fd;
  499. }
  500. user = get_uid(current->user);
  501. if (unlikely(atomic_read(&user->inotify_devs) >=
  502. inotify_max_user_instances)) {
  503. ret = -EMFILE;
  504. goto out_free_uid;
  505. }
  506. dev = kmalloc(sizeof(struct inotify_device), GFP_KERNEL);
  507. if (unlikely(!dev)) {
  508. ret = -ENOMEM;
  509. goto out_free_uid;
  510. }
  511. ih = inotify_init(&inotify_user_ops);
  512. if (IS_ERR(ih)) {
  513. ret = PTR_ERR(ih);
  514. goto out_free_dev;
  515. }
  516. dev->ih = ih;
  517. dev->fa = NULL;
  518. filp->f_op = &inotify_fops;
  519. filp->f_path.mnt = mntget(inotify_mnt);
  520. filp->f_path.dentry = dget(inotify_mnt->mnt_root);
  521. filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
  522. filp->f_mode = FMODE_READ;
  523. filp->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  524. filp->private_data = dev;
  525. INIT_LIST_HEAD(&dev->events);
  526. init_waitqueue_head(&dev->wq);
  527. mutex_init(&dev->ev_mutex);
  528. mutex_init(&dev->up_mutex);
  529. dev->event_count = 0;
  530. dev->queue_size = 0;
  531. dev->max_events = inotify_max_queued_events;
  532. dev->user = user;
  533. atomic_set(&dev->count, 0);
  534. get_inotify_dev(dev);
  535. atomic_inc(&user->inotify_devs);
  536. fd_install(fd, filp);
  537. return fd;
  538. out_free_dev:
  539. kfree(dev);
  540. out_free_uid:
  541. free_uid(user);
  542. put_filp(filp);
  543. out_put_fd:
  544. put_unused_fd(fd);
  545. return ret;
  546. }
  547. asmlinkage long sys_inotify_init(void)
  548. {
  549. return sys_inotify_init1(0);
  550. }
  551. asmlinkage long sys_inotify_add_watch(int fd, const char __user *pathname, u32 mask)
  552. {
  553. struct inode *inode;
  554. struct inotify_device *dev;
  555. struct path path;
  556. struct file *filp;
  557. int ret, fput_needed;
  558. unsigned flags = 0;
  559. filp = fget_light(fd, &fput_needed);
  560. if (unlikely(!filp))
  561. return -EBADF;
  562. /* verify that this is indeed an inotify instance */
  563. if (unlikely(filp->f_op != &inotify_fops)) {
  564. ret = -EINVAL;
  565. goto fput_and_out;
  566. }
  567. if (!(mask & IN_DONT_FOLLOW))
  568. flags |= LOOKUP_FOLLOW;
  569. if (mask & IN_ONLYDIR)
  570. flags |= LOOKUP_DIRECTORY;
  571. ret = find_inode(pathname, &path, flags);
  572. if (unlikely(ret))
  573. goto fput_and_out;
  574. /* inode held in place by reference to path; dev by fget on fd */
  575. inode = path.dentry->d_inode;
  576. dev = filp->private_data;
  577. mutex_lock(&dev->up_mutex);
  578. ret = inotify_find_update_watch(dev->ih, inode, mask);
  579. if (ret == -ENOENT)
  580. ret = create_watch(dev, inode, mask);
  581. mutex_unlock(&dev->up_mutex);
  582. path_put(&path);
  583. fput_and_out:
  584. fput_light(filp, fput_needed);
  585. return ret;
  586. }
  587. asmlinkage long sys_inotify_rm_watch(int fd, u32 wd)
  588. {
  589. struct file *filp;
  590. struct inotify_device *dev;
  591. int ret, fput_needed;
  592. filp = fget_light(fd, &fput_needed);
  593. if (unlikely(!filp))
  594. return -EBADF;
  595. /* verify that this is indeed an inotify instance */
  596. if (unlikely(filp->f_op != &inotify_fops)) {
  597. ret = -EINVAL;
  598. goto out;
  599. }
  600. dev = filp->private_data;
  601. /* we free our watch data when we get IN_IGNORED */
  602. ret = inotify_rm_wd(dev->ih, wd);
  603. out:
  604. fput_light(filp, fput_needed);
  605. return ret;
  606. }
  607. static int
  608. inotify_get_sb(struct file_system_type *fs_type, int flags,
  609. const char *dev_name, void *data, struct vfsmount *mnt)
  610. {
  611. return get_sb_pseudo(fs_type, "inotify", NULL,
  612. INOTIFYFS_SUPER_MAGIC, mnt);
  613. }
  614. static struct file_system_type inotify_fs_type = {
  615. .name = "inotifyfs",
  616. .get_sb = inotify_get_sb,
  617. .kill_sb = kill_anon_super,
  618. };
  619. /*
  620. * inotify_user_setup - Our initialization function. Note that we cannnot return
  621. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  622. * must result in panic().
  623. */
  624. static int __init inotify_user_setup(void)
  625. {
  626. int ret;
  627. ret = register_filesystem(&inotify_fs_type);
  628. if (unlikely(ret))
  629. panic("inotify: register_filesystem returned %d!\n", ret);
  630. inotify_mnt = kern_mount(&inotify_fs_type);
  631. if (IS_ERR(inotify_mnt))
  632. panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
  633. inotify_max_queued_events = 16384;
  634. inotify_max_user_instances = 128;
  635. inotify_max_user_watches = 8192;
  636. watch_cachep = kmem_cache_create("inotify_watch_cache",
  637. sizeof(struct inotify_user_watch),
  638. 0, SLAB_PANIC, NULL);
  639. event_cachep = kmem_cache_create("inotify_event_cache",
  640. sizeof(struct inotify_kernel_event),
  641. 0, SLAB_PANIC, NULL);
  642. return 0;
  643. }
  644. module_init(inotify_user_setup);