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_ctl_conn(ipc_call_t *icall, void *arg)
|
---|
139 | {
|
---|
140 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_ctl_conn()");
|
---|
141 |
|
---|
142 | async_accept_0(icall);
|
---|
143 |
|
---|
144 | while (true) {
|
---|
145 | ipc_call_t call;
|
---|
146 | async_get_call(&call);
|
---|
147 | sysarg_t method = ipc_get_imethod(&call);
|
---|
148 |
|
---|
149 | if (!method) {
|
---|
150 | async_answer_0(&call, EOK);
|
---|
151 | return;
|
---|
152 | }
|
---|
153 |
|
---|
154 | switch (method) {
|
---|
155 | case HR_CREATE:
|
---|
156 | hr_create_srv(&call);
|
---|
157 | break;
|
---|
158 | default:
|
---|
159 | async_answer_0(&call, EINVAL);
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | static hr_volume_t *hr_get_volume(service_id_t svc_id)
|
---|
165 | {
|
---|
166 | log_msg(LOG_DEFAULT, LVL_DEBUG, "hr_get_volume(): (%" PRIun ")",
|
---|
167 | svc_id);
|
---|
168 |
|
---|
169 | fibril_mutex_lock(&hr_volumes_lock);
|
---|
170 | list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
|
---|
171 | if (volume->svc_id == svc_id) {
|
---|
172 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
173 | return volume;
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | fibril_mutex_unlock(&hr_volumes_lock);
|
---|
178 | return NULL;
|
---|
179 | }
|
---|
180 |
|
---|
181 | static void hr_client_conn(ipc_call_t *icall, void *arg)
|
---|
182 | {
|
---|
183 | log_msg(LOG_DEFAULT, LVL_NOTE, "hr_client_conn()");
|
---|
184 |
|
---|
185 | hr_volume_t *vol;
|
---|
186 |
|
---|
187 | service_id_t svc_id = ipc_get_arg2(icall);
|
---|
188 |
|
---|
189 | if (svc_id == ctl_sid) {
|
---|
190 | hr_ctl_conn(icall, arg);
|
---|
191 | } else {
|
---|
192 | log_msg(LOG_DEFAULT, LVL_NOTE, "bd_conn()");
|
---|
193 | vol = hr_get_volume(svc_id);
|
---|
194 | if (vol == NULL)
|
---|
195 | async_answer_0(icall, EINVAL);
|
---|
196 | bd_conn(icall, &vol->hr_bds);
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | int main(int argc, char **argv)
|
---|
201 | {
|
---|
202 | errno_t rc;
|
---|
203 |
|
---|
204 | printf("%s: HelenRAID server\n", NAME);
|
---|
205 |
|
---|
206 | rc = log_init(NAME);
|
---|
207 | if (rc != EOK) {
|
---|
208 | printf("%s: failed to initialize logging\n", NAME);
|
---|
209 | return 1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | fibril_mutex_initialize(&big_lock);
|
---|
213 |
|
---|
214 | fibril_mutex_initialize(&hr_volumes_lock);
|
---|
215 | list_initialize(&hr_volumes);
|
---|
216 |
|
---|
217 | async_set_fallback_port_handler(hr_client_conn, NULL);
|
---|
218 |
|
---|
219 | rc = loc_server_register(NAME, &hr_srv);
|
---|
220 | if (rc != EOK) {
|
---|
221 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
222 | "failed registering server: %s", str_error(rc));
|
---|
223 | return EEXIST;
|
---|
224 | }
|
---|
225 |
|
---|
226 | rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
|
---|
227 | if (rc != EOK) {
|
---|
228 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
---|
229 | "failed registering service: %s", str_error(rc));
|
---|
230 | return EEXIST;
|
---|
231 | }
|
---|
232 |
|
---|
233 | printf("%s: accepting connections\n", NAME);
|
---|
234 | task_retval(0);
|
---|
235 | async_manager();
|
---|
236 |
|
---|
237 | return 0;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /** @}
|
---|
241 | */
|
---|