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
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0
//
// Replication Stream Protocol (Send/Receive)
// ZFS-style incremental replication for backup and migration.
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use crate::fscore::structs::{Blkptr, DnodePhys};
use crate::integrity::checksum::Checksum;
use crate::io::pipeline::Pipeline;
/// Send stream record types for replication protocol
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SendRecordType {
/// Stream begin marker
Begin = 1, // Stream begin marker
/// Stream end marker
End = 2, // Stream end marker
/// New object/file creation
Object = 3, // New object/file
/// Free object range operation
Freeobjects = 4, // Free object range
/// Data block write
Write = 5, // Data block
/// Free block range operation
Free = 6, // Free block range
/// Free specific block pointer
FreeBlockRef = 7, // Free specific block pointer
/// Dataset properties update
Properties = 8, // Dataset properties
/// Snapshot creation marker
Snapshot = 9, // Snapshot marker
/// Destroy object operation
Destroy = 10, // Destroy object
}
/// Send stream header (64 bytes)
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct SendStreamHeader {
/// Magic number (0x53454E44 = "SEND")
pub magic: u64, // 0x53454E44 ("SEND")
/// Protocol version number
pub version: u32, // Protocol version
/// Stream flags
pub flags: u32, // Stream flags
/// Source dataset GUID
pub from_guid: u64, // Source dataset GUID
/// Target dataset GUID (0 for full send)
pub to_guid: u64, // Target dataset GUID (0 for full send)
/// Starting transaction group number
pub from_txg: u64, // Starting TXG
/// Ending transaction group number
pub to_txg: u64, // Ending TXG
/// Stream creation timestamp
pub creation_time: u64, // Stream creation timestamp
/// Fletcher4 checksum of entire stream
pub checksum: u64, // Fletcher4 of entire stream
/// Reserved for future use
pub reserved: [u64; 3],
}
/// Send stream magic number: "SEND"
const SEND_MAGIC: u64 = 0x53454E44; // "SEND"
impl SendStreamHeader {
/// Create a new send stream header
pub fn new(from_guid: u64, to_guid: u64, from_txg: u64, to_txg: u64) -> Self {
Self {
magic: SEND_MAGIC,
version: 1,
flags: 0,
from_guid,
to_guid,
from_txg,
to_txg,
creation_time: 0, // Would use RDTSC
checksum: 0, // Calculated at stream end
reserved: [0; 3],
}
}
}
/// Send stream record (variable length)
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct SendRecord {
/// Record type identifier
pub record_type: u8,
/// Record flags
pub flags: u8,
/// Payload data length
pub length: u32, // Payload length
/// Target object ID
pub object_id: u64, // Target object
/// Offset for write operations
pub offset: u64, // Offset (for writes)
/// Fletcher4 checksum of payload
pub checksum: u64, // Fletcher4 of payload
}
impl SendRecord {
/// Create a new send record
pub fn new(record_type: SendRecordType, object_id: u64, offset: u64, length: u32) -> Self {
Self {
record_type: record_type as u8,
flags: 0,
length,
object_id,
offset,
checksum: 0,
}
}
}
/// Send stream builder for creating replication streams
pub struct SendStream {
/// Stream header with metadata
pub header: SendStreamHeader,
/// Serialized stream buffer
pub buffer: Vec<u8>,
/// Number of records in stream
pub record_count: usize,
}
impl SendStream {
/// Begin a new send stream
pub fn begin_full_send(dataset_guid: u64, to_txg: u64) -> Self {
crate::lcpfs_println!(
"[ SEND ] Beginning full send for GUID {:x} @ TXG {}",
dataset_guid,
to_txg
);
let header = SendStreamHeader::new(dataset_guid, 0, 0, to_txg);
let mut stream = Self {
header,
buffer: Vec::new(),
record_count: 0,
};
// Write header
stream.write_header();
stream
}
/// Begin incremental send (from snapshot to snapshot)
pub fn begin_incremental_send(
from_guid: u64,
to_guid: u64,
from_txg: u64,
to_txg: u64,
) -> Self {
crate::lcpfs_println!(
"[ SEND ] Beginning incremental send {:x} -> {:x} (TXG {}-{})",
from_guid,
to_guid,
from_txg,
to_txg
);
let header = SendStreamHeader::new(from_guid, to_guid, from_txg, to_txg);
let mut stream = Self {
header,
buffer: Vec::new(),
record_count: 0,
};
stream.write_header();
stream
}
/// Write stream header to buffer
fn write_header(&mut self) {
// SAFETY INVARIANTS:
// 1. SendStreamHeader is #[repr(C, packed)] with stable layout
// 2. self.header is valid, initialized SendStreamHeader
// 3. All fields are primitive types (u64, u32, arrays - no Drop)
// 4. Slice lifetime ≤ function scope (does not escape)
// 5. size_of::<SendStreamHeader>() matches packed struct size
//
// VERIFICATION: TODO - Prove packed struct has no padding
//
// JUSTIFICATION:
// ZFS send/receive protocol header (magic, version, flags).
// Binary format for cross-system snapshot transfer.
unsafe {
let header_bytes = core::slice::from_raw_parts(
&self.header as *const SendStreamHeader as *const u8,
core::mem::size_of::<SendStreamHeader>(),
);
self.buffer.extend_from_slice(header_bytes);
}
}
/// Add a data block write record
pub fn write_block(&mut self, object_id: u64, offset: u64, data: &[u8]) {
let record = SendRecord::new(SendRecordType::Write, object_id, offset, data.len() as u32);
self.write_record(&record, data);
}
/// Add object creation record
pub fn write_object(&mut self, object_id: u64, object_type: u8, dnode: &DnodePhys) {
let record = SendRecord::new(
SendRecordType::Object,
object_id,
0,
core::mem::size_of::<DnodePhys>() as u32,
);
// Serialize dnode as payload
// SAFETY INVARIANTS:
// 1. DnodePhys is #[repr(C)] with stable layout
// 2. dnode reference is valid and properly initialized
// 3. All fields are primitive types or arrays (no Drop)
// 4. Slice lifetime ≤ function scope (does not escape)
// 5. size_of::<DnodePhys>() matches struct size
//
// VERIFICATION: TODO - Same as lcpfs_dmu.rs (DnodePhys stability)
//
// JUSTIFICATION:
// Send stream includes object metadata as serialized DnodePhys.
// Required for recreating objects during receive operation.
let payload = unsafe {
core::slice::from_raw_parts(
dnode as *const DnodePhys as *const u8,
core::mem::size_of::<DnodePhys>(),
)
};
self.write_record(&record, payload);
}
/// Add free block record
pub fn write_free(&mut self, object_id: u64, offset: u64, length: u64) {
let record = SendRecord::new(SendRecordType::Free, object_id, offset, length as u32);
self.write_record(&record, &[]);
}
/// Add snapshot marker
pub fn write_snapshot(&mut self, snap_name: &str, guid: u64) {
let record = SendRecord::new(SendRecordType::Snapshot, guid, 0, snap_name.len() as u32);
self.write_record(&record, snap_name.as_bytes());
}
/// Write a record to the stream
fn write_record(&mut self, record: &SendRecord, payload: &[u8]) {
// Calculate checksum
let mut record_with_checksum = *record;
record_with_checksum.checksum = Checksum::calculate(payload).first();
// Serialize record header
// SAFETY INVARIANTS:
// 1. SendRecord is #[repr(C, packed)] with stable layout
// 2. record_with_checksum is valid, initialized SendRecord on stack
// 3. All fields are primitive types (u8, u32, u64 - no Drop)
// 4. Slice lifetime ≤ function scope
// 5. Packed struct has no padding bytes
//
// VERIFICATION: TODO - Prove SendRecord packed layout deterministic
//
// JUSTIFICATION:
// Each send stream record (write/free/snapshot) has binary header.
// Serialization required for ZFS send/receive wire protocol.
unsafe {
let record_bytes = core::slice::from_raw_parts(
&record_with_checksum as *const SendRecord as *const u8,
core::mem::size_of::<SendRecord>(),
);
self.buffer.extend_from_slice(record_bytes);
}
// Append payload
self.buffer.extend_from_slice(payload);
self.record_count += 1;
}
/// Finalize stream and return serialized data
pub fn finalize(mut self) -> Vec<u8> {
// Add stream end marker
let end_record = SendRecord::new(SendRecordType::End, 0, 0, 0);
self.write_record(&end_record, &[]);
// Calculate final checksum of entire stream (except header checksum field)
let stream_checksum = Checksum::calculate(&self.buffer).first();
// Update header checksum
let header_offset = offset_of_checksum_in_header();
// SAFETY INVARIANTS:
// 1. header_offset points to checksum field in SendStreamHeader
// 2. buffer.len() ≥ header_offset + 8 (header written by write_header)
// 3. Writing u64 at header_offset (field is properly aligned)
// 4. Mutable ownership of buffer (no aliasing)
// 5. write_unaligned handles potential misalignment in Vec
//
// VERIFICATION: TODO - Prove offset_of_checksum_in_header() correctness
//
// JUSTIFICATION:
// Stream checksum stored in header for integrity verification.
// In-place update of checksum field after stream construction.
unsafe {
let checksum_ptr = self.buffer.as_mut_ptr().add(header_offset) as *mut u64;
*checksum_ptr = stream_checksum;
}
crate::lcpfs_println!(
"[ SEND ] Finalized stream: {} records, {} bytes",
self.record_count,
self.buffer.len()
);
self.buffer
}
/// Get stream size
pub fn size(&self) -> usize {
self.buffer.len()
}
}
/// Calculate offset of checksum field in header (safe method)
fn offset_of_checksum_in_header() -> usize {
// Use size_of instead of manual calculation to be future-proof
// Offset = size of all fields before checksum
use core::mem::size_of;
size_of::<u64>() + // magic
size_of::<u32>() + // version
size_of::<u32>() + // flags
size_of::<u64>() + // from_guid
size_of::<u64>() + // to_guid
size_of::<u64>() + // from_txg
size_of::<u64>() + // to_txg
size_of::<u64>() // creation_time
// Next field is checksum
}
/// Receive stream parser for applying replication streams
pub struct ReceiveStream {
/// Stream data buffer
pub data: Vec<u8>,
/// Current read offset in stream
pub offset: usize,
/// Parsed stream header
pub header: Option<SendStreamHeader>,
/// Number of records processed
pub processed_records: usize,
/// Whether stream completed with End marker
stream_complete: bool,
}
impl ReceiveStream {
/// Begin receiving a stream
pub fn begin_receive(data: Vec<u8>) -> Result<Self, &'static str> {
let mut stream = Self {
data,
offset: 0,
header: None,
processed_records: 0,
stream_complete: false,
};
stream.read_header()?;
Ok(stream)
}
/// Read and validate stream header
fn read_header(&mut self) -> Result<(), &'static str> {
if self.data.len() < core::mem::size_of::<SendStreamHeader>() {
return Err("Stream too short");
}
// SAFETY INVARIANTS:
// 1. data.len() ≥ size_of::<SendStreamHeader>() - checked above
// 2. SendStreamHeader is #[repr(C, packed)] with stable layout
// 3. read_unaligned handles packed struct (no alignment requirement)
// 4. Data written by SendStream::write_header with same layout
// 5. Owned SendStreamHeader returned (no lifetime issues)
//
// VERIFICATION: TODO - Prove send/receive format compatibility
//
// JUSTIFICATION:
// Stream begins with SendStreamHeader for protocol negotiation.
// Deserialization required to validate magic, version, GUIDs.
let header: SendStreamHeader =
unsafe { core::ptr::read_unaligned(self.data.as_ptr() as *const SendStreamHeader) };
if header.magic != SEND_MAGIC {
return Err("Invalid stream magic");
}
let from_guid = header.from_guid;
let from_txg = header.from_txg;
let to_txg = header.to_txg;
crate::lcpfs_println!(
"[ RECV ] Receiving stream: GUID {:x}, TXG {} -> {}",
from_guid,
from_txg,
to_txg
);
self.offset = core::mem::size_of::<SendStreamHeader>();
self.header = Some(header);
Ok(())
}
/// Read next record
pub fn read_next_record(&mut self) -> Result<Option<(SendRecord, Vec<u8>)>, &'static str> {
if self.offset + core::mem::size_of::<SendRecord>() > self.data.len() {
// Check if stream ended properly with an End marker
if !self.stream_complete {
return Err("Truncated stream: missing End marker");
}
return Ok(None);
}
// SAFETY INVARIANTS:
// 1. offset + size_of::<SendRecord>() ≤ data.len() - checked above
// 2. SendRecord is #[repr(C, packed)] with stable layout
// 3. read_unaligned handles packed struct and potential misalignment
// 4. Data written by SendStream::write_record with same layout
// 5. Owned SendRecord returned (no lifetime issues)
//
// VERIFICATION: TODO - Prove record offset arithmetic correctness
//
// JUSTIFICATION:
// Stream records (write/free/snapshot) have binary headers.
// Sequential deserialization required for processing stream.
let record: SendRecord = unsafe {
core::ptr::read_unaligned(self.data.as_ptr().add(self.offset) as *const SendRecord)
};
self.offset += core::mem::size_of::<SendRecord>();
// Check for end marker
if record.record_type == SendRecordType::End as u8 {
self.stream_complete = true;
crate::lcpfs_println!(
"[ RECV ] Stream complete: {} records processed",
self.processed_records
);
return Ok(None);
}
// Read payload
let payload_end = self.offset + record.length as usize;
if payload_end > self.data.len() {
return Err("Truncated payload");
}
let payload = self.data[self.offset..payload_end].to_vec();
self.offset = payload_end;
// Verify checksum
let expected_checksum = Checksum::calculate(&payload).first();
if record.checksum != expected_checksum {
return Err("Payload checksum mismatch");
}
self.processed_records += 1;
Ok(Some((record, payload)))
}
/// Process all records
pub fn process_stream(&mut self) -> Result<(), &'static str> {
while let Some((record, payload)) = self.read_next_record()? {
self.apply_record(&record, &payload)?;
}
crate::lcpfs_println!(
"[ RECV ] Successfully applied {} records",
self.processed_records
);
Ok(())
}
/// Apply a single record
fn apply_record(&self, record: &SendRecord, payload: &[u8]) -> Result<(), &'static str> {
let record_type = match record.record_type {
1 => SendRecordType::Begin,
2 => SendRecordType::End,
3 => SendRecordType::Object,
4 => SendRecordType::Freeobjects,
5 => SendRecordType::Write,
6 => SendRecordType::Free,
9 => SendRecordType::Snapshot,
10 => SendRecordType::Destroy,
_ => return Err("Unknown record type"),
};
match record_type {
SendRecordType::Write => {
let obj_id = record.object_id;
let offset = record.offset;
crate::lcpfs_println!(
"[ RECV ] Apply WRITE: obj={} offset={} len={}",
obj_id,
offset,
payload.len()
);
// In real implementation: write to DMU object at offset
}
SendRecordType::Object => {
let obj_id = record.object_id;
crate::lcpfs_println!("[ RECV ] Apply CREATE: obj={}", obj_id);
// Create new DMU object
}
SendRecordType::Free => {
let obj_id = record.object_id;
let offset = record.offset;
let length = record.length;
crate::lcpfs_println!(
"[ RECV ] Apply FREE: obj={} offset={} len={}",
obj_id,
offset,
length
);
// Free block range
}
SendRecordType::Snapshot => {
let snap_name = String::from_utf8_lossy(payload);
crate::lcpfs_println!("[ RECV ] Apply SNAPSHOT: {}", snap_name);
}
_ => {}
}
Ok(())
}
}
/// Example: Full dataset send
pub fn example_full_send(dataset_guid: u64, txg: u64) -> Vec<u8> {
let mut stream = SendStream::begin_full_send(dataset_guid, txg);
// Add some example data
stream.write_object(1, 1, &DnodePhys::zero());
stream.write_block(1, 0, b"Hello, LCPFS replication!");
stream.write_block(1, 4096, b"More data here...");
stream.finalize()
}
/// Example: Incremental send
pub fn example_incremental_send(
from_guid: u64,
to_guid: u64,
from_txg: u64,
to_txg: u64,
) -> Vec<u8> {
let mut stream = SendStream::begin_incremental_send(from_guid, to_guid, from_txg, to_txg);
// Only send changes between snapshots
stream.write_block(1, 8192, b"Changed block");
stream.write_free(2, 0, 4096);
stream.finalize()
}