Subversion Repositories HelenOS

Rev

Rev 2536 | 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
/**
34
 * @file    vfs.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>
41
#include <errno.h>
2532 jermar 42
#include <stdio.h>
2523 jermar 43
#include <stdlib.h>
2526 jermar 44
#include <string.h>
45
#include <ctype.h>
2523 jermar 46
#include <bool.h>
2526 jermar 47
#include <futex.h>
2543 jermar 48
#include <as.h>
2526 jermar 49
#include <libadt/list.h>
2520 jermar 50
#include "vfs.h"
51
 
2526 jermar 52
atomic_t fs_head_futex = FUTEX_INITIALIZER;
53
link_t fs_head;
54
 
2523 jermar 55
/** Verify the VFS info structure.
56
 *
57
 * @param info      Info structure to be verified.
58
 *
59
 * @return      Non-zero if the info structure is sane, zero otherwise.
60
 */
2526 jermar 61
static bool vfs_info_sane(vfs_info_t *info)
2521 jermar 62
{
2526 jermar 63
    int i;
64
 
65
    /*
66
     * Check if the name is non-empty and is composed solely of ASCII
67
     * characters [a-z]+[a-z0-9_-]*.
68
     */
2532 jermar 69
    if (!islower(info->name[0])) {
70
        dprintf("The name doesn't start with a lowercase character.\n");
2526 jermar 71
        return false;
2532 jermar 72
    }
2526 jermar 73
    for (i = 1; i < FS_NAME_MAXLEN; i++) {
74
        if (!(islower(info->name[i]) || isdigit(info->name[i])) &&
75
            (info->name[i] != '-') && (info->name[i] != '_')) {
2532 jermar 76
            if (info->name[i] == '\0') {
2526 jermar 77
                break;
2532 jermar 78
            } else {
79
                dprintf("The name contains illegal "
80
                    "characters.\n");
2526 jermar 81
                return false;
2532 jermar 82
            }
2526 jermar 83
        }
84
    }
85
 
86
 
87
    /*
88
     * Check if the FS implements mandatory VFS operations.
89
     */
2532 jermar 90
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_REGISTER)] != VFS_OP_DEFINED) {
91
        dprintf("Operation VFS_REGISTER not defined by the client.\n");
2526 jermar 92
        return false;
2532 jermar 93
    }
94
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_MOUNT)] != VFS_OP_DEFINED) {
95
        dprintf("Operation VFS_MOUNT not defined by the client.\n");
2526 jermar 96
        return false;
2532 jermar 97
    }
98
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_UNMOUNT)] != VFS_OP_DEFINED) {
99
        dprintf("Operation VFS_UNMOUNT not defined by the client.\n");
2526 jermar 100
        return false;
2532 jermar 101
    }
102
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_LOOKUP)] != VFS_OP_DEFINED) {
103
        dprintf("Operation VFS_LOOKUP not defined by the client.\n");
2527 jermar 104
        return false;
2532 jermar 105
    }
106
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_OPEN)] != VFS_OP_DEFINED) {
107
        dprintf("Operation VFS_OPEN not defined by the client.\n");
2526 jermar 108
        return false;
2532 jermar 109
    }
110
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_CLOSE)] != VFS_OP_DEFINED) {
111
        dprintf("Operation VFS_CLOSE not defined by the client.\n");
2526 jermar 112
        return false;
2532 jermar 113
    }
114
    if (info->ops[IPC_METHOD_TO_VFS_OP(VFS_READ)] != VFS_OP_DEFINED) {
115
        dprintf("Operation VFS_READ not defined by the client.\n");
2526 jermar 116
        return false;
2532 jermar 117
    }
2526 jermar 118
 
119
    /*
120
     * Check if each operation is either not defined, defined or default.
121
     */
122
    for (i = VFS_FIRST; i < VFS_LAST; i++) {
2532 jermar 123
        if ((info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_NULL) &&
124
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFAULT) &&
125
            (info->ops[IPC_METHOD_TO_VFS_OP(i)] != VFS_OP_DEFINED)) {
126
            dprintf("Operation info not understood.\n");
127
            return false;
128
        }
2526 jermar 129
    }
130
    return true;
2523 jermar 131
}
132
 
133
/** VFS_REGISTER protocol function.
134
 *
135
 * @param rid       Hash of the call with the request.
136
 * @param request   Call structure with the request.
137
 */
2535 jermar 138
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
2523 jermar 139
{
2521 jermar 140
    ipc_callid_t callid;
141
    ipc_call_t call;
2523 jermar 142
    int rc;
143
    size_t size;
2521 jermar 144
 
2532 jermar 145
    dprintf("Processing VFS_REGISTER request received from %p.\n",
146
        request->in_phone_hash);
147
 
2523 jermar 148
    /*
149
     * The first call has to be IPC_M_DATA_SEND in which we receive the
150
     * VFS info structure from the client FS.
151
     */
2531 jermar 152
    if (!ipc_data_receive(&callid, &call, NULL, &size)) {
2521 jermar 153
        /*
154
         * The client doesn't obey the same protocol as we do.
2532 jermar 155
         */
156
        dprintf("Receiving of VFS info failed.\n");
2521 jermar 157
        ipc_answer_fast(callid, EINVAL, 0, 0);
2523 jermar 158
        ipc_answer_fast(rid, EINVAL, 0, 0);
2521 jermar 159
        return;
160
    }
2523 jermar 161
 
2532 jermar 162
    dprintf("VFS info received, size = %d\n", size);
163
 
2523 jermar 164
    /*
2526 jermar 165
     * We know the size of the VFS info structure. See if the client
166
     * understands this easy concept too.
2523 jermar 167
     */
168
    if (size != sizeof(vfs_info_t)) {
169
        /*
170
         * The client is sending us something, which cannot be
171
         * the info structure.
172
         */
2532 jermar 173
        dprintf("Received VFS info has bad size.\n");
2523 jermar 174
        ipc_answer_fast(callid, EINVAL, 0, 0);
175
        ipc_answer_fast(rid, EINVAL, 0, 0);
176
        return;
177
    }
178
 
179
    /*
2526 jermar 180
     * Allocate and initialize a buffer for the fs_info structure.
2523 jermar 181
     */
2543 jermar 182
    fs_info_t *fs_info;
2526 jermar 183
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
184
    if (!fs_info) {
2532 jermar 185
        dprintf("Could not allocate memory for FS info.\n");
2523 jermar 186
        ipc_answer_fast(callid, ENOMEM, 0, 0);
187
        ipc_answer_fast(rid, ENOMEM, 0, 0);
188
        return;
189
    }
2526 jermar 190
    link_initialize(&fs_info->fs_link);
2523 jermar 191
 
2531 jermar 192
    rc = ipc_data_deliver(callid, &call, &fs_info->vfs_info, size);
2532 jermar 193
    if (rc != EOK) {
194
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
195
            rc);
2526 jermar 196
        free(fs_info);
2523 jermar 197
        ipc_answer_fast(callid, rc, 0, 0);
198
        ipc_answer_fast(rid, rc, 0, 0);
199
        return;
200
    }
2532 jermar 201
 
202
    dprintf("VFS info delivered.\n");
2523 jermar 203
 
2526 jermar 204
    if (!vfs_info_sane(&fs_info->vfs_info)) {
205
        free(fs_info);
2523 jermar 206
        ipc_answer_fast(callid, EINVAL, 0, 0);
207
        ipc_answer_fast(rid, EINVAL, 0, 0);
208
        return;
209
    }
210
 
2526 jermar 211
    futex_down(&fs_head_futex);
212
 
213
    /*
214
     * Check for duplicit registrations.
215
     */
216
    link_t *cur;
217
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
218
        fs_info_t *fi = list_get_instance(cur, fs_info_t,
219
            fs_link);
220
        /* TODO: replace strcmp with strncmp once we have it */
221
        if (strcmp(fs_info->vfs_info.name, fi->vfs_info.name) == 0) {
222
            /*
223
             * We already register a fs like this.
224
             */
2532 jermar 225
            dprintf("FS is already registered.\n");
2526 jermar 226
            futex_up(&fs_head_futex);
227
            free(fs_info);
228
            ipc_answer_fast(callid, EEXISTS, 0, 0);
229
            ipc_answer_fast(rid, EEXISTS, 0, 0);
230
            return;
231
        }
232
    }
2527 jermar 233
 
234
    /*
235
     * Add fs_info to the list of registered FS's.
236
     */
2532 jermar 237
    dprintf("Adding FS into the registered list.\n");
2527 jermar 238
    list_append(&fs_info->fs_link, &fs_head);
239
 
240
    /*
241
     * ACK receiving a properly formatted, non-duplicit vfs_info.
242
     */
243
    ipc_answer_fast(callid, EOK, 0, 0);
2526 jermar 244
 
245
    /*
2527 jermar 246
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
247
     * that a callback connection is created and we have a phone through
248
     * which to forward VFS requests to it.
2526 jermar 249
     */
2527 jermar 250
    callid = async_get_call(&call);
251
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
2532 jermar 252
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
2527 jermar 253
        list_remove(&fs_info->fs_link);
254
        futex_up(&fs_head_futex);
255
        free(fs_info);
256
        ipc_answer_fast(callid, EINVAL, 0, 0);
257
        ipc_answer_fast(rid, EINVAL, 0, 0);
258
        return;
259
    }
260
    fs_info->phone = IPC_GET_ARG3(call);
261
    ipc_answer_fast(callid, EOK, 0, 0);
2526 jermar 262
 
2532 jermar 263
    dprintf("Callback connection to FS created.\n");
264
 
2543 jermar 265
    /*
266
     * The client will want us to send him the address space area with PLB.
267
     */
268
    callid = async_get_call(&call);
269
    if (IPC_GET_METHOD(call) != IPC_M_AS_AREA_RECV) {
270
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
271
        list_remove(&fs_info->fs_link);
272
        futex_up(&fs_head_futex);
273
        ipc_hangup(fs_info->phone);
274
        free(fs_info);
275
        ipc_answer_fast(callid, EINVAL, 0, 0);
276
        ipc_answer_fast(rid, EINVAL, 0, 0);
277
        return;
278
    }
279
 
280
    /*
281
     * We can only send the client address space area PLB_SIZE bytes long.
282
     */
283
    size = IPC_GET_ARG2(call);
284
    if (size != PLB_SIZE) {
285
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
286
        list_remove(&fs_info->fs_link);
287
        futex_up(&fs_head_futex);
288
        ipc_hangup(fs_info->phone);
289
        free(fs_info);
290
        ipc_answer_fast(callid, EINVAL, 0, 0);
291
        ipc_answer_fast(rid, EINVAL, 0, 0);
292
        return;
293
 
294
    }
295
 
296
    /*
297
     * Commit to read-only sharing the PLB with the client.
298
     */
299
    ipc_answer_fast(callid, EOK, (ipcarg_t) plb,
300
        (ipcarg_t) (AS_AREA_READ | AS_AREA_CACHEABLE));
301
 
302
    dprintf("Sharing PLB.\n");
303
 
2526 jermar 304
    futex_up(&fs_head_futex);
2527 jermar 305
 
306
    /*
307
     * That was it. The FS has been registered.
308
     */
309
    ipc_answer_fast(rid, EOK, 0, 0);
2532 jermar 310
    dprintf("\"%s\" filesystem successfully registered.\n",
311
        fs_info->vfs_info.name);
2536 jermar 312
 
2521 jermar 313
}
314
 
2518 jermar 315
/**
316
 * @}
317
 */