110 #ifndef SPIFFS_NUCLEUS_H_
111 #define SPIFFS_NUCLEUS_H_
113 #define _SPIFFS_ERR_CHECK_FIRST (SPIFFS_ERR_INTERNAL - 1)
114 #define SPIFFS_ERR_CHECK_OBJ_ID_MISM (SPIFFS_ERR_INTERNAL - 1)
115 #define SPIFFS_ERR_CHECK_SPIX_MISM (SPIFFS_ERR_INTERNAL - 2)
116 #define SPIFFS_ERR_CHECK_FLAGS_BAD (SPIFFS_ERR_INTERNAL - 3)
117 #define _SPIFFS_ERR_CHECK_LAST (SPIFFS_ERR_INTERNAL - 4)
119 #define SPIFFS_VIS_COUNTINUE (SPIFFS_ERR_INTERNAL - 20)
120 #define SPIFFS_VIS_COUNTINUE_RELOAD (SPIFFS_ERR_INTERNAL - 21)
121 #define SPIFFS_VIS_END (SPIFFS_ERR_INTERNAL - 22)
123 #define SPIFFS_EV_IX_UPD 0
124 #define SPIFFS_EV_IX_NEW 1
125 #define SPIFFS_EV_IX_DEL 2
127 #define SPIFFS_OBJ_ID_IX_FLAG ((spiffs_obj_id)(1<<(8*sizeof(spiffs_obj_id)-1)))
129 #define SPIFFS_UNDEFINED_LEN (u32_t)(-1)
131 #define SPIFFS_OBJ_ID_DELETED ((spiffs_obj_id)0)
132 #define SPIFFS_OBJ_ID_FREE ((spiffs_obj_id)-1)
134 #define SPIFFS_MAGIC(fs) ((spiffs_obj_id)(0x20140529 ^ SPIFFS_CFG_LOG_PAGE_SZ(fs)))
136 #define SPIFFS_CONFIG_MAGIC (0x20090315)
138 #if SPIFFS_SINGLETON == 0
139 #define SPIFFS_CFG_LOG_PAGE_SZ(fs) \
140 ((fs)->cfg.log_page_size)
141 #define SPIFFS_CFG_LOG_BLOCK_SZ(fs) \
142 ((fs)->cfg.log_block_size)
143 #define SPIFFS_CFG_PHYS_SZ(fs) \
144 ((fs)->cfg.phys_size)
145 #define SPIFFS_CFG_PHYS_ERASE_SZ(fs) \
146 ((fs)->cfg.phys_erase_block)
147 #define SPIFFS_CFG_PHYS_ADDR(fs) \
148 ((fs)->cfg.phys_addr)
152 #define SPIFFS_MAX_PAGES(fs) \
153 ( SPIFFS_CFG_PHYS_SZ(fs)/SPIFFS_CFG_LOG_PAGE_SZ(fs) )
155 #define SPIFFS_PAGES_PER_BLOCK(fs) \
156 ( SPIFFS_CFG_LOG_BLOCK_SZ(fs)/SPIFFS_CFG_LOG_PAGE_SZ(fs) )
158 #define SPIFFS_OBJ_LOOKUP_PAGES(fs) \
159 (MAX(1, (SPIFFS_PAGES_PER_BLOCK(fs) * sizeof(spiffs_obj_id)) / SPIFFS_CFG_LOG_PAGE_SZ(fs)) )
161 #define SPIFFS_IS_LOOKUP_PAGE(fs,pix) \
162 (((pix) % SPIFFS_PAGES_PER_BLOCK(fs)) < SPIFFS_OBJ_LOOKUP_PAGES(fs))
164 #define SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) \
165 (SPIFFS_PAGES_PER_BLOCK(fs)-SPIFFS_OBJ_LOOKUP_PAGES(fs))
167 #define SPIFFS_BLOCK_TO_PADDR(fs, block) \
168 ( SPIFFS_CFG_PHYS_ADDR(fs) + (block)* SPIFFS_CFG_LOG_BLOCK_SZ(fs) )
170 #define SPIFFS_OBJ_LOOKUP_ENTRY_TO_PIX(fs, block, entry) \
171 ((block)*SPIFFS_PAGES_PER_BLOCK(fs) + (SPIFFS_OBJ_LOOKUP_PAGES(fs) + entry))
173 #define SPIFFS_OBJ_LOOKUP_ENTRY_TO_PADDR(fs, block, entry) \
174 (SPIFFS_BLOCK_TO_PADDR(fs, block) + (SPIFFS_OBJ_LOOKUP_PAGES(fs) + entry) * SPIFFS_CFG_LOG_PAGE_SZ(fs) )
176 #define SPIFFS_PAGE_TO_PADDR(fs, page) \
177 ( SPIFFS_CFG_PHYS_ADDR(fs) + (page) * SPIFFS_CFG_LOG_PAGE_SZ(fs) )
179 #define SPIFFS_PADDR_TO_PAGE(fs, addr) \
180 ( ((addr) - SPIFFS_CFG_PHYS_ADDR(fs)) / SPIFFS_CFG_LOG_PAGE_SZ(fs) )
182 #define SPIFFS_PADDR_TO_PAGE_OFFSET(fs, addr) \
183 ( ((addr) - SPIFFS_CFG_PHYS_ADDR(fs)) % SPIFFS_CFG_LOG_PAGE_SZ(fs) )
185 #define SPIFFS_BLOCK_FOR_PAGE(fs, page) \
186 ( (page) / SPIFFS_PAGES_PER_BLOCK(fs) )
188 #define SPIFFS_PAGE_FOR_BLOCK(fs, block) \
189 ( (block) * SPIFFS_PAGES_PER_BLOCK(fs) )
191 #define SPIFFS_OBJ_LOOKUP_ENTRY_FOR_PAGE(fs, page) \
192 ( (page) % SPIFFS_PAGES_PER_BLOCK(fs) - SPIFFS_OBJ_LOOKUP_PAGES(fs) )
194 #define SPIFFS_DATA_PAGE_SIZE(fs) \
195 ( SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_header) )
198 #define SPIFFS_ERASE_COUNT_PADDR(fs, bix) \
199 ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id) )
202 #define SPIFFS_MAGIC_PADDR(fs, bix) \
203 ( SPIFFS_BLOCK_TO_PADDR(fs, bix) + SPIFFS_OBJ_LOOKUP_PAGES(fs) * SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_obj_id)*2 )
205 #define SPIFFS_CHECK_MAGIC_POSSIBLE(fs) \
206 ( (SPIFFS_OBJ_LOOKUP_MAX_ENTRIES(fs) % (SPIFFS_CFG_LOG_PAGE_SZ(fs)/sizeof(spiffs_obj_id))) * sizeof(spiffs_obj_id) \
207 <= (SPIFFS_CFG_LOG_PAGE_SZ(fs)-sizeof(spiffs_obj_id)*2) )
212 #define SPIFFS_OBJ_HDR_IX_LEN(fs) \
213 ((SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix_header))/sizeof(spiffs_page_ix))
215 #define SPIFFS_OBJ_IX_LEN(fs) \
216 ((SPIFFS_CFG_LOG_PAGE_SZ(fs) - sizeof(spiffs_page_object_ix))/sizeof(spiffs_page_ix))
218 #define SPIFFS_OBJ_IX_ENTRY(fs, spix) \
219 ((spix) < SPIFFS_OBJ_HDR_IX_LEN(fs) ? (spix) : (((spix)-SPIFFS_OBJ_HDR_IX_LEN(fs))%SPIFFS_OBJ_IX_LEN(fs)))
221 #define SPIFFS_OBJ_IX_ENTRY_SPAN_IX(fs, spix) \
222 ((spix) < SPIFFS_OBJ_HDR_IX_LEN(fs) ? 0 : (1+((spix)-SPIFFS_OBJ_HDR_IX_LEN(fs))/SPIFFS_OBJ_IX_LEN(fs)))
225 #define SPIFFS_OP_T_OBJ_LU (0<<0)
226 #define SPIFFS_OP_T_OBJ_LU2 (1<<0)
227 #define SPIFFS_OP_T_OBJ_IX (2<<0)
228 #define SPIFFS_OP_T_OBJ_DA (3<<0)
229 #define SPIFFS_OP_C_DELE (0<<2)
230 #define SPIFFS_OP_C_UPDT (1<<2)
231 #define SPIFFS_OP_C_MOVS (2<<2)
232 #define SPIFFS_OP_C_MOVD (3<<2)
233 #define SPIFFS_OP_C_FLSH (4<<2)
234 #define SPIFFS_OP_C_READ (5<<2)
235 #define SPIFFS_OP_C_WRTHRU (6<<2)
237 #define SPIFFS_OP_TYPE_MASK (3<<0)
238 #define SPIFFS_OP_COM_MASK (7<<2)
242 #define SPIFFS_PH_FLAG_USED (1<<0)
244 #define SPIFFS_PH_FLAG_FINAL (1<<1)
246 #define SPIFFS_PH_FLAG_INDEX (1<<2)
248 #define SPIFFS_PH_FLAG_DELET (1<<7)
250 #define SPIFFS_PH_FLAG_IXDELE (1<<6)
253 #define SPIFFS_CHECK_MOUNT(fs) \
256 #define SPIFFS_CHECK_CFG(fs) \
257 ((fs)->config_magic == SPIFFS_CONFIG_MAGIC)
259 #define SPIFFS_CHECK_RES(res) \
261 if ((res) < SPIFFS_OK) return (res); \
264 #define SPIFFS_API_CHECK_MOUNT(fs) \
265 if (!SPIFFS_CHECK_MOUNT((fs))) { \
266 (fs)->err_code = SPIFFS_ERR_NOT_MOUNTED; \
270 #define SPIFFS_API_CHECK_CFG(fs) \
271 if (!SPIFFS_CHECK_CFG((fs))) { \
272 (fs)->err_code = SPIFFS_ERR_NOT_CONFIGURED; \
276 #define SPIFFS_API_CHECK_RES(fs, res) \
277 if ((res) < SPIFFS_OK) { \
278 (fs)->err_code = (res); \
282 #define SPIFFS_API_CHECK_RES_UNLOCK(fs, res) \
283 if ((res) < SPIFFS_OK) { \
284 (fs)->err_code = (res); \
289 #define SPIFFS_VALIDATE_OBJIX(ph, objid, spix) \
290 if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \
291 if (((ph).flags & SPIFFS_PH_FLAG_DELET) == 0) return SPIFFS_ERR_DELETED; \
292 if (((ph).flags & SPIFFS_PH_FLAG_FINAL) != 0) return SPIFFS_ERR_NOT_FINALIZED; \
293 if (((ph).flags & SPIFFS_PH_FLAG_INDEX) != 0) return SPIFFS_ERR_NOT_INDEX; \
294 if (((objid) & SPIFFS_OBJ_ID_IX_FLAG) == 0) return SPIFFS_ERR_NOT_INDEX; \
295 if ((ph).span_ix != (spix)) return SPIFFS_ERR_INDEX_SPAN_MISMATCH;
298 #define SPIFFS_VALIDATE_DATA(ph, objid, spix) \
299 if (((ph).flags & SPIFFS_PH_FLAG_USED) != 0) return SPIFFS_ERR_IS_FREE; \
300 if (((ph).flags & SPIFFS_PH_FLAG_DELET) == 0) return SPIFFS_ERR_DELETED; \
301 if (((ph).flags & SPIFFS_PH_FLAG_FINAL) != 0) return SPIFFS_ERR_NOT_FINALIZED; \
302 if (((ph).flags & SPIFFS_PH_FLAG_INDEX) == 0) return SPIFFS_ERR_IS_INDEX; \
303 if ((objid) & SPIFFS_OBJ_ID_IX_FLAG) return SPIFFS_ERR_IS_INDEX; \
304 if ((ph).span_ix != (spix)) return SPIFFS_ERR_DATA_SPAN_MISMATCH;
308 #define SPIFFS_VIS_CHECK_ID (1<<0)
310 #define SPIFFS_VIS_CHECK_PH (1<<1)
312 #define SPIFFS_VIS_NO_WRAP (1<<2)
314 #if SPIFFS_HAL_CALLBACK_EXTRA
316 #define SPIFFS_HAL_WRITE(_fs, _paddr, _len, _src) \
317 (_fs)->cfg.hal_write_f((_fs), (_paddr), (_len), (_src))
318 #define SPIFFS_HAL_READ(_fs, _paddr, _len, _dst) \
319 (_fs)->cfg.hal_read_f((_fs), (_paddr), (_len), (_dst))
320 #define SPIFFS_HAL_ERASE(_fs, _paddr, _len) \
321 (_fs)->cfg.hal_erase_f((_fs), (_paddr), (_len))
323 #else // SPIFFS_HAL_CALLBACK_EXTRA
325 #define SPIFFS_HAL_WRITE(_fs, _paddr, _len, _src) \
326 (_fs)->cfg.hal_write_f((_paddr), (_len), (_src))
327 #define SPIFFS_HAL_READ(_fs, _paddr, _len, _dst) \
328 (_fs)->cfg.hal_read_f((_paddr), (_len), (_dst))
329 #define SPIFFS_HAL_ERASE(_fs, _paddr, _len) \
330 (_fs)->cfg.hal_erase_f((_paddr), (_len))
332 #endif // SPIFFS_HAL_CALLBACK_EXTRA
336 #define SPIFFS_CACHE_FLAG_DIRTY (1<<0)
337 #define SPIFFS_CACHE_FLAG_WRTHRU (1<<1)
338 #define SPIFFS_CACHE_FLAG_OBJLU (1<<2)
339 #define SPIFFS_CACHE_FLAG_OBJIX (1<<3)
340 #define SPIFFS_CACHE_FLAG_DATA (1<<4)
341 #define SPIFFS_CACHE_FLAG_TYPE_WR (1<<7)
343 #define SPIFFS_CACHE_PAGE_SIZE(fs) \
344 (sizeof(spiffs_cache_page) + SPIFFS_CFG_LOG_PAGE_SZ(fs))
346 #define spiffs_get_cache(fs) \
347 ((spiffs_cache *)((fs)->cache))
349 #define spiffs_get_cache_page_hdr(fs, c, ix) \
350 ((spiffs_cache_page *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])))
352 #define spiffs_get_cache_page(fs, c, ix) \
353 ((u8_t *)(&((c)->cpages[(ix) * SPIFFS_CACHE_PAGE_SIZE(fs)])) + sizeof(spiffs_cache_page))
388 u32_t cpage_use_mask;
418 spiffs_cache_page *cache_page;
428 typedef struct __attribute(( packed )) {
430 spiffs_obj_id obj_id;
432 spiffs_span_ix span_ix;
435 } spiffs_page_header;
438 typedef struct __attribute(( packed ))
439 #if SPIFFS_ALIGNED_OBJECT_INDEX_TABLES
440 __attribute(( aligned(sizeof(spiffs_page_ix)) ))
444 spiffs_page_header p_hdr;
446 u8_t _align[4 - ((
sizeof(spiffs_page_header)&3)==0 ? 4 : (
sizeof(spiffs_page_header)&3))];
450 spiffs_obj_type type;
452 u8_t name[SPIFFS_OBJ_NAME_LEN];
453 } spiffs_page_object_ix_header;
456 typedef struct __attribute(( packed )) {
457 spiffs_page_header p_hdr;
458 u8_t _align[4 - ((
sizeof(spiffs_page_header)&3)==0 ? 4 : (
sizeof(spiffs_page_header)&3))];
459 } spiffs_page_object_ix;
462 typedef s32_t (*spiffs_visitor_f)(
spiffs *
fs, spiffs_obj_id id, spiffs_block_ix bix,
int ix_entry,
463 const void *user_const_p,
void *user_var_p);
467 #define _spiffs_rd(fs, op, fh, addr, len, dst) \
468 spiffs_phys_rd((fs), (op), (fh), (addr), (len), (dst))
469 #define _spiffs_wr(fs, op, fh, addr, len, src) \
470 spiffs_phys_wr((fs), (op), (fh), (addr), (len), (src))
472 #define _spiffs_rd(fs, op, fh, addr, len, dst) \
473 spiffs_phys_rd((fs), (addr), (len), (dst))
474 #define _spiffs_wr(fs, op, fh, addr, len, src) \
475 spiffs_phys_wr((fs), (addr), (len), (src))
479 #define MIN(a,b) ((a) < (b) ? (a) : (b))
482 #define MAX(a,b) ((a) > (b) ? (a) : (b))
487 s32_t spiffs_phys_rd(
497 s32_t spiffs_phys_wr(
507 s32_t spiffs_phys_cpy(
514 s32_t spiffs_phys_count_free_blocks(
517 s32_t spiffs_obj_lu_find_entry_visitor(
519 spiffs_block_ix starting_block,
520 int starting_lu_entry,
522 spiffs_obj_id obj_id,
524 const void *user_const_p,
526 spiffs_block_ix *block_ix,
529 s32_t spiffs_erase_block(
531 spiffs_block_ix bix);
535 s32_t spiffs_obj_lu_scan(
538 s32_t spiffs_obj_lu_find_free_obj_id(
540 spiffs_obj_id *obj_id,
541 const u8_t *conflicting_name);
543 s32_t spiffs_obj_lu_find_free(
545 spiffs_block_ix starting_block,
546 int starting_lu_entry,
547 spiffs_block_ix *block_ix,
550 s32_t spiffs_obj_lu_find_id(
552 spiffs_block_ix starting_block,
553 int starting_lu_entry,
554 spiffs_obj_id obj_id,
555 spiffs_block_ix *block_ix,
558 s32_t spiffs_obj_lu_find_id_and_span(
560 spiffs_obj_id obj_id,
562 spiffs_page_ix exclusion_pix,
563 spiffs_page_ix *pix);
565 s32_t spiffs_obj_lu_find_id_and_span_by_phdr(
567 spiffs_obj_id obj_id,
569 spiffs_page_ix exclusion_pix,
570 spiffs_page_ix *pix);
574 s32_t spiffs_page_allocate_data(
576 spiffs_obj_id obj_id,
577 spiffs_page_header *ph,
582 spiffs_page_ix *pix);
584 s32_t spiffs_page_move(
588 spiffs_obj_id obj_id,
589 spiffs_page_header *page_hdr,
590 spiffs_page_ix src_pix,
591 spiffs_page_ix *dst_pix);
593 s32_t spiffs_page_delete(
599 s32_t spiffs_object_create(
601 spiffs_obj_id obj_id,
602 const u8_t name[SPIFFS_OBJ_NAME_LEN],
603 spiffs_obj_type type,
604 spiffs_page_ix *objix_hdr_pix);
606 s32_t spiffs_object_update_index_hdr(
609 spiffs_obj_id obj_id,
610 spiffs_page_ix objix_hdr_pix,
611 u8_t *new_objix_hdr_data,
612 const u8_t name[SPIFFS_OBJ_NAME_LEN],
614 spiffs_page_ix *new_pix);
616 void spiffs_cb_object_event(
620 spiffs_obj_id obj_id,
622 spiffs_page_ix new_pix,
625 s32_t spiffs_object_open_by_id(
627 spiffs_obj_id obj_id,
632 s32_t spiffs_object_open_by_page(
639 s32_t spiffs_object_append(
645 s32_t spiffs_object_modify(
651 s32_t spiffs_object_read(
657 s32_t spiffs_object_truncate(
662 s32_t spiffs_object_find_object_index_header_by_name(
664 const u8_t name[SPIFFS_OBJ_NAME_LEN],
665 spiffs_page_ix *pix);
669 s32_t spiffs_gc_check(
673 s32_t spiffs_gc_erase_page_stats(
675 spiffs_block_ix bix);
677 s32_t spiffs_gc_find_candidate(
679 spiffs_block_ix **block_candidate,
680 int *candidate_count,
683 s32_t spiffs_gc_clean(
685 spiffs_block_ix bix);
687 s32_t spiffs_gc_quick(
692 s32_t spiffs_fd_find_new(
696 s32_t spiffs_fd_return(
706 void spiffs_cache_init(
709 void spiffs_cache_drop_page(
714 spiffs_cache_page *spiffs_cache_page_allocate_by_fd(
718 void spiffs_cache_fd_release(
720 spiffs_cache_page *cp);
722 spiffs_cache_page *spiffs_cache_page_get_by_fd(
728 s32_t spiffs_lookup_consistency_check(
730 u8_t check_all_objects);
732 s32_t spiffs_page_consistency_check(
735 s32_t spiffs_object_index_consistency_check(
u32_t size
Definition: spiffs.h:271
u32_t offset
Definition: spiffs_nucleus.h:412
spiffs_span_ix cursor_objix_spix
Definition: spiffs_nucleus.h:410
u32_t size
Definition: spiffs_nucleus.h:404
spiffs_file file_nbr
Definition: spiffs_nucleus.h:400
spiffs_page_ix objix_hdr_pix
Definition: spiffs_nucleus.h:406
Definition: ESP8266WebServer.h:55
spiffs_page_ix cursor_objix_pix
Definition: spiffs_nucleus.h:408
spiffs * fs
Definition: spiffs_nucleus.h:398
spiffs_page_ix pix
Definition: spiffs.h:272
spiffs_obj_id obj_id
Definition: spiffs_nucleus.h:402
spiffs_flags flags
Definition: spiffs_nucleus.h:416
Definition: spiffs_nucleus.h:396
u32_t fdoffset
Definition: spiffs_nucleus.h:414
spiffs_obj_id obj_id
Definition: spiffs.h:268