2 * bcachefs journalling code, for btree insertions
4 * Copyright 2012 Google, Inc.
8 #include "alloc_foreground.h"
9 #include "bkey_methods.h"
13 #include "journal_io.h"
14 #include "journal_reclaim.h"
15 #include "journal_seq_blacklist.h"
18 #include <trace/events/bcachefs.h>
20 static bool __journal_entry_is_open(union journal_res_state state)
22 return state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL;
25 static bool journal_entry_is_open(struct journal *j)
27 return __journal_entry_is_open(j->reservations);
30 static void journal_pin_new_entry(struct journal *j, int count)
32 struct journal_entry_pin_list *p;
35 * The fifo_push() needs to happen at the same time as j->seq is
36 * incremented for journal_last_seq() to be calculated correctly
38 atomic64_inc(&j->seq);
39 p = fifo_push_ref(&j->pin);
41 INIT_LIST_HEAD(&p->list);
42 INIT_LIST_HEAD(&p->flushed);
43 atomic_set(&p->count, count);
47 static void bch2_journal_buf_init(struct journal *j)
49 struct journal_buf *buf = journal_cur_buf(j);
51 memset(buf->has_inode, 0, sizeof(buf->has_inode));
53 memset(buf->data, 0, sizeof(*buf->data));
54 buf->data->seq = cpu_to_le64(journal_cur_seq(j));
58 static inline bool journal_entry_empty(struct jset *j)
62 if (j->seq != j->last_seq)
65 vstruct_for_each(j, i)
66 if (i->type || i->u64s)
71 void bch2_journal_halt(struct journal *j)
73 union journal_res_state old, new;
74 u64 v = atomic64_read(&j->reservations.counter);
78 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
81 new.cur_entry_offset = JOURNAL_ENTRY_ERROR_VAL;
82 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
83 old.v, new.v)) != old.v);
86 closure_wake_up(&journal_cur_buf(j)->wait);
89 /* journal entry close/open: */
91 void __bch2_journal_buf_put(struct journal *j, bool need_write_just_set)
93 if (!need_write_just_set &&
94 test_bit(JOURNAL_NEED_WRITE, &j->flags))
95 bch2_time_stats_update(j->delay_time,
98 clear_bit(JOURNAL_NEED_WRITE, &j->flags);
100 closure_call(&j->io, bch2_journal_write, system_highpri_wq, NULL);
104 * Returns true if journal entry is now closed:
106 static bool __journal_entry_close(struct journal *j)
108 struct bch_fs *c = container_of(j, struct bch_fs, journal);
109 struct journal_buf *buf = journal_cur_buf(j);
110 union journal_res_state old, new;
111 u64 v = atomic64_read(&j->reservations.counter);
112 bool set_need_write = false;
115 lockdep_assert_held(&j->lock);
119 if (old.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL)
122 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL) {
123 /* this entry will never be written: */
124 closure_wake_up(&buf->wait);
128 if (!test_bit(JOURNAL_NEED_WRITE, &j->flags)) {
129 set_bit(JOURNAL_NEED_WRITE, &j->flags);
130 j->need_write_time = local_clock();
131 set_need_write = true;
134 if (new.prev_buf_unwritten)
137 new.cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL;
139 new.prev_buf_unwritten = 1;
141 BUG_ON(journal_state_count(new, new.idx));
142 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
143 old.v, new.v)) != old.v);
145 buf->data->u64s = cpu_to_le32(old.cur_entry_offset);
147 sectors = vstruct_blocks_plus(buf->data, c->block_bits,
148 buf->u64s_reserved) << c->block_bits;
149 BUG_ON(sectors > buf->sectors);
150 buf->sectors = sectors;
152 bkey_extent_init(&buf->key);
155 * We have to set last_seq here, _before_ opening a new journal entry:
157 * A threads may replace an old pin with a new pin on their current
158 * journal reservation - the expectation being that the journal will
159 * contain either what the old pin protected or what the new pin
162 * After the old pin is dropped journal_last_seq() won't include the old
163 * pin, so we can only write the updated last_seq on the entry that
164 * contains whatever the new pin protects.
166 * Restated, we can _not_ update last_seq for a given entry if there
167 * could be a newer entry open with reservations/pins that have been
170 * Hence, we want update/set last_seq on the current journal entry right
171 * before we open a new one:
173 buf->data->last_seq = cpu_to_le64(journal_last_seq(j));
175 if (journal_entry_empty(buf->data))
176 clear_bit(JOURNAL_NOT_EMPTY, &j->flags);
178 set_bit(JOURNAL_NOT_EMPTY, &j->flags);
180 journal_pin_new_entry(j, 1);
182 bch2_journal_buf_init(j);
184 cancel_delayed_work(&j->write_work);
186 bch2_journal_space_available(j);
188 bch2_journal_buf_put(j, old.idx, set_need_write);
192 static bool journal_entry_close(struct journal *j)
197 ret = __journal_entry_close(j);
198 spin_unlock(&j->lock);
204 * should _only_ called from journal_res_get() - when we actually want a
205 * journal reservation - journal entry is open means journal is dirty:
209 * -ENOSPC: journal currently full, must invoke reclaim
210 * -EAGAIN: journal blocked, must wait
211 * -EROFS: insufficient rw devices or journal error
213 static int journal_entry_open(struct journal *j)
215 struct journal_buf *buf = journal_cur_buf(j);
216 union journal_res_state old, new;
220 lockdep_assert_held(&j->lock);
221 BUG_ON(journal_entry_is_open(j));
226 if (j->cur_entry_error)
227 return j->cur_entry_error;
229 BUG_ON(!j->cur_entry_sectors);
231 buf->u64s_reserved = j->entry_u64s_reserved;
232 buf->disk_sectors = j->cur_entry_sectors;
233 buf->sectors = min(buf->disk_sectors, buf->buf_size >> 9);
235 u64s = (int) (buf->sectors << 9) / sizeof(u64) -
236 journal_entry_overhead(j);
237 u64s = clamp_t(int, u64s, 0, JOURNAL_ENTRY_CLOSED_VAL - 1);
239 if (u64s <= le32_to_cpu(buf->data->u64s))
243 * Must be set before marking the journal entry as open:
245 j->cur_entry_u64s = u64s;
247 v = atomic64_read(&j->reservations.counter);
251 if (old.cur_entry_offset == JOURNAL_ENTRY_ERROR_VAL)
254 /* Handle any already added entries */
255 new.cur_entry_offset = le32_to_cpu(buf->data->u64s);
257 EBUG_ON(journal_state_count(new, new.idx));
258 journal_state_inc(&new);
259 } while ((v = atomic64_cmpxchg(&j->reservations.counter,
260 old.v, new.v)) != old.v);
262 if (j->res_get_blocked_start)
263 bch2_time_stats_update(j->blocked_time,
264 j->res_get_blocked_start);
265 j->res_get_blocked_start = 0;
267 mod_delayed_work(system_freezable_wq,
269 msecs_to_jiffies(j->write_delay_ms));
274 static bool journal_quiesced(struct journal *j)
276 union journal_res_state state = READ_ONCE(j->reservations);
277 bool ret = !state.prev_buf_unwritten && !__journal_entry_is_open(state);
280 journal_entry_close(j);
284 static void journal_quiesce(struct journal *j)
286 wait_event(j->wait, journal_quiesced(j));
289 static void journal_write_work(struct work_struct *work)
291 struct journal *j = container_of(work, struct journal, write_work.work);
293 journal_entry_close(j);
297 * Given an inode number, if that inode number has data in the journal that
298 * hasn't yet been flushed, return the journal sequence number that needs to be
301 u64 bch2_inode_journal_seq(struct journal *j, u64 inode)
303 size_t h = hash_64(inode, ilog2(sizeof(j->buf[0].has_inode) * 8));
306 if (!test_bit(h, j->buf[0].has_inode) &&
307 !test_bit(h, j->buf[1].has_inode))
311 if (test_bit(h, journal_cur_buf(j)->has_inode))
312 seq = journal_cur_seq(j);
313 else if (test_bit(h, journal_prev_buf(j)->has_inode))
314 seq = journal_cur_seq(j) - 1;
315 spin_unlock(&j->lock);
320 static int __journal_res_get(struct journal *j, struct journal_res *res,
323 struct bch_fs *c = container_of(j, struct bch_fs, journal);
324 struct journal_buf *buf;
328 if (journal_res_get_fast(j, res, flags))
331 if (bch2_journal_error(j))
337 * Recheck after taking the lock, so we don't race with another thread
338 * that just did journal_entry_open() and call journal_entry_close()
341 if (journal_res_get_fast(j, res, flags)) {
342 spin_unlock(&j->lock);
346 if (!(flags & JOURNAL_RES_GET_RESERVED) &&
347 !test_bit(JOURNAL_MAY_GET_UNRESERVED, &j->flags)) {
349 * Don't want to close current journal entry, just need to
357 * If we couldn't get a reservation because the current buf filled up,
358 * and we had room for a bigger entry on disk, signal that we want to
359 * realloc the journal bufs:
361 buf = journal_cur_buf(j);
362 if (journal_entry_is_open(j) &&
363 buf->buf_size >> 9 < buf->disk_sectors &&
364 buf->buf_size < JOURNAL_ENTRY_SIZE_MAX)
365 j->buf_size_want = max(j->buf_size_want, buf->buf_size << 1);
367 if (journal_entry_is_open(j) &&
368 !__journal_entry_close(j)) {
370 * We failed to get a reservation on the current open journal
371 * entry because it's full, and we can't close it because
372 * there's still a previous one in flight:
374 trace_journal_entry_full(c);
377 ret = journal_entry_open(j);
380 if ((ret == -EAGAIN || ret == -ENOSPC) &&
381 !j->res_get_blocked_start)
382 j->res_get_blocked_start = local_clock() ?: 1;
384 can_discard = j->can_discard;
385 spin_unlock(&j->lock);
390 if (ret == -ENOSPC) {
391 BUG_ON(!can_discard && (flags & JOURNAL_RES_GET_RESERVED));
394 * Journal is full - can't rely on reclaim from work item due to
397 trace_journal_full(c);
399 if (!(flags & JOURNAL_RES_GET_NONBLOCK)) {
401 bch2_journal_do_discards(j);
405 if (mutex_trylock(&j->reclaim_lock)) {
406 bch2_journal_reclaim(j);
407 mutex_unlock(&j->reclaim_lock);
418 * Essentially the entry function to the journaling code. When bcachefs is doing
419 * a btree insert, it calls this function to get the current journal write.
420 * Journal write is the structure used set up journal writes. The calling
421 * function will then add its keys to the structure, queuing them for the next
424 * To ensure forward progress, the current task must not be holding any
425 * btree node write locks.
427 int bch2_journal_res_get_slowpath(struct journal *j, struct journal_res *res,
432 closure_wait_event(&j->async_wait,
433 (ret = __journal_res_get(j, res, flags)) != -EAGAIN ||
434 (flags & JOURNAL_RES_GET_NONBLOCK));
438 /* journal_preres: */
440 static bool journal_preres_available(struct journal *j,
441 struct journal_preres *res,
444 bool ret = bch2_journal_preres_get_fast(j, res, new_u64s);
447 bch2_journal_reclaim_work(&j->reclaim_work.work);
452 int __bch2_journal_preres_get(struct journal *j,
453 struct journal_preres *res,
458 closure_wait_event(&j->preres_wait,
459 (ret = bch2_journal_error(j)) ||
460 journal_preres_available(j, res, new_u64s));
464 /* journal_entry_res: */
466 void bch2_journal_entry_res_resize(struct journal *j,
467 struct journal_entry_res *res,
470 union journal_res_state state;
471 int d = new_u64s - res->u64s;
475 j->entry_u64s_reserved += d;
479 j->cur_entry_u64s = max_t(int, 0, j->cur_entry_u64s - d);
481 state = READ_ONCE(j->reservations);
483 if (state.cur_entry_offset < JOURNAL_ENTRY_CLOSED_VAL &&
484 state.cur_entry_offset > j->cur_entry_u64s) {
485 j->cur_entry_u64s += d;
487 * Not enough room in current journal entry, have to flush it:
489 __journal_entry_close(j);
491 journal_cur_buf(j)->u64s_reserved += d;
494 spin_unlock(&j->lock);
498 /* journal flushing: */
500 u64 bch2_journal_last_unwritten_seq(struct journal *j)
505 seq = journal_cur_seq(j);
506 if (j->reservations.prev_buf_unwritten)
508 spin_unlock(&j->lock);
514 * bch2_journal_open_seq_async - try to open a new journal entry if @seq isn't
515 * open yet, or wait if we cannot
517 * used by the btree interior update machinery, when it needs to write a new
518 * btree root - every journal entry contains the roots of all the btrees, so it
519 * doesn't need to bother with getting a journal reservation
521 int bch2_journal_open_seq_async(struct journal *j, u64 seq, struct closure *cl)
523 struct bch_fs *c = container_of(j, struct bch_fs, journal);
529 * Can't try to open more than one sequence number ahead:
531 BUG_ON(journal_cur_seq(j) < seq && !journal_entry_is_open(j));
533 if (journal_cur_seq(j) > seq ||
534 journal_entry_is_open(j)) {
535 spin_unlock(&j->lock);
539 if (journal_cur_seq(j) < seq &&
540 !__journal_entry_close(j)) {
541 /* haven't finished writing out the previous one: */
542 trace_journal_entry_full(c);
545 BUG_ON(journal_cur_seq(j) != seq);
547 ret = journal_entry_open(j);
550 if ((ret == -EAGAIN || ret == -ENOSPC) &&
551 !j->res_get_blocked_start)
552 j->res_get_blocked_start = local_clock() ?: 1;
554 if (ret == -EAGAIN || ret == -ENOSPC)
555 closure_wait(&j->async_wait, cl);
557 spin_unlock(&j->lock);
559 if (ret == -ENOSPC) {
560 trace_journal_full(c);
561 bch2_journal_reclaim_work(&j->reclaim_work.work);
568 static int journal_seq_error(struct journal *j, u64 seq)
570 union journal_res_state state = READ_ONCE(j->reservations);
572 if (seq == journal_cur_seq(j))
573 return bch2_journal_error(j);
575 if (seq + 1 == journal_cur_seq(j) &&
576 !state.prev_buf_unwritten &&
583 static inline struct journal_buf *
584 journal_seq_to_buf(struct journal *j, u64 seq)
586 /* seq should be for a journal entry that has been opened: */
587 BUG_ON(seq > journal_cur_seq(j));
588 BUG_ON(seq == journal_cur_seq(j) &&
589 j->reservations.cur_entry_offset == JOURNAL_ENTRY_CLOSED_VAL);
591 if (seq == journal_cur_seq(j))
592 return journal_cur_buf(j);
593 if (seq + 1 == journal_cur_seq(j) &&
594 j->reservations.prev_buf_unwritten)
595 return journal_prev_buf(j);
600 * bch2_journal_wait_on_seq - wait for a journal entry to be written
602 * does _not_ cause @seq to be written immediately - if there is no other
603 * activity to cause the relevant journal entry to be filled up or flushed it
604 * can wait for an arbitrary amount of time (up to @j->write_delay_ms, which is
607 void bch2_journal_wait_on_seq(struct journal *j, u64 seq,
608 struct closure *parent)
610 struct journal_buf *buf;
614 if ((buf = journal_seq_to_buf(j, seq))) {
615 if (!closure_wait(&buf->wait, parent))
618 if (seq == journal_cur_seq(j)) {
620 if (bch2_journal_error(j))
621 closure_wake_up(&buf->wait);
625 spin_unlock(&j->lock);
629 * bch2_journal_flush_seq_async - wait for a journal entry to be written
631 * like bch2_journal_wait_on_seq, except that it triggers a write immediately if
634 void bch2_journal_flush_seq_async(struct journal *j, u64 seq,
635 struct closure *parent)
637 struct journal_buf *buf;
642 (buf = journal_seq_to_buf(j, seq)))
643 if (!closure_wait(&buf->wait, parent))
646 if (seq == journal_cur_seq(j))
647 __journal_entry_close(j);
648 spin_unlock(&j->lock);
651 static int journal_seq_flushed(struct journal *j, u64 seq)
656 ret = seq <= j->seq_ondisk ? 1 : journal_seq_error(j, seq);
658 if (seq == journal_cur_seq(j))
659 __journal_entry_close(j);
660 spin_unlock(&j->lock);
665 int bch2_journal_flush_seq(struct journal *j, u64 seq)
667 u64 start_time = local_clock();
670 ret = wait_event_killable(j->wait, (ret2 = journal_seq_flushed(j, seq)));
672 bch2_time_stats_update(j->flush_seq_time, start_time);
674 return ret ?: ret2 < 0 ? ret2 : 0;
678 * bch2_journal_meta_async - force a journal entry to be written
680 void bch2_journal_meta_async(struct journal *j, struct closure *parent)
682 struct journal_res res;
684 memset(&res, 0, sizeof(res));
686 bch2_journal_res_get(j, &res, jset_u64s(0), 0);
687 bch2_journal_res_put(j, &res);
689 bch2_journal_flush_seq_async(j, res.seq, parent);
692 int bch2_journal_meta(struct journal *j)
694 struct journal_res res;
697 memset(&res, 0, sizeof(res));
699 ret = bch2_journal_res_get(j, &res, jset_u64s(0), 0);
703 bch2_journal_res_put(j, &res);
705 return bch2_journal_flush_seq(j, res.seq);
709 * bch2_journal_flush_async - if there is an open journal entry, or a journal
710 * still being written, write it and wait for the write to complete
712 void bch2_journal_flush_async(struct journal *j, struct closure *parent)
714 u64 seq, journal_seq;
717 journal_seq = journal_cur_seq(j);
719 if (journal_entry_is_open(j)) {
721 } else if (journal_seq) {
722 seq = journal_seq - 1;
724 spin_unlock(&j->lock);
727 spin_unlock(&j->lock);
729 bch2_journal_flush_seq_async(j, seq, parent);
732 int bch2_journal_flush(struct journal *j)
734 u64 seq, journal_seq;
737 journal_seq = journal_cur_seq(j);
739 if (journal_entry_is_open(j)) {
741 } else if (journal_seq) {
742 seq = journal_seq - 1;
744 spin_unlock(&j->lock);
747 spin_unlock(&j->lock);
749 return bch2_journal_flush_seq(j, seq);
752 /* block/unlock the journal: */
754 void bch2_journal_unblock(struct journal *j)
758 spin_unlock(&j->lock);
763 void bch2_journal_block(struct journal *j)
767 spin_unlock(&j->lock);
772 /* allocate journal on a device: */
774 static int __bch2_set_nr_journal_buckets(struct bch_dev *ca, unsigned nr,
775 bool new_fs, struct closure *cl)
777 struct bch_fs *c = ca->fs;
778 struct journal_device *ja = &ca->journal;
779 struct bch_sb_field_journal *journal_buckets;
780 u64 *new_bucket_seq = NULL, *new_buckets = NULL;
783 /* don't handle reducing nr of buckets yet: */
788 new_buckets = kzalloc(nr * sizeof(u64), GFP_KERNEL);
789 new_bucket_seq = kzalloc(nr * sizeof(u64), GFP_KERNEL);
790 if (!new_buckets || !new_bucket_seq)
793 journal_buckets = bch2_sb_resize_journal(&ca->disk_sb,
794 nr + sizeof(*journal_buckets) / sizeof(u64));
795 if (!journal_buckets)
799 * We may be called from the device add path, before the new device has
800 * actually been added to the running filesystem:
803 spin_lock(&c->journal.lock);
805 memcpy(new_buckets, ja->buckets, ja->nr * sizeof(u64));
806 memcpy(new_bucket_seq, ja->bucket_seq, ja->nr * sizeof(u64));
807 swap(new_buckets, ja->buckets);
808 swap(new_bucket_seq, ja->bucket_seq);
811 spin_unlock(&c->journal.lock);
813 while (ja->nr < nr) {
814 struct open_bucket *ob = NULL;
819 bucket = bch2_bucket_alloc_new_fs(ca);
825 ob = bch2_bucket_alloc(c, ca, RESERVE_ALLOC,
828 ret = cl ? -EAGAIN : -ENOSPC;
832 bucket = sector_to_bucket(ca, ob->ptr.offset);
836 percpu_down_read_preempt_disable(&c->mark_lock);
837 spin_lock(&c->journal.lock);
842 pos = ja->nr ? (ja->cur_idx + 1) % ja->nr : 0;
843 __array_insert_item(ja->buckets, ja->nr, pos);
844 __array_insert_item(ja->bucket_seq, ja->nr, pos);
845 __array_insert_item(journal_buckets->buckets, ja->nr, pos);
848 ja->buckets[pos] = bucket;
849 ja->bucket_seq[pos] = 0;
850 journal_buckets->buckets[pos] = cpu_to_le64(bucket);
852 if (pos <= ja->discard_idx)
853 ja->discard_idx = (ja->discard_idx + 1) % ja->nr;
854 if (pos <= ja->dirty_idx_ondisk)
855 ja->dirty_idx_ondisk = (ja->dirty_idx_ondisk + 1) % ja->nr;
856 if (pos <= ja->dirty_idx)
857 ja->dirty_idx = (ja->dirty_idx + 1) % ja->nr;
858 if (pos <= ja->cur_idx)
859 ja->cur_idx = (ja->cur_idx + 1) % ja->nr;
861 bch2_mark_metadata_bucket(c, ca, bucket, BCH_DATA_JOURNAL,
863 gc_phase(GC_PHASE_SB),
867 spin_unlock(&c->journal.lock);
868 percpu_up_read_preempt_enable(&c->mark_lock);
874 bch2_open_bucket_put(c, ob);
879 kfree(new_bucket_seq);
886 * Allocate more journal space at runtime - not currently making use if it, but
889 int bch2_set_nr_journal_buckets(struct bch_fs *c, struct bch_dev *ca,
892 struct journal_device *ja = &ca->journal;
897 closure_init_stack(&cl);
900 struct disk_reservation disk_res = { 0, 0 };
904 mutex_lock(&c->sb_lock);
908 * note: journal buckets aren't really counted as _sectors_ used yet, so
909 * we don't need the disk reservation to avoid the BUG_ON() in buckets.c
910 * when space used goes up without a reservation - but we do need the
911 * reservation to ensure we'll actually be able to allocate:
914 if (bch2_disk_reservation_get(c, &disk_res,
915 bucket_to_sector(ca, nr - ja->nr), 1, 0)) {
916 mutex_unlock(&c->sb_lock);
920 ret = __bch2_set_nr_journal_buckets(ca, nr, false, &cl);
922 bch2_disk_reservation_put(c, &disk_res);
924 if (ja->nr != current_nr)
926 mutex_unlock(&c->sb_lock);
927 } while (ret == -EAGAIN);
932 int bch2_dev_journal_alloc(struct bch_dev *ca)
936 if (dynamic_fault("bcachefs:add:journal_alloc"))
940 * clamp journal size to 1024 buckets or 512MB (in sectors), whichever
943 nr = clamp_t(unsigned, ca->mi.nbuckets >> 8,
944 BCH_JOURNAL_BUCKETS_MIN,
946 (1 << 20) / ca->mi.bucket_size));
948 return __bch2_set_nr_journal_buckets(ca, nr, true, NULL);
951 /* startup/shutdown: */
953 static bool bch2_journal_writing_to_device(struct journal *j, unsigned dev_idx)
955 union journal_res_state state;
956 struct journal_buf *w;
960 state = READ_ONCE(j->reservations);
961 w = j->buf + !state.idx;
963 ret = state.prev_buf_unwritten &&
964 bch2_extent_has_device(bkey_i_to_s_c_extent(&w->key), dev_idx);
965 spin_unlock(&j->lock);
970 void bch2_dev_journal_stop(struct journal *j, struct bch_dev *ca)
972 wait_event(j->wait, !bch2_journal_writing_to_device(j, ca->dev_idx));
975 void bch2_fs_journal_stop(struct journal *j)
977 struct bch_fs *c = container_of(j, struct bch_fs, journal);
979 wait_event(j->wait, journal_entry_close(j));
981 /* do we need to write another journal entry? */
982 if (test_bit(JOURNAL_NOT_EMPTY, &j->flags) ||
983 c->btree_roots_dirty)
984 bch2_journal_meta(j);
988 BUG_ON(!bch2_journal_error(j) &&
989 test_bit(JOURNAL_NOT_EMPTY, &j->flags));
991 cancel_delayed_work_sync(&j->write_work);
992 cancel_delayed_work_sync(&j->reclaim_work);
995 int bch2_fs_journal_start(struct journal *j, u64 cur_seq,
996 struct list_head *journal_entries)
998 struct bch_fs *c = container_of(j, struct bch_fs, journal);
999 struct journal_entry_pin_list *p;
1000 struct journal_replay *i;
1001 u64 last_seq = cur_seq, nr, seq;
1003 if (!list_empty(journal_entries))
1004 last_seq = le64_to_cpu(list_last_entry(journal_entries,
1005 struct journal_replay,
1008 nr = cur_seq - last_seq;
1010 if (nr + 1 > j->pin.size) {
1012 init_fifo(&j->pin, roundup_pow_of_two(nr + 1), GFP_KERNEL);
1014 bch_err(c, "error reallocating journal fifo (%llu open entries)", nr);
1019 j->last_seq_ondisk = last_seq;
1020 j->pin.front = last_seq;
1021 j->pin.back = cur_seq;
1022 atomic64_set(&j->seq, cur_seq - 1);
1024 fifo_for_each_entry_ptr(p, &j->pin, seq) {
1025 INIT_LIST_HEAD(&p->list);
1026 INIT_LIST_HEAD(&p->flushed);
1027 atomic_set(&p->count, 0);
1031 list_for_each_entry(i, journal_entries, list) {
1032 seq = le64_to_cpu(i->j.seq);
1034 BUG_ON(seq < last_seq || seq >= cur_seq);
1036 p = journal_seq_pin(j, seq);
1038 atomic_set(&p->count, 1);
1042 spin_lock(&j->lock);
1044 set_bit(JOURNAL_STARTED, &j->flags);
1046 journal_pin_new_entry(j, 1);
1047 bch2_journal_buf_init(j);
1049 c->last_bucket_seq_cleanup = journal_cur_seq(j);
1051 bch2_journal_space_available(j);
1052 spin_unlock(&j->lock);
1059 void bch2_dev_journal_exit(struct bch_dev *ca)
1061 kfree(ca->journal.bio);
1062 kfree(ca->journal.buckets);
1063 kfree(ca->journal.bucket_seq);
1065 ca->journal.bio = NULL;
1066 ca->journal.buckets = NULL;
1067 ca->journal.bucket_seq = NULL;
1070 int bch2_dev_journal_init(struct bch_dev *ca, struct bch_sb *sb)
1072 struct journal_device *ja = &ca->journal;
1073 struct bch_sb_field_journal *journal_buckets =
1074 bch2_sb_get_journal(sb);
1077 ja->nr = bch2_nr_journal_buckets(journal_buckets);
1079 ja->bucket_seq = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1080 if (!ja->bucket_seq)
1083 ca->journal.bio = bio_kmalloc(GFP_KERNEL,
1084 DIV_ROUND_UP(JOURNAL_ENTRY_SIZE_MAX, PAGE_SIZE));
1085 if (!ca->journal.bio)
1088 ja->buckets = kcalloc(ja->nr, sizeof(u64), GFP_KERNEL);
1092 for (i = 0; i < ja->nr; i++)
1093 ja->buckets[i] = le64_to_cpu(journal_buckets->buckets[i]);
1098 void bch2_fs_journal_exit(struct journal *j)
1100 kvpfree(j->buf[1].data, j->buf[1].buf_size);
1101 kvpfree(j->buf[0].data, j->buf[0].buf_size);
1105 int bch2_fs_journal_init(struct journal *j)
1107 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1108 static struct lock_class_key res_key;
1111 pr_verbose_init(c->opts, "");
1113 spin_lock_init(&j->lock);
1114 spin_lock_init(&j->err_lock);
1115 init_waitqueue_head(&j->wait);
1116 INIT_DELAYED_WORK(&j->write_work, journal_write_work);
1117 INIT_DELAYED_WORK(&j->reclaim_work, bch2_journal_reclaim_work);
1118 init_waitqueue_head(&j->pin_flush_wait);
1119 mutex_init(&j->reclaim_lock);
1120 mutex_init(&j->discard_lock);
1122 lockdep_init_map(&j->res_map, "journal res", &res_key, 0);
1124 j->buf[0].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1125 j->buf[1].buf_size = JOURNAL_ENTRY_SIZE_MIN;
1126 j->write_delay_ms = 1000;
1127 j->reclaim_delay_ms = 100;
1130 j->entry_u64s_reserved +=
1131 BTREE_ID_NR * (JSET_KEYS_U64s + BKEY_EXTENT_U64s_MAX);
1133 atomic64_set(&j->reservations.counter,
1134 ((union journal_res_state)
1135 { .cur_entry_offset = JOURNAL_ENTRY_CLOSED_VAL }).v);
1137 if (!(init_fifo(&j->pin, JOURNAL_PIN, GFP_KERNEL)) ||
1138 !(j->buf[0].data = kvpmalloc(j->buf[0].buf_size, GFP_KERNEL)) ||
1139 !(j->buf[1].data = kvpmalloc(j->buf[1].buf_size, GFP_KERNEL))) {
1144 j->pin.front = j->pin.back = 1;
1146 pr_verbose_init(c->opts, "ret %i", ret);
1152 ssize_t bch2_journal_print_debug(struct journal *j, char *buf)
1154 struct printbuf out = _PBUF(buf, PAGE_SIZE);
1155 struct bch_fs *c = container_of(j, struct bch_fs, journal);
1156 union journal_res_state s;
1161 spin_lock(&j->lock);
1162 s = READ_ONCE(j->reservations);
1165 "active journal entries:\t%llu\n"
1167 "last_seq:\t\t%llu\n"
1168 "last_seq_ondisk:\t%llu\n"
1169 "prereserved:\t\t%u/%u\n"
1170 "current entry sectors:\t%u\n"
1171 "current entry:\t\t",
1174 journal_last_seq(j),
1176 j->prereserved.reserved,
1177 j->prereserved.remaining,
1178 j->cur_entry_sectors);
1180 switch (s.cur_entry_offset) {
1181 case JOURNAL_ENTRY_ERROR_VAL:
1182 pr_buf(&out, "error\n");
1184 case JOURNAL_ENTRY_CLOSED_VAL:
1185 pr_buf(&out, "closed\n");
1188 pr_buf(&out, "%u/%u\n",
1195 "current entry refs:\t%u\n"
1196 "prev entry unwritten:\t",
1197 journal_state_count(s, s.idx));
1199 if (s.prev_buf_unwritten)
1200 pr_buf(&out, "yes, ref %u sectors %u\n",
1201 journal_state_count(s, !s.idx),
1202 journal_prev_buf(j)->sectors);
1204 pr_buf(&out, "no\n");
1207 "need write:\t\t%i\n"
1208 "replay done:\t\t%i\n",
1209 test_bit(JOURNAL_NEED_WRITE, &j->flags),
1210 test_bit(JOURNAL_REPLAY_DONE, &j->flags));
1212 for_each_member_device_rcu(ca, c, iter,
1213 &c->rw_devs[BCH_DATA_JOURNAL]) {
1214 struct journal_device *ja = &ca->journal;
1222 "\tavailable\t%u:%u\n"
1223 "\tdiscard_idx\t\t%u\n"
1224 "\tdirty_idx_ondisk\t%u (seq %llu)\n"
1225 "\tdirty_idx\t\t%u (seq %llu)\n"
1226 "\tcur_idx\t\t%u (seq %llu)\n",
1228 bch2_journal_dev_buckets_available(j, ja, journal_space_discarded),
1231 ja->dirty_idx_ondisk, ja->bucket_seq[ja->dirty_idx_ondisk],
1232 ja->dirty_idx, ja->bucket_seq[ja->dirty_idx],
1233 ja->cur_idx, ja->bucket_seq[ja->cur_idx]);
1236 spin_unlock(&j->lock);
1239 return out.pos - buf;
1242 ssize_t bch2_journal_print_pins(struct journal *j, char *buf)
1244 struct printbuf out = _PBUF(buf, PAGE_SIZE);
1245 struct journal_entry_pin_list *pin_list;
1246 struct journal_entry_pin *pin;
1249 spin_lock(&j->lock);
1250 fifo_for_each_entry_ptr(pin_list, &j->pin, i) {
1251 pr_buf(&out, "%llu: count %u\n",
1252 i, atomic_read(&pin_list->count));
1254 list_for_each_entry(pin, &pin_list->list, list)
1255 pr_buf(&out, "\t%p %pf\n",
1258 if (!list_empty(&pin_list->flushed))
1259 pr_buf(&out, "flushed:\n");
1261 list_for_each_entry(pin, &pin_list->flushed, list)
1262 pr_buf(&out, "\t%p %pf\n",
1265 spin_unlock(&j->lock);
1267 return out.pos - buf;