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