2
* Copyright (c) 2009 Jiri Svoboda
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
35
* @brief File-backed block device driver
37
* Allows accessing a file as a block device. Useful for, e.g., mounting
47
#include <fibril_sync.h>
49
#include <sys/types.h>
54
#define NAME "file_bd"
56
static size_t comm_size;
59
static dev_handle_t dev_handle;
60
static fibril_mutex_t dev_lock;
62
static int file_bd_init(const char *fname);
63
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
64
static int file_bd_read(off_t blk_idx, size_t size, void *buf);
65
static int file_bd_write(off_t blk_idx, size_t size, void *buf);
67
int main(int argc, char **argv)
71
printf(NAME ": File-backed block device driver\n");
74
printf("Expected two arguments (image name, device name).\n");
78
if (file_bd_init(argv[1]) != EOK)
81
rc = devmap_device_register(argv[2], &dev_handle);
83
devmap_hangup_phone(DEVMAP_DRIVER);
84
printf(NAME ": Unable to register device %s.\n",
89
printf(NAME ": Accepting connections\n");
97
static int file_bd_init(const char *fname)
101
rc = devmap_driver_register(NAME, file_bd_connection);
103
printf(NAME ": Unable to register driver.\n");
107
img = fopen(fname, "rb+");
111
fibril_mutex_initialize(&dev_lock);
116
static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
127
/* Answer the IPC_M_CONNECT_ME_TO call. */
128
ipc_answer_0(iid, EOK);
130
if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
131
ipc_answer_0(callid, EHANGUP);
135
fs_va = as_get_mappable_page(comm_size);
137
ipc_answer_0(callid, EHANGUP);
141
(void) ipc_share_out_finalize(callid, fs_va);
144
callid = async_get_call(&call);
145
method = IPC_GET_METHOD(call);
147
case IPC_M_PHONE_HUNGUP:
148
/* The other side has hung up. */
149
ipc_answer_0(callid, EOK);
153
idx = IPC_GET_ARG1(call);
154
size = IPC_GET_ARG2(call);
155
if (size > comm_size) {
159
if (method == BD_READ_BLOCK)
160
retval = file_bd_read(idx, size, fs_va);
162
retval = file_bd_write(idx, size, fs_va);
168
ipc_answer_0(callid, retval);
172
static int file_bd_read(off_t blk_idx, size_t size, void *buf)
176
fibril_mutex_lock(&dev_lock);
178
fseek(img, blk_idx * size, SEEK_SET);
179
n_rd = fread(buf, 1, size, img);
182
fibril_mutex_unlock(&dev_lock);
183
return EIO; /* Read error */
186
fibril_mutex_unlock(&dev_lock);
189
return EINVAL; /* Read beyond end of disk */
194
static int file_bd_write(off_t blk_idx, size_t size, void *buf)
198
fibril_mutex_lock(&dev_lock);
200
fseek(img, blk_idx * size, SEEK_SET);
201
n_wr = fread(buf, 1, size, img);
203
if (ferror(img) || n_wr < size) {
204
fibril_mutex_unlock(&dev_lock);
205
return EIO; /* Write error */
208
fibril_mutex_unlock(&dev_lock);