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 struct bkey_i_alloc_v4 *
214 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
218 struct bkey_i_alloc_v4 *a;
221 bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos,
222 BTREE_ITER_WITH_UPDATES|
225 k = bch2_btree_iter_peek_slot(iter);
228 bch2_trans_iter_exit(trans, iter);
232 a = bch2_alloc_to_v4_mut(trans, k);
234 bch2_trans_iter_exit(trans, iter);
238 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
240 unsigned i, bytes = offsetof(struct bch_alloc, data);
242 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
243 if (a->fields & (1 << i))
244 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
246 return DIV_ROUND_UP(bytes, sizeof(u64));
249 int bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k,
250 int rw, struct printbuf *err)
252 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
254 /* allow for unknown fields */
255 if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v)) {
256 prt_printf(err, "incorrect value size (%zu < %u)",
257 bkey_val_u64s(a.k), bch_alloc_v1_val_u64s(a.v));
264 int bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
265 int rw, struct printbuf *err)
267 struct bkey_alloc_unpacked u;
269 if (bch2_alloc_unpack_v2(&u, k)) {
270 prt_printf(err, "unpack error");
277 int bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
278 int rw, struct printbuf *err)
280 struct bkey_alloc_unpacked u;
282 if (bch2_alloc_unpack_v3(&u, k)) {
283 prt_printf(err, "unpack error");
290 int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
291 int rw, struct printbuf *err)
293 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
295 if (alloc_v4_u64s(a.v) != bkey_val_u64s(k.k)) {
296 prt_printf(err, "bad val size (%lu != %u)",
297 bkey_val_u64s(k.k), alloc_v4_u64s(a.v));
301 if (!BCH_ALLOC_V4_BACKPOINTERS_START(a.v) &&
302 BCH_ALLOC_V4_NR_BACKPOINTERS(a.v)) {
303 prt_printf(err, "invalid backpointers_start");
308 if (alloc_data_type(*a.v, a.v->data_type) != a.v->data_type) {
309 prt_printf(err, "invalid data type (got %u should be %u)",
310 a.v->data_type, alloc_data_type(*a.v, a.v->data_type));
314 switch (a.v->data_type) {
316 case BCH_DATA_need_gc_gens:
317 case BCH_DATA_need_discard:
318 if (a.v->dirty_sectors ||
319 a.v->cached_sectors ||
321 prt_printf(err, "empty data type free but have data");
326 case BCH_DATA_journal:
329 case BCH_DATA_parity:
330 if (!a.v->dirty_sectors) {
331 prt_printf(err, "data_type %s but dirty_sectors==0",
332 bch2_data_types[a.v->data_type]);
336 case BCH_DATA_cached:
337 if (!a.v->cached_sectors ||
338 a.v->dirty_sectors ||
340 prt_printf(err, "data type inconsistency");
344 if (!a.v->io_time[READ] &&
345 test_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags)) {
346 prt_printf(err, "cached bucket with read_time == 0");
350 case BCH_DATA_stripe:
352 prt_printf(err, "data_type %s but stripe==0",
353 bch2_data_types[a.v->data_type]);
363 static inline u64 swab40(u64 x)
365 return (((x & 0x00000000ffULL) << 32)|
366 ((x & 0x000000ff00ULL) << 16)|
367 ((x & 0x0000ff0000ULL) >> 0)|
368 ((x & 0x00ff000000ULL) >> 16)|
369 ((x & 0xff00000000ULL) >> 32));
372 void bch2_alloc_v4_swab(struct bkey_s k)
374 struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
375 struct bch_backpointer *bp, *bps;
377 a->journal_seq = swab64(a->journal_seq);
378 a->flags = swab32(a->flags);
379 a->dirty_sectors = swab32(a->dirty_sectors);
380 a->cached_sectors = swab32(a->cached_sectors);
381 a->io_time[0] = swab64(a->io_time[0]);
382 a->io_time[1] = swab64(a->io_time[1]);
383 a->stripe = swab32(a->stripe);
384 a->nr_external_backpointers = swab32(a->nr_external_backpointers);
386 bps = alloc_v4_backpointers(a);
387 for (bp = bps; bp < bps + BCH_ALLOC_V4_NR_BACKPOINTERS(a); bp++) {
388 bp->bucket_offset = swab40(bp->bucket_offset);
389 bp->bucket_len = swab32(bp->bucket_len);
390 bch2_bpos_swab(&bp->pos);
394 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
396 struct bch_alloc_v4 _a;
397 const struct bch_alloc_v4 *a = &_a;
398 const struct bch_backpointer *bps;
401 if (k.k->type == KEY_TYPE_alloc_v4)
402 a = bkey_s_c_to_alloc_v4(k).v;
404 bch2_alloc_to_v4(k, &_a);
407 printbuf_indent_add(out, 2);
409 prt_printf(out, "gen %u oldest_gen %u data_type %s",
410 a->gen, a->oldest_gen, bch2_data_types[a->data_type]);
412 prt_printf(out, "journal_seq %llu", a->journal_seq);
414 prt_printf(out, "need_discard %llu", BCH_ALLOC_V4_NEED_DISCARD(a));
416 prt_printf(out, "need_inc_gen %llu", BCH_ALLOC_V4_NEED_INC_GEN(a));
418 prt_printf(out, "dirty_sectors %u", a->dirty_sectors);
420 prt_printf(out, "cached_sectors %u", a->cached_sectors);
422 prt_printf(out, "stripe %u", a->stripe);
424 prt_printf(out, "stripe_redundancy %u", a->stripe_redundancy);
426 prt_printf(out, "io_time[READ] %llu", a->io_time[READ]);
428 prt_printf(out, "io_time[WRITE] %llu", a->io_time[WRITE]);
430 prt_printf(out, "backpointers: %llu", BCH_ALLOC_V4_NR_BACKPOINTERS(a));
431 printbuf_indent_add(out, 2);
433 bps = alloc_v4_backpointers_c(a);
434 for (i = 0; i < BCH_ALLOC_V4_NR_BACKPOINTERS(a); i++) {
436 bch2_backpointer_to_text(out, &bps[i]);
439 printbuf_indent_sub(out, 4);
442 void bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
444 if (k.k->type == KEY_TYPE_alloc_v4) {
447 *out = *bkey_s_c_to_alloc_v4(k).v;
449 d = (int) BCH_ALLOC_V4_U64s -
450 (int) (BCH_ALLOC_V4_BACKPOINTERS_START(out) ?: BCH_ALLOC_V4_U64s_V0);
451 if (unlikely(d > 0)) {
452 memset((u64 *) out + BCH_ALLOC_V4_BACKPOINTERS_START(out),
455 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
458 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
460 *out = (struct bch_alloc_v4) {
461 .journal_seq = u.journal_seq,
462 .flags = u.need_discard,
464 .oldest_gen = u.oldest_gen,
465 .data_type = u.data_type,
466 .stripe_redundancy = u.stripe_redundancy,
467 .dirty_sectors = u.dirty_sectors,
468 .cached_sectors = u.cached_sectors,
469 .io_time[READ] = u.read_time,
470 .io_time[WRITE] = u.write_time,
474 SET_BCH_ALLOC_V4_BACKPOINTERS_START(out, BCH_ALLOC_V4_U64s);
478 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
480 unsigned bytes = k.k->type == KEY_TYPE_alloc_v4
482 : sizeof(struct bkey_i_alloc_v4);
483 struct bkey_i_alloc_v4 *ret;
486 * Reserve space for one more backpointer here:
487 * Not sketchy at doing it this way, nope...
489 ret = bch2_trans_kmalloc(trans, bytes + sizeof(struct bch_backpointer));
493 if (k.k->type == KEY_TYPE_alloc_v4) {
494 bkey_reassemble(&ret->k_i, k);
496 if (BCH_ALLOC_V4_BACKPOINTERS_START(&ret->v) < BCH_ALLOC_V4_U64s) {
497 struct bch_backpointer *src, *dst;
499 src = alloc_v4_backpointers(&ret->v);
500 SET_BCH_ALLOC_V4_BACKPOINTERS_START(&ret->v, BCH_ALLOC_V4_U64s);
501 dst = alloc_v4_backpointers(&ret->v);
503 memmove(dst, src, BCH_ALLOC_V4_NR_BACKPOINTERS(&ret->v) *
504 sizeof(struct bch_backpointer));
505 memset(src, 0, dst - src);
506 set_alloc_v4_u64s(ret);
509 bkey_alloc_v4_init(&ret->k_i);
511 bch2_alloc_to_v4(k, &ret->v);
516 int bch2_alloc_read(struct bch_fs *c)
518 struct btree_trans trans;
519 struct btree_iter iter;
521 struct bch_alloc_v4 a;
525 bch2_trans_init(&trans, c, 0, 0);
527 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
528 BTREE_ITER_PREFETCH, k, ret) {
530 * Not a fsck error because this is checked/repaired by
531 * bch2_check_alloc_key() which runs later:
533 if (!bch2_dev_bucket_exists(c, k.k->p))
536 ca = bch_dev_bkey_exists(c, k.k->p.inode);
537 bch2_alloc_to_v4(k, &a);
539 *bucket_gen(ca, k.k->p.offset) = a.gen;
541 bch2_trans_iter_exit(&trans, &iter);
543 bch2_trans_exit(&trans);
546 bch_err(c, "error reading alloc info: %i", ret);
551 /* Free space/discard btree: */
553 static int bch2_bucket_do_index(struct btree_trans *trans,
554 struct bkey_s_c alloc_k,
555 const struct bch_alloc_v4 *a,
558 struct bch_fs *c = trans->c;
559 struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
560 struct btree_iter iter;
564 enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
565 enum bch_bkey_type new_type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
566 struct printbuf buf = PRINTBUF;
569 if (a->data_type != BCH_DATA_free &&
570 a->data_type != BCH_DATA_need_discard)
573 k = bch2_trans_kmalloc(trans, sizeof(*k));
578 k->k.type = new_type;
580 switch (a->data_type) {
582 btree = BTREE_ID_freespace;
583 k->k.p = alloc_freespace_pos(alloc_k.k->p, *a);
584 bch2_key_resize(&k->k, 1);
586 case BCH_DATA_need_discard:
587 btree = BTREE_ID_need_discard;
588 k->k.p = alloc_k.k->p;
594 bch2_trans_iter_init(trans, &iter, btree,
595 bkey_start_pos(&k->k),
597 old = bch2_btree_iter_peek_slot(&iter);
602 if (ca->mi.freespace_initialized &&
603 bch2_trans_inconsistent_on(old.k->type != old_type, trans,
604 "incorrect key when %s %s btree (got %s should be %s)\n"
606 set ? "setting" : "clearing",
607 bch2_btree_ids[btree],
608 bch2_bkey_types[old.k->type],
609 bch2_bkey_types[old_type],
610 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
615 ret = bch2_trans_update(trans, &iter, k, 0);
617 bch2_trans_iter_exit(trans, &iter);
622 int bch2_trans_mark_alloc(struct btree_trans *trans,
623 enum btree_id btree_id, unsigned level,
624 struct bkey_s_c old, struct bkey_i *new,
627 struct bch_fs *c = trans->c;
628 struct bch_alloc_v4 old_a, *new_a;
629 u64 old_lru, new_lru;
633 * Deletion only happens in the device removal path, with
634 * BTREE_TRIGGER_NORUN:
636 BUG_ON(new->k.type != KEY_TYPE_alloc_v4);
638 bch2_alloc_to_v4(old, &old_a);
639 new_a = &bkey_i_to_alloc_v4(new)->v;
641 new_a->data_type = alloc_data_type(*new_a, new_a->data_type);
643 if (new_a->dirty_sectors > old_a.dirty_sectors ||
644 new_a->cached_sectors > old_a.cached_sectors) {
645 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
646 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
647 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
648 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
651 if (data_type_is_empty(new_a->data_type) &&
652 BCH_ALLOC_V4_NEED_INC_GEN(new_a) &&
653 !bch2_bucket_is_open_safe(c, new->k.p.inode, new->k.p.offset)) {
655 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
658 if (old_a.data_type != new_a->data_type ||
659 (new_a->data_type == BCH_DATA_free &&
660 alloc_freespace_genbits(old_a) != alloc_freespace_genbits(*new_a))) {
661 ret = bch2_bucket_do_index(trans, old, &old_a, false) ?:
662 bch2_bucket_do_index(trans, bkey_i_to_s_c(new), new_a, true);
667 if (new_a->data_type == BCH_DATA_cached &&
668 !new_a->io_time[READ])
669 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
671 old_lru = alloc_lru_idx(old_a);
672 new_lru = alloc_lru_idx(*new_a);
674 if (old_lru != new_lru) {
675 ret = bch2_lru_change(trans, new->k.p.inode, new->k.p.offset,
676 old_lru, &new_lru, old);
680 if (new_a->data_type == BCH_DATA_cached)
681 new_a->io_time[READ] = new_lru;
687 static int bch2_check_alloc_key(struct btree_trans *trans,
688 struct btree_iter *alloc_iter)
690 struct bch_fs *c = trans->c;
692 struct btree_iter discard_iter, freespace_iter;
693 struct bch_alloc_v4 a;
694 unsigned discard_key_type, freespace_key_type;
695 struct bkey_s_c alloc_k, k;
696 struct printbuf buf = PRINTBUF;
697 struct printbuf buf2 = PRINTBUF;
700 alloc_k = bch2_btree_iter_peek(alloc_iter);
704 ret = bkey_err(alloc_k);
708 if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_k.k->p), c,
709 "alloc key for invalid device:bucket %llu:%llu",
710 alloc_k.k->p.inode, alloc_k.k->p.offset))
711 return bch2_btree_delete_at(trans, alloc_iter, 0);
713 ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
714 if (!ca->mi.freespace_initialized)
717 bch2_alloc_to_v4(alloc_k, &a);
719 discard_key_type = a.data_type == BCH_DATA_need_discard
721 freespace_key_type = a.data_type == BCH_DATA_free
724 bch2_trans_iter_init(trans, &discard_iter, BTREE_ID_need_discard,
726 bch2_trans_iter_init(trans, &freespace_iter, BTREE_ID_freespace,
727 alloc_freespace_pos(alloc_k.k->p, a), 0);
729 k = bch2_btree_iter_peek_slot(&discard_iter);
734 if (fsck_err_on(k.k->type != discard_key_type, c,
735 "incorrect key in need_discard btree (got %s should be %s)\n"
737 bch2_bkey_types[k.k->type],
738 bch2_bkey_types[discard_key_type],
739 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
740 struct bkey_i *update =
741 bch2_trans_kmalloc(trans, sizeof(*update));
743 ret = PTR_ERR_OR_ZERO(update);
747 bkey_init(&update->k);
748 update->k.type = discard_key_type;
749 update->k.p = discard_iter.pos;
751 ret = bch2_trans_update(trans, &discard_iter, update, 0);
756 k = bch2_btree_iter_peek_slot(&freespace_iter);
761 if (fsck_err_on(k.k->type != freespace_key_type, c,
762 "incorrect key in freespace btree (got %s should be %s)\n"
764 bch2_bkey_types[k.k->type],
765 bch2_bkey_types[freespace_key_type],
766 (printbuf_reset(&buf),
767 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
768 struct bkey_i *update =
769 bch2_trans_kmalloc(trans, sizeof(*update));
771 ret = PTR_ERR_OR_ZERO(update);
775 bkey_init(&update->k);
776 update->k.type = freespace_key_type;
777 update->k.p = freespace_iter.pos;
778 bch2_key_resize(&update->k, 1);
780 ret = bch2_trans_update(trans, &freespace_iter, update, 0);
786 bch2_trans_iter_exit(trans, &freespace_iter);
787 bch2_trans_iter_exit(trans, &discard_iter);
788 printbuf_exit(&buf2);
793 static int bch2_check_discard_freespace_key(struct btree_trans *trans,
794 struct btree_iter *iter)
796 struct bch_fs *c = trans->c;
797 struct btree_iter alloc_iter;
798 struct bkey_s_c k, freespace_k;
799 struct bch_alloc_v4 a;
802 enum bch_data_type state = iter->btree_id == BTREE_ID_need_discard
803 ? BCH_DATA_need_discard
805 struct printbuf buf = PRINTBUF;
808 freespace_k = bch2_btree_iter_peek(iter);
812 ret = bkey_err(freespace_k);
817 pos.offset &= ~(~0ULL << 56);
818 genbits = iter->pos.offset & (~0ULL << 56);
820 bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
822 if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
823 "entry in %s btree for nonexistant dev:bucket %llu:%llu",
824 bch2_btree_ids[iter->btree_id], pos.inode, pos.offset))
827 k = bch2_btree_iter_peek_slot(&alloc_iter);
832 bch2_alloc_to_v4(k, &a);
834 if (fsck_err_on(a.data_type != state ||
835 (state == BCH_DATA_free &&
836 genbits != alloc_freespace_genbits(a)), c,
837 "%s\n incorrectly set in %s index (free %u, genbits %llu should be %llu)",
838 (bch2_bkey_val_to_text(&buf, c, k), buf.buf),
839 bch2_btree_ids[iter->btree_id],
840 a.data_type == state,
841 genbits >> 56, alloc_freespace_genbits(a) >> 56))
846 bch2_trans_iter_exit(trans, &alloc_iter);
850 ret = bch2_btree_delete_extent_at(trans, iter,
851 iter->btree_id == BTREE_ID_freespace ? 1 : 0, 0);
855 int bch2_check_alloc_info(struct bch_fs *c)
857 struct btree_trans trans;
858 struct btree_iter iter;
862 bch2_trans_init(&trans, c, 0, 0);
864 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
865 BTREE_ITER_PREFETCH, k, ret) {
866 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
867 bch2_check_alloc_key(&trans, &iter));
871 bch2_trans_iter_exit(&trans, &iter);
876 bch2_trans_iter_init(&trans, &iter, BTREE_ID_need_discard, POS_MIN,
877 BTREE_ITER_PREFETCH);
879 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
880 bch2_check_discard_freespace_key(&trans, &iter));
884 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
886 bch2_trans_iter_exit(&trans, &iter);
891 bch2_trans_iter_init(&trans, &iter, BTREE_ID_freespace, POS_MIN,
892 BTREE_ITER_PREFETCH);
894 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
895 bch2_check_discard_freespace_key(&trans, &iter));
899 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
901 bch2_trans_iter_exit(&trans, &iter);
903 bch2_trans_exit(&trans);
904 return ret < 0 ? ret : 0;
907 static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
908 struct btree_iter *alloc_iter)
910 struct bch_fs *c = trans->c;
911 struct btree_iter lru_iter;
912 struct bch_alloc_v4 a;
913 struct bkey_s_c alloc_k, k;
914 struct printbuf buf = PRINTBUF;
915 struct printbuf buf2 = PRINTBUF;
918 alloc_k = bch2_btree_iter_peek(alloc_iter);
922 ret = bkey_err(alloc_k);
926 bch2_alloc_to_v4(alloc_k, &a);
928 if (a.data_type != BCH_DATA_cached)
931 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
932 POS(alloc_k.k->p.inode, a.io_time[READ]), 0);
934 k = bch2_btree_iter_peek_slot(&lru_iter);
939 if (fsck_err_on(!a.io_time[READ], c,
940 "cached bucket with read_time 0\n"
942 (printbuf_reset(&buf),
943 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) ||
944 fsck_err_on(k.k->type != KEY_TYPE_lru ||
945 le64_to_cpu(bkey_s_c_to_lru(k).v->idx) != alloc_k.k->p.offset, c,
946 "incorrect/missing lru entry\n"
949 (printbuf_reset(&buf),
950 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
951 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf))) {
952 u64 read_time = a.io_time[READ];
954 if (!a.io_time[READ])
955 a.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
957 ret = bch2_lru_set(trans,
964 if (a.io_time[READ] != read_time) {
965 struct bkey_i_alloc_v4 *a_mut =
966 bch2_alloc_to_v4_mut(trans, alloc_k);
967 ret = PTR_ERR_OR_ZERO(a_mut);
971 a_mut->v.io_time[READ] = a.io_time[READ];
972 ret = bch2_trans_update(trans, alloc_iter,
973 &a_mut->k_i, BTREE_TRIGGER_NORUN);
980 bch2_trans_iter_exit(trans, &lru_iter);
981 printbuf_exit(&buf2);
986 int bch2_check_alloc_to_lru_refs(struct bch_fs *c)
988 struct btree_trans trans;
989 struct btree_iter iter;
993 bch2_trans_init(&trans, c, 0, 0);
995 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
996 BTREE_ITER_PREFETCH, k, ret) {
997 ret = __bch2_trans_do(&trans, NULL, NULL,
999 BTREE_INSERT_LAZY_RW,
1000 bch2_check_alloc_to_lru_ref(&trans, &iter));
1004 bch2_trans_iter_exit(&trans, &iter);
1006 bch2_trans_exit(&trans);
1007 return ret < 0 ? ret : 0;
1010 static int bch2_clear_need_discard(struct btree_trans *trans, struct bpos pos,
1011 struct bch_dev *ca, bool *discard_done)
1013 struct bch_fs *c = trans->c;
1014 struct btree_iter iter;
1016 struct bkey_i_alloc_v4 *a;
1017 struct printbuf buf = PRINTBUF;
1020 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, pos,
1022 k = bch2_btree_iter_peek_slot(&iter);
1027 a = bch2_alloc_to_v4_mut(trans, k);
1028 ret = PTR_ERR_OR_ZERO(a);
1032 if (BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
1034 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1038 if (bch2_trans_inconsistent_on(a->v.journal_seq > c->journal.flushed_seq_ondisk, trans,
1039 "clearing need_discard but journal_seq %llu > flushed_seq %llu\n"
1042 c->journal.flushed_seq_ondisk,
1043 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1048 if (bch2_trans_inconsistent_on(a->v.data_type != BCH_DATA_need_discard, trans,
1049 "bucket incorrectly set in need_discard btree\n"
1051 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
1056 if (!*discard_done && ca->mi.discard && !c->opts.nochanges) {
1058 * This works without any other locks because this is the only
1059 * thread that removes items from the need_discard tree
1061 bch2_trans_unlock(trans);
1062 blkdev_issue_discard(ca->disk_sb.bdev,
1063 k.k->p.offset * ca->mi.bucket_size,
1066 *discard_done = true;
1068 ret = bch2_trans_relock(trans) ? 0 : -EINTR;
1073 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
1074 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
1076 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
1078 bch2_trans_iter_exit(trans, &iter);
1079 printbuf_exit(&buf);
1083 static void bch2_do_discards_work(struct work_struct *work)
1085 struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
1086 struct bch_dev *ca = NULL;
1087 struct btree_trans trans;
1088 struct btree_iter iter;
1090 u64 seen = 0, open = 0, need_journal_commit = 0, discarded = 0;
1093 bch2_trans_init(&trans, c, 0, 0);
1095 for_each_btree_key(&trans, iter, BTREE_ID_need_discard,
1096 POS_MIN, 0, k, ret) {
1097 bool discard_done = false;
1099 if (ca && k.k->p.inode != ca->dev_idx) {
1100 percpu_ref_put(&ca->io_ref);
1105 ca = bch_dev_bkey_exists(c, k.k->p.inode);
1106 if (!percpu_ref_tryget(&ca->io_ref)) {
1108 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
1115 if (bch2_bucket_is_open_safe(c, k.k->p.inode, k.k->p.offset)) {
1120 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
1121 c->journal.flushed_seq_ondisk,
1122 k.k->p.inode, k.k->p.offset)) {
1123 need_journal_commit++;
1127 ret = __bch2_trans_do(&trans, NULL, NULL,
1128 BTREE_INSERT_USE_RESERVE|
1129 BTREE_INSERT_NOFAIL,
1130 bch2_clear_need_discard(&trans, k.k->p, ca, &discard_done));
1134 this_cpu_inc(c->counters[BCH_COUNTER_bucket_discard]);
1137 bch2_trans_iter_exit(&trans, &iter);
1140 percpu_ref_put(&ca->io_ref);
1142 bch2_trans_exit(&trans);
1144 if (need_journal_commit * 2 > seen)
1145 bch2_journal_flush_async(&c->journal, NULL);
1147 percpu_ref_put(&c->writes);
1149 trace_discard_buckets(c, seen, open, need_journal_commit, discarded, ret);
1152 void bch2_do_discards(struct bch_fs *c)
1154 if (percpu_ref_tryget(&c->writes) &&
1155 !queue_work(system_long_wq, &c->discard_work))
1156 percpu_ref_put(&c->writes);
1159 static int invalidate_one_bucket(struct btree_trans *trans, struct bch_dev *ca)
1161 struct bch_fs *c = trans->c;
1162 struct btree_iter lru_iter, alloc_iter = { NULL };
1164 struct bkey_i_alloc_v4 *a;
1166 struct printbuf buf = PRINTBUF;
1169 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
1170 POS(ca->dev_idx, 0), 0);
1172 k = bch2_btree_iter_peek(&lru_iter);
1177 if (!k.k || k.k->p.inode != ca->dev_idx)
1180 if (k.k->type != KEY_TYPE_lru) {
1181 prt_printf(&buf, "non lru key in lru btree:\n ");
1182 bch2_bkey_val_to_text(&buf, c, k);
1184 if (!test_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags)) {
1185 bch_err(c, "%s", buf.buf);
1186 bch2_btree_iter_advance(&lru_iter);
1189 bch2_trans_inconsistent(trans, "%s", buf.buf);
1195 idx = k.k->p.offset;
1196 bucket = le64_to_cpu(bkey_s_c_to_lru(k).v->idx);
1198 a = bch2_trans_start_alloc_update(trans, &alloc_iter,
1199 POS(ca->dev_idx, bucket));
1200 ret = PTR_ERR_OR_ZERO(a);
1204 if (idx != alloc_lru_idx(a->v)) {
1205 prt_printf(&buf, "alloc key does not point back to lru entry when invalidating bucket:\n ");
1206 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&a->k_i));
1207 prt_printf(&buf, "\n ");
1208 bch2_bkey_val_to_text(&buf, c, k);
1210 if (!test_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags)) {
1211 bch_err(c, "%s", buf.buf);
1212 bch2_btree_iter_advance(&lru_iter);
1215 bch2_trans_inconsistent(trans, "%s", buf.buf);
1221 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1224 a->v.dirty_sectors = 0;
1225 a->v.cached_sectors = 0;
1226 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1227 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1229 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1230 BTREE_TRIGGER_BUCKET_INVALIDATE);
1234 trace_invalidate_bucket(c, a->k.p.inode, a->k.p.offset);
1236 bch2_trans_iter_exit(trans, &alloc_iter);
1237 bch2_trans_iter_exit(trans, &lru_iter);
1238 printbuf_exit(&buf);
1242 static void bch2_do_invalidates_work(struct work_struct *work)
1244 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
1246 struct btree_trans trans;
1250 bch2_trans_init(&trans, c, 0, 0);
1252 for_each_member_device(ca, c, i) {
1253 s64 nr_to_invalidate =
1254 should_invalidate_buckets(ca, bch2_dev_usage_read(ca));
1256 while (nr_to_invalidate-- >= 0) {
1257 ret = __bch2_trans_do(&trans, NULL, NULL,
1258 BTREE_INSERT_USE_RESERVE|
1259 BTREE_INSERT_NOFAIL,
1260 invalidate_one_bucket(&trans, ca));
1264 this_cpu_inc(c->counters[BCH_COUNTER_bucket_invalidate]);
1268 bch2_trans_exit(&trans);
1269 percpu_ref_put(&c->writes);
1272 void bch2_do_invalidates(struct bch_fs *c)
1274 if (percpu_ref_tryget(&c->writes))
1275 queue_work(system_long_wq, &c->invalidate_work);
1278 static int bucket_freespace_init(struct btree_trans *trans, struct btree_iter *iter)
1280 struct bch_alloc_v4 a;
1284 k = bch2_btree_iter_peek_slot(iter);
1289 bch2_alloc_to_v4(k, &a);
1290 return bch2_bucket_do_index(trans, k, &a, true);
1293 static int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca)
1295 struct btree_trans trans;
1296 struct btree_iter iter;
1298 struct bch_member *m;
1301 bch2_trans_init(&trans, c, 0, 0);
1303 for_each_btree_key(&trans, iter, BTREE_ID_alloc,
1304 POS(ca->dev_idx, ca->mi.first_bucket),
1306 BTREE_ITER_PREFETCH, k, ret) {
1307 if (iter.pos.offset >= ca->mi.nbuckets)
1310 ret = __bch2_trans_do(&trans, NULL, NULL,
1311 BTREE_INSERT_LAZY_RW,
1312 bucket_freespace_init(&trans, &iter));
1316 bch2_trans_iter_exit(&trans, &iter);
1318 bch2_trans_exit(&trans);
1321 bch_err(ca, "error initializing free space: %i", ret);
1325 mutex_lock(&c->sb_lock);
1326 m = bch2_sb_get_members(c->disk_sb.sb)->members + ca->dev_idx;
1327 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
1328 mutex_unlock(&c->sb_lock);
1333 int bch2_fs_freespace_init(struct bch_fs *c)
1338 bool doing_init = false;
1341 * We can crash during the device add path, so we need to check this on
1345 for_each_member_device(ca, c, i) {
1346 if (ca->mi.freespace_initialized)
1350 bch_info(c, "initializing freespace");
1354 ret = bch2_dev_freespace_init(c, ca);
1356 percpu_ref_put(&ca->ref);
1362 mutex_lock(&c->sb_lock);
1363 bch2_write_super(c);
1364 mutex_unlock(&c->sb_lock);
1366 bch_verbose(c, "done initializing freespace");
1372 /* Bucket IO clocks: */
1374 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
1375 size_t bucket_nr, int rw)
1377 struct bch_fs *c = trans->c;
1378 struct btree_iter iter;
1379 struct bkey_i_alloc_v4 *a;
1383 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
1384 ret = PTR_ERR_OR_ZERO(a);
1388 now = atomic64_read(&c->io_clock[rw].now);
1389 if (a->v.io_time[rw] == now)
1392 a->v.io_time[rw] = now;
1394 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1395 bch2_trans_commit(trans, NULL, NULL, 0);
1397 bch2_trans_iter_exit(trans, &iter);
1401 /* Startup/shutdown (ro/rw): */
1403 void bch2_recalc_capacity(struct bch_fs *c)
1406 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
1407 unsigned bucket_size_max = 0;
1408 unsigned long ra_pages = 0;
1411 lockdep_assert_held(&c->state_lock);
1413 for_each_online_member(ca, c, i) {
1414 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
1416 ra_pages += bdi->ra_pages;
1419 bch2_set_ra_pages(c, ra_pages);
1421 for_each_rw_member(ca, c, i) {
1422 u64 dev_reserve = 0;
1425 * We need to reserve buckets (from the number
1426 * of currently available buckets) against
1427 * foreground writes so that mainly copygc can
1428 * make forward progress.
1430 * We need enough to refill the various reserves
1431 * from scratch - copygc will use its entire
1432 * reserve all at once, then run against when
1433 * its reserve is refilled (from the formerly
1434 * available buckets).
1436 * This reserve is just used when considering if
1437 * allocations for foreground writes must wait -
1438 * not -ENOSPC calculations.
1441 dev_reserve += ca->nr_btree_reserve * 2;
1442 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
1444 dev_reserve += 1; /* btree write point */
1445 dev_reserve += 1; /* copygc write point */
1446 dev_reserve += 1; /* rebalance write point */
1448 dev_reserve *= ca->mi.bucket_size;
1450 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
1451 ca->mi.first_bucket);
1453 reserved_sectors += dev_reserve * 2;
1455 bucket_size_max = max_t(unsigned, bucket_size_max,
1456 ca->mi.bucket_size);
1459 gc_reserve = c->opts.gc_reserve_bytes
1460 ? c->opts.gc_reserve_bytes >> 9
1461 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
1463 reserved_sectors = max(gc_reserve, reserved_sectors);
1465 reserved_sectors = min(reserved_sectors, capacity);
1467 c->capacity = capacity - reserved_sectors;
1469 c->bucket_size_max = bucket_size_max;
1471 /* Wake up case someone was waiting for buckets */
1472 closure_wake_up(&c->freelist_wait);
1475 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
1477 struct open_bucket *ob;
1480 for (ob = c->open_buckets;
1481 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1483 spin_lock(&ob->lock);
1484 if (ob->valid && !ob->on_partial_list &&
1485 ob->dev == ca->dev_idx)
1487 spin_unlock(&ob->lock);
1493 /* device goes ro: */
1494 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
1498 /* First, remove device from allocation groups: */
1500 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1501 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1504 * Capacity is calculated based off of devices in allocation groups:
1506 bch2_recalc_capacity(c);
1508 /* Next, close write points that point to this device... */
1509 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1510 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1512 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1513 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1514 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1516 mutex_lock(&c->btree_reserve_cache_lock);
1517 while (c->btree_reserve_cache_nr) {
1518 struct btree_alloc *a =
1519 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1521 bch2_open_buckets_put(c, &a->ob);
1523 mutex_unlock(&c->btree_reserve_cache_lock);
1526 struct open_bucket *ob;
1528 spin_lock(&c->freelist_lock);
1529 if (!ca->open_buckets_partial_nr) {
1530 spin_unlock(&c->freelist_lock);
1533 ob = c->open_buckets +
1534 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1535 ob->on_partial_list = false;
1536 spin_unlock(&c->freelist_lock);
1538 bch2_open_bucket_put(c, ob);
1541 bch2_ec_stop_dev(c, ca);
1544 * Wake up threads that were blocked on allocation, so they can notice
1545 * the device can no longer be removed and the capacity has changed:
1547 closure_wake_up(&c->freelist_wait);
1550 * journal_res_get() can block waiting for free space in the journal -
1551 * it needs to notice there may not be devices to allocate from anymore:
1553 wake_up(&c->journal.wait);
1555 /* Now wait for any in flight writes: */
1557 closure_wait_event(&c->open_buckets_wait,
1558 !bch2_dev_has_open_write_point(c, ca));
1561 /* device goes rw: */
1562 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1566 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1567 if (ca->mi.data_allowed & (1 << i))
1568 set_bit(ca->dev_idx, c->rw_devs[i].d);
1571 void bch2_fs_allocator_background_init(struct bch_fs *c)
1573 spin_lock_init(&c->freelist_lock);
1574 INIT_WORK(&c->discard_work, bch2_do_discards_work);
1575 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);