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/disk/disk.c # 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 / disk / disk.c` 170 lines ยท 4821 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 * disk.c -- checked entry points and bounded request queue (see disk.h). 19 * Freestanding: no libc, no heap, no clock, no I/O except through the 20 * backend callbacks. 21 */ 22 #include "disk.h" 23 24 int disk_check(const disk_dev *d) 25 { 26 if (!d || !d->read || !d->write || !d->flush) 27 return DISK_EARG; 28 if (d->block_size != 512u && d->block_size != 4096u) 29 return DISK_EGEOMETRY; 30 if (d->block_count == 0 || d->max_blocks_per_io == 0) 31 return DISK_EGEOMETRY; 32 return DISK_OK; 33 } 34 35 static int range_ok(const disk_dev *d, uint64_t lba, uint32_t count) 36 { 37 if (count == 0) return DISK_EARG; /* GUARD:disk-zero */ 38 if (lba >= d->block_count) return DISK_ERANGE; /* GUARD:disk-lba */ 39 if ((uint64_t)count > d->block_count - lba) return DISK_ERANGE; /* GUARD:disk-end */ 40 return DISK_OK; 41 } 42 43 /* Keep the two backend codes a caller must tell apart from a plain I/O error: 44 * a simulated power cut and a controller that failed closed. */ 45 static int backend_rc(int brc) 46 { 47 return (brc == DISK_EPOWER || brc == DISK_ESTATE) ? brc : DISK_EIO; 48 } 49 50 int disk_read(const disk_dev *d, uint64_t lba, uint32_t count, uint8_t *buf) 51 { 52 int rc = disk_check(d); 53 if (rc != DISK_OK) 54 return rc; 55 if (!buf) 56 return DISK_EARG; 57 if ((rc = range_ok(d, lba, count)) != DISK_OK) 58 return rc; 59 while (count) { 60 uint32_t n = count < d->max_blocks_per_io ? count : d->max_blocks_per_io; 61 int brc = d->read(d->ctx, lba, n, buf); 62 if (brc != 0) 63 return backend_rc(brc); 64 lba += n; 65 count -= n; 66 buf += (size_t)n * d->block_size; 67 } 68 return DISK_OK; 69 } 70 71 int disk_write(const disk_dev *d, uint64_t lba, uint32_t count, const uint8_t *buf) 72 { 73 int rc = disk_check(d); 74 if (rc != DISK_OK) 75 return rc; 76 if (!buf) 77 return DISK_EARG; 78 if ((rc = range_ok(d, lba, count)) != DISK_OK) 79 return rc; 80 while (count) { 81 uint32_t n = count < d->max_blocks_per_io ? count : d->max_blocks_per_io; 82 int brc = d->write(d->ctx, lba, n, buf); 83 if (brc != 0) 84 return backend_rc(brc); 85 lba += n; 86 count -= n; 87 buf += (size_t)n * d->block_size; 88 } 89 return DISK_OK; 90 } 91 92 int disk_flush(const disk_dev *d) 93 { 94 int rc = disk_check(d); 95 if (rc != DISK_OK) 96 return rc; 97 rc = d->flush(d->ctx); 98 if (rc != 0) 99 return backend_rc(rc); 100 return DISK_OK; 101 } 102 103 void disk_queue_init(disk_queue *q, const disk_dev *dev) 104 { 105 q->dev = dev; 106 q->head = 0; 107 q->len = 0; 108 q->failed = 0; 109 } 110 111 int disk_queue_push(disk_queue *q, disk_op op, uint64_t lba, uint32_t count, uint8_t *buf) 112 { 113 if (!q || !q->dev) 114 return DISK_EARG; 115 if (q->failed) 116 return DISK_ESTATE; 117 if (op != DISK_OP_READ && op != DISK_OP_WRITE && op != DISK_OP_FLUSH) 118 return DISK_EARG; 119 if (q->len >= DISK_QUEUE_DEPTH) return DISK_EFULL; /* GUARD:queue-bound */ 120 if (op != DISK_OP_FLUSH) { 121 int rc; 122 if (!buf) 123 return DISK_EARG; 124 if ((rc = range_ok(q->dev, lba, count)) != DISK_OK) 125 return rc; 126 } 127 disk_req *r = &q->req[(q->head + q->len) % DISK_QUEUE_DEPTH]; 128 r->op = op; 129 r->lba = lba; 130 r->count = count; 131 r->buf = buf; 132 r->status = 1; /* pending */ 133 q->len++; 134 return DISK_OK; 135 } 136 137 int disk_queue_drain(disk_queue *q) 138 { 139 if (!q || !q->dev) 140 return DISK_EARG; 141 if (q->failed) 142 return DISK_ESTATE; 143 while (q->len) { 144 disk_req *r = &q->req[q->head]; 145 int rc; 146 if (r->op == DISK_OP_READ) 147 rc = disk_read(q->dev, r->lba, r->count, r->buf); 148 else if (r->op == DISK_OP_WRITE) 149 rc = disk_write(q->dev, r->lba, r->count, r->buf); 150 else 151 rc = disk_flush(q->dev); 152 r->status = rc; 153 q->head = (q->head + 1) % DISK_QUEUE_DEPTH; 154 q->len--; 155 if (rc != DISK_OK) 156 q->failed = 1; /* GUARD:queue-failstop */ 157 if (rc != DISK_OK) 158 return rc; 159 } 160 return DISK_OK; 161 } 162 163 void disk_queue_reset(disk_queue *q) 164 { 165 if (!q) 166 return; 167 q->head = 0; 168 q->len = 0; 169 q->failed = 0; 170 }