Subversion Repositories HelenOS

Rev

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