Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 1 | /* |
| 2 | * fs/inotify.c - inode-based file event notifications |
| 3 | * |
| 4 | * Authors: |
| 5 | * John McCutchan <ttb@tentacle.dhs.org> |
| 6 | * Robert Love <rml@novell.com> |
| 7 | * |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 8 | * Kernel API added by: Amy Griffis <amy.griffis@hp.com> |
| 9 | * |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 10 | * Copyright (C) 2005 John McCutchan |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 11 | * Copyright 2006 Hewlett-Packard Development Company, L.P. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify it |
| 14 | * under the terms of the GNU General Public License as published by the |
| 15 | * Free Software Foundation; either version 2, or (at your option) any |
| 16 | * later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, but |
| 19 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | * General Public License for more details. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 26 | #include <linux/spinlock.h> |
| 27 | #include <linux/idr.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/fs.h> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 30 | #include <linux/sched.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 31 | #include <linux/init.h> |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/writeback.h> |
| 34 | #include <linux/inotify.h> |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 35 | #include <linux/fsnotify_backend.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 36 | |
| 37 | static atomic_t inotify_cookie; |
| 38 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 39 | /* |
| 40 | * Lock ordering: |
| 41 | * |
| 42 | * dentry->d_lock (used to keep d_move() away from dentry->d_parent) |
Ingo Molnar | f24075b | 2006-03-23 03:00:34 -0800 | [diff] [blame] | 43 | * iprune_mutex (synchronize shrink_icache_memory()) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 44 | * inode_lock (protects the super_block->s_inodes list) |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 45 | * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 46 | * inotify_handle->mutex (protects inotify_handle and watches->h_list) |
| 47 | * |
| 48 | * The inode->inotify_mutex and inotify_handle->mutex and held during execution |
| 49 | * of a caller's event handler. Thus, the caller must not hold any locks |
| 50 | * taken in their event handler while calling any of the published inotify |
| 51 | * interfaces. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 52 | */ |
| 53 | |
| 54 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 55 | * Lifetimes of the three main data structures--inotify_handle, inode, and |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 56 | * inotify_watch--are managed by reference count. |
| 57 | * |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 58 | * inotify_handle: Lifetime is from inotify_init() to inotify_destroy(). |
| 59 | * Additional references can bump the count via get_inotify_handle() and drop |
| 60 | * the count via put_inotify_handle(). |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 61 | * |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 62 | * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch() |
| 63 | * to remove_watch_no_event(). Additional references can bump the count via |
| 64 | * get_inotify_watch() and drop the count via put_inotify_watch(). The caller |
| 65 | * is reponsible for the final put after receiving IN_IGNORED, or when using |
| 66 | * IN_ONESHOT after receiving the first event. Inotify does the final put if |
| 67 | * inotify_destroy() is called. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 68 | * |
| 69 | * inode: Pinned so long as the inode is associated with a watch, from |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 70 | * inotify_add_watch() to the final put_inotify_watch(). |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 71 | */ |
| 72 | |
| 73 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 74 | * struct inotify_handle - represents an inotify instance |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 75 | * |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 76 | * This structure is protected by the mutex 'mutex'. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 77 | */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 78 | struct inotify_handle { |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 79 | struct idr idr; /* idr mapping wd -> watch */ |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 80 | struct mutex mutex; /* protects this bad boy */ |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 81 | struct list_head watches; /* list of watches */ |
| 82 | atomic_t count; /* reference count */ |
John McCutchan | b9c55d2 | 2005-08-01 11:00:45 -0400 | [diff] [blame] | 83 | u32 last_wd; /* the last wd allocated */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 84 | const struct inotify_operations *in_ops; /* inotify caller operations */ |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 85 | }; |
| 86 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 87 | static inline void get_inotify_handle(struct inotify_handle *ih) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 88 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 89 | atomic_inc(&ih->count); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 92 | static inline void put_inotify_handle(struct inotify_handle *ih) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 93 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 94 | if (atomic_dec_and_test(&ih->count)) { |
| 95 | idr_destroy(&ih->idr); |
| 96 | kfree(ih); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 100 | /** |
| 101 | * get_inotify_watch - grab a reference to an inotify_watch |
| 102 | * @watch: watch to grab |
| 103 | */ |
| 104 | void get_inotify_watch(struct inotify_watch *watch) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 105 | { |
| 106 | atomic_inc(&watch->count); |
| 107 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 108 | EXPORT_SYMBOL_GPL(get_inotify_watch); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 109 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 110 | int pin_inotify_watch(struct inotify_watch *watch) |
| 111 | { |
| 112 | struct super_block *sb = watch->inode->i_sb; |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 113 | if (atomic_inc_not_zero(&sb->s_active)) { |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 114 | atomic_inc(&watch->count); |
| 115 | return 1; |
| 116 | } |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 117 | return 0; |
| 118 | } |
| 119 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 120 | /** |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 121 | * put_inotify_watch - decrements the ref count on a given watch. cleans up |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 122 | * watch references if the count reaches zero. inotify_watch is freed by |
| 123 | * inotify callers via the destroy_watch() op. |
| 124 | * @watch: watch to release |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 125 | */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 126 | void put_inotify_watch(struct inotify_watch *watch) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 127 | { |
| 128 | if (atomic_dec_and_test(&watch->count)) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 129 | struct inotify_handle *ih = watch->ih; |
| 130 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 131 | iput(watch->inode); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 132 | ih->in_ops->destroy_watch(watch); |
| 133 | put_inotify_handle(ih); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 134 | } |
| 135 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 136 | EXPORT_SYMBOL_GPL(put_inotify_watch); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 137 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 138 | void unpin_inotify_watch(struct inotify_watch *watch) |
| 139 | { |
| 140 | struct super_block *sb = watch->inode->i_sb; |
| 141 | put_inotify_watch(watch); |
| 142 | deactivate_super(sb); |
| 143 | } |
| 144 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 145 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 146 | * inotify_handle_get_wd - returns the next WD for use by the given handle |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 147 | * |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 148 | * Callers must hold ih->mutex. This function can sleep. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 149 | */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 150 | static int inotify_handle_get_wd(struct inotify_handle *ih, |
| 151 | struct inotify_watch *watch) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 152 | { |
| 153 | int ret; |
| 154 | |
| 155 | do { |
Ingo Molnar | f04b30d | 2009-02-18 14:48:43 -0800 | [diff] [blame] | 156 | if (unlikely(!idr_pre_get(&ih->idr, GFP_NOFS))) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 157 | return -ENOSPC; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 158 | ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 159 | } while (ret == -EAGAIN); |
| 160 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 161 | if (likely(!ret)) |
| 162 | ih->last_wd = watch->wd; |
| 163 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | /* |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 168 | * inotify_inode_watched - returns nonzero if there are watches on this inode |
| 169 | * and zero otherwise. We call this lockless, we do not care if we race. |
| 170 | */ |
| 171 | static inline int inotify_inode_watched(struct inode *inode) |
| 172 | { |
| 173 | return !list_empty(&inode->inotify_watches); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Get child dentry flag into synch with parent inode. |
| 178 | * Flag should always be clear for negative dentrys. |
| 179 | */ |
| 180 | static void set_dentry_child_flags(struct inode *inode, int watched) |
| 181 | { |
| 182 | struct dentry *alias; |
| 183 | |
| 184 | spin_lock(&dcache_lock); |
| 185 | list_for_each_entry(alias, &inode->i_dentry, d_alias) { |
| 186 | struct dentry *child; |
| 187 | |
| 188 | list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) { |
Nick Piggin | 0d71bd5 | 2008-02-06 01:37:29 -0800 | [diff] [blame] | 189 | if (!child->d_inode) |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 190 | continue; |
Nick Piggin | 0d71bd5 | 2008-02-06 01:37:29 -0800 | [diff] [blame] | 191 | |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 192 | spin_lock(&child->d_lock); |
Nick Piggin | 0d71bd5 | 2008-02-06 01:37:29 -0800 | [diff] [blame] | 193 | if (watched) |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 194 | child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED; |
Nick Piggin | 0d71bd5 | 2008-02-06 01:37:29 -0800 | [diff] [blame] | 195 | else |
| 196 | child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED; |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 197 | spin_unlock(&child->d_lock); |
| 198 | } |
| 199 | } |
| 200 | spin_unlock(&dcache_lock); |
| 201 | } |
| 202 | |
| 203 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 204 | * inotify_find_handle - find the watch associated with the given inode and |
| 205 | * handle |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 206 | * |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 207 | * Callers must hold inode->inotify_mutex. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 208 | */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 209 | static struct inotify_watch *inode_find_handle(struct inode *inode, |
| 210 | struct inotify_handle *ih) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 211 | { |
| 212 | struct inotify_watch *watch; |
| 213 | |
| 214 | list_for_each_entry(watch, &inode->inotify_watches, i_list) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 215 | if (watch->ih == ih) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 216 | return watch; |
| 217 | } |
| 218 | |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | /* |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 223 | * remove_watch_no_event - remove watch without the IN_IGNORED event. |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 224 | * |
| 225 | * Callers must hold both inode->inotify_mutex and ih->mutex. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 226 | */ |
| 227 | static void remove_watch_no_event(struct inotify_watch *watch, |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 228 | struct inotify_handle *ih) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 229 | { |
| 230 | list_del(&watch->i_list); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 231 | list_del(&watch->h_list); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 232 | |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 233 | if (!inotify_inode_watched(watch->inode)) |
| 234 | set_dentry_child_flags(watch->inode, 0); |
| 235 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 236 | idr_remove(&ih->idr, watch->wd); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 239 | /** |
| 240 | * inotify_remove_watch_locked - Remove a watch from both the handle and the |
| 241 | * inode. Sends the IN_IGNORED event signifying that the inode is no longer |
| 242 | * watched. May be invoked from a caller's event handler. |
| 243 | * @ih: inotify handle associated with watch |
| 244 | * @watch: watch to remove |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 245 | * |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 246 | * Callers must hold both inode->inotify_mutex and ih->mutex. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 247 | */ |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 248 | void inotify_remove_watch_locked(struct inotify_handle *ih, |
| 249 | struct inotify_watch *watch) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 250 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 251 | remove_watch_no_event(watch, ih); |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 252 | ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 253 | } |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 254 | EXPORT_SYMBOL_GPL(inotify_remove_watch_locked); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 255 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 256 | /* Kernel API for producing events */ |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 257 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 258 | /* |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 259 | * inotify_d_instantiate - instantiate dcache entry for inode |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 260 | */ |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 261 | void inotify_d_instantiate(struct dentry *entry, struct inode *inode) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 262 | { |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 263 | struct dentry *parent; |
| 264 | |
| 265 | if (!inode) |
| 266 | return; |
| 267 | |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 268 | spin_lock(&entry->d_lock); |
| 269 | parent = entry->d_parent; |
Arnd Bergmann | 091e881 | 2006-04-10 22:54:31 -0700 | [diff] [blame] | 270 | if (parent->d_inode && inotify_inode_watched(parent->d_inode)) |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 271 | entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED; |
| 272 | spin_unlock(&entry->d_lock); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 273 | } |
| 274 | |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 275 | /* |
| 276 | * inotify_d_move - dcache entry has been moved |
| 277 | */ |
| 278 | void inotify_d_move(struct dentry *entry) |
| 279 | { |
| 280 | struct dentry *parent; |
| 281 | |
| 282 | parent = entry->d_parent; |
| 283 | if (inotify_inode_watched(parent->d_inode)) |
| 284 | entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED; |
| 285 | else |
| 286 | entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED; |
| 287 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * inotify_inode_queue_event - queue an event to all watches on this inode |
| 291 | * @inode: inode event is originating from |
| 292 | * @mask: event mask describing this event |
| 293 | * @cookie: cookie for synchronization, or zero |
| 294 | * @name: filename, if any |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 295 | * @n_inode: inode associated with name |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 296 | */ |
| 297 | void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie, |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 298 | const char *name, struct inode *n_inode) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 299 | { |
| 300 | struct inotify_watch *watch, *next; |
| 301 | |
| 302 | if (!inotify_inode_watched(inode)) |
| 303 | return; |
| 304 | |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 305 | mutex_lock(&inode->inotify_mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 306 | list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { |
| 307 | u32 watch_mask = watch->mask; |
| 308 | if (watch_mask & mask) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 309 | struct inotify_handle *ih= watch->ih; |
| 310 | mutex_lock(&ih->mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 311 | if (watch_mask & IN_ONESHOT) |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 312 | remove_watch_no_event(watch, ih); |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 313 | ih->in_ops->handle_event(watch, watch->wd, mask, cookie, |
| 314 | name, n_inode); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 315 | mutex_unlock(&ih->mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 316 | } |
| 317 | } |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 318 | mutex_unlock(&inode->inotify_mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 319 | } |
| 320 | EXPORT_SYMBOL_GPL(inotify_inode_queue_event); |
| 321 | |
| 322 | /** |
| 323 | * inotify_dentry_parent_queue_event - queue an event to a dentry's parent |
| 324 | * @dentry: the dentry in question, we queue against this dentry's parent |
| 325 | * @mask: event mask describing this event |
| 326 | * @cookie: cookie for synchronization, or zero |
| 327 | * @name: filename, if any |
| 328 | */ |
| 329 | void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask, |
| 330 | u32 cookie, const char *name) |
| 331 | { |
| 332 | struct dentry *parent; |
| 333 | struct inode *inode; |
| 334 | |
Nick Piggin | c32ccd8 | 2006-03-25 03:07:09 -0800 | [diff] [blame] | 335 | if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED)) |
John McCutchan | 820249b | 2005-09-06 15:16:38 -0700 | [diff] [blame] | 336 | return; |
| 337 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 338 | spin_lock(&dentry->d_lock); |
| 339 | parent = dentry->d_parent; |
| 340 | inode = parent->d_inode; |
| 341 | |
| 342 | if (inotify_inode_watched(inode)) { |
| 343 | dget(parent); |
| 344 | spin_unlock(&dentry->d_lock); |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 345 | inotify_inode_queue_event(inode, mask, cookie, name, |
| 346 | dentry->d_inode); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 347 | dput(parent); |
| 348 | } else |
| 349 | spin_unlock(&dentry->d_lock); |
| 350 | } |
| 351 | EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event); |
| 352 | |
| 353 | /** |
| 354 | * inotify_get_cookie - return a unique cookie for use in synchronizing events. |
| 355 | */ |
| 356 | u32 inotify_get_cookie(void) |
| 357 | { |
| 358 | return atomic_inc_return(&inotify_cookie); |
| 359 | } |
| 360 | EXPORT_SYMBOL_GPL(inotify_get_cookie); |
| 361 | |
| 362 | /** |
| 363 | * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes. |
| 364 | * @list: list of inodes being unmounted (sb->s_inodes) |
| 365 | * |
| 366 | * Called with inode_lock held, protecting the unmounting super block's list |
Ingo Molnar | f24075b | 2006-03-23 03:00:34 -0800 | [diff] [blame] | 367 | * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 368 | * We temporarily drop inode_lock, however, and CAN block. |
| 369 | */ |
| 370 | void inotify_unmount_inodes(struct list_head *list) |
| 371 | { |
| 372 | struct inode *inode, *next_i, *need_iput = NULL; |
| 373 | |
| 374 | list_for_each_entry_safe(inode, next_i, list, i_sb_list) { |
| 375 | struct inotify_watch *watch, *next_w; |
| 376 | struct inode *need_iput_tmp; |
| 377 | struct list_head *watches; |
| 378 | |
| 379 | /* |
Nick Piggin | aabb8fd | 2009-03-11 13:17:36 -0700 | [diff] [blame] | 380 | * We cannot __iget() an inode in state I_CLEAR, I_FREEING, |
| 381 | * I_WILL_FREE, or I_NEW which is fine because by that point |
| 382 | * the inode cannot have any associated watches. |
| 383 | */ |
| 384 | if (inode->i_state & (I_CLEAR|I_FREEING|I_WILL_FREE|I_NEW)) |
| 385 | continue; |
| 386 | |
| 387 | /* |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 388 | * If i_count is zero, the inode cannot have any watches and |
| 389 | * doing an __iget/iput with MS_ACTIVE clear would actually |
| 390 | * evict all inodes with zero i_count from icache which is |
| 391 | * unnecessarily violent and may in fact be illegal to do. |
| 392 | */ |
| 393 | if (!atomic_read(&inode->i_count)) |
| 394 | continue; |
| 395 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 396 | need_iput_tmp = need_iput; |
| 397 | need_iput = NULL; |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 398 | /* In case inotify_remove_watch_locked() drops a reference. */ |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 399 | if (inode != need_iput_tmp) |
| 400 | __iget(inode); |
| 401 | else |
| 402 | need_iput_tmp = NULL; |
| 403 | /* In case the dropping of a reference would nuke next_i. */ |
| 404 | if ((&next_i->i_sb_list != list) && |
| 405 | atomic_read(&next_i->i_count) && |
| 406 | !(next_i->i_state & (I_CLEAR | I_FREEING | |
| 407 | I_WILL_FREE))) { |
| 408 | __iget(next_i); |
| 409 | need_iput = next_i; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * We can safely drop inode_lock here because we hold |
| 414 | * references on both inode and next_i. Also no new inodes |
| 415 | * will be added since the umount has begun. Finally, |
Ingo Molnar | f24075b | 2006-03-23 03:00:34 -0800 | [diff] [blame] | 416 | * iprune_mutex keeps shrink_icache_memory() away. |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 417 | */ |
| 418 | spin_unlock(&inode_lock); |
| 419 | |
| 420 | if (need_iput_tmp) |
| 421 | iput(need_iput_tmp); |
| 422 | |
| 423 | /* for each watch, send IN_UNMOUNT and then remove it */ |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 424 | mutex_lock(&inode->inotify_mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 425 | watches = &inode->inotify_watches; |
| 426 | list_for_each_entry_safe(watch, next_w, watches, i_list) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 427 | struct inotify_handle *ih= watch->ih; |
Dmitri Monakhov | 6ee5a39 | 2008-12-09 13:14:26 -0800 | [diff] [blame] | 428 | get_inotify_watch(watch); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 429 | mutex_lock(&ih->mutex); |
| 430 | ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0, |
Amy Griffis | 7c29772 | 2006-06-01 13:11:01 -0700 | [diff] [blame] | 431 | NULL, NULL); |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 432 | inotify_remove_watch_locked(ih, watch); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 433 | mutex_unlock(&ih->mutex); |
Dmitri Monakhov | 6ee5a39 | 2008-12-09 13:14:26 -0800 | [diff] [blame] | 434 | put_inotify_watch(watch); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 435 | } |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 436 | mutex_unlock(&inode->inotify_mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 437 | iput(inode); |
| 438 | |
| 439 | spin_lock(&inode_lock); |
| 440 | } |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(inotify_unmount_inodes); |
| 443 | |
| 444 | /** |
| 445 | * inotify_inode_is_dead - an inode has been deleted, cleanup any watches |
| 446 | * @inode: inode that is about to be removed |
| 447 | */ |
| 448 | void inotify_inode_is_dead(struct inode *inode) |
| 449 | { |
| 450 | struct inotify_watch *watch, *next; |
| 451 | |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 452 | mutex_lock(&inode->inotify_mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 453 | list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 454 | struct inotify_handle *ih = watch->ih; |
| 455 | mutex_lock(&ih->mutex); |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 456 | inotify_remove_watch_locked(ih, watch); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 457 | mutex_unlock(&ih->mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 458 | } |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 459 | mutex_unlock(&inode->inotify_mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 460 | } |
| 461 | EXPORT_SYMBOL_GPL(inotify_inode_is_dead); |
| 462 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 463 | /* Kernel Consumer API */ |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 464 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 465 | /** |
| 466 | * inotify_init - allocate and initialize an inotify instance |
| 467 | * @ops: caller's inotify operations |
| 468 | */ |
| 469 | struct inotify_handle *inotify_init(const struct inotify_operations *ops) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 470 | { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 471 | struct inotify_handle *ih; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 472 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 473 | ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL); |
| 474 | if (unlikely(!ih)) |
| 475 | return ERR_PTR(-ENOMEM); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 476 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 477 | idr_init(&ih->idr); |
| 478 | INIT_LIST_HEAD(&ih->watches); |
| 479 | mutex_init(&ih->mutex); |
| 480 | ih->last_wd = 0; |
| 481 | ih->in_ops = ops; |
| 482 | atomic_set(&ih->count, 0); |
| 483 | get_inotify_handle(ih); |
| 484 | |
| 485 | return ih; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 486 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 487 | EXPORT_SYMBOL_GPL(inotify_init); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 488 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 489 | /** |
Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 490 | * inotify_init_watch - initialize an inotify watch |
| 491 | * @watch: watch to initialize |
| 492 | */ |
| 493 | void inotify_init_watch(struct inotify_watch *watch) |
| 494 | { |
| 495 | INIT_LIST_HEAD(&watch->h_list); |
| 496 | INIT_LIST_HEAD(&watch->i_list); |
| 497 | atomic_set(&watch->count, 0); |
| 498 | get_inotify_watch(watch); /* initial get */ |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(inotify_init_watch); |
| 501 | |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 502 | /* |
| 503 | * Watch removals suck violently. To kick the watch out we need (in this |
| 504 | * order) inode->inotify_mutex and ih->mutex. That's fine if we have |
| 505 | * a hold on inode; however, for all other cases we need to make damn sure |
| 506 | * we don't race with umount. We can *NOT* just grab a reference to a |
| 507 | * watch - inotify_unmount_inodes() will happily sail past it and we'll end |
| 508 | * with reference to inode potentially outliving its superblock. Ideally |
| 509 | * we just want to grab an active reference to superblock if we can; that |
| 510 | * will make sure we won't go into inotify_umount_inodes() until we are |
| 511 | * done. Cleanup is just deactivate_super(). However, that leaves a messy |
| 512 | * case - what if we *are* racing with umount() and active references to |
| 513 | * superblock can't be acquired anymore? We can bump ->s_count, grab |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 514 | * ->s_umount, which will wait until the superblock is shut down and the |
| 515 | * watch in question is pining for fjords. |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 516 | * |
| 517 | * And yes, this is far beyond mere "not very pretty"; so's the entire |
| 518 | * concept of inotify to start with. |
| 519 | */ |
| 520 | |
| 521 | /** |
| 522 | * pin_to_kill - pin the watch down for removal |
| 523 | * @ih: inotify handle |
| 524 | * @watch: watch to kill |
| 525 | * |
| 526 | * Called with ih->mutex held, drops it. Possible return values: |
| 527 | * 0 - nothing to do, it has died |
| 528 | * 1 - remove it, drop the reference and deactivate_super() |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 529 | */ |
| 530 | static int pin_to_kill(struct inotify_handle *ih, struct inotify_watch *watch) |
| 531 | { |
| 532 | struct super_block *sb = watch->inode->i_sb; |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 533 | |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 534 | if (atomic_inc_not_zero(&sb->s_active)) { |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 535 | get_inotify_watch(watch); |
| 536 | mutex_unlock(&ih->mutex); |
| 537 | return 1; /* the best outcome */ |
| 538 | } |
Al Viro | b20bd1a | 2010-03-22 08:53:19 -0400 | [diff] [blame] | 539 | spin_lock(&sb_lock); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 540 | sb->s_count++; |
| 541 | spin_unlock(&sb_lock); |
| 542 | mutex_unlock(&ih->mutex); /* can't grab ->s_umount under it */ |
| 543 | down_read(&sb->s_umount); |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 544 | /* fs is already shut down; the watch is dead */ |
| 545 | drop_super(sb); |
| 546 | return 0; |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 549 | static void unpin_and_kill(struct inotify_watch *watch) |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 550 | { |
| 551 | struct super_block *sb = watch->inode->i_sb; |
| 552 | put_inotify_watch(watch); |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 553 | deactivate_super(sb); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 556 | /** |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 557 | * inotify_destroy - clean up and destroy an inotify instance |
| 558 | * @ih: inotify handle |
| 559 | */ |
| 560 | void inotify_destroy(struct inotify_handle *ih) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 561 | { |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 562 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 563 | * Destroy all of the watches for this handle. Unfortunately, not very |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 564 | * pretty. We cannot do a simple iteration over the list, because we |
| 565 | * do not know the inode until we iterate to the watch. But we need to |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 566 | * hold inode->inotify_mutex before ih->mutex. The following works. |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 567 | * |
| 568 | * AV: it had to become even uglier to start working ;-/ |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 569 | */ |
| 570 | while (1) { |
| 571 | struct inotify_watch *watch; |
| 572 | struct list_head *watches; |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 573 | struct super_block *sb; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 574 | struct inode *inode; |
| 575 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 576 | mutex_lock(&ih->mutex); |
| 577 | watches = &ih->watches; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 578 | if (list_empty(watches)) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 579 | mutex_unlock(&ih->mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 580 | break; |
| 581 | } |
Pavel Emelianov | b5e6181 | 2007-05-08 00:30:19 -0700 | [diff] [blame] | 582 | watch = list_first_entry(watches, struct inotify_watch, h_list); |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 583 | sb = watch->inode->i_sb; |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 584 | if (!pin_to_kill(ih, watch)) |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 585 | continue; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 586 | |
| 587 | inode = watch->inode; |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 588 | mutex_lock(&inode->inotify_mutex); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 589 | mutex_lock(&ih->mutex); |
Amy Griffis | 66055a4 | 2006-05-20 15:00:06 -0700 | [diff] [blame] | 590 | |
| 591 | /* make sure we didn't race with another list removal */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 592 | if (likely(idr_find(&ih->idr, watch->wd))) { |
| 593 | remove_watch_no_event(watch, ih); |
| 594 | put_inotify_watch(watch); |
| 595 | } |
Amy Griffis | 66055a4 | 2006-05-20 15:00:06 -0700 | [diff] [blame] | 596 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 597 | mutex_unlock(&ih->mutex); |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 598 | mutex_unlock(&inode->inotify_mutex); |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 599 | unpin_and_kill(watch); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 600 | } |
| 601 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 602 | /* free this handle: the put matching the get in inotify_init() */ |
| 603 | put_inotify_handle(ih); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 604 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 605 | EXPORT_SYMBOL_GPL(inotify_destroy); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 606 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 607 | /** |
Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 608 | * inotify_find_watch - find an existing watch for an (ih,inode) pair |
| 609 | * @ih: inotify handle |
| 610 | * @inode: inode to watch |
| 611 | * @watchp: pointer to existing inotify_watch |
| 612 | * |
| 613 | * Caller must pin given inode (via nameidata). |
| 614 | */ |
| 615 | s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode, |
| 616 | struct inotify_watch **watchp) |
| 617 | { |
| 618 | struct inotify_watch *old; |
| 619 | int ret = -ENOENT; |
| 620 | |
| 621 | mutex_lock(&inode->inotify_mutex); |
| 622 | mutex_lock(&ih->mutex); |
| 623 | |
| 624 | old = inode_find_handle(inode, ih); |
| 625 | if (unlikely(old)) { |
| 626 | get_inotify_watch(old); /* caller must put watch */ |
| 627 | *watchp = old; |
| 628 | ret = old->wd; |
| 629 | } |
| 630 | |
| 631 | mutex_unlock(&ih->mutex); |
| 632 | mutex_unlock(&inode->inotify_mutex); |
| 633 | |
| 634 | return ret; |
| 635 | } |
| 636 | EXPORT_SYMBOL_GPL(inotify_find_watch); |
| 637 | |
| 638 | /** |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 639 | * inotify_find_update_watch - find and update the mask of an existing watch |
| 640 | * @ih: inotify handle |
| 641 | * @inode: inode's watch to update |
| 642 | * @mask: mask of events to watch |
| 643 | * |
| 644 | * Caller must pin given inode (via nameidata). |
| 645 | */ |
| 646 | s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode, |
| 647 | u32 mask) |
| 648 | { |
| 649 | struct inotify_watch *old; |
| 650 | int mask_add = 0; |
| 651 | int ret; |
| 652 | |
| 653 | if (mask & IN_MASK_ADD) |
| 654 | mask_add = 1; |
| 655 | |
| 656 | /* don't allow invalid bits: we don't want flags set */ |
| 657 | mask &= IN_ALL_EVENTS | IN_ONESHOT; |
| 658 | if (unlikely(!mask)) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | mutex_lock(&inode->inotify_mutex); |
| 662 | mutex_lock(&ih->mutex); |
| 663 | |
| 664 | /* |
| 665 | * Handle the case of re-adding a watch on an (inode,ih) pair that we |
| 666 | * are already watching. We just update the mask and return its wd. |
| 667 | */ |
| 668 | old = inode_find_handle(inode, ih); |
| 669 | if (unlikely(!old)) { |
| 670 | ret = -ENOENT; |
| 671 | goto out; |
| 672 | } |
| 673 | |
| 674 | if (mask_add) |
| 675 | old->mask |= mask; |
| 676 | else |
| 677 | old->mask = mask; |
| 678 | ret = old->wd; |
| 679 | out: |
| 680 | mutex_unlock(&ih->mutex); |
| 681 | mutex_unlock(&inode->inotify_mutex); |
| 682 | return ret; |
| 683 | } |
| 684 | EXPORT_SYMBOL_GPL(inotify_find_update_watch); |
| 685 | |
| 686 | /** |
| 687 | * inotify_add_watch - add a watch to an inotify instance |
| 688 | * @ih: inotify handle |
| 689 | * @watch: caller allocated watch structure |
| 690 | * @inode: inode to watch |
| 691 | * @mask: mask of events to watch |
| 692 | * |
| 693 | * Caller must pin given inode (via nameidata). |
| 694 | * Caller must ensure it only calls inotify_add_watch() once per watch. |
| 695 | * Calls inotify_handle_get_wd() so may sleep. |
| 696 | */ |
| 697 | s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch, |
| 698 | struct inode *inode, u32 mask) |
| 699 | { |
| 700 | int ret = 0; |
Nick Piggin | d599e36 | 2008-02-06 01:37:28 -0800 | [diff] [blame] | 701 | int newly_watched; |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 702 | |
| 703 | /* don't allow invalid bits: we don't want flags set */ |
| 704 | mask &= IN_ALL_EVENTS | IN_ONESHOT; |
| 705 | if (unlikely(!mask)) |
| 706 | return -EINVAL; |
| 707 | watch->mask = mask; |
| 708 | |
| 709 | mutex_lock(&inode->inotify_mutex); |
| 710 | mutex_lock(&ih->mutex); |
| 711 | |
| 712 | /* Initialize a new watch */ |
| 713 | ret = inotify_handle_get_wd(ih, watch); |
| 714 | if (unlikely(ret)) |
| 715 | goto out; |
| 716 | ret = watch->wd; |
| 717 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 718 | /* save a reference to handle and bump the count to make it official */ |
| 719 | get_inotify_handle(ih); |
| 720 | watch->ih = ih; |
| 721 | |
| 722 | /* |
| 723 | * Save a reference to the inode and bump the ref count to make it |
| 724 | * official. We hold a reference to nameidata, which makes this safe. |
| 725 | */ |
| 726 | watch->inode = igrab(inode); |
| 727 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 728 | /* Add the watch to the handle's and the inode's list */ |
Nick Piggin | d599e36 | 2008-02-06 01:37:28 -0800 | [diff] [blame] | 729 | newly_watched = !inotify_inode_watched(inode); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 730 | list_add(&watch->h_list, &ih->watches); |
| 731 | list_add(&watch->i_list, &inode->inotify_watches); |
Nick Piggin | d599e36 | 2008-02-06 01:37:28 -0800 | [diff] [blame] | 732 | /* |
| 733 | * Set child flags _after_ adding the watch, so there is no race |
| 734 | * windows where newly instantiated children could miss their parent's |
| 735 | * watched flag. |
| 736 | */ |
| 737 | if (newly_watched) |
| 738 | set_dentry_child_flags(inode, 1); |
| 739 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 740 | out: |
| 741 | mutex_unlock(&ih->mutex); |
| 742 | mutex_unlock(&inode->inotify_mutex); |
| 743 | return ret; |
| 744 | } |
| 745 | EXPORT_SYMBOL_GPL(inotify_add_watch); |
| 746 | |
| 747 | /** |
Al Viro | b9efe8a | 2007-06-07 12:21:44 -0400 | [diff] [blame] | 748 | * inotify_clone_watch - put the watch next to existing one |
| 749 | * @old: already installed watch |
| 750 | * @new: new watch |
| 751 | * |
| 752 | * Caller must hold the inotify_mutex of inode we are dealing with; |
| 753 | * it is expected to remove the old watch before unlocking the inode. |
| 754 | */ |
| 755 | s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new) |
| 756 | { |
| 757 | struct inotify_handle *ih = old->ih; |
| 758 | int ret = 0; |
| 759 | |
| 760 | new->mask = old->mask; |
| 761 | new->ih = ih; |
| 762 | |
| 763 | mutex_lock(&ih->mutex); |
| 764 | |
| 765 | /* Initialize a new watch */ |
| 766 | ret = inotify_handle_get_wd(ih, new); |
| 767 | if (unlikely(ret)) |
| 768 | goto out; |
| 769 | ret = new->wd; |
| 770 | |
| 771 | get_inotify_handle(ih); |
| 772 | |
| 773 | new->inode = igrab(old->inode); |
| 774 | |
| 775 | list_add(&new->h_list, &ih->watches); |
| 776 | list_add(&new->i_list, &old->inode->inotify_watches); |
| 777 | out: |
| 778 | mutex_unlock(&ih->mutex); |
| 779 | return ret; |
| 780 | } |
| 781 | |
Al Viro | 455434d | 2007-06-07 12:22:59 -0400 | [diff] [blame] | 782 | void inotify_evict_watch(struct inotify_watch *watch) |
| 783 | { |
| 784 | get_inotify_watch(watch); |
| 785 | mutex_lock(&watch->ih->mutex); |
| 786 | inotify_remove_watch_locked(watch->ih, watch); |
| 787 | mutex_unlock(&watch->ih->mutex); |
| 788 | } |
| 789 | |
Al Viro | b9efe8a | 2007-06-07 12:21:44 -0400 | [diff] [blame] | 790 | /** |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 791 | * inotify_rm_wd - remove a watch from an inotify instance |
| 792 | * @ih: inotify handle |
| 793 | * @wd: watch descriptor to remove |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 794 | * |
| 795 | * Can sleep. |
| 796 | */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 797 | int inotify_rm_wd(struct inotify_handle *ih, u32 wd) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 798 | { |
| 799 | struct inotify_watch *watch; |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 800 | struct super_block *sb; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 801 | struct inode *inode; |
| 802 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 803 | mutex_lock(&ih->mutex); |
| 804 | watch = idr_find(&ih->idr, wd); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 805 | if (unlikely(!watch)) { |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 806 | mutex_unlock(&ih->mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 807 | return -EINVAL; |
| 808 | } |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 809 | sb = watch->inode->i_sb; |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 810 | if (!pin_to_kill(ih, watch)) |
Al Viro | 8f7b0ba | 2008-11-15 01:15:43 +0000 | [diff] [blame] | 811 | return 0; |
| 812 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 813 | inode = watch->inode; |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 814 | |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 815 | mutex_lock(&inode->inotify_mutex); |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 816 | mutex_lock(&ih->mutex); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 817 | |
| 818 | /* make sure that we did not race */ |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 819 | if (likely(idr_find(&ih->idr, wd) == watch)) |
Amy Griffis | 3ca1006 | 2006-06-01 13:11:05 -0700 | [diff] [blame] | 820 | inotify_remove_watch_locked(ih, watch); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 821 | |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 822 | mutex_unlock(&ih->mutex); |
Ingo Molnar | d4f9af9 | 2006-03-23 03:00:30 -0800 | [diff] [blame] | 823 | mutex_unlock(&inode->inotify_mutex); |
Al Viro | 1712ac8 | 2010-03-22 15:22:31 -0400 | [diff] [blame] | 824 | unpin_and_kill(watch); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 825 | |
| 826 | return 0; |
| 827 | } |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 828 | EXPORT_SYMBOL_GPL(inotify_rm_wd); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 829 | |
Amy Griffis | a9dc971 | 2006-06-01 13:11:03 -0700 | [diff] [blame] | 830 | /** |
| 831 | * inotify_rm_watch - remove a watch from an inotify instance |
| 832 | * @ih: inotify handle |
| 833 | * @watch: watch to remove |
| 834 | * |
| 835 | * Can sleep. |
| 836 | */ |
| 837 | int inotify_rm_watch(struct inotify_handle *ih, |
| 838 | struct inotify_watch *watch) |
| 839 | { |
| 840 | return inotify_rm_wd(ih, watch->wd); |
| 841 | } |
| 842 | EXPORT_SYMBOL_GPL(inotify_rm_watch); |
| 843 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 844 | /* |
Amy Griffis | 2d9048e | 2006-06-01 13:10:59 -0700 | [diff] [blame] | 845 | * inotify_setup - core initialization function |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 846 | */ |
Robert Love | b680716 | 2005-07-25 15:07:13 -0400 | [diff] [blame] | 847 | static int __init inotify_setup(void) |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 848 | { |
Eric Paris | 9058652 | 2009-05-21 17:01:20 -0400 | [diff] [blame] | 849 | BUILD_BUG_ON(IN_ACCESS != FS_ACCESS); |
| 850 | BUILD_BUG_ON(IN_MODIFY != FS_MODIFY); |
| 851 | BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB); |
| 852 | BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE); |
| 853 | BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE); |
| 854 | BUILD_BUG_ON(IN_OPEN != FS_OPEN); |
| 855 | BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM); |
| 856 | BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO); |
| 857 | BUILD_BUG_ON(IN_CREATE != FS_CREATE); |
| 858 | BUILD_BUG_ON(IN_DELETE != FS_DELETE); |
| 859 | BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF); |
| 860 | BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF); |
| 861 | BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW); |
| 862 | |
| 863 | BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT); |
| 864 | BUILD_BUG_ON(IN_ISDIR != FS_IN_ISDIR); |
| 865 | BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED); |
| 866 | BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT); |
| 867 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 868 | atomic_set(&inotify_cookie, 0); |
| 869 | |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 870 | return 0; |
| 871 | } |
| 872 | |
Robert Love | b680716 | 2005-07-25 15:07:13 -0400 | [diff] [blame] | 873 | module_init(inotify_setup); |