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