inotify_user.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 *i_mark)
  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, i_mark, *last_wd + 1,
  305. &i_mark->wd);
  306. /* we added the mark to the idr, take a reference */
  307. if (!ret) {
  308. *last_wd = i_mark->wd;
  309. fsnotify_get_mark(&i_mark->fsn_mark);
  310. }
  311. spin_unlock(idr_lock);
  312. } while (ret == -EAGAIN);
  313. return ret;
  314. }
  315. static struct inotify_inode_mark *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 *i_mark;
  321. assert_spin_locked(idr_lock);
  322. i_mark = idr_find(idr, wd);
  323. if (i_mark) {
  324. struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
  325. fsnotify_get_mark(fsn_mark);
  326. /* One ref for being in the idr, one ref we just took */
  327. BUG_ON(atomic_read(&fsn_mark->refcnt) < 2);
  328. }
  329. return i_mark;
  330. }
  331. static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
  332. int wd)
  333. {
  334. struct inotify_inode_mark *i_mark;
  335. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  336. spin_lock(idr_lock);
  337. i_mark = inotify_idr_find_locked(group, wd);
  338. spin_unlock(idr_lock);
  339. return i_mark;
  340. }
  341. static void do_inotify_remove_from_idr(struct fsnotify_group *group,
  342. struct inotify_inode_mark *i_mark)
  343. {
  344. struct idr *idr = &group->inotify_data.idr;
  345. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  346. int wd = i_mark->wd;
  347. assert_spin_locked(idr_lock);
  348. idr_remove(idr, wd);
  349. /* removed from the idr, drop that ref */
  350. fsnotify_put_mark(&i_mark->fsn_mark);
  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 *i_mark)
  358. {
  359. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  360. struct inotify_inode_mark *found_i_mark = NULL;
  361. int wd;
  362. spin_lock(idr_lock);
  363. wd = i_mark->wd;
  364. /*
  365. * does this i_mark 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: i_mark=%p i_mark->wd=%d i_mark->group=%p"
  370. " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
  371. i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
  372. goto out;
  373. }
  374. /* Lets look in the idr to see if we find it */
  375. found_i_mark = inotify_idr_find_locked(group, wd);
  376. if (unlikely(!found_i_mark)) {
  377. WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
  378. " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
  379. i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
  380. goto out;
  381. }
  382. /*
  383. * We found an mark in the idr at the right wd, but it's
  384. * not the mark we were told to remove. eparis seriously
  385. * fucked up somewhere.
  386. */
  387. if (unlikely(found_i_mark != i_mark)) {
  388. WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
  389. "mark->inode=%p found_i_mark=%p found_i_mark->wd=%d "
  390. "found_i_mark->group=%p found_i_mark->inode=%p\n",
  391. __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group,
  392. i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd,
  393. found_i_mark->fsn_mark.group,
  394. found_i_mark->fsn_mark.i.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(&i_mark->fsn_mark.refcnt) < 3)) {
  403. printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
  404. " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
  405. i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
  406. /* we can't really recover with bad ref cnting.. */
  407. BUG();
  408. }
  409. do_inotify_remove_from_idr(group, i_mark);
  410. out:
  411. /* match the ref taken by inotify_idr_find_locked() */
  412. if (found_i_mark)
  413. fsnotify_put_mark(&found_i_mark->fsn_mark);
  414. i_mark->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 *fsn_mark,
  421. struct fsnotify_group *group)
  422. {
  423. struct inotify_inode_mark *i_mark;
  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. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  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 = i_mark->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 mark from the idr */
  447. inotify_remove_from_idr(group, i_mark);
  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 *fsn_mark)
  452. {
  453. struct inotify_inode_mark *i_mark;
  454. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  455. kmem_cache_free(inotify_inode_mark_cachep, i_mark);
  456. }
  457. static int inotify_update_existing_watch(struct fsnotify_group *group,
  458. struct inode *inode,
  459. u32 arg)
  460. {
  461. struct fsnotify_mark *fsn_mark;
  462. struct inotify_inode_mark *i_mark;
  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. fsn_mark = fsnotify_find_inode_mark(group, inode);
  472. if (!fsn_mark)
  473. return -ENOENT;
  474. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  475. spin_lock(&fsn_mark->lock);
  476. old_mask = fsn_mark->mask;
  477. if (add)
  478. fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask));
  479. else
  480. fsnotify_set_mark_mask_locked(fsn_mark, mask);
  481. new_mask = fsn_mark->mask;
  482. spin_unlock(&fsn_mark->lock);
  483. if (old_mask != new_mask) {
  484. /* more bits in old than in new? */
  485. int dropped = (old_mask & ~new_mask);
  486. /* more bits in this fsn_mark than the inode's mask? */
  487. int do_inode = (new_mask & ~inode->i_fsnotify_mask);
  488. /* more bits in this fsn_mark than the group? */
  489. int do_group = (new_mask & ~group->mask);
  490. /* update the inode with this new fsn_mark */
  491. if (dropped || do_inode)
  492. fsnotify_recalc_inode_mask(inode);
  493. /* update the group mask with the new mask */
  494. if (dropped || do_group)
  495. fsnotify_recalc_group_mask(group);
  496. }
  497. /* return the wd */
  498. ret = i_mark->wd;
  499. /* match the get from fsnotify_find_mark() */
  500. fsnotify_put_mark(fsn_mark);
  501. return ret;
  502. }
  503. static int inotify_new_watch(struct fsnotify_group *group,
  504. struct inode *inode,
  505. u32 arg)
  506. {
  507. struct inotify_inode_mark *tmp_i_mark;
  508. __u32 mask;
  509. int ret;
  510. struct idr *idr = &group->inotify_data.idr;
  511. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  512. /* don't allow invalid bits: we don't want flags set */
  513. mask = inotify_arg_to_mask(arg);
  514. if (unlikely(!mask))
  515. return -EINVAL;
  516. tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
  517. if (unlikely(!tmp_i_mark))
  518. return -ENOMEM;
  519. fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark);
  520. tmp_i_mark->fsn_mark.mask = mask;
  521. tmp_i_mark->wd = -1;
  522. ret = -ENOSPC;
  523. if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
  524. goto out_err;
  525. ret = inotify_add_to_idr(idr, idr_lock, &group->inotify_data.last_wd,
  526. tmp_i_mark);
  527. if (ret)
  528. goto out_err;
  529. /* we are on the idr, now get on the inode */
  530. ret = fsnotify_add_mark(&tmp_i_mark->fsn_mark, group, inode, NULL, 0);
  531. if (ret) {
  532. /* we failed to get on the inode, get off the idr */
  533. inotify_remove_from_idr(group, tmp_i_mark);
  534. goto out_err;
  535. }
  536. /* increment the number of watches the user has */
  537. atomic_inc(&group->inotify_data.user->inotify_watches);
  538. /* return the watch descriptor for this new mark */
  539. ret = tmp_i_mark->wd;
  540. /* if this mark added a new event update the group mask */
  541. if (mask & ~group->mask)
  542. fsnotify_recalc_group_mask(group);
  543. out_err:
  544. /* match the ref from fsnotify_init_mark() */
  545. fsnotify_put_mark(&tmp_i_mark->fsn_mark);
  546. return ret;
  547. }
  548. static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
  549. {
  550. int ret = 0;
  551. retry:
  552. /* try to update and existing watch with the new arg */
  553. ret = inotify_update_existing_watch(group, inode, arg);
  554. /* no mark present, try to add a new one */
  555. if (ret == -ENOENT)
  556. ret = inotify_new_watch(group, inode, arg);
  557. /*
  558. * inotify_new_watch could race with another thread which did an
  559. * inotify_new_watch between the update_existing and the add watch
  560. * here, go back and try to update an existing mark again.
  561. */
  562. if (ret == -EEXIST)
  563. goto retry;
  564. return ret;
  565. }
  566. static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events)
  567. {
  568. struct fsnotify_group *group;
  569. group = fsnotify_alloc_group(&inotify_fsnotify_ops);
  570. if (IS_ERR(group))
  571. return group;
  572. group->max_events = max_events;
  573. spin_lock_init(&group->inotify_data.idr_lock);
  574. idr_init(&group->inotify_data.idr);
  575. group->inotify_data.last_wd = 0;
  576. group->inotify_data.user = user;
  577. group->inotify_data.fa = NULL;
  578. return group;
  579. }
  580. /* inotify syscalls */
  581. SYSCALL_DEFINE1(inotify_init1, int, flags)
  582. {
  583. struct fsnotify_group *group;
  584. struct user_struct *user;
  585. int ret;
  586. /* Check the IN_* constants for consistency. */
  587. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  588. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  589. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  590. return -EINVAL;
  591. user = get_current_user();
  592. if (unlikely(atomic_read(&user->inotify_devs) >=
  593. inotify_max_user_instances)) {
  594. ret = -EMFILE;
  595. goto out_free_uid;
  596. }
  597. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  598. group = inotify_new_group(user, inotify_max_queued_events);
  599. if (IS_ERR(group)) {
  600. ret = PTR_ERR(group);
  601. goto out_free_uid;
  602. }
  603. atomic_inc(&user->inotify_devs);
  604. ret = anon_inode_getfd("inotify", &inotify_fops, group,
  605. O_RDONLY | flags);
  606. if (ret >= 0)
  607. return ret;
  608. atomic_dec(&user->inotify_devs);
  609. out_free_uid:
  610. free_uid(user);
  611. return ret;
  612. }
  613. SYSCALL_DEFINE0(inotify_init)
  614. {
  615. return sys_inotify_init1(0);
  616. }
  617. SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
  618. u32, mask)
  619. {
  620. struct fsnotify_group *group;
  621. struct inode *inode;
  622. struct path path;
  623. struct file *filp;
  624. int ret, fput_needed;
  625. unsigned flags = 0;
  626. filp = fget_light(fd, &fput_needed);
  627. if (unlikely(!filp))
  628. return -EBADF;
  629. /* verify that this is indeed an inotify instance */
  630. if (unlikely(filp->f_op != &inotify_fops)) {
  631. ret = -EINVAL;
  632. goto fput_and_out;
  633. }
  634. if (!(mask & IN_DONT_FOLLOW))
  635. flags |= LOOKUP_FOLLOW;
  636. if (mask & IN_ONLYDIR)
  637. flags |= LOOKUP_DIRECTORY;
  638. ret = inotify_find_inode(pathname, &path, flags);
  639. if (ret)
  640. goto fput_and_out;
  641. /* inode held in place by reference to path; group by fget on fd */
  642. inode = path.dentry->d_inode;
  643. group = filp->private_data;
  644. /* create/update an inode mark */
  645. ret = inotify_update_watch(group, inode, mask);
  646. path_put(&path);
  647. fput_and_out:
  648. fput_light(filp, fput_needed);
  649. return ret;
  650. }
  651. SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
  652. {
  653. struct fsnotify_group *group;
  654. struct inotify_inode_mark *i_mark;
  655. struct file *filp;
  656. int ret = 0, fput_needed;
  657. filp = fget_light(fd, &fput_needed);
  658. if (unlikely(!filp))
  659. return -EBADF;
  660. /* verify that this is indeed an inotify instance */
  661. ret = -EINVAL;
  662. if (unlikely(filp->f_op != &inotify_fops))
  663. goto out;
  664. group = filp->private_data;
  665. ret = -EINVAL;
  666. i_mark = inotify_idr_find(group, wd);
  667. if (unlikely(!i_mark))
  668. goto out;
  669. ret = 0;
  670. fsnotify_destroy_mark(&i_mark->fsn_mark);
  671. /* match ref taken by inotify_idr_find */
  672. fsnotify_put_mark(&i_mark->fsn_mark);
  673. out:
  674. fput_light(filp, fput_needed);
  675. return ret;
  676. }
  677. /*
  678. * inotify_user_setup - Our initialization function. Note that we cannnot return
  679. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  680. * must result in panic().
  681. */
  682. static int __init inotify_user_setup(void)
  683. {
  684. inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
  685. event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
  686. inotify_max_queued_events = 16384;
  687. inotify_max_user_instances = 128;
  688. inotify_max_user_watches = 8192;
  689. return 0;
  690. }
  691. module_init(inotify_user_setup);