From b6f30fd188b25d048e92e3b25abb28fbeb3f189f Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Tue, 28 Feb 2023 02:11:05 -0500 Subject: [PATCH] rust: BkeyValC Now we have a rust-style enum for key types Signed-off-by: Kent Overstreet --- rust-src/bch_bindgen/build.rs | 11 +-- rust-src/bch_bindgen/src/bkey.rs | 114 ++++++++++++++++++++++++++++++ rust-src/bch_bindgen/src/btree.rs | 37 +--------- rust-src/bch_bindgen/src/lib.rs | 1 + 4 files changed, 121 insertions(+), 42 deletions(-) create mode 100644 rust-src/bch_bindgen/src/bkey.rs diff --git a/rust-src/bch_bindgen/build.rs b/rust-src/bch_bindgen/build.rs index 74497b81..8d9986f8 100644 --- a/rust-src/bch_bindgen/build.rs +++ b/rust-src/bch_bindgen/build.rs @@ -52,17 +52,12 @@ fn main() { .allowlist_var("bch.*") .allowlist_var("__BTREE_ITER.*") .allowlist_var("BTREE_ITER.*") - .allowlist_var("POS_MIN") - .allowlist_var("POS_MAX") - .allowlist_var("SPOS_MAX") .blocklist_item("bch2_bkey_ops") - .allowlist_type("bch_kdf_types") - .allowlist_type("bch_sb_field_.*") - .allowlist_type("bch_encrypted_key") + .allowlist_type("bch_.*") .allowlist_type("nonce") - .allowlist_type("bch_errcode") - .allowlist_function("bch2_err_str") + .no_debug("bch_replicas_padded") .newtype_enum("bch_kdf_types") + .rustified_enum("bch_key_types") .opaque_type("gendisk") .opaque_type("gc_stripe") .opaque_type("open_bucket.*") diff --git a/rust-src/bch_bindgen/src/bkey.rs b/rust-src/bch_bindgen/src/bkey.rs new file mode 100644 index 00000000..6735f298 --- /dev/null +++ b/rust-src/bch_bindgen/src/bkey.rs @@ -0,0 +1,114 @@ +#![allow(non_camel_case_types)] + +use crate::c; +use crate::fs::Fs; +use std::ffi::CStr; +use std::fmt; +use std::mem::transmute; + +pub struct BkeySC<'a> { + pub k: &'a c::bkey, + pub v: &'a c::bch_val, +} + +pub enum BkeyValC<'a> { + deleted, + whiteout, + error, + cookie(&'a c::bch_cookie), + hash_whiteout(&'a c::bch_hash_whiteout), + btree_ptr(&'a c::bch_btree_ptr), + extent(&'a c::bch_extent), + reservation(&'a c::bch_reservation), + inode(&'a c::bch_inode), + inode_generation(&'a c::bch_inode_generation), + dirent(&'a c::bch_dirent), + xattr(&'a c::bch_xattr), + alloc(&'a c::bch_alloc), + quota(&'a c::bch_quota), + stripe(&'a c::bch_stripe), + reflink_p(&'a c::bch_reflink_p), + reflink_v(&'a c::bch_reflink_v), + inline_data(&'a c::bch_inline_data), + btree_ptr_v2(&'a c::bch_btree_ptr_v2), + indirect_inline_data(&'a c::bch_indirect_inline_data), + alloc_v2(&'a c::bch_alloc_v2), + subvolume(&'a c::bch_subvolume), + snapshot(&'a c::bch_snapshot), + inode_v2(&'a c::bch_inode_v2), + alloc_v3(&'a c::bch_alloc_v3), + set, + lru(&'a c::bch_lru), + alloc_v4(&'a c::bch_alloc_v4), + backpointer(&'a c::bch_backpointer), + inode_v3(&'a c::bch_inode_v3), + bucket_gens(&'a c::bch_bucket_gens), +} + +impl<'a, 'b> BkeySC<'a> { + unsafe fn to_raw(&self) -> c::bkey_s_c { + c::bkey_s_c { k: self.k, v: self.v } + } + + pub fn to_text(&'a self, fs: &'b Fs) -> BkeySCToText<'a, 'b> { + BkeySCToText { k: self, fs } + } + + pub fn v(&'a self) -> BkeyValC { + let ty: c::bch_bkey_type = unsafe { transmute(self.k.type_ as u32) }; + + use c::bch_bkey_type::*; + use BkeyValC::*; + match ty { + KEY_TYPE_deleted => deleted, + KEY_TYPE_whiteout => whiteout, + KEY_TYPE_error => error, + KEY_TYPE_cookie => cookie(unsafe { transmute(self.v) }), + KEY_TYPE_hash_whiteout => hash_whiteout(unsafe { transmute(self.v) }), + KEY_TYPE_btree_ptr => btree_ptr(unsafe { transmute(self.v) }), + KEY_TYPE_extent => extent(unsafe { transmute(self.v) }), + KEY_TYPE_reservation => reservation(unsafe { transmute(self.v) }), + KEY_TYPE_inode => inode(unsafe { transmute(self.v) }), + KEY_TYPE_inode_generation => inode_generation(unsafe { transmute(self.v) }), + KEY_TYPE_dirent => dirent(unsafe { transmute(self.v) }), + KEY_TYPE_xattr => xattr(unsafe { transmute(self.v) }), + KEY_TYPE_alloc => alloc(unsafe { transmute(self.v) }), + KEY_TYPE_quota => quota(unsafe { transmute(self.v) }), + KEY_TYPE_stripe => stripe(unsafe { transmute(self.v) }), + KEY_TYPE_reflink_p => reflink_p(unsafe { transmute(self.v) }), + KEY_TYPE_reflink_v => reflink_v(unsafe { transmute(self.v) }), + KEY_TYPE_inline_data => inline_data(unsafe { transmute(self.v) }), + KEY_TYPE_btree_ptr_v2 => btree_ptr_v2(unsafe { transmute(self.v) }), + KEY_TYPE_indirect_inline_data => indirect_inline_data(unsafe { transmute(self.v) }), + KEY_TYPE_alloc_v2 => alloc_v2(unsafe { transmute(self.v) }), + KEY_TYPE_subvolume => subvolume(unsafe { transmute(self.v) }), + KEY_TYPE_snapshot => snapshot(unsafe { transmute(self.v) }), + KEY_TYPE_inode_v2 => inode_v2(unsafe { transmute(self.v) }), + KEY_TYPE_alloc_v3 => inode_v3(unsafe { transmute(self.v) }), + KEY_TYPE_set => set, + KEY_TYPE_lru => lru(unsafe { transmute(self.v) }), + KEY_TYPE_alloc_v4 => alloc_v4(unsafe { transmute(self.v) }), + KEY_TYPE_backpointer => backpointer(unsafe { transmute(self.v) }), + KEY_TYPE_inode_v3 => inode_v3(unsafe { transmute(self.v) }), + KEY_TYPE_bucket_gens => bucket_gens(unsafe { transmute(self.v) }), + KEY_TYPE_MAX => unreachable!(), + } + } +} + +pub struct BkeySCToText<'a, 'b> { + k: &'a BkeySC<'a>, + fs: &'b Fs, +} + +impl<'a, 'b> fmt::Display for BkeySCToText<'a, 'b> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut buf = c::printbuf::new(); + + unsafe { c::bch2_bkey_val_to_text(&mut buf, self.fs.raw, self.k.to_raw()) }; + + let s = unsafe { CStr::from_ptr(buf.buf) }; + let s = s.to_str().unwrap(); + write!(f, "{}", s) + } +} diff --git a/rust-src/bch_bindgen/src/btree.rs b/rust-src/bch_bindgen/src/btree.rs index 4b5e86d9..92606be1 100644 --- a/rust-src/bch_bindgen/src/btree.rs +++ b/rust-src/bch_bindgen/src/btree.rs @@ -1,13 +1,14 @@ +#![allow(non_snake_case)] + use crate::SPOS_MAX; use crate::c; +use crate::bkey::BkeySC; use crate::fs::Fs; use crate::errcode::{bch_errcode, errptr_to_result_c}; use std::marker::PhantomData; use std::mem::MaybeUninit; use std::ptr; use bitflags::bitflags; -use std::ffi::CStr; -use std::fmt; pub struct BtreeTrans { raw: c::btree_trans, @@ -105,35 +106,3 @@ impl<'a> Drop for BtreeIter<'a> { unsafe { c::bch2_trans_iter_exit(self.raw.trans, &mut self.raw) } } } - -pub struct BkeySC<'a> { - pub k: &'a c::bkey, - pub v: &'a c::bch_val, -} - -impl<'a, 'b> BkeySC<'a> { - unsafe fn to_raw(&self) -> c::bkey_s_c { - c::bkey_s_c { k: self.k, v: self.v } - } - - pub fn to_text(&'a self, fs: &'b Fs) -> BkeySCToText<'a, 'b> { - BkeySCToText { k: self, fs } - } -} - -pub struct BkeySCToText<'a, 'b> { - k: &'a BkeySC<'a>, - fs: &'b Fs, -} - -impl<'a, 'b> fmt::Display for BkeySCToText<'a, 'b> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut buf = c::printbuf::new(); - - unsafe { c::bch2_bkey_val_to_text(&mut buf, self.fs.raw, self.k.to_raw()) }; - - let s = unsafe { CStr::from_ptr(buf.buf) }; - let s = s.to_str().unwrap(); - write!(f, "{}", s) - } -} diff --git a/rust-src/bch_bindgen/src/lib.rs b/rust-src/bch_bindgen/src/lib.rs index e853edbb..bce150a4 100644 --- a/rust-src/bch_bindgen/src/lib.rs +++ b/rust-src/bch_bindgen/src/lib.rs @@ -1,5 +1,6 @@ pub mod bcachefs; pub mod btree; +pub mod bkey; pub mod errcode; pub mod keyutils; pub mod log;