Subversion Repositories HelenOS

Rev

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

Rev 2694 Rev 2699
1
/*
1
/*
2
 * Copyright (c) 2008 Jakub Jermar
2
 * Copyright (c) 2008 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 libc
29
/** @addtogroup libc
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <vfs.h>
35
#include <vfs.h>
36
#include <stdlib.h>
36
#include <stdlib.h>
37
#include <unistd.h>
37
#include <unistd.h>
38
#include <dirent.h>
38
#include <dirent.h>
39
#include <fcntl.h>
39
#include <fcntl.h>
40
#include <ipc/ipc.h>
40
#include <ipc/ipc.h>
41
#include <ipc/services.h>
41
#include <ipc/services.h>
42
#include <async.h>
42
#include <async.h>
43
#include <atomic.h>
43
#include <atomic.h>
44
#include <futex.h>
44
#include <futex.h>
45
#include <errno.h>
45
#include <errno.h>
46
#include <string.h>
46
#include <string.h>
47
#include "../../srv/vfs/vfs.h"
47
#include "../../srv/vfs/vfs.h"
48
 
48
 
49
int vfs_phone = -1;
49
int vfs_phone = -1;
50
atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
50
atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
51
 
51
 
52
static int vfs_connect(void)
52
static int vfs_connect(void)
53
{
53
{
54
    if (vfs_phone < 0)
54
    if (vfs_phone < 0)
55
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
55
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
56
    return vfs_phone;
56
    return vfs_phone;
57
}
57
}
58
 
58
 
59
int mount(const char *fs_name, const char *mp, const char *dev)
59
int mount(const char *fs_name, const char *mp, const char *dev)
60
{
60
{
61
    int res;
61
    int res;
62
    ipcarg_t rc;
62
    ipcarg_t rc;
63
    aid_t req;
63
    aid_t req;
64
 
64
 
65
    int dev_handle = 0; /* TODO */
65
    int dev_handle = 0; /* TODO */
66
 
66
 
67
    futex_down(&vfs_phone_futex);
67
    futex_down(&vfs_phone_futex);
68
    async_serialize_start();
68
    async_serialize_start();
69
    if (vfs_phone < 0) {
69
    if (vfs_phone < 0) {
70
        res = vfs_connect();
70
        res = vfs_connect();
71
        if (res < 0) {
71
        if (res < 0) {
72
            async_serialize_end();
72
            async_serialize_end();
73
            futex_up(&vfs_phone_futex);
73
            futex_up(&vfs_phone_futex);
74
            return res;
74
            return res;
75
        }
75
        }
76
    }
76
    }
77
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
77
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
78
    rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
78
    rc = ipc_data_write_start(vfs_phone, (void *)fs_name, strlen(fs_name));
79
    if (rc != EOK) {
79
    if (rc != EOK) {
80
        async_wait_for(req, NULL);
80
        async_wait_for(req, NULL);
81
        async_serialize_end();
81
        async_serialize_end();
82
        futex_up(&vfs_phone_futex);
82
        futex_up(&vfs_phone_futex);
83
        return (int) rc;
83
        return (int) rc;
84
    }
84
    }
85
    rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp));
85
    rc = ipc_data_write_start(vfs_phone, (void *)mp, strlen(mp));
86
    if (rc != EOK) {
86
    if (rc != EOK) {
87
        async_wait_for(req, NULL);
87
        async_wait_for(req, NULL);
88
        async_serialize_end();
88
        async_serialize_end();
89
        futex_up(&vfs_phone_futex);
89
        futex_up(&vfs_phone_futex);
90
        return (int) rc;
90
        return (int) rc;
91
    }
91
    }
92
    async_wait_for(req, &rc);
92
    async_wait_for(req, &rc);
93
    async_serialize_end();
93
    async_serialize_end();
94
    futex_up(&vfs_phone_futex);
94
    futex_up(&vfs_phone_futex);
95
    return (int) rc;
95
    return (int) rc;
96
}
96
}
97
 
97
 
98
 
98
 
99
int open(const char *path, int oflag, ...)
99
int open(const char *path, int oflag, ...)
100
{
100
{
101
    int res;
101
    int res;
102
    ipcarg_t rc;
102
    ipcarg_t rc;
103
    ipc_call_t answer;
103
    ipc_call_t answer;
104
    aid_t req;
104
    aid_t req;
105
   
105
   
106
    futex_down(&vfs_phone_futex);
106
    futex_down(&vfs_phone_futex);
107
    async_serialize_start();
107
    async_serialize_start();
108
    if (vfs_phone < 0) {
108
    if (vfs_phone < 0) {
109
        res = vfs_connect();
109
        res = vfs_connect();
110
        if (res < 0) {
110
        if (res < 0) {
111
            async_serialize_end();
111
            async_serialize_end();
112
            futex_up(&vfs_phone_futex);
112
            futex_up(&vfs_phone_futex);
113
            return res;
113
            return res;
114
        }
114
        }
115
    }
115
    }
116
    req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer);
116
    req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer);
117
    rc = ipc_data_write_start(vfs_phone, path, strlen(path));
117
    rc = ipc_data_write_start(vfs_phone, path, strlen(path));
118
    if (rc != EOK) {
118
    if (rc != EOK) {
119
        async_wait_for(req, NULL);
119
        async_wait_for(req, NULL);
120
        async_serialize_end();
120
        async_serialize_end();
121
        futex_up(&vfs_phone_futex);
121
        futex_up(&vfs_phone_futex);
122
        return (int) rc;
122
        return (int) rc;
123
    }
123
    }
124
    async_wait_for(req, &rc);
124
    async_wait_for(req, &rc);
125
    async_serialize_end();
125
    async_serialize_end();
126
    futex_up(&vfs_phone_futex);
126
    futex_up(&vfs_phone_futex);
127
    return (int) IPC_GET_ARG1(answer);
127
    return (int) IPC_GET_ARG1(answer);
128
}
128
}
129
 
129
 
130
ssize_t read(int fildes, void *buf, size_t nbyte)
130
ssize_t read(int fildes, void *buf, size_t nbyte)
131
{
131
{
132
    int res;
132
    int res;
133
    ipcarg_t rc;
133
    ipcarg_t rc;
134
    ipc_call_t answer;
134
    ipc_call_t answer;
135
    aid_t req;
135
    aid_t req;
136
 
136
 
137
    futex_down(&vfs_phone_futex);
137
    futex_down(&vfs_phone_futex);
138
    async_serialize_start();
138
    async_serialize_start();
139
    if (vfs_phone < 0) {
139
    if (vfs_phone < 0) {
140
        res = vfs_connect();
140
        res = vfs_connect();
141
        if (res < 0) {
141
        if (res < 0) {
142
            async_serialize_end();
142
            async_serialize_end();
143
            futex_up(&vfs_phone_futex);
143
            futex_up(&vfs_phone_futex);
144
            return res;
144
            return res;
145
        }
145
        }
146
    }
146
    }
147
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
147
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
148
    if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
148
    if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
149
        async_wait_for(req, NULL);
149
        async_wait_for(req, NULL);
150
        async_serialize_end();
150
        async_serialize_end();
151
        futex_up(&vfs_phone_futex);
151
        futex_up(&vfs_phone_futex);
152
        return (ssize_t) rc;
152
        return (ssize_t) rc;
153
    }
153
    }
154
    async_wait_for(req, &rc);
154
    async_wait_for(req, &rc);
155
    async_serialize_end();
155
    async_serialize_end();
156
    futex_up(&vfs_phone_futex);
156
    futex_up(&vfs_phone_futex);
157
    return (ssize_t) IPC_GET_ARG1(answer);
157
    return (ssize_t) IPC_GET_ARG1(answer);
158
}
158
}
159
 
159
 
160
ssize_t write(int fildes, const void *buf, size_t nbyte)
160
ssize_t write(int fildes, const void *buf, size_t nbyte)
161
{
161
{
162
    int res;
162
    int res;
163
    ipcarg_t rc;
163
    ipcarg_t rc;
164
    ipc_call_t answer;
164
    ipc_call_t answer;
165
    aid_t req;
165
    aid_t req;
166
 
166
 
167
    futex_down(&vfs_phone_futex);
167
    futex_down(&vfs_phone_futex);
168
    async_serialize_start();
168
    async_serialize_start();
169
    if (vfs_phone < 0) {
169
    if (vfs_phone < 0) {
170
        res = vfs_connect();
170
        res = vfs_connect();
171
        if (res < 0) {
171
        if (res < 0) {
172
            async_serialize_end();
172
            async_serialize_end();
173
            futex_up(&vfs_phone_futex);
173
            futex_up(&vfs_phone_futex);
174
            return res;
174
            return res;
175
        }
175
        }
176
    }
176
    }
177
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
177
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
178
    if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
178
    if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
179
        async_wait_for(req, NULL);
179
        async_wait_for(req, NULL);
180
        async_serialize_end();
180
        async_serialize_end();
181
        futex_up(&vfs_phone_futex);
181
        futex_up(&vfs_phone_futex);
182
        return (ssize_t) rc;
182
        return (ssize_t) rc;
183
    }
183
    }
184
    async_wait_for(req, &rc);
184
    async_wait_for(req, &rc);
185
    async_serialize_end();
185
    async_serialize_end();
186
    futex_up(&vfs_phone_futex);
186
    futex_up(&vfs_phone_futex);
187
    return (ssize_t) IPC_GET_ARG1(answer);
187
    return (ssize_t) IPC_GET_ARG1(answer);
188
}
188
}
189
 
189
 
190
off_t lseek(int fildes, off_t offset, int whence)
190
off_t lseek(int fildes, off_t offset, int whence)
191
{
191
{
192
    int res;
192
    int res;
193
    ipcarg_t rc;
193
    ipcarg_t rc;
194
 
194
 
195
    futex_down(&vfs_phone_futex);
195
    futex_down(&vfs_phone_futex);
196
    async_serialize_start();
196
    async_serialize_start();
197
    if (vfs_phone < 0) {
197
    if (vfs_phone < 0) {
198
        res = vfs_connect();
198
        res = vfs_connect();
199
        if (res < 0) {
199
        if (res < 0) {
200
            async_serialize_end();
200
            async_serialize_end();
201
            futex_up(&vfs_phone_futex);
201
            futex_up(&vfs_phone_futex);
202
            return res;
202
            return res;
203
        }
203
        }
204
    }
204
    }
205
       
205
       
206
    off_t newoffs;
206
    off_t newoffs;
207
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
207
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
208
        &newoffs);
208
        &newoffs);
209
 
209
 
210
    async_serialize_end();
210
    async_serialize_end();
211
    futex_up(&vfs_phone_futex);
211
    futex_up(&vfs_phone_futex);
212
 
212
 
213
    if (rc != EOK)
213
    if (rc != EOK)
214
        return (off_t) -1;
214
        return (off_t) -1;
215
   
215
   
216
    return newoffs;
216
    return newoffs;
217
}
217
}
218
 
218
 
219
int ftruncate(int fildes, off_t length)
219
int ftruncate(int fildes, off_t length)
220
{
220
{
221
    int res;
221
    int res;
222
    ipcarg_t rc;
222
    ipcarg_t rc;
223
   
223
   
224
    futex_down(&vfs_phone_futex);
224
    futex_down(&vfs_phone_futex);
225
    async_serialize_start();
225
    async_serialize_start();
226
    if (vfs_phone < 0) {
226
    if (vfs_phone < 0) {
227
        res = vfs_connect();
227
        res = vfs_connect();
228
        if (res < 0) {
228
        if (res < 0) {
229
            async_serialize_end();
229
            async_serialize_end();
230
            futex_up(&vfs_phone_futex);
230
            futex_up(&vfs_phone_futex);
231
            return res;
231
            return res;
232
        }
232
        }
233
    }
233
    }
234
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
234
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
235
    async_serialize_end();
235
    async_serialize_end();
236
    futex_up(&vfs_phone_futex);
236
    futex_up(&vfs_phone_futex);
237
    return (int) rc;
237
    return (int) rc;
238
}
238
}
239
 
239
 
240
DIR *opendir(const char *dirname)
240
DIR *opendir(const char *dirname)
241
{
241
{
242
    DIR *dirp = malloc(sizeof(DIR));
242
    DIR *dirp = malloc(sizeof(DIR));
243
    if (!dirp)
243
    if (!dirp)
244
        return NULL;
244
        return NULL;
245
    dirp->fd = open(dirname, 0);    /* TODO: must be a directory */
245
    dirp->fd = open(dirname, 0);    /* TODO: must be a directory */
246
    if (!dirp->fd) {
246
    if (dirp->fd < 0) {
247
        free(dirp);
247
        free(dirp);
248
        return NULL;
248
        return NULL;
249
    }
249
    }
250
    dirp->pos = 0;
-
 
251
    return dirp;
250
    return dirp;
252
}
251
}
253
 
252
 
254
struct dirent *readdir(DIR *dirp)
253
struct dirent *readdir(DIR *dirp)
255
{
254
{
-
 
255
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
-
 
256
    if (len <= 0)
256
    return NULL;    /* TODO */ 
257
        return NULL;
-
 
258
    return &dirp->res;
257
}
259
}
258
 
260
 
259
void rewinddir(DIR *dirp)
261
void rewinddir(DIR *dirp)
260
{
262
{
261
    dirp->pos = 0;
263
    (void) lseek(dirp->fd, 0, SEEK_SET);
262
}
264
}
263
 
265
 
264
int closedir(DIR *dirp)
266
int closedir(DIR *dirp)
265
{
267
{
266
    (void) close(dirp->fd);
268
    (void) close(dirp->fd);
267
    free(dirp);
269
    free(dirp);
268
    return 0;
270
    return 0;
269
}
271
}
270
 
272
 
271
int close(int fildes)
273
int close(int fildes)
272
{
274
{
273
    return 0;   /* TODO */
275
    return 0;   /* TODO */
274
}
276
}
275
 
277
 
276
/** @}
278
/** @}
277
 */
279
 */
278
 
280