[b3cd9eb] | 1 | /*
|
---|
| 2 | * Copyright (c) 2007 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 | /** @addtogroup fs
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
[ee1b8ca] | 34 | * @file vfs_rdwr.c
|
---|
[b3cd9eb] | 35 | * @brief
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include "vfs.h"
|
---|
| 39 | #include <ipc/ipc.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 |
|
---|
[ee1b8ca] | 43 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
|
---|
[b3cd9eb] | 44 | {
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | * The following code strongly depends on the fact that the files data
|
---|
| 48 | * structure can be only accessed by a single fibril and all file
|
---|
| 49 | * operations are serialized (i.e. the reads and writes cannot
|
---|
| 50 | * interleave and a file cannot be closed while it is being read).
|
---|
| 51 | *
|
---|
[a4eb8a60] | 52 | * Additional synchronization needs to be added once the table of
|
---|
[b3cd9eb] | 53 | * open files supports parallel access!
|
---|
| 54 | */
|
---|
| 55 |
|
---|
| 56 | int fd = IPC_GET_ARG1(*request);
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | * Lookup the file structure corresponding to the file descriptor.
|
---|
| 60 | */
|
---|
| 61 | vfs_file_t *file = vfs_file_get(fd);
|
---|
| 62 | if (!file) {
|
---|
| 63 | ipc_answer_0(rid, ENOENT);
|
---|
| 64 | return;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /*
|
---|
[ee1b8ca] | 68 | * Now we need to receive a call with client's
|
---|
| 69 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
|
---|
[b3cd9eb] | 70 | */
|
---|
| 71 | ipc_callid_t callid;
|
---|
[ee1b8ca] | 72 | int res;
|
---|
| 73 | if (read)
|
---|
| 74 | res = ipc_data_read_receive(&callid, NULL);
|
---|
| 75 | else
|
---|
| 76 | res = ipc_data_write_receive(&callid, NULL, NULL);
|
---|
| 77 | if (!res) {
|
---|
[b3cd9eb] | 78 | ipc_answer_0(callid, EINVAL);
|
---|
| 79 | ipc_answer_0(rid, EINVAL);
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | int fs_phone = vfs_grab_phone(file->node->fs_handle);
|
---|
| 84 |
|
---|
| 85 | /*
|
---|
[ee1b8ca] | 86 | * Make a VFS_READ/VFS_WRITE request at the destination FS server.
|
---|
[b3cd9eb] | 87 | */
|
---|
| 88 | aid_t msg;
|
---|
[a4eb8a60] | 89 | ipc_call_t answer;
|
---|
[ee1b8ca] | 90 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
|
---|
| 91 | file->node->dev_handle, file->node->index, file->pos, &answer);
|
---|
[b3cd9eb] | 92 |
|
---|
| 93 | /*
|
---|
[ee1b8ca] | 94 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
|
---|
| 95 | * destination FS server. The call will be routed as if sent by
|
---|
| 96 | * ourselves. Note that call arguments are immutable in this case so we
|
---|
| 97 | * don't have to bother.
|
---|
[b3cd9eb] | 98 | */
|
---|
[a92da0a] | 99 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
|
---|
[b3cd9eb] | 100 |
|
---|
| 101 | vfs_release_phone(fs_phone);
|
---|
| 102 |
|
---|
| 103 | /*
|
---|
| 104 | * Wait for reply from the FS server.
|
---|
| 105 | */
|
---|
| 106 | ipcarg_t rc;
|
---|
| 107 | async_wait_for(msg, &rc);
|
---|
[a4eb8a60] | 108 | size_t bytes = IPC_GET_ARG1(answer);
|
---|
[b3cd9eb] | 109 |
|
---|
[5c786d1] | 110 | /*
|
---|
| 111 | * Update the position pointer.
|
---|
| 112 | */
|
---|
| 113 | file->pos += bytes;
|
---|
| 114 |
|
---|
[b3cd9eb] | 115 | /*
|
---|
| 116 | * FS server's reply is the final result of the whole operation we
|
---|
| 117 | * return to the client.
|
---|
| 118 | */
|
---|
[a4eb8a60] | 119 | ipc_answer_1(rid, rc, bytes);
|
---|
[b3cd9eb] | 120 | }
|
---|
| 121 |
|
---|
[ee1b8ca] | 122 |
|
---|
| 123 | void vfs_read(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 124 | {
|
---|
| 125 | vfs_rdwr(rid, request, true);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void vfs_write(ipc_callid_t rid, ipc_call_t *request)
|
---|
| 129 | {
|
---|
| 130 | vfs_rdwr(rid, request, false);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[b3cd9eb] | 133 | /**
|
---|
| 134 | * @}
|
---|
| 135 | */
|
---|