Subversion Repositories HelenOS

Rev

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

Rev 2693 Rev 2698
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 <ipc/services.h>
-
 
40
#include <async.h>
39
#include <async.h>
41
#include <fibril.h>
-
 
42
#include <errno.h>
40
#include <errno.h>
43
#include <stdio.h>
41
#include <stdio.h>
44
#include <stdlib.h>
42
#include <stdlib.h>
45
#include <string.h>
43
#include <string.h>
46
#include <bool.h>
44
#include <bool.h>
47
#include <futex.h>
45
#include <futex.h>
48
#include <rwlock.h>
46
#include <rwlock.h>
49
#include <libadt/list.h>
47
#include <libadt/list.h>
50
#include <unistd.h>
48
#include <unistd.h>
51
#include <ctype.h>
49
#include <ctype.h>
52
#include <as.h>
-
 
53
#include <assert.h>
50
#include <assert.h>
54
#include <atomic.h>
51
#include <atomic.h>
55
#include "vfs.h"
52
#include "vfs.h"
56
 
53
 
57
#define min(a, b)   ((a) < (b) ? (a) : (b))
-
 
58
 
-
 
59
/**
54
/**
60
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
55
 * This rwlock prevents the race between a triplet-to-VFS-node resolution and a
61
 * concurrent VFS operation which modifies the file system namespace.
56
 * concurrent VFS operation which modifies the file system namespace.
62
 */
57
 */
63
RWLOCK_INITIALIZE(namespace_rwlock);
58
RWLOCK_INITIALIZE(namespace_rwlock);
64
 
59
 
65
atomic_t plb_futex = FUTEX_INITIALIZER;
-
 
66
link_t plb_head;    /**< PLB entry ring buffer. */
-
 
67
uint8_t *plb = NULL;
-
 
68
 
-
 
69
/** Perform a path lookup.
-
 
70
 *
-
 
71
 * @param path      Path to be resolved; it needn't be an ASCIIZ string.
-
 
72
 * @param len       Number of path characters pointed by path.
-
 
73
 * @param result    Empty structure where the lookup result will be stored.
-
 
74
 * @param altroot   If non-empty, will be used instead of rootfs as the root
-
 
75
 *          of the whole VFS tree.
-
 
76
 *
-
 
77
 * @return      EOK on success or an error code from errno.h.
-
 
78
 */
-
 
79
int vfs_lookup_internal(char *path, size_t len, vfs_lookup_res_t *result,
-
 
80
    vfs_pair_t *altroot)
-
 
81
{
-
 
82
    vfs_pair_t *root;
-
 
83
 
-
 
84
    if (!len)
-
 
85
        return EINVAL;
-
 
86
 
-
 
87
    if (altroot)
-
 
88
        root = altroot;
-
 
89
    else
-
 
90
        root = (vfs_pair_t *) &rootfs;
-
 
91
 
-
 
92
    if (!root->fs_handle)
-
 
93
        return ENOENT;
-
 
94
   
-
 
95
    futex_down(&plb_futex);
-
 
96
 
-
 
97
    plb_entry_t entry;
-
 
98
    link_initialize(&entry.plb_link);
-
 
99
    entry.len = len;
-
 
100
 
-
 
101
    off_t first;    /* the first free index */
-
 
102
    off_t last; /* the last free index */
-
 
103
 
-
 
104
    if (list_empty(&plb_head)) {
-
 
105
        first = 0;
-
 
106
        last = PLB_SIZE - 1;
-
 
107
    } else {
-
 
108
        plb_entry_t *oldest = list_get_instance(plb_head.next,
-
 
109
            plb_entry_t, plb_link);
-
 
110
        plb_entry_t *newest = list_get_instance(plb_head.prev,
-
 
111
            plb_entry_t, plb_link);
-
 
112
 
-
 
113
        first = (newest->index + newest->len) % PLB_SIZE;
-
 
114
        last = (oldest->index - 1) % PLB_SIZE;
-
 
115
    }
-
 
116
 
-
 
117
    if (first <= last) {
-
 
118
        if ((last - first) + 1 < len) {
-
 
119
            /*
-
 
120
             * The buffer cannot absorb the path.
-
 
121
             */
-
 
122
            futex_up(&plb_futex);
-
 
123
            return ELIMIT;
-
 
124
        }
-
 
125
    } else {
-
 
126
        if (PLB_SIZE - ((first - last) + 1) < len) {
-
 
127
            /*
-
 
128
             * The buffer cannot absorb the path.
-
 
129
             */
-
 
130
            futex_up(&plb_futex);
-
 
131
            return ELIMIT;
-
 
132
        }
-
 
133
    }
-
 
134
 
-
 
135
    /*
-
 
136
     * We know the first free index in PLB and we also know that there is
-
 
137
     * enough space in the buffer to hold our path.
-
 
138
     */
-
 
139
 
-
 
140
    entry.index = first;
-
 
141
    entry.len = len;
-
 
142
 
-
 
143
    /*
-
 
144
     * Claim PLB space by inserting the entry into the PLB entry ring
-
 
145
     * buffer.
-
 
146
     */
-
 
147
    list_append(&entry.plb_link, &plb_head);
-
 
148
   
-
 
149
    futex_up(&plb_futex);
-
 
150
 
-
 
151
    /*
-
 
152
     * Copy the path into PLB.
-
 
153
     */
-
 
154
    size_t cnt1 = min(len, (PLB_SIZE - first) + 1);
-
 
155
    size_t cnt2 = len - cnt1;
-
 
156
   
-
 
157
    memcpy(&plb[first], path, cnt1);
-
 
158
    memcpy(plb, &path[cnt1], cnt2);
-
 
159
 
-
 
160
    ipc_call_t answer;
-
 
161
    int phone = vfs_grab_phone(root->fs_handle);
-
 
162
    aid_t req = async_send_3(phone, VFS_LOOKUP, (ipcarg_t) first,
-
 
163
        (ipcarg_t) (first + len - 1) % PLB_SIZE,
-
 
164
        (ipcarg_t) root->dev_handle, &answer);
-
 
165
    vfs_release_phone(phone);
-
 
166
 
-
 
167
    ipcarg_t rc;
-
 
168
    async_wait_for(req, &rc);
-
 
169
 
-
 
170
    futex_down(&plb_futex);
-
 
171
    list_remove(&entry.plb_link);
-
 
172
    /*
-
 
173
     * Erasing the path from PLB will come handy for debugging purposes.
-
 
174
     */
-
 
175
    memset(&plb[first], 0, cnt1);
-
 
176
    memset(plb, 0, cnt2);
-
 
177
    futex_up(&plb_futex);
-
 
178
 
-
 
179
    if (rc == EOK) {
-
 
180
        result->triplet.fs_handle = (int) IPC_GET_ARG1(answer);
-
 
181
        result->triplet.dev_handle = (int) IPC_GET_ARG2(answer);
-
 
182
        result->triplet.index = (int) IPC_GET_ARG3(answer);
-
 
183
        result->size = (size_t) IPC_GET_ARG4(answer);
-
 
184
    }
-
 
185
 
-
 
186
    return rc;
-
 
187
}
-
 
188
 
-
 
189
atomic_t rootfs_futex = FUTEX_INITIALIZER;
60
atomic_t rootfs_futex = FUTEX_INITIALIZER;
190
vfs_triplet_t rootfs = {
61
vfs_triplet_t rootfs = {
191
    .fs_handle = 0,
62
    .fs_handle = 0,
192
    .dev_handle = 0,
63
    .dev_handle = 0,
193
    .index = 0,
64
    .index = 0,
194
};
65
};
195
 
66
 
196
static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result)
67
static int lookup_root(int fs_handle, int dev_handle, vfs_lookup_res_t *result)
197
{
68
{
198
    vfs_pair_t altroot = {
69
    vfs_pair_t altroot = {
199
        .fs_handle = fs_handle,
70
        .fs_handle = fs_handle,
200
        .dev_handle = dev_handle,
71
        .dev_handle = dev_handle,
201
    };
72
    };
202
 
73
 
203
    return vfs_lookup_internal("/", strlen("/"), result, &altroot);
74
    return vfs_lookup_internal("/", strlen("/"), result, &altroot);
204
}
75
}
205
 
76
 
206
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
77
void vfs_mount(ipc_callid_t rid, ipc_call_t *request)
207
{
78
{
208
    int dev_handle;
79
    int dev_handle;
209
    vfs_node_t *mp_node = NULL;
80
    vfs_node_t *mp_node = NULL;
210
 
81
 
211
    /*
82
    /*
212
     * We expect the library to do the device-name to device-handle
83
     * We expect the library to do the device-name to device-handle
213
     * translation for us, thus the device handle will arrive as ARG1
84
     * translation for us, thus the device handle will arrive as ARG1
214
     * in the request.
85
     * in the request.
215
     */
86
     */
216
    dev_handle = IPC_GET_ARG1(*request);
87
    dev_handle = IPC_GET_ARG1(*request);
217
 
88
 
218
    /*
89
    /*
219
     * For now, don't make use of ARG2 and ARG3, but they can be used to
90
     * For now, don't make use of ARG2 and ARG3, but they can be used to
220
     * carry mount options in the future.
91
     * carry mount options in the future.
221
     */
92
     */
222
 
93
 
223
    ipc_callid_t callid;
94
    ipc_callid_t callid;
224
    size_t size;
95
    size_t size;
225
 
96
 
226
    /*
97
    /*
227
     * Now, we expect the client to send us data with the name of the file
98
     * Now, we expect the client to send us data with the name of the file
228
     * system.
99
     * system.
229
     */
100
     */
230
    if (!ipc_data_write_receive(&callid, &size)) {
101
    if (!ipc_data_write_receive(&callid, &size)) {
231
        ipc_answer_0(callid, EINVAL);
102
        ipc_answer_0(callid, EINVAL);
232
        ipc_answer_0(rid, EINVAL);
103
        ipc_answer_0(rid, EINVAL);
233
        return;
104
        return;
234
    }
105
    }
235
 
106
 
236
    /*
107
    /*
237
     * Don't receive more than is necessary for storing a full file system
108
     * Don't receive more than is necessary for storing a full file system
238
     * name.
109
     * name.
239
     */
110
     */
240
    if (size < 1 || size > FS_NAME_MAXLEN) {
111
    if (size < 1 || size > FS_NAME_MAXLEN) {
241
        ipc_answer_0(callid, EINVAL);
112
        ipc_answer_0(callid, EINVAL);
242
        ipc_answer_0(rid, EINVAL);
113
        ipc_answer_0(rid, EINVAL);
243
        return;
114
        return;
244
    }
115
    }
245
 
116
 
246
    /*
117
    /*
247
     * Deliver the file system name.
118
     * Deliver the file system name.
248
     */
119
     */
249
    char fs_name[FS_NAME_MAXLEN + 1];
120
    char fs_name[FS_NAME_MAXLEN + 1];
250
    (void) ipc_data_write_finalize(callid, fs_name, size);
121
    (void) ipc_data_write_finalize(callid, fs_name, size);
251
    fs_name[size] = '\0';
122
    fs_name[size] = '\0';
252
   
123
   
253
    /*
124
    /*
254
     * 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.
255
     * This will also give us its file system handle.
126
     * This will also give us its file system handle.
256
     */
127
     */
257
    int fs_handle = fs_name_to_handle(fs_name, true);
128
    int fs_handle = fs_name_to_handle(fs_name, true);
258
    if (!fs_handle) {
129
    if (!fs_handle) {
259
        ipc_answer_0(rid, ENOENT);
130
        ipc_answer_0(rid, ENOENT);
260
        return;
131
        return;
261
    }
132
    }
262
 
133
 
263
    /*
134
    /*
264
     * Now, we want the client to send us the mount point.
135
     * Now, we want the client to send us the mount point.
265
     */
136
     */
266
    if (!ipc_data_write_receive(&callid, &size)) {
137
    if (!ipc_data_write_receive(&callid, &size)) {
267
        ipc_answer_0(callid, EINVAL);
138
        ipc_answer_0(callid, EINVAL);
268
        ipc_answer_0(rid, EINVAL);
139
        ipc_answer_0(rid, EINVAL);
269
        return;
140
        return;
270
    }
141
    }
271
 
142
 
272
    /*
143
    /*
273
     * Check whether size is reasonable wrt. the mount point.
144
     * Check whether size is reasonable wrt. the mount point.
274
     */
145
     */
275
    if (size < 1 || size > MAX_PATH_LEN) {
146
    if (size < 1 || size > MAX_PATH_LEN) {
276
        ipc_answer_0(callid, EINVAL);
147
        ipc_answer_0(callid, EINVAL);
277
        ipc_answer_0(rid, EINVAL);
148
        ipc_answer_0(rid, EINVAL);
278
        return;
149
        return;
279
    }
150
    }
280
    /*
151
    /*
281
     * Allocate buffer for the mount point data being received.
152
     * Allocate buffer for the mount point data being received.
282
     */
153
     */
283
    uint8_t *buf;
154
    uint8_t *buf;
284
    buf = malloc(size);
155
    buf = malloc(size);
285
    if (!buf) {
156
    if (!buf) {
286
        ipc_answer_0(callid, ENOMEM);
157
        ipc_answer_0(callid, ENOMEM);
287
        ipc_answer_0(rid, ENOMEM);
158
        ipc_answer_0(rid, ENOMEM);
288
        return;
159
        return;
289
    }
160
    }
290
 
161
 
291
    /*
162
    /*
292
     * Deliver the mount point.
163
     * Deliver the mount point.
293
     */
164
     */
294
    (void) ipc_data_write_finalize(callid, buf, size);
165
    (void) ipc_data_write_finalize(callid, buf, size);
295
 
166
 
296
    /*
167
    /*
297
     * Lookup the root node of the filesystem being mounted.
168
     * Lookup the root node of the filesystem being mounted.
298
     * In this case, we don't need to take the namespace_futex as the root
169
     * In this case, we don't need to take the namespace_futex as the root
299
     * node cannot be removed. However, we do take a reference to it so
170
     * node cannot be removed. However, we do take a reference to it so
300
     * that we can track how many times it has been mounted.
171
     * that we can track how many times it has been mounted.
301
     */
172
     */
302
    int rc;
173
    int rc;
303
    vfs_lookup_res_t mr_res;
174
    vfs_lookup_res_t mr_res;
304
    rc = lookup_root(fs_handle, dev_handle, &mr_res);
175
    rc = lookup_root(fs_handle, dev_handle, &mr_res);
305
    if (rc != EOK) {
176
    if (rc != EOK) {
306
        free(buf);
177
        free(buf);
307
        ipc_answer_0(rid, rc);
178
        ipc_answer_0(rid, rc);
308
        return;
179
        return;
309
    }
180
    }
310
    vfs_node_t *mr_node = vfs_node_get(&mr_res);
181
    vfs_node_t *mr_node = vfs_node_get(&mr_res);
311
    if (!mr_node) {
182
    if (!mr_node) {
312
        free(buf);
183
        free(buf);
313
        ipc_answer_0(rid, ENOMEM);
184
        ipc_answer_0(rid, ENOMEM);
314
        return;
185
        return;
315
    }
186
    }
316
 
187
 
317
    /*
188
    /*
318
     * Finally, we need to resolve the path to the mountpoint.
189
     * Finally, we need to resolve the path to the mountpoint.
319
     */
190
     */
320
    vfs_lookup_res_t mp_res;
191
    vfs_lookup_res_t mp_res;
321
    futex_down(&rootfs_futex);
192
    futex_down(&rootfs_futex);
322
    if (rootfs.fs_handle) {
193
    if (rootfs.fs_handle) {
323
        /*
194
        /*
324
         * We already have the root FS.
195
         * We already have the root FS.
325
         */
196
         */
326
        rwlock_write_lock(&namespace_rwlock);
197
        rwlock_write_lock(&namespace_rwlock);
327
        rc = vfs_lookup_internal(buf, size, &mp_res, NULL);
198
        rc = vfs_lookup_internal(buf, size, &mp_res, NULL);
328
        if (rc != EOK) {
199
        if (rc != EOK) {
329
            /*
200
            /*
330
             * The lookup failed for some reason.
201
             * The lookup failed for some reason.
331
             */
202
             */
332
            rwlock_write_unlock(&namespace_rwlock);
203
            rwlock_write_unlock(&namespace_rwlock);
333
            futex_up(&rootfs_futex);
204
            futex_up(&rootfs_futex);
334
            vfs_node_put(mr_node);  /* failed -> drop reference */
205
            vfs_node_put(mr_node);  /* failed -> drop reference */
335
            free(buf);
206
            free(buf);
336
            ipc_answer_0(rid, rc);
207
            ipc_answer_0(rid, rc);
337
            return;
208
            return;
338
        }
209
        }
339
        mp_node = vfs_node_get(&mp_res);
210
        mp_node = vfs_node_get(&mp_res);
340
        if (!mp_node) {
211
        if (!mp_node) {
341
            rwlock_write_unlock(&namespace_rwlock);
212
            rwlock_write_unlock(&namespace_rwlock);
342
            futex_up(&rootfs_futex);
213
            futex_up(&rootfs_futex);
343
            vfs_node_put(mr_node);  /* failed -> drop reference */
214
            vfs_node_put(mr_node);  /* failed -> drop reference */
344
            free(buf);
215
            free(buf);
345
            ipc_answer_0(rid, ENOMEM);
216
            ipc_answer_0(rid, ENOMEM);
346
            return;
217
            return;
347
        }
218
        }
348
        /*
219
        /*
349
         * Now we hold a reference to mp_node.
220
         * Now we hold a reference to mp_node.
350
         * It will be dropped upon the corresponding VFS_UNMOUNT.
221
         * It will be dropped upon the corresponding VFS_UNMOUNT.
351
         * This prevents the mount point from being deleted.
222
         * This prevents the mount point from being deleted.
352
         */
223
         */
353
        rwlock_write_unlock(&namespace_rwlock);
224
        rwlock_write_unlock(&namespace_rwlock);
354
    } else {
225
    } else {
355
        /*
226
        /*
356
         * We still don't have the root file system mounted.
227
         * We still don't have the root file system mounted.
357
         */
228
         */
358
        if ((size == 1) && (buf[0] == '/')) {
229
        if ((size == 1) && (buf[0] == '/')) {
359
            /*
230
            /*
360
             * For this simple, but important case, we are done.
231
             * For this simple, but important case, we are done.
361
             */
232
             */
362
            rootfs = mr_res.triplet;
233
            rootfs = mr_res.triplet;
363
            futex_up(&rootfs_futex);
234
            futex_up(&rootfs_futex);
364
            free(buf);
235
            free(buf);
365
            ipc_answer_0(rid, EOK);
236
            ipc_answer_0(rid, EOK);
366
            return;
237
            return;
367
        } else {
238
        } else {
368
            /*
239
            /*
369
             * We can't resolve this without the root filesystem
240
             * We can't resolve this without the root filesystem
370
             * being mounted first.
241
             * being mounted first.
371
             */
242
             */
372
            futex_up(&rootfs_futex);
243
            futex_up(&rootfs_futex);
373
            free(buf);
244
            free(buf);
374
            vfs_node_put(mr_node);  /* failed -> drop reference */
245
            vfs_node_put(mr_node);  /* failed -> drop reference */
375
            ipc_answer_0(rid, ENOENT);
246
            ipc_answer_0(rid, ENOENT);
376
            return;
247
            return;
377
        }
248
        }
378
    }
249
    }
379
    futex_up(&rootfs_futex);
250
    futex_up(&rootfs_futex);
380
   
251
   
381
    free(buf);  /* The buffer is not needed anymore. */
252
    free(buf);  /* The buffer is not needed anymore. */
382
   
253
   
383
    /*
254
    /*
384
     * At this point, we have all necessary pieces: file system and device
255
     * At this point, we have all necessary pieces: file system and device
385
     * handles, and we know the mount point VFS node and also the root node
256
     * handles, and we know the mount point VFS node and also the root node
386
     * of the file system being mounted.
257
     * of the file system being mounted.
387
     */
258
     */
388
 
259
 
389
    int phone = vfs_grab_phone(mp_res.triplet.fs_handle);
260
    int phone = vfs_grab_phone(mp_res.triplet.fs_handle);
390
    /* Later we can use ARG3 to pass mode/flags. */
261
    /* Later we can use ARG3 to pass mode/flags. */
391
    aid_t req1 = async_send_3(phone, VFS_MOUNT,
262
    aid_t req1 = async_send_3(phone, VFS_MOUNT,
392
        (ipcarg_t) mp_res.triplet.dev_handle,
263
        (ipcarg_t) mp_res.triplet.dev_handle,
393
        (ipcarg_t) mp_res.triplet.index, 0, NULL);
264
        (ipcarg_t) mp_res.triplet.index, 0, NULL);
394
    /* The second call uses the same method. */
265
    /* The second call uses the same method. */
395
    aid_t req2 = async_send_3(phone, VFS_MOUNT,
266
    aid_t req2 = async_send_3(phone, VFS_MOUNT,
396
        (ipcarg_t) mr_res.triplet.fs_handle,
267
        (ipcarg_t) mr_res.triplet.fs_handle,
397
        (ipcarg_t) mr_res.triplet.dev_handle,
268
        (ipcarg_t) mr_res.triplet.dev_handle,
398
        (ipcarg_t) mr_res.triplet.index, NULL);
269
        (ipcarg_t) mr_res.triplet.index, NULL);
399
    vfs_release_phone(phone);
270
    vfs_release_phone(phone);
400
 
271
 
401
    ipcarg_t rc1;
272
    ipcarg_t rc1;
402
    ipcarg_t rc2;
273
    ipcarg_t rc2;
403
    async_wait_for(req1, &rc1);
274
    async_wait_for(req1, &rc1);
404
    async_wait_for(req2, &rc2);
275
    async_wait_for(req2, &rc2);
405
 
276
 
406
    if ((rc1 != EOK) || (rc2 != EOK)) {
277
    if ((rc1 != EOK) || (rc2 != EOK)) {
407
        /* Mount failed, drop references to mr_node and mp_node. */
278
        /* Mount failed, drop references to mr_node and mp_node. */
408
        vfs_node_put(mr_node);
279
        vfs_node_put(mr_node);
409
        if (mp_node)
280
        if (mp_node)
410
            vfs_node_put(mp_node);
281
            vfs_node_put(mp_node);
411
    }
282
    }
412
   
283
   
413
    if (rc2 == EOK)
284
    if (rc2 == EOK)
414
        ipc_answer_0(rid, rc1);
285
        ipc_answer_0(rid, rc1);
415
    else if (rc1 == EOK)
286
    else if (rc1 == EOK)
416
        ipc_answer_0(rid, rc2);
287
        ipc_answer_0(rid, rc2);
417
    else
288
    else
418
        ipc_answer_0(rid, rc1);
289
        ipc_answer_0(rid, rc1);
419
}
290
}
420
 
291
 
421
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
292
void vfs_open(ipc_callid_t rid, ipc_call_t *request)
422
{
293
{
423
    if (!vfs_files_init()) {
294
    if (!vfs_files_init()) {
424
        ipc_answer_0(rid, ENOMEM);
295
        ipc_answer_0(rid, ENOMEM);
425
        return;
296
        return;
426
    }
297
    }
427
 
298
 
428
    /*
299
    /*
429
     * The POSIX interface is open(path, flags, mode).
300
     * The POSIX interface is open(path, flags, mode).
430
     * We can receive flags and mode along with the VFS_OPEN call; the path
301
     * We can receive flags and mode along with the VFS_OPEN call; the path
431
     * will need to arrive in another call.
302
     * will need to arrive in another call.
432
     */
303
     */
433
    int flags = IPC_GET_ARG1(*request);
304
    int flags = IPC_GET_ARG1(*request);
434
    int mode = IPC_GET_ARG2(*request);
305
    int mode = IPC_GET_ARG2(*request);
435
    size_t len;
306
    size_t len;
436
 
307
 
437
    ipc_callid_t callid;
308
    ipc_callid_t callid;
438
 
309
 
439
    if (!ipc_data_write_receive(&callid, &len)) {
310
    if (!ipc_data_write_receive(&callid, &len)) {
440
        ipc_answer_0(callid, EINVAL);
311
        ipc_answer_0(callid, EINVAL);
441
        ipc_answer_0(rid, EINVAL);
312
        ipc_answer_0(rid, EINVAL);
442
        return;
313
        return;
443
    }
314
    }
444
 
315
 
445
    /*
316
    /*
446
     * Now we are on the verge of accepting the path.
317
     * Now we are on the verge of accepting the path.
447
     *
318
     *
448
     * There is one optimization we could do in the future: copy the path
319
     * There is one optimization we could do in the future: copy the path
449
     * directly into the PLB using some kind of a callback.
320
     * directly into the PLB using some kind of a callback.
450
     */
321
     */
451
    char *path = malloc(len);
322
    char *path = malloc(len);
452
   
323
   
453
    if (!path) {
324
    if (!path) {
454
        ipc_answer_0(callid, ENOMEM);
325
        ipc_answer_0(callid, ENOMEM);
455
        ipc_answer_0(rid, ENOMEM);
326
        ipc_answer_0(rid, ENOMEM);
456
        return;
327
        return;
457
    }
328
    }
458
 
329
 
459
    int rc;
330
    int rc;
460
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
331
    if ((rc = ipc_data_write_finalize(callid, path, len))) {
461
        ipc_answer_0(rid, rc);
332
        ipc_answer_0(rid, rc);
462
        free(path);
333
        free(path);
463
        return;
334
        return;
464
    }
335
    }
465
   
336
   
466
    /*
337
    /*
467
     * Avoid the race condition in which the file can be deleted before we
338
     * Avoid the race condition in which the file can be deleted before we
468
     * find/create-and-lock the VFS node corresponding to the looked-up
339
     * find/create-and-lock the VFS node corresponding to the looked-up
469
     * triplet.
340
     * triplet.
470
     */
341
     */
471
    rwlock_read_lock(&namespace_rwlock);
342
    rwlock_read_lock(&namespace_rwlock);
472
 
343
 
473
    /*
344
    /*
474
     * The path is now populated and we can call vfs_lookup_internal().
345
     * The path is now populated and we can call vfs_lookup_internal().
475
     */
346
     */
476
    vfs_lookup_res_t lr;
347
    vfs_lookup_res_t lr;
477
    rc = vfs_lookup_internal(path, len, &lr, NULL);
348
    rc = vfs_lookup_internal(path, len, &lr, NULL);
478
    if (rc) {
349
    if (rc) {
479
        rwlock_read_unlock(&namespace_rwlock);
350
        rwlock_read_unlock(&namespace_rwlock);
480
        ipc_answer_0(rid, rc);
351
        ipc_answer_0(rid, rc);
481
        free(path);
352
        free(path);
482
        return;
353
        return;
483
    }
354
    }
484
 
355
 
485
    /*
356
    /*
486
     * Path is no longer needed.
357
     * Path is no longer needed.
487
     */
358
     */
488
    free(path);
359
    free(path);
489
 
360
 
490
    vfs_node_t *node = vfs_node_get(&lr);
361
    vfs_node_t *node = vfs_node_get(&lr);
491
    rwlock_read_unlock(&namespace_rwlock);
362
    rwlock_read_unlock(&namespace_rwlock);
492
 
363
 
493
    /*
364
    /*
494
     * Get ourselves a file descriptor and the corresponding vfs_file_t
365
     * Get ourselves a file descriptor and the corresponding vfs_file_t
495
     * structure.
366
     * structure.
496
     */
367
     */
497
    int fd = vfs_fd_alloc();
368
    int fd = vfs_fd_alloc();
498
    if (fd < 0) {
369
    if (fd < 0) {
499
        vfs_node_put(node);
370
        vfs_node_put(node);
500
        ipc_answer_0(rid, fd);
371
        ipc_answer_0(rid, fd);
501
        return;
372
        return;
502
    }
373
    }
503
    vfs_file_t *file = vfs_file_get(fd);
374
    vfs_file_t *file = vfs_file_get(fd);
504
    file->node = node;
375
    file->node = node;
505
 
376
 
506
    /*
377
    /*
507
     * The following increase in reference count is for the fact that the
378
     * The following increase in reference count is for the fact that the
508
     * file is being opened and that a file structure is pointing to it.
379
     * file is being opened and that a file structure is pointing to it.
509
     * It is necessary so that the file will not disappear when
380
     * It is necessary so that the file will not disappear when
510
     * vfs_node_put() is called. The reference will be dropped by the
381
     * vfs_node_put() is called. The reference will be dropped by the
511
     * respective VFS_CLOSE.
382
     * respective VFS_CLOSE.
512
     */
383
     */
513
    vfs_node_addref(node);
384
    vfs_node_addref(node);
514
    vfs_node_put(node);
385
    vfs_node_put(node);
515
 
386
 
516
    /*
387
    /*
517
     * Success! Return the new file descriptor to the client.
388
     * Success! Return the new file descriptor to the client.
518
     */
389
     */
519
    ipc_answer_1(rid, EOK, fd);
390
    ipc_answer_1(rid, EOK, fd);
520
}
391
}
521
 
392
 
522
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
393
static void vfs_rdwr(ipc_callid_t rid, ipc_call_t *request, bool read)
523
{
394
{
524
 
395
 
525
    /*
396
    /*
526
     * The following code strongly depends on the fact that the files data
397
     * The following code strongly depends on the fact that the files data
527
     * structure can be only accessed by a single fibril and all file
398
     * structure can be only accessed by a single fibril and all file
528
     * operations are serialized (i.e. the reads and writes cannot
399
     * operations are serialized (i.e. the reads and writes cannot
529
     * interleave and a file cannot be closed while it is being read).
400
     * interleave and a file cannot be closed while it is being read).
530
     *
401
     *
531
     * Additional synchronization needs to be added once the table of
402
     * Additional synchronization needs to be added once the table of
532
     * open files supports parallel access!
403
     * open files supports parallel access!
533
     */
404
     */
534
 
405
 
535
    int fd = IPC_GET_ARG1(*request);
406
    int fd = IPC_GET_ARG1(*request);
536
 
407
 
537
    /*
408
    /*
538
     * Lookup the file structure corresponding to the file descriptor.
409
     * Lookup the file structure corresponding to the file descriptor.
539
     */
410
     */
540
    vfs_file_t *file = vfs_file_get(fd);
411
    vfs_file_t *file = vfs_file_get(fd);
541
    if (!file) {
412
    if (!file) {
542
        ipc_answer_0(rid, ENOENT);
413
        ipc_answer_0(rid, ENOENT);
543
        return;
414
        return;
544
    }
415
    }
545
 
416
 
546
    /*
417
    /*
547
     * Now we need to receive a call with client's
418
     * Now we need to receive a call with client's
548
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
419
     * IPC_M_DATA_READ/IPC_M_DATA_WRITE request.
549
     */
420
     */
550
    ipc_callid_t callid;
421
    ipc_callid_t callid;
551
    int res;
422
    int res;
552
    if (read)
423
    if (read)
553
        res = ipc_data_read_receive(&callid, NULL);
424
        res = ipc_data_read_receive(&callid, NULL);
554
    else
425
    else
555
        res = ipc_data_write_receive(&callid, NULL);
426
        res = ipc_data_write_receive(&callid, NULL);
556
    if (!res) {
427
    if (!res) {
557
        ipc_answer_0(callid, EINVAL);
428
        ipc_answer_0(callid, EINVAL);
558
        ipc_answer_0(rid, EINVAL);
429
        ipc_answer_0(rid, EINVAL);
559
        return;
430
        return;
560
    }
431
    }
561
 
432
 
562
    /*
433
    /*
563
     * Lock the open file structure so that no other thread can manipulate
434
     * Lock the open file structure so that no other thread can manipulate
564
     * the same open file at a time.
435
     * the same open file at a time.
565
     */
436
     */
566
    futex_down(&file->lock);
437
    futex_down(&file->lock);
567
 
438
 
568
    /*
439
    /*
569
     * Lock the file's node so that no other client can read/write to it at
440
     * Lock the file's node so that no other client can read/write to it at
570
     * the same time.
441
     * the same time.
571
     */
442
     */
572
    if (read)
443
    if (read)
573
        rwlock_read_lock(&file->node->contents_rwlock);
444
        rwlock_read_lock(&file->node->contents_rwlock);
574
    else
445
    else
575
        rwlock_write_lock(&file->node->contents_rwlock);
446
        rwlock_write_lock(&file->node->contents_rwlock);
576
 
447
 
577
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
448
    int fs_phone = vfs_grab_phone(file->node->fs_handle);  
578
   
449
   
579
    /*
450
    /*
580
     * Make a VFS_READ/VFS_WRITE request at the destination FS server.
451
     * Make a VFS_READ/VFS_WRITE request at the destination FS server.
581
     */
452
     */
582
    aid_t msg;
453
    aid_t msg;
583
    ipc_call_t answer;
454
    ipc_call_t answer;
584
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
455
    msg = async_send_3(fs_phone, IPC_GET_METHOD(*request),
585
        file->node->dev_handle, file->node->index, file->pos, &answer);
456
        file->node->dev_handle, file->node->index, file->pos, &answer);
586
   
457
   
587
    /*
458
    /*
588
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
459
     * Forward the IPC_M_DATA_READ/IPC_M_DATA_WRITE request to the
589
     * destination FS server. The call will be routed as if sent by
460
     * destination FS server. The call will be routed as if sent by
590
     * ourselves. Note that call arguments are immutable in this case so we
461
     * ourselves. Note that call arguments are immutable in this case so we
591
     * don't have to bother.
462
     * don't have to bother.
592
     */
463
     */
593
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
464
    ipc_forward_fast(callid, fs_phone, 0, 0, 0, IPC_FF_ROUTE_FROM_ME);
594
 
465
 
595
    vfs_release_phone(fs_phone);
466
    vfs_release_phone(fs_phone);
596
 
467
 
597
    /*
468
    /*
598
     * Wait for reply from the FS server.
469
     * Wait for reply from the FS server.
599
     */
470
     */
600
    ipcarg_t rc;
471
    ipcarg_t rc;
601
    async_wait_for(msg, &rc);
472
    async_wait_for(msg, &rc);
602
    size_t bytes = IPC_GET_ARG1(answer);
473
    size_t bytes = IPC_GET_ARG1(answer);
603
 
474
 
604
    /*
475
    /*
605
     * Unlock the VFS node.
476
     * Unlock the VFS node.
606
     */
477
     */
607
    if (read)
478
    if (read)
608
        rwlock_read_unlock(&file->node->contents_rwlock);
479
        rwlock_read_unlock(&file->node->contents_rwlock);
609
    else {
480
    else {
610
        /* Update the cached version of node's size. */
481
        /* Update the cached version of node's size. */
611
        file->node->size = IPC_GET_ARG2(answer);
482
        file->node->size = IPC_GET_ARG2(answer);
612
        rwlock_write_unlock(&file->node->contents_rwlock);
483
        rwlock_write_unlock(&file->node->contents_rwlock);
613
    }
484
    }
614
 
485
 
615
    /*
486
    /*
616
     * Update the position pointer and unlock the open file.
487
     * Update the position pointer and unlock the open file.
617
     */
488
     */
618
    file->pos += bytes;
489
    file->pos += bytes;
619
    futex_up(&file->lock);
490
    futex_up(&file->lock);
620
 
491
 
621
    /*
492
    /*
622
     * FS server's reply is the final result of the whole operation we
493
     * FS server's reply is the final result of the whole operation we
623
     * return to the client.
494
     * return to the client.
624
     */
495
     */
625
    ipc_answer_1(rid, rc, bytes);
496
    ipc_answer_1(rid, rc, bytes);
626
}
497
}
627
 
498
 
628
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
499
void vfs_read(ipc_callid_t rid, ipc_call_t *request)
629
{
500
{
630
    vfs_rdwr(rid, request, true);
501
    vfs_rdwr(rid, request, true);
631
}
502
}
632
 
503
 
633
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
504
void vfs_write(ipc_callid_t rid, ipc_call_t *request)
634
{
505
{
635
    vfs_rdwr(rid, request, false);
506
    vfs_rdwr(rid, request, false);
636
}
507
}
637
 
508
 
638
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
509
void vfs_seek(ipc_callid_t rid, ipc_call_t *request)
639
{
510
{
640
    int fd = (int) IPC_GET_ARG1(*request);
511
    int fd = (int) IPC_GET_ARG1(*request);
641
    off_t off = (off_t) IPC_GET_ARG2(*request);
512
    off_t off = (off_t) IPC_GET_ARG2(*request);
642
    int whence = (int) IPC_GET_ARG3(*request);
513
    int whence = (int) IPC_GET_ARG3(*request);
643
 
514
 
644
 
515
 
645
    /*
516
    /*
646
     * Lookup the file structure corresponding to the file descriptor.
517
     * Lookup the file structure corresponding to the file descriptor.
647
     */
518
     */
648
    vfs_file_t *file = vfs_file_get(fd);
519
    vfs_file_t *file = vfs_file_get(fd);
649
    if (!file) {
520
    if (!file) {
650
        ipc_answer_0(rid, ENOENT);
521
        ipc_answer_0(rid, ENOENT);
651
        return;
522
        return;
652
    }
523
    }
653
 
524
 
654
    off_t newpos;
525
    off_t newpos;
655
    futex_down(&file->lock);
526
    futex_down(&file->lock);
656
    if (whence == SEEK_SET) {
527
    if (whence == SEEK_SET) {
657
        file->pos = off;
528
        file->pos = off;
658
        futex_up(&file->lock);
529
        futex_up(&file->lock);
659
        ipc_answer_1(rid, EOK, off);
530
        ipc_answer_1(rid, EOK, off);
660
        return;
531
        return;
661
    }
532
    }
662
    if (whence == SEEK_CUR) {
533
    if (whence == SEEK_CUR) {
663
        if (file->pos + off < file->pos) {
534
        if (file->pos + off < file->pos) {
664
            futex_up(&file->lock);
535
            futex_up(&file->lock);
665
            ipc_answer_0(rid, EOVERFLOW);
536
            ipc_answer_0(rid, EOVERFLOW);
666
            return;
537
            return;
667
        }
538
        }
668
        file->pos += off;
539
        file->pos += off;
669
        newpos = file->pos;
540
        newpos = file->pos;
670
        futex_up(&file->lock);
541
        futex_up(&file->lock);
671
        ipc_answer_1(rid, EOK, newpos);
542
        ipc_answer_1(rid, EOK, newpos);
672
        return;
543
        return;
673
    }
544
    }
674
    if (whence == SEEK_END) {
545
    if (whence == SEEK_END) {
675
        rwlock_read_lock(&file->node->contents_rwlock);
546
        rwlock_read_lock(&file->node->contents_rwlock);
676
        size_t size = file->node->size;
547
        size_t size = file->node->size;
677
        rwlock_read_unlock(&file->node->contents_rwlock);
548
        rwlock_read_unlock(&file->node->contents_rwlock);
678
        if (size + off < size) {
549
        if (size + off < size) {
679
            futex_up(&file->lock);
550
            futex_up(&file->lock);
680
            ipc_answer_0(rid, EOVERFLOW);
551
            ipc_answer_0(rid, EOVERFLOW);
681
            return;
552
            return;
682
        }
553
        }
683
        newpos = size + off;
554
        newpos = size + off;
684
        futex_up(&file->lock);
555
        futex_up(&file->lock);
685
        ipc_answer_1(rid, EOK, newpos);
556
        ipc_answer_1(rid, EOK, newpos);
686
        return;
557
        return;
687
    }
558
    }
688
    futex_up(&file->lock);
559
    futex_up(&file->lock);
689
    ipc_answer_0(rid, EINVAL);
560
    ipc_answer_0(rid, EINVAL);
690
}
561
}
691
 
562
 
692
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
563
void vfs_truncate(ipc_callid_t rid, ipc_call_t *request)
693
{
564
{
694
    int fd = IPC_GET_ARG1(*request);
565
    int fd = IPC_GET_ARG1(*request);
695
    size_t size = IPC_GET_ARG2(*request);
566
    size_t size = IPC_GET_ARG2(*request);
696
    ipcarg_t rc;
567
    ipcarg_t rc;
697
 
568
 
698
    vfs_file_t *file = vfs_file_get(fd);
569
    vfs_file_t *file = vfs_file_get(fd);
699
    if (!file) {
570
    if (!file) {
700
        ipc_answer_0(rid, ENOENT);
571
        ipc_answer_0(rid, ENOENT);
701
        return;
572
        return;
702
    }
573
    }
703
    futex_down(&file->lock);
574
    futex_down(&file->lock);
704
 
575
 
705
    rwlock_write_lock(&file->node->contents_rwlock);
576
    rwlock_write_lock(&file->node->contents_rwlock);
706
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
577
    int fs_phone = vfs_grab_phone(file->node->fs_handle);
707
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)file->node->dev_handle,
578
    rc = async_req_3_0(fs_phone, VFS_TRUNCATE, (ipcarg_t)file->node->dev_handle,
708
        (ipcarg_t)file->node->index, (ipcarg_t)size);
579
        (ipcarg_t)file->node->index, (ipcarg_t)size);
709
    vfs_release_phone(fs_phone);
580
    vfs_release_phone(fs_phone);
710
    if (rc == EOK)
581
    if (rc == EOK)
711
        file->node->size = size;
582
        file->node->size = size;
712
    rwlock_write_unlock(&file->node->contents_rwlock);
583
    rwlock_write_unlock(&file->node->contents_rwlock);
713
 
584
 
714
    futex_up(&file->lock);
585
    futex_up(&file->lock);
715
 
-
 
716
    return rc; 
-
 
717
}
-
 
718
 
-
 
719
atomic_t fs_head_futex = FUTEX_INITIALIZER;
-
 
720
link_t fs_head;
-
 
721
 
-
 
722
atomic_t fs_handle_next = {
-
 
723
    .count = 1
-
 
724
};
-
 
725
 
-
 
726
/** Verify the VFS info structure.
-
 
727
 *
-
 
728
 * @param info      Info structure to be verified.
-
 
729
 *
-
 
730
 * @return      Non-zero if the info structure is sane, zero otherwise.
-
 
731
 */
-
 
732
static bool vfs_info_sane(vfs_info_t *info)
-
 
733
{
-
 
734
    int i;
-
 
735
 
-
 
736
    /*
-
 
737
     * Check if the name is non-empty and is composed solely of ASCII
-
 
738
     * characters [a-z]+[a-z0-9_-]*.
-
 
739
     */
-
 
740
    if (!islower(info->name[0])) {
-
 
741
        dprintf("The name doesn't start with a lowercase character.\n");
-
 
742
        return false;
-
 
743
    }
-
 
744
    for (i = 1; i < FS_NAME_MAXLEN; i++) {
-
 
745
        if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
-
 
746
            (info->name[i] != '-') && (info->name[i] != '_')) {
-
 
747
            if (info->name[i] == '\0') {
-
 
748
                break;
-
 
749
            } else {
-
 
750
                dprintf("The name contains illegal "
-
 
751
                    "characters.\n");
-
 
752
                return false;
-
 
753
            }
-
 
754
        }
-
 
755
    }
-
 
756
    /*
-
 
757
     * This check is not redundant. It ensures that the name is
-
 
758
     * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
-
 
759
     */
-
 
760
    if (info->name[i] != '\0') {
-
 
761
        dprintf("The name is not properly NULL-terminated.\n");
-
 
762
        return false;
-
 
763
    }
-
 
764
   
-
 
765
 
-
 
766
    /*
-
 
767
     * Check if the FS implements mandatory VFS operations.
-
 
768
     */
-
 
769
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
-
 
770
        dprintf("Operation VFS_LOOKUP not defined by the client.\n");
-
 
771
        return false;
-
 
772
    }
-
 
773
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
-
 
774
        dprintf("Operation VFS_OPEN not defined by the client.\n");
-
 
775
        return false;
-
 
776
    }
-
 
777
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
-
 
778
        dprintf("Operation VFS_CLOSE not defined by the client.\n");
-
 
779
        return false;
-
 
780
    }
-
 
781
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
-
 
782
        dprintf("Operation VFS_READ not defined by the client.\n");
-
 
783
        return false;
-
 
784
    }
-
 
785
   
-
 
786
    /*
-
 
787
     * Check if each operation is either not defined, defined or default.
-
 
788
     */
-
 
789
    for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) {
-
 
790
        if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
-
 
791
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
-
 
792
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
-
 
793
            dprintf("Operation info not understood.\n");
-
 
794
            return false;
-
 
795
        }
-
 
796
    }
-
 
797
    return true;
-
 
798
}
-
 
799
 
-
 
800
/** VFS_REGISTER protocol function.
-
 
801
 *
-
 
802
 * @param rid       Hash of the call with the request.
-
 
803
 * @param request   Call structure with the request.
-
 
804
 */
-
 
805
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
-
 
806
{
-
 
807
    ipc_callid_t callid;
-
 
808
    ipc_call_t call;
-
 
809
    int rc;
-
 
810
    size_t size;
-
 
811
 
-
 
812
    dprintf("Processing VFS_REGISTER request received from %p.\n",
-
 
813
        request->in_phone_hash);
-
 
814
 
-
 
815
    /*
-
 
816
     * The first call has to be IPC_M_DATA_SEND in which we receive the
-
 
817
     * VFS info structure from the client FS.
-
 
818
     */
-
 
819
    if (!ipc_data_write_receive(&callid, &size)) {
-
 
820
        /*
-
 
821
         * The client doesn't obey the same protocol as we do.
-
 
822
         */
-
 
823
        dprintf("Receiving of VFS info failed.\n");
-
 
824
        ipc_answer_0(callid, EINVAL);
-
 
825
        ipc_answer_0(rid, EINVAL);
-
 
826
        return;
-
 
827
    }
-
 
828
   
-
 
829
    dprintf("VFS info received, size = %d\n", size);
-
 
830
   
-
 
831
    /*
-
 
832
     * We know the size of the VFS info structure. See if the client
-
 
833
     * understands this easy concept too.
-
 
834
     */
-
 
835
    if (size != sizeof(vfs_info_t)) {
-
 
836
        /*
-
 
837
         * The client is sending us something, which cannot be
-
 
838
         * the info structure.
-
 
839
         */
-
 
840
        dprintf("Received VFS info has bad size.\n");
-
 
841
        ipc_answer_0(callid, EINVAL);
-
 
842
        ipc_answer_0(rid, EINVAL);
-
 
843
        return;
-
 
844
    }
-
 
845
 
-
 
846
    /*
-
 
847
     * Allocate and initialize a buffer for the fs_info structure.
-
 
848
     */
-
 
849
    fs_info_t *fs_info;
-
 
850
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
-
 
851
    if (!fs_info) {
-
 
852
        dprintf("Could not allocate memory for FS info.\n");
-
 
853
        ipc_answer_0(callid, ENOMEM);
-
 
854
        ipc_answer_0(rid, ENOMEM);
-
 
855
        return;
-
 
856
    }
-
 
857
    link_initialize(&fs_info->fs_link);
-
 
858
    futex_initialize(&fs_info->phone_futex, 1);
-
 
859
       
-
 
860
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
-
 
861
    if (rc != EOK) {
-
 
862
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
-
 
863
            rc);
-
 
864
        free(fs_info);
-
 
865
        ipc_answer_0(callid, rc);
-
 
866
        ipc_answer_0(rid, rc);
586
    ipc_answer_0(rid, rc);
867
        return;
-
 
868
    }
-
 
869
 
-
 
870
    dprintf("VFS info delivered.\n");
-
 
871
       
-
 
872
    if (!vfs_info_sane(&fs_info->vfs_info)) {
-
 
873
        free(fs_info);
-
 
874
        ipc_answer_0(callid, EINVAL);
-
 
875
        ipc_answer_0(rid, EINVAL);
-
 
876
        return;
-
 
877
    }
-
 
878
       
-
 
879
    futex_down(&fs_head_futex);
-
 
880
 
-
 
881
    /*
-
 
882
     * Check for duplicit registrations.
-
 
883
     */
-
 
884
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
-
 
885
        /*
-
 
886
         * We already register a fs like this.
-
 
887
         */
-
 
888
        dprintf("FS is already registered.\n");
-
 
889
        futex_up(&fs_head_futex);
-
 
890
        free(fs_info);
-
 
891
        ipc_answer_0(callid, EEXISTS);
-
 
892
        ipc_answer_0(rid, EEXISTS);
-
 
893
        return;
-
 
894
    }
-
 
895
 
-
 
896
    /*
-
 
897
     * Add fs_info to the list of registered FS's.
-
 
898
     */
-
 
899
    dprintf("Inserting FS into the list of registered file systems.\n");
-
 
900
    list_append(&fs_info->fs_link, &fs_head);
-
 
901
 
-
 
902
    /*
-
 
903
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
-
 
904
     * that a callback connection is created and we have a phone through
-
 
905
     * which to forward VFS requests to it.
-
 
906
     */
-
 
907
    callid = async_get_call(&call);
-
 
908
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
-
 
909
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
-
 
910
        list_remove(&fs_info->fs_link);
-
 
911
        futex_up(&fs_head_futex);
-
 
912
        free(fs_info);
-
 
913
        ipc_answer_0(callid, EINVAL);
-
 
914
        ipc_answer_0(rid, EINVAL);
-
 
915
        return;
-
 
916
    }
-
 
917
    fs_info->phone = IPC_GET_ARG5(call);
-
 
918
    ipc_answer_0(callid, EOK);
-
 
919
 
-
 
920
    dprintf("Callback connection to FS created.\n");
-
 
921
 
-
 
922
    /*
-
 
923
     * The client will want us to send him the address space area with PLB.
-
 
924
     */
-
 
925
 
-
 
926
    if (!ipc_share_in_receive(&callid, &size)) {
-
 
927
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
-
 
928
        list_remove(&fs_info->fs_link);
-
 
929
        futex_up(&fs_head_futex);
-
 
930
        ipc_hangup(fs_info->phone);
-
 
931
        free(fs_info);
-
 
932
        ipc_answer_0(callid, EINVAL);
-
 
933
        ipc_answer_0(rid, EINVAL);
-
 
934
        return;
-
 
935
    }
-
 
936
   
-
 
937
    /*
-
 
938
     * We can only send the client address space area PLB_SIZE bytes long.
-
 
939
     */
-
 
940
    if (size != PLB_SIZE) {
-
 
941
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
-
 
942
        list_remove(&fs_info->fs_link);
-
 
943
        futex_up(&fs_head_futex);
-
 
944
        ipc_hangup(fs_info->phone);
-
 
945
        free(fs_info);
-
 
946
        ipc_answer_0(callid, EINVAL);
-
 
947
        ipc_answer_0(rid, EINVAL);
-
 
948
        return;
-
 
949
    }
-
 
950
 
-
 
951
    /*
-
 
952
     * Commit to read-only sharing the PLB with the client.
-
 
953
     */
-
 
954
    (void) ipc_share_in_finalize(callid, plb,
-
 
955
        AS_AREA_READ | AS_AREA_CACHEABLE);
-
 
956
 
-
 
957
    dprintf("Sharing PLB.\n");
-
 
958
 
-
 
959
    /*
-
 
960
     * That was it. The FS has been registered.
-
 
961
     * In reply to the VFS_REGISTER request, we assign the client file
-
 
962
     * system a global file system handle.
-
 
963
     */
-
 
964
    fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
-
 
965
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
-
 
966
   
-
 
967
    futex_up(&fs_head_futex);
-
 
968
   
-
 
969
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
-
 
970
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
-
 
971
}
-
 
972
 
-
 
973
/** For a given file system handle, implement policy for allocating a phone.
-
 
974
 *
-
 
975
 * @param handle    File system handle.
-
 
976
 *
-
 
977
 * @return      Phone over which a multi-call request can be safely
-
 
978
 *          sent. Return 0 if no phone was found.
-
 
979
 */
-
 
980
int vfs_grab_phone(int handle)
-
 
981
{
-
 
982
    /*
-
 
983
     * For now, we don't try to be very clever and very fast.
-
 
984
     * We simply lookup the phone in the fs_head list. We currently don't
-
 
985
     * open any additional phones (even though that itself would be pretty
-
 
986
     * straightforward; housekeeping multiple open phones to a FS task would
-
 
987
     * be more demanding). Instead, we simply take the respective
-
 
988
     * phone_futex and keep it until vfs_release_phone().
-
 
989
     */
-
 
990
    futex_down(&fs_head_futex);
-
 
991
    link_t *cur;
-
 
992
    fs_info_t *fs;
-
 
993
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
-
 
994
        fs = list_get_instance(cur, fs_info_t, fs_link);
-
 
995
        if (fs->fs_handle == handle) {
-
 
996
            futex_up(&fs_head_futex);
-
 
997
            /*
-
 
998
             * For now, take the futex unconditionally.
-
 
999
             * Oh yeah, serialization rocks.
-
 
1000
             * It will be up'ed in vfs_release_phone().
-
 
1001
             */
-
 
1002
            futex_down(&fs->phone_futex);
-
 
1003
            /*
-
 
1004
             * Avoid deadlock with other fibrils in the same thread
-
 
1005
             * by disabling fibril preemption.
-
 
1006
             */
-
 
1007
            fibril_inc_sercount();
-
 
1008
            return fs->phone;
-
 
1009
        }
-
 
1010
    }
-
 
1011
    futex_up(&fs_head_futex);
-
 
1012
    return 0;
-
 
1013
}
-
 
1014
 
-
 
1015
/** Tell VFS that the phone is in use for any request.
-
 
1016
 *
-
 
1017
 * @param phone     Phone to FS task.
-
 
1018
 */
-
 
1019
void vfs_release_phone(int phone)
-
 
1020
{
-
 
1021
    bool found = false;
-
 
1022
 
-
 
1023
    /*
-
 
1024
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
-
 
1025
     */
-
 
1026
    fibril_dec_sercount();
-
 
1027
   
-
 
1028
    futex_down(&fs_head_futex);
-
 
1029
    link_t *cur;
-
 
1030
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
-
 
1031
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
-
 
1032
        if (fs->phone == phone) {
-
 
1033
            found = true;
-
 
1034
            futex_up(&fs_head_futex);
-
 
1035
            futex_up(&fs->phone_futex);
-
 
1036
            return;
-
 
1037
        }
-
 
1038
    }
-
 
1039
    futex_up(&fs_head_futex);
-
 
1040
 
-
 
1041
    /*
-
 
1042
     * Not good to get here.
-
 
1043
     */
-
 
1044
    assert(found == true);
-
 
1045
}
-
 
1046
 
-
 
1047
/** Convert file system name to its handle.
-
 
1048
 *
-
 
1049
 * @param name      File system name.
-
 
1050
 * @param lock      If true, the function will down and up the
-
 
1051
 *          fs_head_futex.
-
 
1052
 *
-
 
1053
 * @return      File system handle or zero if file system not found.
-
 
1054
 */
-
 
1055
int fs_name_to_handle(char *name, bool lock)
-
 
1056
{
-
 
1057
    int handle = 0;
-
 
1058
   
-
 
1059
    if (lock)
-
 
1060
        futex_down(&fs_head_futex);
-
 
1061
    link_t *cur;
-
 
1062
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
-
 
1063
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
-
 
1064
        if (strncmp(fs->vfs_info.name, name,
-
 
1065
            sizeof(fs->vfs_info.name)) == 0) {
-
 
1066
            handle = fs->fs_handle;
-
 
1067
            break;
-
 
1068
        }
-
 
1069
    }
-
 
1070
    if (lock)
-
 
1071
        futex_up(&fs_head_futex);
-
 
1072
    return handle;
-
 
1073
}
587
}
1074
 
588
 
1075
/**
589
/**
1076
 * @}
590
 * @}
1077
 */
591
 */
1078
 
592