1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_LOCKING_H
3 #define _BCACHEFS_BTREE_LOCKING_H
6 * Only for internal btree use:
8 * The btree iterator tracks what locks it wants to take, and what locks it
9 * currently has - here we have wrappers for locking/unlocking btree nodes and
10 * updating the iterator state
13 #include <linux/six.h>
15 #include "btree_iter.h"
17 static inline bool is_btree_node(struct btree_path *path, unsigned l)
19 return l < BTREE_MAX_DEPTH && !IS_ERR_OR_NULL(path->l[l].b);
22 /* matches six lock types */
23 enum btree_node_locked_type {
24 BTREE_NODE_UNLOCKED = -1,
25 BTREE_NODE_READ_LOCKED = SIX_LOCK_read,
26 BTREE_NODE_INTENT_LOCKED = SIX_LOCK_intent,
29 static inline int btree_node_locked_type(struct btree_path *path,
33 * We're relying on the fact that if nodes_intent_locked is set
34 * nodes_locked must be set as well, so that we can compute without
37 return BTREE_NODE_UNLOCKED +
38 ((path->nodes_locked >> level) & 1) +
39 ((path->nodes_intent_locked >> level) & 1);
42 static inline bool btree_node_intent_locked(struct btree_path *path,
45 return btree_node_locked_type(path, level) == BTREE_NODE_INTENT_LOCKED;
48 static inline bool btree_node_read_locked(struct btree_path *path,
51 return btree_node_locked_type(path, level) == BTREE_NODE_READ_LOCKED;
54 static inline bool btree_node_locked(struct btree_path *path, unsigned level)
56 return path->nodes_locked & (1 << level);
59 static inline void mark_btree_node_unlocked(struct btree_path *path,
62 path->nodes_locked &= ~(1 << level);
63 path->nodes_intent_locked &= ~(1 << level);
66 static inline void mark_btree_node_locked_noreset(struct btree_trans *trans,
67 struct btree_path *path,
69 enum six_lock_type type)
71 /* relying on this to avoid a branch */
72 BUILD_BUG_ON(SIX_LOCK_read != 0);
73 BUILD_BUG_ON(SIX_LOCK_intent != 1);
75 BUG_ON(trans->in_traverse_all && path->sorted_idx > trans->traverse_all_idx);
77 path->nodes_locked |= 1 << level;
78 path->nodes_intent_locked |= type << level;
81 static inline void mark_btree_node_locked(struct btree_trans *trans,
82 struct btree_path *path,
84 enum six_lock_type type)
86 mark_btree_node_locked_noreset(trans, path, level, type);
87 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
88 path->l[level].lock_taken_time = ktime_get_ns();
92 static inline void mark_btree_node_intent_locked(struct btree_trans *trans,
93 struct btree_path *path,
96 mark_btree_node_locked_noreset(trans, path, level, SIX_LOCK_intent);
99 static inline enum six_lock_type __btree_lock_want(struct btree_path *path, int level)
101 return level < path->locks_want
106 static inline enum btree_node_locked_type
107 btree_lock_want(struct btree_path *path, int level)
109 if (level < path->level)
110 return BTREE_NODE_UNLOCKED;
111 if (level < path->locks_want)
112 return BTREE_NODE_INTENT_LOCKED;
113 if (level == path->level)
114 return BTREE_NODE_READ_LOCKED;
115 return BTREE_NODE_UNLOCKED;
118 static inline void btree_node_unlock(struct btree_trans *trans,
119 struct btree_path *path, unsigned level)
121 int lock_type = btree_node_locked_type(path, level);
123 EBUG_ON(level >= BTREE_MAX_DEPTH);
125 if (lock_type != BTREE_NODE_UNLOCKED) {
126 six_unlock_type(&path->l[level].b->c.lock, lock_type);
127 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
128 if (trans->lock_name_idx < BCH_LOCK_TIME_NR) {
129 struct bch_fs *c = trans->c;
131 __bch2_time_stats_update(&c->lock_held_stats.times[trans->lock_name_idx],
132 path->l[level].lock_taken_time,
137 mark_btree_node_unlocked(path, level);
140 static inline void __bch2_btree_path_unlock(struct btree_trans *trans,
141 struct btree_path *path)
143 btree_path_set_dirty(path, BTREE_ITER_NEED_RELOCK);
145 while (path->nodes_locked)
146 btree_node_unlock(trans, path, __ffs(path->nodes_locked));
149 static inline enum bch_time_stats lock_to_time_stat(enum six_lock_type type)
153 return BCH_TIME_btree_lock_contended_read;
154 case SIX_LOCK_intent:
155 return BCH_TIME_btree_lock_contended_intent;
157 return BCH_TIME_btree_lock_contended_write;
163 static inline int btree_node_lock_type(struct btree_trans *trans,
164 struct btree_path *path,
166 struct bpos pos, unsigned level,
167 enum six_lock_type type,
168 six_lock_should_sleep_fn should_sleep_fn, void *p)
170 struct bch_fs *c = trans->c;
174 if (six_trylock_type(&b->c.lock, type))
177 start_time = local_clock();
179 trans->locking_path_idx = path->idx;
180 trans->locking_pos = pos;
181 trans->locking_btree_id = path->btree_id;
182 trans->locking_level = level;
183 trans->locking_lock_type = type;
184 trans->locking = &b->c;
185 ret = six_lock_type(&b->c.lock, type, should_sleep_fn, p);
186 trans->locking = NULL;
191 bch2_time_stats_update(&c->times[lock_to_time_stat(type)], start_time);
196 * Lock a btree node if we already have it locked on one of our linked
199 static inline bool btree_node_lock_increment(struct btree_trans *trans,
200 struct btree *b, unsigned level,
201 enum btree_node_locked_type want)
203 struct btree_path *path;
205 trans_for_each_path(trans, path)
206 if (path->l[level].b == b &&
207 btree_node_locked_type(path, level) >= want) {
208 six_lock_increment(&b->c.lock, want);
215 int __bch2_btree_node_lock(struct btree_trans *, struct btree_path *,
216 struct btree *, struct bpos, unsigned,
218 six_lock_should_sleep_fn, void *,
221 static inline int btree_node_lock(struct btree_trans *trans,
222 struct btree_path *path,
223 struct btree *b, struct bpos pos, unsigned level,
224 enum six_lock_type type,
225 six_lock_should_sleep_fn should_sleep_fn, void *p,
230 EBUG_ON(level >= BTREE_MAX_DEPTH);
231 EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
233 if (likely(six_trylock_type(&b->c.lock, type)) ||
234 btree_node_lock_increment(trans, b, level, type) ||
235 !(ret = __bch2_btree_node_lock(trans, path, b, pos, level, type,
236 should_sleep_fn, p, ip))) {
237 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
238 path->l[b->c.level].lock_taken_time = ktime_get_ns();
245 bool __bch2_btree_node_relock(struct btree_trans *, struct btree_path *, unsigned);
247 static inline bool bch2_btree_node_relock(struct btree_trans *trans,
248 struct btree_path *path, unsigned level)
250 EBUG_ON(btree_node_locked(path, level) &&
251 btree_node_locked_type(path, level) !=
252 __btree_lock_want(path, level));
254 return likely(btree_node_locked(path, level)) ||
255 __bch2_btree_node_relock(trans, path, level);
259 * Updates the saved lock sequence number, so that bch2_btree_node_relock() will
263 bch2_btree_node_unlock_write_inlined(struct btree_trans *trans, struct btree_path *path,
266 struct btree_path *linked;
268 EBUG_ON(path->l[b->c.level].b != b);
269 EBUG_ON(path->l[b->c.level].lock_seq + 1 != b->c.lock.state.seq);
271 trans_for_each_path_with_node(trans, b, linked)
272 linked->l[b->c.level].lock_seq += 2;
274 six_unlock_write(&b->c.lock);
277 void bch2_btree_node_unlock_write(struct btree_trans *,
278 struct btree_path *, struct btree *);
280 void __bch2_btree_node_lock_write(struct btree_trans *, struct btree *);
282 static inline void bch2_btree_node_lock_write(struct btree_trans *trans,
283 struct btree_path *path,
286 EBUG_ON(path->l[b->c.level].b != b);
287 EBUG_ON(path->l[b->c.level].lock_seq != b->c.lock.state.seq);
288 EBUG_ON(!btree_node_intent_locked(path, b->c.level));
290 if (unlikely(!six_trylock_write(&b->c.lock)))
291 __bch2_btree_node_lock_write(trans, b);
294 static inline void btree_path_set_should_be_locked(struct btree_path *path)
296 EBUG_ON(!btree_node_locked(path, path->level));
297 EBUG_ON(path->uptodate);
299 path->should_be_locked = true;
302 static inline void __btree_path_set_level_up(struct btree_trans *trans,
303 struct btree_path *path,
306 btree_node_unlock(trans, path, l);
307 path->l[l].b = ERR_PTR(-BCH_ERR_no_btree_node_up);
310 static inline void btree_path_set_level_up(struct btree_trans *trans,
311 struct btree_path *path)
313 __btree_path_set_level_up(trans, path, path->level++);
314 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
317 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *,
318 struct btree_path *, struct btree *, unsigned);
320 #endif /* _BCACHEFS_BTREE_LOCKING_H */