lfs.h 5.7 KB

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