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