xref: /linux/rust/kernel/block/mq/gen_disk.rs (revision e502b667ee70a7fa2a578e7722672ecd4fe0eb81)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Generic disk abstraction.
4 //!
5 //! C header: [`include/linux/blkdev.h`](srctree/include/linux/blkdev.h)
6 //! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h)
7 
8 use crate::{
9     bindings,
10     block::mq::{
11         Operations,
12         TagSet, //
13     },
14     error::{
15         from_err_ptr,
16         to_result, //
17     },
18     fmt::{
19         self,
20         Write, //
21     },
22     prelude::*,
23     static_lock_class,
24     str::NullTerminatedFormatter,
25     sync::Arc,
26     types::{
27         ForeignOwnable,
28         ScopeGuard, //
29     }, //
30 };
31 
32 /// A builder for [`GenDisk`].
33 ///
34 /// Use this struct to configure and add new [`GenDisk`] to the VFS.
35 pub struct GenDiskBuilder {
36     rotational: bool,
37     logical_block_size: u32,
38     physical_block_size: u32,
39     capacity_sectors: u64,
40 }
41 
42 impl Default for GenDiskBuilder {
43     fn default() -> Self {
44         Self {
45             rotational: false,
46             logical_block_size: bindings::PAGE_SIZE as u32,
47             physical_block_size: bindings::PAGE_SIZE as u32,
48             capacity_sectors: 0,
49         }
50     }
51 }
52 
53 impl GenDiskBuilder {
54     /// Create a new instance.
55     pub fn new() -> Self {
56         Self::default()
57     }
58 
59     /// Set the rotational media attribute for the device to be built.
60     pub fn rotational(mut self, rotational: bool) -> Self {
61         self.rotational = rotational;
62         self
63     }
64 
65     /// Validate block size by verifying that it is between 512 and `PAGE_SIZE`,
66     /// and that it is a power of two.
67     pub fn validate_block_size(size: u32) -> Result {
68         if !(512..=bindings::PAGE_SIZE as u32).contains(&size) || !size.is_power_of_two() {
69             Err(EINVAL)
70         } else {
71             Ok(())
72         }
73     }
74 
75     /// Set the logical block size of the device to be built.
76     ///
77     /// This method will check that block size is a power of two and between 512
78     /// and 4096. If not, an error is returned and the block size is not set.
79     ///
80     /// This is the smallest unit the storage device can address. It is
81     /// typically 4096 bytes.
82     pub fn logical_block_size(mut self, block_size: u32) -> Result<Self> {
83         Self::validate_block_size(block_size)?;
84         self.logical_block_size = block_size;
85         Ok(self)
86     }
87 
88     /// Set the physical block size of the device to be built.
89     ///
90     /// This method will check that block size is a power of two and between 512
91     /// and 4096. If not, an error is returned and the block size is not set.
92     ///
93     /// This is the smallest unit a physical storage device can write
94     /// atomically. It is usually the same as the logical block size but may be
95     /// bigger. One example is SATA drives with 4096 byte physical block size
96     /// that expose a 512 byte logical block size to the operating system.
97     pub fn physical_block_size(mut self, block_size: u32) -> Result<Self> {
98         Self::validate_block_size(block_size)?;
99         self.physical_block_size = block_size;
100         Ok(self)
101     }
102 
103     /// Set the capacity of the device to be built, in sectors (512 bytes).
104     pub fn capacity_sectors(mut self, capacity: u64) -> Self {
105         self.capacity_sectors = capacity;
106         self
107     }
108 
109     /// Build a new `GenDisk` and add it to the VFS.
110     pub fn build<T: Operations>(
111         self,
112         name: fmt::Arguments<'_>,
113         tagset: Arc<TagSet<T>>,
114         queue_data: T::QueueData,
115     ) -> Result<GenDisk<T>> {
116         let data = queue_data.into_foreign();
117         let recover_data = ScopeGuard::new(|| {
118             // SAFETY: T::QueueData was created by the call to `into_foreign()` above
119             drop(unsafe { T::QueueData::from_foreign(data) });
120         });
121 
122         let mut lim: bindings::queue_limits = pin_init::zeroed();
123 
124         lim.logical_block_size = self.logical_block_size;
125         lim.physical_block_size = self.physical_block_size;
126         if self.rotational {
127             lim.features = bindings::BLK_FEAT_ROTATIONAL;
128         }
129 
130         // SAFETY: `tagset.raw_tag_set()` points to a valid and initialized tag set
131         let gendisk = from_err_ptr(unsafe {
132             bindings::__blk_mq_alloc_disk(
133                 tagset.raw_tag_set(),
134                 &mut lim,
135                 data,
136                 static_lock_class!().as_ptr(),
137             )
138         })?;
139 
140         const TABLE: bindings::block_device_operations = bindings::block_device_operations {
141             submit_bio: None,
142             open: None,
143             release: None,
144             ioctl: None,
145             compat_ioctl: None,
146             check_events: None,
147             unlock_native_capacity: None,
148             getgeo: None,
149             set_read_only: None,
150             swap_slot_free_notify: None,
151             report_zones: None,
152             devnode: None,
153             alternative_gpt_sector: None,
154             get_unique_id: None,
155             // TODO: Set to `THIS_MODULE`.
156             owner: core::ptr::null_mut(),
157             pr_ops: core::ptr::null_mut(),
158             free_disk: None,
159             poll_bio: None,
160         };
161 
162         // SAFETY: `gendisk` is a valid pointer as we initialized it above
163         unsafe { (*gendisk).fops = &TABLE };
164 
165         let cleanup_failure = ScopeGuard::new_with_data((gendisk, data), |(gendisk, data)| {
166             // SAFETY: `gendisk` came from `__blk_mq_alloc_disk()` above and
167             // has not been added to the VFS on this cleanup path.
168             unsafe { bindings::put_disk(gendisk) };
169             // SAFETY: `data` came from `into_foreign()` above and has not been
170             // converted back on this cleanup path.
171             drop(unsafe { T::QueueData::from_foreign(data) });
172         });
173 
174         // The failure guard now owns both pieces of cleanup; the early guard
175         // must not run on this path anymore.
176         recover_data.dismiss();
177 
178         let mut writer = NullTerminatedFormatter::new(
179             // SAFETY: `gendisk` points to a valid and initialized instance. We
180             // have exclusive access, since the disk is not added to the VFS
181             // yet.
182             unsafe { &mut (*gendisk).disk_name },
183         )
184         .ok_or(EINVAL)?;
185         writer.write_fmt(name)?;
186 
187         // SAFETY: `gendisk` points to a valid and initialized instance of
188         // `struct gendisk`. `set_capacity` takes a lock to synchronize this
189         // operation, so we will not race.
190         unsafe { bindings::set_capacity(gendisk, self.capacity_sectors) };
191 
192         to_result(
193             // SAFETY: `gendisk` points to a valid and initialized instance of
194             // `struct gendisk`.
195             unsafe {
196                 bindings::device_add_disk(core::ptr::null_mut(), gendisk, core::ptr::null_mut())
197             },
198         )?;
199 
200         cleanup_failure.dismiss();
201 
202         // INVARIANT: `gendisk` was initialized above.
203         // INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above.
204         // INVARIANT: `gendisk.queue.queue_data` is set to `data` in the call to
205         // `__blk_mq_alloc_disk` above.
206         Ok(GenDisk {
207             _tagset: tagset,
208             gendisk,
209         })
210     }
211 }
212 
213 /// A generic block device.
214 ///
215 /// # Invariants
216 ///
217 ///  - `gendisk` must always point to an initialized and valid `struct gendisk`.
218 ///  - `gendisk` was added to the VFS through a call to
219 ///    `bindings::device_add_disk`.
220 ///  - `self.gendisk.queue.queuedata` is initialized by a call to `ForeignOwnable::into_foreign`.
221 pub struct GenDisk<T: Operations> {
222     _tagset: Arc<TagSet<T>>,
223     gendisk: *mut bindings::gendisk,
224 }
225 
226 // SAFETY: `GenDisk` is an owned pointer to a `struct gendisk` and an `Arc` to a
227 // `TagSet`. It is safe to send this to other threads as long as these two are `Send`.
228 unsafe impl<T> Send for GenDisk<T>
229 where
230     T: Operations,
231     T::QueueData: Send,
232     Arc<TagSet<T>>: Send,
233 {
234 }
235 
236 impl<T: Operations> Drop for GenDisk<T> {
237     fn drop(&mut self) {
238         // SAFETY: By type invariant of `Self`, `self.gendisk` points to a valid
239         // and initialized instance of `struct gendisk`, and, `queuedata` was
240         // initialized with the result of a call to
241         // `ForeignOwnable::into_foreign`.
242         let queue_data = unsafe { (*(*self.gendisk).queue).queuedata };
243 
244         // SAFETY: By type invariant, `self.gendisk` points to a valid and
245         // initialized instance of `struct gendisk`, and it was previously added
246         // to the VFS.
247         unsafe { bindings::del_gendisk(self.gendisk) };
248 
249         // SAFETY: By type invariant, `self.gendisk` was added to the VFS, so
250         // `put_disk()` must follow `del_gendisk()` to drop the final gendisk
251         // reference and trigger the remaining release path.
252         unsafe { bindings::put_disk(self.gendisk) };
253 
254         // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with
255         // a call to `ForeignOwnable::into_foreign` to create `queuedata`.
256         // `ForeignOwnable::from_foreign` is only called here.
257         drop(unsafe { T::QueueData::from_foreign(queue_data) });
258     }
259 }
260