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"
13 #include "buckets_waiting_for_journal.h"
22 #include <linux/kthread.h>
23 #include <linux/math64.h>
24 #include <linux/random.h>
25 #include <linux/rculist.h>
26 #include <linux/rcupdate.h>
27 #include <linux/sched/task.h>
28 #include <linux/sort.h>
29 #include <trace/events/bcachefs.h>
31 /* Persistent alloc info: */
33 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
34 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
39 struct bkey_alloc_unpacked {
46 #define x(_name, _bits) u##_bits _name;
51 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
52 const void **p, unsigned field)
54 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
57 if (!(a->fields & (1 << field)))
62 v = *((const u8 *) *p);
81 static inline void alloc_field_v1_put(struct bkey_i_alloc *a, void **p,
82 unsigned field, u64 v)
84 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
89 a->v.fields |= 1 << field;
96 *((__le16 *) *p) = cpu_to_le16(v);
99 *((__le32 *) *p) = cpu_to_le32(v);
102 *((__le64 *) *p) = cpu_to_le64(v);
111 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
114 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
115 const void *d = in->data;
120 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
121 BCH_ALLOC_FIELDS_V1()
125 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
128 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
129 const u8 *in = a.v->data;
130 const u8 *end = bkey_val_end(a);
131 unsigned fieldnr = 0;
136 out->oldest_gen = a.v->oldest_gen;
137 out->data_type = a.v->data_type;
139 #define x(_name, _bits) \
140 if (fieldnr < a.v->nr_fields) { \
141 ret = bch2_varint_decode_fast(in, end, &v); \
149 if (v != out->_name) \
153 BCH_ALLOC_FIELDS_V2()
158 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
161 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
162 const u8 *in = a.v->data;
163 const u8 *end = bkey_val_end(a);
164 unsigned fieldnr = 0;
169 out->oldest_gen = a.v->oldest_gen;
170 out->data_type = a.v->data_type;
171 out->need_discard = BCH_ALLOC_V3_NEED_DISCARD(a.v);
172 out->need_inc_gen = BCH_ALLOC_V3_NEED_INC_GEN(a.v);
173 out->journal_seq = le64_to_cpu(a.v->journal_seq);
175 #define x(_name, _bits) \
176 if (fieldnr < a.v->nr_fields) { \
177 ret = bch2_varint_decode_fast(in, end, &v); \
185 if (v != out->_name) \
189 BCH_ALLOC_FIELDS_V2()
194 static struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
196 struct bkey_alloc_unpacked ret = { .gen = 0 };
200 bch2_alloc_unpack_v1(&ret, k);
202 case KEY_TYPE_alloc_v2:
203 bch2_alloc_unpack_v2(&ret, k);
205 case KEY_TYPE_alloc_v3:
206 bch2_alloc_unpack_v3(&ret, k);
213 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
215 unsigned i, bytes = offsetof(struct bch_alloc, data);
217 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
218 if (a->fields & (1 << i))
219 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
221 return DIV_ROUND_UP(bytes, sizeof(u64));
224 int bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k,
225 int rw, struct printbuf *err)
227 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
229 /* allow for unknown fields */
230 if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v)) {
231 prt_printf(err, "incorrect value size (%zu < %u)",
232 bkey_val_u64s(a.k), bch_alloc_v1_val_u64s(a.v));
239 int bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
240 int rw, struct printbuf *err)
242 struct bkey_alloc_unpacked u;
244 if (bch2_alloc_unpack_v2(&u, k)) {
245 prt_printf(err, "unpack error");
252 int bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
253 int rw, struct printbuf *err)
255 struct bkey_alloc_unpacked u;
257 if (bch2_alloc_unpack_v3(&u, k)) {
258 prt_printf(err, "unpack error");
265 int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
266 int rw, struct printbuf *err)
268 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
270 if (alloc_v4_u64s(a.v) != bkey_val_u64s(k.k)) {
271 prt_printf(err, "bad val size (%lu != %u)",
272 bkey_val_u64s(k.k), alloc_v4_u64s(a.v));
276 if (!BCH_ALLOC_V4_BACKPOINTERS_START(a.v) &&
277 BCH_ALLOC_V4_NR_BACKPOINTERS(a.v)) {
278 prt_printf(err, "invalid backpointers_start");
283 if (alloc_data_type(*a.v, a.v->data_type) != a.v->data_type) {
284 prt_printf(err, "invalid data type (got %u should be %u)",
285 a.v->data_type, alloc_data_type(*a.v, a.v->data_type));
289 switch (a.v->data_type) {
291 case BCH_DATA_need_gc_gens:
292 case BCH_DATA_need_discard:
293 if (a.v->dirty_sectors ||
294 a.v->cached_sectors ||
296 prt_printf(err, "empty data type free but have data");
301 case BCH_DATA_journal:
304 case BCH_DATA_parity:
305 if (!a.v->dirty_sectors) {
306 prt_printf(err, "data_type %s but dirty_sectors==0",
307 bch2_data_types[a.v->data_type]);
311 case BCH_DATA_cached:
312 if (!a.v->cached_sectors ||
313 a.v->dirty_sectors ||
315 prt_printf(err, "data type inconsistency");
319 if (!a.v->io_time[READ] &&
320 test_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags)) {
321 prt_printf(err, "cached bucket with read_time == 0");
325 case BCH_DATA_stripe:
327 prt_printf(err, "data_type %s but stripe==0",
328 bch2_data_types[a.v->data_type]);
338 static inline u64 swab40(u64 x)
340 return (((x & 0x00000000ffULL) << 32)|
341 ((x & 0x000000ff00ULL) << 16)|
342 ((x & 0x0000ff0000ULL) >> 0)|
343 ((x & 0x00ff000000ULL) >> 16)|
344 ((x & 0xff00000000ULL) >> 32));
347 void bch2_alloc_v4_swab(struct bkey_s k)
349 struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
350 struct bch_backpointer *bp, *bps;
352 a->journal_seq = swab64(a->journal_seq);
353 a->flags = swab32(a->flags);
354 a->dirty_sectors = swab32(a->dirty_sectors);
355 a->cached_sectors = swab32(a->cached_sectors);
356 a->io_time[0] = swab64(a->io_time[0]);
357 a->io_time[1] = swab64(a->io_time[1]);
358 a->stripe = swab32(a->stripe);
359 a->nr_external_backpointers = swab32(a->nr_external_backpointers);
361 bps = alloc_v4_backpointers(a);
362 for (bp = bps; bp < bps + BCH_ALLOC_V4_NR_BACKPOINTERS(a); bp++) {
363 bp->bucket_offset = swab40(bp->bucket_offset);
364 bp->bucket_len = swab32(bp->bucket_len);
365 bch2_bpos_swab(&bp->pos);
369 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
371 struct bch_alloc_v4 _a;
372 const struct bch_alloc_v4 *a = &_a;
373 const struct bch_backpointer *bps;
376 if (k.k->type == KEY_TYPE_alloc_v4)
377 a = bkey_s_c_to_alloc_v4(k).v;
379 bch2_alloc_to_v4(k, &_a);
382 printbuf_indent_add(out, 2);
384 prt_printf(out, "gen %u oldest_gen %u data_type %s",
385 a->gen, a->oldest_gen, bch2_data_types[a->data_type]);
387 prt_printf(out, "journal_seq %llu", a->journal_seq);
389 prt_printf(out, "need_discard %llu", BCH_ALLOC_V4_NEED_DISCARD(a));
391 prt_printf(out, "need_inc_gen %llu", BCH_ALLOC_V4_NEED_INC_GEN(a));
393 prt_printf(out, "dirty_sectors %u", a->dirty_sectors);
395 prt_printf(out, "cached_sectors %u", a->cached_sectors);
397 prt_printf(out, "stripe %u", a->stripe);
399 prt_printf(out, "stripe_redundancy %u", a->stripe_redundancy);
401 prt_printf(out, "io_time[READ] %llu", a->io_time[READ]);
403 prt_printf(out, "io_time[WRITE] %llu", a->io_time[WRITE]);
405 prt_printf(out, "backpointers: %llu", BCH_ALLOC_V4_NR_BACKPOINTERS(a));
406 printbuf_indent_add(out, 2);
408 bps = alloc_v4_backpointers_c(a);
409 for (i = 0; i < BCH_ALLOC_V4_NR_BACKPOINTERS(a); i++) {
411 bch2_backpointer_to_text(out, &bps[i]);
414 printbuf_indent_sub(out, 4);
417 void bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
419 if (k.k->type == KEY_TYPE_alloc_v4) {
422 *out = *bkey_s_c_to_alloc_v4(k).v;
424 d = (int) BCH_ALLOC_V4_U64s -
425 (int) (BCH_ALLOC_V4_BACKPOINTERS_START(out) ?: BCH_ALLOC_V4_U64s_V0);
426 if (unlikely(d > 0)) {
427 memset((u64 *) out + BCH_ALLOC_V4_BACKPOINTERS_START(out),
430 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
433 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
435 *out = (struct bch_alloc_v4) {
436 .journal_seq = u.journal_seq,
437 .flags = u.need_discard,
439 .oldest_gen = u.oldest_gen,
440 .data_type = u.data_type,
441 .stripe_redundancy = u.stripe_redundancy,
442 .dirty_sectors = u.dirty_sectors,
443 .cached_sectors = u.cached_sectors,
444 .io_time[READ] = u.read_time,
445 .io_time[WRITE] = u.write_time,
449 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
453 static noinline struct bkey_i_alloc_v4 *
454 __bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
456 struct bkey_i_alloc_v4 *ret;
457 unsigned bytes = k.k->type == KEY_TYPE_alloc_v4
459 : sizeof(struct bkey_i_alloc_v4);
462 * Reserve space for one more backpointer here:
463 * Not sketchy at doing it this way, nope...
465 ret = bch2_trans_kmalloc(trans, bytes + sizeof(struct bch_backpointer));
469 if (k.k->type == KEY_TYPE_alloc_v4) {
470 struct bch_backpointer *src, *dst;
472 bkey_reassemble(&ret->k_i, k);
474 src = alloc_v4_backpointers(&ret->v);
475 SET_BCH_ALLOC_V4_BACKPOINTERS_START(&ret->v, BCH_ALLOC_V4_U64s);
476 dst = alloc_v4_backpointers(&ret->v);
478 memmove(dst, src, BCH_ALLOC_V4_NR_BACKPOINTERS(&ret->v) *
479 sizeof(struct bch_backpointer));
480 memset(src, 0, dst - src);
481 set_alloc_v4_u64s(ret);
483 bkey_alloc_v4_init(&ret->k_i);
485 bch2_alloc_to_v4(k, &ret->v);
490 static inline struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut_inlined(struct btree_trans *trans, struct bkey_s_c k)
492 if (likely(k.k->type == KEY_TYPE_alloc_v4) &&
493 BCH_ALLOC_V4_BACKPOINTERS_START(bkey_s_c_to_alloc_v4(k).v) == BCH_ALLOC_V4_U64s) {
495 * Reserve space for one more backpointer here:
496 * Not sketchy at doing it this way, nope...
498 struct bkey_i_alloc_v4 *ret =
499 bch2_trans_kmalloc(trans, bkey_bytes(k.k) + sizeof(struct bch_backpointer));
501 bkey_reassemble(&ret->k_i, k);
505 return __bch2_alloc_to_v4_mut(trans, k);
508 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
510 return bch2_alloc_to_v4_mut_inlined(trans, k);
513 struct bkey_i_alloc_v4 *
514 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
518 struct bkey_i_alloc_v4 *a;
521 bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos,
522 BTREE_ITER_WITH_UPDATES|
525 k = bch2_btree_iter_peek_slot(iter);
528 bch2_trans_iter_exit(trans, iter);
532 a = bch2_alloc_to_v4_mut_inlined(trans, k);
534 bch2_trans_iter_exit(trans, iter);
538 int bch2_alloc_read(struct bch_fs *c)
540 struct btree_trans trans;
541 struct btree_iter iter;
543 struct bch_alloc_v4 a;
547 bch2_trans_init(&trans, c, 0, 0);
549 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
550 BTREE_ITER_PREFETCH, k, ret) {
552 * Not a fsck error because this is checked/repaired by
553 * bch2_check_alloc_key() which runs later:
555 if (!bch2_dev_bucket_exists(c, k.k->p))
558 ca = bch_dev_bkey_exists(c, k.k->p.inode);
559 bch2_alloc_to_v4(k, &a);
561 *bucket_gen(ca, k.k->p.offset) = a.gen;
563 bch2_trans_iter_exit(&trans, &iter);
565 bch2_trans_exit(&trans);
568 bch_err(c, "error reading alloc info: %s", bch2_err_str(ret));
573 /* Free space/discard btree: */
575 static int bch2_bucket_do_index(struct btree_trans *trans,
576 struct bkey_s_c alloc_k,
577 const struct bch_alloc_v4 *a,
580 struct bch_fs *c = trans->c;
581 struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
582 struct btree_iter iter;
586 enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
587 enum bch_bkey_type new_type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
588 struct printbuf buf = PRINTBUF;
591 if (a->data_type != BCH_DATA_free &&
592 a->data_type != BCH_DATA_need_discard)
595 k = bch2_trans_kmalloc(trans, sizeof(*k));
600 k->k.type = new_type;
602 switch (a->data_type) {
604 btree = BTREE_ID_freespace;
605 k->k.p = alloc_freespace_pos(alloc_k.k->p, *a);
606 bch2_key_resize(&k->k, 1);
608 case BCH_DATA_need_discard:
609 btree = BTREE_ID_need_discard;
610 k->k.p = alloc_k.k->p;
616 bch2_trans_iter_init(trans, &iter, btree,
617 bkey_start_pos(&k->k),
619 old = bch2_btree_iter_peek_slot(&iter);
624 if (ca->mi.freespace_initialized &&
625 bch2_trans_inconsistent_on(old.k->type != old_type, trans,
626 "incorrect key when %s %s btree (got %s should be %s)\n"
628 set ? "setting" : "clearing",
629 bch2_btree_ids[btree],
630 bch2_bkey_types[old.k->type],
631 bch2_bkey_types[old_type],
632 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
637 ret = bch2_trans_update(trans, &iter, k, 0);
639 bch2_trans_iter_exit(trans, &iter);
644 int bch2_trans_mark_alloc(struct btree_trans *trans,
645 enum btree_id btree_id, unsigned level,
646 struct bkey_s_c old, struct bkey_i *new,
649 struct bch_fs *c = trans->c;
650 struct bch_alloc_v4 old_a, *new_a;
651 u64 old_lru, new_lru;
655 * Deletion only happens in the device removal path, with
656 * BTREE_TRIGGER_NORUN:
658 BUG_ON(new->k.type != KEY_TYPE_alloc_v4);
660 bch2_alloc_to_v4(old, &old_a);
661 new_a = &bkey_i_to_alloc_v4(new)->v;
663 new_a->data_type = alloc_data_type(*new_a, new_a->data_type);
665 if (new_a->dirty_sectors > old_a.dirty_sectors ||
666 new_a->cached_sectors > old_a.cached_sectors) {
667 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
668 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
669 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
670 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
673 if (data_type_is_empty(new_a->data_type) &&
674 BCH_ALLOC_V4_NEED_INC_GEN(new_a) &&
675 !bch2_bucket_is_open_safe(c, new->k.p.inode, new->k.p.offset)) {
677 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
680 if (old_a.data_type != new_a->data_type ||
681 (new_a->data_type == BCH_DATA_free &&
682 alloc_freespace_genbits(old_a) != alloc_freespace_genbits(*new_a))) {
683 ret = bch2_bucket_do_index(trans, old, &old_a, false) ?:
684 bch2_bucket_do_index(trans, bkey_i_to_s_c(new), new_a, true);
689 if (new_a->data_type == BCH_DATA_cached &&
690 !new_a->io_time[READ])
691 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
693 old_lru = alloc_lru_idx(old_a);
694 new_lru = alloc_lru_idx(*new_a);
696 if (old_lru != new_lru) {
697 ret = bch2_lru_change(trans, new->k.p.inode, new->k.p.offset,
698 old_lru, &new_lru, old);
702 if (new_a->data_type == BCH_DATA_cached)
703 new_a->io_time[READ] = new_lru;
709 static int bch2_check_alloc_key(struct btree_trans *trans,
710 struct btree_iter *alloc_iter,
711 struct btree_iter *discard_iter,
712 struct btree_iter *freespace_iter)
714 struct bch_fs *c = trans->c;
716 struct bch_alloc_v4 a;
717 unsigned discard_key_type, freespace_key_type;
718 struct bkey_s_c alloc_k, k;
719 struct printbuf buf = PRINTBUF;
722 alloc_k = bch2_dev_bucket_exists(c, alloc_iter->pos)
723 ? bch2_btree_iter_peek_slot(alloc_iter)
724 : bch2_btree_iter_peek(alloc_iter);
728 ret = bkey_err(alloc_k);
732 if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_k.k->p), c,
733 "alloc key for invalid device:bucket %llu:%llu",
734 alloc_k.k->p.inode, alloc_k.k->p.offset))
735 return bch2_btree_delete_at(trans, alloc_iter, 0);
737 ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
738 if (!ca->mi.freespace_initialized)
741 bch2_alloc_to_v4(alloc_k, &a);
743 discard_key_type = a.data_type == BCH_DATA_need_discard
745 freespace_key_type = a.data_type == BCH_DATA_free
748 bch2_btree_iter_set_pos(discard_iter, alloc_k.k->p);
749 bch2_btree_iter_set_pos(freespace_iter, alloc_freespace_pos(alloc_k.k->p, a));
751 k = bch2_btree_iter_peek_slot(discard_iter);
756 if (k.k->type != discard_key_type &&
757 (c->opts.reconstruct_alloc ||
758 fsck_err(c, "incorrect key in need_discard btree (got %s should be %s)\n"
760 bch2_bkey_types[k.k->type],
761 bch2_bkey_types[discard_key_type],
762 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)))) {
763 struct bkey_i *update =
764 bch2_trans_kmalloc(trans, sizeof(*update));
766 ret = PTR_ERR_OR_ZERO(update);
770 bkey_init(&update->k);
771 update->k.type = discard_key_type;
772 update->k.p = discard_iter->pos;
774 ret = bch2_trans_update(trans, discard_iter, update, 0);
779 k = bch2_btree_iter_peek_slot(freespace_iter);
784 if (k.k->type != freespace_key_type &&
785 (c->opts.reconstruct_alloc ||
786 fsck_err(c, "incorrect key in freespace btree (got %s should be %s)\n"
788 bch2_bkey_types[k.k->type],
789 bch2_bkey_types[freespace_key_type],
790 (printbuf_reset(&buf),
791 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)))) {
792 struct bkey_i *update =
793 bch2_trans_kmalloc(trans, sizeof(*update));
795 ret = PTR_ERR_OR_ZERO(update);
799 bkey_init(&update->k);
800 update->k.type = freespace_key_type;
801 update->k.p = freespace_iter->pos;
802 bch2_key_resize(&update->k, 1);
804 ret = bch2_trans_update(trans, freespace_iter, update, 0);
814 static int bch2_check_discard_freespace_key(struct btree_trans *trans,
815 struct btree_iter *iter)
817 struct bch_fs *c = trans->c;
818 struct btree_iter alloc_iter;
819 struct bkey_s_c alloc_k;
820 struct bch_alloc_v4 a;
823 enum bch_data_type state = iter->btree_id == BTREE_ID_need_discard
824 ? BCH_DATA_need_discard
826 struct printbuf buf = PRINTBUF;
830 pos.offset &= ~(~0ULL << 56);
831 genbits = iter->pos.offset & (~0ULL << 56);
833 bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
835 if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
836 "entry in %s btree for nonexistant dev:bucket %llu:%llu",
837 bch2_btree_ids[iter->btree_id], pos.inode, pos.offset))
840 alloc_k = bch2_btree_iter_peek_slot(&alloc_iter);
841 ret = bkey_err(alloc_k);
845 bch2_alloc_to_v4(alloc_k, &a);
847 if (fsck_err_on(a.data_type != state ||
848 (state == BCH_DATA_free &&
849 genbits != alloc_freespace_genbits(a)), c,
850 "%s\n incorrectly set in %s index (free %u, genbits %llu should be %llu)",
851 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
852 bch2_btree_ids[iter->btree_id],
853 a.data_type == state,
854 genbits >> 56, alloc_freespace_genbits(a) >> 56))
859 bch2_trans_iter_exit(trans, &alloc_iter);
863 ret = bch2_btree_delete_extent_at(trans, iter,
864 iter->btree_id == BTREE_ID_freespace ? 1 : 0, 0);
868 int bch2_check_alloc_info(struct bch_fs *c)
870 struct btree_trans trans;
871 struct btree_iter iter, discard_iter, freespace_iter;
875 bch2_trans_init(&trans, c, 0, 0);
877 bch2_trans_iter_init(&trans, &iter, BTREE_ID_alloc, POS_MIN,
878 BTREE_ITER_PREFETCH);
879 bch2_trans_iter_init(&trans, &discard_iter, BTREE_ID_need_discard, POS_MIN,
880 BTREE_ITER_PREFETCH);
881 bch2_trans_iter_init(&trans, &freespace_iter, BTREE_ID_freespace, POS_MIN,
882 BTREE_ITER_PREFETCH);
884 ret = commit_do(&trans, NULL, NULL,
886 BTREE_INSERT_LAZY_RW,
887 bch2_check_alloc_key(&trans, &iter,
893 bch2_btree_iter_advance(&iter);
895 bch2_trans_iter_exit(&trans, &freespace_iter);
896 bch2_trans_iter_exit(&trans, &discard_iter);
897 bch2_trans_iter_exit(&trans, &iter);
902 ret = for_each_btree_key_commit(&trans, iter,
903 BTREE_ID_need_discard, POS_MIN,
904 BTREE_ITER_PREFETCH, k,
905 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
906 bch2_check_discard_freespace_key(&trans, &iter)) ?:
907 for_each_btree_key_commit(&trans, iter,
908 BTREE_ID_freespace, POS_MIN,
909 BTREE_ITER_PREFETCH, k,
910 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
911 bch2_check_discard_freespace_key(&trans, &iter));
913 bch2_trans_exit(&trans);
914 return ret < 0 ? ret : 0;
917 static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
918 struct btree_iter *alloc_iter)
920 struct bch_fs *c = trans->c;
921 struct btree_iter lru_iter;
922 struct bch_alloc_v4 a;
923 struct bkey_s_c alloc_k, k;
924 struct printbuf buf = PRINTBUF;
925 struct printbuf buf2 = PRINTBUF;
928 alloc_k = bch2_btree_iter_peek(alloc_iter);
932 ret = bkey_err(alloc_k);
936 bch2_alloc_to_v4(alloc_k, &a);
938 if (a.data_type != BCH_DATA_cached)
941 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
942 POS(alloc_k.k->p.inode, a.io_time[READ]), 0);
944 k = bch2_btree_iter_peek_slot(&lru_iter);
949 if (fsck_err_on(!a.io_time[READ], c,
950 "cached bucket with read_time 0\n"
952 (printbuf_reset(&buf),
953 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) ||
954 fsck_err_on(k.k->type != KEY_TYPE_lru ||
955 le64_to_cpu(bkey_s_c_to_lru(k).v->idx) != alloc_k.k->p.offset, c,
956 "incorrect/missing lru entry\n"
959 (printbuf_reset(&buf),
960 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
961 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf))) {
962 u64 read_time = a.io_time[READ];
964 if (!a.io_time[READ])
965 a.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
967 ret = bch2_lru_set(trans,
974 if (a.io_time[READ] != read_time) {
975 struct bkey_i_alloc_v4 *a_mut =
976 bch2_alloc_to_v4_mut(trans, alloc_k);
977 ret = PTR_ERR_OR_ZERO(a_mut);
981 a_mut->v.io_time[READ] = a.io_time[READ];
982 ret = bch2_trans_update(trans, alloc_iter,
983 &a_mut->k_i, BTREE_TRIGGER_NORUN);
990 bch2_trans_iter_exit(trans, &lru_iter);
991 printbuf_exit(&buf2);
996 int bch2_check_alloc_to_lru_refs(struct bch_fs *c)
998 struct btree_trans trans;
999 struct btree_iter iter;
1003 bch2_trans_init(&trans, c, 0, 0);
1005 for_each_btree_key_commit(&trans, iter, BTREE_ID_alloc,
1006 POS_MIN, BTREE_ITER_PREFETCH, k,
1007 NULL, NULL, BTREE_INSERT_NOFAIL|BTREE_INSERT_LAZY_RW,
1008 bch2_check_alloc_to_lru_ref(&trans, &iter));
1010 bch2_trans_exit(&trans);
1011 return ret < 0 ? ret : 0;
1014 static int bch2_discard_one_bucket(struct btree_trans *trans,
1015 struct btree_iter *need_discard_iter,
1016 struct bpos *discard_pos_done,
1019 u64 *need_journal_commit,
1022 struct bch_fs *c = trans->c;
1023 struct bpos pos = need_discard_iter->pos;
1024 struct btree_iter iter = { NULL };
1027 struct bkey_i_alloc_v4 *a;
1028 struct printbuf buf = PRINTBUF;
1029 bool did_discard = false;
1032 ca = bch_dev_bkey_exists(c, pos.inode);
1033 if (!percpu_ref_tryget(&ca->io_ref)) {
1034 bch2_btree_iter_set_pos(need_discard_iter, POS(pos.inode + 1, 0));
1038 if (bch2_bucket_is_open_safe(c, pos.inode, pos.offset)) {
1043 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
1044 c->journal.flushed_seq_ondisk,
1045 pos.inode, pos.offset)) {
1046 (*need_journal_commit)++;
1050 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
1051 need_discard_iter->pos,
1053 k = bch2_btree_iter_peek_slot(&iter);
1058 a = bch2_alloc_to_v4_mut(trans, k);
1059 ret = PTR_ERR_OR_ZERO(a);
1063 if (BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
1065 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1069 if (bch2_trans_inconsistent_on(a->v.journal_seq > c->journal.flushed_seq_ondisk, trans,
1070 "clearing need_discard but journal_seq %llu > flushed_seq %llu\n"
1073 c->journal.flushed_seq_ondisk,
1074 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1079 if (bch2_trans_inconsistent_on(a->v.data_type != BCH_DATA_need_discard, trans,
1080 "bucket incorrectly set in need_discard btree\n"
1082 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1087 if (bkey_cmp(*discard_pos_done, iter.pos) &&
1088 ca->mi.discard && !c->opts.nochanges) {
1090 * This works without any other locks because this is the only
1091 * thread that removes items from the need_discard tree
1093 bch2_trans_unlock(trans);
1094 blkdev_issue_discard(ca->disk_sb.bdev,
1095 k.k->p.offset * ca->mi.bucket_size,
1099 ret = bch2_trans_relock(trans);
1104 *discard_pos_done = iter.pos;
1107 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
1108 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
1110 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1111 bch2_trans_commit(trans, NULL, NULL,
1112 BTREE_INSERT_USE_RESERVE|BTREE_INSERT_NOFAIL);
1117 this_cpu_inc(c->counters[BCH_COUNTER_bucket_discard]);
1121 bch2_trans_iter_exit(trans, &iter);
1122 percpu_ref_put(&ca->io_ref);
1123 printbuf_exit(&buf);
1127 static void bch2_do_discards_work(struct work_struct *work)
1129 struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
1130 struct btree_trans trans;
1131 struct btree_iter iter;
1133 u64 seen = 0, open = 0, need_journal_commit = 0, discarded = 0;
1134 struct bpos discard_pos_done = POS_MAX;
1137 bch2_trans_init(&trans, c, 0, 0);
1140 * We're doing the commit in bch2_discard_one_bucket instead of using
1141 * for_each_btree_key_commit() so that we can increment counters after
1142 * successful commit:
1144 ret = for_each_btree_key2(&trans, iter,
1145 BTREE_ID_need_discard, POS_MIN, 0, k,
1146 bch2_discard_one_bucket(&trans, &iter, &discard_pos_done,
1149 &need_journal_commit,
1152 bch2_trans_exit(&trans);
1154 if (need_journal_commit * 2 > seen)
1155 bch2_journal_flush_async(&c->journal, NULL);
1157 percpu_ref_put(&c->writes);
1159 trace_discard_buckets(c, seen, open, need_journal_commit, discarded,
1163 void bch2_do_discards(struct bch_fs *c)
1165 if (percpu_ref_tryget_live(&c->writes) &&
1166 !queue_work(system_long_wq, &c->discard_work))
1167 percpu_ref_put(&c->writes);
1170 static int invalidate_one_bucket(struct btree_trans *trans,
1171 struct btree_iter *lru_iter, struct bkey_s_c k,
1172 unsigned dev_idx, s64 *nr_to_invalidate)
1174 struct bch_fs *c = trans->c;
1175 struct btree_iter alloc_iter = { NULL };
1176 struct bkey_i_alloc_v4 *a;
1178 struct printbuf buf = PRINTBUF;
1179 unsigned cached_sectors;
1182 if (*nr_to_invalidate <= 0 || k.k->p.inode != dev_idx)
1185 if (k.k->type != KEY_TYPE_lru) {
1186 prt_printf(&buf, "non lru key in lru btree:\n ");
1187 bch2_bkey_val_to_text(&buf, c, k);
1189 if (!test_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags)) {
1190 bch_err(c, "%s", buf.buf);
1192 bch2_trans_inconsistent(trans, "%s", buf.buf);
1199 bucket = POS(dev_idx, le64_to_cpu(bkey_s_c_to_lru(k).v->idx));
1201 a = bch2_trans_start_alloc_update(trans, &alloc_iter, bucket);
1202 ret = PTR_ERR_OR_ZERO(a);
1206 if (k.k->p.offset != alloc_lru_idx(a->v)) {
1207 prt_printf(&buf, "alloc key does not point back to lru entry when invalidating bucket:\n ");
1208 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&a->k_i));
1209 prt_printf(&buf, "\n ");
1210 bch2_bkey_val_to_text(&buf, c, k);
1212 if (!test_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags)) {
1213 bch_err(c, "%s", buf.buf);
1215 bch2_trans_inconsistent(trans, "%s", buf.buf);
1222 if (!a->v.cached_sectors)
1223 bch_err(c, "invalidating empty bucket, confused");
1225 cached_sectors = a->v.cached_sectors;
1227 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1230 a->v.dirty_sectors = 0;
1231 a->v.cached_sectors = 0;
1232 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1233 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1235 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1236 BTREE_TRIGGER_BUCKET_INVALIDATE) ?:
1237 bch2_trans_commit(trans, NULL, NULL,
1238 BTREE_INSERT_USE_RESERVE|BTREE_INSERT_NOFAIL);
1242 trace_and_count(c, bucket_invalidate, c, bucket.inode, bucket.offset, cached_sectors);
1243 --*nr_to_invalidate;
1245 bch2_trans_iter_exit(trans, &alloc_iter);
1246 printbuf_exit(&buf);
1250 static void bch2_do_invalidates_work(struct work_struct *work)
1252 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
1254 struct btree_trans trans;
1255 struct btree_iter iter;
1260 bch2_trans_init(&trans, c, 0, 0);
1262 for_each_member_device(ca, c, i) {
1263 s64 nr_to_invalidate =
1264 should_invalidate_buckets(ca, bch2_dev_usage_read(ca));
1266 ret = for_each_btree_key2(&trans, iter, BTREE_ID_lru,
1267 POS(ca->dev_idx, 0), BTREE_ITER_INTENT, k,
1268 invalidate_one_bucket(&trans, &iter, k, ca->dev_idx, &nr_to_invalidate));
1271 percpu_ref_put(&ca->ref);
1276 bch2_trans_exit(&trans);
1277 percpu_ref_put(&c->writes);
1280 void bch2_do_invalidates(struct bch_fs *c)
1282 if (percpu_ref_tryget_live(&c->writes) &&
1283 !queue_work(system_long_wq, &c->invalidate_work))
1284 percpu_ref_put(&c->writes);
1287 static int bucket_freespace_init(struct btree_trans *trans, struct btree_iter *iter,
1288 struct bkey_s_c k, struct bch_dev *ca)
1290 struct bch_alloc_v4 a;
1292 if (iter->pos.offset >= ca->mi.nbuckets)
1295 bch2_alloc_to_v4(k, &a);
1296 return bch2_bucket_do_index(trans, k, &a, true);
1299 static int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca)
1301 struct btree_trans trans;
1302 struct btree_iter iter;
1304 struct bch_member *m;
1307 bch2_trans_init(&trans, c, 0, 0);
1309 ret = for_each_btree_key_commit(&trans, iter, BTREE_ID_alloc,
1310 POS(ca->dev_idx, ca->mi.first_bucket),
1311 BTREE_ITER_SLOTS|BTREE_ITER_PREFETCH, k,
1312 NULL, NULL, BTREE_INSERT_LAZY_RW,
1313 bucket_freespace_init(&trans, &iter, k, ca));
1315 bch2_trans_exit(&trans);
1318 bch_err(ca, "error initializing free space: %s", bch2_err_str(ret));
1322 mutex_lock(&c->sb_lock);
1323 m = bch2_sb_get_members(c->disk_sb.sb)->members + ca->dev_idx;
1324 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
1325 mutex_unlock(&c->sb_lock);
1330 int bch2_fs_freespace_init(struct bch_fs *c)
1335 bool doing_init = false;
1338 * We can crash during the device add path, so we need to check this on
1342 for_each_member_device(ca, c, i) {
1343 if (ca->mi.freespace_initialized)
1347 bch_info(c, "initializing freespace");
1351 ret = bch2_dev_freespace_init(c, ca);
1353 percpu_ref_put(&ca->ref);
1359 mutex_lock(&c->sb_lock);
1360 bch2_write_super(c);
1361 mutex_unlock(&c->sb_lock);
1363 bch_verbose(c, "done initializing freespace");
1369 /* Bucket IO clocks: */
1371 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
1372 size_t bucket_nr, int rw)
1374 struct bch_fs *c = trans->c;
1375 struct btree_iter iter;
1376 struct bkey_i_alloc_v4 *a;
1380 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
1381 ret = PTR_ERR_OR_ZERO(a);
1385 now = atomic64_read(&c->io_clock[rw].now);
1386 if (a->v.io_time[rw] == now)
1389 a->v.io_time[rw] = now;
1391 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1392 bch2_trans_commit(trans, NULL, NULL, 0);
1394 bch2_trans_iter_exit(trans, &iter);
1398 /* Startup/shutdown (ro/rw): */
1400 void bch2_recalc_capacity(struct bch_fs *c)
1403 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
1404 unsigned bucket_size_max = 0;
1405 unsigned long ra_pages = 0;
1408 lockdep_assert_held(&c->state_lock);
1410 for_each_online_member(ca, c, i) {
1411 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
1413 ra_pages += bdi->ra_pages;
1416 bch2_set_ra_pages(c, ra_pages);
1418 for_each_rw_member(ca, c, i) {
1419 u64 dev_reserve = 0;
1422 * We need to reserve buckets (from the number
1423 * of currently available buckets) against
1424 * foreground writes so that mainly copygc can
1425 * make forward progress.
1427 * We need enough to refill the various reserves
1428 * from scratch - copygc will use its entire
1429 * reserve all at once, then run against when
1430 * its reserve is refilled (from the formerly
1431 * available buckets).
1433 * This reserve is just used when considering if
1434 * allocations for foreground writes must wait -
1435 * not -ENOSPC calculations.
1438 dev_reserve += ca->nr_btree_reserve * 2;
1439 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
1441 dev_reserve += 1; /* btree write point */
1442 dev_reserve += 1; /* copygc write point */
1443 dev_reserve += 1; /* rebalance write point */
1445 dev_reserve *= ca->mi.bucket_size;
1447 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
1448 ca->mi.first_bucket);
1450 reserved_sectors += dev_reserve * 2;
1452 bucket_size_max = max_t(unsigned, bucket_size_max,
1453 ca->mi.bucket_size);
1456 gc_reserve = c->opts.gc_reserve_bytes
1457 ? c->opts.gc_reserve_bytes >> 9
1458 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
1460 reserved_sectors = max(gc_reserve, reserved_sectors);
1462 reserved_sectors = min(reserved_sectors, capacity);
1464 c->capacity = capacity - reserved_sectors;
1466 c->bucket_size_max = bucket_size_max;
1468 /* Wake up case someone was waiting for buckets */
1469 closure_wake_up(&c->freelist_wait);
1472 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
1474 struct open_bucket *ob;
1477 for (ob = c->open_buckets;
1478 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1480 spin_lock(&ob->lock);
1481 if (ob->valid && !ob->on_partial_list &&
1482 ob->dev == ca->dev_idx)
1484 spin_unlock(&ob->lock);
1490 /* device goes ro: */
1491 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
1495 /* First, remove device from allocation groups: */
1497 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1498 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1501 * Capacity is calculated based off of devices in allocation groups:
1503 bch2_recalc_capacity(c);
1505 /* Next, close write points that point to this device... */
1506 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1507 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1509 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1510 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1511 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1513 mutex_lock(&c->btree_reserve_cache_lock);
1514 while (c->btree_reserve_cache_nr) {
1515 struct btree_alloc *a =
1516 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1518 bch2_open_buckets_put(c, &a->ob);
1520 mutex_unlock(&c->btree_reserve_cache_lock);
1523 struct open_bucket *ob;
1525 spin_lock(&c->freelist_lock);
1526 if (!ca->open_buckets_partial_nr) {
1527 spin_unlock(&c->freelist_lock);
1530 ob = c->open_buckets +
1531 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1532 ob->on_partial_list = false;
1533 spin_unlock(&c->freelist_lock);
1535 bch2_open_bucket_put(c, ob);
1538 bch2_ec_stop_dev(c, ca);
1541 * Wake up threads that were blocked on allocation, so they can notice
1542 * the device can no longer be removed and the capacity has changed:
1544 closure_wake_up(&c->freelist_wait);
1547 * journal_res_get() can block waiting for free space in the journal -
1548 * it needs to notice there may not be devices to allocate from anymore:
1550 wake_up(&c->journal.wait);
1552 /* Now wait for any in flight writes: */
1554 closure_wait_event(&c->open_buckets_wait,
1555 !bch2_dev_has_open_write_point(c, ca));
1558 /* device goes rw: */
1559 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1563 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1564 if (ca->mi.data_allowed & (1 << i))
1565 set_bit(ca->dev_idx, c->rw_devs[i].d);
1568 void bch2_fs_allocator_background_init(struct bch_fs *c)
1570 spin_lock_init(&c->freelist_lock);
1571 INIT_WORK(&c->discard_work, bch2_do_discards_work);
1572 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);