forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDBStore.cpp
1512 lines (1250 loc) · 44.1 KB
/
TDBStore.cpp
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// ----------------------------------------------------------- Includes -----------------------------------------------------------
#include "tdbstore/TDBStore.h"
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include "mbed_error.h"
#include "mbed_assert.h"
#include "mbed_wait_api.h"
#include "MbedCRC.h"
#include "FlashIAP.h"
using namespace mbed;
// --------------------------------------------------------- Definitions ----------------------------------------------------------
static const uint32_t delete_flag = (1UL << 31);
static const uint32_t internal_flags = delete_flag;
// Only write once flag is supported, other two are kept in storage but ignored
static const uint32_t supported_flags = KVStore::WRITE_ONCE_FLAG | KVStore::REQUIRE_CONFIDENTIALITY_FLAG | KVStore::REQUIRE_REPLAY_PROTECTION_FLAG;
namespace {
typedef struct {
uint32_t magic;
uint16_t header_size;
uint16_t revision;
uint32_t flags;
uint16_t key_size;
uint16_t reserved;
uint32_t data_size;
uint32_t crc;
} record_header_t;
typedef struct {
uint32_t hash;
bd_size_t bd_offset;
} ram_table_entry_t;
static const char *master_rec_key = "TDBS";
static const uint32_t tdbstore_magic = 0x54686683;
static const uint32_t tdbstore_revision = 1;
typedef struct {
uint16_t version;
uint16_t tdbstore_revision;
uint32_t reserved;
} master_record_data_t;
typedef enum {
TDBSTORE_AREA_STATE_NONE = 0,
TDBSTORE_AREA_STATE_ERASED,
TDBSTORE_AREA_STATE_INVALID,
TDBSTORE_AREA_STATE_VALID,
} area_state_e;
typedef struct {
uint16_t trailer_size;
uint16_t data_size;
uint32_t crc;
} reserved_trailer_t;
static const size_t min_work_buf_size = 64;
static const uint32_t initial_crc = 0xFFFFFFFF;
static const uint32_t initial_max_keys = 16;
// incremental set handle
typedef struct {
record_header_t header;
bd_size_t bd_base_offset;
bd_size_t bd_curr_offset;
uint32_t offset_in_data;
uint32_t ram_table_ind;
uint32_t hash;
bool new_key;
} inc_set_handle_t;
// iterator handle
typedef struct {
int iterator_num;
uint32_t ram_table_ind;
char *prefix;
} key_iterator_handle_t;
} // anonymous namespace
// -------------------------------------------------- Local Functions Declaration ----------------------------------------------------
// -------------------------------------------------- Functions Implementation ----------------------------------------------------
static inline uint32_t align_up(uint32_t val, uint32_t size)
{
return (((val - 1) / size) + 1) * size;
}
static inline uint32_t align_down(uint64_t val, uint64_t size)
{
return (((val) / size)) * size;
}
static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data_buf)
{
uint32_t crc;
MbedCRC<POLY_32BIT_ANSI, 32> ct(init_crc, 0x0, true, false);
ct.compute(data_buf, data_size, &crc);
return crc;
}
// Class member functions
TDBStore::TDBStore(BlockDevice *bd) : _ram_table(0), _max_keys(0),
_num_keys(0), _bd(bd), _buff_bd(0), _free_space_offset(0), _master_record_offset(0),
_master_record_size(0), _is_initialized(false), _active_area(0), _active_area_version(0), _size(0),
_area_params{}, _prog_size(0), _work_buf(0), _work_buf_size(0), _key_buf(0), _inc_set_handle(0)
{
for (int i = 0; i < _num_areas; i++) {
_area_params[i] = { 0 };
}
for (int i = 0; i < _max_open_iterators; i++) {
_iterator_table[i] = { 0 };
}
}
TDBStore::~TDBStore()
{
deinit();
}
int TDBStore::read_area(uint8_t area, uint32_t offset, uint32_t size, void *buf)
{
//Check that we are not crossing area boundary
if (offset + size > _size) {
return MBED_ERROR_READ_FAILED;
}
int os_ret = _buff_bd->read(buf, _area_params[area].address + offset, size);
if (os_ret) {
return MBED_ERROR_READ_FAILED;
}
return MBED_SUCCESS;
}
int TDBStore::write_area(uint8_t area, uint32_t offset, uint32_t size, const void *buf)
{
int os_ret = _buff_bd->program(buf, _area_params[area].address + offset, size);
if (os_ret) {
return MBED_ERROR_WRITE_FAILED;
}
return MBED_SUCCESS;
}
int TDBStore::erase_area(uint8_t area, uint32_t offset, uint32_t size)
{
uint32_t bd_offset = _area_params[area].address + offset;
int ret = _buff_bd->erase(bd_offset, size);
if (ret) {
return ret;
}
if (_buff_bd->get_erase_value() == -1) {
// We need to simulate erase to wipe records, as our block device
// may not do it. Program in chunks of _work_buf_size if the minimum
// program size is too small (e.g. one-byte) to avoid performance
// issues.
MBED_ASSERT(_work_buf != nullptr);
MBED_ASSERT(_work_buf_size != 0);
memset(_work_buf, 0xFF, _work_buf_size);
while (size) {
uint32_t chunk = std::min<uint32_t>(_work_buf_size, size);
ret = _buff_bd->program(_work_buf, bd_offset, chunk);
if (ret) {
return ret;
}
size -= chunk;
bd_offset += chunk;
}
}
return MBED_SUCCESS;
}
void TDBStore::calc_area_params()
{
// TDBStore can't exceed 32 bits
bd_size_t bd_size = std::min<bd_size_t>(_bd->size(), 0x80000000L);
memset(_area_params, 0, sizeof(_area_params));
size_t area_0_size = 0;
size_t area_1_size = 0;
// The size calculations are a bit complex because we need to make sure we're
// always aligned to an erase block boundary (whose size may not be uniform
// across the address space), and we also need to make sure that the first
// area never goes over half of the total size even if bd_size is an odd
// number of sectors.
while (true) {
bd_size_t erase_unit_size = _bd->get_erase_size(area_0_size);
if (area_0_size + erase_unit_size <= (bd_size / 2)) {
area_0_size += erase_unit_size;
} else {
break;
}
}
while (true) {
bd_size_t erase_unit_size = _bd->get_erase_size(area_0_size + area_1_size);
if (area_1_size + erase_unit_size <= (bd_size / 2)) {
area_1_size += erase_unit_size;
} else {
break;
}
}
_area_params[0].address = 0;
_area_params[0].size = area_0_size;
_area_params[1].address = area_0_size;
_area_params[1].size = area_1_size;
// The areas must be of same size
MBED_ASSERT(_area_params[0].size == _area_params[1].size);
}
// This function, reading a record from the BD, is used for multiple purposes:
// - Init (scan all records, no need to return file name and data)
// - Get (return file data)
// - Get first/next file (check whether name matches, return name if so)
int TDBStore::read_record(uint8_t area, uint32_t offset, char *key,
void *data_buf, uint32_t data_buf_size,
uint32_t &actual_data_size, size_t data_offset, bool copy_key,
bool copy_data, bool check_expected_key, bool calc_hash,
uint32_t &hash, uint32_t &flags, uint32_t &next_offset)
{
int ret;
record_header_t header;
uint32_t total_size, key_size, data_size;
uint32_t curr_data_offset;
char *user_key_ptr;
uint32_t crc = initial_crc;
// Upper layers typically use non zero offsets for reading the records chunk by chunk,
// so only validate entire record at first chunk (otherwise we'll have a serious performance penalty).
bool validate = (data_offset == 0);
ret = MBED_SUCCESS;
// next offset should only be updated to the end of record if successful
next_offset = offset;
ret = read_area(area, offset, sizeof(header), &header);
if (ret) {
return ret;
}
if (header.magic != tdbstore_magic) {
return MBED_ERROR_INVALID_DATA_DETECTED;
}
offset += align_up(sizeof(header), _prog_size);
key_size = header.key_size;
data_size = header.data_size;
flags = header.flags;
if ((!key_size) || (key_size >= MAX_KEY_SIZE)) {
return MBED_ERROR_INVALID_DATA_DETECTED;
}
total_size = key_size + data_size;
// Make sure our read sizes didn't cause any wraparounds
if ((total_size < key_size) || (total_size < data_size)) {
return MBED_ERROR_INVALID_DATA_DETECTED;
}
if (offset + total_size >= _size) {
return MBED_ERROR_INVALID_DATA_DETECTED;
}
if (data_offset > data_size) {
return MBED_ERROR_INVALID_SIZE;
}
actual_data_size = std::min<size_t>(data_buf_size, data_size - data_offset);
if (copy_data && actual_data_size && !data_buf) {
return MBED_ERROR_INVALID_ARGUMENT;
}
if (validate) {
// Calculate CRC on header (excluding CRC itself)
crc = calc_crc(crc, sizeof(record_header_t) - sizeof(crc), &header);
curr_data_offset = 0;
} else {
// Non validation case: No need to read the key, nor the parts before data_offset
// or after the actual part requested by the user.
total_size = actual_data_size;
curr_data_offset = data_offset;
offset += data_offset + key_size;
// Mark code that key handling is finished
key_size = 0;
}
user_key_ptr = key;
hash = initial_crc;
while (total_size) {
uint8_t *dest_buf;
uint32_t chunk_size;
if (key_size) {
// This means that we're on the key part
if (copy_key) {
dest_buf = reinterpret_cast<uint8_t *>(user_key_ptr);
chunk_size = key_size;
user_key_ptr[key_size] = '\0';
} else {
dest_buf = _work_buf;
chunk_size = std::min<size_t>(key_size, _work_buf_size);
}
} else {
// This means that we're on the data part
// We have four cases that need different handling:
// 1. Before data_offset - read to work buffer
// 2. After data_offset, but before actual part is finished - read to user buffer
// 3. After actual part is finished - read to work buffer
// 4. Copy data flag not set - read to work buffer
if (curr_data_offset < data_offset) {
chunk_size = std::min<size_t>(_work_buf_size, (data_offset - curr_data_offset));
dest_buf = _work_buf;
} else if (copy_data && (curr_data_offset < data_offset + actual_data_size)) {
chunk_size = actual_data_size;
dest_buf = static_cast<uint8_t *>(data_buf);
} else {
chunk_size = std::min<size_t>(_work_buf_size, total_size);
dest_buf = _work_buf;
}
}
ret = read_area(area, offset, chunk_size, dest_buf);
if (ret) {
goto end;
}
if (validate) {
// calculate CRC on current read chunk
crc = calc_crc(crc, chunk_size, dest_buf);
}
if (key_size) {
// We're on key part. May need to calculate hash or check whether key is the expected one
if (check_expected_key) {
if (memcmp(user_key_ptr, dest_buf, chunk_size)) {
ret = MBED_ERROR_ITEM_NOT_FOUND;
}
}
if (calc_hash) {
hash = calc_crc(hash, chunk_size, dest_buf);
}
user_key_ptr += chunk_size;
key_size -= chunk_size;
if (!key_size) {
offset += data_offset;
}
} else {
curr_data_offset += chunk_size;
}
total_size -= chunk_size;
offset += chunk_size;
}
if (validate && (crc != header.crc)) {
ret = MBED_ERROR_INVALID_DATA_DETECTED;
goto end;
}
next_offset = align_up(offset, _prog_size);
end:
return ret;
}
int TDBStore::find_record(uint8_t area, const char *key, uint32_t &offset,
uint32_t &ram_table_ind, uint32_t &hash)
{
ram_table_entry_t *ram_table = (ram_table_entry_t *) _ram_table;
ram_table_entry_t *entry;
int ret = MBED_ERROR_ITEM_NOT_FOUND;
uint32_t actual_data_size;
uint32_t flags, dummy_hash, next_offset;
hash = calc_crc(initial_crc, strlen(key), key);
for (ram_table_ind = 0; ram_table_ind < _num_keys; ram_table_ind++) {
entry = &ram_table[ram_table_ind];
offset = entry->bd_offset;
if (hash < entry->hash) {
continue;
}
if (hash > entry->hash) {
return MBED_ERROR_ITEM_NOT_FOUND;
}
ret = read_record(_active_area, offset, const_cast<char *>(key), 0, 0, actual_data_size, 0,
false, false, true, false, dummy_hash, flags, next_offset);
// not found return code here means that hash doesn't belong to name. Continue searching.
if (ret != MBED_ERROR_ITEM_NOT_FOUND) {
break;
}
}
return ret;
}
uint32_t TDBStore::record_size(const char *key, uint32_t data_size)
{
return align_up(sizeof(record_header_t), _prog_size) +
align_up(strlen(key) + data_size, _prog_size);
}
int TDBStore::set_start(set_handle_t *handle, const char *key, size_t final_data_size,
uint32_t create_flags)
{
int ret;
uint32_t offset = 0;
uint32_t hash = 0, ram_table_ind = 0;
inc_set_handle_t *ih;
bool need_gc = false;
if (!is_valid_key(key)) {
return MBED_ERROR_INVALID_ARGUMENT;
}
if (create_flags & ~(supported_flags | internal_flags)) {
return MBED_ERROR_INVALID_ARGUMENT;
}
*handle = reinterpret_cast<set_handle_t>(_inc_set_handle);
ih = reinterpret_cast<inc_set_handle_t *>(*handle);
if (!strcmp(key, master_rec_key)) {
// Master record - special case (no need to protect by the mutex, as it is already covered
// in the upper layers).
ih->bd_base_offset = _master_record_offset;
ih->new_key = false;
ram_table_ind = 0;
hash = 0;
} else {
_mutex.lock();
// A valid magic in the header means that this function has been called after an aborted
// incremental set process. This means that our media may be in a bad state - call GC.
if (ih->header.magic == tdbstore_magic) {
ret = garbage_collection();
if (ret) {
goto fail;
}
}
// If we have no room for the record, perform garbage collection
uint32_t rec_size = record_size(key, final_data_size);
if (_free_space_offset + rec_size > _size) {
ret = garbage_collection();
if (ret) {
goto fail;
}
}
// If even after GC we have no room for the record, return error
if (_free_space_offset + rec_size > _size) {
ret = MBED_ERROR_MEDIA_FULL;
goto fail;
}
ret = find_record(_active_area, key, offset, ram_table_ind, hash);
if (ret == MBED_SUCCESS) {
ret = read_area(_active_area, offset, sizeof(ih->header), &ih->header);
if (ret) {
goto fail;
}
if (ih->header.flags & WRITE_ONCE_FLAG) {
ret = MBED_ERROR_WRITE_PROTECTED;
goto fail;
}
ih->new_key = false;
} else if (ret == MBED_ERROR_ITEM_NOT_FOUND) {
if (create_flags & delete_flag) {
goto fail;
}
if (_num_keys >= _max_keys) {
increment_max_keys();
}
ih->new_key = true;
} else {
goto fail;
}
ih->bd_base_offset = _free_space_offset;
check_erase_before_write(_active_area, ih->bd_base_offset, rec_size);
}
ret = MBED_SUCCESS;
// Fill handle and header fields
// Jump to offset after header (header will be written at finalize phase)
ih->bd_curr_offset = ih->bd_base_offset + align_up(sizeof(record_header_t), _prog_size);
ih->offset_in_data = 0;
ih->hash = hash;
ih->ram_table_ind = ram_table_ind;
ih->header.magic = tdbstore_magic;
ih->header.header_size = sizeof(record_header_t);
ih->header.revision = tdbstore_revision;
ih->header.flags = create_flags;
ih->header.key_size = strlen(key);
ih->header.reserved = 0;
ih->header.data_size = final_data_size;
// Calculate CRC on header and key
ih->header.crc = calc_crc(initial_crc, sizeof(record_header_t) - sizeof(ih->header.crc), &ih->header);
ih->header.crc = calc_crc(ih->header.crc, ih->header.key_size, key);
// Write key now
ret = write_area(_active_area, ih->bd_curr_offset, ih->header.key_size, key);
if (ret) {
need_gc = true;
goto fail;
}
ih->bd_curr_offset += ih->header.key_size;
goto end;
fail:
if ((need_gc) && (ih->bd_base_offset != _master_record_offset)) {
garbage_collection();
}
// mark handle as invalid by clearing magic field in header
ih->header.magic = 0;
_mutex.unlock();
end:
return ret;
}
int TDBStore::set_add_data(set_handle_t handle, const void *value_data, size_t data_size)
{
int ret = MBED_SUCCESS;
inc_set_handle_t *ih;
bool need_gc = false;
if (handle != _inc_set_handle) {
return MBED_ERROR_INVALID_ARGUMENT;
}
if (!value_data && data_size) {
return MBED_ERROR_INVALID_ARGUMENT;
}
_inc_set_mutex.lock();
ih = reinterpret_cast<inc_set_handle_t *>(handle);
if (!ih->header.magic) {
ret = MBED_ERROR_INVALID_ARGUMENT;
goto end;
}
if (ih->offset_in_data + data_size > ih->header.data_size) {
ret = MBED_ERROR_INVALID_SIZE;
goto end;
}
// Update CRC with data chunk
ih->header.crc = calc_crc(ih->header.crc, data_size, value_data);
// Write the data chunk
ret = write_area(_active_area, ih->bd_curr_offset, data_size, value_data);
if (ret) {
need_gc = true;
goto end;
}
ih->bd_curr_offset += data_size;
ih->offset_in_data += data_size;
end:
if ((need_gc) && (ih->bd_base_offset != _master_record_offset)) {
garbage_collection();
}
_inc_set_mutex.unlock();
return ret;
}
int TDBStore::set_finalize(set_handle_t handle)
{
int os_ret, ret = MBED_SUCCESS;
inc_set_handle_t *ih;
ram_table_entry_t *ram_table = (ram_table_entry_t *) _ram_table;
ram_table_entry_t *entry;
bool need_gc = false;
uint32_t actual_data_size, hash, flags, next_offset;
if (handle != _inc_set_handle) {
return MBED_ERROR_INVALID_ARGUMENT;
}
ih = reinterpret_cast<inc_set_handle_t *>(handle);
if (!ih->header.magic) {
return MBED_ERROR_INVALID_ARGUMENT;
}
_inc_set_mutex.lock();
if (ih->offset_in_data != ih->header.data_size) {
ret = MBED_ERROR_INVALID_SIZE;
need_gc = true;
goto end;
}
// Write header
ret = write_area(_active_area, ih->bd_base_offset, sizeof(record_header_t), &ih->header);
if (ret) {
need_gc = true;
goto end;
}
// Need to flush buffered BD as our record is totally written now
os_ret = _buff_bd->sync();
if (os_ret) {
ret = MBED_ERROR_WRITE_FAILED;
need_gc = true;
goto end;
}
// In master record case we don't update RAM table
if (ih->bd_base_offset == _master_record_offset) {
goto end;
}
// Writes may fail without returning a failure (especially in flash components). Reread the record
// to ensure write success (this won't read the data anywhere - just use the CRC calculation).
ret = read_record(_active_area, ih->bd_base_offset, 0, 0, (uint32_t) -1,
actual_data_size, 0, false, false, false, false,
hash, flags, next_offset);
if (ret) {
need_gc = true;
goto end;
}
// Update RAM table
if (ih->header.flags & delete_flag) {
_num_keys--;
if (ih->ram_table_ind < _num_keys) {
memmove(&ram_table[ih->ram_table_ind], &ram_table[ih->ram_table_ind + 1],
sizeof(ram_table_entry_t) * (_num_keys - ih->ram_table_ind));
}
update_all_iterators(false, ih->ram_table_ind);
} else {
if (ih->new_key) {
if (ih->ram_table_ind < _num_keys) {
memmove(&ram_table[ih->ram_table_ind + 1], &ram_table[ih->ram_table_ind],
sizeof(ram_table_entry_t) * (_num_keys - ih->ram_table_ind));
}
_num_keys++;
update_all_iterators(true, ih->ram_table_ind);
}
entry = &ram_table[ih->ram_table_ind];
entry->hash = ih->hash;
entry->bd_offset = ih->bd_base_offset;
}
_free_space_offset = align_up(ih->bd_curr_offset, _prog_size);
// Safety check: If there seems to be valid keys on the free space
// we should erase one sector more, just to ensure that in case of power failure
// next init() would not extend the scan phase to that section as well.
os_ret = read_record(_active_area, _free_space_offset, 0, 0, 0, actual_data_size, 0,
false, false, false, false, hash, flags, next_offset);
if (os_ret == MBED_SUCCESS) {
check_erase_before_write(_active_area, _free_space_offset, sizeof(record_header_t));
}
end:
// mark handle as invalid by clearing magic field in header
ih->header.magic = 0;
_inc_set_mutex.unlock();
if (ih->bd_base_offset != _master_record_offset) {
if (need_gc) {
garbage_collection();
}
_mutex.unlock();
}
return ret;
}
int TDBStore::set(const char *key, const void *buffer, size_t size, uint32_t create_flags)
{
int ret;
set_handle_t handle;
// Don't wait till we get to set_add_data to catch this
if (!buffer && size) {
return MBED_ERROR_INVALID_ARGUMENT;
}
ret = set_start(&handle, key, size, create_flags);
if (ret) {
return ret;
}
ret = set_add_data(handle, buffer, size);
if (ret) {
return ret;
}
ret = set_finalize(handle);
return ret;
}
int TDBStore::remove(const char *key)
{
return set(key, 0, 0, delete_flag);
}
int TDBStore::get(const char *key, void *buffer, size_t buffer_size, size_t *actual_size, size_t offset)
{
int ret;
uint32_t actual_data_size;
uint32_t bd_offset, next_bd_offset;
uint32_t flags, hash, ram_table_ind;
if (!is_valid_key(key)) {
return MBED_ERROR_INVALID_ARGUMENT;
}
_mutex.lock();
ret = find_record(_active_area, key, bd_offset, ram_table_ind, hash);
if (ret != MBED_SUCCESS) {
goto end;
}
ret = read_record(_active_area, bd_offset, const_cast<char *>(key), buffer, buffer_size,
actual_data_size, offset, false, true, false, false, hash, flags, next_bd_offset);
if (actual_size) {
*actual_size = actual_data_size;
}
end:
_mutex.unlock();
return ret;
}
int TDBStore::get_info(const char *key, info_t *info)
{
int ret;
uint32_t bd_offset, next_bd_offset;
uint32_t flags, hash, ram_table_ind;
uint32_t actual_data_size;
if (!is_valid_key(key)) {
return MBED_ERROR_INVALID_ARGUMENT;
}
_mutex.lock();
ret = find_record(_active_area, key, bd_offset, ram_table_ind, hash);
if (ret) {
goto end;
}
// Give a large dummy buffer size in order to achieve actual data size
// (as copy_data flag is not set, data won't be copied anywhere)
ret = read_record(_active_area, bd_offset, const_cast<char *>(key), 0, (uint32_t) -1,
actual_data_size, 0, false, false, false, false, hash, flags,
next_bd_offset);
if (ret) {
goto end;
}
if (info) {
info->flags = flags;
info->size = actual_data_size;
}
end:
_mutex.unlock();
return ret;
}
int TDBStore::write_master_record(uint8_t area, uint16_t version, uint32_t &next_offset)
{
master_record_data_t master_rec;
master_rec.version = version;
master_rec.tdbstore_revision = tdbstore_revision;
master_rec.reserved = 0;
next_offset = _master_record_offset + _master_record_size;
return set(master_rec_key, &master_rec, sizeof(master_rec), 0);
}
int TDBStore::copy_record(uint8_t from_area, uint32_t from_offset, uint32_t to_offset,
uint32_t &to_next_offset)
{
int ret;
record_header_t header;
uint32_t total_size;
uint16_t chunk_size;
ret = read_area(from_area, from_offset, sizeof(header), &header);
if (ret) {
return ret;
}
total_size = align_up(sizeof(record_header_t), _prog_size) +
align_up(header.key_size + header.data_size, _prog_size);;
if (to_offset + total_size > _size) {
// We are trying to copy more that the are can hold
return MBED_ERROR_MEDIA_FULL;
}
ret = check_erase_before_write(1 - from_area, to_offset, total_size);
if (ret) {
return ret;
}
chunk_size = align_up(sizeof(record_header_t), _prog_size);
// The record header takes up whole program units
memset(_work_buf, 0, chunk_size);
memcpy(_work_buf, &header, sizeof(record_header_t));
ret = write_area(1 - from_area, to_offset, chunk_size, _work_buf);
if (ret) {
return ret;
}
from_offset += chunk_size;
to_offset += chunk_size;
total_size -= chunk_size;
while (total_size) {
chunk_size = std::min<size_t>(total_size, _work_buf_size);
ret = read_area(from_area, from_offset, chunk_size, _work_buf);
if (ret) {
return ret;
}
ret = write_area(1 - from_area, to_offset, chunk_size, _work_buf);
if (ret) {
return ret;
}
from_offset += chunk_size;
to_offset += chunk_size;
total_size -= chunk_size;
}
to_next_offset = align_up(to_offset, _prog_size);
return MBED_SUCCESS;
}
int TDBStore::garbage_collection()
{
ram_table_entry_t *ram_table = (ram_table_entry_t *) _ram_table;
uint32_t to_offset, to_next_offset;
int ret;
size_t ind;
// Reset the standby area
ret = reset_area(1 - _active_area);
if (ret) {
return ret;
}
to_offset = _master_record_offset + _master_record_size;
// Initialize in case table is empty
to_next_offset = to_offset;
// Go over ram table and copy all entries to opposite area
for (ind = 0; ind < _num_keys; ind++) {
uint32_t from_offset = ram_table[ind].bd_offset;
ret = copy_record(_active_area, from_offset, to_offset, to_next_offset);
if (ret) {
return ret;
}
// Update RAM table
ram_table[ind].bd_offset = to_offset;
to_offset = to_next_offset;
}
to_offset = to_next_offset;
_free_space_offset = to_next_offset;
// Now we can switch to the new active area
_active_area = 1 - _active_area;
// Now write master record, with version incremented by 1.
_active_area_version++;
ret = write_master_record(_active_area, _active_area_version, to_offset);
if (ret) {
return ret;
}
return MBED_SUCCESS;
}
int TDBStore::build_ram_table()
{
ram_table_entry_t *ram_table = (ram_table_entry_t *) _ram_table;
uint32_t offset, next_offset = 0, dummy;
int ret = MBED_SUCCESS;
uint32_t hash;
uint32_t flags;
uint32_t actual_data_size;
uint32_t ram_table_ind;
_num_keys = 0;
offset = _master_record_offset;
while (offset + sizeof(record_header_t) < _free_space_offset) {
ret = read_record(_active_area, offset, _key_buf, 0, 0, actual_data_size, 0,
true, false, false, true, hash, flags, next_offset);
if (ret) {
goto end;
}
ret = find_record(_active_area, _key_buf, dummy, ram_table_ind, hash);
if ((ret != MBED_SUCCESS) && (ret != MBED_ERROR_ITEM_NOT_FOUND)) {
goto end;
}
uint32_t save_offset = offset;
offset = next_offset;
if (ret == MBED_ERROR_ITEM_NOT_FOUND) {
// Key doesn't exist, need to add it to RAM table
ret = MBED_SUCCESS;
if (flags & delete_flag) {
continue;
}
if (_num_keys >= _max_keys) {
// In order to avoid numerous reallocations of ram table,
// Add a chunk of entries now
increment_max_keys(reinterpret_cast<void **>(&ram_table));
}
memmove(&ram_table[ram_table_ind + 1], &ram_table[ram_table_ind],
sizeof(ram_table_entry_t) * (_num_keys - ram_table_ind));
_num_keys++;
} else if (flags & delete_flag) {
_num_keys--;
memmove(&ram_table[ram_table_ind], &ram_table[ram_table_ind + 1],
sizeof(ram_table_entry_t) * (_num_keys - ram_table_ind));
continue;
}
// update record parameters
ram_table[ram_table_ind].hash = hash;
ram_table[ram_table_ind].bd_offset = save_offset;
}
end:
_free_space_offset = next_offset;
return ret;
}