source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 88743b5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 88743b5 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
Line 
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>
47#include <fibril_synch.h>
48#include <devmap.h>
49#include <sys/types.h>
50#include <sys/typefmt.h>
51#include <errno.h>
52#include <bool.h>
53#include <task.h>
54#include <macros.h>
55
56#define NAME "file_bd"
57
58#define DEFAULT_BLOCK_SIZE 512
59
60static size_t block_size;
61static aoff64_t num_blocks;
62static FILE *img;
63
64static devmap_handle_t devmap_handle;
65static fibril_mutex_t dev_lock;
66
67static void print_usage(void);
68static int file_bd_init(const char *fname);
69static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
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);
72
73int main(int argc, char **argv)
74{
75 int rc;
76 char *image_name;
77 char *device_name;
78
79 printf(NAME ": File-backed block device driver\n");
80
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();
111 return -1;
112 }
113
114 image_name = argv[0];
115 device_name = argv[1];
116
117 if (file_bd_init(image_name) != EOK)
118 return -1;
119
120 rc = devmap_device_register(device_name, &devmap_handle);
121 if (rc != EOK) {
122 printf(NAME ": Unable to register device '%s'.\n",
123 device_name);
124 return rc;
125 }
126
127 printf(NAME ": Accepting connections\n");
128 task_retval(0);
129 async_manager();
130
131 /* Not reached */
132 return 0;
133}
134
135static void print_usage(void)
136{
137 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
138}
139
140static int file_bd_init(const char *fname)
141{
142 int rc;
143 long img_size;
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
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
168 fibril_mutex_initialize(&dev_lock);
169
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;
178 sysarg_t method;
179 size_t comm_size;
180 int flags;
181 int retval;
182 uint64_t ba;
183 size_t cnt;
184
185 /* Answer the IPC_M_CONNECT_ME_TO call. */
186 ipc_answer_0(iid, EOK);
187
188 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
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
199 (void) async_share_out_finalize(callid, fs_va);
200
201 while (1) {
202 callid = async_get_call(&call);
203 method = IPC_GET_IMETHOD(call);
204 switch (method) {
205 case IPC_M_PHONE_HUNGUP:
206 /* The other side has hung up. */
207 ipc_answer_0(callid, EOK);
208 return;
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;
215 break;
216 }
217 retval = file_bd_read_blocks(ba, cnt, fs_va);
218 break;
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;
232 case BD_GET_NUM_BLOCKS:
233 ipc_answer_2(callid, EOK, LOWER32(num_blocks),
234 UPPER32(num_blocks));
235 continue;
236 default:
237 retval = EINVAL;
238 break;
239 }
240 ipc_answer_0(callid, retval);
241 }
242}
243
244/** Read blocks from the device. */
245static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
246{
247 size_t n_rd;
248 int rc;
249
250 /* Check whether access is within device address bounds. */
251 if (ba + cnt > num_blocks) {
252 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
253 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
254 num_blocks - 1);
255 return ELIMIT;
256 }
257
258 fibril_mutex_lock(&dev_lock);
259
260 clearerr(img);
261 rc = fseek(img, ba * block_size, SEEK_SET);
262 if (rc < 0) {
263 fibril_mutex_unlock(&dev_lock);
264 return EIO;
265 }
266
267 n_rd = fread(buf, block_size, cnt, img);
268
269 if (ferror(img)) {
270 fibril_mutex_unlock(&dev_lock);
271 return EIO; /* Read error */
272 }
273
274 fibril_mutex_unlock(&dev_lock);
275
276 if (n_rd < cnt)
277 return EINVAL; /* Read beyond end of device */
278
279 return EOK;
280}
281
282/** Write blocks to the device. */
283static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
284{
285 size_t n_wr;
286 int rc;
287
288 /* Check whether access is within device address bounds. */
289 if (ba + cnt > num_blocks) {
290 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
291 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
292 num_blocks - 1);
293 return ELIMIT;
294 }
295
296 fibril_mutex_lock(&dev_lock);
297
298 clearerr(img);
299 rc = fseek(img, ba * block_size, SEEK_SET);
300 if (rc < 0) {
301 fibril_mutex_unlock(&dev_lock);
302 return EIO;
303 }
304
305 n_wr = fwrite(buf, block_size, cnt, img);
306
307 if (ferror(img) || n_wr < cnt) {
308 fibril_mutex_unlock(&dev_lock);
309 return EIO; /* Write error */
310 }
311
312 if (fflush(img) != 0) {
313 fibril_mutex_unlock(&dev_lock);
314 return EIO;
315 }
316
317 fibril_mutex_unlock(&dev_lock);
318
319 return EOK;
320}
321
322/**
323 * @}
324 */
Note: See TracBrowser for help on using the repository browser.