store-io-engine 0.5.1

The store-io engine: volumes, regions, barriers with flush sharing, receipts, provisioning and A/B slot objects over any store-io platform.
Documentation
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
//! Container format 2 (`docs/FORMAT.md`, Part I): the paged region
//! table's metadata, create, the open's tear judgement, region checks
//! (`open2`) and healing, and the clean close. Built beside format 1 (D-116
//! makes format 1 read-only once creation switches, v0.5 E5).

use store_io_core::error::{Capability, Error, ErrorContext, Op};
use store_io_core::id::{RegionId, VolumeId};
use store_io_format::judge::{self, PairVerdict, SlotVerdict, Standing};
use store_io_format::meta::VolumeMeta;
use store_io_format::meta2::{
    PageHeader, RawEntry, VOLUME_META2_LEN, VolumeMeta2, decode_page, encode_page, entries_per_page,
};
use store_io_format::slot::{FORMAT_2, SlotIndex, SlotStatus};
use store_io_platform::Platform;

use crate::domain::Domain;
use crate::exec::Lane;
use crate::layout::Layout;
use crate::slots::{PairAt, PairRead, plan_next, read_pair, read_pairs, reread_failed, write_slot};
use crate::store::*;

/// The most regions a format-2 store can be created for (a table of
/// 24,819 pages, two copies each: 776 MiB at 16 KiB pages, all read at
/// every open); a larger `region_capacity` is
/// clamped, and [`StoreReport::table_capacity`] says what was made.
pub const MAX_REGION_CAPACITY: u32 = 1 << 22;

/// What an open made of a damaged metadata slot
/// ([`MetadataReport::damaged_slots`], spec §7).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SlotDamage {
    /// The winner's predecessor, damaged: reported only (its pair stands).
    Older,
    /// The one write a crash left torn: a writable open heals it
    /// ([`MetadataReport::healed`]; a read-only open leaves it).
    InFlight,
    /// Damage a crash cannot explain: its pair stale or lost.
    Damage,
}

/// A metadata slot an open found damaged.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct DamagedSlot {
    /// The pair: `None` for the volume record, `Some(p)` for table page `p`.
    pub page: Option<u32>,
    /// Slot B (`true`) or slot A.
    pub slot_b: bool,
    /// What the judgement made of it.
    pub damage: SlotDamage,
}

/// What a format-2 open found in its metadata pairs (the volume record and
/// the table pages).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MetadataReport {
    /// The last writer closed cleanly: its final volume record is intact
    /// and says so (a damaged or in-flight volume record voids it).
    pub clean_close: bool,
    /// Pages whose newest copy is damaged (Unconfirmed): their regions are
    /// read-only and allocation is frozen.
    pub stale_pages: Vec<u32>,
    /// Pages with no usable copy (Lost), or whose copy fails the page
    /// checks: their regions are unknown and allocation is frozen.
    pub lost_pages: Vec<u32>,
    /// The volume record's newest copy was damaged (a writable open
    /// replaces it).
    pub volume_stale: bool,
    /// The one metadata write a crash left torn, which this open rewrote.
    pub healed: Option<Healed>,
    /// Allocation is frozen: some page is stale or lost (as the open found
    /// it; now: `Store::frozen`).
    pub frozen: bool,
    /// Why a writable open was made read-only, if it was.
    pub read_only_reason: Option<&'static str>,
    /// Metadata pairs read a second time because a slot read failed
    /// (each failed read is retried once).
    pub retried_reads: u32,
    /// Every candidate and every damaged slot of the volume record and the
    /// table pages (spec §9), in pair order, slot A first. A lost pair's
    /// slots are all damage; a page whose winner fails the page checks
    /// counts its winner as damage too.
    pub damaged_slots: Vec<DamagedSlot>,
    /// Table entries whose kind or name do not decode, as `(page, seq)`:
    /// unavailable, their spans reserved (the regions with a readable name
    /// are in `StoreReport::unavailable`).
    pub invalid_entries: Vec<(u32, u32)>,
    /// Extents a region header lists past its entry's count (a growth
    /// beyond capacity cut short): not part of the region; allocated while
    /// the region's page is stale.
    pub extra_extents: Vec<(RegionId, (u64, u64))>,
    /// Regions that may read another region's bytes (spec §9 step 4): an
    /// unavailable region with a newer footprint overlaps them. Read-only.
    pub superseded: Vec<RegionId>,
    /// Regions an unavailable region's span overlaps (those bytes stay
    /// reserved).
    pub shares_reserved: Vec<RegionId>,
    /// For each stale page, whether every region header on it agrees with
    /// its entry. Agreement does not prove nothing was lost (spec §9): a
    /// growth within capacity changes only the entry, and a lost release
    /// can leave a Ready header over reused space.
    pub stale_page_agreement: Vec<(u32, bool)>,
    /// Regions whose header pair shows a newer, unreadable version than the
    /// one the open uses (spec §8): a recycle, slot commit or growth's
    /// header possibly lost to a crash. The older version is served. After
    /// a clean close also a pair whose other copy was written and no longer
    /// verifies, unless it is provably the served version's predecessor
    /// (D-162): damage since, or a torn write an earlier crash left that no
    /// later write of the pair replaced (listed until the region's next
    /// header write). Either way the served version may not be the newest
    /// written (a slot's last commit); a damaged older copy whose header CRC
    /// cannot be read is listed too.
    pub lost_header_writes: Vec<RegionId>,
    /// A list above was cut at its limit (256 items).
    pub truncated: bool,
}

/// Which metadata write a format-2 open healed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Healed {
    /// The volume record.
    VolumeRecord,
    /// Table page `p`.
    Page(u32),
}

impl core::fmt::Display for MetadataReport {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let yes = |b: bool| if b { "yes" } else { "no" };
        writeln!(f, "clean close      {}", yes(self.clean_close))?;
        if self.volume_stale {
            writeln!(f, "volume record    stale")?;
        }
        if !self.stale_pages.is_empty() {
            writeln!(f, "stale pages      {:?}", self.stale_pages)?;
        }
        if !self.lost_pages.is_empty() {
            writeln!(f, "lost pages       {:?}", self.lost_pages)?;
        }
        if let Some(h) = self.healed {
            writeln!(f, "healed           {h:?}")?;
        }
        if self.frozen {
            writeln!(f, "allocation       frozen")?;
        }
        if let Some(r) = self.read_only_reason {
            writeln!(f, "read-only        {r}")?;
        }
        if self.retried_reads > 0 {
            writeln!(f, "retried reads    {}", self.retried_reads)?;
        }
        for d in &self.damaged_slots {
            let pair = d
                .page
                .map_or_else(|| "volume record".to_owned(), |p| format!("page {p}"));
            let slot = if d.slot_b { 'B' } else { 'A' };
            let what = match d.damage {
                SlotDamage::Older => "an older copy, damaged",
                SlotDamage::InFlight => "the write in flight",
                SlotDamage::Damage => "damage",
            };
            writeln!(f, "damaged slot     {pair} {slot}: {what}")?;
        }
        if !self.invalid_entries.is_empty() {
            writeln!(f, "invalid entries  {:?}", self.invalid_entries)?;
        }
        for (id, (off, len)) in &self.extra_extents {
            writeln!(f, "extra extent     region {id}: {len} bytes at {off}")?;
        }
        if !self.superseded.is_empty() {
            writeln!(f, "superseded       {:?}", self.superseded)?;
        }
        if !self.shares_reserved.is_empty() {
            writeln!(f, "shares reserved  {:?}", self.shares_reserved)?;
        }
        for (p, agrees) in &self.stale_page_agreement {
            let word = if *agrees { "agree" } else { "disagree" };
            writeln!(f, "stale page {p}     headers {word} with entries")?;
        }
        if !self.lost_header_writes.is_empty() {
            writeln!(f, "lost header      {:?}", self.lost_header_writes)?;
        }
        if self.truncated {
            writeln!(f, "(lists cut at 256 items)")?;
        }
        Ok(())
    }
}

/// Object id of table page `p`.
pub(crate) const fn page_object(p: u32) -> u64 {
    (1u64 << 61) | p as u64
}

/// Where page `p`'s pair lives.
pub(crate) fn pair_page(volume: VolumeId, l: &Layout, p: u32) -> PairAt {
    let (a, b) = l.page_slots(p);
    PairAt {
        volume: *volume.as_bytes(),
        object: page_object(p),
        a,
        b,
        log2: l.log2_table,
        format: FORMAT_2,
        numbered: true,
    }
}

/// One table page as the open found it.
#[derive(Debug, Clone)]
pub(crate) struct Page2 {
    pub(crate) pair: PairRead,
    pub(crate) standing: Standing,
    /// The winner's page header (none when Lost).
    pub(crate) header: Option<PageHeader>,
    /// The winner's entries, unchecked (none yet in E2a).
    pub(crate) entries: Vec<RawEntry>,
}

/// Format 2's metadata state.
#[derive(Debug, Clone)]
pub(crate) struct Meta2 {
    pub(crate) volume: VolumeMeta2,
    pub(crate) pages: Vec<Page2>,
    /// Free space past the table area, rebuilt as a complement on every
    /// change (spec §9 step 6).
    pub(crate) free: crate::footprint::FreeMap,
    /// Per-tag usage `(tag, logical, physical)` found at open.
    pub(crate) usage: Vec<(u32, u64, u64)>,
    /// Names of entries that failed the entry checks with a kind this
    /// version does not know (not in `StoreReport::unavailable`): reserved
    /// all the same.
    pub(crate) reserved_names: Vec<store_io_format::meta::RegionName>,
    /// An entry names space past the file's end: growth is refused (it
    /// would make zeros of the lost tail).
    pub(crate) cut_short: bool,
    /// Every allocated range: free space is rebuilt from it (never by
    /// adding a range back).
    pub(crate) allocated: Vec<(u64, u64)>,
    /// Provisionings between their placement and their commit: the name
    /// and the table slot each holds (v0.5 E4 C13).
    pub(crate) claims: Vec<Claim>,
    /// Bytes marked allocated by provisionings and growths not yet
    /// committed (in `allocated`, not yet in `usage`).
    pub(crate) marked: u64,
    /// What each unavailable entry (and each of a kind this version does
    /// not know) holds: exactly what the open added for it, so
    /// `repair_unavailable` takes away no more and no less (v0.5 E6).
    pub(crate) holds: Vec<Hold>,
    /// The extra extents counted allocated because their page is stale
    /// (spec §9 step 3): `(page, region id, range)`, freed when the page is
    /// accepted (and copied with its region by `copy_store` when it
    /// overlaps nothing else).
    pub(crate) stale_extra: Vec<(usize, u64, (u64, u64))>,
}

/// How far the open raises a page's `next_seq`, in memory, on every stale
/// page and every page whose pair may carry an accepted rollback
/// ([`PairRead::possibly_accepted`]): an
/// accepted page's lost commit may have handed out seqs past its winner's
/// `next_seq` (a provisioning's claim takes one past every claim held), and
/// the region ids it gave out are never handed out again unless more than
/// this many provisionings failed on that page between its last two
/// commits. The page's next commit writes the raised value.
pub(crate) const SEQ_MARGIN: u32 = 1 << 12;

impl Page2 {
    /// Raises the page's `next_seq` past what a lost commit may have given
    /// out (see [`SEQ_MARGIN`]).
    pub(crate) fn raise_next_seq(&mut self) {
        if let Some(h) = self.header.as_mut() {
            h.next_seq = h.next_seq.saturating_add(SEQ_MARGIN);
        }
    }

    /// The `meta_seq` of the page's winning copy (0 when none).
    pub(crate) fn meta_seq(&self) -> u64 {
        self.pair
            .report
            .winner_info()
            .and_then(|(i, _)| self.pair.facts[usize::from(i.as_u8())])
            .and_then(|f| f.numbered)
            .map_or(0, |n| n.meta_seq)
    }

    /// Entries whose `footprint_seq` is past the page's own write (only
    /// damage writes one): the open counts them newest of all (spec §9
    /// step 4). A rewrite of the page at a later `meta_seq` would count
    /// them otherwise, changing the verdicts that rest on them, so a page
    /// holding one is written only to remove it.
    pub(crate) fn ahead(&self) -> impl Iterator<Item = (u32, u64)> + '_ {
        let ms = self.meta_seq();
        self.entries
            .iter()
            .filter(move |e| e.footprint_seq > ms)
            .map(|e| (e.seq, e.footprint_seq))
    }
}

/// What an unavailable table entry holds (see [`Meta2::holds`]).
#[derive(Debug, Clone)]
pub(crate) struct Hold {
    pub(crate) page: usize,
    pub(crate) seq: u32,
    /// Its range in `allocated` (its reserved span, as the open found it),
    /// if it has one.
    pub(crate) span: Option<(u64, u64)>,
    /// Its usage in `usage` (an entry that passed the entry checks).
    pub(crate) usage: Option<(u32, u64, u64)>,
    /// Its name in `reserved_names` (an entry of an unknown kind).
    pub(crate) name: Option<store_io_format::meta::RegionName>,
    /// It names space past the file's end (`cut_short`).
    pub(crate) short: bool,
    /// Its `footprint_seq` as settled (spec §9 step 4) and what its writes
    /// may have touched (its header blocks and extents when its header
    /// decoded, else its span; none for a region demoted by an overlap).
    pub(crate) footprint_seq: u64,
    pub(crate) own: Vec<(u64, u64)>,
    /// A region that passed step 3 and was demoted in step 4 (by an
    /// overlap or a name): its name and footprint, for judging it again.
    pub(crate) demoted: Option<(store_io_format::meta::RegionName, Vec<(u64, u64)>)>,
    /// Its entry's name, when that decodes (it counts for names in step 4).
    pub(crate) entry_name: Option<store_io_format::meta::RegionName>,
}

impl Hold {
    /// The region id of the entry (`page << 32 | seq`).
    pub(crate) fn id(&self) -> u64 {
        ((self.page as u64) << 32) | u64::from(self.seq)
    }
}

/// A provisioning's hold, from its placement to its commit or its undo:
/// no other provisioning takes its name or its table slot meanwhile.
#[derive(Debug, Clone)]
pub(crate) struct Claim {
    pub(crate) page: usize,
    pub(crate) seq: u32,
    pub(crate) name: store_io_format::meta::RegionName,
}

impl Meta2 {
    /// Marks `ranges` allocated for a provisioning or a growth that fills
    /// them outside the metadata lock (spec §12: "choose and mark"): no
    /// other placement takes them; free space is the complement again in a
    /// container of `len` bytes.
    pub(crate) fn mark(&mut self, ranges: &[(u64, u64)], len: u64) {
        self.allocated.extend_from_slice(ranges);
        self.marked += ranges.iter().map(|r| r.1).sum::<u64>();
        self.rebuild_free(len);
    }

    /// Undoes [`Self::mark`] of exactly `ranges` (an operation that failed
    /// before its commit).
    pub(crate) fn unmark(&mut self, ranges: &[(u64, u64)], len: u64) {
        for r in ranges {
            if let Some(i) = self.allocated.iter().position(|a| a == r) {
                let _gone = self.allocated.swap_remove(i);
            }
        }
        let bytes = ranges.iter().map(|r| r.1).sum::<u64>();
        debug_assert!(self.marked >= bytes, "unmarked more than was marked");
        self.marked = self.marked.saturating_sub(bytes);
        self.rebuild_free(len);
    }

    /// A marked operation committed: its `bytes` are now a footprint (they
    /// stay in `allocated`).
    pub(crate) fn settle(&mut self, bytes: u64) {
        debug_assert!(self.marked >= bytes, "settled more than was marked");
        self.marked = self.marked.saturating_sub(bytes);
    }

    /// Free space again, as the complement of `allocated` in a container
    /// of `len` bytes.
    pub(crate) fn rebuild_free(&mut self, len: u64) {
        let mut all = self.allocated.clone();
        self.free = crate::footprint::FreeMap::complement(self.volume.table_end(), len, &mut all);
    }
}

/// The next `meta_seq` of the store's metadata (overflow refused).
pub(crate) fn next_meta_seq_of(meta: &Meta) -> Result<u64, Error> {
    let v2 = meta.v2.as_deref().ok_or_else(corrupt)?;
    next_meta_seq(&meta.volume_pair, &v2.pages, None)
}

/// Writes table page `p`'s next copy, `payload` at `meta_seq` (chosen by
/// the caller first, so the entries it carries can name it), durably; only
/// a Confirmed page is ever written (spec §9 step 2). The page's state in
/// `meta` follows once the write is durable.
#[allow(clippy::too_many_arguments)]
pub(crate) fn write_page<P: Platform>(
    q: &mut Lane<P::Queue>,
    file: &P::File,
    pool: &store_io_buf::BufPool,
    layout: &Layout,
    volume: VolumeId,
    policy: &Policy,
    domain: &Domain,
    meta: &mut Meta,
    p: usize,
    payload: &[u8],
    meta_seq: u64,
    owner_generation: u64,
) -> Result<(), Error> {
    let v2 = meta.v2.as_deref_mut().ok_or_else(corrupt)?;
    let page = v2.pages.get_mut(p).ok_or_else(corrupt)?;
    if page.standing != Standing::Confirmed {
        return Err(Error::NotWritten {
            cause: store_io_core::error::NotWrittenCause::Frozen,
            ctx: ErrorContext::default(),
        });
    }
    let pi = u32::try_from(p).map_err(|_| corrupt())?;
    let decoded = decode_page(payload, pi, entries_per_page(layout.log2_table))
        .map(|d| (d.header, d.entries().collect::<Vec<_>>()))
        .map_err(|_| corrupt())?;
    // An entry past the page's write, kept as it is, would read as an older
    // one after this write (see `Page2::ahead`): refused before any I/O.
    let ahead: Vec<(u32, u64)> = page.ahead().collect();
    if let Some(kept) = decoded
        .1
        .iter()
        .find(|e| ahead.contains(&(e.seq, e.footprint_seq)))
    {
        // Named: the entry to repair first.
        return Err(Error::NotWritten {
            cause: store_io_core::error::NotWrittenCause::Frozen,
            ctx: ErrorContext {
                region: Some(store_io_core::id::RegionId::from_raw(
                    (u64::from(pi) << 32) | u64::from(kept.seq),
                )),
                ..ErrorContext::default()
            },
        });
    }
    let mut next = plan_next(&page.pair, payload, owner_generation)?;
    next.meta_seq = meta_seq;
    let at = pair_page(volume, layout, pi);
    page.pair = durable_slot::<P>(q, file, pool, &at, &page.pair, &next, policy, domain)?;
    page.header = Some(decoded.0);
    page.entries = decoded.1;
    Ok(())
}

/// The `meta_seq` a slot of `pair` counts for (`Valid`, or `BadPayloadCrc`
/// with a numbered header).
fn numbered(pair: &PairRead, slot: SlotIndex) -> Option<u64> {
    let i = usize::from(slot.as_u8());
    let status = if slot == SlotIndex::A {
        pair.report.a
    } else {
        pair.report.b
    };
    match status {
        SlotStatus::Valid(info) => Some(info.meta_seq),
        SlotStatus::BadPayloadCrc => pair.facts[i].and_then(|f| f.numbered).map(|n| n.meta_seq),
        _ => None,
    }
}

/// One above every numbered slot of the volume record and the pages, except
/// the slot `over` (pair 0 the volume record, `p + 1` page `p`) the write
/// replaces (spec §7).
fn next_meta_seq(
    volume: &PairRead,
    pages: &[Page2],
    over: Option<(usize, SlotIndex)>,
) -> Result<u64, Error> {
    let pairs = core::iter::once(volume).chain(pages.iter().map(|p| &p.pair));
    let mut max = 0u64;
    for (i, pair) in pairs.enumerate() {
        for slot in [SlotIndex::A, SlotIndex::B] {
            if over == Some((i, slot)) {
                continue;
            }
            if let Some(v) = numbered(pair, slot) {
                max = max.max(v);
            }
        }
    }
    max.checked_add(1).ok_or(Error::Unsupported {
        what: Capability::FormatVersion,
    })
}

fn page_payload(p: u32) -> Result<Vec<u8>, Error> {
    let mut buf = vec![0u8; store_io_format::meta2::PAGE_HEADER_LEN];
    let n = encode_page(
        &mut buf,
        &PageHeader {
            page_index: p,
            next_seq: 0,
            count: 0,
        },
        &[],
    )
    .map_err(|_| meta_err())?;
    buf.truncate(n);
    Ok(buf)
}

fn volume_payload(vm: &VolumeMeta2) -> Result<[u8; VOLUME_META2_LEN], Error> {
    let mut buf = [0u8; VOLUME_META2_LEN];
    let _n = vm.encode(&mut buf).map_err(|_| meta_err())?;
    Ok(buf)
}

/// The format-2 layout for a container with blocks of `1 << log2_block`,
/// table pages of `1 << log2_table` and room for `capacity` regions.
pub(crate) fn layout_for(log2_block: u8, log2_table: u8, capacity: u32) -> Layout {
    let e = u32::from(entries_per_page(log2_table)).max(1);
    let pages = capacity
        .min(MAX_REGION_CAPACITY)
        .div_ceil(e)
        .clamp(1, store_io_format::meta2::MAX_TABLE_PAGES - 1);
    Layout {
        log2_block,
        log2_table,
        format: FORMAT_2,
        pages,
    }
}

/// Writes a fresh format-2 container's metadata (spec §12, create): the
/// file sized to the end of page 0's slot A (a one-page table: the whole
/// table), page 0's slot A (generation 1, `meta_seq` 1, empty), for P > 1
/// a full flush, the file sized for the whole table and every other page's
/// slot A (`meta_seq` 2..P), then a full flush, the volume record's slot A (`meta_seq`
/// P + 1), a full flush. The caller flushes the directory. Growing the file
/// only once page 0 is durable means a crash never leaves a file past
/// format 1's size bound without a page a later create recognises.
#[allow(clippy::too_many_arguments)]
pub(crate) fn create_metadata<P: Platform>(
    platform: &P,
    file: &P::File,
    q: &mut Lane<P::Queue>,
    pool: &store_io_buf::BufPool,
    layout: &Layout,
    volume: VolumeId,
    created_unix_s: u64,
) -> Result<Meta, Error> {
    let allocate = |len: u64| {
        platform
            .allocate(file, len)
            .map_err(|e| space_err(Op::Allocate, e, 0))
    };
    // Page 0's slot A; a one-page table is within format 1's bound whole.
    allocate(if layout.pages == 1 {
        layout.data_start()
    } else {
        layout.page_slots(0).1
    })?;
    let mut pages = Vec::with_capacity(layout.pages as usize);
    for p in 0..layout.pages {
        if p == 1 {
            full_flush(platform, file)?;
            allocate(layout.data_start())?;
        }
        let payload = page_payload(p)?;
        let mut next = plan_next(&PairRead::blank(), &payload, 1)?;
        next.meta_seq = u64::from(p) + 1;
        let w = write_slot(q, file, pool, &pair_page(volume, layout, p), &next, false)?;
        pages.push(Page2 {
            pair: PairRead::blank().after(w),
            standing: Standing::Confirmed,
            header: Some(PageHeader {
                page_index: p,
                next_seq: 0,
                count: 0,
            }),
            entries: Vec::new(),
        });
    }
    full_flush(platform, file)?;
    let vm = VolumeMeta2 {
        base: VolumeMeta {
            log2_block_size: layout.log2_block,
            log2_table_slot_size: layout.log2_table,
            device_index: 0,
            owner_generation: 1,
            table_offset: 2 * layout.block(),
            created_unix_s,
            features: 0,
        },
        table_pages: layout.pages,
        entries_per_page: entries_per_page(layout.log2_table),
        clean_close: false,
        flags: 0,
    };
    let vbuf = volume_payload(&vm)?;
    let mut next = plan_next(&PairRead::blank(), &vbuf, 1)?;
    next.meta_seq = u64::from(layout.pages) + 1;
    let w = write_slot(q, file, pool, &pair_volume(volume, layout), &next, false)?;
    full_flush(platform, file)?;
    Ok(Meta {
        volume: vm.base,
        volume_pair: PairRead::blank().after(w),
        table_pair: PairRead::blank(),
        next_region_id: 0,
        entries: Vec::new(),
        container_len: layout.data_start(),
        v2: Some(Box::new(Meta2 {
            volume: vm,
            pages,
            free: crate::footprint::FreeMap::complement(
                layout.data_start(),
                layout.data_start(),
                &mut [],
            ),
            usage: Vec::new(),
            reserved_names: Vec::new(),
            cut_short: false,
            allocated: Vec::new(),
            claims: Vec::new(),
            marked: 0,
            holds: Vec::new(),
            stale_extra: Vec::new(),
        })),
        unavailable: Vec::new(),
        repairs: Vec::new(),
    })
}

fn full_flush<P: Platform>(platform: &P, file: &P::File) -> Result<(), Error> {
    platform
        .flush_all(file)
        .map_err(|e| Error::DurabilityUnknown {
            op: Op::FlushAll,
            raw: Some(e),
            ctx: ErrorContext::default(),
        })
}

/// What a format-2 open decided.
pub(crate) struct Opened {
    pub(crate) meta: Meta,
    pub(crate) owner_generation: u64,
    pub(crate) read_only: bool,
    pub(crate) report: MetadataReport,
    pub(crate) regions: Vec<std::sync::Arc<LiveRegion>>,
    pub(crate) unavailable: Vec<UnavailableRegion>,
}

/// Decodes a format-2 volume record: a longer record, or a `features` or
/// `flags` bit this version does not know, is a newer format (refused);
/// anything else that fails is corruption.
pub(crate) fn decode_volume(bytes: &[u8]) -> Result<VolumeMeta2, Error> {
    if bytes.len() > VOLUME_META2_LEN {
        // A longer record is a newer layout (spec §14).
        return Err(Error::Unsupported {
            what: Capability::FormatVersion,
        });
    }
    let vm = VolumeMeta2::decode(bytes).map_err(|e| match e {
        store_io_format::meta::MetaError::Field("features" | "flags") => Error::Unsupported {
            what: Capability::FormatVersion,
        },
        _ => corrupt(),
    })?;
    // More pages than any store of this version can have: a later version's
    // larger table, or a hostile record, refused before the open reads or
    // holds anything per page (the format admits up to 2^30, and a sparse
    // file of that length costs nothing). Unsupported, not corrupt: a
    // caller must never be invited to recreate a store over it.
    if vm.table_pages > max_table_pages(vm.base.log2_table_slot_size) {
        return Err(Error::Unsupported {
            what: Capability::FormatVersion,
        });
    }
    Ok(vm)
}

/// The most table pages a store with table slots of `1 << log2_table` can
/// have: what a create for [`MAX_REGION_CAPACITY`] makes.
pub(crate) fn max_table_pages(log2_table: u8) -> u32 {
    layout_for(12, log2_table, MAX_REGION_CAPACITY).pages
}

/// Reads a metadata pair, each slot whose read failed again once (spec
/// §7), counting the retries.
pub(crate) fn read_retried<Q: store_io_platform::Queue>(
    q: &mut Lane<Q>,
    file: &Q::File,
    pool: &store_io_buf::BufPool,
    at: &PairAt,
    retried: &mut u32,
) -> Result<PairRead, Error> {
    let first = read_pair(q, file, pool, at)?;
    let (pair, again) = reread_failed(q, file, pool, at, first)?;
    *retried += u32::from(again);
    Ok(pair)
}

/// As [`read_retried`] for every pair in `ats`, many reads in flight at
/// once (the re-reads, rare, one at a time); in `ats`'s order.
pub(crate) fn read_all_retried<Q: store_io_platform::Queue>(
    q: &mut Lane<Q>,
    file: &Q::File,
    pool: &store_io_buf::BufPool,
    ats: &[PairAt],
    retried: &mut u32,
) -> Result<Vec<PairRead>, Error> {
    let first = read_pairs(q, file, pool, ats, usize::MAX)?;
    let mut out = Vec::with_capacity(first.len());
    for (at, pair) in ats.iter().zip(first) {
        let (pair, again) = reread_failed(q, file, pool, at, pair)?;
        *retried += u32::from(again);
        out.push(pair);
    }
    Ok(out)
}

fn judge_read(p: &PairRead) -> judge::PairRead {
    judge::PairRead {
        a: judge::SlotRead {
            status: p.report.a,
            facts: p.facts[0],
        },
        b: judge::SlotRead {
            status: p.report.b,
            facts: p.facts[1],
        },
    }
}

/// The format-2 open after the volume record was found (spec §9): reads
/// every page, judges the metadata, checks every region (`open2`), and only
/// then, when writable, heals the one torn write and writes the volume
/// record with the next owner generation: a read-only verdict from the
/// region checks comes before anything is written.
#[allow(clippy::too_many_arguments)]
pub(crate) fn open_metadata<P: Platform>(
    file: &P::File,
    q: &mut Lane<P::Queue>,
    pool: &store_io_buf::BufPool,
    layout: &Layout,
    volume: VolumeId,
    mut vm: VolumeMeta2,
    mut volume_pair: PairRead,
    container_len: u64,
    read_only: bool,
    policy: &Policy,
    append_window: u64,
    header_queue: &mut dyn FnMut() -> Option<(P::Queue, usize)>,
) -> Result<Opened, Error> {
    if layout.data_start() > container_len {
        return Err(corrupt());
    }
    let mut retried = 0u32;
    let (again, reread) = reread_failed(q, file, pool, &pair_volume(volume, layout), volume_pair)?;
    volume_pair = again;
    if reread {
        // A slot read again may be the newer winner: the record is read from
        // it then, and must describe the same table.
        retried += 1;
        // A newer copy read the second time is refused, not corrupt.
        vm = decode_volume(&crate::open::winner_payload(&volume_pair)?)?;
        if vm.table_pages != layout.pages
            || vm.base.log2_block_size != layout.log2_block
            || vm.base.log2_table_slot_size != layout.log2_table
        {
            return Err(corrupt());
        }
    }
    let mut pages = Vec::with_capacity(layout.pages as usize);
    for p in 0..layout.pages {
        let at = pair_page(volume, layout, p);
        let pair = read_retried(q, file, pool, &at, &mut retried)?;
        pages.push(Page2 {
            pair,
            standing: Standing::Lost,
            header: None,
            entries: Vec::new(),
        });
    }
    let reads: Vec<judge::PairRead> = core::iter::once(&volume_pair)
        .chain(pages.iter().map(|p| &p.pair))
        .map(judge_read)
        .collect();
    let mut verdicts = vec![
        PairVerdict {
            standing: Standing::Lost,
            winner: None,
            a: SlotVerdict::Damage,
            b: SlotVerdict::Damage,
        };
        reads.len()
    ];
    let j = judge::judge(&reads, vm.clean_close, &mut verdicts).map_err(|_| corrupt())?;
    if j.refused {
        return Err(Error::Unsupported {
            what: Capability::FormatVersion,
        });
    }
    let mut report = MetadataReport {
        clean_close: j.clean,
        volume_stale: verdicts[0].standing != Standing::Confirmed,
        retried_reads: retried,
        ..MetadataReport::default()
    };
    let mut decode_lost = false;
    for (i, page) in pages.iter_mut().enumerate() {
        let p = i as u32;
        let mut standing = verdicts[i + 1].standing;
        if standing != Standing::Lost {
            match page.pair.payload.as_deref().map(|b| {
                decode_page(b, p, entries_per_page(layout.log2_table))
                    .map(|d| (d.header, d.entries().collect::<Vec<_>>()))
            }) {
                Some(Ok((h, entries))) => {
                    page.header = Some(h);
                    page.entries = entries;
                    // A stale page (it may be accepted: its newer copy may
                    // even read Blank), or one possibly accepted already:
                    // ids its lost commit gave out stay given (see
                    // `SEQ_MARGIN`).
                    if standing == Standing::Unconfirmed || page.pair.possibly_accepted() {
                        page.raise_next_seq();
                    }
                }
                _ => {
                    standing = Standing::Lost;
                    decode_lost = true;
                }
            }
        }
        page.standing = standing;
    }
    // A page that fails the page checks is Lost, which the judgement could
    // not see: a page's torn write is healed only when nothing else is
    // lost (spec §7), so the heal is dropped and the torn copy is damage.
    // Likewise for a page holding an entry past its own write: the heal's
    // later `meta_seq` would change how the next open counts that entry
    // (see `Page2::ahead`), so the page is left stale, never written.
    let mut to_heal = j.heal;
    if let Some((pair, _)) = to_heal {
        if pair > 0 && (decode_lost || pages[pair - 1].ahead().next().is_some()) {
            to_heal = None;
            if pages[pair - 1].standing == Standing::Confirmed {
                pages[pair - 1].standing = Standing::Unconfirmed;
                // The verdicts agree with the pages from here on.
                verdicts[pair].standing = Standing::Unconfirmed;
            }
        }
    }
    // Every candidate and damage, by slot (spec §9), from the final
    // standings: a heal dropped above leaves damage, and a page failing the
    // page checks its winner too.
    for (i, v) in verdicts.iter().enumerate() {
        let page = i.checked_sub(1).map(|p| p as u32);
        let lost_late = page.is_some_and(|p| pages[p as usize].standing == Standing::Lost)
            && v.standing != Standing::Lost;
        for (slot_b, verdict) in [(false, v.a), (true, v.b)] {
            let damage = match verdict {
                SlotVerdict::OlderDamaged => SlotDamage::Older,
                SlotVerdict::InFlight if to_heal.is_some_and(|(h, _)| h == i) => {
                    SlotDamage::InFlight
                }
                SlotVerdict::InFlight | SlotVerdict::Damage => SlotDamage::Damage,
                SlotVerdict::Winner if lost_late => SlotDamage::Damage,
                SlotVerdict::Winner | SlotVerdict::Nothing => continue,
            };
            report.damaged_slots.push(DamagedSlot {
                page,
                slot_b,
                damage,
            });
        }
    }
    for (i, page) in pages.iter().enumerate() {
        match page.standing {
            Standing::Unconfirmed => report.stale_pages.push(i as u32),
            Standing::Lost | Standing::Refused => report.lost_pages.push(i as u32),
            Standing::Confirmed => {}
        }
    }
    report.frozen = !report.stale_pages.is_empty() || !report.lost_pages.is_empty();
    // A winner with a read-only-compatible feature this version does not
    // know may be read but never rewritten (spec §14).
    let foreign = core::iter::once(&volume_pair)
        .chain(pages.iter().map(|p| &p.pair))
        .any(|p| p.report.winner_info().is_some_and(|(_, i)| !i.writable()));
    let mut meta = Meta {
        volume: vm.base,
        volume_pair,
        table_pair: PairRead::blank(),
        next_region_id: 0,
        entries: Vec::new(),
        container_len,
        v2: Some(Box::new(Meta2 {
            volume: vm,
            pages,
            free: crate::footprint::FreeMap::default(),
            usage: Vec::new(),
            reserved_names: Vec::new(),
            cut_short: false,
            allocated: Vec::new(),
            claims: Vec::new(),
            marked: 0,
            holds: Vec::new(),
            stale_extra: Vec::new(),
        })),
        unavailable: Vec::new(),
        repairs: Vec::new(),
    };
    // Steps 3-6, before anything is written.
    let regions = {
        let v2 = meta.v2.as_deref().ok_or_else(corrupt)?;
        crate::open2::check_regions(
            q,
            file,
            pool,
            layout,
            volume,
            v2,
            container_len,
            append_window,
            &mut report,
            header_queue,
        )?
    };
    if let Some(v2) = meta.v2.as_deref_mut() {
        v2.free = regions.free;
        v2.usage = regions.usage;
        v2.reserved_names = regions.reserved_names;
        v2.cut_short = regions.cut_short;
        v2.allocated = regions.allocated;
        v2.holds = regions.holds;
        v2.stale_extra = regions.stale_extra;
    }
    let read_only = read_only || {
        report.read_only_reason = if !j.writable {
            Some("the volume record is damaged and a table page holds a damaged copy")
        } else if foreign {
            Some("the metadata carries a feature this version may read but not write")
        } else {
            regions.read_only_reason
        };
        report.read_only_reason.is_some()
    };
    if read_only {
        return Ok(Opened {
            meta,
            owner_generation: 0,
            read_only,
            report,
            regions: regions.live,
            unavailable: regions.unavailable,
        });
    }
    let og = vm
        .base
        .owner_generation
        .checked_add(1)
        .ok_or_else(corrupt)?;
    let domain = Domain::new();
    if let Some((pair, slot)) = to_heal {
        heal::<P>(
            q, file, pool, layout, volume, policy, &domain, &mut meta, pair, slot, og,
        )?;
        report.healed = Some(if pair == 0 {
            Healed::VolumeRecord
        } else {
            Healed::Page(pair as u32 - 1)
        });
    }
    write_volume::<P>(
        q, file, pool, layout, volume, policy, &domain, &mut meta, og, false,
    )?;
    Ok(Opened {
        meta,
        owner_generation: og,
        read_only,
        report,
        regions: regions.live,
        unavailable: regions.unavailable,
    })
}

/// Rewrites the one torn metadata write (`pair` 0 the volume record, `p + 1`
/// page `p`; `slot` its failed slot) with the winner's payload byte for
/// byte, at the torn copy's own `meta_seq` when it was readable (spec §7).
#[allow(clippy::too_many_arguments)]
fn heal<P: Platform>(
    q: &mut Lane<P::Queue>,
    file: &P::File,
    pool: &store_io_buf::BufPool,
    layout: &Layout,
    volume: VolumeId,
    policy: &Policy,
    domain: &Domain,
    meta: &mut Meta,
    pair: usize,
    slot: SlotIndex,
    og: u64,
) -> Result<(), Error> {
    let v2 = meta.v2.as_mut().ok_or_else(corrupt)?;
    let (at, prev) = if pair == 0 {
        (pair_volume(volume, layout), &meta.volume_pair)
    } else {
        let p = u32::try_from(pair - 1).map_err(|_| corrupt())?;
        (pair_page(volume, layout, p), &v2.pages[pair - 1].pair)
    };
    let payload = prev.payload.clone().ok_or_else(corrupt)?;
    let mut next = plan_next(prev, &payload, og)?;
    if next.index != slot {
        return Err(corrupt());
    }
    let own = prev.facts[usize::from(slot.as_u8())]
        .and_then(|f| f.numbered)
        .map(|n| n.meta_seq);
    next.meta_seq = match own {
        Some(m) => m,
        None => next_meta_seq(&meta.volume_pair, &v2.pages, Some((pair, slot)))?,
    };
    let healed = durable_slot::<P>(q, file, pool, &at, prev, &next, policy, domain)?;
    if pair == 0 {
        meta.volume_pair = healed;
    } else {
        v2.pages[pair - 1].pair = healed;
        v2.pages[pair - 1].standing = Standing::Confirmed;
    }
    Ok(())
}

/// The bytes of page `p`'s two slots (an error naming the page).
pub(crate) fn page_range(layout: &Layout, p: u32) -> store_io_core::error::ByteRange {
    let (a, b) = layout.page_slots(p);
    store_io_core::error::ByteRange {
        start: a.min(b),
        end: a.max(b) + (1u64 << layout.log2_table),
    }
}

/// Accepts stale page `p` (spec §12 `accept_page_rollback`): its winner
/// rewritten byte for byte into its failed slot at the next `meta_seq`,
/// durably, then the pair read back (retried once, §7): both slots must be
/// Valid with the same payload (the twin the next open will see, which
/// then counts the page Confirmed). On success the page is Confirmed (its
/// `next_seq` was raised by the open that found it stale, see
/// [`SEQ_MARGIN`]). Any failure after the write
/// poisons the store: the new copy is on the media, at a `meta_seq` this
/// state cannot count (its slot may read at the next open), so only the
/// next open knows the page.
#[allow(clippy::too_many_arguments)]
pub(crate) fn accept_page<P: Platform>(
    q: &mut Lane<P::Queue>,
    file: &P::File,
    pool: &store_io_buf::BufPool,
    layout: &Layout,
    volume: VolumeId,
    policy: &Policy,
    domain: &Domain,
    meta: &mut Meta,
    p: usize,
    og: u64,
) -> Result<(), Error> {
    let meta_seq = next_meta_seq_of(meta)?;
    let v2 = meta.v2.as_mut().ok_or_else(corrupt)?;
    let page = v2.pages.get_mut(p).ok_or_else(corrupt)?;
    let at = pair_page(volume, layout, u32::try_from(p).map_err(|_| corrupt())?);
    let payload = page.pair.payload.clone().ok_or_else(corrupt)?;
    let mut next = plan_next(&page.pair, &payload, og)?;
    next.meta_seq = meta_seq;
    let _written = durable_slot::<P>(q, file, pool, &at, &page.pair, &next, policy, domain)?;
    let mut retried = 0;
    let back = read_retried(q, file, pool, &at, &mut retried).and_then(|back| {
        if back.twin {
            Ok(back)
        } else {
            Err(Error::Corruption {
                kind: store_io_core::error::CorruptionKind::MediaError,
                ctx: ErrorContext {
                    range: Some(page_range(layout, u32::try_from(p).map_err(|_| corrupt())?)),
                    ..ErrorContext::default()
                },
            })
        }
    });
    match back {
        Ok(back) => {
            page.pair = back;
            page.standing = Standing::Confirmed;
            Ok(())
        }
        Err(e) => {
            domain.poison(store_io_core::error::FirstCause {
                op: Some(store_io_core::error::Op::Read),
                raw: None,
            });
            Err(e)
        }
    }
}

/// Writes the volume record's next version: `owner_generation` and
/// `clean_close` as given, the next `meta_seq`, durably.
#[allow(clippy::too_many_arguments)]
pub(crate) fn write_volume<P: Platform>(
    q: &mut Lane<P::Queue>,
    file: &P::File,
    pool: &store_io_buf::BufPool,
    layout: &Layout,
    volume: VolumeId,
    policy: &Policy,
    domain: &Domain,
    meta: &mut Meta,
    owner_generation: u64,
    clean_close: bool,
) -> Result<(), Error> {
    let v2 = meta.v2.as_mut().ok_or_else(corrupt)?;
    let mut vm = v2.volume;
    vm.base.owner_generation = owner_generation;
    vm.clean_close = clean_close;
    let vbuf = volume_payload(&vm)?;
    let mut next = plan_next(&meta.volume_pair, &vbuf, owner_generation)?;
    next.meta_seq = next_meta_seq(&meta.volume_pair, &v2.pages, Some((0, next.index)))?;
    let at = pair_volume(volume, layout);
    meta.volume_pair =
        durable_slot::<P>(q, file, pool, &at, &meta.volume_pair, &next, policy, domain)?;
    v2.volume = vm;
    meta.volume = vm.base;
    Ok(())
}

impl<P: Platform> Drop for Inner<P> {
    /// Format 2's clean close: once every handle is gone (so every write has
    /// completed), a writable, unpoisoned store records `clean_close` in a
    /// final durable volume-record write. Not after a panic (the in-memory
    /// metadata may be half-changed). Best effort: a failure leaves the
    /// next open to judge the store as not closed cleanly, which is safe.
    fn drop(&mut self) {
        if self.read_only
            || self.domain.poisoned().is_some()
            || self.meta.is_poisoned()
            || std::thread::panicking()
        {
            return;
        }
        let mut meta = lock_meta(&self.meta);
        if meta.v2.is_none() {
            return;
        }
        let mut q = self.queues.get(0);
        let _closed = write_volume::<P>(
            &mut q,
            &self.file,
            &self.pool,
            &self.layout,
            self.volume,
            &self.policy,
            &self.domain,
            &mut meta,
            self.owner_generation,
            true,
        );
    }
}

#[cfg(all(test, not(loom)))]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;

    fn record(log2_table: u8, table_pages: u32) -> Vec<u8> {
        let vm = VolumeMeta2 {
            base: VolumeMeta {
                log2_block_size: 12,
                log2_table_slot_size: log2_table,
                device_index: 0,
                owner_generation: 1,
                table_offset: 2 << 12,
                created_unix_s: 0,
                features: 0,
            },
            table_pages,
            entries_per_page: entries_per_page(log2_table),
            clean_close: true,
            flags: 0,
        };
        volume_payload(&vm).unwrap().to_vec()
    }

    /// A volume record claiming more table pages than a create for the
    /// largest capacity makes is refused (`Unsupported`: a later version's
    /// table, or hostile, never a store to recreate); the largest real one
    /// opens (independent review F-1).
    #[test]
    fn test_a_volume_record_past_the_largest_table_is_unsupported() {
        for log2_table in 12..=16u8 {
            let max = max_table_pages(log2_table);
            let real = layout_for(12, log2_table, MAX_REGION_CAPACITY).pages;
            assert_eq!(max, real);
            assert!(max > 1 && max < store_io_format::meta2::MAX_TABLE_PAGES - 1);
            assert!(decode_volume(&record(log2_table, max)).is_ok());
            for hostile in [max + 1, store_io_format::meta2::MAX_TABLE_PAGES - 1] {
                assert!(
                    matches!(
                        decode_volume(&record(log2_table, hostile)),
                        Err(Error::Unsupported {
                            what: Capability::FormatVersion,
                        })
                    ),
                    "{log2_table}: {hostile} pages"
                );
            }
        }
    }
}