blob: 6111670b25736a9a91e38e2fe76f14ecd1f16fb0 [file] [log] [blame]
Amy Griffis2d9048e2006-06-01 13:10:59 -07001/*
2 * fs/inotify_user.c - inotify support for userspace
3 *
4 * Authors:
5 * John McCutchan <ttb@tentacle.dhs.org>
6 * Robert Love <rml@novell.com>
7 *
8 * Copyright (C) 2005 John McCutchan
9 * Copyright 2006 Hewlett-Packard Development Company, L.P.
10 *
Eric Paris63c882a2009-05-21 17:02:01 -040011 * Copyright (C) 2009 Eric Paris <Red Hat Inc>
12 * inotify was largely rewriten to make use of the fsnotify infrastructure
13 *
Amy Griffis2d9048e2006-06-01 13:10:59 -070014 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2, or (at your option) any
17 * later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 */
24
Amy Griffis2d9048e2006-06-01 13:10:59 -070025#include <linux/file.h>
Eric Paris63c882a2009-05-21 17:02:01 -040026#include <linux/fs.h> /* struct inode */
27#include <linux/fsnotify_backend.h>
28#include <linux/idr.h>
29#include <linux/init.h> /* module_init */
Amy Griffis2d9048e2006-06-01 13:10:59 -070030#include <linux/inotify.h>
Eric Paris63c882a2009-05-21 17:02:01 -040031#include <linux/kernel.h> /* roundup() */
32#include <linux/magic.h> /* superblock magic number */
33#include <linux/mount.h> /* mntget */
34#include <linux/namei.h> /* LOOKUP_FOLLOW */
35#include <linux/path.h> /* struct path */
36#include <linux/sched.h> /* struct user */
37#include <linux/slab.h> /* struct kmem_cache */
Amy Griffis2d9048e2006-06-01 13:10:59 -070038#include <linux/syscalls.h>
Eric Paris63c882a2009-05-21 17:02:01 -040039#include <linux/types.h>
40#include <linux/uaccess.h>
41#include <linux/poll.h>
42#include <linux/wait.h>
43
44#include "inotify.h"
Amy Griffis2d9048e2006-06-01 13:10:59 -070045
46#include <asm/ioctls.h>
47
Amy Griffis2d9048e2006-06-01 13:10:59 -070048static struct vfsmount *inotify_mnt __read_mostly;
49
50/* these are configurable via /proc/sys/fs/inotify/ */
Harvey Harrison3c828e42008-02-14 19:31:21 -080051static int inotify_max_user_instances __read_mostly;
Harvey Harrison3c828e42008-02-14 19:31:21 -080052static int inotify_max_queued_events __read_mostly;
Eric Paris63c882a2009-05-21 17:02:01 -040053int inotify_max_user_watches __read_mostly;
54
55static struct kmem_cache *inotify_inode_mark_cachep __read_mostly;
56struct kmem_cache *event_priv_cachep __read_mostly;
Amy Griffis2d9048e2006-06-01 13:10:59 -070057
58/*
Eric Paris63c882a2009-05-21 17:02:01 -040059 * When inotify registers a new group it increments this and uses that
60 * value as an offset to set the fsnotify group "name" and priority.
Amy Griffis2d9048e2006-06-01 13:10:59 -070061 */
Eric Paris63c882a2009-05-21 17:02:01 -040062static atomic_t inotify_grp_num;
Amy Griffis2d9048e2006-06-01 13:10:59 -070063
64#ifdef CONFIG_SYSCTL
65
66#include <linux/sysctl.h>
67
68static int zero;
69
70ctl_table inotify_table[] = {
71 {
72 .ctl_name = INOTIFY_MAX_USER_INSTANCES,
73 .procname = "max_user_instances",
74 .data = &inotify_max_user_instances,
75 .maxlen = sizeof(int),
76 .mode = 0644,
77 .proc_handler = &proc_dointvec_minmax,
78 .strategy = &sysctl_intvec,
79 .extra1 = &zero,
80 },
81 {
82 .ctl_name = INOTIFY_MAX_USER_WATCHES,
83 .procname = "max_user_watches",
84 .data = &inotify_max_user_watches,
85 .maxlen = sizeof(int),
86 .mode = 0644,
87 .proc_handler = &proc_dointvec_minmax,
88 .strategy = &sysctl_intvec,
89 .extra1 = &zero,
90 },
91 {
92 .ctl_name = INOTIFY_MAX_QUEUED_EVENTS,
93 .procname = "max_queued_events",
94 .data = &inotify_max_queued_events,
95 .maxlen = sizeof(int),
96 .mode = 0644,
97 .proc_handler = &proc_dointvec_minmax,
98 .strategy = &sysctl_intvec,
99 .extra1 = &zero
100 },
101 { .ctl_name = 0 }
102};
103#endif /* CONFIG_SYSCTL */
104
Eric Paris63c882a2009-05-21 17:02:01 -0400105static inline __u32 inotify_arg_to_mask(u32 arg)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700106{
Eric Paris63c882a2009-05-21 17:02:01 -0400107 __u32 mask;
108
109 /* everything should accept their own ignored and cares about children */
110 mask = (FS_IN_IGNORED | FS_EVENT_ON_CHILD);
111
112 /* mask off the flags used to open the fd */
113 mask |= (arg & (IN_ALL_EVENTS | IN_ONESHOT));
114
115 return mask;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700116}
117
Eric Paris63c882a2009-05-21 17:02:01 -0400118static inline u32 inotify_mask_to_arg(__u32 mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700119{
Eric Paris63c882a2009-05-21 17:02:01 -0400120 return mask & (IN_ALL_EVENTS | IN_ISDIR | IN_UNMOUNT | IN_IGNORED |
121 IN_Q_OVERFLOW);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700122}
123
Eric Paris63c882a2009-05-21 17:02:01 -0400124/* intofiy userspace file descriptor functions */
Amy Griffis2d9048e2006-06-01 13:10:59 -0700125static unsigned int inotify_poll(struct file *file, poll_table *wait)
126{
Eric Paris63c882a2009-05-21 17:02:01 -0400127 struct fsnotify_group *group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700128 int ret = 0;
129
Eric Paris63c882a2009-05-21 17:02:01 -0400130 poll_wait(file, &group->notification_waitq, wait);
131 mutex_lock(&group->notification_mutex);
132 if (!fsnotify_notify_queue_is_empty(group))
Amy Griffis2d9048e2006-06-01 13:10:59 -0700133 ret = POLLIN | POLLRDNORM;
Eric Paris63c882a2009-05-21 17:02:01 -0400134 mutex_unlock(&group->notification_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700135
136 return ret;
137}
138
Vegard Nossum3632dee2009-01-22 15:29:45 +0100139/*
140 * Get an inotify_kernel_event if one exists and is small
141 * enough to fit in "count". Return an error pointer if
142 * not large enough.
143 *
Eric Paris63c882a2009-05-21 17:02:01 -0400144 * Called with the group->notification_mutex held.
Vegard Nossum3632dee2009-01-22 15:29:45 +0100145 */
Eric Paris63c882a2009-05-21 17:02:01 -0400146static struct fsnotify_event *get_one_event(struct fsnotify_group *group,
147 size_t count)
Vegard Nossum3632dee2009-01-22 15:29:45 +0100148{
149 size_t event_size = sizeof(struct inotify_event);
Eric Paris63c882a2009-05-21 17:02:01 -0400150 struct fsnotify_event *event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100151
Eric Paris63c882a2009-05-21 17:02:01 -0400152 if (fsnotify_notify_queue_is_empty(group))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100153 return NULL;
154
Eric Paris63c882a2009-05-21 17:02:01 -0400155 event = fsnotify_peek_notify_event(group);
156
Eric Paris83cb10f2009-08-28 11:57:55 -0400157 if (event->name_len)
158 event_size += roundup(event->name_len + 1, event_size);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100159
160 if (event_size > count)
161 return ERR_PTR(-EINVAL);
162
Eric Paris63c882a2009-05-21 17:02:01 -0400163 /* held the notification_mutex the whole time, so this is the
164 * same event we peeked above */
165 fsnotify_remove_notify_event(group);
166
167 return event;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100168}
169
170/*
171 * Copy an event to user space, returning how much we copied.
172 *
173 * We already checked that the event size is smaller than the
174 * buffer we had in "get_one_event()" above.
175 */
Eric Paris63c882a2009-05-21 17:02:01 -0400176static ssize_t copy_event_to_user(struct fsnotify_group *group,
177 struct fsnotify_event *event,
Vegard Nossum3632dee2009-01-22 15:29:45 +0100178 char __user *buf)
179{
Eric Paris63c882a2009-05-21 17:02:01 -0400180 struct inotify_event inotify_event;
181 struct fsnotify_event_private_data *fsn_priv;
182 struct inotify_event_private_data *priv;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100183 size_t event_size = sizeof(struct inotify_event);
Brian Rogersb962e732009-08-28 10:00:05 -0400184 size_t name_len = 0;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100185
Eric Paris63c882a2009-05-21 17:02:01 -0400186 /* we get the inotify watch descriptor from the event private data */
187 spin_lock(&event->lock);
188 fsn_priv = fsnotify_remove_priv_from_event(group, event);
189 spin_unlock(&event->lock);
190
191 if (!fsn_priv)
192 inotify_event.wd = -1;
193 else {
194 priv = container_of(fsn_priv, struct inotify_event_private_data,
195 fsnotify_event_priv_data);
196 inotify_event.wd = priv->wd;
197 inotify_free_event_priv(fsn_priv);
198 }
199
Brian Rogersb962e732009-08-28 10:00:05 -0400200 /*
201 * round up event->name_len so it is a multiple of event_size
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700202 * plus an extra byte for the terminating '\0'.
203 */
Brian Rogersb962e732009-08-28 10:00:05 -0400204 if (event->name_len)
205 name_len = roundup(event->name_len + 1, event_size);
Eric Paris63c882a2009-05-21 17:02:01 -0400206 inotify_event.len = name_len;
207
208 inotify_event.mask = inotify_mask_to_arg(event->mask);
209 inotify_event.cookie = event->sync_cookie;
210
211 /* send the main event */
212 if (copy_to_user(buf, &inotify_event, event_size))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100213 return -EFAULT;
214
Eric Paris63c882a2009-05-21 17:02:01 -0400215 buf += event_size;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100216
Eric Paris63c882a2009-05-21 17:02:01 -0400217 /*
218 * fsnotify only stores the pathname, so here we have to send the pathname
219 * and then pad that pathname out to a multiple of sizeof(inotify_event)
220 * with zeros. I get my zeros from the nul_inotify_event.
221 */
222 if (name_len) {
223 unsigned int len_to_zero = name_len - event->name_len;
224 /* copy the path name */
225 if (copy_to_user(buf, event->file_name, event->name_len))
Vegard Nossum3632dee2009-01-22 15:29:45 +0100226 return -EFAULT;
Eric Paris63c882a2009-05-21 17:02:01 -0400227 buf += event->name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100228
Eric W. Biederman0db501b2009-08-27 03:20:04 -0700229 /* fill userspace with 0's */
230 if (clear_user(buf, len_to_zero))
Eric Paris63c882a2009-05-21 17:02:01 -0400231 return -EFAULT;
232 buf += len_to_zero;
233 event_size += name_len;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100234 }
Eric Paris63c882a2009-05-21 17:02:01 -0400235
Vegard Nossum3632dee2009-01-22 15:29:45 +0100236 return event_size;
237}
238
Amy Griffis2d9048e2006-06-01 13:10:59 -0700239static ssize_t inotify_read(struct file *file, char __user *buf,
240 size_t count, loff_t *pos)
241{
Eric Paris63c882a2009-05-21 17:02:01 -0400242 struct fsnotify_group *group;
243 struct fsnotify_event *kevent;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700244 char __user *start;
245 int ret;
246 DEFINE_WAIT(wait);
247
248 start = buf;
Eric Paris63c882a2009-05-21 17:02:01 -0400249 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700250
251 while (1) {
Eric Paris63c882a2009-05-21 17:02:01 -0400252 prepare_to_wait(&group->notification_waitq, &wait, TASK_INTERRUPTIBLE);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700253
Eric Paris63c882a2009-05-21 17:02:01 -0400254 mutex_lock(&group->notification_mutex);
255 kevent = get_one_event(group, count);
256 mutex_unlock(&group->notification_mutex);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700257
Vegard Nossum3632dee2009-01-22 15:29:45 +0100258 if (kevent) {
259 ret = PTR_ERR(kevent);
260 if (IS_ERR(kevent))
261 break;
Eric Paris63c882a2009-05-21 17:02:01 -0400262 ret = copy_event_to_user(group, kevent, buf);
263 fsnotify_put_event(kevent);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100264 if (ret < 0)
265 break;
266 buf += ret;
267 count -= ret;
268 continue;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700269 }
270
Vegard Nossum3632dee2009-01-22 15:29:45 +0100271 ret = -EAGAIN;
272 if (file->f_flags & O_NONBLOCK)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700273 break;
Vegard Nossum3632dee2009-01-22 15:29:45 +0100274 ret = -EINTR;
275 if (signal_pending(current))
276 break;
277
278 if (start != buf)
279 break;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700280
281 schedule();
282 }
283
Eric Paris63c882a2009-05-21 17:02:01 -0400284 finish_wait(&group->notification_waitq, &wait);
Vegard Nossum3632dee2009-01-22 15:29:45 +0100285 if (start != buf && ret != -EFAULT)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700286 ret = buf - start;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700287 return ret;
288}
289
Dmitry Antipovbcfbf842008-02-06 01:36:19 -0800290static int inotify_fasync(int fd, struct file *file, int on)
291{
Eric Paris63c882a2009-05-21 17:02:01 -0400292 struct fsnotify_group *group = file->private_data;
Dmitry Antipovbcfbf842008-02-06 01:36:19 -0800293
Eric Paris63c882a2009-05-21 17:02:01 -0400294 return fasync_helper(fd, file, on, &group->inotify_data.fa) >= 0 ? 0 : -EIO;
Dmitry Antipovbcfbf842008-02-06 01:36:19 -0800295}
296
Amy Griffis2d9048e2006-06-01 13:10:59 -0700297static int inotify_release(struct inode *ignored, struct file *file)
298{
Eric Paris63c882a2009-05-21 17:02:01 -0400299 struct fsnotify_group *group = file->private_data;
Keith Packardbdae9972009-07-01 21:56:38 -0700300 struct user_struct *user = group->inotify_data.user;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700301
Eric Paris63c882a2009-05-21 17:02:01 -0400302 fsnotify_clear_marks_by_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700303
Eric Paris63c882a2009-05-21 17:02:01 -0400304 /* free this group, matching get was inotify_init->fsnotify_obtain_group */
305 fsnotify_put_group(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700306
Keith Packardbdae9972009-07-01 21:56:38 -0700307 atomic_dec(&user->inotify_devs);
308
Amy Griffis2d9048e2006-06-01 13:10:59 -0700309 return 0;
310}
311
312static long inotify_ioctl(struct file *file, unsigned int cmd,
313 unsigned long arg)
314{
Eric Paris63c882a2009-05-21 17:02:01 -0400315 struct fsnotify_group *group;
316 struct fsnotify_event_holder *holder;
317 struct fsnotify_event *event;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700318 void __user *p;
319 int ret = -ENOTTY;
Eric Paris63c882a2009-05-21 17:02:01 -0400320 size_t send_len = 0;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700321
Eric Paris63c882a2009-05-21 17:02:01 -0400322 group = file->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700323 p = (void __user *) arg;
324
325 switch (cmd) {
326 case FIONREAD:
Eric Paris63c882a2009-05-21 17:02:01 -0400327 mutex_lock(&group->notification_mutex);
328 list_for_each_entry(holder, &group->notification_list, event_list) {
329 event = holder->event;
330 send_len += sizeof(struct inotify_event);
Eric Paris83cb10f2009-08-28 11:57:55 -0400331 if (event->name_len)
332 send_len += roundup(event->name_len + 1,
333 sizeof(struct inotify_event));
Eric Paris63c882a2009-05-21 17:02:01 -0400334 }
335 mutex_unlock(&group->notification_mutex);
336 ret = put_user(send_len, (int __user *) p);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700337 break;
338 }
339
340 return ret;
341}
342
343static const struct file_operations inotify_fops = {
Eric Paris63c882a2009-05-21 17:02:01 -0400344 .poll = inotify_poll,
345 .read = inotify_read,
346 .fasync = inotify_fasync,
347 .release = inotify_release,
348 .unlocked_ioctl = inotify_ioctl,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700349 .compat_ioctl = inotify_ioctl,
350};
351
Amy Griffis2d9048e2006-06-01 13:10:59 -0700352
Eric Paris63c882a2009-05-21 17:02:01 -0400353/*
354 * find_inode - resolve a user-given path to a specific inode
355 */
356static int inotify_find_inode(const char __user *dirname, struct path *path, unsigned flags)
357{
358 int error;
359
360 error = user_path_at(AT_FDCWD, dirname, flags, path);
361 if (error)
362 return error;
363 /* you can only watch an inode if you have read permissions on it */
364 error = inode_permission(path->dentry->d_inode, MAY_READ);
365 if (error)
366 path_put(path);
367 return error;
368}
369
Eric Parisdead5372009-08-24 16:03:35 -0400370/*
371 * Remove the mark from the idr (if present) and drop the reference
372 * on the mark because it was in the idr.
373 */
Eric Paris7e790dd2009-07-07 10:28:24 -0400374static void inotify_remove_from_idr(struct fsnotify_group *group,
375 struct inotify_inode_mark_entry *ientry)
376{
377 struct idr *idr;
Eric Parisdead5372009-08-24 16:03:35 -0400378 struct fsnotify_mark_entry *entry;
379 struct inotify_inode_mark_entry *found_ientry;
380 int wd;
Eric Paris7e790dd2009-07-07 10:28:24 -0400381
382 spin_lock(&group->inotify_data.idr_lock);
383 idr = &group->inotify_data.idr;
Eric Parisdead5372009-08-24 16:03:35 -0400384 wd = ientry->wd;
385
386 if (wd == -1)
387 goto out;
388
389 entry = idr_find(&group->inotify_data.idr, wd);
390 if (unlikely(!entry))
391 goto out;
392
393 found_ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
394 if (unlikely(found_ientry != ientry)) {
395 /* We found an entry in the idr with the right wd, but it's
396 * not the entry we were told to remove. eparis seriously
397 * fucked up somewhere. */
398 WARN_ON(1);
399 ientry->wd = -1;
400 goto out;
401 }
402
403 /* One ref for being in the idr, one ref held by the caller */
404 BUG_ON(atomic_read(&entry->refcnt) < 2);
405
406 idr_remove(idr, wd);
Eric Paris7e790dd2009-07-07 10:28:24 -0400407 ientry->wd = -1;
Eric Parisdead5372009-08-24 16:03:35 -0400408
409 /* removed from the idr, drop that ref */
410 fsnotify_put_mark(entry);
411out:
412 spin_unlock(&group->inotify_data.idr_lock);
Eric Paris7e790dd2009-07-07 10:28:24 -0400413}
Eric Parisdead5372009-08-24 16:03:35 -0400414
Eric Paris63c882a2009-05-21 17:02:01 -0400415/*
Eric Parisdead5372009-08-24 16:03:35 -0400416 * Send IN_IGNORED for this wd, remove this wd from the idr.
Eric Paris63c882a2009-05-21 17:02:01 -0400417 */
Eric Paris528da3e2009-06-12 16:04:26 -0400418void inotify_ignored_and_remove_idr(struct fsnotify_mark_entry *entry,
419 struct fsnotify_group *group)
Eric Paris63c882a2009-05-21 17:02:01 -0400420{
421 struct inotify_inode_mark_entry *ientry;
Eric Parisf44aebc2009-07-15 15:49:52 -0400422 struct fsnotify_event *ignored_event;
Eric Paris63c882a2009-05-21 17:02:01 -0400423 struct inotify_event_private_data *event_priv;
424 struct fsnotify_event_private_data *fsn_event_priv;
Eric Pariseef3a112009-08-16 21:51:44 -0400425 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400426
Eric Parisf44aebc2009-07-15 15:49:52 -0400427 ignored_event = fsnotify_create_event(NULL, FS_IN_IGNORED, NULL,
428 FSNOTIFY_EVENT_NONE, NULL, 0,
429 GFP_NOFS);
430 if (!ignored_event)
431 return;
432
Eric Paris63c882a2009-05-21 17:02:01 -0400433 ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
434
Eric Parisf44aebc2009-07-15 15:49:52 -0400435 event_priv = kmem_cache_alloc(event_priv_cachep, GFP_NOFS);
Eric Paris63c882a2009-05-21 17:02:01 -0400436 if (unlikely(!event_priv))
437 goto skip_send_ignore;
438
439 fsn_event_priv = &event_priv->fsnotify_event_priv_data;
440
441 fsn_event_priv->group = group;
442 event_priv->wd = ientry->wd;
443
Eric Pariseef3a112009-08-16 21:51:44 -0400444 ret = fsnotify_add_notify_event(group, ignored_event, fsn_event_priv);
445 if (ret)
Eric Paris63c882a2009-05-21 17:02:01 -0400446 inotify_free_event_priv(fsn_event_priv);
447
448skip_send_ignore:
449
Eric Parisf44aebc2009-07-15 15:49:52 -0400450 /* matches the reference taken when the event was created */
451 fsnotify_put_event(ignored_event);
452
Eric Paris63c882a2009-05-21 17:02:01 -0400453 /* remove this entry from the idr */
Eric Paris7e790dd2009-07-07 10:28:24 -0400454 inotify_remove_from_idr(group, ientry);
Eric Paris63c882a2009-05-21 17:02:01 -0400455
Eric Paris5549f7c2009-07-07 10:28:23 -0400456 atomic_dec(&group->inotify_data.user->inotify_watches);
Eric Paris63c882a2009-05-21 17:02:01 -0400457}
458
459/* ding dong the mark is dead */
460static void inotify_free_mark(struct fsnotify_mark_entry *entry)
461{
462 struct inotify_inode_mark_entry *ientry = (struct inotify_inode_mark_entry *)entry;
463
464 kmem_cache_free(inotify_inode_mark_cachep, ientry);
465}
466
Eric Paris52cef752009-08-24 16:03:35 -0400467static int inotify_update_existing_watch(struct fsnotify_group *group,
468 struct inode *inode,
469 u32 arg)
Eric Paris63c882a2009-05-21 17:02:01 -0400470{
Eric Paris52cef752009-08-24 16:03:35 -0400471 struct fsnotify_mark_entry *entry;
Eric Paris63c882a2009-05-21 17:02:01 -0400472 struct inotify_inode_mark_entry *ientry;
Eric Paris63c882a2009-05-21 17:02:01 -0400473 __u32 old_mask, new_mask;
Eric Paris52cef752009-08-24 16:03:35 -0400474 __u32 mask;
475 int add = (arg & IN_MASK_ADD);
476 int ret;
Eric Paris63c882a2009-05-21 17:02:01 -0400477
478 /* don't allow invalid bits: we don't want flags set */
479 mask = inotify_arg_to_mask(arg);
480 if (unlikely(!mask))
481 return -EINVAL;
482
Eric Paris63c882a2009-05-21 17:02:01 -0400483 spin_lock(&inode->i_lock);
484 entry = fsnotify_find_mark_entry(group, inode);
485 spin_unlock(&inode->i_lock);
Eric Paris52cef752009-08-24 16:03:35 -0400486 if (!entry)
487 return -ENOENT;
Eric Paris63c882a2009-05-21 17:02:01 -0400488
Eric Paris52cef752009-08-24 16:03:35 -0400489 ientry = container_of(entry, struct inotify_inode_mark_entry, fsn_entry);
Eric Paris75fe2b22009-07-07 10:28:23 -0400490
Eric Paris63c882a2009-05-21 17:02:01 -0400491 spin_lock(&entry->lock);
492
493 old_mask = entry->mask;
494 if (add) {
495 entry->mask |= mask;
496 new_mask = entry->mask;
497 } else {
498 entry->mask = mask;
499 new_mask = entry->mask;
500 }
501
502 spin_unlock(&entry->lock);
503
504 if (old_mask != new_mask) {
505 /* more bits in old than in new? */
506 int dropped = (old_mask & ~new_mask);
507 /* more bits in this entry than the inode's mask? */
508 int do_inode = (new_mask & ~inode->i_fsnotify_mask);
509 /* more bits in this entry than the group? */
510 int do_group = (new_mask & ~group->mask);
511
512 /* update the inode with this new entry */
513 if (dropped || do_inode)
514 fsnotify_recalc_inode_mask(inode);
515
516 /* update the group mask with the new mask */
517 if (dropped || do_group)
518 fsnotify_recalc_group_mask(group);
519 }
520
Eric Paris52cef752009-08-24 16:03:35 -0400521 /* return the wd */
522 ret = ientry->wd;
523
524 /* match the get from fsnotify_find_mark_entry() */
Eric Paris75fe2b22009-07-07 10:28:23 -0400525 fsnotify_put_mark(entry);
526
Eric Paris52cef752009-08-24 16:03:35 -0400527 return ret;
528}
529
530static int inotify_new_watch(struct fsnotify_group *group,
531 struct inode *inode,
532 u32 arg)
533{
534 struct inotify_inode_mark_entry *tmp_ientry;
535 __u32 mask;
536 int ret;
537
538 /* don't allow invalid bits: we don't want flags set */
539 mask = inotify_arg_to_mask(arg);
540 if (unlikely(!mask))
541 return -EINVAL;
542
543 tmp_ientry = kmem_cache_alloc(inotify_inode_mark_cachep, GFP_KERNEL);
544 if (unlikely(!tmp_ientry))
545 return -ENOMEM;
546
547 fsnotify_init_mark(&tmp_ientry->fsn_entry, inotify_free_mark);
548 tmp_ientry->fsn_entry.mask = mask;
549 tmp_ientry->wd = -1;
550
551 ret = -ENOSPC;
552 if (atomic_read(&group->inotify_data.user->inotify_watches) >= inotify_max_user_watches)
553 goto out_err;
554retry:
555 ret = -ENOMEM;
556 if (unlikely(!idr_pre_get(&group->inotify_data.idr, GFP_KERNEL)))
557 goto out_err;
558
559 spin_lock(&group->inotify_data.idr_lock);
560 ret = idr_get_new_above(&group->inotify_data.idr, &tmp_ientry->fsn_entry,
561 group->inotify_data.last_wd,
562 &tmp_ientry->wd);
563 spin_unlock(&group->inotify_data.idr_lock);
564 if (ret) {
565 /* idr was out of memory allocate and try again */
566 if (ret == -EAGAIN)
567 goto retry;
568 goto out_err;
Eric Paris63c882a2009-05-21 17:02:01 -0400569 }
Eric Paris7e790dd2009-07-07 10:28:24 -0400570
Eric Parisdead5372009-08-24 16:03:35 -0400571 /* we put the mark on the idr, take a reference */
572 fsnotify_get_mark(&tmp_ientry->fsn_entry);
573
Eric Paris52cef752009-08-24 16:03:35 -0400574 /* we are on the idr, now get on the inode */
575 ret = fsnotify_add_mark(&tmp_ientry->fsn_entry, group, inode);
576 if (ret) {
577 /* we failed to get on the inode, get off the idr */
578 inotify_remove_from_idr(group, tmp_ientry);
579 goto out_err;
580 }
581
Eric Paris52cef752009-08-24 16:03:35 -0400582 /* update the idr hint, who cares about races, it's just a hint */
583 group->inotify_data.last_wd = tmp_ientry->wd;
584
585 /* increment the number of watches the user has */
586 atomic_inc(&group->inotify_data.user->inotify_watches);
587
588 /* return the watch descriptor for this new entry */
589 ret = tmp_ientry->wd;
590
591 /* match the ref from fsnotify_init_markentry() */
592 fsnotify_put_mark(&tmp_ientry->fsn_entry);
593
594out_err:
595 if (ret < 0)
596 kmem_cache_free(inotify_inode_mark_cachep, tmp_ientry);
597
598 return ret;
599}
600
601static int inotify_update_watch(struct fsnotify_group *group, struct inode *inode, u32 arg)
602{
603 int ret = 0;
604
605retry:
606 /* try to update and existing watch with the new arg */
607 ret = inotify_update_existing_watch(group, inode, arg);
608 /* no mark present, try to add a new one */
609 if (ret == -ENOENT)
610 ret = inotify_new_watch(group, inode, arg);
611 /*
612 * inotify_new_watch could race with another thread which did an
613 * inotify_new_watch between the update_existing and the add watch
614 * here, go back and try to update an existing mark again.
615 */
616 if (ret == -EEXIST)
617 goto retry;
618
Eric Paris63c882a2009-05-21 17:02:01 -0400619 return ret;
620}
621
622static struct fsnotify_group *inotify_new_group(struct user_struct *user, unsigned int max_events)
623{
624 struct fsnotify_group *group;
625 unsigned int grp_num;
626
627 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
628 grp_num = (INOTIFY_GROUP_NUM - atomic_inc_return(&inotify_grp_num));
629 group = fsnotify_obtain_group(grp_num, 0, &inotify_fsnotify_ops);
630 if (IS_ERR(group))
631 return group;
632
633 group->max_events = max_events;
634
635 spin_lock_init(&group->inotify_data.idr_lock);
636 idr_init(&group->inotify_data.idr);
Eric Paris08e53fc2009-08-16 21:51:55 -0400637 group->inotify_data.last_wd = 1;
Eric Paris63c882a2009-05-21 17:02:01 -0400638 group->inotify_data.user = user;
639 group->inotify_data.fa = NULL;
640
641 return group;
642}
643
644
645/* inotify syscalls */
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100646SYSCALL_DEFINE1(inotify_init1, int, flags)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700647{
Eric Paris63c882a2009-05-21 17:02:01 -0400648 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700649 struct user_struct *user;
650 struct file *filp;
651 int fd, ret;
652
Ulrich Dreppere38b36f2008-07-23 21:29:42 -0700653 /* Check the IN_* constants for consistency. */
654 BUILD_BUG_ON(IN_CLOEXEC != O_CLOEXEC);
655 BUILD_BUG_ON(IN_NONBLOCK != O_NONBLOCK);
656
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700657 if (flags & ~(IN_CLOEXEC | IN_NONBLOCK))
Ulrich Drepper40065532008-07-23 21:29:32 -0700658 return -EINVAL;
659
660 fd = get_unused_fd_flags(flags & O_CLOEXEC);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700661 if (fd < 0)
662 return fd;
663
664 filp = get_empty_filp();
665 if (!filp) {
666 ret = -ENFILE;
667 goto out_put_fd;
668 }
669
David Howellsda9592e2008-11-14 10:39:05 +1100670 user = get_current_user();
Amy Griffis2d9048e2006-06-01 13:10:59 -0700671 if (unlikely(atomic_read(&user->inotify_devs) >=
672 inotify_max_user_instances)) {
673 ret = -EMFILE;
674 goto out_free_uid;
675 }
676
Eric Paris63c882a2009-05-21 17:02:01 -0400677 /* fsnotify_obtain_group took a reference to group, we put this when we kill the file in the end */
678 group = inotify_new_group(user, inotify_max_queued_events);
679 if (IS_ERR(group)) {
680 ret = PTR_ERR(group);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700681 goto out_free_uid;
682 }
683
Amy Griffis2d9048e2006-06-01 13:10:59 -0700684 filp->f_op = &inotify_fops;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800685 filp->f_path.mnt = mntget(inotify_mnt);
686 filp->f_path.dentry = dget(inotify_mnt->mnt_root);
687 filp->f_mapping = filp->f_path.dentry->d_inode->i_mapping;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700688 filp->f_mode = FMODE_READ;
Ulrich Drepper510df2d2008-07-23 21:29:41 -0700689 filp->f_flags = O_RDONLY | (flags & O_NONBLOCK);
Eric Paris63c882a2009-05-21 17:02:01 -0400690 filp->private_data = group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700691
Amy Griffis2d9048e2006-06-01 13:10:59 -0700692 atomic_inc(&user->inotify_devs);
Eric Paris63c882a2009-05-21 17:02:01 -0400693
Amy Griffis2d9048e2006-06-01 13:10:59 -0700694 fd_install(fd, filp);
695
696 return fd;
Eric Paris63c882a2009-05-21 17:02:01 -0400697
Amy Griffis2d9048e2006-06-01 13:10:59 -0700698out_free_uid:
699 free_uid(user);
700 put_filp(filp);
701out_put_fd:
702 put_unused_fd(fd);
703 return ret;
704}
705
Heiko Carstens938bb9f2009-01-14 14:14:30 +0100706SYSCALL_DEFINE0(inotify_init)
Ulrich Drepper40065532008-07-23 21:29:32 -0700707{
708 return sys_inotify_init1(0);
709}
710
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100711SYSCALL_DEFINE3(inotify_add_watch, int, fd, const char __user *, pathname,
712 u32, mask)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700713{
Eric Paris63c882a2009-05-21 17:02:01 -0400714 struct fsnotify_group *group;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700715 struct inode *inode;
Al Viro2d8f3032008-07-22 09:59:21 -0400716 struct path path;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700717 struct file *filp;
718 int ret, fput_needed;
719 unsigned flags = 0;
720
721 filp = fget_light(fd, &fput_needed);
722 if (unlikely(!filp))
723 return -EBADF;
724
725 /* verify that this is indeed an inotify instance */
726 if (unlikely(filp->f_op != &inotify_fops)) {
727 ret = -EINVAL;
728 goto fput_and_out;
729 }
730
731 if (!(mask & IN_DONT_FOLLOW))
732 flags |= LOOKUP_FOLLOW;
733 if (mask & IN_ONLYDIR)
734 flags |= LOOKUP_DIRECTORY;
735
Eric Paris63c882a2009-05-21 17:02:01 -0400736 ret = inotify_find_inode(pathname, &path, flags);
737 if (ret)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700738 goto fput_and_out;
739
Eric Paris63c882a2009-05-21 17:02:01 -0400740 /* inode held in place by reference to path; group by fget on fd */
Al Viro2d8f3032008-07-22 09:59:21 -0400741 inode = path.dentry->d_inode;
Eric Paris63c882a2009-05-21 17:02:01 -0400742 group = filp->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700743
Eric Paris63c882a2009-05-21 17:02:01 -0400744 /* create/update an inode mark */
745 ret = inotify_update_watch(group, inode, mask);
746 if (unlikely(ret))
747 goto path_put_and_out;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700748
Eric Paris63c882a2009-05-21 17:02:01 -0400749path_put_and_out:
Al Viro2d8f3032008-07-22 09:59:21 -0400750 path_put(&path);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700751fput_and_out:
752 fput_light(filp, fput_needed);
753 return ret;
754}
755
Heiko Carstens2e4d0922009-01-14 14:14:31 +0100756SYSCALL_DEFINE2(inotify_rm_watch, int, fd, __s32, wd)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700757{
Eric Paris63c882a2009-05-21 17:02:01 -0400758 struct fsnotify_group *group;
759 struct fsnotify_mark_entry *entry;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700760 struct file *filp;
Eric Paris63c882a2009-05-21 17:02:01 -0400761 int ret = 0, fput_needed;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700762
763 filp = fget_light(fd, &fput_needed);
764 if (unlikely(!filp))
765 return -EBADF;
766
767 /* verify that this is indeed an inotify instance */
768 if (unlikely(filp->f_op != &inotify_fops)) {
769 ret = -EINVAL;
770 goto out;
771 }
772
Eric Paris63c882a2009-05-21 17:02:01 -0400773 group = filp->private_data;
Amy Griffis2d9048e2006-06-01 13:10:59 -0700774
Eric Paris63c882a2009-05-21 17:02:01 -0400775 spin_lock(&group->inotify_data.idr_lock);
776 entry = idr_find(&group->inotify_data.idr, wd);
777 if (unlikely(!entry)) {
778 spin_unlock(&group->inotify_data.idr_lock);
779 ret = -EINVAL;
780 goto out;
781 }
782 fsnotify_get_mark(entry);
783 spin_unlock(&group->inotify_data.idr_lock);
784
Eric Paris528da3e2009-06-12 16:04:26 -0400785 fsnotify_destroy_mark_by_entry(entry);
Eric Paris63c882a2009-05-21 17:02:01 -0400786 fsnotify_put_mark(entry);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700787
788out:
789 fput_light(filp, fput_needed);
790 return ret;
791}
792
David Howells454e2392006-06-23 02:02:57 -0700793static int
Amy Griffis2d9048e2006-06-01 13:10:59 -0700794inotify_get_sb(struct file_system_type *fs_type, int flags,
David Howells454e2392006-06-23 02:02:57 -0700795 const char *dev_name, void *data, struct vfsmount *mnt)
Amy Griffis2d9048e2006-06-01 13:10:59 -0700796{
Andrey Mirkinfd5eea42007-10-16 23:30:13 -0700797 return get_sb_pseudo(fs_type, "inotify", NULL,
798 INOTIFYFS_SUPER_MAGIC, mnt);
Amy Griffis2d9048e2006-06-01 13:10:59 -0700799}
800
801static struct file_system_type inotify_fs_type = {
Eric Paris63c882a2009-05-21 17:02:01 -0400802 .name = "inotifyfs",
803 .get_sb = inotify_get_sb,
804 .kill_sb = kill_anon_super,
Amy Griffis2d9048e2006-06-01 13:10:59 -0700805};
806
807/*
808 * inotify_user_setup - Our initialization function. Note that we cannnot return
809 * error because we have compiled-in VFS hooks. So an (unlikely) failure here
810 * must result in panic().
811 */
812static int __init inotify_user_setup(void)
813{
814 int ret;
815
816 ret = register_filesystem(&inotify_fs_type);
817 if (unlikely(ret))
818 panic("inotify: register_filesystem returned %d!\n", ret);
819
820 inotify_mnt = kern_mount(&inotify_fs_type);
821 if (IS_ERR(inotify_mnt))
822 panic("inotify: kern_mount ret %ld!\n", PTR_ERR(inotify_mnt));
823
Eric Paris63c882a2009-05-21 17:02:01 -0400824 inotify_inode_mark_cachep = KMEM_CACHE(inotify_inode_mark_entry, SLAB_PANIC);
825 event_priv_cachep = KMEM_CACHE(inotify_event_private_data, SLAB_PANIC);
Eric Paris63c882a2009-05-21 17:02:01 -0400826
Amy Griffis2d9048e2006-06-01 13:10:59 -0700827 inotify_max_queued_events = 16384;
828 inotify_max_user_instances = 128;
829 inotify_max_user_watches = 8192;
830
Amy Griffis2d9048e2006-06-01 13:10:59 -0700831 return 0;
832}
Amy Griffis2d9048e2006-06-01 13:10:59 -0700833module_init(inotify_user_setup);