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