inotify_user.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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/signal.h>
  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 <linux/memcontrol.h>
  41. #include "inotify.h"
  42. #include "../fdinfo.h"
  43. #include <asm/ioctls.h>
  44. /* configurable via /proc/sys/fs/inotify/ */
  45. static int inotify_max_queued_events __read_mostly;
  46. struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
  47. #ifdef CONFIG_SYSCTL
  48. #include <linux/sysctl.h>
  49. static int zero;
  50. struct ctl_table inotify_table[] = {
  51. {
  52. .procname = "max_user_instances",
  53. .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
  54. .maxlen = sizeof(int),
  55. .mode = 0644,
  56. .proc_handler = proc_dointvec_minmax,
  57. .extra1 = &zero,
  58. },
  59. {
  60. .procname = "max_user_watches",
  61. .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
  62. .maxlen = sizeof(int),
  63. .mode = 0644,
  64. .proc_handler = proc_dointvec_minmax,
  65. .extra1 = &zero,
  66. },
  67. {
  68. .procname = "max_queued_events",
  69. .data = &inotify_max_queued_events,
  70. .maxlen = sizeof(int),
  71. .mode = 0644,
  72. .proc_handler = proc_dointvec_minmax,
  73. .extra1 = &zero
  74. },
  75. { }
  76. };
  77. #endif /* CONFIG_SYSCTL */
  78. static inline __u32 inotify_arg_to_mask(u32 arg)
  79. {
  80. __u32 mask;
  81. /*
  82. * everything should accept their own ignored, cares about children,
  83. * and should receive events when the inode is unmounted
  84. */
  85. mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
  86. /* mask off the flags used to open the fd */
  87. mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
  88. return mask;
  89. }
  90. static inline u32 inotify_mask_to_arg(__u32 mask)
  91. {
  92. return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
  93. IN_Q_OVERFLOW);
  94. }
  95. /* intofiy userspace file descriptor functions */
  96. static __poll_t inotify_poll(struct file *file, poll_table *wait)
  97. {
  98. struct fsnotify_group *group = file->private_data;
  99. __poll_t ret = 0;
  100. poll_wait(file, &group->notification_waitq, wait);
  101. spin_lock(&group->notification_lock);
  102. if (!fsnotify_notify_queue_is_empty(group))
  103. ret = EPOLLIN | EPOLLRDNORM;
  104. spin_unlock(&group->notification_lock);
  105. return ret;
  106. }
  107. static int round_event_name_len(struct fsnotify_event *fsn_event)
  108. {
  109. struct inotify_event_info *event;
  110. event = INOTIFY_E(fsn_event);
  111. if (!event->name_len)
  112. return 0;
  113. return roundup(event->name_len + 1, sizeof(struct inotify_event));
  114. }
  115. /*
  116. * Get an inotify_kernel_event if one exists and is small
  117. * enough to fit in "count". Return an error pointer if
  118. * not large enough.
  119. *
  120. * Called with the group->notification_lock held.
  121. */
  122. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  123. size_t count)
  124. {
  125. size_t event_size = sizeof(struct inotify_event);
  126. struct fsnotify_event *event;
  127. if (fsnotify_notify_queue_is_empty(group))
  128. return NULL;
  129. event = fsnotify_peek_first_event(group);
  130. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  131. event_size += round_event_name_len(event);
  132. if (event_size > count)
  133. return ERR_PTR(-EINVAL);
  134. /* held the notification_lock the whole time, so this is the
  135. * same event we peeked above */
  136. fsnotify_remove_first_event(group);
  137. return event;
  138. }
  139. /*
  140. * Copy an event to user space, returning how much we copied.
  141. *
  142. * We already checked that the event size is smaller than the
  143. * buffer we had in "get_one_event()" above.
  144. */
  145. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  146. struct fsnotify_event *fsn_event,
  147. char __user *buf)
  148. {
  149. struct inotify_event inotify_event;
  150. struct inotify_event_info *event;
  151. size_t event_size = sizeof(struct inotify_event);
  152. size_t name_len;
  153. size_t pad_name_len;
  154. pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
  155. event = INOTIFY_E(fsn_event);
  156. name_len = event->name_len;
  157. /*
  158. * round up name length so it is a multiple of event_size
  159. * plus an extra byte for the terminating '\0'.
  160. */
  161. pad_name_len = round_event_name_len(fsn_event);
  162. inotify_event.len = pad_name_len;
  163. inotify_event.mask = inotify_mask_to_arg(fsn_event->mask);
  164. inotify_event.wd = event->wd;
  165. inotify_event.cookie = event->sync_cookie;
  166. /* send the main event */
  167. if (copy_to_user(buf, &inotify_event, event_size))
  168. return -EFAULT;
  169. buf += event_size;
  170. /*
  171. * fsnotify only stores the pathname, so here we have to send the pathname
  172. * and then pad that pathname out to a multiple of sizeof(inotify_event)
  173. * with zeros.
  174. */
  175. if (pad_name_len) {
  176. /* copy the path name */
  177. if (copy_to_user(buf, event->name, name_len))
  178. return -EFAULT;
  179. buf += name_len;
  180. /* fill userspace with 0's */
  181. if (clear_user(buf, pad_name_len - name_len))
  182. return -EFAULT;
  183. event_size += pad_name_len;
  184. }
  185. return event_size;
  186. }
  187. static ssize_t inotify_read(struct file *file, char __user *buf,
  188. size_t count, loff_t *pos)
  189. {
  190. struct fsnotify_group *group;
  191. struct fsnotify_event *kevent;
  192. char __user *start;
  193. int ret;
  194. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  195. start = buf;
  196. group = file->private_data;
  197. add_wait_queue(&group->notification_waitq, &wait);
  198. while (1) {
  199. spin_lock(&group->notification_lock);
  200. kevent = get_one_event(group, count);
  201. spin_unlock(&group->notification_lock);
  202. pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
  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_destroy_event(group, 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 = -ERESTARTSYS;
  219. if (signal_pending(current))
  220. break;
  221. if (start != buf)
  222. break;
  223. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  224. }
  225. remove_wait_queue(&group->notification_waitq, &wait);
  226. if (start != buf && ret != -EFAULT)
  227. ret = buf - start;
  228. return ret;
  229. }
  230. static int inotify_release(struct inode *ignored, struct file *file)
  231. {
  232. struct fsnotify_group *group = file->private_data;
  233. pr_debug("%s: group=%p\n", __func__, group);
  234. /* free this group, matching get was inotify_init->fsnotify_obtain_group */
  235. fsnotify_destroy_group(group);
  236. return 0;
  237. }
  238. static long inotify_ioctl(struct file *file, unsigned int cmd,
  239. unsigned long arg)
  240. {
  241. struct fsnotify_group *group;
  242. struct fsnotify_event *fsn_event;
  243. void __user *p;
  244. int ret = -ENOTTY;
  245. size_t send_len = 0;
  246. group = file->private_data;
  247. p = (void __user *) arg;
  248. pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
  249. switch (cmd) {
  250. case FIONREAD:
  251. spin_lock(&group->notification_lock);
  252. list_for_each_entry(fsn_event, &group->notification_list,
  253. list) {
  254. send_len += sizeof(struct inotify_event);
  255. send_len += round_event_name_len(fsn_event);
  256. }
  257. spin_unlock(&group->notification_lock);
  258. ret = put_user(send_len, (int __user *) p);
  259. break;
  260. #ifdef CONFIG_CHECKPOINT_RESTORE
  261. case INOTIFY_IOC_SETNEXTWD:
  262. ret = -EINVAL;
  263. if (arg >= 1 && arg <= INT_MAX) {
  264. struct inotify_group_private_data *data;
  265. data = &group->inotify_data;
  266. spin_lock(&data->idr_lock);
  267. idr_set_cursor(&data->idr, (unsigned int)arg);
  268. spin_unlock(&data->idr_lock);
  269. ret = 0;
  270. }
  271. break;
  272. #endif /* CONFIG_CHECKPOINT_RESTORE */
  273. }
  274. return ret;
  275. }
  276. static const struct file_operations inotify_fops = {
  277. .show_fdinfo = inotify_show_fdinfo,
  278. .poll = inotify_poll,
  279. .read = inotify_read,
  280. .fasync = fsnotify_fasync,
  281. .release = inotify_release,
  282. .unlocked_ioctl = inotify_ioctl,
  283. .compat_ioctl = inotify_ioctl,
  284. .llseek = noop_llseek,
  285. };
  286. /*
  287. * find_inode - resolve a user-given path to a specific inode
  288. */
  289. static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
  290. {
  291. int error;
  292. error = user_path_at(AT_FDCWD, dirname, flags, path);
  293. if (error)
  294. return error;
  295. /* you can only watch an inode if you have read permissions on it */
  296. error = inode_permission(path->dentry->d_inode, MAY_READ);
  297. if (error)
  298. path_put(path);
  299. return error;
  300. }
  301. static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
  302. struct inotify_inode_mark *i_mark)
  303. {
  304. int ret;
  305. idr_preload(GFP_KERNEL);
  306. spin_lock(idr_lock);
  307. ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
  308. if (ret >= 0) {
  309. /* we added the mark to the idr, take a reference */
  310. i_mark->wd = ret;
  311. fsnotify_get_mark(&i_mark->fsn_mark);
  312. }
  313. spin_unlock(idr_lock);
  314. idr_preload_end();
  315. return ret < 0 ? ret : 0;
  316. }
  317. static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
  318. int wd)
  319. {
  320. struct idr *idr = &group->inotify_data.idr;
  321. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  322. struct inotify_inode_mark *i_mark;
  323. assert_spin_locked(idr_lock);
  324. i_mark = idr_find(idr, wd);
  325. if (i_mark) {
  326. struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
  327. fsnotify_get_mark(fsn_mark);
  328. /* One ref for being in the idr, one ref we just took */
  329. BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
  330. }
  331. return i_mark;
  332. }
  333. static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
  334. int wd)
  335. {
  336. struct inotify_inode_mark *i_mark;
  337. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  338. spin_lock(idr_lock);
  339. i_mark = inotify_idr_find_locked(group, wd);
  340. spin_unlock(idr_lock);
  341. return i_mark;
  342. }
  343. /*
  344. * Remove the mark from the idr (if present) and drop the reference
  345. * on the mark because it was in the idr.
  346. */
  347. static void inotify_remove_from_idr(struct fsnotify_group *group,
  348. struct inotify_inode_mark *i_mark)
  349. {
  350. struct idr *idr = &group->inotify_data.idr;
  351. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  352. struct inotify_inode_mark *found_i_mark = NULL;
  353. int wd;
  354. spin_lock(idr_lock);
  355. wd = i_mark->wd;
  356. /*
  357. * does this i_mark think it is in the idr? we shouldn't get called
  358. * if it wasn't....
  359. */
  360. if (wd == -1) {
  361. WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
  362. __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
  363. goto out;
  364. }
  365. /* Lets look in the idr to see if we find it */
  366. found_i_mark = inotify_idr_find_locked(group, wd);
  367. if (unlikely(!found_i_mark)) {
  368. WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
  369. __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
  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. "found_i_mark=%p found_i_mark->wd=%d "
  380. "found_i_mark->group=%p\n", __func__, i_mark,
  381. i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
  382. found_i_mark->wd, found_i_mark->fsn_mark.group);
  383. goto out;
  384. }
  385. /*
  386. * One ref for being in the idr
  387. * one ref grabbed by inotify_idr_find
  388. */
  389. if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
  390. printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
  391. __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
  392. /* we can't really recover with bad ref cnting.. */
  393. BUG();
  394. }
  395. idr_remove(idr, wd);
  396. /* Removed from the idr, drop that ref. */
  397. fsnotify_put_mark(&i_mark->fsn_mark);
  398. out:
  399. i_mark->wd = -1;
  400. spin_unlock(idr_lock);
  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. }
  405. /*
  406. * Send IN_IGNORED for this wd, remove this wd from the idr.
  407. */
  408. void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
  409. struct fsnotify_group *group)
  410. {
  411. struct inotify_inode_mark *i_mark;
  412. struct fsnotify_iter_info iter_info = { };
  413. fsnotify_iter_set_report_type_mark(&iter_info, FSNOTIFY_OBJ_TYPE_INODE,
  414. fsn_mark);
  415. /* Queue ignore event for the watch */
  416. inotify_handle_event(group, NULL, FS_IN_IGNORED, NULL,
  417. FSNOTIFY_EVENT_NONE, NULL, 0, &iter_info);
  418. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  419. /* remove this mark from the idr */
  420. inotify_remove_from_idr(group, i_mark);
  421. dec_inotify_watches(group->inotify_data.ucounts);
  422. }
  423. static int inotify_update_existing_watch(struct fsnotify_group *group,
  424. struct inode *inode,
  425. u32 arg)
  426. {
  427. struct fsnotify_mark *fsn_mark;
  428. struct inotify_inode_mark *i_mark;
  429. __u32 old_mask, new_mask;
  430. __u32 mask;
  431. int add = (arg & IN_MASK_ADD);
  432. int create = (arg & IN_MASK_CREATE);
  433. int ret;
  434. mask = inotify_arg_to_mask(arg);
  435. fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
  436. if (!fsn_mark)
  437. return -ENOENT;
  438. else if (create) {
  439. ret = -EEXIST;
  440. goto out;
  441. }
  442. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  443. spin_lock(&fsn_mark->lock);
  444. old_mask = fsn_mark->mask;
  445. if (add)
  446. fsn_mark->mask |= mask;
  447. else
  448. fsn_mark->mask = mask;
  449. new_mask = fsn_mark->mask;
  450. spin_unlock(&fsn_mark->lock);
  451. if (old_mask != new_mask) {
  452. /* more bits in old than in new? */
  453. int dropped = (old_mask & ~new_mask);
  454. /* more bits in this fsn_mark than the inode's mask? */
  455. int do_inode = (new_mask & ~inode->i_fsnotify_mask);
  456. /* update the inode with this new fsn_mark */
  457. if (dropped || do_inode)
  458. fsnotify_recalc_mask(inode->i_fsnotify_marks);
  459. }
  460. /* return the wd */
  461. ret = i_mark->wd;
  462. out:
  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, group);
  481. tmp_i_mark->fsn_mark.mask = mask;
  482. tmp_i_mark->wd = -1;
  483. ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
  484. if (ret)
  485. goto out_err;
  486. /* increment the number of watches the user has */
  487. if (!inc_inotify_watches(group->inotify_data.ucounts)) {
  488. inotify_remove_from_idr(group, tmp_i_mark);
  489. ret = -ENOSPC;
  490. goto out_err;
  491. }
  492. /* we are on the idr, now get on the inode */
  493. ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
  494. if (ret) {
  495. /* we failed to get on the inode, get off the idr */
  496. inotify_remove_from_idr(group, tmp_i_mark);
  497. goto out_err;
  498. }
  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. group->memcg = get_mem_cgroup_from_mm(current->mm);
  537. spin_lock_init(&group->inotify_data.idr_lock);
  538. idr_init(&group->inotify_data.idr);
  539. group->inotify_data.ucounts = inc_ucount(current_user_ns(),
  540. current_euid(),
  541. UCOUNT_INOTIFY_INSTANCES);
  542. if (!group->inotify_data.ucounts) {
  543. fsnotify_destroy_group(group);
  544. return ERR_PTR(-EMFILE);
  545. }
  546. return group;
  547. }
  548. /* inotify syscalls */
  549. static int do_inotify_init(int flags)
  550. {
  551. struct fsnotify_group *group;
  552. int ret;
  553. /* Check the IN_* constants for consistency. */
  554. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  555. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  556. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  557. return -EINVAL;
  558. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  559. group = inotify_new_group(inotify_max_queued_events);
  560. if (IS_ERR(group))
  561. return PTR_ERR(group);
  562. ret = anon_inode_getfd("inotify", &inotify_fops, group,
  563. O_RDONLY | flags);
  564. if (ret < 0)
  565. fsnotify_destroy_group(group);
  566. return ret;
  567. }
  568. SYSCALL_DEFINE1(inotify_init1, int, flags)
  569. {
  570. return do_inotify_init(flags);
  571. }
  572. SYSCALL_DEFINE0(inotify_init)
  573. {
  574. return do_inotify_init(0);
  575. }
  576. SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
  577. u32, mask)
  578. {
  579. struct fsnotify_group *group;
  580. struct inode *inode;
  581. struct path path;
  582. struct fd f;
  583. int ret;
  584. unsigned flags = 0;
  585. /*
  586. * We share a lot of code with fs/dnotify. We also share
  587. * the bit layout between inotify's IN_* and the fsnotify
  588. * FS_*. This check ensures that only the inotify IN_*
  589. * bits get passed in and set in watches/events.
  590. */
  591. if (unlikely(mask & ~ALL_INOTIFY_BITS))
  592. return -EINVAL;
  593. /*
  594. * Require at least one valid bit set in the mask.
  595. * Without _something_ set, we would have no events to
  596. * watch for.
  597. */
  598. if (unlikely(!(mask & ALL_INOTIFY_BITS)))
  599. return -EINVAL;
  600. f = fdget(fd);
  601. if (unlikely(!f.file))
  602. return -EBADF;
  603. /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
  604. if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
  605. ret = -EINVAL;
  606. goto fput_and_out;
  607. }
  608. /* verify that this is indeed an inotify instance */
  609. if (unlikely(f.file->f_op != &inotify_fops)) {
  610. ret = -EINVAL;
  611. goto fput_and_out;
  612. }
  613. if (!(mask & IN_DONT_FOLLOW))
  614. flags |= LOOKUP_FOLLOW;
  615. if (mask & IN_ONLYDIR)
  616. flags |= LOOKUP_DIRECTORY;
  617. ret = inotify_find_inode(pathname, &path, flags);
  618. if (ret)
  619. goto fput_and_out;
  620. /* inode held in place by reference to path; group by fget on fd */
  621. inode = path.dentry->d_inode;
  622. group = f.file->private_data;
  623. /* create/update an inode mark */
  624. ret = inotify_update_watch(group, inode, mask);
  625. path_put(&path);
  626. fput_and_out:
  627. fdput(f);
  628. return ret;
  629. }
  630. SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
  631. {
  632. struct fsnotify_group *group;
  633. struct inotify_inode_mark *i_mark;
  634. struct fd f;
  635. int ret = 0;
  636. f = fdget(fd);
  637. if (unlikely(!f.file))
  638. return -EBADF;
  639. /* verify that this is indeed an inotify instance */
  640. ret = -EINVAL;
  641. if (unlikely(f.file->f_op != &inotify_fops))
  642. goto out;
  643. group = f.file->private_data;
  644. ret = -EINVAL;
  645. i_mark = inotify_idr_find(group, wd);
  646. if (unlikely(!i_mark))
  647. goto out;
  648. ret = 0;
  649. fsnotify_destroy_mark(&i_mark->fsn_mark, group);
  650. /* match ref taken by inotify_idr_find */
  651. fsnotify_put_mark(&i_mark->fsn_mark);
  652. out:
  653. fdput(f);
  654. return ret;
  655. }
  656. /*
  657. * inotify_user_setup - Our initialization function. Note that we cannot return
  658. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  659. * must result in panic().
  660. */
  661. static int __init inotify_user_setup(void)
  662. {
  663. BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
  664. BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
  665. BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
  666. BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
  667. BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  668. BUILD_BUG_ON(IN_OPEN != FS_OPEN);
  669. BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
  670. BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
  671. BUILD_BUG_ON(IN_CREATE != FS_CREATE);
  672. BUILD_BUG_ON(IN_DELETE != FS_DELETE);
  673. BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
  674. BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
  675. BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
  676. BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
  677. BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
  678. BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
  679. BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
  680. BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
  681. BUG_ON(hweight32(ALL_INOTIFY_BITS) != 22);
  682. inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
  683. SLAB_PANIC|SLAB_ACCOUNT);
  684. inotify_max_queued_events = 16384;
  685. init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
  686. init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = 8192;
  687. return 0;
  688. }
  689. fs_initcall(inotify_user_setup);