lfs.h 6.1 KB

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