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 | fibril_mutex_t big_lock; /* for now */
|
---|
57 |
|
---|
58 | static fibril_mutex_t hr_volumes_lock;
|
---|
59 | static list_t hr_volumes;
|
---|
60 |
|
---|
61 | static service_id_t ctl_sid;
|
---|
62 |
|
---|
63 | static hr_volume_t *hr_get_volume(service_id_t svc_id)
|
---|
64 | {
|
---|
65 | log_msg(LOG_DEFAULT, LVL_DEBUG, "hr_get_volume(): (%" PRIun ")",
|
---|
66 | svc_id);
|
---|
67 |
|
---|
68 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
69 | list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
|
---|
70 | if (volume->svc_id == svc_id) {
|
---|
71 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
72 | return volume;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
77 | return NULL;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static errno_t hr_remove_volume(service_id_t svc_id)
|
---|
81 | {
|
---|
82 | log_msg(LOG_DEFAULT, LVL_DEBUG, "hr_remove_volume(): (%" PRIun ")",
|
---|
83 | svc_id);
|
---|
84 |
|
---|
85 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
86 | list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
|
---|
87 | if (volume->svc_id == svc_id) {
|
---|
88 | hr_fini_devs(volume);
|
---|
89 | list_remove(&volume->lvolumes);
|
---|
90 | free(volume);
|
---|
91 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
92 | return EOK;
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
97 | return ENOENT;
|
---|
98 | }
|
---|
99 |
|
---|
100 | static void hr_create_srv(ipc_call_t *icall, bool assemble)
|
---|
101 | {
|
---|
102 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_create_srv()");
|
---|
103 |
|
---|
104 | errno_t rc;
|
---|
105 | size_t i, size;
|
---|
106 | hr_config_t *cfg;
|
---|
107 | hr_volume_t *new_volume;
|
---|
108 | ipc_call_t call;
|
---|
109 |
|
---|
110 | if (!async_data_write_receive(&call, &size)) {
|
---|
111 | async_answer_0(&call, EREFUSED);
|
---|
112 | async_answer_0(icall, EREFUSED);
|
---|
113 | return;
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (size != sizeof(hr_config_t)) {
|
---|
117 | async_answer_0(&call, EINVAL);
|
---|
118 | async_answer_0(icall, EINVAL);
|
---|
119 | return;
|
---|
120 | }
|
---|
121 |
|
---|
122 | cfg = calloc(1, sizeof(hr_config_t));
|
---|
123 | if (cfg == NULL) {
|
---|
124 | async_answer_0(&call, ENOMEM);
|
---|
125 | async_answer_0(icall, ENOMEM);
|
---|
126 | return;
|
---|
127 | }
|
---|
128 |
|
---|
129 | rc = async_data_write_finalize(&call, cfg, size);
|
---|
130 | if (rc != EOK) {
|
---|
131 | async_answer_0(&call, rc);
|
---|
132 | async_answer_0(icall, rc);
|
---|
133 | return;
|
---|
134 | }
|
---|
135 |
|
---|
136 | new_volume = calloc(1, sizeof(hr_volume_t));
|
---|
137 | if (new_volume == NULL) {
|
---|
138 | free(cfg);
|
---|
139 | async_answer_0(icall, ENOMEM);
|
---|
140 | return;
|
---|
141 | }
|
---|
142 |
|
---|
143 | str_cpy(new_volume->devname, 32, cfg->devname);
|
---|
144 | for (i = 0; i < cfg->dev_no; i++)
|
---|
145 | new_volume->extents[i].svc_id = cfg->devs[i];
|
---|
146 | new_volume->level = cfg->level;
|
---|
147 | new_volume->dev_no = cfg->dev_no;
|
---|
148 |
|
---|
149 | if (assemble) {
|
---|
150 | if (cfg->level != hr_l_empty)
|
---|
151 | log_msg(LOG_DEFAULT, LVL_WARN,
|
---|
152 | "level manually set when assembling, ingoring");
|
---|
153 | new_volume->level = hr_l_empty;
|
---|
154 | }
|
---|
155 |
|
---|
156 | rc = hr_init_devs(new_volume);
|
---|
157 | if (rc != EOK) {
|
---|
158 | free(cfg);
|
---|
159 | async_answer_0(icall, rc);
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | if (assemble) {
|
---|
164 | /* just bsize needed for reading metadata later */
|
---|
165 | rc = hr_check_devs(new_volume, NULL, &new_volume->bsize);
|
---|
166 | if (rc != EOK)
|
---|
167 | goto error;
|
---|
168 |
|
---|
169 | rc = hr_get_vol_from_meta(cfg, new_volume);
|
---|
170 | if (rc != EOK)
|
---|
171 | goto error;
|
---|
172 | }
|
---|
173 |
|
---|
174 | switch (new_volume->level) {
|
---|
175 | case hr_l_1:
|
---|
176 | new_volume->hr_ops.create = hr_raid1_create;
|
---|
177 | new_volume->hr_ops.init = hr_raid1_init;
|
---|
178 | break;
|
---|
179 | case hr_l_0:
|
---|
180 | new_volume->hr_ops.create = hr_raid0_create;
|
---|
181 | new_volume->hr_ops.init = hr_raid0_init;
|
---|
182 | break;
|
---|
183 | case hr_l_4:
|
---|
184 | new_volume->hr_ops.create = hr_raid4_create;
|
---|
185 | new_volume->hr_ops.init = hr_raid4_init;
|
---|
186 | break;
|
---|
187 | case hr_l_5:
|
---|
188 | new_volume->hr_ops.create = hr_raid5_create;
|
---|
189 | new_volume->hr_ops.init = hr_raid5_init;
|
---|
190 | break;
|
---|
191 | default:
|
---|
192 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
193 | "level %d not implemented yet", new_volume->level);
|
---|
194 | rc = EINVAL;
|
---|
195 | goto error;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (!assemble) {
|
---|
199 | new_volume->hr_ops.init(new_volume);
|
---|
200 | if (rc != EOK)
|
---|
201 | goto error;
|
---|
202 |
|
---|
203 | rc = hr_write_meta_to_vol(new_volume);
|
---|
204 | if (rc != EOK)
|
---|
205 | goto error;
|
---|
206 | }
|
---|
207 |
|
---|
208 | rc = new_volume->hr_ops.create(new_volume);
|
---|
209 | if (rc != EOK)
|
---|
210 | goto error;
|
---|
211 |
|
---|
212 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
213 | list_append(&new_volume->lvolumes, &hr_volumes);
|
---|
214 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
215 |
|
---|
216 | if (assemble) {
|
---|
217 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
---|
218 | "assembled volume \"%s\" (%" PRIun ")",
|
---|
219 | new_volume->devname, new_volume->svc_id);
|
---|
220 | } else {
|
---|
221 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
---|
222 | "created volume \"%s\" (%" PRIun ")",
|
---|
223 | new_volume->devname, new_volume->svc_id);
|
---|
224 | }
|
---|
225 |
|
---|
226 | free(cfg);
|
---|
227 | async_answer_0(icall, rc);
|
---|
228 | return;
|
---|
229 | error:
|
---|
230 | free(cfg);
|
---|
231 | hr_fini_devs(new_volume);
|
---|
232 | async_answer_0(icall, rc);
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void hr_stop_srv(ipc_call_t *icall)
|
---|
236 | {
|
---|
237 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_stop_srv()");
|
---|
238 |
|
---|
239 | errno_t rc;
|
---|
240 | service_id_t svc_id;
|
---|
241 | hr_volume_t *vol;
|
---|
242 |
|
---|
243 | svc_id = ipc_get_arg1(icall);
|
---|
244 |
|
---|
245 | vol = hr_get_volume(svc_id);
|
---|
246 | if (vol == NULL) {
|
---|
247 | async_answer_0(icall, ENOENT);
|
---|
248 | return;
|
---|
249 | }
|
---|
250 |
|
---|
251 | rc = hr_remove_volume(svc_id);
|
---|
252 | if (rc != EOK) {
|
---|
253 | async_answer_0(icall, rc);
|
---|
254 | return;
|
---|
255 | }
|
---|
256 |
|
---|
257 | rc = loc_service_unregister(hr_srv, svc_id);
|
---|
258 |
|
---|
259 | async_answer_0(icall, rc);
|
---|
260 | }
|
---|
261 |
|
---|
262 | static void hr_print_status_srv(ipc_call_t *icall)
|
---|
263 | {
|
---|
264 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_status_srv()");
|
---|
265 |
|
---|
266 | errno_t rc;
|
---|
267 | size_t vol_cnt = 0;
|
---|
268 | hr_vol_info_t info;
|
---|
269 | ipc_call_t call;
|
---|
270 | size_t size;
|
---|
271 |
|
---|
272 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
273 |
|
---|
274 | vol_cnt = list_count(&hr_volumes);
|
---|
275 |
|
---|
276 | if (!async_data_read_receive(&call, &size)) {
|
---|
277 | rc = EREFUSED;
|
---|
278 | goto error;
|
---|
279 | }
|
---|
280 |
|
---|
281 | if (size != sizeof(size_t)) {
|
---|
282 | rc = EINVAL;
|
---|
283 | goto error;
|
---|
284 | }
|
---|
285 |
|
---|
286 | rc = async_data_read_finalize(&call, &vol_cnt, size);
|
---|
287 | if (rc != EOK)
|
---|
288 | goto error;
|
---|
289 |
|
---|
290 | list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
|
---|
291 | memcpy(info.extents, volume->extents,
|
---|
292 | sizeof(hr_extent_t) * HR_MAXDEVS);
|
---|
293 | info.svc_id = volume->svc_id;
|
---|
294 | info.extent_no = volume->dev_no;
|
---|
295 | info.level = volume->level;
|
---|
296 | /* print usable number of blocks */
|
---|
297 | info.nblocks = volume->data_blkno;
|
---|
298 | info.strip_size = volume->strip_size;
|
---|
299 | info.bsize = volume->bsize;
|
---|
300 |
|
---|
301 | if (!async_data_read_receive(&call, &size)) {
|
---|
302 | rc = EREFUSED;
|
---|
303 | goto error;
|
---|
304 | }
|
---|
305 |
|
---|
306 | if (size != sizeof(hr_vol_info_t)) {
|
---|
307 | rc = EINVAL;
|
---|
308 | goto error;
|
---|
309 | }
|
---|
310 |
|
---|
311 | rc = async_data_read_finalize(&call, &info, size);
|
---|
312 | if (rc != EOK)
|
---|
313 | goto error;
|
---|
314 | }
|
---|
315 |
|
---|
316 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
317 | async_answer_0(icall, EOK);
|
---|
318 | return;
|
---|
319 | error:
|
---|
320 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
321 | async_answer_0(&call, rc);
|
---|
322 | async_answer_0(icall, rc);
|
---|
323 | }
|
---|
324 |
|
---|
325 | static void hr_ctl_conn(ipc_call_t *icall, void *arg)
|
---|
326 | {
|
---|
327 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_ctl_conn()");
|
---|
328 |
|
---|
329 | async_accept_0(icall);
|
---|
330 |
|
---|
331 | while (true) {
|
---|
332 | ipc_call_t call;
|
---|
333 | async_get_call(&call);
|
---|
334 | sysarg_t method = ipc_get_imethod(&call);
|
---|
335 |
|
---|
336 | if (!method) {
|
---|
337 | async_answer_0(&call, EOK);
|
---|
338 | return;
|
---|
339 | }
|
---|
340 |
|
---|
341 | switch (method) {
|
---|
342 | case HR_CREATE:
|
---|
343 | hr_create_srv(&call, false);
|
---|
344 | break;
|
---|
345 | case HR_ASSEMBLE:
|
---|
346 | hr_create_srv(&call, true);
|
---|
347 | break;
|
---|
348 | case HR_STOP:
|
---|
349 | hr_stop_srv(&call);
|
---|
350 | break;
|
---|
351 | case HR_STATUS:
|
---|
352 | hr_print_status_srv(&call);
|
---|
353 | break;
|
---|
354 | default:
|
---|
355 | async_answer_0(&call, EINVAL);
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | static void hr_client_conn(ipc_call_t *icall, void *arg)
|
---|
361 | {
|
---|
362 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_client_conn()");
|
---|
363 |
|
---|
364 | hr_volume_t *vol;
|
---|
365 |
|
---|
366 | service_id_t svc_id = ipc_get_arg2(icall);
|
---|
367 |
|
---|
368 | if (svc_id == ctl_sid) {
|
---|
369 | hr_ctl_conn(icall, arg);
|
---|
370 | } else {
|
---|
371 | log_msg(LOG_DEFAULT, LVL_NOTE, "bd_conn()");
|
---|
372 | vol = hr_get_volume(svc_id);
|
---|
373 | if (vol == NULL)
|
---|
374 | async_answer_0(icall, EINVAL);
|
---|
375 | bd_conn(icall, &vol->hr_bds);
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | int main(int argc, char **argv)
|
---|
380 | {
|
---|
381 | errno_t rc;
|
---|
382 |
|
---|
383 | printf("%s: HelenRAID server\n", NAME);
|
---|
384 |
|
---|
385 | rc = log_init(NAME);
|
---|
386 | if (rc != EOK) {
|
---|
387 | printf("%s: failed to initialize logging\n", NAME);
|
---|
388 | return 1;
|
---|
389 | }
|
---|
390 |
|
---|
391 | fibril_mutex_initialize(&big_lock);
|
---|
392 |
|
---|
393 | fibril_mutex_initialize(&hr_volumes_lock);
|
---|
394 | list_initialize(&hr_volumes);
|
---|
395 |
|
---|
396 | async_set_fallback_port_handler(hr_client_conn, NULL);
|
---|
397 |
|
---|
398 | rc = loc_server_register(NAME, &hr_srv);
|
---|
399 | if (rc != EOK) {
|
---|
400 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
401 | "failed registering server: %s", str_error(rc));
|
---|
402 | return EEXIST;
|
---|
403 | }
|
---|
404 |
|
---|
405 | rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
|
---|
406 | if (rc != EOK) {
|
---|
407 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
408 | "failed registering service: %s", str_error(rc));
|
---|
409 | return EEXIST;
|
---|
410 | }
|
---|
411 |
|
---|
412 | printf("%s: accepting connections\n", NAME);
|
---|
413 | task_retval(0);
|
---|
414 | async_manager();
|
---|
415 |
|
---|
416 | return 0;
|
---|
417 | }
|
---|
418 |
|
---|
419 | /** @}
|
---|
420 | */
|
---|