source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 4802dd7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4802dd7 was 4802dd7, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Factor out client and server IPC stubs for block devices.

  • Property mode set to 100644
File size: 6.9 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 <unistd.h>
43#include <async.h>
44#include <as.h>
[4802dd7]45#include <bd_srv.h>
[1e4cada]46#include <fibril_synch.h>
[15f3c3f]47#include <loc.h>
[1cbed6b]48#include <sys/types.h>
[fb150d78]49#include <sys/typefmt.h>
[1cbed6b]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
[45df59a]57#define DEFAULT_BLOCK_SIZE 512
58
59static size_t block_size;
[ed903174]60static aoff64_t num_blocks;
[1cbed6b]61static FILE *img;
62
[15f3c3f]63static service_id_t service_id;
[4802dd7]64static bd_srv_t bd_srv;
[12956e57]65static fibril_mutex_t dev_lock;
[1cbed6b]66
[45df59a]67static void print_usage(void);
[1cbed6b]68static int file_bd_init(const char *fname);
[9934f7d]69static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
[4802dd7]70
71static int file_bd_open(bd_srv_t *);
72static int file_bd_close(bd_srv_t *);
73static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
74static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
75static int file_bd_get_block_size(bd_srv_t *, size_t *);
76static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
77
78static bd_ops_t file_bd_ops = {
79 .open = file_bd_open,
80 .close = file_bd_close,
81 .read_blocks = file_bd_read_blocks,
82 .write_blocks = file_bd_write_blocks,
83 .get_block_size = file_bd_get_block_size,
84 .get_num_blocks = file_bd_get_num_blocks
85};
[1cbed6b]86
87int main(int argc, char **argv)
88{
89 int rc;
[45df59a]90 char *image_name;
91 char *device_name;
[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 }
[b16e77d]140
141 printf("%s: Accepting connections\n", NAME);
[95bc57c]142 task_retval(0);
[1cbed6b]143 async_manager();
[b16e77d]144
[1cbed6b]145 /* Not reached */
146 return 0;
147}
148
[45df59a]149static void print_usage(void)
150{
151 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
152}
153
[1cbed6b]154static int file_bd_init(const char *fname)
155{
[4802dd7]156 bd_srv_init(&bd_srv);
157 bd_srv.ops = &file_bd_ops;
158
[f302586]159 async_set_client_connection(file_bd_connection);
[b16e77d]160 int rc = loc_server_register(NAME);
161 if (rc != EOK) {
162 printf("%s: Unable to register driver.\n", NAME);
[1cbed6b]163 return rc;
164 }
[b16e77d]165
[1cbed6b]166 img = fopen(fname, "rb+");
167 if (img == NULL)
168 return EINVAL;
[b16e77d]169
[08232ee]170 if (fseek(img, 0, SEEK_END) != 0) {
171 fclose(img);
172 return EIO;
173 }
[b16e77d]174
175 off64_t img_size = ftell(img);
[08232ee]176 if (img_size < 0) {
177 fclose(img);
178 return EIO;
179 }
[b16e77d]180
[08232ee]181 num_blocks = img_size / block_size;
[b16e77d]182
[12956e57]183 fibril_mutex_initialize(&dev_lock);
[b16e77d]184
[1cbed6b]185 return EOK;
186}
187
[9934f7d]188static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[1cbed6b]189{
[4802dd7]190 bd_conn(iid, icall, &bd_srv);
191}
[1cbed6b]192
[4802dd7]193/** Open device. */
194static int file_bd_open(bd_srv_t *bd)
195{
196 return EOK;
197}
[1cbed6b]198
[4802dd7]199/** Close device. */
200static int file_bd_close(bd_srv_t *bd)
201{
202 return EOK;
[1cbed6b]203}
204
[1ee00b7]205/** Read blocks from the device. */
[4802dd7]206static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
207 size_t size)
[1cbed6b]208{
209 size_t n_rd;
[6c01702]210 int rc;
[1cbed6b]211
[4802dd7]212 if (size < cnt * block_size)
213 return EINVAL;
214
[fb150d78]215 /* Check whether access is within device address bounds. */
216 if (ba + cnt > num_blocks) {
[ed903174]217 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
218 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
[fb150d78]219 num_blocks - 1);
220 return ELIMIT;
221 }
222
[12956e57]223 fibril_mutex_lock(&dev_lock);
[1cbed6b]224
[c77a64f]225 clearerr(img);
[6c01702]226 rc = fseek(img, ba * block_size, SEEK_SET);
227 if (rc < 0) {
228 fibril_mutex_unlock(&dev_lock);
229 return EIO;
230 }
231
[1ee00b7]232 n_rd = fread(buf, block_size, cnt, img);
[1cbed6b]233
234 if (ferror(img)) {
[12956e57]235 fibril_mutex_unlock(&dev_lock);
[1cbed6b]236 return EIO; /* Read error */
237 }
238
[12956e57]239 fibril_mutex_unlock(&dev_lock);
[1cbed6b]240
[1ee00b7]241 if (n_rd < cnt)
242 return EINVAL; /* Read beyond end of device */
[1cbed6b]243
244 return EOK;
245}
246
[1ee00b7]247/** Write blocks to the device. */
[4802dd7]248static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
249 const void *buf, size_t size)
[1cbed6b]250{
251 size_t n_wr;
[6c01702]252 int rc;
[1cbed6b]253
[4802dd7]254 if (size < cnt * block_size)
255 return EINVAL;
256
[fb150d78]257 /* Check whether access is within device address bounds. */
258 if (ba + cnt > num_blocks) {
[ed903174]259 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
260 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
[fb150d78]261 num_blocks - 1);
262 return ELIMIT;
263 }
264
[12956e57]265 fibril_mutex_lock(&dev_lock);
[1cbed6b]266
[c77a64f]267 clearerr(img);
[6c01702]268 rc = fseek(img, ba * block_size, SEEK_SET);
269 if (rc < 0) {
270 fibril_mutex_unlock(&dev_lock);
271 return EIO;
272 }
273
[dccf721]274 n_wr = fwrite(buf, block_size, cnt, img);
[1cbed6b]275
[1ee00b7]276 if (ferror(img) || n_wr < cnt) {
[12956e57]277 fibril_mutex_unlock(&dev_lock);
[1cbed6b]278 return EIO; /* Write error */
279 }
280
[6c01702]281 if (fflush(img) != 0) {
282 fibril_mutex_unlock(&dev_lock);
283 return EIO;
284 }
285
[12956e57]286 fibril_mutex_unlock(&dev_lock);
[1cbed6b]287
288 return EOK;
289}
290
[4802dd7]291/** Get device block size. */
292static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
293{
294 *rsize = block_size;
295 return EOK;
296}
297
298/** Get number of blocks on device. */
299static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
300{
301 *rnb = num_blocks;
302 return EOK;
303}
304
[1cbed6b]305/**
306 * @}
307 */
Note: See TracBrowser for help on using the repository browser.