lfs.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // Configuration provided during initialization of the littlefs
  38. struct lfs_config {
  39. // Opaque user provided context
  40. void *context;
  41. // Read a region in a block
  42. int (*read)(const struct lfs_config *c, lfs_block_t block,
  43. lfs_off_t off, lfs_size_t size, void *buffer);
  44. // Program a region in a block. The block must have previously
  45. // been erased.
  46. int (*prog)(const struct lfs_config *c, lfs_block_t block,
  47. lfs_off_t off, lfs_size_t size, const void *buffer);
  48. // Erase a block. A block must be erased before being programmed.
  49. // The state of an erased block is undefined.
  50. int (*erase)(const struct lfs_config *c, lfs_block_t block);
  51. // Sync the state of the underlying block device
  52. int (*sync)(const struct lfs_config *c);
  53. // Minimum size of a read. This may be larger than the physical
  54. // read size to cache reads from the block device.
  55. lfs_size_t read_size;
  56. // Minimum size of a program. This may be larger than the physical
  57. // program size to cache programs to the block device.
  58. lfs_size_t prog_size;
  59. // Size of an erasable block.
  60. lfs_size_t block_size;
  61. // Number of erasable blocks on the device.
  62. lfs_size_t block_count;
  63. // Number of blocks to lookahead during block allocation.
  64. lfs_size_t lookahead;
  65. // Optional, statically allocated read buffer. Must be read sized.
  66. void *read_buffer;
  67. // Optional, statically allocated program buffer. Must be program sized.
  68. void *prog_buffer;
  69. // Optional, statically allocated lookahead buffer.
  70. // Must be 1 bit per lookahead block.
  71. void *lookahead_buffer;
  72. };
  73. // File info structure
  74. struct lfs_info {
  75. // Type of the file, either REG or DIR
  76. uint8_t type;
  77. // Size of the file, only valid for REG files
  78. lfs_size_t size;
  79. // Name of the file stored as a null-terminated string
  80. char name[LFS_NAME_MAX+1];
  81. };
  82. // Internal data structures
  83. typedef struct lfs_entry {
  84. lfs_block_t pair[2];
  85. lfs_off_t off;
  86. struct lfs_disk_entry {
  87. uint16_t type;
  88. uint16_t len;
  89. union {
  90. struct {
  91. lfs_block_t head;
  92. lfs_size_t size;
  93. } file;
  94. lfs_block_t dir[2];
  95. } u;
  96. } d;
  97. } lfs_entry_t;
  98. typedef struct lfs_file {
  99. lfs_block_t head;
  100. lfs_size_t size;
  101. lfs_block_t wblock;
  102. uint32_t windex;
  103. lfs_block_t rblock;
  104. uint32_t rindex;
  105. lfs_off_t roff;
  106. struct lfs_entry entry;
  107. } lfs_file_t;
  108. typedef struct lfs_dir {
  109. lfs_block_t pair[2];
  110. lfs_off_t off;
  111. struct lfs_disk_dir {
  112. uint32_t rev;
  113. lfs_size_t size;
  114. lfs_block_t tail[2];
  115. } d;
  116. } lfs_dir_t;
  117. typedef struct lfs_superblock {
  118. lfs_block_t dir[2]; //TODO rm me?
  119. lfs_off_t off;
  120. struct lfs_disk_superblock {
  121. uint16_t type;
  122. uint16_t len;
  123. uint32_t version;
  124. char magic[8];
  125. uint32_t block_size;
  126. uint32_t block_count;
  127. lfs_block_t root[2];
  128. } d;
  129. } lfs_superblock_t;
  130. // Little filesystem type
  131. typedef struct lfs {
  132. const struct lfs_config *cfg;
  133. lfs_size_t words; // number of 32-bit words that can fit in a block
  134. lfs_block_t root[2];
  135. struct {
  136. lfs_block_t block;
  137. lfs_off_t off;
  138. uint8_t *buffer;
  139. } rcache, pcache;
  140. struct {
  141. lfs_block_t start;
  142. lfs_block_t off;
  143. uint32_t *lookahead;
  144. } free;
  145. } lfs_t;
  146. // Functions
  147. int lfs_format(lfs_t *lfs, const struct lfs_config *config);
  148. int lfs_mount(lfs_t *lfs, const struct lfs_config *config);
  149. int lfs_unmount(lfs_t *lfs);
  150. int lfs_remove(lfs_t *lfs, const char *path);
  151. int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
  152. int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info);
  153. int lfs_mkdir(lfs_t *lfs, const char *path);
  154. int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path);
  155. int lfs_dir_close(lfs_t *lfs, lfs_dir_t *dir);
  156. int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info);
  157. int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
  158. const char *path, int flags);
  159. int lfs_file_close(lfs_t *lfs, lfs_file_t *file);
  160. lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
  161. const void *buffer, lfs_size_t size);
  162. lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
  163. void *buffer, lfs_size_t size);
  164. int lfs_deorphan(lfs_t *lfs);
  165. int lfs_traverse(lfs_t *lfs, int (*cb)(void*, lfs_block_t), void *data);
  166. #endif