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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
//! [`Store`]: one container on one device.
//!
//! A store owns the container file, the ownership lock, the device's flush
//! domain, the buffer pool and the queues, and the in-memory copy of the
//! volume record and region table. Region handles share it through an `Arc`.
//!
//! The metadata lock (`meta`) is taken only by lifecycle operations
//! (provision, release, lookup by name); reads and writes of region data never
//! take it.
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
pub use crate::v2::{DamagedSlot, Healed, MetadataReport, SlotDamage};
use store_io_buf::{BufPool, PoolConfig};
use store_io_core::class::{DurabilityClass, ReceiptLabel};
use store_io_core::decide::{ClassDecision, DurableOpen, Trust};
use store_io_core::errno::{Stage, classify};
use store_io_core::error::{
Error, ErrorContext, FirstCause, Named, NotWrittenCause, Op, OsError, ReserveTag,
};
use store_io_core::evidence::Evidence;
use store_io_core::id::{Generation, RegionId, VolumeId};
use store_io_core::observe::{Observer, Snapshot};
use store_io_format::meta::{RegionKind, RegionName, TableEntry, VolumeMeta};
use store_io_platform::{FileName, IoOp, IoPath, LinuxTier, Platform, Queue, QueueConfig};
use crate::domain::Domain;
use crate::exec::{Lane, QueueSet, Ran, run};
use crate::frontier::AppendFrontier;
use crate::gate::Gate;
use crate::layout::{Layout, TABLE_OBJECT, VOLUME_OBJECT};
use crate::slots::{NextSlot, PairAt, PairRead, write_slot};
use crate::space::Accounting;
/// The container file name inside the store directory.
pub const CONTAINER: &str = "store.sio";
/// Explicit, reported choices for a store. Every durability-affecting field
/// appears in [`Store::report`].
#[derive(Debug, Clone)]
pub struct StoreOptions {
/// Certificate, attestation and override inputs to the class decision.
pub trust: Trust,
/// Minimum block (slot) size in bytes; the device may require more.
pub block_size: Option<u32>,
/// Region-table slot size as a power of two (14 = 16 KiB: a format-2
/// table page of 169 regions; format 1's single table, 253).
pub log2_table_slot: u8,
/// Largest single I/O of the blocking layers, and the batch buffer size
/// (bytes; rounded up to a power of two, 128 KiB to 1 GiB).
pub max_io: usize,
/// Buffers per size class in the pool, up to `max_io` (at most
/// 16,777,215; more fails `create` and `open` with
/// `NotWritten(PoolExhausted)`).
pub buffers_per_class: u32,
/// Largest single write or read a `Submitter` takes, from one buffer, as
/// one OS write or read operation (bytes; rounded up to a power of two,
/// at most 1 GiB): the pool holds `large_buffers` buffers of that size,
/// used by submitters only (D-101). Ignored when not above `max_io`.
pub max_write: usize,
/// Buffers of `max_write` bytes in the pool (0: none, so a submitter's
/// largest operation is `max_io`; at most 16,777,215). The defaults add
/// 32 MiB on top of about 128 MiB of ordinary buffers per store: Windows
/// charges the whole pool to the commit limit at creation; on both
/// systems a buffer becomes resident when first used, and the most
/// recently returned buffer of a size is reused first, so only about as
/// many as are in use at once become resident. A pool too large to map
/// fails `create` and `open` with `NotWritten(PoolExhausted)`.
pub large_buffers: u32,
/// Queues shared by the blocking layers (0 = number of CPUs). On Linux
/// they perform their operations inline on either tier; the parallel
/// lanes, as many again and created on first use, are io_uring rings on
/// the io_uring tier, each holding two file descriptors (its ring and
/// its eventfd), as does each `Submitter`.
pub queues: usize,
/// Operations in flight per queue.
pub queue_depth: u32,
/// Worker threads for the platform's submit pool, which runs the
/// independent operations of a page batch or a large read concurrently
/// on the Linux synchronous tier (D-051, D-096); 0 = one per CPU, at most
/// 32. The pool is shared by every store on the platform and sized by the
/// first store that uses it (at most 256); other backends ignore this.
pub t3_pool_threads: u32,
/// How far, in bytes, a new append may start ahead of the oldest
/// incomplete one on the same region, in blocks: rounded down to a power
/// of two, or, on a region that cannot grow past a size smaller than the
/// window (format 1's regions, a capped format-2 region), that size
/// rounded up (so a region never limits its own appends); at least 2 and
/// at most 2^20 blocks. Each append region that has appended since the
/// open or its last recycle holds 8 bytes of memory per block of its
/// ring: a format-2 region that may grow (no `max_size`) holds the
/// window's whatever its size, 32 KiB at the default 16 MiB and 4 KiB
/// blocks (2 GiB for 65,536 such regions; a cap, or a smaller window,
/// bounds it). Default 16 MiB.
pub append_window: u64,
/// Fill new regions with the keyed incompressible pattern instead of
/// zeros (for thin, virtual, deduplicating or zero-detecting stacks).
pub keyed_fill: bool,
/// The kernel interface on Linux (D-082): `Auto` (io_uring where the
/// kernel accepts it), `Sync` or `Uring`. Read by the native API when
/// it makes the platform; the choice is in [`StoreReport::io`]. Other
/// systems ignore it.
pub linux_tier: LinuxTier,
/// Register the store's container file with each `Submitter`'s queue
/// where the backend can (D-078, D-107: on Linux's io_uring tier, a
/// read-only twin of it as a direct descriptor on the submitter's ring),
/// so its reads skip the per-operation file lookup in the kernel; writes
/// stay on the store's own descriptor, so a write in flight keeps the
/// store locked until it completes. Best effort: a
/// registration the kernel refuses is counted in `Store::io_path`, never
/// an error. On by default: the gates measured reads 1.03x to 1.08x of
/// an unregistered ring (G3d, CI); writes take the unregistered path.
/// Buffers
/// are never registered: reads into registered buffers hang forever on
/// some kernels (at least Linux 6.5.0 and 6.6.0), which no probe
/// detects (D-107).
pub register_file: bool,
/// Flushes the store runs at once (1 to 4; default 1, one in flight as
/// before, D-124). Several can overlap, meant for drives that execute
/// flushes in parallel (NVMe; not yet measured on one): each runs on a
/// handle and queue of its own, so each reports its own failures, and a
/// receipt still needs every flush up to its own to have succeeded.
/// Platforms that cannot keep flush failures per handle run one (Linux
/// 4.16 and later can); the number run is in
/// [`StoreReport::flushes_in_flight`]. Measure on the target drive
/// before raising it: at eight writers, 2 and 4 gained nothing on a
/// memory-backed device and lost on a virtual disk (1,314, 974 and 595
/// durable writes a second at 1, 2 and 4).
pub flushes_in_flight: u8,
/// Regions a format-2 store can hold, fixed at create (default 65,536:
/// a table of 12.1 MiB at 16 KiB table pages, written at create and
/// read at every open; smaller stores may ask for fewer; at most
/// 4,194,304). Ignored by format 1. Internal until the table's sizing
/// is settled (v0.5).
#[doc(hidden)]
pub region_capacity: u32,
/// The container format a create writes: 2 (the default). Format 1 is
/// for tests and migration fixtures only: a writable open of a format-1
/// store is refused (`Unsupported(FormatVersion)`; copy it to format 2
/// with `copy_store`), a read-only open proceeds.
#[doc(hidden)]
pub container_format: u16,
/// The options the `provision_{append,page}_region_default` calls (of
/// [`Store`] and [`Reservation`](crate::Reservation)) use (default
/// none: those calls are refused). An open-time choice, never stored:
/// each open may give others, and they apply to regions provisioned
/// from then on (an existing region keeps its size and cap). Checked
/// when used, so options a later call would refuse never fail an open.
pub region_defaults: Option<crate::RegionOptions>,
}
impl Default for StoreOptions {
fn default() -> Self {
Self {
trust: Trust::default(),
block_size: None,
log2_table_slot: 14,
max_io: 1 << 20,
buffers_per_class: 64,
max_write: 4 << 20,
large_buffers: 8,
queues: 0,
queue_depth: 64,
t3_pool_threads: 0,
append_window: 16 << 20,
keyed_fill: false,
linux_tier: LinuxTier::Auto,
register_file: true,
flushes_in_flight: 1,
region_capacity: 65_536,
container_format: store_io_format::slot::FORMAT_2,
region_defaults: None,
}
}
}
/// How durable writes are made durable on this store.
#[derive(Debug, Clone, Copy)]
pub(crate) struct Policy {
pub(crate) class: DurabilityClass,
pub(crate) label: ReceiptLabel,
/// Write with `dsync` (durable at completion): power-safe stores.
pub(crate) dsync: bool,
/// Barriers issue a device flush: every class except power-safe (where
/// only regions forcing data sync flush, D-102).
pub(crate) flush: bool,
}
pub use crate::report::{Repair, Unavailable, UnavailableRegion};
/// What the store learned about its device, and what it decided.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StoreReport {
/// Raw evidence from the platform.
pub evidence: Evidence,
/// The class decision.
pub decision: ClassDecision,
/// Block (slot) size in bytes.
pub block_size: u64,
/// Region-table capacity.
pub table_capacity: usize,
/// The volume identity.
pub volume: VolumeId,
/// Owner generation of this open (0 for read-only).
pub owner_generation: u64,
/// Whether the store was opened read-only.
pub read_only: bool,
/// The kernel interface the store's I/O uses, and why, as of create or
/// open. Queues made later (a `Submitter`'s, the parallel lanes, created
/// on first use) are counted live in [`Store::io_path`].
pub io: IoPath,
/// Regions the region table names that could not be opened, with the
/// reason (D-090), as the open found them (repairs since:
/// `Store::unavailable_regions`). Empty for a healthy store.
pub unavailable: Vec<UnavailableRegion>,
/// The container format (1 or 2).
pub format: u16,
/// Format 2: what the open found in the metadata (empty for format 1).
pub metadata: MetadataReport,
/// Flushes run at once ([`StoreOptions::flushes_in_flight`] as far as
/// the store and platform allow): 1 for a read-only store, a device that
/// needs no flush, or a platform that cannot keep flush failures per
/// handle; fewer where a handle for a further slot could not be made.
pub flushes_in_flight: u8,
}
/// In-memory metadata, guarded by the metadata lock.
pub(crate) struct Meta {
pub(crate) volume: VolumeMeta,
pub(crate) volume_pair: PairRead,
pub(crate) table_pair: PairRead,
pub(crate) next_region_id: u32,
pub(crate) entries: Vec<TableEntry>,
pub(crate) container_len: u64,
/// Format 2's metadata (the format-1 fields above are then unused).
pub(crate) v2: Option<Box<crate::v2::Meta2>>,
/// The regions the table names that cannot be used, as they stand now
/// (the open's list, less those repaired since): lookups, the names
/// taken and growth read this, never the open-time report.
pub(crate) unavailable: Vec<UnavailableRegion>,
/// What the store's repairs did, in order ([`Store::repairs`]).
pub(crate) repairs: Vec<crate::report::Repair>,
}
/// One live region.
pub(crate) struct LiveRegion {
pub(crate) id: RegionId,
pub(crate) name: RegionName,
pub(crate) kind: RegionKind,
pub(crate) generation: Generation,
/// Where the data lies in the container (every file offset of the
/// region's data comes from it).
/// Its length is the region's data size.
pub(crate) map: crate::regionmap::RegionMap,
/// The header pair's slot offsets.
pub(crate) header_at: [u64; 2],
pub(crate) header: Mutex<PairRead>,
pub(crate) frontier: Option<AppendFrontier>,
/// Append regions: whether the append position is known (true when
/// provisioned in this process; after a reopen, only after `resume_at`).
pub(crate) positioned: std::sync::atomic::AtomicBool,
/// Admission of data operations; closed by recycle and release.
pub(crate) gate: Gate,
/// Every barrier on the region issues a data flush, even on a
/// power-safe store (D-102). Runtime only: never on disk. Shared by
/// every generation of the region, so a setting made through any handle
/// holds after a recycle.
pub(crate) force_sync: Arc<std::sync::atomic::AtomicBool>,
/// Writes refused, the store being writable: format 2's regions on a
/// page that is not confirmed, possibly superseded, or with a header
/// feature this version may not write (always false in format 1).
/// Lifted by `accept_page_rollback` once every page is confirmed, never
/// for a superseded or foreign one.
pub(crate) read_only: std::sync::atomic::AtomicBool,
/// Possibly superseded (spec §9 step 4): another region's newer
/// footprint may cover its bytes; it stays read-only for good.
pub(crate) superseded: bool,
/// Its header's winner carries a read-only-compatible feature this
/// version does not know (spec §14): read-only for good too.
pub(crate) foreign: bool,
}
impl LiveRegion {
/// Whether barriers on this region always flush (D-102).
pub(crate) fn forced(&self) -> bool {
self.force_sync.load(std::sync::atomic::Ordering::Acquire)
}
/// Whether writes to the region are refused (see `read_only`).
pub(crate) fn is_read_only(&self) -> bool {
self.read_only.load(std::sync::atomic::Ordering::Acquire)
}
}
pub(crate) struct Inner<P: Platform> {
/// Declared first, so the queues drop first (fields drop in declaration
/// order): a queue whose operations run on worker threads waits for them
/// in its drop, and none may land after the ownership lock is released.
pub(crate) queues: QueueSet<P::Queue>,
/// The flush thread (D-099), started by the first submitter request
/// that needs a flush (a flush-required store, or a region forcing data
/// sync); `None` inside if the OS refused it. Declared
/// early too: it is stopped and joined before the file and the ownership
/// lock are released.
pub(crate) flusher: std::sync::OnceLock<Option<crate::flusher::Flusher<P>>>,
pub(crate) platform: P,
/// The store directory, held open for directory syncs.
pub(crate) _dir: P::Dir,
pub(crate) file: P::File,
/// Flush slots 1.. (D-124): a handle each, opened on the container
/// beside `file` (slot 0's). Empty with one slot.
pub(crate) flush_files: Box<[P::File]>,
pub(crate) _lock: P::Lock,
pub(crate) volume: VolumeId,
pub(crate) layout: Layout,
pub(crate) policy: Policy,
pub(crate) domain: Domain,
pub(crate) pool: BufPool,
pub(crate) meta: Mutex<Meta>,
pub(crate) regions: Mutex<Vec<Arc<LiveRegion>>>,
/// What the open found (never changed after: see `Meta::unavailable`
/// and `frozen` for what stands now).
pub(crate) report: StoreReport,
/// Allocation is frozen (format 2: a page is Unconfirmed or Lost), as
/// it stands now: set at open, only ever cleared afterwards (once no
/// page is stale), so a check made without the metadata lock can only
/// refuse what it might have allowed, never the reverse.
pub(crate) frozen: std::sync::atomic::AtomicBool,
pub(crate) read_only: bool,
pub(crate) owner_generation: u64,
pub(crate) opts: StoreOptions,
/// The largest ordinary pool buffer (`Store::ordinary`), fixed at open.
pub(crate) ordinary: usize,
/// One write's largest piece (`Store::chunk`): the ordinary buffer size,
/// in whole blocks; fixed at open.
pub(crate) chunk: usize,
/// Space accounting (lock order: `meta`, then `acct`).
pub(crate) acct: Mutex<Accounting>,
/// Unique per open store in this process: tickets and receipts of one
/// open never vouch for another's writes.
pub(crate) epoch: u64,
/// Per-stage timing and counters (zero-sized without feature `observe`).
pub(crate) observer: Observer,
/// What `oldest_outstanding` queries have seen (D-095).
pub(crate) seen: Mutex<crate::stats::Seen>,
/// Live submitters' busy marks (D-098).
pub(crate) submitters: Mutex<crate::stats::Submitters>,
}
/// A store: one container file on one device.
pub struct Store<P: Platform> {
pub(crate) inner: Arc<Inner<P>>,
}
impl<P: Platform> core::fmt::Debug for Store<P> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Store")
.field("volume", &self.inner.volume)
.field("class", &self.inner.policy.class)
.field("read_only", &self.inner.read_only)
.field("poisoned", &self.inner.domain.poisoned().is_some())
.finish()
}
}
impl<P: Platform> Clone for Store<P> {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
pub(crate) fn io(op: Op, raw: OsError) -> Error {
Error::Io {
op,
raw,
ctx: ErrorContext::default(),
}
}
pub(crate) fn lock_meta(m: &Mutex<Meta>) -> MutexGuard<'_, Meta> {
m.lock().unwrap_or_else(PoisonError::into_inner)
}
/// A random 128-bit identity. Uniqueness matters, secrecy does not: the
/// standard library's OS-seeded hasher keys, mixed with the clock and the
/// process id through SplitMix64.
pub(crate) fn random_uuid() -> [u8; 16] {
use std::hash::{BuildHasher, Hasher};
let mut h = std::collections::hash_map::RandomState::new().build_hasher();
h.write_u128(
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |d| d.as_nanos()),
);
h.write_u32(std::process::id());
let mut z = h.finish();
let mut out = [0u8; 16];
for chunk in out.chunks_exact_mut(8) {
z = z.wrapping_add(0x9E37_79B9_7F4A_7C15);
let mut x = z;
x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
chunk.copy_from_slice(&(x ^ (x >> 31)).to_le_bytes());
}
out
}
impl<P: Platform> Store<P>
where
P::Queue: Send,
{
/// The failure that poisoned the store, if one has: every write and
/// barrier fails with `Poisoned` from then on. A cheap check, for a
/// health probe or a monitor.
#[must_use]
pub fn poisoned(&self) -> Option<FirstCause> {
self.inner.domain.poisoned()
}
/// Event counters and sampled per-stage latencies since this handle was
/// opened. With feature `observe` off (the default) nothing is recorded
/// and the snapshot is all zeros.
#[must_use]
pub fn observe(&self) -> Snapshot {
self.inner.observer.snapshot()
}
/// What the store learned about its device and what it decided, as
/// the open found it (never changed after: what repairs change since
/// is in [`Self::unavailable_regions`], [`Self::frozen`] and
/// [`Self::repairs`]).
#[must_use]
pub fn report(&self) -> &StoreReport {
&self.inner.report
}
/// The kernel interface the store's I/O uses now, and why: as
/// [`StoreReport::io`], with every queue made since counted (a ring the
/// kernel refused for a `Submitter` or a parallel lane, and what
/// served instead).
#[must_use]
pub fn io_path(&self) -> IoPath {
self.inner.platform.io_path()
}
/// The volume identity.
#[must_use]
pub fn volume(&self) -> VolumeId {
self.inner.volume
}
/// Poisons the device domain (for a caller's watchdog that decided the
/// device is hung). Every later write and barrier fails with
/// [`Error::Poisoned`]; reads keep working.
pub fn poison(&self) {
self.inner.domain.poison(FirstCause {
op: None,
raw: None,
});
for r in self.regions_snapshot() {
if let Some(f) = &r.frontier {
f.wake_all();
}
}
}
/// Flush-sharing counters of the device domain.
#[must_use]
pub fn domain_stats(&self) -> crate::domain::DomainStats {
self.inner.domain.stats()
}
/// Worker threads in the submit pool that runs this store's page batches
/// and large reads concurrently, once this store's first such call has
/// started its parallel queues (the pool may have been started earlier by
/// another store on the same platform, and stays `None` if they could
/// not be started);
/// `None` before that, or on a backend that needs no pool (IOCP, the
/// simulator).
#[must_use]
pub fn submit_pool_threads(&self) -> Option<usize> {
self.inner.queues.parallel_workers()
}
pub(crate) fn regions_snapshot(&self) -> Vec<Arc<LiveRegion>> {
self.inner
.regions
.lock()
.unwrap_or_else(PoisonError::into_inner)
.clone()
}
/// Names and kinds of the ready regions.
#[must_use]
pub fn regions(&self) -> Vec<(String, RegionKind)> {
self.regions_snapshot()
.iter()
.map(|r| (r.name.as_str().to_owned(), r.kind))
.collect()
}
pub(crate) fn find(
&self,
name: &str,
kind: RegionKind,
what: Named,
) -> Result<Arc<LiveRegion>, Error> {
let name = RegionName::new(name).map_err(|_| Error::NotWritten {
cause: NotWrittenCause::InvalidName,
ctx: ErrorContext::default(),
})?;
if let Some(r) = self
.regions_snapshot()
.into_iter()
.find(|r| r.name == name && r.kind == kind)
{
return Ok(r);
}
// A region the table names but open could not use is corrupt, not
// missing: a caller must never mistake it for a fresh start (D-090).
match lock_meta(&self.inner.meta)
.unavailable
.iter()
.find(|u| u.name == name.as_str() && u.kind == kind)
.cloned()
{
Some(u) => Err(Error::Corruption {
kind: u.reason.corruption_kind(),
ctx: ErrorContext {
region: Some(u.id),
range: None,
},
}),
None => Err(Error::NotFound { what }),
}
}
/// Regions the region table names that open could not use, with the
/// reason (D-090), less any repaired since. Empty for a healthy store.
/// Their names stay reserved.
#[must_use]
pub fn unavailable_regions(&self) -> Vec<UnavailableRegion> {
lock_meta(&self.inner.meta).unavailable.clone()
}
/// Whether allocation is frozen now (format 2: a table page is stale
/// or lost; spec §9 step 2): no provisioning, no growth beyond
/// capacity, no release, no repair. `report().metadata.frozen` is what
/// the open found.
#[must_use]
pub fn frozen(&self) -> bool {
self.inner.frozen.load(std::sync::atomic::Ordering::Acquire)
}
/// What this handle's repairs and accepts did, in order
/// ([`Self::repair_unavailable`], [`Self::accept_page_rollback`]): the
/// store's log of them, for the caller to record. The library prints
/// nothing.
#[must_use]
pub fn repairs(&self) -> Vec<crate::report::Repair> {
lock_meta(&self.inner.meta).repairs.clone()
}
}
/// Writes one slot durably according to the store's policy: a `dsync` write
/// on power-safe devices; a plain write plus a domain barrier otherwise.
/// Returns the pair's new state, derived from `prev` and the bytes written;
/// the device is never read back (D-069), so once the write is durable no
/// later step can fail or report the old version as current.
// The pair (location, previous state), the slot to write and the store's I/O
// context are all needed and none derives from another.
#[allow(clippy::too_many_arguments)]
pub(crate) fn durable_slot<P: Platform>(
q: &mut Lane<P::Queue>,
file: &P::File,
pool: &BufPool,
at: &PairAt,
prev: &PairRead,
next: &NextSlot<'_>,
policy: &Policy,
domain: &Domain,
) -> Result<PairRead, Error> {
if let Some(first) = domain.poisoned() {
return Err(Error::Poisoned { first });
}
let written = write_slot(q, file, pool, at, next, policy.dsync).inspect_err(|e| {
if matches!(e, Error::DurabilityUnknown { .. }) {
domain.poison(FirstCause {
op: Some(Op::Write),
raw: None,
});
}
})?;
if policy.flush {
let need = domain.ticket();
// Through this lane and the main handle: slot 0's.
domain.barrier_main(need, || flush_data(q, file))?;
}
Ok(prev.after(written))
}
/// One device flush through a queue. A flush refused without an OS error
/// (a full queue) carries no code: none is invented.
pub(crate) fn flush_data<Q: Queue>(q: &mut Lane<Q>, file: &Q::File) -> Result<(), Option<OsError>> {
match run(q, IoOp::FlushData { file }) {
Ok(Ran::Done { result, .. }) => result.map(|_| ()).map_err(Some),
Ok(Ran::Abandoned(raw)) => Err(Some(raw)),
Err((_b, raw)) => Err(raw),
}
}
pub(crate) fn container_name() -> Result<FileName, Error> {
FileName::new(CONTAINER).map_err(|_| Error::NotWritten {
cause: NotWrittenCause::InvalidName,
ctx: ErrorContext::default(),
})
}
pub(crate) fn refuse(d: &ClassDecision) -> Result<(), Error> {
match d.durable_open {
DurableOpen::Allowed | DurableOpen::Overridden => Ok(()),
DurableOpen::RefusedUnsafe => Err(Error::UnsafeDevice { reasons: d.reasons }),
DurableOpen::RefusedUnverified => Err(Error::Unverified {
missing: d.missing,
reasons: d.reasons,
}),
}
}
pub(crate) fn policy_of(d: &ClassDecision) -> Policy {
let power_safe = d.class == DurabilityClass::PowerSafe;
Policy {
class: d.class,
label: d.label,
dsync: power_safe,
flush: !power_safe,
}
}
pub(crate) fn make_pool(
ev: &Evidence,
layout: &Layout,
opts: &StoreOptions,
large: bool,
) -> Result<BufPool, Error> {
let align = [
4096usize,
ev.fs.dio_mem_align as usize,
ev.device.logical_block as usize,
]
.into_iter()
.max()
.unwrap_or(4096)
.next_power_of_two();
let min = (layout.block() as usize).max(align);
let max = io_max(opts).max(min);
let mut cfg = PoolConfig::uniform(align, min, max, opts.buffers_per_class.max(4));
// One class of larger buffers for single large submitter operations
// (D-101), fewer of them; a request between the two sizes takes one.
// Never in the short-lived pools that read the metadata at open.
let size = large_size(opts);
if large && opts.large_buffers > 0 && size > max {
cfg.classes.push(store_io_buf::ClassSpec {
size,
count: opts.large_buffers,
});
}
BufPool::new(&cfg).map_err(|_| Error::NotWritten {
cause: NotWrittenCause::PoolExhausted,
ctx: ErrorContext::default(),
})
}
/// The largest single I/O anything may ask for: 1 GiB (above 2 GiB a single
/// `pwrite` transfers short, above 4 GiB `WriteFile` refuses).
const LARGEST_IO: usize = 1 << 30;
/// The largest ordinary pool buffer, which the blocking layers never exceed:
/// `max_io` rounded up to a power of two, between 128 KiB and 1 GiB.
pub(crate) fn io_max(opts: &StoreOptions) -> usize {
opts.max_io.clamp(2 << 16, LARGEST_IO).next_power_of_two()
}
/// The large class's buffer size: `max_write` rounded up to a power of two,
/// at most 1 GiB (no large class when that is not above [`io_max`]).
pub(crate) fn large_size(opts: &StoreOptions) -> usize {
opts.max_write.clamp(1, LARGEST_IO).next_power_of_two()
}
pub(crate) fn make_queues<P: Platform>(
platform: &P,
opts: &StoreOptions,
flush_lanes: usize,
) -> Result<QueueSet<P::Queue>, Error> {
let n = if opts.queues == 0 {
std::thread::available_parallelism().map_or(4, std::num::NonZeroUsize::get)
} else {
opts.queues
};
let cfg = QueueConfig {
depth: opts.queue_depth.max(1),
..QueueConfig::default()
};
let mut qs = Vec::with_capacity(n);
for _ in 0..n {
qs.push(platform.queue(cfg).map_err(|e| io(Op::Queue, e))?);
}
// One more queue per flush slot, used only by that slot's flush leader
// (D-070, D-124).
let mut flush = Vec::with_capacity(flush_lanes);
for _ in 0..flush_lanes.max(1) {
flush.push(platform.queue(cfg).map_err(|e| io(Op::Queue, e))?);
}
Ok(QueueSet::with_flush_lanes(qs, flush, cfg.depth as usize))
}
/// The flush slots a store runs (D-124): `opts.flushes_in_flight` clamped to
/// `1..=MAX_FLUSH_SLOTS`, and 1 unless the platform reports flush failures
/// per handle.
pub(crate) fn flush_slots<P: Platform>(platform: &P, opts: &StoreOptions) -> usize {
let asked = usize::from(opts.flushes_in_flight).clamp(1, crate::domain::MAX_FLUSH_SLOTS);
if asked > 1 && platform.flush_errors_per_handle() {
asked
} else {
1
}
}
pub(crate) fn space_err(op: Op, raw: OsError, tag: u32) -> Error {
match classify(Stage::Space, raw) {
store_io_core::errno::ErrorKind::NoSpace => Error::NoSpace {
tag: ReserveTag::Caller(tag),
ctx: ErrorContext::default(),
},
_ => io(op, raw),
}
}
pub(crate) fn meta_err() -> Error {
Error::Corruption {
kind: store_io_core::error::CorruptionKind::Metadata,
ctx: ErrorContext::default(),
}
}
pub(crate) fn corrupt() -> Error {
meta_err()
}
pub(crate) fn pair_volume(volume: VolumeId, l: &Layout) -> PairAt {
let (a, b) = l.volume_slots();
PairAt {
volume: *volume.as_bytes(),
object: VOLUME_OBJECT,
a,
b,
log2: l.log2_block,
format: l.format,
numbered: l.format == store_io_format::slot::FORMAT_2,
}
}
pub(crate) fn pair_table(volume: VolumeId, l: &Layout) -> PairAt {
let (a, b) = l.table_slots();
PairAt {
volume: *volume.as_bytes(),
object: TABLE_OBJECT,
a,
b,
log2: l.log2_table,
format: l.format,
numbered: false,
}
}
pub(crate) fn pair_region(volume: VolumeId, l: &Layout, e: &TableEntry) -> PairAt {
PairAt {
volume: *volume.as_bytes(),
object: l.region_object(u64::from(e.region_id)),
a: e.offset,
b: e.offset + l.block(),
log2: l.log2_block,
format: l.format,
numbered: false,
}
}