Subversion Repositories HelenOS

Rev

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

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