Subversion Repositories HelenOS

Rev

Rev 2687 | 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)
2688 jermar 96
        rwlock_read_lock(&file->node->contents_rwlock);
2680 jermar 97
    else
2688 jermar 98
        rwlock_write_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)
2688 jermar 131
        rwlock_read_unlock(&file->node->contents_rwlock);
2687 jermar 132
    else {
133
        /* Update the cached version of node's size. */
134
        file->node->size = IPC_GET_ARG2(answer);
2688 jermar 135
        rwlock_write_unlock(&file->node->contents_rwlock);
2687 jermar 136
    }
2679 jermar 137
 
138
    /*
2683 jermar 139
     * Update the position pointer and unlock the open file.
2659 jermar 140
     */
141
    file->pos += bytes;
2683 jermar 142
    futex_up(&file->lock);
2659 jermar 143
 
144
    /*
2625 jermar 145
     * FS server's reply is the final result of the whole operation we
146
     * return to the client.
147
     */
2658 jermar 148
    ipc_answer_1(rid, rc, bytes);
2625 jermar 149
}
150
 
2666 jermar 151
 
152
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
153
{
154
    vfs_rdwr(rid, request, true);
155
}
156
 
157
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
158
{
159
    vfs_rdwr(rid, request, false);
160
}
161
 
2684 jermar 162
void 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) {
2688 jermar 199
        rwlock_read_lock(&file->node->contents_rwlock);
2684 jermar 200
        size_t size = file->node->size;
2688 jermar 201
        rwlock_read_unlock(&file->node->contents_rwlock);
2684 jermar 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
 
2625 jermar 216
/**
217
 * @}
218
 */