1 // SPDX-License-Identifier: GPL-2.0
3 #include "alloc_background.h"
4 #include "alloc_foreground.h"
5 #include "btree_cache.h"
7 #include "btree_key_cache.h"
8 #include "btree_update.h"
9 #include "btree_update_interior.h"
12 #include "buckets_waiting_for_journal.h"
21 #include <linux/kthread.h>
22 #include <linux/math64.h>
23 #include <linux/random.h>
24 #include <linux/rculist.h>
25 #include <linux/rcupdate.h>
26 #include <linux/sched/task.h>
27 #include <linux/sort.h>
28 #include <trace/events/bcachefs.h>
30 /* Persistent alloc info: */
32 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
33 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
38 struct bkey_alloc_unpacked {
47 #define x(_name, _bits) u##_bits _name;
52 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
53 const void **p, unsigned field)
55 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
58 if (!(a->fields & (1 << field)))
63 v = *((const u8 *) *p);
82 static inline void alloc_field_v1_put(struct bkey_i_alloc *a, void **p,
83 unsigned field, u64 v)
85 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
90 a->v.fields |= 1 << field;
97 *((__le16 *) *p) = cpu_to_le16(v);
100 *((__le32 *) *p) = cpu_to_le32(v);
103 *((__le64 *) *p) = cpu_to_le64(v);
112 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
115 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
116 const void *d = in->data;
121 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
122 BCH_ALLOC_FIELDS_V1()
126 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
129 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
130 const u8 *in = a.v->data;
131 const u8 *end = bkey_val_end(a);
132 unsigned fieldnr = 0;
137 out->oldest_gen = a.v->oldest_gen;
138 out->data_type = a.v->data_type;
140 #define x(_name, _bits) \
141 if (fieldnr < a.v->nr_fields) { \
142 ret = bch2_varint_decode_fast(in, end, &v); \
150 if (v != out->_name) \
154 BCH_ALLOC_FIELDS_V2()
159 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
162 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
163 const u8 *in = a.v->data;
164 const u8 *end = bkey_val_end(a);
165 unsigned fieldnr = 0;
170 out->oldest_gen = a.v->oldest_gen;
171 out->data_type = a.v->data_type;
172 out->need_discard = BCH_ALLOC_V3_NEED_DISCARD(a.v);
173 out->need_inc_gen = BCH_ALLOC_V3_NEED_INC_GEN(a.v);
174 out->journal_seq = le64_to_cpu(a.v->journal_seq);
176 #define x(_name, _bits) \
177 if (fieldnr < a.v->nr_fields) { \
178 ret = bch2_varint_decode_fast(in, end, &v); \
186 if (v != out->_name) \
190 BCH_ALLOC_FIELDS_V2()
195 static struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
197 struct bkey_alloc_unpacked ret = {
199 .bucket = k.k->p.offset,
205 bch2_alloc_unpack_v1(&ret, k);
207 case KEY_TYPE_alloc_v2:
208 bch2_alloc_unpack_v2(&ret, k);
210 case KEY_TYPE_alloc_v3:
211 bch2_alloc_unpack_v3(&ret, k);
218 void bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
220 if (k.k->type == KEY_TYPE_alloc_v4) {
221 *out = *bkey_s_c_to_alloc_v4(k).v;
223 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
225 *out = (struct bch_alloc_v4) {
226 .journal_seq = u.journal_seq,
227 .flags = u.need_discard,
229 .oldest_gen = u.oldest_gen,
230 .data_type = u.data_type,
231 .stripe_redundancy = u.stripe_redundancy,
232 .dirty_sectors = u.dirty_sectors,
233 .cached_sectors = u.cached_sectors,
234 .io_time[READ] = u.read_time,
235 .io_time[WRITE] = u.write_time,
241 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
243 struct bkey_i_alloc_v4 *ret;
245 if (k.k->type == KEY_TYPE_alloc_v4) {
246 ret = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
248 bkey_reassemble(&ret->k_i, k);
250 ret = bch2_trans_kmalloc(trans, sizeof(*ret));
252 bkey_alloc_v4_init(&ret->k_i);
254 bch2_alloc_to_v4(k, &ret->v);
260 struct bkey_i_alloc_v4 *
261 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
265 struct bkey_i_alloc_v4 *a;
268 bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos,
269 BTREE_ITER_WITH_UPDATES|
272 k = bch2_btree_iter_peek_slot(iter);
275 bch2_trans_iter_exit(trans, iter);
279 a = bch2_alloc_to_v4_mut(trans, k);
281 bch2_trans_iter_exit(trans, iter);
285 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
287 unsigned i, bytes = offsetof(struct bch_alloc, data);
289 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
290 if (a->fields & (1 << i))
291 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
293 return DIV_ROUND_UP(bytes, sizeof(u64));
296 int bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k,
297 int rw, struct printbuf *err)
299 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
301 /* allow for unknown fields */
302 if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v)) {
303 prt_printf(err, "incorrect value size (%zu < %u)",
304 bkey_val_u64s(a.k), bch_alloc_v1_val_u64s(a.v));
311 int bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k,
312 int rw, struct printbuf *err)
314 struct bkey_alloc_unpacked u;
316 if (bch2_alloc_unpack_v2(&u, k)) {
317 prt_printf(err, "unpack error");
324 int bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k,
325 int rw, struct printbuf *err)
327 struct bkey_alloc_unpacked u;
329 if (bch2_alloc_unpack_v3(&u, k)) {
330 prt_printf(err, "unpack error");
337 int bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k,
338 int rw, struct printbuf *err)
340 struct bkey_s_c_alloc_v4 a = bkey_s_c_to_alloc_v4(k);
342 if (bkey_val_bytes(k.k) != sizeof(struct bch_alloc_v4)) {
343 prt_printf(err, "bad val size (%zu != %zu)",
344 bkey_val_bytes(k.k), sizeof(struct bch_alloc_v4));
349 if (alloc_data_type(*a.v, a.v->data_type) != a.v->data_type) {
350 prt_printf(err, "invalid data type (got %u should be %u)",
351 a.v->data_type, alloc_data_type(*a.v, a.v->data_type));
355 switch (a.v->data_type) {
357 case BCH_DATA_need_gc_gens:
358 case BCH_DATA_need_discard:
359 if (a.v->dirty_sectors ||
360 a.v->cached_sectors ||
362 prt_printf(err, "empty data type free but have data");
367 case BCH_DATA_journal:
370 case BCH_DATA_parity:
371 if (!a.v->dirty_sectors) {
372 prt_printf(err, "data_type %s but dirty_sectors==0",
373 bch2_data_types[a.v->data_type]);
377 case BCH_DATA_cached:
378 if (!a.v->cached_sectors ||
379 a.v->dirty_sectors ||
381 prt_printf(err, "data type inconsistency");
385 if (!a.v->io_time[READ] &&
386 test_bit(BCH_FS_CHECK_ALLOC_TO_LRU_REFS_DONE, &c->flags)) {
387 prt_printf(err, "cached bucket with read_time == 0");
391 case BCH_DATA_stripe:
393 prt_printf(err, "data_type %s but stripe==0",
394 bch2_data_types[a.v->data_type]);
404 void bch2_alloc_v4_swab(struct bkey_s k)
406 struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
408 a->journal_seq = swab64(a->journal_seq);
409 a->flags = swab32(a->flags);
410 a->dirty_sectors = swab32(a->dirty_sectors);
411 a->cached_sectors = swab32(a->cached_sectors);
412 a->io_time[0] = swab64(a->io_time[0]);
413 a->io_time[1] = swab64(a->io_time[1]);
414 a->stripe = swab32(a->stripe);
415 a->nr_external_backpointers = swab32(a->nr_external_backpointers);
418 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
420 struct bch_alloc_v4 a;
422 bch2_alloc_to_v4(k, &a);
424 prt_printf(out, "gen %u oldest_gen %u data_type %s journal_seq %llu need_discard %llu need_inc_gen %llu",
425 a.gen, a.oldest_gen, bch2_data_types[a.data_type],
427 BCH_ALLOC_V4_NEED_DISCARD(&a),
428 BCH_ALLOC_V4_NEED_INC_GEN(&a));
429 prt_printf(out, " dirty_sectors %u", a.dirty_sectors);
430 prt_printf(out, " cached_sectors %u", a.cached_sectors);
431 prt_printf(out, " stripe %u", a.stripe);
432 prt_printf(out, " stripe_redundancy %u", a.stripe_redundancy);
433 prt_printf(out, " read_time %llu", a.io_time[READ]);
434 prt_printf(out, " write_time %llu", a.io_time[WRITE]);
437 int bch2_alloc_read(struct bch_fs *c)
439 struct btree_trans trans;
440 struct btree_iter iter;
442 struct bch_alloc_v4 a;
446 bch2_trans_init(&trans, c, 0, 0);
448 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
449 BTREE_ITER_PREFETCH, k, ret) {
451 * Not a fsck error because this is checked/repaired by
452 * bch2_check_alloc_key() which runs later:
454 if (!bch2_dev_bucket_exists(c, k.k->p))
457 ca = bch_dev_bkey_exists(c, k.k->p.inode);
458 bch2_alloc_to_v4(k, &a);
460 *bucket_gen(ca, k.k->p.offset) = a.gen;
462 bch2_trans_iter_exit(&trans, &iter);
464 bch2_trans_exit(&trans);
467 bch_err(c, "error reading alloc info: %i", ret);
472 /* Free space/discard btree: */
474 static int bch2_bucket_do_index(struct btree_trans *trans,
475 struct bkey_s_c alloc_k,
476 const struct bch_alloc_v4 *a,
479 struct bch_fs *c = trans->c;
480 struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
481 struct btree_iter iter;
485 enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
486 enum bch_bkey_type new_type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
487 struct printbuf buf = PRINTBUF;
490 if (a->data_type != BCH_DATA_free &&
491 a->data_type != BCH_DATA_need_discard)
494 k = bch2_trans_kmalloc(trans, sizeof(*k));
499 k->k.type = new_type;
501 switch (a->data_type) {
503 btree = BTREE_ID_freespace;
504 k->k.p = alloc_freespace_pos(alloc_k.k->p, *a);
505 bch2_key_resize(&k->k, 1);
507 case BCH_DATA_need_discard:
508 btree = BTREE_ID_need_discard;
509 k->k.p = alloc_k.k->p;
515 bch2_trans_iter_init(trans, &iter, btree,
516 bkey_start_pos(&k->k),
518 old = bch2_btree_iter_peek_slot(&iter);
523 if (ca->mi.freespace_initialized &&
524 bch2_trans_inconsistent_on(old.k->type != old_type, trans,
525 "incorrect key when %s %s btree (got %s should be %s)\n"
527 set ? "setting" : "clearing",
528 bch2_btree_ids[btree],
529 bch2_bkey_types[old.k->type],
530 bch2_bkey_types[old_type],
531 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
536 ret = bch2_trans_update(trans, &iter, k, 0);
538 bch2_trans_iter_exit(trans, &iter);
543 int bch2_trans_mark_alloc(struct btree_trans *trans,
544 enum btree_id btree_id, unsigned level,
545 struct bkey_s_c old, struct bkey_i *new,
548 struct bch_fs *c = trans->c;
549 struct bch_alloc_v4 old_a, *new_a;
550 u64 old_lru, new_lru;
554 * Deletion only happens in the device removal path, with
555 * BTREE_TRIGGER_NORUN:
557 BUG_ON(new->k.type != KEY_TYPE_alloc_v4);
559 bch2_alloc_to_v4(old, &old_a);
560 new_a = &bkey_i_to_alloc_v4(new)->v;
562 new_a->data_type = alloc_data_type(*new_a, new_a->data_type);
564 if (new_a->dirty_sectors > old_a.dirty_sectors ||
565 new_a->cached_sectors > old_a.cached_sectors) {
566 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
567 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
568 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
569 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
572 if (data_type_is_empty(new_a->data_type) &&
573 BCH_ALLOC_V4_NEED_INC_GEN(new_a) &&
574 !bch2_bucket_is_open_safe(c, new->k.p.inode, new->k.p.offset)) {
576 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
579 if (old_a.data_type != new_a->data_type ||
580 (new_a->data_type == BCH_DATA_free &&
581 alloc_freespace_genbits(old_a) != alloc_freespace_genbits(*new_a))) {
582 ret = bch2_bucket_do_index(trans, old, &old_a, false) ?:
583 bch2_bucket_do_index(trans, bkey_i_to_s_c(new), new_a, true);
588 if (new_a->data_type == BCH_DATA_cached &&
589 !new_a->io_time[READ])
590 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
592 old_lru = alloc_lru_idx(old_a);
593 new_lru = alloc_lru_idx(*new_a);
595 if (old_lru != new_lru) {
596 ret = bch2_lru_change(trans, new->k.p.inode, new->k.p.offset,
597 old_lru, &new_lru, old);
601 if (new_a->data_type == BCH_DATA_cached)
602 new_a->io_time[READ] = new_lru;
608 static int bch2_check_alloc_key(struct btree_trans *trans,
609 struct btree_iter *alloc_iter)
611 struct bch_fs *c = trans->c;
613 struct btree_iter discard_iter, freespace_iter;
614 struct bch_alloc_v4 a;
615 unsigned discard_key_type, freespace_key_type;
616 struct bkey_s_c alloc_k, k;
617 struct printbuf buf = PRINTBUF;
618 struct printbuf buf2 = PRINTBUF;
621 alloc_k = bch2_btree_iter_peek(alloc_iter);
625 ret = bkey_err(alloc_k);
629 if (fsck_err_on(!bch2_dev_bucket_exists(c, alloc_k.k->p), c,
630 "alloc key for invalid device:bucket %llu:%llu",
631 alloc_k.k->p.inode, alloc_k.k->p.offset))
632 return bch2_btree_delete_at(trans, alloc_iter, 0);
634 ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
635 if (!ca->mi.freespace_initialized)
638 bch2_alloc_to_v4(alloc_k, &a);
640 discard_key_type = a.data_type == BCH_DATA_need_discard
642 freespace_key_type = a.data_type == BCH_DATA_free
645 bch2_trans_iter_init(trans, &discard_iter, BTREE_ID_need_discard,
647 bch2_trans_iter_init(trans, &freespace_iter, BTREE_ID_freespace,
648 alloc_freespace_pos(alloc_k.k->p, a), 0);
650 k = bch2_btree_iter_peek_slot(&discard_iter);
655 if (fsck_err_on(k.k->type != discard_key_type, c,
656 "incorrect key in need_discard btree (got %s should be %s)\n"
658 bch2_bkey_types[k.k->type],
659 bch2_bkey_types[discard_key_type],
660 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
661 struct bkey_i *update =
662 bch2_trans_kmalloc(trans, sizeof(*update));
664 ret = PTR_ERR_OR_ZERO(update);
668 bkey_init(&update->k);
669 update->k.type = discard_key_type;
670 update->k.p = discard_iter.pos;
672 ret = bch2_trans_update(trans, &discard_iter, update, 0);
677 k = bch2_btree_iter_peek_slot(&freespace_iter);
682 if (fsck_err_on(k.k->type != freespace_key_type, c,
683 "incorrect key in freespace btree (got %s should be %s)\n"
685 bch2_bkey_types[k.k->type],
686 bch2_bkey_types[freespace_key_type],
687 (printbuf_reset(&buf),
688 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
689 struct bkey_i *update =
690 bch2_trans_kmalloc(trans, sizeof(*update));
692 ret = PTR_ERR_OR_ZERO(update);
696 bkey_init(&update->k);
697 update->k.type = freespace_key_type;
698 update->k.p = freespace_iter.pos;
699 bch2_key_resize(&update->k, 1);
701 ret = bch2_trans_update(trans, &freespace_iter, update, 0);
707 bch2_trans_iter_exit(trans, &freespace_iter);
708 bch2_trans_iter_exit(trans, &discard_iter);
709 printbuf_exit(&buf2);
714 static int bch2_check_discard_freespace_key(struct btree_trans *trans,
715 struct btree_iter *iter)
717 struct bch_fs *c = trans->c;
718 struct btree_iter alloc_iter;
719 struct bkey_s_c k, freespace_k;
720 struct bch_alloc_v4 a;
723 enum bch_data_type state = iter->btree_id == BTREE_ID_need_discard
724 ? BCH_DATA_need_discard
726 struct printbuf buf = PRINTBUF;
729 freespace_k = bch2_btree_iter_peek(iter);
733 ret = bkey_err(freespace_k);
738 pos.offset &= ~(~0ULL << 56);
739 genbits = iter->pos.offset & (~0ULL << 56);
741 bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
743 if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
744 "entry in %s btree for nonexistant dev:bucket %llu:%llu",
745 bch2_btree_ids[iter->btree_id], pos.inode, pos.offset))
748 k = bch2_btree_iter_peek_slot(&alloc_iter);
753 bch2_alloc_to_v4(k, &a);
755 if (fsck_err_on(a.data_type != state ||
756 (state == BCH_DATA_free &&
757 genbits != alloc_freespace_genbits(a)), c,
758 "%s\n incorrectly set in %s index (free %u, genbits %llu should be %llu)",
759 (bch2_bkey_val_to_text(&buf, c, k), buf.buf),
760 bch2_btree_ids[iter->btree_id],
761 a.data_type == state,
762 genbits >> 56, alloc_freespace_genbits(a) >> 56))
767 bch2_trans_iter_exit(trans, &alloc_iter);
771 ret = bch2_btree_delete_extent_at(trans, iter,
772 iter->btree_id == BTREE_ID_freespace ? 1 : 0, 0);
776 int bch2_check_alloc_info(struct bch_fs *c)
778 struct btree_trans trans;
779 struct btree_iter iter;
783 bch2_trans_init(&trans, c, 0, 0);
785 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
786 BTREE_ITER_PREFETCH, k, ret) {
787 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
788 bch2_check_alloc_key(&trans, &iter));
792 bch2_trans_iter_exit(&trans, &iter);
797 bch2_trans_iter_init(&trans, &iter, BTREE_ID_need_discard, POS_MIN,
798 BTREE_ITER_PREFETCH);
800 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
801 bch2_check_discard_freespace_key(&trans, &iter));
805 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
807 bch2_trans_iter_exit(&trans, &iter);
812 bch2_trans_iter_init(&trans, &iter, BTREE_ID_freespace, POS_MIN,
813 BTREE_ITER_PREFETCH);
815 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
816 bch2_check_discard_freespace_key(&trans, &iter));
820 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
822 bch2_trans_iter_exit(&trans, &iter);
824 bch2_trans_exit(&trans);
825 return ret < 0 ? ret : 0;
828 static int bch2_check_alloc_to_lru_ref(struct btree_trans *trans,
829 struct btree_iter *alloc_iter)
831 struct bch_fs *c = trans->c;
832 struct btree_iter lru_iter;
833 struct bch_alloc_v4 a;
834 struct bkey_s_c alloc_k, k;
835 struct printbuf buf = PRINTBUF;
836 struct printbuf buf2 = PRINTBUF;
839 alloc_k = bch2_btree_iter_peek(alloc_iter);
843 ret = bkey_err(alloc_k);
847 bch2_alloc_to_v4(alloc_k, &a);
849 if (a.data_type != BCH_DATA_cached)
852 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
853 POS(alloc_k.k->p.inode, a.io_time[READ]), 0);
855 k = bch2_btree_iter_peek_slot(&lru_iter);
860 if (fsck_err_on(!a.io_time[READ], c,
861 "cached bucket with read_time 0\n"
863 (printbuf_reset(&buf),
864 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) ||
865 fsck_err_on(k.k->type != KEY_TYPE_lru ||
866 le64_to_cpu(bkey_s_c_to_lru(k).v->idx) != alloc_k.k->p.offset, c,
867 "incorrect/missing lru entry\n"
870 (printbuf_reset(&buf),
871 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
872 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf))) {
873 u64 read_time = a.io_time[READ];
875 if (!a.io_time[READ])
876 a.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
878 ret = bch2_lru_set(trans,
885 if (a.io_time[READ] != read_time) {
886 struct bkey_i_alloc_v4 *a_mut =
887 bch2_alloc_to_v4_mut(trans, alloc_k);
888 ret = PTR_ERR_OR_ZERO(a_mut);
892 a_mut->v.io_time[READ] = a.io_time[READ];
893 ret = bch2_trans_update(trans, alloc_iter,
894 &a_mut->k_i, BTREE_TRIGGER_NORUN);
901 bch2_trans_iter_exit(trans, &lru_iter);
902 printbuf_exit(&buf2);
907 int bch2_check_alloc_to_lru_refs(struct bch_fs *c)
909 struct btree_trans trans;
910 struct btree_iter iter;
914 bch2_trans_init(&trans, c, 0, 0);
916 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
917 BTREE_ITER_PREFETCH, k, ret) {
918 ret = __bch2_trans_do(&trans, NULL, NULL,
920 BTREE_INSERT_LAZY_RW,
921 bch2_check_alloc_to_lru_ref(&trans, &iter));
925 bch2_trans_iter_exit(&trans, &iter);
927 bch2_trans_exit(&trans);
928 return ret < 0 ? ret : 0;
931 static int bch2_clear_need_discard(struct btree_trans *trans, struct bpos pos,
932 struct bch_dev *ca, bool *discard_done)
934 struct bch_fs *c = trans->c;
935 struct btree_iter iter;
937 struct bkey_i_alloc_v4 *a;
938 struct printbuf buf = PRINTBUF;
941 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, pos,
943 k = bch2_btree_iter_peek_slot(&iter);
948 a = bch2_alloc_to_v4_mut(trans, k);
949 ret = PTR_ERR_OR_ZERO(a);
953 if (BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
955 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
959 if (bch2_trans_inconsistent_on(a->v.journal_seq > c->journal.flushed_seq_ondisk, trans,
960 "clearing need_discard but journal_seq %llu > flushed_seq %llu\n"
963 c->journal.flushed_seq_ondisk,
964 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
969 if (bch2_trans_inconsistent_on(a->v.data_type != BCH_DATA_need_discard, trans,
970 "bucket incorrectly set in need_discard btree\n"
972 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
977 if (!*discard_done && ca->mi.discard && !c->opts.nochanges) {
979 * This works without any other locks because this is the only
980 * thread that removes items from the need_discard tree
982 bch2_trans_unlock(trans);
983 blkdev_issue_discard(ca->disk_sb.bdev,
984 k.k->p.offset * ca->mi.bucket_size,
987 *discard_done = true;
989 ret = bch2_trans_relock(trans) ? 0 : -EINTR;
994 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
995 a->v.data_type = alloc_data_type(a->v, a->v.data_type);
997 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
999 bch2_trans_iter_exit(trans, &iter);
1000 printbuf_exit(&buf);
1004 static void bch2_do_discards_work(struct work_struct *work)
1006 struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
1007 struct bch_dev *ca = NULL;
1008 struct btree_trans trans;
1009 struct btree_iter iter;
1011 u64 seen = 0, open = 0, need_journal_commit = 0, discarded = 0;
1014 bch2_trans_init(&trans, c, 0, 0);
1016 for_each_btree_key(&trans, iter, BTREE_ID_need_discard,
1017 POS_MIN, 0, k, ret) {
1018 bool discard_done = false;
1020 if (ca && k.k->p.inode != ca->dev_idx) {
1021 percpu_ref_put(&ca->io_ref);
1026 ca = bch_dev_bkey_exists(c, k.k->p.inode);
1027 if (!percpu_ref_tryget(&ca->io_ref)) {
1029 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
1036 if (bch2_bucket_is_open_safe(c, k.k->p.inode, k.k->p.offset)) {
1041 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
1042 c->journal.flushed_seq_ondisk,
1043 k.k->p.inode, k.k->p.offset)) {
1044 need_journal_commit++;
1048 ret = __bch2_trans_do(&trans, NULL, NULL,
1049 BTREE_INSERT_USE_RESERVE|
1050 BTREE_INSERT_NOFAIL,
1051 bch2_clear_need_discard(&trans, k.k->p, ca, &discard_done));
1057 bch2_trans_iter_exit(&trans, &iter);
1060 percpu_ref_put(&ca->io_ref);
1062 bch2_trans_exit(&trans);
1064 if (need_journal_commit * 2 > seen)
1065 bch2_journal_flush_async(&c->journal, NULL);
1067 percpu_ref_put(&c->writes);
1069 trace_discard_buckets(c, seen, open, need_journal_commit, discarded, ret);
1072 void bch2_do_discards(struct bch_fs *c)
1074 if (percpu_ref_tryget(&c->writes) &&
1075 !queue_work(system_long_wq, &c->discard_work))
1076 percpu_ref_put(&c->writes);
1079 static int invalidate_one_bucket(struct btree_trans *trans, struct bch_dev *ca)
1081 struct bch_fs *c = trans->c;
1082 struct btree_iter lru_iter, alloc_iter = { NULL };
1084 struct bkey_i_alloc_v4 *a;
1086 struct printbuf buf = PRINTBUF;
1089 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
1090 POS(ca->dev_idx, 0), 0);
1092 k = bch2_btree_iter_peek(&lru_iter);
1097 if (!k.k || k.k->p.inode != ca->dev_idx)
1100 if (k.k->type != KEY_TYPE_lru) {
1101 prt_printf(&buf, "non lru key in lru btree:\n ");
1102 bch2_bkey_val_to_text(&buf, c, k);
1104 if (!test_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags)) {
1105 bch_err(c, "%s", buf.buf);
1106 bch2_btree_iter_advance(&lru_iter);
1109 bch2_trans_inconsistent(trans, "%s", buf.buf);
1115 idx = k.k->p.offset;
1116 bucket = le64_to_cpu(bkey_s_c_to_lru(k).v->idx);
1118 a = bch2_trans_start_alloc_update(trans, &alloc_iter,
1119 POS(ca->dev_idx, bucket));
1120 ret = PTR_ERR_OR_ZERO(a);
1124 if (idx != alloc_lru_idx(a->v)) {
1125 prt_printf(&buf, "alloc key does not point back to lru entry when invalidating bucket:\n ");
1126 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&a->k_i));
1127 prt_printf(&buf, "\n ");
1128 bch2_bkey_val_to_text(&buf, c, k);
1130 if (!test_bit(BCH_FS_CHECK_LRUS_DONE, &c->flags)) {
1131 bch_err(c, "%s", buf.buf);
1132 bch2_btree_iter_advance(&lru_iter);
1135 bch2_trans_inconsistent(trans, "%s", buf.buf);
1141 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1144 a->v.dirty_sectors = 0;
1145 a->v.cached_sectors = 0;
1146 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1147 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1149 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1150 BTREE_TRIGGER_BUCKET_INVALIDATE);
1154 trace_invalidate_bucket(c, a->k.p.inode, a->k.p.offset);
1156 bch2_trans_iter_exit(trans, &alloc_iter);
1157 bch2_trans_iter_exit(trans, &lru_iter);
1158 printbuf_exit(&buf);
1162 static void bch2_do_invalidates_work(struct work_struct *work)
1164 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
1166 struct btree_trans trans;
1170 bch2_trans_init(&trans, c, 0, 0);
1172 for_each_member_device(ca, c, i) {
1173 s64 nr_to_invalidate =
1174 should_invalidate_buckets(ca, bch2_dev_usage_read(ca));
1176 while (!ret && nr_to_invalidate-- >= 0)
1177 ret = __bch2_trans_do(&trans, NULL, NULL,
1178 BTREE_INSERT_USE_RESERVE|
1179 BTREE_INSERT_NOFAIL,
1180 invalidate_one_bucket(&trans, ca));
1183 bch2_trans_exit(&trans);
1184 percpu_ref_put(&c->writes);
1187 void bch2_do_invalidates(struct bch_fs *c)
1189 if (percpu_ref_tryget(&c->writes))
1190 queue_work(system_long_wq, &c->invalidate_work);
1193 static int bucket_freespace_init(struct btree_trans *trans, struct btree_iter *iter)
1195 struct bch_alloc_v4 a;
1199 k = bch2_btree_iter_peek_slot(iter);
1204 bch2_alloc_to_v4(k, &a);
1205 return bch2_bucket_do_index(trans, k, &a, true);
1208 static int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca)
1210 struct btree_trans trans;
1211 struct btree_iter iter;
1213 struct bch_member *m;
1216 bch2_trans_init(&trans, c, 0, 0);
1218 for_each_btree_key(&trans, iter, BTREE_ID_alloc,
1219 POS(ca->dev_idx, ca->mi.first_bucket),
1221 BTREE_ITER_PREFETCH, k, ret) {
1222 if (iter.pos.offset >= ca->mi.nbuckets)
1225 ret = __bch2_trans_do(&trans, NULL, NULL,
1226 BTREE_INSERT_LAZY_RW,
1227 bucket_freespace_init(&trans, &iter));
1231 bch2_trans_iter_exit(&trans, &iter);
1233 bch2_trans_exit(&trans);
1236 bch_err(ca, "error initializing free space: %i", ret);
1240 mutex_lock(&c->sb_lock);
1241 m = bch2_sb_get_members(c->disk_sb.sb)->members + ca->dev_idx;
1242 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
1243 mutex_unlock(&c->sb_lock);
1248 int bch2_fs_freespace_init(struct bch_fs *c)
1253 bool doing_init = false;
1256 * We can crash during the device add path, so we need to check this on
1260 for_each_member_device(ca, c, i) {
1261 if (ca->mi.freespace_initialized)
1265 bch_info(c, "initializing freespace");
1269 ret = bch2_dev_freespace_init(c, ca);
1271 percpu_ref_put(&ca->ref);
1277 mutex_lock(&c->sb_lock);
1278 bch2_write_super(c);
1279 mutex_unlock(&c->sb_lock);
1281 bch_verbose(c, "done initializing freespace");
1287 /* Bucket IO clocks: */
1289 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
1290 size_t bucket_nr, int rw)
1292 struct bch_fs *c = trans->c;
1293 struct btree_iter iter;
1294 struct bkey_i_alloc_v4 *a;
1298 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
1299 ret = PTR_ERR_OR_ZERO(a);
1303 now = atomic64_read(&c->io_clock[rw].now);
1304 if (a->v.io_time[rw] == now)
1307 a->v.io_time[rw] = now;
1309 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1310 bch2_trans_commit(trans, NULL, NULL, 0);
1312 bch2_trans_iter_exit(trans, &iter);
1316 /* Startup/shutdown (ro/rw): */
1318 void bch2_recalc_capacity(struct bch_fs *c)
1321 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
1322 unsigned bucket_size_max = 0;
1323 unsigned long ra_pages = 0;
1326 lockdep_assert_held(&c->state_lock);
1328 for_each_online_member(ca, c, i) {
1329 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
1331 ra_pages += bdi->ra_pages;
1334 bch2_set_ra_pages(c, ra_pages);
1336 for_each_rw_member(ca, c, i) {
1337 u64 dev_reserve = 0;
1340 * We need to reserve buckets (from the number
1341 * of currently available buckets) against
1342 * foreground writes so that mainly copygc can
1343 * make forward progress.
1345 * We need enough to refill the various reserves
1346 * from scratch - copygc will use its entire
1347 * reserve all at once, then run against when
1348 * its reserve is refilled (from the formerly
1349 * available buckets).
1351 * This reserve is just used when considering if
1352 * allocations for foreground writes must wait -
1353 * not -ENOSPC calculations.
1356 dev_reserve += ca->nr_btree_reserve * 2;
1357 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
1359 dev_reserve += 1; /* btree write point */
1360 dev_reserve += 1; /* copygc write point */
1361 dev_reserve += 1; /* rebalance write point */
1363 dev_reserve *= ca->mi.bucket_size;
1365 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
1366 ca->mi.first_bucket);
1368 reserved_sectors += dev_reserve * 2;
1370 bucket_size_max = max_t(unsigned, bucket_size_max,
1371 ca->mi.bucket_size);
1374 gc_reserve = c->opts.gc_reserve_bytes
1375 ? c->opts.gc_reserve_bytes >> 9
1376 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
1378 reserved_sectors = max(gc_reserve, reserved_sectors);
1380 reserved_sectors = min(reserved_sectors, capacity);
1382 c->capacity = capacity - reserved_sectors;
1384 c->bucket_size_max = bucket_size_max;
1386 /* Wake up case someone was waiting for buckets */
1387 closure_wake_up(&c->freelist_wait);
1390 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
1392 struct open_bucket *ob;
1395 for (ob = c->open_buckets;
1396 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1398 spin_lock(&ob->lock);
1399 if (ob->valid && !ob->on_partial_list &&
1400 ob->dev == ca->dev_idx)
1402 spin_unlock(&ob->lock);
1408 /* device goes ro: */
1409 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
1413 /* First, remove device from allocation groups: */
1415 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1416 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1419 * Capacity is calculated based off of devices in allocation groups:
1421 bch2_recalc_capacity(c);
1423 /* Next, close write points that point to this device... */
1424 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1425 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1427 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1428 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1429 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1431 mutex_lock(&c->btree_reserve_cache_lock);
1432 while (c->btree_reserve_cache_nr) {
1433 struct btree_alloc *a =
1434 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1436 bch2_open_buckets_put(c, &a->ob);
1438 mutex_unlock(&c->btree_reserve_cache_lock);
1441 struct open_bucket *ob;
1443 spin_lock(&c->freelist_lock);
1444 if (!ca->open_buckets_partial_nr) {
1445 spin_unlock(&c->freelist_lock);
1448 ob = c->open_buckets +
1449 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1450 ob->on_partial_list = false;
1451 spin_unlock(&c->freelist_lock);
1453 bch2_open_bucket_put(c, ob);
1456 bch2_ec_stop_dev(c, ca);
1459 * Wake up threads that were blocked on allocation, so they can notice
1460 * the device can no longer be removed and the capacity has changed:
1462 closure_wake_up(&c->freelist_wait);
1465 * journal_res_get() can block waiting for free space in the journal -
1466 * it needs to notice there may not be devices to allocate from anymore:
1468 wake_up(&c->journal.wait);
1470 /* Now wait for any in flight writes: */
1472 closure_wait_event(&c->open_buckets_wait,
1473 !bch2_dev_has_open_write_point(c, ca));
1476 /* device goes rw: */
1477 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1481 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1482 if (ca->mi.data_allowed & (1 << i))
1483 set_bit(ca->dev_idx, c->rw_devs[i].d);
1486 void bch2_fs_allocator_background_init(struct bch_fs *c)
1488 spin_lock_init(&c->freelist_lock);
1489 INIT_WORK(&c->discard_work, bch2_do_discards_work);
1490 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);