Remove unistd.h
[helenos.git] / uspace / srv / bd / file_bd / file_bd.c
blob122497b9165c93290bc36e299560ea1156f25210
1 /*
2 * Copyright (c) 2009 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** @addtogroup bd
30 * @{
33 /**
34 * @file
35 * @brief File-backed block device driver
37 * Allows accessing a file as a block device. Useful for, e.g., mounting
38 * a disk image.
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 <sys/types.h>
48 #include <sys/typefmt.h>
49 #include <errno.h>
50 #include <stdbool.h>
51 #include <task.h>
52 #include <macros.h>
54 #define NAME "file_bd"
56 #define DEFAULT_BLOCK_SIZE 512
58 static size_t block_size;
59 static aoff64_t num_blocks;
60 static FILE *img;
62 static service_id_t service_id;
63 static bd_srvs_t bd_srvs;
64 static fibril_mutex_t dev_lock;
66 static void print_usage(void);
67 static int file_bd_init(const char *fname);
68 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
70 static int file_bd_open(bd_srvs_t *, bd_srv_t *);
71 static int file_bd_close(bd_srv_t *);
72 static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
73 static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
74 static int file_bd_get_block_size(bd_srv_t *, size_t *);
75 static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
77 static 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
86 int main(int argc, char **argv)
88 int rc;
89 char *image_name;
90 char *device_name;
91 category_id_t disk_cat;
93 printf(NAME ": File-backed block device driver\n");
95 block_size = DEFAULT_BLOCK_SIZE;
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;
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;
113 ++argv; --argc;
114 } else {
115 printf("Invalid option '%s'.\n", *argv);
116 print_usage();
117 return -1;
119 ++argv; --argc;
122 if (argc < 2) {
123 printf("Missing arguments.\n");
124 print_usage();
125 return -1;
128 image_name = argv[0];
129 device_name = argv[1];
131 if (file_bd_init(image_name) != EOK)
132 return -1;
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;
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;
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;
154 printf("%s: Accepting connections\n", NAME);
155 task_retval(0);
156 async_manager();
158 /* Not reached */
159 return 0;
162 static void print_usage(void)
164 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
167 static int file_bd_init(const char *fname)
169 bd_srvs_init(&bd_srvs);
170 bd_srvs.ops = &file_bd_ops;
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;
179 img = fopen(fname, "rb+");
180 if (img == NULL)
181 return EINVAL;
183 if (fseek(img, 0, SEEK_END) != 0) {
184 fclose(img);
185 return EIO;
188 off64_t img_size = ftell(img);
189 if (img_size < 0) {
190 fclose(img);
191 return EIO;
194 num_blocks = img_size / block_size;
196 fibril_mutex_initialize(&dev_lock);
198 return EOK;
201 static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
203 bd_conn(iid, icall, &bd_srvs);
206 /** Open device. */
207 static int file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
209 return EOK;
212 /** Close device. */
213 static int file_bd_close(bd_srv_t *bd)
215 return EOK;
218 /** Read blocks from the device. */
219 static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
220 size_t size)
222 size_t n_rd;
223 int rc;
225 if (size < cnt * block_size)
226 return EINVAL;
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;
236 fibril_mutex_lock(&dev_lock);
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;
245 n_rd = fread(buf, block_size, cnt, img);
247 if (ferror(img)) {
248 fibril_mutex_unlock(&dev_lock);
249 return EIO; /* Read error */
252 fibril_mutex_unlock(&dev_lock);
254 if (n_rd < cnt)
255 return EINVAL; /* Read beyond end of device */
257 return EOK;
260 /** Write blocks to the device. */
261 static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
262 const void *buf, size_t size)
264 size_t n_wr;
265 int rc;
267 if (size < cnt * block_size)
268 return EINVAL;
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;
278 fibril_mutex_lock(&dev_lock);
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;
287 n_wr = fwrite(buf, block_size, cnt, img);
289 if (ferror(img) || n_wr < cnt) {
290 fibril_mutex_unlock(&dev_lock);
291 return EIO; /* Write error */
294 if (fflush(img) != 0) {
295 fibril_mutex_unlock(&dev_lock);
296 return EIO;
299 fibril_mutex_unlock(&dev_lock);
301 return EOK;
304 /** Get device block size. */
305 static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
307 *rsize = block_size;
308 return EOK;
311 /** Get number of blocks on device. */
312 static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
314 *rnb = num_blocks;
315 return EOK;
319 * @}