]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal_reclaim.c
66f5dcce8889771bb113fa8c93863406a1528576
[bcachefs-tools-debian] / libbcachefs / journal_reclaim.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 #include "bcachefs.h"
4 #include "btree_key_cache.h"
5 #include "journal.h"
6 #include "journal_io.h"
7 #include "journal_reclaim.h"
8 #include "replicas.h"
9 #include "super.h"
10
11 #include <linux/kthread.h>
12 #include <linux/sched/mm.h>
13 #include <trace/events/bcachefs.h>
14
15 /* Free space calculations: */
16
17 static unsigned journal_space_from(struct journal_device *ja,
18                                    enum journal_space_from from)
19 {
20         switch (from) {
21         case journal_space_discarded:
22                 return ja->discard_idx;
23         case journal_space_clean_ondisk:
24                 return ja->dirty_idx_ondisk;
25         case journal_space_clean:
26                 return ja->dirty_idx;
27         default:
28                 BUG();
29         }
30 }
31
32 unsigned bch2_journal_dev_buckets_available(struct journal *j,
33                                             struct journal_device *ja,
34                                             enum journal_space_from from)
35 {
36         unsigned available = (journal_space_from(ja, from) -
37                               ja->cur_idx - 1 + ja->nr) % ja->nr;
38
39         /*
40          * Don't use the last bucket unless writing the new last_seq
41          * will make another bucket available:
42          */
43         if (available && ja->dirty_idx_ondisk == ja->dirty_idx)
44                 --available;
45
46         return available;
47 }
48
49 static void journal_set_remaining(struct journal *j, unsigned u64s_remaining)
50 {
51         union journal_preres_state old, new;
52         u64 v = atomic64_read(&j->prereserved.counter);
53
54         do {
55                 old.v = new.v = v;
56                 new.remaining = u64s_remaining;
57         } while ((v = atomic64_cmpxchg(&j->prereserved.counter,
58                                        old.v, new.v)) != old.v);
59 }
60
61 static struct journal_space {
62         unsigned        next_entry;
63         unsigned        remaining;
64 } __journal_space_available(struct journal *j, unsigned nr_devs_want,
65                             enum journal_space_from from)
66 {
67         struct bch_fs *c = container_of(j, struct bch_fs, journal);
68         struct bch_dev *ca;
69         unsigned sectors_next_entry     = UINT_MAX;
70         unsigned sectors_total          = UINT_MAX;
71         unsigned i, nr_devs = 0;
72         unsigned unwritten_sectors = j->reservations.prev_buf_unwritten
73                 ? journal_prev_buf(j)->sectors
74                 : 0;
75
76         rcu_read_lock();
77         for_each_member_device_rcu(ca, c, i,
78                                    &c->rw_devs[BCH_DATA_journal]) {
79                 struct journal_device *ja = &ca->journal;
80                 unsigned buckets_this_device, sectors_this_device;
81
82                 if (!ja->nr)
83                         continue;
84
85                 buckets_this_device = bch2_journal_dev_buckets_available(j, ja, from);
86                 sectors_this_device = ja->sectors_free;
87
88                 /*
89                  * We that we don't allocate the space for a journal entry
90                  * until we write it out - thus, account for it here:
91                  */
92                 if (unwritten_sectors >= sectors_this_device) {
93                         if (!buckets_this_device)
94                                 continue;
95
96                         buckets_this_device--;
97                         sectors_this_device = ca->mi.bucket_size;
98                 }
99
100                 sectors_this_device -= unwritten_sectors;
101
102                 if (sectors_this_device < ca->mi.bucket_size &&
103                     buckets_this_device) {
104                         buckets_this_device--;
105                         sectors_this_device = ca->mi.bucket_size;
106                 }
107
108                 if (!sectors_this_device)
109                         continue;
110
111                 sectors_next_entry = min(sectors_next_entry,
112                                          sectors_this_device);
113
114                 sectors_total = min(sectors_total,
115                         buckets_this_device * ca->mi.bucket_size +
116                         sectors_this_device);
117
118                 nr_devs++;
119         }
120         rcu_read_unlock();
121
122         if (nr_devs < nr_devs_want)
123                 return (struct journal_space) { 0, 0 };
124
125         return (struct journal_space) {
126                 .next_entry     = sectors_next_entry,
127                 .remaining      = max_t(int, 0, sectors_total - sectors_next_entry),
128         };
129 }
130
131 void bch2_journal_space_available(struct journal *j)
132 {
133         struct bch_fs *c = container_of(j, struct bch_fs, journal);
134         struct bch_dev *ca;
135         struct journal_space discarded, clean_ondisk, clean;
136         unsigned overhead, u64s_remaining = 0;
137         unsigned max_entry_size  = min(j->buf[0].buf_size >> 9,
138                                        j->buf[1].buf_size >> 9);
139         unsigned i, nr_online = 0, nr_devs_want;
140         bool can_discard = false;
141         int ret = 0;
142
143         lockdep_assert_held(&j->lock);
144
145         rcu_read_lock();
146         for_each_member_device_rcu(ca, c, i,
147                                    &c->rw_devs[BCH_DATA_journal]) {
148                 struct journal_device *ja = &ca->journal;
149
150                 if (!ja->nr)
151                         continue;
152
153                 while (ja->dirty_idx != ja->cur_idx &&
154                        ja->bucket_seq[ja->dirty_idx] < journal_last_seq(j))
155                         ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
156
157                 while (ja->dirty_idx_ondisk != ja->dirty_idx &&
158                        ja->bucket_seq[ja->dirty_idx_ondisk] < j->last_seq_ondisk)
159                         ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
160
161                 if (ja->discard_idx != ja->dirty_idx_ondisk)
162                         can_discard = true;
163
164                 max_entry_size = min_t(unsigned, max_entry_size, ca->mi.bucket_size);
165                 nr_online++;
166         }
167         rcu_read_unlock();
168
169         j->can_discard = can_discard;
170
171         if (nr_online < c->opts.metadata_replicas_required) {
172                 ret = cur_entry_insufficient_devices;
173                 goto out;
174         }
175
176         if (!fifo_free(&j->pin)) {
177                 ret = cur_entry_journal_pin_full;
178                 goto out;
179         }
180
181         nr_devs_want = min_t(unsigned, nr_online, c->opts.metadata_replicas);
182
183         discarded       = __journal_space_available(j, nr_devs_want, journal_space_discarded);
184         clean_ondisk    = __journal_space_available(j, nr_devs_want, journal_space_clean_ondisk);
185         clean           = __journal_space_available(j, nr_devs_want, journal_space_clean);
186
187         if (!discarded.next_entry)
188                 ret = cur_entry_journal_full;
189
190         overhead = DIV_ROUND_UP(clean.remaining, max_entry_size) *
191                 journal_entry_overhead(j);
192         u64s_remaining = clean.remaining << 6;
193         u64s_remaining = max_t(int, 0, u64s_remaining - overhead);
194         u64s_remaining /= 4;
195 out:
196         j->cur_entry_sectors    = !ret ? discarded.next_entry : 0;
197         j->cur_entry_error      = ret;
198         journal_set_remaining(j, u64s_remaining);
199         journal_check_may_get_unreserved(j);
200
201         if (!ret)
202                 journal_wake(j);
203 }
204
205 /* Discards - last part of journal reclaim: */
206
207 static bool should_discard_bucket(struct journal *j, struct journal_device *ja)
208 {
209         bool ret;
210
211         spin_lock(&j->lock);
212         ret = ja->discard_idx != ja->dirty_idx_ondisk;
213         spin_unlock(&j->lock);
214
215         return ret;
216 }
217
218 /*
219  * Advance ja->discard_idx as long as it points to buckets that are no longer
220  * dirty, issuing discards if necessary:
221  */
222 void bch2_journal_do_discards(struct journal *j)
223 {
224         struct bch_fs *c = container_of(j, struct bch_fs, journal);
225         struct bch_dev *ca;
226         unsigned iter;
227
228         mutex_lock(&j->discard_lock);
229
230         for_each_rw_member(ca, c, iter) {
231                 struct journal_device *ja = &ca->journal;
232
233                 while (should_discard_bucket(j, ja)) {
234                         if (ca->mi.discard &&
235                             blk_queue_discard(bdev_get_queue(ca->disk_sb.bdev)))
236                                 blkdev_issue_discard(ca->disk_sb.bdev,
237                                         bucket_to_sector(ca,
238                                                 ja->buckets[ja->discard_idx]),
239                                         ca->mi.bucket_size, GFP_NOIO, 0);
240
241                         spin_lock(&j->lock);
242                         ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
243
244                         bch2_journal_space_available(j);
245                         spin_unlock(&j->lock);
246                 }
247         }
248
249         mutex_unlock(&j->discard_lock);
250 }
251
252 /*
253  * Journal entry pinning - machinery for holding a reference on a given journal
254  * entry, holding it open to ensure it gets replayed during recovery:
255  */
256
257 static void bch2_journal_reclaim_fast(struct journal *j)
258 {
259         struct journal_entry_pin_list temp;
260         bool popped = false;
261
262         lockdep_assert_held(&j->lock);
263
264         /*
265          * Unpin journal entries whose reference counts reached zero, meaning
266          * all btree nodes got written out
267          */
268         while (!fifo_empty(&j->pin) &&
269                !atomic_read(&fifo_peek_front(&j->pin).count)) {
270                 BUG_ON(!list_empty(&fifo_peek_front(&j->pin).list));
271                 BUG_ON(!list_empty(&fifo_peek_front(&j->pin).flushed));
272                 BUG_ON(!fifo_pop(&j->pin, temp));
273                 popped = true;
274         }
275
276         if (popped)
277                 bch2_journal_space_available(j);
278 }
279
280 void bch2_journal_pin_put(struct journal *j, u64 seq)
281 {
282         struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
283
284         if (atomic_dec_and_test(&pin_list->count)) {
285                 spin_lock(&j->lock);
286                 bch2_journal_reclaim_fast(j);
287                 spin_unlock(&j->lock);
288         }
289 }
290
291 static inline void __journal_pin_drop(struct journal *j,
292                                       struct journal_entry_pin *pin)
293 {
294         struct journal_entry_pin_list *pin_list;
295
296         if (!journal_pin_active(pin))
297                 return;
298
299         pin_list = journal_seq_pin(j, pin->seq);
300         pin->seq = 0;
301         list_del_init(&pin->list);
302
303         /*
304          * Unpinning a journal entry make make journal_next_bucket() succeed, if
305          * writing a new last_seq will now make another bucket available:
306          */
307         if (atomic_dec_and_test(&pin_list->count) &&
308             pin_list == &fifo_peek_front(&j->pin))
309                 bch2_journal_reclaim_fast(j);
310         else if (fifo_used(&j->pin) == 1 &&
311                  atomic_read(&pin_list->count) == 1)
312                 journal_wake(j);
313 }
314
315 void bch2_journal_pin_drop(struct journal *j,
316                            struct journal_entry_pin *pin)
317 {
318         spin_lock(&j->lock);
319         __journal_pin_drop(j, pin);
320         spin_unlock(&j->lock);
321 }
322
323 static void bch2_journal_pin_add_locked(struct journal *j, u64 seq,
324                             struct journal_entry_pin *pin,
325                             journal_pin_flush_fn flush_fn)
326 {
327         struct journal_entry_pin_list *pin_list = journal_seq_pin(j, seq);
328
329         __journal_pin_drop(j, pin);
330
331         BUG_ON(!atomic_read(&pin_list->count) && seq == journal_last_seq(j));
332
333         atomic_inc(&pin_list->count);
334         pin->seq        = seq;
335         pin->flush      = flush_fn;
336
337         list_add(&pin->list, flush_fn ? &pin_list->list : &pin_list->flushed);
338 }
339
340 void __bch2_journal_pin_add(struct journal *j, u64 seq,
341                             struct journal_entry_pin *pin,
342                             journal_pin_flush_fn flush_fn)
343 {
344         spin_lock(&j->lock);
345         bch2_journal_pin_add_locked(j, seq, pin, flush_fn);
346         spin_unlock(&j->lock);
347
348         /*
349          * If the journal is currently full,  we might want to call flush_fn
350          * immediately:
351          */
352         journal_wake(j);
353 }
354
355 void bch2_journal_pin_update(struct journal *j, u64 seq,
356                              struct journal_entry_pin *pin,
357                              journal_pin_flush_fn flush_fn)
358 {
359         if (journal_pin_active(pin) && pin->seq < seq)
360                 return;
361
362         spin_lock(&j->lock);
363
364         if (pin->seq != seq) {
365                 bch2_journal_pin_add_locked(j, seq, pin, flush_fn);
366         } else {
367                 struct journal_entry_pin_list *pin_list =
368                         journal_seq_pin(j, seq);
369
370                 /*
371                  * If the pin is already pinning the right sequence number, it
372                  * still might've already been flushed:
373                  */
374                 list_move(&pin->list, &pin_list->list);
375         }
376
377         spin_unlock(&j->lock);
378
379         /*
380          * If the journal is currently full,  we might want to call flush_fn
381          * immediately:
382          */
383         journal_wake(j);
384 }
385
386 void bch2_journal_pin_copy(struct journal *j,
387                            struct journal_entry_pin *dst,
388                            struct journal_entry_pin *src,
389                            journal_pin_flush_fn flush_fn)
390 {
391         spin_lock(&j->lock);
392
393         if (journal_pin_active(src) &&
394             (!journal_pin_active(dst) || src->seq < dst->seq))
395                 bch2_journal_pin_add_locked(j, src->seq, dst, flush_fn);
396
397         spin_unlock(&j->lock);
398 }
399
400 /**
401  * bch2_journal_pin_flush: ensure journal pin callback is no longer running
402  */
403 void bch2_journal_pin_flush(struct journal *j, struct journal_entry_pin *pin)
404 {
405         BUG_ON(journal_pin_active(pin));
406
407         wait_event(j->pin_flush_wait, j->flush_in_progress != pin);
408 }
409
410 /*
411  * Journal reclaim: flush references to open journal entries to reclaim space in
412  * the journal
413  *
414  * May be done by the journal code in the background as needed to free up space
415  * for more journal entries, or as part of doing a clean shutdown, or to migrate
416  * data off of a specific device:
417  */
418
419 static struct journal_entry_pin *
420 journal_get_next_pin(struct journal *j, u64 max_seq, u64 *seq)
421 {
422         struct journal_entry_pin_list *pin_list;
423         struct journal_entry_pin *ret = NULL;
424
425         if (!test_bit(JOURNAL_RECLAIM_STARTED, &j->flags))
426                 return NULL;
427
428         spin_lock(&j->lock);
429
430         fifo_for_each_entry_ptr(pin_list, &j->pin, *seq)
431                 if (*seq > max_seq ||
432                     (ret = list_first_entry_or_null(&pin_list->list,
433                                 struct journal_entry_pin, list)))
434                         break;
435
436         if (ret) {
437                 list_move(&ret->list, &pin_list->flushed);
438                 BUG_ON(j->flush_in_progress);
439                 j->flush_in_progress = ret;
440         }
441
442         spin_unlock(&j->lock);
443
444         return ret;
445 }
446
447 /* returns true if we did work */
448 static u64 journal_flush_pins(struct journal *j, u64 seq_to_flush,
449                               unsigned min_nr)
450 {
451         struct journal_entry_pin *pin;
452         u64 seq, ret = 0;
453
454         lockdep_assert_held(&j->reclaim_lock);
455
456         while (1) {
457                 cond_resched();
458
459                 j->last_flushed = jiffies;
460
461                 pin = journal_get_next_pin(j, min_nr
462                                 ? U64_MAX : seq_to_flush, &seq);
463                 if (!pin)
464                         break;
465
466                 if (min_nr)
467                         min_nr--;
468
469                 pin->flush(j, pin, seq);
470
471                 BUG_ON(j->flush_in_progress != pin);
472                 j->flush_in_progress = NULL;
473                 wake_up(&j->pin_flush_wait);
474                 ret++;
475         }
476
477         return ret;
478 }
479
480 static u64 journal_seq_to_flush(struct journal *j)
481 {
482         struct bch_fs *c = container_of(j, struct bch_fs, journal);
483         struct bch_dev *ca;
484         u64 seq_to_flush = 0;
485         unsigned iter;
486
487         spin_lock(&j->lock);
488
489         for_each_rw_member(ca, c, iter) {
490                 struct journal_device *ja = &ca->journal;
491                 unsigned nr_buckets, bucket_to_flush;
492
493                 if (!ja->nr)
494                         continue;
495
496                 /* Try to keep the journal at most half full: */
497                 nr_buckets = ja->nr / 2;
498
499                 /* And include pre-reservations: */
500                 nr_buckets += DIV_ROUND_UP(j->prereserved.reserved,
501                                            (ca->mi.bucket_size << 6) -
502                                            journal_entry_overhead(j));
503
504                 nr_buckets = min(nr_buckets, ja->nr);
505
506                 bucket_to_flush = (ja->cur_idx + nr_buckets) % ja->nr;
507                 seq_to_flush = max(seq_to_flush,
508                                    ja->bucket_seq[bucket_to_flush]);
509         }
510
511         /* Also flush if the pin fifo is more than half full */
512         seq_to_flush = max_t(s64, seq_to_flush,
513                              (s64) journal_cur_seq(j) -
514                              (j->pin.size >> 1));
515         spin_unlock(&j->lock);
516
517         return seq_to_flush;
518 }
519
520 /**
521  * bch2_journal_reclaim - free up journal buckets
522  *
523  * Background journal reclaim writes out btree nodes. It should be run
524  * early enough so that we never completely run out of journal buckets.
525  *
526  * High watermarks for triggering background reclaim:
527  * - FIFO has fewer than 512 entries left
528  * - fewer than 25% journal buckets free
529  *
530  * Background reclaim runs until low watermarks are reached:
531  * - FIFO has more than 1024 entries left
532  * - more than 50% journal buckets free
533  *
534  * As long as a reclaim can complete in the time it takes to fill up
535  * 512 journal entries or 25% of all journal buckets, then
536  * journal_next_bucket() should not stall.
537  */
538 static void __bch2_journal_reclaim(struct journal *j, bool direct)
539 {
540         struct bch_fs *c = container_of(j, struct bch_fs, journal);
541         bool kthread = (current->flags & PF_KTHREAD) != 0;
542         u64 seq_to_flush, nr_flushed = 0;
543         size_t min_nr;
544         unsigned flags;
545
546         /*
547          * We can't invoke memory reclaim while holding the reclaim_lock -
548          * journal reclaim is required to make progress for memory reclaim
549          * (cleaning the caches), so we can't get stuck in memory reclaim while
550          * we're holding the reclaim lock:
551          */
552         lockdep_assert_held(&j->reclaim_lock);
553         flags = memalloc_noreclaim_save();
554
555         do {
556                 if (kthread && kthread_should_stop())
557                         break;
558
559                 bch2_journal_do_discards(j);
560
561                 seq_to_flush = journal_seq_to_flush(j);
562                 min_nr = 0;
563
564                 /*
565                  * If it's been longer than j->reclaim_delay_ms since we last flushed,
566                  * make sure to flush at least one journal pin:
567                  */
568                 if (time_after(jiffies, j->last_flushed +
569                                msecs_to_jiffies(j->reclaim_delay_ms)))
570                         min_nr = 1;
571
572                 if (j->prereserved.reserved * 2 > j->prereserved.remaining)
573                         min_nr = 1;
574
575                 if (atomic_read(&c->btree_cache.dirty) * 4 >
576                     c->btree_cache.used  * 3)
577                         min_nr = 1;
578
579                 min_nr = max(min_nr, bch2_nr_btree_keys_need_flush(c));
580
581                 trace_journal_reclaim_start(c,
582                                 min_nr,
583                                 j->prereserved.reserved,
584                                 j->prereserved.remaining,
585                                 atomic_read(&c->btree_cache.dirty),
586                                 c->btree_cache.used,
587                                 c->btree_key_cache.nr_dirty,
588                                 c->btree_key_cache.nr_keys);
589
590                 nr_flushed = journal_flush_pins(j, seq_to_flush, min_nr);
591
592                 if (direct)
593                         j->nr_direct_reclaim += nr_flushed;
594                 else
595                         j->nr_background_reclaim += nr_flushed;
596                 trace_journal_reclaim_finish(c, nr_flushed);
597         } while (min_nr);
598
599         memalloc_noreclaim_restore(flags);
600 }
601
602 void bch2_journal_reclaim(struct journal *j)
603 {
604         __bch2_journal_reclaim(j, true);
605 }
606
607 static int bch2_journal_reclaim_thread(void *arg)
608 {
609         struct journal *j = arg;
610         unsigned long next;
611
612         set_freezable();
613
614         kthread_wait_freezable(test_bit(JOURNAL_RECLAIM_STARTED, &j->flags));
615
616         while (!kthread_should_stop()) {
617                 j->reclaim_kicked = false;
618
619                 mutex_lock(&j->reclaim_lock);
620                 __bch2_journal_reclaim(j, false);
621                 mutex_unlock(&j->reclaim_lock);
622
623                 next = j->last_flushed + msecs_to_jiffies(j->reclaim_delay_ms);
624
625                 while (1) {
626                         set_current_state(TASK_INTERRUPTIBLE);
627                         if (kthread_should_stop())
628                                 break;
629                         if (j->reclaim_kicked)
630                                 break;
631                         if (time_after_eq(jiffies, next))
632                                 break;
633                         schedule_timeout(next - jiffies);
634                         try_to_freeze();
635
636                 }
637                 __set_current_state(TASK_RUNNING);
638         }
639
640         return 0;
641 }
642
643 void bch2_journal_reclaim_stop(struct journal *j)
644 {
645         struct task_struct *p = j->reclaim_thread;
646
647         j->reclaim_thread = NULL;
648
649         if (p) {
650                 kthread_stop(p);
651                 put_task_struct(p);
652         }
653 }
654
655 int bch2_journal_reclaim_start(struct journal *j)
656 {
657         struct bch_fs *c = container_of(j, struct bch_fs, journal);
658         struct task_struct *p;
659
660         if (j->reclaim_thread)
661                 return 0;
662
663         p = kthread_create(bch2_journal_reclaim_thread, j,
664                            "bch-reclaim/%s", c->name);
665         if (IS_ERR(p))
666                 return PTR_ERR(p);
667
668         get_task_struct(p);
669         j->reclaim_thread = p;
670         wake_up_process(p);
671         return 0;
672 }
673
674 static int journal_flush_done(struct journal *j, u64 seq_to_flush,
675                               bool *did_work)
676 {
677         int ret;
678
679         ret = bch2_journal_error(j);
680         if (ret)
681                 return ret;
682
683         mutex_lock(&j->reclaim_lock);
684
685         *did_work = journal_flush_pins(j, seq_to_flush, 0) != 0;
686
687         spin_lock(&j->lock);
688         /*
689          * If journal replay hasn't completed, the unreplayed journal entries
690          * hold refs on their corresponding sequence numbers
691          */
692         ret = !test_bit(JOURNAL_REPLAY_DONE, &j->flags) ||
693                 journal_last_seq(j) > seq_to_flush ||
694                 (fifo_used(&j->pin) == 1 &&
695                  atomic_read(&fifo_peek_front(&j->pin).count) == 1);
696
697         spin_unlock(&j->lock);
698         mutex_unlock(&j->reclaim_lock);
699
700         return ret;
701 }
702
703 bool bch2_journal_flush_pins(struct journal *j, u64 seq_to_flush)
704 {
705         bool did_work = false;
706
707         if (!test_bit(JOURNAL_STARTED, &j->flags))
708                 return false;
709
710         closure_wait_event(&j->async_wait,
711                 journal_flush_done(j, seq_to_flush, &did_work));
712
713         return did_work;
714 }
715
716 int bch2_journal_flush_device_pins(struct journal *j, int dev_idx)
717 {
718         struct bch_fs *c = container_of(j, struct bch_fs, journal);
719         struct journal_entry_pin_list *p;
720         u64 iter, seq = 0;
721         int ret = 0;
722
723         spin_lock(&j->lock);
724         fifo_for_each_entry_ptr(p, &j->pin, iter)
725                 if (dev_idx >= 0
726                     ? bch2_dev_list_has_dev(p->devs, dev_idx)
727                     : p->devs.nr < c->opts.metadata_replicas)
728                         seq = iter;
729         spin_unlock(&j->lock);
730
731         bch2_journal_flush_pins(j, seq);
732
733         ret = bch2_journal_error(j);
734         if (ret)
735                 return ret;
736
737         mutex_lock(&c->replicas_gc_lock);
738         bch2_replicas_gc_start(c, 1 << BCH_DATA_journal);
739
740         seq = 0;
741
742         spin_lock(&j->lock);
743         while (!ret && seq < j->pin.back) {
744                 struct bch_replicas_padded replicas;
745
746                 seq = max(seq, journal_last_seq(j));
747                 bch2_devlist_to_replicas(&replicas.e, BCH_DATA_journal,
748                                          journal_seq_pin(j, seq)->devs);
749                 seq++;
750
751                 spin_unlock(&j->lock);
752                 ret = bch2_mark_replicas(c, &replicas.e);
753                 spin_lock(&j->lock);
754         }
755         spin_unlock(&j->lock);
756
757         ret = bch2_replicas_gc_end(c, ret);
758         mutex_unlock(&c->replicas_gc_lock);
759
760         return ret;
761 }