]> Pileus Git - ~andy/linux/blob - fs/notify/inotify/inotify_user.c
inotify: provide function for name length rounding
[~andy/linux] / fs / notify / inotify / inotify_user.c
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> /* module_init */
30 #include <linux/inotify.h>
31 #include <linux/kernel.h> /* roundup() */
32 #include <linux/namei.h> /* LOOKUP_FOLLOW */
33 #include <linux/sched.h> /* struct user */
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
42 #include "inotify.h"
43 #include "../fdinfo.h"
44
45 #include <asm/ioctls.h>
46
47 /* these are configurable via /proc/sys/fs/inotify/ */
48 static int inotify_max_user_instances __read_mostly;
49 static int inotify_max_queued_events __read_mostly;
50 static int inotify_max_user_watches __read_mostly;
51
52 static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
53 struct kmem_cache *event_priv_cachep __read_mostly;
54
55 #ifdef CONFIG_SYSCTL
56
57 #include <linux/sysctl.h>
58
59 static int zero;
60
61 ctl_table inotify_table[] = {
62         {
63                 .procname       = "max_user_instances",
64                 .data           = &inotify_max_user_instances,
65                 .maxlen         = sizeof(int),
66                 .mode           = 0644,
67                 .proc_handler   = proc_dointvec_minmax,
68                 .extra1         = &zero,
69         },
70         {
71                 .procname       = "max_user_watches",
72                 .data           = &inotify_max_user_watches,
73                 .maxlen         = sizeof(int),
74                 .mode           = 0644,
75                 .proc_handler   = proc_dointvec_minmax,
76                 .extra1         = &zero,
77         },
78         {
79                 .procname       = "max_queued_events",
80                 .data           = &inotify_max_queued_events,
81                 .maxlen         = sizeof(int),
82                 .mode           = 0644,
83                 .proc_handler   = proc_dointvec_minmax,
84                 .extra1         = &zero
85         },
86         { }
87 };
88 #endif /* CONFIG_SYSCTL */
89
90 static inline __u32 inotify_arg_to_mask(u32 arg)
91 {
92         __u32 mask;
93
94         /*
95          * everything should accept their own ignored, cares about children,
96          * and should receive events when the inode is unmounted
97          */
98         mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD | FS_UNMOUNT);
99
100         /* mask off the flags used to open the fd */
101         mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT | IN_EXCL_UNLINK));
102
103         return mask;
104 }
105
106 static inline u32 inotify_mask_to_arg(__u32 mask)
107 {
108         return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
109                        IN_Q_OVERFLOW);
110 }
111
112 /* intofiy userspace file descriptor functions */
113 static unsigned int inotify_poll(struct file *file, poll_table *wait)
114 {
115         struct fsnotify_group *group = file->private_data;
116         int ret = 0;
117
118         poll_wait(file, &group->notification_waitq, wait);
119         mutex_lock(&group->notification_mutex);
120         if (!fsnotify_notify_queue_is_empty(group))
121                 ret = POLLIN | POLLRDNORM;
122         mutex_unlock(&group->notification_mutex);
123
124         return ret;
125 }
126
127 static int round_event_name_len(struct fsnotify_event *event)
128 {
129         if (!event->name_len)
130                 return 0;
131         return roundup(event->name_len + 1, sizeof(struct inotify_event));
132 }
133
134 /*
135  * Get an inotify_kernel_event if one exists and is small
136  * enough to fit in "count". Return an error pointer if
137  * not large enough.
138  *
139  * Called with the group->notification_mutex held.
140  */
141 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
142                                             size_t count)
143 {
144         size_t event_size = sizeof(struct inotify_event);
145         struct fsnotify_event *event;
146
147         if (fsnotify_notify_queue_is_empty(group))
148                 return NULL;
149
150         event = fsnotify_peek_notify_event(group);
151
152         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
153
154         event_size += round_event_name_len(event);
155         if (event_size > count)
156                 return ERR_PTR(-EINVAL);
157
158         /* held the notification_mutex the whole time, so this is the
159          * same event we peeked above */
160         fsnotify_remove_notify_event(group);
161
162         return event;
163 }
164
165 /*
166  * Copy an event to user space, returning how much we copied.
167  *
168  * We already checked that the event size is smaller than the
169  * buffer we had in "get_one_event()" above.
170  */
171 static ssize_t copy_event_to_user(struct fsnotify_group *group,
172                                   struct fsnotify_event *event,
173                                   char __user *buf)
174 {
175         struct inotify_event inotify_event;
176         struct fsnotify_event_private_data *fsn_priv;
177         struct inotify_event_private_data *priv;
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, event);
183
184         /* we get the inotify watch descriptor from the event private data */
185         spin_lock(&event->lock);
186         fsn_priv = fsnotify_remove_priv_from_event(group, event);
187         spin_unlock(&event->lock);
188
189         if (!fsn_priv)
190                 inotify_event.wd = -1;
191         else {
192                 priv = container_of(fsn_priv, struct inotify_event_private_data,
193                                     fsnotify_event_priv_data);
194                 inotify_event.wd = priv->wd;
195                 inotify_free_event_priv(fsn_priv);
196         }
197
198         name_len = event->name_len;
199         /*
200          * round up name length so it is a multiple of event_size
201          * plus an extra byte for the terminating '\0'.
202          */
203         pad_name_len = round_event_name_len(event);
204         inotify_event.len = pad_name_len;
205         inotify_event.mask = inotify_mask_to_arg(event->mask);
206         inotify_event.cookie = event->sync_cookie;
207
208         /* send the main event */
209         if (copy_to_user(buf, &inotify_event, event_size))
210                 return -EFAULT;
211
212         buf += event_size;
213
214         /*
215          * fsnotify only stores the pathname, so here we have to send the pathname
216          * and then pad that pathname out to a multiple of sizeof(inotify_event)
217          * with zeros.
218          */
219         if (pad_name_len) {
220                 /* copy the path name */
221                 if (copy_to_user(buf, event->file_name, name_len))
222                         return -EFAULT;
223                 buf += name_len;
224
225                 /* fill userspace with 0's */
226                 if (clear_user(buf, pad_name_len - name_len))
227                         return -EFAULT;
228                 event_size += pad_name_len;
229         }
230
231         return event_size;
232 }
233
234 static ssize_t inotify_read(struct file *file, char __user *buf,
235                             size_t count, loff_t *pos)
236 {
237         struct fsnotify_group *group;
238         struct fsnotify_event *kevent;
239         char __user *start;
240         int ret;
241         DEFINE_WAIT(wait);
242
243         start = buf;
244         group = file->private_data;
245
246         while (1) {
247                 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
248
249                 mutex_lock(&group->notification_mutex);
250                 kevent = get_one_event(group, count);
251                 mutex_unlock(&group->notification_mutex);
252
253                 pr_debug("%s: group=%p kevent=%p\n", __func__, group, kevent);
254
255                 if (kevent) {
256                         ret = PTR_ERR(kevent);
257                         if (IS_ERR(kevent))
258                                 break;
259                         ret = copy_event_to_user(group, kevent, buf);
260                         fsnotify_put_event(kevent);
261                         if (ret < 0)
262                                 break;
263                         buf += ret;
264                         count -= ret;
265                         continue;
266                 }
267
268                 ret = -EAGAIN;
269                 if (file->f_flags & O_NONBLOCK)
270                         break;
271                 ret = -ERESTARTSYS;
272                 if (signal_pending(current))
273                         break;
274
275                 if (start != buf)
276                         break;
277
278                 schedule();
279         }
280
281         finish_wait(&group->notification_waitq, &wait);
282         if (start != buf && ret != -EFAULT)
283                 ret = buf - start;
284         return ret;
285 }
286
287 static int inotify_release(struct inode *ignored, struct file *file)
288 {
289         struct fsnotify_group *group = file->private_data;
290
291         pr_debug("%s: group=%p\n", __func__, group);
292
293         /* free this group, matching get was inotify_init->fsnotify_obtain_group */
294         fsnotify_destroy_group(group);
295
296         return 0;
297 }
298
299 static long inotify_ioctl(struct file *file, unsigned int cmd,
300                           unsigned long arg)
301 {
302         struct fsnotify_group *group;
303         struct fsnotify_event_holder *holder;
304         struct fsnotify_event *event;
305         void __user *p;
306         int ret = -ENOTTY;
307         size_t send_len = 0;
308
309         group = file->private_data;
310         p = (void __user *) arg;
311
312         pr_debug("%s: group=%p cmd=%u\n", __func__, group, cmd);
313
314         switch (cmd) {
315         case FIONREAD:
316                 mutex_lock(&group->notification_mutex);
317                 list_for_each_entry(holder, &group->notification_list, event_list) {
318                         event = holder->event;
319                         send_len += sizeof(struct inotify_event);
320                         send_len += round_event_name_len(event);
321                 }
322                 mutex_unlock(&group->notification_mutex);
323                 ret = put_user(send_len, (int __user *) p);
324                 break;
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  */
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
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
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(atomic_read(&fsn_mark->refcnt) < 2);
395         }
396
397         return i_mark;
398 }
399
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 static void do_inotify_remove_from_idr(struct fsnotify_group *group,
414                                        struct inotify_inode_mark *i_mark)
415 {
416         struct idr *idr = &group->inotify_data.idr;
417         spinlock_t *idr_lock = &group->inotify_data.idr_lock;
418         int wd = i_mark->wd;
419
420         assert_spin_locked(idr_lock);
421
422         idr_remove(idr, wd);
423
424         /* removed from the idr, drop that ref */
425         fsnotify_put_mark(&i_mark->fsn_mark);
426 }
427
428 /*
429  * Remove the mark from the idr (if present) and drop the reference
430  * on the mark because it was in the idr.
431  */
432 static void inotify_remove_from_idr(struct fsnotify_group *group,
433                                     struct inotify_inode_mark *i_mark)
434 {
435         spinlock_t *idr_lock = &group->inotify_data.idr_lock;
436         struct inotify_inode_mark *found_i_mark = NULL;
437         int wd;
438
439         spin_lock(idr_lock);
440         wd = i_mark->wd;
441
442         /*
443          * does this i_mark think it is in the idr?  we shouldn't get called
444          * if it wasn't....
445          */
446         if (wd == -1) {
447                 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
448                         " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
449                         i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
450                 goto out;
451         }
452
453         /* Lets look in the idr to see if we find it */
454         found_i_mark = inotify_idr_find_locked(group, wd);
455         if (unlikely(!found_i_mark)) {
456                 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
457                         " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
458                         i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
459                 goto out;
460         }
461
462         /*
463          * We found an mark in the idr at the right wd, but it's
464          * not the mark we were told to remove.  eparis seriously
465          * fucked up somewhere.
466          */
467         if (unlikely(found_i_mark != i_mark)) {
468                 WARN_ONCE(1, "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p "
469                         "mark->inode=%p found_i_mark=%p found_i_mark->wd=%d "
470                         "found_i_mark->group=%p found_i_mark->inode=%p\n",
471                         __func__, i_mark, i_mark->wd, i_mark->fsn_mark.group,
472                         i_mark->fsn_mark.i.inode, found_i_mark, found_i_mark->wd,
473                         found_i_mark->fsn_mark.group,
474                         found_i_mark->fsn_mark.i.inode);
475                 goto out;
476         }
477
478         /*
479          * One ref for being in the idr
480          * one ref held by the caller trying to kill us
481          * one ref grabbed by inotify_idr_find
482          */
483         if (unlikely(atomic_read(&i_mark->fsn_mark.refcnt) < 3)) {
484                 printk(KERN_ERR "%s: i_mark=%p i_mark->wd=%d i_mark->group=%p"
485                         " i_mark->inode=%p\n", __func__, i_mark, i_mark->wd,
486                         i_mark->fsn_mark.group, i_mark->fsn_mark.i.inode);
487                 /* we can't really recover with bad ref cnting.. */
488                 BUG();
489         }
490
491         do_inotify_remove_from_idr(group, i_mark);
492 out:
493         /* match the ref taken by inotify_idr_find_locked() */
494         if (found_i_mark)
495                 fsnotify_put_mark(&found_i_mark->fsn_mark);
496         i_mark->wd = -1;
497         spin_unlock(idr_lock);
498 }
499
500 /*
501  * Send IN_IGNORED for this wd, remove this wd from the idr.
502  */
503 void inotify_ignored_and_remove_idr(struct fsnotify_mark *fsn_mark,
504                                     struct fsnotify_group *group)
505 {
506         struct inotify_inode_mark *i_mark;
507         struct fsnotify_event *ignored_event, *notify_event;
508         struct inotify_event_private_data *event_priv;
509         struct fsnotify_event_private_data *fsn_event_priv;
510         int ret;
511
512         i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
513
514         ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL,
515                                               FSNOTIFY_EVENT_NONE, NULL, 0,
516                                               GFP_NOFS);
517         if (!ignored_event)
518                 goto skip_send_ignore;
519
520         event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS);
521         if (unlikely(!event_priv))
522                 goto skip_send_ignore;
523
524         fsn_event_priv = &event_priv->fsnotify_event_priv_data;
525
526         fsnotify_get_group(group);
527         fsn_event_priv->group = group;
528         event_priv->wd = i_mark->wd;
529
530         notify_event = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv, NULL);
531         if (notify_event) {
532                 if (IS_ERR(notify_event))
533                         ret = PTR_ERR(notify_event);
534                 else
535                         fsnotify_put_event(notify_event);
536                 inotify_free_event_priv(fsn_event_priv);
537         }
538
539 skip_send_ignore:
540         /* matches the reference taken when the event was created */
541         if (ignored_event)
542                 fsnotify_put_event(ignored_event);
543
544         /* remove this mark from the idr */
545         inotify_remove_from_idr(group, i_mark);
546
547         atomic_dec(&group->inotify_data.user->inotify_watches);
548 }
549
550 /* ding dong the mark is dead */
551 static void inotify_free_mark(struct fsnotify_mark *fsn_mark)
552 {
553         struct inotify_inode_mark *i_mark;
554
555         i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
556
557         kmem_cache_free(inotify_inode_mark_cachep, i_mark);
558 }
559
560 static int inotify_update_existing_watch(struct fsnotify_group *group,
561                                          struct inode *inode,
562                                          u32 arg)
563 {
564         struct fsnotify_mark *fsn_mark;
565         struct inotify_inode_mark *i_mark;
566         __u32 old_mask, new_mask;
567         __u32 mask;
568         int add = (arg & IN_MASK_ADD);
569         int ret;
570
571         mask = inotify_arg_to_mask(arg);
572
573         fsn_mark = fsnotify_find_inode_mark(group, inode);
574         if (!fsn_mark)
575                 return -ENOENT;
576
577         i_mark = container_of(fsn_mark, struct inotify_inode_mark, fsn_mark);
578
579         spin_lock(&fsn_mark->lock);
580
581         old_mask = fsn_mark->mask;
582         if (add)
583                 fsnotify_set_mark_mask_locked(fsn_mark, (fsn_mark->mask | mask));
584         else
585                 fsnotify_set_mark_mask_locked(fsn_mark, mask);
586         new_mask = fsn_mark->mask;
587
588         spin_unlock(&fsn_mark->lock);
589
590         if (old_mask != new_mask) {
591                 /* more bits in old than in new? */
592                 int dropped = (old_mask & ~new_mask);
593                 /* more bits in this fsn_mark than the inode's mask? */
594                 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
595
596                 /* update the inode with this new fsn_mark */
597                 if (dropped || do_inode)
598                         fsnotify_recalc_inode_mask(inode);
599
600         }
601
602         /* return the wd */
603         ret = i_mark->wd;
604
605         /* match the get from fsnotify_find_mark() */
606         fsnotify_put_mark(fsn_mark);
607
608         return ret;
609 }
610
611 static int inotify_new_watch(struct fsnotify_group *group,
612                              struct inode *inode,
613                              u32 arg)
614 {
615         struct inotify_inode_mark *tmp_i_mark;
616         __u32 mask;
617         int ret;
618         struct idr *idr = &group->inotify_data.idr;
619         spinlock_t *idr_lock = &group->inotify_data.idr_lock;
620
621         mask = inotify_arg_to_mask(arg);
622
623         tmp_i_mark = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
624         if (unlikely(!tmp_i_mark))
625                 return -ENOMEM;
626
627         fsnotify_init_mark(&tmp_i_mark->fsn_mark, inotify_free_mark);
628         tmp_i_mark->fsn_mark.mask = mask;
629         tmp_i_mark->wd = -1;
630
631         ret = -ENOSPC;
632         if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
633                 goto out_err;
634
635         ret = inotify_add_to_idr(idr, idr_lock, tmp_i_mark);
636         if (ret)
637                 goto out_err;
638
639         /* we are on the idr, now get on the inode */
640         ret = fsnotify_add_mark_locked(&tmp_i_mark->fsn_mark, group, inode,
641                                        NULL, 0);
642         if (ret) {
643                 /* we failed to get on the inode, get off the idr */
644                 inotify_remove_from_idr(group, tmp_i_mark);
645                 goto out_err;
646         }
647
648         /* increment the number of watches the user has */
649         atomic_inc(&group->inotify_data.user->inotify_watches);
650
651         /* return the watch descriptor for this new mark */
652         ret = tmp_i_mark->wd;
653
654 out_err:
655         /* match the ref from fsnotify_init_mark() */
656         fsnotify_put_mark(&tmp_i_mark->fsn_mark);
657
658         return ret;
659 }
660
661 static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
662 {
663         int ret = 0;
664
665         mutex_lock(&group->mark_mutex);
666         /* try to update and existing watch with the new arg */
667         ret = inotify_update_existing_watch(group, inode, arg);
668         /* no mark present, try to add a new one */
669         if (ret == -ENOENT)
670                 ret = inotify_new_watch(group, inode, arg);
671         mutex_unlock(&group->mark_mutex);
672
673         return ret;
674 }
675
676 static struct fsnotify_group *inotify_new_group(unsigned int max_events)
677 {
678         struct fsnotify_group *group;
679
680         group = fsnotify_alloc_group(&inotify_fsnotify_ops);
681         if (IS_ERR(group))
682                 return group;
683
684         group->max_events = max_events;
685
686         spin_lock_init(&group->inotify_data.idr_lock);
687         idr_init(&group->inotify_data.idr);
688         group->inotify_data.user = get_current_user();
689
690         if (atomic_inc_return(&group->inotify_data.user->inotify_devs) >
691             inotify_max_user_instances) {
692                 fsnotify_destroy_group(group);
693                 return ERR_PTR(-EMFILE);
694         }
695
696         return group;
697 }
698
699
700 /* inotify syscalls */
701 SYSCALL_DEFINE1(inotify_init1, int, flags)
702 {
703         struct fsnotify_group *group;
704         int ret;
705
706         /* Check the IN_* constants for consistency.  */
707         BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
708         BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
709
710         if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
711                 return -EINVAL;
712
713         /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
714         group = inotify_new_group(inotify_max_queued_events);
715         if (IS_ERR(group))
716                 return PTR_ERR(group);
717
718         ret = anon_inode_getfd("inotify", &inotify_fops, group,
719                                   O_RDONLY | flags);
720         if (ret < 0)
721                 fsnotify_destroy_group(group);
722
723         return ret;
724 }
725
726 SYSCALL_DEFINE0(inotify_init)
727 {
728         return sys_inotify_init1(0);
729 }
730
731 SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
732                 u32, mask)
733 {
734         struct fsnotify_group *group;
735         struct inode *inode;
736         struct path path;
737         struct fd f;
738         int ret;
739         unsigned flags = 0;
740
741         /* don't allow invalid bits: we don't want flags set */
742         if (unlikely(!(mask & ALL_INOTIFY_BITS)))
743                 return -EINVAL;
744
745         f = fdget(fd);
746         if (unlikely(!f.file))
747                 return -EBADF;
748
749         /* verify that this is indeed an inotify instance */
750         if (unlikely(f.file->f_op != &inotify_fops)) {
751                 ret = -EINVAL;
752                 goto fput_and_out;
753         }
754
755         if (!(mask & IN_DONT_FOLLOW))
756                 flags |= LOOKUP_FOLLOW;
757         if (mask & IN_ONLYDIR)
758                 flags |= LOOKUP_DIRECTORY;
759
760         ret = inotify_find_inode(pathname, &path, flags);
761         if (ret)
762                 goto fput_and_out;
763
764         /* inode held in place by reference to path; group by fget on fd */
765         inode = path.dentry->d_inode;
766         group = f.file->private_data;
767
768         /* create/update an inode mark */
769         ret = inotify_update_watch(group, inode, mask);
770         path_put(&path);
771 fput_and_out:
772         fdput(f);
773         return ret;
774 }
775
776 SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
777 {
778         struct fsnotify_group *group;
779         struct inotify_inode_mark *i_mark;
780         struct fd f;
781         int ret = 0;
782
783         f = fdget(fd);
784         if (unlikely(!f.file))
785                 return -EBADF;
786
787         /* verify that this is indeed an inotify instance */
788         ret = -EINVAL;
789         if (unlikely(f.file->f_op != &inotify_fops))
790                 goto out;
791
792         group = f.file->private_data;
793
794         ret = -EINVAL;
795         i_mark = inotify_idr_find(group, wd);
796         if (unlikely(!i_mark))
797                 goto out;
798
799         ret = 0;
800
801         fsnotify_destroy_mark(&i_mark->fsn_mark, group);
802
803         /* match ref taken by inotify_idr_find */
804         fsnotify_put_mark(&i_mark->fsn_mark);
805
806 out:
807         fdput(f);
808         return ret;
809 }
810
811 /*
812  * inotify_user_setup - Our initialization function.  Note that we cannot return
813  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
814  * must result in panic().
815  */
816 static int __init inotify_user_setup(void)
817 {
818         BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);
819         BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);
820         BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);
821         BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);
822         BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);
823         BUILD_BUG_ON(IN_OPEN != FS_OPEN);
824         BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);
825         BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);
826         BUILD_BUG_ON(IN_CREATE != FS_CREATE);
827         BUILD_BUG_ON(IN_DELETE != FS_DELETE);
828         BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);
829         BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);
830         BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);
831         BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);
832         BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);
833         BUILD_BUG_ON(IN_EXCL_UNLINK != FS_EXCL_UNLINK);
834         BUILD_BUG_ON(IN_ISDIR != FS_ISDIR);
835         BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);
836
837         BUG_ON(hweight32(ALL_INOTIFY_BITS) != 21);
838
839         inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark, SLAB_PANIC);
840         event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
841
842         inotify_max_queued_events = 16384;
843         inotify_max_user_instances = 128;
844         inotify_max_user_watches = 8192;
845
846         return 0;
847 }
848 module_init(inotify_user_setup);