xref: /linux/fs/bcachefs/btree_iter.h (revision 5e2cb28dd7e182dfa641550dfa225913509ad45d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_BTREE_ITER_H
3 #define _BCACHEFS_BTREE_ITER_H
4 
5 #include "bset.h"
6 #include "btree_types.h"
7 #include "trace.h"
8 
9 static inline int __bkey_err(const struct bkey *k)
10 {
11 	return PTR_ERR_OR_ZERO(k);
12 }
13 
14 #define bkey_err(_k)	__bkey_err((_k).k)
15 
16 static inline void __btree_path_get(struct btree_path *path, bool intent)
17 {
18 	path->ref++;
19 	path->intent_ref += intent;
20 }
21 
22 static inline bool __btree_path_put(struct btree_path *path, bool intent)
23 {
24 	EBUG_ON(!path->ref);
25 	EBUG_ON(!path->intent_ref && intent);
26 	path->intent_ref -= intent;
27 	return --path->ref == 0;
28 }
29 
30 static inline void btree_path_set_dirty(struct btree_path *path,
31 					enum btree_path_uptodate u)
32 {
33 	path->uptodate = max_t(unsigned, path->uptodate, u);
34 }
35 
36 static inline struct btree *btree_path_node(struct btree_path *path,
37 					    unsigned level)
38 {
39 	return level < BTREE_MAX_DEPTH ? path->l[level].b : NULL;
40 }
41 
42 static inline bool btree_node_lock_seq_matches(const struct btree_path *path,
43 					const struct btree *b, unsigned level)
44 {
45 	return path->l[level].lock_seq == six_lock_seq(&b->c.lock);
46 }
47 
48 static inline struct btree *btree_node_parent(struct btree_path *path,
49 					      struct btree *b)
50 {
51 	return btree_path_node(path, b->c.level + 1);
52 }
53 
54 /* Iterate over paths within a transaction: */
55 
56 void __bch2_btree_trans_sort_paths(struct btree_trans *);
57 
58 static inline void btree_trans_sort_paths(struct btree_trans *trans)
59 {
60 	if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG) &&
61 	    trans->paths_sorted)
62 		return;
63 	__bch2_btree_trans_sort_paths(trans);
64 }
65 
66 static inline struct btree_path *
67 __trans_next_path(struct btree_trans *trans, unsigned idx)
68 {
69 	u64 l;
70 
71 	if (idx == BTREE_ITER_MAX)
72 		return NULL;
73 
74 	l = trans->paths_allocated >> idx;
75 	if (!l)
76 		return NULL;
77 
78 	idx += __ffs64(l);
79 	EBUG_ON(idx >= BTREE_ITER_MAX);
80 	EBUG_ON(trans->paths[idx].idx != idx);
81 	return &trans->paths[idx];
82 }
83 
84 #define trans_for_each_path_from(_trans, _path, _start)			\
85 	for (_path = __trans_next_path((_trans), _start);		\
86 	     (_path);							\
87 	     _path = __trans_next_path((_trans), (_path)->idx + 1))
88 
89 #define trans_for_each_path(_trans, _path)				\
90 	trans_for_each_path_from(_trans, _path, 0)
91 
92 static inline struct btree_path *
93 __trans_next_path_safe(struct btree_trans *trans, unsigned *idx)
94 {
95 	u64 l;
96 
97 	if (*idx == BTREE_ITER_MAX)
98 		return NULL;
99 
100 	l = trans->paths_allocated >> *idx;
101 	if (!l)
102 		return NULL;
103 
104 	*idx += __ffs64(l);
105 	EBUG_ON(*idx >= BTREE_ITER_MAX);
106 	return &trans->paths[*idx];
107 }
108 
109 /*
110  * This version is intended to be safe for use on a btree_trans that is owned by
111  * another thread, for bch2_btree_trans_to_text();
112  */
113 #define trans_for_each_path_safe_from(_trans, _path, _idx, _start)	\
114 	for (_idx = _start;						\
115 	     (_path = __trans_next_path_safe((_trans), &_idx));		\
116 	     _idx++)
117 
118 #define trans_for_each_path_safe(_trans, _path, _idx)			\
119 	trans_for_each_path_safe_from(_trans, _path, _idx, 0)
120 
121 static inline struct btree_path *next_btree_path(struct btree_trans *trans, struct btree_path *path)
122 {
123 	unsigned idx = path ? path->sorted_idx + 1 : 0;
124 
125 	EBUG_ON(idx > trans->nr_sorted);
126 
127 	return idx < trans->nr_sorted
128 		? trans->paths + trans->sorted[idx]
129 		: NULL;
130 }
131 
132 static inline struct btree_path *prev_btree_path(struct btree_trans *trans, struct btree_path *path)
133 {
134 	unsigned idx = path ? path->sorted_idx : trans->nr_sorted;
135 
136 	return idx
137 		? trans->paths + trans->sorted[idx - 1]
138 		: NULL;
139 }
140 
141 #define trans_for_each_path_inorder(_trans, _path, _i)			\
142 	for (_i = 0;							\
143 	     ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) < (_trans)->nr_sorted;\
144 	     _i++)
145 
146 #define trans_for_each_path_inorder_reverse(_trans, _path, _i)		\
147 	for (_i = trans->nr_sorted - 1;					\
148 	     ((_path) = (_trans)->paths + trans->sorted[_i]), (_i) >= 0;\
149 	     --_i)
150 
151 static inline bool __path_has_node(const struct btree_path *path,
152 				   const struct btree *b)
153 {
154 	return path->l[b->c.level].b == b &&
155 		btree_node_lock_seq_matches(path, b, b->c.level);
156 }
157 
158 static inline struct btree_path *
159 __trans_next_path_with_node(struct btree_trans *trans, struct btree *b,
160 			    unsigned idx)
161 {
162 	struct btree_path *path = __trans_next_path(trans, idx);
163 
164 	while (path && !__path_has_node(path, b))
165 		path = __trans_next_path(trans, path->idx + 1);
166 
167 	return path;
168 }
169 
170 #define trans_for_each_path_with_node(_trans, _b, _path)		\
171 	for (_path = __trans_next_path_with_node((_trans), (_b), 0);	\
172 	     (_path);							\
173 	     _path = __trans_next_path_with_node((_trans), (_b),	\
174 						 (_path)->idx + 1))
175 
176 struct btree_path *__bch2_btree_path_make_mut(struct btree_trans *, struct btree_path *,
177 			 bool, unsigned long);
178 
179 static inline struct btree_path * __must_check
180 bch2_btree_path_make_mut(struct btree_trans *trans,
181 			 struct btree_path *path, bool intent,
182 			 unsigned long ip)
183 {
184 	if (path->ref > 1 || path->preserve)
185 		path = __bch2_btree_path_make_mut(trans, path, intent, ip);
186 	path->should_be_locked = false;
187 	return path;
188 }
189 
190 struct btree_path * __must_check
191 __bch2_btree_path_set_pos(struct btree_trans *, struct btree_path *,
192 			struct bpos, bool, unsigned long, int);
193 
194 static inline struct btree_path * __must_check
195 bch2_btree_path_set_pos(struct btree_trans *trans,
196 		   struct btree_path *path, struct bpos new_pos,
197 		   bool intent, unsigned long ip)
198 {
199 	int cmp = bpos_cmp(new_pos, path->pos);
200 
201 	return cmp
202 		? __bch2_btree_path_set_pos(trans, path, new_pos, intent, ip, cmp)
203 		: path;
204 }
205 
206 int __must_check bch2_btree_path_traverse_one(struct btree_trans *, struct btree_path *,
207 					      unsigned, unsigned long);
208 
209 static inline int __must_check bch2_btree_path_traverse(struct btree_trans *trans,
210 					  struct btree_path *path, unsigned flags)
211 {
212 	if (path->uptodate < BTREE_ITER_NEED_RELOCK)
213 		return 0;
214 
215 	return bch2_btree_path_traverse_one(trans, path, flags, _RET_IP_);
216 }
217 
218 int __must_check bch2_btree_path_traverse(struct btree_trans *,
219 					  struct btree_path *, unsigned);
220 struct btree_path *bch2_path_get(struct btree_trans *, enum btree_id, struct bpos,
221 				 unsigned, unsigned, unsigned, unsigned long);
222 struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *, struct bkey *);
223 
224 /*
225  * bch2_btree_path_peek_slot() for a cached iterator might return a key in a
226  * different snapshot:
227  */
228 static inline struct bkey_s_c bch2_btree_path_peek_slot_exact(struct btree_path *path, struct bkey *u)
229 {
230 	struct bkey_s_c k = bch2_btree_path_peek_slot(path, u);
231 
232 	if (k.k && bpos_eq(path->pos, k.k->p))
233 		return k;
234 
235 	bkey_init(u);
236 	u->p = path->pos;
237 	return (struct bkey_s_c) { u, NULL };
238 }
239 
240 struct bkey_i *bch2_btree_journal_peek_slot(struct btree_trans *,
241 					struct btree_iter *, struct bpos);
242 
243 void bch2_btree_path_level_init(struct btree_trans *, struct btree_path *, struct btree *);
244 
245 int __bch2_trans_mutex_lock(struct btree_trans *, struct mutex *);
246 
247 static inline int bch2_trans_mutex_lock(struct btree_trans *trans, struct mutex *lock)
248 {
249 	return mutex_trylock(lock)
250 		? 0
251 		: __bch2_trans_mutex_lock(trans, lock);
252 }
253 
254 #ifdef CONFIG_BCACHEFS_DEBUG
255 void bch2_trans_verify_paths(struct btree_trans *);
256 void bch2_assert_pos_locked(struct btree_trans *, enum btree_id,
257 			    struct bpos, bool);
258 #else
259 static inline void bch2_trans_verify_paths(struct btree_trans *trans) {}
260 static inline void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
261 					  struct bpos pos, bool key_cache) {}
262 #endif
263 
264 void bch2_btree_path_fix_key_modified(struct btree_trans *trans,
265 				      struct btree *, struct bkey_packed *);
266 void bch2_btree_node_iter_fix(struct btree_trans *trans, struct btree_path *,
267 			      struct btree *, struct btree_node_iter *,
268 			      struct bkey_packed *, unsigned, unsigned);
269 
270 int bch2_btree_path_relock_intent(struct btree_trans *, struct btree_path *);
271 
272 void bch2_path_put(struct btree_trans *, struct btree_path *, bool);
273 
274 int bch2_trans_relock(struct btree_trans *);
275 int bch2_trans_relock_notrace(struct btree_trans *);
276 void bch2_trans_unlock(struct btree_trans *);
277 bool bch2_trans_locked(struct btree_trans *);
278 
279 static inline int trans_was_restarted(struct btree_trans *trans, u32 restart_count)
280 {
281 	return restart_count != trans->restart_count
282 		? -BCH_ERR_transaction_restart_nested
283 		: 0;
284 }
285 
286 void __noreturn bch2_trans_restart_error(struct btree_trans *, u32);
287 
288 static inline void bch2_trans_verify_not_restarted(struct btree_trans *trans,
289 						   u32 restart_count)
290 {
291 	if (trans_was_restarted(trans, restart_count))
292 		bch2_trans_restart_error(trans, restart_count);
293 }
294 
295 void __noreturn bch2_trans_in_restart_error(struct btree_trans *);
296 
297 static inline void bch2_trans_verify_not_in_restart(struct btree_trans *trans)
298 {
299 	if (trans->restarted)
300 		bch2_trans_in_restart_error(trans);
301 }
302 
303 __always_inline
304 static int btree_trans_restart_nounlock(struct btree_trans *trans, int err)
305 {
306 	BUG_ON(err <= 0);
307 	BUG_ON(!bch2_err_matches(-err, BCH_ERR_transaction_restart));
308 
309 	trans->restarted = err;
310 	trans->last_restarted_ip = _THIS_IP_;
311 	return -err;
312 }
313 
314 __always_inline
315 static int btree_trans_restart(struct btree_trans *trans, int err)
316 {
317 	btree_trans_restart_nounlock(trans, err);
318 	return -err;
319 }
320 
321 bool bch2_btree_node_upgrade(struct btree_trans *,
322 			     struct btree_path *, unsigned);
323 
324 void __bch2_btree_path_downgrade(struct btree_trans *, struct btree_path *, unsigned);
325 
326 static inline void bch2_btree_path_downgrade(struct btree_trans *trans,
327 					     struct btree_path *path)
328 {
329 	unsigned new_locks_want = path->level + !!path->intent_ref;
330 
331 	if (path->locks_want > new_locks_want)
332 		__bch2_btree_path_downgrade(trans, path, new_locks_want);
333 }
334 
335 void bch2_trans_downgrade(struct btree_trans *);
336 
337 void bch2_trans_node_add(struct btree_trans *trans, struct btree *);
338 void bch2_trans_node_reinit_iter(struct btree_trans *, struct btree *);
339 
340 int __must_check __bch2_btree_iter_traverse(struct btree_iter *iter);
341 int __must_check bch2_btree_iter_traverse(struct btree_iter *);
342 
343 struct btree *bch2_btree_iter_peek_node(struct btree_iter *);
344 struct btree *bch2_btree_iter_peek_node_and_restart(struct btree_iter *);
345 struct btree *bch2_btree_iter_next_node(struct btree_iter *);
346 
347 struct bkey_s_c bch2_btree_iter_peek_upto(struct btree_iter *, struct bpos);
348 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *);
349 
350 struct bkey_s_c bch2_btree_iter_peek_all_levels(struct btree_iter *);
351 
352 static inline struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *iter)
353 {
354 	return bch2_btree_iter_peek_upto(iter, SPOS_MAX);
355 }
356 
357 struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *);
358 struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *);
359 
360 struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *);
361 struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *);
362 struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *);
363 
364 bool bch2_btree_iter_advance(struct btree_iter *);
365 bool bch2_btree_iter_rewind(struct btree_iter *);
366 
367 static inline void __bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
368 {
369 	iter->k.type = KEY_TYPE_deleted;
370 	iter->k.p.inode		= iter->pos.inode	= new_pos.inode;
371 	iter->k.p.offset	= iter->pos.offset	= new_pos.offset;
372 	iter->k.p.snapshot	= iter->pos.snapshot	= new_pos.snapshot;
373 	iter->k.size = 0;
374 }
375 
376 static inline void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
377 {
378 	if (unlikely(iter->update_path))
379 		bch2_path_put(iter->trans, iter->update_path,
380 			      iter->flags & BTREE_ITER_INTENT);
381 	iter->update_path = NULL;
382 
383 	if (!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS))
384 		new_pos.snapshot = iter->snapshot;
385 
386 	__bch2_btree_iter_set_pos(iter, new_pos);
387 }
388 
389 static inline void bch2_btree_iter_set_pos_to_extent_start(struct btree_iter *iter)
390 {
391 	BUG_ON(!(iter->flags & BTREE_ITER_IS_EXTENTS));
392 	iter->pos = bkey_start_pos(&iter->k);
393 }
394 
395 static inline void bch2_btree_iter_set_snapshot(struct btree_iter *iter, u32 snapshot)
396 {
397 	struct bpos pos = iter->pos;
398 
399 	iter->snapshot = snapshot;
400 	pos.snapshot = snapshot;
401 	bch2_btree_iter_set_pos(iter, pos);
402 }
403 
404 void bch2_trans_iter_exit(struct btree_trans *, struct btree_iter *);
405 
406 static inline unsigned __bch2_btree_iter_flags(struct btree_trans *trans,
407 					       unsigned btree_id,
408 					       unsigned flags)
409 {
410 	if (flags & BTREE_ITER_ALL_LEVELS)
411 		flags |= BTREE_ITER_ALL_SNAPSHOTS|__BTREE_ITER_ALL_SNAPSHOTS;
412 
413 	if (!(flags & (BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_NOT_EXTENTS)) &&
414 	    btree_node_type_is_extents(btree_id))
415 		flags |= BTREE_ITER_IS_EXTENTS;
416 
417 	if (!(flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
418 	    !btree_type_has_snapshots(btree_id))
419 		flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
420 
421 	if (!(flags & BTREE_ITER_ALL_SNAPSHOTS) &&
422 	    btree_type_has_snapshots(btree_id))
423 		flags |= BTREE_ITER_FILTER_SNAPSHOTS;
424 
425 	if (trans->journal_replay_not_finished)
426 		flags |= BTREE_ITER_WITH_JOURNAL;
427 
428 	return flags;
429 }
430 
431 static inline unsigned bch2_btree_iter_flags(struct btree_trans *trans,
432 					     unsigned btree_id,
433 					     unsigned flags)
434 {
435 	if (!btree_id_cached(trans->c, btree_id)) {
436 		flags &= ~BTREE_ITER_CACHED;
437 		flags &= ~BTREE_ITER_WITH_KEY_CACHE;
438 	} else if (!(flags & BTREE_ITER_CACHED))
439 		flags |= BTREE_ITER_WITH_KEY_CACHE;
440 
441 	return __bch2_btree_iter_flags(trans, btree_id, flags);
442 }
443 
444 static inline void bch2_trans_iter_init_common(struct btree_trans *trans,
445 					  struct btree_iter *iter,
446 					  unsigned btree_id, struct bpos pos,
447 					  unsigned locks_want,
448 					  unsigned depth,
449 					  unsigned flags,
450 					  unsigned long ip)
451 {
452 	memset(iter, 0, sizeof(*iter));
453 	iter->trans	= trans;
454 	iter->btree_id	= btree_id;
455 	iter->flags	= flags;
456 	iter->snapshot	= pos.snapshot;
457 	iter->pos	= pos;
458 	iter->k.p	= pos;
459 
460 #ifdef CONFIG_BCACHEFS_DEBUG
461 	iter->ip_allocated = ip;
462 #endif
463 	iter->path = bch2_path_get(trans, btree_id, iter->pos,
464 				   locks_want, depth, flags, ip);
465 }
466 
467 void bch2_trans_iter_init_outlined(struct btree_trans *, struct btree_iter *,
468 			  enum btree_id, struct bpos, unsigned);
469 
470 static inline void bch2_trans_iter_init(struct btree_trans *trans,
471 			  struct btree_iter *iter,
472 			  unsigned btree_id, struct bpos pos,
473 			  unsigned flags)
474 {
475 	if (__builtin_constant_p(btree_id) &&
476 	    __builtin_constant_p(flags))
477 		bch2_trans_iter_init_common(trans, iter, btree_id, pos, 0, 0,
478 				bch2_btree_iter_flags(trans, btree_id, flags),
479 				_THIS_IP_);
480 	else
481 		bch2_trans_iter_init_outlined(trans, iter, btree_id, pos, flags);
482 }
483 
484 void bch2_trans_node_iter_init(struct btree_trans *, struct btree_iter *,
485 			       enum btree_id, struct bpos,
486 			       unsigned, unsigned, unsigned);
487 void bch2_trans_copy_iter(struct btree_iter *, struct btree_iter *);
488 
489 static inline void set_btree_iter_dontneed(struct btree_iter *iter)
490 {
491 	if (!iter->trans->restarted)
492 		iter->path->preserve = false;
493 }
494 
495 void *__bch2_trans_kmalloc(struct btree_trans *, size_t);
496 
497 static inline void *bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
498 {
499 	size = roundup(size, 8);
500 
501 	if (likely(trans->mem_top + size <= trans->mem_bytes)) {
502 		void *p = trans->mem + trans->mem_top;
503 
504 		trans->mem_top += size;
505 		memset(p, 0, size);
506 		return p;
507 	} else {
508 		return __bch2_trans_kmalloc(trans, size);
509 	}
510 }
511 
512 static inline void *bch2_trans_kmalloc_nomemzero(struct btree_trans *trans, size_t size)
513 {
514 	size = roundup(size, 8);
515 
516 	if (likely(trans->mem_top + size <= trans->mem_bytes)) {
517 		void *p = trans->mem + trans->mem_top;
518 
519 		trans->mem_top += size;
520 		return p;
521 	} else {
522 		return __bch2_trans_kmalloc(trans, size);
523 	}
524 }
525 
526 static inline struct bkey_s_c __bch2_bkey_get_iter(struct btree_trans *trans,
527 				struct btree_iter *iter,
528 				unsigned btree_id, struct bpos pos,
529 				unsigned flags, unsigned type)
530 {
531 	struct bkey_s_c k;
532 
533 	bch2_trans_iter_init(trans, iter, btree_id, pos, flags);
534 	k = bch2_btree_iter_peek_slot(iter);
535 
536 	if (!bkey_err(k) && type && k.k->type != type)
537 		k = bkey_s_c_err(-BCH_ERR_ENOENT_bkey_type_mismatch);
538 	if (unlikely(bkey_err(k)))
539 		bch2_trans_iter_exit(trans, iter);
540 	return k;
541 }
542 
543 static inline struct bkey_s_c bch2_bkey_get_iter(struct btree_trans *trans,
544 				struct btree_iter *iter,
545 				unsigned btree_id, struct bpos pos,
546 				unsigned flags)
547 {
548 	return __bch2_bkey_get_iter(trans, iter, btree_id, pos, flags, 0);
549 }
550 
551 #define bch2_bkey_get_iter_typed(_trans, _iter, _btree_id, _pos, _flags, _type)\
552 	bkey_s_c_to_##_type(__bch2_bkey_get_iter(_trans, _iter,			\
553 				       _btree_id, _pos, _flags, KEY_TYPE_##_type))
554 
555 static inline int __bch2_bkey_get_val_typed(struct btree_trans *trans,
556 				unsigned btree_id, struct bpos pos,
557 				unsigned flags, unsigned type,
558 				unsigned val_size, void *val)
559 {
560 	struct btree_iter iter;
561 	struct bkey_s_c k;
562 	int ret;
563 
564 	k = __bch2_bkey_get_iter(trans, &iter, btree_id, pos, flags, type);
565 	ret = bkey_err(k);
566 	if (!ret) {
567 		unsigned b = min_t(unsigned, bkey_val_bytes(k.k), val_size);
568 
569 		memcpy(val, k.v, b);
570 		if (unlikely(b < sizeof(*val)))
571 			memset((void *) val + b, 0, sizeof(*val) - b);
572 		bch2_trans_iter_exit(trans, &iter);
573 	}
574 
575 	return ret;
576 }
577 
578 #define bch2_bkey_get_val_typed(_trans, _btree_id, _pos, _flags, _type, _val)\
579 	__bch2_bkey_get_val_typed(_trans, _btree_id, _pos, _flags,	\
580 				  KEY_TYPE_##_type, sizeof(*_val), _val)
581 
582 u32 bch2_trans_begin(struct btree_trans *);
583 
584 /*
585  * XXX
586  * this does not handle transaction restarts from bch2_btree_iter_next_node()
587  * correctly
588  */
589 #define __for_each_btree_node(_trans, _iter, _btree_id, _start,		\
590 			      _locks_want, _depth, _flags, _b, _ret)	\
591 	for (bch2_trans_node_iter_init((_trans), &(_iter), (_btree_id),	\
592 				_start, _locks_want, _depth, _flags);	\
593 	     (_b) = bch2_btree_iter_peek_node_and_restart(&(_iter)),	\
594 	     !((_ret) = PTR_ERR_OR_ZERO(_b)) && (_b);			\
595 	     (_b) = bch2_btree_iter_next_node(&(_iter)))
596 
597 #define for_each_btree_node(_trans, _iter, _btree_id, _start,		\
598 			    _flags, _b, _ret)				\
599 	__for_each_btree_node(_trans, _iter, _btree_id, _start,		\
600 			      0, 0, _flags, _b, _ret)
601 
602 static inline struct bkey_s_c bch2_btree_iter_peek_prev_type(struct btree_iter *iter,
603 							     unsigned flags)
604 {
605 	BUG_ON(flags & BTREE_ITER_ALL_LEVELS);
606 
607 	return  flags & BTREE_ITER_SLOTS      ? bch2_btree_iter_peek_slot(iter) :
608 						bch2_btree_iter_peek_prev(iter);
609 }
610 
611 static inline struct bkey_s_c bch2_btree_iter_peek_type(struct btree_iter *iter,
612 							unsigned flags)
613 {
614 	return  flags & BTREE_ITER_ALL_LEVELS ? bch2_btree_iter_peek_all_levels(iter) :
615 		flags & BTREE_ITER_SLOTS      ? bch2_btree_iter_peek_slot(iter) :
616 						bch2_btree_iter_peek(iter);
617 }
618 
619 static inline struct bkey_s_c bch2_btree_iter_peek_upto_type(struct btree_iter *iter,
620 							     struct bpos end,
621 							     unsigned flags)
622 {
623 	if (!(flags & BTREE_ITER_SLOTS))
624 		return bch2_btree_iter_peek_upto(iter, end);
625 
626 	if (bkey_gt(iter->pos, end))
627 		return bkey_s_c_null;
628 
629 	return bch2_btree_iter_peek_slot(iter);
630 }
631 
632 static inline int btree_trans_too_many_iters(struct btree_trans *trans)
633 {
634 	if (hweight64(trans->paths_allocated) > BTREE_ITER_MAX - 8) {
635 		trace_and_count(trans->c, trans_restart_too_many_iters, trans, _THIS_IP_);
636 		return btree_trans_restart(trans, BCH_ERR_transaction_restart_too_many_iters);
637 	}
638 
639 	return 0;
640 }
641 
642 struct bkey_s_c bch2_btree_iter_peek_and_restart_outlined(struct btree_iter *);
643 
644 static inline struct bkey_s_c
645 __bch2_btree_iter_peek_and_restart(struct btree_trans *trans,
646 				   struct btree_iter *iter, unsigned flags)
647 {
648 	struct bkey_s_c k;
649 
650 	while (btree_trans_too_many_iters(trans) ||
651 	       (k = bch2_btree_iter_peek_type(iter, flags),
652 		bch2_err_matches(bkey_err(k), BCH_ERR_transaction_restart)))
653 		bch2_trans_begin(trans);
654 
655 	return k;
656 }
657 
658 static inline struct bkey_s_c
659 __bch2_btree_iter_peek_upto_and_restart(struct btree_trans *trans,
660 					struct btree_iter *iter,
661 					struct bpos end,
662 					unsigned flags)
663 {
664 	struct bkey_s_c k;
665 
666 	while (btree_trans_too_many_iters(trans) ||
667 	       (k = bch2_btree_iter_peek_upto_type(iter, end, flags),
668 		bch2_err_matches(bkey_err(k), BCH_ERR_transaction_restart)))
669 		bch2_trans_begin(trans);
670 
671 	return k;
672 }
673 
674 #define lockrestart_do(_trans, _do)					\
675 ({									\
676 	u32 _restart_count;						\
677 	int _ret2;							\
678 									\
679 	do {								\
680 		_restart_count = bch2_trans_begin(_trans);		\
681 		_ret2 = (_do);						\
682 	} while (bch2_err_matches(_ret2, BCH_ERR_transaction_restart));	\
683 									\
684 	if (!_ret2)							\
685 		bch2_trans_verify_not_restarted(_trans, _restart_count);\
686 									\
687 	_ret2;								\
688 })
689 
690 /*
691  * nested_lockrestart_do(), nested_commit_do():
692  *
693  * These are like lockrestart_do() and commit_do(), with two differences:
694  *
695  *  - We don't call bch2_trans_begin() unless we had a transaction restart
696  *  - We return -BCH_ERR_transaction_restart_nested if we succeeded after a
697  *  transaction restart
698  */
699 #define nested_lockrestart_do(_trans, _do)				\
700 ({									\
701 	u32 _restart_count, _orig_restart_count;			\
702 	int _ret2;							\
703 									\
704 	_restart_count = _orig_restart_count = (_trans)->restart_count;	\
705 									\
706 	while (bch2_err_matches(_ret2 = (_do), BCH_ERR_transaction_restart))\
707 		_restart_count = bch2_trans_begin(_trans);		\
708 									\
709 	if (!_ret2)							\
710 		bch2_trans_verify_not_restarted(_trans, _restart_count);\
711 									\
712 	_ret2 ?: trans_was_restarted(_trans, _restart_count);		\
713 })
714 
715 #define for_each_btree_key2(_trans, _iter, _btree_id,			\
716 			    _start, _flags, _k, _do)			\
717 ({									\
718 	int _ret3 = 0;							\
719 									\
720 	bch2_trans_iter_init((_trans), &(_iter), (_btree_id),		\
721 			     (_start), (_flags));			\
722 									\
723 	while (1) {							\
724 		u32 _restart_count = bch2_trans_begin(_trans);		\
725 									\
726 		_ret3 = 0;						\
727 		(_k) = bch2_btree_iter_peek_type(&(_iter), (_flags));	\
728 		if (!(_k).k)						\
729 			break;						\
730 									\
731 		_ret3 = bkey_err(_k) ?: (_do);				\
732 		if (bch2_err_matches(_ret3, BCH_ERR_transaction_restart))\
733 			continue;					\
734 		if (_ret3)						\
735 			break;						\
736 		bch2_trans_verify_not_restarted(_trans, _restart_count);\
737 		if (!bch2_btree_iter_advance(&(_iter)))			\
738 			break;						\
739 	}								\
740 									\
741 	bch2_trans_iter_exit((_trans), &(_iter));			\
742 	_ret3;								\
743 })
744 
745 #define for_each_btree_key2_upto(_trans, _iter, _btree_id,		\
746 			    _start, _end, _flags, _k, _do)		\
747 ({									\
748 	int _ret3 = 0;							\
749 									\
750 	bch2_trans_iter_init((_trans), &(_iter), (_btree_id),		\
751 			     (_start), (_flags));			\
752 									\
753 	while (1) {							\
754 		u32 _restart_count = bch2_trans_begin(_trans);		\
755 									\
756 		_ret3 = 0;						\
757 		(_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, (_flags));\
758 		if (!(_k).k)						\
759 			break;						\
760 									\
761 		_ret3 = bkey_err(_k) ?: (_do);				\
762 		if (bch2_err_matches(_ret3, BCH_ERR_transaction_restart))\
763 			continue;					\
764 		if (_ret3)						\
765 			break;						\
766 		bch2_trans_verify_not_restarted(_trans, _restart_count);\
767 		if (!bch2_btree_iter_advance(&(_iter)))			\
768 			break;						\
769 	}								\
770 									\
771 	bch2_trans_iter_exit((_trans), &(_iter));			\
772 	_ret3;								\
773 })
774 
775 #define for_each_btree_key_reverse(_trans, _iter, _btree_id,		\
776 				   _start, _flags, _k, _do)		\
777 ({									\
778 	int _ret3 = 0;							\
779 									\
780 	bch2_trans_iter_init((_trans), &(_iter), (_btree_id),		\
781 			     (_start), (_flags));			\
782 									\
783 	while (1) {							\
784 		u32 _restart_count = bch2_trans_begin(_trans);		\
785 		(_k) = bch2_btree_iter_peek_prev_type(&(_iter), (_flags));\
786 		if (!(_k).k) {						\
787 			_ret3 = 0;					\
788 			break;						\
789 		}							\
790 									\
791 		_ret3 = bkey_err(_k) ?: (_do);				\
792 		if (bch2_err_matches(_ret3, BCH_ERR_transaction_restart))\
793 			continue;					\
794 		if (_ret3)						\
795 			break;						\
796 		bch2_trans_verify_not_restarted(_trans, _restart_count);\
797 		if (!bch2_btree_iter_rewind(&(_iter)))			\
798 			break;						\
799 	}								\
800 									\
801 	bch2_trans_iter_exit((_trans), &(_iter));			\
802 	_ret3;								\
803 })
804 
805 #define for_each_btree_key_commit(_trans, _iter, _btree_id,		\
806 				  _start, _iter_flags, _k,		\
807 				  _disk_res, _journal_seq, _commit_flags,\
808 				  _do)					\
809 	for_each_btree_key2(_trans, _iter, _btree_id, _start, _iter_flags, _k,\
810 			    (_do) ?: bch2_trans_commit(_trans, (_disk_res),\
811 					(_journal_seq), (_commit_flags)))
812 
813 #define for_each_btree_key_reverse_commit(_trans, _iter, _btree_id,	\
814 				  _start, _iter_flags, _k,		\
815 				  _disk_res, _journal_seq, _commit_flags,\
816 				  _do)					\
817 	for_each_btree_key_reverse(_trans, _iter, _btree_id, _start, _iter_flags, _k,\
818 			    (_do) ?: bch2_trans_commit(_trans, (_disk_res),\
819 					(_journal_seq), (_commit_flags)))
820 
821 #define for_each_btree_key_upto_commit(_trans, _iter, _btree_id,	\
822 				  _start, _end, _iter_flags, _k,	\
823 				  _disk_res, _journal_seq, _commit_flags,\
824 				  _do)					\
825 	for_each_btree_key2_upto(_trans, _iter, _btree_id, _start, _end, _iter_flags, _k,\
826 			    (_do) ?: bch2_trans_commit(_trans, (_disk_res),\
827 					(_journal_seq), (_commit_flags)))
828 
829 #define for_each_btree_key(_trans, _iter, _btree_id,			\
830 			   _start, _flags, _k, _ret)			\
831 	for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id),	\
832 				  (_start), (_flags));			\
833 	     (_k) = __bch2_btree_iter_peek_and_restart((_trans), &(_iter), _flags),\
834 	     !((_ret) = bkey_err(_k)) && (_k).k;			\
835 	     bch2_btree_iter_advance(&(_iter)))
836 
837 #define for_each_btree_key_upto(_trans, _iter, _btree_id,		\
838 				_start, _end, _flags, _k, _ret)		\
839 	for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id),	\
840 				  (_start), (_flags));			\
841 	     (_k) = __bch2_btree_iter_peek_upto_and_restart((_trans),	\
842 						&(_iter), _end, _flags),\
843 	     !((_ret) = bkey_err(_k)) && (_k).k;			\
844 	     bch2_btree_iter_advance(&(_iter)))
845 
846 #define for_each_btree_key_norestart(_trans, _iter, _btree_id,		\
847 			   _start, _flags, _k, _ret)			\
848 	for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id),	\
849 				  (_start), (_flags));			\
850 	     (_k) = bch2_btree_iter_peek_type(&(_iter), _flags),	\
851 	     !((_ret) = bkey_err(_k)) && (_k).k;			\
852 	     bch2_btree_iter_advance(&(_iter)))
853 
854 #define for_each_btree_key_upto_norestart(_trans, _iter, _btree_id,	\
855 			   _start, _end, _flags, _k, _ret)		\
856 	for (bch2_trans_iter_init((_trans), &(_iter), (_btree_id),	\
857 				  (_start), (_flags));			\
858 	     (_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, _flags),\
859 	     !((_ret) = bkey_err(_k)) && (_k).k;			\
860 	     bch2_btree_iter_advance(&(_iter)))
861 
862 #define for_each_btree_key_continue(_trans, _iter, _flags, _k, _ret)	\
863 	for (;								\
864 	     (_k) = __bch2_btree_iter_peek_and_restart((_trans), &(_iter), _flags),\
865 	     !((_ret) = bkey_err(_k)) && (_k).k;			\
866 	     bch2_btree_iter_advance(&(_iter)))
867 
868 #define for_each_btree_key_continue_norestart(_iter, _flags, _k, _ret)	\
869 	for (;								\
870 	     (_k) = bch2_btree_iter_peek_type(&(_iter), _flags),	\
871 	     !((_ret) = bkey_err(_k)) && (_k).k;			\
872 	     bch2_btree_iter_advance(&(_iter)))
873 
874 #define for_each_btree_key_upto_continue_norestart(_iter, _end, _flags, _k, _ret)\
875 	for (;									\
876 	     (_k) = bch2_btree_iter_peek_upto_type(&(_iter), _end, _flags),	\
877 	     !((_ret) = bkey_err(_k)) && (_k).k;				\
878 	     bch2_btree_iter_advance(&(_iter)))
879 
880 #define drop_locks_do(_trans, _do)					\
881 ({									\
882 	bch2_trans_unlock(_trans);					\
883 	_do ?: bch2_trans_relock(_trans);				\
884 })
885 
886 #define allocate_dropping_locks_errcode(_trans, _do)			\
887 ({									\
888 	gfp_t _gfp = GFP_NOWAIT|__GFP_NOWARN;				\
889 	int _ret = _do;							\
890 									\
891 	if (bch2_err_matches(_ret, ENOMEM)) {				\
892 		_gfp = GFP_KERNEL;					\
893 		_ret = drop_locks_do(trans, _do);			\
894 	}								\
895 	_ret;								\
896 })
897 
898 #define allocate_dropping_locks(_trans, _ret, _do)			\
899 ({									\
900 	gfp_t _gfp = GFP_NOWAIT|__GFP_NOWARN;				\
901 	typeof(_do) _p = _do;						\
902 									\
903 	_ret = 0;							\
904 	if (unlikely(!_p)) {						\
905 		_gfp = GFP_KERNEL;					\
906 		_ret = drop_locks_do(trans, ((_p = _do), 0));		\
907 	}								\
908 	_p;								\
909 })
910 
911 /* new multiple iterator interface: */
912 
913 void bch2_trans_updates_to_text(struct printbuf *, struct btree_trans *);
914 void bch2_btree_path_to_text(struct printbuf *, struct btree_path *);
915 void bch2_trans_paths_to_text(struct printbuf *, struct btree_trans *);
916 void bch2_dump_trans_updates(struct btree_trans *);
917 void bch2_dump_trans_paths_updates(struct btree_trans *);
918 
919 struct btree_trans *__bch2_trans_get(struct bch_fs *, unsigned);
920 void bch2_trans_put(struct btree_trans *);
921 
922 extern const char *bch2_btree_transaction_fns[BCH_TRANSACTIONS_NR];
923 unsigned bch2_trans_get_fn_idx(const char *);
924 
925 #define bch2_trans_get(_c)						\
926 ({									\
927 	static unsigned trans_fn_idx;					\
928 									\
929 	if (unlikely(!trans_fn_idx))					\
930 		trans_fn_idx = bch2_trans_get_fn_idx(__func__);		\
931 	__bch2_trans_get(_c, trans_fn_idx);				\
932 })
933 
934 void bch2_btree_trans_to_text(struct printbuf *, struct btree_trans *);
935 
936 void bch2_fs_btree_iter_exit(struct bch_fs *);
937 int bch2_fs_btree_iter_init(struct bch_fs *);
938 
939 #endif /* _BCACHEFS_BTREE_ITER_H */
940