source: mainline/uspace/srv/bd/hr/hr.c@ 00d80c6

Last change on this file since 00d80c6 was 56602e0, checked in by Miroslav Cimerman <mc@…>, 3 months ago

hr: rename all strings status' -> state'

  • Property mode set to 100644
File size: 12.9 KB
Line 
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
56static void hr_assemble_srv(ipc_call_t *);
57static void hr_auto_assemble_srv(ipc_call_t *);
58static void hr_stop_srv(ipc_call_t *);
59static void hr_stop_all_srv(ipc_call_t *);
60static void hr_add_hotspare_srv(ipc_call_t *);
61static void hr_print_state_srv(ipc_call_t *);
62static void hr_ctl_conn(ipc_call_t *);
63static void hr_client_conn(ipc_call_t *, void *);
64
65loc_srv_t *hr_srv;
66list_t hr_volumes;
67fibril_rwlock_t hr_volumes_lock;
68
69static 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 */
78static 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 /*
116 * If there was a missing device provided
117 * for creation of a new array, abort
118 */
119 for (i = 0; i < cfg->dev_no; i++) {
120 if (cfg->devs[i] == 0) {
121 /*
122 * XXX: own error codes, no need to log this...
123 * its user error not service error
124 */
125 HR_ERROR("missing device provided for array "
126 "creation, aborting");
127 free(cfg);
128 async_answer_0(icall, EINVAL);
129 return;
130 }
131 }
132
133 rc = hr_create_vol_struct(&vol, cfg->level, cfg->devname,
134 HR_METADATA_NATIVE);
135 if (rc != EOK) {
136 free(cfg);
137 async_answer_0(icall, rc);
138 return;
139 }
140
141 rc = hr_init_extents_from_cfg(vol, cfg);
142 if (rc != EOK)
143 goto error;
144
145 vol->hr_ops.init(vol);
146 if (rc != EOK)
147 goto error;
148
149 rc = vol->meta_ops->init_vol2meta(vol, vol->in_mem_md);
150 if (rc != EOK)
151 goto error;
152
153 rc = vol->hr_ops.create(vol);
154 if (rc != EOK)
155 goto error;
156
157 rc = vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
158 if (rc != EOK)
159 goto error;
160
161 rc = hr_register_volume(vol);
162 if (rc != EOK)
163 goto error;
164
165 fibril_rwlock_write_lock(&hr_volumes_lock);
166 list_append(&vol->lvolumes, &hr_volumes);
167 fibril_rwlock_write_unlock(&hr_volumes_lock);
168
169 HR_NOTE("created volume \"%s\"\n", vol->devname);
170
171 free(cfg);
172 async_answer_0(icall, rc);
173 return;
174error:
175 free(cfg);
176 hr_destroy_vol_struct(vol);
177 async_answer_0(icall, rc);
178}
179
180/** Manual volume assembly (server).
181 *
182 * Tries to assemble a volume from devices in hr_config_t and
183 * sends the number of successful volumes assembled back to the
184 * client.
185 *
186 * @param icall hr_config_t
187 */
188static void hr_assemble_srv(ipc_call_t *icall)
189{
190 HR_DEBUG("%s()", __func__);
191
192 errno_t rc;
193 size_t size, assembled_cnt;
194 hr_config_t *cfg;
195 ipc_call_t call;
196
197 if (!async_data_write_receive(&call, &size)) {
198 async_answer_0(&call, EREFUSED);
199 async_answer_0(icall, EREFUSED);
200 return;
201 }
202
203 if (size != sizeof(hr_config_t)) {
204 async_answer_0(&call, EINVAL);
205 async_answer_0(icall, EINVAL);
206 return;
207 }
208
209 cfg = calloc(1, sizeof(hr_config_t));
210 if (cfg == NULL) {
211 async_answer_0(&call, ENOMEM);
212 async_answer_0(icall, ENOMEM);
213 return;
214 }
215
216 rc = async_data_write_finalize(&call, cfg, size);
217 if (rc != EOK)
218 goto error;
219
220 if (!async_data_read_receive(&call, &size)) {
221 async_answer_0(icall, EREFUSED);
222 return;
223 }
224
225 if (size != sizeof(size_t)) {
226 async_answer_0(icall, EINVAL);
227 return;
228 }
229
230 rc = hr_util_try_assemble(cfg, &assembled_cnt);
231 if (rc != EOK)
232 goto error;
233
234 rc = async_data_read_finalize(&call, &assembled_cnt, size);
235 if (rc != EOK)
236 goto error;
237
238 free(cfg);
239 async_answer_0(icall, EOK);
240 return;
241error:
242 free(cfg);
243 async_answer_0(&call, rc);
244 async_answer_0(icall, rc);
245}
246
247/** Automatic volume assembly (server).
248 *
249 * Tries to assemble a volume from devices in disk location
250 * category and sends the number of successful volumes assembled
251 * back to client.
252 */
253static void hr_auto_assemble_srv(ipc_call_t *icall)
254{
255 HR_DEBUG("%s()", __func__);
256
257 errno_t rc;
258 size_t size;
259 size_t assembled_cnt = 0;
260 ipc_call_t call;
261
262 if (!async_data_read_receive(&call, &size)) {
263 async_answer_0(icall, EREFUSED);
264 return;
265 }
266
267 if (size != sizeof(size_t)) {
268 async_answer_0(&call, EINVAL);
269 async_answer_0(icall, EINVAL);
270 return;
271 }
272
273 rc = hr_util_try_assemble(NULL, &assembled_cnt);
274 if (rc != EOK)
275 goto error;
276
277 rc = async_data_read_finalize(&call, &assembled_cnt, size);
278 if (rc != EOK)
279 goto error;
280
281 async_answer_0(icall, EOK);
282 return;
283error:
284 async_answer_0(&call, rc);
285 async_answer_0(icall, rc);
286}
287
288/** Volume deactivation (server).
289 *
290 * Deactivates/detaches specified volume.
291 */
292static void hr_stop_srv(ipc_call_t *icall)
293{
294 HR_DEBUG("%s()", __func__);
295
296 errno_t rc = EOK;
297 service_id_t svc_id;
298 hr_volume_t *vol;
299
300 svc_id = ipc_get_arg1(icall);
301
302 vol = hr_get_volume(svc_id);
303 if (vol == NULL) {
304 async_answer_0(icall, ENOENT);
305 return;
306 }
307
308 rc = hr_remove_volume(vol);
309
310 async_answer_0(icall, rc);
311}
312
313/** Automatic volume deactivation (server).
314 *
315 * Tries to deactivate/detach all volumes.
316 */
317static void hr_stop_all_srv(ipc_call_t *icall)
318{
319 HR_DEBUG("%s()", __func__);
320
321 hr_volume_t *vol;
322 errno_t rc = EOK;
323
324 while (true) {
325 fibril_rwlock_write_lock(&hr_volumes_lock);
326 if (list_empty(&hr_volumes)) {
327 fibril_rwlock_write_unlock(&hr_volumes_lock);
328 break;
329 }
330
331 vol = list_pop(&hr_volumes, hr_volume_t, lvolumes);
332
333 fibril_rwlock_write_unlock(&hr_volumes_lock);
334
335 rc = hr_remove_volume(vol);
336 if (rc != EOK)
337 break;
338 }
339
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 */
350static 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 fail_extent;
356 hr_volume_t *vol;
357
358 svc_id = (service_id_t)ipc_get_arg1(icall);
359 fail_extent = (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_read_lock(&vol->extents_lock);
368 fibril_rwlock_write_lock(&vol->states_lock);
369
370 switch (vol->extents[fail_extent].state) {
371 case HR_EXT_NONE:
372 case HR_EXT_MISSING:
373 case HR_EXT_FAILED:
374 fibril_rwlock_write_unlock(&vol->states_lock);
375 fibril_rwlock_read_unlock(&vol->extents_lock);
376 async_answer_0(icall, EINVAL);
377 return;
378 default:
379 hr_update_ext_state(vol, fail_extent, HR_EXT_FAILED);
380 hr_mark_vol_state_dirty(vol);
381 }
382
383 fibril_rwlock_write_unlock(&vol->states_lock);
384 fibril_rwlock_read_unlock(&vol->extents_lock);
385
386 vol->hr_ops.state_event(vol);
387
388 async_answer_0(icall, EOK);
389}
390
391/** Add hotspare to volume (server).
392 *
393 * Adds hotspare to a volume.
394 */
395static void hr_add_hotspare_srv(ipc_call_t *icall)
396{
397 HR_DEBUG("%s()", __func__);
398
399 errno_t rc = EOK;
400 service_id_t vol_svc_id;
401 service_id_t hotspare;
402 hr_volume_t *vol;
403
404 vol_svc_id = ipc_get_arg1(icall);
405 hotspare = ipc_get_arg2(icall);
406
407 vol = hr_get_volume(vol_svc_id);
408 if (vol == NULL) {
409 async_answer_0(icall, ENOENT);
410 return;
411 }
412
413 if (vol->hr_ops.add_hotspare == NULL) {
414 HR_NOTE("hotspare not supported on RAID level = %d, "
415 "metadata type = %s\n", vol->level,
416 hr_get_metadata_type_str(vol->meta_ops->get_type()));
417 async_answer_0(icall, ENOTSUP);
418 return;
419 }
420
421 rc = vol->hr_ops.add_hotspare(vol, hotspare);
422
423 async_answer_0(icall, rc);
424}
425
426/** Volume state printing (server).
427 *
428 * Prints info about all active volumes.
429 */
430static void hr_print_state_srv(ipc_call_t *icall)
431{
432 HR_DEBUG("%s()", __func__);
433
434 errno_t rc;
435 size_t vol_cnt = 0;
436 hr_vol_info_t info;
437 ipc_call_t call;
438 size_t size;
439
440 fibril_rwlock_read_lock(&hr_volumes_lock);
441
442 vol_cnt = list_count(&hr_volumes);
443
444 if (!async_data_read_receive(&call, &size)) {
445 rc = EREFUSED;
446 goto error;
447 }
448
449 if (size != sizeof(size_t)) {
450 rc = EINVAL;
451 goto error;
452 }
453
454 rc = async_data_read_finalize(&call, &vol_cnt, size);
455 if (rc != EOK)
456 goto error;
457
458 list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
459 memcpy(info.extents, vol->extents,
460 sizeof(hr_extent_t) * HR_MAX_EXTENTS);
461 memcpy(info.hotspares, vol->hotspares,
462 sizeof(hr_extent_t) * HR_MAX_HOTSPARES);
463 info.svc_id = vol->svc_id;
464 info.extent_no = vol->extent_no;
465 info.hotspare_no = vol->hotspare_no;
466 info.level = vol->level;
467 /* print usable number of blocks */
468 /* TODO: change to data_blkno */
469 info.nblocks = vol->data_blkno;
470 info.strip_size = vol->strip_size;
471 info.bsize = vol->bsize;
472 info.state = vol->state;
473 info.layout = vol->layout;
474
475 if (!async_data_read_receive(&call, &size)) {
476 rc = EREFUSED;
477 goto error;
478 }
479
480 if (size != sizeof(hr_vol_info_t)) {
481 rc = EINVAL;
482 goto error;
483 }
484
485 rc = async_data_read_finalize(&call, &info, 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;
493error:
494 fibril_rwlock_read_unlock(&hr_volumes_lock);
495 async_answer_0(&call, rc);
496 async_answer_0(icall, rc);
497}
498
499/** HelenRAID server control IPC methods crossroad.
500 */
501static void hr_ctl_conn(ipc_call_t *icall)
502{
503 HR_DEBUG("%s()", __func__);
504
505 async_accept_0(icall);
506
507 while (true) {
508 ipc_call_t call;
509 async_get_call(&call);
510 sysarg_t method = ipc_get_imethod(&call);
511
512 if (!method) {
513 async_answer_0(&call, EOK);
514 return;
515 }
516
517 switch (method) {
518 case HR_CREATE:
519 hr_create_srv(&call);
520 break;
521 case HR_ASSEMBLE:
522 hr_assemble_srv(&call);
523 break;
524 case HR_AUTO_ASSEMBLE:
525 hr_auto_assemble_srv(&call);
526 break;
527 case HR_STOP:
528 hr_stop_srv(&call);
529 break;
530 case HR_STOP_ALL:
531 hr_stop_all_srv(&call);
532 break;
533 case HR_FAIL_EXTENT:
534 hr_fail_extent_srv(&call);
535 break;
536 case HR_ADD_HOTSPARE:
537 hr_add_hotspare_srv(&call);
538 break;
539 case HR_STATUS:
540 hr_print_state_srv(&call);
541 break;
542 default:
543 async_answer_0(&call, EINVAL);
544 }
545 }
546}
547
548/** HelenRAID server IPC method crossroad.
549 *
550 * Distinguishes between control IPC and block device
551 * IPC calls.
552 */
553static void hr_client_conn(ipc_call_t *icall, void *arg)
554{
555 HR_DEBUG("%s()", __func__);
556
557 hr_volume_t *vol;
558
559 service_id_t svc_id = ipc_get_arg2(icall);
560
561 if (svc_id == ctl_sid) {
562 hr_ctl_conn(icall);
563 } else {
564 vol = hr_get_volume(svc_id);
565 if (vol == NULL)
566 async_answer_0(icall, ENOENT);
567 bd_conn(icall, &vol->hr_bds);
568 }
569}
570
571int main(int argc, char **argv)
572{
573 errno_t rc;
574
575 printf("%s: HelenRAID server\n", NAME);
576
577 rc = log_init(NAME);
578 if (rc != EOK) {
579 printf("%s: failed to initialize logging\n", NAME);
580 return 1;
581 }
582
583 fibril_rwlock_initialize(&hr_volumes_lock);
584 list_initialize(&hr_volumes);
585
586 async_set_fallback_port_handler(hr_client_conn, NULL);
587
588 rc = loc_server_register(NAME, &hr_srv);
589 if (rc != EOK) {
590 HR_ERROR("failed registering server: %s", str_error(rc));
591 return EEXIST;
592 }
593
594 rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
595 if (rc != EOK) {
596 HR_ERROR("failed registering service: %s", str_error(rc));
597 return EEXIST;
598 }
599
600 printf("%s: Accepting connections.\n", NAME);
601 task_retval(0);
602 async_manager();
603
604 return 0;
605}
606
607/** @}
608 */
Note: See TracBrowser for help on using the repository browser.