2 * Primary bucket allocation code
4 * Copyright 2012 Google, Inc.
6 * Allocation in bcache is done in terms of buckets:
8 * Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
9 * btree pointers - they must match for the pointer to be considered valid.
11 * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
12 * bucket simply by incrementing its gen.
14 * The gens (along with the priorities; it's really the gens are important but
15 * the code is named as if it's the priorities) are written in an arbitrary list
16 * of buckets on disk, with a pointer to them in the journal header.
18 * When we invalidate a bucket, we have to write its new gen to disk and wait
19 * for that write to complete before we use it - otherwise after a crash we
20 * could have pointers that appeared to be good but pointed to data that had
23 * Since the gens and priorities are all stored contiguously on disk, we can
24 * batch this up: We fill up the free_inc list with freshly invalidated buckets,
25 * call prio_write(), and when prio_write() finishes we pull buckets off the
26 * free_inc list and optionally discard them.
28 * free_inc isn't the only freelist - if it was, we'd often have to sleep while
29 * priorities and gens were being written before we could allocate. c->free is a
30 * smaller freelist, and buckets on that list are always ready to be used.
32 * If we've got discards enabled, that happens when a bucket moves from the
33 * free_inc list to the free list.
35 * It's important to ensure that gens don't wrap around - with respect to
36 * either the oldest gen in the btree or the gen on disk. This is quite
37 * difficult to do in practice, but we explicitly guard against it anyways - if
38 * a bucket is in danger of wrapping around we simply skip invalidating it that
39 * time around, and we garbage collect or rewrite the priorities sooner than we
40 * would have otherwise.
42 * bch2_bucket_alloc() allocates a single bucket from a specific device.
44 * bch2_bucket_alloc_set() allocates one or more buckets from different devices
45 * in a given filesystem.
47 * invalidate_buckets() drives all the processes described above. It's called
48 * from bch2_bucket_alloc() and a few other places that need to make sure free
51 * invalidate_buckets_(lru|fifo)() find buckets that are available to be
52 * invalidated, and then invalidate them and stick them on the free_inc list -
53 * in either lru or fifo order.
57 #include "alloc_background.h"
58 #include "alloc_foreground.h"
63 #include "disk_groups.h"
67 #include <linux/math64.h>
68 #include <linux/rculist.h>
69 #include <linux/rcupdate.h>
70 #include <trace/events/bcachefs.h>
72 enum bucket_alloc_ret {
75 FREELIST_EMPTY, /* Allocator thread not keeping up */
79 * Open buckets represent a bucket that's currently being allocated from. They
82 * - They track buckets that have been partially allocated, allowing for
83 * sub-bucket sized allocations - they're used by the sector allocator below
85 * - They provide a reference to the buckets they own that mark and sweep GC
86 * can find, until the new allocation has a pointer to it inserted into the
89 * When allocating some space with the sector allocator, the allocation comes
90 * with a reference to an open bucket - the caller is required to put that
91 * reference _after_ doing the index update that makes its allocation reachable.
94 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
96 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
99 bch2_ec_bucket_written(c, ob);
103 percpu_down_read_preempt_disable(&c->mark_lock);
104 spin_lock(&ob->lock);
106 bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr),
107 false, gc_pos_alloc(c, ob), 0);
110 spin_unlock(&ob->lock);
111 percpu_up_read_preempt_enable(&c->mark_lock);
113 spin_lock(&c->freelist_lock);
114 ob->freelist = c->open_buckets_freelist;
115 c->open_buckets_freelist = ob - c->open_buckets;
116 c->open_buckets_nr_free++;
117 spin_unlock(&c->freelist_lock);
119 closure_wake_up(&c->open_buckets_wait);
122 void bch2_open_bucket_write_error(struct bch_fs *c,
123 struct open_buckets *obs,
126 struct open_bucket *ob;
129 open_bucket_for_each(c, obs, ob, i)
130 if (ob->ptr.dev == dev &&
132 bch2_ec_bucket_cancel(c, ob);
135 static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
137 struct open_bucket *ob;
139 BUG_ON(!c->open_buckets_freelist || !c->open_buckets_nr_free);
141 ob = c->open_buckets + c->open_buckets_freelist;
142 c->open_buckets_freelist = ob->freelist;
143 atomic_set(&ob->pin, 1);
145 c->open_buckets_nr_free--;
149 static void open_bucket_free_unused(struct bch_fs *c,
150 struct open_bucket *ob,
153 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
155 BUG_ON(ca->open_buckets_partial_nr >=
156 ARRAY_SIZE(ca->open_buckets_partial));
158 if (ca->open_buckets_partial_nr <
159 ARRAY_SIZE(ca->open_buckets_partial) &&
161 spin_lock(&c->freelist_lock);
162 ob->on_partial_list = true;
163 ca->open_buckets_partial[ca->open_buckets_partial_nr++] =
164 ob - c->open_buckets;
165 spin_unlock(&c->freelist_lock);
167 closure_wake_up(&c->open_buckets_wait);
168 closure_wake_up(&c->freelist_wait);
170 bch2_open_bucket_put(c, ob);
174 static void verify_not_stale(struct bch_fs *c, const struct open_buckets *obs)
176 #ifdef CONFIG_BCACHEFS_DEBUG
177 struct open_bucket *ob;
180 open_bucket_for_each(c, obs, ob, i) {
181 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
183 BUG_ON(ptr_stale(ca, &ob->ptr));
188 /* _only_ for allocating the journal on a new device: */
189 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
191 struct bucket_array *buckets;
195 buckets = bucket_array(ca);
197 for (b = ca->mi.first_bucket; b < ca->mi.nbuckets; b++)
198 if (is_available_bucket(buckets->b[b].mark))
206 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
212 return BTREE_NODE_RESERVE / 2;
214 return BTREE_NODE_RESERVE;
219 * bch_bucket_alloc - allocate a single bucket from a specific device
221 * Returns index of bucket on success, 0 on failure
223 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
224 enum alloc_reserve reserve,
225 bool may_alloc_partial,
228 struct bucket_array *buckets;
229 struct open_bucket *ob;
232 spin_lock(&c->freelist_lock);
234 if (may_alloc_partial &&
235 ca->open_buckets_partial_nr) {
236 ob = c->open_buckets +
237 ca->open_buckets_partial[--ca->open_buckets_partial_nr];
238 ob->on_partial_list = false;
239 spin_unlock(&c->freelist_lock);
243 if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
245 closure_wait(&c->open_buckets_wait, cl);
246 spin_unlock(&c->freelist_lock);
247 trace_open_bucket_alloc_fail(ca, reserve);
248 return ERR_PTR(-OPEN_BUCKETS_EMPTY);
251 if (likely(fifo_pop(&ca->free[RESERVE_NONE], bucket)))
256 if (fifo_pop(&ca->free[RESERVE_BTREE], bucket))
260 if (fifo_used(&ca->free[RESERVE_BTREE]) * 2 >=
261 ca->free[RESERVE_BTREE].size &&
262 fifo_pop(&ca->free[RESERVE_BTREE], bucket))
265 case RESERVE_MOVINGGC:
266 if (fifo_pop(&ca->free[RESERVE_MOVINGGC], bucket))
274 closure_wait(&c->freelist_wait, cl);
276 spin_unlock(&c->freelist_lock);
278 trace_bucket_alloc_fail(ca, reserve);
279 return ERR_PTR(-FREELIST_EMPTY);
281 verify_not_on_freelist(c, ca, bucket);
283 ob = bch2_open_bucket_alloc(c);
285 spin_lock(&ob->lock);
286 buckets = bucket_array(ca);
289 ob->sectors_free = ca->mi.bucket_size;
290 ob->ptr = (struct bch_extent_ptr) {
291 .type = 1 << BCH_EXTENT_ENTRY_ptr,
292 .gen = buckets->b[bucket].mark.gen,
293 .offset = bucket_to_sector(ca, bucket),
297 bucket_io_clock_reset(c, ca, bucket, READ);
298 bucket_io_clock_reset(c, ca, bucket, WRITE);
299 spin_unlock(&ob->lock);
301 spin_unlock(&c->freelist_lock);
303 bch2_wake_allocator(ca);
305 trace_bucket_alloc(ca, reserve);
309 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
310 unsigned l, unsigned r)
312 return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
313 (stripe->next_alloc[l] < stripe->next_alloc[r]));
316 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
318 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
319 struct dev_stripe_state *stripe,
320 struct bch_devs_mask *devs)
322 struct dev_alloc_list ret = { .nr = 0 };
326 for_each_member_device_rcu(ca, c, i, devs)
327 ret.devs[ret.nr++] = i;
329 bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
333 void bch2_dev_stripe_increment(struct bch_fs *c, struct bch_dev *ca,
334 struct dev_stripe_state *stripe)
336 u64 *v = stripe->next_alloc + ca->dev_idx;
337 u64 free_space = dev_buckets_free(c, ca);
338 u64 free_space_inv = free_space
339 ? div64_u64(1ULL << 48, free_space)
343 if (*v + free_space_inv >= *v)
344 *v += free_space_inv;
348 for (v = stripe->next_alloc;
349 v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
350 *v = *v < scale ? 0 : *v - scale;
353 #define BUCKET_MAY_ALLOC_PARTIAL (1 << 0)
354 #define BUCKET_ALLOC_USE_DURABILITY (1 << 1)
356 static int bch2_bucket_alloc_set(struct bch_fs *c,
357 struct open_buckets *ptrs,
358 struct dev_stripe_state *stripe,
359 struct bch_devs_mask *devs_may_alloc,
360 unsigned nr_replicas,
361 unsigned *nr_effective,
363 enum alloc_reserve reserve,
367 struct dev_alloc_list devs_sorted =
368 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
370 bool alloc_failure = false;
371 unsigned i, durability;
373 BUG_ON(*nr_effective >= nr_replicas);
375 for (i = 0; i < devs_sorted.nr; i++) {
376 struct open_bucket *ob;
378 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
382 if (!ca->mi.durability && *have_cache)
385 ob = bch2_bucket_alloc(c, ca, reserve,
386 flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
388 enum bucket_alloc_ret ret = -PTR_ERR(ob);
390 WARN_ON(reserve == RESERVE_MOVINGGC &&
391 ret != OPEN_BUCKETS_EMPTY);
395 if (ret == OPEN_BUCKETS_EMPTY)
397 alloc_failure = true;
401 durability = (flags & BUCKET_ALLOC_USE_DURABILITY)
402 ? ca->mi.durability : 1;
404 __clear_bit(ca->dev_idx, devs_may_alloc->d);
405 *nr_effective += durability;
406 *have_cache |= !durability;
408 ob_push(c, ptrs, ob);
410 bch2_dev_stripe_increment(c, ca, stripe);
412 if (*nr_effective >= nr_replicas)
416 return alloc_failure ? -ENOSPC : -EROFS;
419 /* Allocate from stripes: */
422 * XXX: use a higher watermark for allocating open buckets here:
424 static int ec_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
426 struct bch_devs_mask devs;
427 struct open_bucket *ob;
428 unsigned i, nr_have = 0, nr_data =
429 min_t(unsigned, h->nr_active_devs,
430 EC_STRIPE_MAX) - h->redundancy;
431 bool have_cache = true;
434 BUG_ON(h->blocks.nr > nr_data);
435 BUG_ON(h->parity.nr > h->redundancy);
439 open_bucket_for_each(c, &h->parity, ob, i)
440 __clear_bit(ob->ptr.dev, devs.d);
441 open_bucket_for_each(c, &h->blocks, ob, i)
442 __clear_bit(ob->ptr.dev, devs.d);
444 percpu_down_read_preempt_disable(&c->mark_lock);
447 if (h->parity.nr < h->redundancy) {
448 nr_have = h->parity.nr;
450 ret = bch2_bucket_alloc_set(c, &h->parity,
463 if (h->blocks.nr < nr_data) {
464 nr_have = h->blocks.nr;
466 ret = bch2_bucket_alloc_set(c, &h->blocks,
480 percpu_up_read_preempt_enable(&c->mark_lock);
482 return bch2_ec_stripe_new_alloc(c, h);
485 percpu_up_read_preempt_enable(&c->mark_lock);
490 * if we can't allocate a new stripe because there are already too many
491 * partially filled stripes, force allocating from an existing stripe even when
492 * it's to a device we don't want:
495 static void bucket_alloc_from_stripe(struct bch_fs *c,
496 struct open_buckets *ptrs,
497 struct write_point *wp,
498 struct bch_devs_mask *devs_may_alloc,
500 unsigned erasure_code,
501 unsigned nr_replicas,
502 unsigned *nr_effective,
505 struct dev_alloc_list devs_sorted;
506 struct ec_stripe_head *h;
507 struct open_bucket *ob;
517 if (ec_open_bucket(c, ptrs))
520 h = bch2_ec_stripe_head_get(c, target, erasure_code, nr_replicas - 1);
524 if (!h->s && ec_stripe_alloc(c, h))
528 devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
531 for (i = 0; i < devs_sorted.nr; i++)
532 open_bucket_for_each(c, &h->s->blocks, ob, ec_idx)
533 if (ob->ptr.dev == devs_sorted.devs[i] &&
534 !test_and_set_bit(ec_idx, h->s->blocks_allocated))
538 ca = bch_dev_bkey_exists(c, ob->ptr.dev);
543 __clear_bit(ob->ptr.dev, devs_may_alloc->d);
544 *nr_effective += ca->mi.durability;
545 *have_cache |= !ca->mi.durability;
547 ob_push(c, ptrs, ob);
548 atomic_inc(&h->s->pin);
550 bch2_ec_stripe_head_put(h);
553 /* Sector allocator */
555 static void get_buckets_from_writepoint(struct bch_fs *c,
556 struct open_buckets *ptrs,
557 struct write_point *wp,
558 struct bch_devs_mask *devs_may_alloc,
559 unsigned nr_replicas,
560 unsigned *nr_effective,
564 struct open_buckets ptrs_skip = { .nr = 0 };
565 struct open_bucket *ob;
568 open_bucket_for_each(c, &wp->ptrs, ob, i) {
569 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
571 if (*nr_effective < nr_replicas &&
572 test_bit(ob->ptr.dev, devs_may_alloc->d) &&
573 (ca->mi.durability ||
574 (wp->type == BCH_DATA_USER && !*have_cache)) &&
575 (ob->ec || !need_ec)) {
576 __clear_bit(ob->ptr.dev, devs_may_alloc->d);
577 *nr_effective += ca->mi.durability;
578 *have_cache |= !ca->mi.durability;
580 ob_push(c, ptrs, ob);
582 ob_push(c, &ptrs_skip, ob);
585 wp->ptrs = ptrs_skip;
588 static int open_bucket_add_buckets(struct bch_fs *c,
589 struct open_buckets *ptrs,
590 struct write_point *wp,
591 struct bch_devs_list *devs_have,
593 unsigned erasure_code,
594 unsigned nr_replicas,
595 unsigned *nr_effective,
597 enum alloc_reserve reserve,
600 struct bch_devs_mask devs;
601 struct open_bucket *ob;
602 struct closure *cl = NULL;
603 unsigned i, flags = BUCKET_ALLOC_USE_DURABILITY;
606 if (wp->type == BCH_DATA_USER)
607 flags |= BUCKET_MAY_ALLOC_PARTIAL;
610 devs = target_rw_devs(c, wp->type, target);
613 /* Don't allocate from devices we already have pointers to: */
614 for (i = 0; i < devs_have->nr; i++)
615 __clear_bit(devs_have->devs[i], devs.d);
617 open_bucket_for_each(c, ptrs, ob, i)
618 __clear_bit(ob->ptr.dev, devs.d);
621 get_buckets_from_writepoint(c, ptrs, wp, &devs,
622 nr_replicas, nr_effective,
624 if (*nr_effective >= nr_replicas)
627 bucket_alloc_from_stripe(c, ptrs, wp, &devs,
628 target, erasure_code,
629 nr_replicas, nr_effective,
631 if (*nr_effective >= nr_replicas)
635 get_buckets_from_writepoint(c, ptrs, wp, &devs,
636 nr_replicas, nr_effective,
638 if (*nr_effective >= nr_replicas)
641 percpu_down_read_preempt_disable(&c->mark_lock);
646 * Try nonblocking first, so that if one device is full we'll try from
649 ret = bch2_bucket_alloc_set(c, ptrs, &wp->stripe, &devs,
650 nr_replicas, nr_effective, have_cache,
652 if (ret && ret != -EROFS && !cl && _cl) {
658 percpu_up_read_preempt_enable(&c->mark_lock);
663 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
664 struct open_buckets *obs,
665 enum bch_data_type data_type)
667 struct open_buckets ptrs = { .nr = 0 };
668 struct open_bucket *ob, *ob2;
671 open_bucket_for_each(c, obs, ob, i) {
672 bool drop = !ca || ob->ptr.dev == ca->dev_idx;
674 if (!drop && ob->ec) {
675 mutex_lock(&ob->ec->lock);
676 open_bucket_for_each(c, &ob->ec->blocks, ob2, j)
677 drop |= ob2->ptr.dev == ca->dev_idx;
678 open_bucket_for_each(c, &ob->ec->parity, ob2, j)
679 drop |= ob2->ptr.dev == ca->dev_idx;
680 mutex_unlock(&ob->ec->lock);
684 bch2_open_bucket_put(c, ob);
686 ob_push(c, &ptrs, ob);
692 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
693 struct write_point *wp)
695 mutex_lock(&wp->lock);
696 bch2_open_buckets_stop_dev(c, ca, &wp->ptrs, wp->type);
697 mutex_unlock(&wp->lock);
700 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
701 unsigned long write_point)
704 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
706 return &c->write_points_hash[hash];
709 static struct write_point *__writepoint_find(struct hlist_head *head,
710 unsigned long write_point)
712 struct write_point *wp;
714 hlist_for_each_entry_rcu(wp, head, node)
715 if (wp->write_point == write_point)
721 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
723 u64 stranded = c->write_points_nr * c->bucket_size_max;
724 u64 free = bch2_fs_sectors_free(c);
726 return stranded * factor > free;
729 static bool try_increase_writepoints(struct bch_fs *c)
731 struct write_point *wp;
733 if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
734 too_many_writepoints(c, 32))
737 wp = c->write_points + c->write_points_nr++;
738 hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
742 static bool try_decrease_writepoints(struct bch_fs *c,
745 struct write_point *wp;
747 mutex_lock(&c->write_points_hash_lock);
748 if (c->write_points_nr < old_nr) {
749 mutex_unlock(&c->write_points_hash_lock);
753 if (c->write_points_nr == 1 ||
754 !too_many_writepoints(c, 8)) {
755 mutex_unlock(&c->write_points_hash_lock);
759 wp = c->write_points + --c->write_points_nr;
761 hlist_del_rcu(&wp->node);
762 mutex_unlock(&c->write_points_hash_lock);
764 bch2_writepoint_stop(c, NULL, wp);
768 static struct write_point *writepoint_find(struct bch_fs *c,
769 unsigned long write_point)
771 struct write_point *wp, *oldest;
772 struct hlist_head *head;
774 if (!(write_point & 1UL)) {
775 wp = (struct write_point *) write_point;
776 mutex_lock(&wp->lock);
780 head = writepoint_hash(c, write_point);
782 wp = __writepoint_find(head, write_point);
785 mutex_lock(&wp->lock);
786 if (wp->write_point == write_point)
788 mutex_unlock(&wp->lock);
793 for (wp = c->write_points;
794 wp < c->write_points + c->write_points_nr; wp++)
795 if (!oldest || time_before64(wp->last_used, oldest->last_used))
798 mutex_lock(&oldest->lock);
799 mutex_lock(&c->write_points_hash_lock);
800 if (oldest >= c->write_points + c->write_points_nr ||
801 try_increase_writepoints(c)) {
802 mutex_unlock(&c->write_points_hash_lock);
803 mutex_unlock(&oldest->lock);
804 goto restart_find_oldest;
807 wp = __writepoint_find(head, write_point);
808 if (wp && wp != oldest) {
809 mutex_unlock(&c->write_points_hash_lock);
810 mutex_unlock(&oldest->lock);
815 hlist_del_rcu(&wp->node);
816 wp->write_point = write_point;
817 hlist_add_head_rcu(&wp->node, head);
818 mutex_unlock(&c->write_points_hash_lock);
820 wp->last_used = sched_clock();
825 * Get us an open_bucket we can allocate from, return with it locked:
827 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
829 unsigned erasure_code,
830 struct write_point_specifier write_point,
831 struct bch_devs_list *devs_have,
832 unsigned nr_replicas,
833 unsigned nr_replicas_required,
834 enum alloc_reserve reserve,
838 struct write_point *wp;
839 struct open_bucket *ob;
840 unsigned nr_effective = 0;
841 struct open_buckets ptrs = { .nr = 0 };
842 bool have_cache = false;
843 unsigned write_points_nr;
846 BUG_ON(!nr_replicas || !nr_replicas_required);
848 write_points_nr = c->write_points_nr;
850 wp = writepoint_find(c, write_point.v);
852 /* metadata may not allocate on cache devices: */
853 if (wp->type != BCH_DATA_USER)
856 if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
857 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
858 target, erasure_code,
859 nr_replicas, &nr_effective,
860 &have_cache, reserve, cl);
862 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
863 target, erasure_code,
864 nr_replicas, &nr_effective,
865 &have_cache, reserve, NULL);
869 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
871 nr_replicas, &nr_effective,
872 &have_cache, reserve, cl);
875 BUG_ON(!ret && nr_effective < nr_replicas);
877 if (erasure_code && !ec_open_bucket(c, &ptrs))
878 pr_debug("failed to get ec bucket: ret %u", ret);
881 nr_effective >= nr_replicas_required)
887 /* Free buckets we didn't use: */
888 open_bucket_for_each(c, &wp->ptrs, ob, i)
889 open_bucket_free_unused(c, ob, wp->type == BCH_DATA_USER);
893 wp->sectors_free = UINT_MAX;
895 open_bucket_for_each(c, &wp->ptrs, ob, i)
896 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
898 BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
900 verify_not_stale(c, &wp->ptrs);
904 open_bucket_for_each(c, &wp->ptrs, ob, i)
905 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
906 ob_push(c, &ptrs, ob);
908 open_bucket_free_unused(c, ob,
909 wp->type == BCH_DATA_USER);
912 mutex_unlock(&wp->lock);
914 if (ret == -ENOSPC &&
915 try_decrease_writepoints(c, write_points_nr))
922 * Append pointers to the space we just allocated to @k, and mark @sectors space
923 * as allocated out of @ob
925 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
926 struct bkey_i *k, unsigned sectors)
929 struct open_bucket *ob;
932 BUG_ON(sectors > wp->sectors_free);
933 wp->sectors_free -= sectors;
935 open_bucket_for_each(c, &wp->ptrs, ob, i) {
936 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
937 struct bch_extent_ptr tmp = ob->ptr;
939 tmp.cached = !ca->mi.durability &&
940 wp->type == BCH_DATA_USER;
942 tmp.offset += ca->mi.bucket_size - ob->sectors_free;
943 bch2_bkey_append_ptr(k, tmp);
945 BUG_ON(sectors > ob->sectors_free);
946 ob->sectors_free -= sectors;
951 * Append pointers to the space we just allocated to @k, and mark @sectors space
952 * as allocated out of @ob
954 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
956 struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
957 struct open_bucket *ob;
960 open_bucket_for_each(c, &wp->ptrs, ob, i)
961 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
964 mutex_unlock(&wp->lock);
966 bch2_open_buckets_put(c, &ptrs);
969 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
971 struct open_bucket *ob;
972 struct write_point *wp;
974 mutex_init(&c->write_points_hash_lock);
975 c->write_points_nr = ARRAY_SIZE(c->write_points);
977 /* open bucket 0 is a sentinal NULL: */
978 spin_lock_init(&c->open_buckets[0].lock);
980 for (ob = c->open_buckets + 1;
981 ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
982 spin_lock_init(&ob->lock);
983 c->open_buckets_nr_free++;
985 ob->freelist = c->open_buckets_freelist;
986 c->open_buckets_freelist = ob - c->open_buckets;
989 writepoint_init(&c->btree_write_point, BCH_DATA_BTREE);
990 writepoint_init(&c->rebalance_write_point, BCH_DATA_USER);
992 for (wp = c->write_points;
993 wp < c->write_points + c->write_points_nr; wp++) {
994 writepoint_init(wp, BCH_DATA_USER);
996 wp->last_used = sched_clock();
997 wp->write_point = (unsigned long) wp;
998 hlist_add_head_rcu(&wp->node,
999 writepoint_hash(c, wp->write_point));