]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_debug.c
Update bcachefs sources to bdf6d7c135 fixup! bcachefs: Kill journal buf bloom filter
[bcachefs-tools-debian] / cmd_debug.c
index 36cadfa9a70c8898f4f1c37b6aec060c489a0528..c2206dacdb5697ef168f365d92f92bde04869fac 100644 (file)
@@ -29,19 +29,19 @@ static void dump_usage(void)
             "Options:\n"
             "  -o output     Output qcow2 image(s)\n"
             "  -f            Force; overwrite when needed\n"
+            "  -j            Dump entire journal, not just dirty entries\n"
             "  -h            Display this help and exit\n"
             "Report bugs to <linux-bcache@vger.kernel.org>");
 }
 
-static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd)
+static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd,
+                           bool entire_journal)
 {
        struct bch_sb *sb = ca->disk_sb.sb;
-       ranges data;
+       ranges data = { 0 };
        unsigned i;
        int ret;
 
-       darray_init(data);
-
        /* Superblock: */
        range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
                  sizeof(struct bch_sb_layout));
@@ -53,7 +53,8 @@ static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd)
 
        /* Journal: */
        for (i = 0; i < ca->journal.nr; i++)
-               if (ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
+               if (entire_journal ||
+                   ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
                        u64 bucket = ca->journal.buckets[i];
 
                        range_add(&data,
@@ -107,7 +108,7 @@ static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd)
 
        qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
                          max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
-       darray_free(data);
+       darray_exit(&data);
 }
 
 int cmd_dump(int argc, char *argv[])
@@ -116,7 +117,7 @@ int cmd_dump(int argc, char *argv[])
        struct bch_dev *ca;
        char *out = NULL;
        unsigned i, nr_devices = 0;
-       bool force = false;
+       bool force = false, entire_journal = false;
        int fd, opt;
 
        opt_set(opts, nochanges,        true);
@@ -125,7 +126,7 @@ int cmd_dump(int argc, char *argv[])
        opt_set(opts, errors,           BCH_ON_ERROR_continue);
        opt_set(opts, fix_errors,       FSCK_OPT_NO);
 
-       while ((opt = getopt(argc, argv, "o:fvh")) != -1)
+       while ((opt = getopt(argc, argv, "o:fjvh")) != -1)
                switch (opt) {
                case 'o':
                        out = optarg;
@@ -133,6 +134,9 @@ int cmd_dump(int argc, char *argv[])
                case 'f':
                        force = true;
                        break;
+               case 'j':
+                       entire_journal = true;
+                       break;
                case 'v':
                        opt_set(opts, verbose, true);
                        break;
@@ -174,7 +178,7 @@ int cmd_dump(int argc, char *argv[])
                fd = xopen(path, flags, 0600);
                free(path);
 
-               dump_one_device(c, ca, fd);
+               dump_one_device(c, ca, fd, entire_journal);
                close(fd);
        }
 
@@ -574,6 +578,7 @@ static void list_journal_usage(void)
             "\n"
             "Options:\n"
             "  -a            Read entire journal, not just dirty entries\n"
+            "  -n            Number of journal entries to print, starting from the most recent\n"
             "  -h            Display this help and exit\n"
             "Report bugs to <linux-bcache@vger.kernel.org>");
 }
@@ -592,6 +597,7 @@ static void star_start_of_lines(char *buf)
 int cmd_list_journal(int argc, char *argv[])
 {
        struct bch_opts opts = bch2_opts_empty();
+       u32 nr_entries = U32_MAX;
        int opt;
 
        opt_set(opts, nochanges,        true);
@@ -602,11 +608,15 @@ int cmd_list_journal(int argc, char *argv[])
        opt_set(opts, keep_journal,     true);
        opt_set(opts, read_journal_only,true);
 
-       while ((opt = getopt(argc, argv, "ah")) != -1)
+       while ((opt = getopt(argc, argv, "an:h")) != -1)
                switch (opt) {
                case 'a':
                        opt_set(opts, read_entire_journal, true);
                        break;
+               case 'n':
+                       nr_entries = kstrtouint(optarg, 10, &nr_entries);
+                       opt_set(opts, read_entire_journal, true);
+                       break;
                case 'h':
                        list_journal_usage();
                        exit(EXIT_SUCCESS);
@@ -620,16 +630,23 @@ int cmd_list_journal(int argc, char *argv[])
        if (IS_ERR(c))
                die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
 
-       struct journal_replay *p;
+       struct journal_replay *p, **_p;
+       struct genradix_iter iter;
        struct jset_entry *entry;
        struct printbuf buf = PRINTBUF;
 
-       list_for_each_entry(p, &c->journal_entries, list) {
+       genradix_for_each(&c->journal_entries, iter, _p) {
+               p = *_p;
+               if (!p)
+                       continue;
+
+               if (le64_to_cpu(p->j.seq) + nr_entries < atomic64_read(&c->journal.seq))
+                       continue;
+
                bool blacklisted =
                        bch2_journal_seq_is_blacklisted(c,
                                        le64_to_cpu(p->j.seq), false);
 
-
                if (blacklisted)
                        printf("blacklisted ");
 
@@ -658,8 +675,9 @@ int cmd_list_journal(int argc, char *argv[])
                         * log entries denote the start of a new transaction
                         * commit:
                         */
-                       pr_indent_push(&buf,
-                               entry->type == BCH_JSET_ENTRY_log ? 2 : 4);
+                       if (entry->type == BCH_JSET_ENTRY_log && !entry->level)
+                               pr_newline(&buf);
+                       pr_indent_push(&buf, 4);
                        bch2_journal_entry_to_text(&buf, c, entry);
 
                        if (blacklisted)
@@ -672,3 +690,109 @@ int cmd_list_journal(int argc, char *argv[])
        bch2_fs_stop(c);
        return 0;
 }
+
+static void kill_btree_node_usage(void)
+{
+       puts("bcachefs kill_btree_node - make btree nodes unreadable\n"
+            "Usage: bcachefs kill_btree_node [OPTION]... <devices>\n"
+            "\n"
+            "Options:\n"
+            "  -b (extents|inodes|dirents|xattrs)    Btree to delete from\n"
+            "  -l level                              Levle to delete from (0 == leaves)\n"
+            "  -i index                              Index of btree node to kill\n"
+            "  -h                                    Display this help and exit\n"
+            "Report bugs to <linux-bcache@vger.kernel.org>");
+}
+
+int cmd_kill_btree_node(int argc, char *argv[])
+{
+       struct bch_opts opts = bch2_opts_empty();
+       enum btree_id btree_id = 0;
+       unsigned level = 0;
+       u64 node_index = 0;
+       int opt;
+
+       opt_set(opts, read_only,        true);
+
+       while ((opt = getopt(argc, argv, "b:l:i:h")) != -1)
+               switch (opt) {
+               case 'b':
+                       btree_id = read_string_list_or_die(optarg,
+                                               bch2_btree_ids, "btree id");
+                       break;
+               case 'l':
+                       if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
+                               die("invalid level");
+                       break;
+               case 'i':
+                       if (kstrtoull(optarg, 10, &node_index))
+                               die("invalid index %s", optarg);
+                       break;
+               case 'h':
+                       kill_btree_node_usage();
+                       exit(EXIT_SUCCESS);
+               }
+       args_shift(optind);
+
+       if (!argc)
+               die("Please supply device(s)");
+
+       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 btree_trans trans;
+       struct btree_iter iter;
+       struct btree *b;
+       int ret;
+       void *zeroes;
+
+       ret = posix_memalign(&zeroes, c->opts.block_size, c->opts.block_size);
+       if (ret)
+               die("error %s from posix_memalign", strerror(ret));
+
+       bch2_trans_init(&trans, c, 0, 0);
+
+       __for_each_btree_node(&trans, iter, btree_id, POS_MIN, 0, level, 0, b, ret) {
+               if (b->c.level != level)
+                       continue;
+
+               if (!node_index) {
+                       struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
+                       const struct bch_extent_ptr *ptr;
+
+                       struct printbuf buf = PRINTBUF;
+
+                       bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
+                       bch_info(c, "killing btree node %s", buf.buf);
+                       printbuf_exit(&buf);
+
+                       bkey_for_each_ptr(ptrs, ptr) {
+                               struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
+
+                               ret = pwrite(ca->disk_sb.bdev->bd_fd, zeroes,
+                                            c->opts.block_size, ptr->offset << 9);
+                               if (ret != c->opts.block_size) {
+                                       bch_err(c, "pwrite error: expected %u got %i %s",
+                                               c->opts.block_size, ret, strerror(errno));
+                                       ret = EXIT_FAILURE;
+                                       goto done;
+                               }
+                       }
+                       goto done;
+               }
+
+               node_index--;
+       }
+       if (ret)
+               bch_err(c, "error %i walking btree nodes", ret);
+       else
+               bch_err(c, "node at specified index not found");
+       ret = EXIT_FAILURE;
+done:
+       bch2_trans_iter_exit(&trans, &iter);
+       bch2_trans_exit(&trans);
+
+       bch2_fs_stop(c);
+       return ret;
+}