coordinode-lsm-tree 6.0.0

Embedded LSM-tree storage engine in pure Rust, no C/C++ dependency. MVCC snapshots, BuRR filters, zstd dictionary compression, columnar PAX blocks, AES-256-GCM at rest, self-healing per-block ECC, compaction on a near-full disk, no_std support.
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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026-present, Dmitry Prudnikov

//! Power-loss crash simulator [`Fs`] backend for recovery testing.
//!
//! [`CrashFs`] wraps an inner [`Fs`] and models the durability contract of a
//! real disk: bytes become durable only when a file handle is `fsync`ed
//! ([`FsFile::sync_all`] / [`FsFile::sync_data`]). [`CrashFs::crash`] simulates
//! a power loss by rolling every file back to its last-synced content and
//! removing files that were never synced at all — exactly the bytes a real
//! crash would lose. Reopening the storage engine on the post-crash backend
//! (via [`CrashFs::inner`]) then exercises its recovery path against the
//! worst-case durable image.
//!
//! Unlike [`FaultFs`](crate::fs::FaultFs), which makes a *chosen* operation
//! return an error, `CrashFs` is about *durability*: every operation succeeds
//! during the run, but a `crash()` reveals which writes were actually durable.
//! The two compose — wrap `CrashFs` in a `FaultFs` to fail a specific `fsync`
//! and then crash to discard the tail that the failed sync never made durable,
//! reproducing a torn write mid-flush.
//!
//! # Model and limitations
//!
//! Durability is tracked at file-content granularity: a file's durable image is
//! its full content as of its most recent successful `sync_all` / `sync_data`.
//! A file written but never synced vanishes on `crash()`; a synced file keeps
//! exactly its last-synced bytes (a later un-synced append or truncate is
//! rolled back). Hard links the backend reports as one file
//! ([`Fs::same_file`]) share one durable image, since they share their bytes;
//! on a backend where a hard link is a copy each name keeps its own. Every
//! name is tracked resolved, as the backend resolves it: a symbolic link,
//! final or in a directory along the way, and `..` lead to the entry they
//! reach, so a file written or synced through any spelling of that kind is
//! the one file. Two names the filesystem alone makes one (a case it
//! ignores, a bind mount) are tracked as two files.
//!
//! A new directory entry (a created file, the destination of a rename, a hard
//! link or a reflink) is durable only once its parent directory is synced, as
//! POSIX promises: `crash()` removes a file whose entry was never made durable,
//! even when its content was synced. The directory synced is matched by what
//! the backend reports as the same directory, so any spelling of it covers
//! its entries. Removed entries are not brought back, except a durable file
//! removed behind a [`SyncMode::Barrier`] sync a power loss loses (see
//! below), and directories themselves are not rolled back. Opens, operations that make or remove
//! entries, and directory syncs are linearized: each
//! happens wholly before or wholly after any other, so an entry is made
//! either before a sync that then covers it or after one that does not.
//!
//! One `CrashFs` is one device. A sync under [`SyncMode::Barrier`], of a file
//! or a directory, keeps writes in order without making them durable: it is
//! queued, and [`Fs::sync_device`], or any other sync on the device, makes
//! every queued one durable. A removal or a rename issued behind a queued
//! sync is queued with it, as one step. [`CrashFs::crash`] loses them all;
//! [`CrashFs::crash_keeping`] keeps a prefix of the steps in the order they
//! were issued, the most a power loss can leave.
//!
//! This is a test/dev surface: it is gated behind the `std` feature and is not
//! part of the production storage path.
//!
//! # Examples
//!
//! ```
//! use lsm_tree::fs::{CrashFs, MemFs};
//!
//! let fs = CrashFs::new(MemFs::new());
//! // ... run a workload through `fs`, fsyncing durable checkpoints ...
//! fs.crash(); // discard everything written since the last fsync of each file
//! // ... reopen the engine on `fs.inner()` and verify recovery ...
//! ```

use super::{
    FileHint, Fs, FsCapabilities, FsDirEntry, FsFile, FsMetadata, FsOpenOptions, SyncMode,
};
use crate::io;
use crate::path::{Path, PathBuf};
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use hashbrown::{HashMap, HashSet};
use std::path::Component;

/// Shared crash state: the durable image of each file plus the set of files
/// written during the current run (so `crash()` can vanish never-synced ones).
#[derive(Default)]
struct CrashState {
    /// Last-synced full content per file path. Presence means "this path has
    /// been made durable at least once"; the bytes are its durable image.
    durable: HashMap<PathBuf, Vec<u8>>,
    /// Every path opened for writing this run, whether or not yet synced.
    /// `crash()` visits these (plus `durable` keys) to roll back or remove.
    touched: HashSet<PathBuf>,
    /// Paths whose directory entry was made this run and whose parent
    /// directory has not been synced since: `crash()` removes them.
    pending_entries: HashSet<PathBuf>,
    /// Names the backend reports as hard links of one file, by group: they
    /// share their bytes, so they share one durable image.
    link_group: HashMap<PathBuf, u64>,
    /// The id the next link group takes.
    next_link_group: u64,
    /// The durable target of each symbolic link a queued removal or rename
    /// took, captured before it went: a crash that loses that step puts the
    /// link back, as it puts a file back from its durable image.
    links: HashMap<PathBuf, Link>,
    /// Hard-linked names a queued removal took out of their link group, with
    /// the group: a crash that loses the removal links the name back to a
    /// name of that group still on disk.
    unlinked: HashMap<PathBuf, u64>,
    /// What [`SyncMode::Barrier`] synced, in order, and no device flush or
    /// full sync has made durable since: a crash keeps a prefix of it.
    ordered: Vec<Ordered>,
}

/// A symbolic link as a crash puts it back: its target, stored as given,
/// and whether it was made as a link to a directory, the kind Windows keeps
/// with the link whatever its target is.
#[derive(Clone)]
struct Link {
    target: PathBuf,
    to_dir: bool,
}

/// One sync [`SyncMode::Barrier`] issued, durable once the device is flushed.
enum Ordered {
    /// A file's content as of its sync.
    Image(PathBuf, Vec<u8>),
    /// The entries a directory sync covered, each with whether it names a
    /// regular file.
    Entries(Vec<(PathBuf, bool)>),
    /// A removal issued behind an ordered sync: a barrier keeps it behind
    /// that sync, so a power loss that loses the sync loses the removal too
    /// and leaves the file as the syncs before it made it. The flag says the
    /// name's entry was not durable yet when it was removed.
    Removed(PathBuf, bool),
    /// A rename issued behind an ordered sync, kept or lost whole: kept, the
    /// destination's old file and the source's name are gone and the
    /// destination holds `bytes`; lost, both names are as the syncs before it
    /// left them. The flags say each name's entry was not durable yet.
    Renamed {
        from: PathBuf,
        to: PathBuf,
        from_pending: bool,
        to_pending: bool,
        bytes: Option<Vec<u8>>,
    },
}

impl CrashState {
    /// Makes durable the first `count` ordered steps, in the order they were
    /// issued, and drops the rest.
    fn settle(&mut self, count: usize) {
        let ordered = core::mem::take(&mut self.ordered);
        // A kept rename made its destination's entry with it: the entry is
        // durable from there, unless a later KEPT step takes the name again
        // and leaves whatever holds it then to its own sync. A lost step
        // changes nothing on disk, so the name keeps the renamed file.
        let renamed_durable: Vec<PathBuf> = ordered
            .iter()
            .enumerate()
            .take(count)
            .filter_map(|(at, sync)| match sync {
                Ordered::Renamed { to, .. } => Some((at, to)),
                _ => None,
            })
            .filter(|&(at, to)| {
                !ordered
                    .iter()
                    .take(count)
                    .skip(at + 1)
                    .any(|later| match later {
                        Ordered::Removed(name, _) => name == to,
                        Ordered::Renamed {
                            from: source,
                            to: target,
                            ..
                        } => source == to || target == to,
                        _ => false,
                    })
            })
            .map(|(_, to)| to.clone())
            .collect();
        for to in &renamed_durable {
            self.pending_entries.remove(to);
        }
        let mut ordered = ordered.into_iter();
        for sync in ordered.by_ref().take(count) {
            match sync {
                Ordered::Image(path, bytes) => self.set_durable(&path, bytes),
                Ordered::Entries(entries) => {
                    for (entry, file) in &entries {
                        self.entry_made_durable(entry, *file);
                    }
                }
                // Kept, in its place: the file is gone from here on, and an
                // image of a file made under its name later still lands.
                Ordered::Removed(path, _) => {
                    self.durable.remove(&path);
                    self.links.remove(&path);
                    self.unlinked.remove(&path);
                }
                Ordered::Renamed {
                    from, to, bytes, ..
                } => {
                    self.durable.remove(&to);
                    self.durable.remove(&from);
                    self.links.remove(&to);
                    let link = self.links.remove(&from);
                    if let Some(bytes) = bytes {
                        self.set_durable(&to, bytes);
                    } else if let Some(target) = link {
                        // The link moved, under its new name.
                        self.links.insert(to, target);
                    }
                }
            }
        }
        // A removal behind a lost sync is lost with it, and so is everything
        // made under that name after it: no sync that could make the new
        // entry durable comes before the lost one. The file stays as the kept
        // syncs left it; an entry made only after the removal is dropped,
        // one that was pending before it stays pending. A lost rename leaves
        // both of its names so.
        for sync in ordered {
            match sync {
                Ordered::Removed(path, false) => {
                    self.pending_entries.remove(&path);
                }
                Ordered::Renamed {
                    from,
                    to,
                    from_pending,
                    to_pending,
                    ..
                } => {
                    if !from_pending {
                        self.pending_entries.remove(&from);
                    }
                    if !to_pending {
                        self.pending_entries.remove(&to);
                    }
                }
                _ => {}
            }
        }
    }

    /// Makes durable every ordered step: a device flush, or a full sync that
    /// flushes the device cache with it.
    fn settle_all(&mut self) {
        self.settle(self.ordered.len());
    }

    /// The entry `path` made durable by a sync of its directory. A regular
    /// file none of whose bytes was ever synced is still there after a power
    /// loss, empty: the file was made with its entry, and the bytes written
    /// since are what no sync covered.
    fn entry_made_durable(&mut self, path: &Path, file: bool) {
        self.pending_entries.remove(path);
        if file && !self.durable.contains_key(path) {
            self.set_durable(path, Vec::new());
        }
    }

    /// Drops what the ordered syncs still owe the name `path`, removed or
    /// replaced: a sync of its old file or its old entry does not carry over
    /// to whatever takes the name.
    fn forget_ordered(&mut self, path: &Path) {
        self.ordered.retain_mut(|sync| match sync {
            Ordered::Image(name, _) => name != path,
            Ordered::Entries(entries) => {
                entries.retain(|(entry, _)| entry != path);
                true
            }
            Ordered::Removed(..) | Ordered::Renamed { .. } => true,
        });
    }

    /// `path` and every name linked to the same file.
    fn names_of(&self, path: &Path) -> Vec<PathBuf> {
        match self.link_group.get(path) {
            Some(&group) => self
                .link_group
                .iter()
                .filter(|&(_, &other)| other == group)
                .map(|(name, _)| name.clone())
                .collect(),
            None => alloc::vec![path.to_path_buf()],
        }
    }

    /// Records `bytes` as the durable image of `path` and of every name
    /// linked to it.
    fn set_durable(&mut self, path: &Path, bytes: Vec<u8>) {
        let mut names = self.names_of(path);
        let last = names.pop().unwrap_or_else(|| path.to_path_buf());
        for name in names {
            self.durable.insert(name, bytes.clone());
        }
        self.durable.insert(last, bytes);
    }

    /// Forgets the name `path`, removed from the namespace. The file it named
    /// lives on under its other hard links, and what the file went through
    /// stays with them: a write through the removed name that was never
    /// synced is still unsynced under each of them.
    fn forget_name(&mut self, path: &Path) {
        self.forget_ordered(path);
        self.durable.remove(path);
        self.links.remove(path);
        self.unlinked.remove(path);
        self.pending_entries.remove(path);
        let touched = self.touched.remove(path);
        if let Some(group) = self.link_group.remove(path)
            && touched
        {
            let survivors: Vec<PathBuf> = self
                .link_group
                .iter()
                .filter(|&(_, &other)| other == group)
                .map(|(name, _)| name.clone())
                .collect();
            self.touched.extend(survivors);
        }
    }

    /// Records `dst` as a hard link of the file `src` names.
    fn link(&mut self, src: &Path, dst: &Path) {
        let group = if let Some(&group) = self.link_group.get(src) {
            group
        } else {
            let group = self.next_link_group;
            // One group per hard link a test makes, far below 2^64.
            self.next_link_group += 1;
            self.link_group.insert(src.to_path_buf(), group);
            group
        };
        self.link_group.insert(dst.to_path_buf(), group);
    }
}

/// How many symbolic links a path may pass through before the chain is
/// refused: Linux's `MAXSYMLINKS`.
const MAX_SYMLINKS: usize = 40;

/// The components of `path`, each as a path of its own, last first.
fn components_reversed(path: &Path) -> Vec<PathBuf> {
    path.components()
        .rev()
        .map(|component| PathBuf::from(component.as_os_str()))
        .collect()
}

/// `path` with `.` and `..` folded away by spelling alone; `..` of a root is
/// the root, a `..` with nothing to climb out of stays, and nothing left is
/// `.`.
fn folded(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => match out.components().next_back() {
                Some(Component::Normal(_)) => {
                    out.pop();
                }
                Some(Component::RootDir | Component::Prefix(_)) => {}
                _ => out.push(component.as_os_str()),
            },
            other => out.push(other.as_os_str()),
        }
    }
    if out.as_os_str().is_empty() {
        out.push(".");
    }
    out
}

/// A power-loss crash simulator wrapping an inner [`Fs`].
///
/// See the module-level documentation for the durability model. The inner backend is
/// held as an [`Arc<dyn Fs>`]; obtain a clone with [`inner`](Self::inner) to
/// reopen the engine directly on the post-crash store (e.g. via
/// [`Config::with_shared_fs`](crate::Config::with_shared_fs)).
#[derive(Clone)]
pub struct CrashFs {
    inner: Arc<dyn Fs>,
    state: Arc<spin::Mutex<CrashState>>,
    /// Linearizes the namespace: every operation that makes or removes a
    /// directory entry, every open and every write that resolves the entry it
    /// lands on,
    /// holds it from its checks through the backend call to the state it
    /// records, and a directory sync holds it from the backend
    /// sync to clearing what that sync made durable. An entry is then made
    /// either before a sync, and covered by it, or after, and pending; and
    /// whether an open or a rename made an entry is decided by probes no
    /// other entry change can interleave with. Blocking, not spinning: a
    /// directory sync holds it across a system call.
    namespace: Arc<parking_lot::Mutex<()>>,
}

impl CrashFs {
    /// Wraps `inner`, treating its current contents as the initial durable
    /// image (nothing is rolled back until something is written and then a
    /// `crash()` occurs).
    #[must_use]
    pub fn new<F: Fs>(inner: F) -> Self {
        Self::from_shared(Arc::new(inner))
    }

    /// Wraps an existing shared backend handle.
    #[must_use]
    pub fn from_shared(inner: Arc<dyn Fs>) -> Self {
        Self {
            inner,
            state: Arc::new(spin::Mutex::new(CrashState::default())),
            namespace: Arc::new(parking_lot::Mutex::new(())),
        }
    }

    /// Returns a clone of the wrapped backend handle, for reopening the engine
    /// on the same store after a [`crash`](Self::crash).
    #[must_use]
    pub fn inner(&self) -> Arc<dyn Fs> {
        Arc::clone(&self.inner)
    }

    /// Simulates a power loss: every file is rolled back to its last-synced
    /// content, and files written but never synced are removed. After this the
    /// backend holds exactly the bytes a real crash would have left durable.
    ///
    /// # Panics
    ///
    /// Panics if rolling a file back to its durable image fails (open / write /
    /// remove on the inner backend). A crash simulator that could not restore
    /// the durable image would silently under-test recovery, so the failure is
    /// surfaced loudly rather than swallowed. In-memory backends never hit this.
    pub fn crash(&self) {
        self.crash_keeping(0);
    }

    /// How many ordered steps no device flush has made durable yet: each sync
    /// [`SyncMode::Barrier`] issued, and each removal or rename issued behind
    /// one, which the barrier orders like a sync. A power loss keeps some
    /// prefix of them, each step whole, which
    /// [`crash_keeping`](Self::crash_keeping) picks.
    #[must_use]
    pub fn ordered_steps(&self) -> usize {
        self.state.lock().ordered.len()
    }

    /// Simulates a power loss that keeps the first `kept` of the
    /// [ordered steps](Self::ordered_steps), in the order they were issued,
    /// and loses the rest, then rolls back as [`crash`](Self::crash) does.
    ///
    /// # Panics
    ///
    /// As [`crash`](Self::crash).
    pub fn crash_keeping(&self, kept: usize) {
        let mut state = self.state.lock();
        state.settle(kept);
        // An entry its directory never made durable is lost with whatever
        // content it had.
        let lost: Vec<PathBuf> = state.pending_entries.drain().collect();
        for path in &lost {
            state.durable.remove(path);
            state.links.remove(path);
            state.link_group.remove(path);
            state.touched.insert(path.clone());
        }
        // Visit every path we wrote, plus any durable path (defensive: a file
        // synced in a prior life but only read this run still gets its durable
        // image reasserted).
        let paths: Vec<PathBuf> = state
            .touched
            .iter()
            .chain(state.durable.keys())
            .chain(state.links.keys())
            .cloned()
            .collect();

        for path in paths {
            if let Some(link) = state.links.get(&path) {
                self.restore_link(&path, link);
                continue;
            }
            if state.unlinked.contains_key(&path) {
                // Linked back below, once its group's names are restored.
                continue;
            }
            match state.durable.get(&path) {
                Some(bytes) => self.restore_durable(&path, bytes),
                None => {
                    // Never synced -> the file never existed durably.
                    match self.inner.remove_file(&path) {
                        Ok(()) => {}
                        Err(e) if e.kind() == io::ErrorKind::NotFound => {}
                        Err(e) => {
                            panic!("crash(): removing un-synced {} failed: {e}", path.display())
                        }
                    }
                }
            }
        }

        // A hard link whose removal was lost comes back as a link of a name
        // its file still has, not as a copy of it.
        let unlinked: Vec<(PathBuf, u64)> = state.unlinked.drain().collect();
        for (path, group) in unlinked {
            self.relink(&state, &path, group);
        }

        // Post-crash, only durable files exist and they are "clean"; a link
        // put back is the disk's own again, untouched by this run.
        state.touched = state.durable.keys().cloned().collect();
        state.links.clear();
        let CrashState {
            durable,
            link_group,
            ..
        } = &mut *state;
        link_group.retain(|name, _| durable.contains_key(name));
    }

    /// Purges the crash state of every tracked path under the removed folder
    /// `path`, so a crash neither resurrects nor panics recreating a file
    /// whose parent is gone.
    fn forget_folder(&self, path: &Path) {
        let name = self.name_after(path);
        let path = name.as_path();
        let mut state = self.state.lock();
        // A linked name under the directory hands what its file went through
        // to the names that survive outside it.
        let linked: Vec<PathBuf> = state
            .link_group
            .keys()
            .filter(|k| k.starts_with(path))
            .cloned()
            .collect();
        for name in &linked {
            state.forget_name(name);
        }
        state.durable.retain(|k, _| !k.starts_with(path));
        state.touched.retain(|k| !k.starts_with(path));
        state.links.retain(|k, _| !k.starts_with(path));
        state.unlinked.retain(|k, _| !k.starts_with(path));
        state.pending_entries.retain(|k| !k.starts_with(path));
        state.ordered.retain_mut(|sync| match sync {
            Ordered::Image(name, _) | Ordered::Removed(name, _) => !name.starts_with(path),
            Ordered::Renamed { from, to, .. } => !from.starts_with(path) && !to.starts_with(path),
            Ordered::Entries(entries) => {
                entries.retain(|(entry, _)| !entry.starts_with(path));
                true
            }
        });
    }

    /// Captures what the name `name` holds before a queued step may take it,
    /// on its first touch this run: a file's bytes as its durable image, a
    /// symbolic link's target as the link to put back.
    fn capture_name(&self, name: &Path) -> io::Result<()> {
        match self.inner.read_link(name) {
            Ok(None) => self.capture_first_touch(name),
            Ok(Some(target)) => {
                let to_dir = self.inner.symlink_to_dir(name)?;
                let mut state = self.state.lock();
                if !state.touched.contains(name) && !state.links.contains_key(name) {
                    state
                        .links
                        .insert(name.to_path_buf(), Link { target, to_dir });
                }
                state.touched.insert(name.to_path_buf());
                Ok(())
            }
            // A probe that fails says nothing to capture; the step itself
            // reports what is wrong with the name.
            Err(_) => Ok(()),
        }
    }

    /// Makes `path` a hard link of a name of link group `group` on disk, in
    /// place of whatever the run left under it; with no such name left, the
    /// file is gone with every name, and so is `path`.
    fn relink(&self, state: &CrashState, path: &Path, group: u64) {
        let source = state
            .link_group
            .iter()
            .filter(|&(_, &other)| other == group)
            .map(|(name, _)| name.clone())
            .find(|name| matches!(self.inner.exists(name), Ok(true)));
        match self.inner.remove_file(path) {
            Ok(()) => {}
            Err(e) if e.kind() == io::ErrorKind::NotFound => {}
            Err(e) => panic!(
                "crash(): clearing {} for its link failed: {e}",
                path.display()
            ),
        }
        let Some(source) = source else {
            return;
        };
        self.inner.hard_link(&source, path).unwrap_or_else(|e| {
            panic!(
                "crash(): linking {} back to {} failed: {e}",
                path.display(),
                source.display()
            )
        });
    }

    /// Puts back the symbolic link `path`, in place of whatever the run left
    /// under the name.
    fn restore_link(&self, path: &Path, link: &Link) {
        let target = &link.target;
        if matches!(self.inner.read_link(path), Ok(Some(now)) if now == *target)
            && matches!(self.inner.symlink_to_dir(path), Ok(kind) if kind == link.to_dir)
        {
            return;
        }
        match self.inner.remove_file(path) {
            Ok(()) => {}
            Err(e) if e.kind() == io::ErrorKind::NotFound => {}
            Err(e) => panic!(
                "crash(): clearing {} for its link failed: {e}",
                path.display()
            ),
        }
        self.inner
            .symlink(target, path, link.to_dir)
            .unwrap_or_else(|e| {
                panic!(
                    "crash(): putting back the link {} failed: {e}",
                    path.display()
                )
            });
    }

    /// Overwrites the inner file at `path` with its durable image `bytes`.
    fn restore_durable(&self, path: &Path, bytes: &[u8]) {
        let mut file = self
            .inner
            .open(
                path,
                &FsOpenOptions::new().write(true).create(true).truncate(true),
            )
            .unwrap_or_else(|e| {
                panic!(
                    "crash(): reopening {} for rollback failed: {e}",
                    path.display()
                )
            });
        std::io::Write::write_all(&mut file, bytes).unwrap_or_else(|e| {
            panic!(
                "crash(): rewriting durable image of {} failed: {e}",
                path.display()
            )
        });
    }

    /// Reads the current content of `path` if it already exists, for use as the
    /// initial durable baseline. Returns `Ok(None)` only when the file does not
    /// exist (a brand-new file has no durable image until its first sync); any
    /// other open/read error is propagated rather than swallowed, so a transient
    /// read failure cannot silently drop a pre-existing file's durable image.
    fn read_baseline(&self, path: &Path) -> io::Result<Option<Vec<u8>>> {
        let mut f = match self.inner.open(path, &FsOpenOptions::new().read(true)) {
            Ok(f) => f,
            Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
            Err(e) => return Err(e),
        };
        let mut buf = Vec::new();
        std::io::Read::read_to_end(&mut f, &mut buf)?;
        Ok(Some(buf))
    }

    /// Records `path`'s pre-existing content as its durable baseline on the FIRST
    /// touch of this run, then marks it touched. Re-touching is a no-op for the
    /// baseline (a file already touched is tracked by sync; re-reading it would
    /// wrongly promote un-synced bytes to durable). Used by every entry point
    /// that creates or mutates a file's content: `open` (writable), `punch_hole`,
    /// `truncate_file`.
    fn capture_first_touch(&self, path: &Path) -> io::Result<()> {
        let pb = path.to_path_buf();
        // A file is first touched when none of its names was: a write through
        // a linked name already put un-synced bytes in it, and its durable
        // image is the one its names share.
        let first_touch = {
            let state = self.state.lock();
            !state
                .names_of(path)
                .iter()
                .any(|name| state.touched.contains(name))
        };
        if first_touch {
            let baseline = self.read_baseline(path)?;
            if let Some(bytes) = baseline {
                self.state.lock().set_durable(path, bytes);
            }
        }
        self.state.lock().touched.insert(pb);
        Ok(())
    }

    /// The entry a write through `path` lands on: `path` with every symlink
    /// it passes through resolved, the final one included, which is the
    /// entry an open that creates makes and the one whose bytes a sync makes
    /// durable.
    fn entry_of(&self, path: &Path) -> io::Result<PathBuf> {
        self.resolve(path, true)
    }

    /// The entry `path` names itself, as `rename`, `unlink` and `link` see
    /// it: its directory resolved, its final component kept even when it is
    /// a symlink.
    fn name_of(&self, path: &Path) -> io::Result<PathBuf> {
        self.resolve(path, false)
    }

    /// [`Self::name_of`] for an operation the backend has already done: the
    /// operation stands, so a probe that cannot answer leaves the name as it
    /// was given rather than failing it.
    fn name_after(&self, path: &Path) -> PathBuf {
        self.name_of(path).unwrap_or_else(|_| path.to_path_buf())
    }

    /// `path` spelled the one way every name of its entry is tracked under,
    /// so a file reached through a symlinked directory or through `..` is the
    /// file reached through its real directory.
    ///
    /// Symlinks are resolved component by component, as the kernel walks a
    /// path, through at most `MAX_SYMLINKS` links. Then `.` and `..` are
    /// folded away, which is what the walk would do over a prefix that holds
    /// no symlink, but only when the backend agrees that the folded directory
    /// is the one it reaches: a backend that keeps `..` as part of a name
    /// (`MemFs`) gets the name it was given.
    fn resolve(&self, path: &Path, follow_final: bool) -> io::Result<PathBuf> {
        let mut resolved = PathBuf::new();
        // Components still to walk, the next one last.
        let mut rest: Vec<PathBuf> = components_reversed(path);
        let mut followed = 0;
        while let Some(part) = rest.pop() {
            if !matches!(part.components().next(), Some(Component::Normal(_))) {
                // A root replaces what was walked, `.` adds nothing, and `..`
                // is folded below, once the prefix it climbs out of is known
                // to hold no symlink.
                if !matches!(part.components().next(), Some(Component::CurDir)) {
                    resolved.push(part);
                }
                continue;
            }
            let candidate = resolved.join(&part);
            if rest.is_empty() && !follow_final {
                resolved = candidate;
                break;
            }
            match self.inner.read_link(&candidate)? {
                Some(target) => {
                    // MAX_SYMLINKS links are followed; only a link past them
                    // is refused.
                    if followed == MAX_SYMLINKS {
                        return Err(io::Error::new(
                            io::ErrorKind::InvalidInput,
                            "too many levels of symbolic links",
                        ));
                    }
                    followed += 1;
                    // A relative target continues from the directory holding
                    // the link; an absolute one starts again at its root.
                    rest.extend(components_reversed(&target));
                }
                None => resolved = candidate,
            }
        }
        if resolved.as_os_str().is_empty() {
            // `.` alone: the current directory, as the engine names it.
            resolved.push(".");
        }
        let folded = folded(&resolved);
        if folded == resolved {
            return Ok(resolved);
        }
        // The entry itself may not exist yet (an open that creates), its
        // directory does: that is what the backend is asked about.
        let (spelled, plain) = match (resolved.components().next_back(), folded.file_name()) {
            (Some(Component::Normal(_)), Some(_)) => (
                crate::file::entry_directory(&resolved).to_path_buf(),
                crate::file::entry_directory(&folded).to_path_buf(),
            ),
            _ => (resolved.clone(), folded.clone()),
        };
        Ok(
            if matches!(self.inner.same_file(&spelled, &plain), Ok(true)) {
                folded
            } else {
                resolved
            },
        )
    }

    /// Whether `path`'s directory lists an entry spelled exactly as its final
    /// component. A listing that cannot be read answers no.
    fn listed_as_spelled(&self, path: &Path) -> bool {
        let Some(name) = path.file_name() else {
            return false;
        };
        self.inner
            .read_dir(crate::file::entry_directory(path))
            .is_ok_and(|entries| {
                entries
                    .iter()
                    .any(|entry| std::ffi::OsStr::new(&entry.file_name) == name)
            })
    }

    /// Records that `path` got a new directory entry, durable once its parent
    /// directory is synced.
    fn new_entry(&self, path: &Path) {
        self.state.lock().pending_entries.insert(path.to_path_buf());
    }

    /// Runs the backend sync of `directory` and then makes its pending
    /// entries durable, with the namespace held throughout: no entry can be
    /// made or removed between the backend sync and what it is credited with.
    fn sync_entries_of(
        &self,
        directory: &Path,
        mode: SyncMode,
        sync: impl FnOnce() -> io::Result<()>,
    ) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        sync()?;
        // Entries are tracked under their resolved names, so the directory
        // is matched resolved too: a sync through a symlink or `..` covers
        // the entries of the directory it reaches. The sync is done, so a
        // probe that cannot answer matches the directory as it was named.
        let directory = self
            .entry_of(directory)
            .unwrap_or_else(|_| directory.to_path_buf());
        // A spelling that resolving does not fold, a case the filesystem
        // ignores or a bind mount, still reaches the same directory: what
        // the backend says about identity decides, outside the state lock.
        // The namespace is held, so the pending set cannot change meanwhile.
        let pending = self.state.lock().pending_entries.clone();
        // Each with whether it names a regular file, which a power loss
        // leaves in place once its entry is durable (see
        // `CrashState::entry_made_durable`); asked of the backend here, the
        // state lock not held.
        let covered: Vec<(PathBuf, bool)> = pending
            .into_iter()
            .filter(|entry| {
                let parent = crate::file::entry_directory(entry);
                parent == directory || matches!(self.inner.same_file(parent, &directory), Ok(true))
            })
            .map(|entry| {
                let file = !matches!(self.inner.read_link(&entry), Ok(Some(_)))
                    && self.inner.metadata(&entry).is_ok_and(|meta| meta.is_file);
                (entry, file)
            })
            .collect();
        let mut state = self.state.lock();
        // A barrier makes the entries durable only once the device is
        // flushed; any other sync flushes what was ordered before it too.
        if mode == SyncMode::Barrier {
            state.ordered.push(Ordered::Entries(covered));
            return Ok(());
        }
        state.settle_all();
        for (entry, file) in &covered {
            state.entry_made_durable(entry, *file);
        }
        Ok(())
    }

    /// Records the destination of a copy-style op (`hard_link` / `reflink`): it
    /// mirrors the source's durability so a crash either restores the
    /// linked/cloned bytes (durable source) or removes an un-synced copy.
    ///
    /// The source's durable image comes from one of two places: an entry already
    /// in `durable` (synced or captured this run), or, for a source that is
    /// pre-existing and never touched this run, its on-disk baseline (the same
    /// "current contents are initially durable" rule [`Self::capture_first_touch`]
    /// applies). A source that is touched but not durable holds un-synced bytes,
    /// so the copy correctly inherits no durable image and a crash removes it.
    ///
    /// Reads the baseline outside the state lock (mirroring
    /// [`Self::capture_first_touch`]) so backend I/O never runs under the mutex.
    fn track_copy(&self, src: &Path, dst: &Path) -> io::Result<()> {
        // The backend made `dst` already: record it as a written, pending
        // entry before the fallible baseline read, so a read that fails
        // leaves an entry a crash removes rather than one it never saw.
        let (src_durable, src_touched) = {
            let mut state = self.state.lock();
            state.touched.insert(dst.to_path_buf());
            state.pending_entries.insert(dst.to_path_buf());
            (state.durable.get(src).cloned(), state.touched.contains(src))
        };
        let dst_image = match src_durable {
            Some(bytes) => Some(bytes),
            None if !src_touched => self.read_baseline(src)?,
            None => None,
        };
        if let Some(bytes) = dst_image {
            self.state.lock().durable.insert(dst.to_path_buf(), bytes);
        }
        Ok(())
    }
}

impl Fs for CrashFs {
    fn open(&self, path: &Path, opts: &FsOpenOptions) -> io::Result<Box<dyn FsFile>> {
        let writable = opts.write || opts.create || opts.create_new || opts.append || opts.truncate;
        // An open holds the namespace from resolving its entry to the
        // registration: the backend follows the same symlinks to the same
        // file, and whether an open that may create made the entry is what
        // the open did. A handle opened only for reading resolves too, since
        // a sync through it makes the file it reached durable.
        let may_create = opts.create || opts.create_new;
        let namespace = self.namespace.lock();
        // An open that may only create (`O_EXCL`) does not follow a final
        // symlink and refuses any existing name: the backend answers whether
        // it can, and if it does the file is new, at the path itself, with no
        // prior image to capture.
        if opts.create_new {
            let inner = self.inner.open(path, opts)?;
            let entry = self.name_after(path);
            self.state.lock().touched.insert(entry.clone());
            self.new_entry(&entry);
            drop(namespace);
            return Ok(Box::new(CrashFile {
                inner,
                path: entry,
                fs: Arc::clone(&self.inner),
                state: Arc::clone(&self.state),
            }));
        }
        // A write follows symlinks: through a dangling one it creates the
        // target, so the target is the entry made and the file tracked.
        if !writable {
            // The open stands, so a probe that cannot answer leaves the
            // handle tracked under the name it was opened with.
            let inner = self.inner.open(path, opts)?;
            let entry = self.entry_of(path).unwrap_or_else(|_| path.to_path_buf());
            drop(namespace);
            return Ok(Box::new(CrashFile {
                inner,
                path: entry,
                fs: Arc::clone(&self.inner),
                state: Arc::clone(&self.state),
            }));
        }
        let entry = self.entry_of(path)?;
        let creates = may_create && !self.inner.exists(&entry)?;
        // Capture the pre-existing durable image BEFORE the open (which may
        // truncate); a brand-new file captures nothing, so a crash before its
        // first sync removes it.
        self.capture_first_touch(&entry)?;
        let inner = self.inner.open(path, opts)?;
        if creates {
            self.new_entry(&entry);
        }
        drop(namespace);
        Ok(Box::new(CrashFile {
            inner,
            path: entry,
            fs: Arc::clone(&self.inner),
            state: Arc::clone(&self.state),
        }))
    }

    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        self.inner.create_dir_all(path)
    }

    fn create_dir(&self, path: &Path) -> io::Result<()> {
        self.inner.create_dir(path)
    }

    fn read_dir(&self, path: &Path) -> io::Result<Vec<FsDirEntry>> {
        self.inner.read_dir(path)
    }

    fn remove_file(&self, path: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        // A removal that may be queued (see below) can be lost, and then the
        // file is back as it was: a file this run never touched has its bytes
        // captured now, before they are gone. A symlink names no bytes of
        // its own. Captured whether or not a step is queued yet: a barrier
        // sync on another thread can queue one before the removal is
        // recorded, and only the namespace changes are serialized here.
        let name = self.name_after(path);
        self.capture_name(&name)?;
        self.inner.remove_file(path)?;
        let name = self.name_after(path);
        let mut state = self.state.lock();
        // Behind an ordered sync, the removal is ordered too: a power loss
        // that keeps only a prefix of the syncs may lose it, so what the file
        // went through stays queued, in order, for the crash to replay.
        if state.ordered.is_empty() {
            state.forget_name(&name);
            return Ok(());
        }
        let was_pending = state.pending_entries.contains(&name);
        // A name with hard links leaves its group now, so a file made under
        // it later is its own; a lost removal links it back to the group.
        if state.names_of(&name).len() > 1
            && let Some(group) = state.link_group.remove(&name)
        {
            if state.touched.contains(&name) {
                let survivors: Vec<PathBuf> = state
                    .link_group
                    .iter()
                    .filter(|&(_, &other)| other == group)
                    .map(|(other, _)| other.clone())
                    .collect();
                state.touched.extend(survivors);
            }
            state.durable.remove(&name);
            state.unlinked.insert(name.clone(), group);
        }
        state.touched.insert(name.clone());
        state.ordered.push(Ordered::Removed(name, was_pending));
        Ok(())
    }

    fn remove_dir(&self, path: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        self.inner.remove_dir(path)?;
        self.forget_folder(path);
        Ok(())
    }

    fn remove_dir_all(&self, path: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        self.inner.remove_dir_all(path)?;
        self.forget_folder(path);
        Ok(())
    }

    fn same_file(&self, a: &Path, b: &Path) -> io::Result<bool> {
        self.inner.same_file(a, b)
    }

    fn read_link(&self, path: &Path) -> io::Result<Option<PathBuf>> {
        self.inner.read_link(path)
    }

    fn rename(&self, from: &Path, to: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        // POSIX rename(2): when both entries are links to one file (one path,
        // or two hard links of one inode) the call succeeds and changes
        // nothing, and the crash state stays as it was. Two names the backend
        // reports as one file may still be two entries (two symlinks to one
        // file) or one entry under two spellings (a filesystem that ignores
        // case), so whether the rename changed anything is told by the exact
        // spellings its directories list, before and after it. The namespace
        // is held, so nothing else changes them meanwhile, and the inner
        // backend is the disk this simulates: a fault layer composes above the
        // simulator, never below it.
        let spelled = || (self.listed_as_spelled(from), self.listed_as_spelled(to));
        let before = matches!(self.inner.same_file(from, to), Ok(true)).then(spelled);
        // A rename that may be queued (see below) can be lost, and then both
        // names are back as they were: files this run never touched have
        // their bytes captured now, before the rename moves or replaces them.
        // A symlink names no bytes of its own. Captured whether or not a step
        // is queued yet, as a removal's are.
        if from != to {
            for name in [self.name_after(from), self.name_after(to)] {
                self.capture_name(&name)?;
            }
        }
        self.inner.rename(from, to)?;
        // Without a file identity to go on (a backend that has none, a
        // dangling symlink), the source still being there, the link itself
        // rather than what it points to, under its own spelling, is what
        // tells. A probe that fails is read as an ordinary rename.
        let unchanged = match before {
            Some(before) => before == spelled(),
            None => {
                (matches!(self.inner.read_link(from), Ok(Some(_)))
                    || matches!(self.inner.exists(from), Ok(true)))
                    && self.listed_as_spelled(from)
            }
        };
        if from == to || unchanged {
            return Ok(());
        }
        let (from, to) = (self.name_after(from), self.name_after(to));
        let (from, to) = (from.as_path(), to.as_path());
        let mut state = self.state.lock();
        // Behind an ordered sync, the rename is ordered too, as a removal is,
        // and as one step: a power loss that keeps only a prefix of the syncs
        // either keeps it whole or loses it, and with it everything after,
        // leaving both names as the kept syncs made them. Kept, the
        // destination's old file and the source's name are gone and the
        // destination holds the bytes the source had durable or queued by
        // then. Names with hard links keep the immediate rename.
        if !state.ordered.is_empty()
            && state.names_of(from).len() == 1
            && state.names_of(to).len() <= 1
        {
            // The last queued step that took the source's name from the file
            // it held (a removal, a rename away or onto it): what the name
            // had before belongs to that file. The file renamed now has only
            // what that step and the ones after gave it.
            let since = state.ordered.iter().rposition(|sync| match sync {
                Ordered::Removed(name, _) => name.as_path() == from,
                Ordered::Renamed {
                    from: source,
                    to: target,
                    ..
                } => source.as_path() == from || target.as_path() == from,
                _ => false,
            });
            let queued = state
                .ordered
                .iter()
                .skip(since.unwrap_or(0))
                .rev()
                .find_map(|sync| match sync {
                    Ordered::Image(name, bytes) if name.as_path() == from => Some(bytes.clone()),
                    Ordered::Renamed {
                        to: target,
                        bytes: Some(bytes),
                        ..
                    } if target.as_path() == from => Some(bytes.clone()),
                    _ => None,
                });
            let bytes = queued.or_else(|| {
                since
                    .is_none()
                    .then(|| state.durable.get(from).cloned())
                    .flatten()
            });
            let to_pending = state.pending_entries.contains(to);
            let from_pending = state.pending_entries.contains(from);
            state.ordered.push(Ordered::Renamed {
                from: from.to_path_buf(),
                to: to.to_path_buf(),
                from_pending,
                to_pending,
                bytes,
            });
            state.touched.insert(from.to_path_buf());
            state.touched.insert(to.to_path_buf());
            // The destination's entry is new until its directory is synced.
            state.pending_entries.insert(to.to_path_buf());
            if let Some(group) = state.link_group.remove(from) {
                state.link_group.insert(to.to_path_buf(), group);
            }
            return Ok(());
        }
        // The source's state moves with its file, under the new name.
        let from_durable = state.durable.remove(from);
        let from_link = state.links.remove(from);
        let from_touched = state.touched.remove(from);
        let from_group = state.link_group.remove(from);
        state.pending_entries.remove(from);
        // The destination is replaced on disk, as a removal of that name: its
        // durable image and write-tracking go (so a replaced, previously
        // synced destination is not resurrected to its stale content on
        // crash), and what its file went through stays with the file's other
        // hard links.
        state.forget_name(to);
        // An ordered sync of the source's content follows its file; one of
        // its entry does not, since the new entry is pending until its own
        // directory is synced.
        for sync in &mut state.ordered {
            match sync {
                Ordered::Image(name, _) if name.as_path() == from => *name = to.to_path_buf(),
                Ordered::Image(..) | Ordered::Removed(..) | Ordered::Renamed { .. } => {}
                Ordered::Entries(entries) => entries.retain(|(entry, _)| entry != from),
            }
        }
        // The rename is as durable as the source was.
        if let Some(bytes) = from_durable {
            state.durable.insert(to.to_path_buf(), bytes);
        }
        if let Some(target) = from_link {
            state.links.insert(to.to_path_buf(), target);
        }
        if from_touched {
            state.touched.insert(to.to_path_buf());
        }
        // The destination's entry is new until its directory is synced.
        state.pending_entries.insert(to.to_path_buf());
        if let Some(group) = from_group {
            state.link_group.insert(to.to_path_buf(), group);
        }
        Ok(())
    }

    fn metadata(&self, path: &Path) -> io::Result<FsMetadata> {
        self.inner.metadata(path)
    }

    fn sync_directory(&self, path: &Path) -> io::Result<()> {
        self.sync_entries_of(path, SyncMode::Full, || self.inner.sync_directory(path))
    }

    fn sync_directory_with(&self, path: &Path, mode: SyncMode) -> io::Result<()> {
        self.sync_entries_of(path, mode, || self.inner.sync_directory_with(path, mode))
    }

    fn sync_device(&self, path: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        self.inner.sync_device(path)?;
        self.state.lock().settle_all();
        Ok(())
    }

    fn exists(&self, path: &Path) -> io::Result<bool> {
        self.inner.exists(path)
    }

    fn hard_link(&self, src: &Path, dst: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        self.inner.hard_link(src, dst)?;
        // A symlink linked as itself (Linux `linkat(2)` without
        // `AT_SYMLINK_FOLLOW`) is a new entry with no bytes of its own: no
        // image is kept under it, and it is grouped with nothing. The link is
        // made, so a probe that cannot answer does not fail it either.
        let link = self.name_after(dst);
        if matches!(self.inner.read_link(dst), Ok(Some(_))) {
            self.new_entry(&link);
            return Ok(());
        }
        // A backend that followed a symlink source made `dst` a name of the
        // file the link chain ends at, so that file is the one copied and
        // the name `dst` is grouped with.
        let file = self.entry_of(src).unwrap_or_else(|_| self.name_after(src));
        self.track_copy(&file, &link)?;
        // On a backend where a hard link is a second name of one file, a sync
        // through either name makes the other's bytes durable too; on one
        // where it is a copy (`MemFs`), each name keeps its own image. A
        // probe that cannot answer leaves the names separate.
        if matches!(self.inner.same_file(&file, &link), Ok(true)) {
            let mut state = self.state.lock();
            state.link(&file, &link);
            // The image `dst` took is the one file's, `src`'s baseline
            // included: every name holds it, so a later first touch through
            // any of them, which finds the group already touched, still has
            // durable bytes to roll back to.
            if let Some(bytes) = state.durable.get(&link).cloned() {
                state.set_durable(&link, bytes);
            }
        }
        Ok(())
    }

    fn backend_id(&self) -> Option<u64> {
        self.inner.backend_id()
    }

    fn volume_id(&self, path: &Path) -> Option<u64> {
        self.inner.volume_id(path)
    }

    fn capabilities(&self, path: &Path) -> FsCapabilities {
        self.inner.capabilities(path)
    }

    fn try_disable_cow(&self, path: &Path) -> io::Result<()> {
        self.inner.try_disable_cow(path)
    }

    fn punch_hole(&self, path: &Path, offset: u64, len: u64) -> io::Result<()> {
        // Content-mutating: capture the pre-mutation durable image so an
        // un-synced punch rolls back on crash. The namespace is held so the
        // backend reaches the file that was resolved.
        let _namespace = self.namespace.lock();
        self.capture_first_touch(&self.entry_of(path)?)?;
        self.inner.punch_hole(path, offset, len)
    }

    fn reflink_file(&self, src: &Path, dst: &Path) -> io::Result<()> {
        let _namespace = self.namespace.lock();
        self.inner.reflink_file(src, dst)?;
        let file = self.entry_of(src).unwrap_or_else(|_| self.name_after(src));
        self.track_copy(&file, &self.name_after(dst))?;
        Ok(())
    }

    fn truncate_file(&self, path: &Path) -> io::Result<()> {
        // Content-mutating: capture the pre-truncate image so an un-synced
        // reclaim rolls back on crash. The namespace is held so the backend
        // reaches the file that was resolved.
        let _namespace = self.namespace.lock();
        self.capture_first_touch(&self.entry_of(path)?)?;
        self.inner.truncate_file(path)
    }

    fn hard_link_count(&self, path: &Path) -> io::Result<u64> {
        self.inner.hard_link_count(path)
    }

    fn available_space(&self, path: &Path) -> io::Result<u64> {
        self.inner.available_space(path)
    }

    fn allocated_size(&self, path: &Path) -> io::Result<Option<u64>> {
        self.inner.allocated_size(path)
    }

    fn extent_is_hole(&self, path: &Path, offset: u64, len: u64) -> io::Result<Option<bool>> {
        self.inner.extent_is_hole(path, offset, len)
    }

    fn extent_contains_hole(&self, path: &Path, offset: u64, len: u64) -> io::Result<Option<bool>> {
        self.inner.extent_contains_hole(path, offset, len)
    }
}

/// A file handle that records its durable image on `fsync`.
struct CrashFile {
    inner: Box<dyn FsFile>,
    path: PathBuf,
    /// Backend handle, used to reopen `path` read-only when snapshotting its
    /// content on `fsync` (the write handle may lack read access).
    fs: Arc<dyn Fs>,
    state: Arc<spin::Mutex<CrashState>>,
}

impl CrashFile {
    /// Captures this path's full current content as its durable image. Called
    /// after a successful `fsync`. Reopens the path read-only because the write
    /// handle being synced is not necessarily readable.
    fn snapshot(&self, mode: SyncMode) -> io::Result<()> {
        let mut rf = self.fs.open(&self.path, &FsOpenOptions::new().read(true))?;
        let mut buf = Vec::new();
        std::io::Read::read_to_end(&mut rf, &mut buf)?;
        let mut state = self.state.lock();
        // A barrier makes the content durable only once the device is
        // flushed; any other sync flushes what was ordered before it too.
        if mode == SyncMode::Barrier {
            state.ordered.push(Ordered::Image(self.path.clone(), buf));
        } else {
            state.settle_all();
            state.set_durable(&self.path, buf);
        }
        Ok(())
    }
}

impl std::io::Read for CrashFile {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.inner.read(buf)
    }
}

impl std::io::Write for CrashFile {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.inner.write(buf)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        // flush pushes to the backend but is NOT a durability barrier; the
        // durable image is captured on sync, not flush.
        self.inner.flush()
    }
}

impl std::io::Seek for CrashFile {
    fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
        self.inner.seek(pos)
    }
}

impl FsFile for CrashFile {
    fn sync_all(&self) -> io::Result<()> {
        self.inner.sync_all()?;
        self.snapshot(SyncMode::Full)
    }

    fn sync_data(&self) -> io::Result<()> {
        self.inner.sync_data()?;
        self.snapshot(SyncMode::Full)
    }

    fn sync_all_with(&self, mode: SyncMode) -> io::Result<()> {
        self.inner.sync_all_with(mode)?;
        self.snapshot(mode)
    }

    fn sync_data_with(&self, mode: SyncMode) -> io::Result<()> {
        self.inner.sync_data_with(mode)?;
        self.snapshot(mode)
    }

    fn start_writeback(&self, offset: u64, len: u64) -> io::Result<()> {
        // Writeback makes nothing durable; only a sync does.
        self.inner.start_writeback(offset, len)
    }

    fn metadata(&self) -> io::Result<FsMetadata> {
        self.inner.metadata()
    }

    fn hard_link_count(&self) -> io::Result<u64> {
        self.inner.hard_link_count()
    }

    fn set_len(&self, size: u64) -> io::Result<()> {
        self.inner.set_len(size)
    }

    fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
        self.inner.read_at(buf, offset)
    }

    fn lock_exclusive(&self) -> io::Result<()> {
        self.inner.lock_exclusive()
    }

    fn try_lock_exclusive(&self) -> io::Result<bool> {
        self.inner.try_lock_exclusive()
    }

    fn hint(&self, hint: FileHint) -> io::Result<()> {
        self.inner.hint(hint)
    }

    fn set_write_lifetime(&self, lifetime: super::WriteLifetime) -> io::Result<()> {
        self.inner.set_write_lifetime(lifetime)
    }
}

#[cfg(test)]
#[expect(clippy::unwrap_used, clippy::expect_used, reason = "test code")]
mod tests;