Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
4419 trochtova 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>
4537 trochtova 48
#include <fibril_sync.h>
49
#include <adt/list.h>
4419 trochtova 50
#include <as.h>
51
#include <assert.h>
52
#include <atomic.h>
53
#include "vfs.h"
54
 
4537 trochtova 55
FIBRIL_MUTEX_INITIALIZE(fs_head_lock);
4419 trochtova 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
    return true;
102
}
103
 
104
/** VFS_REGISTER protocol function.
105
 *
106
 * @param rid       Hash of the call with the request.
107
 * @param request   Call structure with the request.
108
 */
109
void vfs_register(ipc_callid_t rid, ipc_call_t *request)
110
{
111
    ipc_callid_t callid;
112
    ipc_call_t call;
113
    int rc;
114
    size_t size;
115
 
116
    dprintf("Processing VFS_REGISTER request received from %p.\n",
117
        request->in_phone_hash);
118
 
119
    /*
120
     * The first call has to be IPC_M_DATA_SEND in which we receive the
121
     * VFS info structure from the client FS.
122
     */
123
    if (!ipc_data_write_receive(&callid, &size)) {
124
        /*
125
         * The client doesn't obey the same protocol as we do.
126
         */
127
        dprintf("Receiving of VFS info failed.\n");
128
        ipc_answer_0(callid, EINVAL);
129
        ipc_answer_0(rid, EINVAL);
130
        return;
131
    }
132
 
133
    dprintf("VFS info received, size = %d\n", size);
134
 
135
    /*
136
     * We know the size of the VFS info structure. See if the client
137
     * understands this easy concept too.
138
     */
139
    if (size != sizeof(vfs_info_t)) {
140
        /*
141
         * The client is sending us something, which cannot be
142
         * the info structure.
143
         */
144
        dprintf("Received VFS info has bad size.\n");
145
        ipc_answer_0(callid, EINVAL);
146
        ipc_answer_0(rid, EINVAL);
147
        return;
148
    }
149
 
150
    /*
151
     * Allocate and initialize a buffer for the fs_info structure.
152
     */
153
    fs_info_t *fs_info;
154
    fs_info = (fs_info_t *) malloc(sizeof(fs_info_t));
155
    if (!fs_info) {
156
        dprintf("Could not allocate memory for FS info.\n");
157
        ipc_answer_0(callid, ENOMEM);
158
        ipc_answer_0(rid, ENOMEM);
159
        return;
160
    }
161
    link_initialize(&fs_info->fs_link);
4537 trochtova 162
    fibril_mutex_initialize(&fs_info->phone_lock);
4419 trochtova 163
 
164
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
165
    if (rc != EOK) {
166
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
167
            rc);
168
        free(fs_info);
169
        ipc_answer_0(callid, rc);
170
        ipc_answer_0(rid, rc);
171
        return;
172
    }
173
 
174
    dprintf("VFS info delivered.\n");
175
 
176
    if (!vfs_info_sane(&fs_info->vfs_info)) {
177
        free(fs_info);
178
        ipc_answer_0(callid, EINVAL);
179
        ipc_answer_0(rid, EINVAL);
180
        return;
181
    }
182
 
4537 trochtova 183
    fibril_mutex_lock(&fs_head_lock);
4419 trochtova 184
 
185
    /*
186
     * Check for duplicit registrations.
187
     */
188
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
189
        /*
190
         * We already register a fs like this.
191
         */
192
        dprintf("FS is already registered.\n");
4537 trochtova 193
        fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 194
        free(fs_info);
195
        ipc_answer_0(callid, EEXISTS);
196
        ipc_answer_0(rid, EEXISTS);
197
        return;
198
    }
199
 
200
    /*
201
     * Add fs_info to the list of registered FS's.
202
     */
203
    dprintf("Inserting FS into the list of registered file systems.\n");
204
    list_append(&fs_info->fs_link, &fs_head);
205
 
206
    /*
207
     * Now we want the client to send us the IPC_M_CONNECT_TO_ME call so
208
     * that a callback connection is created and we have a phone through
209
     * which to forward VFS requests to it.
210
     */
211
    callid = async_get_call(&call);
212
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
213
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
214
        list_remove(&fs_info->fs_link);
4537 trochtova 215
        fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 216
        free(fs_info);
217
        ipc_answer_0(callid, EINVAL);
218
        ipc_answer_0(rid, EINVAL);
219
        return;
220
    }
221
    fs_info->phone = IPC_GET_ARG5(call);
222
    ipc_answer_0(callid, EOK);
223
 
224
    dprintf("Callback connection to FS created.\n");
225
 
226
    /*
227
     * The client will want us to send him the address space area with PLB.
228
     */
229
 
230
    if (!ipc_share_in_receive(&callid, &size)) {
231
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
232
        list_remove(&fs_info->fs_link);
4537 trochtova 233
        fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 234
        ipc_hangup(fs_info->phone);
235
        free(fs_info);
236
        ipc_answer_0(callid, EINVAL);
237
        ipc_answer_0(rid, EINVAL);
238
        return;
239
    }
240
 
241
    /*
242
     * We can only send the client address space area PLB_SIZE bytes long.
243
     */
244
    if (size != PLB_SIZE) {
245
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
246
        list_remove(&fs_info->fs_link);
4537 trochtova 247
        fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 248
        ipc_hangup(fs_info->phone);
249
        free(fs_info);
250
        ipc_answer_0(callid, EINVAL);
251
        ipc_answer_0(rid, EINVAL);
252
        return;
253
    }
254
 
255
    /*
256
     * Commit to read-only sharing the PLB with the client.
257
     */
258
    (void) ipc_share_in_finalize(callid, plb,
259
        AS_AREA_READ | AS_AREA_CACHEABLE);
260
 
261
    dprintf("Sharing PLB.\n");
262
 
263
    /*
264
     * That was it. The FS has been registered.
265
     * In reply to the VFS_REGISTER request, we assign the client file
266
     * system a global file system handle.
267
     */
268
    fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
269
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
270
 
4537 trochtova 271
    fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 272
 
273
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
274
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
275
}
276
 
277
/** For a given file system handle, implement policy for allocating a phone.
278
 *
279
 * @param handle    File system handle.
280
 *
281
 * @return      Phone over which a multi-call request can be safely
282
 *          sent. Return 0 if no phone was found.
283
 */
284
int vfs_grab_phone(fs_handle_t handle)
285
{
286
    /*
287
     * For now, we don't try to be very clever and very fast.
288
     * We simply lookup the phone in the fs_head list. We currently don't
289
     * open any additional phones (even though that itself would be pretty
290
     * straightforward; housekeeping multiple open phones to a FS task would
291
     * be more demanding). Instead, we simply take the respective
292
     * phone_futex and keep it until vfs_release_phone().
293
     */
4537 trochtova 294
    fibril_mutex_lock(&fs_head_lock);
4419 trochtova 295
    link_t *cur;
296
    fs_info_t *fs;
297
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
298
        fs = list_get_instance(cur, fs_info_t, fs_link);
299
        if (fs->fs_handle == handle) {
4537 trochtova 300
            fibril_mutex_unlock(&fs_head_lock);
301
            fibril_mutex_lock(&fs->phone_lock);
302
            return fs->phone;
4419 trochtova 303
        }
304
    }
4537 trochtova 305
    fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 306
    return 0;
307
}
308
 
309
/** Tell VFS that the phone is in use for any request.
310
 *
311
 * @param phone     Phone to FS task.
312
 */
313
void vfs_release_phone(int phone)
314
{
315
    bool found = false;
316
 
4537 trochtova 317
    fibril_mutex_lock(&fs_head_lock);
4419 trochtova 318
    link_t *cur;
319
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
320
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
321
        if (fs->phone == phone) {
322
            found = true;
4537 trochtova 323
            fibril_mutex_unlock(&fs_head_lock);
324
            fibril_mutex_unlock(&fs->phone_lock);
4419 trochtova 325
            return;
326
        }
327
    }
4537 trochtova 328
    fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 329
 
330
    /*
331
     * Not good to get here.
332
     */
333
    assert(found == true);
334
}
335
 
336
/** Convert file system name to its handle.
337
 *
338
 * @param name      File system name.
4537 trochtova 339
 * @param lock      If true, the function will lock and unlock the
340
 *          fs_head_lock.
4419 trochtova 341
 *
342
 * @return      File system handle or zero if file system not found.
343
 */
344
fs_handle_t fs_name_to_handle(char *name, bool lock)
345
{
346
    int handle = 0;
347
 
348
    if (lock)
4537 trochtova 349
        fibril_mutex_lock(&fs_head_lock);
4419 trochtova 350
    link_t *cur;
351
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
352
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
353
        if (str_cmp(fs->vfs_info.name, name) == 0) {
354
            handle = fs->fs_handle;
355
            break;
356
        }
357
    }
358
    if (lock)
4537 trochtova 359
        fibril_mutex_unlock(&fs_head_lock);
4419 trochtova 360
    return handle;
361
}
362
 
363
/**
364
 * @}
365
 */