Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_o3db_sync/src/file/refusal.rs

8.7 KiB, 25 runs

created by r1870400018:70844, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! The refusal marker (2026-10-10, QA F1). The kernel reports a write-back error to the first
2//! caller that asks and to no later one, so an open refused over such an error, or a run stopped
3//! by one, cannot rely on the next open being told again: a restart by systemd, or another
4//! program's `syncfs` on the same filesystem, would consume the report, and the open after it
5//! would replay tails that may not be on the disk. The marker makes the refusal outlast the
6//! report. Only an operator clears it, and clearing it says that the files in doubt were checked,
7//! or that the store was restored.
8
9use crate::{
10 prelude::*,
11 comm::channels::{
12 Cut,
13 FaultCause,
14 },
15 file::zdir::{
16 sync_dir,
17 write_whole,
18 },
19 test::hooks,
20};
21
22use oxedyne_fe2o3_core::file::ExclusiveLock;
23
24use std::{
25 path::{
26 Path,
27 PathBuf,
28 },
29 time::{
30 SystemTime,
31 UNIX_EPOCH,
32 },
33};
34
35/// The marker in a store's root that refuses every open of the store while it stands.
36pub struct Refusal;
37
38impl Refusal {
39 pub fn path(root: &Path) -> PathBuf {
40 root.join(constant::REFUSAL_FILENAME)
41 }
42
43 /// Leaves the marker, naming `why`, the run-time fault's `cause` if a fault left it, and
44 /// every file in doubt in `cuts`, durably: through a synced temporary file, renamed, and the
45 /// root synced. The cause and cut lines are the fault record's, so the marker alone can
46 /// stand in for a record that was lost or never written.
47 pub fn write(root: &Path, why: &str, cause: Option<FaultCause>, cuts: &[Cut])
48 -> Outcome<PathBuf>
49 {
50 if hooks::fault_record_fails() {
51 return Err(err!("The refusal marker could not be written (test::hooks)."; IO, Write));
52 }
53 let secs = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(0);
54 let mut text = fmt!("when {}\nwhy {}\n", secs, why.replace('\n', " "));
55 if let Some(c) = cause {
56 text.push_str(&fmt!("cause {}\n", c.word()));
57 }
58 for c in cuts {
59 text.push_str(&c.line());
60 text.push('\n');
61 }
62 write_whole(root, constant::REFUSAL_FILENAME, &text, None)
63 }
64
65 /// Leaves the marker for the open refused by `e`, and returns `e` saying whether it was left.
66 pub(crate) fn refuse(root: &Path, e: Error<ErrTag>) -> Error<ErrTag> {
67 match Self::write(root, &e.plain(), None, &[]) {
68 Ok(path) => err!(e,
69 "The refusal is kept in {:?}, so every later open of the store is refused too, \
70 since the kernel will not report the error again. Once the files in doubt are \
71 checked, or the store is restored, clear it with `Refusal::clear` \
72 (`o3db_verify --clear-refusal --o3db DIR`).", path;
73 Init, File, Fatal),
74 Err(w) => err!(e,
75 "CRITICAL: the refusal marker could not be written either ({}), so a later open \
76 may be told nothing of the error and replay what is not on the disk. Do not \
77 open the store at {:?} again before its files are checked, or it is restored.",
78 w, root;
79 Init, File, Fatal),
80 }
81 }
82
83 /// Refuses the open of the store at `root` while its marker stands.
84 pub(crate) fn check(root: &Path) -> Outcome<()> {
85 let path = Self::path(root);
86 if !path.exists() {
87 return Ok(());
88 }
89 let why = match std::fs::read_to_string(&path) {
90 Ok(text) => text.lines()
91 .find_map(|l| l.strip_prefix("why ").map(String::from))
92 .unwrap_or_default(),
93 Err(e) => fmt!("(the marker cannot be read: {})", e),
94 };
95 Err(err!(
96 "The store at {:?} refuses to open while {:?} stands: {} The kernel reports a \
97 write-back error only once, so this open could not see it again and would replay \
98 what may not be on the disk. Once the files in doubt are checked, or the store is \
99 restored, clear the refusal with `Refusal::clear` (`o3db_verify --clear-refusal \
100 --o3db DIR`).", root, path, why;
101 Init, File, Fatal))
102 }
103
104 /// The operator's verb: removes the marker, durably, so the store opens again. Calling it
105 /// says that the files in doubt were checked, or that the store was restored; nothing here
106 /// checks either. The marker's cuts are first folded into the fault record, durably, so the
107 /// next open still cuts every file in doubt even when the record had been lost (QA r2 F2).
108 /// It takes the store's lock, and refuses while another open holds the store, whose files
109 /// may still change. Returns whether there was a marker.
110 pub fn clear(root: &Path) -> Outcome<bool> {
111 let path = Self::path(root);
112 if !path.exists() {
113 return Ok(false);
114 }
115 let lock_path = root.join(constant::STORE_LOCK_FILENAME);
116 let _lock = match res!(ExclusiveLock::try_acquire(&lock_path)) {
117 Some(l) => l,
118 None => return Err(err!(
119 "The refusal of the Ozone store at {:?} is not cleared: the store is held by \
120 another open, in another process or this one ({:?} is locked), whose files may \
121 still change. Stop the one holding it, then clear.", root, lock_path;
122 Init, Lock, Conflict)),
123 };
124 let marker = match std::fs::read_to_string(&path) {
125 Ok(text) => text,
126 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(false),
127 Err(e) => return Err(err!(e,
128 "Cannot read the refusal marker {:?} to fold its cuts into the fault record.", path;
129 IO, File, Read)),
130 };
131 let folded = res!(Self::fold(root, &marker));
132 match std::fs::remove_file(&path) {
133 Ok(()) => (),
134 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(false),
135 Err(e) => return Err(err!(e,
136 "Cannot remove the refusal marker {:?}.", path;
137 IO, File, Write)),
138 }
139 res!(sync_dir(root));
140 warn!(sync_log::stream(),
141 "The refusal of the store at {:?} was cleared: the operator holds that the files in \
142 doubt were checked, or that the store was restored. The next open cuts these files \
143 in doubt (zone, file number, data and index marks): {:?}", root, folded);
144 Ok(true)
145 }
146
147 // Copies the cut lines of the marker text `marker` that the fault record lacks into the
148 // record, written whole, and returns every cut line the record now holds.
149 fn fold(root: &Path, marker: &str) -> Outcome<Vec<String>> {
150 let cuts: Vec<&str> = marker.lines().filter(|l| l.starts_with("cut ")).collect();
151 let rpath = root.join(constant::FAULT_FILENAME);
152 let mut text = match std::fs::read_to_string(&rpath) {
153 Ok(text) => text,
154 Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
155 if cuts.is_empty() {
156 return Ok(Vec::new());
157 }
158 // The record was lost, or never written: the marker's own lines head it.
159 let mut head = String::new();
160 for l in marker.lines() {
161 if let Some(w) = l.strip_prefix("when ") {
162 head.push_str(&fmt!("when {}\n", w));
163 } else if let Some(w) = l.strip_prefix("cause ") {
164 head.push_str(&fmt!("cause {}\n", w));
165 } else if let Some(w) = l.strip_prefix("why ") {
166 head.push_str(&fmt!("error {}\n", w));
167 }
168 }
169 head
170 },
171 Err(e) => return Err(err!(e,
172 "Cannot read the fault record {:?} to fold the refusal marker's cuts into it, so \
173 the marker is kept.", rpath;
174 IO, File, Read)),
175 };
176 if !text.is_empty() && !text.ends_with('\n') {
177 text.push('\n');
178 }
179 let mut added = false;
180 for l in &cuts {
181 if !text.lines().any(|r| r == *l) {
182 text.push_str(l);
183 text.push('\n');
184 added = true;
185 }
186 }
187 if added {
188 if let Err(e) = write_whole(root, constant::FAULT_FILENAME, &text, None) {
189 return Err(err!(e,
190 "Cannot fold the refusal marker's cuts into the fault record {:?}, so the \
191 marker is kept.", rpath;
192 IO, File, Write));
193 }
194 }
195 Ok(text.lines().filter(|l| l.starts_with("cut ")).map(String::from).collect())
196 }
197}