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