inotify_user.c 21 KB

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