7 #include "libbcachefs.h"
9 #include "tools-util.h"
11 #include "libbcachefs/bcachefs.h"
12 #include "libbcachefs/bset.h"
13 #include "libbcachefs/btree_cache.h"
14 #include "libbcachefs/btree_io.h"
15 #include "libbcachefs/btree_iter.h"
16 #include "libbcachefs/buckets.h"
17 #include "libbcachefs/checksum.h"
18 #include "libbcachefs/error.h"
19 #include "libbcachefs/journal.h"
20 #include "libbcachefs/journal_io.h"
21 #include "libbcachefs/journal_seq_blacklist.h"
22 #include "libbcachefs/super.h"
24 static void dump_usage(void)
26 puts("bcachefs dump - dump filesystem metadata\n"
27 "Usage: bcachefs dump [OPTION]... <devices>\n"
30 " -o output Output qcow2 image(s)\n"
31 " -f Force; overwrite when needed\n"
32 " -j Dump entire journal, not just dirty entries\n"
33 " -h Display this help and exit\n"
34 "Report bugs to <linux-bcache@vger.kernel.org>");
37 static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd,
40 struct bch_sb *sb = ca->disk_sb.sb;
48 range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
49 sizeof(struct bch_sb_layout));
51 for (i = 0; i < sb->layout.nr_superblocks; i++)
53 le64_to_cpu(sb->layout.sb_offset[i]) << 9,
57 for (i = 0; i < ca->journal.nr; i++)
59 ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
60 u64 bucket = ca->journal.buckets[i];
63 bucket_bytes(ca) * bucket,
68 for (i = 0; i < BTREE_ID_NR; i++) {
69 const struct bch_extent_ptr *ptr;
70 struct bkey_ptrs_c ptrs;
71 struct btree_trans trans;
72 struct btree_iter iter;
75 bch2_trans_init(&trans, c, 0, 0);
77 __for_each_btree_node(&trans, iter, i, POS_MIN, 0, 1, 0, b, ret) {
78 struct btree_node_iter iter;
82 for_each_btree_node_key_unpack(b, k, &iter, &u) {
83 ptrs = bch2_bkey_ptrs_c(k);
85 bkey_for_each_ptr(ptrs, ptr)
86 if (ptr->dev == ca->dev_idx)
94 die("error %s walking btree nodes", strerror(-ret));
96 b = c->btree_roots[i].b;
97 if (!btree_node_fake(b)) {
98 ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
100 bkey_for_each_ptr(ptrs, ptr)
101 if (ptr->dev == ca->dev_idx)
107 bch2_trans_iter_exit(&trans, &iter);
108 bch2_trans_exit(&trans);
111 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
112 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
116 int cmd_dump(int argc, char *argv[])
118 struct bch_opts opts = bch2_opts_empty();
121 unsigned i, nr_devices = 0;
122 bool force = false, entire_journal = false;
125 opt_set(opts, nochanges, true);
126 opt_set(opts, norecovery, true);
127 opt_set(opts, degraded, true);
128 opt_set(opts, errors, BCH_ON_ERROR_continue);
129 opt_set(opts, fix_errors, FSCK_OPT_NO);
131 while ((opt = getopt(argc, argv, "o:fjvh")) != -1)
140 entire_journal = true;
143 opt_set(opts, verbose, true);
152 die("Please supply output filename");
155 die("Please supply device(s) to check");
157 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
159 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
161 down_read(&c->gc_lock);
163 for_each_online_member(ca, c, i)
168 for_each_online_member(ca, c, i) {
169 int flags = O_WRONLY|O_CREAT|O_TRUNC;
177 char *path = nr_devices > 1
178 ? mprintf("%s.%u", out, i)
180 fd = xopen(path, flags, 0600);
183 dump_one_device(c, ca, fd, entire_journal);
187 up_read(&c->gc_lock);
193 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
194 struct bpos start, struct bpos end)
196 struct btree_trans trans;
197 struct btree_iter iter;
199 struct printbuf buf = PRINTBUF;
202 bch2_trans_init(&trans, c, 0, 0);
204 for_each_btree_key(&trans, iter, btree_id, start,
205 BTREE_ITER_ALL_SNAPSHOTS|
206 BTREE_ITER_PREFETCH, k, ret) {
207 if (bkey_cmp(k.k->p, end) > 0)
210 printbuf_reset(&buf);
211 bch2_bkey_val_to_text(&buf, c, k);
214 bch2_trans_iter_exit(&trans, &iter);
216 bch2_trans_exit(&trans);
221 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id, unsigned level,
222 struct bpos start, struct bpos end)
224 struct btree_trans trans;
225 struct btree_iter iter;
227 struct printbuf buf = PRINTBUF;
230 bch2_trans_init(&trans, c, 0, 0);
232 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
233 if (bkey_cmp(b->key.k.p, end) > 0)
236 printbuf_reset(&buf);
237 bch2_btree_node_to_text(&buf, c, b);
240 bch2_trans_iter_exit(&trans, &iter);
243 die("error %s walking btree nodes", strerror(-ret));
245 bch2_trans_exit(&trans);
249 static void list_nodes(struct bch_fs *c, enum btree_id btree_id, unsigned level,
250 struct bpos start, struct bpos end)
252 struct btree_trans trans;
253 struct btree_iter iter;
255 struct printbuf buf = PRINTBUF;
258 bch2_trans_init(&trans, c, 0, 0);
260 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
261 if (bkey_cmp(b->key.k.p, end) > 0)
264 printbuf_reset(&buf);
265 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
266 fputs(buf.buf, stdout);
269 bch2_trans_iter_exit(&trans, &iter);
272 die("error %s walking btree nodes", strerror(-ret));
274 bch2_trans_exit(&trans);
278 static void print_node_ondisk(struct bch_fs *c, struct btree *b)
280 struct btree_node *n_ondisk;
281 struct extent_ptr_decoded pick;
286 if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key), NULL, &pick) <= 0) {
287 printf("error getting device to read from\n");
291 ca = bch_dev_bkey_exists(c, pick.ptr.dev);
292 if (!bch2_dev_get_ioref(ca, READ)) {
293 printf("error getting device to read from\n");
297 n_ondisk = malloc(btree_bytes(c));
299 bio = bio_alloc_bioset(GFP_NOIO,
300 buf_pages(n_ondisk, btree_bytes(c)),
302 bio_set_dev(bio, ca->disk_sb.bdev);
303 bio->bi_opf = REQ_OP_READ|REQ_META;
304 bio->bi_iter.bi_sector = pick.ptr.offset;
305 bch2_bio_map(bio, n_ondisk, btree_bytes(c));
307 submit_bio_wait(bio);
310 percpu_ref_put(&ca->io_ref);
312 while (offset < btree_sectors(c)) {
315 struct bch_csum csum;
316 struct bkey_packed *k;
322 if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)))
323 die("unknown checksum type");
325 nonce = btree_nonce(i, offset << 9);
326 csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, n_ondisk);
328 if (bch2_crc_cmp(csum, n_ondisk->csum))
329 die("invalid checksum\n");
331 bset_encrypt(c, i, offset << 9);
333 sectors = vstruct_sectors(n_ondisk, c->block_bits);
335 struct btree_node_entry *bne = (void *) n_ondisk + (offset << 9);
339 if (i->seq != n_ondisk->keys.seq)
342 if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)))
343 die("unknown checksum type");
345 nonce = btree_nonce(i, offset << 9);
346 csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, bne);
348 if (bch2_crc_cmp(csum, bne->csum))
349 die("invalid checksum");
351 bset_encrypt(c, i, offset << 9);
353 sectors = vstruct_sectors(bne, c->block_bits);
356 fprintf(stdout, " offset %u version %u, journal seq %llu\n",
358 le16_to_cpu(i->version),
359 le64_to_cpu(i->journal_seq));
362 for (k = i->start; k != vstruct_last(i); k = bkey_next(k)) {
364 struct printbuf buf = PRINTBUF;
366 bch2_bkey_val_to_text(&buf, c, bkey_disassemble(b, k, &u));
367 fprintf(stdout, " %s\n", buf.buf);
376 static void list_nodes_ondisk(struct bch_fs *c, enum btree_id btree_id, unsigned level,
377 struct bpos start, struct bpos end)
379 struct btree_trans trans;
380 struct btree_iter iter;
382 struct printbuf buf = PRINTBUF;
385 bch2_trans_init(&trans, c, 0, 0);
387 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
388 if (bkey_cmp(b->key.k.p, end) > 0)
391 printbuf_reset(&buf);
392 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
393 fputs(buf.buf, stdout);
396 print_node_ondisk(c, b);
398 bch2_trans_iter_exit(&trans, &iter);
401 die("error %s walking btree nodes", strerror(-ret));
403 bch2_trans_exit(&trans);
407 static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id, unsigned level,
408 struct bpos start, struct bpos end)
410 struct btree_trans trans;
411 struct btree_iter iter;
412 struct btree_node_iter node_iter;
413 struct bkey unpacked;
416 struct printbuf buf = PRINTBUF;
419 bch2_trans_init(&trans, c, 0, 0);
421 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
422 if (bkey_cmp(b->key.k.p, end) > 0)
425 printbuf_reset(&buf);
426 bch2_btree_node_to_text(&buf, c, b);
427 fputs(buf.buf, stdout);
429 for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) {
430 printbuf_reset(&buf);
431 bch2_bkey_val_to_text(&buf, c, k);
436 bch2_trans_iter_exit(&trans, &iter);
439 die("error %s walking btree nodes", strerror(-ret));
441 bch2_trans_exit(&trans);
445 static void list_keys_usage(void)
447 puts("bcachefs list - list filesystem metadata to stdout\n"
448 "Usage: bcachefs list [OPTION]... <devices>\n"
451 " -b (extents|inodes|dirents|xattrs) Btree to list from\n"
452 " -l level Btree depth to descend to (0 == leaves)\n"
453 " -s inode:offset Start position to list from\n"
454 " -e inode:offset End position\n"
455 " -i inode List keys for a given inode number\n"
456 " -m (keys|formats|nodes|nodes_ondisk|nodes_keys)\n"
458 " -f Check (fsck) the filesystem first\n"
460 " -h Display this help and exit\n"
461 "Report bugs to <linux-bcache@vger.kernel.org>");
464 #define LIST_MODES() \
472 #define x(n) LIST_MODE_##n,
477 static const char * const list_modes[] = {
484 int cmd_list(int argc, char *argv[])
486 struct bch_opts opts = bch2_opts_empty();
487 enum btree_id btree_id_start = 0;
488 enum btree_id btree_id_end = BTREE_ID_NR;
489 enum btree_id btree_id;
491 struct bpos start = POS_MIN, end = POS_MAX;
495 opt_set(opts, nochanges, true);
496 opt_set(opts, norecovery, true);
497 opt_set(opts, degraded, true);
498 opt_set(opts, errors, BCH_ON_ERROR_continue);
500 while ((opt = getopt(argc, argv, "b:l:s:e:i:m:fvh")) != -1)
503 btree_id_start = read_string_list_or_die(optarg,
504 bch2_btree_ids, "btree id");
505 btree_id_end = btree_id_start + 1;
508 if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
509 die("invalid level");
512 start = bpos_parse(optarg);
515 end = bpos_parse(optarg);
518 if (kstrtoull(optarg, 10, &inum))
519 die("invalid inode %s", optarg);
520 start = POS(inum, 0);
521 end = POS(inum + 1, 0);
524 mode = read_string_list_or_die(optarg,
525 list_modes, "list mode");
528 opt_set(opts, fix_errors, FSCK_OPT_YES);
529 opt_set(opts, norecovery, false);
532 opt_set(opts, verbose, true);
541 die("Please supply device(s)");
543 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
545 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
548 for (btree_id = btree_id_start;
549 btree_id < btree_id_end;
553 list_keys(c, btree_id, start, end);
555 case LIST_MODE_formats:
556 list_btree_formats(c, btree_id, level, start, end);
558 case LIST_MODE_nodes:
559 list_nodes(c, btree_id, level, start, end);
561 case LIST_MODE_nodes_ondisk:
562 list_nodes_ondisk(c, btree_id, level, start, end);
564 case LIST_MODE_nodes_keys:
565 list_nodes_keys(c, btree_id, level, start, end);
576 static void list_journal_usage(void)
578 puts("bcachefs list_journal - print contents of journal\n"
579 "Usage: bcachefs list_journal [OPTION]... <devices>\n"
582 " -a Read entire journal, not just dirty entries\n"
583 " -h Display this help and exit\n"
584 "Report bugs to <linux-bcache@vger.kernel.org>");
587 static void star_start_of_lines(char *buf)
594 while ((p = strstr(p, "\n ")))
598 int cmd_list_journal(int argc, char *argv[])
600 struct bch_opts opts = bch2_opts_empty();
603 opt_set(opts, nochanges, true);
604 opt_set(opts, norecovery, true);
605 opt_set(opts, degraded, true);
606 opt_set(opts, errors, BCH_ON_ERROR_continue);
607 opt_set(opts, fix_errors, FSCK_OPT_YES);
608 opt_set(opts, keep_journal, true);
609 opt_set(opts, read_journal_only,true);
611 while ((opt = getopt(argc, argv, "ah")) != -1)
614 opt_set(opts, read_entire_journal, true);
617 list_journal_usage();
623 die("Please supply device(s) to open");
625 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
627 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
629 struct journal_replay *p;
630 struct jset_entry *entry;
631 struct printbuf buf = PRINTBUF;
633 list_for_each_entry(p, &c->journal_entries, list) {
635 bch2_journal_seq_is_blacklisted(c,
636 le64_to_cpu(p->j.seq), false);
640 printf("blacklisted ");
642 printf("journal entry %llu\n", le64_to_cpu(p->j.seq));
644 printbuf_reset(&buf);
651 le32_to_cpu(p->j.version),
652 le64_to_cpu(p->j.last_seq),
653 !JSET_NO_FLUSH(&p->j));
654 bch2_journal_ptrs_to_text(&buf, c, p);
657 star_start_of_lines(buf.buf);
658 printf("%s\n", buf.buf);
660 vstruct_for_each(&p->j, entry) {
661 printbuf_reset(&buf);
664 * log entries denote the start of a new transaction
667 if (entry->type == BCH_JSET_ENTRY_log)
669 pr_indent_push(&buf, 4);
670 bch2_journal_entry_to_text(&buf, c, entry);
673 star_start_of_lines(buf.buf);
674 printf("%s\n", buf.buf);