1 | /*
|
---|
2 | * Copyright (c) 2025 Miroslav Cimerman
|
---|
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 hr
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file hr.c
|
---|
34 | * @brief HelenRAID server methods.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <bd_srv.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <hr.h>
|
---|
42 | #include <io/log.h>
|
---|
43 | #include <ipc/hr.h>
|
---|
44 | #include <ipc/services.h>
|
---|
45 | #include <loc.h>
|
---|
46 | #include <task.h>
|
---|
47 | #include <stdio.h>
|
---|
48 | #include <stdlib.h>
|
---|
49 | #include <str.h>
|
---|
50 | #include <str_error.h>
|
---|
51 | #include <block.h>
|
---|
52 |
|
---|
53 | #include "util.h"
|
---|
54 | #include "var.h"
|
---|
55 |
|
---|
56 | static void hr_assemble_srv(ipc_call_t *);
|
---|
57 | static void hr_auto_assemble_srv(ipc_call_t *);
|
---|
58 | static void hr_stop_srv(ipc_call_t *);
|
---|
59 | static void hr_stop_all_srv(ipc_call_t *);
|
---|
60 | static void hr_add_hotspare_srv(ipc_call_t *);
|
---|
61 | static void hr_get_vol_states_srv(ipc_call_t *);
|
---|
62 | static void hr_ctl_conn(ipc_call_t *);
|
---|
63 | static void hr_call_handler(ipc_call_t *, void *);
|
---|
64 |
|
---|
65 | loc_srv_t *hr_srv;
|
---|
66 | list_t hr_volumes;
|
---|
67 | fibril_rwlock_t hr_volumes_lock;
|
---|
68 |
|
---|
69 | static service_id_t ctl_sid;
|
---|
70 |
|
---|
71 | /** Volume creation (server).
|
---|
72 | *
|
---|
73 | * Creates HelenRAID volume from parameters and
|
---|
74 | * devices specified in hr_config_t.
|
---|
75 | *
|
---|
76 | * @param icall hr_config_t
|
---|
77 | */
|
---|
78 | static void hr_create_srv(ipc_call_t *icall)
|
---|
79 | {
|
---|
80 | HR_DEBUG("%s()", __func__);
|
---|
81 |
|
---|
82 | errno_t rc;
|
---|
83 | size_t i, size;
|
---|
84 | hr_config_t *cfg;
|
---|
85 | hr_volume_t *vol;
|
---|
86 | ipc_call_t call;
|
---|
87 |
|
---|
88 | if (!async_data_write_receive(&call, &size)) {
|
---|
89 | async_answer_0(&call, EREFUSED);
|
---|
90 | async_answer_0(icall, EREFUSED);
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (size != sizeof(hr_config_t)) {
|
---|
95 | async_answer_0(&call, EINVAL);
|
---|
96 | async_answer_0(icall, EINVAL);
|
---|
97 | return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | cfg = calloc(1, sizeof(hr_config_t));
|
---|
101 | if (cfg == NULL) {
|
---|
102 | async_answer_0(&call, ENOMEM);
|
---|
103 | async_answer_0(icall, ENOMEM);
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | rc = async_data_write_finalize(&call, cfg, size);
|
---|
108 | if (rc != EOK) {
|
---|
109 | free(cfg);
|
---|
110 | async_answer_0(&call, rc);
|
---|
111 | async_answer_0(icall, rc);
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (cfg->dev_no > HR_MAX_EXTENTS) {
|
---|
116 | HR_ERROR("provided %u devices (max = %u)",
|
---|
117 | (unsigned)cfg->dev_no, HR_MAX_EXTENTS);
|
---|
118 | free(cfg);
|
---|
119 | async_answer_0(icall, ELIMIT);
|
---|
120 | return;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * If there was a missing device provided
|
---|
125 | * for creation of a new volume, abort
|
---|
126 | */
|
---|
127 | for (i = 0; i < cfg->dev_no; i++) {
|
---|
128 | if (cfg->devs[i] == 0) {
|
---|
129 | HR_ERROR("missing device provided for volume "
|
---|
130 | "creation, aborting");
|
---|
131 | free(cfg);
|
---|
132 | async_answer_0(icall, EINVAL);
|
---|
133 | return;
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | hr_metadata_type_t meta_type;
|
---|
138 | if (cfg->vol_flags & HR_VOL_FLAG_NOOP_META)
|
---|
139 | meta_type = HR_METADATA_NOOP;
|
---|
140 | else
|
---|
141 | meta_type = HR_METADATA_NATIVE;
|
---|
142 |
|
---|
143 | rc = hr_create_vol_struct(&vol, cfg->level, cfg->devname, meta_type,
|
---|
144 | cfg->vol_flags);
|
---|
145 | if (rc != EOK) {
|
---|
146 | free(cfg);
|
---|
147 | async_answer_0(icall, rc);
|
---|
148 | return;
|
---|
149 | }
|
---|
150 |
|
---|
151 | rc = hr_init_extents_from_cfg(vol, cfg);
|
---|
152 | if (rc != EOK)
|
---|
153 | goto error;
|
---|
154 |
|
---|
155 | vol->hr_ops.init(vol);
|
---|
156 | if (rc != EOK)
|
---|
157 | goto error;
|
---|
158 |
|
---|
159 | rc = vol->meta_ops->init_vol2meta(vol);
|
---|
160 | if (rc != EOK)
|
---|
161 | goto error;
|
---|
162 |
|
---|
163 | rc = vol->hr_ops.create(vol);
|
---|
164 | if (rc != EOK)
|
---|
165 | goto error;
|
---|
166 |
|
---|
167 | vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
|
---|
168 |
|
---|
169 | rc = hr_register_volume(vol);
|
---|
170 | if (rc != EOK)
|
---|
171 | goto error;
|
---|
172 |
|
---|
173 | fibril_rwlock_write_lock(&hr_volumes_lock);
|
---|
174 | list_append(&vol->lvolumes, &hr_volumes);
|
---|
175 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
176 |
|
---|
177 | HR_NOTE("created volume \"%s\" (%" PRIun ")\n", vol->devname,
|
---|
178 | vol->svc_id);
|
---|
179 |
|
---|
180 | free(cfg);
|
---|
181 | async_answer_0(icall, rc);
|
---|
182 | return;
|
---|
183 | error:
|
---|
184 | free(cfg);
|
---|
185 | hr_destroy_vol_struct(vol);
|
---|
186 | async_answer_0(icall, rc);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /** Manual volume assembly (server).
|
---|
190 | *
|
---|
191 | * Tries to assemble a volume from devices in hr_config_t and
|
---|
192 | * sends the number of successful volumes assembled back to the
|
---|
193 | * client.
|
---|
194 | *
|
---|
195 | * @param icall hr_config_t
|
---|
196 | */
|
---|
197 | static void hr_assemble_srv(ipc_call_t *icall)
|
---|
198 | {
|
---|
199 | HR_DEBUG("%s()", __func__);
|
---|
200 |
|
---|
201 | errno_t rc;
|
---|
202 | size_t size, assembled_cnt;
|
---|
203 | hr_config_t *cfg;
|
---|
204 | ipc_call_t call;
|
---|
205 |
|
---|
206 | if (!async_data_write_receive(&call, &size)) {
|
---|
207 | async_answer_0(&call, EREFUSED);
|
---|
208 | async_answer_0(icall, EREFUSED);
|
---|
209 | return;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (size != sizeof(hr_config_t)) {
|
---|
213 | async_answer_0(&call, EINVAL);
|
---|
214 | async_answer_0(icall, EINVAL);
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | cfg = calloc(1, sizeof(hr_config_t));
|
---|
219 | if (cfg == NULL) {
|
---|
220 | async_answer_0(&call, ENOMEM);
|
---|
221 | async_answer_0(icall, ENOMEM);
|
---|
222 | return;
|
---|
223 | }
|
---|
224 |
|
---|
225 | rc = async_data_write_finalize(&call, cfg, size);
|
---|
226 | if (rc != EOK)
|
---|
227 | goto error;
|
---|
228 |
|
---|
229 | if (!async_data_read_receive(&call, &size)) {
|
---|
230 | async_answer_0(icall, EREFUSED);
|
---|
231 | return;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (size != sizeof(size_t)) {
|
---|
235 | async_answer_0(icall, EINVAL);
|
---|
236 | return;
|
---|
237 | }
|
---|
238 |
|
---|
239 | rc = hr_util_try_assemble(cfg, &assembled_cnt);
|
---|
240 | if (rc != EOK)
|
---|
241 | goto error;
|
---|
242 |
|
---|
243 | rc = async_data_read_finalize(&call, &assembled_cnt, size);
|
---|
244 | if (rc != EOK)
|
---|
245 | goto error;
|
---|
246 |
|
---|
247 | free(cfg);
|
---|
248 | async_answer_0(icall, EOK);
|
---|
249 | return;
|
---|
250 | error:
|
---|
251 | free(cfg);
|
---|
252 | async_answer_0(&call, rc);
|
---|
253 | async_answer_0(icall, rc);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** Automatic volume assembly (server).
|
---|
257 | *
|
---|
258 | * Tries to assemble a volume from devices in disk location
|
---|
259 | * category and sends the number of successful volumes assembled
|
---|
260 | * back to client.
|
---|
261 | */
|
---|
262 | static void hr_auto_assemble_srv(ipc_call_t *icall)
|
---|
263 | {
|
---|
264 | HR_DEBUG("%s()", __func__);
|
---|
265 |
|
---|
266 | errno_t rc;
|
---|
267 | size_t size;
|
---|
268 | size_t assembled_cnt = 0;
|
---|
269 | ipc_call_t call;
|
---|
270 |
|
---|
271 | if (!async_data_read_receive(&call, &size)) {
|
---|
272 | async_answer_0(icall, EREFUSED);
|
---|
273 | return;
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (size != sizeof(size_t)) {
|
---|
277 | async_answer_0(&call, EINVAL);
|
---|
278 | async_answer_0(icall, EINVAL);
|
---|
279 | return;
|
---|
280 | }
|
---|
281 |
|
---|
282 | rc = hr_util_try_assemble(NULL, &assembled_cnt);
|
---|
283 | if (rc != EOK)
|
---|
284 | goto error;
|
---|
285 |
|
---|
286 | rc = async_data_read_finalize(&call, &assembled_cnt, size);
|
---|
287 | if (rc != EOK)
|
---|
288 | goto error;
|
---|
289 |
|
---|
290 | async_answer_0(icall, EOK);
|
---|
291 | return;
|
---|
292 | error:
|
---|
293 | async_answer_0(&call, rc);
|
---|
294 | async_answer_0(icall, rc);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** Volume deactivation (server).
|
---|
298 | *
|
---|
299 | * Deactivates/detaches specified volume.
|
---|
300 | */
|
---|
301 | static void hr_stop_srv(ipc_call_t *icall)
|
---|
302 | {
|
---|
303 | HR_DEBUG("%s()", __func__);
|
---|
304 |
|
---|
305 | errno_t rc = EOK;
|
---|
306 | service_id_t svc_id;
|
---|
307 |
|
---|
308 | svc_id = ipc_get_arg1(icall);
|
---|
309 |
|
---|
310 | rc = hr_remove_volume(svc_id);
|
---|
311 |
|
---|
312 | async_answer_0(icall, rc);
|
---|
313 | }
|
---|
314 |
|
---|
315 | /** Automatic volume deactivation (server).
|
---|
316 | *
|
---|
317 | * Tries to deactivate/detach all volumes.
|
---|
318 | */
|
---|
319 | static void hr_stop_all_srv(ipc_call_t *icall)
|
---|
320 | {
|
---|
321 | HR_DEBUG("%s()", __func__);
|
---|
322 |
|
---|
323 | service_id_t *vol_svcs = NULL;
|
---|
324 | errno_t rc = EOK;
|
---|
325 | size_t i, vol_cnt;
|
---|
326 |
|
---|
327 | rc = hr_get_volume_svcs(&vol_cnt, &vol_svcs);
|
---|
328 | if (rc != EOK)
|
---|
329 | goto fail;
|
---|
330 |
|
---|
331 | for (i = 0; i < vol_cnt; i++) {
|
---|
332 | errno_t rc2 = hr_remove_volume(vol_svcs[i]);
|
---|
333 | if (rc2 == EBUSY)
|
---|
334 | rc = EBUSY;
|
---|
335 | }
|
---|
336 |
|
---|
337 | fail:
|
---|
338 | if (vol_svcs != NULL)
|
---|
339 | free(vol_svcs);
|
---|
340 | async_answer_0(icall, rc);
|
---|
341 | }
|
---|
342 |
|
---|
343 | /** Simulate volume extent failure (server).
|
---|
344 | *
|
---|
345 | * Changes the specified extent's state to FAULTY.
|
---|
346 | * Other extents' metadata are marked as dirty, therefore
|
---|
347 | * it effectively invalides the specified extent as well
|
---|
348 | * for further uses.
|
---|
349 | */
|
---|
350 | static void hr_fail_extent_srv(ipc_call_t *icall)
|
---|
351 | {
|
---|
352 | HR_DEBUG("%s()", __func__);
|
---|
353 |
|
---|
354 | service_id_t svc_id;
|
---|
355 | size_t extent_idx_to_fail;
|
---|
356 | hr_volume_t *vol;
|
---|
357 |
|
---|
358 | svc_id = (service_id_t)ipc_get_arg1(icall);
|
---|
359 | extent_idx_to_fail = (size_t)ipc_get_arg2(icall);
|
---|
360 |
|
---|
361 | vol = hr_get_volume(svc_id);
|
---|
362 | if (vol == NULL) {
|
---|
363 | async_answer_0(icall, ENOENT);
|
---|
364 | return;
|
---|
365 | }
|
---|
366 |
|
---|
367 | fibril_rwlock_write_lock(&vol->extents_lock);
|
---|
368 | fibril_rwlock_write_lock(&vol->states_lock);
|
---|
369 |
|
---|
370 | hr_extent_t *ext = &vol->extents[extent_idx_to_fail];
|
---|
371 |
|
---|
372 | switch (ext->state) {
|
---|
373 | case HR_EXT_NONE:
|
---|
374 | case HR_EXT_MISSING:
|
---|
375 | case HR_EXT_FAILED:
|
---|
376 | fibril_rwlock_write_unlock(&vol->states_lock);
|
---|
377 | fibril_rwlock_write_unlock(&vol->extents_lock);
|
---|
378 | async_answer_0(icall, EINVAL);
|
---|
379 | return;
|
---|
380 | default:
|
---|
381 | hr_update_ext_state(vol, extent_idx_to_fail, HR_EXT_FAILED);
|
---|
382 | (void)vol->meta_ops->erase_block(ext->svc_id);
|
---|
383 | block_fini(ext->svc_id);
|
---|
384 | ext->svc_id = 0;
|
---|
385 | hr_mark_vol_state_dirty(vol);
|
---|
386 | }
|
---|
387 |
|
---|
388 | fibril_rwlock_write_unlock(&vol->states_lock);
|
---|
389 | fibril_rwlock_write_unlock(&vol->extents_lock);
|
---|
390 |
|
---|
391 | vol->hr_ops.vol_state_eval(vol);
|
---|
392 |
|
---|
393 | async_answer_0(icall, EOK);
|
---|
394 | }
|
---|
395 |
|
---|
396 | /** Add hotspare to volume (server).
|
---|
397 | *
|
---|
398 | * Adds hotspare to a volume.
|
---|
399 | */
|
---|
400 | static void hr_add_hotspare_srv(ipc_call_t *icall)
|
---|
401 | {
|
---|
402 | HR_DEBUG("%s()", __func__);
|
---|
403 |
|
---|
404 | errno_t rc = EOK;
|
---|
405 | service_id_t vol_svc_id;
|
---|
406 | service_id_t hotspare;
|
---|
407 | hr_volume_t *vol;
|
---|
408 |
|
---|
409 | vol_svc_id = ipc_get_arg1(icall);
|
---|
410 | hotspare = ipc_get_arg2(icall);
|
---|
411 |
|
---|
412 | vol = hr_get_volume(vol_svc_id);
|
---|
413 | if (vol == NULL) {
|
---|
414 | async_answer_0(icall, ENOENT);
|
---|
415 | return;
|
---|
416 | }
|
---|
417 |
|
---|
418 | if (vol->level == HR_LVL_0) {
|
---|
419 | HR_NOTE("hotspare not supported on RAID level = %s\n",
|
---|
420 | hr_get_level_str(vol->level));
|
---|
421 | async_answer_0(icall, ENOTSUP);
|
---|
422 | return;
|
---|
423 | }
|
---|
424 |
|
---|
425 | if (!(vol->meta_ops->get_flags() & HR_METADATA_HOTSPARE_SUPPORT)) {
|
---|
426 | HR_NOTE("hotspare not supported on metadata type = %s\n",
|
---|
427 | hr_get_metadata_type_str(vol->meta_ops->get_type()));
|
---|
428 | async_answer_0(icall, ENOTSUP);
|
---|
429 | return;
|
---|
430 | }
|
---|
431 |
|
---|
432 | rc = hr_util_add_hotspare(vol, hotspare);
|
---|
433 |
|
---|
434 | vol->hr_ops.vol_state_eval(vol);
|
---|
435 |
|
---|
436 | async_answer_0(icall, rc);
|
---|
437 | }
|
---|
438 |
|
---|
439 | /** Send volume states.
|
---|
440 | *
|
---|
441 | * Sends the client pairs of (volume service_id, state).
|
---|
442 | */
|
---|
443 | static void hr_get_vol_states_srv(ipc_call_t *icall)
|
---|
444 | {
|
---|
445 | HR_DEBUG("%s()", __func__);
|
---|
446 |
|
---|
447 | errno_t rc;
|
---|
448 | size_t vol_cnt = 0;
|
---|
449 | hr_pair_vol_state_t pair;
|
---|
450 | ipc_call_t call;
|
---|
451 | size_t size;
|
---|
452 |
|
---|
453 | fibril_rwlock_read_lock(&hr_volumes_lock);
|
---|
454 |
|
---|
455 | vol_cnt = list_count(&hr_volumes);
|
---|
456 |
|
---|
457 | if (!async_data_read_receive(&call, &size)) {
|
---|
458 | rc = EREFUSED;
|
---|
459 | goto error;
|
---|
460 | }
|
---|
461 |
|
---|
462 | if (size != sizeof(vol_cnt)) {
|
---|
463 | rc = EINVAL;
|
---|
464 | goto error;
|
---|
465 | }
|
---|
466 |
|
---|
467 | rc = async_data_read_finalize(&call, &vol_cnt, size);
|
---|
468 | if (rc != EOK)
|
---|
469 | goto error;
|
---|
470 |
|
---|
471 | list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
|
---|
472 | pair.svc_id = vol->svc_id;
|
---|
473 | pair.state = vol->state;
|
---|
474 |
|
---|
475 | if (!async_data_read_receive(&call, &size)) {
|
---|
476 | rc = EREFUSED;
|
---|
477 | goto error;
|
---|
478 | }
|
---|
479 |
|
---|
480 | if (size != sizeof(pair)) {
|
---|
481 | rc = EINVAL;
|
---|
482 | goto error;
|
---|
483 | }
|
---|
484 |
|
---|
485 | rc = async_data_read_finalize(&call, &pair, size);
|
---|
486 | if (rc != EOK)
|
---|
487 | goto error;
|
---|
488 | }
|
---|
489 |
|
---|
490 | fibril_rwlock_read_unlock(&hr_volumes_lock);
|
---|
491 | async_answer_0(icall, EOK);
|
---|
492 | return;
|
---|
493 | error:
|
---|
494 | fibril_rwlock_read_unlock(&hr_volumes_lock);
|
---|
495 | async_answer_0(&call, rc);
|
---|
496 | async_answer_0(icall, rc);
|
---|
497 | }
|
---|
498 |
|
---|
499 | /** Send volume info.
|
---|
500 | *
|
---|
501 | * Sends the client volume info.
|
---|
502 | */
|
---|
503 | static void hr_get_vol_info_srv(ipc_call_t *icall)
|
---|
504 | {
|
---|
505 | HR_DEBUG("%s()", __func__);
|
---|
506 |
|
---|
507 | errno_t rc;
|
---|
508 | size_t size;
|
---|
509 | ipc_call_t call;
|
---|
510 | service_id_t svc_id;
|
---|
511 | hr_vol_info_t info;
|
---|
512 | hr_volume_t *vol;
|
---|
513 |
|
---|
514 | if (!async_data_write_receive(&call, &size)) {
|
---|
515 | rc = EREFUSED;
|
---|
516 | goto error;
|
---|
517 | }
|
---|
518 |
|
---|
519 | if (size != sizeof(service_id_t)) {
|
---|
520 | rc = EINVAL;
|
---|
521 | goto error;
|
---|
522 | }
|
---|
523 |
|
---|
524 | rc = async_data_write_finalize(&call, &svc_id, size);
|
---|
525 | if (rc != EOK)
|
---|
526 | goto error;
|
---|
527 |
|
---|
528 | vol = hr_get_volume(svc_id);
|
---|
529 | if (vol == NULL) {
|
---|
530 | rc = ENOENT;
|
---|
531 | goto error;
|
---|
532 | }
|
---|
533 |
|
---|
534 | memcpy(info.extents, vol->extents,
|
---|
535 | sizeof(hr_extent_t) * HR_MAX_EXTENTS);
|
---|
536 | memcpy(info.hotspares, vol->hotspares,
|
---|
537 | sizeof(hr_extent_t) * HR_MAX_HOTSPARES);
|
---|
538 | info.svc_id = vol->svc_id;
|
---|
539 | info.extent_no = vol->extent_no;
|
---|
540 | info.hotspare_no = vol->hotspare_no;
|
---|
541 | info.level = vol->level;
|
---|
542 | info.data_blkno = vol->data_blkno;
|
---|
543 | info.rebuild_blk = vol->rebuild_blk;
|
---|
544 | info.strip_size = vol->strip_size;
|
---|
545 | info.bsize = vol->bsize;
|
---|
546 | info.state = vol->state;
|
---|
547 | info.layout = vol->layout;
|
---|
548 | info.meta_type = vol->meta_ops->get_type();
|
---|
549 | memcpy(info.devname, vol->devname, HR_DEVNAME_LEN);
|
---|
550 | info.vflags = vol->vflags;
|
---|
551 |
|
---|
552 | if (!async_data_read_receive(&call, &size)) {
|
---|
553 | rc = EREFUSED;
|
---|
554 | goto error;
|
---|
555 | }
|
---|
556 |
|
---|
557 | if (size != sizeof(info)) {
|
---|
558 | rc = EINVAL;
|
---|
559 | goto error;
|
---|
560 | }
|
---|
561 |
|
---|
562 | rc = async_data_read_finalize(&call, &info, size);
|
---|
563 | if (rc != EOK)
|
---|
564 | goto error;
|
---|
565 |
|
---|
566 | async_answer_0(icall, EOK);
|
---|
567 | return;
|
---|
568 | error:
|
---|
569 | async_answer_0(&call, rc);
|
---|
570 | async_answer_0(icall, rc);
|
---|
571 | }
|
---|
572 |
|
---|
573 | /** HelenRAID server control IPC methods crossroad.
|
---|
574 | */
|
---|
575 | static void hr_ctl_conn(ipc_call_t *icall)
|
---|
576 | {
|
---|
577 | HR_DEBUG("%s()", __func__);
|
---|
578 |
|
---|
579 | async_accept_0(icall);
|
---|
580 |
|
---|
581 | while (true) {
|
---|
582 | ipc_call_t call;
|
---|
583 | async_get_call(&call);
|
---|
584 | sysarg_t method = ipc_get_imethod(&call);
|
---|
585 |
|
---|
586 | if (!method) {
|
---|
587 | async_answer_0(&call, EOK);
|
---|
588 | return;
|
---|
589 | }
|
---|
590 |
|
---|
591 | switch (method) {
|
---|
592 | case HR_CREATE:
|
---|
593 | hr_create_srv(&call);
|
---|
594 | break;
|
---|
595 | case HR_ASSEMBLE:
|
---|
596 | hr_assemble_srv(&call);
|
---|
597 | break;
|
---|
598 | case HR_AUTO_ASSEMBLE:
|
---|
599 | hr_auto_assemble_srv(&call);
|
---|
600 | break;
|
---|
601 | case HR_STOP:
|
---|
602 | hr_stop_srv(&call);
|
---|
603 | break;
|
---|
604 | case HR_STOP_ALL:
|
---|
605 | hr_stop_all_srv(&call);
|
---|
606 | break;
|
---|
607 | case HR_FAIL_EXTENT:
|
---|
608 | hr_fail_extent_srv(&call);
|
---|
609 | break;
|
---|
610 | case HR_ADD_HOTSPARE:
|
---|
611 | hr_add_hotspare_srv(&call);
|
---|
612 | break;
|
---|
613 | case HR_GET_VOL_STATES:
|
---|
614 | hr_get_vol_states_srv(&call);
|
---|
615 | break;
|
---|
616 | case HR_GET_VOL_INFO:
|
---|
617 | hr_get_vol_info_srv(&call);
|
---|
618 | break;
|
---|
619 | default:
|
---|
620 | async_answer_0(&call, EINVAL);
|
---|
621 | }
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | /** HelenRAID server IPC method crossroad.
|
---|
626 | *
|
---|
627 | * Distinguishes between control IPC and block device
|
---|
628 | * IPC calls.
|
---|
629 | */
|
---|
630 | static void hr_call_handler(ipc_call_t *icall, void *arg)
|
---|
631 | {
|
---|
632 | HR_DEBUG("%s()", __func__);
|
---|
633 |
|
---|
634 | hr_volume_t *vol;
|
---|
635 |
|
---|
636 | service_id_t svc_id = ipc_get_arg2(icall);
|
---|
637 |
|
---|
638 | if (svc_id == ctl_sid) {
|
---|
639 | hr_ctl_conn(icall);
|
---|
640 | } else {
|
---|
641 | vol = hr_get_volume(svc_id);
|
---|
642 | if (vol == NULL) {
|
---|
643 | async_answer_0(icall, ENOENT);
|
---|
644 | return;
|
---|
645 | }
|
---|
646 | bd_conn(icall, &vol->hr_bds);
|
---|
647 | }
|
---|
648 | }
|
---|
649 |
|
---|
650 | int main(int argc, char **argv)
|
---|
651 | {
|
---|
652 | errno_t rc;
|
---|
653 |
|
---|
654 | printf("%s: HelenRAID server\n", NAME);
|
---|
655 |
|
---|
656 | rc = log_init(NAME);
|
---|
657 | if (rc != EOK) {
|
---|
658 | printf("%s: failed to initialize logging\n", NAME);
|
---|
659 | return 1;
|
---|
660 | }
|
---|
661 |
|
---|
662 | fibril_rwlock_initialize(&hr_volumes_lock);
|
---|
663 | list_initialize(&hr_volumes);
|
---|
664 |
|
---|
665 | async_set_fallback_port_handler(hr_call_handler, NULL);
|
---|
666 |
|
---|
667 | rc = loc_server_register(NAME, &hr_srv);
|
---|
668 | if (rc != EOK) {
|
---|
669 | HR_ERROR("failed registering server: %s", str_error(rc));
|
---|
670 | return EEXIST;
|
---|
671 | }
|
---|
672 |
|
---|
673 | rc = loc_service_register(hr_srv, SERVICE_NAME_HR, fallback_port_id,
|
---|
674 | &ctl_sid);
|
---|
675 | if (rc != EOK) {
|
---|
676 | HR_ERROR("failed registering service: %s", str_error(rc));
|
---|
677 | return EEXIST;
|
---|
678 | }
|
---|
679 |
|
---|
680 | printf("%s: Trying automatic assembly.\n", NAME);
|
---|
681 | size_t assembled = 0;
|
---|
682 | (void)hr_util_try_assemble(NULL, &assembled);
|
---|
683 | printf("%s: Assembled %zu volume(s).\n", NAME, assembled);
|
---|
684 |
|
---|
685 | printf("%s: Accepting connections.\n", NAME);
|
---|
686 | task_retval(0);
|
---|
687 | async_manager();
|
---|
688 |
|
---|
689 | return 0;
|
---|
690 | }
|
---|
691 |
|
---|
692 | /** @}
|
---|
693 | */
|
---|