source: mainline/uspace/srv/bd/file_bd/file_bd.c@ 177f6ff

Last change on this file since 177f6ff was 4c6fd56, checked in by Jiri Svoboda <jiri@…>, 2 years ago

loc_server_register() should be callable more than once (API only)

Now loc_server_register() returns a pointer to a loc_srv_t object,
that is then passed to loc_service_register() and
loc_service_add_to_cat().

Added loc_server_unregister() that unregisters the server
and frees the loc_srv_t object.

Updated all callers. The implementation, however, is a stub.
It is not actually possible to call loc_server_register() more
than once, yet.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 * Copyright (c) 2023 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, &service_id);
141 if (rc != EOK) {
142 printf("%s: Unable to register device '%s': %s.\n",
143 NAME, device_name, str_error(rc));
144 return rc;
145 }
146
147 rc = loc_category_get_id("disk", &disk_cat, IPC_FLAG_BLOCKING);
148 if (rc != EOK) {
149 printf("%s: Failed resolving category 'disk': %s\n", NAME, str_error(rc));
150 return rc;
151 }
152
153 rc = loc_service_add_to_cat(srv, service_id, disk_cat);
154 if (rc != EOK) {
155 printf("%s: Failed adding %s to category: %s",
156 NAME, device_name, str_error(rc));
157 return rc;
158 }
159
160 printf("%s: Accepting connections\n", NAME);
161 task_retval(0);
162 async_manager();
163
164 /* Not reached */
165 return 0;
166}
167
168static void print_usage(void)
169{
170 printf("Usage: " NAME " [-b <block_size>] <image_file> <device_name>\n");
171}
172
173static errno_t file_bd_init(const char *fname)
174{
175 bd_srvs_init(&bd_srvs);
176 bd_srvs.ops = &file_bd_ops;
177
178 async_set_fallback_port_handler(file_bd_connection, NULL);
179 errno_t rc = loc_server_register(NAME, &srv);
180 if (rc != EOK) {
181 printf("%s: Unable to register driver.\n", NAME);
182 return rc;
183 }
184
185 img = fopen(fname, "rb+");
186 if (img == NULL) {
187 rc = EINVAL;
188 goto error;
189 }
190
191 if (fseek(img, 0, SEEK_END) != 0) {
192 rc = EIO;
193 goto error;
194 }
195
196 off64_t img_size = ftell(img);
197 if (img_size < 0) {
198 rc = EIO;
199 goto error;
200 }
201
202 num_blocks = img_size / block_size;
203
204 fibril_mutex_initialize(&dev_lock);
205
206 return EOK;
207error:
208 if (img != NULL) {
209 fclose(img);
210 img = NULL;
211 }
212
213 if (srv != NULL) {
214 loc_server_unregister(srv);
215 srv = NULL;
216 }
217
218 return rc;
219}
220
221static void file_bd_connection(ipc_call_t *icall, void *arg)
222{
223 bd_conn(icall, &bd_srvs);
224}
225
226/** Open device. */
227static errno_t file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
228{
229 return EOK;
230}
231
232/** Close device. */
233static errno_t file_bd_close(bd_srv_t *bd)
234{
235 return EOK;
236}
237
238/** Read blocks from the device. */
239static errno_t file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
240 size_t size)
241{
242 size_t n_rd;
243
244 if (size < cnt * block_size)
245 return EINVAL;
246
247 /* Check whether access is within device address bounds. */
248 if (ba + cnt > num_blocks) {
249 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
250 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
251 num_blocks - 1);
252 return ELIMIT;
253 }
254
255 fibril_mutex_lock(&dev_lock);
256
257 clearerr(img);
258 if (fseek(img, ba * block_size, SEEK_SET) < 0) {
259 fibril_mutex_unlock(&dev_lock);
260 return EIO;
261 }
262
263 n_rd = fread(buf, block_size, cnt, img);
264
265 if (ferror(img)) {
266 fibril_mutex_unlock(&dev_lock);
267 return EIO; /* Read error */
268 }
269
270 fibril_mutex_unlock(&dev_lock);
271
272 if (n_rd < cnt)
273 return EINVAL; /* Read beyond end of device */
274
275 return EOK;
276}
277
278/** Write blocks to the device. */
279static errno_t file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
280 const void *buf, size_t size)
281{
282 size_t n_wr;
283
284 if (size < cnt * block_size)
285 return EINVAL;
286
287 /* Check whether access is within device address bounds. */
288 if (ba + cnt > num_blocks) {
289 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "
290 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,
291 num_blocks - 1);
292 return ELIMIT;
293 }
294
295 fibril_mutex_lock(&dev_lock);
296
297 clearerr(img);
298 if (fseek(img, ba * block_size, SEEK_SET) < 0) {
299 fibril_mutex_unlock(&dev_lock);
300 return EIO;
301 }
302
303 n_wr = fwrite(buf, block_size, cnt, img);
304
305 if (ferror(img) || n_wr < cnt) {
306 fibril_mutex_unlock(&dev_lock);
307 return EIO; /* Write error */
308 }
309
310 if (fflush(img) != 0) {
311 fibril_mutex_unlock(&dev_lock);
312 return EIO;
313 }
314
315 fibril_mutex_unlock(&dev_lock);
316
317 return EOK;
318}
319
320/** Get device block size. */
321static errno_t file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
322{
323 *rsize = block_size;
324 return EOK;
325}
326
327/** Get number of blocks on device. */
328static errno_t file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
329{
330 *rnb = num_blocks;
331 return EOK;
332}
333
334/**
335 * @}
336 */
Note: See TracBrowser for help on using the repository browser.