]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/replicas.c
Move c_src dirs back to toplevel
[bcachefs-tools-debian] / libbcachefs / replicas.c
index 4128a1b3ad0014b5f6c6758a6f31e2e9a523f636..92ba56ef1fc89690656e9625871ecd7ee38b5f9b 100644 (file)
@@ -11,7 +11,7 @@ static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *,
 
 /* Replicas tracking - in memory: */
 
-static void verify_replicas_entry(struct bch_replicas_entry *e)
+static void verify_replicas_entry(struct bch_replicas_entry_v1 *e)
 {
 #ifdef CONFIG_BCACHEFS_DEBUG
        unsigned i;
@@ -26,7 +26,7 @@ static void verify_replicas_entry(struct bch_replicas_entry *e)
 #endif
 }
 
-void bch2_replicas_entry_sort(struct bch_replicas_entry *e)
+void bch2_replicas_entry_sort(struct bch_replicas_entry_v1 *e)
 {
        bubble_sort(e->devs, e->nr_devs, u8_cmp);
 }
@@ -36,30 +36,74 @@ static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r)
        eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL);
 }
 
+static void bch2_replicas_entry_v0_to_text(struct printbuf *out,
+                                          struct bch_replicas_entry_v0 *e)
+{
+       unsigned i;
+
+       if (e->data_type < BCH_DATA_NR)
+               prt_printf(out, "%s", bch2_data_types[e->data_type]);
+       else
+               prt_printf(out, "(invalid data type %u)", e->data_type);
+
+       prt_printf(out, ": %u [", e->nr_devs);
+       for (i = 0; i < e->nr_devs; i++)
+               prt_printf(out, i ? " %u" : "%u", e->devs[i]);
+       prt_printf(out, "]");
+}
+
 void bch2_replicas_entry_to_text(struct printbuf *out,
-                                struct bch_replicas_entry *e)
+                                struct bch_replicas_entry_v1 *e)
 {
        unsigned i;
 
-       pr_buf(out, "%s: %u/%u [",
-              bch2_data_types[e->data_type],
-              e->nr_required,
-              e->nr_devs);
+       if (e->data_type < BCH_DATA_NR)
+               prt_printf(out, "%s", bch2_data_types[e->data_type]);
+       else
+               prt_printf(out, "(invalid data type %u)", e->data_type);
 
+       prt_printf(out, ": %u/%u [", e->nr_required, e->nr_devs);
        for (i = 0; i < e->nr_devs; i++)
-               pr_buf(out, i ? " %u" : "%u", e->devs[i]);
-       pr_buf(out, "]");
+               prt_printf(out, i ? " %u" : "%u", e->devs[i]);
+       prt_printf(out, "]");
+}
+
+int bch2_replicas_entry_validate(struct bch_replicas_entry_v1 *r,
+                                struct bch_sb *sb,
+                                struct printbuf *err)
+{
+       if (!r->nr_devs) {
+               prt_printf(err, "no devices in entry ");
+               goto bad;
+       }
+
+       if (r->nr_required > 1 &&
+           r->nr_required >= r->nr_devs) {
+               prt_printf(err, "bad nr_required in entry ");
+               goto bad;
+       }
+
+       for (unsigned i = 0; i < r->nr_devs; i++)
+               if (!bch2_dev_exists(sb, r->devs[i])) {
+                       prt_printf(err, "invalid device %u in entry ", r->devs[i]);
+                       goto bad;
+               }
+
+       return 0;
+bad:
+       bch2_replicas_entry_to_text(err, r);
+       return -BCH_ERR_invalid_replicas_entry;
 }
 
 void bch2_cpu_replicas_to_text(struct printbuf *out,
-                             struct bch_replicas_cpu *r)
+                              struct bch_replicas_cpu *r)
 {
-       struct bch_replicas_entry *e;
+       struct bch_replicas_entry_v1 *e;
        bool first = true;
 
        for_each_cpu_replicas_entry(r, e) {
                if (!first)
-                       pr_buf(out, " ");
+                       prt_printf(out, " ");
                first = false;
 
                bch2_replicas_entry_to_text(out, e);
@@ -67,7 +111,7 @@ void bch2_cpu_replicas_to_text(struct printbuf *out,
 }
 
 static void extent_to_replicas(struct bkey_s_c k,
-                              struct bch_replicas_entry *r)
+                              struct bch_replicas_entry_v1 *r)
 {
        struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
        const union bch_extent_entry *entry;
@@ -87,7 +131,7 @@ static void extent_to_replicas(struct bkey_s_c k,
 }
 
 static void stripe_to_replicas(struct bkey_s_c k,
-                              struct bch_replicas_entry *r)
+                              struct bch_replicas_entry_v1 *r)
 {
        struct bkey_s_c_stripe s = bkey_s_c_to_stripe(k);
        const struct bch_extent_ptr *ptr;
@@ -100,7 +144,7 @@ static void stripe_to_replicas(struct bkey_s_c k,
                r->devs[r->nr_devs++] = ptr->dev;
 }
 
-void bch2_bkey_to_replicas(struct bch_replicas_entry *e,
+void bch2_bkey_to_replicas(struct bch_replicas_entry_v1 *e,
                           struct bkey_s_c k)
 {
        e->nr_devs = 0;
@@ -125,12 +169,10 @@ void bch2_bkey_to_replicas(struct bch_replicas_entry *e,
        bch2_replicas_entry_sort(e);
 }
 
-void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
+void bch2_devlist_to_replicas(struct bch_replicas_entry_v1 *e,
                              enum bch_data_type data_type,
                              struct bch_devs_list devs)
 {
-       unsigned i;
-
        BUG_ON(!data_type ||
               data_type == BCH_DATA_sb ||
               data_type >= BCH_DATA_NR);
@@ -139,15 +181,16 @@ void bch2_devlist_to_replicas(struct bch_replicas_entry *e,
        e->nr_devs      = 0;
        e->nr_required  = 1;
 
-       for (i = 0; i < devs.nr; i++)
-               e->devs[e->nr_devs++] = devs.devs[i];
+       darray_for_each(devs, i)
+               e->devs[e->nr_devs++] = *i;
 
        bch2_replicas_entry_sort(e);
 }
 
 static struct bch_replicas_cpu
-cpu_replicas_add_entry(struct bch_replicas_cpu *old,
-                      struct bch_replicas_entry *new_entry)
+cpu_replicas_add_entry(struct bch_fs *c,
+                      struct bch_replicas_cpu *old,
+                      struct bch_replicas_entry_v1 *new_entry)
 {
        unsigned i;
        struct bch_replicas_cpu new = {
@@ -156,6 +199,9 @@ cpu_replicas_add_entry(struct bch_replicas_cpu *old,
                                        replicas_entry_bytes(new_entry)),
        };
 
+       for (i = 0; i < new_entry->nr_devs; i++)
+               BUG_ON(!bch2_dev_exists2(c, new_entry->devs[i]));
+
        BUG_ON(!new_entry->data_type);
        verify_replicas_entry(new_entry);
 
@@ -177,7 +223,7 @@ cpu_replicas_add_entry(struct bch_replicas_cpu *old,
 }
 
 static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
-                                      struct bch_replicas_entry *search)
+                                      struct bch_replicas_entry_v1 *search)
 {
        int idx, entry_size = replicas_entry_bytes(search);
 
@@ -195,7 +241,7 @@ static inline int __replicas_entry_idx(struct bch_replicas_cpu *r,
 }
 
 int bch2_replicas_entry_idx(struct bch_fs *c,
-                           struct bch_replicas_entry *search)
+                           struct bch_replicas_entry_v1 *search)
 {
        bch2_replicas_entry_sort(search);
 
@@ -203,13 +249,13 @@ int bch2_replicas_entry_idx(struct bch_fs *c,
 }
 
 static bool __replicas_has_entry(struct bch_replicas_cpu *r,
-                                struct bch_replicas_entry *search)
+                                struct bch_replicas_entry_v1 *search)
 {
        return __replicas_entry_idx(r, search) >= 0;
 }
 
 bool bch2_replicas_marked(struct bch_fs *c,
-                         struct bch_replicas_entry *search)
+                         struct bch_replicas_entry_v1 *search)
 {
        bool marked;
 
@@ -255,7 +301,7 @@ static void __replicas_table_update_pcpu(struct bch_fs_usage __percpu *dst_p,
 {
        unsigned src_nr = sizeof(struct bch_fs_usage) / sizeof(u64) + src_r->nr;
        struct bch_fs_usage *dst, *src = (void *)
-               bch2_acc_percpu_u64s((void *) src_p, src_nr);
+               bch2_acc_percpu_u64s((u64 __percpu *) src_p, src_nr);
 
        preempt_disable();
        dst = this_cpu_ptr(dst_p);
@@ -319,14 +365,14 @@ out:
        return ret;
 err:
        bch_err(c, "error updating replicas table: memory allocation failure");
-       ret = -ENOMEM;
+       ret = -BCH_ERR_ENOMEM_replicas_table;
        goto out;
 }
 
 static unsigned reserve_journal_replicas(struct bch_fs *c,
                                     struct bch_replicas_cpu *r)
 {
-       struct bch_replicas_entry *e;
+       struct bch_replicas_entry_v1 *e;
        unsigned journal_res_u64s = 0;
 
        /* nr_inodes: */
@@ -351,7 +397,7 @@ static unsigned reserve_journal_replicas(struct bch_fs *c,
 
 noinline
 static int bch2_mark_replicas_slowpath(struct bch_fs *c,
-                               struct bch_replicas_entry *new_entry)
+                               struct bch_replicas_entry_v1 *new_entry)
 {
        struct bch_replicas_cpu new_r, new_gc;
        int ret = 0;
@@ -365,15 +411,19 @@ static int bch2_mark_replicas_slowpath(struct bch_fs *c,
 
        if (c->replicas_gc.entries &&
            !__replicas_has_entry(&c->replicas_gc, new_entry)) {
-               new_gc = cpu_replicas_add_entry(&c->replicas_gc, new_entry);
-               if (!new_gc.entries)
+               new_gc = cpu_replicas_add_entry(c, &c->replicas_gc, new_entry);
+               if (!new_gc.entries) {
+                       ret = -BCH_ERR_ENOMEM_cpu_replicas;
                        goto err;
+               }
        }
 
        if (!__replicas_has_entry(&c->replicas, new_entry)) {
-               new_r = cpu_replicas_add_entry(&c->replicas, new_entry);
-               if (!new_r.entries)
+               new_r = cpu_replicas_add_entry(c, &c->replicas, new_entry);
+               if (!new_r.entries) {
+                       ret = -BCH_ERR_ENOMEM_cpu_replicas;
                        goto err;
+               }
 
                ret = bch2_cpu_replicas_to_sb_replicas(c, &new_r);
                if (ret)
@@ -408,78 +458,18 @@ out:
 
        return ret;
 err:
-       bch_err(c, "error adding replicas entry: memory allocation failure");
-       ret = -ENOMEM;
+       bch_err_msg(c, ret, "adding replicas entry");
        goto out;
 }
 
-static int __bch2_mark_replicas(struct bch_fs *c,
-                               struct bch_replicas_entry *r,
-                               bool check)
-{
-       return likely(bch2_replicas_marked(c, r))       ? 0
-               : check                                 ? -1
-               : bch2_mark_replicas_slowpath(c, r);
-}
-
-int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry *r)
+int bch2_mark_replicas(struct bch_fs *c, struct bch_replicas_entry_v1 *r)
 {
-       return __bch2_mark_replicas(c, r, false);
-}
-
-static int __bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k,
-                                    bool check)
-{
-       struct bch_replicas_padded search;
-       struct bch_devs_list cached = bch2_bkey_cached_devs(k);
-       unsigned i;
-       int ret;
-
-       for (i = 0; i < cached.nr; i++) {
-               bch2_replicas_entry_cached(&search.e, cached.devs[i]);
-
-               ret = __bch2_mark_replicas(c, &search.e, check);
-               if (ret)
-                       return ret;
-       }
-
-       bch2_bkey_to_replicas(&search.e, k);
-
-       ret = __bch2_mark_replicas(c, &search.e, check);
-       if (ret)
-               return ret;
-
-       if (search.e.data_type == BCH_DATA_parity) {
-               search.e.data_type = BCH_DATA_cached;
-               ret = __bch2_mark_replicas(c, &search.e, check);
-               if (ret)
-                       return ret;
-
-               search.e.data_type = BCH_DATA_user;
-               ret = __bch2_mark_replicas(c, &search.e, check);
-               if (ret)
-                       return ret;
-       }
-
-       return 0;
+       return likely(bch2_replicas_marked(c, r))
+               ? 0 : bch2_mark_replicas_slowpath(c, r);
 }
 
 /* replicas delta list: */
 
-bool bch2_replicas_delta_list_marked(struct bch_fs *c,
-                                    struct replicas_delta_list *r)
-{
-       struct replicas_delta *d = r->d;
-       struct replicas_delta *top = (void *) r->d + r->used;
-
-       percpu_rwsem_assert_held(&c->mark_lock);
-
-       for (d = r->d; d != top; d = replicas_delta_next(d))
-               if (bch2_replicas_entry_idx(c, &d->r) < 0)
-                       return false;
-       return true;
-}
-
 int bch2_replicas_delta_list_mark(struct bch_fs *c,
                                  struct replicas_delta_list *r)
 {
@@ -492,19 +482,6 @@ int bch2_replicas_delta_list_mark(struct bch_fs *c,
        return ret;
 }
 
-/* bkey replicas: */
-
-bool bch2_bkey_replicas_marked(struct bch_fs *c,
-                              struct bkey_s_c k)
-{
-       return __bch2_mark_bkey_replicas(c, k, true) == 0;
-}
-
-int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
-{
-       return __bch2_mark_bkey_replicas(c, k, false);
-}
-
 /*
  * Old replicas_gc mechanism: only used for journal replicas entries now, should
  * die at some point:
@@ -512,43 +489,15 @@ int bch2_mark_bkey_replicas(struct bch_fs *c, struct bkey_s_c k)
 
 int bch2_replicas_gc_end(struct bch_fs *c, int ret)
 {
-       unsigned i;
-
        lockdep_assert_held(&c->replicas_gc_lock);
 
        mutex_lock(&c->sb_lock);
        percpu_down_write(&c->mark_lock);
 
-       /*
-        * this is kind of crappy; the replicas gc mechanism needs to be ripped
-        * out
-        */
-
-       for (i = 0; i < c->replicas.nr; i++) {
-               struct bch_replicas_entry *e =
-                       cpu_replicas_entry(&c->replicas, i);
-               struct bch_replicas_cpu n;
-
-               if (!__replicas_has_entry(&c->replicas_gc, e) &&
-                   bch2_fs_usage_read_one(c, &c->usage_base->replicas[i])) {
-                       n = cpu_replicas_add_entry(&c->replicas_gc, e);
-                       if (!n.entries) {
-                               ret = -ENOSPC;
-                               goto err;
-                       }
-
-                       swap(n, c->replicas_gc);
-                       kfree(n.entries);
-               }
-       }
-
-       if (bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc)) {
-               ret = -ENOSPC;
-               goto err;
-       }
+       ret =   ret ?:
+               bch2_cpu_replicas_to_sb_replicas(c, &c->replicas_gc) ?:
+               replicas_table_update(c, &c->replicas_gc);
 
-       ret = replicas_table_update(c, &c->replicas_gc);
-err:
        kfree(c->replicas_gc.entries);
        c->replicas_gc.entries = NULL;
 
@@ -564,7 +513,7 @@ err:
 
 int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
 {
-       struct bch_replicas_entry *e;
+       struct bch_replicas_entry_v1 *e;
        unsigned i = 0;
 
        lockdep_assert_held(&c->replicas_gc_lock);
@@ -589,7 +538,7 @@ int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
        if (!c->replicas_gc.entries) {
                mutex_unlock(&c->sb_lock);
                bch_err(c, "error allocating c->replicas_gc");
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_replicas_gc;
        }
 
        for_each_cpu_replicas_entry(&c->replicas, e)
@@ -603,8 +552,14 @@ int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask)
        return 0;
 }
 
-/* New much simpler mechanism for clearing out unneeded replicas entries: */
-
+/*
+ * New much simpler mechanism for clearing out unneeded replicas entries - drop
+ * replicas entries that have 0 sectors used.
+ *
+ * However, we don't track sector counts for journal usage, so this doesn't drop
+ * any BCH_DATA_journal entries; the old bch2_replicas_gc_(start|end) mechanism
+ * is retained for that.
+ */
 int bch2_replicas_gc2(struct bch_fs *c)
 {
        struct bch_replicas_cpu new = { 0 };
@@ -618,7 +573,7 @@ retry:
        new.entries     = kcalloc(nr, new.entry_size, GFP_KERNEL);
        if (!new.entries) {
                bch_err(c, "error allocating c->replicas_gc");
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_replicas_gc;
        }
 
        mutex_lock(&c->sb_lock);
@@ -633,7 +588,7 @@ retry:
        }
 
        for (i = 0; i < c->replicas.nr; i++) {
-               struct bch_replicas_entry *e =
+               struct bch_replicas_entry_v1 *e =
                        cpu_replicas_entry(&c->replicas, i);
 
                if (e->data_type == BCH_DATA_journal ||
@@ -648,13 +603,9 @@ retry:
 
        bch2_cpu_replicas_sort(&new);
 
-       if (bch2_cpu_replicas_to_sb_replicas(c, &new)) {
-               ret = -ENOSPC;
-               goto err;
-       }
+       ret =   bch2_cpu_replicas_to_sb_replicas(c, &new) ?:
+               replicas_table_update(c, &new);
 
-       ret = replicas_table_update(c, &new);
-err:
        kfree(new.entries);
 
        percpu_up_write(&c->mark_lock);
@@ -668,7 +619,7 @@ err:
 }
 
 int bch2_replicas_set_usage(struct bch_fs *c,
-                           struct bch_replicas_entry *r,
+                           struct bch_replicas_entry_v1 *r,
                            u64 sectors)
 {
        int ret, idx = bch2_replicas_entry_idx(c, r);
@@ -676,9 +627,9 @@ int bch2_replicas_set_usage(struct bch_fs *c,
        if (idx < 0) {
                struct bch_replicas_cpu n;
 
-               n = cpu_replicas_add_entry(&c->replicas, r);
+               n = cpu_replicas_add_entry(c, &c->replicas, r);
                if (!n.entries)
-                       return -ENOMEM;
+                       return -BCH_ERR_ENOMEM_cpu_replicas;
 
                ret = replicas_table_update(c, &n);
                if (ret)
@@ -701,7 +652,7 @@ static int
 __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
                                   struct bch_replicas_cpu *cpu_r)
 {
-       struct bch_replicas_entry *e, *dst;
+       struct bch_replicas_entry_v1 *e, *dst;
        unsigned nr = 0, entry_size = 0, idx = 0;
 
        for_each_replicas_entry(sb_r, e) {
@@ -712,7 +663,7 @@ __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r,
 
        cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
        if (!cpu_r->entries)
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_cpu_replicas;
 
        cpu_r->nr               = nr;
        cpu_r->entry_size       = entry_size;
@@ -739,18 +690,18 @@ __bch2_sb_replicas_v0_to_cpu_replicas(struct bch_sb_field_replicas_v0 *sb_r,
                nr++;
        }
 
-       entry_size += sizeof(struct bch_replicas_entry) -
+       entry_size += sizeof(struct bch_replicas_entry_v1) -
                sizeof(struct bch_replicas_entry_v0);
 
        cpu_r->entries = kcalloc(nr, entry_size, GFP_KERNEL);
        if (!cpu_r->entries)
-               return -ENOMEM;
+               return -BCH_ERR_ENOMEM_cpu_replicas;
 
        cpu_r->nr               = nr;
        cpu_r->entry_size       = entry_size;
 
        for_each_replicas_entry(sb_r, e) {
-               struct bch_replicas_entry *dst =
+               struct bch_replicas_entry_v1 *dst =
                        cpu_replicas_entry(cpu_r, idx++);
 
                dst->data_type  = e->data_type;
@@ -770,13 +721,12 @@ int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c)
        struct bch_replicas_cpu new_r = { 0, 0, NULL };
        int ret = 0;
 
-       if ((sb_v1 = bch2_sb_get_replicas(c->disk_sb.sb)))
+       if ((sb_v1 = bch2_sb_field_get(c->disk_sb.sb, replicas)))
                ret = __bch2_sb_replicas_to_cpu_replicas(sb_v1, &new_r);
-       else if ((sb_v0 = bch2_sb_get_replicas_v0(c->disk_sb.sb)))
+       else if ((sb_v0 = bch2_sb_field_get(c->disk_sb.sb, replicas_v0)))
                ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_v0, &new_r);
-
        if (ret)
-               return -ENOMEM;
+               return ret;
 
        bch2_cpu_replicas_sort(&new_r);
 
@@ -795,7 +745,7 @@ static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
 {
        struct bch_sb_field_replicas_v0 *sb_r;
        struct bch_replicas_entry_v0 *dst;
-       struct bch_replicas_entry *src;
+       struct bch_replicas_entry_v1 *src;
        size_t bytes;
 
        bytes = sizeof(struct bch_sb_field_replicas);
@@ -803,13 +753,13 @@ static int bch2_cpu_replicas_to_sb_replicas_v0(struct bch_fs *c,
        for_each_cpu_replicas_entry(r, src)
                bytes += replicas_entry_bytes(src) - 1;
 
-       sb_r = bch2_sb_resize_replicas_v0(&c->disk_sb,
+       sb_r = bch2_sb_field_resize(&c->disk_sb, replicas_v0,
                        DIV_ROUND_UP(bytes, sizeof(u64)));
        if (!sb_r)
-               return -ENOSPC;
+               return -BCH_ERR_ENOSPC_sb_replicas;
 
        bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas);
-       sb_r = bch2_sb_get_replicas_v0(c->disk_sb.sb);
+       sb_r = bch2_sb_field_get(c->disk_sb.sb, replicas_v0);
 
        memset(&sb_r->entries, 0,
               vstruct_end(&sb_r->field) -
@@ -833,7 +783,7 @@ static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
                                            struct bch_replicas_cpu *r)
 {
        struct bch_sb_field_replicas *sb_r;
-       struct bch_replicas_entry *dst, *src;
+       struct bch_replicas_entry_v1 *dst, *src;
        bool need_v1 = false;
        size_t bytes;
 
@@ -848,13 +798,13 @@ static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
        if (!need_v1)
                return bch2_cpu_replicas_to_sb_replicas_v0(c, r);
 
-       sb_r = bch2_sb_resize_replicas(&c->disk_sb,
+       sb_r = bch2_sb_field_resize(&c->disk_sb, replicas,
                        DIV_ROUND_UP(bytes, sizeof(u64)));
        if (!sb_r)
-               return -ENOSPC;
+               return -BCH_ERR_ENOSPC_sb_replicas;
 
        bch2_sb_field_delete(&c->disk_sb, BCH_SB_FIELD_replicas_v0);
-       sb_r = bch2_sb_get_replicas(c->disk_sb.sb);
+       sb_r = bch2_sb_field_get(c->disk_sb.sb, replicas);
 
        memset(&sb_r->entries, 0,
               vstruct_end(&sb_r->field) -
@@ -872,7 +822,9 @@ static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c,
        return 0;
 }
 
-static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
+static int bch2_cpu_replicas_validate(struct bch_replicas_cpu *cpu_r,
+                                     struct bch_sb *sb,
+                                     struct printbuf *err)
 {
        unsigned i;
 
@@ -881,58 +833,45 @@ static const char *check_dup_replicas_entries(struct bch_replicas_cpu *cpu_r)
                      cpu_r->entry_size,
                      memcmp, NULL);
 
-       for (i = 0; i + 1 < cpu_r->nr; i++) {
-               struct bch_replicas_entry *l =
+       for (i = 0; i < cpu_r->nr; i++) {
+               struct bch_replicas_entry_v1 *e =
                        cpu_replicas_entry(cpu_r, i);
-               struct bch_replicas_entry *r =
-                       cpu_replicas_entry(cpu_r, i + 1);
 
-               BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0);
+               int ret = bch2_replicas_entry_validate(e, sb, err);
+               if (ret)
+                       return ret;
+
+               if (i + 1 < cpu_r->nr) {
+                       struct bch_replicas_entry_v1 *n =
+                               cpu_replicas_entry(cpu_r, i + 1);
 
-               if (!memcmp(l, r, cpu_r->entry_size))
-                       return "duplicate replicas entry";
+                       BUG_ON(memcmp(e, n, cpu_r->entry_size) > 0);
+
+                       if (!memcmp(e, n, cpu_r->entry_size)) {
+                               prt_printf(err, "duplicate replicas entry ");
+                               bch2_replicas_entry_to_text(err, e);
+                               return -BCH_ERR_invalid_sb_replicas;
+                       }
+               }
        }
 
-       return NULL;
+       return 0;
 }
 
-static const char *bch2_sb_validate_replicas(struct bch_sb *sb, struct bch_sb_field *f)
+static int bch2_sb_replicas_validate(struct bch_sb *sb, struct bch_sb_field *f,
+                                    struct printbuf *err)
 {
        struct bch_sb_field_replicas *sb_r = field_to_type(f, replicas);
-       struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
-       struct bch_replicas_cpu cpu_r = { .entries = NULL };
-       struct bch_replicas_entry *e;
-       const char *err;
-       unsigned i;
-
-       for_each_replicas_entry(sb_r, e) {
-               err = "invalid replicas entry: invalid data type";
-               if (e->data_type >= BCH_DATA_NR)
-                       goto err;
-
-               err = "invalid replicas entry: no devices";
-               if (!e->nr_devs)
-                       goto err;
-
-               err = "invalid replicas entry: bad nr_required";
-               if (e->nr_required > 1 &&
-                   e->nr_required >= e->nr_devs)
-                       goto err;
-
-               err = "invalid replicas entry: invalid device";
-               for (i = 0; i < e->nr_devs; i++)
-                       if (!bch2_dev_exists(sb, mi, e->devs[i]))
-                               goto err;
-       }
+       struct bch_replicas_cpu cpu_r;
+       int ret;
 
-       err = "cannot allocate memory";
-       if (__bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r))
-               goto err;
+       ret = __bch2_sb_replicas_to_cpu_replicas(sb_r, &cpu_r);
+       if (ret)
+               return ret;
 
-       err = check_dup_replicas_entries(&cpu_r);
-err:
+       ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
        kfree(cpu_r.entries);
-       return err;
+       return ret;
 }
 
 static void bch2_sb_replicas_to_text(struct printbuf *out,
@@ -940,59 +879,61 @@ static void bch2_sb_replicas_to_text(struct printbuf *out,
                                     struct bch_sb_field *f)
 {
        struct bch_sb_field_replicas *r = field_to_type(f, replicas);
-       struct bch_replicas_entry *e;
+       struct bch_replicas_entry_v1 *e;
        bool first = true;
 
        for_each_replicas_entry(r, e) {
                if (!first)
-                       pr_buf(out, " ");
+                       prt_printf(out, " ");
                first = false;
 
                bch2_replicas_entry_to_text(out, e);
        }
+       prt_newline(out);
 }
 
 const struct bch_sb_field_ops bch_sb_field_ops_replicas = {
-       .validate       = bch2_sb_validate_replicas,
+       .validate       = bch2_sb_replicas_validate,
        .to_text        = bch2_sb_replicas_to_text,
 };
 
-static const char *bch2_sb_validate_replicas_v0(struct bch_sb *sb, struct bch_sb_field *f)
+static int bch2_sb_replicas_v0_validate(struct bch_sb *sb, struct bch_sb_field *f,
+                                       struct printbuf *err)
 {
        struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
-       struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
-       struct bch_replicas_cpu cpu_r = { .entries = NULL };
-       struct bch_replicas_entry_v0 *e;
-       const char *err;
-       unsigned i;
+       struct bch_replicas_cpu cpu_r;
+       int ret;
 
-       for_each_replicas_entry_v0(sb_r, e) {
-               err = "invalid replicas entry: invalid data type";
-               if (e->data_type >= BCH_DATA_NR)
-                       goto err;
+       ret = __bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r);
+       if (ret)
+               return ret;
 
-               err = "invalid replicas entry: no devices";
-               if (!e->nr_devs)
-                       goto err;
+       ret = bch2_cpu_replicas_validate(&cpu_r, sb, err);
+       kfree(cpu_r.entries);
+       return ret;
+}
 
-               err = "invalid replicas entry: invalid device";
-               for (i = 0; i < e->nr_devs; i++)
-                       if (!bch2_dev_exists(sb, mi, e->devs[i]))
-                               goto err;
-       }
+static void bch2_sb_replicas_v0_to_text(struct printbuf *out,
+                                       struct bch_sb *sb,
+                                       struct bch_sb_field *f)
+{
+       struct bch_sb_field_replicas_v0 *sb_r = field_to_type(f, replicas_v0);
+       struct bch_replicas_entry_v0 *e;
+       bool first = true;
 
-       err = "cannot allocate memory";
-       if (__bch2_sb_replicas_v0_to_cpu_replicas(sb_r, &cpu_r))
-               goto err;
+       for_each_replicas_entry(sb_r, e) {
+               if (!first)
+                       prt_printf(out, " ");
+               first = false;
 
-       err = check_dup_replicas_entries(&cpu_r);
-err:
-       kfree(cpu_r.entries);
-       return err;
+               bch2_replicas_entry_v0_to_text(out, e);
+       }
+       prt_newline(out);
 }
 
 const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
-       .validate       = bch2_sb_validate_replicas_v0,
+       .validate       = bch2_sb_replicas_v0_validate,
+       .to_text        = bch2_sb_replicas_v0_to_text,
 };
 
 /* Query replicas: */
@@ -1000,7 +941,7 @@ const struct bch_sb_field_ops bch_sb_field_ops_replicas_v0 = {
 bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
                           unsigned flags, bool print)
 {
-       struct bch_replicas_entry *e;
+       struct bch_replicas_entry_v1 *e;
        bool ret = true;
 
        percpu_down_read(&c->mark_lock);
@@ -1008,6 +949,9 @@ bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
                unsigned i, nr_online = 0, nr_failed = 0, dflags = 0;
                bool metadata = e->data_type < BCH_DATA_user;
 
+               if (e->data_type == BCH_DATA_cached)
+                       continue;
+
                for (i = 0; i < e->nr_devs; i++) {
                        struct bch_dev *ca = bch_dev_bkey_exists(c, e->devs[i]);
 
@@ -1030,11 +974,12 @@ bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
 
                if (dflags & ~flags) {
                        if (print) {
-                               char buf[100];
+                               struct printbuf buf = PRINTBUF;
 
-                               bch2_replicas_entry_to_text(&PBUF(buf), e);
+                               bch2_replicas_entry_to_text(&buf, e);
                                bch_err(c, "insufficient devices online (%u) for replicas entry %s",
-                                       nr_online, buf);
+                                       nr_online, buf.buf);
+                               printbuf_exit(&buf);
                        }
                        ret = false;
                        break;
@@ -1046,28 +991,67 @@ bool bch2_have_enough_devs(struct bch_fs *c, struct bch_devs_mask devs,
        return ret;
 }
 
-unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
+unsigned bch2_sb_dev_has_data(struct bch_sb *sb, unsigned dev)
 {
-       struct bch_replicas_entry *e;
-       unsigned i, ret = 0;
+       struct bch_sb_field_replicas *replicas;
+       struct bch_sb_field_replicas_v0 *replicas_v0;
+       unsigned i, data_has = 0;
+
+       replicas = bch2_sb_field_get(sb, replicas);
+       replicas_v0 = bch2_sb_field_get(sb, replicas_v0);
+
+       if (replicas) {
+               struct bch_replicas_entry_v1 *r;
+
+               for_each_replicas_entry(replicas, r)
+                       for (i = 0; i < r->nr_devs; i++)
+                               if (r->devs[i] == dev)
+                                       data_has |= 1 << r->data_type;
+       } else if (replicas_v0) {
+               struct bch_replicas_entry_v0 *r;
+
+               for_each_replicas_entry_v0(replicas_v0, r)
+                       for (i = 0; i < r->nr_devs; i++)
+                               if (r->devs[i] == dev)
+                                       data_has |= 1 << r->data_type;
+       }
 
-       percpu_down_read(&c->mark_lock);
 
-       for_each_cpu_replicas_entry(&c->replicas, e)
-               for (i = 0; i < e->nr_devs; i++)
-                       if (e->devs[i] == ca->dev_idx)
-                               ret |= 1 << e->data_type;
+       return data_has;
+}
 
-       percpu_up_read(&c->mark_lock);
+unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca)
+{
+       unsigned ret;
+
+       mutex_lock(&c->sb_lock);
+       ret = bch2_sb_dev_has_data(c->disk_sb.sb, ca->dev_idx);
+       mutex_unlock(&c->sb_lock);
 
        return ret;
 }
 
+void bch2_fs_replicas_exit(struct bch_fs *c)
+{
+       unsigned i;
+
+       kfree(c->usage_scratch);
+       for (i = 0; i < ARRAY_SIZE(c->usage); i++)
+               free_percpu(c->usage[i]);
+       kfree(c->usage_base);
+       kfree(c->replicas.entries);
+       kfree(c->replicas_gc.entries);
+
+       mempool_exit(&c->replicas_delta_pool);
+}
+
 int bch2_fs_replicas_init(struct bch_fs *c)
 {
        bch2_journal_entry_res_resize(&c->journal,
                        &c->replicas_journal_res,
                        reserve_journal_replicas(c, &c->replicas));
 
-       return replicas_table_update(c, &c->replicas);
+       return mempool_init_kmalloc_pool(&c->replicas_delta_pool, 1,
+                                        REPLICAS_DELTA_LIST_MAX) ?:
+               replicas_table_update(c, &c->replicas);
 }