]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - cmd_list_journal.c
Update bcachefs sources to 1a739db0b256 bcachefs; guard against overflow in btree...
[bcachefs-tools-debian] / cmd_list_journal.c
index ee467d43661c3bfb5b47d52575a470b7684cfc24..5bae622c84335ccf2f1c656095a5d951c9267de7 100644 (file)
@@ -16,6 +16,9 @@
 #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"
@@ -52,21 +55,34 @@ typedef DARRAY(enum btree_id) d_btree_id;
 
 static bool bkey_matches_filter(d_bbpos filter, struct jset_entry *entry, struct bkey_i *k)
 {
-       struct bbpos *i;
-
        darray_for_each(filter, i) {
                if (i->btree != entry->btree_id)
                        continue;
 
-               if (!btree_node_type_is_extents(i->btree)) {
-                       if (bkey_eq(i->pos, k->k.p))
-                               return true;
-               } else {
-                       if (bkey_ge(i->pos, bkey_start_pos(&k->k)) &&
-                           bkey_lt(i->pos, k->k.p))
+               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;
 }
 
@@ -78,16 +94,9 @@ static bool should_print_transaction(struct jset_entry *entry, struct jset_entry
 
        for (entry = vstruct_next(entry);
             entry != end && !entry_is_transaction_start(entry);
-            entry = vstruct_next(entry)) {
-               if (entry->type == BCH_JSET_ENTRY_btree_root ||
-                   entry->type == BCH_JSET_ENTRY_btree_keys) {
-                       struct bkey_i *k;
-
-                       vstruct_for_each(entry, k)
-                               if (bkey_matches_filter(filter, entry, k))
-                                       return true;
-               }
-       }
+            entry = vstruct_next(entry))
+               if (entry_matches_transaction_filter(entry, filter))
+                       return true;
 
        return false;
 }
@@ -95,7 +104,6 @@ static bool should_print_transaction(struct jset_entry *entry, struct jset_entry
 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;
@@ -105,7 +113,7 @@ static bool should_print_entry(struct jset_entry *entry, d_btree_id filter)
            entry->type != BCH_JSET_ENTRY_overwrite)
                return true;
 
-       vstruct_for_each(entry, k)
+       jset_entry_for_each_key(entry, k)
                darray_for_each(filter, id)
                        if (entry->btree_id == *id)
                                return true;
@@ -129,11 +137,11 @@ static void journal_entries_print(struct bch_fs *c, unsigned nr_entries,
                if (le64_to_cpu(p->j.seq) + nr_entries < atomic64_read(&c->journal.seq))
                        continue;
 
-               bool blacklisted =
+               bool blacklisted = p->ignore ||
                        bch2_journal_seq_is_blacklisted(c,
                                        le64_to_cpu(p->j.seq), false);
 
-               if (!transaction_filter.nr) {
+               if (transaction_filter.nr) {
                        if (blacklisted)
                                printf("blacklisted ");
 
@@ -177,16 +185,24 @@ static void journal_entries_print(struct bch_fs *c, unsigned nr_entries,
                                prt_newline(&buf);
                        }
 
-                       if (should_print_entry(entry, key_filter)) {
-                               printbuf_indent_add(&buf, 4);
-                               bch2_journal_entry_to_text(&buf, c, entry);
+                       if (!should_print_entry(entry, key_filter))
+                               goto next;
 
-                               if (blacklisted)
-                                       star_start_of_lines(buf.buf);
-                               printf("%s\n", buf.buf);
-                               printbuf_reset(&buf);
-                       }
+                       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);
                }
        }
@@ -214,7 +230,7 @@ int cmd_list_journal(int argc, char *argv[])
        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, fix_errors,       FSCK_FIX_yes);
        opt_set(opts, keep_journal,     true);
        opt_set(opts, read_journal_only,true);
 
@@ -233,7 +249,7 @@ int cmd_list_journal(int argc, char *argv[])
                        darray_push(&transaction_filter, bbpos_parse(optarg));
                        break;
                case 'k':
-                       darray_push(&key_filter, read_string_list_or_die(optarg, bch2_btree_ids, "btree id"));
+                       darray_push(&key_filter, read_string_list_or_die(optarg, __bch2_btree_ids, "btree id"));
                        break;
                case 'v':
                        opt_set(opts, verbose, true);