]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/alloc_foreground.c
Update bcachefs sources to 176718966e bcachefs: Re-enable hash_redo_key()
[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         trace_and_count(c, bucket_alloc, ca, bch2_alloc_reserves[reserve]);
272         return ob;
273 }
274
275 static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bch_dev *ca,
276                                             enum alloc_reserve reserve, u64 free_entry,
277                                             u64 *skipped_open,
278                                             u64 *skipped_need_journal_commit,
279                                             u64 *skipped_nouse,
280                                             struct bkey_s_c freespace_k,
281                                             struct closure *cl)
282 {
283         struct bch_fs *c = trans->c;
284         struct btree_iter iter = { NULL };
285         struct bkey_s_c k;
286         struct open_bucket *ob;
287         struct bch_alloc_v4 a;
288         u64 b = free_entry & ~(~0ULL << 56);
289         unsigned genbits = free_entry >> 56;
290         struct printbuf buf = PRINTBUF;
291         int ret;
292
293         if (b < ca->mi.first_bucket || b >= ca->mi.nbuckets) {
294                 prt_printf(&buf, "freespace btree has bucket outside allowed range %u-%llu\n"
295                        "  freespace key ",
296                         ca->mi.first_bucket, ca->mi.nbuckets);
297                 bch2_bkey_val_to_text(&buf, c, freespace_k);
298                 bch2_trans_inconsistent(trans, "%s", buf.buf);
299                 ob = ERR_PTR(-EIO);
300                 goto err;
301         }
302
303         bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(ca->dev_idx, b), BTREE_ITER_CACHED);
304         k = bch2_btree_iter_peek_slot(&iter);
305         ret = bkey_err(k);
306         if (ret) {
307                 ob = ERR_PTR(ret);
308                 goto err;
309         }
310
311         bch2_alloc_to_v4(k, &a);
312
313         if (genbits != (alloc_freespace_genbits(a) >> 56)) {
314                 prt_printf(&buf, "bucket in freespace btree with wrong genbits (got %u should be %llu)\n"
315                        "  freespace key ",
316                        genbits, alloc_freespace_genbits(a) >> 56);
317                 bch2_bkey_val_to_text(&buf, c, freespace_k);
318                 prt_printf(&buf, "\n  ");
319                 bch2_bkey_val_to_text(&buf, c, k);
320                 bch2_trans_inconsistent(trans, "%s", buf.buf);
321                 ob = ERR_PTR(-EIO);
322                 goto err;
323
324         }
325
326         if (a.data_type != BCH_DATA_free) {
327                 prt_printf(&buf, "non free bucket in freespace btree\n"
328                        "  freespace key ");
329                 bch2_bkey_val_to_text(&buf, c, freespace_k);
330                 prt_printf(&buf, "\n  ");
331                 bch2_bkey_val_to_text(&buf, c, k);
332                 bch2_trans_inconsistent(trans, "%s", buf.buf);
333                 ob = ERR_PTR(-EIO);
334                 goto err;
335         }
336
337         ob = __try_alloc_bucket(c, ca, b, reserve, &a,
338                                 skipped_open,
339                                 skipped_need_journal_commit,
340                                 skipped_nouse,
341                                 cl);
342         if (!ob)
343                 iter.path->preserve = false;
344 err:
345         set_btree_iter_dontneed(&iter);
346         bch2_trans_iter_exit(trans, &iter);
347         printbuf_exit(&buf);
348         return ob;
349 }
350
351 static struct open_bucket *try_alloc_partial_bucket(struct bch_fs *c, struct bch_dev *ca,
352                                                     enum alloc_reserve reserve)
353 {
354         struct open_bucket *ob;
355         int i;
356
357         spin_lock(&c->freelist_lock);
358
359         for (i = ca->open_buckets_partial_nr - 1; i >= 0; --i) {
360                 ob = c->open_buckets + ca->open_buckets_partial[i];
361
362                 if (reserve <= ob->alloc_reserve) {
363                         array_remove_item(ca->open_buckets_partial,
364                                           ca->open_buckets_partial_nr,
365                                           i);
366                         ob->on_partial_list = false;
367                         ob->alloc_reserve = reserve;
368                         spin_unlock(&c->freelist_lock);
369                         return ob;
370                 }
371         }
372
373         spin_unlock(&c->freelist_lock);
374         return NULL;
375 }
376
377 /*
378  * This path is for before the freespace btree is initialized:
379  *
380  * If ca->new_fs_bucket_idx is nonzero, we haven't yet marked superblock &
381  * journal buckets - journal buckets will be < ca->new_fs_bucket_idx
382  */
383 static noinline struct open_bucket *
384 bch2_bucket_alloc_early(struct btree_trans *trans,
385                         struct bch_dev *ca,
386                         enum alloc_reserve reserve,
387                         u64 *cur_bucket,
388                         u64 *buckets_seen,
389                         u64 *skipped_open,
390                         u64 *skipped_need_journal_commit,
391                         u64 *skipped_nouse,
392                         struct closure *cl)
393 {
394         struct btree_iter iter;
395         struct bkey_s_c k;
396         struct open_bucket *ob = NULL;
397         int ret;
398
399         *cur_bucket = max_t(u64, *cur_bucket, ca->mi.first_bucket);
400         *cur_bucket = max_t(u64, *cur_bucket, ca->new_fs_bucket_idx);
401
402         for_each_btree_key_norestart(trans, iter, BTREE_ID_alloc, POS(ca->dev_idx, *cur_bucket),
403                            BTREE_ITER_SLOTS, k, ret) {
404                 struct bch_alloc_v4 a;
405
406                 if (bkey_cmp(k.k->p, POS(ca->dev_idx, ca->mi.nbuckets)) >= 0)
407                         break;
408
409                 if (ca->new_fs_bucket_idx &&
410                     is_superblock_bucket(ca, k.k->p.offset))
411                         continue;
412
413                 bch2_alloc_to_v4(k, &a);
414
415                 if (a.data_type != BCH_DATA_free)
416                         continue;
417
418                 (*buckets_seen)++;
419
420                 ob = __try_alloc_bucket(trans->c, ca, k.k->p.offset, reserve, &a,
421                                         skipped_open,
422                                         skipped_need_journal_commit,
423                                         skipped_nouse,
424                                         cl);
425                 if (ob)
426                         break;
427         }
428         bch2_trans_iter_exit(trans, &iter);
429
430         *cur_bucket = iter.pos.offset;
431
432         return ob ?: ERR_PTR(ret ?: -BCH_ERR_no_buckets_found);
433 }
434
435 static struct open_bucket *bch2_bucket_alloc_freelist(struct btree_trans *trans,
436                                                    struct bch_dev *ca,
437                                                    enum alloc_reserve reserve,
438                                                    u64 *cur_bucket,
439                                                    u64 *buckets_seen,
440                                                    u64 *skipped_open,
441                                                    u64 *skipped_need_journal_commit,
442                                                    u64 *skipped_nouse,
443                                                    struct closure *cl)
444 {
445         struct btree_iter iter;
446         struct bkey_s_c k;
447         struct open_bucket *ob = NULL;
448         int ret;
449
450         BUG_ON(ca->new_fs_bucket_idx);
451
452         /*
453          * XXX:
454          * On transaction restart, we'd like to restart from the bucket we were
455          * at previously
456          */
457         for_each_btree_key_norestart(trans, iter, BTREE_ID_freespace,
458                                      POS(ca->dev_idx, *cur_bucket), 0, k, ret) {
459                 if (k.k->p.inode != ca->dev_idx)
460                         break;
461
462                 for (*cur_bucket = max(*cur_bucket, bkey_start_offset(k.k));
463                      *cur_bucket < k.k->p.offset;
464                      (*cur_bucket)++) {
465                         ret = btree_trans_too_many_iters(trans);
466                         if (ret)
467                                 break;
468
469                         (*buckets_seen)++;
470
471                         ob = try_alloc_bucket(trans, ca, reserve,
472                                               *cur_bucket,
473                                               skipped_open,
474                                               skipped_need_journal_commit,
475                                               skipped_nouse,
476                                               k, cl);
477                         if (ob)
478                                 break;
479                 }
480
481                 if (ob || ret)
482                         break;
483         }
484         bch2_trans_iter_exit(trans, &iter);
485
486         return ob ?: ERR_PTR(ret);
487 }
488
489 /**
490  * bch_bucket_alloc - allocate a single bucket from a specific device
491  *
492  * Returns index of bucket on success, 0 on failure
493  * */
494 static struct open_bucket *bch2_bucket_alloc_trans(struct btree_trans *trans,
495                                       struct bch_dev *ca,
496                                       enum alloc_reserve reserve,
497                                       bool may_alloc_partial,
498                                       struct closure *cl)
499 {
500         struct bch_fs *c = trans->c;
501         struct open_bucket *ob = NULL;
502         struct bch_dev_usage usage;
503         bool freespace_initialized = READ_ONCE(ca->mi.freespace_initialized);
504         u64 start = freespace_initialized ? 0 : ca->bucket_alloc_trans_early_cursor;
505         u64 avail;
506         u64 cur_bucket = start;
507         u64 buckets_seen = 0;
508         u64 skipped_open = 0;
509         u64 skipped_need_journal_commit = 0;
510         u64 skipped_nouse = 0;
511         bool waiting = false;
512 again:
513         usage = bch2_dev_usage_read(ca);
514         avail = dev_buckets_free(ca, usage, reserve);
515
516         if (usage.d[BCH_DATA_need_discard].buckets > avail)
517                 bch2_do_discards(c);
518
519         if (usage.d[BCH_DATA_need_gc_gens].buckets > avail)
520                 bch2_do_gc_gens(c);
521
522         if (should_invalidate_buckets(ca, usage))
523                 bch2_do_invalidates(c);
524
525         if (!avail) {
526                 if (cl && !waiting) {
527                         closure_wait(&c->freelist_wait, cl);
528                         waiting = true;
529                         goto again;
530                 }
531
532                 if (!c->blocked_allocate)
533                         c->blocked_allocate = local_clock();
534
535                 ob = ERR_PTR(-BCH_ERR_freelist_empty);
536                 goto err;
537         }
538
539         if (waiting)
540                 closure_wake_up(&c->freelist_wait);
541
542         if (may_alloc_partial) {
543                 ob = try_alloc_partial_bucket(c, ca, reserve);
544                 if (ob)
545                         return ob;
546         }
547
548         ob = likely(ca->mi.freespace_initialized)
549                 ? bch2_bucket_alloc_freelist(trans, ca, reserve,
550                                         &cur_bucket,
551                                         &buckets_seen,
552                                         &skipped_open,
553                                         &skipped_need_journal_commit,
554                                         &skipped_nouse,
555                                         cl)
556                 : bch2_bucket_alloc_early(trans, ca, reserve,
557                                         &cur_bucket,
558                                         &buckets_seen,
559                                         &skipped_open,
560                                         &skipped_need_journal_commit,
561                                         &skipped_nouse,
562                                         cl);
563
564         if (skipped_need_journal_commit * 2 > avail)
565                 bch2_journal_flush_async(&c->journal, NULL);
566
567         if (!ob && !freespace_initialized && start) {
568                 start = cur_bucket = 0;
569                 goto again;
570         }
571
572         if (!freespace_initialized)
573                 ca->bucket_alloc_trans_early_cursor = cur_bucket;
574 err:
575         if (!ob)
576                 ob = ERR_PTR(-BCH_ERR_no_buckets_found);
577
578         if (IS_ERR(ob))
579                 trace_and_count(c, bucket_alloc_fail,
580                                 ca, bch2_alloc_reserves[reserve],
581                                 usage.d[BCH_DATA_free].buckets,
582                                 avail,
583                                 bch2_copygc_wait_amount(c),
584                                 c->copygc_wait - atomic64_read(&c->io_clock[WRITE].now),
585                                 buckets_seen,
586                                 skipped_open,
587                                 skipped_need_journal_commit,
588                                 skipped_nouse,
589                                 cl == NULL,
590                                 bch2_err_str(PTR_ERR(ob)));
591
592         return ob;
593 }
594
595 struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
596                                       enum alloc_reserve reserve,
597                                       bool may_alloc_partial,
598                                       struct closure *cl)
599 {
600         struct open_bucket *ob;
601
602         bch2_trans_do(c, NULL, NULL, 0,
603                       PTR_ERR_OR_ZERO(ob = bch2_bucket_alloc_trans(&trans, ca, reserve,
604                                                                    may_alloc_partial, cl)));
605         return ob;
606 }
607
608 static int __dev_stripe_cmp(struct dev_stripe_state *stripe,
609                             unsigned l, unsigned r)
610 {
611         return ((stripe->next_alloc[l] > stripe->next_alloc[r]) -
612                 (stripe->next_alloc[l] < stripe->next_alloc[r]));
613 }
614
615 #define dev_stripe_cmp(l, r) __dev_stripe_cmp(stripe, l, r)
616
617 struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
618                                           struct dev_stripe_state *stripe,
619                                           struct bch_devs_mask *devs)
620 {
621         struct dev_alloc_list ret = { .nr = 0 };
622         unsigned i;
623
624         for_each_set_bit(i, devs->d, BCH_SB_MEMBERS_MAX)
625                 ret.devs[ret.nr++] = i;
626
627         bubble_sort(ret.devs, ret.nr, dev_stripe_cmp);
628         return ret;
629 }
630
631 void bch2_dev_stripe_increment(struct bch_dev *ca,
632                                struct dev_stripe_state *stripe)
633 {
634         u64 *v = stripe->next_alloc + ca->dev_idx;
635         u64 free_space = dev_buckets_available(ca, RESERVE_none);
636         u64 free_space_inv = free_space
637                 ? div64_u64(1ULL << 48, free_space)
638                 : 1ULL << 48;
639         u64 scale = *v / 4;
640
641         if (*v + free_space_inv >= *v)
642                 *v += free_space_inv;
643         else
644                 *v = U64_MAX;
645
646         for (v = stripe->next_alloc;
647              v < stripe->next_alloc + ARRAY_SIZE(stripe->next_alloc); v++)
648                 *v = *v < scale ? 0 : *v - scale;
649 }
650
651 #define BUCKET_MAY_ALLOC_PARTIAL        (1 << 0)
652 #define BUCKET_ALLOC_USE_DURABILITY     (1 << 1)
653
654 static void add_new_bucket(struct bch_fs *c,
655                            struct open_buckets *ptrs,
656                            struct bch_devs_mask *devs_may_alloc,
657                            unsigned *nr_effective,
658                            bool *have_cache,
659                            unsigned flags,
660                            struct open_bucket *ob)
661 {
662         unsigned durability =
663                 bch_dev_bkey_exists(c, ob->dev)->mi.durability;
664
665         __clear_bit(ob->dev, devs_may_alloc->d);
666         *nr_effective   += (flags & BUCKET_ALLOC_USE_DURABILITY)
667                 ? durability : 1;
668         *have_cache     |= !durability;
669
670         ob_push(c, ptrs, ob);
671 }
672
673 static int bch2_bucket_alloc_set_trans(struct btree_trans *trans,
674                       struct open_buckets *ptrs,
675                       struct dev_stripe_state *stripe,
676                       struct bch_devs_mask *devs_may_alloc,
677                       unsigned nr_replicas,
678                       unsigned *nr_effective,
679                       bool *have_cache,
680                       enum alloc_reserve reserve,
681                       unsigned flags,
682                       struct closure *cl)
683 {
684         struct bch_fs *c = trans->c;
685         struct dev_alloc_list devs_sorted =
686                 bch2_dev_alloc_list(c, stripe, devs_may_alloc);
687         unsigned dev;
688         struct bch_dev *ca;
689         int ret = 0;
690         unsigned i;
691
692         BUG_ON(*nr_effective >= nr_replicas);
693
694         for (i = 0; i < devs_sorted.nr; i++) {
695                 struct open_bucket *ob;
696
697                 dev = devs_sorted.devs[i];
698
699                 rcu_read_lock();
700                 ca = rcu_dereference(c->devs[dev]);
701                 if (ca)
702                         percpu_ref_get(&ca->ref);
703                 rcu_read_unlock();
704
705                 if (!ca)
706                         continue;
707
708                 if (!ca->mi.durability && *have_cache) {
709                         percpu_ref_put(&ca->ref);
710                         continue;
711                 }
712
713                 ob = bch2_bucket_alloc_trans(trans, ca, reserve,
714                                 flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
715                 if (!IS_ERR(ob))
716                         bch2_dev_stripe_increment(ca, stripe);
717                 percpu_ref_put(&ca->ref);
718
719                 ret = PTR_ERR_OR_ZERO(ob);
720                 if (ret) {
721                         if (bch2_err_matches(ret, BCH_ERR_transaction_restart) || cl)
722                                 break;
723                         continue;
724                 }
725
726                 add_new_bucket(c, ptrs, devs_may_alloc,
727                                nr_effective, have_cache, flags, ob);
728
729                 if (*nr_effective >= nr_replicas)
730                         break;
731         }
732
733         if (*nr_effective >= nr_replicas)
734                 ret = 0;
735         else if (!ret)
736                 ret = -BCH_ERR_insufficient_devices;
737
738         return ret;
739 }
740
741 int bch2_bucket_alloc_set(struct bch_fs *c,
742                       struct open_buckets *ptrs,
743                       struct dev_stripe_state *stripe,
744                       struct bch_devs_mask *devs_may_alloc,
745                       unsigned nr_replicas,
746                       unsigned *nr_effective,
747                       bool *have_cache,
748                       enum alloc_reserve reserve,
749                       unsigned flags,
750                       struct closure *cl)
751 {
752         return bch2_trans_do(c, NULL, NULL, 0,
753                       bch2_bucket_alloc_set_trans(&trans, ptrs, stripe,
754                                               devs_may_alloc, nr_replicas,
755                                               nr_effective, have_cache, reserve,
756                                               flags, cl));
757 }
758
759 /* Allocate from stripes: */
760
761 /*
762  * if we can't allocate a new stripe because there are already too many
763  * partially filled stripes, force allocating from an existing stripe even when
764  * it's to a device we don't want:
765  */
766
767 static int bucket_alloc_from_stripe(struct bch_fs *c,
768                          struct open_buckets *ptrs,
769                          struct write_point *wp,
770                          struct bch_devs_mask *devs_may_alloc,
771                          u16 target,
772                          unsigned erasure_code,
773                          unsigned nr_replicas,
774                          unsigned *nr_effective,
775                          bool *have_cache,
776                          unsigned flags,
777                          struct closure *cl)
778 {
779         struct dev_alloc_list devs_sorted;
780         struct ec_stripe_head *h;
781         struct open_bucket *ob;
782         struct bch_dev *ca;
783         unsigned i, ec_idx;
784
785         if (!erasure_code)
786                 return 0;
787
788         if (nr_replicas < 2)
789                 return 0;
790
791         if (ec_open_bucket(c, ptrs))
792                 return 0;
793
794         h = bch2_ec_stripe_head_get(c, target, 0, nr_replicas - 1,
795                                     wp == &c->copygc_write_point,
796                                     cl);
797         if (IS_ERR(h))
798                 return -PTR_ERR(h);
799         if (!h)
800                 return 0;
801
802         devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
803
804         for (i = 0; i < devs_sorted.nr; i++)
805                 for (ec_idx = 0; ec_idx < h->s->nr_data; ec_idx++) {
806                         if (!h->s->blocks[ec_idx])
807                                 continue;
808
809                         ob = c->open_buckets + h->s->blocks[ec_idx];
810                         if (ob->dev == devs_sorted.devs[i] &&
811                             !test_and_set_bit(ec_idx, h->s->blocks_allocated))
812                                 goto got_bucket;
813                 }
814         goto out_put_head;
815 got_bucket:
816         ca = bch_dev_bkey_exists(c, ob->dev);
817
818         ob->ec_idx      = ec_idx;
819         ob->ec          = h->s;
820
821         add_new_bucket(c, ptrs, devs_may_alloc,
822                        nr_effective, have_cache, flags, ob);
823         atomic_inc(&h->s->pin);
824 out_put_head:
825         bch2_ec_stripe_head_put(c, h);
826         return 0;
827 }
828
829 /* Sector allocator */
830
831 static void get_buckets_from_writepoint(struct bch_fs *c,
832                                         struct open_buckets *ptrs,
833                                         struct write_point *wp,
834                                         struct bch_devs_mask *devs_may_alloc,
835                                         unsigned nr_replicas,
836                                         unsigned *nr_effective,
837                                         bool *have_cache,
838                                         unsigned flags,
839                                         bool need_ec)
840 {
841         struct open_buckets ptrs_skip = { .nr = 0 };
842         struct open_bucket *ob;
843         unsigned i;
844
845         open_bucket_for_each(c, &wp->ptrs, ob, i) {
846                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
847
848                 if (*nr_effective < nr_replicas &&
849                     test_bit(ob->dev, devs_may_alloc->d) &&
850                     (ca->mi.durability ||
851                      (wp->data_type == BCH_DATA_user && !*have_cache)) &&
852                     (ob->ec || !need_ec)) {
853                         add_new_bucket(c, ptrs, devs_may_alloc,
854                                        nr_effective, have_cache,
855                                        flags, ob);
856                 } else {
857                         ob_push(c, &ptrs_skip, ob);
858                 }
859         }
860         wp->ptrs = ptrs_skip;
861 }
862
863 static int open_bucket_add_buckets(struct btree_trans *trans,
864                         struct open_buckets *ptrs,
865                         struct write_point *wp,
866                         struct bch_devs_list *devs_have,
867                         u16 target,
868                         unsigned erasure_code,
869                         unsigned nr_replicas,
870                         unsigned *nr_effective,
871                         bool *have_cache,
872                         enum alloc_reserve reserve,
873                         unsigned flags,
874                         struct closure *_cl)
875 {
876         struct bch_fs *c = trans->c;
877         struct bch_devs_mask devs;
878         struct open_bucket *ob;
879         struct closure *cl = NULL;
880         int ret;
881         unsigned i;
882
883         rcu_read_lock();
884         devs = target_rw_devs(c, wp->data_type, target);
885         rcu_read_unlock();
886
887         /* Don't allocate from devices we already have pointers to: */
888         for (i = 0; i < devs_have->nr; i++)
889                 __clear_bit(devs_have->devs[i], devs.d);
890
891         open_bucket_for_each(c, ptrs, ob, i)
892                 __clear_bit(ob->dev, devs.d);
893
894         if (erasure_code) {
895                 if (!ec_open_bucket(c, ptrs)) {
896                         get_buckets_from_writepoint(c, ptrs, wp, &devs,
897                                                     nr_replicas, nr_effective,
898                                                     have_cache, flags, true);
899                         if (*nr_effective >= nr_replicas)
900                                 return 0;
901                 }
902
903                 if (!ec_open_bucket(c, ptrs)) {
904                         ret = bucket_alloc_from_stripe(c, ptrs, wp, &devs,
905                                                  target, erasure_code,
906                                                  nr_replicas, nr_effective,
907                                                  have_cache, flags, _cl);
908                         if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
909                             bch2_err_matches(ret, BCH_ERR_freelist_empty) ||
910                             bch2_err_matches(ret, BCH_ERR_open_buckets_empty))
911                                 return ret;
912                         if (*nr_effective >= nr_replicas)
913                                 return 0;
914                 }
915         }
916
917         get_buckets_from_writepoint(c, ptrs, wp, &devs,
918                                     nr_replicas, nr_effective,
919                                     have_cache, flags, false);
920         if (*nr_effective >= nr_replicas)
921                 return 0;
922
923 retry_blocking:
924         /*
925          * Try nonblocking first, so that if one device is full we'll try from
926          * other devices:
927          */
928         ret = bch2_bucket_alloc_set_trans(trans, ptrs, &wp->stripe, &devs,
929                                 nr_replicas, nr_effective, have_cache,
930                                 reserve, flags, cl);
931         if (ret &&
932             !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
933             !bch2_err_matches(ret, BCH_ERR_insufficient_devices) &&
934             !cl && _cl) {
935                 cl = _cl;
936                 goto retry_blocking;
937         }
938
939         return ret;
940 }
941
942 void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
943                                 struct open_buckets *obs)
944 {
945         struct open_buckets ptrs = { .nr = 0 };
946         struct open_bucket *ob, *ob2;
947         unsigned i, j;
948
949         open_bucket_for_each(c, obs, ob, i) {
950                 bool drop = !ca || ob->dev == ca->dev_idx;
951
952                 if (!drop && ob->ec) {
953                         mutex_lock(&ob->ec->lock);
954                         for (j = 0; j < ob->ec->new_stripe.key.v.nr_blocks; j++) {
955                                 if (!ob->ec->blocks[j])
956                                         continue;
957
958                                 ob2 = c->open_buckets + ob->ec->blocks[j];
959                                 drop |= ob2->dev == ca->dev_idx;
960                         }
961                         mutex_unlock(&ob->ec->lock);
962                 }
963
964                 if (drop)
965                         bch2_open_bucket_put(c, ob);
966                 else
967                         ob_push(c, &ptrs, ob);
968         }
969
970         *obs = ptrs;
971 }
972
973 void bch2_writepoint_stop(struct bch_fs *c, struct bch_dev *ca,
974                           struct write_point *wp)
975 {
976         mutex_lock(&wp->lock);
977         bch2_open_buckets_stop_dev(c, ca, &wp->ptrs);
978         mutex_unlock(&wp->lock);
979 }
980
981 static inline struct hlist_head *writepoint_hash(struct bch_fs *c,
982                                                  unsigned long write_point)
983 {
984         unsigned hash =
985                 hash_long(write_point, ilog2(ARRAY_SIZE(c->write_points_hash)));
986
987         return &c->write_points_hash[hash];
988 }
989
990 static struct write_point *__writepoint_find(struct hlist_head *head,
991                                              unsigned long write_point)
992 {
993         struct write_point *wp;
994
995         rcu_read_lock();
996         hlist_for_each_entry_rcu(wp, head, node)
997                 if (wp->write_point == write_point)
998                         goto out;
999         wp = NULL;
1000 out:
1001         rcu_read_unlock();
1002         return wp;
1003 }
1004
1005 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
1006 {
1007         u64 stranded    = c->write_points_nr * c->bucket_size_max;
1008         u64 free        = bch2_fs_usage_read_short(c).free;
1009
1010         return stranded * factor > free;
1011 }
1012
1013 static bool try_increase_writepoints(struct bch_fs *c)
1014 {
1015         struct write_point *wp;
1016
1017         if (c->write_points_nr == ARRAY_SIZE(c->write_points) ||
1018             too_many_writepoints(c, 32))
1019                 return false;
1020
1021         wp = c->write_points + c->write_points_nr++;
1022         hlist_add_head_rcu(&wp->node, writepoint_hash(c, wp->write_point));
1023         return true;
1024 }
1025
1026 static bool try_decrease_writepoints(struct bch_fs *c,
1027                                      unsigned old_nr)
1028 {
1029         struct write_point *wp;
1030
1031         mutex_lock(&c->write_points_hash_lock);
1032         if (c->write_points_nr < old_nr) {
1033                 mutex_unlock(&c->write_points_hash_lock);
1034                 return true;
1035         }
1036
1037         if (c->write_points_nr == 1 ||
1038             !too_many_writepoints(c, 8)) {
1039                 mutex_unlock(&c->write_points_hash_lock);
1040                 return false;
1041         }
1042
1043         wp = c->write_points + --c->write_points_nr;
1044
1045         hlist_del_rcu(&wp->node);
1046         mutex_unlock(&c->write_points_hash_lock);
1047
1048         bch2_writepoint_stop(c, NULL, wp);
1049         return true;
1050 }
1051
1052 static void bch2_trans_mutex_lock(struct btree_trans *trans,
1053                                   struct mutex *lock)
1054 {
1055         if (!mutex_trylock(lock)) {
1056                 bch2_trans_unlock(trans);
1057                 mutex_lock(lock);
1058         }
1059 }
1060
1061 static struct write_point *writepoint_find(struct btree_trans *trans,
1062                                            unsigned long write_point)
1063 {
1064         struct bch_fs *c = trans->c;
1065         struct write_point *wp, *oldest;
1066         struct hlist_head *head;
1067
1068         if (!(write_point & 1UL)) {
1069                 wp = (struct write_point *) write_point;
1070                 bch2_trans_mutex_lock(trans, &wp->lock);
1071                 return wp;
1072         }
1073
1074         head = writepoint_hash(c, write_point);
1075 restart_find:
1076         wp = __writepoint_find(head, write_point);
1077         if (wp) {
1078 lock_wp:
1079                 bch2_trans_mutex_lock(trans, &wp->lock);
1080                 if (wp->write_point == write_point)
1081                         goto out;
1082                 mutex_unlock(&wp->lock);
1083                 goto restart_find;
1084         }
1085 restart_find_oldest:
1086         oldest = NULL;
1087         for (wp = c->write_points;
1088              wp < c->write_points + c->write_points_nr; wp++)
1089                 if (!oldest || time_before64(wp->last_used, oldest->last_used))
1090                         oldest = wp;
1091
1092         bch2_trans_mutex_lock(trans, &oldest->lock);
1093         bch2_trans_mutex_lock(trans, &c->write_points_hash_lock);
1094         if (oldest >= c->write_points + c->write_points_nr ||
1095             try_increase_writepoints(c)) {
1096                 mutex_unlock(&c->write_points_hash_lock);
1097                 mutex_unlock(&oldest->lock);
1098                 goto restart_find_oldest;
1099         }
1100
1101         wp = __writepoint_find(head, write_point);
1102         if (wp && wp != oldest) {
1103                 mutex_unlock(&c->write_points_hash_lock);
1104                 mutex_unlock(&oldest->lock);
1105                 goto lock_wp;
1106         }
1107
1108         wp = oldest;
1109         hlist_del_rcu(&wp->node);
1110         wp->write_point = write_point;
1111         hlist_add_head_rcu(&wp->node, head);
1112         mutex_unlock(&c->write_points_hash_lock);
1113 out:
1114         wp->last_used = sched_clock();
1115         return wp;
1116 }
1117
1118 /*
1119  * Get us an open_bucket we can allocate from, return with it locked:
1120  */
1121 struct write_point *bch2_alloc_sectors_start_trans(struct btree_trans *trans,
1122                                 unsigned target,
1123                                 unsigned erasure_code,
1124                                 struct write_point_specifier write_point,
1125                                 struct bch_devs_list *devs_have,
1126                                 unsigned nr_replicas,
1127                                 unsigned nr_replicas_required,
1128                                 enum alloc_reserve reserve,
1129                                 unsigned flags,
1130                                 struct closure *cl)
1131 {
1132         struct bch_fs *c = trans->c;
1133         struct write_point *wp;
1134         struct open_bucket *ob;
1135         struct open_buckets ptrs;
1136         unsigned nr_effective, write_points_nr;
1137         unsigned ob_flags = 0;
1138         bool have_cache;
1139         int ret;
1140         int i;
1141
1142         if (!(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS))
1143                 ob_flags |= BUCKET_ALLOC_USE_DURABILITY;
1144
1145         BUG_ON(!nr_replicas || !nr_replicas_required);
1146 retry:
1147         ptrs.nr         = 0;
1148         nr_effective    = 0;
1149         write_points_nr = c->write_points_nr;
1150         have_cache      = false;
1151
1152         wp = writepoint_find(trans, write_point.v);
1153
1154         if (wp->data_type == BCH_DATA_user)
1155                 ob_flags |= BUCKET_MAY_ALLOC_PARTIAL;
1156
1157         /* metadata may not allocate on cache devices: */
1158         if (wp->data_type != BCH_DATA_user)
1159                 have_cache = true;
1160
1161         if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
1162                 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1163                                               target, erasure_code,
1164                                               nr_replicas, &nr_effective,
1165                                               &have_cache, reserve,
1166                                               ob_flags, cl);
1167         } else {
1168                 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1169                                               target, erasure_code,
1170                                               nr_replicas, &nr_effective,
1171                                               &have_cache, reserve,
1172                                               ob_flags, NULL);
1173                 if (!ret ||
1174                     bch2_err_matches(ret, BCH_ERR_transaction_restart))
1175                         goto alloc_done;
1176
1177                 ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
1178                                               0, erasure_code,
1179                                               nr_replicas, &nr_effective,
1180                                               &have_cache, reserve,
1181                                               ob_flags, cl);
1182         }
1183 alloc_done:
1184         BUG_ON(!ret && nr_effective < nr_replicas);
1185
1186         if (erasure_code && !ec_open_bucket(c, &ptrs))
1187                 pr_debug("failed to get ec bucket: ret %u", ret);
1188
1189         if (ret == -BCH_ERR_insufficient_devices &&
1190             nr_effective >= nr_replicas_required)
1191                 ret = 0;
1192
1193         if (ret)
1194                 goto err;
1195
1196         /* Free buckets we didn't use: */
1197         open_bucket_for_each(c, &wp->ptrs, ob, i)
1198                 open_bucket_free_unused(c, wp, ob);
1199
1200         wp->ptrs = ptrs;
1201
1202         wp->sectors_free = UINT_MAX;
1203
1204         open_bucket_for_each(c, &wp->ptrs, ob, i)
1205                 wp->sectors_free = min(wp->sectors_free, ob->sectors_free);
1206
1207         BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
1208
1209         return wp;
1210 err:
1211         open_bucket_for_each(c, &wp->ptrs, ob, i)
1212                 if (ptrs.nr < ARRAY_SIZE(ptrs.v))
1213                         ob_push(c, &ptrs, ob);
1214                 else
1215                         open_bucket_free_unused(c, wp, ob);
1216         wp->ptrs = ptrs;
1217
1218         mutex_unlock(&wp->lock);
1219
1220         if (bch2_err_matches(ret, BCH_ERR_freelist_empty) &&
1221             try_decrease_writepoints(c, write_points_nr))
1222                 goto retry;
1223
1224         if (bch2_err_matches(ret, BCH_ERR_open_buckets_empty) ||
1225             bch2_err_matches(ret, BCH_ERR_freelist_empty))
1226                 return cl ? ERR_PTR(-EAGAIN) : ERR_PTR(-ENOSPC);
1227
1228         if (bch2_err_matches(ret, BCH_ERR_insufficient_devices))
1229                 return ERR_PTR(-EROFS);
1230
1231         return ERR_PTR(ret);
1232 }
1233
1234 struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
1235                                 unsigned target,
1236                                 unsigned erasure_code,
1237                                 struct write_point_specifier write_point,
1238                                 struct bch_devs_list *devs_have,
1239                                 unsigned nr_replicas,
1240                                 unsigned nr_replicas_required,
1241                                 enum alloc_reserve reserve,
1242                                 unsigned flags,
1243                                 struct closure *cl)
1244 {
1245         struct write_point *wp;
1246
1247         bch2_trans_do(c, NULL, NULL, 0,
1248                       PTR_ERR_OR_ZERO(wp = bch2_alloc_sectors_start_trans(&trans, target,
1249                                                         erasure_code,
1250                                                         write_point,
1251                                                         devs_have,
1252                                                         nr_replicas,
1253                                                         nr_replicas_required,
1254                                                         reserve,
1255                                                         flags, cl)));
1256         return wp;
1257
1258 }
1259
1260 struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *c, struct open_bucket *ob)
1261 {
1262         struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1263
1264         return (struct bch_extent_ptr) {
1265                 .type   = 1 << BCH_EXTENT_ENTRY_ptr,
1266                 .gen    = ob->gen,
1267                 .dev    = ob->dev,
1268                 .offset = bucket_to_sector(ca, ob->bucket) +
1269                         ca->mi.bucket_size -
1270                         ob->sectors_free,
1271         };
1272 }
1273
1274 /*
1275  * Append pointers to the space we just allocated to @k, and mark @sectors space
1276  * as allocated out of @ob
1277  */
1278 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
1279                                     struct bkey_i *k, unsigned sectors,
1280                                     bool cached)
1281
1282 {
1283         struct open_bucket *ob;
1284         unsigned i;
1285
1286         BUG_ON(sectors > wp->sectors_free);
1287         wp->sectors_free -= sectors;
1288
1289         open_bucket_for_each(c, &wp->ptrs, ob, i) {
1290                 struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
1291                 struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
1292
1293                 ptr.cached = cached ||
1294                         (!ca->mi.durability &&
1295                          wp->data_type == BCH_DATA_user);
1296
1297                 bch2_bkey_append_ptr(k, ptr);
1298
1299                 BUG_ON(sectors > ob->sectors_free);
1300                 ob->sectors_free -= sectors;
1301         }
1302 }
1303
1304 /*
1305  * Append pointers to the space we just allocated to @k, and mark @sectors space
1306  * as allocated out of @ob
1307  */
1308 void bch2_alloc_sectors_done(struct bch_fs *c, struct write_point *wp)
1309 {
1310         struct open_buckets ptrs = { .nr = 0 }, keep = { .nr = 0 };
1311         struct open_bucket *ob;
1312         unsigned i;
1313
1314         open_bucket_for_each(c, &wp->ptrs, ob, i)
1315                 ob_push(c, !ob->sectors_free ? &ptrs : &keep, ob);
1316         wp->ptrs = keep;
1317
1318         mutex_unlock(&wp->lock);
1319
1320         bch2_open_buckets_put(c, &ptrs);
1321 }
1322
1323 static inline void writepoint_init(struct write_point *wp,
1324                                    enum bch_data_type type)
1325 {
1326         mutex_init(&wp->lock);
1327         wp->data_type = type;
1328 }
1329
1330 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
1331 {
1332         struct open_bucket *ob;
1333         struct write_point *wp;
1334
1335         mutex_init(&c->write_points_hash_lock);
1336         c->write_points_nr = ARRAY_SIZE(c->write_points);
1337
1338         /* open bucket 0 is a sentinal NULL: */
1339         spin_lock_init(&c->open_buckets[0].lock);
1340
1341         for (ob = c->open_buckets + 1;
1342              ob < c->open_buckets + ARRAY_SIZE(c->open_buckets); ob++) {
1343                 spin_lock_init(&ob->lock);
1344                 c->open_buckets_nr_free++;
1345
1346                 ob->freelist = c->open_buckets_freelist;
1347                 c->open_buckets_freelist = ob - c->open_buckets;
1348         }
1349
1350         writepoint_init(&c->btree_write_point,          BCH_DATA_btree);
1351         writepoint_init(&c->rebalance_write_point,      BCH_DATA_user);
1352         writepoint_init(&c->copygc_write_point,         BCH_DATA_user);
1353
1354         for (wp = c->write_points;
1355              wp < c->write_points + c->write_points_nr; wp++) {
1356                 writepoint_init(wp, BCH_DATA_user);
1357
1358                 wp->last_used   = sched_clock();
1359                 wp->write_point = (unsigned long) wp;
1360                 hlist_add_head_rcu(&wp->node,
1361                                    writepoint_hash(c, wp->write_point));
1362         }
1363 }
1364
1365 void bch2_open_buckets_to_text(struct printbuf *out, struct bch_fs *c)
1366 {
1367         struct open_bucket *ob;
1368
1369         for (ob = c->open_buckets;
1370              ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
1371              ob++) {
1372                 spin_lock(&ob->lock);
1373                 if (ob->valid && !ob->on_partial_list) {
1374                         prt_printf(out, "%zu ref %u type %s %u:%llu:%u\n",
1375                                ob - c->open_buckets,
1376                                atomic_read(&ob->pin),
1377                                bch2_data_types[ob->data_type],
1378                                ob->dev, ob->bucket, ob->gen);
1379                 }
1380                 spin_unlock(&ob->lock);
1381         }
1382 }