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->buckets_waiting_for_journal,
546 ca->buckets_waiting_on_journal++;
550 if (e.nr && e.bucket + e.nr == b && e.key == key) {
554 heap_add_or_replace(&ca->alloc_heap, e,
555 -bucket_alloc_cmp, NULL);
557 e = (struct alloc_heap_entry) {
566 heap_add_or_replace(&ca->alloc_heap, e,
567 -bucket_alloc_cmp, NULL);
569 for (i = 0; i < ca->alloc_heap.used; i++)
570 nr += ca->alloc_heap.data[i].nr;
572 while (nr - ca->alloc_heap.data[0].nr >= ALLOC_SCAN_BATCH(ca)) {
573 nr -= ca->alloc_heap.data[0].nr;
574 heap_pop(&ca->alloc_heap, e, -bucket_alloc_cmp, NULL);
577 up_read(&ca->bucket_lock);
580 static size_t find_reclaimable_buckets(struct bch_fs *c, struct bch_dev *ca)
584 ca->inc_gen_needs_gc = 0;
585 ca->inc_gen_really_needs_gc = 0;
586 ca->buckets_waiting_on_journal = 0;
588 find_reclaimable_buckets_lru(c, ca);
590 heap_resort(&ca->alloc_heap, bucket_alloc_cmp, NULL);
592 for (i = 0; i < ca->alloc_heap.used; i++)
593 nr += ca->alloc_heap.data[i].nr;
598 static int bucket_invalidate_btree(struct btree_trans *trans,
599 struct bch_dev *ca, u64 b,
600 struct bkey_alloc_unpacked *u)
602 struct bch_fs *c = trans->c;
603 struct btree_iter iter;
607 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
612 k = bch2_btree_iter_peek_slot(&iter);
617 *u = bch2_alloc_unpack(k);
620 u->dirty_sectors = 0;
621 u->cached_sectors = 0;
622 u->read_time = atomic64_read(&c->io_clock[READ].now);
623 u->write_time = atomic64_read(&c->io_clock[WRITE].now);
625 ret = bch2_alloc_write(trans, &iter, u,
626 BTREE_TRIGGER_BUCKET_INVALIDATE);
628 bch2_trans_iter_exit(trans, &iter);
632 static int bch2_invalidate_one_bucket(struct bch_fs *c, struct bch_dev *ca,
633 u64 *journal_seq, unsigned flags)
635 struct bkey_alloc_unpacked u;
641 * If the read-only path is trying to shut down, we can't be generating
644 if (test_bit(BCH_FS_ALLOCATOR_STOPPING, &c->flags))
647 BUG_ON(!ca->alloc_heap.used ||
648 !ca->alloc_heap.data[0].nr);
649 b = ca->alloc_heap.data[0].bucket;
651 /* first, put on free_inc and mark as owned by allocator: */
652 percpu_down_read(&c->mark_lock);
654 bch2_mark_alloc_bucket(c, ca, b, true);
656 spin_lock(&c->freelist_lock);
657 verify_not_on_freelist(c, ca, b);
658 BUG_ON(!fifo_push(&ca->free_inc, b));
659 spin_unlock(&c->freelist_lock);
661 percpu_up_read(&c->mark_lock);
663 ret = bch2_trans_do(c, NULL, &commit_seq,
664 BTREE_INSERT_NOCHECK_RW|
666 BTREE_INSERT_JOURNAL_RESERVED|
668 bucket_invalidate_btree(&trans, ca, b, &u));
671 /* remove from alloc_heap: */
672 struct alloc_heap_entry e, *top = ca->alloc_heap.data;
678 heap_pop(&ca->alloc_heap, e, bucket_alloc_cmp, NULL);
681 * If we invalidating cached data then we need to wait on the
685 *journal_seq = max(*journal_seq, commit_seq);
688 * We already waiting on u.alloc_seq when we filtered out
689 * buckets that need journal commit:
691 BUG_ON(*journal_seq > u.journal_seq);
695 /* remove from free_inc: */
696 percpu_down_read(&c->mark_lock);
697 spin_lock(&c->freelist_lock);
699 bch2_mark_alloc_bucket(c, ca, b, false);
701 BUG_ON(!fifo_pop_back(&ca->free_inc, b2));
704 spin_unlock(&c->freelist_lock);
705 percpu_up_read(&c->mark_lock);
708 return ret < 0 ? ret : 0;
712 * Pull buckets off ca->alloc_heap, invalidate them, move them to ca->free_inc:
714 static int bch2_invalidate_buckets(struct bch_fs *c, struct bch_dev *ca)
719 /* Only use nowait if we've already invalidated at least one bucket: */
721 !fifo_full(&ca->free_inc) &&
722 ca->alloc_heap.used) {
723 if (kthread_should_stop()) {
728 ret = bch2_invalidate_one_bucket(c, ca, &journal_seq,
729 (!fifo_empty(&ca->free_inc)
730 ? BTREE_INSERT_NOWAIT : 0));
732 * We only want to batch up invalidates when they're going to
733 * require flushing the journal:
739 /* If we used NOWAIT, don't return the error: */
740 if (!fifo_empty(&ca->free_inc))
743 bch_err(ca, "error invalidating buckets: %i", ret);
748 ret = bch2_journal_flush_seq(&c->journal, journal_seq);
750 bch_err(ca, "journal error: %i", ret);
757 static void alloc_thread_set_state(struct bch_dev *ca, unsigned new_state)
759 if (ca->allocator_state != new_state) {
760 ca->allocator_state = new_state;
761 closure_wake_up(&ca->fs->freelist_wait);
765 static int push_invalidated_bucket(struct bch_fs *c, struct bch_dev *ca, u64 b)
770 spin_lock(&c->freelist_lock);
771 for (i = 0; i < RESERVE_NR; i++) {
773 * Don't strand buckets on the copygc freelist until
774 * after recovery is finished:
776 if (i == RESERVE_MOVINGGC &&
777 !test_bit(BCH_FS_STARTED, &c->flags))
780 if (fifo_push(&ca->free[i], b)) {
781 fifo_pop(&ca->free_inc, b);
786 spin_unlock(&c->freelist_lock);
788 ca->allocator_state = ret
790 : ALLOCATOR_blocked_full;
791 closure_wake_up(&c->freelist_wait);
795 static void discard_one_bucket(struct bch_fs *c, struct bch_dev *ca, u64 b)
797 if (!c->opts.nochanges &&
799 blk_queue_discard(bdev_get_queue(ca->disk_sb.bdev)))
800 blkdev_issue_discard(ca->disk_sb.bdev, bucket_to_sector(ca, b),
801 ca->mi.bucket_size, GFP_NOFS, 0);
804 static bool allocator_thread_running(struct bch_dev *ca)
806 unsigned state = ca->mi.state == BCH_MEMBER_STATE_rw &&
807 test_bit(BCH_FS_ALLOCATOR_RUNNING, &ca->fs->flags)
810 alloc_thread_set_state(ca, state);
811 return state == ALLOCATOR_running;
814 static int buckets_available(struct bch_dev *ca, unsigned long gc_count)
816 s64 available = dev_buckets_reclaimable(ca) -
817 (gc_count == ca->fs->gc_count ? ca->inc_gen_really_needs_gc : 0);
818 bool ret = available > 0;
820 alloc_thread_set_state(ca, ret
822 : ALLOCATOR_blocked);
827 * bch_allocator_thread - move buckets from free_inc to reserves
829 * The free_inc FIFO is populated by find_reclaimable_buckets(), and
830 * the reserves are depleted by bucket allocation. When we run out
831 * of free_inc, try to invalidate some buckets and write out
834 static int bch2_allocator_thread(void *arg)
836 struct bch_dev *ca = arg;
837 struct bch_fs *c = ca->fs;
838 unsigned long gc_count = c->gc_count;
845 ret = kthread_wait_freezable(allocator_thread_running(ca));
849 while (!ca->alloc_heap.used) {
852 ret = kthread_wait_freezable(buckets_available(ca, gc_count));
856 gc_count = c->gc_count;
857 nr = find_reclaimable_buckets(c, ca);
859 if (!nr && ca->buckets_waiting_on_journal) {
860 ret = bch2_journal_flush(&c->journal);
863 } else if (nr < (ca->mi.nbuckets >> 6) &&
864 ca->buckets_waiting_on_journal >= nr / 2) {
865 bch2_journal_flush_async(&c->journal, NULL);
868 if ((ca->inc_gen_needs_gc >= ALLOC_SCAN_BATCH(ca) ||
869 ca->inc_gen_really_needs_gc) &&
871 atomic_inc(&c->kick_gc);
872 wake_up_process(c->gc_thread);
875 trace_alloc_scan(ca, nr, ca->inc_gen_needs_gc,
876 ca->inc_gen_really_needs_gc);
879 ret = bch2_invalidate_buckets(c, ca);
883 while (!fifo_empty(&ca->free_inc)) {
884 u64 b = fifo_peek(&ca->free_inc);
886 discard_one_bucket(c, ca, b);
888 ret = kthread_wait_freezable(push_invalidated_bucket(c, ca, b));
894 alloc_thread_set_state(ca, ALLOCATOR_stopped);
898 /* Startup/shutdown (ro/rw): */
900 void bch2_recalc_capacity(struct bch_fs *c)
903 u64 capacity = 0, reserved_sectors = 0, gc_reserve;
904 unsigned bucket_size_max = 0;
905 unsigned long ra_pages = 0;
908 lockdep_assert_held(&c->state_lock);
910 for_each_online_member(ca, c, i) {
911 struct backing_dev_info *bdi = ca->disk_sb.bdev->bd_disk->bdi;
913 ra_pages += bdi->ra_pages;
916 bch2_set_ra_pages(c, ra_pages);
918 for_each_rw_member(ca, c, i) {
922 * We need to reserve buckets (from the number
923 * of currently available buckets) against
924 * foreground writes so that mainly copygc can
925 * make forward progress.
927 * We need enough to refill the various reserves
928 * from scratch - copygc will use its entire
929 * reserve all at once, then run against when
930 * its reserve is refilled (from the formerly
931 * available buckets).
933 * This reserve is just used when considering if
934 * allocations for foreground writes must wait -
935 * not -ENOSPC calculations.
937 for (j = 0; j < RESERVE_NONE; j++)
938 dev_reserve += ca->free[j].size;
940 dev_reserve += 1; /* btree write point */
941 dev_reserve += 1; /* copygc write point */
942 dev_reserve += 1; /* rebalance write point */
944 dev_reserve *= ca->mi.bucket_size;
946 capacity += bucket_to_sector(ca, ca->mi.nbuckets -
947 ca->mi.first_bucket);
949 reserved_sectors += dev_reserve * 2;
951 bucket_size_max = max_t(unsigned, bucket_size_max,
955 gc_reserve = c->opts.gc_reserve_bytes
956 ? c->opts.gc_reserve_bytes >> 9
957 : div64_u64(capacity * c->opts.gc_reserve_percent, 100);
959 reserved_sectors = max(gc_reserve, reserved_sectors);
961 reserved_sectors = min(reserved_sectors, capacity);
963 c->capacity = capacity - reserved_sectors;
965 c->bucket_size_max = bucket_size_max;
967 /* Wake up case someone was waiting for buckets */
968 closure_wake_up(&c->freelist_wait);
971 static bool bch2_dev_has_open_write_point(struct bch_fs *c, struct bch_dev *ca)
973 struct open_bucket *ob;
976 for (ob = c->open_buckets;
977 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
979 spin_lock(&ob->lock);
980 if (ob->valid && !ob->on_partial_list &&
981 ob->dev == ca->dev_idx)
983 spin_unlock(&ob->lock);
989 /* device goes ro: */
990 void bch2_dev_allocator_remove(struct bch_fs *c, struct bch_dev *ca)
994 BUG_ON(ca->alloc_thread);
996 /* First, remove device from allocation groups: */
998 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
999 clear_bit(ca->dev_idx, c->rw_devs[i].d);
1002 * Capacity is calculated based off of devices in allocation groups:
1004 bch2_recalc_capacity(c);
1006 /* Next, close write points that point to this device... */
1007 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1008 bch2_writepoint_stop(c, ca, &c->write_points[i]);
1010 bch2_writepoint_stop(c, ca, &c->copygc_write_point);
1011 bch2_writepoint_stop(c, ca, &c->rebalance_write_point);
1012 bch2_writepoint_stop(c, ca, &c->btree_write_point);
1014 mutex_lock(&c->btree_reserve_cache_lock);
1015 while (c->btree_reserve_cache_nr) {
1016 struct btree_alloc *a =
1017 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1019 bch2_open_buckets_put(c, &a->ob);
1021 mutex_unlock(&c->btree_reserve_cache_lock);
1024 struct open_bucket *ob;
1026 spin_lock(&c->freelist_lock);
1027 if (!ca->open_buckets_partial_nr) {
1028 spin_unlock(&c->freelist_lock);
1031 ob = c->open_buckets +
1032 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
1033 ob->on_partial_list = false;
1034 spin_unlock(&c->freelist_lock);
1036 bch2_open_bucket_put(c, ob);
1039 bch2_ec_stop_dev(c, ca);
1042 * Wake up threads that were blocked on allocation, so they can notice
1043 * the device can no longer be removed and the capacity has changed:
1045 closure_wake_up(&c->freelist_wait);
1048 * journal_res_get() can block waiting for free space in the journal -
1049 * it needs to notice there may not be devices to allocate from anymore:
1051 wake_up(&c->journal.wait);
1053 /* Now wait for any in flight writes: */
1055 closure_wait_event(&c->open_buckets_wait,
1056 !bch2_dev_has_open_write_point(c, ca));
1059 /* device goes rw: */
1060 void bch2_dev_allocator_add(struct bch_fs *c, struct bch_dev *ca)
1064 for (i = 0; i < ARRAY_SIZE(c->rw_devs); i++)
1065 if (ca->mi.data_allowed & (1 << i))
1066 set_bit(ca->dev_idx, c->rw_devs[i].d);
1069 void bch2_dev_allocator_quiesce(struct bch_fs *c, struct bch_dev *ca)
1071 if (ca->alloc_thread)
1072 closure_wait_event(&c->freelist_wait,
1073 ca->allocator_state != ALLOCATOR_running);
1076 /* stop allocator thread: */
1077 void bch2_dev_allocator_stop(struct bch_dev *ca)
1079 struct task_struct *p;
1081 p = rcu_dereference_protected(ca->alloc_thread, 1);
1082 ca->alloc_thread = NULL;
1085 * We need an rcu barrier between setting ca->alloc_thread = NULL and
1086 * the thread shutting down to avoid bch2_wake_allocator() racing:
1088 * XXX: it would be better to have the rcu barrier be asynchronous
1089 * instead of blocking us here
1099 /* start allocator thread: */
1100 int bch2_dev_allocator_start(struct bch_dev *ca)
1102 struct task_struct *p;
1105 * allocator thread already started?
1107 if (ca->alloc_thread)
1110 p = kthread_create(bch2_allocator_thread, ca,
1111 "bch-alloc/%s", ca->name);
1113 bch_err(ca->fs, "error creating allocator thread: %li",
1119 rcu_assign_pointer(ca->alloc_thread, p);
1124 void bch2_fs_allocator_background_init(struct bch_fs *c)
1126 spin_lock_init(&c->freelist_lock);