Subversion Repositories HelenOS

Rev

Rev 3150 | Rev 3588 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3150 Rev 3153
Line 48... Line 48...
48
#include <assert.h>
48
#include <assert.h>
49
#include <sys/types.h>
49
#include <sys/types.h>
50
#include <libadt/hash_table.h>
50
#include <libadt/hash_table.h>
51
#include <as.h>
51
#include <as.h>
52
#include <libfs.h>
52
#include <libfs.h>
53
#include <ipc/services.h>
-
 
54
#include <ipc/devmap.h>
-
 
55
#include <sys/mman.h>
-
 
56
#include <byteorder.h>
-
 
57
 
53
 
58
#define min(a, b)       ((a) < (b) ? (a) : (b))
54
#define min(a, b)       ((a) < (b) ? (a) : (b))
59
#define max(a, b)       ((a) > (b) ? (a) : (b))
55
#define max(a, b)       ((a) > (b) ? (a) : (b))
60
 
56
 
61
#define DENTRIES_BUCKETS    256
57
#define DENTRIES_BUCKETS    256
62
 
58
 
63
#define NAMES_BUCKETS       4
59
#define NAMES_BUCKETS       4
64
 
60
 
65
#define BLOCK_SIZE          1024    // FIXME
-
 
66
#define RD_BASE             1024    // FIXME
-
 
67
#define RD_READ_BLOCK   (RD_BASE + 1)
-
 
68
 
-
 
69
/*
61
/*
70
 * For now, we don't distinguish between different dev_handles/instances. All
62
 * For now, we don't distinguish between different dev_handles/instances. All
71
 * requests resolve to the only instance, rooted in the following variable.
63
 * requests resolve to the only instance, rooted in the following variable.
72
 */
64
 */
73
static tmpfs_dentry_t *root;
65
static tmpfs_dentry_t *root;
Line 146... Line 138...
146
};
138
};
147
 
139
 
148
/** Hash table of all directory entries. */
140
/** Hash table of all directory entries. */
149
hash_table_t dentries;
141
hash_table_t dentries;
150
 
142
 
151
struct rdentry {
-
 
152
    uint8_t type;
-
 
153
    uint32_t len;
-
 
154
} __attribute__((packed));
-
 
155
 
-
 
156
/* Implementation of hash table interface for the dentries hash table. */
143
/* Implementation of hash table interface for the dentries hash table. */
157
static hash_index_t dentries_hash(unsigned long *key)
144
static hash_index_t dentries_hash(unsigned long *key)
158
{
145
{
159
    return *key % DENTRIES_BUCKETS;
146
    return *key % DENTRIES_BUCKETS;
160
}
147
}
Line 248... Line 235...
248
    }
235
    }
249
    root->lnkcnt = 1;
236
    root->lnkcnt = 1;
250
    return true;
237
    return true;
251
}
238
}
252
 
239
 
253
static bool tmpfs_blockread(int phone, void *buffer, size_t *bufpos, size_t *buflen, size_t *pos, void *dst, size_t size)
-
 
254
{
-
 
255
    size_t offset = 0;
-
 
256
    size_t left = size;
-
 
257
   
-
 
258
    while (left > 0) {
-
 
259
        size_t rd;
-
 
260
       
-
 
261
        if (*bufpos + left < *buflen)
-
 
262
            rd = left;
-
 
263
        else
-
 
264
            rd = *buflen - *bufpos;
-
 
265
       
-
 
266
        if (rd > 0) {
-
 
267
            memcpy(dst + offset, buffer + *bufpos, rd);
-
 
268
            offset += rd;
-
 
269
            *bufpos += rd;
-
 
270
            *pos += rd;
-
 
271
            left -= rd;
-
 
272
        }
-
 
273
       
-
 
274
        if (*bufpos == *buflen) {
-
 
275
            int retval;
-
 
276
            int rc = ipc_call_sync_2_1(phone, RD_READ_BLOCK, *pos / BLOCK_SIZE, BLOCK_SIZE, (sysarg_t *) &retval);
-
 
277
            if ((rc != EOK) || (retval != EOK))
-
 
278
                return false;
-
 
279
           
-
 
280
            *bufpos = 0;
-
 
281
            *buflen = BLOCK_SIZE;
-
 
282
        }
-
 
283
    }
-
 
284
   
-
 
285
    return true;
-
 
286
}
-
 
287
 
-
 
288
static bool tmpfs_restore_recursion(int phone, void *block, size_t *bufpos, size_t *buflen, size_t *pos, tmpfs_dentry_t *parent)
-
 
289
{
-
 
290
    struct rdentry entry;
-
 
291
   
-
 
292
    do {
-
 
293
        char *fname;
-
 
294
        tmpfs_dentry_t *node;
-
 
295
        uint32_t size;
-
 
296
       
-
 
297
        if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &entry, sizeof(entry)))
-
 
298
            return false;
-
 
299
       
-
 
300
        entry.len = uint32_t_le2host(entry.len);
-
 
301
       
-
 
302
        switch (entry.type) {
-
 
303
        case 0:
-
 
304
            break;
-
 
305
        case 1:
-
 
306
            fname = malloc(entry.len + 1);
-
 
307
            if (fname == NULL)
-
 
308
                return false;
-
 
309
           
-
 
310
            node = (tmpfs_dentry_t *) tmpfs_create_node(L_FILE);
-
 
311
            if (node == NULL) {
-
 
312
                free(fname);
-
 
313
                return false;
-
 
314
            }
-
 
315
           
-
 
316
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) {
-
 
317
                tmpfs_destroy_node((void *) node);
-
 
318
                free(fname);
-
 
319
                return false;
-
 
320
            }
-
 
321
            fname[entry.len] = 0;
-
 
322
           
-
 
323
            if (!tmpfs_link_node((void *) parent, (void *) node, fname)) {
-
 
324
                tmpfs_destroy_node((void *) node);
-
 
325
                free(fname);
-
 
326
                return false;
-
 
327
            }
-
 
328
            free(fname);
-
 
329
           
-
 
330
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, &size, sizeof(size)))
-
 
331
                return false;
-
 
332
           
-
 
333
            size = uint32_t_le2host(size);
-
 
334
           
-
 
335
            node->data = malloc(size);
-
 
336
            if (node->data == NULL)
-
 
337
                return false;
-
 
338
           
-
 
339
            node->size = size;
-
 
340
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, node->data, size))
-
 
341
                return false;
-
 
342
           
-
 
343
            break;
-
 
344
        case 2:
-
 
345
            fname = malloc(entry.len + 1);
-
 
346
            if (fname == NULL)
-
 
347
                return false;
-
 
348
           
-
 
349
            node = (tmpfs_dentry_t *) tmpfs_create_node(L_DIRECTORY);
-
 
350
            if (node == NULL) {
-
 
351
                free(fname);
-
 
352
                return false;
-
 
353
            }
-
 
354
           
-
 
355
            if (!tmpfs_blockread(phone, block, bufpos, buflen, pos, fname, entry.len)) {
-
 
356
                tmpfs_destroy_node((void *) node);
-
 
357
                free(fname);
-
 
358
                return false;
-
 
359
            }
-
 
360
            fname[entry.len] = 0;
-
 
361
           
-
 
362
            if (!tmpfs_link_node((void *) parent, (void *) node, fname)) {
-
 
363
                tmpfs_destroy_node((void *) node);
-
 
364
                free(fname);
-
 
365
                return false;
-
 
366
            }
-
 
367
            free(fname);
-
 
368
           
-
 
369
            if (!tmpfs_restore_recursion(phone, block, bufpos, buflen, pos, node))
-
 
370
                return false;
-
 
371
           
-
 
372
            break;
-
 
373
        default:
-
 
374
            return false;
-
 
375
        }
-
 
376
    } while (entry.type != 0);
-
 
377
   
-
 
378
    return true;
-
 
379
}
-
 
380
 
-
 
381
static bool tmpfs_restore(dev_handle_t dev)
-
 
382
{
-
 
383
    void *block = mmap(NULL, BLOCK_SIZE,
-
 
384
        PROTO_READ | PROTO_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-
 
385
   
-
 
386
    if (block == NULL)
-
 
387
        return false;
-
 
388
   
-
 
389
    int phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, DEVMAP_CONNECT_TO_DEVICE, dev);
-
 
390
 
-
 
391
    if (phone < 0) {
-
 
392
        munmap(block, BLOCK_SIZE);
-
 
393
        return false;
-
 
394
    }
-
 
395
   
-
 
396
    if (ipc_share_out_start(phone, block, AS_AREA_READ | AS_AREA_WRITE) != EOK)
-
 
397
        goto error;
-
 
398
   
-
 
399
    size_t bufpos = 0;
-
 
400
    size_t buflen = 0;
-
 
401
    size_t pos = 0;
-
 
402
   
-
 
403
    char tag[6];
-
 
404
    if (!tmpfs_blockread(phone, block, &bufpos, &buflen, &pos, tag, 5))
-
 
405
        goto error;
-
 
406
   
-
 
407
    tag[5] = 0;
-
 
408
    if (strcmp(tag, "TMPFS") != 0)
-
 
409
        goto error;
-
 
410
   
-
 
411
    if (!tmpfs_restore_recursion(phone, block, &bufpos, &buflen, &pos, root))
-
 
412
        goto error;
-
 
413
       
-
 
414
    ipc_hangup(phone);
-
 
415
    munmap(block, BLOCK_SIZE);
-
 
416
    return true;
-
 
417
   
-
 
418
error:
-
 
419
    ipc_hangup(phone);
-
 
420
    munmap(block, BLOCK_SIZE);
-
 
421
    return false;
-
 
422
}
-
 
423
 
-
 
424
/** Compare one component of path to a directory entry.
240
/** Compare one component of path to a directory entry.
425
 *
241
 *
426
 * @param parentp   Pointer to node from which we descended.
242
 * @param parentp   Pointer to node from which we descended.
427
 * @param childp    Pointer to node to compare the path component with.
243
 * @param childp    Pointer to node to compare the path component with.
428
 * @param component Array of characters holding component name.
244
 * @param component Array of characters holding component name.
Line 575... Line 391...
575
        free(dentry->data);
391
        free(dentry->data);
576
    free(dentry);
392
    free(dentry);
577
    return EOK;
393
    return EOK;
578
}
394
}
579
 
395
 
580
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
396
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
581
{
397
{
582
    dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
398
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
583
    fs_index_t mr_index = (fs_index_t) IPC_GET_ARG2(*request);
-
 
584
    fs_handle_t mp_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
-
 
585
    dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
-
 
586
    fs_index_t mp_index = (fs_index_t) IPC_GET_ARG5(*request);
-
 
587
   
-
 
588
    if ((mr_index == root->index) &&
-
 
589
        (mp_fs_handle == tmpfs_reg.fs_handle) &&
-
 
590
        (mp_index == mr_index)) {
-
 
591
       
-
 
592
        if (mr_dev_handle >= 0) {
-
 
593
            if (tmpfs_restore(mr_dev_handle))
-
 
594
                ipc_answer_0(rid, EOK);
-
 
595
            else
-
 
596
                ipc_answer_0(rid, ELIMIT);
-
 
597
        } else
-
 
598
            ipc_answer_0(rid, EOK);
-
 
599
    } else
-
 
600
        ipc_answer_0(rid, ENOTSUP);
-
 
601
}
-
 
602
 
399
 
603
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
-
 
604
{
-
 
605
    /* Initialize TMPFS. */
400
    /* Initialize TMPFS. */
606
    if (!root && !tmpfs_init()) {
401
    if (!root && !tmpfs_init()) {
607
        ipc_answer_0(rid, ENOMEM);
402
        ipc_answer_0(rid, ENOMEM);
608
        return;
403
        return;
609
    }
404
    }
-
 
405
 
-
 
406
    if (dev_handle >= 0) {
-
 
407
        if (tmpfs_restore(dev_handle))
-
 
408
            ipc_answer_0(rid, EOK);
-
 
409
        else
-
 
410
            ipc_answer_0(rid, ELIMIT);
-
 
411
    } else {
-
 
412
            ipc_answer_0(rid, EOK);
-
 
413
    }
-
 
414
}
-
 
415
 
-
 
416
void tmpfs_mount(ipc_callid_t rid, ipc_call_t *request)
-
 
417
{
-
 
418
    dev_handle_t mp_dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
-
 
419
    fs_index_t mp_index = (fs_index_t) IPC_GET_ARG2(*request);
-
 
420
    fs_handle_t mr_fs_handle = (fs_handle_t) IPC_GET_ARG3(*request);
-
 
421
    dev_handle_t mr_dev_handle = (dev_handle_t) IPC_GET_ARG4(*request);
-
 
422
   
-
 
423
    ipc_answer_0(rid, ENOTSUP);
-
 
424
}
-
 
425
 
-
 
426
void tmpfs_lookup(ipc_callid_t rid, ipc_call_t *request)
-
 
427
{
610
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
428
    libfs_lookup(&tmpfs_libfs_ops, tmpfs_reg.fs_handle, rid, request);
611
}
429
}
612
 
430
 
613
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
431
void tmpfs_read(ipc_callid_t rid, ipc_call_t *request)
614
{
432
{