+
+ for (btree_id = btree_id_start;
+ btree_id < btree_id_end;
+ btree_id++) {
+ switch (mode) {
+ case 0:
+ list_keys(c, btree_id, start, end);
+ break;
+ case 1:
+ list_btree_formats(c, btree_id, start, end);
+ break;
+ case 2:
+ list_nodes(c, btree_id, start, end);
+ break;
+ case 3:
+ list_nodes_ondisk(c, btree_id, start, end);
+ break;
+ case 4:
+ list_nodes_keys(c, btree_id, start, end);
+ break;
+ default:
+ die("Invalid mode");
+ }
+ }
+
+ 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"
+ " -a Read entire journal, not just dirty entries\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, "ah")) != -1)
+ switch (opt) {
+ case 'a':
+ opt_set(opts, read_entire_journal, true);
+ break;
+ 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) {
+ printf("journal entry %8llu\n"
+ " version %8u\n"
+ " last seq %8llu\n"
+ ,
+ le64_to_cpu(p->j.seq),
+ le32_to_cpu(p->j.version),
+ le64_to_cpu(p->j.last_seq));
+
+ 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));
+ printf("btree %s l %u: %s\n",
+ bch2_btree_ids[entry->btree_id],
+ entry->level,
+ buf);
+ }