[1cbed6b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Jiri Svoboda
|
---|
| 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 |
|
---|
| 29 | /** @addtogroup bd
|
---|
| 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>
|
---|
[1cbed6b] | 54 |
|
---|
| 55 | #define NAME "file_bd"
|
---|
| 56 |
|
---|
[45df59a] | 57 | #define DEFAULT_BLOCK_SIZE 512
|
---|
| 58 |
|
---|
| 59 | static size_t block_size;
|
---|
[ed903174] | 60 | static aoff64_t num_blocks;
|
---|
[1cbed6b] | 61 | static FILE *img;
|
---|
| 62 |
|
---|
[15f3c3f] | 63 | static service_id_t service_id;
|
---|
[135486d] | 64 | static bd_srvs_t bd_srvs;
|
---|
[12956e57] | 65 | static fibril_mutex_t dev_lock;
|
---|
[1cbed6b] | 66 |
|
---|
[45df59a] | 67 | static void print_usage(void);
|
---|
[1cbed6b] | 68 | static int file_bd_init(const char *fname);
|
---|
[9934f7d] | 69 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
|
---|
[4802dd7] | 70 |
|
---|
[135486d] | 71 | static int file_bd_open(bd_srvs_t *, bd_srv_t *);
|
---|
[4802dd7] | 72 | static int file_bd_close(bd_srv_t *);
|
---|
| 73 | static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
|
---|
| 74 | static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
|
---|
| 75 | static int file_bd_get_block_size(bd_srv_t *, size_t *);
|
---|
| 76 | static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
|
---|
| 77 |
|
---|
| 78 | static bd_ops_t file_bd_ops = {
|
---|
| 79 | .open = file_bd_open,
|
---|
| 80 | .close = file_bd_close,
|
---|
| 81 | .read_blocks = file_bd_read_blocks,
|
---|
| 82 | .write_blocks = file_bd_write_blocks,
|
---|
| 83 | .get_block_size = file_bd_get_block_size,
|
---|
| 84 | .get_num_blocks = file_bd_get_num_blocks
|
---|
| 85 | };
|
---|
[1cbed6b] | 86 |
|
---|
| 87 | int main(int argc, char **argv)
|
---|
| 88 | {
|
---|
| 89 | int rc;
|
---|
[45df59a] | 90 | char *image_name;
|
---|
| 91 | char *device_name;
|
---|
[f97f5cc2] | 92 | category_id_t disk_cat;
|
---|
[1cbed6b] | 93 |
|
---|
| 94 | printf(NAME ": File-backed block device driver\n");
|
---|
| 95 |
|
---|
[45df59a] | 96 | block_size = DEFAULT_BLOCK_SIZE;
|
---|
| 97 |
|
---|
| 98 | ++argv; --argc;
|
---|
| 99 | while (*argv != NULL && (*argv)[0] == '-') {
|
---|
| 100 | /* Option */
|
---|
| 101 | if (str_cmp(*argv, "-b") == 0) {
|
---|
| 102 | if (argc < 2) {
|
---|
| 103 | printf("Argument missing.\n");
|
---|
| 104 | print_usage();
|
---|
| 105 | return -1;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | rc = str_size_t(argv[1], NULL, 10, true, &block_size);
|
---|
| 109 | if (rc != EOK || block_size == 0) {
|
---|
| 110 | printf("Invalid block size '%s'.\n", argv[1]);
|
---|
| 111 | print_usage();
|
---|
| 112 | return -1;
|
---|
| 113 | }
|
---|
| 114 | ++argv; --argc;
|
---|
| 115 | } else {
|
---|
| 116 | printf("Invalid option '%s'.\n", *argv);
|
---|
| 117 | print_usage();
|
---|
| 118 | return -1;
|
---|
| 119 | }
|
---|
| 120 | ++argv; --argc;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | if (argc < 2) {
|
---|
| 124 | printf("Missing arguments.\n");
|
---|
| 125 | print_usage();
|
---|
[1cbed6b] | 126 | return -1;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[45df59a] | 129 | image_name = argv[0];
|
---|
| 130 | device_name = argv[1];
|
---|
| 131 |
|
---|
| 132 | if (file_bd_init(image_name) != EOK)
|
---|
[1cbed6b] | 133 | return -1;
|
---|
| 134 |
|
---|
[15f3c3f] | 135 | rc = loc_service_register(device_name, &service_id);
|
---|
[1cbed6b] | 136 | if (rc != EOK) {
|
---|
[c1694b6b] | 137 | printf("%s: Unable to register device '%s': %s.\n",
|
---|
| 138 | NAME, device_name, str_error(rc));
|
---|
[1cbed6b] | 139 | return rc;
|
---|
| 140 | }
|
---|
[f97f5cc2] | 141 |
|
---|
| 142 | rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING);
|
---|
| 143 | if (rc != EOK) {
|
---|
[c1694b6b] | 144 | printf("%s: Failed resolving category 'disk': %s\n", NAME, str_error(rc));
|
---|
[f97f5cc2] | 145 | return rc;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | rc = loc_service_add_to_cat(service_id, disk_cat);
|
---|
| 149 | if (rc != EOK) {
|
---|
[c1694b6b] | 150 | printf("%s: Failed adding %s to category: %s",
|
---|
| 151 | NAME, device_name, str_error(rc));
|
---|
[f97f5cc2] | 152 | return rc;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[b16e77d] | 155 | printf("%s: Accepting connections\n", NAME);
|
---|
[95bc57c] | 156 | task_retval(0);
|
---|
[1cbed6b] | 157 | async_manager();
|
---|
[b16e77d] | 158 |
|
---|
[1cbed6b] | 159 | /* Not reached */
|
---|
| 160 | return 0;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[45df59a] | 163 | static void print_usage(void)
|
---|
| 164 | {
|
---|
| 165 | printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[1cbed6b] | 168 | static int file_bd_init(const char *fname)
|
---|
| 169 | {
|
---|
[135486d] | 170 | bd_srvs_init(&bd_srvs);
|
---|
| 171 | bd_srvs.ops = &file_bd_ops;
|
---|
[4802dd7] | 172 |
|
---|
[b688fd8] | 173 | async_set_fallback_port_handler(file_bd_connection, NULL);
|
---|
[b16e77d] | 174 | int rc = loc_server_register(NAME);
|
---|
| 175 | if (rc != EOK) {
|
---|
| 176 | printf("%s: Unable to register driver.\n", NAME);
|
---|
[1cbed6b] | 177 | return rc;
|
---|
| 178 | }
|
---|
[b16e77d] | 179 |
|
---|
[1cbed6b] | 180 | img = fopen(fname, "rb+");
|
---|
| 181 | if (img == NULL)
|
---|
| 182 | return EINVAL;
|
---|
[b16e77d] | 183 |
|
---|
[08232ee] | 184 | if (fseek(img, 0, SEEK_END) != 0) {
|
---|
| 185 | fclose(img);
|
---|
| 186 | return EIO;
|
---|
| 187 | }
|
---|
[b16e77d] | 188 |
|
---|
| 189 | off64_t img_size = ftell(img);
|
---|
[08232ee] | 190 | if (img_size < 0) {
|
---|
| 191 | fclose(img);
|
---|
| 192 | return EIO;
|
---|
| 193 | }
|
---|
[b16e77d] | 194 |
|
---|
[08232ee] | 195 | num_blocks = img_size / block_size;
|
---|
[b16e77d] | 196 |
|
---|
[12956e57] | 197 | fibril_mutex_initialize(&dev_lock);
|
---|
[b16e77d] | 198 |
|
---|
[1cbed6b] | 199 | return EOK;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[9934f7d] | 202 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[1cbed6b] | 203 | {
|
---|
[135486d] | 204 | bd_conn(iid, icall, &bd_srvs);
|
---|
[4802dd7] | 205 | }
|
---|
[1cbed6b] | 206 |
|
---|
[4802dd7] | 207 | /** Open device. */
|
---|
[135486d] | 208 | static int file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
|
---|
[4802dd7] | 209 | {
|
---|
| 210 | return EOK;
|
---|
| 211 | }
|
---|
[1cbed6b] | 212 |
|
---|
[4802dd7] | 213 | /** Close device. */
|
---|
| 214 | static int file_bd_close(bd_srv_t *bd)
|
---|
| 215 | {
|
---|
| 216 | return EOK;
|
---|
[1cbed6b] | 217 | }
|
---|
| 218 |
|
---|
[1ee00b7] | 219 | /** Read blocks from the device. */
|
---|
[4802dd7] | 220 | static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
|
---|
| 221 | size_t size)
|
---|
[1cbed6b] | 222 | {
|
---|
| 223 | size_t n_rd;
|
---|
| 224 |
|
---|
[4802dd7] | 225 | if (size < cnt * block_size)
|
---|
| 226 | return EINVAL;
|
---|
| 227 |
|
---|
[fb150d78] | 228 | /* Check whether access is within device address bounds. */
|
---|
| 229 | if (ba + cnt > num_blocks) {
|
---|
[ed903174] | 230 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
---|
| 231 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
---|
[fb150d78] | 232 | num_blocks - 1);
|
---|
| 233 | return ELIMIT;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[12956e57] | 236 | fibril_mutex_lock(&dev_lock);
|
---|
[1cbed6b] | 237 |
|
---|
[c77a64f] | 238 | clearerr(img);
|
---|
[d5c1051] | 239 | if (fseek(img, ba * block_size, SEEK_SET) < 0) {
|
---|
[6c01702] | 240 | fibril_mutex_unlock(&dev_lock);
|
---|
| 241 | return EIO;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[1ee00b7] | 244 | n_rd = fread(buf, block_size, cnt, img);
|
---|
[1cbed6b] | 245 |
|
---|
| 246 | if (ferror(img)) {
|
---|
[12956e57] | 247 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 248 | return EIO; /* Read error */
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[12956e57] | 251 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 252 |
|
---|
[1ee00b7] | 253 | if (n_rd < cnt)
|
---|
| 254 | return EINVAL; /* Read beyond end of device */
|
---|
[1cbed6b] | 255 |
|
---|
| 256 | return EOK;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[1ee00b7] | 259 | /** Write blocks to the device. */
|
---|
[4802dd7] | 260 | static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
|
---|
| 261 | const void *buf, size_t size)
|
---|
[1cbed6b] | 262 | {
|
---|
| 263 | size_t n_wr;
|
---|
| 264 |
|
---|
[4802dd7] | 265 | if (size < cnt * block_size)
|
---|
| 266 | return EINVAL;
|
---|
| 267 |
|
---|
[fb150d78] | 268 | /* Check whether access is within device address bounds. */
|
---|
| 269 | if (ba + cnt > num_blocks) {
|
---|
[ed903174] | 270 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
---|
| 271 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
---|
[fb150d78] | 272 | num_blocks - 1);
|
---|
| 273 | return ELIMIT;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[12956e57] | 276 | fibril_mutex_lock(&dev_lock);
|
---|
[1cbed6b] | 277 |
|
---|
[c77a64f] | 278 | clearerr(img);
|
---|
[d5c1051] | 279 | if (fseek(img, ba * block_size, SEEK_SET) < 0) {
|
---|
[6c01702] | 280 | fibril_mutex_unlock(&dev_lock);
|
---|
| 281 | return EIO;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[dccf721] | 284 | n_wr = fwrite(buf, block_size, cnt, img);
|
---|
[1cbed6b] | 285 |
|
---|
[1ee00b7] | 286 | if (ferror(img) || n_wr < cnt) {
|
---|
[12956e57] | 287 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 288 | return EIO; /* Write error */
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[6c01702] | 291 | if (fflush(img) != 0) {
|
---|
| 292 | fibril_mutex_unlock(&dev_lock);
|
---|
| 293 | return EIO;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
[12956e57] | 296 | fibril_mutex_unlock(&dev_lock);
|
---|
[1cbed6b] | 297 |
|
---|
| 298 | return EOK;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[4802dd7] | 301 | /** Get device block size. */
|
---|
| 302 | static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
|
---|
| 303 | {
|
---|
| 304 | *rsize = block_size;
|
---|
| 305 | return EOK;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | /** Get number of blocks on device. */
|
---|
| 309 | static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
|
---|
| 310 | {
|
---|
| 311 | *rnb = num_blocks;
|
---|
| 312 | return EOK;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[1cbed6b] | 315 | /**
|
---|
| 316 | * @}
|
---|
| 317 | */
|
---|