| 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 <unistd.h>
|
|---|
| 43 | #include <ipc/ipc.h>
|
|---|
| 44 | #include <ipc/bd.h>
|
|---|
| 45 | #include <async.h>
|
|---|
| 46 | #include <as.h>
|
|---|
| 47 | #include <fibril_synch.h>
|
|---|
| 48 | #include <devmap.h>
|
|---|
| 49 | #include <sys/types.h>
|
|---|
| 50 | #include <sys/typefmt.h>
|
|---|
| 51 | #include <errno.h>
|
|---|
| 52 | #include <bool.h>
|
|---|
| 53 | #include <task.h>
|
|---|
| 54 | #include <macros.h>
|
|---|
| 55 |
|
|---|
| 56 | #define NAME "file_bd"
|
|---|
| 57 |
|
|---|
| 58 | #define DEFAULT_BLOCK_SIZE 512
|
|---|
| 59 |
|
|---|
| 60 | static size_t block_size;
|
|---|
| 61 | static aoff64_t num_blocks;
|
|---|
| 62 | static FILE *img;
|
|---|
| 63 |
|
|---|
| 64 | static devmap_handle_t devmap_handle;
|
|---|
| 65 | static fibril_mutex_t dev_lock;
|
|---|
| 66 |
|
|---|
| 67 | static void print_usage(void);
|
|---|
| 68 | static int file_bd_init(const char *fname);
|
|---|
| 69 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
|
|---|
| 70 | static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
|
|---|
| 71 | static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
|
|---|
| 72 |
|
|---|
| 73 | int main(int argc, char **argv)
|
|---|
| 74 | {
|
|---|
| 75 | int rc;
|
|---|
| 76 | char *image_name;
|
|---|
| 77 | char *device_name;
|
|---|
| 78 |
|
|---|
| 79 | printf(NAME ": File-backed block device driver\n");
|
|---|
| 80 |
|
|---|
| 81 | block_size = DEFAULT_BLOCK_SIZE;
|
|---|
| 82 |
|
|---|
| 83 | ++argv; --argc;
|
|---|
| 84 | while (*argv != NULL && (*argv)[0] == '-') {
|
|---|
| 85 | /* Option */
|
|---|
| 86 | if (str_cmp(*argv, "-b") == 0) {
|
|---|
| 87 | if (argc < 2) {
|
|---|
| 88 | printf("Argument missing.\n");
|
|---|
| 89 | print_usage();
|
|---|
| 90 | return -1;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | rc = str_size_t(argv[1], NULL, 10, true, &block_size);
|
|---|
| 94 | if (rc != EOK || block_size == 0) {
|
|---|
| 95 | printf("Invalid block size '%s'.\n", argv[1]);
|
|---|
| 96 | print_usage();
|
|---|
| 97 | return -1;
|
|---|
| 98 | }
|
|---|
| 99 | ++argv; --argc;
|
|---|
| 100 | } else {
|
|---|
| 101 | printf("Invalid option '%s'.\n", *argv);
|
|---|
| 102 | print_usage();
|
|---|
| 103 | return -1;
|
|---|
| 104 | }
|
|---|
| 105 | ++argv; --argc;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (argc < 2) {
|
|---|
| 109 | printf("Missing arguments.\n");
|
|---|
| 110 | print_usage();
|
|---|
| 111 | return -1;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | image_name = argv[0];
|
|---|
| 115 | device_name = argv[1];
|
|---|
| 116 |
|
|---|
| 117 | if (file_bd_init(image_name) != EOK)
|
|---|
| 118 | return -1;
|
|---|
| 119 |
|
|---|
| 120 | rc = devmap_device_register(device_name, &devmap_handle);
|
|---|
| 121 | if (rc != EOK) {
|
|---|
| 122 | devmap_hangup_phone(DEVMAP_DRIVER);
|
|---|
| 123 | printf(NAME ": Unable to register device '%s'.\n",
|
|---|
| 124 | device_name);
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | printf(NAME ": Accepting connections\n");
|
|---|
| 129 | task_retval(0);
|
|---|
| 130 | async_manager();
|
|---|
| 131 |
|
|---|
| 132 | /* Not reached */
|
|---|
| 133 | return 0;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static void print_usage(void)
|
|---|
| 137 | {
|
|---|
| 138 | printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | static int file_bd_init(const char *fname)
|
|---|
| 142 | {
|
|---|
| 143 | int rc;
|
|---|
| 144 | long img_size;
|
|---|
| 145 |
|
|---|
| 146 | rc = devmap_driver_register(NAME, file_bd_connection);
|
|---|
| 147 | if (rc < 0) {
|
|---|
| 148 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 149 | return rc;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | img = fopen(fname, "rb+");
|
|---|
| 153 | if (img == NULL)
|
|---|
| 154 | return EINVAL;
|
|---|
| 155 |
|
|---|
| 156 | if (fseek(img, 0, SEEK_END) != 0) {
|
|---|
| 157 | fclose(img);
|
|---|
| 158 | return EIO;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | img_size = ftell(img);
|
|---|
| 162 | if (img_size < 0) {
|
|---|
| 163 | fclose(img);
|
|---|
| 164 | return EIO;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | num_blocks = img_size / block_size;
|
|---|
| 168 |
|
|---|
| 169 | fibril_mutex_initialize(&dev_lock);
|
|---|
| 170 |
|
|---|
| 171 | return EOK;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 175 | {
|
|---|
| 176 | void *fs_va = NULL;
|
|---|
| 177 | ipc_callid_t callid;
|
|---|
| 178 | ipc_call_t call;
|
|---|
| 179 | sysarg_t method;
|
|---|
| 180 | size_t comm_size;
|
|---|
| 181 | int flags;
|
|---|
| 182 | int retval;
|
|---|
| 183 | uint64_t ba;
|
|---|
| 184 | size_t cnt;
|
|---|
| 185 |
|
|---|
| 186 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 187 | ipc_answer_0(iid, EOK);
|
|---|
| 188 |
|
|---|
| 189 | if (!async_share_out_receive(&callid, &comm_size, &flags)) {
|
|---|
| 190 | ipc_answer_0(callid, EHANGUP);
|
|---|
| 191 | return;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | fs_va = as_get_mappable_page(comm_size);
|
|---|
| 195 | if (fs_va == NULL) {
|
|---|
| 196 | ipc_answer_0(callid, EHANGUP);
|
|---|
| 197 | return;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | (void) async_share_out_finalize(callid, fs_va);
|
|---|
| 201 |
|
|---|
| 202 | while (1) {
|
|---|
| 203 | callid = async_get_call(&call);
|
|---|
| 204 | method = IPC_GET_IMETHOD(call);
|
|---|
| 205 | switch (method) {
|
|---|
| 206 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 207 | /* The other side has hung up. */
|
|---|
| 208 | ipc_answer_0(callid, EOK);
|
|---|
| 209 | return;
|
|---|
| 210 | case BD_READ_BLOCKS:
|
|---|
| 211 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
|---|
| 212 | IPC_GET_ARG2(call));
|
|---|
| 213 | cnt = IPC_GET_ARG3(call);
|
|---|
| 214 | if (cnt * block_size > comm_size) {
|
|---|
| 215 | retval = ELIMIT;
|
|---|
| 216 | break;
|
|---|
| 217 | }
|
|---|
| 218 | retval = file_bd_read_blocks(ba, cnt, fs_va);
|
|---|
| 219 | break;
|
|---|
| 220 | case BD_WRITE_BLOCKS:
|
|---|
| 221 | ba = MERGE_LOUP32(IPC_GET_ARG1(call),
|
|---|
| 222 | IPC_GET_ARG2(call));
|
|---|
| 223 | cnt = IPC_GET_ARG3(call);
|
|---|
| 224 | if (cnt * block_size > comm_size) {
|
|---|
| 225 | retval = ELIMIT;
|
|---|
| 226 | break;
|
|---|
| 227 | }
|
|---|
| 228 | retval = file_bd_write_blocks(ba, cnt, fs_va);
|
|---|
| 229 | break;
|
|---|
| 230 | case BD_GET_BLOCK_SIZE:
|
|---|
| 231 | ipc_answer_1(callid, EOK, block_size);
|
|---|
| 232 | continue;
|
|---|
| 233 | case BD_GET_NUM_BLOCKS:
|
|---|
| 234 | ipc_answer_2(callid, EOK, LOWER32(num_blocks),
|
|---|
| 235 | UPPER32(num_blocks));
|
|---|
| 236 | continue;
|
|---|
| 237 | default:
|
|---|
| 238 | retval = EINVAL;
|
|---|
| 239 | break;
|
|---|
| 240 | }
|
|---|
| 241 | ipc_answer_0(callid, retval);
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /** Read blocks from the device. */
|
|---|
| 246 | static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
|
|---|
| 247 | {
|
|---|
| 248 | size_t n_rd;
|
|---|
| 249 | int rc;
|
|---|
| 250 |
|
|---|
| 251 | /* Check whether access is within device address bounds. */
|
|---|
| 252 | if (ba + cnt > num_blocks) {
|
|---|
| 253 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
|---|
| 254 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
|---|
| 255 | num_blocks - 1);
|
|---|
| 256 | return ELIMIT;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | fibril_mutex_lock(&dev_lock);
|
|---|
| 260 |
|
|---|
| 261 | clearerr(img);
|
|---|
| 262 | rc = fseek(img, ba * block_size, SEEK_SET);
|
|---|
| 263 | if (rc < 0) {
|
|---|
| 264 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 265 | return EIO;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | n_rd = fread(buf, block_size, cnt, img);
|
|---|
| 269 |
|
|---|
| 270 | if (ferror(img)) {
|
|---|
| 271 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 272 | return EIO; /* Read error */
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 276 |
|
|---|
| 277 | if (n_rd < cnt)
|
|---|
| 278 | return EINVAL; /* Read beyond end of device */
|
|---|
| 279 |
|
|---|
| 280 | return EOK;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | /** Write blocks to the device. */
|
|---|
| 284 | static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
|
|---|
| 285 | {
|
|---|
| 286 | size_t n_wr;
|
|---|
| 287 | int rc;
|
|---|
| 288 |
|
|---|
| 289 | /* Check whether access is within device address bounds. */
|
|---|
| 290 | if (ba + cnt > num_blocks) {
|
|---|
| 291 | printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
|
|---|
| 292 | "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
|
|---|
| 293 | num_blocks - 1);
|
|---|
| 294 | return ELIMIT;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | fibril_mutex_lock(&dev_lock);
|
|---|
| 298 |
|
|---|
| 299 | clearerr(img);
|
|---|
| 300 | rc = fseek(img, ba * block_size, SEEK_SET);
|
|---|
| 301 | if (rc < 0) {
|
|---|
| 302 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 303 | return EIO;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | n_wr = fwrite(buf, block_size, cnt, img);
|
|---|
| 307 |
|
|---|
| 308 | if (ferror(img) || n_wr < cnt) {
|
|---|
| 309 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 310 | return EIO; /* Write error */
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | if (fflush(img) != 0) {
|
|---|
| 314 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 315 | return EIO;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | fibril_mutex_unlock(&dev_lock);
|
|---|
| 319 |
|
|---|
| 320 | return EOK;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | /**
|
|---|
| 324 | * @}
|
|---|
| 325 | */
|
|---|