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;
46 range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
47 sizeof(struct bch_sb_layout));
49 for (i = 0; i < sb->layout.nr_superblocks; i++)
51 le64_to_cpu(sb->layout.sb_offset[i]) << 9,
55 for (i = 0; i < ca->journal.nr; i++)
57 ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
58 u64 bucket = ca->journal.buckets[i];
61 bucket_bytes(ca) * bucket,
66 for (i = 0; i < BTREE_ID_NR; i++) {
67 const struct bch_extent_ptr *ptr;
68 struct bkey_ptrs_c ptrs;
69 struct btree_trans trans;
70 struct btree_iter iter;
73 bch2_trans_init(&trans, c, 0, 0);
75 __for_each_btree_node(&trans, iter, i, POS_MIN, 0, 1, 0, b, ret) {
76 struct btree_node_iter iter;
80 for_each_btree_node_key_unpack(b, k, &iter, &u) {
81 ptrs = bch2_bkey_ptrs_c(k);
83 bkey_for_each_ptr(ptrs, ptr)
84 if (ptr->dev == ca->dev_idx)
92 die("error %s walking btree nodes", strerror(-ret));
94 b = c->btree_roots[i].b;
95 if (!btree_node_fake(b)) {
96 ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
98 bkey_for_each_ptr(ptrs, ptr)
99 if (ptr->dev == ca->dev_idx)
105 bch2_trans_iter_exit(&trans, &iter);
106 bch2_trans_exit(&trans);
109 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
110 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
114 int cmd_dump(int argc, char *argv[])
116 struct bch_opts opts = bch2_opts_empty();
119 unsigned i, nr_devices = 0;
120 bool force = false, entire_journal = false;
123 opt_set(opts, nochanges, true);
124 opt_set(opts, norecovery, true);
125 opt_set(opts, degraded, true);
126 opt_set(opts, errors, BCH_ON_ERROR_continue);
127 opt_set(opts, fix_errors, FSCK_OPT_NO);
129 while ((opt = getopt(argc, argv, "o:fjvh")) != -1)
138 entire_journal = true;
141 opt_set(opts, verbose, true);
150 die("Please supply output filename");
153 die("Please supply device(s) to check");
155 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
157 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
159 down_read(&c->gc_lock);
161 for_each_online_member(ca, c, i)
166 for_each_online_member(ca, c, i) {
167 int flags = O_WRONLY|O_CREAT|O_TRUNC;
175 char *path = nr_devices > 1
176 ? mprintf("%s.%u", out, i)
178 fd = xopen(path, flags, 0600);
181 dump_one_device(c, ca, fd, entire_journal);
185 up_read(&c->gc_lock);
191 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
192 struct bpos start, struct bpos end)
194 struct btree_trans trans;
195 struct btree_iter iter;
197 struct printbuf buf = PRINTBUF;
200 bch2_trans_init(&trans, c, 0, 0);
202 for_each_btree_key(&trans, iter, btree_id, start,
203 BTREE_ITER_ALL_SNAPSHOTS|
204 BTREE_ITER_PREFETCH, k, ret) {
205 if (bkey_cmp(k.k->p, end) > 0)
208 printbuf_reset(&buf);
209 bch2_bkey_val_to_text(&buf, c, k);
212 bch2_trans_iter_exit(&trans, &iter);
214 bch2_trans_exit(&trans);
219 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id, unsigned level,
220 struct bpos start, struct bpos end)
222 struct btree_trans trans;
223 struct btree_iter iter;
225 struct printbuf buf = PRINTBUF;
228 bch2_trans_init(&trans, c, 0, 0);
230 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
231 if (bkey_cmp(b->key.k.p, end) > 0)
234 printbuf_reset(&buf);
235 bch2_btree_node_to_text(&buf, c, b);
238 bch2_trans_iter_exit(&trans, &iter);
241 die("error %s walking btree nodes", strerror(-ret));
243 bch2_trans_exit(&trans);
247 static void list_nodes(struct bch_fs *c, enum btree_id btree_id, unsigned level,
248 struct bpos start, struct bpos end)
250 struct btree_trans trans;
251 struct btree_iter iter;
253 struct printbuf buf = PRINTBUF;
256 bch2_trans_init(&trans, c, 0, 0);
258 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
259 if (bkey_cmp(b->key.k.p, end) > 0)
262 printbuf_reset(&buf);
263 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
264 fputs(buf.buf, stdout);
267 bch2_trans_iter_exit(&trans, &iter);
270 die("error %s walking btree nodes", strerror(-ret));
272 bch2_trans_exit(&trans);
276 static void print_node_ondisk(struct bch_fs *c, struct btree *b)
278 struct btree_node *n_ondisk;
279 struct extent_ptr_decoded pick;
284 if (bch2_bkey_pick_read_device(c, bkey_i_to_s_c(&b->key), NULL, &pick) <= 0) {
285 printf("error getting device to read from\n");
289 ca = bch_dev_bkey_exists(c, pick.ptr.dev);
290 if (!bch2_dev_get_ioref(ca, READ)) {
291 printf("error getting device to read from\n");
295 n_ondisk = malloc(btree_bytes(c));
297 bio = bio_alloc_bioset(GFP_NOIO,
298 buf_pages(n_ondisk, btree_bytes(c)),
300 bio_set_dev(bio, ca->disk_sb.bdev);
301 bio->bi_opf = REQ_OP_READ|REQ_META;
302 bio->bi_iter.bi_sector = pick.ptr.offset;
303 bch2_bio_map(bio, n_ondisk, btree_bytes(c));
305 submit_bio_wait(bio);
308 percpu_ref_put(&ca->io_ref);
310 while (offset < btree_sectors(c)) {
313 struct bch_csum csum;
314 struct bkey_packed *k;
320 if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)))
321 die("unknown checksum type");
323 nonce = btree_nonce(i, offset << 9);
324 csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, n_ondisk);
326 if (bch2_crc_cmp(csum, n_ondisk->csum))
327 die("invalid checksum\n");
329 bset_encrypt(c, i, offset << 9);
331 sectors = vstruct_sectors(n_ondisk, c->block_bits);
333 struct btree_node_entry *bne = (void *) n_ondisk + (offset << 9);
337 if (i->seq != n_ondisk->keys.seq)
340 if (!bch2_checksum_type_valid(c, BSET_CSUM_TYPE(i)))
341 die("unknown checksum type");
343 nonce = btree_nonce(i, offset << 9);
344 csum = csum_vstruct(c, BSET_CSUM_TYPE(i), nonce, bne);
346 if (bch2_crc_cmp(csum, bne->csum))
347 die("invalid checksum");
349 bset_encrypt(c, i, offset << 9);
351 sectors = vstruct_sectors(bne, c->block_bits);
354 fprintf(stdout, " offset %u version %u, journal seq %llu\n",
356 le16_to_cpu(i->version),
357 le64_to_cpu(i->journal_seq));
360 for (k = i->start; k != vstruct_last(i); k = bkey_next(k)) {
362 struct printbuf buf = PRINTBUF;
364 bch2_bkey_val_to_text(&buf, c, bkey_disassemble(b, k, &u));
365 fprintf(stdout, " %s\n", buf.buf);
374 static void list_nodes_ondisk(struct bch_fs *c, enum btree_id btree_id, unsigned level,
375 struct bpos start, struct bpos end)
377 struct btree_trans trans;
378 struct btree_iter iter;
380 struct printbuf buf = PRINTBUF;
383 bch2_trans_init(&trans, c, 0, 0);
385 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
386 if (bkey_cmp(b->key.k.p, end) > 0)
389 printbuf_reset(&buf);
390 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
391 fputs(buf.buf, stdout);
394 print_node_ondisk(c, b);
396 bch2_trans_iter_exit(&trans, &iter);
399 die("error %s walking btree nodes", strerror(-ret));
401 bch2_trans_exit(&trans);
405 static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id, unsigned level,
406 struct bpos start, struct bpos end)
408 struct btree_trans trans;
409 struct btree_iter iter;
410 struct btree_node_iter node_iter;
411 struct bkey unpacked;
414 struct printbuf buf = PRINTBUF;
417 bch2_trans_init(&trans, c, 0, 0);
419 __for_each_btree_node(&trans, iter, btree_id, start, 0, level, 0, b, ret) {
420 if (bkey_cmp(b->key.k.p, end) > 0)
423 printbuf_reset(&buf);
424 bch2_btree_node_to_text(&buf, c, b);
425 fputs(buf.buf, stdout);
427 for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) {
428 printbuf_reset(&buf);
429 bch2_bkey_val_to_text(&buf, c, k);
434 bch2_trans_iter_exit(&trans, &iter);
437 die("error %s walking btree nodes", strerror(-ret));
439 bch2_trans_exit(&trans);
443 static void list_keys_usage(void)
445 puts("bcachefs list - list filesystem metadata to stdout\n"
446 "Usage: bcachefs list [OPTION]... <devices>\n"
449 " -b (extents|inodes|dirents|xattrs) Btree to list from\n"
450 " -l level Btree depth to descend to (0 == leaves)\n"
451 " -s inode:offset Start position to list from\n"
452 " -e inode:offset End position\n"
453 " -i inode List keys for a given inode number\n"
454 " -m (keys|formats|nodes|nodes_ondisk|nodes_keys)\n"
456 " -f Check (fsck) the filesystem first\n"
458 " -h Display this help and exit\n"
459 "Report bugs to <linux-bcache@vger.kernel.org>");
462 #define LIST_MODES() \
470 #define x(n) LIST_MODE_##n,
475 static const char * const list_modes[] = {
482 int cmd_list(int argc, char *argv[])
484 struct bch_opts opts = bch2_opts_empty();
485 enum btree_id btree_id_start = 0;
486 enum btree_id btree_id_end = BTREE_ID_NR;
487 enum btree_id btree_id;
489 struct bpos start = POS_MIN, end = POS_MAX;
493 opt_set(opts, nochanges, true);
494 opt_set(opts, norecovery, true);
495 opt_set(opts, degraded, true);
496 opt_set(opts, errors, BCH_ON_ERROR_continue);
498 while ((opt = getopt(argc, argv, "b:l:s:e:i:m:fvh")) != -1)
501 btree_id_start = read_string_list_or_die(optarg,
502 bch2_btree_ids, "btree id");
503 btree_id_end = btree_id_start + 1;
506 if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
507 die("invalid level");
510 start = bpos_parse(optarg);
513 end = bpos_parse(optarg);
516 if (kstrtoull(optarg, 10, &inum))
517 die("invalid inode %s", optarg);
518 start = POS(inum, 0);
519 end = POS(inum + 1, 0);
522 mode = read_string_list_or_die(optarg,
523 list_modes, "list mode");
526 opt_set(opts, fix_errors, FSCK_OPT_YES);
527 opt_set(opts, norecovery, false);
530 opt_set(opts, verbose, true);
539 die("Please supply device(s)");
541 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
543 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
546 for (btree_id = btree_id_start;
547 btree_id < btree_id_end;
551 list_keys(c, btree_id, start, end);
553 case LIST_MODE_formats:
554 list_btree_formats(c, btree_id, level, start, end);
556 case LIST_MODE_nodes:
557 list_nodes(c, btree_id, level, start, end);
559 case LIST_MODE_nodes_ondisk:
560 list_nodes_ondisk(c, btree_id, level, start, end);
562 case LIST_MODE_nodes_keys:
563 list_nodes_keys(c, btree_id, level, start, end);
574 static void list_journal_usage(void)
576 puts("bcachefs list_journal - print contents of journal\n"
577 "Usage: bcachefs list_journal [OPTION]... <devices>\n"
580 " -a Read entire journal, not just dirty entries\n"
581 " -n Number of journal entries to print, starting from the most recent\n"
582 " -h Display this help and exit\n"
583 "Report bugs to <linux-bcache@vger.kernel.org>");
586 static void star_start_of_lines(char *buf)
593 while ((p = strstr(p, "\n ")))
597 int cmd_list_journal(int argc, char *argv[])
599 struct bch_opts opts = bch2_opts_empty();
600 u32 nr_entries = U32_MAX;
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, "an:h")) != -1)
614 opt_set(opts, read_entire_journal, true);
617 nr_entries = kstrtouint(optarg, 10, &nr_entries);
618 opt_set(opts, read_entire_journal, true);
621 list_journal_usage();
627 die("Please supply device(s) to open");
629 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
631 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
633 struct journal_replay *p, **_p;
634 struct genradix_iter iter;
635 struct jset_entry *entry;
636 struct printbuf buf = PRINTBUF;
638 genradix_for_each(&c->journal_entries, iter, _p) {
643 if (le64_to_cpu(p->j.seq) + nr_entries < atomic64_read(&c->journal.seq))
647 bch2_journal_seq_is_blacklisted(c,
648 le64_to_cpu(p->j.seq), false);
651 printf("blacklisted ");
653 printf("journal entry %llu\n", le64_to_cpu(p->j.seq));
655 printbuf_reset(&buf);
662 le32_to_cpu(p->j.version),
663 le64_to_cpu(p->j.last_seq),
664 !JSET_NO_FLUSH(&p->j));
665 bch2_journal_ptrs_to_text(&buf, c, p);
668 star_start_of_lines(buf.buf);
669 printf("%s\n", buf.buf);
671 vstruct_for_each(&p->j, entry) {
672 printbuf_reset(&buf);
675 * log entries denote the start of a new transaction
678 if (entry->type == BCH_JSET_ENTRY_log && !entry->level)
680 pr_indent_push(&buf, 4);
681 bch2_journal_entry_to_text(&buf, c, entry);
684 star_start_of_lines(buf.buf);
685 printf("%s\n", buf.buf);
694 static void kill_btree_node_usage(void)
696 puts("bcachefs kill_btree_node - make btree nodes unreadable\n"
697 "Usage: bcachefs kill_btree_node [OPTION]... <devices>\n"
700 " -b (extents|inodes|dirents|xattrs) Btree to delete from\n"
701 " -l level Levle to delete from (0 == leaves)\n"
702 " -i index Index of btree node to kill\n"
703 " -h Display this help and exit\n"
704 "Report bugs to <linux-bcache@vger.kernel.org>");
707 int cmd_kill_btree_node(int argc, char *argv[])
709 struct bch_opts opts = bch2_opts_empty();
710 enum btree_id btree_id = 0;
715 opt_set(opts, read_only, true);
717 while ((opt = getopt(argc, argv, "b:l:i:h")) != -1)
720 btree_id = read_string_list_or_die(optarg,
721 bch2_btree_ids, "btree id");
724 if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
725 die("invalid level");
728 if (kstrtoull(optarg, 10, &node_index))
729 die("invalid index %s", optarg);
732 kill_btree_node_usage();
738 die("Please supply device(s)");
740 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
742 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
744 struct btree_trans trans;
745 struct btree_iter iter;
750 ret = posix_memalign(&zeroes, c->opts.block_size, c->opts.block_size);
752 die("error %s from posix_memalign", strerror(ret));
754 bch2_trans_init(&trans, c, 0, 0);
756 __for_each_btree_node(&trans, iter, btree_id, POS_MIN, 0, level, 0, b, ret) {
757 if (b->c.level != level)
761 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
762 const struct bch_extent_ptr *ptr;
764 struct printbuf buf = PRINTBUF;
766 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
767 bch_info(c, "killing btree node %s", buf.buf);
770 bkey_for_each_ptr(ptrs, ptr) {
771 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
773 ret = pwrite(ca->disk_sb.bdev->bd_fd, zeroes,
774 c->opts.block_size, ptr->offset << 9);
775 if (ret != c->opts.block_size) {
776 bch_err(c, "pwrite error: expected %u got %i %s",
777 c->opts.block_size, ret, strerror(errno));
788 bch_err(c, "error %i walking btree nodes", ret);
790 bch_err(c, "node at specified index not found");
793 bch2_trans_iter_exit(&trans, &iter);
794 bch2_trans_exit(&trans);