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_iter.h"
15 #include "libbcachefs/buckets.h"
16 #include "libbcachefs/error.h"
17 #include "libbcachefs/journal.h"
18 #include "libbcachefs/journal_io.h"
19 #include "libbcachefs/super.h"
21 static void dump_usage(void)
23 puts("bcachefs dump - dump filesystem metadata\n"
24 "Usage: bcachefs dump [OPTION]... <devices>\n"
27 " -o output Output qcow2 image(s)\n"
28 " -f Force; overwrite when needed\n"
29 " -h Display this help and exit\n"
30 "Report bugs to <linux-bcache@vger.kernel.org>");
33 static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd)
35 struct bch_sb *sb = ca->disk_sb.sb;
42 range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
43 sizeof(struct bch_sb_layout));
45 for (i = 0; i < sb->layout.nr_superblocks; i++)
47 le64_to_cpu(sb->layout.sb_offset[i]) << 9,
51 for (i = 0; i < ca->journal.nr; i++)
52 if (ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
53 u64 bucket = ca->journal.buckets[i];
56 bucket_bytes(ca) * bucket,
61 for (i = 0; i < BTREE_ID_NR; i++) {
62 const struct bch_extent_ptr *ptr;
63 struct bkey_ptrs_c ptrs;
64 struct btree_trans trans;
65 struct btree_iter *iter;
68 bch2_trans_init(&trans, c, 0, 0);
70 __for_each_btree_node(&trans, iter, i, POS_MIN, 0, 1, 0, b) {
71 struct btree_node_iter iter;
75 for_each_btree_node_key_unpack(b, k, &iter, &u) {
76 ptrs = bch2_bkey_ptrs_c(k);
78 bkey_for_each_ptr(ptrs, ptr)
79 if (ptr->dev == ca->dev_idx)
86 b = c->btree_roots[i].b;
87 if (!btree_node_fake(b)) {
88 ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
90 bkey_for_each_ptr(ptrs, ptr)
91 if (ptr->dev == ca->dev_idx)
96 bch2_trans_exit(&trans);
99 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
100 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
103 int cmd_dump(int argc, char *argv[])
105 struct bch_opts opts = bch2_opts_empty();
108 unsigned i, nr_devices = 0;
112 opt_set(opts, nochanges, true);
113 opt_set(opts, norecovery, true);
114 opt_set(opts, degraded, true);
115 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
116 opt_set(opts, fix_errors, FSCK_OPT_YES);
118 while ((opt = getopt(argc, argv, "o:fvh")) != -1)
127 opt_set(opts, verbose, true);
136 die("Please supply output filename");
139 die("Please supply device(s) to check");
141 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
143 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
145 down_read(&c->gc_lock);
147 for_each_online_member(ca, c, i)
152 for_each_online_member(ca, c, i) {
153 int flags = O_WRONLY|O_CREAT|O_TRUNC;
161 char *path = nr_devices > 1
162 ? mprintf("%s.%u", out, i)
164 fd = xopen(path, flags, 0600);
167 dump_one_device(c, ca, fd);
171 up_read(&c->gc_lock);
177 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
178 struct bpos start, struct bpos end)
180 struct btree_trans trans;
181 struct btree_iter *iter;
186 bch2_trans_init(&trans, c, 0, 0);
188 for_each_btree_key(&trans, iter, btree_id, start,
189 BTREE_ITER_PREFETCH, k, ret) {
190 if (bkey_cmp(k.k->p, end) > 0)
193 bch2_bkey_val_to_text(&PBUF(buf), c, k);
196 bch2_trans_exit(&trans);
199 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
200 struct bpos start, struct bpos end)
202 struct btree_trans trans;
203 struct btree_iter *iter;
207 bch2_trans_init(&trans, c, 0, 0);
209 for_each_btree_node(&trans, iter, btree_id, start, 0, b) {
210 if (bkey_cmp(b->key.k.p, end) > 0)
213 bch2_btree_node_to_text(&PBUF(buf), c, b);
216 bch2_trans_exit(&trans);
219 static void list_nodes(struct bch_fs *c, enum btree_id btree_id,
220 struct bpos start, struct bpos end)
222 struct btree_trans trans;
223 struct btree_iter *iter;
227 bch2_trans_init(&trans, c, 0, 0);
229 for_each_btree_node(&trans, iter, btree_id, start, 0, b) {
230 if (bkey_cmp(b->key.k.p, end) > 0)
233 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(&b->key));
237 bch2_trans_exit(&trans);
240 static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id,
241 struct bpos start, struct bpos end)
243 struct btree_trans trans;
244 struct btree_iter *iter;
245 struct btree_node_iter node_iter;
246 struct bkey unpacked;
251 bch2_trans_init(&trans, c, 0, 0);
253 for_each_btree_node(&trans, iter, btree_id, start, 0, b) {
254 if (bkey_cmp(b->key.k.p, end) > 0)
257 bch2_btree_node_to_text(&PBUF(buf), c, b);
260 for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) {
261 bch2_bkey_val_to_text(&PBUF(buf), c, k);
266 bch2_trans_exit(&trans);
269 static struct bpos parse_pos(char *buf)
271 char *s = buf, *field;
272 u64 inode_v = 0, offset_v = 0;
274 if (!(field = strsep(&s, ":")) ||
275 kstrtoull(field, 10, &inode_v))
276 die("invalid bpos %s", buf);
278 if ((field = strsep(&s, ":")) &&
279 kstrtoull(field, 10, &offset_v))
280 die("invalid bpos %s", buf);
283 die("invalid bpos %s", buf);
285 return (struct bpos) { .inode = inode_v, .offset = offset_v };
288 static void list_keys_usage(void)
290 puts("bcachefs list - list filesystem metadata to stdout\n"
291 "Usage: bcachefs list [OPTION]... <devices>\n"
294 " -b (extents|inodes|dirents|xattrs) Btree to list from\n"
295 " -s inode:offset Start position to list from\n"
296 " -e inode:offset End position\n"
297 " -i inode List keys for a given inode number\n"
298 " -m (keys|formats) List mode\n"
299 " -f Check (fsck) the filesystem first\n"
301 " -h Display this help and exit\n"
302 "Report bugs to <linux-bcache@vger.kernel.org>");
305 static const char * const list_modes[] = {
313 int cmd_list(int argc, char *argv[])
315 struct bch_opts opts = bch2_opts_empty();
316 enum btree_id btree_id_start = 0;
317 enum btree_id btree_id_end = BTREE_ID_NR;
318 enum btree_id btree_id;
319 struct bpos start = POS_MIN, end = POS_MAX;
323 opt_set(opts, nochanges, true);
324 opt_set(opts, norecovery, true);
325 opt_set(opts, degraded, true);
326 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
328 while ((opt = getopt(argc, argv, "b:s:e:i:m:fvh")) != -1)
331 btree_id_start = read_string_list_or_die(optarg,
332 bch2_btree_ids, "btree id");
333 btree_id_end = btree_id_start + 1;
336 start = parse_pos(optarg);
339 end = parse_pos(optarg);
342 if (kstrtoull(optarg, 10, &inum))
343 die("invalid inode %s", optarg);
344 start = POS(inum, 0);
345 end = POS(inum + 1, 0);
348 mode = read_string_list_or_die(optarg,
349 list_modes, "list mode");
352 opt_set(opts, fix_errors, FSCK_OPT_YES);
353 opt_set(opts, norecovery, false);
356 opt_set(opts, verbose, true);
365 die("Please supply device(s)");
367 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
369 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
372 for (btree_id = btree_id_start;
373 btree_id < btree_id_end;
377 list_keys(c, btree_id, start, end);
380 list_btree_formats(c, btree_id, start, end);
383 list_nodes(c, btree_id, start, end);
386 list_nodes_keys(c, btree_id, start, end);
397 static void list_journal_usage(void)
399 puts("bcachefs list_journal - print contents of journal\n"
400 "Usage: bcachefs list_journal [OPTION]... <devices>\n"
403 " -h Display this help and exit\n"
404 "Report bugs to <linux-bcache@vger.kernel.org>");
407 int cmd_list_journal(int argc, char *argv[])
409 struct bch_opts opts = bch2_opts_empty();
412 opt_set(opts, nochanges, true);
413 opt_set(opts, norecovery, true);
414 opt_set(opts, degraded, true);
415 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
416 opt_set(opts, fix_errors, FSCK_OPT_YES);
417 opt_set(opts, keep_journal, true);
419 while ((opt = getopt(argc, argv, "h")) != -1)
422 list_journal_usage();
428 die("Please supply device(s) to open");
430 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
432 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
434 struct journal_replay *p;
435 struct jset_entry *entry;
436 struct bkey_i *k, *_n;
438 /* This could be greatly expanded: */
440 list_for_each_entry(p, &c->journal_entries, list) {
441 printf("journal entry %8llu\n"
447 le64_to_cpu(p->j.seq),
448 le32_to_cpu(p->j.seq),
449 le64_to_cpu(p->j.last_seq),
450 le16_to_cpu(p->j.read_clock),
451 le16_to_cpu(p->j.write_clock));
453 for_each_jset_key(k, _n, entry, &p->j) {
456 bch2_bkey_val_to_text(&PBUF(buf), c, bkey_i_to_s_c(k));
457 printf("btree %s l %u: %s\n",
458 bch2_btree_ids[entry->btree_id],