inotify_user.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. static void inotify_remove_from_idr(struct fsnotify_group *group,
  307. struct inotify_inode_mark_entry *ientry)
  308. {
  309. struct idr *idr;
  310. spin_lock(&group->inotify_data.idr_lock);
  311. idr = &group->inotify_data.idr;
  312. idr_remove(idr, ientry->wd);
  313. spin_unlock(&group->inotify_data.idr_lock);
  314. ientry->wd = -1;
  315. }
  316. /*
  317. * Send IN_IGNORED for this wd, remove this wd from the idr, and drop the
  318. * internal reference help on the mark because it is in the idr.
  319. */
  320. void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
  321. struct fsnotify_group *group)
  322. {
  323. struct inotify_inode_mark_entry *ientry;
  324. struct inotify_event_private_data *event_priv;
  325. struct fsnotify_event_private_data *fsn_event_priv;
  326. ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
  327. event_priv = kmem_cache_alloc(event_priv_cachep, GFP_KERNEL);
  328. if (unlikely(!event_priv))
  329. goto skip_send_ignore;
  330. fsn_event_priv = &event_priv->fsnotify_event_priv_data;
  331. fsn_event_priv->group = group;
  332. event_priv->wd = ientry->wd;
  333. fsnotify_add_notify_event(group, inotify_ignored_event, fsn_event_priv);
  334. /* did the private data get added? */
  335. if (list_empty(&fsn_event_priv->event_list))
  336. inotify_free_event_priv(fsn_event_priv);
  337. skip_send_ignore:
  338. /* remove this entry from the idr */
  339. inotify_remove_from_idr(group, ientry);
  340. /* removed from idr, drop that reference */
  341. fsnotify_put_mark(entry);
  342. atomic_dec(&group->inotify_data.user->inotify_watches);
  343. }
  344. /* ding dong the mark is dead */
  345. static void inotify_free_mark(struct fsnotify_mark_entry *entry)
  346. {
  347. struct inotify_inode_mark_entry *ientry = (struct inotify_inode_mark_entry *)entry;
  348. kmem_cache_free(inotify_inode_mark_cachep, ientry);
  349. }
  350. static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
  351. {
  352. struct fsnotify_mark_entry *entry = NULL;
  353. struct inotify_inode_mark_entry *ientry;
  354. struct inotify_inode_mark_entry *tmp_ientry;
  355. int ret = 0;
  356. int add = (arg & IN_MASK_ADD);
  357. __u32 mask;
  358. __u32 old_mask, new_mask;
  359. /* don't allow invalid bits: we don't want flags set */
  360. mask = inotify_arg_to_mask(arg);
  361. if (unlikely(!mask))
  362. return -EINVAL;
  363. tmp_ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
  364. if (unlikely(!tmp_ientry))
  365. return -ENOMEM;
  366. /* we set the mask at the end after attaching it */
  367. fsnotify_init_mark(&tmp_ientry->fsn_entry, inotify_free_mark);
  368. tmp_ientry->wd = -1;
  369. find_entry:
  370. spin_lock(&inode->i_lock);
  371. entry = fsnotify_find_mark_entry(group, inode);
  372. spin_unlock(&inode->i_lock);
  373. if (entry) {
  374. ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
  375. } else {
  376. ret = -ENOSPC;
  377. if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
  378. goto out_err;
  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. ret = idr_get_new_above(&group->inotify_data.idr, &tmp_ientry->fsn_entry,
  385. group->inotify_data.last_wd,
  386. &tmp_ientry->wd);
  387. spin_unlock(&group->inotify_data.idr_lock);
  388. if (ret) {
  389. if (ret == -EAGAIN)
  390. goto retry;
  391. goto out_err;
  392. }
  393. ret = fsnotify_add_mark(&tmp_ientry->fsn_entry, group, inode);
  394. if (ret) {
  395. inotify_remove_from_idr(group, tmp_ientry);
  396. if (ret == -EEXIST)
  397. goto find_entry;
  398. goto out_err;
  399. }
  400. /* tmp_ientry has been added to the inode, so we are all set up.
  401. * now we just need to make sure tmp_ientry doesn't get freed and
  402. * we need to set up entry and ientry so the generic code can
  403. * do its thing. */
  404. ientry = tmp_ientry;
  405. entry = &ientry->fsn_entry;
  406. tmp_ientry = NULL;
  407. atomic_inc(&group->inotify_data.user->inotify_watches);
  408. /* update the idr hint */
  409. group->inotify_data.last_wd = ientry->wd;
  410. /* we put the mark on the idr, take a reference */
  411. fsnotify_get_mark(entry);
  412. }
  413. ret = ientry->wd;
  414. spin_lock(&entry->lock);
  415. old_mask = entry->mask;
  416. if (add) {
  417. entry->mask |= mask;
  418. new_mask = entry->mask;
  419. } else {
  420. entry->mask = mask;
  421. new_mask = entry->mask;
  422. }
  423. spin_unlock(&entry->lock);
  424. if (old_mask != new_mask) {
  425. /* more bits in old than in new? */
  426. int dropped = (old_mask & ~new_mask);
  427. /* more bits in this entry than the inode's mask? */
  428. int do_inode = (new_mask & ~inode->i_fsnotify_mask);
  429. /* more bits in this entry than the group? */
  430. int do_group = (new_mask & ~group->mask);
  431. /* update the inode with this new entry */
  432. if (dropped || do_inode)
  433. fsnotify_recalc_inode_mask(inode);
  434. /* update the group mask with the new mask */
  435. if (dropped || do_group)
  436. fsnotify_recalc_group_mask(group);
  437. }
  438. /* this either matches fsnotify_find_mark_entry, or init_mark_entry
  439. * depending on which path we took... */
  440. fsnotify_put_mark(entry);
  441. out_err:
  442. /* could be an error, could be that we found an existing mark */
  443. if (tmp_ientry) {
  444. /* on the idr but didn't make it on the inode */
  445. if (tmp_ientry->wd != -1)
  446. inotify_remove_from_idr(group, tmp_ientry);
  447. kmem_cache_free(inotify_inode_mark_cachep, tmp_ientry);
  448. }
  449. return ret;
  450. }
  451. static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events)
  452. {
  453. struct fsnotify_group *group;
  454. unsigned int grp_num;
  455. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  456. grp_num = (INOTIFY_GROUP_NUM - atomic_inc_return(&inotify_grp_num));
  457. group = fsnotify_obtain_group(grp_num, 0, &inotify_fsnotify_ops);
  458. if (IS_ERR(group))
  459. return group;
  460. group->max_events = max_events;
  461. spin_lock_init(&group->inotify_data.idr_lock);
  462. idr_init(&group->inotify_data.idr);
  463. group->inotify_data.last_wd = 0;
  464. group->inotify_data.user = user;
  465. group->inotify_data.fa = NULL;
  466. return group;
  467. }
  468. /* inotify syscalls */
  469. SYSCALL_DEFINE1(inotify_init1, int, flags)
  470. {
  471. struct fsnotify_group *group;
  472. struct user_struct *user;
  473. struct file *filp;
  474. int fd, ret;
  475. /* Check the IN_* constants for consistency. */
  476. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  477. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  478. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  479. return -EINVAL;
  480. fd = get_unused_fd_flags(flags & O_CLOEXEC);
  481. if (fd < 0)
  482. return fd;
  483. filp = get_empty_filp();
  484. if (!filp) {
  485. ret = -ENFILE;
  486. goto out_put_fd;
  487. }
  488. user = get_current_user();
  489. if (unlikely(atomic_read(&user->inotify_devs) >=
  490. inotify_max_user_instances)) {
  491. ret = -EMFILE;
  492. goto out_free_uid;
  493. }
  494. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  495. group = inotify_new_group(user, inotify_max_queued_events);
  496. if (IS_ERR(group)) {
  497. ret = PTR_ERR(group);
  498. goto out_free_uid;
  499. }
  500. filp->f_op = &inotify_fops;
  501. filp->f_path.mnt = mntget(inotify_mnt);
  502. filp->f_path.dentry = dget(inotify_mnt->mnt_root);
  503. filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
  504. filp->f_mode = FMODE_READ;
  505. filp->f_flags = O_RDONLY | (flags & O_NONBLOCK);
  506. filp->private_data = group;
  507. atomic_inc(&user->inotify_devs);
  508. fd_install(fd, filp);
  509. return fd;
  510. out_free_uid:
  511. free_uid(user);
  512. put_filp(filp);
  513. out_put_fd:
  514. put_unused_fd(fd);
  515. return ret;
  516. }
  517. SYSCALL_DEFINE0(inotify_init)
  518. {
  519. return sys_inotify_init1(0);
  520. }
  521. SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
  522. u32, mask)
  523. {
  524. struct fsnotify_group *group;
  525. struct inode *inode;
  526. struct path path;
  527. struct file *filp;
  528. int ret, fput_needed;
  529. unsigned flags = 0;
  530. filp = fget_light(fd, &fput_needed);
  531. if (unlikely(!filp))
  532. return -EBADF;
  533. /* verify that this is indeed an inotify instance */
  534. if (unlikely(filp->f_op != &inotify_fops)) {
  535. ret = -EINVAL;
  536. goto fput_and_out;
  537. }
  538. if (!(mask & IN_DONT_FOLLOW))
  539. flags |= LOOKUP_FOLLOW;
  540. if (mask & IN_ONLYDIR)
  541. flags |= LOOKUP_DIRECTORY;
  542. ret = inotify_find_inode(pathname, &path, flags);
  543. if (ret)
  544. goto fput_and_out;
  545. /* inode held in place by reference to path; group by fget on fd */
  546. inode = path.dentry->d_inode;
  547. group = filp->private_data;
  548. /* create/update an inode mark */
  549. ret = inotify_update_watch(group, inode, mask);
  550. if (unlikely(ret))
  551. goto path_put_and_out;
  552. path_put_and_out:
  553. path_put(&path);
  554. fput_and_out:
  555. fput_light(filp, fput_needed);
  556. return ret;
  557. }
  558. SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
  559. {
  560. struct fsnotify_group *group;
  561. struct fsnotify_mark_entry *entry;
  562. struct file *filp;
  563. int ret = 0, fput_needed;
  564. filp = fget_light(fd, &fput_needed);
  565. if (unlikely(!filp))
  566. return -EBADF;
  567. /* verify that this is indeed an inotify instance */
  568. if (unlikely(filp->f_op != &inotify_fops)) {
  569. ret = -EINVAL;
  570. goto out;
  571. }
  572. group = filp->private_data;
  573. spin_lock(&group->inotify_data.idr_lock);
  574. entry = idr_find(&group->inotify_data.idr, wd);
  575. if (unlikely(!entry)) {
  576. spin_unlock(&group->inotify_data.idr_lock);
  577. ret = -EINVAL;
  578. goto out;
  579. }
  580. fsnotify_get_mark(entry);
  581. spin_unlock(&group->inotify_data.idr_lock);
  582. fsnotify_destroy_mark_by_entry(entry);
  583. fsnotify_put_mark(entry);
  584. out:
  585. fput_light(filp, fput_needed);
  586. return ret;
  587. }
  588. static int
  589. inotify_get_sb(struct file_system_type *fs_type, int flags,
  590. const char *dev_name, void *data, struct vfsmount *mnt)
  591. {
  592. return get_sb_pseudo(fs_type, "inotify", NULL,
  593. INOTIFYFS_SUPER_MAGIC, mnt);
  594. }
  595. static struct file_system_type inotify_fs_type = {
  596. .name = "inotifyfs",
  597. .get_sb = inotify_get_sb,
  598. .kill_sb = kill_anon_super,
  599. };
  600. /*
  601. * inotify_user_setup - Our initialization function. Note that we cannnot return
  602. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  603. * must result in panic().
  604. */
  605. static int __init inotify_user_setup(void)
  606. {
  607. int ret;
  608. ret = register_filesystem(&inotify_fs_type);
  609. if (unlikely(ret))
  610. panic("inotify: register_filesystem returned %d!\n", ret);
  611. inotify_mnt = kern_mount(&inotify_fs_type);
  612. if (IS_ERR(inotify_mnt))
  613. panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
  614. inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC);
  615. event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
  616. inotify_ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL, FSNOTIFY_EVENT_NONE, NULL, 0);
  617. if (!inotify_ignored_event)
  618. panic("unable to allocate the inotify ignored event\n");
  619. inotify_max_queued_events = 16384;
  620. inotify_max_user_instances = 128;
  621. inotify_max_user_watches = 8192;
  622. return 0;
  623. }
  624. module_init(inotify_user_setup);