Format with rustfmt

Note that we're using struct/enum align options, which require nightly.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2024-05-26 20:38:08 -04:00
parent 5b216318b8
commit aa985e6a44
15 changed files with 418 additions and 264 deletions

View File

@ -29,8 +29,7 @@ fn main() {
.to_string(),
)
.clang_args(
urcu
.include_paths
urcu.include_paths
.iter()
.map(|p| format!("-I{}", p.display())),
)

View File

@ -1,3 +0,0 @@
# Default settings, i.e. idiomatic rust
edition = "2021"
newline_style = "Unix"

View File

@ -1,8 +1,8 @@
#![allow(non_camel_case_types)]
use crate::btree::BtreeIter;
use crate::c;
use crate::fs::Fs;
use crate::btree::BtreeIter;
use crate::printbuf_to_formatter;
use std::fmt;
use std::marker::PhantomData;
@ -11,7 +11,7 @@ use std::mem::transmute;
pub struct BkeySC<'a> {
pub k: &'a c::bkey,
pub v: &'a c::bch_val,
pub(crate) iter: PhantomData<&'a mut BtreeIter<'a>>
pub(crate) iter: PhantomData<&'a mut BtreeIter<'a>>,
}
pub enum BkeyValC<'a> {
@ -53,7 +53,10 @@ pub enum BkeyValC<'a> {
impl<'a, 'b> BkeySC<'a> {
unsafe fn to_raw(&self) -> c::bkey_s_c {
c::bkey_s_c { k: self.k, v: self.v }
c::bkey_s_c {
k: self.k,
v: self.v,
}
}
pub fn to_text(&'a self, fs: &'b Fs) -> BkeySCToText<'a, 'b> {
@ -109,7 +112,11 @@ impl<'a, 'b> BkeySC<'a> {
impl<'a> From<&'a c::bkey_i> for BkeySC<'a> {
fn from(k: &'a c::bkey_i) -> Self {
BkeySC { k: &k.k, v: &k.v, iter: PhantomData }
BkeySC {
k: &k.k,
v: &k.v,
iter: PhantomData,
}
}
}
@ -121,7 +128,9 @@ pub struct BkeySCToText<'a, 'b> {
impl<'a, 'b> fmt::Display for BkeySCToText<'a, 'b> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
unsafe {
printbuf_to_formatter(f, |buf| c::bch2_bkey_val_to_text(buf, self.fs.raw, self.k.to_raw()))
printbuf_to_formatter(f, |buf| {
c::bch2_bkey_val_to_text(buf, self.fs.raw, self.k.to_raw())
})
}
}
}

View File

@ -1,23 +1,26 @@
use crate::SPOS_MAX;
use crate::c;
use crate::bkey::BkeySC;
use crate::fs::Fs;
use crate::c;
use crate::errcode::{bch_errcode, errptr_to_result_c};
use crate::fs::Fs;
use crate::printbuf_to_formatter;
use crate::SPOS_MAX;
use bitflags::bitflags;
use std::fmt;
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use bitflags::bitflags;
pub struct BtreeTrans<'f> {
raw: *mut c::btree_trans,
fs: PhantomData<&'f Fs>
fs: PhantomData<&'f Fs>,
}
impl<'f> BtreeTrans<'f> {
pub fn new(fs: &'f Fs) -> BtreeTrans {
unsafe {
BtreeTrans { raw: &mut *c::__bch2_trans_get(fs.raw, 0), fs: PhantomData }
BtreeTrans {
raw: &mut *c::__bch2_trans_get(fs.raw, 0),
fs: PhantomData,
}
}
}
}
@ -54,7 +57,12 @@ pub struct BtreeIter<'t> {
}
impl<'t> BtreeIter<'t> {
pub fn new(trans: &'t BtreeTrans<'t>, btree: c::btree_id, pos: c::bpos, flags: BtreeIterFlags) -> BtreeIter<'t> {
pub fn new(
trans: &'t BtreeTrans<'t>,
btree: c::btree_id,
pos: c::bpos,
flags: BtreeIterFlags,
) -> BtreeIter<'t> {
unsafe {
let mut iter: MaybeUninit<c::btree_iter> = MaybeUninit::uninit();
@ -63,17 +71,30 @@ impl<'t> BtreeIter<'t> {
iter.as_mut_ptr(),
btree,
pos,
flags.bits as u32);
flags.bits as u32,
);
BtreeIter { raw: iter.assume_init(), trans: PhantomData }
BtreeIter {
raw: iter.assume_init(),
trans: PhantomData,
}
}
}
pub fn peek_upto<'i>(&'i mut self, end: c::bpos) -> Result<Option<BkeySC>, bch_errcode> {
unsafe {
let k = c::bch2_btree_iter_peek_upto(&mut self.raw, end);
errptr_to_result_c(k.k)
.map(|_| if !k.k.is_null() { Some(BkeySC { k: &*k.k, v: &*k.v, iter: PhantomData }) } else { None } )
errptr_to_result_c(k.k).map(|_| {
if !k.k.is_null() {
Some(BkeySC {
k: &*k.k,
v: &*k.v,
iter: PhantomData,
})
} else {
None
}
})
}
}
@ -85,8 +106,17 @@ impl<'t> BtreeIter<'t> {
unsafe {
let k = c::bch2_btree_iter_peek_and_restart_outlined(&mut self.raw);
errptr_to_result_c(k.k)
.map(|_| if !k.k.is_null() { Some(BkeySC{ k: &*k.k, v: &*k.v, iter: PhantomData }) } else { None } )
errptr_to_result_c(k.k).map(|_| {
if !k.k.is_null() {
Some(BkeySC {
k: &*k.k,
v: &*k.v,
iter: PhantomData,
})
} else {
None
}
})
}
}
@ -109,12 +139,14 @@ pub struct BtreeNodeIter<'t> {
}
impl<'t> BtreeNodeIter<'t> {
pub fn new(trans: &'t BtreeTrans<'t>,
pub fn new(
trans: &'t BtreeTrans<'t>,
btree: c::btree_id,
pos: c::bpos,
locks_want: u32,
depth: u32,
flags: BtreeIterFlags) -> BtreeNodeIter {
flags: BtreeIterFlags,
) -> BtreeNodeIter {
unsafe {
let mut iter: MaybeUninit<c::btree_iter> = MaybeUninit::uninit();
c::bch2_trans_node_iter_init(
@ -124,9 +156,13 @@ impl<'t> BtreeNodeIter<'t> {
pos,
locks_want,
depth,
flags.bits as u32);
flags.bits as u32,
);
BtreeNodeIter { raw: iter.assume_init(), trans: PhantomData }
BtreeNodeIter {
raw: iter.assume_init(),
trans: PhantomData,
}
}
}
@ -181,7 +217,9 @@ pub struct BtreeNodeToText<'b, 'f> {
impl<'b, 'f> fmt::Display for BtreeNodeToText<'b, 'f> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
printbuf_to_formatter(f, |buf| unsafe { c::bch2_btree_node_to_text(buf, self.fs.raw, self.b) })
printbuf_to_formatter(f, |buf| unsafe {
c::bch2_btree_node_to_text(buf, self.fs.raw, self.b)
})
}
}
@ -192,6 +230,8 @@ pub struct BtreeNodeOndiskToText<'b, 'f> {
impl<'b, 'f> fmt::Display for BtreeNodeOndiskToText<'b, 'f> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
printbuf_to_formatter(f, |buf| unsafe { c::bch2_btree_node_ondisk_to_text(buf, self.fs.raw, self.b) })
printbuf_to_formatter(f, |buf| unsafe {
c::bch2_btree_node_ondisk_to_text(buf, self.fs.raw, self.b)
})
}
}

View File

@ -1,8 +1,8 @@
use crate::c;
use crate::errcode::{bch_errcode, errptr_to_result};
use std::ffi::CString;
use std::os::unix::ffi::OsStrExt;
use std::path::PathBuf;
use crate::c;
use crate::errcode::{bch_errcode, errptr_to_result};
pub struct Fs {
pub raw: *mut c::bch_fs,
@ -10,13 +10,14 @@ pub struct Fs {
impl Fs {
pub fn open(devs: &Vec<PathBuf>, opts: c::bch_opts) -> Result<Fs, bch_errcode> {
let devs: Vec<_> = devs.iter()
let devs: Vec<_> = devs
.iter()
.map(|i| CString::new(i.as_os_str().as_bytes()).unwrap().into_raw())
.collect();
let ret = unsafe { c::bch2_fs_open(devs[..].as_ptr(), devs.len() as u32, opts) };
errptr_to_result(ret).map(|fs| Fs { raw: fs})
errptr_to_result(ret).map(|fs| Fs { raw: fs })
}
}

View File

@ -1,11 +1,11 @@
pub mod bcachefs;
pub mod btree;
pub mod bkey;
pub mod btree;
pub mod errcode;
pub mod keyutils;
pub mod sb_io;
pub mod fs;
pub mod keyutils;
pub mod opts;
pub mod sb_io;
pub use paste::paste;
pub mod c {
@ -15,7 +15,11 @@ pub mod c {
use c::bpos as Bpos;
pub const fn spos(inode: u64, offset: u64, snapshot: u32) -> Bpos {
Bpos { inode, offset, snapshot }
Bpos {
inode,
offset,
snapshot,
}
}
pub const fn pos(inode: u64, offset: u64) -> Bpos {
@ -51,7 +55,8 @@ impl Ord for Bpos {
let l_snapshot = self.snapshot;
let r_snapshot = other.snapshot;
l_inode.cmp(&r_inode)
l_inode
.cmp(&r_inode)
.then(l_offset.cmp(&r_offset))
.then(l_snapshot.cmp(&r_snapshot))
}
@ -68,9 +73,9 @@ impl fmt::Display for c::btree_id {
}
}
use std::str::FromStr;
use std::ffi::CString;
use std::{path::Path,os::unix::ffi::OsStrExt};
use std::str::FromStr;
use std::{os::unix::ffi::OsStrExt, path::Path};
pub fn path_to_cstr<P: AsRef<Path>>(p: P) -> CString {
CString::new(p.as_ref().as_os_str().as_bytes()).unwrap()
@ -95,8 +100,7 @@ impl fmt::Display for BchToolsErr {
}
}
impl Error for BchToolsErr {
}
impl Error for BchToolsErr {}
impl FromStr for c::btree_id {
type Err = BchToolsErr;
@ -105,7 +109,13 @@ impl FromStr for c::btree_id {
let s = CString::new(s).unwrap();
let p = s.as_ptr();
let v = unsafe {c::match_string(c::__bch2_btree_ids[..].as_ptr(), (-(1 as isize)) as usize, p)};
let v = unsafe {
c::match_string(
c::__bch2_btree_ids[..].as_ptr(),
(-(1 as isize)) as usize,
p,
)
};
if v >= 0 {
Ok(unsafe { std::mem::transmute(v) })
} else {
@ -121,7 +131,9 @@ impl FromStr for c::bch_bkey_type {
let s = CString::new(s).unwrap();
let p = s.as_ptr();
let v = unsafe {c::match_string(c::bch2_bkey_types[..].as_ptr(), (-(1 as isize)) as usize, p)};
let v = unsafe {
c::match_string(c::bch2_bkey_types[..].as_ptr(), (-(1 as isize)) as usize, p)
};
if v >= 0 {
Ok(unsafe { std::mem::transmute(v) })
} else {
@ -182,12 +194,18 @@ impl FromStr for c::bpos {
let off: u64 = off_str.parse().map_err(|_| BchToolsErr::InvalidBpos)?;
let snp: u32 = snp_str.map(|s| s.parse().ok()).flatten().unwrap_or(0);
Ok(c::bpos { inode: ino, offset: off, snapshot: snp })
Ok(c::bpos {
inode: ino,
offset: off,
snapshot: snp,
})
}
}
pub fn printbuf_to_formatter<F>(f: &mut fmt::Formatter<'_>, func: F) -> fmt::Result
where F: Fn(*mut c::printbuf) {
where
F: Fn(*mut c::printbuf),
{
let mut buf = c::printbuf::new();
func(&mut buf);

View File

@ -1,8 +1,8 @@
use anyhow::anyhow;
use crate::path_to_cstr;
use crate::bcachefs;
use crate::bcachefs::*;
use crate::errcode::bch_errcode;
use crate::path_to_cstr;
use anyhow::anyhow;
pub fn read_super_opts(
path: &std::path::Path,
@ -11,7 +11,8 @@ pub fn read_super_opts(
let path = path_to_cstr(path);
let mut sb = std::mem::MaybeUninit::zeroed();
let ret = unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
let ret =
unsafe { crate::bcachefs::bch2_read_super(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
if ret != 0 {
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };
@ -33,7 +34,9 @@ pub fn read_super_silent(
let path = path_to_cstr(path);
let mut sb = std::mem::MaybeUninit::zeroed();
let ret = unsafe { crate::bcachefs::bch2_read_super_silent(path.as_ptr(), &mut opts, sb.as_mut_ptr()) };
let ret = unsafe {
crate::bcachefs::bch2_read_super_silent(path.as_ptr(), &mut opts, sb.as_mut_ptr())
};
if ret != 0 {
let err: bch_errcode = unsafe { ::std::mem::transmute(ret) };

View File

@ -1,3 +1,5 @@
# Default settings, i.e. idiomatic rust
edition = "2021"
newline_style = "Unix"
enum_discrim_align_threshold = 20
struct_field_align_threshold = 20

View File

@ -1,11 +1,11 @@
mod wrappers;
mod commands;
mod key;
mod wrappers;
use std::ffi::{c_char, CString};
use commands::logger::SimpleLogger;
use bch_bindgen::c;
use commands::logger::SimpleLogger;
#[derive(Debug)]
pub struct ErrnoError(pub errno::Errno);
@ -25,10 +25,7 @@ fn handle_c_command(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
let argc: i32 = argv.len().try_into().unwrap();
let argv: Vec<_> = argv
.into_iter()
.map(|s| CString::new(s).unwrap())
.collect();
let argv: Vec<_> = argv.into_iter().map(|s| CString::new(s).unwrap()).collect();
let mut argv = argv
.into_iter()
.map(|s| Box::into_raw(s.into_boxed_c_str()) as *mut c_char)
@ -41,7 +38,7 @@ fn handle_c_command(mut argv: Vec<String>, symlink_cmd: Option<&str>) -> i32 {
"--help" => {
c::bcachefs_usage();
0
},
}
"data" => c::data_cmds(argc, argv),
"device" => c::device_cmds(argc, argv),
"dump" => c::cmd_dump(argc, argv),

View File

@ -1,20 +1,23 @@
use log::{error};
use bch_bindgen::bcachefs;
use bch_bindgen::opt_set;
use bch_bindgen::fs::Fs;
use bch_bindgen::bkey::BkeySC;
use bch_bindgen::btree::BtreeTrans;
use bch_bindgen::btree::BtreeIter;
use bch_bindgen::btree::BtreeNodeIter;
use bch_bindgen::btree::BtreeIterFlags;
use clap::{Parser};
use bch_bindgen::btree::BtreeNodeIter;
use bch_bindgen::btree::BtreeTrans;
use bch_bindgen::fs::Fs;
use bch_bindgen::opt_set;
use clap::Parser;
use log::error;
use std::io::{stdout, IsTerminal};
fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
let mut iter = BtreeIter::new(&trans, opt.btree, opt.start,
BtreeIterFlags::ALL_SNAPSHOTS|
BtreeIterFlags::PREFETCH);
let mut iter = BtreeIter::new(
&trans,
opt.btree,
opt.start,
BtreeIterFlags::ALL_SNAPSHOTS | BtreeIterFlags::PREFETCH,
);
while let Some(k) = iter.peek_and_restart()? {
if k.k.p > opt.end {
@ -37,9 +40,14 @@ fn list_keys(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
fn list_btree_formats(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
let mut iter = BtreeNodeIter::new(&trans, opt.btree, opt.start,
0, opt.level,
BtreeIterFlags::PREFETCH);
let mut iter = BtreeNodeIter::new(
&trans,
opt.btree,
opt.start,
0,
opt.level,
BtreeIterFlags::PREFETCH,
);
while let Some(b) = iter.peek_and_restart()? {
if b.key.k.p > opt.end {
@ -55,9 +63,14 @@ fn list_btree_formats(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
fn list_btree_nodes(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
let mut iter = BtreeNodeIter::new(&trans, opt.btree, opt.start,
0, opt.level,
BtreeIterFlags::PREFETCH);
let mut iter = BtreeNodeIter::new(
&trans,
opt.btree,
opt.start,
0,
opt.level,
BtreeIterFlags::PREFETCH,
);
while let Some(b) = iter.peek_and_restart()? {
if b.key.k.p > opt.end {
@ -73,9 +86,14 @@ fn list_btree_nodes(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
fn list_nodes_ondisk(fs: &Fs, opt: Cli) -> anyhow::Result<()> {
let trans = BtreeTrans::new(fs);
let mut iter = BtreeNodeIter::new(&trans, opt.btree, opt.start,
0, opt.level,
BtreeIterFlags::PREFETCH);
let mut iter = BtreeNodeIter::new(
&trans,
opt.btree,
opt.start,
0,
opt.level,
BtreeIterFlags::PREFETCH,
);
while let Some(b) = iter.peek_and_restart()? {
if b.key.k.p > opt.end {
@ -105,22 +123,22 @@ pub struct Cli {
btree: bcachefs::btree_id,
/// Bkey type to list
#[arg(short='k', long)]
#[arg(short = 'k', long)]
bkey_type: Option<bcachefs::bch_bkey_type>,
/// Btree depth to descend to (0 == leaves)
#[arg(short, long, default_value_t=0)]
#[arg(short, long, default_value_t = 0)]
level: u32,
/// Start position to list from
#[arg(short, long, default_value="POS_MIN")]
#[arg(short, long, default_value = "POS_MIN")]
start: bcachefs::bpos,
/// End position
#[arg(short, long, default_value="SPOS_MAX")]
#[arg(short, long, default_value = "SPOS_MAX")]
end: bcachefs::bpos,
#[arg(short, long, default_value="keys")]
#[arg(short, long, default_value = "keys")]
mode: Mode,
/// Check (fsck) the filesystem first
@ -147,10 +165,18 @@ fn cmd_list_inner(opt: Cli) -> anyhow::Result<()> {
opt_set!(fs_opts, norecovery, 1);
opt_set!(fs_opts, degraded, 1);
opt_set!(fs_opts, very_degraded, 1);
opt_set!(fs_opts, errors, bcachefs::bch_error_actions::BCH_ON_ERROR_continue as u8);
opt_set!(
fs_opts,
errors,
bcachefs::bch_error_actions::BCH_ON_ERROR_continue as u8
);
if opt.fsck {
opt_set!(fs_opts, fix_errors, bcachefs::fsck_err_opts::FSCK_FIX_yes as u8);
opt_set!(
fs_opts,
fix_errors,
bcachefs::fsck_err_opts::FSCK_FIX_yes as u8
);
opt_set!(fs_opts, norecovery, 0);
}

View File

@ -1,14 +1,14 @@
use clap::Subcommand;
pub mod completions;
pub mod list;
pub mod logger;
pub mod mount;
pub mod list;
pub mod completions;
pub mod subvolume;
pub use mount::mount;
pub use list::list;
pub use completions::completions;
pub use list::list;
pub use mount::mount;
pub use subvolume::subvolume;
#[derive(clap::Parser, Debug)]

View File

@ -1,14 +1,14 @@
use bch_bindgen::{path_to_cstr, bcachefs, bcachefs::bch_sb_handle, opt_set};
use log::{info, debug, error, LevelFilter};
use std::collections::HashMap;
use clap::Parser;
use uuid::Uuid;
use std::io::{stdout, IsTerminal};
use std::path::{Path, PathBuf};
use std::{fs, str, env};
use crate::key;
use crate::key::UnlockPolicy;
use std::ffi::{CString, c_char, c_void};
use bch_bindgen::{bcachefs, bcachefs::bch_sb_handle, opt_set, path_to_cstr};
use clap::Parser;
use log::{debug, error, info, LevelFilter};
use std::collections::HashMap;
use std::ffi::{c_char, c_void, CString};
use std::io::{stdout, IsTerminal};
use std::path::{Path, PathBuf};
use std::{env, fs, str};
use uuid::Uuid;
fn mount_inner(
src: String,
@ -17,7 +17,6 @@ fn mount_inner(
mountflags: libc::c_ulong,
data: Option<String>,
) -> anyhow::Result<()> {
// bind the CStrings to keep them alive
let src = CString::new(src)?;
let target = path_to_cstr(target);
@ -243,7 +242,12 @@ pub struct Cli {
/// "fail" - don't ask for password, fail if filesystem is encrypted;
/// "wait" - wait for password to become available before mounting;
/// "ask" - prompt the user for password;
#[arg(short = 'k', long = "key_location", default_value = "ask", verbatim_doc_comment)]
#[arg(
short = 'k',
long = "key_location",
default_value = "ask",
verbatim_doc_comment
)]
unlock_policy: UnlockPolicy,
/// Device, or UUID=\<UUID\>
@ -358,7 +362,10 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
{
// First by password_file, if available
let fallback_to_unlock_policy = if let Some(passphrase_file) = &opt.passphrase_file {
match key::read_from_passphrase_file(&block_devices_to_mount[0], passphrase_file.as_path()) {
match key::read_from_passphrase_file(
&block_devices_to_mount[0],
passphrase_file.as_path(),
) {
Ok(()) => {
// Decryption succeeded
false
@ -391,8 +398,7 @@ fn cmd_mount_inner(opt: Cli) -> anyhow::Result<()> {
} else {
info!(
"would mount with params: device: {}, options: {}",
devices,
&opt.options
devices, &opt.options
);
}

View File

@ -17,13 +17,13 @@ enum Subcommands {
#[command(visible_aliases = ["new"])]
Create {
/// Paths
targets: Vec<PathBuf>
targets: Vec<PathBuf>,
},
#[command(visible_aliases = ["del"])]
Delete {
/// Path
target: PathBuf
target: PathBuf,
},
#[command(allow_missing_positional = true, visible_aliases = ["snap"])]
@ -32,8 +32,8 @@ enum Subcommands {
#[arg(long, short)]
read_only: bool,
source: Option<PathBuf>,
dest: PathBuf
}
dest: PathBuf,
},
}
pub fn subvolume(argv: Vec<String>) -> i32 {
@ -44,24 +44,42 @@ pub fn subvolume(argv: Vec<String>) -> i32 {
for target in targets {
if let Some(dirname) = target.parent() {
let fs = unsafe { BcachefsHandle::open(dirname) };
fs.create_subvolume(target).expect("Failed to create the subvolume");
fs.create_subvolume(target)
.expect("Failed to create the subvolume");
}
}
}
,
Subcommands::Delete { target } => {
if let Some(dirname) = target.parent() {
let fs = unsafe { BcachefsHandle::open(dirname) };
fs.delete_subvolume(target).expect("Failed to delete the subvolume");
fs.delete_subvolume(target)
.expect("Failed to delete the subvolume");
}
},
Subcommands::Snapshot { read_only, source, dest } => {
}
Subcommands::Snapshot {
read_only,
source,
dest,
} => {
if let Some(dirname) = dest.parent() {
let dot = PathBuf::from(".");
let dir = if dirname.as_os_str().is_empty() { &dot } else { dirname };
let dir = if dirname.as_os_str().is_empty() {
&dot
} else {
dirname
};
let fs = unsafe { BcachefsHandle::open(dir) };
fs.snapshot_subvolume(if read_only { BCH_SUBVOL_SNAPSHOT_RO } else { 0x0 }, source, dest).expect("Failed to snapshot the subvolume");
fs.snapshot_subvolume(
if read_only {
BCH_SUBVOL_SNAPSHOT_RO
} else {
0x0
},
source,
dest,
)
.expect("Failed to snapshot the subvolume");
}
}
}

View File

@ -1,10 +1,13 @@
use std::{fmt, fs, io::{stdin, IsTerminal}};
use std::{
fmt, fs,
io::{stdin, IsTerminal},
};
use log::info;
use bch_bindgen::bcachefs::bch_sb_handle;
use clap::builder::PossibleValue;
use crate::c_str;
use anyhow::anyhow;
use bch_bindgen::bcachefs::bch_sb_handle;
use clap::builder::PossibleValue;
use log::info;
#[derive(Clone, Debug)]
pub enum UnlockPolicy {
@ -18,7 +21,7 @@ impl std::str::FromStr for UnlockPolicy {
type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<Self> {
match s {
""|"none" => Ok(UnlockPolicy::None),
"" | "none" => Ok(UnlockPolicy::None),
"fail" => Ok(UnlockPolicy::Fail),
"wait" => Ok(UnlockPolicy::Wait),
"ask" => Ok(UnlockPolicy::Ask),
@ -152,18 +155,32 @@ fn unlock_master_key(sb: &bch_sb_handle, passphrase: &str) -> anyhow::Result<()>
}
}
pub fn read_from_passphrase_file(block_device: &bch_sb_handle, passphrase_file: &std::path::Path) -> anyhow::Result<()> {
pub fn read_from_passphrase_file(
block_device: &bch_sb_handle,
passphrase_file: &std::path::Path,
) -> anyhow::Result<()> {
// Attempts to unlock the master key by password_file
// Return true if unlock was successful, false otherwise
info!("Attempting to unlock master key for filesystem {}, using password from file {}", block_device.sb().uuid(), passphrase_file.display());
info!(
"Attempting to unlock master key for filesystem {}, using password from file {}",
block_device.sb().uuid(),
passphrase_file.display()
);
// Read the contents of the password_file into a string
let passphrase = fs::read_to_string(passphrase_file)?;
// Call decrypt_master_key with the read string
unlock_master_key(block_device, &passphrase)
}
pub fn apply_key_unlocking_policy(block_device: &bch_sb_handle, unlock_policy: UnlockPolicy) -> anyhow::Result<()> {
info!("Attempting to unlock master key for filesystem {}, using unlock policy {}", block_device.sb().uuid(), unlock_policy);
pub fn apply_key_unlocking_policy(
block_device: &bch_sb_handle,
unlock_policy: UnlockPolicy,
) -> anyhow::Result<()> {
info!(
"Attempting to unlock master key for filesystem {}, using unlock policy {}",
block_device.sb().uuid(),
unlock_policy
);
match unlock_policy {
UnlockPolicy::Fail => Err(anyhow!("no passphrase available")),
UnlockPolicy::Wait => Ok(wait_for_unlock(&block_device.sb().uuid())?),

View File

@ -1,13 +1,16 @@
use std::path::Path;
use bch_bindgen::c::{bchfs_handle, BCH_IOCTL_SUBVOLUME_CREATE, BCH_IOCTL_SUBVOLUME_DESTROY, bch_ioctl_subvolume, bcache_fs_open, BCH_SUBVOL_SNAPSHOT_CREATE, bcache_fs_close};
use bch_bindgen::c::{
bcache_fs_close, bcache_fs_open, bch_ioctl_subvolume, bchfs_handle, BCH_IOCTL_SUBVOLUME_CREATE,
BCH_IOCTL_SUBVOLUME_DESTROY, BCH_SUBVOL_SNAPSHOT_CREATE,
};
use bch_bindgen::path_to_cstr;
use errno::Errno;
/// A handle to a bcachefs filesystem
/// This can be used to send [`libc::ioctl`] to the underlying filesystem.
pub(crate) struct BcachefsHandle {
inner: bchfs_handle
inner: bchfs_handle,
}
impl BcachefsHandle {
@ -16,7 +19,7 @@ impl BcachefsHandle {
pub(crate) unsafe fn open<P: AsRef<Path>>(path: P) -> Self {
let path = path_to_cstr(path);
Self {
inner: bcache_fs_open(path.as_ptr())
inner: bcache_fs_open(path.as_ptr()),
}
}
}
@ -39,14 +42,18 @@ pub enum BcachefsIoctlPayload {
impl From<&BcachefsIoctlPayload> for *const libc::c_void {
fn from(value: &BcachefsIoctlPayload) -> Self {
match value {
BcachefsIoctlPayload::Subvolume(p) => p as *const _ as *const libc::c_void
BcachefsIoctlPayload::Subvolume(p) => p as *const _ as *const libc::c_void,
}
}
}
impl BcachefsHandle {
/// Type-safe [`libc::ioctl`] for bcachefs filesystems
pub fn ioctl(&self, request: BcachefsIoctl, payload: &BcachefsIoctlPayload) -> Result<(), Errno> {
pub fn ioctl(
&self,
request: BcachefsIoctl,
payload: &BcachefsIoctlPayload,
) -> Result<(), Errno> {
let payload_ptr: *const libc::c_void = payload.into();
let ret = unsafe { libc::ioctl(self.inner.ioctl_fd, request as libc::Ioctl, payload_ptr) };
@ -61,33 +68,46 @@ impl BcachefsHandle {
/// at the given path
pub fn create_subvolume<P: AsRef<Path>>(&self, dst: P) -> Result<(), Errno> {
let dst = path_to_cstr(dst);
self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
self.ioctl(
BcachefsIoctl::SubvolumeCreate,
&BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
dirfd: libc::AT_FDCWD as u32,
mode: 0o777,
dst_ptr: dst.as_ptr() as u64,
..Default::default()
}))
}),
)
}
/// Delete the subvolume at the given path
/// for this bcachefs filesystem
pub fn delete_subvolume<P: AsRef<Path>>(&self, dst: P) -> Result<(), Errno> {
let dst = path_to_cstr(dst);
self.ioctl(BcachefsIoctl::SubvolumeDestroy, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
self.ioctl(
BcachefsIoctl::SubvolumeDestroy,
&BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
dirfd: libc::AT_FDCWD as u32,
mode: 0o777,
dst_ptr: dst.as_ptr() as u64,
..Default::default()
}))
}),
)
}
/// Snapshot a subvolume for this bcachefs filesystem
/// at the given path
pub fn snapshot_subvolume<P: AsRef<Path>>(&self, extra_flags: u32, src: Option<P>, dst: P) -> Result<(), Errno> {
pub fn snapshot_subvolume<P: AsRef<Path>>(
&self,
extra_flags: u32,
src: Option<P>,
dst: P,
) -> Result<(), Errno> {
let src = src.map(|src| path_to_cstr(src));
let dst = path_to_cstr(dst);
let res = self.ioctl(BcachefsIoctl::SubvolumeCreate, &BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
let res = self.ioctl(
BcachefsIoctl::SubvolumeCreate,
&BcachefsIoctlPayload::Subvolume(bch_ioctl_subvolume {
flags: BCH_SUBVOL_SNAPSHOT_CREATE | extra_flags,
dirfd: libc::AT_FDCWD as u32,
mode: 0o777,
@ -95,7 +115,8 @@ impl BcachefsHandle {
//src_ptr: if let Some(src) = src { src.as_ptr() } else { std::ptr::null() } as u64,
dst_ptr: dst.as_ptr() as u64,
..Default::default()
}));
}),
);
drop(src);
drop(dst);