kfsw-services/src/journal/journal_store.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | #include <zephyr/fs/fs.h> | ||
| 5 | #include <zephyr/sys/byteorder.h> | ||
| 6 | #include <zephyr/sys/crc.h> | ||
| 7 | #include <zephyr/sys/util.h> | ||
| 8 | #include <kfsw/platform/storage.h> | ||
| 9 | |||
| 10 | #include "journal_store.h" | ||
| 11 | |||
| 12 | 404 | static int close_file(struct fs_file_t *file, int result) | |
| 13 | { | ||
| 14 | 404 | int closed = fs_close(file); | |
| 15 | |||
| 16 | 404 | return result ? result : closed; | |
| 17 | } | ||
| 18 | |||
| 19 | 270 | static int write_all(struct fs_file_t *file, const uint8_t *bytes, size_t size) | |
| 20 | { | ||
| 21 | 534 | while (size != 0U) { | |
| 22 | 272 | ssize_t written = fs_write(file, bytes, size); | |
| 23 | |||
| 24 | 272 | if (written <= 0) { | |
| 25 | 8 | return written < 0 ? (int)written : -EIO; | |
| 26 | } | ||
| 27 | 264 | bytes += written; | |
| 28 | 264 | size -= (size_t)written; | |
| 29 | } | ||
| 30 | 262 | return 0; | |
| 31 | } | ||
| 32 | |||
| 33 | 224 | static void encode(const struct kfsw_journal_record *record, uint8_t *bytes) | |
| 34 | { | ||
| 35 | 224 | memset(bytes, 0, KFSW_JOURNAL_RECORD_SIZE); | |
| 36 | /* codechecker_intentional [bugprone-not-null-terminated-result] a binary magic */ | ||
| 37 | 224 | memcpy(bytes, "KFE1", 4); | |
| 38 | 224 | bytes[4] = 1; | |
| 39 | 224 | bytes[5] = record->utc_valid; | |
| 40 | 224 | sys_put_be16(KFSW_JOURNAL_RECORD_SIZE, bytes + 6); | |
| 41 | 224 | sys_put_be64(record->sequence, bytes + 8); | |
| 42 | 224 | sys_put_be64(record->boot, bytes + 16); | |
| 43 | 224 | sys_put_be64(record->event.monotonic_us, bytes + 24); | |
| 44 | 224 | sys_put_be64((uint64_t)record->utc_seconds, bytes + 32); | |
| 45 | 224 | sys_put_be32(record->event.sequence, bytes + 40); | |
| 46 | 224 | sys_put_be16(record->event.source, bytes + 44); | |
| 47 | 224 | sys_put_be16(record->event.id, bytes + 46); | |
| 48 | 224 | bytes[48] = record->event.severity; | |
| 49 | 224 | bytes[49] = record->event.payload_size; | |
| 50 | 224 | memcpy(bytes + 52, record->event.payload, record->event.payload_size); | |
| 51 | 224 | sys_put_be32(crc32_ieee(bytes, 68), bytes + 68); | |
| 52 | 224 | } | |
| 53 | |||
| 54 | 234 | static int decode(const uint8_t *bytes, struct kfsw_journal_record *record) | |
| 55 | { | ||
| 56 | 468 | if ((memcmp(bytes, "KFE1", 4) != 0) || (bytes[4] != 1U) || (bytes[5] > 1U) || | |
| 57 | 468 | (sys_get_be16(bytes + 6) != KFSW_JOURNAL_RECORD_SIZE) || | |
| 58 | 464 | (sys_get_be32(bytes + 68) != crc32_ieee(bytes, 68)) || | |
| 59 | 456 | (sys_get_be16(bytes + 44) < KFSW_EVENT_SOURCE_BOOT) || | |
| 60 | 226 | (sys_get_be16(bytes + 44) > KFSW_EVENT_SOURCE_LIMITS) || | |
| 61 | 218 | (bytes[48] > KFSW_EVENT_CRITICAL) || (bytes[49] > KFSW_EVENT_MAX_PAYLOAD_SIZE) || | |
| 62 | 218 | (bytes[50] != 0U) || (bytes[51] != 0U)) { | |
| 63 | 16 | return -EBADMSG; | |
| 64 | } | ||
| 65 | 218 | memset(record, 0, sizeof(*record)); | |
| 66 | 218 | record->sequence = sys_get_be64(bytes + 8); | |
| 67 | 218 | record->boot = sys_get_be64(bytes + 16); | |
| 68 | 218 | if ((record->sequence == 0U) || (record->sequence == UINT64_MAX) || (record->boot == 0U) || | |
| 69 | 218 | (record->boot == UINT64_MAX)) { | |
| 70 | ✗ | return -EBADMSG; | |
| 71 | } | ||
| 72 | 218 | record->utc_valid = bytes[5] != 0U; | |
| 73 | 218 | record->utc_seconds = (int64_t)sys_get_be64(bytes + 32); | |
| 74 | 218 | record->event.monotonic_us = sys_get_be64(bytes + 24); | |
| 75 | 218 | record->event.sequence = sys_get_be32(bytes + 40); | |
| 76 | 218 | record->event.source = sys_get_be16(bytes + 44); | |
| 77 | 218 | record->event.id = sys_get_be16(bytes + 46); | |
| 78 | 218 | record->event.severity = bytes[48]; | |
| 79 | 218 | record->event.payload_size = bytes[49]; | |
| 80 | 218 | memcpy(record->event.payload, bytes + 52, record->event.payload_size); | |
| 81 | 218 | return 0; | |
| 82 | } | ||
| 83 | |||
| 84 | 92 | int kfsw_journal_store_open(struct kfsw_journal_store *store) | |
| 85 | { | ||
| 86 | 92 | uint8_t header[KFSW_JOURNAL_HEADER_SIZE] = {0}; | |
| 87 | uint8_t bytes[KFSW_JOURNAL_RECORD_SIZE]; | ||
| 88 | struct kfsw_journal_record record; | ||
| 89 | struct fs_dirent info; | ||
| 90 | struct fs_file_t file; | ||
| 91 | int result; | ||
| 92 | |||
| 93 | 92 | memset(store, 0, sizeof(*store)); | |
| 94 | 92 | store->next_sequence = 1U; | |
| 95 | 92 | if (!kfsw_storage_is_ready()) { | |
| 96 | 2 | return -ENODEV; | |
| 97 | } | ||
| 98 | 90 | result = fs_stat(KFSW_JOURNAL_PATH, &info); | |
| 99 | 90 | if ((result != 0) && (result != -ENOENT)) { | |
| 100 | ✗ | return result; | |
| 101 | } | ||
| 102 | 90 | fs_file_t_init(&file); | |
| 103 | 90 | if (result == -ENOENT) { | |
| 104 | 46 | memcpy(header, "KFJ1", 4); | |
| 105 | 46 | header[4] = 1U; | |
| 106 | 46 | sys_put_be16(KFSW_JOURNAL_RECORD_SIZE, header + 6); | |
| 107 | 46 | sys_put_be32(CONFIG_KFSW_JOURNAL_CAPACITY, header + 8); | |
| 108 | 46 | sys_put_be32(crc32_ieee(header, 12), header + 12); | |
| 109 | 46 | result = fs_open(&file, KFSW_JOURNAL_PATH, FS_O_CREATE | FS_O_WRITE); | |
| 110 | 46 | if (result != 0) { | |
| 111 | ✗ | return result; | |
| 112 | } | ||
| 113 | 46 | result = write_all(&file, header, sizeof(header)); | |
| 114 | 46 | if (result == 0) { | |
| 115 | 42 | result = fs_sync(&file); | |
| 116 | } | ||
| 117 | 46 | result = close_file(&file, result); | |
| 118 | 46 | if (result != 0) { | |
| 119 | 4 | (void)fs_unlink(KFSW_JOURNAL_PATH); | |
| 120 | } | ||
| 121 | 46 | store->ready = result == 0; | |
| 122 | 46 | return result; | |
| 123 | } | ||
| 124 | 44 | if ((info.type != FS_DIR_ENTRY_FILE) || | |
| 125 | 44 | (info.size > | |
| 126 | KFSW_JOURNAL_HEADER_SIZE + CONFIG_KFSW_JOURNAL_CAPACITY * KFSW_JOURNAL_RECORD_SIZE)) { | ||
| 127 | ✗ | return -EFBIG; | |
| 128 | } | ||
| 129 | 44 | result = fs_open(&file, KFSW_JOURNAL_PATH, FS_O_READ); | |
| 130 | 44 | if (result != 0) { | |
| 131 | ✗ | return result; | |
| 132 | } | ||
| 133 | 44 | ssize_t read = fs_read(&file, header, sizeof(header)); | |
| 134 | |||
| 135 | 44 | if ((read != sizeof(header)) || (memcmp(header, "KFJ1", 4) != 0) || (header[4] != 1U) || | |
| 136 | 80 | (header[5] != 0U) || (sys_get_be16(header + 6) != KFSW_JOURNAL_RECORD_SIZE) || | |
| 137 | 80 | (sys_get_be32(header + 8) != CONFIG_KFSW_JOURNAL_CAPACITY) || | |
| 138 | 40 | (sys_get_be32(header + 12) != crc32_ieee(header, 12))) { | |
| 139 | 4 | return close_file(&file, read < 0 ? (int)read : -EBADMSG); | |
| 140 | } | ||
| 141 | 184 | for (size_t i = 0; i < ARRAY_SIZE(store->sequences); i++) { | |
| 142 | 176 | read = fs_read(&file, bytes, sizeof(bytes)); | |
| 143 | 176 | if (read < 0) { | |
| 144 | ✗ | return close_file(&file, (int)read); | |
| 145 | } | ||
| 146 | 176 | if (read == 0) { | |
| 147 | 26 | break; | |
| 148 | } | ||
| 149 | 150 | if (read != sizeof(bytes)) { | |
| 150 | 6 | store->corrupt++; | |
| 151 | 6 | break; | |
| 152 | } | ||
| 153 | 144 | if ((decode(bytes, &record) != 0) || | |
| 154 | 128 | ((record.sequence - 1U) % ARRAY_SIZE(store->sequences) != i)) { | |
| 155 | 16 | store->corrupt++; | |
| 156 | 16 | continue; | |
| 157 | } | ||
| 158 | 128 | store->sequences[i] = record.sequence; | |
| 159 | 128 | store->next_sequence = MAX(store->next_sequence, record.sequence + 1U); | |
| 160 | 128 | store->highest_boot = MAX(store->highest_boot, record.boot); | |
| 161 | 128 | store->held++; | |
| 162 | } | ||
| 163 | 40 | result = close_file(&file, 0); | |
| 164 | 40 | store->ready = result == 0; | |
| 165 | 40 | return result; | |
| 166 | } | ||
| 167 | |||
| 168 | 228 | int kfsw_journal_store_append(struct kfsw_journal_store *store, struct kfsw_journal_record *record) | |
| 169 | { | ||
| 170 | uint8_t bytes[KFSW_JOURNAL_RECORD_SIZE]; | ||
| 171 | struct fs_file_t file; | ||
| 172 | size_t slot; | ||
| 173 | int result; | ||
| 174 | |||
| 175 | 228 | if (!store->ready || !kfsw_storage_is_ready()) { | |
| 176 | ✗ | return -ENODEV; | |
| 177 | } | ||
| 178 | 228 | if ((record->boot == 0U) || (record->boot == UINT64_MAX) || | |
| 179 | 228 | (record->event.payload_size > KFSW_EVENT_MAX_PAYLOAD_SIZE) || | |
| 180 | 228 | (record->event.severity > KFSW_EVENT_CRITICAL)) { | |
| 181 | ✗ | return -EINVAL; | |
| 182 | } | ||
| 183 | 228 | if (record->sequence == 0U) { | |
| 184 | 214 | record->sequence = store->next_sequence; | |
| 185 | } | ||
| 186 | 228 | if ((record->sequence == UINT64_MAX) || (record->sequence > store->next_sequence)) { | |
| 187 | ✗ | return -EOVERFLOW; | |
| 188 | } | ||
| 189 | 228 | slot = (record->sequence - 1U) % ARRAY_SIZE(store->sequences); | |
| 190 | 228 | if (store->sequences[slot] > record->sequence) { | |
| 191 | 4 | return -ESTALE; | |
| 192 | } | ||
| 193 | 224 | encode(record, bytes); | |
| 194 | 224 | fs_file_t_init(&file); | |
| 195 | 224 | result = fs_open(&file, KFSW_JOURNAL_PATH, FS_O_WRITE); | |
| 196 | 224 | if (result != 0) { | |
| 197 | ✗ | store->ready = false; | |
| 198 | ✗ | return result; | |
| 199 | } | ||
| 200 | 224 | result = fs_seek(&file, KFSW_JOURNAL_HEADER_SIZE + slot * sizeof(bytes), FS_SEEK_SET); | |
| 201 | 224 | if (result == 0) { | |
| 202 | 224 | result = write_all(&file, bytes, sizeof(bytes)); | |
| 203 | } | ||
| 204 | 224 | if (result == 0) { | |
| 205 | 220 | result = fs_sync(&file); | |
| 206 | } | ||
| 207 | 224 | result = close_file(&file, result); | |
| 208 | 224 | if (result != 0) { | |
| 209 | 6 | store->ready = false; | |
| 210 | 6 | return result; | |
| 211 | } | ||
| 212 | 218 | if (store->sequences[slot] == 0U) { | |
| 213 | 144 | store->held++; | |
| 214 | } | ||
| 215 | 218 | store->sequences[slot] = record->sequence; | |
| 216 | 218 | store->next_sequence = MAX(store->next_sequence, record->sequence + 1U); | |
| 217 | 218 | store->highest_boot = MAX(store->highest_boot, record->boot); | |
| 218 | 218 | return 0; | |
| 219 | } | ||
| 220 | |||
| 221 | 94 | int kfsw_journal_store_get(struct kfsw_journal_store *store, uint16_t age, | |
| 222 | struct kfsw_journal_record *record) | ||
| 223 | { | ||
| 224 | 94 | uint64_t ceiling = UINT64_MAX; | |
| 225 | 94 | uint64_t sequence = 0; | |
| 226 | uint8_t bytes[KFSW_JOURNAL_RECORD_SIZE]; | ||
| 227 | struct fs_file_t file; | ||
| 228 | 94 | size_t slot = 0; | |
| 229 | int result; | ||
| 230 | |||
| 231 | 94 | if (!store->ready) { | |
| 232 | ✗ | return -ENODEV; | |
| 233 | } | ||
| 234 | 94 | if (age >= store->held) { | |
| 235 | 4 | return -ENOENT; | |
| 236 | } | ||
| 237 | 308 | for (unsigned int rank = 0; rank <= age; rank++) { | |
| 238 | 218 | sequence = 0; | |
| 239 | 1962 | for (size_t i = 0; i < ARRAY_SIZE(store->sequences); i++) { | |
| 240 | 1744 | if ((store->sequences[i] > sequence) && (store->sequences[i] < ceiling)) { | |
| 241 | 598 | sequence = store->sequences[i]; | |
| 242 | 598 | slot = i; | |
| 243 | } | ||
| 244 | } | ||
| 245 | 218 | ceiling = sequence; | |
| 246 | } | ||
| 247 | 90 | fs_file_t_init(&file); | |
| 248 | 90 | result = fs_open(&file, KFSW_JOURNAL_PATH, FS_O_READ); | |
| 249 | 90 | if (result != 0) { | |
| 250 | ✗ | return result; | |
| 251 | } | ||
| 252 | 90 | result = fs_seek(&file, KFSW_JOURNAL_HEADER_SIZE + slot * sizeof(bytes), FS_SEEK_SET); | |
| 253 | 90 | if (result == 0) { | |
| 254 | 90 | ssize_t read = fs_read(&file, bytes, sizeof(bytes)); | |
| 255 | |||
| 256 | 90 | result = (read < 0) ? (int)read | |
| 257 | 90 | : ((read == sizeof(bytes)) ? decode(bytes, record) : -EBADMSG); | |
| 258 | 90 | if ((result == 0) && (record->sequence != sequence)) { | |
| 259 | ✗ | result = -EBADMSG; | |
| 260 | } | ||
| 261 | } | ||
| 262 | 90 | return close_file(&file, result); | |
| 263 | } | ||
| 264 |