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