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 |
|
---|
55 | const char *vol_cfg_file = "/data/cfg/volsrv.sif";
|
---|
56 |
|
---|
57 | static void vol_client_conn(ipc_call_t *, void *);
|
---|
58 |
|
---|
59 | static 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;
|
---|
96 | error:
|
---|
97 | vol_volumes_destroy(volumes);
|
---|
98 | vol_parts_destroy(parts);
|
---|
99 | return rc;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static 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 |
|
---|
135 | static 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 |
|
---|
151 | static 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);
|
---|
196 | error:
|
---|
197 | vol_part_del_ref(part);
|
---|
198 | }
|
---|
199 |
|
---|
200 | static 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 | goto error;
|
---|
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);
|
---|
222 | error:
|
---|
223 | vol_part_del_ref(part);
|
---|
224 | }
|
---|
225 |
|
---|
226 | static void vol_part_empty_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_empty_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_empty_part(part);
|
---|
242 | if (rc != EOK) {
|
---|
243 | async_answer_0(icall, EIO);
|
---|
244 | goto error;
|
---|
245 | }
|
---|
246 |
|
---|
247 | async_answer_0(icall, EOK);
|
---|
248 | error:
|
---|
249 | vol_part_del_ref(part);
|
---|
250 | }
|
---|
251 |
|
---|
252 | static void vol_part_get_lsupp_srv(vol_parts_t *parts, ipc_call_t *icall)
|
---|
253 | {
|
---|
254 | vol_fstype_t fstype;
|
---|
255 | vol_label_supp_t vlsupp;
|
---|
256 | errno_t rc;
|
---|
257 |
|
---|
258 | fstype = IPC_GET_ARG1(*icall);
|
---|
259 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_get_lsupp_srv(%u)",
|
---|
260 | fstype);
|
---|
261 |
|
---|
262 | volsrv_part_get_lsupp(fstype, &vlsupp);
|
---|
263 |
|
---|
264 | ipc_call_t call;
|
---|
265 | size_t size;
|
---|
266 | if (!async_data_read_receive(&call, &size)) {
|
---|
267 | async_answer_0(&call, EREFUSED);
|
---|
268 | async_answer_0(icall, EREFUSED);
|
---|
269 | return;
|
---|
270 | }
|
---|
271 |
|
---|
272 | if (size != sizeof(vol_label_supp_t)) {
|
---|
273 | async_answer_0(&call, EINVAL);
|
---|
274 | async_answer_0(icall, EINVAL);
|
---|
275 | return;
|
---|
276 | }
|
---|
277 |
|
---|
278 | rc = async_data_read_finalize(&call, &vlsupp,
|
---|
279 | min(size, sizeof(vlsupp)));
|
---|
280 | if (rc != EOK) {
|
---|
281 | async_answer_0(&call, rc);
|
---|
282 | async_answer_0(icall, rc);
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | async_answer_0(icall, EOK);
|
---|
287 | }
|
---|
288 |
|
---|
289 | static void vol_part_mkfs_srv(vol_parts_t *parts, ipc_call_t *icall)
|
---|
290 | {
|
---|
291 | service_id_t sid;
|
---|
292 | vol_part_t *part;
|
---|
293 | vol_fstype_t fstype;
|
---|
294 | char *label = NULL;
|
---|
295 | char *mountp = NULL;
|
---|
296 | errno_t rc;
|
---|
297 |
|
---|
298 | log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv()");
|
---|
299 |
|
---|
300 | sid = IPC_GET_ARG1(*icall);
|
---|
301 | fstype = IPC_GET_ARG2(*icall);
|
---|
302 |
|
---|
303 | rc = async_data_write_accept((void **)&label, true, 0, VOL_LABEL_MAXLEN,
|
---|
304 | 0, NULL);
|
---|
305 | if (rc != EOK) {
|
---|
306 | async_answer_0(icall, rc);
|
---|
307 | goto error;
|
---|
308 | }
|
---|
309 |
|
---|
310 | if (label != NULL) {
|
---|
311 | log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: label='%s'",
|
---|
312 | label);
|
---|
313 | }
|
---|
314 |
|
---|
315 | rc = async_data_write_accept((void **)&mountp, true, 0, VOL_MOUNTP_MAXLEN,
|
---|
316 | 0, NULL);
|
---|
317 | if (rc != EOK) {
|
---|
318 | async_answer_0(icall, rc);
|
---|
319 | goto error;
|
---|
320 | }
|
---|
321 |
|
---|
322 | if (mountp != NULL) {
|
---|
323 | log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: mountp='%s'",
|
---|
324 | mountp);
|
---|
325 | }
|
---|
326 |
|
---|
327 | rc = vol_part_find_by_id_ref(parts, sid, &part);
|
---|
328 | if (rc != EOK) {
|
---|
329 | async_answer_0(icall, ENOENT);
|
---|
330 | goto error;
|
---|
331 | }
|
---|
332 |
|
---|
333 | rc = vol_part_mkfs_part(part, fstype, label, mountp);
|
---|
334 | if (rc != EOK) {
|
---|
335 | async_answer_0(icall, rc);
|
---|
336 | vol_part_del_ref(part);
|
---|
337 | goto error;
|
---|
338 | }
|
---|
339 |
|
---|
340 | free(label);
|
---|
341 | free(mountp);
|
---|
342 | async_answer_0(icall, EOK);
|
---|
343 |
|
---|
344 | return;
|
---|
345 | error:
|
---|
346 | if (label != NULL)
|
---|
347 | free(label);
|
---|
348 | if (mountp != NULL)
|
---|
349 | free(mountp);
|
---|
350 | }
|
---|
351 |
|
---|
352 | static void vol_part_set_mountp_srv(vol_parts_t *parts,
|
---|
353 | ipc_call_t *icall)
|
---|
354 | {
|
---|
355 | service_id_t sid;
|
---|
356 | vol_part_t *part;
|
---|
357 | char *mountp;
|
---|
358 | errno_t rc;
|
---|
359 |
|
---|
360 | log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_set_mountp_srv()");
|
---|
361 |
|
---|
362 | sid = IPC_GET_ARG1(*icall);
|
---|
363 |
|
---|
364 | rc = async_data_write_accept((void **)&mountp, true, 0,
|
---|
365 | VOL_MOUNTP_MAXLEN, 0, NULL);
|
---|
366 | if (rc != EOK) {
|
---|
367 | async_answer_0(icall, rc);
|
---|
368 | return;
|
---|
369 | }
|
---|
370 |
|
---|
371 | if (mountp != NULL) {
|
---|
372 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
---|
373 | "vol_part_set_mountp_srv: mountp='%s'", mountp);
|
---|
374 | }
|
---|
375 |
|
---|
376 | rc = vol_part_find_by_id_ref(parts, sid, &part);
|
---|
377 | if (rc != EOK) {
|
---|
378 | free(mountp);
|
---|
379 | async_answer_0(icall, ENOENT);
|
---|
380 | return;
|
---|
381 | }
|
---|
382 |
|
---|
383 | rc = vol_part_set_mountp_part(part, mountp);
|
---|
384 | if (rc != EOK) {
|
---|
385 | free(mountp);
|
---|
386 | async_answer_0(icall, rc);
|
---|
387 | vol_part_del_ref(part);
|
---|
388 | return;
|
---|
389 | }
|
---|
390 |
|
---|
391 | free(mountp);
|
---|
392 | async_answer_0(icall, EOK);
|
---|
393 | }
|
---|
394 |
|
---|
395 | static void vol_client_conn(ipc_call_t *icall, void *arg)
|
---|
396 | {
|
---|
397 | vol_parts_t *parts = (vol_parts_t *) arg;
|
---|
398 |
|
---|
399 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
|
---|
400 |
|
---|
401 | /* Accept the connection */
|
---|
402 | async_answer_0(icall, EOK);
|
---|
403 |
|
---|
404 | while (true) {
|
---|
405 | ipc_call_t call;
|
---|
406 | async_get_call(&call);
|
---|
407 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
408 |
|
---|
409 | if (!method) {
|
---|
410 | /* The other side has hung up */
|
---|
411 | async_answer_0(&call, EOK);
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | switch (method) {
|
---|
416 | case VOL_GET_PARTS:
|
---|
417 | vol_get_parts_srv(parts, &call);
|
---|
418 | break;
|
---|
419 | case VOL_PART_ADD:
|
---|
420 | vol_part_add_srv(parts, &call);
|
---|
421 | break;
|
---|
422 | case VOL_PART_INFO:
|
---|
423 | vol_part_info_srv(parts, &call);
|
---|
424 | break;
|
---|
425 | case VOL_PART_EJECT:
|
---|
426 | vol_part_eject_srv(parts, &call);
|
---|
427 | break;
|
---|
428 | case VOL_PART_EMPTY:
|
---|
429 | vol_part_empty_srv(parts, &call);
|
---|
430 | break;
|
---|
431 | case VOL_PART_LSUPP:
|
---|
432 | vol_part_get_lsupp_srv(parts, &call);
|
---|
433 | break;
|
---|
434 | case VOL_PART_MKFS:
|
---|
435 | vol_part_mkfs_srv(parts, &call);
|
---|
436 | break;
|
---|
437 | case VOL_PART_SET_MOUNTP:
|
---|
438 | vol_part_set_mountp_srv(parts, &call);
|
---|
439 | break;
|
---|
440 | default:
|
---|
441 | async_answer_0(&call, EINVAL);
|
---|
442 | }
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | int main(int argc, char *argv[])
|
---|
447 | {
|
---|
448 | errno_t rc;
|
---|
449 |
|
---|
450 | printf("%s: Volume service\n", NAME);
|
---|
451 |
|
---|
452 | if (log_init(NAME) != EOK) {
|
---|
453 | printf(NAME ": Failed to initialize logging.\n");
|
---|
454 | return 1;
|
---|
455 | }
|
---|
456 |
|
---|
457 | rc = vol_init();
|
---|
458 | if (rc != EOK)
|
---|
459 | return 1;
|
---|
460 |
|
---|
461 | printf(NAME ": Accepting connections.\n");
|
---|
462 | task_retval(0);
|
---|
463 | async_manager();
|
---|
464 |
|
---|
465 | return 0;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /** @}
|
---|
469 | */
|
---|