lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
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
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0

//! Transaction log (Write-Ahead Log / WAL) implementation.
//!
//! This module provides durable logging for transactions to ensure
//! crash recovery and atomicity.

use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use super::types::{
    RecoveryStats, TXN_LOG_MAGIC, TXN_LOG_VERSION, TxnError, TxnId, TxnLogEntry, TxnLogType,
    TxnOperation, TxnResultType, TxnState,
};

// ═══════════════════════════════════════════════════════════════════════════════
// LOG HEADER
// ═══════════════════════════════════════════════════════════════════════════════

/// Transaction log header.
#[derive(Debug, Clone)]
pub struct TxnLogHeader {
    /// Magic number.
    pub magic: u64,
    /// Version.
    pub version: u32,
    /// Log ID.
    pub log_id: u64,
    /// Creation timestamp.
    pub created_at: u64,
    /// Last LSN written.
    pub last_lsn: u64,
    /// Last checkpoint LSN.
    pub checkpoint_lsn: u64,
    /// Header checksum.
    pub checksum: u64,
}

impl TxnLogHeader {
    /// Create a new log header.
    pub fn new(log_id: u64, timestamp: u64) -> Self {
        let mut header = Self {
            magic: TXN_LOG_MAGIC,
            version: TXN_LOG_VERSION,
            log_id,
            created_at: timestamp,
            last_lsn: 0,
            checkpoint_lsn: 0,
            checksum: 0,
        };
        header.update_checksum();
        header
    }

    /// Update the checksum.
    pub fn update_checksum(&mut self) {
        self.checksum = self.magic
            ^ self.version as u64
            ^ self.log_id
            ^ self.created_at
            ^ self.last_lsn
            ^ self.checkpoint_lsn;
    }

    /// Verify the checksum.
    pub fn verify(&self) -> bool {
        let expected = self.magic
            ^ self.version as u64
            ^ self.log_id
            ^ self.created_at
            ^ self.last_lsn
            ^ self.checkpoint_lsn;
        expected == self.checksum
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TRANSACTION LOG
// ═══════════════════════════════════════════════════════════════════════════════

/// Transaction log for a dataset.
#[derive(Debug)]
pub struct TxnLog {
    /// Dataset name.
    dataset: String,
    /// Log header.
    header: TxnLogHeader,
    /// Log entries (in-memory buffer).
    entries: Vec<TxnLogEntry>,
    /// Next LSN to assign.
    next_lsn: u64,
    /// Active transactions.
    active_txns: BTreeMap<TxnId, TxnState>,
    /// Pending operations by transaction.
    pending_ops: BTreeMap<TxnId, Vec<TxnOperation>>,
    /// Maximum entries to buffer before flush.
    max_buffer_entries: usize,
    /// Sync after each write.
    sync_on_write: bool,
}

impl TxnLog {
    /// Create a new transaction log.
    pub fn new(dataset: &str, log_id: u64, timestamp: u64) -> Self {
        Self {
            dataset: dataset.to_string(),
            header: TxnLogHeader::new(log_id, timestamp),
            entries: Vec::new(),
            next_lsn: 1,
            active_txns: BTreeMap::new(),
            pending_ops: BTreeMap::new(),
            max_buffer_entries: 1000,
            sync_on_write: true,
        }
    }

    /// Get the dataset name.
    pub fn dataset(&self) -> &str {
        &self.dataset
    }

    /// Get the log ID.
    pub fn log_id(&self) -> u64 {
        self.header.log_id
    }

    /// Get the current LSN.
    pub fn current_lsn(&self) -> u64 {
        self.next_lsn.saturating_sub(1)
    }

    /// Get the next LSN.
    pub fn next_lsn(&self) -> u64 {
        self.next_lsn
    }

    /// Get the checkpoint LSN.
    pub fn checkpoint_lsn(&self) -> u64 {
        self.header.checkpoint_lsn
    }

    /// Get number of buffered entries.
    pub fn buffered_entries(&self) -> usize {
        self.entries.len()
    }

    /// Get number of active transactions.
    pub fn active_txn_count(&self) -> usize {
        self.active_txns.len()
    }

    /// Check if a transaction exists.
    pub fn has_txn(&self, txn_id: TxnId) -> bool {
        self.active_txns.contains_key(&txn_id)
    }

    /// Get transaction state.
    pub fn txn_state(&self, txn_id: TxnId) -> Option<TxnState> {
        self.active_txns.get(&txn_id).copied()
    }

    /// Enable/disable sync on write.
    pub fn set_sync_on_write(&mut self, sync: bool) {
        self.sync_on_write = sync;
    }

    /// Set maximum buffer entries.
    pub fn set_max_buffer_entries(&mut self, max: usize) {
        self.max_buffer_entries = max;
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // TRANSACTION LIFECYCLE
    // ═══════════════════════════════════════════════════════════════════════════

    /// Begin a new transaction.
    pub fn begin(&mut self, txn_id: TxnId, timestamp: u64) -> TxnResultType<()> {
        if self.active_txns.contains_key(&txn_id) {
            return Err(TxnError::AlreadyExists(txn_id));
        }

        let lsn = self.allocate_lsn();
        let mut entry = TxnLogEntry::begin(txn_id, lsn, timestamp);
        entry.calculate_checksum();

        self.entries.push(entry);
        self.active_txns.insert(txn_id, TxnState::Active);
        self.pending_ops.insert(txn_id, Vec::new());

        Ok(())
    }

    /// Log an operation.
    pub fn log_operation(
        &mut self,
        txn_id: TxnId,
        op: TxnOperation,
        timestamp: u64,
    ) -> TxnResultType<u32> {
        // Check state first
        let state = self
            .active_txns
            .get(&txn_id)
            .copied()
            .ok_or(TxnError::NotFound(txn_id))?;
        if state != TxnState::Active {
            return Err(TxnError::InvalidState {
                txn_id,
                current: state,
                expected: &[TxnState::Active],
            });
        }

        // Get op_index
        let op_index = self
            .pending_ops
            .get(&txn_id)
            .map(|ops| ops.len() as u32)
            .unwrap_or(0);

        // Allocate LSN and create entry
        let lsn = self.allocate_lsn();
        let mut entry = TxnLogEntry::operation(txn_id, op_index, lsn, timestamp, op.clone());
        entry.calculate_checksum();

        self.entries.push(entry);
        if let Some(ops) = self.pending_ops.get_mut(&txn_id) {
            ops.push(op);
        }

        Ok(op_index)
    }

    /// Mark transaction as prepared.
    pub fn prepare(&mut self, txn_id: TxnId, timestamp: u64) -> TxnResultType<()> {
        // Check state first
        let state = self
            .active_txns
            .get(&txn_id)
            .copied()
            .ok_or(TxnError::NotFound(txn_id))?;
        if state != TxnState::Active {
            return Err(TxnError::InvalidState {
                txn_id,
                current: state,
                expected: &[TxnState::Active],
            });
        }

        // Allocate LSN and create entry
        let lsn = self.allocate_lsn();
        let mut entry = TxnLogEntry::prepare(txn_id, lsn, timestamp);
        entry.calculate_checksum();

        self.entries.push(entry);
        self.active_txns.insert(txn_id, TxnState::Prepared);

        Ok(())
    }

    /// Commit a transaction.
    pub fn commit(&mut self, txn_id: TxnId, timestamp: u64) -> TxnResultType<Vec<TxnOperation>> {
        // Check state first
        let state = self
            .active_txns
            .get(&txn_id)
            .copied()
            .ok_or(TxnError::NotFound(txn_id))?;
        if !matches!(state, TxnState::Active | TxnState::Prepared) {
            return Err(TxnError::InvalidState {
                txn_id,
                current: state,
                expected: &[TxnState::Active, TxnState::Prepared],
            });
        }

        // Allocate LSN and create entry
        let lsn = self.allocate_lsn();
        let mut entry = TxnLogEntry::commit(txn_id, lsn, timestamp);
        entry.calculate_checksum();

        self.entries.push(entry);
        self.active_txns.insert(txn_id, TxnState::Committed);

        // Return the operations to execute
        let ops = self.pending_ops.remove(&txn_id).unwrap_or_default();
        self.active_txns.remove(&txn_id);

        Ok(ops)
    }

    /// Abort a transaction.
    pub fn abort(&mut self, txn_id: TxnId, timestamp: u64) -> TxnResultType<Vec<TxnOperation>> {
        // Check state first
        let state = self
            .active_txns
            .get(&txn_id)
            .copied()
            .ok_or(TxnError::NotFound(txn_id))?;
        if state == TxnState::Committed {
            return Err(TxnError::InvalidState {
                txn_id,
                current: state,
                expected: &[TxnState::Active, TxnState::Prepared],
            });
        }

        // Allocate LSN and create entry
        let lsn = self.allocate_lsn();
        let mut entry = TxnLogEntry::abort(txn_id, lsn, timestamp);
        entry.calculate_checksum();

        self.entries.push(entry);
        self.active_txns.insert(txn_id, TxnState::Aborted);

        // Return the operations to rollback
        let ops = self.pending_ops.remove(&txn_id).unwrap_or_default();
        self.active_txns.remove(&txn_id);

        Ok(ops)
    }

    /// Log a rollback operation.
    pub fn log_rollback(
        &mut self,
        txn_id: TxnId,
        op_index: u32,
        op: TxnOperation,
        timestamp: u64,
    ) -> TxnResultType<()> {
        let lsn = self.allocate_lsn();
        let mut entry = TxnLogEntry::rollback(txn_id, op_index, lsn, timestamp, op);
        entry.calculate_checksum();

        self.entries.push(entry);
        Ok(())
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // CHECKPOINTING
    // ═══════════════════════════════════════════════════════════════════════════

    /// Create a checkpoint.
    pub fn checkpoint(&mut self, timestamp: u64) -> TxnResultType<u64> {
        // Don't checkpoint if there are active transactions
        // (In a real implementation, we'd handle this differently)
        if !self.active_txns.is_empty() {
            // Just update the checkpoint LSN to before active txns
            // For now, skip checkpointing
            return Ok(self.header.checkpoint_lsn);
        }

        let lsn = self.current_lsn();
        self.header.checkpoint_lsn = lsn;
        self.header.last_lsn = lsn;
        self.header.update_checksum();

        // Clear committed entries before checkpoint
        // In a real implementation, this would write to disk
        self.entries.retain(|e| e.lsn > lsn);

        Ok(lsn)
    }

    /// Flush buffered entries.
    pub fn flush(&mut self) -> TxnResultType<usize> {
        // In a real implementation, this would write to disk
        let count = self.entries.len();
        self.header.last_lsn = self.current_lsn();
        self.header.update_checksum();
        Ok(count)
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // RECOVERY
    // ═══════════════════════════════════════════════════════════════════════════

    /// Get transactions needing recovery.
    pub fn get_recovery_txns(&self) -> Vec<(TxnId, TxnState, Vec<TxnOperation>)> {
        let mut result = Vec::new();

        for (&txn_id, &state) in &self.active_txns {
            if state.is_recoverable() {
                let ops = self.pending_ops.get(&txn_id).cloned().unwrap_or_default();
                result.push((txn_id, state, ops));
            }
        }

        result
    }

    /// Replay entries from a given LSN.
    pub fn replay_from(&self, from_lsn: u64) -> impl Iterator<Item = &TxnLogEntry> {
        self.entries.iter().filter(move |e| e.lsn >= from_lsn)
    }

    /// Load entries from a byte buffer (for testing/recovery).
    pub fn load_entries(&mut self, entries: Vec<TxnLogEntry>) -> TxnResultType<RecoveryStats> {
        let mut stats = RecoveryStats::new();

        for entry in entries {
            // Verify checksum
            if !entry.verify_checksum() {
                stats.errors += 1;
                continue;
            }

            stats.log_entries_processed += 1;

            match entry.entry_type {
                TxnLogType::Begin => {
                    self.active_txns.insert(entry.txn_id, TxnState::Active);
                    self.pending_ops.insert(entry.txn_id, Vec::new());
                }
                TxnLogType::Operation => {
                    if let Some(op) = entry.operation {
                        if let Some(ops) = self.pending_ops.get_mut(&entry.txn_id) {
                            ops.push(op);
                        }
                    }
                }
                TxnLogType::Prepare => {
                    if let Some(state) = self.active_txns.get_mut(&entry.txn_id) {
                        *state = TxnState::Prepared;
                    }
                }
                TxnLogType::Commit => {
                    self.active_txns.remove(&entry.txn_id);
                    self.pending_ops.remove(&entry.txn_id);
                    stats.txns_recovered += 1;
                }
                TxnLogType::Abort => {
                    self.active_txns.remove(&entry.txn_id);
                    self.pending_ops.remove(&entry.txn_id);
                    stats.txns_rolled_back += 1;
                }
                TxnLogType::Rollback => {
                    stats.ops_undone += 1;
                }
                TxnLogType::Checkpoint => {
                    // Update checkpoint LSN
                    self.header.checkpoint_lsn = entry.lsn;
                }
            }

            // Update next LSN
            if entry.lsn >= self.next_lsn {
                self.next_lsn = entry.lsn + 1;
            }
        }

        Ok(stats)
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // INTERNAL HELPERS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Allocate the next LSN.
    fn allocate_lsn(&mut self) -> u64 {
        let lsn = self.next_lsn;
        self.next_lsn += 1;
        lsn
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::vec;

    fn test_log() -> TxnLog {
        TxnLog::new("test/pool", 1, 1000)
    }

    #[test]
    fn test_log_header() {
        let header = TxnLogHeader::new(1, 1000);
        assert_eq!(header.magic, TXN_LOG_MAGIC);
        assert_eq!(header.version, TXN_LOG_VERSION);
        assert!(header.verify());
    }

    #[test]
    fn test_begin_transaction() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();
        assert!(log.has_txn(txn_id));
        assert_eq!(log.txn_state(txn_id), Some(TxnState::Active));
    }

    #[test]
    fn test_begin_duplicate() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();
        assert!(log.begin(txn_id, 1001).is_err());
    }

    #[test]
    fn test_log_operation() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();

        let op = TxnOperation::Create {
            path: "/test.txt".into(),
            content: vec![1, 2, 3],
            mode: 0o644,
        };

        let op_index = log.log_operation(txn_id, op, 1001).unwrap();
        assert_eq!(op_index, 0);
    }

    #[test]
    fn test_prepare() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();
        log.prepare(txn_id, 1001).unwrap();
        assert_eq!(log.txn_state(txn_id), Some(TxnState::Prepared));
    }

    #[test]
    fn test_commit() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();

        let op = TxnOperation::Create {
            path: "/test.txt".into(),
            content: vec![1, 2, 3],
            mode: 0o644,
        };
        log.log_operation(txn_id, op, 1001).unwrap();

        let ops = log.commit(txn_id, 1002).unwrap();
        assert_eq!(ops.len(), 1);
        assert!(!log.has_txn(txn_id));
    }

    #[test]
    fn test_abort() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();

        let op = TxnOperation::Create {
            path: "/test.txt".into(),
            content: vec![1, 2, 3],
            mode: 0o644,
        };
        log.log_operation(txn_id, op, 1001).unwrap();

        let ops = log.abort(txn_id, 1002).unwrap();
        assert_eq!(ops.len(), 1);
        assert!(!log.has_txn(txn_id));
    }

    #[test]
    fn test_lsn_allocation() {
        let mut log = test_log();

        assert_eq!(log.next_lsn(), 1);

        log.begin(TxnId::new(1), 1000).unwrap();
        assert_eq!(log.next_lsn(), 2);

        log.log_operation(
            TxnId::new(1),
            TxnOperation::Mkdir {
                path: "/dir".into(),
                mode: 0o755,
            },
            1001,
        )
        .unwrap();
        assert_eq!(log.next_lsn(), 3);
    }

    #[test]
    fn test_checkpoint() {
        let mut log = test_log();
        let txn_id = TxnId::new(1);

        log.begin(txn_id, 1000).unwrap();
        log.commit(txn_id, 1001).unwrap();

        let cp_lsn = log.checkpoint(1002).unwrap();
        assert!(cp_lsn > 0);
        assert_eq!(log.checkpoint_lsn(), cp_lsn);
    }

    #[test]
    fn test_flush() {
        let mut log = test_log();

        log.begin(TxnId::new(1), 1000).unwrap();
        log.log_operation(
            TxnId::new(1),
            TxnOperation::Mkdir {
                path: "/dir".into(),
                mode: 0o755,
            },
            1001,
        )
        .unwrap();

        let count = log.flush().unwrap();
        assert_eq!(count, 2);
    }

    #[test]
    fn test_recovery_txns() {
        let mut log = test_log();

        // Active transaction
        log.begin(TxnId::new(1), 1000).unwrap();
        log.log_operation(
            TxnId::new(1),
            TxnOperation::Mkdir {
                path: "/dir".into(),
                mode: 0o755,
            },
            1001,
        )
        .unwrap();

        // Prepared transaction
        log.begin(TxnId::new(2), 1002).unwrap();
        log.prepare(TxnId::new(2), 1003).unwrap();

        let recovery = log.get_recovery_txns();
        assert_eq!(recovery.len(), 2);
    }

    #[test]
    fn test_load_entries() {
        let mut log = test_log();

        let entries = vec![
            {
                let mut e = TxnLogEntry::begin(TxnId::new(1), 1, 1000);
                e.calculate_checksum();
                e
            },
            {
                let mut e = TxnLogEntry::operation(
                    TxnId::new(1),
                    0,
                    2,
                    1001,
                    TxnOperation::Mkdir {
                        path: "/dir".into(),
                        mode: 0o755,
                    },
                );
                e.calculate_checksum();
                e
            },
            {
                let mut e = TxnLogEntry::commit(TxnId::new(1), 3, 1002);
                e.calculate_checksum();
                e
            },
        ];

        let stats = log.load_entries(entries).unwrap();
        assert_eq!(stats.log_entries_processed, 3);
        assert_eq!(stats.txns_recovered, 1);
    }

    #[test]
    fn test_replay_from() {
        let mut log = test_log();

        log.begin(TxnId::new(1), 1000).unwrap();
        log.log_operation(
            TxnId::new(1),
            TxnOperation::Mkdir {
                path: "/dir".into(),
                mode: 0o755,
            },
            1001,
        )
        .unwrap();
        log.commit(TxnId::new(1), 1002).unwrap();

        let entries: Vec<_> = log.replay_from(2).collect();
        assert_eq!(entries.len(), 2); // Operation and Commit
    }
}