]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_list_journal.c
remove debian/files
[bcachefs-tools-debian] / cmd_list_journal.c
index 869d3341dba1f5db32a425e99a2a0246da2f3da9..655bfe2e686e430416850c0d1fe9a54182371923 100644 (file)
@@ -1,30 +1,36 @@
 #include <fcntl.h>
+#include <getopt.h>
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #include "cmds.h"
 #include "libbcachefs.h"
-#include "qcow2.h"
 #include "tools-util.h"
 
 #include "libbcachefs/bcachefs.h"
 #include "libbcachefs/btree_iter.h"
+#include "libbcachefs/errcode.h"
 #include "libbcachefs/error.h"
 #include "libbcachefs/journal_io.h"
 #include "libbcachefs/journal_seq_blacklist.h"
 #include "libbcachefs/super.h"
 
+static const char *NORMAL      = "\x1B[0m";
+static const char *RED         = "\x1B[31m";
+
 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"
-            "  -a            Read entire journal, not just dirty entries\n"
-            "  -n            Number of journal entries to print, starting from the most recent\n"
-            "  -v            Verbose mode\n"
-            "  -h            Display this help and exit\n"
+            "  -a                                Read entire journal, not just dirty entries\n"
+            "  -n, --nr-entries=nr               Number of journal entries to print, starting from the most recent\n"
+            "  -t, --transaction-filter=bbpos    Filter transactions not updating <bbpos>\n"
+            "  -k, --key-filter=btree            Filter keys not updating btree\n"
+            "  -v, --verbose                     Verbose mode\n"
+            "  -h, --help                        Display this help and exit\n"
             "Report bugs to <linux-bcachefs@vger.kernel.org>");
 }
 
@@ -39,48 +45,91 @@ static void star_start_of_lines(char *buf)
                p[1] = '*';
 }
 
-int cmd_list_journal(int argc, char *argv[])
+static inline bool entry_is_transaction_start(struct jset_entry *entry)
 {
-       struct bch_opts opts = bch2_opts_empty();
-       u32 nr_entries = U32_MAX;
-       int opt;
+       return entry->type == BCH_JSET_ENTRY_log && !entry->level;
+}
 
-       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);
-       opt_set(opts, read_journal_only,true);
+typedef DARRAY(struct bbpos) d_bbpos;
+typedef DARRAY(enum btree_id) d_btree_id;
 
-       while ((opt = getopt(argc, argv, "an:vh")) != -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 'v':
-                       opt_set(opts, verbose, true);
-                       break;
-               case 'h':
-                       list_journal_usage();
-                       exit(EXIT_SUCCESS);
-               }
-       args_shift(optind);
+static bool bkey_matches_filter(d_bbpos filter, struct jset_entry *entry, struct bkey_i *k)
+{
+       struct bbpos *i;
 
-       if (!argc)
-               die("Please supply device(s) to open");
+       darray_for_each(filter, i) {
+               if (i->btree != entry->btree_id)
+                       continue;
 
-       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)));
+               if (bkey_eq(i->pos, k->k.p))
+                       return true;
+
+               if (btree_node_type_is_extents(i->btree) &&
+                   bkey_ge(i->pos, bkey_start_pos(&k->k)) &&
+                   bkey_lt(i->pos, k->k.p))
+                       return true;
+       }
+       return false;
+}
+
+static bool entry_matches_transaction_filter(struct jset_entry *entry,
+                                            d_bbpos filter)
+{
+       if (entry->type == BCH_JSET_ENTRY_btree_root ||
+           entry->type == BCH_JSET_ENTRY_btree_keys ||
+           entry->type == BCH_JSET_ENTRY_overwrite) {
+               struct bkey_i *k;
+
+               jset_entry_for_each_key(entry, k)
+                       if (bkey_matches_filter(filter, entry, k))
+                               return true;
+       }
+
+       return false;
+}
+
+static bool should_print_transaction(struct jset_entry *entry, struct jset_entry *end,
+                                    d_bbpos filter)
+{
+       if (!filter.nr)
+               return true;
 
+       for (entry = vstruct_next(entry);
+            entry != end && !entry_is_transaction_start(entry);
+            entry = vstruct_next(entry))
+               if (entry_matches_transaction_filter(entry, filter))
+                       return true;
+
+       return false;
+}
+
+static bool should_print_entry(struct jset_entry *entry, d_btree_id filter)
+{
+       struct bkey_i *k;
+       enum btree_id *id;
+
+       if (!filter.nr)
+               return true;
+
+       if (entry->type != BCH_JSET_ENTRY_btree_root &&
+           entry->type != BCH_JSET_ENTRY_btree_keys &&
+           entry->type != BCH_JSET_ENTRY_overwrite)
+               return true;
+
+       jset_entry_for_each_key(entry, k)
+               darray_for_each(filter, id)
+                       if (entry->btree_id == *id)
+                               return true;
+
+       return false;
+}
+
+static void journal_entries_print(struct bch_fs *c, unsigned nr_entries,
+                                 d_bbpos transaction_filter,
+                                 d_btree_id key_filter)
+{
        struct journal_replay *p, **_p;
        struct genradix_iter iter;
-       struct jset_entry *entry;
        struct printbuf buf = PRINTBUF;
 
        genradix_for_each(&c->journal_entries, iter, _p) {
@@ -95,152 +144,133 @@ int cmd_list_journal(int argc, char *argv[])
                        bch2_journal_seq_is_blacklisted(c,
                                        le64_to_cpu(p->j.seq), false);
 
-               if (blacklisted)
-                       printf("blacklisted ");
-
-               printf("journal entry       %llu\n", le64_to_cpu(p->j.seq));
+               if (!transaction_filter.nr) {
+                       if (blacklisted)
+                               printf("blacklisted ");
 
-               printbuf_reset(&buf);
+                       printf("journal entry     %llu\n", le64_to_cpu(p->j.seq));
 
-               prt_printf(&buf,
-                      "  version         %u\n"
-                      "  last seq        %llu\n"
-                      "  flush           %u\n"
-                      "  written at      ",
-                      le32_to_cpu(p->j.version),
-                      le64_to_cpu(p->j.last_seq),
-                      !JSET_NO_FLUSH(&p->j));
-               bch2_journal_ptrs_to_text(&buf, c, p);
+                       printbuf_reset(&buf);
 
-               if (blacklisted)
-                       star_start_of_lines(buf.buf);
-               printf("%s\n", buf.buf);
+                       prt_printf(&buf,
+                                  "  version         %u\n"
+                                  "  last seq        %llu\n"
+                                  "  flush           %u\n"
+                                  "  written at      ",
+                                  le32_to_cpu(p->j.version),
+                                  le64_to_cpu(p->j.last_seq),
+                                  !JSET_NO_FLUSH(&p->j));
+                       bch2_journal_ptrs_to_text(&buf, c, p);
 
-               vstruct_for_each(&p->j, entry) {
+                       if (blacklisted)
+                               star_start_of_lines(buf.buf);
+                       printf("%s\n", buf.buf);
                        printbuf_reset(&buf);
+               }
+
+               struct jset_entry *entry = p->j.start;
+               struct jset_entry *end = vstruct_last(&p->j);
+               while (entry != end) {
 
                        /*
                         * log entries denote the start of a new transaction
                         * commit:
                         */
-                       if (entry->type == BCH_JSET_ENTRY_log && !entry->level)
+                       if (entry_is_transaction_start(entry)) {
+                               if (!should_print_transaction(entry, end, transaction_filter)) {
+                                       do {
+                                               entry = vstruct_next(entry);
+                                       } while (entry != end && !entry_is_transaction_start(entry));
+
+                                       continue;
+                               }
+
                                prt_newline(&buf);
+                       }
+
+                       if (!should_print_entry(entry, key_filter))
+                               goto next;
+
+                       bool highlight = entry_matches_transaction_filter(entry, transaction_filter);
+                       if (highlight)
+                               fputs(RED, stdout);
+
                        printbuf_indent_add(&buf, 4);
                        bch2_journal_entry_to_text(&buf, c, entry);
 
                        if (blacklisted)
                                star_start_of_lines(buf.buf);
                        printf("%s\n", buf.buf);
+                       printbuf_reset(&buf);
+
+                       if (highlight)
+                               fputs(NORMAL, stdout);
+next:
+                       entry = vstruct_next(entry);
                }
        }
 
        printbuf_exit(&buf);
-       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-bcachefs@vger.kernel.org>");
 }
 
-int cmd_kill_btree_node(int argc, char *argv[])
+int cmd_list_journal(int argc, char *argv[])
 {
+       static const struct option longopts[] = {
+               { "nr-entries",         required_argument,      NULL, 'n' },
+               { "transaction-filter", required_argument,      NULL, 't' },
+               { "key-filter",         required_argument,      NULL, 'k' },
+               { "verbose",            no_argument,            NULL, 'v' },
+               { "help",               no_argument,            NULL, 'h' },
+               { NULL }
+       };
        struct bch_opts opts = bch2_opts_empty();
-       enum btree_id btree_id = 0;
-       unsigned level = 0;
-       u64 node_index = 0;
+       u32 nr_entries = U32_MAX;
+       d_bbpos         transaction_filter = { 0 };
+       d_btree_id      key_filter = { 0 };
        int opt;
 
-       opt_set(opts, read_only,        true);
+       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_FIX_yes);
+       opt_set(opts, keep_journal,     true);
+       opt_set(opts, read_journal_only,true);
 
-       while ((opt = getopt(argc, argv, "b:l:i:h")) != -1)
+       while ((opt = getopt_long(argc, argv, "an:t:k:vh",
+                                 longopts, NULL)) != -1)
                switch (opt) {
-               case 'b':
-                       btree_id = read_string_list_or_die(optarg,
-                                               bch2_btree_ids, "btree id");
+               case 'a':
+                       opt_set(opts, read_entire_journal, true);
+                       break;
+               case 'n':
+                       if (kstrtouint(optarg, 10, &nr_entries))
+                               die("error parsing nr_entries");
+                       opt_set(opts, read_entire_journal, true);
                        break;
-               case 'l':
-                       if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
-                               die("invalid level");
+               case 't':
+                       darray_push(&transaction_filter, bbpos_parse(optarg));
                        break;
-               case 'i':
-                       if (kstrtoull(optarg, 10, &node_index))
-                               die("invalid index %s", optarg);
+               case 'k':
+                       darray_push(&key_filter, read_string_list_or_die(optarg, bch2_btree_ids, "btree id"));
+                       break;
+               case 'v':
+                       opt_set(opts, verbose, true);
                        break;
                case 'h':
-                       kill_btree_node_usage();
+                       list_journal_usage();
                        exit(EXIT_SUCCESS);
                }
        args_shift(optind);
 
        if (!argc)
-               die("Please supply device(s)");
+               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 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);
+               die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
 
+       journal_entries_print(c, nr_entries, transaction_filter, key_filter);
        bch2_fs_stop(c);
-       return ret;
+       return 0;
 }