Subversion Repositories HelenOS

Rev

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

Rev 3674 Rev 4348
Line 56... Line 56...
56
 
56
 
57
#define DENTRIES_BUCKETS    256
57
#define DENTRIES_BUCKETS    256
58
 
58
 
59
#define NAMES_BUCKETS       4
59
#define NAMES_BUCKETS       4
60
 
60
 
61
/*
-
 
62
 * For now, we don't distinguish between different dev_handles/instances. All
61
/** All root nodes have index 0. */
-
 
62
#define TMPFS_SOME_ROOT     0
63
 * requests resolve to the only instance, rooted in the following variable.
63
/** Global counter for assigning node indices. Shared by all instances. */
64
 */
-
 
65
static tmpfs_dentry_t *root;
64
fs_index_t tmpfs_next_index = 1;
66
 
-
 
67
#define TMPFS_DEV       0   /**< Dummy device handle for TMPFS */
-
 
68
 
65
 
69
/*
66
/*
70
 * Implementation of the libfs interface.
67
 * Implementation of the libfs interface.
71
 */
68
 */
72
 
69
 
Line 100... Line 97...
100
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
97
    return ((tmpfs_dentry_t *) nodep)->child != NULL;
101
}
98
}
102
 
99
 
103
static void *tmpfs_root_get(dev_handle_t dev_handle)
100
static void *tmpfs_root_get(dev_handle_t dev_handle)
104
{
101
{
105
    return root;
102
    return tmpfs_node_get(dev_handle, TMPFS_SOME_ROOT);
106
}
103
}
107
 
104
 
108
static char tmpfs_plb_get_char(unsigned pos)
105
static char tmpfs_plb_get_char(unsigned pos)
109
{
106
{
110
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
107
    return tmpfs_reg.plb_ro[pos % PLB_SIZE];
Line 140... Line 137...
140
};
137
};
141
 
138
 
142
/** Hash table of all directory entries. */
139
/** Hash table of all directory entries. */
143
hash_table_t dentries;
140
hash_table_t dentries;
144
 
141
 
-
 
142
#define DENTRIES_KEY_INDEX  0
-
 
143
#define DENTRIES_KEY_DEV    1
-
 
144
 
145
/* Implementation of hash table interface for the dentries hash table. */
145
/* Implementation of hash table interface for the dentries hash table. */
146
static hash_index_t dentries_hash(unsigned long *key)
146
static hash_index_t dentries_hash(unsigned long key[])
147
{
147
{
148
    return *key % DENTRIES_BUCKETS;
148
    return key[DENTRIES_KEY_INDEX] % DENTRIES_BUCKETS;
149
}
149
}
150
 
150
 
151
static int dentries_compare(unsigned long *key, hash_count_t keys,
151
static int dentries_compare(unsigned long key[], hash_count_t keys,
152
    link_t *item)
152
    link_t *item)
153
{
153
{
154
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
154
    tmpfs_dentry_t *dentry = hash_table_get_instance(item, tmpfs_dentry_t,
155
        dh_link);
155
        dh_link);
156
    return dentry->index == *key;
156
    return (dentry->index == key[DENTRIES_KEY_INDEX] &&
-
 
157
        dentry->dev_handle == key[DENTRIES_KEY_DEV]);
157
}
158
}
158
 
159
 
159
static void dentries_remove_callback(link_t *item)
160
static void dentries_remove_callback(link_t *item)
160
{
161
{
161
}
162
}
Line 165... Line 166...
165
    .hash = dentries_hash,
166
    .hash = dentries_hash,
166
    .compare = dentries_compare,
167
    .compare = dentries_compare,
167
    .remove_callback = dentries_remove_callback
168
    .remove_callback = dentries_remove_callback
168
};
169
};
169
 
170
 
170
fs_index_t tmpfs_next_index = 1;
-
 
171
 
-
 
172
typedef struct {
171
typedef struct {
173
    char *name;
172
    char *name;
174
    tmpfs_dentry_t *parent;
173
    tmpfs_dentry_t *parent;
175
    link_t link;
174
    link_t link;
176
} tmpfs_name_t;
175
} tmpfs_name_t;
Line 213... Line 212...
213
}
212
}
214
 
213
 
215
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
214
static bool tmpfs_dentry_initialize(tmpfs_dentry_t *dentry)
216
{
215
{
217
    dentry->index = 0;
216
    dentry->index = 0;
-
 
217
    dentry->dev_handle = 0;
218
    dentry->sibling = NULL;
218
    dentry->sibling = NULL;
219
    dentry->child = NULL;
219
    dentry->child = NULL;
220
    dentry->type = TMPFS_NONE;
220
    dentry->type = TMPFS_NONE;
221
    dentry->lnkcnt = 0;
221
    dentry->lnkcnt = 0;
222
    dentry->size = 0;
222
    dentry->size = 0;
Line 224... Line 224...
224
    link_initialize(&dentry->dh_link);
224
    link_initialize(&dentry->dh_link);
225
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
225
    return (bool)hash_table_create(&dentry->names, NAMES_BUCKETS, 1,
226
        &names_ops);
226
        &names_ops);
227
}
227
}
228
 
228
 
229
static bool tmpfs_init(void)
229
bool tmpfs_init(void)
230
{
230
{
231
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 1, &dentries_ops))
231
    if (!hash_table_create(&dentries, DENTRIES_BUCKETS, 2, &dentries_ops))
232
        return false;
232
        return false;
-
 
233
   
-
 
234
    return true;
-
 
235
}
-
 
236
 
-
 
237
static bool tmpfs_instance_init(dev_handle_t dev_handle)
-
 
238
{
-
 
239
    tmpfs_dentry_t *root;
-
 
240
   
233
    root = (tmpfs_dentry_t *) tmpfs_create_node(TMPFS_DEV, L_DIRECTORY);
241
    root = (tmpfs_dentry_t *) tmpfs_create_node(dev_handle, L_DIRECTORY);
234
    if (!root) {
242
    if (!root)
235
        hash_table_destroy(&dentries);
-
 
236
        return false;
243
        return false;
237
    }
-
 
238
    root->lnkcnt = 0;   /* FS root is not linked */
244
    root->lnkcnt = 0;   /* FS root is not linked */
239
    return true;
245
    return true;
240
}
246
}
241
 
247
 
242
/** Compare one component of path to a directory entry.
248
/** Compare one component of path to a directory entry.
Line 253... Line 259...
253
{
259
{
254
    unsigned long key = (unsigned long) parentp;
260
    unsigned long key = (unsigned long) parentp;
255
    link_t *hlp = hash_table_find(&childp->names, &key);
261
    link_t *hlp = hash_table_find(&childp->names, &key);
256
    assert(hlp);
262
    assert(hlp);
257
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
263
    tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t, link);
258
    return !strcmp(namep->name, component);
264
    return !str_cmp(namep->name, component);
259
}
265
}
260
 
266
 
261
void *tmpfs_match(void *prnt, const char *component)
267
void *tmpfs_match(void *prnt, const char *component)
262
{
268
{
263
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
269
    tmpfs_dentry_t *parentp = (tmpfs_dentry_t *) prnt;
Line 270... Line 276...
270
}
276
}
271
 
277
 
272
void *
278
void *
273
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
279
tmpfs_node_get(dev_handle_t dev_handle, fs_index_t index)
274
{
280
{
275
    unsigned long key = index;
281
    unsigned long key[] = {
-
 
282
        [DENTRIES_KEY_INDEX] = index,
-
 
283
        [DENTRIES_KEY_DEV] = dev_handle
-
 
284
    };
276
    link_t *lnk = hash_table_find(&dentries, &key);
285
    link_t *lnk = hash_table_find(&dentries, key);
277
    if (!lnk)
286
    if (!lnk)
278
        return NULL;
287
        return NULL;
279
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
288
    return hash_table_get_instance(lnk, tmpfs_dentry_t, dh_link);
280
}
289
}
281
 
290
 
Line 294... Line 303...
294
 
303
 
295
    if (!tmpfs_dentry_initialize(node)) {
304
    if (!tmpfs_dentry_initialize(node)) {
296
        free(node);
305
        free(node);
297
        return NULL;
306
        return NULL;
298
    }
307
    }
-
 
308
    if (!tmpfs_root_get(dev_handle))
-
 
309
        node->index = TMPFS_SOME_ROOT;
-
 
310
    else
299
    node->index = tmpfs_next_index++;
311
        node->index = tmpfs_next_index++;
-
 
312
    node->dev_handle = dev_handle;
300
    if (lflag & L_DIRECTORY)
313
    if (lflag & L_DIRECTORY)
301
        node->type = TMPFS_DIRECTORY;
314
        node->type = TMPFS_DIRECTORY;
302
    else
315
    else
303
        node->type = TMPFS_FILE;
316
        node->type = TMPFS_FILE;
304
 
317
 
305
    /* Insert the new node into the dentry hash table. */
318
    /* Insert the new node into the dentry hash table. */
306
    unsigned long key = node->index;
319
    unsigned long key[] = {
-
 
320
        [DENTRIES_KEY_INDEX] = node->index,
-
 
321
        [DENTRIES_KEY_DEV] = node->dev_handle
-
 
322
    };
307
    hash_table_insert(&dentries, &key, &node->dh_link);
323
    hash_table_insert(&dentries, key, &node->dh_link);
308
    return (void *) node;
324
    return (void *) node;
309
}
325
}
310
 
326
 
311
int tmpfs_link_node(void *prnt, void *chld, const char *nm)
327
int tmpfs_link_node(void *prnt, void *chld, const char *nm)
312
{
328
{
Line 317... Line 333...
317
 
333
 
318
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
334
    tmpfs_name_t *namep = malloc(sizeof(tmpfs_name_t));
319
    if (!namep)
335
    if (!namep)
320
        return ENOMEM;
336
        return ENOMEM;
321
    tmpfs_name_initialize(namep);
337
    tmpfs_name_initialize(namep);
322
    size_t len = strlen(nm);
338
    size_t size = str_size(nm);
323
    namep->name = malloc(len + 1);
339
    namep->name = malloc(size + 1);
324
    if (!namep->name) {
340
    if (!namep->name) {
325
        free(namep);
341
        free(namep);
326
        return ENOMEM;
342
        return ENOMEM;
327
    }
343
    }
328
    strcpy(namep->name, nm);
344
    str_cpy(namep->name, size + 1, nm);
329
    namep->parent = parentp;
345
    namep->parent = parentp;
330
   
346
   
331
    childp->lnkcnt++;
347
    childp->lnkcnt++;
332
 
348
 
333
    unsigned long key = (unsigned long) parentp;
349
    unsigned long key = (unsigned long) parentp;
Line 382... Line 398...
382
   
398
   
383
    assert(!dentry->lnkcnt);
399
    assert(!dentry->lnkcnt);
384
    assert(!dentry->child);
400
    assert(!dentry->child);
385
    assert(!dentry->sibling);
401
    assert(!dentry->sibling);
386
 
402
 
387
    unsigned long key = dentry->index;
403
    unsigned long key[] = {
-
 
404
        [DENTRIES_KEY_INDEX] = dentry->index,
-
 
405
        [DENTRIES_KEY_DEV] = dentry->dev_handle
-
 
406
    };
388
    hash_table_remove(&dentries, &key, 1);
407
    hash_table_remove(&dentries, key, 2);
389
 
408
 
390
    hash_table_destroy(&dentry->names);
409
    hash_table_destroy(&dentry->names);
391
 
410
 
392
    if (dentry->type == TMPFS_FILE)
411
    if (dentry->type == TMPFS_FILE)
393
        free(dentry->data);
412
        free(dentry->data);
Line 397... Line 416...
397
 
416
 
398
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
417
void tmpfs_mounted(ipc_callid_t rid, ipc_call_t *request)
399
{
418
{
400
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
419
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
401
 
420
 
-
 
421
    /* accept the mount options */
-
 
422
    ipc_callid_t callid;
-
 
423
    size_t size;
-
 
424
    if (!ipc_data_write_receive(&callid, &size)) {
-
 
425
        ipc_answer_0(callid, EINVAL);
-
 
426
        ipc_answer_0(rid, EINVAL);
-
 
427
        return;
-
 
428
    }
-
 
429
    char *opts = malloc(size + 1);
-
 
430
    if (!opts) {
-
 
431
        ipc_answer_0(callid, ENOMEM);
-
 
432
        ipc_answer_0(rid, ENOMEM);
-
 
433
        return;
-
 
434
    }
-
 
435
    ipcarg_t retval = ipc_data_write_finalize(callid, opts, size);
-
 
436
    if (retval != EOK) {
-
 
437
        ipc_answer_0(rid, retval);
-
 
438
        free(opts);
-
 
439
        return;
-
 
440
    }
-
 
441
    opts[size] = '\0';
-
 
442
 
402
    /* Initialize TMPFS. */
443
    /* Initialize TMPFS instance. */
403
    if (!root && !tmpfs_init()) {
444
    if (!tmpfs_instance_init(dev_handle)) {
404
        ipc_answer_0(rid, ENOMEM);
445
        ipc_answer_0(rid, ENOMEM);
405
        return;
446
        return;
406
    }
447
    }
407
 
448
 
-
 
449
    tmpfs_dentry_t *root = tmpfs_root_get(dev_handle);
408
    if (dev_handle >= 0) {
450
    if (str_cmp(opts, "restore") == 0) {
409
        if (tmpfs_restore(dev_handle))
451
        if (tmpfs_restore(dev_handle))
410
            ipc_answer_3(rid, EOK, root->index, root->size,
452
            ipc_answer_3(rid, EOK, root->index, root->size,
411
                root->lnkcnt);
453
                root->lnkcnt);
412
        else
454
        else
413
            ipc_answer_0(rid, ELIMIT);
455
            ipc_answer_0(rid, ELIMIT);
Line 439... Line 481...
439
 
481
 
440
    /*
482
    /*
441
     * Lookup the respective dentry.
483
     * Lookup the respective dentry.
442
     */
484
     */
443
    link_t *hlp;
485
    link_t *hlp;
444
    unsigned long key = index;
486
    unsigned long key[] = {
-
 
487
        [DENTRIES_KEY_INDEX] = index,
-
 
488
        [DENTRIES_KEY_DEV] = dev_handle,
-
 
489
    };
445
    hlp = hash_table_find(&dentries, &key);
490
    hlp = hash_table_find(&dentries, key);
446
    if (!hlp) {
491
    if (!hlp) {
447
        ipc_answer_0(rid, ENOENT);
492
        ipc_answer_0(rid, ENOENT);
448
        return;
493
        return;
449
    }
494
    }
450
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
495
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
Line 452... Line 497...
452
 
497
 
453
    /*
498
    /*
454
     * Receive the read request.
499
     * Receive the read request.
455
     */
500
     */
456
    ipc_callid_t callid;
501
    ipc_callid_t callid;
457
    size_t len;
502
    size_t size;
458
    if (!ipc_data_read_receive(&callid, &len)) {
503
    if (!ipc_data_read_receive(&callid, &size)) {
459
        ipc_answer_0(callid, EINVAL);  
504
        ipc_answer_0(callid, EINVAL);  
460
        ipc_answer_0(rid, EINVAL);
505
        ipc_answer_0(rid, EINVAL);
461
        return;
506
        return;
462
    }
507
    }
463
 
508
 
464
    size_t bytes;
509
    size_t bytes;
465
    if (dentry->type == TMPFS_FILE) {
510
    if (dentry->type == TMPFS_FILE) {
466
        bytes = max(0, min(dentry->size - pos, len));
511
        bytes = max(0, min(dentry->size - pos, size));
467
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
512
        (void) ipc_data_read_finalize(callid, dentry->data + pos,
468
            bytes);
513
            bytes);
469
    } else {
514
    } else {
470
        int i;
515
        int i;
471
        tmpfs_dentry_t *cur;
516
        tmpfs_dentry_t *cur;
Line 492... Line 537...
492
        assert(hlp);
537
        assert(hlp);
493
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
538
        tmpfs_name_t *namep = hash_table_get_instance(hlp, tmpfs_name_t,
494
            link);
539
            link);
495
 
540
 
496
        (void) ipc_data_read_finalize(callid, namep->name,
541
        (void) ipc_data_read_finalize(callid, namep->name,
497
            strlen(namep->name) + 1);
542
            str_size(namep->name) + 1);
498
        bytes = 1;
543
        bytes = 1;
499
    }
544
    }
500
 
545
 
501
    /*
546
    /*
502
     * Answer the VFS_READ call.
547
     * Answer the VFS_READ call.
Line 512... Line 557...
512
 
557
 
513
    /*
558
    /*
514
     * Lookup the respective dentry.
559
     * Lookup the respective dentry.
515
     */
560
     */
516
    link_t *hlp;
561
    link_t *hlp;
517
    unsigned long key = index;
562
    unsigned long key[] = {
-
 
563
        [DENTRIES_KEY_INDEX] = index,
-
 
564
        [DENTRIES_KEY_DEV] = dev_handle
-
 
565
    };
518
    hlp = hash_table_find(&dentries, &key);
566
    hlp = hash_table_find(&dentries, key);
519
    if (!hlp) {
567
    if (!hlp) {
520
        ipc_answer_0(rid, ENOENT);
568
        ipc_answer_0(rid, ENOENT);
521
        return;
569
        return;
522
    }
570
    }
523
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
571
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
Line 525... Line 573...
525
 
573
 
526
    /*
574
    /*
527
     * Receive the write request.
575
     * Receive the write request.
528
     */
576
     */
529
    ipc_callid_t callid;
577
    ipc_callid_t callid;
530
    size_t len;
578
    size_t size;
531
    if (!ipc_data_write_receive(&callid, &len)) {
579
    if (!ipc_data_write_receive(&callid, &size)) {
532
        ipc_answer_0(callid, EINVAL);  
580
        ipc_answer_0(callid, EINVAL);  
533
        ipc_answer_0(rid, EINVAL);
581
        ipc_answer_0(rid, EINVAL);
534
        return;
582
        return;
535
    }
583
    }
536
 
584
 
537
    /*
585
    /*
538
     * Check whether the file needs to grow.
586
     * Check whether the file needs to grow.
539
     */
587
     */
540
    if (pos + len <= dentry->size) {
588
    if (pos + size <= dentry->size) {
541
        /* The file size is not changing. */
589
        /* The file size is not changing. */
542
        (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
590
        (void) ipc_data_write_finalize(callid, dentry->data + pos, size);
543
        ipc_answer_2(rid, EOK, len, dentry->size);
591
        ipc_answer_2(rid, EOK, size, dentry->size);
544
        return;
592
        return;
545
    }
593
    }
546
    size_t delta = (pos + len) - dentry->size;
594
    size_t delta = (pos + size) - dentry->size;
547
    /*
595
    /*
548
     * At this point, we are deliberately extremely straightforward and
596
     * At this point, we are deliberately extremely straightforward and
549
     * simply realloc the contents of the file on every write that grows the
597
     * simply realloc the contents of the file on every write that grows the
550
     * file. In the end, the situation might not be as bad as it may look:
598
     * file. In the end, the situation might not be as bad as it may look:
551
     * our heap allocator can save us and just grow the block whenever
599
     * our heap allocator can save us and just grow the block whenever
Line 559... Line 607...
559
    }
607
    }
560
    /* Clear any newly allocated memory in order to emulate gaps. */
608
    /* Clear any newly allocated memory in order to emulate gaps. */
561
    memset(newdata + dentry->size, 0, delta);
609
    memset(newdata + dentry->size, 0, delta);
562
    dentry->size += delta;
610
    dentry->size += delta;
563
    dentry->data = newdata;
611
    dentry->data = newdata;
564
    (void) ipc_data_write_finalize(callid, dentry->data + pos, len);
612
    (void) ipc_data_write_finalize(callid, dentry->data + pos, size);
565
    ipc_answer_2(rid, EOK, len, dentry->size);
613
    ipc_answer_2(rid, EOK, size, dentry->size);
566
}
614
}
567
 
615
 
568
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
616
void tmpfs_truncate(ipc_callid_t rid, ipc_call_t *request)
569
{
617
{
570
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
618
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
Line 573... Line 621...
573
 
621
 
574
    /*
622
    /*
575
     * Lookup the respective dentry.
623
     * Lookup the respective dentry.
576
     */
624
     */
577
    link_t *hlp;
625
    link_t *hlp;
578
    unsigned long key = index;
626
    unsigned long key[] = {
-
 
627
        [DENTRIES_KEY_INDEX] = index,
-
 
628
        [DENTRIES_KEY_DEV] = dev_handle
-
 
629
    };
579
    hlp = hash_table_find(&dentries, &key);
630
    hlp = hash_table_find(&dentries, key);
580
    if (!hlp) {
631
    if (!hlp) {
581
        ipc_answer_0(rid, ENOENT);
632
        ipc_answer_0(rid, ENOENT);
582
        return;
633
        return;
583
    }
634
    }
584
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
635
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
Line 608... Line 659...
608
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
659
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
609
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
660
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
610
    int rc;
661
    int rc;
611
 
662
 
612
    link_t *hlp;
663
    link_t *hlp;
613
    unsigned long key = index;
664
    unsigned long key[] = {
-
 
665
        [DENTRIES_KEY_INDEX] = index,
-
 
666
        [DENTRIES_KEY_DEV] = dev_handle
-
 
667
    };
614
    hlp = hash_table_find(&dentries, &key);
668
    hlp = hash_table_find(&dentries, key);
615
    if (!hlp) {
669
    if (!hlp) {
616
        ipc_answer_0(rid, ENOENT);
670
        ipc_answer_0(rid, ENOENT);
617
        return;
671
        return;
618
    }
672
    }
619
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,
673
    tmpfs_dentry_t *dentry = hash_table_get_instance(hlp, tmpfs_dentry_t,