1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2012 Google, Inc.
5 * Foreground allocator code: allocate buckets from freelist, and allocate in
6 * sector granularity from writepoints.
8 * bch2_bucket_alloc() allocates a single bucket from a specific device.
10 * bch2_bucket_alloc_set() allocates one or more buckets from different devices
11 * in a given filesystem.
15 #include "alloc_background.h"
16 #include "alloc_foreground.h"
17 #include "backpointers.h"
18 #include "btree_iter.h"
19 #include "btree_update.h"
22 #include "buckets_waiting_for_journal.h"
25 #include "disk_groups.h"
31 #include "nocow_locking.h"
34 #include <linux/math64.h>
35 #include <linux/rculist.h>
36 #include <linux/rcupdate.h>
38 const char * const bch2_alloc_reserves[] = {
46 * Open buckets represent a bucket that's currently being allocated from. They
49 * - They track buckets that have been partially allocated, allowing for
50 * sub-bucket sized allocations - they're used by the sector allocator below
52 * - They provide a reference to the buckets they own that mark and sweep GC
53 * can find, until the new allocation has a pointer to it inserted into the
56 * When allocating some space with the sector allocator, the allocation comes
57 * with a reference to an open bucket - the caller is required to put that
58 * reference _after_ doing the index update that makes its allocation reachable.
61 void bch2_reset_alloc_cursors(struct bch_fs *c)
67 for_each_member_device_rcu(ca, c, i, NULL)
72 static void bch2_open_bucket_hash_add(struct bch_fs *c, struct open_bucket *ob)
74 open_bucket_idx_t idx = ob - c->open_buckets;
75 open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
81 static void bch2_open_bucket_hash_remove(struct bch_fs *c, struct open_bucket *ob)
83 open_bucket_idx_t idx = ob - c->open_buckets;
84 open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
86 while (*slot != idx) {
88 slot = &c->open_buckets[*slot].hash;
95 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
97 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
100 ec_stripe_new_put(c, ob->ec, STRIPE_REF_io);
104 percpu_down_read(&c->mark_lock);
105 spin_lock(&ob->lock);
110 spin_unlock(&ob->lock);
111 percpu_up_read(&c->mark_lock);
113 spin_lock(&c->freelist_lock);
114 bch2_open_bucket_hash_remove(c, ob);
116 ob->freelist = c->open_buckets_freelist;
117 c->open_buckets_freelist = ob - c->open_buckets;
119 c->open_buckets_nr_free++;
120 ca->nr_open_buckets--;
121 spin_unlock(&c->freelist_lock);
123 closure_wake_up(&c->open_buckets_wait);
126 void bch2_open_bucket_write_error(struct bch_fs *c,
127 struct open_buckets *obs,
130 struct open_bucket *ob;
133 open_bucket_for_each(c, obs, ob, i)
134 if (ob->dev == dev && ob->ec)
135 bch2_ec_bucket_cancel(c, ob);
138 static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
140 struct open_bucket *ob;
142 BUG_ON(!c->open_buckets_freelist || !c->open_buckets_nr_free);
144 ob = c->open_buckets + c->open_buckets_freelist;
145 c->open_buckets_freelist = ob->freelist;
146 atomic_set(&ob->pin, 1);
149 c->open_buckets_nr_free--;
153 static void open_bucket_free_unused(struct bch_fs *c,
154 struct write_point *wp,
155 struct open_bucket *ob)
157 BUG_ON(c->open_buckets_partial_nr >=
158 ARRAY_SIZE(c->open_buckets_partial));
160 spin_lock(&c->freelist_lock);
161 ob->on_partial_list = true;
162 c->open_buckets_partial[c->open_buckets_partial_nr++] =
163 ob - c->open_buckets;
164 spin_unlock(&c->freelist_lock);
166 closure_wake_up(&c->open_buckets_wait);
167 closure_wake_up(&c->freelist_wait);
170 /* _only_ for allocating the journal on a new device: */
171 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
173 while (ca->new_fs_bucket_idx < ca->mi.nbuckets) {
174 u64 b = ca->new_fs_bucket_idx++;
176 if (!is_superblock_bucket(ca, b) &&
177 (!ca->buckets_nouse || !test_bit(b, ca->buckets_nouse)))
184 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
188 case RESERVE_btree_movinggc:
190 case RESERVE_movinggc:
191 return OPEN_BUCKETS_COUNT / 4;
193 return OPEN_BUCKETS_COUNT / 2;
197 static struct open_bucket *__try_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
199 enum alloc_reserve reserve,
200 const struct bch_alloc_v4 *a,
201 struct bucket_alloc_state *s,
204 struct open_bucket *ob;
206 if (unlikely(ca->buckets_nouse && test_bit(bucket, ca->buckets_nouse))) {
211 if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
216 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
217 c->journal.flushed_seq_ondisk, ca->dev_idx, bucket)) {
218 s->skipped_need_journal_commit++;
222 if (bch2_bucket_nocow_is_locked(&c->nocow_locks, POS(ca->dev_idx, bucket))) {
227 spin_lock(&c->freelist_lock);
229 if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
231 closure_wait(&c->open_buckets_wait, cl);
233 if (!c->blocked_allocate_open_bucket)
234 c->blocked_allocate_open_bucket = local_clock();
236 spin_unlock(&c->freelist_lock);
237 return ERR_PTR(-BCH_ERR_open_buckets_empty);
240 /* Recheck under lock: */
241 if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
242 spin_unlock(&c->freelist_lock);
247 ob = bch2_open_bucket_alloc(c);
249 spin_lock(&ob->lock);
252 ob->sectors_free = ca->mi.bucket_size;
253 ob->dev = ca->dev_idx;
256 spin_unlock(&ob->lock);
258 ca->nr_open_buckets++;
259 bch2_open_bucket_hash_add(c, ob);
261 if (c->blocked_allocate_open_bucket) {
262 bch2_time_stats_update(
263 &c->times[BCH_TIME_blocked_allocate_open_bucket],
264 c->blocked_allocate_open_bucket);
265 c->blocked_allocate_open_bucket = 0;
268 if (c->blocked_allocate) {
269 bch2_time_stats_update(
270 &c->times[BCH_TIME_blocked_allocate],
271 c->blocked_allocate);
272 c->blocked_allocate = 0;
275 spin_unlock(&c->freelist_lock);
279 static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bch_dev *ca,
280 enum alloc_reserve reserve, u64 free_entry,
281 struct bucket_alloc_state *s,
282 struct bkey_s_c freespace_k,
285 struct bch_fs *c = trans->c;
286 struct btree_iter iter = { NULL };
288 struct open_bucket *ob;
289 struct bch_alloc_v4 a_convert;
290 const struct bch_alloc_v4 *a;
291 u64 b = free_entry & ~(~0ULL << 56);
292 unsigned genbits = free_entry >> 56;
293 struct printbuf buf = PRINTBUF;
296 if (b < ca->mi.first_bucket || b >= ca->mi.nbuckets) {
297 prt_printf(&buf, "freespace btree has bucket outside allowed range %u-%llu\n"
299 ca->mi.first_bucket, ca->mi.nbuckets);
300 bch2_bkey_val_to_text(&buf, c, freespace_k);
301 bch2_trans_inconsistent(trans, "%s", buf.buf);
306 k = bch2_bkey_get_iter(trans, &iter,
307 BTREE_ID_alloc, POS(ca->dev_idx, b),
315 a = bch2_alloc_to_v4(k, &a_convert);
317 if (a->data_type != BCH_DATA_free) {
318 if (!test_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags)) {
323 prt_printf(&buf, "non free bucket in freespace btree\n"
325 bch2_bkey_val_to_text(&buf, c, freespace_k);
326 prt_printf(&buf, "\n ");
327 bch2_bkey_val_to_text(&buf, c, k);
328 bch2_trans_inconsistent(trans, "%s", buf.buf);
333 if (genbits != (alloc_freespace_genbits(*a) >> 56) &&
334 test_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags)) {
335 prt_printf(&buf, "bucket in freespace btree with wrong genbits (got %u should be %llu)\n"
337 genbits, alloc_freespace_genbits(*a) >> 56);
338 bch2_bkey_val_to_text(&buf, c, freespace_k);
339 prt_printf(&buf, "\n ");
340 bch2_bkey_val_to_text(&buf, c, k);
341 bch2_trans_inconsistent(trans, "%s", buf.buf);
347 if (!test_bit(BCH_FS_CHECK_BACKPOINTERS_DONE, &c->flags)) {
348 struct bch_backpointer bp;
349 struct bpos bp_pos = POS_MIN;
351 ret = bch2_get_next_backpointer(trans, POS(ca->dev_idx, b), -1,
353 BTREE_ITER_NOPRESERVE);
359 if (!bkey_eq(bp_pos, POS_MAX)) {
361 * Bucket may have data in it - we don't call
362 * bc2h_trans_inconnsistent() because fsck hasn't
370 ob = __try_alloc_bucket(c, ca, b, reserve, a, s, cl);
372 iter.path->preserve = false;
374 set_btree_iter_dontneed(&iter);
375 bch2_trans_iter_exit(trans, &iter);
381 * This path is for before the freespace btree is initialized:
383 * If ca->new_fs_bucket_idx is nonzero, we haven't yet marked superblock &
384 * journal buckets - journal buckets will be < ca->new_fs_bucket_idx
386 static noinline struct open_bucket *
387 bch2_bucket_alloc_early(struct btree_trans *trans,
389 enum alloc_reserve reserve,
390 struct bucket_alloc_state *s,
393 struct btree_iter iter;
395 struct open_bucket *ob = NULL;
396 u64 alloc_start = max_t(u64, ca->mi.first_bucket, ca->new_fs_bucket_idx);
397 u64 alloc_cursor = max(alloc_start, READ_ONCE(ca->alloc_cursor));
400 for_each_btree_key_norestart(trans, iter, BTREE_ID_alloc, POS(ca->dev_idx, alloc_cursor),
401 BTREE_ITER_SLOTS, k, ret) {
402 struct bch_alloc_v4 a_convert;
403 const struct bch_alloc_v4 *a;
405 if (bkey_ge(k.k->p, POS(ca->dev_idx, ca->mi.nbuckets)))
408 if (ca->new_fs_bucket_idx &&
409 is_superblock_bucket(ca, k.k->p.offset))
412 a = bch2_alloc_to_v4(k, &a_convert);
414 if (a->data_type != BCH_DATA_free)
419 ob = __try_alloc_bucket(trans->c, ca, k.k->p.offset, reserve, a, s, cl);
423 bch2_trans_iter_exit(trans, &iter);
425 ca->alloc_cursor = alloc_cursor;
430 if (!ob && alloc_cursor > alloc_start) {
431 alloc_cursor = alloc_start;
438 static struct open_bucket *bch2_bucket_alloc_freelist(struct btree_trans *trans,
440 enum alloc_reserve reserve,
441 struct bucket_alloc_state *s,
444 struct btree_iter iter;
446 struct open_bucket *ob = NULL;
447 u64 alloc_start = max_t(u64, ca->mi.first_bucket, READ_ONCE(ca->alloc_cursor));
448 u64 alloc_cursor = alloc_start;
451 BUG_ON(ca->new_fs_bucket_idx);
453 for_each_btree_key_norestart(trans, iter, BTREE_ID_freespace,
454 POS(ca->dev_idx, alloc_cursor), 0, k, ret) {
455 if (k.k->p.inode != ca->dev_idx)
458 for (alloc_cursor = max(alloc_cursor, bkey_start_offset(k.k));
459 alloc_cursor < k.k->p.offset;
461 ret = btree_trans_too_many_iters(trans);
469 ob = try_alloc_bucket(trans, ca, reserve,
470 alloc_cursor, s, k, cl);
472 iter.path->preserve = false;
480 bch2_trans_iter_exit(trans, &iter);
482 ca->alloc_cursor = alloc_cursor;
487 if (!ob && alloc_start > ca->mi.first_bucket) {
488 alloc_cursor = alloc_start = ca->mi.first_bucket;
496 * bch_bucket_alloc - allocate a single bucket from a specific device
498 * Returns index of bucket on success, 0 on failure
500 static struct open_bucket *bch2_bucket_alloc_trans(struct btree_trans *trans,
502 enum alloc_reserve reserve,
504 struct bch_dev_usage *usage)
506 struct bch_fs *c = trans->c;
507 struct open_bucket *ob = NULL;
508 bool freespace = READ_ONCE(ca->mi.freespace_initialized);
510 struct bucket_alloc_state s = { 0 };
511 bool waiting = false;
513 bch2_dev_usage_read_fast(ca, usage);
514 avail = dev_buckets_free(ca, *usage, reserve);
516 if (usage->d[BCH_DATA_need_discard].buckets > avail)
519 if (usage->d[BCH_DATA_need_gc_gens].buckets > avail)
522 if (should_invalidate_buckets(ca, *usage))
523 bch2_do_invalidates(c);
526 if (cl && !waiting) {
527 closure_wait(&c->freelist_wait, cl);
532 if (!c->blocked_allocate)
533 c->blocked_allocate = local_clock();
535 ob = ERR_PTR(-BCH_ERR_freelist_empty);
540 closure_wake_up(&c->freelist_wait);
542 ob = likely(freespace)
543 ? bch2_bucket_alloc_freelist(trans, ca, reserve, &s, cl)
544 : bch2_bucket_alloc_early(trans, ca, reserve, &s, cl);
546 if (s.skipped_need_journal_commit * 2 > avail)
547 bch2_journal_flush_async(&c->journal, NULL);
549 if (!ob && freespace && !test_bit(BCH_FS_CHECK_ALLOC_DONE, &c->flags)) {
555 ob = ERR_PTR(-BCH_ERR_no_buckets_found);
558 trace_and_count(c, bucket_alloc, ca,
559 bch2_alloc_reserves[reserve],
561 usage->d[BCH_DATA_free].buckets,
563 bch2_copygc_wait_amount(c),
564 c->copygc_wait - atomic64_read(&c->io_clock[WRITE].now),
568 else if (!bch2_err_matches(PTR_ERR(ob), BCH_ERR_transaction_restart))
569 trace_and_count(c, bucket_alloc_fail, ca,
570 bch2_alloc_reserves[reserve],
572 usage->d[BCH_DATA_free].buckets,
574 bch2_copygc_wait_amount(c),
575 c->copygc_wait - atomic64_read(&c->io_clock[WRITE].now),
578 bch2_err_str(PTR_ERR(ob)));
583 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
584 enum alloc_reserve reserve,
587 struct bch_dev_usage usage;
588 struct open_bucket *ob;
590 bch2_trans_do(c, NULL, NULL, 0,
591 PTR_ERR_OR_ZERO(ob = bch2_bucket_alloc_trans(&trans, ca, reserve,
596 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
597 unsigned l, unsigned r)
599 return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
600 (stripe->next_alloc[l] < stripe->next_alloc[r]));
603 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
605 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
606 struct dev_stripe_state *stripe,
607 struct bch_devs_mask *devs)
609 struct dev_alloc_list ret = { .nr = 0 };
612 for_each_set_bit(i, devs->d, BCH_SB_MEMBERS_MAX)
613 ret.devs[ret.nr++] = i;
615 bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
619 static inline void bch2_dev_stripe_increment_inlined(struct bch_dev *ca,
620 struct dev_stripe_state *stripe,
621 struct bch_dev_usage *usage)
623 u64 *v = stripe->next_alloc + ca->dev_idx;
624 u64 free_space = dev_buckets_available(ca, RESERVE_none);
625 u64 free_space_inv = free_space
626 ? div64_u64(1ULL << 48, free_space)
630 if (*v + free_space_inv >= *v)
631 *v += free_space_inv;
635 for (v = stripe->next_alloc;
636 v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
637 *v = *v < scale ? 0 : *v - scale;
640 void bch2_dev_stripe_increment(struct bch_dev *ca,
641 struct dev_stripe_state *stripe)
643 struct bch_dev_usage usage;
645 bch2_dev_usage_read_fast(ca, &usage);
646 bch2_dev_stripe_increment_inlined(ca, stripe, &usage);
649 static int add_new_bucket(struct bch_fs *c,
650 struct open_buckets *ptrs,
651 struct bch_devs_mask *devs_may_alloc,
652 unsigned nr_replicas,
653 unsigned *nr_effective,
656 struct open_bucket *ob)
658 unsigned durability =
659 bch_dev_bkey_exists(c, ob->dev)->mi.durability;
661 BUG_ON(*nr_effective >= nr_replicas);
662 BUG_ON(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS);
664 __clear_bit(ob->dev, devs_may_alloc->d);
665 *nr_effective += (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)
667 *have_cache |= !durability;
669 ob_push(c, ptrs, ob);
671 if (*nr_effective >= nr_replicas)
678 int bch2_bucket_alloc_set_trans(struct btree_trans *trans,
679 struct open_buckets *ptrs,
680 struct dev_stripe_state *stripe,
681 struct bch_devs_mask *devs_may_alloc,
682 unsigned nr_replicas,
683 unsigned *nr_effective,
686 enum bch_data_type data_type,
687 enum alloc_reserve reserve,
690 struct bch_fs *c = trans->c;
691 struct dev_alloc_list devs_sorted =
692 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
695 int ret = -BCH_ERR_insufficient_devices;
698 BUG_ON(*nr_effective >= nr_replicas);
700 for (i = 0; i < devs_sorted.nr; i++) {
701 struct bch_dev_usage usage;
702 struct open_bucket *ob;
704 dev = devs_sorted.devs[i];
707 ca = rcu_dereference(c->devs[dev]);
709 percpu_ref_get(&ca->ref);
715 if (!ca->mi.durability && *have_cache) {
716 percpu_ref_put(&ca->ref);
720 ob = bch2_bucket_alloc_trans(trans, ca, reserve, cl, &usage);
722 bch2_dev_stripe_increment_inlined(ca, stripe, &usage);
723 percpu_ref_put(&ca->ref);
727 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) || cl)
732 ob->data_type = data_type;
734 if (add_new_bucket(c, ptrs, devs_may_alloc,
735 nr_replicas, nr_effective,
736 have_cache, flags, ob)) {
745 /* Allocate from stripes: */
748 * if we can't allocate a new stripe because there are already too many
749 * partially filled stripes, force allocating from an existing stripe even when
750 * it's to a device we don't want:
753 static int bucket_alloc_from_stripe(struct btree_trans *trans,
754 struct open_buckets *ptrs,
755 struct write_point *wp,
756 struct bch_devs_mask *devs_may_alloc,
758 unsigned nr_replicas,
759 unsigned *nr_effective,
761 enum alloc_reserve reserve,
765 struct bch_fs *c = trans->c;
766 struct dev_alloc_list devs_sorted;
767 struct ec_stripe_head *h;
768 struct open_bucket *ob;
776 if (ec_open_bucket(c, ptrs))
779 h = bch2_ec_stripe_head_get(trans, target, 0, nr_replicas - 1, reserve, cl);
785 devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
787 for (i = 0; i < devs_sorted.nr; i++)
788 for (ec_idx = 0; ec_idx < h->s->nr_data; ec_idx++) {
789 if (!h->s->blocks[ec_idx])
792 ob = c->open_buckets + h->s->blocks[ec_idx];
793 if (ob->dev == devs_sorted.devs[i] &&
794 !test_and_set_bit(ec_idx, h->s->blocks_allocated))
799 ca = bch_dev_bkey_exists(c, ob->dev);
803 ec_stripe_new_get(h->s, STRIPE_REF_io);
805 ret = add_new_bucket(c, ptrs, devs_may_alloc,
806 nr_replicas, nr_effective,
807 have_cache, flags, ob);
809 bch2_ec_stripe_head_put(c, h);
813 /* Sector allocator */
815 static bool want_bucket(struct bch_fs *c,
816 struct write_point *wp,
817 struct bch_devs_mask *devs_may_alloc,
818 bool *have_cache, bool ec,
819 struct open_bucket *ob)
821 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
823 if (!test_bit(ob->dev, devs_may_alloc->d))
826 if (ob->data_type != wp->data_type)
829 if (!ca->mi.durability &&
830 (wp->data_type == BCH_DATA_btree || ec || *have_cache))
833 if (ec != (ob->ec != NULL))
839 static int bucket_alloc_set_writepoint(struct bch_fs *c,
840 struct open_buckets *ptrs,
841 struct write_point *wp,
842 struct bch_devs_mask *devs_may_alloc,
843 unsigned nr_replicas,
844 unsigned *nr_effective,
846 bool ec, unsigned flags)
848 struct open_buckets ptrs_skip = { .nr = 0 };
849 struct open_bucket *ob;
853 open_bucket_for_each(c, &wp->ptrs, ob, i) {
854 if (!ret && want_bucket(c, wp, devs_may_alloc,
856 ret = add_new_bucket(c, ptrs, devs_may_alloc,
857 nr_replicas, nr_effective,
858 have_cache, flags, ob);
860 ob_push(c, &ptrs_skip, ob);
862 wp->ptrs = ptrs_skip;
867 static int bucket_alloc_set_partial(struct bch_fs *c,
868 struct open_buckets *ptrs,
869 struct write_point *wp,
870 struct bch_devs_mask *devs_may_alloc,
871 unsigned nr_replicas,
872 unsigned *nr_effective,
873 bool *have_cache, bool ec,
874 enum alloc_reserve reserve,
879 if (!c->open_buckets_partial_nr)
882 spin_lock(&c->freelist_lock);
884 if (!c->open_buckets_partial_nr)
887 for (i = c->open_buckets_partial_nr - 1; i >= 0; --i) {
888 struct open_bucket *ob = c->open_buckets + c->open_buckets_partial[i];
890 if (want_bucket(c, wp, devs_may_alloc, have_cache, ec, ob)) {
891 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
892 struct bch_dev_usage usage;
895 bch2_dev_usage_read_fast(ca, &usage);
896 avail = dev_buckets_free(ca, usage, reserve);
900 array_remove_item(c->open_buckets_partial,
901 c->open_buckets_partial_nr,
903 ob->on_partial_list = false;
905 ret = add_new_bucket(c, ptrs, devs_may_alloc,
906 nr_replicas, nr_effective,
907 have_cache, flags, ob);
913 spin_unlock(&c->freelist_lock);
917 static int __open_bucket_add_buckets(struct btree_trans *trans,
918 struct open_buckets *ptrs,
919 struct write_point *wp,
920 struct bch_devs_list *devs_have,
923 unsigned nr_replicas,
924 unsigned *nr_effective,
926 enum alloc_reserve reserve,
930 struct bch_fs *c = trans->c;
931 struct bch_devs_mask devs;
932 struct open_bucket *ob;
933 struct closure *cl = NULL;
938 devs = target_rw_devs(c, wp->data_type, target);
941 /* Don't allocate from devices we already have pointers to: */
942 for (i = 0; i < devs_have->nr; i++)
943 __clear_bit(devs_have->devs[i], devs.d);
945 open_bucket_for_each(c, ptrs, ob, i)
946 __clear_bit(ob->dev, devs.d);
948 if (erasure_code && ec_open_bucket(c, ptrs))
951 ret = bucket_alloc_set_writepoint(c, ptrs, wp, &devs,
952 nr_replicas, nr_effective,
953 have_cache, erasure_code, flags);
957 ret = bucket_alloc_set_partial(c, ptrs, wp, &devs,
958 nr_replicas, nr_effective,
959 have_cache, erasure_code, reserve, flags);
964 ret = bucket_alloc_from_stripe(trans, ptrs, wp, &devs,
966 nr_replicas, nr_effective,
968 reserve, flags, _cl);
972 * Try nonblocking first, so that if one device is full we'll try from
975 ret = bch2_bucket_alloc_set_trans(trans, ptrs, &wp->stripe, &devs,
976 nr_replicas, nr_effective, have_cache,
977 flags, wp->data_type, reserve, cl);
979 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
980 !bch2_err_matches(ret, BCH_ERR_insufficient_devices) &&
991 static int open_bucket_add_buckets(struct btree_trans *trans,
992 struct open_buckets *ptrs,
993 struct write_point *wp,
994 struct bch_devs_list *devs_have,
996 unsigned erasure_code,
997 unsigned nr_replicas,
998 unsigned *nr_effective,
1000 enum alloc_reserve reserve,
1007 ret = __open_bucket_add_buckets(trans, ptrs, wp,
1008 devs_have, target, erasure_code,
1009 nr_replicas, nr_effective, have_cache,
1010 reserve, flags, cl);
1011 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
1012 bch2_err_matches(ret, BCH_ERR_operation_blocked) ||
1013 bch2_err_matches(ret, BCH_ERR_freelist_empty) ||
1014 bch2_err_matches(ret, BCH_ERR_open_buckets_empty))
1016 if (*nr_effective >= nr_replicas)
1020 ret = __open_bucket_add_buckets(trans, ptrs, wp,
1021 devs_have, target, false,
1022 nr_replicas, nr_effective, have_cache,
1023 reserve, flags, cl);
1024 return ret < 0 ? ret : 0;
1027 static bool should_drop_bucket(struct open_bucket *ob, struct bch_fs *c,
1028 struct bch_dev *ca, bool ec)
1031 return ob->ec != NULL;
1033 bool drop = ob->dev == ca->dev_idx;
1034 struct open_bucket *ob2;
1037 if (!drop && ob->ec) {
1038 mutex_lock(&ob->ec->lock);
1039 for (i = 0; i < ob->ec->new_stripe.key.v.nr_blocks; i++) {
1040 if (!ob->ec->blocks[i])
1043 ob2 = c->open_buckets + ob->ec->blocks[i];
1044 drop |= ob2->dev == ca->dev_idx;
1046 mutex_unlock(&ob->ec->lock);
1055 static void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
1056 bool ec, struct write_point *wp)
1058 struct open_buckets ptrs = { .nr = 0 };
1059 struct open_bucket *ob;
1062 mutex_lock(&wp->lock);
1063 open_bucket_for_each(c, &wp->ptrs, ob, i)
1064 if (should_drop_bucket(ob, c, ca, ec))
1065 bch2_open_bucket_put(c, ob);
1067 ob_push(c, &ptrs, ob);
1069 mutex_unlock(&wp->lock);
1072 void bch2_open_buckets_stop(struct bch_fs *c, struct bch_dev *ca,
1077 /* Next, close write points that point to this device... */
1078 for (i = 0; i < ARRAY_SIZE(c->write_points); i++)
1079 bch2_writepoint_stop(c, ca, ec, &c->write_points[i]);
1081 bch2_writepoint_stop(c, ca, ec, &c->copygc_write_point);
1082 bch2_writepoint_stop(c, ca, ec, &c->rebalance_write_point);
1083 bch2_writepoint_stop(c, ca, ec, &c->btree_write_point);
1085 mutex_lock(&c->btree_reserve_cache_lock);
1086 while (c->btree_reserve_cache_nr) {
1087 struct btree_alloc *a =
1088 &c->btree_reserve_cache[--c->btree_reserve_cache_nr];
1090 bch2_open_buckets_put(c, &a->ob);
1092 mutex_unlock(&c->btree_reserve_cache_lock);
1094 spin_lock(&c->freelist_lock);
1096 while (i < c->open_buckets_partial_nr) {
1097 struct open_bucket *ob =
1098 c->open_buckets + c->open_buckets_partial[i];
1100 if (should_drop_bucket(ob, c, ca, ec)) {
1101 --c->open_buckets_partial_nr;
1102 swap(c->open_buckets_partial[i],
1103 c->open_buckets_partial[c->open_buckets_partial_nr]);
1104 ob->on_partial_list = false;
1105 spin_unlock(&c->freelist_lock);
1106 bch2_open_bucket_put(c, ob);
1107 spin_lock(&c->freelist_lock);
1112 spin_unlock(&c->freelist_lock);
1114 bch2_ec_stop_dev(c, ca);
1117 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
1118 unsigned long write_point)
1121 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
1123 return &c->write_points_hash[hash];
1126 static struct write_point *__writepoint_find(struct hlist_head *head,
1127 unsigned long write_point)
1129 struct write_point *wp;
1132 hlist_for_each_entry_rcu(wp, head, node)
1133 if (wp->write_point == write_point)
1141 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
1143 u64 stranded = c->write_points_nr * c->bucket_size_max;
1144 u64 free = bch2_fs_usage_read_short(c).free;
1146 return stranded * factor > free;
1149 static bool try_increase_writepoints(struct bch_fs *c)
1151 struct write_point *wp;
1153 if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
1154 too_many_writepoints(c, 32))
1157 wp = c->write_points + c->write_points_nr++;
1158 hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
1162 static bool try_decrease_writepoints(struct bch_fs *c, unsigned old_nr)
1164 struct write_point *wp;
1166 mutex_lock(&c->write_points_hash_lock);
1167 if (c->write_points_nr < old_nr) {
1168 mutex_unlock(&c->write_points_hash_lock);
1172 if (c->write_points_nr == 1 ||
1173 !too_many_writepoints(c, 8)) {
1174 mutex_unlock(&c->write_points_hash_lock);
1178 wp = c->write_points + --c->write_points_nr;
1180 hlist_del_rcu(&wp->node);
1181 mutex_unlock(&c->write_points_hash_lock);
1183 bch2_writepoint_stop(c, NULL, false, wp);
1187 static void bch2_trans_mutex_lock_norelock(struct btree_trans *trans,
1190 if (!mutex_trylock(lock)) {
1191 bch2_trans_unlock(trans);
1196 static struct write_point *writepoint_find(struct btree_trans *trans,
1197 unsigned long write_point)
1199 struct bch_fs *c = trans->c;
1200 struct write_point *wp, *oldest;
1201 struct hlist_head *head;
1203 if (!(write_point & 1UL)) {
1204 wp = (struct write_point *) write_point;
1205 bch2_trans_mutex_lock_norelock(trans, &wp->lock);
1209 head = writepoint_hash(c, write_point);
1211 wp = __writepoint_find(head, write_point);
1214 bch2_trans_mutex_lock_norelock(trans, &wp->lock);
1215 if (wp->write_point == write_point)
1217 mutex_unlock(&wp->lock);
1220 restart_find_oldest:
1222 for (wp = c->write_points;
1223 wp < c->write_points + c->write_points_nr; wp++)
1224 if (!oldest || time_before64(wp->last_used, oldest->last_used))
1227 bch2_trans_mutex_lock_norelock(trans, &oldest->lock);
1228 bch2_trans_mutex_lock_norelock(trans, &c->write_points_hash_lock);
1229 if (oldest >= c->write_points + c->write_points_nr ||
1230 try_increase_writepoints(c)) {
1231 mutex_unlock(&c->write_points_hash_lock);
1232 mutex_unlock(&oldest->lock);
1233 goto restart_find_oldest;
1236 wp = __writepoint_find(head, write_point);
1237 if (wp && wp != oldest) {
1238 mutex_unlock(&c->write_points_hash_lock);
1239 mutex_unlock(&oldest->lock);
1244 hlist_del_rcu(&wp->node);
1245 wp->write_point = write_point;
1246 hlist_add_head_rcu(&wp->node, head);
1247 mutex_unlock(&c->write_points_hash_lock);
1249 wp->last_used = local_clock();
1254 * Get us an open_bucket we can allocate from, return with it locked:
1256 int bch2_alloc_sectors_start_trans(struct btree_trans *trans,
1258 unsigned erasure_code,
1259 struct write_point_specifier write_point,
1260 struct bch_devs_list *devs_have,
1261 unsigned nr_replicas,
1262 unsigned nr_replicas_required,
1263 enum alloc_reserve reserve,
1266 struct write_point **wp_ret)
1268 struct bch_fs *c = trans->c;
1269 struct write_point *wp;
1270 struct open_bucket *ob;
1271 struct open_buckets ptrs;
1272 unsigned nr_effective, write_points_nr;
1277 BUG_ON(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS);
1279 BUG_ON(!nr_replicas || !nr_replicas_required);
1283 write_points_nr = c->write_points_nr;
1286 *wp_ret = wp = writepoint_find(trans, write_point.v);
1288 /* metadata may not allocate on cache devices: */
1289 if (wp->data_type != BCH_DATA_user)
1292 if (target && !(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
1293 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1294 target, erasure_code,
1295 nr_replicas, &nr_effective,
1296 &have_cache, reserve,
1299 bch2_err_matches(ret, BCH_ERR_transaction_restart))
1302 /* Don't retry from all devices if we're out of open buckets: */
1303 if (bch2_err_matches(ret, BCH_ERR_open_buckets_empty))
1304 goto allocate_blocking;
1307 * Only try to allocate cache (durability = 0 devices) from the
1312 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1314 nr_replicas, &nr_effective,
1315 &have_cache, reserve,
1319 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1320 target, erasure_code,
1321 nr_replicas, &nr_effective,
1322 &have_cache, reserve,
1326 BUG_ON(!ret && nr_effective < nr_replicas);
1328 if (erasure_code && !ec_open_bucket(c, &ptrs))
1329 pr_debug("failed to get ec bucket: ret %u", ret);
1331 if (ret == -BCH_ERR_insufficient_devices &&
1332 nr_effective >= nr_replicas_required)
1338 /* Free buckets we didn't use: */
1339 open_bucket_for_each(c, &wp->ptrs, ob, i)
1340 open_bucket_free_unused(c, wp, ob);
1344 wp->sectors_free = UINT_MAX;
1346 open_bucket_for_each(c, &wp->ptrs, ob, i)
1347 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
1349 BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
1353 open_bucket_for_each(c, &wp->ptrs, ob, i)
1354 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
1355 ob_push(c, &ptrs, ob);
1357 open_bucket_free_unused(c, wp, ob);
1360 mutex_unlock(&wp->lock);
1362 if (bch2_err_matches(ret, BCH_ERR_freelist_empty) &&
1363 try_decrease_writepoints(c, write_points_nr))
1366 if (bch2_err_matches(ret, BCH_ERR_open_buckets_empty) ||
1367 bch2_err_matches(ret, BCH_ERR_freelist_empty))
1369 ? -BCH_ERR_bucket_alloc_blocked
1370 : -BCH_ERR_ENOSPC_bucket_alloc;
1375 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *c, struct open_bucket *ob)
1377 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1379 return (struct bch_extent_ptr) {
1380 .type = 1 << BCH_EXTENT_ENTRY_ptr,
1383 .offset = bucket_to_sector(ca, ob->bucket) +
1384 ca->mi.bucket_size -
1389 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
1390 struct bkey_i *k, unsigned sectors,
1393 bch2_alloc_sectors_append_ptrs_inlined(c, wp, k, sectors, cached);
1397 * Append pointers to the space we just allocated to @k, and mark @sectors space
1398 * as allocated out of @ob
1400 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
1402 bch2_alloc_sectors_done_inlined(c, wp);
1405 static inline void writepoint_init(struct write_point *wp,
1406 enum bch_data_type type)
1408 mutex_init(&wp->lock);
1409 wp->data_type = type;
1411 INIT_WORK(&wp->index_update_work, bch2_write_point_do_index_updates);
1412 INIT_LIST_HEAD(&wp->writes);
1413 spin_lock_init(&wp->writes_lock);
1416 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
1418 struct open_bucket *ob;
1419 struct write_point *wp;
1421 mutex_init(&c->write_points_hash_lock);
1422 c->write_points_nr = ARRAY_SIZE(c->write_points);
1424 /* open bucket 0 is a sentinal NULL: */
1425 spin_lock_init(&c->open_buckets[0].lock);
1427 for (ob = c->open_buckets + 1;
1428 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
1429 spin_lock_init(&ob->lock);
1430 c->open_buckets_nr_free++;
1432 ob->freelist = c->open_buckets_freelist;
1433 c->open_buckets_freelist = ob - c->open_buckets;
1436 writepoint_init(&c->btree_write_point, BCH_DATA_btree);
1437 writepoint_init(&c->rebalance_write_point, BCH_DATA_user);
1438 writepoint_init(&c->copygc_write_point, BCH_DATA_user);
1440 for (wp = c->write_points;
1441 wp < c->write_points + c->write_points_nr; wp++) {
1442 writepoint_init(wp, BCH_DATA_user);
1444 wp->last_used = local_clock();
1445 wp->write_point = (unsigned long) wp;
1446 hlist_add_head_rcu(&wp->node,
1447 writepoint_hash(c, wp->write_point));
1451 static void bch2_open_bucket_to_text(struct printbuf *out, struct bch_fs *c, struct open_bucket *ob)
1453 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1454 unsigned data_type = ob->data_type;
1455 barrier(); /* READ_ONCE() doesn't work on bitfields */
1457 prt_printf(out, "%zu ref %u %s %u:%llu gen %u allocated %u/%u",
1458 ob - c->open_buckets,
1459 atomic_read(&ob->pin),
1460 data_type < BCH_DATA_NR ? bch2_data_types[data_type] : "invalid data type",
1461 ob->dev, ob->bucket, ob->gen,
1462 ca->mi.bucket_size - ob->sectors_free, ca->mi.bucket_size);
1464 prt_printf(out, " ec idx %llu", ob->ec->idx);
1465 if (ob->on_partial_list)
1466 prt_str(out, " partial");
1470 void bch2_open_buckets_to_text(struct printbuf *out, struct bch_fs *c)
1472 struct open_bucket *ob;
1476 for (ob = c->open_buckets;
1477 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1479 spin_lock(&ob->lock);
1480 if (ob->valid && !ob->on_partial_list)
1481 bch2_open_bucket_to_text(out, c, ob);
1482 spin_unlock(&ob->lock);
1488 void bch2_open_buckets_partial_to_text(struct printbuf *out, struct bch_fs *c)
1493 spin_lock(&c->freelist_lock);
1495 for (i = 0; i < c->open_buckets_partial_nr; i++)
1496 bch2_open_bucket_to_text(out, c,
1497 c->open_buckets + c->open_buckets_partial[i]);
1499 spin_unlock(&c->freelist_lock);
1503 static const char * const bch2_write_point_states[] = {
1505 WRITE_POINT_STATES()
1510 void bch2_write_points_to_text(struct printbuf *out, struct bch_fs *c)
1512 struct write_point *wp;
1515 for (wp = c->write_points;
1516 wp < c->write_points + ARRAY_SIZE(c->write_points);
1518 prt_printf(out, "%lu: ", wp->write_point);
1519 prt_human_readable_u64(out, wp->sectors_allocated);
1521 prt_printf(out, " last wrote: ");
1522 bch2_pr_time_units(out, sched_clock() - wp->last_used);
1524 for (i = 0; i < WRITE_POINT_STATE_NR; i++) {
1525 prt_printf(out, " %s: ", bch2_write_point_states[i]);
1526 bch2_pr_time_units(out, wp->time[i]);