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>
112992df73SNikolay Borisov #include <linux/percpu_counter.h>
1231f6e769SDavid Sterba #include "extent_io.h"
1331f6e769SDavid Sterba
14bd681513SChris Mason #define BTRFS_WRITE_LOCK 1
15bd681513SChris Mason #define BTRFS_READ_LOCK 2
16bd681513SChris Mason
17fd7ba1c1SJosef Bacik /*
18fd7ba1c1SJosef Bacik * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
19fd7ba1c1SJosef Bacik * the time of this patch is 8, which is how many we use. Keep this in mind if
20fd7ba1c1SJosef Bacik * you decide you want to add another subclass.
21fd7ba1c1SJosef Bacik */
22fd7ba1c1SJosef Bacik enum btrfs_lock_nesting {
23fd7ba1c1SJosef Bacik BTRFS_NESTING_NORMAL,
24fd7ba1c1SJosef Bacik
25fd7ba1c1SJosef Bacik /*
269631e4ccSJosef Bacik * When we COW a block we are holding the lock on the original block,
279631e4ccSJosef Bacik * and since our lockdep maps are rootid+level, this confuses lockdep
289631e4ccSJosef Bacik * when we lock the newly allocated COW'd block. Handle this by having
299631e4ccSJosef Bacik * a subclass for COW'ed blocks so that lockdep doesn't complain.
309631e4ccSJosef Bacik */
319631e4ccSJosef Bacik BTRFS_NESTING_COW,
329631e4ccSJosef Bacik
339631e4ccSJosef Bacik /*
34bf77467aSJosef Bacik * Oftentimes we need to lock adjacent nodes on the same level while
35bf77467aSJosef Bacik * still holding the lock on the original node we searched to, such as
36bf77467aSJosef Bacik * for searching forward or for split/balance.
37bf77467aSJosef Bacik *
38bf77467aSJosef Bacik * Because of this we need to indicate to lockdep that this is
39bf77467aSJosef Bacik * acceptable by having a different subclass for each of these
40bf77467aSJosef Bacik * operations.
41bf77467aSJosef Bacik */
42bf77467aSJosef Bacik BTRFS_NESTING_LEFT,
43bf77467aSJosef Bacik BTRFS_NESTING_RIGHT,
44bf77467aSJosef Bacik
45bf77467aSJosef Bacik /*
46bf59a5a2SJosef Bacik * When splitting we will be holding a lock on the left/right node when
47bf59a5a2SJosef Bacik * we need to cow that node, thus we need a new set of subclasses for
48bf59a5a2SJosef Bacik * these two operations.
49bf59a5a2SJosef Bacik */
50bf59a5a2SJosef Bacik BTRFS_NESTING_LEFT_COW,
51bf59a5a2SJosef Bacik BTRFS_NESTING_RIGHT_COW,
52bf59a5a2SJosef Bacik
53bf59a5a2SJosef Bacik /*
544dff97e6SJosef Bacik * When splitting we may push nodes to the left or right, but still use
554dff97e6SJosef Bacik * the subsequent nodes in our path, keeping our locks on those adjacent
564dff97e6SJosef Bacik * blocks. Thus when we go to allocate a new split block we've already
574dff97e6SJosef Bacik * used up all of our available subclasses, so this subclass exists to
584dff97e6SJosef Bacik * handle this case where we need to allocate a new split block.
594dff97e6SJosef Bacik */
604dff97e6SJosef Bacik BTRFS_NESTING_SPLIT,
614dff97e6SJosef Bacik
624dff97e6SJosef Bacik /*
63cf6f34aaSJosef Bacik * When promoting a new block to a root we need to have a special
64cf6f34aaSJosef Bacik * subclass so we don't confuse lockdep, as it will appear that we are
65cf6f34aaSJosef Bacik * locking a higher level node before a lower level one. Copying also
66cf6f34aaSJosef Bacik * has this problem as it appears we're locking the same block again
67cf6f34aaSJosef Bacik * when we make a snapshot of an existing root.
68cf6f34aaSJosef Bacik */
69cf6f34aaSJosef Bacik BTRFS_NESTING_NEW_ROOT,
70cf6f34aaSJosef Bacik
71cf6f34aaSJosef Bacik /*
72fd7ba1c1SJosef Bacik * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
73fd7ba1c1SJosef Bacik * add this in here and add a static_assert to keep us from going over
74fd7ba1c1SJosef Bacik * the limit. As of this writing we're limited to 8, and we're
75fd7ba1c1SJosef Bacik * definitely using 8, hence this check to keep us from messing up in
76fd7ba1c1SJosef Bacik * the future.
77fd7ba1c1SJosef Bacik */
78fd7ba1c1SJosef Bacik BTRFS_NESTING_MAX,
79fd7ba1c1SJosef Bacik };
80fd7ba1c1SJosef Bacik
81eb33a4d6SJosef Bacik enum btrfs_lockdep_trans_states {
82*77d20c68SJosef Bacik BTRFS_LOCKDEP_TRANS_COMMIT_PREP,
83eb33a4d6SJosef Bacik BTRFS_LOCKDEP_TRANS_UNBLOCKED,
84eb33a4d6SJosef Bacik BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED,
85eb33a4d6SJosef Bacik BTRFS_LOCKDEP_TRANS_COMPLETED,
86eb33a4d6SJosef Bacik };
87eb33a4d6SJosef Bacik
88eb33a4d6SJosef Bacik /*
89eb33a4d6SJosef Bacik * Lockdep annotation for wait events.
90eb33a4d6SJosef Bacik *
91eb33a4d6SJosef Bacik * @owner: The struct where the lockdep map is defined
92eb33a4d6SJosef Bacik * @lock: The lockdep map corresponding to a wait event
93eb33a4d6SJosef Bacik *
94eb33a4d6SJosef Bacik * This macro is used to annotate a wait event. In this case a thread acquires
95eb33a4d6SJosef Bacik * the lockdep map as writer (exclusive lock) because it has to block until all
96eb33a4d6SJosef Bacik * the threads that hold the lock as readers signal the condition for the wait
97eb33a4d6SJosef Bacik * event and release their locks.
98eb33a4d6SJosef Bacik */
99eb33a4d6SJosef Bacik #define btrfs_might_wait_for_event(owner, lock) \
100eb33a4d6SJosef Bacik do { \
101eb33a4d6SJosef Bacik rwsem_acquire(&owner->lock##_map, 0, 0, _THIS_IP_); \
102eb33a4d6SJosef Bacik rwsem_release(&owner->lock##_map, _THIS_IP_); \
103eb33a4d6SJosef Bacik } while (0)
104eb33a4d6SJosef Bacik
105eb33a4d6SJosef Bacik /*
106eb33a4d6SJosef Bacik * Protection for the resource/condition of a wait event.
107eb33a4d6SJosef Bacik *
108eb33a4d6SJosef Bacik * @owner: The struct where the lockdep map is defined
109eb33a4d6SJosef Bacik * @lock: The lockdep map corresponding to a wait event
110eb33a4d6SJosef Bacik *
111eb33a4d6SJosef Bacik * Many threads can modify the condition for the wait event at the same time
112eb33a4d6SJosef Bacik * and signal the threads that block on the wait event. The threads that modify
113eb33a4d6SJosef Bacik * the condition and do the signaling acquire the lock as readers (shared
114eb33a4d6SJosef Bacik * lock).
115eb33a4d6SJosef Bacik */
116eb33a4d6SJosef Bacik #define btrfs_lockdep_acquire(owner, lock) \
117eb33a4d6SJosef Bacik rwsem_acquire_read(&owner->lock##_map, 0, 0, _THIS_IP_)
118eb33a4d6SJosef Bacik
119eb33a4d6SJosef Bacik /*
120eb33a4d6SJosef Bacik * Used after signaling the condition for a wait event to release the lockdep
121eb33a4d6SJosef Bacik * map held by a reader thread.
122eb33a4d6SJosef Bacik */
123eb33a4d6SJosef Bacik #define btrfs_lockdep_release(owner, lock) \
124eb33a4d6SJosef Bacik rwsem_release(&owner->lock##_map, _THIS_IP_)
125eb33a4d6SJosef Bacik
126eb33a4d6SJosef Bacik /*
127eb33a4d6SJosef Bacik * Macros for the transaction states wait events, similar to the generic wait
128eb33a4d6SJosef Bacik * event macros.
129eb33a4d6SJosef Bacik */
130eb33a4d6SJosef Bacik #define btrfs_might_wait_for_state(owner, i) \
131eb33a4d6SJosef Bacik do { \
132eb33a4d6SJosef Bacik rwsem_acquire(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_); \
133eb33a4d6SJosef Bacik rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_); \
134eb33a4d6SJosef Bacik } while (0)
135eb33a4d6SJosef Bacik
136eb33a4d6SJosef Bacik #define btrfs_trans_state_lockdep_acquire(owner, i) \
137eb33a4d6SJosef Bacik rwsem_acquire_read(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_)
138eb33a4d6SJosef Bacik
139eb33a4d6SJosef Bacik #define btrfs_trans_state_lockdep_release(owner, i) \
140eb33a4d6SJosef Bacik rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_)
141eb33a4d6SJosef Bacik
142eb33a4d6SJosef Bacik /* Initialization of the lockdep map */
143eb33a4d6SJosef Bacik #define btrfs_lockdep_init_map(owner, lock) \
144eb33a4d6SJosef Bacik do { \
145eb33a4d6SJosef Bacik static struct lock_class_key lock##_key; \
146eb33a4d6SJosef Bacik lockdep_init_map(&owner->lock##_map, #lock, &lock##_key, 0); \
147eb33a4d6SJosef Bacik } while (0)
148eb33a4d6SJosef Bacik
149eb33a4d6SJosef Bacik /* Initialization of the transaction states lockdep maps. */
150eb33a4d6SJosef Bacik #define btrfs_state_lockdep_init_map(owner, lock, state) \
151eb33a4d6SJosef Bacik do { \
152eb33a4d6SJosef Bacik static struct lock_class_key lock##_key; \
153eb33a4d6SJosef Bacik lockdep_init_map(&owner->btrfs_state_change_map[state], #lock, \
154eb33a4d6SJosef Bacik &lock##_key, 0); \
155eb33a4d6SJosef Bacik } while (0)
156eb33a4d6SJosef Bacik
157fd7ba1c1SJosef Bacik static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
158fd7ba1c1SJosef Bacik "too many lock subclasses defined");
159fd7ba1c1SJosef Bacik
1602992df73SNikolay Borisov struct btrfs_path;
1612992df73SNikolay Borisov
162fd7ba1c1SJosef Bacik void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
163143bede5SJeff Mahoney void btrfs_tree_lock(struct extent_buffer *eb);
164143bede5SJeff Mahoney void btrfs_tree_unlock(struct extent_buffer *eb);
165b4ce94deSChris Mason
1660ecae6ffSJosef Bacik void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
167bd681513SChris Mason void btrfs_tree_read_lock(struct extent_buffer *eb);
168bd681513SChris Mason void btrfs_tree_read_unlock(struct extent_buffer *eb);
169bd681513SChris Mason int btrfs_try_tree_read_lock(struct extent_buffer *eb);
170bd681513SChris Mason int btrfs_try_tree_write_lock(struct extent_buffer *eb);
17151899412SJosef Bacik struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
1721bb96598SJosef Bacik struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
173857bc13fSJosef Bacik struct extent_buffer *btrfs_try_read_lock_root_node(struct btrfs_root *root);
174f82c458aSChris Mason
17531f6e769SDavid Sterba #ifdef CONFIG_BTRFS_DEBUG
btrfs_assert_tree_write_locked(struct extent_buffer * eb)17649d0c642SFilipe Manana static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb)
17749d0c642SFilipe Manana {
17849d0c642SFilipe Manana lockdep_assert_held_write(&eb->lock);
17931f6e769SDavid Sterba }
18031f6e769SDavid Sterba #else
btrfs_assert_tree_write_locked(struct extent_buffer * eb)18149d0c642SFilipe Manana static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { }
18231f6e769SDavid Sterba #endif
183bd681513SChris Mason
1841f95ec01SDavid Sterba void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
185ed2b1d36SDavid Sterba
btrfs_tree_unlock_rw(struct extent_buffer * eb,int rw)186bd681513SChris Mason static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
187bd681513SChris Mason {
188ac5887c8SJosef Bacik if (rw == BTRFS_WRITE_LOCK)
189bd681513SChris Mason btrfs_tree_unlock(eb);
190bd681513SChris Mason else if (rw == BTRFS_READ_LOCK)
191bd681513SChris Mason btrfs_tree_read_unlock(eb);
192bd681513SChris Mason else
193bd681513SChris Mason BUG();
194bd681513SChris Mason }
195bd681513SChris Mason
1962992df73SNikolay Borisov struct btrfs_drew_lock {
1972992df73SNikolay Borisov atomic_t readers;
1980b548539SDavid Sterba atomic_t writers;
1992992df73SNikolay Borisov wait_queue_head_t pending_writers;
2002992df73SNikolay Borisov wait_queue_head_t pending_readers;
2012992df73SNikolay Borisov };
2022992df73SNikolay Borisov
2030b548539SDavid Sterba void btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
2042992df73SNikolay Borisov void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
2052992df73SNikolay Borisov bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
2062992df73SNikolay Borisov void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
2072992df73SNikolay Borisov void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
2082992df73SNikolay Borisov void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
2092992df73SNikolay Borisov
2100a27a047SJosef Bacik #ifdef CONFIG_DEBUG_LOCK_ALLOC
2110a27a047SJosef Bacik void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level);
212b40130b2SJosef Bacik void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buffer *eb);
2130a27a047SJosef Bacik #else
btrfs_set_buffer_lockdep_class(u64 objectid,struct extent_buffer * eb,int level)2140a27a047SJosef Bacik static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
2150a27a047SJosef Bacik struct extent_buffer *eb, int level)
2160a27a047SJosef Bacik {
2170a27a047SJosef Bacik }
btrfs_maybe_reset_lockdep_class(struct btrfs_root * root,struct extent_buffer * eb)218b40130b2SJosef Bacik static inline void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root,
219b40130b2SJosef Bacik struct extent_buffer *eb)
220b40130b2SJosef Bacik {
221b40130b2SJosef Bacik }
2220a27a047SJosef Bacik #endif
2230a27a047SJosef Bacik
224925baeddSChris Mason #endif
225