Commit d4c8bb69 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: Convert bch2_fs_open() to darray

Open coded dynamic arrays are deprecated.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 0f0fc312
...@@ -69,9 +69,15 @@ static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more, ...@@ -69,9 +69,15 @@ static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more,
_ret; \ _ret; \
}) })
#define darray_remove_item(_d, _pos) \
array_remove_item((_d)->data, (_d)->nr, (_pos) - (_d)->data)
#define darray_for_each(_d, _i) \ #define darray_for_each(_d, _i) \
for (_i = (_d).data; _i < (_d).data + (_d).nr; _i++) for (_i = (_d).data; _i < (_d).data + (_d).nr; _i++)
#define darray_for_each_reverse(_d, _i) \
for (_i = (_d).data + (_d).nr - 1; _i >= (_d).data; --_i)
#define darray_init(_d) \ #define darray_init(_d) \
do { \ do { \
(_d)->data = NULL; \ (_d)->data = NULL; \
......
...@@ -1885,9 +1885,9 @@ struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name) ...@@ -1885,9 +1885,9 @@ struct bch_dev *bch2_dev_lookup(struct bch_fs *c, const char *name)
struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
struct bch_opts opts) struct bch_opts opts)
{ {
struct bch_sb_handle *sb = NULL; DARRAY(struct bch_sb_handle) sbs = { 0 };
struct bch_fs *c = NULL; struct bch_fs *c = NULL;
unsigned i, best_sb = 0; struct bch_sb_handle *sb, *best = NULL;
struct printbuf errbuf = PRINTBUF; struct printbuf errbuf = PRINTBUF;
int ret = 0; int ret = 0;
...@@ -1899,49 +1899,46 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, ...@@ -1899,49 +1899,46 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
goto err; goto err;
} }
sb = kcalloc(nr_devices, sizeof(*sb), GFP_KERNEL); ret = darray_make_room(&sbs, nr_devices);
if (!sb) { if (ret)
ret = -ENOMEM;
goto err; goto err;
}
for (i = 0; i < nr_devices; i++) { for (unsigned i = 0; i < nr_devices; i++) {
ret = bch2_read_super(devices[i], &opts, &sb[i]); struct bch_sb_handle sb = { NULL };
ret = bch2_read_super(devices[i], &opts, &sb);
if (ret) if (ret)
goto err; goto err;
BUG_ON(darray_push(&sbs, sb));
} }
for (i = 1; i < nr_devices; i++) darray_for_each(sbs, sb)
if (le64_to_cpu(sb[i].sb->seq) > if (!best || le64_to_cpu(sb->sb->seq) > le64_to_cpu(best->sb->seq))
le64_to_cpu(sb[best_sb].sb->seq)) best = sb;
best_sb = i;
darray_for_each_reverse(sbs, sb) {
i = 0; if (sb != best && !bch2_dev_exists(best->sb, sb->sb->dev_idx)) {
while (i < nr_devices) { pr_info("%pg has been removed, skipping", sb->bdev);
if (i != best_sb && bch2_free_super(sb);
!bch2_dev_exists(sb[best_sb].sb, sb[i].sb->dev_idx)) { darray_remove_item(&sbs, sb);
pr_info("%pg has been removed, skipping", sb[i].bdev); best -= best > sb;
bch2_free_super(&sb[i]);
array_remove_item(sb, nr_devices, i);
continue; continue;
} }
ret = bch2_dev_in_fs(sb[best_sb].sb, sb[i].sb); ret = bch2_dev_in_fs(best->sb, sb->sb);
if (ret) if (ret)
goto err_print; goto err_print;
i++;
} }
c = bch2_fs_alloc(sb[best_sb].sb, opts); c = bch2_fs_alloc(best->sb, opts);
if (IS_ERR(c)) { ret = PTR_ERR_OR_ZERO(c);
ret = PTR_ERR(c); if (ret)
goto err; goto err;
}
down_write(&c->state_lock); down_write(&c->state_lock);
for (i = 0; i < nr_devices; i++) { darray_for_each(sbs, sb) {
ret = bch2_dev_attach_bdev(c, &sb[i]); ret = bch2_dev_attach_bdev(c, sb);
if (ret) { if (ret) {
up_write(&c->state_lock); up_write(&c->state_lock);
goto err; goto err;
...@@ -1960,7 +1957,9 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, ...@@ -1960,7 +1957,9 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
goto err; goto err;
} }
out: out:
kfree(sb); darray_for_each(sbs, sb)
bch2_free_super(sb);
darray_exit(&sbs);
printbuf_exit(&errbuf); printbuf_exit(&errbuf);
module_put(THIS_MODULE); module_put(THIS_MODULE);
return c; return c;
...@@ -1970,9 +1969,6 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices, ...@@ -1970,9 +1969,6 @@ struct bch_fs *bch2_fs_open(char * const *devices, unsigned nr_devices,
err: err:
if (!IS_ERR_OR_NULL(c)) if (!IS_ERR_OR_NULL(c))
bch2_fs_stop(c); bch2_fs_stop(c);
if (sb)
for (i = 0; i < nr_devices; i++)
bch2_free_super(&sb[i]);
c = ERR_PTR(ret); c = ERR_PTR(ret);
goto out; goto out;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment