Subversion Repositories HelenOS

Rev

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

Rev 4305 Rev 4306
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 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 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 419... Line 438...
419
        free(opts);
438
        free(opts);
420
        return;
439
        return;
421
    }
440
    }
422
    opts[size] = '\0';
441
    opts[size] = '\0';
423
 
442
 
424
    /* Initialize TMPFS. */
443
    /* Initialize TMPFS instance. */
425
    if (!root && !tmpfs_init()) {
444
    if (!tmpfs_instance_init(dev_handle)) {
426
        ipc_answer_0(rid, ENOMEM);
445
        ipc_answer_0(rid, ENOMEM);
427
        return;
446
        return;
428
    }
447
    }
429
 
448
 
-
 
449
    tmpfs_dentry_t *root = tmpfs_root_get(dev_handle);
430
    if (str_cmp(opts, "restore") == 0) {
450
    if (str_cmp(opts, "restore") == 0) {
431
        if (tmpfs_restore(dev_handle))
451
        if (tmpfs_restore(dev_handle))
432
            ipc_answer_3(rid, EOK, root->index, root->size,
452
            ipc_answer_3(rid, EOK, root->index, root->size,
433
                root->lnkcnt);
453
                root->lnkcnt);
434
        else
454
        else
Line 461... Line 481...
461
 
481
 
462
    /*
482
    /*
463
     * Lookup the respective dentry.
483
     * Lookup the respective dentry.
464
     */
484
     */
465
    link_t *hlp;
485
    link_t *hlp;
466
    unsigned long key = index;
486
    unsigned long key[] = {
-
 
487
        [DENTRIES_KEY_INDEX] = index,
-
 
488
        [DENTRIES_KEY_DEV] = dev_handle,
-
 
489
    };
467
    hlp = hash_table_find(&dentries, &key);
490
    hlp = hash_table_find(&dentries, key);
468
    if (!hlp) {
491
    if (!hlp) {
469
        ipc_answer_0(rid, ENOENT);
492
        ipc_answer_0(rid, ENOENT);
470
        return;
493
        return;
471
    }
494
    }
472
    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 534... Line 557...
534
 
557
 
535
    /*
558
    /*
536
     * Lookup the respective dentry.
559
     * Lookup the respective dentry.
537
     */
560
     */
538
    link_t *hlp;
561
    link_t *hlp;
539
    unsigned long key = index;
562
    unsigned long key[] = {
-
 
563
        [DENTRIES_KEY_INDEX] = index,
-
 
564
        [DENTRIES_KEY_DEV] = dev_handle
-
 
565
    };
540
    hlp = hash_table_find(&dentries, &key);
566
    hlp = hash_table_find(&dentries, key);
541
    if (!hlp) {
567
    if (!hlp) {
542
        ipc_answer_0(rid, ENOENT);
568
        ipc_answer_0(rid, ENOENT);
543
        return;
569
        return;
544
    }
570
    }
545
    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 595... Line 621...
595
 
621
 
596
    /*
622
    /*
597
     * Lookup the respective dentry.
623
     * Lookup the respective dentry.
598
     */
624
     */
599
    link_t *hlp;
625
    link_t *hlp;
600
    unsigned long key = index;
626
    unsigned long key[] = {
-
 
627
        [DENTRIES_KEY_INDEX] = index,
-
 
628
        [DENTRIES_KEY_DEV] = dev_handle
-
 
629
    };
601
    hlp = hash_table_find(&dentries, &key);
630
    hlp = hash_table_find(&dentries, key);
602
    if (!hlp) {
631
    if (!hlp) {
603
        ipc_answer_0(rid, ENOENT);
632
        ipc_answer_0(rid, ENOENT);
604
        return;
633
        return;
605
    }
634
    }
606
    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 630... Line 659...
630
    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);
631
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
660
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
632
    int rc;
661
    int rc;
633
 
662
 
634
    link_t *hlp;
663
    link_t *hlp;
635
    unsigned long key = index;
664
    unsigned long key[] = {
-
 
665
        [DENTRIES_KEY_INDEX] = index,
-
 
666
        [DENTRIES_KEY_DEV] = dev_handle
-
 
667
    };
636
    hlp = hash_table_find(&dentries, &key);
668
    hlp = hash_table_find(&dentries, key);
637
    if (!hlp) {
669
    if (!hlp) {
638
        ipc_answer_0(rid, ENOENT);
670
        ipc_answer_0(rid, ENOENT);
639
        return;
671
        return;
640
    }
672
    }
641
    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,