blob: 09747d6c554b82d2a076b6bac8d5cc5be242586a [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
Wyatt Heplerbab0e202020-02-04 07:40:08 -080017#include <algorithm>
Wyatt Heplerb7609542020-01-24 10:29:54 -080018#include <cstring>
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080019#include <type_traits>
Wyatt Heplerb7609542020-01-24 10:29:54 -080020
Keir Mierle8c352dc2020-02-02 13:58:19 -080021#define PW_LOG_USE_ULTRA_SHORT_NAMES 1
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080022#include "pw_kvs_private/format.h"
23#include "pw_kvs_private/macros.h"
Keir Mierle8c352dc2020-02-02 13:58:19 -080024#include "pw_log/log.h"
Wyatt Heplerb7609542020-01-24 10:29:54 -080025
Wyatt Hepler2ad60672020-01-21 08:00:16 -080026namespace pw::kvs {
Wyatt Heplerb7609542020-01-24 10:29:54 -080027
Wyatt Hepleracaacf92020-01-24 10:58:30 -080028using std::byte;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080029using std::string_view;
Wyatt Hepleracaacf92020-01-24 10:58:30 -080030
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080031namespace {
32
Wyatt Heplerbab0e202020-02-04 07:40:08 -080033using Address = FlashPartition::Address;
34
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080035// constexpr uint32_t SixFiveFiveNineNine(std::string_view string) {
36constexpr uint32_t HashKey(std::string_view string) {
37 uint32_t hash = 0;
38 uint32_t coefficient = 65599u;
39
40 for (char ch : string) {
41 hash += coefficient * unsigned(ch);
42 coefficient *= 65599u;
Wyatt Heplerb7609542020-01-24 10:29:54 -080043 }
44
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080045 return hash;
Wyatt Heplerb7609542020-01-24 10:29:54 -080046}
47
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080048} // namespace
49
50Status KeyValueStore::Init() {
Keir Mierle8c352dc2020-02-02 13:58:19 -080051 // Reset the number of occupied key descriptors; we will fill them later.
52 key_descriptor_list_size_ = 0;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -080053
Keir Mierle8c352dc2020-02-02 13:58:19 -080054 const size_t sector_size_bytes = partition_.sector_size_bytes();
55 const size_t sector_count = partition_.sector_count();
56
David Rogersf0a35442020-02-04 12:16:38 -080057 if (working_buffer_.size() < sector_size_bytes) {
58 CRT("ERROR: working_buffer_ (%zu bytes) is smaller than sector "
59 "size (%zu bytes)",
60 working_buffer_.size(),
61 sector_size_bytes);
62 return Status::INVALID_ARGUMENT;
63 }
64
Keir Mierle8c352dc2020-02-02 13:58:19 -080065 DBG("First pass: Read all entries from all sectors");
66 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
67 // Track writable bytes in this sector. Updated after reading each entry.
68 sector_map_[sector_id].tail_free_bytes = sector_size_bytes;
69
70 const Address sector_address = sector_id * sector_size_bytes;
71 Address entry_address = sector_address;
72
73 for (int num_entries_in_sector = 0;; num_entries_in_sector++) {
74 DBG("Load entry: sector=%zu, entry#=%d, address=%zu",
75 sector_id,
76 num_entries_in_sector,
77 size_t(entry_address));
78
79 if (!AddressInSector(sector_map_[sector_id], entry_address)) {
80 DBG("Fell off end of sector; moving to the next sector");
81 break;
82 }
83
84 Address next_entry_address;
85 Status status = LoadEntry(entry_address, &next_entry_address);
86 if (status == Status::NOT_FOUND) {
87 DBG("Hit un-written data in sector; moving to the next sector");
88 break;
89 }
90 if (status == Status::DATA_LOSS) {
91 // It's not clear KVS can make a unilateral decision about what to do
92 // in corruption cases. It's an application decision, for which we
93 // should offer some configurability. For now, entirely bail out of
94 // loading and give up.
95 //
96 // Later, scan for remaining valid keys; since it's entirely possible
97 // that there is a duplicate of the key elsewhere and everything is
98 // fine. Later, we can wipe and maybe recover the sector.
99 //
100 // TODO: Implement rest-of-sector scanning for valid entries.
101 return Status::DATA_LOSS;
102 }
103 TRY(status);
104
105 // Entry loaded successfully; so get ready to load the next one.
106 entry_address = next_entry_address;
107
108 // Update of the number of writable bytes in this sector.
109 sector_map_[sector_id].tail_free_bytes =
110 sector_size_bytes - (entry_address - sector_address);
111 }
112 }
113
114 DBG("Second pass: Count valid bytes in each sector");
115 // Initialize the sector sizes.
116 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
117 sector_map_[sector_id].valid_bytes = 0;
118 }
119 // For every valid key, increment the valid bytes for that sector.
120 for (size_t key_id = 0; key_id < key_descriptor_list_size_; ++key_id) {
121 uint32_t sector_id =
122 key_descriptor_list_[key_id].address / sector_size_bytes;
123 KeyDescriptor key_descriptor;
124 key_descriptor.address = key_descriptor_list_[key_id].address;
125 EntryHeader header;
126 TRY(ReadEntryHeader(key_descriptor, &header));
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800127 sector_map_[sector_id].valid_bytes += header.size();
Keir Mierle8c352dc2020-02-02 13:58:19 -0800128 }
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800129 initialized_ = true;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800130 return Status::OK;
131}
132
133Status KeyValueStore::LoadEntry(Address entry_address,
134 Address* next_entry_address) {
135 const size_t alignment_bytes = partition_.alignment_bytes();
136
137 KeyDescriptor key_descriptor;
138 key_descriptor.address = entry_address;
139
140 EntryHeader header;
141 TRY(ReadEntryHeader(key_descriptor, &header));
142 // TODO: Should likely add a "LogHeader" method or similar.
143 DBG("Header: ");
144 DBG(" Address = 0x%zx", size_t(entry_address));
145 DBG(" Magic = 0x%zx", size_t(header.magic()));
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800146 DBG(" Checksum = 0x%zx", size_t(header.checksum()));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800147 DBG(" Key length = 0x%zx", size_t(header.key_length()));
148 DBG(" Value length = 0x%zx", size_t(header.value_length()));
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800149 DBG(" Entry size = 0x%zx", size_t(header.size()));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800150 DBG(" Padded size = 0x%zx",
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800151 size_t(AlignUp(header.size(), alignment_bytes)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800152
153 if (HeaderLooksLikeUnwrittenData(header)) {
154 return Status::NOT_FOUND;
155 }
156 key_descriptor.key_version = header.key_version();
157
158 // TODO: Handle multiple magics for formats that have changed.
159 if (header.magic() != entry_header_format_.magic) {
160 // TODO: It may be cleaner to have some logging helpers for these cases.
161 CRT("Found corrupt magic: %zx; expecting %zx; at address %zx",
162 size_t(header.magic()),
163 size_t(entry_header_format_.magic),
164 size_t(entry_address));
165 return Status::DATA_LOSS;
166 }
167
168 // Read the key from flash & validate the entry (which reads the value).
169 KeyBuffer key_buffer;
170 TRY(ReadEntryKey(key_descriptor, header.key_length(), key_buffer.data()));
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800171 const string_view key(key_buffer.data(), header.key_length());
172
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800173 TRY(header.VerifyChecksumInFlash(
Wyatt Hepler0a223582020-02-04 17:47:40 -0800174 &partition_, key_descriptor.address, entry_header_format_.checksum));
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800175 key_descriptor.key_hash = HashKey(key);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800176
177 DBG("Key hash: %zx (%zu)",
178 size_t(key_descriptor.key_hash),
179 size_t(key_descriptor.key_hash));
180
181 TRY(AppendNewOrOverwriteStaleExistingDescriptor(key_descriptor));
182
183 // TODO: Extract this to something like "NextValidEntryAddress".
184 *next_entry_address =
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800185 AlignUp(key_descriptor.address + header.size(), alignment_bytes);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800186
187 return Status::OK;
188}
189
190// TODO: This method is the trigger of the O(valid_entries * all_entries) time
191// complexity for reading. At some cost to memory, this could be optimized by
192// using a hash table instead of scanning, but in practice this should be fine
193// for a small number of keys
194Status KeyValueStore::AppendNewOrOverwriteStaleExistingDescriptor(
195 const KeyDescriptor& key_descriptor) {
196 // With the new key descriptor, either add it to the descriptor table or
197 // overwrite an existing entry with an older version of the key.
198 KeyDescriptor* existing_descriptor = FindDescriptor(key_descriptor.key_hash);
199 if (existing_descriptor) {
200 if (existing_descriptor->key_version < key_descriptor.key_version) {
201 // Existing entry is old; replace the existing entry with the new one.
202 *existing_descriptor = key_descriptor;
203 } else {
204 // Otherwise, check for data integrity and leave the existing entry.
205 if (existing_descriptor->key_version == key_descriptor.key_version) {
206 ERR("Data loss: Duplicated old(=%zu) and new(=%zu) version",
207 size_t(existing_descriptor->key_version),
208 size_t(key_descriptor.key_version));
209 return Status::DATA_LOSS;
210 }
211 DBG("Found stale entry when appending; ignoring");
212 }
213 return Status::OK;
214 }
215 // Write new entry.
216 KeyDescriptor* newly_allocated_key_descriptor;
217 TRY(AppendEmptyDescriptor(&newly_allocated_key_descriptor));
218 *newly_allocated_key_descriptor = key_descriptor;
219 return Status::OK;
220}
221
222// TODO: Need a better name.
223Status KeyValueStore::AppendEmptyDescriptor(KeyDescriptor** new_descriptor) {
224 if (KeyListFull()) {
225 // TODO: Is this the right return code?
226 return Status::RESOURCE_EXHAUSTED;
227 }
228 *new_descriptor = &key_descriptor_list_[key_descriptor_list_size_++];
229 return Status::OK;
230}
231
232// TODO: Finish.
233bool KeyValueStore::HeaderLooksLikeUnwrittenData(
234 const EntryHeader& header) const {
235 // TODO: This is not correct; it should call through to flash memory.
236 return header.magic() == 0xffffffff;
237}
238
239KeyValueStore::KeyDescriptor* KeyValueStore::FindDescriptor(uint32_t hash) {
240 for (size_t key_id = 0; key_id < key_descriptor_list_size_; key_id++) {
241 if (key_descriptor_list_[key_id].key_hash == hash) {
242 return &(key_descriptor_list_[key_id]);
243 }
244 }
245 return nullptr;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800246}
247
248StatusWithSize KeyValueStore::Get(string_view key,
249 span<byte> value_buffer) const {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800250 TRY(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800251
David Rogers2761aeb2020-01-31 17:09:00 -0800252 const KeyDescriptor* key_descriptor;
253 TRY(FindKeyDescriptor(key, &key_descriptor));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800254
255 EntryHeader header;
David Rogers2761aeb2020-01-31 17:09:00 -0800256 TRY(ReadEntryHeader(*key_descriptor, &header));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800257
Keir Mierle8c352dc2020-02-02 13:58:19 -0800258 StatusWithSize result = ReadEntryValue(*key_descriptor, header, value_buffer);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800259 if (result.ok() && options_.verify_on_read) {
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800260 return header.VerifyChecksum(entry_header_format_.checksum,
261 key,
262 value_buffer.subspan(0, result.size()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800263 }
264 return result;
265}
266
267Status KeyValueStore::Put(string_view key, span<const byte> value) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800268 DBG("Writing key/value; key length=%zu, value length=%zu",
269 key.size(),
270 value.size());
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800271
272 TRY(CheckOperation(key));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800273
274 if (value.size() > (1 << 24)) {
275 // TODO: Reject sizes that are larger than the maximum?
276 }
277
David Rogers2761aeb2020-01-31 17:09:00 -0800278 KeyDescriptor* key_descriptor;
279 if (FindKeyDescriptor(key, &key_descriptor).ok()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800280 DBG("Writing over existing entry");
David Rogers2761aeb2020-01-31 17:09:00 -0800281 return WriteEntryForExistingKey(key_descriptor, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800282 }
David Rogers2761aeb2020-01-31 17:09:00 -0800283
Keir Mierle8c352dc2020-02-02 13:58:19 -0800284 DBG("Writing new entry");
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800285 return WriteEntryForNewKey(key, value);
286}
287
288Status KeyValueStore::Delete(string_view key) {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800289 TRY(CheckOperation(key));
290
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800291 return Status::UNIMPLEMENTED;
292}
293
Wyatt Heplerce0da522020-02-04 16:56:44 -0800294const KeyValueStore::Item& KeyValueStore::Iterator::operator*() {
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800295 const KeyDescriptor& descriptor = entry_.kvs_.key_descriptor_list_[index_];
296
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800297 std::memset(entry_.key_buffer_.data(), 0, entry_.key_buffer_.size());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800298
299 EntryHeader header;
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800300 if (entry_.kvs_.ReadEntryHeader(descriptor, &header).ok()) {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800301 entry_.kvs_.ReadEntryKey(
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800302 descriptor, header.key_length(), entry_.key_buffer_.data());
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800303 }
304
305 return entry_;
306}
307
Wyatt Heplered163b02020-02-03 17:49:32 -0800308StatusWithSize KeyValueStore::ValueSize(std::string_view key) const {
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800309 TRY(CheckOperation(key));
Wyatt Heplered163b02020-02-03 17:49:32 -0800310
311 const KeyDescriptor* key_descriptor;
312 TRY(FindKeyDescriptor(key, &key_descriptor));
313
314 EntryHeader header;
315 TRY(ReadEntryHeader(*key_descriptor, &header));
316
317 return StatusWithSize(header.value_length());
318}
319
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800320Status KeyValueStore::FixedSizeGet(std::string_view key,
321 byte* value,
322 size_t size_bytes) const {
323 // Ensure that the size of the stored value matches the size of the type.
324 // Otherwise, report error. This check avoids potential memory corruption.
325 StatusWithSize result = ValueSize(key);
326 if (!result.ok()) {
327 return result.status();
Keir Mierle8c352dc2020-02-02 13:58:19 -0800328 }
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800329 if (result.size() != size_bytes) {
330 return Status::INVALID_ARGUMENT;
Wyatt Heplerbab0e202020-02-04 07:40:08 -0800331 }
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800332 return Get(key, span(value, size_bytes)).status();
Keir Mierle8c352dc2020-02-02 13:58:19 -0800333}
334
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800335Status KeyValueStore::CheckOperation(string_view key) const {
Wyatt Hepleracaacf92020-01-24 10:58:30 -0800336 if (InvalidKey(key)) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800337 return Status::INVALID_ARGUMENT;
338 }
Wyatt Hepler729f28c2020-02-05 09:46:00 -0800339 if (!initialized_) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800340 return Status::FAILED_PRECONDITION;
341 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800342 return Status::OK;
343}
344
David Rogers2761aeb2020-01-31 17:09:00 -0800345Status KeyValueStore::FindKeyDescriptor(string_view key,
346 const KeyDescriptor** result) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800347 char key_buffer[kMaxKeyLength];
348 const uint32_t hash = HashKey(key);
Wyatt Heplerb7609542020-01-24 10:29:54 -0800349
David Rogers2761aeb2020-01-31 17:09:00 -0800350 for (auto& descriptor : key_descriptors()) {
351 if (descriptor.key_hash == hash) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800352 DBG("Found match! For hash: %zx", size_t(hash));
David Rogers2761aeb2020-01-31 17:09:00 -0800353 TRY(ReadEntryKey(descriptor, key.size(), key_buffer));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800354
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800355 if (key == string_view(key_buffer, key.size())) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800356 DBG("Keys matched too");
David Rogers2761aeb2020-01-31 17:09:00 -0800357 *result = &descriptor;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800358 return Status::OK;
359 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800360 }
361 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800362 return Status::NOT_FOUND;
363}
364
David Rogers2761aeb2020-01-31 17:09:00 -0800365Status KeyValueStore::ReadEntryHeader(const KeyDescriptor& descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800366 EntryHeader* header) const {
David Rogers2761aeb2020-01-31 17:09:00 -0800367 return partition_.Read(descriptor.address, sizeof(*header), header).status();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800368}
369
David Rogers2761aeb2020-01-31 17:09:00 -0800370Status KeyValueStore::ReadEntryKey(const KeyDescriptor& descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800371 size_t key_length,
372 char* key) const {
373 // TODO: This check probably shouldn't be here; this is like
374 // checking that the Cortex M's RAM isn't corrupt. This should be
375 // done at boot time.
376 // ^^ This argument sometimes comes from EntryHeader::key_value_len,
377 // which is read directly from flash. If it's corrupted, we shouldn't try
378 // to read a bunch of extra data.
379 if (key_length == 0u || key_length > kMaxKeyLength) {
380 return Status::DATA_LOSS;
381 }
382 // The key is immediately after the entry header.
David Rogers2761aeb2020-01-31 17:09:00 -0800383 return partition_
384 .Read(descriptor.address + sizeof(EntryHeader), key_length, key)
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800385 .status();
386}
387
David Rogers2761aeb2020-01-31 17:09:00 -0800388StatusWithSize KeyValueStore::ReadEntryValue(
389 const KeyDescriptor& key_descriptor,
390 const EntryHeader& header,
391 span<byte> value) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800392 const size_t read_size = std::min(header.value_length(), value.size());
David Rogers2761aeb2020-01-31 17:09:00 -0800393 StatusWithSize result = partition_.Read(
394 key_descriptor.address + sizeof(header) + header.key_length(),
Keir Mierle8c352dc2020-02-02 13:58:19 -0800395 value.subspan(0, read_size));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800396 TRY(result);
397 if (read_size != header.value_length()) {
398 return StatusWithSize(Status::RESOURCE_EXHAUSTED, read_size);
399 }
400 return StatusWithSize(read_size);
401}
402
David Rogers2761aeb2020-01-31 17:09:00 -0800403Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800404 string_view key,
405 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800406 SectorDescriptor* sector;
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800407 TRY(FindOrRecoverSectorWithSpace(&sector, EntryHeader::size(key, value)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800408 DBG("Writing existing entry; found sector: %d",
409 static_cast<int>(sector - sector_map_.data()));
David Rogers2761aeb2020-01-31 17:09:00 -0800410 return AppendEntry(sector, key_descriptor, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800411}
412
413Status KeyValueStore::WriteEntryForNewKey(string_view key,
414 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800415 if (KeyListFull()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800416 WRN("KVS full: trying to store a new entry, but can't. Have %zu entries",
417 key_descriptor_list_size_);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800418 return Status::RESOURCE_EXHAUSTED;
419 }
420
David Rogers2761aeb2020-01-31 17:09:00 -0800421 // Modify the key descriptor at the end of the array, without bumping the map
422 // size so the key descriptor is prepared and written without committing
423 // first.
424 KeyDescriptor& key_descriptor =
425 key_descriptor_list_[key_descriptor_list_size_];
426 key_descriptor.key_hash = HashKey(key);
427 key_descriptor.key_version = 0; // will be incremented by AppendEntry()
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800428
David Rogers2761aeb2020-01-31 17:09:00 -0800429 SectorDescriptor* sector;
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800430 TRY(FindOrRecoverSectorWithSpace(&sector, EntryHeader::size(key, value)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800431 DBG("Writing new entry; found sector: %d",
432 static_cast<int>(sector - sector_map_.data()));
David Rogers2761aeb2020-01-31 17:09:00 -0800433 TRY(AppendEntry(sector, &key_descriptor, key, value));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800434
Keir Mierle8c352dc2020-02-02 13:58:19 -0800435 // Only increment bump our size when we are certain the write succeeded.
David Rogers2761aeb2020-01-31 17:09:00 -0800436 key_descriptor_list_size_ += 1;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800437 return Status::OK;
438}
439
David Rogers2761aeb2020-01-31 17:09:00 -0800440Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor) {
David Rogersf0a35442020-02-04 12:16:38 -0800441 struct TempEntry {
442 std::array<char, kMaxKeyLength + 1> key;
443 std::array<char, sizeof(working_buffer_) - sizeof(key)> value;
444 };
445 TempEntry* entry = reinterpret_cast<TempEntry*>(working_buffer_.data());
446
447 // Read the entry to be relocated. Store the header in a local variable and
448 // store the key and value in the TempEntry stored in the static allocated
449 // working_buffer_.
450 EntryHeader header;
451 TRY(ReadEntryHeader(key_descriptor, &header));
452 TRY(ReadEntryKey(key_descriptor, header.key_length(), entry->key.data()));
453 string_view key = string_view(entry->key.data(), header.key_length());
454 StatusWithSize result = ReadEntryValue(
455 key_descriptor, header, as_writable_bytes(span(entry->value)));
456 if (!result.status().ok()) {
457 return Status::INTERNAL;
458 }
459
460 auto value = span(entry->value.data(), result.size());
461
462 TRY(header.VerifyChecksum(
463 entry_header_format_.checksum, key, as_bytes(value)));
464
465 SectorDescriptor* old_sector = SectorFromAddress(key_descriptor.address);
466 if (old_sector == nullptr) {
467 return Status::INTERNAL;
468 }
469
470 // Find a new sector for the entry and write it to the new location.
471 SectorDescriptor* new_sector =
Wyatt Hepler93b889d2020-02-05 09:01:18 -0800472 FindSectorWithSpace(header.size(), old_sector, true);
David Rogersf0a35442020-02-04 12:16:38 -0800473 if (new_sector == nullptr) {
474 return Status::RESOURCE_EXHAUSTED;
475 }
476 return AppendEntry(new_sector, &key_descriptor, key, as_bytes(value));
David Rogersa12786b2020-01-31 16:02:33 -0800477}
478
David Rogers8db5a722020-02-03 18:28:34 -0800479// Find either an existing sector with enough space that is not the sector to
480// skip, or an empty sector. Maintains the invariant that there is always at
481// least 1 empty sector unless set to bypass the rule.
David Rogers2761aeb2020-01-31 17:09:00 -0800482KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorWithSpace(
David Rogers8db5a722020-02-03 18:28:34 -0800483 size_t size,
484 SectorDescriptor* sector_to_skip,
485 bool bypass_empty_sector_rule) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800486 const size_t sector_count = partition_.sector_count();
487
488 // TODO: Ignore last written sector for now and scan from the beginning.
489 last_written_sector_ = sector_count - 1;
490
491 size_t start = (last_written_sector_ + 1) % sector_count;
David Rogers2761aeb2020-01-31 17:09:00 -0800492 SectorDescriptor* first_empty_sector = nullptr;
David Rogers8db5a722020-02-03 18:28:34 -0800493 bool at_least_two_empty_sectors = bypass_empty_sector_rule;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800494
495 for (size_t i = start; i != last_written_sector_;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800496 i = (i + 1) % sector_count) {
497 DBG("Examining sector %zu", i);
David Rogers2761aeb2020-01-31 17:09:00 -0800498 SectorDescriptor& sector = sector_map_[i];
Keir Mierle8c352dc2020-02-02 13:58:19 -0800499
David Rogers8db5a722020-02-03 18:28:34 -0800500 if (sector_to_skip == &sector) {
501 DBG("Skipping the skip sector");
502 continue;
503 }
504
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800505 if (!SectorEmpty(sector) && sector.HasSpace(size)) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800506 DBG("Partially occupied sector with enough space; done!");
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800507 return &sector;
508 }
509
510 if (SectorEmpty(sector)) {
511 if (first_empty_sector == nullptr) {
512 first_empty_sector = &sector;
513 } else {
514 at_least_two_empty_sectors = true;
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800515 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800516 }
517 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800518
519 if (at_least_two_empty_sectors) {
David Rogersf0a35442020-02-04 12:16:38 -0800520 DBG("Found a usable empty sector; returning the first found (%d)",
Keir Mierle8c352dc2020-02-02 13:58:19 -0800521 static_cast<int>(first_empty_sector - sector_map_.data()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800522 return first_empty_sector;
523 }
524 return nullptr;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800525}
526
David Rogers2761aeb2020-01-31 17:09:00 -0800527Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800528 size_t size) {
529 *sector = FindSectorWithSpace(size);
530 if (*sector != nullptr) {
531 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800532 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800533 if (options_.partial_gc_on_write) {
534 return GarbageCollectOneSector(sector);
535 }
536 return Status::RESOURCE_EXHAUSTED;
537}
538
David Rogers2761aeb2020-01-31 17:09:00 -0800539KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() {
540 SectorDescriptor* sector_candidate = nullptr;
David Rogersa12786b2020-01-31 16:02:33 -0800541 size_t candidate_bytes = 0;
542
543 // Step 1: Try to find a sectors with stale keys and no valid keys (no
544 // relocation needed). If any such sectors are found, use the sector with the
545 // most reclaimable bytes.
546 for (auto& sector : sector_map_) {
547 if ((sector.valid_bytes == 0) &&
548 (RecoverableBytes(sector) > candidate_bytes)) {
549 sector_candidate = &sector;
550 candidate_bytes = RecoverableBytes(sector);
551 }
552 }
553
554 // Step 2: If step 1 yields no sectors, just find the sector with the most
555 // reclaimable bytes.
556 if (sector_candidate == nullptr) {
557 for (auto& sector : sector_map_) {
558 if (RecoverableBytes(sector) > candidate_bytes) {
559 sector_candidate = &sector;
560 candidate_bytes = RecoverableBytes(sector);
561 }
562 }
563 }
564
565 return sector_candidate;
566}
567
David Rogers2761aeb2020-01-31 17:09:00 -0800568Status KeyValueStore::GarbageCollectOneSector(SectorDescriptor** sector) {
David Rogersa12786b2020-01-31 16:02:33 -0800569 // Step 1: Find the sector to garbage collect
David Rogers2761aeb2020-01-31 17:09:00 -0800570 SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800571
David Rogersa12786b2020-01-31 16:02:33 -0800572 if (sector_to_gc == nullptr) {
573 return Status::RESOURCE_EXHAUSTED;
574 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800575
David Rogersa12786b2020-01-31 16:02:33 -0800576 // Step 2: Move any valid entries in the GC sector to other sectors
577 if (sector_to_gc->valid_bytes != 0) {
David Rogers2761aeb2020-01-31 17:09:00 -0800578 for (auto& descriptor : key_descriptors()) {
579 if (AddressInSector(*sector_to_gc, descriptor.address)) {
580 TRY(RelocateEntry(descriptor));
David Rogersa12786b2020-01-31 16:02:33 -0800581 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800582 }
583 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800584
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800585 if (sector_to_gc->valid_bytes != 0) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800586 return Status::INTERNAL;
587 }
588
David Rogersa12786b2020-01-31 16:02:33 -0800589 // Step 3: Reinitialize the sector
590 sector_to_gc->tail_free_bytes = 0;
591 TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1));
592 sector_to_gc->tail_free_bytes = partition_.sector_size_bytes();
Wyatt Heplerb7609542020-01-24 10:29:54 -0800593
David Rogersa12786b2020-01-31 16:02:33 -0800594 *sector = sector_to_gc;
595 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800596}
597
David Rogers2761aeb2020-01-31 17:09:00 -0800598Status KeyValueStore::AppendEntry(SectorDescriptor* sector,
599 KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800600 const string_view key,
601 span<const byte> value) {
602 // write header, key, and value
603 const EntryHeader header(entry_header_format_.magic,
Wyatt Hepler6e3a83b2020-02-04 07:36:45 -0800604 entry_header_format_.checksum,
605 key,
606 value,
David Rogers2761aeb2020-01-31 17:09:00 -0800607 key_descriptor->key_version + 1);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800608 DBG("Appending entry with key version: %zx", size_t(header.key_version()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800609
610 // Handles writing multiple concatenated buffers, while breaking up the writes
611 // into alignment-sized blocks.
612 Address address = NextWritableAddress(sector);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800613 DBG("Appending to address: %zx", size_t(address));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800614 TRY_ASSIGN(
615 size_t written,
616 partition_.Write(
617 address, {as_bytes(span(&header, 1)), as_bytes(span(key)), value}));
618
619 if (options_.verify_on_write) {
Wyatt Hepler0a223582020-02-04 17:47:40 -0800620 TRY(header.VerifyChecksumInFlash(
621 &partition_, address, entry_header_format_.checksum));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800622 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800623
624 // TODO: UPDATE last_written_sector_ appropriately
625
David Rogers2761aeb2020-01-31 17:09:00 -0800626 key_descriptor->address = address;
627 key_descriptor->key_version = header.key_version();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800628 sector->valid_bytes += written;
629 sector->tail_free_bytes -= written;
630 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800631}
632
Keir Mierle8c352dc2020-02-02 13:58:19 -0800633void KeyValueStore::LogDebugInfo() {
634 const size_t sector_count = partition_.sector_count();
635 const size_t sector_size_bytes = partition_.sector_size_bytes();
636 DBG("====================== KEY VALUE STORE DUMP =========================");
637 DBG(" ");
638 DBG("Flash partition:");
639 DBG(" Sector count = %zu", sector_count);
640 DBG(" Sector max count = %zu", kUsableSectors);
641 DBG(" Sector size = %zu", sector_size_bytes);
642 DBG(" Total size = %zu", partition_.size_bytes());
643 DBG(" Alignment = %zu", partition_.alignment_bytes());
644 DBG(" ");
645 DBG("Key descriptors:");
646 DBG(" Entry count = %zu", key_descriptor_list_size_);
647 DBG(" Max entry count = %zu", kMaxEntries);
648 DBG(" ");
649 DBG(" # hash version address address (hex)");
650 for (size_t i = 0; i < key_descriptor_list_size_; ++i) {
651 const KeyDescriptor& kd = key_descriptor_list_[i];
652 DBG(" |%3zu: | %8zx |%8zu | %8zu | %8zx",
653 i,
654 size_t(kd.key_hash),
655 size_t(kd.key_version),
656 size_t(kd.address),
657 size_t(kd.address));
658 }
659 DBG(" ");
660
661 DBG("Sector descriptors:");
662 DBG(" # tail free valid has_space");
663 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
664 const SectorDescriptor& sd = sector_map_[sector_id];
665 DBG(" |%3zu: | %8zu |%8zu | %s",
666 sector_id,
667 size_t(sd.tail_free_bytes),
668 size_t(sd.valid_bytes),
669 sd.tail_free_bytes ? "YES" : "");
670 }
671 DBG(" ");
672
673 // TODO: This should stop logging after some threshold.
674 // size_t dumped_bytes = 0;
675 DBG("Sector raw data:");
676 for (size_t sector_id = 0; sector_id < sector_count; ++sector_id) {
677 // Read sector data. Yes, this will blow the stack on embedded.
678 std::array<byte, 500> raw_sector_data; // TODO
679 StatusWithSize sws =
680 partition_.Read(sector_id * sector_size_bytes, raw_sector_data);
681 DBG("Read: %zu bytes", sws.size());
682
683 DBG(" base addr offs 0 1 2 3 4 5 6 7");
684 for (size_t i = 0; i < sector_size_bytes; i += 8) {
685 DBG(" %3zu %8zx %5zu | %02x %02x %02x %02x %02x %02x %02x %02x",
686 sector_id,
687 (sector_id * sector_size_bytes) + i,
688 i,
689 static_cast<unsigned int>(raw_sector_data[i + 0]),
690 static_cast<unsigned int>(raw_sector_data[i + 1]),
691 static_cast<unsigned int>(raw_sector_data[i + 2]),
692 static_cast<unsigned int>(raw_sector_data[i + 3]),
693 static_cast<unsigned int>(raw_sector_data[i + 4]),
694 static_cast<unsigned int>(raw_sector_data[i + 5]),
695 static_cast<unsigned int>(raw_sector_data[i + 6]),
696 static_cast<unsigned int>(raw_sector_data[i + 7]));
697
698 // TODO: Fix exit condition.
699 if (i > 128) {
700 break;
701 }
702 }
703 DBG(" ");
704 }
705
706 DBG("////////////////////// KEY VALUE STORE DUMP END /////////////////////");
707}
708
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800709} // namespace pw::kvs