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 | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 108 | /* Calculate mask of events for a list of marks */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 109 | u32 fsnotify_recalc_mask(struct fsnotify_mark_connector *conn) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 110 | { |
| 111 | u32 new_mask = 0; |
| 112 | struct fsnotify_mark *mark; |
| 113 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 114 | if (!conn) |
| 115 | return 0; |
| 116 | |
| 117 | hlist_for_each_entry(mark, &conn->list, obj_list) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 118 | new_mask |= mark->mask; |
| 119 | return new_mask; |
| 120 | } |
| 121 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 122 | /* |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 123 | * Remove mark from inode / vfsmount list, group list, drop inode reference |
| 124 | * if we got one. |
| 125 | * |
| 126 | * Must be called with group->mark_mutex held. |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 127 | */ |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 128 | void fsnotify_detach_mark(struct fsnotify_mark *mark) |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 129 | { |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 130 | struct inode *inode = NULL; |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 131 | struct fsnotify_group *group = mark->group; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 132 | |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 133 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
| 134 | |
Lino Sanfilippo | 104d06f | 2011-06-14 17:29:48 +0200 | [diff] [blame] | 135 | spin_lock(&mark->lock); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 136 | |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 137 | /* something else already called this function on this mark */ |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 138 | if (!(mark->flags & FSNOTIFY_MARK_FLAG_ATTACHED)) { |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 139 | spin_unlock(&mark->lock); |
Lino Sanfilippo | e2a2994 | 2011-06-14 17:29:51 +0200 | [diff] [blame] | 140 | return; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 143 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ATTACHED; |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 144 | |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 145 | if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE) { |
| 146 | inode = mark->connector->inode; |
Eric Paris | b31d397 | 2010-04-21 16:49:38 -0400 | [diff] [blame] | 147 | fsnotify_destroy_inode_mark(mark); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 148 | } else if (mark->connector->flags & FSNOTIFY_OBJ_TYPE_VFSMOUNT) |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 149 | fsnotify_destroy_vfsmount_mark(mark); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 150 | else |
| 151 | BUG(); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 152 | /* |
| 153 | * Note that we didn't update flags telling whether inode cares about |
| 154 | * what's happening with children. We update these flags from |
| 155 | * __fsnotify_parent() lazily when next event happens on one of our |
| 156 | * children. |
| 157 | */ |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 158 | |
| 159 | list_del_init(&mark->g_list); |
Linus Torvalds | d725e66 | 2015-07-21 16:06:53 -0700 | [diff] [blame] | 160 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 161 | spin_unlock(&mark->lock); |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 162 | |
Lino Sanfilippo | 6960b0d | 2011-08-12 01:13:31 +0200 | [diff] [blame] | 163 | if (inode && (mark->flags & FSNOTIFY_MARK_FLAG_OBJECT_PINNED)) |
| 164 | iput(inode); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 165 | |
| 166 | atomic_dec(&group->num_marks); |
| 167 | } |
| 168 | |
| 169 | /* |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 170 | * Prepare mark for freeing and add it to the list of marks prepared for |
| 171 | * freeing. The actual freeing must happen after SRCU period ends and the |
| 172 | * caller is responsible for this. |
| 173 | * |
| 174 | * The function returns true if the mark was added to the list of marks for |
| 175 | * freeing. The function returns false if someone else has already called |
| 176 | * __fsnotify_free_mark() for the mark. |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 177 | */ |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 178 | static bool __fsnotify_free_mark(struct fsnotify_mark *mark) |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 179 | { |
| 180 | struct fsnotify_group *group = mark->group; |
| 181 | |
| 182 | spin_lock(&mark->lock); |
| 183 | /* something else already called this function on this mark */ |
| 184 | if (!(mark->flags & FSNOTIFY_MARK_FLAG_ALIVE)) { |
| 185 | spin_unlock(&mark->lock); |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 186 | return false; |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 187 | } |
| 188 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; |
| 189 | spin_unlock(&mark->lock); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 190 | |
Linus Torvalds | d725e66 | 2015-07-21 16:06:53 -0700 | [diff] [blame] | 191 | /* |
| 192 | * Some groups like to know that marks are being freed. This is a |
| 193 | * callback to the group function to let it know that this mark |
| 194 | * is being freed. |
| 195 | */ |
| 196 | if (group->ops->freeing_mark) |
| 197 | group->ops->freeing_mark(mark, group); |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 198 | |
| 199 | spin_lock(&destroy_lock); |
| 200 | list_add(&mark->g_list, &destroy_list); |
| 201 | spin_unlock(&destroy_lock); |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Free fsnotify mark. The freeing is actually happening from a workqueue which |
| 208 | * first waits for srcu period end. Caller must have a reference to the mark |
| 209 | * or be protected by fsnotify_mark_srcu. |
| 210 | */ |
| 211 | void fsnotify_free_mark(struct fsnotify_mark *mark) |
| 212 | { |
| 213 | if (__fsnotify_free_mark(mark)) { |
| 214 | queue_delayed_work(system_unbound_wq, &reaper_work, |
| 215 | FSNOTIFY_REAPER_DELAY); |
| 216 | } |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void fsnotify_destroy_mark(struct fsnotify_mark *mark, |
| 220 | struct fsnotify_group *group) |
| 221 | { |
Lino Sanfilippo | 6960b0d | 2011-08-12 01:13:31 +0200 | [diff] [blame] | 222 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 223 | fsnotify_detach_mark(mark); |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 224 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 225 | fsnotify_free_mark(mark); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 228 | void fsnotify_destroy_marks(struct fsnotify_mark_connector *conn, |
| 229 | spinlock_t *lock) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 230 | { |
Jan Kara | 925d113 | 2015-09-04 15:43:09 -0700 | [diff] [blame] | 231 | struct fsnotify_mark *mark; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 232 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 233 | if (!conn) |
| 234 | return; |
| 235 | |
Jan Kara | 925d113 | 2015-09-04 15:43:09 -0700 | [diff] [blame] | 236 | while (1) { |
| 237 | /* |
| 238 | * We have to be careful since we can race with e.g. |
| 239 | * fsnotify_clear_marks_by_group() and once we drop 'lock', |
| 240 | * mark can get removed from the obj_list and destroyed. But |
| 241 | * we are holding mark reference so mark cannot be freed and |
| 242 | * calling fsnotify_destroy_mark() more than once is fine. |
| 243 | */ |
| 244 | spin_lock(lock); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 245 | if (hlist_empty(&conn->list)) { |
Jan Kara | 925d113 | 2015-09-04 15:43:09 -0700 | [diff] [blame] | 246 | spin_unlock(lock); |
| 247 | break; |
| 248 | } |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 249 | mark = hlist_entry(conn->list.first, struct fsnotify_mark, |
| 250 | obj_list); |
Jan Kara | 925d113 | 2015-09-04 15:43:09 -0700 | [diff] [blame] | 251 | /* |
| 252 | * We don't update i_fsnotify_mask / mnt_fsnotify_mask here |
| 253 | * since inode / mount is going away anyway. So just remove |
| 254 | * mark from the list. |
| 255 | */ |
| 256 | hlist_del_init_rcu(&mark->obj_list); |
| 257 | fsnotify_get_mark(mark); |
| 258 | spin_unlock(lock); |
| 259 | fsnotify_destroy_mark(mark, mark->group); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 260 | fsnotify_put_mark(mark); |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 264 | void fsnotify_connector_free(struct fsnotify_mark_connector **connp) |
| 265 | { |
| 266 | if (*connp) { |
| 267 | kmem_cache_free(fsnotify_mark_connector_cachep, *connp); |
| 268 | *connp = NULL; |
| 269 | } |
| 270 | } |
| 271 | |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 272 | void fsnotify_set_mark_mask_locked(struct fsnotify_mark *mark, __u32 mask) |
| 273 | { |
| 274 | assert_spin_locked(&mark->lock); |
| 275 | |
| 276 | mark->mask = mask; |
| 277 | |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 278 | if (mark->connector && mark->connector->flags & FSNOTIFY_OBJ_TYPE_INODE) |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 279 | fsnotify_set_inode_mark_mask_locked(mark, mask); |
| 280 | } |
| 281 | |
Eric Paris | 33af5e3 | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 282 | void fsnotify_set_mark_ignored_mask_locked(struct fsnotify_mark *mark, __u32 mask) |
| 283 | { |
| 284 | assert_spin_locked(&mark->lock); |
| 285 | |
| 286 | mark->ignored_mask = mask; |
| 287 | } |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 288 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 289 | /* |
Jan Kara | 8edc6e1 | 2014-11-13 15:19:33 -0800 | [diff] [blame] | 290 | * Sorting function for lists of fsnotify marks. |
| 291 | * |
| 292 | * Fanotify supports different notification classes (reflected as priority of |
| 293 | * notification group). Events shall be passed to notification groups in |
| 294 | * decreasing priority order. To achieve this marks in notification lists for |
| 295 | * inodes and vfsmounts are sorted so that priorities of corresponding groups |
| 296 | * are descending. |
| 297 | * |
| 298 | * Furthermore correct handling of the ignore mask requires processing inode |
| 299 | * and vfsmount marks of each group together. Using the group address as |
| 300 | * further sort criterion provides a unique sorting order and thus we can |
| 301 | * merge inode and vfsmount lists of marks in linear time and find groups |
| 302 | * present in both lists. |
| 303 | * |
| 304 | * A return value of 1 signifies that b has priority over a. |
| 305 | * A return value of 0 signifies that the two marks have to be handled together. |
| 306 | * A return value of -1 signifies that a has priority over b. |
| 307 | */ |
| 308 | int fsnotify_compare_groups(struct fsnotify_group *a, struct fsnotify_group *b) |
| 309 | { |
| 310 | if (a == b) |
| 311 | return 0; |
| 312 | if (!a) |
| 313 | return 1; |
| 314 | if (!b) |
| 315 | return -1; |
| 316 | if (a->priority < b->priority) |
| 317 | return 1; |
| 318 | if (a->priority > b->priority) |
| 319 | return -1; |
| 320 | if (a < b) |
| 321 | return 1; |
| 322 | return -1; |
| 323 | } |
| 324 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 325 | static int fsnotify_attach_connector_to_object( |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 326 | struct fsnotify_mark_connector **connp, |
| 327 | struct inode *inode, |
| 328 | struct vfsmount *mnt) |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 329 | { |
| 330 | struct fsnotify_mark_connector *conn; |
| 331 | |
| 332 | conn = kmem_cache_alloc(fsnotify_mark_connector_cachep, GFP_ATOMIC); |
| 333 | if (!conn) |
| 334 | return -ENOMEM; |
| 335 | INIT_HLIST_HEAD(&conn->list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 336 | if (inode) { |
| 337 | conn->flags = FSNOTIFY_OBJ_TYPE_INODE; |
| 338 | conn->inode = inode; |
| 339 | } else { |
| 340 | conn->flags = FSNOTIFY_OBJ_TYPE_VFSMOUNT; |
| 341 | conn->mnt = mnt; |
| 342 | } |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 343 | /* |
| 344 | * Make sure 'conn' initialization is visible. Matches |
| 345 | * lockless_dereference() in fsnotify(). |
| 346 | */ |
| 347 | smp_wmb(); |
| 348 | *connp = conn; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Add mark into proper place in given list of marks. These marks may be used |
| 355 | * for the fsnotify backend to determine which event types should be delivered |
| 356 | * to which group and for which inodes. These marks are ordered according to |
| 357 | * priority, highest number first, and then by the group's location in memory. |
| 358 | */ |
| 359 | int fsnotify_add_mark_list(struct fsnotify_mark_connector **connp, |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 360 | struct fsnotify_mark *mark, struct inode *inode, |
| 361 | struct vfsmount *mnt, int allow_dups) |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 362 | { |
| 363 | struct fsnotify_mark *lmark, *last = NULL; |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 364 | struct fsnotify_mark_connector *conn; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 365 | int cmp; |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 366 | int err; |
| 367 | |
| 368 | if (!*connp) { |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 369 | err = fsnotify_attach_connector_to_object(connp, inode, mnt); |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 370 | if (err) |
| 371 | return err; |
| 372 | } |
| 373 | conn = *connp; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 374 | |
| 375 | /* is mark the first mark? */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 376 | if (hlist_empty(&conn->list)) { |
| 377 | hlist_add_head_rcu(&mark->obj_list, &conn->list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 378 | goto added; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* should mark be in the middle of the current list? */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 382 | hlist_for_each_entry(lmark, &conn->list, obj_list) { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 383 | last = lmark; |
| 384 | |
| 385 | if ((lmark->group == mark->group) && !allow_dups) |
| 386 | return -EEXIST; |
| 387 | |
| 388 | cmp = fsnotify_compare_groups(lmark->group, mark->group); |
| 389 | if (cmp >= 0) { |
| 390 | hlist_add_before_rcu(&mark->obj_list, &lmark->obj_list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 391 | goto added; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
| 395 | BUG_ON(last == NULL); |
| 396 | /* mark should be the last entry. last is the current last entry */ |
| 397 | hlist_add_behind_rcu(&mark->obj_list, &last->obj_list); |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 398 | added: |
| 399 | mark->connector = conn; |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Jan Kara | 8edc6e1 | 2014-11-13 15:19:33 -0800 | [diff] [blame] | 403 | /* |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 404 | * Attach an initialized mark to a given group and fs object. |
| 405 | * These marks may be used for the fsnotify backend to determine which |
| 406 | * event types should be delivered to which group. |
| 407 | */ |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 408 | int fsnotify_add_mark_locked(struct fsnotify_mark *mark, |
| 409 | struct fsnotify_group *group, struct inode *inode, |
| 410 | struct vfsmount *mnt, int allow_dups) |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 411 | { |
| 412 | int ret = 0; |
| 413 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 414 | BUG_ON(inode && mnt); |
| 415 | BUG_ON(!inode && !mnt); |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 416 | BUG_ON(!mutex_is_locked(&group->mark_mutex)); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 417 | |
| 418 | /* |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 419 | * LOCKING ORDER!!!! |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 420 | * group->mark_mutex |
Lino Sanfilippo | 104d06f | 2011-06-14 17:29:48 +0200 | [diff] [blame] | 421 | * mark->lock |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 422 | * inode->i_lock |
| 423 | */ |
Lino Sanfilippo | 104d06f | 2011-06-14 17:29:48 +0200 | [diff] [blame] | 424 | spin_lock(&mark->lock); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 425 | mark->flags |= FSNOTIFY_MARK_FLAG_ALIVE | FSNOTIFY_MARK_FLAG_ATTACHED; |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 426 | |
Lino Sanfilippo | 23e964c | 2011-06-14 17:29:47 +0200 | [diff] [blame] | 427 | fsnotify_get_group(group); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 428 | mark->group = group; |
| 429 | list_add(&mark->g_list, &group->marks_list); |
| 430 | atomic_inc(&group->num_marks); |
| 431 | fsnotify_get_mark(mark); /* for i_list and g_list */ |
| 432 | |
| 433 | if (inode) { |
| 434 | ret = fsnotify_add_inode_mark(mark, group, inode, allow_dups); |
| 435 | if (ret) |
| 436 | goto err; |
Eric Paris | 0d48b7f | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 437 | } else if (mnt) { |
| 438 | ret = fsnotify_add_vfsmount_mark(mark, group, mnt, allow_dups); |
| 439 | if (ret) |
| 440 | goto err; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 441 | } else { |
| 442 | BUG(); |
| 443 | } |
| 444 | |
Eric Paris | 90b1e7a | 2009-12-17 21:24:33 -0500 | [diff] [blame] | 445 | /* this will pin the object if appropriate */ |
| 446 | fsnotify_set_mark_mask_locked(mark, mark->mask); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 447 | spin_unlock(&mark->lock); |
| 448 | |
| 449 | if (inode) |
| 450 | __fsnotify_update_child_dentry_flags(inode); |
| 451 | |
| 452 | return ret; |
| 453 | err: |
Eric Paris | 700307a | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 454 | mark->flags &= ~FSNOTIFY_MARK_FLAG_ALIVE; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 455 | list_del_init(&mark->g_list); |
Lino Sanfilippo | 23e964c | 2011-06-14 17:29:47 +0200 | [diff] [blame] | 456 | fsnotify_put_group(group); |
Eric Paris | 75c1be4 | 2010-07-28 10:18:38 -0400 | [diff] [blame] | 457 | mark->group = NULL; |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 458 | atomic_dec(&group->num_marks); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 459 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 460 | spin_unlock(&mark->lock); |
| 461 | |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 462 | spin_lock(&destroy_lock); |
| 463 | list_add(&mark->g_list, &destroy_list); |
| 464 | spin_unlock(&destroy_lock); |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 465 | queue_delayed_work(system_unbound_wq, &reaper_work, |
| 466 | FSNOTIFY_REAPER_DELAY); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 467 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 468 | return ret; |
| 469 | } |
| 470 | |
Lino Sanfilippo | d5a335b | 2011-06-14 17:29:52 +0200 | [diff] [blame] | 471 | int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group, |
| 472 | struct inode *inode, struct vfsmount *mnt, int allow_dups) |
| 473 | { |
| 474 | int ret; |
| 475 | mutex_lock(&group->mark_mutex); |
| 476 | ret = fsnotify_add_mark_locked(mark, group, inode, mnt, allow_dups); |
| 477 | mutex_unlock(&group->mark_mutex); |
| 478 | return ret; |
| 479 | } |
| 480 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 481 | /* |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 482 | * Given a list of marks, find the mark associated with given group. If found |
| 483 | * take a reference to that mark and return it, else return NULL. |
| 484 | */ |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 485 | struct fsnotify_mark *fsnotify_find_mark(struct fsnotify_mark_connector *conn, |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 486 | struct fsnotify_group *group) |
| 487 | { |
| 488 | struct fsnotify_mark *mark; |
| 489 | |
Jan Kara | 9dd813c | 2017-03-14 12:31:02 +0100 | [diff] [blame] | 490 | if (!conn) |
| 491 | return NULL; |
| 492 | |
| 493 | hlist_for_each_entry(mark, &conn->list, obj_list) { |
Jan Kara | 0809ab6 | 2014-12-12 16:58:36 -0800 | [diff] [blame] | 494 | if (mark->group == group) { |
| 495 | fsnotify_get_mark(mark); |
| 496 | return mark; |
| 497 | } |
| 498 | } |
| 499 | return NULL; |
| 500 | } |
| 501 | |
| 502 | /* |
Linus Torvalds | d725e66 | 2015-07-21 16:06:53 -0700 | [diff] [blame] | 503 | * 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] | 504 | */ |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 505 | void fsnotify_clear_marks_by_group_flags(struct fsnotify_group *group, |
| 506 | unsigned int flags) |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 507 | { |
| 508 | struct fsnotify_mark *lmark, *mark; |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 509 | LIST_HEAD(to_free); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 510 | |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 511 | /* |
| 512 | * We have to be really careful here. Anytime we drop mark_mutex, e.g. |
| 513 | * fsnotify_clear_marks_by_inode() can come and free marks. Even in our |
| 514 | * to_free list so we have to use mark_mutex even when accessing that |
| 515 | * list. And freeing mark requires us to drop mark_mutex. So we can |
| 516 | * reliably free only the first mark in the list. That's why we first |
| 517 | * move marks to free to to_free list in one go and then free marks in |
| 518 | * to_free list one by one. |
| 519 | */ |
Lino Sanfilippo | 6960b0d | 2011-08-12 01:13:31 +0200 | [diff] [blame] | 520 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 521 | list_for_each_entry_safe(mark, lmark, &group->marks_list, g_list) { |
Jan Kara | 86ffe24 | 2017-03-14 14:29:35 +0100 | [diff] [blame^] | 522 | if (mark->connector->flags & flags) |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 523 | list_move(&mark->g_list, &to_free); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 524 | } |
Lino Sanfilippo | 986ab09 | 2011-06-14 17:29:50 +0200 | [diff] [blame] | 525 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 526 | |
| 527 | while (1) { |
| 528 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
| 529 | if (list_empty(&to_free)) { |
| 530 | mutex_unlock(&group->mark_mutex); |
| 531 | break; |
| 532 | } |
| 533 | mark = list_first_entry(&to_free, struct fsnotify_mark, g_list); |
| 534 | fsnotify_get_mark(mark); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 535 | fsnotify_detach_mark(mark); |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 536 | mutex_unlock(&group->mark_mutex); |
Jan Kara | 4712e722 | 2015-09-04 15:43:12 -0700 | [diff] [blame] | 537 | fsnotify_free_mark(mark); |
Jan Kara | 8f2f3eb | 2015-08-06 15:46:42 -0700 | [diff] [blame] | 538 | fsnotify_put_mark(mark); |
| 539 | } |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 542 | /* |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 543 | * Given a group, prepare for freeing all the marks associated with that group. |
| 544 | * The marks are attached to the list of marks prepared for destruction, the |
| 545 | * caller is responsible for freeing marks in that list after SRCU period has |
| 546 | * ended. |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 547 | */ |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 548 | void fsnotify_detach_group_marks(struct fsnotify_group *group) |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 549 | { |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 550 | struct fsnotify_mark *mark; |
| 551 | |
| 552 | while (1) { |
| 553 | mutex_lock_nested(&group->mark_mutex, SINGLE_DEPTH_NESTING); |
| 554 | if (list_empty(&group->marks_list)) { |
| 555 | mutex_unlock(&group->mark_mutex); |
| 556 | break; |
| 557 | } |
| 558 | mark = list_first_entry(&group->marks_list, |
| 559 | struct fsnotify_mark, g_list); |
| 560 | fsnotify_get_mark(mark); |
| 561 | fsnotify_detach_mark(mark); |
| 562 | mutex_unlock(&group->mark_mutex); |
| 563 | __fsnotify_free_mark(mark); |
| 564 | fsnotify_put_mark(mark); |
| 565 | } |
Eric Paris | 4d92604 | 2009-12-17 21:24:34 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 568 | /* |
| 569 | * Nothing fancy, just initialize lists and locks and counters. |
| 570 | */ |
| 571 | void fsnotify_init_mark(struct fsnotify_mark *mark, |
| 572 | void (*free_mark)(struct fsnotify_mark *mark)) |
| 573 | { |
Eric Paris | ba643f0 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 574 | memset(mark, 0, sizeof(*mark)); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 575 | spin_lock_init(&mark->lock); |
| 576 | atomic_set(&mark->refcnt, 1); |
Eric Paris | 5444e29 | 2009-12-17 21:24:27 -0500 | [diff] [blame] | 577 | mark->free_mark = free_mark; |
| 578 | } |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 579 | |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 580 | /* |
| 581 | * Destroy all marks in destroy_list, waits for SRCU period to finish before |
| 582 | * actually freeing marks. |
| 583 | */ |
| 584 | void fsnotify_mark_destroy_list(void) |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 585 | { |
| 586 | struct fsnotify_mark *mark, *next; |
| 587 | struct list_head private_destroy_list; |
| 588 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 589 | spin_lock(&destroy_lock); |
| 590 | /* exchange the list head */ |
| 591 | list_replace_init(&destroy_list, &private_destroy_list); |
| 592 | spin_unlock(&destroy_lock); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 593 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 594 | synchronize_srcu(&fsnotify_mark_srcu); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 595 | |
Jeff Layton | 0918f1c | 2016-02-17 13:11:21 -0800 | [diff] [blame] | 596 | list_for_each_entry_safe(mark, next, &private_destroy_list, g_list) { |
| 597 | list_del_init(&mark->g_list); |
| 598 | fsnotify_put_mark(mark); |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 599 | } |
Jeff Layton | 13d34ac | 2016-02-17 13:11:18 -0800 | [diff] [blame] | 600 | } |
Jan Kara | 35e4817 | 2016-05-19 17:08:59 -0700 | [diff] [blame] | 601 | |
| 602 | static void fsnotify_mark_destroy_workfn(struct work_struct *work) |
| 603 | { |
| 604 | fsnotify_mark_destroy_list(); |
| 605 | } |