1、littlefs主要用在微控制器和flash上,是一種嵌入式文件系統。主要有3個特點:
1)、掉電恢復
在寫入時即使復位或者掉電也可以恢復到上一個正確的狀態。
2)、擦寫均衡
有效延長flash的使用壽命
3)、有限的RAM/ROM
節省ROM和RAM空間
2、已有的文件系統
1)非掉電恢復,基於block的文件系統,常見的有FAT和EXT2。這兩個文件系統在寫入文件時是原地更新的,不具備非掉電恢復的特性。
2) 日志式的文件系統,比如JFFS,YAFFS等,具備掉電恢復的特性。但是這幾個系統消耗了太多的RAM,且性能較低。
3) EXT4和COW類型的btrfs具有良好的恢復性和讀寫性能,但是需要的資源過多,不適合小型的嵌入式系統。
littlefs綜合了日志式文件系統和COW文件系統的優點。從sub-block的角度來看,littlefs是基於日志的文件系統,提供了metadata的原子更新;從super-block的角度,littlefs是基於block的COW樹。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
移植LittleFs
MT2503的板子外掛8Mbit的SPI Flash,本打算移植Fatfs,但Fatfs並不支持Wear leveling,后發現LittleFs,一個專門為單片機設計的文件系統,並且支持fail-safe。
詳細介紹:https://os.mbed.com/blog/entry/littlefs-high-integrity-embedded-fs/
LittleFs Github https://github.com/ARMmbed/littlefs
LittleFs的移植很簡單,只需實現Flash的讀、寫和擦除基本操作即可,官方例子如下:
-
#include "lfs.h"
-
-
// variables used by the filesystem
-
lfs_t lfs;
-
lfs_file_t file;
-
-
// configuration of the filesystem is provided by this struct
-
const struct lfs_config cfg = {
-
// block device operations
-
.read = user_provided_block_device_read,
-
.prog = user_provided_block_device_prog,
-
.erase = user_provided_block_device_erase,
-
.sync = user_provided_block_device_sync,
-
-
// block device configuration
-
.read_size = 16,
-
.prog_size = 16,
-
.block_size = 4096,
-
.block_count = 128,
-
.lookahead = 128,
-
};
-
-
// entry point
-
int main(void) {
-
// mount the filesystem
-
int err = lfs_mount( &lfs, &cfg);
-
-
// reformat if we can't mount the filesystem
-
// this should only happen on the first boot
-
if (err) {
-
lfs_format( &lfs, &cfg);
-
lfs_mount( &lfs, &cfg);
-
}
-
-
// read current count
-
uint32_t boot_count = 0;
-
lfs_file_open( &lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
-
lfs_file_read( &lfs, &file, &boot_count, sizeof(boot_count));
-
-
// update boot count
-
boot_count += 1;
-
lfs_file_rewind( &lfs, &file);
-
lfs_file_write( &lfs, &file, &boot_count, sizeof(boot_count));
-
-
// remember the storage is not updated until the file is closed successfully
-
lfs_file_close( &lfs, &file);
-
-
// release any resources we were using
-
lfs_unmount( &lfs);
-
-
// print the boot count
-
printf("boot_count: %d\n", boot_count);
-
}
已經實現的Flash驅動聲明:
-
void flash_read_block(kal_uint32 addr, kal_uint8 *buff, kal_uint32 len);
-
void flash_write_block(kal_uint32 addr, kal_uint8 *buff, kal_uint32 len);
-
void flash_erase_block(kal_uint32 addr);
操作接口配置:
-
static int _block_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
-
{
-
flash_read_block(block * c->block_size + off, (kal_uint8 *)buffer, (kal_uint32)size);
-
return 0;
-
}
-
static int _block_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
-
{
-
flash_write_block(block * c->block_size + off, (kal_uint8 *)buffer, (kal_uint32)size);
-
return 0;
-
}
-
static int _block_erase(const struct lfs_config *c, lfs_block_t block)
-
{
-
flash_erase_block(block * c->block_size);
-
return 0;
-
}
-
static int _block_sync(const struct lfs_config *c)
-
{
-
return 0;
-
}
-
-
const struct lfs_config cfg = {
-
// block device operations
-
.read = _block_read,
-
.prog = _block_prog,
-
.erase = _block_erase,
-
.sync = _block_sync,
-
-
// block device configuration
-
.read_size = 16,
-
.prog_size = 16,
-
.block_size = 4096,
-
.block_count = 256,
-
.lookahead = 256,
-
};
編譯時出現大量Error,經過折騰最終搞定。
總結了一下:
1. armcc默認不支持對結構體指定成員名稱進行初始化、不支持可執行代碼之后聲明變量,CFLAG添加 –gnu 可以解決問題,添加方法請參考《MTK功能機MMI,添加GNU特性》;
2. LittleFs需要提供系統的malloc, free函數接口。
原文地址:http://www.noblock.cn/?p=183
-------------------------------------------------------------------------------------------------------------------------------------------------------------
littlefs 重要的數據結構
1、文件類型
-
// File types
-
enum lfs_type {
-
// file types
-
LFS_TYPE_REG = 0x001,
-
LFS_TYPE_DIR = 0x002,
-
// internally used types
-
LFS_TYPE_SPLICE = 0x400,
-
LFS_TYPE_NAME = 0x000,
-
LFS_TYPE_STRUCT = 0x200,
-
LFS_TYPE_USERATTR = 0x300,
-
LFS_TYPE_FROM = 0x100,
-
LFS_TYPE_TAIL = 0x600,
-
LFS_TYPE_GLOBALS = 0x700,
-
LFS_TYPE_CRC = 0x500,
-
// internally used type specializations
-
LFS_TYPE_CREATE = 0x401,
-
LFS_TYPE_DELETE = 0x4ff,
-
LFS_TYPE_SUPERBLOCK = 0x0ff,
-
LFS_TYPE_DIRSTRUCT = 0x200,
-
LFS_TYPE_CTZSTRUCT = 0x202,
-
LFS_TYPE_INLINESTRUCT = 0x201,
-
LFS_TYPE_SOFTTAIL = 0x600,
-
LFS_TYPE_HARDTAIL = 0x601,
-
LFS_TYPE_MOVESTATE = 0x7ff,
-
// internal chip sources
-
LFS_FROM_NOOP = 0x000,
-
LFS_FROM_MOVE = 0x101,
-
LFS_FROM_USERATTRS = 0x102,
-
};
2、文件打開時的標志
-
// File open flags
-
enum lfs_open_flags {
-
// open flags
-
LFS_O_RDONLY = 1, // Open a file as read only
-
LFS_O_WRONLY = 2, // Open a file as write only
-
LFS_O_RDWR = 3, // Open a file as read and write
-
LFS_O_CREAT = 0x0100, // Create a file if it does not exist
-
LFS_O_EXCL = 0x0200, // Fail if a file already exists
-
LFS_O_TRUNC = 0x0400, // Truncate the existing file to zero size
-
LFS_O_APPEND = 0x0800, // Move to end of file on every write
-
// internally used flags
-
LFS_F_DIRTY = 0x010000, // File does not match storage
-
LFS_F_WRITING = 0x020000, // File has been written since last flush
-
LFS_F_READING = 0x040000, // File has been read since last flush
-
LFS_F_ERRED = 0x080000, // An error occured during write
-
LFS_F_INLINE = 0x100000, // Currently inlined in directory entry
-
LFS_F_OPENED = 0x200000, // File has been opened
-
};
3、文件seek時的標志
-
// File seek flags
-
enum lfs_whence_flags {
-
LFS_SEEK_SET = 0, // Seek relative to an absolute position
-
LFS_SEEK_CUR = 1, // Seek relative to the current file position
-
LFS_SEEK_END = 2, // Seek relative to the end of the file
-
};
4、lfs的配置參數
-
// Configuration provided during initialization of the littlefs
-
struct lfs_config {
-
// Opaque user provided context that can be used to pass
-
// information to the block device operations
-
/* 這個參數主要是傳遞給block驅動代碼 */
-
void *context;
-
/* 從設備讀數據 */
-
int (*read)(const struct lfs_config *c, lfs_block_t block,
-
lfs_off_t off, void *buffer, lfs_size_t size);
-
/* 向設備寫入數據,block設備在寫入前必須已經erase了 */
-
int (*prog)(const struct lfs_config *c, lfs_block_t block,
-
lfs_off_t off, const void *buffer, lfs_size_t size);
-
/* 擦除block */
-
int (*erase)(const struct lfs_config *c, lfs_block_t block);
-
/* sync塊設備的狀態 */
-
int (*sync)(const struct lfs_config *c);
-
/* 最小的讀取單元大小 */
-
lfs_size_t read_size;
-
/* 最小的寫入數據單元大小,也是數據metadata pair中tag的對齊尺寸 */
-
lfs_size_t prog_size;
-
/* 最小的擦除單元大小。可以比flash的實際block尺寸大。但是對於ctz類型的文件,block size是最小的分配單元。同時block size必須是
-
read size和program size的倍數,block size會存儲在superblock中 */
-
lfs_size_t block_size;
-
/* 屬於文件系統的block數量,block count會存儲在superblock中 */
-
lfs_size_t block_count;
-
/* 文件系統進行垃圾回收時的block的擦除次數,推薦取值100-1000.值越大垃圾回收的次數越少,性能越好 */
-
int32_t block_cycles;
-
/* littlefs需要一個read cache,一個program cache,每個文件也需要一個cache。cache越大性能越好,會減少會flash的訪問次數,
-
cache必須是block的read size和program size的倍數,同時是block size的因數 */
-
lfs_size_t cache_size;
-
/* lookahead buffer的尺寸。lookahead buffer主要是block alloctor在分配塊的時候用到。lookahead size必須是8的倍數,
-
因為它是采用bitmap的形式存儲的 */
-
lfs_size_t lookahead_size;
-
/* cache size大小的read buffer,可以靜態分配也可以動態分配 */
-
void *read_buffer;
-
/* cache size大小的program buffer,可以靜態分配也可以動態分配 */
-
void *prog_buffer;
-
/* lookahead_size大小的lookahead buffer,且是32-bit對齊的,即可以靜態分配也可以動態分配 */
-
void *lookahead_buffer;
-
/* 文件名的最大長度,這個值會存儲在superblock中 */
-
lfs_size_t name_max;
-
/* 文件的最大長度,存儲在superblock中 */
-
lfs_size_t file_max;
-
/* 用戶屬性的最大長度 */
-
lfs_size_t attr_max;
-
};
5、文件信息
-
// File info structure
-
struct lfs_info {
-
// Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR 普通文件或者目錄
-
uint8_t type;
-
// Size of the file, only valid for REG files. Limited to 32-bits. 對於普通文件才有意義
-
lfs_size_t size;
-
// Name of the file stored as a null-terminated string. Limited to
-
// LFS_NAME_MAX+1, which can be changed by redefining LFS_NAME_MAX to
-
// reduce RAM. LFS_NAME_MAX is stored in superblock and must be
-
// respected by other littlefs drivers.
-
/* 字符串形式的文件名 */
-
char name[LFS_NAME_MAX+1];
-
};
6、用戶屬性
-
struct lfs_attr {
-
/* 屬性類型 */
-
uint8_t type;
-
/* 存儲屬性的buffer */
-
void *buffer;
-
/* 屬性的長度,最大值為LFS_ATTR_MAX */
-
lfs_size_t size;
-
};
7、文件open時的配置
-
struct lfs_file_config {
-
/* cache size長度的buffer,可以靜態分配也可以動態分配 */
-
void *buffer;
-
/* 用戶屬性,讀文件時,attr存儲從flash上讀取的文件用戶屬性,寫入文件時,attr存放用戶指定的文件屬性並會寫入到flash中 */
-
struct lfs_attr *attrs;
-
/* 用戶屬性的長度 */
-
lfs_size_t attr_count;
-
};
8、lfs_cache結構
-
typedef struct lfs_cache {
-
lfs_block_t block; // cache中的數據屬於的block
-
lfs_off_t off; // cache中的數據在block上的偏移地址
-
lfs_size_t size; // cache的大小
-
uint8_t *buffer; // cache數據的存放地址
-
} lfs_cache_t;
9、lfs_mdir結構,代表metadata pair,dir本身所在的block
-
typedef struct lfs_mdir {
-
lfs_block_t pair[2]; // dir的metadata pair所在的block
-
uint32_t rev; // metadata pair的revision
-
lfs_off_t off; // tag的偏移地址
-
uint32_t etag;
-
uint16_t count;
-
bool erased;
-
bool split; // metadata pair是否是鏈表
-
lfs_block_t tail[2]; // 用於metadata pair鏈表
-
} lfs_mdir_t;
10、lfs目錄結構
-
// littlefs directory type
-
typedef struct lfs_dir {
-
struct lfs_dir *next; // 指向子目錄
-
uint16_t id;
-
uint8_t type; // LFS_TYPE_DIR
-
lfs_mdir_t m; // 代表dir的metadata pair
-
lfs_off_t pos; // 在目錄中的當前位置,主要用在seek,tell和rewind操作中
-
lfs_block_t head[2];
-
} lfs_dir_t;
11、lfs文件類型
-
// littlefs file type
-
typedef struct lfs_file {
-
struct lfs_file *next;
-
uint16_t id; // metadata tag中的id,在文件open時獲取
-
uint8_t type; // LFS_TYPE_REG 或者 LFS_TYPE_DIR
-
lfs_mdir_t m; // 文件所在的目錄的metadata pair
-
struct lfs_ctz {
-
lfs_block_t head;
-
lfs_size_t size;
-
} ctz; // 指向大文件的CTZ skip-list。對於小文件則直接inline了,無需CTZ skip-list
-
uint32_t flags; // lfs_open_flags中的值
-
lfs_off_t pos; // 文件訪問時的偏移
-
lfs_block_t block; // file當前的block
-
lfs_off_t off; // 在block內的offset
-
lfs_cache_t cache; // 文件訪問時的cache
-
const struct lfs_file_config *cfg; // 文件open時的配置參數,包含一個buffer以及用戶屬性
-
} lfs_file_t;
12、lfs superblock結構
-
typedef struct lfs_superblock {
-
uint32_t version; // 文件系統的版本號
-
lfs_size_t block_size; // 文件系統的block size,和flash的block size不一定相同
-
lfs_size_t block_count; // 文件系統包含的block數量,每個block的大小等於上面的block size
-
lfs_size_t name_max; // 文件名的最大長度
-
lfs_size_t file_max; // 文件的最大長度
-
lfs_size_t attr_max; // 用戶屬性的最大長度
-
} lfs_superblock_t;
13、lfs文件系統類型結構
-
// The littlefs filesystem type
-
typedef struct lfs {
-
lfs_cache_t rcache; // read cache
-
lfs_cache_t pcache; // program cache
-
lfs_block_t root[2]; // 根目錄所在的block
-
struct lfs_mlist {
-
struct lfs_mlist *next; // 指向下一個節點
-
uint16_t id; // metadata pair的id
-
uint8_t type; // metadata pair的類型
-
lfs_mdir_t m; // metadata pair
-
} *mlist; // metadata pair list
-
uint32_t seed; // block alloctor的隨機數生成的seed
-
struct lfs_gstate {
-
uint32_t tag;
-
lfs_block_t pair[2];
-
} gstate, gpending, gdelta; // 用於目錄操作sync的global state,
-
struct lfs_free {
-
lfs_block_t off; // 記錄lookahead buffer中起始block的偏移
-
lfs_block_t size; // lookahead buffer中block的數量,注意lookahead采用的是bitmap的形式,因此size=8*lookahead_size
-
lfs_block_t i; // lookahead buffer內部的偏移地址
-
lfs_block_t ack; // 剩余block的數量,初始值為block count,如果該值為0,表示已經沒有free block了
-
uint32_t *buffer; // buffer的長度為lookahead size
-
} free; // lookahead buffer,用於分配free block
-
const struct lfs_config *cfg; // 文件系統的配置參數
-
lfs_size_t name_max; // 文件名的最大長度,和superblock中的name_max值相同
-
lfs_size_t file_max; // 文件的最大長度,和superblock中的file_max值相同
-
lfs_size_t attr_max; // 用戶屬性的最大長度,和superblock中的attr_max值相同
-
} lfs_t;
----------------------------------------------------------------------------------------------------------------------------------------------------------------
小型文件系統FatFS和LittleFS對比和區別
對於許多物聯網設備而言,擁有一個小型且具有彈性的文件系統至關重要。
在MCU上運行的文件系統不多,絕大部分人應該知道FatFS這個文件系統,今天就給大家講講FatFS和LittleFS的內容,以及他們之間的一些差異。
一、文件系統FatFS
FatFs是一個通用的文件系統(FAT/exFAT)模塊,用於在小型嵌入式系統中實現FAT文件系統。
網址:
http://elm-chan.org/fsw/ff/00index_e.html
FatFs組件的編寫遵循ANSI C(C89),完全分離於磁盤 I/O 層,因此不依賴於硬件平台。它可以嵌入到資源有限的微控制器中,如 8051, PIC, AVR, ARM, Z80, RX等等,不需要做任何修改。
---來自百度百科
特征
a.DOS/ Windows兼容的FAT/exFAT文件系統。
b.平台無關,容易移植。
c.程序代碼和工作區的占用空間非常小。
d.支持以下各種配置選項:
-
ANSI / OEM或Unicode中的長文件名。
-
exFAT文件系統,64位LBA和GPT可存儲大量數據。
-
RTOS的線程安全。
-
多個卷(物理驅動器和分區)。
-
可變扇區大小。
-
多個代碼頁,包括DBCS。
-
只讀,可選API,I / O緩沖區等...
如果你會使用STM32CubeMX,想要使用FatFS非常容易,輕松幾步就能將STM32“變成”一個U盤。
二、文件系統Littlefs
知道Littlefs文件系統的人相對比較少,但是如果使用過Mbed OS系統的人絕大部分都應該知道。
Mbed OS是Arm公司針對Cortex-M系列處理器,面向IoT開發的一套免費、開源開源嵌入式操作系統,專門為物聯網中的“things”而設計。
而Littlefs只是Mbed其中的一部分內容,如下框圖:
源碼地址:
https://github.com/armmbed/mbed-littlefs
Littlefs特點:
-
占用資源小:物聯網設備受到ROM和RAM的限制。
-
斷電恢復能力:要求文件系統保持一致,並將數據刷新到底層存儲。
-
平均磨損:通常情況下,存儲支持每塊數量有限的擦除,因此使用整個存儲設備對於可靠性非常重要。
用法也挺簡單,參看官方例程:
-
-
-
-
-
// Physical block device, can be any device that supports the BlockDevice API
-
SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5);
-
-
-
// Storage for the littlefs
-
LittleFileSystem2 fs("fs");
-
-
-
// Entry point
-
int main() {
-
// Mount the filesystem
-
int err = fs.mount(&bd);
-
if (err) {
-
// Reformat if we can't mount the filesystem,
-
// this should only happen on the first boot
-
LittleFileSystem2::format(&bd);
-
fs.mount(&bd);
-
}
-
-
-
// Read the boot count
-
uint32_t boot_count = 0;
-
FILE *f = fopen( "/fs/boot_count", "r+");
-
if (!f) {
-
// Create the file if it doesn't exist
-
f = fopen( "/fs/boot_count", "w+");
-
}
-
fread(&boot_count, sizeof(boot_count), 1, f);
-
-
-
// Update the boot count
-
boot_count += 1;
-
rewind(f);
-
fwrite(&boot_count, sizeof(boot_count), 1, f);
-
-
-
// Remember that storage may not be updated until the file
-
// is closed successfully
-
fclose(f);
-
-
-
// Release any resources we were using
-
fs.unmount();
-
-
-
// Print the boot count
-
printf("boot_count: %ld\n", boot_count);
-
}
三、文件系統對比
每一種產物都有它存在的價值,文件系統也同樣如此,各有各的優缺點,下面簡單羅列幾點它們的區別。
1.資源RAM / ROM大小
Littlefs是Mbed OS中的高完整性嵌入式文件系統,經過優化可與RAM和ROM有限的MCU一起使用。
Littlefs高度集成的嵌入式文件系統使用比FAT少的13K ROM和少於4K的RAM。
2.失電恢復能力
littlefs具有強大的copy-on-write保證,並且磁盤上的存儲總是保持有效狀態,可能有隨機電源故障的系統適合該文件系統。
3.磨損均衡
嵌入式設備使用的大多數存儲芯片都支持每個扇區有限的擦除集,如果沒有均衡,則嵌入式設備的壽命可能會受到影響。
參考來源:
https://os.mbed.com/blog/entry/littlefs-high-integrity-embedded-fs/