-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoperations.rs
More file actions
417 lines (370 loc) · 15.8 KB
/
operations.rs
File metadata and controls
417 lines (370 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// SPDX-License-Identifier: GPL-2.0
//! This module provides an interface for blk-mq drivers to implement.
//!
//! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h)
use crate::{
bindings,
block::mq::{request::RequestDataWrapper, Request},
error::{from_result, Result},
prelude::*,
sync::{aref::ARef, Refcount},
types::{ForeignOwnable, Opaque},
};
use core::marker::PhantomData;
type ForeignBorrowed<'a, T> = <T as ForeignOwnable>::Borrowed<'a>;
/// A borrowed blk-mq hardware queue context.
///
/// # Invariants
///
/// [`HwCtx`] is a transparent wrapper around a live `bindings::blk_mq_hw_ctx`.
#[repr(transparent)]
pub struct HwCtx(Opaque<bindings::blk_mq_hw_ctx>);
impl HwCtx {
/// Creates a reference to an [`HwCtx`] from a valid raw pointer.
///
/// # Safety
///
/// The caller must ensure that `ptr` points to a live
/// `bindings::blk_mq_hw_ctx` for the duration of `'a`.
///
/// The caller must also ensure that the returned reference is the only
/// Rust view used to access the underlying hardware context for the
/// duration of `'a`.
pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::blk_mq_hw_ctx) -> &'a Self {
// SAFETY: `Self` is a transparent wrapper around `bindings::blk_mq_hw_ctx`.
unsafe { &*ptr.cast() }
}
/// Returns a raw pointer to the underlying `struct blk_mq_hw_ctx`.
pub fn as_ptr(&self) -> *mut bindings::blk_mq_hw_ctx {
self as *const Self as *mut bindings::blk_mq_hw_ctx
}
/// Returns the index of this hardware queue.
pub fn queue_num(&self) -> u32 {
// SAFETY: By the type invariant, `self` wraps a live hardware context.
unsafe { (*self.as_ptr()).queue_num }
}
}
/// A borrowed blk-mq completion batching context.
///
/// # Invariants
///
/// [`IoCompBatch`] is a transparent wrapper around a live
/// `bindings::io_comp_batch`.
#[repr(transparent)]
pub struct IoCompBatch(Opaque<bindings::io_comp_batch>);
impl IoCompBatch {
/// Creates an optional reference to an [`IoCompBatch`] from a raw pointer.
///
/// # Safety
///
/// If `ptr` is non-null, the caller must ensure that it points to a live
/// `bindings::io_comp_batch` for the duration of `'a`.
///
/// The caller must also ensure that the returned reference is the only
/// Rust view used to access the underlying completion batch for the
/// duration of `'a`.
pub(crate) unsafe fn from_raw<'a>(ptr: *mut bindings::io_comp_batch) -> Option<&'a Self> {
if ptr.is_null() {
None
} else {
// SAFETY: `Self` is a transparent wrapper around `bindings::io_comp_batch`.
Some(unsafe { &*ptr.cast() })
}
}
/// Returns a raw pointer to the underlying `struct io_comp_batch`.
pub fn as_ptr(&self) -> *mut bindings::io_comp_batch {
self as *const Self as *mut bindings::io_comp_batch
}
}
/// Result returned from blk-mq poll callbacks.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PollResult {
/// The driver completed the given number of requests.
Completed(u32),
/// The driver cannot make progress and blk-mq should stop polling.
Stop,
}
impl PollResult {
/// Returns a result indicating that no requests were completed.
pub const fn none() -> Self {
Self::Completed(0)
}
/// Returns a result indicating that `completed` requests were completed.
pub const fn completed(completed: u32) -> Self {
Self::Completed(completed)
}
/// Returns a result indicating that blk-mq should stop polling for now.
pub const fn stop() -> Self {
Self::Stop
}
fn as_raw(self) -> crate::ffi::c_int {
match self {
Self::Completed(completed) => {
debug_assert!(completed <= crate::ffi::c_int::MAX as u32);
completed as crate::ffi::c_int
}
Self::Stop => -1,
}
}
}
/// Implement this trait to interface blk-mq as block devices.
///
/// To implement a block device driver, implement this trait as described in the
/// [module level documentation]. The kernel will use the implementation of the
/// functions defined in this trait to interface a block device driver. Note:
/// There is no need for an exit_request() implementation, because the `drop`
/// implementation of the [`Request`] type will be invoked by automatically by
/// the C/Rust glue logic.
///
/// [module level documentation]: kernel::block::mq
#[macros::vtable]
pub trait Operations: Sized {
/// Data associated with the `struct request_queue` that is allocated for
/// the `GenDisk` associated with this `Operations` implementation.
type QueueData: ForeignOwnable;
/// Called by the kernel to queue a request with the driver. If `is_last` is
/// `false`, the driver is allowed to defer committing the request.
fn queue_rq(
queue_data: ForeignBorrowed<'_, Self::QueueData>,
rq: ARef<Request<Self>>,
is_last: bool,
) -> Result;
/// Called by the kernel to indicate that queued requests should be submitted.
fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>);
/// Called by the kernel when the request is completed.
fn complete(rq: ARef<Request<Self>>);
/// Called by the kernel to poll the device for completed requests. Only
/// used for poll queues.
fn poll(
_queue_data: ForeignBorrowed<'_, Self::QueueData>,
_hctx: &HwCtx,
_iob: Option<&IoCompBatch>,
) -> PollResult {
build_error!(crate::error::VTABLE_DEFAULT_ERROR)
}
}
/// A vtable for blk-mq to interact with a block device driver.
///
/// A `bindings::blk_mq_ops` vtable is constructed from pointers to the `extern
/// "C"` functions of this struct, exposed through the `OperationsVTable::VTABLE`.
///
/// For general documentation of these methods, see the kernel source
/// documentation related to `struct blk_mq_operations` in
/// [`include/linux/blk-mq.h`].
///
/// [`include/linux/blk-mq.h`]: srctree/include/linux/blk-mq.h
pub(crate) struct OperationsVTable<T: Operations>(PhantomData<T>);
impl<T: Operations> OperationsVTable<T> {
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// - The caller of this function must ensure that the pointee of `bd` is
/// valid for reads for the duration of this function.
/// - This function must be called for an initialized and live `hctx`. That
/// is, `Self::init_hctx_callback` was called and
/// `Self::exit_hctx_callback()` was not yet called.
/// - `(*bd).rq` must point to an initialized and live `bindings:request`.
/// That is, `Self::init_request_callback` was called but
/// `Self::exit_request_callback` was not yet called for the request.
/// - `(*bd).rq` must be owned by the driver. That is, the block layer must
/// promise to not access the request until the driver calls
/// `bindings::blk_mq_end_request` for the request.
unsafe extern "C" fn queue_rq_callback(
hctx: *mut bindings::blk_mq_hw_ctx,
bd: *const bindings::blk_mq_queue_data,
) -> bindings::blk_status_t {
// SAFETY: `bd.rq` is valid as required by the safety requirement for
// this function.
let request = unsafe { &*(*bd).rq.cast::<Request<T>>() };
// One refcount for the ARef, one for being in flight
request.wrapper_ref().refcount().set(2);
// SAFETY:
// - We own a refcount that we took above. We pass that to `ARef`.
// - By the safety requirements of this function, `request` is a valid
// `struct request` and the private data is properly initialized.
// - `rq` will be alive until `blk_mq_end_request` is called and is
// reference counted by `ARef` until then.
let rq = unsafe { Request::aref_from_raw((*bd).rq) };
// SAFETY: `hctx` is valid as required by this function.
let queue_data = unsafe { (*(*hctx).queue).queuedata };
// SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with
// a call to `ForeignOwnable::into_foreign` to create `queuedata`.
// `ForeignOwnable::from_foreign` is only called when the tagset is
// dropped, which happens after we are dropped.
let queue_data = unsafe { T::QueueData::borrow(queue_data) };
// SAFETY: We have exclusive access and we just set the refcount above.
unsafe { Request::start_unchecked(&rq) };
let ret = T::queue_rq(
queue_data,
rq,
// SAFETY: `bd` is valid as required by the safety requirement for
// this function.
unsafe { (*bd).last },
);
if let Err(e) = ret {
e.to_blk_status()
} else {
bindings::BLK_STS_OK as bindings::blk_status_t
}
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// This function may only be called by blk-mq C infrastructure. The caller
/// must ensure that `hctx` is valid.
unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_hw_ctx) {
// SAFETY: `hctx` is valid as required by this function.
let queue_data = unsafe { (*(*hctx).queue).queuedata };
// SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` with a
// call to `ForeignOwnable::into_foreign()` to create `queuedata`.
// `ForeignOwnable::from_foreign()` is only called when the tagset is
// dropped, which happens after we are dropped.
let queue_data = unsafe { T::QueueData::borrow(queue_data) };
T::commit_rqs(queue_data)
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// This function may only be called by blk-mq C infrastructure. `rq` must
/// point to a valid request that has been marked as completed. The pointee
/// of `rq` must be valid for write for the duration of this function.
unsafe extern "C" fn complete_callback(rq: *mut bindings::request) {
// SAFETY: This function can only be dispatched through
// `Request::complete`. We leaked a refcount then which we pick back up
// now.
let aref = unsafe { Request::aref_from_raw(rq) };
T::complete(aref);
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// This function may only be called by blk-mq C infrastructure. The caller
/// must ensure that `hctx` is valid. If non-null, `iob` must point to a
/// live completion batch for the duration of this callback.
unsafe extern "C" fn poll_callback(
hctx: *mut bindings::blk_mq_hw_ctx,
iob: *mut bindings::io_comp_batch,
) -> crate::ffi::c_int {
// SAFETY: `hctx` is valid as required by this function.
let hctx = unsafe { HwCtx::from_raw(hctx) };
// SAFETY: `hctx` is live as required by this function, so the request
// queue and its `queuedata` are also live for the duration of this
// callback.
let queue_data = unsafe { (*(*hctx.as_ptr()).queue).queuedata };
// SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build`
// with a call to `ForeignOwnable::into_foreign` to create
// `queuedata`. `ForeignOwnable::from_foreign` is only called when the
// tagset is dropped, which happens after we are dropped.
let queue_data = unsafe { T::QueueData::borrow(queue_data) };
// SAFETY: If non-null, `iob` is valid as required by this function.
let iob = unsafe { IoCompBatch::from_raw(iob) };
T::poll(queue_data, hctx, iob).as_raw()
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// This function may only be called by blk-mq C infrastructure. This
/// function may only be called once before `exit_hctx_callback` is called
/// for the same context.
unsafe extern "C" fn init_hctx_callback(
_hctx: *mut bindings::blk_mq_hw_ctx,
_tagset_data: *mut crate::ffi::c_void,
_hctx_idx: crate::ffi::c_uint,
) -> crate::ffi::c_int {
from_result(|| Ok(0))
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// This function may only be called by blk-mq C infrastructure.
unsafe extern "C" fn exit_hctx_callback(
_hctx: *mut bindings::blk_mq_hw_ctx,
_hctx_idx: crate::ffi::c_uint,
) {
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// - This function may only be called by blk-mq C infrastructure.
/// - `_set` must point to an initialized `TagSet<T>`.
/// - `rq` must point to an initialized `bindings::request`.
/// - The allocation pointed to by `rq` must be at the size of `Request`
/// plus the size of `RequestDataWrapper`.
unsafe extern "C" fn init_request_callback(
_set: *mut bindings::blk_mq_tag_set,
rq: *mut bindings::request,
_hctx_idx: crate::ffi::c_uint,
_numa_node: crate::ffi::c_uint,
) -> crate::ffi::c_int {
from_result(|| {
// SAFETY: By the safety requirements of this function, `rq` points
// to a valid allocation.
let pdu = unsafe { Request::wrapper_ptr(rq.cast::<Request<T>>()) };
// SAFETY: The refcount field is allocated but not initialized, so
// it is valid for writes.
unsafe { RequestDataWrapper::refcount_ptr(pdu.as_ptr()).write(Refcount::new(0)) };
Ok(0)
})
}
/// This function is called by the C kernel. A pointer to this function is
/// installed in the `blk_mq_ops` vtable for the driver.
///
/// # Safety
///
/// - This function may only be called by blk-mq C infrastructure.
/// - `_set` must point to an initialized `TagSet<T>`.
/// - `rq` must point to an initialized and valid `Request`.
unsafe extern "C" fn exit_request_callback(
_set: *mut bindings::blk_mq_tag_set,
rq: *mut bindings::request,
_hctx_idx: crate::ffi::c_uint,
) {
// SAFETY: The tagset invariants guarantee that all requests are allocated with extra memory
// for the request data.
let pdu = unsafe { bindings::blk_mq_rq_to_pdu(rq) }.cast::<RequestDataWrapper>();
// SAFETY: `pdu` is valid for read and write and is properly initialised.
unsafe { core::ptr::drop_in_place(pdu) };
}
const VTABLE: bindings::blk_mq_ops = bindings::blk_mq_ops {
queue_rq: Some(Self::queue_rq_callback),
queue_rqs: None,
commit_rqs: Some(Self::commit_rqs_callback),
get_budget: None,
put_budget: None,
set_rq_budget_token: None,
get_rq_budget_token: None,
timeout: None,
poll: if T::HAS_POLL {
Some(Self::poll_callback)
} else {
None
},
complete: Some(Self::complete_callback),
init_hctx: Some(Self::init_hctx_callback),
exit_hctx: Some(Self::exit_hctx_callback),
init_request: Some(Self::init_request_callback),
exit_request: Some(Self::exit_request_callback),
cleanup_rq: None,
busy: None,
map_queues: None,
#[cfg(CONFIG_BLK_DEBUG_FS)]
show_rq: None,
};
pub(crate) const fn build() -> &'static bindings::blk_mq_ops {
&Self::VTABLE
}
}