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

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

hr: create util.c

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