-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathdisk.rs
649 lines (582 loc) · 23.7 KB
/
disk.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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Disks and snapshots
use crate::app::sagas;
use crate::authn;
use crate::authz;
use crate::db;
use crate::db::lookup;
use crate::db::lookup::LookupPath;
use crate::external_api::params;
use nexus_db_queries::context::OpContext;
use nexus_types::identity::Resource;
use omicron_common::api::external::http_pagination::PaginatedBy;
use omicron_common::api::external::ByteCount;
use omicron_common::api::external::CreateResult;
use omicron_common::api::external::DeleteResult;
use omicron_common::api::external::DiskState;
use omicron_common::api::external::Error;
use omicron_common::api::external::InternalContext;
use omicron_common::api::external::ListResultVec;
use omicron_common::api::external::LookupResult;
use omicron_common::api::external::NameOrId;
use omicron_common::api::external::UpdateResult;
use omicron_common::api::internal::nexus::DiskRuntimeState;
use sled_agent_client::Client as SledAgentClient;
use std::sync::Arc;
use uuid::Uuid;
fn validate_disk_create_params(
params: ¶ms::DiskCreate,
block_size: u64,
) -> Result<(), Error> {
// Reject disks where the block size doesn't evenly divide the
// total size
if (params.size.to_bytes() % block_size) != 0 {
return Err(Error::InvalidValue {
label: String::from("size and block_size"),
message: format!(
"total size must be a multiple of block size {}",
block_size,
),
});
}
// Reject disks where the size isn't at least
// MIN_DISK_SIZE_BYTES
if params.size.to_bytes() < params::MIN_DISK_SIZE_BYTES as u64 {
return Err(Error::InvalidValue {
label: String::from("size"),
message: format!(
"total size must be at least {}",
ByteCount::from(params::MIN_DISK_SIZE_BYTES)
),
});
}
// Reject disks where the MIN_DISK_SIZE_BYTES doesn't evenly
// divide the size
if (params.size.to_bytes() % params::MIN_DISK_SIZE_BYTES as u64) != 0 {
return Err(Error::InvalidValue {
label: String::from("size"),
message: format!(
"total size must be a multiple of {}",
ByteCount::from(params::MIN_DISK_SIZE_BYTES)
),
});
}
Ok(())
}
impl super::Nexus {
// Disks
pub fn disk_lookup<'a>(
&'a self,
opctx: &'a OpContext,
disk_selector: params::DiskSelector,
) -> LookupResult<lookup::Disk<'a>> {
match disk_selector {
params::DiskSelector { disk: NameOrId::Id(id), project: None } => {
let disk =
LookupPath::new(opctx, &self.db_datastore).disk_id(id);
Ok(disk)
}
params::DiskSelector {
disk: NameOrId::Name(name),
project: Some(project),
} => {
let disk = self
.project_lookup(opctx, params::ProjectSelector { project })?
.disk_name_owned(name.into());
Ok(disk)
}
params::DiskSelector {
disk: NameOrId::Id(_),
..
} => Err(Error::invalid_request(
"when providing disk as an ID project should not be specified",
)),
_ => Err(Error::invalid_request(
"disk should either be UUID or project should be specified",
)),
}
}
pub async fn project_create_disk(
self: &Arc<Self>,
opctx: &OpContext,
project_lookup: &lookup::Project<'_>,
params: ¶ms::DiskCreate,
) -> CreateResult<db::model::Disk> {
let (.., authz_project) =
project_lookup.lookup_for(authz::Action::CreateChild).await?;
match ¶ms.disk_source {
params::DiskSource::Blank { block_size } => {
validate_disk_create_params(¶ms, (*block_size).into())?;
}
params::DiskSource::Snapshot { snapshot_id } => {
let (.., db_snapshot) =
LookupPath::new(opctx, &self.db_datastore)
.snapshot_id(*snapshot_id)
.fetch()
.await?;
validate_disk_create_params(
¶ms,
db_snapshot.block_size.to_bytes().into(),
)?;
// If the size of the snapshot is greater than the size of the
// disk, return an error.
if db_snapshot.size.to_bytes() > params.size.to_bytes() {
return Err(Error::invalid_request(
&format!(
"disk size {} must be greater than or equal to snapshot size {}",
params.size.to_bytes(),
db_snapshot.size.to_bytes(),
),
));
}
}
params::DiskSource::Image { image_id } => {
let (.., db_image) = LookupPath::new(opctx, &self.db_datastore)
.image_id(*image_id)
.fetch()
.await?;
// Reject disks where the block size doesn't evenly divide the
// total size
if (params.size.to_bytes()
% db_image.block_size.to_bytes() as u64)
!= 0
{
return Err(Error::InvalidValue {
label: String::from("size and block_size"),
message: String::from(
"total size must be a multiple of global image's block size",
),
});
}
// If the size of the image is greater than the size of the
// disk, return an error.
if db_image.size.to_bytes() > params.size.to_bytes() {
return Err(Error::invalid_request(
&format!(
"disk size {} must be greater than or equal to image size {}",
params.size.to_bytes(),
db_image.size.to_bytes(),
),
));
}
// Reject disks where the size isn't at least
// MIN_DISK_SIZE_BYTES
if params.size.to_bytes() < params::MIN_DISK_SIZE_BYTES as u64 {
return Err(Error::InvalidValue {
label: String::from("size"),
message: format!(
"total size must be at least {}",
ByteCount::from(params::MIN_DISK_SIZE_BYTES)
),
});
}
// Reject disks where the MIN_DISK_SIZE_BYTES doesn't evenly
// divide the size
if (params.size.to_bytes() % params::MIN_DISK_SIZE_BYTES as u64)
!= 0
{
return Err(Error::InvalidValue {
label: String::from("size"),
message: format!(
"total size must be a multiple of {}",
ByteCount::from(params::MIN_DISK_SIZE_BYTES)
),
});
}
}
params::DiskSource::GlobalImage { image_id } => {
let (.., db_global_image) =
LookupPath::new(opctx, &self.db_datastore)
.global_image_id(*image_id)
.fetch()
.await?;
validate_disk_create_params(
¶ms,
db_global_image.block_size.to_bytes().into(),
)?;
// If the size of the image is greater than the size of the
// disk, return an error.
if db_global_image.size.to_bytes() > params.size.to_bytes() {
return Err(Error::invalid_request(
&format!(
"disk size {} must be greater than or equal to image size {}",
params.size.to_bytes(),
db_global_image.size.to_bytes(),
),
));
}
}
params::DiskSource::ImportingBlocks { block_size } => {
validate_disk_create_params(¶ms, (*block_size).into())?;
}
}
let saga_params = sagas::disk_create::Params {
serialized_authn: authn::saga::Serialized::for_opctx(opctx),
project_id: authz_project.id(),
create_params: params.clone(),
};
let saga_outputs = self
.execute_saga::<sagas::disk_create::SagaDiskCreate>(saga_params)
.await?;
let disk_created = saga_outputs
.lookup_node_output::<db::model::Disk>("created_disk")
.map_err(|e| Error::internal_error(&format!("{:#}", &e)))
.internal_context("looking up output from disk create saga")?;
Ok(disk_created)
}
pub async fn disk_list(
&self,
opctx: &OpContext,
project_lookup: &lookup::Project<'_>,
pagparams: &PaginatedBy<'_>,
) -> ListResultVec<db::model::Disk> {
let (.., authz_project) =
project_lookup.lookup_for(authz::Action::ListChildren).await?;
self.db_datastore.disk_list(opctx, &authz_project, pagparams).await
}
/// Modifies the runtime state of the Disk as requested. This generally
/// means attaching or detaching the disk.
// TODO(https://github.com/oxidecomputer/omicron/issues/811):
// This will be unused until we implement hot-plug support.
// However, it has been left for reference until then, as it will
// likely be needed once that feature is implemented.
#[allow(dead_code)]
pub(crate) async fn disk_set_runtime(
&self,
opctx: &OpContext,
authz_disk: &authz::Disk,
db_disk: &db::model::Disk,
sa: Arc<SledAgentClient>,
requested: sled_agent_client::types::DiskStateRequested,
) -> Result<(), Error> {
let runtime: DiskRuntimeState = db_disk.runtime().into();
opctx.authorize(authz::Action::Modify, authz_disk).await?;
// Ask the Sled Agent to begin the state change. Then update the
// database to reflect the new intermediate state.
let new_runtime = sa
.disk_put(
&authz_disk.id(),
&sled_agent_client::types::DiskEnsureBody {
initial_runtime:
sled_agent_client::types::DiskRuntimeState::from(
runtime,
),
target: requested,
},
)
.await
.map_err(Error::from)?;
let new_runtime: DiskRuntimeState = new_runtime.into_inner().into();
self.db_datastore
.disk_update_runtime(opctx, authz_disk, &new_runtime.into())
.await
.map(|_| ())
}
pub async fn notify_disk_updated(
&self,
opctx: &OpContext,
id: Uuid,
new_state: &DiskRuntimeState,
) -> Result<(), Error> {
let log = &self.log;
let (.., authz_disk) = LookupPath::new(&opctx, &self.db_datastore)
.disk_id(id)
.lookup_for(authz::Action::Modify)
.await?;
let result = self
.db_datastore
.disk_update_runtime(opctx, &authz_disk, &new_state.clone().into())
.await;
// TODO-cleanup commonize with notify_instance_updated()
match result {
Ok(true) => {
info!(log, "disk updated by sled agent";
"disk_id" => %id,
"new_state" => ?new_state);
Ok(())
}
Ok(false) => {
info!(log, "disk update from sled agent ignored (old)";
"disk_id" => %id);
Ok(())
}
// If the disk doesn't exist, swallow the error -- there's
// nothing to do here.
// TODO-robustness This could only be possible if we've removed a
// disk from the datastore altogether. When would we do that?
// We don't want to do it as soon as something's destroyed, I think,
// and in that case, we'd need some async task for cleaning these
// up.
Err(Error::ObjectNotFound { .. }) => {
warn!(log, "non-existent disk updated by sled agent";
"instance_id" => %id,
"new_state" => ?new_state);
Ok(())
}
// If the datastore is unavailable, propagate that to the caller.
Err(error) => {
warn!(log, "failed to update disk from sled agent";
"disk_id" => %id,
"new_state" => ?new_state,
"error" => ?error);
Err(error)
}
}
}
pub async fn project_delete_disk(
self: &Arc<Self>,
opctx: &OpContext,
disk_lookup: &lookup::Disk<'_>,
) -> DeleteResult {
let (.., project, authz_disk) =
disk_lookup.lookup_for(authz::Action::Delete).await?;
let saga_params = sagas::disk_delete::Params {
serialized_authn: authn::saga::Serialized::for_opctx(opctx),
project_id: project.id(),
disk_id: authz_disk.id(),
};
self.execute_saga::<sagas::disk_delete::SagaDiskDelete>(saga_params)
.await?;
Ok(())
}
/// Remove a read only parent from a disk.
/// This is just a wrapper around the volume operation of the same
/// name, but we provide this interface when all the caller has is
/// the disk UUID as the internal volume_id is not exposed.
pub async fn disk_remove_read_only_parent(
self: &Arc<Self>,
opctx: &OpContext,
disk_id: Uuid,
) -> DeleteResult {
// First get the internal volume ID that is stored in the disk
// database entry, once we have that just call the volume method
// to remove the read only parent.
let (.., db_disk) = LookupPath::new(opctx, &self.db_datastore)
.disk_id(disk_id)
.fetch()
.await?;
self.volume_remove_read_only_parent(&opctx, db_disk.volume_id).await?;
Ok(())
}
/// Import blocks from a URL into a disk
pub async fn import_blocks_from_url_for_disk(
self: &Arc<Self>,
opctx: &OpContext,
disk_lookup: &lookup::Disk<'_>,
params: params::ImportBlocksFromUrl,
) -> UpdateResult<()> {
let authz_disk: authz::Disk;
(.., authz_disk) =
disk_lookup.lookup_for(authz::Action::Modify).await?;
let saga_params = sagas::import_blocks_from_url::Params {
serialized_authn: authn::saga::Serialized::for_opctx(opctx),
disk_id: authz_disk.id(),
import_params: params.clone(),
};
self
.execute_saga::<sagas::import_blocks_from_url::SagaImportBlocksFromUrl>(saga_params)
.await?;
Ok(())
}
/// Move a disk from the "ImportReady" state to the "Importing" state,
/// blocking any import from URL jobs.
pub async fn disk_manual_import_start(
self: &Arc<Self>,
opctx: &OpContext,
disk_lookup: &lookup::Disk<'_>,
) -> UpdateResult<()> {
let authz_disk: authz::Disk;
let db_disk: db::model::Disk;
(.., authz_disk, db_disk) =
disk_lookup.fetch_for(authz::Action::Modify).await?;
let disk_state: DiskState = db_disk.state().into();
match disk_state {
DiskState::ImportReady => {
// ok
}
_ => {
return Err(Error::invalid_request(&format!(
"cannot set disk in state {:?} to {:?}",
disk_state,
DiskState::ImportingFromBulkWrites.label()
)));
}
}
self.db_datastore
.disk_update_runtime(
opctx,
&authz_disk,
&db_disk.runtime().importing_from_bulk_writes(),
)
.await
.map(|_| ())
}
/// Bulk write some bytes into a disk that's in state ImportingFromBulkWrites
pub async fn disk_manual_import(
self: &Arc<Self>,
disk_lookup: &lookup::Disk<'_>,
param: params::ImportBlocksBulkWrite,
) -> UpdateResult<()> {
let db_disk: db::model::Disk;
(.., db_disk) = disk_lookup.fetch_for(authz::Action::Modify).await?;
let disk_state: DiskState = db_disk.state().into();
match disk_state {
DiskState::ImportingFromBulkWrites => {
// ok
}
_ => {
return Err(Error::invalid_request(&format!(
"cannot import blocks with a bulk write for disk in state {:?}",
disk_state,
)));
}
}
if let Some(endpoint) = db_disk.pantry_address() {
let data: Vec<u8> = base64::Engine::decode(
&base64::engine::general_purpose::STANDARD,
¶m.base64_encoded_data,
)
.map_err(|e| {
Error::invalid_request(&format!(
"error base64 decoding data: {}",
e
))
})?;
info!(
self.log,
"bulk write of {} bytes to offset {} of disk {} using pantry endpoint {:?}",
data.len(),
param.offset,
db_disk.id(),
endpoint,
);
// The the disk state can change between the check above and here
// because there's no disk state change associated with this write.
// I believe that toggling the disk's state and generation number in
// the DB is too expensive to be done for every 512k chunk of a disk
// (or whatever the eventual import chunk size is), however I didn't
// actually measure it.
//
// For example, between the check against state
// ImportingFromBulkWrites and here, the user could have called the
// bulk-write-stop endpoint, which would put the disk into state
// ImportReady. They could have then called the finalize endpoint,
// which would kick off the disk finalizing saga: set the state to
// finalzing, optionally take a snapshot, detach it from the
// associated Pantry, and set the state to Detached.
//
// In this scenario the write here would fail because the volume
// would have been detached from the Pantry, but say that the user
// instead called bulk-write-stop, then import, thereby kicking off
// the import blocks from URL saga? The Pantry locks its internal
// entry by the ID of the volume, so at least the requests would be
// ordered by their arrival time: either this bulk write would land
// first, or the import would land first:
//
// - if this bulk write landed first, the import would likely
// overwrite it with blocks from a URL.
//
// - if the import landed first, then the bulk write would overwrite
// what the import job imported, probably corrupting the data.
//
// I also believe it's not correct to use a saga here. Really, the
// user's cli (or any other program really) is responsible for this
// bulk write operation, not Nexus. If the bulk_write call below
// fails, then that failure would be propagated up to the user, and
// that user's program can act accordingly. In a way, the user's
// program is an externally driven saga instead.
let client = crucible_pantry_client::Client::new(&format!(
"http://{}",
endpoint
));
let request = crucible_pantry_client::types::BulkWriteRequest {
offset: param.offset,
base64_encoded_data: param.base64_encoded_data,
};
client
.bulk_write(&db_disk.id().to_string(), &request)
.await
.map_err(|e| match e {
crucible_pantry_client::Error::ErrorResponse(rv) => {
match rv.status() {
status if status.is_client_error() => {
Error::invalid_request(&rv.message)
}
_ => Error::internal_error(&rv.message),
}
}
_ => Error::internal_error(&format!(
"error sending bulk write to pantry: {}",
e,
)),
})?;
Ok(())
} else {
error!(self.log, "disk {} has no pantry address!", db_disk.id());
Err(Error::internal_error(&format!(
"disk {} has no pantry address!",
db_disk.id(),
)))
}
}
/// Move a disk from the "ImportingFromBulkWrites" state to the
/// "ImportReady" state, usually signalling the end of manually importing
/// blocks.
pub async fn disk_manual_import_stop(
self: &Arc<Self>,
opctx: &OpContext,
disk_lookup: &lookup::Disk<'_>,
) -> UpdateResult<()> {
let authz_disk: authz::Disk;
let db_disk: db::model::Disk;
(.., authz_disk, db_disk) =
disk_lookup.fetch_for(authz::Action::Modify).await?;
let disk_state: DiskState = db_disk.state().into();
match disk_state {
DiskState::ImportingFromBulkWrites => {
// ok
}
_ => {
return Err(Error::invalid_request(&format!(
"cannot set disk in state {:?} to {:?}",
disk_state,
DiskState::ImportReady.label()
)));
}
}
self.db_datastore
.disk_update_runtime(
opctx,
&authz_disk,
&db_disk.runtime().import_ready(),
)
.await
.map(|_| ())
}
/// Move a disk from the "ImportReady" state to the "Detach" state, making
/// it ready for general use.
pub async fn disk_finalize_import(
self: &Arc<Self>,
opctx: &OpContext,
disk_lookup: &lookup::Disk<'_>,
finalize_params: ¶ms::FinalizeDisk,
) -> UpdateResult<()> {
let (authz_silo, authz_proj, authz_disk, db_disk) =
disk_lookup.fetch_for(authz::Action::Modify).await?;
let saga_params = sagas::finalize_disk::Params {
serialized_authn: authn::saga::Serialized::for_opctx(opctx),
silo_id: authz_silo.id(),
project_id: authz_proj.id(),
disk_id: authz_disk.id(),
disk_name: db_disk.name().clone(),
snapshot_name: finalize_params.snapshot_name.clone(),
};
self.execute_saga::<sagas::finalize_disk::SagaFinalizeDisk>(
saga_params,
)
.await?;
Ok(())
}
}