summaryrefslogtreecommitdiff
path: root/src/commands/debug/mod.rs
blob: c9786089830636f73e21ffd8ac6cd8686c4d0db4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use clap::Parser;
use std::io::{BufRead, Write};

use bch_bindgen::bcachefs;
use bch_bindgen::c;
use bch_bindgen::fs::Fs;

mod bkey_types;
mod parser;

use bch_bindgen::c::bpos;

/// Debug a bcachefs filesystem.
#[derive(Parser, Debug)]
pub struct Cli {
    #[arg(required(true))]
    devices: Vec<std::path::PathBuf>,

    #[arg(short, long)]
    command: Option<String>,
}

#[derive(Debug)]
enum DebugCommand {
    Dump(DumpCommand),
    Update(UpdateCommand),
}

#[derive(Debug)]
struct DumpCommand {
    btree: String,
    bpos: bpos,
}

#[derive(Debug)]
struct UpdateCommand {
    btree: String,
    bpos: bpos,
    bkey: String,
    field: String,
    value: u64,
}

fn update(fs: &Fs, type_list: &bkey_types::BkeyTypes, cmd: UpdateCommand) {
    let id: bch_bindgen::c::btree_id = cmd.btree.parse().expect("no such btree");

    let (bkey, inode_bkey) = if cmd.bkey == "bch_inode_unpacked" {
        (c::bch_bkey_type::KEY_TYPE_MAX, true)
    } else {
        (cmd.bkey["bch_".len()..].parse().expect("no such bkey"), false)
    };

    if let Some((size, offset)) = type_list.get_member_layout(&cmd.bkey, &cmd.field) {
        let update = c::bkey_update {
            id,
            bkey,
            inode_bkey,
            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 = cmd.btree.parse().expect("no such btree");

    unsafe {
        c::cmd_dump_bkey(fs.raw, id, cmd.bpos);
    }
}

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
        }
        Err(e) => {
            println!("{e}");
            usage();

            1
        }
    }
}

pub fn debug(argv: Vec<String>) -> i32 {
    fn prompt() {
        print!("bcachefs> ");
        std::io::stdout().flush().unwrap();
    }

    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) {
            Ok(cmd) => {
                let fs = match Fs::open(&opt.devices, fs_opts) {
                    Ok(fs) => fs,
                    Err(_) => {
                        return 1;
                    }
                };
                match cmd {
                    DebugCommand::Dump(cmd) => dump(&fs, cmd),
                    DebugCommand::Update(cmd) => update(&fs, &type_list, cmd),
                }

                0
            }
            Err(e) => {
                println!("{e}");
                usage();

                1
            }
        };
    }

    let fs = match Fs::open(&opt.devices, fs_opts) {
        Ok(fs) => fs,
        Err(_) => {
            return 1;
        }
    };

    prompt();
    let stdin = std::io::stdin();
    for line in stdin.lock().lines() {
        do_command(&fs, &type_list, &line.unwrap());
        prompt();
    }

    0
}

pub fn list_bkeys() -> i32 {
    print!("{}", bkey_types::get_bkey_type_info());

    0
}