Linux Audio

Check our new training course

Loading...
v6.13.7
  1/* SPDX-License-Identifier: GPL-2.0 */
  2/*
  3 * Copyright (C) 2008 Oracle.  All rights reserved.
  4 */
  5
  6#ifndef BTRFS_LOCKING_H
  7#define BTRFS_LOCKING_H
  8
  9#include <linux/atomic.h>
 10#include <linux/wait.h>
 11#include <linux/lockdep.h>
 12#include <linux/percpu_counter.h>
 13#include "extent_io.h"
 
 14
 15struct extent_buffer;
 16struct btrfs_path;
 17struct btrfs_root;
 18
 19#define BTRFS_WRITE_LOCK 1
 20#define BTRFS_READ_LOCK 2
 21
 22/*
 23 * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
 24 * the time of this patch is 8, which is how many we use.  Keep this in mind if
 25 * you decide you want to add another subclass.
 26 */
 27enum btrfs_lock_nesting {
 28	BTRFS_NESTING_NORMAL,
 29
 30	/*
 31	 * When we COW a block we are holding the lock on the original block,
 32	 * and since our lockdep maps are rootid+level, this confuses lockdep
 33	 * when we lock the newly allocated COW'd block.  Handle this by having
 34	 * a subclass for COW'ed blocks so that lockdep doesn't complain.
 35	 */
 36	BTRFS_NESTING_COW,
 37
 38	/*
 39	 * Oftentimes we need to lock adjacent nodes on the same level while
 40	 * still holding the lock on the original node we searched to, such as
 41	 * for searching forward or for split/balance.
 42	 *
 43	 * Because of this we need to indicate to lockdep that this is
 44	 * acceptable by having a different subclass for each of these
 45	 * operations.
 46	 */
 47	BTRFS_NESTING_LEFT,
 48	BTRFS_NESTING_RIGHT,
 49
 50	/*
 51	 * When splitting we will be holding a lock on the left/right node when
 52	 * we need to cow that node, thus we need a new set of subclasses for
 53	 * these two operations.
 54	 */
 55	BTRFS_NESTING_LEFT_COW,
 56	BTRFS_NESTING_RIGHT_COW,
 57
 58	/*
 59	 * When splitting we may push nodes to the left or right, but still use
 60	 * the subsequent nodes in our path, keeping our locks on those adjacent
 61	 * blocks.  Thus when we go to allocate a new split block we've already
 62	 * used up all of our available subclasses, so this subclass exists to
 63	 * handle this case where we need to allocate a new split block.
 64	 */
 65	BTRFS_NESTING_SPLIT,
 66
 67	/*
 68	 * When promoting a new block to a root we need to have a special
 69	 * subclass so we don't confuse lockdep, as it will appear that we are
 70	 * locking a higher level node before a lower level one.  Copying also
 71	 * has this problem as it appears we're locking the same block again
 72	 * when we make a snapshot of an existing root.
 73	 */
 74	BTRFS_NESTING_NEW_ROOT,
 75
 76	/*
 77	 * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
 78	 * add this in here and add a static_assert to keep us from going over
 79	 * the limit.  As of this writing we're limited to 8, and we're
 80	 * definitely using 8, hence this check to keep us from messing up in
 81	 * the future.
 82	 */
 83	BTRFS_NESTING_MAX,
 84};
 85
 86enum btrfs_lockdep_trans_states {
 87	BTRFS_LOCKDEP_TRANS_COMMIT_PREP,
 88	BTRFS_LOCKDEP_TRANS_UNBLOCKED,
 89	BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED,
 90	BTRFS_LOCKDEP_TRANS_COMPLETED,
 91};
 92
 93/*
 94 * Lockdep annotation for wait events.
 95 *
 96 * @owner:  The struct where the lockdep map is defined
 97 * @lock:   The lockdep map corresponding to a wait event
 98 *
 99 * This macro is used to annotate a wait event. In this case a thread acquires
100 * the lockdep map as writer (exclusive lock) because it has to block until all
101 * the threads that hold the lock as readers signal the condition for the wait
102 * event and release their locks.
103 */
104#define btrfs_might_wait_for_event(owner, lock)					\
105	do {									\
106		rwsem_acquire(&owner->lock##_map, 0, 0, _THIS_IP_);		\
107		rwsem_release(&owner->lock##_map, _THIS_IP_);			\
108	} while (0)
109
110/*
111 * Protection for the resource/condition of a wait event.
112 *
113 * @owner:  The struct where the lockdep map is defined
114 * @lock:   The lockdep map corresponding to a wait event
115 *
116 * Many threads can modify the condition for the wait event at the same time
117 * and signal the threads that block on the wait event. The threads that modify
118 * the condition and do the signaling acquire the lock as readers (shared
119 * lock).
120 */
121#define btrfs_lockdep_acquire(owner, lock)					\
122	rwsem_acquire_read(&owner->lock##_map, 0, 0, _THIS_IP_)
123
124/*
125 * Used after signaling the condition for a wait event to release the lockdep
126 * map held by a reader thread.
127 */
128#define btrfs_lockdep_release(owner, lock)					\
129	rwsem_release(&owner->lock##_map, _THIS_IP_)
130
131/*
132 * Used to account for the fact that when doing io_uring encoded I/O, we can
133 * return to userspace with the inode lock still held.
134 */
135#define btrfs_lockdep_inode_acquire(owner, lock)				\
136	rwsem_acquire_read(&owner->vfs_inode.lock.dep_map, 0, 0, _THIS_IP_)
137
138#define btrfs_lockdep_inode_release(owner, lock)				\
139	rwsem_release(&owner->vfs_inode.lock.dep_map, _THIS_IP_)
140
141/*
142 * Macros for the transaction states wait events, similar to the generic wait
143 * event macros.
144 */
145#define btrfs_might_wait_for_state(owner, i)					\
146	do {									\
147		rwsem_acquire(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_); \
148		rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_);	\
149	} while (0)
150
151#define btrfs_trans_state_lockdep_acquire(owner, i)				\
152	rwsem_acquire_read(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_)
153
154#define btrfs_trans_state_lockdep_release(owner, i)				\
155	rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_)
156
157/* Initialization of the lockdep map */
158#define btrfs_lockdep_init_map(owner, lock)					\
159	do {									\
160		static struct lock_class_key lock##_key;			\
161		lockdep_init_map(&owner->lock##_map, #lock, &lock##_key, 0);	\
162	} while (0)
163
164/* Initialization of the transaction states lockdep maps. */
165#define btrfs_state_lockdep_init_map(owner, lock, state)			\
166	do {									\
167		static struct lock_class_key lock##_key;			\
168		lockdep_init_map(&owner->btrfs_state_change_map[state], #lock,	\
169				 &lock##_key, 0);				\
170	} while (0)
171
172static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
173	      "too many lock subclasses defined");
174
175void btrfs_tree_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
176
177static inline void btrfs_tree_lock(struct extent_buffer *eb)
178{
179	btrfs_tree_lock_nested(eb, BTRFS_NESTING_NORMAL);
180}
181
182void btrfs_tree_unlock(struct extent_buffer *eb);
183
184void btrfs_tree_read_lock_nested(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
185
186static inline void btrfs_tree_read_lock(struct extent_buffer *eb)
187{
188	btrfs_tree_read_lock_nested(eb, BTRFS_NESTING_NORMAL);
189}
190
191void btrfs_tree_read_unlock(struct extent_buffer *eb);
192int btrfs_try_tree_read_lock(struct extent_buffer *eb);
 
193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
194struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
195struct extent_buffer *btrfs_try_read_lock_root_node(struct btrfs_root *root);
196
197#ifdef CONFIG_BTRFS_DEBUG
198static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb)
199{
200	lockdep_assert_held_write(&eb->lock);
201}
202#else
203static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { }
204#endif
205
206void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
207
208static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
209{
210	if (rw == BTRFS_WRITE_LOCK)
211		btrfs_tree_unlock(eb);
212	else if (rw == BTRFS_READ_LOCK)
213		btrfs_tree_read_unlock(eb);
214	else
215		BUG();
216}
217
218struct btrfs_drew_lock {
219	atomic_t readers;
220	atomic_t writers;
221	wait_queue_head_t pending_writers;
222	wait_queue_head_t pending_readers;
223};
224
225void btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
226void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
227bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
228void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
229void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
230void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
231
232#ifdef CONFIG_DEBUG_LOCK_ALLOC
233void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level);
234void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buffer *eb);
235#else
236static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
237					struct extent_buffer *eb, int level)
238{
239}
240static inline void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root,
241						   struct extent_buffer *eb)
242{
243}
244#endif
245
246#endif
v6.9.4
  1/* SPDX-License-Identifier: GPL-2.0 */
  2/*
  3 * Copyright (C) 2008 Oracle.  All rights reserved.
  4 */
  5
  6#ifndef BTRFS_LOCKING_H
  7#define BTRFS_LOCKING_H
  8
  9#include <linux/atomic.h>
 10#include <linux/wait.h>
 11#include <linux/lockdep.h>
 12#include <linux/percpu_counter.h>
 13#include "extent_io.h"
 14#include "locking.h"
 15
 16struct extent_buffer;
 17struct btrfs_path;
 18struct btrfs_root;
 19
 20#define BTRFS_WRITE_LOCK 1
 21#define BTRFS_READ_LOCK 2
 22
 23/*
 24 * We are limited in number of subclasses by MAX_LOCKDEP_SUBCLASSES, which at
 25 * the time of this patch is 8, which is how many we use.  Keep this in mind if
 26 * you decide you want to add another subclass.
 27 */
 28enum btrfs_lock_nesting {
 29	BTRFS_NESTING_NORMAL,
 30
 31	/*
 32	 * When we COW a block we are holding the lock on the original block,
 33	 * and since our lockdep maps are rootid+level, this confuses lockdep
 34	 * when we lock the newly allocated COW'd block.  Handle this by having
 35	 * a subclass for COW'ed blocks so that lockdep doesn't complain.
 36	 */
 37	BTRFS_NESTING_COW,
 38
 39	/*
 40	 * Oftentimes we need to lock adjacent nodes on the same level while
 41	 * still holding the lock on the original node we searched to, such as
 42	 * for searching forward or for split/balance.
 43	 *
 44	 * Because of this we need to indicate to lockdep that this is
 45	 * acceptable by having a different subclass for each of these
 46	 * operations.
 47	 */
 48	BTRFS_NESTING_LEFT,
 49	BTRFS_NESTING_RIGHT,
 50
 51	/*
 52	 * When splitting we will be holding a lock on the left/right node when
 53	 * we need to cow that node, thus we need a new set of subclasses for
 54	 * these two operations.
 55	 */
 56	BTRFS_NESTING_LEFT_COW,
 57	BTRFS_NESTING_RIGHT_COW,
 58
 59	/*
 60	 * When splitting we may push nodes to the left or right, but still use
 61	 * the subsequent nodes in our path, keeping our locks on those adjacent
 62	 * blocks.  Thus when we go to allocate a new split block we've already
 63	 * used up all of our available subclasses, so this subclass exists to
 64	 * handle this case where we need to allocate a new split block.
 65	 */
 66	BTRFS_NESTING_SPLIT,
 67
 68	/*
 69	 * When promoting a new block to a root we need to have a special
 70	 * subclass so we don't confuse lockdep, as it will appear that we are
 71	 * locking a higher level node before a lower level one.  Copying also
 72	 * has this problem as it appears we're locking the same block again
 73	 * when we make a snapshot of an existing root.
 74	 */
 75	BTRFS_NESTING_NEW_ROOT,
 76
 77	/*
 78	 * We are limited to MAX_LOCKDEP_SUBLCLASSES number of subclasses, so
 79	 * add this in here and add a static_assert to keep us from going over
 80	 * the limit.  As of this writing we're limited to 8, and we're
 81	 * definitely using 8, hence this check to keep us from messing up in
 82	 * the future.
 83	 */
 84	BTRFS_NESTING_MAX,
 85};
 86
 87enum btrfs_lockdep_trans_states {
 88	BTRFS_LOCKDEP_TRANS_COMMIT_PREP,
 89	BTRFS_LOCKDEP_TRANS_UNBLOCKED,
 90	BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED,
 91	BTRFS_LOCKDEP_TRANS_COMPLETED,
 92};
 93
 94/*
 95 * Lockdep annotation for wait events.
 96 *
 97 * @owner:  The struct where the lockdep map is defined
 98 * @lock:   The lockdep map corresponding to a wait event
 99 *
100 * This macro is used to annotate a wait event. In this case a thread acquires
101 * the lockdep map as writer (exclusive lock) because it has to block until all
102 * the threads that hold the lock as readers signal the condition for the wait
103 * event and release their locks.
104 */
105#define btrfs_might_wait_for_event(owner, lock)					\
106	do {									\
107		rwsem_acquire(&owner->lock##_map, 0, 0, _THIS_IP_);		\
108		rwsem_release(&owner->lock##_map, _THIS_IP_);			\
109	} while (0)
110
111/*
112 * Protection for the resource/condition of a wait event.
113 *
114 * @owner:  The struct where the lockdep map is defined
115 * @lock:   The lockdep map corresponding to a wait event
116 *
117 * Many threads can modify the condition for the wait event at the same time
118 * and signal the threads that block on the wait event. The threads that modify
119 * the condition and do the signaling acquire the lock as readers (shared
120 * lock).
121 */
122#define btrfs_lockdep_acquire(owner, lock)					\
123	rwsem_acquire_read(&owner->lock##_map, 0, 0, _THIS_IP_)
124
125/*
126 * Used after signaling the condition for a wait event to release the lockdep
127 * map held by a reader thread.
128 */
129#define btrfs_lockdep_release(owner, lock)					\
130	rwsem_release(&owner->lock##_map, _THIS_IP_)
131
132/*
 
 
 
 
 
 
 
 
 
 
133 * Macros for the transaction states wait events, similar to the generic wait
134 * event macros.
135 */
136#define btrfs_might_wait_for_state(owner, i)					\
137	do {									\
138		rwsem_acquire(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_); \
139		rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_);	\
140	} while (0)
141
142#define btrfs_trans_state_lockdep_acquire(owner, i)				\
143	rwsem_acquire_read(&owner->btrfs_state_change_map[i], 0, 0, _THIS_IP_)
144
145#define btrfs_trans_state_lockdep_release(owner, i)				\
146	rwsem_release(&owner->btrfs_state_change_map[i], _THIS_IP_)
147
148/* Initialization of the lockdep map */
149#define btrfs_lockdep_init_map(owner, lock)					\
150	do {									\
151		static struct lock_class_key lock##_key;			\
152		lockdep_init_map(&owner->lock##_map, #lock, &lock##_key, 0);	\
153	} while (0)
154
155/* Initialization of the transaction states lockdep maps. */
156#define btrfs_state_lockdep_init_map(owner, lock, state)			\
157	do {									\
158		static struct lock_class_key lock##_key;			\
159		lockdep_init_map(&owner->btrfs_state_change_map[state], #lock,	\
160				 &lock##_key, 0);				\
161	} while (0)
162
163static_assert(BTRFS_NESTING_MAX <= MAX_LOCKDEP_SUBCLASSES,
164	      "too many lock subclasses defined");
165
166void __btrfs_tree_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
167void btrfs_tree_lock(struct extent_buffer *eb);
 
 
 
 
 
168void btrfs_tree_unlock(struct extent_buffer *eb);
169
170void __btrfs_tree_read_lock(struct extent_buffer *eb, enum btrfs_lock_nesting nest);
171void btrfs_tree_read_lock(struct extent_buffer *eb);
 
 
 
 
 
172void btrfs_tree_read_unlock(struct extent_buffer *eb);
173int btrfs_try_tree_read_lock(struct extent_buffer *eb);
174int btrfs_try_tree_write_lock(struct extent_buffer *eb);
175struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
176struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
177struct extent_buffer *btrfs_try_read_lock_root_node(struct btrfs_root *root);
178
179#ifdef CONFIG_BTRFS_DEBUG
180static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb)
181{
182	lockdep_assert_held_write(&eb->lock);
183}
184#else
185static inline void btrfs_assert_tree_write_locked(struct extent_buffer *eb) { }
186#endif
187
188void btrfs_unlock_up_safe(struct btrfs_path *path, int level);
189
190static inline void btrfs_tree_unlock_rw(struct extent_buffer *eb, int rw)
191{
192	if (rw == BTRFS_WRITE_LOCK)
193		btrfs_tree_unlock(eb);
194	else if (rw == BTRFS_READ_LOCK)
195		btrfs_tree_read_unlock(eb);
196	else
197		BUG();
198}
199
200struct btrfs_drew_lock {
201	atomic_t readers;
202	atomic_t writers;
203	wait_queue_head_t pending_writers;
204	wait_queue_head_t pending_readers;
205};
206
207void btrfs_drew_lock_init(struct btrfs_drew_lock *lock);
208void btrfs_drew_write_lock(struct btrfs_drew_lock *lock);
209bool btrfs_drew_try_write_lock(struct btrfs_drew_lock *lock);
210void btrfs_drew_write_unlock(struct btrfs_drew_lock *lock);
211void btrfs_drew_read_lock(struct btrfs_drew_lock *lock);
212void btrfs_drew_read_unlock(struct btrfs_drew_lock *lock);
213
214#ifdef CONFIG_DEBUG_LOCK_ALLOC
215void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb, int level);
216void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root, struct extent_buffer *eb);
217#else
218static inline void btrfs_set_buffer_lockdep_class(u64 objectid,
219					struct extent_buffer *eb, int level)
220{
221}
222static inline void btrfs_maybe_reset_lockdep_class(struct btrfs_root *root,
223						   struct extent_buffer *eb)
224{
225}
226#endif
227
228#endif