| 1 | /*
|
|---|
| 2 | * Copyright (c) 2008 Jakub Jermar
|
|---|
| 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 | #include "vfs.h"
|
|---|
| 30 |
|
|---|
| 31 | #include <errno.h>
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <str.h>
|
|---|
| 34 | #include <vfs/canonify.h>
|
|---|
| 35 |
|
|---|
| 36 | static void vfs_in_clone(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 37 | {
|
|---|
| 38 | int oldfd = IPC_GET_ARG1(*request);
|
|---|
| 39 | bool desc = IPC_GET_ARG2(*request);
|
|---|
| 40 |
|
|---|
| 41 | int ret = vfs_op_clone(oldfd, desc);
|
|---|
| 42 | async_answer_0(rid, ret);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | static void vfs_in_close(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 46 | {
|
|---|
| 47 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 48 | int rc = vfs_op_close(fd);
|
|---|
| 49 | async_answer_0(rid, rc);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | static void vfs_in_dup(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 53 | {
|
|---|
| 54 | int oldfd = IPC_GET_ARG1(*request);
|
|---|
| 55 | int newfd = IPC_GET_ARG2(*request);
|
|---|
| 56 | int rc = vfs_op_dup(oldfd, newfd);
|
|---|
| 57 | async_answer_1(rid, rc, newfd);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | static void vfs_in_fstat(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 61 | {
|
|---|
| 62 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 63 | int rc = vfs_op_fstat_forward(fd);
|
|---|
| 64 | async_answer_0(rid, rc);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | static void vfs_in_mount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 68 | {
|
|---|
| 69 | int mpfd = IPC_GET_ARG1(*request);
|
|---|
| 70 |
|
|---|
| 71 | /*
|
|---|
| 72 | * We expect the library to do the device-name to device-handle
|
|---|
| 73 | * translation for us, thus the device handle will arrive as ARG1
|
|---|
| 74 | * in the request.
|
|---|
| 75 | */
|
|---|
| 76 | service_id_t service_id = (service_id_t) IPC_GET_ARG2(*request);
|
|---|
| 77 |
|
|---|
| 78 | unsigned int flags = (unsigned int) IPC_GET_ARG3(*request);
|
|---|
| 79 | unsigned int instance = IPC_GET_ARG4(*request);
|
|---|
| 80 |
|
|---|
| 81 | char *opts = NULL;
|
|---|
| 82 | char *fs_name = NULL;
|
|---|
| 83 |
|
|---|
| 84 | /* Now we expect to receive the mount options. */
|
|---|
| 85 | int rc = async_data_write_accept((void **) &opts, true, 0,
|
|---|
| 86 | MAX_MNTOPTS_LEN, 0, NULL);
|
|---|
| 87 | if (rc != EOK) {
|
|---|
| 88 | async_answer_0(rid, rc);
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* Now, we expect the client to send us data with the name of the file
|
|---|
| 93 | * system.
|
|---|
| 94 | */
|
|---|
| 95 | rc = async_data_write_accept((void **) &fs_name, true, 0,
|
|---|
| 96 | FS_NAME_MAXLEN, 0, NULL);
|
|---|
| 97 | if (rc != EOK) {
|
|---|
| 98 | free(opts);
|
|---|
| 99 | async_answer_0(rid, rc);
|
|---|
| 100 | return;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | int outfd = 0;
|
|---|
| 104 | rc = vfs_op_mount(mpfd, service_id, flags, instance, opts, fs_name,
|
|---|
| 105 | &outfd);
|
|---|
| 106 | async_answer_1(rid, rc, outfd);
|
|---|
| 107 |
|
|---|
| 108 | free(opts);
|
|---|
| 109 | free(fs_name);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | static void vfs_in_mtab_get(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 113 | {
|
|---|
| 114 | int rc = vfs_op_mtab_get();
|
|---|
| 115 | async_answer_0(rid, rc);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | static void vfs_in_open2(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 119 | {
|
|---|
| 120 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 121 | int flags = IPC_GET_ARG2(*request);
|
|---|
| 122 |
|
|---|
| 123 | int rc = vfs_op_open2(fd, flags);
|
|---|
| 124 | async_answer_0(rid, rc);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | static void vfs_in_read(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 128 | {
|
|---|
| 129 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 130 |
|
|---|
| 131 | size_t bytes = 0;
|
|---|
| 132 | int rc = vfs_op_read(fd, &bytes);
|
|---|
| 133 | async_answer_1(rid, rc, bytes);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static void vfs_in_rename(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 137 | {
|
|---|
| 138 | /* The common base directory. */
|
|---|
| 139 | int basefd;
|
|---|
| 140 | char *old = NULL;
|
|---|
| 141 | char *new = NULL;
|
|---|
| 142 | int rc;
|
|---|
| 143 |
|
|---|
| 144 | basefd = IPC_GET_ARG1(*request);
|
|---|
| 145 |
|
|---|
| 146 | /* Retrieve the old path. */
|
|---|
| 147 | rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
|---|
| 148 | if (rc != EOK) {
|
|---|
| 149 | goto out;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /* Retrieve the new path. */
|
|---|
| 153 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
|---|
| 154 | if (rc != EOK) {
|
|---|
| 155 | goto out;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | size_t olen;
|
|---|
| 159 | size_t nlen;
|
|---|
| 160 | char *oldc = canonify(old, &olen);
|
|---|
| 161 | char *newc = canonify(new, &nlen);
|
|---|
| 162 |
|
|---|
| 163 | if ((!oldc) || (!newc)) {
|
|---|
| 164 | rc = EINVAL;
|
|---|
| 165 | goto out;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | assert(oldc[olen] == '\0');
|
|---|
| 169 | assert(newc[nlen] == '\0');
|
|---|
| 170 |
|
|---|
| 171 | rc = vfs_op_rename(basefd, oldc, newc);
|
|---|
| 172 |
|
|---|
| 173 | out:
|
|---|
| 174 | async_answer_0(rid, rc);
|
|---|
| 175 |
|
|---|
| 176 | if (old) {
|
|---|
| 177 | free(old);
|
|---|
| 178 | }
|
|---|
| 179 | if (new) {
|
|---|
| 180 | free(new);
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | static void vfs_in_seek(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 185 | {
|
|---|
| 186 | int fd = (int) IPC_GET_ARG1(*request);
|
|---|
| 187 | int64_t off = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(*request), IPC_GET_ARG3(*request));
|
|---|
| 188 | int whence = (int) IPC_GET_ARG4(*request);
|
|---|
| 189 |
|
|---|
| 190 | int64_t new_offset = 0;
|
|---|
| 191 | int rc = vfs_op_seek(fd, off, whence, &new_offset);
|
|---|
| 192 | async_answer_2(rid, rc, LOWER32(new_offset), UPPER32(new_offset));
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | static void vfs_in_statfs(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 196 | {
|
|---|
| 197 | int fd = (int) IPC_GET_ARG1(*request);
|
|---|
| 198 |
|
|---|
| 199 | int rc = vfs_op_statfs(fd);
|
|---|
| 200 | async_answer_0(rid, rc);
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | static void vfs_in_sync(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 204 | {
|
|---|
| 205 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 206 | int rc = vfs_op_sync(fd);
|
|---|
| 207 | async_answer_0(rid, rc);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | static void vfs_in_truncate(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 211 | {
|
|---|
| 212 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 213 | int64_t size = MERGE_LOUP32(IPC_GET_ARG2(*request), IPC_GET_ARG3(*request));
|
|---|
| 214 | int rc = vfs_op_truncate(fd, size);
|
|---|
| 215 | async_answer_0(rid, rc);
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | static void vfs_in_unlink2(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 219 | {
|
|---|
| 220 | int parentfd = IPC_GET_ARG1(*request);
|
|---|
| 221 | int expectfd = IPC_GET_ARG2(*request);
|
|---|
| 222 | int wflag = IPC_GET_ARG3(*request);
|
|---|
| 223 |
|
|---|
| 224 | char *path;
|
|---|
| 225 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
|---|
| 226 | if (rc == EOK) {
|
|---|
| 227 | rc = vfs_op_unlink2(parentfd, expectfd, wflag, path);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | async_answer_0(rid, rc);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | static void vfs_in_unmount(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 234 | {
|
|---|
| 235 | int mpfd = IPC_GET_ARG1(*request);
|
|---|
| 236 | int rc = vfs_op_unmount(mpfd);
|
|---|
| 237 | async_answer_0(rid, rc);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | static void vfs_in_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 241 | {
|
|---|
| 242 | bool high_fd = IPC_GET_ARG1(*request);
|
|---|
| 243 | int fd = vfs_op_wait_handle(high_fd);
|
|---|
| 244 | async_answer_1(rid, EOK, fd);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | static void vfs_in_walk(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 248 | {
|
|---|
| 249 | /*
|
|---|
| 250 | * Parent is our relative root for file lookup.
|
|---|
| 251 | * For defined flags, see <ipc/vfs.h>.
|
|---|
| 252 | */
|
|---|
| 253 | int parentfd = IPC_GET_ARG1(*request);
|
|---|
| 254 | int flags = IPC_GET_ARG2(*request);
|
|---|
| 255 |
|
|---|
| 256 | int fd = 0;
|
|---|
| 257 | char *path;
|
|---|
| 258 | int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
|
|---|
| 259 | if (rc == EOK) {
|
|---|
| 260 | rc = vfs_op_walk(parentfd, flags, path, &fd);
|
|---|
| 261 | free(path);
|
|---|
| 262 | }
|
|---|
| 263 | async_answer_1(rid, rc, fd);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | static void vfs_in_write(ipc_callid_t rid, ipc_call_t *request)
|
|---|
| 267 | {
|
|---|
| 268 | int fd = IPC_GET_ARG1(*request);
|
|---|
| 269 |
|
|---|
| 270 | size_t bytes = 0;
|
|---|
| 271 | int rc = vfs_op_write(fd, &bytes);
|
|---|
| 272 | async_answer_1(rid, rc, bytes);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 276 | {
|
|---|
| 277 | bool cont = true;
|
|---|
| 278 |
|
|---|
| 279 | /*
|
|---|
| 280 | * The connection was opened via the IPC_CONNECT_ME_TO call.
|
|---|
| 281 | * This call needs to be answered.
|
|---|
| 282 | */
|
|---|
| 283 | async_answer_0(iid, EOK);
|
|---|
| 284 |
|
|---|
| 285 | while (cont) {
|
|---|
| 286 | ipc_call_t call;
|
|---|
| 287 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 288 |
|
|---|
| 289 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 290 | break;
|
|---|
| 291 |
|
|---|
| 292 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 293 | case VFS_IN_CLONE:
|
|---|
| 294 | vfs_in_clone(callid, &call);
|
|---|
| 295 | break;
|
|---|
| 296 | case VFS_IN_CLOSE:
|
|---|
| 297 | vfs_in_close(callid, &call);
|
|---|
| 298 | break;
|
|---|
| 299 | case VFS_IN_DUP:
|
|---|
| 300 | vfs_in_dup(callid, &call);
|
|---|
| 301 | break;
|
|---|
| 302 | case VFS_IN_FSTAT:
|
|---|
| 303 | vfs_in_fstat(callid, &call);
|
|---|
| 304 | break;
|
|---|
| 305 | case VFS_IN_MOUNT:
|
|---|
| 306 | vfs_in_mount(callid, &call);
|
|---|
| 307 | break;
|
|---|
| 308 | case VFS_IN_MTAB_GET:
|
|---|
| 309 | vfs_in_mtab_get(callid, &call);
|
|---|
| 310 | break;
|
|---|
| 311 | case VFS_IN_OPEN2:
|
|---|
| 312 | vfs_in_open2(callid, &call);
|
|---|
| 313 | break;
|
|---|
| 314 | case VFS_IN_READ:
|
|---|
| 315 | vfs_in_read(callid, &call);
|
|---|
| 316 | break;
|
|---|
| 317 | case VFS_IN_REGISTER:
|
|---|
| 318 | vfs_register(callid, &call);
|
|---|
| 319 | cont = false;
|
|---|
| 320 | break;
|
|---|
| 321 | case VFS_IN_RENAME:
|
|---|
| 322 | vfs_in_rename(callid, &call);
|
|---|
| 323 | break;
|
|---|
| 324 | case VFS_IN_SEEK:
|
|---|
| 325 | vfs_in_seek(callid, &call);
|
|---|
| 326 | break;
|
|---|
| 327 | case VFS_IN_STATFS:
|
|---|
| 328 | vfs_in_statfs(callid, &call);
|
|---|
| 329 | break;
|
|---|
| 330 | case VFS_IN_SYNC:
|
|---|
| 331 | vfs_in_sync(callid, &call);
|
|---|
| 332 | break;
|
|---|
| 333 | case VFS_IN_TRUNCATE:
|
|---|
| 334 | vfs_in_truncate(callid, &call);
|
|---|
| 335 | break;
|
|---|
| 336 | case VFS_IN_UNLINK2:
|
|---|
| 337 | vfs_in_unlink2(callid, &call);
|
|---|
| 338 | break;
|
|---|
| 339 | case VFS_IN_UNMOUNT:
|
|---|
| 340 | vfs_in_unmount(callid, &call);
|
|---|
| 341 | break;
|
|---|
| 342 | case VFS_IN_WAIT_HANDLE:
|
|---|
| 343 | vfs_in_wait_handle(callid, &call);
|
|---|
| 344 | break;
|
|---|
| 345 | case VFS_IN_WALK:
|
|---|
| 346 | vfs_in_walk(callid, &call);
|
|---|
| 347 | break;
|
|---|
| 348 | case VFS_IN_WRITE:
|
|---|
| 349 | vfs_in_write(callid, &call);
|
|---|
| 350 | break;
|
|---|
| 351 | default:
|
|---|
| 352 | async_answer_0(callid, ENOTSUP);
|
|---|
| 353 | break;
|
|---|
| 354 | }
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /*
|
|---|
| 358 | * Open files for this client will be cleaned up when its last
|
|---|
| 359 | * connection fibril terminates.
|
|---|
| 360 | */
|
|---|
| 361 | }
|
|---|