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"
20 #include <linux/kthread.h>
21 #include <linux/math64.h>
22 #include <linux/random.h>
23 #include <linux/rculist.h>
24 #include <linux/rcupdate.h>
25 #include <linux/sched/task.h>
26 #include <linux/sort.h>
27 #include <trace/events/bcachefs.h>
29 const char * const bch2_allocator_states[] = {
36 static const unsigned BCH_ALLOC_V1_FIELD_BYTES[] = {
37 #define x(name, bits) [BCH_ALLOC_FIELD_V1_##name] = bits / 8,
42 /* Persistent alloc info: */
44 static inline u64 alloc_field_v1_get(const struct bch_alloc *a,
45 const void **p, unsigned field)
47 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
50 if (!(a->fields & (1 << field)))
55 v = *((const u8 *) *p);
74 static inline void alloc_field_v1_put(struct bkey_i_alloc *a, void **p,
75 unsigned field, u64 v)
77 unsigned bytes = BCH_ALLOC_V1_FIELD_BYTES[field];
82 a->v.fields |= 1 << field;
89 *((__le16 *) *p) = cpu_to_le16(v);
92 *((__le32 *) *p) = cpu_to_le32(v);
95 *((__le64 *) *p) = cpu_to_le64(v);
104 static void bch2_alloc_unpack_v1(struct bkey_alloc_unpacked *out,
107 const struct bch_alloc *in = bkey_s_c_to_alloc(k).v;
108 const void *d = in->data;
113 #define x(_name, _bits) out->_name = alloc_field_v1_get(in, &d, idx++);
114 BCH_ALLOC_FIELDS_V1()
118 static int bch2_alloc_unpack_v2(struct bkey_alloc_unpacked *out,
121 struct bkey_s_c_alloc_v2 a = bkey_s_c_to_alloc_v2(k);
122 const u8 *in = a.v->data;
123 const u8 *end = bkey_val_end(a);
124 unsigned fieldnr = 0;
129 out->oldest_gen = a.v->oldest_gen;
130 out->data_type = a.v->data_type;
132 #define x(_name, _bits) \
133 if (fieldnr < a.v->nr_fields) { \
134 ret = bch2_varint_decode_fast(in, end, &v); \
142 if (v != out->_name) \
146 BCH_ALLOC_FIELDS_V2()
151 static int bch2_alloc_unpack_v3(struct bkey_alloc_unpacked *out,
154 struct bkey_s_c_alloc_v3 a = bkey_s_c_to_alloc_v3(k);
155 const u8 *in = a.v->data;
156 const u8 *end = bkey_val_end(a);
157 unsigned fieldnr = 0;
162 out->oldest_gen = a.v->oldest_gen;
163 out->data_type = a.v->data_type;
164 out->journal_seq = le64_to_cpu(a.v->journal_seq);
166 #define x(_name, _bits) \
167 if (fieldnr < a.v->nr_fields) { \
168 ret = bch2_varint_decode_fast(in, end, &v); \
176 if (v != out->_name) \
180 BCH_ALLOC_FIELDS_V2()
185 static void bch2_alloc_pack_v3(struct bkey_alloc_buf *dst,
186 const struct bkey_alloc_unpacked src)
188 struct bkey_i_alloc_v3 *a = bkey_alloc_v3_init(&dst->k);
189 unsigned nr_fields = 0, last_nonzero_fieldnr = 0;
191 u8 *end = (void *) &dst[1];
192 u8 *last_nonzero_field = out;
195 a->k.p = POS(src.dev, src.bucket);
197 a->v.oldest_gen = src.oldest_gen;
198 a->v.data_type = src.data_type;
199 a->v.journal_seq = cpu_to_le64(src.journal_seq);
201 #define x(_name, _bits) \
205 out += bch2_varint_encode_fast(out, src._name); \
207 last_nonzero_field = out; \
208 last_nonzero_fieldnr = nr_fields; \
213 BCH_ALLOC_FIELDS_V2()
217 out = last_nonzero_field;
218 a->v.nr_fields = last_nonzero_fieldnr;
220 bytes = (u8 *) out - (u8 *) &a->v;
221 set_bkey_val_bytes(&a->k, bytes);
222 memset_u64s_tail(&a->v, 0, bytes);
225 struct bkey_alloc_unpacked bch2_alloc_unpack(struct bkey_s_c k)
227 struct bkey_alloc_unpacked ret = {
229 .bucket = k.k->p.offset,
235 bch2_alloc_unpack_v1(&ret, k);
237 case KEY_TYPE_alloc_v2:
238 bch2_alloc_unpack_v2(&ret, k);
240 case KEY_TYPE_alloc_v3:
241 bch2_alloc_unpack_v3(&ret, k);
248 struct bkey_alloc_buf *bch2_alloc_pack(struct btree_trans *trans,
249 const struct bkey_alloc_unpacked src)
251 struct bkey_alloc_buf *dst;
253 dst = bch2_trans_kmalloc(trans, sizeof(struct bkey_alloc_buf));
255 bch2_alloc_pack_v3(dst, src);
260 int bch2_alloc_write(struct btree_trans *trans, struct btree_iter *iter,
261 struct bkey_alloc_unpacked *u, unsigned trigger_flags)
263 struct bkey_alloc_buf *a = bch2_alloc_pack(trans, *u);
265 return PTR_ERR_OR_ZERO(a) ?:
266 bch2_trans_update(trans, iter, &a->k, trigger_flags);
269 static unsigned bch_alloc_v1_val_u64s(const struct bch_alloc *a)
271 unsigned i, bytes = offsetof(struct bch_alloc, data);
273 for (i = 0; i < ARRAY_SIZE(BCH_ALLOC_V1_FIELD_BYTES); i++)
274 if (a->fields & (1 << i))
275 bytes += BCH_ALLOC_V1_FIELD_BYTES[i];
277 return DIV_ROUND_UP(bytes, sizeof(u64));
280 const char *bch2_alloc_v1_invalid(const struct bch_fs *c, struct bkey_s_c k)
282 struct bkey_s_c_alloc a = bkey_s_c_to_alloc(k);
284 if (k.k->p.inode >= c->sb.nr_devices ||
285 !c->devs[k.k->p.inode])
286 return "invalid device";
288 /* allow for unknown fields */
289 if (bkey_val_u64s(a.k) < bch_alloc_v1_val_u64s(a.v))
290 return "incorrect value size";
295 const char *bch2_alloc_v2_invalid(const struct bch_fs *c, struct bkey_s_c k)
297 struct bkey_alloc_unpacked u;
299 if (k.k->p.inode >= c->sb.nr_devices ||
300 !c->devs[k.k->p.inode])
301 return "invalid device";
303 if (bch2_alloc_unpack_v2(&u, k))
304 return "unpack error";
309 const char *bch2_alloc_v3_invalid(const struct bch_fs *c, struct bkey_s_c k)
311 struct bkey_alloc_unpacked u;
313 if (k.k->p.inode >= c->sb.nr_devices ||
314 !c->devs[k.k->p.inode])
315 return "invalid device";
317 if (bch2_alloc_unpack_v3(&u, k))
318 return "unpack error";
323 void bch2_alloc_to_text(struct printbuf *out, struct bch_fs *c,
326 struct bkey_alloc_unpacked u = bch2_alloc_unpack(k);
328 pr_buf(out, "gen %u oldest_gen %u data_type %s journal_seq %llu",
329 u.gen, u.oldest_gen, bch2_data_types[u.data_type],
331 #define x(_name, ...) pr_buf(out, " " #_name " %llu", (u64) u._name);
332 BCH_ALLOC_FIELDS_V2()
336 int bch2_alloc_read(struct bch_fs *c, bool gc, bool metadata_only)
338 struct btree_trans trans;
339 struct btree_iter iter;
343 struct bkey_alloc_unpacked u;
346 bch2_trans_init(&trans, c, 0, 0);
348 for_each_btree_key(&trans, iter, BTREE_ID_alloc, POS_MIN,
349 BTREE_ITER_PREFETCH, k, ret) {
350 ca = bch_dev_bkey_exists(c, k.k->p.inode);
351 g = __bucket(ca, k.k->p.offset, gc);
352 u = bch2_alloc_unpack(k);
355 *bucket_gen(ca, k.k->p.offset) = u.gen;
357 g->_mark.gen = u.gen;
358 g->io_time[READ] = u.read_time;
359 g->io_time[WRITE] = u.write_time;
360 g->oldest_gen = !gc ? u.oldest_gen : u.gen;
365 (u.data_type == BCH_DATA_user ||
366 u.data_type == BCH_DATA_cached ||
367 u.data_type == BCH_DATA_parity))) {
368 g->_mark.data_type = u.data_type;
369 g->_mark.dirty_sectors = u.dirty_sectors;
370 g->_mark.cached_sectors = u.cached_sectors;
371 g->_mark.stripe = u.stripe != 0;
372 g->stripe = u.stripe;
373 g->stripe_redundancy = u.stripe_redundancy;
377 bch2_trans_iter_exit(&trans, &iter);
379 bch2_trans_exit(&trans);
382 bch_err(c, "error reading alloc info: %i", ret);
387 /* Bucket IO clocks: */
389 int bch2_bucket_io_time_reset(struct btree_trans *trans, unsigned dev,
390 size_t bucket_nr, int rw)
392 struct bch_fs *c = trans->c;
393 struct btree_iter iter;
395 struct bkey_alloc_unpacked u;
399 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(dev, bucket_nr),
402 k = bch2_btree_iter_peek_slot(&iter);
407 u = bch2_alloc_unpack(k);
409 time = rw == READ ? &u.read_time : &u.write_time;
410 now = atomic64_read(&c->io_clock[rw].now);
416 ret = bch2_alloc_write(trans, &iter, &u, 0) ?:
417 bch2_trans_commit(trans, NULL, NULL, 0);
419 bch2_trans_iter_exit(trans, &iter);
423 /* Background allocator thread: */
426 * Scans for buckets to be invalidated, invalidates them, rewrites prios/gens
427 * (marking them as invalidated on disk), then optionally issues discard
428 * commands to the newly free buckets, then puts them on the various freelists.
431 static bool bch2_can_invalidate_bucket(struct bch_dev *ca, size_t b,
432 struct bucket_mark m)
436 if (!is_available_bucket(m))
439 if (m.owned_by_allocator)
442 if (ca->buckets_nouse &&
443 test_bit(b, ca->buckets_nouse))
446 if (ca->new_fs_bucket_idx) {
448 * Device or filesystem is still being initialized, and we
449 * haven't fully marked superblocks & journal:
451 if (is_superblock_bucket(ca, b))
454 if (b < ca->new_fs_bucket_idx)
458 gc_gen = bucket_gc_gen(bucket(ca, b));
460 ca->inc_gen_needs_gc += gc_gen >= BUCKET_GC_GEN_MAX / 2;
461 ca->inc_gen_really_needs_gc += gc_gen >= BUCKET_GC_GEN_MAX;
463 return gc_gen < BUCKET_GC_GEN_MAX;
467 * Determines what order we're going to reuse buckets, smallest bucket_key()
471 static unsigned bucket_sort_key(struct bucket *g, struct bucket_mark m,
472 u64 now, u64 last_seq_ondisk)
474 unsigned used = m.cached_sectors;
478 * Prefer to keep buckets that have been read more recently, and
479 * buckets that have more data in them:
481 u64 last_read = max_t(s64, 0, now - g->io_time[READ]);
482 u32 last_read_scaled = max_t(u64, U32_MAX, div_u64(last_read, used));
484 return -last_read_scaled;
487 * Prefer to use buckets with smaller gc_gen so that we don't
488 * have to walk the btree and recalculate oldest_gen - but shift
489 * off the low bits so that buckets will still have equal sort
490 * keys when there's only a small difference, so that we can
491 * keep sequential buckets together:
493 return 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))
543 bch2_bucket_needs_journal_commit(c, last_seq_ondisk,
545 ca->buckets_waiting_on_journal++;
549 if (e.nr && e.bucket + e.nr == b && e.key == key) {
553 heap_add_or_replace(&ca->alloc_heap, e,
554 -bucket_alloc_cmp, NULL);
556 e = (struct alloc_heap_entry) {
565 heap_add_or_replace(&ca->alloc_heap, e,
566 -bucket_alloc_cmp, NULL);
568 for (i = 0; i < ca->alloc_heap.used; i++)
569 nr += ca->alloc_heap.data[i].nr;
571 while (nr - ca->alloc_heap.data[0].nr >= ALLOC_SCAN_BATCH(ca)) {
572 nr -= ca->alloc_heap.data[0].nr;
573 heap_pop(&ca->alloc_heap, e, -bucket_alloc_cmp, NULL);
576 up_read(&ca->bucket_lock);
579 static size_t find_reclaimable_buckets(struct bch_fs *c, struct bch_dev *ca)
583 ca->inc_gen_needs_gc = 0;
584 ca->inc_gen_really_needs_gc = 0;
585 ca->buckets_waiting_on_journal = 0;
587 find_reclaimable_buckets_lru(c, ca);
589 heap_resort(&ca->alloc_heap, bucket_alloc_cmp, NULL);
591 for (i = 0; i < ca->alloc_heap.used; i++)
592 nr += ca->alloc_heap.data[i].nr;
597 static int bucket_invalidate_btree(struct btree_trans *trans,
598 struct bch_dev *ca, u64 b,
599 struct bkey_alloc_unpacked *u)
601 struct bch_fs *c = trans->c;
602 struct btree_iter iter;
606 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
611 k = bch2_btree_iter_peek_slot(&iter);
616 *u = bch2_alloc_unpack(k);
619 u->dirty_sectors = 0;
620 u->cached_sectors = 0;
621 u->read_time = atomic64_read(&c->io_clock[READ].now);
622 u->write_time = atomic64_read(&c->io_clock[WRITE].now);
624 ret = bch2_alloc_write(trans, &iter, u,
625 BTREE_TRIGGER_BUCKET_INVALIDATE);
627 bch2_trans_iter_exit(trans, &iter);
631 static int bch2_invalidate_one_bucket(struct bch_fs *c, struct bch_dev *ca,
632 u64 *journal_seq, unsigned flags)
634 struct bkey_alloc_unpacked u;
640 * If the read-only path is trying to shut down, we can't be generating
643 if (test_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags))
646 BUG_ON(!ca->alloc_heap.used ||
647 !ca->alloc_heap.data[0].nr);
648 b = ca->alloc_heap.data[0].bucket;
650 /* first, put on free_inc and mark as owned by allocator: */
651 percpu_down_read(&c->mark_lock);
653 bch2_mark_alloc_bucket(c, ca, b, true);
655 spin_lock(&c->freelist_lock);
656 verify_not_on_freelist(c, ca, b);
657 BUG_ON(!fifo_push(&ca->free_inc, b));
658 spin_unlock(&c->freelist_lock);
660 percpu_up_read(&c->mark_lock);
662 ret = bch2_trans_do(c, NULL, &commit_seq,
663 BTREE_INSERT_NOCHECK_RW|
665 BTREE_INSERT_JOURNAL_RESERVED|
667 bucket_invalidate_btree(&trans, ca, b, &u));
670 /* remove from alloc_heap: */
671 struct alloc_heap_entry e, *top = ca->alloc_heap.data;
677 heap_pop(&ca->alloc_heap, e, bucket_alloc_cmp, NULL);
680 * If we invalidating cached data then we need to wait on the
684 *journal_seq = max(*journal_seq, commit_seq);
687 * We already waiting on u.alloc_seq when we filtered out
688 * buckets that need journal commit:
690 BUG_ON(*journal_seq > u.journal_seq);
694 /* remove from free_inc: */
695 percpu_down_read(&c->mark_lock);
696 spin_lock(&c->freelist_lock);
698 bch2_mark_alloc_bucket(c, ca, b, false);
700 BUG_ON(!fifo_pop_back(&ca->free_inc, b2));
703 spin_unlock(&c->freelist_lock);
704 percpu_up_read(&c->mark_lock);
707 return ret < 0 ? ret : 0;
711 * Pull buckets off ca->alloc_heap, invalidate them, move them to ca->free_inc:
713 static int bch2_invalidate_buckets(struct bch_fs *c, struct bch_dev *ca)
718 /* Only use nowait if we've already invalidated at least one bucket: */
720 !fifo_full(&ca->free_inc) &&
721 ca->alloc_heap.used) {
722 if (kthread_should_stop()) {
727 ret = bch2_invalidate_one_bucket(c, ca, &journal_seq,
728 (!fifo_empty(&ca->free_inc)
729 ? BTREE_INSERT_NOWAIT : 0));
731 * We only want to batch up invalidates when they're going to
732 * require flushing the journal:
738 /* If we used NOWAIT, don't return the error: */
739 if (!fifo_empty(&ca->free_inc))
742 bch_err(ca, "error invalidating buckets: %i", ret);
747 ret = bch2_journal_flush_seq(&c->journal, journal_seq);
749 bch_err(ca, "journal error: %i", ret);
756 static void alloc_thread_set_state(struct bch_dev *ca, unsigned new_state)
758 if (ca->allocator_state != new_state) {
759 ca->allocator_state = new_state;
760 closure_wake_up(&ca->fs->freelist_wait);
764 static int push_invalidated_bucket(struct bch_fs *c, struct bch_dev *ca, u64 b)
769 spin_lock(&c->freelist_lock);
770 for (i = 0; i < RESERVE_NR; i++) {
772 * Don't strand buckets on the copygc freelist until
773 * after recovery is finished:
775 if (i == RESERVE_MOVINGGC &&
776 !test_bit(BCH_FS_STARTED, &c->flags))
779 if (fifo_push(&ca->free[i], b)) {
780 fifo_pop(&ca->free_inc, b);
785 spin_unlock(&c->freelist_lock);
787 ca->allocator_state = ret
789 : ALLOCATOR_blocked_full;
790 closure_wake_up(&c->freelist_wait);
794 static void discard_one_bucket(struct bch_fs *c, struct bch_dev *ca, u64 b)
796 if (ca->mi.discard &&
797 blk_queue_discard(bdev_get_queue(ca->disk_sb.bdev)))
798 blkdev_issue_discard(ca->disk_sb.bdev, bucket_to_sector(ca, b),
799 ca->mi.bucket_size, GFP_NOFS, 0);
802 static bool allocator_thread_running(struct bch_dev *ca)
804 unsigned state = ca->mi.state == BCH_MEMBER_STATE_rw &&
805 test_bit(BCH_FS_ALLOCATOR_RUNNING, &ca->fs->flags)
808 alloc_thread_set_state(ca, state);
809 return state == ALLOCATOR_running;
812 static int buckets_available(struct bch_dev *ca, unsigned long gc_count)
814 s64 available = dev_buckets_reclaimable(ca) -
815 (gc_count == ca->fs->gc_count ? ca->inc_gen_really_needs_gc : 0);
816 bool ret = available > 0;
818 alloc_thread_set_state(ca, ret
820 : ALLOCATOR_blocked);
825 * bch_allocator_thread - move buckets from free_inc to reserves
827 * The free_inc FIFO is populated by find_reclaimable_buckets(), and
828 * the reserves are depleted by bucket allocation. When we run out
829 * of free_inc, try to invalidate some buckets and write out
832 static int bch2_allocator_thread(void *arg)
834 struct bch_dev *ca = arg;
835 struct bch_fs *c = ca->fs;
836 unsigned long gc_count = c->gc_count;
843 ret = kthread_wait_freezable(allocator_thread_running(ca));
847 while (!ca->alloc_heap.used) {
850 ret = kthread_wait_freezable(buckets_available(ca, gc_count));
854 gc_count = c->gc_count;
855 nr = find_reclaimable_buckets(c, ca);
857 if (!nr && ca->buckets_waiting_on_journal) {
858 ret = bch2_journal_flush(&c->journal);
861 } else if (nr < (ca->mi.nbuckets >> 6) &&
862 ca->buckets_waiting_on_journal >= nr / 2) {
863 bch2_journal_flush_async(&c->journal, NULL);
866 if ((ca->inc_gen_needs_gc >= ALLOC_SCAN_BATCH(ca) ||
867 ca->inc_gen_really_needs_gc) &&
869 atomic_inc(&c->kick_gc);
870 wake_up_process(c->gc_thread);
873 trace_alloc_scan(ca, nr, ca->inc_gen_needs_gc,
874 ca->inc_gen_really_needs_gc);
877 ret = bch2_invalidate_buckets(c, ca);
881 while (!fifo_empty(&ca->free_inc)) {
882 u64 b = fifo_peek(&ca->free_inc);
884 discard_one_bucket(c, ca, b);
886 ret = kthread_wait_freezable(push_invalidated_bucket(c, ca, b));
892 alloc_thread_set_state(ca, ALLOCATOR_stopped);
896 /* Startup/shutdown (ro/rw): */
898 void bch2_recalc_capacity(struct bch_fs *c)
901 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
902 unsigned bucket_size_max = 0;
903 unsigned long ra_pages = 0;
906 lockdep_assert_held(&c->state_lock);
908 for_each_online_member(ca, c, i) {
909 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
911 ra_pages += bdi->ra_pages;
914 bch2_set_ra_pages(c, ra_pages);
916 for_each_rw_member(ca, c, i) {
920 * We need to reserve buckets (from the number
921 * of currently available buckets) against
922 * foreground writes so that mainly copygc can
923 * make forward progress.
925 * We need enough to refill the various reserves
926 * from scratch - copygc will use its entire
927 * reserve all at once, then run against when
928 * its reserve is refilled (from the formerly
929 * available buckets).
931 * This reserve is just used when considering if
932 * allocations for foreground writes must wait -
933 * not -ENOSPC calculations.
935 for (j = 0; j < RESERVE_NONE; j++)
936 dev_reserve += ca->free[j].size;
938 dev_reserve += 1; /* btree write point */
939 dev_reserve += 1; /* copygc write point */
940 dev_reserve += 1; /* rebalance write point */
942 dev_reserve *= ca->mi.bucket_size;
944 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
945 ca->mi.first_bucket);
947 reserved_sectors += dev_reserve * 2;
949 bucket_size_max = max_t(unsigned, bucket_size_max,
953 gc_reserve = c->opts.gc_reserve_bytes
954 ? c->opts.gc_reserve_bytes >> 9
955 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
957 reserved_sectors = max(gc_reserve, reserved_sectors);
959 reserved_sectors = min(reserved_sectors, capacity);
961 c->capacity = capacity - reserved_sectors;
963 c->bucket_size_max = bucket_size_max;
965 /* Wake up case someone was waiting for buckets */
966 closure_wake_up(&c->freelist_wait);
969 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
971 struct open_bucket *ob;
974 for (ob = c->open_buckets;
975 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
977 spin_lock(&ob->lock);
978 if (ob->valid && !ob->on_partial_list &&
979 ob->dev == ca->dev_idx)
981 spin_unlock(&ob->lock);
987 /* device goes ro: */
988 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
992 BUG_ON(ca->alloc_thread);
994 /* First, remove device from allocation groups: */
996 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
997 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1000 * Capacity is calculated based off of devices in allocation groups:
1002 bch2_recalc_capacity(c);
1004 /* Next, close write points that point to this device... */
1005 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1006 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1008 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1009 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1010 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1012 mutex_lock(&c->btree_reserve_cache_lock);
1013 while (c->btree_reserve_cache_nr) {
1014 struct btree_alloc *a =
1015 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1017 bch2_open_buckets_put(c, &a->ob);
1019 mutex_unlock(&c->btree_reserve_cache_lock);
1022 struct open_bucket *ob;
1024 spin_lock(&c->freelist_lock);
1025 if (!ca->open_buckets_partial_nr) {
1026 spin_unlock(&c->freelist_lock);
1029 ob = c->open_buckets +
1030 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1031 ob->on_partial_list = false;
1032 spin_unlock(&c->freelist_lock);
1034 bch2_open_bucket_put(c, ob);
1037 bch2_ec_stop_dev(c, ca);
1040 * Wake up threads that were blocked on allocation, so they can notice
1041 * the device can no longer be removed and the capacity has changed:
1043 closure_wake_up(&c->freelist_wait);
1046 * journal_res_get() can block waiting for free space in the journal -
1047 * it needs to notice there may not be devices to allocate from anymore:
1049 wake_up(&c->journal.wait);
1051 /* Now wait for any in flight writes: */
1053 closure_wait_event(&c->open_buckets_wait,
1054 !bch2_dev_has_open_write_point(c, ca));
1057 /* device goes rw: */
1058 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1062 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1063 if (ca->mi.data_allowed & (1 << i))
1064 set_bit(ca->dev_idx, c->rw_devs[i].d);
1067 void bch2_dev_allocator_quiesce(struct bch_fs *c, struct bch_dev *ca)
1069 if (ca->alloc_thread)
1070 closure_wait_event(&c->freelist_wait,
1071 ca->allocator_state != ALLOCATOR_running);
1074 /* stop allocator thread: */
1075 void bch2_dev_allocator_stop(struct bch_dev *ca)
1077 struct task_struct *p;
1079 p = rcu_dereference_protected(ca->alloc_thread, 1);
1080 ca->alloc_thread = NULL;
1083 * We need an rcu barrier between setting ca->alloc_thread = NULL and
1084 * the thread shutting down to avoid bch2_wake_allocator() racing:
1086 * XXX: it would be better to have the rcu barrier be asynchronous
1087 * instead of blocking us here
1097 /* start allocator thread: */
1098 int bch2_dev_allocator_start(struct bch_dev *ca)
1100 struct task_struct *p;
1103 * allocator thread already started?
1105 if (ca->alloc_thread)
1108 p = kthread_create(bch2_allocator_thread, ca,
1109 "bch-alloc/%s", ca->name);
1111 bch_err(ca->fs, "error creating allocator thread: %li",
1117 rcu_assign_pointer(ca->alloc_thread, p);
1122 void bch2_fs_allocator_background_init(struct bch_fs *c)
1124 spin_lock_init(&c->freelist_lock);