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