inotify_user.c 21 KB

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