7 #include "libbcachefs.h"
8 #include "tools-util.h"
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"
16 static void kill_btree_node_usage(void)
18 puts("bcachefs kill_btree_node - make btree nodes unreadable\n"
19 "Usage: bcachefs kill_btree_node [OPTION]... <devices>\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>");
29 int cmd_kill_btree_node(int argc, char *argv[])
31 struct bch_opts opts = bch2_opts_empty();
32 enum btree_id btree_id = 0;
37 opt_set(opts, read_only, true);
39 while ((opt = getopt(argc, argv, "b:l:i:h")) != -1)
42 btree_id = read_string_list_or_die(optarg,
43 bch2_btree_ids, "btree id");
46 if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
50 if (kstrtoull(optarg, 10, &node_index))
51 die("invalid index %s", optarg);
54 kill_btree_node_usage();
60 die("Please supply device(s)");
62 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
64 die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
66 struct btree_trans trans;
67 struct btree_iter iter;
72 ret = posix_memalign(&zeroes, c->opts.block_size, c->opts.block_size);
74 die("error %s from posix_memalign", bch2_err_str(ret));
76 bch2_trans_init(&trans, c, 0, 0);
78 __for_each_btree_node(&trans, iter, btree_id, POS_MIN, 0, level, 0, b, ret) {
79 if (b->c.level != level)
83 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(&b->key));
84 const struct bch_extent_ptr *ptr;
86 struct printbuf buf = PRINTBUF;
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);
92 bkey_for_each_ptr(ptrs, ptr) {
93 struct bch_dev *ca = bch_dev_bkey_exists(c, ptr->dev);
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));
110 bch_err(c, "error %i walking btree nodes", ret);
112 bch_err(c, "node at specified index not found");
115 bch2_trans_iter_exit(&trans, &iter);
116 bch2_trans_exit(&trans);