@@ -29,10 +29,8 @@ static int zms_ate_valid_different_sector(struct zms_fs *fs, const struct zms_at
29
29
30
30
#ifdef CONFIG_ZMS_LOOKUP_CACHE
31
31
32
- static inline size_t zms_lookup_cache_pos (uint32_t id )
32
+ static inline size_t zms_lookup_cache_pos (zms_id_t id )
33
33
{
34
- uint32_t hash = id ;
35
-
36
34
#ifdef CONFIG_ZMS_LOOKUP_CACHE_FOR_SETTINGS
37
35
/*
38
36
* 1. Settings subsystem is storing the name ID and the linked list node ID
@@ -52,14 +50,27 @@ static inline size_t zms_lookup_cache_pos(uint32_t id)
52
50
uint32_t key_value_bit ;
53
51
uint32_t key_value_hash ;
54
52
uint32_t key_value_ll ;
53
+ uint32_t hash ;
55
54
56
55
key_value_bit = (id >> LOG2 (ZMS_DATA_ID_OFFSET )) & 1 ;
57
56
key_value_hash = (id & ZMS_HASH_MASK ) >> (CONFIG_SETTINGS_ZMS_MAX_COLLISIONS_BITS + 1 );
58
57
key_value_ll = id & BIT (0 );
59
58
60
59
hash = (key_value_hash << 2 ) | (key_value_bit << 1 ) | key_value_ll ;
60
+
61
+ #elif defined(CONFIG_ZMS_ID_64BIT )
62
+ /* 64-bit integer hash function found by https://github.com/skeeto/hash-prospector. */
63
+ uint64_t hash = id ;
64
+
65
+ hash ^= hash >> 32 ;
66
+ hash *= 0x42ab4abe4c475039ULL ;
67
+ hash ^= hash >> 31 ;
68
+ hash *= 0xfa90c4424c537791ULL ;
69
+ hash ^= hash >> 32 ;
61
70
#else
62
71
/* 32-bit integer hash function found by https://github.com/skeeto/hash-prospector. */
72
+ uint32_t hash = id ;
73
+
63
74
hash ^= hash >> 16 ;
64
75
hash *= 0x7feb352dU ;
65
76
hash ^= hash >> 15 ;
@@ -239,7 +250,7 @@ static int zms_flash_ate_wrt(struct zms_fs *fs, const struct zms_ate *entry)
239
250
goto end ;
240
251
}
241
252
#ifdef CONFIG_ZMS_LOOKUP_CACHE
242
- /* 0xFFFFFFFF is a special-purpose identifier. Exclude it from the cache */
253
+ /* ZMS_HEAD_ID is a special-purpose identifier. Exclude it from the cache */
243
254
if (entry -> id != ZMS_HEAD_ID ) {
244
255
fs -> lookup_cache [zms_lookup_cache_pos (entry -> id )] = fs -> ate_wra ;
245
256
}
@@ -487,7 +498,7 @@ static bool zms_close_ate_valid(struct zms_fs *fs, const struct zms_ate *entry)
487
498
/* zms_empty_ate_valid validates an sector empty ate.
488
499
* A valid sector empty ate should be:
489
500
* - a valid ate
490
- * - with len = 0xffff and id = 0xffffffff
501
+ * - with len = 0xffff and id = ZMS_HEAD_ID
491
502
* return true if valid, false otherwise
492
503
*/
493
504
static bool zms_empty_ate_valid (struct zms_fs * fs , const struct zms_ate * entry )
@@ -500,7 +511,7 @@ static bool zms_empty_ate_valid(struct zms_fs *fs, const struct zms_ate *entry)
500
511
* Valid gc_done_ate:
501
512
* - valid ate
502
513
* - len = 0
503
- * - id = 0xffffffff
514
+ * - id = ZMS_HEAD_ID
504
515
* return true if valid, false otherwise
505
516
*/
506
517
static bool zms_gc_done_ate_valid (struct zms_fs * fs , const struct zms_ate * entry )
@@ -509,6 +520,18 @@ static bool zms_gc_done_ate_valid(struct zms_fs *fs, const struct zms_ate *entry
509
520
(entry -> id == ZMS_HEAD_ID ));
510
521
}
511
522
523
+ /* zms_sector_closed checks whether the current sector is closed, which would imply
524
+ * that the empty ATE and close ATE are both valid and have matching cycle counters
525
+ *
526
+ * return true if closed, false otherwise
527
+ */
528
+ static bool zms_sector_closed (struct zms_fs * fs , struct zms_ate * empty_ate ,
529
+ struct zms_ate * close_ate )
530
+ {
531
+ return (zms_empty_ate_valid (fs , empty_ate ) && zms_close_ate_valid (fs , close_ate ) &&
532
+ (empty_ate -> cycle_cnt == close_ate -> cycle_cnt ));
533
+ }
534
+
512
535
/* Read empty and close ATE of the sector where belongs address "addr" and
513
536
* validates that the sector is closed.
514
537
* retval: 0 if sector is not close
@@ -526,8 +549,7 @@ static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct z
526
549
return rc ;
527
550
}
528
551
529
- if (zms_empty_ate_valid (fs , empty_ate ) && zms_close_ate_valid (fs , close_ate ) &&
530
- (empty_ate -> cycle_cnt == close_ate -> cycle_cnt )) {
552
+ if (zms_sector_closed (fs , empty_ate , close_ate )) {
531
553
/* Closed sector validated */
532
554
return 1 ;
533
555
}
@@ -536,7 +558,7 @@ static int zms_validate_closed_sector(struct zms_fs *fs, uint64_t addr, struct z
536
558
}
537
559
538
560
/* store an entry in flash */
539
- static int zms_flash_write_entry (struct zms_fs * fs , uint32_t id , const void * data , size_t len )
561
+ static int zms_flash_write_entry (struct zms_fs * fs , zms_id_t id , const void * data , size_t len )
540
562
{
541
563
int rc ;
542
564
struct zms_ate entry ;
@@ -549,13 +571,13 @@ static int zms_flash_write_entry(struct zms_fs *fs, uint32_t id, const void *dat
549
571
entry .cycle_cnt = fs -> sector_cycle ;
550
572
551
573
if (len > ZMS_DATA_IN_ATE_SIZE ) {
552
- /* only compute CRC if len is greater than 8 bytes */
553
- if ( IS_ENABLED ( CONFIG_ZMS_DATA_CRC )) {
554
- entry .data_crc = crc32_ieee (data , len );
555
- }
574
+ #ifdef CONFIG_ZMS_DATA_CRC
575
+ /* only compute CRC if data is to be stored outside of entry */
576
+ entry .data_crc = crc32_ieee (data , len );
577
+ #endif
556
578
entry .offset = (uint32_t )SECTOR_OFFSET (fs -> data_wra );
557
579
} else if ((len > 0 ) && (len <= ZMS_DATA_IN_ATE_SIZE )) {
558
- /* Copy data into entry for small data ( < 8B ) */
580
+ /* Copy data into entry for small data (at most ZMS_DATA_IN_ATE_SIZE bytes ) */
559
581
memcpy (& entry .data , data , len );
560
582
}
561
583
@@ -688,10 +710,12 @@ static int zms_sector_close(struct zms_fs *fs)
688
710
struct zms_ate close_ate ;
689
711
struct zms_ate garbage_ate ;
690
712
713
+ /* Initialize all members to 0xff */
714
+ memset (& close_ate , 0xff , sizeof (struct zms_ate ));
715
+
691
716
close_ate .id = ZMS_HEAD_ID ;
692
717
close_ate .len = 0U ;
693
718
close_ate .offset = (uint32_t )SECTOR_OFFSET (fs -> ate_wra + fs -> ate_size );
694
- close_ate .metadata = 0xffffffff ;
695
719
close_ate .cycle_cnt = fs -> sector_cycle ;
696
720
697
721
/* When we close the sector, we must write all non used ATE with
@@ -740,11 +764,13 @@ static int zms_add_gc_done_ate(struct zms_fs *fs)
740
764
{
741
765
struct zms_ate gc_done_ate ;
742
766
767
+ /* Initialize all members to 0xff */
768
+ memset (& gc_done_ate , 0xff , sizeof (struct zms_ate ));
769
+
743
770
LOG_DBG ("Adding gc done ate at %llx" , fs -> ate_wra );
744
771
gc_done_ate .id = ZMS_HEAD_ID ;
745
772
gc_done_ate .len = 0U ;
746
773
gc_done_ate .offset = (uint32_t )SECTOR_OFFSET (fs -> data_wra );
747
- gc_done_ate .metadata = 0xffffffff ;
748
774
gc_done_ate .cycle_cnt = fs -> sector_cycle ;
749
775
750
776
zms_ate_crc8_update (& gc_done_ate );
@@ -793,14 +819,17 @@ static int zms_add_empty_ate(struct zms_fs *fs, uint64_t addr)
793
819
int rc = 0 ;
794
820
uint64_t previous_ate_wra ;
795
821
822
+ /* Initialize all members to 0 */
823
+ memset (& empty_ate , 0 , sizeof (struct zms_ate ));
824
+
796
825
addr &= ADDR_SECT_MASK ;
797
826
798
827
LOG_DBG ("Adding empty ate at %llx" , (uint64_t )(addr + fs -> sector_size - fs -> ate_size ));
799
828
empty_ate .id = ZMS_HEAD_ID ;
800
829
empty_ate .len = 0xffff ;
801
- empty_ate .offset = 0U ;
802
- empty_ate . metadata =
803
- FIELD_PREP (ZMS_MAGIC_NUMBER_MASK , ZMS_MAGIC_NUMBER ) | ZMS_DEFAULT_VERSION ;
830
+ empty_ate .metadata = FIELD_PREP ( ZMS_VERSION_MASK , ZMS_DEFAULT_VERSION ) |
831
+ FIELD_PREP ( ZMS_MAGIC_NUMBER_MASK , ZMS_MAGIC_NUMBER ) |
832
+ FIELD_PREP (ZMS_ATE_FORMAT_MASK , ZMS_DEFAULT_ATE_FORMAT ) ;
804
833
805
834
rc = zms_get_sector_cycle (fs , addr , & cycle_cnt );
806
835
if (rc == - ENOENT ) {
@@ -893,7 +922,7 @@ static int zms_get_sector_header(struct zms_fs *fs, uint64_t addr, struct zms_at
893
922
* @retval 1 valid ATE with same ID found
894
923
* @retval < 0 An error happened
895
924
*/
896
- static int zms_find_ate_with_id (struct zms_fs * fs , uint32_t id , uint64_t start_addr ,
925
+ static int zms_find_ate_with_id (struct zms_fs * fs , zms_id_t id , uint64_t start_addr ,
897
926
uint64_t end_addr , struct zms_ate * ate , uint64_t * ate_addr )
898
927
{
899
928
int rc ;
@@ -1044,10 +1073,10 @@ static int zms_gc(struct zms_fs *fs)
1044
1073
*/
1045
1074
if (wlk_prev_addr == gc_prev_addr ) {
1046
1075
/* copy needed */
1047
- LOG_DBG ("Moving %d , len %d" , gc_ate .id , gc_ate .len );
1076
+ LOG_DBG ("Moving %lld , len %d" , ( long long ) gc_ate .id , gc_ate .len );
1048
1077
1049
1078
if (gc_ate .len > ZMS_DATA_IN_ATE_SIZE ) {
1050
- /* Copy Data only when len > 8
1079
+ /* Copy Data only when len > ZMS_DATA_IN_ATE_SIZE
1051
1080
* Otherwise, Data is already inside ATE
1052
1081
*/
1053
1082
data_addr = (gc_prev_addr & ADDR_SECT_MASK );
@@ -1151,16 +1180,28 @@ static int zms_init(struct zms_fs *fs)
1151
1180
for (i = 0 ; i < fs -> sector_count ; i ++ ) {
1152
1181
addr = zms_close_ate_addr (fs , ((uint64_t )i << ADDR_SECT_SHIFT ));
1153
1182
1154
- /* verify if the sector is closed */
1155
- sec_closed = zms_validate_closed_sector (fs , addr , & empty_ate , & close_ate );
1156
- if (sec_closed < 0 ) {
1157
- rc = sec_closed ;
1183
+ /* read the header ATEs */
1184
+ rc = zms_get_sector_header (fs , addr , & empty_ate , & close_ate );
1185
+ if (rc ) {
1158
1186
goto end ;
1159
1187
}
1160
1188
/* update cycle count */
1161
1189
fs -> sector_cycle = empty_ate .cycle_cnt ;
1162
1190
1163
- if (sec_closed == 1 ) {
1191
+ /* Check the ATE format indicator so that we know how to validate ATEs.
1192
+ * The metadata field has the same offset and size in all ATE formats
1193
+ * (the same is guaranteed for crc8 and cycle_cnt).
1194
+ * Currently, ZMS can only recognize one of its supported ATE formats
1195
+ * (the one chosen at build time), so their indicators are defined for
1196
+ * the possibility of a future extension.
1197
+ * If this indicator is unknown, then consider the header ATEs invalid,
1198
+ * because we might not be dealing with ZMS contents at all.
1199
+ */
1200
+ if (ZMS_GET_ATE_FORMAT (empty_ate .metadata ) != ZMS_DEFAULT_ATE_FORMAT ) {
1201
+ continue ;
1202
+ }
1203
+
1204
+ if (zms_sector_closed (fs , & empty_ate , & close_ate )) {
1164
1205
/* closed sector */
1165
1206
closed_sectors ++ ;
1166
1207
/* Let's verify that this is a ZMS storage system */
@@ -1218,7 +1259,8 @@ static int zms_init(struct zms_fs *fs)
1218
1259
goto end ;
1219
1260
}
1220
1261
1221
- if (zms_empty_ate_valid (fs , & empty_ate )) {
1262
+ if ((ZMS_GET_ATE_FORMAT (empty_ate .metadata ) == ZMS_DEFAULT_ATE_FORMAT ) &&
1263
+ zms_empty_ate_valid (fs , & empty_ate )) {
1222
1264
/* Empty ATE is valid, let's verify that this is a ZMS storage system */
1223
1265
if (ZMS_GET_MAGIC_NUMBER (empty_ate .metadata ) == ZMS_MAGIC_NUMBER ) {
1224
1266
zms_magic_exist = true;
@@ -1458,7 +1500,7 @@ int zms_mount(struct zms_fs *fs)
1458
1500
return 0 ;
1459
1501
}
1460
1502
1461
- ssize_t zms_write (struct zms_fs * fs , uint32_t id , const void * data , size_t len )
1503
+ ssize_t zms_write (struct zms_fs * fs , zms_id_t id , const void * data , size_t len )
1462
1504
{
1463
1505
int rc ;
1464
1506
size_t data_size ;
@@ -1598,12 +1640,12 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len)
1598
1640
return rc ;
1599
1641
}
1600
1642
1601
- int zms_delete (struct zms_fs * fs , uint32_t id )
1643
+ int zms_delete (struct zms_fs * fs , zms_id_t id )
1602
1644
{
1603
1645
return zms_write (fs , id , NULL , 0 );
1604
1646
}
1605
1647
1606
- ssize_t zms_read_hist (struct zms_fs * fs , uint32_t id , void * data , size_t len , uint32_t cnt )
1648
+ ssize_t zms_read_hist (struct zms_fs * fs , zms_id_t id , void * data , size_t len , uint32_t cnt )
1607
1649
{
1608
1650
int rc ;
1609
1651
int prev_found = 0 ;
@@ -1700,7 +1742,7 @@ ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, ui
1700
1742
return rc ;
1701
1743
}
1702
1744
1703
- ssize_t zms_read (struct zms_fs * fs , uint32_t id , void * data , size_t len )
1745
+ ssize_t zms_read (struct zms_fs * fs , zms_id_t id , void * data , size_t len )
1704
1746
{
1705
1747
int rc ;
1706
1748
@@ -1713,7 +1755,7 @@ ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len)
1713
1755
return MIN (rc , len );
1714
1756
}
1715
1757
1716
- ssize_t zms_get_data_length (struct zms_fs * fs , uint32_t id )
1758
+ ssize_t zms_get_data_length (struct zms_fs * fs , zms_id_t id )
1717
1759
{
1718
1760
int rc ;
1719
1761
0 commit comments