This introduces a new subcommand, "update", which is used to update a given field of a key to a value. For example:
$ bcachefs debug ~/test-img -c "update inodes 0:4096:U32_MAX bch_inode_unpacked.bi_nlink=35" $ bcachefs debug ~/test-img -c "dump inodes 0:4096:U32_MAX" | grep nlink bi_nlink=35 Signed-off-by: Thomas Bertschinger <tahbertschin...@gmail.com> --- c_src/cmd_debug.c | 107 +++++++++++++++++++++++++++++++ c_src/cmds.h | 16 +++++ src/commands/debug/bkey_types.rs | 20 ++++++ src/commands/debug/mod.rs | 62 +++++++++++++++++- src/commands/debug/parser.rs | 58 +++++++++++++++-- 5 files changed, 255 insertions(+), 8 deletions(-) diff --git a/c_src/cmd_debug.c b/c_src/cmd_debug.c index 73ba3995..9eec1ddb 100644 --- a/c_src/cmd_debug.c +++ b/c_src/cmd_debug.c @@ -3,9 +3,41 @@ #include "libbcachefs/bkey_types.h" #include "libbcachefs/btree_update.h" #include "libbcachefs/printbuf.h" +#include "libbcachefs/inode.h" #include "cmds.h" +void write_field(enum bkey_update_op op, void *base, u64 size, u64 offset, + u64 value) +{ +#define x(_size, _bits) \ + u##_bits *field_##_bits; \ + case _size: \ + field_##_bits = (u##_bits *) ((u8 *)base + offset); \ + switch (op) { \ + case BKEY_CMD_SET: \ + *field_##_bits = (u##_bits) value; \ + break; \ + case BKEY_CMD_ADD: \ + *field_##_bits += (u##_bits) value; \ + break; \ + default: \ + fprintf(stderr, "invalid operation: %d\n", op); \ + break; \ + } \ + break; + + switch (size) { + x(1, 8) + x(2, 16) + x(4, 32) + x(8, 64) + default: + fprintf(stderr, "invalid size: %llu\n", size); + } +#undef x +} + int cmd_dump_bkey(struct bch_fs *c, enum btree_id id, struct bpos pos) { struct btree_trans *trans = bch2_trans_get(c); @@ -36,3 +68,78 @@ out: return ret; } + +int cmd_update_bkey(struct bch_fs *c, struct bkey_update u, struct bpos pos) +{ + struct btree_trans *trans = bch2_trans_get(c); + struct btree_iter iter = { NULL }; + struct printbuf buf = PRINTBUF; + int ret = 0; + + set_bit(BCH_FS_no_invalid_checks, &c->flags); + + bch2_trans_iter_init(trans, &iter, u.id, pos, BTREE_ITER_all_snapshots); + + struct bkey_s_c k = bch2_btree_iter_peek(&iter); + if ((ret = bkey_err(k))) { + fprintf(stderr, "bch2_btree_iter_peek() failed: %s\n", bch2_err_str(ret)); + goto out; + } + if (!k.k || !bpos_eq(pos, k.k->p)) { + bch2_bpos_to_text(&buf, pos); + printf("no key at pos %s\n", buf.buf); + ret = 1; + goto out; + } + + if (u.inode_unpacked) { + if (k.k->type != KEY_TYPE_inode_v2 && k.k->type != KEY_TYPE_inode_v3) { + fprintf(stderr, "Wanted bch_inode_unpacked, got 'bch_%s'\n", + bch2_bkey_types[k.k->type]); + goto out; + } + + struct bch_inode_unpacked inode; + ret = bch2_inode_unpack(k, &inode); + if (ret != 0) { + fprintf(stderr, "bch2_inode_unpack() failed: %s\n", bch2_err_str(ret)); + goto out; + } + + write_field(u.op, &inode, u.size, u.offset, u.value); + + ret = bch2_inode_write(trans, &iter, &inode) ?: + bch2_trans_commit(trans, NULL, NULL, 0); + if (ret != 0) { + fprintf(stderr, "inode update failed: %s\n", bch2_err_str(ret)); + } + } else { + if (u.bkey != k.k->type) { + fprintf(stderr, "Wanted type 'bch_%s', got type 'bch_%s'\n", + bch2_bkey_types[u.bkey], bch2_bkey_types[k.k->type]); + goto out; + } + + bch2_trans_unlock(trans); + + struct bkey_i *n = bch2_bkey_make_mut_noupdate(trans, k); + if ((ret = PTR_ERR_OR_ZERO(n))) { + fprintf(stderr, "bch2_bkey_make_mut_noupdate() failed: %s\n", + bch2_err_str(ret)); + goto out; + } + + write_field(u.op, &n->v, u.size, u.offset, u.value); + + ret = bch2_btree_insert(c, u.id, n, NULL, 0, 0); + if (ret != 0) { + fprintf(stderr, "bch2_btree_insert() failed: %s\n", bch2_err_str(ret)); + } + } + +out: + bch2_trans_iter_exit(trans, &iter); + bch2_trans_put(trans); + + return ret; +} diff --git a/c_src/cmds.h b/c_src/cmds.h index d55a1440..8e744e3f 100644 --- a/c_src/cmds.h +++ b/c_src/cmds.h @@ -9,6 +9,21 @@ #include "tools-util.h" +enum bkey_update_op { + BKEY_CMD_SET, + BKEY_CMD_ADD, +}; + +struct bkey_update { + enum btree_id id; + enum bch_bkey_type bkey; + enum bkey_update_op op; + bool inode_unpacked; + u64 offset; + u64 size; + u64 value; +}; + int cmd_format(int argc, char *argv[]); int cmd_show_super(int argc, char *argv[]); int cmd_reset_counters(int argc, char *argv[]); @@ -55,6 +70,7 @@ int cmd_subvolume_snapshot(int argc, char *argv[]); int cmd_fusemount(int argc, char *argv[]); int cmd_dump_bkey(struct bch_fs *, enum btree_id, struct bpos); +int cmd_update_bkey(struct bch_fs *, struct bkey_update, struct bpos); void bcachefs_usage(void); int device_cmds(int argc, char *argv[]); diff --git a/src/commands/debug/bkey_types.rs b/src/commands/debug/bkey_types.rs index 680d4410..9bb39669 100644 --- a/src/commands/debug/bkey_types.rs +++ b/src/commands/debug/bkey_types.rs @@ -13,6 +13,15 @@ impl BkeyTypes { pub fn new() -> Self { BkeyTypes(Vec::new()) } + + /// Given a struct name and a member name, return the size and offset of + /// the member within the struct, or None if it does not exist. + pub fn get_member_layout(&self, outer: &str, member: &str) -> Option<(u64, u64)> { + self.0 + .iter() + .find(|i| i.name == *outer) + .and_then(|i| i.member_layout(member)) + } } impl std::fmt::Display for BkeyTypes { @@ -39,6 +48,17 @@ pub struct BchStruct { pub members: Vec<BchMember>, } +impl BchStruct { + /// Given a struct member name, return the size and offset of the member + /// within its parent, or None if there is no member with the given name. + pub fn member_layout(&self, name: &str) -> Option<(u64, u64)> { + self.members + .iter() + .find(|i| i.name == *name) + .map(|i| (i.size, i.offset)) + } +} + /// The representation of a struct member. We need its name, size, and offset /// within the parent struct. #[derive(Debug)] diff --git a/src/commands/debug/mod.rs b/src/commands/debug/mod.rs index 31b23c7e..813b80d5 100644 --- a/src/commands/debug/mod.rs +++ b/src/commands/debug/mod.rs @@ -8,7 +8,7 @@ use bch_bindgen::fs::Fs; mod bkey_types; mod parser; -use bch_bindgen::c::bpos; +use bch_bindgen::c::{bkey_update_op, bpos}; use anyhow::Result; @@ -25,6 +25,7 @@ pub struct Cli { #[derive(Debug)] enum DebugCommand { Dump(DumpCommand), + Update(UpdateCommand), } #[derive(Debug)] @@ -33,6 +34,57 @@ struct DumpCommand { bpos: bpos, } +#[derive(Debug)] +struct UpdateCommand { + btree: String, + bpos: bpos, + bkey: String, + field: String, + op: bkey_update_op, + value: u64, +} + +fn update(fs: &Fs, type_list: &bkey_types::BkeyTypes, cmd: UpdateCommand) { + let id: bch_bindgen::c::btree_id = match cmd.btree.parse() { + Ok(b) => b, + Err(_) => { + eprintln!("unknown btree '{}'", cmd.btree); + return; + } + }; + + let (bkey, inode_unpacked) = if cmd.bkey == "bch_inode_unpacked" { + (c::bch_bkey_type::KEY_TYPE_MAX, true) + } else { + let bkey = match cmd.bkey["bch_".len()..].parse() { + Ok(k) => k, + Err(_) => { + eprintln!("unknown bkey type '{}'", cmd.bkey); + return; + } + }; + + (bkey, false) + }; + + if let Some((size, offset)) = type_list.get_member_layout(&cmd.bkey, &cmd.field) { + let update = c::bkey_update { + id, + bkey, + op: cmd.op, + inode_unpacked, + offset, + size, + value: cmd.value, + }; + unsafe { + c::cmd_update_bkey(fs.raw, update, cmd.bpos); + } + } else { + println!("unknown field '{}'", cmd.field); + } +} + fn dump(fs: &Fs, cmd: DumpCommand) { let id: bch_bindgen::c::btree_id = match cmd.btree.parse() { Ok(b) => b, @@ -50,13 +102,15 @@ fn dump(fs: &Fs, cmd: DumpCommand) { fn usage() { println!("Usage:"); println!(" dump <btree_type> <bpos>"); + println!(" update <btree_type> <bpos> <bkey_type>.<field>=<value>"); } -fn do_command(fs: &Fs, cmd: &str) -> i32 { +fn do_command(fs: &Fs, type_list: &bkey_types::BkeyTypes, cmd: &str) -> i32 { match parser::parse_command(cmd) { Ok(cmd) => { match cmd { DebugCommand::Dump(cmd) => dump(fs, cmd), + DebugCommand::Update(cmd) => update(fs, type_list, cmd), }; 0 @@ -78,6 +132,7 @@ pub fn debug(argv: Vec<String>) -> Result<()> { let opt = Cli::parse_from(argv); let fs_opts: bcachefs::bch_opts = Default::default(); + let type_list = bkey_types::get_bkey_type_info()?; if let Some(cmd) = opt.command { return match parser::parse_command(&cmd) { @@ -85,6 +140,7 @@ pub fn debug(argv: Vec<String>) -> Result<()> { let fs = Fs::open(&opt.devices, fs_opts)?; match cmd { DebugCommand::Dump(cmd) => dump(&fs, cmd), + DebugCommand::Update(cmd) => update(&fs, &type_list, cmd), } Ok(()) @@ -103,7 +159,7 @@ pub fn debug(argv: Vec<String>) -> Result<()> { prompt(); let stdin = std::io::stdin(); for line in stdin.lock().lines() { - do_command(&fs, &line.unwrap()); + do_command(&fs, &type_list, &line.unwrap()); prompt(); } diff --git a/src/commands/debug/parser.rs b/src/commands/debug/parser.rs index fa036447..19adeba7 100644 --- a/src/commands/debug/parser.rs +++ b/src/commands/debug/parser.rs @@ -1,13 +1,13 @@ use nom::branch::alt; -use nom::bytes::complete::tag; +use nom::bytes::complete::{tag, take_while}; use nom::character::complete::{alpha1, char, space1, u32, u64}; use nom::combinator::{all_consuming, value}; use nom::sequence::tuple; use nom::IResult; -use bch_bindgen::c::bpos; +use bch_bindgen::c::{bkey_update_op, bpos}; -use crate::commands::debug::{DebugCommand, DumpCommand}; +use crate::commands::debug::{DebugCommand, DumpCommand, UpdateCommand}; fn parse_bpos(input: &str) -> IResult<&str, bpos> { let (input, (inode, _, offset, _, snapshot)) = tuple(( @@ -41,10 +41,58 @@ fn parse_dump_cmd(input: &str) -> IResult<&str, DebugCommand> { )) } +fn bkey_name(input: &str) -> IResult<&str, &str> { + take_while(|c: char| c.is_alphanumeric() || c == '_')(input) +} + +fn field_name(input: &str) -> IResult<&str, &str> { + take_while(|c: char| c.is_alphanumeric() || c == '_' || c == '.')(input) +} + +fn bkey_op(input: &str) -> IResult<&str, bkey_update_op> { + let (input, op) = alt((tag("="), tag("+=")))(input)?; + match op { + "=" => Ok((input, bkey_update_op::BKEY_CMD_SET)), + "+=" => Ok((input, bkey_update_op::BKEY_CMD_ADD)), + _ => unreachable!(), + } +} + +fn parse_update_cmd(input: &str) -> IResult<&str, DebugCommand> { + let (input, (_, btree, _, bpos, _, bkey, _, field, op, value)) = all_consuming(tuple(( + space1, + alpha1, + space1, + parse_bpos, + space1, + bkey_name, + char('.'), + field_name, + bkey_op, + u64, + )))(input)?; + + Ok(( + input, + DebugCommand::Update(UpdateCommand { + btree: btree.to_string(), + bpos, + bkey: bkey.to_string(), + field: field.to_string(), + op, + value, + }), + )) +} + fn parse_command_inner(input: &str) -> IResult<&str, DebugCommand> { - let (input, _) = tag("dump")(input)?; + let (input, cmd) = alt((tag("dump"), tag("update")))(input)?; - parse_dump_cmd(input) + match cmd { + "dump" => parse_dump_cmd(input), + "update" => parse_update_cmd(input), + _ => unreachable!(), + } } /// Given an input string, tries to parse it into a valid -- 2.45.2