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