Subversion Repositories HelenOS

Rev

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