inotify_user.c 20 KB

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