inotify_user.c 21 KB

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