Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 1 | #include <linux/fanotify.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 2 | #include <linux/fcntl.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 3 | #include <linux/file.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 4 | #include <linux/fs.h> |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 5 | #include <linux/anon_inodes.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 6 | #include <linux/fsnotify_backend.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 7 | #include <linux/init.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 8 | #include <linux/mount.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 9 | #include <linux/namei.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 10 | #include <linux/poll.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 11 | #include <linux/security.h> |
| 12 | #include <linux/syscalls.h> |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 13 | #include <linux/types.h> |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
| 15 | |
| 16 | #include <asm/ioctls.h> |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 17 | |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 18 | extern const struct fsnotify_ops fanotify_fsnotify_ops; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 19 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 20 | static struct kmem_cache *fanotify_mark_cache __read_mostly; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 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 | }; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 28 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 54 | static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 55 | { |
| 56 | int client_fd; |
| 57 | struct dentry *dentry; |
| 58 | struct vfsmount *mnt; |
| 59 | struct file *new_file; |
| 60 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 61 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 101 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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; |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 113 | metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS; |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 114 | metadata->pid = pid_vnr(event->tgid); |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 115 | metadata->fd = create_fd(group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 116 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 117 | return metadata->fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 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 | { |
Eric Paris | 8860f06 | 2009-12-23 00:10:25 -0500 | [diff] [blame] | 234 | return; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 235 | } |
| 236 | #endif |
| 237 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 243 | int fd, ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 244 | |
| 245 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 246 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 247 | fd = fill_event_metadata(group, &fanotify_event_metadata, event); |
| 248 | if (fd < 0) |
| 249 | return fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 250 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 251 | ret = prepare_for_access_response(group, event, fd); |
| 252 | if (ret) |
| 253 | goto out_close_fd; |
| 254 | |
| 255 | ret = -EFAULT; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 256 | if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN)) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 257 | goto out_kill_access_response; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 258 | |
| 259 | return FAN_EVENT_METADATA_LEN; |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 260 | |
| 261 | out_kill_access_response: |
| 262 | remove_access_response(group, event, fd); |
| 263 | out_close_fd: |
| 264 | sys_close(fd); |
| 265 | return ret; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 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 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 400 | static const struct file_operations fanotify_fops = { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 401 | .poll = fanotify_poll, |
| 402 | .read = fanotify_read, |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 403 | .write = fanotify_write, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 404 | .fasync = NULL, |
| 405 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 406 | .unlocked_ioctl = fanotify_ioctl, |
| 407 | .compat_ioctl = fanotify_ioctl, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 408 | }; |
| 409 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 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 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 463 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 464 | __u32 mask, |
| 465 | unsigned int flags) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 466 | { |
| 467 | __u32 oldmask; |
| 468 | |
| 469 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 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 | } |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 477 | spin_unlock(&fsn_mark->lock); |
| 478 | |
| 479 | if (!(oldmask & ~mask)) |
| 480 | fsnotify_destroy_mark(fsn_mark); |
| 481 | |
| 482 | return mask & oldmask; |
| 483 | } |
| 484 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 485 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 486 | struct vfsmount *mnt, __u32 mask, |
| 487 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 488 | { |
| 489 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 490 | __u32 removed; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 491 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 492 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 493 | if (!fsn_mark) |
| 494 | return -ENOENT; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 495 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 496 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 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); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 502 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 507 | struct inode *inode, __u32 mask, |
| 508 | unsigned int flags) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 509 | { |
| 510 | struct fsnotify_mark *fsn_mark = NULL; |
| 511 | __u32 removed; |
| 512 | |
| 513 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 514 | if (!fsn_mark) |
| 515 | return -ENOENT; |
| 516 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 517 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 518 | /* matches the fsnotify_find_inode_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 519 | fsnotify_put_mark(fsn_mark); |
| 520 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 521 | if (removed & group->mask) |
| 522 | fsnotify_recalc_group_mask(group); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 523 | if (removed & inode->i_fsnotify_mask) |
| 524 | fsnotify_recalc_inode_mask(inode); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 525 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 529 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 530 | __u32 mask, |
| 531 | unsigned int flags) |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 532 | { |
| 533 | __u32 oldmask; |
| 534 | |
| 535 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 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)); |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 542 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 543 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 544 | } |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 545 | spin_unlock(&fsn_mark->lock); |
| 546 | |
| 547 | return mask & ~oldmask; |
| 548 | } |
| 549 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 550 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 551 | struct vfsmount *mnt, __u32 mask, |
| 552 | unsigned int flags) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 553 | { |
| 554 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 555 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 556 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 557 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 558 | if (!fsn_mark) { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 559 | int ret; |
| 560 | |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 561 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 562 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 563 | return -ENOMEM; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 564 | |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 565 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 566 | ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 567 | if (ret) { |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 568 | fanotify_free_mark(fsn_mark); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 569 | return ret; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 570 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 571 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 572 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 573 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 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 | } |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 580 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 581 | } |
| 582 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 583 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 584 | struct inode *inode, __u32 mask, |
| 585 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 586 | { |
| 587 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 588 | __u32 added; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 589 | |
| 590 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 591 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 592 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 593 | if (!fsn_mark) { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 594 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 595 | |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 596 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 597 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 598 | return -ENOMEM; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 599 | |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 600 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 601 | ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 602 | if (ret) { |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 603 | fanotify_free_mark(fsn_mark); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 604 | return ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 605 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 606 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 607 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 608 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | 912ee39 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 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 | } |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 615 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 616 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 617 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 618 | /* fanotify syscalls */ |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame^] | 619 | SYSCALL_DEFINE2(fanotify_init, unsigned int, flags, unsigned int, event_f_flags) |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 620 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 621 | struct fsnotify_group *group; |
| 622 | int f_flags, fd; |
| 623 | |
Eric Paris | 08ae893 | 2010-05-27 09:41:40 -0400 | [diff] [blame^] | 624 | pr_debug("%s: flags=%d event_f_flags=%d\n", |
| 625 | __func__, flags, event_f_flags); |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 626 | |
| 627 | if (event_f_flags) |
| 628 | return -EINVAL; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 629 | |
| 630 | if (!capable(CAP_SYS_ADMIN)) |
| 631 | return -EACCES; |
| 632 | |
| 633 | if (flags & ~FAN_ALL_INIT_FLAGS) |
| 634 | return -EINVAL; |
| 635 | |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 636 | f_flags = O_RDWR | FMODE_NONOTIFY; |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 637 | if (flags & FAN_CLOEXEC) |
| 638 | f_flags |= O_CLOEXEC; |
| 639 | if (flags & FAN_NONBLOCK) |
| 640 | f_flags |= O_NONBLOCK; |
| 641 | |
| 642 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 643 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
| 644 | if (IS_ERR(group)) |
| 645 | return PTR_ERR(group); |
| 646 | |
Eric Paris | 9e66e42 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 647 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 648 | mutex_init(&group->fanotify_data.access_mutex); |
| 649 | init_waitqueue_head(&group->fanotify_data.access_waitq); |
| 650 | INIT_LIST_HEAD(&group->fanotify_data.access_list); |
| 651 | #endif |
Eric Paris | cb2d429 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 652 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 653 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 654 | if (fd < 0) |
| 655 | goto out_put_group; |
| 656 | |
| 657 | return fd; |
| 658 | |
| 659 | out_put_group: |
| 660 | fsnotify_put_group(group); |
| 661 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 662 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 663 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 664 | SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags, |
| 665 | __u64 mask, int dfd, |
| 666 | const char __user * pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 667 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 668 | struct inode *inode = NULL; |
| 669 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 670 | struct fsnotify_group *group; |
| 671 | struct file *filp; |
| 672 | struct path path; |
| 673 | int ret, fput_needed; |
| 674 | |
| 675 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 676 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 677 | |
| 678 | /* we only use the lower 32 bits as of right now. */ |
| 679 | if (mask & ((__u64)0xffffffff << 32)) |
| 680 | return -EINVAL; |
| 681 | |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 682 | if (flags & ~FAN_ALL_MARK_FLAGS) |
| 683 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 684 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 685 | case FAN_MARK_ADD: |
| 686 | case FAN_MARK_REMOVE: |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 687 | case FAN_MARK_FLUSH: |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 688 | break; |
| 689 | default: |
| 690 | return -EINVAL; |
| 691 | } |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 692 | #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS |
| 693 | if (mask & ~(FAN_ALL_EVENTS | FAN_ALL_PERM_EVENTS | FAN_EVENT_ON_CHILD)) |
| 694 | #else |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 695 | if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD)) |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 696 | #endif |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 697 | return -EINVAL; |
| 698 | |
| 699 | filp = fget_light(fanotify_fd, &fput_needed); |
| 700 | if (unlikely(!filp)) |
| 701 | return -EBADF; |
| 702 | |
| 703 | /* verify that this is indeed an fanotify instance */ |
| 704 | ret = -EINVAL; |
| 705 | if (unlikely(filp->f_op != &fanotify_fops)) |
| 706 | goto fput_and_out; |
| 707 | |
| 708 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 709 | if (ret) |
| 710 | goto fput_and_out; |
| 711 | |
| 712 | /* inode held in place by reference to path; group by fget on fd */ |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 713 | if (!(flags & FAN_MARK_MOUNT)) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 714 | inode = path.dentry->d_inode; |
| 715 | else |
| 716 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 717 | group = filp->private_data; |
| 718 | |
| 719 | /* create/update an inode mark */ |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 720 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 721 | case FAN_MARK_ADD: |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 722 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 723 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 724 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 725 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 726 | break; |
| 727 | case FAN_MARK_REMOVE: |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 728 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 729 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 730 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 731 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 732 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 733 | case FAN_MARK_FLUSH: |
| 734 | if (flags & FAN_MARK_MOUNT) |
| 735 | fsnotify_clear_vfsmount_marks_by_group(group); |
| 736 | else |
| 737 | fsnotify_clear_inode_marks_by_group(group); |
| 738 | fsnotify_recalc_group_mask(group); |
| 739 | break; |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 740 | default: |
| 741 | ret = -EINVAL; |
| 742 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 743 | |
| 744 | path_put(&path); |
| 745 | fput_and_out: |
| 746 | fput_light(filp, fput_needed); |
| 747 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 748 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 749 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 750 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 751 | asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask, |
| 752 | long dfd, long pathname) |
| 753 | { |
| 754 | return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags, |
| 755 | mask, (int) dfd, |
| 756 | (const char __user *) pathname); |
| 757 | } |
| 758 | SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark); |
| 759 | #endif |
| 760 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 761 | /* |
| 762 | * fanotify_user_setup - Our initialization function. Note that we cannnot return |
| 763 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 764 | * must result in panic(). |
| 765 | */ |
| 766 | static int __init fanotify_user_setup(void) |
| 767 | { |
| 768 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); |
Eric Paris | b2d8790 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 769 | fanotify_response_event_cache = KMEM_CACHE(fanotify_response_event, |
| 770 | SLAB_PANIC); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | device_initcall(fanotify_user_setup); |