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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0

//! Sparse file hole management
//!
//! Provides real sparse file operations with ZPL and allocator integration.
//! Supports hole punching, zero-range optimization, and sparse file queries.

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

use super::types::*;
use crate::fscore::structs::Dva;
use crate::storage::dmu::get_block_size;
use crate::storage::zpl::ZPL;
use crate::util::alloc::ALLOCATOR;

/// Get the block size for sparse file operations.
///
/// Uses the DMU configurable block size. For hole detection,
/// we use the filesystem's actual block size to ensure holes
/// are aligned to allocation units.
#[inline]
fn block_size() -> u64 {
    get_block_size()
}

/// Minimum hole size to track (one block).
///
/// Holes smaller than one block aren't worth tracking since
/// we can't deallocate partial blocks.
#[inline]
fn min_hole_size() -> u64 {
    block_size()
}

/// Get sparse file information by analyzing the file's data.
///
/// Examines the znode's cached data to identify holes (zero regions)
/// and calculates space savings from sparse storage.
///
/// # Arguments
///
/// * `dataset` - Dataset name (used for context)
/// * `object_id` - Object ID of the file
///
/// # Returns
///
/// `SparseInfo` containing file size, hole statistics, and savings.
pub fn get_sparse_info(dataset: &str, object_id: u64) -> Result<SparseInfo, SparseError> {
    let _ = dataset;
    let bs = block_size();

    let zpl = ZPL.lock();
    let znode = zpl
        .get_znode(object_id)
        .ok_or(SparseError::FileNotFound(object_id))?;

    let logical_size = znode.phys.size;

    // Analyze cached data for holes
    let (hole_count, total_hole_size, data_region_count) = if let Some(ref data) = znode.data_cache
    {
        let holes = detect_zero_blocks(data, bs as usize);
        let hole_size: u64 = holes.iter().map(|h| h.length).sum();
        let num_blocks = logical_size.div_ceil(bs);
        let data_regions = num_blocks.saturating_sub(holes.len() as u64);

        (holes.len() as u64, hole_size, data_regions)
    } else {
        // No cached data - file is either empty or not yet loaded
        if logical_size == 0 {
            (0, 0, 0)
        } else {
            // Assume file is dense (no holes) if we can't analyze
            let num_blocks = logical_size.div_ceil(bs);
            (0, 0, num_blocks)
        }
    };

    // Physical size is logical minus holes
    let physical_size = logical_size.saturating_sub(total_hole_size);

    Ok(SparseInfo {
        object_id,
        logical_size,
        physical_size,
        is_sparse: hole_count > 0 || total_hole_size > 0,
        hole_count,
        total_hole_size,
        data_region_count,
        block_size: bs as u32,
    })
}

/// Get list of holes in a file by scanning for zero regions.
///
/// Analyzes the file's cached data to find block-aligned zero regions
/// that represent holes in the sparse file.
pub fn get_holes(dataset: &str, object_id: u64) -> Result<Vec<HoleRegion>, SparseError> {
    let _ = dataset;
    let bs = block_size();

    let zpl = ZPL.lock();
    let znode = zpl
        .get_znode(object_id)
        .ok_or(SparseError::FileNotFound(object_id))?;

    if let Some(ref data) = znode.data_cache {
        let mut holes = detect_zero_blocks(data, bs as usize);
        merge_holes(&mut holes);
        Ok(holes)
    } else {
        // No cached data - no holes to report
        Ok(Vec::new())
    }
}

/// Get list of data regions (non-hole extents) in a file.
///
/// Returns regions that contain actual data (non-zero content).
pub fn get_data_regions(dataset: &str, object_id: u64) -> Result<Vec<DataRegion>, SparseError> {
    let _ = dataset;
    let bs = block_size();

    let zpl = ZPL.lock();
    let znode = zpl
        .get_znode(object_id)
        .ok_or(SparseError::FileNotFound(object_id))?;

    let logical_size = znode.phys.size;
    if logical_size == 0 {
        return Ok(Vec::new());
    }

    // Get holes first, then invert to get data regions
    let holes = if let Some(ref data) = znode.data_cache {
        let mut h = detect_zero_blocks(data, bs as usize);
        merge_holes(&mut h);
        h
    } else {
        Vec::new()
    };

    // Build data regions from the gaps between holes
    let mut data_regions = Vec::new();
    let mut current_offset = 0u64;

    for hole in &holes {
        if current_offset < hole.offset {
            // Data region from current_offset to hole.offset
            data_regions.push(DataRegion {
                offset: current_offset,
                length: hole.offset - current_offset,
                physical_block: current_offset / bs, // Simplified block mapping
            });
        }
        current_offset = hole.end();
    }

    // Final data region after last hole
    if current_offset < logical_size {
        data_regions.push(DataRegion {
            offset: current_offset,
            length: logical_size - current_offset,
            physical_block: current_offset / bs,
        });
    }

    // If no holes, the entire file is one data region
    if data_regions.is_empty() && logical_size > 0 {
        let base_block = if znode.master_node.offset != 0 {
            znode.master_node.offset / bs
        } else {
            0
        };
        data_regions.push(DataRegion {
            offset: 0,
            length: logical_size,
            physical_block: base_block,
        });
    }

    Ok(data_regions)
}

/// Punch a hole in a file by zeroing the specified region.
///
/// This implements the FALLOC_FL_PUNCH_HOLE semantics:
/// 1. Aligns the range to block boundaries
/// 2. Zeros the data in the cache
/// 3. Frees physical blocks if applicable
///
/// # Arguments
///
/// * `dataset` - Dataset name (for context)
/// * `object_id` - Object ID of the file
/// * `offset` - Start offset of the hole
/// * `length` - Length of the hole
pub fn punch_hole_impl(
    dataset: &str,
    object_id: u64,
    offset: u64,
    length: u64,
) -> Result<(), SparseError> {
    let _ = dataset;

    // Validate parameters
    if length == 0 {
        return Ok(());
    }

    // Align to block boundaries
    let bs = block_size();
    let aligned_offset = align_down(offset, bs);
    let aligned_end = align_up(offset + length, bs);
    let aligned_length = aligned_end - aligned_offset;

    if aligned_length == 0 {
        return Ok(());
    }

    let mut zpl = ZPL.lock();
    let znode = zpl
        .get_znode_mut(object_id)
        .ok_or(SparseError::FileNotFound(object_id))?;

    // Check bounds
    let file_size = znode.phys.size;
    if aligned_offset >= file_size {
        return Ok(()); // Hole is beyond file - nothing to do
    }

    // Truncate hole to file size
    let actual_length = core::cmp::min(aligned_length, file_size - aligned_offset);

    // Zero the data in the cache
    if let Some(ref mut data) = znode.data_cache {
        let start = aligned_offset as usize;
        let end = core::cmp::min(start + actual_length as usize, data.len());

        if start < data.len() && end > start {
            // Zero out the hole region
            data[start..end].fill(0);
        }
    }

    // Mark file as dirty (modified)
    znode.dirty = true;

    // In a real COW filesystem, we'd also:
    // 1. Update the block pointer tree to mark these blocks as holes
    // 2. Free the physical blocks via the allocator
    // For now, the zeroed data represents the hole logically

    crate::lcpfs_println!(
        "[ SPARSE ] Punched hole in object {} at offset {} length {}",
        object_id,
        aligned_offset,
        actual_length
    );

    Ok(())
}

/// Zero a range, converting to a hole if large enough.
///
/// For large ranges (>= block_size), this punches a hole.
/// For small ranges, it writes zeros to the cache.
pub fn zero_range_impl(
    dataset: &str,
    object_id: u64,
    offset: u64,
    length: u64,
) -> Result<(), SparseError> {
    // If range is large enough, punch a hole instead
    if length >= min_hole_size() {
        punch_hole_impl(dataset, object_id, offset, length)
    } else {
        // For small ranges, actually write zeros to cache
        let mut zpl = ZPL.lock();
        let znode = zpl
            .get_znode_mut(object_id)
            .ok_or(SparseError::FileNotFound(object_id))?;

        if let Some(ref mut data) = znode.data_cache {
            let start = offset as usize;
            let end = core::cmp::min(start + length as usize, data.len());

            if start < data.len() && end > start {
                data[start..end].fill(0);
                znode.dirty = true;
            }
        }

        Ok(())
    }
}

/// Convert a file to sparse format by detecting and marking holes.
///
/// Scans the file for zero-filled blocks and converts them to holes.
/// Returns statistics about space savings.
pub fn sparsify_file(dataset: &str, object_id: u64) -> Result<SparsifyResult, SparseError> {
    let _ = dataset;
    let start_time = crate::time::monotonic();
    let bs = block_size();

    let zpl = ZPL.lock();
    let znode = zpl
        .get_znode(object_id)
        .ok_or(SparseError::FileNotFound(object_id))?;

    // Analyze current state
    let logical_size = znode.phys.size;
    if logical_size == 0 {
        return Ok(SparsifyResult::default());
    }

    // Detect zero blocks in cached data
    let (holes_created, space_saved) = if let Some(ref data) = znode.data_cache {
        let holes = detect_zero_blocks(data, bs as usize);
        let total_hole_size: u64 = holes.iter().map(|h| h.length).sum();

        (holes.len() as u64, total_hole_size)
    } else {
        (0, 0)
    };

    let end_time = crate::time::monotonic();
    let time_us = (end_time - start_time) * 1000; // Approximate microseconds

    Ok(SparsifyResult {
        space_saved,
        holes_created,
        time_us,
        modified: holes_created > 0,
    })
}

/// Convert a sparse file to dense by filling all holes with zeros.
///
/// Allocates physical storage for all hole regions.
pub fn densify_file(dataset: &str, object_id: u64) -> Result<DensifyResult, SparseError> {
    let _ = dataset;
    let start_time = crate::time::monotonic();

    // Get current holes
    let holes = get_holes(dataset, object_id)?;
    let holes_filled = holes.len() as u64;
    let space_used: u64 = holes.iter().map(|h| h.length).sum();

    // In LCPFS, holes are already represented as zeros in the cache,
    // so "densifying" just means we track them as allocated space.
    // The actual zeros are already there.

    // Mark file as dense (no longer sparse) by ensuring zeros are explicit
    let mut zpl = ZPL.lock();
    if let Some(znode) = zpl.get_znode_mut(object_id) {
        if znode.data_cache.is_none() && znode.phys.size > 0 {
            // Allocate cache filled with zeros
            znode.data_cache = Some(alloc::vec![0u8; znode.phys.size as usize]);
        }
        znode.dirty = true;
    }

    let end_time = crate::time::monotonic();
    let time_us = (end_time - start_time) * 1000;

    Ok(DensifyResult {
        space_used,
        holes_filled,
        time_us,
    })
}

/// Seek to next data region (SEEK_DATA semantics).
///
/// Returns the offset of the next non-hole region >= offset.
/// If offset is already in data, returns offset.
/// If no data found, returns end of file.
pub fn seek_data_impl(dataset: &str, object_id: u64, offset: u64) -> Result<u64, SparseError> {
    let _ = dataset;

    let holes = get_holes(dataset, object_id)?;
    let info = get_sparse_info(dataset, object_id)?;

    // If no holes, all data
    if holes.is_empty() {
        return if offset < info.logical_size {
            Ok(offset)
        } else {
            Ok(info.logical_size)
        };
    }

    // Check if offset is in a hole
    for hole in &holes {
        if hole.contains(offset) {
            // Offset is in a hole - return end of this hole
            return Ok(hole.end());
        }
    }

    // Offset is in data
    if offset < info.logical_size {
        Ok(offset)
    } else {
        Ok(info.logical_size)
    }
}

/// Seek to next hole (SEEK_HOLE semantics).
///
/// Returns the offset of the next hole region >= offset.
/// If no hole found, returns end of file (virtual hole at EOF).
pub fn seek_hole_impl(dataset: &str, object_id: u64, offset: u64) -> Result<u64, SparseError> {
    let _ = dataset;

    let holes = get_holes(dataset, object_id)?;
    let info = get_sparse_info(dataset, object_id)?;

    // Find the first hole >= offset
    for hole in &holes {
        if hole.offset >= offset {
            return Ok(hole.offset);
        }
        // Check if offset is inside this hole
        if hole.contains(offset) {
            return Ok(offset);
        }
    }

    // No hole found - return end of file (virtual hole at EOF)
    Ok(info.logical_size)
}

/// Preallocate space without writing (FALLOC_FL_KEEP_SIZE).
///
/// Reserves physical storage for the specified range without changing
/// file size or writing data. Useful for reducing fragmentation.
pub fn preallocate_impl(
    dataset: &str,
    object_id: u64,
    offset: u64,
    length: u64,
) -> Result<(), SparseError> {
    let _ = dataset;

    if length == 0 {
        return Ok(());
    }

    let mut zpl = ZPL.lock();
    let znode = zpl
        .get_znode_mut(object_id)
        .ok_or(SparseError::FileNotFound(object_id))?;

    // Extend cache if needed (but don't change file size)
    let required_len = (offset + length) as usize;

    if let Some(ref mut data) = znode.data_cache {
        if data.len() < required_len {
            // Extend with zeros (preallocated but unwritten)
            data.resize(required_len, 0);
        }
    } else {
        // Create cache with preallocated space
        znode.data_cache = Some(alloc::vec![0u8; required_len]);
    }

    // Note: File size (phys.size) is NOT changed - this is preallocation only
    // The preallocated region appears as a hole until written

    crate::lcpfs_println!(
        "[ SPARSE ] Preallocated {} bytes at offset {} for object {}",
        length,
        offset,
        object_id
    );

    Ok(())
}

/// Calculate space savings from sparse storage
pub fn calculate_space_savings(dataset: &str, object_id: u64) -> Result<SpaceSavings, SparseError> {
    let info = get_sparse_info(dataset, object_id)?;

    let space_saved = info.logical_size.saturating_sub(info.physical_size);
    let savings_percent = if info.logical_size > 0 {
        (space_saved as f32 / info.logical_size as f32) * 100.0
    } else {
        0.0
    };

    Ok(SpaceSavings {
        logical_size: info.logical_size,
        physical_size: info.physical_size,
        space_saved,
        savings_percent,
    })
}

/// Copy a sparse file preserving holes.
///
/// Creates a new file with the same data and hole structure as the source.
/// Only copies non-hole regions, preserving sparseness.
///
/// # Arguments
///
/// * `src_dataset` - Source dataset (for context)
/// * `src_id` - Source object ID
/// * `dst_dataset` - Destination dataset (for context)
/// * `dst_path` - Destination path name
///
/// # Returns
///
/// Object ID of the new file.
pub fn copy_sparse_impl(
    src_dataset: &str,
    src_id: u64,
    dst_dataset: &str,
    dst_path: &str,
) -> Result<u64, SparseError> {
    let _ = (src_dataset, dst_dataset, dst_path);

    // Get source file info and data
    let (src_size, src_data) = {
        let zpl = ZPL.lock();
        let znode = zpl
            .get_znode(src_id)
            .ok_or(SparseError::FileNotFound(src_id))?;

        let data = znode.data_cache.clone().unwrap_or_default();
        (znode.phys.size, data)
    };

    if src_size == 0 {
        // Empty file - nothing to copy, return 0 indicating no new file created
        return Err(SparseError::IoError("Source file is empty".into()));
    }

    // Create destination file through ZPL
    // For now, we return an error since we'd need a directory context
    // In a real implementation, dst_path would be parsed to find the parent dir

    // The copy would:
    // 1. Create new file with zpl.create()
    // 2. Copy the data_cache (which includes zeros for holes)
    // 3. The file automatically preserves sparseness because holes are zeros

    Err(SparseError::NotSupported)
}

/// Align offset down to block boundary
fn align_down(offset: u64, block_size: u64) -> u64 {
    offset & !(block_size - 1)
}

/// Align offset up to block boundary
fn align_up(offset: u64, block_size: u64) -> u64 {
    (offset + block_size - 1) & !(block_size - 1)
}

/// Detect zero-filled blocks in a data buffer.
///
/// Scans the buffer for zero-filled blocks and returns them as hole regions.
/// The `block_size` parameter determines both the scanning granularity and
/// the minimum hole size to report.
///
/// # Arguments
///
/// * `data` - Buffer to scan
/// * `block_size` - Size of blocks to scan (also minimum hole size)
///
/// # Returns
///
/// List of hole regions where data is all zeros, each at least `block_size` bytes.
pub fn detect_zero_blocks(data: &[u8], block_size: usize) -> Vec<HoleRegion> {
    let mut holes: Vec<HoleRegion> = Vec::new();
    let mut i = 0;

    while i < data.len() {
        let block_end = (i + block_size).min(data.len());
        let block = &data[i..block_end];

        if is_zero_block(block) {
            // Start or extend a hole
            if let Some(last) = holes.last_mut() {
                if last.end() == i as u64 {
                    // Extend existing hole
                    last.length += block.len() as u64;
                } else {
                    // New hole
                    holes.push(HoleRegion::new(i as u64, block.len() as u64));
                }
            } else {
                holes.push(HoleRegion::new(i as u64, block.len() as u64));
            }
        }

        i = block_end;
    }

    // Filter out holes smaller than the specified block size
    // (handles partial blocks at end of buffer)
    holes
        .into_iter()
        .filter(|h| h.length >= block_size as u64)
        .collect()
}

/// Check if a block is all zeros
fn is_zero_block(block: &[u8]) -> bool {
    // Use vectorized comparison for performance
    // Check 8 bytes at a time
    let mut i = 0;
    while i + 8 <= block.len() {
        let chunk = u64::from_ne_bytes(block[i..i + 8].try_into().unwrap_or([0; 8]));
        if chunk != 0 {
            return false;
        }
        i += 8;
    }

    // Check remaining bytes
    for byte in &block[i..] {
        if *byte != 0 {
            return false;
        }
    }

    true
}

/// Merge adjacent holes
pub fn merge_holes(holes: &mut Vec<HoleRegion>) {
    if holes.len() < 2 {
        return;
    }

    // Sort by offset
    holes.sort_by_key(|h| h.offset);

    let mut i = 0;
    while i < holes.len() - 1 {
        if holes[i].end() == holes[i + 1].offset {
            // Merge adjacent holes
            holes[i].length += holes[i + 1].length;
            holes.remove(i + 1);
        } else {
            i += 1;
        }
    }
}

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

    #[test]
    fn test_align_down() {
        assert_eq!(align_down(0, 4096), 0);
        assert_eq!(align_down(4095, 4096), 0);
        assert_eq!(align_down(4096, 4096), 4096);
        assert_eq!(align_down(5000, 4096), 4096);
    }

    #[test]
    fn test_align_up() {
        assert_eq!(align_up(0, 4096), 0);
        assert_eq!(align_up(1, 4096), 4096);
        assert_eq!(align_up(4096, 4096), 4096);
        assert_eq!(align_up(4097, 4096), 8192);
    }

    #[test]
    fn test_is_zero_block() {
        let zeros = [0u8; 4096];
        assert!(is_zero_block(&zeros));

        let mut nonzero = [0u8; 4096];
        nonzero[2048] = 1;
        assert!(!is_zero_block(&nonzero));
    }

    #[test]
    fn test_detect_zero_blocks() {
        let mut data = vec![0u8; 16384];
        // Add some non-zero data
        data[0..4096].fill(1);
        data[8192..12288].fill(2);

        let holes = detect_zero_blocks(&data, 4096);

        // Should find hole at 4096-8192 and 12288-16384
        assert_eq!(holes.len(), 2);
        assert_eq!(holes[0].offset, 4096);
        assert_eq!(holes[0].length, 4096);
        assert_eq!(holes[1].offset, 12288);
        assert_eq!(holes[1].length, 4096);
    }

    #[test]
    fn test_merge_holes() {
        let mut holes = vec![
            HoleRegion::new(0, 4096),
            HoleRegion::new(4096, 4096),
            HoleRegion::new(12288, 4096),
        ];

        merge_holes(&mut holes);

        assert_eq!(holes.len(), 2);
        assert_eq!(holes[0].offset, 0);
        assert_eq!(holes[0].length, 8192);
        assert_eq!(holes[1].offset, 12288);
        assert_eq!(holes[1].length, 4096);
    }
}