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/super.h"
20 static void dump_usage(void)
22 puts("bcachefs dump - dump filesystem metadata\n"
23 "Usage: bcachefs dump [OPTION]... <devices>\n"
26 " -o output Output qcow2 image(s)\n"
27 " -f Force; overwrite when needed\n"
28 " -h Display this help and exit\n"
29 "Report bugs to <linux-bcache@vger.kernel.org>");
32 static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd)
34 struct bch_sb *sb = ca->disk_sb.sb;
41 range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
42 sizeof(struct bch_sb_layout));
44 for (i = 0; i < sb->layout.nr_superblocks; i++)
46 le64_to_cpu(sb->layout.sb_offset[i]) << 9,
50 for (i = 0; i < ca->journal.nr; i++)
51 if (ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
52 u64 bucket = ca->journal.buckets[i];
55 bucket_bytes(ca) * bucket,
60 for (i = 0; i < BTREE_ID_NR; i++) {
61 const struct bch_extent_ptr *ptr;
62 struct btree_trans trans;
63 struct btree_iter *iter;
66 bch2_trans_init(&trans, c, 0, 0);
68 for_each_btree_node(&trans, iter, i, POS_MIN, 0, b) {
69 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
71 extent_for_each_ptr(e, ptr)
72 if (ptr->dev == ca->dev_idx)
77 bch2_trans_exit(&trans);
80 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
81 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
84 int cmd_dump(int argc, char *argv[])
86 struct bch_opts opts = bch2_opts_empty();
89 unsigned i, nr_devices = 0;
93 opt_set(opts, nochanges, true);
94 opt_set(opts, norecovery, true);
95 opt_set(opts, degraded, true);
96 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
98 while ((opt = getopt(argc, argv, "o:fh")) != -1)
113 die("Please supply output filename");
116 die("Please supply device(s) to check");
118 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
120 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
122 down_read(&c->gc_lock);
124 for_each_online_member(ca, c, i)
129 for_each_online_member(ca, c, i) {
130 int flags = O_WRONLY|O_CREAT|O_TRUNC;
138 char *path = nr_devices > 1
139 ? mprintf("%s.%u", out, i)
141 fd = xopen(path, flags, 0600);
144 dump_one_device(c, ca, fd);
148 up_read(&c->gc_lock);
154 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
155 struct bpos start, struct bpos end)
157 struct btree_trans trans;
158 struct btree_iter *iter;
163 bch2_trans_init(&trans, c, 0, 0);
165 for_each_btree_key(&trans, iter, btree_id, start,
166 BTREE_ITER_PREFETCH, k, ret) {
167 if (bkey_cmp(k.k->p, end) > 0)
170 bch2_bkey_val_to_text(&PBUF(buf), c, k);
173 bch2_trans_exit(&trans);
176 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
177 struct bpos start, struct bpos end)
179 struct btree_trans trans;
180 struct btree_iter *iter;
184 bch2_trans_init(&trans, c, 0, 0);
186 for_each_btree_node(&trans, iter, btree_id, start, 0, b) {
187 if (bkey_cmp(b->key.k.p, end) > 0)
190 bch2_btree_node_to_text(&PBUF(buf), c, b);
193 bch2_trans_exit(&trans);
196 static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id,
197 struct bpos start, struct bpos end)
199 struct btree_trans trans;
200 struct btree_iter *iter;
201 struct btree_node_iter node_iter;
202 struct bkey unpacked;
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 for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) {
217 bch2_bkey_val_to_text(&PBUF(buf), c, k);
222 bch2_trans_exit(&trans);
225 static struct bpos parse_pos(char *buf)
227 char *s = buf, *field;
228 u64 inode_v = 0, offset_v = 0;
230 if (!(field = strsep(&s, ":")) ||
231 kstrtoull(field, 10, &inode_v))
232 die("invalid bpos %s", buf);
234 if ((field = strsep(&s, ":")) &&
235 kstrtoull(field, 10, &offset_v))
236 die("invalid bpos %s", buf);
239 die("invalid bpos %s", buf);
241 return (struct bpos) { .inode = inode_v, .offset = offset_v };
244 static void list_keys_usage(void)
246 puts("bcachefs list - list filesystem metadata to stdout\n"
247 "Usage: bcachefs list [OPTION]... <devices>\n"
250 " -b (extents|inodes|dirents|xattrs) Btree to list from\n"
251 " -s inode:offset Start position to list from\n"
252 " -e inode:offset End position\n"
253 " -i inode List keys for a given inode number\n"
254 " -m (keys|formats) List mode\n"
255 " -f Check (fsck) the filesystem first\n"
257 " -h Display this help and exit\n"
258 "Report bugs to <linux-bcache@vger.kernel.org>");
261 static const char * const list_modes[] = {
268 int cmd_list(int argc, char *argv[])
270 struct bch_opts opts = bch2_opts_empty();
271 enum btree_id btree_id = BTREE_ID_EXTENTS;
272 struct bpos start = POS_MIN, end = POS_MAX;
276 opt_set(opts, nochanges, true);
277 opt_set(opts, norecovery, true);
278 opt_set(opts, degraded, true);
279 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
281 while ((opt = getopt(argc, argv, "b:s:e:i:m:fvh")) != -1)
284 btree_id = read_string_list_or_die(optarg,
285 bch2_btree_ids, "btree id");
288 start = parse_pos(optarg);
291 end = parse_pos(optarg);
294 if (kstrtoull(optarg, 10, &inum))
295 die("invalid inode %s", optarg);
296 start = POS(inum, 0);
297 end = POS(inum + 1, 0);
300 mode = read_string_list_or_die(optarg,
301 list_modes, "list mode");
304 opt_set(opts, fix_errors, FSCK_OPT_YES);
305 opt_set(opts, norecovery, false);
308 opt_set(opts, verbose, true);
317 die("Please supply device(s)");
319 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
321 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
325 list_keys(c, btree_id, start, end);
328 list_btree_formats(c, btree_id, start, end);
331 list_nodes_keys(c, btree_id, start, end);