]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_foreground.c
Update bcachefs sources to ffe09df106 bcachefs: Verify fs hasn't been modified before...
[bcachefs-tools-debian] / libbcachefs / alloc_foreground.c
1 /*
2  * Primary bucket allocation code
3  *
4  * Copyright 2012 Google, Inc.
5  *
6  * Allocation in bcache is done in terms of buckets:
7  *
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.
10  *
11  * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
12  * bucket simply by incrementing its gen.
13  *
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.
17  *
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
21  * been overwritten.
22  *
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.
27  *
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.
31  *
32  * If we've got discards enabled, that happens when a bucket moves from the
33  * free_inc list to the free list.
34  *
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.
41  *
42  * bch2_bucket_alloc() allocates a single bucket from a specific device.
43  *
44  * bch2_bucket_alloc_set() allocates one or more buckets from different devices
45  * in a given filesystem.
46  *
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
49  * buckets are ready.
50  *
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.
54  */
55
56 #include "bcachefs.h"
57 #include "alloc_background.h"
58 #include "alloc_foreground.h"
59 #include "btree_gc.h"
60 #include "buckets.h"
61 #include "clock.h"
62 #include "debug.h"
63 #include "disk_groups.h"
64 #include "ec.h"
65 #include "io.h"
66
67 #include <linux/math64.h>
68 #include <linux/rculist.h>
69 #include <linux/rcupdate.h>
70 #include <trace/events/bcachefs.h>
71
72 enum bucket_alloc_ret {
73         ALLOC_SUCCESS,
74         OPEN_BUCKETS_EMPTY,
75         FREELIST_EMPTY,         /* Allocator thread not keeping up */
76 };
77
78 /*
79  * Open buckets represent a bucket that's currently being allocated from.  They
80  * serve two purposes:
81  *
82  *  - They track buckets that have been partially allocated, allowing for
83  *    sub-bucket sized allocations - they're used by the sector allocator below
84  *
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
87  *    btree
88  *
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.
92  */
93
94 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
95 {
96         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
97
98         if (ob->ec) {
99                 bch2_ec_bucket_written(c, ob);
100                 return;
101         }
102
103         percpu_down_read_preempt_disable(&c->mark_lock);
104         spin_lock(&ob->lock);
105
106         bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr),
107                                false, gc_pos_alloc(c, ob), 0);
108         ob->valid = false;
109         ob->type = 0;
110
111         spin_unlock(&ob->lock);
112         percpu_up_read_preempt_enable(&c->mark_lock);
113
114         spin_lock(&c->freelist_lock);
115         ob->freelist = c->open_buckets_freelist;
116         c->open_buckets_freelist = ob - c->open_buckets;
117         c->open_buckets_nr_free++;
118         spin_unlock(&c->freelist_lock);
119
120         closure_wake_up(&c->open_buckets_wait);
121 }
122
123 void bch2_open_bucket_write_error(struct bch_fs *c,
124                                   struct open_buckets *obs,
125                                   unsigned dev)
126 {
127         struct open_bucket *ob;
128         unsigned i;
129
130         open_bucket_for_each(c, obs, ob, i)
131                 if (ob->ptr.dev == dev &&
132                     ob->ec)
133                         bch2_ec_bucket_cancel(c, ob);
134 }
135
136 static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
137 {
138         struct open_bucket *ob;
139
140         BUG_ON(!c->open_buckets_freelist || !c->open_buckets_nr_free);
141
142         ob = c->open_buckets + c->open_buckets_freelist;
143         c->open_buckets_freelist = ob->freelist;
144         atomic_set(&ob->pin, 1);
145         ob->type = 0;
146
147         c->open_buckets_nr_free--;
148         return ob;
149 }
150
151 static void open_bucket_free_unused(struct bch_fs *c,
152                                     struct open_bucket *ob,
153                                     bool may_realloc)
154 {
155         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
156
157         BUG_ON(ca->open_buckets_partial_nr >=
158                ARRAY_SIZE(ca->open_buckets_partial));
159
160         if (ca->open_buckets_partial_nr <
161             ARRAY_SIZE(ca->open_buckets_partial) &&
162             may_realloc) {
163                 spin_lock(&c->freelist_lock);
164                 ob->on_partial_list = true;
165                 ca->open_buckets_partial[ca->open_buckets_partial_nr++] =
166                         ob - c->open_buckets;
167                 spin_unlock(&c->freelist_lock);
168
169                 closure_wake_up(&c->open_buckets_wait);
170                 closure_wake_up(&c->freelist_wait);
171         } else {
172                 bch2_open_bucket_put(c, ob);
173         }
174 }
175
176 static void verify_not_stale(struct bch_fs *c, const struct open_buckets *obs)
177 {
178 #ifdef CONFIG_BCACHEFS_DEBUG
179         struct open_bucket *ob;
180         unsigned i;
181
182         open_bucket_for_each(c, obs, ob, i) {
183                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
184
185                 BUG_ON(ptr_stale(ca, &ob->ptr));
186         }
187 #endif
188 }
189
190 /* _only_ for allocating the journal on a new device: */
191 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
192 {
193         struct bucket_array *buckets;
194         ssize_t b;
195
196         rcu_read_lock();
197         buckets = bucket_array(ca);
198
199         for (b = ca->mi.first_bucket; b < ca->mi.nbuckets; b++)
200                 if (is_available_bucket(buckets->b[b].mark))
201                         goto success;
202         b = -1;
203 success:
204         rcu_read_unlock();
205         return b;
206 }
207
208 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
209 {
210         switch (reserve) {
211         case RESERVE_ALLOC:
212                 return 0;
213         case RESERVE_BTREE:
214                 return BTREE_NODE_OPEN_BUCKET_RESERVE;
215         default:
216                 return BTREE_NODE_OPEN_BUCKET_RESERVE * 2;
217         }
218 }
219
220 /**
221  * bch_bucket_alloc - allocate a single bucket from a specific device
222  *
223  * Returns index of bucket on success, 0 on failure
224  * */
225 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
226                                       enum alloc_reserve reserve,
227                                       bool may_alloc_partial,
228                                       struct closure *cl)
229 {
230         struct bucket_array *buckets;
231         struct open_bucket *ob;
232         long bucket = 0;
233
234         spin_lock(&c->freelist_lock);
235
236         if (may_alloc_partial &&
237             ca->open_buckets_partial_nr) {
238                 ob = c->open_buckets +
239                         ca->open_buckets_partial[--ca->open_buckets_partial_nr];
240                 ob->on_partial_list = false;
241                 spin_unlock(&c->freelist_lock);
242                 return ob;
243         }
244
245         if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
246                 if (cl)
247                         closure_wait(&c->open_buckets_wait, cl);
248
249                 if (!c->blocked_allocate_open_bucket)
250                         c->blocked_allocate_open_bucket = local_clock();
251
252                 spin_unlock(&c->freelist_lock);
253                 trace_open_bucket_alloc_fail(ca, reserve);
254                 return ERR_PTR(-OPEN_BUCKETS_EMPTY);
255         }
256
257         if (likely(fifo_pop(&ca->free[RESERVE_NONE], bucket)))
258                 goto out;
259
260         switch (reserve) {
261         case RESERVE_ALLOC:
262                 if (fifo_pop(&ca->free[RESERVE_BTREE], bucket))
263                         goto out;
264                 break;
265         case RESERVE_BTREE:
266                 if (fifo_used(&ca->free[RESERVE_BTREE]) * 2 >=
267                     ca->free[RESERVE_BTREE].size &&
268                     fifo_pop(&ca->free[RESERVE_BTREE], bucket))
269                         goto out;
270                 break;
271         case RESERVE_MOVINGGC:
272                 if (fifo_pop(&ca->free[RESERVE_MOVINGGC], bucket))
273                         goto out;
274                 break;
275         default:
276                 break;
277         }
278
279         if (cl)
280                 closure_wait(&c->freelist_wait, cl);
281
282         if (!c->blocked_allocate)
283                 c->blocked_allocate = local_clock();
284
285         spin_unlock(&c->freelist_lock);
286
287         trace_bucket_alloc_fail(ca, reserve);
288         return ERR_PTR(-FREELIST_EMPTY);
289 out:
290         verify_not_on_freelist(c, ca, bucket);
291
292         ob = bch2_open_bucket_alloc(c);
293
294         spin_lock(&ob->lock);
295         buckets = bucket_array(ca);
296
297         ob->valid       = true;
298         ob->sectors_free = ca->mi.bucket_size;
299         ob->ptr         = (struct bch_extent_ptr) {
300                 .type   = 1 << BCH_EXTENT_ENTRY_ptr,
301                 .gen    = buckets->b[bucket].mark.gen,
302                 .offset = bucket_to_sector(ca, bucket),
303                 .dev    = ca->dev_idx,
304         };
305
306         bucket_io_clock_reset(c, ca, bucket, READ);
307         bucket_io_clock_reset(c, ca, bucket, WRITE);
308         spin_unlock(&ob->lock);
309
310         if (c->blocked_allocate_open_bucket) {
311                 bch2_time_stats_update(
312                         &c->times[BCH_TIME_blocked_allocate_open_bucket],
313                         c->blocked_allocate_open_bucket);
314                 c->blocked_allocate_open_bucket = 0;
315         }
316
317         if (c->blocked_allocate) {
318                 bch2_time_stats_update(
319                         &c->times[BCH_TIME_blocked_allocate],
320                         c->blocked_allocate);
321                 c->blocked_allocate = 0;
322         }
323
324         spin_unlock(&c->freelist_lock);
325
326         bch2_wake_allocator(ca);
327
328         trace_bucket_alloc(ca, reserve);
329         return ob;
330 }
331
332 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
333                             unsigned l, unsigned r)
334 {
335         return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
336                 (stripe->next_alloc[l] < stripe->next_alloc[r]));
337 }
338
339 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
340
341 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
342                                           struct dev_stripe_state *stripe,
343                                           struct bch_devs_mask *devs)
344 {
345         struct dev_alloc_list ret = { .nr = 0 };
346         struct bch_dev *ca;
347         unsigned i;
348
349         for_each_member_device_rcu(ca, c, i, devs)
350                 ret.devs[ret.nr++] = i;
351
352         bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
353         return ret;
354 }
355
356 void bch2_dev_stripe_increment(struct bch_fs *c, struct bch_dev *ca,
357                                struct dev_stripe_state *stripe)
358 {
359         u64 *v = stripe->next_alloc + ca->dev_idx;
360         u64 free_space = dev_buckets_free(c, ca);
361         u64 free_space_inv = free_space
362                 ? div64_u64(1ULL << 48, free_space)
363                 : 1ULL << 48;
364         u64 scale = *v / 4;
365
366         if (*v + free_space_inv >= *v)
367                 *v += free_space_inv;
368         else
369                 *v = U64_MAX;
370
371         for (v = stripe->next_alloc;
372              v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
373                 *v = *v < scale ? 0 : *v - scale;
374 }
375
376 #define BUCKET_MAY_ALLOC_PARTIAL        (1 << 0)
377 #define BUCKET_ALLOC_USE_DURABILITY     (1 << 1)
378
379 static int bch2_bucket_alloc_set(struct bch_fs *c,
380                                  struct open_buckets *ptrs,
381                                  struct dev_stripe_state *stripe,
382                                  struct bch_devs_mask *devs_may_alloc,
383                                  unsigned nr_replicas,
384                                  unsigned *nr_effective,
385                                  bool *have_cache,
386                                  enum alloc_reserve reserve,
387                                  unsigned flags,
388                                  struct closure *cl)
389 {
390         struct dev_alloc_list devs_sorted =
391                 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
392         struct bch_dev *ca;
393         bool alloc_failure = false;
394         unsigned i, durability;
395
396         BUG_ON(*nr_effective >= nr_replicas);
397
398         for (i = 0; i < devs_sorted.nr; i++) {
399                 struct open_bucket *ob;
400
401                 ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
402                 if (!ca)
403                         continue;
404
405                 if (!ca->mi.durability && *have_cache)
406                         continue;
407
408                 ob = bch2_bucket_alloc(c, ca, reserve,
409                                 flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
410                 if (IS_ERR(ob)) {
411                         enum bucket_alloc_ret ret = -PTR_ERR(ob);
412
413                         WARN_ON(reserve == RESERVE_MOVINGGC &&
414                                 ret != OPEN_BUCKETS_EMPTY);
415
416                         if (cl)
417                                 return -EAGAIN;
418                         if (ret == OPEN_BUCKETS_EMPTY)
419                                 return -ENOSPC;
420                         alloc_failure = true;
421                         continue;
422                 }
423
424                 durability = (flags & BUCKET_ALLOC_USE_DURABILITY)
425                         ? ca->mi.durability : 1;
426
427                 __clear_bit(ca->dev_idx, devs_may_alloc->d);
428                 *nr_effective   += durability;
429                 *have_cache     |= !durability;
430
431                 ob_push(c, ptrs, ob);
432
433                 bch2_dev_stripe_increment(c, ca, stripe);
434
435                 if (*nr_effective >= nr_replicas)
436                         return 0;
437         }
438
439         return alloc_failure ? -ENOSPC : -EROFS;
440 }
441
442 /* Allocate from stripes: */
443
444 /*
445  * XXX: use a higher watermark for allocating open buckets here:
446  */
447 static int ec_stripe_alloc(struct bch_fs *c, struct ec_stripe_head *h)
448 {
449         struct bch_devs_mask devs;
450         struct open_bucket *ob;
451         unsigned i, nr_have = 0, nr_data =
452                 min_t(unsigned, h->nr_active_devs,
453                       EC_STRIPE_MAX) - h->redundancy;
454         bool have_cache = true;
455         int ret = 0;
456
457         BUG_ON(h->blocks.nr > nr_data);
458         BUG_ON(h->parity.nr > h->redundancy);
459
460         devs = h->devs;
461
462         open_bucket_for_each(c, &h->parity, ob, i)
463                 __clear_bit(ob->ptr.dev, devs.d);
464         open_bucket_for_each(c, &h->blocks, ob, i)
465                 __clear_bit(ob->ptr.dev, devs.d);
466
467         percpu_down_read_preempt_disable(&c->mark_lock);
468         rcu_read_lock();
469
470         if (h->parity.nr < h->redundancy) {
471                 nr_have = h->parity.nr;
472
473                 ret = bch2_bucket_alloc_set(c, &h->parity,
474                                             &h->parity_stripe,
475                                             &devs,
476                                             h->redundancy,
477                                             &nr_have,
478                                             &have_cache,
479                                             RESERVE_NONE,
480                                             0,
481                                             NULL);
482                 if (ret)
483                         goto err;
484         }
485
486         if (h->blocks.nr < nr_data) {
487                 nr_have = h->blocks.nr;
488
489                 ret = bch2_bucket_alloc_set(c, &h->blocks,
490                                             &h->block_stripe,
491                                             &devs,
492                                             nr_data,
493                                             &nr_have,
494                                             &have_cache,
495                                             RESERVE_NONE,
496                                             0,
497                                             NULL);
498                 if (ret)
499                         goto err;
500         }
501
502         rcu_read_unlock();
503         percpu_up_read_preempt_enable(&c->mark_lock);
504
505         return bch2_ec_stripe_new_alloc(c, h);
506 err:
507         rcu_read_unlock();
508         percpu_up_read_preempt_enable(&c->mark_lock);
509         return -1;
510 }
511
512 /*
513  * if we can't allocate a new stripe because there are already too many
514  * partially filled stripes, force allocating from an existing stripe even when
515  * it's to a device we don't want:
516  */
517
518 static void bucket_alloc_from_stripe(struct bch_fs *c,
519                                      struct open_buckets *ptrs,
520                                      struct write_point *wp,
521                                      struct bch_devs_mask *devs_may_alloc,
522                                      u16 target,
523                                      unsigned erasure_code,
524                                      unsigned nr_replicas,
525                                      unsigned *nr_effective,
526                                      bool *have_cache)
527 {
528         struct dev_alloc_list devs_sorted;
529         struct ec_stripe_head *h;
530         struct open_bucket *ob;
531         struct bch_dev *ca;
532         unsigned i, ec_idx;
533
534         if (!erasure_code)
535                 return;
536
537         if (nr_replicas < 2)
538                 return;
539
540         if (ec_open_bucket(c, ptrs))
541                 return;
542
543         h = bch2_ec_stripe_head_get(c, target, erasure_code, nr_replicas - 1);
544         if (!h)
545                 return;
546
547         if (!h->s && ec_stripe_alloc(c, h))
548                 goto out_put_head;
549
550         rcu_read_lock();
551         devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
552         rcu_read_unlock();
553
554         for (i = 0; i < devs_sorted.nr; i++)
555                 open_bucket_for_each(c, &h->s->blocks, ob, ec_idx)
556                         if (ob->ptr.dev == devs_sorted.devs[i] &&
557                             !test_and_set_bit(ec_idx, h->s->blocks_allocated))
558                                 goto got_bucket;
559         goto out_put_head;
560 got_bucket:
561         ca = bch_dev_bkey_exists(c, ob->ptr.dev);
562
563         ob->ec_idx      = ec_idx;
564         ob->ec          = h->s;
565
566         __clear_bit(ob->ptr.dev, devs_may_alloc->d);
567         *nr_effective   += ca->mi.durability;
568         *have_cache     |= !ca->mi.durability;
569
570         ob_push(c, ptrs, ob);
571         atomic_inc(&h->s->pin);
572 out_put_head:
573         bch2_ec_stripe_head_put(h);
574 }
575
576 /* Sector allocator */
577
578 static void get_buckets_from_writepoint(struct bch_fs *c,
579                                         struct open_buckets *ptrs,
580                                         struct write_point *wp,
581                                         struct bch_devs_mask *devs_may_alloc,
582                                         unsigned nr_replicas,
583                                         unsigned *nr_effective,
584                                         bool *have_cache,
585                                         bool need_ec)
586 {
587         struct open_buckets ptrs_skip = { .nr = 0 };
588         struct open_bucket *ob;
589         unsigned i;
590
591         open_bucket_for_each(c, &wp->ptrs, ob, i) {
592                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
593
594                 if (*nr_effective < nr_replicas &&
595                     test_bit(ob->ptr.dev, devs_may_alloc->d) &&
596                     (ca->mi.durability ||
597                      (wp->type == BCH_DATA_USER && !*have_cache)) &&
598                     (ob->ec || !need_ec)) {
599                         __clear_bit(ob->ptr.dev, devs_may_alloc->d);
600                         *nr_effective   += ca->mi.durability;
601                         *have_cache     |= !ca->mi.durability;
602
603                         ob_push(c, ptrs, ob);
604                 } else {
605                         ob_push(c, &ptrs_skip, ob);
606                 }
607         }
608         wp->ptrs = ptrs_skip;
609 }
610
611 static int open_bucket_add_buckets(struct bch_fs *c,
612                                    struct open_buckets *ptrs,
613                                    struct write_point *wp,
614                                    struct bch_devs_list *devs_have,
615                                    u16 target,
616                                    unsigned erasure_code,
617                                    unsigned nr_replicas,
618                                    unsigned *nr_effective,
619                                    bool *have_cache,
620                                    enum alloc_reserve reserve,
621                                    struct closure *_cl)
622 {
623         struct bch_devs_mask devs;
624         struct open_bucket *ob;
625         struct closure *cl = NULL;
626         unsigned i, flags = BUCKET_ALLOC_USE_DURABILITY;
627         int ret;
628
629         if (wp->type == BCH_DATA_USER)
630                 flags |= BUCKET_MAY_ALLOC_PARTIAL;
631
632         rcu_read_lock();
633         devs = target_rw_devs(c, wp->type, target);
634         rcu_read_unlock();
635
636         /* Don't allocate from devices we already have pointers to: */
637         for (i = 0; i < devs_have->nr; i++)
638                 __clear_bit(devs_have->devs[i], devs.d);
639
640         open_bucket_for_each(c, ptrs, ob, i)
641                 __clear_bit(ob->ptr.dev, devs.d);
642
643         if (erasure_code) {
644                 get_buckets_from_writepoint(c, ptrs, wp, &devs,
645                                             nr_replicas, nr_effective,
646                                             have_cache, true);
647                 if (*nr_effective >= nr_replicas)
648                         return 0;
649
650                 bucket_alloc_from_stripe(c, ptrs, wp, &devs,
651                                          target, erasure_code,
652                                          nr_replicas, nr_effective,
653                                          have_cache);
654                 if (*nr_effective >= nr_replicas)
655                         return 0;
656         }
657
658         get_buckets_from_writepoint(c, ptrs, wp, &devs,
659                                     nr_replicas, nr_effective,
660                                     have_cache, false);
661         if (*nr_effective >= nr_replicas)
662                 return 0;
663
664         percpu_down_read_preempt_disable(&c->mark_lock);
665         rcu_read_lock();
666
667 retry_blocking:
668         /*
669          * Try nonblocking first, so that if one device is full we'll try from
670          * other devices:
671          */
672         ret = bch2_bucket_alloc_set(c, ptrs, &wp->stripe, &devs,
673                                 nr_replicas, nr_effective, have_cache,
674                                 reserve, flags, cl);
675         if (ret && ret != -EROFS && !cl && _cl) {
676                 cl = _cl;
677                 goto retry_blocking;
678         }
679
680         rcu_read_unlock();
681         percpu_up_read_preempt_enable(&c->mark_lock);
682
683         return ret;
684 }
685
686 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
687                                 struct open_buckets *obs,
688                                 enum bch_data_type data_type)
689 {
690         struct open_buckets ptrs = { .nr = 0 };
691         struct open_bucket *ob, *ob2;
692         unsigned i, j;
693
694         open_bucket_for_each(c, obs, ob, i) {
695                 bool drop = !ca || ob->ptr.dev == ca->dev_idx;
696
697                 if (!drop && ob->ec) {
698                         mutex_lock(&ob->ec->lock);
699                         open_bucket_for_each(c, &ob->ec->blocks, ob2, j)
700                                 drop |= ob2->ptr.dev == ca->dev_idx;
701                         open_bucket_for_each(c, &ob->ec->parity, ob2, j)
702                                 drop |= ob2->ptr.dev == ca->dev_idx;
703                         mutex_unlock(&ob->ec->lock);
704                 }
705
706                 if (drop)
707                         bch2_open_bucket_put(c, ob);
708                 else
709                         ob_push(c, &ptrs, ob);
710         }
711
712         *obs = ptrs;
713 }
714
715 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
716                           struct write_point *wp)
717 {
718         mutex_lock(&wp->lock);
719         bch2_open_buckets_stop_dev(c, ca, &wp->ptrs, wp->type);
720         mutex_unlock(&wp->lock);
721 }
722
723 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
724                                                  unsigned long write_point)
725 {
726         unsigned hash =
727                 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
728
729         return &c->write_points_hash[hash];
730 }
731
732 static struct write_point *__writepoint_find(struct hlist_head *head,
733                                              unsigned long write_point)
734 {
735         struct write_point *wp;
736
737         hlist_for_each_entry_rcu(wp, head, node)
738                 if (wp->write_point == write_point)
739                         return wp;
740
741         return NULL;
742 }
743
744 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
745 {
746         u64 stranded    = c->write_points_nr * c->bucket_size_max;
747         u64 free        = bch2_fs_usage_read_short(c).free;
748
749         return stranded * factor > free;
750 }
751
752 static bool try_increase_writepoints(struct bch_fs *c)
753 {
754         struct write_point *wp;
755
756         if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
757             too_many_writepoints(c, 32))
758                 return false;
759
760         wp = c->write_points + c->write_points_nr++;
761         hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
762         return true;
763 }
764
765 static bool try_decrease_writepoints(struct bch_fs *c,
766                                      unsigned old_nr)
767 {
768         struct write_point *wp;
769
770         mutex_lock(&c->write_points_hash_lock);
771         if (c->write_points_nr < old_nr) {
772                 mutex_unlock(&c->write_points_hash_lock);
773                 return true;
774         }
775
776         if (c->write_points_nr == 1 ||
777             !too_many_writepoints(c, 8)) {
778                 mutex_unlock(&c->write_points_hash_lock);
779                 return false;
780         }
781
782         wp = c->write_points + --c->write_points_nr;
783
784         hlist_del_rcu(&wp->node);
785         mutex_unlock(&c->write_points_hash_lock);
786
787         bch2_writepoint_stop(c, NULL, wp);
788         return true;
789 }
790
791 static struct write_point *writepoint_find(struct bch_fs *c,
792                                            unsigned long write_point)
793 {
794         struct write_point *wp, *oldest;
795         struct hlist_head *head;
796
797         if (!(write_point & 1UL)) {
798                 wp = (struct write_point *) write_point;
799                 mutex_lock(&wp->lock);
800                 return wp;
801         }
802
803         head = writepoint_hash(c, write_point);
804 restart_find:
805         wp = __writepoint_find(head, write_point);
806         if (wp) {
807 lock_wp:
808                 mutex_lock(&wp->lock);
809                 if (wp->write_point == write_point)
810                         goto out;
811                 mutex_unlock(&wp->lock);
812                 goto restart_find;
813         }
814 restart_find_oldest:
815         oldest = NULL;
816         for (wp = c->write_points;
817              wp < c->write_points + c->write_points_nr; wp++)
818                 if (!oldest || time_before64(wp->last_used, oldest->last_used))
819                         oldest = wp;
820
821         mutex_lock(&oldest->lock);
822         mutex_lock(&c->write_points_hash_lock);
823         if (oldest >= c->write_points + c->write_points_nr ||
824             try_increase_writepoints(c)) {
825                 mutex_unlock(&c->write_points_hash_lock);
826                 mutex_unlock(&oldest->lock);
827                 goto restart_find_oldest;
828         }
829
830         wp = __writepoint_find(head, write_point);
831         if (wp && wp != oldest) {
832                 mutex_unlock(&c->write_points_hash_lock);
833                 mutex_unlock(&oldest->lock);
834                 goto lock_wp;
835         }
836
837         wp = oldest;
838         hlist_del_rcu(&wp->node);
839         wp->write_point = write_point;
840         hlist_add_head_rcu(&wp->node, head);
841         mutex_unlock(&c->write_points_hash_lock);
842 out:
843         wp->last_used = sched_clock();
844         return wp;
845 }
846
847 /*
848  * Get us an open_bucket we can allocate from, return with it locked:
849  */
850 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
851                                 unsigned target,
852                                 unsigned erasure_code,
853                                 struct write_point_specifier write_point,
854                                 struct bch_devs_list *devs_have,
855                                 unsigned nr_replicas,
856                                 unsigned nr_replicas_required,
857                                 enum alloc_reserve reserve,
858                                 unsigned flags,
859                                 struct closure *cl)
860 {
861         struct write_point *wp;
862         struct open_bucket *ob;
863         struct open_buckets ptrs;
864         unsigned nr_effective, write_points_nr;
865         bool have_cache;
866         int ret, i;
867
868         BUG_ON(!nr_replicas || !nr_replicas_required);
869 retry:
870         ptrs.nr         = 0;
871         nr_effective    = 0;
872         write_points_nr = c->write_points_nr;
873         have_cache      = false;
874
875         wp = writepoint_find(c, write_point.v);
876
877         /* metadata may not allocate on cache devices: */
878         if (wp->type != BCH_DATA_USER)
879                 have_cache = true;
880
881         if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
882                 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
883                                               target, erasure_code,
884                                               nr_replicas, &nr_effective,
885                                               &have_cache, reserve, cl);
886         } else {
887                 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
888                                               target, erasure_code,
889                                               nr_replicas, &nr_effective,
890                                               &have_cache, reserve, NULL);
891                 if (!ret)
892                         goto alloc_done;
893
894                 ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
895                                               0, erasure_code,
896                                               nr_replicas, &nr_effective,
897                                               &have_cache, reserve, cl);
898         }
899 alloc_done:
900         BUG_ON(!ret && nr_effective < nr_replicas);
901
902         if (erasure_code && !ec_open_bucket(c, &ptrs))
903                 pr_debug("failed to get ec bucket: ret %u", ret);
904
905         if (ret == -EROFS &&
906             nr_effective >= nr_replicas_required)
907                 ret = 0;
908
909         if (ret)
910                 goto err;
911
912         /* Free buckets we didn't use: */
913         open_bucket_for_each(c, &wp->ptrs, ob, i)
914                 open_bucket_free_unused(c, ob, wp->type == BCH_DATA_USER);
915
916         wp->ptrs = ptrs;
917
918         wp->sectors_free = UINT_MAX;
919
920         open_bucket_for_each(c, &wp->ptrs, ob, i)
921                 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
922
923         BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
924
925         verify_not_stale(c, &wp->ptrs);
926
927         return wp;
928 err:
929         open_bucket_for_each(c, &wp->ptrs, ob, i)
930                 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
931                         ob_push(c, &ptrs, ob);
932                 else
933                         open_bucket_free_unused(c, ob,
934                                         wp->type == BCH_DATA_USER);
935         wp->ptrs = ptrs;
936
937         mutex_unlock(&wp->lock);
938
939         if (ret == -ENOSPC &&
940             try_decrease_writepoints(c, write_points_nr))
941                 goto retry;
942
943         return ERR_PTR(ret);
944 }
945
946 /*
947  * Append pointers to the space we just allocated to @k, and mark @sectors space
948  * as allocated out of @ob
949  */
950 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
951                                     struct bkey_i *k, unsigned sectors)
952
953 {
954         struct open_bucket *ob;
955         unsigned i;
956
957         BUG_ON(sectors > wp->sectors_free);
958         wp->sectors_free -= sectors;
959
960         open_bucket_for_each(c, &wp->ptrs, ob, i) {
961                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
962                 struct bch_extent_ptr tmp = ob->ptr;
963
964                 tmp.cached = !ca->mi.durability &&
965                         wp->type == BCH_DATA_USER;
966
967                 tmp.offset += ca->mi.bucket_size - ob->sectors_free;
968                 bch2_bkey_append_ptr(k, tmp);
969
970                 BUG_ON(sectors > ob->sectors_free);
971                 ob->sectors_free -= sectors;
972         }
973 }
974
975 /*
976  * Append pointers to the space we just allocated to @k, and mark @sectors space
977  * as allocated out of @ob
978  */
979 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
980 {
981         struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
982         struct open_bucket *ob;
983         unsigned i;
984
985         open_bucket_for_each(c, &wp->ptrs, ob, i)
986                 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
987         wp->ptrs = keep;
988
989         mutex_unlock(&wp->lock);
990
991         bch2_open_buckets_put(c, &ptrs);
992 }
993
994 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
995 {
996         struct open_bucket *ob;
997         struct write_point *wp;
998
999         mutex_init(&c->write_points_hash_lock);
1000         c->write_points_nr = ARRAY_SIZE(c->write_points);
1001
1002         /* open bucket 0 is a sentinal NULL: */
1003         spin_lock_init(&c->open_buckets[0].lock);
1004
1005         for (ob = c->open_buckets + 1;
1006              ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
1007                 spin_lock_init(&ob->lock);
1008                 c->open_buckets_nr_free++;
1009
1010                 ob->freelist = c->open_buckets_freelist;
1011                 c->open_buckets_freelist = ob - c->open_buckets;
1012         }
1013
1014         writepoint_init(&c->btree_write_point, BCH_DATA_BTREE);
1015         writepoint_init(&c->rebalance_write_point, BCH_DATA_USER);
1016
1017         for (wp = c->write_points;
1018              wp < c->write_points + c->write_points_nr; wp++) {
1019                 writepoint_init(wp, BCH_DATA_USER);
1020
1021                 wp->last_used   = sched_clock();
1022                 wp->write_point = (unsigned long) wp;
1023                 hlist_add_head_rcu(&wp->node,
1024                                    writepoint_hash(c, wp->write_point));
1025         }
1026 }