lfs.h 6.1 KB

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