Subversion Repositories HelenOS

Rev

Rev 2684 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2684 Rev 2687
1
/*
1
/*
2
 * Copyright (c) 2007 Jakub Jermar
2
 * Copyright (c) 2007 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    vfs_rdwr.c
34
 * @file    vfs_rdwr.c
35
 * @brief
35
 * @brief
36
 */
36
 */
37
 
37
 
38
#include "vfs.h"
38
#include "vfs.h"
39
#include <ipc/ipc.h>
39
#include <ipc/ipc.h>
40
#include <async.h>
40
#include <async.h>
41
#include <errno.h>
41
#include <errno.h>
42
#include <rwlock.h>
42
#include <rwlock.h>
43
#include <unistd.h>
43
#include <unistd.h>
44
 
44
 
45
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
45
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
46
{
46
{
47
 
47
 
48
    /*
48
    /*
49
     * The following code strongly depends on the fact that the files data
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
50
     * structure can be only accessed by a single fibril and all file
51
     * operations are serialized (i.e. the reads and writes cannot
51
     * operations are serialized (i.e. the reads and writes cannot
52
     * interleave and a file cannot be closed while it is being read).
52
     * interleave and a file cannot be closed while it is being read).
53
     *
53
     *
54
     * Additional synchronization needs to be added once the table of
54
     * Additional synchronization needs to be added once the table of
55
     * open files supports parallel access!
55
     * open files supports parallel access!
56
     */
56
     */
57
 
57
 
58
    int fd = IPC_GET_ARG1(*request);
58
    int fd = IPC_GET_ARG1(*request);
59
 
59
 
60
    /*
60
    /*
61
     * Lookup the file structure corresponding to the file descriptor.
61
     * Lookup the file structure corresponding to the file descriptor.
62
     */
62
     */
63
    vfs_file_t *file = vfs_file_get(fd);
63
    vfs_file_t *file = vfs_file_get(fd);
64
    if (!file) {
64
    if (!file) {
65
        ipc_answer_0(rid, ENOENT);
65
        ipc_answer_0(rid, ENOENT);
66
        return;
66
        return;
67
    }
67
    }
68
 
68
 
69
    /*
69
    /*
70
     * Now we need to receive a call with client's
70
     * Now we need to receive a call with client's
71
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
71
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
72
     */
72
     */
73
    ipc_callid_t callid;
73
    ipc_callid_t callid;
74
    int res;
74
    int res;
75
    if (read)
75
    if (read)
76
        res = ipc_data_read_receive(&callid, NULL);
76
        res = ipc_data_read_receive(&callid, NULL);
77
    else
77
    else
78
        res = ipc_data_write_receive(&callid, NULL);
78
        res = ipc_data_write_receive(&callid, NULL);
79
    if (!res) {
79
    if (!res) {
80
        ipc_answer_0(callid, EINVAL);
80
        ipc_answer_0(callid, EINVAL);
81
        ipc_answer_0(rid, EINVAL);
81
        ipc_answer_0(rid, EINVAL);
82
        return;
82
        return;
83
    }
83
    }
84
 
84
 
85
    /*
85
    /*
86
     * Lock the open file structure so that no other thread can manipulate
86
     * Lock the open file structure so that no other thread can manipulate
87
     * the same open file at a time.
87
     * the same open file at a time.
88
     */
88
     */
89
    futex_down(&file->lock);
89
    futex_down(&file->lock);
90
 
90
 
91
    /*
91
    /*
92
     * Lock the file's node so that no other client can read/write to it at
92
     * Lock the file's node so that no other client can read/write to it at
93
     * the same time.
93
     * the same time.
94
     */
94
     */
95
    if (read)
95
    if (read)
96
        rwlock_reader_lock(&file->node->contents_rwlock);
96
        rwlock_reader_lock(&file->node->contents_rwlock);
97
    else
97
    else
98
        rwlock_writer_lock(&file->node->contents_rwlock);
98
        rwlock_writer_lock(&file->node->contents_rwlock);
99
 
99
 
100
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
100
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
101
   
101
   
102
    /*
102
    /*
103
     * Make a VFS_READ/VFS_WRITE request at the destination FS server.
103
     * Make a VFS_READ/VFS_WRITE request at the destination FS server.
104
     */
104
     */
105
    aid_t msg;
105
    aid_t msg;
106
    ipc_call_t answer;
106
    ipc_call_t answer;
107
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
107
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
108
        file->node->dev_handle, file->node->index, file->pos, &answer);
108
        file->node->dev_handle, file->node->index, file->pos, &answer);
109
   
109
   
110
    /*
110
    /*
111
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
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
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
113
     * ourselves. Note that call arguments are immutable in this case so we
114
     * don't have to bother.
114
     * don't have to bother.
115
     */
115
     */
116
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
116
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
117
 
117
 
118
    vfs_release_phone(fs_phone);
118
    vfs_release_phone(fs_phone);
119
 
119
 
120
    /*
120
    /*
121
     * Wait for reply from the FS server.
121
     * Wait for reply from the FS server.
122
     */
122
     */
123
    ipcarg_t rc;
123
    ipcarg_t rc;
124
    async_wait_for(msg, &rc);
124
    async_wait_for(msg, &rc);
125
    size_t bytes = IPC_GET_ARG1(answer);
125
    size_t bytes = IPC_GET_ARG1(answer);
126
 
126
 
127
    /*
127
    /*
128
     * Unlock the VFS node.
128
     * Unlock the VFS node.
129
     */
129
     */
130
    if (read)
130
    if (read)
131
        rwlock_reader_unlock(&file->node->contents_rwlock);
131
        rwlock_reader_unlock(&file->node->contents_rwlock);
132
    else
132
    else {
-
 
133
        /* Update the cached version of node's size. */
-
 
134
        file->node->size = IPC_GET_ARG2(answer);
133
        rwlock_writer_unlock(&file->node->contents_rwlock);
135
        rwlock_writer_unlock(&file->node->contents_rwlock);
-
 
136
    }
134
 
137
 
135
    /*
138
    /*
136
     * Update the position pointer and unlock the open file.
139
     * Update the position pointer and unlock the open file.
137
     */
140
     */
138
    file->pos += bytes;
141
    file->pos += bytes;
139
    futex_up(&file->lock);
142
    futex_up(&file->lock);
140
 
143
 
141
    /*
144
    /*
142
     * FS server's reply is the final result of the whole operation we
145
     * FS server's reply is the final result of the whole operation we
143
     * return to the client.
146
     * return to the client.
144
     */
147
     */
145
    ipc_answer_1(rid, rc, bytes);
148
    ipc_answer_1(rid, rc, bytes);
146
}
149
}
147
 
150
 
148
 
151
 
149
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
152
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
150
{
153
{
151
    vfs_rdwr(rid, request, true);
154
    vfs_rdwr(rid, request, true);
152
}
155
}
153
 
156
 
154
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
157
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
155
{
158
{
156
    vfs_rdwr(rid, request, false);
159
    vfs_rdwr(rid, request, false);
157
}
160
}
158
 
161
 
159
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
162
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
160
{
163
{
161
    int fd = (int) IPC_GET_ARG1(*request);
164
    int fd = (int) IPC_GET_ARG1(*request);
162
    off_t off = (off_t) IPC_GET_ARG2(*request);
165
    off_t off = (off_t) IPC_GET_ARG2(*request);
163
    int whence = (int) IPC_GET_ARG3(*request);
166
    int whence = (int) IPC_GET_ARG3(*request);
164
 
167
 
165
 
168
 
166
    /*
169
    /*
167
     * Lookup the file structure corresponding to the file descriptor.
170
     * Lookup the file structure corresponding to the file descriptor.
168
     */
171
     */
169
    vfs_file_t *file = vfs_file_get(fd);
172
    vfs_file_t *file = vfs_file_get(fd);
170
    if (!file) {
173
    if (!file) {
171
        ipc_answer_0(rid, ENOENT);
174
        ipc_answer_0(rid, ENOENT);
172
        return;
175
        return;
173
    }
176
    }
174
 
177
 
175
    off_t newpos;
178
    off_t newpos;
176
    futex_down(&file->lock);
179
    futex_down(&file->lock);
177
    if (whence == SEEK_SET) {
180
    if (whence == SEEK_SET) {
178
        file->pos = off;
181
        file->pos = off;
179
        futex_up(&file->lock);
182
        futex_up(&file->lock);
180
        ipc_answer_1(rid, EOK, off);
183
        ipc_answer_1(rid, EOK, off);
181
        return;
184
        return;
182
    }
185
    }
183
    if (whence == SEEK_CUR) {
186
    if (whence == SEEK_CUR) {
184
        if (file->pos + off < file->pos) {
187
        if (file->pos + off < file->pos) {
185
            futex_up(&file->lock);
188
            futex_up(&file->lock);
186
            ipc_answer_0(rid, EOVERFLOW);
189
            ipc_answer_0(rid, EOVERFLOW);
187
            return;
190
            return;
188
        }
191
        }
189
        file->pos += off;
192
        file->pos += off;
190
        newpos = file->pos;
193
        newpos = file->pos;
191
        futex_up(&file->lock);
194
        futex_up(&file->lock);
192
        ipc_answer_1(rid, EOK, newpos);
195
        ipc_answer_1(rid, EOK, newpos);
193
        return;
196
        return;
194
    }
197
    }
195
    if (whence == SEEK_END) {
198
    if (whence == SEEK_END) {
196
        rwlock_reader_lock(&file->node->contents_rwlock);
199
        rwlock_reader_lock(&file->node->contents_rwlock);
197
        size_t size = file->node->size;
200
        size_t size = file->node->size;
198
        rwlock_reader_unlock(&file->node->contents_rwlock);
201
        rwlock_reader_unlock(&file->node->contents_rwlock);
199
        if (size + off < size) {
202
        if (size + off < size) {
200
            futex_up(&file->lock);
203
            futex_up(&file->lock);
201
            ipc_answer_0(rid, EOVERFLOW);
204
            ipc_answer_0(rid, EOVERFLOW);
202
            return;
205
            return;
203
        }
206
        }
204
        newpos = size + off;
207
        newpos = size + off;
205
        futex_up(&file->lock);
208
        futex_up(&file->lock);
206
        ipc_answer_1(rid, EOK, newpos);
209
        ipc_answer_1(rid, EOK, newpos);
207
        return;
210
        return;
208
    }
211
    }
209
    futex_up(&file->lock);
212
    futex_up(&file->lock);
210
    ipc_answer_0(rid, EINVAL);
213
    ipc_answer_0(rid, EINVAL);
211
}
214
}
212
 
215
 
213
/**
216
/**
214
 * @}
217
 * @}
215
 */
218
 */
216
 
219