]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/alloc_foreground.c
Update bcachefs sources to cd779e0cc5 bcachefs: Skip inode unpack/pack in bch2_extent...
[bcachefs-tools-debian] / libbcachefs / alloc_foreground.c
index dcbe04040a39c913e199e33f97e27057b05ef881..a9e0c7397292fcab2e6dbcac08b7dd335c6c0147 100644 (file)
@@ -1,75 +1,45 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * Primary bucket allocation code
- *
  * Copyright 2012 Google, Inc.
  *
- * Allocation in bcache is done in terms of buckets:
- *
- * Each bucket has associated an 8 bit gen; this gen corresponds to the gen in
- * btree pointers - they must match for the pointer to be considered valid.
- *
- * Thus (assuming a bucket has no dirty data or metadata in it) we can reuse a
- * bucket simply by incrementing its gen.
- *
- * The gens (along with the priorities; it's really the gens are important but
- * the code is named as if it's the priorities) are written in an arbitrary list
- * of buckets on disk, with a pointer to them in the journal header.
- *
- * When we invalidate a bucket, we have to write its new gen to disk and wait
- * for that write to complete before we use it - otherwise after a crash we
- * could have pointers that appeared to be good but pointed to data that had
- * been overwritten.
- *
- * Since the gens and priorities are all stored contiguously on disk, we can
- * batch this up: We fill up the free_inc list with freshly invalidated buckets,
- * call prio_write(), and when prio_write() finishes we pull buckets off the
- * free_inc list and optionally discard them.
- *
- * free_inc isn't the only freelist - if it was, we'd often have to sleep while
- * priorities and gens were being written before we could allocate. c->free is a
- * smaller freelist, and buckets on that list are always ready to be used.
- *
- * If we've got discards enabled, that happens when a bucket moves from the
- * free_inc list to the free list.
- *
- * It's important to ensure that gens don't wrap around - with respect to
- * either the oldest gen in the btree or the gen on disk. This is quite
- * difficult to do in practice, but we explicitly guard against it anyways - if
- * a bucket is in danger of wrapping around we simply skip invalidating it that
- * time around, and we garbage collect or rewrite the priorities sooner than we
- * would have otherwise.
+ * Foreground allocator code: allocate buckets from freelist, and allocate in
+ * sector granularity from writepoints.
  *
  * bch2_bucket_alloc() allocates a single bucket from a specific device.
  *
  * bch2_bucket_alloc_set() allocates one or more buckets from different devices
  * in a given filesystem.
- *
- * invalidate_buckets() drives all the processes described above. It's called
- * from bch2_bucket_alloc() and a few other places that need to make sure free
- * buckets are ready.
- *
- * invalidate_buckets_(lru|fifo)() find buckets that are available to be
- * invalidated, and then invalidate them and stick them on the free_inc list -
- * in either lru or fifo order.
  */
 
 #include "bcachefs.h"
 #include "alloc_background.h"
 #include "alloc_foreground.h"
+#include "btree_iter.h"
+#include "btree_update.h"
 #include "btree_gc.h"
 #include "buckets.h"
+#include "buckets_waiting_for_journal.h"
 #include "clock.h"
 #include "debug.h"
 #include "disk_groups.h"
 #include "ec.h"
+#include "error.h"
 #include "io.h"
+#include "journal.h"
+#include "movinggc.h"
 
 #include <linux/math64.h>
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
 #include <trace/events/bcachefs.h>
 
+const char * const bch2_alloc_reserves[] = {
+#define x(t) #t,
+       BCH_ALLOC_RESERVES()
+#undef x
+       NULL
+};
+
 /*
  * Open buckets represent a bucket that's currently being allocated from.  They
  * serve two purposes:
  * reference _after_ doing the index update that makes its allocation reachable.
  */
 
+static void bch2_open_bucket_hash_add(struct bch_fs *c, struct open_bucket *ob)
+{
+       open_bucket_idx_t idx = ob - c->open_buckets;
+       open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
+
+       ob->hash = *slot;
+       *slot = idx;
+}
+
+static void bch2_open_bucket_hash_remove(struct bch_fs *c, struct open_bucket *ob)
+{
+       open_bucket_idx_t idx = ob - c->open_buckets;
+       open_bucket_idx_t *slot = open_bucket_hashslot(c, ob->dev, ob->bucket);
+
+       while (*slot != idx) {
+               BUG_ON(!*slot);
+               slot = &c->open_buckets[*slot].hash;
+       }
+
+       *slot = ob->hash;
+       ob->hash = 0;
+}
+
 void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
 {
-       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
 
        if (ob->ec) {
                bch2_ec_bucket_written(c, ob);
@@ -98,18 +91,20 @@ void __bch2_open_bucket_put(struct bch_fs *c, struct open_bucket *ob)
        percpu_down_read(&c->mark_lock);
        spin_lock(&ob->lock);
 
-       bch2_mark_alloc_bucket(c, ca, PTR_BUCKET_NR(ca, &ob->ptr),
-                              false, gc_pos_alloc(c, ob), 0);
        ob->valid = false;
-       ob->type = 0;
+       ob->data_type = 0;
 
        spin_unlock(&ob->lock);
        percpu_up_read(&c->mark_lock);
 
        spin_lock(&c->freelist_lock);
+       bch2_open_bucket_hash_remove(c, ob);
+
        ob->freelist = c->open_buckets_freelist;
        c->open_buckets_freelist = ob - c->open_buckets;
+
        c->open_buckets_nr_free++;
+       ca->nr_open_buckets--;
        spin_unlock(&c->freelist_lock);
 
        closure_wake_up(&c->open_buckets_wait);
@@ -123,8 +118,7 @@ void bch2_open_bucket_write_error(struct bch_fs *c,
        unsigned i;
 
        open_bucket_for_each(c, obs, ob, i)
-               if (ob->ptr.dev == dev &&
-                   ob->ec)
+               if (ob->dev == dev && ob->ec)
                        bch2_ec_bucket_cancel(c, ob);
 }
 
@@ -137,7 +131,7 @@ static struct open_bucket *bch2_open_bucket_alloc(struct bch_fs *c)
        ob = c->open_buckets + c->open_buckets_freelist;
        c->open_buckets_freelist = ob->freelist;
        atomic_set(&ob->pin, 1);
-       ob->type = 0;
+       ob->data_type = 0;
 
        c->open_buckets_nr_free--;
        return ob;
@@ -147,8 +141,8 @@ static void open_bucket_free_unused(struct bch_fs *c,
                                    struct write_point *wp,
                                    struct open_bucket *ob)
 {
-       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
-       bool may_realloc = wp->type == BCH_DATA_user;
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
+       bool may_realloc = wp->data_type == BCH_DATA_user;
 
        BUG_ON(ca->open_buckets_partial_nr >
               ARRAY_SIZE(ca->open_buckets_partial));
@@ -169,85 +163,62 @@ static void open_bucket_free_unused(struct bch_fs *c,
        }
 }
 
-static void verify_not_stale(struct bch_fs *c, const struct open_buckets *obs)
-{
-#ifdef CONFIG_BCACHEFS_DEBUG
-       struct open_bucket *ob;
-       unsigned i;
-
-       open_bucket_for_each(c, obs, ob, i) {
-               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
-
-               BUG_ON(ptr_stale(ca, &ob->ptr));
-       }
-#endif
-}
-
 /* _only_ for allocating the journal on a new device: */
 long bch2_bucket_alloc_new_fs(struct bch_dev *ca)
 {
-       struct bucket_array *buckets;
-       ssize_t b;
+       while (ca->new_fs_bucket_idx < ca->mi.nbuckets) {
+               u64 b = ca->new_fs_bucket_idx++;
 
-       rcu_read_lock();
-       buckets = bucket_array(ca);
+               if (!is_superblock_bucket(ca, b) &&
+                   (!ca->buckets_nouse || !test_bit(b, ca->buckets_nouse)))
+                       return b;
+       }
 
-       for (b = ca->mi.first_bucket; b < ca->mi.nbuckets; b++)
-               if (is_available_bucket(buckets->b[b].mark))
-                       goto success;
-       b = -1;
-success:
-       rcu_read_unlock();
-       return b;
+       return -1;
 }
 
 static inline unsigned open_buckets_reserved(enum alloc_reserve reserve)
 {
        switch (reserve) {
-       case RESERVE_BTREE:
-       case RESERVE_BTREE_MOVINGGC:
+       case RESERVE_btree:
+       case RESERVE_btree_movinggc:
                return 0;
-       case RESERVE_MOVINGGC:
+       case RESERVE_movinggc:
                return OPEN_BUCKETS_COUNT / 4;
        default:
                return OPEN_BUCKETS_COUNT / 2;
        }
 }
 
-/**
- * bch_bucket_alloc - allocate a single bucket from a specific device
- *
- * Returns index of bucket on success, 0 on failure
- * */
-struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
-                                     enum alloc_reserve reserve,
-                                     bool may_alloc_partial,
-                                     struct closure *cl)
+static struct open_bucket *__try_alloc_bucket(struct bch_fs *c, struct bch_dev *ca,
+                                             u64 bucket,
+                                             enum alloc_reserve reserve,
+                                             struct bch_alloc_v4 *a,
+                                             u64 *skipped_open,
+                                             u64 *skipped_need_journal_commit,
+                                             u64 *skipped_nouse,
+                                             struct closure *cl)
 {
-       struct bucket_array *buckets;
        struct open_bucket *ob;
-       long bucket = 0;
 
-       spin_lock(&c->freelist_lock);
+       if (unlikely(ca->buckets_nouse && test_bit(bucket, ca->buckets_nouse))) {
+               (*skipped_nouse)++;
+               return NULL;
+       }
 
-       if (may_alloc_partial) {
-               int i;
-
-               for (i = ca->open_buckets_partial_nr - 1; i >= 0; --i) {
-                       ob = c->open_buckets + ca->open_buckets_partial[i];
-
-                       if (reserve <= ob->alloc_reserve) {
-                               array_remove_item(ca->open_buckets_partial,
-                                                 ca->open_buckets_partial_nr,
-                                                 i);
-                               ob->on_partial_list = false;
-                               ob->alloc_reserve = reserve;
-                               spin_unlock(&c->freelist_lock);
-                               return ob;
-                       }
-               }
+       if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
+               (*skipped_open)++;
+               return NULL;
+       }
+
+       if (bch2_bucket_needs_journal_commit(&c->buckets_waiting_for_journal,
+                       c->journal.flushed_seq_ondisk, ca->dev_idx, bucket)) {
+               (*skipped_need_journal_commit)++;
+               return NULL;
        }
 
+       spin_lock(&c->freelist_lock);
+
        if (unlikely(c->open_buckets_nr_free <= open_buckets_reserved(reserve))) {
                if (cl)
                        closure_wait(&c->open_buckets_wait, cl);
@@ -256,53 +227,31 @@ struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
                        c->blocked_allocate_open_bucket = local_clock();
 
                spin_unlock(&c->freelist_lock);
-               trace_open_bucket_alloc_fail(ca, reserve);
-               return ERR_PTR(-OPEN_BUCKETS_EMPTY);
+               return ERR_PTR(-BCH_ERR_open_buckets_empty);
        }
 
-       if (likely(fifo_pop(&ca->free[RESERVE_NONE], bucket)))
-               goto out;
-
-       switch (reserve) {
-       case RESERVE_BTREE_MOVINGGC:
-       case RESERVE_MOVINGGC:
-               if (fifo_pop(&ca->free[RESERVE_MOVINGGC], bucket))
-                       goto out;
-               break;
-       default:
-               break;
+       /* Recheck under lock: */
+       if (bch2_bucket_is_open(c, ca->dev_idx, bucket)) {
+               spin_unlock(&c->freelist_lock);
+               (*skipped_open)++;
+               return NULL;
        }
 
-       if (cl)
-               closure_wait(&c->freelist_wait, cl);
-
-       if (!c->blocked_allocate)
-               c->blocked_allocate = local_clock();
-
-       spin_unlock(&c->freelist_lock);
-
-       trace_bucket_alloc_fail(ca, reserve);
-       return ERR_PTR(-FREELIST_EMPTY);
-out:
-       verify_not_on_freelist(c, ca, bucket);
-
        ob = bch2_open_bucket_alloc(c);
 
        spin_lock(&ob->lock);
-       buckets = bucket_array(ca);
 
        ob->valid       = true;
        ob->sectors_free = ca->mi.bucket_size;
        ob->alloc_reserve = reserve;
-       ob->ptr         = (struct bch_extent_ptr) {
-               .type   = 1 << BCH_EXTENT_ENTRY_ptr,
-               .gen    = buckets->b[bucket].mark.gen,
-               .offset = bucket_to_sector(ca, bucket),
-               .dev    = ca->dev_idx,
-       };
-
+       ob->dev         = ca->dev_idx;
+       ob->gen         = a->gen;
+       ob->bucket      = bucket;
        spin_unlock(&ob->lock);
 
+       ca->nr_open_buckets++;
+       bch2_open_bucket_hash_add(c, ob);
+
        if (c->blocked_allocate_open_bucket) {
                bch2_time_stats_update(
                        &c->times[BCH_TIME_blocked_allocate_open_bucket],
@@ -319,9 +268,343 @@ out:
 
        spin_unlock(&c->freelist_lock);
 
-       bch2_wake_allocator(ca);
+       return ob;
+}
+
+static struct open_bucket *try_alloc_bucket(struct btree_trans *trans, struct bch_dev *ca,
+                                           enum alloc_reserve reserve, u64 free_entry,
+                                           u64 *skipped_open,
+                                           u64 *skipped_need_journal_commit,
+                                           u64 *skipped_nouse,
+                                           struct bkey_s_c freespace_k,
+                                           struct closure *cl)
+{
+       struct bch_fs *c = trans->c;
+       struct btree_iter iter = { NULL };
+       struct bkey_s_c k;
+       struct open_bucket *ob;
+       struct bch_alloc_v4 a;
+       u64 b = free_entry & ~(~0ULL << 56);
+       unsigned genbits = free_entry >> 56;
+       struct printbuf buf = PRINTBUF;
+       int ret;
+
+       if (b < ca->mi.first_bucket || b >= ca->mi.nbuckets) {
+               prt_printf(&buf, "freespace btree has bucket outside allowed range %u-%llu\n"
+                      "  freespace key ",
+                       ca->mi.first_bucket, ca->mi.nbuckets);
+               bch2_bkey_val_to_text(&buf, c, freespace_k);
+               bch2_trans_inconsistent(trans, "%s", buf.buf);
+               ob = ERR_PTR(-EIO);
+               goto err;
+       }
+
+       bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc, POS(ca->dev_idx, b), BTREE_ITER_CACHED);
+       k = bch2_btree_iter_peek_slot(&iter);
+       ret = bkey_err(k);
+       if (ret) {
+               ob = ERR_PTR(ret);
+               goto err;
+       }
+
+       bch2_alloc_to_v4(k, &a);
+
+       if (genbits != (alloc_freespace_genbits(a) >> 56)) {
+               prt_printf(&buf, "bucket in freespace btree with wrong genbits (got %u should be %llu)\n"
+                      "  freespace key ",
+                      genbits, alloc_freespace_genbits(a) >> 56);
+               bch2_bkey_val_to_text(&buf, c, freespace_k);
+               prt_printf(&buf, "\n  ");
+               bch2_bkey_val_to_text(&buf, c, k);
+               bch2_trans_inconsistent(trans, "%s", buf.buf);
+               ob = ERR_PTR(-EIO);
+               goto err;
+
+       }
+
+       if (a.data_type != BCH_DATA_free) {
+               prt_printf(&buf, "non free bucket in freespace btree\n"
+                      "  freespace key ");
+               bch2_bkey_val_to_text(&buf, c, freespace_k);
+               prt_printf(&buf, "\n  ");
+               bch2_bkey_val_to_text(&buf, c, k);
+               bch2_trans_inconsistent(trans, "%s", buf.buf);
+               ob = ERR_PTR(-EIO);
+               goto err;
+       }
+
+       ob = __try_alloc_bucket(c, ca, b, reserve, &a,
+                               skipped_open,
+                               skipped_need_journal_commit,
+                               skipped_nouse,
+                               cl);
+       if (!ob)
+               iter.path->preserve = false;
+err:
+       set_btree_iter_dontneed(&iter);
+       bch2_trans_iter_exit(trans, &iter);
+       printbuf_exit(&buf);
+       return ob;
+}
+
+static struct open_bucket *try_alloc_partial_bucket(struct bch_fs *c, struct bch_dev *ca,
+                                                   enum alloc_reserve reserve)
+{
+       struct open_bucket *ob;
+       int i;
+
+       spin_lock(&c->freelist_lock);
+
+       for (i = ca->open_buckets_partial_nr - 1; i >= 0; --i) {
+               ob = c->open_buckets + ca->open_buckets_partial[i];
+
+               if (reserve <= ob->alloc_reserve) {
+                       array_remove_item(ca->open_buckets_partial,
+                                         ca->open_buckets_partial_nr,
+                                         i);
+                       ob->on_partial_list = false;
+                       ob->alloc_reserve = reserve;
+                       spin_unlock(&c->freelist_lock);
+                       return ob;
+               }
+       }
+
+       spin_unlock(&c->freelist_lock);
+       return NULL;
+}
+
+/*
+ * This path is for before the freespace btree is initialized:
+ *
+ * If ca->new_fs_bucket_idx is nonzero, we haven't yet marked superblock &
+ * journal buckets - journal buckets will be < ca->new_fs_bucket_idx
+ */
+static noinline struct open_bucket *
+bch2_bucket_alloc_early(struct btree_trans *trans,
+                       struct bch_dev *ca,
+                       enum alloc_reserve reserve,
+                       u64 *cur_bucket,
+                       u64 *buckets_seen,
+                       u64 *skipped_open,
+                       u64 *skipped_need_journal_commit,
+                       u64 *skipped_nouse,
+                       struct closure *cl)
+{
+       struct btree_iter iter;
+       struct bkey_s_c k;
+       struct open_bucket *ob = NULL;
+       int ret;
+
+       *cur_bucket = max_t(u64, *cur_bucket, ca->mi.first_bucket);
+       *cur_bucket = max_t(u64, *cur_bucket, ca->new_fs_bucket_idx);
+
+       for_each_btree_key_norestart(trans, iter, BTREE_ID_alloc, POS(ca->dev_idx, *cur_bucket),
+                          BTREE_ITER_SLOTS, k, ret) {
+               struct bch_alloc_v4 a;
+
+               if (bkey_cmp(k.k->p, POS(ca->dev_idx, ca->mi.nbuckets)) >= 0)
+                       break;
+
+               if (ca->new_fs_bucket_idx &&
+                   is_superblock_bucket(ca, k.k->p.offset))
+                       continue;
+
+               bch2_alloc_to_v4(k, &a);
+
+               if (a.data_type != BCH_DATA_free)
+                       continue;
+
+               (*buckets_seen)++;
+
+               ob = __try_alloc_bucket(trans->c, ca, k.k->p.offset, reserve, &a,
+                                       skipped_open,
+                                       skipped_need_journal_commit,
+                                       skipped_nouse,
+                                       cl);
+               if (ob)
+                       break;
+       }
+       bch2_trans_iter_exit(trans, &iter);
+
+       *cur_bucket = iter.pos.offset;
+
+       return ob ?: ERR_PTR(ret ?: -BCH_ERR_no_buckets_found);
+}
+
+static struct open_bucket *bch2_bucket_alloc_freelist(struct btree_trans *trans,
+                                                  struct bch_dev *ca,
+                                                  enum alloc_reserve reserve,
+                                                  u64 *cur_bucket,
+                                                  u64 *buckets_seen,
+                                                  u64 *skipped_open,
+                                                  u64 *skipped_need_journal_commit,
+                                                  u64 *skipped_nouse,
+                                                  struct closure *cl)
+{
+       struct btree_iter iter;
+       struct bkey_s_c k;
+       struct open_bucket *ob = NULL;
+       int ret;
+
+       BUG_ON(ca->new_fs_bucket_idx);
+
+       /*
+        * XXX:
+        * On transaction restart, we'd like to restart from the bucket we were
+        * at previously
+        */
+       for_each_btree_key_norestart(trans, iter, BTREE_ID_freespace,
+                                    POS(ca->dev_idx, *cur_bucket), 0, k, ret) {
+               if (k.k->p.inode != ca->dev_idx)
+                       break;
+
+               for (*cur_bucket = max(*cur_bucket, bkey_start_offset(k.k));
+                    *cur_bucket < k.k->p.offset;
+                    (*cur_bucket)++) {
+                       ret = btree_trans_too_many_iters(trans);
+                       if (ret)
+                               break;
+
+                       (*buckets_seen)++;
+
+                       ob = try_alloc_bucket(trans, ca, reserve,
+                                             *cur_bucket,
+                                             skipped_open,
+                                             skipped_need_journal_commit,
+                                             skipped_nouse,
+                                             k, cl);
+                       if (ob)
+                               break;
+               }
+
+               if (ob || ret)
+                       break;
+       }
+       bch2_trans_iter_exit(trans, &iter);
+
+       return ob ?: ERR_PTR(ret);
+}
+
+/**
+ * bch_bucket_alloc - allocate a single bucket from a specific device
+ *
+ * Returns index of bucket on success, 0 on failure
+ */
+static struct open_bucket *bch2_bucket_alloc_trans(struct btree_trans *trans,
+                                     struct bch_dev *ca,
+                                     enum alloc_reserve reserve,
+                                     bool may_alloc_partial,
+                                     struct closure *cl,
+                                     struct bch_dev_usage *usage)
+{
+       struct bch_fs *c = trans->c;
+       struct open_bucket *ob = NULL;
+       bool freespace_initialized = READ_ONCE(ca->mi.freespace_initialized);
+       u64 start = freespace_initialized ? 0 : ca->bucket_alloc_trans_early_cursor;
+       u64 avail;
+       u64 cur_bucket = start;
+       u64 buckets_seen = 0;
+       u64 skipped_open = 0;
+       u64 skipped_need_journal_commit = 0;
+       u64 skipped_nouse = 0;
+       bool waiting = false;
+again:
+       bch2_dev_usage_read_fast(ca, usage);
+       avail = dev_buckets_free(ca, *usage, reserve);
+
+       if (usage->d[BCH_DATA_need_discard].buckets > avail)
+               bch2_do_discards(c);
+
+       if (usage->d[BCH_DATA_need_gc_gens].buckets > avail)
+               bch2_do_gc_gens(c);
+
+       if (should_invalidate_buckets(ca, *usage))
+               bch2_do_invalidates(c);
+
+       if (!avail) {
+               if (cl && !waiting) {
+                       closure_wait(&c->freelist_wait, cl);
+                       waiting = true;
+                       goto again;
+               }
+
+               if (!c->blocked_allocate)
+                       c->blocked_allocate = local_clock();
+
+               ob = ERR_PTR(-BCH_ERR_freelist_empty);
+               goto err;
+       }
+
+       if (waiting)
+               closure_wake_up(&c->freelist_wait);
+
+       if (may_alloc_partial) {
+               ob = try_alloc_partial_bucket(c, ca, reserve);
+               if (ob)
+                       return ob;
+       }
+
+       ob = likely(ca->mi.freespace_initialized)
+               ? bch2_bucket_alloc_freelist(trans, ca, reserve,
+                                       &cur_bucket,
+                                       &buckets_seen,
+                                       &skipped_open,
+                                       &skipped_need_journal_commit,
+                                       &skipped_nouse,
+                                       cl)
+               : bch2_bucket_alloc_early(trans, ca, reserve,
+                                       &cur_bucket,
+                                       &buckets_seen,
+                                       &skipped_open,
+                                       &skipped_need_journal_commit,
+                                       &skipped_nouse,
+                                       cl);
+
+       if (skipped_need_journal_commit * 2 > avail)
+               bch2_journal_flush_async(&c->journal, NULL);
+
+       if (!ob && !freespace_initialized && start) {
+               start = cur_bucket = 0;
+               goto again;
+       }
+
+       if (!freespace_initialized)
+               ca->bucket_alloc_trans_early_cursor = cur_bucket;
+err:
+       if (!ob)
+               ob = ERR_PTR(-BCH_ERR_no_buckets_found);
+
+       if (!IS_ERR(ob))
+               trace_and_count(c, bucket_alloc, ca, bch2_alloc_reserves[reserve],
+                               may_alloc_partial, ob->bucket);
+       else if (!bch2_err_matches(PTR_ERR(ob), BCH_ERR_transaction_restart))
+               trace_and_count(c, bucket_alloc_fail,
+                               ca, bch2_alloc_reserves[reserve],
+                               usage->d[BCH_DATA_free].buckets,
+                               avail,
+                               bch2_copygc_wait_amount(c),
+                               c->copygc_wait - atomic64_read(&c->io_clock[WRITE].now),
+                               buckets_seen,
+                               skipped_open,
+                               skipped_need_journal_commit,
+                               skipped_nouse,
+                               cl == NULL,
+                               bch2_err_str(PTR_ERR(ob)));
 
-       trace_bucket_alloc(ca, reserve);
+       return ob;
+}
+
+struct open_bucket *bch2_bucket_alloc(struct bch_fs *c, struct bch_dev *ca,
+                                     enum alloc_reserve reserve,
+                                     bool may_alloc_partial,
+                                     struct closure *cl)
+{
+       struct bch_dev_usage usage;
+       struct open_bucket *ob;
+
+       bch2_trans_do(c, NULL, NULL, 0,
+                     PTR_ERR_OR_ZERO(ob = bch2_bucket_alloc_trans(&trans, ca, reserve,
+                                                       may_alloc_partial, cl, &usage)));
        return ob;
 }
 
@@ -348,11 +631,12 @@ struct dev_alloc_list bch2_dev_alloc_list(struct bch_fs *c,
        return ret;
 }
 
-void bch2_dev_stripe_increment(struct bch_dev *ca,
-                              struct dev_stripe_state *stripe)
+static inline void bch2_dev_stripe_increment_inlined(struct bch_dev *ca,
+                              struct dev_stripe_state *stripe,
+                              struct bch_dev_usage *usage)
 {
        u64 *v = stripe->next_alloc + ca->dev_idx;
-       u64 free_space = dev_buckets_free(ca);
+       u64 free_space = dev_buckets_available(ca, RESERVE_none);
        u64 free_space_inv = free_space
                ? div64_u64(1ULL << 48, free_space)
                : 1ULL << 48;
@@ -368,6 +652,15 @@ void bch2_dev_stripe_increment(struct bch_dev *ca,
                *v = *v < scale ? 0 : *v - scale;
 }
 
+void bch2_dev_stripe_increment(struct bch_dev *ca,
+                              struct dev_stripe_state *stripe)
+{
+       struct bch_dev_usage usage;
+
+       bch2_dev_usage_read_fast(ca, &usage);
+       bch2_dev_stripe_increment_inlined(ca, stripe, &usage);
+}
+
 #define BUCKET_MAY_ALLOC_PARTIAL       (1 << 0)
 #define BUCKET_ALLOC_USE_DURABILITY    (1 << 1)
 
@@ -380,9 +673,9 @@ static void add_new_bucket(struct bch_fs *c,
                           struct open_bucket *ob)
 {
        unsigned durability =
-               bch_dev_bkey_exists(c, ob->ptr.dev)->mi.durability;
+               bch_dev_bkey_exists(c, ob->dev)->mi.durability;
 
-       __clear_bit(ob->ptr.dev, devs_may_alloc->d);
+       __clear_bit(ob->dev, devs_may_alloc->d);
        *nr_effective   += (flags & BUCKET_ALLOC_USE_DURABILITY)
                ? durability : 1;
        *have_cache     |= !durability;
@@ -390,8 +683,7 @@ static void add_new_bucket(struct bch_fs *c,
        ob_push(c, ptrs, ob);
 }
 
-enum bucket_alloc_ret
-bch2_bucket_alloc_set(struct bch_fs *c,
+static int bch2_bucket_alloc_set_trans(struct btree_trans *trans,
                      struct open_buckets *ptrs,
                      struct dev_stripe_state *stripe,
                      struct bch_devs_mask *devs_may_alloc,
@@ -402,46 +694,79 @@ bch2_bucket_alloc_set(struct bch_fs *c,
                      unsigned flags,
                      struct closure *cl)
 {
+       struct bch_fs *c = trans->c;
        struct dev_alloc_list devs_sorted =
                bch2_dev_alloc_list(c, stripe, devs_may_alloc);
+       unsigned dev;
        struct bch_dev *ca;
-       enum bucket_alloc_ret ret = INSUFFICIENT_DEVICES;
+       int ret = -BCH_ERR_insufficient_devices;
        unsigned i;
 
        BUG_ON(*nr_effective >= nr_replicas);
 
        for (i = 0; i < devs_sorted.nr; i++) {
+               struct bch_dev_usage usage;
                struct open_bucket *ob;
 
-               ca = rcu_dereference(c->devs[devs_sorted.devs[i]]);
+               dev = devs_sorted.devs[i];
+
+               rcu_read_lock();
+               ca = rcu_dereference(c->devs[dev]);
+               if (ca)
+                       percpu_ref_get(&ca->ref);
+               rcu_read_unlock();
+
                if (!ca)
                        continue;
 
-               if (!ca->mi.durability && *have_cache)
+               if (!ca->mi.durability && *have_cache) {
+                       percpu_ref_put(&ca->ref);
                        continue;
+               }
 
-               ob = bch2_bucket_alloc(c, ca, reserve,
-                               flags & BUCKET_MAY_ALLOC_PARTIAL, cl);
-               if (IS_ERR(ob)) {
-                       ret = -PTR_ERR(ob);
+               ob = bch2_bucket_alloc_trans(trans, ca, reserve,
+                               flags & BUCKET_MAY_ALLOC_PARTIAL, cl, &usage);
+               if (!IS_ERR(ob))
+                       bch2_dev_stripe_increment_inlined(ca, stripe, &usage);
+               percpu_ref_put(&ca->ref);
 
-                       if (cl)
-                               return ret;
+               if (IS_ERR(ob)) {
+                       ret = PTR_ERR(ob);
+                       if (bch2_err_matches(ret, BCH_ERR_transaction_restart) || cl)
+                               break;
                        continue;
                }
 
                add_new_bucket(c, ptrs, devs_may_alloc,
                               nr_effective, have_cache, flags, ob);
 
-               bch2_dev_stripe_increment(ca, stripe);
-
-               if (*nr_effective >= nr_replicas)
-                       return ALLOC_SUCCESS;
+               if (*nr_effective >= nr_replicas) {
+                       ret = 0;
+                       break;
+               }
        }
 
        return ret;
 }
 
+int bch2_bucket_alloc_set(struct bch_fs *c,
+                     struct open_buckets *ptrs,
+                     struct dev_stripe_state *stripe,
+                     struct bch_devs_mask *devs_may_alloc,
+                     unsigned nr_replicas,
+                     unsigned *nr_effective,
+                     bool *have_cache,
+                     enum alloc_reserve reserve,
+                     unsigned flags,
+                     struct closure *cl)
+{
+       return bch2_trans_do(c, NULL, NULL, 0,
+                     bch2_bucket_alloc_set_trans(&trans, ptrs, stripe,
+                                             devs_may_alloc, nr_replicas,
+                                             nr_effective, have_cache, reserve,
+                                             flags, cl));
+}
+
 /* Allocate from stripes: */
 
 /*
@@ -450,8 +775,7 @@ bch2_bucket_alloc_set(struct bch_fs *c,
  * it's to a device we don't want:
  */
 
-static enum bucket_alloc_ret
-bucket_alloc_from_stripe(struct bch_fs *c,
+static int bucket_alloc_from_stripe(struct bch_fs *c,
                         struct open_buckets *ptrs,
                         struct write_point *wp,
                         struct bch_devs_mask *devs_may_alloc,
@@ -489,16 +813,20 @@ bucket_alloc_from_stripe(struct bch_fs *c,
        devs_sorted = bch2_dev_alloc_list(c, &wp->stripe, devs_may_alloc);
 
        for (i = 0; i < devs_sorted.nr; i++)
-               open_bucket_for_each(c, &h->s->blocks, ob, ec_idx)
-                       if (ob->ptr.dev == devs_sorted.devs[i] &&
-                           !test_and_set_bit(h->s->data_block_idx[ec_idx],
-                                             h->s->blocks_allocated))
+               for (ec_idx = 0; ec_idx < h->s->nr_data; ec_idx++) {
+                       if (!h->s->blocks[ec_idx])
+                               continue;
+
+                       ob = c->open_buckets + h->s->blocks[ec_idx];
+                       if (ob->dev == devs_sorted.devs[i] &&
+                           !test_and_set_bit(ec_idx, h->s->blocks_allocated))
                                goto got_bucket;
+               }
        goto out_put_head;
 got_bucket:
-       ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+       ca = bch_dev_bkey_exists(c, ob->dev);
 
-       ob->ec_idx      = h->s->data_block_idx[ec_idx];
+       ob->ec_idx      = ec_idx;
        ob->ec          = h->s;
 
        add_new_bucket(c, ptrs, devs_may_alloc,
@@ -526,12 +854,12 @@ static void get_buckets_from_writepoint(struct bch_fs *c,
        unsigned i;
 
        open_bucket_for_each(c, &wp->ptrs, ob, i) {
-               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
 
                if (*nr_effective < nr_replicas &&
-                   test_bit(ob->ptr.dev, devs_may_alloc->d) &&
+                   test_bit(ob->dev, devs_may_alloc->d) &&
                    (ca->mi.durability ||
-                    (wp->type == BCH_DATA_user && !*have_cache)) &&
+                    (wp->data_type == BCH_DATA_user && !*have_cache)) &&
                    (ob->ec || !need_ec)) {
                        add_new_bucket(c, ptrs, devs_may_alloc,
                                       nr_effective, have_cache,
@@ -543,8 +871,7 @@ static void get_buckets_from_writepoint(struct bch_fs *c,
        wp->ptrs = ptrs_skip;
 }
 
-static enum bucket_alloc_ret
-open_bucket_add_buckets(struct bch_fs *c,
+static int open_bucket_add_buckets(struct btree_trans *trans,
                        struct open_buckets *ptrs,
                        struct write_point *wp,
                        struct bch_devs_list *devs_have,
@@ -557,14 +884,15 @@ open_bucket_add_buckets(struct bch_fs *c,
                        unsigned flags,
                        struct closure *_cl)
 {
+       struct bch_fs *c = trans->c;
        struct bch_devs_mask devs;
        struct open_bucket *ob;
        struct closure *cl = NULL;
-       enum bucket_alloc_ret ret;
+       int ret;
        unsigned i;
 
        rcu_read_lock();
-       devs = target_rw_devs(c, wp->type, target);
+       devs = target_rw_devs(c, wp->data_type, target);
        rcu_read_unlock();
 
        /* Don't allocate from devices we already have pointers to: */
@@ -572,7 +900,7 @@ open_bucket_add_buckets(struct bch_fs *c,
                __clear_bit(devs_have->devs[i], devs.d);
 
        open_bucket_for_each(c, ptrs, ob, i)
-               __clear_bit(ob->ptr.dev, devs.d);
+               __clear_bit(ob->dev, devs.d);
 
        if (erasure_code) {
                if (!ec_open_bucket(c, ptrs)) {
@@ -588,8 +916,9 @@ open_bucket_add_buckets(struct bch_fs *c,
                                                 target, erasure_code,
                                                 nr_replicas, nr_effective,
                                                 have_cache, flags, _cl);
-                       if (ret == FREELIST_EMPTY ||
-                           ret == OPEN_BUCKETS_EMPTY)
+                       if (bch2_err_matches(ret, BCH_ERR_transaction_restart) ||
+                           bch2_err_matches(ret, BCH_ERR_freelist_empty) ||
+                           bch2_err_matches(ret, BCH_ERR_open_buckets_empty))
                                return ret;
                        if (*nr_effective >= nr_replicas)
                                return 0;
@@ -602,25 +931,22 @@ open_bucket_add_buckets(struct bch_fs *c,
        if (*nr_effective >= nr_replicas)
                return 0;
 
-       percpu_down_read(&c->mark_lock);
-       rcu_read_lock();
-
 retry_blocking:
        /*
         * Try nonblocking first, so that if one device is full we'll try from
         * other devices:
         */
-       ret = bch2_bucket_alloc_set(c, ptrs, &wp->stripe, &devs,
+       ret = bch2_bucket_alloc_set_trans(trans, ptrs, &wp->stripe, &devs,
                                nr_replicas, nr_effective, have_cache,
                                reserve, flags, cl);
-       if (ret && ret != INSUFFICIENT_DEVICES && !cl && _cl) {
+       if (ret &&
+           !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
+           !bch2_err_matches(ret, BCH_ERR_insufficient_devices) &&
+           !cl && _cl) {
                cl = _cl;
                goto retry_blocking;
        }
 
-       rcu_read_unlock();
-       percpu_up_read(&c->mark_lock);
-
        return ret;
 }
 
@@ -632,14 +958,17 @@ void bch2_open_buckets_stop_dev(struct bch_fs *c, struct bch_dev *ca,
        unsigned i, j;
 
        open_bucket_for_each(c, obs, ob, i) {
-               bool drop = !ca || ob->ptr.dev == ca->dev_idx;
+               bool drop = !ca || ob->dev == ca->dev_idx;
 
                if (!drop && ob->ec) {
                        mutex_lock(&ob->ec->lock);
-                       open_bucket_for_each(c, &ob->ec->blocks, ob2, j)
-                               drop |= ob2->ptr.dev == ca->dev_idx;
-                       open_bucket_for_each(c, &ob->ec->parity, ob2, j)
-                               drop |= ob2->ptr.dev == ca->dev_idx;
+                       for (j = 0; j < ob->ec->new_stripe.key.v.nr_blocks; j++) {
+                               if (!ob->ec->blocks[j])
+                                       continue;
+
+                               ob2 = c->open_buckets + ob->ec->blocks[j];
+                               drop |= ob2->dev == ca->dev_idx;
+                       }
                        mutex_unlock(&ob->ec->lock);
                }
 
@@ -674,11 +1003,14 @@ static struct write_point *__writepoint_find(struct hlist_head *head,
 {
        struct write_point *wp;
 
+       rcu_read_lock();
        hlist_for_each_entry_rcu(wp, head, node)
                if (wp->write_point == write_point)
-                       return wp;
-
-       return NULL;
+                       goto out;
+       wp = NULL;
+out:
+       rcu_read_unlock();
+       return wp;
 }
 
 static inline bool too_many_writepoints(struct bch_fs *c, unsigned factor)
@@ -728,15 +1060,25 @@ static bool try_decrease_writepoints(struct bch_fs *c,
        return true;
 }
 
-static struct write_point *writepoint_find(struct bch_fs *c,
+static void bch2_trans_mutex_lock(struct btree_trans *trans,
+                                 struct mutex *lock)
+{
+       if (!mutex_trylock(lock)) {
+               bch2_trans_unlock(trans);
+               mutex_lock(lock);
+       }
+}
+
+static struct write_point *writepoint_find(struct btree_trans *trans,
                                           unsigned long write_point)
 {
+       struct bch_fs *c = trans->c;
        struct write_point *wp, *oldest;
        struct hlist_head *head;
 
        if (!(write_point & 1UL)) {
                wp = (struct write_point *) write_point;
-               mutex_lock(&wp->lock);
+               bch2_trans_mutex_lock(trans, &wp->lock);
                return wp;
        }
 
@@ -745,7 +1087,7 @@ restart_find:
        wp = __writepoint_find(head, write_point);
        if (wp) {
 lock_wp:
-               mutex_lock(&wp->lock);
+               bch2_trans_mutex_lock(trans, &wp->lock);
                if (wp->write_point == write_point)
                        goto out;
                mutex_unlock(&wp->lock);
@@ -758,8 +1100,8 @@ restart_find_oldest:
                if (!oldest || time_before64(wp->last_used, oldest->last_used))
                        oldest = wp;
 
-       mutex_lock(&oldest->lock);
-       mutex_lock(&c->write_points_hash_lock);
+       bch2_trans_mutex_lock(trans, &oldest->lock);
+       bch2_trans_mutex_lock(trans, &c->write_points_hash_lock);
        if (oldest >= c->write_points + c->write_points_nr ||
            try_increase_writepoints(c)) {
                mutex_unlock(&c->write_points_hash_lock);
@@ -780,14 +1122,14 @@ restart_find_oldest:
        hlist_add_head_rcu(&wp->node, head);
        mutex_unlock(&c->write_points_hash_lock);
 out:
-       wp->last_used = sched_clock();
+       wp->last_used = local_clock();
        return wp;
 }
 
 /*
  * Get us an open_bucket we can allocate from, return with it locked:
  */
-struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
+struct write_point *bch2_alloc_sectors_start_trans(struct btree_trans *trans,
                                unsigned target,
                                unsigned erasure_code,
                                struct write_point_specifier write_point,
@@ -798,13 +1140,14 @@ struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
                                unsigned flags,
                                struct closure *cl)
 {
+       struct bch_fs *c = trans->c;
        struct write_point *wp;
        struct open_bucket *ob;
        struct open_buckets ptrs;
        unsigned nr_effective, write_points_nr;
        unsigned ob_flags = 0;
        bool have_cache;
-       enum bucket_alloc_ret ret;
+       int ret;
        int i;
 
        if (!(flags & BCH_WRITE_ONLY_SPECIFIED_DEVS))
@@ -817,31 +1160,32 @@ retry:
        write_points_nr = c->write_points_nr;
        have_cache      = false;
 
-       wp = writepoint_find(c, write_point.v);
+       wp = writepoint_find(trans, write_point.v);
 
-       if (wp->type == BCH_DATA_user)
+       if (wp->data_type == BCH_DATA_user)
                ob_flags |= BUCKET_MAY_ALLOC_PARTIAL;
 
        /* metadata may not allocate on cache devices: */
-       if (wp->type != BCH_DATA_user)
+       if (wp->data_type != BCH_DATA_user)
                have_cache = true;
 
        if (!target || (flags & BCH_WRITE_ONLY_SPECIFIED_DEVS)) {
-               ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
+               ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
                                              target, erasure_code,
                                              nr_replicas, &nr_effective,
                                              &have_cache, reserve,
                                              ob_flags, cl);
        } else {
-               ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
+               ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
                                              target, erasure_code,
                                              nr_replicas, &nr_effective,
                                              &have_cache, reserve,
                                              ob_flags, NULL);
-               if (!ret)
+               if (!ret ||
+                   bch2_err_matches(ret, BCH_ERR_transaction_restart))
                        goto alloc_done;
 
-               ret = open_bucket_add_buckets(c, &ptrs, wp, devs_have,
+               ret = open_bucket_add_buckets(trans, &ptrs, wp, devs_have,
                                              0, erasure_code,
                                              nr_replicas, &nr_effective,
                                              &have_cache, reserve,
@@ -853,7 +1197,7 @@ alloc_done:
        if (erasure_code && !ec_open_bucket(c, &ptrs))
                pr_debug("failed to get ec bucket: ret %u", ret);
 
-       if (ret == INSUFFICIENT_DEVICES &&
+       if (ret == -BCH_ERR_insufficient_devices &&
            nr_effective >= nr_replicas_required)
                ret = 0;
 
@@ -873,8 +1217,6 @@ alloc_done:
 
        BUG_ON(!wp->sectors_free || wp->sectors_free == UINT_MAX);
 
-       verify_not_stale(c, &wp->ptrs);
-
        return wp;
 err:
        open_bucket_for_each(c, &wp->ptrs, ob, i)
@@ -886,19 +1228,60 @@ err:
 
        mutex_unlock(&wp->lock);
 
-       if (ret == FREELIST_EMPTY &&
+       if (bch2_err_matches(ret, BCH_ERR_freelist_empty) &&
            try_decrease_writepoints(c, write_points_nr))
                goto retry;
 
-       switch (ret) {
-       case OPEN_BUCKETS_EMPTY:
-       case FREELIST_EMPTY:
-               return cl ? ERR_PTR(-EAGAIN) : ERR_PTR(-ENOSPC);
-       case INSUFFICIENT_DEVICES:
+       if (bch2_err_matches(ret, BCH_ERR_open_buckets_empty) ||
+           bch2_err_matches(ret, BCH_ERR_freelist_empty))
+               return cl
+                       ? ERR_PTR(-EAGAIN)
+                       : ERR_PTR(-BCH_ERR_ENOSPC_bucket_alloc);
+
+       if (bch2_err_matches(ret, BCH_ERR_insufficient_devices))
                return ERR_PTR(-EROFS);
-       default:
-               BUG();
-       }
+
+       return ERR_PTR(ret);
+}
+
+struct write_point *bch2_alloc_sectors_start(struct bch_fs *c,
+                               unsigned target,
+                               unsigned erasure_code,
+                               struct write_point_specifier write_point,
+                               struct bch_devs_list *devs_have,
+                               unsigned nr_replicas,
+                               unsigned nr_replicas_required,
+                               enum alloc_reserve reserve,
+                               unsigned flags,
+                               struct closure *cl)
+{
+       struct write_point *wp;
+
+       bch2_trans_do(c, NULL, NULL, 0,
+                     PTR_ERR_OR_ZERO(wp = bch2_alloc_sectors_start_trans(&trans, target,
+                                                       erasure_code,
+                                                       write_point,
+                                                       devs_have,
+                                                       nr_replicas,
+                                                       nr_replicas_required,
+                                                       reserve,
+                                                       flags, cl)));
+       return wp;
+
+}
+
+struct bch_extent_ptr bch2_ob_ptr(struct bch_fs *c, struct open_bucket *ob)
+{
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
+
+       return (struct bch_extent_ptr) {
+               .type   = 1 << BCH_EXTENT_ENTRY_ptr,
+               .gen    = ob->gen,
+               .dev    = ob->dev,
+               .offset = bucket_to_sector(ca, ob->bucket) +
+                       ca->mi.bucket_size -
+                       ob->sectors_free,
+       };
 }
 
 /*
@@ -906,7 +1289,8 @@ err:
  * as allocated out of @ob
  */
 void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
-                                   struct bkey_i *k, unsigned sectors)
+                                   struct bkey_i *k, unsigned sectors,
+                                   bool cached)
 
 {
        struct open_bucket *ob;
@@ -916,14 +1300,14 @@ void bch2_alloc_sectors_append_ptrs(struct bch_fs *c, struct write_point *wp,
        wp->sectors_free -= sectors;
 
        open_bucket_for_each(c, &wp->ptrs, ob, i) {
-               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
-               struct bch_extent_ptr tmp = ob->ptr;
+               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->dev);
+               struct bch_extent_ptr ptr = bch2_ob_ptr(c, ob);
 
-               tmp.cached = !ca->mi.durability &&
-                       wp->type == BCH_DATA_user;
+               ptr.cached = cached ||
+                       (!ca->mi.durability &&
+                        wp->data_type == BCH_DATA_user);
 
-               tmp.offset += ca->mi.bucket_size - ob->sectors_free;
-               bch2_bkey_append_ptr(k, tmp);
+               bch2_bkey_append_ptr(k, ptr);
 
                BUG_ON(sectors > ob->sectors_free);
                ob->sectors_free -= sectors;
@@ -953,7 +1337,7 @@ static inline void writepoint_init(struct write_point *wp,
                                   enum bch_data_type type)
 {
        mutex_init(&wp->lock);
-       wp->type = type;
+       wp->data_type = type;
 }
 
 void bch2_fs_allocator_foreground_init(struct bch_fs *c)
@@ -984,9 +1368,28 @@ void bch2_fs_allocator_foreground_init(struct bch_fs *c)
             wp < c->write_points + c->write_points_nr; wp++) {
                writepoint_init(wp, BCH_DATA_user);
 
-               wp->last_used   = sched_clock();
+               wp->last_used   = local_clock();
                wp->write_point = (unsigned long) wp;
                hlist_add_head_rcu(&wp->node,
                                   writepoint_hash(c, wp->write_point));
        }
 }
+
+void bch2_open_buckets_to_text(struct printbuf *out, struct bch_fs *c)
+{
+       struct open_bucket *ob;
+
+       for (ob = c->open_buckets;
+            ob < c->open_buckets + ARRAY_SIZE(c->open_buckets);
+            ob++) {
+               spin_lock(&ob->lock);
+               if (ob->valid && !ob->on_partial_list) {
+                       prt_printf(out, "%zu ref %u type %s %u:%llu:%u\n",
+                              ob - c->open_buckets,
+                              atomic_read(&ob->pin),
+                              bch2_data_types[ob->data_type],
+                              ob->dev, ob->bucket, ob->gen);
+               }
+               spin_unlock(&ob->lock);
+       }
+}