Subversion Repositories HelenOS

Rev

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

Rev 2698 Rev 2731
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_register.c
34
 * @file    vfs_register.c
35
 * @brief
35
 * @brief
36
 */
36
 */
37
 
37
 
38
#include <ipc/ipc.h>
38
#include <ipc/ipc.h>
39
#include <ipc/services.h>
39
#include <ipc/services.h>
40
#include <async.h>
40
#include <async.h>
41
#include <fibril.h>
41
#include <fibril.h>
42
#include <errno.h>
42
#include <errno.h>
43
#include <stdio.h>
43
#include <stdio.h>
44
#include <stdlib.h>
44
#include <stdlib.h>
45
#include <string.h>
45
#include <string.h>
46
#include <ctype.h>
46
#include <ctype.h>
47
#include <bool.h>
47
#include <bool.h>
48
#include <futex.h>
48
#include <futex.h>
49
#include <libadt/list.h>
49
#include <libadt/list.h>
50
#include <as.h>
50
#include <as.h>
51
#include <assert.h>
51
#include <assert.h>
52
#include <atomic.h>
52
#include <atomic.h>
53
#include "vfs.h"
53
#include "vfs.h"
54
 
54
 
55
atomic_t fs_head_futex = FUTEX_INITIALIZER;
55
atomic_t fs_head_futex = FUTEX_INITIALIZER;
56
link_t fs_head;
56
link_t fs_head;
57
 
57
 
58
atomic_t fs_handle_next = {
58
atomic_t fs_handle_next = {
59
    .count = 1
59
    .count = 1
60
};
60
};
61
 
61
 
62
/** Verify the VFS info structure.
62
/** Verify the VFS info structure.
63
 *
63
 *
64
 * @param info      Info structure to be verified.
64
 * @param info      Info structure to be verified.
65
 *
65
 *
66
 * @return      Non-zero if the info structure is sane, zero otherwise.
66
 * @return      Non-zero if the info structure is sane, zero otherwise.
67
 */
67
 */
68
static bool vfs_info_sane(vfs_info_t *info)
68
static bool vfs_info_sane(vfs_info_t *info)
69
{
69
{
70
    int i;
70
    int i;
71
 
71
 
72
    /*
72
    /*
73
     * Check if the name is non-empty and is composed solely of ASCII
73
     * Check if the name is non-empty and is composed solely of ASCII
74
     * characters [a-z]+[a-z0-9_-]*.
74
     * characters [a-z]+[a-z0-9_-]*.
75
     */
75
     */
76
    if (!islower(info->name[0])) {
76
    if (!islower(info->name[0])) {
77
        dprintf("The name doesn't start with a lowercase character.\n");
77
        dprintf("The name doesn't start with a lowercase character.\n");
78
        return false;
78
        return false;
79
    }
79
    }
80
    for (i = 1; i < FS_NAME_MAXLEN; i++) {
80
    for (i = 1; i < FS_NAME_MAXLEN; i++) {
81
        if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
81
        if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
82
            (info->name[i] != '-') && (info->name[i] != '_')) {
82
            (info->name[i] != '-') && (info->name[i] != '_')) {
83
            if (info->name[i] == '\0') {
83
            if (info->name[i] == '\0') {
84
                break;
84
                break;
85
            } else {
85
            } else {
86
                dprintf("The name contains illegal "
86
                dprintf("The name contains illegal "
87
                    "characters.\n");
87
                    "characters.\n");
88
                return false;
88
                return false;
89
            }
89
            }
90
        }
90
        }
91
    }
91
    }
92
    /*
92
    /*
93
     * This check is not redundant. It ensures that the name is
93
     * This check is not redundant. It ensures that the name is
94
     * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
94
     * NULL-terminated, even if FS_NAME_MAXLEN characters are used.
95
     */
95
     */
96
    if (info->name[i] != '\0') {
96
    if (info->name[i] != '\0') {
97
        dprintf("The name is not properly NULL-terminated.\n");
97
        dprintf("The name is not properly NULL-terminated.\n");
98
        return false;
98
        return false;
99
    }
99
    }
100
   
100
   
101
 
101
 
102
    /*
102
    /*
103
     * Check if the FS implements mandatory VFS operations.
103
     * Check if the FS implements mandatory VFS operations.
104
     */
104
     */
105
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
105
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
106
        dprintf("Operation VFS_LOOKUP not defined by the client.\n");
106
        dprintf("Operation VFS_LOOKUP not defined by the client.\n");
107
        return false;
107
        return false;
108
    }
108
    }
109
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
-
 
110
        dprintf("Operation VFS_OPEN not defined by the client.\n");
-
 
111
        return false;
-
 
112
    }
-
 
113
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
-
 
114
        dprintf("Operation VFS_CLOSE not defined by the client.\n");
-
 
115
        return false;
-
 
116
    }
-
 
117
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
109
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
118
        dprintf("Operation VFS_READ not defined by the client.\n");
110
        dprintf("Operation VFS_READ not defined by the client.\n");
119
        return false;
111
        return false;
120
    }
112
    }
121
   
113
   
122
    /*
114
    /*
123
     * Check if each operation is either not defined, defined or default.
115
     * Check if each operation is either not defined, defined or default.
124
     */
116
     */
125
    for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) {
117
    for (i = VFS_FIRST; i < VFS_LAST_CLNT; i++) {
126
        if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
118
        if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
127
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
119
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
128
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
120
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
129
            dprintf("Operation info not understood.\n");
121
            dprintf("Operation info not understood.\n");
130
            return false;
122
            return false;
131
        }
123
        }
132
    }
124
    }
133
    return true;
125
    return true;
134
}
126
}
135
 
127
 
136
/** VFS_REGISTER protocol function.
128
/** VFS_REGISTER protocol function.
137
 *
129
 *
138
 * @param rid       Hash of the call with the request.
130
 * @param rid       Hash of the call with the request.
139
 * @param request   Call structure with the request.
131
 * @param request   Call structure with the request.
140
 */
132
 */
141
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
133
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
142
{
134
{
143
    ipc_callid_t callid;
135
    ipc_callid_t callid;
144
    ipc_call_t call;
136
    ipc_call_t call;
145
    int rc;
137
    int rc;
146
    size_t size;
138
    size_t size;
147
 
139
 
148
    dprintf("Processing VFS_REGISTER request received from %p.\n",
140
    dprintf("Processing VFS_REGISTER request received from %p.\n",
149
        request->in_phone_hash);
141
        request->in_phone_hash);
150
 
142
 
151
    /*
143
    /*
152
     * The first call has to be IPC_M_DATA_SEND in which we receive the
144
     * The first call has to be IPC_M_DATA_SEND in which we receive the
153
     * VFS info structure from the client FS.
145
     * VFS info structure from the client FS.
154
     */
146
     */
155
    if (!ipc_data_write_receive(&callid, &size)) {
147
    if (!ipc_data_write_receive(&callid, &size)) {
156
        /*
148
        /*
157
         * The client doesn't obey the same protocol as we do.
149
         * The client doesn't obey the same protocol as we do.
158
         */
150
         */
159
        dprintf("Receiving of VFS info failed.\n");
151
        dprintf("Receiving of VFS info failed.\n");
160
        ipc_answer_0(callid, EINVAL);
152
        ipc_answer_0(callid, EINVAL);
161
        ipc_answer_0(rid, EINVAL);
153
        ipc_answer_0(rid, EINVAL);
162
        return;
154
        return;
163
    }
155
    }
164
   
156
   
165
    dprintf("VFS info received, size = %d\n", size);
157
    dprintf("VFS info received, size = %d\n", size);
166
   
158
   
167
    /*
159
    /*
168
     * We know the size of the VFS info structure. See if the client
160
     * We know the size of the VFS info structure. See if the client
169
     * understands this easy concept too.
161
     * understands this easy concept too.
170
     */
162
     */
171
    if (size != sizeof(vfs_info_t)) {
163
    if (size != sizeof(vfs_info_t)) {
172
        /*
164
        /*
173
         * The client is sending us something, which cannot be
165
         * The client is sending us something, which cannot be
174
         * the info structure.
166
         * the info structure.
175
         */
167
         */
176
        dprintf("Received VFS info has bad size.\n");
168
        dprintf("Received VFS info has bad size.\n");
177
        ipc_answer_0(callid, EINVAL);
169
        ipc_answer_0(callid, EINVAL);
178
        ipc_answer_0(rid, EINVAL);
170
        ipc_answer_0(rid, EINVAL);
179
        return;
171
        return;
180
    }
172
    }
181
 
173
 
182
    /*
174
    /*
183
     * Allocate and initialize a buffer for the fs_info structure.
175
     * Allocate and initialize a buffer for the fs_info structure.
184
     */
176
     */
185
    fs_info_t *fs_info;
177
    fs_info_t *fs_info;
186
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
178
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
187
    if (!fs_info) {
179
    if (!fs_info) {
188
        dprintf("Could not allocate memory for FS info.\n");
180
        dprintf("Could not allocate memory for FS info.\n");
189
        ipc_answer_0(callid, ENOMEM);
181
        ipc_answer_0(callid, ENOMEM);
190
        ipc_answer_0(rid, ENOMEM);
182
        ipc_answer_0(rid, ENOMEM);
191
        return;
183
        return;
192
    }
184
    }
193
    link_initialize(&fs_info->fs_link);
185
    link_initialize(&fs_info->fs_link);
194
    futex_initialize(&fs_info->phone_futex, 1);
186
    futex_initialize(&fs_info->phone_futex, 1);
195
       
187
       
196
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
188
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
197
    if (rc != EOK) {
189
    if (rc != EOK) {
198
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
190
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
199
            rc);
191
            rc);
200
        free(fs_info);
192
        free(fs_info);
201
        ipc_answer_0(callid, rc);
193
        ipc_answer_0(callid, rc);
202
        ipc_answer_0(rid, rc);
194
        ipc_answer_0(rid, rc);
203
        return;
195
        return;
204
    }
196
    }
205
 
197
 
206
    dprintf("VFS info delivered.\n");
198
    dprintf("VFS info delivered.\n");
207
       
199
       
208
    if (!vfs_info_sane(&fs_info->vfs_info)) {
200
    if (!vfs_info_sane(&fs_info->vfs_info)) {
209
        free(fs_info);
201
        free(fs_info);
210
        ipc_answer_0(callid, EINVAL);
202
        ipc_answer_0(callid, EINVAL);
211
        ipc_answer_0(rid, EINVAL);
203
        ipc_answer_0(rid, EINVAL);
212
        return;
204
        return;
213
    }
205
    }
214
       
206
       
215
    futex_down(&fs_head_futex);
207
    futex_down(&fs_head_futex);
216
 
208
 
217
    /*
209
    /*
218
     * Check for duplicit registrations.
210
     * Check for duplicit registrations.
219
     */
211
     */
220
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
212
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
221
        /*
213
        /*
222
         * We already register a fs like this.
214
         * We already register a fs like this.
223
         */
215
         */
224
        dprintf("FS is already registered.\n");
216
        dprintf("FS is already registered.\n");
225
        futex_up(&fs_head_futex);
217
        futex_up(&fs_head_futex);
226
        free(fs_info);
218
        free(fs_info);
227
        ipc_answer_0(callid, EEXISTS);
219
        ipc_answer_0(callid, EEXISTS);
228
        ipc_answer_0(rid, EEXISTS);
220
        ipc_answer_0(rid, EEXISTS);
229
        return;
221
        return;
230
    }
222
    }
231
 
223
 
232
    /*
224
    /*
233
     * Add fs_info to the list of registered FS's.
225
     * Add fs_info to the list of registered FS's.
234
     */
226
     */
235
    dprintf("Inserting FS into the list of registered file systems.\n");
227
    dprintf("Inserting FS into the list of registered file systems.\n");
236
    list_append(&fs_info->fs_link, &fs_head);
228
    list_append(&fs_info->fs_link, &fs_head);
237
 
229
 
238
    /*
230
    /*
239
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
231
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
240
     * that a callback connection is created and we have a phone through
232
     * that a callback connection is created and we have a phone through
241
     * which to forward VFS requests to it.
233
     * which to forward VFS requests to it.
242
     */
234
     */
243
    callid = async_get_call(&call);
235
    callid = async_get_call(&call);
244
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
236
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
245
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
237
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
246
        list_remove(&fs_info->fs_link);
238
        list_remove(&fs_info->fs_link);
247
        futex_up(&fs_head_futex);
239
        futex_up(&fs_head_futex);
248
        free(fs_info);
240
        free(fs_info);
249
        ipc_answer_0(callid, EINVAL);
241
        ipc_answer_0(callid, EINVAL);
250
        ipc_answer_0(rid, EINVAL);
242
        ipc_answer_0(rid, EINVAL);
251
        return;
243
        return;
252
    }
244
    }
253
    fs_info->phone = IPC_GET_ARG5(call);
245
    fs_info->phone = IPC_GET_ARG5(call);
254
    ipc_answer_0(callid, EOK);
246
    ipc_answer_0(callid, EOK);
255
 
247
 
256
    dprintf("Callback connection to FS created.\n");
248
    dprintf("Callback connection to FS created.\n");
257
 
249
 
258
    /*
250
    /*
259
     * The client will want us to send him the address space area with PLB.
251
     * The client will want us to send him the address space area with PLB.
260
     */
252
     */
261
 
253
 
262
    if (!ipc_share_in_receive(&callid, &size)) {
254
    if (!ipc_share_in_receive(&callid, &size)) {
263
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
255
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
264
        list_remove(&fs_info->fs_link);
256
        list_remove(&fs_info->fs_link);
265
        futex_up(&fs_head_futex);
257
        futex_up(&fs_head_futex);
266
        ipc_hangup(fs_info->phone);
258
        ipc_hangup(fs_info->phone);
267
        free(fs_info);
259
        free(fs_info);
268
        ipc_answer_0(callid, EINVAL);
260
        ipc_answer_0(callid, EINVAL);
269
        ipc_answer_0(rid, EINVAL);
261
        ipc_answer_0(rid, EINVAL);
270
        return;
262
        return;
271
    }
263
    }
272
   
264
   
273
    /*
265
    /*
274
     * We can only send the client address space area PLB_SIZE bytes long.
266
     * We can only send the client address space area PLB_SIZE bytes long.
275
     */
267
     */
276
    if (size != PLB_SIZE) {
268
    if (size != PLB_SIZE) {
277
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
269
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
278
        list_remove(&fs_info->fs_link);
270
        list_remove(&fs_info->fs_link);
279
        futex_up(&fs_head_futex);
271
        futex_up(&fs_head_futex);
280
        ipc_hangup(fs_info->phone);
272
        ipc_hangup(fs_info->phone);
281
        free(fs_info);
273
        free(fs_info);
282
        ipc_answer_0(callid, EINVAL);
274
        ipc_answer_0(callid, EINVAL);
283
        ipc_answer_0(rid, EINVAL);
275
        ipc_answer_0(rid, EINVAL);
284
        return;
276
        return;
285
    }
277
    }
286
 
278
 
287
    /*
279
    /*
288
     * Commit to read-only sharing the PLB with the client.
280
     * Commit to read-only sharing the PLB with the client.
289
     */
281
     */
290
    (void) ipc_share_in_finalize(callid, plb,
282
    (void) ipc_share_in_finalize(callid, plb,
291
        AS_AREA_READ | AS_AREA_CACHEABLE);
283
        AS_AREA_READ | AS_AREA_CACHEABLE);
292
 
284
 
293
    dprintf("Sharing PLB.\n");
285
    dprintf("Sharing PLB.\n");
294
 
286
 
295
    /*
287
    /*
296
     * That was it. The FS has been registered.
288
     * That was it. The FS has been registered.
297
     * In reply to the VFS_REGISTER request, we assign the client file
289
     * In reply to the VFS_REGISTER request, we assign the client file
298
     * system a global file system handle.
290
     * system a global file system handle.
299
     */
291
     */
300
    fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
292
    fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
301
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
293
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
302
   
294
   
303
    futex_up(&fs_head_futex);
295
    futex_up(&fs_head_futex);
304
   
296
   
305
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
297
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
306
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
298
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
307
}
299
}
308
 
300
 
309
/** For a given file system handle, implement policy for allocating a phone.
301
/** For a given file system handle, implement policy for allocating a phone.
310
 *
302
 *
311
 * @param handle    File system handle.
303
 * @param handle    File system handle.
312
 *
304
 *
313
 * @return      Phone over which a multi-call request can be safely
305
 * @return      Phone over which a multi-call request can be safely
314
 *          sent. Return 0 if no phone was found.
306
 *          sent. Return 0 if no phone was found.
315
 */
307
 */
316
int vfs_grab_phone(int handle)
308
int vfs_grab_phone(int handle)
317
{
309
{
318
    /*
310
    /*
319
     * For now, we don't try to be very clever and very fast.
311
     * For now, we don't try to be very clever and very fast.
320
     * We simply lookup the phone in the fs_head list. We currently don't
312
     * We simply lookup the phone in the fs_head list. We currently don't
321
     * open any additional phones (even though that itself would be pretty
313
     * open any additional phones (even though that itself would be pretty
322
     * straightforward; housekeeping multiple open phones to a FS task would
314
     * straightforward; housekeeping multiple open phones to a FS task would
323
     * be more demanding). Instead, we simply take the respective
315
     * be more demanding). Instead, we simply take the respective
324
     * phone_futex and keep it until vfs_release_phone().
316
     * phone_futex and keep it until vfs_release_phone().
325
     */
317
     */
326
    futex_down(&fs_head_futex);
318
    futex_down(&fs_head_futex);
327
    link_t *cur;
319
    link_t *cur;
328
    fs_info_t *fs;
320
    fs_info_t *fs;
329
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
321
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
330
        fs = list_get_instance(cur, fs_info_t, fs_link);
322
        fs = list_get_instance(cur, fs_info_t, fs_link);
331
        if (fs->fs_handle == handle) {
323
        if (fs->fs_handle == handle) {
332
            futex_up(&fs_head_futex);
324
            futex_up(&fs_head_futex);
333
            /*
325
            /*
334
             * For now, take the futex unconditionally.
326
             * For now, take the futex unconditionally.
335
             * Oh yeah, serialization rocks.
327
             * Oh yeah, serialization rocks.
336
             * It will be up'ed in vfs_release_phone().
328
             * It will be up'ed in vfs_release_phone().
337
             */
329
             */
338
            futex_down(&fs->phone_futex);
330
            futex_down(&fs->phone_futex);
339
            /*
331
            /*
340
             * Avoid deadlock with other fibrils in the same thread
332
             * Avoid deadlock with other fibrils in the same thread
341
             * by disabling fibril preemption.
333
             * by disabling fibril preemption.
342
             */
334
             */
343
            fibril_inc_sercount();
335
            fibril_inc_sercount();
344
            return fs->phone;
336
            return fs->phone;
345
        }
337
        }
346
    }
338
    }
347
    futex_up(&fs_head_futex);
339
    futex_up(&fs_head_futex);
348
    return 0;
340
    return 0;
349
}
341
}
350
 
342
 
351
/** Tell VFS that the phone is in use for any request.
343
/** Tell VFS that the phone is in use for any request.
352
 *
344
 *
353
 * @param phone     Phone to FS task.
345
 * @param phone     Phone to FS task.
354
 */
346
 */
355
void vfs_release_phone(int phone)
347
void vfs_release_phone(int phone)
356
{
348
{
357
    bool found = false;
349
    bool found = false;
358
 
350
 
359
    /*
351
    /*
360
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
352
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
361
     */
353
     */
362
    fibril_dec_sercount();
354
    fibril_dec_sercount();
363
   
355
   
364
    futex_down(&fs_head_futex);
356
    futex_down(&fs_head_futex);
365
    link_t *cur;
357
    link_t *cur;
366
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
358
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
367
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
359
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
368
        if (fs->phone == phone) {
360
        if (fs->phone == phone) {
369
            found = true;
361
            found = true;
370
            futex_up(&fs_head_futex);
362
            futex_up(&fs_head_futex);
371
            futex_up(&fs->phone_futex);
363
            futex_up(&fs->phone_futex);
372
            return;
364
            return;
373
        }
365
        }
374
    }
366
    }
375
    futex_up(&fs_head_futex);
367
    futex_up(&fs_head_futex);
376
 
368
 
377
    /*
369
    /*
378
     * Not good to get here.
370
     * Not good to get here.
379
     */
371
     */
380
    assert(found == true);
372
    assert(found == true);
381
}
373
}
382
 
374
 
383
/** Convert file system name to its handle.
375
/** Convert file system name to its handle.
384
 *
376
 *
385
 * @param name      File system name.
377
 * @param name      File system name.
386
 * @param lock      If true, the function will down and up the
378
 * @param lock      If true, the function will down and up the
387
 *          fs_head_futex.
379
 *          fs_head_futex.
388
 *
380
 *
389
 * @return      File system handle or zero if file system not found.
381
 * @return      File system handle or zero if file system not found.
390
 */
382
 */
391
int fs_name_to_handle(char *name, bool lock)
383
int fs_name_to_handle(char *name, bool lock)
392
{
384
{
393
    int handle = 0;
385
    int handle = 0;
394
   
386
   
395
    if (lock)
387
    if (lock)
396
        futex_down(&fs_head_futex);
388
        futex_down(&fs_head_futex);
397
    link_t *cur;
389
    link_t *cur;
398
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
390
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
399
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
391
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
400
        if (strncmp(fs->vfs_info.name, name,
392
        if (strncmp(fs->vfs_info.name, name,
401
            sizeof(fs->vfs_info.name)) == 0) {
393
            sizeof(fs->vfs_info.name)) == 0) {
402
            handle = fs->fs_handle;
394
            handle = fs->fs_handle;
403
            break;
395
            break;
404
        }
396
        }
405
    }
397
    }
406
    if (lock)
398
    if (lock)
407
        futex_up(&fs_head_futex);
399
        futex_up(&fs_head_futex);
408
    return handle;
400
    return handle;
409
}
401
}
410
 
402
 
411
/**
403
/**
412
 * @}
404
 * @}
413
 */
405
 */
414
 
406