Rev 2683 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2625 | jermar | 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 | /** |
||
2666 | jermar | 34 | * @file vfs_rdwr.c |
2625 | jermar | 35 | * @brief |
36 | */ |
||
37 | |||
38 | #include "vfs.h" |
||
39 | #include <ipc/ipc.h> |
||
40 | #include <async.h> |
||
41 | #include <errno.h> |
||
2680 | jermar | 42 | #include <rwlock.h> |
2684 | jermar | 43 | #include <unistd.h> |
2625 | jermar | 44 | |
2666 | jermar | 45 | static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read) |
2625 | jermar | 46 | { |
47 | |||
48 | /* |
||
49 | * The following code strongly depends on the fact that the files data |
||
50 | * structure can be only accessed by a single fibril and all file |
||
51 | * operations are serialized (i.e. the reads and writes cannot |
||
52 | * interleave and a file cannot be closed while it is being read). |
||
53 | * |
||
2658 | jermar | 54 | * Additional synchronization needs to be added once the table of |
2625 | jermar | 55 | * open files supports parallel access! |
56 | */ |
||
57 | |||
58 | int fd = IPC_GET_ARG1(*request); |
||
59 | |||
60 | /* |
||
61 | * Lookup the file structure corresponding to the file descriptor. |
||
62 | */ |
||
63 | vfs_file_t *file = vfs_file_get(fd); |
||
64 | if (!file) { |
||
65 | ipc_answer_0(rid, ENOENT); |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | /* |
||
2666 | jermar | 70 | * Now we need to receive a call with client's |
71 | * IPC_M_DATA_READ/IPC_M_DATA_WRITE request. |
||
2625 | jermar | 72 | */ |
73 | ipc_callid_t callid; |
||
2666 | jermar | 74 | int res; |
75 | if (read) |
||
76 | res = ipc_data_read_receive(&callid, NULL); |
||
77 | else |
||
2676 | jermar | 78 | res = ipc_data_write_receive(&callid, NULL); |
2666 | jermar | 79 | if (!res) { |
2625 | jermar | 80 | ipc_answer_0(callid, EINVAL); |
81 | ipc_answer_0(rid, EINVAL); |
||
82 | return; |
||
83 | } |
||
84 | |||
2679 | jermar | 85 | /* |
2683 | jermar | 86 | * Lock the open file structure so that no other thread can manipulate |
87 | * the same open file at a time. |
||
88 | */ |
||
89 | futex_down(&file->lock); |
||
90 | |||
91 | /* |
||
2679 | jermar | 92 | * Lock the file's node so that no other client can read/write to it at |
93 | * the same time. |
||
94 | */ |
||
2680 | jermar | 95 | if (read) |
96 | rwlock_reader_lock(&file->node->contents_rwlock); |
||
97 | else |
||
98 | rwlock_writer_lock(&file->node->contents_rwlock); |
||
2679 | jermar | 99 | |
2625 | jermar | 100 | int fs_phone = vfs_grab_phone(file->node->fs_handle); |
101 | |||
102 | /* |
||
2666 | jermar | 103 | * Make a VFS_READ/VFS_WRITE request at the destination FS server. |
2625 | jermar | 104 | */ |
105 | aid_t msg; |
||
2658 | jermar | 106 | ipc_call_t answer; |
2666 | jermar | 107 | msg = async_send_3(fs_phone, IPC_GET_METHOD(*request), |
108 | file->node->dev_handle, file->node->index, file->pos, &answer); |
||
2625 | jermar | 109 | |
110 | /* |
||
2666 | jermar | 111 | * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the |
112 | * destination FS server. The call will be routed as if sent by |
||
113 | * ourselves. Note that call arguments are immutable in this case so we |
||
114 | * don't have to bother. |
||
2625 | jermar | 115 | */ |
2663 | jermar | 116 | ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME); |
2625 | jermar | 117 | |
118 | vfs_release_phone(fs_phone); |
||
119 | |||
120 | /* |
||
121 | * Wait for reply from the FS server. |
||
122 | */ |
||
123 | ipcarg_t rc; |
||
124 | async_wait_for(msg, &rc); |
||
2658 | jermar | 125 | size_t bytes = IPC_GET_ARG1(answer); |
2625 | jermar | 126 | |
127 | /* |
||
2679 | jermar | 128 | * Unlock the VFS node. |
129 | */ |
||
2680 | jermar | 130 | if (read) |
131 | rwlock_reader_unlock(&file->node->contents_rwlock); |
||
132 | else |
||
133 | rwlock_writer_unlock(&file->node->contents_rwlock); |
||
2679 | jermar | 134 | |
135 | /* |
||
2683 | jermar | 136 | * Update the position pointer and unlock the open file. |
2659 | jermar | 137 | */ |
138 | file->pos += bytes; |
||
2683 | jermar | 139 | futex_up(&file->lock); |
2659 | jermar | 140 | |
141 | /* |
||
2625 | jermar | 142 | * FS server's reply is the final result of the whole operation we |
143 | * return to the client. |
||
144 | */ |
||
2658 | jermar | 145 | ipc_answer_1(rid, rc, bytes); |
2625 | jermar | 146 | } |
147 | |||
2666 | jermar | 148 | |
149 | void vfs_read(ipc_callid_t rid, ipc_call_t *request) |
||
150 | { |
||
151 | vfs_rdwr(rid, request, true); |
||
152 | } |
||
153 | |||
154 | void vfs_write(ipc_callid_t rid, ipc_call_t *request) |
||
155 | { |
||
156 | vfs_rdwr(rid, request, false); |
||
157 | } |
||
158 | |||
2684 | jermar | 159 | void vfs_seek(ipc_callid_t rid, ipc_call_t *request) |
160 | { |
||
161 | int fd = (int) IPC_GET_ARG1(*request); |
||
162 | off_t off = (off_t) IPC_GET_ARG2(*request); |
||
163 | int whence = (int) IPC_GET_ARG3(*request); |
||
164 | |||
165 | |||
166 | /* |
||
167 | * Lookup the file structure corresponding to the file descriptor. |
||
168 | */ |
||
169 | vfs_file_t *file = vfs_file_get(fd); |
||
170 | if (!file) { |
||
171 | ipc_answer_0(rid, ENOENT); |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | off_t newpos; |
||
176 | futex_down(&file->lock); |
||
177 | if (whence == SEEK_SET) { |
||
178 | file->pos = off; |
||
179 | futex_up(&file->lock); |
||
180 | ipc_answer_1(rid, EOK, off); |
||
181 | return; |
||
182 | } |
||
183 | if (whence == SEEK_CUR) { |
||
184 | if (file->pos + off < file->pos) { |
||
185 | futex_up(&file->lock); |
||
186 | ipc_answer_0(rid, EOVERFLOW); |
||
187 | return; |
||
188 | } |
||
189 | file->pos += off; |
||
190 | newpos = file->pos; |
||
191 | futex_up(&file->lock); |
||
192 | ipc_answer_1(rid, EOK, newpos); |
||
193 | return; |
||
194 | } |
||
195 | if (whence == SEEK_END) { |
||
196 | rwlock_reader_lock(&file->node->contents_rwlock); |
||
197 | size_t size = file->node->size; |
||
198 | rwlock_reader_unlock(&file->node->contents_rwlock); |
||
199 | if (size + off < size) { |
||
200 | futex_up(&file->lock); |
||
201 | ipc_answer_0(rid, EOVERFLOW); |
||
202 | return; |
||
203 | } |
||
204 | newpos = size + off; |
||
205 | futex_up(&file->lock); |
||
206 | ipc_answer_1(rid, EOK, newpos); |
||
207 | return; |
||
208 | } |
||
209 | futex_up(&file->lock); |
||
210 | ipc_answer_0(rid, EINVAL); |
||
211 | } |
||
212 | |||
2625 | jermar | 213 | /** |
214 | * @} |
||
215 | */ |