source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 7300c37

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7300c37 was 7ea7db31, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cease using devmap_get_phone() and devmap_hangup_phone() in drivers.

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