Subversion Repositories HelenOS

Rev

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

Rev 2671 Rev 2674
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 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 <unistd.h>
-
 
37
#include <fcntl.h>
36
#include <ipc/ipc.h>
38
#include <ipc/ipc.h>
37
#include <ipc/services.h>
39
#include <ipc/services.h>
38
#include <async.h>
40
#include <async.h>
39
#include <atomic.h>
41
#include <atomic.h>
40
#include <futex.h>
42
#include <futex.h>
41
#include <errno.h>
43
#include <errno.h>
42
#include <string.h>
44
#include <string.h>
43
#include "../../srv/vfs/vfs.h"
45
#include "../../srv/vfs/vfs.h"
44
 
46
 
45
int vfs_phone = -1;
47
int vfs_phone = -1;
46
atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
48
atomic_t vfs_phone_futex = FUTEX_INITIALIZER;
47
 
49
 
48
static int vfs_connect(void)
50
static int vfs_connect(void)
49
{
51
{
50
    if (vfs_phone < 0)
52
    if (vfs_phone < 0)
51
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
53
        vfs_phone = ipc_connect_me_to(PHONE_NS, SERVICE_VFS, 0, 0);
52
    return vfs_phone;
54
    return vfs_phone;
53
}
55
}
54
 
56
 
55
int mount(const char *fs_name, const char *mp, const char *dev)
57
int mount(const char *fs_name, const char *mp, const char *dev)
56
{
58
{
57
    int res;
59
    int res;
58
    ipcarg_t rc;
60
    ipcarg_t rc;
59
    aid_t req;
61
    aid_t req;
60
 
62
 
61
    int dev_handle = 0; /* TODO */
63
    int dev_handle = 0; /* TODO */
62
 
64
 
63
    futex_down(&vfs_phone_futex);
65
    futex_down(&vfs_phone_futex);
64
    async_serialize_start();
66
    async_serialize_start();
65
    if (vfs_phone < 0) {
67
    if (vfs_phone < 0) {
66
        res = vfs_connect();
68
        res = vfs_connect();
67
        if (res < 0) {
69
        if (res < 0) {
68
            async_serialize_end();
70
            async_serialize_end();
69
            futex_up(&vfs_phone_futex);
71
            futex_up(&vfs_phone_futex);
70
            return res;
72
            return res;
71
        }
73
        }
72
    }
74
    }
73
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
75
    req = async_send_1(vfs_phone, VFS_MOUNT, dev_handle, NULL);
74
    rc = ipc_data_write_send(vfs_phone, (void *)fs_name, strlen(fs_name));
76
    rc = ipc_data_write_send(vfs_phone, (void *)fs_name, strlen(fs_name));
75
    if (rc != EOK) {
77
    if (rc != EOK) {
76
        async_wait_for(req, NULL);
78
        async_wait_for(req, NULL);
77
        async_serialize_end();
79
        async_serialize_end();
78
        futex_up(&vfs_phone_futex);
80
        futex_up(&vfs_phone_futex);
79
        return (int) rc;
81
        return (int) rc;
80
    }
82
    }
81
    rc = ipc_data_write_send(vfs_phone, (void *)mp, strlen(mp));
83
    rc = ipc_data_write_send(vfs_phone, (void *)mp, strlen(mp));
82
    if (rc != EOK) {
84
    if (rc != EOK) {
83
        async_wait_for(req, NULL);
85
        async_wait_for(req, NULL);
84
        async_serialize_end();
86
        async_serialize_end();
85
        futex_up(&vfs_phone_futex);
87
        futex_up(&vfs_phone_futex);
86
        return (int) rc;
88
        return (int) rc;
87
    }
89
    }
88
    async_wait_for(req, &rc);
90
    async_wait_for(req, &rc);
89
    async_serialize_end();
91
    async_serialize_end();
90
    futex_up(&vfs_phone_futex);
92
    futex_up(&vfs_phone_futex);
91
    return (int) rc;
93
    return (int) rc;
92
}
94
}
93
 
95
 
94
 
96
 
95
int open(const char *name, int oflag, ...)
97
int open(const char *name, int oflag, ...)
96
{
98
{
97
    int res;
99
    int res;
98
    ipcarg_t rc;
100
    ipcarg_t rc;
99
    ipc_call_t answer;
101
    ipc_call_t answer;
100
    aid_t req;
102
    aid_t req;
101
   
103
   
102
    futex_down(&vfs_phone_futex);
104
    futex_down(&vfs_phone_futex);
103
    async_serialize_start();
105
    async_serialize_start();
104
    if (vfs_phone < 0) {
106
    if (vfs_phone < 0) {
105
        res = vfs_connect();
107
        res = vfs_connect();
106
        if (res < 0) {
108
        if (res < 0) {
107
            async_serialize_end();
109
            async_serialize_end();
108
            futex_up(&vfs_phone_futex);
110
            futex_up(&vfs_phone_futex);
109
            return res;
111
            return res;
110
        }
112
        }
111
    }
113
    }
112
    req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer);
114
    req = async_send_2(vfs_phone, VFS_OPEN, oflag, 0, &answer);
113
    rc = ipc_data_write_send(vfs_phone, name, strlen(name));
115
    rc = ipc_data_write_send(vfs_phone, name, strlen(name));
114
    if (rc != EOK) {
116
    if (rc != EOK) {
115
        async_wait_for(req, NULL);
117
        async_wait_for(req, NULL);
116
        async_serialize_end();
118
        async_serialize_end();
117
        futex_up(&vfs_phone_futex);
119
        futex_up(&vfs_phone_futex);
118
        return (int) rc;
120
        return (int) rc;
119
    }
121
    }
120
    async_wait_for(req, &rc);
122
    async_wait_for(req, &rc);
121
    async_serialize_end();
123
    async_serialize_end();
122
    futex_up(&vfs_phone_futex);
124
    futex_up(&vfs_phone_futex);
123
    return (int) IPC_GET_ARG1(answer);
125
    return (int) IPC_GET_ARG1(answer);
124
}
126
}
125
 
127
 
126
ssize_t read(int fildes, void *buf, size_t nbyte)
128
ssize_t read(int fildes, void *buf, size_t nbyte)
127
{
129
{
128
    int res;
130
    int res;
129
    ipcarg_t rc;
131
    ipcarg_t rc;
130
    ipc_call_t answer;
132
    ipc_call_t answer;
131
    aid_t req;
133
    aid_t req;
132
 
134
 
133
    futex_down(&vfs_phone_futex);
135
    futex_down(&vfs_phone_futex);
134
    async_serialize_start();
136
    async_serialize_start();
135
    if (vfs_phone < 0) {
137
    if (vfs_phone < 0) {
136
        res = vfs_connect();
138
        res = vfs_connect();
137
        if (res < 0) {
139
        if (res < 0) {
138
            async_serialize_end();
140
            async_serialize_end();
139
            futex_up(&vfs_phone_futex);
141
            futex_up(&vfs_phone_futex);
140
            return res;
142
            return res;
141
        }
143
        }
142
    }
144
    }
143
    req = async_send_1(vfs_phone, VFS_READ, 0, &answer);
145
    req = async_send_1(vfs_phone, VFS_READ, fildes, &answer);
144
    if (ipc_data_read_send(vfs_phone, buf, sizeof(buf)) != EOK) {
146
    if (ipc_data_read_send(vfs_phone, buf, sizeof(buf)) != EOK) {
145
        async_wait_for(req, NULL);
147
        async_wait_for(req, NULL);
146
        async_serialize_end();
148
        async_serialize_end();
147
        futex_up(&vfs_phone_futex);
149
        futex_up(&vfs_phone_futex);
148
        return (ssize_t) rc;
150
        return (ssize_t) rc;
149
    }
151
    }
150
    async_wait_for(req, &rc);
152
    async_wait_for(req, &rc);
151
    async_serialize_end();
153
    async_serialize_end();
152
    futex_up(&vfs_phone_futex);
154
    futex_up(&vfs_phone_futex);
153
    return (ssize_t) IPC_GET_ARG1(answer);
155
    return (ssize_t) IPC_GET_ARG1(answer);
154
}
156
}
155
 
157
 
-
 
158
ssize_t write(int fildes, const void *buf, size_t nbyte)
-
 
159
{
-
 
160
    int res;
-
 
161
    ipcarg_t rc;
-
 
162
    ipc_call_t answer;
-
 
163
    aid_t req;
-
 
164
 
-
 
165
    futex_down(&vfs_phone_futex);
-
 
166
    async_serialize_start();
-
 
167
    if (vfs_phone < 0) {
-
 
168
        res = vfs_connect();
-
 
169
        if (res < 0) {
-
 
170
            async_serialize_end();
-
 
171
            futex_up(&vfs_phone_futex);
-
 
172
            return res;
-
 
173
        }
-
 
174
    }
-
 
175
    req = async_send_1(vfs_phone, VFS_WRITE, fildes, &answer);
-
 
176
    if (ipc_data_write_send(vfs_phone, buf, sizeof(buf)) != EOK) {
-
 
177
        async_wait_for(req, NULL);
-
 
178
        async_serialize_end();
-
 
179
        futex_up(&vfs_phone_futex);
-
 
180
        return (ssize_t) rc;
-
 
181
    }
-
 
182
    async_wait_for(req, &rc);
-
 
183
    async_serialize_end();
-
 
184
    futex_up(&vfs_phone_futex);
-
 
185
    return (ssize_t) IPC_GET_ARG1(answer);
-
 
186
}
156
/** @}
187
/** @}
157
 */
188
 */
158
 
189