Skip to content

Commit

Permalink
introduce new "debug update" command
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
bertschingert committed Jul 14, 2024
1 parent 294fce6 commit 9fcce42
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 5 deletions.
107 changes: 107 additions & 0 deletions c_src/cmd_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -36,3 +68,78 @@ int cmd_dump_bkey(struct bch_fs *c, enum btree_id id, struct bpos pos)

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;
}
16 changes: 16 additions & 0 deletions c_src/cmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -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[]);
Expand Down Expand Up @@ -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[]);
Expand Down
57 changes: 56 additions & 1 deletion src/commands/debug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,6 +25,7 @@ pub struct Cli {
#[derive(Debug)]
enum DebugCommand {
Dump(DumpCommand),
Update(UpdateCommand),
}

#[derive(Debug)]
Expand All @@ -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,
Expand All @@ -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, 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
Expand Down Expand Up @@ -86,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(())
Expand Down
56 changes: 52 additions & 4 deletions src/commands/debug/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ 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((
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9fcce42

Please sign in to comment.