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