lfs.h 4.7 KB

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