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_iter iter;
65 for_each_btree_node(&iter, c, i, POS_MIN, 0, b) {
66 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
68 extent_for_each_ptr(e, ptr)
69 if (ptr->dev == ca->dev_idx)
74 bch2_btree_iter_unlock(&iter);
77 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
78 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
81 int cmd_dump(int argc, char *argv[])
83 struct bch_opts opts = bch2_opts_empty();
86 unsigned i, nr_devices = 0;
90 opt_set(opts, nochanges, true);
91 opt_set(opts, noreplay, true);
92 opt_set(opts, degraded, true);
93 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
95 while ((opt = getopt(argc, argv, "o:fh")) != -1)
110 die("Please supply output filename");
113 die("Please supply device(s) to check");
115 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
117 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
119 down_read(&c->gc_lock);
121 for_each_online_member(ca, c, i)
126 for_each_online_member(ca, c, i) {
127 int flags = O_WRONLY|O_CREAT|O_TRUNC;
135 char *path = nr_devices > 1
136 ? mprintf("%s.%u", out, i)
138 fd = xopen(path, flags, 0600);
141 dump_one_device(c, ca, fd);
145 up_read(&c->gc_lock);
151 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
152 struct bpos start, struct bpos end)
154 struct btree_iter iter;
158 for_each_btree_key(&iter, c, btree_id, start,
159 BTREE_ITER_PREFETCH, k) {
160 if (bkey_cmp(k.k->p, end) > 0)
163 bch2_bkey_val_to_text(c, bkey_type(0, btree_id),
164 buf, sizeof(buf), k);
167 bch2_btree_iter_unlock(&iter);
170 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
171 struct bpos start, struct bpos end)
173 struct btree_iter iter;
177 for_each_btree_node(&iter, c, btree_id, start, 0, b) {
178 if (bkey_cmp(b->key.k.p, end) > 0)
181 bch2_print_btree_node(c, b, buf, sizeof(buf));
184 bch2_btree_iter_unlock(&iter);
187 static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id,
188 struct bpos start, struct bpos end)
190 struct btree_iter iter;
191 struct btree_node_iter node_iter;
192 struct bkey unpacked;
197 for_each_btree_node(&iter, c, btree_id, start, 0, b) {
198 if (bkey_cmp(b->key.k.p, end) > 0)
201 bch2_print_btree_node(c, b, buf, sizeof(buf));
206 for_each_btree_node_key_unpack(b, k, &node_iter, &unpacked) {
207 bch2_bkey_val_to_text(c, bkey_type(0, btree_id),
208 buf + 1, sizeof(buf) - 1, k);
212 bch2_btree_iter_unlock(&iter);
215 static struct bpos parse_pos(char *buf)
217 char *s = buf, *field;
218 u64 inode_v = 0, offset_v = 0;
220 if (!(field = strsep(&s, ":")) ||
221 kstrtoull(field, 10, &inode_v))
222 die("invalid bpos %s", buf);
224 if ((field = strsep(&s, ":")) &&
225 kstrtoull(field, 10, &offset_v))
226 die("invalid bpos %s", buf);
229 die("invalid bpos %s", buf);
231 return (struct bpos) { .inode = inode_v, .offset = offset_v };
234 static void list_keys_usage(void)
236 puts("bcachefs list - list filesystem metadata to stdout\n"
237 "Usage: bcachefs list [OPTION]... <devices>\n"
240 " -b (extents|inodes|dirents|xattrs) Btree to list from\n"
241 " -s inode:offset Start position to list from\n"
242 " -e inode:offset End position\n"
243 " -i inode List keys for a given inode number\n"
244 " -m (keys|formats) List mode\n"
245 " -f Check (fsck) the filesystem first\n"
247 " -h Display this help and exit\n"
248 "Report bugs to <linux-bcache@vger.kernel.org>");
251 static const char * const list_modes[] = {
258 int cmd_list(int argc, char *argv[])
260 struct bch_opts opts = bch2_opts_empty();
261 enum btree_id btree_id = BTREE_ID_EXTENTS;
262 struct bpos start = POS_MIN, end = POS_MAX;
266 opt_set(opts, nochanges, true);
267 opt_set(opts, norecovery, true);
268 opt_set(opts, degraded, true);
269 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
271 while ((opt = getopt(argc, argv, "b:s:e:i:m:fvh")) != -1)
274 btree_id = read_string_list_or_die(optarg,
275 bch2_btree_ids, "btree id");
278 start = parse_pos(optarg);
281 end = parse_pos(optarg);
284 if (kstrtoull(optarg, 10, &inum))
285 die("invalid inode %s", optarg);
286 start = POS(inum, 0);
287 end = POS(inum + 1, 0);
290 mode = read_string_list_or_die(optarg,
291 list_modes, "list mode");
294 opt_set(opts, fix_errors, FSCK_OPT_YES);
295 opt_set(opts, norecovery, false);
298 opt_set(opts, verbose_recovery, true);
307 die("Please supply device(s)");
309 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
311 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
315 list_keys(c, btree_id, start, end);
318 list_btree_formats(c, btree_id, start, end);
321 list_nodes_keys(c, btree_id, start, end);