]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_iter.c
Update bcachefs sources to 078a1a596a bcachefs: Optimize bucket reuse
[bcachefs-tools-debian] / libbcachefs / btree_iter.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "bkey_methods.h"
5 #include "bkey_buf.h"
6 #include "btree_cache.h"
7 #include "btree_iter.h"
8 #include "btree_key_cache.h"
9 #include "btree_locking.h"
10 #include "btree_update.h"
11 #include "debug.h"
12 #include "error.h"
13 #include "extents.h"
14 #include "journal.h"
15 #include "replicas.h"
16 #include "subvolume.h"
17
18 #include <linux/prefetch.h>
19 #include <trace/events/bcachefs.h>
20
21 static void btree_trans_verify_sorted(struct btree_trans *);
22 static void btree_path_check_sort(struct btree_trans *, struct btree_path *, int);
23
24 static inline void btree_path_list_remove(struct btree_trans *, struct btree_path *);
25 static inline void btree_path_list_add(struct btree_trans *, struct btree_path *,
26                                        struct btree_path *);
27
28 static inline unsigned long btree_iter_ip_allocated(struct btree_iter *iter)
29 {
30 #ifdef CONFIG_BCACHEFS_DEBUG
31         return iter->ip_allocated;
32 #else
33         return 0;
34 #endif
35 }
36
37 static struct btree_path *btree_path_alloc(struct btree_trans *, struct btree_path *);
38
39 /*
40  * Unlocks before scheduling
41  * Note: does not revalidate iterator
42  */
43 static inline int bch2_trans_cond_resched(struct btree_trans *trans)
44 {
45         if (need_resched() || race_fault()) {
46                 bch2_trans_unlock(trans);
47                 schedule();
48                 return bch2_trans_relock(trans) ? 0 : -EINTR;
49         } else {
50                 return 0;
51         }
52 }
53
54 static inline int __btree_path_cmp(const struct btree_path *l,
55                                    enum btree_id        r_btree_id,
56                                    bool                 r_cached,
57                                    struct bpos          r_pos,
58                                    unsigned             r_level)
59 {
60         return   cmp_int(l->btree_id,   r_btree_id) ?:
61                  cmp_int((int) l->cached,       (int) r_cached) ?:
62                  bpos_cmp(l->pos,       r_pos) ?:
63                 -cmp_int(l->level,      r_level);
64 }
65
66 static inline int btree_path_cmp(const struct btree_path *l,
67                                  const struct btree_path *r)
68 {
69         return __btree_path_cmp(l, r->btree_id, r->cached, r->pos, r->level);
70 }
71
72 static inline struct bpos bkey_successor(struct btree_iter *iter, struct bpos p)
73 {
74         /* Are we iterating over keys in all snapshots? */
75         if (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) {
76                 p = bpos_successor(p);
77         } else {
78                 p = bpos_nosnap_successor(p);
79                 p.snapshot = iter->snapshot;
80         }
81
82         return p;
83 }
84
85 static inline struct bpos bkey_predecessor(struct btree_iter *iter, struct bpos p)
86 {
87         /* Are we iterating over keys in all snapshots? */
88         if (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) {
89                 p = bpos_predecessor(p);
90         } else {
91                 p = bpos_nosnap_predecessor(p);
92                 p.snapshot = iter->snapshot;
93         }
94
95         return p;
96 }
97
98 static inline bool is_btree_node(struct btree_path *path, unsigned l)
99 {
100         return l < BTREE_MAX_DEPTH &&
101                 (unsigned long) path->l[l].b >= 128;
102 }
103
104 static inline struct bpos btree_iter_search_key(struct btree_iter *iter)
105 {
106         struct bpos pos = iter->pos;
107
108         if ((iter->flags & BTREE_ITER_IS_EXTENTS) &&
109             bkey_cmp(pos, POS_MAX))
110                 pos = bkey_successor(iter, pos);
111         return pos;
112 }
113
114 static inline bool btree_path_pos_before_node(struct btree_path *path,
115                                               struct btree *b)
116 {
117         return bpos_cmp(path->pos, b->data->min_key) < 0;
118 }
119
120 static inline bool btree_path_pos_after_node(struct btree_path *path,
121                                              struct btree *b)
122 {
123         return bpos_cmp(b->key.k.p, path->pos) < 0;
124 }
125
126 static inline bool btree_path_pos_in_node(struct btree_path *path,
127                                           struct btree *b)
128 {
129         return path->btree_id == b->c.btree_id &&
130                 !btree_path_pos_before_node(path, b) &&
131                 !btree_path_pos_after_node(path, b);
132 }
133
134 /* Btree node locking: */
135
136 void bch2_btree_node_unlock_write(struct btree_trans *trans,
137                         struct btree_path *path, struct btree *b)
138 {
139         bch2_btree_node_unlock_write_inlined(trans, path, b);
140 }
141
142 void __bch2_btree_node_lock_write(struct btree_trans *trans, struct btree *b)
143 {
144         struct btree_path *linked;
145         unsigned readers = 0;
146
147         trans_for_each_path(trans, linked)
148                 if (linked->l[b->c.level].b == b &&
149                     btree_node_read_locked(linked, b->c.level))
150                         readers++;
151
152         /*
153          * Must drop our read locks before calling six_lock_write() -
154          * six_unlock() won't do wakeups until the reader count
155          * goes to 0, and it's safe because we have the node intent
156          * locked:
157          */
158         if (!b->c.lock.readers)
159                 atomic64_sub(__SIX_VAL(read_lock, readers),
160                              &b->c.lock.state.counter);
161         else
162                 this_cpu_sub(*b->c.lock.readers, readers);
163
164         btree_node_lock_type(trans->c, b, SIX_LOCK_write);
165
166         if (!b->c.lock.readers)
167                 atomic64_add(__SIX_VAL(read_lock, readers),
168                              &b->c.lock.state.counter);
169         else
170                 this_cpu_add(*b->c.lock.readers, readers);
171 }
172
173 bool __bch2_btree_node_relock(struct btree_trans *trans,
174                               struct btree_path *path, unsigned level)
175 {
176         struct btree *b = btree_path_node(path, level);
177         int want = __btree_lock_want(path, level);
178
179         if (!is_btree_node(path, level))
180                 return false;
181
182         if (race_fault())
183                 return false;
184
185         if (six_relock_type(&b->c.lock, want, path->l[level].lock_seq) ||
186             (btree_node_lock_seq_matches(path, b, level) &&
187              btree_node_lock_increment(trans, b, level, want))) {
188                 mark_btree_node_locked(path, level, want);
189                 return true;
190         } else {
191                 return false;
192         }
193 }
194
195 bool bch2_btree_node_upgrade(struct btree_trans *trans,
196                              struct btree_path *path, unsigned level)
197 {
198         struct btree *b = path->l[level].b;
199
200         if (!is_btree_node(path, level))
201                 return false;
202
203         switch (btree_lock_want(path, level)) {
204         case BTREE_NODE_UNLOCKED:
205                 BUG_ON(btree_node_locked(path, level));
206                 return true;
207         case BTREE_NODE_READ_LOCKED:
208                 BUG_ON(btree_node_intent_locked(path, level));
209                 return bch2_btree_node_relock(trans, path, level);
210         case BTREE_NODE_INTENT_LOCKED:
211                 break;
212         }
213
214         if (btree_node_intent_locked(path, level))
215                 return true;
216
217         if (race_fault())
218                 return false;
219
220         if (btree_node_locked(path, level)
221             ? six_lock_tryupgrade(&b->c.lock)
222             : six_relock_type(&b->c.lock, SIX_LOCK_intent, path->l[level].lock_seq))
223                 goto success;
224
225         if (btree_node_lock_seq_matches(path, b, level) &&
226             btree_node_lock_increment(trans, b, level, BTREE_NODE_INTENT_LOCKED)) {
227                 btree_node_unlock(path, level);
228                 goto success;
229         }
230
231         return false;
232 success:
233         mark_btree_node_intent_locked(path, level);
234         return true;
235 }
236
237 static inline bool btree_path_get_locks(struct btree_trans *trans,
238                                         struct btree_path *path,
239                                         bool upgrade, unsigned long trace_ip)
240 {
241         unsigned l = path->level;
242         int fail_idx = -1;
243
244         do {
245                 if (!btree_path_node(path, l))
246                         break;
247
248                 if (!(upgrade
249                       ? bch2_btree_node_upgrade(trans, path, l)
250                       : bch2_btree_node_relock(trans, path, l)))
251                         fail_idx = l;
252
253                 l++;
254         } while (l < path->locks_want);
255
256         /*
257          * When we fail to get a lock, we have to ensure that any child nodes
258          * can't be relocked so bch2_btree_path_traverse has to walk back up to
259          * the node that we failed to relock:
260          */
261         if (fail_idx >= 0) {
262                 __bch2_btree_path_unlock(path);
263                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
264
265                 do {
266                         path->l[fail_idx].b = BTREE_ITER_NO_NODE_GET_LOCKS;
267                         --fail_idx;
268                 } while (fail_idx >= 0);
269         }
270
271         if (path->uptodate == BTREE_ITER_NEED_RELOCK)
272                 path->uptodate = BTREE_ITER_UPTODATE;
273
274         bch2_trans_verify_locks(trans);
275
276         return path->uptodate < BTREE_ITER_NEED_RELOCK;
277 }
278
279 static struct bpos btree_node_pos(struct btree_bkey_cached_common *_b,
280                                   bool cached)
281 {
282         return !cached
283                 ? container_of(_b, struct btree, c)->key.k.p
284                 : container_of(_b, struct bkey_cached, c)->key.pos;
285 }
286
287 /* Slowpath: */
288 bool __bch2_btree_node_lock(struct btree_trans *trans,
289                             struct btree_path *path,
290                             struct btree *b,
291                             struct bpos pos, unsigned level,
292                             enum six_lock_type type,
293                             six_lock_should_sleep_fn should_sleep_fn, void *p,
294                             unsigned long ip)
295 {
296         struct btree_path *linked, *deadlock_path = NULL;
297         u64 start_time = local_clock();
298         unsigned reason = 9;
299         bool ret;
300
301         /* Check if it's safe to block: */
302         trans_for_each_path(trans, linked) {
303                 if (!linked->nodes_locked)
304                         continue;
305
306                 /*
307                  * Can't block taking an intent lock if we have _any_ nodes read
308                  * locked:
309                  *
310                  * - Our read lock blocks another thread with an intent lock on
311                  *   the same node from getting a write lock, and thus from
312                  *   dropping its intent lock
313                  *
314                  * - And the other thread may have multiple nodes intent locked:
315                  *   both the node we want to intent lock, and the node we
316                  *   already have read locked - deadlock:
317                  */
318                 if (type == SIX_LOCK_intent &&
319                     linked->nodes_locked != linked->nodes_intent_locked) {
320                         deadlock_path = linked;
321                         reason = 1;
322                 }
323
324                 if (linked->btree_id != path->btree_id) {
325                         if (linked->btree_id > path->btree_id) {
326                                 deadlock_path = linked;
327                                 reason = 3;
328                         }
329                         continue;
330                 }
331
332                 /*
333                  * Within the same btree, cached paths come before non
334                  * cached paths:
335                  */
336                 if (linked->cached != path->cached) {
337                         if (path->cached) {
338                                 deadlock_path = linked;
339                                 reason = 4;
340                         }
341                         continue;
342                 }
343
344                 /*
345                  * Interior nodes must be locked before their descendants: if
346                  * another path has possible descendants locked of the node
347                  * we're about to lock, it must have the ancestors locked too:
348                  */
349                 if (level > __fls(linked->nodes_locked)) {
350                         deadlock_path = linked;
351                         reason = 5;
352                 }
353
354                 /* Must lock btree nodes in key order: */
355                 if (btree_node_locked(linked, level) &&
356                     bpos_cmp(pos, btree_node_pos((void *) linked->l[level].b,
357                                                  linked->cached)) <= 0) {
358                         deadlock_path = linked;
359                         reason = 7;
360                         BUG_ON(trans->in_traverse_all);
361                 }
362         }
363
364         if (unlikely(deadlock_path)) {
365                 trace_trans_restart_would_deadlock(trans->ip, ip,
366                                 trans->in_traverse_all, reason,
367                                 deadlock_path->btree_id,
368                                 deadlock_path->cached,
369                                 &deadlock_path->pos,
370                                 path->btree_id,
371                                 path->cached,
372                                 &pos);
373                 btree_trans_restart(trans);
374                 return false;
375         }
376
377         if (six_trylock_type(&b->c.lock, type))
378                 return true;
379
380         trans->locking_path_idx = path->idx;
381         trans->locking_pos      = pos;
382         trans->locking_btree_id = path->btree_id;
383         trans->locking_level    = level;
384         trans->locking          = b;
385
386         ret = six_lock_type(&b->c.lock, type, should_sleep_fn, p) == 0;
387
388         trans->locking = NULL;
389
390         if (ret)
391                 bch2_time_stats_update(&trans->c->times[lock_to_time_stat(type)],
392                                        start_time);
393         return ret;
394 }
395
396 /* Btree iterator locking: */
397
398 #ifdef CONFIG_BCACHEFS_DEBUG
399
400 static void bch2_btree_path_verify_locks(struct btree_path *path)
401 {
402         unsigned l;
403
404         if (!path->nodes_locked) {
405                 BUG_ON(path->uptodate == BTREE_ITER_UPTODATE &&
406                        btree_path_node(path, path->level));
407                 return;
408         }
409
410         for (l = 0; btree_path_node(path, l); l++)
411                 BUG_ON(btree_lock_want(path, l) !=
412                        btree_node_locked_type(path, l));
413 }
414
415 void bch2_trans_verify_locks(struct btree_trans *trans)
416 {
417         struct btree_path *path;
418
419         trans_for_each_path(trans, path)
420                 bch2_btree_path_verify_locks(path);
421 }
422 #else
423 static inline void bch2_btree_path_verify_locks(struct btree_path *path) {}
424 #endif
425
426 /* Btree path locking: */
427
428 /*
429  * Only for btree_cache.c - only relocks intent locks
430  */
431 bool bch2_btree_path_relock_intent(struct btree_trans *trans,
432                                    struct btree_path *path)
433 {
434         unsigned l;
435
436         for (l = path->level;
437              l < path->locks_want && btree_path_node(path, l);
438              l++) {
439                 if (!bch2_btree_node_relock(trans, path, l)) {
440                         __bch2_btree_path_unlock(path);
441                         btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
442                         btree_trans_restart(trans);
443                         return false;
444                 }
445         }
446
447         return true;
448 }
449
450 __flatten
451 static bool bch2_btree_path_relock(struct btree_trans *trans,
452                         struct btree_path *path, unsigned long trace_ip)
453 {
454         bool ret = btree_path_get_locks(trans, path, false, trace_ip);
455
456         if (!ret)
457                 btree_trans_restart(trans);
458         return ret;
459 }
460
461 bool __bch2_btree_path_upgrade(struct btree_trans *trans,
462                                struct btree_path *path,
463                                unsigned new_locks_want)
464 {
465         struct btree_path *linked;
466
467         EBUG_ON(path->locks_want >= new_locks_want);
468
469         path->locks_want = new_locks_want;
470
471         if (btree_path_get_locks(trans, path, true, _THIS_IP_))
472                 return true;
473
474         /*
475          * XXX: this is ugly - we'd prefer to not be mucking with other
476          * iterators in the btree_trans here.
477          *
478          * On failure to upgrade the iterator, setting iter->locks_want and
479          * calling get_locks() is sufficient to make bch2_btree_path_traverse()
480          * get the locks we want on transaction restart.
481          *
482          * But if this iterator was a clone, on transaction restart what we did
483          * to this iterator isn't going to be preserved.
484          *
485          * Possibly we could add an iterator field for the parent iterator when
486          * an iterator is a copy - for now, we'll just upgrade any other
487          * iterators with the same btree id.
488          *
489          * The code below used to be needed to ensure ancestor nodes get locked
490          * before interior nodes - now that's handled by
491          * bch2_btree_path_traverse_all().
492          */
493         trans_for_each_path(trans, linked)
494                 if (linked != path &&
495                     linked->cached == path->cached &&
496                     linked->btree_id == path->btree_id &&
497                     linked->locks_want < new_locks_want) {
498                         linked->locks_want = new_locks_want;
499                         btree_path_get_locks(trans, linked, true, _THIS_IP_);
500                 }
501
502         return false;
503 }
504
505 void __bch2_btree_path_downgrade(struct btree_path *path,
506                                  unsigned new_locks_want)
507 {
508         unsigned l;
509
510         EBUG_ON(path->locks_want < new_locks_want);
511
512         path->locks_want = new_locks_want;
513
514         while (path->nodes_locked &&
515                (l = __fls(path->nodes_locked)) >= path->locks_want) {
516                 if (l > path->level) {
517                         btree_node_unlock(path, l);
518                 } else {
519                         if (btree_node_intent_locked(path, l)) {
520                                 six_lock_downgrade(&path->l[l].b->c.lock);
521                                 path->nodes_intent_locked ^= 1 << l;
522                         }
523                         break;
524                 }
525         }
526
527         bch2_btree_path_verify_locks(path);
528 }
529
530 void bch2_trans_downgrade(struct btree_trans *trans)
531 {
532         struct btree_path *path;
533
534         trans_for_each_path(trans, path)
535                 bch2_btree_path_downgrade(path);
536 }
537
538 /* Btree transaction locking: */
539
540 bool bch2_trans_relock(struct btree_trans *trans)
541 {
542         struct btree_path *path;
543
544         if (unlikely(trans->restarted))
545                 return false;
546
547         trans_for_each_path(trans, path)
548                 if (path->should_be_locked &&
549                     !bch2_btree_path_relock(trans, path, _RET_IP_)) {
550                         trace_trans_restart_relock(trans->ip, _RET_IP_,
551                                         path->btree_id, &path->pos);
552                         BUG_ON(!trans->restarted);
553                         return false;
554                 }
555         return true;
556 }
557
558 void bch2_trans_unlock(struct btree_trans *trans)
559 {
560         struct btree_path *path;
561
562         trans_for_each_path(trans, path)
563                 __bch2_btree_path_unlock(path);
564
565         BUG_ON(lock_class_is_held(&bch2_btree_node_lock_key));
566 }
567
568 /* Btree iterator: */
569
570 #ifdef CONFIG_BCACHEFS_DEBUG
571
572 static void bch2_btree_path_verify_cached(struct btree_trans *trans,
573                                           struct btree_path *path)
574 {
575         struct bkey_cached *ck;
576         bool locked = btree_node_locked(path, 0);
577
578         if (!bch2_btree_node_relock(trans, path, 0))
579                 return;
580
581         ck = (void *) path->l[0].b;
582         BUG_ON(ck->key.btree_id != path->btree_id ||
583                bkey_cmp(ck->key.pos, path->pos));
584
585         if (!locked)
586                 btree_node_unlock(path, 0);
587 }
588
589 static void bch2_btree_path_verify_level(struct btree_trans *trans,
590                                 struct btree_path *path, unsigned level)
591 {
592         struct btree_path_level *l;
593         struct btree_node_iter tmp;
594         bool locked;
595         struct bkey_packed *p, *k;
596         char buf1[100], buf2[100], buf3[100];
597         const char *msg;
598
599         if (!bch2_debug_check_iterators)
600                 return;
601
602         l       = &path->l[level];
603         tmp     = l->iter;
604         locked  = btree_node_locked(path, level);
605
606         if (path->cached) {
607                 if (!level)
608                         bch2_btree_path_verify_cached(trans, path);
609                 return;
610         }
611
612         if (!btree_path_node(path, level))
613                 return;
614
615         if (!bch2_btree_node_relock(trans, path, level))
616                 return;
617
618         BUG_ON(!btree_path_pos_in_node(path, l->b));
619
620         bch2_btree_node_iter_verify(&l->iter, l->b);
621
622         /*
623          * For interior nodes, the iterator will have skipped past deleted keys:
624          */
625         p = level
626                 ? bch2_btree_node_iter_prev(&tmp, l->b)
627                 : bch2_btree_node_iter_prev_all(&tmp, l->b);
628         k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
629
630         if (p && bkey_iter_pos_cmp(l->b, p, &path->pos) >= 0) {
631                 msg = "before";
632                 goto err;
633         }
634
635         if (k && bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) {
636                 msg = "after";
637                 goto err;
638         }
639
640         if (!locked)
641                 btree_node_unlock(path, level);
642         return;
643 err:
644         strcpy(buf2, "(none)");
645         strcpy(buf3, "(none)");
646
647         bch2_bpos_to_text(&PBUF(buf1), path->pos);
648
649         if (p) {
650                 struct bkey uk = bkey_unpack_key(l->b, p);
651                 bch2_bkey_to_text(&PBUF(buf2), &uk);
652         }
653
654         if (k) {
655                 struct bkey uk = bkey_unpack_key(l->b, k);
656                 bch2_bkey_to_text(&PBUF(buf3), &uk);
657         }
658
659         panic("path should be %s key at level %u:\n"
660               "path pos %s\n"
661               "prev key %s\n"
662               "cur  key %s\n",
663               msg, level, buf1, buf2, buf3);
664 }
665
666 static void bch2_btree_path_verify(struct btree_trans *trans,
667                                    struct btree_path *path)
668 {
669         struct bch_fs *c = trans->c;
670         unsigned i;
671
672         EBUG_ON(path->btree_id >= BTREE_ID_NR);
673
674         for (i = 0; i < (!path->cached ? BTREE_MAX_DEPTH : 1); i++) {
675                 if (!path->l[i].b) {
676                         BUG_ON(!path->cached &&
677                                c->btree_roots[path->btree_id].b->c.level > i);
678                         break;
679                 }
680
681                 bch2_btree_path_verify_level(trans, path, i);
682         }
683
684         bch2_btree_path_verify_locks(path);
685 }
686
687 void bch2_trans_verify_paths(struct btree_trans *trans)
688 {
689         struct btree_path *path;
690
691         trans_for_each_path(trans, path)
692                 bch2_btree_path_verify(trans, path);
693 }
694
695 static void bch2_btree_iter_verify(struct btree_iter *iter)
696 {
697         struct btree_trans *trans = iter->trans;
698
699         BUG_ON(iter->btree_id >= BTREE_ID_NR);
700
701         BUG_ON(!!(iter->flags & BTREE_ITER_CACHED) != iter->path->cached);
702
703         BUG_ON(!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
704                iter->pos.snapshot != iter->snapshot);
705
706         BUG_ON((iter->flags & BTREE_ITER_IS_EXTENTS) &&
707                (iter->flags & BTREE_ITER_ALL_SNAPSHOTS));
708
709         BUG_ON(!(iter->flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
710                (iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
711                !btree_type_has_snapshots(iter->btree_id));
712
713         bch2_btree_path_verify(trans, iter->path);
714 }
715
716 static void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter)
717 {
718         BUG_ON((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
719                !iter->pos.snapshot);
720
721         BUG_ON(!(iter->flags & BTREE_ITER_ALL_SNAPSHOTS) &&
722                iter->pos.snapshot != iter->snapshot);
723
724         BUG_ON(bkey_cmp(iter->pos, bkey_start_pos(&iter->k)) < 0 ||
725                bkey_cmp(iter->pos, iter->k.p) > 0);
726 }
727
728 static int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k)
729 {
730         struct btree_trans *trans = iter->trans;
731         struct btree_iter copy;
732         struct bkey_s_c prev;
733         int ret = 0;
734
735         if (!bch2_debug_check_iterators)
736                 return 0;
737
738         if (!(iter->flags & BTREE_ITER_FILTER_SNAPSHOTS))
739                 return 0;
740
741         if (bkey_err(k) || !k.k)
742                 return 0;
743
744         BUG_ON(!bch2_snapshot_is_ancestor(trans->c,
745                                           iter->snapshot,
746                                           k.k->p.snapshot));
747
748         bch2_trans_iter_init(trans, &copy, iter->btree_id, iter->pos,
749                              BTREE_ITER_NOPRESERVE|
750                              BTREE_ITER_ALL_SNAPSHOTS);
751         prev = bch2_btree_iter_prev(&copy);
752         if (!prev.k)
753                 goto out;
754
755         ret = bkey_err(prev);
756         if (ret)
757                 goto out;
758
759         if (!bkey_cmp(prev.k->p, k.k->p) &&
760             bch2_snapshot_is_ancestor(trans->c, iter->snapshot,
761                                       prev.k->p.snapshot) > 0) {
762                 char buf1[100], buf2[200];
763
764                 bch2_bkey_to_text(&PBUF(buf1), k.k);
765                 bch2_bkey_to_text(&PBUF(buf2), prev.k);
766
767                 panic("iter snap %u\n"
768                       "k    %s\n"
769                       "prev %s\n",
770                       iter->snapshot,
771                       buf1, buf2);
772         }
773 out:
774         bch2_trans_iter_exit(trans, &copy);
775         return ret;
776 }
777
778 void bch2_assert_pos_locked(struct btree_trans *trans, enum btree_id id,
779                             struct bpos pos, bool key_cache)
780 {
781         struct btree_path *path;
782         unsigned idx;
783         char buf[100];
784
785         trans_for_each_path_inorder(trans, path, idx) {
786                 int cmp = cmp_int(path->btree_id, id) ?:
787                         cmp_int(path->cached, key_cache);
788
789                 if (cmp > 0)
790                         break;
791                 if (cmp < 0)
792                         continue;
793
794                 if (!(path->nodes_locked & 1) ||
795                     !path->should_be_locked)
796                         continue;
797
798                 if (!key_cache) {
799                         if (bkey_cmp(pos, path->l[0].b->data->min_key) >= 0 &&
800                             bkey_cmp(pos, path->l[0].b->key.k.p) <= 0)
801                                 return;
802                 } else {
803                         if (!bkey_cmp(pos, path->pos))
804                                 return;
805                 }
806         }
807
808         bch2_dump_trans_paths_updates(trans);
809         panic("not locked: %s %s%s\n",
810               bch2_btree_ids[id],
811               (bch2_bpos_to_text(&PBUF(buf), pos), buf),
812               key_cache ? " cached" : "");
813 }
814
815 #else
816
817 static inline void bch2_btree_path_verify_level(struct btree_trans *trans,
818                                                 struct btree_path *path, unsigned l) {}
819 static inline void bch2_btree_path_verify(struct btree_trans *trans,
820                                           struct btree_path *path) {}
821 static inline void bch2_btree_iter_verify(struct btree_iter *iter) {}
822 static inline void bch2_btree_iter_verify_entry_exit(struct btree_iter *iter) {}
823 static inline int bch2_btree_iter_verify_ret(struct btree_iter *iter, struct bkey_s_c k) { return 0; }
824
825 #endif
826
827 /* Btree path: fixups after btree updates */
828
829 static void btree_node_iter_set_set_pos(struct btree_node_iter *iter,
830                                         struct btree *b,
831                                         struct bset_tree *t,
832                                         struct bkey_packed *k)
833 {
834         struct btree_node_iter_set *set;
835
836         btree_node_iter_for_each(iter, set)
837                 if (set->end == t->end_offset) {
838                         set->k = __btree_node_key_to_offset(b, k);
839                         bch2_btree_node_iter_sort(iter, b);
840                         return;
841                 }
842
843         bch2_btree_node_iter_push(iter, b, k, btree_bkey_last(b, t));
844 }
845
846 static void __bch2_btree_path_fix_key_modified(struct btree_path *path,
847                                                struct btree *b,
848                                                struct bkey_packed *where)
849 {
850         struct btree_path_level *l = &path->l[b->c.level];
851
852         if (where != bch2_btree_node_iter_peek_all(&l->iter, l->b))
853                 return;
854
855         if (bkey_iter_pos_cmp(l->b, where, &path->pos) < 0)
856                 bch2_btree_node_iter_advance(&l->iter, l->b);
857 }
858
859 void bch2_btree_path_fix_key_modified(struct btree_trans *trans,
860                                       struct btree *b,
861                                       struct bkey_packed *where)
862 {
863         struct btree_path *path;
864
865         trans_for_each_path_with_node(trans, b, path) {
866                 __bch2_btree_path_fix_key_modified(path, b, where);
867                 bch2_btree_path_verify_level(trans, path, b->c.level);
868         }
869 }
870
871 static void __bch2_btree_node_iter_fix(struct btree_path *path,
872                                        struct btree *b,
873                                        struct btree_node_iter *node_iter,
874                                        struct bset_tree *t,
875                                        struct bkey_packed *where,
876                                        unsigned clobber_u64s,
877                                        unsigned new_u64s)
878 {
879         const struct bkey_packed *end = btree_bkey_last(b, t);
880         struct btree_node_iter_set *set;
881         unsigned offset = __btree_node_key_to_offset(b, where);
882         int shift = new_u64s - clobber_u64s;
883         unsigned old_end = t->end_offset - shift;
884         unsigned orig_iter_pos = node_iter->data[0].k;
885         bool iter_current_key_modified =
886                 orig_iter_pos >= offset &&
887                 orig_iter_pos <= offset + clobber_u64s;
888
889         btree_node_iter_for_each(node_iter, set)
890                 if (set->end == old_end)
891                         goto found;
892
893         /* didn't find the bset in the iterator - might have to readd it: */
894         if (new_u64s &&
895             bkey_iter_pos_cmp(b, where, &path->pos) >= 0) {
896                 bch2_btree_node_iter_push(node_iter, b, where, end);
897                 goto fixup_done;
898         } else {
899                 /* Iterator is after key that changed */
900                 return;
901         }
902 found:
903         set->end = t->end_offset;
904
905         /* Iterator hasn't gotten to the key that changed yet: */
906         if (set->k < offset)
907                 return;
908
909         if (new_u64s &&
910             bkey_iter_pos_cmp(b, where, &path->pos) >= 0) {
911                 set->k = offset;
912         } else if (set->k < offset + clobber_u64s) {
913                 set->k = offset + new_u64s;
914                 if (set->k == set->end)
915                         bch2_btree_node_iter_set_drop(node_iter, set);
916         } else {
917                 /* Iterator is after key that changed */
918                 set->k = (int) set->k + shift;
919                 return;
920         }
921
922         bch2_btree_node_iter_sort(node_iter, b);
923 fixup_done:
924         if (node_iter->data[0].k != orig_iter_pos)
925                 iter_current_key_modified = true;
926
927         /*
928          * When a new key is added, and the node iterator now points to that
929          * key, the iterator might have skipped past deleted keys that should
930          * come after the key the iterator now points to. We have to rewind to
931          * before those deleted keys - otherwise
932          * bch2_btree_node_iter_prev_all() breaks:
933          */
934         if (!bch2_btree_node_iter_end(node_iter) &&
935             iter_current_key_modified &&
936             b->c.level) {
937                 struct bset_tree *t;
938                 struct bkey_packed *k, *k2, *p;
939
940                 k = bch2_btree_node_iter_peek_all(node_iter, b);
941
942                 for_each_bset(b, t) {
943                         bool set_pos = false;
944
945                         if (node_iter->data[0].end == t->end_offset)
946                                 continue;
947
948                         k2 = bch2_btree_node_iter_bset_pos(node_iter, b, t);
949
950                         while ((p = bch2_bkey_prev_all(b, t, k2)) &&
951                                bkey_iter_cmp(b, k, p) < 0) {
952                                 k2 = p;
953                                 set_pos = true;
954                         }
955
956                         if (set_pos)
957                                 btree_node_iter_set_set_pos(node_iter,
958                                                             b, t, k2);
959                 }
960         }
961 }
962
963 void bch2_btree_node_iter_fix(struct btree_trans *trans,
964                               struct btree_path *path,
965                               struct btree *b,
966                               struct btree_node_iter *node_iter,
967                               struct bkey_packed *where,
968                               unsigned clobber_u64s,
969                               unsigned new_u64s)
970 {
971         struct bset_tree *t = bch2_bkey_to_bset(b, where);
972         struct btree_path *linked;
973
974         if (node_iter != &path->l[b->c.level].iter) {
975                 __bch2_btree_node_iter_fix(path, b, node_iter, t,
976                                            where, clobber_u64s, new_u64s);
977
978                 if (bch2_debug_check_iterators)
979                         bch2_btree_node_iter_verify(node_iter, b);
980         }
981
982         trans_for_each_path_with_node(trans, b, linked) {
983                 __bch2_btree_node_iter_fix(linked, b,
984                                            &linked->l[b->c.level].iter, t,
985                                            where, clobber_u64s, new_u64s);
986                 bch2_btree_path_verify_level(trans, linked, b->c.level);
987         }
988 }
989
990 /* Btree path level: pointer to a particular btree node and node iter */
991
992 static inline struct bkey_s_c __btree_iter_unpack(struct bch_fs *c,
993                                                   struct btree_path_level *l,
994                                                   struct bkey *u,
995                                                   struct bkey_packed *k)
996 {
997         struct bkey_s_c ret;
998
999         if (unlikely(!k)) {
1000                 /*
1001                  * signal to bch2_btree_iter_peek_slot() that we're currently at
1002                  * a hole
1003                  */
1004                 u->type = KEY_TYPE_deleted;
1005                 return bkey_s_c_null;
1006         }
1007
1008         ret = bkey_disassemble(l->b, k, u);
1009
1010         /*
1011          * XXX: bch2_btree_bset_insert_key() generates invalid keys when we
1012          * overwrite extents - it sets k->type = KEY_TYPE_deleted on the key
1013          * being overwritten but doesn't change k->size. But this is ok, because
1014          * those keys are never written out, we just have to avoid a spurious
1015          * assertion here:
1016          */
1017         if (bch2_debug_check_bkeys && !bkey_deleted(ret.k))
1018                 bch2_bkey_debugcheck(c, l->b, ret);
1019
1020         return ret;
1021 }
1022
1023 static inline struct bkey_s_c btree_path_level_peek_all(struct bch_fs *c,
1024                                                         struct btree_path_level *l,
1025                                                         struct bkey *u)
1026 {
1027         return __btree_iter_unpack(c, l, u,
1028                         bch2_btree_node_iter_peek_all(&l->iter, l->b));
1029 }
1030
1031 static inline struct bkey_s_c btree_path_level_peek(struct bch_fs *c,
1032                                                     struct btree_path *path,
1033                                                     struct btree_path_level *l,
1034                                                     struct bkey *u)
1035 {
1036         struct bkey_s_c k = __btree_iter_unpack(c, l, u,
1037                         bch2_btree_node_iter_peek(&l->iter, l->b));
1038
1039         path->pos = k.k ? k.k->p : l->b->key.k.p;
1040         return k;
1041 }
1042
1043 static inline struct bkey_s_c btree_path_level_prev(struct bch_fs *c,
1044                                                     struct btree_path *path,
1045                                                     struct btree_path_level *l,
1046                                                     struct bkey *u)
1047 {
1048         struct bkey_s_c k = __btree_iter_unpack(c, l, u,
1049                         bch2_btree_node_iter_prev(&l->iter, l->b));
1050
1051         path->pos = k.k ? k.k->p : l->b->data->min_key;
1052         return k;
1053 }
1054
1055 static inline bool btree_path_advance_to_pos(struct btree_path *path,
1056                                              struct btree_path_level *l,
1057                                              int max_advance)
1058 {
1059         struct bkey_packed *k;
1060         int nr_advanced = 0;
1061
1062         while ((k = bch2_btree_node_iter_peek_all(&l->iter, l->b)) &&
1063                bkey_iter_pos_cmp(l->b, k, &path->pos) < 0) {
1064                 if (max_advance > 0 && nr_advanced >= max_advance)
1065                         return false;
1066
1067                 bch2_btree_node_iter_advance(&l->iter, l->b);
1068                 nr_advanced++;
1069         }
1070
1071         return true;
1072 }
1073
1074 /*
1075  * Verify that iterator for parent node points to child node:
1076  */
1077 static void btree_path_verify_new_node(struct btree_trans *trans,
1078                                        struct btree_path *path, struct btree *b)
1079 {
1080         struct btree_path_level *l;
1081         unsigned plevel;
1082         bool parent_locked;
1083         struct bkey_packed *k;
1084
1085         if (!IS_ENABLED(CONFIG_BCACHEFS_DEBUG))
1086                 return;
1087
1088         plevel = b->c.level + 1;
1089         if (!btree_path_node(path, plevel))
1090                 return;
1091
1092         parent_locked = btree_node_locked(path, plevel);
1093
1094         if (!bch2_btree_node_relock(trans, path, plevel))
1095                 return;
1096
1097         l = &path->l[plevel];
1098         k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1099         if (!k ||
1100             bkey_deleted(k) ||
1101             bkey_cmp_left_packed(l->b, k, &b->key.k.p)) {
1102                 char buf1[100];
1103                 char buf2[100];
1104                 char buf3[100];
1105                 char buf4[100];
1106                 struct bkey uk = bkey_unpack_key(b, k);
1107
1108                 bch2_dump_btree_node(trans->c, l->b);
1109                 bch2_bpos_to_text(&PBUF(buf1), path->pos);
1110                 bch2_bkey_to_text(&PBUF(buf2), &uk);
1111                 bch2_bpos_to_text(&PBUF(buf3), b->data->min_key);
1112                 bch2_bpos_to_text(&PBUF(buf3), b->data->max_key);
1113                 panic("parent iter doesn't point to new node:\n"
1114                       "iter pos %s %s\n"
1115                       "iter key %s\n"
1116                       "new node %s-%s\n",
1117                       bch2_btree_ids[path->btree_id], buf1,
1118                       buf2, buf3, buf4);
1119         }
1120
1121         if (!parent_locked)
1122                 btree_node_unlock(path, plevel);
1123 }
1124
1125 static inline void __btree_path_level_init(struct btree_path *path,
1126                                            unsigned level)
1127 {
1128         struct btree_path_level *l = &path->l[level];
1129
1130         bch2_btree_node_iter_init(&l->iter, l->b, &path->pos);
1131
1132         /*
1133          * Iterators to interior nodes should always be pointed at the first non
1134          * whiteout:
1135          */
1136         if (level)
1137                 bch2_btree_node_iter_peek(&l->iter, l->b);
1138 }
1139
1140 static inline void btree_path_level_init(struct btree_trans *trans,
1141                                          struct btree_path *path,
1142                                          struct btree *b)
1143 {
1144         BUG_ON(path->cached);
1145
1146         btree_path_verify_new_node(trans, path, b);
1147
1148         EBUG_ON(!btree_path_pos_in_node(path, b));
1149         EBUG_ON(b->c.lock.state.seq & 1);
1150
1151         path->l[b->c.level].lock_seq = b->c.lock.state.seq;
1152         path->l[b->c.level].b = b;
1153         __btree_path_level_init(path, b->c.level);
1154 }
1155
1156 /* Btree path: fixups after btree node updates: */
1157
1158 /*
1159  * A btree node is being replaced - update the iterator to point to the new
1160  * node:
1161  */
1162 void bch2_trans_node_add(struct btree_trans *trans, struct btree *b)
1163 {
1164         struct btree_path *path;
1165
1166         trans_for_each_path(trans, path)
1167                 if (!path->cached &&
1168                     btree_path_pos_in_node(path, b)) {
1169                         enum btree_node_locked_type t =
1170                                 btree_lock_want(path, b->c.level);
1171
1172                         if (path->nodes_locked &&
1173                             t != BTREE_NODE_UNLOCKED) {
1174                                 btree_node_unlock(path, b->c.level);
1175                                 six_lock_increment(&b->c.lock, t);
1176                                 mark_btree_node_locked(path, b->c.level, t);
1177                         }
1178
1179                         btree_path_level_init(trans, path, b);
1180                 }
1181 }
1182
1183 /*
1184  * A btree node has been modified in such a way as to invalidate iterators - fix
1185  * them:
1186  */
1187 void bch2_trans_node_reinit_iter(struct btree_trans *trans, struct btree *b)
1188 {
1189         struct btree_path *path;
1190
1191         trans_for_each_path_with_node(trans, b, path)
1192                 __btree_path_level_init(path, b->c.level);
1193 }
1194
1195 /* Btree path: traverse, set_pos: */
1196
1197 static int lock_root_check_fn(struct six_lock *lock, void *p)
1198 {
1199         struct btree *b = container_of(lock, struct btree, c.lock);
1200         struct btree **rootp = p;
1201
1202         return b == *rootp ? 0 : -1;
1203 }
1204
1205 static inline int btree_path_lock_root(struct btree_trans *trans,
1206                                        struct btree_path *path,
1207                                        unsigned depth_want,
1208                                        unsigned long trace_ip)
1209 {
1210         struct bch_fs *c = trans->c;
1211         struct btree *b, **rootp = &c->btree_roots[path->btree_id].b;
1212         enum six_lock_type lock_type;
1213         unsigned i;
1214
1215         EBUG_ON(path->nodes_locked);
1216
1217         while (1) {
1218                 b = READ_ONCE(*rootp);
1219                 path->level = READ_ONCE(b->c.level);
1220
1221                 if (unlikely(path->level < depth_want)) {
1222                         /*
1223                          * the root is at a lower depth than the depth we want:
1224                          * got to the end of the btree, or we're walking nodes
1225                          * greater than some depth and there are no nodes >=
1226                          * that depth
1227                          */
1228                         path->level = depth_want;
1229                         for (i = path->level; i < BTREE_MAX_DEPTH; i++)
1230                                 path->l[i].b = NULL;
1231                         return 1;
1232                 }
1233
1234                 lock_type = __btree_lock_want(path, path->level);
1235                 if (unlikely(!btree_node_lock(trans, path, b, SPOS_MAX,
1236                                               path->level, lock_type,
1237                                               lock_root_check_fn, rootp,
1238                                               trace_ip))) {
1239                         if (trans->restarted)
1240                                 return -EINTR;
1241                         continue;
1242                 }
1243
1244                 if (likely(b == READ_ONCE(*rootp) &&
1245                            b->c.level == path->level &&
1246                            !race_fault())) {
1247                         for (i = 0; i < path->level; i++)
1248                                 path->l[i].b = BTREE_ITER_NO_NODE_LOCK_ROOT;
1249                         path->l[path->level].b = b;
1250                         for (i = path->level + 1; i < BTREE_MAX_DEPTH; i++)
1251                                 path->l[i].b = NULL;
1252
1253                         mark_btree_node_locked(path, path->level, lock_type);
1254                         btree_path_level_init(trans, path, b);
1255                         return 0;
1256                 }
1257
1258                 six_unlock_type(&b->c.lock, lock_type);
1259         }
1260 }
1261
1262 noinline
1263 static int btree_path_prefetch(struct btree_trans *trans, struct btree_path *path)
1264 {
1265         struct bch_fs *c = trans->c;
1266         struct btree_path_level *l = path_l(path);
1267         struct btree_node_iter node_iter = l->iter;
1268         struct bkey_packed *k;
1269         struct bkey_buf tmp;
1270         unsigned nr = test_bit(BCH_FS_STARTED, &c->flags)
1271                 ? (path->level > 1 ? 0 :  2)
1272                 : (path->level > 1 ? 1 : 16);
1273         bool was_locked = btree_node_locked(path, path->level);
1274         int ret = 0;
1275
1276         bch2_bkey_buf_init(&tmp);
1277
1278         while (nr && !ret) {
1279                 if (!bch2_btree_node_relock(trans, path, path->level))
1280                         break;
1281
1282                 bch2_btree_node_iter_advance(&node_iter, l->b);
1283                 k = bch2_btree_node_iter_peek(&node_iter, l->b);
1284                 if (!k)
1285                         break;
1286
1287                 bch2_bkey_buf_unpack(&tmp, c, l->b, k);
1288                 ret = bch2_btree_node_prefetch(c, trans, path, tmp.k, path->btree_id,
1289                                                path->level - 1);
1290         }
1291
1292         if (!was_locked)
1293                 btree_node_unlock(path, path->level);
1294
1295         bch2_bkey_buf_exit(&tmp, c);
1296         return ret;
1297 }
1298
1299 static noinline void btree_node_mem_ptr_set(struct btree_trans *trans,
1300                                             struct btree_path *path,
1301                                             unsigned plevel, struct btree *b)
1302 {
1303         struct btree_path_level *l = &path->l[plevel];
1304         bool locked = btree_node_locked(path, plevel);
1305         struct bkey_packed *k;
1306         struct bch_btree_ptr_v2 *bp;
1307
1308         if (!bch2_btree_node_relock(trans, path, plevel))
1309                 return;
1310
1311         k = bch2_btree_node_iter_peek_all(&l->iter, l->b);
1312         BUG_ON(k->type != KEY_TYPE_btree_ptr_v2);
1313
1314         bp = (void *) bkeyp_val(&l->b->format, k);
1315         bp->mem_ptr = (unsigned long)b;
1316
1317         if (!locked)
1318                 btree_node_unlock(path, plevel);
1319 }
1320
1321 static __always_inline int btree_path_down(struct btree_trans *trans,
1322                                            struct btree_path *path,
1323                                            unsigned flags,
1324                                            unsigned long trace_ip)
1325 {
1326         struct bch_fs *c = trans->c;
1327         struct btree_path_level *l = path_l(path);
1328         struct btree *b;
1329         unsigned level = path->level - 1;
1330         enum six_lock_type lock_type = __btree_lock_want(path, level);
1331         struct bkey_buf tmp;
1332         int ret;
1333
1334         EBUG_ON(!btree_node_locked(path, path->level));
1335
1336         bch2_bkey_buf_init(&tmp);
1337         bch2_bkey_buf_unpack(&tmp, c, l->b,
1338                          bch2_btree_node_iter_peek(&l->iter, l->b));
1339
1340         b = bch2_btree_node_get(trans, path, tmp.k, level, lock_type, trace_ip);
1341         ret = PTR_ERR_OR_ZERO(b);
1342         if (unlikely(ret))
1343                 goto err;
1344
1345         mark_btree_node_locked(path, level, lock_type);
1346         btree_path_level_init(trans, path, b);
1347
1348         if (tmp.k->k.type == KEY_TYPE_btree_ptr_v2 &&
1349             unlikely(b != btree_node_mem_ptr(tmp.k)))
1350                 btree_node_mem_ptr_set(trans, path, level + 1, b);
1351
1352         if (flags & BTREE_ITER_PREFETCH)
1353                 ret = btree_path_prefetch(trans, path);
1354
1355         if (btree_node_read_locked(path, level + 1))
1356                 btree_node_unlock(path, level + 1);
1357         path->level = level;
1358
1359         bch2_btree_path_verify_locks(path);
1360 err:
1361         bch2_bkey_buf_exit(&tmp, c);
1362         return ret;
1363 }
1364
1365 static int btree_path_traverse_one(struct btree_trans *, struct btree_path *,
1366                                    unsigned, unsigned long);
1367
1368 static int __btree_path_traverse_all(struct btree_trans *trans, int ret,
1369                                      unsigned long trace_ip)
1370 {
1371         struct bch_fs *c = trans->c;
1372         struct btree_path *path;
1373         int i;
1374
1375         if (trans->in_traverse_all)
1376                 return -EINTR;
1377
1378         trans->in_traverse_all = true;
1379 retry_all:
1380         trans->restarted = false;
1381
1382         trans_for_each_path(trans, path)
1383                 path->should_be_locked = false;
1384
1385         btree_trans_verify_sorted(trans);
1386
1387         for (i = trans->nr_sorted - 2; i >= 0; --i) {
1388                 struct btree_path *path1 = trans->paths + trans->sorted[i];
1389                 struct btree_path *path2 = trans->paths + trans->sorted[i + 1];
1390
1391                 if (path1->btree_id == path2->btree_id &&
1392                     path1->locks_want < path2->locks_want)
1393                         __bch2_btree_path_upgrade(trans, path1, path2->locks_want);
1394                 else if (!path1->locks_want && path2->locks_want)
1395                         __bch2_btree_path_upgrade(trans, path1, 1);
1396         }
1397
1398         bch2_trans_unlock(trans);
1399         cond_resched();
1400
1401         if (unlikely(ret == -ENOMEM)) {
1402                 struct closure cl;
1403
1404                 closure_init_stack(&cl);
1405
1406                 do {
1407                         ret = bch2_btree_cache_cannibalize_lock(c, &cl);
1408                         closure_sync(&cl);
1409                 } while (ret);
1410         }
1411
1412         if (unlikely(ret == -EIO))
1413                 goto out;
1414
1415         BUG_ON(ret && ret != -EINTR);
1416
1417         /* Now, redo traversals in correct order: */
1418         i = 0;
1419         while (i < trans->nr_sorted) {
1420                 path = trans->paths + trans->sorted[i];
1421
1422                 EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
1423
1424                 ret = btree_path_traverse_one(trans, path, 0, _THIS_IP_);
1425                 if (ret)
1426                         goto retry_all;
1427
1428                 EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
1429
1430                 if (path->nodes_locked ||
1431                     !btree_path_node(path, path->level))
1432                         i++;
1433         }
1434
1435         /*
1436          * BTREE_ITER_NEED_RELOCK is ok here - if we called bch2_trans_unlock()
1437          * and relock(), relock() won't relock since path->should_be_locked
1438          * isn't set yet, which is all fine
1439          */
1440         trans_for_each_path(trans, path)
1441                 BUG_ON(path->uptodate >= BTREE_ITER_NEED_TRAVERSE);
1442 out:
1443         bch2_btree_cache_cannibalize_unlock(c);
1444
1445         trans->in_traverse_all = false;
1446
1447         trace_trans_traverse_all(trans->ip, trace_ip);
1448         return ret;
1449 }
1450
1451 static int bch2_btree_path_traverse_all(struct btree_trans *trans)
1452 {
1453         return __btree_path_traverse_all(trans, 0, _RET_IP_);
1454 }
1455
1456 static inline bool btree_path_good_node(struct btree_trans *trans,
1457                                         struct btree_path *path,
1458                                         unsigned l, int check_pos)
1459 {
1460         if (!is_btree_node(path, l) ||
1461             !bch2_btree_node_relock(trans, path, l))
1462                 return false;
1463
1464         if (check_pos < 0 && btree_path_pos_before_node(path, path->l[l].b))
1465                 return false;
1466         if (check_pos > 0 && btree_path_pos_after_node(path, path->l[l].b))
1467                 return false;
1468         return true;
1469 }
1470
1471 static inline unsigned btree_path_up_until_good_node(struct btree_trans *trans,
1472                                                      struct btree_path *path,
1473                                                      int check_pos)
1474 {
1475         unsigned i, l = path->level;
1476
1477         while (btree_path_node(path, l) &&
1478                !btree_path_good_node(trans, path, l, check_pos)) {
1479                 btree_node_unlock(path, l);
1480                 path->l[l].b = BTREE_ITER_NO_NODE_UP;
1481                 l++;
1482         }
1483
1484         /* If we need intent locks, take them too: */
1485         for (i = l + 1;
1486              i < path->locks_want && btree_path_node(path, i);
1487              i++)
1488                 if (!bch2_btree_node_relock(trans, path, i))
1489                         while (l <= i) {
1490                                 btree_node_unlock(path, l);
1491                                 path->l[l].b = BTREE_ITER_NO_NODE_UP;
1492                                 l++;
1493                         }
1494
1495         return l;
1496 }
1497
1498 /*
1499  * This is the main state machine for walking down the btree - walks down to a
1500  * specified depth
1501  *
1502  * Returns 0 on success, -EIO on error (error reading in a btree node).
1503  *
1504  * On error, caller (peek_node()/peek_key()) must return NULL; the error is
1505  * stashed in the iterator and returned from bch2_trans_exit().
1506  */
1507 static int btree_path_traverse_one(struct btree_trans *trans,
1508                                    struct btree_path *path,
1509                                    unsigned flags,
1510                                    unsigned long trace_ip)
1511 {
1512         unsigned depth_want = path->level;
1513         int ret = 0;
1514
1515         if (unlikely(trans->restarted)) {
1516                 ret = -EINTR;
1517                 goto out;
1518         }
1519
1520         /*
1521          * Ensure we obey path->should_be_locked: if it's set, we can't unlock
1522          * and re-traverse the path without a transaction restart:
1523          */
1524         if (path->should_be_locked) {
1525                 ret = bch2_btree_path_relock(trans, path, trace_ip) ? 0 : -EINTR;
1526                 goto out;
1527         }
1528
1529         if (path->cached) {
1530                 ret = bch2_btree_path_traverse_cached(trans, path, flags);
1531                 goto out;
1532         }
1533
1534         if (unlikely(path->level >= BTREE_MAX_DEPTH))
1535                 goto out;
1536
1537         path->level = btree_path_up_until_good_node(trans, path, 0);
1538
1539         /*
1540          * Note: path->nodes[path->level] may be temporarily NULL here - that
1541          * would indicate to other code that we got to the end of the btree,
1542          * here it indicates that relocking the root failed - it's critical that
1543          * btree_path_lock_root() comes next and that it can't fail
1544          */
1545         while (path->level > depth_want) {
1546                 ret = btree_path_node(path, path->level)
1547                         ? btree_path_down(trans, path, flags, trace_ip)
1548                         : btree_path_lock_root(trans, path, depth_want, trace_ip);
1549                 if (unlikely(ret)) {
1550                         if (ret == 1) {
1551                                 /*
1552                                  * No nodes at this level - got to the end of
1553                                  * the btree:
1554                                  */
1555                                 ret = 0;
1556                                 goto out;
1557                         }
1558
1559                         __bch2_btree_path_unlock(path);
1560                         path->level = depth_want;
1561
1562                         if (ret == -EIO)
1563                                 path->l[path->level].b =
1564                                         BTREE_ITER_NO_NODE_ERROR;
1565                         else
1566                                 path->l[path->level].b =
1567                                         BTREE_ITER_NO_NODE_DOWN;
1568                         goto out;
1569                 }
1570         }
1571
1572         path->uptodate = BTREE_ITER_UPTODATE;
1573 out:
1574         BUG_ON((ret == -EINTR) != !!trans->restarted);
1575         bch2_btree_path_verify(trans, path);
1576         return ret;
1577 }
1578
1579 static int __btree_path_traverse_all(struct btree_trans *, int, unsigned long);
1580
1581 int __must_check bch2_btree_path_traverse(struct btree_trans *trans,
1582                                           struct btree_path *path, unsigned flags)
1583 {
1584         if (path->uptodate < BTREE_ITER_NEED_RELOCK)
1585                 return 0;
1586
1587         return  bch2_trans_cond_resched(trans) ?:
1588                 btree_path_traverse_one(trans, path, flags, _RET_IP_);
1589 }
1590
1591 static void btree_path_copy(struct btree_trans *trans, struct btree_path *dst,
1592                             struct btree_path *src)
1593 {
1594         unsigned i;
1595
1596         memcpy(&dst->pos, &src->pos,
1597                sizeof(struct btree_path) - offsetof(struct btree_path, pos));
1598
1599         for (i = 0; i < BTREE_MAX_DEPTH; i++)
1600                 if (btree_node_locked(dst, i))
1601                         six_lock_increment(&dst->l[i].b->c.lock,
1602                                            __btree_lock_want(dst, i));
1603
1604         btree_path_check_sort(trans, dst, 0);
1605 }
1606
1607 static struct btree_path *btree_path_clone(struct btree_trans *trans, struct btree_path *src,
1608                                            bool intent)
1609 {
1610         struct btree_path *new = btree_path_alloc(trans, src);
1611
1612         btree_path_copy(trans, new, src);
1613         __btree_path_get(new, intent);
1614         return new;
1615 }
1616
1617 inline struct btree_path * __must_check
1618 bch2_btree_path_make_mut(struct btree_trans *trans,
1619                          struct btree_path *path, bool intent,
1620                          unsigned long ip)
1621 {
1622         if (path->ref > 1 || path->preserve) {
1623                 __btree_path_put(path, intent);
1624                 path = btree_path_clone(trans, path, intent);
1625                 path->preserve = false;
1626 #ifdef CONFIG_BCACHEFS_DEBUG
1627                 path->ip_allocated = ip;
1628 #endif
1629                 btree_trans_verify_sorted(trans);
1630         }
1631
1632         return path;
1633 }
1634
1635 static struct btree_path * __must_check
1636 btree_path_set_pos(struct btree_trans *trans,
1637                    struct btree_path *path, struct bpos new_pos,
1638                    bool intent, unsigned long ip)
1639 {
1640         int cmp = bpos_cmp(new_pos, path->pos);
1641         unsigned l = path->level;
1642
1643         EBUG_ON(trans->restarted);
1644         EBUG_ON(!path->ref);
1645
1646         if (!cmp)
1647                 return path;
1648
1649         path = bch2_btree_path_make_mut(trans, path, intent, ip);
1650
1651         path->pos               = new_pos;
1652         path->should_be_locked  = false;
1653
1654         btree_path_check_sort(trans, path, cmp);
1655
1656         if (unlikely(path->cached)) {
1657                 btree_node_unlock(path, 0);
1658                 path->l[0].b = BTREE_ITER_NO_NODE_CACHED;
1659                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1660                 goto out;
1661         }
1662
1663         l = btree_path_up_until_good_node(trans, path, cmp);
1664
1665         if (btree_path_node(path, l)) {
1666                 /*
1667                  * We might have to skip over many keys, or just a few: try
1668                  * advancing the node iterator, and if we have to skip over too
1669                  * many keys just reinit it (or if we're rewinding, since that
1670                  * is expensive).
1671                  */
1672                 if (cmp < 0 ||
1673                     !btree_path_advance_to_pos(path, &path->l[l], 8))
1674                         __btree_path_level_init(path, l);
1675         }
1676
1677         if (l != path->level) {
1678                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
1679                 __bch2_btree_path_unlock(path);
1680         }
1681 out:
1682         bch2_btree_path_verify(trans, path);
1683         return path;
1684 }
1685
1686 /* Btree path: main interface: */
1687
1688 static struct btree_path *have_path_at_pos(struct btree_trans *trans, struct btree_path *path)
1689 {
1690         struct btree_path *next;
1691
1692         next = prev_btree_path(trans, path);
1693         if (next && !btree_path_cmp(next, path))
1694                 return next;
1695
1696         next = next_btree_path(trans, path);
1697         if (next && !btree_path_cmp(next, path))
1698                 return next;
1699
1700         return NULL;
1701 }
1702
1703 static struct btree_path *have_node_at_pos(struct btree_trans *trans, struct btree_path *path)
1704 {
1705         struct btree_path *next;
1706
1707         next = prev_btree_path(trans, path);
1708         if (next && next->level == path->level && path_l(next)->b == path_l(path)->b)
1709                 return next;
1710
1711         next = next_btree_path(trans, path);
1712         if (next && next->level == path->level && path_l(next)->b == path_l(path)->b)
1713                 return next;
1714
1715         return NULL;
1716 }
1717
1718 static inline void __bch2_path_free(struct btree_trans *trans, struct btree_path *path)
1719 {
1720         __bch2_btree_path_unlock(path);
1721         btree_path_list_remove(trans, path);
1722         trans->paths_allocated &= ~(1ULL << path->idx);
1723 }
1724
1725 void bch2_path_put(struct btree_trans *trans, struct btree_path *path, bool intent)
1726 {
1727         struct btree_path *dup;
1728
1729         EBUG_ON(trans->paths + path->idx != path);
1730         EBUG_ON(!path->ref);
1731
1732         if (!__btree_path_put(path, intent))
1733                 return;
1734
1735         /*
1736          * Perhaps instead we should check for duplicate paths in traverse_all:
1737          */
1738         if (path->preserve &&
1739             (dup = have_path_at_pos(trans, path))) {
1740                 dup->preserve = true;
1741                 path->preserve = false;
1742                 goto free;
1743         }
1744
1745         if (!path->preserve &&
1746             (dup = have_node_at_pos(trans, path)))
1747                 goto free;
1748         return;
1749 free:
1750         if (path->should_be_locked &&
1751             !btree_node_locked(dup, path->level))
1752                 return;
1753
1754         dup->should_be_locked |= path->should_be_locked;
1755         __bch2_path_free(trans, path);
1756 }
1757
1758 noinline __cold
1759 void bch2_dump_trans_paths_updates(struct btree_trans *trans)
1760 {
1761         struct btree_path *path;
1762         struct btree_insert_entry *i;
1763         unsigned idx;
1764         char buf1[300], buf2[300];
1765
1766         btree_trans_verify_sorted(trans);
1767
1768         trans_for_each_path_inorder(trans, path, idx)
1769                 printk(KERN_ERR "path: idx %u ref %u:%u%s%s btree %s pos %s locks %u %pS\n",
1770                        path->idx, path->ref, path->intent_ref,
1771                        path->should_be_locked ? " S" : "",
1772                        path->preserve ? " P" : "",
1773                        bch2_btree_ids[path->btree_id],
1774                        (bch2_bpos_to_text(&PBUF(buf1), path->pos), buf1),
1775                        path->nodes_locked,
1776 #ifdef CONFIG_BCACHEFS_DEBUG
1777                        (void *) path->ip_allocated
1778 #else
1779                        NULL
1780 #endif
1781                        );
1782
1783         trans_for_each_update(trans, i) {
1784                 struct bkey u;
1785                 struct bkey_s_c old = bch2_btree_path_peek_slot(i->path, &u);
1786
1787                 printk(KERN_ERR "update: btree %s %pS\n  old %s\n  new %s",
1788                        bch2_btree_ids[i->btree_id],
1789                        (void *) i->ip_allocated,
1790                        (bch2_bkey_val_to_text(&PBUF(buf1), trans->c, old), buf1),
1791                        (bch2_bkey_val_to_text(&PBUF(buf2), trans->c, bkey_i_to_s_c(i->k)), buf2));
1792         }
1793 }
1794
1795 static struct btree_path *btree_path_alloc(struct btree_trans *trans,
1796                                            struct btree_path *pos)
1797 {
1798         struct btree_path *path;
1799         unsigned idx;
1800
1801         if (unlikely(trans->paths_allocated ==
1802                      ~((~0ULL << 1) << (BTREE_ITER_MAX - 1)))) {
1803                 bch2_dump_trans_paths_updates(trans);
1804                 panic("trans path oveflow\n");
1805         }
1806
1807         idx = __ffs64(~trans->paths_allocated);
1808         trans->paths_allocated |= 1ULL << idx;
1809
1810         path = &trans->paths[idx];
1811
1812         path->idx               = idx;
1813         path->ref               = 0;
1814         path->intent_ref        = 0;
1815         path->nodes_locked      = 0;
1816         path->nodes_intent_locked = 0;
1817
1818         btree_path_list_add(trans, pos, path);
1819         return path;
1820 }
1821
1822 struct btree_path *bch2_path_get(struct btree_trans *trans,
1823                                  enum btree_id btree_id, struct bpos pos,
1824                                  unsigned locks_want, unsigned level,
1825                                  unsigned flags, unsigned long ip)
1826 {
1827         struct btree_path *path, *path_pos = NULL;
1828         bool cached = flags & BTREE_ITER_CACHED;
1829         bool intent = flags & BTREE_ITER_INTENT;
1830         int i;
1831
1832         BUG_ON(trans->restarted);
1833
1834         trans_for_each_path_inorder(trans, path, i) {
1835                 if (__btree_path_cmp(path,
1836                                      btree_id,
1837                                      cached,
1838                                      pos,
1839                                      level) > 0)
1840                         break;
1841
1842                 path_pos = path;
1843         }
1844
1845         if (path_pos &&
1846             path_pos->cached    == cached &&
1847             path_pos->btree_id  == btree_id &&
1848             path_pos->level     == level) {
1849                 __btree_path_get(path_pos, intent);
1850                 path = btree_path_set_pos(trans, path_pos, pos, intent, ip);
1851         } else {
1852                 path = btree_path_alloc(trans, path_pos);
1853                 path_pos = NULL;
1854
1855                 __btree_path_get(path, intent);
1856                 path->pos                       = pos;
1857                 path->btree_id                  = btree_id;
1858                 path->cached                    = cached;
1859                 path->uptodate                  = BTREE_ITER_NEED_TRAVERSE;
1860                 path->should_be_locked          = false;
1861                 path->level                     = level;
1862                 path->locks_want                = locks_want;
1863                 path->nodes_locked              = 0;
1864                 path->nodes_intent_locked       = 0;
1865                 for (i = 0; i < ARRAY_SIZE(path->l); i++)
1866                         path->l[i].b            = BTREE_ITER_NO_NODE_INIT;
1867 #ifdef CONFIG_BCACHEFS_DEBUG
1868                 path->ip_allocated              = ip;
1869 #endif
1870                 btree_trans_verify_sorted(trans);
1871         }
1872
1873         if (!(flags & BTREE_ITER_NOPRESERVE))
1874                 path->preserve = true;
1875
1876         if (path->intent_ref)
1877                 locks_want = max(locks_want, level + 1);
1878
1879         /*
1880          * If the path has locks_want greater than requested, we don't downgrade
1881          * it here - on transaction restart because btree node split needs to
1882          * upgrade locks, we might be putting/getting the iterator again.
1883          * Downgrading iterators only happens via bch2_trans_downgrade(), after
1884          * a successful transaction commit.
1885          */
1886
1887         locks_want = min(locks_want, BTREE_MAX_DEPTH);
1888         if (locks_want > path->locks_want) {
1889                 path->locks_want = locks_want;
1890                 btree_path_get_locks(trans, path, true, _THIS_IP_);
1891         }
1892
1893         return path;
1894 }
1895
1896 inline struct bkey_s_c bch2_btree_path_peek_slot(struct btree_path *path, struct bkey *u)
1897 {
1898
1899         struct bkey_s_c k;
1900
1901         BUG_ON(path->uptodate != BTREE_ITER_UPTODATE);
1902
1903         if (!path->cached) {
1904                 struct btree_path_level *l = path_l(path);
1905                 struct bkey_packed *_k =
1906                         bch2_btree_node_iter_peek_all(&l->iter, l->b);
1907
1908                 k = _k ? bkey_disassemble(l->b, _k, u) : bkey_s_c_null;
1909
1910                 EBUG_ON(k.k && bkey_deleted(k.k) && bpos_cmp(k.k->p, path->pos) == 0);
1911
1912                 if (!k.k || bpos_cmp(path->pos, k.k->p))
1913                         goto hole;
1914         } else {
1915                 struct bkey_cached *ck = (void *) path->l[0].b;
1916
1917                 EBUG_ON(path->btree_id != ck->key.btree_id ||
1918                         bkey_cmp(path->pos, ck->key.pos));
1919
1920                 /* BTREE_ITER_CACHED_NOFILL? */
1921                 if (unlikely(!ck->valid))
1922                         goto hole;
1923
1924                 k = bkey_i_to_s_c(ck->k);
1925         }
1926
1927         return k;
1928 hole:
1929         bkey_init(u);
1930         u->p = path->pos;
1931         return (struct bkey_s_c) { u, NULL };
1932 }
1933
1934 /* Btree iterators: */
1935
1936 int __must_check
1937 __bch2_btree_iter_traverse(struct btree_iter *iter)
1938 {
1939         return bch2_btree_path_traverse(iter->trans, iter->path, iter->flags);
1940 }
1941
1942 int __must_check
1943 bch2_btree_iter_traverse(struct btree_iter *iter)
1944 {
1945         int ret;
1946
1947         iter->path = btree_path_set_pos(iter->trans, iter->path,
1948                                         btree_iter_search_key(iter),
1949                                         iter->flags & BTREE_ITER_INTENT,
1950                                         btree_iter_ip_allocated(iter));
1951
1952         ret = bch2_btree_path_traverse(iter->trans, iter->path, iter->flags);
1953         if (ret)
1954                 return ret;
1955
1956         iter->path->should_be_locked = true;
1957         return 0;
1958 }
1959
1960 /* Iterate across nodes (leaf and interior nodes) */
1961
1962 struct btree *bch2_btree_iter_peek_node(struct btree_iter *iter)
1963 {
1964         struct btree_trans *trans = iter->trans;
1965         struct btree *b = NULL;
1966         int ret;
1967
1968         EBUG_ON(iter->path->cached);
1969         bch2_btree_iter_verify(iter);
1970
1971         ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
1972         if (ret)
1973                 goto err;
1974
1975         b = btree_path_node(iter->path, iter->path->level);
1976         if (!b)
1977                 goto out;
1978
1979         BUG_ON(bpos_cmp(b->key.k.p, iter->pos) < 0);
1980
1981         bkey_init(&iter->k);
1982         iter->k.p = iter->pos = b->key.k.p;
1983
1984         iter->path = btree_path_set_pos(trans, iter->path, b->key.k.p,
1985                                         iter->flags & BTREE_ITER_INTENT,
1986                                         btree_iter_ip_allocated(iter));
1987         iter->path->should_be_locked = true;
1988         BUG_ON(iter->path->uptodate);
1989 out:
1990         bch2_btree_iter_verify_entry_exit(iter);
1991         bch2_btree_iter_verify(iter);
1992
1993         return b;
1994 err:
1995         b = ERR_PTR(ret);
1996         goto out;
1997 }
1998
1999 struct btree *bch2_btree_iter_next_node(struct btree_iter *iter)
2000 {
2001         struct btree_trans *trans = iter->trans;
2002         struct btree_path *path = iter->path;
2003         struct btree *b = NULL;
2004         unsigned l;
2005         int ret;
2006
2007         BUG_ON(trans->restarted);
2008         EBUG_ON(iter->path->cached);
2009         bch2_btree_iter_verify(iter);
2010
2011         /* already at end? */
2012         if (!btree_path_node(path, path->level))
2013                 return NULL;
2014
2015         /* got to end? */
2016         if (!btree_path_node(path, path->level + 1)) {
2017                 btree_node_unlock(path, path->level);
2018                 path->l[path->level].b = BTREE_ITER_NO_NODE_UP;
2019                 path->level++;
2020                 return NULL;
2021         }
2022
2023         if (!bch2_btree_node_relock(trans, path, path->level + 1)) {
2024                 __bch2_btree_path_unlock(path);
2025                 path->l[path->level].b = BTREE_ITER_NO_NODE_GET_LOCKS;
2026                 path->l[path->level + 1].b = BTREE_ITER_NO_NODE_GET_LOCKS;
2027                 btree_trans_restart(trans);
2028                 ret = -EINTR;
2029                 goto err;
2030         }
2031
2032         b = btree_path_node(path, path->level + 1);
2033
2034         if (!bpos_cmp(iter->pos, b->key.k.p)) {
2035                 btree_node_unlock(path, path->level);
2036                 path->l[path->level].b = BTREE_ITER_NO_NODE_UP;
2037                 path->level++;
2038         } else {
2039                 /*
2040                  * Haven't gotten to the end of the parent node: go back down to
2041                  * the next child node
2042                  */
2043                 path = iter->path =
2044                         btree_path_set_pos(trans, path, bpos_successor(iter->pos),
2045                                            iter->flags & BTREE_ITER_INTENT,
2046                                            btree_iter_ip_allocated(iter));
2047
2048                 path->level = iter->min_depth;
2049
2050                 for (l = path->level + 1; l < BTREE_MAX_DEPTH; l++)
2051                         if (btree_lock_want(path, l) == BTREE_NODE_UNLOCKED)
2052                                 btree_node_unlock(path, l);
2053
2054                 btree_path_set_dirty(path, BTREE_ITER_NEED_TRAVERSE);
2055                 bch2_btree_iter_verify(iter);
2056
2057                 ret = bch2_btree_path_traverse(trans, path, iter->flags);
2058                 if (ret)
2059                         goto err;
2060
2061                 b = path->l[path->level].b;
2062         }
2063
2064         bkey_init(&iter->k);
2065         iter->k.p = iter->pos = b->key.k.p;
2066
2067         iter->path = btree_path_set_pos(trans, iter->path, b->key.k.p,
2068                                         iter->flags & BTREE_ITER_INTENT,
2069                                         btree_iter_ip_allocated(iter));
2070         iter->path->should_be_locked = true;
2071         BUG_ON(iter->path->uptodate);
2072 out:
2073         bch2_btree_iter_verify_entry_exit(iter);
2074         bch2_btree_iter_verify(iter);
2075
2076         return b;
2077 err:
2078         b = ERR_PTR(ret);
2079         goto out;
2080 }
2081
2082 /* Iterate across keys (in leaf nodes only) */
2083
2084 inline bool bch2_btree_iter_advance(struct btree_iter *iter)
2085 {
2086         struct bpos pos = iter->k.p;
2087         bool ret = (iter->flags & BTREE_ITER_ALL_SNAPSHOTS
2088                     ? bpos_cmp(pos, SPOS_MAX)
2089                     : bkey_cmp(pos, SPOS_MAX)) != 0;
2090
2091         if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS))
2092                 pos = bkey_successor(iter, pos);
2093         bch2_btree_iter_set_pos(iter, pos);
2094         return ret;
2095 }
2096
2097 inline bool bch2_btree_iter_rewind(struct btree_iter *iter)
2098 {
2099         struct bpos pos = bkey_start_pos(&iter->k);
2100         bool ret = (iter->flags & BTREE_ITER_ALL_SNAPSHOTS
2101                     ? bpos_cmp(pos, POS_MIN)
2102                     : bkey_cmp(pos, POS_MIN)) != 0;
2103
2104         if (ret && !(iter->flags & BTREE_ITER_IS_EXTENTS))
2105                 pos = bkey_predecessor(iter, pos);
2106         bch2_btree_iter_set_pos(iter, pos);
2107         return ret;
2108 }
2109
2110 /**
2111  * bch2_btree_iter_peek: returns first key greater than or equal to iterator's
2112  * current position
2113  */
2114 struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *iter)
2115 {
2116         struct btree_trans *trans = iter->trans;
2117         struct bpos search_key = btree_iter_search_key(iter);
2118         struct bkey_i *next_update;
2119         struct bkey_s_c k;
2120         int ret, cmp;
2121
2122         EBUG_ON(iter->path->cached || iter->path->level);
2123         bch2_btree_iter_verify(iter);
2124         bch2_btree_iter_verify_entry_exit(iter);
2125
2126         while (1) {
2127                 iter->path = btree_path_set_pos(trans, iter->path, search_key,
2128                                         iter->flags & BTREE_ITER_INTENT,
2129                                         btree_iter_ip_allocated(iter));
2130
2131                 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2132                 if (unlikely(ret)) {
2133                         /* ensure that iter->k is consistent with iter->pos: */
2134                         bch2_btree_iter_set_pos(iter, iter->pos);
2135                         k = bkey_s_c_err(ret);
2136                         goto out;
2137                 }
2138
2139                 next_update = iter->flags & BTREE_ITER_WITH_UPDATES
2140                         ? btree_trans_peek_updates(trans, iter->btree_id, search_key)
2141                         : NULL;
2142                 k = btree_path_level_peek_all(trans->c, &iter->path->l[0], &iter->k);
2143
2144                 /* * In the btree, deleted keys sort before non deleted: */
2145                 if (k.k && bkey_deleted(k.k) &&
2146                     (!next_update ||
2147                      bpos_cmp(k.k->p, next_update->k.p) <= 0)) {
2148                         search_key = k.k->p;
2149                         continue;
2150                 }
2151
2152                 if (next_update &&
2153                     bpos_cmp(next_update->k.p,
2154                              k.k ? k.k->p : iter->path->l[0].b->key.k.p) <= 0) {
2155                         iter->k = next_update->k;
2156                         k = bkey_i_to_s_c(next_update);
2157                 }
2158
2159                 if (likely(k.k)) {
2160                         /*
2161                          * We can never have a key in a leaf node at POS_MAX, so
2162                          * we don't have to check these successor() calls:
2163                          */
2164                         if ((iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) &&
2165                             !bch2_snapshot_is_ancestor(trans->c,
2166                                                        iter->snapshot,
2167                                                        k.k->p.snapshot)) {
2168                                 search_key = bpos_successor(k.k->p);
2169                                 continue;
2170                         }
2171
2172                         if (bkey_whiteout(k.k) &&
2173                             !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) {
2174                                 search_key = bkey_successor(iter, k.k->p);
2175                                 continue;
2176                         }
2177
2178                         break;
2179                 } else if (likely(bpos_cmp(iter->path->l[0].b->key.k.p, SPOS_MAX))) {
2180                         /* Advance to next leaf node: */
2181                         search_key = bpos_successor(iter->path->l[0].b->key.k.p);
2182                 } else {
2183                         /* End of btree: */
2184                         bch2_btree_iter_set_pos(iter, SPOS_MAX);
2185                         k = bkey_s_c_null;
2186                         goto out;
2187                 }
2188         }
2189
2190         /*
2191          * iter->pos should be mononotically increasing, and always be equal to
2192          * the key we just returned - except extents can straddle iter->pos:
2193          */
2194         if (!(iter->flags & BTREE_ITER_IS_EXTENTS))
2195                 iter->pos = k.k->p;
2196         else if (bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0)
2197                 iter->pos = bkey_start_pos(k.k);
2198
2199         if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2200                 iter->pos.snapshot = iter->snapshot;
2201
2202         cmp = bpos_cmp(k.k->p, iter->path->pos);
2203         if (cmp) {
2204                 iter->path = bch2_btree_path_make_mut(trans, iter->path,
2205                                         iter->flags & BTREE_ITER_INTENT,
2206                                         btree_iter_ip_allocated(iter));
2207                 iter->path->pos = k.k->p;
2208                 btree_path_check_sort(trans, iter->path, cmp);
2209         }
2210 out:
2211         iter->path->should_be_locked = true;
2212
2213         bch2_btree_iter_verify_entry_exit(iter);
2214         bch2_btree_iter_verify(iter);
2215         ret = bch2_btree_iter_verify_ret(iter, k);
2216         if (unlikely(ret))
2217                 return bkey_s_c_err(ret);
2218
2219         return k;
2220 }
2221
2222 /**
2223  * bch2_btree_iter_next: returns first key greater than iterator's current
2224  * position
2225  */
2226 struct bkey_s_c bch2_btree_iter_next(struct btree_iter *iter)
2227 {
2228         if (!bch2_btree_iter_advance(iter))
2229                 return bkey_s_c_null;
2230
2231         return bch2_btree_iter_peek(iter);
2232 }
2233
2234 /**
2235  * bch2_btree_iter_peek_prev: returns first key less than or equal to
2236  * iterator's current position
2237  */
2238 struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter)
2239 {
2240         struct btree_trans *trans = iter->trans;
2241         struct bpos search_key = iter->pos;
2242         struct btree_path *saved_path = NULL;
2243         struct bkey_s_c k;
2244         struct bkey saved_k;
2245         const struct bch_val *saved_v;
2246         int ret;
2247
2248         EBUG_ON(iter->path->cached || iter->path->level);
2249         EBUG_ON(iter->flags & BTREE_ITER_WITH_UPDATES);
2250         bch2_btree_iter_verify(iter);
2251         bch2_btree_iter_verify_entry_exit(iter);
2252
2253         if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2254                 search_key.snapshot = U32_MAX;
2255
2256         while (1) {
2257                 iter->path = btree_path_set_pos(trans, iter->path, search_key,
2258                                                 iter->flags & BTREE_ITER_INTENT,
2259                                                 btree_iter_ip_allocated(iter));
2260
2261                 ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2262                 if (unlikely(ret)) {
2263                         /* ensure that iter->k is consistent with iter->pos: */
2264                         bch2_btree_iter_set_pos(iter, iter->pos);
2265                         k = bkey_s_c_err(ret);
2266                         goto out;
2267                 }
2268
2269                 k = btree_path_level_peek(trans->c, iter->path,
2270                                           &iter->path->l[0], &iter->k);
2271                 if (!k.k ||
2272                     ((iter->flags & BTREE_ITER_IS_EXTENTS)
2273                      ? bpos_cmp(bkey_start_pos(k.k), search_key) >= 0
2274                      : bpos_cmp(k.k->p, search_key) > 0))
2275                         k = btree_path_level_prev(trans->c, iter->path,
2276                                                   &iter->path->l[0], &iter->k);
2277
2278                 btree_path_check_sort(trans, iter->path, 0);
2279
2280                 if (likely(k.k)) {
2281                         if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS) {
2282                                 if (k.k->p.snapshot == iter->snapshot)
2283                                         goto got_key;
2284
2285                                 /*
2286                                  * If we have a saved candidate, and we're no
2287                                  * longer at the same _key_ (not pos), return
2288                                  * that candidate
2289                                  */
2290                                 if (saved_path && bkey_cmp(k.k->p, saved_k.p)) {
2291                                         bch2_path_put(trans, iter->path,
2292                                                       iter->flags & BTREE_ITER_INTENT);
2293                                         iter->path = saved_path;
2294                                         saved_path = NULL;
2295                                         iter->k = saved_k;
2296                                         k.v     = saved_v;
2297                                         goto got_key;
2298                                 }
2299
2300                                 if (bch2_snapshot_is_ancestor(iter->trans->c,
2301                                                               iter->snapshot,
2302                                                               k.k->p.snapshot)) {
2303                                         if (saved_path)
2304                                                 bch2_path_put(trans, saved_path,
2305                                                       iter->flags & BTREE_ITER_INTENT);
2306                                         saved_path = btree_path_clone(trans, iter->path,
2307                                                                 iter->flags & BTREE_ITER_INTENT);
2308                                         saved_k = *k.k;
2309                                         saved_v = k.v;
2310                                 }
2311
2312                                 search_key = bpos_predecessor(k.k->p);
2313                                 continue;
2314                         }
2315 got_key:
2316                         if (bkey_whiteout(k.k) &&
2317                             !(iter->flags & BTREE_ITER_ALL_SNAPSHOTS)) {
2318                                 search_key = bkey_predecessor(iter, k.k->p);
2319                                 if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2320                                         search_key.snapshot = U32_MAX;
2321                                 continue;
2322                         }
2323
2324                         break;
2325                 } else if (likely(bpos_cmp(iter->path->l[0].b->data->min_key, POS_MIN))) {
2326                         /* Advance to previous leaf node: */
2327                         search_key = bpos_predecessor(iter->path->l[0].b->data->min_key);
2328                 } else {
2329                         /* Start of btree: */
2330                         bch2_btree_iter_set_pos(iter, POS_MIN);
2331                         k = bkey_s_c_null;
2332                         goto out;
2333                 }
2334         }
2335
2336         EBUG_ON(bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0);
2337
2338         /* Extents can straddle iter->pos: */
2339         if (bkey_cmp(k.k->p, iter->pos) < 0)
2340                 iter->pos = k.k->p;
2341
2342         if (iter->flags & BTREE_ITER_FILTER_SNAPSHOTS)
2343                 iter->pos.snapshot = iter->snapshot;
2344 out:
2345         if (saved_path)
2346                 bch2_path_put(trans, saved_path, iter->flags & BTREE_ITER_INTENT);
2347         iter->path->should_be_locked = true;
2348
2349         bch2_btree_iter_verify_entry_exit(iter);
2350         bch2_btree_iter_verify(iter);
2351
2352         return k;
2353 }
2354
2355 /**
2356  * bch2_btree_iter_prev: returns first key less than iterator's current
2357  * position
2358  */
2359 struct bkey_s_c bch2_btree_iter_prev(struct btree_iter *iter)
2360 {
2361         if (!bch2_btree_iter_rewind(iter))
2362                 return bkey_s_c_null;
2363
2364         return bch2_btree_iter_peek_prev(iter);
2365 }
2366
2367 struct bkey_s_c bch2_btree_iter_peek_slot(struct btree_iter *iter)
2368 {
2369         struct btree_trans *trans = iter->trans;
2370         struct bpos search_key;
2371         struct bkey_s_c k;
2372         int ret;
2373
2374         EBUG_ON(iter->path->level);
2375         bch2_btree_iter_verify(iter);
2376         bch2_btree_iter_verify_entry_exit(iter);
2377
2378         /* extents can't span inode numbers: */
2379         if ((iter->flags & BTREE_ITER_IS_EXTENTS) &&
2380             unlikely(iter->pos.offset == KEY_OFFSET_MAX)) {
2381                 if (iter->pos.inode == KEY_INODE_MAX)
2382                         return bkey_s_c_null;
2383
2384                 bch2_btree_iter_set_pos(iter, bpos_nosnap_successor(iter->pos));
2385         }
2386
2387         search_key = btree_iter_search_key(iter);
2388         iter->path = btree_path_set_pos(trans, iter->path, search_key,
2389                                         iter->flags & BTREE_ITER_INTENT,
2390                                         btree_iter_ip_allocated(iter));
2391
2392         ret = bch2_btree_path_traverse(trans, iter->path, iter->flags);
2393         if (unlikely(ret))
2394                 return bkey_s_c_err(ret);
2395
2396         if ((iter->flags & BTREE_ITER_CACHED) ||
2397             !(iter->flags & (BTREE_ITER_IS_EXTENTS|BTREE_ITER_FILTER_SNAPSHOTS))) {
2398                 struct bkey_i *next_update;
2399
2400                 next_update = iter->flags & BTREE_ITER_WITH_UPDATES
2401                         ? btree_trans_peek_updates(trans, iter->btree_id, search_key)
2402                         : NULL;
2403
2404                 if (next_update &&
2405                     !bpos_cmp(next_update->k.p, iter->pos)) {
2406                         iter->k = next_update->k;
2407                         k = bkey_i_to_s_c(next_update);
2408                 } else {
2409                         k = bch2_btree_path_peek_slot(iter->path, &iter->k);
2410                 }
2411         } else {
2412                 struct bpos next;
2413
2414                 if (iter->flags & BTREE_ITER_INTENT) {
2415                         struct btree_iter iter2;
2416
2417                         bch2_trans_copy_iter(&iter2, iter);
2418                         k = bch2_btree_iter_peek(&iter2);
2419
2420                         if (k.k && !bkey_err(k)) {
2421                                 iter->k = iter2.k;
2422                                 k.k = &iter->k;
2423                         }
2424                         bch2_trans_iter_exit(trans, &iter2);
2425                 } else {
2426                         struct bpos pos = iter->pos;
2427
2428                         k = bch2_btree_iter_peek(iter);
2429                         iter->pos = pos;
2430                 }
2431
2432                 if (unlikely(bkey_err(k)))
2433                         return k;
2434
2435                 next = k.k ? bkey_start_pos(k.k) : POS_MAX;
2436
2437                 if (bkey_cmp(iter->pos, next) < 0) {
2438                         bkey_init(&iter->k);
2439                         iter->k.p = iter->pos;
2440                         bch2_key_resize(&iter->k,
2441                                         min_t(u64, KEY_SIZE_MAX,
2442                                               (next.inode == iter->pos.inode
2443                                                ? next.offset
2444                                                : KEY_OFFSET_MAX) -
2445                                               iter->pos.offset));
2446
2447                         k = (struct bkey_s_c) { &iter->k, NULL };
2448                         EBUG_ON(!k.k->size);
2449                 }
2450         }
2451
2452         iter->path->should_be_locked = true;
2453
2454         bch2_btree_iter_verify_entry_exit(iter);
2455         bch2_btree_iter_verify(iter);
2456         ret = bch2_btree_iter_verify_ret(iter, k);
2457         if (unlikely(ret))
2458                 return bkey_s_c_err(ret);
2459
2460         return k;
2461 }
2462
2463 struct bkey_s_c bch2_btree_iter_next_slot(struct btree_iter *iter)
2464 {
2465         if (!bch2_btree_iter_advance(iter))
2466                 return bkey_s_c_null;
2467
2468         return bch2_btree_iter_peek_slot(iter);
2469 }
2470
2471 struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *iter)
2472 {
2473         if (!bch2_btree_iter_rewind(iter))
2474                 return bkey_s_c_null;
2475
2476         return bch2_btree_iter_peek_slot(iter);
2477 }
2478
2479 /* new transactional stuff: */
2480
2481 static inline void btree_path_verify_sorted_ref(struct btree_trans *trans,
2482                                                 struct btree_path *path)
2483 {
2484         EBUG_ON(path->sorted_idx >= trans->nr_sorted);
2485         EBUG_ON(trans->sorted[path->sorted_idx] != path->idx);
2486         EBUG_ON(!(trans->paths_allocated & (1ULL << path->idx)));
2487 }
2488
2489 static inline void btree_trans_verify_sorted_refs(struct btree_trans *trans)
2490 {
2491 #ifdef CONFIG_BCACHEFS_DEBUG
2492         unsigned i;
2493
2494         for (i = 0; i < trans->nr_sorted; i++)
2495                 btree_path_verify_sorted_ref(trans, trans->paths + trans->sorted[i]);
2496 #endif
2497 }
2498
2499 static void btree_trans_verify_sorted(struct btree_trans *trans)
2500 {
2501 #ifdef CONFIG_BCACHEFS_DEBUG
2502         struct btree_path *path, *prev = NULL;
2503         unsigned i;
2504
2505         trans_for_each_path_inorder(trans, path, i) {
2506                 BUG_ON(prev && btree_path_cmp(prev, path) > 0);
2507                 prev = path;
2508         }
2509 #endif
2510 }
2511
2512 static inline void btree_path_swap(struct btree_trans *trans,
2513                                    struct btree_path *l, struct btree_path *r)
2514 {
2515         swap(l->sorted_idx, r->sorted_idx);
2516         swap(trans->sorted[l->sorted_idx],
2517              trans->sorted[r->sorted_idx]);
2518
2519         btree_path_verify_sorted_ref(trans, l);
2520         btree_path_verify_sorted_ref(trans, r);
2521 }
2522
2523 static void btree_path_check_sort(struct btree_trans *trans, struct btree_path *path,
2524                                   int cmp)
2525 {
2526         struct btree_path *n;
2527
2528         if (cmp <= 0) {
2529                 n = prev_btree_path(trans, path);
2530                 if (n && btree_path_cmp(n, path) > 0) {
2531                         do {
2532                                 btree_path_swap(trans, n, path);
2533                                 n = prev_btree_path(trans, path);
2534                         } while (n && btree_path_cmp(n, path) > 0);
2535
2536                         goto out;
2537                 }
2538         }
2539
2540         if (cmp >= 0) {
2541                 n = next_btree_path(trans, path);
2542                 if (n && btree_path_cmp(path, n) > 0) {
2543                         do {
2544                                 btree_path_swap(trans, path, n);
2545                                 n = next_btree_path(trans, path);
2546                         } while (n && btree_path_cmp(path, n) > 0);
2547                 }
2548         }
2549 out:
2550         btree_trans_verify_sorted(trans);
2551 }
2552
2553 static inline void btree_path_list_remove(struct btree_trans *trans,
2554                                           struct btree_path *path)
2555 {
2556         unsigned i;
2557
2558         EBUG_ON(path->sorted_idx >= trans->nr_sorted);
2559
2560         array_remove_item(trans->sorted, trans->nr_sorted, path->sorted_idx);
2561
2562         for (i = path->sorted_idx; i < trans->nr_sorted; i++)
2563                 trans->paths[trans->sorted[i]].sorted_idx = i;
2564
2565         path->sorted_idx = U8_MAX;
2566
2567         btree_trans_verify_sorted_refs(trans);
2568 }
2569
2570 static inline void btree_path_list_add(struct btree_trans *trans,
2571                                        struct btree_path *pos,
2572                                        struct btree_path *path)
2573 {
2574         unsigned i;
2575
2576         btree_trans_verify_sorted_refs(trans);
2577
2578         path->sorted_idx = pos ? pos->sorted_idx + 1 : 0;
2579
2580         array_insert_item(trans->sorted, trans->nr_sorted, path->sorted_idx, path->idx);
2581
2582         for (i = path->sorted_idx; i < trans->nr_sorted; i++)
2583                 trans->paths[trans->sorted[i]].sorted_idx = i;
2584
2585         btree_trans_verify_sorted_refs(trans);
2586 }
2587
2588 void bch2_trans_iter_exit(struct btree_trans *trans, struct btree_iter *iter)
2589 {
2590         if (iter->path)
2591                 bch2_path_put(trans, iter->path,
2592                               iter->flags & BTREE_ITER_INTENT);
2593         iter->path = NULL;
2594 }
2595
2596 static void __bch2_trans_iter_init(struct btree_trans *trans,
2597                                    struct btree_iter *iter,
2598                                    unsigned btree_id, struct bpos pos,
2599                                    unsigned locks_want,
2600                                    unsigned depth,
2601                                    unsigned flags,
2602                                    unsigned long ip)
2603 {
2604         EBUG_ON(trans->restarted);
2605
2606         if (!(flags & (BTREE_ITER_ALL_SNAPSHOTS|BTREE_ITER_NOT_EXTENTS)) &&
2607             btree_node_type_is_extents(btree_id))
2608                 flags |= BTREE_ITER_IS_EXTENTS;
2609
2610         if (!(flags & __BTREE_ITER_ALL_SNAPSHOTS) &&
2611             !btree_type_has_snapshots(btree_id))
2612                 flags &= ~BTREE_ITER_ALL_SNAPSHOTS;
2613
2614         if (!(flags & BTREE_ITER_ALL_SNAPSHOTS) &&
2615             btree_type_has_snapshots(btree_id))
2616                 flags |= BTREE_ITER_FILTER_SNAPSHOTS;
2617
2618         iter->trans     = trans;
2619         iter->path      = NULL;
2620         iter->btree_id  = btree_id;
2621         iter->min_depth = depth;
2622         iter->flags     = flags;
2623         iter->snapshot  = pos.snapshot;
2624         iter->pos       = pos;
2625         iter->k.type    = KEY_TYPE_deleted;
2626         iter->k.p       = pos;
2627         iter->k.size    = 0;
2628 #ifdef CONFIG_BCACHEFS_DEBUG
2629         iter->ip_allocated = ip;
2630 #endif
2631
2632         iter->path = bch2_path_get(trans, btree_id, iter->pos,
2633                                    locks_want, depth, flags, ip);
2634 }
2635
2636 void bch2_trans_iter_init(struct btree_trans *trans,
2637                           struct btree_iter *iter,
2638                           unsigned btree_id, struct bpos pos,
2639                           unsigned flags)
2640 {
2641         __bch2_trans_iter_init(trans, iter, btree_id, pos,
2642                                0, 0, flags, _RET_IP_);
2643 }
2644
2645 void bch2_trans_node_iter_init(struct btree_trans *trans,
2646                                struct btree_iter *iter,
2647                                enum btree_id btree_id,
2648                                struct bpos pos,
2649                                unsigned locks_want,
2650                                unsigned depth,
2651                                unsigned flags)
2652 {
2653         __bch2_trans_iter_init(trans, iter, btree_id, pos, locks_want, depth,
2654                                BTREE_ITER_NOT_EXTENTS|
2655                                __BTREE_ITER_ALL_SNAPSHOTS|
2656                                BTREE_ITER_ALL_SNAPSHOTS|
2657                                flags, _RET_IP_);
2658         BUG_ON(iter->path->locks_want    < min(locks_want, BTREE_MAX_DEPTH));
2659         BUG_ON(iter->path->level        != depth);
2660         BUG_ON(iter->min_depth          != depth);
2661 }
2662
2663 void bch2_trans_copy_iter(struct btree_iter *dst, struct btree_iter *src)
2664 {
2665         *dst = *src;
2666         if (src->path)
2667                 __btree_path_get(src->path, src->flags & BTREE_ITER_INTENT);
2668 }
2669
2670 void *bch2_trans_kmalloc(struct btree_trans *trans, size_t size)
2671 {
2672         size_t new_top = trans->mem_top + size;
2673         void *p;
2674
2675         if (new_top > trans->mem_bytes) {
2676                 size_t old_bytes = trans->mem_bytes;
2677                 size_t new_bytes = roundup_pow_of_two(new_top);
2678                 void *new_mem;
2679
2680                 WARN_ON_ONCE(new_bytes > BTREE_TRANS_MEM_MAX);
2681
2682                 new_mem = krealloc(trans->mem, new_bytes, GFP_NOFS);
2683                 if (!new_mem && new_bytes <= BTREE_TRANS_MEM_MAX) {
2684                         new_mem = mempool_alloc(&trans->c->btree_trans_mem_pool, GFP_KERNEL);
2685                         new_bytes = BTREE_TRANS_MEM_MAX;
2686                         kfree(trans->mem);
2687                 }
2688
2689                 if (!new_mem)
2690                         return ERR_PTR(-ENOMEM);
2691
2692                 trans->mem = new_mem;
2693                 trans->mem_bytes = new_bytes;
2694
2695                 if (old_bytes) {
2696                         trace_trans_restart_mem_realloced(trans->ip, _RET_IP_, new_bytes);
2697                         btree_trans_restart(trans);
2698                         return ERR_PTR(-EINTR);
2699                 }
2700         }
2701
2702         p = trans->mem + trans->mem_top;
2703         trans->mem_top += size;
2704         memset(p, 0, size);
2705         return p;
2706 }
2707
2708 /**
2709  * bch2_trans_begin() - reset a transaction after a interrupted attempt
2710  * @trans: transaction to reset
2711  *
2712  * While iterating over nodes or updating nodes a attempt to lock a btree
2713  * node may return EINTR when the trylock fails. When this occurs
2714  * bch2_trans_begin() should be called and the transaction retried.
2715  */
2716 void bch2_trans_begin(struct btree_trans *trans)
2717 {
2718         struct btree_insert_entry *i;
2719         struct btree_path *path;
2720
2721         trans_for_each_update(trans, i)
2722                 __btree_path_put(i->path, true);
2723
2724         memset(&trans->journal_res, 0, sizeof(trans->journal_res));
2725         trans->extra_journal_res        = 0;
2726         trans->nr_updates               = 0;
2727         trans->mem_top                  = 0;
2728
2729         trans->hooks                    = NULL;
2730         trans->extra_journal_entries    = NULL;
2731         trans->extra_journal_entry_u64s = 0;
2732
2733         if (trans->fs_usage_deltas) {
2734                 trans->fs_usage_deltas->used = 0;
2735                 memset(&trans->fs_usage_deltas->memset_start, 0,
2736                        (void *) &trans->fs_usage_deltas->memset_end -
2737                        (void *) &trans->fs_usage_deltas->memset_start);
2738         }
2739
2740         trans_for_each_path(trans, path) {
2741                 path->should_be_locked = false;
2742
2743                 /*
2744                  * XXX: we probably shouldn't be doing this if the transaction
2745                  * was restarted, but currently we still overflow transaction
2746                  * iterators if we do that
2747                  */
2748                 if (!path->ref && !path->preserve)
2749                         __bch2_path_free(trans, path);
2750                 else if (!path->ref)
2751                         path->preserve = false;
2752         }
2753
2754         bch2_trans_cond_resched(trans);
2755
2756         if (trans->restarted)
2757                 bch2_btree_path_traverse_all(trans);
2758
2759         trans->restarted = false;
2760 }
2761
2762 static void bch2_trans_alloc_paths(struct btree_trans *trans, struct bch_fs *c)
2763 {
2764         size_t paths_bytes      = sizeof(struct btree_path) * BTREE_ITER_MAX;
2765         size_t updates_bytes    = sizeof(struct btree_insert_entry) * BTREE_ITER_MAX;
2766         void *p = NULL;
2767
2768         BUG_ON(trans->used_mempool);
2769
2770 #ifdef __KERNEL__
2771         p = this_cpu_xchg(c->btree_paths_bufs->path , NULL);
2772 #endif
2773         if (!p)
2774                 p = mempool_alloc(&trans->c->btree_paths_pool, GFP_NOFS);
2775
2776         trans->paths            = p; p += paths_bytes;
2777         trans->updates          = p; p += updates_bytes;
2778 }
2779
2780 void bch2_trans_init(struct btree_trans *trans, struct bch_fs *c,
2781                      unsigned expected_nr_iters,
2782                      size_t expected_mem_bytes)
2783         __acquires(&c->btree_trans_barrier)
2784 {
2785         memset(trans, 0, sizeof(*trans));
2786         trans->c                = c;
2787         trans->ip               = _RET_IP_;
2788
2789         bch2_trans_alloc_paths(trans, c);
2790
2791         if (expected_mem_bytes) {
2792                 trans->mem_bytes = roundup_pow_of_two(expected_mem_bytes);
2793                 trans->mem = kmalloc(trans->mem_bytes, GFP_KERNEL|__GFP_NOFAIL);
2794
2795                 if (!unlikely(trans->mem)) {
2796                         trans->mem = mempool_alloc(&c->btree_trans_mem_pool, GFP_KERNEL);
2797                         trans->mem_bytes = BTREE_TRANS_MEM_MAX;
2798                 }
2799         }
2800
2801         trans->srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
2802
2803         trans->pid = current->pid;
2804         mutex_lock(&c->btree_trans_lock);
2805         list_add(&trans->list, &c->btree_trans_list);
2806         mutex_unlock(&c->btree_trans_lock);
2807 }
2808
2809 static void check_btree_paths_leaked(struct btree_trans *trans)
2810 {
2811 #ifdef CONFIG_BCACHEFS_DEBUG
2812         struct bch_fs *c = trans->c;
2813         struct btree_path *path;
2814
2815         trans_for_each_path(trans, path)
2816                 if (path->ref)
2817                         goto leaked;
2818         return;
2819 leaked:
2820         bch_err(c, "btree paths leaked from %pS!", (void *) trans->ip);
2821         trans_for_each_path(trans, path)
2822                 if (path->ref)
2823                         printk(KERN_ERR "  btree %s %pS\n",
2824                                bch2_btree_ids[path->btree_id],
2825                                (void *) path->ip_allocated);
2826         /* Be noisy about this: */
2827         bch2_fatal_error(c);
2828 #endif
2829 }
2830
2831 void bch2_trans_exit(struct btree_trans *trans)
2832         __releases(&c->btree_trans_barrier)
2833 {
2834         struct btree_insert_entry *i;
2835         struct bch_fs *c = trans->c;
2836
2837         bch2_trans_unlock(trans);
2838
2839         trans_for_each_update(trans, i)
2840                 __btree_path_put(i->path, true);
2841         trans->nr_updates               = 0;
2842
2843         check_btree_paths_leaked(trans);
2844
2845         mutex_lock(&c->btree_trans_lock);
2846         list_del(&trans->list);
2847         mutex_unlock(&c->btree_trans_lock);
2848
2849         srcu_read_unlock(&c->btree_trans_barrier, trans->srcu_idx);
2850
2851         bch2_journal_preres_put(&c->journal, &trans->journal_preres);
2852
2853         if (trans->fs_usage_deltas) {
2854                 if (trans->fs_usage_deltas->size + sizeof(trans->fs_usage_deltas) ==
2855                     REPLICAS_DELTA_LIST_MAX)
2856                         mempool_free(trans->fs_usage_deltas,
2857                                      &c->replicas_delta_pool);
2858                 else
2859                         kfree(trans->fs_usage_deltas);
2860         }
2861
2862         if (trans->mem_bytes == BTREE_TRANS_MEM_MAX)
2863                 mempool_free(trans->mem, &c->btree_trans_mem_pool);
2864         else
2865                 kfree(trans->mem);
2866
2867 #ifdef __KERNEL__
2868         /*
2869          * Userspace doesn't have a real percpu implementation:
2870          */
2871         trans->paths = this_cpu_xchg(c->btree_paths_bufs->path, trans->paths);
2872 #endif
2873
2874         if (trans->paths)
2875                 mempool_free(trans->paths, &c->btree_paths_pool);
2876
2877         trans->mem      = (void *) 0x1;
2878         trans->paths    = (void *) 0x1;
2879 }
2880
2881 static void __maybe_unused
2882 bch2_btree_path_node_to_text(struct printbuf *out,
2883                              struct btree_bkey_cached_common *_b,
2884                              bool cached)
2885 {
2886         pr_buf(out, "    l=%u %s:",
2887                _b->level, bch2_btree_ids[_b->btree_id]);
2888         bch2_bpos_to_text(out, btree_node_pos(_b, cached));
2889 }
2890
2891 static bool trans_has_locks(struct btree_trans *trans)
2892 {
2893         struct btree_path *path;
2894
2895         trans_for_each_path(trans, path)
2896                 if (path->nodes_locked)
2897                         return true;
2898         return false;
2899 }
2900
2901 void bch2_btree_trans_to_text(struct printbuf *out, struct bch_fs *c)
2902 {
2903         struct btree_trans *trans;
2904         struct btree_path *path;
2905         struct btree *b;
2906         unsigned l;
2907
2908         mutex_lock(&c->btree_trans_lock);
2909         list_for_each_entry(trans, &c->btree_trans_list, list) {
2910                 if (!trans_has_locks(trans))
2911                         continue;
2912
2913                 pr_buf(out, "%i %ps\n", trans->pid, (void *) trans->ip);
2914
2915                 trans_for_each_path(trans, path) {
2916                         if (!path->nodes_locked)
2917                                 continue;
2918
2919                         pr_buf(out, "  path %u %c l=%u %s:",
2920                                path->idx,
2921                                path->cached ? 'c' : 'b',
2922                                path->level,
2923                                bch2_btree_ids[path->btree_id]);
2924                         bch2_bpos_to_text(out, path->pos);
2925                         pr_buf(out, "\n");
2926
2927                         for (l = 0; l < BTREE_MAX_DEPTH; l++) {
2928                                 if (btree_node_locked(path, l)) {
2929                                         pr_buf(out, "    %s l=%u ",
2930                                                btree_node_intent_locked(path, l) ? "i" : "r", l);
2931                                         bch2_btree_path_node_to_text(out,
2932                                                         (void *) path->l[l].b,
2933                                                         path->cached);
2934                                         pr_buf(out, "\n");
2935                                 }
2936                         }
2937                 }
2938
2939                 b = READ_ONCE(trans->locking);
2940                 if (b) {
2941                         path = &trans->paths[trans->locking_path_idx];
2942                         pr_buf(out, "  locking path %u %c l=%u %s:",
2943                                trans->locking_path_idx,
2944                                path->cached ? 'c' : 'b',
2945                                trans->locking_level,
2946                                bch2_btree_ids[trans->locking_btree_id]);
2947                         bch2_bpos_to_text(out, trans->locking_pos);
2948
2949                         pr_buf(out, " node ");
2950                         bch2_btree_path_node_to_text(out,
2951                                         (void *) b, path->cached);
2952                         pr_buf(out, "\n");
2953                 }
2954         }
2955         mutex_unlock(&c->btree_trans_lock);
2956 }
2957
2958 void bch2_fs_btree_iter_exit(struct bch_fs *c)
2959 {
2960         if (c->btree_trans_barrier_initialized)
2961                 cleanup_srcu_struct(&c->btree_trans_barrier);
2962         mempool_exit(&c->btree_trans_mem_pool);
2963         mempool_exit(&c->btree_paths_pool);
2964 }
2965
2966 int bch2_fs_btree_iter_init(struct bch_fs *c)
2967 {
2968         unsigned nr = BTREE_ITER_MAX;
2969         int ret;
2970
2971         INIT_LIST_HEAD(&c->btree_trans_list);
2972         mutex_init(&c->btree_trans_lock);
2973
2974         ret   = mempool_init_kmalloc_pool(&c->btree_paths_pool, 1,
2975                         sizeof(struct btree_path) * nr +
2976                         sizeof(struct btree_insert_entry) * nr) ?:
2977                 mempool_init_kmalloc_pool(&c->btree_trans_mem_pool, 1,
2978                                           BTREE_TRANS_MEM_MAX) ?:
2979                 init_srcu_struct(&c->btree_trans_barrier);
2980         if (!ret)
2981                 c->btree_trans_barrier_initialized = true;
2982         return ret;
2983 }