public inbox for linux-bcachefs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] chore(util): minimal Rust build example
@ 2023-05-03  6:53 TruongSinh Tran-Nguyen
  2023-05-09 17:47 ` Kent Overstreet
  0 siblings, 1 reply; 28+ messages in thread
From: TruongSinh Tran-Nguyen @ 2023-05-03  6:53 UTC (permalink / raw)
  To: linux-bcachefs; +Cc: TruongSinh Tran-Nguyen

This is a minimal demonstration of how to use Rust in kernel side
of bcachefs. Kernel side rust does not have Rust `std`, thus
formating is a bit more challenging than doing from user space

This minimal demo implement `uuid_le_cmp`, but eventually,
in Rust, we probably do not even need to use it, just simply
`u1.b != u2.b` is good enough. Furthermore, we might consider
(even in C code) to use uuid_t instead of uuid_le

Signed-off-by: TruongSinh Tran-Nguyen <i@truongsinh.pro>
---
 fs/bcachefs/Makefile              |  3 +++
 fs/bcachefs/util.h                |  8 ++++++++
 fs/bcachefs/util_rust.rs          | 17 +++++++++++++++++
 scripts/generate_rust_analyzer.py |  2 +-
 4 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 fs/bcachefs/util_rust.rs

diff --git a/fs/bcachefs/Makefile b/fs/bcachefs/Makefile
index a71956048a027..972976aac6921 100644
--- a/fs/bcachefs/Makefile
+++ b/fs/bcachefs/Makefile
@@ -72,3 +72,6 @@ bcachefs-y		:=	\
 	xattr.o
 
 bcachefs-$(CONFIG_BCACHEFS_POSIX_ACL) += acl.o
+
+bcachefs-$(CONFIG_RUST) +=	\
+	util_rust.o	\
diff --git a/fs/bcachefs/util.h b/fs/bcachefs/util.h
index 9eb86c58e5509..6e87baee760a9 100644
--- a/fs/bcachefs/util.h
+++ b/fs/bcachefs/util.h
@@ -839,9 +839,17 @@ static inline int u8_cmp(u8 l, u8 r)
 
 #include <linux/uuid.h>
 
+#ifdef CONFIG_RUST
+int deprecated_uuid_le_cmp(const uuid_le *b1, const uuid_le *b2);
+#endif
+
 static inline int uuid_le_cmp(const uuid_le u1, const uuid_le u2)
 {
+	#ifdef CONFIG_RUST
+	return deprecated_uuid_le_cmp(&u1, &u2);
+	#else
 	return memcmp(&u1, &u2, sizeof(uuid_le));
+	#endif
 }
 
 #endif /* _BCACHEFS_UTIL_H */
diff --git a/fs/bcachefs/util_rust.rs b/fs/bcachefs/util_rust.rs
new file mode 100644
index 0000000000000..23338c29384c0
--- /dev/null
+++ b/fs/bcachefs/util_rust.rs
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: MIT */
+
+#[repr(C)]
+pub struct UuidLe {
+  b: [u8; 16],
+}
+
+// this function is prefixed `deprecated` because it uses
+// uuid_le from /usr/include/linux/uuid.h, which itself is
+// deprecated in favor of uuid_t from include/linux/uuid.h
+// it also does not exactly matches memcmp
+#[no_mangle]
+pub extern "C" fn deprecated_uuid_le_cmp( u1 : &UuidLe, u2 : &UuidLe) -> bool
+{
+  // pr_info!("using deprecated_uuid_le_cmp");
+  return u1.b != u2.b;
+}
diff --git a/scripts/generate_rust_analyzer.py b/scripts/generate_rust_analyzer.py
index 946e250c1b2a6..d258d10bb6e31 100755
--- a/scripts/generate_rust_analyzer.py
+++ b/scripts/generate_rust_analyzer.py
@@ -98,7 +98,7 @@ def generate_crates(srctree, objtree, sysroot_src):
     # Then, the rest outside of `rust/`.
     #
     # We explicitly mention the top-level folders we want to cover.
-    for folder in ("samples", "drivers"):
+    for folder in ("samples", "drivers", 'fs'):
         for path in (srctree / folder).rglob("*.rs"):
             logging.info("Checking %s", path)
             name = path.name.replace(".rs", "")
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2023-05-12 14:26 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-03  6:53 [PATCH] chore(util): minimal Rust build example TruongSinh Tran-Nguyen
2023-05-09 17:47 ` Kent Overstreet
2023-05-09 17:57   ` TruongSinh Tran-Nguyen
2023-05-09 18:12     ` Kent Overstreet
2023-05-09 18:41       ` Wedson Almeida Filho
2023-05-09 20:57         ` TruongSinh Tran-Nguyen
2023-05-10  3:10           ` TruongSinh Tran-Nguyen
2023-05-10 17:59             ` Miguel Ojeda
2023-05-10 18:21               ` TruongSinh Tran-Nguyen
2023-05-11 23:37                 ` Miguel Ojeda
2023-05-11 23:54                   ` TruongSinh Tran-Nguyen
2023-05-12  0:02                     ` Miguel Ojeda
2023-05-12  3:31                     ` Kent Overstreet
2023-05-12  4:16                       ` TruongSinh Tran-Nguyen
2023-05-12  4:18                         ` Kent Overstreet
2023-05-12  4:39                           ` TruongSinh Tran-Nguyen
2023-05-12  5:33                             ` Kent Overstreet
2023-05-12 14:26                               ` TruongSinh Tran-Nguyen
2023-05-10 18:38               ` Kent Overstreet
2023-05-11 23:39                 ` Miguel Ojeda
2023-05-11 16:16             ` Björn Roy Baron
2023-05-11 16:46               ` TruongSinh Tran-Nguyen
2023-05-11 17:05                 ` Björn Roy Baron
2023-05-11 23:10                   ` TruongSinh Tran-Nguyen
2023-05-09 20:52     ` Andreas Hindborg
2023-05-10 17:56   ` Miguel Ojeda
2023-05-10 18:44     ` Kent Overstreet
2023-05-11 23:37       ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox