source: mainline/uspace/srv/volsrv/volsrv.c@ b82985e

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

Inserting volume by path.

  • Property mode set to 100644
File size: 13.2 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_insert_by_path_srv(vol_parts_t *parts, ipc_call_t *icall)
253{
254 vol_part_t *part;
255 char *path = NULL;
256 errno_t rc;
257
258 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_insert_by_path_srv()");
259
260 rc = async_data_write_accept((void **)&path, true, 0, VOL_MOUNTP_MAXLEN,
261 0, NULL);
262 if (rc != EOK) {
263 async_answer_0(icall, rc);
264 goto error;
265 }
266
267 rc = vol_part_find_by_path_ref(parts, path, &part);
268 if (rc != EOK) {
269 async_answer_0(icall, ENOENT);
270 goto error;
271 }
272
273 rc = vol_part_insert_part(part);
274 if (rc != EOK) {
275 async_answer_0(icall, rc);
276 vol_part_del_ref(part);
277 goto error;
278 }
279
280 free(path);
281 vol_part_del_ref(part);
282 async_answer_0(icall, EOK);
283
284 return;
285error:
286 if (path != NULL)
287 free(path);
288}
289
290
291static void vol_part_empty_srv(vol_parts_t *parts, ipc_call_t *icall)
292{
293 service_id_t sid;
294 vol_part_t *part;
295 errno_t rc;
296
297 sid = IPC_GET_ARG1(*icall);
298 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_srv(%zu)", sid);
299
300 rc = vol_part_find_by_id_ref(parts, sid, &part);
301 if (rc != EOK) {
302 async_answer_0(icall, ENOENT);
303 return;
304 }
305
306 rc = vol_part_empty_part(part);
307 if (rc != EOK) {
308 async_answer_0(icall, EIO);
309 goto error;
310 }
311
312 async_answer_0(icall, EOK);
313error:
314 vol_part_del_ref(part);
315}
316
317static void vol_part_get_lsupp_srv(vol_parts_t *parts, ipc_call_t *icall)
318{
319 vol_fstype_t fstype;
320 vol_label_supp_t vlsupp;
321 errno_t rc;
322
323 fstype = IPC_GET_ARG1(*icall);
324 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_get_lsupp_srv(%u)",
325 fstype);
326
327 volsrv_part_get_lsupp(fstype, &vlsupp);
328
329 ipc_call_t call;
330 size_t size;
331 if (!async_data_read_receive(&call, &size)) {
332 async_answer_0(&call, EREFUSED);
333 async_answer_0(icall, EREFUSED);
334 return;
335 }
336
337 if (size != sizeof(vol_label_supp_t)) {
338 async_answer_0(&call, EINVAL);
339 async_answer_0(icall, EINVAL);
340 return;
341 }
342
343 rc = async_data_read_finalize(&call, &vlsupp,
344 min(size, sizeof(vlsupp)));
345 if (rc != EOK) {
346 async_answer_0(&call, rc);
347 async_answer_0(icall, rc);
348 return;
349 }
350
351 async_answer_0(icall, EOK);
352}
353
354static void vol_part_mkfs_srv(vol_parts_t *parts, ipc_call_t *icall)
355{
356 service_id_t sid;
357 vol_part_t *part;
358 vol_fstype_t fstype;
359 char *label = NULL;
360 char *mountp = NULL;
361 errno_t rc;
362
363 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv()");
364
365 sid = IPC_GET_ARG1(*icall);
366 fstype = IPC_GET_ARG2(*icall);
367
368 rc = async_data_write_accept((void **)&label, true, 0, VOL_LABEL_MAXLEN,
369 0, NULL);
370 if (rc != EOK) {
371 async_answer_0(icall, rc);
372 goto error;
373 }
374
375 if (label != NULL) {
376 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: label='%s'",
377 label);
378 }
379
380 rc = async_data_write_accept((void **)&mountp, true, 0, VOL_MOUNTP_MAXLEN,
381 0, NULL);
382 if (rc != EOK) {
383 async_answer_0(icall, rc);
384 goto error;
385 }
386
387 if (mountp != NULL) {
388 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: mountp='%s'",
389 mountp);
390 }
391
392 rc = vol_part_find_by_id_ref(parts, sid, &part);
393 if (rc != EOK) {
394 async_answer_0(icall, ENOENT);
395 goto error;
396 }
397
398 rc = vol_part_mkfs_part(part, fstype, label, mountp);
399 if (rc != EOK) {
400 async_answer_0(icall, rc);
401 vol_part_del_ref(part);
402 goto error;
403 }
404
405 free(label);
406 free(mountp);
407 async_answer_0(icall, EOK);
408
409 return;
410error:
411 if (label != NULL)
412 free(label);
413 if (mountp != NULL)
414 free(mountp);
415}
416
417static void vol_part_set_mountp_srv(vol_parts_t *parts,
418 ipc_call_t *icall)
419{
420 service_id_t sid;
421 vol_part_t *part;
422 char *mountp;
423 errno_t rc;
424
425 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_set_mountp_srv()");
426
427 sid = IPC_GET_ARG1(*icall);
428
429 rc = async_data_write_accept((void **)&mountp, true, 0,
430 VOL_MOUNTP_MAXLEN, 0, NULL);
431 if (rc != EOK) {
432 async_answer_0(icall, rc);
433 return;
434 }
435
436 if (mountp != NULL) {
437 log_msg(LOG_DEFAULT, LVL_NOTE,
438 "vol_part_set_mountp_srv: mountp='%s'", mountp);
439 }
440
441 rc = vol_part_find_by_id_ref(parts, sid, &part);
442 if (rc != EOK) {
443 free(mountp);
444 async_answer_0(icall, ENOENT);
445 return;
446 }
447
448 rc = vol_part_set_mountp_part(part, mountp);
449 if (rc != EOK) {
450 free(mountp);
451 async_answer_0(icall, rc);
452 vol_part_del_ref(part);
453 return;
454 }
455
456 free(mountp);
457 async_answer_0(icall, EOK);
458}
459
460static void vol_get_volumes_srv(vol_parts_t *parts, ipc_call_t *icall)
461{
462 ipc_call_t call;
463 size_t size;
464 size_t act_size;
465 errno_t rc;
466
467 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_get_volumes_srv()");
468
469 if (!async_data_read_receive(&call, &size)) {
470 async_answer_0(&call, EREFUSED);
471 async_answer_0(icall, EREFUSED);
472 return;
473 }
474
475 volume_id_t *id_buf = (volume_id_t *) malloc(size);
476 if (id_buf == NULL) {
477 async_answer_0(&call, ENOMEM);
478 async_answer_0(icall, ENOMEM);
479 return;
480 }
481
482 rc = vol_get_ids(parts->volumes, id_buf, size, &act_size);
483 if (rc != EOK) {
484 async_answer_0(&call, rc);
485 async_answer_0(icall, rc);
486 return;
487 }
488
489 errno_t retval = async_data_read_finalize(&call, id_buf, size);
490 free(id_buf);
491
492 async_answer_1(icall, retval, act_size);
493}
494
495static void vol_info_srv(vol_parts_t *parts, ipc_call_t *icall)
496{
497 volume_id_t vid;
498 vol_volume_t *volume;
499 vol_info_t vinfo;
500 errno_t rc;
501
502 vid.id = IPC_GET_ARG1(*icall);
503 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv(%zu)", vid.id);
504
505 rc = vol_volume_find_by_id_ref(parts->volumes, vid, &volume);
506 if (rc != EOK) {
507 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv: volume %zu not found",
508 vid.id);
509 async_answer_0(icall, ENOENT);
510 return;
511 }
512
513 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_info_srv: vol_volume_get_info");
514 rc = vol_volume_get_info(volume, &vinfo);
515 if (rc != EOK) {
516 async_answer_0(icall, EIO);
517 goto error;
518 }
519
520 ipc_call_t call;
521 size_t size;
522 if (!async_data_read_receive(&call, &size)) {
523 async_answer_0(&call, EREFUSED);
524 async_answer_0(icall, EREFUSED);
525 goto error;
526 }
527
528 if (size != sizeof(vol_info_t)) {
529 async_answer_0(&call, EINVAL);
530 async_answer_0(icall, EINVAL);
531 goto error;
532 }
533
534 rc = async_data_read_finalize(&call, &vinfo,
535 min(size, sizeof(vinfo)));
536 if (rc != EOK) {
537 async_answer_0(&call, rc);
538 async_answer_0(icall, rc);
539 goto error;
540 }
541
542 async_answer_0(icall, EOK);
543error:
544 vol_volume_del_ref(volume);
545}
546
547static void vol_client_conn(ipc_call_t *icall, void *arg)
548{
549 vol_parts_t *parts = (vol_parts_t *) arg;
550
551 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
552
553 /* Accept the connection */
554 async_answer_0(icall, EOK);
555
556 while (true) {
557 ipc_call_t call;
558 async_get_call(&call);
559 sysarg_t method = IPC_GET_IMETHOD(call);
560
561 if (!method) {
562 /* The other side has hung up */
563 async_answer_0(&call, EOK);
564 return;
565 }
566
567 switch (method) {
568 case VOL_GET_PARTS:
569 vol_get_parts_srv(parts, &call);
570 break;
571 case VOL_PART_ADD:
572 vol_part_add_srv(parts, &call);
573 break;
574 case VOL_PART_INFO:
575 vol_part_info_srv(parts, &call);
576 break;
577 case VOL_PART_EJECT:
578 vol_part_eject_srv(parts, &call);
579 break;
580 case VOL_PART_EMPTY:
581 vol_part_empty_srv(parts, &call);
582 break;
583 case VOL_PART_INSERT:
584 vol_part_insert_srv(parts, &call);
585 break;
586 case VOL_PART_INSERT_BY_PATH:
587 vol_part_insert_by_path_srv(parts, &call);
588 break;
589 case VOL_PART_LSUPP:
590 vol_part_get_lsupp_srv(parts, &call);
591 break;
592 case VOL_PART_MKFS:
593 vol_part_mkfs_srv(parts, &call);
594 break;
595 case VOL_PART_SET_MOUNTP:
596 vol_part_set_mountp_srv(parts, &call);
597 break;
598 case VOL_GET_VOLUMES:
599 vol_get_volumes_srv(parts, &call);
600 break;
601 case VOL_INFO:
602 vol_info_srv(parts, &call);
603 break;
604 default:
605 async_answer_0(&call, EINVAL);
606 }
607 }
608}
609
610int main(int argc, char *argv[])
611{
612 errno_t rc;
613
614 printf("%s: Volume service\n", NAME);
615
616 if (log_init(NAME) != EOK) {
617 printf(NAME ": Failed to initialize logging.\n");
618 return 1;
619 }
620
621 rc = vol_init();
622 if (rc != EOK)
623 return 1;
624
625 printf(NAME ": Accepting connections.\n");
626 task_retval(0);
627 async_manager();
628
629 return 0;
630}
631
632/** @}
633 */
Note: See TracBrowser for help on using the repository browser.