]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_kill_btree_node.c
Merge https://github.com/fougner/bcachefs-tools
[bcachefs-tools-debian] / cmd_kill_btree_node.c
1 #include <fcntl.h>
2 #include <string.h>
3 #include <sys/stat.h>
4 #include <sys/types.h>
5
6 #include "cmds.h"
7 #include "libbcachefs.h"
8 #include "tools-util.h"
9
10 #include "libbcachefs/bcachefs.h"
11 #include "libbcachefs/btree_iter.h"
12 #include "libbcachefs/errcode.h"
13 #include "libbcachefs/error.h"
14 #include "libbcachefs/super.h"
15
16 static void kill_btree_node_usage(void)
17 {
18         puts("bcachefs kill_btree_node - make btree nodes unreadable\n"
19              "Usage: bcachefs kill_btree_node [OPTION]... <devices>\n"
20              "\n"
21              "Options:\n"
22              "  -b (extents|inodes|dirents|xattrs)    Btree to delete from\n"
23              "  -l level                              Levle to delete from (0 == leaves)\n"
24              "  -i index                              Index of btree node to kill\n"
25              "  -h                                    Display this help and exit\n"
26              "Report bugs to <linux-bcachefs@vger.kernel.org>");
27 }
28
29 int cmd_kill_btree_node(int argc, char *argv[])
30 {
31         struct bch_opts opts = bch2_opts_empty();
32         enum btree_id btree_id = 0;
33         unsigned level = 0;
34         u64 node_index = 0;
35         int opt;
36
37         opt_set(opts, read_only,        true);
38
39         while ((opt = getopt(argc, argv, "b:l:i:h")) != -1)
40                 switch (opt) {
41                 case 'b':
42                         btree_id = read_string_list_or_die(optarg,
43                                                 bch2_btree_ids, "btree id");
44                         break;
45                 case 'l':
46                         if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
47                                 die("invalid level");
48                         break;
49                 case 'i':
50                         if (kstrtoull(optarg, 10, &node_index))
51                                 die("invalid index %s", optarg);
52                         break;
53                 case 'h':
54                         kill_btree_node_usage();
55                         exit(EXIT_SUCCESS);
56                 }
57         args_shift(optind);
58
59         if (!argc)
60                 die("Please supply device(s)");
61
62         struct bch_fs *c = bch2_fs_open(argv, argc, opts);
63         if (IS_ERR(c))
64                 die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
65
66         struct btree_trans trans;
67         struct btree_iter iter;
68         struct btree *b;
69         int ret;
70         void *zeroes;
71
72         ret = posix_memalign(&zeroes, c->opts.block_size, c->opts.block_size);
73         if (ret)
74                 die("error %s from posix_memalign", bch2_err_str(ret));
75
76         bch2_trans_init(&trans, c, 0, 0);
77
78         __for_each_btree_node(&trans, iter, btree_id, POS_MIN, 0, level, 0, b, ret) {
79                 if (b->c.level != level)
80                         continue;
81
82                 if (!node_index) {
83                         struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
84                         const struct bch_extent_ptr *ptr;
85
86                         struct printbuf buf = PRINTBUF;
87
88                         bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
89                         bch_info(c, "killing btree node %s", buf.buf);
90                         printbuf_exit(&buf);
91
92                         bkey_for_each_ptr(ptrs, ptr) {
93                                 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
94
95                                 ret = pwrite(ca->disk_sb.bdev->bd_fd, zeroes,
96                                              c->opts.block_size, ptr->offset << 9);
97                                 if (ret != c->opts.block_size) {
98                                         bch_err(c, "pwrite error: expected %u got %i %s",
99                                                 c->opts.block_size, ret, strerror(errno));
100                                         ret = EXIT_FAILURE;
101                                         goto done;
102                                 }
103                         }
104                         goto done;
105                 }
106
107                 node_index--;
108         }
109         if (ret)
110                 bch_err(c, "error %i walking btree nodes", ret);
111         else
112                 bch_err(c, "node at specified index not found");
113         ret = EXIT_FAILURE;
114 done:
115         bch2_trans_iter_exit(&trans, &iter);
116         bch2_trans_exit(&trans);
117
118         bch2_fs_stop(c);
119         return ret;
120 }