Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: GPL-2.0
#
# Rust null block device driver configuration
config BLK_DEV_RUST_NULL
tristate "Rust null block driver (Experimental)"
depends on RUST && CONFIGFS_FS
help
This is the Rust implementation of the null block driver. Like
the C version, the driver allows the user to create virutal block
devices that can be configured via various configuration options.
If unsure, say N.
+3
View File
@@ -0,0 +1,3 @@
obj-$(CONFIG_BLK_DEV_RUST_NULL) += rnull_mod.o
rnull_mod-y := rnull.o
+261
View File
@@ -0,0 +1,261 @@
// SPDX-License-Identifier: GPL-2.0
use super::{NullBlkDevice, THIS_MODULE};
use kernel::{
block::mq::gen_disk::{GenDisk, GenDiskBuilder},
configfs::{self, AttributeOperations},
configfs_attrs,
fmt::{self, Write as _},
new_mutex,
page::PAGE_SIZE,
prelude::*,
str::{kstrtobool_bytes, CString},
sync::Mutex,
};
pub(crate) fn subsystem() -> impl PinInit<kernel::configfs::Subsystem<Config>, Error> {
let item_type = configfs_attrs! {
container: configfs::Subsystem<Config>,
data: Config,
child: DeviceConfig,
attributes: [
features: 0,
],
};
kernel::configfs::Subsystem::new(c"rnull", item_type, try_pin_init!(Config {}))
}
#[pin_data]
pub(crate) struct Config {}
#[vtable]
impl AttributeOperations<0> for Config {
type Data = Config;
fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_str("blocksize,size,rotational,irqmode\n")?;
Ok(writer.bytes_written())
}
}
#[vtable]
impl configfs::GroupOperations for Config {
type Child = DeviceConfig;
fn make_group(
&self,
name: &CStr,
) -> Result<impl PinInit<configfs::Group<DeviceConfig>, Error>> {
let item_type = configfs_attrs! {
container: configfs::Group<DeviceConfig>,
data: DeviceConfig,
attributes: [
// Named for compatibility with C null_blk
power: 0,
blocksize: 1,
rotational: 2,
size: 3,
irqmode: 4,
],
};
Ok(configfs::Group::new(
name.try_into()?,
item_type,
// TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>, so put mutex inside
try_pin_init!( DeviceConfig {
data <- new_mutex!(DeviceConfigInner {
powered: false,
block_size: 4096,
rotational: false,
disk: None,
capacity_mib: 4096,
irq_mode: IRQMode::None,
name: name.try_into()?,
}),
}),
))
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum IRQMode {
None,
Soft,
}
impl TryFrom<u8> for IRQMode {
type Error = kernel::error::Error;
fn try_from(value: u8) -> Result<Self> {
match value {
0 => Ok(Self::None),
1 => Ok(Self::Soft),
_ => Err(EINVAL),
}
}
}
impl fmt::Display for IRQMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::None => f.write_str("0")?,
Self::Soft => f.write_str("1")?,
}
Ok(())
}
}
#[pin_data]
pub(crate) struct DeviceConfig {
#[pin]
data: Mutex<DeviceConfigInner>,
}
#[pin_data]
struct DeviceConfigInner {
powered: bool,
name: CString,
block_size: u32,
rotational: bool,
capacity_mib: u64,
irq_mode: IRQMode,
disk: Option<GenDisk<NullBlkDevice>>,
}
#[vtable]
impl configfs::AttributeOperations<0> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
if this.data.lock().powered {
writer.write_str("1\n")?;
} else {
writer.write_str("0\n")?;
}
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
let power_op = kstrtobool_bytes(page)?;
let mut guard = this.data.lock();
if !guard.powered && power_op {
guard.disk = Some(NullBlkDevice::new(
&guard.name,
guard.block_size,
guard.rotational,
guard.capacity_mib,
guard.irq_mode,
)?);
guard.powered = true;
} else if guard.powered && !power_op {
drop(guard.disk.take());
guard.powered = false;
}
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<1> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?;
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
let text = core::str::from_utf8(page)?.trim();
let value = text.parse::<u32>().map_err(|_| EINVAL)?;
GenDiskBuilder::validate_block_size(value)?;
this.data.lock().block_size = value;
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<2> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
if this.data.lock().rotational {
writer.write_str("1\n")?;
} else {
writer.write_str("0\n")?;
}
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
this.data.lock().rotational = kstrtobool_bytes(page)?;
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<3> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?;
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
let text = core::str::from_utf8(page)?.trim();
let value = text.parse::<u64>().map_err(|_| EINVAL)?;
this.data.lock().capacity_mib = value;
Ok(())
}
}
#[vtable]
impl configfs::AttributeOperations<4> for DeviceConfig {
type Data = DeviceConfig;
fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
let mut writer = kernel::str::Formatter::new(page);
writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?;
Ok(writer.bytes_written())
}
fn store(this: &DeviceConfig, page: &[u8]) -> Result {
if this.data.lock().powered {
return Err(EBUSY);
}
let text = core::str::from_utf8(page)?.trim();
let value = text.parse::<u8>().map_err(|_| EINVAL)?;
this.data.lock().irq_mode = IRQMode::try_from(value)?;
Ok(())
}
}
+100
View File
@@ -0,0 +1,100 @@
// SPDX-License-Identifier: GPL-2.0
//! This is a Rust implementation of the C null block driver.
mod configfs;
use configfs::IRQMode;
use kernel::{
block::{
self,
mq::{
self,
gen_disk::{self, GenDisk},
Operations, TagSet,
},
},
prelude::*,
sync::{aref::ARef, Arc},
};
module! {
type: NullBlkModule,
name: "rnull_mod",
authors: ["Andreas Hindborg"],
description: "Rust implementation of the C null block driver",
license: "GPL v2",
}
#[pin_data]
struct NullBlkModule {
#[pin]
configfs_subsystem: kernel::configfs::Subsystem<configfs::Config>,
}
impl kernel::InPlaceModule for NullBlkModule {
fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
pr_info!("Rust null_blk loaded\n");
try_pin_init!(Self {
configfs_subsystem <- configfs::subsystem(),
})
}
}
struct NullBlkDevice;
impl NullBlkDevice {
fn new(
name: &CStr,
block_size: u32,
rotational: bool,
capacity_mib: u64,
irq_mode: IRQMode,
) -> Result<GenDisk<Self>> {
let tagset = Arc::pin_init(TagSet::new(1, 256, 1), GFP_KERNEL)?;
let queue_data = Box::new(QueueData { irq_mode }, GFP_KERNEL)?;
gen_disk::GenDiskBuilder::new()
.capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT))
.logical_block_size(block_size)?
.physical_block_size(block_size)?
.rotational(rotational)
.build(fmt!("{}", name.to_str()?), tagset, queue_data)
}
}
struct QueueData {
irq_mode: IRQMode,
}
#[vtable]
impl Operations for NullBlkDevice {
type QueueData = KBox<QueueData>;
#[inline(always)]
fn queue_rq(queue_data: &QueueData, rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result {
match queue_data.irq_mode {
IRQMode::None => mq::Request::end_ok(rq)
.map_err(|_e| kernel::error::code::EIO)
// We take no refcounts on the request, so we expect to be able to
// end the request. The request reference must be unique at this
// point, and so `end_ok` cannot fail.
.expect("Fatal error - expected to be able to end request"),
IRQMode::Soft => mq::Request::complete(rq),
}
Ok(())
}
fn commit_rqs(_queue_data: &QueueData) {}
fn complete(rq: ARef<mq::Request<Self>>) {
mq::Request::end_ok(rq)
.map_err(|_e| kernel::error::code::EIO)
// We take no refcounts on the request, so we expect to be able to
// end the request. The request reference must be unique at this
// point, and so `end_ok` cannot fail.
.expect("Fatal error - expected to be able to end request");
}
}