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