source: mainline/uspace/srv/bd/hr/hr.c@ 94d84a0

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

hr: initial trivial mirroring implementation

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[94d84a0]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 <block.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 <stdio.h>
48#include <stdlib.h>
49#include <str_error.h>
50
51#include "var.h"
52
53loc_srv_t *hr_srv;
54fibril_mutex_t big_lock; /* for now */
55
56static fibril_mutex_t hr_volumes_lock;
57static list_t hr_volumes;
58
59static service_id_t ctl_sid;
60
61errno_t hr_init_devs(hr_volume_t *vol)
62{
63 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_init_devs()");
64
65 errno_t rc;
66 size_t i;
67
68 for (i = 0; i < vol->dev_no; i++) {
69 rc = block_init(vol->devs[i]);
70 log_msg(LOG_DEFAULT, LVL_DEBUG,
71 "hr_init_devs(): initing (%" PRIun ")", vol->devs[i]);
72 if (rc != EOK) {
73 log_msg(LOG_DEFAULT, LVL_ERROR,
74 "hr_init_devs(): initing (%" PRIun ") failed, aborting",
75 vol->devs[i]);
76 break;
77 }
78 }
79
80 return rc;
81}
82
83void hr_fini_devs(hr_volume_t *vol)
84{
85 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_fini_devs()");
86
87 size_t i;
88
89 for (i = 0; i < vol->dev_no; i++)
90 block_fini(vol->devs[i]);
91}
92
93static void hr_create_srv(ipc_call_t *icall)
94{
95 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_create_srv()");
96
97 errno_t rc;
98 size_t size;
99 hr_config_t *hr_config;
100
101 hr_config = calloc(1, sizeof(hr_config_t));
102 if (hr_config == NULL) {
103 async_answer_0(icall, ENOMEM);
104 return;
105 }
106
107 hr_config->level = ipc_get_arg1(icall);
108
109 rc = async_data_write_accept((void **) &hr_config->name, true, 0, 0, 0,
110 NULL);
111 if (rc != EOK) {
112 async_answer_0(icall, EREFUSED);
113 return;
114 }
115
116 rc = async_data_write_accept((void **) &hr_config->devs, false,
117 sizeof(service_id_t), 0, 0, &size);
118 if (rc != EOK) {
119 async_answer_0(icall, EREFUSED);
120 return;
121 }
122
123 hr_config->dev_no = size / sizeof(service_id_t);
124
125 fibril_mutex_initialize(&big_lock);
126
127 hr_volume_t *new_volume = calloc(1, sizeof(hr_volume_t));
128 if (new_volume == NULL) {
129 rc = ENOMEM;
130 goto end;
131 }
132 new_volume->devname = hr_config->name;
133 new_volume->level = hr_config->level;
134 new_volume->dev_no = hr_config->dev_no;
135 new_volume->devs = hr_config->devs;
136
137 switch (hr_config->level) {
138 case hr_l_1:
139 new_volume->hr_ops.create = hr_raid1_create;
140 break;
141 default:
142 log_msg(LOG_DEFAULT, LVL_NOTE,
143 "level %d not implemented yet\n", new_volume->level);
144 rc = EINVAL;
145 goto end;
146 }
147
148 rc = new_volume->hr_ops.create(new_volume);
149 if (rc != EOK)
150 goto end;
151
152 fibril_mutex_lock(&hr_volumes_lock);
153 list_append(&new_volume->lvolumes, &hr_volumes);
154 fibril_mutex_unlock(&hr_volumes_lock);
155
156 log_msg(LOG_DEFAULT, LVL_NOTE, "created volume \"%s\" (%" PRIun ")\n",
157 new_volume->devname, new_volume->svc_id);
158
159end:
160 free(hr_config);
161 async_answer_0(icall, rc);
162}
163
164static void hr_ctl_conn(ipc_call_t *icall, void *arg)
165{
166 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_ctl_conn()");
167
168 async_accept_0(icall);
169
170 while (true) {
171 ipc_call_t call;
172 async_get_call(&call);
173 sysarg_t method = ipc_get_imethod(&call);
174
175 if (!method) {
176 async_answer_0(&call, EOK);
177 return;
178 }
179
180 switch (method) {
181 case HR_CREATE:
182 hr_create_srv(&call);
183 break;
184 default:
185 async_answer_0(&call, EINVAL);
186 }
187 }
188}
189
190static hr_volume_t *hr_get_volume(service_id_t svc_id)
191{
192 log_msg(LOG_DEFAULT, LVL_DEBUG, "hr_get_volume(): (%" PRIun ")",
193 svc_id);
194
195 fibril_mutex_lock(&hr_volumes_lock);
196 list_foreach(hr_volumes, lvolumes, hr_volume_t, volume) {
197 if (volume->svc_id == svc_id) {
198 fibril_mutex_unlock(&hr_volumes_lock);
199 return volume;
200 }
201 }
202
203 fibril_mutex_lock(&hr_volumes_lock);
204 return NULL;
205}
206
207static void hr_client_conn(ipc_call_t *icall, void *arg)
208{
209 log_msg(LOG_DEFAULT, LVL_NOTE, "hr_client_conn()");
210
211 hr_volume_t *vol;
212
213 sysarg_t svc_id = ipc_get_arg2(icall);
214
215 if (svc_id == ctl_sid) {
216 hr_ctl_conn(icall, arg);
217 } else {
218 log_msg(LOG_DEFAULT, LVL_NOTE, "bd_conn()");
219 vol = hr_get_volume(svc_id);
220 if (vol == NULL)
221 async_answer_0(icall, EINVAL);
222 bd_conn(icall, &vol->hr_bds);
223 }
224}
225
226int main(int argc, char **argv)
227{
228 errno_t rc;
229
230 printf("%s: HelenRAID server\n", NAME);
231
232 rc = log_init(NAME);
233 if (rc != EOK) {
234 printf("%s: failed to initialize logging\n", NAME);
235 return 1;
236 }
237
238 fibril_mutex_initialize(&hr_volumes_lock);
239 list_initialize(&hr_volumes);
240
241 async_set_fallback_port_handler(hr_client_conn, NULL);
242
243 rc = loc_server_register(NAME, &hr_srv);
244 if (rc != EOK) {
245 log_msg(LOG_DEFAULT, LVL_ERROR,
246 "failed registering server: %s", str_error(rc));
247 return EEXIST;
248 }
249
250 rc = loc_service_register(hr_srv, SERVICE_NAME_HR, &ctl_sid);
251 if (rc != EOK) {
252 log_msg(LOG_DEFAULT, LVL_ERROR,
253 "failed registering service: %s", str_error(rc));
254 return EEXIST;
255 }
256
257 printf("%s: accepting connections\n", NAME);
258 task_retval(0);
259 async_manager();
260
261 return 0;
262}
263
264/** @}
265 */
Note: See TracBrowser for help on using the repository browser.