[1cbed6b] | 1 | /*
|
---|
[ca48672] | 2 | * Copyright (c) 2025 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 |
|
---|
[ca48672] | 140 | rc = loc_service_register(srv, device_name, fallback_port_id,
|
---|
| 141 | &service_id);
|
---|
[1cbed6b] | 142 | if (rc != EOK) {
|
---|
[c1694b6b] | 143 | printf("%s: Unable to register device '%s': %s.\n",
|
---|
| 144 | NAME, device_name, str_error(rc));
|
---|
[1cbed6b] | 145 | return rc;
|
---|
| 146 | }
|
---|
[f97f5cc2] | 147 |
|
---|
| 148 | rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING);
|
---|
| 149 | if (rc != EOK) {
|
---|
[c1694b6b] | 150 | printf("%s: Failed resolving category 'disk': %s\n", NAME, str_error(rc));
|
---|
[f97f5cc2] | 151 | return rc;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[4c6fd56] | 154 | rc = loc_service_add_to_cat(srv, service_id, disk_cat);
|
---|
[f97f5cc2] | 155 | if (rc != EOK) {
|
---|
[c1694b6b] | 156 | printf("%s: Failed adding %s to category: %s",
|
---|
| 157 | NAME, device_name, str_error(rc));
|
---|
[f97f5cc2] | 158 | return rc;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[b16e77d] | 161 | printf("%s: Accepting connections\n", NAME);
|
---|
[95bc57c] | 162 | task_retval(0);
|
---|
[1cbed6b] | 163 | async_manager();
|
---|
[a35b458] | 164 |
|
---|
[1cbed6b] | 165 | /* Not reached */
|
---|
| 166 | return 0;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[45df59a] | 169 | static void print_usage(void)
|
---|
| 170 | {
|
---|
| 171 | printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[b7fd2a0] | 174 | static errno_t file_bd_init(const char *fname)
|
---|
[1cbed6b] | 175 | {
|
---|
[135486d] | 176 | bd_srvs_init(&bd_srvs);
|
---|
| 177 | bd_srvs.ops = &file_bd_ops;
|
---|
[a35b458] | 178 |
|
---|
[b688fd8] | 179 | async_set_fallback_port_handler(file_bd_connection, NULL);
|
---|
[4c6fd56] | 180 | errno_t rc = loc_server_register(NAME, &srv);
|
---|
[b16e77d] | 181 | if (rc != EOK) {
|
---|
| 182 | printf("%s: Unable to register driver.\n", NAME);
|
---|
[1cbed6b] | 183 | return rc;
|
---|
| 184 | }
|
---|
[a35b458] | 185 |
|
---|
[1cbed6b] | 186 | img = fopen(fname, "rb+");
|
---|
[4c6fd56] | 187 | if (img == NULL) {
|
---|
| 188 | rc = EINVAL;
|
---|
| 189 | goto error;
|
---|
| 190 | }
|
---|
[a35b458] | 191 |
|
---|
[08232ee] | 192 | if (fseek(img, 0, SEEK_END) != 0) {
|
---|
[4c6fd56] | 193 | rc = EIO;
|
---|
| 194 | goto error;
|
---|
[08232ee] | 195 | }
|
---|
[a35b458] | 196 |
|
---|
[b16e77d] | 197 | off64_t img_size = ftell(img);
|
---|
[08232ee] | 198 | if (img_size < 0) {
|
---|
[4c6fd56] | 199 | rc = EIO;
|
---|
| 200 | goto error;
|
---|
[08232ee] | 201 | }
|
---|
[a35b458] | 202 |
|
---|
[08232ee] | 203 | num_blocks = img_size / block_size;
|
---|
[a35b458] | 204 |
|
---|
[12956e57] | 205 | fibril_mutex_initialize(&dev_lock);
|
---|
[a35b458] | 206 |
|
---|
[1cbed6b] | 207 | return EOK;
|
---|
[4c6fd56] | 208 | error:
|
---|
| 209 | if (img != NULL) {
|
---|
| 210 | fclose(img);
|
---|
| 211 | img = NULL;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | if (srv != NULL) {
|
---|
| 215 | loc_server_unregister(srv);
|
---|
| 216 | srv = NULL;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | return rc;
|
---|
[1cbed6b] | 220 | }
|
---|
| 221 |
|
---|
[984a9ba] | 222 | static void file_bd_connection(ipc_call_t *icall, void *arg)
|
---|
[1cbed6b] | 223 | {
|
---|
[984a9ba] | 224 | bd_conn(icall, &bd_srvs);
|
---|
[4802dd7] | 225 | }
|
---|
[1cbed6b] | 226 |
|
---|
[4802dd7] | 227 | /** Open device. */
|
---|
[b7fd2a0] | 228 | static errno_t file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
[4802dd7] | 229 | {
|
---|
| 230 | return EOK;
|
---|
| 231 | }
|
---|
[1cbed6b] | 232 |
|
---|
[4802dd7] | 233 | /** Close device. */
|
---|
[b7fd2a0] | 234 | static errno_t file_bd_close(bd_srv_t *bd)
|
---|
[4802dd7] | 235 | {
|
---|
| 236 | return EOK;
|
---|
[1cbed6b] | 237 | }
|
---|
| 238 |
|
---|
[1ee00b7] | 239 | /** Read blocks from the device. */
|
---|
[b7fd2a0] | 240 | static errno_t file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
|
---|
[4802dd7] | 241 | size_t size)
|
---|
[1cbed6b] | 242 | {
|
---|
| 243 | size_t n_rd;
|
---|
| 244 |
|
---|
[4802dd7] | 245 | if (size < cnt * block_size)
|
---|
| 246 | return EINVAL;
|
---|
| 247 |
|
---|
[fb150d78] | 248 | /* Check whether access is within device address bounds. */
|
---|
| 249 | if (ba + cnt > num_blocks) {
|
---|
[ed903174] | 250 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
---|
| 251 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
---|
[fb150d78] | 252 | num_blocks - 1);
|
---|
| 253 | return ELIMIT;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[12956e57] | 256 | fibril_mutex_lock(&dev_lock);
|
---|
[1cbed6b] | 257 |
|
---|
[c77a64f] | 258 | clearerr(img);
|
---|
[d5c1051] | 259 | if (fseek(img, ba * block_size, SEEK_SET) < 0) {
|
---|
[6c01702] | 260 | fibril_mutex_unlock(&dev_lock);
|
---|
| 261 | return EIO;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[1ee00b7] | 264 | n_rd = fread(buf, block_size, cnt, img);
|
---|
[1cbed6b] | 265 |
|
---|
| 266 | if (ferror(img)) {
|
---|
[12956e57] | 267 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 268 | return EIO; /* Read error */
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[12956e57] | 271 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 272 |
|
---|
[1ee00b7] | 273 | if (n_rd < cnt)
|
---|
| 274 | return EINVAL; /* Read beyond end of device */
|
---|
[1cbed6b] | 275 |
|
---|
| 276 | return EOK;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[1ee00b7] | 279 | /** Write blocks to the device. */
|
---|
[b7fd2a0] | 280 | static errno_t file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
|
---|
[4802dd7] | 281 | const void *buf, size_t size)
|
---|
[1cbed6b] | 282 | {
|
---|
| 283 | size_t n_wr;
|
---|
| 284 |
|
---|
[4802dd7] | 285 | if (size < cnt * block_size)
|
---|
| 286 | return EINVAL;
|
---|
| 287 |
|
---|
[fb150d78] | 288 | /* Check whether access is within device address bounds. */
|
---|
| 289 | if (ba + cnt > num_blocks) {
|
---|
[ed903174] | 290 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
---|
| 291 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
---|
[fb150d78] | 292 | num_blocks - 1);
|
---|
| 293 | return ELIMIT;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[12956e57] | 296 | fibril_mutex_lock(&dev_lock);
|
---|
[1cbed6b] | 297 |
|
---|
[c77a64f] | 298 | clearerr(img);
|
---|
[d5c1051] | 299 | if (fseek(img, ba * block_size, SEEK_SET) < 0) {
|
---|
[6c01702] | 300 | fibril_mutex_unlock(&dev_lock);
|
---|
| 301 | return EIO;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[dccf721] | 304 | n_wr = fwrite(buf, block_size, cnt, img);
|
---|
[1cbed6b] | 305 |
|
---|
[1ee00b7] | 306 | if (ferror(img) || n_wr < cnt) {
|
---|
[12956e57] | 307 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 308 | return EIO; /* Write error */
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[6c01702] | 311 | if (fflush(img) != 0) {
|
---|
| 312 | fibril_mutex_unlock(&dev_lock);
|
---|
| 313 | return EIO;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[12956e57] | 316 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 317 |
|
---|
| 318 | return EOK;
|
---|
| 319 | }
|
---|
| 320 |
|
---|
[4802dd7] | 321 | /** Get device block size. */
|
---|
[b7fd2a0] | 322 | static errno_t file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
[4802dd7] | 323 | {
|
---|
| 324 | *rsize = block_size;
|
---|
| 325 | return EOK;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 | /** Get number of blocks on device. */
|
---|
[b7fd2a0] | 329 | static errno_t file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
[4802dd7] | 330 | {
|
---|
| 331 | *rnb = num_blocks;
|
---|
| 332 | return EOK;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[1cbed6b] | 335 | /**
|
---|
| 336 | * @}
|
---|
| 337 | */
|
---|