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; |
| 21 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 22 | /* |
| 23 | * Get an fsnotify notification event if one exists and is small |
| 24 | * enough to fit in "count". Return an error pointer if the count |
| 25 | * is not large enough. |
| 26 | * |
| 27 | * Called with the group->notification_mutex held. |
| 28 | */ |
| 29 | static struct fsnotify_event *get_one_event(struct fsnotify_group *group, |
| 30 | size_t count) |
| 31 | { |
| 32 | BUG_ON(!mutex_is_locked(&group->notification_mutex)); |
| 33 | |
| 34 | pr_debug("%s: group=%p count=%zd\n", __func__, group, count); |
| 35 | |
| 36 | if (fsnotify_notify_queue_is_empty(group)) |
| 37 | return NULL; |
| 38 | |
| 39 | if (FAN_EVENT_METADATA_LEN > count) |
| 40 | return ERR_PTR(-EINVAL); |
| 41 | |
| 42 | /* held the notification_mutex the whole time, so this is the |
| 43 | * same event we peeked above */ |
| 44 | return fsnotify_remove_notify_event(group); |
| 45 | } |
| 46 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 47 | static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 48 | { |
| 49 | int client_fd; |
| 50 | struct dentry *dentry; |
| 51 | struct vfsmount *mnt; |
| 52 | struct file *new_file; |
| 53 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 54 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 55 | |
| 56 | client_fd = get_unused_fd(); |
| 57 | if (client_fd < 0) |
| 58 | return client_fd; |
| 59 | |
| 60 | if (event->data_type != FSNOTIFY_EVENT_PATH) { |
| 61 | WARN_ON(1); |
| 62 | put_unused_fd(client_fd); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * we need a new file handle for the userspace program so it can read even if it was |
| 68 | * originally opened O_WRONLY. |
| 69 | */ |
| 70 | dentry = dget(event->path.dentry); |
| 71 | mnt = mntget(event->path.mnt); |
| 72 | /* it's possible this event was an overflow event. in that case dentry and mnt |
| 73 | * are NULL; That's fine, just don't call dentry open */ |
| 74 | if (dentry && mnt) |
| 75 | new_file = dentry_open(dentry, mnt, |
| 76 | O_RDONLY | O_LARGEFILE | FMODE_NONOTIFY, |
| 77 | current_cred()); |
| 78 | else |
| 79 | new_file = ERR_PTR(-EOVERFLOW); |
| 80 | if (IS_ERR(new_file)) { |
| 81 | /* |
| 82 | * we still send an event even if we can't open the file. this |
| 83 | * can happen when say tasks are gone and we try to open their |
| 84 | * /proc files or we try to open a WRONLY file like in sysfs |
| 85 | * we just send the errno to userspace since there isn't much |
| 86 | * else we can do. |
| 87 | */ |
| 88 | put_unused_fd(client_fd); |
| 89 | client_fd = PTR_ERR(new_file); |
| 90 | } else { |
| 91 | fd_install(client_fd, new_file); |
| 92 | } |
| 93 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 94 | return client_fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static ssize_t fill_event_metadata(struct fsnotify_group *group, |
| 98 | struct fanotify_event_metadata *metadata, |
| 99 | struct fsnotify_event *event) |
| 100 | { |
| 101 | pr_debug("%s: group=%p metadata=%p event=%p\n", __func__, |
| 102 | group, metadata, event); |
| 103 | |
| 104 | metadata->event_len = FAN_EVENT_METADATA_LEN; |
| 105 | metadata->vers = FANOTIFY_METADATA_VERSION; |
Andreas Gruenbacher | 33d3dff | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 106 | metadata->mask = event->mask & FAN_ALL_OUTGOING_EVENTS; |
Andreas Gruenbacher | 32c3263 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 107 | metadata->pid = pid_vnr(event->tgid); |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 108 | metadata->fd = create_fd(group, event); |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 109 | |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 110 | return metadata->fd; |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static ssize_t copy_event_to_user(struct fsnotify_group *group, |
| 114 | struct fsnotify_event *event, |
| 115 | char __user *buf) |
| 116 | { |
| 117 | struct fanotify_event_metadata fanotify_event_metadata; |
| 118 | int ret; |
| 119 | |
| 120 | pr_debug("%s: group=%p event=%p\n", __func__, group, event); |
| 121 | |
| 122 | ret = fill_event_metadata(group, &fanotify_event_metadata, event); |
Andreas Gruenbacher | 22aa425 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 123 | if (ret < 0) |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 124 | return ret; |
| 125 | |
| 126 | if (copy_to_user(buf, &fanotify_event_metadata, FAN_EVENT_METADATA_LEN)) |
| 127 | return -EFAULT; |
| 128 | |
| 129 | return FAN_EVENT_METADATA_LEN; |
| 130 | } |
| 131 | |
| 132 | /* intofiy userspace file descriptor functions */ |
| 133 | static unsigned int fanotify_poll(struct file *file, poll_table *wait) |
| 134 | { |
| 135 | struct fsnotify_group *group = file->private_data; |
| 136 | int ret = 0; |
| 137 | |
| 138 | poll_wait(file, &group->notification_waitq, wait); |
| 139 | mutex_lock(&group->notification_mutex); |
| 140 | if (!fsnotify_notify_queue_is_empty(group)) |
| 141 | ret = POLLIN | POLLRDNORM; |
| 142 | mutex_unlock(&group->notification_mutex); |
| 143 | |
| 144 | return ret; |
| 145 | } |
| 146 | |
| 147 | static ssize_t fanotify_read(struct file *file, char __user *buf, |
| 148 | size_t count, loff_t *pos) |
| 149 | { |
| 150 | struct fsnotify_group *group; |
| 151 | struct fsnotify_event *kevent; |
| 152 | char __user *start; |
| 153 | int ret; |
| 154 | DEFINE_WAIT(wait); |
| 155 | |
| 156 | start = buf; |
| 157 | group = file->private_data; |
| 158 | |
| 159 | pr_debug("%s: group=%p\n", __func__, group); |
| 160 | |
| 161 | while (1) { |
| 162 | prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE); |
| 163 | |
| 164 | mutex_lock(&group->notification_mutex); |
| 165 | kevent = get_one_event(group, count); |
| 166 | mutex_unlock(&group->notification_mutex); |
| 167 | |
| 168 | if (kevent) { |
| 169 | ret = PTR_ERR(kevent); |
| 170 | if (IS_ERR(kevent)) |
| 171 | break; |
| 172 | ret = copy_event_to_user(group, kevent, buf); |
| 173 | fsnotify_put_event(kevent); |
| 174 | if (ret < 0) |
| 175 | break; |
| 176 | buf += ret; |
| 177 | count -= ret; |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | ret = -EAGAIN; |
| 182 | if (file->f_flags & O_NONBLOCK) |
| 183 | break; |
| 184 | ret = -EINTR; |
| 185 | if (signal_pending(current)) |
| 186 | break; |
| 187 | |
| 188 | if (start != buf) |
| 189 | break; |
| 190 | |
| 191 | schedule(); |
| 192 | } |
| 193 | |
| 194 | finish_wait(&group->notification_waitq, &wait); |
| 195 | if (start != buf && ret != -EFAULT) |
| 196 | ret = buf - start; |
| 197 | return ret; |
| 198 | } |
| 199 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 200 | static int fanotify_release(struct inode *ignored, struct file *file) |
| 201 | { |
| 202 | struct fsnotify_group *group = file->private_data; |
| 203 | |
| 204 | pr_debug("%s: file=%p group=%p\n", __func__, file, group); |
| 205 | |
| 206 | /* matches the fanotify_init->fsnotify_alloc_group */ |
| 207 | fsnotify_put_group(group); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 212 | static long fanotify_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 213 | { |
| 214 | struct fsnotify_group *group; |
| 215 | struct fsnotify_event_holder *holder; |
| 216 | void __user *p; |
| 217 | int ret = -ENOTTY; |
| 218 | size_t send_len = 0; |
| 219 | |
| 220 | group = file->private_data; |
| 221 | |
| 222 | p = (void __user *) arg; |
| 223 | |
| 224 | switch (cmd) { |
| 225 | case FIONREAD: |
| 226 | mutex_lock(&group->notification_mutex); |
| 227 | list_for_each_entry(holder, &group->notification_list, event_list) |
| 228 | send_len += FAN_EVENT_METADATA_LEN; |
| 229 | mutex_unlock(&group->notification_mutex); |
| 230 | ret = put_user(send_len, (int __user *) p); |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 237 | static const struct file_operations fanotify_fops = { |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 238 | .poll = fanotify_poll, |
| 239 | .read = fanotify_read, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 240 | .fasync = NULL, |
| 241 | .release = fanotify_release, |
Eric Paris | a1014f1 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 242 | .unlocked_ioctl = fanotify_ioctl, |
| 243 | .compat_ioctl = fanotify_ioctl, |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 244 | }; |
| 245 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 246 | static void fanotify_free_mark(struct fsnotify_mark *fsn_mark) |
| 247 | { |
| 248 | kmem_cache_free(fanotify_mark_cache, fsn_mark); |
| 249 | } |
| 250 | |
| 251 | static int fanotify_find_path(int dfd, const char __user *filename, |
| 252 | struct path *path, unsigned int flags) |
| 253 | { |
| 254 | int ret; |
| 255 | |
| 256 | pr_debug("%s: dfd=%d filename=%p flags=%x\n", __func__, |
| 257 | dfd, filename, flags); |
| 258 | |
| 259 | if (filename == NULL) { |
| 260 | struct file *file; |
| 261 | int fput_needed; |
| 262 | |
| 263 | ret = -EBADF; |
| 264 | file = fget_light(dfd, &fput_needed); |
| 265 | if (!file) |
| 266 | goto out; |
| 267 | |
| 268 | ret = -ENOTDIR; |
| 269 | if ((flags & FAN_MARK_ONLYDIR) && |
| 270 | !(S_ISDIR(file->f_path.dentry->d_inode->i_mode))) { |
| 271 | fput_light(file, fput_needed); |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | *path = file->f_path; |
| 276 | path_get(path); |
| 277 | fput_light(file, fput_needed); |
| 278 | } else { |
| 279 | unsigned int lookup_flags = 0; |
| 280 | |
| 281 | if (!(flags & FAN_MARK_DONT_FOLLOW)) |
| 282 | lookup_flags |= LOOKUP_FOLLOW; |
| 283 | if (flags & FAN_MARK_ONLYDIR) |
| 284 | lookup_flags |= LOOKUP_DIRECTORY; |
| 285 | |
| 286 | ret = user_path_at(dfd, filename, lookup_flags, path); |
| 287 | if (ret) |
| 288 | goto out; |
| 289 | } |
| 290 | |
| 291 | /* you can only watch an inode if you have read permissions on it */ |
| 292 | ret = inode_permission(path->dentry->d_inode, MAY_READ); |
| 293 | if (ret) |
| 294 | path_put(path); |
| 295 | out: |
| 296 | return ret; |
| 297 | } |
| 298 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 299 | static __u32 fanotify_mark_remove_from_mask(struct fsnotify_mark *fsn_mark, |
| 300 | __u32 mask, |
| 301 | unsigned int flags) |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 302 | { |
| 303 | __u32 oldmask; |
| 304 | |
| 305 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 306 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 307 | oldmask = fsn_mark->mask; |
| 308 | fsnotify_set_mark_mask_locked(fsn_mark, (oldmask & ~mask)); |
| 309 | } else { |
| 310 | oldmask = fsn_mark->ignored_mask; |
| 311 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask & ~mask)); |
| 312 | } |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 313 | spin_unlock(&fsn_mark->lock); |
| 314 | |
| 315 | if (!(oldmask & ~mask)) |
| 316 | fsnotify_destroy_mark(fsn_mark); |
| 317 | |
| 318 | return mask & oldmask; |
| 319 | } |
| 320 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 321 | static int fanotify_remove_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 322 | struct vfsmount *mnt, __u32 mask, |
| 323 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 324 | { |
| 325 | struct fsnotify_mark *fsn_mark = NULL; |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 326 | __u32 removed; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 327 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 328 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 329 | if (!fsn_mark) |
| 330 | return -ENOENT; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 331 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 332 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 333 | fsnotify_put_mark(fsn_mark); |
| 334 | if (removed & group->mask) |
| 335 | fsnotify_recalc_group_mask(group); |
| 336 | if (removed & mnt->mnt_fsnotify_mask) |
| 337 | fsnotify_recalc_vfsmount_mask(mnt); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 338 | |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | static int fanotify_remove_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 343 | struct inode *inode, __u32 mask, |
| 344 | unsigned int flags) |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 345 | { |
| 346 | struct fsnotify_mark *fsn_mark = NULL; |
| 347 | __u32 removed; |
| 348 | |
| 349 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 350 | if (!fsn_mark) |
| 351 | return -ENOENT; |
| 352 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 353 | removed = fanotify_mark_remove_from_mask(fsn_mark, mask, flags); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 354 | /* matches the fsnotify_find_inode_mark() */ |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 355 | fsnotify_put_mark(fsn_mark); |
| 356 | |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 357 | if (removed & group->mask) |
| 358 | fsnotify_recalc_group_mask(group); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 359 | if (removed & inode->i_fsnotify_mask) |
| 360 | fsnotify_recalc_inode_mask(inode); |
Andreas Gruenbacher | 088b09b | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 361 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 365 | static __u32 fanotify_mark_add_to_mask(struct fsnotify_mark *fsn_mark, |
| 366 | __u32 mask, |
| 367 | unsigned int flags) |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 368 | { |
| 369 | __u32 oldmask; |
| 370 | |
| 371 | spin_lock(&fsn_mark->lock); |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 372 | if (!(flags & FAN_MARK_IGNORED_MASK)) { |
| 373 | oldmask = fsn_mark->mask; |
| 374 | fsnotify_set_mark_mask_locked(fsn_mark, (oldmask | mask)); |
| 375 | } else { |
| 376 | oldmask = fsn_mark->ignored_mask; |
| 377 | fsnotify_set_mark_ignored_mask_locked(fsn_mark, (oldmask | mask)); |
Eric Paris | c9778a9 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 378 | if (flags & FAN_MARK_IGNORED_SURV_MODIFY) |
| 379 | fsn_mark->flags |= FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY; |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 380 | } |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 381 | spin_unlock(&fsn_mark->lock); |
| 382 | |
| 383 | return mask & ~oldmask; |
| 384 | } |
| 385 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 386 | static int fanotify_add_vfsmount_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 387 | struct vfsmount *mnt, __u32 mask, |
| 388 | unsigned int flags) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 389 | { |
| 390 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 391 | __u32 added; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 392 | |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 393 | fsn_mark = fsnotify_find_vfsmount_mark(group, mnt); |
| 394 | if (!fsn_mark) { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 395 | int ret; |
| 396 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 397 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 398 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 399 | return -ENOMEM; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 400 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 401 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 402 | ret = fsnotify_add_mark(fsn_mark, group, NULL, mnt, 0); |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 403 | if (ret) { |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 404 | fanotify_free_mark(fsn_mark); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 405 | return ret; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 406 | } |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 407 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 408 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 409 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 410 | if (added) { |
| 411 | if (added & ~group->mask) |
| 412 | fsnotify_recalc_group_mask(group); |
| 413 | if (added & ~mnt->mnt_fsnotify_mask) |
| 414 | fsnotify_recalc_vfsmount_mask(mnt); |
| 415 | } |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 416 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 417 | } |
| 418 | |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 419 | static int fanotify_add_inode_mark(struct fsnotify_group *group, |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 420 | struct inode *inode, __u32 mask, |
| 421 | unsigned int flags) |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 422 | { |
| 423 | struct fsnotify_mark *fsn_mark; |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 424 | __u32 added; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 425 | |
| 426 | pr_debug("%s: group=%p inode=%p\n", __func__, group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 427 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 428 | fsn_mark = fsnotify_find_inode_mark(group, inode); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 429 | if (!fsn_mark) { |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 430 | int ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 431 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 432 | fsn_mark = kmem_cache_alloc(fanotify_mark_cache, GFP_KERNEL); |
| 433 | if (!fsn_mark) |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 434 | return -ENOMEM; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 435 | |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 436 | fsnotify_init_mark(fsn_mark, fanotify_free_mark); |
| 437 | ret = fsnotify_add_mark(fsn_mark, group, inode, NULL, 0); |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 438 | if (ret) { |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 439 | fanotify_free_mark(fsn_mark); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 440 | return ret; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 441 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 442 | } |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 443 | added = fanotify_mark_add_to_mask(fsn_mark, mask, flags); |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 444 | fsnotify_put_mark(fsn_mark); |
Andreas Gruenbacher | 912ee3946 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 445 | if (added) { |
| 446 | if (added & ~group->mask) |
| 447 | fsnotify_recalc_group_mask(group); |
| 448 | if (added & ~inode->i_fsnotify_mask) |
| 449 | fsnotify_recalc_inode_mask(inode); |
| 450 | } |
Andreas Gruenbacher | 52202df | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 451 | return 0; |
Eric Paris | 8882627 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 452 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 453 | |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 454 | /* fanotify syscalls */ |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 455 | SYSCALL_DEFINE3(fanotify_init, unsigned int, flags, unsigned int, event_f_flags, |
| 456 | unsigned int, priority) |
| 457 | { |
Eric Paris | 52c923d | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 458 | struct fsnotify_group *group; |
| 459 | int f_flags, fd; |
| 460 | |
| 461 | pr_debug("%s: flags=%d event_f_flags=%d priority=%d\n", |
| 462 | __func__, flags, event_f_flags, priority); |
| 463 | |
| 464 | if (event_f_flags) |
| 465 | return -EINVAL; |
| 466 | if (priority) |
| 467 | return -EINVAL; |
| 468 | |
| 469 | if (!capable(CAP_SYS_ADMIN)) |
| 470 | return -EACCES; |
| 471 | |
| 472 | if (flags & ~FAN_ALL_INIT_FLAGS) |
| 473 | return -EINVAL; |
| 474 | |
| 475 | f_flags = (O_RDONLY | FMODE_NONOTIFY); |
| 476 | if (flags & FAN_CLOEXEC) |
| 477 | f_flags |= O_CLOEXEC; |
| 478 | if (flags & FAN_NONBLOCK) |
| 479 | f_flags |= O_NONBLOCK; |
| 480 | |
| 481 | /* fsnotify_alloc_group takes a ref. Dropped in fanotify_release */ |
| 482 | group = fsnotify_alloc_group(&fanotify_fsnotify_ops); |
| 483 | if (IS_ERR(group)) |
| 484 | return PTR_ERR(group); |
| 485 | |
| 486 | fd = anon_inode_getfd("[fanotify]", &fanotify_fops, group, f_flags); |
| 487 | if (fd < 0) |
| 488 | goto out_put_group; |
| 489 | |
| 490 | return fd; |
| 491 | |
| 492 | out_put_group: |
| 493 | fsnotify_put_group(group); |
| 494 | return fd; |
Eric Paris | 11637e4 | 2009-12-17 21:24:25 -0500 | [diff] [blame] | 495 | } |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 496 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 497 | SYSCALL_DEFINE(fanotify_mark)(int fanotify_fd, unsigned int flags, |
| 498 | __u64 mask, int dfd, |
| 499 | const char __user * pathname) |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 500 | { |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 501 | struct inode *inode = NULL; |
| 502 | struct vfsmount *mnt = NULL; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 503 | struct fsnotify_group *group; |
| 504 | struct file *filp; |
| 505 | struct path path; |
| 506 | int ret, fput_needed; |
| 507 | |
| 508 | pr_debug("%s: fanotify_fd=%d flags=%x dfd=%d pathname=%p mask=%llx\n", |
| 509 | __func__, fanotify_fd, flags, dfd, pathname, mask); |
| 510 | |
| 511 | /* we only use the lower 32 bits as of right now. */ |
| 512 | if (mask & ((__u64)0xffffffff << 32)) |
| 513 | return -EINVAL; |
| 514 | |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 515 | if (flags & ~FAN_ALL_MARK_FLAGS) |
| 516 | return -EINVAL; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame^] | 517 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 518 | case FAN_MARK_ADD: |
| 519 | case FAN_MARK_REMOVE: |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame^] | 520 | case FAN_MARK_FLUSH: |
Andreas Gruenbacher | 88380fe | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 521 | break; |
| 522 | default: |
| 523 | return -EINVAL; |
| 524 | } |
| 525 | if (mask & ~(FAN_ALL_EVENTS | FAN_EVENT_ON_CHILD)) |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | |
| 528 | filp = fget_light(fanotify_fd, &fput_needed); |
| 529 | if (unlikely(!filp)) |
| 530 | return -EBADF; |
| 531 | |
| 532 | /* verify that this is indeed an fanotify instance */ |
| 533 | ret = -EINVAL; |
| 534 | if (unlikely(filp->f_op != &fanotify_fops)) |
| 535 | goto fput_and_out; |
| 536 | |
| 537 | ret = fanotify_find_path(dfd, pathname, &path, flags); |
| 538 | if (ret) |
| 539 | goto fput_and_out; |
| 540 | |
| 541 | /* 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] | 542 | if (!(flags & FAN_MARK_MOUNT)) |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 543 | inode = path.dentry->d_inode; |
| 544 | else |
| 545 | mnt = path.mnt; |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 546 | group = filp->private_data; |
| 547 | |
| 548 | /* create/update an inode mark */ |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame^] | 549 | switch (flags & (FAN_MARK_ADD | FAN_MARK_REMOVE | FAN_MARK_FLUSH)) { |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 550 | case FAN_MARK_ADD: |
Andreas Gruenbacher | eac8e9e | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 551 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 552 | ret = fanotify_add_vfsmount_mark(group, mnt, mask, flags); |
Eric Paris | 0ff21db | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 553 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 554 | ret = fanotify_add_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 555 | break; |
| 556 | case FAN_MARK_REMOVE: |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 557 | if (flags & FAN_MARK_MOUNT) |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 558 | ret = fanotify_remove_vfsmount_mark(group, mnt, mask, flags); |
Andreas Gruenbacher | f364019 | 2009-12-17 21:24:29 -0500 | [diff] [blame] | 559 | else |
Eric Paris | b9e4e3b | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 560 | ret = fanotify_remove_inode_mark(group, inode, mask, flags); |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 561 | break; |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame^] | 562 | case FAN_MARK_FLUSH: |
| 563 | if (flags & FAN_MARK_MOUNT) |
| 564 | fsnotify_clear_vfsmount_marks_by_group(group); |
| 565 | else |
| 566 | fsnotify_clear_inode_marks_by_group(group); |
| 567 | fsnotify_recalc_group_mask(group); |
| 568 | break; |
Andreas Gruenbacher | c6223f4 | 2009-12-17 21:24:28 -0500 | [diff] [blame] | 569 | default: |
| 570 | ret = -EINVAL; |
| 571 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 572 | |
| 573 | path_put(&path); |
| 574 | fput_and_out: |
| 575 | fput_light(filp, fput_needed); |
| 576 | return ret; |
Eric Paris | bbaa416 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 577 | } |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 578 | |
Heiko Carstens | 9bbfc96 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 579 | #ifdef CONFIG_HAVE_SYSCALL_WRAPPERS |
| 580 | asmlinkage long SyS_fanotify_mark(long fanotify_fd, long flags, __u64 mask, |
| 581 | long dfd, long pathname) |
| 582 | { |
| 583 | return SYSC_fanotify_mark((int) fanotify_fd, (unsigned int) flags, |
| 584 | mask, (int) dfd, |
| 585 | (const char __user *) pathname); |
| 586 | } |
| 587 | SYSCALL_ALIAS(sys_fanotify_mark, SyS_fanotify_mark); |
| 588 | #endif |
| 589 | |
Eric Paris | 2a3edf8 | 2009-12-17 21:24:26 -0500 | [diff] [blame] | 590 | /* |
| 591 | * fanotify_user_setup - Our initialization function. Note that we cannnot return |
| 592 | * error because we have compiled-in VFS hooks. So an (unlikely) failure here |
| 593 | * must result in panic(). |
| 594 | */ |
| 595 | static int __init fanotify_user_setup(void) |
| 596 | { |
| 597 | fanotify_mark_cache = KMEM_CACHE(fsnotify_mark, SLAB_PANIC); |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | device_initcall(fanotify_user_setup); |