[1cbed6b] | 1 | /*
|
---|
[4c6fd56] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[1cbed6b] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[c8ea6eca] | 29 | /** @addtogroup file_bd
|
---|
[1cbed6b] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief File-backed block device driver
|
---|
| 36 | *
|
---|
| 37 | * Allows accessing a file as a block device. Useful for, e.g., mounting
|
---|
| 38 | * a disk image.
|
---|
| 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <stdio.h>
|
---|
| 42 | #include <async.h>
|
---|
| 43 | #include <as.h>
|
---|
[4802dd7] | 44 | #include <bd_srv.h>
|
---|
[1e4cada] | 45 | #include <fibril_synch.h>
|
---|
[15f3c3f] | 46 | #include <loc.h>
|
---|
[8d2dd7f2] | 47 | #include <stddef.h>
|
---|
| 48 | #include <stdint.h>
|
---|
[1cbed6b] | 49 | #include <errno.h>
|
---|
[c1694b6b] | 50 | #include <str_error.h>
|
---|
[3e6a98c5] | 51 | #include <stdbool.h>
|
---|
[95bc57c] | 52 | #include <task.h>
|
---|
[1ee00b7] | 53 | #include <macros.h>
|
---|
[1d6dd2a] | 54 | #include <str.h>
|
---|
[1cbed6b] | 55 |
|
---|
| 56 | #define NAME "file_bd"
|
---|
| 57 |
|
---|
[45df59a] | 58 | #define DEFAULT_BLOCK_SIZE 512
|
---|
| 59 |
|
---|
| 60 | static size_t block_size;
|
---|
[ed903174] | 61 | static aoff64_t num_blocks;
|
---|
[1cbed6b] | 62 | static FILE *img;
|
---|
[4c6fd56] | 63 | static loc_srv_t *srv;
|
---|
[1cbed6b] | 64 |
|
---|
[15f3c3f] | 65 | static service_id_t service_id;
|
---|
[135486d] | 66 | static bd_srvs_t bd_srvs;
|
---|
[12956e57] | 67 | static fibril_mutex_t dev_lock;
|
---|
[1cbed6b] | 68 |
|
---|
[45df59a] | 69 | static void print_usage(void);
|
---|
[b7fd2a0] | 70 | static errno_t file_bd_init(const char *fname);
|
---|
[984a9ba] | 71 | static void file_bd_connection(ipc_call_t *icall, void *);
|
---|
[4802dd7] | 72 |
|
---|
[b7fd2a0] | 73 | static errno_t file_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
| 74 | static errno_t file_bd_close(bd_srv_t *);
|
---|
| 75 | static errno_t file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
|
---|
| 76 | static errno_t file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
|
---|
| 77 | static errno_t file_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 78 | static errno_t file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
[4802dd7] | 79 |
|
---|
| 80 | static bd_ops_t file_bd_ops = {
|
---|
| 81 | .open = file_bd_open,
|
---|
| 82 | .close = file_bd_close,
|
---|
| 83 | .read_blocks = file_bd_read_blocks,
|
---|
| 84 | .write_blocks = file_bd_write_blocks,
|
---|
| 85 | .get_block_size = file_bd_get_block_size,
|
---|
| 86 | .get_num_blocks = file_bd_get_num_blocks
|
---|
| 87 | };
|
---|
[1cbed6b] | 88 |
|
---|
| 89 | int main(int argc, char **argv)
|
---|
| 90 | {
|
---|
[b7fd2a0] | 91 | errno_t rc;
|
---|
[45df59a] | 92 | char *image_name;
|
---|
| 93 | char *device_name;
|
---|
[f97f5cc2] | 94 | category_id_t disk_cat;
|
---|
[1cbed6b] | 95 |
|
---|
| 96 | printf(NAME ": File-backed block device driver\n");
|
---|
| 97 |
|
---|
[45df59a] | 98 | block_size = DEFAULT_BLOCK_SIZE;
|
---|
| 99 |
|
---|
[1433ecda] | 100 | ++argv;
|
---|
| 101 | --argc;
|
---|
[45df59a] | 102 | while (*argv != NULL && (*argv)[0] == '-') {
|
---|
| 103 | /* Option */
|
---|
| 104 | if (str_cmp(*argv, "-b") == 0) {
|
---|
| 105 | if (argc < 2) {
|
---|
| 106 | printf("Argument missing.\n");
|
---|
| 107 | print_usage();
|
---|
| 108 | return -1;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | rc = str_size_t(argv[1], NULL, 10, true, &block_size);
|
---|
| 112 | if (rc != EOK || block_size == 0) {
|
---|
| 113 | printf("Invalid block size '%s'.\n", argv[1]);
|
---|
| 114 | print_usage();
|
---|
| 115 | return -1;
|
---|
| 116 | }
|
---|
[1433ecda] | 117 | ++argv;
|
---|
| 118 | --argc;
|
---|
[45df59a] | 119 | } else {
|
---|
| 120 | printf("Invalid option '%s'.\n", *argv);
|
---|
| 121 | print_usage();
|
---|
| 122 | return -1;
|
---|
| 123 | }
|
---|
[1433ecda] | 124 | ++argv;
|
---|
| 125 | --argc;
|
---|
[45df59a] | 126 | }
|
---|
| 127 |
|
---|
| 128 | if (argc < 2) {
|
---|
| 129 | printf("Missing arguments.\n");
|
---|
| 130 | print_usage();
|
---|
[1cbed6b] | 131 | return -1;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
[45df59a] | 134 | image_name = argv[0];
|
---|
| 135 | device_name = argv[1];
|
---|
| 136 |
|
---|
| 137 | if (file_bd_init(image_name) != EOK)
|
---|
[1cbed6b] | 138 | return -1;
|
---|
| 139 |
|
---|
[4c6fd56] | 140 | rc = loc_service_register(srv, device_name, &service_id);
|
---|
[1cbed6b] | 141 | if (rc != EOK) {
|
---|
[c1694b6b] | 142 | printf("%s: Unable to register device '%s': %s.\n",
|
---|
| 143 | NAME, device_name, str_error(rc));
|
---|
[1cbed6b] | 144 | return rc;
|
---|
| 145 | }
|
---|
[f97f5cc2] | 146 |
|
---|
| 147 | rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING);
|
---|
| 148 | if (rc != EOK) {
|
---|
[c1694b6b] | 149 | printf("%s: Failed resolving category 'disk': %s\n", NAME, str_error(rc));
|
---|
[f97f5cc2] | 150 | return rc;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[4c6fd56] | 153 | rc = loc_service_add_to_cat(srv, service_id, disk_cat);
|
---|
[f97f5cc2] | 154 | if (rc != EOK) {
|
---|
[c1694b6b] | 155 | printf("%s: Failed adding %s to category: %s",
|
---|
| 156 | NAME, device_name, str_error(rc));
|
---|
[f97f5cc2] | 157 | return rc;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[b16e77d] | 160 | printf("%s: Accepting connections\n", NAME);
|
---|
[95bc57c] | 161 | task_retval(0);
|
---|
[1cbed6b] | 162 | async_manager();
|
---|
[a35b458] | 163 |
|
---|
[1cbed6b] | 164 | /* Not reached */
|
---|
| 165 | return 0;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[45df59a] | 168 | static void print_usage(void)
|
---|
| 169 | {
|
---|
| 170 | printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[b7fd2a0] | 173 | static errno_t file_bd_init(const char *fname)
|
---|
[1cbed6b] | 174 | {
|
---|
[135486d] | 175 | bd_srvs_init(&bd_srvs);
|
---|
| 176 | bd_srvs.ops = &file_bd_ops;
|
---|
[a35b458] | 177 |
|
---|
[b688fd8] | 178 | async_set_fallback_port_handler(file_bd_connection, NULL);
|
---|
[4c6fd56] | 179 | errno_t rc = loc_server_register(NAME, &srv);
|
---|
[b16e77d] | 180 | if (rc != EOK) {
|
---|
| 181 | printf("%s: Unable to register driver.\n", NAME);
|
---|
[1cbed6b] | 182 | return rc;
|
---|
| 183 | }
|
---|
[a35b458] | 184 |
|
---|
[1cbed6b] | 185 | img = fopen(fname, "rb+");
|
---|
[4c6fd56] | 186 | if (img == NULL) {
|
---|
| 187 | rc = EINVAL;
|
---|
| 188 | goto error;
|
---|
| 189 | }
|
---|
[a35b458] | 190 |
|
---|
[08232ee] | 191 | if (fseek(img, 0, SEEK_END) != 0) {
|
---|
[4c6fd56] | 192 | rc = EIO;
|
---|
| 193 | goto error;
|
---|
[08232ee] | 194 | }
|
---|
[a35b458] | 195 |
|
---|
[b16e77d] | 196 | off64_t img_size = ftell(img);
|
---|
[08232ee] | 197 | if (img_size < 0) {
|
---|
[4c6fd56] | 198 | rc = EIO;
|
---|
| 199 | goto error;
|
---|
[08232ee] | 200 | }
|
---|
[a35b458] | 201 |
|
---|
[08232ee] | 202 | num_blocks = img_size / block_size;
|
---|
[a35b458] | 203 |
|
---|
[12956e57] | 204 | fibril_mutex_initialize(&dev_lock);
|
---|
[a35b458] | 205 |
|
---|
[1cbed6b] | 206 | return EOK;
|
---|
[4c6fd56] | 207 | error:
|
---|
| 208 | if (img != NULL) {
|
---|
| 209 | fclose(img);
|
---|
| 210 | img = NULL;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if (srv != NULL) {
|
---|
| 214 | loc_server_unregister(srv);
|
---|
| 215 | srv = NULL;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | return rc;
|
---|
[1cbed6b] | 219 | }
|
---|
| 220 |
|
---|
[984a9ba] | 221 | static void file_bd_connection(ipc_call_t *icall, void *arg)
|
---|
[1cbed6b] | 222 | {
|
---|
[984a9ba] | 223 | bd_conn(icall, &bd_srvs);
|
---|
[4802dd7] | 224 | }
|
---|
[1cbed6b] | 225 |
|
---|
[4802dd7] | 226 | /** Open device. */
|
---|
[b7fd2a0] | 227 | static errno_t file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
[4802dd7] | 228 | {
|
---|
| 229 | return EOK;
|
---|
| 230 | }
|
---|
[1cbed6b] | 231 |
|
---|
[4802dd7] | 232 | /** Close device. */
|
---|
[b7fd2a0] | 233 | static errno_t file_bd_close(bd_srv_t *bd)
|
---|
[4802dd7] | 234 | {
|
---|
| 235 | return EOK;
|
---|
[1cbed6b] | 236 | }
|
---|
| 237 |
|
---|
[1ee00b7] | 238 | /** Read blocks from the device. */
|
---|
[b7fd2a0] | 239 | static errno_t file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
|
---|
[4802dd7] | 240 | size_t size)
|
---|
[1cbed6b] | 241 | {
|
---|
| 242 | size_t n_rd;
|
---|
| 243 |
|
---|
[4802dd7] | 244 | if (size < cnt * block_size)
|
---|
| 245 | return EINVAL;
|
---|
| 246 |
|
---|
[fb150d78] | 247 | /* Check whether access is within device address bounds. */
|
---|
| 248 | if (ba + cnt > num_blocks) {
|
---|
[ed903174] | 249 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
---|
| 250 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
---|
[fb150d78] | 251 | num_blocks - 1);
|
---|
| 252 | return ELIMIT;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[12956e57] | 255 | fibril_mutex_lock(&dev_lock);
|
---|
[1cbed6b] | 256 |
|
---|
[c77a64f] | 257 | clearerr(img);
|
---|
[d5c1051] | 258 | if (fseek(img, ba * block_size, SEEK_SET) < 0) {
|
---|
[6c01702] | 259 | fibril_mutex_unlock(&dev_lock);
|
---|
| 260 | return EIO;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[1ee00b7] | 263 | n_rd = fread(buf, block_size, cnt, img);
|
---|
[1cbed6b] | 264 |
|
---|
| 265 | if (ferror(img)) {
|
---|
[12956e57] | 266 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 267 | return EIO; /* Read error */
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[12956e57] | 270 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 271 |
|
---|
[1ee00b7] | 272 | if (n_rd < cnt)
|
---|
| 273 | return EINVAL; /* Read beyond end of device */
|
---|
[1cbed6b] | 274 |
|
---|
| 275 | return EOK;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[1ee00b7] | 278 | /** Write blocks to the device. */
|
---|
[b7fd2a0] | 279 | static errno_t file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
|
---|
[4802dd7] | 280 | const void *buf, size_t size)
|
---|
[1cbed6b] | 281 | {
|
---|
| 282 | size_t n_wr;
|
---|
| 283 |
|
---|
[4802dd7] | 284 | if (size < cnt * block_size)
|
---|
| 285 | return EINVAL;
|
---|
| 286 |
|
---|
[fb150d78] | 287 | /* Check whether access is within device address bounds. */
|
---|
| 288 | if (ba + cnt > num_blocks) {
|
---|
[ed903174] | 289 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
---|
| 290 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
---|
[fb150d78] | 291 | num_blocks - 1);
|
---|
| 292 | return ELIMIT;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[12956e57] | 295 | fibril_mutex_lock(&dev_lock);
|
---|
[1cbed6b] | 296 |
|
---|
[c77a64f] | 297 | clearerr(img);
|
---|
[d5c1051] | 298 | if (fseek(img, ba * block_size, SEEK_SET) < 0) {
|
---|
[6c01702] | 299 | fibril_mutex_unlock(&dev_lock);
|
---|
| 300 | return EIO;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[dccf721] | 303 | n_wr = fwrite(buf, block_size, cnt, img);
|
---|
[1cbed6b] | 304 |
|
---|
[1ee00b7] | 305 | if (ferror(img) || n_wr < cnt) {
|
---|
[12956e57] | 306 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 307 | return EIO; /* Write error */
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[6c01702] | 310 | if (fflush(img) != 0) {
|
---|
| 311 | fibril_mutex_unlock(&dev_lock);
|
---|
| 312 | return EIO;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[12956e57] | 315 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 316 |
|
---|
| 317 | return EOK;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
[4802dd7] | 320 | /** Get device block size. */
|
---|
[b7fd2a0] | 321 | static errno_t file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
[4802dd7] | 322 | {
|
---|
| 323 | *rsize = block_size;
|
---|
| 324 | return EOK;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /** Get number of blocks on device. */
|
---|
[b7fd2a0] | 328 | static errno_t file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
[4802dd7] | 329 | {
|
---|
| 330 | *rnb = num_blocks;
|
---|
| 331 | return EOK;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
[1cbed6b] | 334 | /**
|
---|
| 335 | * @}
|
---|
| 336 | */
|
---|