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