store-io-engine 0.6.0

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
//! Provisioning a region in container format 2 (spec §10, §12), in three
//! phases (v0.5 E4 C13): under the metadata lock, its name and a table slot
//! claimed, its space placed by first fit over the free space (else past
//! everything allocated) and marked allocated, its tag charged (or the
//! bytes taken from its reservation, which keeps counting them reserved
//! until the commit); without the lock, filled, flushed and verified as in
//! format 1 and its header written in slot A; under the lock again,
//! committed by one table-page write carrying its entry (the page as it is
//! then), whose `footprint_seq` is that write's own `meta_seq`, and the
//! region made live in the same hold of the lock. Other provisionings,
//! growths, releases, reservations and space reports run during the fill.
//! A failure (or a panic) before the commit gives every hold back
//! ([`Holds`]).
//!
//! A capped region (`RegionOptions::max_size`, v0.5 E4 C10) is one extent
//! of its cap, flagged `FLAG_CAPPED`, charged whole: its data filled and
//! verified as any region's; the rest (the slack) allocated without being
//! written, or written too where the system cannot allocate it unwritten or
//! pays for it later (Windows, macOS: [`Store::writes_slack`]).

use std::sync::{Arc, Mutex, PoisonError};

use store_io_buf::IoBuf;
use store_io_core::error::{Error, ErrorContext, Named, NotWrittenCause, ReserveTag};
use store_io_core::id::{Generation, RegionId};
use store_io_format::judge::Standing;
use store_io_format::meta::{FillPattern, RegionKind, RegionName, RegionState};
use store_io_format::meta2::{
    Entry2, FLAG_CAPPED, PAGE_HEADER_LEN, PageHeader, RegionHeader2, encode_page,
    encode_region_header, entries_per_page,
};
use store_io_format::slot::FORMAT_2;
use store_io_platform::Platform;

use crate::frontier::AppendFrontier;
use crate::gate::Gate;
use crate::io::not_written;
use crate::layout::DATA_ALIGN;
use crate::slots::{PairAt, PairRead, plan_next};
use crate::space::{Reservation, lock_acct, no_space};
use crate::store::*;

impl<P: Platform> Store<P>
where
    P::Queue: Send,
{
    /// Provisions a format-2 region (the caller checked read-only and
    /// poison). See [`Store::provision`].
    pub(crate) fn provision2(
        &self,
        name: &str,
        kind: RegionKind,
        opts: crate::RegionOptions,
        from: Option<&mut Reservation<P>>,
        copied: Option<&crate::copy::CopyAttrs>,
        data: Option<&mut crate::provision::Content<'_>>,
    ) -> Result<Arc<LiveRegion>, Error> {
        let (placed, mut holds) = self.place2(name, kind, opts, from, copied)?;
        // Any return before the commit drops `holds` armed: every hold
        // given back.
        self.fill_and_commit2(&placed, &mut holds, data)
    }

    /// Phase 1 of a format-2 provisioning (see the module docs): the name
    /// and a table slot claimed, the space placed and marked, the tag
    /// charged or the reservation's bytes held; what it holds is given back
    /// when the returned [`Holds`] drops armed.
    fn place2<'a>(
        &self,
        name: &str,
        kind: RegionKind,
        opts: crate::RegionOptions,
        from: Option<&'a mut Reservation<P>>,
        copied: Option<&crate::copy::CopyAttrs>,
    ) -> Result<(Placed, Holds<'a, P>), Error> {
        let inner = &self.inner;
        let data_size = opts.size_bytes();
        // A region `copy_store` recreates keeps its tag, generation, fill
        // pattern and key.
        let tag = copied.map_or_else(|| from.as_ref().map_or(0, |r| r.tag()), |c| c.tag);
        let (fill, fill_key) = match copied {
            Some(c) => (c.fill, c.fill_key),
            None if inner.opts.keyed_fill => (FillPattern::KeyedV1, random_uuid()),
            None => (FillPattern::Zeros, [0; 16]),
        };
        let generation =
            Generation::from_raw(copied.map_or(1, |c| c.generation)).ok_or_else(corrupt)?;
        // Refusals before any I/O (spec §9 step 2, §10).
        if inner.frozen.load(std::sync::atomic::Ordering::Acquire) {
            return Err(not_written(
                NotWrittenCause::Frozen,
                ErrorContext::default(),
            ));
        }
        let too_large = || not_written(NotWrittenCause::TooLarge, ErrorContext::default());
        let rname = RegionName::new(name)
            .map_err(|_| not_written(NotWrittenCause::InvalidName, ErrorContext::default()))?;
        let block = inner.layout.block();
        let slot = kind == RegionKind::Slot;
        let data_size = if slot || (data_size == 0 && copied.is_some()) {
            0
        } else {
            store_io_core::align::align_up(data_size.max(1), block).ok_or_else(too_large)?
        };
        // A capped region: one extent of its cap (at least its size).
        let capped = opts.max_size_bytes().filter(|_| !slot);
        let capacity = match capped {
            Some(cap) => store_io_core::align::align_up(cap, block)
                .ok_or_else(too_large)?
                .max(data_size),
            None if slot => 0,
            None => data_size.max(block),
        };
        if capacity / block >= u64::from(u32::MAX) {
            return Err(too_large());
        }
        let extent = capacity.checked_add(2 * block).ok_or_else(too_large)?;
        let flags = if capped.is_some() { FLAG_CAPPED } else { 0 };
        let mut from = from;
        // Phase 1, under the metadata lock (spec §12, "choose and mark"):
        // the name and a table slot claimed, the space placed and marked,
        // the tag charged or the reservation's bytes held.
        let mut meta = lock_meta(&inner.meta);
        self.name_taken2(&meta, &rname)?;
        let (p, seq) = {
            let v2 = meta.v2.as_deref().ok_or_else(corrupt)?;
            let per_page = entries_per_page(inner.layout.log2_table);
            v2.pages
                .iter()
                .enumerate()
                .find_map(|(p, page)| {
                    let h = page.header?;
                    // Slots other provisionings hold count as taken; a seq
                    // past every one claimed (a seq never committed may be
                    // handed out again, as before: the next fill zeroes any
                    // header a failed one left).
                    let held = v2.claims.iter().filter(|c| c.page == p);
                    let claimed = held.clone().count();
                    let seq = held.map(|c| c.seq + 1).fold(h.next_seq, u32::max);
                    // Not a page holding an entry past its own write (see
                    // `Page2::ahead`): it is written only to remove it.
                    (page.standing == Standing::Confirmed
                        && usize::from(h.count) + claimed < usize::from(per_page)
                        && seq < u32::MAX
                        && page.ahead().next().is_none())
                    .then_some((p, seq))
                })
                .ok_or(Error::NoSpace {
                    tag: ReserveTag::Table,
                    ctx: ErrorContext::default(),
                })?
        };
        {
            let mut acct = lock_acct(&inner.acct);
            match &from {
                Some(r) if extent > r.remaining() => return Err(no_space(tag)),
                None if !acct.fits(tag, extent) => return Err(no_space(tag)),
                _ => {}
            }
        }
        // Placement (spec §10): first fit over the free space, else past
        // everything allocated. A large region's data area is aligned as in
        // format 1; a reservation's growth that finds no room for the
        // alignment places the region unaligned instead (format 1's rule).
        let reserved_after = {
            let acct = lock_acct(&inner.acct);
            match &from {
                Some(_) => acct.reserved().saturating_sub(extent),
                None => acct.reserved(),
            }
        };
        let mut align = if capacity >= DATA_ALIGN {
            DATA_ALIGN
        } else {
            block
        };
        let offset = loop {
            let v2 = meta.v2.as_deref().ok_or_else(corrupt)?;
            let offset = match v2.free.first_fit(extent, align, 2 * block) {
                Some(at) => at,
                None => v2.free.tail(align, 2 * block).ok_or_else(too_large)?,
            };
            let end = offset.checked_add(extent).ok_or_else(too_large)?;
            let target = self
                .tail(&meta)
                .max(end)
                .checked_add(reserved_after)
                .ok_or_else(too_large)?;
            match self.grow_to(&mut meta, target, tag) {
                Ok(()) => break offset,
                Err(Error::NoSpace { .. }) if align > block && from.is_some() => align = block,
                Err(e) => return Err(e),
            }
        };
        let extents: Vec<(u64, u64)> = if slot {
            Vec::new()
        } else {
            vec![(offset + 2 * block, capacity)]
        };
        // As the open records a footprint: the header blocks, then the data
        // extent (release removes exactly these).
        let mut footprint = vec![(offset, block), (offset + block, block)];
        footprint.extend(extents.iter().copied());
        // The holds, all at once (nothing below fails until phase 2). A
        // reservation's bytes stay counted reserved until the commit, so
        // every placement meanwhile still sizes the container past them.
        if let Some(r) = from.as_deref_mut() {
            r.hold(extent)?;
        }
        let container = meta.container_len;
        if let Some(v2) = meta.v2.as_deref_mut() {
            v2.claims.push(crate::v2::Claim {
                page: p,
                seq,
                name: rname,
            });
            v2.mark(&footprint, container);
        }
        let charged = if from.is_none() {
            lock_acct(&inner.acct).add_usage(tag, 0, extent);
            extent
        } else {
            0
        };
        drop(meta);
        let holds = Holds {
            store: self.clone(),
            ranges: footprint,
            claim: Some((p, seq)),
            tag,
            charged,
            from: from.map(|r| (r, extent)),
            armed: true,
        };
        let placed = Placed {
            kind,
            name: rname,
            tag,
            flags,
            id: ((p as u64) << 32) | u64::from(seq),
            page: p,
            seq,
            offset,
            data_size,
            capacity,
            extent,
            extents,
            fill_whole: capped.is_none() || self.writes_slack(),
            fill,
            fill_key,
            generation,
        };
        Ok((placed, holds))
    }

    /// Phases 2 and 3 of a format-2 provisioning placed and marked by
    /// phase 1: filled, made durable and verified, its header written, all
    /// without the metadata lock (other provisionings, growths, releases,
    /// reservations and space reports run meanwhile); then, under it, the
    /// commit: its page as it is now with the entry added, at the next
    /// `meta_seq` (the entry's `footprint_seq`), and in the same hold of the
    /// lock memory follows (the claim ends, the mark becomes the footprint,
    /// the usage counted, `holds` disarmed) and the region becomes live, so
    /// no other provisioning can take its name between the two.
    fn fill_and_commit2(
        &self,
        pl: &Placed,
        holds: &mut Holds<'_, P>,
        data: Option<&mut crate::provision::Content<'_>>,
    ) -> Result<Arc<LiveRegion>, Error> {
        let block = self.inner.layout.block();
        let written = match data {
            None => 0,
            // A region `copy_store` recreates (D-137): its data area is the
            // source's bytes instead of the pattern; the header blocks zero
            // and any slack the pattern, as a fill writes them.
            Some(data) => {
                let head = 2 * block + pl.data_size;
                self.write_range(pl.offset, head, 2 * block, 0, data)?;
                head
            }
        };
        self.commit2(pl, holds, written)
    }

    /// Phases 2 and 3 of a format-2 provisioning whose first `written`
    /// bytes from its header blocks on are written: the rest filled as a
    /// fill writes it (the header blocks zero, the data area and any slack
    /// the region's pattern at its offset), made durable and verified, its
    /// header written, and committed (see [`Self::fill_and_commit2`]).
    fn commit2(
        &self,
        pl: &Placed,
        holds: &mut Holds<'_, P>,
        written: u64,
    ) -> Result<Arc<LiveRegion>, Error> {
        let inner = &self.inner;
        let block = inner.layout.block();
        let (fill, fill_key) = (pl.fill, pl.fill_key);
        // 1-3. Fill, make durable, verify (as format 1); a capped region's
        // slack allocated, not written (but where the slack is written),
        // never verified.
        let filled = if pl.fill_whole {
            pl.extent
        } else {
            2 * block + pl.data_size
        };
        if written < filled {
            self.fill_range(
                pl.offset + written,
                filled - written,
                (2 * block).saturating_sub(written),
                fill,
                &fill_key,
                written.saturating_sub(2 * block),
            )?;
        }
        if filled < pl.extent {
            inner
                .platform
                .allocate_range(&inner.file, pl.offset + filled, pl.extent - filled)
                .map_err(|raw| {
                    crate::store::space_err(store_io_core::error::Op::Allocate, raw, pl.tag)
                })?;
        }
        self.flush_all_accounted()?;
        self.verify_ready(pl.offset, filled)?;
        // 4. The header in slot A, generation 1, Ready.
        let h = RegionHeader2 {
            region_id: pl.id,
            kind: pl.kind,
            state: RegionState::Ready,
            fill,
            flags: pl.flags,
            compat_flags: 0,
            log2_block: inner.layout.log2_block,
            extent_count: pl.extents.len() as u16,
            region_generation: pl.generation.get(),
            fill_key,
            object_len: 0,
        };
        let mut hbuf = vec![0u8; block as usize];
        let hlen = encode_region_header(&mut hbuf, &h, &pl.extents, &[]).map_err(|_| meta_err())?;
        let at = PairAt {
            volume: *inner.volume.as_bytes(),
            object: inner.layout.region_object(pl.id),
            a: pl.offset,
            b: pl.offset + block,
            log2: inner.layout.log2_block,
            format: FORMAT_2,
            numbered: false,
        };
        let blank = PairRead::blank();
        let next = plan_next(&blank, &hbuf[..hlen], inner.owner_generation)?;
        let header = {
            let mut q = inner.queues.get(pl.id as usize);
            durable_slot::<P>(
                &mut q,
                &inner.file,
                &inner.pool,
                &at,
                &blank,
                &next,
                &inner.policy,
                &inner.domain,
            )
            .inspect_err(|_| self.wake_frontiers())?
        };
        // 5. The commit: the page as it is now (other operations may have
        // written it meanwhile) with the entry added, at the next
        // `meta_seq`.
        let mut meta = lock_meta(&inner.meta);
        let meta_seq = crate::v2::next_meta_seq_of(&meta)?;
        let entry = Entry2 {
            seq: pl.seq,
            kind: pl.kind,
            flags: pl.flags,
            compat_flags: 0,
            name: pl.name,
            tag: pl.tag,
            extent_count: pl.extents.len() as u16,
            header_a: pl.offset,
            header_b: pl.offset + block,
            data_size: pl.data_size,
            capacity: pl.capacity,
            span_start: pl.offset,
            span_end: pl.offset + pl.extent,
            footprint_seq: meta_seq,
        };
        let payload = {
            let v2 = meta.v2.as_deref().ok_or_else(corrupt)?;
            let page = v2.pages.get(pl.page).ok_or_else(corrupt)?;
            let ph = page.header.ok_or_else(corrupt)?;
            if page.standing != Standing::Confirmed {
                return Err(not_written(
                    NotWrittenCause::Frozen,
                    ErrorContext::default(),
                ));
            }
            let mut raws = page.entries.clone();
            raws.push(entry.to_raw());
            let next = PageHeader {
                page_index: pl.page as u32,
                // Past this seq and every one committed meanwhile.
                next_seq: ph.next_seq.max(pl.seq + 1),
                count: raws.len() as u16,
            };
            let mut buf = vec![0u8; PAGE_HEADER_LEN + 96 * raws.len()];
            let n = encode_page(&mut buf, &next, &raws).map_err(|_| meta_err())?;
            buf.truncate(n);
            buf
        };
        {
            let mut q = inner.queues.get(0);
            crate::v2::write_page::<P>(
                &mut q,
                &inner.file,
                &inner.pool,
                &inner.layout,
                inner.volume,
                &inner.policy,
                &inner.domain,
                &mut meta,
                pl.page,
                &payload,
                meta_seq,
                inner.owner_generation,
            )
            .inspect_err(|_| self.wake_frontiers())?;
        }
        // Durable: memory follows, in the same hold of the lock.
        holds.armed = false;
        if let Some(v2) = meta.v2.as_deref_mut() {
            v2.settle(pl.extent);
            v2.usage.push((pl.tag, pl.data_size, pl.extent));
        }
        {
            let mut acct = lock_acct(&inner.acct);
            if holds.from.is_some() {
                // The reservation's bytes, counted reserved until now.
                acct.unreserve(pl.tag, pl.extent);
                acct.add_usage(pl.tag, pl.data_size, pl.extent);
            } else {
                acct.add_usage(pl.tag, pl.data_size, 0);
            }
        }
        let state = Arc::new(LiveRegion {
            id: RegionId::from_raw(pl.id),
            name: pl.name,
            kind: pl.kind,
            generation: pl.generation,
            map: match pl.kind == RegionKind::Slot {
                true => crate::regionmap::RegionMap::single(pl.offset + 2 * block, 0),
                false => crate::regionmap::RegionMap::from_extents(&pl.extents, pl.data_size),
            }
            .with_room(store_io_format::meta2::max_extents(inner.layout.log2_block) - 1),
            header_at: [pl.offset, pl.offset + block],
            header: Mutex::new(header),
            frontier: (pl.kind == RegionKind::Append).then(|| {
                AppendFrontier::new(
                    0,
                    pl.data_size,
                    u32::from(inner.layout.log2_block),
                    crate::frontier::ring_for(
                        inner.opts.append_window,
                        crate::frontier::ring_reach(pl.capacity, pl.flags),
                        u32::from(inner.layout.log2_block),
                    ),
                )
            }),
            positioned: std::sync::atomic::AtomicBool::new(true),
            force_sync: Arc::new(std::sync::atomic::AtomicBool::new(false)),
            read_only: std::sync::atomic::AtomicBool::new(false),
            superseded: false,
            foreign: false,
            gate: Gate::new(),
        });
        // Live before its claim ends and the metadata lock is released:
        // `name_taken2` (under that lock) finds the name one way or the
        // other throughout.
        inner
            .regions
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .push(Arc::clone(&state));
        if let Some(v2) = meta.v2.as_deref_mut() {
            v2.claims
                .retain(|c| !(c.page == pl.page && c.seq == pl.seq));
        }
        drop(meta);
        Ok(state)
    }

    /// A name is taken by a live region, by an unavailable one, by an
    /// entry of a kind this version does not know (spec §9 step 5), or by a
    /// provisioning in flight.
    fn name_taken2(&self, meta: &Meta, name: &RegionName) -> Result<(), Error> {
        let taken = || Error::AlreadyExists {
            what: Named::Region,
        };
        let live = self
            .inner
            .regions
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .iter()
            .any(|r| r.name == *name);
        if live
            || meta.unavailable.iter().any(|u| u.name == name.as_str())
            || meta.v2.as_deref().is_some_and(|v| {
                v.reserved_names.contains(name) || v.claims.iter().any(|c| c.name == *name)
            })
        {
            return Err(taken());
        }
        Ok(())
    }
}

/// What phase 1 of a format-2 provisioning or growth beyond capacity holds
/// until its commit: its marked space, its claim on a name and a table slot,
/// its tag's charge or its reservation's bytes. Dropped armed by a failure
/// before the commit, it gives them all back; the commit disarms it. Once a
/// write was submitted and failed (the store poisoned), or on a panic
/// (which poisons the store), nothing is given back: that commit may have
/// landed, so the space stays held until the store is reopened (a
/// reservation's held bytes then stay counted reserved too). While a
/// reservation-backed provisioning fills, `space()` counts its extent both
/// in `regions` (marked) and in `reserved` (held): `released` is low by
/// that much until the commit. Built after phase 1 releases the metadata
/// lock (its drop takes that lock).
pub(crate) struct Holds<'a, P: Platform>
where
    P::Queue: Send,
{
    pub(crate) store: Store<P>,
    /// The ranges marked allocated.
    pub(crate) ranges: Vec<(u64, u64)>,
    /// A provisioning's claimed table slot `(page, seq)` (its name with it).
    pub(crate) claim: Option<(usize, u32)>,
    pub(crate) tag: u32,
    /// Physical bytes charged to the tag at the mark.
    pub(crate) charged: u64,
    /// The reservation the bytes were held from, and how many.
    pub(crate) from: Option<(&'a mut Reservation<P>, u64)>,
    pub(crate) armed: bool,
}

impl<P: Platform> Drop for Holds<'_, P>
where
    P::Queue: Send,
{
    fn drop(&mut self) {
        if !self.armed {
            return;
        }
        // A panic may come after a write was submitted (the commit may have
        // landed): fail-stop, keeping every hold, as a failure after
        // submission does.
        if std::thread::panicking() {
            self.store
                .inner
                .domain
                .poison(store_io_core::error::FirstCause {
                    op: None,
                    raw: None,
                });
            return;
        }
        if self.store.inner.domain.poisoned().is_some() {
            return;
        }
        let inner = &self.store.inner;
        let mut meta = lock_meta(&inner.meta);
        let container = meta.container_len;
        if let Some(v2) = meta.v2.as_deref_mut() {
            if let Some((page, seq)) = self.claim {
                v2.claims.retain(|c| !(c.page == page && c.seq == seq));
            }
            v2.unmark(&self.ranges, container);
        }
        drop(meta);
        if self.charged > 0 {
            lock_acct(&inner.acct).remove_usage(self.tag, 0, self.charged);
        }
        if let Some((r, bytes)) = self.from.take() {
            r.unhold(bytes);
        }
    }
}

/// A format-2 provisioning placed, marked and claimed (phase 1), for
/// [`Store::fill_and_commit2`].
struct Placed {
    kind: RegionKind,
    name: RegionName,
    tag: u32,
    flags: u8,
    id: u64,
    /// Its claimed table slot.
    page: usize,
    seq: u32,
    /// Its header blocks' offset; its data's size and capacity; its whole
    /// footprint's length.
    offset: u64,
    data_size: u64,
    capacity: u64,
    extent: u64,
    extents: Vec<(u64, u64)>,
    /// The whole footprint written (else the slack allocated).
    fill_whole: bool,
    /// Its fill pattern and key, and its region generation (1, or the
    /// source's for a region `copy_store` recreates).
    fill: FillPattern,
    fill_key: [u8; 16],
    generation: Generation,
}

/// A format-2 region placed (phase 1) whose data area is given to it in
/// order, from its first byte (an import's or a [`StoreSink`]'s region,
/// D-170): each piece filled in place in its buffer (one copy), written
/// once full, admitted and completed before the call that filled it
/// returns (the progress rule, R5.4: nothing is in flight between calls);
/// at [`Self::finish`] the rest of the data area and any slack its
/// pattern, then made durable, verified, its header written and committed,
/// as any provisioning. Dropped before that, every hold is given back
/// (`Holds`).
///
/// [`StoreSink`]: crate::StoreSink
pub(crate) struct Pushed<P: Platform>
where
    P::Queue: Send,
{
    pl: Placed,
    holds: Holds<'static, P>,
    /// The bytes from its header blocks on written.
    at: u64,
    /// The piece being filled: its buffer, its length, the bytes in it.
    piece: Option<(IoBuf, u64, usize)>,
}

impl<P: Platform> Store<P>
where
    P::Queue: Send,
{
    /// A format-2 region recreated with `copied`'s attributes, its data
    /// area to be given in order ([`Pushed`]). Refusals as
    /// [`Self::provision2`]'s, before any I/O.
    pub(crate) fn push_region(
        &self,
        name: &str,
        kind: RegionKind,
        opts: crate::RegionOptions,
        copied: &crate::copy::CopyAttrs,
    ) -> Result<Pushed<P>, Error> {
        let (pl, holds) = self.place2(name, kind, opts, None, Some(copied))?;
        Ok(Pushed {
            pl,
            holds,
            at: 0,
            piece: None,
        })
    }
}

impl<P: Platform> Pushed<P>
where
    P::Queue: Send,
{
    /// The data area's bytes given and written (a failed write began
    /// there).
    pub(crate) fn written(&self) -> u64 {
        let block = self.holds.store.inner.layout.block();
        self.at.saturating_sub(2 * block)
    }

    /// Room for the data area's next bytes: the rest of the piece being
    /// filled, a new one taken when there is none (its share of the header
    /// blocks zeroed first, a piece of them alone written at once). Empty
    /// once the data area is whole.
    pub(crate) fn room(&mut self) -> Result<&mut [u8], Error> {
        let block = self.holds.store.inner.layout.block();
        let head = 2 * block + self.pl.data_size;
        loop {
            if self.piece.is_none() {
                if self.at >= head {
                    return Ok(&mut []);
                }
                let (mut buf, len) = self.holds.store.fill_piece(head - self.at)?;
                let z = (2 * block).saturating_sub(self.at).min(len) as usize;
                buf.as_mut_slice()[..z].fill(0);
                self.piece = Some((buf, len, z));
            }
            match &self.piece {
                Some((_, len, n)) if (*n as u64) < *len => break,
                _ => self.write_piece()?,
            }
        }
        match self.piece.as_mut() {
            Some((buf, _, n)) => Ok(&mut buf.as_mut_slice()[*n..]),
            None => Ok(&mut []),
        }
    }

    /// `n` bytes of the [`Self::room`] last given were filled: the piece is
    /// written once full.
    pub(crate) fn filled(&mut self, n: usize) -> Result<(), Error> {
        let Some((_, len, at)) = self.piece.as_mut() else {
            return Err(corrupt());
        };
        let to = at.checked_add(n).filter(|&t| t as u64 <= *len);
        let Some(to) = to else {
            return Err(corrupt());
        };
        *at = to;
        if to as u64 == *len {
            self.write_piece()?;
        }
        Ok(())
    }

    /// Gives `bytes`, the data area's next, in order.
    pub(crate) fn give(&mut self, mut bytes: &[u8]) -> Result<(), Error> {
        while !bytes.is_empty() {
            let room = self.room()?;
            // Past the data area (a backstop: a stream's length is at most
            // its data size).
            if room.is_empty() {
                return Err(corrupt());
            }
            let n = room.len().min(bytes.len());
            room[..n].copy_from_slice(&bytes[..n]);
            self.filled(n)?;
            bytes = &bytes[n..];
        }
        Ok(())
    }

    /// Writes the piece being filled, its unfilled rest the region's
    /// pattern (the data area past the bytes given), each write admitted
    /// first: an admission that ends without one is
    /// `NotWritten(Interrupted)` (R5.4: another part's failure, or pieces of
    /// the call this thread holds that no collector can collect). Sized when
    /// it was taken, a piece larger than the fill's piece now (commits began
    /// while it filled: D-156) goes out as pieces of that size, each whole
    /// blocks and admitted on its own, never holding two of the pool's
    /// buffers; the written length moves past each write done.
    fn write_piece(&mut self) -> Result<(), Error> {
        let store = &self.holds.store;
        let block = store.inner.layout.block();
        let Some((mut buf, len, n)) = self.piece.take() else {
            return Ok(());
        };
        if (n as u64) < len {
            let index = (self.at + n as u64).saturating_sub(2 * block);
            let rest = &mut buf.as_mut_slice()[n..];
            match self.pl.fill {
                FillPattern::KeyedV1 => {
                    store_io_format::fill::fill_keyed_v1(&self.pl.fill_key, index, rest)
                }
                FillPattern::Zeros => rest.fill(0),
            }
        }
        let now = store.fill_piece_len();
        if len <= now {
            store.write_fill_piece(self.pl.offset + self.at, buf, len)?;
            self.at += len;
            return Ok(());
        }
        // Never two of the pool's buffers at once: the rest copied out,
        // the buffer written shortened to the first part, each later part
        // in a buffer taken once the one before it was given back.
        let rest = buf.as_slice()[now as usize..len as usize].to_vec();
        if !buf.set_len(now as usize) {
            return Err(crate::store::corrupt());
        }
        store.write_fill_piece(self.pl.offset + self.at, buf, now)?;
        self.at += now;
        let mut done = 0usize;
        while done < rest.len() {
            let part = (now as usize).min(rest.len() - done);
            let mut b = store.fill_buf(part as u64)?;
            b.as_mut_slice().copy_from_slice(&rest[done..done + part]);
            store.write_fill_piece(self.pl.offset + self.at, b, part as u64)?;
            self.at += part as u64;
            done += part;
        }
        Ok(())
    }

    /// Its data size as placed (a slot's: 0).
    pub(crate) fn data_size(&self) -> u64 {
        self.pl.data_size
    }

    /// The data area's rest (past the bytes given) and any slack its
    /// pattern, then made durable, verified, its header written and
    /// committed: the region live.
    pub(crate) fn finish(&mut self) -> Result<Arc<LiveRegion>, Error> {
        self.write_piece()?;
        let store = self.holds.store.clone();
        store.commit2(&self.pl, &mut self.holds, self.at)
    }
}