vfat-rs 0.1.1

A no_std-compatible FAT32/VFAT filesystem implementation in Rust for custom kernels
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
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec;
use alloc::vec::Vec;

use log::info;
use spin::mutex::SpinMutex;

use crate::SectorId;
use crate::error::Result;
use crate::formats::cluster_id::ClusterId;
use crate::traits::BlockDevice;

/// A cached sector entry.
struct CacheEntry {
    sector: SectorId,
    data: Vec<u8>,
    dirty: bool,
    /// Monotonic counter for LRU eviction.
    last_used: u64,
}

/// An interface to the underlying Block Device with an optional write-back sector cache.
///
/// When `cache_capacity` is 0, all reads and writes pass directly through to the
/// device (no caching overhead). When non-zero, sectors are cached in memory and
/// dirty sectors are flushed to the device on [`flush()`](Self::flush), eviction,
/// or [`Drop`].
pub(crate) struct CachedPartition {
    device: SpinMutex<Box<dyn BlockDevice + Send>>,
    pub(crate) sector_size: usize,
    pub(crate) fat_start_sector: SectorId,
    /// How many sectors are mapped to a single cluster
    pub(crate) sectors_per_cluster: u32,
    /// First sector containing actual data - after all FAT tables.
    pub(crate) data_start_sector: SectorId,
    /// Number of FAT copies (typically 2 for redundancy)
    pub(crate) fat_amount: u8,
    /// Number of sectors per FAT table
    pub(crate) sectors_per_fat: u32,
    /// Sector cache. Protected by its own lock to avoid holding the device lock.
    cache: SpinMutex<SectorCache>,
}

struct SectorCache {
    entries: Vec<CacheEntry>,
    capacity: usize,
    /// Monotonically increasing counter for LRU ordering.
    access_counter: u64,
}

impl SectorCache {
    fn new(capacity: usize) -> Self {
        Self {
            entries: Vec::with_capacity(capacity),
            capacity,
            access_counter: 0,
        }
    }

    fn next_tick(&mut self) -> u64 {
        self.access_counter += 1;
        self.access_counter
    }

    /// Find a cached entry by sector id, returning its index.
    fn find(&self, sector: SectorId) -> Option<usize> {
        self.entries.iter().position(|e| e.sector == sector)
    }

    /// Find the index of the least recently used entry.
    fn lru_index(&self) -> Option<usize> {
        self.entries
            .iter()
            .enumerate()
            .min_by_key(|(_, e)| e.last_used)
            .map(|(i, _)| i)
    }
}

impl CachedPartition {
    /// Create a new CachedPartition with caching disabled (capacity 0).
    #[cfg(test)]
    pub fn new<T>(
        device: T,
        sector_size: usize,
        fat_start_sector: SectorId,
        sectors_per_cluster: u32,
        data_start_sector: SectorId,
        fat_amount: u8,
        sectors_per_fat: u32,
    ) -> Self
    where
        T: BlockDevice + Send + 'static,
    {
        Self::new_with_cache(
            device,
            sector_size,
            fat_start_sector,
            sectors_per_cluster,
            data_start_sector,
            fat_amount,
            sectors_per_fat,
            0, // default: no caching (preserves existing behavior)
        )
    }

    pub fn new_with_cache<T>(
        device: T,
        sector_size: usize,
        fat_start_sector: SectorId,
        sectors_per_cluster: u32,
        data_start_sector: SectorId,
        fat_amount: u8,
        sectors_per_fat: u32,
        cache_capacity: usize,
    ) -> Self
    where
        T: BlockDevice + Send + 'static,
    {
        info!(
            "Creating cached partition (cache_capacity={})",
            cache_capacity
        );
        Self {
            device: SpinMutex::new(Box::new(device)),
            sector_size,
            fat_start_sector,
            sectors_per_cluster,
            data_start_sector,
            fat_amount,
            sectors_per_fat,
            cache: SpinMutex::new(SectorCache::new(cache_capacity)),
        }
    }

    /// Flush all dirty cached sectors to the device.
    pub fn flush(&self) -> Result<()> {
        let mut cache = self.cache.lock();
        let mut device = self.device.lock();
        for entry in cache.entries.iter_mut() {
            if entry.dirty {
                device.write_sector(entry.sector, &entry.data)?;
                entry.dirty = false;
            }
        }
        Ok(())
    }

    /// Flush a single cache entry to the device (caller must hold both locks).
    fn flush_entry(device: &mut Box<dyn BlockDevice + Send>, entry: &mut CacheEntry) -> Result<()> {
        if entry.dirty {
            device.write_sector(entry.sector, &entry.data)?;
            entry.dirty = false;
        }
        Ok(())
    }

    pub(crate) fn read_sector(&self, sector: SectorId, buf: &mut [u8]) -> Result<usize> {
        let mut cache = self.cache.lock();
        if cache.capacity == 0 {
            drop(cache);
            let mut dev_lock = self.device.lock();
            return dev_lock.read_sector(sector, buf);
        }

        // Cache hit
        if let Some(idx) = cache.find(sector) {
            let tick = cache.next_tick();
            let entry = &mut cache.entries[idx];
            entry.last_used = tick;
            let len = core::cmp::min(buf.len(), entry.data.len());
            buf[..len].copy_from_slice(&entry.data[..len]);
            return Ok(len);
        }

        // Cache miss: read from device
        let mut dev_lock = self.device.lock();
        let mut sector_buf = vec![0u8; self.sector_size];
        let read = dev_lock.read_sector(sector, &mut sector_buf)?;
        let len = core::cmp::min(buf.len(), read);
        buf[..len].copy_from_slice(&sector_buf[..len]);

        // Insert into cache (evict LRU if full)
        if cache.entries.len() >= cache.capacity {
            if let Some(lru) = cache.lru_index() {
                Self::flush_entry(&mut dev_lock, &mut cache.entries[lru])?;
                let tick = cache.next_tick();
                cache.entries[lru] = CacheEntry {
                    sector,
                    data: sector_buf,
                    dirty: false,
                    last_used: tick,
                };
            }
        } else {
            let tick = cache.next_tick();
            cache.entries.push(CacheEntry {
                sector,
                data: sector_buf,
                dirty: false,
                last_used: tick,
            });
        }

        Ok(len)
    }

    pub(crate) fn read_sector_offset(
        self: Arc<Self>,
        sector: SectorId,
        offset: usize,
        buf: &mut [u8],
    ) -> Result<usize> {
        let mut cache = self.cache.lock();
        if cache.capacity == 0 {
            drop(cache);
            let mut dev_lock = self.device.lock();
            return dev_lock.read_sector_offset(sector, offset, buf);
        }

        // Cache hit
        if let Some(idx) = cache.find(sector) {
            let tick = cache.next_tick();
            let entry = &mut cache.entries[idx];
            entry.last_used = tick;
            let available = entry.data.len().saturating_sub(offset);
            let len = core::cmp::min(buf.len(), available);
            buf[..len].copy_from_slice(&entry.data[offset..offset + len]);
            return Ok(len);
        }

        // Cache miss: read full sector from device, cache it, return slice
        let mut dev_lock = self.device.lock();
        let mut sector_buf = vec![0u8; self.sector_size];
        dev_lock.read_sector(sector, &mut sector_buf)?;
        let available = sector_buf.len().saturating_sub(offset);
        let len = core::cmp::min(buf.len(), available);
        buf[..len].copy_from_slice(&sector_buf[offset..offset + len]);

        if cache.entries.len() >= cache.capacity {
            if let Some(lru) = cache.lru_index() {
                Self::flush_entry(&mut dev_lock, &mut cache.entries[lru])?;
                let tick = cache.next_tick();
                cache.entries[lru] = CacheEntry {
                    sector,
                    data: sector_buf,
                    dirty: false,
                    last_used: tick,
                };
            }
        } else {
            let tick = cache.next_tick();
            cache.entries.push(CacheEntry {
                sector,
                data: sector_buf,
                dirty: false,
                last_used: tick,
            });
        }

        Ok(len)
    }

    #[allow(unused)]
    fn write_sector(self: Arc<Self>, sector: SectorId, buf: &[u8]) -> Result<usize> {
        let mut cache = self.cache.lock();
        if cache.capacity == 0 {
            drop(cache);
            let mut dev_lock = self.device.lock();
            return dev_lock.write_sector(sector, buf);
        }

        let len = buf.len();
        let tick = cache.next_tick();

        // Cache hit: update in place
        if let Some(idx) = cache.find(sector) {
            let entry = &mut cache.entries[idx];
            entry.data[..len].copy_from_slice(buf);
            entry.dirty = true;
            entry.last_used = tick;
            return Ok(len);
        }

        // Cache miss: insert new dirty entry
        let mut data = vec![0u8; self.sector_size];
        data[..len].copy_from_slice(buf);

        if cache.entries.len() >= cache.capacity {
            if let Some(lru) = cache.lru_index() {
                let mut dev_lock = self.device.lock();
                Self::flush_entry(&mut dev_lock, &mut cache.entries[lru])?;
                cache.entries[lru] = CacheEntry {
                    sector,
                    data,
                    dirty: true,
                    last_used: tick,
                };
            }
        } else {
            cache.entries.push(CacheEntry {
                sector,
                data,
                dirty: true,
                last_used: tick,
            });
        }

        Ok(len)
    }

    pub(crate) fn write_sector_offset(
        self: Arc<Self>,
        sector: SectorId,
        offset: usize,
        buf: &[u8],
    ) -> Result<usize> {
        let mut cache = self.cache.lock();
        if cache.capacity == 0 {
            drop(cache);
            let mut dev_lock = self.device.lock();
            return dev_lock.write_sector_offset(sector, offset, buf);
        }

        let len = buf.len();
        let tick = cache.next_tick();

        // Cache hit: update in place
        if let Some(idx) = cache.find(sector) {
            let entry = &mut cache.entries[idx];
            entry.data[offset..offset + len].copy_from_slice(buf);
            entry.dirty = true;
            entry.last_used = tick;
            return Ok(len);
        }

        // Cache miss: read full sector first, then apply the write
        let mut dev_lock = self.device.lock();
        let mut data = vec![0u8; self.sector_size];
        dev_lock.read_sector(sector, &mut data)?;
        data[offset..offset + len].copy_from_slice(buf);

        if cache.entries.len() >= cache.capacity {
            if let Some(lru) = cache.lru_index() {
                Self::flush_entry(&mut dev_lock, &mut cache.entries[lru])?;
                cache.entries[lru] = CacheEntry {
                    sector,
                    data,
                    dirty: true,
                    last_used: tick,
                };
            }
        } else {
            cache.entries.push(CacheEntry {
                sector,
                data,
                dirty: true,
                last_used: tick,
            });
        }

        Ok(len)
    }

    /// Converts a cluster (a FAT concept) to a sector (a BlockDevice concept).
    ///
    /// To do so, it uses some useful info from the BPB section.
    pub(crate) fn cluster_to_sector(&self, cluster: ClusterId) -> SectorId {
        let selected_sector = u32::from(cluster).saturating_sub(2) * self.sectors_per_cluster;
        let sect = self.data_start_sector.0 + selected_sector;
        SectorId(sect)
    }

    #[allow(unused)]
    fn get_canonical_name() -> &'static str
    where
        Self: Sized,
    {
        "CachePartition"
    }
}

impl Drop for CachedPartition {
    fn drop(&mut self) {
        // Best-effort flush on drop. We can't propagate errors here,
        // so dirty data is silently lost if the device write fails.
        let cache = self.cache.get_mut();
        let device = self.device.get_mut();
        for entry in cache.entries.iter_mut() {
            if entry.dirty {
                let _ = device.write_sector(entry.sector, &entry.data);
                entry.dirty = false;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::sync::Arc;
    use spin::mutex::SpinMutex;

    /// A simple in-memory block device that tracks reads and writes.
    struct MemBlockDevice {
        /// Flat storage: sector N occupies bytes [N*512 .. (N+1)*512].
        data: Arc<SpinMutex<Vec<u8>>>,
        read_count: Arc<SpinMutex<u32>>,
        write_count: Arc<SpinMutex<u32>>,
    }

    impl MemBlockDevice {
        fn new(num_sectors: usize) -> Self {
            Self {
                data: Arc::new(SpinMutex::new(vec![0u8; num_sectors * 512])),
                read_count: Arc::new(SpinMutex::new(0)),
                write_count: Arc::new(SpinMutex::new(0)),
            }
        }

        fn reads(&self) -> u32 {
            *self.read_count.lock()
        }

        fn writes(&self) -> u32 {
            *self.write_count.lock()
        }

        /// Read a sector directly from backing store (bypasses cache).
        fn read_raw(&self, sector: SectorId, buf: &mut [u8]) {
            let start = sector.0 as usize * 512;
            let len = core::cmp::min(buf.len(), 512);
            buf[..len].copy_from_slice(&self.data.lock()[start..start + len]);
        }
    }

    impl Clone for MemBlockDevice {
        fn clone(&self) -> Self {
            Self {
                data: self.data.clone(),
                read_count: self.read_count.clone(),
                write_count: self.write_count.clone(),
            }
        }
    }

    impl BlockDevice for MemBlockDevice {
        fn read_sector_offset(
            &mut self,
            sector: SectorId,
            offset: usize,
            buf: &mut [u8],
        ) -> Result<usize> {
            *self.read_count.lock() += 1;
            let start = sector.0 as usize * 512 + offset;
            let data = self.data.lock();
            let len = core::cmp::min(buf.len(), data.len() - start);
            buf[..len].copy_from_slice(&data[start..start + len]);
            Ok(len)
        }

        fn write_sector_offset(
            &mut self,
            sector: SectorId,
            offset: usize,
            buf: &[u8],
        ) -> Result<usize> {
            *self.write_count.lock() += 1;
            let start = sector.0 as usize * 512 + offset;
            let mut data = self.data.lock();
            data[start..start + buf.len()].copy_from_slice(buf);
            Ok(buf.len())
        }

        fn get_canonical_name() -> &'static str
        where
            Self: Sized,
        {
            "MemBlockDevice"
        }
    }

    fn make_cached(dev: MemBlockDevice, cache_capacity: usize) -> Arc<CachedPartition> {
        Arc::new(CachedPartition::new_with_cache(
            dev,
            512,
            SectorId(0),
            1,
            SectorId(10),
            1,
            1,
            cache_capacity,
        ))
    }

    #[test]
    fn test_cache_zero_capacity_passthrough() {
        let dev = MemBlockDevice::new(16);
        let dev_clone = dev.clone();
        let cp = make_cached(dev, 0);

        // Write goes directly to device
        cp.clone()
            .write_sector_offset(SectorId(1), 0, &[0xAA; 4])
            .unwrap();
        assert_eq!(dev_clone.writes(), 1);

        // Read goes directly to device
        let mut buf = [0u8; 4];
        cp.read_sector(SectorId(1), &mut buf).unwrap();
        assert_eq!(dev_clone.reads(), 1);
        assert_eq!(buf, [0xAA; 4]);
    }

    #[test]
    fn test_cache_read_hit_avoids_device() {
        let dev = MemBlockDevice::new(16);
        let dev_clone = dev.clone();
        let cp = make_cached(dev, 4);

        // First read: cache miss → device read
        let mut buf = [0u8; 512];
        cp.read_sector(SectorId(1), &mut buf).unwrap();
        assert_eq!(dev_clone.reads(), 1);

        // Second read: cache hit → no device read
        cp.read_sector(SectorId(1), &mut buf).unwrap();
        assert_eq!(dev_clone.reads(), 1);
    }

    #[test]
    fn test_cache_write_is_deferred() {
        let dev = MemBlockDevice::new(16);
        let dev_clone = dev.clone();
        let cp = make_cached(dev, 4);

        // Populate cache with a read first
        let mut buf = [0u8; 512];
        cp.read_sector(SectorId(1), &mut buf).unwrap();

        // Write to cached sector: should NOT hit device yet
        cp.clone()
            .write_sector_offset(SectorId(1), 0, &[0xBB; 4])
            .unwrap();
        assert_eq!(dev_clone.writes(), 0);

        // Flush: now device should be written
        cp.flush().unwrap();
        assert_eq!(dev_clone.writes(), 1);

        // Verify data on device
        let mut raw = [0u8; 4];
        dev_clone.read_raw(SectorId(1), &mut raw);
        assert_eq!(raw, [0xBB; 4]);
    }

    #[test]
    fn test_cache_lru_eviction_flushes_dirty() {
        let dev = MemBlockDevice::new(16);
        let dev_clone = dev.clone();
        // Cache of size 2
        let cp = make_cached(dev, 2);

        // Read and dirty sector 1 (last_used=2 after write)
        let mut buf = [0u8; 512];
        cp.read_sector(SectorId(1), &mut buf).unwrap();
        cp.clone()
            .write_sector_offset(SectorId(1), 0, &[0xCC; 4])
            .unwrap();
        assert_eq!(dev_clone.writes(), 0); // still cached

        // Read sector 2: sector 1 is LRU but cache not full yet
        cp.read_sector(SectorId(2), &mut buf).unwrap();

        // Read sector 3: evicts LRU (sector 1, which is dirty) → device write
        cp.read_sector(SectorId(3), &mut buf).unwrap();
        assert_eq!(dev_clone.writes(), 1); // sector 1 flushed on eviction

        // Verify evicted data landed on device
        let mut raw = [0u8; 4];
        dev_clone.read_raw(SectorId(1), &mut raw);
        assert_eq!(raw, [0xCC; 4]);
    }

    #[test]
    fn test_cache_drop_flushes_dirty() {
        let dev = MemBlockDevice::new(16);
        let dev_clone = dev.clone();

        {
            let cp = make_cached(dev, 4);
            // Read then dirty a sector
            let mut buf = [0u8; 512];
            cp.read_sector(SectorId(5), &mut buf).unwrap();
            cp.clone()
                .write_sector_offset(SectorId(5), 0, &[0xDD; 4])
                .unwrap();
            assert_eq!(dev_clone.writes(), 0);
            // cp dropped here
        }

        // After drop, dirty sector should be flushed
        assert_eq!(dev_clone.writes(), 1);
        let mut raw = [0u8; 4];
        dev_clone.read_raw(SectorId(5), &mut raw);
        assert_eq!(raw, [0xDD; 4]);
    }

    #[test]
    fn test_cache_flush_idempotent() {
        let dev = MemBlockDevice::new(16);
        let dev_clone = dev.clone();
        let cp = make_cached(dev, 4);

        let mut buf = [0u8; 512];
        cp.read_sector(SectorId(1), &mut buf).unwrap();
        cp.clone()
            .write_sector_offset(SectorId(1), 0, &[0xEE; 4])
            .unwrap();

        cp.flush().unwrap();
        assert_eq!(dev_clone.writes(), 1);

        // Second flush: nothing dirty → no additional writes
        cp.flush().unwrap();
        assert_eq!(dev_clone.writes(), 1);
    }
}