source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 5f94a0c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5f94a0c was 47b7006, checked in by Martin Decky <martin@…>, 15 years ago

improve run-time termination

  • get rid of exit()
  • get rid of _exit(), use the common exit()
  • get rid of core(), use the common abort()
  • make main() more fail-safe (call abort() on unhealthy conditions), call async_sess_init() explicitly
  • add several libc-private headers for cleaner environment
  • use SYS_TASK_EXIT in exit() and abort()
  • 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 <unistd.h>
43#include <ipc/bd.h>
44#include <async.h>
45#include <as.h>
[1e4cada]46#include <fibril_synch.h>
[1cbed6b]47#include <devmap.h>
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
[991f645]63static devmap_handle_t devmap_handle;
[12956e57]64static fibril_mutex_t dev_lock;
[1cbed6b]65
[45df59a]66static void print_usage(void);
[1cbed6b]67static int file_bd_init(const char *fname);
68static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
[1ee00b7]69static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
70static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
[1cbed6b]71
72int main(int argc, char **argv)
73{
74 int rc;
[45df59a]75 char *image_name;
76 char *device_name;
[1cbed6b]77
78 printf(NAME ": File-backed block device driver\n");
79
[45df59a]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();
[1cbed6b]110 return -1;
111 }
112
[45df59a]113 image_name = argv[0];
114 device_name = argv[1];
115
116 if (file_bd_init(image_name) != EOK)
[1cbed6b]117 return -1;
118
[45df59a]119 rc = devmap_device_register(device_name, &devmap_handle);
[1cbed6b]120 if (rc != EOK) {
[45df59a]121 printf(NAME ": Unable to register device '%s'.\n",
122 device_name);
[1cbed6b]123 return rc;
124 }
125
126 printf(NAME ": Accepting connections\n");
[95bc57c]127 task_retval(0);
[1cbed6b]128 async_manager();
129
130 /* Not reached */
131 return 0;
132}
133
[45df59a]134static void print_usage(void)
135{
136 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
137}
138
[1cbed6b]139static int file_bd_init(const char *fname)
140{
141 int rc;
[08232ee]142 long img_size;
[1cbed6b]143
144 rc = devmap_driver_register(NAME, file_bd_connection);
145 if (rc < 0) {
146 printf(NAME ": Unable to register driver.\n");
147 return rc;
148 }
149
150 img = fopen(fname, "rb+");
151 if (img == NULL)
152 return EINVAL;
153
[08232ee]154 if (fseek(img, 0, SEEK_END) != 0) {
155 fclose(img);
156 return EIO;
157 }
158
159 img_size = ftell(img);
160 if (img_size < 0) {
161 fclose(img);
162 return EIO;
163 }
164
165 num_blocks = img_size / block_size;
166
[12956e57]167 fibril_mutex_initialize(&dev_lock);
168
[1cbed6b]169 return EOK;
170}
171
172static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
173{
174 void *fs_va = NULL;
175 ipc_callid_t callid;
176 ipc_call_t call;
[96b02eb9]177 sysarg_t method;
[1ee00b7]178 size_t comm_size;
[47b7006]179 unsigned int flags;
[1cbed6b]180 int retval;
[1ee00b7]181 uint64_t ba;
182 size_t cnt;
[1cbed6b]183
184 /* Answer the IPC_M_CONNECT_ME_TO call. */
[ffa2c8ef]185 async_answer_0(iid, EOK);
[1cbed6b]186
[0da4e41]187 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
[ffa2c8ef]188 async_answer_0(callid, EHANGUP);
[1cbed6b]189 return;
190 }
191
192 fs_va = as_get_mappable_page(comm_size);
193 if (fs_va == NULL) {
[ffa2c8ef]194 async_answer_0(callid, EHANGUP);
[1cbed6b]195 return;
196 }
197
[0da4e41]198 (void) async_share_out_finalize(callid, fs_va);
[1cbed6b]199
200 while (1) {
201 callid = async_get_call(&call);
[228e490]202 method = IPC_GET_IMETHOD(call);
[1cbed6b]203 switch (method) {
204 case IPC_M_PHONE_HUNGUP:
205 /* The other side has hung up. */
[ffa2c8ef]206 async_answer_0(callid, EOK);
[1cbed6b]207 return;
[1ee00b7]208 case BD_READ_BLOCKS:
209 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
210 IPC_GET_ARG2(call));
211 cnt = IPC_GET_ARG3(call);
212 if (cnt * block_size > comm_size) {
213 retval = ELIMIT;
[1cbed6b]214 break;
215 }
[1ee00b7]216 retval = file_bd_read_blocks(ba, cnt, fs_va);
[1cbed6b]217 break;
[1ee00b7]218 case BD_WRITE_BLOCKS:
219 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
220 IPC_GET_ARG2(call));
221 cnt = IPC_GET_ARG3(call);
222 if (cnt * block_size > comm_size) {
223 retval = ELIMIT;
224 break;
225 }
226 retval = file_bd_write_blocks(ba, cnt, fs_va);
227 break;
228 case BD_GET_BLOCK_SIZE:
[ffa2c8ef]229 async_answer_1(callid, EOK, block_size);
[1ee00b7]230 continue;
[08232ee]231 case BD_GET_NUM_BLOCKS:
[ffa2c8ef]232 async_answer_2(callid, EOK, LOWER32(num_blocks),
[08232ee]233 UPPER32(num_blocks));
234 continue;
[1cbed6b]235 default:
236 retval = EINVAL;
237 break;
238 }
[ffa2c8ef]239 async_answer_0(callid, retval);
[1cbed6b]240 }
241}
242
[1ee00b7]243/** Read blocks from the device. */
244static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
[1cbed6b]245{
246 size_t n_rd;
[6c01702]247 int rc;
[1cbed6b]248
[fb150d78]249 /* Check whether access is within device address bounds. */
250 if (ba + cnt > num_blocks) {
[ed903174]251 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
252 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
[fb150d78]253 num_blocks - 1);
254 return ELIMIT;
255 }
256
[12956e57]257 fibril_mutex_lock(&dev_lock);
[1cbed6b]258
[c77a64f]259 clearerr(img);
[6c01702]260 rc = fseek(img, ba * block_size, SEEK_SET);
261 if (rc < 0) {
262 fibril_mutex_unlock(&dev_lock);
263 return EIO;
264 }
265
[1ee00b7]266 n_rd = fread(buf, block_size, cnt, img);
[1cbed6b]267
268 if (ferror(img)) {
[12956e57]269 fibril_mutex_unlock(&dev_lock);
[1cbed6b]270 return EIO; /* Read error */
271 }
272
[12956e57]273 fibril_mutex_unlock(&dev_lock);
[1cbed6b]274
[1ee00b7]275 if (n_rd < cnt)
276 return EINVAL; /* Read beyond end of device */
[1cbed6b]277
278 return EOK;
279}
280
[1ee00b7]281/** Write blocks to the device. */
282static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
[1cbed6b]283{
284 size_t n_wr;
[6c01702]285 int rc;
[1cbed6b]286
[fb150d78]287 /* Check whether access is within device address bounds. */
288 if (ba + cnt > num_blocks) {
[ed903174]289 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
290 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
[fb150d78]291 num_blocks - 1);
292 return ELIMIT;
293 }
294
[12956e57]295 fibril_mutex_lock(&dev_lock);
[1cbed6b]296
[c77a64f]297 clearerr(img);
[6c01702]298 rc = fseek(img, ba * block_size, SEEK_SET);
299 if (rc < 0) {
300 fibril_mutex_unlock(&dev_lock);
301 return EIO;
302 }
303
[dccf721]304 n_wr = fwrite(buf, block_size, cnt, img);
[1cbed6b]305
[1ee00b7]306 if (ferror(img) || n_wr < cnt) {
[12956e57]307 fibril_mutex_unlock(&dev_lock);
[1cbed6b]308 return EIO; /* Write error */
309 }
310
[6c01702]311 if (fflush(img) != 0) {
312 fibril_mutex_unlock(&dev_lock);
313 return EIO;
314 }
315
[12956e57]316 fibril_mutex_unlock(&dev_lock);
[1cbed6b]317
318 return EOK;
319}
320
321/**
322 * @}
323 */
Note: See TracBrowser for help on using the repository browser.