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
45 #include <fibril_synch.h>
47 #include <sys/types.h>
48 #include <sys/typefmt.h>
54 #define NAME "file_bd"
56 #define DEFAULT_BLOCK_SIZE 512
58 static size_t block_size
;
59 static aoff64_t num_blocks
;
62 static service_id_t service_id
;
63 static bd_srvs_t bd_srvs
;
64 static fibril_mutex_t dev_lock
;
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 *);
70 static int file_bd_open(bd_srvs_t
*, bd_srv_t
*);
71 static int file_bd_close(bd_srv_t
*);
72 static int file_bd_read_blocks(bd_srv_t
*, aoff64_t
, size_t, void *, size_t);
73 static int file_bd_write_blocks(bd_srv_t
*, aoff64_t
, size_t, const void *, size_t);
74 static int file_bd_get_block_size(bd_srv_t
*, size_t *);
75 static int file_bd_get_num_blocks(bd_srv_t
*, aoff64_t
*);
77 static bd_ops_t file_bd_ops
= {
79 .close
= file_bd_close
,
80 .read_blocks
= file_bd_read_blocks
,
81 .write_blocks
= file_bd_write_blocks
,
82 .get_block_size
= file_bd_get_block_size
,
83 .get_num_blocks
= file_bd_get_num_blocks
86 int main(int argc
, char **argv
)
91 category_id_t disk_cat
;
93 printf(NAME
": File-backed block device driver\n");
95 block_size
= DEFAULT_BLOCK_SIZE
;
98 while (*argv
!= NULL
&& (*argv
)[0] == '-') {
100 if (str_cmp(*argv
, "-b") == 0) {
102 printf("Argument missing.\n");
107 rc
= str_size_t(argv
[1], NULL
, 10, true, &block_size
);
108 if (rc
!= EOK
|| block_size
== 0) {
109 printf("Invalid block size '%s'.\n", argv
[1]);
115 printf("Invalid option '%s'.\n", *argv
);
123 printf("Missing arguments.\n");
128 image_name
= argv
[0];
129 device_name
= argv
[1];
131 if (file_bd_init(image_name
) != EOK
)
134 rc
= loc_service_register(device_name
, &service_id
);
136 printf("%s: Unable to register device '%s'.\n",
141 rc
= loc_category_get_id("disk", &disk_cat
, IPC_FLAG_BLOCKING
);
143 printf("%s: Failed resolving category 'disk'.\n", NAME
);
147 rc
= loc_service_add_to_cat(service_id
, disk_cat
);
149 printf("%s: Failed adding %s to category.",
154 printf("%s: Accepting connections\n", NAME
);
162 static void print_usage(void)
164 printf("Usage: " NAME
" [-b <block_size>] <image_file> <device_name>\n");
167 static int file_bd_init(const char *fname
)
169 bd_srvs_init(&bd_srvs
);
170 bd_srvs
.ops
= &file_bd_ops
;
172 async_set_fallback_port_handler(file_bd_connection
, NULL
);
173 int rc
= loc_server_register(NAME
);
175 printf("%s: Unable to register driver.\n", NAME
);
179 img
= fopen(fname
, "rb+");
183 if (fseek(img
, 0, SEEK_END
) != 0) {
188 off64_t img_size
= ftell(img
);
194 num_blocks
= img_size
/ block_size
;
196 fibril_mutex_initialize(&dev_lock
);
201 static void file_bd_connection(ipc_callid_t iid
, ipc_call_t
*icall
, void *arg
)
203 bd_conn(iid
, icall
, &bd_srvs
);
207 static int file_bd_open(bd_srvs_t
*bds
, bd_srv_t
*bd
)
213 static int file_bd_close(bd_srv_t
*bd
)
218 /** Read blocks from the device. */
219 static int file_bd_read_blocks(bd_srv_t
*bd
, uint64_t ba
, size_t cnt
, void *buf
,
225 if (size
< cnt
* block_size
)
228 /* Check whether access is within device address bounds. */
229 if (ba
+ cnt
> num_blocks
) {
230 printf(NAME
": Accessed blocks %" PRIuOFF64
"-%" PRIuOFF64
", while "
231 "max block number is %" PRIuOFF64
".\n", ba
, ba
+ cnt
- 1,
236 fibril_mutex_lock(&dev_lock
);
239 rc
= fseek(img
, ba
* block_size
, SEEK_SET
);
241 fibril_mutex_unlock(&dev_lock
);
245 n_rd
= fread(buf
, block_size
, cnt
, img
);
248 fibril_mutex_unlock(&dev_lock
);
249 return EIO
; /* Read error */
252 fibril_mutex_unlock(&dev_lock
);
255 return EINVAL
; /* Read beyond end of device */
260 /** Write blocks to the device. */
261 static int file_bd_write_blocks(bd_srv_t
*bd
, uint64_t ba
, size_t cnt
,
262 const void *buf
, size_t size
)
267 if (size
< cnt
* block_size
)
270 /* Check whether access is within device address bounds. */
271 if (ba
+ cnt
> num_blocks
) {
272 printf(NAME
": Accessed blocks %" PRIuOFF64
"-%" PRIuOFF64
", while "
273 "max block number is %" PRIuOFF64
".\n", ba
, ba
+ cnt
- 1,
278 fibril_mutex_lock(&dev_lock
);
281 rc
= fseek(img
, ba
* block_size
, SEEK_SET
);
283 fibril_mutex_unlock(&dev_lock
);
287 n_wr
= fwrite(buf
, block_size
, cnt
, img
);
289 if (ferror(img
) || n_wr
< cnt
) {
290 fibril_mutex_unlock(&dev_lock
);
291 return EIO
; /* Write error */
294 if (fflush(img
) != 0) {
295 fibril_mutex_unlock(&dev_lock
);
299 fibril_mutex_unlock(&dev_lock
);
304 /** Get device block size. */
305 static int file_bd_get_block_size(bd_srv_t
*bd
, size_t *rsize
)
311 /** Get number of blocks on device. */
312 static int file_bd_get_num_blocks(bd_srv_t
*bd
, aoff64_t
*rnb
)