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