lfs.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. LFS_O_RDONLY = 0,
  41. LFS_O_WRONLY = 1,
  42. LFS_O_RDWR = 2,
  43. LFS_O_CREAT = 0x020,
  44. LFS_O_EXCL = 0x040,
  45. LFS_O_TRUNC = 0x080,
  46. LFS_O_APPEND = 0x100,
  47. LFS_O_SYNC = 0x200,
  48. LFS_O_DIRTY = 0x10000,
  49. };
  50. enum lfs_whence_flags {
  51. LFS_SEEK_SET = 0,
  52. LFS_SEEK_CUR = 1,
  53. LFS_SEEK_END = 2,
  54. };
  55. // Configuration provided during initialization of the littlefs
  56. struct lfs_config {
  57. // Opaque user provided context
  58. void *context;
  59. // Read a region in a block
  60. int (*read)(const struct lfs_config *c, lfs_block_t block,
  61. lfs_off_t off, void *buffer, lfs_size_t size);
  62. // Program a region in a block. The block must have previously
  63. // been erased.
  64. int (*prog)(const struct lfs_config *c, lfs_block_t block,
  65. lfs_off_t off, const void *buffer, lfs_size_t size);
  66. // Erase a block. A block must be erased before being programmed.
  67. // The state of an erased block is undefined.
  68. int (*erase)(const struct lfs_config *c, lfs_block_t block);
  69. // Sync the state of the underlying block device
  70. int (*sync)(const struct lfs_config *c);
  71. // Minimum size of a read. This may be larger than the physical
  72. // read size to cache reads from the block device.
  73. lfs_size_t read_size;
  74. // Minimum size of a program. This may be larger than the physical
  75. // program size to cache programs to the block device.
  76. lfs_size_t prog_size;
  77. // Size of an erasable block.
  78. lfs_size_t block_size;
  79. // Number of erasable blocks on the device.
  80. lfs_size_t block_count;
  81. // Number of blocks to lookahead during block allocation.
  82. lfs_size_t lookahead;
  83. // Optional, statically allocated read buffer. Must be read sized.
  84. void *read_buffer;
  85. // Optional, statically allocated program buffer. Must be program sized.
  86. void *prog_buffer;
  87. // Optional, statically allocated lookahead buffer.
  88. // Must be 1 bit per lookahead block.
  89. void *lookahead_buffer;
  90. };
  91. // File info structure
  92. struct lfs_info {
  93. // Type of the file, either REG or DIR
  94. uint8_t type;
  95. // Size of the file, only valid for REG files
  96. lfs_size_t size;
  97. // Name of the file stored as a null-terminated string
  98. char name[LFS_NAME_MAX+1];
  99. };
  100. // littlefs data structures
  101. typedef struct lfs_entry {
  102. lfs_off_t off;
  103. struct lfs_disk_entry {
  104. uint16_t type;
  105. uint16_t len;
  106. union {
  107. struct {
  108. lfs_block_t head;
  109. lfs_size_t size;
  110. } file;
  111. lfs_block_t dir[2];
  112. } u;
  113. } d;
  114. } lfs_entry_t;
  115. typedef struct lfs_file {
  116. lfs_block_t pair[2];
  117. lfs_off_t off;
  118. lfs_block_t head;
  119. lfs_size_t size;
  120. uint32_t flags;
  121. lfs_off_t pos;
  122. lfs_block_t wblock;
  123. lfs_off_t woff;
  124. lfs_block_t rblock;
  125. lfs_off_t roff;
  126. } lfs_file_t;
  127. typedef struct lfs_dir {
  128. lfs_block_t pair[2];
  129. lfs_off_t off;
  130. lfs_block_t head[2];
  131. lfs_off_t pos;
  132. struct lfs_disk_dir {
  133. uint32_t rev;
  134. lfs_size_t size;
  135. lfs_block_t tail[2];
  136. } d;
  137. } lfs_dir_t;
  138. typedef struct lfs_superblock {
  139. lfs_off_t off;
  140. struct lfs_disk_superblock {
  141. uint16_t type;
  142. uint16_t len;
  143. uint32_t version;
  144. char magic[8];
  145. uint32_t block_size;
  146. uint32_t block_count;
  147. lfs_block_t root[2];
  148. } d;
  149. } lfs_superblock_t;
  150. // littlefs type
  151. typedef struct lfs {
  152. const struct lfs_config *cfg;
  153. lfs_size_t words; // number of 32-bit words that can fit in a block
  154. lfs_block_t root[2];
  155. struct {
  156. lfs_block_t block;
  157. lfs_off_t off;
  158. uint8_t *buffer;
  159. } rcache, pcache;
  160. struct {
  161. lfs_block_t start;
  162. lfs_block_t off;
  163. uint32_t *lookahead;
  164. } free;
  165. } lfs_t;
  166. // filesystem functions
  167. int lfs_format(lfs_t *lfs, const struct lfs_config *config);
  168. int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
  169. int lfs_unmount(lfs_t *lfs);
  170. // general operations
  171. int lfs_remove(lfs_t *lfs, const char *path);
  172. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
  173. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
  174. // directory operations
  175. int lfs_mkdir(lfs_t *lfs, const char *path);
  176. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
  177. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
  178. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
  179. int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off);
  180. lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
  181. int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
  182. // file operations
  183. int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
  184. const char *path, int flags);
  185. int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
  186. int lfs_file_sync(lfs_t *lfs, lfs_file_t *file);
  187. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  188. void *buffer, lfs_size_t size);
  189. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  190. const void *buffer, lfs_size_t size);
  191. lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
  192. lfs_soff_t off, int whence);
  193. lfs_soff_t lfs_file_tell(lfs_t *lfs, lfs_file_t *file);
  194. int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
  195. lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
  196. // miscellaneous lfs specific operations
  197. int lfs_deorphan(lfs_t *lfs);
  198. int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
  199. #endif