1 // SPDX-License-Identifier: GPL-2.0 2 3 use super::NullBlkDevice; 4 use kernel::{ 5 block::mq::gen_disk::{ 6 GenDisk, 7 GenDiskBuilder, // 8 }, 9 configfs::{ 10 self, 11 AttributeOperations, // 12 }, 13 configfs_attrs, 14 fmt::{ 15 self, 16 Write as _, // 17 }, 18 new_mutex, 19 page::PAGE_SIZE, 20 prelude::*, 21 str::{ 22 kstrtobool_bytes, 23 CString, // 24 }, 25 sync::Mutex, // 26 }; 27 28 pub(crate) fn subsystem() -> impl PinInit<kernel::configfs::Subsystem<Config>, Error> { 29 let item_type = configfs_attrs! { 30 container: configfs::Subsystem<Config>, 31 data: Config, 32 child: DeviceConfig, 33 attributes: [ 34 features: 0, 35 ], 36 }; 37 38 kernel::configfs::Subsystem::new(c"rnull", item_type, try_pin_init!(Config {})) 39 } 40 41 #[pin_data] 42 pub(crate) struct Config {} 43 44 #[vtable] 45 impl AttributeOperations<0> for Config { 46 type Data = Config; 47 48 fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { 49 let mut writer = kernel::str::Formatter::new(page); 50 writer.write_str("blocksize,size,rotational,irqmode,power\n")?; 51 Ok(writer.bytes_written()) 52 } 53 } 54 55 #[vtable] 56 impl configfs::GroupOperations for Config { 57 type Child = DeviceConfig; 58 59 fn make_group( 60 &self, 61 name: &CStr, 62 ) -> Result<impl PinInit<configfs::Group<DeviceConfig>, Error>> { 63 let item_type = configfs_attrs! { 64 container: configfs::Group<DeviceConfig>, 65 data: DeviceConfig, 66 attributes: [ 67 // Named for compatibility with C null_blk 68 power: 0, 69 blocksize: 1, 70 rotational: 2, 71 size: 3, 72 irqmode: 4, 73 ], 74 }; 75 76 Ok(configfs::Group::new( 77 name.try_into()?, 78 item_type, 79 // TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>, so put mutex inside 80 try_pin_init!( DeviceConfig { 81 data <- new_mutex!(DeviceConfigInner { 82 powered: false, 83 block_size: 4096, 84 rotational: false, 85 disk: None, 86 capacity_mib: 4096, 87 irq_mode: IRQMode::None, 88 name: name.try_into()?, 89 }), 90 }), 91 )) 92 } 93 } 94 95 #[derive(Debug, Clone, Copy)] 96 pub(crate) enum IRQMode { 97 None, 98 Soft, 99 } 100 101 impl TryFrom<u8> for IRQMode { 102 type Error = kernel::error::Error; 103 104 fn try_from(value: u8) -> Result<Self> { 105 match value { 106 0 => Ok(Self::None), 107 1 => Ok(Self::Soft), 108 _ => Err(EINVAL), 109 } 110 } 111 } 112 113 impl fmt::Display for IRQMode { 114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 115 match self { 116 Self::None => f.write_str("0")?, 117 Self::Soft => f.write_str("1")?, 118 } 119 Ok(()) 120 } 121 } 122 123 #[pin_data] 124 pub(crate) struct DeviceConfig { 125 #[pin] 126 data: Mutex<DeviceConfigInner>, 127 } 128 129 #[pin_data] 130 struct DeviceConfigInner { 131 powered: bool, 132 name: CString, 133 block_size: u32, 134 rotational: bool, 135 capacity_mib: u64, 136 irq_mode: IRQMode, 137 disk: Option<GenDisk<NullBlkDevice>>, 138 } 139 140 #[vtable] 141 impl configfs::AttributeOperations<0> 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::Formatter::new(page); 146 147 if this.data.lock().powered { 148 writer.write_str("1\n")?; 149 } else { 150 writer.write_str("0\n")?; 151 } 152 153 Ok(writer.bytes_written()) 154 } 155 156 fn store(this: &DeviceConfig, page: &[u8]) -> Result { 157 let power_op = kstrtobool_bytes(page)?; 158 let mut guard = this.data.lock(); 159 160 if !guard.powered && power_op { 161 guard.disk = Some(NullBlkDevice::new( 162 &guard.name, 163 guard.block_size, 164 guard.rotational, 165 guard.capacity_mib, 166 guard.irq_mode, 167 )?); 168 guard.powered = true; 169 } else if guard.powered && !power_op { 170 drop(guard.disk.take()); 171 guard.powered = false; 172 } 173 174 Ok(()) 175 } 176 } 177 178 #[vtable] 179 impl configfs::AttributeOperations<1> for DeviceConfig { 180 type Data = DeviceConfig; 181 182 fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { 183 let mut writer = kernel::str::Formatter::new(page); 184 writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?; 185 Ok(writer.bytes_written()) 186 } 187 188 fn store(this: &DeviceConfig, page: &[u8]) -> Result { 189 let mut guard = this.data.lock(); 190 if guard.powered { 191 return Err(EBUSY); 192 } 193 194 let text = core::str::from_utf8(page)?.trim(); 195 let value = text.parse::<u32>().map_err(|_| EINVAL)?; 196 197 GenDiskBuilder::validate_block_size(value)?; 198 guard.block_size = value; 199 Ok(()) 200 } 201 } 202 203 #[vtable] 204 impl configfs::AttributeOperations<2> for DeviceConfig { 205 type Data = DeviceConfig; 206 207 fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { 208 let mut writer = kernel::str::Formatter::new(page); 209 210 if this.data.lock().rotational { 211 writer.write_str("1\n")?; 212 } else { 213 writer.write_str("0\n")?; 214 } 215 216 Ok(writer.bytes_written()) 217 } 218 219 fn store(this: &DeviceConfig, page: &[u8]) -> Result { 220 let mut guard = this.data.lock(); 221 if guard.powered { 222 return Err(EBUSY); 223 } 224 225 guard.rotational = kstrtobool_bytes(page)?; 226 227 Ok(()) 228 } 229 } 230 231 #[vtable] 232 impl configfs::AttributeOperations<3> for DeviceConfig { 233 type Data = DeviceConfig; 234 235 fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { 236 let mut writer = kernel::str::Formatter::new(page); 237 writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?; 238 Ok(writer.bytes_written()) 239 } 240 241 fn store(this: &DeviceConfig, page: &[u8]) -> Result { 242 let mut guard = this.data.lock(); 243 if guard.powered { 244 return Err(EBUSY); 245 } 246 247 let text = core::str::from_utf8(page)?.trim(); 248 let value = text.parse::<u64>().map_err(|_| EINVAL)?; 249 250 guard.capacity_mib = value; 251 Ok(()) 252 } 253 } 254 255 #[vtable] 256 impl configfs::AttributeOperations<4> for DeviceConfig { 257 type Data = DeviceConfig; 258 259 fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> { 260 let mut writer = kernel::str::Formatter::new(page); 261 writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?; 262 Ok(writer.bytes_written()) 263 } 264 265 fn store(this: &DeviceConfig, page: &[u8]) -> Result { 266 let mut guard = this.data.lock(); 267 if guard.powered { 268 return Err(EBUSY); 269 } 270 271 let text = core::str::from_utf8(page)?.trim(); 272 let value = text.parse::<u8>().map_err(|_| EINVAL)?; 273 274 guard.irq_mode = IRQMode::try_from(value)?; 275 Ok(()) 276 } 277 } 278