]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_foreground.c
Update bcachefs sources to cd779e0cc5 bcachefs: Skip inode unpack/pack in bch2_extent...
[bcachefs-tools-debian] / libbcachefs / alloc_foreground.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2012 Google, Inc.
4  *
5  * Foreground allocator code: allocate buckets from freelist, and allocate in
6  * sector granularity from writepoints.
7  *
8  * bch2_bucket_alloc() allocates a single bucket from a specific device.
9  *
10  * bch2_bucket_alloc_set() allocates one or more buckets from different devices
11  * in a given filesystem.
12  */
13
14 #include "bcachefs.h"
15 #include "alloc_background.h"
16 #include "alloc_foreground.h"
17 #include "btree_iter.h"
18 #include "btree_update.h"
19 #include "btree_gc.h"
20 #include "buckets.h"
21 #include "buckets_waiting_for_journal.h"
22 #include "clock.h"
23 #include "debug.h"
24 #include "disk_groups.h"
25 #include "ec.h"
26 #include "error.h"
27 #include "io.h"
28 #include "journal.h"
29 #include "movinggc.h"
30
31 #include <linux/math64.h>
32 #include <linux/rculist.h>
33 #include <linux/rcupdate.h>
34 #include <trace/events/bcachefs.h>
35
36 const char * const bch2_alloc_reserves[] = {
37 #define x(t) #t,
38         BCH_ALLOC_RESERVES()
39 #undef x
40         NULL
41 };
42
43 /*
44  * Open buckets represent a bucket that's currently being allocated from.  They
45  * serve two purposes:
46  *
47  *  - They track buckets that have been partially allocated, allowing for
48  *    sub-bucket sized allocations - they're used by the sector allocator below
49  *
50  *  - They provide a reference to the buckets they own that mark and sweep GC
51  *    can find, until the new allocation has a pointer to it inserted into the
52  *    btree
53  *
54  * When allocating some space with the sector allocator, the allocation comes
55  * with a reference to an open bucket - the caller is required to put that
56  * reference _after_ doing the index update that makes its allocation reachable.
57  */
58
59 static void bch2_open_bucket_hash_add(struct bch_fs *c, struct open_bucket *ob)
60 {
61         open_bucket_idx_t idx = ob - c->open_buckets;
62         open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
63
64         ob->hash = *slot;
65         *slot = idx;
66 }
67
68 static void bch2_open_bucket_hash_remove(struct bch_fs *c, struct open_bucket *ob)
69 {
70         open_bucket_idx_t idx = ob - c->open_buckets;
71         open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
72
73         while (*slot != idx) {
74                 BUG_ON(!*slot);
75                 slot = &c->open_buckets[*slot].hash;
76         }
77
78         *slot = ob->hash;
79         ob->hash = 0;
80 }
81
82 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
83 {
84         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
85
86         if (ob->ec) {
87                 bch2_ec_bucket_written(c, ob);
88                 return;
89         }
90
91         percpu_down_read(&c->mark_lock);
92         spin_lock(&ob->lock);
93
94         ob->valid = false;
95         ob->data_type = 0;
96
97         spin_unlock(&ob->lock);
98         percpu_up_read(&c->mark_lock);
99
100         spin_lock(&c->freelist_lock);
101         bch2_open_bucket_hash_remove(c, ob);
102
103         ob->freelist = c->open_buckets_freelist;
104         c->open_buckets_freelist = ob - c->open_buckets;
105
106         c->open_buckets_nr_free++;
107         ca->nr_open_buckets--;
108         spin_unlock(&c->freelist_lock);
109
110         closure_wake_up(&c->open_buckets_wait);
111 }
112
113 void bch2_open_bucket_write_error(struct bch_fs *c,
114                                   struct open_buckets *obs,
115                                   unsigned dev)
116 {
117         struct open_bucket *ob;
118         unsigned i;
119
120         open_bucket_for_each(c, obs, ob, i)
121                 if (ob->dev == dev && ob->ec)
122                         bch2_ec_bucket_cancel(c, ob);
123 }
124
125 static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
126 {
127         struct open_bucket *ob;
128
129         BUG_ON(!c->open_buckets_freelist || !c->open_buckets_nr_free);
130
131         ob = c->open_buckets + c->open_buckets_freelist;
132         c->open_buckets_freelist = ob->freelist;
133         atomic_set(&ob->pin, 1);
134         ob->data_type = 0;
135
136         c->open_buckets_nr_free--;
137         return ob;
138 }
139
140 static void open_bucket_free_unused(struct bch_fs *c,
141                                     struct write_point *wp,
142                                     struct open_bucket *ob)
143 {
144         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
145         bool may_realloc = wp->data_type == BCH_DATA_user;
146
147         BUG_ON(ca->open_buckets_partial_nr >
148                ARRAY_SIZE(ca->open_buckets_partial));
149
150         if (ca->open_buckets_partial_nr <
151             ARRAY_SIZE(ca->open_buckets_partial) &&
152             may_realloc) {
153                 spin_lock(&c->freelist_lock);
154                 ob->on_partial_list = true;
155                 ca->open_buckets_partial[ca->open_buckets_partial_nr++] =
156                         ob - c->open_buckets;
157                 spin_unlock(&c->freelist_lock);
158
159                 closure_wake_up(&c->open_buckets_wait);
160                 closure_wake_up(&c->freelist_wait);
161         } else {
162                 bch2_open_bucket_put(c, ob);
163         }
164 }
165
166 /* _only_ for allocating the journal on a new device: */
167 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
168 {
169         while (ca->new_fs_bucket_idx < ca->mi.nbuckets) {
170                 u64 b = ca->new_fs_bucket_idx++;
171
172                 if (!is_superblock_bucket(ca, b) &&
173                     (!ca->buckets_nouse || !test_bit(b, ca->buckets_nouse)))
174                         return b;
175         }
176
177         return -1;
178 }
179
180 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
181 {
182         switch (reserve) {
183         case RESERVE_btree:
184         case RESERVE_btree_movinggc:
185                 return 0;
186         case RESERVE_movinggc:
187                 return OPEN_BUCKETS_COUNT / 4;
188         default:
189                 return OPEN_BUCKETS_COUNT / 2;
190         }
191 }
192
193 static struct open_bucket *__try_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
194                                               u64 bucket,
195                                               enum alloc_reserve reserve,
196                                               struct bch_alloc_v4 *a,
197                                               u64 *skipped_open,
198                                               u64 *skipped_need_journal_commit,
199                                               u64 *skipped_nouse,
200                                               struct closure *cl)
201 {
202         struct open_bucket *ob;
203
204         if (unlikely(ca->buckets_nouse && test_bit(bucket, ca->buckets_nouse))) {
205                 (*skipped_nouse)++;
206                 return NULL;
207         }
208
209         if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
210                 (*skipped_open)++;
211                 return NULL;
212         }
213
214         if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
215                         c->journal.flushed_seq_ondisk, ca->dev_idx, bucket)) {
216                 (*skipped_need_journal_commit)++;
217                 return NULL;
218         }
219
220         spin_lock(&c->freelist_lock);
221
222         if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
223                 if (cl)
224                         closure_wait(&c->open_buckets_wait, cl);
225
226                 if (!c->blocked_allocate_open_bucket)
227                         c->blocked_allocate_open_bucket = local_clock();
228
229                 spin_unlock(&c->freelist_lock);
230                 return ERR_PTR(-BCH_ERR_open_buckets_empty);
231         }
232
233         /* Recheck under lock: */
234         if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
235                 spin_unlock(&c->freelist_lock);
236                 (*skipped_open)++;
237                 return NULL;
238         }
239
240         ob = bch2_open_bucket_alloc(c);
241
242         spin_lock(&ob->lock);
243
244         ob->valid       = true;
245         ob->sectors_free = ca->mi.bucket_size;
246         ob->alloc_reserve = reserve;
247         ob->dev         = ca->dev_idx;
248         ob->gen         = a->gen;
249         ob->bucket      = bucket;
250         spin_unlock(&ob->lock);
251
252         ca->nr_open_buckets++;
253         bch2_open_bucket_hash_add(c, ob);
254
255         if (c->blocked_allocate_open_bucket) {
256                 bch2_time_stats_update(
257                         &c->times[BCH_TIME_blocked_allocate_open_bucket],
258                         c->blocked_allocate_open_bucket);
259                 c->blocked_allocate_open_bucket = 0;
260         }
261
262         if (c->blocked_allocate) {
263                 bch2_time_stats_update(
264                         &c->times[BCH_TIME_blocked_allocate],
265                         c->blocked_allocate);
266                 c->blocked_allocate = 0;
267         }
268
269         spin_unlock(&c->freelist_lock);
270
271         return ob;
272 }
273
274 static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bch_dev *ca,
275                                             enum alloc_reserve reserve, u64 free_entry,
276                                             u64 *skipped_open,
277                                             u64 *skipped_need_journal_commit,
278                                             u64 *skipped_nouse,
279                                             struct bkey_s_c freespace_k,
280                                             struct closure *cl)
281 {
282         struct bch_fs *c = trans->c;
283         struct btree_iter iter = { NULL };
284         struct bkey_s_c k;
285         struct open_bucket *ob;
286         struct bch_alloc_v4 a;
287         u64 b = free_entry & ~(~0ULL << 56);
288         unsigned genbits = free_entry >> 56;
289         struct printbuf buf = PRINTBUF;
290         int ret;
291
292         if (b < ca->mi.first_bucket || b >= ca->mi.nbuckets) {
293                 prt_printf(&buf, "freespace btree has bucket outside allowed range %u-%llu\n"
294                        "  freespace key ",
295                         ca->mi.first_bucket, ca->mi.nbuckets);
296                 bch2_bkey_val_to_text(&buf, c, freespace_k);
297                 bch2_trans_inconsistent(trans, "%s", buf.buf);
298                 ob = ERR_PTR(-EIO);
299                 goto err;
300         }
301
302         bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(ca->dev_idx, b), BTREE_ITER_CACHED);
303         k = bch2_btree_iter_peek_slot(&iter);
304         ret = bkey_err(k);
305         if (ret) {
306                 ob = ERR_PTR(ret);
307                 goto err;
308         }
309
310         bch2_alloc_to_v4(k, &a);
311
312         if (genbits != (alloc_freespace_genbits(a) >> 56)) {
313                 prt_printf(&buf, "bucket in freespace btree with wrong genbits (got %u should be %llu)\n"
314                        "  freespace key ",
315                        genbits, alloc_freespace_genbits(a) >> 56);
316                 bch2_bkey_val_to_text(&buf, c, freespace_k);
317                 prt_printf(&buf, "\n  ");
318                 bch2_bkey_val_to_text(&buf, c, k);
319                 bch2_trans_inconsistent(trans, "%s", buf.buf);
320                 ob = ERR_PTR(-EIO);
321                 goto err;
322
323         }
324
325         if (a.data_type != BCH_DATA_free) {
326                 prt_printf(&buf, "non free bucket in freespace btree\n"
327                        "  freespace key ");
328                 bch2_bkey_val_to_text(&buf, c, freespace_k);
329                 prt_printf(&buf, "\n  ");
330                 bch2_bkey_val_to_text(&buf, c, k);
331                 bch2_trans_inconsistent(trans, "%s", buf.buf);
332                 ob = ERR_PTR(-EIO);
333                 goto err;
334         }
335
336         ob = __try_alloc_bucket(c, ca, b, reserve, &a,
337                                 skipped_open,
338                                 skipped_need_journal_commit,
339                                 skipped_nouse,
340                                 cl);
341         if (!ob)
342                 iter.path->preserve = false;
343 err:
344         set_btree_iter_dontneed(&iter);
345         bch2_trans_iter_exit(trans, &iter);
346         printbuf_exit(&buf);
347         return ob;
348 }
349
350 static struct open_bucket *try_alloc_partial_bucket(struct bch_fs *c, struct bch_dev *ca,
351                                                     enum alloc_reserve reserve)
352 {
353         struct open_bucket *ob;
354         int i;
355
356         spin_lock(&c->freelist_lock);
357
358         for (i = ca->open_buckets_partial_nr - 1; i >= 0; --i) {
359                 ob = c->open_buckets + ca->open_buckets_partial[i];
360
361                 if (reserve <= ob->alloc_reserve) {
362                         array_remove_item(ca->open_buckets_partial,
363                                           ca->open_buckets_partial_nr,
364                                           i);
365                         ob->on_partial_list = false;
366                         ob->alloc_reserve = reserve;
367                         spin_unlock(&c->freelist_lock);
368                         return ob;
369                 }
370         }
371
372         spin_unlock(&c->freelist_lock);
373         return NULL;
374 }
375
376 /*
377  * This path is for before the freespace btree is initialized:
378  *
379  * If ca->new_fs_bucket_idx is nonzero, we haven't yet marked superblock &
380  * journal buckets - journal buckets will be < ca->new_fs_bucket_idx
381  */
382 static noinline struct open_bucket *
383 bch2_bucket_alloc_early(struct btree_trans *trans,
384                         struct bch_dev *ca,
385                         enum alloc_reserve reserve,
386                         u64 *cur_bucket,
387                         u64 *buckets_seen,
388                         u64 *skipped_open,
389                         u64 *skipped_need_journal_commit,
390                         u64 *skipped_nouse,
391                         struct closure *cl)
392 {
393         struct btree_iter iter;
394         struct bkey_s_c k;
395         struct open_bucket *ob = NULL;
396         int ret;
397
398         *cur_bucket = max_t(u64, *cur_bucket, ca->mi.first_bucket);
399         *cur_bucket = max_t(u64, *cur_bucket, ca->new_fs_bucket_idx);
400
401         for_each_btree_key_norestart(trans, iter, BTREE_ID_alloc, POS(ca->dev_idx, *cur_bucket),
402                            BTREE_ITER_SLOTS, k, ret) {
403                 struct bch_alloc_v4 a;
404
405                 if (bkey_cmp(k.k->p, POS(ca->dev_idx, ca->mi.nbuckets)) >= 0)
406                         break;
407
408                 if (ca->new_fs_bucket_idx &&
409                     is_superblock_bucket(ca, k.k->p.offset))
410                         continue;
411
412                 bch2_alloc_to_v4(k, &a);
413
414                 if (a.data_type != BCH_DATA_free)
415                         continue;
416
417                 (*buckets_seen)++;
418
419                 ob = __try_alloc_bucket(trans->c, ca, k.k->p.offset, reserve, &a,
420                                         skipped_open,
421                                         skipped_need_journal_commit,
422                                         skipped_nouse,
423                                         cl);
424                 if (ob)
425                         break;
426         }
427         bch2_trans_iter_exit(trans, &iter);
428
429         *cur_bucket = iter.pos.offset;
430
431         return ob ?: ERR_PTR(ret ?: -BCH_ERR_no_buckets_found);
432 }
433
434 static struct open_bucket *bch2_bucket_alloc_freelist(struct btree_trans *trans,
435                                                    struct bch_dev *ca,
436                                                    enum alloc_reserve reserve,
437                                                    u64 *cur_bucket,
438                                                    u64 *buckets_seen,
439                                                    u64 *skipped_open,
440                                                    u64 *skipped_need_journal_commit,
441                                                    u64 *skipped_nouse,
442                                                    struct closure *cl)
443 {
444         struct btree_iter iter;
445         struct bkey_s_c k;
446         struct open_bucket *ob = NULL;
447         int ret;
448
449         BUG_ON(ca->new_fs_bucket_idx);
450
451         /*
452          * XXX:
453          * On transaction restart, we'd like to restart from the bucket we were
454          * at previously
455          */
456         for_each_btree_key_norestart(trans, iter, BTREE_ID_freespace,
457                                      POS(ca->dev_idx, *cur_bucket), 0, k, ret) {
458                 if (k.k->p.inode != ca->dev_idx)
459                         break;
460
461                 for (*cur_bucket = max(*cur_bucket, bkey_start_offset(k.k));
462                      *cur_bucket < k.k->p.offset;
463                      (*cur_bucket)++) {
464                         ret = btree_trans_too_many_iters(trans);
465                         if (ret)
466                                 break;
467
468                         (*buckets_seen)++;
469
470                         ob = try_alloc_bucket(trans, ca, reserve,
471                                               *cur_bucket,
472                                               skipped_open,
473                                               skipped_need_journal_commit,
474                                               skipped_nouse,
475                                               k, cl);
476                         if (ob)
477                                 break;
478                 }
479
480                 if (ob || ret)
481                         break;
482         }
483         bch2_trans_iter_exit(trans, &iter);
484
485         return ob ?: ERR_PTR(ret);
486 }
487
488 /**
489  * bch_bucket_alloc - allocate a single bucket from a specific device
490  *
491  * Returns index of bucket on success, 0 on failure
492  */
493 static struct open_bucket *bch2_bucket_alloc_trans(struct btree_trans *trans,
494                                       struct bch_dev *ca,
495                                       enum alloc_reserve reserve,
496                                       bool may_alloc_partial,
497                                       struct closure *cl,
498                                       struct bch_dev_usage *usage)
499 {
500         struct bch_fs *c = trans->c;
501         struct open_bucket *ob = NULL;
502         bool freespace_initialized = READ_ONCE(ca->mi.freespace_initialized);
503         u64 start = freespace_initialized ? 0 : ca->bucket_alloc_trans_early_cursor;
504         u64 avail;
505         u64 cur_bucket = start;
506         u64 buckets_seen = 0;
507         u64 skipped_open = 0;
508         u64 skipped_need_journal_commit = 0;
509         u64 skipped_nouse = 0;
510         bool waiting = false;
511 again:
512         bch2_dev_usage_read_fast(ca, usage);
513         avail = dev_buckets_free(ca, *usage, reserve);
514
515         if (usage->d[BCH_DATA_need_discard].buckets > avail)
516                 bch2_do_discards(c);
517
518         if (usage->d[BCH_DATA_need_gc_gens].buckets > avail)
519                 bch2_do_gc_gens(c);
520
521         if (should_invalidate_buckets(ca, *usage))
522                 bch2_do_invalidates(c);
523
524         if (!avail) {
525                 if (cl && !waiting) {
526                         closure_wait(&c->freelist_wait, cl);
527                         waiting = true;
528                         goto again;
529                 }
530
531                 if (!c->blocked_allocate)
532                         c->blocked_allocate = local_clock();
533
534                 ob = ERR_PTR(-BCH_ERR_freelist_empty);
535                 goto err;
536         }
537
538         if (waiting)
539                 closure_wake_up(&c->freelist_wait);
540
541         if (may_alloc_partial) {
542                 ob = try_alloc_partial_bucket(c, ca, reserve);
543                 if (ob)
544                         return ob;
545         }
546
547         ob = likely(ca->mi.freespace_initialized)
548                 ? bch2_bucket_alloc_freelist(trans, ca, reserve,
549                                         &cur_bucket,
550                                         &buckets_seen,
551                                         &skipped_open,
552                                         &skipped_need_journal_commit,
553                                         &skipped_nouse,
554                                         cl)
555                 : bch2_bucket_alloc_early(trans, ca, reserve,
556                                         &cur_bucket,
557                                         &buckets_seen,
558                                         &skipped_open,
559                                         &skipped_need_journal_commit,
560                                         &skipped_nouse,
561                                         cl);
562
563         if (skipped_need_journal_commit * 2 > avail)
564                 bch2_journal_flush_async(&c->journal, NULL);
565
566         if (!ob && !freespace_initialized && start) {
567                 start = cur_bucket = 0;
568                 goto again;
569         }
570
571         if (!freespace_initialized)
572                 ca->bucket_alloc_trans_early_cursor = cur_bucket;
573 err:
574         if (!ob)
575                 ob = ERR_PTR(-BCH_ERR_no_buckets_found);
576
577         if (!IS_ERR(ob))
578                 trace_and_count(c, bucket_alloc, ca, bch2_alloc_reserves[reserve],
579                                 may_alloc_partial, ob->bucket);
580         else if (!bch2_err_matches(PTR_ERR(ob), BCH_ERR_transaction_restart))
581                 trace_and_count(c, bucket_alloc_fail,
582                                 ca, bch2_alloc_reserves[reserve],
583                                 usage->d[BCH_DATA_free].buckets,
584                                 avail,
585                                 bch2_copygc_wait_amount(c),
586                                 c->copygc_wait - atomic64_read(&c->io_clock[WRITE].now),
587                                 buckets_seen,
588                                 skipped_open,
589                                 skipped_need_journal_commit,
590                                 skipped_nouse,
591                                 cl == NULL,
592                                 bch2_err_str(PTR_ERR(ob)));
593
594         return ob;
595 }
596
597 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
598                                       enum alloc_reserve reserve,
599                                       bool may_alloc_partial,
600                                       struct closure *cl)
601 {
602         struct bch_dev_usage usage;
603         struct open_bucket *ob;
604
605         bch2_trans_do(c, NULL, NULL, 0,
606                       PTR_ERR_OR_ZERO(ob = bch2_bucket_alloc_trans(&trans, ca, reserve,
607                                                         may_alloc_partial, cl, &usage)));
608         return ob;
609 }
610
611 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
612                             unsigned l, unsigned r)
613 {
614         return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
615                 (stripe->next_alloc[l] < stripe->next_alloc[r]));
616 }
617
618 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
619
620 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
621                                           struct dev_stripe_state *stripe,
622                                           struct bch_devs_mask *devs)
623 {
624         struct dev_alloc_list ret = { .nr = 0 };
625         unsigned i;
626
627         for_each_set_bit(i, devs->d, BCH_SB_MEMBERS_MAX)
628                 ret.devs[ret.nr++] = i;
629
630         bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
631         return ret;
632 }
633
634 static inline void bch2_dev_stripe_increment_inlined(struct bch_dev *ca,
635                                struct dev_stripe_state *stripe,
636                                struct bch_dev_usage *usage)
637 {
638         u64 *v = stripe->next_alloc + ca->dev_idx;
639         u64 free_space = dev_buckets_available(ca, RESERVE_none);
640         u64 free_space_inv = free_space
641                 ? div64_u64(1ULL << 48, free_space)
642                 : 1ULL << 48;
643         u64 scale = *v / 4;
644
645         if (*v + free_space_inv >= *v)
646                 *v += free_space_inv;
647         else
648                 *v = U64_MAX;
649
650         for (v = stripe->next_alloc;
651              v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
652                 *v = *v < scale ? 0 : *v - scale;
653 }
654
655 void bch2_dev_stripe_increment(struct bch_dev *ca,
656                                struct dev_stripe_state *stripe)
657 {
658         struct bch_dev_usage usage;
659
660         bch2_dev_usage_read_fast(ca, &usage);
661         bch2_dev_stripe_increment_inlined(ca, stripe, &usage);
662 }
663
664 #define BUCKET_MAY_ALLOC_PARTIAL        (1 << 0)
665 #define BUCKET_ALLOC_USE_DURABILITY     (1 << 1)
666
667 static void add_new_bucket(struct bch_fs *c,
668                            struct open_buckets *ptrs,
669                            struct bch_devs_mask *devs_may_alloc,
670                            unsigned *nr_effective,
671                            bool *have_cache,
672                            unsigned flags,
673                            struct open_bucket *ob)
674 {
675         unsigned durability =
676                 bch_dev_bkey_exists(c, ob->dev)->mi.durability;
677
678         __clear_bit(ob->dev, devs_may_alloc->d);
679         *nr_effective   += (flags & BUCKET_ALLOC_USE_DURABILITY)
680                 ? durability : 1;
681         *have_cache     |= !durability;
682
683         ob_push(c, ptrs, ob);
684 }
685
686 static int bch2_bucket_alloc_set_trans(struct btree_trans *trans,
687                       struct open_buckets *ptrs,
688                       struct dev_stripe_state *stripe,
689                       struct bch_devs_mask *devs_may_alloc,
690                       unsigned nr_replicas,
691                       unsigned *nr_effective,
692                       bool *have_cache,
693                       enum alloc_reserve reserve,
694                       unsigned flags,
695                       struct closure *cl)
696 {
697         struct bch_fs *c = trans->c;
698         struct dev_alloc_list devs_sorted =
699                 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
700         unsigned dev;
701         struct bch_dev *ca;
702         int ret = -BCH_ERR_insufficient_devices;
703         unsigned i;
704
705         BUG_ON(*nr_effective >= nr_replicas);
706
707         for (i = 0; i < devs_sorted.nr; i++) {
708                 struct bch_dev_usage usage;
709                 struct open_bucket *ob;
710
711                 dev = devs_sorted.devs[i];
712
713                 rcu_read_lock();
714                 ca = rcu_dereference(c->devs[dev]);
715                 if (ca)
716                         percpu_ref_get(&ca->ref);
717                 rcu_read_unlock();
718
719                 if (!ca)
720                         continue;
721
722                 if (!ca->mi.durability && *have_cache) {
723                         percpu_ref_put(&ca->ref);
724                         continue;
725                 }
726
727                 ob = bch2_bucket_alloc_trans(trans, ca, reserve,
728                                 flags & BUCKET_MAY_ALLOC_PARTIAL, cl, &usage);
729                 if (!IS_ERR(ob))
730                         bch2_dev_stripe_increment_inlined(ca, stripe, &usage);
731                 percpu_ref_put(&ca->ref);
732
733                 if (IS_ERR(ob)) {
734                         ret = PTR_ERR(ob);
735                         if (bch2_err_matches(ret, BCH_ERR_transaction_restart) || cl)
736                                 break;
737                         continue;
738                 }
739
740                 add_new_bucket(c, ptrs, devs_may_alloc,
741                                nr_effective, have_cache, flags, ob);
742
743                 if (*nr_effective >= nr_replicas) {
744                         ret = 0;
745                         break;
746                 }
747         }
748
749         return ret;
750 }
751
752 int bch2_bucket_alloc_set(struct bch_fs *c,
753                       struct open_buckets *ptrs,
754                       struct dev_stripe_state *stripe,
755                       struct bch_devs_mask *devs_may_alloc,
756                       unsigned nr_replicas,
757                       unsigned *nr_effective,
758                       bool *have_cache,
759                       enum alloc_reserve reserve,
760                       unsigned flags,
761                       struct closure *cl)
762 {
763         return bch2_trans_do(c, NULL, NULL, 0,
764                       bch2_bucket_alloc_set_trans(&trans, ptrs, stripe,
765                                               devs_may_alloc, nr_replicas,
766                                               nr_effective, have_cache, reserve,
767                                               flags, cl));
768 }
769
770 /* Allocate from stripes: */
771
772 /*
773  * if we can't allocate a new stripe because there are already too many
774  * partially filled stripes, force allocating from an existing stripe even when
775  * it's to a device we don't want:
776  */
777
778 static int bucket_alloc_from_stripe(struct bch_fs *c,
779                          struct open_buckets *ptrs,
780                          struct write_point *wp,
781                          struct bch_devs_mask *devs_may_alloc,
782                          u16 target,
783                          unsigned erasure_code,
784                          unsigned nr_replicas,
785                          unsigned *nr_effective,
786                          bool *have_cache,
787                          unsigned flags,
788                          struct closure *cl)
789 {
790         struct dev_alloc_list devs_sorted;
791         struct ec_stripe_head *h;
792         struct open_bucket *ob;
793         struct bch_dev *ca;
794         unsigned i, ec_idx;
795
796         if (!erasure_code)
797                 return 0;
798
799         if (nr_replicas < 2)
800                 return 0;
801
802         if (ec_open_bucket(c, ptrs))
803                 return 0;
804
805         h = bch2_ec_stripe_head_get(c, target, 0, nr_replicas - 1,
806                                     wp == &c->copygc_write_point,
807                                     cl);
808         if (IS_ERR(h))
809                 return -PTR_ERR(h);
810         if (!h)
811                 return 0;
812
813         devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
814
815         for (i = 0; i < devs_sorted.nr; i++)
816                 for (ec_idx = 0; ec_idx < h->s->nr_data; ec_idx++) {
817                         if (!h->s->blocks[ec_idx])
818                                 continue;
819
820                         ob = c->open_buckets + h->s->blocks[ec_idx];
821                         if (ob->dev == devs_sorted.devs[i] &&
822                             !test_and_set_bit(ec_idx, h->s->blocks_allocated))
823                                 goto got_bucket;
824                 }
825         goto out_put_head;
826 got_bucket:
827         ca = bch_dev_bkey_exists(c, ob->dev);
828
829         ob->ec_idx      = ec_idx;
830         ob->ec          = h->s;
831
832         add_new_bucket(c, ptrs, devs_may_alloc,
833                        nr_effective, have_cache, flags, ob);
834         atomic_inc(&h->s->pin);
835 out_put_head:
836         bch2_ec_stripe_head_put(c, h);
837         return 0;
838 }
839
840 /* Sector allocator */
841
842 static void get_buckets_from_writepoint(struct bch_fs *c,
843                                         struct open_buckets *ptrs,
844                                         struct write_point *wp,
845                                         struct bch_devs_mask *devs_may_alloc,
846                                         unsigned nr_replicas,
847                                         unsigned *nr_effective,
848                                         bool *have_cache,
849                                         unsigned flags,
850                                         bool need_ec)
851 {
852         struct open_buckets ptrs_skip = { .nr = 0 };
853         struct open_bucket *ob;
854         unsigned i;
855
856         open_bucket_for_each(c, &wp->ptrs, ob, i) {
857                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
858
859                 if (*nr_effective < nr_replicas &&
860                     test_bit(ob->dev, devs_may_alloc->d) &&
861                     (ca->mi.durability ||
862                      (wp->data_type == BCH_DATA_user && !*have_cache)) &&
863                     (ob->ec || !need_ec)) {
864                         add_new_bucket(c, ptrs, devs_may_alloc,
865                                        nr_effective, have_cache,
866                                        flags, ob);
867                 } else {
868                         ob_push(c, &ptrs_skip, ob);
869                 }
870         }
871         wp->ptrs = ptrs_skip;
872 }
873
874 static int open_bucket_add_buckets(struct btree_trans *trans,
875                         struct open_buckets *ptrs,
876                         struct write_point *wp,
877                         struct bch_devs_list *devs_have,
878                         u16 target,
879                         unsigned erasure_code,
880                         unsigned nr_replicas,
881                         unsigned *nr_effective,
882                         bool *have_cache,
883                         enum alloc_reserve reserve,
884                         unsigned flags,
885                         struct closure *_cl)
886 {
887         struct bch_fs *c = trans->c;
888         struct bch_devs_mask devs;
889         struct open_bucket *ob;
890         struct closure *cl = NULL;
891         int ret;
892         unsigned i;
893
894         rcu_read_lock();
895         devs = target_rw_devs(c, wp->data_type, target);
896         rcu_read_unlock();
897
898         /* Don't allocate from devices we already have pointers to: */
899         for (i = 0; i < devs_have->nr; i++)
900                 __clear_bit(devs_have->devs[i], devs.d);
901
902         open_bucket_for_each(c, ptrs, ob, i)
903                 __clear_bit(ob->dev, devs.d);
904
905         if (erasure_code) {
906                 if (!ec_open_bucket(c, ptrs)) {
907                         get_buckets_from_writepoint(c, ptrs, wp, &devs,
908                                                     nr_replicas, nr_effective,
909                                                     have_cache, flags, true);
910                         if (*nr_effective >= nr_replicas)
911                                 return 0;
912                 }
913
914                 if (!ec_open_bucket(c, ptrs)) {
915                         ret = bucket_alloc_from_stripe(c, ptrs, wp, &devs,
916                                                  target, erasure_code,
917                                                  nr_replicas, nr_effective,
918                                                  have_cache, flags, _cl);
919                         if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
920                             bch2_err_matches(ret, BCH_ERR_freelist_empty) ||
921                             bch2_err_matches(ret, BCH_ERR_open_buckets_empty))
922                                 return ret;
923                         if (*nr_effective >= nr_replicas)
924                                 return 0;
925                 }
926         }
927
928         get_buckets_from_writepoint(c, ptrs, wp, &devs,
929                                     nr_replicas, nr_effective,
930                                     have_cache, flags, false);
931         if (*nr_effective >= nr_replicas)
932                 return 0;
933
934 retry_blocking:
935         /*
936          * Try nonblocking first, so that if one device is full we'll try from
937          * other devices:
938          */
939         ret = bch2_bucket_alloc_set_trans(trans, ptrs, &wp->stripe, &devs,
940                                 nr_replicas, nr_effective, have_cache,
941                                 reserve, flags, cl);
942         if (ret &&
943             !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
944             !bch2_err_matches(ret, BCH_ERR_insufficient_devices) &&
945             !cl && _cl) {
946                 cl = _cl;
947                 goto retry_blocking;
948         }
949
950         return ret;
951 }
952
953 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
954                                 struct open_buckets *obs)
955 {
956         struct open_buckets ptrs = { .nr = 0 };
957         struct open_bucket *ob, *ob2;
958         unsigned i, j;
959
960         open_bucket_for_each(c, obs, ob, i) {
961                 bool drop = !ca || ob->dev == ca->dev_idx;
962
963                 if (!drop && ob->ec) {
964                         mutex_lock(&ob->ec->lock);
965                         for (j = 0; j < ob->ec->new_stripe.key.v.nr_blocks; j++) {
966                                 if (!ob->ec->blocks[j])
967                                         continue;
968
969                                 ob2 = c->open_buckets + ob->ec->blocks[j];
970                                 drop |= ob2->dev == ca->dev_idx;
971                         }
972                         mutex_unlock(&ob->ec->lock);
973                 }
974
975                 if (drop)
976                         bch2_open_bucket_put(c, ob);
977                 else
978                         ob_push(c, &ptrs, ob);
979         }
980
981         *obs = ptrs;
982 }
983
984 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
985                           struct write_point *wp)
986 {
987         mutex_lock(&wp->lock);
988         bch2_open_buckets_stop_dev(c, ca, &wp->ptrs);
989         mutex_unlock(&wp->lock);
990 }
991
992 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
993                                                  unsigned long write_point)
994 {
995         unsigned hash =
996                 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
997
998         return &c->write_points_hash[hash];
999 }
1000
1001 static struct write_point *__writepoint_find(struct hlist_head *head,
1002                                              unsigned long write_point)
1003 {
1004         struct write_point *wp;
1005
1006         rcu_read_lock();
1007         hlist_for_each_entry_rcu(wp, head, node)
1008                 if (wp->write_point == write_point)
1009                         goto out;
1010         wp = NULL;
1011 out:
1012         rcu_read_unlock();
1013         return wp;
1014 }
1015
1016 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
1017 {
1018         u64 stranded    = c->write_points_nr * c->bucket_size_max;
1019         u64 free        = bch2_fs_usage_read_short(c).free;
1020
1021         return stranded * factor > free;
1022 }
1023
1024 static bool try_increase_writepoints(struct bch_fs *c)
1025 {
1026         struct write_point *wp;
1027
1028         if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
1029             too_many_writepoints(c, 32))
1030                 return false;
1031
1032         wp = c->write_points + c->write_points_nr++;
1033         hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
1034         return true;
1035 }
1036
1037 static bool try_decrease_writepoints(struct bch_fs *c,
1038                                      unsigned old_nr)
1039 {
1040         struct write_point *wp;
1041
1042         mutex_lock(&c->write_points_hash_lock);
1043         if (c->write_points_nr < old_nr) {
1044                 mutex_unlock(&c->write_points_hash_lock);
1045                 return true;
1046         }
1047
1048         if (c->write_points_nr == 1 ||
1049             !too_many_writepoints(c, 8)) {
1050                 mutex_unlock(&c->write_points_hash_lock);
1051                 return false;
1052         }
1053
1054         wp = c->write_points + --c->write_points_nr;
1055
1056         hlist_del_rcu(&wp->node);
1057         mutex_unlock(&c->write_points_hash_lock);
1058
1059         bch2_writepoint_stop(c, NULL, wp);
1060         return true;
1061 }
1062
1063 static void bch2_trans_mutex_lock(struct btree_trans *trans,
1064                                   struct mutex *lock)
1065 {
1066         if (!mutex_trylock(lock)) {
1067                 bch2_trans_unlock(trans);
1068                 mutex_lock(lock);
1069         }
1070 }
1071
1072 static struct write_point *writepoint_find(struct btree_trans *trans,
1073                                            unsigned long write_point)
1074 {
1075         struct bch_fs *c = trans->c;
1076         struct write_point *wp, *oldest;
1077         struct hlist_head *head;
1078
1079         if (!(write_point & 1UL)) {
1080                 wp = (struct write_point *) write_point;
1081                 bch2_trans_mutex_lock(trans, &wp->lock);
1082                 return wp;
1083         }
1084
1085         head = writepoint_hash(c, write_point);
1086 restart_find:
1087         wp = __writepoint_find(head, write_point);
1088         if (wp) {
1089 lock_wp:
1090                 bch2_trans_mutex_lock(trans, &wp->lock);
1091                 if (wp->write_point == write_point)
1092                         goto out;
1093                 mutex_unlock(&wp->lock);
1094                 goto restart_find;
1095         }
1096 restart_find_oldest:
1097         oldest = NULL;
1098         for (wp = c->write_points;
1099              wp < c->write_points + c->write_points_nr; wp++)
1100                 if (!oldest || time_before64(wp->last_used, oldest->last_used))
1101                         oldest = wp;
1102
1103         bch2_trans_mutex_lock(trans, &oldest->lock);
1104         bch2_trans_mutex_lock(trans, &c->write_points_hash_lock);
1105         if (oldest >= c->write_points + c->write_points_nr ||
1106             try_increase_writepoints(c)) {
1107                 mutex_unlock(&c->write_points_hash_lock);
1108                 mutex_unlock(&oldest->lock);
1109                 goto restart_find_oldest;
1110         }
1111
1112         wp = __writepoint_find(head, write_point);
1113         if (wp && wp != oldest) {
1114                 mutex_unlock(&c->write_points_hash_lock);
1115                 mutex_unlock(&oldest->lock);
1116                 goto lock_wp;
1117         }
1118
1119         wp = oldest;
1120         hlist_del_rcu(&wp->node);
1121         wp->write_point = write_point;
1122         hlist_add_head_rcu(&wp->node, head);
1123         mutex_unlock(&c->write_points_hash_lock);
1124 out:
1125         wp->last_used = local_clock();
1126         return wp;
1127 }
1128
1129 /*
1130  * Get us an open_bucket we can allocate from, return with it locked:
1131  */
1132 struct write_point *bch2_alloc_sectors_start_trans(struct btree_trans *trans,
1133                                 unsigned target,
1134                                 unsigned erasure_code,
1135                                 struct write_point_specifier write_point,
1136                                 struct bch_devs_list *devs_have,
1137                                 unsigned nr_replicas,
1138                                 unsigned nr_replicas_required,
1139                                 enum alloc_reserve reserve,
1140                                 unsigned flags,
1141                                 struct closure *cl)
1142 {
1143         struct bch_fs *c = trans->c;
1144         struct write_point *wp;
1145         struct open_bucket *ob;
1146         struct open_buckets ptrs;
1147         unsigned nr_effective, write_points_nr;
1148         unsigned ob_flags = 0;
1149         bool have_cache;
1150         int ret;
1151         int i;
1152
1153         if (!(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS))
1154                 ob_flags |= BUCKET_ALLOC_USE_DURABILITY;
1155
1156         BUG_ON(!nr_replicas || !nr_replicas_required);
1157 retry:
1158         ptrs.nr         = 0;
1159         nr_effective    = 0;
1160         write_points_nr = c->write_points_nr;
1161         have_cache      = false;
1162
1163         wp = writepoint_find(trans, write_point.v);
1164
1165         if (wp->data_type == BCH_DATA_user)
1166                 ob_flags |= BUCKET_MAY_ALLOC_PARTIAL;
1167
1168         /* metadata may not allocate on cache devices: */
1169         if (wp->data_type != BCH_DATA_user)
1170                 have_cache = true;
1171
1172         if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
1173                 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1174                                               target, erasure_code,
1175                                               nr_replicas, &nr_effective,
1176                                               &have_cache, reserve,
1177                                               ob_flags, cl);
1178         } else {
1179                 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1180                                               target, erasure_code,
1181                                               nr_replicas, &nr_effective,
1182                                               &have_cache, reserve,
1183                                               ob_flags, NULL);
1184                 if (!ret ||
1185                     bch2_err_matches(ret, BCH_ERR_transaction_restart))
1186                         goto alloc_done;
1187
1188                 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1189                                               0, erasure_code,
1190                                               nr_replicas, &nr_effective,
1191                                               &have_cache, reserve,
1192                                               ob_flags, cl);
1193         }
1194 alloc_done:
1195         BUG_ON(!ret && nr_effective < nr_replicas);
1196
1197         if (erasure_code && !ec_open_bucket(c, &ptrs))
1198                 pr_debug("failed to get ec bucket: ret %u", ret);
1199
1200         if (ret == -BCH_ERR_insufficient_devices &&
1201             nr_effective >= nr_replicas_required)
1202                 ret = 0;
1203
1204         if (ret)
1205                 goto err;
1206
1207         /* Free buckets we didn't use: */
1208         open_bucket_for_each(c, &wp->ptrs, ob, i)
1209                 open_bucket_free_unused(c, wp, ob);
1210
1211         wp->ptrs = ptrs;
1212
1213         wp->sectors_free = UINT_MAX;
1214
1215         open_bucket_for_each(c, &wp->ptrs, ob, i)
1216                 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
1217
1218         BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
1219
1220         return wp;
1221 err:
1222         open_bucket_for_each(c, &wp->ptrs, ob, i)
1223                 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
1224                         ob_push(c, &ptrs, ob);
1225                 else
1226                         open_bucket_free_unused(c, wp, ob);
1227         wp->ptrs = ptrs;
1228
1229         mutex_unlock(&wp->lock);
1230
1231         if (bch2_err_matches(ret, BCH_ERR_freelist_empty) &&
1232             try_decrease_writepoints(c, write_points_nr))
1233                 goto retry;
1234
1235         if (bch2_err_matches(ret, BCH_ERR_open_buckets_empty) ||
1236             bch2_err_matches(ret, BCH_ERR_freelist_empty))
1237                 return cl
1238                         ? ERR_PTR(-EAGAIN)
1239                         : ERR_PTR(-BCH_ERR_ENOSPC_bucket_alloc);
1240
1241         if (bch2_err_matches(ret, BCH_ERR_insufficient_devices))
1242                 return ERR_PTR(-EROFS);
1243
1244         return ERR_PTR(ret);
1245 }
1246
1247 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
1248                                 unsigned target,
1249                                 unsigned erasure_code,
1250                                 struct write_point_specifier write_point,
1251                                 struct bch_devs_list *devs_have,
1252                                 unsigned nr_replicas,
1253                                 unsigned nr_replicas_required,
1254                                 enum alloc_reserve reserve,
1255                                 unsigned flags,
1256                                 struct closure *cl)
1257 {
1258         struct write_point *wp;
1259
1260         bch2_trans_do(c, NULL, NULL, 0,
1261                       PTR_ERR_OR_ZERO(wp = bch2_alloc_sectors_start_trans(&trans, target,
1262                                                         erasure_code,
1263                                                         write_point,
1264                                                         devs_have,
1265                                                         nr_replicas,
1266                                                         nr_replicas_required,
1267                                                         reserve,
1268                                                         flags, cl)));
1269         return wp;
1270
1271 }
1272
1273 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *c, struct open_bucket *ob)
1274 {
1275         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1276
1277         return (struct bch_extent_ptr) {
1278                 .type   = 1 << BCH_EXTENT_ENTRY_ptr,
1279                 .gen    = ob->gen,
1280                 .dev    = ob->dev,
1281                 .offset = bucket_to_sector(ca, ob->bucket) +
1282                         ca->mi.bucket_size -
1283                         ob->sectors_free,
1284         };
1285 }
1286
1287 /*
1288  * Append pointers to the space we just allocated to @k, and mark @sectors space
1289  * as allocated out of @ob
1290  */
1291 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
1292                                     struct bkey_i *k, unsigned sectors,
1293                                     bool cached)
1294
1295 {
1296         struct open_bucket *ob;
1297         unsigned i;
1298
1299         BUG_ON(sectors > wp->sectors_free);
1300         wp->sectors_free -= sectors;
1301
1302         open_bucket_for_each(c, &wp->ptrs, ob, i) {
1303                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1304                 struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
1305
1306                 ptr.cached = cached ||
1307                         (!ca->mi.durability &&
1308                          wp->data_type == BCH_DATA_user);
1309
1310                 bch2_bkey_append_ptr(k, ptr);
1311
1312                 BUG_ON(sectors > ob->sectors_free);
1313                 ob->sectors_free -= sectors;
1314         }
1315 }
1316
1317 /*
1318  * Append pointers to the space we just allocated to @k, and mark @sectors space
1319  * as allocated out of @ob
1320  */
1321 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
1322 {
1323         struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
1324         struct open_bucket *ob;
1325         unsigned i;
1326
1327         open_bucket_for_each(c, &wp->ptrs, ob, i)
1328                 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
1329         wp->ptrs = keep;
1330
1331         mutex_unlock(&wp->lock);
1332
1333         bch2_open_buckets_put(c, &ptrs);
1334 }
1335
1336 static inline void writepoint_init(struct write_point *wp,
1337                                    enum bch_data_type type)
1338 {
1339         mutex_init(&wp->lock);
1340         wp->data_type = type;
1341 }
1342
1343 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
1344 {
1345         struct open_bucket *ob;
1346         struct write_point *wp;
1347
1348         mutex_init(&c->write_points_hash_lock);
1349         c->write_points_nr = ARRAY_SIZE(c->write_points);
1350
1351         /* open bucket 0 is a sentinal NULL: */
1352         spin_lock_init(&c->open_buckets[0].lock);
1353
1354         for (ob = c->open_buckets + 1;
1355              ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
1356                 spin_lock_init(&ob->lock);
1357                 c->open_buckets_nr_free++;
1358
1359                 ob->freelist = c->open_buckets_freelist;
1360                 c->open_buckets_freelist = ob - c->open_buckets;
1361         }
1362
1363         writepoint_init(&c->btree_write_point,          BCH_DATA_btree);
1364         writepoint_init(&c->rebalance_write_point,      BCH_DATA_user);
1365         writepoint_init(&c->copygc_write_point,         BCH_DATA_user);
1366
1367         for (wp = c->write_points;
1368              wp < c->write_points + c->write_points_nr; wp++) {
1369                 writepoint_init(wp, BCH_DATA_user);
1370
1371                 wp->last_used   = local_clock();
1372                 wp->write_point = (unsigned long) wp;
1373                 hlist_add_head_rcu(&wp->node,
1374                                    writepoint_hash(c, wp->write_point));
1375         }
1376 }
1377
1378 void bch2_open_buckets_to_text(struct printbuf *out, struct bch_fs *c)
1379 {
1380         struct open_bucket *ob;
1381
1382         for (ob = c->open_buckets;
1383              ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1384              ob++) {
1385                 spin_lock(&ob->lock);
1386                 if (ob->valid && !ob->on_partial_list) {
1387                         prt_printf(out, "%zu ref %u type %s %u:%llu:%u\n",
1388                                ob - c->open_buckets,
1389                                atomic_read(&ob->pin),
1390                                bch2_data_types[ob->data_type],
1391                                ob->dev, ob->bucket, ob->gen);
1392                 }
1393                 spin_unlock(&ob->lock);
1394         }
1395 }