source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 991f645

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

Rename dev_handle_t to devmap_handle_t and make it explicitly clear that
dev_handle_t is a handle understood by devmap.

  • Property mode set to 100644
File size: 6.5 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
58static const size_t block_size = 512;
59static aoff64_t num_blocks;
60static FILE *img;
61
62static devmap_handle_t devmap_handle;
63static fibril_mutex_t dev_lock;
64
65static int file_bd_init(const char *fname);
66static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
67static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
68static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
69
70int main(int argc, char **argv)
71{
72 int rc;
73
74 printf(NAME ": File-backed block device driver\n");
75
76 if (argc != 3) {
77 printf("Expected two arguments (image name, device name).\n");
78 return -1;
79 }
80
81 if (file_bd_init(argv[1]) != EOK)
82 return -1;
83
84 rc = devmap_device_register(argv[2], &devmap_handle);
85 if (rc != EOK) {
86 devmap_hangup_phone(DEVMAP_DRIVER);
87 printf(NAME ": Unable to register device %s.\n",
88 argv[2]);
89 return rc;
90 }
91
92 printf(NAME ": Accepting connections\n");
93 task_retval(0);
94 async_manager();
95
96 /* Not reached */
97 return 0;
98}
99
100static int file_bd_init(const char *fname)
101{
102 int rc;
103 long img_size;
104
105 rc = devmap_driver_register(NAME, file_bd_connection);
106 if (rc < 0) {
107 printf(NAME ": Unable to register driver.\n");
108 return rc;
109 }
110
111 img = fopen(fname, "rb+");
112 if (img == NULL)
113 return EINVAL;
114
115 if (fseek(img, 0, SEEK_END) != 0) {
116 fclose(img);
117 return EIO;
118 }
119
120 img_size = ftell(img);
121 if (img_size < 0) {
122 fclose(img);
123 return EIO;
124 }
125
126 num_blocks = img_size / block_size;
127
128 fibril_mutex_initialize(&dev_lock);
129
130 return EOK;
131}
132
133static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall)
134{
135 void *fs_va = NULL;
136 ipc_callid_t callid;
137 ipc_call_t call;
138 ipcarg_t method;
139 size_t comm_size;
140 int flags;
141 int retval;
142 uint64_t ba;
143 size_t cnt;
144
145 /* Answer the IPC_M_CONNECT_ME_TO call. */
146 ipc_answer_0(iid, EOK);
147
148 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
149 ipc_answer_0(callid, EHANGUP);
150 return;
151 }
152
153 fs_va = as_get_mappable_page(comm_size);
154 if (fs_va == NULL) {
155 ipc_answer_0(callid, EHANGUP);
156 return;
157 }
158
159 (void) async_share_out_finalize(callid, fs_va);
160
161 while (1) {
162 callid = async_get_call(&call);
163 method = IPC_GET_METHOD(call);
164 switch (method) {
165 case IPC_M_PHONE_HUNGUP:
166 /* The other side has hung up. */
167 ipc_answer_0(callid, EOK);
168 return;
169 case BD_READ_BLOCKS:
170 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
171 IPC_GET_ARG2(call));
172 cnt = IPC_GET_ARG3(call);
173 if (cnt * block_size > comm_size) {
174 retval = ELIMIT;
175 break;
176 }
177 retval = file_bd_read_blocks(ba, cnt, fs_va);
178 break;
179 case BD_WRITE_BLOCKS:
180 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
181 IPC_GET_ARG2(call));
182 cnt = IPC_GET_ARG3(call);
183 if (cnt * block_size > comm_size) {
184 retval = ELIMIT;
185 break;
186 }
187 retval = file_bd_write_blocks(ba, cnt, fs_va);
188 break;
189 case BD_GET_BLOCK_SIZE:
190 ipc_answer_1(callid, EOK, block_size);
191 continue;
192 case BD_GET_NUM_BLOCKS:
193 ipc_answer_2(callid, EOK, LOWER32(num_blocks),
194 UPPER32(num_blocks));
195 continue;
196 default:
197 retval = EINVAL;
198 break;
199 }
200 ipc_answer_0(callid, retval);
201 }
202}
203
204/** Read blocks from the device. */
205static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
206{
207 size_t n_rd;
208 int rc;
209
210 /* Check whether access is within device address bounds. */
211 if (ba + cnt > num_blocks) {
212 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
213 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
214 num_blocks - 1);
215 return ELIMIT;
216 }
217
218 fibril_mutex_lock(&dev_lock);
219
220 clearerr(img);
221 rc = fseek(img, ba * block_size, SEEK_SET);
222 if (rc < 0) {
223 fibril_mutex_unlock(&dev_lock);
224 return EIO;
225 }
226
227 n_rd = fread(buf, block_size, cnt, img);
228
229 if (ferror(img)) {
230 fibril_mutex_unlock(&dev_lock);
231 return EIO; /* Read error */
232 }
233
234 fibril_mutex_unlock(&dev_lock);
235
236 if (n_rd < cnt)
237 return EINVAL; /* Read beyond end of device */
238
239 return EOK;
240}
241
242/** Write blocks to the device. */
243static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
244{
245 size_t n_wr;
246 int rc;
247
248 /* Check whether access is within device address bounds. */
249 if (ba + cnt > num_blocks) {
250 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
251 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
252 num_blocks - 1);
253 return ELIMIT;
254 }
255
256 fibril_mutex_lock(&dev_lock);
257
258 clearerr(img);
259 rc = fseek(img, ba * block_size, SEEK_SET);
260 if (rc < 0) {
261 fibril_mutex_unlock(&dev_lock);
262 return EIO;
263 }
264
265 n_wr = fwrite(buf, block_size, cnt, img);
266
267 if (ferror(img) || n_wr < cnt) {
268 fibril_mutex_unlock(&dev_lock);
269 return EIO; /* Write error */
270 }
271
272 if (fflush(img) != 0) {
273 fibril_mutex_unlock(&dev_lock);
274 return EIO;
275 }
276
277 fibril_mutex_unlock(&dev_lock);
278
279 return EOK;
280}
281
282/**
283 * @}
284 */
Note: See TracBrowser for help on using the repository browser.