[PATCH v2 51/83] block: rust: add zoned block device support

From: Andreas Hindborg

Date: Tue Jun 09 2026 - 15:20:17 EST


Add support for zoned block devices to the Rust block layer bindings.
This includes the `report_zones` callback in `Operations` and methods
in `GenDiskBuilder` to configure zoned device parameters.

Drivers can mark a disk as zoned and configure the zone size and
maximum zone append size. The `report_zones` callback is invoked by
the block layer to query zone information.

Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
---
rust/bindings/bindings_helper.h | 1 +
rust/kernel/block/mq/gen_disk.rs | 95 +++++++++++++++++++++++++++++++++-----
rust/kernel/block/mq/operations.rs | 61 +++++++++++++++++++++++-
3 files changed, 145 insertions(+), 12 deletions(-)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index eaf05d60dda9..2a69c17bf271 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -139,6 +139,7 @@ const blk_status_t RUST_CONST_HELPER_BLK_STS_ZONE_ACTIVE_RESOURCE = BLK_STS_ZONE
const blk_status_t RUST_CONST_HELPER_BLK_STS_OFFLINE = BLK_STS_OFFLINE;
const blk_status_t RUST_CONST_HELPER_BLK_STS_DURATION_LIMIT = BLK_STS_DURATION_LIMIT;
const blk_status_t RUST_CONST_HELPER_BLK_STS_INVAL = BLK_STS_INVAL;
+const blk_features_t RUST_CONST_HELPER_BLK_FEAT_ZONED = BLK_FEAT_ZONED;
const fop_flags_t RUST_CONST_HELPER_FOP_UNSIGNED_OFFSET = FOP_UNSIGNED_OFFSET;

const xa_mark_t RUST_CONST_HELPER_XA_PRESENT = XA_PRESENT;
diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_disk.rs
index 79a67b545eca..eedba691e167 100644
--- a/rust/kernel/block/mq/gen_disk.rs
+++ b/rust/kernel/block/mq/gen_disk.rs
@@ -8,6 +8,7 @@
use crate::{
bindings,
block::mq::{
+ operations::OperationsVTable,
Operations,
RequestQueue,
TagSet, //
@@ -48,6 +49,12 @@ pub struct GenDiskBuilder<T> {
physical_block_size: u32,
capacity_sectors: u64,
max_hw_discard_sectors: u32,
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ zoned: bool,
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ zone_size_sectors: u32,
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ zone_append_max_sectors: u32,
_p: PhantomData<T>,
}

@@ -59,6 +66,12 @@ fn default() -> Self {
physical_block_size: bindings::PAGE_SIZE as u32,
capacity_sectors: 0,
max_hw_discard_sectors: 0,
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ zoned: false,
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ zone_size_sectors: 0,
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ zone_append_max_sectors: 0,
_p: PhantomData,
}
}
@@ -130,6 +143,27 @@ pub fn max_hw_discard_sectors(mut self, max_hw_discard_sectors: u32) -> Self {
self
}

+ /// Mark this device as a zoned block device.
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ pub fn zoned(mut self, enable: bool) -> Self {
+ self.zoned = enable;
+ self
+ }
+
+ /// Set the zone size of this block device.
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ pub fn zone_size(mut self, sectors: u32) -> Self {
+ self.zone_size_sectors = sectors;
+ self
+ }
+
+ /// Set the max zone append size for this block device.
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ pub fn zone_append_max(mut self, sectors: u32) -> Self {
+ self.zone_append_max_sectors = sectors;
+ self
+ }
+
/// Build a new `GenDisk` and add it to the VFS.
pub fn build(
self,
@@ -149,7 +183,18 @@ pub fn build(
lim.physical_block_size = self.physical_block_size;
lim.max_hw_discard_sectors = self.max_hw_discard_sectors;
if self.rotational {
- lim.features = bindings::BLK_FEAT_ROTATIONAL;
+ lim.features |= bindings::BLK_FEAT_ROTATIONAL;
+ }
+
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ if self.zoned {
+ if !T::HAS_REPORT_ZONES {
+ return Err(error::code::EINVAL);
+ }
+
+ lim.features |= bindings::BLK_FEAT_ZONED;
+ lim.chunk_sectors = self.zone_size_sectors;
+ lim.max_hw_zone_append_sectors = self.zone_append_max_sectors;
}

// SAFETY: `tagset.raw_tag_set()` points to a valid and initialized tag set
@@ -179,14 +224,6 @@ pub fn build(
// operation, so we will not race.
unsafe { bindings::set_capacity(gendisk, self.capacity_sectors) };

- crate::error::to_result(
- // SAFETY: `gendisk` points to a valid and initialized instance of
- // `struct gendisk`.
- unsafe {
- bindings::device_add_disk(core::ptr::null_mut(), gendisk, core::ptr::null_mut())
- },
- )?;
-
recover_data.dismiss();

// INVARIANT: `gendisk` was initialized above.
@@ -214,7 +251,27 @@ pub fn build(
GFP_KERNEL,
)?;

- Ok(disk.into())
+ let disk: Arc<_> = disk.into();
+
+ // SAFETY: `disk.gendisk` is valid for write as we initialized it above. We have exclusive
+ // access.
+ unsafe { (*disk.gendisk).private_data = Arc::as_ptr(&disk).cast_mut().cast() };
+
+ #[cfg(CONFIG_BLK_DEV_ZONED)]
+ if self.zoned {
+ // SAFETY: `disk.gendisk` is valid as we initialized it above. We have exclusive access.
+ unsafe { bindings::blk_revalidate_disk_zones(gendisk) };
+ }
+
+ crate::error::to_result(
+ // SAFETY: `gendisk` points to a valid and initialized instance of
+ // `struct gendisk`.
+ unsafe {
+ bindings::device_add_disk(core::ptr::null_mut(), gendisk, core::ptr::null_mut())
+ },
+ )?;
+
+ Ok(disk)
}

const VTABLE: bindings::block_device_operations = bindings::block_device_operations {
@@ -228,7 +285,11 @@ pub fn build(
getgeo: None,
set_read_only: None,
swap_slot_free_notify: None,
- report_zones: None,
+ report_zones: if T::HAS_REPORT_ZONES {
+ Some(OperationsVTable::<T>::report_zones_callback)
+ } else {
+ None
+ },
devnode: None,
alternative_gpt_sector: None,
get_unique_id: None,
@@ -327,6 +388,18 @@ fn drop(&mut self) {
/// `self.0` is valid for use as a reference.
pub struct GenDiskRef<T: Operations>(NonNull<GenDisk<T>>);

+impl<T: Operations> GenDiskRef<T> {
+ /// Create a `GenDiskRef` from a pointer to a `GenDisk`.
+ ///
+ /// # Safety
+ ///
+ /// `ptr` must be valid for use as a `GenDisk` reference for the lifetime of the returned
+ /// `GenDiskRef`.
+ pub(crate) unsafe fn from_ptr(ptr: NonNull<GenDisk<T>>) -> GenDiskRef<T> {
+ Self(ptr)
+ }
+}
+
// SAFETY: It is safe to transfer ownership of `GenDiskRef` across thread boundaries.
unsafe impl<T: Operations> Send for GenDiskRef<T> {}

diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/operations.rs
index b9a2bf6592b3..71d4192d627f 100644
--- a/rust/kernel/block/mq/operations.rs
+++ b/rust/kernel/block/mq/operations.rs
@@ -9,6 +9,7 @@
block::{
error::BlkResult,
mq::{
+ gen_disk::GenDiskRef,
request::RequestDataWrapper,
IdleRequest,
Request, //
@@ -16,6 +17,7 @@
},
error::{
from_result,
+ to_result,
Result, //
},
prelude::*,
@@ -29,7 +31,10 @@
Owned, //
},
};
-use core::marker::PhantomData;
+use core::{
+ marker::PhantomData,
+ ptr::NonNull, //
+};
use pin_init::PinInit;

type ForeignBorrowed<'a, T> = <T as ForeignOwnable>::Borrowed<'a>;
@@ -107,6 +112,20 @@ fn init_hctx(
fn poll(_hw_data: ForeignBorrowed<'_, Self::HwData>) -> bool {
build_error!(crate::error::VTABLE_DEFAULT_ERROR)
}
+
+ /// Called by the kernel to get a zone report from the driver.
+ ///
+ /// The driver must call `callback` once for each zone on `disk` and populate the first argument
+ /// with a zone descriptor and the second argument when the zone index.
+ // TODO: We cannot gate this on CONFIG_BLK_DEV_ZONED due to limitations of the `vtable` macro.
+ fn report_zones(
+ _disk: &GenDiskRef<Self>,
+ _sector: u64,
+ _nr_zones: u32,
+ _callback: impl Fn(&bindings::blk_zone, u32) -> Result,
+ ) -> Result<u32> {
+ Err(ENOTSUPP)
+ }
}

/// A vtable for blk-mq to interact with a block device driver.
@@ -359,6 +378,46 @@ impl<T: Operations> OperationsVTable<T> {
unsafe { core::ptr::drop_in_place(pdu) };
}

+ /// This function is a callback hook for the C kernel. A pointer to this function is
+ /// installed in the `blk_mq_ops` vtable for the driver.
+ ///
+ /// # Safety
+ ///
+ /// - This function may only be called by blk-mq C infrastructure.
+ /// - `disk_ptr` must be a pointer to a gendisk initialized by `GenDisk::build`.
+ pub(crate) unsafe extern "C" fn report_zones_callback(
+ disk_ptr: *mut bindings::gendisk,
+ sector: u64,
+ nr_zones: u32,
+ args: *mut bindings::blk_report_zones_args,
+ ) -> i32 {
+ // SAFETY: As `disk_ptr` is a gendisk initialized by `GenDisk::build`, `private_data` is not
+ // null.
+ let disk_ref_ptr = unsafe { NonNull::new_unchecked((*disk_ptr).private_data.cast()) };
+
+ // SAFETY: `disk_ptr.private_data` is a pointer to the `GenDisk` owner of `disk_ptr` that we
+ // installed when we initialized `disk_ptr`. It is valid for use as a reference for the
+ // duration of this call.
+ let disk = unsafe { GenDiskRef::from_ptr(disk_ref_ptr) };
+
+ from_result(|| {
+ T::report_zones(&disk, sector, nr_zones, |zone, idx| -> Result {
+ to_result(
+ // SAFETY: `disk_ptr` is valid by function safety requirements.
+ unsafe {
+ bindings::disk_report_zone(
+ disk_ptr,
+ core::ptr::from_ref(zone).cast_mut(),
+ idx,
+ args,
+ )
+ },
+ )
+ })
+ .and_then(|v: u32| -> Result<_> { Ok(v.try_into()?) })
+ })
+ }
+
const VTABLE: bindings::blk_mq_ops = bindings::blk_mq_ops {
queue_rq: Some(Self::queue_rq_callback),
queue_rqs: None,

--
2.51.2