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"
19 #include <linux/kthread.h>
20 #include <linux/math64.h>
21 #include <linux/random.h>
22 #include <linux/rculist.h>
23 #include <linux/rcupdate.h>
24 #include <linux/sched/task.h>
25 #include <linux/sort.h>
26 #include <trace/events/bcachefs.h>
28 const char * const bch2_allocator_states[] = {
35 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
36 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
41 /* Persistent alloc info: */
43 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
44 const void **p, unsigned field)
46 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
49 if (!(a->fields & (1 << field)))
54 v = *((const u8 *) *p);
73 static inline void alloc_field_v1_put(struct bkey_i_alloc *a, void **p,
74 unsigned field, u64 v)
76 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
81 a->v.fields |= 1 << field;
88 *((__le16 *) *p) = cpu_to_le16(v);
91 *((__le32 *) *p) = cpu_to_le32(v);
94 *((__le64 *) *p) = cpu_to_le64(v);
103 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
106 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
107 const void *d = in->data;
112 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
113 BCH_ALLOC_FIELDS_V1()
117 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
120 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
121 const u8 *in = a.v->data;
122 const u8 *end = bkey_val_end(a);
123 unsigned fieldnr = 0;
128 out->oldest_gen = a.v->oldest_gen;
129 out->data_type = a.v->data_type;
131 #define x(_name, _bits) \
132 if (fieldnr < a.v->nr_fields) { \
133 ret = bch2_varint_decode_fast(in, end, &v); \
141 if (v != out->_name) \
145 BCH_ALLOC_FIELDS_V2()
150 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
153 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
154 const u8 *in = a.v->data;
155 const u8 *end = bkey_val_end(a);
156 unsigned fieldnr = 0;
161 out->oldest_gen = a.v->oldest_gen;
162 out->data_type = a.v->data_type;
163 out->journal_seq = le64_to_cpu(a.v->journal_seq);
165 #define x(_name, _bits) \
166 if (fieldnr < a.v->nr_fields) { \
167 ret = bch2_varint_decode_fast(in, end, &v); \
175 if (v != out->_name) \
179 BCH_ALLOC_FIELDS_V2()
184 static void bch2_alloc_pack_v3(struct bkey_alloc_buf *dst,
185 const struct bkey_alloc_unpacked src)
187 struct bkey_i_alloc_v3 *a = bkey_alloc_v3_init(&dst->k);
188 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
190 u8 *end = (void *) &dst[1];
191 u8 *last_nonzero_field = out;
194 a->k.p = POS(src.dev, src.bucket);
196 a->v.oldest_gen = src.oldest_gen;
197 a->v.data_type = src.data_type;
198 a->v.journal_seq = cpu_to_le64(src.journal_seq);
200 #define x(_name, _bits) \
204 out += bch2_varint_encode_fast(out, src._name); \
206 last_nonzero_field = out; \
207 last_nonzero_fieldnr = nr_fields; \
212 BCH_ALLOC_FIELDS_V2()
216 out = last_nonzero_field;
217 a->v.nr_fields = last_nonzero_fieldnr;
219 bytes = (u8 *) out - (u8 *) &a->v;
220 set_bkey_val_bytes(&a->k, bytes);
221 memset_u64s_tail(&a->v, 0, bytes);
224 struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
226 struct bkey_alloc_unpacked ret = {
228 .bucket = k.k->p.offset,
234 bch2_alloc_unpack_v1(&ret, k);
236 case KEY_TYPE_alloc_v2:
237 bch2_alloc_unpack_v2(&ret, k);
239 case KEY_TYPE_alloc_v3:
240 bch2_alloc_unpack_v3(&ret, k);
247 struct bkey_alloc_buf *bch2_alloc_pack(struct btree_trans *trans,
248 const struct bkey_alloc_unpacked src)
250 struct bkey_alloc_buf *dst;
252 dst = bch2_trans_kmalloc(trans, sizeof(struct bkey_alloc_buf));
254 bch2_alloc_pack_v3(dst, src);
259 int bch2_alloc_write(struct btree_trans *trans, struct btree_iter *iter,
260 struct bkey_alloc_unpacked *u, unsigned trigger_flags)
262 struct bkey_alloc_buf *a = bch2_alloc_pack(trans, *u);
264 return PTR_ERR_OR_ZERO(a) ?:
265 bch2_trans_update(trans, iter, &a->k, trigger_flags);
268 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
270 unsigned i, bytes = offsetof(struct bch_alloc, data);
272 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
273 if (a->fields & (1 << i))
274 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
276 return DIV_ROUND_UP(bytes, sizeof(u64));
279 const char *bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k)
281 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
283 if (k.k->p.inode >= c->sb.nr_devices ||
284 !c->devs[k.k->p.inode])
285 return "invalid device";
287 /* allow for unknown fields */
288 if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v))
289 return "incorrect value size";
294 const char *bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
296 struct bkey_alloc_unpacked u;
298 if (k.k->p.inode >= c->sb.nr_devices ||
299 !c->devs[k.k->p.inode])
300 return "invalid device";
302 if (bch2_alloc_unpack_v2(&u, k))
303 return "unpack error";
308 const char *bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k)
310 struct bkey_alloc_unpacked u;
312 if (k.k->p.inode >= c->sb.nr_devices ||
313 !c->devs[k.k->p.inode])
314 return "invalid device";
316 if (bch2_alloc_unpack_v3(&u, k))
317 return "unpack error";
322 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c,
325 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
327 pr_buf(out, "gen %u oldest_gen %u data_type %s journal_seq %llu",
328 u.gen, u.oldest_gen, bch2_data_types[u.data_type],
330 #define x(_name, ...) pr_buf(out, " " #_name " %llu", (u64) u._name);
331 BCH_ALLOC_FIELDS_V2()
335 int bch2_alloc_read(struct bch_fs *c, bool gc, bool metadata_only)
337 struct btree_trans trans;
338 struct btree_iter iter;
342 struct bkey_alloc_unpacked u;
345 bch2_trans_init(&trans, c, 0, 0);
347 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
348 BTREE_ITER_PREFETCH, k, ret) {
349 ca = bch_dev_bkey_exists(c, k.k->p.inode);
350 g = __bucket(ca, k.k->p.offset, gc);
351 u = bch2_alloc_unpack(k);
354 *bucket_gen(ca, k.k->p.offset) = u.gen;
356 g->_mark.gen = u.gen;
357 g->io_time[READ] = u.read_time;
358 g->io_time[WRITE] = u.write_time;
359 g->oldest_gen = !gc ? u.oldest_gen : u.gen;
364 (u.data_type == BCH_DATA_user ||
365 u.data_type == BCH_DATA_cached ||
366 u.data_type == BCH_DATA_parity))) {
367 g->_mark.data_type = u.data_type;
368 g->_mark.dirty_sectors = u.dirty_sectors;
369 g->_mark.cached_sectors = u.cached_sectors;
370 g->_mark.stripe = u.stripe != 0;
371 g->stripe = u.stripe;
372 g->stripe_redundancy = u.stripe_redundancy;
376 bch2_trans_iter_exit(&trans, &iter);
378 bch2_trans_exit(&trans);
381 bch_err(c, "error reading alloc info: %i", ret);
386 /* Bucket IO clocks: */
388 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
389 size_t bucket_nr, int rw)
391 struct bch_fs *c = trans->c;
392 struct btree_iter iter;
394 struct bkey_alloc_unpacked u;
398 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(dev, bucket_nr),
401 k = bch2_btree_iter_peek_slot(&iter);
406 u = bch2_alloc_unpack(k);
408 time = rw == READ ? &u.read_time : &u.write_time;
409 now = atomic64_read(&c->io_clock[rw].now);
415 ret = bch2_alloc_write(trans, &iter, &u, 0) ?:
416 bch2_trans_commit(trans, NULL, NULL, 0);
418 bch2_trans_iter_exit(trans, &iter);
422 /* Background allocator thread: */
425 * Scans for buckets to be invalidated, invalidates them, rewrites prios/gens
426 * (marking them as invalidated on disk), then optionally issues discard
427 * commands to the newly free buckets, then puts them on the various freelists.
430 static bool bch2_can_invalidate_bucket(struct bch_dev *ca, size_t b,
431 struct bucket_mark m)
435 if (!is_available_bucket(m))
438 if (m.owned_by_allocator)
441 if (ca->buckets_nouse &&
442 test_bit(b, ca->buckets_nouse))
445 if (ca->new_fs_bucket_idx) {
447 * Device or filesystem is still being initialized, and we
448 * haven't fully marked superblocks & journal:
450 if (is_superblock_bucket(ca, b))
453 if (b < ca->new_fs_bucket_idx)
457 gc_gen = bucket_gc_gen(bucket(ca, b));
459 ca->inc_gen_needs_gc += gc_gen >= BUCKET_GC_GEN_MAX / 2;
460 ca->inc_gen_really_needs_gc += gc_gen >= BUCKET_GC_GEN_MAX;
462 return gc_gen < BUCKET_GC_GEN_MAX;
466 * Determines what order we're going to reuse buckets, smallest bucket_key()
470 static unsigned bucket_sort_key(struct bucket *g, struct bucket_mark m,
471 u64 now, u64 last_seq_ondisk)
473 unsigned used = bucket_sectors_used(m);
477 * Prefer to keep buckets that have been read more recently, and
478 * buckets that have more data in them:
480 u64 last_read = max_t(s64, 0, now - g->io_time[READ]);
481 u32 last_read_scaled = max_t(u64, U32_MAX, div_u64(last_read, used));
483 return -last_read_scaled;
486 * Prefer to use buckets with smaller gc_gen so that we don't
487 * have to walk the btree and recalculate oldest_gen - but shift
488 * off the low bits so that buckets will still have equal sort
489 * keys when there's only a small difference, so that we can
490 * keep sequential buckets together:
492 return (bucket_needs_journal_commit(m, last_seq_ondisk) << 4)|
493 (bucket_gc_gen(g) >> 4);
497 static inline int bucket_alloc_cmp(alloc_heap *h,
498 struct alloc_heap_entry l,
499 struct alloc_heap_entry r)
501 return cmp_int(l.key, r.key) ?:
502 cmp_int(r.nr, l.nr) ?:
503 cmp_int(l.bucket, r.bucket);
506 static inline int bucket_idx_cmp(const void *_l, const void *_r)
508 const struct alloc_heap_entry *l = _l, *r = _r;
510 return cmp_int(l->bucket, r->bucket);
513 static void find_reclaimable_buckets_lru(struct bch_fs *c, struct bch_dev *ca)
515 struct bucket_array *buckets;
516 struct alloc_heap_entry e = { 0 };
517 u64 now, last_seq_ondisk;
520 down_read(&ca->bucket_lock);
522 buckets = bucket_array(ca);
523 ca->alloc_heap.used = 0;
524 now = atomic64_read(&c->io_clock[READ].now);
525 last_seq_ondisk = c->journal.flushed_seq_ondisk;
528 * Find buckets with lowest read priority, by building a maxheap sorted
529 * by read priority and repeatedly replacing the maximum element until
530 * all buckets have been visited.
532 for (b = ca->mi.first_bucket; b < ca->mi.nbuckets; b++) {
533 struct bucket *g = &buckets->b[b];
534 struct bucket_mark m = READ_ONCE(g->mark);
535 unsigned key = bucket_sort_key(g, m, now, last_seq_ondisk);
539 if (!bch2_can_invalidate_bucket(ca, b, m))
542 if (e.nr && e.bucket + e.nr == b && e.key == key) {
546 heap_add_or_replace(&ca->alloc_heap, e,
547 -bucket_alloc_cmp, NULL);
549 e = (struct alloc_heap_entry) {
558 heap_add_or_replace(&ca->alloc_heap, e,
559 -bucket_alloc_cmp, NULL);
561 for (i = 0; i < ca->alloc_heap.used; i++)
562 nr += ca->alloc_heap.data[i].nr;
564 while (nr - ca->alloc_heap.data[0].nr >= ALLOC_SCAN_BATCH(ca)) {
565 nr -= ca->alloc_heap.data[0].nr;
566 heap_pop(&ca->alloc_heap, e, -bucket_alloc_cmp, NULL);
569 up_read(&ca->bucket_lock);
572 static size_t find_reclaimable_buckets(struct bch_fs *c, struct bch_dev *ca)
576 ca->inc_gen_needs_gc = 0;
577 ca->inc_gen_really_needs_gc = 0;
579 find_reclaimable_buckets_lru(c, ca);
581 heap_resort(&ca->alloc_heap, bucket_alloc_cmp, NULL);
583 for (i = 0; i < ca->alloc_heap.used; i++)
584 nr += ca->alloc_heap.data[i].nr;
589 static int bucket_invalidate_btree(struct btree_trans *trans,
590 struct bch_dev *ca, u64 b,
591 struct bkey_alloc_unpacked *u)
593 struct bch_fs *c = trans->c;
594 struct btree_iter iter;
598 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
603 k = bch2_btree_iter_peek_slot(&iter);
608 *u = bch2_alloc_unpack(k);
611 u->dirty_sectors = 0;
612 u->cached_sectors = 0;
613 u->read_time = atomic64_read(&c->io_clock[READ].now);
614 u->write_time = atomic64_read(&c->io_clock[WRITE].now);
616 ret = bch2_alloc_write(trans, &iter, u,
617 BTREE_TRIGGER_BUCKET_INVALIDATE);
619 bch2_trans_iter_exit(trans, &iter);
623 static int bch2_invalidate_one_bucket(struct bch_fs *c, struct bch_dev *ca,
624 u64 *journal_seq, unsigned flags)
626 struct bkey_alloc_unpacked u;
631 * If the read-only path is trying to shut down, we can't be generating
634 if (test_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags))
637 BUG_ON(!ca->alloc_heap.used ||
638 !ca->alloc_heap.data[0].nr);
639 b = ca->alloc_heap.data[0].bucket;
641 /* first, put on free_inc and mark as owned by allocator: */
642 percpu_down_read(&c->mark_lock);
644 bch2_mark_alloc_bucket(c, ca, b, true);
646 spin_lock(&c->freelist_lock);
647 verify_not_on_freelist(c, ca, b);
648 BUG_ON(!fifo_push(&ca->free_inc, b));
649 spin_unlock(&c->freelist_lock);
651 percpu_up_read(&c->mark_lock);
653 ret = bch2_trans_do(c, NULL, journal_seq,
654 BTREE_INSERT_NOCHECK_RW|
656 BTREE_INSERT_JOURNAL_RESERVED|
658 bucket_invalidate_btree(&trans, ca, b, &u));
661 /* remove from alloc_heap: */
662 struct alloc_heap_entry e, *top = ca->alloc_heap.data;
668 heap_pop(&ca->alloc_heap, e, bucket_alloc_cmp, NULL);
671 * Make sure we flush the last journal entry that updated this
672 * bucket (i.e. deleting the last reference) before writing to
675 *journal_seq = max(*journal_seq, u.journal_seq);
679 /* remove from free_inc: */
680 percpu_down_read(&c->mark_lock);
681 spin_lock(&c->freelist_lock);
683 bch2_mark_alloc_bucket(c, ca, b, false);
685 BUG_ON(!fifo_pop_back(&ca->free_inc, b2));
688 spin_unlock(&c->freelist_lock);
689 percpu_up_read(&c->mark_lock);
692 return ret < 0 ? ret : 0;
696 * Pull buckets off ca->alloc_heap, invalidate them, move them to ca->free_inc:
698 static int bch2_invalidate_buckets(struct bch_fs *c, struct bch_dev *ca)
703 /* Only use nowait if we've already invalidated at least one bucket: */
705 !fifo_full(&ca->free_inc) &&
706 ca->alloc_heap.used) {
707 if (kthread_should_stop()) {
712 ret = bch2_invalidate_one_bucket(c, ca, &journal_seq,
713 (!fifo_empty(&ca->free_inc)
714 ? BTREE_INSERT_NOWAIT : 0));
716 * We only want to batch up invalidates when they're going to
717 * require flushing the journal:
723 /* If we used NOWAIT, don't return the error: */
724 if (!fifo_empty(&ca->free_inc))
727 bch_err(ca, "error invalidating buckets: %i", ret);
732 ret = bch2_journal_flush_seq(&c->journal, journal_seq);
734 bch_err(ca, "journal error: %i", ret);
741 static void alloc_thread_set_state(struct bch_dev *ca, unsigned new_state)
743 if (ca->allocator_state != new_state) {
744 ca->allocator_state = new_state;
745 closure_wake_up(&ca->fs->freelist_wait);
749 static int push_invalidated_bucket(struct bch_fs *c, struct bch_dev *ca, u64 b)
754 spin_lock(&c->freelist_lock);
755 for (i = 0; i < RESERVE_NR; i++) {
757 * Don't strand buckets on the copygc freelist until
758 * after recovery is finished:
760 if (i == RESERVE_MOVINGGC &&
761 !test_bit(BCH_FS_STARTED, &c->flags))
764 if (fifo_push(&ca->free[i], b)) {
765 fifo_pop(&ca->free_inc, b);
770 spin_unlock(&c->freelist_lock);
772 ca->allocator_state = ret
774 : ALLOCATOR_blocked_full;
775 closure_wake_up(&c->freelist_wait);
779 static void discard_one_bucket(struct bch_fs *c, struct bch_dev *ca, u64 b)
781 if (ca->mi.discard &&
782 blk_queue_discard(bdev_get_queue(ca->disk_sb.bdev)))
783 blkdev_issue_discard(ca->disk_sb.bdev, bucket_to_sector(ca, b),
784 ca->mi.bucket_size, GFP_NOFS, 0);
787 static bool allocator_thread_running(struct bch_dev *ca)
789 unsigned state = ca->mi.state == BCH_MEMBER_STATE_rw &&
790 test_bit(BCH_FS_ALLOCATOR_RUNNING, &ca->fs->flags)
793 alloc_thread_set_state(ca, state);
794 return state == ALLOCATOR_running;
797 static int buckets_available(struct bch_dev *ca, unsigned long gc_count)
799 s64 available = dev_buckets_reclaimable(ca) -
800 (gc_count == ca->fs->gc_count ? ca->inc_gen_really_needs_gc : 0);
801 bool ret = available > 0;
803 alloc_thread_set_state(ca, ret
805 : ALLOCATOR_blocked);
810 * bch_allocator_thread - move buckets from free_inc to reserves
812 * The free_inc FIFO is populated by find_reclaimable_buckets(), and
813 * the reserves are depleted by bucket allocation. When we run out
814 * of free_inc, try to invalidate some buckets and write out
817 static int bch2_allocator_thread(void *arg)
819 struct bch_dev *ca = arg;
820 struct bch_fs *c = ca->fs;
821 unsigned long gc_count = c->gc_count;
828 ret = kthread_wait_freezable(allocator_thread_running(ca));
832 while (!ca->alloc_heap.used) {
835 ret = kthread_wait_freezable(buckets_available(ca, gc_count));
839 gc_count = c->gc_count;
840 nr = find_reclaimable_buckets(c, ca);
842 trace_alloc_scan(ca, nr, ca->inc_gen_needs_gc,
843 ca->inc_gen_really_needs_gc);
845 if ((ca->inc_gen_needs_gc >= ALLOC_SCAN_BATCH(ca) ||
846 ca->inc_gen_really_needs_gc) &&
848 atomic_inc(&c->kick_gc);
849 wake_up_process(c->gc_thread);
853 ret = bch2_invalidate_buckets(c, ca);
857 while (!fifo_empty(&ca->free_inc)) {
858 u64 b = fifo_peek(&ca->free_inc);
860 discard_one_bucket(c, ca, b);
862 ret = kthread_wait_freezable(push_invalidated_bucket(c, ca, b));
868 alloc_thread_set_state(ca, ALLOCATOR_stopped);
872 /* Startup/shutdown (ro/rw): */
874 void bch2_recalc_capacity(struct bch_fs *c)
877 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
878 unsigned bucket_size_max = 0;
879 unsigned long ra_pages = 0;
882 lockdep_assert_held(&c->state_lock);
884 for_each_online_member(ca, c, i) {
885 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
887 ra_pages += bdi->ra_pages;
890 bch2_set_ra_pages(c, ra_pages);
892 for_each_rw_member(ca, c, i) {
896 * We need to reserve buckets (from the number
897 * of currently available buckets) against
898 * foreground writes so that mainly copygc can
899 * make forward progress.
901 * We need enough to refill the various reserves
902 * from scratch - copygc will use its entire
903 * reserve all at once, then run against when
904 * its reserve is refilled (from the formerly
905 * available buckets).
907 * This reserve is just used when considering if
908 * allocations for foreground writes must wait -
909 * not -ENOSPC calculations.
911 for (j = 0; j < RESERVE_NONE; j++)
912 dev_reserve += ca->free[j].size;
914 dev_reserve += 1; /* btree write point */
915 dev_reserve += 1; /* copygc write point */
916 dev_reserve += 1; /* rebalance write point */
918 dev_reserve *= ca->mi.bucket_size;
920 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
921 ca->mi.first_bucket);
923 reserved_sectors += dev_reserve * 2;
925 bucket_size_max = max_t(unsigned, bucket_size_max,
929 gc_reserve = c->opts.gc_reserve_bytes
930 ? c->opts.gc_reserve_bytes >> 9
931 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
933 reserved_sectors = max(gc_reserve, reserved_sectors);
935 reserved_sectors = min(reserved_sectors, capacity);
937 c->capacity = capacity - reserved_sectors;
939 c->bucket_size_max = bucket_size_max;
941 /* Wake up case someone was waiting for buckets */
942 closure_wake_up(&c->freelist_wait);
945 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
947 struct open_bucket *ob;
950 for (ob = c->open_buckets;
951 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
953 spin_lock(&ob->lock);
954 if (ob->valid && !ob->on_partial_list &&
955 ob->dev == ca->dev_idx)
957 spin_unlock(&ob->lock);
963 /* device goes ro: */
964 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
968 BUG_ON(ca->alloc_thread);
970 /* First, remove device from allocation groups: */
972 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
973 clear_bit(ca->dev_idx, c->rw_devs[i].d);
976 * Capacity is calculated based off of devices in allocation groups:
978 bch2_recalc_capacity(c);
980 /* Next, close write points that point to this device... */
981 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
982 bch2_writepoint_stop(c, ca, &c->write_points[i]);
984 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
985 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
986 bch2_writepoint_stop(c, ca, &c->btree_write_point);
988 mutex_lock(&c->btree_reserve_cache_lock);
989 while (c->btree_reserve_cache_nr) {
990 struct btree_alloc *a =
991 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
993 bch2_open_buckets_put(c, &a->ob);
995 mutex_unlock(&c->btree_reserve_cache_lock);
998 struct open_bucket *ob;
1000 spin_lock(&c->freelist_lock);
1001 if (!ca->open_buckets_partial_nr) {
1002 spin_unlock(&c->freelist_lock);
1005 ob = c->open_buckets +
1006 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1007 ob->on_partial_list = false;
1008 spin_unlock(&c->freelist_lock);
1010 bch2_open_bucket_put(c, ob);
1013 bch2_ec_stop_dev(c, ca);
1016 * Wake up threads that were blocked on allocation, so they can notice
1017 * the device can no longer be removed and the capacity has changed:
1019 closure_wake_up(&c->freelist_wait);
1022 * journal_res_get() can block waiting for free space in the journal -
1023 * it needs to notice there may not be devices to allocate from anymore:
1025 wake_up(&c->journal.wait);
1027 /* Now wait for any in flight writes: */
1029 closure_wait_event(&c->open_buckets_wait,
1030 !bch2_dev_has_open_write_point(c, ca));
1033 /* device goes rw: */
1034 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1038 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1039 if (ca->mi.data_allowed & (1 << i))
1040 set_bit(ca->dev_idx, c->rw_devs[i].d);
1043 void bch2_dev_allocator_quiesce(struct bch_fs *c, struct bch_dev *ca)
1045 if (ca->alloc_thread)
1046 closure_wait_event(&c->freelist_wait,
1047 ca->allocator_state != ALLOCATOR_running);
1050 /* stop allocator thread: */
1051 void bch2_dev_allocator_stop(struct bch_dev *ca)
1053 struct task_struct *p;
1055 p = rcu_dereference_protected(ca->alloc_thread, 1);
1056 ca->alloc_thread = NULL;
1059 * We need an rcu barrier between setting ca->alloc_thread = NULL and
1060 * the thread shutting down to avoid bch2_wake_allocator() racing:
1062 * XXX: it would be better to have the rcu barrier be asynchronous
1063 * instead of blocking us here
1073 /* start allocator thread: */
1074 int bch2_dev_allocator_start(struct bch_dev *ca)
1076 struct task_struct *p;
1079 * allocator thread already started?
1081 if (ca->alloc_thread)
1084 p = kthread_create(bch2_allocator_thread, ca,
1085 "bch-alloc/%s", ca->name);
1087 bch_err(ca->fs, "error creating allocator thread: %li",
1093 rcu_assign_pointer(ca->alloc_thread, p);
1098 void bch2_fs_allocator_background_init(struct bch_fs *c)
1100 spin_lock_init(&c->freelist_lock);