]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_list_journal.c
cmd_list_journal: Add filter options
[bcachefs-tools-debian] / cmd_list_journal.c
1 #include <fcntl.h>
2 #include <getopt.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6
7 #include "cmds.h"
8 #include "libbcachefs.h"
9 #include "tools-util.h"
10
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"
18
19 static void list_journal_usage(void)
20 {
21         puts("bcachefs list_journal - print contents of journal\n"
22              "Usage: bcachefs list_journal [OPTION]... <devices>\n"
23              "\n"
24              "Options:\n"
25              "  -a                                Read entire journal, not just dirty entries\n"
26              "  -n, --nr-entries=nr               Number of journal entries to print, starting from the most recent\n"
27              "  -t, --transaction-filter=bbpos    Filter transactions not updating <bbpos>\n"
28              "  -k, --key-filter=btree            Filter keys not updating btree\n"
29              "  -v, --verbose                     Verbose mode\n"
30              "  -h, --help                        Display this help and exit\n"
31              "Report bugs to <linux-bcachefs@vger.kernel.org>");
32 }
33
34 static void star_start_of_lines(char *buf)
35 {
36         char *p = buf;
37
38         if (*p == ' ')
39                 *p = '*';
40
41         while ((p = strstr(p, "\n ")))
42                 p[1] = '*';
43 }
44
45 static inline bool entry_is_transaction_start(struct jset_entry *entry)
46 {
47         return entry->type == BCH_JSET_ENTRY_log && !entry->level;
48 }
49
50 typedef DARRAY(struct bbpos) d_bbpos;
51 typedef DARRAY(enum btree_id) d_btree_id;
52
53 static bool bkey_matches_filter(d_bbpos filter, struct jset_entry *entry, struct bkey_i *k)
54 {
55         struct bbpos *i;
56
57         darray_for_each(filter, i) {
58                 if (i->btree != entry->btree_id)
59                         continue;
60
61                 if (!btree_node_type_is_extents(i->btree)) {
62                         if (bkey_eq(i->pos, k->k.p))
63                                 return true;
64                 } else {
65                         if (bkey_ge(i->pos, bkey_start_pos(&k->k)) &&
66                             bkey_lt(i->pos, k->k.p))
67                                 return true;
68                 }
69         }
70         return false;
71 }
72
73 static bool should_print_transaction(struct jset_entry *entry, struct jset_entry *end,
74                                      d_bbpos filter)
75 {
76         if (!filter.nr)
77                 return true;
78
79         for (entry = vstruct_next(entry);
80              entry != end && !entry_is_transaction_start(entry);
81              entry = vstruct_next(entry)) {
82                 if (entry->type == BCH_JSET_ENTRY_btree_root ||
83                     entry->type == BCH_JSET_ENTRY_btree_keys) {
84                         struct bkey_i *k;
85
86                         vstruct_for_each(entry, k)
87                                 if (bkey_matches_filter(filter, entry, k))
88                                         return true;
89                 }
90         }
91
92         return false;
93 }
94
95 static bool should_print_entry(struct jset_entry *entry, d_btree_id filter)
96 {
97         struct bkey_i *k;
98         enum btree_id *id;
99
100         if (!filter.nr)
101                 return true;
102
103         if (entry->type != BCH_JSET_ENTRY_btree_root &&
104             entry->type != BCH_JSET_ENTRY_btree_keys &&
105             entry->type != BCH_JSET_ENTRY_overwrite)
106                 return true;
107
108         vstruct_for_each(entry, k)
109                 darray_for_each(filter, id)
110                         if (entry->btree_id == *id)
111                                 return true;
112
113         return false;
114 }
115
116 static void journal_entries_print(struct bch_fs *c, unsigned nr_entries,
117                                   d_bbpos transaction_filter,
118                                   d_btree_id key_filter)
119 {
120         struct journal_replay *p, **_p;
121         struct genradix_iter iter;
122         struct printbuf buf = PRINTBUF;
123
124         genradix_for_each(&c->journal_entries, iter, _p) {
125                 p = *_p;
126                 if (!p)
127                         continue;
128
129                 if (le64_to_cpu(p->j.seq) + nr_entries < atomic64_read(&c->journal.seq))
130                         continue;
131
132                 bool blacklisted =
133                         bch2_journal_seq_is_blacklisted(c,
134                                         le64_to_cpu(p->j.seq), false);
135
136                 if (!transaction_filter.nr) {
137                         if (blacklisted)
138                                 printf("blacklisted ");
139
140                         printf("journal entry     %llu\n", le64_to_cpu(p->j.seq));
141
142                         printbuf_reset(&buf);
143
144                         prt_printf(&buf,
145                                    "  version         %u\n"
146                                    "  last seq        %llu\n"
147                                    "  flush           %u\n"
148                                    "  written at      ",
149                                    le32_to_cpu(p->j.version),
150                                    le64_to_cpu(p->j.last_seq),
151                                    !JSET_NO_FLUSH(&p->j));
152                         bch2_journal_ptrs_to_text(&buf, c, p);
153
154                         if (blacklisted)
155                                 star_start_of_lines(buf.buf);
156                         printf("%s\n", buf.buf);
157                         printbuf_reset(&buf);
158                 }
159
160                 struct jset_entry *entry = p->j.start;
161                 struct jset_entry *end = vstruct_last(&p->j);
162                 while (entry != end) {
163
164                         /*
165                          * log entries denote the start of a new transaction
166                          * commit:
167                          */
168                         if (entry_is_transaction_start(entry)) {
169                                 if (!should_print_transaction(entry, end, transaction_filter)) {
170                                         do {
171                                                 entry = vstruct_next(entry);
172                                         } while (entry != end && !entry_is_transaction_start(entry));
173
174                                         continue;
175                                 }
176
177                                 prt_newline(&buf);
178                         }
179
180                         if (should_print_entry(entry, key_filter)) {
181                                 printbuf_indent_add(&buf, 4);
182                                 bch2_journal_entry_to_text(&buf, c, entry);
183
184                                 if (blacklisted)
185                                         star_start_of_lines(buf.buf);
186                                 printf("%s\n", buf.buf);
187                                 printbuf_reset(&buf);
188                         }
189
190                         entry = vstruct_next(entry);
191                 }
192         }
193
194         printbuf_exit(&buf);
195 }
196
197 int cmd_list_journal(int argc, char *argv[])
198 {
199         static const struct option longopts[] = {
200                 { "nr-entries",         required_argument,      NULL, 'n' },
201                 { "transaction-filter", required_argument,      NULL, 't' },
202                 { "key-filter",         required_argument,      NULL, 'k' },
203                 { "verbose",            no_argument,            NULL, 'v' },
204                 { "help",               no_argument,            NULL, 'h' },
205                 { NULL }
206         };
207         struct bch_opts opts = bch2_opts_empty();
208         u32 nr_entries = U32_MAX;
209         d_bbpos         transaction_filter = { 0 };
210         d_btree_id      key_filter = { 0 };
211         int opt;
212
213         opt_set(opts, nochanges,        true);
214         opt_set(opts, norecovery,       true);
215         opt_set(opts, degraded,         true);
216         opt_set(opts, errors,           BCH_ON_ERROR_continue);
217         opt_set(opts, fix_errors,       FSCK_OPT_YES);
218         opt_set(opts, keep_journal,     true);
219         opt_set(opts, read_journal_only,true);
220
221         while ((opt = getopt_long(argc, argv, "an:t:k:vh",
222                                   longopts, NULL)) != -1)
223                 switch (opt) {
224                 case 'a':
225                         opt_set(opts, read_entire_journal, true);
226                         break;
227                 case 'n':
228                         if (kstrtouint(optarg, 10, &nr_entries))
229                                 die("error parsing nr_entries");
230                         opt_set(opts, read_entire_journal, true);
231                         break;
232                 case 't':
233                         darray_push(&transaction_filter, bbpos_parse(optarg));
234                         break;
235                 case 'k':
236                         darray_push(&key_filter, read_string_list_or_die(optarg, bch2_btree_ids, "btree id"));
237                         break;
238                 case 'v':
239                         opt_set(opts, verbose, true);
240                         break;
241                 case 'h':
242                         list_journal_usage();
243                         exit(EXIT_SUCCESS);
244                 }
245         args_shift(optind);
246
247         if (!argc)
248                 die("Please supply device(s) to open");
249
250         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
251         if (IS_ERR(c))
252                 die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
253
254         journal_entries_print(c, nr_entries, transaction_filter, key_filter);
255         bch2_fs_stop(c);
256         return 0;
257 }