]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/notify/fanotify/fanotify_user.c
fanotify: do not return 0 in a void function
[net-next-2.6.git] / fs / notify / fanotify / fanotify_user.c
1 #include <linux/fanotify.h>
2 #include <linux/fcntl.h>
3 #include <linux/file.h>
4 #include <linux/fs.h>
5 #include <linux/anon_inodes.h>
6 #include <linux/fsnotify_backend.h>
7 #include <linux/init.h>
8 #include <linux/mount.h>
9 #include <linux/namei.h>
10 #include <linux/poll.h>
11 #include <linux/security.h>
12 #include <linux/syscalls.h>
13 #include <linux/types.h>
14 #include <linux/uaccess.h>
15
16 #include <asm/ioctls.h>
17
18 extern const struct fsnotify_ops fanotify_fsnotify_ops;
19
20 static struct kmem_cache *fanotify_mark_cache __read_mostly;
21 static struct kmem_cache *fanotify_response_event_cache __read_mostly;
22
23 struct fanotify_response_event {
24         struct list_head list;
25         __s32 fd;
26         struct fsnotify_event *event;
27 };
28
29 /*
30  * Get an fsnotify notification event if one exists and is small
31  * enough to fit in "count". Return an error pointer if the count
32  * is not large enough.
33  *
34  * Called with the group->notification_mutex held.
35  */
36 static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
37                                             size_t count)
38 {
39         BUG_ON(!mutex_is_locked(&group->notification_mutex));
40
41         pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
42
43         if (fsnotify_notify_queue_is_empty(group))
44                 return NULL;
45
46         if (FAN_EVENT_METADATA_LEN > count)
47                 return ERR_PTR(-EINVAL);
48
49         /* held the notification_mutex the whole time, so this is the
50          * same event we peeked above */
51         return fsnotify_remove_notify_event(group);
52 }
53
54 static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
55 {
56         int client_fd;
57         struct dentry *dentry;
58         struct vfsmount *mnt;
59         struct file *new_file;
60
61         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
62
63         client_fd = get_unused_fd();
64         if (client_fd < 0)
65                 return client_fd;
66
67         if (event->data_type != FSNOTIFY_EVENT_PATH) {
68                 WARN_ON(1);
69                 put_unused_fd(client_fd);
70                 return -EINVAL;
71         }
72
73         /*
74          * we need a new file handle for the userspace program so it can read even if it was
75          * originally opened O_WRONLY.
76          */
77         dentry = dget(event->path.dentry);
78         mnt = mntget(event->path.mnt);
79         /* it's possible this event was an overflow event.  in that case dentry and mnt
80          * are NULL;  That's fine, just don't call dentry open */
81         if (dentry && mnt)
82                 new_file = dentry_open(dentry, mnt,
83                                        O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
84                                        current_cred());
85         else
86                 new_file = ERR_PTR(-EOVERFLOW);
87         if (IS_ERR(new_file)) {
88                 /*
89                  * we still send an event even if we can't open the file.  this
90                  * can happen when say tasks are gone and we try to open their
91                  * /proc files or we try to open a WRONLY file like in sysfs
92                  * we just send the errno to userspace since there isn't much
93                  * else we can do.
94                  */
95                 put_unused_fd(client_fd);
96                 client_fd = PTR_ERR(new_file);
97         } else {
98                 fd_install(client_fd, new_file);
99         }
100
101         return client_fd;
102 }
103
104 static ssize_t fill_event_metadata(struct fsnotify_group *group,
105                                    struct fanotify_event_metadata *metadata,
106                                    struct fsnotify_event *event)
107 {
108         pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
109                  group, metadata, event);
110
111         metadata->event_len = FAN_EVENT_METADATA_LEN;
112         metadata->vers = FANOTIFY_METADATA_VERSION;
113         metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS;
114         metadata->pid = pid_vnr(event->tgid);
115         metadata->fd = create_fd(group, event);
116
117         return metadata->fd;
118 }
119
120 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
121 static struct fanotify_response_event *dequeue_re(struct fsnotify_group *group,
122                                                   __s32 fd)
123 {
124         struct fanotify_response_event *re, *return_re = NULL;
125
126         mutex_lock(&group->fanotify_data.access_mutex);
127         list_for_each_entry(re, &group->fanotify_data.access_list, list) {
128                 if (re->fd != fd)
129                         continue;
130
131                 list_del_init(&re->list);
132                 return_re = re;
133                 break;
134         }
135         mutex_unlock(&group->fanotify_data.access_mutex);
136
137         pr_debug("%s: found return_re=%p\n", __func__, return_re);
138
139         return return_re;
140 }
141
142 static int process_access_response(struct fsnotify_group *group,
143                                    struct fanotify_response *response_struct)
144 {
145         struct fanotify_response_event *re;
146         __s32 fd = response_struct->fd;
147         __u32 response = response_struct->response;
148
149         pr_debug("%s: group=%p fd=%d response=%d\n", __func__, group,
150                  fd, response);
151         /*
152          * make sure the response is valid, if invalid we do nothing and either
153          * userspace can send a valid responce or we will clean it up after the
154          * timeout
155          */
156         switch (response) {
157         case FAN_ALLOW:
158         case FAN_DENY:
159                 break;
160         default:
161                 return -EINVAL;
162         }
163
164         if (fd < 0)
165                 return -EINVAL;
166
167         re = dequeue_re(group, fd);
168         if (!re)
169                 return -ENOENT;
170
171         re->event->response = response;
172
173         wake_up(&group->fanotify_data.access_waitq);
174
175         kmem_cache_free(fanotify_response_event_cache, re);
176
177         return 0;
178 }
179
180 static int prepare_for_access_response(struct fsnotify_group *group,
181                                        struct fsnotify_event *event,
182                                        __s32 fd)
183 {
184         struct fanotify_response_event *re;
185
186         if (!(event->mask & FAN_ALL_PERM_EVENTS))
187                 return 0;
188
189         re = kmem_cache_alloc(fanotify_response_event_cache, GFP_KERNEL);
190         if (!re)
191                 return -ENOMEM;
192
193         re->event = event;
194         re->fd = fd;
195
196         mutex_lock(&group->fanotify_data.access_mutex);
197         list_add_tail(&re->list, &group->fanotify_data.access_list);
198         mutex_unlock(&group->fanotify_data.access_mutex);
199
200         return 0;
201 }
202
203 static void remove_access_response(struct fsnotify_group *group,
204                                    struct fsnotify_event *event,
205                                    __s32 fd)
206 {
207         struct fanotify_response_event *re;
208
209         if (!(event->mask & FAN_ALL_PERM_EVENTS))
210                 return;
211
212         re = dequeue_re(group, fd);
213         if (!re)
214                 return;
215
216         BUG_ON(re->event != event);
217
218         kmem_cache_free(fanotify_response_event_cache, re);
219
220         return;
221 }
222 #else
223 static int prepare_for_access_response(struct fsnotify_group *group,
224                                        struct fsnotify_event *event,
225                                        __s32 fd)
226 {
227         return 0;
228 }
229
230 static void remove_access_response(struct fsnotify_group *group,
231                                    struct fsnotify_event *event,
232                                    __s32 fd)
233 {
234         return;
235 }
236 #endif
237
238 static ssize_t copy_event_to_user(struct fsnotify_group *group,
239                                   struct fsnotify_event *event,
240                                   char __user *buf)
241 {
242         struct fanotify_event_metadata fanotify_event_metadata;
243         int fd, ret;
244
245         pr_debug("%s: group=%p event=%p\n", __func__, group, event);
246
247         fd = fill_event_metadata(group, &fanotify_event_metadata, event);
248         if (fd < 0)
249                 return fd;
250
251         ret = prepare_for_access_response(group, event, fd);
252         if (ret)
253                 goto out_close_fd;
254
255         ret = -EFAULT;
256         if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
257                 goto out_kill_access_response;
258
259         return FAN_EVENT_METADATA_LEN;
260
261 out_kill_access_response:
262         remove_access_response(group, event, fd);
263 out_close_fd:
264         sys_close(fd);
265         return ret;
266 }
267
268 /* intofiy userspace file descriptor functions */
269 static unsigned int fanotify_poll(struct file *file, poll_table *wait)
270 {
271         struct fsnotify_group *group = file->private_data;
272         int ret = 0;
273
274         poll_wait(file, &group->notification_waitq, wait);
275         mutex_lock(&group->notification_mutex);
276         if (!fsnotify_notify_queue_is_empty(group))
277                 ret = POLLIN | POLLRDNORM;
278         mutex_unlock(&group->notification_mutex);
279
280         return ret;
281 }
282
283 static ssize_t fanotify_read(struct file *file, char __user *buf,
284                              size_t count, loff_t *pos)
285 {
286         struct fsnotify_group *group;
287         struct fsnotify_event *kevent;
288         char __user *start;
289         int ret;
290         DEFINE_WAIT(wait);
291
292         start = buf;
293         group = file->private_data;
294
295         pr_debug("%s: group=%p\n", __func__, group);
296
297         while (1) {
298                 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
299
300                 mutex_lock(&group->notification_mutex);
301                 kevent = get_one_event(group, count);
302                 mutex_unlock(&group->notification_mutex);
303
304                 if (kevent) {
305                         ret = PTR_ERR(kevent);
306                         if (IS_ERR(kevent))
307                                 break;
308                         ret = copy_event_to_user(group, kevent, buf);
309                         fsnotify_put_event(kevent);
310                         if (ret < 0)
311                                 break;
312                         buf += ret;
313                         count -= ret;
314                         continue;
315                 }
316
317                 ret = -EAGAIN;
318                 if (file->f_flags & O_NONBLOCK)
319                         break;
320                 ret = -EINTR;
321                 if (signal_pending(current))
322                         break;
323
324                 if (start != buf)
325                         break;
326
327                 schedule();
328         }
329
330         finish_wait(&group->notification_waitq, &wait);
331         if (start != buf && ret != -EFAULT)
332                 ret = buf - start;
333         return ret;
334 }
335
336 static ssize_t fanotify_write(struct file *file, const char __user *buf, size_t count, loff_t *pos)
337 {
338 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
339         struct fanotify_response response = { .fd = -1, .response = -1 };
340         struct fsnotify_group *group;
341         int ret;
342
343         group = file->private_data;
344
345         if (count > sizeof(response))
346                 count = sizeof(response);
347
348         pr_debug("%s: group=%p count=%zu\n", __func__, group, count);
349
350         if (copy_from_user(&response, buf, count))
351                 return -EFAULT;
352
353         ret = process_access_response(group, &response);
354         if (ret < 0)
355                 count = ret;
356
357         return count;
358 #else
359         return -EINVAL;
360 #endif
361 }
362
363 static int fanotify_release(struct inode *ignored, struct file *file)
364 {
365         struct fsnotify_group *group = file->private_data;
366
367         pr_debug("%s: file=%p group=%p\n", __func__, file, group);
368
369         /* matches the fanotify_init->fsnotify_alloc_group */
370         fsnotify_put_group(group);
371
372         return 0;
373 }
374
375 static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
376 {
377         struct fsnotify_group *group;
378         struct fsnotify_event_holder *holder;
379         void __user *p;
380         int ret = -ENOTTY;
381         size_t send_len = 0;
382
383         group = file->private_data;
384
385         p = (void __user *) arg;
386
387         switch (cmd) {
388         case FIONREAD:
389                 mutex_lock(&group->notification_mutex);
390                 list_for_each_entry(holder, &group->notification_list, event_list)
391                         send_len += FAN_EVENT_METADATA_LEN;
392                 mutex_unlock(&group->notification_mutex);
393                 ret = put_user(send_len, (int __user *) p);
394                 break;
395         }
396
397         return ret;
398 }
399
400 static const struct file_operations fanotify_fops = {
401         .poll           = fanotify_poll,
402         .read           = fanotify_read,
403         .write          = fanotify_write,
404         .fasync         = NULL,
405         .release        = fanotify_release,
406         .unlocked_ioctl = fanotify_ioctl,
407         .compat_ioctl   = fanotify_ioctl,
408 };
409
410 static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
411 {
412         kmem_cache_free(fanotify_mark_cache, fsn_mark);
413 }
414
415 static int fanotify_find_path(int dfd, const char __user *filename,
416                               struct path *path, unsigned int flags)
417 {
418         int ret;
419
420         pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
421                  dfd, filename, flags);
422
423         if (filename == NULL) {
424                 struct file *file;
425                 int fput_needed;
426
427                 ret = -EBADF;
428                 file = fget_light(dfd, &fput_needed);
429                 if (!file)
430                         goto out;
431
432                 ret = -ENOTDIR;
433                 if ((flags & FAN_MARK_ONLYDIR) &&
434                     !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
435                         fput_light(file, fput_needed);
436                         goto out;
437                 }
438
439                 *path = file->f_path;
440                 path_get(path);
441                 fput_light(file, fput_needed);
442         } else {
443                 unsigned int lookup_flags = 0;
444
445                 if (!(flags & FAN_MARK_DONT_FOLLOW))
446                         lookup_flags |= LOOKUP_FOLLOW;
447                 if (flags & FAN_MARK_ONLYDIR)
448                         lookup_flags |= LOOKUP_DIRECTORY;
449
450                 ret = user_path_at(dfd, filename, lookup_flags, path);
451                 if (ret)
452                         goto out;
453         }
454
455         /* you can only watch an inode if you have read permissions on it */
456         ret = inode_permission(path->dentry->d_inode, MAY_READ);
457         if (ret)
458                 path_put(path);
459 out:
460         return ret;
461 }
462
463 static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark,
464                                             __u32 mask,
465                                             unsigned int flags)
466 {
467         __u32 oldmask;
468
469         spin_lock(&fsn_mark->lock);
470         if (!(flags & FAN_MARK_IGNORED_MASK)) {
471                 oldmask = fsn_mark->mask;
472                 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask));
473         } else {
474                 oldmask = fsn_mark->ignored_mask;
475                 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask));
476         }
477         spin_unlock(&fsn_mark->lock);
478
479         if (!(oldmask & ~mask))
480                 fsnotify_destroy_mark(fsn_mark);
481
482         return mask & oldmask;
483 }
484
485 static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group,
486                                          struct vfsmount *mnt, __u32 mask,
487                                          unsigned int flags)
488 {
489         struct fsnotify_mark *fsn_mark = NULL;
490         __u32 removed;
491
492         fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
493         if (!fsn_mark)
494                 return -ENOENT;
495
496         removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
497         fsnotify_put_mark(fsn_mark);
498         if (removed & group->mask)
499                 fsnotify_recalc_group_mask(group);
500         if (removed & mnt->mnt_fsnotify_mask)
501                 fsnotify_recalc_vfsmount_mask(mnt);
502
503         return 0;
504 }
505
506 static int fanotify_remove_inode_mark(struct fsnotify_group *group,
507                                       struct inode *inode, __u32 mask,
508                                       unsigned int flags)
509 {
510         struct fsnotify_mark *fsn_mark = NULL;
511         __u32 removed;
512
513         fsn_mark = fsnotify_find_inode_mark(group, inode);
514         if (!fsn_mark)
515                 return -ENOENT;
516
517         removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags);
518         /* matches the fsnotify_find_inode_mark() */
519         fsnotify_put_mark(fsn_mark);
520
521         if (removed & group->mask)
522                 fsnotify_recalc_group_mask(group);
523         if (removed & inode->i_fsnotify_mask)
524                 fsnotify_recalc_inode_mask(inode);
525
526         return 0;
527 }
528
529 static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark,
530                                        __u32 mask,
531                                        unsigned int flags)
532 {
533         __u32 oldmask;
534
535         spin_lock(&fsn_mark->lock);
536         if (!(flags & FAN_MARK_IGNORED_MASK)) {
537                 oldmask = fsn_mark->mask;
538                 fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask));
539         } else {
540                 oldmask = fsn_mark->ignored_mask;
541                 fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask));
542                 if (flags & FAN_MARK_IGNORED_SURV_MODIFY)
543                         fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY;
544         }
545         spin_unlock(&fsn_mark->lock);
546
547         return mask & ~oldmask;
548 }
549
550 static int fanotify_add_vfsmount_mark(struct fsnotify_group *group,
551                                       struct vfsmount *mnt, __u32 mask,
552                                       unsigned int flags)
553 {
554         struct fsnotify_mark *fsn_mark;
555         __u32 added;
556
557         fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
558         if (!fsn_mark) {
559                 int ret;
560
561                 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
562                 if (!fsn_mark)
563                         return -ENOMEM;
564
565                 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
566                 ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0);
567                 if (ret) {
568                         fanotify_free_mark(fsn_mark);
569                         return ret;
570                 }
571         }
572         added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
573         fsnotify_put_mark(fsn_mark);
574         if (added) {
575                 if (added & ~group->mask)
576                         fsnotify_recalc_group_mask(group);
577                 if (added & ~mnt->mnt_fsnotify_mask)
578                         fsnotify_recalc_vfsmount_mask(mnt);
579         }
580         return 0;
581 }
582
583 static int fanotify_add_inode_mark(struct fsnotify_group *group,
584                                    struct inode *inode, __u32 mask,
585                                    unsigned int flags)
586 {
587         struct fsnotify_mark *fsn_mark;
588         __u32 added;
589
590         pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
591
592         fsn_mark = fsnotify_find_inode_mark(group, inode);
593         if (!fsn_mark) {
594                 int ret;
595
596                 fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
597                 if (!fsn_mark)
598                         return -ENOMEM;
599
600                 fsnotify_init_mark(fsn_mark, fanotify_free_mark);
601                 ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0);
602                 if (ret) {
603                         fanotify_free_mark(fsn_mark);
604                         return ret;
605                 }
606         }
607         added = fanotify_mark_add_to_mask(fsn_mark, mask, flags);
608         fsnotify_put_mark(fsn_mark);
609         if (added) {
610                 if (added & ~group->mask)
611                         fsnotify_recalc_group_mask(group);
612                 if (added & ~inode->i_fsnotify_mask)
613                         fsnotify_recalc_inode_mask(inode);
614         }
615         return 0;
616 }
617
618 /* fanotify syscalls */
619 SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
620                 unsigned int, priority)
621 {
622         struct fsnotify_group *group;
623         int f_flags, fd;
624
625         pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
626                 __func__, flags, event_f_flags, priority);
627
628         if (event_f_flags)
629                 return -EINVAL;
630
631         if (!capable(CAP_SYS_ADMIN))
632                 return -EACCES;
633
634         if (flags & ~FAN_ALL_INIT_FLAGS)
635                 return -EINVAL;
636
637         f_flags = O_RDWR | FMODE_NONOTIFY;
638         if (flags & FAN_CLOEXEC)
639                 f_flags |= O_CLOEXEC;
640         if (flags & FAN_NONBLOCK)
641                 f_flags |= O_NONBLOCK;
642
643         /* fsnotify_alloc_group takes a ref.  Dropped in fanotify_release */
644         group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
645         if (IS_ERR(group))
646                 return PTR_ERR(group);
647
648         group->priority = priority;
649 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
650         mutex_init(&group->fanotify_data.access_mutex);
651         init_waitqueue_head(&group->fanotify_data.access_waitq);
652         INIT_LIST_HEAD(&group->fanotify_data.access_list);
653 #endif
654
655         fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
656         if (fd < 0)
657                 goto out_put_group;
658
659         return fd;
660
661 out_put_group:
662         fsnotify_put_group(group);
663         return fd;
664 }
665
666 SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
667                               __u64 mask, int dfd,
668                               const char  __user * pathname)
669 {
670         struct inode *inode = NULL;
671         struct vfsmount *mnt = NULL;
672         struct fsnotify_group *group;
673         struct file *filp;
674         struct path path;
675         int ret, fput_needed;
676
677         pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
678                  __func__, fanotify_fd, flags, dfd, pathname, mask);
679
680         /* we only use the lower 32 bits as of right now. */
681         if (mask & ((__u64)0xffffffff << 32))
682                 return -EINVAL;
683
684         if (flags & ~FAN_ALL_MARK_FLAGS)
685                 return -EINVAL;
686         switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
687         case FAN_MARK_ADD:
688         case FAN_MARK_REMOVE:
689         case FAN_MARK_FLUSH:
690                 break;
691         default:
692                 return -EINVAL;
693         }
694 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
695         if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD))
696 #else
697         if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD))
698 #endif
699                 return -EINVAL;
700
701         filp = fget_light(fanotify_fd, &fput_needed);
702         if (unlikely(!filp))
703                 return -EBADF;
704
705         /* verify that this is indeed an fanotify instance */
706         ret = -EINVAL;
707         if (unlikely(filp->f_op != &fanotify_fops))
708                 goto fput_and_out;
709
710         ret = fanotify_find_path(dfd, pathname, &path, flags);
711         if (ret)
712                 goto fput_and_out;
713
714         /* inode held in place by reference to path; group by fget on fd */
715         if (!(flags & FAN_MARK_MOUNT))
716                 inode = path.dentry->d_inode;
717         else
718                 mnt = path.mnt;
719         group = filp->private_data;
720
721         /* create/update an inode mark */
722         switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) {
723         case FAN_MARK_ADD:
724                 if (flags & FAN_MARK_MOUNT)
725                         ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags);
726                 else
727                         ret = fanotify_add_inode_mark(group, inode, mask, flags);
728                 break;
729         case FAN_MARK_REMOVE:
730                 if (flags & FAN_MARK_MOUNT)
731                         ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags);
732                 else
733                         ret = fanotify_remove_inode_mark(group, inode, mask, flags);
734                 break;
735         case FAN_MARK_FLUSH:
736                 if (flags & FAN_MARK_MOUNT)
737                         fsnotify_clear_vfsmount_marks_by_group(group);
738                 else
739                         fsnotify_clear_inode_marks_by_group(group);
740                 fsnotify_recalc_group_mask(group);
741                 break;
742         default:
743                 ret = -EINVAL;
744         }
745
746         path_put(&path);
747 fput_and_out:
748         fput_light(filp, fput_needed);
749         return ret;
750 }
751
752 #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
753 asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
754                                   long dfd, long pathname)
755 {
756         return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
757                                   mask, (int) dfd,
758                                   (const char  __user *) pathname);
759 }
760 SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
761 #endif
762
763 /*
764  * fanotify_user_setup - Our initialization function.  Note that we cannnot return
765  * error because we have compiled-in VFS hooks.  So an (unlikely) failure here
766  * must result in panic().
767  */
768 static int __init fanotify_user_setup(void)
769 {
770         fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
771         fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event,
772                                                    SLAB_PANIC);
773
774         return 0;
775 }
776 device_initcall(fanotify_user_setup);