blob: c6ccca8f37c31f6801311230d038de0ebb4271c3 [file] [log] [blame]
Wyatt Heplerb7609542020-01-24 10:29:54 -08001// 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
Wyatt Heplerb7609542020-01-24 10:29:54 -080015#include "pw_kvs/key_value_store.h"
16
17#include <cstring>
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080018#include <type_traits>
Wyatt Heplerb7609542020-01-24 10:29:54 -080019
Keir Mierle8c352dc2020-02-02 13:58:19 -080020#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080021#include "pw_kvs_private/format.h"
22#include "pw_kvs_private/macros.h"
Keir Mierle8c352dc2020-02-02 13:58:19 -080023#include "pw_log/log.h"
Wyatt Heplerb7609542020-01-24 10:29:54 -080024
Wyatt Hepler2ad60672020-01-21 08:00:16 -080025namespace pw::kvs {
Wyatt Heplerb7609542020-01-24 10:29:54 -080026
Wyatt Hepleracaacf92020-01-24 10:58:30 -080027using std::byte;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080028using std::string_view;
Wyatt Hepleracaacf92020-01-24 10:58:30 -080029
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080030namespace {
31
32// constexpr uint32_t SixFiveFiveNineNine(std::string_view string) {
33constexpr uint32_t HashKey(std::string_view string) {
34 uint32_t hash = 0;
35 uint32_t coefficient = 65599u;
36
37 for (char ch : string) {
38 hash += coefficient * unsigned(ch);
39 coefficient *= 65599u;
Wyatt Heplerb7609542020-01-24 10:29:54 -080040 }
41
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080042 return hash;
Wyatt Heplerb7609542020-01-24 10:29:54 -080043}
44
Keir Mierle8c352dc2020-02-02 13:58:19 -080045// TODO: Remove this when checksums in test are implemented.
46const uint32_t kNoChecksum = 0x12345678;
47
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080048constexpr size_t EntrySize(string_view key, span<const byte> value) {
49 return sizeof(EntryHeader) + key.size() + value.size();
50}
51
Keir Mierle8c352dc2020-02-02 13:58:19 -080052typedef FlashPartition::Address Address;
53
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080054} // namespace
55
56Status KeyValueStore::Init() {
Keir Mierle8c352dc2020-02-02 13:58:19 -080057 // Reset the number of occupied key descriptors; we will fill them later.
58 key_descriptor_list_size_ = 0;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080059
Keir Mierle8c352dc2020-02-02 13:58:19 -080060 const size_t sector_size_bytes = partition_.sector_size_bytes();
61 const size_t sector_count = partition_.sector_count();
62
63 DBG("First pass: Read all entries from all sectors");
64 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
65 // Track writable bytes in this sector. Updated after reading each entry.
66 sector_map_[sector_id].tail_free_bytes = sector_size_bytes;
67
68 const Address sector_address = sector_id * sector_size_bytes;
69 Address entry_address = sector_address;
70
71 for (int num_entries_in_sector = 0;; num_entries_in_sector++) {
72 DBG("Load entry: sector=%zu, entry#=%d, address=%zu",
73 sector_id,
74 num_entries_in_sector,
75 size_t(entry_address));
76
77 if (!AddressInSector(sector_map_[sector_id], entry_address)) {
78 DBG("Fell off end of sector; moving to the next sector");
79 break;
80 }
81
82 Address next_entry_address;
83 Status status = LoadEntry(entry_address, &next_entry_address);
84 if (status == Status::NOT_FOUND) {
85 DBG("Hit un-written data in sector; moving to the next sector");
86 break;
87 }
88 if (status == Status::DATA_LOSS) {
89 // It's not clear KVS can make a unilateral decision about what to do
90 // in corruption cases. It's an application decision, for which we
91 // should offer some configurability. For now, entirely bail out of
92 // loading and give up.
93 //
94 // Later, scan for remaining valid keys; since it's entirely possible
95 // that there is a duplicate of the key elsewhere and everything is
96 // fine. Later, we can wipe and maybe recover the sector.
97 //
98 // TODO: Implement rest-of-sector scanning for valid entries.
99 return Status::DATA_LOSS;
100 }
101 TRY(status);
102
103 // Entry loaded successfully; so get ready to load the next one.
104 entry_address = next_entry_address;
105
106 // Update of the number of writable bytes in this sector.
107 sector_map_[sector_id].tail_free_bytes =
108 sector_size_bytes - (entry_address - sector_address);
109 }
110 }
111
112 DBG("Second pass: Count valid bytes in each sector");
113 // Initialize the sector sizes.
114 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
115 sector_map_[sector_id].valid_bytes = 0;
116 }
117 // For every valid key, increment the valid bytes for that sector.
118 for (size_t key_id = 0; key_id < key_descriptor_list_size_; ++key_id) {
119 uint32_t sector_id =
120 key_descriptor_list_[key_id].address / sector_size_bytes;
121 KeyDescriptor key_descriptor;
122 key_descriptor.address = key_descriptor_list_[key_id].address;
123 EntryHeader header;
124 TRY(ReadEntryHeader(key_descriptor, &header));
125 sector_map_[sector_id].valid_bytes += header.entry_size();
126 }
127 enabled_ = true;
128 return Status::OK;
129}
130
131Status KeyValueStore::LoadEntry(Address entry_address,
132 Address* next_entry_address) {
133 const size_t alignment_bytes = partition_.alignment_bytes();
134
135 KeyDescriptor key_descriptor;
136 key_descriptor.address = entry_address;
137
138 EntryHeader header;
139 TRY(ReadEntryHeader(key_descriptor, &header));
140 // TODO: Should likely add a "LogHeader" method or similar.
141 DBG("Header: ");
142 DBG(" Address = 0x%zx", size_t(entry_address));
143 DBG(" Magic = 0x%zx", size_t(header.magic()));
144 DBG(" Checksum = 0x%zx", size_t(header.checksum_as_uint32()));
145 DBG(" Key length = 0x%zx", size_t(header.key_length()));
146 DBG(" Value length = 0x%zx", size_t(header.value_length()));
147 DBG(" Entry size = 0x%zx", size_t(header.entry_size()));
148 DBG(" Padded size = 0x%zx",
149 size_t(AlignUp(header.entry_size(), alignment_bytes)));
150
151 if (HeaderLooksLikeUnwrittenData(header)) {
152 return Status::NOT_FOUND;
153 }
154 key_descriptor.key_version = header.key_version();
155
156 // TODO: Handle multiple magics for formats that have changed.
157 if (header.magic() != entry_header_format_.magic) {
158 // TODO: It may be cleaner to have some logging helpers for these cases.
159 CRT("Found corrupt magic: %zx; expecting %zx; at address %zx",
160 size_t(header.magic()),
161 size_t(entry_header_format_.magic),
162 size_t(entry_address));
163 return Status::DATA_LOSS;
164 }
165
166 // Read the key from flash & validate the entry (which reads the value).
167 KeyBuffer key_buffer;
168 TRY(ReadEntryKey(key_descriptor, header.key_length(), key_buffer.data()));
169 TRY(ValidateEntryChecksum(
170 header, string_view(key_buffer.data()), key_descriptor));
171 key_descriptor.key_hash = HashKey(string_view(key_buffer.data()));
172
173 DBG("Key hash: %zx (%zu)",
174 size_t(key_descriptor.key_hash),
175 size_t(key_descriptor.key_hash));
176
177 TRY(AppendNewOrOverwriteStaleExistingDescriptor(key_descriptor));
178
179 // TODO: Extract this to something like "NextValidEntryAddress".
180 *next_entry_address =
181 AlignUp(key_descriptor.address + header.entry_size(), alignment_bytes);
182
183 return Status::OK;
184}
185
186// TODO: This method is the trigger of the O(valid_entries * all_entries) time
187// complexity for reading. At some cost to memory, this could be optimized by
188// using a hash table instead of scanning, but in practice this should be fine
189// for a small number of keys
190Status KeyValueStore::AppendNewOrOverwriteStaleExistingDescriptor(
191 const KeyDescriptor& key_descriptor) {
192 // With the new key descriptor, either add it to the descriptor table or
193 // overwrite an existing entry with an older version of the key.
194 KeyDescriptor* existing_descriptor = FindDescriptor(key_descriptor.key_hash);
195 if (existing_descriptor) {
196 if (existing_descriptor->key_version < key_descriptor.key_version) {
197 // Existing entry is old; replace the existing entry with the new one.
198 *existing_descriptor = key_descriptor;
199 } else {
200 // Otherwise, check for data integrity and leave the existing entry.
201 if (existing_descriptor->key_version == key_descriptor.key_version) {
202 ERR("Data loss: Duplicated old(=%zu) and new(=%zu) version",
203 size_t(existing_descriptor->key_version),
204 size_t(key_descriptor.key_version));
205 return Status::DATA_LOSS;
206 }
207 DBG("Found stale entry when appending; ignoring");
208 }
209 return Status::OK;
210 }
211 // Write new entry.
212 KeyDescriptor* newly_allocated_key_descriptor;
213 TRY(AppendEmptyDescriptor(&newly_allocated_key_descriptor));
214 *newly_allocated_key_descriptor = key_descriptor;
215 return Status::OK;
216}
217
218// TODO: Need a better name.
219Status KeyValueStore::AppendEmptyDescriptor(KeyDescriptor** new_descriptor) {
220 if (KeyListFull()) {
221 // TODO: Is this the right return code?
222 return Status::RESOURCE_EXHAUSTED;
223 }
224 *new_descriptor = &key_descriptor_list_[key_descriptor_list_size_++];
225 return Status::OK;
226}
227
228// TODO: Finish.
229bool KeyValueStore::HeaderLooksLikeUnwrittenData(
230 const EntryHeader& header) const {
231 // TODO: This is not correct; it should call through to flash memory.
232 return header.magic() == 0xffffffff;
233}
234
235KeyValueStore::KeyDescriptor* KeyValueStore::FindDescriptor(uint32_t hash) {
236 for (size_t key_id = 0; key_id < key_descriptor_list_size_; key_id++) {
237 if (key_descriptor_list_[key_id].key_hash == hash) {
238 return &(key_descriptor_list_[key_id]);
239 }
240 }
241 return nullptr;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800242}
243
244StatusWithSize KeyValueStore::Get(string_view key,
245 span<byte> value_buffer) const {
246 TRY(InvalidOperation(key));
247
David Rogers2761aeb2020-01-31 17:09:00 -0800248 const KeyDescriptor* key_descriptor;
249 TRY(FindKeyDescriptor(key, &key_descriptor));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800250
251 EntryHeader header;
David Rogers2761aeb2020-01-31 17:09:00 -0800252 TRY(ReadEntryHeader(*key_descriptor, &header));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800253
Keir Mierle8c352dc2020-02-02 13:58:19 -0800254 StatusWithSize result = ReadEntryValue(*key_descriptor, header, value_buffer);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800255 if (result.ok() && options_.verify_on_read) {
256 return ValidateEntryChecksum(header, key, value_buffer);
257 }
258 return result;
259}
260
261Status KeyValueStore::Put(string_view key, span<const byte> value) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800262 DBG("Writing key/value; key length=%zu, value length=%zu",
263 key.size(),
264 value.size());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800265 TRY(InvalidOperation(key));
266
267 if (value.size() > (1 << 24)) {
268 // TODO: Reject sizes that are larger than the maximum?
269 }
270
David Rogers2761aeb2020-01-31 17:09:00 -0800271 KeyDescriptor* key_descriptor;
272 if (FindKeyDescriptor(key, &key_descriptor).ok()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800273 DBG("Writing over existing entry");
David Rogers2761aeb2020-01-31 17:09:00 -0800274 return WriteEntryForExistingKey(key_descriptor, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800275 }
David Rogers2761aeb2020-01-31 17:09:00 -0800276
Keir Mierle8c352dc2020-02-02 13:58:19 -0800277 DBG("Writing new entry");
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800278 return WriteEntryForNewKey(key, value);
279}
280
281Status KeyValueStore::Delete(string_view key) {
282 TRY(InvalidOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800283 return Status::UNIMPLEMENTED;
284}
285
286const KeyValueStore::Entry& KeyValueStore::Iterator::operator*() {
David Rogers2761aeb2020-01-31 17:09:00 -0800287 const KeyDescriptor& key_descriptor =
288 entry_.kvs_.key_descriptor_list_[index_];
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800289
290 EntryHeader header;
David Rogers2761aeb2020-01-31 17:09:00 -0800291 if (entry_.kvs_.ReadEntryHeader(key_descriptor, &header).ok()) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800292 entry_.kvs_.ReadEntryKey(
David Rogers2761aeb2020-01-31 17:09:00 -0800293 key_descriptor, header.key_length(), entry_.key_buffer_.data());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800294 }
295
296 return entry_;
297}
298
Keir Mierle8c352dc2020-02-02 13:58:19 -0800299Status KeyValueStore::ValidateEntryChecksum(const EntryHeader& header,
300 string_view key,
301 const KeyDescriptor& entry) const {
302 // TODO: Remove this block once the checksums are implemented in test.
303 if (entry_header_format_.checksum == nullptr) {
304 const auto header_checksum = header.checksum();
305 const auto fixed_checksum = as_bytes(span(&kNoChecksum, 1));
306 if (std::memcmp(header_checksum.data(),
307 fixed_checksum.data(),
308 header_checksum.size()) == 0) {
309 return Status::OK;
310 }
311 return Status::DATA_LOSS;
312
313 // TODO: It's unclear why the below version doesn't work.
314 // return header.checksum() == as_bytes(span(&kNoChecksum, 1));
315 }
316 (void)(header);
317 (void)(key);
318 (void)(entry);
319 // TODO: Do incremental checksum verification by reading the value from
320 // flash a few blocks at a time.
321 return Status::OK;
322}
323
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800324Status KeyValueStore::InvalidOperation(string_view key) const {
Wyatt Hepleracaacf92020-01-24 10:58:30 -0800325 if (InvalidKey(key)) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800326 return Status::INVALID_ARGUMENT;
327 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800328 if (!enabled_) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800329 return Status::FAILED_PRECONDITION;
330 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800331 return Status::OK;
332}
333
David Rogers2761aeb2020-01-31 17:09:00 -0800334Status KeyValueStore::FindKeyDescriptor(string_view key,
335 const KeyDescriptor** result) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800336 char key_buffer[kMaxKeyLength];
337 const uint32_t hash = HashKey(key);
Wyatt Heplerb7609542020-01-24 10:29:54 -0800338
David Rogers2761aeb2020-01-31 17:09:00 -0800339 for (auto& descriptor : key_descriptors()) {
340 if (descriptor.key_hash == hash) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800341 DBG("Found match! For hash: %zx", size_t(hash));
David Rogers2761aeb2020-01-31 17:09:00 -0800342 TRY(ReadEntryKey(descriptor, key.size(), key_buffer));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800343
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800344 if (key == string_view(key_buffer, key.size())) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800345 DBG("Keys matched too");
David Rogers2761aeb2020-01-31 17:09:00 -0800346 *result = &descriptor;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800347 return Status::OK;
348 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800349 }
350 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800351 return Status::NOT_FOUND;
352}
353
David Rogers2761aeb2020-01-31 17:09:00 -0800354Status KeyValueStore::ReadEntryHeader(const KeyDescriptor& descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800355 EntryHeader* header) const {
David Rogers2761aeb2020-01-31 17:09:00 -0800356 return partition_.Read(descriptor.address, sizeof(*header), header).status();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800357}
358
David Rogers2761aeb2020-01-31 17:09:00 -0800359Status KeyValueStore::ReadEntryKey(const KeyDescriptor& descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800360 size_t key_length,
361 char* key) const {
362 // TODO: This check probably shouldn't be here; this is like
363 // checking that the Cortex M's RAM isn't corrupt. This should be
364 // done at boot time.
365 // ^^ This argument sometimes comes from EntryHeader::key_value_len,
366 // which is read directly from flash. If it's corrupted, we shouldn't try
367 // to read a bunch of extra data.
368 if (key_length == 0u || key_length > kMaxKeyLength) {
369 return Status::DATA_LOSS;
370 }
371 // The key is immediately after the entry header.
David Rogers2761aeb2020-01-31 17:09:00 -0800372 return partition_
373 .Read(descriptor.address + sizeof(EntryHeader), key_length, key)
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800374 .status();
375}
376
David Rogers2761aeb2020-01-31 17:09:00 -0800377StatusWithSize KeyValueStore::ReadEntryValue(
378 const KeyDescriptor& key_descriptor,
379 const EntryHeader& header,
380 span<byte> value) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800381 const size_t read_size = std::min(header.value_length(), value.size());
David Rogers2761aeb2020-01-31 17:09:00 -0800382 StatusWithSize result = partition_.Read(
383 key_descriptor.address + sizeof(header) + header.key_length(),
Keir Mierle8c352dc2020-02-02 13:58:19 -0800384 value.subspan(0, read_size));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800385 TRY(result);
386 if (read_size != header.value_length()) {
387 return StatusWithSize(Status::RESOURCE_EXHAUSTED, read_size);
388 }
389 return StatusWithSize(read_size);
390}
391
392Status KeyValueStore::ValidateEntryChecksum(const EntryHeader& header,
393 string_view key,
394 span<const byte> value) const {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800395 // TODO: Re-enable this when we have checksum implementations.
396 return Status::OK;
397
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800398 CalculateEntryChecksum(header, key, value);
399 return entry_header_format_.checksum->Verify(header.checksum());
400}
401
David Rogers2761aeb2020-01-31 17:09:00 -0800402Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800403 string_view key,
404 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800405 SectorDescriptor* sector;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800406 TRY(FindOrRecoverSectorWithSpace(&sector, EntrySize(key, value)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800407 DBG("Writing existing entry; found sector: %d",
408 static_cast<int>(sector - sector_map_.data()));
David Rogers2761aeb2020-01-31 17:09:00 -0800409 return AppendEntry(sector, key_descriptor, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800410}
411
412Status KeyValueStore::WriteEntryForNewKey(string_view key,
413 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800414 if (KeyListFull()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800415 WRN("KVS full: trying to store a new entry, but can't. Have %zu entries",
416 key_descriptor_list_size_);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800417 return Status::RESOURCE_EXHAUSTED;
418 }
419
David Rogers2761aeb2020-01-31 17:09:00 -0800420 // Modify the key descriptor at the end of the array, without bumping the map
421 // size so the key descriptor is prepared and written without committing
422 // first.
423 KeyDescriptor& key_descriptor =
424 key_descriptor_list_[key_descriptor_list_size_];
425 key_descriptor.key_hash = HashKey(key);
426 key_descriptor.key_version = 0; // will be incremented by AppendEntry()
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800427
David Rogers2761aeb2020-01-31 17:09:00 -0800428 SectorDescriptor* sector;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800429 TRY(FindOrRecoverSectorWithSpace(&sector, EntrySize(key, value)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800430 DBG("Writing new entry; found sector: %d",
431 static_cast<int>(sector - sector_map_.data()));
David Rogers2761aeb2020-01-31 17:09:00 -0800432 TRY(AppendEntry(sector, &key_descriptor, key, value));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800433
Keir Mierle8c352dc2020-02-02 13:58:19 -0800434 // Only increment bump our size when we are certain the write succeeded.
David Rogers2761aeb2020-01-31 17:09:00 -0800435 key_descriptor_list_size_ += 1;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800436 return Status::OK;
437}
438
David Rogers2761aeb2020-01-31 17:09:00 -0800439Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800440 // TODO: Implement this function!
David Rogers2761aeb2020-01-31 17:09:00 -0800441 (void)key_descriptor;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800442 return Status::OK;
David Rogersa12786b2020-01-31 16:02:33 -0800443 return Status::UNIMPLEMENTED;
444}
445
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800446// Find either an existing sector with enough space, or an empty sector.
447// Maintains the invariant that there is always at least 1 empty sector.
David Rogers2761aeb2020-01-31 17:09:00 -0800448KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorWithSpace(
449 size_t size) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800450 const size_t sector_count = partition_.sector_count();
451
452 // TODO: Ignore last written sector for now and scan from the beginning.
453 last_written_sector_ = sector_count - 1;
454
455 size_t start = (last_written_sector_ + 1) % sector_count;
David Rogers2761aeb2020-01-31 17:09:00 -0800456 SectorDescriptor* first_empty_sector = nullptr;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800457 bool at_least_two_empty_sectors = false;
458
459 for (size_t i = start; i != last_written_sector_;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800460 i = (i + 1) % sector_count) {
461 DBG("Examining sector %zu", i);
David Rogers2761aeb2020-01-31 17:09:00 -0800462 SectorDescriptor& sector = sector_map_[i];
Keir Mierle8c352dc2020-02-02 13:58:19 -0800463
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800464 if (!SectorEmpty(sector) && sector.HasSpace(size)) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800465 DBG("Partially occupied sector with enough space; done!");
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800466 return &sector;
467 }
468
469 if (SectorEmpty(sector)) {
470 if (first_empty_sector == nullptr) {
471 first_empty_sector = &sector;
472 } else {
473 at_least_two_empty_sectors = true;
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800474 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800475 }
476 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800477
478 if (at_least_two_empty_sectors) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800479 DBG("Found a empty sector and a spare one; returning the first found (%d)",
480 static_cast<int>(first_empty_sector - sector_map_.data()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800481 return first_empty_sector;
482 }
483 return nullptr;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800484}
485
David Rogers2761aeb2020-01-31 17:09:00 -0800486Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800487 size_t size) {
488 *sector = FindSectorWithSpace(size);
489 if (*sector != nullptr) {
490 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800491 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800492 if (options_.partial_gc_on_write) {
493 return GarbageCollectOneSector(sector);
494 }
495 return Status::RESOURCE_EXHAUSTED;
496}
497
David Rogers2761aeb2020-01-31 17:09:00 -0800498KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() {
499 SectorDescriptor* sector_candidate = nullptr;
David Rogersa12786b2020-01-31 16:02:33 -0800500 size_t candidate_bytes = 0;
501
502 // Step 1: Try to find a sectors with stale keys and no valid keys (no
503 // relocation needed). If any such sectors are found, use the sector with the
504 // most reclaimable bytes.
505 for (auto& sector : sector_map_) {
506 if ((sector.valid_bytes == 0) &&
507 (RecoverableBytes(sector) > candidate_bytes)) {
508 sector_candidate = &sector;
509 candidate_bytes = RecoverableBytes(sector);
510 }
511 }
512
513 // Step 2: If step 1 yields no sectors, just find the sector with the most
514 // reclaimable bytes.
515 if (sector_candidate == nullptr) {
516 for (auto& sector : sector_map_) {
517 if (RecoverableBytes(sector) > candidate_bytes) {
518 sector_candidate = &sector;
519 candidate_bytes = RecoverableBytes(sector);
520 }
521 }
522 }
523
524 return sector_candidate;
525}
526
David Rogers2761aeb2020-01-31 17:09:00 -0800527Status KeyValueStore::GarbageCollectOneSector(SectorDescriptor** sector) {
David Rogersa12786b2020-01-31 16:02:33 -0800528 // Step 1: Find the sector to garbage collect
David Rogers2761aeb2020-01-31 17:09:00 -0800529 SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800530
David Rogersa12786b2020-01-31 16:02:33 -0800531 if (sector_to_gc == nullptr) {
532 return Status::RESOURCE_EXHAUSTED;
533 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800534
David Rogersa12786b2020-01-31 16:02:33 -0800535 // Step 2: Move any valid entries in the GC sector to other sectors
536 if (sector_to_gc->valid_bytes != 0) {
David Rogers2761aeb2020-01-31 17:09:00 -0800537 for (auto& descriptor : key_descriptors()) {
538 if (AddressInSector(*sector_to_gc, descriptor.address)) {
539 TRY(RelocateEntry(descriptor));
David Rogersa12786b2020-01-31 16:02:33 -0800540 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800541 }
542 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800543
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800544 if (sector_to_gc->valid_bytes != 0) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800545 return Status::INTERNAL;
546 }
547
David Rogersa12786b2020-01-31 16:02:33 -0800548 // Step 3: Reinitialize the sector
549 sector_to_gc->tail_free_bytes = 0;
550 TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1));
551 sector_to_gc->tail_free_bytes = partition_.sector_size_bytes();
Wyatt Heplerb7609542020-01-24 10:29:54 -0800552
David Rogersa12786b2020-01-31 16:02:33 -0800553 *sector = sector_to_gc;
554 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800555}
556
David Rogers2761aeb2020-01-31 17:09:00 -0800557Status KeyValueStore::AppendEntry(SectorDescriptor* sector,
558 KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800559 const string_view key,
560 span<const byte> value) {
561 // write header, key, and value
562 const EntryHeader header(entry_header_format_.magic,
563 CalculateEntryChecksum(header, key, value),
564 key.size(),
565 value.size(),
David Rogers2761aeb2020-01-31 17:09:00 -0800566 key_descriptor->key_version + 1);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800567 DBG("Appending entry with key version: %zx", size_t(header.key_version()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800568
569 // Handles writing multiple concatenated buffers, while breaking up the writes
570 // into alignment-sized blocks.
571 Address address = NextWritableAddress(sector);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800572 DBG("Appending to address: %zx", size_t(address));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800573 TRY_ASSIGN(
574 size_t written,
575 partition_.Write(
576 address, {as_bytes(span(&header, 1)), as_bytes(span(key)), value}));
577
578 if (options_.verify_on_write) {
David Rogers2761aeb2020-01-31 17:09:00 -0800579 TRY(VerifyEntry(sector, key_descriptor));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800580 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800581
582 // TODO: UPDATE last_written_sector_ appropriately
583
David Rogers2761aeb2020-01-31 17:09:00 -0800584 key_descriptor->address = address;
585 key_descriptor->key_version = header.key_version();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800586 sector->valid_bytes += written;
587 sector->tail_free_bytes -= written;
588 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800589}
590
David Rogers2761aeb2020-01-31 17:09:00 -0800591Status KeyValueStore::VerifyEntry(SectorDescriptor* sector,
592 KeyDescriptor* key_descriptor) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800593 if (entry_header_format_.checksum == nullptr) {
594 // TODO: Remove this once checksums are fully implemented.
595 return Status::OK;
596 }
597 return Status::UNIMPLEMENTED;
598
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800599 // TODO: Implement me!
600 (void)sector;
David Rogers2761aeb2020-01-31 17:09:00 -0800601 (void)key_descriptor;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800602 return Status::UNIMPLEMENTED;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800603}
604
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800605span<const byte> KeyValueStore::CalculateEntryChecksum(
606 const EntryHeader& header,
607 const string_view key,
608 span<const byte> value) const {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800609 if (entry_header_format_.checksum == nullptr) {
610 return as_bytes(span(&kNoChecksum, 1));
611 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800612 auto& checksum = *entry_header_format_.checksum;
613
614 checksum.Reset();
615 checksum.Update(header.DataForChecksum());
616 checksum.Update(as_bytes(span(key)));
617 checksum.Update(value.data(), value.size_bytes());
618 return checksum.state();
Wyatt Heplerb7609542020-01-24 10:29:54 -0800619}
620
Keir Mierle8c352dc2020-02-02 13:58:19 -0800621void KeyValueStore::LogDebugInfo() {
622 const size_t sector_count = partition_.sector_count();
623 const size_t sector_size_bytes = partition_.sector_size_bytes();
624 DBG("====================== KEY VALUE STORE DUMP =========================");
625 DBG(" ");
626 DBG("Flash partition:");
627 DBG(" Sector count = %zu", sector_count);
628 DBG(" Sector max count = %zu", kUsableSectors);
629 DBG(" Sector size = %zu", sector_size_bytes);
630 DBG(" Total size = %zu", partition_.size_bytes());
631 DBG(" Alignment = %zu", partition_.alignment_bytes());
632 DBG(" ");
633 DBG("Key descriptors:");
634 DBG(" Entry count = %zu", key_descriptor_list_size_);
635 DBG(" Max entry count = %zu", kMaxEntries);
636 DBG(" ");
637 DBG(" # hash version address address (hex)");
638 for (size_t i = 0; i < key_descriptor_list_size_; ++i) {
639 const KeyDescriptor& kd = key_descriptor_list_[i];
640 DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx",
641 i,
642 size_t(kd.key_hash),
643 size_t(kd.key_version),
644 size_t(kd.address),
645 size_t(kd.address));
646 }
647 DBG(" ");
648
649 DBG("Sector descriptors:");
650 DBG(" # tail free valid has_space");
651 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
652 const SectorDescriptor& sd = sector_map_[sector_id];
653 DBG(" |%3zu: | %8zu |%8zu | %s",
654 sector_id,
655 size_t(sd.tail_free_bytes),
656 size_t(sd.valid_bytes),
657 sd.tail_free_bytes ? "YES" : "");
658 }
659 DBG(" ");
660
661 // TODO: This should stop logging after some threshold.
662 // size_t dumped_bytes = 0;
663 DBG("Sector raw data:");
664 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
665 // Read sector data. Yes, this will blow the stack on embedded.
666 std::array<byte, 500> raw_sector_data; // TODO
667 StatusWithSize sws =
668 partition_.Read(sector_id * sector_size_bytes, raw_sector_data);
669 DBG("Read: %zu bytes", sws.size());
670
671 DBG(" base addr offs 0 1 2 3 4 5 6 7");
672 for (size_t i = 0; i < sector_size_bytes; i += 8) {
673 DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x",
674 sector_id,
675 (sector_id * sector_size_bytes) + i,
676 i,
677 static_cast<unsigned int>(raw_sector_data[i + 0]),
678 static_cast<unsigned int>(raw_sector_data[i + 1]),
679 static_cast<unsigned int>(raw_sector_data[i + 2]),
680 static_cast<unsigned int>(raw_sector_data[i + 3]),
681 static_cast<unsigned int>(raw_sector_data[i + 4]),
682 static_cast<unsigned int>(raw_sector_data[i + 5]),
683 static_cast<unsigned int>(raw_sector_data[i + 6]),
684 static_cast<unsigned int>(raw_sector_data[i + 7]));
685
686 // TODO: Fix exit condition.
687 if (i > 128) {
688 break;
689 }
690 }
691 DBG(" ");
692 }
693
694 DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////");
695}
696
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800697} // namespace pw::kvs