inotify_user.c 21 KB

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