Subversion Repositories HelenOS

Rev

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

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