[0d35511] | 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);
|
---|
[55a7fee] | 63 | int rc = vfs_op_fstat(fd);
|
---|
[0d35511] | 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_open2(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 113 | {
|
---|
| 114 | int fd = IPC_GET_ARG1(*request);
|
---|
| 115 | int flags = IPC_GET_ARG2(*request);
|
---|
| 116 |
|
---|
| 117 | int rc = vfs_op_open2(fd, flags);
|
---|
| 118 | async_answer_0(rid, rc);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | static void vfs_in_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 122 | {
|
---|
| 123 | int fd = IPC_GET_ARG1(*request);
|
---|
[58898d1d] | 124 | aoff64_t pos = MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
| 125 | IPC_GET_ARG3(*request));
|
---|
[0d35511] | 126 |
|
---|
| 127 | size_t bytes = 0;
|
---|
[58898d1d] | 128 | int rc = vfs_op_read(fd, pos, &bytes);
|
---|
[0d35511] | 129 | async_answer_1(rid, rc, bytes);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | static void vfs_in_rename(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 133 | {
|
---|
| 134 | /* The common base directory. */
|
---|
| 135 | int basefd;
|
---|
| 136 | char *old = NULL;
|
---|
| 137 | char *new = NULL;
|
---|
| 138 | int rc;
|
---|
| 139 |
|
---|
| 140 | basefd = IPC_GET_ARG1(*request);
|
---|
| 141 |
|
---|
| 142 | /* Retrieve the old path. */
|
---|
| 143 | rc = async_data_write_accept((void **) &old, true, 0, 0, 0, NULL);
|
---|
[ebf1011] | 144 | if (rc != EOK)
|
---|
[0d35511] | 145 | goto out;
|
---|
| 146 |
|
---|
| 147 | /* Retrieve the new path. */
|
---|
| 148 | rc = async_data_write_accept((void **) &new, true, 0, 0, 0, NULL);
|
---|
[ebf1011] | 149 | if (rc != EOK)
|
---|
[0d35511] | 150 | goto out;
|
---|
| 151 |
|
---|
| 152 | size_t olen;
|
---|
| 153 | size_t nlen;
|
---|
| 154 | char *oldc = canonify(old, &olen);
|
---|
| 155 | char *newc = canonify(new, &nlen);
|
---|
| 156 |
|
---|
| 157 | if ((!oldc) || (!newc)) {
|
---|
| 158 | rc = EINVAL;
|
---|
| 159 | goto out;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | assert(oldc[olen] == '\0');
|
---|
| 163 | assert(newc[nlen] == '\0');
|
---|
| 164 |
|
---|
| 165 | rc = vfs_op_rename(basefd, oldc, newc);
|
---|
| 166 |
|
---|
| 167 | out:
|
---|
| 168 | async_answer_0(rid, rc);
|
---|
| 169 |
|
---|
[ebf1011] | 170 | if (old)
|
---|
[0d35511] | 171 | free(old);
|
---|
[ebf1011] | 172 | if (new)
|
---|
[0d35511] | 173 | free(new);
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | static void vfs_in_statfs(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 177 | {
|
---|
| 178 | int fd = (int) IPC_GET_ARG1(*request);
|
---|
| 179 |
|
---|
| 180 | int rc = vfs_op_statfs(fd);
|
---|
| 181 | async_answer_0(rid, rc);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | static void vfs_in_sync(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 185 | {
|
---|
| 186 | int fd = IPC_GET_ARG1(*request);
|
---|
| 187 | int rc = vfs_op_sync(fd);
|
---|
| 188 | async_answer_0(rid, rc);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | static void vfs_in_truncate(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 192 | {
|
---|
| 193 | int fd = IPC_GET_ARG1(*request);
|
---|
| 194 | int64_t size = MERGE_LOUP32(IPC_GET_ARG2(*request), IPC_GET_ARG3(*request));
|
---|
| 195 | int rc = vfs_op_truncate(fd, size);
|
---|
| 196 | async_answer_0(rid, rc);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | static void vfs_in_unlink2(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 200 | {
|
---|
| 201 | int parentfd = IPC_GET_ARG1(*request);
|
---|
| 202 | int expectfd = IPC_GET_ARG2(*request);
|
---|
| 203 | int wflag = IPC_GET_ARG3(*request);
|
---|
| 204 |
|
---|
| 205 | char *path;
|
---|
| 206 | int rc = async_data_write_accept((void **) &path, true, 0, 0, 0, NULL);
|
---|
[ebf1011] | 207 | if (rc == EOK)
|
---|
[0d35511] | 208 | rc = vfs_op_unlink2(parentfd, expectfd, wflag, path);
|
---|
| 209 |
|
---|
| 210 | async_answer_0(rid, rc);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | static void vfs_in_unmount(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 214 | {
|
---|
| 215 | int mpfd = IPC_GET_ARG1(*request);
|
---|
| 216 | int rc = vfs_op_unmount(mpfd);
|
---|
| 217 | async_answer_0(rid, rc);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | static void vfs_in_wait_handle(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 221 | {
|
---|
| 222 | bool high_fd = IPC_GET_ARG1(*request);
|
---|
| 223 | int fd = vfs_op_wait_handle(high_fd);
|
---|
| 224 | async_answer_1(rid, EOK, fd);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | static void vfs_in_walk(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 228 | {
|
---|
| 229 | /*
|
---|
| 230 | * Parent is our relative root for file lookup.
|
---|
| 231 | * For defined flags, see <ipc/vfs.h>.
|
---|
| 232 | */
|
---|
| 233 | int parentfd = IPC_GET_ARG1(*request);
|
---|
| 234 | int flags = IPC_GET_ARG2(*request);
|
---|
| 235 |
|
---|
| 236 | int fd = 0;
|
---|
| 237 | char *path;
|
---|
| 238 | int rc = async_data_write_accept((void **)&path, true, 0, 0, 0, NULL);
|
---|
| 239 | if (rc == EOK) {
|
---|
| 240 | rc = vfs_op_walk(parentfd, flags, path, &fd);
|
---|
| 241 | free(path);
|
---|
| 242 | }
|
---|
| 243 | async_answer_1(rid, rc, fd);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | static void vfs_in_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 247 | {
|
---|
| 248 | int fd = IPC_GET_ARG1(*request);
|
---|
[58898d1d] | 249 | aoff64_t pos = MERGE_LOUP32(IPC_GET_ARG2(*request),
|
---|
| 250 | IPC_GET_ARG3(*request));
|
---|
[0d35511] | 251 |
|
---|
| 252 | size_t bytes = 0;
|
---|
[58898d1d] | 253 | int rc = vfs_op_write(fd, pos, &bytes);
|
---|
[0d35511] | 254 | async_answer_1(rid, rc, bytes);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
| 258 | {
|
---|
| 259 | bool cont = true;
|
---|
| 260 |
|
---|
| 261 | /*
|
---|
| 262 | * The connection was opened via the IPC_CONNECT_ME_TO call.
|
---|
| 263 | * This call needs to be answered.
|
---|
| 264 | */
|
---|
| 265 | async_answer_0(iid, EOK);
|
---|
| 266 |
|
---|
| 267 | while (cont) {
|
---|
| 268 | ipc_call_t call;
|
---|
| 269 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 270 |
|
---|
| 271 | if (!IPC_GET_IMETHOD(call))
|
---|
| 272 | break;
|
---|
| 273 |
|
---|
| 274 | switch (IPC_GET_IMETHOD(call)) {
|
---|
| 275 | case VFS_IN_CLONE:
|
---|
| 276 | vfs_in_clone(callid, &call);
|
---|
| 277 | break;
|
---|
| 278 | case VFS_IN_CLOSE:
|
---|
| 279 | vfs_in_close(callid, &call);
|
---|
| 280 | break;
|
---|
| 281 | case VFS_IN_DUP:
|
---|
| 282 | vfs_in_dup(callid, &call);
|
---|
| 283 | break;
|
---|
| 284 | case VFS_IN_FSTAT:
|
---|
| 285 | vfs_in_fstat(callid, &call);
|
---|
| 286 | break;
|
---|
| 287 | case VFS_IN_MOUNT:
|
---|
| 288 | vfs_in_mount(callid, &call);
|
---|
| 289 | break;
|
---|
| 290 | case VFS_IN_OPEN2:
|
---|
| 291 | vfs_in_open2(callid, &call);
|
---|
| 292 | break;
|
---|
| 293 | case VFS_IN_READ:
|
---|
| 294 | vfs_in_read(callid, &call);
|
---|
| 295 | break;
|
---|
| 296 | case VFS_IN_REGISTER:
|
---|
| 297 | vfs_register(callid, &call);
|
---|
| 298 | cont = false;
|
---|
| 299 | break;
|
---|
| 300 | case VFS_IN_RENAME:
|
---|
| 301 | vfs_in_rename(callid, &call);
|
---|
| 302 | break;
|
---|
| 303 | case VFS_IN_STATFS:
|
---|
| 304 | vfs_in_statfs(callid, &call);
|
---|
| 305 | break;
|
---|
| 306 | case VFS_IN_SYNC:
|
---|
| 307 | vfs_in_sync(callid, &call);
|
---|
| 308 | break;
|
---|
| 309 | case VFS_IN_TRUNCATE:
|
---|
| 310 | vfs_in_truncate(callid, &call);
|
---|
| 311 | break;
|
---|
| 312 | case VFS_IN_UNLINK2:
|
---|
| 313 | vfs_in_unlink2(callid, &call);
|
---|
| 314 | break;
|
---|
| 315 | case VFS_IN_UNMOUNT:
|
---|
| 316 | vfs_in_unmount(callid, &call);
|
---|
| 317 | break;
|
---|
| 318 | case VFS_IN_WAIT_HANDLE:
|
---|
| 319 | vfs_in_wait_handle(callid, &call);
|
---|
| 320 | break;
|
---|
| 321 | case VFS_IN_WALK:
|
---|
| 322 | vfs_in_walk(callid, &call);
|
---|
| 323 | break;
|
---|
| 324 | case VFS_IN_WRITE:
|
---|
| 325 | vfs_in_write(callid, &call);
|
---|
| 326 | break;
|
---|
| 327 | default:
|
---|
| 328 | async_answer_0(callid, ENOTSUP);
|
---|
| 329 | break;
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | /*
|
---|
| 334 | * Open files for this client will be cleaned up when its last
|
---|
| 335 | * connection fibril terminates.
|
---|
| 336 | */
|
---|
| 337 | }
|
---|