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 "btree_iter.h"
18 #include "btree_update.h"
21 #include "buckets_waiting_for_journal.h"
24 #include "disk_groups.h"
31 #include <linux/math64.h>
32 #include <linux/rculist.h>
33 #include <linux/rcupdate.h>
34 #include <trace/events/bcachefs.h>
36 const char * const bch2_alloc_reserves[] = {
44 * Open buckets represent a bucket that's currently being allocated from. They
47 * - They track buckets that have been partially allocated, allowing for
48 * sub-bucket sized allocations - they're used by the sector allocator below
50 * - They provide a reference to the buckets they own that mark and sweep GC
51 * can find, until the new allocation has a pointer to it inserted into the
54 * When allocating some space with the sector allocator, the allocation comes
55 * with a reference to an open bucket - the caller is required to put that
56 * reference _after_ doing the index update that makes its allocation reachable.
59 static void bch2_open_bucket_hash_add(struct bch_fs *c, struct open_bucket *ob)
61 open_bucket_idx_t idx = ob - c->open_buckets;
62 open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
68 static void bch2_open_bucket_hash_remove(struct bch_fs *c, struct open_bucket *ob)
70 open_bucket_idx_t idx = ob - c->open_buckets;
71 open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
73 while (*slot != idx) {
75 slot = &c->open_buckets[*slot].hash;
82 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
84 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
87 bch2_ec_bucket_written(c, ob);
91 percpu_down_read(&c->mark_lock);
97 spin_unlock(&ob->lock);
98 percpu_up_read(&c->mark_lock);
100 spin_lock(&c->freelist_lock);
101 bch2_open_bucket_hash_remove(c, ob);
103 ob->freelist = c->open_buckets_freelist;
104 c->open_buckets_freelist = ob - c->open_buckets;
106 c->open_buckets_nr_free++;
107 ca->nr_open_buckets--;
108 spin_unlock(&c->freelist_lock);
110 closure_wake_up(&c->open_buckets_wait);
113 void bch2_open_bucket_write_error(struct bch_fs *c,
114 struct open_buckets *obs,
117 struct open_bucket *ob;
120 open_bucket_for_each(c, obs, ob, i)
121 if (ob->dev == dev && ob->ec)
122 bch2_ec_bucket_cancel(c, ob);
125 static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
127 struct open_bucket *ob;
129 BUG_ON(!c->open_buckets_freelist || !c->open_buckets_nr_free);
131 ob = c->open_buckets + c->open_buckets_freelist;
132 c->open_buckets_freelist = ob->freelist;
133 atomic_set(&ob->pin, 1);
136 c->open_buckets_nr_free--;
140 static void open_bucket_free_unused(struct bch_fs *c,
141 struct write_point *wp,
142 struct open_bucket *ob)
144 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
145 bool may_realloc = wp->data_type == BCH_DATA_user;
147 BUG_ON(ca->open_buckets_partial_nr >
148 ARRAY_SIZE(ca->open_buckets_partial));
150 if (ca->open_buckets_partial_nr <
151 ARRAY_SIZE(ca->open_buckets_partial) &&
153 spin_lock(&c->freelist_lock);
154 ob->on_partial_list = true;
155 ca->open_buckets_partial[ca->open_buckets_partial_nr++] =
156 ob - c->open_buckets;
157 spin_unlock(&c->freelist_lock);
159 closure_wake_up(&c->open_buckets_wait);
160 closure_wake_up(&c->freelist_wait);
162 bch2_open_bucket_put(c, ob);
166 /* _only_ for allocating the journal on a new device: */
167 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
169 while (ca->new_fs_bucket_idx < ca->mi.nbuckets) {
170 u64 b = ca->new_fs_bucket_idx++;
172 if (!is_superblock_bucket(ca, b) &&
173 (!ca->buckets_nouse || !test_bit(b, ca->buckets_nouse)))
180 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
184 case RESERVE_btree_movinggc:
186 case RESERVE_movinggc:
187 return OPEN_BUCKETS_COUNT / 4;
189 return OPEN_BUCKETS_COUNT / 2;
193 static struct open_bucket *__try_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
195 enum alloc_reserve reserve,
196 struct bch_alloc_v4 *a,
198 u64 *skipped_need_journal_commit,
202 struct open_bucket *ob;
204 if (unlikely(ca->buckets_nouse && test_bit(bucket, ca->buckets_nouse))) {
209 if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
214 if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
215 c->journal.flushed_seq_ondisk, ca->dev_idx, bucket)) {
216 (*skipped_need_journal_commit)++;
220 spin_lock(&c->freelist_lock);
222 if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
224 closure_wait(&c->open_buckets_wait, cl);
226 if (!c->blocked_allocate_open_bucket)
227 c->blocked_allocate_open_bucket = local_clock();
229 spin_unlock(&c->freelist_lock);
230 return ERR_PTR(-BCH_ERR_open_buckets_empty);
233 /* Recheck under lock: */
234 if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
235 spin_unlock(&c->freelist_lock);
240 ob = bch2_open_bucket_alloc(c);
242 spin_lock(&ob->lock);
245 ob->sectors_free = ca->mi.bucket_size;
246 ob->alloc_reserve = reserve;
247 ob->dev = ca->dev_idx;
250 spin_unlock(&ob->lock);
252 ca->nr_open_buckets++;
253 bch2_open_bucket_hash_add(c, ob);
255 if (c->blocked_allocate_open_bucket) {
256 bch2_time_stats_update(
257 &c->times[BCH_TIME_blocked_allocate_open_bucket],
258 c->blocked_allocate_open_bucket);
259 c->blocked_allocate_open_bucket = 0;
262 if (c->blocked_allocate) {
263 bch2_time_stats_update(
264 &c->times[BCH_TIME_blocked_allocate],
265 c->blocked_allocate);
266 c->blocked_allocate = 0;
269 spin_unlock(&c->freelist_lock);
274 static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bch_dev *ca,
275 enum alloc_reserve reserve, u64 free_entry,
277 u64 *skipped_need_journal_commit,
279 struct bkey_s_c freespace_k,
282 struct bch_fs *c = trans->c;
283 struct btree_iter iter = { NULL };
285 struct open_bucket *ob;
286 struct bch_alloc_v4 a;
287 u64 b = free_entry & ~(~0ULL << 56);
288 unsigned genbits = free_entry >> 56;
289 struct printbuf buf = PRINTBUF;
292 if (b < ca->mi.first_bucket || b >= ca->mi.nbuckets) {
293 prt_printf(&buf, "freespace btree has bucket outside allowed range %u-%llu\n"
295 ca->mi.first_bucket, ca->mi.nbuckets);
296 bch2_bkey_val_to_text(&buf, c, freespace_k);
297 bch2_trans_inconsistent(trans, "%s", buf.buf);
302 bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(ca->dev_idx, b), BTREE_ITER_CACHED);
303 k = bch2_btree_iter_peek_slot(&iter);
310 bch2_alloc_to_v4(k, &a);
312 if (genbits != (alloc_freespace_genbits(a) >> 56)) {
313 prt_printf(&buf, "bucket in freespace btree with wrong genbits (got %u should be %llu)\n"
315 genbits, alloc_freespace_genbits(a) >> 56);
316 bch2_bkey_val_to_text(&buf, c, freespace_k);
317 prt_printf(&buf, "\n ");
318 bch2_bkey_val_to_text(&buf, c, k);
319 bch2_trans_inconsistent(trans, "%s", buf.buf);
325 if (a.data_type != BCH_DATA_free) {
326 prt_printf(&buf, "non free bucket in freespace btree\n"
328 bch2_bkey_val_to_text(&buf, c, freespace_k);
329 prt_printf(&buf, "\n ");
330 bch2_bkey_val_to_text(&buf, c, k);
331 bch2_trans_inconsistent(trans, "%s", buf.buf);
336 ob = __try_alloc_bucket(c, ca, b, reserve, &a,
338 skipped_need_journal_commit,
342 iter.path->preserve = false;
344 set_btree_iter_dontneed(&iter);
345 bch2_trans_iter_exit(trans, &iter);
350 static struct open_bucket *try_alloc_partial_bucket(struct bch_fs *c, struct bch_dev *ca,
351 enum alloc_reserve reserve)
353 struct open_bucket *ob;
356 spin_lock(&c->freelist_lock);
358 for (i = ca->open_buckets_partial_nr - 1; i >= 0; --i) {
359 ob = c->open_buckets + ca->open_buckets_partial[i];
361 if (reserve <= ob->alloc_reserve) {
362 array_remove_item(ca->open_buckets_partial,
363 ca->open_buckets_partial_nr,
365 ob->on_partial_list = false;
366 ob->alloc_reserve = reserve;
367 spin_unlock(&c->freelist_lock);
372 spin_unlock(&c->freelist_lock);
377 * This path is for before the freespace btree is initialized:
379 * If ca->new_fs_bucket_idx is nonzero, we haven't yet marked superblock &
380 * journal buckets - journal buckets will be < ca->new_fs_bucket_idx
382 static noinline struct open_bucket *
383 bch2_bucket_alloc_early(struct btree_trans *trans,
385 enum alloc_reserve reserve,
389 u64 *skipped_need_journal_commit,
393 struct btree_iter iter;
395 struct open_bucket *ob = NULL;
398 *cur_bucket = max_t(u64, *cur_bucket, ca->mi.first_bucket);
399 *cur_bucket = max_t(u64, *cur_bucket, ca->new_fs_bucket_idx);
401 for_each_btree_key_norestart(trans, iter, BTREE_ID_alloc, POS(ca->dev_idx, *cur_bucket),
402 BTREE_ITER_SLOTS, k, ret) {
403 struct bch_alloc_v4 a;
405 if (bkey_cmp(k.k->p, POS(ca->dev_idx, ca->mi.nbuckets)) >= 0)
408 if (ca->new_fs_bucket_idx &&
409 is_superblock_bucket(ca, k.k->p.offset))
412 bch2_alloc_to_v4(k, &a);
414 if (a.data_type != BCH_DATA_free)
419 ob = __try_alloc_bucket(trans->c, ca, k.k->p.offset, reserve, &a,
421 skipped_need_journal_commit,
427 bch2_trans_iter_exit(trans, &iter);
429 *cur_bucket = iter.pos.offset;
431 return ob ?: ERR_PTR(ret ?: -BCH_ERR_no_buckets_found);
434 static struct open_bucket *bch2_bucket_alloc_freelist(struct btree_trans *trans,
436 enum alloc_reserve reserve,
440 u64 *skipped_need_journal_commit,
444 struct btree_iter iter;
446 struct open_bucket *ob = NULL;
449 BUG_ON(ca->new_fs_bucket_idx);
453 * On transaction restart, we'd like to restart from the bucket we were
456 for_each_btree_key_norestart(trans, iter, BTREE_ID_freespace,
457 POS(ca->dev_idx, *cur_bucket), 0, k, ret) {
458 if (k.k->p.inode != ca->dev_idx)
461 for (*cur_bucket = max(*cur_bucket, bkey_start_offset(k.k));
462 *cur_bucket < k.k->p.offset;
464 ret = btree_trans_too_many_iters(trans);
470 ob = try_alloc_bucket(trans, ca, reserve,
473 skipped_need_journal_commit,
483 bch2_trans_iter_exit(trans, &iter);
485 return ob ?: ERR_PTR(ret);
489 * bch_bucket_alloc - allocate a single bucket from a specific device
491 * Returns index of bucket on success, 0 on failure
493 static struct open_bucket *bch2_bucket_alloc_trans(struct btree_trans *trans,
495 enum alloc_reserve reserve,
496 bool may_alloc_partial,
499 struct bch_fs *c = trans->c;
500 struct open_bucket *ob = NULL;
501 struct bch_dev_usage usage;
502 bool freespace_initialized = READ_ONCE(ca->mi.freespace_initialized);
503 u64 start = freespace_initialized ? 0 : ca->bucket_alloc_trans_early_cursor;
505 u64 cur_bucket = start;
506 u64 buckets_seen = 0;
507 u64 skipped_open = 0;
508 u64 skipped_need_journal_commit = 0;
509 u64 skipped_nouse = 0;
510 bool waiting = false;
512 usage = bch2_dev_usage_read(ca);
513 avail = dev_buckets_free(ca, usage, reserve);
515 if (usage.d[BCH_DATA_need_discard].buckets > avail)
518 if (usage.d[BCH_DATA_need_gc_gens].buckets > avail)
521 if (should_invalidate_buckets(ca, usage))
522 bch2_do_invalidates(c);
525 if (cl && !waiting) {
526 closure_wait(&c->freelist_wait, cl);
531 if (!c->blocked_allocate)
532 c->blocked_allocate = local_clock();
534 ob = ERR_PTR(-BCH_ERR_freelist_empty);
539 closure_wake_up(&c->freelist_wait);
541 if (may_alloc_partial) {
542 ob = try_alloc_partial_bucket(c, ca, reserve);
547 ob = likely(ca->mi.freespace_initialized)
548 ? bch2_bucket_alloc_freelist(trans, ca, reserve,
552 &skipped_need_journal_commit,
555 : bch2_bucket_alloc_early(trans, ca, reserve,
559 &skipped_need_journal_commit,
563 if (skipped_need_journal_commit * 2 > avail)
564 bch2_journal_flush_async(&c->journal, NULL);
566 if (!ob && !freespace_initialized && start) {
567 start = cur_bucket = 0;
571 if (!freespace_initialized)
572 ca->bucket_alloc_trans_early_cursor = cur_bucket;
575 ob = ERR_PTR(-BCH_ERR_no_buckets_found);
578 trace_and_count(c, bucket_alloc, ca, bch2_alloc_reserves[reserve],
579 may_alloc_partial, ob->bucket);
581 trace_and_count(c, bucket_alloc_fail,
582 ca, bch2_alloc_reserves[reserve],
583 usage.d[BCH_DATA_free].buckets,
585 bch2_copygc_wait_amount(c),
586 c->copygc_wait - atomic64_read(&c->io_clock[WRITE].now),
589 skipped_need_journal_commit,
592 bch2_err_str(PTR_ERR(ob)));
597 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
598 enum alloc_reserve reserve,
599 bool may_alloc_partial,
602 struct open_bucket *ob;
604 bch2_trans_do(c, NULL, NULL, 0,
605 PTR_ERR_OR_ZERO(ob = bch2_bucket_alloc_trans(&trans, ca, reserve,
606 may_alloc_partial, cl)));
610 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
611 unsigned l, unsigned r)
613 return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
614 (stripe->next_alloc[l] < stripe->next_alloc[r]));
617 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
619 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
620 struct dev_stripe_state *stripe,
621 struct bch_devs_mask *devs)
623 struct dev_alloc_list ret = { .nr = 0 };
626 for_each_set_bit(i, devs->d, BCH_SB_MEMBERS_MAX)
627 ret.devs[ret.nr++] = i;
629 bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
633 void bch2_dev_stripe_increment(struct bch_dev *ca,
634 struct dev_stripe_state *stripe)
636 u64 *v = stripe->next_alloc + ca->dev_idx;
637 u64 free_space = dev_buckets_available(ca, RESERVE_none);
638 u64 free_space_inv = free_space
639 ? div64_u64(1ULL << 48, free_space)
643 if (*v + free_space_inv >= *v)
644 *v += free_space_inv;
648 for (v = stripe->next_alloc;
649 v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
650 *v = *v < scale ? 0 : *v - scale;
653 #define BUCKET_MAY_ALLOC_PARTIAL (1 << 0)
654 #define BUCKET_ALLOC_USE_DURABILITY (1 << 1)
656 static void add_new_bucket(struct bch_fs *c,
657 struct open_buckets *ptrs,
658 struct bch_devs_mask *devs_may_alloc,
659 unsigned *nr_effective,
662 struct open_bucket *ob)
664 unsigned durability =
665 bch_dev_bkey_exists(c, ob->dev)->mi.durability;
667 __clear_bit(ob->dev, devs_may_alloc->d);
668 *nr_effective += (flags & BUCKET_ALLOC_USE_DURABILITY)
670 *have_cache |= !durability;
672 ob_push(c, ptrs, ob);
675 static int bch2_bucket_alloc_set_trans(struct btree_trans *trans,
676 struct open_buckets *ptrs,
677 struct dev_stripe_state *stripe,
678 struct bch_devs_mask *devs_may_alloc,
679 unsigned nr_replicas,
680 unsigned *nr_effective,
682 enum alloc_reserve reserve,
686 struct bch_fs *c = trans->c;
687 struct dev_alloc_list devs_sorted =
688 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
691 int ret = -BCH_ERR_insufficient_devices;
694 BUG_ON(*nr_effective >= nr_replicas);
696 for (i = 0; i < devs_sorted.nr; i++) {
697 struct open_bucket *ob;
699 dev = devs_sorted.devs[i];
702 ca = rcu_dereference(c->devs[dev]);
704 percpu_ref_get(&ca->ref);
710 if (!ca->mi.durability && *have_cache) {
711 percpu_ref_put(&ca->ref);
715 ob = bch2_bucket_alloc_trans(trans, ca, reserve,
716 flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
718 bch2_dev_stripe_increment(ca, stripe);
719 percpu_ref_put(&ca->ref);
723 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) || cl)
728 add_new_bucket(c, ptrs, devs_may_alloc,
729 nr_effective, have_cache, flags, ob);
731 if (*nr_effective >= nr_replicas) {
740 int bch2_bucket_alloc_set(struct bch_fs *c,
741 struct open_buckets *ptrs,
742 struct dev_stripe_state *stripe,
743 struct bch_devs_mask *devs_may_alloc,
744 unsigned nr_replicas,
745 unsigned *nr_effective,
747 enum alloc_reserve reserve,
751 return bch2_trans_do(c, NULL, NULL, 0,
752 bch2_bucket_alloc_set_trans(&trans, ptrs, stripe,
753 devs_may_alloc, nr_replicas,
754 nr_effective, have_cache, reserve,
758 /* Allocate from stripes: */
761 * if we can't allocate a new stripe because there are already too many
762 * partially filled stripes, force allocating from an existing stripe even when
763 * it's to a device we don't want:
766 static int bucket_alloc_from_stripe(struct bch_fs *c,
767 struct open_buckets *ptrs,
768 struct write_point *wp,
769 struct bch_devs_mask *devs_may_alloc,
771 unsigned erasure_code,
772 unsigned nr_replicas,
773 unsigned *nr_effective,
778 struct dev_alloc_list devs_sorted;
779 struct ec_stripe_head *h;
780 struct open_bucket *ob;
790 if (ec_open_bucket(c, ptrs))
793 h = bch2_ec_stripe_head_get(c, target, 0, nr_replicas - 1,
794 wp == &c->copygc_write_point,
801 devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
803 for (i = 0; i < devs_sorted.nr; i++)
804 for (ec_idx = 0; ec_idx < h->s->nr_data; ec_idx++) {
805 if (!h->s->blocks[ec_idx])
808 ob = c->open_buckets + h->s->blocks[ec_idx];
809 if (ob->dev == devs_sorted.devs[i] &&
810 !test_and_set_bit(ec_idx, h->s->blocks_allocated))
815 ca = bch_dev_bkey_exists(c, ob->dev);
820 add_new_bucket(c, ptrs, devs_may_alloc,
821 nr_effective, have_cache, flags, ob);
822 atomic_inc(&h->s->pin);
824 bch2_ec_stripe_head_put(c, h);
828 /* Sector allocator */
830 static void get_buckets_from_writepoint(struct bch_fs *c,
831 struct open_buckets *ptrs,
832 struct write_point *wp,
833 struct bch_devs_mask *devs_may_alloc,
834 unsigned nr_replicas,
835 unsigned *nr_effective,
840 struct open_buckets ptrs_skip = { .nr = 0 };
841 struct open_bucket *ob;
844 open_bucket_for_each(c, &wp->ptrs, ob, i) {
845 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
847 if (*nr_effective < nr_replicas &&
848 test_bit(ob->dev, devs_may_alloc->d) &&
849 (ca->mi.durability ||
850 (wp->data_type == BCH_DATA_user && !*have_cache)) &&
851 (ob->ec || !need_ec)) {
852 add_new_bucket(c, ptrs, devs_may_alloc,
853 nr_effective, have_cache,
856 ob_push(c, &ptrs_skip, ob);
859 wp->ptrs = ptrs_skip;
862 static int open_bucket_add_buckets(struct btree_trans *trans,
863 struct open_buckets *ptrs,
864 struct write_point *wp,
865 struct bch_devs_list *devs_have,
867 unsigned erasure_code,
868 unsigned nr_replicas,
869 unsigned *nr_effective,
871 enum alloc_reserve reserve,
875 struct bch_fs *c = trans->c;
876 struct bch_devs_mask devs;
877 struct open_bucket *ob;
878 struct closure *cl = NULL;
883 devs = target_rw_devs(c, wp->data_type, target);
886 /* Don't allocate from devices we already have pointers to: */
887 for (i = 0; i < devs_have->nr; i++)
888 __clear_bit(devs_have->devs[i], devs.d);
890 open_bucket_for_each(c, ptrs, ob, i)
891 __clear_bit(ob->dev, devs.d);
894 if (!ec_open_bucket(c, ptrs)) {
895 get_buckets_from_writepoint(c, ptrs, wp, &devs,
896 nr_replicas, nr_effective,
897 have_cache, flags, true);
898 if (*nr_effective >= nr_replicas)
902 if (!ec_open_bucket(c, ptrs)) {
903 ret = bucket_alloc_from_stripe(c, ptrs, wp, &devs,
904 target, erasure_code,
905 nr_replicas, nr_effective,
906 have_cache, flags, _cl);
907 if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
908 bch2_err_matches(ret, BCH_ERR_freelist_empty) ||
909 bch2_err_matches(ret, BCH_ERR_open_buckets_empty))
911 if (*nr_effective >= nr_replicas)
916 get_buckets_from_writepoint(c, ptrs, wp, &devs,
917 nr_replicas, nr_effective,
918 have_cache, flags, false);
919 if (*nr_effective >= nr_replicas)
924 * Try nonblocking first, so that if one device is full we'll try from
927 ret = bch2_bucket_alloc_set_trans(trans, ptrs, &wp->stripe, &devs,
928 nr_replicas, nr_effective, have_cache,
931 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
932 !bch2_err_matches(ret, BCH_ERR_insufficient_devices) &&
941 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
942 struct open_buckets *obs)
944 struct open_buckets ptrs = { .nr = 0 };
945 struct open_bucket *ob, *ob2;
948 open_bucket_for_each(c, obs, ob, i) {
949 bool drop = !ca || ob->dev == ca->dev_idx;
951 if (!drop && ob->ec) {
952 mutex_lock(&ob->ec->lock);
953 for (j = 0; j < ob->ec->new_stripe.key.v.nr_blocks; j++) {
954 if (!ob->ec->blocks[j])
957 ob2 = c->open_buckets + ob->ec->blocks[j];
958 drop |= ob2->dev == ca->dev_idx;
960 mutex_unlock(&ob->ec->lock);
964 bch2_open_bucket_put(c, ob);
966 ob_push(c, &ptrs, ob);
972 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
973 struct write_point *wp)
975 mutex_lock(&wp->lock);
976 bch2_open_buckets_stop_dev(c, ca, &wp->ptrs);
977 mutex_unlock(&wp->lock);
980 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
981 unsigned long write_point)
984 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
986 return &c->write_points_hash[hash];
989 static struct write_point *__writepoint_find(struct hlist_head *head,
990 unsigned long write_point)
992 struct write_point *wp;
995 hlist_for_each_entry_rcu(wp, head, node)
996 if (wp->write_point == write_point)
1004 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
1006 u64 stranded = c->write_points_nr * c->bucket_size_max;
1007 u64 free = bch2_fs_usage_read_short(c).free;
1009 return stranded * factor > free;
1012 static bool try_increase_writepoints(struct bch_fs *c)
1014 struct write_point *wp;
1016 if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
1017 too_many_writepoints(c, 32))
1020 wp = c->write_points + c->write_points_nr++;
1021 hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
1025 static bool try_decrease_writepoints(struct bch_fs *c,
1028 struct write_point *wp;
1030 mutex_lock(&c->write_points_hash_lock);
1031 if (c->write_points_nr < old_nr) {
1032 mutex_unlock(&c->write_points_hash_lock);
1036 if (c->write_points_nr == 1 ||
1037 !too_many_writepoints(c, 8)) {
1038 mutex_unlock(&c->write_points_hash_lock);
1042 wp = c->write_points + --c->write_points_nr;
1044 hlist_del_rcu(&wp->node);
1045 mutex_unlock(&c->write_points_hash_lock);
1047 bch2_writepoint_stop(c, NULL, wp);
1051 static void bch2_trans_mutex_lock(struct btree_trans *trans,
1054 if (!mutex_trylock(lock)) {
1055 bch2_trans_unlock(trans);
1060 static struct write_point *writepoint_find(struct btree_trans *trans,
1061 unsigned long write_point)
1063 struct bch_fs *c = trans->c;
1064 struct write_point *wp, *oldest;
1065 struct hlist_head *head;
1067 if (!(write_point & 1UL)) {
1068 wp = (struct write_point *) write_point;
1069 bch2_trans_mutex_lock(trans, &wp->lock);
1073 head = writepoint_hash(c, write_point);
1075 wp = __writepoint_find(head, write_point);
1078 bch2_trans_mutex_lock(trans, &wp->lock);
1079 if (wp->write_point == write_point)
1081 mutex_unlock(&wp->lock);
1084 restart_find_oldest:
1086 for (wp = c->write_points;
1087 wp < c->write_points + c->write_points_nr; wp++)
1088 if (!oldest || time_before64(wp->last_used, oldest->last_used))
1091 bch2_trans_mutex_lock(trans, &oldest->lock);
1092 bch2_trans_mutex_lock(trans, &c->write_points_hash_lock);
1093 if (oldest >= c->write_points + c->write_points_nr ||
1094 try_increase_writepoints(c)) {
1095 mutex_unlock(&c->write_points_hash_lock);
1096 mutex_unlock(&oldest->lock);
1097 goto restart_find_oldest;
1100 wp = __writepoint_find(head, write_point);
1101 if (wp && wp != oldest) {
1102 mutex_unlock(&c->write_points_hash_lock);
1103 mutex_unlock(&oldest->lock);
1108 hlist_del_rcu(&wp->node);
1109 wp->write_point = write_point;
1110 hlist_add_head_rcu(&wp->node, head);
1111 mutex_unlock(&c->write_points_hash_lock);
1113 wp->last_used = sched_clock();
1118 * Get us an open_bucket we can allocate from, return with it locked:
1120 struct write_point *bch2_alloc_sectors_start_trans(struct btree_trans *trans,
1122 unsigned erasure_code,
1123 struct write_point_specifier write_point,
1124 struct bch_devs_list *devs_have,
1125 unsigned nr_replicas,
1126 unsigned nr_replicas_required,
1127 enum alloc_reserve reserve,
1131 struct bch_fs *c = trans->c;
1132 struct write_point *wp;
1133 struct open_bucket *ob;
1134 struct open_buckets ptrs;
1135 unsigned nr_effective, write_points_nr;
1136 unsigned ob_flags = 0;
1141 if (!(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS))
1142 ob_flags |= BUCKET_ALLOC_USE_DURABILITY;
1144 BUG_ON(!nr_replicas || !nr_replicas_required);
1148 write_points_nr = c->write_points_nr;
1151 wp = writepoint_find(trans, write_point.v);
1153 if (wp->data_type == BCH_DATA_user)
1154 ob_flags |= BUCKET_MAY_ALLOC_PARTIAL;
1156 /* metadata may not allocate on cache devices: */
1157 if (wp->data_type != BCH_DATA_user)
1160 if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
1161 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1162 target, erasure_code,
1163 nr_replicas, &nr_effective,
1164 &have_cache, reserve,
1167 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1168 target, erasure_code,
1169 nr_replicas, &nr_effective,
1170 &have_cache, reserve,
1173 bch2_err_matches(ret, BCH_ERR_transaction_restart))
1176 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1178 nr_replicas, &nr_effective,
1179 &have_cache, reserve,
1183 BUG_ON(!ret && nr_effective < nr_replicas);
1185 if (erasure_code && !ec_open_bucket(c, &ptrs))
1186 pr_debug("failed to get ec bucket: ret %u", ret);
1188 if (ret == -BCH_ERR_insufficient_devices &&
1189 nr_effective >= nr_replicas_required)
1195 /* Free buckets we didn't use: */
1196 open_bucket_for_each(c, &wp->ptrs, ob, i)
1197 open_bucket_free_unused(c, wp, ob);
1201 wp->sectors_free = UINT_MAX;
1203 open_bucket_for_each(c, &wp->ptrs, ob, i)
1204 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
1206 BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
1210 open_bucket_for_each(c, &wp->ptrs, ob, i)
1211 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
1212 ob_push(c, &ptrs, ob);
1214 open_bucket_free_unused(c, wp, ob);
1217 mutex_unlock(&wp->lock);
1219 if (bch2_err_matches(ret, BCH_ERR_freelist_empty) &&
1220 try_decrease_writepoints(c, write_points_nr))
1223 if (bch2_err_matches(ret, BCH_ERR_open_buckets_empty) ||
1224 bch2_err_matches(ret, BCH_ERR_freelist_empty))
1227 : ERR_PTR(-BCH_ERR_ENOSPC_bucket_alloc);
1229 if (bch2_err_matches(ret, BCH_ERR_insufficient_devices))
1230 return ERR_PTR(-EROFS);
1232 return ERR_PTR(ret);
1235 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
1237 unsigned erasure_code,
1238 struct write_point_specifier write_point,
1239 struct bch_devs_list *devs_have,
1240 unsigned nr_replicas,
1241 unsigned nr_replicas_required,
1242 enum alloc_reserve reserve,
1246 struct write_point *wp;
1248 bch2_trans_do(c, NULL, NULL, 0,
1249 PTR_ERR_OR_ZERO(wp = bch2_alloc_sectors_start_trans(&trans, target,
1254 nr_replicas_required,
1261 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *c, struct open_bucket *ob)
1263 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1265 return (struct bch_extent_ptr) {
1266 .type = 1 << BCH_EXTENT_ENTRY_ptr,
1269 .offset = bucket_to_sector(ca, ob->bucket) +
1270 ca->mi.bucket_size -
1276 * Append pointers to the space we just allocated to @k, and mark @sectors space
1277 * as allocated out of @ob
1279 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
1280 struct bkey_i *k, unsigned sectors,
1284 struct open_bucket *ob;
1287 BUG_ON(sectors > wp->sectors_free);
1288 wp->sectors_free -= sectors;
1290 open_bucket_for_each(c, &wp->ptrs, ob, i) {
1291 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1292 struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
1294 ptr.cached = cached ||
1295 (!ca->mi.durability &&
1296 wp->data_type == BCH_DATA_user);
1298 bch2_bkey_append_ptr(k, ptr);
1300 BUG_ON(sectors > ob->sectors_free);
1301 ob->sectors_free -= sectors;
1306 * Append pointers to the space we just allocated to @k, and mark @sectors space
1307 * as allocated out of @ob
1309 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
1311 struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
1312 struct open_bucket *ob;
1315 open_bucket_for_each(c, &wp->ptrs, ob, i)
1316 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
1319 mutex_unlock(&wp->lock);
1321 bch2_open_buckets_put(c, &ptrs);
1324 static inline void writepoint_init(struct write_point *wp,
1325 enum bch_data_type type)
1327 mutex_init(&wp->lock);
1328 wp->data_type = type;
1331 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
1333 struct open_bucket *ob;
1334 struct write_point *wp;
1336 mutex_init(&c->write_points_hash_lock);
1337 c->write_points_nr = ARRAY_SIZE(c->write_points);
1339 /* open bucket 0 is a sentinal NULL: */
1340 spin_lock_init(&c->open_buckets[0].lock);
1342 for (ob = c->open_buckets + 1;
1343 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
1344 spin_lock_init(&ob->lock);
1345 c->open_buckets_nr_free++;
1347 ob->freelist = c->open_buckets_freelist;
1348 c->open_buckets_freelist = ob - c->open_buckets;
1351 writepoint_init(&c->btree_write_point, BCH_DATA_btree);
1352 writepoint_init(&c->rebalance_write_point, BCH_DATA_user);
1353 writepoint_init(&c->copygc_write_point, BCH_DATA_user);
1355 for (wp = c->write_points;
1356 wp < c->write_points + c->write_points_nr; wp++) {
1357 writepoint_init(wp, BCH_DATA_user);
1359 wp->last_used = sched_clock();
1360 wp->write_point = (unsigned long) wp;
1361 hlist_add_head_rcu(&wp->node,
1362 writepoint_hash(c, wp->write_point));
1366 void bch2_open_buckets_to_text(struct printbuf *out, struct bch_fs *c)
1368 struct open_bucket *ob;
1370 for (ob = c->open_buckets;
1371 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1373 spin_lock(&ob->lock);
1374 if (ob->valid && !ob->on_partial_list) {
1375 prt_printf(out, "%zu ref %u type %s %u:%llu:%u\n",
1376 ob - c->open_buckets,
1377 atomic_read(&ob->pin),
1378 bch2_data_types[ob->data_type],
1379 ob->dev, ob->bucket, ob->gen);
1381 spin_unlock(&ob->lock);