vmm: config: Expose disk lock granularity option

Add a per-disk lock_granularity parameter that lets users choose
between byte-range OFD locks and whole-file OFD locks:

  --disk path=/foo.img,lock_granularity=byte-range
  --disk path=/bar.img,lock_granularity=full

Byte-range is the default and matches QEMU behavior, working
best with storage backends where whole-file OFD locks are treated
as mandatory. The full option restores the original whole-file
locking for environments that depend on it.

The LockGranularityChoice enum and its FromStr impl live in the
block crate alongside the existing LockGranularity type. The
Block device converts the user-facing choice to the internal
LockGranularity at lock time, keeping device_manager.rs simple.

Closes: #7553

Signed-off-by: Victor Vieux <vieux@repl.it>
This commit is contained in:
Victor Vieux
2026-03-06 12:09:09 -08:00
committed by Rob Bradford
parent da0d0a2090
commit 7c690ffec0
7 changed files with 91 additions and 20 deletions

View File

@@ -16,6 +16,7 @@
use std::fmt::Debug;
use std::io;
use std::os::fd::{AsRawFd, RawFd};
use std::str::FromStr;
use thiserror::Error;
@@ -140,6 +141,38 @@ impl LockGranularity {
}
}
/// User-facing choice for the lock granularity.
///
/// This allows external management software to create snapshots of the disk
/// image. Without a byte-range lock, some NFS implementations may treat the
/// entire file as exclusively locked and prevent such operations (e.g. NetApp).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum LockGranularityChoice {
/// Byte-range lock covering [0, size).
#[default]
ByteRange,
/// Whole-file lock (l_start=0, l_len=0) - original OFD whole-file lock behavior.
Full,
}
/// Error returned when parsing a [`LockGranularityChoice`] from a string.
#[derive(Error, Debug)]
#[error("Invalid lock granularity value: {0}, expected 'byte-range' or 'full'")]
pub struct LockGranularityParseError(String);
impl FromStr for LockGranularityChoice {
type Err = LockGranularityParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"byte-range" => Ok(LockGranularityChoice::ByteRange),
"full" => Ok(LockGranularityChoice::Full),
_ => Err(LockGranularityParseError(s.to_owned())),
}
}
}
/// Returns a [`struct@libc::flock`] structure for the whole file.
const fn get_flock(lock_type: LockType, granularity: LockGranularity) -> libc::flock {
libc::flock {