source: mainline/uspace/srv/volsrv/volsrv.c@ 9b1baac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9b1baac was e89a06a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Encapsulate partitions list in volume server. (Global state is not good coding practice.)

  • Property mode set to 100644
File size: 8.3 KB
Line 
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
54static void vol_client_conn(ipc_call_t *, void *);
55
56static errno_t vol_init(void)
57{
58 errno_t rc;
59 vol_parts_t *parts = NULL;
60
61 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_init()");
62
63 rc = vol_parts_create(&parts);
64 if (rc != EOK)
65 goto error;
66
67 rc = vol_part_discovery_start(parts);
68 if (rc != EOK)
69 goto error;
70
71 async_set_fallback_port_handler(vol_client_conn, parts);
72
73 rc = loc_server_register(NAME);
74 if (rc != EOK) {
75 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server: %s.", str_error(rc));
76 rc = EEXIST;
77 }
78
79 service_id_t sid;
80 rc = loc_service_register(SERVICE_NAME_VOLSRV, &sid);
81 if (rc != EOK) {
82 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service: %s.", str_error(rc));
83 rc = EEXIST;
84 goto error;
85 }
86
87 return EOK;
88error:
89 vol_parts_destroy(parts);
90 return rc;
91}
92
93static void vol_get_parts_srv(vol_parts_t *parts, ipc_call_t *icall)
94{
95 ipc_call_t call;
96 size_t size;
97 size_t act_size;
98 errno_t rc;
99
100 if (!async_data_read_receive(&call, &size)) {
101 async_answer_0(&call, EREFUSED);
102 async_answer_0(icall, EREFUSED);
103 return;
104 }
105
106 service_id_t *id_buf = (service_id_t *) malloc(size);
107 if (id_buf == NULL) {
108 async_answer_0(&call, ENOMEM);
109 async_answer_0(icall, ENOMEM);
110 return;
111 }
112
113 rc = vol_part_get_ids(parts, id_buf, size, &act_size);
114 if (rc != EOK) {
115 async_answer_0(&call, rc);
116 async_answer_0(icall, rc);
117 return;
118 }
119
120 errno_t retval = async_data_read_finalize(&call, id_buf, size);
121 free(id_buf);
122
123 async_answer_1(icall, retval, act_size);
124}
125
126static void vol_part_add_srv(vol_parts_t *parts, ipc_call_t *icall)
127{
128 service_id_t sid;
129 errno_t rc;
130
131 sid = IPC_GET_ARG1(*icall);
132
133 rc = vol_part_add(parts, sid);
134 if (rc != EOK) {
135 async_answer_0(icall, rc);
136 return;
137 }
138
139 async_answer_0(icall, EOK);
140}
141
142static void vol_part_info_srv(vol_parts_t *parts, ipc_call_t *icall)
143{
144 service_id_t sid;
145 vol_part_t *part;
146 vol_part_info_t pinfo;
147 errno_t rc;
148
149 sid = IPC_GET_ARG1(*icall);
150 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_info_srv(%zu)",
151 sid);
152 rc = vol_part_find_by_id_ref(parts, sid, &part);
153 if (rc != EOK) {
154 async_answer_0(icall, ENOENT);
155 return;
156 }
157
158 rc = vol_part_get_info(part, &pinfo);
159 if (rc != EOK) {
160 async_answer_0(icall, EIO);
161 goto error;
162 }
163
164 ipc_call_t call;
165 size_t size;
166 if (!async_data_read_receive(&call, &size)) {
167 async_answer_0(&call, EREFUSED);
168 async_answer_0(icall, EREFUSED);
169 goto error;
170 }
171
172 if (size != sizeof(vol_part_info_t)) {
173 async_answer_0(&call, EINVAL);
174 async_answer_0(icall, EINVAL);
175 goto error;
176 }
177
178 rc = async_data_read_finalize(&call, &pinfo,
179 min(size, sizeof(pinfo)));
180 if (rc != EOK) {
181 async_answer_0(&call, rc);
182 async_answer_0(icall, rc);
183 goto error;
184 }
185
186 async_answer_0(icall, EOK);
187error:
188 vol_part_del_ref(part);
189}
190
191static void vol_part_eject_srv(vol_parts_t *parts, ipc_call_t *icall)
192{
193 service_id_t sid;
194 vol_part_t *part;
195 errno_t rc;
196
197 sid = IPC_GET_ARG1(*icall);
198 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_eject_srv(%zu)", sid);
199
200 rc = vol_part_find_by_id_ref(parts, sid, &part);
201 if (rc != EOK) {
202 async_answer_0(icall, ENOENT);
203 goto error;
204 }
205
206 rc = vol_part_eject_part(part);
207 if (rc != EOK) {
208 async_answer_0(icall, EIO);
209 goto error;
210 }
211
212 async_answer_0(icall, EOK);
213error:
214 vol_part_del_ref(part);
215}
216
217static void vol_part_empty_srv(vol_parts_t *parts, ipc_call_t *icall)
218{
219 service_id_t sid;
220 vol_part_t *part;
221 errno_t rc;
222
223 sid = IPC_GET_ARG1(*icall);
224 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_empty_srv(%zu)", sid);
225
226 rc = vol_part_find_by_id_ref(parts, sid, &part);
227 if (rc != EOK) {
228 async_answer_0(icall, ENOENT);
229 return;
230 }
231
232 rc = vol_part_empty_part(part);
233 if (rc != EOK) {
234 async_answer_0(icall, EIO);
235 goto error;
236 }
237
238 async_answer_0(icall, EOK);
239error:
240 vol_part_del_ref(part);
241}
242
243static void vol_part_get_lsupp_srv(vol_parts_t *parts, ipc_call_t *icall)
244{
245 vol_fstype_t fstype;
246 vol_label_supp_t vlsupp;
247 errno_t rc;
248
249 fstype = IPC_GET_ARG1(*icall);
250 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_part_get_lsupp_srv(%u)",
251 fstype);
252
253 volsrv_part_get_lsupp(fstype, &vlsupp);
254
255 ipc_call_t call;
256 size_t size;
257 if (!async_data_read_receive(&call, &size)) {
258 async_answer_0(&call, EREFUSED);
259 async_answer_0(icall, EREFUSED);
260 return;
261 }
262
263 if (size != sizeof(vol_label_supp_t)) {
264 async_answer_0(&call, EINVAL);
265 async_answer_0(icall, EINVAL);
266 return;
267 }
268
269 rc = async_data_read_finalize(&call, &vlsupp,
270 min(size, sizeof(vlsupp)));
271 if (rc != EOK) {
272 async_answer_0(&call, rc);
273 async_answer_0(icall, rc);
274 return;
275 }
276
277 async_answer_0(icall, EOK);
278}
279
280
281static void vol_part_mkfs_srv(vol_parts_t *parts, ipc_call_t *icall)
282{
283 service_id_t sid;
284 vol_part_t *part;
285 vol_fstype_t fstype;
286 char *label;
287 errno_t rc;
288
289 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv()");
290
291 sid = IPC_GET_ARG1(*icall);
292 fstype = IPC_GET_ARG2(*icall);
293
294 rc = async_data_write_accept((void **)&label, true, 0, VOL_LABEL_MAXLEN,
295 0, NULL);
296 if (rc != EOK) {
297 async_answer_0(icall, rc);
298 return;
299 }
300
301 if (label != NULL) {
302 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_mkfs_srv: label='%s'",
303 label);
304 }
305
306 rc = vol_part_find_by_id_ref(parts, sid, &part);
307 if (rc != EOK) {
308 free(label);
309 async_answer_0(icall, ENOENT);
310 return;
311 }
312
313 rc = vol_part_mkfs_part(part, fstype, label);
314 if (rc != EOK) {
315 free(label);
316 async_answer_0(icall, rc);
317 vol_part_del_ref(part);
318 return;
319 }
320
321 free(label);
322 async_answer_0(icall, EOK);
323}
324
325static void vol_client_conn(ipc_call_t *icall, void *arg)
326{
327 vol_parts_t *parts = (vol_parts_t *) arg;
328
329 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
330
331 /* Accept the connection */
332 async_answer_0(icall, EOK);
333
334 while (true) {
335 ipc_call_t call;
336 async_get_call(&call);
337 sysarg_t method = IPC_GET_IMETHOD(call);
338
339 if (!method) {
340 /* The other side has hung up */
341 async_answer_0(&call, EOK);
342 return;
343 }
344
345 switch (method) {
346 case VOL_GET_PARTS:
347 vol_get_parts_srv(parts, &call);
348 break;
349 case VOL_PART_ADD:
350 vol_part_add_srv(parts, &call);
351 break;
352 case VOL_PART_INFO:
353 vol_part_info_srv(parts, &call);
354 break;
355 case VOL_PART_EJECT:
356 vol_part_eject_srv(parts, &call);
357 break;
358 case VOL_PART_EMPTY:
359 vol_part_empty_srv(parts, &call);
360 break;
361 case VOL_PART_LSUPP:
362 vol_part_get_lsupp_srv(parts, &call);
363 break;
364 case VOL_PART_MKFS:
365 vol_part_mkfs_srv(parts, &call);
366 break;
367 default:
368 async_answer_0(&call, EINVAL);
369 }
370 }
371}
372
373int main(int argc, char *argv[])
374{
375 errno_t rc;
376
377 printf("%s: Volume service\n", NAME);
378
379 if (log_init(NAME) != EOK) {
380 printf(NAME ": Failed to initialize logging.\n");
381 return 1;
382 }
383
384 rc = vol_init();
385 if (rc != EOK)
386 return 1;
387
388 printf(NAME ": Accepting connections.\n");
389 task_retval(0);
390 async_manager();
391
392 return 0;
393}
394
395/** @}
396 */
Note: See TracBrowser for help on using the repository browser.