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