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