Subversion Repositories HelenOS

Rev

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

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