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) ? 0 : -EINTR;
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;
910 u64 seen = 0, open = 0, need_journal_commit = 0, discarded = 0;
913 bch2_trans_init(&trans, c, 0, 0);
915 for_each_btree_key(&trans, iter, BTREE_ID_need_discard,
916 POS_MIN, 0, k, ret) {
917 bool discard_done = false;
919 if (ca && k.k->p.inode != ca->dev_idx) {
920 percpu_ref_put(&ca->io_ref);
925 ca = bch_dev_bkey_exists(c, k.k->p.inode);
926 if (!percpu_ref_tryget(&ca->io_ref)) {
928 bch2_btree_iter_set_pos(&iter, POS(k.k->p.inode + 1, 0));
935 if (bch2_bucket_is_open_safe(c, k.k->p.inode, k.k->p.offset)) {
940 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
941 c->journal.flushed_seq_ondisk,
942 k.k->p.inode, k.k->p.offset)) {
943 need_journal_commit++;
947 ret = __bch2_trans_do(&trans, NULL, NULL,
948 BTREE_INSERT_USE_RESERVE|
950 bch2_clear_need_discard(&trans, k.k->p, ca, &discard_done));
956 bch2_trans_iter_exit(&trans, &iter);
959 percpu_ref_put(&ca->io_ref);
961 bch2_trans_exit(&trans);
963 if (need_journal_commit * 2 > seen)
964 bch2_journal_flush_async(&c->journal, NULL);
966 percpu_ref_put(&c->writes);
968 trace_do_discards(c, seen, open, need_journal_commit, discarded, ret);
971 void bch2_do_discards(struct bch_fs *c)
973 if (percpu_ref_tryget(&c->writes) &&
974 !queue_work(system_long_wq, &c->discard_work))
975 percpu_ref_put(&c->writes);
978 static int invalidate_one_bucket(struct btree_trans *trans, struct bch_dev *ca)
980 struct bch_fs *c = trans->c;
981 struct btree_iter lru_iter, alloc_iter = { NULL };
983 struct bkey_i_alloc_v4 *a;
987 bch2_trans_iter_init(trans, &lru_iter, BTREE_ID_lru,
988 POS(ca->dev_idx, 0), 0);
989 k = bch2_btree_iter_peek(&lru_iter);
994 if (!k.k || k.k->p.inode != ca->dev_idx)
997 if (bch2_fs_inconsistent_on(k.k->type != KEY_TYPE_lru, c,
998 "non lru key in lru btree"))
1001 idx = k.k->p.offset;
1002 bucket = le64_to_cpu(bkey_s_c_to_lru(k).v->idx);
1004 a = bch2_trans_start_alloc_update(trans, &alloc_iter,
1005 POS(ca->dev_idx, bucket));
1006 ret = PTR_ERR_OR_ZERO(a);
1010 if (bch2_fs_inconsistent_on(idx != alloc_lru_idx(a->v), c,
1011 "invalidating bucket with wrong lru idx (got %llu should be %llu",
1012 idx, alloc_lru_idx(a->v)))
1015 SET_BCH_ALLOC_V4_NEED_INC_GEN(&a->v, false);
1018 a->v.dirty_sectors = 0;
1019 a->v.cached_sectors = 0;
1020 a->v.io_time[READ] = atomic64_read(&c->io_clock[READ].now);
1021 a->v.io_time[WRITE] = atomic64_read(&c->io_clock[WRITE].now);
1023 ret = bch2_trans_update(trans, &alloc_iter, &a->k_i,
1024 BTREE_TRIGGER_BUCKET_INVALIDATE);
1026 bch2_trans_iter_exit(trans, &alloc_iter);
1027 bch2_trans_iter_exit(trans, &lru_iter);
1031 static void bch2_do_invalidates_work(struct work_struct *work)
1033 struct bch_fs *c = container_of(work, struct bch_fs, invalidate_work);
1035 struct btree_trans trans;
1039 bch2_trans_init(&trans, c, 0, 0);
1041 for_each_member_device(ca, c, i)
1042 while (!ret && should_invalidate_buckets(ca))
1043 ret = __bch2_trans_do(&trans, NULL, NULL,
1044 BTREE_INSERT_USE_RESERVE|
1045 BTREE_INSERT_NOFAIL,
1046 invalidate_one_bucket(&trans, ca));
1048 bch2_trans_exit(&trans);
1049 percpu_ref_put(&c->writes);
1052 void bch2_do_invalidates(struct bch_fs *c)
1054 if (percpu_ref_tryget(&c->writes))
1055 queue_work(system_long_wq, &c->invalidate_work);
1058 static int bch2_dev_freespace_init(struct bch_fs *c, struct bch_dev *ca)
1060 struct btree_trans trans;
1061 struct btree_iter iter;
1063 struct bch_alloc_v4 a;
1064 struct bch_member *m;
1067 bch2_trans_init(&trans, c, 0, 0);
1069 for_each_btree_key(&trans, iter, BTREE_ID_alloc,
1070 POS(ca->dev_idx, ca->mi.first_bucket),
1072 BTREE_ITER_PREFETCH, k, ret) {
1073 if (iter.pos.offset >= ca->mi.nbuckets)
1076 bch2_alloc_to_v4(k, &a);
1077 ret = __bch2_trans_do(&trans, NULL, NULL,
1078 BTREE_INSERT_LAZY_RW,
1079 bch2_bucket_do_index(&trans, k, a, true));
1083 bch2_trans_iter_exit(&trans, &iter);
1085 bch2_trans_exit(&trans);
1088 bch_err(ca, "error initializing free space: %i", ret);
1092 mutex_lock(&c->sb_lock);
1093 m = bch2_sb_get_members(c->disk_sb.sb)->members + ca->dev_idx;
1094 SET_BCH_MEMBER_FREESPACE_INITIALIZED(m, true);
1095 mutex_unlock(&c->sb_lock);
1100 int bch2_fs_freespace_init(struct bch_fs *c)
1105 bool doing_init = false;
1108 * We can crash during the device add path, so we need to check this on
1112 for_each_member_device(ca, c, i) {
1113 if (ca->mi.freespace_initialized)
1117 bch_info(c, "initializing freespace");
1121 ret = bch2_dev_freespace_init(c, ca);
1123 percpu_ref_put(&ca->ref);
1129 mutex_lock(&c->sb_lock);
1130 bch2_write_super(c);
1131 mutex_unlock(&c->sb_lock);
1133 bch_verbose(c, "done initializing freespace");
1139 /* Bucket IO clocks: */
1141 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
1142 size_t bucket_nr, int rw)
1144 struct bch_fs *c = trans->c;
1145 struct btree_iter iter;
1146 struct bkey_i_alloc_v4 *a;
1150 a = bch2_trans_start_alloc_update(trans, &iter, POS(dev, bucket_nr));
1151 ret = PTR_ERR_OR_ZERO(a);
1155 now = atomic64_read(&c->io_clock[rw].now);
1156 if (a->v.io_time[rw] == now)
1159 a->v.io_time[rw] = now;
1161 ret = bch2_trans_update(trans, &iter, &a->k_i, 0) ?:
1162 bch2_trans_commit(trans, NULL, NULL, 0);
1164 bch2_trans_iter_exit(trans, &iter);
1168 /* Startup/shutdown (ro/rw): */
1170 void bch2_recalc_capacity(struct bch_fs *c)
1173 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
1174 unsigned bucket_size_max = 0;
1175 unsigned long ra_pages = 0;
1178 lockdep_assert_held(&c->state_lock);
1180 for_each_online_member(ca, c, i) {
1181 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
1183 ra_pages += bdi->ra_pages;
1186 bch2_set_ra_pages(c, ra_pages);
1188 for_each_rw_member(ca, c, i) {
1189 u64 dev_reserve = 0;
1192 * We need to reserve buckets (from the number
1193 * of currently available buckets) against
1194 * foreground writes so that mainly copygc can
1195 * make forward progress.
1197 * We need enough to refill the various reserves
1198 * from scratch - copygc will use its entire
1199 * reserve all at once, then run against when
1200 * its reserve is refilled (from the formerly
1201 * available buckets).
1203 * This reserve is just used when considering if
1204 * allocations for foreground writes must wait -
1205 * not -ENOSPC calculations.
1208 dev_reserve += ca->nr_btree_reserve * 2;
1209 dev_reserve += ca->mi.nbuckets >> 6; /* copygc reserve */
1211 dev_reserve += 1; /* btree write point */
1212 dev_reserve += 1; /* copygc write point */
1213 dev_reserve += 1; /* rebalance write point */
1215 dev_reserve *= ca->mi.bucket_size;
1217 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
1218 ca->mi.first_bucket);
1220 reserved_sectors += dev_reserve * 2;
1222 bucket_size_max = max_t(unsigned, bucket_size_max,
1223 ca->mi.bucket_size);
1226 gc_reserve = c->opts.gc_reserve_bytes
1227 ? c->opts.gc_reserve_bytes >> 9
1228 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
1230 reserved_sectors = max(gc_reserve, reserved_sectors);
1232 reserved_sectors = min(reserved_sectors, capacity);
1234 c->capacity = capacity - reserved_sectors;
1236 c->bucket_size_max = bucket_size_max;
1238 /* Wake up case someone was waiting for buckets */
1239 closure_wake_up(&c->freelist_wait);
1242 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
1244 struct open_bucket *ob;
1247 for (ob = c->open_buckets;
1248 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1250 spin_lock(&ob->lock);
1251 if (ob->valid && !ob->on_partial_list &&
1252 ob->dev == ca->dev_idx)
1254 spin_unlock(&ob->lock);
1260 /* device goes ro: */
1261 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
1265 /* First, remove device from allocation groups: */
1267 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1268 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1271 * Capacity is calculated based off of devices in allocation groups:
1273 bch2_recalc_capacity(c);
1275 /* Next, close write points that point to this device... */
1276 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1277 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1279 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1280 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1281 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1283 mutex_lock(&c->btree_reserve_cache_lock);
1284 while (c->btree_reserve_cache_nr) {
1285 struct btree_alloc *a =
1286 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1288 bch2_open_buckets_put(c, &a->ob);
1290 mutex_unlock(&c->btree_reserve_cache_lock);
1293 struct open_bucket *ob;
1295 spin_lock(&c->freelist_lock);
1296 if (!ca->open_buckets_partial_nr) {
1297 spin_unlock(&c->freelist_lock);
1300 ob = c->open_buckets +
1301 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1302 ob->on_partial_list = false;
1303 spin_unlock(&c->freelist_lock);
1305 bch2_open_bucket_put(c, ob);
1308 bch2_ec_stop_dev(c, ca);
1311 * Wake up threads that were blocked on allocation, so they can notice
1312 * the device can no longer be removed and the capacity has changed:
1314 closure_wake_up(&c->freelist_wait);
1317 * journal_res_get() can block waiting for free space in the journal -
1318 * it needs to notice there may not be devices to allocate from anymore:
1320 wake_up(&c->journal.wait);
1322 /* Now wait for any in flight writes: */
1324 closure_wait_event(&c->open_buckets_wait,
1325 !bch2_dev_has_open_write_point(c, ca));
1328 /* device goes rw: */
1329 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1333 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1334 if (ca->mi.data_allowed & (1 << i))
1335 set_bit(ca->dev_idx, c->rw_devs[i].d);
1338 void bch2_fs_allocator_background_init(struct bch_fs *c)
1340 spin_lock_init(&c->freelist_lock);
1341 INIT_WORK(&c->discard_work, bch2_do_discards_work);
1342 INIT_WORK(&c->invalidate_work, bch2_do_invalidates_work);