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