inotify_user.c 22 KB

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