1 | /*
|
---|
2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
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 volsrv
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file Volume service
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <async.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <str_error.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <ipc/services.h>
|
---|
41 | #include <ipc/vol.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <macros.h>
|
---|
44 | #include <stdio.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <task.h>
|
---|
47 | #include <types/vol.h>
|
---|
48 |
|
---|
49 | #include "mkfs.h"
|
---|
50 | #include "part.h"
|
---|
51 |
|
---|
52 | #define NAME "volsrv"
|
---|
53 |
|
---|
54 | static void vol_client_conn(cap_call_handle_t, ipc_call_t *, void *);
|
---|
55 |
|
---|
56 | static errno_t vol_init(void)
|
---|
57 | {
|
---|
58 | errno_t rc;
|
---|
59 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_init()");
|
---|
60 |
|
---|
61 | rc = vol_part_init();
|
---|
62 | if (rc != EOK)
|
---|
63 | return rc;
|
---|
64 |
|
---|
65 | rc = vol_part_discovery_start();
|
---|
66 | if (rc != EOK)
|
---|
67 | return rc;
|
---|
68 |
|
---|
69 | async_set_fallback_port_handler(vol_client_conn, NULL);
|
---|
70 |
|
---|
71 | rc = loc_server_register(NAME);
|
---|
72 | if (rc != EOK) {
|
---|
73 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
|
---|
74 | return EEXIST;
|
---|
75 | }
|
---|
76 |
|
---|
77 | service_id_t sid;
|
---|
78 | rc = loc_service_register(SERVICE_NAME_VOLSRV, &sid);
|
---|
79 | if (rc != EOK) {
|
---|
80 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
|
---|
81 | return EEXIST;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | static void vol_get_parts_srv(cap_call_handle_t icall_handle, ipc_call_t *icall)
|
---|
88 | {
|
---|
89 | cap_call_handle_t chandle;
|
---|
90 | size_t size;
|
---|
91 | size_t act_size;
|
---|
92 | errno_t rc;
|
---|
93 |
|
---|
94 | if (!async_data_read_receive(&chandle, &size)) {
|
---|
95 | async_answer_0(chandle, EREFUSED);
|
---|
96 | async_answer_0(icall_handle, EREFUSED);
|
---|
97 | return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | service_id_t *id_buf = (service_id_t *) malloc(size);
|
---|
101 | if (id_buf == NULL) {
|
---|
102 | async_answer_0(chandle, ENOMEM);
|
---|
103 | async_answer_0(icall_handle, ENOMEM);
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | rc = vol_part_get_ids(id_buf, size, &act_size);
|
---|
108 | if (rc != EOK) {
|
---|
109 | async_answer_0(chandle, rc);
|
---|
110 | async_answer_0(icall_handle, rc);
|
---|
111 | return;
|
---|
112 | }
|
---|
113 |
|
---|
114 | errno_t retval = async_data_read_finalize(chandle, id_buf, size);
|
---|
115 | free(id_buf);
|
---|
116 |
|
---|
117 | async_answer_1(icall_handle, retval, act_size);
|
---|
118 | }
|
---|
119 |
|
---|
120 | static void vol_part_add_srv(cap_call_handle_t icall_handle, ipc_call_t *icall)
|
---|
121 | {
|
---|
122 | service_id_t sid;
|
---|
123 | errno_t rc;
|
---|
124 |
|
---|
125 | sid = IPC_GET_ARG1(*icall);
|
---|
126 |
|
---|
127 | rc = vol_part_add(sid);
|
---|
128 | if (rc != EOK) {
|
---|
129 | async_answer_0(icall_handle, rc);
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | async_answer_0(icall_handle, EOK);
|
---|
134 | }
|
---|
135 |
|
---|
136 | static void vol_part_info_srv(cap_call_handle_t icall_handle, ipc_call_t *icall)
|
---|
137 | {
|
---|
138 | service_id_t sid;
|
---|
139 | vol_part_t *part;
|
---|
140 | vol_part_info_t pinfo;
|
---|
141 | errno_t rc;
|
---|
142 |
|
---|
143 | sid = IPC_GET_ARG1(*icall);
|
---|
144 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_info_srv(%zu)",
|
---|
145 | sid);
|
---|
146 | rc = vol_part_find_by_id(sid, &part);
|
---|
147 | if (rc != EOK) {
|
---|
148 | async_answer_0(icall_handle, ENOENT);
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | rc = vol_part_get_info(part, &pinfo);
|
---|
153 | if (rc != EOK) {
|
---|
154 | async_answer_0(icall_handle, EIO);
|
---|
155 | return;
|
---|
156 | }
|
---|
157 |
|
---|
158 | cap_call_handle_t chandle;
|
---|
159 | size_t size;
|
---|
160 | if (!async_data_read_receive(&chandle, &size)) {
|
---|
161 | async_answer_0(chandle, EREFUSED);
|
---|
162 | async_answer_0(icall_handle, EREFUSED);
|
---|
163 | return;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (size != sizeof(vol_part_info_t)) {
|
---|
167 | async_answer_0(chandle, EINVAL);
|
---|
168 | async_answer_0(icall_handle, EINVAL);
|
---|
169 | return;
|
---|
170 | }
|
---|
171 |
|
---|
172 | rc = async_data_read_finalize(chandle, &pinfo,
|
---|
173 | min(size, sizeof(pinfo)));
|
---|
174 | if (rc != EOK) {
|
---|
175 | async_answer_0(chandle, rc);
|
---|
176 | async_answer_0(icall_handle, rc);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | async_answer_0(icall_handle, EOK);
|
---|
181 | }
|
---|
182 |
|
---|
183 | static void vol_part_empty_srv(cap_call_handle_t icall_handle, ipc_call_t *icall)
|
---|
184 | {
|
---|
185 | service_id_t sid;
|
---|
186 | vol_part_t *part;
|
---|
187 | errno_t rc;
|
---|
188 |
|
---|
189 | sid = IPC_GET_ARG1(*icall);
|
---|
190 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_srv(%zu)", sid);
|
---|
191 |
|
---|
192 | rc = vol_part_find_by_id(sid, &part);
|
---|
193 | if (rc != EOK) {
|
---|
194 | async_answer_0(icall_handle, ENOENT);
|
---|
195 | return;
|
---|
196 | }
|
---|
197 |
|
---|
198 | rc = vol_part_empty_part(part);
|
---|
199 | if (rc != EOK) {
|
---|
200 | async_answer_0(icall_handle, EIO);
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | async_answer_0(icall_handle, EOK);
|
---|
205 | }
|
---|
206 |
|
---|
207 | static void vol_part_get_lsupp_srv(cap_call_handle_t icall_handle, ipc_call_t *icall)
|
---|
208 | {
|
---|
209 | vol_fstype_t fstype;
|
---|
210 | vol_label_supp_t vlsupp;
|
---|
211 | errno_t rc;
|
---|
212 |
|
---|
213 | fstype = IPC_GET_ARG1(*icall);
|
---|
214 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_get_lsupp_srv(%u)",
|
---|
215 | fstype);
|
---|
216 |
|
---|
217 | volsrv_part_get_lsupp(fstype, &vlsupp);
|
---|
218 |
|
---|
219 | cap_call_handle_t chandle;
|
---|
220 | size_t size;
|
---|
221 | if (!async_data_read_receive(&chandle, &size)) {
|
---|
222 | async_answer_0(chandle, EREFUSED);
|
---|
223 | async_answer_0(icall_handle, EREFUSED);
|
---|
224 | return;
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (size != sizeof(vol_label_supp_t)) {
|
---|
228 | async_answer_0(chandle, EINVAL);
|
---|
229 | async_answer_0(icall_handle, EINVAL);
|
---|
230 | return;
|
---|
231 | }
|
---|
232 |
|
---|
233 | rc = async_data_read_finalize(chandle, &vlsupp,
|
---|
234 | min(size, sizeof(vlsupp)));
|
---|
235 | if (rc != EOK) {
|
---|
236 | async_answer_0(chandle, rc);
|
---|
237 | async_answer_0(icall_handle, rc);
|
---|
238 | return;
|
---|
239 | }
|
---|
240 |
|
---|
241 | async_answer_0(icall_handle, EOK);
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | static void vol_part_mkfs_srv(cap_call_handle_t icall_handle, ipc_call_t *icall)
|
---|
246 | {
|
---|
247 | service_id_t sid;
|
---|
248 | vol_part_t *part;
|
---|
249 | vol_fstype_t fstype;
|
---|
250 | char *label;
|
---|
251 | errno_t rc;
|
---|
252 |
|
---|
253 | sid = IPC_GET_ARG1(*icall);
|
---|
254 | fstype = IPC_GET_ARG2(*icall);
|
---|
255 |
|
---|
256 | rc = async_data_write_accept((void **)&label, true, 0, VOL_LABEL_MAXLEN,
|
---|
257 | 0, NULL);
|
---|
258 | if (rc != EOK) {
|
---|
259 | async_answer_0(icall_handle, rc);
|
---|
260 | return;
|
---|
261 | }
|
---|
262 |
|
---|
263 | printf("vol_part_mkfs_srv: label=%p\n", label);
|
---|
264 | if (label != NULL)
|
---|
265 | printf("vol_part_mkfs_srv: label='%s'\n", label);
|
---|
266 |
|
---|
267 | rc = vol_part_find_by_id(sid, &part);
|
---|
268 | if (rc != EOK) {
|
---|
269 | free(label);
|
---|
270 | async_answer_0(icall_handle, ENOENT);
|
---|
271 | return;
|
---|
272 | }
|
---|
273 |
|
---|
274 | rc = vol_part_mkfs_part(part, fstype, label);
|
---|
275 | if (rc != EOK) {
|
---|
276 | free(label);
|
---|
277 | async_answer_0(icall_handle, rc);
|
---|
278 | return;
|
---|
279 | }
|
---|
280 |
|
---|
281 | free(label);
|
---|
282 | async_answer_0(icall_handle, EOK);
|
---|
283 | }
|
---|
284 |
|
---|
285 | static void vol_client_conn(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
|
---|
286 | {
|
---|
287 | log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
|
---|
288 |
|
---|
289 | /* Accept the connection */
|
---|
290 | async_answer_0(icall_handle, EOK);
|
---|
291 |
|
---|
292 | while (true) {
|
---|
293 | ipc_call_t call;
|
---|
294 | cap_call_handle_t chandle = async_get_call(&call);
|
---|
295 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
296 |
|
---|
297 | if (!method) {
|
---|
298 | /* The other side has hung up */
|
---|
299 | async_answer_0(chandle, EOK);
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 | switch (method) {
|
---|
304 | case VOL_GET_PARTS:
|
---|
305 | vol_get_parts_srv(chandle, &call);
|
---|
306 | break;
|
---|
307 | case VOL_PART_ADD:
|
---|
308 | vol_part_add_srv(chandle, &call);
|
---|
309 | break;
|
---|
310 | case VOL_PART_INFO:
|
---|
311 | vol_part_info_srv(chandle, &call);
|
---|
312 | break;
|
---|
313 | case VOL_PART_EMPTY:
|
---|
314 | vol_part_empty_srv(chandle, &call);
|
---|
315 | break;
|
---|
316 | case VOL_PART_LSUPP:
|
---|
317 | vol_part_get_lsupp_srv(chandle, &call);
|
---|
318 | break;
|
---|
319 | case VOL_PART_MKFS:
|
---|
320 | vol_part_mkfs_srv(chandle, &call);
|
---|
321 | break;
|
---|
322 | default:
|
---|
323 | async_answer_0(chandle, EINVAL);
|
---|
324 | }
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | int main(int argc, char *argv[])
|
---|
329 | {
|
---|
330 | errno_t rc;
|
---|
331 |
|
---|
332 | printf("%s: Volume service\n", NAME);
|
---|
333 |
|
---|
334 | if (log_init(NAME) != EOK) {
|
---|
335 | printf(NAME ": Failed to initialize logging.\n");
|
---|
336 | return 1;
|
---|
337 | }
|
---|
338 |
|
---|
339 | rc = vol_init();
|
---|
340 | if (rc != EOK)
|
---|
341 | return 1;
|
---|
342 |
|
---|
343 | printf(NAME ": Accepting connections.\n");
|
---|
344 | task_retval(0);
|
---|
345 | async_manager();
|
---|
346 |
|
---|
347 | return 0;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /** @}
|
---|
351 | */
|
---|