source: mainline/uspace/srv/volsrv/volsrv.c@ a46e56b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a46e56b was a46e56b, checked in by Jakub Jermar <jakub@…>, 7 years ago

Prefer handle over ID in naming handle variables

  • Property mode set to 100644
File size: 7.8 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(cap_call_handle_t, ipc_call_t *, void *);
55
56static 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
87static 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
120static 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
136static 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
183static 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
207static 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
245static 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) printf("vol_part_mkfs_srv: label='%s'\n", label);
265
266 rc = vol_part_find_by_id(sid, &part);
267 if (rc != EOK) {
268 free(label);
269 async_answer_0(icall_handle, ENOENT);
270 return;
271 }
272
273 rc = vol_part_mkfs_part(part, fstype, label);
274 if (rc != EOK) {
275 free(label);
276 async_answer_0(icall_handle, rc);
277 return;
278 }
279
280 free(label);
281 async_answer_0(icall_handle, EOK);
282}
283
284static void vol_client_conn(cap_call_handle_t icall_handle, ipc_call_t *icall, void *arg)
285{
286 log_msg(LOG_DEFAULT, LVL_DEBUG, "vol_client_conn()");
287
288 /* Accept the connection */
289 async_answer_0(icall_handle, EOK);
290
291 while (true) {
292 ipc_call_t call;
293 cap_call_handle_t chandle = async_get_call(&call);
294 sysarg_t method = IPC_GET_IMETHOD(call);
295
296 if (!method) {
297 /* The other side has hung up */
298 async_answer_0(chandle, EOK);
299 return;
300 }
301
302 switch (method) {
303 case VOL_GET_PARTS:
304 vol_get_parts_srv(chandle, &call);
305 break;
306 case VOL_PART_ADD:
307 vol_part_add_srv(chandle, &call);
308 break;
309 case VOL_PART_INFO:
310 vol_part_info_srv(chandle, &call);
311 break;
312 case VOL_PART_EMPTY:
313 vol_part_empty_srv(chandle, &call);
314 break;
315 case VOL_PART_LSUPP:
316 vol_part_get_lsupp_srv(chandle, &call);
317 break;
318 case VOL_PART_MKFS:
319 vol_part_mkfs_srv(chandle, &call);
320 break;
321 default:
322 async_answer_0(chandle, EINVAL);
323 }
324 }
325}
326
327int main(int argc, char *argv[])
328{
329 errno_t rc;
330
331 printf("%s: Volume service\n", NAME);
332
333 if (log_init(NAME) != EOK) {
334 printf(NAME ": Failed to initialize logging.\n");
335 return 1;
336 }
337
338 rc = vol_init();
339 if (rc != EOK)
340 return 1;
341
342 printf(NAME ": Accepting connections.\n");
343 task_retval(0);
344 async_manager();
345
346 return 0;
347}
348
349/** @}
350 */
Note: See TracBrowser for help on using the repository browser.