paul@112 | 1 | /* |
paul@112 | 2 | * blockserver_io.c --- L4Re block server I/O manager. |
paul@112 | 3 | * |
paul@112 | 4 | * Copyright (C) 2019, 2020, 2021 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@112 | 29 | |
paul@112 | 30 | |
paul@112 | 31 | |
paul@112 | 32 | /* Specific data for interacting with block devices. */ |
paul@112 | 33 | |
paul@112 | 34 | struct blockserver_private_data |
paul@112 | 35 | { |
paul@112 | 36 | int magic; |
paul@112 | 37 | int flags; |
paul@112 | 38 | file_t *file; |
paul@112 | 39 | }; |
paul@112 | 40 | |
paul@112 | 41 | io_manager blockserver_io_manager; /* defined below */ |
paul@112 | 42 | |
paul@112 | 43 | const char *blockserver_default_cap = "server"; |
paul@112 | 44 | |
paul@112 | 45 | |
paul@112 | 46 | |
paul@112 | 47 | /* Allocate and initialise a channel for the indicated block device. */ |
paul@112 | 48 | |
paul@112 | 49 | static errcode_t blockserver_open_channel(const char *name, |
paul@112 | 50 | file_t *file, int flags, |
paul@112 | 51 | io_channel *channel, |
paul@112 | 52 | io_manager io_mgr) |
paul@112 | 53 | { |
paul@112 | 54 | io_channel io = NULL; |
paul@112 | 55 | struct blockserver_private_data *data = NULL; |
paul@112 | 56 | errcode_t retval; |
paul@112 | 57 | |
paul@112 | 58 | /* Allocate memory for the channel. */ |
paul@112 | 59 | |
paul@112 | 60 | retval = ext2fs_get_mem(sizeof(struct struct_io_channel), &io); |
paul@112 | 61 | if (retval) |
paul@112 | 62 | goto free_io; |
paul@112 | 63 | |
paul@112 | 64 | /* Initialise the channel. */ |
paul@112 | 65 | |
paul@112 | 66 | memset(io, 0, sizeof(struct struct_io_channel)); |
paul@112 | 67 | |
paul@112 | 68 | io->magic = EXT2_ET_MAGIC_IO_CHANNEL; |
paul@112 | 69 | io->manager = io_mgr; |
paul@112 | 70 | io->block_size = 1024; |
paul@112 | 71 | io->read_error = 0; |
paul@112 | 72 | io->write_error = 0; |
paul@112 | 73 | io->refcount = 1; |
paul@112 | 74 | |
paul@112 | 75 | /* Allocate memory for the name and store it. */ |
paul@112 | 76 | |
paul@112 | 77 | retval = ext2fs_get_mem(strlen(name)+1, &io->name); |
paul@112 | 78 | if (retval) |
paul@112 | 79 | goto free_io; |
paul@112 | 80 | |
paul@112 | 81 | strcpy(io->name, name); |
paul@112 | 82 | |
paul@112 | 83 | /* Allocate memory for the private data. */ |
paul@112 | 84 | |
paul@112 | 85 | retval = ext2fs_get_mem(sizeof(struct blockserver_private_data), &data); |
paul@112 | 86 | if (retval) |
paul@112 | 87 | goto free_data; |
paul@112 | 88 | |
paul@112 | 89 | /* Initialise the private data. */ |
paul@112 | 90 | |
paul@112 | 91 | io->private_data = data; |
paul@112 | 92 | |
paul@112 | 93 | memset(data, 0, sizeof(struct blockserver_private_data)); |
paul@112 | 94 | |
paul@112 | 95 | data->magic = EXT2_ET_MAGIC_UNIX_IO_CHANNEL; |
paul@112 | 96 | data->flags = flags; |
paul@112 | 97 | data->file = file; |
paul@112 | 98 | |
paul@112 | 99 | /* Return the allocated channel. */ |
paul@112 | 100 | |
paul@112 | 101 | *channel = io; |
paul@112 | 102 | return 0; |
paul@112 | 103 | |
paul@112 | 104 | /* Deallocation upon failure. */ |
paul@112 | 105 | |
paul@112 | 106 | free_data: |
paul@112 | 107 | if (data) |
paul@112 | 108 | ext2fs_free_mem(&data); |
paul@112 | 109 | |
paul@112 | 110 | free_io: |
paul@112 | 111 | if (io) |
paul@112 | 112 | { |
paul@112 | 113 | if (io->name) |
paul@112 | 114 | ext2fs_free_mem(&io->name); |
paul@112 | 115 | ext2fs_free_mem(&io); |
paul@112 | 116 | } |
paul@112 | 117 | |
paul@112 | 118 | return retval; |
paul@112 | 119 | } |
paul@112 | 120 | |
paul@112 | 121 | |
paul@112 | 122 | |
paul@112 | 123 | /* Open a block device using the named capability. */ |
paul@112 | 124 | |
paul@112 | 125 | static errcode_t blockserver_open(const char *name, int flags, io_channel *channel) |
paul@112 | 126 | { |
paul@144 | 127 | l4_cap_idx_t server = l4re_env_get_cap(blockserver_default_cap); |
paul@144 | 128 | |
paul@112 | 129 | /* Open a device via the named capability. */ |
paul@112 | 130 | |
paul@144 | 131 | file_t *file = client_open_using(name, flags, server); |
paul@112 | 132 | |
paul@112 | 133 | /* NOTE: May want a more appropriate error code. */ |
paul@112 | 134 | |
paul@112 | 135 | if (file == NULL) |
paul@112 | 136 | return EXT2_ET_BAD_DEVICE_NAME; |
paul@112 | 137 | |
paul@112 | 138 | return blockserver_open_channel(name, file, flags, channel, blockserver_io_manager); |
paul@112 | 139 | } |
paul@112 | 140 | |
paul@112 | 141 | |
paul@112 | 142 | |
paul@112 | 143 | /* Close the block device associated with the given channel. */ |
paul@112 | 144 | |
paul@112 | 145 | static errcode_t blockserver_close(io_channel channel) |
paul@112 | 146 | { |
paul@112 | 147 | struct blockserver_private_data *data; |
paul@112 | 148 | |
paul@112 | 149 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 150 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 151 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 152 | |
paul@112 | 153 | client_close(data->file); |
paul@112 | 154 | return 0; |
paul@112 | 155 | } |
paul@112 | 156 | |
paul@112 | 157 | /* Update the block size in use. */ |
paul@112 | 158 | |
paul@112 | 159 | static errcode_t blockserver_set_blksize(io_channel channel, int blksize) |
paul@112 | 160 | { |
paul@112 | 161 | struct blockserver_private_data *data; |
paul@112 | 162 | |
paul@112 | 163 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 164 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 165 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 166 | |
paul@112 | 167 | if (channel->block_size != blksize) |
paul@112 | 168 | channel->block_size = blksize; |
paul@112 | 169 | |
paul@112 | 170 | return 0; |
paul@112 | 171 | } |
paul@112 | 172 | |
paul@112 | 173 | /* Flush unwritten data. Without a cache, this does nothing. */ |
paul@112 | 174 | |
paul@112 | 175 | static errcode_t blockserver_flush(io_channel channel) |
paul@112 | 176 | { |
paul@173 | 177 | (void) channel; |
paul@112 | 178 | return 0; |
paul@112 | 179 | } |
paul@112 | 180 | |
paul@112 | 181 | static errcode_t blockserver_set_option(io_channel channel, const char *option, const char *arg) |
paul@112 | 182 | { |
paul@173 | 183 | (void) channel; (void) option; (void) arg; |
paul@112 | 184 | return 0; |
paul@112 | 185 | } |
paul@112 | 186 | |
paul@112 | 187 | static errcode_t blockserver_get_stats(io_channel channel, io_stats *stats) |
paul@112 | 188 | { |
paul@173 | 189 | (void) channel; (void) stats; |
paul@112 | 190 | return 0; |
paul@112 | 191 | } |
paul@112 | 192 | |
paul@112 | 193 | static errcode_t blockserver_read_blk64(io_channel channel, unsigned long long block, int count, void *buf) |
paul@112 | 194 | { |
paul@112 | 195 | /* Negative count values indicate a precise amount to read. */ |
paul@112 | 196 | |
paul@112 | 197 | unsigned long long to_read = count < 0 ? -count : count * channel->block_size; |
paul@112 | 198 | ssize_t read; |
paul@112 | 199 | struct blockserver_private_data *data; |
paul@112 | 200 | errcode_t retval = 0; |
paul@112 | 201 | |
paul@112 | 202 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 203 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 204 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 205 | |
paul@112 | 206 | /* Perform the read at the given location. */ |
paul@112 | 207 | |
paul@112 | 208 | client_seek(data->file, block * channel->block_size, SEEK_SET); |
paul@112 | 209 | read = client_read(data->file, buf, to_read); |
paul@112 | 210 | |
paul@112 | 211 | /* Check for a short read. */ |
paul@112 | 212 | |
paul@112 | 213 | if ((read < 0) || ((size_t) read < to_read)) |
paul@112 | 214 | retval = EXT2_ET_SHORT_READ; |
paul@112 | 215 | |
paul@112 | 216 | /* Handle read errors generally. */ |
paul@112 | 217 | |
paul@112 | 218 | if (retval) |
paul@112 | 219 | if (channel->read_error) |
paul@112 | 220 | retval = (channel->read_error)(channel, block, count, buf, |
paul@112 | 221 | to_read, read, retval); |
paul@112 | 222 | |
paul@112 | 223 | return retval; |
paul@112 | 224 | } |
paul@112 | 225 | |
paul@112 | 226 | static errcode_t blockserver_write_blk64(io_channel channel, unsigned long long block, int count, const void *buf) |
paul@112 | 227 | { |
paul@112 | 228 | /* Negative count values indicate a precise amount to write. */ |
paul@112 | 229 | |
paul@112 | 230 | unsigned long long to_write = count < 0 ? -count : count * channel->block_size; |
paul@112 | 231 | ssize_t written; |
paul@112 | 232 | struct blockserver_private_data *data; |
paul@112 | 233 | errcode_t retval = 0; |
paul@112 | 234 | |
paul@112 | 235 | EXT2_CHECK_MAGIC(channel, EXT2_ET_MAGIC_IO_CHANNEL); |
paul@112 | 236 | data = (struct blockserver_private_data *) channel->private_data; |
paul@112 | 237 | EXT2_CHECK_MAGIC(data, EXT2_ET_MAGIC_UNIX_IO_CHANNEL); |
paul@112 | 238 | |
paul@112 | 239 | /* Perform the write at the given location. */ |
paul@112 | 240 | |
paul@112 | 241 | client_seek(data->file, block * channel->block_size, SEEK_SET); |
paul@112 | 242 | written = client_write(data->file, buf, to_write); |
paul@112 | 243 | |
paul@112 | 244 | /* Check for a short write. */ |
paul@112 | 245 | |
paul@112 | 246 | if ((written < 0) || ((size_t) written < to_write)) |
paul@112 | 247 | retval = EXT2_ET_SHORT_WRITE; |
paul@112 | 248 | |
paul@112 | 249 | /* Handle write errors generally. */ |
paul@112 | 250 | |
paul@112 | 251 | if (retval) |
paul@112 | 252 | if (channel->write_error) |
paul@112 | 253 | retval = (channel->write_error)(channel, block, count, buf, |
paul@112 | 254 | to_write, written, retval); |
paul@112 | 255 | |
paul@112 | 256 | return retval; |
paul@112 | 257 | } |
paul@112 | 258 | |
paul@112 | 259 | static errcode_t blockserver_read_blk(io_channel channel, unsigned long block, int count, void *buf) |
paul@112 | 260 | { |
paul@112 | 261 | return blockserver_read_blk64(channel, block, count, buf); |
paul@112 | 262 | } |
paul@112 | 263 | |
paul@112 | 264 | static errcode_t blockserver_write_blk(io_channel channel, unsigned long block, int count, const void *buf) |
paul@112 | 265 | { |
paul@112 | 266 | return blockserver_write_blk64(channel, block, count, buf); |
paul@112 | 267 | } |
paul@112 | 268 | |
paul@112 | 269 | /* Unsupported operations are omitted: see io_manager.c for optional operation |
paul@112 | 270 | details. */ |
paul@112 | 271 | |
paul@112 | 272 | static struct struct_io_manager struct_blockserver_manager = { |
paul@112 | 273 | .magic = EXT2_ET_MAGIC_IO_MANAGER, |
paul@112 | 274 | .name = "L4Re blockserver I/O Manager", |
paul@112 | 275 | .open = blockserver_open, |
paul@112 | 276 | .close = blockserver_close, |
paul@112 | 277 | .set_blksize = blockserver_set_blksize, |
paul@112 | 278 | .read_blk = blockserver_read_blk, |
paul@112 | 279 | .write_blk = blockserver_write_blk, |
paul@112 | 280 | .flush = blockserver_flush, |
paul@112 | 281 | .write_byte = 0, |
paul@112 | 282 | .set_option = blockserver_set_option, |
paul@112 | 283 | .get_stats = blockserver_get_stats, |
paul@112 | 284 | .read_blk64 = blockserver_read_blk64, |
paul@112 | 285 | .write_blk64 = blockserver_write_blk64, |
paul@112 | 286 | .discard = 0, |
paul@112 | 287 | .cache_readahead = 0, |
paul@112 | 288 | .zeroout = 0, |
paul@112 | 289 | }; |
paul@112 | 290 | |
paul@112 | 291 | io_manager blockserver_io_manager = &struct_blockserver_manager; |