-
Notifications
You must be signed in to change notification settings - Fork 3
/
blob.rs
381 lines (332 loc) · 10.9 KB
/
blob.rs
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
use crate::blob_bdev::BlobStoreBDev;
use crate::env::Buf;
use crate::generated::{
spdk_blob, spdk_blob_close, spdk_blob_get_id, spdk_blob_get_num_clusters, spdk_blob_id,
spdk_blob_io_read, spdk_blob_io_write, spdk_blob_resize, spdk_blob_store, spdk_blob_sync_md,
spdk_bs_alloc_io_channel, spdk_bs_create_blob, spdk_bs_delete_blob, spdk_bs_free_cluster_count,
spdk_bs_free_io_channel, spdk_bs_get_page_size, spdk_bs_init, spdk_bs_open_blob,
spdk_bs_unload, spdk_io_channel,
};
use futures::channel::oneshot;
use futures::channel::oneshot::Sender;
use libc::c_int;
use libc::c_void;
use std::fmt;
use std::fmt::Debug;
use std::ptr;
#[derive(Debug, Error)]
pub enum BlobstoreError {
#[error(display = "Failed to initialize blob store: {}", _0)]
InitError(i32),
#[error(display = "Failed to allocate io channel")]
IoChannelAllocateError,
#[error(display = "Failed to unload blob store: {}", _0)]
UnloadError(i32),
}
#[derive(Debug)]
pub struct Blobstore {
pub(crate) blob_store: *mut spdk_blob_store,
}
impl Blobstore {
pub fn get_page_size(&self) -> u64 {
unsafe { spdk_bs_get_page_size(self.blob_store) }
}
pub fn get_free_cluster_count(&self) -> u64 {
unsafe { spdk_bs_free_cluster_count(self.blob_store) }
}
pub fn alloc_io_channel(&mut self) -> Result<IoChannel, BlobstoreError> {
let io_channel = unsafe { spdk_bs_alloc_io_channel(self.blob_store) };
if io_channel.is_null() {
return Err(BlobstoreError::IoChannelAllocateError);
}
Ok(IoChannel { io_channel })
}
}
#[derive(Debug, Error)]
pub enum BlobError {
#[error(display = "Failed to create blob: {}", _0)]
CreateError(i32),
#[error(display = "Failed to open blob({}): {}", _0, _1)]
OpenError(BlobId, i32),
#[error(display = "Failed to resize blob({}): {}", _0, _1)]
ResizeError(BlobId, i32),
#[error(display = "Failed to sync metadata for blob({}): {}", _0, _1)]
SyncError(BlobId, i32),
#[error(
display = "Error in write completion({}): {}, offset: {}, length: {}",
_0,
_1,
_2,
_3
)]
WriteError(BlobId, i32, u64, u64),
#[error(
display = "Error in read completion({}): {}, offset: {}, length: {}",
_0,
_1,
_2,
_3
)]
ReadError(BlobId, i32, u64, u64),
#[error(display = "Failed to close blob: {}", _0)]
CloseError(i32),
#[error(display = "Failed to delete blob({}): {}", _0, _1)]
DeleteError(BlobId, i32),
}
#[derive(Debug, Clone, Copy)]
pub struct BlobId {
pub(crate) blob_id: spdk_blob_id,
}
impl fmt::Display for BlobId {
fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Debug)]
pub struct Blob {
pub(crate) blob: *mut spdk_blob,
}
impl Blob {
pub fn get_num_clusters(&self) -> u64 {
unsafe { spdk_blob_get_num_clusters(self.blob) }
}
pub fn get_blob_id(&self) -> BlobId {
let blob_id = unsafe { spdk_blob_get_id(self.blob) };
BlobId { blob_id }
}
}
// TODO: Drop for Blob
pub struct IoChannel {
pub(crate) io_channel: *mut spdk_io_channel,
}
impl Drop for IoChannel {
fn drop(&mut self) {
unsafe { spdk_bs_free_io_channel(self.io_channel) };
}
}
// TODO: Implement Drop correctly with a call to spdk_bs_unload:
// Funny thing is that this is async, so will be interesting to see how to do that?
// I can't block
/// Initialize a blobstore on the given device.
pub async fn bs_init(bs_dev: &mut BlobStoreBDev) -> Result<Blobstore, BlobstoreError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_bs_init(
bs_dev.bs_dev,
ptr::null_mut(),
Some(complete_callback_1::<*mut spdk_blob_store>),
cb_arg(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(blob_store) => Ok(Blobstore { blob_store }),
Err(bserrno) => Err(BlobstoreError::InitError(bserrno)),
}
}
pub async fn bs_unload(blob_store: Blobstore) -> Result<(), BlobstoreError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_bs_unload(
blob_store.blob_store,
Some(complete_callback_0),
cb_arg::<()>(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobstoreError::UnloadError(bserrno)),
}
}
pub async fn create(blob_store: &Blobstore) -> Result<BlobId, BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_bs_create_blob(
blob_store.blob_store,
Some(complete_callback_1::<spdk_blob_id>),
cb_arg(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(blob_id) => Ok(BlobId { blob_id }),
Err(bserrno) => Err(BlobError::CreateError(bserrno)),
}
}
pub async fn open(blob_store: &Blobstore, blob_id: BlobId) -> Result<Blob, BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_bs_open_blob(
blob_store.blob_store,
blob_id.blob_id,
Some(complete_callback_1::<*mut spdk_blob>),
cb_arg(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(blob) => Ok(Blob { blob }),
Err(bserrno) => Err(BlobError::OpenError(blob_id, bserrno)),
}
}
pub async fn resize(blob: &Blob, required_size: u64) -> Result<(), BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_blob_resize(
blob.blob,
required_size,
Some(complete_callback_0),
cb_arg::<()>(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobError::ResizeError(blob.get_blob_id(), bserrno)),
}
}
/**
* Sync a blob.
*
* Make a blob persistent. This applies to open, resize, set xattr, and remove
* xattr. These operations will not be persistent until the blob has been synced.
*
* \param blob Blob to sync.
* \param cb_fn Called when the operation is complete.
* \param cb_arg Argument passed to function cb_fn.
*/
/// Metadata is stored in volatile memory for performance
/// reasons and therefore needs to be synchronized with
/// non-volatile storage to make it persistent. This can be
/// done manually, as shown here, or if not it will be done
/// automatically when the blob is closed. It is always a
/// good idea to sync after making metadata changes unless
/// it has an unacceptable impact on application performance.
pub async fn sync_metadata(blob: &Blob) -> Result<(), BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_blob_sync_md(blob.blob, Some(complete_callback_0), cb_arg::<()>(sender));
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobError::SyncError(blob.get_blob_id(), bserrno)),
}
}
/// Write data to a blob.
///
/// \param blob Blob to write.
/// \param channel The I/O channel used to submit requests.
/// \param payload The specified buffer which should contain the data to be written.
/// \param offset Offset is in pages from the beginning of the blob.
/// \param length Size of data in pages.
/// \param cb_fn Called when the operation is complete.
/// \param cb_arg Argument passed to function cb_fn.
/// TODO: the interface here is funky as is, needs work;
/// Specifically writes need to happen in pages, so the buf abstraction should probably enforce that.
/// Similarly, spdk_blob_io_writev is probably the more interesting case if we don't want to
/// have to do copies.
pub async fn write<'a>(
blob: &'a Blob,
io_channel: &'a IoChannel,
buf: &'a Buf,
offset: u64,
length: u64,
) -> Result<(), BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_blob_io_write(
blob.blob,
io_channel.io_channel,
buf.ptr,
offset,
length,
Some(complete_callback_0),
cb_arg::<()>(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobError::WriteError(
blob.get_blob_id(),
bserrno,
offset,
length,
)),
}
}
pub async fn read<'a>(
blob: &'a Blob,
io_channel: &'a IoChannel,
buf: &'a Buf,
offset: u64,
length: u64,
) -> Result<(), BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_blob_io_read(
blob.blob,
io_channel.io_channel,
buf.ptr,
offset,
length,
Some(complete_callback_0),
cb_arg::<()>(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobError::ReadError(
blob.get_blob_id(),
bserrno,
offset,
length,
)),
}
}
pub async fn close(blob: Blob) -> Result<(), BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_blob_close(blob.blob, Some(complete_callback_0), cb_arg::<()>(sender));
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobError::CloseError(bserrno)),
}
}
pub async fn delete(blob_store: &Blobstore, blob_id: BlobId) -> Result<(), BlobError> {
let (sender, receiver) = oneshot::channel();
unsafe {
spdk_bs_delete_blob(
blob_store.blob_store,
blob_id.blob_id,
Some(complete_callback_0),
cb_arg::<()>(sender),
);
}
let res = receiver.await.expect("Cancellation is not supported");
match res {
Ok(()) => Ok(()),
Err(bserrno) => Err(BlobError::DeleteError(blob_id, bserrno)),
}
}
fn cb_arg<T>(sender: Sender<Result<T, i32>>) -> *mut c_void {
Box::into_raw(Box::new(sender)) as *const _ as *mut c_void
}
extern "C" fn complete_callback_0(sender_ptr: *mut c_void, bserrno: c_int) {
let sender = unsafe { Box::from_raw(sender_ptr as *mut Sender<Result<(), i32>>) };
let ret = if bserrno != 0 { Err(bserrno) } else { Ok(()) };
sender.send(ret).expect("Receiver is gone");
}
extern "C" fn complete_callback_1<T>(sender_ptr: *mut c_void, bs: T, bserrno: c_int)
where
T: Debug,
{
let sender = unsafe { Box::from_raw(sender_ptr as *mut Sender<Result<T, i32>>) };
let ret = if bserrno != 0 { Err(bserrno) } else { Ok(bs) };
sender.send(ret).expect("Receiver is gone");
}