|
|
@@ -1,14 +1,14 @@
|
|
|
/*
|
|
|
* The little filesystem
|
|
|
*
|
|
|
+ * Copyright (c) 2022, The littlefs authors.
|
|
|
* Copyright (c) 2017, Arm Limited. All rights reserved.
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
*/
|
|
|
#ifndef LFS_H
|
|
|
#define LFS_H
|
|
|
|
|
|
-#include <stdint.h>
|
|
|
-#include <stdbool.h>
|
|
|
+#include "lfs_util.h"
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
extern "C"
|
|
|
@@ -21,14 +21,14 @@ extern "C"
|
|
|
// Software library version
|
|
|
// Major (top-nibble), incremented on backwards incompatible changes
|
|
|
// Minor (bottom-nibble), incremented on feature additions
|
|
|
-#define LFS_VERSION 0x00020001
|
|
|
+#define LFS_VERSION 0x00020007
|
|
|
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
|
|
|
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
|
|
|
|
|
|
// Version of On-disk data structures
|
|
|
// Major (top-nibble), incremented on backwards incompatible changes
|
|
|
// Minor (bottom-nibble), incremented on feature additions
|
|
|
-#define LFS_DISK_VERSION 0x00020000
|
|
|
+#define LFS_DISK_VERSION 0x00020001
|
|
|
#define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
|
|
|
#define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0))
|
|
|
|
|
|
@@ -112,6 +112,8 @@ enum lfs_type {
|
|
|
LFS_TYPE_SOFTTAIL = 0x600,
|
|
|
LFS_TYPE_HARDTAIL = 0x601,
|
|
|
LFS_TYPE_MOVESTATE = 0x7ff,
|
|
|
+ LFS_TYPE_CCRC = 0x500,
|
|
|
+ LFS_TYPE_FCRC = 0x5ff,
|
|
|
|
|
|
// internal chip sources
|
|
|
LFS_FROM_NOOP = 0x000,
|
|
|
@@ -123,20 +125,25 @@ enum lfs_type {
|
|
|
enum lfs_open_flags {
|
|
|
// open flags
|
|
|
LFS_O_RDONLY = 1, // Open a file as read only
|
|
|
+#ifndef LFS_READONLY
|
|
|
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
|
|
|
+#endif
|
|
|
|
|
|
// internally used flags
|
|
|
+#ifndef LFS_READONLY
|
|
|
LFS_F_DIRTY = 0x010000, // File does not match storage
|
|
|
LFS_F_WRITING = 0x020000, // File has been written since last flush
|
|
|
+#endif
|
|
|
LFS_F_READING = 0x040000, // File has been read since last flush
|
|
|
- LFS_F_ERRED = 0x080000, // An error occured during write
|
|
|
+#ifndef LFS_READONLY
|
|
|
+ LFS_F_ERRED = 0x080000, // An error occurred during write
|
|
|
+#endif
|
|
|
LFS_F_INLINE = 0x100000, // Currently inlined in directory entry
|
|
|
- LFS_F_OPENED = 0x200000, // File has been opened
|
|
|
};
|
|
|
|
|
|
// File seek flags
|
|
|
@@ -153,45 +160,55 @@ struct lfs_config {
|
|
|
// information to the block device operations
|
|
|
void *context;
|
|
|
|
|
|
- // Read a region in a block. Negative error codes are propogated
|
|
|
+ // Read a region in a block. Negative error codes are propagated
|
|
|
// to the user.
|
|
|
int (*read)(const struct lfs_config *c, lfs_block_t block,
|
|
|
lfs_off_t off, void *buffer, lfs_size_t size);
|
|
|
|
|
|
// Program a region in a block. The block must have previously
|
|
|
- // been erased. Negative error codes are propogated to the user.
|
|
|
+ // been erased. Negative error codes are propagated to the user.
|
|
|
// May return LFS_ERR_CORRUPT if the block should be considered bad.
|
|
|
int (*prog)(const struct lfs_config *c, lfs_block_t block,
|
|
|
lfs_off_t off, const void *buffer, lfs_size_t size);
|
|
|
|
|
|
// Erase a block. A block must be erased before being programmed.
|
|
|
// The state of an erased block is undefined. Negative error codes
|
|
|
- // are propogated to the user.
|
|
|
+ // are propagated to the user.
|
|
|
// May return LFS_ERR_CORRUPT if the block should be considered bad.
|
|
|
int (*erase)(const struct lfs_config *c, lfs_block_t block);
|
|
|
|
|
|
// Sync the state of the underlying block device. Negative error codes
|
|
|
- // are propogated to the user.
|
|
|
+ // are propagated to the user.
|
|
|
int (*sync)(const struct lfs_config *c);
|
|
|
|
|
|
- // Minimum size of a block read. All read operations will be a
|
|
|
+#ifdef LFS_THREADSAFE
|
|
|
+ // Lock the underlying block device. Negative error codes
|
|
|
+ // are propagated to the user.
|
|
|
+ int (*lock)(const struct lfs_config *c);
|
|
|
+
|
|
|
+ // Unlock the underlying block device. Negative error codes
|
|
|
+ // are propagated to the user.
|
|
|
+ int (*unlock)(const struct lfs_config *c);
|
|
|
+#endif
|
|
|
+
|
|
|
+ // Minimum size of a block read in bytes. All read operations will be a
|
|
|
// multiple of this value.
|
|
|
lfs_size_t read_size;
|
|
|
|
|
|
- // Minimum size of a block program. All program operations will be a
|
|
|
- // multiple of this value.
|
|
|
+ // Minimum size of a block program in bytes. All program operations will be
|
|
|
+ // a multiple of this value.
|
|
|
lfs_size_t prog_size;
|
|
|
|
|
|
- // Size of an erasable block. This does not impact ram consumption and
|
|
|
- // may be larger than the physical erase size. However, non-inlined files
|
|
|
- // take up at minimum one block. Must be a multiple of the read
|
|
|
- // and program sizes.
|
|
|
+ // Size of an erasable block in bytes. This does not impact ram consumption
|
|
|
+ // and may be larger than the physical erase size. However, non-inlined
|
|
|
+ // files take up at minimum one block. Must be a multiple of the read and
|
|
|
+ // program sizes.
|
|
|
lfs_size_t block_size;
|
|
|
|
|
|
// Number of erasable blocks on the device.
|
|
|
lfs_size_t block_count;
|
|
|
|
|
|
- // Number of erase cycles before littlefs evicts metadata logs and moves
|
|
|
+ // Number of erase cycles before littlefs evicts metadata logs and moves
|
|
|
// the metadata to another block. Suggested values are in the
|
|
|
// range 100-1000, with large values having better performance at the cost
|
|
|
// of less consistent wear distribution.
|
|
|
@@ -199,11 +216,11 @@ struct lfs_config {
|
|
|
// Set to -1 to disable block-level wear-leveling.
|
|
|
int32_t block_cycles;
|
|
|
|
|
|
- // Size of block caches. Each cache buffers a portion of a block in RAM.
|
|
|
- // The littlefs needs a read cache, a program cache, and one additional
|
|
|
+ // Size of block caches in bytes. Each cache buffers a portion of a block in
|
|
|
+ // RAM. The littlefs needs a read cache, a program cache, and one additional
|
|
|
// cache per file. Larger caches can improve performance by storing more
|
|
|
- // data and reducing the number of disk accesses. Must be a multiple of
|
|
|
- // the read and program sizes, and a factor of the block size.
|
|
|
+ // data and reducing the number of disk accesses. Must be a multiple of the
|
|
|
+ // read and program sizes, and a factor of the block size.
|
|
|
lfs_size_t cache_size;
|
|
|
|
|
|
// Size of the lookahead buffer in bytes. A larger lookahead buffer
|
|
|
@@ -240,6 +257,20 @@ struct lfs_config {
|
|
|
// larger attributes size but must be <= LFS_ATTR_MAX. Defaults to
|
|
|
// LFS_ATTR_MAX when zero.
|
|
|
lfs_size_t attr_max;
|
|
|
+
|
|
|
+ // Optional upper limit on total space given to metadata pairs in bytes. On
|
|
|
+ // devices with large blocks (e.g. 128kB) setting this to a low size (2-8kB)
|
|
|
+ // can help bound the metadata compaction time. Must be <= block_size.
|
|
|
+ // Defaults to block_size when zero.
|
|
|
+ lfs_size_t metadata_max;
|
|
|
+
|
|
|
+#ifdef LFS_MULTIVERSION
|
|
|
+ // On-disk version to use when writing in the form of 16-bit major version
|
|
|
+ // + 16-bit minor version. This limiting metadata to what is supported by
|
|
|
+ // older minor versions. Note that some features will be lost. Defaults to
|
|
|
+ // to the most recent minor version when zero.
|
|
|
+ uint32_t disk_version;
|
|
|
+#endif
|
|
|
};
|
|
|
|
|
|
// File info structure
|
|
|
@@ -257,6 +288,21 @@ struct lfs_info {
|
|
|
char name[LFS_NAME_MAX+1];
|
|
|
};
|
|
|
|
|
|
+// Filesystem info structure
|
|
|
+struct lfs_fsinfo {
|
|
|
+ // On-disk version.
|
|
|
+ uint32_t disk_version;
|
|
|
+
|
|
|
+ // Upper limit on the length of file names in bytes.
|
|
|
+ lfs_size_t name_max;
|
|
|
+
|
|
|
+ // Upper limit on the size of files in bytes.
|
|
|
+ lfs_size_t file_max;
|
|
|
+
|
|
|
+ // Upper limit on the size of custom attributes in bytes.
|
|
|
+ lfs_size_t attr_max;
|
|
|
+};
|
|
|
+
|
|
|
// Custom attribute structure, used to describe custom attributes
|
|
|
// committed atomically during file writes.
|
|
|
struct lfs_attr {
|
|
|
@@ -355,6 +401,11 @@ typedef struct lfs_superblock {
|
|
|
lfs_size_t attr_max;
|
|
|
} lfs_superblock_t;
|
|
|
|
|
|
+typedef struct lfs_gstate {
|
|
|
+ uint32_t tag;
|
|
|
+ lfs_block_t pair[2];
|
|
|
+} lfs_gstate_t;
|
|
|
+
|
|
|
// The littlefs filesystem type
|
|
|
typedef struct lfs {
|
|
|
lfs_cache_t rcache;
|
|
|
@@ -369,10 +420,9 @@ typedef struct lfs {
|
|
|
} *mlist;
|
|
|
uint32_t seed;
|
|
|
|
|
|
- struct lfs_gstate {
|
|
|
- uint32_t tag;
|
|
|
- lfs_block_t pair[2];
|
|
|
- } gstate, gpending, gdelta;
|
|
|
+ lfs_gstate_t gstate;
|
|
|
+ lfs_gstate_t gdisk;
|
|
|
+ lfs_gstate_t gdelta;
|
|
|
|
|
|
struct lfs_free {
|
|
|
lfs_block_t off;
|
|
|
@@ -395,6 +445,7 @@ typedef struct lfs {
|
|
|
|
|
|
/// Filesystem functions ///
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Format a block device with the littlefs
|
|
|
//
|
|
|
// Requires a littlefs object and config struct. This clobbers the littlefs
|
|
|
@@ -403,6 +454,7 @@ typedef struct lfs {
|
|
|
//
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_format(lfs_t *lfs, const struct lfs_config *config);
|
|
|
+#endif
|
|
|
|
|
|
// Mounts a littlefs
|
|
|
//
|
|
|
@@ -422,12 +474,15 @@ int lfs_unmount(lfs_t *lfs);
|
|
|
|
|
|
/// General operations ///
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Removes a file or directory
|
|
|
//
|
|
|
// If removing a directory, the directory must be empty.
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_remove(lfs_t *lfs, const char *path);
|
|
|
+#endif
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Rename or move a file or directory
|
|
|
//
|
|
|
// If the destination exists, it must match the source in type.
|
|
|
@@ -435,6 +490,7 @@ int lfs_remove(lfs_t *lfs, const char *path);
|
|
|
//
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
|
|
|
+#endif
|
|
|
|
|
|
// Find info about a file or directory
|
|
|
//
|
|
|
@@ -453,10 +509,11 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
|
|
|
// Returns the size of the attribute, or a negative error code on failure.
|
|
|
// Note, the returned size is the size of the attribute on disk, irrespective
|
|
|
// of the size of the buffer. This can be used to dynamically allocate a buffer
|
|
|
-// or check for existance.
|
|
|
+// or check for existence.
|
|
|
lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
|
|
|
uint8_t type, void *buffer, lfs_size_t size);
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Set custom attributes
|
|
|
//
|
|
|
// Custom attributes are uniquely identified by an 8-bit type and limited
|
|
|
@@ -466,17 +523,21 @@ lfs_ssize_t lfs_getattr(lfs_t *lfs, const char *path,
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_setattr(lfs_t *lfs, const char *path,
|
|
|
uint8_t type, const void *buffer, lfs_size_t size);
|
|
|
+#endif
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Removes a custom attribute
|
|
|
//
|
|
|
// If an attribute is not found, nothing happens.
|
|
|
//
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
|
|
|
+#endif
|
|
|
|
|
|
|
|
|
/// File operations ///
|
|
|
|
|
|
+#ifndef LFS_NO_MALLOC
|
|
|
// Open a file
|
|
|
//
|
|
|
// The mode that the file is opened in is determined by the flags, which
|
|
|
@@ -486,14 +547,18 @@ int lfs_removeattr(lfs_t *lfs, const char *path, uint8_t type);
|
|
|
int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
|
|
|
const char *path, int flags);
|
|
|
|
|
|
+// if LFS_NO_MALLOC is defined, lfs_file_open() will fail with LFS_ERR_NOMEM
|
|
|
+// thus use lfs_file_opencfg() with config.buffer set.
|
|
|
+#endif
|
|
|
+
|
|
|
// Open a file with extra configuration
|
|
|
//
|
|
|
// The mode that the file is opened in is determined by the flags, which
|
|
|
// are values from the enum lfs_open_flags that are bitwise-ored together.
|
|
|
//
|
|
|
// The config struct provides additional config options per file as described
|
|
|
-// above. The config struct must be allocated while the file is open, and the
|
|
|
-// config struct must be zeroed for defaults and backwards compatibility.
|
|
|
+// above. The config struct must remain allocated while the file is open, and
|
|
|
+// the config struct must be zeroed for defaults and backwards compatibility.
|
|
|
//
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_file_opencfg(lfs_t *lfs, lfs_file_t *file,
|
|
|
@@ -521,6 +586,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
|
|
|
lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
|
|
void *buffer, lfs_size_t size);
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Write data to file
|
|
|
//
|
|
|
// Takes a buffer and size indicating the data to write. The file will not
|
|
|
@@ -529,6 +595,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
|
|
|
// Returns the number of bytes written, or a negative error code on failure.
|
|
|
lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
|
|
const void *buffer, lfs_size_t size);
|
|
|
+#endif
|
|
|
|
|
|
// Change the position of the file
|
|
|
//
|
|
|
@@ -537,10 +604,12 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
|
|
|
lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
|
|
|
lfs_soff_t off, int whence);
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Truncates the size of the file to the specified size
|
|
|
//
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size);
|
|
|
+#endif
|
|
|
|
|
|
// Return the position of the file
|
|
|
//
|
|
|
@@ -563,10 +632,12 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
|
|
|
|
|
|
/// Directory operations ///
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
// Create a directory
|
|
|
//
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_mkdir(lfs_t *lfs, const char *path);
|
|
|
+#endif
|
|
|
|
|
|
// Open a directory
|
|
|
//
|
|
|
@@ -611,6 +682,12 @@ int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
|
|
|
|
|
|
/// Filesystem-level filesystem operations
|
|
|
|
|
|
+// Find on-disk info about the filesystem
|
|
|
+//
|
|
|
+// Fills out the fsinfo structure based on the filesystem found on-disk.
|
|
|
+// Returns a negative error code on failure.
|
|
|
+int lfs_fs_stat(lfs_t *lfs, struct lfs_fsinfo *fsinfo);
|
|
|
+
|
|
|
// Finds the current size of the filesystem
|
|
|
//
|
|
|
// Note: Result is best effort. If files share COW structures, the returned
|
|
|
@@ -628,6 +705,19 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs);
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
|
|
|
|
|
|
+#ifndef LFS_READONLY
|
|
|
+// Attempt to make the filesystem consistent and ready for writing
|
|
|
+//
|
|
|
+// Calling this function is not required, consistency will be implicitly
|
|
|
+// enforced on the first operation that writes to the filesystem, but this
|
|
|
+// function allows the work to be performed earlier and without other
|
|
|
+// filesystem changes.
|
|
|
+//
|
|
|
+// Returns a negative error code on failure.
|
|
|
+int lfs_fs_mkconsistent(lfs_t *lfs);
|
|
|
+#endif
|
|
|
+
|
|
|
+#ifndef LFS_READONLY
|
|
|
#ifdef LFS_MIGRATE
|
|
|
// Attempts to migrate a previous version of littlefs
|
|
|
//
|
|
|
@@ -642,6 +732,7 @@ int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
|
|
|
// Returns a negative error code on failure.
|
|
|
int lfs_migrate(lfs_t *lfs, const struct lfs_config *cfg);
|
|
|
#endif
|
|
|
+#endif
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|