Subversion Repositories HelenOS

Rev

Rev 2637 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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