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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5c1051 was d5c1051, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

"Obviously harmless" error handling tweaks.

  • 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 <str_error.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': %s.\n",
138 NAME, device_name, str_error(rc));
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': %s\n", NAME, str_error(rc));
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: %s",
151 NAME, device_name, str_error(rc));
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
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 if (fseek(img, ba * block_size, SEEK_SET) < 0) {
240 fibril_mutex_unlock(&dev_lock);
241 return EIO;
242 }
243
244 n_rd = fread(buf, block_size, cnt, img);
245
246 if (ferror(img)) {
247 fibril_mutex_unlock(&dev_lock);
248 return EIO; /* Read error */
249 }
250
251 fibril_mutex_unlock(&dev_lock);
252
253 if (n_rd < cnt)
254 return EINVAL; /* Read beyond end of device */
255
256 return EOK;
257}
258
259/** Write blocks to the device. */
260static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
261 const void *buf, size_t size)
262{
263 size_t n_wr;
264
265 if (size < cnt * block_size)
266 return EINVAL;
267
268 /* Check whether access is within device address bounds. */
269 if (ba + cnt > num_blocks) {
270 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
271 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
272 num_blocks - 1);
273 return ELIMIT;
274 }
275
276 fibril_mutex_lock(&dev_lock);
277
278 clearerr(img);
279 if (fseek(img, ba * block_size, SEEK_SET) < 0) {
280 fibril_mutex_unlock(&dev_lock);
281 return EIO;
282 }
283
284 n_wr = fwrite(buf, block_size, cnt, img);
285
286 if (ferror(img) || n_wr < cnt) {
287 fibril_mutex_unlock(&dev_lock);
288 return EIO; /* Write error */
289 }
290
291 if (fflush(img) != 0) {
292 fibril_mutex_unlock(&dev_lock);
293 return EIO;
294 }
295
296 fibril_mutex_unlock(&dev_lock);
297
298 return EOK;
299}
300
301/** Get device block size. */
302static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
303{
304 *rsize = block_size;
305 return EOK;
306}
307
308/** Get number of blocks on device. */
309static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
310{
311 *rnb = num_blocks;
312 return EOK;
313}
314
315/**
316 * @}
317 */
Note: See TracBrowser for help on using the repository browser.