1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #define PW_LOG_MODULE_NAME "KVS"
16 #define PW_LOG_LEVEL PW_KVS_LOG_LEVEL
17
18 #include "pw_kvs/key_value_store.h"
19
20 #include <algorithm>
21 #include <cinttypes>
22 #include <cstring>
23 #include <type_traits>
24
25 #include "pw_assert/check.h"
26 #include "pw_kvs_private/config.h"
27 #include "pw_log/log.h"
28 #include "pw_status/try.h"
29
30 namespace pw::kvs {
31 namespace {
32
33 using std::byte;
34
InvalidKey(Key key)35 constexpr bool InvalidKey(Key key) {
36 return key.empty() || (key.size() > internal::Entry::kMaxKeyLength);
37 }
38
39 } // namespace
40
KeyValueStore(FlashPartition * partition,span<const EntryFormat> formats,const Options & options,size_t redundancy,Vector<SectorDescriptor> & sector_descriptor_list,const SectorDescriptor ** temp_sectors_to_skip,Vector<KeyDescriptor> & key_descriptor_list,Address * addresses)41 KeyValueStore::KeyValueStore(FlashPartition* partition,
42 span<const EntryFormat> formats,
43 const Options& options,
44 size_t redundancy,
45 Vector<SectorDescriptor>& sector_descriptor_list,
46 const SectorDescriptor** temp_sectors_to_skip,
47 Vector<KeyDescriptor>& key_descriptor_list,
48 Address* addresses)
49 : partition_(*partition),
50 formats_(formats),
51 sectors_(sector_descriptor_list, *partition, temp_sectors_to_skip),
52 entry_cache_(key_descriptor_list, addresses, redundancy),
53 options_(options),
54 initialized_(InitializationState::kNotInitialized),
55 error_detected_(false),
56 internal_stats_({}),
57 last_transaction_id_(0) {}
58
Init()59 Status KeyValueStore::Init() {
60 initialized_ = InitializationState::kNotInitialized;
61 error_detected_ = false;
62 last_transaction_id_ = 0;
63
64 PW_LOG_INFO("Initializing key value store");
65 if (partition_.sector_count() > sectors_.max_size()) {
66 PW_LOG_ERROR(
67 "KVS init failed: kMaxUsableSectors (=%u) must be at least as "
68 "large as the number of sectors in the flash partition (=%u)",
69 unsigned(sectors_.max_size()),
70 unsigned(partition_.sector_count()));
71 return Status::FailedPrecondition();
72 }
73
74 if (partition_.sector_count() < 2) {
75 PW_LOG_ERROR(
76 "KVS init failed: FlashParition sector count (=%u) must be at 2. KVS "
77 "requires at least 1 working sector + 1 free/reserved sector",
78 unsigned(partition_.sector_count()));
79 return Status::FailedPrecondition();
80 }
81
82 const size_t sector_size_bytes = partition_.sector_size_bytes();
83
84 // TODO(davidrogers): investigate doing this as a static assert/compile-time
85 // check.
86 if (sector_size_bytes > SectorDescriptor::max_sector_size()) {
87 PW_LOG_ERROR(
88 "KVS init failed: sector_size_bytes (=%u) is greater than maximum "
89 "allowed sector size (=%u)",
90 unsigned(sector_size_bytes),
91 unsigned(SectorDescriptor::max_sector_size()));
92 return Status::FailedPrecondition();
93 }
94
95 Status metadata_result = InitializeMetadata();
96
97 if (!error_detected_) {
98 initialized_ = InitializationState::kReady;
99 } else {
100 initialized_ = InitializationState::kNeedsMaintenance;
101
102 if (options_.recovery != ErrorRecovery::kManual) {
103 size_t pre_fix_redundancy_errors =
104 internal_stats_.missing_redundant_entries_recovered;
105 Status recovery_status = FixErrors();
106
107 if (recovery_status.ok()) {
108 if (metadata_result.IsOutOfRange()) {
109 internal_stats_.missing_redundant_entries_recovered =
110 pre_fix_redundancy_errors;
111 PW_LOG_INFO("KVS init: Redundancy level successfully updated");
112 } else {
113 PW_LOG_WARN("KVS init: Corruption detected and fully repaired");
114 }
115 initialized_ = InitializationState::kReady;
116 } else if (recovery_status.IsResourceExhausted()) {
117 PW_LOG_WARN("KVS init: Unable to maintain required free sector");
118 } else {
119 PW_LOG_WARN("KVS init: Corruption detected and unable repair");
120 }
121 } else {
122 PW_LOG_WARN(
123 "KVS init: Corruption detected, no repair attempted due to options");
124 }
125 }
126
127 PW_LOG_INFO(
128 "KeyValueStore init complete: active keys %u, deleted keys %u, sectors "
129 "%u, logical sector size %u bytes",
130 unsigned(size()),
131 unsigned(entry_cache_.total_entries() - size()),
132 unsigned(sectors_.size()),
133 unsigned(partition_.sector_size_bytes()));
134
135 // Report any corruption was not repaired.
136 if (error_detected_) {
137 PW_LOG_WARN(
138 "KVS init: Corruption found but not repaired, KVS unavailable until "
139 "successful maintenance.");
140 return Status::DataLoss();
141 }
142
143 return OkStatus();
144 }
145
InitializeMetadata()146 Status KeyValueStore::InitializeMetadata() {
147 const size_t sector_size_bytes = partition_.sector_size_bytes();
148
149 sectors_.Reset();
150 entry_cache_.Reset();
151
152 PW_LOG_DEBUG("First pass: Read all entries from all sectors");
153 Address sector_address = 0;
154
155 size_t total_corrupt_bytes = 0;
156 size_t corrupt_entries = 0;
157 bool empty_sector_found = false;
158 size_t entry_copies_missing = 0;
159
160 for (SectorDescriptor& sector : sectors_) {
161 Address entry_address = sector_address;
162
163 size_t sector_corrupt_bytes = 0;
164
165 for (int num_entries_in_sector = 0; true; num_entries_in_sector++) {
166 PW_LOG_DEBUG("Load entry: sector=%u, entry#=%d, address=%u",
167 unsigned(sector_address),
168 num_entries_in_sector,
169 unsigned(entry_address));
170
171 if (!sectors_.AddressInSector(sector, entry_address)) {
172 PW_LOG_DEBUG("Fell off end of sector; moving to the next sector");
173 break;
174 }
175
176 Address next_entry_address;
177 Status status = LoadEntry(entry_address, &next_entry_address);
178 if (status.IsNotFound()) {
179 PW_LOG_DEBUG(
180 "Hit un-written data in sector; moving to the next sector");
181 break;
182 } else if (!status.ok()) {
183 // The entry could not be read, indicating likely data corruption within
184 // the sector. Try to scan the remainder of the sector for other
185 // entries.
186
187 error_detected_ = true;
188 corrupt_entries++;
189
190 status = ScanForEntry(sector,
191 entry_address + Entry::kMinAlignmentBytes,
192 &next_entry_address);
193 if (!status.ok()) {
194 // No further entries in this sector. Mark the remaining bytes in the
195 // sector as corrupt (since we can't reliably know the size of the
196 // corrupt entry).
197 sector_corrupt_bytes +=
198 sector_size_bytes - (entry_address - sector_address);
199 break;
200 }
201
202 sector_corrupt_bytes += next_entry_address - entry_address;
203 }
204
205 // Entry loaded successfully; so get ready to load the next one.
206 entry_address = next_entry_address;
207
208 // Update of the number of writable bytes in this sector.
209 sector.set_writable_bytes(sector_size_bytes -
210 (entry_address - sector_address));
211 }
212
213 if (sector_corrupt_bytes > 0) {
214 // If the sector contains corrupt data, prevent any further entries from
215 // being written to it by indicating that it has no space. This should
216 // also make it a decent GC candidate. Valid keys in the sector are still
217 // readable as normal.
218 sector.mark_corrupt();
219 error_detected_ = true;
220
221 PW_LOG_WARN("Sector %u contains %uB of corrupt data",
222 sectors_.Index(sector),
223 unsigned(sector_corrupt_bytes));
224 }
225
226 if (sector.Empty(sector_size_bytes)) {
227 empty_sector_found = true;
228 }
229 sector_address += sector_size_bytes;
230 total_corrupt_bytes += sector_corrupt_bytes;
231 }
232
233 PW_LOG_DEBUG("Second pass: Count valid bytes in each sector");
234 Address newest_key = 0;
235
236 // For every valid entry, for each address, count the valid bytes in that
237 // sector. If the address fails to read, remove the address and mark the
238 // sector as corrupt. Track which entry has the newest transaction ID for
239 // initializing last_new_sector_.
240 for (EntryMetadata& metadata : entry_cache_) {
241 if (metadata.addresses().size() < redundancy()) {
242 PW_LOG_DEBUG("Key 0x%08x missing copies, has %u, needs %u",
243 unsigned(metadata.hash()),
244 unsigned(metadata.addresses().size()),
245 unsigned(redundancy()));
246 entry_copies_missing++;
247 }
248 size_t index = 0;
249 while (index < metadata.addresses().size()) {
250 Address address = metadata.addresses()[index];
251 Entry entry;
252
253 Status read_result = Entry::Read(partition_, address, formats_, &entry);
254
255 SectorDescriptor& sector = sectors_.FromAddress(address);
256
257 if (read_result.ok()) {
258 sector.AddValidBytes(entry.size());
259 index++;
260 } else {
261 corrupt_entries++;
262 total_corrupt_bytes += sector.writable_bytes();
263 error_detected_ = true;
264 sector.mark_corrupt();
265
266 // Remove the bad address and stay at this index. The removal
267 // replaces out the removed address with the back address so
268 // this index needs to be rechecked with the new address.
269 metadata.RemoveAddress(address);
270 }
271 }
272
273 if (metadata.IsNewerThan(last_transaction_id_)) {
274 last_transaction_id_ = metadata.transaction_id();
275 newest_key = metadata.addresses().back();
276 }
277 }
278
279 sectors_.set_last_new_sector(newest_key);
280
281 if (!empty_sector_found) {
282 PW_LOG_DEBUG("No empty sector found");
283 error_detected_ = true;
284 }
285
286 if (entry_copies_missing > 0) {
287 bool other_errors = error_detected_;
288 error_detected_ = true;
289
290 if (!other_errors && entry_copies_missing == entry_cache_.total_entries()) {
291 PW_LOG_INFO(
292 "KVS configuration changed to redundancy of %u total copies per key",
293 unsigned(redundancy()));
294 return Status::OutOfRange();
295 }
296 }
297
298 if (error_detected_) {
299 PW_LOG_WARN(
300 "Corruption detected. Found %u corrupt bytes, %u corrupt entries, "
301 "and %u keys missing redundant copies.",
302 unsigned(total_corrupt_bytes),
303 unsigned(corrupt_entries),
304 unsigned(entry_copies_missing));
305 return Status::FailedPrecondition();
306 }
307 return OkStatus();
308 }
309
GetStorageStats() const310 KeyValueStore::StorageStats KeyValueStore::GetStorageStats() const {
311 StorageStats stats{};
312 const size_t sector_size = partition_.sector_size_bytes();
313 bool found_empty_sector = false;
314 stats.sector_erase_count = internal_stats_.sector_erase_count;
315 stats.corrupt_sectors_recovered = internal_stats_.corrupt_sectors_recovered;
316 stats.missing_redundant_entries_recovered =
317 internal_stats_.missing_redundant_entries_recovered;
318
319 for (const SectorDescriptor& sector : sectors_) {
320 stats.in_use_bytes += sector.valid_bytes();
321 stats.reclaimable_bytes += sector.RecoverableBytes(sector_size);
322
323 if (!found_empty_sector && sector.Empty(sector_size)) {
324 // The KVS tries to always keep an empty sector for GC, so don't count
325 // the first empty sector seen as writable space. However, a free sector
326 // cannot always be assumed to exist; if a GC operation fails, all sectors
327 // may be partially written, in which case the space reported might be
328 // inaccurate.
329 found_empty_sector = true;
330 continue;
331 }
332
333 stats.writable_bytes += sector.writable_bytes();
334 }
335
336 return stats;
337 }
338
339 // Check KVS for any error conditions. Primarily intended for test and
340 // internal use.
CheckForErrors()341 bool KeyValueStore::CheckForErrors() {
342 // Check for corrupted sectors
343 for (SectorDescriptor& sector : sectors_) {
344 if (sector.corrupt()) {
345 error_detected_ = true;
346 return error_detected();
347 }
348 }
349
350 // Check for missing redundancy.
351 if (redundancy() > 1) {
352 for (const EntryMetadata& metadata : entry_cache_) {
353 if (metadata.addresses().size() < redundancy()) {
354 error_detected_ = true;
355 return error_detected();
356 }
357 }
358 }
359
360 return error_detected();
361 }
362
LoadEntry(Address entry_address,Address * next_entry_address)363 Status KeyValueStore::LoadEntry(Address entry_address,
364 Address* next_entry_address) {
365 Entry entry;
366 PW_TRY(Entry::Read(partition_, entry_address, formats_, &entry));
367
368 // Read the key from flash & validate the entry (which reads the value).
369 Entry::KeyBuffer key_buffer;
370 PW_TRY_ASSIGN(size_t key_length, entry.ReadKey(key_buffer));
371 const Key key(key_buffer.data(), key_length);
372
373 PW_TRY(entry.VerifyChecksumInFlash());
374
375 // A valid entry was found, so update the next entry address before doing any
376 // of the checks that happen in AddNewOrUpdateExisting.
377 *next_entry_address = entry.next_address();
378 return entry_cache_.AddNewOrUpdateExisting(
379 entry.descriptor(key), entry.address(), partition_.sector_size_bytes());
380 }
381
382 // Scans flash memory within a sector to find a KVS entry magic.
ScanForEntry(const SectorDescriptor & sector,Address start_address,Address * next_entry_address)383 Status KeyValueStore::ScanForEntry(const SectorDescriptor& sector,
384 Address start_address,
385 Address* next_entry_address) {
386 PW_LOG_DEBUG("Scanning sector %u for entries starting from address %u",
387 sectors_.Index(sector),
388 unsigned(start_address));
389
390 // Entries must start at addresses which are aligned on a multiple of
391 // Entry::kMinAlignmentBytes. However, that multiple can vary between entries.
392 // When scanning, we don't have an entry to tell us what the current alignment
393 // is, so the minimum alignment is used to be exhaustive.
394 for (Address address = AlignUp(start_address, Entry::kMinAlignmentBytes);
395 sectors_.AddressInSector(sector, address);
396 address += Entry::kMinAlignmentBytes) {
397 uint32_t magic;
398 StatusWithSize read_result =
399 partition_.Read(address, as_writable_bytes(span(&magic, 1)));
400 if (!read_result.ok()) {
401 continue;
402 }
403 if (formats_.KnownMagic(magic)) {
404 PW_LOG_DEBUG("Found entry magic at address %u", unsigned(address));
405 *next_entry_address = address;
406 return OkStatus();
407 }
408 }
409
410 return Status::NotFound();
411 }
412
413 #if PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE
414
RemoveDeletedKeyEntries()415 Status KeyValueStore::RemoveDeletedKeyEntries() {
416 for (internal::EntryCache::iterator it = entry_cache_.begin();
417 it != entry_cache_.end();
418 ++it) {
419 EntryMetadata& entry_metadata = *it;
420
421 // The iterator we are given back from RemoveEntry could also be deleted,
422 // so loop until we find one that isn't deleted.
423 while (entry_metadata.state() == EntryState::kDeleted) {
424 // Read the original entry to get the size for sector accounting purposes.
425 Entry entry;
426 PW_TRY(ReadEntry(entry_metadata, entry));
427
428 for (Address address : entry_metadata.addresses()) {
429 sectors_.FromAddress(address).RemoveValidBytes(entry.size());
430 }
431
432 it = entry_cache_.RemoveEntry(it);
433
434 if (it == entry_cache_.end()) {
435 return OkStatus(); // new iterator is the end, bail
436 }
437
438 entry_metadata = *it; // set entry_metadata to check for deletion again
439 }
440 }
441
442 return OkStatus();
443 }
444
445 #endif // PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE
446
Get(Key key,span<byte> value_buffer,size_t offset_bytes) const447 StatusWithSize KeyValueStore::Get(Key key,
448 span<byte> value_buffer,
449 size_t offset_bytes) const {
450 PW_TRY_WITH_SIZE(CheckReadOperation(key));
451
452 EntryMetadata metadata;
453 PW_TRY_WITH_SIZE(FindExisting(key, &metadata));
454
455 return Get(key, metadata, value_buffer, offset_bytes);
456 }
457
PutBytes(Key key,span<const byte> value)458 Status KeyValueStore::PutBytes(Key key, span<const byte> value) {
459 PW_TRY(CheckWriteOperation(key));
460 PW_LOG_DEBUG("Writing key/value; key length=%u, value length=%u",
461 unsigned(key.size()),
462 unsigned(value.size()));
463
464 if (Entry::size(partition_, key, value) > partition_.sector_size_bytes()) {
465 PW_LOG_DEBUG("%u B value with %u B key cannot fit in one sector",
466 unsigned(value.size()),
467 unsigned(key.size()));
468 return Status::InvalidArgument();
469 }
470
471 EntryMetadata metadata;
472 Status status = FindEntry(key, &metadata);
473
474 if (status.ok()) {
475 // TODO(davidrogers): figure out logging how to support multiple addresses.
476 PW_LOG_DEBUG("Overwriting entry for key 0x%08x in %u sectors including %u",
477 unsigned(metadata.hash()),
478 unsigned(metadata.addresses().size()),
479 sectors_.Index(metadata.first_address()));
480 return WriteEntryForExistingKey(metadata, EntryState::kValid, key, value);
481 }
482
483 if (status.IsNotFound()) {
484 return WriteEntryForNewKey(key, value);
485 }
486
487 return status;
488 }
489
Delete(Key key)490 Status KeyValueStore::Delete(Key key) {
491 PW_TRY(CheckWriteOperation(key));
492
493 EntryMetadata metadata;
494 PW_TRY(FindExisting(key, &metadata));
495
496 // TODO(davidrogers): figure out logging how to support multiple addresses.
497 PW_LOG_DEBUG("Writing tombstone for key 0x%08x in %u sectors including %u",
498 unsigned(metadata.hash()),
499 unsigned(metadata.addresses().size()),
500 sectors_.Index(metadata.first_address()));
501 return WriteEntryForExistingKey(metadata, EntryState::kDeleted, key, {});
502 }
503
ReadKey()504 void KeyValueStore::Item::ReadKey() {
505 key_buffer_.fill('\0');
506
507 Entry entry;
508 if (kvs_.ReadEntry(*iterator_, entry).ok()) {
509 entry.ReadKey(key_buffer_)
510 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
511 }
512 }
513
operator ++()514 KeyValueStore::iterator& KeyValueStore::iterator::operator++() {
515 // Skip to the next entry that is valid (not deleted).
516 while (++item_.iterator_ != item_.kvs_.entry_cache_.end() &&
517 item_.iterator_->state() != EntryState::kValid) {
518 }
519 return *this;
520 }
521
begin() const522 KeyValueStore::iterator KeyValueStore::begin() const {
523 internal::EntryCache::const_iterator cache_iterator = entry_cache_.begin();
524 // Skip over any deleted entries at the start of the descriptor list.
525 while (cache_iterator != entry_cache_.end() &&
526 cache_iterator->state() != EntryState::kValid) {
527 ++cache_iterator;
528 }
529 return iterator(*this, cache_iterator);
530 }
531
ValueSize(Key key) const532 StatusWithSize KeyValueStore::ValueSize(Key key) const {
533 PW_TRY_WITH_SIZE(CheckReadOperation(key));
534
535 EntryMetadata metadata;
536 PW_TRY_WITH_SIZE(FindExisting(key, &metadata));
537
538 return ValueSize(metadata);
539 }
540
ReadEntry(const EntryMetadata & metadata,Entry & entry) const541 Status KeyValueStore::ReadEntry(const EntryMetadata& metadata,
542 Entry& entry) const {
543 // Try to read an entry
544 Status read_result = Status::DataLoss();
545 for (Address address : metadata.addresses()) {
546 read_result = Entry::Read(partition_, address, formats_, &entry);
547 if (read_result.ok()) {
548 return read_result;
549 }
550
551 // Found a bad address. Set the sector as corrupt.
552 error_detected_ = true;
553 sectors_.FromAddress(address).mark_corrupt();
554 }
555
556 PW_LOG_ERROR("No valid entries for key. Data has been lost!");
557 return read_result;
558 }
559
FindEntry(Key key,EntryMetadata * metadata_out) const560 Status KeyValueStore::FindEntry(Key key, EntryMetadata* metadata_out) const {
561 StatusWithSize find_result =
562 entry_cache_.Find(partition_, sectors_, formats_, key, metadata_out);
563
564 if (find_result.size() > 0u) {
565 error_detected_ = true;
566 }
567 return find_result.status();
568 }
569
FindExisting(Key key,EntryMetadata * metadata_out) const570 Status KeyValueStore::FindExisting(Key key, EntryMetadata* metadata_out) const {
571 Status status = FindEntry(key, metadata_out);
572
573 // If the key's hash collides with an existing key or if the key is deleted,
574 // treat it as if it is not in the KVS.
575 if (status.IsAlreadyExists() ||
576 (status.ok() && metadata_out->state() == EntryState::kDeleted)) {
577 return Status::NotFound();
578 }
579 return status;
580 }
581
Get(Key key,const EntryMetadata & metadata,span<std::byte> value_buffer,size_t offset_bytes) const582 StatusWithSize KeyValueStore::Get(Key key,
583 const EntryMetadata& metadata,
584 span<std::byte> value_buffer,
585 size_t offset_bytes) const {
586 Entry entry;
587
588 PW_TRY_WITH_SIZE(ReadEntry(metadata, entry));
589
590 StatusWithSize result = entry.ReadValue(value_buffer, offset_bytes);
591 if (result.ok() && options_.verify_on_read && offset_bytes == 0u) {
592 Status verify_result =
593 entry.VerifyChecksum(key, value_buffer.first(result.size()));
594 if (!verify_result.ok()) {
595 std::memset(value_buffer.data(), 0, result.size());
596 return StatusWithSize(verify_result, 0);
597 }
598
599 return StatusWithSize(verify_result, result.size());
600 }
601 return result;
602 }
603
FixedSizeGet(Key key,void * value,size_t size_bytes) const604 Status KeyValueStore::FixedSizeGet(Key key,
605 void* value,
606 size_t size_bytes) const {
607 PW_TRY(CheckWriteOperation(key));
608
609 EntryMetadata metadata;
610 PW_TRY(FindExisting(key, &metadata));
611
612 return FixedSizeGet(key, metadata, value, size_bytes);
613 }
614
FixedSizeGet(Key key,const EntryMetadata & metadata,void * value,size_t size_bytes) const615 Status KeyValueStore::FixedSizeGet(Key key,
616 const EntryMetadata& metadata,
617 void* value,
618 size_t size_bytes) const {
619 // Ensure that the size of the stored value matches the size of the type.
620 // Otherwise, report error. This check avoids potential memory corruption.
621 PW_TRY_ASSIGN(const size_t actual_size, ValueSize(metadata));
622
623 if (actual_size != size_bytes) {
624 PW_LOG_DEBUG("Requested %u B read, but value is %u B",
625 unsigned(size_bytes),
626 unsigned(actual_size));
627 return Status::InvalidArgument();
628 }
629
630 StatusWithSize result =
631 Get(key, metadata, span(static_cast<byte*>(value), size_bytes), 0);
632
633 return result.status();
634 }
635
ValueSize(const EntryMetadata & metadata) const636 StatusWithSize KeyValueStore::ValueSize(const EntryMetadata& metadata) const {
637 Entry entry;
638 PW_TRY_WITH_SIZE(ReadEntry(metadata, entry));
639
640 return StatusWithSize(entry.value_size());
641 }
642
CheckWriteOperation(Key key) const643 Status KeyValueStore::CheckWriteOperation(Key key) const {
644 if (InvalidKey(key)) {
645 return Status::InvalidArgument();
646 }
647
648 // For normal write operation the KVS must be fully ready.
649 if (!initialized()) {
650 return Status::FailedPrecondition();
651 }
652 return OkStatus();
653 }
654
CheckReadOperation(Key key) const655 Status KeyValueStore::CheckReadOperation(Key key) const {
656 if (InvalidKey(key)) {
657 return Status::InvalidArgument();
658 }
659
660 // Operations that are explicitly read-only can be done after init() has been
661 // called but not fully ready (when needing maintenance).
662 if (initialized_ == InitializationState::kNotInitialized) {
663 return Status::FailedPrecondition();
664 }
665 return OkStatus();
666 }
667
WriteEntryForExistingKey(EntryMetadata & metadata,EntryState new_state,Key key,span<const byte> value)668 Status KeyValueStore::WriteEntryForExistingKey(EntryMetadata& metadata,
669 EntryState new_state,
670 Key key,
671 span<const byte> value) {
672 // Read the original entry to get the size for sector accounting purposes.
673 Entry entry;
674 PW_TRY(ReadEntry(metadata, entry));
675
676 return WriteEntry(key, value, new_state, &metadata, &entry);
677 }
678
WriteEntryForNewKey(Key key,span<const byte> value)679 Status KeyValueStore::WriteEntryForNewKey(Key key, span<const byte> value) {
680 // If there is no room in the cache for a new entry, it is possible some cache
681 // entries could be freed by removing deleted keys. If deleted key removal is
682 // enabled and the KVS is configured to make all possible writes succeed,
683 // attempt heavy maintenance now.
684 #if PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE
685 if (options_.gc_on_write == GargbageCollectOnWrite::kAsManySectorsNeeded &&
686 entry_cache_.full()) {
687 Status maintenance_status = HeavyMaintenance();
688 if (!maintenance_status.ok()) {
689 PW_LOG_WARN("KVS Maintenance failed for write: %s",
690 maintenance_status.str());
691 return maintenance_status;
692 }
693 }
694 #endif // PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE
695
696 if (entry_cache_.full()) {
697 PW_LOG_WARN(
698 "KVS full: trying to store a new entry, but can't. Have %u entries",
699 unsigned(entry_cache_.total_entries()));
700 return Status::ResourceExhausted();
701 }
702
703 return WriteEntry(key, value, EntryState::kValid);
704 }
705
WriteEntry(Key key,span<const byte> value,EntryState new_state,EntryMetadata * prior_metadata,const Entry * prior_entry)706 Status KeyValueStore::WriteEntry(Key key,
707 span<const byte> value,
708 EntryState new_state,
709 EntryMetadata* prior_metadata,
710 const Entry* prior_entry) {
711 // If new entry and prior entry have matching value size, state, and checksum,
712 // check if the values match. Directly compare the prior and new values
713 // because the checksum can not be depended on to establish equality, it can
714 // only be depended on to establish inequality.
715 if (prior_entry != nullptr && prior_entry->value_size() == value.size() &&
716 prior_metadata->state() == new_state &&
717 prior_entry->ValueMatches(value).ok()) {
718 // The new value matches the prior value, don't need to write anything. Just
719 // keep the existing entry.
720 PW_LOG_DEBUG("Write for key 0x%08x with matching value skipped",
721 unsigned(prior_metadata->hash()));
722 return OkStatus();
723 }
724
725 // List of addresses for sectors with space for this entry.
726 Address* reserved_addresses = entry_cache_.TempReservedAddressesForWrite();
727
728 // Find addresses to write the entry to. This may involve garbage collecting
729 // one or more sectors.
730 const size_t entry_size = Entry::size(partition_, key, value);
731 PW_TRY(GetAddressesForWrite(reserved_addresses, entry_size));
732
733 // Write the entry at the first address that was found.
734 Entry entry = CreateEntry(reserved_addresses[0], key, value, new_state);
735 PW_TRY(AppendEntry(entry, key, value));
736
737 // After writing the first entry successfully, update the key descriptors.
738 // Once a single new the entry is written, the old entries are invalidated.
739 size_t prior_size = prior_entry != nullptr ? prior_entry->size() : 0;
740 EntryMetadata new_metadata =
741 CreateOrUpdateKeyDescriptor(entry, key, prior_metadata, prior_size);
742
743 // Write the additional copies of the entry, if redundancy is greater than 1.
744 for (size_t i = 1; i < redundancy(); ++i) {
745 entry.set_address(reserved_addresses[i]);
746 PW_TRY(AppendEntry(entry, key, value));
747 new_metadata.AddNewAddress(reserved_addresses[i]);
748 }
749 return OkStatus();
750 }
751
CreateOrUpdateKeyDescriptor(const Entry & entry,Key key,EntryMetadata * prior_metadata,size_t prior_size)752 KeyValueStore::EntryMetadata KeyValueStore::CreateOrUpdateKeyDescriptor(
753 const Entry& entry,
754 Key key,
755 EntryMetadata* prior_metadata,
756 size_t prior_size) {
757 // If there is no prior descriptor, create a new one.
758 if (prior_metadata == nullptr) {
759 return entry_cache_.AddNew(entry.descriptor(key), entry.address());
760 }
761
762 return UpdateKeyDescriptor(
763 entry, entry.address(), prior_metadata, prior_size);
764 }
765
UpdateKeyDescriptor(const Entry & entry,Address new_address,EntryMetadata * prior_metadata,size_t prior_size)766 KeyValueStore::EntryMetadata KeyValueStore::UpdateKeyDescriptor(
767 const Entry& entry,
768 Address new_address,
769 EntryMetadata* prior_metadata,
770 size_t prior_size) {
771 // Remove valid bytes for the old entry and its copies, which are now stale.
772 for (Address address : prior_metadata->addresses()) {
773 sectors_.FromAddress(address).RemoveValidBytes(prior_size);
774 }
775
776 prior_metadata->Reset(entry.descriptor(prior_metadata->hash()), new_address);
777 return *prior_metadata;
778 }
779
GetAddressesForWrite(Address * write_addresses,size_t write_size)780 Status KeyValueStore::GetAddressesForWrite(Address* write_addresses,
781 size_t write_size) {
782 for (size_t i = 0; i < redundancy(); i++) {
783 SectorDescriptor* sector;
784 PW_TRY(GetSectorForWrite(§or, write_size, span(write_addresses, i)));
785 write_addresses[i] = sectors_.NextWritableAddress(*sector);
786
787 PW_LOG_DEBUG("Found space for entry in sector %u at address %u",
788 sectors_.Index(sector),
789 unsigned(write_addresses[i]));
790 }
791
792 return OkStatus();
793 }
794
795 // Finds a sector to use for writing a new entry to. Does automatic garbage
796 // collection if needed and allowed.
797 //
798 // OK: Sector found with needed space.
799 // RESOURCE_EXHAUSTED: No sector available with the needed space.
GetSectorForWrite(SectorDescriptor ** sector,size_t entry_size,span<const Address> reserved_addresses)800 Status KeyValueStore::GetSectorForWrite(
801 SectorDescriptor** sector,
802 size_t entry_size,
803 span<const Address> reserved_addresses) {
804 Status result = sectors_.FindSpace(sector, entry_size, reserved_addresses);
805
806 size_t gc_sector_count = 0;
807 bool do_auto_gc = options_.gc_on_write != GargbageCollectOnWrite::kDisabled;
808
809 // Do garbage collection as needed, so long as policy allows.
810 while (result.IsResourceExhausted() && do_auto_gc) {
811 if (options_.gc_on_write == GargbageCollectOnWrite::kOneSector) {
812 // If GC config option is kOneSector clear the flag to not do any more
813 // GC after this try.
814 do_auto_gc = false;
815 }
816 // Garbage collect and then try again to find the best sector.
817 Status gc_status = GarbageCollect(reserved_addresses);
818 if (!gc_status.ok()) {
819 if (gc_status.IsNotFound()) {
820 // Not enough space, and no reclaimable bytes, this KVS is full!
821 return Status::ResourceExhausted();
822 }
823 return gc_status;
824 }
825
826 result = sectors_.FindSpace(sector, entry_size, reserved_addresses);
827
828 gc_sector_count++;
829 // Allow total sectors + 2 number of GC cycles so that once reclaimable
830 // bytes in all the sectors have been reclaimed can try and free up space by
831 // moving entries for keys other than the one being worked on in to sectors
832 // that have copies of the key trying to be written.
833 if (gc_sector_count > (partition_.sector_count() + 2)) {
834 PW_LOG_ERROR("Did more GC sectors than total sectors!!!!");
835 return Status::ResourceExhausted();
836 }
837 }
838
839 if (!result.ok()) {
840 PW_LOG_WARN("Unable to find sector to write %u B", unsigned(entry_size));
841 }
842 return result;
843 }
844
MarkSectorCorruptIfNotOk(Status status,SectorDescriptor * sector)845 Status KeyValueStore::MarkSectorCorruptIfNotOk(Status status,
846 SectorDescriptor* sector) {
847 if (!status.ok()) {
848 PW_LOG_DEBUG(" Sector %u corrupt", sectors_.Index(sector));
849 sector->mark_corrupt();
850 error_detected_ = true;
851 }
852 return status;
853 }
854
AppendEntry(const Entry & entry,Key key,span<const byte> value)855 Status KeyValueStore::AppendEntry(const Entry& entry,
856 Key key,
857 span<const byte> value) {
858 const StatusWithSize result = entry.Write(key, value);
859
860 SectorDescriptor& sector = sectors_.FromAddress(entry.address());
861
862 if (!result.ok()) {
863 PW_LOG_ERROR("Failed to write %u bytes at %#x. %u actually written",
864 unsigned(entry.size()),
865 unsigned(entry.address()),
866 unsigned(result.size()));
867 PW_TRY(MarkSectorCorruptIfNotOk(result.status(), §or));
868 }
869
870 if (options_.verify_on_write) {
871 PW_TRY(MarkSectorCorruptIfNotOk(entry.VerifyChecksumInFlash(), §or));
872 }
873
874 sector.RemoveWritableBytes(result.size());
875 sector.AddValidBytes(result.size());
876 return OkStatus();
877 }
878
CopyEntryToSector(Entry & entry,SectorDescriptor * new_sector,Address new_address)879 StatusWithSize KeyValueStore::CopyEntryToSector(Entry& entry,
880 SectorDescriptor* new_sector,
881 Address new_address) {
882 const StatusWithSize result = entry.Copy(new_address);
883
884 PW_TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(result.status(), new_sector));
885
886 if (options_.verify_on_write) {
887 Entry new_entry;
888 PW_TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(
889 Entry::Read(partition_, new_address, formats_, &new_entry),
890 new_sector));
891 // TODO(davidrogers): add test that catches doing the verify on the old
892 // entry.
893 PW_TRY_WITH_SIZE(MarkSectorCorruptIfNotOk(new_entry.VerifyChecksumInFlash(),
894 new_sector));
895 }
896 // Entry was written successfully; update descriptor's address and the sector
897 // descriptors to reflect the new entry.
898 new_sector->RemoveWritableBytes(result.size());
899 new_sector->AddValidBytes(result.size());
900
901 return result;
902 }
903
RelocateEntry(const EntryMetadata & metadata,KeyValueStore::Address & address,span<const Address> reserved_addresses)904 Status KeyValueStore::RelocateEntry(const EntryMetadata& metadata,
905 KeyValueStore::Address& address,
906 span<const Address> reserved_addresses) {
907 Entry entry;
908 PW_TRY(ReadEntry(metadata, entry));
909
910 // Find a new sector for the entry and write it to the new location. For
911 // relocation the find should not not be a sector already containing the key
912 // but can be the always empty sector, since this is part of the GC process
913 // that will result in a new empty sector. Also find a sector that does not
914 // have reclaimable space (mostly for the full GC, where that would result in
915 // an immediate extra relocation).
916 SectorDescriptor* new_sector;
917
918 PW_TRY(sectors_.FindSpaceDuringGarbageCollection(
919 &new_sector, entry.size(), metadata.addresses(), reserved_addresses));
920
921 Address new_address = sectors_.NextWritableAddress(*new_sector);
922 PW_TRY_ASSIGN(const size_t result_size,
923 CopyEntryToSector(entry, new_sector, new_address));
924 sectors_.FromAddress(address).RemoveValidBytes(result_size);
925 address = new_address;
926
927 return OkStatus();
928 }
929
FullMaintenanceHelper(MaintenanceType maintenance_type)930 Status KeyValueStore::FullMaintenanceHelper(MaintenanceType maintenance_type) {
931 if (initialized_ == InitializationState::kNotInitialized) {
932 return Status::FailedPrecondition();
933 }
934
935 // Full maintenance can be a potentially heavy operation, and should be
936 // relatively infrequent, so log start/end at INFO level.
937 PW_LOG_INFO("Beginning full maintenance");
938 CheckForErrors();
939
940 // Step 1: Repair errors
941 if (error_detected_) {
942 PW_TRY(Repair());
943 }
944
945 // Step 2: Make sure all the entries are on the primary format.
946 StatusWithSize update_status = UpdateEntriesToPrimaryFormat();
947 Status overall_status = update_status.status();
948
949 if (!overall_status.ok()) {
950 PW_LOG_ERROR("Failed to update all entries to the primary format");
951 }
952
953 SectorDescriptor* sector = sectors_.last_new();
954
955 // Calculate number of bytes for the threshold.
956 size_t threshold_bytes =
957 (partition_.size_bytes() * kGcUsageThresholdPercentage) / 100;
958
959 // Is bytes in use over the threshold.
960 StorageStats stats = GetStorageStats();
961 bool over_usage_threshold = stats.in_use_bytes > threshold_bytes;
962 bool heavy = (maintenance_type == MaintenanceType::kHeavy);
963 bool force_gc = heavy || over_usage_threshold || (update_status.size() > 0);
964
965 auto do_garbage_collect_pass = [&]() {
966 // TODO(drempel): look in to making an iterator method for cycling through
967 // sectors starting from last_new_sector_.
968 Status gc_status;
969 for (size_t j = 0; j < sectors_.size(); j++) {
970 sector += 1;
971 if (sector == sectors_.end()) {
972 sector = sectors_.begin();
973 }
974
975 if (sector->RecoverableBytes(partition_.sector_size_bytes()) > 0 &&
976 (force_gc || sector->valid_bytes() == 0)) {
977 gc_status = GarbageCollectSector(*sector, {});
978 if (!gc_status.ok()) {
979 PW_LOG_ERROR("Failed to garbage collect all sectors");
980 break;
981 }
982 }
983 }
984 if (overall_status.ok()) {
985 overall_status = gc_status;
986 }
987 };
988
989 // Step 3: Do full garbage collect pass for all sectors. This will erase all
990 // old/state entries from flash and leave only current/valid entries.
991 do_garbage_collect_pass();
992
993 #if PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE
994 // Step 4: (if heavy maintenance) garbage collect all the deleted keys.
995 if (heavy) {
996 // If enabled, remove deleted keys from the entry cache, including freeing
997 // sector bytes used by those keys. This must only be done directly after a
998 // full garbage collection, otherwise the current deleted entry could be
999 // garbage collected before the older stale entry producing a window for an
1000 // invalid/corrupted KVS state if there was a power-fault, crash or other
1001 // interruption.
1002 overall_status.Update(RemoveDeletedKeyEntries());
1003
1004 // Do another garbage collect pass that will fully remove the deleted keys
1005 // from flash. Garbage collect will only touch sectors that have something
1006 // to garbage collect, which in this case is only sectors containing deleted
1007 // keys.
1008 do_garbage_collect_pass();
1009 }
1010 #endif // PW_KVS_REMOVE_DELETED_KEYS_IN_HEAVY_MAINTENANCE
1011
1012 if (overall_status.ok()) {
1013 PW_LOG_INFO("Full maintenance complete");
1014 } else {
1015 PW_LOG_ERROR("Full maintenance finished with some errors");
1016 }
1017 return overall_status;
1018 }
1019
PartialMaintenance()1020 Status KeyValueStore::PartialMaintenance() {
1021 if (initialized_ == InitializationState::kNotInitialized) {
1022 return Status::FailedPrecondition();
1023 }
1024
1025 CheckForErrors();
1026 // Do automatic repair, if KVS options allow for it.
1027 if (error_detected_ && options_.recovery != ErrorRecovery::kManual) {
1028 PW_TRY(Repair());
1029 }
1030 return GarbageCollect(span<const Address>());
1031 }
1032
GarbageCollect(span<const Address> reserved_addresses)1033 Status KeyValueStore::GarbageCollect(span<const Address> reserved_addresses) {
1034 PW_LOG_DEBUG("Garbage Collect a single sector");
1035 for ([[maybe_unused]] Address address : reserved_addresses) {
1036 PW_LOG_DEBUG(" Avoid address %u", unsigned(address));
1037 }
1038
1039 // Step 1: Find the sector to garbage collect
1040 SectorDescriptor* sector_to_gc =
1041 sectors_.FindSectorToGarbageCollect(reserved_addresses);
1042
1043 if (sector_to_gc == nullptr) {
1044 // Nothing to GC.
1045 return Status::NotFound();
1046 }
1047
1048 // Step 2: Garbage collect the selected sector.
1049 return GarbageCollectSector(*sector_to_gc, reserved_addresses);
1050 }
1051
RelocateKeyAddressesInSector(SectorDescriptor & sector_to_gc,const EntryMetadata & metadata,span<const Address> reserved_addresses)1052 Status KeyValueStore::RelocateKeyAddressesInSector(
1053 SectorDescriptor& sector_to_gc,
1054 const EntryMetadata& metadata,
1055 span<const Address> reserved_addresses) {
1056 for (FlashPartition::Address& address : metadata.addresses()) {
1057 if (sectors_.AddressInSector(sector_to_gc, address)) {
1058 PW_LOG_DEBUG(" Relocate entry for Key 0x%08" PRIx32 ", sector %u",
1059 metadata.hash(),
1060 sectors_.Index(sectors_.FromAddress(address)));
1061 PW_TRY(RelocateEntry(metadata, address, reserved_addresses));
1062 }
1063 }
1064
1065 return OkStatus();
1066 }
1067
GarbageCollectSector(SectorDescriptor & sector_to_gc,span<const Address> reserved_addresses)1068 Status KeyValueStore::GarbageCollectSector(
1069 SectorDescriptor& sector_to_gc, span<const Address> reserved_addresses) {
1070 PW_LOG_DEBUG(" Garbage Collect sector %u", sectors_.Index(sector_to_gc));
1071
1072 // Step 1: Move any valid entries in the GC sector to other sectors
1073 if (sector_to_gc.valid_bytes() != 0) {
1074 for (EntryMetadata& metadata : entry_cache_) {
1075 PW_TRY(RelocateKeyAddressesInSector(
1076 sector_to_gc, metadata, reserved_addresses));
1077 }
1078 }
1079
1080 if (sector_to_gc.valid_bytes() != 0) {
1081 PW_LOG_ERROR(
1082 " Failed to relocate valid entries from sector being garbage "
1083 "collected, %u valid bytes remain",
1084 unsigned(sector_to_gc.valid_bytes()));
1085 return Status::Internal();
1086 }
1087
1088 // Step 2: Reinitialize the sector
1089 if (!sector_to_gc.Empty(partition_.sector_size_bytes())) {
1090 sector_to_gc.mark_corrupt();
1091 internal_stats_.sector_erase_count++;
1092 PW_TRY(partition_.Erase(sectors_.BaseAddress(sector_to_gc), 1));
1093 sector_to_gc.set_writable_bytes(partition_.sector_size_bytes());
1094 }
1095
1096 PW_LOG_DEBUG(" Garbage Collect sector %u complete",
1097 sectors_.Index(sector_to_gc));
1098 return OkStatus();
1099 }
1100
UpdateEntriesToPrimaryFormat()1101 StatusWithSize KeyValueStore::UpdateEntriesToPrimaryFormat() {
1102 size_t entries_updated = 0;
1103 for (EntryMetadata& prior_metadata : entry_cache_) {
1104 Entry entry;
1105 PW_TRY_WITH_SIZE(ReadEntry(prior_metadata, entry));
1106 if (formats_.primary().magic == entry.magic()) {
1107 // Ignore entries that are already on the primary format.
1108 continue;
1109 }
1110
1111 PW_LOG_DEBUG(
1112 "Updating entry 0x%08x from old format [0x%08x] to new format "
1113 "[0x%08x]",
1114 unsigned(prior_metadata.hash()),
1115 unsigned(entry.magic()),
1116 unsigned(formats_.primary().magic));
1117
1118 entries_updated++;
1119
1120 last_transaction_id_ += 1;
1121 PW_TRY_WITH_SIZE(entry.Update(formats_.primary(), last_transaction_id_));
1122
1123 // List of addresses for sectors with space for this entry.
1124 Address* reserved_addresses = entry_cache_.TempReservedAddressesForWrite();
1125
1126 // Find addresses to write the entry to. This may involve garbage collecting
1127 // one or more sectors.
1128 PW_TRY_WITH_SIZE(GetAddressesForWrite(reserved_addresses, entry.size()));
1129
1130 PW_TRY_WITH_SIZE(
1131 CopyEntryToSector(entry,
1132 §ors_.FromAddress(reserved_addresses[0]),
1133 reserved_addresses[0]));
1134
1135 // After writing the first entry successfully, update the key descriptors.
1136 // Once a single new the entry is written, the old entries are invalidated.
1137 EntryMetadata new_metadata = UpdateKeyDescriptor(
1138 entry, reserved_addresses[0], &prior_metadata, entry.size());
1139
1140 // Write the additional copies of the entry, if redundancy is greater
1141 // than 1.
1142 for (size_t i = 1; i < redundancy(); ++i) {
1143 PW_TRY_WITH_SIZE(
1144 CopyEntryToSector(entry,
1145 §ors_.FromAddress(reserved_addresses[i]),
1146 reserved_addresses[i]));
1147 new_metadata.AddNewAddress(reserved_addresses[i]);
1148 }
1149 }
1150
1151 return StatusWithSize(entries_updated);
1152 }
1153
1154 // Add any missing redundant entries/copies for a key.
AddRedundantEntries(EntryMetadata & metadata)1155 Status KeyValueStore::AddRedundantEntries(EntryMetadata& metadata) {
1156 Entry entry;
1157 PW_TRY(ReadEntry(metadata, entry));
1158 PW_TRY(entry.VerifyChecksumInFlash());
1159
1160 while (metadata.addresses().size() < redundancy()) {
1161 SectorDescriptor* new_sector;
1162 PW_TRY(GetSectorForWrite(&new_sector, entry.size(), metadata.addresses()));
1163
1164 Address new_address = sectors_.NextWritableAddress(*new_sector);
1165 PW_TRY(CopyEntryToSector(entry, new_sector, new_address));
1166
1167 metadata.AddNewAddress(new_address);
1168 }
1169 return OkStatus();
1170 }
1171
RepairCorruptSectors()1172 Status KeyValueStore::RepairCorruptSectors() {
1173 // Try to GC each corrupt sector, even if previous sectors fail. If GC of a
1174 // sector failed on the first pass, then do a second pass, since a later
1175 // sector might have cleared up space or otherwise unblocked the earlier
1176 // failed sector.
1177 Status repair_status = OkStatus();
1178
1179 size_t loop_count = 0;
1180 do {
1181 loop_count++;
1182 // Error of RESOURCE_EXHAUSTED indicates no space found for relocation.
1183 // Reset back to OK for the next pass.
1184 if (repair_status.IsResourceExhausted()) {
1185 repair_status = OkStatus();
1186 }
1187
1188 PW_LOG_DEBUG(" Pass %u", unsigned(loop_count));
1189 for (SectorDescriptor& sector : sectors_) {
1190 if (sector.corrupt()) {
1191 PW_LOG_DEBUG(" Found sector %u with corruption",
1192 sectors_.Index(sector));
1193 Status sector_status = GarbageCollectSector(sector, {});
1194 if (sector_status.ok()) {
1195 internal_stats_.corrupt_sectors_recovered += 1;
1196 } else if (repair_status.ok() || repair_status.IsResourceExhausted()) {
1197 repair_status = sector_status;
1198 }
1199 }
1200 }
1201 PW_LOG_DEBUG(" Pass %u complete", unsigned(loop_count));
1202 } while (!repair_status.ok() && loop_count < 2);
1203
1204 return repair_status;
1205 }
1206
EnsureFreeSectorExists()1207 Status KeyValueStore::EnsureFreeSectorExists() {
1208 Status repair_status = OkStatus();
1209 bool empty_sector_found = false;
1210
1211 PW_LOG_DEBUG(" Find empty sector");
1212 for (SectorDescriptor& sector : sectors_) {
1213 if (sector.Empty(partition_.sector_size_bytes())) {
1214 empty_sector_found = true;
1215 PW_LOG_DEBUG(" Empty sector found");
1216 break;
1217 }
1218 }
1219 if (empty_sector_found == false) {
1220 PW_LOG_DEBUG(" No empty sector found, attempting to GC a free sector");
1221 Status sector_status = GarbageCollect(span<const Address, 0>());
1222 if (repair_status.ok() && !sector_status.ok()) {
1223 PW_LOG_DEBUG(" Unable to free an empty sector");
1224 repair_status = sector_status;
1225 }
1226 }
1227
1228 return repair_status;
1229 }
1230
EnsureEntryRedundancy()1231 Status KeyValueStore::EnsureEntryRedundancy() {
1232 Status repair_status = OkStatus();
1233
1234 if (redundancy() == 1) {
1235 PW_LOG_DEBUG(" Redundancy not in use, nothting to check");
1236 return OkStatus();
1237 }
1238
1239 PW_LOG_DEBUG(
1240 " Write any needed additional duplicate copies of keys to fulfill %u"
1241 " redundancy",
1242 unsigned(redundancy()));
1243 for (EntryMetadata& metadata : entry_cache_) {
1244 if (metadata.addresses().size() >= redundancy()) {
1245 continue;
1246 }
1247
1248 PW_LOG_DEBUG(" Key with %u of %u copies found, adding missing copies",
1249 unsigned(metadata.addresses().size()),
1250 unsigned(redundancy()));
1251 Status fill_status = AddRedundantEntries(metadata);
1252 if (fill_status.ok()) {
1253 internal_stats_.missing_redundant_entries_recovered += 1;
1254 PW_LOG_DEBUG(" Key missing copies added");
1255 } else {
1256 PW_LOG_DEBUG(" Failed to add key missing copies");
1257 if (repair_status.ok()) {
1258 repair_status = fill_status;
1259 }
1260 }
1261 }
1262
1263 return repair_status;
1264 }
1265
FixErrors()1266 Status KeyValueStore::FixErrors() {
1267 PW_LOG_DEBUG("Fixing KVS errors");
1268
1269 // Step 1: Garbage collect any sectors marked as corrupt.
1270 Status overall_status = RepairCorruptSectors();
1271
1272 // Step 2: Make sure there is at least 1 empty sector. This needs to be a
1273 // seperate check of sectors from step 1, because a found empty sector might
1274 // get written to by a later GC that fails and does not result in a free
1275 // sector.
1276 Status repair_status = EnsureFreeSectorExists();
1277 if (overall_status.ok()) {
1278 overall_status = repair_status;
1279 }
1280
1281 // Step 3: Make sure each stored key has the full number of redundant
1282 // entries.
1283 repair_status = EnsureEntryRedundancy();
1284 if (overall_status.ok()) {
1285 overall_status = repair_status;
1286 }
1287
1288 if (overall_status.ok()) {
1289 error_detected_ = false;
1290 initialized_ = InitializationState::kReady;
1291 }
1292 return overall_status;
1293 }
1294
Repair()1295 Status KeyValueStore::Repair() {
1296 // If errors have been detected, just reinit the KVS metadata. This does a
1297 // full deep error check and any needed repairs. Then repair any errors.
1298 PW_LOG_INFO("Starting KVS repair");
1299
1300 PW_LOG_DEBUG("Reinitialize KVS metadata");
1301 InitializeMetadata()
1302 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
1303
1304 return FixErrors();
1305 }
1306
CreateEntry(Address address,Key key,span<const byte> value,EntryState state)1307 KeyValueStore::Entry KeyValueStore::CreateEntry(Address address,
1308 Key key,
1309 span<const byte> value,
1310 EntryState state) {
1311 // Always bump the transaction ID when creating a new entry.
1312 //
1313 // Burning transaction IDs prevents inconsistencies between flash and memory
1314 // that which could happen if a write succeeds, but for some reason the read
1315 // and verify step fails. Here's how this would happen:
1316 //
1317 // 1. The entry is written but for some reason the flash reports failure OR
1318 // The write succeeds, but the read / verify operation fails.
1319 // 2. The transaction ID is NOT incremented, because of the failure
1320 // 3. (later) A new entry is written, re-using the transaction ID (oops)
1321 //
1322 // By always burning transaction IDs, the above problem can't happen.
1323 last_transaction_id_ += 1;
1324
1325 if (state == EntryState::kDeleted) {
1326 return Entry::Tombstone(
1327 partition_, address, formats_.primary(), key, last_transaction_id_);
1328 }
1329 return Entry::Valid(partition_,
1330 address,
1331 formats_.primary(),
1332 key,
1333 value,
1334 last_transaction_id_);
1335 }
1336
LogDebugInfo() const1337 void KeyValueStore::LogDebugInfo() const {
1338 const size_t sector_size_bytes = partition_.sector_size_bytes();
1339 PW_LOG_DEBUG(
1340 "====================== KEY VALUE STORE DUMP =========================");
1341 PW_LOG_DEBUG(" ");
1342 PW_LOG_DEBUG("Flash partition:");
1343 PW_LOG_DEBUG(" Sector count = %u", unsigned(partition_.sector_count()));
1344 PW_LOG_DEBUG(" Sector max count = %u", unsigned(sectors_.max_size()));
1345 PW_LOG_DEBUG(" Sectors in use = %u", unsigned(sectors_.size()));
1346 PW_LOG_DEBUG(" Sector size = %u", unsigned(sector_size_bytes));
1347 PW_LOG_DEBUG(" Total size = %u", unsigned(partition_.size_bytes()));
1348 PW_LOG_DEBUG(" Alignment = %u",
1349 unsigned(partition_.alignment_bytes()));
1350 PW_LOG_DEBUG(" ");
1351 PW_LOG_DEBUG("Key descriptors:");
1352 PW_LOG_DEBUG(" Entry count = %u",
1353 unsigned(entry_cache_.total_entries()));
1354 PW_LOG_DEBUG(" Max entry count = %u", unsigned(entry_cache_.max_entries()));
1355 PW_LOG_DEBUG(" ");
1356 PW_LOG_DEBUG(" # hash version address address (hex)");
1357 size_t count = 0;
1358 for (const EntryMetadata& metadata : entry_cache_) {
1359 PW_LOG_DEBUG(" |%3zu: | %8zx |%8zu | %8zu | %8zx",
1360 count++,
1361 size_t(metadata.hash()),
1362 size_t(metadata.transaction_id()),
1363 size_t(metadata.first_address()),
1364 size_t(metadata.first_address()));
1365 }
1366 PW_LOG_DEBUG(" ");
1367
1368 PW_LOG_DEBUG("Sector descriptors:");
1369 PW_LOG_DEBUG(" # tail free valid has_space");
1370 for (const SectorDescriptor& sd : sectors_) {
1371 PW_LOG_DEBUG(" |%3u: | %8zu |%8zu | %s",
1372 sectors_.Index(sd),
1373 size_t(sd.writable_bytes()),
1374 sd.valid_bytes(),
1375 sd.writable_bytes() ? "YES" : "");
1376 }
1377 PW_LOG_DEBUG(" ");
1378
1379 // TODO(keir): This should stop logging after some threshold.
1380 // size_t dumped_bytes = 0;
1381 PW_LOG_DEBUG("Sector raw data:");
1382 for (size_t sector_id = 0; sector_id < sectors_.size(); ++sector_id) {
1383 // Read sector data. Yes, this will blow the stack on embedded.
1384 std::array<byte, 500> raw_sector_data; // TODO!!!
1385 [[maybe_unused]] StatusWithSize sws =
1386 partition_.Read(sector_id * sector_size_bytes, raw_sector_data);
1387 PW_LOG_DEBUG("Read: %u bytes", unsigned(sws.size()));
1388
1389 PW_LOG_DEBUG(" base addr offs 0 1 2 3 4 5 6 7");
1390 for (size_t i = 0; i < sector_size_bytes; i += 8) {
1391 PW_LOG_DEBUG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x",
1392 sector_id,
1393 (sector_id * sector_size_bytes) + i,
1394 i,
1395 static_cast<unsigned int>(raw_sector_data[i + 0]),
1396 static_cast<unsigned int>(raw_sector_data[i + 1]),
1397 static_cast<unsigned int>(raw_sector_data[i + 2]),
1398 static_cast<unsigned int>(raw_sector_data[i + 3]),
1399 static_cast<unsigned int>(raw_sector_data[i + 4]),
1400 static_cast<unsigned int>(raw_sector_data[i + 5]),
1401 static_cast<unsigned int>(raw_sector_data[i + 6]),
1402 static_cast<unsigned int>(raw_sector_data[i + 7]));
1403
1404 // TODO(keir): Fix exit condition.
1405 if (i > 128) {
1406 break;
1407 }
1408 }
1409 PW_LOG_DEBUG(" ");
1410 }
1411
1412 PW_LOG_DEBUG(
1413 "////////////////////// KEY VALUE STORE DUMP END /////////////////////");
1414 }
1415
LogSectors() const1416 void KeyValueStore::LogSectors() const {
1417 PW_LOG_DEBUG("Sector descriptors: count %u", unsigned(sectors_.size()));
1418 for (auto& sector : sectors_) {
1419 PW_LOG_DEBUG(
1420 " - Sector %u: valid %u, recoverable %u, free %u",
1421 sectors_.Index(sector),
1422 unsigned(sector.valid_bytes()),
1423 unsigned(sector.RecoverableBytes(partition_.sector_size_bytes())),
1424 unsigned(sector.writable_bytes()));
1425 }
1426 }
1427
LogKeyDescriptor() const1428 void KeyValueStore::LogKeyDescriptor() const {
1429 PW_LOG_DEBUG("Key descriptors: count %u",
1430 unsigned(entry_cache_.total_entries()));
1431 for (const EntryMetadata& metadata : entry_cache_) {
1432 PW_LOG_DEBUG(" - Key: %s, hash %#x, transaction ID %u, first address %#x",
1433 metadata.state() == EntryState::kDeleted ? "Deleted" : "Valid",
1434 unsigned(metadata.hash()),
1435 unsigned(metadata.transaction_id()),
1436 unsigned(metadata.first_address()));
1437 }
1438 }
1439
1440 } // namespace pw::kvs
1441