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