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

Last change on this file since ca48672 was ca48672, checked in by Jiri Svoboda <jiri@…>, 8 days ago

loc_service_register() needs to take a port ID argument.

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