David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008 Oracle. All rights reserved. |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4 | */ |
David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 5 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 7 | #include <linux/pagemap.h> |
| 8 | #include <linux/spinlock.h> |
| 9 | #include <linux/page-flags.h> |
Chris Mason | 4881ee5 | 2008-07-24 09:51:08 -0400 | [diff] [blame] | 10 | #include <asm/bug.h> |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 11 | #include "ctree.h" |
| 12 | #include "extent_io.h" |
| 13 | #include "locking.h" |
| 14 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 15 | static void btrfs_assert_tree_read_locked(struct extent_buffer *eb); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 16 | |
David Sterba | e4e9fd0 | 2018-08-24 14:45:20 +0200 | [diff] [blame^] | 17 | #ifdef CONFIG_BTRFS_DEBUG |
| 18 | static void btrfs_assert_spinning_writers_get(struct extent_buffer *eb) |
| 19 | { |
| 20 | WARN_ON(atomic_read(&eb->spinning_writers)); |
| 21 | atomic_inc(&eb->spinning_writers); |
| 22 | } |
| 23 | |
| 24 | static void btrfs_assert_spinning_writers_put(struct extent_buffer *eb) |
| 25 | { |
| 26 | WARN_ON(atomic_read(&eb->spinning_writers) != 1); |
| 27 | atomic_dec(&eb->spinning_writers); |
| 28 | } |
| 29 | |
| 30 | static void btrfs_assert_no_spinning_writers(struct extent_buffer *eb) |
| 31 | { |
| 32 | WARN_ON(atomic_read(&eb->spinning_writers)); |
| 33 | } |
| 34 | |
| 35 | #else |
| 36 | static void btrfs_assert_spinning_writers_get(struct extent_buffer *eb) { } |
| 37 | static void btrfs_assert_spinning_writers_put(struct extent_buffer *eb) { } |
| 38 | static void btrfs_assert_no_spinning_writers(struct extent_buffer *eb) { } |
| 39 | #endif |
| 40 | |
David Sterba | b95be2d | 2018-04-04 01:43:05 +0200 | [diff] [blame] | 41 | void btrfs_set_lock_blocking_read(struct extent_buffer *eb) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 42 | { |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 43 | /* |
David Sterba | b95be2d | 2018-04-04 01:43:05 +0200 | [diff] [blame] | 44 | * No lock is required. The lock owner may change if we have a read |
| 45 | * lock, but it won't change to or away from us. If we have the write |
| 46 | * lock, we are the owner and it'll never change. |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 47 | */ |
| 48 | if (eb->lock_nested && current->pid == eb->lock_owner) |
| 49 | return; |
David Sterba | b95be2d | 2018-04-04 01:43:05 +0200 | [diff] [blame] | 50 | btrfs_assert_tree_read_locked(eb); |
| 51 | atomic_inc(&eb->blocking_readers); |
| 52 | WARN_ON(atomic_read(&eb->spinning_readers) == 0); |
| 53 | atomic_dec(&eb->spinning_readers); |
| 54 | read_unlock(&eb->lock); |
| 55 | } |
| 56 | |
| 57 | void btrfs_set_lock_blocking_write(struct extent_buffer *eb) |
| 58 | { |
| 59 | /* |
| 60 | * No lock is required. The lock owner may change if we have a read |
| 61 | * lock, but it won't change to or away from us. If we have the write |
| 62 | * lock, we are the owner and it'll never change. |
| 63 | */ |
| 64 | if (eb->lock_nested && current->pid == eb->lock_owner) |
| 65 | return; |
| 66 | if (atomic_read(&eb->blocking_writers) == 0) { |
| 67 | WARN_ON(atomic_read(&eb->spinning_writers) != 1); |
| 68 | atomic_dec(&eb->spinning_writers); |
| 69 | btrfs_assert_tree_locked(eb); |
| 70 | atomic_inc(&eb->blocking_writers); |
| 71 | write_unlock(&eb->lock); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 72 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 73 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 74 | |
David Sterba | aa12c02 | 2018-04-04 01:52:31 +0200 | [diff] [blame] | 75 | void btrfs_clear_lock_blocking_read(struct extent_buffer *eb) |
| 76 | { |
| 77 | /* |
| 78 | * No lock is required. The lock owner may change if we have a read |
| 79 | * lock, but it won't change to or away from us. If we have the write |
| 80 | * lock, we are the owner and it'll never change. |
| 81 | */ |
| 82 | if (eb->lock_nested && current->pid == eb->lock_owner) |
| 83 | return; |
| 84 | BUG_ON(atomic_read(&eb->blocking_readers) == 0); |
| 85 | read_lock(&eb->lock); |
| 86 | atomic_inc(&eb->spinning_readers); |
| 87 | /* atomic_dec_and_test implies a barrier */ |
| 88 | if (atomic_dec_and_test(&eb->blocking_readers)) |
| 89 | cond_wake_up_nomb(&eb->read_lock_wq); |
| 90 | } |
| 91 | |
| 92 | void btrfs_clear_lock_blocking_write(struct extent_buffer *eb) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 93 | { |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 94 | /* |
| 95 | * no lock is required. The lock owner may change if |
| 96 | * we have a read lock, but it won't change to or away |
| 97 | * from us. If we have the write lock, we are the owner |
| 98 | * and it'll never change. |
| 99 | */ |
| 100 | if (eb->lock_nested && current->pid == eb->lock_owner) |
| 101 | return; |
David Sterba | aa12c02 | 2018-04-04 01:52:31 +0200 | [diff] [blame] | 102 | BUG_ON(atomic_read(&eb->blocking_writers) != 1); |
| 103 | write_lock(&eb->lock); |
| 104 | WARN_ON(atomic_read(&eb->spinning_writers)); |
| 105 | atomic_inc(&eb->spinning_writers); |
| 106 | /* atomic_dec_and_test implies a barrier */ |
| 107 | if (atomic_dec_and_test(&eb->blocking_writers)) |
| 108 | cond_wake_up_nomb(&eb->write_lock_wq); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 112 | * take a spinning read lock. This will wait for any blocking |
| 113 | * writers |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 114 | */ |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 115 | void btrfs_tree_read_lock(struct extent_buffer *eb) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 116 | { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 117 | again: |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 118 | BUG_ON(!atomic_read(&eb->blocking_writers) && |
| 119 | current->pid == eb->lock_owner); |
| 120 | |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 121 | read_lock(&eb->lock); |
| 122 | if (atomic_read(&eb->blocking_writers) && |
| 123 | current->pid == eb->lock_owner) { |
| 124 | /* |
| 125 | * This extent is already write-locked by our thread. We allow |
| 126 | * an additional read lock to be added because it's for the same |
| 127 | * thread. btrfs_find_all_roots() depends on this as it may be |
| 128 | * called on a partly (write-)locked tree. |
| 129 | */ |
| 130 | BUG_ON(eb->lock_nested); |
| 131 | eb->lock_nested = 1; |
| 132 | read_unlock(&eb->lock); |
| 133 | return; |
| 134 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 135 | if (atomic_read(&eb->blocking_writers)) { |
| 136 | read_unlock(&eb->lock); |
Liu Bo | 39f9d02 | 2012-12-27 09:01:22 +0000 | [diff] [blame] | 137 | wait_event(eb->write_lock_wq, |
| 138 | atomic_read(&eb->blocking_writers) == 0); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 139 | goto again; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 140 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 141 | atomic_inc(&eb->read_locks); |
| 142 | atomic_inc(&eb->spinning_readers); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* |
Chris Mason | f82c458 | 2014-11-19 10:25:09 -0800 | [diff] [blame] | 146 | * take a spinning read lock. |
| 147 | * returns 1 if we get the read lock and 0 if we don't |
| 148 | * this won't wait for blocking writers |
| 149 | */ |
| 150 | int btrfs_tree_read_lock_atomic(struct extent_buffer *eb) |
| 151 | { |
| 152 | if (atomic_read(&eb->blocking_writers)) |
| 153 | return 0; |
| 154 | |
| 155 | read_lock(&eb->lock); |
| 156 | if (atomic_read(&eb->blocking_writers)) { |
| 157 | read_unlock(&eb->lock); |
| 158 | return 0; |
| 159 | } |
| 160 | atomic_inc(&eb->read_locks); |
| 161 | atomic_inc(&eb->spinning_readers); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | /* |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 166 | * returns 1 if we get the read lock and 0 if we don't |
| 167 | * this won't wait for blocking writers |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 168 | */ |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 169 | int btrfs_try_tree_read_lock(struct extent_buffer *eb) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 170 | { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 171 | if (atomic_read(&eb->blocking_writers)) |
| 172 | return 0; |
Chris Mason | 66d7e85 | 2009-03-12 20:12:45 -0400 | [diff] [blame] | 173 | |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 174 | if (!read_trylock(&eb->lock)) |
| 175 | return 0; |
| 176 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 177 | if (atomic_read(&eb->blocking_writers)) { |
| 178 | read_unlock(&eb->lock); |
| 179 | return 0; |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 180 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 181 | atomic_inc(&eb->read_locks); |
| 182 | atomic_inc(&eb->spinning_readers); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | /* |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 187 | * returns 1 if we get the read lock and 0 if we don't |
| 188 | * this won't wait for blocking writers or readers |
| 189 | */ |
| 190 | int btrfs_try_tree_write_lock(struct extent_buffer *eb) |
| 191 | { |
| 192 | if (atomic_read(&eb->blocking_writers) || |
| 193 | atomic_read(&eb->blocking_readers)) |
| 194 | return 0; |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 195 | |
Chris Mason | f82c458 | 2014-11-19 10:25:09 -0800 | [diff] [blame] | 196 | write_lock(&eb->lock); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 197 | if (atomic_read(&eb->blocking_writers) || |
| 198 | atomic_read(&eb->blocking_readers)) { |
| 199 | write_unlock(&eb->lock); |
| 200 | return 0; |
| 201 | } |
| 202 | atomic_inc(&eb->write_locks); |
| 203 | atomic_inc(&eb->spinning_writers); |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 204 | eb->lock_owner = current->pid; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 205 | return 1; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * drop a spinning read lock |
| 210 | */ |
| 211 | void btrfs_tree_read_unlock(struct extent_buffer *eb) |
| 212 | { |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 213 | /* |
| 214 | * if we're nested, we have the write lock. No new locking |
| 215 | * is needed as long as we are the lock owner. |
| 216 | * The write unlock will do a barrier for us, and the lock_nested |
| 217 | * field only matters to the lock owner. |
| 218 | */ |
| 219 | if (eb->lock_nested && current->pid == eb->lock_owner) { |
| 220 | eb->lock_nested = 0; |
| 221 | return; |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 222 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 223 | btrfs_assert_tree_read_locked(eb); |
| 224 | WARN_ON(atomic_read(&eb->spinning_readers) == 0); |
| 225 | atomic_dec(&eb->spinning_readers); |
| 226 | atomic_dec(&eb->read_locks); |
| 227 | read_unlock(&eb->lock); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * drop a blocking read lock |
| 232 | */ |
| 233 | void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb) |
| 234 | { |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 235 | /* |
| 236 | * if we're nested, we have the write lock. No new locking |
| 237 | * is needed as long as we are the lock owner. |
| 238 | * The write unlock will do a barrier for us, and the lock_nested |
| 239 | * field only matters to the lock owner. |
| 240 | */ |
| 241 | if (eb->lock_nested && current->pid == eb->lock_owner) { |
| 242 | eb->lock_nested = 0; |
| 243 | return; |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 244 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 245 | btrfs_assert_tree_read_locked(eb); |
| 246 | WARN_ON(atomic_read(&eb->blocking_readers) == 0); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 247 | /* atomic_dec_and_test implies a barrier */ |
| 248 | if (atomic_dec_and_test(&eb->blocking_readers)) |
| 249 | cond_wake_up_nomb(&eb->read_lock_wq); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 250 | atomic_dec(&eb->read_locks); |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * take a spinning write lock. This will wait for both |
| 255 | * blocking readers or writers |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 256 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 257 | void btrfs_tree_lock(struct extent_buffer *eb) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 258 | { |
Zhaolei | 166f66d | 2015-08-06 22:39:36 +0800 | [diff] [blame] | 259 | WARN_ON(eb->lock_owner == current->pid); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 260 | again: |
| 261 | wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0); |
| 262 | wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0); |
| 263 | write_lock(&eb->lock); |
David Sterba | 970e74d | 2018-04-04 02:11:50 +0200 | [diff] [blame] | 264 | if (atomic_read(&eb->blocking_readers) || |
| 265 | atomic_read(&eb->blocking_writers)) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 266 | write_unlock(&eb->lock); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 267 | goto again; |
| 268 | } |
| 269 | WARN_ON(atomic_read(&eb->spinning_writers)); |
| 270 | atomic_inc(&eb->spinning_writers); |
| 271 | atomic_inc(&eb->write_locks); |
Arne Jansen | 5b25f70 | 2011-09-13 10:55:48 +0200 | [diff] [blame] | 272 | eb->lock_owner = current->pid; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 273 | } |
| 274 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 275 | /* |
| 276 | * drop a spinning or a blocking write lock. |
| 277 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 278 | void btrfs_tree_unlock(struct extent_buffer *eb) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 279 | { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 280 | int blockers = atomic_read(&eb->blocking_writers); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 281 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 282 | BUG_ON(blockers > 1); |
| 283 | |
| 284 | btrfs_assert_tree_locked(eb); |
Chris Mason | ea4ebde | 2014-06-19 14:16:52 -0700 | [diff] [blame] | 285 | eb->lock_owner = 0; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 286 | atomic_dec(&eb->write_locks); |
| 287 | |
| 288 | if (blockers) { |
| 289 | WARN_ON(atomic_read(&eb->spinning_writers)); |
| 290 | atomic_dec(&eb->blocking_writers); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 291 | /* Use the lighter barrier after atomic */ |
Nikolay Borisov | 2e32ef8 | 2018-02-14 14:37:26 +0200 | [diff] [blame] | 292 | smp_mb__after_atomic(); |
David Sterba | 093258e | 2018-02-26 16:15:17 +0100 | [diff] [blame] | 293 | cond_wake_up_nomb(&eb->write_lock_wq); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 294 | } else { |
| 295 | WARN_ON(atomic_read(&eb->spinning_writers) != 1); |
| 296 | atomic_dec(&eb->spinning_writers); |
| 297 | write_unlock(&eb->lock); |
| 298 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 301 | void btrfs_assert_tree_locked(struct extent_buffer *eb) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 302 | { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 303 | BUG_ON(!atomic_read(&eb->write_locks)); |
| 304 | } |
| 305 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 306 | static void btrfs_assert_tree_read_locked(struct extent_buffer *eb) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 307 | { |
| 308 | BUG_ON(!atomic_read(&eb->read_locks)); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 309 | } |