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>
13 #include "extent_io.h"
17 * Extent buffer locking
18 * =====================
20 * We use a rw_semaphore for tree locking, and the semantics are exactly the
23 * - reader/writer exclusion
24 * - writer/writer exclusion
25 * - reader/reader sharing
26 * - try-lock semantics for readers and writers
28 * The rwsem implementation does opportunistic spinning which reduces number of
29 * times the locking task needs to sleep.
33 * __btrfs_tree_read_lock - lock extent buffer for read
34 * @eb: the eb to be locked
35 * @nest: the nesting level to be used for lockdep
37 * This takes the read lock on the extent buffer, using the specified nesting
38 * level for lockdep purposes.
40 void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
44 if (trace_btrfs_tree_read_lock_enabled())
45 start_ns = ktime_get_ns();
47 down_read_nested(&eb->lock, nest);
48 trace_btrfs_tree_read_lock(eb, start_ns);
51 void btrfs_tree_read_lock(struct extent_buffer *eb)
53 __btrfs_tree_read_lock(eb, BTRFS_NESTING_NORMAL);
59 * Return 1 if the rwlock has been taken, 0 otherwise
61 int btrfs_try_tree_read_lock(struct extent_buffer *eb)
63 if (down_read_trylock(&eb->lock)) {
64 trace_btrfs_try_tree_read_lock(eb);
73 * Return 1 if the rwlock has been taken, 0 otherwise
75 int btrfs_try_tree_write_lock(struct extent_buffer *eb)
77 if (down_write_trylock(&eb->lock)) {
78 eb->lock_owner = current->pid;
79 trace_btrfs_try_tree_write_lock(eb);
88 void btrfs_tree_read_unlock(struct extent_buffer *eb)
90 trace_btrfs_tree_read_unlock(eb);
95 * __btrfs_tree_lock - lock eb for write
97 * @nest: the nesting to use for the lock
99 * Returns with the eb->lock write locked.
101 void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
102 __acquires(&eb->lock)
106 if (trace_btrfs_tree_lock_enabled())
107 start_ns = ktime_get_ns();
109 down_write_nested(&eb->lock, nest);
110 eb->lock_owner = current->pid;
111 trace_btrfs_tree_lock(eb, start_ns);
114 void btrfs_tree_lock(struct extent_buffer *eb)
116 __btrfs_tree_lock(eb, BTRFS_NESTING_NORMAL);
120 * Release the write lock.
122 void btrfs_tree_unlock(struct extent_buffer *eb)
124 trace_btrfs_tree_unlock(eb);
130 * This releases any locks held in the path starting at level and going all the
131 * way up to the root.
133 * btrfs_search_slot will keep the lock held on higher nodes in a few corner
134 * cases, such as COW of the block at slot zero in the node. This ignores
135 * those rules, and it should only be called when there are no more updates to
136 * be done higher up in the tree.
138 void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
142 if (path->keep_locks)
145 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
150 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
156 * Loop around taking references on and locking the root node of the tree until
157 * we end up with a lock on the root node.
159 * Return: root extent buffer with write lock held
161 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
163 struct extent_buffer *eb;
166 eb = btrfs_root_node(root);
168 if (eb == root->node)
170 btrfs_tree_unlock(eb);
171 free_extent_buffer(eb);
177 * Loop around taking references on and locking the root node of the tree until
178 * we end up with a lock on the root node.
180 * Return: root extent buffer with read lock held
182 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
184 struct extent_buffer *eb;
187 eb = btrfs_root_node(root);
188 btrfs_tree_read_lock(eb);
189 if (eb == root->node)
191 btrfs_tree_read_unlock(eb);
192 free_extent_buffer(eb);
201 * DREW stands for double-reader-writer-exclusion lock. It's used in situation
202 * where you want to provide A-B exclusion but not AA or BB.
204 * Currently implementation gives more priority to reader. If a reader and a
205 * writer both race to acquire their respective sides of the lock the writer
206 * would yield its lock as soon as it detects a concurrent reader. Additionally
207 * if there are pending readers no new writers would be allowed to come in and
211 int btrfs_drew_lock_init(struct btrfs_drew_lock *lock)
215 ret = percpu_counter_init(&lock->writers, 0, GFP_KERNEL);
219 atomic_set(&lock->readers, 0);
220 init_waitqueue_head(&lock->pending_readers);
221 init_waitqueue_head(&lock->pending_writers);
226 void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock)
228 percpu_counter_destroy(&lock->writers);
231 /* Return true if acquisition is successful, false otherwise */
232 bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock)
234 if (atomic_read(&lock->readers))
237 percpu_counter_inc(&lock->writers);
239 /* Ensure writers count is updated before we check for pending readers */
241 if (atomic_read(&lock->readers)) {
242 btrfs_drew_write_unlock(lock);
249 void btrfs_drew_write_lock(struct btrfs_drew_lock *lock)
252 if (btrfs_drew_try_write_lock(lock))
254 wait_event(lock->pending_writers, !atomic_read(&lock->readers));
258 void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock)
260 percpu_counter_dec(&lock->writers);
261 cond_wake_up(&lock->pending_readers);
264 void btrfs_drew_read_lock(struct btrfs_drew_lock *lock)
266 atomic_inc(&lock->readers);
269 * Ensure the pending reader count is perceieved BEFORE this reader
270 * goes to sleep in case of active writers. This guarantees new writers
271 * won't be allowed and that the current reader will be woken up when
272 * the last active writer finishes its jobs.
274 smp_mb__after_atomic();
276 wait_event(lock->pending_readers,
277 percpu_counter_sum(&lock->writers) == 0);
280 void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock)
283 * atomic_dec_and_test implies a full barrier, so woken up writers
284 * are guaranteed to see the decrement
286 if (atomic_dec_and_test(&lock->readers))
287 wake_up(&lock->pending_writers);