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