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