paul@112 | 1 | /* |
paul@112 | 2 | * blockserver_io.c --- L4Re block server I/O manager. |
paul@112 | 3 | * |
paul@507 | 4 | * Copyright (C) 2019, 2020, 2021, 2022, 2023 Paul Boddie <paul@boddie.org.uk> |
paul@112 | 5 | * |
paul@112 | 6 | * This library is free software; you can redistribute it and/or |
paul@112 | 7 | * modify it under the terms of the GNU Library General Public |
paul@112 | 8 | * License as published by the Free Software Foundation; either |
paul@112 | 9 | * version 2 of the License, or (at your option) any later version. |
paul@112 | 10 | * |
paul@112 | 11 | * This library is distributed in the hope that it will be useful, |
paul@112 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@112 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
paul@112 | 14 | * Library General Public License for more details. |
paul@112 | 15 | * |
paul@112 | 16 | * You should have received a copy of the GNU Library General Public |
paul@112 | 17 | * License along with this library; if not, write to the Free Software |
paul@112 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
paul@112 | 19 | * 02110-1301 USA |
paul@112 | 20 | */ |
paul@112 | 21 | |
paul@144 | 22 | #include <l4/re/env.h> |
paul@144 | 23 | |
paul@112 | 24 | #include <stdio.h> |
paul@112 | 25 | #include <stdlib.h> |
paul@112 | 26 | |
paul@112 | 27 | #include <ext2fs/ext2fs.h> |
paul@112 | 28 | #include <fsclient/client.h> |
paul@507 | 29 | #include <systypes/env.h> |
paul@112 | 30 | |
paul@112 | 31 | |
paul@112 | 32 | |
paul@112 | 33 | /* Specific data for interacting with block devices. */ |
paul@112 | 34 | |
paul@112 | 35 | struct blockserver_private_data |
paul@112 | 36 | { |
paul@112 | 37 | int magic; |
paul@112 | 38 | int flags; |
paul@112 | 39 | file_t *file; |
paul@112 | 40 | }; |
paul@112 | 41 | |
paul@112 | 42 | io_manager blockserver_io_manager; /* defined below */ |
paul@112 | 43 | |
paul@507 | 44 | const char *blockserver_default_cap = ENV_FILESYSTEM_SERVER_NAME; |
paul@112 | 45 | |
paul@112 | 46 | |
paul@112 | 47 | |
paul@112 | 48 | /* Allocate and initialise a channel for the indicated block device. */ |
paul@112 | 49 | |
paul@112 | 50 | static errcode_t blockserver_open_channel(const char *name, |
paul@112 | 51 | file_t *file, int flags, |
paul@112 | 52 | io_channel *channel, |
paul@112 | 53 | io_manager io_mgr) |
paul@112 | 54 | { |
paul@112 | 55 | io_channel io = NULL; |
paul@112 | 56 | struct blockserver_private_data *data = NULL; |
paul@112 | 57 | errcode_t retval; |
paul@112 | 58 | |
paul@112 | 59 | /* Allocate memory for the channel. */ |
paul@112 | 60 | |
paul@112 | 61 | retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io); |
paul@112 | 62 | if (retval) |
paul@112 | 63 | goto free_io; |
paul@112 | 64 | |
paul@112 | 65 | /* Initialise the channel. */ |
paul@112 | 66 | |
paul@112 | 67 | memset(io, 0, sizeof(struct struct_io_channel)); |
paul@112 | 68 | |
paul@112 | 69 | io->magic = EXT2_ET_MAGIC_IO_CHANNEL; |
paul@112 | 70 | io->manager = io_mgr; |
paul@112 | 71 | io->block_size = 1024; |
paul@112 | 72 | io->read_error = 0; |
paul@112 | 73 | io->write_error = 0; |
paul@112 | 74 | io->refcount = 1; |
paul@112 | 75 | |
paul@112 | 76 | /* Allocate memory for the name and store it. */ |
paul@112 | 77 | |
paul@112 | 78 | retval = ext2fs_get_mem(strlen(name)+1, &io->name); |
paul@112 | 79 | if (retval) |
paul@112 | 80 | goto free_io; |
paul@112 | 81 | |
paul@112 | 82 | strcpy(io->name, name); |
paul@112 | 83 | |
paul@112 | 84 | /* Allocate memory for the private data. */ |
paul@112 | 85 | |
paul@112 | 86 | retval = ext2fs_get_mem(sizeof(struct blockserver_private_data), &data); |
paul@112 | 87 | if (retval) |
paul@112 | 88 | goto free_data; |
paul@112 | 89 | |
paul@112 | 90 | /* Initialise the private data. */ |
paul@112 | 91 | |
paul@112 | 92 | io->private_data = data; |
paul@112 | 93 | |
paul@112 | 94 | memset(data, 0, sizeof(struct blockserver_private_data)); |
paul@112 | 95 | |
paul@112 | 96 | data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL; |
paul@112 | 97 | data->flags = flags; |
paul@112 | 98 | data->file = file; |
paul@112 | 99 | |
paul@112 | 100 | /* Return the allocated channel. */ |
paul@112 | 101 | |
paul@112 | 102 | *channel = io; |
paul@112 | 103 | return 0; |
paul@112 | 104 | |
paul@112 | 105 | /* Deallocation upon failure. */ |
paul@112 | 106 | |
paul@112 | 107 | free_data: |
paul@112 | 108 | if (data) |
paul@112 | 109 | ext2fs_free_mem(&data); |
paul@112 | 110 | |
paul@112 | 111 | free_io: |
paul@112 | 112 | if (io) |
paul@112 | 113 | { |
paul@112 | 114 | if (io->name) |
paul@112 | 115 | ext2fs_free_mem(&io->name); |
paul@112 | 116 | ext2fs_free_mem(&io); |
paul@112 | 117 | } |
paul@112 | 118 | |
paul@112 | 119 | return retval; |
paul@112 | 120 | } |
paul@112 | 121 | |
paul@112 | 122 | |
paul@112 | 123 | |
paul@112 | 124 | /* Open a block device using the named capability. */ |
paul@112 | 125 | |
paul@112 | 126 | static errcode_t blockserver_open(const char *name, int flags, io_channel *channel) |
paul@112 | 127 | { |
paul@144 | 128 | l4_cap_idx_t server = l4re_env_get_cap(blockserver_default_cap); |
paul@144 | 129 | |
paul@112 | 130 | /* Open a device via the named capability. */ |
paul@112 | 131 | |
paul@144 | 132 | file_t *file = client_open_using(name, flags, server); |
paul@112 | 133 | |
paul@112 | 134 | /* NOTE: May want a more appropriate error code. */ |
paul@112 | 135 | |
paul@552 | 136 | if (!client_opened(file)) |
paul@112 | 137 | return EXT2_ET_BAD_DEVICE_NAME; |
paul@112 | 138 | |
paul@112 | 139 | return blockserver_open_channel(name, file, flags, channel, blockserver_io_manager); |
paul@112 | 140 | } |
paul@112 | 141 | |
paul@112 | 142 | |
paul@112 | 143 | |
paul@112 | 144 | /* Close the block device associated with the given channel. */ |
paul@112 | 145 | |
paul@112 | 146 | static errcode_t blockserver_close(io_channel channel) |
paul@112 | 147 | { |
paul@112 | 148 | struct blockserver_private_data *data; |
paul@112 | 149 | |
paul@112 | 150 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 151 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 152 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 153 | |
paul@112 | 154 | client_close(data->file); |
paul@112 | 155 | return 0; |
paul@112 | 156 | } |
paul@112 | 157 | |
paul@112 | 158 | /* Update the block size in use. */ |
paul@112 | 159 | |
paul@112 | 160 | static errcode_t blockserver_set_blksize(io_channel channel, int blksize) |
paul@112 | 161 | { |
paul@112 | 162 | struct blockserver_private_data *data; |
paul@112 | 163 | |
paul@112 | 164 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 165 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 166 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 167 | |
paul@112 | 168 | if (channel->block_size != blksize) |
paul@112 | 169 | channel->block_size = blksize; |
paul@112 | 170 | |
paul@112 | 171 | return 0; |
paul@112 | 172 | } |
paul@112 | 173 | |
paul@112 | 174 | /* Flush unwritten data. Without a cache, this does nothing. */ |
paul@112 | 175 | |
paul@112 | 176 | static errcode_t blockserver_flush(io_channel channel) |
paul@112 | 177 | { |
paul@173 | 178 | (void) channel; |
paul@112 | 179 | return 0; |
paul@112 | 180 | } |
paul@112 | 181 | |
paul@112 | 182 | static errcode_t blockserver_set_option(io_channel channel, const char *option, const char *arg) |
paul@112 | 183 | { |
paul@173 | 184 | (void) channel; (void) option; (void) arg; |
paul@112 | 185 | return 0; |
paul@112 | 186 | } |
paul@112 | 187 | |
paul@112 | 188 | static errcode_t blockserver_get_stats(io_channel channel, io_stats *stats) |
paul@112 | 189 | { |
paul@173 | 190 | (void) channel; (void) stats; |
paul@112 | 191 | return 0; |
paul@112 | 192 | } |
paul@112 | 193 | |
paul@112 | 194 | static errcode_t blockserver_read_blk64(io_channel channel, unsigned long long block, int count, void *buf) |
paul@112 | 195 | { |
paul@112 | 196 | /* Negative count values indicate a precise amount to read. */ |
paul@112 | 197 | |
paul@112 | 198 | unsigned long long to_read = count < 0 ? -count : count * channel->block_size; |
paul@435 | 199 | offset_t read; |
paul@112 | 200 | struct blockserver_private_data *data; |
paul@112 | 201 | errcode_t retval = 0; |
paul@112 | 202 | |
paul@112 | 203 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 204 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 205 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 206 | |
paul@112 | 207 | /* Perform the read at the given location. */ |
paul@112 | 208 | |
paul@436 | 209 | if (client_seek(data->file, block * channel->block_size, SEEK_SET) != block * channel->block_size) |
paul@436 | 210 | { |
paul@436 | 211 | read = 0; |
paul@436 | 212 | retval = EXT2_ET_LLSEEK_FAILED; |
paul@436 | 213 | } |
paul@436 | 214 | else |
paul@436 | 215 | { |
paul@436 | 216 | read = client_read(data->file, buf, to_read); |
paul@112 | 217 | |
paul@436 | 218 | /* Check for a short read. */ |
paul@112 | 219 | |
paul@436 | 220 | if (read < to_read) |
paul@436 | 221 | retval = EXT2_ET_SHORT_READ; |
paul@436 | 222 | } |
paul@112 | 223 | |
paul@112 | 224 | /* Handle read errors generally. */ |
paul@112 | 225 | |
paul@112 | 226 | if (retval) |
paul@112 | 227 | if (channel->read_error) |
paul@112 | 228 | retval = (channel->read_error)(channel, block, count, buf, |
paul@112 | 229 | to_read, read, retval); |
paul@112 | 230 | |
paul@112 | 231 | return retval; |
paul@112 | 232 | } |
paul@112 | 233 | |
paul@112 | 234 | static errcode_t blockserver_write_blk64(io_channel channel, unsigned long long block, int count, const void *buf) |
paul@112 | 235 | { |
paul@112 | 236 | /* Negative count values indicate a precise amount to write. */ |
paul@112 | 237 | |
paul@112 | 238 | unsigned long long to_write = count < 0 ? -count : count * channel->block_size; |
paul@435 | 239 | offset_t written; |
paul@112 | 240 | struct blockserver_private_data *data; |
paul@112 | 241 | errcode_t retval = 0; |
paul@112 | 242 | |
paul@112 | 243 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 244 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 245 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 246 | |
paul@112 | 247 | /* Perform the write at the given location. */ |
paul@112 | 248 | |
paul@436 | 249 | if (client_seek(data->file, block * channel->block_size, SEEK_SET) != block * channel->block_size) |
paul@436 | 250 | { |
paul@436 | 251 | retval = EXT2_ET_LLSEEK_FAILED; |
paul@436 | 252 | written = 0; |
paul@436 | 253 | } |
paul@436 | 254 | else |
paul@436 | 255 | { |
paul@436 | 256 | written = client_write(data->file, buf, to_write); |
paul@112 | 257 | |
paul@436 | 258 | /* Check for a short write. */ |
paul@112 | 259 | |
paul@436 | 260 | if (written < to_write) |
paul@436 | 261 | retval = EXT2_ET_SHORT_WRITE; |
paul@436 | 262 | } |
paul@112 | 263 | |
paul@112 | 264 | /* Handle write errors generally. */ |
paul@112 | 265 | |
paul@112 | 266 | if (retval) |
paul@112 | 267 | if (channel->write_error) |
paul@112 | 268 | retval = (channel->write_error)(channel, block, count, buf, |
paul@112 | 269 | to_write, written, retval); |
paul@112 | 270 | |
paul@112 | 271 | return retval; |
paul@112 | 272 | } |
paul@112 | 273 | |
paul@112 | 274 | static errcode_t blockserver_read_blk(io_channel channel, unsigned long block, int count, void *buf) |
paul@112 | 275 | { |
paul@112 | 276 | return blockserver_read_blk64(channel, block, count, buf); |
paul@112 | 277 | } |
paul@112 | 278 | |
paul@112 | 279 | static errcode_t blockserver_write_blk(io_channel channel, unsigned long block, int count, const void *buf) |
paul@112 | 280 | { |
paul@112 | 281 | return blockserver_write_blk64(channel, block, count, buf); |
paul@112 | 282 | } |
paul@112 | 283 | |
paul@112 | 284 | /* Unsupported operations are omitted: see io_manager.c for optional operation |
paul@112 | 285 | details. */ |
paul@112 | 286 | |
paul@112 | 287 | static struct struct_io_manager struct_blockserver_manager = { |
paul@112 | 288 | .magic = EXT2_ET_MAGIC_IO_MANAGER, |
paul@112 | 289 | .name = "L4Re blockserver I/O Manager", |
paul@112 | 290 | .open = blockserver_open, |
paul@112 | 291 | .close = blockserver_close, |
paul@112 | 292 | .set_blksize = blockserver_set_blksize, |
paul@112 | 293 | .read_blk = blockserver_read_blk, |
paul@112 | 294 | .write_blk = blockserver_write_blk, |
paul@112 | 295 | .flush = blockserver_flush, |
paul@112 | 296 | .write_byte = 0, |
paul@112 | 297 | .set_option = blockserver_set_option, |
paul@112 | 298 | .get_stats = blockserver_get_stats, |
paul@112 | 299 | .read_blk64 = blockserver_read_blk64, |
paul@112 | 300 | .write_blk64 = blockserver_write_blk64, |
paul@112 | 301 | .discard = 0, |
paul@112 | 302 | .cache_readahead = 0, |
paul@112 | 303 | .zeroout = 0, |
paul@112 | 304 | }; |
paul@112 | 305 | |
paul@112 | 306 | io_manager blockserver_io_manager = &struct_blockserver_manager; |