]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/journal.c
ded4b6800d4c4c13f58d29556f41a3e7f68dfbbc
[bcachefs-tools-debian] / libbcachefs / journal.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * bcachefs journalling code, for btree insertions
4  *
5  * Copyright 2012 Google, Inc.
6  */
7
8 #include "bcachefs.h"
9 #include "alloc_foreground.h"
10 #include "bkey_methods.h"
11 #include "btree_gc.h"
12 #include "btree_update.h"
13 #include "buckets.h"
14 #include "error.h"
15 #include "journal.h"
16 #include "journal_io.h"
17 #include "journal_reclaim.h"
18 #include "journal_seq_blacklist.h"
19 #include "super-io.h"
20
21 #include <trace/events/bcachefs.h>
22
23 static inline bool journal_seq_unwritten(struct journal *j, u64 seq)
24 {
25         return seq > j->seq_ondisk;
26 }
27
28 static bool __journal_entry_is_open(union journal_res_state state)
29 {
30         return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
31 }
32
33 static inline unsigned nr_unwritten_journal_entries(struct journal *j)
34 {
35         return atomic64_read(&j->seq) - j->seq_ondisk;
36 }
37
38 static bool journal_entry_is_open(struct journal *j)
39 {
40         return __journal_entry_is_open(j->reservations);
41 }
42
43 static inline struct journal_buf *
44 journal_seq_to_buf(struct journal *j, u64 seq)
45 {
46         struct journal_buf *buf = NULL;
47
48         EBUG_ON(seq > journal_cur_seq(j));
49
50         if (journal_seq_unwritten(j, seq)) {
51                 buf = j->buf + (seq & JOURNAL_BUF_MASK);
52                 EBUG_ON(le64_to_cpu(buf->data->seq) != seq);
53         }
54         return buf;
55 }
56
57 static void journal_pin_list_init(struct journal_entry_pin_list *p, int count)
58 {
59         INIT_LIST_HEAD(&p->list);
60         INIT_LIST_HEAD(&p->key_cache_list);
61         INIT_LIST_HEAD(&p->flushed);
62         atomic_set(&p->count, count);
63         p->devs.nr = 0;
64 }
65
66 /* journal entry close/open: */
67
68 void __bch2_journal_buf_put(struct journal *j)
69 {
70         struct bch_fs *c = container_of(j, struct bch_fs, journal);
71
72         closure_call(&j->io, bch2_journal_write, c->io_complete_wq, NULL);
73 }
74
75 /*
76  * Returns true if journal entry is now closed:
77  *
78  * We don't close a journal_buf until the next journal_buf is finished writing,
79  * and can be opened again - this also initializes the next journal_buf:
80  */
81 static void __journal_entry_close(struct journal *j, unsigned closed_val)
82 {
83         struct bch_fs *c = container_of(j, struct bch_fs, journal);
84         struct journal_buf *buf = journal_cur_buf(j);
85         union journal_res_state old, new;
86         u64 v = atomic64_read(&j->reservations.counter);
87         unsigned sectors;
88
89         BUG_ON(closed_val != JOURNAL_ENTRY_CLOSED_VAL &&
90                closed_val != JOURNAL_ENTRY_ERROR_VAL);
91
92         lockdep_assert_held(&j->lock);
93
94         do {
95                 old.v = new.v = v;
96                 new.cur_entry_offset = closed_val;
97
98                 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL ||
99                     old.cur_entry_offset == new.cur_entry_offset)
100                         return;
101         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
102                                        old.v, new.v)) != old.v);
103
104         if (!__journal_entry_is_open(old))
105                 return;
106
107         /* Close out old buffer: */
108         buf->data->u64s         = cpu_to_le32(old.cur_entry_offset);
109
110         sectors = vstruct_blocks_plus(buf->data, c->block_bits,
111                                       buf->u64s_reserved) << c->block_bits;
112         BUG_ON(sectors > buf->sectors);
113         buf->sectors = sectors;
114
115         /*
116          * We have to set last_seq here, _before_ opening a new journal entry:
117          *
118          * A threads may replace an old pin with a new pin on their current
119          * journal reservation - the expectation being that the journal will
120          * contain either what the old pin protected or what the new pin
121          * protects.
122          *
123          * After the old pin is dropped journal_last_seq() won't include the old
124          * pin, so we can only write the updated last_seq on the entry that
125          * contains whatever the new pin protects.
126          *
127          * Restated, we can _not_ update last_seq for a given entry if there
128          * could be a newer entry open with reservations/pins that have been
129          * taken against it.
130          *
131          * Hence, we want update/set last_seq on the current journal entry right
132          * before we open a new one:
133          */
134         buf->last_seq           = journal_last_seq(j);
135         buf->data->last_seq     = cpu_to_le64(buf->last_seq);
136         BUG_ON(buf->last_seq > le64_to_cpu(buf->data->seq));
137
138         __bch2_journal_pin_put(j, le64_to_cpu(buf->data->seq));
139
140         cancel_delayed_work(&j->write_work);
141
142         bch2_journal_space_available(j);
143
144         bch2_journal_buf_put(j, old.idx);
145 }
146
147 void bch2_journal_halt(struct journal *j)
148 {
149         spin_lock(&j->lock);
150         __journal_entry_close(j, JOURNAL_ENTRY_ERROR_VAL);
151         if (!j->err_seq)
152                 j->err_seq = journal_cur_seq(j);
153         spin_unlock(&j->lock);
154 }
155
156 static bool journal_entry_want_write(struct journal *j)
157 {
158         bool ret = !journal_entry_is_open(j) ||
159                 journal_cur_seq(j) == journal_last_unwritten_seq(j);
160
161         /* Don't close it yet if we already have a write in flight: */
162         if (ret)
163                 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
164         else if (nr_unwritten_journal_entries(j)) {
165                 struct journal_buf *buf = journal_cur_buf(j);
166
167                 if (!buf->flush_time) {
168                         buf->flush_time = local_clock() ?: 1;
169                         buf->expires = jiffies;
170                 }
171         }
172
173         return ret;
174 }
175
176 static bool journal_entry_close(struct journal *j)
177 {
178         bool ret;
179
180         spin_lock(&j->lock);
181         ret = journal_entry_want_write(j);
182         spin_unlock(&j->lock);
183
184         return ret;
185 }
186
187 /*
188  * should _only_ called from journal_res_get() - when we actually want a
189  * journal reservation - journal entry is open means journal is dirty:
190  *
191  * returns:
192  * 0:           success
193  * -ENOSPC:     journal currently full, must invoke reclaim
194  * -EAGAIN:     journal blocked, must wait
195  * -EROFS:      insufficient rw devices or journal error
196  */
197 static int journal_entry_open(struct journal *j)
198 {
199         struct bch_fs *c = container_of(j, struct bch_fs, journal);
200         struct journal_buf *buf = j->buf +
201                 ((journal_cur_seq(j) + 1) & JOURNAL_BUF_MASK);
202         union journal_res_state old, new;
203         int u64s;
204         u64 v;
205
206         lockdep_assert_held(&j->lock);
207         BUG_ON(journal_entry_is_open(j));
208         BUG_ON(BCH_SB_CLEAN(c->disk_sb.sb));
209
210         if (j->blocked)
211                 return cur_entry_blocked;
212
213         if (j->cur_entry_error)
214                 return j->cur_entry_error;
215
216         if (bch2_journal_error(j))
217                 return cur_entry_insufficient_devices; /* -EROFS */
218
219         if (!fifo_free(&j->pin))
220                 return cur_entry_journal_pin_full;
221
222         if (nr_unwritten_journal_entries(j) == ARRAY_SIZE(j->buf) - 1)
223                 return cur_entry_max_in_flight;
224
225         BUG_ON(!j->cur_entry_sectors);
226
227         buf->expires            =
228                 (journal_cur_seq(j) == j->flushed_seq_ondisk
229                  ? jiffies
230                  : j->last_flush_write) +
231                 msecs_to_jiffies(c->opts.journal_flush_delay);
232
233         buf->u64s_reserved      = j->entry_u64s_reserved;
234         buf->disk_sectors       = j->cur_entry_sectors;
235         buf->sectors            = min(buf->disk_sectors, buf->buf_size >> 9);
236
237         u64s = (int) (buf->sectors << 9) / sizeof(u64) -
238                 journal_entry_overhead(j);
239         u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
240
241         if (u64s <= 0)
242                 return cur_entry_journal_full;
243
244         /*
245          * The fifo_push() needs to happen at the same time as j->seq is
246          * incremented for journal_last_seq() to be calculated correctly
247          */
248         atomic64_inc(&j->seq);
249         journal_pin_list_init(fifo_push_ref(&j->pin), 1);
250
251         BUG_ON(j->buf + (journal_cur_seq(j) & JOURNAL_BUF_MASK) != buf);
252
253         bkey_extent_init(&buf->key);
254         buf->noflush    = false;
255         buf->must_flush = false;
256         buf->separate_flush = false;
257         buf->flush_time = 0;
258
259         memset(buf->data, 0, sizeof(*buf->data));
260         buf->data->seq  = cpu_to_le64(journal_cur_seq(j));
261         buf->data->u64s = 0;
262
263         /*
264          * Must be set before marking the journal entry as open:
265          */
266         j->cur_entry_u64s = u64s;
267
268         v = atomic64_read(&j->reservations.counter);
269         do {
270                 old.v = new.v = v;
271
272                 BUG_ON(old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL);
273
274                 new.idx++;
275                 BUG_ON(journal_state_count(new, new.idx));
276                 BUG_ON(new.idx != (journal_cur_seq(j) & JOURNAL_BUF_MASK));
277
278                 journal_state_inc(&new);
279                 new.cur_entry_offset = 0;
280         } while ((v = atomic64_cmpxchg(&j->reservations.counter,
281                                        old.v, new.v)) != old.v);
282
283         if (j->res_get_blocked_start)
284                 bch2_time_stats_update(j->blocked_time,
285                                        j->res_get_blocked_start);
286         j->res_get_blocked_start = 0;
287
288         mod_delayed_work(c->io_complete_wq,
289                          &j->write_work,
290                          msecs_to_jiffies(c->opts.journal_flush_delay));
291         journal_wake(j);
292         return 0;
293 }
294
295 static bool journal_quiesced(struct journal *j)
296 {
297         bool ret = atomic64_read(&j->seq) == j->seq_ondisk;
298
299         if (!ret)
300                 journal_entry_close(j);
301         return ret;
302 }
303
304 static void journal_quiesce(struct journal *j)
305 {
306         wait_event(j->wait, journal_quiesced(j));
307 }
308
309 static void journal_write_work(struct work_struct *work)
310 {
311         struct journal *j = container_of(work, struct journal, write_work.work);
312         struct bch_fs *c = container_of(j, struct bch_fs, journal);
313         long delta;
314
315         spin_lock(&j->lock);
316         if (!__journal_entry_is_open(j->reservations))
317                 goto unlock;
318
319         delta = journal_cur_buf(j)->expires - jiffies;
320
321         if (delta > 0)
322                 mod_delayed_work(c->io_complete_wq, &j->write_work, delta);
323         else
324                 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
325 unlock:
326         spin_unlock(&j->lock);
327 }
328
329 static int __journal_res_get(struct journal *j, struct journal_res *res,
330                              unsigned flags)
331 {
332         struct bch_fs *c = container_of(j, struct bch_fs, journal);
333         struct journal_buf *buf;
334         bool can_discard;
335         int ret;
336 retry:
337         if (journal_res_get_fast(j, res, flags))
338                 return 0;
339
340         if (bch2_journal_error(j))
341                 return -EROFS;
342
343         spin_lock(&j->lock);
344
345         /*
346          * Recheck after taking the lock, so we don't race with another thread
347          * that just did journal_entry_open() and call journal_entry_close()
348          * unnecessarily
349          */
350         if (journal_res_get_fast(j, res, flags)) {
351                 spin_unlock(&j->lock);
352                 return 0;
353         }
354
355         if (!(flags & JOURNAL_RES_GET_RESERVED) &&
356             !test_bit(JOURNAL_MAY_GET_UNRESERVED, &j->flags)) {
357                 /*
358                  * Don't want to close current journal entry, just need to
359                  * invoke reclaim:
360                  */
361                 ret = cur_entry_journal_full;
362                 goto unlock;
363         }
364
365         /*
366          * If we couldn't get a reservation because the current buf filled up,
367          * and we had room for a bigger entry on disk, signal that we want to
368          * realloc the journal bufs:
369          */
370         buf = journal_cur_buf(j);
371         if (journal_entry_is_open(j) &&
372             buf->buf_size >> 9 < buf->disk_sectors &&
373             buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
374                 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
375
376         __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
377         ret = journal_entry_open(j);
378
379         if (ret == cur_entry_max_in_flight)
380                 trace_journal_entry_full(c);
381 unlock:
382         if ((ret && ret != cur_entry_insufficient_devices) &&
383             !j->res_get_blocked_start) {
384                 j->res_get_blocked_start = local_clock() ?: 1;
385                 trace_journal_full(c);
386         }
387
388         can_discard = j->can_discard;
389         spin_unlock(&j->lock);
390
391         if (!ret)
392                 goto retry;
393
394         if ((ret == cur_entry_journal_full ||
395              ret == cur_entry_journal_pin_full) &&
396             !can_discard &&
397             !nr_unwritten_journal_entries(j) &&
398             (flags & JOURNAL_RES_GET_RESERVED)) {
399                 struct printbuf buf = PRINTBUF;
400
401                 bch_err(c, "Journal stuck! Hava a pre-reservation but journal full");
402
403                 bch2_journal_debug_to_text(&buf, j);
404                 bch_err(c, "%s", buf.buf);
405
406                 printbuf_reset(&buf);
407                 bch2_journal_pins_to_text(&buf, j);
408                 bch_err(c, "Journal pins:\n%s", buf.buf);
409
410                 printbuf_exit(&buf);
411                 bch2_fatal_error(c);
412                 dump_stack();
413         }
414
415         /*
416          * Journal is full - can't rely on reclaim from work item due to
417          * freezing:
418          */
419         if ((ret == cur_entry_journal_full ||
420              ret == cur_entry_journal_pin_full) &&
421             !(flags & JOURNAL_RES_GET_NONBLOCK)) {
422                 if (can_discard) {
423                         bch2_journal_do_discards(j);
424                         goto retry;
425                 }
426
427                 if (mutex_trylock(&j->reclaim_lock)) {
428                         bch2_journal_reclaim(j);
429                         mutex_unlock(&j->reclaim_lock);
430                 }
431         }
432
433         return ret == cur_entry_insufficient_devices ? -EROFS : -EAGAIN;
434 }
435
436 /*
437  * Essentially the entry function to the journaling code. When bcachefs is doing
438  * a btree insert, it calls this function to get the current journal write.
439  * Journal write is the structure used set up journal writes. The calling
440  * function will then add its keys to the structure, queuing them for the next
441  * write.
442  *
443  * To ensure forward progress, the current task must not be holding any
444  * btree node write locks.
445  */
446 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
447                                   unsigned flags)
448 {
449         int ret;
450
451         closure_wait_event(&j->async_wait,
452                    (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
453                    (flags & JOURNAL_RES_GET_NONBLOCK));
454         return ret;
455 }
456
457 /* journal_preres: */
458
459 static bool journal_preres_available(struct journal *j,
460                                      struct journal_preres *res,
461                                      unsigned new_u64s,
462                                      unsigned flags)
463 {
464         bool ret = bch2_journal_preres_get_fast(j, res, new_u64s, flags, true);
465
466         if (!ret && mutex_trylock(&j->reclaim_lock)) {
467                 bch2_journal_reclaim(j);
468                 mutex_unlock(&j->reclaim_lock);
469         }
470
471         return ret;
472 }
473
474 int __bch2_journal_preres_get(struct journal *j,
475                               struct journal_preres *res,
476                               unsigned new_u64s,
477                               unsigned flags)
478 {
479         int ret;
480
481         closure_wait_event(&j->preres_wait,
482                    (ret = bch2_journal_error(j)) ||
483                    journal_preres_available(j, res, new_u64s, flags));
484         return ret;
485 }
486
487 /* journal_entry_res: */
488
489 void bch2_journal_entry_res_resize(struct journal *j,
490                                    struct journal_entry_res *res,
491                                    unsigned new_u64s)
492 {
493         union journal_res_state state;
494         int d = new_u64s - res->u64s;
495
496         spin_lock(&j->lock);
497
498         j->entry_u64s_reserved += d;
499         if (d <= 0)
500                 goto out;
501
502         j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
503         smp_mb();
504         state = READ_ONCE(j->reservations);
505
506         if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
507             state.cur_entry_offset > j->cur_entry_u64s) {
508                 j->cur_entry_u64s += d;
509                 /*
510                  * Not enough room in current journal entry, have to flush it:
511                  */
512                 __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
513         } else {
514                 journal_cur_buf(j)->u64s_reserved += d;
515         }
516 out:
517         spin_unlock(&j->lock);
518         res->u64s += d;
519 }
520
521 /* journal flushing: */
522
523 /**
524  * bch2_journal_flush_seq_async - wait for a journal entry to be written
525  *
526  * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
527  * necessary
528  */
529 int bch2_journal_flush_seq_async(struct journal *j, u64 seq,
530                                  struct closure *parent)
531 {
532         struct journal_buf *buf;
533         int ret = 0;
534
535         if (seq <= j->flushed_seq_ondisk)
536                 return 1;
537
538         spin_lock(&j->lock);
539
540         if (WARN_ONCE(seq > journal_cur_seq(j),
541                       "requested to flush journal seq %llu, but currently at %llu",
542                       seq, journal_cur_seq(j)))
543                 goto out;
544
545         /* Recheck under lock: */
546         if (j->err_seq && seq >= j->err_seq) {
547                 ret = -EIO;
548                 goto out;
549         }
550
551         if (seq <= j->flushed_seq_ondisk) {
552                 ret = 1;
553                 goto out;
554         }
555
556         /* if seq was written, but not flushed - flush a newer one instead */
557         seq = max(seq, journal_last_unwritten_seq(j));
558
559 recheck_need_open:
560         if (seq > journal_cur_seq(j)) {
561                 struct journal_res res = { 0 };
562
563                 if (journal_entry_is_open(j))
564                         __journal_entry_close(j, JOURNAL_ENTRY_CLOSED_VAL);
565
566                 spin_unlock(&j->lock);
567
568                 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
569                 if (ret)
570                         return ret;
571
572                 seq = res.seq;
573                 buf = j->buf + (seq & JOURNAL_BUF_MASK);
574                 buf->must_flush = true;
575
576                 if (!buf->flush_time) {
577                         buf->flush_time = local_clock() ?: 1;
578                         buf->expires = jiffies;
579                 }
580
581                 if (parent && !closure_wait(&buf->wait, parent))
582                         BUG();
583
584                 bch2_journal_res_put(j, &res);
585
586                 spin_lock(&j->lock);
587                 goto want_write;
588         }
589
590         /*
591          * if write was kicked off without a flush, flush the next sequence
592          * number instead
593          */
594         buf = journal_seq_to_buf(j, seq);
595         if (buf->noflush) {
596                 seq++;
597                 goto recheck_need_open;
598         }
599
600         buf->must_flush = true;
601
602         if (parent && !closure_wait(&buf->wait, parent))
603                 BUG();
604 want_write:
605         if (seq == journal_cur_seq(j))
606                 journal_entry_want_write(j);
607 out:
608         spin_unlock(&j->lock);
609         return ret;
610 }
611
612 int bch2_journal_flush_seq(struct journal *j, u64 seq)
613 {
614         u64 start_time = local_clock();
615         int ret, ret2;
616
617         /*
618          * Don't update time_stats when @seq is already flushed:
619          */
620         if (seq <= j->flushed_seq_ondisk)
621                 return 0;
622
623         ret = wait_event_interruptible(j->wait, (ret2 = bch2_journal_flush_seq_async(j, seq, NULL)));
624
625         if (!ret)
626                 bch2_time_stats_update(j->flush_seq_time, start_time);
627
628         return ret ?: ret2 < 0 ? ret2 : 0;
629 }
630
631 int bch2_journal_meta(struct journal *j)
632 {
633         struct journal_buf *buf;
634         struct journal_res res;
635         int ret;
636
637         memset(&res, 0, sizeof(res));
638
639         ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
640         if (ret)
641                 return ret;
642
643         buf = j->buf + (res.seq & JOURNAL_BUF_MASK);
644         buf->must_flush = true;
645
646         if (!buf->flush_time) {
647                 buf->flush_time = local_clock() ?: 1;
648                 buf->expires = jiffies;
649         }
650
651         bch2_journal_res_put(j, &res);
652
653         return bch2_journal_flush_seq(j, res.seq);
654 }
655
656 /*
657  * bch2_journal_flush_async - if there is an open journal entry, or a journal
658  * still being written, write it and wait for the write to complete
659  */
660 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
661 {
662         bch2_journal_flush_seq_async(j, atomic64_read(&j->seq), parent);
663 }
664
665 int bch2_journal_flush(struct journal *j)
666 {
667         return bch2_journal_flush_seq(j, atomic64_read(&j->seq));
668 }
669
670 /*
671  * bch2_journal_noflush_seq - tell the journal not to issue any flushes before
672  * @seq
673  */
674 bool bch2_journal_noflush_seq(struct journal *j, u64 seq)
675 {
676         struct bch_fs *c = container_of(j, struct bch_fs, journal);
677         u64 unwritten_seq;
678         bool ret = false;
679
680         if (!(c->sb.features & (1ULL << BCH_FEATURE_journal_no_flush)))
681                 return false;
682
683         if (seq <= c->journal.flushed_seq_ondisk)
684                 return false;
685
686         spin_lock(&j->lock);
687         if (seq <= c->journal.flushed_seq_ondisk)
688                 goto out;
689
690         for (unwritten_seq = journal_last_unwritten_seq(j);
691              unwritten_seq < seq;
692              unwritten_seq++) {
693                 struct journal_buf *buf = journal_seq_to_buf(j, unwritten_seq);
694
695                 /* journal write is already in flight, and was a flush write: */
696                 if (unwritten_seq == journal_last_unwritten_seq(j) && !buf->noflush)
697                         goto out;
698
699                 buf->noflush = true;
700         }
701
702         ret = true;
703 out:
704         spin_unlock(&j->lock);
705         return ret;
706 }
707
708 /* block/unlock the journal: */
709
710 void bch2_journal_unblock(struct journal *j)
711 {
712         spin_lock(&j->lock);
713         j->blocked--;
714         spin_unlock(&j->lock);
715
716         journal_wake(j);
717 }
718
719 void bch2_journal_block(struct journal *j)
720 {
721         spin_lock(&j->lock);
722         j->blocked++;
723         spin_unlock(&j->lock);
724
725         journal_quiesce(j);
726 }
727
728 /* allocate journal on a device: */
729
730 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
731                                          bool new_fs, struct closure *cl)
732 {
733         struct bch_fs *c = ca->fs;
734         struct journal_device *ja = &ca->journal;
735         struct bch_sb_field_journal *journal_buckets;
736         u64 *new_bucket_seq = NULL, *new_buckets = NULL;
737         int ret = 0;
738
739         /* don't handle reducing nr of buckets yet: */
740         if (nr <= ja->nr)
741                 return 0;
742
743         new_buckets     = kzalloc(nr * sizeof(u64), GFP_KERNEL);
744         new_bucket_seq  = kzalloc(nr * sizeof(u64), GFP_KERNEL);
745         if (!new_buckets || !new_bucket_seq) {
746                 ret = -ENOMEM;
747                 goto err;
748         }
749
750         journal_buckets = bch2_sb_resize_journal(&ca->disk_sb,
751                                         nr + sizeof(*journal_buckets) / sizeof(u64));
752         if (!journal_buckets) {
753                 ret = -ENOSPC;
754                 goto err;
755         }
756
757         /*
758          * We may be called from the device add path, before the new device has
759          * actually been added to the running filesystem:
760          */
761         if (!new_fs)
762                 spin_lock(&c->journal.lock);
763
764         memcpy(new_buckets,     ja->buckets,    ja->nr * sizeof(u64));
765         memcpy(new_bucket_seq,  ja->bucket_seq, ja->nr * sizeof(u64));
766         swap(new_buckets,       ja->buckets);
767         swap(new_bucket_seq,    ja->bucket_seq);
768
769         if (!new_fs)
770                 spin_unlock(&c->journal.lock);
771
772         while (ja->nr < nr) {
773                 struct open_bucket *ob = NULL;
774                 unsigned pos;
775                 long b;
776
777                 if (new_fs) {
778                         b = bch2_bucket_alloc_new_fs(ca);
779                         if (b < 0) {
780                                 ret = -ENOSPC;
781                                 goto err;
782                         }
783                 } else {
784                         rcu_read_lock();
785                         ob = bch2_bucket_alloc(c, ca, RESERVE_NONE,
786                                                false, cl);
787                         rcu_read_unlock();
788                         if (IS_ERR(ob)) {
789                                 ret = cl ? -EAGAIN : -ENOSPC;
790                                 goto err;
791                         }
792
793                         b = ob->bucket;
794                 }
795
796                 if (c)
797                         spin_lock(&c->journal.lock);
798
799                 /*
800                  * XXX
801                  * For resize at runtime, we should be writing the new
802                  * superblock before inserting into the journal array
803                  */
804
805                 pos = ja->nr ? (ja->cur_idx + 1) % ja->nr : 0;
806                 __array_insert_item(ja->buckets,                ja->nr, pos);
807                 __array_insert_item(ja->bucket_seq,             ja->nr, pos);
808                 __array_insert_item(journal_buckets->buckets,   ja->nr, pos);
809                 ja->nr++;
810
811                 ja->buckets[pos] = b;
812                 ja->bucket_seq[pos] = 0;
813                 journal_buckets->buckets[pos] = cpu_to_le64(b);
814
815                 if (pos <= ja->discard_idx)
816                         ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
817                 if (pos <= ja->dirty_idx_ondisk)
818                         ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
819                 if (pos <= ja->dirty_idx)
820                         ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
821                 if (pos <= ja->cur_idx)
822                         ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
823
824                 if (c)
825                         spin_unlock(&c->journal.lock);
826
827                 if (!new_fs) {
828                         ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
829                                 bch2_trans_mark_metadata_bucket(&trans, ca,
830                                                 b, BCH_DATA_journal,
831                                                 ca->mi.bucket_size));
832
833                         bch2_open_bucket_put(c, ob);
834
835                         if (ret)
836                                 goto err;
837                 }
838         }
839 err:
840         bch2_sb_resize_journal(&ca->disk_sb,
841                 ja->nr + sizeof(*journal_buckets) / sizeof(u64));
842         kfree(new_bucket_seq);
843         kfree(new_buckets);
844
845         return ret;
846 }
847
848 /*
849  * Allocate more journal space at runtime - not currently making use if it, but
850  * the code works:
851  */
852 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
853                                 unsigned nr)
854 {
855         struct journal_device *ja = &ca->journal;
856         struct closure cl;
857         unsigned current_nr;
858         int ret;
859
860         closure_init_stack(&cl);
861
862         do {
863                 struct disk_reservation disk_res = { 0, 0 };
864
865                 closure_sync(&cl);
866
867                 mutex_lock(&c->sb_lock);
868                 current_nr = ja->nr;
869
870                 /*
871                  * note: journal buckets aren't really counted as _sectors_ used yet, so
872                  * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
873                  * when space used goes up without a reservation - but we do need the
874                  * reservation to ensure we'll actually be able to allocate:
875                  */
876
877                 if (bch2_disk_reservation_get(c, &disk_res,
878                                               bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
879                         mutex_unlock(&c->sb_lock);
880                         return -ENOSPC;
881                 }
882
883                 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
884
885                 bch2_disk_reservation_put(c, &disk_res);
886
887                 if (ja->nr != current_nr)
888                         bch2_write_super(c);
889                 mutex_unlock(&c->sb_lock);
890         } while (ret == -EAGAIN);
891
892         return ret;
893 }
894
895 int bch2_dev_journal_alloc(struct bch_dev *ca)
896 {
897         unsigned nr;
898
899         if (dynamic_fault("bcachefs:add:journal_alloc"))
900                 return -ENOMEM;
901
902         /* 1/128th of the device by default: */
903         nr = ca->mi.nbuckets >> 7;
904
905         /*
906          * clamp journal size to 8192 buckets or 8GB (in sectors), whichever
907          * is smaller:
908          */
909         nr = clamp_t(unsigned, nr,
910                      BCH_JOURNAL_BUCKETS_MIN,
911                      min(1 << 13,
912                          (1 << 24) / ca->mi.bucket_size));
913
914         return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
915 }
916
917 /* startup/shutdown: */
918
919 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
920 {
921         bool ret = false;
922         u64 seq;
923
924         spin_lock(&j->lock);
925         for (seq = journal_last_unwritten_seq(j);
926              seq <= journal_cur_seq(j) && !ret;
927              seq++) {
928                 struct journal_buf *buf = journal_seq_to_buf(j, seq);
929
930                 if (bch2_bkey_has_device(bkey_i_to_s_c(&buf->key), dev_idx))
931                         ret = true;
932         }
933         spin_unlock(&j->lock);
934
935         return ret;
936 }
937
938 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
939 {
940         wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
941 }
942
943 void bch2_fs_journal_stop(struct journal *j)
944 {
945         bch2_journal_reclaim_stop(j);
946         bch2_journal_flush_all_pins(j);
947
948         wait_event(j->wait, journal_entry_close(j));
949
950         /*
951          * Always write a new journal entry, to make sure the clock hands are up
952          * to date (and match the superblock)
953          */
954         bch2_journal_meta(j);
955
956         journal_quiesce(j);
957
958         BUG_ON(!bch2_journal_error(j) &&
959                test_bit(JOURNAL_REPLAY_DONE, &j->flags) &&
960                j->last_empty_seq != journal_cur_seq(j));
961
962         cancel_delayed_work_sync(&j->write_work);
963 }
964
965 int bch2_fs_journal_start(struct journal *j, u64 cur_seq,
966                           struct list_head *journal_entries)
967 {
968         struct bch_fs *c = container_of(j, struct bch_fs, journal);
969         struct journal_entry_pin_list *p;
970         struct journal_replay *i;
971         u64 last_seq = cur_seq, nr, seq;
972
973         if (!list_empty(journal_entries))
974                 last_seq = le64_to_cpu(list_last_entry(journal_entries,
975                                 struct journal_replay, list)->j.last_seq);
976
977         nr = cur_seq - last_seq;
978
979         if (nr + 1 > j->pin.size) {
980                 free_fifo(&j->pin);
981                 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
982                 if (!j->pin.data) {
983                         bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
984                         return -ENOMEM;
985                 }
986         }
987
988         j->replay_journal_seq   = last_seq;
989         j->replay_journal_seq_end = cur_seq;
990         j->last_seq_ondisk      = last_seq;
991         j->flushed_seq_ondisk   = cur_seq - 1;
992         j->seq_ondisk           = cur_seq - 1;
993         j->pin.front            = last_seq;
994         j->pin.back             = cur_seq;
995         atomic64_set(&j->seq, cur_seq - 1);
996
997         if (list_empty(journal_entries))
998                 j->last_empty_seq = cur_seq - 1;
999
1000         fifo_for_each_entry_ptr(p, &j->pin, seq)
1001                 journal_pin_list_init(p, 1);
1002
1003         list_for_each_entry(i, journal_entries, list) {
1004                 unsigned ptr;
1005
1006                 seq = le64_to_cpu(i->j.seq);
1007                 BUG_ON(seq >= cur_seq);
1008
1009                 if (seq < last_seq)
1010                         continue;
1011
1012                 if (journal_entry_empty(&i->j))
1013                         j->last_empty_seq = le64_to_cpu(i->j.seq);
1014
1015                 p = journal_seq_pin(j, seq);
1016
1017                 p->devs.nr = 0;
1018                 for (ptr = 0; ptr < i->nr_ptrs; ptr++)
1019                         bch2_dev_list_add_dev(&p->devs, i->ptrs[ptr].dev);
1020         }
1021
1022         if (list_empty(journal_entries))
1023                 j->last_empty_seq = cur_seq;
1024
1025         spin_lock(&j->lock);
1026
1027         set_bit(JOURNAL_STARTED, &j->flags);
1028         j->last_flush_write = jiffies;
1029
1030         j->reservations.idx = j->reservations.unwritten_idx = journal_cur_seq(j);
1031         j->reservations.unwritten_idx++;
1032
1033         c->last_bucket_seq_cleanup = journal_cur_seq(j);
1034
1035         bch2_journal_space_available(j);
1036         spin_unlock(&j->lock);
1037
1038         return bch2_journal_reclaim_start(j);
1039 }
1040
1041 /* init/exit: */
1042
1043 void bch2_dev_journal_exit(struct bch_dev *ca)
1044 {
1045         kfree(ca->journal.bio);
1046         kfree(ca->journal.buckets);
1047         kfree(ca->journal.bucket_seq);
1048
1049         ca->journal.bio         = NULL;
1050         ca->journal.buckets     = NULL;
1051         ca->journal.bucket_seq  = NULL;
1052 }
1053
1054 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1055 {
1056         struct journal_device *ja = &ca->journal;
1057         struct bch_sb_field_journal *journal_buckets =
1058                 bch2_sb_get_journal(sb);
1059         unsigned i;
1060
1061         ja->nr = bch2_nr_journal_buckets(journal_buckets);
1062
1063         ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1064         if (!ja->bucket_seq)
1065                 return -ENOMEM;
1066
1067         ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1068                         DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1069         if (!ca->journal.bio)
1070                 return -ENOMEM;
1071
1072         ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1073         if (!ja->buckets)
1074                 return -ENOMEM;
1075
1076         for (i = 0; i < ja->nr; i++)
1077                 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1078
1079         return 0;
1080 }
1081
1082 void bch2_fs_journal_exit(struct journal *j)
1083 {
1084         unsigned i;
1085
1086         for (i = 0; i < ARRAY_SIZE(j->buf); i++)
1087                 kvpfree(j->buf[i].data, j->buf[i].buf_size);
1088         free_fifo(&j->pin);
1089 }
1090
1091 int bch2_fs_journal_init(struct journal *j)
1092 {
1093         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1094         static struct lock_class_key res_key;
1095         unsigned i;
1096         int ret = 0;
1097
1098         pr_verbose_init(c->opts, "");
1099
1100         spin_lock_init(&j->lock);
1101         spin_lock_init(&j->err_lock);
1102         init_waitqueue_head(&j->wait);
1103         INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1104         init_waitqueue_head(&j->reclaim_wait);
1105         init_waitqueue_head(&j->pin_flush_wait);
1106         mutex_init(&j->reclaim_lock);
1107         mutex_init(&j->discard_lock);
1108
1109         lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1110
1111         atomic64_set(&j->reservations.counter,
1112                 ((union journal_res_state)
1113                  { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1114
1115         if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL))) {
1116                 ret = -ENOMEM;
1117                 goto out;
1118         }
1119
1120         for (i = 0; i < ARRAY_SIZE(j->buf); i++) {
1121                 j->buf[i].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1122                 j->buf[i].data = kvpmalloc(j->buf[i].buf_size, GFP_KERNEL);
1123                 if (!j->buf[i].data) {
1124                         ret = -ENOMEM;
1125                         goto out;
1126                 }
1127         }
1128
1129         j->pin.front = j->pin.back = 1;
1130 out:
1131         pr_verbose_init(c->opts, "ret %i", ret);
1132         return ret;
1133 }
1134
1135 /* debug: */
1136
1137 void __bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1138 {
1139         struct bch_fs *c = container_of(j, struct bch_fs, journal);
1140         union journal_res_state s;
1141         struct bch_dev *ca;
1142         unsigned long now = jiffies;
1143         u64 seq;
1144         unsigned i;
1145
1146         out->atomic++;
1147         out->tabstops[0] = 24;
1148
1149         rcu_read_lock();
1150         s = READ_ONCE(j->reservations);
1151
1152         pr_buf(out, "dirty journal entries:\t%llu\n",   fifo_used(&j->pin));
1153         pr_buf(out, "seq:\t\t\t%llu\n",                 journal_cur_seq(j));
1154         pr_buf(out, "seq_ondisk:\t\t%llu\n",            j->seq_ondisk);
1155         pr_buf(out, "last_seq:\t\t%llu\n",              journal_last_seq(j));
1156         pr_buf(out, "last_seq_ondisk:\t%llu\n",         j->last_seq_ondisk);
1157         pr_buf(out, "flushed_seq_ondisk:\t%llu\n",      j->flushed_seq_ondisk);
1158         pr_buf(out, "prereserved:\t\t%u/%u\n",          j->prereserved.reserved, j->prereserved.remaining);
1159         pr_buf(out, "each entry reserved:\t%u\n",       j->entry_u64s_reserved);
1160         pr_buf(out, "nr flush writes:\t%llu\n",         j->nr_flush_writes);
1161         pr_buf(out, "nr noflush writes:\t%llu\n",       j->nr_noflush_writes);
1162         pr_buf(out, "nr direct reclaim:\t%llu\n",       j->nr_direct_reclaim);
1163         pr_buf(out, "nr background reclaim:\t%llu\n",   j->nr_background_reclaim);
1164         pr_buf(out, "reclaim kicked:\t\t%u\n",          j->reclaim_kicked);
1165         pr_buf(out, "reclaim runs in:\t%u ms\n",        time_after(j->next_reclaim, now)
1166                ? jiffies_to_msecs(j->next_reclaim - jiffies) : 0);
1167         pr_buf(out, "current entry sectors:\t%u\n",     j->cur_entry_sectors);
1168         pr_buf(out, "current entry error:\t%u\n",       j->cur_entry_error);
1169         pr_buf(out, "current entry:\t\t");
1170
1171         switch (s.cur_entry_offset) {
1172         case JOURNAL_ENTRY_ERROR_VAL:
1173                 pr_buf(out, "error");
1174                 break;
1175         case JOURNAL_ENTRY_CLOSED_VAL:
1176                 pr_buf(out, "closed");
1177                 break;
1178         default:
1179                 pr_buf(out, "%u/%u", s.cur_entry_offset, j->cur_entry_u64s);
1180                 break;
1181         }
1182
1183         pr_newline(out);
1184
1185         for (seq = journal_cur_seq(j);
1186              seq >= journal_last_unwritten_seq(j);
1187              --seq) {
1188                 i = seq & JOURNAL_BUF_MASK;
1189
1190                 pr_buf(out, "unwritten entry:");
1191                 pr_tab(out);
1192                 pr_buf(out, "%llu", seq);
1193                 pr_newline(out);
1194                 pr_indent_push(out, 2);
1195
1196                 pr_buf(out, "refcount:");
1197                 pr_tab(out);
1198                 pr_buf(out, "%u", journal_state_count(s, i));
1199                 pr_newline(out);
1200
1201                 pr_buf(out, "sectors:");
1202                 pr_tab(out);
1203                 pr_buf(out, "%u", j->buf[i].sectors);
1204                 pr_newline(out);
1205
1206                 pr_buf(out, "expires");
1207                 pr_tab(out);
1208                 pr_buf(out, "%li jiffies", j->buf[i].expires - jiffies);
1209                 pr_newline(out);
1210
1211                 pr_indent_pop(out, 2);
1212         }
1213
1214         pr_buf(out,
1215                "replay done:\t\t%i\n",
1216                test_bit(JOURNAL_REPLAY_DONE,    &j->flags));
1217
1218         pr_buf(out, "space:\n");
1219         pr_buf(out, "\tdiscarded\t%u:%u\n",
1220                j->space[journal_space_discarded].next_entry,
1221                j->space[journal_space_discarded].total);
1222         pr_buf(out, "\tclean ondisk\t%u:%u\n",
1223                j->space[journal_space_clean_ondisk].next_entry,
1224                j->space[journal_space_clean_ondisk].total);
1225         pr_buf(out, "\tclean\t\t%u:%u\n",
1226                j->space[journal_space_clean].next_entry,
1227                j->space[journal_space_clean].total);
1228         pr_buf(out, "\ttotal\t\t%u:%u\n",
1229                j->space[journal_space_total].next_entry,
1230                j->space[journal_space_total].total);
1231
1232         for_each_member_device_rcu(ca, c, i,
1233                                    &c->rw_devs[BCH_DATA_journal]) {
1234                 struct journal_device *ja = &ca->journal;
1235
1236                 if (!test_bit(ca->dev_idx, c->rw_devs[BCH_DATA_journal].d))
1237                         continue;
1238
1239                 if (!ja->nr)
1240                         continue;
1241
1242                 pr_buf(out, "dev %u:\n",                i);
1243                 pr_buf(out, "\tnr\t\t%u\n",             ja->nr);
1244                 pr_buf(out, "\tbucket size\t%u\n",      ca->mi.bucket_size);
1245                 pr_buf(out, "\tavailable\t%u:%u\n",     bch2_journal_dev_buckets_available(j, ja, journal_space_discarded), ja->sectors_free);
1246                 pr_buf(out, "\tdiscard_idx\t%u\n",      ja->discard_idx);
1247                 pr_buf(out, "\tdirty_ondisk\t%u (seq %llu)\n", ja->dirty_idx_ondisk,    ja->bucket_seq[ja->dirty_idx_ondisk]);
1248                 pr_buf(out, "\tdirty_idx\t%u (seq %llu)\n", ja->dirty_idx,              ja->bucket_seq[ja->dirty_idx]);
1249                 pr_buf(out, "\tcur_idx\t\t%u (seq %llu)\n", ja->cur_idx,                ja->bucket_seq[ja->cur_idx]);
1250         }
1251
1252         rcu_read_unlock();
1253
1254         --out->atomic;
1255 }
1256
1257 void bch2_journal_debug_to_text(struct printbuf *out, struct journal *j)
1258 {
1259         spin_lock(&j->lock);
1260         __bch2_journal_debug_to_text(out, j);
1261         spin_unlock(&j->lock);
1262 }
1263
1264 bool bch2_journal_seq_pins_to_text(struct printbuf *out, struct journal *j, u64 *seq)
1265 {
1266         struct journal_entry_pin_list *pin_list;
1267         struct journal_entry_pin *pin;
1268
1269         spin_lock(&j->lock);
1270         *seq = max(*seq, j->pin.front);
1271
1272         if (*seq >= j->pin.back) {
1273                 spin_unlock(&j->lock);
1274                 return true;
1275         }
1276
1277         out->atomic++;
1278
1279         pin_list = journal_seq_pin(j, *seq);
1280
1281         pr_buf(out, "%llu: count %u", *seq, atomic_read(&pin_list->count));
1282         pr_newline(out);
1283         pr_indent_push(out, 2);
1284
1285         list_for_each_entry(pin, &pin_list->list, list) {
1286                 pr_buf(out, "\t%px %ps", pin, pin->flush);
1287                 pr_newline(out);
1288         }
1289
1290         list_for_each_entry(pin, &pin_list->key_cache_list, list) {
1291                 pr_buf(out, "\t%px %ps", pin, pin->flush);
1292                 pr_newline(out);
1293         }
1294
1295         if (!list_empty(&pin_list->flushed)) {
1296                 pr_buf(out, "flushed:");
1297                 pr_newline(out);
1298         }
1299
1300         list_for_each_entry(pin, &pin_list->flushed, list) {
1301                 pr_buf(out, "\t%px %ps", pin, pin->flush);
1302                 pr_newline(out);
1303         }
1304
1305         pr_indent_pop(out, 2);
1306
1307         --out->atomic;
1308         spin_unlock(&j->lock);
1309
1310         return false;
1311 }
1312
1313 void bch2_journal_pins_to_text(struct printbuf *out, struct journal *j)
1314 {
1315         u64 seq = 0;
1316
1317         while (!bch2_journal_seq_pins_to_text(out, j, &seq))
1318                 seq++;
1319 }