use std::io;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Point {
Preallocate,
MapSegment,
Link,
CheckpointSync,
RollAfterLink,
CheckpointMarkerSync,
CheckpointBeforeMarker,
RetentionRename,
RetentionMarkerSync,
RetentionCopySync,
RetentionDestinationSync,
RetentionRemove,
CheckpointWait,
SparePreallocate,
SpareMap,
SpareHeader,
RepairBeforeRename,
RepairAfterRename,
RestoreBeforeRename,
BetweenHeldChanges,
ReaderOpen,
ReaderSeal,
ReaderSealReread,
ReaderCheckpoint,
ReaderCheckpointIdentity,
InspectOpen,
InspectSeal,
InspectRescan,
AbandonedWriteSync,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
EarlyWriteback,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Pause {
CreatorWaitsForDirectoryLock,
CreatorWroteTemporary,
CreatorOpenedExisting,
CreatorFoundStale,
OwnerBeforeUnlink,
ObserverOpened,
ObserverRead,
ObserverHoldsProbe,
}
#[cfg(feature = "fault-injection")]
mod paused {
use super::Pause;
use std::cell::RefCell;
type Hook = Box<dyn FnMut(Pause)>;
thread_local! {
pub(super) static HOOK: RefCell<Option<Hook>> = const { RefCell::new(None) };
}
}
#[cfg(feature = "fault-injection")]
pub fn on_pause(hook: Option<Box<dyn FnMut(Pause)>>) {
let old = paused::HOOK.with(|h| std::mem::replace(&mut *h.borrow_mut(), hook));
drop(old);
}
#[cfg(feature = "fault-injection")]
pub(crate) fn pause(point: Pause) {
let hook = paused::HOOK.with(|h| h.borrow_mut().take());
if let Some(mut hook) = hook {
hook(point);
paused::HOOK.with(|h| {
let mut slot = h.borrow_mut();
if slot.is_none() {
*slot = Some(hook);
}
});
}
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn pause(_point: Pause) {}
#[cfg(feature = "fault-injection")]
mod armed {
use super::Point;
use std::cell::RefCell;
pub(super) type Hook = (Point, usize, Box<dyn FnOnce()>);
thread_local! {
pub(super) static ARMED: RefCell<Vec<(Point, i32)>> = const { RefCell::new(Vec::new()) };
pub(super) static HOOKS: RefCell<Vec<Hook>> = const { RefCell::new(Vec::new()) };
}
}
#[cfg(feature = "fault-injection")]
pub fn run_at(point: Point, skip: usize, action: impl FnOnce() + 'static) {
armed::HOOKS.with(|h| h.borrow_mut().push((point, skip, Box::new(action))));
}
#[cfg(feature = "fault-injection")]
pub(crate) fn hook(point: Point) {
let action = armed::HOOKS.with(|h| {
let mut h = h.borrow_mut();
let i = h.iter().position(|(p, _, _)| *p == point)?;
if h[i].1 > 0 {
h[i].1 -= 1;
return None;
}
Some(h.remove(i).2)
});
if let Some(action) = action {
action();
}
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn hook(_point: Point) {}
#[cfg(feature = "fault-injection")]
pub fn fail_next(point: Point, errno: i32) {
armed::ARMED.with(|a| a.borrow_mut().push((point, errno)));
}
#[cfg(feature = "fault-injection")]
pub fn clear() {
armed::ARMED.with(|a| a.borrow_mut().clear());
armed::HOOKS.with(|h| h.borrow_mut().clear());
background::ARMED
.lock()
.unwrap_or_else(|e| e.into_inner())
.clear();
publish::HOOK.with(|h| *h.borrow_mut() = None);
}
#[cfg(feature = "fault-injection")]
pub fn fail_next_background(point: Point, errno: i32) {
background::ARMED
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((point, errno));
}
#[cfg(feature = "fault-injection")]
mod background {
use super::Point;
use std::sync::{Arc, Condvar, Mutex};
pub(super) static ARMED: Mutex<Vec<(Point, i32)>> = Mutex::new(Vec::new());
pub(super) static HOLDS: Mutex<Vec<(Point, Arc<HoldState>)>> = Mutex::new(Vec::new());
#[derive(Debug, Default)]
pub(super) struct HoldState {
pub(super) flags: Mutex<(bool, bool)>,
pub(super) changed: Condvar,
}
}
#[cfg(feature = "fault-injection")]
#[derive(Debug)]
pub struct Hold {
point: Point,
state: std::sync::Arc<background::HoldState>,
}
#[cfg(feature = "fault-injection")]
pub fn hold_background(point: Point) -> Hold {
let state = std::sync::Arc::new(background::HoldState::default());
background::HOLDS
.lock()
.unwrap_or_else(|e| e.into_inner())
.push((point, std::sync::Arc::clone(&state)));
Hold { point, state }
}
#[cfg(feature = "fault-injection")]
impl Hold {
pub fn wait_reached(&self, timeout: std::time::Duration) -> bool {
let flags = self.state.flags.lock().unwrap_or_else(|e| e.into_inner());
let (flags, _) = self
.state
.changed
.wait_timeout_while(flags, timeout, |f| !f.0)
.unwrap_or_else(|e| e.into_inner());
flags.0
}
pub fn reached(&self) -> bool {
self.state.flags.lock().unwrap_or_else(|e| e.into_inner()).0
}
pub fn release(self) {
drop(self);
}
}
#[cfg(feature = "fault-injection")]
impl Drop for Hold {
fn drop(&mut self) {
background::HOLDS
.lock()
.unwrap_or_else(|e| e.into_inner())
.retain(|(p, s)| !(*p == self.point && std::sync::Arc::ptr_eq(s, &self.state)));
self.state.flags.lock().unwrap_or_else(|e| e.into_inner()).1 = true;
self.state.changed.notify_all();
}
}
#[cfg(feature = "fault-injection")]
pub(crate) fn hold_point(point: Point) {
let state = {
let mut holds = background::HOLDS.lock().unwrap_or_else(|e| e.into_inner());
match holds.iter().position(|(p, _)| *p == point) {
Some(i) => holds.remove(i).1,
None => return,
}
};
let mut flags = state.flags.lock().unwrap_or_else(|e| e.into_inner());
flags.0 = true;
state.changed.notify_all();
while !flags.1 {
flags = state.changed.wait(flags).unwrap_or_else(|e| e.into_inner());
}
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn hold_point(_point: Point) {}
#[cfg(feature = "fault-injection")]
pub(crate) fn inject_background(point: Point) -> io::Result<()> {
let mut armed = background::ARMED.lock().unwrap_or_else(|e| e.into_inner());
match armed.iter().position(|(p, _)| *p == point) {
Some(i) => Err(io::Error::from_raw_os_error(armed.remove(i).1)),
None => Ok(()),
}
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn inject_background(_point: Point) -> io::Result<()> {
Ok(())
}
#[cfg(feature = "fault-injection")]
pub(crate) fn inject(point: Point) -> io::Result<()> {
armed::ARMED.with(|a| {
let mut a = a.borrow_mut();
match a.iter().position(|(p, _)| *p == point) {
Some(i) => Err(io::Error::from_raw_os_error(a.remove(i).1)),
None => Ok(()),
}
})
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn inject(_point: Point) -> io::Result<()> {
Ok(())
}
#[cfg(feature = "fault-injection")]
mod publish {
use std::cell::RefCell;
thread_local! {
pub(super) static HOOK: RefCell<Option<Box<dyn FnOnce()>>> = const { RefCell::new(None) };
}
}
#[cfg(feature = "fault-injection")]
pub fn before_next_publish(hook: impl FnOnce() + 'static) {
publish::HOOK.with(|h| *h.borrow_mut() = Some(Box::new(hook)));
}
#[cfg(feature = "fault-injection")]
pub(crate) fn pause_before_publish() {
if let Some(hook) = publish::HOOK.with(|h| h.borrow_mut().take()) {
hook();
}
}
#[cfg(not(feature = "fault-injection"))]
#[inline(always)]
pub(crate) fn pause_before_publish() {}