Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/store/store_engine.h # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / store / store_engine.h` 182 lines ยท 6333 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * store_engine.h -- C twin of the Store v1 mount and transaction engine 19 * (crates/aienos-kernel/src/store/{engine,device,checkpoint}.rs). 20 * 21 * The engine sees only 4096-byte Store units through st_dev. st_disk binds 22 * st_dev to a native/disk disk_dev (512-byte blocks: 8 per unit; 4096-byte 23 * blocks: 1 per unit; anything else refused; never read-modify-write). 24 * 25 * No heap: every catalog, plan and scratch buffer lives in a caller-provided 26 * st_workspace (about 0.9 MiB; place it in static storage). No clock. All 27 * I/O goes through st_dev. 28 */ 29 #ifndef AIENOS_STORE_ENGINE_H 30 #define AIENOS_STORE_ENGINE_H 31 32 #include 33 #include 34 35 #include "disk.h" 36 #include "store_v1.h" 37 38 /* MountState */ 39 enum { ST_VALID = 0, ST_DEGRADED_RECOVERY = 1 }; 40 /* PeerCondition */ 41 enum { ST_PEER_ZERO = 0, ST_PEER_VALID, ST_PEER_MALFORMED, ST_PEER_GRAPH_BAD_NEWER, 42 ST_PEER_GRAPH_BAD_OLDER }; 43 44 /* MountError (returned by st_open). */ 45 enum { 46 ST_M_IO = -101, 47 ST_M_UNFORMATTED = -102, 48 ST_M_FOREIGN = -103, 49 ST_M_UNSUPPORTED_VERSION = -104, 50 ST_M_CORRUPT_RECOVERY_REQUIRED = -105, 51 ST_M_CONFLICTING_ROOTS = -106, 52 ST_M_INCONSISTENT_HISTORY = -107, 53 }; 54 55 /* StoreError. StoreError::Format(e) is returned as the SV1_E_* value. */ 56 enum { 57 ST_E_IO = -201, 58 ST_E_CORRUPT = -202, 59 ST_E_READ_ONLY_DEGRADED = -203, 60 ST_E_DUPLICATE_OBJECT_CONFLICT = -204, 61 ST_E_NO_SPACE = -205, 62 ST_E_CATALOG_FULL = -206, 63 ST_E_GENERATION_EXHAUSTED = -207, 64 ST_E_TRANSACTION_LIMIT = -208, 65 ST_E_INVALID_OBJECT = -209, 66 ST_E_NEEDS_REOPEN = -210, 67 /* C only */ 68 ST_E_ARG = -211, /* NULL pointer or caller buffer too small */ 69 ST_E_GEOMETRY = -212, /* st_disk: block size, alignment or region bounds */ 70 }; 71 72 const char *st_strerror(int e); 73 74 /* Checkpoints (checkpoint.rs), observed in this order by a hook. */ 75 enum { 76 ST_CP_BEFORE_FIRST_WRITE = 0, 77 ST_CP_AFTER_PAYLOAD_OBJECTS, 78 ST_CP_AFTER_CATALOG, 79 ST_CP_AFTER_COMMIT_RECORD, 80 ST_CP_AFTER_FIRST_FLUSH, 81 ST_CP_AFTER_INACTIVE_SUPERBLOCK, 82 ST_CP_AFTER_FINAL_FLUSH, 83 }; 84 const char *st_checkpoint_name(int cp); 85 typedef void (*st_hook)(void *arg, int checkpoint); 86 87 /* StoreDevice: bounded 4096-byte unit I/O. Callbacks return 0 on success. */ 88 typedef struct { 89 void *ctx; 90 uint64_t region_units; 91 int (*read_unit)(void *ctx, uint64_t unit, uint8_t out[SV1_UNIT]); 92 int (*write_unit)(void *ctx, uint64_t unit, const uint8_t in[SV1_UNIT]); 93 int (*flush)(void *ctx); 94 } st_dev; 95 96 /* ---- geometry adapter over native/disk ---- */ 97 typedef struct { 98 const disk_dev *disk; 99 uint64_t base_lba; 100 uint32_t blocks_per_unit; 101 uint64_t region_units; 102 } st_disk; 103 104 /* Bind region [base_lba, base_lba + units * blocks_per_unit) of disk. 105 * region_units == 0 means every whole unit to the end of the device. 106 * Refuses (ST_E_GEOMETRY) a block size other than 512/4096, a base_lba not 107 * aligned to a unit, or a region past the device end. Touches no media. */ 108 int st_disk_bind(st_disk *a, const disk_dev *disk, uint64_t base_lba, uint64_t region_units, 109 st_dev *out); 110 111 /* ---- engine ---- */ 112 #define ST_CAT_CAP (SV1_MAX_CATALOG_ENTRIES + SV1_MAX_TRANSACTION_OBJECTS) 113 114 typedef struct { 115 uint16_t kind; 116 uint16_t version; 117 const uint8_t *bytes; /* must stay valid for the transaction */ 118 uint64_t len; 119 } st_object; 120 121 typedef struct { 122 uint8_t id[32]; 123 uint16_t kind, version; 124 const uint8_t *bytes; 125 uint64_t len; 126 uint32_t units; 127 uint64_t first_unit; 128 int append; /* 1 if written by this transaction */ 129 } st_req; 130 131 typedef struct { 132 sv1_entry cat[2][ST_CAT_CAP]; /* active root, spare (open: slot A, slot B) */ 133 uint8_t catbuf[SV1_MAX_CATALOG_UNITS * SV1_UNIT]; 134 uint8_t unit[SV1_UNIT]; 135 uint8_t unit2[SV1_UNIT]; 136 uint8_t plan_commit[SV1_UNIT]; /* commit unit of the transaction being written */ 137 uint8_t plan_sb[SV1_UNIT]; /* superblock of the transaction being written */ 138 sv1_extent ext[ST_CAT_CAP]; 139 st_req req[SV1_MAX_TRANSACTION_OBJECTS]; 140 } st_workspace; 141 142 typedef struct { 143 sv1_superblock sb; 144 sv1_commit commit; 145 uint8_t commit_id[32]; 146 uint32_t cat_index; /* which ws->cat[] holds the entries */ 147 uint32_t n; 148 } st_root; 149 150 typedef struct { 151 st_dev dev; 152 st_workspace *ws; 153 st_root root; 154 int state; 155 int peer; 156 int poisoned; 157 } st_store; 158 159 /* Store::open. Never writes or flushes. Returns 0 or ST_M_*. */ 160 int st_open(st_store *s, const st_dev *dev, st_workspace *ws); 161 /* Store::transact / transact_with_hook (hook may be NULL). 162 * The caller must keep every objects[i].data buffer unchanged and alive until 163 * st_transact returns: payloads are hashed and then written in separate passes 164 * and are not copied. */ 165 int st_transact(st_store *s, const st_object *objects, size_t n, st_hook hook, void *arg); 166 /* Store::read_object into out (cap bytes). On any error out is zeroed. */ 167 int st_read_object(st_store *s, const uint8_t id[32], uint8_t *out, size_t cap, size_t *len); 168 169 static inline uint64_t st_generation(const st_store *s) { return s->root.commit.generation; } 170 static inline uint64_t st_high_water(const st_store *s) { return s->root.commit.committed_high_water; } 171 static inline uint32_t st_active_slot(const st_store *s) { return s->root.sb.slot_id; } 172 static inline const sv1_entry *st_catalog(const st_store *s, uint32_t *n) 173 { 174 *n = s->root.n; 175 return s->ws->cat[s->root.cat_index]; 176 } 177 178 /* Format a blank region: genesis units 2, 3, a zero slot B, flush, then 179 * superblock A, flush (the same bytes as genesis.rs genesis_units). */ 180 int st_format(const st_dev *dev, const uint8_t uuid[16]); 181 182 #endif /* AIENOS_STORE_ENGINE_H */