lfs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * The little filesystem
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the Apache 2.0 license
  6. */
  7. #ifndef LFS_H
  8. #define LFS_H
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. /// Definitions ///
  12. // Type definitions
  13. typedef uint32_t lfs_size_t;
  14. typedef uint32_t lfs_off_t;
  15. typedef int32_t lfs_ssize_t;
  16. typedef int32_t lfs_soff_t;
  17. typedef uint32_t lfs_block_t;
  18. // Max name size in bytes
  19. #ifndef LFS_NAME_MAX
  20. #define LFS_NAME_MAX 255
  21. #endif
  22. // Possible error codes, these are negative to allow
  23. // valid positive return values
  24. enum lfs_error {
  25. LFS_ERR_OK = 0, // No error
  26. LFS_ERR_IO = -5, // Error during device operation
  27. LFS_ERR_CORRUPT = -52, // Corrupted
  28. LFS_ERR_NOENT = -2, // No directory entry
  29. LFS_ERR_EXISTS = -17, // Entry already exists
  30. LFS_ERR_NOTDIR = -20, // Entry is not a dir
  31. LFS_ERR_ISDIR = -21, // Entry is a dir
  32. LFS_ERR_INVAL = -22, // Invalid parameter
  33. LFS_ERR_NOSPC = -28, // No space left on device
  34. LFS_ERR_NOMEM = -12, // No more memory available
  35. };
  36. // File types
  37. enum lfs_type {
  38. LFS_TYPE_REG = 0x11,
  39. LFS_TYPE_DIR = 0x22,
  40. LFS_TYPE_SUPERBLOCK = 0xe2,
  41. };
  42. // File open flags
  43. enum lfs_open_flags {
  44. // open flags
  45. LFS_O_RDONLY = 1, // Open a file as read only
  46. LFS_O_WRONLY = 2, // Open a file as write only
  47. LFS_O_RDWR = 3, // Open a file as read and write
  48. LFS_O_CREAT = 0x0100, // Create a file if it does not exist
  49. LFS_O_EXCL = 0x0200, // Fail if a file already exists
  50. LFS_O_TRUNC = 0x0400, // Truncate the existing file to zero size
  51. LFS_O_APPEND = 0x0800, // Move to end of file on every write
  52. // internally used flags
  53. LFS_F_DIRTY = 0x10000, // File does not match storage
  54. LFS_F_WRITING = 0x20000, // File has been written since last flush
  55. LFS_F_READING = 0x40000, // File has been read since last flush
  56. };
  57. // File seek flags
  58. enum lfs_whence_flags {
  59. LFS_SEEK_SET = 0, // Seek relative to an absolute position
  60. LFS_SEEK_CUR = 1, // Seek relative to the current file position
  61. LFS_SEEK_END = 2, // Seek relative to the end of the file
  62. };
  63. // Configuration provided during initialization of the littlefs
  64. struct lfs_config {
  65. // Opaque user provided context that can be used to pass
  66. // information to the block device operations
  67. void *context;
  68. // Read a region in a block. Negative error codes are propogated
  69. // to the user.
  70. int (*read)(const struct lfs_config *c, lfs_block_t block,
  71. lfs_off_t off, void *buffer, lfs_size_t size);
  72. // Program a region in a block. The block must have previously
  73. // been erased. Negative error codes are propogated to the user.
  74. // The prog function must return LFS_ERR_CORRUPT if the block should
  75. // be considered bad.
  76. int (*prog)(const struct lfs_config *c, lfs_block_t block,
  77. lfs_off_t off, const void *buffer, lfs_size_t size);
  78. // Erase a block. A block must be erased before being programmed.
  79. // The state of an erased block is undefined. Negative error codes
  80. // are propogated to the user.
  81. int (*erase)(const struct lfs_config *c, lfs_block_t block);
  82. // Sync the state of the underlying block device. Negative error codes
  83. // are propogated to the user.
  84. int (*sync)(const struct lfs_config *c);
  85. // Minimum size of a block read. This determines the size of read buffers.
  86. // This may be larger than the physical read size to improve performance
  87. // by caching more of the block device.
  88. lfs_size_t read_size;
  89. // Minimum size of a block program. This determines the size of program
  90. // buffers. This may be larger than the physical program size to improve
  91. // performance by caching more of the block device.
  92. lfs_size_t prog_size;
  93. // Size of an erasable block. This does not impact ram consumption and
  94. // may be larger than the physical erase size. However, this should be
  95. // kept small as each file currently takes up an entire block .
  96. lfs_size_t block_size;
  97. // Number of erasable blocks on the device.
  98. lfs_size_t block_count;
  99. // Number of blocks to lookahead during block allocation. A larger
  100. // lookahead reduces the number of passes required to allocate a block.
  101. // The lookahead buffer requires only 1 bit per block so it can be quite
  102. // large with little ram impact. Should be a multiple of 32.
  103. lfs_size_t lookahead;
  104. // Optional, statically allocated read buffer. Must be read sized.
  105. void *read_buffer;
  106. // Optional, statically allocated program buffer. Must be program sized.
  107. void *prog_buffer;
  108. // Optional, statically allocated lookahead buffer. Must be 1 bit per
  109. // lookahead block.
  110. void *lookahead_buffer;
  111. // Optional, statically allocated buffer for files. Must be program sized.
  112. // If enabled, only one file may be opened at a time.
  113. void *file_buffer;
  114. };
  115. // File info structure
  116. struct lfs_info {
  117. // Type of the file, either LFS_TYPE_REG or LFS_TYPE_DIR
  118. uint8_t type;
  119. // Size of the file, only valid for REG files
  120. lfs_size_t size;
  121. // Name of the file stored as a null-terminated string
  122. char name[LFS_NAME_MAX+1];
  123. };
  124. /// littlefs data structures ///
  125. typedef struct lfs_entry {
  126. lfs_off_t off;
  127. struct lfs_disk_entry {
  128. uint8_t type;
  129. uint8_t elen;
  130. uint8_t alen;
  131. uint8_t nlen;
  132. union {
  133. struct {
  134. lfs_block_t head;
  135. lfs_size_t size;
  136. } file;
  137. lfs_block_t dir[2];
  138. } u;
  139. } d;
  140. } lfs_entry_t;
  141. typedef struct lfs_cache {
  142. lfs_block_t block;
  143. lfs_off_t off;
  144. uint8_t *buffer;
  145. } lfs_cache_t;
  146. typedef struct lfs_file {
  147. struct lfs_file *next;
  148. lfs_block_t pair[2];
  149. lfs_off_t poff;
  150. lfs_block_t head;
  151. lfs_size_t size;
  152. uint32_t flags;
  153. lfs_off_t pos;
  154. lfs_block_t block;
  155. lfs_off_t off;
  156. lfs_cache_t cache;
  157. } lfs_file_t;
  158. typedef struct lfs_dir {
  159. lfs_block_t pair[2];
  160. lfs_off_t off;
  161. lfs_block_t head[2];
  162. lfs_off_t pos;
  163. struct lfs_disk_dir {
  164. uint32_t rev;
  165. lfs_size_t size;
  166. lfs_block_t tail[2];
  167. } d;
  168. } lfs_dir_t;
  169. typedef struct lfs_superblock {
  170. lfs_off_t off;
  171. struct lfs_disk_superblock {
  172. uint8_t type;
  173. uint8_t elen;
  174. uint8_t alen;
  175. uint8_t nlen;
  176. lfs_block_t root[2];
  177. uint32_t block_size;
  178. uint32_t block_count;
  179. uint32_t version;
  180. char magic[8];
  181. } d;
  182. } lfs_superblock_t;
  183. typedef struct lfs_free {
  184. lfs_block_t end;
  185. lfs_block_t start;
  186. lfs_block_t off;
  187. uint32_t *lookahead;
  188. } lfs_free_t;
  189. // The littlefs type
  190. typedef struct lfs {
  191. const struct lfs_config *cfg;
  192. lfs_block_t root[2];
  193. lfs_file_t *files;
  194. bool deorphaned;
  195. lfs_cache_t rcache;
  196. lfs_cache_t pcache;
  197. lfs_free_t free;
  198. } lfs_t;
  199. /// Filesystem functions ///
  200. // Format a block device with the littlefs
  201. //
  202. // Requires a littlefs object and config struct. This clobbers the littlefs
  203. // object, and does not leave the filesystem mounted.
  204. //
  205. // Returns a negative error code on failure.
  206. int lfs_format(lfs_t *lfs, const struct lfs_config *config);
  207. // Mounts a littlefs
  208. //
  209. // Requires a littlefs object and config struct. Multiple filesystems
  210. // may be mounted simultaneously with multiple littlefs objects. Both
  211. // lfs and config must be allocated while mounted.
  212. //
  213. // Returns a negative error code on failure.
  214. int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
  215. // Unmounts a littlefs
  216. //
  217. // Does nothing besides releasing any allocated resources.
  218. // Returns a negative error code on failure.
  219. int lfs_unmount(lfs_t *lfs);
  220. /// General operations ///
  221. // Removes a file or directory
  222. //
  223. // If removing a directory, the directory must be empty.
  224. // Returns a negative error code on failure.
  225. int lfs_remove(lfs_t *lfs, const char *path);
  226. // Rename or move a file or directory
  227. //
  228. // If the destination exists, it must match the source in type.
  229. // If the destination is a directory, the directory must be empty.
  230. //
  231. // Note: If power loss occurs, it is possible that the file or directory
  232. // will exist in both the oldpath and newpath simultaneously after the
  233. // next mount.
  234. //
  235. // Returns a negative error code on failure.
  236. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
  237. // Find info about a file or directory
  238. //
  239. // Fills out the info structure, based on the specified file or directory.
  240. // Returns a negative error code on failure.
  241. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
  242. /// File operations ///
  243. // Open a file
  244. //
  245. // The mode that the file is opened in is determined
  246. // by the flags, which are values from the enum lfs_open_flags
  247. // that are bitwise-ored together.
  248. //
  249. // Returns a negative error code on failure.
  250. int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
  251. const char *path, int flags);
  252. // Close a file
  253. //
  254. // Any pending writes are written out to storage as though
  255. // sync had been called and releases any allocated resources.
  256. //
  257. // Returns a negative error code on failure.
  258. int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
  259. // Synchronize a file on storage
  260. //
  261. // Any pending writes are written out to storage.
  262. // Returns a negative error code on failure.
  263. int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
  264. // Read data from file
  265. //
  266. // Takes a buffer and size indicating where to store the read data.
  267. // Returns the number of bytes read, or a negative error code on failure.
  268. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  269. void *buffer, lfs_size_t size);
  270. // Write data to file
  271. //
  272. // Takes a buffer and size indicating the data to write. The file will not
  273. // actually be updated on the storage until either sync or close is called.
  274. //
  275. // Returns the number of bytes written, or a negative error code on failure.
  276. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  277. const void *buffer, lfs_size_t size);
  278. // Change the position of the file
  279. //
  280. // The change in position is determined by the offset and whence flag.
  281. // Returns the old position of the file, or a negative error code on failure.
  282. lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
  283. lfs_soff_t off, int whence);
  284. // Return the position of the file
  285. //
  286. // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
  287. // Returns the position of the file, or a negative error code on failure.
  288. lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
  289. // Change the position of the file to the beginning of the file
  290. //
  291. // Equivalent to lfs_file_seek(lfs, file, 0, LFS_SEEK_CUR)
  292. // Returns a negative error code on failure.
  293. int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
  294. // Return the size of the file
  295. //
  296. // Similar to lfs_file_seek(lfs, file, 0, LFS_SEEK_END)
  297. // Returns the size of the file, or a negative error code on failure.
  298. lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
  299. /// Directory operations ///
  300. // Create a directory
  301. //
  302. // Returns a negative error code on failure.
  303. int lfs_mkdir(lfs_t *lfs, const char *path);
  304. // Open a directory
  305. //
  306. // Once open a directory can be used with read to iterate over files.
  307. // Returns a negative error code on failure.
  308. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
  309. // Close a directory
  310. //
  311. // Releases any allocated resources.
  312. // Returns a negative error code on failure.
  313. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
  314. // Read an entry in the directory
  315. //
  316. // Fills out the info structure, based on the specified file or directory.
  317. // Returns a negative error code on failure.
  318. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
  319. // Change the position of the directory
  320. //
  321. // The new off must be a value previous returned from tell and specifies
  322. // an absolute offset in the directory seek.
  323. //
  324. // Returns a negative error code on failure.
  325. int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
  326. // Return the position of the directory
  327. //
  328. // The returned offset is only meant to be consumed by seek and may not make
  329. // sense, but does indicate the current position in the directory iteration.
  330. //
  331. // Returns the position of the directory, or a negative error code on failure.
  332. lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
  333. // Change the position of the directory to the beginning of the directory
  334. //
  335. // Returns a negative error code on failure.
  336. int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
  337. /// Miscellaneous littlefs specific operations ///
  338. // Traverse through all blocks in use by the filesystem
  339. //
  340. // The provided callback will be called with each block address that is
  341. // currently in use by the filesystem. This can be used to determine which
  342. // blocks are in use or how much of the storage is available.
  343. //
  344. // Returns a negative error code on failure.
  345. int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
  346. // Prunes any recoverable errors that may have occured in the filesystem
  347. //
  348. // Not needed to be called by user unless an operation is interrupted
  349. // but the filesystem is still mounted. This is already called on first
  350. // allocation.
  351. //
  352. // Returns a negative error code on failure.
  353. int lfs_deorphan(lfs_t *lfs);
  354. #endif