Handle both IDE channels in the same driver instance
[helenos.git] / uspace / srv / volsrv / volsrv.c
blob93a0e38f46e8049f9b431833bc6ff147ce5c04ef
1 /*
2 * Copyright (c) 2023 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 volsrv
30 * @{
32 /**
33 * @file Volume service
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>
49 #include "mkfs.h"
50 #include "part.h"
51 #include "volume.h"
53 #define NAME "volsrv"
55 const char *vol_cfg_file = "/cfg/volsrv.sif";
57 static void vol_client_conn(ipc_call_t *, void *);
59 static errno_t vol_init(void)
61 errno_t rc;
62 vol_volumes_t *volumes = NULL;
63 vol_parts_t *parts = NULL;
64 loc_srv_t *srv = NULL;
66 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_init()");
68 rc = vol_volumes_create(vol_cfg_file, &volumes);
69 if (rc != EOK)
70 goto error;
72 rc = vol_parts_create(volumes, &parts);
73 if (rc != EOK)
74 goto error;
76 rc = vol_part_discovery_start(parts);
77 if (rc != EOK)
78 goto error;
80 async_set_fallback_port_handler(vol_client_conn, parts);
82 rc = loc_server_register(NAME, &srv);
83 if (rc != EOK) {
84 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
85 rc = EEXIST;
88 service_id_t sid;
89 rc = loc_service_register(srv, SERVICE_NAME_VOLSRV, &sid);
90 if (rc != EOK) {
91 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
92 rc = EEXIST;
93 goto error;
96 return EOK;
97 error:
98 if (srv != NULL)
99 loc_server_unregister(srv);
100 vol_volumes_destroy(volumes);
101 vol_parts_destroy(parts);
102 return rc;
105 static void vol_get_parts_srv(vol_parts_t *parts, ipc_call_t *icall)
107 ipc_call_t call;
108 size_t size;
109 size_t act_size;
110 errno_t rc;
112 if (!async_data_read_receive(&call, &size)) {
113 async_answer_0(&call, EREFUSED);
114 async_answer_0(icall, EREFUSED);
115 return;
118 service_id_t *id_buf = (service_id_t *) malloc(size);
119 if (id_buf == NULL) {
120 async_answer_0(&call, ENOMEM);
121 async_answer_0(icall, ENOMEM);
122 return;
125 rc = vol_part_get_ids(parts, id_buf, size, &act_size);
126 if (rc != EOK) {
127 async_answer_0(&call, rc);
128 async_answer_0(icall, rc);
129 return;
132 errno_t retval = async_data_read_finalize(&call, id_buf, size);
133 free(id_buf);
135 async_answer_1(icall, retval, act_size);
138 static void vol_part_add_srv(vol_parts_t *parts, ipc_call_t *icall)
140 service_id_t sid;
141 errno_t rc;
143 sid = ipc_get_arg1(icall);
145 rc = vol_part_add_part(parts, sid);
146 if (rc != EOK) {
147 async_answer_0(icall, rc);
148 return;
151 async_answer_0(icall, EOK);
154 static void vol_part_info_srv(vol_parts_t *parts, ipc_call_t *icall)
156 service_id_t sid;
157 vol_part_t *part;
158 vol_part_info_t pinfo;
159 errno_t rc;
161 sid = ipc_get_arg1(icall);
162 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_info_srv(%zu)",
163 sid);
164 rc = vol_part_find_by_id_ref(parts, sid, &part);
165 if (rc != EOK) {
166 async_answer_0(icall, ENOENT);
167 return;
170 rc = vol_part_get_info(part, &pinfo);
171 if (rc != EOK) {
172 async_answer_0(icall, EIO);
173 goto error;
176 ipc_call_t call;
177 size_t size;
178 if (!async_data_read_receive(&call, &size)) {
179 async_answer_0(&call, EREFUSED);
180 async_answer_0(icall, EREFUSED);
181 goto error;
184 if (size != sizeof(vol_part_info_t)) {
185 async_answer_0(&call, EINVAL);
186 async_answer_0(icall, EINVAL);
187 goto error;
190 rc = async_data_read_finalize(&call, &pinfo,
191 min(size, sizeof(pinfo)));
192 if (rc != EOK) {
193 async_answer_0(&call, rc);
194 async_answer_0(icall, rc);
195 goto error;
198 async_answer_0(icall, EOK);
199 error:
200 vol_part_del_ref(part);
203 static void vol_part_eject_srv(vol_parts_t *parts, ipc_call_t *icall)
205 service_id_t sid;
206 vol_part_t *part;
207 errno_t rc;
209 sid = ipc_get_arg1(icall);
210 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_eject_srv(%zu)", sid);
212 rc = vol_part_find_by_id_ref(parts, sid, &part);
213 if (rc != EOK) {
214 async_answer_0(icall, ENOENT);
215 return;
218 rc = vol_part_eject_part(part);
219 if (rc != EOK) {
220 async_answer_0(icall, EIO);
221 goto error;
224 async_answer_0(icall, EOK);
225 error:
226 vol_part_del_ref(part);
229 static void vol_part_insert_srv(vol_parts_t *parts, ipc_call_t *icall)
231 service_id_t sid;
232 vol_part_t *part;
233 errno_t rc;
235 sid = ipc_get_arg1(icall);
236 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_insert_srv(%zu)", sid);
238 rc = vol_part_find_by_id_ref(parts, sid, &part);
239 if (rc != EOK) {
240 async_answer_0(icall, ENOENT);
241 return;
244 rc = vol_part_insert_part(part);
245 if (rc != EOK) {
246 async_answer_0(icall, EIO);
247 goto error;
250 async_answer_0(icall, EOK);
251 error:
252 vol_part_del_ref(part);
255 static void vol_part_insert_by_path_srv(vol_parts_t *parts, ipc_call_t *icall)
257 vol_part_t *part;
258 char *path = NULL;
259 errno_t rc;
261 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_insert_by_path_srv()");
263 rc = async_data_write_accept((void **)&path, true, 0, VOL_MOUNTP_MAXLEN,
264 0, NULL);
265 if (rc != EOK) {
266 async_answer_0(icall, rc);
267 goto error;
270 rc = vol_part_find_by_path_ref(parts, path, &part);
271 if (rc != EOK) {
272 async_answer_0(icall, ENOENT);
273 goto error;
276 rc = vol_part_insert_part(part);
277 if (rc != EOK) {
278 async_answer_0(icall, rc);
279 vol_part_del_ref(part);
280 goto error;
283 free(path);
284 vol_part_del_ref(part);
285 async_answer_0(icall, EOK);
287 return;
288 error:
289 if (path != NULL)
290 free(path);
293 static void vol_part_empty_srv(vol_parts_t *parts, ipc_call_t *icall)
295 service_id_t sid;
296 vol_part_t *part;
297 errno_t rc;
299 sid = ipc_get_arg1(icall);
300 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_srv(%zu)", sid);
302 rc = vol_part_find_by_id_ref(parts, sid, &part);
303 if (rc != EOK) {
304 async_answer_0(icall, ENOENT);
305 return;
308 rc = vol_part_empty_part(part);
309 if (rc != EOK) {
310 async_answer_0(icall, EIO);
311 goto error;
314 async_answer_0(icall, EOK);
315 error:
316 vol_part_del_ref(part);
319 static void vol_part_get_lsupp_srv(vol_parts_t *parts, ipc_call_t *icall)
321 vol_fstype_t fstype;
322 vol_label_supp_t vlsupp;
323 errno_t rc;
325 fstype = ipc_get_arg1(icall);
326 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_get_lsupp_srv(%u)",
327 fstype);
329 volsrv_part_get_lsupp(fstype, &vlsupp);
331 ipc_call_t call;
332 size_t size;
333 if (!async_data_read_receive(&call, &size)) {
334 async_answer_0(&call, EREFUSED);
335 async_answer_0(icall, EREFUSED);
336 return;
339 if (size != sizeof(vol_label_supp_t)) {
340 async_answer_0(&call, EINVAL);
341 async_answer_0(icall, EINVAL);
342 return;
345 rc = async_data_read_finalize(&call, &vlsupp,
346 min(size, sizeof(vlsupp)));
347 if (rc != EOK) {
348 async_answer_0(&call, rc);
349 async_answer_0(icall, rc);
350 return;
353 async_answer_0(icall, EOK);
356 static void vol_part_mkfs_srv(vol_parts_t *parts, ipc_call_t *icall)
358 service_id_t sid;
359 vol_part_t *part;
360 vol_fstype_t fstype;
361 char *label = NULL;
362 char *mountp = NULL;
363 errno_t rc;
365 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_srv()");
367 sid = ipc_get_arg1(icall);
368 fstype = ipc_get_arg2(icall);
370 rc = async_data_write_accept((void **)&label, true, 0, VOL_LABEL_MAXLEN,
371 0, NULL);
372 if (rc != EOK) {
373 async_answer_0(icall, rc);
374 goto error;
377 if (label != NULL) {
378 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_srv: label='%s'",
379 label);
382 rc = async_data_write_accept((void **)&mountp, true, 0, VOL_MOUNTP_MAXLEN,
383 0, NULL);
384 if (rc != EOK) {
385 async_answer_0(icall, rc);
386 goto error;
389 if (mountp != NULL) {
390 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_mkfs_srv: mountp='%s'",
391 mountp);
394 rc = vol_part_find_by_id_ref(parts, sid, &part);
395 if (rc != EOK) {
396 async_answer_0(icall, ENOENT);
397 goto error;
400 rc = vol_part_mkfs_part(part, fstype, label, mountp);
401 if (rc != EOK) {
402 async_answer_0(icall, rc);
403 vol_part_del_ref(part);
404 goto error;
407 free(label);
408 free(mountp);
409 async_answer_0(icall, EOK);
411 return;
412 error:
413 if (label != NULL)
414 free(label);
415 if (mountp != NULL)
416 free(mountp);
419 static void vol_part_set_mountp_srv(vol_parts_t *parts,
420 ipc_call_t *icall)
422 service_id_t sid;
423 vol_part_t *part;
424 char *mountp;
425 errno_t rc;
427 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_set_mountp_srv()");
429 sid = ipc_get_arg1(icall);
431 rc = async_data_write_accept((void **)&mountp, true, 0,
432 VOL_MOUNTP_MAXLEN, 0, NULL);
433 if (rc != EOK) {
434 async_answer_0(icall, rc);
435 return;
438 if (mountp != NULL) {
439 log_msg(LOG_DEFAULT, LVL_DEBUG,
440 "vol_part_set_mountp_srv: mountp='%s'", mountp);
443 rc = vol_part_find_by_id_ref(parts, sid, &part);
444 if (rc != EOK) {
445 free(mountp);
446 async_answer_0(icall, ENOENT);
447 return;
450 rc = vol_part_set_mountp_part(part, mountp);
451 if (rc != EOK) {
452 free(mountp);
453 async_answer_0(icall, rc);
454 vol_part_del_ref(part);
455 return;
458 free(mountp);
459 async_answer_0(icall, EOK);
462 static void vol_get_volumes_srv(vol_parts_t *parts, ipc_call_t *icall)
464 ipc_call_t call;
465 size_t size;
466 size_t act_size;
467 errno_t rc;
469 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_get_volumes_srv()");
471 if (!async_data_read_receive(&call, &size)) {
472 async_answer_0(&call, EREFUSED);
473 async_answer_0(icall, EREFUSED);
474 return;
477 volume_id_t *id_buf = (volume_id_t *) malloc(size);
478 if (id_buf == NULL) {
479 async_answer_0(&call, ENOMEM);
480 async_answer_0(icall, ENOMEM);
481 return;
484 rc = vol_get_ids(parts->volumes, id_buf, size, &act_size);
485 if (rc != EOK) {
486 async_answer_0(&call, rc);
487 async_answer_0(icall, rc);
488 return;
491 errno_t retval = async_data_read_finalize(&call, id_buf, size);
492 free(id_buf);
494 async_answer_1(icall, retval, act_size);
497 static void vol_info_srv(vol_parts_t *parts, ipc_call_t *icall)
499 volume_id_t vid;
500 vol_volume_t *volume;
501 vol_info_t vinfo;
502 errno_t rc;
504 vid.id = ipc_get_arg1(icall);
505 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv(%zu)", vid.id);
507 rc = vol_volume_find_by_id_ref(parts->volumes, vid, &volume);
508 if (rc != EOK) {
509 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv: volume %zu not found",
510 vid.id);
511 async_answer_0(icall, ENOENT);
512 return;
515 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv: vol_volume_get_info");
516 rc = vol_volume_get_info(volume, &vinfo);
517 if (rc != EOK) {
518 async_answer_0(icall, EIO);
519 goto error;
522 ipc_call_t call;
523 size_t size;
524 if (!async_data_read_receive(&call, &size)) {
525 async_answer_0(&call, EREFUSED);
526 async_answer_0(icall, EREFUSED);
527 goto error;
530 if (size != sizeof(vol_info_t)) {
531 async_answer_0(&call, EINVAL);
532 async_answer_0(icall, EINVAL);
533 goto error;
536 rc = async_data_read_finalize(&call, &vinfo,
537 min(size, sizeof(vinfo)));
538 if (rc != EOK) {
539 async_answer_0(&call, rc);
540 async_answer_0(icall, rc);
541 goto error;
544 async_answer_0(icall, EOK);
545 error:
546 vol_volume_del_ref(volume);
549 static void vol_client_conn(ipc_call_t *icall, void *arg)
551 vol_parts_t *parts = (vol_parts_t *) arg;
553 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
555 /* Accept the connection */
556 async_accept_0(icall);
558 while (true) {
559 ipc_call_t call;
560 async_get_call(&call);
561 sysarg_t method = ipc_get_imethod(&call);
563 if (!method) {
564 /* The other side has hung up */
565 async_answer_0(&call, EOK);
566 return;
569 switch (method) {
570 case VOL_GET_PARTS:
571 vol_get_parts_srv(parts, &call);
572 break;
573 case VOL_PART_ADD:
574 vol_part_add_srv(parts, &call);
575 break;
576 case VOL_PART_INFO:
577 vol_part_info_srv(parts, &call);
578 break;
579 case VOL_PART_EJECT:
580 vol_part_eject_srv(parts, &call);
581 break;
582 case VOL_PART_EMPTY:
583 vol_part_empty_srv(parts, &call);
584 break;
585 case VOL_PART_INSERT:
586 vol_part_insert_srv(parts, &call);
587 break;
588 case VOL_PART_INSERT_BY_PATH:
589 vol_part_insert_by_path_srv(parts, &call);
590 break;
591 case VOL_PART_LSUPP:
592 vol_part_get_lsupp_srv(parts, &call);
593 break;
594 case VOL_PART_MKFS:
595 vol_part_mkfs_srv(parts, &call);
596 break;
597 case VOL_PART_SET_MOUNTP:
598 vol_part_set_mountp_srv(parts, &call);
599 break;
600 case VOL_GET_VOLUMES:
601 vol_get_volumes_srv(parts, &call);
602 break;
603 case VOL_INFO:
604 vol_info_srv(parts, &call);
605 break;
606 default:
607 async_answer_0(&call, EINVAL);
612 int main(int argc, char *argv[])
614 errno_t rc;
616 printf("%s: Volume service\n", NAME);
618 if (log_init(NAME) != EOK) {
619 printf(NAME ": Failed to initialize logging.\n");
620 return 1;
623 rc = vol_init();
624 if (rc != EOK)
625 return 1;
627 printf(NAME ": Accepting connections.\n");
628 task_retval(0);
629 async_manager();
631 return 0;
634 /** @}