]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_list_journal.c
Add packaging workflow
[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         struct bbpos *i;
59
60         darray_for_each(filter, i) {
61                 if (i->btree != entry->btree_id)
62                         continue;
63
64                 if (!btree_node_type_is_extents(i->btree)) {
65                         if (bkey_eq(i->pos, k->k.p))
66                                 return true;
67                 } else {
68                         if (bkey_ge(i->pos, bkey_start_pos(&k->k)) &&
69                             bkey_lt(i->pos, k->k.p))
70                                 return true;
71                 }
72         }
73         return false;
74 }
75
76 static bool entry_matches_transaction_filter(struct jset_entry *entry,
77                                              d_bbpos filter)
78 {
79         if (entry->type == BCH_JSET_ENTRY_btree_root ||
80             entry->type == BCH_JSET_ENTRY_btree_keys ||
81             entry->type == BCH_JSET_ENTRY_overwrite) {
82                 struct bkey_i *k;
83
84                 jset_entry_for_each_key(entry, k)
85                         if (bkey_matches_filter(filter, entry, k))
86                                 return true;
87         }
88
89         return false;
90 }
91
92 static bool should_print_transaction(struct jset_entry *entry, struct jset_entry *end,
93                                      d_bbpos filter)
94 {
95         if (!filter.nr)
96                 return true;
97
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))
102                         return true;
103
104         return false;
105 }
106
107 static bool should_print_entry(struct jset_entry *entry, d_btree_id filter)
108 {
109         struct bkey_i *k;
110         enum btree_id *id;
111
112         if (!filter.nr)
113                 return true;
114
115         if (entry->type != BCH_JSET_ENTRY_btree_root &&
116             entry->type != BCH_JSET_ENTRY_btree_keys &&
117             entry->type != BCH_JSET_ENTRY_overwrite)
118                 return true;
119
120         jset_entry_for_each_key(entry, k)
121                 darray_for_each(filter, id)
122                         if (entry->btree_id == *id)
123                                 return true;
124
125         return false;
126 }
127
128 static void journal_entries_print(struct bch_fs *c, unsigned nr_entries,
129                                   d_bbpos transaction_filter,
130                                   d_btree_id key_filter)
131 {
132         struct journal_replay *p, **_p;
133         struct genradix_iter iter;
134         struct printbuf buf = PRINTBUF;
135
136         genradix_for_each(&c->journal_entries, iter, _p) {
137                 p = *_p;
138                 if (!p)
139                         continue;
140
141                 if (le64_to_cpu(p->j.seq) + nr_entries < atomic64_read(&c->journal.seq))
142                         continue;
143
144                 bool blacklisted =
145                         bch2_journal_seq_is_blacklisted(c,
146                                         le64_to_cpu(p->j.seq), false);
147
148                 if (!transaction_filter.nr) {
149                         if (blacklisted)
150                                 printf("blacklisted ");
151
152                         printf("journal entry     %llu\n", le64_to_cpu(p->j.seq));
153
154                         printbuf_reset(&buf);
155
156                         prt_printf(&buf,
157                                    "  version         %u\n"
158                                    "  last seq        %llu\n"
159                                    "  flush           %u\n"
160                                    "  written at      ",
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);
165
166                         if (blacklisted)
167                                 star_start_of_lines(buf.buf);
168                         printf("%s\n", buf.buf);
169                         printbuf_reset(&buf);
170                 }
171
172                 struct jset_entry *entry = p->j.start;
173                 struct jset_entry *end = vstruct_last(&p->j);
174                 while (entry != end) {
175
176                         /*
177                          * log entries denote the start of a new transaction
178                          * commit:
179                          */
180                         if (entry_is_transaction_start(entry)) {
181                                 if (!should_print_transaction(entry, end, transaction_filter)) {
182                                         do {
183                                                 entry = vstruct_next(entry);
184                                         } while (entry != end && !entry_is_transaction_start(entry));
185
186                                         continue;
187                                 }
188
189                                 prt_newline(&buf);
190                         }
191
192                         if (!should_print_entry(entry, key_filter))
193                                 goto next;
194
195                         bool highlight = entry_matches_transaction_filter(entry, transaction_filter);
196                         if (highlight)
197                                 fputs(RED, stdout);
198
199                         printbuf_indent_add(&buf, 4);
200                         bch2_journal_entry_to_text(&buf, c, entry);
201
202                         if (blacklisted)
203                                 star_start_of_lines(buf.buf);
204                         printf("%s\n", buf.buf);
205                         printbuf_reset(&buf);
206
207                         if (highlight)
208                                 fputs(NORMAL, stdout);
209 next:
210                         entry = vstruct_next(entry);
211                 }
212         }
213
214         printbuf_exit(&buf);
215 }
216
217 int cmd_list_journal(int argc, char *argv[])
218 {
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' },
225                 { NULL }
226         };
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 };
231         int opt;
232
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);
240
241         while ((opt = getopt_long(argc, argv, "an:t:k:vh",
242                                   longopts, NULL)) != -1)
243                 switch (opt) {
244                 case 'a':
245                         opt_set(opts, read_entire_journal, true);
246                         break;
247                 case 'n':
248                         if (kstrtouint(optarg, 10, &nr_entries))
249                                 die("error parsing nr_entries");
250                         opt_set(opts, read_entire_journal, true);
251                         break;
252                 case 't':
253                         darray_push(&transaction_filter, bbpos_parse(optarg));
254                         break;
255                 case 'k':
256                         darray_push(&key_filter, read_string_list_or_die(optarg, bch2_btree_ids, "btree id"));
257                         break;
258                 case 'v':
259                         opt_set(opts, verbose, true);
260                         break;
261                 case 'h':
262                         list_journal_usage();
263                         exit(EXIT_SUCCESS);
264                 }
265         args_shift(optind);
266
267         if (!argc)
268                 die("Please supply device(s) to open");
269
270         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
271         if (IS_ERR(c))
272                 die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
273
274         journal_entries_print(c, nr_entries, transaction_filter, key_filter);
275         bch2_fs_stop(c);
276         return 0;
277 }