diff options
Diffstat (limited to 'libbcachefs/super-io.c')
-rw-r--r-- | libbcachefs/super-io.c | 716 |
1 files changed, 369 insertions, 347 deletions
diff --git a/libbcachefs/super-io.c b/libbcachefs/super-io.c index a3ecfb92..3f55c244 100644 --- a/libbcachefs/super-io.c +++ b/libbcachefs/super-io.c @@ -12,6 +12,8 @@ #include <linux/sort.h> static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *); +static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *, + struct bch_replicas_cpu *); static const char *bch2_sb_validate_replicas(struct bch_sb *); static inline void __bch2_sb_layout_size_assert(void) @@ -157,7 +159,7 @@ struct bch_sb_field *bch2_sb_field_resize(struct bch_sb_handle *sb, return NULL; f = __bch2_sb_field_resize(sb->sb, f, u64s); - f->type = type; + f->type = cpu_to_le32(type); return f; } @@ -188,7 +190,7 @@ struct bch_sb_field *bch2_fs_sb_field_resize(struct bch_fs *c, } f = __bch2_sb_field_resize(c->disk_sb, f, u64s); - f->type = type; + f->type = cpu_to_le32(type); return f; } @@ -354,7 +356,16 @@ const char *bch2_sb_validate(struct bch_sb_handle *disk_sb) if (!BCH_SB_DATA_REPLICAS_REQ(sb) || BCH_SB_DATA_REPLICAS_REQ(sb) >= BCH_REPLICAS_MAX) - return "Invalid number of metadata replicas"; + return "Invalid number of data replicas"; + + if (BCH_SB_META_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR) + return "Invalid metadata checksum type"; + + if (BCH_SB_DATA_CSUM_TYPE(sb) >= BCH_CSUM_OPT_NR) + return "Invalid metadata checksum type"; + + if (BCH_SB_COMPRESSION_TYPE(sb) >= BCH_COMPRESSION_OPT_NR) + return "Invalid compression type"; if (!BCH_SB_BTREE_NODE_SIZE(sb)) return "Btree node size not set"; @@ -507,7 +518,7 @@ static void __copy_super(struct bch_sb *dst, struct bch_sb *src) if (src_f->type == BCH_SB_FIELD_journal) continue; - dst_f = bch2_sb_field_get(dst, src_f->type); + dst_f = bch2_sb_field_get(dst, le32_to_cpu(src_f->type)); dst_f = __bch2_sb_field_resize(dst, dst_f, le32_to_cpu(src_f->u64s)); @@ -601,7 +612,7 @@ reread: /* XXX: verify MACs */ csum = csum_vstruct(NULL, BCH_SB_CSUM_TYPE(sb->sb), - (struct nonce) { 0 }, sb->sb); + null_nonce(), sb->sb); if (bch2_crc_cmp(csum, sb->sb->csum)) return "bad checksum reading superblock"; @@ -688,9 +699,9 @@ const char *bch2_read_super(const char *path, got_super: pr_debug("read sb version %llu, flags %llu, seq %llu, journal size %u", le64_to_cpu(ret->sb->version), - le64_to_cpu(ret->sb->flags), + le64_to_cpu(ret->sb->flags[0]), le64_to_cpu(ret->sb->seq), - le16_to_cpu(ret->sb->u64s)); + le32_to_cpu(ret->sb->u64s)); err = "Superblock block size smaller than device block size"; if (le16_to_cpu(ret->sb->block_size) << 9 < @@ -711,7 +722,7 @@ static void write_super_endio(struct bio *bio) /* XXX: return errors directly */ - if (bch2_dev_io_err_on(bio->bi_error, ca, "superblock write")) + if (bch2_dev_io_err_on(bio->bi_status, ca, "superblock write")) ca->sb_write_error = 1; closure_put(&ca->fs->sb_write); @@ -727,7 +738,7 @@ static void write_one_super(struct bch_fs *c, struct bch_dev *ca, unsigned idx) SET_BCH_SB_CSUM_TYPE(sb, c->opts.metadata_checksum); sb->csum = csum_vstruct(c, BCH_SB_CSUM_TYPE(sb), - (struct nonce) { 0 }, sb); + null_nonce(), sb); bio_reset(bio); bio->bi_bdev = ca->disk_sb.bdev; @@ -830,7 +841,12 @@ out: bch2_sb_update(c); } -/* replica information: */ +/* Replicas tracking - in memory: */ + +#define for_each_cpu_replicas_entry(_r, _i) \ + for (_i = (_r)->entries; \ + (void *) (_i) < (void *) (_r)->entries + (_r)->nr * (_r)->entry_size;\ + _i = (void *) (_i) + (_r)->entry_size) static inline struct bch_replicas_cpu_entry * cpu_replicas_entry(struct bch_replicas_cpu *r, unsigned i) @@ -838,6 +854,11 @@ cpu_replicas_entry(struct bch_replicas_cpu *r, unsigned i) return (void *) r->entries + r->entry_size * i; } +static void bch2_cpu_replicas_sort(struct bch_replicas_cpu *r) +{ + eytzinger0_sort(r->entries, r->nr, r->entry_size, memcmp, NULL); +} + static inline bool replicas_test_dev(struct bch_replicas_cpu_entry *e, unsigned dev) { @@ -856,6 +877,246 @@ static inline unsigned replicas_dev_slots(struct bch_replicas_cpu *r) offsetof(struct bch_replicas_cpu_entry, devs)) * 8; } +static unsigned bkey_to_replicas(struct bkey_s_c_extent e, + enum bch_data_type data_type, + struct bch_replicas_cpu_entry *r, + unsigned *max_dev) +{ + const struct bch_extent_ptr *ptr; + unsigned nr = 0; + + BUG_ON(!data_type || + data_type == BCH_DATA_SB || + data_type >= BCH_DATA_NR); + + memset(r, 0, sizeof(*r)); + r->data_type = data_type; + + *max_dev = 0; + + extent_for_each_ptr(e, ptr) + if (!ptr->cached) { + *max_dev = max_t(unsigned, *max_dev, ptr->dev); + replicas_set_dev(r, ptr->dev); + nr++; + } + return nr; +} + +static struct bch_replicas_cpu * +cpu_replicas_add_entry(struct bch_replicas_cpu *old, + struct bch_replicas_cpu_entry new_entry, + unsigned max_dev) +{ + struct bch_replicas_cpu *new; + unsigned i, nr, entry_size; + + entry_size = offsetof(struct bch_replicas_cpu_entry, devs) + + DIV_ROUND_UP(max_dev + 1, 8); + entry_size = max(entry_size, old->entry_size); + nr = old->nr + 1; + + new = kzalloc(sizeof(struct bch_replicas_cpu) + + nr * entry_size, GFP_NOIO); + if (!new) + return NULL; + + new->nr = nr; + new->entry_size = entry_size; + + for (i = 0; i < old->nr; i++) + memcpy(cpu_replicas_entry(new, i), + cpu_replicas_entry(old, i), + min(new->entry_size, old->entry_size)); + + memcpy(cpu_replicas_entry(new, old->nr), + &new_entry, + new->entry_size); + + bch2_cpu_replicas_sort(new); + return new; +} + +static bool replicas_has_entry(struct bch_replicas_cpu *r, + struct bch_replicas_cpu_entry search, + unsigned max_dev) +{ + return max_dev < replicas_dev_slots(r) && + eytzinger0_find(r->entries, r->nr, + r->entry_size, + memcmp, &search) < r->nr; +} + +noinline +static int bch2_check_mark_super_slowpath(struct bch_fs *c, + struct bch_replicas_cpu_entry new_entry, + unsigned max_dev) +{ + struct bch_replicas_cpu *old_gc, *new_gc = NULL, *old_r, *new_r; + int ret = -ENOMEM; + + mutex_lock(&c->sb_lock); + + old_gc = rcu_dereference_protected(c->replicas_gc, + lockdep_is_held(&c->sb_lock)); + if (old_gc && !replicas_has_entry(old_gc, new_entry, max_dev)) { + new_gc = cpu_replicas_add_entry(old_gc, new_entry, max_dev); + if (!new_gc) + goto err; + } + + old_r = rcu_dereference_protected(c->replicas, + lockdep_is_held(&c->sb_lock)); + /* recheck, might have raced */ + if (replicas_has_entry(old_r, new_entry, max_dev)) + goto out; + + new_r = cpu_replicas_add_entry(old_r, new_entry, max_dev); + if (!new_r) + goto err; + + ret = bch2_cpu_replicas_to_sb_replicas(c, new_r); + if (ret) + goto err; + + if (new_gc) { + rcu_assign_pointer(c->replicas_gc, new_gc); + kfree_rcu(old_gc, rcu); + } + + rcu_assign_pointer(c->replicas, new_r); + kfree_rcu(old_r, rcu); + + bch2_write_super(c); +out: + ret = 0; +err: + mutex_unlock(&c->sb_lock); + return ret; +} + +static inline int __bch2_check_mark_super(struct bch_fs *c, + struct bch_replicas_cpu_entry search, + unsigned max_dev) +{ + struct bch_replicas_cpu *r, *gc_r; + bool marked; + + rcu_read_lock(); + r = rcu_dereference(c->replicas); + gc_r = rcu_dereference(c->replicas_gc); + marked = replicas_has_entry(r, search, max_dev) && + (!likely(gc_r) || replicas_has_entry(gc_r, search, max_dev)); + rcu_read_unlock(); + + return likely(marked) ? 0 + : bch2_check_mark_super_slowpath(c, search, max_dev); +} + +int bch2_check_mark_super(struct bch_fs *c, struct bkey_s_c_extent e, + enum bch_data_type data_type) +{ + struct bch_replicas_cpu_entry search; + unsigned max_dev; + + if (!bkey_to_replicas(e, data_type, &search, &max_dev)) + return 0; + + return __bch2_check_mark_super(c, search, max_dev); +} + +int bch2_check_mark_super_devlist(struct bch_fs *c, + struct bch_devs_list *devs, + enum bch_data_type data_type) +{ + struct bch_replicas_cpu_entry search = { .data_type = data_type }; + unsigned i, max_dev = 0; + + if (!devs->nr) + return 0; + + for (i = 0; i < devs->nr; i++) { + max_dev = max_t(unsigned, max_dev, devs->devs[i]); + replicas_set_dev(&search, devs->devs[i]); + } + + return __bch2_check_mark_super(c, search, max_dev); +} + +int bch2_replicas_gc_end(struct bch_fs *c, int err) +{ + struct bch_replicas_cpu *new_r, *old_r; + int ret = 0; + + lockdep_assert_held(&c->replicas_gc_lock); + + mutex_lock(&c->sb_lock); + + new_r = rcu_dereference_protected(c->replicas_gc, + lockdep_is_held(&c->sb_lock)); + + if (err) { + rcu_assign_pointer(c->replicas_gc, NULL); + kfree_rcu(new_r, rcu); + goto err; + } + + if (bch2_cpu_replicas_to_sb_replicas(c, new_r)) { + ret = -ENOSPC; + goto err; + } + + old_r = rcu_dereference_protected(c->replicas, + lockdep_is_held(&c->sb_lock)); + + rcu_assign_pointer(c->replicas, new_r); + rcu_assign_pointer(c->replicas_gc, NULL); + kfree_rcu(old_r, rcu); + + bch2_write_super(c); +err: + mutex_unlock(&c->sb_lock); + return ret; +} + +int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask) +{ + struct bch_replicas_cpu *dst, *src; + struct bch_replicas_cpu_entry *e; + + lockdep_assert_held(&c->replicas_gc_lock); + + mutex_lock(&c->sb_lock); + BUG_ON(c->replicas_gc); + + src = rcu_dereference_protected(c->replicas, + lockdep_is_held(&c->sb_lock)); + + dst = kzalloc(sizeof(struct bch_replicas_cpu) + + src->nr * src->entry_size, GFP_NOIO); + if (!dst) { + mutex_unlock(&c->sb_lock); + return -ENOMEM; + } + + dst->nr = 0; + dst->entry_size = src->entry_size; + + for_each_cpu_replicas_entry(src, e) + if (!((1 << e->data_type) & typemask)) + memcpy(cpu_replicas_entry(dst, dst->nr++), + e, dst->entry_size); + + bch2_cpu_replicas_sort(dst); + + rcu_assign_pointer(c->replicas_gc, dst); + mutex_unlock(&c->sb_lock); + + return 0; +} + +/* Replicas tracking - superblock: */ + static void bch2_sb_replicas_nr_entries(struct bch_sb_field_replicas *r, unsigned *nr, unsigned *bytes, @@ -914,10 +1175,7 @@ __bch2_sb_replicas_to_cpu_replicas(struct bch_sb_field_replicas *sb_r) } } - eytzinger0_sort(cpu_r->entries, - cpu_r->nr, - cpu_r->entry_size, - memcmp, NULL); + bch2_cpu_replicas_sort(cpu_r); return cpu_r; } @@ -926,14 +1184,12 @@ static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c) struct bch_sb_field_replicas *sb_r; struct bch_replicas_cpu *cpu_r, *old_r; - lockdep_assert_held(&c->sb_lock); - sb_r = bch2_sb_get_replicas(c->disk_sb); cpu_r = __bch2_sb_replicas_to_cpu_replicas(sb_r); if (!cpu_r) return -ENOMEM; - old_r = c->replicas; + old_r = rcu_dereference_check(c->replicas, lockdep_is_held(&c->sb_lock)); rcu_assign_pointer(c->replicas, cpu_r); if (old_r) kfree_rcu(old_r, rcu); @@ -941,192 +1197,133 @@ static int bch2_sb_replicas_to_cpu_replicas(struct bch_fs *c) return 0; } -static void bkey_to_replicas(struct bkey_s_c_extent e, - enum bch_data_type data_type, - struct bch_replicas_cpu_entry *r, - unsigned *max_dev) +static int bch2_cpu_replicas_to_sb_replicas(struct bch_fs *c, + struct bch_replicas_cpu *r) { - const struct bch_extent_ptr *ptr; - - BUG_ON(!data_type || - data_type == BCH_DATA_SB || - data_type >= BCH_DATA_NR); - - memset(r, 0, sizeof(*r)); - r->data_type = data_type; - - *max_dev = 0; - - extent_for_each_ptr(e, ptr) - if (!ptr->cached) { - *max_dev = max_t(unsigned, *max_dev, ptr->dev); - replicas_set_dev(r, ptr->dev); - } -} + struct bch_sb_field_replicas *sb_r; + struct bch_replicas_entry *sb_e; + struct bch_replicas_cpu_entry *e; + size_t i, bytes; -/* - * for when gc of replica information is in progress: - */ -static int bch2_update_gc_replicas(struct bch_fs *c, - struct bch_replicas_cpu *gc_r, - struct bkey_s_c_extent e, - enum bch_data_type data_type) -{ - struct bch_replicas_cpu_entry new_e; - struct bch_replicas_cpu *new; - unsigned i, nr, entry_size, max_dev; + bytes = sizeof(struct bch_sb_field_replicas); - bkey_to_replicas(e, data_type, &new_e, &max_dev); + for_each_cpu_replicas_entry(r, e) { + bytes += sizeof(struct bch_replicas_entry); + for (i = 0; i < r->entry_size - 1; i++) + bytes += hweight8(e->devs[i]); + } - entry_size = offsetof(struct bch_replicas_cpu_entry, devs) + - DIV_ROUND_UP(max_dev + 1, 8); - entry_size = max(entry_size, gc_r->entry_size); - nr = gc_r->nr + 1; + sb_r = bch2_fs_sb_resize_replicas(c, + DIV_ROUND_UP(sizeof(*sb_r) + bytes, sizeof(u64))); + if (!sb_r) + return -ENOSPC; - new = kzalloc(sizeof(struct bch_replicas_cpu) + - nr * entry_size, GFP_NOIO); - if (!new) - return -ENOMEM; + memset(&sb_r->entries, 0, + vstruct_end(&sb_r->field) - + (void *) &sb_r->entries); - new->nr = nr; - new->entry_size = entry_size; + sb_e = sb_r->entries; + for_each_cpu_replicas_entry(r, e) { + sb_e->data_type = e->data_type; - for (i = 0; i < gc_r->nr; i++) - memcpy(cpu_replicas_entry(new, i), - cpu_replicas_entry(gc_r, i), - gc_r->entry_size); + for (i = 0; i < replicas_dev_slots(r); i++) + if (replicas_test_dev(e, i)) + sb_e->devs[sb_e->nr++] = i; - memcpy(cpu_replicas_entry(new, nr - 1), - &new_e, - new->entry_size); + sb_e = replicas_entry_next(sb_e); - eytzinger0_sort(new->entries, - new->nr, - new->entry_size, - memcmp, NULL); + BUG_ON((void *) sb_e > vstruct_end(&sb_r->field)); + } - rcu_assign_pointer(c->replicas_gc, new); - kfree_rcu(gc_r, rcu); return 0; } -static bool replicas_has_extent(struct bch_replicas_cpu *r, - struct bkey_s_c_extent e, - enum bch_data_type data_type) -{ - struct bch_replicas_cpu_entry search; - unsigned max_dev; - - bkey_to_replicas(e, data_type, &search, &max_dev); - - return max_dev < replicas_dev_slots(r) && - eytzinger0_find(r->entries, r->nr, - r->entry_size, - memcmp, &search) < r->nr; -} - -bool bch2_sb_has_replicas(struct bch_fs *c, struct bkey_s_c_extent e, - enum bch_data_type data_type) -{ - bool ret; - - rcu_read_lock(); - ret = replicas_has_extent(rcu_dereference(c->replicas), - e, data_type); - rcu_read_unlock(); - - return ret; -} - -noinline -static int bch2_check_mark_super_slowpath(struct bch_fs *c, - struct bkey_s_c_extent e, - enum bch_data_type data_type) +static const char *bch2_sb_validate_replicas(struct bch_sb *sb) { - struct bch_replicas_cpu *gc_r; - const struct bch_extent_ptr *ptr; + struct bch_sb_field_members *mi; struct bch_sb_field_replicas *sb_r; - struct bch_replicas_entry *new_entry; - unsigned new_entry_bytes, new_u64s, nr, bytes, max_dev; - int ret = 0; + struct bch_replicas_cpu *cpu_r = NULL; + struct bch_replicas_entry *e; + const char *err; + unsigned i; - mutex_lock(&c->sb_lock); + mi = bch2_sb_get_members(sb); + sb_r = bch2_sb_get_replicas(sb); + if (!sb_r) + return NULL; - gc_r = rcu_dereference_protected(c->replicas_gc, - lockdep_is_held(&c->sb_lock)); - if (gc_r && - !replicas_has_extent(gc_r, e, data_type)) { - ret = bch2_update_gc_replicas(c, gc_r, e, data_type); - if (ret) + for_each_replicas_entry(sb_r, e) { + err = "invalid replicas entry: invalid data type"; + if (e->data_type >= BCH_DATA_NR) goto err; - } - - /* recheck, might have raced */ - if (bch2_sb_has_replicas(c, e, data_type)) { - mutex_unlock(&c->sb_lock); - return 0; - } - new_entry_bytes = sizeof(struct bch_replicas_entry) + - bch2_extent_nr_dirty_ptrs(e.s_c); - - sb_r = bch2_sb_get_replicas(c->disk_sb); + err = "invalid replicas entry: no devices"; + if (!e->nr) + goto err; - bch2_sb_replicas_nr_entries(sb_r, &nr, &bytes, &max_dev); + err = "invalid replicas entry: too many devices"; + if (e->nr >= BCH_REPLICAS_MAX) + goto err; - new_u64s = DIV_ROUND_UP(bytes + new_entry_bytes, sizeof(u64)); + err = "invalid replicas entry: invalid device"; + for (i = 0; i < e->nr; i++) + if (!bch2_dev_exists(sb, mi, e->devs[i])) + goto err; + } - sb_r = bch2_fs_sb_resize_replicas(c, - DIV_ROUND_UP(sizeof(*sb_r) + bytes + new_entry_bytes, - sizeof(u64))); - if (!sb_r) { - ret = -ENOSPC; + err = "cannot allocate memory"; + cpu_r = __bch2_sb_replicas_to_cpu_replicas(sb_r); + if (!cpu_r) goto err; - } - new_entry = (void *) sb_r + bytes; - new_entry->data_type = data_type; - new_entry->nr = 0; + sort_cmp_size(cpu_r->entries, + cpu_r->nr, + cpu_r->entry_size, + memcmp, NULL); + + for (i = 0; i + 1 < cpu_r->nr; i++) { + struct bch_replicas_cpu_entry *l = + cpu_replicas_entry(cpu_r, i); + struct bch_replicas_cpu_entry *r = + cpu_replicas_entry(cpu_r, i + 1); - extent_for_each_ptr(e, ptr) - if (!ptr->cached) - new_entry->devs[new_entry->nr++] = ptr->dev; + BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0); - ret = bch2_sb_replicas_to_cpu_replicas(c); - if (ret) { - memset(new_entry, 0, - vstruct_end(&sb_r->field) - (void *) new_entry); - goto err; + err = "duplicate replicas entry"; + if (!memcmp(l, r, cpu_r->entry_size)) + goto err; } - bch2_write_super(c); + err = NULL; err: - mutex_unlock(&c->sb_lock); - return ret; + kfree(cpu_r); + return err; } -int bch2_check_mark_super(struct bch_fs *c, struct bkey_s_c_extent e, +/* Query replicas: */ + +bool bch2_sb_has_replicas(struct bch_fs *c, struct bkey_s_c_extent e, enum bch_data_type data_type) { - struct bch_replicas_cpu *gc_r; - bool marked; + struct bch_replicas_cpu_entry search; + unsigned max_dev; + bool ret; + + if (!bkey_to_replicas(e, data_type, &search, &max_dev)) + return true; rcu_read_lock(); - marked = replicas_has_extent(rcu_dereference(c->replicas), - e, data_type) && - (!(gc_r = rcu_dereference(c->replicas_gc)) || - replicas_has_extent(gc_r, e, data_type)); + ret = replicas_has_entry(rcu_dereference(c->replicas), + search, max_dev); rcu_read_unlock(); - if (marked) - return 0; - - return bch2_check_mark_super_slowpath(c, e, data_type); + return ret; } struct replicas_status __bch2_replicas_status(struct bch_fs *c, - struct bch_devs_mask online_devs) + struct bch_devs_mask online_devs) { + struct bch_sb_field_members *mi; struct bch_replicas_cpu_entry *e; struct bch_replicas_cpu *r; unsigned i, dev, dev_slots, nr_online, nr_offline; @@ -1137,14 +1334,15 @@ struct replicas_status __bch2_replicas_status(struct bch_fs *c, for (i = 0; i < ARRAY_SIZE(ret.replicas); i++) ret.replicas[i].nr_online = UINT_MAX; + mi = bch2_sb_get_members(c->disk_sb); rcu_read_lock(); - r = rcu_dereference(c->replicas); - dev_slots = min_t(unsigned, replicas_dev_slots(r), c->sb.nr_devices); - for (i = 0; i < r->nr; i++) { - e = cpu_replicas_entry(r, i); + r = rcu_dereference(c->replicas); + dev_slots = replicas_dev_slots(r); - BUG_ON(e->data_type >= ARRAY_SIZE(ret.replicas)); + for_each_cpu_replicas_entry(r, e) { + if (e->data_type >= ARRAY_SIZE(ret.replicas)) + panic("e %p data_type %u\n", e, e->data_type); nr_online = nr_offline = 0; @@ -1152,6 +1350,8 @@ struct replicas_status __bch2_replicas_status(struct bch_fs *c, if (!replicas_test_dev(e, dev)) continue; + BUG_ON(!bch2_dev_exists(c->disk_sb, mi, dev)); + if (test_bit(dev, online_devs.d)) nr_online++; else @@ -1216,7 +1416,7 @@ unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca) { struct bch_replicas_cpu_entry *e; struct bch_replicas_cpu *r; - unsigned i, ret = 0; + unsigned ret = 0; rcu_read_lock(); r = rcu_dereference(c->replicas); @@ -1224,191 +1424,13 @@ unsigned bch2_dev_has_data(struct bch_fs *c, struct bch_dev *ca) if (ca->dev_idx >= replicas_dev_slots(r)) goto out; - for (i = 0; i < r->nr; i++) { - e = cpu_replicas_entry(r, i); - + for_each_cpu_replicas_entry(r, e) if (replicas_test_dev(e, ca->dev_idx)) { ret |= 1 << e->data_type; break; } - } out: rcu_read_unlock(); return ret; } - -static const char *bch2_sb_validate_replicas(struct bch_sb *sb) -{ - struct bch_sb_field_members *mi; - struct bch_sb_field_replicas *sb_r; - struct bch_replicas_cpu *cpu_r = NULL; - struct bch_replicas_entry *e; - const char *err; - unsigned i; - - mi = bch2_sb_get_members(sb); - sb_r = bch2_sb_get_replicas(sb); - if (!sb_r) - return NULL; - - 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: too many devices"; - if (e->nr >= BCH_REPLICAS_MAX) - goto err; - - err = "invalid replicas entry: invalid device"; - for (i = 0; i < e->nr; i++) - if (!bch2_dev_exists(sb, mi, e->devs[i])) - goto err; - } - - err = "cannot allocate memory"; - cpu_r = __bch2_sb_replicas_to_cpu_replicas(sb_r); - if (!cpu_r) - goto err; - - sort_cmp_size(cpu_r->entries, - cpu_r->nr, - cpu_r->entry_size, - memcmp, NULL); - - for (i = 0; i + 1 < cpu_r->nr; i++) { - struct bch_replicas_cpu_entry *l = - cpu_replicas_entry(cpu_r, i); - struct bch_replicas_cpu_entry *r = - cpu_replicas_entry(cpu_r, i + 1); - - BUG_ON(memcmp(l, r, cpu_r->entry_size) > 0); - - err = "duplicate replicas entry"; - if (!memcmp(l, r, cpu_r->entry_size)) - goto err; - } - - err = NULL; -err: - kfree(cpu_r); - return err; -} - -int bch2_replicas_gc_end(struct bch_fs *c, int err) -{ - struct bch_sb_field_replicas *sb_r; - struct bch_replicas_cpu *r, *old_r; - struct bch_replicas_entry *dst_e; - size_t i, j, bytes, dev_slots; - int ret = 0; - - lockdep_assert_held(&c->replicas_gc_lock); - - mutex_lock(&c->sb_lock); - - r = rcu_dereference_protected(c->replicas_gc, - lockdep_is_held(&c->sb_lock)); - - if (err) { - rcu_assign_pointer(c->replicas_gc, NULL); - kfree_rcu(r, rcu); - goto err; - } - - dev_slots = replicas_dev_slots(r); - - bytes = sizeof(struct bch_sb_field_replicas); - - for (i = 0; i < r->nr; i++) { - struct bch_replicas_cpu_entry *e = - cpu_replicas_entry(r, i); - - bytes += sizeof(struct bch_replicas_entry); - for (j = 0; j < r->entry_size - 1; j++) - bytes += hweight8(e->devs[j]); - } - - sb_r = bch2_fs_sb_resize_replicas(c, - DIV_ROUND_UP(sizeof(*sb_r) + bytes, sizeof(u64))); - if (!sb_r) { - ret = -ENOSPC; - goto err; - } - - memset(&sb_r->entries, 0, - vstruct_end(&sb_r->field) - - (void *) &sb_r->entries); - - dst_e = sb_r->entries; - for (i = 0; i < r->nr; i++) { - struct bch_replicas_cpu_entry *src_e = - cpu_replicas_entry(r, i); - - dst_e->data_type = src_e->data_type; - - for (j = 0; j < dev_slots; j++) - if (replicas_test_dev(src_e, j)) - dst_e->devs[dst_e->nr++] = j; - - dst_e = replicas_entry_next(dst_e); - } - - old_r = rcu_dereference_protected(c->replicas, - lockdep_is_held(&c->sb_lock)); - rcu_assign_pointer(c->replicas, r); - rcu_assign_pointer(c->replicas_gc, NULL); - kfree_rcu(old_r, rcu); - - bch2_write_super(c); -err: - mutex_unlock(&c->sb_lock); - return ret; -} - -int bch2_replicas_gc_start(struct bch_fs *c, unsigned typemask) -{ - struct bch_replicas_cpu *r, *src; - unsigned i; - - lockdep_assert_held(&c->replicas_gc_lock); - - mutex_lock(&c->sb_lock); - BUG_ON(c->replicas_gc); - - src = rcu_dereference_protected(c->replicas, - lockdep_is_held(&c->sb_lock)); - - r = kzalloc(sizeof(struct bch_replicas_cpu) + - src->nr * src->entry_size, GFP_NOIO); - if (!r) { - mutex_unlock(&c->sb_lock); - return -ENOMEM; - } - - r->entry_size = src->entry_size; - r->nr = 0; - - for (i = 0; i < src->nr; i++) { - struct bch_replicas_cpu_entry *dst_e = - cpu_replicas_entry(r, r->nr); - struct bch_replicas_cpu_entry *src_e = - cpu_replicas_entry(src, i); - - if (!(src_e->data_type & typemask)) { - memcpy(dst_e, src_e, r->entry_size); - r->nr++; - } - } - - eytzinger0_sort(r->entries, - r->nr, - r->entry_size, - memcmp, NULL); - - rcu_assign_pointer(c->replicas_gc, r); - mutex_unlock(&c->sb_lock); - - return 0; -} |