1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2008 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/pagemap.h>
8 #include <linux/spinlock.h>
9 #include <linux/page-flags.h>
12 #include "extent_io.h"
15 static void btrfs_assert_tree_read_locked(struct extent_buffer *eb);
17 void btrfs_set_lock_blocking_read(struct extent_buffer *eb)
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.
24 if (eb->lock_nested && current->pid == eb->lock_owner)
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);
33 void btrfs_set_lock_blocking_write(struct extent_buffer *eb)
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.
40 if (eb->lock_nested && current->pid == eb->lock_owner)
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);
51 void btrfs_clear_lock_blocking_read(struct extent_buffer *eb)
54 * No lock is required. The lock owner may change if we have a read
55 * lock, but it won't change to or away from us. If we have the write
56 * lock, we are the owner and it'll never change.
58 if (eb->lock_nested && current->pid == eb->lock_owner)
60 BUG_ON(atomic_read(&eb->blocking_readers) == 0);
62 atomic_inc(&eb->spinning_readers);
63 /* atomic_dec_and_test implies a barrier */
64 if (atomic_dec_and_test(&eb->blocking_readers))
65 cond_wake_up_nomb(&eb->read_lock_wq);
68 void btrfs_clear_lock_blocking_write(struct extent_buffer *eb)
71 * no lock is required. The lock owner may change if
72 * we have a read lock, but it won't change to or away
73 * from us. If we have the write lock, we are the owner
74 * and it'll never change.
76 if (eb->lock_nested && current->pid == eb->lock_owner)
78 BUG_ON(atomic_read(&eb->blocking_writers) != 1);
79 write_lock(&eb->lock);
80 WARN_ON(atomic_read(&eb->spinning_writers));
81 atomic_inc(&eb->spinning_writers);
82 /* atomic_dec_and_test implies a barrier */
83 if (atomic_dec_and_test(&eb->blocking_writers))
84 cond_wake_up_nomb(&eb->write_lock_wq);
88 * take a spinning read lock. This will wait for any blocking
91 void btrfs_tree_read_lock(struct extent_buffer *eb)
94 BUG_ON(!atomic_read(&eb->blocking_writers) &&
95 current->pid == eb->lock_owner);
98 if (atomic_read(&eb->blocking_writers) &&
99 current->pid == eb->lock_owner) {
101 * This extent is already write-locked by our thread. We allow
102 * an additional read lock to be added because it's for the same
103 * thread. btrfs_find_all_roots() depends on this as it may be
104 * called on a partly (write-)locked tree.
106 BUG_ON(eb->lock_nested);
108 read_unlock(&eb->lock);
111 if (atomic_read(&eb->blocking_writers)) {
112 read_unlock(&eb->lock);
113 wait_event(eb->write_lock_wq,
114 atomic_read(&eb->blocking_writers) == 0);
117 atomic_inc(&eb->read_locks);
118 atomic_inc(&eb->spinning_readers);
122 * take a spinning read lock.
123 * returns 1 if we get the read lock and 0 if we don't
124 * this won't wait for blocking writers
126 int btrfs_tree_read_lock_atomic(struct extent_buffer *eb)
128 if (atomic_read(&eb->blocking_writers))
131 read_lock(&eb->lock);
132 if (atomic_read(&eb->blocking_writers)) {
133 read_unlock(&eb->lock);
136 atomic_inc(&eb->read_locks);
137 atomic_inc(&eb->spinning_readers);
142 * returns 1 if we get the read lock and 0 if we don't
143 * this won't wait for blocking writers
145 int btrfs_try_tree_read_lock(struct extent_buffer *eb)
147 if (atomic_read(&eb->blocking_writers))
150 if (!read_trylock(&eb->lock))
153 if (atomic_read(&eb->blocking_writers)) {
154 read_unlock(&eb->lock);
157 atomic_inc(&eb->read_locks);
158 atomic_inc(&eb->spinning_readers);
163 * returns 1 if we get the read lock and 0 if we don't
164 * this won't wait for blocking writers or readers
166 int btrfs_try_tree_write_lock(struct extent_buffer *eb)
168 if (atomic_read(&eb->blocking_writers) ||
169 atomic_read(&eb->blocking_readers))
172 write_lock(&eb->lock);
173 if (atomic_read(&eb->blocking_writers) ||
174 atomic_read(&eb->blocking_readers)) {
175 write_unlock(&eb->lock);
178 atomic_inc(&eb->write_locks);
179 atomic_inc(&eb->spinning_writers);
180 eb->lock_owner = current->pid;
185 * drop a spinning read lock
187 void btrfs_tree_read_unlock(struct extent_buffer *eb)
190 * if we're nested, we have the write lock. No new locking
191 * is needed as long as we are the lock owner.
192 * The write unlock will do a barrier for us, and the lock_nested
193 * field only matters to the lock owner.
195 if (eb->lock_nested && current->pid == eb->lock_owner) {
199 btrfs_assert_tree_read_locked(eb);
200 WARN_ON(atomic_read(&eb->spinning_readers) == 0);
201 atomic_dec(&eb->spinning_readers);
202 atomic_dec(&eb->read_locks);
203 read_unlock(&eb->lock);
207 * drop a blocking read lock
209 void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
212 * if we're nested, we have the write lock. No new locking
213 * is needed as long as we are the lock owner.
214 * The write unlock will do a barrier for us, and the lock_nested
215 * field only matters to the lock owner.
217 if (eb->lock_nested && current->pid == eb->lock_owner) {
221 btrfs_assert_tree_read_locked(eb);
222 WARN_ON(atomic_read(&eb->blocking_readers) == 0);
223 /* atomic_dec_and_test implies a barrier */
224 if (atomic_dec_and_test(&eb->blocking_readers))
225 cond_wake_up_nomb(&eb->read_lock_wq);
226 atomic_dec(&eb->read_locks);
230 * take a spinning write lock. This will wait for both
231 * blocking readers or writers
233 void btrfs_tree_lock(struct extent_buffer *eb)
235 WARN_ON(eb->lock_owner == current->pid);
237 wait_event(eb->read_lock_wq, atomic_read(&eb->blocking_readers) == 0);
238 wait_event(eb->write_lock_wq, atomic_read(&eb->blocking_writers) == 0);
239 write_lock(&eb->lock);
240 if (atomic_read(&eb->blocking_readers) ||
241 atomic_read(&eb->blocking_writers)) {
242 write_unlock(&eb->lock);
245 WARN_ON(atomic_read(&eb->spinning_writers));
246 atomic_inc(&eb->spinning_writers);
247 atomic_inc(&eb->write_locks);
248 eb->lock_owner = current->pid;
252 * drop a spinning or a blocking write lock.
254 void btrfs_tree_unlock(struct extent_buffer *eb)
256 int blockers = atomic_read(&eb->blocking_writers);
258 BUG_ON(blockers > 1);
260 btrfs_assert_tree_locked(eb);
262 atomic_dec(&eb->write_locks);
265 WARN_ON(atomic_read(&eb->spinning_writers));
266 atomic_dec(&eb->blocking_writers);
267 /* Use the lighter barrier after atomic */
268 smp_mb__after_atomic();
269 cond_wake_up_nomb(&eb->write_lock_wq);
271 WARN_ON(atomic_read(&eb->spinning_writers) != 1);
272 atomic_dec(&eb->spinning_writers);
273 write_unlock(&eb->lock);
277 void btrfs_assert_tree_locked(struct extent_buffer *eb)
279 BUG_ON(!atomic_read(&eb->write_locks));
282 static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
284 BUG_ON(!atomic_read(&eb->read_locks));