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->usage_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->usage_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 .gen = buckets->b[bucket].mark.gen,
292 .offset = bucket_to_sector(ca, bucket),
296 bucket_io_clock_reset(c, ca, bucket, READ);
297 bucket_io_clock_reset(c, ca, bucket, WRITE);
298 spin_unlock(&ob->lock);
300 spin_unlock(&c->freelist_lock);
302 bch2_wake_allocator(ca);
304 trace_bucket_alloc(ca, reserve);
308 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
309 unsigned l, unsigned r)
311 return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
312 (stripe->next_alloc[l] < stripe->next_alloc[r]));
315 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
317 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
318 struct dev_stripe_state *stripe,
319 struct bch_devs_mask *devs)
321 struct dev_alloc_list ret = { .nr = 0 };
325 for_each_member_device_rcu(ca, c, i, devs)
326 ret.devs[ret.nr++] = i;
328 bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
332 void bch2_dev_stripe_increment(struct bch_fs *c, struct bch_dev *ca,
333 struct dev_stripe_state *stripe)
335 u64 *v = stripe->next_alloc + ca->dev_idx;
336 u64 free_space = dev_buckets_free(c, ca);
337 u64 free_space_inv = free_space
338 ? div64_u64(1ULL << 48, free_space)
342 if (*v + free_space_inv >= *v)
343 *v += free_space_inv;
347 for (v = stripe->next_alloc;
348 v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
349 *v = *v < scale ? 0 : *v - scale;
352 #define BUCKET_MAY_ALLOC_PARTIAL (1 << 0)
353 #define BUCKET_ALLOC_USE_DURABILITY (1 << 1)
355 static int bch2_bucket_alloc_set(struct bch_fs *c,
356 struct open_buckets *ptrs,
357 struct dev_stripe_state *stripe,
358 struct bch_devs_mask *devs_may_alloc,
359 unsigned nr_replicas,
360 unsigned *nr_effective,
362 enum alloc_reserve reserve,
366 struct dev_alloc_list devs_sorted =
367 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
369 bool alloc_failure = false;
370 unsigned i, durability;
372 BUG_ON(*nr_effective >= nr_replicas);
374 for (i = 0; i < devs_sorted.nr; i++) {
375 struct open_bucket *ob;
377 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
381 if (!ca->mi.durability && *have_cache)
384 ob = bch2_bucket_alloc(c, ca, reserve,
385 flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
387 enum bucket_alloc_ret ret = -PTR_ERR(ob);
389 WARN_ON(reserve == RESERVE_MOVINGGC &&
390 ret != OPEN_BUCKETS_EMPTY);
394 if (ret == OPEN_BUCKETS_EMPTY)
396 alloc_failure = true;
400 durability = (flags & BUCKET_ALLOC_USE_DURABILITY)
401 ? ca->mi.durability : 1;
403 __clear_bit(ca->dev_idx, devs_may_alloc->d);
404 *nr_effective += durability;
405 *have_cache |= !durability;
407 ob_push(c, ptrs, ob);
409 bch2_dev_stripe_increment(c, ca, stripe);
411 if (*nr_effective >= nr_replicas)
415 return alloc_failure ? -ENOSPC : -EROFS;
418 /* Allocate from stripes: */
421 * XXX: use a higher watermark for allocating open buckets here:
423 static int ec_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
425 struct bch_devs_mask devs;
426 struct open_bucket *ob;
427 unsigned i, nr_have = 0, nr_data =
428 min_t(unsigned, h->nr_active_devs,
429 EC_STRIPE_MAX) - h->redundancy;
430 bool have_cache = true;
433 BUG_ON(h->blocks.nr > nr_data);
434 BUG_ON(h->parity.nr > h->redundancy);
438 open_bucket_for_each(c, &h->parity, ob, i)
439 __clear_bit(ob->ptr.dev, devs.d);
440 open_bucket_for_each(c, &h->blocks, ob, i)
441 __clear_bit(ob->ptr.dev, devs.d);
443 percpu_down_read_preempt_disable(&c->usage_lock);
446 if (h->parity.nr < h->redundancy) {
447 nr_have = h->parity.nr;
449 ret = bch2_bucket_alloc_set(c, &h->parity,
462 if (h->blocks.nr < nr_data) {
463 nr_have = h->blocks.nr;
465 ret = bch2_bucket_alloc_set(c, &h->blocks,
479 percpu_up_read_preempt_enable(&c->usage_lock);
481 return bch2_ec_stripe_new_alloc(c, h);
484 percpu_up_read_preempt_enable(&c->usage_lock);
489 * if we can't allocate a new stripe because there are already too many
490 * partially filled stripes, force allocating from an existing stripe even when
491 * it's to a device we don't want:
494 static void bucket_alloc_from_stripe(struct bch_fs *c,
495 struct open_buckets *ptrs,
496 struct write_point *wp,
497 struct bch_devs_mask *devs_may_alloc,
499 unsigned erasure_code,
500 unsigned nr_replicas,
501 unsigned *nr_effective,
504 struct dev_alloc_list devs_sorted;
505 struct ec_stripe_head *h;
506 struct open_bucket *ob;
516 if (ec_open_bucket(c, ptrs))
519 h = bch2_ec_stripe_head_get(c, target, erasure_code, nr_replicas - 1);
523 if (!h->s && ec_stripe_alloc(c, h))
527 devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
530 for (i = 0; i < devs_sorted.nr; i++)
531 open_bucket_for_each(c, &h->s->blocks, ob, ec_idx)
532 if (ob->ptr.dev == devs_sorted.devs[i] &&
533 !test_and_set_bit(ec_idx, h->s->blocks_allocated))
537 ca = bch_dev_bkey_exists(c, ob->ptr.dev);
542 __clear_bit(ob->ptr.dev, devs_may_alloc->d);
543 *nr_effective += ca->mi.durability;
544 *have_cache |= !ca->mi.durability;
546 ob_push(c, ptrs, ob);
547 atomic_inc(&h->s->pin);
549 bch2_ec_stripe_head_put(h);
552 /* Sector allocator */
554 static void get_buckets_from_writepoint(struct bch_fs *c,
555 struct open_buckets *ptrs,
556 struct write_point *wp,
557 struct bch_devs_mask *devs_may_alloc,
558 unsigned nr_replicas,
559 unsigned *nr_effective,
563 struct open_buckets ptrs_skip = { .nr = 0 };
564 struct open_bucket *ob;
567 open_bucket_for_each(c, &wp->ptrs, ob, i) {
568 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
570 if (*nr_effective < nr_replicas &&
571 test_bit(ob->ptr.dev, devs_may_alloc->d) &&
572 (ca->mi.durability ||
573 (wp->type == BCH_DATA_USER && !*have_cache)) &&
574 (ob->ec || !need_ec)) {
575 __clear_bit(ob->ptr.dev, devs_may_alloc->d);
576 *nr_effective += ca->mi.durability;
577 *have_cache |= !ca->mi.durability;
579 ob_push(c, ptrs, ob);
581 ob_push(c, &ptrs_skip, ob);
584 wp->ptrs = ptrs_skip;
587 static int open_bucket_add_buckets(struct bch_fs *c,
588 struct open_buckets *ptrs,
589 struct write_point *wp,
590 struct bch_devs_list *devs_have,
592 unsigned erasure_code,
593 unsigned nr_replicas,
594 unsigned *nr_effective,
596 enum alloc_reserve reserve,
599 struct bch_devs_mask devs;
600 struct open_bucket *ob;
601 struct closure *cl = NULL;
602 unsigned i, flags = BUCKET_ALLOC_USE_DURABILITY;
605 if (wp->type == BCH_DATA_USER)
606 flags |= BUCKET_MAY_ALLOC_PARTIAL;
609 devs = target_rw_devs(c, wp->type, target);
612 /* Don't allocate from devices we already have pointers to: */
613 for (i = 0; i < devs_have->nr; i++)
614 __clear_bit(devs_have->devs[i], devs.d);
616 open_bucket_for_each(c, ptrs, ob, i)
617 __clear_bit(ob->ptr.dev, devs.d);
620 get_buckets_from_writepoint(c, ptrs, wp, &devs,
621 nr_replicas, nr_effective,
623 if (*nr_effective >= nr_replicas)
626 bucket_alloc_from_stripe(c, ptrs, wp, &devs,
627 target, erasure_code,
628 nr_replicas, nr_effective,
630 if (*nr_effective >= nr_replicas)
634 get_buckets_from_writepoint(c, ptrs, wp, &devs,
635 nr_replicas, nr_effective,
637 if (*nr_effective >= nr_replicas)
640 percpu_down_read_preempt_disable(&c->usage_lock);
645 * Try nonblocking first, so that if one device is full we'll try from
648 ret = bch2_bucket_alloc_set(c, ptrs, &wp->stripe, &devs,
649 nr_replicas, nr_effective, have_cache,
651 if (ret && ret != -EROFS && !cl && _cl) {
657 percpu_up_read_preempt_enable(&c->usage_lock);
662 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
663 struct open_buckets *obs,
664 enum bch_data_type data_type)
666 struct open_buckets ptrs = { .nr = 0 };
667 struct open_bucket *ob, *ob2;
670 open_bucket_for_each(c, obs, ob, i) {
671 bool drop = !ca || ob->ptr.dev == ca->dev_idx;
673 if (!drop && ob->ec) {
674 mutex_lock(&ob->ec->lock);
675 open_bucket_for_each(c, &ob->ec->blocks, ob2, j)
676 drop |= ob2->ptr.dev == ca->dev_idx;
677 open_bucket_for_each(c, &ob->ec->parity, ob2, j)
678 drop |= ob2->ptr.dev == ca->dev_idx;
679 mutex_unlock(&ob->ec->lock);
683 bch2_open_bucket_put(c, ob);
685 ob_push(c, &ptrs, ob);
691 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
692 struct write_point *wp)
694 mutex_lock(&wp->lock);
695 bch2_open_buckets_stop_dev(c, ca, &wp->ptrs, wp->type);
696 mutex_unlock(&wp->lock);
699 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
700 unsigned long write_point)
703 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
705 return &c->write_points_hash[hash];
708 static struct write_point *__writepoint_find(struct hlist_head *head,
709 unsigned long write_point)
711 struct write_point *wp;
713 hlist_for_each_entry_rcu(wp, head, node)
714 if (wp->write_point == write_point)
720 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
722 u64 stranded = c->write_points_nr * c->bucket_size_max;
723 u64 free = bch2_fs_sectors_free(c, bch2_fs_usage_read(c));
725 return stranded * factor > free;
728 static bool try_increase_writepoints(struct bch_fs *c)
730 struct write_point *wp;
732 if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
733 too_many_writepoints(c, 32))
736 wp = c->write_points + c->write_points_nr++;
737 hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
741 static bool try_decrease_writepoints(struct bch_fs *c,
744 struct write_point *wp;
746 mutex_lock(&c->write_points_hash_lock);
747 if (c->write_points_nr < old_nr) {
748 mutex_unlock(&c->write_points_hash_lock);
752 if (c->write_points_nr == 1 ||
753 !too_many_writepoints(c, 8)) {
754 mutex_unlock(&c->write_points_hash_lock);
758 wp = c->write_points + --c->write_points_nr;
760 hlist_del_rcu(&wp->node);
761 mutex_unlock(&c->write_points_hash_lock);
763 bch2_writepoint_stop(c, NULL, wp);
767 static struct write_point *writepoint_find(struct bch_fs *c,
768 unsigned long write_point)
770 struct write_point *wp, *oldest;
771 struct hlist_head *head;
773 if (!(write_point & 1UL)) {
774 wp = (struct write_point *) write_point;
775 mutex_lock(&wp->lock);
779 head = writepoint_hash(c, write_point);
781 wp = __writepoint_find(head, write_point);
784 mutex_lock(&wp->lock);
785 if (wp->write_point == write_point)
787 mutex_unlock(&wp->lock);
792 for (wp = c->write_points;
793 wp < c->write_points + c->write_points_nr; wp++)
794 if (!oldest || time_before64(wp->last_used, oldest->last_used))
797 mutex_lock(&oldest->lock);
798 mutex_lock(&c->write_points_hash_lock);
799 if (oldest >= c->write_points + c->write_points_nr ||
800 try_increase_writepoints(c)) {
801 mutex_unlock(&c->write_points_hash_lock);
802 mutex_unlock(&oldest->lock);
803 goto restart_find_oldest;
806 wp = __writepoint_find(head, write_point);
807 if (wp && wp != oldest) {
808 mutex_unlock(&c->write_points_hash_lock);
809 mutex_unlock(&oldest->lock);
814 hlist_del_rcu(&wp->node);
815 wp->write_point = write_point;
816 hlist_add_head_rcu(&wp->node, head);
817 mutex_unlock(&c->write_points_hash_lock);
819 wp->last_used = sched_clock();
824 * Get us an open_bucket we can allocate from, return with it locked:
826 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
828 unsigned erasure_code,
829 struct write_point_specifier write_point,
830 struct bch_devs_list *devs_have,
831 unsigned nr_replicas,
832 unsigned nr_replicas_required,
833 enum alloc_reserve reserve,
837 struct write_point *wp;
838 struct open_bucket *ob;
839 unsigned nr_effective = 0;
840 struct open_buckets ptrs = { .nr = 0 };
841 bool have_cache = false;
842 unsigned write_points_nr;
845 BUG_ON(!nr_replicas || !nr_replicas_required);
847 write_points_nr = c->write_points_nr;
849 wp = writepoint_find(c, write_point.v);
851 /* metadata may not allocate on cache devices: */
852 if (wp->type != BCH_DATA_USER)
855 if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
856 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
857 target, erasure_code,
858 nr_replicas, &nr_effective,
859 &have_cache, reserve, cl);
861 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
862 target, erasure_code,
863 nr_replicas, &nr_effective,
864 &have_cache, reserve, NULL);
868 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
870 nr_replicas, &nr_effective,
871 &have_cache, reserve, cl);
874 BUG_ON(!ret && nr_effective < nr_replicas);
876 if (erasure_code && !ec_open_bucket(c, &ptrs))
877 pr_debug("failed to get ec bucket: ret %u", ret);
880 nr_effective >= nr_replicas_required)
886 /* Free buckets we didn't use: */
887 open_bucket_for_each(c, &wp->ptrs, ob, i)
888 open_bucket_free_unused(c, ob, wp->type == BCH_DATA_USER);
892 wp->sectors_free = UINT_MAX;
894 open_bucket_for_each(c, &wp->ptrs, ob, i)
895 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
897 BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
899 verify_not_stale(c, &wp->ptrs);
903 open_bucket_for_each(c, &wp->ptrs, ob, i)
904 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
905 ob_push(c, &ptrs, ob);
907 open_bucket_free_unused(c, ob,
908 wp->type == BCH_DATA_USER);
911 mutex_unlock(&wp->lock);
913 if (ret == -ENOSPC &&
914 try_decrease_writepoints(c, write_points_nr))
921 * Append pointers to the space we just allocated to @k, and mark @sectors space
922 * as allocated out of @ob
924 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
925 struct bkey_i_extent *e, unsigned sectors)
927 struct open_bucket *ob;
930 BUG_ON(sectors > wp->sectors_free);
931 wp->sectors_free -= sectors;
933 open_bucket_for_each(c, &wp->ptrs, ob, i) {
934 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
935 struct bch_extent_ptr tmp = ob->ptr;
937 EBUG_ON(bch2_extent_has_device(extent_i_to_s_c(e), ob->ptr.dev));
939 tmp.cached = bkey_extent_is_cached(&e->k) ||
940 (!ca->mi.durability && wp->type == BCH_DATA_USER);
942 tmp.offset += ca->mi.bucket_size - ob->sectors_free;
943 extent_ptr_append(e, 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));