Subversion Repositories HelenOS

Rev

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

Rev 2707 Rev 2708
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 fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    vfs_ops.c
34
 * @file    vfs_ops.c
35
 * @brief   Operations that VFS offers to its clients.
35
 * @brief   Operations that VFS offers to its clients.
36
 */
36
 */
37
 
37
 
38
#include <ipc/ipc.h>
38
#include <ipc/ipc.h>
39
#include <async.h>
39
#include <async.h>
40
#include <errno.h>
40
#include <errno.h>
41
#include <stdio.h>
41
#include <stdio.h>
42
#include <stdlib.h>
42
#include <stdlib.h>
43
#include <string.h>
43
#include <string.h>
44
#include <bool.h>
44
#include <bool.h>
45
#include <futex.h>
45
#include <futex.h>
46
#include <rwlock.h>
46
#include <rwlock.h>
47
#include <libadt/list.h>
47
#include <libadt/list.h>
48
#include <unistd.h>
48
#include <unistd.h>
49
#include <ctype.h>
49
#include <ctype.h>
-
 
50
#include <fcntl.h>
50
#include <assert.h>
51
#include <assert.h>
51
#include <atomic.h>
52
#include <atomic.h>
52
#include "vfs.h"
53
#include "vfs.h"
53
 
54
 
54
/**
55
/**
55
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
56
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
56
 * concurrent VFS operation which modifies the file system namespace.
57
 * concurrent VFS operation which modifies the file system namespace.
57
 */
58
 */
58
RWLOCK_INITIALIZE(namespace_rwlock);
59
RWLOCK_INITIALIZE(namespace_rwlock);
59
 
60
 
60
atomic_t rootfs_futex = FUTEX_INITIALIZER;
61
atomic_t rootfs_futex = FUTEX_INITIALIZER;
61
vfs_triplet_t rootfs = {
62
vfs_triplet_t rootfs = {
62
    .fs_handle = 0,
63
    .fs_handle = 0,
63
    .dev_handle = 0,
64
    .dev_handle = 0,
64
    .index = 0,
65
    .index = 0,
65
};
66
};
66
 
67
 
67
static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result)
68
static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result)
68
{
69
{
69
    vfs_pair_t altroot = {
70
    vfs_pair_t altroot = {
70
        .fs_handle = fs_handle,
71
        .fs_handle = fs_handle,
71
        .dev_handle = dev_handle,
72
        .dev_handle = dev_handle,
72
    };
73
    };
73
 
74
 
74
    return vfs_lookup_internal("/", strlen("/"), L_DIRECTORY, result,
75
    return vfs_lookup_internal("/", strlen("/"), L_DIRECTORY, result,
75
        &altroot);
76
        &altroot);
76
}
77
}
77
 
78
 
78
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
79
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
79
{
80
{
80
    int dev_handle;
81
    int dev_handle;
81
    vfs_node_t *mp_node = NULL;
82
    vfs_node_t *mp_node = NULL;
82
 
83
 
83
    /*
84
    /*
84
     * We expect the library to do the device-name to device-handle
85
     * We expect the library to do the device-name to device-handle
85
     * translation for us, thus the device handle will arrive as ARG1
86
     * translation for us, thus the device handle will arrive as ARG1
86
     * in the request.
87
     * in the request.
87
     */
88
     */
88
    dev_handle = IPC_GET_ARG1(*request);
89
    dev_handle = IPC_GET_ARG1(*request);
89
 
90
 
90
    /*
91
    /*
91
     * For now, don't make use of ARG2 and ARG3, but they can be used to
92
     * For now, don't make use of ARG2 and ARG3, but they can be used to
92
     * carry mount options in the future.
93
     * carry mount options in the future.
93
     */
94
     */
94
 
95
 
95
    ipc_callid_t callid;
96
    ipc_callid_t callid;
96
    size_t size;
97
    size_t size;
97
 
98
 
98
    /*
99
    /*
99
     * Now, we expect the client to send us data with the name of the file
100
     * Now, we expect the client to send us data with the name of the file
100
     * system.
101
     * system.
101
     */
102
     */
102
    if (!ipc_data_write_receive(&callid, &size)) {
103
    if (!ipc_data_write_receive(&callid, &size)) {
103
        ipc_answer_0(callid, EINVAL);
104
        ipc_answer_0(callid, EINVAL);
104
        ipc_answer_0(rid, EINVAL);
105
        ipc_answer_0(rid, EINVAL);
105
        return;
106
        return;
106
    }
107
    }
107
 
108
 
108
    /*
109
    /*
109
     * Don't receive more than is necessary for storing a full file system
110
     * Don't receive more than is necessary for storing a full file system
110
     * name.
111
     * name.
111
     */
112
     */
112
    if (size < 1 || size > FS_NAME_MAXLEN) {
113
    if (size < 1 || size > FS_NAME_MAXLEN) {
113
        ipc_answer_0(callid, EINVAL);
114
        ipc_answer_0(callid, EINVAL);
114
        ipc_answer_0(rid, EINVAL);
115
        ipc_answer_0(rid, EINVAL);
115
        return;
116
        return;
116
    }
117
    }
117
 
118
 
118
    /* Deliver the file system name. */
119
    /* Deliver the file system name. */
119
    char fs_name[FS_NAME_MAXLEN + 1];
120
    char fs_name[FS_NAME_MAXLEN + 1];
120
    (void) ipc_data_write_finalize(callid, fs_name, size);
121
    (void) ipc_data_write_finalize(callid, fs_name, size);
121
    fs_name[size] = '\0';
122
    fs_name[size] = '\0';
122
   
123
   
123
    /*
124
    /*
124
     * Check if we know a file system with the same name as is in fs_name.
125
     * Check if we know a file system with the same name as is in fs_name.
125
     * This will also give us its file system handle.
126
     * This will also give us its file system handle.
126
     */
127
     */
127
    int fs_handle = fs_name_to_handle(fs_name, true);
128
    int fs_handle = fs_name_to_handle(fs_name, true);
128
    if (!fs_handle) {
129
    if (!fs_handle) {
129
        ipc_answer_0(rid, ENOENT);
130
        ipc_answer_0(rid, ENOENT);
130
        return;
131
        return;
131
    }
132
    }
132
 
133
 
133
    /* Now, we want the client to send us the mount point. */
134
    /* Now, we want the client to send us the mount point. */
134
    if (!ipc_data_write_receive(&callid, &size)) {
135
    if (!ipc_data_write_receive(&callid, &size)) {
135
        ipc_answer_0(callid, EINVAL);
136
        ipc_answer_0(callid, EINVAL);
136
        ipc_answer_0(rid, EINVAL);
137
        ipc_answer_0(rid, EINVAL);
137
        return;
138
        return;
138
    }
139
    }
139
 
140
 
140
    /* Check whether size is reasonable wrt. the mount point. */
141
    /* Check whether size is reasonable wrt. the mount point. */
141
    if (size < 1 || size > MAX_PATH_LEN) {
142
    if (size < 1 || size > MAX_PATH_LEN) {
142
        ipc_answer_0(callid, EINVAL);
143
        ipc_answer_0(callid, EINVAL);
143
        ipc_answer_0(rid, EINVAL);
144
        ipc_answer_0(rid, EINVAL);
144
        return;
145
        return;
145
    }
146
    }
146
    /* Allocate buffer for the mount point data being received. */
147
    /* Allocate buffer for the mount point data being received. */
147
    uint8_t *buf;
148
    uint8_t *buf;
148
    buf = malloc(size);
149
    buf = malloc(size);
149
    if (!buf) {
150
    if (!buf) {
150
        ipc_answer_0(callid, ENOMEM);
151
        ipc_answer_0(callid, ENOMEM);
151
        ipc_answer_0(rid, ENOMEM);
152
        ipc_answer_0(rid, ENOMEM);
152
        return;
153
        return;
153
    }
154
    }
154
 
155
 
155
    /* Deliver the mount point. */
156
    /* Deliver the mount point. */
156
    (void) ipc_data_write_finalize(callid, buf, size);
157
    (void) ipc_data_write_finalize(callid, buf, size);
157
 
158
 
158
    /*
159
    /*
159
     * Lookup the root node of the filesystem being mounted.
160
     * Lookup the root node of the filesystem being mounted.
160
     * In this case, we don't need to take the namespace_futex as the root
161
     * In this case, we don't need to take the namespace_futex as the root
161
     * node cannot be removed. However, we do take a reference to it so
162
     * node cannot be removed. However, we do take a reference to it so
162
     * that we can track how many times it has been mounted.
163
     * that we can track how many times it has been mounted.
163
     */
164
     */
164
    int rc;
165
    int rc;
165
    vfs_lookup_res_t mr_res;
166
    vfs_lookup_res_t mr_res;
166
    rc = lookup_root(fs_handle, dev_handle, &mr_res);
167
    rc = lookup_root(fs_handle, dev_handle, &mr_res);
167
    if (rc != EOK) {
168
    if (rc != EOK) {
168
        free(buf);
169
        free(buf);
169
        ipc_answer_0(rid, rc);
170
        ipc_answer_0(rid, rc);
170
        return;
171
        return;
171
    }
172
    }
172
    vfs_node_t *mr_node = vfs_node_get(&mr_res);
173
    vfs_node_t *mr_node = vfs_node_get(&mr_res);
173
    if (!mr_node) {
174
    if (!mr_node) {
174
        free(buf);
175
        free(buf);
175
        ipc_answer_0(rid, ENOMEM);
176
        ipc_answer_0(rid, ENOMEM);
176
        return;
177
        return;
177
    }
178
    }
178
 
179
 
179
    /* Finally, we need to resolve the path to the mountpoint. */
180
    /* Finally, we need to resolve the path to the mountpoint. */
180
    vfs_lookup_res_t mp_res;
181
    vfs_lookup_res_t mp_res;
181
    futex_down(&rootfs_futex);
182
    futex_down(&rootfs_futex);
182
    if (rootfs.fs_handle) {
183
    if (rootfs.fs_handle) {
183
        /* We already have the root FS. */
184
        /* We already have the root FS. */
184
        rwlock_write_lock(&namespace_rwlock);
185
        rwlock_write_lock(&namespace_rwlock);
185
        rc = vfs_lookup_internal(buf, size, L_DIRECTORY, &mp_res,
186
        rc = vfs_lookup_internal(buf, size, L_DIRECTORY, &mp_res,
186
            NULL);
187
            NULL);
187
        if (rc != EOK) {
188
        if (rc != EOK) {
188
            /* The lookup failed for some reason. */
189
            /* The lookup failed for some reason. */
189
            rwlock_write_unlock(&namespace_rwlock);
190
            rwlock_write_unlock(&namespace_rwlock);
190
            futex_up(&rootfs_futex);
191
            futex_up(&rootfs_futex);
191
            vfs_node_put(mr_node);  /* failed -> drop reference */
192
            vfs_node_put(mr_node);  /* failed -> drop reference */
192
            free(buf);
193
            free(buf);
193
            ipc_answer_0(rid, rc);
194
            ipc_answer_0(rid, rc);
194
            return;
195
            return;
195
        }
196
        }
196
        mp_node = vfs_node_get(&mp_res);
197
        mp_node = vfs_node_get(&mp_res);
197
        if (!mp_node) {
198
        if (!mp_node) {
198
            rwlock_write_unlock(&namespace_rwlock);
199
            rwlock_write_unlock(&namespace_rwlock);
199
            futex_up(&rootfs_futex);
200
            futex_up(&rootfs_futex);
200
            vfs_node_put(mr_node);  /* failed -> drop reference */
201
            vfs_node_put(mr_node);  /* failed -> drop reference */
201
            free(buf);
202
            free(buf);
202
            ipc_answer_0(rid, ENOMEM);
203
            ipc_answer_0(rid, ENOMEM);
203
            return;
204
            return;
204
        }
205
        }
205
        /*
206
        /*
206
         * Now we hold a reference to mp_node.
207
         * Now we hold a reference to mp_node.
207
         * It will be dropped upon the corresponding VFS_UNMOUNT.
208
         * It will be dropped upon the corresponding VFS_UNMOUNT.
208
         * This prevents the mount point from being deleted.
209
         * This prevents the mount point from being deleted.
209
         */
210
         */
210
        rwlock_write_unlock(&namespace_rwlock);
211
        rwlock_write_unlock(&namespace_rwlock);
211
    } else {
212
    } else {
212
        /* We still don't have the root file system mounted. */
213
        /* We still don't have the root file system mounted. */
213
        if ((size == 1) && (buf[0] == '/')) {
214
        if ((size == 1) && (buf[0] == '/')) {
214
            /* For this simple, but important case, we are done. */
215
            /* For this simple, but important case, we are done. */
215
            rootfs = mr_res.triplet;
216
            rootfs = mr_res.triplet;
216
            futex_up(&rootfs_futex);
217
            futex_up(&rootfs_futex);
217
            free(buf);
218
            free(buf);
218
            ipc_answer_0(rid, EOK);
219
            ipc_answer_0(rid, EOK);
219
            return;
220
            return;
220
        } else {
221
        } else {
221
            /*
222
            /*
222
             * We can't resolve this without the root filesystem
223
             * We can't resolve this without the root filesystem
223
             * being mounted first.
224
             * being mounted first.
224
             */
225
             */
225
            futex_up(&rootfs_futex);
226
            futex_up(&rootfs_futex);
226
            free(buf);
227
            free(buf);
227
            vfs_node_put(mr_node);  /* failed -> drop reference */
228
            vfs_node_put(mr_node);  /* failed -> drop reference */
228
            ipc_answer_0(rid, ENOENT);
229
            ipc_answer_0(rid, ENOENT);
229
            return;
230
            return;
230
        }
231
        }
231
    }
232
    }
232
    futex_up(&rootfs_futex);
233
    futex_up(&rootfs_futex);
233
   
234
   
234
    free(buf);  /* The buffer is not needed anymore. */
235
    free(buf);  /* The buffer is not needed anymore. */
235
   
236
   
236
    /*
237
    /*
237
     * At this point, we have all necessary pieces: file system and device
238
     * At this point, we have all necessary pieces: file system and device
238
     * handles, and we know the mount point VFS node and also the root node
239
     * handles, and we know the mount point VFS node and also the root node
239
     * of the file system being mounted.
240
     * of the file system being mounted.
240
     */
241
     */
241
 
242
 
242
    int phone = vfs_grab_phone(mp_res.triplet.fs_handle);
243
    int phone = vfs_grab_phone(mp_res.triplet.fs_handle);
243
    /* Later we can use ARG3 to pass mode/flags. */
244
    /* Later we can use ARG3 to pass mode/flags. */
244
    aid_t req1 = async_send_3(phone, VFS_MOUNT,
245
    aid_t req1 = async_send_3(phone, VFS_MOUNT,
245
        (ipcarg_t) mp_res.triplet.dev_handle,
246
        (ipcarg_t) mp_res.triplet.dev_handle,
246
        (ipcarg_t) mp_res.triplet.index, 0, NULL);
247
        (ipcarg_t) mp_res.triplet.index, 0, NULL);
247
    /* The second call uses the same method. */
248
    /* The second call uses the same method. */
248
    aid_t req2 = async_send_3(phone, VFS_MOUNT,
249
    aid_t req2 = async_send_3(phone, VFS_MOUNT,
249
        (ipcarg_t) mr_res.triplet.fs_handle,
250
        (ipcarg_t) mr_res.triplet.fs_handle,
250
        (ipcarg_t) mr_res.triplet.dev_handle,
251
        (ipcarg_t) mr_res.triplet.dev_handle,
251
        (ipcarg_t) mr_res.triplet.index, NULL);
252
        (ipcarg_t) mr_res.triplet.index, NULL);
252
    vfs_release_phone(phone);
253
    vfs_release_phone(phone);
253
 
254
 
254
    ipcarg_t rc1;
255
    ipcarg_t rc1;
255
    ipcarg_t rc2;
256
    ipcarg_t rc2;
256
    async_wait_for(req1, &rc1);
257
    async_wait_for(req1, &rc1);
257
    async_wait_for(req2, &rc2);
258
    async_wait_for(req2, &rc2);
258
 
259
 
259
    if ((rc1 != EOK) || (rc2 != EOK)) {
260
    if ((rc1 != EOK) || (rc2 != EOK)) {
260
        /* Mount failed, drop references to mr_node and mp_node. */
261
        /* Mount failed, drop references to mr_node and mp_node. */
261
        vfs_node_put(mr_node);
262
        vfs_node_put(mr_node);
262
        if (mp_node)
263
        if (mp_node)
263
            vfs_node_put(mp_node);
264
            vfs_node_put(mp_node);
264
    }
265
    }
265
   
266
   
266
    if (rc2 == EOK)
267
    if (rc2 == EOK)
267
        ipc_answer_0(rid, rc1);
268
        ipc_answer_0(rid, rc1);
268
    else if (rc1 == EOK)
269
    else if (rc1 == EOK)
269
        ipc_answer_0(rid, rc2);
270
        ipc_answer_0(rid, rc2);
270
    else
271
    else
271
        ipc_answer_0(rid, rc1);
272
        ipc_answer_0(rid, rc1);
272
}
273
}
273
 
274
 
274
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
275
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
275
{
276
{
276
    if (!vfs_files_init()) {
277
    if (!vfs_files_init()) {
277
        ipc_answer_0(rid, ENOMEM);
278
        ipc_answer_0(rid, ENOMEM);
278
        return;
279
        return;
279
    }
280
    }
280
 
281
 
281
    /*
282
    /*
282
     * The POSIX interface is open(path, oflag, mode).
283
     * The POSIX interface is open(path, oflag, mode).
283
     * We can receive oflags and mode along with the VFS_OPEN call; the path
284
     * We can receive oflags and mode along with the VFS_OPEN call; the path
284
     * will need to arrive in another call.
285
     * will need to arrive in another call.
285
     *
286
     *
286
     * We also receive one private, non-POSIX set of flags called lflag
287
     * We also receive one private, non-POSIX set of flags called lflag
287
     * used to pass information to vfs_lookup_internal().
288
     * used to pass information to vfs_lookup_internal().
288
     */
289
     */
289
    int lflag = IPC_GET_ARG1(*request);
290
    int lflag = IPC_GET_ARG1(*request);
290
    int oflag = IPC_GET_ARG2(*request);
291
    int oflag = IPC_GET_ARG2(*request);
291
    int mode = IPC_GET_ARG3(*request);
292
    int mode = IPC_GET_ARG3(*request);
292
    size_t len;
293
    size_t len;
293
 
294
 
-
 
295
    if (oflag & O_CREAT)
-
 
296
        lflag |= L_CREATE;
-
 
297
    if (oflag & O_EXCL)
-
 
298
        lflag |= L_EXCLUSIVE;
-
 
299
 
294
    ipc_callid_t callid;
300
    ipc_callid_t callid;
295
 
301
 
296
    if (!ipc_data_write_receive(&callid, &len)) {
302
    if (!ipc_data_write_receive(&callid, &len)) {
297
        ipc_answer_0(callid, EINVAL);
303
        ipc_answer_0(callid, EINVAL);
298
        ipc_answer_0(rid, EINVAL);
304
        ipc_answer_0(rid, EINVAL);
299
        return;
305
        return;
300
    }
306
    }
301
 
307
 
302
    /*
308
    /*
303
     * Now we are on the verge of accepting the path.
309
     * Now we are on the verge of accepting the path.
304
     *
310
     *
305
     * There is one optimization we could do in the future: copy the path
311
     * There is one optimization we could do in the future: copy the path
306
     * directly into the PLB using some kind of a callback.
312
     * directly into the PLB using some kind of a callback.
307
     */
313
     */
308
    char *path = malloc(len);
314
    char *path = malloc(len);
309
   
315
   
310
    if (!path) {
316
    if (!path) {
311
        ipc_answer_0(callid, ENOMEM);
317
        ipc_answer_0(callid, ENOMEM);
312
        ipc_answer_0(rid, ENOMEM);
318
        ipc_answer_0(rid, ENOMEM);
313
        return;
319
        return;
314
    }
320
    }
315
 
321
 
316
    int rc;
322
    int rc;
317
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
323
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
318
        ipc_answer_0(rid, rc);
324
        ipc_answer_0(rid, rc);
319
        free(path);
325
        free(path);
320
        return;
326
        return;
321
    }
327
    }
322
   
328
   
323
    /*
329
    /*
324
     * Avoid the race condition in which the file can be deleted before we
330
     * Avoid the race condition in which the file can be deleted before we
325
     * find/create-and-lock the VFS node corresponding to the looked-up
331
     * find/create-and-lock the VFS node corresponding to the looked-up
326
     * triplet.
332
     * triplet.
327
     */
333
     */
-
 
334
    if (lflag & L_CREATE)
-
 
335
        rwlock_write_lock(&namespace_rwlock);
-
 
336
    else
328
    rwlock_read_lock(&namespace_rwlock);
337
        rwlock_read_lock(&namespace_rwlock);
329
 
338
 
330
    /* The path is now populated and we can call vfs_lookup_internal(). */
339
    /* The path is now populated and we can call vfs_lookup_internal(). */
331
    vfs_lookup_res_t lr;
340
    vfs_lookup_res_t lr;
332
    rc = vfs_lookup_internal(path, len, lflag, &lr, NULL);
341
    rc = vfs_lookup_internal(path, len, lflag, &lr, NULL);
333
    if (rc) {
342
    if (rc) {
-
 
343
        if (lflag & L_CREATE)
-
 
344
            rwlock_write_unlock(&namespace_rwlock);
-
 
345
        else
334
        rwlock_read_unlock(&namespace_rwlock);
346
            rwlock_read_unlock(&namespace_rwlock);
335
        ipc_answer_0(rid, rc);
347
        ipc_answer_0(rid, rc);
336
        free(path);
348
        free(path);
337
        return;
349
        return;
338
    }
350
    }
339
 
351
 
340
    /** Path is no longer needed. */
352
    /** Path is no longer needed. */
341
    free(path);
353
    free(path);
342
 
354
 
343
    vfs_node_t *node = vfs_node_get(&lr);
355
    vfs_node_t *node = vfs_node_get(&lr);
-
 
356
    if (lflag & L_CREATE)
-
 
357
        rwlock_write_unlock(&namespace_rwlock);
-
 
358
    else
344
    rwlock_read_unlock(&namespace_rwlock);
359
        rwlock_read_unlock(&namespace_rwlock);
345
 
360
 
346
    /*
361
    /*
347
     * Get ourselves a file descriptor and the corresponding vfs_file_t
362
     * Get ourselves a file descriptor and the corresponding vfs_file_t
348
     * structure.
363
     * structure.
349
     */
364
     */
350
    int fd = vfs_fd_alloc();
365
    int fd = vfs_fd_alloc();
351
    if (fd < 0) {
366
    if (fd < 0) {
352
        vfs_node_put(node);
367
        vfs_node_put(node);
353
        ipc_answer_0(rid, fd);
368
        ipc_answer_0(rid, fd);
354
        return;
369
        return;
355
    }
370
    }
356
    vfs_file_t *file = vfs_file_get(fd);
371
    vfs_file_t *file = vfs_file_get(fd);
357
    file->node = node;
372
    file->node = node;
358
 
373
 
359
    /*
374
    /*
360
     * The following increase in reference count is for the fact that the
375
     * The following increase in reference count is for the fact that the
361
     * file is being opened and that a file structure is pointing to it.
376
     * file is being opened and that a file structure is pointing to it.
362
     * It is necessary so that the file will not disappear when
377
     * It is necessary so that the file will not disappear when
363
     * vfs_node_put() is called. The reference will be dropped by the
378
     * vfs_node_put() is called. The reference will be dropped by the
364
     * respective VFS_CLOSE.
379
     * respective VFS_CLOSE.
365
     */
380
     */
366
    vfs_node_addref(node);
381
    vfs_node_addref(node);
367
    vfs_node_put(node);
382
    vfs_node_put(node);
368
 
383
 
369
    /* Success! Return the new file descriptor to the client. */
384
    /* Success! Return the new file descriptor to the client. */
370
    ipc_answer_1(rid, EOK, fd);
385
    ipc_answer_1(rid, EOK, fd);
371
}
386
}
372
 
387
 
373
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
388
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
374
{
389
{
375
 
390
 
376
    /*
391
    /*
377
     * The following code strongly depends on the fact that the files data
392
     * The following code strongly depends on the fact that the files data
378
     * structure can be only accessed by a single fibril and all file
393
     * structure can be only accessed by a single fibril and all file
379
     * operations are serialized (i.e. the reads and writes cannot
394
     * operations are serialized (i.e. the reads and writes cannot
380
     * interleave and a file cannot be closed while it is being read).
395
     * interleave and a file cannot be closed while it is being read).
381
     *
396
     *
382
     * Additional synchronization needs to be added once the table of
397
     * Additional synchronization needs to be added once the table of
383
     * open files supports parallel access!
398
     * open files supports parallel access!
384
     */
399
     */
385
 
400
 
386
    int fd = IPC_GET_ARG1(*request);
401
    int fd = IPC_GET_ARG1(*request);
387
 
402
 
388
    /* Lookup the file structure corresponding to the file descriptor. */
403
    /* Lookup the file structure corresponding to the file descriptor. */
389
    vfs_file_t *file = vfs_file_get(fd);
404
    vfs_file_t *file = vfs_file_get(fd);
390
    if (!file) {
405
    if (!file) {
391
        ipc_answer_0(rid, ENOENT);
406
        ipc_answer_0(rid, ENOENT);
392
        return;
407
        return;
393
    }
408
    }
394
 
409
 
395
    /*
410
    /*
396
     * Now we need to receive a call with client's
411
     * Now we need to receive a call with client's
397
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
412
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
398
     */
413
     */
399
    ipc_callid_t callid;
414
    ipc_callid_t callid;
400
    int res;
415
    int res;
401
    if (read)
416
    if (read)
402
        res = ipc_data_read_receive(&callid, NULL);
417
        res = ipc_data_read_receive(&callid, NULL);
403
    else
418
    else
404
        res = ipc_data_write_receive(&callid, NULL);
419
        res = ipc_data_write_receive(&callid, NULL);
405
    if (!res) {
420
    if (!res) {
406
        ipc_answer_0(callid, EINVAL);
421
        ipc_answer_0(callid, EINVAL);
407
        ipc_answer_0(rid, EINVAL);
422
        ipc_answer_0(rid, EINVAL);
408
        return;
423
        return;
409
    }
424
    }
410
 
425
 
411
    /*
426
    /*
412
     * Lock the open file structure so that no other thread can manipulate
427
     * Lock the open file structure so that no other thread can manipulate
413
     * the same open file at a time.
428
     * the same open file at a time.
414
     */
429
     */
415
    futex_down(&file->lock);
430
    futex_down(&file->lock);
416
 
431
 
417
    /*
432
    /*
418
     * Lock the file's node so that no other client can read/write to it at
433
     * Lock the file's node so that no other client can read/write to it at
419
     * the same time.
434
     * the same time.
420
     */
435
     */
421
    if (read)
436
    if (read)
422
        rwlock_read_lock(&file->node->contents_rwlock);
437
        rwlock_read_lock(&file->node->contents_rwlock);
423
    else
438
    else
424
        rwlock_write_lock(&file->node->contents_rwlock);
439
        rwlock_write_lock(&file->node->contents_rwlock);
425
 
440
 
426
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
441
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
427
   
442
   
428
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
443
    /* Make a VFS_READ/VFS_WRITE request at the destination FS server. */
429
    aid_t msg;
444
    aid_t msg;
430
    ipc_call_t answer;
445
    ipc_call_t answer;
431
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
446
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
432
        file->node->dev_handle, file->node->index, file->pos, &answer);
447
        file->node->dev_handle, file->node->index, file->pos, &answer);
433
   
448
   
434
    /*
449
    /*
435
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
450
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
436
     * destination FS server. The call will be routed as if sent by
451
     * destination FS server. The call will be routed as if sent by
437
     * ourselves. Note that call arguments are immutable in this case so we
452
     * ourselves. Note that call arguments are immutable in this case so we
438
     * don't have to bother.
453
     * don't have to bother.
439
     */
454
     */
440
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
455
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
441
 
456
 
442
    vfs_release_phone(fs_phone);
457
    vfs_release_phone(fs_phone);
443
 
458
 
444
    /* Wait for reply from the FS server. */
459
    /* Wait for reply from the FS server. */
445
    ipcarg_t rc;
460
    ipcarg_t rc;
446
    async_wait_for(msg, &rc);
461
    async_wait_for(msg, &rc);
447
    size_t bytes = IPC_GET_ARG1(answer);
462
    size_t bytes = IPC_GET_ARG1(answer);
448
 
463
 
449
    /* Unlock the VFS node. */
464
    /* Unlock the VFS node. */
450
    if (read)
465
    if (read)
451
        rwlock_read_unlock(&file->node->contents_rwlock);
466
        rwlock_read_unlock(&file->node->contents_rwlock);
452
    else {
467
    else {
453
        /* Update the cached version of node's size. */
468
        /* Update the cached version of node's size. */
454
        file->node->size = IPC_GET_ARG2(answer);
469
        file->node->size = IPC_GET_ARG2(answer);
455
        rwlock_write_unlock(&file->node->contents_rwlock);
470
        rwlock_write_unlock(&file->node->contents_rwlock);
456
    }
471
    }
457
 
472
 
458
    /* Update the position pointer and unlock the open file. */
473
    /* Update the position pointer and unlock the open file. */
459
    file->pos += bytes;
474
    file->pos += bytes;
460
    futex_up(&file->lock);
475
    futex_up(&file->lock);
461
 
476
 
462
    /*
477
    /*
463
     * FS server's reply is the final result of the whole operation we
478
     * FS server's reply is the final result of the whole operation we
464
     * return to the client.
479
     * return to the client.
465
     */
480
     */
466
    ipc_answer_1(rid, rc, bytes);
481
    ipc_answer_1(rid, rc, bytes);
467
}
482
}
468
 
483
 
469
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
484
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
470
{
485
{
471
    vfs_rdwr(rid, request, true);
486
    vfs_rdwr(rid, request, true);
472
}
487
}
473
 
488
 
474
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
489
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
475
{
490
{
476
    vfs_rdwr(rid, request, false);
491
    vfs_rdwr(rid, request, false);
477
}
492
}
478
 
493
 
479
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
494
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
480
{
495
{
481
    int fd = (int) IPC_GET_ARG1(*request);
496
    int fd = (int) IPC_GET_ARG1(*request);
482
    off_t off = (off_t) IPC_GET_ARG2(*request);
497
    off_t off = (off_t) IPC_GET_ARG2(*request);
483
    int whence = (int) IPC_GET_ARG3(*request);
498
    int whence = (int) IPC_GET_ARG3(*request);
484
 
499
 
485
 
500
 
486
    /* Lookup the file structure corresponding to the file descriptor. */
501
    /* Lookup the file structure corresponding to the file descriptor. */
487
    vfs_file_t *file = vfs_file_get(fd);
502
    vfs_file_t *file = vfs_file_get(fd);
488
    if (!file) {
503
    if (!file) {
489
        ipc_answer_0(rid, ENOENT);
504
        ipc_answer_0(rid, ENOENT);
490
        return;
505
        return;
491
    }
506
    }
492
 
507
 
493
    off_t newpos;
508
    off_t newpos;
494
    futex_down(&file->lock);
509
    futex_down(&file->lock);
495
    if (whence == SEEK_SET) {
510
    if (whence == SEEK_SET) {
496
        file->pos = off;
511
        file->pos = off;
497
        futex_up(&file->lock);
512
        futex_up(&file->lock);
498
        ipc_answer_1(rid, EOK, off);
513
        ipc_answer_1(rid, EOK, off);
499
        return;
514
        return;
500
    }
515
    }
501
    if (whence == SEEK_CUR) {
516
    if (whence == SEEK_CUR) {
502
        if (file->pos + off < file->pos) {
517
        if (file->pos + off < file->pos) {
503
            futex_up(&file->lock);
518
            futex_up(&file->lock);
504
            ipc_answer_0(rid, EOVERFLOW);
519
            ipc_answer_0(rid, EOVERFLOW);
505
            return;
520
            return;
506
        }
521
        }
507
        file->pos += off;
522
        file->pos += off;
508
        newpos = file->pos;
523
        newpos = file->pos;
509
        futex_up(&file->lock);
524
        futex_up(&file->lock);
510
        ipc_answer_1(rid, EOK, newpos);
525
        ipc_answer_1(rid, EOK, newpos);
511
        return;
526
        return;
512
    }
527
    }
513
    if (whence == SEEK_END) {
528
    if (whence == SEEK_END) {
514
        rwlock_read_lock(&file->node->contents_rwlock);
529
        rwlock_read_lock(&file->node->contents_rwlock);
515
        size_t size = file->node->size;
530
        size_t size = file->node->size;
516
        rwlock_read_unlock(&file->node->contents_rwlock);
531
        rwlock_read_unlock(&file->node->contents_rwlock);
517
        if (size + off < size) {
532
        if (size + off < size) {
518
            futex_up(&file->lock);
533
            futex_up(&file->lock);
519
            ipc_answer_0(rid, EOVERFLOW);
534
            ipc_answer_0(rid, EOVERFLOW);
520
            return;
535
            return;
521
        }
536
        }
522
        newpos = size + off;
537
        newpos = size + off;
523
        futex_up(&file->lock);
538
        futex_up(&file->lock);
524
        ipc_answer_1(rid, EOK, newpos);
539
        ipc_answer_1(rid, EOK, newpos);
525
        return;
540
        return;
526
    }
541
    }
527
    futex_up(&file->lock);
542
    futex_up(&file->lock);
528
    ipc_answer_0(rid, EINVAL);
543
    ipc_answer_0(rid, EINVAL);
529
}
544
}
530
 
545
 
531
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
546
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
532
{
547
{
533
    int fd = IPC_GET_ARG1(*request);
548
    int fd = IPC_GET_ARG1(*request);
534
    size_t size = IPC_GET_ARG2(*request);
549
    size_t size = IPC_GET_ARG2(*request);
535
    ipcarg_t rc;
550
    ipcarg_t rc;
536
 
551
 
537
    vfs_file_t *file = vfs_file_get(fd);
552
    vfs_file_t *file = vfs_file_get(fd);
538
    if (!file) {
553
    if (!file) {
539
        ipc_answer_0(rid, ENOENT);
554
        ipc_answer_0(rid, ENOENT);
540
        return;
555
        return;
541
    }
556
    }
542
    futex_down(&file->lock);
557
    futex_down(&file->lock);
543
 
558
 
544
    rwlock_write_lock(&file->node->contents_rwlock);
559
    rwlock_write_lock(&file->node->contents_rwlock);
545
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
560
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
546
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE,
561
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE,
547
        (ipcarg_t)file->node->dev_handle, (ipcarg_t)file->node->index,
562
        (ipcarg_t)file->node->dev_handle, (ipcarg_t)file->node->index,
548
        (ipcarg_t)size);
563
        (ipcarg_t)size);
549
    vfs_release_phone(fs_phone);
564
    vfs_release_phone(fs_phone);
550
    if (rc == EOK)
565
    if (rc == EOK)
551
        file->node->size = size;
566
        file->node->size = size;
552
    rwlock_write_unlock(&file->node->contents_rwlock);
567
    rwlock_write_unlock(&file->node->contents_rwlock);
553
 
568
 
554
    futex_up(&file->lock);
569
    futex_up(&file->lock);
555
    ipc_answer_0(rid, rc);
570
    ipc_answer_0(rid, rc);
556
}
571
}
557
 
572
 
558
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
573
void vfs_mkdir(ipc_callid_t rid, ipc_call_t *request)
559
{
574
{
560
    int mode = IPC_GET_ARG1(*request);
575
    int mode = IPC_GET_ARG1(*request);
561
    size_t len;
576
    size_t len;
562
 
577
 
563
    ipc_callid_t callid;
578
    ipc_callid_t callid;
564
 
579
 
565
    if (!ipc_data_write_receive(&callid, &len)) {
580
    if (!ipc_data_write_receive(&callid, &len)) {
566
        ipc_answer_0(callid, EINVAL);
581
        ipc_answer_0(callid, EINVAL);
567
        ipc_answer_0(rid, EINVAL);
582
        ipc_answer_0(rid, EINVAL);
568
        return;
583
        return;
569
    }
584
    }
570
 
585
 
571
    /*
586
    /*
572
     * Now we are on the verge of accepting the path.
587
     * Now we are on the verge of accepting the path.
573
     *
588
     *
574
     * There is one optimization we could do in the future: copy the path
589
     * There is one optimization we could do in the future: copy the path
575
     * directly into the PLB using some kind of a callback.
590
     * directly into the PLB using some kind of a callback.
576
     */
591
     */
577
    char *path = malloc(len);
592
    char *path = malloc(len);
578
   
593
   
579
    if (!path) {
594
    if (!path) {
580
        ipc_answer_0(callid, ENOMEM);
595
        ipc_answer_0(callid, ENOMEM);
581
        ipc_answer_0(rid, ENOMEM);
596
        ipc_answer_0(rid, ENOMEM);
582
        return;
597
        return;
583
    }
598
    }
584
 
599
 
585
    int rc;
600
    int rc;
586
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
601
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
587
        ipc_answer_0(rid, rc);
602
        ipc_answer_0(rid, rc);
588
        free(path);
603
        free(path);
589
        return;
604
        return;
590
    }
605
    }
591
   
606
   
592
    rwlock_write_lock(&namespace_rwlock);
607
    rwlock_write_lock(&namespace_rwlock);
593
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
608
    int lflag = L_DIRECTORY | L_CREATE | L_EXCLUSIVE;
594
    rc = vfs_lookup_internal(path, len, lflag, NULL, NULL);
609
    rc = vfs_lookup_internal(path, len, lflag, NULL, NULL);
595
    rwlock_write_unlock(&namespace_rwlock);
610
    rwlock_write_unlock(&namespace_rwlock);
596
    free(path);
611
    free(path);
597
    ipc_answer_0(rid, rc);
612
    ipc_answer_0(rid, rc);
598
}
613
}
599
 
614
 
600
/**
615
/**
601
 * @}
616
 * @}
602
 */
617
 */
603
 
618