-
Notifications
You must be signed in to change notification settings - Fork 1
/
fvs.c
executable file
·371 lines (297 loc) · 8.64 KB
/
fvs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/** Flash Variable System
*
* Implementations. This is part of FVS project
*/
#include <string.h>
#include <rtthread.h>
#include "fvs.h"
#define ASSERT RT_ASSERT
#define FVS_VN_STATUS_EMPTY ((fvs_native_t)-1)
#define FVS_VN_STATUS_WRITTEN ((fvs_native_t)0x0)
static rt_err_t vn_do_create(
rt_uint8_t *base_addr,
struct fvs_vnode *node,
fvs_id_t id,
fvs_size_t size);
static rt_err_t vn_fill_data(
rt_uint8_t *base_addr,
struct fvs_vnode *node,
void* data);
rt_inline struct fvs_vnode* vn_next(struct fvs_vnode *node)
{
return (struct fvs_vnode*)((char*)node + sizeof(*node) + node->size);
}
rt_inline int vn_is_valid(struct fvs_vnode *node)
{
return node->id != 0;
}
rt_inline void blk_mark_as_using(
rt_uint8_t *base_addr,
size_t size)
{
struct fvs_vnode *node = (struct fvs_vnode*)(base_addr + size);
fvs_verbose("FVS: mark page 0x%p as using\n", base_addr);
fvs_begin_write(base_addr);
fvs_native_write_r((void*)&node->status, 0);
fvs_end_write(base_addr);
}
rt_inline rt_bool_t blk_page_inuse(
const struct fvs_block *page,
int idx)
{
struct fvs_vnode *node = (struct fvs_vnode*)(page->pages[idx] + page->size);
return node->status == 0;
}
rt_inline rt_uint8_t* blk_find_using(
const struct fvs_block *page)
{
int i;
for (i = 0; i < FVS_BLK_PAGE_NR; i++)
{
if (blk_page_inuse(page, i))
return page->pages[i];
}
return RT_NULL;
}
rt_err_t blk_roll_pages(const struct fvs_block *blk)
{
struct fvs_vnode *node;
rt_uint8_t *using_page, *empty_page, *ptr;
using_page = blk_find_using(blk);
ASSERT(using_page);
if (using_page == blk->pages[0])
empty_page = blk->pages[1];
else
empty_page = blk->pages[0];
fvs_verbose("FVS: rolling pages: from(0x%x), to(0x%x)\n",
using_page, empty_page);
ptr = empty_page;
for (node = (struct fvs_vnode*)using_page;
node->id != FVS_END_OF_ID;
node = vn_next(node)) {
if (!vn_is_valid(node))
continue;
vn_do_create((rt_uint8_t*)empty_page,
(struct fvs_vnode*)ptr, node->id, node->size);
vn_fill_data((rt_uint8_t*)empty_page,
(struct fvs_vnode*)ptr, node+1);
ptr += sizeof(struct fvs_vnode) + node->size;
}
/* mark the empty page as using */
blk_mark_as_using(empty_page, blk->size);
/* erase the old page in the last */
fvs_begin_write(using_page);
fvs_erase_page(using_page);
fvs_end_write(using_page);
return RT_EOK;
}
static rt_err_t vn_do_create(
rt_uint8_t *base_addr,
struct fvs_vnode *node,
fvs_id_t id,
fvs_size_t size)
{
fvs_begin_write(base_addr);
fvs_verbose("FVS: do create vnode on 0x%p, ", node);
fvs_verbose("id: %d, size %d\n", id, size);
fvs_native_write_r((void*)&node->id, id);
fvs_native_write_r((void*)&node->size, size);
fvs_native_write_r((void*)&node->status, FVS_VN_STATUS_EMPTY);
fvs_end_write(base_addr);
return RT_EOK;
}
static void vn_mark_written(
rt_uint8_t *base_addr,
struct fvs_vnode *node)
{
fvs_begin_write(base_addr);
fvs_verbose("FVS: mark 0x%p as written, ", node);
fvs_verbose("id: %d, size %d\n", node->id, node->size);
fvs_native_write_r((void*)&node->status, FVS_VN_STATUS_WRITTEN);
fvs_end_write(base_addr);
}
static int vn_is_empty(struct fvs_vnode *node)
{
return node->status == FVS_VN_STATUS_EMPTY;
}
static void vn_mark_invalid(
rt_uint8_t *base_addr,
struct fvs_vnode *node)
{
fvs_begin_write(base_addr);
fvs_verbose("FVS: mark 0x%p as invalid, ", node);
fvs_verbose("id: %d, size %d\n", node->id, node->size);
fvs_native_write_r((void*)&node->id, 0);
fvs_end_write(base_addr);
}
static struct fvs_vnode *vn_find(
rt_uint8_t *base_addr,
size_t page_sz,
fvs_id_t id,
size_t size)
{
struct fvs_vnode *node;
ASSERT(base_addr);
/* return the pointer to data if it has been created. */
for (node = (struct fvs_vnode*)base_addr;
node->id != FVS_END_OF_ID;
node = vn_next(node)) {
fvs_debug("FVS: vn_found node id:%d, size: %d\n", node->id, node->size);
ASSERT((char*)node < (char*)(base_addr) + page_sz);
if ((node->id) == id && node->size == size)
return node;
}
return node;
}
size_t fvs_page_used_size(const struct fvs_block *blk)
{
size_t s = 0;
struct fvs_vnode *node;
for (node = (struct fvs_vnode*)blk->pages;
node->id != FVS_END_OF_ID;
node = vn_next(node))
{
if (!vn_is_valid(node))
continue;
s += node->size;
}
return s;
}
rt_bool_t fvs_page_used(const struct fvs_block *blk)
{
/* if there no blk we are using, the page would be never be written. */
return blk_find_using(blk) != RT_NULL;
}
void *fvs_vnode_get(const struct fvs_block *blk, fvs_id_t id, size_t size)
{
struct fvs_vnode *node;
rt_uint8_t *base_addr;
int s;
ASSERT(blk);
ASSERT(id);
ASSERT(id != FVS_END_OF_ID);
/* the size of the data should be multiple of fvs_native_t */
ASSERT((size & (sizeof(fvs_native_t)-1)) == 0);
base_addr = blk_find_using(blk);
/* empty block, use the first page */
if (base_addr == RT_NULL)
{
base_addr = blk->pages[0];
blk_mark_as_using(base_addr, blk->size);
}
/* return the pointer to data if it has been created. */
node = vn_find(base_addr, blk->size, id, size);
if (node->id != FVS_END_OF_ID)
return node+1;
/* there is enough space to create the node we need. */
if ((rt_uint8_t*)(node+1) + size <= base_addr + blk->size)
{
vn_do_create(base_addr, node, id, size);
return node+1;
}
s = 0;
for (node = (struct fvs_vnode*)base_addr;
node->id != FVS_END_OF_ID;
node = vn_next(node))
{
if (!vn_is_valid(node))
continue;
s += sizeof(*node) + node->size;
}
if (s + sizeof(*node) + size > blk->size)
/* we run out of luck */
return NULL;
blk_roll_pages(blk);
/* refresh the base_addr as the using page is changed */
base_addr = blk_find_using(blk);
ASSERT(base_addr);
/* return the pointer to data if it has been created. */
node = vn_find(base_addr, blk->size, id, size);
ASSERT(node->id == FVS_END_OF_ID);
ASSERT((rt_uint8_t*)(node+1) + size <= base_addr + blk->size);
vn_do_create(base_addr, node, id, size);
return node+1;
}
static rt_err_t vn_fill_data(
rt_uint8_t *base_addr,
struct fvs_vnode *node,
void* data)
{
ASSERT(node);
ASSERT(data);
ASSERT(node->status == FVS_VN_STATUS_EMPTY);
fvs_verbose("FVS: fill node 0x%p with data from 0x%p, ",
node, data);
fvs_verbose("id: %d, size: %d\n", node->id, node->size);
fvs_begin_write(base_addr);
fvs_native_write_m(node+1, data, node->size);
vn_mark_written(base_addr, node);
fvs_end_write(base_addr);
return RT_EOK;
}
void fvs_vnode_delete(const struct fvs_block *blk, fvs_id_t id, fvs_size_t size)
{
struct fvs_vnode *node;
rt_uint8_t *base_addr;
ASSERT(blk);
ASSERT(id != FVS_END_OF_ID);
base_addr = blk_find_using(blk);
if (!base_addr)
return;
node = vn_find(base_addr, blk->size, id, size);
if (node->id == FVS_END_OF_ID)
return;
fvs_verbose("FVS: delete node 0x%p, ", node);
fvs_verbose("id: %d, size: %d\n", id, size);
vn_mark_invalid(base_addr, node);
}
rt_err_t fvs_vnode_write(const struct fvs_block *blk, fvs_id_t id, fvs_size_t size, void *data)
{
struct fvs_vnode *node, *new_node;
rt_uint8_t *base_addr;
ASSERT(blk);
ASSERT(id != FVS_END_OF_ID);
base_addr = blk_find_using(blk);
if (!base_addr)
return -RT_ERROR;
node = vn_find(base_addr, blk->size, id, size);
if (node->id == FVS_END_OF_ID)
return -RT_ERROR;
/* find the fresh node if possible. */
if (vn_is_empty(node)) {
fvs_verbose("FVS: first write on node 0x%p, ", node);
fvs_verbose("id: %d, size: %d\n", id, size);
vn_fill_data(base_addr, node, data);
return RT_EOK;
}
/* if the content does not change, there is nothing to do. */
if (rt_memcmp(node+1, data, size) == 0) {
fvs_verbose("FVS: write old data on node 0x%p\n", node);
return RT_EOK;
}
new_node = vn_find(base_addr, blk->size, FVS_END_OF_ID, (fvs_size_t)-1);
/* we need rewrite the whole blk since there is no free node left. The
* other page will be able to contain all the nodes since we have had that
* node in this page. (We will return -RT_ERROR on node not found.) */
if ((rt_uint8_t*)(new_node+1) + size > base_addr + blk->size) {
fvs_verbose("FVS: rewrite whole blk 0x%p, page 0x%p ", blk, base_addr);
fvs_verbose("id: %d, size: %d\n", id, size);
/* delete and write back */
fvs_vnode_delete(blk, id, size);
blk_roll_pages(blk);
new_node = fvs_vnode_get(blk, id, size);
ASSERT(new_node);
/* fvs_vnode_get return the data address and we need to rewind it */
new_node -= 1;
vn_fill_data(base_addr, new_node, data);
} else {
fvs_verbose("FVS: write to new node:0x%p, old node:0x%p, ",
new_node, node);
fvs_verbose("id: %d, size: %d\n", id, size);
/* create the new node before mark the old one as invalid */
vn_do_create(base_addr, new_node, id, size);
vn_fill_data(base_addr, new_node, data);
vn_mark_invalid(base_addr, node);
}
return RT_EOK;
}