Subversion Repositories HelenOS

Rev

Rev 4377 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4377 Rev 4692
Line 43... Line 43...
43
#include <stdio.h>
43
#include <stdio.h>
44
#include <stdlib.h>
44
#include <stdlib.h>
45
#include <string.h>
45
#include <string.h>
46
#include <ctype.h>
46
#include <ctype.h>
47
#include <bool.h>
47
#include <bool.h>
48
#include <futex.h>
48
#include <fibril_sync.h>
49
#include <libadt/list.h>
49
#include <adt/list.h>
50
#include <as.h>
50
#include <as.h>
51
#include <assert.h>
51
#include <assert.h>
52
#include <atomic.h>
52
#include <atomic.h>
53
#include "vfs.h"
53
#include "vfs.h"
54
 
54
 
-
 
55
FIBRIL_CONDVAR_INITIALIZE(fs_head_cv);
55
atomic_t fs_head_futex = FUTEX_INITIALIZER;
56
FIBRIL_MUTEX_INITIALIZE(fs_head_lock);
56
link_t fs_head;
57
LIST_INITIALIZE(fs_head);
57
 
58
 
58
atomic_t fs_handle_next = {
59
atomic_t fs_handle_next = {
59
    .count = 1
60
    .count = 1
60
};
61
};
61
 
62
 
Line 157... Line 158...
157
        ipc_answer_0(callid, ENOMEM);
158
        ipc_answer_0(callid, ENOMEM);
158
        ipc_answer_0(rid, ENOMEM);
159
        ipc_answer_0(rid, ENOMEM);
159
        return;
160
        return;
160
    }
161
    }
161
    link_initialize(&fs_info->fs_link);
162
    link_initialize(&fs_info->fs_link);
162
    futex_initialize(&fs_info->phone_futex, 1);
163
    fibril_mutex_initialize(&fs_info->phone_lock);
163
       
164
       
164
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
165
    rc = ipc_data_write_finalize(callid, &fs_info->vfs_info, size);
165
    if (rc != EOK) {
166
    if (rc != EOK) {
166
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
167
        dprintf("Failed to deliver the VFS info into our AS, rc=%d.\n",
167
            rc);
168
            rc);
Line 178... Line 179...
178
        ipc_answer_0(callid, EINVAL);
179
        ipc_answer_0(callid, EINVAL);
179
        ipc_answer_0(rid, EINVAL);
180
        ipc_answer_0(rid, EINVAL);
180
        return;
181
        return;
181
    }
182
    }
182
       
183
       
183
    futex_down(&fs_head_futex);
184
    fibril_mutex_lock(&fs_head_lock);
184
    fibril_inc_sercount();
-
 
185
 
185
 
186
    /*
186
    /*
187
     * Check for duplicit registrations.
187
     * Check for duplicit registrations.
188
     */
188
     */
189
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
189
    if (fs_name_to_handle(fs_info->vfs_info.name, false)) {
190
        /*
190
        /*
191
         * We already register a fs like this.
191
         * We already register a fs like this.
192
         */
192
         */
193
        dprintf("FS is already registered.\n");
193
        dprintf("FS is already registered.\n");
194
        fibril_dec_sercount();
-
 
195
        futex_up(&fs_head_futex);
194
        fibril_mutex_unlock(&fs_head_lock);
196
        free(fs_info);
195
        free(fs_info);
197
        ipc_answer_0(callid, EEXISTS);
196
        ipc_answer_0(callid, EEXISTS);
198
        ipc_answer_0(rid, EEXISTS);
197
        ipc_answer_0(rid, EEXISTS);
199
        return;
198
        return;
200
    }
199
    }
Line 212... Line 211...
212
     */
211
     */
213
    callid = async_get_call(&call);
212
    callid = async_get_call(&call);
214
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
213
    if (IPC_GET_METHOD(call) != IPC_M_CONNECT_TO_ME) {
215
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
214
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
216
        list_remove(&fs_info->fs_link);
215
        list_remove(&fs_info->fs_link);
217
        fibril_dec_sercount();
-
 
218
        futex_up(&fs_head_futex);
216
        fibril_mutex_unlock(&fs_head_lock);
219
        free(fs_info);
217
        free(fs_info);
220
        ipc_answer_0(callid, EINVAL);
218
        ipc_answer_0(callid, EINVAL);
221
        ipc_answer_0(rid, EINVAL);
219
        ipc_answer_0(rid, EINVAL);
222
        return;
220
        return;
223
    }
221
    }
Line 231... Line 229...
231
     */
229
     */
232
 
230
 
233
    if (!ipc_share_in_receive(&callid, &size)) {
231
    if (!ipc_share_in_receive(&callid, &size)) {
234
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
232
        dprintf("Unexpected call, method = %d\n", IPC_GET_METHOD(call));
235
        list_remove(&fs_info->fs_link);
233
        list_remove(&fs_info->fs_link);
236
        fibril_dec_sercount();
-
 
237
        futex_up(&fs_head_futex);
234
        fibril_mutex_unlock(&fs_head_lock);
238
        ipc_hangup(fs_info->phone);
235
        ipc_hangup(fs_info->phone);
239
        free(fs_info);
236
        free(fs_info);
240
        ipc_answer_0(callid, EINVAL);
237
        ipc_answer_0(callid, EINVAL);
241
        ipc_answer_0(rid, EINVAL);
238
        ipc_answer_0(rid, EINVAL);
242
        return;
239
        return;
Line 246... Line 243...
246
     * We can only send the client address space area PLB_SIZE bytes long.
243
     * We can only send the client address space area PLB_SIZE bytes long.
247
     */
244
     */
248
    if (size != PLB_SIZE) {
245
    if (size != PLB_SIZE) {
249
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
246
        dprintf("Client suggests wrong size of PFB, size = %d\n", size);
250
        list_remove(&fs_info->fs_link);
247
        list_remove(&fs_info->fs_link);
251
        fibril_dec_sercount();
-
 
252
        futex_up(&fs_head_futex);
248
        fibril_mutex_unlock(&fs_head_lock);
253
        ipc_hangup(fs_info->phone);
249
        ipc_hangup(fs_info->phone);
254
        free(fs_info);
250
        free(fs_info);
255
        ipc_answer_0(callid, EINVAL);
251
        ipc_answer_0(callid, EINVAL);
256
        ipc_answer_0(rid, EINVAL);
252
        ipc_answer_0(rid, EINVAL);
257
        return;
253
        return;
Line 271... Line 267...
271
     * system a global file system handle.
267
     * system a global file system handle.
272
     */
268
     */
273
    fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
269
    fs_info->fs_handle = (fs_handle_t) atomic_postinc(&fs_handle_next);
274
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
270
    ipc_answer_1(rid, EOK, (ipcarg_t) fs_info->fs_handle);
275
   
271
   
276
    fibril_dec_sercount();
272
    fibril_condvar_broadcast(&fs_head_cv);
277
    futex_up(&fs_head_futex);
273
    fibril_mutex_unlock(&fs_head_lock);
278
   
274
   
279
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
275
    dprintf("\"%.*s\" filesystem successfully registered, handle=%d.\n",
280
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
276
        FS_NAME_MAXLEN, fs_info->vfs_info.name, fs_info->fs_handle);
281
   
-
 
282
    /* Process pending mount requests possibly waiting
-
 
283
     * for this filesystem implementation.
-
 
284
     */
-
 
285
    vfs_process_pending_mount();
-
 
286
}
277
}
287
 
278
 
288
/** For a given file system handle, implement policy for allocating a phone.
279
/** For a given file system handle, implement policy for allocating a phone.
289
 *
280
 *
290
 * @param handle    File system handle.
281
 * @param handle    File system handle.
Line 292... Line 283...
292
 * @return      Phone over which a multi-call request can be safely
283
 * @return      Phone over which a multi-call request can be safely
293
 *          sent. Return 0 if no phone was found.
284
 *          sent. Return 0 if no phone was found.
294
 */
285
 */
295
int vfs_grab_phone(fs_handle_t handle)
286
int vfs_grab_phone(fs_handle_t handle)
296
{
287
{
-
 
288
    int phone;
-
 
289
 
297
    /*
290
    /*
298
     * For now, we don't try to be very clever and very fast.
291
     * For now, we don't try to be very clever and very fast.  We simply
299
     * We simply lookup the phone in the fs_head list. We currently don't
292
     * lookup the phone in the fs_head list and duplicate it.  The duplicate
300
     * open any additional phones (even though that itself would be pretty
293
     * phone will be returned to the client and the client will use it for
301
     * straightforward; housekeeping multiple open phones to a FS task would
-
 
302
     * be more demanding). Instead, we simply take the respective
294
     * communication.  In the future, we should cache the connections so
303
     * phone_futex and keep it until vfs_release_phone().
295
     * that they do not have to be reestablished over and over again.
304
     */
296
     */
305
    futex_down(&fs_head_futex);
297
    fibril_mutex_lock(&fs_head_lock);
306
    link_t *cur;
298
    link_t *cur;
307
    fs_info_t *fs;
299
    fs_info_t *fs;
308
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
300
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
309
        fs = list_get_instance(cur, fs_info_t, fs_link);
301
        fs = list_get_instance(cur, fs_info_t, fs_link);
310
        if (fs->fs_handle == handle) {
302
        if (fs->fs_handle == handle) {
311
            futex_up(&fs_head_futex);
303
            fibril_mutex_unlock(&fs_head_lock);
312
            /*
-
 
313
             * For now, take the futex unconditionally.
-
 
314
             * Oh yeah, serialization rocks.
304
            fibril_mutex_lock(&fs->phone_lock);
315
             * It will be up'ed in vfs_release_phone().
305
            phone = ipc_connect_me_to(fs->phone, 0, 0, 0);
316
             */
-
 
317
            futex_down(&fs->phone_futex);
306
            fibril_mutex_unlock(&fs->phone_lock);
318
            /*
307
 
319
             * Avoid deadlock with other fibrils in the same thread
-
 
320
             * by disabling fibril preemption.
-
 
321
             */
-
 
322
            fibril_inc_sercount();
308
            assert(phone > 0);
323
            return fs->phone;
309
            return phone;
324
        }
310
        }
325
    }
311
    }
326
    futex_up(&fs_head_futex);
312
    fibril_mutex_unlock(&fs_head_lock);
327
    return 0;
313
    return 0;
328
}
314
}
329
 
315
 
330
/** Tell VFS that the phone is in use for any request.
316
/** Tell VFS that the phone is not needed anymore.
331
 *
317
 *
332
 * @param phone     Phone to FS task.
318
 * @param phone     Phone to FS task.
333
 */
319
 */
334
void vfs_release_phone(int phone)
320
void vfs_release_phone(int phone)
335
{
321
{
336
    bool found = false;
-
 
337
 
-
 
338
    /*
-
 
339
     * Undo the fibril_inc_sercount() done in vfs_grab_phone().
322
    /* TODO: implement connection caching */
340
     */
-
 
341
    fibril_dec_sercount();
-
 
342
   
-
 
343
    futex_down(&fs_head_futex);
-
 
344
    link_t *cur;
-
 
345
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
-
 
346
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
-
 
347
        if (fs->phone == phone) {
323
    ipc_hangup(phone);
348
            found = true;
-
 
349
            futex_up(&fs_head_futex);
-
 
350
            futex_up(&fs->phone_futex);
-
 
351
            return;
-
 
352
        }
-
 
353
    }
-
 
354
    futex_up(&fs_head_futex);
-
 
355
 
-
 
356
    /*
-
 
357
     * Not good to get here.
-
 
358
     */
-
 
359
    assert(found == true);
-
 
360
}
324
}
361
 
325
 
362
/** Convert file system name to its handle.
326
/** Convert file system name to its handle.
363
 *
327
 *
364
 * @param name      File system name.
328
 * @param name      File system name.
365
 * @param lock      If true, the function will down and up the
329
 * @param lock      If true, the function will lock and unlock the
366
 *          fs_head_futex.
330
 *          fs_head_lock.
367
 *
331
 *
368
 * @return      File system handle or zero if file system not found.
332
 * @return      File system handle or zero if file system not found.
369
 */
333
 */
370
fs_handle_t fs_name_to_handle(char *name, bool lock)
334
fs_handle_t fs_name_to_handle(char *name, bool lock)
371
{
335
{
372
    int handle = 0;
336
    int handle = 0;
373
   
337
   
374
    if (lock)
338
    if (lock)
375
        futex_down(&fs_head_futex);
339
        fibril_mutex_lock(&fs_head_lock);
376
    link_t *cur;
340
    link_t *cur;
377
    for (cur = fs_head.next; cur != &fs_head; cur = cur->next) {
341
    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);
342
        fs_info_t *fs = list_get_instance(cur, fs_info_t, fs_link);
379
        if (str_cmp(fs->vfs_info.name, name) == 0) {
343
        if (str_cmp(fs->vfs_info.name, name) == 0) {
380
            handle = fs->fs_handle;
344
            handle = fs->fs_handle;
381
            break;
345
            break;
382
        }
346
        }
383
    }
347
    }
384
    if (lock)
348
    if (lock)
385
        futex_up(&fs_head_futex);
349
        fibril_mutex_unlock(&fs_head_lock);
386
    return handle;
350
    return handle;
387
}
351
}
388
 
352
 
389
/**
353
/**
390
 * @}
354
 * @}