7 #include "libbcachefs.h"
9 #include "tools-util.h"
11 #include "libbcachefs/bcachefs.h"
12 #include "libbcachefs/alloc.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 " -h Display this help and exit\n"
28 "Report bugs to <linux-bcache@vger.kernel.org>");
31 static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd)
33 struct bch_sb *sb = ca->disk_sb.sb;
40 range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
41 sizeof(struct bch_sb_layout));
43 for (i = 0; i < sb->layout.nr_superblocks; i++)
45 le64_to_cpu(sb->layout.sb_offset[i]) << 9,
49 for (i = 0; i < ca->journal.nr; i++)
50 if (ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
51 u64 bucket = ca->journal.buckets[i];
54 bucket_bytes(ca) * bucket,
59 for (i = 0; i < BTREE_ID_NR; i++) {
60 const struct bch_extent_ptr *ptr;
61 struct btree_iter iter;
64 for_each_btree_node(&iter, c, i, POS_MIN, 0, b) {
65 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
67 extent_for_each_ptr(e, ptr)
68 if (ptr->dev == ca->dev_idx)
73 bch2_btree_iter_unlock(&iter);
76 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
77 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
80 int cmd_dump(int argc, char *argv[])
82 struct bch_opts opts = bch2_opts_empty();
85 unsigned i, nr_devices = 0;
89 opt_set(opts, nochanges, true);
90 opt_set(opts, noreplay, true);
91 opt_set(opts, degraded, true);
92 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
94 while ((opt = getopt(argc, argv, "o:fh")) != -1)
109 die("Please supply output filename");
112 die("Please supply device(s) to check");
114 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
116 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
118 down_read(&c->gc_lock);
120 for_each_online_member(ca, c, i)
125 for_each_online_member(ca, c, i) {
126 int flags = O_WRONLY|O_CREAT|O_TRUNC;
134 char *path = nr_devices > 1
135 ? mprintf("%s.%u", out, i)
137 fd = xopen(path, flags, 0600);
140 dump_one_device(c, ca, fd);
144 up_read(&c->gc_lock);
150 static void list_keys(struct bch_fs *c, enum btree_id btree_id,
151 struct bpos start, struct bpos end)
153 struct btree_iter iter;
157 for_each_btree_key(&iter, c, btree_id, start,
158 BTREE_ITER_PREFETCH, k) {
159 if (bkey_cmp(k.k->p, end) > 0)
162 bch2_bkey_val_to_text(c, bkey_type(0, btree_id),
163 buf, sizeof(buf), k);
166 bch2_btree_iter_unlock(&iter);
169 static void list_btree_formats(struct bch_fs *c, enum btree_id btree_id,
170 struct bpos start, struct bpos end)
172 struct btree_iter iter;
176 for_each_btree_node(&iter, c, btree_id, start, 0, b) {
177 if (bkey_cmp(b->key.k.p, end) > 0)
180 bch2_print_btree_node(c, b, buf, sizeof(buf));
183 bch2_btree_iter_unlock(&iter);
186 static void list_nodes_keys(struct bch_fs *c, enum btree_id btree_id,
187 struct bpos start, struct bpos end)
189 struct btree_iter iter;
190 struct btree_node_iter node_iter;
191 struct bkey unpacked;
196 for_each_btree_node(&iter, c, btree_id, start, 0, b) {
197 if (bkey_cmp(b->key.k.p, end) > 0)
200 bch2_print_btree_node(c, b, buf, sizeof(buf));
205 for_each_btree_node_key_unpack(b, k, &node_iter,
206 btree_node_is_extents(b),
208 bch2_bkey_val_to_text(c, bkey_type(0, btree_id),
209 buf + 1, sizeof(buf) - 1, k);
213 bch2_btree_iter_unlock(&iter);
216 static struct bpos parse_pos(char *buf)
218 char *s = buf, *field;
219 u64 inode_v = 0, offset_v = 0;
221 if (!(field = strsep(&s, ":")) ||
222 kstrtoull(field, 10, &inode_v))
223 die("invalid bpos %s", buf);
225 if ((field = strsep(&s, ":")) &&
226 kstrtoull(field, 10, &offset_v))
227 die("invalid bpos %s", buf);
230 die("invalid bpos %s", buf);
232 return (struct bpos) { .inode = inode_v, .offset = offset_v };
235 static void list_keys_usage(void)
237 puts("bcachefs list_keys - list filesystem metadata to stdout\n"
238 "Usage: bcachefs list_keys [OPTION]... <devices>\n"
241 " -b (extents|inodes|dirents|xattrs) Btree to list from\n"
242 " -s inode:offset Start position to list from\n"
243 " -e inode:offset End position\n"
244 " -i inode List keys for a given inode number\n"
245 " -m (keys|formats) List mode\n"
246 " -h Display this help and exit\n"
247 "Report bugs to <linux-bcache@vger.kernel.org>");
250 static const char * const list_modes[] = {
257 int cmd_list(int argc, char *argv[])
259 struct bch_opts opts = bch2_opts_empty();
260 enum btree_id btree_id = BTREE_ID_EXTENTS;
261 struct bpos start = POS_MIN, end = POS_MAX;
265 opt_set(opts, nochanges, true);
266 opt_set(opts, norecovery, true);
267 opt_set(opts, degraded, true);
268 opt_set(opts, errors, BCH_ON_ERROR_CONTINUE);
270 while ((opt = getopt(argc, argv, "b:s:e:i:m:fvh")) != -1)
273 btree_id = read_string_list_or_die(optarg,
274 bch2_btree_ids, "btree id");
277 start = parse_pos(optarg);
280 end = parse_pos(optarg);
283 if (kstrtoull(optarg, 10, &inum))
284 die("invalid inode %s", optarg);
285 start = POS(inum, 0);
286 end = POS(inum + 1, 0);
289 mode = read_string_list_or_die(optarg,
290 list_modes, "list mode");
293 opt_set(opts, fix_errors, FSCK_OPT_YES);
294 opt_set(opts, norecovery, false);
297 opt_set(opts, verbose_recovery, true);
306 die("Please supply device(s)");
308 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
310 die("error opening %s: %s", argv[0], strerror(-PTR_ERR(c)));
314 list_keys(c, btree_id, start, end);
317 list_btree_formats(c, btree_id, start, end);
320 list_nodes_keys(c, btree_id, start, end);