source: mainline/uspace/srv/bd/file_bd/file_bd.c@ d60115a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d60115a was 7354b5e, checked in by Jakub Jermar <jakub@…>, 9 years ago

Remove sys/typefmt.h

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[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 <async.h>
43#include <as.h>
[4802dd7]44#include <bd_srv.h>
[1e4cada]45#include <fibril_synch.h>
[15f3c3f]46#include <loc.h>
[8d2dd7f2]47#include <stddef.h>
48#include <stdint.h>
[1cbed6b]49#include <errno.h>
[3e6a98c5]50#include <stdbool.h>
[95bc57c]51#include <task.h>
[1ee00b7]52#include <macros.h>
[1cbed6b]53
54#define NAME "file_bd"
55
[45df59a]56#define DEFAULT_BLOCK_SIZE 512
57
58static size_t block_size;
[ed903174]59static aoff64_t num_blocks;
[1cbed6b]60static FILE *img;
61
[15f3c3f]62static service_id_t service_id;
[135486d]63static bd_srvs_t bd_srvs;
[12956e57]64static fibril_mutex_t dev_lock;
[1cbed6b]65
[45df59a]66static void print_usage(void);
[1cbed6b]67static int file_bd_init(const char *fname);
[9934f7d]68static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
[4802dd7]69
[135486d]70static int file_bd_open(bd_srvs_t *, bd_srv_t *);
[4802dd7]71static int file_bd_close(bd_srv_t *);
72static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
73static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
74static int file_bd_get_block_size(bd_srv_t *, size_t *);
75static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
76
77static bd_ops_t file_bd_ops = {
78 .open = file_bd_open,
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
84};
[1cbed6b]85
86int main(int argc, char **argv)
87{
88 int rc;
[45df59a]89 char *image_name;
90 char *device_name;
[f97f5cc2]91 category_id_t disk_cat;
[1cbed6b]92
93 printf(NAME ": File-backed block device driver\n");
94
[45df59a]95 block_size = DEFAULT_BLOCK_SIZE;
96
97 ++argv; --argc;
98 while (*argv != NULL && (*argv)[0] == '-') {
99 /* Option */
100 if (str_cmp(*argv, "-b") == 0) {
101 if (argc < 2) {
102 printf("Argument missing.\n");
103 print_usage();
104 return -1;
105 }
106
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]);
110 print_usage();
111 return -1;
112 }
113 ++argv; --argc;
114 } else {
115 printf("Invalid option '%s'.\n", *argv);
116 print_usage();
117 return -1;
118 }
119 ++argv; --argc;
120 }
121
122 if (argc < 2) {
123 printf("Missing arguments.\n");
124 print_usage();
[1cbed6b]125 return -1;
126 }
127
[45df59a]128 image_name = argv[0];
129 device_name = argv[1];
130
131 if (file_bd_init(image_name) != EOK)
[1cbed6b]132 return -1;
133
[15f3c3f]134 rc = loc_service_register(device_name, &service_id);
[1cbed6b]135 if (rc != EOK) {
[b16e77d]136 printf("%s: Unable to register device '%s'.\n",
137 NAME, device_name);
[1cbed6b]138 return rc;
139 }
[f97f5cc2]140
141 rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING);
142 if (rc != EOK) {
143 printf("%s: Failed resolving category 'disk'.\n", NAME);
144 return rc;
145 }
146
147 rc = loc_service_add_to_cat(service_id, disk_cat);
148 if (rc != EOK) {
149 printf("%s: Failed adding %s to category.",
150 NAME, device_name);
151 return rc;
152 }
153
[b16e77d]154 printf("%s: Accepting connections\n", NAME);
[95bc57c]155 task_retval(0);
[1cbed6b]156 async_manager();
[b16e77d]157
[1cbed6b]158 /* Not reached */
159 return 0;
160}
161
[45df59a]162static void print_usage(void)
163{
164 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
165}
166
[1cbed6b]167static int file_bd_init(const char *fname)
168{
[135486d]169 bd_srvs_init(&bd_srvs);
170 bd_srvs.ops = &file_bd_ops;
[4802dd7]171
[b688fd8]172 async_set_fallback_port_handler(file_bd_connection, NULL);
[b16e77d]173 int rc = loc_server_register(NAME);
174 if (rc != EOK) {
175 printf("%s: Unable to register driver.\n", NAME);
[1cbed6b]176 return rc;
177 }
[b16e77d]178
[1cbed6b]179 img = fopen(fname, "rb+");
180 if (img == NULL)
181 return EINVAL;
[b16e77d]182
[08232ee]183 if (fseek(img, 0, SEEK_END) != 0) {
184 fclose(img);
185 return EIO;
186 }
[b16e77d]187
188 off64_t img_size = ftell(img);
[08232ee]189 if (img_size < 0) {
190 fclose(img);
191 return EIO;
192 }
[b16e77d]193
[08232ee]194 num_blocks = img_size / block_size;
[b16e77d]195
[12956e57]196 fibril_mutex_initialize(&dev_lock);
[b16e77d]197
[1cbed6b]198 return EOK;
199}
200
[9934f7d]201static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[1cbed6b]202{
[135486d]203 bd_conn(iid, icall, &bd_srvs);
[4802dd7]204}
[1cbed6b]205
[4802dd7]206/** Open device. */
[135486d]207static int file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
[4802dd7]208{
209 return EOK;
210}
[1cbed6b]211
[4802dd7]212/** Close device. */
213static int file_bd_close(bd_srv_t *bd)
214{
215 return EOK;
[1cbed6b]216}
217
[1ee00b7]218/** Read blocks from the device. */
[4802dd7]219static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
220 size_t size)
[1cbed6b]221{
222 size_t n_rd;
[6c01702]223 int rc;
[1cbed6b]224
[4802dd7]225 if (size < cnt * block_size)
226 return EINVAL;
227
[fb150d78]228 /* Check whether access is within device address bounds. */
229 if (ba + cnt > num_blocks) {
[ed903174]230 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
231 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
[fb150d78]232 num_blocks - 1);
233 return ELIMIT;
234 }
235
[12956e57]236 fibril_mutex_lock(&dev_lock);
[1cbed6b]237
[c77a64f]238 clearerr(img);
[6c01702]239 rc = fseek(img, ba * block_size, SEEK_SET);
240 if (rc < 0) {
241 fibril_mutex_unlock(&dev_lock);
242 return EIO;
243 }
244
[1ee00b7]245 n_rd = fread(buf, block_size, cnt, img);
[1cbed6b]246
247 if (ferror(img)) {
[12956e57]248 fibril_mutex_unlock(&dev_lock);
[1cbed6b]249 return EIO; /* Read error */
250 }
251
[12956e57]252 fibril_mutex_unlock(&dev_lock);
[1cbed6b]253
[1ee00b7]254 if (n_rd < cnt)
255 return EINVAL; /* Read beyond end of device */
[1cbed6b]256
257 return EOK;
258}
259
[1ee00b7]260/** Write blocks to the device. */
[4802dd7]261static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
262 const void *buf, size_t size)
[1cbed6b]263{
264 size_t n_wr;
[6c01702]265 int rc;
[1cbed6b]266
[4802dd7]267 if (size < cnt * block_size)
268 return EINVAL;
269
[fb150d78]270 /* Check whether access is within device address bounds. */
271 if (ba + cnt > num_blocks) {
[ed903174]272 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
273 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
[fb150d78]274 num_blocks - 1);
275 return ELIMIT;
276 }
277
[12956e57]278 fibril_mutex_lock(&dev_lock);
[1cbed6b]279
[c77a64f]280 clearerr(img);
[6c01702]281 rc = fseek(img, ba * block_size, SEEK_SET);
282 if (rc < 0) {
283 fibril_mutex_unlock(&dev_lock);
284 return EIO;
285 }
286
[dccf721]287 n_wr = fwrite(buf, block_size, cnt, img);
[1cbed6b]288
[1ee00b7]289 if (ferror(img) || n_wr < cnt) {
[12956e57]290 fibril_mutex_unlock(&dev_lock);
[1cbed6b]291 return EIO; /* Write error */
292 }
293
[6c01702]294 if (fflush(img) != 0) {
295 fibril_mutex_unlock(&dev_lock);
296 return EIO;
297 }
298
[12956e57]299 fibril_mutex_unlock(&dev_lock);
[1cbed6b]300
301 return EOK;
302}
303
[4802dd7]304/** Get device block size. */
305static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
306{
307 *rsize = block_size;
308 return EOK;
309}
310
311/** Get number of blocks on device. */
312static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
313{
314 *rnb = num_blocks;
315 return EOK;
316}
317
[1cbed6b]318/**
319 * @}
320 */
Note: See TracBrowser for help on using the repository browser.