interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / disk / disk_file.c

152 lines · 4227 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_file.c -- hosted file-backed backend with power-cut injection
19 * (see disk_file.h). Test-only; uses libc.
20 */
21#define _GNU_SOURCE
22#include "disk_file.h"
23
24#include <errno.h>
25#include <fcntl.h>
26#include <string.h>
27#include <sys/stat.h>
28#include <unistd.h>
29
30static int full_pread(int fd, uint8_t *buf, size_t len, off_t off)
31{
32 while (len) {
33 ssize_t n = pread(fd, buf, len, off);
34 if (n < 0 && errno == EINTR)
35 continue;
36 if (n <= 0)
37 return -1;
38 buf += n;
39 len -= (size_t)n;
40 off += n;
41 }
42 return 0;
43}
44
45static int full_pwrite(int fd, const uint8_t *buf, size_t len, off_t off)
46{
47 while (len) {
48 ssize_t n = pwrite(fd, buf, len, off);
49 if (n < 0 && errno == EINTR)
50 continue;
51 if (n <= 0)
52 return -1;
53 buf += n;
54 len -= (size_t)n;
55 off += n;
56 }
57 return 0;
58}
59
60static int f_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *buf)
61{
62 disk_file *f = ctx;
63 return full_pread(f->fd, buf, (size_t)count * f->block_size, (off_t)(lba * f->block_size))
64 ? DISK_EIO : DISK_OK;
65}
66
67static int f_write(void *ctx, uint64_t lba, uint32_t count, const uint8_t *buf)
68{
69 disk_file *f = ctx;
70 for (uint32_t i = 0; i < count; i++) {
71 if (f->cut_armed) {
72 if (f->cut_remaining == 0)
73 return DISK_EPOWER;
74 f->cut_remaining--;
75 }
76 if (full_pwrite(f->fd, buf + (size_t)i * f->block_size, f->block_size,
77 (off_t)((lba + i) * f->block_size)))
78 return DISK_EIO;
79 f->blocks_written++;
80 }
81 return DISK_OK;
82}
83
84static int f_flush(void *ctx)
85{
86 disk_file *f = ctx;
87 if (f->cut_armed && f->cut_remaining == 0)
88 return DISK_EPOWER;
89 f->flushes++;
90 return fdatasync(f->fd) ? DISK_EIO : DISK_OK;
91}
92
93int disk_file_open(disk_file *f, disk_dev *dev, const char *path, uint32_t block_size,
94 uint64_t block_count, int create)
95{
96 if (!f || !dev || !path)
97 return DISK_EARG;
98 if (block_size != 512u && block_size != 4096u)
99 return DISK_EGEOMETRY;
100 memset(f, 0, sizeof *f);
101 f->fd = open(path, create ? (O_RDWR | O_CREAT | O_TRUNC) : O_RDWR, 0600);
102 if (f->fd < 0)
103 return DISK_EIO;
104 if (create) {
105 if (block_count == 0 || block_count > (uint64_t)INT64_MAX / block_size || ftruncate(f->fd, (off_t)(block_count * block_size))) {
106 close(f->fd);
107 return DISK_EIO;
108 }
109 } else {
110 struct stat st;
111 if (fstat(f->fd, &st) || st.st_size <= 0 || (uint64_t)st.st_size % block_size) {
112 close(f->fd);
113 return DISK_EGEOMETRY;
114 }
115 if (block_count == 0)
116 block_count = (uint64_t)st.st_size / block_size;
117 else if (block_count != (uint64_t)st.st_size / block_size) {
118 close(f->fd);
119 return DISK_EGEOMETRY;
120 }
121 }
122 f->block_size = block_size;
123 f->block_count = block_count;
124 dev->ctx = f;
125 dev->block_size = block_size;
126 dev->block_count = block_count;
127 dev->read = f_read;
128 dev->write = f_write;
129 dev->flush = f_flush;
130 dev->max_blocks_per_io = 256;
131 return DISK_OK;
132}
133
134void disk_file_close(disk_file *f)
135{
136 if (f && f->fd >= 0) {
137 close(f->fd);
138 f->fd = -1;
139 }
140}
141
142void disk_file_arm_cut(disk_file *f, uint64_t blocks_allowed)
143{
144 f->cut_armed = 1;
145 f->cut_remaining = blocks_allowed;
146}
147
148void disk_file_disarm(disk_file *f)
149{
150 f->cut_armed = 0;
151 f->cut_remaining = 0;
152}