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