source: mainline/uspace/srv/bd/hr/hr.c@ 30140c1b

Last change on this file since 30140c1b was 30140c1b, checked in by Miroslav Cimerman <mc@…>, 9 months ago

hr: RAID4: hotspare + rebuild

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