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