xref: /linux/fs/btrfs/locking.h (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
19888c340SDavid Sterba /* SPDX-License-Identifier: GPL-2.0 */
2925baeddSChris Mason /*
3925baeddSChris Mason  * Copyright (C) 2008 Oracle.  All rights reserved.
4925baeddSChris Mason  */
5925baeddSChris Mason 
69888c340SDavid Sterba #ifndef BTRFS_LOCKING_H
79888c340SDavid Sterba #define BTRFS_LOCKING_H
8925baeddSChris Mason 
92992df73SNikolay Borisov #include <linux/atomic.h>
102992df73SNikolay Borisov #include <linux/wait.h>
11602035d7SDavid Sterba #include <linux/lockdep.h>
122992df73SNikolay Borisov #include <linux/percpu_counter.h>
1331f6e769SDavid Sterba #include "extent_io.h"
14602035d7SDavid Sterba 
15602035d7SDavid Sterba struct extent_buffer;
16602035d7SDavid Sterba struct btrfs_path;
17602035d7SDavid Sterba struct btrfs_root;
1831f6e769SDavid Sterba 
19bd681513SChris Mason #define BTRFS_WRITE_LOCK 1
20bd681513SChris Mason #define BTRFS_READ_LOCK 2
21bd681513SChris Mason 
22fd7ba1c1SJosef Bacik /*
23fd7ba1c1SJosef Bacik  * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
24fd7ba1c1SJosef Bacik  * the time of this patch is 8, which is how many we use.  Keep this in mind if
25fd7ba1c1SJosef Bacik  * you decide you want to add another subclass.
26fd7ba1c1SJosef Bacik  */
27fd7ba1c1SJosef Bacik enum btrfs_lock_nesting {
28fd7ba1c1SJosef Bacik 	BTRFS_NESTING_NORMAL,
29fd7ba1c1SJosef Bacik 
30fd7ba1c1SJosef Bacik 	/*
319631e4ccSJosef Bacik 	 * When we COW a block we are holding the lock on the original block,
329631e4ccSJosef Bacik 	 * and since our lockdep maps are rootid+level, this confuses lockdep
339631e4ccSJosef Bacik 	 * when we lock the newly allocated COW'd block.  Handle this by having
349631e4ccSJosef Bacik 	 * a subclass for COW'ed blocks so that lockdep doesn't complain.
359631e4ccSJosef Bacik 	 */
369631e4ccSJosef Bacik 	BTRFS_NESTING_COW,
379631e4ccSJosef Bacik 
389631e4ccSJosef Bacik 	/*
39bf77467aSJosef Bacik 	 * Oftentimes we need to lock adjacent nodes on the same level while
40bf77467aSJosef Bacik 	 * still holding the lock on the original node we searched to, such as
41bf77467aSJosef Bacik 	 * for searching forward or for split/balance.
42bf77467aSJosef Bacik 	 *
43bf77467aSJosef Bacik 	 * Because of this we need to indicate to lockdep that this is
44bf77467aSJosef Bacik 	 * acceptable by having a different subclass for each of these
45bf77467aSJosef Bacik 	 * operations.
46bf77467aSJosef Bacik 	 */
47bf77467aSJosef Bacik 	BTRFS_NESTING_LEFT,
48bf77467aSJosef Bacik 	BTRFS_NESTING_RIGHT,
49bf77467aSJosef Bacik 
50bf77467aSJosef Bacik 	/*
51bf59a5a2SJosef Bacik 	 * When splitting we will be holding a lock on the left/right node when
52bf59a5a2SJosef Bacik 	 * we need to cow that node, thus we need a new set of subclasses for
53bf59a5a2SJosef Bacik 	 * these two operations.
54bf59a5a2SJosef Bacik 	 */
55bf59a5a2SJosef Bacik 	BTRFS_NESTING_LEFT_COW,
56bf59a5a2SJosef Bacik 	BTRFS_NESTING_RIGHT_COW,
57bf59a5a2SJosef Bacik 
58bf59a5a2SJosef Bacik 	/*
594dff97e6SJosef Bacik 	 * When splitting we may push nodes to the left or right, but still use
604dff97e6SJosef Bacik 	 * the subsequent nodes in our path, keeping our locks on those adjacent
614dff97e6SJosef Bacik 	 * blocks.  Thus when we go to allocate a new split block we've already
624dff97e6SJosef Bacik 	 * used up all of our available subclasses, so this subclass exists to
634dff97e6SJosef Bacik 	 * handle this case where we need to allocate a new split block.
644dff97e6SJosef Bacik 	 */
654dff97e6SJosef Bacik 	BTRFS_NESTING_SPLIT,
664dff97e6SJosef Bacik 
674dff97e6SJosef Bacik 	/*
68cf6f34aaSJosef Bacik 	 * When promoting a new block to a root we need to have a special
69cf6f34aaSJosef Bacik 	 * subclass so we don't confuse lockdep, as it will appear that we are
70cf6f34aaSJosef Bacik 	 * locking a higher level node before a lower level one.  Copying also
71cf6f34aaSJosef Bacik 	 * has this problem as it appears we're locking the same block again
72cf6f34aaSJosef Bacik 	 * when we make a snapshot of an existing root.
73cf6f34aaSJosef Bacik 	 */
74cf6f34aaSJosef Bacik 	BTRFS_NESTING_NEW_ROOT,
75cf6f34aaSJosef Bacik 
76cf6f34aaSJosef Bacik 	/*
77fd7ba1c1SJosef Bacik 	 * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
78fd7ba1c1SJosef Bacik 	 * add this in here and add a static_assert to keep us from going over
79fd7ba1c1SJosef Bacik 	 * the limit.  As of this writing we're limited to 8, and we're
80fd7ba1c1SJosef Bacik 	 * definitely using 8, hence this check to keep us from messing up in
81fd7ba1c1SJosef Bacik 	 * the future.
82fd7ba1c1SJosef Bacik 	 */
83fd7ba1c1SJosef Bacik 	BTRFS_NESTING_MAX,
84fd7ba1c1SJosef Bacik };
85fd7ba1c1SJosef Bacik 
86eb33a4d6SJosef Bacik enum btrfs_lockdep_trans_states {
8777d20c68SJosef Bacik 	BTRFS_LOCKDEP_TRANS_COMMIT_PREP,
88eb33a4d6SJosef Bacik 	BTRFS_LOCKDEP_TRANS_UNBLOCKED,
89eb33a4d6SJosef Bacik 	BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED,
90eb33a4d6SJosef Bacik 	BTRFS_LOCKDEP_TRANS_COMPLETED,
91eb33a4d6SJosef Bacik };
92eb33a4d6SJosef Bacik 
93eb33a4d6SJosef Bacik /*
94eb33a4d6SJosef Bacik  * Lockdep annotation for wait events.
95eb33a4d6SJosef Bacik  *
96eb33a4d6SJosef Bacik  * @owner:  The struct where the lockdep map is defined
97eb33a4d6SJosef Bacik  * @lock:   The lockdep map corresponding to a wait event
98eb33a4d6SJosef Bacik  *
99eb33a4d6SJosef Bacik  * This macro is used to annotate a wait event. In this case a thread acquires
100eb33a4d6SJosef Bacik  * the lockdep map as writer (exclusive lock) because it has to block until all
101eb33a4d6SJosef Bacik  * the threads that hold the lock as readers signal the condition for the wait
102eb33a4d6SJosef Bacik  * event and release their locks.
103eb33a4d6SJosef Bacik  */
104eb33a4d6SJosef Bacik #define btrfs_might_wait_for_event(owner, lock)					\
105eb33a4d6SJosef Bacik 	do {									\
106eb33a4d6SJosef Bacik 		rwsem_acquire(&owner->lock##_map, 0, 0, _THIS_IP_);		\
107eb33a4d6SJosef Bacik 		rwsem_release(&owner->lock##_map, _THIS_IP_);			\
108eb33a4d6SJosef Bacik 	} while (0)
109eb33a4d6SJosef Bacik 
110eb33a4d6SJosef Bacik /*
111eb33a4d6SJosef Bacik  * Protection for the resource/condition of a wait event.
112eb33a4d6SJosef Bacik  *
113eb33a4d6SJosef Bacik  * @owner:  The struct where the lockdep map is defined
114eb33a4d6SJosef Bacik  * @lock:   The lockdep map corresponding to a wait event
115eb33a4d6SJosef Bacik  *
116eb33a4d6SJosef Bacik  * Many threads can modify the condition for the wait event at the same time
117eb33a4d6SJosef Bacik  * and signal the threads that block on the wait event. The threads that modify
118eb33a4d6SJosef Bacik  * the condition and do the signaling acquire the lock as readers (shared
119eb33a4d6SJosef Bacik  * lock).
120eb33a4d6SJosef Bacik  */
121eb33a4d6SJosef Bacik #define btrfs_lockdep_acquire(owner, lock)					\
122eb33a4d6SJosef Bacik 	rwsem_acquire_read(&owner->lock##_map, 0, 0, _THIS_IP_)
123eb33a4d6SJosef Bacik 
124eb33a4d6SJosef Bacik /*
125eb33a4d6SJosef Bacik  * Used after signaling the condition for a wait event to release the lockdep
126eb33a4d6SJosef Bacik  * map held by a reader thread.
127eb33a4d6SJosef Bacik  */
128eb33a4d6SJosef Bacik #define btrfs_lockdep_release(owner, lock)					\
129eb33a4d6SJosef Bacik 	rwsem_release(&owner->lock##_map, _THIS_IP_)
130eb33a4d6SJosef Bacik 
131eb33a4d6SJosef Bacik /*
132eb33a4d6SJosef Bacik  * Macros for the transaction states wait events, similar to the generic wait
133eb33a4d6SJosef Bacik  * event macros.
134eb33a4d6SJosef Bacik  */
135eb33a4d6SJosef Bacik #define btrfs_might_wait_for_state(owner, i)					\
136eb33a4d6SJosef Bacik 	do {									\
137eb33a4d6SJosef Bacik 		rwsem_acquire(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_); \
138eb33a4d6SJosef Bacik 		rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_);	\
139eb33a4d6SJosef Bacik 	} while (0)
140eb33a4d6SJosef Bacik 
141eb33a4d6SJosef Bacik #define btrfs_trans_state_lockdep_acquire(owner, i)				\
142eb33a4d6SJosef Bacik 	rwsem_acquire_read(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_)
143eb33a4d6SJosef Bacik 
144eb33a4d6SJosef Bacik #define btrfs_trans_state_lockdep_release(owner, i)				\
145eb33a4d6SJosef Bacik 	rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_)
146eb33a4d6SJosef Bacik 
147eb33a4d6SJosef Bacik /* Initialization of the lockdep map */
148eb33a4d6SJosef Bacik #define btrfs_lockdep_init_map(owner, lock)					\
149eb33a4d6SJosef Bacik 	do {									\
150eb33a4d6SJosef Bacik 		static struct lock_class_key lock##_key;			\
151eb33a4d6SJosef Bacik 		lockdep_init_map(&owner->lock##_map, #lock, &lock##_key, 0);	\
152eb33a4d6SJosef Bacik 	} while (0)
153eb33a4d6SJosef Bacik 
154eb33a4d6SJosef Bacik /* Initialization of the transaction states lockdep maps. */
155eb33a4d6SJosef Bacik #define btrfs_state_lockdep_init_map(owner, lock, state)			\
156eb33a4d6SJosef Bacik 	do {									\
157eb33a4d6SJosef Bacik 		static struct lock_class_key lock##_key;			\
158eb33a4d6SJosef Bacik 		lockdep_init_map(&owner->btrfs_state_change_map[state], #lock,	\
159eb33a4d6SJosef Bacik 				 &lock##_key, 0);				\
160eb33a4d6SJosef Bacik 	} while (0)
161eb33a4d6SJosef Bacik 
162fd7ba1c1SJosef Bacik static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
163fd7ba1c1SJosef Bacik 	      "too many lock subclasses defined");
164fd7ba1c1SJosef Bacik 
165*2066bbfcSFilipe Manana void btrfs_tree_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
166f40ca9cbSFilipe Manana 
btrfs_tree_lock(struct extent_buffer * eb)167f40ca9cbSFilipe Manana static inline void btrfs_tree_lock(struct extent_buffer *eb)
168f40ca9cbSFilipe Manana {
169*2066bbfcSFilipe Manana 	btrfs_tree_lock_nested(eb, BTRFS_NESTING_NORMAL);
170f40ca9cbSFilipe Manana }
171f40ca9cbSFilipe Manana 
172143bede5SJeff Mahoney void btrfs_tree_unlock(struct extent_buffer *eb);
173b4ce94deSChris Mason 
174*2066bbfcSFilipe Manana void btrfs_tree_read_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
175f40ca9cbSFilipe Manana 
btrfs_tree_read_lock(struct extent_buffer * eb)176f40ca9cbSFilipe Manana static inline void btrfs_tree_read_lock(struct extent_buffer *eb)
177f40ca9cbSFilipe Manana {
178*2066bbfcSFilipe Manana 	btrfs_tree_read_lock_nested(eb, BTRFS_NESTING_NORMAL);
179f40ca9cbSFilipe Manana }
180f40ca9cbSFilipe Manana 
181bd681513SChris Mason void btrfs_tree_read_unlock(struct extent_buffer *eb);
182bd681513SChris Mason int btrfs_try_tree_read_lock(struct extent_buffer *eb);
183bd681513SChris Mason int btrfs_try_tree_write_lock(struct extent_buffer *eb);
18451899412SJosef Bacik struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
1851bb96598SJosef Bacik struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
186857bc13fSJosef Bacik struct extent_buffer *btrfs_try_read_lock_root_node(struct btrfs_root *root);
187f82c458aSChris Mason 
18831f6e769SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
btrfs_assert_tree_write_locked(struct extent_buffer * eb)18949d0c642SFilipe Manana static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb)
19049d0c642SFilipe Manana {
19149d0c642SFilipe Manana 	lockdep_assert_held_write(&eb->lock);
19231f6e769SDavid Sterba }
19331f6e769SDavid Sterba #else
btrfs_assert_tree_write_locked(struct extent_buffer * eb)19449d0c642SFilipe Manana static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { }
19531f6e769SDavid Sterba #endif
196bd681513SChris Mason 
1971f95ec01SDavid Sterba void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
198ed2b1d36SDavid Sterba 
btrfs_tree_unlock_rw(struct extent_buffer * eb,int rw)199bd681513SChris Mason static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
200bd681513SChris Mason {
201ac5887c8SJosef Bacik 	if (rw == BTRFS_WRITE_LOCK)
202bd681513SChris Mason 		btrfs_tree_unlock(eb);
203bd681513SChris Mason 	else if (rw == BTRFS_READ_LOCK)
204bd681513SChris Mason 		btrfs_tree_read_unlock(eb);
205bd681513SChris Mason 	else
206bd681513SChris Mason 		BUG();
207bd681513SChris Mason }
208bd681513SChris Mason 
2092992df73SNikolay Borisov struct btrfs_drew_lock {
2102992df73SNikolay Borisov 	atomic_t readers;
2110b548539SDavid Sterba 	atomic_t writers;
2122992df73SNikolay Borisov 	wait_queue_head_t pending_writers;
2132992df73SNikolay Borisov 	wait_queue_head_t pending_readers;
2142992df73SNikolay Borisov };
2152992df73SNikolay Borisov 
2160b548539SDavid Sterba void btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
2172992df73SNikolay Borisov void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
2182992df73SNikolay Borisov bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
2192992df73SNikolay Borisov void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
2202992df73SNikolay Borisov void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
2212992df73SNikolay Borisov void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
2222992df73SNikolay Borisov 
2230a27a047SJosef Bacik #ifdef CONFIG_DEBUG_LOCK_ALLOC
2240a27a047SJosef Bacik void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level);
225b40130b2SJosef Bacik void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buffer *eb);
2260a27a047SJosef Bacik #else
btrfs_set_buffer_lockdep_class(u64 objectid,struct extent_buffer * eb,int level)2270a27a047SJosef Bacik static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
2280a27a047SJosef Bacik 					struct extent_buffer *eb, int level)
2290a27a047SJosef Bacik {
2300a27a047SJosef Bacik }
btrfs_maybe_reset_lockdep_class(struct btrfs_root * root,struct extent_buffer * eb)231b40130b2SJosef Bacik static inline void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root,
232b40130b2SJosef Bacik 						   struct extent_buffer *eb)
233b40130b2SJosef Bacik {
234b40130b2SJosef Bacik }
2350a27a047SJosef Bacik #endif
2360a27a047SJosef Bacik 
237925baeddSChris Mason #endif
238