blob: a245dec446bfa865bb76c88ec061908e278c34ce [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
Wyatt Heplered163b02020-02-03 17:49:32 -0800299StatusWithSize KeyValueStore::ValueSize(std::string_view key) const {
300 TRY(InvalidOperation(key));
301
302 const KeyDescriptor* key_descriptor;
303 TRY(FindKeyDescriptor(key, &key_descriptor));
304
305 EntryHeader header;
306 TRY(ReadEntryHeader(*key_descriptor, &header));
307
308 return StatusWithSize(header.value_length());
309}
310
Keir Mierle8c352dc2020-02-02 13:58:19 -0800311Status KeyValueStore::ValidateEntryChecksum(const EntryHeader& header,
312 string_view key,
313 const KeyDescriptor& entry) const {
314 // TODO: Remove this block once the checksums are implemented in test.
315 if (entry_header_format_.checksum == nullptr) {
316 const auto header_checksum = header.checksum();
317 const auto fixed_checksum = as_bytes(span(&kNoChecksum, 1));
318 if (std::memcmp(header_checksum.data(),
319 fixed_checksum.data(),
320 header_checksum.size()) == 0) {
321 return Status::OK;
322 }
323 return Status::DATA_LOSS;
324
325 // TODO: It's unclear why the below version doesn't work.
326 // return header.checksum() == as_bytes(span(&kNoChecksum, 1));
327 }
328 (void)(header);
329 (void)(key);
330 (void)(entry);
331 // TODO: Do incremental checksum verification by reading the value from
332 // flash a few blocks at a time.
333 return Status::OK;
334}
335
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800336Status KeyValueStore::InvalidOperation(string_view key) const {
Wyatt Hepleracaacf92020-01-24 10:58:30 -0800337 if (InvalidKey(key)) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800338 return Status::INVALID_ARGUMENT;
339 }
Keir Mierle8c352dc2020-02-02 13:58:19 -0800340 if (!enabled_) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800341 return Status::FAILED_PRECONDITION;
342 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800343 return Status::OK;
344}
345
David Rogers2761aeb2020-01-31 17:09:00 -0800346Status KeyValueStore::FindKeyDescriptor(string_view key,
347 const KeyDescriptor** result) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800348 char key_buffer[kMaxKeyLength];
349 const uint32_t hash = HashKey(key);
Wyatt Heplerb7609542020-01-24 10:29:54 -0800350
David Rogers2761aeb2020-01-31 17:09:00 -0800351 for (auto& descriptor : key_descriptors()) {
352 if (descriptor.key_hash == hash) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800353 DBG("Found match! For hash: %zx", size_t(hash));
David Rogers2761aeb2020-01-31 17:09:00 -0800354 TRY(ReadEntryKey(descriptor, key.size(), key_buffer));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800355
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800356 if (key == string_view(key_buffer, key.size())) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800357 DBG("Keys matched too");
David Rogers2761aeb2020-01-31 17:09:00 -0800358 *result = &descriptor;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800359 return Status::OK;
360 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800361 }
362 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800363 return Status::NOT_FOUND;
364}
365
David Rogers2761aeb2020-01-31 17:09:00 -0800366Status KeyValueStore::ReadEntryHeader(const KeyDescriptor& descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800367 EntryHeader* header) const {
David Rogers2761aeb2020-01-31 17:09:00 -0800368 return partition_.Read(descriptor.address, sizeof(*header), header).status();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800369}
370
David Rogers2761aeb2020-01-31 17:09:00 -0800371Status KeyValueStore::ReadEntryKey(const KeyDescriptor& descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800372 size_t key_length,
373 char* key) const {
374 // TODO: This check probably shouldn't be here; this is like
375 // checking that the Cortex M's RAM isn't corrupt. This should be
376 // done at boot time.
377 // ^^ This argument sometimes comes from EntryHeader::key_value_len,
378 // which is read directly from flash. If it's corrupted, we shouldn't try
379 // to read a bunch of extra data.
380 if (key_length == 0u || key_length > kMaxKeyLength) {
381 return Status::DATA_LOSS;
382 }
383 // The key is immediately after the entry header.
David Rogers2761aeb2020-01-31 17:09:00 -0800384 return partition_
385 .Read(descriptor.address + sizeof(EntryHeader), key_length, key)
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800386 .status();
387}
388
David Rogers2761aeb2020-01-31 17:09:00 -0800389StatusWithSize KeyValueStore::ReadEntryValue(
390 const KeyDescriptor& key_descriptor,
391 const EntryHeader& header,
392 span<byte> value) const {
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800393 const size_t read_size = std::min(header.value_length(), value.size());
David Rogers2761aeb2020-01-31 17:09:00 -0800394 StatusWithSize result = partition_.Read(
395 key_descriptor.address + sizeof(header) + header.key_length(),
Keir Mierle8c352dc2020-02-02 13:58:19 -0800396 value.subspan(0, read_size));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800397 TRY(result);
398 if (read_size != header.value_length()) {
399 return StatusWithSize(Status::RESOURCE_EXHAUSTED, read_size);
400 }
401 return StatusWithSize(read_size);
402}
403
404Status KeyValueStore::ValidateEntryChecksum(const EntryHeader& header,
405 string_view key,
406 span<const byte> value) const {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800407 // TODO: Re-enable this when we have checksum implementations.
408 return Status::OK;
409
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800410 CalculateEntryChecksum(header, key, value);
411 return entry_header_format_.checksum->Verify(header.checksum());
412}
413
David Rogers2761aeb2020-01-31 17:09:00 -0800414Status KeyValueStore::WriteEntryForExistingKey(KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800415 string_view key,
416 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800417 SectorDescriptor* sector;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800418 TRY(FindOrRecoverSectorWithSpace(&sector, EntrySize(key, value)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800419 DBG("Writing existing entry; found sector: %d",
420 static_cast<int>(sector - sector_map_.data()));
David Rogers2761aeb2020-01-31 17:09:00 -0800421 return AppendEntry(sector, key_descriptor, key, value);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800422}
423
424Status KeyValueStore::WriteEntryForNewKey(string_view key,
425 span<const byte> value) {
David Rogers2761aeb2020-01-31 17:09:00 -0800426 if (KeyListFull()) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800427 WRN("KVS full: trying to store a new entry, but can't. Have %zu entries",
428 key_descriptor_list_size_);
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800429 return Status::RESOURCE_EXHAUSTED;
430 }
431
David Rogers2761aeb2020-01-31 17:09:00 -0800432 // Modify the key descriptor at the end of the array, without bumping the map
433 // size so the key descriptor is prepared and written without committing
434 // first.
435 KeyDescriptor& key_descriptor =
436 key_descriptor_list_[key_descriptor_list_size_];
437 key_descriptor.key_hash = HashKey(key);
438 key_descriptor.key_version = 0; // will be incremented by AppendEntry()
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800439
David Rogers2761aeb2020-01-31 17:09:00 -0800440 SectorDescriptor* sector;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800441 TRY(FindOrRecoverSectorWithSpace(&sector, EntrySize(key, value)));
Keir Mierle8c352dc2020-02-02 13:58:19 -0800442 DBG("Writing new entry; found sector: %d",
443 static_cast<int>(sector - sector_map_.data()));
David Rogers2761aeb2020-01-31 17:09:00 -0800444 TRY(AppendEntry(sector, &key_descriptor, key, value));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800445
Keir Mierle8c352dc2020-02-02 13:58:19 -0800446 // Only increment bump our size when we are certain the write succeeded.
David Rogers2761aeb2020-01-31 17:09:00 -0800447 key_descriptor_list_size_ += 1;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800448 return Status::OK;
449}
450
David Rogers2761aeb2020-01-31 17:09:00 -0800451Status KeyValueStore::RelocateEntry(KeyDescriptor& key_descriptor) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800452 // TODO: Implement this function!
David Rogers2761aeb2020-01-31 17:09:00 -0800453 (void)key_descriptor;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800454 return Status::OK;
David Rogersa12786b2020-01-31 16:02:33 -0800455 return Status::UNIMPLEMENTED;
456}
457
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800458// Find either an existing sector with enough space, or an empty sector.
459// Maintains the invariant that there is always at least 1 empty sector.
David Rogers2761aeb2020-01-31 17:09:00 -0800460KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorWithSpace(
461 size_t size) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800462 const size_t sector_count = partition_.sector_count();
463
464 // TODO: Ignore last written sector for now and scan from the beginning.
465 last_written_sector_ = sector_count - 1;
466
467 size_t start = (last_written_sector_ + 1) % sector_count;
David Rogers2761aeb2020-01-31 17:09:00 -0800468 SectorDescriptor* first_empty_sector = nullptr;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800469 bool at_least_two_empty_sectors = false;
470
471 for (size_t i = start; i != last_written_sector_;
Keir Mierle8c352dc2020-02-02 13:58:19 -0800472 i = (i + 1) % sector_count) {
473 DBG("Examining sector %zu", i);
David Rogers2761aeb2020-01-31 17:09:00 -0800474 SectorDescriptor& sector = sector_map_[i];
Keir Mierle8c352dc2020-02-02 13:58:19 -0800475
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800476 if (!SectorEmpty(sector) && sector.HasSpace(size)) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800477 DBG("Partially occupied sector with enough space; done!");
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800478 return &sector;
479 }
480
481 if (SectorEmpty(sector)) {
482 if (first_empty_sector == nullptr) {
483 first_empty_sector = &sector;
484 } else {
485 at_least_two_empty_sectors = true;
Wyatt Hepler2ad60672020-01-21 08:00:16 -0800486 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800487 }
488 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800489
490 if (at_least_two_empty_sectors) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800491 DBG("Found a empty sector and a spare one; returning the first found (%d)",
492 static_cast<int>(first_empty_sector - sector_map_.data()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800493 return first_empty_sector;
494 }
495 return nullptr;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800496}
497
David Rogers2761aeb2020-01-31 17:09:00 -0800498Status KeyValueStore::FindOrRecoverSectorWithSpace(SectorDescriptor** sector,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800499 size_t size) {
500 *sector = FindSectorWithSpace(size);
501 if (*sector != nullptr) {
502 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800503 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800504 if (options_.partial_gc_on_write) {
505 return GarbageCollectOneSector(sector);
506 }
507 return Status::RESOURCE_EXHAUSTED;
508}
509
David Rogers2761aeb2020-01-31 17:09:00 -0800510KeyValueStore::SectorDescriptor* KeyValueStore::FindSectorToGarbageCollect() {
511 SectorDescriptor* sector_candidate = nullptr;
David Rogersa12786b2020-01-31 16:02:33 -0800512 size_t candidate_bytes = 0;
513
514 // Step 1: Try to find a sectors with stale keys and no valid keys (no
515 // relocation needed). If any such sectors are found, use the sector with the
516 // most reclaimable bytes.
517 for (auto& sector : sector_map_) {
518 if ((sector.valid_bytes == 0) &&
519 (RecoverableBytes(sector) > candidate_bytes)) {
520 sector_candidate = &sector;
521 candidate_bytes = RecoverableBytes(sector);
522 }
523 }
524
525 // Step 2: If step 1 yields no sectors, just find the sector with the most
526 // reclaimable bytes.
527 if (sector_candidate == nullptr) {
528 for (auto& sector : sector_map_) {
529 if (RecoverableBytes(sector) > candidate_bytes) {
530 sector_candidate = &sector;
531 candidate_bytes = RecoverableBytes(sector);
532 }
533 }
534 }
535
536 return sector_candidate;
537}
538
David Rogers2761aeb2020-01-31 17:09:00 -0800539Status KeyValueStore::GarbageCollectOneSector(SectorDescriptor** sector) {
David Rogersa12786b2020-01-31 16:02:33 -0800540 // Step 1: Find the sector to garbage collect
David Rogers2761aeb2020-01-31 17:09:00 -0800541 SectorDescriptor* sector_to_gc = FindSectorToGarbageCollect();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800542
David Rogersa12786b2020-01-31 16:02:33 -0800543 if (sector_to_gc == nullptr) {
544 return Status::RESOURCE_EXHAUSTED;
545 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800546
David Rogersa12786b2020-01-31 16:02:33 -0800547 // Step 2: Move any valid entries in the GC sector to other sectors
548 if (sector_to_gc->valid_bytes != 0) {
David Rogers2761aeb2020-01-31 17:09:00 -0800549 for (auto& descriptor : key_descriptors()) {
550 if (AddressInSector(*sector_to_gc, descriptor.address)) {
551 TRY(RelocateEntry(descriptor));
David Rogersa12786b2020-01-31 16:02:33 -0800552 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800553 }
554 }
Wyatt Heplerb7609542020-01-24 10:29:54 -0800555
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800556 if (sector_to_gc->valid_bytes != 0) {
Wyatt Heplerb7609542020-01-24 10:29:54 -0800557 return Status::INTERNAL;
558 }
559
David Rogersa12786b2020-01-31 16:02:33 -0800560 // Step 3: Reinitialize the sector
561 sector_to_gc->tail_free_bytes = 0;
562 TRY(partition_.Erase(SectorBaseAddress(sector_to_gc), 1));
563 sector_to_gc->tail_free_bytes = partition_.sector_size_bytes();
Wyatt Heplerb7609542020-01-24 10:29:54 -0800564
David Rogersa12786b2020-01-31 16:02:33 -0800565 *sector = sector_to_gc;
566 return Status::OK;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800567}
568
David Rogers2761aeb2020-01-31 17:09:00 -0800569Status KeyValueStore::AppendEntry(SectorDescriptor* sector,
570 KeyDescriptor* key_descriptor,
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800571 const string_view key,
572 span<const byte> value) {
573 // write header, key, and value
574 const EntryHeader header(entry_header_format_.magic,
575 CalculateEntryChecksum(header, key, value),
576 key.size(),
577 value.size(),
David Rogers2761aeb2020-01-31 17:09:00 -0800578 key_descriptor->key_version + 1);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800579 DBG("Appending entry with key version: %zx", size_t(header.key_version()));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800580
581 // Handles writing multiple concatenated buffers, while breaking up the writes
582 // into alignment-sized blocks.
583 Address address = NextWritableAddress(sector);
Keir Mierle8c352dc2020-02-02 13:58:19 -0800584 DBG("Appending to address: %zx", size_t(address));
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800585 TRY_ASSIGN(
586 size_t written,
587 partition_.Write(
588 address, {as_bytes(span(&header, 1)), as_bytes(span(key)), value}));
589
590 if (options_.verify_on_write) {
David Rogers2761aeb2020-01-31 17:09:00 -0800591 TRY(VerifyEntry(sector, key_descriptor));
Wyatt Heplerb7609542020-01-24 10:29:54 -0800592 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800593
594 // TODO: UPDATE last_written_sector_ appropriately
595
David Rogers2761aeb2020-01-31 17:09:00 -0800596 key_descriptor->address = address;
597 key_descriptor->key_version = header.key_version();
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800598 sector->valid_bytes += written;
599 sector->tail_free_bytes -= written;
600 return Status::OK;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800601}
602
David Rogers2761aeb2020-01-31 17:09:00 -0800603Status KeyValueStore::VerifyEntry(SectorDescriptor* sector,
604 KeyDescriptor* key_descriptor) {
Wyatt Heplered163b02020-02-03 17:49:32 -0800605 // TODO: Remove this once checksums are fully implemented.
606 return Status::OK;
607
Keir Mierle8c352dc2020-02-02 13:58:19 -0800608 if (entry_header_format_.checksum == nullptr) {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800609 return Status::OK;
610 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800611 // TODO: Implement me!
612 (void)sector;
David Rogers2761aeb2020-01-31 17:09:00 -0800613 (void)key_descriptor;
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800614 return Status::UNIMPLEMENTED;
Wyatt Heplerb7609542020-01-24 10:29:54 -0800615}
616
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800617span<const byte> KeyValueStore::CalculateEntryChecksum(
618 const EntryHeader& header,
619 const string_view key,
620 span<const byte> value) const {
Keir Mierle8c352dc2020-02-02 13:58:19 -0800621 if (entry_header_format_.checksum == nullptr) {
622 return as_bytes(span(&kNoChecksum, 1));
623 }
Wyatt Hepler4da1fcb2020-01-30 17:32:18 -0800624 auto& checksum = *entry_header_format_.checksum;
625
626 checksum.Reset();
627 checksum.Update(header.DataForChecksum());
628 checksum.Update(as_bytes(span(key)));
629 checksum.Update(value.data(), value.size_bytes());
630 return checksum.state();
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