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
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <adt/list.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <bd_srv.h>
|
---|
39 | #include <errno.h>
|
---|
40 | #include <hr.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <inttypes.h>
|
---|
43 | #include <ipc/hr.h>
|
---|
44 | #include <ipc/services.h>
|
---|
45 | #include <loc.h>
|
---|
46 | #include <task.h>
|
---|
47 | #include <stdatomic.h>
|
---|
48 | #include <stdio.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 | #include <str.h>
|
---|
51 | #include <str_error.h>
|
---|
52 | #include <block.h>
|
---|
53 |
|
---|
54 | #include "fge.h"
|
---|
55 | #include "io.h"
|
---|
56 | #include "superblock.h"
|
---|
57 | #include "util.h"
|
---|
58 | #include "var.h"
|
---|
59 |
|
---|
60 | static void hr_assemble_srv(ipc_call_t *);
|
---|
61 | static void hr_auto_assemble_srv(ipc_call_t *);
|
---|
62 | static void hr_stop_srv(ipc_call_t *);
|
---|
63 | static void hr_add_hotspare_srv(ipc_call_t *);
|
---|
64 | static void hr_print_status_srv(ipc_call_t *);
|
---|
65 | static void hr_ctl_conn(ipc_call_t *, void *);
|
---|
66 | static void hr_client_conn(ipc_call_t *, void *);
|
---|
67 |
|
---|
68 | loc_srv_t *hr_srv;
|
---|
69 | list_t hr_volumes;
|
---|
70 | fibril_rwlock_t hr_volumes_lock;
|
---|
71 |
|
---|
72 | static service_id_t ctl_sid;
|
---|
73 |
|
---|
74 | static void hr_create_srv(ipc_call_t *icall)
|
---|
75 | {
|
---|
76 | HR_DEBUG("%s()", __func__);
|
---|
77 |
|
---|
78 | errno_t rc;
|
---|
79 | size_t i, size;
|
---|
80 | hr_config_t *cfg;
|
---|
81 | hr_volume_t *new_volume;
|
---|
82 | ipc_call_t call;
|
---|
83 |
|
---|
84 | if (!async_data_write_receive(&call, &size)) {
|
---|
85 | async_answer_0(&call, EREFUSED);
|
---|
86 | async_answer_0(icall, EREFUSED);
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (size != sizeof(hr_config_t)) {
|
---|
91 | async_answer_0(&call, EINVAL);
|
---|
92 | async_answer_0(icall, EINVAL);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | cfg = calloc(1, sizeof(hr_config_t));
|
---|
97 | if (cfg == NULL) {
|
---|
98 | async_answer_0(&call, ENOMEM);
|
---|
99 | async_answer_0(icall, ENOMEM);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = async_data_write_finalize(&call, cfg, size);
|
---|
104 | if (rc != EOK) {
|
---|
105 | free(cfg);
|
---|
106 | async_answer_0(&call, rc);
|
---|
107 | async_answer_0(icall, rc);
|
---|
108 | return;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*
|
---|
112 | * If there was a missing device provided
|
---|
113 | * for creation of a new array, abort
|
---|
114 | */
|
---|
115 | for (i = 0; i < cfg->dev_no; i++) {
|
---|
116 | if (cfg->devs[i] == 0) {
|
---|
117 | /*
|
---|
118 | * XXX: own error codes, no need to log this...
|
---|
119 | * its user error not service error
|
---|
120 | */
|
---|
121 | HR_ERROR("missing device provided for array "
|
---|
122 | "creation, aborting");
|
---|
123 | free(cfg);
|
---|
124 | async_answer_0(icall, EINVAL);
|
---|
125 | return;
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | rc = hr_create_vol_struct(&new_volume, cfg->level, cfg->devname);
|
---|
130 | if (rc != EOK) {
|
---|
131 | free(cfg);
|
---|
132 | async_answer_0(icall, rc);
|
---|
133 | return;
|
---|
134 | }
|
---|
135 |
|
---|
136 | rc = hr_init_extents_from_cfg(new_volume, cfg);
|
---|
137 | if (rc != EOK)
|
---|
138 | goto error;
|
---|
139 |
|
---|
140 | new_volume->hr_ops.init(new_volume);
|
---|
141 | if (rc != EOK)
|
---|
142 | goto error;
|
---|
143 |
|
---|
144 | rc = hr_metadata_init(new_volume, new_volume->in_mem_md);
|
---|
145 | if (rc != EOK)
|
---|
146 | goto error;
|
---|
147 |
|
---|
148 | rc = hr_metadata_save(new_volume);
|
---|
149 | if (rc != EOK)
|
---|
150 | goto error;
|
---|
151 |
|
---|
152 | rc = new_volume->hr_ops.create(new_volume);
|
---|
153 | if (rc != EOK)
|
---|
154 | goto error;
|
---|
155 |
|
---|
156 | rc = hr_register_volume(new_volume);
|
---|
157 | if (rc != EOK)
|
---|
158 | goto error;
|
---|
159 |
|
---|
160 | fibril_rwlock_write_lock(&hr_volumes_lock);
|
---|
161 | list_append(&new_volume->lvolumes, &hr_volumes);
|
---|
162 | fibril_rwlock_write_unlock(&hr_volumes_lock);
|
---|
163 |
|
---|
164 | HR_DEBUG("created volume \"%s\" (%" PRIun ")\n", new_volume->devname,
|
---|
165 | new_volume->svc_id);
|
---|
166 |
|
---|
167 | free(cfg);
|
---|
168 | async_answer_0(icall, rc);
|
---|
169 | return;
|
---|
170 | error:
|
---|
171 | free(cfg);
|
---|
172 | hr_destroy_vol_struct(new_volume);
|
---|
173 | async_answer_0(icall, rc);
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void hr_assemble_srv(ipc_call_t *icall)
|
---|
177 | {
|
---|
178 | HR_DEBUG("%s()", __func__);
|
---|
179 |
|
---|
180 | errno_t rc;
|
---|
181 | size_t size, assembled_cnt;
|
---|
182 | hr_config_t *cfg;
|
---|
183 | ipc_call_t call;
|
---|
184 |
|
---|
185 | if (!async_data_write_receive(&call, &size)) {
|
---|
186 | async_answer_0(&call, EREFUSED);
|
---|
187 | async_answer_0(icall, EREFUSED);
|
---|
188 | return;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (size != sizeof(hr_config_t)) {
|
---|
192 | async_answer_0(&call, EINVAL);
|
---|
193 | async_answer_0(icall, EINVAL);
|
---|
194 | return;
|
---|
195 | }
|
---|
196 |
|
---|
197 | cfg = calloc(1, sizeof(hr_config_t));
|
---|
198 | if (cfg == NULL) {
|
---|
199 | async_answer_0(&call, ENOMEM);
|
---|
200 | async_answer_0(icall, ENOMEM);
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | rc = async_data_write_finalize(&call, cfg, size);
|
---|
205 | if (rc != EOK)
|
---|
206 | goto error;
|
---|
207 |
|
---|
208 | if (!async_data_read_receive(&call, &size)) {
|
---|
209 | async_answer_0(icall, EREFUSED);
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (size != sizeof(size_t)) {
|
---|
214 | async_answer_0(icall, EINVAL);
|
---|
215 | return;
|
---|
216 | }
|
---|
217 |
|
---|
218 | rc = hr_util_try_assemble(cfg, &assembled_cnt);
|
---|
219 | if (rc != EOK)
|
---|
220 | goto error;
|
---|
221 |
|
---|
222 | rc = async_data_read_finalize(&call, &assembled_cnt, size);
|
---|
223 | if (rc != EOK)
|
---|
224 | goto error;
|
---|
225 |
|
---|
226 | free(cfg);
|
---|
227 | async_answer_0(icall, EOK);
|
---|
228 | return;
|
---|
229 | error:
|
---|
230 | free(cfg);
|
---|
231 | async_answer_0(&call, rc);
|
---|
232 | async_answer_0(icall, rc);
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void hr_auto_assemble_srv(ipc_call_t *icall)
|
---|
236 | {
|
---|
237 | HR_DEBUG("%s()", __func__);
|
---|
238 |
|
---|
239 | errno_t rc;
|
---|
240 | size_t size;
|
---|
241 | size_t assembled_cnt = 0;
|
---|
242 | ipc_call_t call;
|
---|
243 |
|
---|
244 | if (!async_data_read_receive(&call, &size)) {
|
---|
245 | async_answer_0(icall, EREFUSED);
|
---|
246 | return;
|
---|
247 | }
|
---|
248 |
|
---|
249 | if (size != sizeof(size_t)) {
|
---|
250 | async_answer_0(&call, EINVAL);
|
---|
251 | async_answer_0(icall, EINVAL);
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 | rc = hr_util_try_assemble(NULL, &assembled_cnt);
|
---|
256 | if (rc != EOK)
|
---|
257 | goto error;
|
---|
258 |
|
---|
259 | rc = async_data_read_finalize(&call, &assembled_cnt, size);
|
---|
260 | if (rc != EOK)
|
---|
261 | goto error;
|
---|
262 |
|
---|
263 | async_answer_0(icall, EOK);
|
---|
264 | return;
|
---|
265 | error:
|
---|
266 | async_answer_0(&call, rc);
|
---|
267 | async_answer_0(icall, rc);
|
---|
268 | }
|
---|
269 |
|
---|
270 | static void hr_stop_srv(ipc_call_t *icall)
|
---|
271 | {
|
---|
272 | HR_DEBUG("%s()", __func__);
|
---|
273 |
|
---|
274 | errno_t rc = EOK;
|
---|
275 | service_id_t svc_id;
|
---|
276 | long fail_extent;
|
---|
277 | hr_volume_t *vol;
|
---|
278 |
|
---|
279 | svc_id = ipc_get_arg1(icall);
|
---|
280 | fail_extent = (long)ipc_get_arg2(icall);
|
---|
281 |
|
---|
282 | vol = hr_get_volume(svc_id);
|
---|
283 | if (vol == NULL) {
|
---|
284 | async_answer_0(icall, ENOENT);
|
---|
285 | return;
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (fail_extent == -1) {
|
---|
289 | rc = hr_remove_volume(svc_id);
|
---|
290 | if (rc != EOK) {
|
---|
291 | async_answer_0(icall, rc);
|
---|
292 | return;
|
---|
293 | }
|
---|
294 | } else {
|
---|
295 | fibril_rwlock_write_lock(&vol->states_lock);
|
---|
296 | fibril_rwlock_read_lock(&vol->extents_lock);
|
---|
297 |
|
---|
298 | /* TODO: maybe expose extent state callbacks */
|
---|
299 | hr_update_ext_status(vol, fail_extent, HR_EXT_FAILED);
|
---|
300 | hr_mark_vol_state_dirty(vol);
|
---|
301 |
|
---|
302 | fibril_rwlock_read_unlock(&vol->extents_lock);
|
---|
303 | fibril_rwlock_write_unlock(&vol->states_lock);
|
---|
304 |
|
---|
305 | vol->hr_ops.status_event(vol);
|
---|
306 | }
|
---|
307 | async_answer_0(icall, rc);
|
---|
308 | }
|
---|
309 |
|
---|
310 | static void hr_add_hotspare_srv(ipc_call_t *icall)
|
---|
311 | {
|
---|
312 | HR_DEBUG("%s()", __func__);
|
---|
313 |
|
---|
314 | errno_t rc = EOK;
|
---|
315 | service_id_t vol_svc_id;
|
---|
316 | service_id_t hotspare;
|
---|
317 | hr_volume_t *vol;
|
---|
318 |
|
---|
319 | vol_svc_id = ipc_get_arg1(icall);
|
---|
320 | hotspare = ipc_get_arg2(icall);
|
---|
321 |
|
---|
322 | vol = hr_get_volume(vol_svc_id);
|
---|
323 | if (vol == NULL) {
|
---|
324 | async_answer_0(icall, ENOENT);
|
---|
325 | return;
|
---|
326 | }
|
---|
327 |
|
---|
328 | if (vol->hr_ops.add_hotspare == NULL) {
|
---|
329 | HR_DEBUG("hr_add_hotspare(): not supported on RAID level %d\n",
|
---|
330 | vol->level);
|
---|
331 | async_answer_0(icall, ENOTSUP);
|
---|
332 | return;
|
---|
333 | }
|
---|
334 |
|
---|
335 | rc = vol->hr_ops.add_hotspare(vol, hotspare);
|
---|
336 |
|
---|
337 | async_answer_0(icall, rc);
|
---|
338 | }
|
---|
339 |
|
---|
340 | static void hr_print_status_srv(ipc_call_t *icall)
|
---|
341 | {
|
---|
342 | HR_DEBUG("%s()", __func__);
|
---|
343 |
|
---|
344 | errno_t rc;
|
---|
345 | size_t vol_cnt = 0;
|
---|
346 | hr_vol_info_t info;
|
---|
347 | ipc_call_t call;
|
---|
348 | size_t size;
|
---|
349 |
|
---|
350 | fibril_rwlock_read_lock(&hr_volumes_lock);
|
---|
351 |
|
---|
352 | vol_cnt = list_count(&hr_volumes);
|
---|
353 |
|
---|
354 | if (!async_data_read_receive(&call, &size)) {
|
---|
355 | rc = EREFUSED;
|
---|
356 | goto error;
|
---|
357 | }
|
---|
358 |
|
---|
359 | if (size != sizeof(size_t)) {
|
---|
360 | rc = EINVAL;
|
---|
361 | goto error;
|
---|
362 | }
|
---|
363 |
|
---|
364 | rc = async_data_read_finalize(&call, &vol_cnt, size);
|
---|
365 | if (rc != EOK)
|
---|
366 | goto error;
|
---|
367 |
|
---|
368 | list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
|
---|
369 | memcpy(info.extents, vol->extents,
|
---|
370 | sizeof(hr_extent_t) * HR_MAX_EXTENTS);
|
---|
371 | memcpy(info.hotspares, vol->hotspares,
|
---|
372 | sizeof(hr_extent_t) * HR_MAX_HOTSPARES);
|
---|
373 | info.svc_id = vol->svc_id;
|
---|
374 | info.extent_no = vol->extent_no;
|
---|
375 | info.hotspare_no = vol->hotspare_no;
|
---|
376 | info.level = vol->level;
|
---|
377 | /* print usable number of blocks */
|
---|
378 | info.nblocks = vol->data_blkno;
|
---|
379 | info.strip_size = vol->strip_size;
|
---|
380 | info.bsize = vol->bsize;
|
---|
381 | info.status = vol->status;
|
---|
382 | info.layout = vol->layout;
|
---|
383 |
|
---|
384 | if (!async_data_read_receive(&call, &size)) {
|
---|
385 | rc = EREFUSED;
|
---|
386 | goto error;
|
---|
387 | }
|
---|
388 |
|
---|
389 | if (size != sizeof(hr_vol_info_t)) {
|
---|
390 | rc = EINVAL;
|
---|
391 | goto error;
|
---|
392 | }
|
---|
393 |
|
---|
394 | rc = async_data_read_finalize(&call, &info, size);
|
---|
395 | if (rc != EOK)
|
---|
396 | goto error;
|
---|
397 | }
|
---|
398 |
|
---|
399 | fibril_rwlock_read_unlock(&hr_volumes_lock);
|
---|
400 | async_answer_0(icall, EOK);
|
---|
401 | return;
|
---|
402 | error:
|
---|
403 | fibril_rwlock_read_unlock(&hr_volumes_lock);
|
---|
404 | async_answer_0(&call, rc);
|
---|
405 | async_answer_0(icall, rc);
|
---|
406 | }
|
---|
407 |
|
---|
408 | static void hr_ctl_conn(ipc_call_t *icall, void *arg)
|
---|
409 | {
|
---|
410 | HR_DEBUG("%s()", __func__);
|
---|
411 |
|
---|
412 | async_accept_0(icall);
|
---|
413 |
|
---|
414 | while (true) {
|
---|
415 | ipc_call_t call;
|
---|
416 | async_get_call(&call);
|
---|
417 | sysarg_t method = ipc_get_imethod(&call);
|
---|
418 |
|
---|
419 | if (!method) {
|
---|
420 | async_answer_0(&call, EOK);
|
---|
421 | return;
|
---|
422 | }
|
---|
423 |
|
---|
424 | switch (method) {
|
---|
425 | case HR_CREATE:
|
---|
426 | hr_create_srv(&call);
|
---|
427 | break;
|
---|
428 | case HR_ASSEMBLE:
|
---|
429 | hr_assemble_srv(&call);
|
---|
430 | break;
|
---|
431 | case HR_AUTO_ASSEMBLE:
|
---|
432 | hr_auto_assemble_srv(&call);
|
---|
433 | break;
|
---|
434 | case HR_STOP:
|
---|
435 | hr_stop_srv(&call);
|
---|
436 | break;
|
---|
437 | case HR_ADD_HOTSPARE:
|
---|
438 | hr_add_hotspare_srv(&call);
|
---|
439 | break;
|
---|
440 | case HR_STATUS:
|
---|
441 | hr_print_status_srv(&call);
|
---|
442 | break;
|
---|
443 | default:
|
---|
444 | async_answer_0(&call, EINVAL);
|
---|
445 | }
|
---|
446 | }
|
---|
447 | }
|
---|
448 |
|
---|
449 | static void hr_client_conn(ipc_call_t *icall, void *arg)
|
---|
450 | {
|
---|
451 | HR_DEBUG("%s()", __func__);
|
---|
452 |
|
---|
453 | hr_volume_t *vol;
|
---|
454 |
|
---|
455 | service_id_t svc_id = ipc_get_arg2(icall);
|
---|
456 |
|
---|
457 | if (svc_id == ctl_sid) {
|
---|
458 | hr_ctl_conn(icall, arg);
|
---|
459 | } else {
|
---|
460 | HR_DEBUG("bd_conn()\n");
|
---|
461 | vol = hr_get_volume(svc_id);
|
---|
462 | if (vol == NULL)
|
---|
463 | async_answer_0(icall, ENOENT);
|
---|
464 | bd_conn(icall, &vol->hr_bds);
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | int main(int argc, char **argv)
|
---|
469 | {
|
---|
470 | errno_t rc;
|
---|
471 |
|
---|
472 | printf("%s: HelenRAID server\n", NAME);
|
---|
473 |
|
---|
474 | rc = log_init(NAME);
|
---|
475 | if (rc != EOK) {
|
---|
476 | printf("%s: failed to initialize logging\n", NAME);
|
---|
477 | return 1;
|
---|
478 | }
|
---|
479 |
|
---|
480 | fibril_rwlock_initialize(&hr_volumes_lock);
|
---|
481 | list_initialize(&hr_volumes);
|
---|
482 |
|
---|
483 | async_set_fallback_port_handler(hr_client_conn, NULL);
|
---|
484 |
|
---|
485 | rc = loc_server_register(NAME, &hr_srv);
|
---|
486 | if (rc != EOK) {
|
---|
487 | HR_ERROR("failed registering server: %s", str_error(rc));
|
---|
488 | return EEXIST;
|
---|
489 | }
|
---|
490 |
|
---|
491 | rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
|
---|
492 | if (rc != EOK) {
|
---|
493 | HR_ERROR("failed registering service: %s", str_error(rc));
|
---|
494 | return EEXIST;
|
---|
495 | }
|
---|
496 |
|
---|
497 | printf("%s: accepting connections\n", NAME);
|
---|
498 | task_retval(0);
|
---|
499 | async_manager();
|
---|
500 |
|
---|
501 | return 0;
|
---|
502 | }
|
---|
503 |
|
---|
504 | /** @}
|
---|
505 | */
|
---|