X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=cmd_debug.c;h=6d5f1c5f9d13c14c713d44b3e23ab532c072111e;hb=2aed686c235a7d08adf601f147f823eb62e697a1;hp=1a2c1dbd195c83b699d1cbcb6b81435ae2b83beb;hpb=1cf4d51dc4661f336f5318c176a3561ddf5bf04f;p=bcachefs-tools-debian diff --git a/cmd_debug.c b/cmd_debug.c index 1a2c1db..6d5f1c5 100644 --- a/cmd_debug.c +++ b/cmd_debug.c @@ -9,12 +9,13 @@ #include "tools-util.h" #include "libbcachefs/bcachefs.h" -#include "libbcachefs/alloc.h" +#include "libbcachefs/bset.h" #include "libbcachefs/btree_cache.h" #include "libbcachefs/btree_iter.h" #include "libbcachefs/buckets.h" #include "libbcachefs/error.h" #include "libbcachefs/journal.h" +#include "libbcachefs/journal_io.h" #include "libbcachefs/super.h" static void dump_usage(void) @@ -24,6 +25,7 @@ static void dump_usage(void) "\n" "Options:\n" " -o output Output qcow2 image(s)\n" + " -f Force; overwrite when needed\n" " -h Display this help and exit\n" "Report bugs to "); } @@ -58,19 +60,40 @@ static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd) /* Btree: */ for (i = 0; i < BTREE_ID_NR; i++) { const struct bch_extent_ptr *ptr; - struct btree_iter iter; + struct bkey_ptrs_c ptrs; + struct btree_trans trans; + struct btree_iter *iter; struct btree *b; - for_each_btree_node(&iter, c, i, POS_MIN, 0, b) { - struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key); + bch2_trans_init(&trans, c, 0, 0); - extent_for_each_ptr(e, ptr) + __for_each_btree_node(&trans, iter, i, POS_MIN, 0, 1, 0, b) { + struct btree_node_iter iter; + struct bkey u; + struct bkey_s_c k; + + for_each_btree_node_key_unpack(b, k, &iter, &u) { + ptrs = bch2_bkey_ptrs_c(k); + + bkey_for_each_ptr(ptrs, ptr) + if (ptr->dev == ca->dev_idx) + range_add(&data, + ptr->offset << 9, + btree_bytes(c)); + } + } + + b = c->btree_roots[i].b; + if (!btree_node_fake(b)) { + ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key)); + + bkey_for_each_ptr(ptrs, ptr) if (ptr->dev == ca->dev_idx) range_add(&data, ptr->offset << 9, - b->written << 9); + btree_bytes(c)); } - bch2_btree_iter_unlock(&iter); + bch2_trans_exit(&trans); } qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data, @@ -80,20 +103,19 @@ static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd) int cmd_dump(int argc, char *argv[]) { struct bch_opts opts = bch2_opts_empty(); - struct bch_fs *c = NULL; struct bch_dev *ca; - const char *err; char *out = NULL; unsigned i, nr_devices = 0; bool force = false; int fd, opt; opt_set(opts, nochanges, true); - opt_set(opts, noreplay, true); + opt_set(opts, norecovery, true); opt_set(opts, degraded, true); opt_set(opts, errors, BCH_ON_ERROR_CONTINUE); + opt_set(opts, fix_errors, FSCK_OPT_YES); - while ((opt = getopt(argc, argv, "o:fh")) != -1) + while ((opt = getopt(argc, argv, "o:fvh")) != -1) switch (opt) { case 'o': out = optarg; @@ -101,20 +123,24 @@ int cmd_dump(int argc, char *argv[]) case 'f': force = true; break; + case 'v': + opt_set(opts, verbose, true); + break; case 'h': dump_usage(); exit(EXIT_SUCCESS); } - - if (optind >= argc) - die("Please supply device(s) to check"); + args_shift(optind); if (!out) die("Please supply output filename"); - err = bch2_fs_open(argv + optind, argc - optind, opts, &c); - if (err) - die("error opening %s: %s", argv[optind], err); + if (!argc) + die("Please supply device(s) to check"); + + struct bch_fs *c = bch2_fs_open(argv, argc, opts); + if (IS_ERR(c)) + die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c))); down_read(&c->gc_lock); @@ -151,67 +177,72 @@ int cmd_dump(int argc, char *argv[]) static void list_keys(struct bch_fs *c, enum btree_id btree_id, struct bpos start, struct bpos end) { - struct btree_iter iter; + struct btree_trans trans; + struct btree_iter *iter; struct bkey_s_c k; char buf[512]; + int ret; - for_each_btree_key(&iter, c, btree_id, start, - BTREE_ITER_PREFETCH, k) { + bch2_trans_init(&trans, c, 0, 0); + + for_each_btree_key(&trans, iter, btree_id, start, + BTREE_ITER_PREFETCH, k, ret) { if (bkey_cmp(k.k->p, end) > 0) break; - bch2_bkey_val_to_text(c, bkey_type(0, btree_id), - buf, sizeof(buf), k); + bch2_bkey_val_to_text(&PBUF(buf), c, k); puts(buf); } - bch2_btree_iter_unlock(&iter); + bch2_trans_exit(&trans); } static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id, struct bpos start, struct bpos end) { - struct btree_iter iter; + struct btree_trans trans; + struct btree_iter *iter; struct btree *b; char buf[4096]; - for_each_btree_node(&iter, c, btree_id, start, 0, b) { + bch2_trans_init(&trans, c, 0, 0); + + for_each_btree_node(&trans, iter, btree_id, start, 0, b) { if (bkey_cmp(b->key.k.p, end) > 0) break; - bch2_print_btree_node(c, b, buf, sizeof(buf)); + bch2_btree_node_to_text(&PBUF(buf), c, b); puts(buf); } - bch2_btree_iter_unlock(&iter); + bch2_trans_exit(&trans); } static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id, struct bpos start, struct bpos end) { - struct btree_iter iter; + struct btree_trans trans; + struct btree_iter *iter; struct btree_node_iter node_iter; struct bkey unpacked; struct bkey_s_c k; struct btree *b; char buf[4096]; - for_each_btree_node(&iter, c, btree_id, start, 0, b) { + bch2_trans_init(&trans, c, 0, 0); + + for_each_btree_node(&trans, iter, btree_id, start, 0, b) { if (bkey_cmp(b->key.k.p, end) > 0) break; - bch2_print_btree_node(c, b, buf, sizeof(buf)); + bch2_btree_node_to_text(&PBUF(buf), c, b); fputs(buf, stdout); - buf[0] = '\t'; - - for_each_btree_node_key_unpack(b, k, &node_iter, - btree_node_is_extents(b), - &unpacked) { - bch2_bkey_val_to_text(c, bkey_type(0, btree_id), - buf + 1, sizeof(buf) - 1, k); + for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) { + bch2_bkey_val_to_text(&PBUF(buf), c, k); + putchar('\t'); puts(buf); } } - bch2_btree_iter_unlock(&iter); + bch2_trans_exit(&trans); } static struct bpos parse_pos(char *buf) @@ -235,8 +266,8 @@ static struct bpos parse_pos(char *buf) static void list_keys_usage(void) { - puts("bcachefs list_keys - list filesystem metadata to stdout\n" - "Usage: bcachefs list_keys [OPTION]... \n" + puts("bcachefs list - list filesystem metadata to stdout\n" + "Usage: bcachefs list [OPTION]... \n" "\n" "Options:\n" " -b (extents|inodes|dirents|xattrs) Btree to list from\n" @@ -244,6 +275,8 @@ static void list_keys_usage(void) " -e inode:offset End position\n" " -i inode List keys for a given inode number\n" " -m (keys|formats) List mode\n" + " -f Check (fsck) the filesystem first\n" + " -v Verbose mode\n" " -h Display this help and exit\n" "Report bugs to "); } @@ -258,10 +291,8 @@ static const char * const list_modes[] = { int cmd_list(int argc, char *argv[]) { struct bch_opts opts = bch2_opts_empty(); - struct bch_fs *c = NULL; enum btree_id btree_id = BTREE_ID_EXTENTS; struct bpos start = POS_MIN, end = POS_MAX; - const char *err; u64 inum; int mode = 0, opt; @@ -297,19 +328,20 @@ int cmd_list(int argc, char *argv[]) opt_set(opts, norecovery, false); break; case 'v': - opt_set(opts, verbose_recovery, true); + opt_set(opts, verbose, true); break; case 'h': list_keys_usage(); exit(EXIT_SUCCESS); } + args_shift(optind); - if (optind >= argc) - die("Please supply device(s) to check"); + if (!argc) + die("Please supply device(s)"); - err = bch2_fs_open(argv + optind, argc - optind, opts, &c); - if (err) - die("error opening %s: %s", argv[optind], err); + struct bch_fs *c = bch2_fs_open(argv, argc, opts); + if (IS_ERR(c)) + die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c))); switch (mode) { case 0: @@ -328,3 +360,58 @@ int cmd_list(int argc, char *argv[]) bch2_fs_stop(c); return 0; } + +static void list_journal_usage(void) +{ + puts("bcachefs list_journal - print contents of journal\n" + "Usage: bcachefs list_journal [OPTION]... \n" + "\n" + "Options:\n" + " -h Display this help and exit\n" + "Report bugs to "); +} + +int cmd_list_journal(int argc, char *argv[]) +{ + struct bch_opts opts = bch2_opts_empty(); + int opt; + + opt_set(opts, nochanges, true); + opt_set(opts, norecovery, true); + opt_set(opts, degraded, true); + opt_set(opts, errors, BCH_ON_ERROR_CONTINUE); + opt_set(opts, fix_errors, FSCK_OPT_YES); + opt_set(opts, keep_journal, true); + + while ((opt = getopt(argc, argv, "h")) != -1) + switch (opt) { + case 'h': + list_journal_usage(); + exit(EXIT_SUCCESS); + } + args_shift(optind); + + if (!argc) + die("Please supply device(s) to open"); + + struct bch_fs *c = bch2_fs_open(argv, argc, opts); + if (IS_ERR(c)) + die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c))); + + struct journal_replay *p; + struct jset_entry *entry; + struct bkey_i *k, *_n; + + /* This could be greatly expanded: */ + + list_for_each_entry(p, &c->journal_entries, list) + for_each_jset_key(k, _n, entry, &p->j) { + char buf[200]; + + bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(k)); + printk(KERN_INFO "%s\n", buf); + } + + bch2_fs_stop(c); + return 0; +}