]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_debug.c
Update bcachefs sources to 96b991466a bcachefs: Improve error message in fsck
[bcachefs-tools-debian] / cmd_debug.c
index 5da97daa4ac139031af4ecc8516a7fbf9efe08af..6d5f1c5f9d13c14c713d44b3e23ab532c072111e 100644 (file)
@@ -15,6 +15,7 @@
 #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)
@@ -59,20 +60,38 @@ 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 bkey_ptrs_c ptrs;
                struct btree_trans trans;
                struct btree_iter *iter;
                struct btree *b;
 
-               bch2_trans_init(&trans, c);
+               bch2_trans_init(&trans, c, 0, 0);
 
-               for_each_btree_node(&trans, iter, i, POS_MIN, 0, b) {
-                       struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
+               __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;
 
-                       extent_for_each_ptr(e, ptr)
+                       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_trans_exit(&trans);
        }
@@ -91,11 +110,12 @@ int cmd_dump(int argc, char *argv[])
        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;
@@ -103,6 +123,9 @@ 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);
@@ -158,11 +181,12 @@ static void list_keys(struct bch_fs *c, enum btree_id btree_id,
        struct btree_iter *iter;
        struct bkey_s_c k;
        char buf[512];
+       int ret;
 
-       bch2_trans_init(&trans, c);
+       bch2_trans_init(&trans, c, 0, 0);
 
        for_each_btree_key(&trans, iter, btree_id, start,
-                          BTREE_ITER_PREFETCH, k) {
+                          BTREE_ITER_PREFETCH, k, ret) {
                if (bkey_cmp(k.k->p, end) > 0)
                        break;
 
@@ -180,7 +204,7 @@ static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
        struct btree *b;
        char buf[4096];
 
-       bch2_trans_init(&trans, c);
+       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)
@@ -203,7 +227,7 @@ static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id,
        struct btree *b;
        char buf[4096];
 
-       bch2_trans_init(&trans, c);
+       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)
@@ -336,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]... <devices>\n"
+            "\n"
+            "Options:\n"
+            "  -h            Display this help and exit\n"
+            "Report bugs to <linux-bcache@vger.kernel.org>");
+}
+
+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;
+}