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