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 * Lockdep class keys for extent_buffer->lock's in this root. For a given
18 * eb, the lockdep key is determined by the btrfs_root it belongs to and
19 * the level the eb occupies in the tree.
21 * Different roots are used for different purposes and may nest inside each
22 * other and they require separate keysets. As lockdep keys should be
23 * static, assign keysets according to the purpose of the root as indicated
24 * by btrfs_root->root_key.objectid. This ensures that all special purpose
25 * roots have separate keysets.
27 * Lock-nesting across peer nodes is always done with the immediate parent
28 * node locked thus preventing deadlock. As lockdep doesn't know this, use
29 * subclass to avoid triggering lockdep warning in such cases.
31 * The key is set by the readpage_end_io_hook after the buffer has passed
32 * csum validation but before the pages are unlocked. It is also set by
33 * btrfs_init_new_buffer on freshly allocated blocks.
35 * We also add a check to make sure the highest level of the tree is the
36 * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
37 * needs update as well.
39 #ifdef CONFIG_DEBUG_LOCK_ALLOC
40 #if BTRFS_MAX_LEVEL != 8
44 #define DEFINE_LEVEL(stem, level) \
45 .names[level] = "btrfs-" stem "-0" #level,
47 #define DEFINE_NAME(stem) \
48 DEFINE_LEVEL(stem, 0) \
49 DEFINE_LEVEL(stem, 1) \
50 DEFINE_LEVEL(stem, 2) \
51 DEFINE_LEVEL(stem, 3) \
52 DEFINE_LEVEL(stem, 4) \
53 DEFINE_LEVEL(stem, 5) \
54 DEFINE_LEVEL(stem, 6) \
57 static struct btrfs_lockdep_keyset {
58 u64 id; /* root objectid */
59 /* Longest entry: btrfs-free-space-00 */
60 char names[BTRFS_MAX_LEVEL][20];
61 struct lock_class_key keys[BTRFS_MAX_LEVEL];
62 } btrfs_lockdep_keysets[] = {
63 { .id = BTRFS_ROOT_TREE_OBJECTID, DEFINE_NAME("root") },
64 { .id = BTRFS_EXTENT_TREE_OBJECTID, DEFINE_NAME("extent") },
65 { .id = BTRFS_CHUNK_TREE_OBJECTID, DEFINE_NAME("chunk") },
66 { .id = BTRFS_DEV_TREE_OBJECTID, DEFINE_NAME("dev") },
67 { .id = BTRFS_CSUM_TREE_OBJECTID, DEFINE_NAME("csum") },
68 { .id = BTRFS_QUOTA_TREE_OBJECTID, DEFINE_NAME("quota") },
69 { .id = BTRFS_TREE_LOG_OBJECTID, DEFINE_NAME("log") },
70 { .id = BTRFS_TREE_RELOC_OBJECTID, DEFINE_NAME("treloc") },
71 { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, DEFINE_NAME("dreloc") },
72 { .id = BTRFS_UUID_TREE_OBJECTID, DEFINE_NAME("uuid") },
73 { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, DEFINE_NAME("free-space") },
74 { .id = 0, DEFINE_NAME("tree") },
80 void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level)
82 struct btrfs_lockdep_keyset *ks;
84 BUG_ON(level >= ARRAY_SIZE(ks->keys));
86 /* Find the matching keyset, id 0 is the default entry */
87 for (ks = btrfs_lockdep_keysets; ks->id; ks++)
88 if (ks->id == objectid)
91 lockdep_set_class_and_name(&eb->lock, &ks->keys[level], ks->names[level]);
97 * Extent buffer locking
98 * =====================
100 * We use a rw_semaphore for tree locking, and the semantics are exactly the
103 * - reader/writer exclusion
104 * - writer/writer exclusion
105 * - reader/reader sharing
106 * - try-lock semantics for readers and writers
108 * The rwsem implementation does opportunistic spinning which reduces number of
109 * times the locking task needs to sleep.
113 * __btrfs_tree_read_lock - lock extent buffer for read
114 * @eb: the eb to be locked
115 * @nest: the nesting level to be used for lockdep
117 * This takes the read lock on the extent buffer, using the specified nesting
118 * level for lockdep purposes.
120 void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
124 if (trace_btrfs_tree_read_lock_enabled())
125 start_ns = ktime_get_ns();
127 down_read_nested(&eb->lock, nest);
128 trace_btrfs_tree_read_lock(eb, start_ns);
131 void btrfs_tree_read_lock(struct extent_buffer *eb)
133 __btrfs_tree_read_lock(eb, BTRFS_NESTING_NORMAL);
139 * Return 1 if the rwlock has been taken, 0 otherwise
141 int btrfs_try_tree_read_lock(struct extent_buffer *eb)
143 if (down_read_trylock(&eb->lock)) {
144 trace_btrfs_try_tree_read_lock(eb);
151 * Try-lock for write.
153 * Return 1 if the rwlock has been taken, 0 otherwise
155 int btrfs_try_tree_write_lock(struct extent_buffer *eb)
157 if (down_write_trylock(&eb->lock)) {
158 eb->lock_owner = current->pid;
159 trace_btrfs_try_tree_write_lock(eb);
168 void btrfs_tree_read_unlock(struct extent_buffer *eb)
170 trace_btrfs_tree_read_unlock(eb);
175 * __btrfs_tree_lock - lock eb for write
176 * @eb: the eb to lock
177 * @nest: the nesting to use for the lock
179 * Returns with the eb->lock write locked.
181 void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest)
182 __acquires(&eb->lock)
186 if (trace_btrfs_tree_lock_enabled())
187 start_ns = ktime_get_ns();
189 down_write_nested(&eb->lock, nest);
190 eb->lock_owner = current->pid;
191 trace_btrfs_tree_lock(eb, start_ns);
194 void btrfs_tree_lock(struct extent_buffer *eb)
196 __btrfs_tree_lock(eb, BTRFS_NESTING_NORMAL);
200 * Release the write lock.
202 void btrfs_tree_unlock(struct extent_buffer *eb)
204 trace_btrfs_tree_unlock(eb);
210 * This releases any locks held in the path starting at level and going all the
211 * way up to the root.
213 * btrfs_search_slot will keep the lock held on higher nodes in a few corner
214 * cases, such as COW of the block at slot zero in the node. This ignores
215 * those rules, and it should only be called when there are no more updates to
216 * be done higher up in the tree.
218 void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
222 if (path->keep_locks)
225 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
230 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
236 * Loop around taking references on and locking the root node of the tree until
237 * we end up with a lock on the root node.
239 * Return: root extent buffer with write lock held
241 struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
243 struct extent_buffer *eb;
246 eb = btrfs_root_node(root);
248 if (eb == root->node)
250 btrfs_tree_unlock(eb);
251 free_extent_buffer(eb);
257 * Loop around taking references on and locking the root node of the tree until
258 * we end up with a lock on the root node.
260 * Return: root extent buffer with read lock held
262 struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
264 struct extent_buffer *eb;
267 eb = btrfs_root_node(root);
268 btrfs_tree_read_lock(eb);
269 if (eb == root->node)
271 btrfs_tree_read_unlock(eb);
272 free_extent_buffer(eb);
281 * DREW stands for double-reader-writer-exclusion lock. It's used in situation
282 * where you want to provide A-B exclusion but not AA or BB.
284 * Currently implementation gives more priority to reader. If a reader and a
285 * writer both race to acquire their respective sides of the lock the writer
286 * would yield its lock as soon as it detects a concurrent reader. Additionally
287 * if there are pending readers no new writers would be allowed to come in and
291 int btrfs_drew_lock_init(struct btrfs_drew_lock *lock)
295 ret = percpu_counter_init(&lock->writers, 0, GFP_KERNEL);
299 atomic_set(&lock->readers, 0);
300 init_waitqueue_head(&lock->pending_readers);
301 init_waitqueue_head(&lock->pending_writers);
306 void btrfs_drew_lock_destroy(struct btrfs_drew_lock *lock)
308 percpu_counter_destroy(&lock->writers);
311 /* Return true if acquisition is successful, false otherwise */
312 bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock)
314 if (atomic_read(&lock->readers))
317 percpu_counter_inc(&lock->writers);
319 /* Ensure writers count is updated before we check for pending readers */
321 if (atomic_read(&lock->readers)) {
322 btrfs_drew_write_unlock(lock);
329 void btrfs_drew_write_lock(struct btrfs_drew_lock *lock)
332 if (btrfs_drew_try_write_lock(lock))
334 wait_event(lock->pending_writers, !atomic_read(&lock->readers));
338 void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock)
340 percpu_counter_dec(&lock->writers);
341 cond_wake_up(&lock->pending_readers);
344 void btrfs_drew_read_lock(struct btrfs_drew_lock *lock)
346 atomic_inc(&lock->readers);
349 * Ensure the pending reader count is perceieved BEFORE this reader
350 * goes to sleep in case of active writers. This guarantees new writers
351 * won't be allowed and that the current reader will be woken up when
352 * the last active writer finishes its jobs.
354 smp_mb__after_atomic();
356 wait_event(lock->pending_readers,
357 percpu_counter_sum(&lock->writers) == 0);
360 void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock)
363 * atomic_dec_and_test implies a full barrier, so woken up writers
364 * are guaranteed to see the decrement
366 if (atomic_dec_and_test(&lock->readers))
367 wake_up(&lock->pending_writers);