inotify_user.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * fs/inotify_user.c - inotify support for userspace
  4. *
  5. * Authors:
  6. * John McCutchan <[email protected]>
  7. * Robert Love <[email protected]>
  8. *
  9. * Copyright (C) 2005 John McCutchan
  10. * Copyright 2006 Hewlett-Packard Development Company, L.P.
  11. *
  12. * Copyright (C) 2009 Eric Paris <Red Hat Inc>
  13. * inotify was largely rewriten to make use of the fsnotify infrastructure
  14. */
  15. #include <linux/file.h>
  16. #include <linux/fs.h> /* struct inode */
  17. #include <linux/fsnotify_backend.h>
  18. #include <linux/idr.h>
  19. #include <linux/init.h> /* fs_initcall */
  20. #include <linux/inotify.h>
  21. #include <linux/kernel.h> /* roundup() */
  22. #include <linux/namei.h> /* LOOKUP_FOLLOW */
  23. #include <linux/sched/signal.h>
  24. #include <linux/slab.h> /* struct kmem_cache */
  25. #include <linux/syscalls.h>
  26. #include <linux/types.h>
  27. #include <linux/anon_inodes.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/poll.h>
  30. #include <linux/wait.h>
  31. #include <linux/memcontrol.h>
  32. #include <linux/security.h>
  33. #include "inotify.h"
  34. #include "../fdinfo.h"
  35. #include <asm/ioctls.h>
  36. /*
  37. * An inotify watch requires allocating an inotify_inode_mark structure as
  38. * well as pinning the watched inode. Doubling the size of a VFS inode
  39. * should be more than enough to cover the additional filesystem inode
  40. * size increase.
  41. */
  42. #define INOTIFY_WATCH_COST (sizeof(struct inotify_inode_mark) + \
  43. 2 * sizeof(struct inode))
  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 long it_zero = 0;
  50. static long it_int_max = INT_MAX;
  51. static struct ctl_table inotify_table[] = {
  52. {
  53. .procname = "max_user_instances",
  54. .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES],
  55. .maxlen = sizeof(long),
  56. .mode = 0644,
  57. .proc_handler = proc_doulongvec_minmax,
  58. .extra1 = &it_zero,
  59. .extra2 = &it_int_max,
  60. },
  61. {
  62. .procname = "max_user_watches",
  63. .data = &init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES],
  64. .maxlen = sizeof(long),
  65. .mode = 0644,
  66. .proc_handler = proc_doulongvec_minmax,
  67. .extra1 = &it_zero,
  68. .extra2 = &it_int_max,
  69. },
  70. {
  71. .procname = "max_queued_events",
  72. .data = &inotify_max_queued_events,
  73. .maxlen = sizeof(int),
  74. .mode = 0644,
  75. .proc_handler = proc_dointvec_minmax,
  76. .extra1 = SYSCTL_ZERO
  77. },
  78. { }
  79. };
  80. static void __init inotify_sysctls_init(void)
  81. {
  82. register_sysctl("fs/inotify", inotify_table);
  83. }
  84. #else
  85. #define inotify_sysctls_init() do { } while (0)
  86. #endif /* CONFIG_SYSCTL */
  87. static inline __u32 inotify_arg_to_mask(struct inode *inode, u32 arg)
  88. {
  89. __u32 mask;
  90. /*
  91. * Everything should receive events when the inode is unmounted.
  92. * All directories care about children.
  93. */
  94. mask = (FS_UNMOUNT);
  95. if (S_ISDIR(inode->i_mode))
  96. mask |= FS_EVENT_ON_CHILD;
  97. /* mask off the flags used to open the fd */
  98. mask |= (arg & INOTIFY_USER_MASK);
  99. return mask;
  100. }
  101. #define INOTIFY_MARK_FLAGS \
  102. (FSNOTIFY_MARK_FLAG_EXCL_UNLINK | FSNOTIFY_MARK_FLAG_IN_ONESHOT)
  103. static inline unsigned int inotify_arg_to_flags(u32 arg)
  104. {
  105. unsigned int flags = 0;
  106. if (arg & IN_EXCL_UNLINK)
  107. flags |= FSNOTIFY_MARK_FLAG_EXCL_UNLINK;
  108. if (arg & IN_ONESHOT)
  109. flags |= FSNOTIFY_MARK_FLAG_IN_ONESHOT;
  110. return flags;
  111. }
  112. static inline u32 inotify_mask_to_arg(__u32 mask)
  113. {
  114. return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
  115. IN_Q_OVERFLOW);
  116. }
  117. /* inotify userspace file descriptor functions */
  118. static __poll_t inotify_poll(struct file *file, poll_table *wait)
  119. {
  120. struct fsnotify_group *group = file->private_data;
  121. __poll_t ret = 0;
  122. poll_wait(file, &group->notification_waitq, wait);
  123. spin_lock(&group->notification_lock);
  124. if (!fsnotify_notify_queue_is_empty(group))
  125. ret = EPOLLIN | EPOLLRDNORM;
  126. spin_unlock(&group->notification_lock);
  127. return ret;
  128. }
  129. static int round_event_name_len(struct fsnotify_event *fsn_event)
  130. {
  131. struct inotify_event_info *event;
  132. event = INOTIFY_E(fsn_event);
  133. if (!event->name_len)
  134. return 0;
  135. return roundup(event->name_len + 1, sizeof(struct inotify_event));
  136. }
  137. /*
  138. * Get an inotify_kernel_event if one exists and is small
  139. * enough to fit in "count". Return an error pointer if
  140. * not large enough.
  141. *
  142. * Called with the group->notification_lock held.
  143. */
  144. static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
  145. size_t count)
  146. {
  147. size_t event_size = sizeof(struct inotify_event);
  148. struct fsnotify_event *event;
  149. event = fsnotify_peek_first_event(group);
  150. if (!event)
  151. return NULL;
  152. pr_debug("%s: group=%p event=%p\n", __func__, group, event);
  153. event_size += round_event_name_len(event);
  154. if (event_size > count)
  155. return ERR_PTR(-EINVAL);
  156. /* held the notification_lock the whole time, so this is the
  157. * same event we peeked above */
  158. fsnotify_remove_first_event(group);
  159. return event;
  160. }
  161. /*
  162. * Copy an event to user space, returning how much we copied.
  163. *
  164. * We already checked that the event size is smaller than the
  165. * buffer we had in "get_one_event()" above.
  166. */
  167. static ssize_t copy_event_to_user(struct fsnotify_group *group,
  168. struct fsnotify_event *fsn_event,
  169. char __user *buf)
  170. {
  171. struct inotify_event inotify_event;
  172. struct inotify_event_info *event;
  173. size_t event_size = sizeof(struct inotify_event);
  174. size_t name_len;
  175. size_t pad_name_len;
  176. pr_debug("%s: group=%p event=%p\n", __func__, group, fsn_event);
  177. event = INOTIFY_E(fsn_event);
  178. name_len = event->name_len;
  179. /*
  180. * round up name length so it is a multiple of event_size
  181. * plus an extra byte for the terminating '\0'.
  182. */
  183. pad_name_len = round_event_name_len(fsn_event);
  184. inotify_event.len = pad_name_len;
  185. inotify_event.mask = inotify_mask_to_arg(event->mask);
  186. inotify_event.wd = event->wd;
  187. inotify_event.cookie = event->sync_cookie;
  188. /* send the main event */
  189. if (copy_to_user(buf, &inotify_event, event_size))
  190. return -EFAULT;
  191. buf += event_size;
  192. /*
  193. * fsnotify only stores the pathname, so here we have to send the pathname
  194. * and then pad that pathname out to a multiple of sizeof(inotify_event)
  195. * with zeros.
  196. */
  197. if (pad_name_len) {
  198. /* copy the path name */
  199. if (copy_to_user(buf, event->name, name_len))
  200. return -EFAULT;
  201. buf += name_len;
  202. /* fill userspace with 0's */
  203. if (clear_user(buf, pad_name_len - name_len))
  204. return -EFAULT;
  205. event_size += pad_name_len;
  206. }
  207. return event_size;
  208. }
  209. static ssize_t inotify_read(struct file *file, char __user *buf,
  210. size_t count, loff_t *pos)
  211. {
  212. struct fsnotify_group *group;
  213. struct fsnotify_event *kevent;
  214. char __user *start;
  215. int ret;
  216. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  217. start = buf;
  218. group = file->private_data;
  219. add_wait_queue(&group->notification_waitq, &wait);
  220. while (1) {
  221. spin_lock(&group->notification_lock);
  222. kevent = get_one_event(group, count);
  223. spin_unlock(&group->notification_lock);
  224. pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
  225. if (kevent) {
  226. ret = PTR_ERR(kevent);
  227. if (IS_ERR(kevent))
  228. break;
  229. ret = copy_event_to_user(group, kevent, buf);
  230. fsnotify_destroy_event(group, kevent);
  231. if (ret < 0)
  232. break;
  233. buf += ret;
  234. count -= ret;
  235. continue;
  236. }
  237. ret = -EAGAIN;
  238. if (file->f_flags & O_NONBLOCK)
  239. break;
  240. ret = -ERESTARTSYS;
  241. if (signal_pending(current))
  242. break;
  243. if (start != buf)
  244. break;
  245. wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  246. }
  247. remove_wait_queue(&group->notification_waitq, &wait);
  248. if (start != buf && ret != -EFAULT)
  249. ret = buf - start;
  250. return ret;
  251. }
  252. static int inotify_release(struct inode *ignored, struct file *file)
  253. {
  254. struct fsnotify_group *group = file->private_data;
  255. pr_debug("%s: group=%p\n", __func__, group);
  256. /* free this group, matching get was inotify_init->fsnotify_obtain_group */
  257. fsnotify_destroy_group(group);
  258. return 0;
  259. }
  260. static long inotify_ioctl(struct file *file, unsigned int cmd,
  261. unsigned long arg)
  262. {
  263. struct fsnotify_group *group;
  264. struct fsnotify_event *fsn_event;
  265. void __user *p;
  266. int ret = -ENOTTY;
  267. size_t send_len = 0;
  268. group = file->private_data;
  269. p = (void __user *) arg;
  270. pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
  271. switch (cmd) {
  272. case FIONREAD:
  273. spin_lock(&group->notification_lock);
  274. list_for_each_entry(fsn_event, &group->notification_list,
  275. list) {
  276. send_len += sizeof(struct inotify_event);
  277. send_len += round_event_name_len(fsn_event);
  278. }
  279. spin_unlock(&group->notification_lock);
  280. ret = put_user(send_len, (int __user *) p);
  281. break;
  282. #ifdef CONFIG_CHECKPOINT_RESTORE
  283. case INOTIFY_IOC_SETNEXTWD:
  284. ret = -EINVAL;
  285. if (arg >= 1 && arg <= INT_MAX) {
  286. struct inotify_group_private_data *data;
  287. data = &group->inotify_data;
  288. spin_lock(&data->idr_lock);
  289. idr_set_cursor(&data->idr, (unsigned int)arg);
  290. spin_unlock(&data->idr_lock);
  291. ret = 0;
  292. }
  293. break;
  294. #endif /* CONFIG_CHECKPOINT_RESTORE */
  295. }
  296. return ret;
  297. }
  298. static const struct file_operations inotify_fops = {
  299. .show_fdinfo = inotify_show_fdinfo,
  300. .poll = inotify_poll,
  301. .read = inotify_read,
  302. .fasync = fsnotify_fasync,
  303. .release = inotify_release,
  304. .unlocked_ioctl = inotify_ioctl,
  305. .compat_ioctl = inotify_ioctl,
  306. .llseek = noop_llseek,
  307. };
  308. /*
  309. * find_inode - resolve a user-given path to a specific inode
  310. */
  311. static int inotify_find_inode(const char __user *dirname, struct path *path,
  312. unsigned int flags, __u64 mask)
  313. {
  314. int error;
  315. error = user_path_at(AT_FDCWD, dirname, flags, path);
  316. if (error)
  317. return error;
  318. /* you can only watch an inode if you have read permissions on it */
  319. error = path_permission(path, MAY_READ);
  320. if (error) {
  321. path_put(path);
  322. return error;
  323. }
  324. error = security_path_notify(path, mask,
  325. FSNOTIFY_OBJ_TYPE_INODE);
  326. if (error)
  327. path_put(path);
  328. return error;
  329. }
  330. static int inotify_add_to_idr(struct idr *idr, spinlock_t *idr_lock,
  331. struct inotify_inode_mark *i_mark)
  332. {
  333. int ret;
  334. idr_preload(GFP_KERNEL);
  335. spin_lock(idr_lock);
  336. ret = idr_alloc_cyclic(idr, i_mark, 1, 0, GFP_NOWAIT);
  337. if (ret >= 0) {
  338. /* we added the mark to the idr, take a reference */
  339. i_mark->wd = ret;
  340. fsnotify_get_mark(&i_mark->fsn_mark);
  341. }
  342. spin_unlock(idr_lock);
  343. idr_preload_end();
  344. return ret < 0 ? ret : 0;
  345. }
  346. static struct inotify_inode_mark *inotify_idr_find_locked(struct fsnotify_group *group,
  347. int wd)
  348. {
  349. struct idr *idr = &group->inotify_data.idr;
  350. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  351. struct inotify_inode_mark *i_mark;
  352. assert_spin_locked(idr_lock);
  353. i_mark = idr_find(idr, wd);
  354. if (i_mark) {
  355. struct fsnotify_mark *fsn_mark = &i_mark->fsn_mark;
  356. fsnotify_get_mark(fsn_mark);
  357. /* One ref for being in the idr, one ref we just took */
  358. BUG_ON(refcount_read(&fsn_mark->refcnt) < 2);
  359. }
  360. return i_mark;
  361. }
  362. static struct inotify_inode_mark *inotify_idr_find(struct fsnotify_group *group,
  363. int wd)
  364. {
  365. struct inotify_inode_mark *i_mark;
  366. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  367. spin_lock(idr_lock);
  368. i_mark = inotify_idr_find_locked(group, wd);
  369. spin_unlock(idr_lock);
  370. return i_mark;
  371. }
  372. /*
  373. * Remove the mark from the idr (if present) and drop the reference
  374. * on the mark because it was in the idr.
  375. */
  376. static void inotify_remove_from_idr(struct fsnotify_group *group,
  377. struct inotify_inode_mark *i_mark)
  378. {
  379. struct idr *idr = &group->inotify_data.idr;
  380. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  381. struct inotify_inode_mark *found_i_mark = NULL;
  382. int wd;
  383. spin_lock(idr_lock);
  384. wd = i_mark->wd;
  385. /*
  386. * does this i_mark think it is in the idr? we shouldn't get called
  387. * if it wasn't....
  388. */
  389. if (wd == -1) {
  390. WARN_ONCE(1, "%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. goto out;
  393. }
  394. /* Lets look in the idr to see if we find it */
  395. found_i_mark = inotify_idr_find_locked(group, wd);
  396. if (unlikely(!found_i_mark)) {
  397. WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
  398. __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
  399. goto out;
  400. }
  401. /*
  402. * We found an mark in the idr at the right wd, but it's
  403. * not the mark we were told to remove. eparis seriously
  404. * fucked up somewhere.
  405. */
  406. if (unlikely(found_i_mark != i_mark)) {
  407. WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
  408. "found_i_mark=%p found_i_mark->wd=%d "
  409. "found_i_mark->group=%p\n", __func__, i_mark,
  410. i_mark->wd, i_mark->fsn_mark.group, found_i_mark,
  411. found_i_mark->wd, found_i_mark->fsn_mark.group);
  412. goto out;
  413. }
  414. /*
  415. * One ref for being in the idr
  416. * one ref grabbed by inotify_idr_find
  417. */
  418. if (unlikely(refcount_read(&i_mark->fsn_mark.refcnt) < 2)) {
  419. printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p\n",
  420. __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group);
  421. /* we can't really recover with bad ref cnting.. */
  422. BUG();
  423. }
  424. idr_remove(idr, wd);
  425. /* Removed from the idr, drop that ref. */
  426. fsnotify_put_mark(&i_mark->fsn_mark);
  427. out:
  428. i_mark->wd = -1;
  429. spin_unlock(idr_lock);
  430. /* match the ref taken by inotify_idr_find_locked() */
  431. if (found_i_mark)
  432. fsnotify_put_mark(&found_i_mark->fsn_mark);
  433. }
  434. /*
  435. * Send IN_IGNORED for this wd, remove this wd from the idr.
  436. */
  437. void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
  438. struct fsnotify_group *group)
  439. {
  440. struct inotify_inode_mark *i_mark;
  441. /* Queue ignore event for the watch */
  442. inotify_handle_inode_event(fsn_mark, FS_IN_IGNORED, NULL, NULL, NULL,
  443. 0);
  444. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  445. /* remove this mark from the idr */
  446. inotify_remove_from_idr(group, i_mark);
  447. dec_inotify_watches(group->inotify_data.ucounts);
  448. }
  449. static int inotify_update_existing_watch(struct fsnotify_group *group,
  450. struct inode *inode,
  451. u32 arg)
  452. {
  453. struct fsnotify_mark *fsn_mark;
  454. struct inotify_inode_mark *i_mark;
  455. __u32 old_mask, new_mask;
  456. int replace = !(arg & IN_MASK_ADD);
  457. int create = (arg & IN_MASK_CREATE);
  458. int ret;
  459. fsn_mark = fsnotify_find_mark(&inode->i_fsnotify_marks, group);
  460. if (!fsn_mark)
  461. return -ENOENT;
  462. else if (create) {
  463. ret = -EEXIST;
  464. goto out;
  465. }
  466. i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
  467. spin_lock(&fsn_mark->lock);
  468. old_mask = fsn_mark->mask;
  469. if (replace) {
  470. fsn_mark->mask = 0;
  471. fsn_mark->flags &= ~INOTIFY_MARK_FLAGS;
  472. }
  473. fsn_mark->mask |= inotify_arg_to_mask(inode, arg);
  474. fsn_mark->flags |= inotify_arg_to_flags(arg);
  475. new_mask = fsn_mark->mask;
  476. spin_unlock(&fsn_mark->lock);
  477. if (old_mask != new_mask) {
  478. /* more bits in old than in new? */
  479. int dropped = (old_mask & ~new_mask);
  480. /* more bits in this fsn_mark than the inode's mask? */
  481. int do_inode = (new_mask & ~inode->i_fsnotify_mask);
  482. /* update the inode with this new fsn_mark */
  483. if (dropped || do_inode)
  484. fsnotify_recalc_mask(inode->i_fsnotify_marks);
  485. }
  486. /* return the wd */
  487. ret = i_mark->wd;
  488. out:
  489. /* match the get from fsnotify_find_mark() */
  490. fsnotify_put_mark(fsn_mark);
  491. return ret;
  492. }
  493. static int inotify_new_watch(struct fsnotify_group *group,
  494. struct inode *inode,
  495. u32 arg)
  496. {
  497. struct inotify_inode_mark *tmp_i_mark;
  498. int ret;
  499. struct idr *idr = &group->inotify_data.idr;
  500. spinlock_t *idr_lock = &group->inotify_data.idr_lock;
  501. tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
  502. if (unlikely(!tmp_i_mark))
  503. return -ENOMEM;
  504. fsnotify_init_mark(&tmp_i_mark->fsn_mark, group);
  505. tmp_i_mark->fsn_mark.mask = inotify_arg_to_mask(inode, arg);
  506. tmp_i_mark->fsn_mark.flags = inotify_arg_to_flags(arg);
  507. tmp_i_mark->wd = -1;
  508. ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
  509. if (ret)
  510. goto out_err;
  511. /* increment the number of watches the user has */
  512. if (!inc_inotify_watches(group->inotify_data.ucounts)) {
  513. inotify_remove_from_idr(group, tmp_i_mark);
  514. ret = -ENOSPC;
  515. goto out_err;
  516. }
  517. /* we are on the idr, now get on the inode */
  518. ret = fsnotify_add_inode_mark_locked(&tmp_i_mark->fsn_mark, inode, 0);
  519. if (ret) {
  520. /* we failed to get on the inode, get off the idr */
  521. inotify_remove_from_idr(group, tmp_i_mark);
  522. goto out_err;
  523. }
  524. /* return the watch descriptor for this new mark */
  525. ret = tmp_i_mark->wd;
  526. out_err:
  527. /* match the ref from fsnotify_init_mark() */
  528. fsnotify_put_mark(&tmp_i_mark->fsn_mark);
  529. return ret;
  530. }
  531. static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
  532. {
  533. int ret = 0;
  534. fsnotify_group_lock(group);
  535. /* try to update and existing watch with the new arg */
  536. ret = inotify_update_existing_watch(group, inode, arg);
  537. /* no mark present, try to add a new one */
  538. if (ret == -ENOENT)
  539. ret = inotify_new_watch(group, inode, arg);
  540. fsnotify_group_unlock(group);
  541. return ret;
  542. }
  543. static struct fsnotify_group *inotify_new_group(unsigned int max_events)
  544. {
  545. struct fsnotify_group *group;
  546. struct inotify_event_info *oevent;
  547. group = fsnotify_alloc_group(&inotify_fsnotify_ops,
  548. FSNOTIFY_GROUP_USER);
  549. if (IS_ERR(group))
  550. return group;
  551. oevent = kmalloc(sizeof(struct inotify_event_info), GFP_KERNEL_ACCOUNT);
  552. if (unlikely(!oevent)) {
  553. fsnotify_destroy_group(group);
  554. return ERR_PTR(-ENOMEM);
  555. }
  556. group->overflow_event = &oevent->fse;
  557. fsnotify_init_event(group->overflow_event);
  558. oevent->mask = FS_Q_OVERFLOW;
  559. oevent->wd = -1;
  560. oevent->sync_cookie = 0;
  561. oevent->name_len = 0;
  562. group->max_events = max_events;
  563. group->memcg = get_mem_cgroup_from_mm(current->mm);
  564. spin_lock_init(&group->inotify_data.idr_lock);
  565. idr_init(&group->inotify_data.idr);
  566. group->inotify_data.ucounts = inc_ucount(current_user_ns(),
  567. current_euid(),
  568. UCOUNT_INOTIFY_INSTANCES);
  569. if (!group->inotify_data.ucounts) {
  570. fsnotify_destroy_group(group);
  571. return ERR_PTR(-EMFILE);
  572. }
  573. return group;
  574. }
  575. /* inotify syscalls */
  576. static int do_inotify_init(int flags)
  577. {
  578. struct fsnotify_group *group;
  579. int ret;
  580. /* Check the IN_* constants for consistency. */
  581. BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
  582. BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
  583. if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
  584. return -EINVAL;
  585. /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
  586. group = inotify_new_group(inotify_max_queued_events);
  587. if (IS_ERR(group))
  588. return PTR_ERR(group);
  589. ret = anon_inode_getfd("inotify", &inotify_fops, group,
  590. O_RDONLY | flags);
  591. if (ret < 0)
  592. fsnotify_destroy_group(group);
  593. return ret;
  594. }
  595. SYSCALL_DEFINE1(inotify_init1, int, flags)
  596. {
  597. return do_inotify_init(flags);
  598. }
  599. SYSCALL_DEFINE0(inotify_init)
  600. {
  601. return do_inotify_init(0);
  602. }
  603. SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
  604. u32, mask)
  605. {
  606. struct fsnotify_group *group;
  607. struct inode *inode;
  608. struct path path;
  609. struct path alteredpath;
  610. struct path *canonical_path = &path;
  611. struct fd f;
  612. int ret;
  613. unsigned flags = 0;
  614. /*
  615. * We share a lot of code with fs/dnotify. We also share
  616. * the bit layout between inotify's IN_* and the fsnotify
  617. * FS_*. This check ensures that only the inotify IN_*
  618. * bits get passed in and set in watches/events.
  619. */
  620. if (unlikely(mask & ~ALL_INOTIFY_BITS))
  621. return -EINVAL;
  622. /*
  623. * Require at least one valid bit set in the mask.
  624. * Without _something_ set, we would have no events to
  625. * watch for.
  626. */
  627. if (unlikely(!(mask & ALL_INOTIFY_BITS)))
  628. return -EINVAL;
  629. f = fdget(fd);
  630. if (unlikely(!f.file))
  631. return -EBADF;
  632. /* IN_MASK_ADD and IN_MASK_CREATE don't make sense together */
  633. if (unlikely((mask & IN_MASK_ADD) && (mask & IN_MASK_CREATE))) {
  634. ret = -EINVAL;
  635. goto fput_and_out;
  636. }
  637. /* verify that this is indeed an inotify instance */
  638. if (unlikely(f.file->f_op != &inotify_fops)) {
  639. ret = -EINVAL;
  640. goto fput_and_out;
  641. }
  642. if (!(mask & IN_DONT_FOLLOW))
  643. flags |= LOOKUP_FOLLOW;
  644. if (mask & IN_ONLYDIR)
  645. flags |= LOOKUP_DIRECTORY;
  646. ret = inotify_find_inode(pathname, &path, flags,
  647. (mask & IN_ALL_EVENTS));
  648. if (ret)
  649. goto fput_and_out;
  650. /* support stacked filesystems */
  651. if (path.dentry && path.dentry->d_op) {
  652. if (path.dentry->d_op->d_canonical_path) {
  653. ret = path.dentry->d_op->d_canonical_path(&path,
  654. &alteredpath);
  655. if (ret)
  656. goto path_put_and_out;
  657. canonical_path = &alteredpath;
  658. path_put(&path);
  659. }
  660. }
  661. /* inode held in place by reference to path; group by fget on fd */
  662. inode = canonical_path->dentry->d_inode;
  663. group = f.file->private_data;
  664. /* create/update an inode mark */
  665. ret = inotify_update_watch(group, inode, mask);
  666. path_put_and_out:
  667. path_put(canonical_path);
  668. fput_and_out:
  669. fdput(f);
  670. return ret;
  671. }
  672. SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
  673. {
  674. struct fsnotify_group *group;
  675. struct inotify_inode_mark *i_mark;
  676. struct fd f;
  677. int ret = -EINVAL;
  678. f = fdget(fd);
  679. if (unlikely(!f.file))
  680. return -EBADF;
  681. /* verify that this is indeed an inotify instance */
  682. if (unlikely(f.file->f_op != &inotify_fops))
  683. goto out;
  684. group = f.file->private_data;
  685. i_mark = inotify_idr_find(group, wd);
  686. if (unlikely(!i_mark))
  687. goto out;
  688. ret = 0;
  689. fsnotify_destroy_mark(&i_mark->fsn_mark, group);
  690. /* match ref taken by inotify_idr_find */
  691. fsnotify_put_mark(&i_mark->fsn_mark);
  692. out:
  693. fdput(f);
  694. return ret;
  695. }
  696. /*
  697. * inotify_user_setup - Our initialization function. Note that we cannot return
  698. * error because we have compiled-in VFS hooks. So an (unlikely) failure here
  699. * must result in panic().
  700. */
  701. static int __init inotify_user_setup(void)
  702. {
  703. unsigned long watches_max;
  704. struct sysinfo si;
  705. si_meminfo(&si);
  706. /*
  707. * Allow up to 1% of addressable memory to be allocated for inotify
  708. * watches (per user) limited to the range [8192, 1048576].
  709. */
  710. watches_max = (((si.totalram - si.totalhigh) / 100) << PAGE_SHIFT) /
  711. INOTIFY_WATCH_COST;
  712. watches_max = clamp(watches_max, 8192UL, 1048576UL);
  713. BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
  714. BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
  715. BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
  716. BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
  717. BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
  718. BUILD_BUG_ON(IN_OPEN != FS_OPEN);
  719. BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
  720. BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
  721. BUILD_BUG_ON(IN_CREATE != FS_CREATE);
  722. BUILD_BUG_ON(IN_DELETE != FS_DELETE);
  723. BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
  724. BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
  725. BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
  726. BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
  727. BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
  728. BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
  729. BUILD_BUG_ON(HWEIGHT32(ALL_INOTIFY_BITS) != 22);
  730. inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark,
  731. SLAB_PANIC|SLAB_ACCOUNT);
  732. inotify_max_queued_events = 16384;
  733. init_user_ns.ucount_max[UCOUNT_INOTIFY_INSTANCES] = 128;
  734. init_user_ns.ucount_max[UCOUNT_INOTIFY_WATCHES] = watches_max;
  735. inotify_sysctls_init();
  736. return 0;
  737. }
  738. fs_initcall(inotify_user_setup);