• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# LittleFS
2
3## Basic Concepts
4
5LittleFS is a small file system designed for flash. By combining the log-structured file system and the copy-on-write \(COW\) file system, LittleFS stores metadata in log structure and data in the COW structure. This special storage empowers LittleFS high power-loss resilience. LittleFS uses the statistical wear leveling algorithm when allocating COW data blocks, effectively prolonging the service life of flash devices. LittleFS is designed for small-sized devices with limited resources, such as ROM and RAM. All RAM resources are allocated through a buffer with the fixed size \(configurable\). That is, the RAM usage does not grow with the file system.
6
7LittleFS is a good choice when you look for a flash file system that is power-cut resilient and has wear leveling support on a small device with limited resources.
8
9## Development Guidelines
10
11When porting LittleFS to a new hardware device, you need to declare  **lfs\_config**:
12
13```
14const struct lfs_config cfg = {
15    // block device operations
16    .read  = user_provided_block_device_read,
17    .prog  = user_provided_block_device_prog,
18    .erase = user_provided_block_device_erase,
19    .sync  = user_provided_block_device_sync,
20
21    // block device configuration
22    .read_size = 16,
23    .prog_size = 16,
24    .block_size = 4096,
25    .block_count = 128,
26    .cache_size = 16,
27    .lookahead_size = 16,
28    .block_cycles = 500,
29};
30```
31
32**.read**,  **.prog**,  **.erase**, and  **.sync**  correspond to the read, write, erase, and synchronization APIs at the bottom layer of the hardware platform, respectively.
33
34**read\_size**  indicates the number of bytes read each time. You can set it to a value greater than the physical read unit to improve performance. This value determines the size of the read cache. However, if the value is too large, more memory is consumed.
35
36**prog\_size**  indicates the number of bytes written each time. You can set it to a value greater than the physical write unit to improve performance. This value determines the size of the write cache and must be an integral multiple of  **read\_size**. However, if the value is too large, more memory is consumed.
37
38**block\_size**: indicates the number of bytes in each erase block. The value can be greater than that of the physical erase unit. However, a smaller value is recommended because each file occupies at least one block. The value must be an integral multiple of  **prog\_size**.
39
40**block\_count**  indicates the number of blocks that can be erased, which depends on the capacity of the block device and the size of the block to be erased \(**block\_size**\).
41
42## Sample Code
43
44The sample code is as follows:
45
46```
47#include "lfs.h"
48#include "stdio.h"
49lfs_t lfs;
50lfs_file_t file;
51const struct lfs_config cfg = {
52    // block device operations
53    .read  = user_provided_block_device_read,
54    .prog  = user_provided_block_device_prog,
55    .erase = user_provided_block_device_erase,
56    .sync  = user_provided_block_device_sync,
57    // block device configuration
58    .read_size = 16,
59    .prog_size = 16,
60    .block_size = 4096,
61    .block_count = 128,
62    .cache_size = 16,
63    .lookahead_size = 16,
64    .block_cycles = 500,
65};
66int main(void) {
67    // mount the filesystem
68    int err = lfs_mount(&lfs, &cfg);
69    // reformat if we can't mount the filesystem
70    // this should only happen on the first boot
71    if (err) {
72        lfs_format(&lfs, &cfg);
73        lfs_mount(&lfs, &cfg);
74    }
75    // read current count
76    uint32_t boot_count = 0;
77    lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
78    lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
79    // update boot count
80    boot_count += 1;
81    lfs_file_rewind(&lfs, &file);
82    lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
83    // remember the storage is not updated until the file is closed successfully
84    lfs_file_close(&lfs, &file);
85    // release any resources we were using
86    lfs_unmount(&lfs);
87    // print the boot count
88    printf("boot_count: %d\n", boot_count);
89}
90```
91
92### Verification
93
94The development is successful if the return result is as follows:
95
96```
97Say hello 1 times.
98```
99
100