1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
5 #include "backpointers.h"
6 #include "btree_cache.h"
8 #include "btree_key_cache.h"
9 #include "btree_update.h"
10 #include "btree_update_interior.h"
12 #include "btree_write_buffer.h"
14 #include "buckets_waiting_for_journal.h"
24 #include <linux/kthread.h>
25 #include <linux/math64.h>
26 #include <linux/random.h>
27 #include <linux/rculist.h>
28 #include <linux/rcupdate.h>
29 #include <linux/sched/task.h>
30 #include <linux/sort.h>
32 static void bch2_discard_one_bucket_fast(struct bch_fs *c, struct bpos bucket);
34 /* Persistent alloc info: */
36 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
37 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
42 struct bkey_alloc_unpacked {
49 #define x(_name, _bits) u##_bits _name;
54 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
55 const void **p, unsigned field)
57 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
60 if (!(a->fields & (1 << field)))
65 v = *((const u8 *) *p);
84 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
87 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
88 const void *d = in->data;
93 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
98 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
101 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
102 const u8 *in = a.v->data;
103 const u8 *end = bkey_val_end(a);
104 unsigned fieldnr = 0;
109 out->oldest_gen = a.v->oldest_gen;
110 out->data_type = a.v->data_type;
112 #define x(_name, _bits) \
113 if (fieldnr < a.v->nr_fields) { \
114 ret = bch2_varint_decode_fast(in, end, &v); \
122 if (v != out->_name) \
126 BCH_ALLOC_FIELDS_V2()
131 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
134 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
135 const u8 *in = a.v->data;
136 const u8 *end = bkey_val_end(a);
137 unsigned fieldnr = 0;
142 out->oldest_gen = a.v->oldest_gen;
143 out->data_type = a.v->data_type;
144 out->need_discard = BCH_ALLOC_V3_NEED_DISCARD(a.v);
145 out->need_inc_gen = BCH_ALLOC_V3_NEED_INC_GEN(a.v);
146 out->journal_seq = le64_to_cpu(a.v->journal_seq);
148 #define x(_name, _bits) \
149 if (fieldnr < a.v->nr_fields) { \
150 ret = bch2_varint_decode_fast(in, end, &v); \
158 if (v != out->_name) \
162 BCH_ALLOC_FIELDS_V2()
167 static struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
169 struct bkey_alloc_unpacked ret = { .gen = 0 };
173 bch2_alloc_unpack_v1(&ret, k);
175 case KEY_TYPE_alloc_v2:
176 bch2_alloc_unpack_v2(&ret, k);
178 case KEY_TYPE_alloc_v3:
179 bch2_alloc_unpack_v3(&ret, k);
186 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
188 unsigned i, bytes = offsetof(struct bch_alloc, data);
190 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
191 if (a->fields & (1 << i))
192 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
194 return DIV_ROUND_UP(bytes, sizeof(u64));
197 int bch2_alloc_v1_invalid(struct bch_fs *c, struct bkey_s_c k,
198 enum bkey_invalid_flags flags,
199 struct printbuf *err)
201 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
204 /* allow for unknown fields */
205 bkey_fsck_err_on(bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v), c, err,
206 alloc_v1_val_size_bad,
207 "incorrect value size (%zu < %u)",
208 bkey_val_u64s(a.k), bch_alloc_v1_val_u64s(a.v));
213 int bch2_alloc_v2_invalid(struct bch_fs *c, struct bkey_s_c k,
214 enum bkey_invalid_flags flags,
215 struct printbuf *err)
217 struct bkey_alloc_unpacked u;
220 bkey_fsck_err_on(bch2_alloc_unpack_v2(&u, k), c, err,
221 alloc_v2_unpack_error,
227 int bch2_alloc_v3_invalid(struct bch_fs *c, struct bkey_s_c k,
228 enum bkey_invalid_flags flags,
229 struct printbuf *err)
231 struct bkey_alloc_unpacked u;
234 bkey_fsck_err_on(bch2_alloc_unpack_v3(&u, k), c, err,
235 alloc_v2_unpack_error,
241 int bch2_alloc_v4_invalid(struct bch_fs *c, struct bkey_s_c k,
242 enum bkey_invalid_flags flags, struct printbuf *err)
244 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
247 bkey_fsck_err_on(alloc_v4_u64s_noerror(a.v) > bkey_val_u64s(k.k), c, err,
248 alloc_v4_val_size_bad,
249 "bad val size (%u > %zu)",
250 alloc_v4_u64s_noerror(a.v), bkey_val_u64s(k.k));
252 bkey_fsck_err_on(!BCH_ALLOC_V4_BACKPOINTERS_START(a.v) &&
253 BCH_ALLOC_V4_NR_BACKPOINTERS(a.v), c, err,
254 alloc_v4_backpointers_start_bad,
255 "invalid backpointers_start");
257 bkey_fsck_err_on(alloc_data_type(*a.v, a.v->data_type) != a.v->data_type, c, err,
258 alloc_key_data_type_bad,
259 "invalid data type (got %u should be %u)",
260 a.v->data_type, alloc_data_type(*a.v, a.v->data_type));
262 switch (a.v->data_type) {
264 case BCH_DATA_need_gc_gens:
265 case BCH_DATA_need_discard:
266 bkey_fsck_err_on(bch2_bucket_sectors(*a.v) || a.v->stripe,
267 c, err, alloc_key_empty_but_have_data,
268 "empty data type free but have data");
271 case BCH_DATA_journal:
274 case BCH_DATA_parity:
275 bkey_fsck_err_on(!bch2_bucket_sectors_dirty(*a.v),
276 c, err, alloc_key_dirty_sectors_0,
277 "data_type %s but dirty_sectors==0",
278 bch2_data_type_str(a.v->data_type));
280 case BCH_DATA_cached:
281 bkey_fsck_err_on(!a.v->cached_sectors ||
282 bch2_bucket_sectors_dirty(*a.v) ||
284 c, err, alloc_key_cached_inconsistency,
285 "data type inconsistency");
287 bkey_fsck_err_on(!a.v->io_time[READ] &&
288 c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_to_lru_refs,
289 c, err, alloc_key_cached_but_read_time_zero,
290 "cached bucket with read_time == 0");
292 case BCH_DATA_stripe:
299 void bch2_alloc_v4_swab(struct bkey_s k)
301 struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
302 struct bch_backpointer *bp, *bps;
304 a->journal_seq = swab64(a->journal_seq);
305 a->flags = swab32(a->flags);
306 a->dirty_sectors = swab32(a->dirty_sectors);
307 a->cached_sectors = swab32(a->cached_sectors);
308 a->io_time[0] = swab64(a->io_time[0]);
309 a->io_time[1] = swab64(a->io_time[1]);
310 a->stripe = swab32(a->stripe);
311 a->nr_external_backpointers = swab32(a->nr_external_backpointers);
312 a->fragmentation_lru = swab64(a->fragmentation_lru);
314 bps = alloc_v4_backpointers(a);
315 for (bp = bps; bp < bps + BCH_ALLOC_V4_NR_BACKPOINTERS(a); bp++) {
316 bp->bucket_offset = swab40(bp->bucket_offset);
317 bp->bucket_len = swab32(bp->bucket_len);
318 bch2_bpos_swab(&bp->pos);
322 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
324 struct bch_alloc_v4 _a;
325 const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &_a);
328 printbuf_indent_add(out, 2);
330 prt_printf(out, "gen %u oldest_gen %u data_type ", a->gen, a->oldest_gen);
331 bch2_prt_data_type(out, a->data_type);
333 prt_printf(out, "journal_seq %llu", a->journal_seq);
335 prt_printf(out, "need_discard %llu", BCH_ALLOC_V4_NEED_DISCARD(a));
337 prt_printf(out, "need_inc_gen %llu", BCH_ALLOC_V4_NEED_INC_GEN(a));
339 prt_printf(out, "dirty_sectors %u", a->dirty_sectors);
341 prt_printf(out, "cached_sectors %u", a->cached_sectors);
343 prt_printf(out, "stripe %u", a->stripe);
345 prt_printf(out, "stripe_redundancy %u", a->stripe_redundancy);
347 prt_printf(out, "io_time[READ] %llu", a->io_time[READ]);
349 prt_printf(out, "io_time[WRITE] %llu", a->io_time[WRITE]);
351 prt_printf(out, "fragmentation %llu", a->fragmentation_lru);
353 prt_printf(out, "bp_start %llu", BCH_ALLOC_V4_BACKPOINTERS_START(a));
354 printbuf_indent_sub(out, 2);
357 void __bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
359 if (k.k->type == KEY_TYPE_alloc_v4) {
362 *out = *bkey_s_c_to_alloc_v4(k).v;
364 src = alloc_v4_backpointers(out);
365 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
366 dst = alloc_v4_backpointers(out);
369 memset(src, 0, dst - src);
371 SET_BCH_ALLOC_V4_NR_BACKPOINTERS(out, 0);
373 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
375 *out = (struct bch_alloc_v4) {
376 .journal_seq = u.journal_seq,
377 .flags = u.need_discard,
379 .oldest_gen = u.oldest_gen,
380 .data_type = u.data_type,
381 .stripe_redundancy = u.stripe_redundancy,
382 .dirty_sectors = u.dirty_sectors,
383 .cached_sectors = u.cached_sectors,
384 .io_time[READ] = u.read_time,
385 .io_time[WRITE] = u.write_time,
389 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
393 static noinline struct bkey_i_alloc_v4 *
394 __bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
396 struct bkey_i_alloc_v4 *ret;
398 ret = bch2_trans_kmalloc(trans, max(bkey_bytes(k.k), sizeof(struct bkey_i_alloc_v4)));
402 if (k.k->type == KEY_TYPE_alloc_v4) {
405 bkey_reassemble(&ret->k_i, k);
407 src = alloc_v4_backpointers(&ret->v);
408 SET_BCH_ALLOC_V4_BACKPOINTERS_START(&ret->v, BCH_ALLOC_V4_U64s);
409 dst = alloc_v4_backpointers(&ret->v);
412 memset(src, 0, dst - src);
414 SET_BCH_ALLOC_V4_NR_BACKPOINTERS(&ret->v, 0);
415 set_alloc_v4_u64s(ret);
417 bkey_alloc_v4_init(&ret->k_i);
419 bch2_alloc_to_v4(k, &ret->v);
424 static inline struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut_inlined(struct btree_trans *trans, struct bkey_s_c k)
426 struct bkey_s_c_alloc_v4 a;
428 if (likely(k.k->type == KEY_TYPE_alloc_v4) &&
429 ((a = bkey_s_c_to_alloc_v4(k), true) &&
430 BCH_ALLOC_V4_NR_BACKPOINTERS(a.v) == 0))
431 return bch2_bkey_make_mut_noupdate_typed(trans, k, alloc_v4);
433 return __bch2_alloc_to_v4_mut(trans, k);
436 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
438 return bch2_alloc_to_v4_mut_inlined(trans, k);
441 struct bkey_i_alloc_v4 *
442 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
446 struct bkey_i_alloc_v4 *a;
449 k = bch2_bkey_get_iter(trans, iter, BTREE_ID_alloc, pos,
450 BTREE_ITER_WITH_UPDATES|
457 a = bch2_alloc_to_v4_mut_inlined(trans, k);
458 ret = PTR_ERR_OR_ZERO(a);
463 bch2_trans_iter_exit(trans, iter);
467 static struct bpos alloc_gens_pos(struct bpos pos, unsigned *offset)
469 *offset = pos.offset & KEY_TYPE_BUCKET_GENS_MASK;
471 pos.offset >>= KEY_TYPE_BUCKET_GENS_BITS;
475 static struct bpos bucket_gens_pos_to_alloc(struct bpos pos, unsigned offset)
477 pos.offset <<= KEY_TYPE_BUCKET_GENS_BITS;
478 pos.offset += offset;
482 static unsigned alloc_gen(struct bkey_s_c k, unsigned offset)
484 return k.k->type == KEY_TYPE_bucket_gens
485 ? bkey_s_c_to_bucket_gens(k).v->gens[offset]
489 int bch2_bucket_gens_invalid(struct bch_fs *c, struct bkey_s_c k,
490 enum bkey_invalid_flags flags,
491 struct printbuf *err)
495 bkey_fsck_err_on(bkey_val_bytes(k.k) != sizeof(struct bch_bucket_gens), c, err,
496 bucket_gens_val_size_bad,
497 "bad val size (%zu != %zu)",
498 bkey_val_bytes(k.k), sizeof(struct bch_bucket_gens));
503 void bch2_bucket_gens_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
505 struct bkey_s_c_bucket_gens g = bkey_s_c_to_bucket_gens(k);
508 for (i = 0; i < ARRAY_SIZE(g.v->gens); i++) {
511 prt_printf(out, "%u", g.v->gens[i]);
515 int bch2_bucket_gens_init(struct bch_fs *c)
517 struct btree_trans *trans = bch2_trans_get(c);
518 struct bkey_i_bucket_gens g;
519 bool have_bucket_gens_key = false;
522 ret = for_each_btree_key(trans, iter, BTREE_ID_alloc, POS_MIN,
523 BTREE_ITER_PREFETCH, k, ({
525 * Not a fsck error because this is checked/repaired by
526 * bch2_check_alloc_key() which runs later:
528 if (!bch2_dev_bucket_exists(c, k.k->p))
531 struct bch_alloc_v4 a;
532 u8 gen = bch2_alloc_to_v4(k, &a)->gen;
534 struct bpos pos = alloc_gens_pos(iter.pos, &offset);
537 if (have_bucket_gens_key && bkey_cmp(iter.pos, pos)) {
538 ret2 = bch2_btree_insert_trans(trans, BTREE_ID_bucket_gens, &g.k_i, 0) ?:
539 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
542 have_bucket_gens_key = false;
545 if (!have_bucket_gens_key) {
546 bkey_bucket_gens_init(&g.k_i);
548 have_bucket_gens_key = true;
551 g.v.gens[offset] = gen;
556 if (have_bucket_gens_key && !ret)
557 ret = commit_do(trans, NULL, NULL,
558 BCH_TRANS_COMMIT_no_enospc,
559 bch2_btree_insert_trans(trans, BTREE_ID_bucket_gens, &g.k_i, 0));
561 bch2_trans_put(trans);
567 int bch2_alloc_read(struct bch_fs *c)
569 struct btree_trans *trans = bch2_trans_get(c);
572 down_read(&c->gc_lock);
574 if (c->sb.version_upgrade_complete >= bcachefs_metadata_version_bucket_gens) {
575 ret = for_each_btree_key(trans, iter, BTREE_ID_bucket_gens, POS_MIN,
576 BTREE_ITER_PREFETCH, k, ({
577 u64 start = bucket_gens_pos_to_alloc(k.k->p, 0).offset;
578 u64 end = bucket_gens_pos_to_alloc(bpos_nosnap_successor(k.k->p), 0).offset;
580 if (k.k->type != KEY_TYPE_bucket_gens)
583 const struct bch_bucket_gens *g = bkey_s_c_to_bucket_gens(k).v;
586 * Not a fsck error because this is checked/repaired by
587 * bch2_check_alloc_key() which runs later:
589 if (!bch2_dev_exists2(c, k.k->p.inode))
592 struct bch_dev *ca = bch_dev_bkey_exists(c, k.k->p.inode);
594 for (u64 b = max_t(u64, ca->mi.first_bucket, start);
595 b < min_t(u64, ca->mi.nbuckets, end);
597 *bucket_gen(ca, b) = g->gens[b & KEY_TYPE_BUCKET_GENS_MASK];
601 ret = for_each_btree_key(trans, iter, BTREE_ID_alloc, POS_MIN,
602 BTREE_ITER_PREFETCH, k, ({
604 * Not a fsck error because this is checked/repaired by
605 * bch2_check_alloc_key() which runs later:
607 if (!bch2_dev_bucket_exists(c, k.k->p))
610 struct bch_dev *ca = bch_dev_bkey_exists(c, k.k->p.inode);
612 struct bch_alloc_v4 a;
613 *bucket_gen(ca, k.k->p.offset) = bch2_alloc_to_v4(k, &a)->gen;
618 bch2_trans_put(trans);
619 up_read(&c->gc_lock);
625 /* Free space/discard btree: */
627 static int bch2_bucket_do_index(struct btree_trans *trans,
628 struct bkey_s_c alloc_k,
629 const struct bch_alloc_v4 *a,
632 struct bch_fs *c = trans->c;
633 struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
634 struct btree_iter iter;
638 enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
639 enum bch_bkey_type new_type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
640 struct printbuf buf = PRINTBUF;
643 if (a->data_type != BCH_DATA_free &&
644 a->data_type != BCH_DATA_need_discard)
647 k = bch2_trans_kmalloc_nomemzero(trans, sizeof(*k));
652 k->k.type = new_type;
654 switch (a->data_type) {
656 btree = BTREE_ID_freespace;
657 k->k.p = alloc_freespace_pos(alloc_k.k->p, *a);
658 bch2_key_resize(&k->k, 1);
660 case BCH_DATA_need_discard:
661 btree = BTREE_ID_need_discard;
662 k->k.p = alloc_k.k->p;
668 old = bch2_bkey_get_iter(trans, &iter, btree,
669 bkey_start_pos(&k->k),
675 if (ca->mi.freespace_initialized &&
676 c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info &&
677 bch2_trans_inconsistent_on(old.k->type != old_type, trans,
678 "incorrect key when %s %s:%llu:%llu:0 (got %s should be %s)\n"
680 set ? "setting" : "clearing",
681 bch2_btree_id_str(btree),
684 bch2_bkey_types[old.k->type],
685 bch2_bkey_types[old_type],
686 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
691 ret = bch2_trans_update(trans, &iter, k, 0);
693 bch2_trans_iter_exit(trans, &iter);
698 static noinline int bch2_bucket_gen_update(struct btree_trans *trans,
699 struct bpos bucket, u8 gen)
701 struct btree_iter iter;
703 struct bpos pos = alloc_gens_pos(bucket, &offset);
704 struct bkey_i_bucket_gens *g;
708 g = bch2_trans_kmalloc(trans, sizeof(*g));
709 ret = PTR_ERR_OR_ZERO(g);
713 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_bucket_gens, pos,
715 BTREE_ITER_WITH_UPDATES);
720 if (k.k->type != KEY_TYPE_bucket_gens) {
721 bkey_bucket_gens_init(&g->k_i);
724 bkey_reassemble(&g->k_i, k);
727 g->v.gens[offset] = gen;
729 ret = bch2_trans_update(trans, &iter, &g->k_i, 0);
730 bch2_trans_iter_exit(trans, &iter);
734 int bch2_trigger_alloc(struct btree_trans *trans,
735 enum btree_id btree, unsigned level,
736 struct bkey_s_c old, struct bkey_s new,
739 struct bch_fs *c = trans->c;
742 if (bch2_trans_inconsistent_on(!bch2_dev_bucket_exists(c, new.k->p), trans,
743 "alloc key for invalid device or bucket"))
746 struct bch_dev *ca = bch_dev_bkey_exists(c, new.k->p.inode);
748 struct bch_alloc_v4 old_a_convert;
749 const struct bch_alloc_v4 *old_a = bch2_alloc_to_v4(old, &old_a_convert);
751 if (flags & BTREE_TRIGGER_TRANSACTIONAL) {
752 struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
754 new_a->data_type = alloc_data_type(*new_a, new_a->data_type);
756 if (bch2_bucket_sectors(*new_a) > bch2_bucket_sectors(*old_a)) {
757 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
758 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
759 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
760 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
763 if (data_type_is_empty(new_a->data_type) &&
764 BCH_ALLOC_V4_NEED_INC_GEN(new_a) &&
765 !bch2_bucket_is_open_safe(c, new.k->p.inode, new.k->p.offset)) {
767 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
770 if (old_a->data_type != new_a->data_type ||
771 (new_a->data_type == BCH_DATA_free &&
772 alloc_freespace_genbits(*old_a) != alloc_freespace_genbits(*new_a))) {
773 ret = bch2_bucket_do_index(trans, old, old_a, false) ?:
774 bch2_bucket_do_index(trans, new.s_c, new_a, true);
779 if (new_a->data_type == BCH_DATA_cached &&
780 !new_a->io_time[READ])
781 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
783 u64 old_lru = alloc_lru_idx_read(*old_a);
784 u64 new_lru = alloc_lru_idx_read(*new_a);
785 if (old_lru != new_lru) {
786 ret = bch2_lru_change(trans, new.k->p.inode,
787 bucket_to_u64(new.k->p),
793 new_a->fragmentation_lru = alloc_lru_idx_fragmentation(*new_a,
794 bch_dev_bkey_exists(c, new.k->p.inode));
795 if (old_a->fragmentation_lru != new_a->fragmentation_lru) {
796 ret = bch2_lru_change(trans,
797 BCH_LRU_FRAGMENTATION_START,
798 bucket_to_u64(new.k->p),
799 old_a->fragmentation_lru, new_a->fragmentation_lru);
804 if (old_a->gen != new_a->gen) {
805 ret = bch2_bucket_gen_update(trans, new.k->p, new_a->gen);
811 * need to know if we're getting called from the invalidate path or
815 if ((flags & BTREE_TRIGGER_BUCKET_INVALIDATE) &&
816 old_a->cached_sectors) {
817 ret = bch2_update_cached_sectors_list(trans, new.k->p.inode,
818 -((s64) old_a->cached_sectors));
824 if ((flags & BTREE_TRIGGER_ATOMIC) && (flags & BTREE_TRIGGER_INSERT)) {
825 struct bch_alloc_v4 *new_a = bkey_s_to_alloc_v4(new).v;
826 u64 journal_seq = trans->journal_res.seq;
827 u64 bucket_journal_seq = new_a->journal_seq;
829 if ((flags & BTREE_TRIGGER_INSERT) &&
830 data_type_is_empty(old_a->data_type) !=
831 data_type_is_empty(new_a->data_type) &&
832 new.k->type == KEY_TYPE_alloc_v4) {
833 struct bch_alloc_v4 *v = bkey_s_to_alloc_v4(new).v;
836 * If the btree updates referring to a bucket weren't flushed
837 * before the bucket became empty again, then the we don't have
838 * to wait on a journal flush before we can reuse the bucket:
840 v->journal_seq = bucket_journal_seq =
841 data_type_is_empty(new_a->data_type) &&
842 (journal_seq == v->journal_seq ||
843 bch2_journal_noflush_seq(&c->journal, v->journal_seq))
847 if (!data_type_is_empty(old_a->data_type) &&
848 data_type_is_empty(new_a->data_type) &&
849 bucket_journal_seq) {
850 ret = bch2_set_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
851 c->journal.flushed_seq_ondisk,
852 new.k->p.inode, new.k->p.offset,
855 bch2_fs_fatal_error(c,
856 "setting bucket_needs_journal_commit: %s", bch2_err_str(ret));
861 percpu_down_read(&c->mark_lock);
862 if (new_a->gen != old_a->gen)
863 *bucket_gen(ca, new.k->p.offset) = new_a->gen;
865 bch2_dev_usage_update(c, ca, old_a, new_a, journal_seq, false);
866 percpu_up_read(&c->mark_lock);
868 #define eval_state(_a, expr) ({ const struct bch_alloc_v4 *a = _a; expr; })
869 #define statechange(expr) !eval_state(old_a, expr) && eval_state(new_a, expr)
870 #define bucket_flushed(a) (!a->journal_seq || a->journal_seq <= c->journal.flushed_seq_ondisk)
872 if (statechange(a->data_type == BCH_DATA_free) &&
873 bucket_flushed(new_a))
874 closure_wake_up(&c->freelist_wait);
876 if (statechange(a->data_type == BCH_DATA_need_discard) &&
877 !bch2_bucket_is_open(c, new.k->p.inode, new.k->p.offset) &&
878 bucket_flushed(new_a))
879 bch2_discard_one_bucket_fast(c, new.k->p);
881 if (statechange(a->data_type == BCH_DATA_cached) &&
882 !bch2_bucket_is_open(c, new.k->p.inode, new.k->p.offset) &&
883 should_invalidate_buckets(ca, bch2_dev_usage_read(ca)))
884 bch2_do_invalidates(c);
886 if (statechange(a->data_type == BCH_DATA_need_gc_gens))
890 if ((flags & BTREE_TRIGGER_GC) &&
891 (flags & BTREE_TRIGGER_BUCKET_INVALIDATE)) {
892 struct bch_alloc_v4 new_a_convert;
893 const struct bch_alloc_v4 *new_a = bch2_alloc_to_v4(new.s_c, &new_a_convert);
895 percpu_down_read(&c->mark_lock);
896 struct bucket *g = gc_bucket(ca, new.k->p.offset);
902 g->data_type = new_a->data_type;
903 g->stripe = new_a->stripe;
904 g->stripe_redundancy = new_a->stripe_redundancy;
905 g->dirty_sectors = new_a->dirty_sectors;
906 g->cached_sectors = new_a->cached_sectors;
909 percpu_up_read(&c->mark_lock);
916 * This synthesizes deleted extents for holes, similar to BTREE_ITER_SLOTS for
917 * extents style btrees, but works on non-extents btrees:
919 static struct bkey_s_c bch2_get_key_or_hole(struct btree_iter *iter, struct bpos end, struct bkey *hole)
921 struct bkey_s_c k = bch2_btree_iter_peek_slot(iter);
929 struct btree_iter iter2;
932 bch2_trans_copy_iter(&iter2, iter);
934 struct btree_path *path = btree_iter_path(iter->trans, iter);
935 if (!bpos_eq(path->l[0].b->key.k.p, SPOS_MAX))
936 end = bkey_min(end, bpos_nosnap_successor(path->l[0].b->key.k.p));
938 end = bkey_min(end, POS(iter->pos.inode, iter->pos.offset + U32_MAX - 1));
941 * btree node min/max is a closed interval, upto takes a half
944 k = bch2_btree_iter_peek_upto(&iter2, end);
946 bch2_trans_iter_exit(iter->trans, &iter2);
948 BUG_ON(next.offset >= iter->pos.offset + U32_MAX);
956 bch2_key_resize(hole, next.offset - iter->pos.offset);
957 return (struct bkey_s_c) { hole, NULL };
961 static bool next_bucket(struct bch_fs *c, struct bpos *bucket)
965 if (bch2_dev_bucket_exists(c, *bucket))
968 if (bch2_dev_exists2(c, bucket->inode)) {
969 ca = bch_dev_bkey_exists(c, bucket->inode);
971 if (bucket->offset < ca->mi.first_bucket) {
972 bucket->offset = ca->mi.first_bucket;
981 ca = __bch2_next_dev_idx(c, bucket->inode, NULL);
983 *bucket = POS(ca->dev_idx, ca->mi.first_bucket);
989 static struct bkey_s_c bch2_get_key_or_real_bucket_hole(struct btree_iter *iter, struct bkey *hole)
991 struct bch_fs *c = iter->trans->c;
994 k = bch2_get_key_or_hole(iter, POS_MAX, hole);
999 struct bpos bucket = bkey_start_pos(k.k);
1001 if (!bch2_dev_bucket_exists(c, bucket)) {
1002 if (!next_bucket(c, &bucket))
1003 return bkey_s_c_null;
1005 bch2_btree_iter_set_pos(iter, bucket);
1009 if (!bch2_dev_bucket_exists(c, k.k->p)) {
1010 struct bch_dev *ca = bch_dev_bkey_exists(c, bucket.inode);
1012 bch2_key_resize(hole, ca->mi.nbuckets - bucket.offset);
1019 static noinline_for_stack
1020 int bch2_check_alloc_key(struct btree_trans *trans,
1021 struct bkey_s_c alloc_k,
1022 struct btree_iter *alloc_iter,
1023 struct btree_iter *discard_iter,
1024 struct btree_iter *freespace_iter,
1025 struct btree_iter *bucket_gens_iter)
1027 struct bch_fs *c = trans->c;
1029 struct bch_alloc_v4 a_convert;
1030 const struct bch_alloc_v4 *a;
1031 unsigned discard_key_type, freespace_key_type;
1032 unsigned gens_offset;
1034 struct printbuf buf = PRINTBUF;
1037 if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_k.k->p), c,
1038 alloc_key_to_missing_dev_bucket,
1039 "alloc key for invalid device:bucket %llu:%llu",
1040 alloc_k.k->p.inode, alloc_k.k->p.offset))
1041 return bch2_btree_delete_at(trans, alloc_iter, 0);
1043 ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
1044 if (!ca->mi.freespace_initialized)
1047 a = bch2_alloc_to_v4(alloc_k, &a_convert);
1049 discard_key_type = a->data_type == BCH_DATA_need_discard ? KEY_TYPE_set : 0;
1050 bch2_btree_iter_set_pos(discard_iter, alloc_k.k->p);
1051 k = bch2_btree_iter_peek_slot(discard_iter);
1056 if (fsck_err_on(k.k->type != discard_key_type,
1057 c, need_discard_key_wrong,
1058 "incorrect key in need_discard btree (got %s should be %s)\n"
1060 bch2_bkey_types[k.k->type],
1061 bch2_bkey_types[discard_key_type],
1062 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
1063 struct bkey_i *update =
1064 bch2_trans_kmalloc(trans, sizeof(*update));
1066 ret = PTR_ERR_OR_ZERO(update);
1070 bkey_init(&update->k);
1071 update->k.type = discard_key_type;
1072 update->k.p = discard_iter->pos;
1074 ret = bch2_trans_update(trans, discard_iter, update, 0);
1079 freespace_key_type = a->data_type == BCH_DATA_free ? KEY_TYPE_set : 0;
1080 bch2_btree_iter_set_pos(freespace_iter, alloc_freespace_pos(alloc_k.k->p, *a));
1081 k = bch2_btree_iter_peek_slot(freespace_iter);
1086 if (fsck_err_on(k.k->type != freespace_key_type,
1087 c, freespace_key_wrong,
1088 "incorrect key in freespace btree (got %s should be %s)\n"
1090 bch2_bkey_types[k.k->type],
1091 bch2_bkey_types[freespace_key_type],
1092 (printbuf_reset(&buf),
1093 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
1094 struct bkey_i *update =
1095 bch2_trans_kmalloc(trans, sizeof(*update));
1097 ret = PTR_ERR_OR_ZERO(update);
1101 bkey_init(&update->k);
1102 update->k.type = freespace_key_type;
1103 update->k.p = freespace_iter->pos;
1104 bch2_key_resize(&update->k, 1);
1106 ret = bch2_trans_update(trans, freespace_iter, update, 0);
1111 bch2_btree_iter_set_pos(bucket_gens_iter, alloc_gens_pos(alloc_k.k->p, &gens_offset));
1112 k = bch2_btree_iter_peek_slot(bucket_gens_iter);
1117 if (fsck_err_on(a->gen != alloc_gen(k, gens_offset),
1118 c, bucket_gens_key_wrong,
1119 "incorrect gen in bucket_gens btree (got %u should be %u)\n"
1121 alloc_gen(k, gens_offset), a->gen,
1122 (printbuf_reset(&buf),
1123 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
1124 struct bkey_i_bucket_gens *g =
1125 bch2_trans_kmalloc(trans, sizeof(*g));
1127 ret = PTR_ERR_OR_ZERO(g);
1131 if (k.k->type == KEY_TYPE_bucket_gens) {
1132 bkey_reassemble(&g->k_i, k);
1134 bkey_bucket_gens_init(&g->k_i);
1135 g->k.p = alloc_gens_pos(alloc_k.k->p, &gens_offset);
1138 g->v.gens[gens_offset] = a->gen;
1140 ret = bch2_trans_update(trans, bucket_gens_iter, &g->k_i, 0);
1146 printbuf_exit(&buf);
1150 static noinline_for_stack
1151 int bch2_check_alloc_hole_freespace(struct btree_trans *trans,
1154 struct btree_iter *freespace_iter)
1156 struct bch_fs *c = trans->c;
1159 struct printbuf buf = PRINTBUF;
1162 ca = bch_dev_bkey_exists(c, start.inode);
1163 if (!ca->mi.freespace_initialized)
1166 bch2_btree_iter_set_pos(freespace_iter, start);
1168 k = bch2_btree_iter_peek_slot(freespace_iter);
1173 *end = bkey_min(k.k->p, *end);
1175 if (fsck_err_on(k.k->type != KEY_TYPE_set,
1176 c, freespace_hole_missing,
1177 "hole in alloc btree missing in freespace btree\n"
1178 " device %llu buckets %llu-%llu",
1179 freespace_iter->pos.inode,
1180 freespace_iter->pos.offset,
1182 struct bkey_i *update =
1183 bch2_trans_kmalloc(trans, sizeof(*update));
1185 ret = PTR_ERR_OR_ZERO(update);
1189 bkey_init(&update->k);
1190 update->k.type = KEY_TYPE_set;
1191 update->k.p = freespace_iter->pos;
1192 bch2_key_resize(&update->k,
1193 min_t(u64, U32_MAX, end->offset -
1194 freespace_iter->pos.offset));
1196 ret = bch2_trans_update(trans, freespace_iter, update, 0);
1202 printbuf_exit(&buf);
1206 static noinline_for_stack
1207 int bch2_check_alloc_hole_bucket_gens(struct btree_trans *trans,
1210 struct btree_iter *bucket_gens_iter)
1212 struct bch_fs *c = trans->c;
1214 struct printbuf buf = PRINTBUF;
1215 unsigned i, gens_offset, gens_end_offset;
1218 bch2_btree_iter_set_pos(bucket_gens_iter, alloc_gens_pos(start, &gens_offset));
1220 k = bch2_btree_iter_peek_slot(bucket_gens_iter);
1225 if (bkey_cmp(alloc_gens_pos(start, &gens_offset),
1226 alloc_gens_pos(*end, &gens_end_offset)))
1227 gens_end_offset = KEY_TYPE_BUCKET_GENS_NR;
1229 if (k.k->type == KEY_TYPE_bucket_gens) {
1230 struct bkey_i_bucket_gens g;
1231 bool need_update = false;
1233 bkey_reassemble(&g.k_i, k);
1235 for (i = gens_offset; i < gens_end_offset; i++) {
1236 if (fsck_err_on(g.v.gens[i], c,
1237 bucket_gens_hole_wrong,
1238 "hole in alloc btree at %llu:%llu with nonzero gen in bucket_gens btree (%u)",
1239 bucket_gens_pos_to_alloc(k.k->p, i).inode,
1240 bucket_gens_pos_to_alloc(k.k->p, i).offset,
1248 struct bkey_i *u = bch2_trans_kmalloc(trans, sizeof(g));
1250 ret = PTR_ERR_OR_ZERO(u);
1254 memcpy(u, &g, sizeof(g));
1256 ret = bch2_trans_update(trans, bucket_gens_iter, u, 0);
1262 *end = bkey_min(*end, bucket_gens_pos_to_alloc(bpos_nosnap_successor(k.k->p), 0));
1265 printbuf_exit(&buf);
1269 static noinline_for_stack int bch2_check_discard_freespace_key(struct btree_trans *trans,
1270 struct btree_iter *iter)
1272 struct bch_fs *c = trans->c;
1273 struct btree_iter alloc_iter;
1274 struct bkey_s_c alloc_k;
1275 struct bch_alloc_v4 a_convert;
1276 const struct bch_alloc_v4 *a;
1279 enum bch_data_type state = iter->btree_id == BTREE_ID_need_discard
1280 ? BCH_DATA_need_discard
1282 struct printbuf buf = PRINTBUF;
1286 pos.offset &= ~(~0ULL << 56);
1287 genbits = iter->pos.offset & (~0ULL << 56);
1289 alloc_k = bch2_bkey_get_iter(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
1290 ret = bkey_err(alloc_k);
1294 if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
1295 need_discard_freespace_key_to_invalid_dev_bucket,
1296 "entry in %s btree for nonexistant dev:bucket %llu:%llu",
1297 bch2_btree_id_str(iter->btree_id), pos.inode, pos.offset))
1300 a = bch2_alloc_to_v4(alloc_k, &a_convert);
1302 if (fsck_err_on(a->data_type != state ||
1303 (state == BCH_DATA_free &&
1304 genbits != alloc_freespace_genbits(*a)), c,
1305 need_discard_freespace_key_bad,
1306 "%s\n incorrectly set at %s:%llu:%llu:0 (free %u, genbits %llu should be %llu)",
1307 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
1308 bch2_btree_id_str(iter->btree_id),
1311 a->data_type == state,
1312 genbits >> 56, alloc_freespace_genbits(*a) >> 56))
1316 set_btree_iter_dontneed(&alloc_iter);
1317 bch2_trans_iter_exit(trans, &alloc_iter);
1318 printbuf_exit(&buf);
1321 ret = bch2_btree_delete_extent_at(trans, iter,
1322 iter->btree_id == BTREE_ID_freespace ? 1 : 0, 0) ?:
1323 bch2_trans_commit(trans, NULL, NULL,
1324 BCH_TRANS_COMMIT_no_enospc);
1329 * We've already checked that generation numbers in the bucket_gens btree are
1330 * valid for buckets that exist; this just checks for keys for nonexistent
1333 static noinline_for_stack
1334 int bch2_check_bucket_gens_key(struct btree_trans *trans,
1335 struct btree_iter *iter,
1338 struct bch_fs *c = trans->c;
1339 struct bkey_i_bucket_gens g;
1341 u64 start = bucket_gens_pos_to_alloc(k.k->p, 0).offset;
1342 u64 end = bucket_gens_pos_to_alloc(bpos_nosnap_successor(k.k->p), 0).offset;
1344 bool need_update = false, dev_exists;
1345 struct printbuf buf = PRINTBUF;
1348 BUG_ON(k.k->type != KEY_TYPE_bucket_gens);
1349 bkey_reassemble(&g.k_i, k);
1351 /* if no bch_dev, skip out whether we repair or not */
1352 dev_exists = bch2_dev_exists2(c, k.k->p.inode);
1354 if (fsck_err_on(!dev_exists, c,
1355 bucket_gens_to_invalid_dev,
1356 "bucket_gens key for invalid device:\n %s",
1357 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1358 ret = bch2_btree_delete_at(trans, iter, 0);
1363 ca = bch_dev_bkey_exists(c, k.k->p.inode);
1364 if (fsck_err_on(end <= ca->mi.first_bucket ||
1365 start >= ca->mi.nbuckets, c,
1366 bucket_gens_to_invalid_buckets,
1367 "bucket_gens key for invalid buckets:\n %s",
1368 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1369 ret = bch2_btree_delete_at(trans, iter, 0);
1373 for (b = start; b < ca->mi.first_bucket; b++)
1374 if (fsck_err_on(g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK], c,
1375 bucket_gens_nonzero_for_invalid_buckets,
1376 "bucket_gens key has nonzero gen for invalid bucket")) {
1377 g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK] = 0;
1381 for (b = ca->mi.nbuckets; b < end; b++)
1382 if (fsck_err_on(g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK], c,
1383 bucket_gens_nonzero_for_invalid_buckets,
1384 "bucket_gens key has nonzero gen for invalid bucket")) {
1385 g.v.gens[b & KEY_TYPE_BUCKET_GENS_MASK] = 0;
1390 struct bkey_i *u = bch2_trans_kmalloc(trans, sizeof(g));
1392 ret = PTR_ERR_OR_ZERO(u);
1396 memcpy(u, &g, sizeof(g));
1397 ret = bch2_trans_update(trans, iter, u, 0);
1401 printbuf_exit(&buf);
1405 int bch2_check_alloc_info(struct bch_fs *c)
1407 struct btree_trans *trans = bch2_trans_get(c);
1408 struct btree_iter iter, discard_iter, freespace_iter, bucket_gens_iter;
1413 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS_MIN,
1414 BTREE_ITER_PREFETCH);
1415 bch2_trans_iter_init(trans, &discard_iter, BTREE_ID_need_discard, POS_MIN,
1416 BTREE_ITER_PREFETCH);
1417 bch2_trans_iter_init(trans, &freespace_iter, BTREE_ID_freespace, POS_MIN,
1418 BTREE_ITER_PREFETCH);
1419 bch2_trans_iter_init(trans, &bucket_gens_iter, BTREE_ID_bucket_gens, POS_MIN,
1420 BTREE_ITER_PREFETCH);
1425 bch2_trans_begin(trans);
1427 k = bch2_get_key_or_real_bucket_hole(&iter, &hole);
1436 next = bpos_nosnap_successor(k.k->p);
1438 ret = bch2_check_alloc_key(trans,
1448 ret = bch2_check_alloc_hole_freespace(trans,
1449 bkey_start_pos(k.k),
1452 bch2_check_alloc_hole_bucket_gens(trans,
1453 bkey_start_pos(k.k),
1460 ret = bch2_trans_commit(trans, NULL, NULL,
1461 BCH_TRANS_COMMIT_no_enospc);
1465 bch2_btree_iter_set_pos(&iter, next);
1467 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1472 bch2_trans_iter_exit(trans, &bucket_gens_iter);
1473 bch2_trans_iter_exit(trans, &freespace_iter);
1474 bch2_trans_iter_exit(trans, &discard_iter);
1475 bch2_trans_iter_exit(trans, &iter);
1480 ret = for_each_btree_key(trans, iter,
1481 BTREE_ID_need_discard, POS_MIN,
1482 BTREE_ITER_PREFETCH, k,
1483 bch2_check_discard_freespace_key(trans, &iter));
1487 bch2_trans_iter_init(trans, &iter, BTREE_ID_freespace, POS_MIN,
1488 BTREE_ITER_PREFETCH);
1490 bch2_trans_begin(trans);
1491 k = bch2_btree_iter_peek(&iter);
1495 ret = bkey_err(k) ?:
1496 bch2_check_discard_freespace_key(trans, &iter);
1497 if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
1502 struct printbuf buf = PRINTBUF;
1503 bch2_bkey_val_to_text(&buf, c, k);
1505 bch_err(c, "while checking %s", buf.buf);
1506 printbuf_exit(&buf);
1510 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
1512 bch2_trans_iter_exit(trans, &iter);
1516 ret = for_each_btree_key_commit(trans, iter,
1517 BTREE_ID_bucket_gens, POS_MIN,
1518 BTREE_ITER_PREFETCH, k,
1519 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1520 bch2_check_bucket_gens_key(trans, &iter, k));
1522 bch2_trans_put(trans);
1527 static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
1528 struct btree_iter *alloc_iter)
1530 struct bch_fs *c = trans->c;
1531 struct btree_iter lru_iter;
1532 struct bch_alloc_v4 a_convert;
1533 const struct bch_alloc_v4 *a;
1534 struct bkey_s_c alloc_k, lru_k;
1535 struct printbuf buf = PRINTBUF;
1538 alloc_k = bch2_btree_iter_peek(alloc_iter);
1542 ret = bkey_err(alloc_k);
1546 a = bch2_alloc_to_v4(alloc_k, &a_convert);
1548 if (a->data_type != BCH_DATA_cached)
1551 if (fsck_err_on(!a->io_time[READ], c,
1552 alloc_key_cached_but_read_time_zero,
1553 "cached bucket with read_time 0\n"
1555 (printbuf_reset(&buf),
1556 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
1557 struct bkey_i_alloc_v4 *a_mut =
1558 bch2_alloc_to_v4_mut(trans, alloc_k);
1559 ret = PTR_ERR_OR_ZERO(a_mut);
1563 a_mut->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1564 ret = bch2_trans_update(trans, alloc_iter,
1565 &a_mut->k_i, BTREE_TRIGGER_NORUN);
1572 lru_k = bch2_bkey_get_iter(trans, &lru_iter, BTREE_ID_lru,
1573 lru_pos(alloc_k.k->p.inode,
1574 bucket_to_u64(alloc_k.k->p),
1575 a->io_time[READ]), 0);
1576 ret = bkey_err(lru_k);
1580 if (fsck_err_on(lru_k.k->type != KEY_TYPE_set, c,
1581 alloc_key_to_missing_lru_entry,
1582 "missing lru entry\n"
1584 (printbuf_reset(&buf),
1585 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
1586 ret = bch2_lru_set(trans,
1588 bucket_to_u64(alloc_k.k->p),
1595 bch2_trans_iter_exit(trans, &lru_iter);
1596 printbuf_exit(&buf);
1600 int bch2_check_alloc_to_lru_refs(struct bch_fs *c)
1602 int ret = bch2_trans_run(c,
1603 for_each_btree_key_commit(trans, iter, BTREE_ID_alloc,
1604 POS_MIN, BTREE_ITER_PREFETCH, k,
1605 NULL, NULL, BCH_TRANS_COMMIT_no_enospc,
1606 bch2_check_alloc_to_lru_ref(trans, &iter)));
1611 static int discard_in_flight_add(struct bch_fs *c, struct bpos bucket)
1615 mutex_lock(&c->discard_buckets_in_flight_lock);
1616 darray_for_each(c->discard_buckets_in_flight, i)
1617 if (bkey_eq(*i, bucket)) {
1622 ret = darray_push(&c->discard_buckets_in_flight, bucket);
1624 mutex_unlock(&c->discard_buckets_in_flight_lock);
1628 static void discard_in_flight_remove(struct bch_fs *c, struct bpos bucket)
1630 mutex_lock(&c->discard_buckets_in_flight_lock);
1631 darray_for_each(c->discard_buckets_in_flight, i)
1632 if (bkey_eq(*i, bucket)) {
1633 darray_remove_item(&c->discard_buckets_in_flight, i);
1638 mutex_unlock(&c->discard_buckets_in_flight_lock);
1641 struct discard_buckets_state {
1644 u64 need_journal_commit;
1647 u64 need_journal_commit_this_dev;
1650 static void discard_buckets_next_dev(struct bch_fs *c, struct discard_buckets_state *s, struct bch_dev *ca)
1655 if (s->ca && s->need_journal_commit_this_dev >
1656 bch2_dev_usage_read(s->ca).d[BCH_DATA_free].buckets)
1657 bch2_journal_flush_async(&c->journal, NULL);
1660 percpu_ref_put(&s->ca->ref);
1662 percpu_ref_get(&ca->ref);
1664 s->need_journal_commit_this_dev = 0;
1667 static int bch2_discard_one_bucket(struct btree_trans *trans,
1668 struct btree_iter *need_discard_iter,
1669 struct bpos *discard_pos_done,
1670 struct discard_buckets_state *s)
1672 struct bch_fs *c = trans->c;
1673 struct bpos pos = need_discard_iter->pos;
1674 struct btree_iter iter = { NULL };
1677 struct bkey_i_alloc_v4 *a;
1678 struct printbuf buf = PRINTBUF;
1679 bool discard_locked = false;
1682 ca = bch_dev_bkey_exists(c, pos.inode);
1684 if (!percpu_ref_tryget(&ca->io_ref)) {
1685 bch2_btree_iter_set_pos(need_discard_iter, POS(pos.inode + 1, 0));
1689 discard_buckets_next_dev(c, s, ca);
1691 if (bch2_bucket_is_open_safe(c, pos.inode, pos.offset)) {
1696 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
1697 c->journal.flushed_seq_ondisk,
1698 pos.inode, pos.offset)) {
1699 s->need_journal_commit++;
1700 s->need_journal_commit_this_dev++;
1704 k = bch2_bkey_get_iter(trans, &iter, BTREE_ID_alloc,
1705 need_discard_iter->pos,
1711 a = bch2_alloc_to_v4_mut(trans, k);
1712 ret = PTR_ERR_OR_ZERO(a);
1716 if (a->v.dirty_sectors) {
1717 if (bch2_trans_inconsistent_on(c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info,
1718 trans, "attempting to discard bucket with dirty data\n%s",
1719 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1724 if (a->v.data_type != BCH_DATA_need_discard) {
1725 if (data_type_is_empty(a->v.data_type) &&
1726 BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
1728 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1732 if (bch2_trans_inconsistent_on(c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info,
1733 trans, "bucket incorrectly set in need_discard btree\n"
1735 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1740 if (a->v.journal_seq > c->journal.flushed_seq_ondisk) {
1741 if (bch2_trans_inconsistent_on(c->curr_recovery_pass > BCH_RECOVERY_PASS_check_alloc_info,
1742 trans, "clearing need_discard but journal_seq %llu > flushed_seq %llu\n%s",
1744 c->journal.flushed_seq_ondisk,
1745 (bch2_bkey_val_to_text(&buf, c, k), buf.buf)))
1750 if (discard_in_flight_add(c, SPOS(iter.pos.inode, iter.pos.offset, true)))
1753 discard_locked = true;
1755 if (!bkey_eq(*discard_pos_done, iter.pos) &&
1756 ca->mi.discard && !c->opts.nochanges) {
1758 * This works without any other locks because this is the only
1759 * thread that removes items from the need_discard tree
1761 bch2_trans_unlock_long(trans);
1762 blkdev_issue_discard(ca->disk_sb.bdev,
1763 k.k->p.offset * ca->mi.bucket_size,
1766 *discard_pos_done = iter.pos;
1768 ret = bch2_trans_relock_notrace(trans);
1773 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
1774 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
1776 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1777 bch2_trans_commit(trans, NULL, NULL,
1778 BCH_WATERMARK_btree|
1779 BCH_TRANS_COMMIT_no_enospc);
1783 count_event(c, bucket_discard);
1787 discard_in_flight_remove(c, iter.pos);
1789 bch2_trans_iter_exit(trans, &iter);
1790 percpu_ref_put(&ca->io_ref);
1791 printbuf_exit(&buf);
1795 static void bch2_do_discards_work(struct work_struct *work)
1797 struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
1798 struct discard_buckets_state s = {};
1799 struct bpos discard_pos_done = POS_MAX;
1803 * We're doing the commit in bch2_discard_one_bucket instead of using
1804 * for_each_btree_key_commit() so that we can increment counters after
1805 * successful commit:
1807 ret = bch2_trans_run(c,
1808 for_each_btree_key(trans, iter,
1809 BTREE_ID_need_discard, POS_MIN, 0, k,
1810 bch2_discard_one_bucket(trans, &iter, &discard_pos_done, &s)));
1812 discard_buckets_next_dev(c, &s, NULL);
1814 trace_discard_buckets(c, s.seen, s.open, s.need_journal_commit, s.discarded,
1817 bch2_write_ref_put(c, BCH_WRITE_REF_discard);
1820 void bch2_do_discards(struct bch_fs *c)
1822 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_discard) &&
1823 !queue_work(c->write_ref_wq, &c->discard_work))
1824 bch2_write_ref_put(c, BCH_WRITE_REF_discard);
1827 static int bch2_clear_bucket_needs_discard(struct btree_trans *trans, struct bpos bucket)
1829 struct btree_iter iter;
1830 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, bucket, BTREE_ITER_INTENT);
1831 struct bkey_s_c k = bch2_btree_iter_peek_slot(&iter);
1832 int ret = bkey_err(k);
1836 struct bkey_i_alloc_v4 *a = bch2_alloc_to_v4_mut(trans, k);
1837 ret = PTR_ERR_OR_ZERO(a);
1841 BUG_ON(a->v.dirty_sectors);
1842 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
1843 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
1845 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1847 bch2_trans_iter_exit(trans, &iter);
1851 static void bch2_do_discards_fast_work(struct work_struct *work)
1853 struct bch_fs *c = container_of(work, struct bch_fs, discard_fast_work);
1856 bool got_bucket = false;
1860 mutex_lock(&c->discard_buckets_in_flight_lock);
1861 darray_for_each(c->discard_buckets_in_flight, i) {
1865 ca = bch_dev_bkey_exists(c, i->inode);
1867 if (!percpu_ref_tryget(&ca->io_ref)) {
1868 darray_remove_item(&c->discard_buckets_in_flight, i);
1877 mutex_unlock(&c->discard_buckets_in_flight_lock);
1882 if (ca->mi.discard && !c->opts.nochanges)
1883 blkdev_issue_discard(ca->disk_sb.bdev,
1884 bucket.offset * ca->mi.bucket_size,
1888 int ret = bch2_trans_do(c, NULL, NULL,
1889 BCH_WATERMARK_btree|
1890 BCH_TRANS_COMMIT_no_enospc,
1891 bch2_clear_bucket_needs_discard(trans, bucket));
1894 percpu_ref_put(&ca->io_ref);
1895 discard_in_flight_remove(c, bucket);
1901 bch2_write_ref_put(c, BCH_WRITE_REF_discard_fast);
1904 static void bch2_discard_one_bucket_fast(struct bch_fs *c, struct bpos bucket)
1906 struct bch_dev *ca = bch_dev_bkey_exists(c, bucket.inode);
1908 if (!percpu_ref_is_dying(&ca->io_ref) &&
1909 !discard_in_flight_add(c, bucket) &&
1910 bch2_write_ref_tryget(c, BCH_WRITE_REF_discard_fast) &&
1911 !queue_work(c->write_ref_wq, &c->discard_fast_work))
1912 bch2_write_ref_put(c, BCH_WRITE_REF_discard_fast);
1915 static int invalidate_one_bucket(struct btree_trans *trans,
1916 struct btree_iter *lru_iter,
1917 struct bkey_s_c lru_k,
1918 s64 *nr_to_invalidate)
1920 struct bch_fs *c = trans->c;
1921 struct btree_iter alloc_iter = { NULL };
1922 struct bkey_i_alloc_v4 *a = NULL;
1923 struct printbuf buf = PRINTBUF;
1924 struct bpos bucket = u64_to_bucket(lru_k.k->p.offset);
1925 unsigned cached_sectors;
1928 if (*nr_to_invalidate <= 0)
1931 if (!bch2_dev_bucket_exists(c, bucket)) {
1932 prt_str(&buf, "lru entry points to invalid bucket");
1936 if (bch2_bucket_is_open_safe(c, bucket.inode, bucket.offset))
1939 a = bch2_trans_start_alloc_update(trans, &alloc_iter, bucket);
1940 ret = PTR_ERR_OR_ZERO(a);
1944 /* We expect harmless races here due to the btree write buffer: */
1945 if (lru_pos_time(lru_iter->pos) != alloc_lru_idx_read(a->v))
1948 BUG_ON(a->v.data_type != BCH_DATA_cached);
1949 BUG_ON(a->v.dirty_sectors);
1951 if (!a->v.cached_sectors)
1952 bch_err(c, "invalidating empty bucket, confused");
1954 cached_sectors = a->v.cached_sectors;
1956 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1959 a->v.dirty_sectors = 0;
1960 a->v.cached_sectors = 0;
1961 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1962 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1964 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1965 BTREE_TRIGGER_BUCKET_INVALIDATE) ?:
1966 bch2_trans_commit(trans, NULL, NULL,
1967 BCH_WATERMARK_btree|
1968 BCH_TRANS_COMMIT_no_enospc);
1972 trace_and_count(c, bucket_invalidate, c, bucket.inode, bucket.offset, cached_sectors);
1973 --*nr_to_invalidate;
1975 bch2_trans_iter_exit(trans, &alloc_iter);
1976 printbuf_exit(&buf);
1979 prt_str(&buf, "\n lru key: ");
1980 bch2_bkey_val_to_text(&buf, c, lru_k);
1982 prt_str(&buf, "\n lru entry: ");
1983 bch2_lru_pos_to_text(&buf, lru_iter->pos);
1985 prt_str(&buf, "\n alloc key: ");
1987 bch2_bpos_to_text(&buf, bucket);
1989 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&a->k_i));
1991 bch_err(c, "%s", buf.buf);
1992 if (c->curr_recovery_pass > BCH_RECOVERY_PASS_check_lrus) {
1993 bch2_inconsistent_error(c);
2000 static void bch2_do_invalidates_work(struct work_struct *work)
2002 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
2003 struct btree_trans *trans = bch2_trans_get(c);
2006 ret = bch2_btree_write_buffer_tryflush(trans);
2010 for_each_member_device(c, ca) {
2011 s64 nr_to_invalidate =
2012 should_invalidate_buckets(ca, bch2_dev_usage_read(ca));
2014 ret = for_each_btree_key_upto(trans, iter, BTREE_ID_lru,
2015 lru_pos(ca->dev_idx, 0, 0),
2016 lru_pos(ca->dev_idx, U64_MAX, LRU_TIME_MAX),
2017 BTREE_ITER_INTENT, k,
2018 invalidate_one_bucket(trans, &iter, k, &nr_to_invalidate));
2021 percpu_ref_put(&ca->ref);
2026 bch2_trans_put(trans);
2027 bch2_write_ref_put(c, BCH_WRITE_REF_invalidate);
2030 void bch2_do_invalidates(struct bch_fs *c)
2032 if (bch2_write_ref_tryget(c, BCH_WRITE_REF_invalidate) &&
2033 !queue_work(c->write_ref_wq, &c->invalidate_work))
2034 bch2_write_ref_put(c, BCH_WRITE_REF_invalidate);
2037 int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca,
2038 u64 bucket_start, u64 bucket_end)
2040 struct btree_trans *trans = bch2_trans_get(c);
2041 struct btree_iter iter;
2044 struct bpos end = POS(ca->dev_idx, bucket_end);
2045 struct bch_member *m;
2046 unsigned long last_updated = jiffies;
2049 BUG_ON(bucket_start > bucket_end);
2050 BUG_ON(bucket_end > ca->mi.nbuckets);
2052 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
2053 POS(ca->dev_idx, max_t(u64, ca->mi.first_bucket, bucket_start)),
2054 BTREE_ITER_PREFETCH);
2056 * Scan the alloc btree for every bucket on @ca, and add buckets to the
2057 * freespace/need_discard/need_gc_gens btrees as needed:
2060 if (last_updated + HZ * 10 < jiffies) {
2061 bch_info(ca, "%s: currently at %llu/%llu",
2062 __func__, iter.pos.offset, ca->mi.nbuckets);
2063 last_updated = jiffies;
2066 bch2_trans_begin(trans);
2068 if (bkey_ge(iter.pos, end)) {
2073 k = bch2_get_key_or_hole(&iter, end, &hole);
2080 * We process live keys in the alloc btree one at a
2083 struct bch_alloc_v4 a_convert;
2084 const struct bch_alloc_v4 *a = bch2_alloc_to_v4(k, &a_convert);
2086 ret = bch2_bucket_do_index(trans, k, a, true) ?:
2087 bch2_trans_commit(trans, NULL, NULL,
2088 BCH_TRANS_COMMIT_no_enospc);
2092 bch2_btree_iter_advance(&iter);
2094 struct bkey_i *freespace;
2096 freespace = bch2_trans_kmalloc(trans, sizeof(*freespace));
2097 ret = PTR_ERR_OR_ZERO(freespace);
2101 bkey_init(&freespace->k);
2102 freespace->k.type = KEY_TYPE_set;
2103 freespace->k.p = k.k->p;
2104 freespace->k.size = k.k->size;
2106 ret = bch2_btree_insert_trans(trans, BTREE_ID_freespace, freespace, 0) ?:
2107 bch2_trans_commit(trans, NULL, NULL,
2108 BCH_TRANS_COMMIT_no_enospc);
2112 bch2_btree_iter_set_pos(&iter, k.k->p);
2115 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
2121 bch2_trans_iter_exit(trans, &iter);
2122 bch2_trans_put(trans);
2125 bch_err_msg(ca, ret, "initializing free space");
2129 mutex_lock(&c->sb_lock);
2130 m = bch2_members_v2_get_mut(c->disk_sb.sb, ca->dev_idx);
2131 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
2132 mutex_unlock(&c->sb_lock);
2137 int bch2_fs_freespace_init(struct bch_fs *c)
2140 bool doing_init = false;
2143 * We can crash during the device add path, so we need to check this on
2147 for_each_member_device(c, ca) {
2148 if (ca->mi.freespace_initialized)
2152 bch_info(c, "initializing freespace");
2156 ret = bch2_dev_freespace_init(c, ca, 0, ca->mi.nbuckets);
2158 percpu_ref_put(&ca->ref);
2165 mutex_lock(&c->sb_lock);
2166 bch2_write_super(c);
2167 mutex_unlock(&c->sb_lock);
2168 bch_verbose(c, "done initializing freespace");
2174 /* Bucket IO clocks: */
2176 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
2177 size_t bucket_nr, int rw)
2179 struct bch_fs *c = trans->c;
2180 struct btree_iter iter;
2181 struct bkey_i_alloc_v4 *a;
2185 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
2186 ret = PTR_ERR_OR_ZERO(a);
2190 now = atomic64_read(&c->io_clock[rw].now);
2191 if (a->v.io_time[rw] == now)
2194 a->v.io_time[rw] = now;
2196 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
2197 bch2_trans_commit(trans, NULL, NULL, 0);
2199 bch2_trans_iter_exit(trans, &iter);
2203 /* Startup/shutdown (ro/rw): */
2205 void bch2_recalc_capacity(struct bch_fs *c)
2207 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
2208 unsigned bucket_size_max = 0;
2209 unsigned long ra_pages = 0;
2211 lockdep_assert_held(&c->state_lock);
2213 for_each_online_member(c, ca) {
2214 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
2216 ra_pages += bdi->ra_pages;
2219 bch2_set_ra_pages(c, ra_pages);
2221 for_each_rw_member(c, ca) {
2222 u64 dev_reserve = 0;
2225 * We need to reserve buckets (from the number
2226 * of currently available buckets) against
2227 * foreground writes so that mainly copygc can
2228 * make forward progress.
2230 * We need enough to refill the various reserves
2231 * from scratch - copygc will use its entire
2232 * reserve all at once, then run against when
2233 * its reserve is refilled (from the formerly
2234 * available buckets).
2236 * This reserve is just used when considering if
2237 * allocations for foreground writes must wait -
2238 * not -ENOSPC calculations.
2241 dev_reserve += ca->nr_btree_reserve * 2;
2242 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
2244 dev_reserve += 1; /* btree write point */
2245 dev_reserve += 1; /* copygc write point */
2246 dev_reserve += 1; /* rebalance write point */
2248 dev_reserve *= ca->mi.bucket_size;
2250 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
2251 ca->mi.first_bucket);
2253 reserved_sectors += dev_reserve * 2;
2255 bucket_size_max = max_t(unsigned, bucket_size_max,
2256 ca->mi.bucket_size);
2259 gc_reserve = c->opts.gc_reserve_bytes
2260 ? c->opts.gc_reserve_bytes >> 9
2261 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
2263 reserved_sectors = max(gc_reserve, reserved_sectors);
2265 reserved_sectors = min(reserved_sectors, capacity);
2267 c->capacity = capacity - reserved_sectors;
2269 c->bucket_size_max = bucket_size_max;
2271 /* Wake up case someone was waiting for buckets */
2272 closure_wake_up(&c->freelist_wait);
2275 u64 bch2_min_rw_member_capacity(struct bch_fs *c)
2279 for_each_rw_member(c, ca)
2280 ret = min(ret, ca->mi.nbuckets * ca->mi.bucket_size);
2284 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
2286 struct open_bucket *ob;
2289 for (ob = c->open_buckets;
2290 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
2292 spin_lock(&ob->lock);
2293 if (ob->valid && !ob->on_partial_list &&
2294 ob->dev == ca->dev_idx)
2296 spin_unlock(&ob->lock);
2302 /* device goes ro: */
2303 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
2307 /* First, remove device from allocation groups: */
2309 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
2310 clear_bit(ca->dev_idx, c->rw_devs[i].d);
2313 * Capacity is calculated based off of devices in allocation groups:
2315 bch2_recalc_capacity(c);
2317 bch2_open_buckets_stop(c, ca, false);
2320 * Wake up threads that were blocked on allocation, so they can notice
2321 * the device can no longer be removed and the capacity has changed:
2323 closure_wake_up(&c->freelist_wait);
2326 * journal_res_get() can block waiting for free space in the journal -
2327 * it needs to notice there may not be devices to allocate from anymore:
2329 wake_up(&c->journal.wait);
2331 /* Now wait for any in flight writes: */
2333 closure_wait_event(&c->open_buckets_wait,
2334 !bch2_dev_has_open_write_point(c, ca));
2337 /* device goes rw: */
2338 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
2342 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
2343 if (ca->mi.data_allowed & (1 << i))
2344 set_bit(ca->dev_idx, c->rw_devs[i].d);
2347 void bch2_fs_allocator_background_exit(struct bch_fs *c)
2349 darray_exit(&c->discard_buckets_in_flight);
2352 void bch2_fs_allocator_background_init(struct bch_fs *c)
2354 spin_lock_init(&c->freelist_lock);
2355 mutex_init(&c->discard_buckets_in_flight_lock);
2356 INIT_WORK(&c->discard_work, bch2_do_discards_work);
2357 INIT_WORK(&c->discard_fast_work, bch2_do_discards_fast_work);
2358 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);