source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 1433ecda

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

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