inotify_user.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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. * Copyright (C) 2009 Eric Paris <Red Hat Inc>
  12. * inotify was largely rewriten to make use of the fsnotify infrastructure
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2, or (at your option) any
  17. * later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. */
  24. #include <linux/file.h>
  25. #include <linux/fs.h> /* struct inode */
  26. #include <linux/fsnotify_backend.h>
  27. #include <linux/idr.h>
  28. #include <linux/init.h> /* module_init */
  29. #include <linux/inotify.h>
  30. #include <linux/kernel.h> /* roundup() */
  31. #include <linux/magic.h> /* superblock magic number */
  32. #include <linux/mount.h> /* mntget */
  33. #include <linux/namei.h> /* LOOKUP_FOLLOW */
  34. #include <linux/path.h> /* struct path */
  35. #include <linux/sched.h> /* struct user */
  36. #include <linux/slab.h> /* struct kmem_cache */
  37. #include <linux/syscalls.h>
  38. #include <linux/types.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/poll.h>
  41. #include <linux/wait.h>
  42. #include "inotify.h"
  43. #include <asm/ioctls.h>
  44. static struct vfsmount *inotify_mnt __read_mostly;
  45. /* this just sits here and wastes global memory. used to just pad userspace messages with zeros */
  46. static struct inotify_event nul_inotify_event;
  47. /* these are configurable via /proc/sys/fs/inotify/ */
  48. static int inotify_max_user_instances __read_mostly;
  49. static int inotify_max_queued_events __read_mostly;
  50. int inotify_max_user_watches __read_mostly;
  51. static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
  52. struct kmem_cache *event_priv_cachep __read_mostly;
  53. static struct fsnotify_event *inotify_ignored_event;
  54. /*
  55. * When inotify registers a new group it increments this and uses that
  56. * value as an offset to set the fsnotify group "name" and priority.
  57. */
  58. static atomic_t inotify_grp_num;
  59. #ifdef CONFIG_SYSCTL
  60. #include <linux/sysctl.h>
  61. static int zero;
  62. ctl_table inotify_table[] = {
  63. {
  64. .ctl_name = INOTIFY_MAX_USER_INSTANCES,
  65. .procname = "max_user_instances",
  66. .data = &inotify_max_user_instances,
  67. .maxlen = sizeof(int),
  68. .mode = 0644,
  69. .proc_handler = &proc_dointvec_minmax,
  70. .strategy = &sysctl_intvec,
  71. .extra1 = &zero,
  72. },
  73. {
  74. .ctl_name = INOTIFY_MAX_USER_WATCHES,
  75. .procname = "max_user_watches",
  76. .data = &inotify_max_user_watches,
  77. .maxlen = sizeof(int),
  78. .mode = 0644,
  79. .proc_handler = &proc_dointvec_minmax,
  80. .strategy = &sysctl_intvec,
  81. .extra1 = &zero,
  82. },
  83. {
  84. .ctl_name = INOTIFY_MAX_QUEUED_EVENTS,
  85. .procname = "max_queued_events",
  86. .data = &inotify_max_queued_events,
  87. .maxlen = sizeof(int),
  88. .mode = 0644,
  89. .proc_handler = &proc_dointvec_minmax,
  90. .strategy = &sysctl_intvec,
  91. .extra1 = &zero
  92. },
  93. { .ctl_name = 0 }
  94. };
  95. #endif /* CONFIG_SYSCTL */
  96. static inline __u32 inotify_arg_to_mask(u32 arg)
  97. {
  98. __u32 mask;
  99. /* everything should accept their own ignored and cares about children */
  100. mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD);
  101. /* mask off the flags used to open the fd */
  102. mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT));
  103. return mask;
  104. }
  105. static inline u32 inotify_mask_to_arg(__u32 mask)
  106. {
  107. return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
  108. IN_Q_OVERFLOW);
  109. }
  110. /* intofiy userspace file descriptor functions */
  111. static unsigned int inotify_poll(struct file *file, poll_table *wait)
  112. {
  113. struct fsnotify_group *group = file->private_data;
  114. int ret = 0;
  115. poll_wait(file, &group->notification_waitq, wait);
  116. mutex_lock(&group->notification_mutex);
  117. if (!fsnotify_notify_queue_is_empty(group))
  118. ret = POLLIN | POLLRDNORM;
  119. mutex_unlock(&group->notification_mutex);
  120. return ret;
  121. }
  122. /*
  123. * Get an inotify_kernel_event if one exists and is small
  124. * enough to fit in "count". Return an error pointer if
  125. * not large enough.
  126. *
  127. * Called with the group->notification_mutex held.
  128. */
  129. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  130. size_t count)
  131. {
  132. size_t event_size = sizeof(struct inotify_event);
  133. struct fsnotify_event *event;
  134. if (fsnotify_notify_queue_is_empty(group))
  135. return NULL;
  136. event = fsnotify_peek_notify_event(group);
  137. event_size += roundup(event->name_len, event_size);
  138. if (event_size > count)
  139. return ERR_PTR(-EINVAL);
  140. /* held the notification_mutex the whole time, so this is the
  141. * same event we peeked above */
  142. fsnotify_remove_notify_event(group);
  143. return event;
  144. }
  145. /*
  146. * Copy an event to user space, returning how much we copied.
  147. *
  148. * We already checked that the event size is smaller than the
  149. * buffer we had in "get_one_event()" above.
  150. */
  151. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  152. struct fsnotify_event *event,
  153. char __user *buf)
  154. {
  155. struct inotify_event inotify_event;
  156. struct fsnotify_event_private_data *fsn_priv;
  157. struct inotify_event_private_data *priv;
  158. size_t event_size = sizeof(struct inotify_event);
  159. size_t name_len;
  160. /* we get the inotify watch descriptor from the event private data */
  161. spin_lock(&event->lock);
  162. fsn_priv = fsnotify_remove_priv_from_event(group, event);
  163. spin_unlock(&event->lock);
  164. if (!fsn_priv)
  165. inotify_event.wd = -1;
  166. else {
  167. priv = container_of(fsn_priv, struct inotify_event_private_data,
  168. fsnotify_event_priv_data);
  169. inotify_event.wd = priv->wd;
  170. inotify_free_event_priv(fsn_priv);
  171. }
  172. /* round up event->name_len so it is a multiple of event_size */
  173. name_len = roundup(event->name_len, event_size);
  174. inotify_event.len = name_len;
  175. inotify_event.mask = inotify_mask_to_arg(event->mask);
  176. inotify_event.cookie = event->sync_cookie;
  177. /* send the main event */
  178. if (copy_to_user(buf, &inotify_event, event_size))
  179. return -EFAULT;
  180. buf += event_size;
  181. /*
  182. * fsnotify only stores the pathname, so here we have to send the pathname
  183. * and then pad that pathname out to a multiple of sizeof(inotify_event)
  184. * with zeros. I get my zeros from the nul_inotify_event.
  185. */
  186. if (name_len) {
  187. unsigned int len_to_zero = name_len - event->name_len;
  188. /* copy the path name */
  189. if (copy_to_user(buf, event->file_name, event->name_len))
  190. return -EFAULT;
  191. buf += event->name_len;
  192. /* fill userspace with 0's from nul_inotify_event */
  193. if (copy_to_user(buf, &nul_inotify_event, len_to_zero))
  194. return -EFAULT;
  195. buf += len_to_zero;
  196. event_size += name_len;
  197. }
  198. return event_size;
  199. }
  200. static ssize_t inotify_read(struct file *file, char __user *buf,
  201. size_t count, loff_t *pos)
  202. {
  203. struct fsnotify_group *group;
  204. struct fsnotify_event *kevent;
  205. char __user *start;
  206. int ret;
  207. DEFINE_WAIT(wait);
  208. start = buf;
  209. group = file->private_data;
  210. while (1) {
  211. prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
  212. mutex_lock(&group->notification_mutex);
  213. kevent = get_one_event(group, count);
  214. mutex_unlock(&group->notification_mutex);
  215. if (kevent) {
  216. ret = PTR_ERR(kevent);
  217. if (IS_ERR(kevent))
  218. break;
  219. ret = copy_event_to_user(group, kevent, buf);
  220. fsnotify_put_event(kevent);
  221. if (ret < 0)
  222. break;
  223. buf += ret;
  224. count -= ret;
  225. continue;
  226. }
  227. ret = -EAGAIN;
  228. if (file->f_flags & O_NONBLOCK)
  229. break;
  230. ret = -EINTR;
  231. if (signal_pending(current))
  232. break;
  233. if (start != buf)
  234. break;
  235. schedule();
  236. }
  237. finish_wait(&group->notification_waitq, &wait);
  238. if (start != buf && ret != -EFAULT)
  239. ret = buf - start;
  240. return ret;
  241. }
  242. static int inotify_fasync(int fd, struct file *file, int on)
  243. {
  244. struct fsnotify_group *group = file->private_data;
  245. return fasync_helper(fd, file, on, &group->inotify_data.fa) >= 0 ? 0 : -EIO;
  246. }
  247. static int inotify_release(struct inode *ignored, struct file *file)
  248. {
  249. struct fsnotify_group *group = file->private_data;
  250. struct user_struct *user = group->inotify_data.user;
  251. fsnotify_clear_marks_by_group(group);
  252. /* free this group, matching get was inotify_init->fsnotify_obtain_group */
  253. fsnotify_put_group(group);
  254. atomic_dec(&user->inotify_devs);
  255. return 0;
  256. }
  257. static long inotify_ioctl(struct file *file, unsigned int cmd,
  258. unsigned long arg)
  259. {
  260. struct fsnotify_group *group;
  261. struct fsnotify_event_holder *holder;
  262. struct fsnotify_event *event;
  263. void __user *p;
  264. int ret = -ENOTTY;
  265. size_t send_len = 0;
  266. group = file->private_data;
  267. p = (void __user *) arg;
  268. switch (cmd) {
  269. case FIONREAD:
  270. mutex_lock(&group->notification_mutex);
  271. list_for_each_entry(holder, &group->notification_list, event_list) {
  272. event = holder->event;
  273. send_len += sizeof(struct inotify_event);
  274. send_len += roundup(event->name_len,
  275. sizeof(struct inotify_event));
  276. }
  277. mutex_unlock(&group->notification_mutex);
  278. ret = put_user(send_len, (int __user *) p);
  279. break;
  280. }
  281. return ret;
  282. }
  283. static const struct file_operations inotify_fops = {
  284. .poll = inotify_poll,
  285. .read = inotify_read,
  286. .fasync = inotify_fasync,
  287. .release = inotify_release,
  288. .unlocked_ioctl = inotify_ioctl,
  289. .compat_ioctl = inotify_ioctl,
  290. };
  291. /*
  292. * find_inode - resolve a user-given path to a specific inode
  293. */
  294. static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
  295. {
  296. int error;
  297. error = user_path_at(AT_FDCWD, dirname, flags, path);
  298. if (error)
  299. return error;
  300. /* you can only watch an inode if you have read permissions on it */
  301. error = inode_permission(path->dentry->d_inode, MAY_READ);
  302. if (error)
  303. path_put(path);
  304. return error;
  305. }
  306. /*
  307. * Send IN_IGNORED for this wd, remove this wd from the idr, and drop the
  308. * internal reference help on the mark because it is in the idr.
  309. */
  310. void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
  311. struct fsnotify_group *group)
  312. {
  313. struct inotify_inode_mark_entry *ientry;
  314. struct inotify_event_private_data *event_priv;
  315. struct fsnotify_event_private_data *fsn_event_priv;
  316. struct idr *idr;
  317. ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
  318. event_priv = kmem_cache_alloc(event_priv_cachep, GFP_KERNEL);
  319. if (unlikely(!event_priv))
  320. goto skip_send_ignore;
  321. fsn_event_priv = &event_priv->fsnotify_event_priv_data;
  322. fsn_event_priv->group = group;
  323. event_priv->wd = ientry->wd;
  324. fsnotify_add_notify_event(group, inotify_ignored_event, fsn_event_priv);
  325. /* did the private data get added? */
  326. if (list_empty(&fsn_event_priv->event_list))
  327. inotify_free_event_priv(fsn_event_priv);
  328. skip_send_ignore:
  329. /* remove this entry from the idr */
  330. spin_lock(&group->inotify_data.idr_lock);
  331. idr = &group->inotify_data.idr;
  332. idr_remove(idr, ientry->wd);
  333. spin_unlock(&group->inotify_data.idr_lock);
  334. /* removed from idr, drop that reference */
  335. fsnotify_put_mark(entry);
  336. }
  337. /* ding dong the mark is dead */
  338. static void inotify_free_mark(struct fsnotify_mark_entry *entry)
  339. {
  340. struct inotify_inode_mark_entry *ientry = (struct inotify_inode_mark_entry *)entry;
  341. kmem_cache_free(inotify_inode_mark_cachep, ientry);
  342. }
  343. static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
  344. {
  345. struct fsnotify_mark_entry *entry = NULL;
  346. struct inotify_inode_mark_entry *ientry;
  347. int ret = 0;
  348. int add = (arg & IN_MASK_ADD);
  349. __u32 mask;
  350. __u32 old_mask, new_mask;
  351. /* don't allow invalid bits: we don't want flags set */
  352. mask = inotify_arg_to_mask(arg);
  353. if (unlikely(!mask))
  354. return -EINVAL;
  355. ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
  356. if (unlikely(!ientry))
  357. return -ENOMEM;
  358. /* we set the mask at the end after attaching it */
  359. fsnotify_init_mark(&ientry->fsn_entry, inotify_free_mark);
  360. ientry->wd = 0;
  361. find_entry:
  362. spin_lock(&inode->i_lock);
  363. entry = fsnotify_find_mark_entry(group, inode);
  364. spin_unlock(&inode->i_lock);
  365. if (entry) {
  366. kmem_cache_free(inotify_inode_mark_cachep, ientry);
  367. ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
  368. } else {
  369. if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches) {
  370. ret = -ENOSPC;
  371. goto out_err;
  372. }
  373. ret = fsnotify_add_mark(&ientry->fsn_entry, group, inode);
  374. if (ret == -EEXIST)
  375. goto find_entry;
  376. else if (ret)
  377. goto out_err;
  378. entry = &ientry->fsn_entry;
  379. retry:
  380. ret = -ENOMEM;
  381. if (unlikely(!idr_pre_get(&group->inotify_data.idr, GFP_KERNEL)))
  382. goto out_err;
  383. spin_lock(&group->inotify_data.idr_lock);
  384. /* if entry is added to the idr we keep the reference obtained
  385. * through fsnotify_mark_add. remember to drop this reference
  386. * when entry is removed from idr */
  387. ret = idr_get_new_above(&group->inotify_data.idr, entry,
  388. ++group->inotify_data.last_wd,
  389. &ientry->wd);
  390. spin_unlock(&group->inotify_data.idr_lock);
  391. if (ret) {
  392. if (ret == -EAGAIN)
  393. goto retry;
  394. goto out_err;
  395. }
  396. atomic_inc(&group->inotify_data.user->inotify_watches);
  397. }
  398. spin_lock(&entry->lock);
  399. old_mask = entry->mask;
  400. if (add) {
  401. entry->mask |= mask;
  402. new_mask = entry->mask;
  403. } else {
  404. entry->mask = mask;
  405. new_mask = entry->mask;
  406. }
  407. spin_unlock(&entry->lock);
  408. if (old_mask != new_mask) {
  409. /* more bits in old than in new? */
  410. int dropped = (old_mask & ~new_mask);
  411. /* more bits in this entry than the inode's mask? */
  412. int do_inode = (new_mask & ~inode->i_fsnotify_mask);
  413. /* more bits in this entry than the group? */
  414. int do_group = (new_mask & ~group->mask);
  415. /* update the inode with this new entry */
  416. if (dropped || do_inode)
  417. fsnotify_recalc_inode_mask(inode);
  418. /* update the group mask with the new mask */
  419. if (dropped || do_group)
  420. fsnotify_recalc_group_mask(group);
  421. }
  422. return ientry->wd;
  423. out_err:
  424. /* see this isn't supposed to happen, just kill the watch */
  425. if (entry) {
  426. fsnotify_destroy_mark_by_entry(entry);
  427. fsnotify_put_mark(entry);
  428. }
  429. return ret;
  430. }
  431. static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events)
  432. {
  433. struct fsnotify_group *group;
  434. unsigned int grp_num;
  435. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  436. grp_num = (INOTIFY_GROUP_NUM - atomic_inc_return(&inotify_grp_num));
  437. group = fsnotify_obtain_group(grp_num, 0, &inotify_fsnotify_ops);
  438. if (IS_ERR(group))
  439. return group;
  440. group->max_events = max_events;
  441. spin_lock_init(&group->inotify_data.idr_lock);
  442. idr_init(&group->inotify_data.idr);
  443. group->inotify_data.last_wd = 0;
  444. group->inotify_data.user = user;
  445. group->inotify_data.fa = NULL;
  446. return group;
  447. }
  448. /* inotify syscalls */
  449. SYSCALL_DEFINE1(inotify_init1, int, flags)
  450. {
  451. struct fsnotify_group *group;
  452. struct user_struct *user;
  453. struct file *filp;
  454. int fd, ret;
  455. /* Check the IN_* constants for consistency. */
  456. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  457. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  458. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  459. return -EINVAL;
  460. fd = get_unused_fd_flags(flags & O_CLOEXEC);
  461. if (fd < 0)
  462. return fd;
  463. filp = get_empty_filp();
  464. if (!filp) {
  465. ret = -ENFILE;
  466. goto out_put_fd;
  467. }
  468. user = get_current_user();
  469. if (unlikely(atomic_read(&user->inotify_devs) >=
  470. inotify_max_user_instances)) {
  471. ret = -EMFILE;
  472. goto out_free_uid;
  473. }
  474. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  475. group = inotify_new_group(user, inotify_max_queued_events);
  476. if (IS_ERR(group)) {
  477. ret = PTR_ERR(group);
  478. goto out_free_uid;
  479. }
  480. filp->f_op = &inotify_fops;
  481. filp->f_path.mnt = mntget(inotify_mnt);
  482. filp->f_path.dentry = dget(inotify_mnt->mnt_root);
  483. filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
  484. filp->f_mode = FMODE_READ;
  485. filp->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  486. filp->private_data = group;
  487. atomic_inc(&user->inotify_devs);
  488. fd_install(fd, filp);
  489. return fd;
  490. out_free_uid:
  491. free_uid(user);
  492. put_filp(filp);
  493. out_put_fd:
  494. put_unused_fd(fd);
  495. return ret;
  496. }
  497. SYSCALL_DEFINE0(inotify_init)
  498. {
  499. return sys_inotify_init1(0);
  500. }
  501. SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
  502. u32, mask)
  503. {
  504. struct fsnotify_group *group;
  505. struct inode *inode;
  506. struct path path;
  507. struct file *filp;
  508. int ret, fput_needed;
  509. unsigned flags = 0;
  510. filp = fget_light(fd, &fput_needed);
  511. if (unlikely(!filp))
  512. return -EBADF;
  513. /* verify that this is indeed an inotify instance */
  514. if (unlikely(filp->f_op != &inotify_fops)) {
  515. ret = -EINVAL;
  516. goto fput_and_out;
  517. }
  518. if (!(mask & IN_DONT_FOLLOW))
  519. flags |= LOOKUP_FOLLOW;
  520. if (mask & IN_ONLYDIR)
  521. flags |= LOOKUP_DIRECTORY;
  522. ret = inotify_find_inode(pathname, &path, flags);
  523. if (ret)
  524. goto fput_and_out;
  525. /* inode held in place by reference to path; group by fget on fd */
  526. inode = path.dentry->d_inode;
  527. group = filp->private_data;
  528. /* create/update an inode mark */
  529. ret = inotify_update_watch(group, inode, mask);
  530. if (unlikely(ret))
  531. goto path_put_and_out;
  532. path_put_and_out:
  533. path_put(&path);
  534. fput_and_out:
  535. fput_light(filp, fput_needed);
  536. return ret;
  537. }
  538. SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
  539. {
  540. struct fsnotify_group *group;
  541. struct fsnotify_mark_entry *entry;
  542. struct file *filp;
  543. int ret = 0, fput_needed;
  544. filp = fget_light(fd, &fput_needed);
  545. if (unlikely(!filp))
  546. return -EBADF;
  547. /* verify that this is indeed an inotify instance */
  548. if (unlikely(filp->f_op != &inotify_fops)) {
  549. ret = -EINVAL;
  550. goto out;
  551. }
  552. group = filp->private_data;
  553. spin_lock(&group->inotify_data.idr_lock);
  554. entry = idr_find(&group->inotify_data.idr, wd);
  555. if (unlikely(!entry)) {
  556. spin_unlock(&group->inotify_data.idr_lock);
  557. ret = -EINVAL;
  558. goto out;
  559. }
  560. fsnotify_get_mark(entry);
  561. spin_unlock(&group->inotify_data.idr_lock);
  562. fsnotify_destroy_mark_by_entry(entry);
  563. fsnotify_put_mark(entry);
  564. out:
  565. fput_light(filp, fput_needed);
  566. return ret;
  567. }
  568. static int
  569. inotify_get_sb(struct file_system_type *fs_type, int flags,
  570. const char *dev_name, void *data, struct vfsmount *mnt)
  571. {
  572. return get_sb_pseudo(fs_type, "inotify", NULL,
  573. INOTIFYFS_SUPER_MAGIC, mnt);
  574. }
  575. static struct file_system_type inotify_fs_type = {
  576. .name = "inotifyfs",
  577. .get_sb = inotify_get_sb,
  578. .kill_sb = kill_anon_super,
  579. };
  580. /*
  581. * inotify_user_setup - Our initialization function. Note that we cannnot return
  582. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  583. * must result in panic().
  584. */
  585. static int __init inotify_user_setup(void)
  586. {
  587. int ret;
  588. ret = register_filesystem(&inotify_fs_type);
  589. if (unlikely(ret))
  590. panic("inotify: register_filesystem returned %d!\n", ret);
  591. inotify_mnt = kern_mount(&inotify_fs_type);
  592. if (IS_ERR(inotify_mnt))
  593. panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
  594. inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC);
  595. event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
  596. inotify_ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL, FSNOTIFY_EVENT_NONE, NULL, 0);
  597. if (!inotify_ignored_event)
  598. panic("unable to allocate the inotify ignored event\n");
  599. inotify_max_queued_events = 16384;
  600. inotify_max_user_instances = 128;
  601. inotify_max_user_watches = 8192;
  602. return 0;
  603. }
  604. module_init(inotify_user_setup);