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 const char * const bch2_bucket_states[] = {
47 struct bkey_alloc_unpacked {
56 #define x(_name, _bits) u##_bits _name;
61 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
62 const void **p, unsigned field)
64 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
67 if (!(a->fields & (1 << field)))
72 v = *((const u8 *) *p);
91 static inline void alloc_field_v1_put(struct bkey_i_alloc *a, void **p,
92 unsigned field, u64 v)
94 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
99 a->v.fields |= 1 << field;
106 *((__le16 *) *p) = cpu_to_le16(v);
109 *((__le32 *) *p) = cpu_to_le32(v);
112 *((__le64 *) *p) = cpu_to_le64(v);
121 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
124 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
125 const void *d = in->data;
130 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
131 BCH_ALLOC_FIELDS_V1()
135 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
138 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
139 const u8 *in = a.v->data;
140 const u8 *end = bkey_val_end(a);
141 unsigned fieldnr = 0;
146 out->oldest_gen = a.v->oldest_gen;
147 out->data_type = a.v->data_type;
149 #define x(_name, _bits) \
150 if (fieldnr < a.v->nr_fields) { \
151 ret = bch2_varint_decode_fast(in, end, &v); \
159 if (v != out->_name) \
163 BCH_ALLOC_FIELDS_V2()
168 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
171 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
172 const u8 *in = a.v->data;
173 const u8 *end = bkey_val_end(a);
174 unsigned fieldnr = 0;
179 out->oldest_gen = a.v->oldest_gen;
180 out->data_type = a.v->data_type;
181 out->need_discard = BCH_ALLOC_V3_NEED_DISCARD(a.v);
182 out->need_inc_gen = BCH_ALLOC_V3_NEED_INC_GEN(a.v);
183 out->journal_seq = le64_to_cpu(a.v->journal_seq);
185 #define x(_name, _bits) \
186 if (fieldnr < a.v->nr_fields) { \
187 ret = bch2_varint_decode_fast(in, end, &v); \
195 if (v != out->_name) \
199 BCH_ALLOC_FIELDS_V2()
204 static struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
206 struct bkey_alloc_unpacked ret = {
208 .bucket = k.k->p.offset,
214 bch2_alloc_unpack_v1(&ret, k);
216 case KEY_TYPE_alloc_v2:
217 bch2_alloc_unpack_v2(&ret, k);
219 case KEY_TYPE_alloc_v3:
220 bch2_alloc_unpack_v3(&ret, k);
227 void bch2_alloc_to_v4(struct bkey_s_c k, struct bch_alloc_v4 *out)
229 if (k.k->type == KEY_TYPE_alloc_v4) {
230 *out = *bkey_s_c_to_alloc_v4(k).v;
232 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
234 *out = (struct bch_alloc_v4) {
235 .journal_seq = u.journal_seq,
236 .flags = u.need_discard,
238 .oldest_gen = u.oldest_gen,
239 .data_type = u.data_type,
240 .stripe_redundancy = u.stripe_redundancy,
241 .dirty_sectors = u.dirty_sectors,
242 .cached_sectors = u.cached_sectors,
243 .io_time[READ] = u.read_time,
244 .io_time[WRITE] = u.write_time,
250 struct bkey_i_alloc_v4 *bch2_alloc_to_v4_mut(struct btree_trans *trans, struct bkey_s_c k)
252 struct bkey_i_alloc_v4 *ret;
254 if (k.k->type == KEY_TYPE_alloc_v4) {
255 ret = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
257 bkey_reassemble(&ret->k_i, k);
259 ret = bch2_trans_kmalloc(trans, sizeof(*ret));
261 bkey_alloc_v4_init(&ret->k_i);
263 bch2_alloc_to_v4(k, &ret->v);
269 struct bkey_i_alloc_v4 *
270 bch2_trans_start_alloc_update(struct btree_trans *trans, struct btree_iter *iter,
274 struct bkey_i_alloc_v4 *a;
277 bch2_trans_iter_init(trans, iter, BTREE_ID_alloc, pos,
278 BTREE_ITER_WITH_UPDATES|
281 k = bch2_btree_iter_peek_slot(iter);
284 bch2_trans_iter_exit(trans, iter);
288 a = bch2_alloc_to_v4_mut(trans, k);
290 bch2_trans_iter_exit(trans, iter);
294 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
296 unsigned i, bytes = offsetof(struct bch_alloc, data);
298 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
299 if (a->fields & (1 << i))
300 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
302 return DIV_ROUND_UP(bytes, sizeof(u64));
305 const char *bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k)
307 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
309 if (k.k->p.inode >= c->sb.nr_devices ||
310 !c->devs[k.k->p.inode])
311 return "invalid device";
313 /* allow for unknown fields */
314 if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v))
315 return "incorrect value size";
320 const char *bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
322 struct bkey_alloc_unpacked u;
324 if (k.k->p.inode >= c->sb.nr_devices ||
325 !c->devs[k.k->p.inode])
326 return "invalid device";
328 if (bch2_alloc_unpack_v2(&u, k))
329 return "unpack error";
334 const char *bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k)
336 struct bkey_alloc_unpacked u;
339 if (k.k->p.inode >= c->sb.nr_devices ||
340 !c->devs[k.k->p.inode])
341 return "invalid device";
343 ca = bch_dev_bkey_exists(c, k.k->p.inode);
345 if (k.k->p.offset < ca->mi.first_bucket ||
346 k.k->p.offset >= ca->mi.nbuckets)
347 return "invalid bucket";
349 if (bch2_alloc_unpack_v3(&u, k))
350 return "unpack error";
355 const char *bch2_alloc_v4_invalid(const struct bch_fs *c, struct bkey_s_c k)
359 if (k.k->p.inode >= c->sb.nr_devices ||
360 !c->devs[k.k->p.inode])
361 return "invalid device";
363 ca = bch_dev_bkey_exists(c, k.k->p.inode);
365 if (k.k->p.offset < ca->mi.first_bucket ||
366 k.k->p.offset >= ca->mi.nbuckets)
367 return "invalid bucket";
372 void bch2_alloc_v4_swab(struct bkey_s k)
374 struct bch_alloc_v4 *a = bkey_s_to_alloc_v4(k).v;
376 a->journal_seq = swab64(a->journal_seq);
377 a->flags = swab32(a->flags);
378 a->dirty_sectors = swab32(a->dirty_sectors);
379 a->cached_sectors = swab32(a->cached_sectors);
380 a->io_time[0] = swab64(a->io_time[0]);
381 a->io_time[1] = swab64(a->io_time[1]);
382 a->stripe = swab32(a->stripe);
383 a->nr_external_backpointers = swab32(a->nr_external_backpointers);
386 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c, struct bkey_s_c k)
388 struct bch_alloc_v4 a;
390 bch2_alloc_to_v4(k, &a);
392 pr_buf(out, "gen %u oldest_gen %u data_type %s journal_seq %llu need_discard %llu",
393 a.gen, a.oldest_gen, bch2_data_types[a.data_type],
394 a.journal_seq, BCH_ALLOC_V4_NEED_DISCARD(&a));
395 pr_buf(out, " dirty_sectors %u", a.dirty_sectors);
396 pr_buf(out, " cached_sectors %u", a.cached_sectors);
397 pr_buf(out, " stripe %u", a.stripe);
398 pr_buf(out, " stripe_redundancy %u", a.stripe_redundancy);
399 pr_buf(out, " read_time %llu", a.io_time[READ]);
400 pr_buf(out, " write_time %llu", a.io_time[WRITE]);
403 int bch2_alloc_read(struct bch_fs *c)
405 struct btree_trans trans;
406 struct btree_iter iter;
408 struct bch_alloc_v4 a;
412 bch2_trans_init(&trans, c, 0, 0);
414 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
415 BTREE_ITER_PREFETCH, k, ret) {
416 ca = bch_dev_bkey_exists(c, k.k->p.inode);
417 bch2_alloc_to_v4(k, &a);
419 *bucket_gen(ca, k.k->p.offset) = a.gen;
421 bch2_trans_iter_exit(&trans, &iter);
423 bch2_trans_exit(&trans);
426 bch_err(c, "error reading alloc info: %i", ret);
431 /* Free space/discard btree: */
433 static int bch2_bucket_do_index(struct btree_trans *trans,
434 struct bkey_s_c alloc_k,
435 struct bch_alloc_v4 a,
438 struct bch_fs *c = trans->c;
439 struct bch_dev *ca = bch_dev_bkey_exists(c, alloc_k.k->p.inode);
440 struct btree_iter iter;
443 enum bucket_state state = bucket_state(a);
445 enum bch_bkey_type old_type = !set ? KEY_TYPE_set : KEY_TYPE_deleted;
446 enum bch_bkey_type new_type = set ? KEY_TYPE_set : KEY_TYPE_deleted;
447 struct printbuf buf = PRINTBUF;
450 if (state != BUCKET_free &&
451 state != BUCKET_need_discard)
454 k = bch2_trans_kmalloc(trans, sizeof(*k));
459 k->k.type = new_type;
463 btree = BTREE_ID_freespace;
464 k->k.p = alloc_freespace_pos(alloc_k.k->p, a);
465 bch2_key_resize(&k->k, 1);
467 case BUCKET_need_discard:
468 btree = BTREE_ID_need_discard;
469 k->k.p = alloc_k.k->p;
475 bch2_trans_iter_init(trans, &iter, btree,
476 bkey_start_pos(&k->k),
478 old = bch2_btree_iter_peek_slot(&iter);
483 if (ca->mi.freespace_initialized &&
484 bch2_fs_inconsistent_on(old.k->type != old_type, c,
485 "incorrect key when %s %s btree (got %s should be %s)\n"
487 set ? "setting" : "clearing",
488 bch2_btree_ids[btree],
489 bch2_bkey_types[old.k->type],
490 bch2_bkey_types[old_type],
491 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
496 ret = bch2_trans_update(trans, &iter, k, 0);
498 bch2_trans_iter_exit(trans, &iter);
503 int bch2_trans_mark_alloc(struct btree_trans *trans,
504 struct bkey_s_c old, struct bkey_i *new,
507 struct bch_fs *c = trans->c;
508 struct bch_alloc_v4 old_a, *new_a;
509 u64 old_lru, new_lru;
513 * Deletion only happens in the device removal path, with
514 * BTREE_TRIGGER_NORUN:
516 BUG_ON(new->k.type != KEY_TYPE_alloc_v4);
518 bch2_alloc_to_v4(old, &old_a);
519 new_a = &bkey_i_to_alloc_v4(new)->v;
521 if (new_a->dirty_sectors > old_a.dirty_sectors ||
522 new_a->cached_sectors > old_a.cached_sectors) {
523 new_a->io_time[READ] = max_t(u64, 1, atomic64_read(&c->io_clock[READ].now));
524 new_a->io_time[WRITE]= max_t(u64, 1, atomic64_read(&c->io_clock[WRITE].now));
525 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, true);
526 SET_BCH_ALLOC_V4_NEED_DISCARD(new_a, true);
529 if (old_a.data_type && !new_a->data_type &&
530 old_a.gen == new_a->gen &&
531 !bch2_bucket_is_open_safe(c, new->k.p.inode, new->k.p.offset)) {
533 SET_BCH_ALLOC_V4_NEED_INC_GEN(new_a, false);
536 if (bucket_state(old_a) != bucket_state(*new_a) ||
537 (bucket_state(*new_a) == BUCKET_free &&
538 alloc_freespace_genbits(old_a) != alloc_freespace_genbits(*new_a))) {
539 ret = bch2_bucket_do_index(trans, old, old_a, false) ?:
540 bch2_bucket_do_index(trans, bkey_i_to_s_c(new), *new_a, true);
545 old_lru = alloc_lru_idx(old_a);
546 new_lru = alloc_lru_idx(*new_a);
548 if (old_lru != new_lru) {
549 ret = bch2_lru_change(trans, new->k.p.inode, new->k.p.offset,
554 if (new_lru && new_a->io_time[READ] != new_lru)
555 new_a->io_time[READ] = new_lru;
561 static int bch2_check_alloc_key(struct btree_trans *trans,
562 struct btree_iter *alloc_iter)
564 struct bch_fs *c = trans->c;
565 struct btree_iter discard_iter, freespace_iter, lru_iter;
566 struct bch_alloc_v4 a;
567 unsigned discard_key_type, freespace_key_type;
568 struct bkey_s_c alloc_k, k;
569 struct printbuf buf = PRINTBUF;
570 struct printbuf buf2 = PRINTBUF;
573 alloc_k = bch2_btree_iter_peek(alloc_iter);
577 ret = bkey_err(alloc_k);
581 bch2_alloc_to_v4(alloc_k, &a);
582 discard_key_type = bucket_state(a) == BUCKET_need_discard
584 freespace_key_type = bucket_state(a) == BUCKET_free
587 bch2_trans_iter_init(trans, &discard_iter, BTREE_ID_need_discard,
589 bch2_trans_iter_init(trans, &freespace_iter, BTREE_ID_freespace,
590 alloc_freespace_pos(alloc_k.k->p, a), 0);
591 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
592 POS(alloc_k.k->p.inode, a.io_time[READ]), 0);
594 k = bch2_btree_iter_peek_slot(&discard_iter);
599 if (fsck_err_on(k.k->type != discard_key_type, c,
600 "incorrect key in need_discard btree (got %s should be %s)\n"
602 bch2_bkey_types[k.k->type],
603 bch2_bkey_types[discard_key_type],
604 (bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
605 struct bkey_i *update =
606 bch2_trans_kmalloc(trans, sizeof(*update));
608 ret = PTR_ERR_OR_ZERO(update);
612 bkey_init(&update->k);
613 update->k.type = discard_key_type;
614 update->k.p = discard_iter.pos;
616 ret = bch2_trans_update(trans, &discard_iter, update, 0) ?:
617 bch2_trans_commit(trans, NULL, NULL, 0);
622 k = bch2_btree_iter_peek_slot(&freespace_iter);
627 if (fsck_err_on(k.k->type != freespace_key_type, c,
628 "incorrect key in freespace btree (got %s should be %s)\n"
630 bch2_bkey_types[k.k->type],
631 bch2_bkey_types[freespace_key_type],
632 (printbuf_reset(&buf),
633 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf))) {
634 struct bkey_i *update =
635 bch2_trans_kmalloc(trans, sizeof(*update));
637 ret = PTR_ERR_OR_ZERO(update);
641 bkey_init(&update->k);
642 update->k.type = freespace_key_type;
643 update->k.p = freespace_iter.pos;
644 bch2_key_resize(&update->k, 1);
646 ret = bch2_trans_update(trans, &freespace_iter, update, 0) ?:
647 bch2_trans_commit(trans, NULL, NULL, 0);
652 if (bucket_state(a) == BUCKET_cached) {
653 k = bch2_btree_iter_peek_slot(&lru_iter);
658 if (fsck_err_on(!a.io_time[READ], c,
659 "cached bucket with read_time 0\n"
661 (printbuf_reset(&buf),
662 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf)) ||
663 fsck_err_on(k.k->type != KEY_TYPE_lru ||
664 le64_to_cpu(bkey_s_c_to_lru(k).v->idx) != alloc_k.k->p.offset, c,
665 "incorrect/missing lru entry\n"
668 (printbuf_reset(&buf),
669 bch2_bkey_val_to_text(&buf, c, alloc_k), buf.buf),
670 (bch2_bkey_val_to_text(&buf2, c, k), buf2.buf))) {
671 u64 read_time = a.io_time[READ];
673 if (!a.io_time[READ])
674 a.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
676 ret = bch2_lru_change(trans,
679 0, &a.io_time[READ]);
683 if (a.io_time[READ] != read_time) {
684 struct bkey_i_alloc_v4 *a_mut =
685 bch2_alloc_to_v4_mut(trans, alloc_k);
686 ret = PTR_ERR_OR_ZERO(a_mut);
690 a_mut->v.io_time[READ] = a.io_time[READ];
691 ret = bch2_trans_update(trans, alloc_iter,
692 &a_mut->k_i, BTREE_TRIGGER_NORUN);
697 ret = bch2_trans_commit(trans, NULL, NULL, 0);
704 bch2_trans_iter_exit(trans, &lru_iter);
705 bch2_trans_iter_exit(trans, &freespace_iter);
706 bch2_trans_iter_exit(trans, &discard_iter);
707 printbuf_exit(&buf2);
712 static inline bool bch2_dev_bucket_exists(struct bch_fs *c, struct bpos pos)
716 if (pos.inode >= c->sb.nr_devices || !c->devs[pos.inode])
719 ca = bch_dev_bkey_exists(c, pos.inode);
720 return pos.offset >= ca->mi.first_bucket &&
721 pos.offset < ca->mi.nbuckets;
724 static int bch2_check_freespace_key(struct btree_trans *trans,
725 struct btree_iter *freespace_iter,
728 struct bch_fs *c = trans->c;
729 struct btree_iter alloc_iter;
730 struct bkey_s_c k, freespace_k;
731 struct bch_alloc_v4 a;
734 struct bkey_i *update;
735 struct printbuf buf = PRINTBUF;
738 freespace_k = bch2_btree_iter_peek(freespace_iter);
742 ret = bkey_err(freespace_k);
746 pos = freespace_iter->pos;
747 pos.offset &= ~(~0ULL << 56);
748 genbits = freespace_iter->pos.offset & (~0ULL << 56);
750 bch2_trans_iter_init(trans, &alloc_iter, BTREE_ID_alloc, pos, 0);
752 if (fsck_err_on(!bch2_dev_bucket_exists(c, pos), c,
753 "%llu:%llu set in freespace btree but device or bucket does not exist",
754 pos.inode, pos.offset))
757 k = bch2_btree_iter_peek_slot(&alloc_iter);
762 bch2_alloc_to_v4(k, &a);
764 if (fsck_err_on(bucket_state(a) != BUCKET_free ||
765 genbits != alloc_freespace_genbits(a), c,
766 "%s\n incorrectly set in freespace index (free %u, genbits %llu should be %llu)",
767 (bch2_bkey_val_to_text(&buf, c, k), buf.buf),
768 bucket_state(a) == BUCKET_free,
769 genbits >> 56, alloc_freespace_genbits(a) >> 56))
774 bch2_trans_iter_exit(trans, &alloc_iter);
778 update = bch2_trans_kmalloc(trans, sizeof(*update));
779 ret = PTR_ERR_OR_ZERO(update);
783 bkey_init(&update->k);
784 update->k.p = freespace_iter->pos;
785 bch2_key_resize(&update->k, 1);
787 ret = bch2_trans_update(trans, freespace_iter, update, 0) ?:
788 bch2_trans_commit(trans, NULL, NULL, 0);
792 int bch2_check_alloc_info(struct bch_fs *c, bool initial)
794 struct btree_trans trans;
795 struct btree_iter iter;
797 int ret = 0, last_dev = -1;
799 bch2_trans_init(&trans, c, 0, 0);
801 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
802 BTREE_ITER_PREFETCH, k, ret) {
803 if (k.k->p.inode != last_dev) {
804 struct bch_dev *ca = bch_dev_bkey_exists(c, k.k->p.inode);
806 if (!ca->mi.freespace_initialized) {
807 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
811 last_dev = k.k->p.inode;
814 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
815 bch2_check_alloc_key(&trans, &iter));
819 bch2_trans_iter_exit(&trans, &iter);
824 bch2_trans_iter_init(&trans, &iter, BTREE_ID_freespace, POS_MIN,
825 BTREE_ITER_PREFETCH);
827 ret = __bch2_trans_do(&trans, NULL, NULL, 0,
828 bch2_check_freespace_key(&trans, &iter, initial));
832 bch2_btree_iter_set_pos(&iter, bpos_nosnap_successor(iter.pos));
834 bch2_trans_iter_exit(&trans, &iter);
836 bch2_trans_exit(&trans);
837 return ret < 0 ? ret : 0;
840 static int bch2_clear_need_discard(struct btree_trans *trans, struct bpos pos,
841 struct bch_dev *ca, bool *discard_done)
843 struct bch_fs *c = trans->c;
844 struct btree_iter iter;
846 struct bkey_i_alloc_v4 *a;
847 struct printbuf buf = PRINTBUF;
850 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, pos,
852 k = bch2_btree_iter_peek_slot(&iter);
857 a = bch2_alloc_to_v4_mut(trans, k);
858 ret = PTR_ERR_OR_ZERO(a);
862 if (BCH_ALLOC_V4_NEED_INC_GEN(&a->v)) {
864 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
868 BUG_ON(a->v.journal_seq > c->journal.flushed_seq_ondisk);
870 if (bch2_fs_inconsistent_on(!BCH_ALLOC_V4_NEED_DISCARD(&a->v), c,
871 "%s\n incorrectly set in need_discard btree",
872 (bch2_bkey_val_to_text(&buf, c, k), buf.buf))) {
877 if (!*discard_done && ca->mi.discard && !c->opts.nochanges) {
879 * This works without any other locks because this is the only
880 * thread that removes items from the need_discard tree
882 bch2_trans_unlock(trans);
883 blkdev_issue_discard(ca->disk_sb.bdev,
884 k.k->p.offset * ca->mi.bucket_size,
887 *discard_done = true;
889 ret = bch2_trans_relock(trans);
894 SET_BCH_ALLOC_V4_NEED_DISCARD(&a->v, false);
896 ret = bch2_trans_update(trans, &iter, &a->k_i, 0);
898 bch2_trans_iter_exit(trans, &iter);
903 static void bch2_do_discards_work(struct work_struct *work)
905 struct bch_fs *c = container_of(work, struct bch_fs, discard_work);
906 struct bch_dev *ca = NULL;
907 struct btree_trans trans;
908 struct btree_iter iter;
912 bch2_trans_init(&trans, c, 0, 0);
914 for_each_btree_key(&trans, iter, BTREE_ID_need_discard,
915 POS_MIN, 0, k, ret) {
916 bool discard_done = false;
918 if (ca && k.k->p.inode != ca->dev_idx) {
919 percpu_ref_put(&ca->io_ref);
924 ca = bch_dev_bkey_exists(c, k.k->p.inode);
925 if (!percpu_ref_tryget(&ca->io_ref)) {
927 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
932 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
933 c->journal.flushed_seq_ondisk,
934 k.k->p.inode, k.k->p.offset) ||
935 bch2_bucket_is_open_safe(c, k.k->p.inode, k.k->p.offset))
938 ret = __bch2_trans_do(&trans, NULL, NULL,
939 BTREE_INSERT_USE_RESERVE|
941 bch2_clear_need_discard(&trans, k.k->p, ca, &discard_done));
945 bch2_trans_iter_exit(&trans, &iter);
948 percpu_ref_put(&ca->io_ref);
950 bch2_trans_exit(&trans);
951 percpu_ref_put(&c->writes);
954 void bch2_do_discards(struct bch_fs *c)
956 if (percpu_ref_tryget(&c->writes) &&
957 !queue_work(system_long_wq, &c->discard_work))
958 percpu_ref_put(&c->writes);
961 static int invalidate_one_bucket(struct btree_trans *trans, struct bch_dev *ca)
963 struct bch_fs *c = trans->c;
964 struct btree_iter lru_iter, alloc_iter = { NULL };
966 struct bkey_i_alloc_v4 *a;
970 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
971 POS(ca->dev_idx, 0), 0);
972 k = bch2_btree_iter_peek(&lru_iter);
977 if (!k.k || k.k->p.inode != ca->dev_idx)
980 if (bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_lru, c,
981 "non lru key in lru btree"))
985 bucket = le64_to_cpu(bkey_s_c_to_lru(k).v->idx);
987 a = bch2_trans_start_alloc_update(trans, &alloc_iter,
988 POS(ca->dev_idx, bucket));
989 ret = PTR_ERR_OR_ZERO(a);
993 if (bch2_fs_inconsistent_on(idx != alloc_lru_idx(a->v), c,
994 "invalidating bucket with wrong lru idx (got %llu should be %llu",
995 idx, alloc_lru_idx(a->v)))
998 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1001 a->v.dirty_sectors = 0;
1002 a->v.cached_sectors = 0;
1003 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1004 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1006 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1007 BTREE_TRIGGER_BUCKET_INVALIDATE);
1009 bch2_trans_iter_exit(trans, &alloc_iter);
1010 bch2_trans_iter_exit(trans, &lru_iter);
1014 static void bch2_do_invalidates_work(struct work_struct *work)
1016 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
1018 struct btree_trans trans;
1022 bch2_trans_init(&trans, c, 0, 0);
1024 for_each_member_device(ca, c, i)
1025 while (!ret && should_invalidate_buckets(ca))
1026 ret = __bch2_trans_do(&trans, NULL, NULL,
1027 BTREE_INSERT_USE_RESERVE|
1028 BTREE_INSERT_NOFAIL,
1029 invalidate_one_bucket(&trans, ca));
1031 bch2_trans_exit(&trans);
1032 percpu_ref_put(&c->writes);
1035 void bch2_do_invalidates(struct bch_fs *c)
1037 if (percpu_ref_tryget(&c->writes))
1038 queue_work(system_long_wq, &c->invalidate_work);
1041 static int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca)
1043 struct btree_trans trans;
1044 struct btree_iter iter;
1046 struct bch_alloc_v4 a;
1047 struct bch_member *m;
1050 bch2_trans_init(&trans, c, 0, 0);
1052 for_each_btree_key(&trans, iter, BTREE_ID_alloc,
1053 POS(ca->dev_idx, ca->mi.first_bucket),
1055 BTREE_ITER_PREFETCH, k, ret) {
1056 if (iter.pos.offset >= ca->mi.nbuckets)
1059 bch2_alloc_to_v4(k, &a);
1060 ret = __bch2_trans_do(&trans, NULL, NULL,
1061 BTREE_INSERT_LAZY_RW,
1062 bch2_bucket_do_index(&trans, k, a, true));
1066 bch2_trans_iter_exit(&trans, &iter);
1068 bch2_trans_exit(&trans);
1071 bch_err(ca, "error initializing free space: %i", ret);
1075 mutex_lock(&c->sb_lock);
1076 m = bch2_sb_get_members(c->disk_sb.sb)->members + ca->dev_idx;
1077 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
1078 mutex_unlock(&c->sb_lock);
1083 int bch2_fs_freespace_init(struct bch_fs *c)
1088 bool doing_init = false;
1091 * We can crash during the device add path, so we need to check this on
1095 for_each_member_device(ca, c, i) {
1096 if (ca->mi.freespace_initialized)
1100 bch_info(c, "initializing freespace");
1104 ret = bch2_dev_freespace_init(c, ca);
1106 percpu_ref_put(&ca->ref);
1112 mutex_lock(&c->sb_lock);
1113 bch2_write_super(c);
1114 mutex_unlock(&c->sb_lock);
1116 bch_verbose(c, "done initializing freespace");
1122 /* Bucket IO clocks: */
1124 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
1125 size_t bucket_nr, int rw)
1127 struct bch_fs *c = trans->c;
1128 struct btree_iter iter;
1129 struct bkey_i_alloc_v4 *a;
1133 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
1134 ret = PTR_ERR_OR_ZERO(a);
1138 now = atomic64_read(&c->io_clock[rw].now);
1139 if (a->v.io_time[rw] == now)
1142 a->v.io_time[rw] = now;
1144 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1145 bch2_trans_commit(trans, NULL, NULL, 0);
1147 bch2_trans_iter_exit(trans, &iter);
1151 /* Startup/shutdown (ro/rw): */
1153 void bch2_recalc_capacity(struct bch_fs *c)
1156 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
1157 unsigned bucket_size_max = 0;
1158 unsigned long ra_pages = 0;
1161 lockdep_assert_held(&c->state_lock);
1163 for_each_online_member(ca, c, i) {
1164 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
1166 ra_pages += bdi->ra_pages;
1169 bch2_set_ra_pages(c, ra_pages);
1171 for_each_rw_member(ca, c, i) {
1172 u64 dev_reserve = 0;
1175 * We need to reserve buckets (from the number
1176 * of currently available buckets) against
1177 * foreground writes so that mainly copygc can
1178 * make forward progress.
1180 * We need enough to refill the various reserves
1181 * from scratch - copygc will use its entire
1182 * reserve all at once, then run against when
1183 * its reserve is refilled (from the formerly
1184 * available buckets).
1186 * This reserve is just used when considering if
1187 * allocations for foreground writes must wait -
1188 * not -ENOSPC calculations.
1191 dev_reserve += ca->nr_btree_reserve * 2;
1192 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
1194 dev_reserve += 1; /* btree write point */
1195 dev_reserve += 1; /* copygc write point */
1196 dev_reserve += 1; /* rebalance write point */
1198 dev_reserve *= ca->mi.bucket_size;
1200 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
1201 ca->mi.first_bucket);
1203 reserved_sectors += dev_reserve * 2;
1205 bucket_size_max = max_t(unsigned, bucket_size_max,
1206 ca->mi.bucket_size);
1209 gc_reserve = c->opts.gc_reserve_bytes
1210 ? c->opts.gc_reserve_bytes >> 9
1211 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
1213 reserved_sectors = max(gc_reserve, reserved_sectors);
1215 reserved_sectors = min(reserved_sectors, capacity);
1217 c->capacity = capacity - reserved_sectors;
1219 c->bucket_size_max = bucket_size_max;
1221 /* Wake up case someone was waiting for buckets */
1222 closure_wake_up(&c->freelist_wait);
1225 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
1227 struct open_bucket *ob;
1230 for (ob = c->open_buckets;
1231 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1233 spin_lock(&ob->lock);
1234 if (ob->valid && !ob->on_partial_list &&
1235 ob->dev == ca->dev_idx)
1237 spin_unlock(&ob->lock);
1243 /* device goes ro: */
1244 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
1248 /* First, remove device from allocation groups: */
1250 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1251 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1254 * Capacity is calculated based off of devices in allocation groups:
1256 bch2_recalc_capacity(c);
1258 /* Next, close write points that point to this device... */
1259 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1260 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1262 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1263 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1264 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1266 mutex_lock(&c->btree_reserve_cache_lock);
1267 while (c->btree_reserve_cache_nr) {
1268 struct btree_alloc *a =
1269 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1271 bch2_open_buckets_put(c, &a->ob);
1273 mutex_unlock(&c->btree_reserve_cache_lock);
1276 struct open_bucket *ob;
1278 spin_lock(&c->freelist_lock);
1279 if (!ca->open_buckets_partial_nr) {
1280 spin_unlock(&c->freelist_lock);
1283 ob = c->open_buckets +
1284 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1285 ob->on_partial_list = false;
1286 spin_unlock(&c->freelist_lock);
1288 bch2_open_bucket_put(c, ob);
1291 bch2_ec_stop_dev(c, ca);
1294 * Wake up threads that were blocked on allocation, so they can notice
1295 * the device can no longer be removed and the capacity has changed:
1297 closure_wake_up(&c->freelist_wait);
1300 * journal_res_get() can block waiting for free space in the journal -
1301 * it needs to notice there may not be devices to allocate from anymore:
1303 wake_up(&c->journal.wait);
1305 /* Now wait for any in flight writes: */
1307 closure_wait_event(&c->open_buckets_wait,
1308 !bch2_dev_has_open_write_point(c, ca));
1311 /* device goes rw: */
1312 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1316 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1317 if (ca->mi.data_allowed & (1 << i))
1318 set_bit(ca->dev_idx, c->rw_devs[i].d);
1321 void bch2_fs_allocator_background_init(struct bch_fs *c)
1323 spin_lock_init(&c->freelist_lock);
1324 INIT_WORK(&c->discard_work, bch2_do_discards_work);
1325 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);