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

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

Remove sys/typefmt.h

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