8 #include "libbcachefs.h"
9 #include "tools-util.h"
11 #include "libbcachefs/bcachefs.h"
12 #include "libbcachefs/btree_iter.h"
13 #include "libbcachefs/errcode.h"
14 #include "libbcachefs/error.h"
15 #include "libbcachefs/journal_io.h"
16 #include "libbcachefs/journal_seq_blacklist.h"
17 #include "libbcachefs/super.h"
19 static const char *NORMAL = "\x1B[0m";
20 static const char *RED = "\x1B[31m";
22 static void list_journal_usage(void)
24 puts("bcachefs list_journal - print contents of journal\n"
25 "Usage: bcachefs list_journal [OPTION]... <devices>\n"
28 " -a Read entire journal, not just dirty entries\n"
29 " -n, --nr-entries=nr Number of journal entries to print, starting from the most recent\n"
30 " -t, --transaction-filter=bbpos Filter transactions not updating <bbpos>\n"
31 " -k, --key-filter=btree Filter keys not updating btree\n"
32 " -v, --verbose Verbose mode\n"
33 " -h, --help Display this help and exit\n"
34 "Report bugs to <linux-bcachefs@vger.kernel.org>");
37 static void star_start_of_lines(char *buf)
44 while ((p = strstr(p, "\n ")))
48 static inline bool entry_is_transaction_start(struct jset_entry *entry)
50 return entry->type == BCH_JSET_ENTRY_log && !entry->level;
53 typedef DARRAY(struct bbpos) d_bbpos;
54 typedef DARRAY(enum btree_id) d_btree_id;
56 static bool bkey_matches_filter(d_bbpos filter, struct jset_entry *entry, struct bkey_i *k)
60 darray_for_each(filter, i) {
61 if (i->btree != entry->btree_id)
64 if (!btree_node_type_is_extents(i->btree)) {
65 if (bkey_eq(i->pos, k->k.p))
68 if (bkey_ge(i->pos, bkey_start_pos(&k->k)) &&
69 bkey_lt(i->pos, k->k.p))
76 static bool entry_matches_transaction_filter(struct jset_entry *entry,
79 if (entry->type == BCH_JSET_ENTRY_btree_root ||
80 entry->type == BCH_JSET_ENTRY_btree_keys ||
81 entry->type == BCH_JSET_ENTRY_overwrite) {
84 jset_entry_for_each_key(entry, k)
85 if (bkey_matches_filter(filter, entry, k))
92 static bool should_print_transaction(struct jset_entry *entry, struct jset_entry *end,
98 for (entry = vstruct_next(entry);
99 entry != end && !entry_is_transaction_start(entry);
100 entry = vstruct_next(entry))
101 if (entry_matches_transaction_filter(entry, filter))
107 static bool should_print_entry(struct jset_entry *entry, d_btree_id filter)
115 if (entry->type != BCH_JSET_ENTRY_btree_root &&
116 entry->type != BCH_JSET_ENTRY_btree_keys &&
117 entry->type != BCH_JSET_ENTRY_overwrite)
120 jset_entry_for_each_key(entry, k)
121 darray_for_each(filter, id)
122 if (entry->btree_id == *id)
128 static void journal_entries_print(struct bch_fs *c, unsigned nr_entries,
129 d_bbpos transaction_filter,
130 d_btree_id key_filter)
132 struct journal_replay *p, **_p;
133 struct genradix_iter iter;
134 struct printbuf buf = PRINTBUF;
136 genradix_for_each(&c->journal_entries, iter, _p) {
141 if (le64_to_cpu(p->j.seq) + nr_entries < atomic64_read(&c->journal.seq))
145 bch2_journal_seq_is_blacklisted(c,
146 le64_to_cpu(p->j.seq), false);
148 if (!transaction_filter.nr) {
150 printf("blacklisted ");
152 printf("journal entry %llu\n", le64_to_cpu(p->j.seq));
154 printbuf_reset(&buf);
161 le32_to_cpu(p->j.version),
162 le64_to_cpu(p->j.last_seq),
163 !JSET_NO_FLUSH(&p->j));
164 bch2_journal_ptrs_to_text(&buf, c, p);
167 star_start_of_lines(buf.buf);
168 printf("%s\n", buf.buf);
169 printbuf_reset(&buf);
172 struct jset_entry *entry = p->j.start;
173 struct jset_entry *end = vstruct_last(&p->j);
174 while (entry != end) {
177 * log entries denote the start of a new transaction
180 if (entry_is_transaction_start(entry)) {
181 if (!should_print_transaction(entry, end, transaction_filter)) {
183 entry = vstruct_next(entry);
184 } while (entry != end && !entry_is_transaction_start(entry));
192 if (!should_print_entry(entry, key_filter))
195 bool highlight = entry_matches_transaction_filter(entry, transaction_filter);
199 printbuf_indent_add(&buf, 4);
200 bch2_journal_entry_to_text(&buf, c, entry);
203 star_start_of_lines(buf.buf);
204 printf("%s\n", buf.buf);
205 printbuf_reset(&buf);
208 fputs(NORMAL, stdout);
210 entry = vstruct_next(entry);
217 int cmd_list_journal(int argc, char *argv[])
219 static const struct option longopts[] = {
220 { "nr-entries", required_argument, NULL, 'n' },
221 { "transaction-filter", required_argument, NULL, 't' },
222 { "key-filter", required_argument, NULL, 'k' },
223 { "verbose", no_argument, NULL, 'v' },
224 { "help", no_argument, NULL, 'h' },
227 struct bch_opts opts = bch2_opts_empty();
228 u32 nr_entries = U32_MAX;
229 d_bbpos transaction_filter = { 0 };
230 d_btree_id key_filter = { 0 };
233 opt_set(opts, nochanges, true);
234 opt_set(opts, norecovery, true);
235 opt_set(opts, degraded, true);
236 opt_set(opts, errors, BCH_ON_ERROR_continue);
237 opt_set(opts, fix_errors, FSCK_OPT_YES);
238 opt_set(opts, keep_journal, true);
239 opt_set(opts, read_journal_only,true);
241 while ((opt = getopt_long(argc, argv, "an:t:k:vh",
242 longopts, NULL)) != -1)
245 opt_set(opts, read_entire_journal, true);
248 if (kstrtouint(optarg, 10, &nr_entries))
249 die("error parsing nr_entries");
250 opt_set(opts, read_entire_journal, true);
253 darray_push(&transaction_filter, bbpos_parse(optarg));
256 darray_push(&key_filter, read_string_list_or_die(optarg, bch2_btree_ids, "btree id"));
259 opt_set(opts, verbose, true);
262 list_journal_usage();
268 die("Please supply device(s) to open");
270 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
272 die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
274 journal_entries_print(c, nr_entries, transaction_filter, key_filter);