1 // SPDX-License-Identifier: GPL-2.0
3 * Primary bucket allocation code
5 * Copyright 2012 Google, Inc.
7 * Allocation in bcache is done in terms of buckets:
9 * Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
10 * btree pointers - they must match for the pointer to be considered valid.
12 * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
13 * bucket simply by incrementing its gen.
15 * The gens (along with the priorities; it's really the gens are important but
16 * the code is named as if it's the priorities) are written in an arbitrary list
17 * of buckets on disk, with a pointer to them in the journal header.
19 * When we invalidate a bucket, we have to write its new gen to disk and wait
20 * for that write to complete before we use it - otherwise after a crash we
21 * could have pointers that appeared to be good but pointed to data that had
24 * Since the gens and priorities are all stored contiguously on disk, we can
25 * batch this up: We fill up the free_inc list with freshly invalidated buckets,
26 * call prio_write(), and when prio_write() finishes we pull buckets off the
27 * free_inc list and optionally discard them.
29 * free_inc isn't the only freelist - if it was, we'd often have to sleep while
30 * priorities and gens were being written before we could allocate. c->free is a
31 * smaller freelist, and buckets on that list are always ready to be used.
33 * If we've got discards enabled, that happens when a bucket moves from the
34 * free_inc list to the free list.
36 * It's important to ensure that gens don't wrap around - with respect to
37 * either the oldest gen in the btree or the gen on disk. This is quite
38 * difficult to do in practice, but we explicitly guard against it anyways - if
39 * a bucket is in danger of wrapping around we simply skip invalidating it that
40 * time around, and we garbage collect or rewrite the priorities sooner than we
41 * would have otherwise.
43 * bch2_bucket_alloc() allocates a single bucket from a specific device.
45 * bch2_bucket_alloc_set() allocates one or more buckets from different devices
46 * in a given filesystem.
48 * invalidate_buckets() drives all the processes described above. It's called
49 * from bch2_bucket_alloc() and a few other places that need to make sure free
52 * invalidate_buckets_(lru|fifo)() find buckets that are available to be
53 * invalidated, and then invalidate them and stick them on the free_inc list -
54 * in either lru or fifo order.
58 #include "alloc_background.h"
59 #include "alloc_foreground.h"
64 #include "disk_groups.h"
69 #include <linux/math64.h>
70 #include <linux/rculist.h>
71 #include <linux/rcupdate.h>
74 * Open buckets represent a bucket that's currently being allocated from. They
77 * - They track buckets that have been partially allocated, allowing for
78 * sub-bucket sized allocations - they're used by the sector allocator below
80 * - They provide a reference to the buckets they own that mark and sweep GC
81 * can find, until the new allocation has a pointer to it inserted into the
84 * When allocating some space with the sector allocator, the allocation comes
85 * with a reference to an open bucket - the caller is required to put that
86 * reference _after_ doing the index update that makes its allocation reachable.
89 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
91 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
94 bch2_ec_bucket_written(c, ob);
98 percpu_down_read(&c->mark_lock);
101 bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr),
102 false, gc_pos_alloc(c, ob), 0);
106 spin_unlock(&ob->lock);
107 percpu_up_read(&c->mark_lock);
109 spin_lock(&c->freelist_lock);
110 ob->freelist = c->open_buckets_freelist;
111 c->open_buckets_freelist = ob - c->open_buckets;
112 c->open_buckets_nr_free++;
113 spin_unlock(&c->freelist_lock);
115 closure_wake_up(&c->open_buckets_wait);
118 void bch2_open_bucket_write_error(struct bch_fs *c,
119 struct open_buckets *obs,
122 struct open_bucket *ob;
125 open_bucket_for_each(c, obs, ob, i)
126 if (ob->ptr.dev == dev &&
128 bch2_ec_bucket_cancel(c, ob);
131 static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
133 struct open_bucket *ob;
135 BUG_ON(!c->open_buckets_freelist || !c->open_buckets_nr_free);
137 ob = c->open_buckets + c->open_buckets_freelist;
138 c->open_buckets_freelist = ob->freelist;
139 atomic_set(&ob->pin, 1);
142 c->open_buckets_nr_free--;
146 static void open_bucket_free_unused(struct bch_fs *c,
147 struct write_point *wp,
148 struct open_bucket *ob)
150 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
151 bool may_realloc = wp->type == BCH_DATA_user;
153 BUG_ON(ca->open_buckets_partial_nr >
154 ARRAY_SIZE(ca->open_buckets_partial));
156 if (ca->open_buckets_partial_nr <
157 ARRAY_SIZE(ca->open_buckets_partial) &&
159 spin_lock(&c->freelist_lock);
160 ob->on_partial_list = true;
161 ca->open_buckets_partial[ca->open_buckets_partial_nr++] =
162 ob - c->open_buckets;
163 spin_unlock(&c->freelist_lock);
165 closure_wake_up(&c->open_buckets_wait);
166 closure_wake_up(&c->freelist_wait);
168 bch2_open_bucket_put(c, ob);
172 static void verify_not_stale(struct bch_fs *c, const struct open_buckets *obs)
174 #ifdef CONFIG_BCACHEFS_DEBUG
175 struct open_bucket *ob;
178 open_bucket_for_each(c, obs, ob, i) {
179 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
181 BUG_ON(ptr_stale(ca, &ob->ptr));
186 /* _only_ for allocating the journal on a new device: */
187 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
189 struct bucket_array *buckets;
193 buckets = bucket_array(ca);
195 for (b = ca->mi.first_bucket; b < ca->mi.nbuckets; b++)
196 if (is_available_bucket(buckets->b[b].mark))
204 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
210 return OPEN_BUCKETS_COUNT / 4;
212 return OPEN_BUCKETS_COUNT / 2;
217 * bch_bucket_alloc - allocate a single bucket from a specific device
219 * Returns index of bucket on success, 0 on failure
221 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
222 enum alloc_reserve reserve,
223 bool may_alloc_partial,
226 struct bucket_array *buckets;
227 struct open_bucket *ob;
230 spin_lock(&c->freelist_lock);
232 if (may_alloc_partial) {
235 for (i = ca->open_buckets_partial_nr - 1; i >= 0; --i) {
236 ob = c->open_buckets + ca->open_buckets_partial[i];
238 if (reserve <= ob->alloc_reserve) {
239 array_remove_item(ca->open_buckets_partial,
240 ca->open_buckets_partial_nr,
242 ob->on_partial_list = false;
243 ob->alloc_reserve = reserve;
244 spin_unlock(&c->freelist_lock);
250 if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
252 closure_wait(&c->open_buckets_wait, cl);
254 if (!c->blocked_allocate_open_bucket)
255 c->blocked_allocate_open_bucket = local_clock();
257 spin_unlock(&c->freelist_lock);
258 trace_open_bucket_alloc_fail(ca, reserve);
259 return ERR_PTR(-OPEN_BUCKETS_EMPTY);
262 if (likely(fifo_pop(&ca->free[RESERVE_NONE], bucket)))
267 if (fifo_pop(&ca->free[RESERVE_BTREE], bucket))
271 if (fifo_used(&ca->free[RESERVE_BTREE]) * 2 >=
272 ca->free[RESERVE_BTREE].size &&
273 fifo_pop(&ca->free[RESERVE_BTREE], bucket))
276 case RESERVE_MOVINGGC:
277 if (fifo_pop(&ca->free[RESERVE_MOVINGGC], bucket))
285 closure_wait(&c->freelist_wait, cl);
287 if (!c->blocked_allocate)
288 c->blocked_allocate = local_clock();
290 spin_unlock(&c->freelist_lock);
292 trace_bucket_alloc_fail(ca, reserve);
293 return ERR_PTR(-FREELIST_EMPTY);
295 verify_not_on_freelist(c, ca, bucket);
297 ob = bch2_open_bucket_alloc(c);
299 spin_lock(&ob->lock);
300 buckets = bucket_array(ca);
303 ob->sectors_free = ca->mi.bucket_size;
304 ob->alloc_reserve = reserve;
305 ob->ptr = (struct bch_extent_ptr) {
306 .type = 1 << BCH_EXTENT_ENTRY_ptr,
307 .gen = buckets->b[bucket].mark.gen,
308 .offset = bucket_to_sector(ca, bucket),
312 bucket_io_clock_reset(c, ca, bucket, READ);
313 bucket_io_clock_reset(c, ca, bucket, WRITE);
314 spin_unlock(&ob->lock);
316 if (c->blocked_allocate_open_bucket) {
317 bch2_time_stats_update(
318 &c->times[BCH_TIME_blocked_allocate_open_bucket],
319 c->blocked_allocate_open_bucket);
320 c->blocked_allocate_open_bucket = 0;
323 if (c->blocked_allocate) {
324 bch2_time_stats_update(
325 &c->times[BCH_TIME_blocked_allocate],
326 c->blocked_allocate);
327 c->blocked_allocate = 0;
330 spin_unlock(&c->freelist_lock);
332 bch2_wake_allocator(ca);
334 trace_bucket_alloc(ca, reserve);
338 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
339 unsigned l, unsigned r)
341 return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
342 (stripe->next_alloc[l] < stripe->next_alloc[r]));
345 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
347 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
348 struct dev_stripe_state *stripe,
349 struct bch_devs_mask *devs)
351 struct dev_alloc_list ret = { .nr = 0 };
354 for_each_set_bit(i, devs->d, BCH_SB_MEMBERS_MAX)
355 ret.devs[ret.nr++] = i;
357 bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
361 void bch2_dev_stripe_increment(struct bch_dev *ca,
362 struct dev_stripe_state *stripe)
364 u64 *v = stripe->next_alloc + ca->dev_idx;
365 u64 free_space = dev_buckets_free(ca);
366 u64 free_space_inv = free_space
367 ? div64_u64(1ULL << 48, free_space)
371 if (*v + free_space_inv >= *v)
372 *v += free_space_inv;
376 for (v = stripe->next_alloc;
377 v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
378 *v = *v < scale ? 0 : *v - scale;
381 #define BUCKET_MAY_ALLOC_PARTIAL (1 << 0)
382 #define BUCKET_ALLOC_USE_DURABILITY (1 << 1)
384 static void add_new_bucket(struct bch_fs *c,
385 struct open_buckets *ptrs,
386 struct bch_devs_mask *devs_may_alloc,
387 unsigned *nr_effective,
390 struct open_bucket *ob)
392 unsigned durability =
393 bch_dev_bkey_exists(c, ob->ptr.dev)->mi.durability;
395 __clear_bit(ob->ptr.dev, devs_may_alloc->d);
396 *nr_effective += (flags & BUCKET_ALLOC_USE_DURABILITY)
398 *have_cache |= !durability;
400 ob_push(c, ptrs, ob);
403 enum bucket_alloc_ret
404 bch2_bucket_alloc_set(struct bch_fs *c,
405 struct open_buckets *ptrs,
406 struct dev_stripe_state *stripe,
407 struct bch_devs_mask *devs_may_alloc,
408 unsigned nr_replicas,
409 unsigned *nr_effective,
411 enum alloc_reserve reserve,
415 struct dev_alloc_list devs_sorted =
416 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
418 enum bucket_alloc_ret ret = INSUFFICIENT_DEVICES;
421 BUG_ON(*nr_effective >= nr_replicas);
423 for (i = 0; i < devs_sorted.nr; i++) {
424 struct open_bucket *ob;
426 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
430 if (!ca->mi.durability && *have_cache)
433 ob = bch2_bucket_alloc(c, ca, reserve,
434 flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
443 add_new_bucket(c, ptrs, devs_may_alloc,
444 nr_effective, have_cache, flags, ob);
446 bch2_dev_stripe_increment(ca, stripe);
448 if (*nr_effective >= nr_replicas)
449 return ALLOC_SUCCESS;
455 /* Allocate from stripes: */
458 * if we can't allocate a new stripe because there are already too many
459 * partially filled stripes, force allocating from an existing stripe even when
460 * it's to a device we don't want:
463 static void bucket_alloc_from_stripe(struct bch_fs *c,
464 struct open_buckets *ptrs,
465 struct write_point *wp,
466 struct bch_devs_mask *devs_may_alloc,
468 unsigned erasure_code,
469 unsigned nr_replicas,
470 unsigned *nr_effective,
474 struct dev_alloc_list devs_sorted;
475 struct ec_stripe_head *h;
476 struct open_bucket *ob;
486 if (ec_open_bucket(c, ptrs))
489 h = bch2_ec_stripe_head_get(c, target, 0, nr_replicas - 1);
493 devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
495 for (i = 0; i < devs_sorted.nr; i++)
496 open_bucket_for_each(c, &h->s->blocks, ob, ec_idx)
497 if (ob->ptr.dev == devs_sorted.devs[i] &&
498 !test_and_set_bit(h->s->data_block_idx[ec_idx],
499 h->s->blocks_allocated))
503 ca = bch_dev_bkey_exists(c, ob->ptr.dev);
505 ob->ec_idx = h->s->data_block_idx[ec_idx];
508 add_new_bucket(c, ptrs, devs_may_alloc,
509 nr_effective, have_cache, flags, ob);
510 atomic_inc(&h->s->pin);
512 bch2_ec_stripe_head_put(c, h);
515 /* Sector allocator */
517 static void get_buckets_from_writepoint(struct bch_fs *c,
518 struct open_buckets *ptrs,
519 struct write_point *wp,
520 struct bch_devs_mask *devs_may_alloc,
521 unsigned nr_replicas,
522 unsigned *nr_effective,
527 struct open_buckets ptrs_skip = { .nr = 0 };
528 struct open_bucket *ob;
531 open_bucket_for_each(c, &wp->ptrs, ob, i) {
532 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
534 if (*nr_effective < nr_replicas &&
535 test_bit(ob->ptr.dev, devs_may_alloc->d) &&
536 (ca->mi.durability ||
537 (wp->type == BCH_DATA_user && !*have_cache)) &&
538 (ob->ec || !need_ec)) {
539 add_new_bucket(c, ptrs, devs_may_alloc,
540 nr_effective, have_cache,
543 ob_push(c, &ptrs_skip, ob);
546 wp->ptrs = ptrs_skip;
549 static enum bucket_alloc_ret
550 open_bucket_add_buckets(struct bch_fs *c,
551 struct open_buckets *ptrs,
552 struct write_point *wp,
553 struct bch_devs_list *devs_have,
555 unsigned erasure_code,
556 unsigned nr_replicas,
557 unsigned *nr_effective,
559 enum alloc_reserve reserve,
563 struct bch_devs_mask devs;
564 struct open_bucket *ob;
565 struct closure *cl = NULL;
566 enum bucket_alloc_ret ret;
570 devs = target_rw_devs(c, wp->type, target);
573 /* Don't allocate from devices we already have pointers to: */
574 for (i = 0; i < devs_have->nr; i++)
575 __clear_bit(devs_have->devs[i], devs.d);
577 open_bucket_for_each(c, ptrs, ob, i)
578 __clear_bit(ob->ptr.dev, devs.d);
581 get_buckets_from_writepoint(c, ptrs, wp, &devs,
582 nr_replicas, nr_effective,
583 have_cache, flags, true);
584 if (*nr_effective >= nr_replicas)
587 bucket_alloc_from_stripe(c, ptrs, wp, &devs,
588 target, erasure_code,
589 nr_replicas, nr_effective,
591 if (*nr_effective >= nr_replicas)
595 get_buckets_from_writepoint(c, ptrs, wp, &devs,
596 nr_replicas, nr_effective,
597 have_cache, flags, false);
598 if (*nr_effective >= nr_replicas)
601 percpu_down_read(&c->mark_lock);
606 * Try nonblocking first, so that if one device is full we'll try from
609 ret = bch2_bucket_alloc_set(c, ptrs, &wp->stripe, &devs,
610 nr_replicas, nr_effective, have_cache,
612 if (ret && ret != INSUFFICIENT_DEVICES && !cl && _cl) {
618 percpu_up_read(&c->mark_lock);
623 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
624 struct open_buckets *obs)
626 struct open_buckets ptrs = { .nr = 0 };
627 struct open_bucket *ob, *ob2;
630 open_bucket_for_each(c, obs, ob, i) {
631 bool drop = !ca || ob->ptr.dev == ca->dev_idx;
633 if (!drop && ob->ec) {
634 mutex_lock(&ob->ec->lock);
635 open_bucket_for_each(c, &ob->ec->blocks, ob2, j)
636 drop |= ob2->ptr.dev == ca->dev_idx;
637 open_bucket_for_each(c, &ob->ec->parity, ob2, j)
638 drop |= ob2->ptr.dev == ca->dev_idx;
639 mutex_unlock(&ob->ec->lock);
643 bch2_open_bucket_put(c, ob);
645 ob_push(c, &ptrs, ob);
651 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
652 struct write_point *wp)
654 mutex_lock(&wp->lock);
655 bch2_open_buckets_stop_dev(c, ca, &wp->ptrs);
656 mutex_unlock(&wp->lock);
659 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
660 unsigned long write_point)
663 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
665 return &c->write_points_hash[hash];
668 static struct write_point *__writepoint_find(struct hlist_head *head,
669 unsigned long write_point)
671 struct write_point *wp;
673 hlist_for_each_entry_rcu(wp, head, node)
674 if (wp->write_point == write_point)
680 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
682 u64 stranded = c->write_points_nr * c->bucket_size_max;
683 u64 free = bch2_fs_usage_read_short(c).free;
685 return stranded * factor > free;
688 static bool try_increase_writepoints(struct bch_fs *c)
690 struct write_point *wp;
692 if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
693 too_many_writepoints(c, 32))
696 wp = c->write_points + c->write_points_nr++;
697 hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
701 static bool try_decrease_writepoints(struct bch_fs *c,
704 struct write_point *wp;
706 mutex_lock(&c->write_points_hash_lock);
707 if (c->write_points_nr < old_nr) {
708 mutex_unlock(&c->write_points_hash_lock);
712 if (c->write_points_nr == 1 ||
713 !too_many_writepoints(c, 8)) {
714 mutex_unlock(&c->write_points_hash_lock);
718 wp = c->write_points + --c->write_points_nr;
720 hlist_del_rcu(&wp->node);
721 mutex_unlock(&c->write_points_hash_lock);
723 bch2_writepoint_stop(c, NULL, wp);
727 static struct write_point *writepoint_find(struct bch_fs *c,
728 unsigned long write_point)
730 struct write_point *wp, *oldest;
731 struct hlist_head *head;
733 if (!(write_point & 1UL)) {
734 wp = (struct write_point *) write_point;
735 mutex_lock(&wp->lock);
739 head = writepoint_hash(c, write_point);
741 wp = __writepoint_find(head, write_point);
744 mutex_lock(&wp->lock);
745 if (wp->write_point == write_point)
747 mutex_unlock(&wp->lock);
752 for (wp = c->write_points;
753 wp < c->write_points + c->write_points_nr; wp++)
754 if (!oldest || time_before64(wp->last_used, oldest->last_used))
757 mutex_lock(&oldest->lock);
758 mutex_lock(&c->write_points_hash_lock);
759 if (oldest >= c->write_points + c->write_points_nr ||
760 try_increase_writepoints(c)) {
761 mutex_unlock(&c->write_points_hash_lock);
762 mutex_unlock(&oldest->lock);
763 goto restart_find_oldest;
766 wp = __writepoint_find(head, write_point);
767 if (wp && wp != oldest) {
768 mutex_unlock(&c->write_points_hash_lock);
769 mutex_unlock(&oldest->lock);
774 hlist_del_rcu(&wp->node);
775 wp->write_point = write_point;
776 hlist_add_head_rcu(&wp->node, head);
777 mutex_unlock(&c->write_points_hash_lock);
779 wp->last_used = sched_clock();
784 * Get us an open_bucket we can allocate from, return with it locked:
786 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
788 unsigned erasure_code,
789 struct write_point_specifier write_point,
790 struct bch_devs_list *devs_have,
791 unsigned nr_replicas,
792 unsigned nr_replicas_required,
793 enum alloc_reserve reserve,
797 struct write_point *wp;
798 struct open_bucket *ob;
799 struct open_buckets ptrs;
800 unsigned nr_effective, write_points_nr;
801 unsigned ob_flags = 0;
803 enum bucket_alloc_ret ret;
806 if (!(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS))
807 ob_flags |= BUCKET_ALLOC_USE_DURABILITY;
809 BUG_ON(!nr_replicas || !nr_replicas_required);
813 write_points_nr = c->write_points_nr;
816 wp = writepoint_find(c, write_point.v);
818 if (wp->type == BCH_DATA_user)
819 ob_flags |= BUCKET_MAY_ALLOC_PARTIAL;
821 /* metadata may not allocate on cache devices: */
822 if (wp->type != BCH_DATA_user)
825 if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
826 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
827 target, erasure_code,
828 nr_replicas, &nr_effective,
829 &have_cache, reserve,
832 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
833 target, erasure_code,
834 nr_replicas, &nr_effective,
835 &have_cache, reserve,
840 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
842 nr_replicas, &nr_effective,
843 &have_cache, reserve,
847 BUG_ON(!ret && nr_effective < nr_replicas);
849 if (erasure_code && !ec_open_bucket(c, &ptrs))
850 pr_debug("failed to get ec bucket: ret %u", ret);
852 if (ret == INSUFFICIENT_DEVICES &&
853 nr_effective >= nr_replicas_required)
859 /* Free buckets we didn't use: */
860 open_bucket_for_each(c, &wp->ptrs, ob, i)
861 open_bucket_free_unused(c, wp, ob);
865 wp->sectors_free = UINT_MAX;
867 open_bucket_for_each(c, &wp->ptrs, ob, i)
868 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
870 BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
872 verify_not_stale(c, &wp->ptrs);
876 open_bucket_for_each(c, &wp->ptrs, ob, i)
877 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
878 ob_push(c, &ptrs, ob);
880 open_bucket_free_unused(c, wp, ob);
883 mutex_unlock(&wp->lock);
885 if (ret == FREELIST_EMPTY &&
886 try_decrease_writepoints(c, write_points_nr))
890 case OPEN_BUCKETS_EMPTY:
892 return cl ? ERR_PTR(-EAGAIN) : ERR_PTR(-ENOSPC);
893 case INSUFFICIENT_DEVICES:
894 return ERR_PTR(-EROFS);
901 * Append pointers to the space we just allocated to @k, and mark @sectors space
902 * as allocated out of @ob
904 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
905 struct bkey_i *k, unsigned sectors)
908 struct open_bucket *ob;
911 BUG_ON(sectors > wp->sectors_free);
912 wp->sectors_free -= sectors;
914 open_bucket_for_each(c, &wp->ptrs, ob, i) {
915 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
916 struct bch_extent_ptr tmp = ob->ptr;
918 tmp.cached = !ca->mi.durability &&
919 wp->type == BCH_DATA_user;
921 tmp.offset += ca->mi.bucket_size - ob->sectors_free;
922 bch2_bkey_append_ptr(k, tmp);
924 BUG_ON(sectors > ob->sectors_free);
925 ob->sectors_free -= sectors;
930 * Append pointers to the space we just allocated to @k, and mark @sectors space
931 * as allocated out of @ob
933 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
935 struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
936 struct open_bucket *ob;
939 open_bucket_for_each(c, &wp->ptrs, ob, i)
940 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
943 mutex_unlock(&wp->lock);
945 bch2_open_buckets_put(c, &ptrs);
948 static inline void writepoint_init(struct write_point *wp,
949 enum bch_data_type type)
951 mutex_init(&wp->lock);
955 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
957 struct open_bucket *ob;
958 struct write_point *wp;
960 mutex_init(&c->write_points_hash_lock);
961 c->write_points_nr = ARRAY_SIZE(c->write_points);
963 /* open bucket 0 is a sentinal NULL: */
964 spin_lock_init(&c->open_buckets[0].lock);
966 for (ob = c->open_buckets + 1;
967 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
968 spin_lock_init(&ob->lock);
969 c->open_buckets_nr_free++;
971 ob->freelist = c->open_buckets_freelist;
972 c->open_buckets_freelist = ob - c->open_buckets;
975 writepoint_init(&c->btree_write_point, BCH_DATA_btree);
976 writepoint_init(&c->rebalance_write_point, BCH_DATA_user);
977 writepoint_init(&c->copygc_write_point, BCH_DATA_user);
979 for (wp = c->write_points;
980 wp < c->write_points + c->write_points_nr; wp++) {
981 writepoint_init(wp, BCH_DATA_user);
983 wp->last_used = sched_clock();
984 wp->write_point = (unsigned long) wp;
985 hlist_add_head_rcu(&wp->node,
986 writepoint_hash(c, wp->write_point));