source: mainline/uspace/srv/bd/hr/hr.c@ b235c67

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

hr: use shorthand vol for volume

  • Property mode set to 100644
File size: 9.6 KB
Line 
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
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
62static hr_volume_t *hr_get_volume(service_id_t svc_id)
63{
64 DPRINTF("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
78static errno_t hr_remove_volume(service_id_t svc_id)
79{
80 DPRINTF("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
97static void hr_create_srv(ipc_call_t *icall, bool assemble)
98{
99 DPRINTF("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 ERR_PRINTF("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 log_msg(LOG_DEFAULT, LVL_WARN,
166 "level manually set when assembling, ingoring");
167 new_volume->level = HR_LVL_UNKNOWN;
168 }
169
170 rc = hr_init_devs(new_volume);
171 if (rc != EOK) {
172 free(cfg);
173 free(new_volume);
174 async_answer_0(icall, rc);
175 return;
176 }
177
178 if (assemble) {
179 /* just bsize needed for reading metadata later */
180 rc = hr_check_devs(new_volume, NULL, &new_volume->bsize);
181 if (rc != EOK)
182 goto error;
183
184 rc = hr_fill_vol_from_meta(new_volume);
185 if (rc != EOK)
186 goto error;
187 }
188
189 switch (new_volume->level) {
190 case HR_LVL_1:
191 new_volume->hr_ops.create = hr_raid1_create;
192 new_volume->hr_ops.init = hr_raid1_init;
193 break;
194 case HR_LVL_0:
195 new_volume->hr_ops.create = hr_raid0_create;
196 new_volume->hr_ops.init = hr_raid0_init;
197 break;
198 case HR_LVL_4:
199 new_volume->hr_ops.create = hr_raid4_create;
200 new_volume->hr_ops.init = hr_raid4_init;
201 break;
202 case HR_LVL_5:
203 new_volume->hr_ops.create = hr_raid5_create;
204 new_volume->hr_ops.init = hr_raid5_init;
205 break;
206 default:
207 ERR_PRINTF("unkown level: %d, aborting\n", new_volume->level);
208 rc = EINVAL;
209 goto error;
210 }
211
212 if (!assemble) {
213 new_volume->hr_ops.init(new_volume);
214 if (rc != EOK)
215 goto error;
216
217 rc = hr_write_meta_to_vol(new_volume);
218 if (rc != EOK)
219 goto error;
220 }
221
222 fibril_mutex_initialize(&new_volume->lock);
223
224 rc = new_volume->hr_ops.create(new_volume);
225 if (rc != EOK)
226 goto error;
227
228 fibril_mutex_lock(&hr_volumes_lock);
229 list_append(&new_volume->lvolumes, &hr_volumes);
230 fibril_mutex_unlock(&hr_volumes_lock);
231
232 if (assemble) {
233 DPRINTF("assembled volume \"%s\" (%" PRIun ")\n",
234 new_volume->devname, new_volume->svc_id);
235 } else {
236 DPRINTF("created volume \"%s\" (%" PRIun ")\n",
237 new_volume->devname, new_volume->svc_id);
238 }
239
240 free(cfg);
241 async_answer_0(icall, rc);
242 return;
243error:
244 free(cfg);
245 hr_fini_devs(new_volume);
246 free(new_volume);
247 async_answer_0(icall, rc);
248}
249
250static void hr_stop_srv(ipc_call_t *icall)
251{
252 DPRINTF("hr_stop_srv()\n");
253
254 errno_t rc = EOK;
255 service_id_t svc_id;
256 long fail_extent;
257 hr_volume_t *vol;
258
259 svc_id = ipc_get_arg1(icall);
260 fail_extent = (long) ipc_get_arg2(icall);
261
262 vol = hr_get_volume(svc_id);
263 if (vol == NULL) {
264 async_answer_0(icall, ENOENT);
265 return;
266 }
267
268 if (fail_extent == -1) {
269 rc = hr_remove_volume(svc_id);
270 if (rc != EOK) {
271 async_answer_0(icall, rc);
272 return;
273 }
274 rc = loc_service_unregister(hr_srv, svc_id);
275 } else {
276 /* fibril safe for now */
277 fibril_mutex_lock(&vol->lock);
278 hr_update_ext_status(vol, fail_extent, HR_EXT_FAILED);
279 fibril_mutex_unlock(&vol->lock);
280 }
281 async_answer_0(icall, rc);
282}
283
284static void hr_print_status_srv(ipc_call_t *icall)
285{
286 DPRINTF("hr_status_srv()\n");
287
288 errno_t rc;
289 size_t vol_cnt = 0;
290 hr_vol_info_t info;
291 ipc_call_t call;
292 size_t size;
293
294 fibril_mutex_lock(&hr_volumes_lock);
295
296 vol_cnt = list_count(&hr_volumes);
297
298 if (!async_data_read_receive(&call, &size)) {
299 rc = EREFUSED;
300 goto error;
301 }
302
303 if (size != sizeof(size_t)) {
304 rc = EINVAL;
305 goto error;
306 }
307
308 rc = async_data_read_finalize(&call, &vol_cnt, size);
309 if (rc != EOK)
310 goto error;
311
312 list_foreach(hr_volumes, lvolumes, hr_volume_t, vol) {
313 memcpy(info.extents, vol->extents,
314 sizeof(hr_extent_t) * HR_MAXDEVS);
315 info.svc_id = vol->svc_id;
316 info.extent_no = vol->dev_no;
317 info.level = vol->level;
318 /* print usable number of blocks */
319 info.nblocks = vol->data_blkno;
320 info.strip_size = vol->strip_size;
321 info.bsize = vol->bsize;
322 info.status = vol->status;
323
324 if (!async_data_read_receive(&call, &size)) {
325 rc = EREFUSED;
326 goto error;
327 }
328
329 if (size != sizeof(hr_vol_info_t)) {
330 rc = EINVAL;
331 goto error;
332 }
333
334 rc = async_data_read_finalize(&call, &info, size);
335 if (rc != EOK)
336 goto error;
337 }
338
339 fibril_mutex_unlock(&hr_volumes_lock);
340 async_answer_0(icall, EOK);
341 return;
342error:
343 fibril_mutex_unlock(&hr_volumes_lock);
344 async_answer_0(&call, rc);
345 async_answer_0(icall, rc);
346}
347
348static void hr_ctl_conn(ipc_call_t *icall, void *arg)
349{
350 DPRINTF("hr_ctl_conn()\n");
351
352 async_accept_0(icall);
353
354 while (true) {
355 ipc_call_t call;
356 async_get_call(&call);
357 sysarg_t method = ipc_get_imethod(&call);
358
359 if (!method) {
360 async_answer_0(&call, EOK);
361 return;
362 }
363
364 switch (method) {
365 case HR_CREATE:
366 hr_create_srv(&call, false);
367 break;
368 case HR_ASSEMBLE:
369 hr_create_srv(&call, true);
370 break;
371 case HR_STOP:
372 hr_stop_srv(&call);
373 break;
374 case HR_STATUS:
375 hr_print_status_srv(&call);
376 break;
377 default:
378 async_answer_0(&call, EINVAL);
379 }
380 }
381}
382
383static void hr_client_conn(ipc_call_t *icall, void *arg)
384{
385 DPRINTF("hr_client_conn()\n");
386
387 hr_volume_t *vol;
388
389 service_id_t svc_id = ipc_get_arg2(icall);
390
391 if (svc_id == ctl_sid) {
392 hr_ctl_conn(icall, arg);
393 } else {
394 DPRINTF("bd_conn()\n");
395 vol = hr_get_volume(svc_id);
396 if (vol == NULL)
397 async_answer_0(icall, EINVAL);
398 bd_conn(icall, &vol->hr_bds);
399 }
400}
401
402int main(int argc, char **argv)
403{
404 errno_t rc;
405
406 printf("%s: HelenRAID server\n", NAME);
407
408 rc = log_init(NAME);
409 if (rc != EOK) {
410 printf("%s: failed to initialize logging\n", NAME);
411 return 1;
412 }
413
414 fibril_mutex_initialize(&hr_volumes_lock);
415 list_initialize(&hr_volumes);
416
417 async_set_fallback_port_handler(hr_client_conn, NULL);
418
419 rc = loc_server_register(NAME, &hr_srv);
420 if (rc != EOK) {
421 ERR_PRINTF("failed registering server: %s", str_error(rc));
422 return EEXIST;
423 }
424
425 rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
426 if (rc != EOK) {
427 ERR_PRINTF("failed registering service: %s", str_error(rc));
428 return EEXIST;
429 }
430
431 printf("%s: accepting connections\n", NAME);
432 task_retval(0);
433 async_manager();
434
435 return 0;
436}
437
438/** @}
439 */
Note: See TracBrowser for help on using the repository browser.