|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | + |
| 3 | +use super::{NullBlkDevice, THIS_MODULE}; |
| 4 | +use core::fmt::Write; |
| 5 | +use kernel::{ |
| 6 | + block::mq::gen_disk::{GenDisk, GenDiskBuilder}, |
| 7 | + c_str, |
| 8 | + configfs::{self, AttributeOperations}, |
| 9 | + configfs_attrs, new_mutex, |
| 10 | + page::PAGE_SIZE, |
| 11 | + prelude::*, |
| 12 | + str::CString, |
| 13 | + sync::Mutex, |
| 14 | +}; |
| 15 | +use pin_init::PinInit; |
| 16 | + |
| 17 | +pub(crate) fn subsystem() -> impl PinInit<kernel::configfs::Subsystem<Config>, Error> { |
| 18 | + let item_type = configfs_attrs! { |
| 19 | + container: configfs::Subsystem<Config>, |
| 20 | + data: Config, |
| 21 | + child: DeviceConfig, |
| 22 | + attributes: [ |
| 23 | + features: 0, |
| 24 | + ], |
| 25 | + }; |
| 26 | + |
| 27 | + kernel::configfs::Subsystem::new(c_str!("rnull"), item_type, try_pin_init!(Config {})) |
| 28 | +} |
| 29 | + |
| 30 | +#[pin_data] |
| 31 | +pub(crate) struct Config {} |
| 32 | + |
| 33 | +#[vtable] |
| 34 | +impl AttributeOperations<0> for Config { |
| 35 | + type Data = Config; |
| 36 | + |
| 37 | + fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { |
| 38 | + let mut writer = kernel::str::BorrowFormatter::new(page)?; |
| 39 | + writer.write_str("blocksize,size,rotational\n")?; |
| 40 | + Ok(writer.bytes_written()) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[vtable] |
| 45 | +impl configfs::GroupOperations for Config { |
| 46 | + type Child = DeviceConfig; |
| 47 | + |
| 48 | + fn make_group( |
| 49 | + &self, |
| 50 | + name: &CStr, |
| 51 | + ) -> Result<impl PinInit<configfs::Group<DeviceConfig>, Error>> { |
| 52 | + let item_type = configfs_attrs! { |
| 53 | + container: configfs::Group<DeviceConfig>, |
| 54 | + data: DeviceConfig, |
| 55 | + attributes: [ |
| 56 | + // Named for compatibility with C null_blk |
| 57 | + power: 0, |
| 58 | + blocksize: 1, |
| 59 | + rotational: 2, |
| 60 | + size: 3, |
| 61 | + ], |
| 62 | + }; |
| 63 | + |
| 64 | + Ok(configfs::Group::new( |
| 65 | + name.try_into()?, |
| 66 | + item_type, |
| 67 | + // TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>, so put mutex inside |
| 68 | + try_pin_init!( DeviceConfig { |
| 69 | + data <- new_mutex!( DeviceConfigInner { |
| 70 | + powered: false, |
| 71 | + block_size: 4096, |
| 72 | + rotational: false, |
| 73 | + disk: None, |
| 74 | + capacity_mib: 4096, |
| 75 | + name: name.try_into()?, |
| 76 | + }), |
| 77 | + }), |
| 78 | + )) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[pin_data] |
| 83 | +pub(crate) struct DeviceConfig { |
| 84 | + #[pin] |
| 85 | + data: Mutex<DeviceConfigInner>, |
| 86 | +} |
| 87 | + |
| 88 | +#[pin_data] |
| 89 | +struct DeviceConfigInner { |
| 90 | + powered: bool, |
| 91 | + name: CString, |
| 92 | + block_size: u32, |
| 93 | + rotational: bool, |
| 94 | + capacity_mib: u64, |
| 95 | + disk: Option<GenDisk<NullBlkDevice>>, |
| 96 | +} |
| 97 | + |
| 98 | +#[vtable] |
| 99 | +impl configfs::AttributeOperations<0> for DeviceConfig { |
| 100 | + type Data = DeviceConfig; |
| 101 | + |
| 102 | + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { |
| 103 | + let mut writer = kernel::str::BorrowFormatter::new(page)?; |
| 104 | + |
| 105 | + if this.data.lock().powered { |
| 106 | + writer.write_fmt(fmt!("1\n"))?; |
| 107 | + } else { |
| 108 | + writer.write_fmt(fmt!("0\n"))?; |
| 109 | + } |
| 110 | + |
| 111 | + Ok(writer.bytes_written()) |
| 112 | + } |
| 113 | + |
| 114 | + fn store(this: &DeviceConfig, page: &[u8]) -> Result { |
| 115 | + let power_op: bool = core::str::from_utf8(page)? |
| 116 | + .trim() |
| 117 | + .parse::<u8>() |
| 118 | + .map_err(|_| kernel::error::code::EINVAL)? |
| 119 | + != 0; |
| 120 | + |
| 121 | + let mut guard = this.data.lock(); |
| 122 | + |
| 123 | + if !guard.powered && power_op { |
| 124 | + guard.disk = Some(NullBlkDevice::new( |
| 125 | + &guard.name, |
| 126 | + guard.block_size, |
| 127 | + guard.rotational, |
| 128 | + guard.capacity_mib, |
| 129 | + )?); |
| 130 | + guard.powered = true; |
| 131 | + } else if guard.powered && !power_op { |
| 132 | + drop(guard.disk.take()); |
| 133 | + guard.powered = false; |
| 134 | + } |
| 135 | + |
| 136 | + Ok(()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[vtable] |
| 141 | +impl configfs::AttributeOperations<1> for DeviceConfig { |
| 142 | + type Data = DeviceConfig; |
| 143 | + |
| 144 | + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { |
| 145 | + let mut writer = kernel::str::BorrowFormatter::new(page)?; |
| 146 | + writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?; |
| 147 | + Ok(writer.bytes_written()) |
| 148 | + } |
| 149 | + |
| 150 | + fn store(this: &DeviceConfig, page: &[u8]) -> Result { |
| 151 | + if this.data.lock().powered { |
| 152 | + return Err(EBUSY); |
| 153 | + } |
| 154 | + |
| 155 | + let text = core::str::from_utf8(page)?.trim(); |
| 156 | + let value = text |
| 157 | + .parse::<u32>() |
| 158 | + .map_err(|_| kernel::error::code::EINVAL)?; |
| 159 | + |
| 160 | + GenDiskBuilder::validate_block_size(value)?; |
| 161 | + this.data.lock().block_size = value; |
| 162 | + Ok(()) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +#[vtable] |
| 167 | +impl configfs::AttributeOperations<2> for DeviceConfig { |
| 168 | + type Data = DeviceConfig; |
| 169 | + |
| 170 | + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { |
| 171 | + let mut writer = kernel::str::BorrowFormatter::new(page)?; |
| 172 | + |
| 173 | + if this.data.lock().rotational { |
| 174 | + writer.write_fmt(fmt!("1\n"))?; |
| 175 | + } else { |
| 176 | + writer.write_fmt(fmt!("0\n"))?; |
| 177 | + } |
| 178 | + |
| 179 | + Ok(writer.bytes_written()) |
| 180 | + } |
| 181 | + |
| 182 | + fn store(this: &DeviceConfig, page: &[u8]) -> Result { |
| 183 | + if this.data.lock().powered { |
| 184 | + return Err(EBUSY); |
| 185 | + } |
| 186 | + |
| 187 | + this.data.lock().rotational = core::str::from_utf8(page)? |
| 188 | + .trim() |
| 189 | + .parse::<u8>() |
| 190 | + .map_err(|_| kernel::error::code::EINVAL)? |
| 191 | + != 0; |
| 192 | + |
| 193 | + Ok(()) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +#[vtable] |
| 198 | +impl configfs::AttributeOperations<3> for DeviceConfig { |
| 199 | + type Data = DeviceConfig; |
| 200 | + |
| 201 | + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { |
| 202 | + let mut writer = kernel::str::BorrowFormatter::new(page)?; |
| 203 | + writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?; |
| 204 | + Ok(writer.bytes_written()) |
| 205 | + } |
| 206 | + |
| 207 | + fn store(this: &DeviceConfig, page: &[u8]) -> Result { |
| 208 | + if this.data.lock().powered { |
| 209 | + return Err(EBUSY); |
| 210 | + } |
| 211 | + |
| 212 | + let text = core::str::from_utf8(page)?.trim(); |
| 213 | + let value = text |
| 214 | + .parse::<u64>() |
| 215 | + .map_err(|_| kernel::error::code::EINVAL)?; |
| 216 | + |
| 217 | + this.data.lock().capacity_mib = value; |
| 218 | + Ok(()) |
| 219 | + } |
| 220 | +} |
0 commit comments