From: Wedson Almeida Filho <wedsonaf@gmail.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
Matthew Wilcox <willy@infradead.org>
Cc: Kent Overstreet <kent.overstreet@gmail.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-fsdevel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Wedson Almeida Filho <walmeida@microsoft.com>
Subject: [RFC PATCH 16/19] rust: fs: allow file systems backed by a block device
Date: Wed, 18 Oct 2023 09:25:15 -0300 [thread overview]
Message-ID: <20231018122518.128049-17-wedsonaf@gmail.com> (raw)
In-Reply-To: <20231018122518.128049-1-wedsonaf@gmail.com>
From: Wedson Almeida Filho <walmeida@microsoft.com>
Allow Rust file systems that are backed by block devices (in addition to
in-memory ones).
Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
---
rust/bindings/bindings_helper.h | 1 +
rust/helpers.c | 14 +++
rust/kernel/fs.rs | 177 +++++++++++++++++++++++++++++---
rust/kernel/fs/buffer.rs | 1 -
4 files changed, 180 insertions(+), 13 deletions(-)
diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index d328375f7cb7..8403f13d4d48 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -7,6 +7,7 @@
*/
#include <kunit/test.h>
+#include <linux/bio.h>
#include <linux/buffer_head.h>
#include <linux/errname.h>
#include <linux/fs.h>
diff --git a/rust/helpers.c b/rust/helpers.c
index a5393c6b93f2..bc19f3b7b93e 100644
--- a/rust/helpers.c
+++ b/rust/helpers.c
@@ -21,6 +21,7 @@
*/
#include <kunit/test-bug.h>
+#include <linux/blkdev.h>
#include <linux/buffer_head.h>
#include <linux/bug.h>
#include <linux/build_bug.h>
@@ -252,6 +253,13 @@ unsigned int rust_helper_MKDEV(unsigned int major, unsigned int minor)
EXPORT_SYMBOL_GPL(rust_helper_MKDEV);
#ifdef CONFIG_BUFFER_HEAD
+struct buffer_head *rust_helper_sb_bread(struct super_block *sb,
+ sector_t block)
+{
+ return sb_bread(sb, block);
+}
+EXPORT_SYMBOL_GPL(rust_helper_sb_bread);
+
void rust_helper_get_bh(struct buffer_head *bh)
{
get_bh(bh);
@@ -265,6 +273,12 @@ void rust_helper_put_bh(struct buffer_head *bh)
EXPORT_SYMBOL_GPL(rust_helper_put_bh);
#endif
+sector_t rust_helper_bdev_nr_sectors(struct block_device *bdev)
+{
+ return bdev_nr_sectors(bdev);
+}
+EXPORT_SYMBOL_GPL(rust_helper_bdev_nr_sectors);
+
/*
* `bindgen` binds the C `size_t` type as the Rust `usize` type, so we can
* use it in contexts where Rust expects a `usize` like slice (array) indices.
diff --git a/rust/kernel/fs.rs b/rust/kernel/fs.rs
index 4f04cb1d3c6f..b1ad5c110dbb 100644
--- a/rust/kernel/fs.rs
+++ b/rust/kernel/fs.rs
@@ -7,11 +7,9 @@
//! C headers: [`include/linux/fs.h`](../../include/linux/fs.h)
use crate::error::{code::*, from_result, to_result, Error, Result};
-use crate::types::{ARef, AlwaysRefCounted, Either, ForeignOwnable, Opaque};
-use crate::{
- bindings, folio::LockedFolio, init::PinInit, str::CStr, time::Timespec, try_pin_init,
- ThisModule,
-};
+use crate::folio::{LockedFolio, UniqueFolio};
+use crate::types::{ARef, AlwaysRefCounted, Either, ForeignOwnable, Opaque, ScopeGuard};
+use crate::{bindings, init::PinInit, str::CStr, time::Timespec, try_pin_init, ThisModule};
use core::{marker::PhantomData, marker::PhantomPinned, mem::ManuallyDrop, pin::Pin, ptr};
use macros::{pin_data, pinned_drop};
@@ -21,6 +19,17 @@
/// Maximum size of an inode.
pub const MAX_LFS_FILESIZE: i64 = bindings::MAX_LFS_FILESIZE;
+/// Type of superblock keying.
+///
+/// It determines how C's `fs_context_operations::get_tree` is implemented.
+pub enum Super {
+ /// Multiple independent superblocks may exist.
+ Independent,
+
+ /// Uses a block device.
+ BlockDev,
+}
+
/// A file system type.
pub trait FileSystem {
/// Data associated with each file system instance (super-block).
@@ -29,6 +38,9 @@ pub trait FileSystem {
/// The name of the file system type.
const NAME: &'static CStr;
+ /// Determines how superblocks for this file system type are keyed.
+ const SUPER_TYPE: Super = Super::Independent;
+
/// Returns the parameters to initialise a super block.
fn super_params(sb: &NewSuperBlock<Self>) -> Result<SuperParams<Self::Data>>;
@@ -181,7 +193,9 @@ pub fn new<T: FileSystem + ?Sized>(module: &'static ThisModule) -> impl PinInit<
fs.name = T::NAME.as_char_ptr();
fs.init_fs_context = Some(Self::init_fs_context_callback::<T>);
fs.kill_sb = Some(Self::kill_sb_callback::<T>);
- fs.fs_flags = 0;
+ fs.fs_flags = if let Super::BlockDev = T::SUPER_TYPE {
+ bindings::FS_REQUIRES_DEV as i32
+ } else { 0 };
// SAFETY: Pointers stored in `fs` are static so will live for as long as the
// registration is active (it is undone in `drop`).
@@ -204,9 +218,16 @@ pub fn new<T: FileSystem + ?Sized>(module: &'static ThisModule) -> impl PinInit<
unsafe extern "C" fn kill_sb_callback<T: FileSystem + ?Sized>(
sb_ptr: *mut bindings::super_block,
) {
- // SAFETY: In `get_tree_callback` we always call `get_tree_nodev`, so `kill_anon_super` is
- // the appropriate function to call for cleanup.
- unsafe { bindings::kill_anon_super(sb_ptr) };
+ match T::SUPER_TYPE {
+ // SAFETY: In `get_tree_callback` we always call `get_tree_bdev` for
+ // `Super::BlockDev`, so `kill_block_super` is the appropriate function to call
+ // for cleanup.
+ Super::BlockDev => unsafe { bindings::kill_block_super(sb_ptr) },
+ // SAFETY: In `get_tree_callback` we always call `get_tree_nodev` for
+ // `Super::Independent`, so `kill_anon_super` is the appropriate function to call
+ // for cleanup.
+ Super::Independent => unsafe { bindings::kill_anon_super(sb_ptr) },
+ }
// SAFETY: The C API contract guarantees that `sb_ptr` is valid for read.
let ptr = unsafe { (*sb_ptr).s_fs_info };
@@ -479,6 +500,65 @@ pub fn get_or_create_inode(&self, ino: Ino) -> Result<Either<ARef<INode<T>>, New
})))
}
}
+
+ /// Reads a block from the block device.
+ #[cfg(CONFIG_BUFFER_HEAD)]
+ pub fn bread(&self, block: u64) -> Result<ARef<buffer::Head>> {
+ // Fail requests for non-blockdev file systems. This is a compile-time check.
+ match T::SUPER_TYPE {
+ Super::BlockDev => {}
+ _ => return Err(EIO),
+ }
+
+ // SAFETY: This function is only valid after the `NeedsInit` typestate, so the block size
+ // is known and the superblock can be used to read blocks.
+ let ptr =
+ ptr::NonNull::new(unsafe { bindings::sb_bread(self.0.get(), block) }).ok_or(EIO)?;
+ // SAFETY: `sb_bread` returns a referenced buffer head. Ownership of the increment is
+ // passed to the `ARef` instance.
+ Ok(unsafe { ARef::from_raw(ptr.cast()) })
+ }
+
+ /// Reads `size` bytes starting from `offset` bytes.
+ ///
+ /// Returns an iterator that returns slices based on blocks.
+ #[cfg(CONFIG_BUFFER_HEAD)]
+ pub fn read(
+ &self,
+ offset: u64,
+ size: u64,
+ ) -> Result<impl Iterator<Item = Result<buffer::View>> + '_> {
+ struct BlockIter<'a, T: FileSystem + ?Sized> {
+ sb: &'a SuperBlock<T>,
+ next_offset: u64,
+ end: u64,
+ }
+ impl<'a, T: FileSystem + ?Sized> Iterator for BlockIter<'a, T> {
+ type Item = Result<buffer::View>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.next_offset >= self.end {
+ return None;
+ }
+
+ // SAFETY: The superblock is valid and has had its block size initialised.
+ let block_size = unsafe { (*self.sb.0.get()).s_blocksize };
+ let bh = match self.sb.bread(self.next_offset / block_size) {
+ Ok(bh) => bh,
+ Err(e) => return Some(Err(e)),
+ };
+ let boffset = self.next_offset & (block_size - 1);
+ let bsize = core::cmp::min(self.end - self.next_offset, block_size - boffset);
+ self.next_offset += bsize;
+ Some(Ok(buffer::View::new(bh, boffset as usize, bsize as usize)))
+ }
+ }
+ Ok(BlockIter {
+ sb: self,
+ next_offset: offset,
+ end: offset.checked_add(size).ok_or(ERANGE)?,
+ })
+ }
}
/// Required superblock parameters.
@@ -511,6 +591,70 @@ pub struct SuperParams<T: ForeignOwnable + Send + Sync> {
#[repr(transparent)]
pub struct NewSuperBlock<T: FileSystem + ?Sized>(bindings::super_block, PhantomData<T>);
+impl<T: FileSystem + ?Sized> NewSuperBlock<T> {
+ /// Reads sectors.
+ ///
+ /// `count` must be such that the total size doesn't exceed a page.
+ pub fn sread(&self, sector: u64, count: usize, folio: &mut UniqueFolio) -> Result {
+ // Fail requests for non-blockdev file systems. This is a compile-time check.
+ match T::SUPER_TYPE {
+ // The superblock is valid and given that it's a blockdev superblock it must have a
+ // valid `s_bdev`.
+ Super::BlockDev => {}
+ _ => return Err(EIO),
+ }
+
+ crate::build_assert!(count * (bindings::SECTOR_SIZE as usize) <= bindings::PAGE_SIZE);
+
+ // Read the sectors.
+ let mut bio = bindings::bio::default();
+ let bvec = Opaque::<bindings::bio_vec>::uninit();
+
+ // SAFETY: `bio` and `bvec` are allocated on the stack, they're both valid.
+ unsafe {
+ bindings::bio_init(
+ &mut bio,
+ self.0.s_bdev,
+ bvec.get(),
+ 1,
+ bindings::req_op_REQ_OP_READ,
+ )
+ };
+
+ // SAFETY: `bio` was just initialised with `bio_init` above, so it's safe to call
+ // `bio_uninit` on the way out.
+ let mut bio =
+ ScopeGuard::new_with_data(bio, |mut b| unsafe { bindings::bio_uninit(&mut b) });
+
+ // SAFETY: We have one free `bvec` (initialsied above). We also know that size won't exceed
+ // a page size (build_assert above).
+ unsafe {
+ bindings::bio_add_folio_nofail(
+ &mut *bio,
+ folio.0 .0.get(),
+ count * (bindings::SECTOR_SIZE as usize),
+ 0,
+ )
+ };
+ bio.bi_iter.bi_sector = sector;
+
+ // SAFETY: The bio was fully initialised above.
+ to_result(unsafe { bindings::submit_bio_wait(&mut *bio) })?;
+ Ok(())
+ }
+
+ /// Returns the number of sectors in the underlying block device.
+ pub fn sector_count(&self) -> Result<u64> {
+ // Fail requests for non-blockdev file systems. This is a compile-time check.
+ match T::SUPER_TYPE {
+ // The superblock is valid and given that it's a blockdev superblock it must have a
+ // valid `s_bdev`.
+ Super::BlockDev => Ok(unsafe { bindings::bdev_nr_sectors(self.0.s_bdev) }),
+ _ => Err(EIO),
+ }
+ }
+}
+
struct Tables<T: FileSystem + ?Sized>(T);
impl<T: FileSystem + ?Sized> Tables<T> {
const CONTEXT: bindings::fs_context_operations = bindings::fs_context_operations {
@@ -523,9 +667,18 @@ impl<T: FileSystem + ?Sized> Tables<T> {
};
unsafe extern "C" fn get_tree_callback(fc: *mut bindings::fs_context) -> core::ffi::c_int {
- // SAFETY: `fc` is valid per the callback contract. `fill_super_callback` also has
- // the right type and is a valid callback.
- unsafe { bindings::get_tree_nodev(fc, Some(Self::fill_super_callback)) }
+ match T::SUPER_TYPE {
+ // SAFETY: `fc` is valid per the callback contract. `fill_super_callback` also has
+ // the right type and is a valid callback.
+ Super::BlockDev => unsafe {
+ bindings::get_tree_bdev(fc, Some(Self::fill_super_callback))
+ },
+ // SAFETY: `fc` is valid per the callback contract. `fill_super_callback` also has
+ // the right type and is a valid callback.
+ Super::Independent => unsafe {
+ bindings::get_tree_nodev(fc, Some(Self::fill_super_callback))
+ },
+ }
}
unsafe extern "C" fn fill_super_callback(
diff --git a/rust/kernel/fs/buffer.rs b/rust/kernel/fs/buffer.rs
index 6052af8822b3..de23d0fee66c 100644
--- a/rust/kernel/fs/buffer.rs
+++ b/rust/kernel/fs/buffer.rs
@@ -49,7 +49,6 @@ pub struct View {
}
impl View {
- #[allow(dead_code)]
pub(crate) fn new(head: ARef<Head>, offset: usize, size: usize) -> Self {
Self { head, size, offset }
}
--
2.34.1
next prev parent reply other threads:[~2023-10-18 12:26 UTC|newest]
Thread overview: 125+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-18 12:24 [RFC PATCH 00/19] Rust abstractions for VFS Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 01/19] rust: fs: add registration/unregistration of file systems Wedson Almeida Filho
2023-10-18 15:38 ` Benno Lossin
2024-01-10 18:32 ` Wedson Almeida Filho
2024-01-25 9:15 ` Benno Lossin
2023-10-18 12:25 ` [RFC PATCH 02/19] rust: fs: introduce the `module_fs` macro Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 03/19] samples: rust: add initial ro file system sample Wedson Almeida Filho
2023-10-28 16:18 ` Alice Ryhl
2024-01-10 18:25 ` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 04/19] rust: fs: introduce `FileSystem::super_params` Wedson Almeida Filho
2023-10-18 16:34 ` Benno Lossin
2023-10-28 16:39 ` Alice Ryhl
2023-10-30 8:21 ` Benno Lossin
2023-10-30 21:36 ` Alice Ryhl
2023-10-20 15:04 ` Ariel Miculas (amiculas)
2024-01-03 12:25 ` Andreas Hindborg (Samsung)
2023-10-18 12:25 ` [RFC PATCH 05/19] rust: fs: introduce `INode<T>` Wedson Almeida Filho
2023-10-28 18:00 ` Alice Ryhl
2024-01-03 12:45 ` Andreas Hindborg (Samsung)
2024-01-03 12:54 ` Andreas Hindborg (Samsung)
2024-01-04 5:20 ` Darrick J. Wong
2024-01-04 9:57 ` Andreas Hindborg (Samsung)
2024-01-10 9:45 ` Benno Lossin
2024-01-04 5:14 ` Darrick J. Wong
2024-01-24 18:17 ` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 06/19] rust: fs: introduce `FileSystem::init_root` Wedson Almeida Filho
2023-10-19 14:30 ` Benno Lossin
2023-10-20 0:52 ` Boqun Feng
2023-10-21 13:48 ` Benno Lossin
2023-10-21 15:57 ` Boqun Feng
2023-10-21 17:01 ` Matthew Wilcox
2023-10-21 19:33 ` Boqun Feng
2023-10-23 5:29 ` Dave Chinner
2023-10-23 12:55 ` Wedson Almeida Filho
2023-10-30 2:29 ` Dave Chinner
2023-10-31 20:49 ` Wedson Almeida Filho
2023-11-08 4:54 ` Dave Chinner
2023-11-08 6:15 ` Wedson Almeida Filho
2023-10-20 0:30 ` Boqun Feng
2023-10-23 12:36 ` Wedson Almeida Filho
2024-01-03 13:29 ` Andreas Hindborg (Samsung)
2024-01-24 4:07 ` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 07/19] rust: fs: introduce `FileSystem::read_dir` Wedson Almeida Filho
2023-10-21 8:33 ` Benno Lossin
2024-01-03 14:09 ` Andreas Hindborg (Samsung)
2024-01-21 21:00 ` Askar Safin
2024-01-21 21:51 ` Dave Chinner
2023-10-18 12:25 ` [RFC PATCH 08/19] rust: fs: introduce `FileSystem::lookup` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 09/19] rust: folio: introduce basic support for folios Wedson Almeida Filho
2023-10-18 17:17 ` Matthew Wilcox
2023-10-18 18:32 ` Wedson Almeida Filho
2023-10-18 19:21 ` Matthew Wilcox
2023-10-19 13:25 ` Wedson Almeida Filho
2023-10-20 4:11 ` Matthew Wilcox
2023-10-20 15:17 ` Matthew Wilcox
2023-10-23 12:32 ` Wedson Almeida Filho
2023-10-23 10:48 ` Andreas Hindborg (Samsung)
2023-10-23 14:28 ` Matthew Wilcox
2023-10-24 15:04 ` Ariel Miculas (amiculas)
2023-10-23 12:29 ` Wedson Almeida Filho
2023-10-21 9:21 ` Benno Lossin
2023-10-18 12:25 ` [RFC PATCH 10/19] rust: fs: introduce `FileSystem::read_folio` Wedson Almeida Filho
2023-11-07 22:18 ` Matthew Wilcox
2023-11-07 22:22 ` Al Viro
2023-11-08 0:35 ` Wedson Almeida Filho
2023-11-08 0:56 ` Al Viro
2023-11-08 2:39 ` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 11/19] rust: fs: introduce `FileSystem::read_xattr` Wedson Almeida Filho
2023-10-18 13:06 ` Ariel Miculas (amiculas)
2023-10-19 13:35 ` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 12/19] rust: fs: introduce `FileSystem::statfs` Wedson Almeida Filho
2024-01-03 14:13 ` Andreas Hindborg (Samsung)
2024-01-04 5:33 ` Darrick J. Wong
2024-01-24 4:24 ` Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 13/19] rust: fs: introduce more inode types Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 14/19] rust: fs: add per-superblock data Wedson Almeida Filho
2023-10-25 15:51 ` Ariel Miculas (amiculas)
2023-10-26 13:46 ` Ariel Miculas (amiculas)
2024-01-03 14:16 ` Andreas Hindborg (Samsung)
2023-10-18 12:25 ` [RFC PATCH 15/19] rust: fs: add basic support for fs buffer heads Wedson Almeida Filho
2024-01-03 14:17 ` Andreas Hindborg (Samsung)
2023-10-18 12:25 ` Wedson Almeida Filho [this message]
2023-10-21 13:39 ` [RFC PATCH 16/19] rust: fs: allow file systems backed by a block device Benno Lossin
2024-01-24 4:14 ` Wedson Almeida Filho
2024-01-03 14:38 ` Andreas Hindborg (Samsung)
2023-10-18 12:25 ` [RFC PATCH 17/19] rust: fs: allow per-inode data Wedson Almeida Filho
2023-10-21 13:57 ` Benno Lossin
2024-01-03 14:39 ` Andreas Hindborg (Samsung)
2023-10-18 12:25 ` [RFC PATCH 18/19] rust: fs: export file type from mode constants Wedson Almeida Filho
2023-10-18 12:25 ` [RFC PATCH 19/19] tarfs: introduce tar fs Wedson Almeida Filho
2023-10-18 16:57 ` Matthew Wilcox
2023-10-18 17:05 ` Wedson Almeida Filho
2023-10-18 17:20 ` Matthew Wilcox
2023-10-18 18:07 ` Wedson Almeida Filho
2024-01-24 5:05 ` Matthew Wilcox
2024-01-24 5:23 ` Matthew Wilcox
2024-01-24 18:26 ` Wedson Almeida Filho
2024-01-24 21:05 ` Dave Chinner
2024-01-24 21:28 ` Matthew Wilcox
2024-01-24 5:34 ` Gao Xiang
2023-10-18 13:40 ` [RFC PATCH 00/19] Rust abstractions for VFS Ariel Miculas (amiculas)
2023-10-18 17:12 ` Wedson Almeida Filho
2023-10-29 20:31 ` Matthew Wilcox
2023-10-31 20:14 ` Wedson Almeida Filho
2024-01-03 18:02 ` Matthew Wilcox
2024-01-03 19:04 ` Wedson Almeida Filho
2024-01-03 19:53 ` Al Viro
2024-01-03 20:38 ` Kent Overstreet
2024-01-04 1:49 ` Matthew Wilcox
2024-01-09 18:25 ` Wedson Almeida Filho
2024-01-09 19:30 ` Matthew Wilcox
2024-01-03 19:14 ` Kent Overstreet
2024-01-03 20:41 ` Al Viro
2024-01-09 19:13 ` Wedson Almeida Filho
2024-01-09 19:25 ` Matthew Wilcox
2024-01-09 19:32 ` Greg Kroah-Hartman
2024-01-10 7:49 ` Wedson Almeida Filho
2024-01-10 7:57 ` Greg Kroah-Hartman
2024-01-10 12:56 ` Matthew Wilcox
2024-01-09 22:19 ` Dave Chinner
2024-01-10 19:19 ` Kent Overstreet
2024-01-24 13:08 ` FUJITA Tomonori
2024-01-24 19:49 ` Kent Overstreet
2024-01-05 0:04 ` David Howells
2024-01-05 15:54 ` Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231018122518.128049-17-wedsonaf@gmail.com \
--to=wedsonaf@gmail.com \
--cc=brauner@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=kent.overstreet@gmail.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
--cc=walmeida@microsoft.com \
--cc=willy@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).