Subversion Repositories HelenOS

Rev

Rev 2698 | Rev 2768 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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