source: mainline/uspace/srv/vfs/vfs_rdwr.c@ 67f63c4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 67f63c4 was 67f63c4, checked in by Jakub Jermar <jakub@…>, 18 years ago

Make uspace rwlock API naming conventions consistent with kernel rwlock API.

  • Property mode set to 100644
File size: 5.5 KB
Line 
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/**
34 * @file vfs_rdwr.c
35 * @brief
36 */
37
38#include "vfs.h"
39#include <ipc/ipc.h>
40#include <async.h>
41#include <errno.h>
42#include <rwlock.h>
43#include <unistd.h>
44
45static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
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 *
54 * Additional synchronization needs to be added once the table of
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 /*
70 * Now we need to receive a call with client's
71 * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
72 */
73 ipc_callid_t callid;
74 int res;
75 if (read)
76 res = ipc_data_read_receive(&callid, NULL);
77 else
78 res = ipc_data_write_receive(&callid, NULL);
79 if (!res) {
80 ipc_answer_0(callid, EINVAL);
81 ipc_answer_0(rid, EINVAL);
82 return;
83 }
84
85 /*
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 /*
92 * Lock the file's node so that no other client can read/write to it at
93 * the same time.
94 */
95 if (read)
96 rwlock_read_lock(&file->node->contents_rwlock);
97 else
98 rwlock_write_lock(&file->node->contents_rwlock);
99
100 int fs_phone = vfs_grab_phone(file->node->fs_handle);
101
102 /*
103 * Make a VFS_READ/VFS_WRITE request at the destination FS server.
104 */
105 aid_t msg;
106 ipc_call_t answer;
107 msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
108 file->node->dev_handle, file->node->index, file->pos, &answer);
109
110 /*
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.
115 */
116 ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
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);
125 size_t bytes = IPC_GET_ARG1(answer);
126
127 /*
128 * Unlock the VFS node.
129 */
130 if (read)
131 rwlock_read_unlock(&file->node->contents_rwlock);
132 else {
133 /* Update the cached version of node's size. */
134 file->node->size = IPC_GET_ARG2(answer);
135 rwlock_write_unlock(&file->node->contents_rwlock);
136 }
137
138 /*
139 * Update the position pointer and unlock the open file.
140 */
141 file->pos += bytes;
142 futex_up(&file->lock);
143
144 /*
145 * FS server's reply is the final result of the whole operation we
146 * return to the client.
147 */
148 ipc_answer_1(rid, rc, bytes);
149}
150
151
152void vfs_read(ipc_callid_t rid, ipc_call_t *request)
153{
154 vfs_rdwr(rid, request, true);
155}
156
157void vfs_write(ipc_callid_t rid, ipc_call_t *request)
158{
159 vfs_rdwr(rid, request, false);
160}
161
162void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
163{
164 int fd = (int) IPC_GET_ARG1(*request);
165 off_t off = (off_t) IPC_GET_ARG2(*request);
166 int whence = (int) IPC_GET_ARG3(*request);
167
168
169 /*
170 * Lookup the file structure corresponding to the file descriptor.
171 */
172 vfs_file_t *file = vfs_file_get(fd);
173 if (!file) {
174 ipc_answer_0(rid, ENOENT);
175 return;
176 }
177
178 off_t newpos;
179 futex_down(&file->lock);
180 if (whence == SEEK_SET) {
181 file->pos = off;
182 futex_up(&file->lock);
183 ipc_answer_1(rid, EOK, off);
184 return;
185 }
186 if (whence == SEEK_CUR) {
187 if (file->pos + off < file->pos) {
188 futex_up(&file->lock);
189 ipc_answer_0(rid, EOVERFLOW);
190 return;
191 }
192 file->pos += off;
193 newpos = file->pos;
194 futex_up(&file->lock);
195 ipc_answer_1(rid, EOK, newpos);
196 return;
197 }
198 if (whence == SEEK_END) {
199 rwlock_read_lock(&file->node->contents_rwlock);
200 size_t size = file->node->size;
201 rwlock_read_unlock(&file->node->contents_rwlock);
202 if (size + off < size) {
203 futex_up(&file->lock);
204 ipc_answer_0(rid, EOVERFLOW);
205 return;
206 }
207 newpos = size + off;
208 futex_up(&file->lock);
209 ipc_answer_1(rid, EOK, newpos);
210 return;
211 }
212 futex_up(&file->lock);
213 ipc_answer_0(rid, EINVAL);
214}
215
216/**
217 * @}
218 */
Note: See TracBrowser for help on using the repository browser.