Skip to content
Merged
11 changes: 10 additions & 1 deletion nexus/db-model/src/disk_type_local_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ pub struct DiskTypeLocalStorage {

impl DiskTypeLocalStorage {
/// Creates a new `DiskTypeLocalStorage`. Returns Err if the computed
/// required dataset overhead does not fit in a `ByteCount`.
/// required dataset overhead does not fit in a `ByteCount`, or if the
/// requested size plus the required overhead does not fit in a `ByteCount`.
pub fn new(
disk_id: Uuid,
size: external::ByteCount,
Expand All @@ -56,6 +57,14 @@ impl DiskTypeLocalStorage {
let required_dataset_overhead =
external::ByteCount::try_from(overhead)?;

// Check that later `required_dataset_size` calls won't overflow.
let size_bytes: i64 = size.into();
let overhead_bytes: i64 = required_dataset_overhead.into();

if size_bytes.checked_add(overhead_bytes).is_none() {
return Err(external::ByteCountRangeError::TooLarge);
}

Ok(DiskTypeLocalStorage {
disk_id,
required_dataset_overhead: required_dataset_overhead.into(),
Expand Down
19 changes: 18 additions & 1 deletion nexus/db-queries/src/db/datastore/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,27 @@ impl LocalStorageDisk {
self.model().slot()
}

pub fn required_dataset_overhead(&self) -> external::ByteCount {
fn required_dataset_overhead(&self) -> external::ByteCount {
self.disk_type_local_storage.required_dataset_overhead()
}

/// Size required for the disk's data plus overhead
pub fn required_dataset_size(&self) -> i64 {
// The value in ByteCount is capped at i64::MAX, but a disk backed by
// local storage cannot be created that does not fit in a zpool at the
// time of the disk creation request (note this is different from the
// time of instance reservation where Nexus actually assigns the disk an
// allocation). This means that the size is well below that MAX, meaning
// this won't overflow.
//
// Additionally, there's a checked_add in DiskTypeLocalStorage::New that
// checks this won't happen. Note this won't protect against cases of
// database modification, but not much will!

self.size().to_bytes() as i64
+ self.required_dataset_overhead().to_bytes() as i64
Comment thread
hawkw marked this conversation as resolved.
Resolved
}

/// Return the full path to the local storage zvol's device
pub fn zvol_path(&self) -> Result<String, Error> {
let Some(allocation) = &self.local_storage_dataset_allocation else {
Expand Down
Loading
Loading