]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/btrfs/locking.c
btrfs: Remove EXTENT_IOBITS
[mirror_ubuntu-jammy-kernel.git] / fs / btrfs / locking.c
CommitLineData
c1d7c514 1// SPDX-License-Identifier: GPL-2.0
925baedd
CM
2/*
3 * Copyright (C) 2008 Oracle. All rights reserved.
925baedd 4 */
c1d7c514 5
925baedd 6#include <linux/sched.h>
925baedd
CM
7#include <linux/pagemap.h>
8#include <linux/spinlock.h>
9#include <linux/page-flags.h>
4881ee5a 10#include <asm/bug.h>
925baedd
CM
11#include "ctree.h"
12#include "extent_io.h"
13#include "locking.h"
14
48a3b636 15static void btrfs_assert_tree_read_locked(struct extent_buffer *eb);
d397712b 16
b95be2d9
DS
17void btrfs_set_lock_blocking_read(struct extent_buffer *eb)
18{
19 /*
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.
23 */
24 if (eb->lock_nested && current->pid == eb->lock_owner)
25 return;
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
33void btrfs_set_lock_blocking_write(struct extent_buffer *eb)
925baedd 34{
ea4ebde0 35 /*
b95be2d9
DS
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.
ea4ebde0
CM
39 */
40 if (eb->lock_nested && current->pid == eb->lock_owner)
41 return;
b95be2d9
DS
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);
b4ce94de 48 }
b4ce94de 49}
f9efa9c7 50
aa12c027
DS
51void btrfs_clear_lock_blocking_read(struct extent_buffer *eb)
52{
53 /*
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.
57 */
58 if (eb->lock_nested && current->pid == eb->lock_owner)
59 return;
60 BUG_ON(atomic_read(&eb->blocking_readers) == 0);
61 read_lock(&eb->lock);
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);
66}
67
68void btrfs_clear_lock_blocking_write(struct extent_buffer *eb)
b4ce94de 69{
ea4ebde0
CM
70 /*
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.
75 */
76 if (eb->lock_nested && current->pid == eb->lock_owner)
77 return;
aa12c027
DS
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);
b4ce94de
CM
85}
86
87/*
bd681513
CM
88 * take a spinning read lock. This will wait for any blocking
89 * writers
b4ce94de 90 */
bd681513 91void btrfs_tree_read_lock(struct extent_buffer *eb)
b4ce94de 92{
bd681513 93again:
ea4ebde0
CM
94 BUG_ON(!atomic_read(&eb->blocking_writers) &&
95 current->pid == eb->lock_owner);
96
5b25f70f
AJ
97 read_lock(&eb->lock);
98 if (atomic_read(&eb->blocking_writers) &&
99 current->pid == eb->lock_owner) {
100 /*
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.
105 */
106 BUG_ON(eb->lock_nested);
107 eb->lock_nested = 1;
108 read_unlock(&eb->lock);
109 return;
110 }
bd681513
CM
111 if (atomic_read(&eb->blocking_writers)) {
112 read_unlock(&eb->lock);
39f9d028
LB
113 wait_event(eb->write_lock_wq,
114 atomic_read(&eb->blocking_writers) == 0);
bd681513 115 goto again;
b4ce94de 116 }
bd681513
CM
117 atomic_inc(&eb->read_locks);
118 atomic_inc(&eb->spinning_readers);
b4ce94de
CM
119}
120
f82c458a
CM
121/*
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
125 */
126int btrfs_tree_read_lock_atomic(struct extent_buffer *eb)
127{
128 if (atomic_read(&eb->blocking_writers))
129 return 0;
130
131 read_lock(&eb->lock);
132 if (atomic_read(&eb->blocking_writers)) {
133 read_unlock(&eb->lock);
134 return 0;
135 }
136 atomic_inc(&eb->read_locks);
137 atomic_inc(&eb->spinning_readers);
138 return 1;
139}
140
b4ce94de 141/*
bd681513
CM
142 * returns 1 if we get the read lock and 0 if we don't
143 * this won't wait for blocking writers
b4ce94de 144 */
bd681513 145int btrfs_try_tree_read_lock(struct extent_buffer *eb)
b4ce94de 146{
bd681513
CM
147 if (atomic_read(&eb->blocking_writers))
148 return 0;
b4ce94de 149
ea4ebde0
CM
150 if (!read_trylock(&eb->lock))
151 return 0;
152
bd681513
CM
153 if (atomic_read(&eb->blocking_writers)) {
154 read_unlock(&eb->lock);
155 return 0;
b9473439 156 }
bd681513
CM
157 atomic_inc(&eb->read_locks);
158 atomic_inc(&eb->spinning_readers);
159 return 1;
b4ce94de
CM
160}
161
162/*
bd681513
CM
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
b4ce94de 165 */
bd681513 166int btrfs_try_tree_write_lock(struct extent_buffer *eb)
b4ce94de 167{
bd681513
CM
168 if (atomic_read(&eb->blocking_writers) ||
169 atomic_read(&eb->blocking_readers))
170 return 0;
ea4ebde0 171
f82c458a 172 write_lock(&eb->lock);
bd681513
CM
173 if (atomic_read(&eb->blocking_writers) ||
174 atomic_read(&eb->blocking_readers)) {
175 write_unlock(&eb->lock);
176 return 0;
177 }
178 atomic_inc(&eb->write_locks);
179 atomic_inc(&eb->spinning_writers);
5b25f70f 180 eb->lock_owner = current->pid;
b4ce94de
CM
181 return 1;
182}
183
184/*
bd681513
CM
185 * drop a spinning read lock
186 */
187void btrfs_tree_read_unlock(struct extent_buffer *eb)
188{
ea4ebde0
CM
189 /*
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.
194 */
195 if (eb->lock_nested && current->pid == eb->lock_owner) {
196 eb->lock_nested = 0;
197 return;
5b25f70f 198 }
bd681513
CM
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);
204}
205
206/*
207 * drop a blocking read lock
208 */
209void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
210{
ea4ebde0
CM
211 /*
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.
216 */
217 if (eb->lock_nested && current->pid == eb->lock_owner) {
218 eb->lock_nested = 0;
219 return;
5b25f70f 220 }
bd681513
CM
221 btrfs_assert_tree_read_locked(eb);
222 WARN_ON(atomic_read(&eb->blocking_readers) == 0);
093258e6
DS
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);
bd681513
CM
226 atomic_dec(&eb->read_locks);
227}
228
229/*
230 * take a spinning write lock. This will wait for both
231 * blocking readers or writers
b4ce94de 232 */
143bede5 233void btrfs_tree_lock(struct extent_buffer *eb)
b4ce94de 234{
166f66d0 235 WARN_ON(eb->lock_owner == current->pid);
bd681513
CM
236again:
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);
970e74d9
DS
240 if (atomic_read(&eb->blocking_readers) ||
241 atomic_read(&eb->blocking_writers)) {
bd681513 242 write_unlock(&eb->lock);
bd681513
CM
243 goto again;
244 }
245 WARN_ON(atomic_read(&eb->spinning_writers));
246 atomic_inc(&eb->spinning_writers);
247 atomic_inc(&eb->write_locks);
5b25f70f 248 eb->lock_owner = current->pid;
925baedd
CM
249}
250
bd681513
CM
251/*
252 * drop a spinning or a blocking write lock.
253 */
143bede5 254void btrfs_tree_unlock(struct extent_buffer *eb)
925baedd 255{
bd681513
CM
256 int blockers = atomic_read(&eb->blocking_writers);
257
258 BUG_ON(blockers > 1);
259
260 btrfs_assert_tree_locked(eb);
ea4ebde0 261 eb->lock_owner = 0;
bd681513
CM
262 atomic_dec(&eb->write_locks);
263
264 if (blockers) {
265 WARN_ON(atomic_read(&eb->spinning_writers));
266 atomic_dec(&eb->blocking_writers);
093258e6 267 /* Use the lighter barrier after atomic */
2e32ef87 268 smp_mb__after_atomic();
093258e6 269 cond_wake_up_nomb(&eb->write_lock_wq);
bd681513
CM
270 } else {
271 WARN_ON(atomic_read(&eb->spinning_writers) != 1);
272 atomic_dec(&eb->spinning_writers);
273 write_unlock(&eb->lock);
274 }
925baedd
CM
275}
276
b9447ef8 277void btrfs_assert_tree_locked(struct extent_buffer *eb)
925baedd 278{
bd681513
CM
279 BUG_ON(!atomic_read(&eb->write_locks));
280}
281
48a3b636 282static void btrfs_assert_tree_read_locked(struct extent_buffer *eb)
bd681513
CM
283{
284 BUG_ON(!atomic_read(&eb->read_locks));
925baedd 285}