littlefs-rust-core 0.1.0

Port of LittleFS to unsafe rust
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
//! Block device operations. Per lfs.c lfs_bd_read, lfs_bd_prog, lfs_bd_crc, etc.

use crate::bd::LfsCache;
use crate::error::LFS_ERR_CORRUPT;
use crate::fs::Lfs;
use crate::types::{lfs_block_t, lfs_off_t, lfs_size_t};
use crate::util::{lfs_aligndown, lfs_alignup, lfs_min};

/// Per lfs.c lfs_cache_drop (lines 31-36)
///
/// C:
/// ```c
/// static inline void lfs_cache_drop(lfs_t *lfs, lfs_cache_t *rcache) {
///     // do not zero, cheaper if cache is readonly or only going to be
///     // written with identical data (during relocates)
///     (void)lfs;
///     rcache->block = LFS_BLOCK_NULL;
/// }
/// ```
#[inline(always)]
pub fn lfs_cache_drop(_lfs: *const Lfs, rcache: *mut LfsCache) {
    unsafe {
        (*rcache).block = crate::types::LFS_BLOCK_NULL;
    }
}

/// Per lfs.c lfs_cache_zero (lines 38-42)
///
/// C:
/// ```c
/// static inline void lfs_cache_zero(lfs_t *lfs, lfs_cache_t *pcache) {
///     // zero to avoid information leak
///     memset(pcache->buffer, 0xff, lfs->cfg->cache_size);
///     pcache->block = LFS_BLOCK_NULL;
/// }
/// ```
#[inline(always)]
pub fn lfs_cache_zero(lfs: *const Lfs, pcache: *mut LfsCache) {
    unsafe {
        let cfg = (*lfs).cfg;
        let cache_size = (*cfg).cache_size as usize;
        let buf = (*pcache).buffer;
        if !buf.is_null() {
            core::ptr::write_bytes(buf, 0xff, cache_size);
        }
        (*pcache).block = crate::types::LFS_BLOCK_NULL;
    }
}

/// Per lfs.c lfs_bd_read (lines 44-126)
///
/// C:
/// ```c
/// static int lfs_bd_read(lfs_t *lfs,
///         const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
///         lfs_block_t block, lfs_off_t off,
///         void *buffer, lfs_size_t size) {
///     uint8_t *data = buffer;
///     if (off+size > lfs->cfg->block_size
///             || (lfs->block_count && block >= lfs->block_count)) {
///         return LFS_ERR_CORRUPT;
///     }
///
///     while (size > 0) {
///         lfs_size_t diff = size;
///
///         if (pcache && block == pcache->block &&
///                 off < pcache->off + pcache->size) {
///             if (off >= pcache->off) {
///                 // is already in pcache?
///                 diff = lfs_min(diff, pcache->size - (off-pcache->off));
///                 memcpy(data, &pcache->buffer[off-pcache->off], diff);
///
///                 data += diff;
///                 off += diff;
///                 size -= diff;
///                 continue;
///             }
///
///             // pcache takes priority
///             diff = lfs_min(diff, pcache->off-off);
///         }
///
///         if (block == rcache->block &&
///                 off < rcache->off + rcache->size) {
///             if (off >= rcache->off) {
///                 // is already in rcache?
///                 diff = lfs_min(diff, rcache->size - (off-rcache->off));
///                 memcpy(data, &rcache->buffer[off-rcache->off], diff);
///
///                 data += diff;
///                 off += diff;
///                 size -= diff;
///                 continue;
///             }
///
///             // rcache takes priority
///             diff = lfs_min(diff, rcache->off-off);
///         }
///
///         if (size >= hint && off % lfs->cfg->read_size == 0 &&
///                 size >= lfs->cfg->read_size) {
///             // bypass cache?
///             diff = lfs_aligndown(diff, lfs->cfg->read_size);
///             int err = lfs->cfg->read(lfs->cfg, block, off, data, diff);
///             LFS_ASSERT(err <= 0);
///             if (err) {
///                 return err;
///             }
///
///             data += diff;
///             off += diff;
///             size -= diff;
///             continue;
///         }
///
///         // load to cache, first condition can no longer fail
///         LFS_ASSERT(!lfs->block_count || block < lfs->block_count);
///         rcache->block = block;
///         rcache->off = lfs_aligndown(off, lfs->cfg->read_size);
///         rcache->size = lfs_min(
///                 lfs_min(
///                     lfs_alignup(off+hint, lfs->cfg->read_size),
///                     lfs->cfg->block_size)
///                 - rcache->off,
///                 lfs->cfg->cache_size);
///         int err = lfs->cfg->read(lfs->cfg, rcache->block,
///                 rcache->off, rcache->buffer, rcache->size);
///         LFS_ASSERT(err <= 0);
///         if (err) {
///             return err;
///         }
///     }
///
///     return 0;
/// }
/// ```
pub fn lfs_bd_read(
    lfs: *mut Lfs,
    pcache: *const LfsCache,
    rcache: *mut LfsCache,
    hint: lfs_size_t,
    block: lfs_block_t,
    off: lfs_off_t,
    buffer: *mut u8,
    size: lfs_size_t,
) -> i32 {
    unsafe {
        let lfs = &mut *lfs;
        let cfg = &*lfs.cfg;
        let read = match cfg.read {
            Some(f) => f,
            None => return LFS_ERR_CORRUPT,
        };

        if off + size > cfg.block_size || (lfs.block_count != 0 && block >= lfs.block_count) {
            return crate::lfs_err!(LFS_ERR_CORRUPT);
        }

        let mut data = buffer;
        let mut off = off;
        let mut size = size;

        while size > 0 {
            let mut diff = size;

            if !pcache.is_null() {
                let pcache = &*pcache;
                if block == pcache.block && off < pcache.off + pcache.size {
                    if off >= pcache.off {
                        diff = lfs_min(diff, pcache.size - (off - pcache.off));
                        if !pcache.buffer.is_null() {
                            core::ptr::copy_nonoverlapping(
                                pcache.buffer.add((off - pcache.off) as usize),
                                data,
                                diff as usize,
                            );
                        }
                        data = data.add(diff as usize);
                        off += diff;
                        size -= diff;
                        continue;
                    }
                    diff = lfs_min(diff, pcache.off - off);
                }
            }

            let rcache = &mut *rcache;
            if block == rcache.block && off < rcache.off + rcache.size {
                if off >= rcache.off {
                    diff = lfs_min(diff, rcache.size - (off - rcache.off));
                    if !rcache.buffer.is_null() {
                        core::ptr::copy_nonoverlapping(
                            rcache.buffer.add((off - rcache.off) as usize),
                            data,
                            diff as usize,
                        );
                    }
                    data = data.add(diff as usize);
                    off += diff;
                    size -= diff;
                    continue;
                }
                diff = lfs_min(diff, rcache.off - off);
            }

            if size >= hint && off.is_multiple_of(cfg.read_size) && size >= cfg.read_size {
                diff = lfs_aligndown(diff, cfg.read_size);
                crate::lfs_trace!("bd_read block={} off={} size={}", block, off, diff);
                let err = read(cfg as *const _, block, off, data, diff);
                crate::lfs_assert!(err <= 0);
                if err != 0 {
                    crate::lfs_trace!("bd_read block={} -> CORRUPT", block);
                    return crate::lfs_pass_err!(err);
                }
                data = data.add(diff as usize);
                off += diff;
                size -= diff;
                continue;
            }

            crate::lfs_assert!(lfs.block_count == 0 || block < lfs.block_count);
            rcache.block = block;
            rcache.off = lfs_aligndown(off, cfg.read_size);
            rcache.size = lfs_min(
                lfs_min(lfs_alignup(off + hint, cfg.read_size), cfg.block_size)
                    .saturating_sub(rcache.off),
                cfg.cache_size,
            );
            crate::lfs_trace!(
                "bd_read block={} off={} size={}",
                rcache.block,
                rcache.off,
                rcache.size
            );
            let err = read(
                cfg as *const _,
                rcache.block,
                rcache.off,
                rcache.buffer,
                rcache.size,
            );
            crate::lfs_assert!(err <= 0);
            if err != 0 {
                crate::lfs_trace!("bd_read block={} -> CORRUPT", rcache.block);
                // Don't leave rcache claiming to have this block when the buffer wasn't filled.
                // A retry (e.g. after bad-block clear) would otherwise serve stale data.
                rcache.block = crate::types::LFS_BLOCK_NULL;
                return crate::lfs_pass_err!(err);
            }
        }

        0
    }
}

/// Per lfs.c lfs_bd_cmp (lines 128-154)
///
/// C:
/// ```c
/// static int lfs_bd_cmp(lfs_t *lfs,
///         const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
///         lfs_block_t block, lfs_off_t off,
///         const void *buffer, lfs_size_t size) {
///     const uint8_t *data = buffer;
///     lfs_size_t diff = 0;
///
///     for (lfs_off_t i = 0; i < size; i += diff) {
///         uint8_t dat[8];
///
///         diff = lfs_min(size-i, sizeof(dat));
///         int err = lfs_bd_read(lfs,
///                 pcache, rcache, hint-i,
///                 block, off+i, &dat, diff);
///         if (err) {
///             return err;
///         }
///
///         int res = memcmp(dat, data + i, diff);
///         if (res) {
///             return res < 0 ? LFS_CMP_LT : LFS_CMP_GT;
///         }
///     }
///
///     return LFS_CMP_EQ;
/// }
/// ```
pub fn lfs_bd_cmp(
    lfs: *mut Lfs,
    pcache: *const LfsCache,
    rcache: *mut LfsCache,
    hint: lfs_size_t,
    block: lfs_block_t,
    off: lfs_off_t,
    buffer: *const u8,
    size: lfs_size_t,
) -> i32 {
    // Per lfs.c enum: LFS_CMP_EQ=0, LFS_CMP_LT=1, LFS_CMP_GT=2 (positive = not error)
    const LFS_CMP_EQ: i32 = 0;
    const LFS_CMP_LT: i32 = 1;
    const LFS_CMP_GT: i32 = 2;

    let mut i: lfs_off_t = 0;
    while i < size {
        let mut dat = [0u8; 8];
        let diff = lfs_min(size - i, 8) as usize;
        let err = lfs_bd_read(
            lfs,
            pcache,
            rcache,
            hint.saturating_sub(i),
            block,
            off + i,
            dat.as_mut_ptr(),
            diff as lfs_size_t,
        );
        if err != 0 {
            return crate::lfs_pass_err!(err);
        }
        let res = unsafe {
            let disk = &dat[..diff];
            let expected = core::slice::from_raw_parts(buffer.add(i as usize), diff);
            disk.cmp(expected)
        };
        match res {
            core::cmp::Ordering::Equal => {}
            core::cmp::Ordering::Less => return LFS_CMP_LT,
            core::cmp::Ordering::Greater => return LFS_CMP_GT,
        }
        i += diff as lfs_off_t;
    }
    LFS_CMP_EQ
}

/// Per lfs.c lfs_bd_crc (lines 155-175)
///
/// C:
/// ```c
/// static int lfs_bd_crc(lfs_t *lfs,
///         const lfs_cache_t *pcache, lfs_cache_t *rcache, lfs_size_t hint,
///         lfs_block_t block, lfs_off_t off, lfs_size_t size, uint32_t *crc) {
///     lfs_size_t diff = 0;
///
///     for (lfs_off_t i = 0; i < size; i += diff) {
///         uint8_t dat[8];
///         diff = lfs_min(size-i, sizeof(dat));
///         int err = lfs_bd_read(lfs,
///                 pcache, rcache, hint-i,
///                 block, off+i, &dat, diff);
///         if (err) {
///             return err;
///         }
///
///         *crc = lfs_crc(*crc, &dat, diff);
///     }
///
///     return 0;
/// }
/// ```
pub fn lfs_bd_crc(
    lfs: *mut Lfs,
    pcache: *const LfsCache,
    rcache: *mut LfsCache,
    hint: lfs_size_t,
    block: lfs_block_t,
    off: lfs_off_t,
    size: lfs_size_t,
    crc: *mut u32,
) -> i32 {
    use crate::crc::lfs_crc;
    use crate::util::lfs_min;

    let mut i: lfs_off_t = 0;
    while i < size {
        let mut dat = [0u8; 8];
        let diff = lfs_min(size - i, 8) as usize;
        let err = lfs_bd_read(
            lfs,
            pcache,
            rcache,
            hint.saturating_sub(i),
            block,
            off + i,
            dat.as_mut_ptr(),
            diff as lfs_size_t,
        );
        if err != 0 {
            return crate::lfs_pass_err!(err);
        }
        unsafe {
            *crc = lfs_crc(*crc, dat.as_ptr(), diff);
        }
        i += diff as lfs_off_t;
    }
    0
}

/// Per lfs.c lfs_bd_flush (lines 177-210)
///
/// C:
/// ```c
/// #ifndef LFS_READONLY
/// static int lfs_bd_flush(lfs_t *lfs,
///         lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate) {
///     if (pcache->block != LFS_BLOCK_NULL && pcache->block != LFS_BLOCK_INLINE) {
///         LFS_ASSERT(pcache->block < lfs->block_count);
///         lfs_size_t diff = lfs_alignup(pcache->size, lfs->cfg->prog_size);
///         int err = lfs->cfg->prog(lfs->cfg, pcache->block,
///                 pcache->off, pcache->buffer, diff);
///         LFS_ASSERT(err <= 0);
///         if (err) {
///             return err;
///         }
///
///         if (validate) {
///             // check data on disk
///             lfs_cache_drop(lfs, rcache);
///             int res = lfs_bd_cmp(lfs,
///                     NULL, rcache, diff,
///                     pcache->block, pcache->off, pcache->buffer, diff);
///             if (res < 0) {
///                 return res;
///             }
///
///             if (res != LFS_CMP_EQ) {
///                 return LFS_ERR_CORRUPT;
///             }
///         }
///
///         lfs_cache_zero(lfs, pcache);
///     }
///
///     return 0;
/// }
/// #endif
/// ```
pub fn lfs_bd_flush(
    lfs: *const Lfs,
    pcache: *mut LfsCache,
    rcache: *mut LfsCache,
    validate: bool,
) -> i32 {
    use crate::types::LFS_BLOCK_INLINE;
    use crate::util::lfs_alignup;

    unsafe {
        let lfs_ptr = lfs;
        let lfs = &*lfs;
        let pcache = &mut *pcache;
        let cfg = &*lfs.cfg;

        if pcache.block != crate::types::LFS_BLOCK_NULL && pcache.block != LFS_BLOCK_INLINE {
            crate::lfs_assert!(pcache.block < lfs.block_count);
            let diff = lfs_alignup(pcache.size, cfg.prog_size);
            crate::lfs_trace!(
                "bd_prog block={} off={} size={}",
                pcache.block,
                pcache.off,
                diff
            );
            let prog = match cfg.prog {
                Some(f) => f,
                None => return LFS_ERR_CORRUPT,
            };
            let err = prog(
                cfg as *const _,
                pcache.block,
                pcache.off,
                pcache.buffer,
                diff,
            );
            crate::lfs_assert!(err <= 0);
            if err != 0 {
                crate::lfs_trace!("bd_prog block={} -> CORRUPT", pcache.block);
                return crate::lfs_pass_err!(err);
            }

            if validate {
                lfs_cache_drop(lfs, rcache);
                let res = lfs_bd_cmp(
                    lfs_ptr as *mut Lfs,
                    core::ptr::null(),
                    rcache,
                    diff,
                    pcache.block,
                    pcache.off,
                    pcache.buffer,
                    diff,
                );
                if res < 0 {
                    return res;
                }
                if res != 0 {
                    return crate::lfs_err!(LFS_ERR_CORRUPT);
                }
            }

            lfs_cache_zero(lfs, pcache);
        }

        0
    }
}

/// Per lfs.c lfs_bd_sync (lines 213-226)
///
/// C:
/// ```c
/// #ifndef LFS_READONLY
/// static int lfs_bd_sync(lfs_t *lfs,
///         lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate) {
///     lfs_cache_drop(lfs, rcache);
///
///     int err = lfs_bd_flush(lfs, pcache, rcache, validate);
///     if (err) {
///         return err;
///     }
///
///     err = lfs->cfg->sync(lfs->cfg);
///     LFS_ASSERT(err <= 0);
///     return err;
/// }
/// #endif
/// ```
pub fn lfs_bd_sync(
    lfs: *const Lfs,
    pcache: *mut LfsCache,
    rcache: *mut LfsCache,
    validate: bool,
) -> i32 {
    unsafe {
        lfs_cache_drop(lfs, rcache);

        let err = lfs_bd_flush(lfs, pcache, rcache, validate);
        if err != 0 {
            return crate::lfs_pass_err!(err);
        }

        let cfg = &*(*lfs).cfg;
        let sync = match cfg.sync {
            Some(f) => f,
            None => return LFS_ERR_CORRUPT,
        };
        let err = sync(cfg as *const _);
        crate::lfs_assert!(err <= 0);
        err
    }
}

/// Per lfs.c lfs_bd_prog (lines 228-274)
///
/// C:
/// ```c
/// #ifndef LFS_READONLY
/// static int lfs_bd_prog(lfs_t *lfs,
///         lfs_cache_t *pcache, lfs_cache_t *rcache, bool validate,
///         lfs_block_t block, lfs_off_t off,
///         const void *buffer, lfs_size_t size) {
///     const uint8_t *data = buffer;
///     LFS_ASSERT(block == LFS_BLOCK_INLINE || block < lfs->block_count);
///     LFS_ASSERT(off + size <= lfs->cfg->block_size);
///
///     while (size > 0) {
///         if (block == pcache->block &&
///                 off >= pcache->off &&
///                 off < pcache->off + lfs->cfg->cache_size) {
///             // already fits in pcache?
///             lfs_size_t diff = lfs_min(size,
///                     lfs->cfg->cache_size - (off-pcache->off));
///             memcpy(&pcache->buffer[off-pcache->off], data, diff);
///
///             data += diff;
///             off += diff;
///             size -= diff;
///
///             pcache->size = lfs_max(pcache->size, off - pcache->off);
///             if (pcache->size == lfs->cfg->cache_size) {
///                 // eagerly flush out pcache if we fill up
///                 int err = lfs_bd_flush(lfs, pcache, rcache, validate);
///                 if (err) {
///                     return err;
///                 }
///             }
///
///             continue;
///         }
///
///         // pcache must have been flushed, either by programming and
///         // entire block or manually flushing the pcache
///         LFS_ASSERT(pcache->block == LFS_BLOCK_NULL);
///
///         // prepare pcache, first condition can no longer fail
///         pcache->block = block;
///         pcache->off = lfs_aligndown(off, lfs->cfg->prog_size);
///         pcache->size = 0;
///     }
///
///     return 0;
/// }
/// #endif
/// ```
pub fn lfs_bd_prog(
    lfs: *const Lfs,
    pcache: *mut LfsCache,
    rcache: *mut LfsCache,
    validate: bool,
    block: lfs_block_t,
    off: lfs_off_t,
    buffer: *const u8,
    size: lfs_size_t,
) -> i32 {
    use crate::types::LFS_BLOCK_INLINE;
    use crate::util::{lfs_aligndown, lfs_max, lfs_min};

    unsafe {
        let lfs = &*lfs;
        let cfg = &*lfs.cfg;
        let pcache = &mut *pcache;

        crate::lfs_assert!(block == LFS_BLOCK_INLINE || block < lfs.block_count);
        crate::lfs_assert!(off + size <= cfg.block_size);

        let mut data = buffer;
        let mut off = off;
        let mut size = size;

        while size > 0 {
            if block == pcache.block && off >= pcache.off && off < pcache.off + cfg.cache_size {
                let diff = lfs_min(size, cfg.cache_size - (off - pcache.off));
                if !pcache.buffer.is_null() && !data.is_null() {
                    // Trace superblock magic region (offset 12-20 in block 0/1)
                    if (block == 0 || block == 1) && off <= 12 && off + diff > 12 {
                        let magic_start = 12usize.saturating_sub(off as usize);
                        let magic_len = (8).min(diff as usize - magic_start);
                        if magic_len > 0 {
                            let slice =
                                core::slice::from_raw_parts(data.add(magic_start), magic_len);
                            crate::lfs_trace!(
                                "bd_prog superblock block={} off={} size={} magic_region[{}..{}]={:?}",
                                block,
                                off,
                                size,
                                magic_start,
                                magic_start + magic_len,
                                slice
                            );
                        }
                    }
                    core::ptr::copy_nonoverlapping(
                        data,
                        pcache.buffer.add((off - pcache.off) as usize),
                        diff as usize,
                    );
                }

                data = data.add(diff as usize);
                off += diff;
                size -= diff;

                pcache.size = lfs_max(pcache.size, off - pcache.off);
                if pcache.size == cfg.cache_size {
                    let err = lfs_bd_flush(lfs, pcache, rcache, validate);
                    if err != 0 {
                        return crate::lfs_pass_err!(err);
                    }
                }

                continue;
            }

            crate::lfs_assert!(pcache.block == crate::types::LFS_BLOCK_NULL);

            pcache.block = block;
            pcache.off = lfs_aligndown(off, cfg.prog_size);
            pcache.size = 0;
        }

        0
    }
}

/// Per lfs.c lfs_bd_erase (lines 277-282)
///
/// C:
/// ```c
/// #ifndef LFS_READONLY
/// static int lfs_bd_erase(lfs_t *lfs, lfs_block_t block) {
///     LFS_ASSERT(block < lfs->block_count);
///     int err = lfs->cfg->erase(lfs->cfg, block);
///     LFS_ASSERT(err <= 0);
///     return err;
/// }
/// #endif
/// ```
pub fn lfs_bd_erase(lfs: *const Lfs, block: lfs_block_t) -> i32 {
    unsafe {
        let lfs = &*lfs;
        crate::lfs_assert!(block < lfs.block_count);
        let erase = match (*lfs.cfg).erase {
            Some(f) => f,
            None => return LFS_ERR_CORRUPT,
        };
        crate::lfs_trace!("bd_erase block={}", block);
        let err = erase(lfs.cfg, block);
        crate::lfs_assert!(err <= 0);
        if err != 0 {
            crate::lfs_trace!("bd_erase block={} -> CORRUPT", block);
        }
        err
    }
}