source: mainline/uspace/srv/bd/hr/hr.c@ 75262d2f

Last change on this file since 75262d2f was f647b87, checked in by Miroslav Cimerman <mc@…>, 7 months ago

srv/bd/hr: remove unused nblocks variable

  • Property mode set to 100644
File size: 10.7 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
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
60static void hr_assemble_srv(ipc_call_t *);
61static void hr_auto_assemble_srv(ipc_call_t *);
62static void hr_stop_srv(ipc_call_t *);
63static void hr_add_hotspare_srv(ipc_call_t *);
64static void hr_print_status_srv(ipc_call_t *);
65static void hr_ctl_conn(ipc_call_t *, void *);
66static void hr_client_conn(ipc_call_t *, void *);
67
68loc_srv_t *hr_srv;
69list_t hr_volumes;
70fibril_rwlock_t hr_volumes_lock;
71
72static service_id_t ctl_sid;
73
74static 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 *vol;
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(&vol, cfg->level, cfg->devname,
130 HR_METADATA_NATIVE);
131 if (rc != EOK) {
132 free(cfg);
133 async_answer_0(icall, rc);
134 return;
135 }
136
137 rc = hr_init_extents_from_cfg(vol, cfg);
138 if (rc != EOK)
139 goto error;
140
141 vol->hr_ops.init(vol);
142 if (rc != EOK)
143 goto error;
144
145 rc = vol->meta_ops->init_vol2meta(vol, vol->in_mem_md);
146 if (rc != EOK)
147 goto error;
148
149 rc = vol->hr_ops.create(vol);
150 if (rc != EOK)
151 goto error;
152
153 rc = vol->meta_ops->save(vol, WITH_STATE_CALLBACK);
154 if (rc != EOK)
155 goto error;
156
157 rc = hr_register_volume(vol);
158 if (rc != EOK)
159 goto error;
160
161 fibril_rwlock_write_lock(&hr_volumes_lock);
162 list_append(&vol->lvolumes, &hr_volumes);
163 fibril_rwlock_write_unlock(&hr_volumes_lock);
164
165 HR_DEBUG("created volume \"%s\" (%" PRIun ")\n", vol->devname,
166 vol->svc_id);
167
168 free(cfg);
169 async_answer_0(icall, rc);
170 return;
171error:
172 free(cfg);
173 hr_destroy_vol_struct(vol);
174 async_answer_0(icall, rc);
175}
176
177static void hr_assemble_srv(ipc_call_t *icall)
178{
179 HR_DEBUG("%s()", __func__);
180
181 errno_t rc;
182 size_t size, assembled_cnt;
183 hr_config_t *cfg;
184 ipc_call_t call;
185
186 if (!async_data_write_receive(&call, &size)) {
187 async_answer_0(&call, EREFUSED);
188 async_answer_0(icall, EREFUSED);
189 return;
190 }
191
192 if (size != sizeof(hr_config_t)) {
193 async_answer_0(&call, EINVAL);
194 async_answer_0(icall, EINVAL);
195 return;
196 }
197
198 cfg = calloc(1, sizeof(hr_config_t));
199 if (cfg == NULL) {
200 async_answer_0(&call, ENOMEM);
201 async_answer_0(icall, ENOMEM);
202 return;
203 }
204
205 rc = async_data_write_finalize(&call, cfg, size);
206 if (rc != EOK)
207 goto error;
208
209 if (!async_data_read_receive(&call, &size)) {
210 async_answer_0(icall, EREFUSED);
211 return;
212 }
213
214 if (size != sizeof(size_t)) {
215 async_answer_0(icall, EINVAL);
216 return;
217 }
218
219 rc = hr_util_try_assemble(cfg, &assembled_cnt);
220 if (rc != EOK)
221 goto error;
222
223 rc = async_data_read_finalize(&call, &assembled_cnt, size);
224 if (rc != EOK)
225 goto error;
226
227 free(cfg);
228 async_answer_0(icall, EOK);
229 return;
230error:
231 free(cfg);
232 async_answer_0(&call, rc);
233 async_answer_0(icall, rc);
234}
235
236static void hr_auto_assemble_srv(ipc_call_t *icall)
237{
238 HR_DEBUG("%s()", __func__);
239
240 errno_t rc;
241 size_t size;
242 size_t assembled_cnt = 0;
243 ipc_call_t call;
244
245 if (!async_data_read_receive(&call, &size)) {
246 async_answer_0(icall, EREFUSED);
247 return;
248 }
249
250 if (size != sizeof(size_t)) {
251 async_answer_0(&call, EINVAL);
252 async_answer_0(icall, EINVAL);
253 return;
254 }
255
256 rc = hr_util_try_assemble(NULL, &assembled_cnt);
257 if (rc != EOK)
258 goto error;
259
260 rc = async_data_read_finalize(&call, &assembled_cnt, size);
261 if (rc != EOK)
262 goto error;
263
264 async_answer_0(icall, EOK);
265 return;
266error:
267 async_answer_0(&call, rc);
268 async_answer_0(icall, rc);
269}
270
271static void hr_stop_srv(ipc_call_t *icall)
272{
273 HR_DEBUG("%s()", __func__);
274
275 errno_t rc = EOK;
276 service_id_t svc_id;
277 long fail_extent;
278 hr_volume_t *vol;
279
280 svc_id = ipc_get_arg1(icall);
281 fail_extent = (long)ipc_get_arg2(icall);
282
283 vol = hr_get_volume(svc_id);
284 if (vol == NULL) {
285 async_answer_0(icall, ENOENT);
286 return;
287 }
288
289 if (fail_extent == -1) {
290 rc = hr_remove_volume(svc_id);
291 if (rc != EOK) {
292 async_answer_0(icall, rc);
293 return;
294 }
295 } else {
296 fibril_rwlock_write_lock(&vol->states_lock);
297 fibril_rwlock_read_lock(&vol->extents_lock);
298
299 /* TODO: maybe expose extent state callbacks */
300 hr_update_ext_status(vol, fail_extent, HR_EXT_FAILED);
301 hr_mark_vol_state_dirty(vol);
302
303 fibril_rwlock_read_unlock(&vol->extents_lock);
304 fibril_rwlock_write_unlock(&vol->states_lock);
305
306 vol->hr_ops.status_event(vol);
307 }
308 async_answer_0(icall, rc);
309}
310
311static void hr_add_hotspare_srv(ipc_call_t *icall)
312{
313 HR_DEBUG("%s()", __func__);
314
315 errno_t rc = EOK;
316 service_id_t vol_svc_id;
317 service_id_t hotspare;
318 hr_volume_t *vol;
319
320 vol_svc_id = ipc_get_arg1(icall);
321 hotspare = ipc_get_arg2(icall);
322
323 vol = hr_get_volume(vol_svc_id);
324 if (vol == NULL) {
325 async_answer_0(icall, ENOENT);
326 return;
327 }
328
329 if (vol->hr_ops.add_hotspare == NULL) {
330 HR_DEBUG("hr_add_hotspare(): not supported on RAID level %d\n",
331 vol->level);
332 async_answer_0(icall, ENOTSUP);
333 return;
334 }
335
336 rc = vol->hr_ops.add_hotspare(vol, hotspare);
337
338 async_answer_0(icall, rc);
339}
340
341static void hr_print_status_srv(ipc_call_t *icall)
342{
343 HR_DEBUG("%s()", __func__);
344
345 errno_t rc;
346 size_t vol_cnt = 0;
347 hr_vol_info_t info;
348 ipc_call_t call;
349 size_t size;
350
351 fibril_rwlock_read_lock(&hr_volumes_lock);
352
353 vol_cnt = list_count(&hr_volumes);
354
355 if (!async_data_read_receive(&call, &size)) {
356 rc = EREFUSED;
357 goto error;
358 }
359
360 if (size != sizeof(size_t)) {
361 rc = EINVAL;
362 goto error;
363 }
364
365 rc = async_data_read_finalize(&call, &vol_cnt, size);
366 if (rc != EOK)
367 goto error;
368
369 list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
370 memcpy(info.extents, vol->extents,
371 sizeof(hr_extent_t) * HR_MAX_EXTENTS);
372 memcpy(info.hotspares, vol->hotspares,
373 sizeof(hr_extent_t) * HR_MAX_HOTSPARES);
374 info.svc_id = vol->svc_id;
375 info.extent_no = vol->extent_no;
376 info.hotspare_no = vol->hotspare_no;
377 info.level = vol->level;
378 /* print usable number of blocks */
379 /* TODO: change to data_blkno */
380 info.nblocks = vol->data_blkno;
381 info.strip_size = vol->strip_size;
382 info.bsize = vol->bsize;
383 info.status = vol->status;
384 info.layout = vol->layout;
385
386 if (!async_data_read_receive(&call, &size)) {
387 rc = EREFUSED;
388 goto error;
389 }
390
391 if (size != sizeof(hr_vol_info_t)) {
392 rc = EINVAL;
393 goto error;
394 }
395
396 rc = async_data_read_finalize(&call, &info, size);
397 if (rc != EOK)
398 goto error;
399 }
400
401 fibril_rwlock_read_unlock(&hr_volumes_lock);
402 async_answer_0(icall, EOK);
403 return;
404error:
405 fibril_rwlock_read_unlock(&hr_volumes_lock);
406 async_answer_0(&call, rc);
407 async_answer_0(icall, rc);
408}
409
410static void hr_ctl_conn(ipc_call_t *icall, void *arg)
411{
412 HR_DEBUG("%s()", __func__);
413
414 async_accept_0(icall);
415
416 while (true) {
417 ipc_call_t call;
418 async_get_call(&call);
419 sysarg_t method = ipc_get_imethod(&call);
420
421 if (!method) {
422 async_answer_0(&call, EOK);
423 return;
424 }
425
426 switch (method) {
427 case HR_CREATE:
428 hr_create_srv(&call);
429 break;
430 case HR_ASSEMBLE:
431 hr_assemble_srv(&call);
432 break;
433 case HR_AUTO_ASSEMBLE:
434 hr_auto_assemble_srv(&call);
435 break;
436 case HR_STOP:
437 hr_stop_srv(&call);
438 break;
439 case HR_ADD_HOTSPARE:
440 hr_add_hotspare_srv(&call);
441 break;
442 case HR_STATUS:
443 hr_print_status_srv(&call);
444 break;
445 default:
446 async_answer_0(&call, EINVAL);
447 }
448 }
449}
450
451static void hr_client_conn(ipc_call_t *icall, void *arg)
452{
453 HR_DEBUG("%s()", __func__);
454
455 hr_volume_t *vol;
456
457 service_id_t svc_id = ipc_get_arg2(icall);
458
459 if (svc_id == ctl_sid) {
460 hr_ctl_conn(icall, arg);
461 } else {
462 HR_DEBUG("bd_conn()\n");
463 vol = hr_get_volume(svc_id);
464 if (vol == NULL)
465 async_answer_0(icall, ENOENT);
466 bd_conn(icall, &vol->hr_bds);
467 }
468}
469
470int main(int argc, char **argv)
471{
472 errno_t rc;
473
474 printf("%s: HelenRAID server\n", NAME);
475
476 rc = log_init(NAME);
477 if (rc != EOK) {
478 printf("%s: failed to initialize logging\n", NAME);
479 return 1;
480 }
481
482 fibril_rwlock_initialize(&hr_volumes_lock);
483 list_initialize(&hr_volumes);
484
485 async_set_fallback_port_handler(hr_client_conn, NULL);
486
487 rc = loc_server_register(NAME, &hr_srv);
488 if (rc != EOK) {
489 HR_ERROR("failed registering server: %s", str_error(rc));
490 return EEXIST;
491 }
492
493 rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
494 if (rc != EOK) {
495 HR_ERROR("failed registering service: %s", str_error(rc));
496 return EEXIST;
497 }
498
499 printf("%s: accepting connections\n", NAME);
500 task_retval(0);
501 async_manager();
502
503 return 0;
504}
505
506/** @}
507 */
Note: See TracBrowser for help on using the repository browser.