On Wed, Feb 7, 2024 at 12:59 AM Thomas Bertschinger
<[email protected]> wrote:
>
> This patch uses the bcachefs bindgen framework to introduce a Rust
> implementation of the module entry and exit functions. With this change,
> bcachefs is now a Rust kernel module (that calls C functions to do most
> of its work).
>
> This is only if CONFIG_BCACHEFS_RUST is defined; the C implementation of
> the module init and exit code is left around so that bcachefs remains
> usable in kernels compiled without Rust support.
>
> Signed-off-by: Thomas Bertschinger <[email protected]>
> ---
>
> [...]
>
> diff --git a/fs/bcachefs/bcachefs_module.rs b/fs/bcachefs/bcachefs_module.rs
> new file mode 100644
> index 000000000000..8db2de8139bc
> --- /dev/null
> +++ b/fs/bcachefs/bcachefs_module.rs
> @@ -0,0 +1,66 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +//! bcachefs
> +//!
> +//! Rust kernel module for bcachefs.
> +
> +pub mod bindings;
> +
> +use kernel::prelude::*;
> +
> +use crate::bindings::*;
Most in-tree code uses the `bindings::` prefix when referencing C to
make extern calls clear, rather than doing the glob import. I think we
probably want to keep this style.
> +module! {
> + type: Bcachefs,
> + name: "bcachefs",
> + author: "Kent Overstreet <[email protected]>",
> + description: "bcachefs filesystem",
> + license: "GPL",
> +}
> +
> +struct Bcachefs;
> +
> +impl kernel::Module for Bcachefs {
> + #[link_section = ".init.text"]
Is the attribute still needed if this lands?
https://lore.kernel.org/rust-for-linux/[email protected]/T/#u
> + fn init(_module: &'static ThisModule) -> Result<Self> {
> + // SAFETY: this block registers the bcachefs services with the
> kernel. After succesful
> + // registration, all such services are guaranteed by the kernel to
> exist as long as the
> + // driver is loaded. In the event of any failure in the
> registration, all registered
> + // services are unregistered.
> + unsafe {
> + bch2_bkey_pack_test();
> +
> + if bch2_kset_init() != 0
> + || bch2_btree_key_cache_init() != 0
> + || bch2_chardev_init() != 0
> + || bch2_vfs_init() != 0
> + || bch2_debug_init() != 0
> + {
> + __drop();
> + return Err(ENOMEM);
Do these init functions ever return anything more descriptive than
ENOMEM that should be returned instead? Maybe not worth changing if
the next phase will let you `?` the results.
> + }
> + }
> +
> + Ok(Bcachefs)
> + }
> +}
> +
> +fn __drop() {
Something like `drop_impl` or `unregister` is probably more in line
with naming, dunder is really only used when something
unstable/generated needs to be made public.
> [...]
Cool to see the ball rolling on this :)
- Trevor