forked from koverstreet/bcachefs-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_list.c
408 lines (335 loc) · 9.71 KB
/
cmd_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "cmds.h"
#include "libbcachefs.h"
#include "qcow2.h"
#include "tools-util.h"
#include "libbcachefs/bcachefs.h"
#include "libbcachefs/btree_cache.h"
#include "libbcachefs/btree_io.h"
#include "libbcachefs/btree_iter.h"
#include "libbcachefs/checksum.h"
#include "libbcachefs/error.h"
#include "libbcachefs/extents.h"
#include "libbcachefs/super.h"
static void list_keys(struct bch_fs *c, enum btree_id btree_id,
struct bpos start, struct bpos end)
{
struct btree_trans trans;
struct btree_iter iter;
struct bkey_s_c k;
struct printbuf buf = PRINTBUF;
int ret;
bch2_trans_init(&trans, c, 0, 0);
for_each_btree_key(&trans, iter, btree_id, start,
BTREE_ITER_ALL_SNAPSHOTS|
BTREE_ITER_PREFETCH, k, ret) {
if (bkey_cmp(k.k->p, end) > 0)
break;
printbuf_reset(&buf);
bch2_bkey_val_to_text(&buf, c, k);
puts(buf.buf);
}
bch2_trans_iter_exit(&trans, &iter);
bch2_trans_exit(&trans);
printbuf_exit(&buf);
}
static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id, unsigned level,
struct bpos start, struct bpos end)
{
struct btree_trans trans;
struct btree_iter iter;
struct btree *b;
struct printbuf buf = PRINTBUF;
int ret;
bch2_trans_init(&trans, c, 0, 0);
__for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
if (bkey_cmp(b->key.k.p, end) > 0)
break;
printbuf_reset(&buf);
bch2_btree_node_to_text(&buf, c, b);
puts(buf.buf);
}
bch2_trans_iter_exit(&trans, &iter);
if (ret)
die("error %s walking btree nodes", strerror(-ret));
bch2_trans_exit(&trans);
printbuf_exit(&buf);
}
static void list_nodes(struct bch_fs *c, enum btree_id btree_id, unsigned level,
struct bpos start, struct bpos end)
{
struct btree_trans trans;
struct btree_iter iter;
struct btree *b;
struct printbuf buf = PRINTBUF;
int ret;
bch2_trans_init(&trans, c, 0, 0);
__for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
if (bkey_cmp(b->key.k.p, end) > 0)
break;
printbuf_reset(&buf);
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
fputs(buf.buf, stdout);
putchar('\n');
}
bch2_trans_iter_exit(&trans, &iter);
if (ret)
die("error %s walking btree nodes", strerror(-ret));
bch2_trans_exit(&trans);
printbuf_exit(&buf);
}
static void print_node_ondisk(struct bch_fs *c, struct btree *b)
{
struct btree_node *n_ondisk;
struct extent_ptr_decoded pick;
struct bch_dev *ca;
struct bio *bio;
unsigned offset = 0;
int ret;
if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key), NULL, &pick) <= 0) {
printf("error getting device to read from\n");
return;
}
ca = bch_dev_bkey_exists(c, pick.ptr.dev);
if (!bch2_dev_get_ioref(ca, READ)) {
printf("error getting device to read from\n");
return;
}
n_ondisk = aligned_alloc(block_bytes(c), btree_bytes(c));
bio = bio_alloc_bioset(ca->disk_sb.bdev,
buf_pages(n_ondisk, btree_bytes(c)),
REQ_OP_READ|REQ_META,
GFP_NOIO,
&c->btree_bio);
bio->bi_iter.bi_sector = pick.ptr.offset;
bch2_bio_map(bio, n_ondisk, btree_bytes(c));
ret = submit_bio_wait(bio);
if (ret)
die("error reading btree node: %i", ret);
bio_put(bio);
percpu_ref_put(&ca->io_ref);
while (offset < btree_sectors(c)) {
struct bset *i;
struct nonce nonce;
struct bch_csum csum;
struct bkey_packed *k;
unsigned sectors;
if (!offset) {
i = &n_ondisk->keys;
if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)))
die("unknown checksum type at offset %u: %llu",
offset, BSET_CSUM_TYPE(i));
nonce = btree_nonce(i, offset << 9);
csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, n_ondisk);
if (bch2_crc_cmp(csum, n_ondisk->csum))
die("invalid checksum\n");
bset_encrypt(c, i, offset << 9);
sectors = vstruct_sectors(n_ondisk, c->block_bits);
} else {
struct btree_node_entry *bne = (void *) n_ondisk + (offset << 9);
i = &bne->keys;
if (i->seq != n_ondisk->keys.seq)
break;
if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)))
die("unknown checksum type at offset %u: %llu",
offset, BSET_CSUM_TYPE(i));
nonce = btree_nonce(i, offset << 9);
csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, bne);
if (bch2_crc_cmp(csum, bne->csum))
die("invalid checksum");
bset_encrypt(c, i, offset << 9);
sectors = vstruct_sectors(bne, c->block_bits);
}
fprintf(stdout, " offset %u version %u, journal seq %llu\n",
offset,
le16_to_cpu(i->version),
le64_to_cpu(i->journal_seq));
offset += sectors;
for (k = i->start; k != vstruct_last(i); k = bkey_next(k)) {
struct bkey u;
struct printbuf buf = PRINTBUF;
printbuf_indent_add(&buf, 4);
bch2_bkey_val_to_text(&buf, c, bkey_disassemble(b, k, &u));
fprintf(stdout, "%s\n", buf.buf);
printbuf_exit(&buf);
}
}
free(n_ondisk);
}
static void list_nodes_ondisk(struct bch_fs *c, enum btree_id btree_id, unsigned level,
struct bpos start, struct bpos end)
{
struct btree_trans trans;
struct btree_iter iter;
struct btree *b;
struct printbuf buf = PRINTBUF;
int ret;
bch2_trans_init(&trans, c, 0, 0);
__for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
if (bkey_cmp(b->key.k.p, end) > 0)
break;
printbuf_reset(&buf);
bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
fputs(buf.buf, stdout);
putchar('\n');
print_node_ondisk(c, b);
}
bch2_trans_iter_exit(&trans, &iter);
if (ret)
die("error %s walking btree nodes", strerror(-ret));
bch2_trans_exit(&trans);
printbuf_exit(&buf);
}
static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id, unsigned level,
struct bpos start, struct bpos end)
{
struct btree_trans trans;
struct btree_iter iter;
struct btree_node_iter node_iter;
struct bkey unpacked;
struct bkey_s_c k;
struct btree *b;
struct printbuf buf = PRINTBUF;
int ret;
bch2_trans_init(&trans, c, 0, 0);
__for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
if (bkey_cmp(b->key.k.p, end) > 0)
break;
printbuf_reset(&buf);
bch2_btree_node_to_text(&buf, c, b);
fputs(buf.buf, stdout);
for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) {
printbuf_reset(&buf);
bch2_bkey_val_to_text(&buf, c, k);
putchar('\t');
puts(buf.buf);
}
}
bch2_trans_iter_exit(&trans, &iter);
if (ret)
die("error %s walking btree nodes", strerror(-ret));
bch2_trans_exit(&trans);
printbuf_exit(&buf);
}
static void list_keys_usage(void)
{
puts("bcachefs list - list filesystem metadata to stdout\n"
"Usage: bcachefs list [OPTION]... <devices>\n"
"\n"
"Options:\n"
" -b (extents|inodes|dirents|xattrs) Btree to list from\n"
" -l level Btree depth to descend to (0 == leaves)\n"
" -s inode:offset Start position to list from\n"
" -e inode:offset End position\n"
" -i inode List keys for a given inode number\n"
" -m (keys|formats|nodes|nodes_ondisk|nodes_keys)\n"
" List mode\n"
" -f Check (fsck) the filesystem first\n"
" -v Verbose mode\n"
" -h Display this help and exit\n"
"Report bugs to <[email protected]>");
}
#define LIST_MODES() \
x(keys) \
x(formats) \
x(nodes) \
x(nodes_ondisk) \
x(nodes_keys)
enum list_modes {
#define x(n) LIST_MODE_##n,
LIST_MODES()
#undef x
};
static const char * const list_modes[] = {
#define x(n) #n,
LIST_MODES()
#undef x
NULL
};
int cmd_list(int argc, char *argv[])
{
struct bch_opts opts = bch2_opts_empty();
enum btree_id btree_id_start = 0;
enum btree_id btree_id_end = BTREE_ID_NR;
enum btree_id btree_id;
unsigned level = 0;
struct bpos start = POS_MIN, end = POS_MAX;
u64 inum = 0;
int mode = 0, opt;
opt_set(opts, nochanges, true);
opt_set(opts, norecovery, true);
opt_set(opts, degraded, true);
opt_set(opts, errors, BCH_ON_ERROR_continue);
while ((opt = getopt(argc, argv, "b:l:s:e:i:m:fvh")) != -1)
switch (opt) {
case 'b':
btree_id_start = read_string_list_or_die(optarg,
bch2_btree_ids, "btree id");
btree_id_end = btree_id_start + 1;
break;
case 'l':
if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
die("invalid level");
break;
case 's':
start = bpos_parse(optarg);
break;
case 'e':
end = bpos_parse(optarg);
break;
case 'i':
if (kstrtoull(optarg, 10, &inum))
die("invalid inode %s", optarg);
start = POS(inum, 0);
end = POS(inum + 1, 0);
break;
case 'm':
mode = read_string_list_or_die(optarg,
list_modes, "list mode");
break;
case 'f':
opt_set(opts, fix_errors, FSCK_OPT_YES);
opt_set(opts, norecovery, false);
break;
case 'v':
opt_set(opts, verbose, true);
break;
case 'h':
list_keys_usage();
exit(EXIT_SUCCESS);
}
args_shift(optind);
if (!argc)
die("Please supply device(s)");
struct bch_fs *c = bch2_fs_open(argv, argc, opts);
if (IS_ERR(c))
die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
for (btree_id = btree_id_start;
btree_id < btree_id_end;
btree_id++) {
switch (mode) {
case LIST_MODE_keys:
list_keys(c, btree_id, start, end);
break;
case LIST_MODE_formats:
list_btree_formats(c, btree_id, level, start, end);
break;
case LIST_MODE_nodes:
list_nodes(c, btree_id, level, start, end);
break;
case LIST_MODE_nodes_ondisk:
list_nodes_ondisk(c, btree_id, level, start, end);
break;
case LIST_MODE_nodes_keys:
list_nodes_keys(c, btree_id, level, start, end);
break;
default:
die("Invalid mode");
}
}
bch2_fs_stop(c);
return 0;
}