source: mainline/uspace/srv/volsrv/volsrv.c@ 6d00aff

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

Persistence of Tetris highscore table. Detect live mode and create directory structure in init task. Reading volume configuration, vol -c.

  • Property mode set to 100644
File size: 12.4 KB
Line 
1/*
2 * Copyright (c) 2015 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 volsrv
30 * @{
31 */
32/**
33 * @file Volume service
34 */
35
36#include <async.h>
37#include <errno.h>
38#include <str_error.h>
39#include <io/log.h>
40#include <ipc/services.h>
41#include <ipc/vol.h>
42#include <loc.h>
43#include <macros.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <task.h>
47#include <types/vol.h>
48
49#include "mkfs.h"
50#include "part.h"
51#include "volume.h"
52
53#define NAME "volsrv"
54
55const char *vol_cfg_file = "/cfg/volsrv.sif";
56
57static void vol_client_conn(ipc_call_t *, void *);
58
59static errno_t vol_init(void)
60{
61 errno_t rc;
62 vol_volumes_t *volumes = NULL;
63 vol_parts_t *parts = NULL;
64
65 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_init()");
66
67 rc = vol_volumes_create(vol_cfg_file, &volumes);
68 if (rc != EOK)
69 goto error;
70
71 rc = vol_parts_create(volumes, &parts);
72 if (rc != EOK)
73 goto error;
74
75 rc = vol_part_discovery_start(parts);
76 if (rc != EOK)
77 goto error;
78
79 async_set_fallback_port_handler(vol_client_conn, parts);
80
81 rc = loc_server_register(NAME);
82 if (rc != EOK) {
83 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
84 rc = EEXIST;
85 }
86
87 service_id_t sid;
88 rc = loc_service_register(SERVICE_NAME_VOLSRV, &sid);
89 if (rc != EOK) {
90 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
91 rc = EEXIST;
92 goto error;
93 }
94
95 return EOK;
96error:
97 vol_volumes_destroy(volumes);
98 vol_parts_destroy(parts);
99 return rc;
100}
101
102static void vol_get_parts_srv(vol_parts_t *parts, ipc_call_t *icall)
103{
104 ipc_call_t call;
105 size_t size;
106 size_t act_size;
107 errno_t rc;
108
109 if (!async_data_read_receive(&call, &size)) {
110 async_answer_0(&call, EREFUSED);
111 async_answer_0(icall, EREFUSED);
112 return;
113 }
114
115 service_id_t *id_buf = (service_id_t *) malloc(size);
116 if (id_buf == NULL) {
117 async_answer_0(&call, ENOMEM);
118 async_answer_0(icall, ENOMEM);
119 return;
120 }
121
122 rc = vol_part_get_ids(parts, id_buf, size, &act_size);
123 if (rc != EOK) {
124 async_answer_0(&call, rc);
125 async_answer_0(icall, rc);
126 return;
127 }
128
129 errno_t retval = async_data_read_finalize(&call, id_buf, size);
130 free(id_buf);
131
132 async_answer_1(icall, retval, act_size);
133}
134
135static void vol_part_add_srv(vol_parts_t *parts, ipc_call_t *icall)
136{
137 service_id_t sid;
138 errno_t rc;
139
140 sid = IPC_GET_ARG1(*icall);
141
142 rc = vol_part_add_part(parts, sid);
143 if (rc != EOK) {
144 async_answer_0(icall, rc);
145 return;
146 }
147
148 async_answer_0(icall, EOK);
149}
150
151static void vol_part_info_srv(vol_parts_t *parts, ipc_call_t *icall)
152{
153 service_id_t sid;
154 vol_part_t *part;
155 vol_part_info_t pinfo;
156 errno_t rc;
157
158 sid = IPC_GET_ARG1(*icall);
159 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_info_srv(%zu)",
160 sid);
161 rc = vol_part_find_by_id_ref(parts, sid, &part);
162 if (rc != EOK) {
163 async_answer_0(icall, ENOENT);
164 return;
165 }
166
167 rc = vol_part_get_info(part, &pinfo);
168 if (rc != EOK) {
169 async_answer_0(icall, EIO);
170 goto error;
171 }
172
173 ipc_call_t call;
174 size_t size;
175 if (!async_data_read_receive(&call, &size)) {
176 async_answer_0(&call, EREFUSED);
177 async_answer_0(icall, EREFUSED);
178 goto error;
179 }
180
181 if (size != sizeof(vol_part_info_t)) {
182 async_answer_0(&call, EINVAL);
183 async_answer_0(icall, EINVAL);
184 goto error;
185 }
186
187 rc = async_data_read_finalize(&call, &pinfo,
188 min(size, sizeof(pinfo)));
189 if (rc != EOK) {
190 async_answer_0(&call, rc);
191 async_answer_0(icall, rc);
192 goto error;
193 }
194
195 async_answer_0(icall, EOK);
196error:
197 vol_part_del_ref(part);
198}
199
200static void vol_part_eject_srv(vol_parts_t *parts, ipc_call_t *icall)
201{
202 service_id_t sid;
203 vol_part_t *part;
204 errno_t rc;
205
206 sid = IPC_GET_ARG1(*icall);
207 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_eject_srv(%zu)", sid);
208
209 rc = vol_part_find_by_id_ref(parts, sid, &part);
210 if (rc != EOK) {
211 async_answer_0(icall, ENOENT);
212 return;
213 }
214
215 rc = vol_part_eject_part(part);
216 if (rc != EOK) {
217 async_answer_0(icall, EIO);
218 goto error;
219 }
220
221 async_answer_0(icall, EOK);
222error:
223 vol_part_del_ref(part);
224}
225
226static void vol_part_insert_srv(vol_parts_t *parts, ipc_call_t *icall)
227{
228 service_id_t sid;
229 vol_part_t *part;
230 errno_t rc;
231
232 sid = IPC_GET_ARG1(*icall);
233 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_insert_srv(%zu)", sid);
234
235 rc = vol_part_find_by_id_ref(parts, sid, &part);
236 if (rc != EOK) {
237 async_answer_0(icall, ENOENT);
238 return;
239 }
240
241 rc = vol_part_insert_part(part);
242 if (rc != EOK) {
243 async_answer_0(icall, EIO);
244 goto error;
245 }
246
247 async_answer_0(icall, EOK);
248error:
249 vol_part_del_ref(part);
250}
251
252static void vol_part_empty_srv(vol_parts_t *parts, ipc_call_t *icall)
253{
254 service_id_t sid;
255 vol_part_t *part;
256 errno_t rc;
257
258 sid = IPC_GET_ARG1(*icall);
259 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_srv(%zu)", sid);
260
261 rc = vol_part_find_by_id_ref(parts, sid, &part);
262 if (rc != EOK) {
263 async_answer_0(icall, ENOENT);
264 return;
265 }
266
267 rc = vol_part_empty_part(part);
268 if (rc != EOK) {
269 async_answer_0(icall, EIO);
270 goto error;
271 }
272
273 async_answer_0(icall, EOK);
274error:
275 vol_part_del_ref(part);
276}
277
278static void vol_part_get_lsupp_srv(vol_parts_t *parts, ipc_call_t *icall)
279{
280 vol_fstype_t fstype;
281 vol_label_supp_t vlsupp;
282 errno_t rc;
283
284 fstype = IPC_GET_ARG1(*icall);
285 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_get_lsupp_srv(%u)",
286 fstype);
287
288 volsrv_part_get_lsupp(fstype, &vlsupp);
289
290 ipc_call_t call;
291 size_t size;
292 if (!async_data_read_receive(&call, &size)) {
293 async_answer_0(&call, EREFUSED);
294 async_answer_0(icall, EREFUSED);
295 return;
296 }
297
298 if (size != sizeof(vol_label_supp_t)) {
299 async_answer_0(&call, EINVAL);
300 async_answer_0(icall, EINVAL);
301 return;
302 }
303
304 rc = async_data_read_finalize(&call, &vlsupp,
305 min(size, sizeof(vlsupp)));
306 if (rc != EOK) {
307 async_answer_0(&call, rc);
308 async_answer_0(icall, rc);
309 return;
310 }
311
312 async_answer_0(icall, EOK);
313}
314
315static void vol_part_mkfs_srv(vol_parts_t *parts, ipc_call_t *icall)
316{
317 service_id_t sid;
318 vol_part_t *part;
319 vol_fstype_t fstype;
320 char *label = NULL;
321 char *mountp = NULL;
322 errno_t rc;
323
324 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv()");
325
326 sid = IPC_GET_ARG1(*icall);
327 fstype = IPC_GET_ARG2(*icall);
328
329 rc = async_data_write_accept((void **)&label, true, 0, VOL_LABEL_MAXLEN,
330 0, NULL);
331 if (rc != EOK) {
332 async_answer_0(icall, rc);
333 goto error;
334 }
335
336 if (label != NULL) {
337 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: label='%s'",
338 label);
339 }
340
341 rc = async_data_write_accept((void **)&mountp, true, 0, VOL_MOUNTP_MAXLEN,
342 0, NULL);
343 if (rc != EOK) {
344 async_answer_0(icall, rc);
345 goto error;
346 }
347
348 if (mountp != NULL) {
349 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: mountp='%s'",
350 mountp);
351 }
352
353 rc = vol_part_find_by_id_ref(parts, sid, &part);
354 if (rc != EOK) {
355 async_answer_0(icall, ENOENT);
356 goto error;
357 }
358
359 rc = vol_part_mkfs_part(part, fstype, label, mountp);
360 if (rc != EOK) {
361 async_answer_0(icall, rc);
362 vol_part_del_ref(part);
363 goto error;
364 }
365
366 free(label);
367 free(mountp);
368 async_answer_0(icall, EOK);
369
370 return;
371error:
372 if (label != NULL)
373 free(label);
374 if (mountp != NULL)
375 free(mountp);
376}
377
378static void vol_part_set_mountp_srv(vol_parts_t *parts,
379 ipc_call_t *icall)
380{
381 service_id_t sid;
382 vol_part_t *part;
383 char *mountp;
384 errno_t rc;
385
386 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_set_mountp_srv()");
387
388 sid = IPC_GET_ARG1(*icall);
389
390 rc = async_data_write_accept((void **)&mountp, true, 0,
391 VOL_MOUNTP_MAXLEN, 0, NULL);
392 if (rc != EOK) {
393 async_answer_0(icall, rc);
394 return;
395 }
396
397 if (mountp != NULL) {
398 log_msg(LOG_DEFAULT, LVL_NOTE,
399 "vol_part_set_mountp_srv: mountp='%s'", mountp);
400 }
401
402 rc = vol_part_find_by_id_ref(parts, sid, &part);
403 if (rc != EOK) {
404 free(mountp);
405 async_answer_0(icall, ENOENT);
406 return;
407 }
408
409 rc = vol_part_set_mountp_part(part, mountp);
410 if (rc != EOK) {
411 free(mountp);
412 async_answer_0(icall, rc);
413 vol_part_del_ref(part);
414 return;
415 }
416
417 free(mountp);
418 async_answer_0(icall, EOK);
419}
420
421static void vol_get_volumes_srv(vol_parts_t *parts, ipc_call_t *icall)
422{
423 ipc_call_t call;
424 size_t size;
425 size_t act_size;
426 errno_t rc;
427
428 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_get_volumes_srv()");
429
430 if (!async_data_read_receive(&call, &size)) {
431 async_answer_0(&call, EREFUSED);
432 async_answer_0(icall, EREFUSED);
433 return;
434 }
435
436 volume_id_t *id_buf = (volume_id_t *) malloc(size);
437 if (id_buf == NULL) {
438 async_answer_0(&call, ENOMEM);
439 async_answer_0(icall, ENOMEM);
440 return;
441 }
442
443 rc = vol_get_ids(parts->volumes, id_buf, size, &act_size);
444 if (rc != EOK) {
445 async_answer_0(&call, rc);
446 async_answer_0(icall, rc);
447 return;
448 }
449
450 errno_t retval = async_data_read_finalize(&call, id_buf, size);
451 free(id_buf);
452
453 async_answer_1(icall, retval, act_size);
454}
455
456static void vol_info_srv(vol_parts_t *parts, ipc_call_t *icall)
457{
458 volume_id_t vid;
459 vol_volume_t *volume;
460 vol_info_t vinfo;
461 errno_t rc;
462
463 vid.id = IPC_GET_ARG1(*icall);
464 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv(%zu)", vid.id);
465
466 rc = vol_volume_find_by_id_ref(parts->volumes, vid, &volume);
467 if (rc != EOK) {
468 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv: volume %zu not found",
469 vid.id);
470 async_answer_0(icall, ENOENT);
471 return;
472 }
473
474 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv: vol_volume_get_info");
475 rc = vol_volume_get_info(volume, &vinfo);
476 if (rc != EOK) {
477 async_answer_0(icall, EIO);
478 goto error;
479 }
480
481 ipc_call_t call;
482 size_t size;
483 if (!async_data_read_receive(&call, &size)) {
484 async_answer_0(&call, EREFUSED);
485 async_answer_0(icall, EREFUSED);
486 goto error;
487 }
488
489 if (size != sizeof(vol_info_t)) {
490 async_answer_0(&call, EINVAL);
491 async_answer_0(icall, EINVAL);
492 goto error;
493 }
494
495 rc = async_data_read_finalize(&call, &vinfo,
496 min(size, sizeof(vinfo)));
497 if (rc != EOK) {
498 async_answer_0(&call, rc);
499 async_answer_0(icall, rc);
500 goto error;
501 }
502
503 async_answer_0(icall, EOK);
504error:
505 vol_volume_del_ref(volume);
506}
507
508static void vol_client_conn(ipc_call_t *icall, void *arg)
509{
510 vol_parts_t *parts = (vol_parts_t *) arg;
511
512 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
513
514 /* Accept the connection */
515 async_answer_0(icall, EOK);
516
517 while (true) {
518 ipc_call_t call;
519 async_get_call(&call);
520 sysarg_t method = IPC_GET_IMETHOD(call);
521
522 if (!method) {
523 /* The other side has hung up */
524 async_answer_0(&call, EOK);
525 return;
526 }
527
528 switch (method) {
529 case VOL_GET_PARTS:
530 vol_get_parts_srv(parts, &call);
531 break;
532 case VOL_PART_ADD:
533 vol_part_add_srv(parts, &call);
534 break;
535 case VOL_PART_INFO:
536 vol_part_info_srv(parts, &call);
537 break;
538 case VOL_PART_EJECT:
539 vol_part_eject_srv(parts, &call);
540 break;
541 case VOL_PART_EMPTY:
542 vol_part_empty_srv(parts, &call);
543 break;
544 case VOL_PART_INSERT:
545 vol_part_insert_srv(parts, &call);
546 break;
547 case VOL_PART_LSUPP:
548 vol_part_get_lsupp_srv(parts, &call);
549 break;
550 case VOL_PART_MKFS:
551 vol_part_mkfs_srv(parts, &call);
552 break;
553 case VOL_PART_SET_MOUNTP:
554 vol_part_set_mountp_srv(parts, &call);
555 break;
556 case VOL_GET_VOLUMES:
557 vol_get_volumes_srv(parts, &call);
558 break;
559 case VOL_INFO:
560 vol_info_srv(parts, &call);
561 break;
562 default:
563 async_answer_0(&call, EINVAL);
564 }
565 }
566}
567
568int main(int argc, char *argv[])
569{
570 errno_t rc;
571
572 printf("%s: Volume service\n", NAME);
573
574 if (log_init(NAME) != EOK) {
575 printf(NAME ": Failed to initialize logging.\n");
576 return 1;
577 }
578
579 rc = vol_init();
580 if (rc != EOK)
581 return 1;
582
583 printf(NAME ": Accepting connections.\n");
584 task_retval(0);
585 async_manager();
586
587 return 0;
588}
589
590/** @}
591 */
Note: See TracBrowser for help on using the repository browser.