Skip to content

Commit cb36eba

Browse files
committed
kv gc bugfix
if a power down or crash happened during gc process, kv_init will fail with NO_WRITEABLE_BLOK next time.
1 parent fb7715a commit cb36eba

File tree

3 files changed

+215
-35
lines changed

3 files changed

+215
-35
lines changed

components/fs/kv/include/tos_kv.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,21 @@ typedef uint64_t kv_dword_t; // double word
101101
#define KV_ITEM_ADDR_OF_BODY(item) (item->pos + KV_ITEM_HDR_SIZE)
102102

103103
#define KV_BLK_HDR_MAGIC 0x1234ABCD4321DCBA
104+
#define KV_BLK_HDR_GC_SRC 0x5678DCBA8765ABCD
105+
#define KV_BLK_HDR_GC_DST 0x8765ABCD8765DCBA
106+
#define KV_BLK_HDR_GC_DONE 0x4321DCBA1234ABCD
107+
104108
#define KV_BLK_IS_LEGAL(blk_hdr) ((blk_hdr)->magic == KV_BLK_HDR_MAGIC)
109+
110+
// DO NOT use gc_src == KV_BLK_HDR_GC_SRC here, in case of an incomplete write of src magic due to a power down
111+
#define KV_BLK_IS_GC_SRC(blk_hdr) ((blk_hdr)->gc_src != (kv_dword_t)-1)
112+
// DO NOT use gc_dst == KV_BLK_HDR_GC_DST here, in case of an incomplete write of dst magic due to a power down
113+
#define KV_BLK_IS_GC_DST(blk_hdr) ((blk_hdr)->gc_dst != (kv_dword_t)-1)
114+
// DO NOT use gc_done == KV_BLK_HDR_GC_DONE here, in case of an incomplete write of done magic due to a power down
115+
#define KV_BLK_IS_GC_DONE(blk_hdr) ((blk_hdr)->gc_done != (kv_dword_t)-1)
116+
117+
#define KV_BLK_IS_GC_DST_NOT_DONE(blk_hdr) (KV_BLK_IS_GC_DST(blk_hdr) && !KV_BLK_IS_GC_DONE(blk_hdr))
118+
105119
#define KV_BLK_INVALID ((uint32_t)-1)
106120
#define KV_BLK_HDR_SIZE KV_ALIGNED_SIZE(sizeof(kv_blk_hdr_t))
107121
#define KV_BLK_SIZE (KV_FLASH_SECTOR_SIZE)
@@ -113,7 +127,7 @@ typedef uint64_t kv_dword_t; // double word
113127
#define KV_BLK_NEXT(blk_start) (blk_start + KV_BLK_SIZE >= KV_FLASH_END ? KV_FLASH_START : blk_start + KV_BLK_SIZE)
114128

115129
#define KV_BLK_FOR_EACH_FROM(cur_blk, start_blk) \
116-
for (cur_blk = KV_BLK_NEXT(start_blk); \
130+
for (cur_blk = KV_BLK_NEXT(start_blk); \
117131
cur_blk != start_blk; \
118132
cur_blk = KV_BLK_NEXT(cur_blk))
119133

@@ -159,6 +173,9 @@ typedef struct kv_control_st {
159173

160174
typedef struct kv_block_header_st {
161175
kv_wunit_t magic; /*< is this block formatted? */
176+
kv_wunit_t gc_src; /*< is this block gc-ing(as a source block)? */
177+
kv_wunit_t gc_dst; /*< is this block gc-ing(as a destination block)? */
178+
kv_wunit_t gc_done; /*< is this block gc done(as a destination block)? */
162179
} __PACKED__ kv_blk_hdr_t;
163180

164181
typedef struct kv_item_header_st {

0 commit comments

Comments
 (0)