xref: /linux/drivers/block/rnull/configfs.rs (revision e502b667ee70a7fa2a578e7722672ecd4fe0eb81)
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\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         if this.data.lock().powered {
190             return Err(EBUSY);
191         }
192 
193         let text = core::str::from_utf8(page)?.trim();
194         let value = text.parse::<u32>().map_err(|_| EINVAL)?;
195 
196         GenDiskBuilder::validate_block_size(value)?;
197         this.data.lock().block_size = value;
198         Ok(())
199     }
200 }
201 
202 #[vtable]
203 impl configfs::AttributeOperations<2> for DeviceConfig {
204     type Data = DeviceConfig;
205 
206     fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
207         let mut writer = kernel::str::Formatter::new(page);
208 
209         if this.data.lock().rotational {
210             writer.write_str("1\n")?;
211         } else {
212             writer.write_str("0\n")?;
213         }
214 
215         Ok(writer.bytes_written())
216     }
217 
218     fn store(this: &DeviceConfig, page: &[u8]) -> Result {
219         if this.data.lock().powered {
220             return Err(EBUSY);
221         }
222 
223         this.data.lock().rotational = kstrtobool_bytes(page)?;
224 
225         Ok(())
226     }
227 }
228 
229 #[vtable]
230 impl configfs::AttributeOperations<3> for DeviceConfig {
231     type Data = DeviceConfig;
232 
233     fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
234         let mut writer = kernel::str::Formatter::new(page);
235         writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?;
236         Ok(writer.bytes_written())
237     }
238 
239     fn store(this: &DeviceConfig, page: &[u8]) -> Result {
240         if this.data.lock().powered {
241             return Err(EBUSY);
242         }
243 
244         let text = core::str::from_utf8(page)?.trim();
245         let value = text.parse::<u64>().map_err(|_| EINVAL)?;
246 
247         this.data.lock().capacity_mib = value;
248         Ok(())
249     }
250 }
251 
252 #[vtable]
253 impl configfs::AttributeOperations<4> for DeviceConfig {
254     type Data = DeviceConfig;
255 
256     fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result<usize> {
257         let mut writer = kernel::str::Formatter::new(page);
258         writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?;
259         Ok(writer.bytes_written())
260     }
261 
262     fn store(this: &DeviceConfig, page: &[u8]) -> Result {
263         if this.data.lock().powered {
264             return Err(EBUSY);
265         }
266 
267         let text = core::str::from_utf8(page)?.trim();
268         let value = text.parse::<u8>().map_err(|_| EINVAL)?;
269 
270         this.data.lock().irq_mode = IRQMode::try_from(value)?;
271         Ok(())
272     }
273 }
274