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