@@ -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 );
@@ -1156,16 +1185,28 @@ static int zms_init(struct zms_fs *fs)
1156
1185
for (i = 0 ; i < fs -> sector_count ; i ++ ) {
1157
1186
addr = zms_close_ate_addr (fs , ((uint64_t )i << ADDR_SECT_SHIFT ));
1158
1187
1159
- /* verify if the sector is closed */
1160
- sec_closed = zms_validate_closed_sector (fs , addr , & empty_ate , & close_ate );
1161
- if (sec_closed < 0 ) {
1162
- rc = sec_closed ;
1188
+ /* read the header ATEs */
1189
+ rc = zms_get_sector_header (fs , addr , & empty_ate , & close_ate );
1190
+ if (rc ) {
1163
1191
goto end ;
1164
1192
}
1165
1193
/* update cycle count */
1166
1194
fs -> sector_cycle = empty_ate .cycle_cnt ;
1167
1195
1168
- if (sec_closed == 1 ) {
1196
+ /* Check the ATE format indicator so that we know how to validate ATEs.
1197
+ * The metadata field has the same offset and size in all ATE formats
1198
+ * (the same is guaranteed for crc8 and cycle_cnt).
1199
+ * Currently, ZMS can only recognize one of its supported ATE formats
1200
+ * (the one chosen at build time), so their indicators are defined for
1201
+ * the possibility of a future extension.
1202
+ * If this indicator is unknown, then consider the header ATEs invalid,
1203
+ * because we might not be dealing with ZMS contents at all.
1204
+ */
1205
+ if (ZMS_GET_ATE_FORMAT (empty_ate .metadata ) != ZMS_DEFAULT_ATE_FORMAT ) {
1206
+ continue ;
1207
+ }
1208
+
1209
+ if (zms_sector_closed (fs , & empty_ate , & close_ate )) {
1169
1210
/* closed sector */
1170
1211
closed_sectors ++ ;
1171
1212
/* Let's verify that this is a ZMS storage system */
@@ -1223,7 +1264,8 @@ static int zms_init(struct zms_fs *fs)
1223
1264
goto end ;
1224
1265
}
1225
1266
1226
- if (zms_empty_ate_valid (fs , & empty_ate )) {
1267
+ if ((ZMS_GET_ATE_FORMAT (empty_ate .metadata ) == ZMS_DEFAULT_ATE_FORMAT ) &&
1268
+ zms_empty_ate_valid (fs , & empty_ate )) {
1227
1269
/* Empty ATE is valid, let's verify that this is a ZMS storage system */
1228
1270
if (ZMS_GET_MAGIC_NUMBER (empty_ate .metadata ) == ZMS_MAGIC_NUMBER ) {
1229
1271
zms_magic_exist = true;
@@ -1468,7 +1510,7 @@ int zms_mount(struct zms_fs *fs)
1468
1510
return 0 ;
1469
1511
}
1470
1512
1471
- ssize_t zms_write (struct zms_fs * fs , uint32_t id , const void * data , size_t len )
1513
+ ssize_t zms_write (struct zms_fs * fs , zms_id_t id , const void * data , size_t len )
1472
1514
{
1473
1515
int rc ;
1474
1516
size_t data_size ;
@@ -1620,12 +1662,12 @@ ssize_t zms_write(struct zms_fs *fs, uint32_t id, const void *data, size_t len)
1620
1662
return rc ;
1621
1663
}
1622
1664
1623
- int zms_delete (struct zms_fs * fs , uint32_t id )
1665
+ int zms_delete (struct zms_fs * fs , zms_id_t id )
1624
1666
{
1625
1667
return zms_write (fs , id , NULL , 0 );
1626
1668
}
1627
1669
1628
- ssize_t zms_read_hist (struct zms_fs * fs , uint32_t id , void * data , size_t len , uint32_t cnt )
1670
+ ssize_t zms_read_hist (struct zms_fs * fs , zms_id_t id , void * data , size_t len , uint32_t cnt )
1629
1671
{
1630
1672
int rc ;
1631
1673
int prev_found = 0 ;
@@ -1726,7 +1768,7 @@ ssize_t zms_read_hist(struct zms_fs *fs, uint32_t id, void *data, size_t len, ui
1726
1768
return rc ;
1727
1769
}
1728
1770
1729
- ssize_t zms_read (struct zms_fs * fs , uint32_t id , void * data , size_t len )
1771
+ ssize_t zms_read (struct zms_fs * fs , zms_id_t id , void * data , size_t len )
1730
1772
{
1731
1773
int rc ;
1732
1774
@@ -1739,7 +1781,7 @@ ssize_t zms_read(struct zms_fs *fs, uint32_t id, void *data, size_t len)
1739
1781
return MIN (rc , len );
1740
1782
}
1741
1783
1742
- ssize_t zms_get_data_length (struct zms_fs * fs , uint32_t id )
1784
+ ssize_t zms_get_data_length (struct zms_fs * fs , zms_id_t id )
1743
1785
{
1744
1786
int rc ;
1745
1787
0 commit comments