lfs.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * The little filesystem
  3. *
  4. * Copyright (c) 2017 Christopher Haster
  5. * Distributed under the MIT license
  6. */
  7. #ifndef LFS_H
  8. #define LFS_H
  9. #include <stdint.h>
  10. #include <stdbool.h>
  11. // Type definitions
  12. typedef uint32_t lfs_size_t;
  13. typedef uint32_t lfs_off_t;
  14. typedef int32_t lfs_ssize_t;
  15. typedef int32_t lfs_soff_t;
  16. typedef uint32_t lfs_block_t;
  17. // Configurable littlefs constants
  18. #ifndef LFS_NAME_MAX
  19. #define LFS_NAME_MAX 255
  20. #endif
  21. // The littefs constants
  22. enum lfs_error {
  23. LFS_ERR_OK = 0,
  24. LFS_ERR_IO = -5,
  25. LFS_ERR_CORRUPT = -77,
  26. LFS_ERR_NOENT = -2,
  27. LFS_ERR_EXISTS = -17,
  28. LFS_ERR_NOTDIR = -20,
  29. LFS_ERR_ISDIR = -21,
  30. LFS_ERR_INVAL = -22,
  31. LFS_ERR_NOSPC = -28,
  32. LFS_ERR_NOMEM = -12,
  33. };
  34. enum lfs_type {
  35. LFS_TYPE_REG = 0x01,
  36. LFS_TYPE_DIR = 0x02,
  37. LFS_TYPE_SUPERBLOCK = 0x10,
  38. };
  39. enum lfs_open_flags {
  40. // open flags
  41. LFS_O_RDONLY = 1,
  42. LFS_O_WRONLY = 2,
  43. LFS_O_RDWR = 3,
  44. LFS_O_CREAT = 0x0100,
  45. LFS_O_EXCL = 0x0200,
  46. LFS_O_TRUNC = 0x0400,
  47. LFS_O_APPEND = 0x0800,
  48. // internally used flags
  49. LFS_F_DIRTY = 0x10000,
  50. LFS_F_WRITING = 0x20000,
  51. LFS_F_READING = 0x40000,
  52. };
  53. enum lfs_whence_flags {
  54. LFS_SEEK_SET = 0,
  55. LFS_SEEK_CUR = 1,
  56. LFS_SEEK_END = 2,
  57. };
  58. // Configuration provided during initialization of the littlefs
  59. struct lfs_config {
  60. // Opaque user provided context
  61. void *context;
  62. // Read a region in a block
  63. int (*read)(const struct lfs_config *c, lfs_block_t block,
  64. lfs_off_t off, void *buffer, lfs_size_t size);
  65. // Program a region in a block. The block must have previously
  66. // been erased.
  67. int (*prog)(const struct lfs_config *c, lfs_block_t block,
  68. lfs_off_t off, const void *buffer, lfs_size_t size);
  69. // Erase a block. A block must be erased before being programmed.
  70. // The state of an erased block is undefined.
  71. int (*erase)(const struct lfs_config *c, lfs_block_t block);
  72. // Sync the state of the underlying block device
  73. int (*sync)(const struct lfs_config *c);
  74. // Minimum size of a read. This may be larger than the physical
  75. // read size to cache reads from the block device.
  76. lfs_size_t read_size;
  77. // Minimum size of a program. This may be larger than the physical
  78. // program size to cache programs to the block device.
  79. lfs_size_t prog_size;
  80. // Size of an erasable block.
  81. lfs_size_t block_size;
  82. // Number of erasable blocks on the device.
  83. lfs_size_t block_count;
  84. // Number of blocks to lookahead during block allocation.
  85. lfs_size_t lookahead;
  86. // Optional, statically allocated read buffer. Must be read sized.
  87. void *read_buffer;
  88. // Optional, statically allocated program buffer. Must be program sized.
  89. void *prog_buffer;
  90. // Optional, statically allocated lookahead buffer.
  91. // Must be 1 bit per lookahead block.
  92. void *lookahead_buffer;
  93. // Optional, statically allocated buffer for files. Must be program sized.
  94. // If enabled, only one file may be opened at a time
  95. void *file_buffer;
  96. };
  97. // File info structure
  98. struct lfs_info {
  99. // Type of the file, either REG or DIR
  100. uint8_t type;
  101. // Size of the file, only valid for REG files
  102. lfs_size_t size;
  103. // Name of the file stored as a null-terminated string
  104. char name[LFS_NAME_MAX+1];
  105. };
  106. // littlefs data structures
  107. typedef struct lfs_entry {
  108. lfs_off_t off;
  109. struct lfs_disk_entry {
  110. uint16_t type;
  111. uint16_t len;
  112. union {
  113. struct {
  114. lfs_block_t head;
  115. lfs_size_t size;
  116. } file;
  117. lfs_block_t dir[2];
  118. } u;
  119. } d;
  120. } lfs_entry_t;
  121. typedef struct lfs_cache {
  122. lfs_block_t block;
  123. lfs_off_t off;
  124. uint8_t *buffer;
  125. } lfs_cache_t;
  126. typedef struct lfs_file {
  127. struct lfs_file *next;
  128. lfs_block_t pair[2];
  129. lfs_off_t poff;
  130. lfs_block_t head;
  131. lfs_size_t size;
  132. uint32_t flags;
  133. lfs_off_t pos;
  134. lfs_block_t block;
  135. lfs_off_t off;
  136. lfs_cache_t cache;
  137. } lfs_file_t;
  138. typedef struct lfs_dir {
  139. lfs_block_t pair[2];
  140. lfs_off_t off;
  141. lfs_block_t head[2];
  142. lfs_off_t pos;
  143. struct lfs_disk_dir {
  144. uint32_t rev;
  145. lfs_size_t size;
  146. lfs_block_t tail[2];
  147. } d;
  148. } lfs_dir_t;
  149. typedef struct lfs_superblock {
  150. lfs_off_t off;
  151. struct lfs_disk_superblock {
  152. uint16_t type;
  153. uint16_t len;
  154. uint32_t version;
  155. char magic[8];
  156. uint32_t block_size;
  157. uint32_t block_count;
  158. lfs_block_t root[2];
  159. } d;
  160. } lfs_superblock_t;
  161. typedef struct lfs_free {
  162. lfs_block_t start;
  163. lfs_block_t off;
  164. uint32_t *lookahead;
  165. } lfs_free_t;
  166. // littlefs type
  167. typedef struct lfs {
  168. const struct lfs_config *cfg;
  169. lfs_size_t words; // number of 32-bit words that can fit in a block
  170. lfs_block_t root[2];
  171. lfs_dir_t *scratch;
  172. lfs_file_t *files;
  173. lfs_cache_t rcache;
  174. lfs_cache_t pcache;
  175. lfs_free_t free;
  176. } lfs_t;
  177. // filesystem functions
  178. int lfs_format(lfs_t *lfs, const struct lfs_config *config);
  179. int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
  180. int lfs_unmount(lfs_t *lfs);
  181. // general operations
  182. int lfs_remove(lfs_t *lfs, const char *path);
  183. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
  184. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
  185. // directory operations
  186. int lfs_mkdir(lfs_t *lfs, const char *path);
  187. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
  188. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
  189. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
  190. int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
  191. lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
  192. int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
  193. // file operations
  194. int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
  195. const char *path, int flags);
  196. int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
  197. int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
  198. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  199. void *buffer, lfs_size_t size);
  200. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  201. const void *buffer, lfs_size_t size);
  202. lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
  203. lfs_soff_t off, int whence);
  204. lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
  205. int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
  206. lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
  207. // miscellaneous lfs specific operations
  208. int lfs_deorphan(lfs_t *lfs);
  209. int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
  210. #endif