2 * The little filesystem
4 * Copyright (c) 2022, The littlefs authors.
5 * Copyright (c) 2017, Arm Limited. All rights reserved.
6 * SPDX-License-Identifier: BSD-3-Clause
21 // Software library version
22 // Major (top-nibble), incremented on backwards incompatible changes
23 // Minor (bottom-nibble), incremented on feature additions
24 #define LFS_VERSION 0x00020007
25 #define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
26 #define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
28 // Version of On-disk data structures
29 // Major (top-nibble), incremented on backwards incompatible changes
30 // Minor (bottom-nibble), incremented on feature additions
31 #define LFS_DISK_VERSION 0x00020001
32 #define LFS_DISK_VERSION_MAJOR (0xffff & (LFS_DISK_VERSION >> 16))
33 #define LFS_DISK_VERSION_MINOR (0xffff & (LFS_DISK_VERSION >> 0))
39 typedef uint32_t lfs_size_t
;
40 typedef uint32_t lfs_off_t
;
42 typedef int32_t lfs_ssize_t
;
43 typedef int32_t lfs_soff_t
;
45 typedef uint32_t lfs_block_t
;
47 // Maximum name size in bytes, may be redefined to reduce the size of the
48 // info struct. Limited to <= 1022. Stored in superblock and must be
49 // respected by other littlefs drivers.
51 #define LFS_NAME_MAX 255
54 // Maximum size of a file in bytes, may be redefined to limit to support other
55 // drivers. Limited on disk to <= 4294967296. However, above 2147483647 the
56 // functions lfs_file_seek, lfs_file_size, and lfs_file_tell will return
57 // incorrect values due to using signed integers. Stored in superblock and
58 // must be respected by other littlefs drivers.
60 #define LFS_FILE_MAX 2147483647
63 // Maximum size of custom attributes in bytes, may be redefined, but there is
64 // no real benefit to using a smaller LFS_ATTR_MAX. Limited to <= 1022.
66 #define LFS_ATTR_MAX 1022
69 // Possible error codes, these are negative to allow
70 // valid positive return values
72 LFS_ERR_OK
= 0, // No error
73 LFS_ERR_IO
= -5, // Error during device operation
74 LFS_ERR_CORRUPT
= -84, // Corrupted
75 LFS_ERR_NOENT
= -2, // No directory entry
76 LFS_ERR_EXIST
= -17, // Entry already exists
77 LFS_ERR_NOTDIR
= -20, // Entry is not a dir
78 LFS_ERR_ISDIR
= -21, // Entry is a dir
79 LFS_ERR_NOTEMPTY
= -39, // Dir is not empty
80 LFS_ERR_BADF
= -9, // Bad file number
81 LFS_ERR_FBIG
= -27, // File too large
82 LFS_ERR_INVAL
= -22, // Invalid parameter
83 LFS_ERR_NOSPC
= -28, // No space left on device
84 LFS_ERR_NOMEM
= -12, // No more memory available
85 LFS_ERR_NOATTR
= -61, // No data/attr available
86 LFS_ERR_NAMETOOLONG
= -36, // File name too long
95 // internally used types
96 LFS_TYPE_SPLICE
= 0x400,
97 LFS_TYPE_NAME
= 0x000,
98 LFS_TYPE_STRUCT
= 0x200,
99 LFS_TYPE_USERATTR
= 0x300,
100 LFS_TYPE_FROM
= 0x100,
101 LFS_TYPE_TAIL
= 0x600,
102 LFS_TYPE_GLOBALS
= 0x700,
103 LFS_TYPE_CRC
= 0x500,
105 // internally used type specializations
106 LFS_TYPE_CREATE
= 0x401,
107 LFS_TYPE_DELETE
= 0x4ff,
108 LFS_TYPE_SUPERBLOCK
= 0x0ff,
109 LFS_TYPE_DIRSTRUCT
= 0x200,
110 LFS_TYPE_CTZSTRUCT
= 0x202,
111 LFS_TYPE_INLINESTRUCT
= 0x201,
112 LFS_TYPE_SOFTTAIL
= 0x600,
113 LFS_TYPE_HARDTAIL
= 0x601,
114 LFS_TYPE_MOVESTATE
= 0x7ff,
115 LFS_TYPE_CCRC
= 0x500,
116 LFS_TYPE_FCRC
= 0x5ff,
118 // internal chip sources
119 LFS_FROM_NOOP
= 0x000,
120 LFS_FROM_MOVE
= 0x101,
121 LFS_FROM_USERATTRS
= 0x102,
125 enum lfs_open_flags
{
127 LFS_O_RDONLY
= 1, // Open a file as read only
129 LFS_O_WRONLY
= 2, // Open a file as write only
130 LFS_O_RDWR
= 3, // Open a file as read and write
131 LFS_O_CREAT
= 0x0100, // Create a file if it does not exist
132 LFS_O_EXCL
= 0x0200, // Fail if a file already exists
133 LFS_O_TRUNC
= 0x0400, // Truncate the existing file to zero size
134 LFS_O_APPEND
= 0x0800, // Move to end of file on every write
137 // internally used flags
139 LFS_F_DIRTY
= 0x010000, // File does not match storage
140 LFS_F_WRITING
= 0x020000, // File has been written since last flush
142 LFS_F_READING
= 0x040000, // File has been read since last flush
144 LFS_F_ERRED
= 0x080000, // An error occurred during write
146 LFS_F_INLINE
= 0x100000, // Currently inlined in directory entry
150 enum lfs_whence_flags
{
151 LFS_SEEK_SET
= 0, // Seek relative to an absolute position
152 LFS_SEEK_CUR
= 1, // Seek relative to the current file position
153 LFS_SEEK_END
= 2, // Seek relative to the end of the file
157 // Configuration provided during initialization of the littlefs
159 // Opaque user provided context that can be used to pass
160 // information to the block device operations
163 // Read a region in a block. Negative error codes are propagated
165 int (*read
)(const struct lfs_config
*c
, lfs_block_t block
,
166 lfs_off_t off
, void *buffer
, lfs_size_t size
);
168 // Program a region in a block. The block must have previously
169 // been erased. Negative error codes are propagated to the user.
170 // May return LFS_ERR_CORRUPT if the block should be considered bad.
171 int (*prog
)(const struct lfs_config
*c
, lfs_block_t block
,
172 lfs_off_t off
, const void *buffer
, lfs_size_t size
);
174 // Erase a block. A block must be erased before being programmed.
175 // The state of an erased block is undefined. Negative error codes
176 // are propagated to the user.
177 // May return LFS_ERR_CORRUPT if the block should be considered bad.
178 int (*erase
)(const struct lfs_config
*c
, lfs_block_t block
);
180 // Sync the state of the underlying block device. Negative error codes
181 // are propagated to the user.
182 int (*sync
)(const struct lfs_config
*c
);
184 #ifdef LFS_THREADSAFE
185 // Lock the underlying block device. Negative error codes
186 // are propagated to the user.
187 int (*lock
)(const struct lfs_config
*c
);
189 // Unlock the underlying block device. Negative error codes
190 // are propagated to the user.
191 int (*unlock
)(const struct lfs_config
*c
);
194 // Minimum size of a block read in bytes. All read operations will be a
195 // multiple of this value.
196 lfs_size_t read_size
;
198 // Minimum size of a block program in bytes. All program operations will be
199 // a multiple of this value.
200 lfs_size_t prog_size
;
202 // Size of an erasable block in bytes. This does not impact ram consumption
203 // and may be larger than the physical erase size. However, non-inlined
204 // files take up at minimum one block. Must be a multiple of the read and
206 lfs_size_t block_size
;
208 // Number of erasable blocks on the device.
209 lfs_size_t block_count
;
211 // Number of erase cycles before littlefs evicts metadata logs and moves
212 // the metadata to another block. Suggested values are in the
213 // range 100-1000, with large values having better performance at the cost
214 // of less consistent wear distribution.
216 // Set to -1 to disable block-level wear-leveling.
217 int32_t block_cycles
;
219 // Size of block caches in bytes. Each cache buffers a portion of a block in
220 // RAM. The littlefs needs a read cache, a program cache, and one additional
221 // cache per file. Larger caches can improve performance by storing more
222 // data and reducing the number of disk accesses. Must be a multiple of the
223 // read and program sizes, and a factor of the block size.
224 lfs_size_t cache_size
;
226 // Size of the lookahead buffer in bytes. A larger lookahead buffer
227 // increases the number of blocks found during an allocation pass. The
228 // lookahead buffer is stored as a compact bitmap, so each byte of RAM
229 // can track 8 blocks. Must be a multiple of 8.
230 lfs_size_t lookahead_size
;
232 // Optional statically allocated read buffer. Must be cache_size.
233 // By default lfs_malloc is used to allocate this buffer.
236 // Optional statically allocated program buffer. Must be cache_size.
237 // By default lfs_malloc is used to allocate this buffer.
240 // Optional statically allocated lookahead buffer. Must be lookahead_size
241 // and aligned to a 32-bit boundary. By default lfs_malloc is used to
242 // allocate this buffer.
243 void *lookahead_buffer
;
245 // Optional upper limit on length of file names in bytes. No downside for
246 // larger names except the size of the info struct which is controlled by
247 // the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX when zero. Stored in
248 // superblock and must be respected by other littlefs drivers.
251 // Optional upper limit on files in bytes. No downside for larger files
252 // but must be <= LFS_FILE_MAX. Defaults to LFS_FILE_MAX when zero. Stored
253 // in superblock and must be respected by other littlefs drivers.
256 // Optional upper limit on custom attributes in bytes. No downside for
257 // larger attributes size but must be <= LFS_ATTR_MAX. Defaults to
258 // LFS_ATTR_MAX when zero.
261 // Optional upper limit on total space given to metadata pairs in bytes. On
262 // devices with large blocks (e.g. 128kB) setting this to a low size (2-8kB)
263 // can help bound the metadata compaction time. Must be <= block_size.
264 // Defaults to block_size when zero.
265 lfs_size_t metadata_max
;
267 #ifdef LFS_MULTIVERSION
268 // On-disk version to use when writing in the form of 16-bit major version
269 // + 16-bit minor version. This limiting metadata to what is supported by
270 // older minor versions. Note that some features will be lost. Defaults to
271 // to the most recent minor version when zero.
272 uint32_t disk_version
;
276 // File info structure
278 // Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR
281 // Size of the file, only valid for REG files. Limited to 32-bits.
284 // Name of the file stored as a null-terminated string. Limited to
285 // LFS_NAME_MAX+1, which can be changed by redefining LFS_NAME_MAX to
286 // reduce RAM. LFS_NAME_MAX is stored in superblock and must be
287 // respected by other littlefs drivers.
288 char name
[LFS_NAME_MAX
+1];
291 // Filesystem info structure
294 uint32_t disk_version
;
296 // Upper limit on the length of file names in bytes.
299 // Upper limit on the size of files in bytes.
302 // Upper limit on the size of custom attributes in bytes.
306 // Custom attribute structure, used to describe custom attributes
307 // committed atomically during file writes.
309 // 8-bit type of attribute, provided by user and used to
310 // identify the attribute
313 // Pointer to buffer containing the attribute
316 // Size of attribute in bytes, limited to LFS_ATTR_MAX
320 // Optional configuration provided during lfs_file_opencfg
321 struct lfs_file_config
{
322 // Optional statically allocated file buffer. Must be cache_size.
323 // By default lfs_malloc is used to allocate this buffer.
326 // Optional list of custom attributes related to the file. If the file
327 // is opened with read access, these attributes will be read from disk
328 // during the open call. If the file is opened with write access, the
329 // attributes will be written to disk every file sync or close. This
330 // write occurs atomically with update to the file's contents.
332 // Custom attributes are uniquely identified by an 8-bit type and limited
333 // to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller
334 // than the buffer, it will be padded with zeros. If the stored attribute
335 // is larger, then it will be silently truncated. If the attribute is not
336 // found, it will be created implicitly.
337 struct lfs_attr
*attrs
;
339 // Number of custom attributes in the list
340 lfs_size_t attr_count
;
344 /// internal littlefs data structures ///
345 typedef struct lfs_cache
{
352 typedef struct lfs_mdir
{
363 // littlefs directory type
364 typedef struct lfs_dir
{
365 struct lfs_dir
*next
;
374 // littlefs file type
375 typedef struct lfs_file
{
376 struct lfs_file
*next
;
392 const struct lfs_file_config
*cfg
;
395 typedef struct lfs_superblock
{
397 lfs_size_t block_size
;
398 lfs_size_t block_count
;
404 typedef struct lfs_gstate
{
409 // The littlefs filesystem type
416 struct lfs_mlist
*next
;
435 const struct lfs_config
*cfg
;
446 /// Filesystem functions ///
449 // Format a block device with the littlefs
451 // Requires a littlefs object and config struct. This clobbers the littlefs
452 // object, and does not leave the filesystem mounted. The config struct must
453 // be zeroed for defaults and backwards compatibility.
455 // Returns a negative error code on failure.
456 int lfs_format(lfs_t
*lfs
, const struct lfs_config
*config
);
461 // Requires a littlefs object and config struct. Multiple filesystems
462 // may be mounted simultaneously with multiple littlefs objects. Both
463 // lfs and config must be allocated while mounted. The config struct must
464 // be zeroed for defaults and backwards compatibility.
466 // Returns a negative error code on failure.
467 int lfs_mount(lfs_t
*lfs
, const struct lfs_config
*config
);
469 // Unmounts a littlefs
471 // Does nothing besides releasing any allocated resources.
472 // Returns a negative error code on failure.
473 int lfs_unmount(lfs_t
*lfs
);
475 /// General operations ///
478 // Removes a file or directory
480 // If removing a directory, the directory must be empty.
481 // Returns a negative error code on failure.
482 int lfs_remove(lfs_t
*lfs
, const char *path
);
486 // Rename or move a file or directory
488 // If the destination exists, it must match the source in type.
489 // If the destination is a directory, the directory must be empty.
491 // Returns a negative error code on failure.
492 int lfs_rename(lfs_t
*lfs
, const char *oldpath
, const char *newpath
);
495 // Find info about a file or directory
497 // Fills out the info structure, based on the specified file or directory.
498 // Returns a negative error code on failure.
499 int lfs_stat(lfs_t
*lfs
, const char *path
, struct lfs_info
*info
);
501 // Get a custom attribute
503 // Custom attributes are uniquely identified by an 8-bit type and limited
504 // to LFS_ATTR_MAX bytes. When read, if the stored attribute is smaller than
505 // the buffer, it will be padded with zeros. If the stored attribute is larger,
506 // then it will be silently truncated. If no attribute is found, the error
507 // LFS_ERR_NOATTR is returned and the buffer is filled with zeros.
509 // Returns the size of the attribute, or a negative error code on failure.
510 // Note, the returned size is the size of the attribute on disk, irrespective
511 // of the size of the buffer. This can be used to dynamically allocate a buffer
512 // or check for existence.
513 lfs_ssize_t
lfs_getattr(lfs_t
*lfs
, const char *path
,
514 uint8_t type
, void *buffer
, lfs_size_t size
);
517 // Set custom attributes
519 // Custom attributes are uniquely identified by an 8-bit type and limited
520 // to LFS_ATTR_MAX bytes. If an attribute is not found, it will be
521 // implicitly created.
523 // Returns a negative error code on failure.
524 int lfs_setattr(lfs_t
*lfs
, const char *path
,
525 uint8_t type
, const void *buffer
, lfs_size_t size
);
529 // Removes a custom attribute
531 // If an attribute is not found, nothing happens.
533 // Returns a negative error code on failure.
534 int lfs_removeattr(lfs_t
*lfs
, const char *path
, uint8_t type
);
538 /// File operations ///
540 #ifndef LFS_NO_MALLOC
543 // The mode that the file is opened in is determined by the flags, which
544 // are values from the enum lfs_open_flags that are bitwise-ored together.
546 // Returns a negative error code on failure.
547 int lfs_file_open(lfs_t
*lfs
, lfs_file_t
*file
,
548 const char *path
, int flags
);
550 // if LFS_NO_MALLOC is defined, lfs_file_open() will fail with LFS_ERR_NOMEM
551 // thus use lfs_file_opencfg() with config.buffer set.
554 // Open a file with extra configuration
556 // The mode that the file is opened in is determined by the flags, which
557 // are values from the enum lfs_open_flags that are bitwise-ored together.
559 // The config struct provides additional config options per file as described
560 // above. The config struct must remain allocated while the file is open, and
561 // the config struct must be zeroed for defaults and backwards compatibility.
563 // Returns a negative error code on failure.
564 int lfs_file_opencfg(lfs_t
*lfs
, lfs_file_t
*file
,
565 const char *path
, int flags
,
566 const struct lfs_file_config
*config
);
570 // Any pending writes are written out to storage as though
571 // sync had been called and releases any allocated resources.
573 // Returns a negative error code on failure.
574 int lfs_file_close(lfs_t
*lfs
, lfs_file_t
*file
);
576 // Synchronize a file on storage
578 // Any pending writes are written out to storage.
579 // Returns a negative error code on failure.
580 int lfs_file_sync(lfs_t
*lfs
, lfs_file_t
*file
);
582 // Read data from file
584 // Takes a buffer and size indicating where to store the read data.
585 // Returns the number of bytes read, or a negative error code on failure.
586 lfs_ssize_t
lfs_file_read(lfs_t
*lfs
, lfs_file_t
*file
,
587 void *buffer
, lfs_size_t size
);
590 // Write data to file
592 // Takes a buffer and size indicating the data to write. The file will not
593 // actually be updated on the storage until either sync or close is called.
595 // Returns the number of bytes written, or a negative error code on failure.
596 lfs_ssize_t
lfs_file_write(lfs_t
*lfs
, lfs_file_t
*file
,
597 const void *buffer
, lfs_size_t size
);
600 // Change the position of the file
602 // The change in position is determined by the offset and whence flag.
603 // Returns the new position of the file, or a negative error code on failure.
604 lfs_soff_t
lfs_file_seek(lfs_t
*lfs
, lfs_file_t
*file
,
605 lfs_soff_t off
, int whence
);
608 // Truncates the size of the file to the specified size
610 // Returns a negative error code on failure.
611 int lfs_file_truncate(lfs_t
*lfs
, lfs_file_t
*file
, lfs_off_t size
);
614 // Return the position of the file
616 // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
617 // Returns the position of the file, or a negative error code on failure.
618 lfs_soff_t
lfs_file_tell(lfs_t
*lfs
, lfs_file_t
*file
);
620 // Change the position of the file to the beginning of the file
622 // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_SET)
623 // Returns a negative error code on failure.
624 int lfs_file_rewind(lfs_t
*lfs
, lfs_file_t
*file
);
626 // Return the size of the file
628 // Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END)
629 // Returns the size of the file, or a negative error code on failure.
630 lfs_soff_t
lfs_file_size(lfs_t
*lfs
, lfs_file_t
*file
);
633 /// Directory operations ///
636 // Create a directory
638 // Returns a negative error code on failure.
639 int lfs_mkdir(lfs_t
*lfs
, const char *path
);
644 // Once open a directory can be used with read to iterate over files.
645 // Returns a negative error code on failure.
646 int lfs_dir_open(lfs_t
*lfs
, lfs_dir_t
*dir
, const char *path
);
650 // Releases any allocated resources.
651 // Returns a negative error code on failure.
652 int lfs_dir_close(lfs_t
*lfs
, lfs_dir_t
*dir
);
654 // Read an entry in the directory
656 // Fills out the info structure, based on the specified file or directory.
657 // Returns a positive value on success, 0 at the end of directory,
658 // or a negative error code on failure.
659 int lfs_dir_read(lfs_t
*lfs
, lfs_dir_t
*dir
, struct lfs_info
*info
);
661 // Change the position of the directory
663 // The new off must be a value previous returned from tell and specifies
664 // an absolute offset in the directory seek.
666 // Returns a negative error code on failure.
667 int lfs_dir_seek(lfs_t
*lfs
, lfs_dir_t
*dir
, lfs_off_t off
);
669 // Return the position of the directory
671 // The returned offset is only meant to be consumed by seek and may not make
672 // sense, but does indicate the current position in the directory iteration.
674 // Returns the position of the directory, or a negative error code on failure.
675 lfs_soff_t
lfs_dir_tell(lfs_t
*lfs
, lfs_dir_t
*dir
);
677 // Change the position of the directory to the beginning of the directory
679 // Returns a negative error code on failure.
680 int lfs_dir_rewind(lfs_t
*lfs
, lfs_dir_t
*dir
);
683 /// Filesystem-level filesystem operations
685 // Find on-disk info about the filesystem
687 // Fills out the fsinfo structure based on the filesystem found on-disk.
688 // Returns a negative error code on failure.
689 int lfs_fs_stat(lfs_t
*lfs
, struct lfs_fsinfo
*fsinfo
);
691 // Finds the current size of the filesystem
693 // Note: Result is best effort. If files share COW structures, the returned
694 // size may be larger than the filesystem actually is.
696 // Returns the number of allocated blocks, or a negative error code on failure.
697 lfs_ssize_t
lfs_fs_size(lfs_t
*lfs
);
699 // Traverse through all blocks in use by the filesystem
701 // The provided callback will be called with each block address that is
702 // currently in use by the filesystem. This can be used to determine which
703 // blocks are in use or how much of the storage is available.
705 // Returns a negative error code on failure.
706 int lfs_fs_traverse(lfs_t
*lfs
, int (*cb
)(void*, lfs_block_t
), void *data
);
709 // Attempt to make the filesystem consistent and ready for writing
711 // Calling this function is not required, consistency will be implicitly
712 // enforced on the first operation that writes to the filesystem, but this
713 // function allows the work to be performed earlier and without other
714 // filesystem changes.
716 // Returns a negative error code on failure.
717 int lfs_fs_mkconsistent(lfs_t
*lfs
);
722 // Attempts to migrate a previous version of littlefs
724 // Behaves similarly to the lfs_format function. Attempts to mount
725 // the previous version of littlefs and update the filesystem so it can be
726 // mounted with the current version of littlefs.
728 // Requires a littlefs object and config struct. This clobbers the littlefs
729 // object, and does not leave the filesystem mounted. The config struct must
730 // be zeroed for defaults and backwards compatibility.
732 // Returns a negative error code on failure.
733 int lfs_migrate(lfs_t
*lfs
, const struct lfs_config
*cfg
);