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