]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_locking.h
Update bcachefs sources to 070ec8d07b bcachefs: Snapshot depth, skiplist fields
[bcachefs-tools-debian] / libbcachefs / btree_locking.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_LOCKING_H
3 #define _BCACHEFS_BTREE_LOCKING_H
4
5 /*
6  * Only for internal btree use:
7  *
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
11  */
12
13 #include <linux/six.h>
14
15 #include "btree_iter.h"
16
17 void bch2_btree_lock_init(struct btree_bkey_cached_common *, enum six_lock_init_flags);
18
19 #ifdef CONFIG_LOCKDEP
20 void bch2_assert_btree_nodes_not_locked(void);
21 #else
22 static inline void bch2_assert_btree_nodes_not_locked(void) {}
23 #endif
24
25 void bch2_trans_unlock_noassert(struct btree_trans *);
26
27 static inline bool is_btree_node(struct btree_path *path, unsigned l)
28 {
29         return l < BTREE_MAX_DEPTH && !IS_ERR_OR_NULL(path->l[l].b);
30 }
31
32 static inline struct btree_transaction_stats *btree_trans_stats(struct btree_trans *trans)
33 {
34         return trans->fn_idx < ARRAY_SIZE(trans->c->btree_transaction_stats)
35                 ? &trans->c->btree_transaction_stats[trans->fn_idx]
36                 : NULL;
37 }
38
39 /* matches six lock types */
40 enum btree_node_locked_type {
41         BTREE_NODE_UNLOCKED             = -1,
42         BTREE_NODE_READ_LOCKED          = SIX_LOCK_read,
43         BTREE_NODE_INTENT_LOCKED        = SIX_LOCK_intent,
44         BTREE_NODE_WRITE_LOCKED         = SIX_LOCK_write,
45 };
46
47 static inline int btree_node_locked_type(struct btree_path *path,
48                                          unsigned level)
49 {
50         return BTREE_NODE_UNLOCKED + ((path->nodes_locked >> (level << 1)) & 3);
51 }
52
53 static inline bool btree_node_write_locked(struct btree_path *path, unsigned l)
54 {
55         return btree_node_locked_type(path, l) == BTREE_NODE_WRITE_LOCKED;
56 }
57
58 static inline bool btree_node_intent_locked(struct btree_path *path, unsigned l)
59 {
60         return btree_node_locked_type(path, l) == BTREE_NODE_INTENT_LOCKED;
61 }
62
63 static inline bool btree_node_read_locked(struct btree_path *path, unsigned l)
64 {
65         return btree_node_locked_type(path, l) == BTREE_NODE_READ_LOCKED;
66 }
67
68 static inline bool btree_node_locked(struct btree_path *path, unsigned level)
69 {
70         return btree_node_locked_type(path, level) != BTREE_NODE_UNLOCKED;
71 }
72
73 static inline void mark_btree_node_locked_noreset(struct btree_path *path,
74                                                   unsigned level,
75                                                   enum btree_node_locked_type type)
76 {
77         /* relying on this to avoid a branch */
78         BUILD_BUG_ON(SIX_LOCK_read   != 0);
79         BUILD_BUG_ON(SIX_LOCK_intent != 1);
80
81         path->nodes_locked &= ~(3U << (level << 1));
82         path->nodes_locked |= (type + 1) << (level << 1);
83 }
84
85 static inline void mark_btree_node_unlocked(struct btree_path *path,
86                                             unsigned level)
87 {
88         EBUG_ON(btree_node_write_locked(path, level));
89         mark_btree_node_locked_noreset(path, level, BTREE_NODE_UNLOCKED);
90 }
91
92 static inline void mark_btree_node_locked(struct btree_trans *trans,
93                                           struct btree_path *path,
94                                           unsigned level,
95                                           enum six_lock_type type)
96 {
97         mark_btree_node_locked_noreset(path, level, (enum btree_node_locked_type) type);
98 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
99         path->l[level].lock_taken_time = local_clock();
100 #endif
101 }
102
103 static inline enum six_lock_type __btree_lock_want(struct btree_path *path, int level)
104 {
105         return level < path->locks_want
106                 ? SIX_LOCK_intent
107                 : SIX_LOCK_read;
108 }
109
110 static inline enum btree_node_locked_type
111 btree_lock_want(struct btree_path *path, int level)
112 {
113         if (level < path->level)
114                 return BTREE_NODE_UNLOCKED;
115         if (level < path->locks_want)
116                 return BTREE_NODE_INTENT_LOCKED;
117         if (level == path->level)
118                 return BTREE_NODE_READ_LOCKED;
119         return BTREE_NODE_UNLOCKED;
120 }
121
122 static void btree_trans_lock_hold_time_update(struct btree_trans *trans,
123                                               struct btree_path *path, unsigned level)
124 {
125 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
126         struct btree_transaction_stats *s = btree_trans_stats(trans);
127
128         if (s)
129                 __bch2_time_stats_update(&s->lock_hold_times,
130                                          path->l[level].lock_taken_time,
131                                          local_clock());
132 #endif
133 }
134
135 /* unlock: */
136
137 static inline void btree_node_unlock(struct btree_trans *trans,
138                                      struct btree_path *path, unsigned level)
139 {
140         int lock_type = btree_node_locked_type(path, level);
141
142         EBUG_ON(level >= BTREE_MAX_DEPTH);
143
144         if (lock_type != BTREE_NODE_UNLOCKED) {
145                 six_unlock_type(&path->l[level].b->c.lock, lock_type);
146                 btree_trans_lock_hold_time_update(trans, path, level);
147         }
148         mark_btree_node_unlocked(path, level);
149 }
150
151 static inline int btree_path_lowest_level_locked(struct btree_path *path)
152 {
153         return __ffs(path->nodes_locked) >> 1;
154 }
155
156 static inline int btree_path_highest_level_locked(struct btree_path *path)
157 {
158         return __fls(path->nodes_locked) >> 1;
159 }
160
161 static inline void __bch2_btree_path_unlock(struct btree_trans *trans,
162                                             struct btree_path *path)
163 {
164         btree_path_set_dirty(path, BTREE_ITER_NEED_RELOCK);
165
166         while (path->nodes_locked)
167                 btree_node_unlock(trans, path, btree_path_lowest_level_locked(path));
168 }
169
170 /*
171  * Updates the saved lock sequence number, so that bch2_btree_node_relock() will
172  * succeed:
173  */
174 static inline void
175 bch2_btree_node_unlock_write_inlined(struct btree_trans *trans, struct btree_path *path,
176                                      struct btree *b)
177 {
178         struct btree_path *linked;
179
180         EBUG_ON(path->l[b->c.level].b != b);
181         EBUG_ON(path->l[b->c.level].lock_seq != six_lock_seq(&b->c.lock));
182         EBUG_ON(btree_node_locked_type(path, b->c.level) != SIX_LOCK_write);
183
184         mark_btree_node_locked_noreset(path, b->c.level, SIX_LOCK_intent);
185
186         trans_for_each_path_with_node(trans, b, linked)
187                 linked->l[b->c.level].lock_seq++;
188
189         six_unlock_write(&b->c.lock);
190 }
191
192 void bch2_btree_node_unlock_write(struct btree_trans *,
193                         struct btree_path *, struct btree *);
194
195 int bch2_six_check_for_deadlock(struct six_lock *lock, void *p);
196
197 /* lock: */
198
199 static inline int __btree_node_lock_nopath(struct btree_trans *trans,
200                                          struct btree_bkey_cached_common *b,
201                                          enum six_lock_type type,
202                                          bool lock_may_not_fail,
203                                          unsigned long ip)
204 {
205         int ret;
206
207         trans->lock_may_not_fail = lock_may_not_fail;
208         trans->lock_must_abort  = false;
209         trans->locking          = b;
210
211         ret = six_lock_ip_waiter(&b->lock, type, &trans->locking_wait,
212                                  bch2_six_check_for_deadlock, trans, ip);
213         WRITE_ONCE(trans->locking, NULL);
214         WRITE_ONCE(trans->locking_wait.start_time, 0);
215         return ret;
216 }
217
218 static inline int __must_check
219 btree_node_lock_nopath(struct btree_trans *trans,
220                        struct btree_bkey_cached_common *b,
221                        enum six_lock_type type,
222                        unsigned long ip)
223 {
224         return __btree_node_lock_nopath(trans, b, type, false, ip);
225 }
226
227 static inline void btree_node_lock_nopath_nofail(struct btree_trans *trans,
228                                          struct btree_bkey_cached_common *b,
229                                          enum six_lock_type type)
230 {
231         int ret = __btree_node_lock_nopath(trans, b, type, true, _THIS_IP_);
232
233         BUG_ON(ret);
234 }
235
236 /*
237  * Lock a btree node if we already have it locked on one of our linked
238  * iterators:
239  */
240 static inline bool btree_node_lock_increment(struct btree_trans *trans,
241                                              struct btree_bkey_cached_common *b,
242                                              unsigned level,
243                                              enum btree_node_locked_type want)
244 {
245         struct btree_path *path;
246
247         trans_for_each_path(trans, path)
248                 if (&path->l[level].b->c == b &&
249                     btree_node_locked_type(path, level) >= want) {
250                         six_lock_increment(&b->lock, (enum six_lock_type) want);
251                         return true;
252                 }
253
254         return false;
255 }
256
257 static inline int btree_node_lock(struct btree_trans *trans,
258                         struct btree_path *path,
259                         struct btree_bkey_cached_common *b,
260                         unsigned level,
261                         enum six_lock_type type,
262                         unsigned long ip)
263 {
264         int ret = 0;
265
266         EBUG_ON(level >= BTREE_MAX_DEPTH);
267         EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
268
269         if (likely(six_trylock_type(&b->lock, type)) ||
270             btree_node_lock_increment(trans, b, level, (enum btree_node_locked_type) type) ||
271             !(ret = btree_node_lock_nopath(trans, b, type, btree_path_ip_allocated(path)))) {
272 #ifdef CONFIG_BCACHEFS_LOCK_TIME_STATS
273                 path->l[b->level].lock_taken_time = local_clock();
274 #endif
275         }
276
277         return ret;
278 }
279
280 int __bch2_btree_node_lock_write(struct btree_trans *, struct btree_path *,
281                                  struct btree_bkey_cached_common *b, bool);
282
283 static inline int __btree_node_lock_write(struct btree_trans *trans,
284                                           struct btree_path *path,
285                                           struct btree_bkey_cached_common *b,
286                                           bool lock_may_not_fail)
287 {
288         EBUG_ON(&path->l[b->level].b->c != b);
289         EBUG_ON(path->l[b->level].lock_seq != six_lock_seq(&b->lock));
290         EBUG_ON(!btree_node_intent_locked(path, b->level));
291
292         /*
293          * six locks are unfair, and read locks block while a thread wants a
294          * write lock: thus, we need to tell the cycle detector we have a write
295          * lock _before_ taking the lock:
296          */
297         mark_btree_node_locked_noreset(path, b->level, SIX_LOCK_write);
298
299         return likely(six_trylock_write(&b->lock))
300                 ? 0
301                 : __bch2_btree_node_lock_write(trans, path, b, lock_may_not_fail);
302 }
303
304 static inline int __must_check
305 bch2_btree_node_lock_write(struct btree_trans *trans,
306                            struct btree_path *path,
307                            struct btree_bkey_cached_common *b)
308 {
309         return __btree_node_lock_write(trans, path, b, false);
310 }
311
312 void bch2_btree_node_lock_write_nofail(struct btree_trans *,
313                                        struct btree_path *,
314                                        struct btree_bkey_cached_common *);
315
316 /* relock: */
317
318 bool bch2_btree_path_relock_norestart(struct btree_trans *,
319                                       struct btree_path *, unsigned long);
320 int __bch2_btree_path_relock(struct btree_trans *,
321                              struct btree_path *, unsigned long);
322
323 static inline int bch2_btree_path_relock(struct btree_trans *trans,
324                                 struct btree_path *path, unsigned long trace_ip)
325 {
326         return btree_node_locked(path, path->level)
327                 ? 0
328                 : __bch2_btree_path_relock(trans, path, trace_ip);
329 }
330
331 bool __bch2_btree_node_relock(struct btree_trans *, struct btree_path *, unsigned, bool trace);
332
333 static inline bool bch2_btree_node_relock(struct btree_trans *trans,
334                                           struct btree_path *path, unsigned level)
335 {
336         EBUG_ON(btree_node_locked(path, level) &&
337                 !btree_node_write_locked(path, level) &&
338                 btree_node_locked_type(path, level) != __btree_lock_want(path, level));
339
340         return likely(btree_node_locked(path, level)) ||
341                 (!IS_ERR_OR_NULL(path->l[level].b) &&
342                  __bch2_btree_node_relock(trans, path, level, true));
343 }
344
345 static inline bool bch2_btree_node_relock_notrace(struct btree_trans *trans,
346                                                   struct btree_path *path, unsigned level)
347 {
348         EBUG_ON(btree_node_locked(path, level) &&
349                 !btree_node_write_locked(path, level) &&
350                 btree_node_locked_type(path, level) != __btree_lock_want(path, level));
351
352         return likely(btree_node_locked(path, level)) ||
353                 (!IS_ERR_OR_NULL(path->l[level].b) &&
354                  __bch2_btree_node_relock(trans, path, level, false));
355 }
356
357 /* upgrade */
358
359 bool bch2_btree_path_upgrade_noupgrade_sibs(struct btree_trans *,
360                                struct btree_path *, unsigned);
361 bool __bch2_btree_path_upgrade(struct btree_trans *,
362                                struct btree_path *, unsigned);
363
364 static inline int bch2_btree_path_upgrade(struct btree_trans *trans,
365                                           struct btree_path *path,
366                                           unsigned new_locks_want)
367 {
368         unsigned old_locks_want = path->locks_want;
369
370         new_locks_want = min(new_locks_want, BTREE_MAX_DEPTH);
371
372         if (path->locks_want < new_locks_want
373             ? __bch2_btree_path_upgrade(trans, path, new_locks_want)
374             : path->uptodate == BTREE_ITER_UPTODATE)
375                 return 0;
376
377         trace_and_count(trans->c, trans_restart_upgrade, trans, _THIS_IP_, path,
378                         old_locks_want, new_locks_want);
379         return btree_trans_restart(trans, BCH_ERR_transaction_restart_upgrade);
380 }
381
382 /* misc: */
383
384 static inline void btree_path_set_should_be_locked(struct btree_path *path)
385 {
386         EBUG_ON(!btree_node_locked(path, path->level));
387         EBUG_ON(path->uptodate);
388
389         path->should_be_locked = true;
390 }
391
392 static inline void __btree_path_set_level_up(struct btree_trans *trans,
393                                       struct btree_path *path,
394                                       unsigned l)
395 {
396         btree_node_unlock(trans, path, l);
397         path->l[l].b = ERR_PTR(-BCH_ERR_no_btree_node_up);
398 }
399
400 static inline void btree_path_set_level_up(struct btree_trans *trans,
401                                     struct btree_path *path)
402 {
403         __btree_path_set_level_up(trans, path, path->level++);
404         btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
405 }
406
407 /* debug */
408
409 struct six_lock_count bch2_btree_node_lock_counts(struct btree_trans *,
410                                 struct btree_path *,
411                                 struct btree_bkey_cached_common *b,
412                                 unsigned);
413
414 int bch2_check_for_deadlock(struct btree_trans *, struct printbuf *);
415
416 #ifdef CONFIG_BCACHEFS_DEBUG
417 void bch2_btree_path_verify_locks(struct btree_path *);
418 void bch2_trans_verify_locks(struct btree_trans *);
419 #else
420 static inline void bch2_btree_path_verify_locks(struct btree_path *path) {}
421 static inline void bch2_trans_verify_locks(struct btree_trans *trans) {}
422 #endif
423
424 #endif /* _BCACHEFS_BTREE_LOCKING_H */