source: mainline/uspace/srv/bd/file_bd/file_bd.c@ d776329b

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

Enable partition support with all disk 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 <async.h>
44#include <as.h>
45#include <bd_srv.h>
46#include <fibril_synch.h>
47#include <loc.h>
48#include <sys/types.h>
49#include <sys/typefmt.h>
50#include <errno.h>
51#include <stdbool.h>
52#include <task.h>
53#include <macros.h>
54
55#define NAME "file_bd"
56
57#define DEFAULT_BLOCK_SIZE 512
58
59static size_t block_size;
60static aoff64_t num_blocks;
61static FILE *img;
62
63static service_id_t service_id;
64static bd_srvs_t bd_srvs;
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, void *);
70
71static int file_bd_open(bd_srvs_t *, 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};
86
87int main(int argc, char **argv)
88{
89 int rc;
90 char *image_name;
91 char *device_name;
92 category_id_t disk_cat;
93
94 printf(NAME ": File-backed block device driver\n");
95
96 block_size = DEFAULT_BLOCK_SIZE;
97
98 ++argv; --argc;
99 while (*argv != NULL && (*argv)[0] == '-') {
100 /* Option */
101 if (str_cmp(*argv, "-b") == 0) {
102 if (argc < 2) {
103 printf("Argument missing.\n");
104 print_usage();
105 return -1;
106 }
107
108 rc = str_size_t(argv[1], NULL, 10, true, &block_size);
109 if (rc != EOK || block_size == 0) {
110 printf("Invalid block size '%s'.\n", argv[1]);
111 print_usage();
112 return -1;
113 }
114 ++argv; --argc;
115 } else {
116 printf("Invalid option '%s'.\n", *argv);
117 print_usage();
118 return -1;
119 }
120 ++argv; --argc;
121 }
122
123 if (argc < 2) {
124 printf("Missing arguments.\n");
125 print_usage();
126 return -1;
127 }
128
129 image_name = argv[0];
130 device_name = argv[1];
131
132 if (file_bd_init(image_name) != EOK)
133 return -1;
134
135 rc = loc_service_register(device_name, &service_id);
136 if (rc != EOK) {
137 printf("%s: Unable to register device '%s'.\n",
138 NAME, device_name);
139 return rc;
140 }
141
142 rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING);
143 if (rc != EOK) {
144 printf("%s: Failed resolving category 'disk'.\n", NAME);
145 return rc;
146 }
147
148 rc = loc_service_add_to_cat(service_id, disk_cat);
149 if (rc != EOK) {
150 printf("%s: Failed adding %s to category.",
151 NAME, device_name);
152 return rc;
153 }
154
155 printf("%s: Accepting connections\n", NAME);
156 task_retval(0);
157 async_manager();
158
159 /* Not reached */
160 return 0;
161}
162
163static void print_usage(void)
164{
165 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
166}
167
168static int file_bd_init(const char *fname)
169{
170 bd_srvs_init(&bd_srvs);
171 bd_srvs.ops = &file_bd_ops;
172
173 async_set_fallback_port_handler(file_bd_connection, NULL);
174 int rc = loc_server_register(NAME);
175 if (rc != EOK) {
176 printf("%s: Unable to register driver.\n", NAME);
177 return rc;
178 }
179
180 img = fopen(fname, "rb+");
181 if (img == NULL)
182 return EINVAL;
183
184 if (fseek(img, 0, SEEK_END) != 0) {
185 fclose(img);
186 return EIO;
187 }
188
189 off64_t img_size = ftell(img);
190 if (img_size < 0) {
191 fclose(img);
192 return EIO;
193 }
194
195 num_blocks = img_size / block_size;
196
197 fibril_mutex_initialize(&dev_lock);
198
199 return EOK;
200}
201
202static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
203{
204 bd_conn(iid, icall, &bd_srvs);
205}
206
207/** Open device. */
208static int file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
209{
210 return EOK;
211}
212
213/** Close device. */
214static int file_bd_close(bd_srv_t *bd)
215{
216 return EOK;
217}
218
219/** Read blocks from the device. */
220static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
221 size_t size)
222{
223 size_t n_rd;
224 int rc;
225
226 if (size < cnt * block_size)
227 return EINVAL;
228
229 /* Check whether access is within device address bounds. */
230 if (ba + cnt > num_blocks) {
231 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
232 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
233 num_blocks - 1);
234 return ELIMIT;
235 }
236
237 fibril_mutex_lock(&dev_lock);
238
239 clearerr(img);
240 rc = fseek(img, ba * block_size, SEEK_SET);
241 if (rc < 0) {
242 fibril_mutex_unlock(&dev_lock);
243 return EIO;
244 }
245
246 n_rd = fread(buf, block_size, cnt, img);
247
248 if (ferror(img)) {
249 fibril_mutex_unlock(&dev_lock);
250 return EIO; /* Read error */
251 }
252
253 fibril_mutex_unlock(&dev_lock);
254
255 if (n_rd < cnt)
256 return EINVAL; /* Read beyond end of device */
257
258 return EOK;
259}
260
261/** Write blocks to the device. */
262static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
263 const void *buf, size_t size)
264{
265 size_t n_wr;
266 int rc;
267
268 if (size < cnt * block_size)
269 return EINVAL;
270
271 /* Check whether access is within device address bounds. */
272 if (ba + cnt > num_blocks) {
273 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
274 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
275 num_blocks - 1);
276 return ELIMIT;
277 }
278
279 fibril_mutex_lock(&dev_lock);
280
281 clearerr(img);
282 rc = fseek(img, ba * block_size, SEEK_SET);
283 if (rc < 0) {
284 fibril_mutex_unlock(&dev_lock);
285 return EIO;
286 }
287
288 n_wr = fwrite(buf, block_size, cnt, img);
289
290 if (ferror(img) || n_wr < cnt) {
291 fibril_mutex_unlock(&dev_lock);
292 return EIO; /* Write error */
293 }
294
295 if (fflush(img) != 0) {
296 fibril_mutex_unlock(&dev_lock);
297 return EIO;
298 }
299
300 fibril_mutex_unlock(&dev_lock);
301
302 return EOK;
303}
304
305/** Get device block size. */
306static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
307{
308 *rsize = block_size;
309 return EOK;
310}
311
312/** Get number of blocks on device. */
313static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
314{
315 *rnb = num_blocks;
316 return EOK;
317}
318
319/**
320 * @}
321 */
Note: See TracBrowser for help on using the repository browser.