blob: 96d4ffd725193c1c40367252985cb52606e93a14 [file] [log] [blame]
Eric Paris11637e42009-12-17 21:24:25 -05001#include <linux/fcntl.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05002#include <linux/file.h>
Eric Paris11637e42009-12-17 21:24:25 -05003#include <linux/fs.h>
Eric Paris52c923d2009-12-17 21:24:26 -05004#include <linux/anon_inodes.h>
Eric Paris11637e42009-12-17 21:24:25 -05005#include <linux/fsnotify_backend.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05006#include <linux/init.h>
Eric Parisa1014f12009-12-17 21:24:26 -05007#include <linux/mount.h>
Eric Paris2a3edf82009-12-17 21:24:26 -05008#include <linux/namei.h>
Eric Parisa1014f12009-12-17 21:24:26 -05009#include <linux/poll.h>
Eric Paris11637e42009-12-17 21:24:25 -050010#include <linux/security.h>
11#include <linux/syscalls.h>
Eric Paris2a3edf82009-12-17 21:24:26 -050012#include <linux/types.h>
Eric Parisa1014f12009-12-17 21:24:26 -050013#include <linux/uaccess.h>
14
15#include <asm/ioctls.h>
Eric Paris11637e42009-12-17 21:24:25 -050016
17#include "fanotify.h"
18
Eric Paris2a3edf82009-12-17 21:24:26 -050019static struct kmem_cache *fanotify_mark_cache __read_mostly;
20
Eric Parisa1014f12009-12-17 21:24:26 -050021/*
22 * Get an fsnotify notification event if one exists and is small
23 * enough to fit in "count". Return an error pointer if the count
24 * is not large enough.
25 *
26 * Called with the group->notification_mutex held.
27 */
28static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
29 size_t count)
30{
31 BUG_ON(!mutex_is_locked(&group->notification_mutex));
32
33 pr_debug("%s: group=%p count=%zd\n", __func__, group, count);
34
35 if (fsnotify_notify_queue_is_empty(group))
36 return NULL;
37
38 if (FAN_EVENT_METADATA_LEN > count)
39 return ERR_PTR(-EINVAL);
40
41 /* held the notification_mutex the whole time, so this is the
42 * same event we peeked above */
43 return fsnotify_remove_notify_event(group);
44}
45
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050046static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
Eric Parisa1014f12009-12-17 21:24:26 -050047{
48 int client_fd;
49 struct dentry *dentry;
50 struct vfsmount *mnt;
51 struct file *new_file;
52
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050053 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
Eric Parisa1014f12009-12-17 21:24:26 -050054
55 client_fd = get_unused_fd();
56 if (client_fd < 0)
57 return client_fd;
58
59 if (event->data_type != FSNOTIFY_EVENT_PATH) {
60 WARN_ON(1);
61 put_unused_fd(client_fd);
62 return -EINVAL;
63 }
64
65 /*
66 * we need a new file handle for the userspace program so it can read even if it was
67 * originally opened O_WRONLY.
68 */
69 dentry = dget(event->path.dentry);
70 mnt = mntget(event->path.mnt);
71 /* it's possible this event was an overflow event. in that case dentry and mnt
72 * are NULL; That's fine, just don't call dentry open */
73 if (dentry && mnt)
74 new_file = dentry_open(dentry, mnt,
75 O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY,
76 current_cred());
77 else
78 new_file = ERR_PTR(-EOVERFLOW);
79 if (IS_ERR(new_file)) {
80 /*
81 * we still send an event even if we can't open the file. this
82 * can happen when say tasks are gone and we try to open their
83 * /proc files or we try to open a WRONLY file like in sysfs
84 * we just send the errno to userspace since there isn't much
85 * else we can do.
86 */
87 put_unused_fd(client_fd);
88 client_fd = PTR_ERR(new_file);
89 } else {
90 fd_install(client_fd, new_file);
91 }
92
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -050093 return client_fd;
Eric Parisa1014f12009-12-17 21:24:26 -050094}
95
96static ssize_t fill_event_metadata(struct fsnotify_group *group,
97 struct fanotify_event_metadata *metadata,
98 struct fsnotify_event *event)
99{
100 pr_debug("%s: group=%p metadata=%p event=%p\n", __func__,
101 group, metadata, event);
102
103 metadata->event_len = FAN_EVENT_METADATA_LEN;
104 metadata->vers = FANOTIFY_METADATA_VERSION;
105 metadata->mask = fanotify_outgoing_mask(event->mask);
Andreas Gruenbacher32c32632009-12-17 21:24:27 -0500106 metadata->pid = pid_vnr(event->tgid);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500107 metadata->fd = create_fd(group, event);
Eric Parisa1014f12009-12-17 21:24:26 -0500108
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500109 return metadata->fd;
Eric Parisa1014f12009-12-17 21:24:26 -0500110}
111
112static ssize_t copy_event_to_user(struct fsnotify_group *group,
113 struct fsnotify_event *event,
114 char __user *buf)
115{
116 struct fanotify_event_metadata fanotify_event_metadata;
117 int ret;
118
119 pr_debug("%s: group=%p event=%p\n", __func__, group, event);
120
121 ret = fill_event_metadata(group, &fanotify_event_metadata, event);
Andreas Gruenbacher22aa4252009-12-17 21:24:26 -0500122 if (ret < 0)
Eric Parisa1014f12009-12-17 21:24:26 -0500123 return ret;
124
125 if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN))
126 return -EFAULT;
127
128 return FAN_EVENT_METADATA_LEN;
129}
130
131/* intofiy userspace file descriptor functions */
132static unsigned int fanotify_poll(struct file *file, poll_table *wait)
133{
134 struct fsnotify_group *group = file->private_data;
135 int ret = 0;
136
137 poll_wait(file, &group->notification_waitq, wait);
138 mutex_lock(&group->notification_mutex);
139 if (!fsnotify_notify_queue_is_empty(group))
140 ret = POLLIN | POLLRDNORM;
141 mutex_unlock(&group->notification_mutex);
142
143 return ret;
144}
145
146static ssize_t fanotify_read(struct file *file, char __user *buf,
147 size_t count, loff_t *pos)
148{
149 struct fsnotify_group *group;
150 struct fsnotify_event *kevent;
151 char __user *start;
152 int ret;
153 DEFINE_WAIT(wait);
154
155 start = buf;
156 group = file->private_data;
157
158 pr_debug("%s: group=%p\n", __func__, group);
159
160 while (1) {
161 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
162
163 mutex_lock(&group->notification_mutex);
164 kevent = get_one_event(group, count);
165 mutex_unlock(&group->notification_mutex);
166
167 if (kevent) {
168 ret = PTR_ERR(kevent);
169 if (IS_ERR(kevent))
170 break;
171 ret = copy_event_to_user(group, kevent, buf);
172 fsnotify_put_event(kevent);
173 if (ret < 0)
174 break;
175 buf += ret;
176 count -= ret;
177 continue;
178 }
179
180 ret = -EAGAIN;
181 if (file->f_flags & O_NONBLOCK)
182 break;
183 ret = -EINTR;
184 if (signal_pending(current))
185 break;
186
187 if (start != buf)
188 break;
189
190 schedule();
191 }
192
193 finish_wait(&group->notification_waitq, &wait);
194 if (start != buf && ret != -EFAULT)
195 ret = buf - start;
196 return ret;
197}
198
Eric Paris52c923d2009-12-17 21:24:26 -0500199static int fanotify_release(struct inode *ignored, struct file *file)
200{
201 struct fsnotify_group *group = file->private_data;
202
203 pr_debug("%s: file=%p group=%p\n", __func__, file, group);
204
205 /* matches the fanotify_init->fsnotify_alloc_group */
206 fsnotify_put_group(group);
207
208 return 0;
209}
210
Eric Parisa1014f12009-12-17 21:24:26 -0500211static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
212{
213 struct fsnotify_group *group;
214 struct fsnotify_event_holder *holder;
215 void __user *p;
216 int ret = -ENOTTY;
217 size_t send_len = 0;
218
219 group = file->private_data;
220
221 p = (void __user *) arg;
222
223 switch (cmd) {
224 case FIONREAD:
225 mutex_lock(&group->notification_mutex);
226 list_for_each_entry(holder, &group->notification_list, event_list)
227 send_len += FAN_EVENT_METADATA_LEN;
228 mutex_unlock(&group->notification_mutex);
229 ret = put_user(send_len, (int __user *) p);
230 break;
231 }
232
233 return ret;
234}
235
Eric Paris52c923d2009-12-17 21:24:26 -0500236static const struct file_operations fanotify_fops = {
Eric Parisa1014f12009-12-17 21:24:26 -0500237 .poll = fanotify_poll,
238 .read = fanotify_read,
Eric Paris52c923d2009-12-17 21:24:26 -0500239 .fasync = NULL,
240 .release = fanotify_release,
Eric Parisa1014f12009-12-17 21:24:26 -0500241 .unlocked_ioctl = fanotify_ioctl,
242 .compat_ioctl = fanotify_ioctl,
Eric Paris52c923d2009-12-17 21:24:26 -0500243};
244
Eric Paris2a3edf82009-12-17 21:24:26 -0500245static void fanotify_free_mark(struct fsnotify_mark *fsn_mark)
246{
247 kmem_cache_free(fanotify_mark_cache, fsn_mark);
248}
249
250static int fanotify_find_path(int dfd, const char __user *filename,
251 struct path *path, unsigned int flags)
252{
253 int ret;
254
255 pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__,
256 dfd, filename, flags);
257
258 if (filename == NULL) {
259 struct file *file;
260 int fput_needed;
261
262 ret = -EBADF;
263 file = fget_light(dfd, &fput_needed);
264 if (!file)
265 goto out;
266
267 ret = -ENOTDIR;
268 if ((flags & FAN_MARK_ONLYDIR) &&
269 !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) {
270 fput_light(file, fput_needed);
271 goto out;
272 }
273
274 *path = file->f_path;
275 path_get(path);
276 fput_light(file, fput_needed);
277 } else {
278 unsigned int lookup_flags = 0;
279
280 if (!(flags & FAN_MARK_DONT_FOLLOW))
281 lookup_flags |= LOOKUP_FOLLOW;
282 if (flags & FAN_MARK_ONLYDIR)
283 lookup_flags |= LOOKUP_DIRECTORY;
284
285 ret = user_path_at(dfd, filename, lookup_flags, path);
286 if (ret)
287 goto out;
288 }
289
290 /* you can only watch an inode if you have read permissions on it */
291 ret = inode_permission(path->dentry->d_inode, MAY_READ);
292 if (ret)
293 path_put(path);
294out:
295 return ret;
296}
297
Eric Paris88826272009-12-17 21:24:28 -0500298static void fanotify_update_object_mask(struct fsnotify_group *group,
299 struct inode *inode,
300 struct vfsmount *mnt,
301 struct fsnotify_mark *fsn_mark,
302 unsigned int flags,
303 __u32 mask)
Eric Paris2a3edf82009-12-17 21:24:26 -0500304{
Eric Paris88826272009-12-17 21:24:28 -0500305 __u32 old_mask, new_mask;
Eric Paris2a3edf82009-12-17 21:24:26 -0500306
Eric Paris88826272009-12-17 21:24:28 -0500307 pr_debug("%s: group=%p inode=%p mnt=%p fsn_mark=%p flags=%x mask=%x\n",
308 __func__, group, inode, mnt, fsn_mark, flags, mask);
Eric Paris2a3edf82009-12-17 21:24:26 -0500309
310 spin_lock(&fsn_mark->lock);
Eric Paris88826272009-12-17 21:24:28 -0500311 old_mask = fsn_mark->mask;
312 if (flags & FAN_MARK_ADD)
313 fsn_mark->mask |= mask;
314 else if (flags & FAN_MARK_REMOVE)
315 fsn_mark->mask &= ~mask;
316 else
317 BUG();
Eric Paris2a3edf82009-12-17 21:24:26 -0500318 new_mask = fsn_mark->mask;
319 spin_unlock(&fsn_mark->lock);
320
321 if (!new_mask)
322 fsnotify_destroy_mark(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500323
Eric Paris88826272009-12-17 21:24:28 -0500324 /* we made changes to a mask, update the group mask and the object mask
325 * so things happen quickly. */
326 if (old_mask != new_mask) {
327 __u32 dropped, do_object, do_group;
328
329 /* more bits in old than in new? */
330 dropped = (old_mask & ~new_mask);
331 /* more bits in this fsn_mark than the group? */
332 do_group = (new_mask & ~group->mask);
333
334 if (inode) {
335 /* more bits in this fsn_mark than the object's mask? */
336 do_object = (new_mask & ~inode->i_fsnotify_mask);
337 /* update the object with this new fsn_mark */
338 if (dropped || do_object)
339 fsnotify_recalc_inode_mask(inode);
340 } else if (mnt) {
341 /* more bits in this fsn_mark than the object's mask? */
342 do_object = (new_mask & ~mnt->mnt_fsnotify_mask);
343 /* update the object with this new fsn_mark */
344 if (dropped || do_object)
345 fsnotify_recalc_vfsmount_mask(mnt);
346 } else {
347 BUG();
348 }
349
350 /* update the group mask with the new mask */
351 if (dropped || do_group)
352 fsnotify_recalc_group_mask(group);
353 }
354}
355
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500356static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, __u32 mask)
357{
358 __u32 oldmask;
359
360 spin_lock(&fsn_mark->lock);
361 oldmask = fsn_mark->mask;
362 fsn_mark->mask = oldmask & ~mask;
363 spin_unlock(&fsn_mark->lock);
364
365 if (!(oldmask & ~mask))
366 fsnotify_destroy_mark(fsn_mark);
367
368 return mask & oldmask;
369}
370
Eric Paris88826272009-12-17 21:24:28 -0500371static int fanotify_remove_mark(struct fsnotify_group *group, struct inode *inode,
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500372 struct vfsmount *mnt, __u32 mask)
Eric Paris88826272009-12-17 21:24:28 -0500373{
374 struct fsnotify_mark *fsn_mark = NULL;
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500375 __u32 removed;
Eric Paris88826272009-12-17 21:24:28 -0500376
377 BUG_ON(inode && mnt);
378 BUG_ON(!inode && !mnt);
379
380 if (inode)
381 fsn_mark = fsnotify_find_inode_mark(group, inode);
382 else if (mnt)
383 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
384 else
385 BUG();
386
387 if (!fsn_mark)
388 return -ENOENT;
389
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500390 removed = fanotify_mark_remove_from_mask(fsn_mark, mask);
Eric Paris5444e292009-12-17 21:24:27 -0500391 /* matches the fsnotify_find_inode_mark() */
Eric Paris2a3edf82009-12-17 21:24:26 -0500392 fsnotify_put_mark(fsn_mark);
393
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500394 if (removed & group->mask)
395 fsnotify_recalc_group_mask(group);
396 if (inode) {
397 if (removed & inode->i_fsnotify_mask)
398 fsnotify_recalc_inode_mask(inode);
399 } else if (mnt) {
400 if (removed & mnt->mnt_fsnotify_mask)
401 fsnotify_recalc_vfsmount_mask(mnt);
402 }
403
Eric Paris2a3edf82009-12-17 21:24:26 -0500404 return 0;
405}
406
Eric Paris88826272009-12-17 21:24:28 -0500407static struct fsnotify_mark *fanotify_add_vfsmount_mark(struct fsnotify_group *group,
408 struct vfsmount *mnt)
Eric Paris2a3edf82009-12-17 21:24:26 -0500409{
410 struct fsnotify_mark *fsn_mark;
Eric Paris2a3edf82009-12-17 21:24:26 -0500411
Eric Paris88826272009-12-17 21:24:28 -0500412 fsn_mark = fsnotify_find_vfsmount_mark(group, mnt);
413 if (!fsn_mark) {
414 struct fsnotify_mark *new_fsn_mark;
415 int ret;
416
417 fsn_mark = ERR_PTR(-ENOMEM);
418 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
419 if (!new_fsn_mark)
420 goto out;
421
422 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
423 ret = fsnotify_add_mark(new_fsn_mark, group, NULL, mnt, 0);
424 if (ret) {
425 fsn_mark = ERR_PTR(ret);
426 fanotify_free_mark(new_fsn_mark);
427 goto out;
428 }
429
430 fsn_mark = new_fsn_mark;
431 }
432out:
433 return fsn_mark;
434}
435
436static struct fsnotify_mark *fanotify_add_inode_mark(struct fsnotify_group *group,
437 struct inode *inode)
438{
439 struct fsnotify_mark *fsn_mark;
440
441 pr_debug("%s: group=%p inode=%p\n", __func__, group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500442
Eric Paris5444e292009-12-17 21:24:27 -0500443 fsn_mark = fsnotify_find_inode_mark(group, inode);
Eric Paris2a3edf82009-12-17 21:24:26 -0500444 if (!fsn_mark) {
445 struct fsnotify_mark *new_fsn_mark;
Eric Paris88826272009-12-17 21:24:28 -0500446 int ret;
Eric Paris2a3edf82009-12-17 21:24:26 -0500447
Eric Paris88826272009-12-17 21:24:28 -0500448 fsn_mark = ERR_PTR(-ENOMEM);
Eric Paris2a3edf82009-12-17 21:24:26 -0500449 new_fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL);
450 if (!new_fsn_mark)
451 goto out;
452
453 fsnotify_init_mark(new_fsn_mark, fanotify_free_mark);
Eric Paris5444e292009-12-17 21:24:27 -0500454 ret = fsnotify_add_mark(new_fsn_mark, group, inode, NULL, 0);
Eric Paris2a3edf82009-12-17 21:24:26 -0500455 if (ret) {
Eric Paris88826272009-12-17 21:24:28 -0500456 fsn_mark = ERR_PTR(ret);
Eric Paris2a3edf82009-12-17 21:24:26 -0500457 fanotify_free_mark(new_fsn_mark);
458 goto out;
459 }
460
461 fsn_mark = new_fsn_mark;
462 }
Eric Paris88826272009-12-17 21:24:28 -0500463out:
464 return fsn_mark;
465}
Eric Paris2a3edf82009-12-17 21:24:26 -0500466
Eric Paris88826272009-12-17 21:24:28 -0500467static int fanotify_add_mark(struct fsnotify_group *group, struct inode *inode,
468 struct vfsmount *mnt, unsigned int flags, __u32 mask)
469{
470 struct fsnotify_mark *fsn_mark;
Eric Paris2a3edf82009-12-17 21:24:26 -0500471
Eric Paris88826272009-12-17 21:24:28 -0500472 pr_debug("%s: group=%p inode=%p mnt=%p flags=%x mask=%x\n",
473 __func__, group, inode, mnt, flags, mask);
Eric Paris2a3edf82009-12-17 21:24:26 -0500474
Eric Paris88826272009-12-17 21:24:28 -0500475 BUG_ON(inode && mnt);
476 BUG_ON(!inode && !mnt);
Eric Paris2a3edf82009-12-17 21:24:26 -0500477
Eric Paris88826272009-12-17 21:24:28 -0500478 if (inode)
479 fsn_mark = fanotify_add_inode_mark(group, inode);
480 else if (mnt)
481 fsn_mark = fanotify_add_vfsmount_mark(group, mnt);
482 else
483 BUG();
Eric Paris2a3edf82009-12-17 21:24:26 -0500484
Eric Paris88826272009-12-17 21:24:28 -0500485 if (IS_ERR(fsn_mark))
486 goto out;
487
488 fanotify_update_object_mask(group, inode, mnt, fsn_mark, flags, mask);
Eric Paris2a3edf82009-12-17 21:24:26 -0500489
490 /* match the init or the find.... */
491 fsnotify_put_mark(fsn_mark);
492out:
Eric Paris88826272009-12-17 21:24:28 -0500493 return PTR_ERR(fsn_mark);
Eric Paris2a3edf82009-12-17 21:24:26 -0500494}
495
Eric Paris2a3edf82009-12-17 21:24:26 -0500496static bool fanotify_mark_validate_input(int flags,
497 __u32 mask)
498{
499 pr_debug("%s: flags=%x mask=%x\n", __func__, flags, mask);
500
501 /* are flags valid of this operation? */
502 if (!fanotify_mark_flags_valid(flags))
503 return false;
504 /* is the mask valid? */
505 if (!fanotify_mask_valid(mask))
506 return false;
507 return true;
508}
509
Eric Paris52c923d2009-12-17 21:24:26 -0500510/* fanotify syscalls */
Eric Paris11637e42009-12-17 21:24:25 -0500511SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags,
512 unsigned int, priority)
513{
Eric Paris52c923d2009-12-17 21:24:26 -0500514 struct fsnotify_group *group;
515 int f_flags, fd;
516
517 pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n",
518 __func__, flags, event_f_flags, priority);
519
520 if (event_f_flags)
521 return -EINVAL;
522 if (priority)
523 return -EINVAL;
524
525 if (!capable(CAP_SYS_ADMIN))
526 return -EACCES;
527
528 if (flags & ~FAN_ALL_INIT_FLAGS)
529 return -EINVAL;
530
531 f_flags = (O_RDONLY | FMODE_NONOTIFY);
532 if (flags & FAN_CLOEXEC)
533 f_flags |= O_CLOEXEC;
534 if (flags & FAN_NONBLOCK)
535 f_flags |= O_NONBLOCK;
536
537 /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */
538 group = fsnotify_alloc_group(&fanotify_fsnotify_ops);
539 if (IS_ERR(group))
540 return PTR_ERR(group);
541
542 fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags);
543 if (fd < 0)
544 goto out_put_group;
545
546 return fd;
547
548out_put_group:
549 fsnotify_put_group(group);
550 return fd;
Eric Paris11637e42009-12-17 21:24:25 -0500551}
Eric Parisbbaa4162009-12-17 21:24:26 -0500552
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500553SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags,
554 __u64 mask, int dfd,
555 const char __user * pathname)
Eric Parisbbaa4162009-12-17 21:24:26 -0500556{
Eric Paris2a3edf82009-12-17 21:24:26 -0500557 struct inode *inode;
558 struct fsnotify_group *group;
559 struct file *filp;
560 struct path path;
561 int ret, fput_needed;
562
563 pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n",
564 __func__, fanotify_fd, flags, dfd, pathname, mask);
565
566 /* we only use the lower 32 bits as of right now. */
567 if (mask & ((__u64)0xffffffff << 32))
568 return -EINVAL;
569
570 if (!fanotify_mark_validate_input(flags, mask))
571 return -EINVAL;
572
573 filp = fget_light(fanotify_fd, &fput_needed);
574 if (unlikely(!filp))
575 return -EBADF;
576
577 /* verify that this is indeed an fanotify instance */
578 ret = -EINVAL;
579 if (unlikely(filp->f_op != &fanotify_fops))
580 goto fput_and_out;
581
582 ret = fanotify_find_path(dfd, pathname, &path, flags);
583 if (ret)
584 goto fput_and_out;
585
586 /* inode held in place by reference to path; group by fget on fd */
587 inode = path.dentry->d_inode;
588 group = filp->private_data;
589
590 /* create/update an inode mark */
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500591 switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE)) {
592 case FAN_MARK_ADD:
593 ret = fanotify_add_mark(group, inode, NULL, flags, mask);
594 break;
595 case FAN_MARK_REMOVE:
Andreas Gruenbacher088b09b2009-12-17 21:24:28 -0500596 ret = fanotify_remove_mark(group, inode, NULL, mask);
Andreas Gruenbacherc6223f42009-12-17 21:24:28 -0500597 break;
598 default:
599 ret = -EINVAL;
600 }
Eric Paris2a3edf82009-12-17 21:24:26 -0500601
602 path_put(&path);
603fput_and_out:
604 fput_light(filp, fput_needed);
605 return ret;
Eric Parisbbaa4162009-12-17 21:24:26 -0500606}
Eric Paris2a3edf82009-12-17 21:24:26 -0500607
Heiko Carstens9bbfc962009-12-17 21:24:26 -0500608#ifdef CONFIG_HAVE_SYSCALL_WRAPPERS
609asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask,
610 long dfd, long pathname)
611{
612 return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags,
613 mask, (int) dfd,
614 (const char __user *) pathname);
615}
616SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark);
617#endif
618
Eric Paris2a3edf82009-12-17 21:24:26 -0500619/*
620 * fanotify_user_setup - Our initialization function. Note that we cannnot return
621 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
622 * must result in panic().
623 */
624static int __init fanotify_user_setup(void)
625{
626 fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC);
627
628 return 0;
629}
630device_initcall(fanotify_user_setup);