9 #include "tools-util.h"
13 #include "btree_cache.h"
14 #include "btree_iter.h"
19 static void dump_usage(void)
21 puts("bcache dump - dump filesystem metadata\n"
22 "Usage: bcache dump [OPTION]... <devices>\n"
25 " -o output Output qcow2 image(s)\n"
26 " -h Display this help and exit\n"
27 "Report bugs to <linux-bcache@vger.kernel.org>");
30 static void dump_one_device(struct cache_set *c, struct cache *ca, int fd)
32 struct bch_sb *sb = ca->disk_sb.sb;
39 data_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
40 sizeof(struct bch_sb_layout));
42 for (i = 0; i < sb->layout.nr_superblocks; i++)
44 le64_to_cpu(sb->layout.sb_offset[i]) << 9,
48 for (i = 0; i < ca->journal.nr; i++)
49 if (ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
50 u64 bucket = ca->journal.buckets[i];
53 bucket_bytes(ca) * bucket,
58 for (i = 0; i < prio_buckets(ca); i++)
60 bucket_bytes(ca) * ca->prio_last_buckets[i],
64 for (i = 0; i < BTREE_ID_NR; i++) {
65 const struct bch_extent_ptr *ptr;
66 struct btree_iter iter;
69 for_each_btree_node(&iter, c, i, POS_MIN, 0, b) {
70 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(&b->key);
72 extent_for_each_ptr(e, ptr)
73 if (ptr->dev == ca->dev_idx)
78 bch_btree_iter_unlock(&iter);
81 qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
82 max_t(unsigned, btree_bytes(c) / 8, block_bytes(c)));
85 int cmd_dump(int argc, char *argv[])
87 struct bch_opts opts = bch_opts_empty();
88 struct cache_set *c = NULL;
90 char *out = NULL, *buf;
91 unsigned i, nr_devices = 0;
95 opts.nochanges = true;
97 opts.errors = BCH_ON_ERROR_CONTINUE;
98 fsck_err_opt = FSCK_ERR_NO;
100 while ((opt = getopt(argc, argv, "o:fh")) != -1)
114 die("Please supply device(s) to check");
117 die("Please supply output filename");
119 buf = alloca(strlen(out) + 10);
122 err = bch_fs_open(argv + optind, argc - optind, opts, &c);
124 die("error opening %s: %s", argv[optind], err);
126 down_read(&c->gc_lock);
128 for (i = 0; i < c->sb.nr_devices; i++)
134 for (i = 0; i < c->sb.nr_devices; i++) {
135 int mode = O_WRONLY|O_CREAT|O_TRUNC;
144 sprintf(buf, "%s.%u", out, i);
146 fd = open(buf, mode, 0600);
148 die("error opening %s: %s", buf, strerror(errno));
150 dump_one_device(c, c->cache[i], fd);
154 up_read(&c->gc_lock);
160 static void list_keys(struct cache_set *c, enum btree_id btree_id,
161 struct bpos start, struct bpos end, int mode)
163 struct btree_iter iter;
167 for_each_btree_key(&iter, c, btree_id, start, k) {
168 if (bkey_cmp(k.k->p, end) > 0)
171 bch_bkey_val_to_text(c, bkey_type(0, btree_id),
172 buf, sizeof(buf), k);
175 bch_btree_iter_unlock(&iter);
178 static void list_btree_formats(struct cache_set *c, enum btree_id btree_id,
179 struct bpos start, struct bpos end, int mode)
181 struct btree_iter iter;
185 for_each_btree_node(&iter, c, btree_id, start, 0, b) {
186 if (bkey_cmp(b->key.k.p, end) > 0)
189 bch_print_btree_node(c, b, buf, sizeof(buf));
192 bch_btree_iter_unlock(&iter);
195 static struct bpos parse_pos(char *buf)
198 char *inode = strsep(&s, ":");
199 char *offset = strsep(&s, ":");
200 struct bpos ret = { 0 };
202 if (!inode || !offset || s ||
203 kstrtoull(inode, 10, &ret.inode) ||
204 kstrtoull(offset, 10, &ret.offset))
205 die("invalid bpos %s", buf);
210 static void list_keys_usage(void)
212 puts("bcache list_keys - list filesystem metadata to stdout\n"
213 "Usage: bcache list_keys [OPTION]... <devices>\n"
216 " -b btree_id Integer btree id to list\n"
217 " -s start Start pos (as inode:offset)\n"
219 " -m mode Mode for listing\n"
220 " -h Display this help and exit\n"
221 "Report bugs to <linux-bcache@vger.kernel.org>");
224 int cmd_list(int argc, char *argv[])
226 struct bch_opts opts = bch_opts_empty();
227 struct cache_set *c = NULL;
228 enum btree_id btree_id = BTREE_ID_EXTENTS;
229 struct bpos start = POS_MIN, end = POS_MAX;
234 opts.nochanges = true;
235 opts.norecovery = true;
236 opts.errors = BCH_ON_ERROR_CONTINUE;
237 fsck_err_opt = FSCK_ERR_NO;
239 while ((opt = getopt(argc, argv, "b:s:e:m:h")) != -1)
242 if (kstrtoull(optarg, 10, &v) ||
244 die("invalid btree id");
248 start = parse_pos(optarg);
251 end = parse_pos(optarg);
261 die("Please supply device(s) to check");
263 err = bch_fs_open(argv + optind, argc - optind, opts, &c);
265 die("error opening %s: %s", argv[optind], err);
269 list_keys(c, btree_id, start, end, mode);
272 list_btree_formats(c, btree_id, start, end, mode);