source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 3b47db6

Last change on this file since 3b47db6 was 3b47db6, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

(optional) Remove EXIT_RC().

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