Subversion Repositories HelenOS

Rev

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

Rev 2600 Rev 2619
1
/*
1
/*
2
 * Copyright (c) 2007 Jakub Jermar
2
 * Copyright (c) 2007 Jakub Jermar
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup fs
29
/** @addtogroup fs
30
 * @{
30
 * @{
31
 */
31
 */
32
 
32
 
33
/**
33
/**
34
 * @file    vfs_register.c
34
 * @file    vfs_register.c
35
 * @brief   VFS_REGISTER method.
35
 * @brief   VFS_REGISTER method.
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 <as.h>
49
#include <as.h>
50
#include <libadt/list.h>
50
#include <libadt/list.h>
51
#include <assert.h>
51
#include <assert.h>
52
#include "vfs.h"
52
#include "vfs.h"
53
 
53
 
54
atomic_t fs_head_futex = FUTEX_INITIALIZER;
54
atomic_t fs_head_futex = FUTEX_INITIALIZER;
55
link_t fs_head;
55
link_t fs_head;
56
 
56
 
57
atomic_t fs_handle_next = {
57
atomic_t fs_handle_next = {
58
    .count = 1
58
    .count = 1
59
};
59
};
60
 
60
 
61
/** Verify the VFS info structure.
61
/** Verify the VFS info structure.
62
 *
62
 *
63
 * @param info      Info structure to be verified.
63
 * @param info      Info structure to be verified.
64
 *
64
 *
65
 * @return      Non-zero if the info structure is sane, zero otherwise.
65
 * @return      Non-zero if the info structure is sane, zero otherwise.
66
 */
66
 */
67
static bool vfs_info_sane(vfs_info_t *info)
67
static bool vfs_info_sane(vfs_info_t *info)
68
{
68
{
69
    int i;
69
    int i;
70
 
70
 
71
    /*
71
    /*
72
     * Check if the name is non-empty and is composed solely of ASCII
72
     * Check if the name is non-empty and is composed solely of ASCII
73
     * characters [a-z]+[a-z0-9_-]*.
73
     * characters [a-z]+[a-z0-9_-]*.
74
     */
74
     */
75
    if (!islower(info->name[0])) {
75
    if (!islower(info->name[0])) {
76
        dprintf("The name doesn't start with a lowercase character.\n");
76
        dprintf("The name doesn't start with a lowercase character.\n");
77
        return false;
77
        return false;
78
    }
78
    }
79
    for (i = 1; i < FS_NAME_MAXLEN; i++) {
79
    for (i = 1; i < FS_NAME_MAXLEN; i++) {
80
        if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
80
        if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
81
            (info->name[i] != '-') && (info->name[i] != '_')) {
81
            (info->name[i] != '-') && (info->name[i] != '_')) {
82
            if (info->name[i] == '\0') {
82
            if (info->name[i] == '\0') {
83
                break;
83
                break;
84
            } else {
84
            } else {
85
                dprintf("The name contains illegal "
85
                dprintf("The name contains illegal "
86
                    "characters.\n");
86
                    "characters.\n");
87
                return false;
87
                return false;
88
            }
88
            }
89
        }
89
        }
90
    }
90
    }
91
   
91
   
92
 
92
 
93
    /*
93
    /*
94
     * Check if the FS implements mandatory VFS operations.
94
     * Check if the FS implements mandatory VFS operations.
95
     */
95
     */
96
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) {
96
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) {
97
        dprintf("Operation VFS_REGISTER not defined by the client.\n");
97
        dprintf("Operation VFS_REGISTER not defined by the client.\n");
98
        return false;
98
        return false;
99
    }
99
    }
100
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) {
100
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) {
101
        dprintf("Operation VFS_MOUNT not defined by the client.\n");
101
        dprintf("Operation VFS_MOUNT not defined by the client.\n");
102
        return false;
102
        return false;
103
    }
103
    }
104
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) {
104
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) {
105
        dprintf("Operation VFS_UNMOUNT not defined by the client.\n");
105
        dprintf("Operation VFS_UNMOUNT not defined by the client.\n");
106
        return false;
106
        return false;
107
    }
107
    }
108
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
108
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
109
        dprintf("Operation VFS_LOOKUP not defined by the client.\n");
109
        dprintf("Operation VFS_LOOKUP not defined by the client.\n");
110
        return false;
110
        return false;
111
    }
111
    }
112
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
112
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
113
        dprintf("Operation VFS_OPEN not defined by the client.\n");
113
        dprintf("Operation VFS_OPEN not defined by the client.\n");
114
        return false;
114
        return false;
115
    }
115
    }
116
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
116
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
117
        dprintf("Operation VFS_CLOSE not defined by the client.\n");
117
        dprintf("Operation VFS_CLOSE not defined by the client.\n");
118
        return false;
118
        return false;
119
    }
119
    }
120
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
120
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
121
        dprintf("Operation VFS_READ not defined by the client.\n");
121
        dprintf("Operation VFS_READ not defined by the client.\n");
122
        return false;
122
        return false;
123
    }
123
    }
124
   
124
   
125
    /*
125
    /*
126
     * Check if each operation is either not defined, defined or default.
126
     * Check if each operation is either not defined, defined or default.
127
     */
127
     */
128
    for (i = VFS_FIRST; i < VFS_LAST; i++) {
128
    for (i = VFS_FIRST; i < VFS_LAST; i++) {
129
        if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
129
        if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
130
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
130
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
131
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
131
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
132
            dprintf("Operation info not understood.\n");
132
            dprintf("Operation info not understood.\n");
133
            return false;
133
            return false;
134
        }
134
        }
135
    }
135
    }
136
    return true;
136
    return true;
137
}
137
}
138
 
138
 
139
/** VFS_REGISTER protocol function.
139
/** VFS_REGISTER protocol function.
140
 *
140
 *
141
 * @param rid       Hash of the call with the request.
141
 * @param rid       Hash of the call with the request.
142
 * @param request   Call structure with the request.
142
 * @param request   Call structure with the request.
143
 */
143
 */
144
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
144
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
145
{
145
{
146
    ipc_callid_t callid;
146
    ipc_callid_t callid;
147
    ipc_call_t call;
147
    ipc_call_t call;
148
    int rc;
148
    int rc;
149
    size_t size;
149
    size_t size;
150
 
150
 
151
    dprintf("Processing VFS_REGISTER request received from %p.\n",
151
    dprintf("Processing VFS_REGISTER request received from %p.\n",
152
        request->in_phone_hash);
152
        request->in_phone_hash);
153
 
153
 
154
    /*
154
    /*
155
     * The first call has to be IPC_M_DATA_SEND in which we receive the
155
     * The first call has to be IPC_M_DATA_SEND in which we receive the
156
     * VFS info structure from the client FS.
156
     * VFS info structure from the client FS.
157
     */
157
     */
158
    if (!ipc_data_receive(&callid, &call, NULL, &size)) {
158
    if (!ipc_data_receive(&callid, NULL, &size)) {
159
        /*
159
        /*
160
         * The client doesn't obey the same protocol as we do.
160
         * The client doesn't obey the same protocol as we do.
161
         */
161
         */
162
        dprintf("Receiving of VFS info failed.\n");
162
        dprintf("Receiving of VFS info failed.\n");
163
        ipc_answer_fast_0(callid, EINVAL);
163
        ipc_answer_0(callid, EINVAL);
164
        ipc_answer_fast_0(rid, EINVAL);
164
        ipc_answer_0(rid, EINVAL);
165
        return;
165
        return;
166
    }
166
    }
167
   
167
   
168
    dprintf("VFS info received, size = %d\n", size);
168
    dprintf("VFS info received, size = %d\n", size);
169
   
169
   
170
    /*
170
    /*
171
     * We know the size of the VFS info structure. See if the client
171
     * We know the size of the VFS info structure. See if the client
172
     * understands this easy concept too.
172
     * understands this easy concept too.
173
     */
173
     */
174
    if (size != sizeof(vfs_info_t)) {
174
    if (size != sizeof(vfs_info_t)) {
175
        /*
175
        /*
176
         * The client is sending us something, which cannot be
176
         * The client is sending us something, which cannot be
177
         * the info structure.
177
         * the info structure.
178
         */
178
         */
179
        dprintf("Received VFS info has bad size.\n");
179
        dprintf("Received VFS info has bad size.\n");
180
        ipc_answer_fast_0(callid, EINVAL);
180
        ipc_answer_0(callid, EINVAL);
181
        ipc_answer_fast_0(rid, EINVAL);
181
        ipc_answer_0(rid, EINVAL);
182
        return;
182
        return;
183
    }
183
    }
184
 
184
 
185
    /*
185
    /*
186
     * Allocate and initialize a buffer for the fs_info structure.
186
     * Allocate and initialize a buffer for the fs_info structure.
187
     */
187
     */
188
    fs_info_t *fs_info;
188
    fs_info_t *fs_info;
189
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
189
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
190
    if (!fs_info) {
190
    if (!fs_info) {
191
        dprintf("Could not allocate memory for FS info.\n");
191
        dprintf("Could not allocate memory for FS info.\n");
192
        ipc_answer_fast_0(callid, ENOMEM);
192
        ipc_answer_0(callid, ENOMEM);
193
        ipc_answer_fast_0(rid, ENOMEM);
193
        ipc_answer_0(rid, ENOMEM);
194
        return;
194
        return;
195
    }
195
    }
196
    link_initialize(&fs_info->fs_link);
196
    link_initialize(&fs_info->fs_link);
197
       
197
       
198
    rc = ipc_data_deliver(callid, &call, &fs_info->vfs_info, size);
198
    rc = ipc_data_deliver(callid, &fs_info->vfs_info, size);
199
    if (rc != EOK) {
199
    if (rc != EOK) {
200
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
200
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
201
            rc);
201
            rc);
202
        free(fs_info);
202
        free(fs_info);
203
        ipc_answer_fast_0(callid, rc);
203
        ipc_answer_0(callid, rc);
204
        ipc_answer_fast_0(rid, rc);
204
        ipc_answer_0(rid, rc);
205
        return;
205
        return;
206
    }
206
    }
207
 
207
 
208
    dprintf("VFS info delivered.\n");
208
    dprintf("VFS info delivered.\n");
209
       
209
       
210
    if (!vfs_info_sane(&fs_info->vfs_info)) {
210
    if (!vfs_info_sane(&fs_info->vfs_info)) {
211
        free(fs_info);
211
        free(fs_info);
212
        ipc_answer_fast_0(callid, EINVAL);
212
        ipc_answer_0(callid, EINVAL);
213
        ipc_answer_fast_0(rid, EINVAL);
213
        ipc_answer_0(rid, EINVAL);
214
        return;
214
        return;
215
    }
215
    }
216
       
216
       
217
    futex_down(&fs_head_futex);
217
    futex_down(&fs_head_futex);
218
 
218
 
219
    /*
219
    /*
220
     * Check for duplicit registrations.
220
     * Check for duplicit registrations.
221
     */
221
     */
222
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
222
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
223
        /*
223
        /*
224
         * We already register a fs like this.
224
         * We already register a fs like this.
225
         */
225
         */
226
        dprintf("FS is already registered.\n");
226
        dprintf("FS is already registered.\n");
227
        futex_up(&fs_head_futex);
227
        futex_up(&fs_head_futex);
228
        free(fs_info);
228
        free(fs_info);
229
        ipc_answer_fast_0(callid, EEXISTS);
229
        ipc_answer_0(callid, EEXISTS);
230
        ipc_answer_fast_0(rid, EEXISTS);
230
        ipc_answer_0(rid, EEXISTS);
231
        return;
231
        return;
232
    }
232
    }
233
 
233
 
234
    /*
234
    /*
235
     * Add fs_info to the list of registered FS's.
235
     * Add fs_info to the list of registered FS's.
236
     */
236
     */
237
    dprintf("Inserting FS into the list of registered file systems.\n");
237
    dprintf("Inserting FS into the list of registered file systems.\n");
238
    list_append(&fs_info->fs_link, &fs_head);
238
    list_append(&fs_info->fs_link, &fs_head);
239
 
239
 
240
    /*
240
    /*
241
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
241
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
242
     * that a callback connection is created and we have a phone through
242
     * that a callback connection is created and we have a phone through
243
     * which to forward VFS requests to it.
243
     * which to forward VFS requests to it.
244
     */
244
     */
245
    callid = async_get_call(&call);
245
    callid = async_get_call(&call);
246
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
246
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
247
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
247
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
248
        list_remove(&fs_info->fs_link);
248
        list_remove(&fs_info->fs_link);
249
        futex_up(&fs_head_futex);
249
        futex_up(&fs_head_futex);
250
        free(fs_info);
250
        free(fs_info);
251
        ipc_answer_fast_0(callid, EINVAL);
251
        ipc_answer_0(callid, EINVAL);
252
        ipc_answer_fast_0(rid, EINVAL);
252
        ipc_answer_0(rid, EINVAL);
253
        return;
253
        return;
254
    }
254
    }
255
    fs_info->phone = IPC_GET_ARG3(call);
255
    fs_info->phone = IPC_GET_ARG3(call);
256
    ipc_answer_fast_0(callid, EOK);
256
    ipc_answer_0(callid, EOK);
257
 
257
 
258
    dprintf("Callback connection to FS created.\n");
258
    dprintf("Callback connection to FS created.\n");
259
 
259
 
260
    /*
260
    /*
261
     * The client will want us to send him the address space area with PLB.
261
     * The client will want us to send him the address space area with PLB.
262
     */
262
     */
263
    callid = async_get_call(&call);
263
    callid = async_get_call(&call);
264
    if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_RECV) {
264
    if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_RECV) {
265
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
265
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
266
        list_remove(&fs_info->fs_link);
266
        list_remove(&fs_info->fs_link);
267
        futex_up(&fs_head_futex);
267
        futex_up(&fs_head_futex);
268
        ipc_hangup(fs_info->phone);
268
        ipc_hangup(fs_info->phone);
269
        free(fs_info);
269
        free(fs_info);
270
        ipc_answer_fast_0(callid, EINVAL);
270
        ipc_answer_0(callid, EINVAL);
271
        ipc_answer_fast_0(rid, EINVAL);
271
        ipc_answer_0(rid, EINVAL);
272
        return;
272
        return;
273
    }
273
    }
274
   
274
   
275
    /*
275
    /*
276
     * We can only send the client address space area PLB_SIZE bytes long.
276
     * We can only send the client address space area PLB_SIZE bytes long.
277
     */
277
     */
278
    size = IPC_GET_ARG2(call);
278
    size = IPC_GET_ARG2(call);
279
    if (size != PLB_SIZE) {
279
    if (size != PLB_SIZE) {
280
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
280
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
281
        list_remove(&fs_info->fs_link);
281
        list_remove(&fs_info->fs_link);
282
        futex_up(&fs_head_futex);
282
        futex_up(&fs_head_futex);
283
        ipc_hangup(fs_info->phone);
283
        ipc_hangup(fs_info->phone);
284
        free(fs_info);
284
        free(fs_info);
285
        ipc_answer_fast_0(callid, EINVAL);
285
        ipc_answer_0(callid, EINVAL);
286
        ipc_answer_fast_0(rid, EINVAL);
286
        ipc_answer_0(rid, EINVAL);
287
        return;
287
        return;
288
    }
288
    }
289
 
289
 
290
    /*
290
    /*
291
     * Commit to read-only sharing the PLB with the client.
291
     * Commit to read-only sharing the PLB with the client.
292
     */
292
     */
293
    ipc_answer_fast(callid, EOK, (ipcarg_t) plb,
293
    ipc_answer_2(callid, EOK, (ipcarg_t) plb,
294
        (ipcarg_t) (AS_AREA_READ | AS_AREA_CACHEABLE));
294
        (ipcarg_t) (AS_AREA_READ | AS_AREA_CACHEABLE));
295
 
295
 
296
    dprintf("Sharing PLB.\n");
296
    dprintf("Sharing PLB.\n");
297
 
297
 
298
    /*
298
    /*
299
     * That was it. The FS has been registered.
299
     * That was it. The FS has been registered.
300
     * In reply to the VFS_REGISTER request, we assign the client file
300
     * In reply to the VFS_REGISTER request, we assign the client file
301
     * system a global file system handle.
301
     * system a global file system handle.
302
     */
302
     */
303
    fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
303
    fs_info->fs_handle = (int) atomic_postinc(&fs_handle_next);
304
    ipc_answer_fast_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
304
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
305
   
305
   
306
    futex_up(&fs_head_futex);
306
    futex_up(&fs_head_futex);
307
   
307
   
308
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
308
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
309
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
309
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
310
}
310
}
311
 
311
 
312
/** For a given file system handle, implement policy for allocating a phone.
312
/** For a given file system handle, implement policy for allocating a phone.
313
 *
313
 *
314
 * @param handle    File system handle.
314
 * @param handle    File system handle.
315
 *
315
 *
316
 * @return      Phone over which a multi-call request can be safely
316
 * @return      Phone over which a multi-call request can be safely
317
 *          sent. Return 0 if no phone was found.
317
 *          sent. Return 0 if no phone was found.
318
 */
318
 */
319
int vfs_grab_phone(int handle)
319
int vfs_grab_phone(int handle)
320
{
320
{
321
    /*
321
    /*
322
     * For now, we don't try to be very clever and very fast.
322
     * For now, we don't try to be very clever and very fast.
323
     * We simply lookup the phone in the fs_head list. We currently don't
323
     * We simply lookup the phone in the fs_head list. We currently don't
324
     * open any additional phones (even though that itself would be pretty
324
     * open any additional phones (even though that itself would be pretty
325
     * straightforward; housekeeping multiple open phones to a FS task would
325
     * straightforward; housekeeping multiple open phones to a FS task would
326
     * be more demanding). Instead, we simply take the respective
326
     * be more demanding). Instead, we simply take the respective
327
     * phone_futex and keep it until vfs_release_phone().
327
     * phone_futex and keep it until vfs_release_phone().
328
     */
328
     */
329
    futex_down(&fs_head_futex);
329
    futex_down(&fs_head_futex);
330
    link_t *cur;
330
    link_t *cur;
331
    fs_info_t *fs;
331
    fs_info_t *fs;
332
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
332
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
333
        fs = list_get_instance(cur, fs_info_t, fs_link);
333
        fs = list_get_instance(cur, fs_info_t, fs_link);
334
        if (fs->fs_handle == handle) {
334
        if (fs->fs_handle == handle) {
335
            futex_up(&fs_head_futex);
335
            futex_up(&fs_head_futex);
336
            /*
336
            /*
337
             * For now, take the futex unconditionally.
337
             * For now, take the futex unconditionally.
338
             * Oh yeah, serialization rocks.
338
             * Oh yeah, serialization rocks.
339
             * It will be up'ed in vfs_release_phone().
339
             * It will be up'ed in vfs_release_phone().
340
             */
340
             */
341
            futex_down(&fs->phone_futex);
341
            futex_down(&fs->phone_futex);
342
            /*
342
            /*
343
             * Avoid deadlock with other fibrils in the same thread
343
             * Avoid deadlock with other fibrils in the same thread
344
             * by disabling fibril preemption.
344
             * by disabling fibril preemption.
345
             */
345
             */
346
            fibril_inc_sercount();
346
            fibril_inc_sercount();
347
            return fs->phone;
347
            return fs->phone;
348
        }
348
        }
349
    }
349
    }
350
    futex_up(&fs_head_futex);
350
    futex_up(&fs_head_futex);
351
    return 0;
351
    return 0;
352
}
352
}
353
 
353
 
354
/** Tell VFS that the phone is in use for any request.
354
/** Tell VFS that the phone is in use for any request.
355
 *
355
 *
356
 * @param phone     Phone to FS task.
356
 * @param phone     Phone to FS task.
357
 */
357
 */
358
void vfs_release_phone(int phone)
358
void vfs_release_phone(int phone)
359
{
359
{
360
    bool found = false;
360
    bool found = false;
361
 
361
 
362
    /*
362
    /*
363
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
363
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
364
     */
364
     */
365
    fibril_dec_sercount();
365
    fibril_dec_sercount();
366
   
366
   
367
    futex_down(&fs_head_futex);
367
    futex_down(&fs_head_futex);
368
    link_t *cur;
368
    link_t *cur;
369
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
369
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
370
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
370
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
371
        if (fs->phone == phone) {
371
        if (fs->phone == phone) {
372
            found = true;
372
            found = true;
373
            futex_up(&fs_head_futex);
373
            futex_up(&fs_head_futex);
374
            futex_up(&fs->phone_futex);
374
            futex_up(&fs->phone_futex);
375
            return;
375
            return;
376
        }
376
        }
377
    }
377
    }
378
    futex_up(&fs_head_futex);
378
    futex_up(&fs_head_futex);
379
 
379
 
380
    /*
380
    /*
381
     * Not good to get here.
381
     * Not good to get here.
382
     */
382
     */
383
    assert(found == true);
383
    assert(found == true);
384
}
384
}
385
 
385
 
386
/** Convert file system name to its handle.
386
/** Convert file system name to its handle.
387
 *
387
 *
388
 * @param name      File system name.
388
 * @param name      File system name.
389
 * @param lock      If true, the function will down and up the
389
 * @param lock      If true, the function will down and up the
390
 *          fs_head_futex.
390
 *          fs_head_futex.
391
 *
391
 *
392
 * @return      File system handle or zero if file system not found.
392
 * @return      File system handle or zero if file system not found.
393
 */
393
 */
394
int fs_name_to_handle(char *name, bool lock)
394
int fs_name_to_handle(char *name, bool lock)
395
{
395
{
396
    int handle = 0;
396
    int handle = 0;
397
   
397
   
398
    if (lock)
398
    if (lock)
399
        futex_down(&fs_head_futex);
399
        futex_down(&fs_head_futex);
400
    link_t *cur;
400
    link_t *cur;
401
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
401
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
402
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
402
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
403
        if (strcmp(fs->vfs_info.name, name) == 0) { /* XXX: strncmp() */
403
        if (strcmp(fs->vfs_info.name, name) == 0) { /* XXX: strncmp() */
404
            handle = fs->fs_handle;
404
            handle = fs->fs_handle;
405
            break;
405
            break;
406
        }
406
        }
407
    }
407
    }
408
    if (lock)
408
    if (lock)
409
        futex_up(&fs_head_futex);
409
        futex_up(&fs_head_futex);
410
    return handle;
410
    return handle;
411
}
411
}
412
 
412
 
413
/**
413
/**
414
 * @}
414
 * @}
415
 */
415
 */
416
 
416