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/sb-members.h"
15 #include "libbcachefs/super.h"
17 static void kill_btree_node_usage(void)
19 puts("bcachefs kill_btree_node - make btree nodes unreadable\n"
20 "Usage: bcachefs kill_btree_node [OPTION]... <devices>\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>");
30 int cmd_kill_btree_node(int argc, char *argv[])
32 struct bch_opts opts = bch2_opts_empty();
33 enum btree_id btree_id = 0;
38 opt_set(opts, read_only, true);
40 while ((opt = getopt(argc, argv, "b:l:i:h")) != -1)
43 btree_id = read_string_list_or_die(optarg,
44 __bch2_btree_ids, "btree id");
47 if (kstrtouint(optarg, 10, &level) || level >= BTREE_MAX_DEPTH)
51 if (kstrtoull(optarg, 10, &node_index))
52 die("invalid index %s", optarg);
55 kill_btree_node_usage();
61 die("Please supply device(s)");
63 struct bch_fs *c = bch2_fs_open(argv, argc, opts);
65 die("error opening %s: %s", argv[0], bch2_err_str(PTR_ERR(c)));
67 struct btree_trans *trans = bch2_trans_get(c);
68 struct btree_iter iter;
73 ret = posix_memalign(&zeroes, c->opts.block_size, c->opts.block_size);
75 die("error %s from posix_memalign", bch2_err_str(ret));
77 __for_each_btree_node(trans, iter, btree_id, POS_MIN, 0, level, 0, b, ret) {
78 if (b->c.level != level)
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);
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);
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));
107 bch_err(c, "error %i walking btree nodes", ret);
109 bch_err(c, "node at specified index not found");
112 bch2_trans_iter_exit(trans, &iter);
113 bch2_trans_put(trans);