]> git.sesse.net Git - bcachefs-tools-debian/blob - c_src/cmd_dump.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / c_src / cmd_dump.c
1 #include <fcntl.h>
2 #include <getopt.h>
3 #include <string.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6
7 #include "cmds.h"
8 #include "libbcachefs.h"
9 #include "qcow2.h"
10
11 #include "libbcachefs/bcachefs.h"
12 #include "libbcachefs/btree_cache.h"
13 #include "libbcachefs/btree_io.h"
14 #include "libbcachefs/btree_iter.h"
15 #include "libbcachefs/error.h"
16 #include "libbcachefs/extents.h"
17 #include "libbcachefs/sb-members.h"
18 #include "libbcachefs/super.h"
19
20 static void dump_usage(void)
21 {
22         puts("bcachefs dump - dump filesystem metadata\n"
23              "Usage: bcachefs dump [OPTION]... <devices>\n"
24              "\n"
25              "Options:\n"
26              "  -o output     Output qcow2 image(s)\n"
27              "  -f, --force   Force; overwrite when needed\n"
28              "  --nojournal   Don't dump entire journal, just dirty entries\n"
29              "  -h, --help    Display this help and exit\n"
30              "Report bugs to <linux-bcachefs@vger.kernel.org>");
31 }
32
33 static void dump_one_device(struct bch_fs *c, struct bch_dev *ca, int fd,
34                             bool entire_journal)
35 {
36         struct bch_sb *sb = ca->disk_sb.sb;
37         ranges data = { 0 };
38         unsigned i;
39         int ret;
40
41         /* Superblock: */
42         range_add(&data, BCH_SB_LAYOUT_SECTOR << 9,
43                   sizeof(struct bch_sb_layout));
44
45         for (i = 0; i < sb->layout.nr_superblocks; i++)
46                 range_add(&data,
47                           le64_to_cpu(sb->layout.sb_offset[i]) << 9,
48                           vstruct_bytes(sb));
49
50         /* Journal: */
51         for (i = 0; i < ca->journal.nr; i++)
52                 if (entire_journal ||
53                     ca->journal.bucket_seq[i] >= c->journal.last_seq_ondisk) {
54                         u64 bucket = ca->journal.buckets[i];
55
56                         range_add(&data,
57                                   bucket_bytes(ca) * bucket,
58                                   bucket_bytes(ca));
59                 }
60
61         /* Btree: */
62         for (i = 0; i < BTREE_ID_NR; i++) {
63                 struct bkey_ptrs_c ptrs;
64                 struct btree_trans *trans = bch2_trans_get(c);
65                 struct btree_iter iter;
66                 struct btree *b;
67
68                 __for_each_btree_node(trans, iter, i, POS_MIN, 0, 1, 0, b, ret) {
69                         struct btree_node_iter iter;
70                         struct bkey u;
71                         struct bkey_s_c k;
72
73                         for_each_btree_node_key_unpack(b, k, &iter, &u) {
74                                 ptrs = bch2_bkey_ptrs_c(k);
75
76                                 bkey_for_each_ptr(ptrs, ptr)
77                                         if (ptr->dev == ca->dev_idx)
78                                                 range_add(&data,
79                                                           ptr->offset << 9,
80                                                           btree_ptr_sectors_written(&b->key));
81                         }
82                 }
83
84                 if (ret)
85                         die("error %s walking btree nodes", bch2_err_str(ret));
86
87                 b = bch2_btree_id_root(c, i)->b;
88                 if (!btree_node_fake(b)) {
89                         ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
90
91                         bkey_for_each_ptr(ptrs, ptr)
92                                 if (ptr->dev == ca->dev_idx)
93                                         range_add(&data,
94                                                   ptr->offset << 9,
95                                                   btree_ptr_sectors_written(&b->key));
96                 }
97
98                 bch2_trans_iter_exit(trans, &iter);
99                 bch2_trans_put(trans);
100         }
101
102         qcow2_write_image(ca->disk_sb.bdev->bd_fd, fd, &data,
103                           max_t(unsigned, c->opts.btree_node_size / 8, block_bytes(c)));
104         darray_exit(&data);
105 }
106
107 int cmd_dump(int argc, char *argv[])
108 {
109         static const struct option longopts[] = {
110                 { "force",              no_argument,            NULL, 'f' },
111                 { "nojournal",          no_argument,            NULL, 'j' },
112                 { "verbose",            no_argument,            NULL, 'v' },
113                 { "help",               no_argument,            NULL, 'h' },
114                 { NULL }
115         };
116         struct bch_opts opts = bch2_opts_empty();
117         char *out = NULL;
118         unsigned nr_devices = 0;
119         bool force = false, entire_journal = true;
120         int fd, opt;
121
122         opt_set(opts, direct_io,        false);
123         opt_set(opts, read_only,        true);
124         opt_set(opts, nochanges,        true);
125         opt_set(opts, norecovery,       true);
126         opt_set(opts, degraded,         true);
127         opt_set(opts, errors,           BCH_ON_ERROR_continue);
128         opt_set(opts, fix_errors,       FSCK_FIX_no);
129
130         while ((opt = getopt_long(argc, argv, "o:fvh",
131                                   longopts, NULL)) != -1)
132                 switch (opt) {
133                 case 'o':
134                         out = optarg;
135                         break;
136                 case 'f':
137                         force = true;
138                         break;
139                 case 'j':
140                         entire_journal = false;
141                         break;
142                 case 'v':
143                         opt_set(opts, verbose, true);
144                         break;
145                 case 'h':
146                         dump_usage();
147                         exit(EXIT_SUCCESS);
148                 }
149         args_shift(optind);
150
151         if (!out)
152                 die("Please supply output filename");
153
154         if (!argc)
155                 die("Please supply device(s) to check");
156
157         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
158         if (IS_ERR(c))
159                 die("error opening devices: %s", bch2_err_str(PTR_ERR(c)));
160
161         down_read(&c->gc_lock);
162
163         for_each_online_member(c, ca)
164                 nr_devices++;
165
166         BUG_ON(!nr_devices);
167
168         for_each_online_member(c, ca) {
169                 int flags = O_WRONLY|O_CREAT|O_TRUNC;
170
171                 if (!force)
172                         flags |= O_EXCL;
173
174                 char *path = nr_devices > 1
175                         ? mprintf("%s.%u.qcow2", out, ca->dev_idx)
176                         : mprintf("%s.qcow2", out);
177                 fd = xopen(path, flags, 0600);
178                 free(path);
179
180                 dump_one_device(c, ca, fd, entire_journal);
181                 close(fd);
182         }
183
184         up_read(&c->gc_lock);
185
186         bch2_fs_stop(c);
187         return 0;
188 }