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