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 "var.h"
|
---|
52 |
|
---|
53 | loc_srv_t *hr_srv;
|
---|
54 | fibril_mutex_t big_lock; /* for now */
|
---|
55 |
|
---|
56 | static fibril_mutex_t hr_volumes_lock;
|
---|
57 | static list_t hr_volumes;
|
---|
58 |
|
---|
59 | static service_id_t ctl_sid;
|
---|
60 |
|
---|
61 | static void hr_create_srv(ipc_call_t *icall)
|
---|
62 | {
|
---|
63 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_create_srv()");
|
---|
64 |
|
---|
65 | errno_t rc;
|
---|
66 | size_t size;
|
---|
67 | hr_config_t *cfg;
|
---|
68 | hr_volume_t *new_volume;
|
---|
69 | ipc_call_t call;
|
---|
70 |
|
---|
71 | if (!async_data_write_receive(&call, &size)) {
|
---|
72 | async_answer_0(&call, EREFUSED);
|
---|
73 | async_answer_0(icall, EREFUSED);
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | if (size != sizeof(hr_config_t)) {
|
---|
78 | async_answer_0(&call, EINVAL);
|
---|
79 | async_answer_0(icall, EINVAL);
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | cfg = calloc(1, sizeof(hr_config_t));
|
---|
84 | if (cfg == NULL) {
|
---|
85 | async_answer_0(&call, ENOMEM);
|
---|
86 | async_answer_0(icall, ENOMEM);
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | rc = async_data_write_finalize(&call, cfg, size);
|
---|
91 | if (rc != EOK) {
|
---|
92 | async_answer_0(&call, rc);
|
---|
93 | async_answer_0(icall, rc);
|
---|
94 | }
|
---|
95 |
|
---|
96 | new_volume = calloc(1, sizeof(hr_volume_t));
|
---|
97 | if (new_volume == NULL) {
|
---|
98 | rc = ENOMEM;
|
---|
99 | goto end;
|
---|
100 | }
|
---|
101 |
|
---|
102 | str_cpy(new_volume->devname, 32, cfg->devname);
|
---|
103 | memcpy(new_volume->devs, cfg->devs, sizeof(service_id_t) * HR_MAXDEVS);
|
---|
104 | new_volume->level = cfg->level;
|
---|
105 | new_volume->dev_no = cfg->dev_no;
|
---|
106 |
|
---|
107 | switch (new_volume->level) {
|
---|
108 | case hr_l_1:
|
---|
109 | new_volume->hr_ops.create = hr_raid1_create;
|
---|
110 | break;
|
---|
111 | case hr_l_0:
|
---|
112 | new_volume->hr_ops.create = hr_raid0_create;
|
---|
113 | break;
|
---|
114 | default:
|
---|
115 | log_msg(LOG_DEFAULT, LVL_NOTE,
|
---|
116 | "level %d not implemented yet\n", new_volume->level);
|
---|
117 | rc = EINVAL;
|
---|
118 | goto end;
|
---|
119 | }
|
---|
120 |
|
---|
121 | rc = new_volume->hr_ops.create(new_volume);
|
---|
122 | if (rc != EOK) {
|
---|
123 | goto end;
|
---|
124 | }
|
---|
125 |
|
---|
126 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
127 | list_append(&new_volume->lvolumes, &hr_volumes);
|
---|
128 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
129 |
|
---|
130 | log_msg(LOG_DEFAULT, LVL_NOTE, "created volume \"%s\" (%" PRIun ")\n",
|
---|
131 | new_volume->devname, new_volume->svc_id);
|
---|
132 |
|
---|
133 | end:
|
---|
134 | free(cfg);
|
---|
135 | async_answer_0(icall, rc);
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void hr_print_status_srv(ipc_call_t *icall)
|
---|
139 | {
|
---|
140 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_status_srv()");
|
---|
141 |
|
---|
142 | errno_t rc;
|
---|
143 | size_t vol_cnt = 0;
|
---|
144 | hr_vol_info_t info;
|
---|
145 | ipc_call_t call;
|
---|
146 | size_t size;
|
---|
147 |
|
---|
148 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
149 |
|
---|
150 | vol_cnt = list_count(&hr_volumes);
|
---|
151 |
|
---|
152 | if (!async_data_read_receive(&call, &size)) {
|
---|
153 | rc = EREFUSED;
|
---|
154 | goto error;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (size != sizeof(size_t)) {
|
---|
158 | rc = EINVAL;
|
---|
159 | goto error;
|
---|
160 | }
|
---|
161 |
|
---|
162 | rc = async_data_read_finalize(&call, &vol_cnt, size);
|
---|
163 | if (rc != EOK)
|
---|
164 | goto error;
|
---|
165 |
|
---|
166 | list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
|
---|
167 | memcpy(info.extents, volume->devs,
|
---|
168 | sizeof(service_id_t) * HR_MAXDEVS);
|
---|
169 | info.svc_id = volume->svc_id;
|
---|
170 | info.extent_no = volume->dev_no;
|
---|
171 | info.level = volume->level;
|
---|
172 | info.nblocks = volume->nblocks;
|
---|
173 | info.bsize = volume->bsize;
|
---|
174 |
|
---|
175 | if (!async_data_read_receive(&call, &size)) {
|
---|
176 | rc = EREFUSED;
|
---|
177 | goto error;
|
---|
178 | }
|
---|
179 |
|
---|
180 | if (size != sizeof(hr_vol_info_t)) {
|
---|
181 | rc = EINVAL;
|
---|
182 | goto error;
|
---|
183 | }
|
---|
184 |
|
---|
185 | rc = async_data_read_finalize(&call, &info, size);
|
---|
186 | if (rc != EOK)
|
---|
187 | goto error;
|
---|
188 | }
|
---|
189 |
|
---|
190 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
191 | async_answer_0(icall, EOK);
|
---|
192 | return;
|
---|
193 | error:
|
---|
194 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
195 | async_answer_0(&call, rc);
|
---|
196 | async_answer_0(icall, rc);
|
---|
197 | }
|
---|
198 |
|
---|
199 | static void hr_ctl_conn(ipc_call_t *icall, void *arg)
|
---|
200 | {
|
---|
201 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_ctl_conn()");
|
---|
202 |
|
---|
203 | async_accept_0(icall);
|
---|
204 |
|
---|
205 | while (true) {
|
---|
206 | ipc_call_t call;
|
---|
207 | async_get_call(&call);
|
---|
208 | sysarg_t method = ipc_get_imethod(&call);
|
---|
209 |
|
---|
210 | if (!method) {
|
---|
211 | async_answer_0(&call, EOK);
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | switch (method) {
|
---|
216 | case HR_CREATE:
|
---|
217 | hr_create_srv(&call);
|
---|
218 | break;
|
---|
219 | case HR_STATUS:
|
---|
220 | hr_print_status_srv(&call);
|
---|
221 | break;
|
---|
222 | default:
|
---|
223 | async_answer_0(&call, EINVAL);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | static hr_volume_t *hr_get_volume(service_id_t svc_id)
|
---|
229 | {
|
---|
230 | log_msg(LOG_DEFAULT, LVL_DEBUG, "hr_get_volume(): (%" PRIun ")",
|
---|
231 | svc_id);
|
---|
232 |
|
---|
233 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
234 | list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
|
---|
235 | if (volume->svc_id == svc_id) {
|
---|
236 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
237 | return volume;
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
242 | return NULL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | static void hr_client_conn(ipc_call_t *icall, void *arg)
|
---|
246 | {
|
---|
247 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_client_conn()");
|
---|
248 |
|
---|
249 | hr_volume_t *vol;
|
---|
250 |
|
---|
251 | service_id_t svc_id = ipc_get_arg2(icall);
|
---|
252 |
|
---|
253 | if (svc_id == ctl_sid) {
|
---|
254 | hr_ctl_conn(icall, arg);
|
---|
255 | } else {
|
---|
256 | log_msg(LOG_DEFAULT, LVL_NOTE, "bd_conn()");
|
---|
257 | vol = hr_get_volume(svc_id);
|
---|
258 | if (vol == NULL)
|
---|
259 | async_answer_0(icall, EINVAL);
|
---|
260 | bd_conn(icall, &vol->hr_bds);
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | int main(int argc, char **argv)
|
---|
265 | {
|
---|
266 | errno_t rc;
|
---|
267 |
|
---|
268 | printf("%s: HelenRAID server\n", NAME);
|
---|
269 |
|
---|
270 | rc = log_init(NAME);
|
---|
271 | if (rc != EOK) {
|
---|
272 | printf("%s: failed to initialize logging\n", NAME);
|
---|
273 | return 1;
|
---|
274 | }
|
---|
275 |
|
---|
276 | fibril_mutex_initialize(&big_lock);
|
---|
277 |
|
---|
278 | fibril_mutex_initialize(&hr_volumes_lock);
|
---|
279 | list_initialize(&hr_volumes);
|
---|
280 |
|
---|
281 | async_set_fallback_port_handler(hr_client_conn, NULL);
|
---|
282 |
|
---|
283 | rc = loc_server_register(NAME, &hr_srv);
|
---|
284 | if (rc != EOK) {
|
---|
285 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
286 | "failed registering server: %s", str_error(rc));
|
---|
287 | return EEXIST;
|
---|
288 | }
|
---|
289 |
|
---|
290 | rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
|
---|
291 | if (rc != EOK) {
|
---|
292 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
293 | "failed registering service: %s", str_error(rc));
|
---|
294 | return EEXIST;
|
---|
295 | }
|
---|
296 |
|
---|
297 | printf("%s: accepting connections\n", NAME);
|
---|
298 | task_retval(0);
|
---|
299 | async_manager();
|
---|
300 |
|
---|
301 | return 0;
|
---|
302 | }
|
---|
303 |
|
---|
304 | /** @}
|
---|
305 | */
|
---|