[4802dd7] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 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 libc
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief Block device server stub
|
---|
| 35 | */
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <ipc/bd.h>
|
---|
| 38 | #include <macros.h>
|
---|
| 39 | #include <stdlib.h>
|
---|
[8d2dd7f2] | 40 | #include <stddef.h>
|
---|
| 41 | #include <stdint.h>
|
---|
[4802dd7] | 42 |
|
---|
| 43 | #include <bd_srv.h>
|
---|
| 44 |
|
---|
[984a9ba] | 45 | static void bd_read_blocks_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
[4802dd7] | 46 | {
|
---|
| 47 | aoff64_t ba;
|
---|
| 48 | size_t cnt;
|
---|
| 49 | void *buf;
|
---|
| 50 | size_t size;
|
---|
[b7fd2a0] | 51 | errno_t rc;
|
---|
[4802dd7] | 52 |
|
---|
[fafb8e5] | 53 | ba = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
|
---|
| 54 | cnt = ipc_get_arg3(call);
|
---|
[4802dd7] | 55 |
|
---|
[984a9ba] | 56 | ipc_call_t rcall;
|
---|
| 57 | if (!async_data_read_receive(&rcall, &size)) {
|
---|
| 58 | async_answer_0(call, EINVAL);
|
---|
[4802dd7] | 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | buf = malloc(size);
|
---|
| 63 | if (buf == NULL) {
|
---|
[984a9ba] | 64 | async_answer_0(&rcall, ENOMEM);
|
---|
| 65 | async_answer_0(call, ENOMEM);
|
---|
[4802dd7] | 66 | return;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[135486d] | 69 | if (srv->srvs->ops->read_blocks == NULL) {
|
---|
[984a9ba] | 70 | async_answer_0(&rcall, ENOTSUP);
|
---|
| 71 | async_answer_0(call, ENOTSUP);
|
---|
[ccfe9c3] | 72 | free(buf);
|
---|
[4802dd7] | 73 | return;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[135486d] | 76 | rc = srv->srvs->ops->read_blocks(srv, ba, cnt, buf, size);
|
---|
[4802dd7] | 77 | if (rc != EOK) {
|
---|
[984a9ba] | 78 | async_answer_0(&rcall, ENOMEM);
|
---|
| 79 | async_answer_0(call, ENOMEM);
|
---|
[ccfe9c3] | 80 | free(buf);
|
---|
[4802dd7] | 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[984a9ba] | 84 | async_data_read_finalize(&rcall, buf, size);
|
---|
[4802dd7] | 85 |
|
---|
| 86 | free(buf);
|
---|
[984a9ba] | 87 | async_answer_0(call, EOK);
|
---|
[4802dd7] | 88 | }
|
---|
| 89 |
|
---|
[984a9ba] | 90 | static void bd_read_toc_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
[4802dd7] | 91 | {
|
---|
| 92 | uint8_t session;
|
---|
| 93 | void *buf;
|
---|
| 94 | size_t size;
|
---|
[b7fd2a0] | 95 | errno_t rc;
|
---|
[4802dd7] | 96 |
|
---|
[fafb8e5] | 97 | session = ipc_get_arg1(call);
|
---|
[4802dd7] | 98 |
|
---|
[984a9ba] | 99 | ipc_call_t rcall;
|
---|
| 100 | if (!async_data_read_receive(&rcall, &size)) {
|
---|
| 101 | async_answer_0(call, EINVAL);
|
---|
[4802dd7] | 102 | return;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | buf = malloc(size);
|
---|
| 106 | if (buf == NULL) {
|
---|
[984a9ba] | 107 | async_answer_0(&rcall, ENOMEM);
|
---|
| 108 | async_answer_0(call, ENOMEM);
|
---|
[4802dd7] | 109 | return;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[135486d] | 112 | if (srv->srvs->ops->read_toc == NULL) {
|
---|
[984a9ba] | 113 | async_answer_0(&rcall, ENOTSUP);
|
---|
| 114 | async_answer_0(call, ENOTSUP);
|
---|
[ccfe9c3] | 115 | free(buf);
|
---|
[4802dd7] | 116 | return;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[135486d] | 119 | rc = srv->srvs->ops->read_toc(srv, session, buf, size);
|
---|
[4802dd7] | 120 | if (rc != EOK) {
|
---|
[984a9ba] | 121 | async_answer_0(&rcall, ENOMEM);
|
---|
| 122 | async_answer_0(call, ENOMEM);
|
---|
[ccfe9c3] | 123 | free(buf);
|
---|
[4802dd7] | 124 | return;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[984a9ba] | 127 | async_data_read_finalize(&rcall, buf, size);
|
---|
[4802dd7] | 128 |
|
---|
| 129 | free(buf);
|
---|
[984a9ba] | 130 | async_answer_0(call, EOK);
|
---|
[4802dd7] | 131 | }
|
---|
| 132 |
|
---|
[984a9ba] | 133 | static void bd_sync_cache_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
[dd8b6a8] | 134 | {
|
---|
| 135 | aoff64_t ba;
|
---|
| 136 | size_t cnt;
|
---|
[b7fd2a0] | 137 | errno_t rc;
|
---|
[dd8b6a8] | 138 |
|
---|
[fafb8e5] | 139 | ba = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
|
---|
| 140 | cnt = ipc_get_arg3(call);
|
---|
[dd8b6a8] | 141 |
|
---|
| 142 | if (srv->srvs->ops->sync_cache == NULL) {
|
---|
[984a9ba] | 143 | async_answer_0(call, ENOTSUP);
|
---|
[dd8b6a8] | 144 | return;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | rc = srv->srvs->ops->sync_cache(srv, ba, cnt);
|
---|
[984a9ba] | 148 | async_answer_0(call, rc);
|
---|
[dd8b6a8] | 149 | }
|
---|
| 150 |
|
---|
[984a9ba] | 151 | static void bd_write_blocks_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
[4802dd7] | 152 | {
|
---|
| 153 | aoff64_t ba;
|
---|
| 154 | size_t cnt;
|
---|
| 155 | void *data;
|
---|
| 156 | size_t size;
|
---|
[b7fd2a0] | 157 | errno_t rc;
|
---|
[4802dd7] | 158 |
|
---|
[fafb8e5] | 159 | ba = MERGE_LOUP32(ipc_get_arg1(call), ipc_get_arg2(call));
|
---|
| 160 | cnt = ipc_get_arg3(call);
|
---|
[4802dd7] | 161 |
|
---|
| 162 | rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
|
---|
| 163 | if (rc != EOK) {
|
---|
[984a9ba] | 164 | async_answer_0(call, rc);
|
---|
[4802dd7] | 165 | return;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[135486d] | 168 | if (srv->srvs->ops->write_blocks == NULL) {
|
---|
[984a9ba] | 169 | async_answer_0(call, ENOTSUP);
|
---|
[4802dd7] | 170 | return;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[135486d] | 173 | rc = srv->srvs->ops->write_blocks(srv, ba, cnt, data, size);
|
---|
[4802dd7] | 174 | free(data);
|
---|
[984a9ba] | 175 | async_answer_0(call, rc);
|
---|
[4802dd7] | 176 | }
|
---|
| 177 |
|
---|
[984a9ba] | 178 | static void bd_get_block_size_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
[4802dd7] | 179 | {
|
---|
[b7fd2a0] | 180 | errno_t rc;
|
---|
[4802dd7] | 181 | size_t block_size;
|
---|
| 182 |
|
---|
[135486d] | 183 | if (srv->srvs->ops->get_block_size == NULL) {
|
---|
[984a9ba] | 184 | async_answer_0(call, ENOTSUP);
|
---|
[4802dd7] | 185 | return;
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[135486d] | 188 | rc = srv->srvs->ops->get_block_size(srv, &block_size);
|
---|
[984a9ba] | 189 | async_answer_1(call, rc, block_size);
|
---|
[4802dd7] | 190 | }
|
---|
| 191 |
|
---|
[984a9ba] | 192 | static void bd_get_num_blocks_srv(bd_srv_t *srv, ipc_call_t *call)
|
---|
[4802dd7] | 193 | {
|
---|
[b7fd2a0] | 194 | errno_t rc;
|
---|
[4802dd7] | 195 | aoff64_t num_blocks;
|
---|
| 196 |
|
---|
[135486d] | 197 | if (srv->srvs->ops->get_num_blocks == NULL) {
|
---|
[984a9ba] | 198 | async_answer_0(call, ENOTSUP);
|
---|
[4802dd7] | 199 | return;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[135486d] | 202 | rc = srv->srvs->ops->get_num_blocks(srv, &num_blocks);
|
---|
[984a9ba] | 203 | async_answer_2(call, rc, LOWER32(num_blocks), UPPER32(num_blocks));
|
---|
[4802dd7] | 204 | }
|
---|
| 205 |
|
---|
[135486d] | 206 | static bd_srv_t *bd_srv_create(bd_srvs_t *srvs)
|
---|
[4802dd7] | 207 | {
|
---|
[135486d] | 208 | bd_srv_t *srv;
|
---|
| 209 |
|
---|
[75751db6] | 210 | srv = calloc(1, sizeof(bd_srv_t));
|
---|
[135486d] | 211 | if (srv == NULL)
|
---|
| 212 | return NULL;
|
---|
| 213 |
|
---|
| 214 | srv->srvs = srvs;
|
---|
| 215 | return srv;
|
---|
[4802dd7] | 216 | }
|
---|
| 217 |
|
---|
[135486d] | 218 | void bd_srvs_init(bd_srvs_t *srvs)
|
---|
[4802dd7] | 219 | {
|
---|
[135486d] | 220 | srvs->ops = NULL;
|
---|
| 221 | srvs->sarg = NULL;
|
---|
| 222 | }
|
---|
[4802dd7] | 223 |
|
---|
[984a9ba] | 224 | errno_t bd_conn(ipc_call_t *icall, bd_srvs_t *srvs)
|
---|
[135486d] | 225 | {
|
---|
| 226 | bd_srv_t *srv;
|
---|
[b7fd2a0] | 227 | errno_t rc;
|
---|
[4802dd7] | 228 |
|
---|
| 229 | /* Accept the connection */
|
---|
[beb83c1] | 230 | async_accept_0(icall);
|
---|
[4802dd7] | 231 |
|
---|
[135486d] | 232 | srv = bd_srv_create(srvs);
|
---|
| 233 | if (srv == NULL)
|
---|
| 234 | return ENOMEM;
|
---|
| 235 |
|
---|
[4802dd7] | 236 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 237 | if (sess == NULL)
|
---|
| 238 | return ENOMEM;
|
---|
| 239 |
|
---|
| 240 | srv->client_sess = sess;
|
---|
| 241 |
|
---|
[135486d] | 242 | rc = srvs->ops->open(srvs, srv);
|
---|
[4802dd7] | 243 | if (rc != EOK)
|
---|
| 244 | return rc;
|
---|
| 245 |
|
---|
| 246 | while (true) {
|
---|
| 247 | ipc_call_t call;
|
---|
[984a9ba] | 248 | async_get_call(&call);
|
---|
[fafb8e5] | 249 | sysarg_t method = ipc_get_imethod(&call);
|
---|
[4802dd7] | 250 |
|
---|
| 251 | if (!method) {
|
---|
| 252 | /* The other side has hung up */
|
---|
[984a9ba] | 253 | async_answer_0(&call, EOK);
|
---|
[4802dd7] | 254 | break;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | switch (method) {
|
---|
| 258 | case BD_READ_BLOCKS:
|
---|
[984a9ba] | 259 | bd_read_blocks_srv(srv, &call);
|
---|
[4802dd7] | 260 | break;
|
---|
| 261 | case BD_READ_TOC:
|
---|
[984a9ba] | 262 | bd_read_toc_srv(srv, &call);
|
---|
[4802dd7] | 263 | break;
|
---|
[dd8b6a8] | 264 | case BD_SYNC_CACHE:
|
---|
[984a9ba] | 265 | bd_sync_cache_srv(srv, &call);
|
---|
[dd8b6a8] | 266 | break;
|
---|
[4802dd7] | 267 | case BD_WRITE_BLOCKS:
|
---|
[984a9ba] | 268 | bd_write_blocks_srv(srv, &call);
|
---|
[4802dd7] | 269 | break;
|
---|
| 270 | case BD_GET_BLOCK_SIZE:
|
---|
[984a9ba] | 271 | bd_get_block_size_srv(srv, &call);
|
---|
[4802dd7] | 272 | break;
|
---|
| 273 | case BD_GET_NUM_BLOCKS:
|
---|
[984a9ba] | 274 | bd_get_num_blocks_srv(srv, &call);
|
---|
[4802dd7] | 275 | break;
|
---|
| 276 | default:
|
---|
[984a9ba] | 277 | async_answer_0(&call, EINVAL);
|
---|
[4802dd7] | 278 | }
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[135486d] | 281 | rc = srvs->ops->close(srv);
|
---|
| 282 | free(srv);
|
---|
| 283 |
|
---|
| 284 | return rc;
|
---|
[4802dd7] | 285 | }
|
---|
| 286 |
|
---|
| 287 | /** @}
|
---|
| 288 | */
|
---|