Subversion Repositories HelenOS

Rev

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

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