]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
chore(rust): add opt_get! and expose some FMODE_* as Rust const
authorTruongSinh Tran-Nguyen <i@truongsinh.pro>
Thu, 27 Apr 2023 18:02:00 +0000 (11:02 -0700)
committerKent Overstreet <kent.overstreet@linux.dev>
Thu, 27 Apr 2023 23:23:17 +0000 (19:23 -0400)
In an effort to rewrite `bch2_read_super` from C to Rust,
it is neccessary to have `opt_get!` macro defined, and some
FMODE_* consts (defined as macro in `include/linux/blkdev.h`)
defined as Rust const.

Bindgen is currently unable to exapnd C functional macro [1],
this this commit use the workaround as introduced in [2].

[1] https://github.com/rust-lang/rust-bindgen/issues/753
[2] https://github.com/rust-lang/rust-bindgen/issues/753#issuecomment-608546390

Signed-off-by: TruongSinh Tran-Nguyen <i@truongsinh.pro>
rust-src/bch_bindgen/build.rs
rust-src/bch_bindgen/src/libbcachefs_wrapper.h
rust-src/bch_bindgen/src/opts.rs

index f426316f42fff1237fbcb67d6a857706f8e80f77..92ec3cefc38a030647dd619add8a65d00a5848ab 100644 (file)
@@ -1,3 +1,12 @@
+
+#[derive(Debug)]
+pub struct Fix753 {}
+impl bindgen::callbacks::ParseCallbacks for Fix753 {
+    fn item_name(&self, original_item_name: &str) -> Option<String> {
+        Some(original_item_name.trim_start_matches("Fix753_").to_owned())
+    }
+}
+
 fn main() {
     use std::path::PathBuf;
 
@@ -49,6 +58,7 @@ fn main() {
         .blocklist_type("srcu_struct")
         .allowlist_var("BCH_.*")
         .allowlist_var("KEY_SPEC_.*")
+        .allowlist_var("Fix753_FMODE_.*")
         .allowlist_var("bch.*")
         .allowlist_var("__BTREE_ITER.*")
         .allowlist_var("BTREE_ITER.*")
@@ -69,6 +79,7 @@ fn main() {
         .no_partialeq("bkey")
         .no_partialeq("bpos")
         .generate_inline_functions(true)
+        .parse_callbacks(Box::new(Fix753 {}))
         .generate()
         .expect("BindGen Generation Failiure: [libbcachefs_wrapper]");
     bindings
index d1ebf4b639454ca326069a3cfb788154ea75ef37..e7bcfcfb22574f6ea61a1c4980334ba31e624e31 100644 (file)
 #include "../libbcachefs.h"
 #include "../crypto.h"
 #include "../include/linux/bio.h"
+#include "../include/linux/blkdev.h"
 
+
+#define MARK_FIX_753(req_name) const fmode_t Fix753_##req_name = req_name;
+
+MARK_FIX_753(FMODE_READ);
+MARK_FIX_753(FMODE_WRITE);
+MARK_FIX_753(FMODE_EXCL);
\ No newline at end of file
index e2261993b98f8e8690db28d8bea9a247e3bf159b..d38d469ce654f2a64d1a945d87fa45bb8c936b11 100644 (file)
@@ -3,7 +3,33 @@ macro_rules! opt_set {
     ($opts:ident, $n:ident, $v:expr) => {
         bch_bindgen::paste! {
             $opts.$n = $v;
-            $opts.[<set_ $n _defined>](1);
+            $opts.[<set_ $n _defined>](1)
         }
-    }
+    };
+}
+
+#[macro_export]
+macro_rules! opt_defined {
+    ($opts:ident, $n:ident) => {
+        bch_bindgen::paste! {
+            $opts.[< $n _defined>]()
+        }
+    };
+}
+
+#[macro_export]
+macro_rules! opt_get {
+    ($opts:ident, $n:ident) => {
+        if bch_bindgen::opt_defined!($opts, $n) == 0 {
+            bch_bindgen::paste! {
+                unsafe {
+                    bch_bindgen::bcachefs::bch2_opts_default.$n
+                }
+            }
+        } else {
+            bch_bindgen::paste! {
+                $opts.$n
+            }
+        }
+    };
 }