Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Red Hat, Inc., Eric Paris <eparis@redhat.com> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * fsnotify inode mark locking/lifetime/and refcnting |
| 21 | * |
| 22 | * REFCNT: |
Lino Sanfilippo | 9756b91 | 2013-07-08 15:59:46 -0700 | [diff] [blame] | 23 | * The group->recnt and mark->refcnt tell how many "things" in the kernel |
| 24 | * currently are referencing the objects. Both kind of objects typically will |
| 25 | * live inside the kernel with a refcnt of 2, one for its creation and one for |
| 26 | * the reference a group and a mark hold to each other. |
| 27 | * If you are holding the appropriate locks, you can take a reference and the |
| 28 | * object itself is guaranteed to survive until the reference is dropped. |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 29 | * |
| 30 | * LOCKING: |
Lino Sanfilippo | 9756b91 | 2013-07-08 15:59:46 -0700 | [diff] [blame] | 31 | * There are 3 locks involved with fsnotify inode marks and they MUST be taken |
| 32 | * in order as follows: |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 33 | * |
Lino Sanfilippo | 9756b91 | 2013-07-08 15:59:46 -0700 | [diff] [blame] | 34 | * group->mark_mutex |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 35 | * mark->lock |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 36 | * inode->i_lock |
| 37 | * |
Lino Sanfilippo | 9756b91 | 2013-07-08 15:59:46 -0700 | [diff] [blame] | 38 | * group->mark_mutex protects the marks_list anchored inside a given group and |
| 39 | * each mark is hooked via the g_list. It also protects the groups private |
| 40 | * data (i.e group limits). |
| 41 | |
| 42 | * mark->lock protects the marks attributes like its masks and flags. |
| 43 | * Furthermore it protects the access to a reference of the group that the mark |
| 44 | * is assigned to as well as the access to a reference of the inode/vfsmount |
| 45 | * that is being watched by the mark. |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 46 | * |
| 47 | * inode->i_lock protects the i_fsnotify_marks list anchored inside a |
| 48 | * given inode and each mark is hooked via the i_list. (and sorta the |
| 49 | * free_i_list) |
| 50 | * |
| 51 | * |
| 52 | * LIFETIME: |
| 53 | * Inode marks survive between when they are added to an inode and when their |
Jan Kara | c1f3307 | 2016-12-16 10:53:32 +0100 | [diff] [blame] | 54 | * refcnt==0. Marks are also protected by fsnotify_mark_srcu. |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 55 | * |
| 56 | * The inode mark can be cleared for a number of different reasons including: |
| 57 | * - The inode is unlinked for the last time. (fsnotify_inode_remove) |
| 58 | * - The inode is being evicted from cache. (fsnotify_inode_delete) |
| 59 | * - The fs the inode is on is unmounted. (fsnotify_inode_delete/fsnotify_unmount_inodes) |
| 60 | * - Something explicitly requests that it be removed. (fsnotify_destroy_mark) |
| 61 | * - The fsnotify_group associated with the mark is going away and all such marks |
| 62 | * need to be cleaned up. (fsnotify_clear_marks_by_group) |
| 63 | * |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 64 | * This has the very interesting property of being able to run concurrently with |
| 65 | * any (or all) other directions. |
| 66 | */ |
| 67 | |
| 68 | #include <linux/fs.h> |
| 69 | #include <linux/init.h> |
| 70 | #include <linux/kernel.h> |
Eric Paris | 75c1be4 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 71 | #include <linux/kthread.h> |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 72 | #include <linux/module.h> |
| 73 | #include <linux/mutex.h> |
| 74 | #include <linux/slab.h> |
| 75 | #include <linux/spinlock.h> |
Eric Paris | 75c1be4 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 76 | #include <linux/srcu.h> |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 77 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 78 | #include <linux/atomic.h> |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 79 | |
| 80 | #include <linux/fsnotify_backend.h> |
| 81 | #include "fsnotify.h" |
| 82 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 83 | #define FSNOTIFY_REAPER_DELAY (1) /* 1 jiffy */ |
| 84 | |
Eric Paris | 75c1be4 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 85 | struct srcu_struct fsnotify_mark_srcu; |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 86 | struct kmem_cache *fsnotify_mark_connector_cachep; |
| 87 | |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 88 | static DEFINE_SPINLOCK(destroy_lock); |
| 89 | static LIST_HEAD(destroy_list); |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 90 | |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 91 | static void fsnotify_mark_destroy_workfn(struct work_struct *work); |
| 92 | static DECLARE_DELAYED_WORK(reaper_work, fsnotify_mark_destroy_workfn); |
Eric Paris | 75c1be4 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 93 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 94 | void fsnotify_get_mark(struct fsnotify_mark *mark) |
| 95 | { |
| 96 | atomic_inc(&mark->refcnt); |
| 97 | } |
| 98 | |
| 99 | void fsnotify_put_mark(struct fsnotify_mark *mark) |
| 100 | { |
Lino Sanfilippo | 23e964c | 2011-06-14 17:29:47 +0200 | [diff] [blame] | 101 | if (atomic_dec_and_test(&mark->refcnt)) { |
| 102 | if (mark->group) |
| 103 | fsnotify_put_group(mark->group); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 104 | mark->free_mark(mark); |
Lino Sanfilippo | 23e964c | 2011-06-14 17:29:47 +0200 | [diff] [blame] | 105 | } |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Jan Kara | a242677 | 2017-03-15 09:16:27 +0100 | [diff] [blame] | 108 | static void __fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 109 | { |
| 110 | u32 new_mask = 0; |
| 111 | struct fsnotify_mark *mark; |
| 112 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 113 | hlist_for_each_entry(mark, &conn->list, obj_list) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 114 | new_mask |= mark->mask; |
Jan Kara | a242677 | 2017-03-15 09:16:27 +0100 | [diff] [blame] | 115 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) |
| 116 | conn->inode->i_fsnotify_mask = new_mask; |
| 117 | else if (conn->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) |
| 118 | real_mount(conn->mnt)->mnt_fsnotify_mask = new_mask; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Calculate mask of events for a list of marks. The caller must make sure |
| 123 | * connector cannot disappear under us (usually by holding a mark->lock or |
| 124 | * mark->group->mark_mutex for a mark on this list). |
| 125 | */ |
| 126 | void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) |
| 127 | { |
| 128 | if (!conn) |
| 129 | return; |
| 130 | |
| 131 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) |
| 132 | spin_lock(&conn->inode->i_lock); |
| 133 | else |
| 134 | spin_lock(&conn->mnt->mnt_root->d_lock); |
| 135 | __fsnotify_recalc_mask(conn); |
| 136 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) { |
| 137 | spin_unlock(&conn->inode->i_lock); |
| 138 | __fsnotify_update_child_dentry_flags(conn->inode); |
| 139 | } else { |
| 140 | spin_unlock(&conn->mnt->mnt_root->d_lock); |
| 141 | } |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Jan Kara | 8212a60 | 2017-03-15 09:48:11 +0100 | [diff] [blame^] | 144 | static struct inode *fsnotify_detach_from_object(struct fsnotify_mark *mark) |
| 145 | { |
| 146 | struct fsnotify_mark_connector *conn; |
| 147 | struct inode *inode = NULL; |
| 148 | spinlock_t *lock; |
| 149 | |
| 150 | conn = mark->connector; |
| 151 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) |
| 152 | lock = &conn->inode->i_lock; |
| 153 | else |
| 154 | lock = &conn->mnt->mnt_root->d_lock; |
| 155 | spin_lock(lock); |
| 156 | hlist_del_init_rcu(&mark->obj_list); |
| 157 | if (hlist_empty(&conn->list)) { |
| 158 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) |
| 159 | inode = conn->inode; |
| 160 | } |
| 161 | mark->connector = NULL; |
| 162 | spin_unlock(lock); |
| 163 | fsnotify_recalc_mask(conn); |
| 164 | |
| 165 | return inode; |
| 166 | } |
| 167 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 168 | /* |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 169 | * Remove mark from inode / vfsmount list, group list, drop inode reference |
| 170 | * if we got one. |
| 171 | * |
| 172 | * Must be called with group->mark_mutex held. |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 173 | */ |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 174 | void fsnotify_detach_mark(struct fsnotify_mark *mark) |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 175 | { |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 176 | struct inode *inode = NULL; |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 177 | struct fsnotify_group *group = mark->group; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 178 | |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 179 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
| 180 | |
Lino Sanfilippo | 104d06f | 2011-06-14 17:29:48 +0200 | [diff] [blame] | 181 | spin_lock(&mark->lock); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 182 | |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 183 | /* something else already called this function on this mark */ |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 184 | if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 185 | spin_unlock(&mark->lock); |
Lino Sanfilippo | e2a2994 | 2011-06-14 17:29:51 +0200 | [diff] [blame] | 186 | return; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 189 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 190 | |
Jan Kara | 8212a60 | 2017-03-15 09:48:11 +0100 | [diff] [blame^] | 191 | inode = fsnotify_detach_from_object(mark); |
| 192 | |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 193 | /* |
| 194 | * Note that we didn't update flags telling whether inode cares about |
| 195 | * what's happening with children. We update these flags from |
| 196 | * __fsnotify_parent() lazily when next event happens on one of our |
| 197 | * children. |
| 198 | */ |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 199 | |
| 200 | list_del_init(&mark->g_list); |
Linus Torvalds | d725e66 | 2015-07-21 16:06:53 -0700 | [diff] [blame] | 201 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 202 | spin_unlock(&mark->lock); |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 203 | |
Jan Kara | e911d8a | 2017-03-14 14:48:00 +0100 | [diff] [blame] | 204 | if (inode) |
Lino Sanfilippo | 6960b0d | 2011-08-12 01:13:31 +0200 | [diff] [blame] | 205 | iput(inode); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 206 | |
| 207 | atomic_dec(&group->num_marks); |
| 208 | } |
| 209 | |
| 210 | /* |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 211 | * Prepare mark for freeing and add it to the list of marks prepared for |
| 212 | * freeing. The actual freeing must happen after SRCU period ends and the |
| 213 | * caller is responsible for this. |
| 214 | * |
| 215 | * The function returns true if the mark was added to the list of marks for |
| 216 | * freeing. The function returns false if someone else has already called |
| 217 | * __fsnotify_free_mark() for the mark. |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 218 | */ |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 219 | static bool __fsnotify_free_mark(struct fsnotify_mark *mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 220 | { |
| 221 | struct fsnotify_group *group = mark->group; |
| 222 | |
| 223 | spin_lock(&mark->lock); |
| 224 | /* something else already called this function on this mark */ |
| 225 | if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { |
| 226 | spin_unlock(&mark->lock); |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 227 | return false; |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 228 | } |
| 229 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; |
| 230 | spin_unlock(&mark->lock); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 231 | |
Linus Torvalds | d725e66 | 2015-07-21 16:06:53 -0700 | [diff] [blame] | 232 | /* |
| 233 | * Some groups like to know that marks are being freed. This is a |
| 234 | * callback to the group function to let it know that this mark |
| 235 | * is being freed. |
| 236 | */ |
| 237 | if (group->ops->freeing_mark) |
| 238 | group->ops->freeing_mark(mark, group); |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 239 | |
| 240 | spin_lock(&destroy_lock); |
| 241 | list_add(&mark->g_list, &destroy_list); |
| 242 | spin_unlock(&destroy_lock); |
| 243 | |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Free fsnotify mark. The freeing is actually happening from a workqueue which |
| 249 | * first waits for srcu period end. Caller must have a reference to the mark |
| 250 | * or be protected by fsnotify_mark_srcu. |
| 251 | */ |
| 252 | void fsnotify_free_mark(struct fsnotify_mark *mark) |
| 253 | { |
| 254 | if (__fsnotify_free_mark(mark)) { |
| 255 | queue_delayed_work(system_unbound_wq, &reaper_work, |
| 256 | FSNOTIFY_REAPER_DELAY); |
| 257 | } |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void fsnotify_destroy_mark(struct fsnotify_mark *mark, |
| 261 | struct fsnotify_group *group) |
| 262 | { |
Lino Sanfilippo | 6960b0d | 2011-08-12 01:13:31 +0200 | [diff] [blame] | 263 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 264 | fsnotify_detach_mark(mark); |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 265 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 266 | fsnotify_free_mark(mark); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 269 | void fsnotify_connector_free(struct fsnotify_mark_connector **connp) |
| 270 | { |
| 271 | if (*connp) { |
| 272 | kmem_cache_free(fsnotify_mark_connector_cachep, *connp); |
| 273 | *connp = NULL; |
| 274 | } |
| 275 | } |
| 276 | |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 277 | void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) |
| 278 | { |
| 279 | assert_spin_locked(&mark->lock); |
| 280 | |
| 281 | mark->mask = mask; |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Eric Paris | 33af5e3 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 284 | void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) |
| 285 | { |
| 286 | assert_spin_locked(&mark->lock); |
| 287 | |
| 288 | mark->ignored_mask = mask; |
| 289 | } |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 290 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 291 | /* |
Jan Kara | 8edc6e1 | 2014-11-13 15:19:33 -0800 | [diff] [blame] | 292 | * Sorting function for lists of fsnotify marks. |
| 293 | * |
| 294 | * Fanotify supports different notification classes (reflected as priority of |
| 295 | * notification group). Events shall be passed to notification groups in |
| 296 | * decreasing priority order. To achieve this marks in notification lists for |
| 297 | * inodes and vfsmounts are sorted so that priorities of corresponding groups |
| 298 | * are descending. |
| 299 | * |
| 300 | * Furthermore correct handling of the ignore mask requires processing inode |
| 301 | * and vfsmount marks of each group together. Using the group address as |
| 302 | * further sort criterion provides a unique sorting order and thus we can |
| 303 | * merge inode and vfsmount lists of marks in linear time and find groups |
| 304 | * present in both lists. |
| 305 | * |
| 306 | * A return value of 1 signifies that b has priority over a. |
| 307 | * A return value of 0 signifies that the two marks have to be handled together. |
| 308 | * A return value of -1 signifies that a has priority over b. |
| 309 | */ |
| 310 | int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) |
| 311 | { |
| 312 | if (a == b) |
| 313 | return 0; |
| 314 | if (!a) |
| 315 | return 1; |
| 316 | if (!b) |
| 317 | return -1; |
| 318 | if (a->priority < b->priority) |
| 319 | return 1; |
| 320 | if (a->priority > b->priority) |
| 321 | return -1; |
| 322 | if (a < b) |
| 323 | return 1; |
| 324 | return -1; |
| 325 | } |
| 326 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 327 | static int fsnotify_attach_connector_to_object( |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 328 | struct fsnotify_mark_connector **connp, |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 329 | spinlock_t *lock, |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 330 | struct inode *inode, |
| 331 | struct vfsmount *mnt) |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 332 | { |
| 333 | struct fsnotify_mark_connector *conn; |
| 334 | |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 335 | conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_KERNEL); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 336 | if (!conn) |
| 337 | return -ENOMEM; |
| 338 | INIT_HLIST_HEAD(&conn->list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 339 | if (inode) { |
| 340 | conn->flags = FSNOTIFY_OBJ_TYPE_INODE; |
| 341 | conn->inode = inode; |
| 342 | } else { |
| 343 | conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT; |
| 344 | conn->mnt = mnt; |
| 345 | } |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 346 | /* |
| 347 | * Make sure 'conn' initialization is visible. Matches |
| 348 | * lockless_dereference() in fsnotify(). |
| 349 | */ |
| 350 | smp_wmb(); |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 351 | spin_lock(lock); |
| 352 | if (!*connp) |
| 353 | *connp = conn; |
| 354 | else |
| 355 | kmem_cache_free(fsnotify_mark_connector_cachep, conn); |
| 356 | spin_unlock(lock); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Add mark into proper place in given list of marks. These marks may be used |
| 363 | * for the fsnotify backend to determine which event types should be delivered |
| 364 | * to which group and for which inodes. These marks are ordered according to |
| 365 | * priority, highest number first, and then by the group's location in memory. |
| 366 | */ |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 367 | static int fsnotify_add_mark_list(struct fsnotify_mark *mark, |
| 368 | struct inode *inode, struct vfsmount *mnt, |
| 369 | int allow_dups) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 370 | { |
| 371 | struct fsnotify_mark *lmark, *last = NULL; |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 372 | struct fsnotify_mark_connector *conn; |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 373 | struct fsnotify_mark_connector **connp; |
| 374 | spinlock_t *lock; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 375 | int cmp; |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 376 | int err = 0; |
| 377 | |
| 378 | if (WARN_ON(!inode && !mnt)) |
| 379 | return -EINVAL; |
| 380 | if (inode) { |
| 381 | connp = &inode->i_fsnotify_marks; |
| 382 | lock = &inode->i_lock; |
| 383 | } else { |
| 384 | connp = &real_mount(mnt)->mnt_fsnotify_marks; |
| 385 | lock = &mnt->mnt_root->d_lock; |
| 386 | } |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 387 | |
| 388 | if (!*connp) { |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 389 | err = fsnotify_attach_connector_to_object(connp, lock, |
| 390 | inode, mnt); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 391 | if (err) |
| 392 | return err; |
| 393 | } |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 394 | spin_lock(&mark->lock); |
| 395 | spin_lock(lock); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 396 | conn = *connp; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 397 | |
| 398 | /* is mark the first mark? */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 399 | if (hlist_empty(&conn->list)) { |
| 400 | hlist_add_head_rcu(&mark->obj_list, &conn->list); |
Jan Kara | e911d8a | 2017-03-14 14:48:00 +0100 | [diff] [blame] | 401 | if (inode) |
| 402 | __iget(inode); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 403 | goto added; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* should mark be in the middle of the current list? */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 407 | hlist_for_each_entry(lmark, &conn->list, obj_list) { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 408 | last = lmark; |
| 409 | |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 410 | if ((lmark->group == mark->group) && !allow_dups) { |
| 411 | err = -EEXIST; |
| 412 | goto out_err; |
| 413 | } |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 414 | |
| 415 | cmp = fsnotify_compare_groups(lmark->group, mark->group); |
| 416 | if (cmp >= 0) { |
| 417 | hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 418 | goto added; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
| 422 | BUG_ON(last == NULL); |
| 423 | /* mark should be the last entry. last is the current last entry */ |
| 424 | hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 425 | added: |
| 426 | mark->connector = conn; |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 427 | out_err: |
| 428 | spin_unlock(lock); |
| 429 | spin_unlock(&mark->lock); |
| 430 | return err; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 431 | } |
| 432 | |
Jan Kara | 8edc6e1 | 2014-11-13 15:19:33 -0800 | [diff] [blame] | 433 | /* |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 434 | * Attach an initialized mark to a given group and fs object. |
| 435 | * These marks may be used for the fsnotify backend to determine which |
| 436 | * event types should be delivered to which group. |
| 437 | */ |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 438 | int fsnotify_add_mark_locked(struct fsnotify_mark *mark, |
| 439 | struct fsnotify_group *group, struct inode *inode, |
| 440 | struct vfsmount *mnt, int allow_dups) |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 441 | { |
| 442 | int ret = 0; |
| 443 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 444 | BUG_ON(inode && mnt); |
| 445 | BUG_ON(!inode && !mnt); |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 446 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 447 | |
| 448 | /* |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 449 | * LOCKING ORDER!!!! |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 450 | * group->mark_mutex |
Lino Sanfilippo | 104d06f | 2011-06-14 17:29:48 +0200 | [diff] [blame] | 451 | * mark->lock |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 452 | * inode->i_lock |
| 453 | */ |
Lino Sanfilippo | 104d06f | 2011-06-14 17:29:48 +0200 | [diff] [blame] | 454 | spin_lock(&mark->lock); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 455 | mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 456 | |
Lino Sanfilippo | 23e964c | 2011-06-14 17:29:47 +0200 | [diff] [blame] | 457 | fsnotify_get_group(group); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 458 | mark->group = group; |
| 459 | list_add(&mark->g_list, &group->marks_list); |
| 460 | atomic_inc(&group->num_marks); |
| 461 | fsnotify_get_mark(mark); /* for i_list and g_list */ |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 462 | spin_unlock(&mark->lock); |
| 463 | |
Jan Kara | 755b5bc | 2017-03-14 16:11:23 +0100 | [diff] [blame] | 464 | ret = fsnotify_add_mark_list(mark, inode, mnt, allow_dups); |
| 465 | if (ret) |
| 466 | goto err; |
| 467 | |
Jan Kara | a242677 | 2017-03-15 09:16:27 +0100 | [diff] [blame] | 468 | if (mark->mask) |
| 469 | fsnotify_recalc_mask(mark->connector); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 470 | |
| 471 | return ret; |
| 472 | err: |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 473 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 474 | list_del_init(&mark->g_list); |
Lino Sanfilippo | 23e964c | 2011-06-14 17:29:47 +0200 | [diff] [blame] | 475 | fsnotify_put_group(group); |
Eric Paris | 75c1be4 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 476 | mark->group = NULL; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 477 | atomic_dec(&group->num_marks); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 478 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 479 | spin_unlock(&mark->lock); |
| 480 | |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 481 | spin_lock(&destroy_lock); |
| 482 | list_add(&mark->g_list, &destroy_list); |
| 483 | spin_unlock(&destroy_lock); |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 484 | queue_delayed_work(system_unbound_wq, &reaper_work, |
| 485 | FSNOTIFY_REAPER_DELAY); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 486 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 487 | return ret; |
| 488 | } |
| 489 | |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 490 | int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, |
| 491 | struct inode *inode, struct vfsmount *mnt, int allow_dups) |
| 492 | { |
| 493 | int ret; |
| 494 | mutex_lock(&group->mark_mutex); |
| 495 | ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); |
| 496 | mutex_unlock(&group->mark_mutex); |
| 497 | return ret; |
| 498 | } |
| 499 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 500 | /* |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 501 | * Given a list of marks, find the mark associated with given group. If found |
| 502 | * take a reference to that mark and return it, else return NULL. |
| 503 | */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 504 | struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn, |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 505 | struct fsnotify_group *group) |
| 506 | { |
| 507 | struct fsnotify_mark *mark; |
Jan Kara | f06fd98 | 2017-03-15 09:33:58 +0100 | [diff] [blame] | 508 | spinlock_t *lock; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 509 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 510 | if (!conn) |
| 511 | return NULL; |
| 512 | |
Jan Kara | f06fd98 | 2017-03-15 09:33:58 +0100 | [diff] [blame] | 513 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) |
| 514 | lock = &conn->inode->i_lock; |
| 515 | else |
| 516 | lock = &conn->mnt->mnt_root->d_lock; |
| 517 | spin_lock(lock); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 518 | hlist_for_each_entry(mark, &conn->list, obj_list) { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 519 | if (mark->group == group) { |
| 520 | fsnotify_get_mark(mark); |
Jan Kara | f06fd98 | 2017-03-15 09:33:58 +0100 | [diff] [blame] | 521 | spin_unlock(lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 522 | return mark; |
| 523 | } |
| 524 | } |
Jan Kara | f06fd98 | 2017-03-15 09:33:58 +0100 | [diff] [blame] | 525 | spin_unlock(lock); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | /* |
Linus Torvalds | d725e66 | 2015-07-21 16:06:53 -0700 | [diff] [blame] | 530 | * clear any marks in a group in which mark->flags & flags is true |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 531 | */ |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 532 | void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, |
| 533 | unsigned int flags) |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 534 | { |
| 535 | struct fsnotify_mark *lmark, *mark; |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 536 | LIST_HEAD(to_free); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 537 | |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 538 | /* |
| 539 | * We have to be really careful here. Anytime we drop mark_mutex, e.g. |
| 540 | * fsnotify_clear_marks_by_inode() can come and free marks. Even in our |
| 541 | * to_free list so we have to use mark_mutex even when accessing that |
| 542 | * list. And freeing mark requires us to drop mark_mutex. So we can |
| 543 | * reliably free only the first mark in the list. That's why we first |
| 544 | * move marks to free to to_free list in one go and then free marks in |
| 545 | * to_free list one by one. |
| 546 | */ |
Lino Sanfilippo | 6960b0d | 2011-08-12 01:13:31 +0200 | [diff] [blame] | 547 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 548 | list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame] | 549 | if (mark->connector->flags & flags) |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 550 | list_move(&mark->g_list, &to_free); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 551 | } |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 552 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 553 | |
| 554 | while (1) { |
| 555 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
| 556 | if (list_empty(&to_free)) { |
| 557 | mutex_unlock(&group->mark_mutex); |
| 558 | break; |
| 559 | } |
| 560 | mark = list_first_entry(&to_free, struct fsnotify_mark, g_list); |
| 561 | fsnotify_get_mark(mark); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 562 | fsnotify_detach_mark(mark); |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 563 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 564 | fsnotify_free_mark(mark); |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 565 | fsnotify_put_mark(mark); |
| 566 | } |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 567 | } |
| 568 | |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 569 | /* |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 570 | * Given a group, prepare for freeing all the marks associated with that group. |
| 571 | * The marks are attached to the list of marks prepared for destruction, the |
| 572 | * caller is responsible for freeing marks in that list after SRCU period has |
| 573 | * ended. |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 574 | */ |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 575 | void fsnotify_detach_group_marks(struct fsnotify_group *group) |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 576 | { |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 577 | struct fsnotify_mark *mark; |
| 578 | |
| 579 | while (1) { |
| 580 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
| 581 | if (list_empty(&group->marks_list)) { |
| 582 | mutex_unlock(&group->mark_mutex); |
| 583 | break; |
| 584 | } |
| 585 | mark = list_first_entry(&group->marks_list, |
| 586 | struct fsnotify_mark, g_list); |
| 587 | fsnotify_get_mark(mark); |
| 588 | fsnotify_detach_mark(mark); |
| 589 | mutex_unlock(&group->mark_mutex); |
| 590 | __fsnotify_free_mark(mark); |
| 591 | fsnotify_put_mark(mark); |
| 592 | } |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 593 | } |
| 594 | |
Jan Kara | a03e2e4 | 2017-03-15 09:41:15 +0100 | [diff] [blame] | 595 | void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn) |
Jan Kara | 0810b4f | 2017-02-01 09:23:48 +0100 | [diff] [blame] | 596 | { |
| 597 | struct fsnotify_mark *mark; |
Jan Kara | a03e2e4 | 2017-03-15 09:41:15 +0100 | [diff] [blame] | 598 | spinlock_t *lock; |
Jan Kara | 0810b4f | 2017-02-01 09:23:48 +0100 | [diff] [blame] | 599 | |
| 600 | if (!conn) |
| 601 | return; |
| 602 | |
Jan Kara | a03e2e4 | 2017-03-15 09:41:15 +0100 | [diff] [blame] | 603 | if (conn->flags & FSNOTIFY_OBJ_TYPE_INODE) |
| 604 | lock = &conn->inode->i_lock; |
| 605 | else |
| 606 | lock = &conn->mnt->mnt_root->d_lock; |
| 607 | |
Jan Kara | 0810b4f | 2017-02-01 09:23:48 +0100 | [diff] [blame] | 608 | while (1) { |
| 609 | /* |
| 610 | * We have to be careful since we can race with e.g. |
| 611 | * fsnotify_clear_marks_by_group() and once we drop 'lock', |
| 612 | * mark can get removed from the obj_list and destroyed. But |
| 613 | * we are holding mark reference so mark cannot be freed and |
| 614 | * calling fsnotify_destroy_mark() more than once is fine. |
| 615 | */ |
| 616 | spin_lock(lock); |
| 617 | if (hlist_empty(&conn->list)) { |
| 618 | spin_unlock(lock); |
| 619 | break; |
| 620 | } |
| 621 | mark = hlist_entry(conn->list.first, struct fsnotify_mark, |
| 622 | obj_list); |
| 623 | /* |
| 624 | * We don't update i_fsnotify_mask / mnt_fsnotify_mask here |
| 625 | * since inode / mount is going away anyway. So just remove |
| 626 | * mark from the list. |
| 627 | */ |
| 628 | hlist_del_init_rcu(&mark->obj_list); |
| 629 | fsnotify_get_mark(mark); |
| 630 | spin_unlock(lock); |
| 631 | fsnotify_destroy_mark(mark, mark->group); |
| 632 | fsnotify_put_mark(mark); |
| 633 | } |
| 634 | } |
| 635 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 636 | /* |
| 637 | * Nothing fancy, just initialize lists and locks and counters. |
| 638 | */ |
| 639 | void fsnotify_init_mark(struct fsnotify_mark *mark, |
| 640 | void (*free_mark)(struct fsnotify_mark *mark)) |
| 641 | { |
Eric Paris | ba643f0 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 642 | memset(mark, 0, sizeof(*mark)); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 643 | spin_lock_init(&mark->lock); |
| 644 | atomic_set(&mark->refcnt, 1); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 645 | mark->free_mark = free_mark; |
| 646 | } |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 647 | |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 648 | /* |
| 649 | * Destroy all marks in destroy_list, waits for SRCU period to finish before |
| 650 | * actually freeing marks. |
| 651 | */ |
| 652 | void fsnotify_mark_destroy_list(void) |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 653 | { |
| 654 | struct fsnotify_mark *mark, *next; |
| 655 | struct list_head private_destroy_list; |
| 656 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 657 | spin_lock(&destroy_lock); |
| 658 | /* exchange the list head */ |
| 659 | list_replace_init(&destroy_list, &private_destroy_list); |
| 660 | spin_unlock(&destroy_lock); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 661 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 662 | synchronize_srcu(&fsnotify_mark_srcu); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 663 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 664 | list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { |
| 665 | list_del_init(&mark->g_list); |
| 666 | fsnotify_put_mark(mark); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 667 | } |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 668 | } |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 669 | |
| 670 | static void fsnotify_mark_destroy_workfn(struct work_struct *work) |
| 671 | { |
| 672 | fsnotify_mark_destroy_list(); |
| 673 | } |