Subversion Repositories HelenOS

Rev

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

Rev 2710 Rev 2734
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
    int res;
-
 
139
    ipcarg_t rc;
-
 
140
 
-
 
141
    futex_down(&vfs_phone_futex);
-
 
142
    async_serialize_start();
-
 
143
    if (vfs_phone < 0) {
-
 
144
        res = vfs_connect();
-
 
145
        if (res < 0) {
-
 
146
            async_serialize_end();
-
 
147
            futex_up(&vfs_phone_futex);
-
 
148
            return res;
-
 
149
        }
-
 
150
    }
-
 
151
       
-
 
152
    rc = async_req_1_0(vfs_phone, VFS_CLOSE, fildes);
-
 
153
 
-
 
154
    async_serialize_end();
-
 
155
    futex_up(&vfs_phone_futex);
-
 
156
   
138
    return 0;   /* TODO */
157
    return (int)rc;
139
}
158
}
140
 
159
 
141
ssize_t read(int fildes, void *buf, size_t nbyte)
160
ssize_t read(int fildes, void *buf, size_t nbyte)
142
{
161
{
143
    int res;
162
    int res;
144
    ipcarg_t rc;
163
    ipcarg_t rc;
145
    ipc_call_t answer;
164
    ipc_call_t answer;
146
    aid_t req;
165
    aid_t req;
147
 
166
 
148
    futex_down(&vfs_phone_futex);
167
    futex_down(&vfs_phone_futex);
149
    async_serialize_start();
168
    async_serialize_start();
150
    if (vfs_phone < 0) {
169
    if (vfs_phone < 0) {
151
        res = vfs_connect();
170
        res = vfs_connect();
152
        if (res < 0) {
171
        if (res < 0) {
153
            async_serialize_end();
172
            async_serialize_end();
154
            futex_up(&vfs_phone_futex);
173
            futex_up(&vfs_phone_futex);
155
            return res;
174
            return res;
156
        }
175
        }
157
    }
176
    }
158
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
177
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
159
    if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
178
    if (ipc_data_read_start(vfs_phone, (void *)buf, nbyte) != EOK) {
160
        async_wait_for(req, NULL);
179
        async_wait_for(req, NULL);
161
        async_serialize_end();
180
        async_serialize_end();
162
        futex_up(&vfs_phone_futex);
181
        futex_up(&vfs_phone_futex);
163
        return (ssize_t) rc;
182
        return (ssize_t) rc;
164
    }
183
    }
165
    async_wait_for(req, &rc);
184
    async_wait_for(req, &rc);
166
    async_serialize_end();
185
    async_serialize_end();
167
    futex_up(&vfs_phone_futex);
186
    futex_up(&vfs_phone_futex);
168
    if (rc == EOK)
187
    if (rc == EOK)
169
        return (ssize_t) IPC_GET_ARG1(answer);
188
        return (ssize_t) IPC_GET_ARG1(answer);
170
    else
189
    else
171
        return -1;
190
        return -1;
172
}
191
}
173
 
192
 
174
ssize_t write(int fildes, const void *buf, size_t nbyte)
193
ssize_t write(int fildes, const void *buf, size_t nbyte)
175
{
194
{
176
    int res;
195
    int res;
177
    ipcarg_t rc;
196
    ipcarg_t rc;
178
    ipc_call_t answer;
197
    ipc_call_t answer;
179
    aid_t req;
198
    aid_t req;
180
 
199
 
181
    futex_down(&vfs_phone_futex);
200
    futex_down(&vfs_phone_futex);
182
    async_serialize_start();
201
    async_serialize_start();
183
    if (vfs_phone < 0) {
202
    if (vfs_phone < 0) {
184
        res = vfs_connect();
203
        res = vfs_connect();
185
        if (res < 0) {
204
        if (res < 0) {
186
            async_serialize_end();
205
            async_serialize_end();
187
            futex_up(&vfs_phone_futex);
206
            futex_up(&vfs_phone_futex);
188
            return res;
207
            return res;
189
        }
208
        }
190
    }
209
    }
191
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
210
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
192
    if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
211
    if (ipc_data_write_start(vfs_phone, (void *)buf, nbyte) != EOK) {
193
        async_wait_for(req, NULL);
212
        async_wait_for(req, NULL);
194
        async_serialize_end();
213
        async_serialize_end();
195
        futex_up(&vfs_phone_futex);
214
        futex_up(&vfs_phone_futex);
196
        return (ssize_t) rc;
215
        return (ssize_t) rc;
197
    }
216
    }
198
    async_wait_for(req, &rc);
217
    async_wait_for(req, &rc);
199
    async_serialize_end();
218
    async_serialize_end();
200
    futex_up(&vfs_phone_futex);
219
    futex_up(&vfs_phone_futex);
201
    if (rc == EOK)
220
    if (rc == EOK)
202
        return (ssize_t) IPC_GET_ARG1(answer);
221
        return (ssize_t) IPC_GET_ARG1(answer);
203
    else
222
    else
204
        return -1;
223
        return -1;
205
}
224
}
206
 
225
 
207
off_t lseek(int fildes, off_t offset, int whence)
226
off_t lseek(int fildes, off_t offset, int whence)
208
{
227
{
209
    int res;
228
    int res;
210
    ipcarg_t rc;
229
    ipcarg_t rc;
211
 
230
 
212
    futex_down(&vfs_phone_futex);
231
    futex_down(&vfs_phone_futex);
213
    async_serialize_start();
232
    async_serialize_start();
214
    if (vfs_phone < 0) {
233
    if (vfs_phone < 0) {
215
        res = vfs_connect();
234
        res = vfs_connect();
216
        if (res < 0) {
235
        if (res < 0) {
217
            async_serialize_end();
236
            async_serialize_end();
218
            futex_up(&vfs_phone_futex);
237
            futex_up(&vfs_phone_futex);
219
            return res;
238
            return res;
220
        }
239
        }
221
    }
240
    }
222
       
241
       
223
    off_t newoffs;
242
    off_t newoffs;
224
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
243
    rc = async_req_3_1(vfs_phone, VFS_SEEK, fildes, offset, whence,
225
        (ipcarg_t)&newoffs);
244
        (ipcarg_t)&newoffs);
226
 
245
 
227
    async_serialize_end();
246
    async_serialize_end();
228
    futex_up(&vfs_phone_futex);
247
    futex_up(&vfs_phone_futex);
229
 
248
 
230
    if (rc != EOK)
249
    if (rc != EOK)
231
        return (off_t) -1;
250
        return (off_t) -1;
232
   
251
   
233
    return newoffs;
252
    return newoffs;
234
}
253
}
235
 
254
 
236
int ftruncate(int fildes, off_t length)
255
int ftruncate(int fildes, off_t length)
237
{
256
{
238
    int res;
257
    int res;
239
    ipcarg_t rc;
258
    ipcarg_t rc;
240
   
259
   
241
    futex_down(&vfs_phone_futex);
260
    futex_down(&vfs_phone_futex);
242
    async_serialize_start();
261
    async_serialize_start();
243
    if (vfs_phone < 0) {
262
    if (vfs_phone < 0) {
244
        res = vfs_connect();
263
        res = vfs_connect();
245
        if (res < 0) {
264
        if (res < 0) {
246
            async_serialize_end();
265
            async_serialize_end();
247
            futex_up(&vfs_phone_futex);
266
            futex_up(&vfs_phone_futex);
248
            return res;
267
            return res;
249
        }
268
        }
250
    }
269
    }
251
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
270
    rc = async_req_2_0(vfs_phone, VFS_TRUNCATE, fildes, length);
252
    async_serialize_end();
271
    async_serialize_end();
253
    futex_up(&vfs_phone_futex);
272
    futex_up(&vfs_phone_futex);
254
    return (int) rc;
273
    return (int) rc;
255
}
274
}
256
 
275
 
257
DIR *opendir(const char *dirname)
276
DIR *opendir(const char *dirname)
258
{
277
{
259
    DIR *dirp = malloc(sizeof(DIR));
278
    DIR *dirp = malloc(sizeof(DIR));
260
    if (!dirp)
279
    if (!dirp)
261
        return NULL;
280
        return NULL;
262
    dirp->fd = _open(dirname, L_DIRECTORY, 0);
281
    dirp->fd = _open(dirname, L_DIRECTORY, 0);
263
    if (dirp->fd < 0) {
282
    if (dirp->fd < 0) {
264
        free(dirp);
283
        free(dirp);
265
        return NULL;
284
        return NULL;
266
    }
285
    }
267
    return dirp;
286
    return dirp;
268
}
287
}
269
 
288
 
270
struct dirent *readdir(DIR *dirp)
289
struct dirent *readdir(DIR *dirp)
271
{
290
{
272
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
291
    ssize_t len = read(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1);
273
    if (len <= 0)
292
    if (len <= 0)
274
        return NULL;
293
        return NULL;
275
    return &dirp->res;
294
    return &dirp->res;
276
}
295
}
277
 
296
 
278
void rewinddir(DIR *dirp)
297
void rewinddir(DIR *dirp)
279
{
298
{
280
    (void) lseek(dirp->fd, 0, SEEK_SET);
299
    (void) lseek(dirp->fd, 0, SEEK_SET);
281
}
300
}
282
 
301
 
283
int closedir(DIR *dirp)
302
int closedir(DIR *dirp)
284
{
303
{
285
    (void) close(dirp->fd);
304
    (void) close(dirp->fd);
286
    free(dirp);
305
    free(dirp);
287
    return 0;
306
    return 0;
288
}
307
}
289
 
308
 
290
int mkdir(const char *path, mode_t mode)
309
int mkdir(const char *path, mode_t mode)
291
{
310
{
292
    int res;
311
    int res;
293
    ipcarg_t rc;
312
    ipcarg_t rc;
294
    ipc_call_t answer;
313
    ipc_call_t answer;
295
    aid_t req;
314
    aid_t req;
296
   
315
   
297
    futex_down(&vfs_phone_futex);
316
    futex_down(&vfs_phone_futex);
298
    async_serialize_start();
317
    async_serialize_start();
299
    if (vfs_phone < 0) {
318
    if (vfs_phone < 0) {
300
        res = vfs_connect();
319
        res = vfs_connect();
301
        if (res < 0) {
320
        if (res < 0) {
302
            async_serialize_end();
321
            async_serialize_end();
303
            futex_up(&vfs_phone_futex);
322
            futex_up(&vfs_phone_futex);
304
            return res;
323
            return res;
305
        }
324
        }
306
    }
325
    }
307
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, &answer);
326
    req = async_send_1(vfs_phone, VFS_MKDIR, mode, &answer);
308
    rc = ipc_data_write_start(vfs_phone, path, strlen(path));
327
    rc = ipc_data_write_start(vfs_phone, path, strlen(path));
309
    if (rc != EOK) {
328
    if (rc != EOK) {
310
        async_wait_for(req, NULL);
329
        async_wait_for(req, NULL);
311
        async_serialize_end();
330
        async_serialize_end();
312
        futex_up(&vfs_phone_futex);
331
        futex_up(&vfs_phone_futex);
313
        return (int) rc;
332
        return (int) rc;
314
    }
333
    }
315
    async_wait_for(req, &rc);
334
    async_wait_for(req, &rc);
316
    async_serialize_end();
335
    async_serialize_end();
317
    futex_up(&vfs_phone_futex);
336
    futex_up(&vfs_phone_futex);
318
    return EOK;
337
    return EOK;
319
}
338
}
320
 
339
 
321
/** @}
340
/** @}
322
 */
341
 */
323
 
342