inotify_user.c 21 KB

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