Subversion Repositories HelenOS

Rev

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

Rev 3623 Rev 3675
Line 92... Line 92...
92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
93
 
93
 
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
95
 
95
 
96
    d->firstc = host2uint16_t_le(node->firstc);
96
    d->firstc = host2uint16_t_le(node->firstc);
97
    if (node->type == FAT_FILE)
97
    if (node->type == FAT_FILE) {
98
        d->size = host2uint32_t_le(node->size);
98
        d->size = host2uint32_t_le(node->size);
-
 
99
    } else if (node->type == FAT_DIRECTORY) {
-
 
100
        d->attr = FAT_ATTR_SUBDIR;
-
 
101
    }
-
 
102
   
99
    /* TODO: update other fields? (e.g time fields, attr field) */
103
    /* TODO: update other fields? (e.g time fields) */
100
   
104
   
101
    b->dirty = true;        /* need to sync block */
105
    b->dirty = true;        /* need to sync block */
102
    block_put(b);
106
    block_put(b);
103
}
107
}
104
 
108
 
Line 221... Line 225...
221
 */
225
 */
222
static void *fat_node_get(dev_handle_t, fs_index_t);
226
static void *fat_node_get(dev_handle_t, fs_index_t);
223
static void fat_node_put(void *);
227
static void fat_node_put(void *);
224
static void *fat_create_node(dev_handle_t, int);
228
static void *fat_create_node(dev_handle_t, int);
225
static int fat_destroy_node(void *);
229
static int fat_destroy_node(void *);
226
static bool fat_link(void *, void *, const char *);
230
static int fat_link(void *, void *, const char *);
227
static int fat_unlink(void *, void *);
231
static int fat_unlink(void *, void *);
228
static void *fat_match(void *, const char *);
232
static void *fat_match(void *, const char *);
229
static fs_index_t fat_index_get(void *);
233
static fs_index_t fat_index_get(void *);
230
static size_t fat_size_get(void *);
234
static size_t fat_size_get(void *);
231
static unsigned fat_lnkcnt_get(void *);
235
static unsigned fat_lnkcnt_get(void *);
Line 282... Line 286...
282
 
286
 
283
void *fat_create_node(dev_handle_t dev_handle, int flags)
287
void *fat_create_node(dev_handle_t dev_handle, int flags)
284
{
288
{
285
    fat_idx_t *idxp;
289
    fat_idx_t *idxp;
286
    fat_node_t *nodep;
290
    fat_node_t *nodep;
-
 
291
    fat_bs_t *bs;
-
 
292
    fat_cluster_t mcl, lcl;
-
 
293
    uint16_t bps;
-
 
294
    int rc;
-
 
295
 
-
 
296
    bs = block_bb_get(dev_handle);
-
 
297
    bps = uint16_t_le2host(bs->bps);
-
 
298
    if (flags & L_DIRECTORY) {
-
 
299
        /* allocate a cluster */
-
 
300
        rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
-
 
301
        if (rc != EOK)
-
 
302
            return NULL;
-
 
303
    }
287
 
304
 
288
    nodep = fat_node_get_new();
305
    nodep = fat_node_get_new();
289
    if (!nodep)
306
    if (!nodep) {
-
 
307
        fat_free_clusters(bs, dev_handle, mcl);
290
        return NULL;
308
        return NULL;
-
 
309
    }
291
    idxp = fat_idx_get_new(dev_handle);
310
    idxp = fat_idx_get_new(dev_handle);
292
    if (!idxp) {
311
    if (!idxp) {
-
 
312
        fat_free_clusters(bs, dev_handle, mcl);
293
        fat_node_put(nodep);
313
        fat_node_put(nodep);
294
        return NULL;
314
        return NULL;
295
    }
315
    }
296
    /* idxp->lock held */
316
    /* idxp->lock held */
297
    if (flags & L_DIRECTORY) {
317
    if (flags & L_DIRECTORY) {
-
 
318
        int i;
-
 
319
        block_t *b;
-
 
320
 
-
 
321
        /*
-
 
322
         * Populate the new cluster with unused dentries.
-
 
323
         */
-
 
324
        for (i = 0; i < bs->spc; i++) {
-
 
325
            b = _fat_block_get(bs, dev_handle, mcl, i,
-
 
326
                BLOCK_FLAGS_NOREAD);
-
 
327
            /* mark all dentries as never-used */
-
 
328
            memset(b->data, 0, bps);
-
 
329
            b->dirty = false;
-
 
330
            block_put(b);
-
 
331
        }
298
        nodep->type = FAT_DIRECTORY;
332
        nodep->type = FAT_DIRECTORY;
-
 
333
        nodep->firstc = mcl;
-
 
334
        nodep->size = bps * bs->spc;
299
    } else {
335
    } else {
300
        nodep->type = FAT_FILE;
336
        nodep->type = FAT_FILE;
-
 
337
        nodep->firstc = FAT_CLST_RES0;
-
 
338
        nodep->size = 0;
301
    }
339
    }
302
    nodep->size = 0;
-
 
303
    nodep->firstc = FAT_CLST_RES0;
-
 
304
    nodep->lnkcnt = 0;  /* not linked anywhere */
340
    nodep->lnkcnt = 0;  /* not linked anywhere */
305
    nodep->refcnt = 1;
341
    nodep->refcnt = 1;
-
 
342
    nodep->dirty = true;
306
 
343
 
307
    nodep->idx = idxp;
344
    nodep->idx = idxp;
308
    idxp->nodep = nodep;
345
    idxp->nodep = nodep;
309
 
346
 
310
    futex_up(&idxp->lock);
347
    futex_up(&idxp->lock);
Line 339... Line 376...
339
    fat_idx_destroy(nodep->idx);
376
    fat_idx_destroy(nodep->idx);
340
    free(nodep);
377
    free(nodep);
341
    return EOK;
378
    return EOK;
342
}
379
}
343
 
380
 
344
bool fat_link(void *prnt, void *chld, const char *name)
381
int fat_link(void *prnt, void *chld, const char *name)
345
{
382
{
-
 
383
    fat_node_t *parentp = (fat_node_t *)prnt;
-
 
384
    fat_node_t *childp = (fat_node_t *)chld;
-
 
385
    fat_dentry_t *d;
-
 
386
    fat_bs_t *bs;
-
 
387
    block_t *b;
-
 
388
    int i, j;
-
 
389
    uint16_t bps;
-
 
390
    unsigned dps;
-
 
391
    unsigned blocks;
-
 
392
 
-
 
393
    futex_down(&childp->lock);
-
 
394
    if (childp->lnkcnt == 1) {
-
 
395
        /*
346
    return false;   /* not supported at the moment */
396
         * On FAT, we don't support multiple hard links.
-
 
397
         */
-
 
398
        futex_up(&childp->lock);
-
 
399
        return EMLINK;
-
 
400
    }
-
 
401
    assert(childp->lnkcnt == 0);
-
 
402
    futex_up(&childp->lock);
-
 
403
 
-
 
404
    if (!fat_dentry_name_verify(name)) {
-
 
405
        /*
-
 
406
         * Attempt to create unsupported name.
-
 
407
         */
-
 
408
        return ENOTSUP;
-
 
409
    }
-
 
410
 
-
 
411
    /*
-
 
412
     * Get us an unused parent node's dentry or grow the parent and allocate
-
 
413
     * a new one.
-
 
414
     */
-
 
415
   
-
 
416
    futex_down(&parentp->idx->lock);
-
 
417
    bs = block_bb_get(parentp->idx->dev_handle);
-
 
418
    bps = uint16_t_le2host(bs->bps);
-
 
419
    dps = bps / sizeof(fat_dentry_t);
-
 
420
 
-
 
421
    blocks = parentp->size / bps;
-
 
422
 
-
 
423
    for (i = 0; i < blocks; i++) {
-
 
424
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
-
 
425
        for (j = 0; j < dps; j++) {
-
 
426
            d = ((fat_dentry_t *)b->data) + j;
-
 
427
            switch (fat_classify_dentry(d)) {
-
 
428
            case FAT_DENTRY_SKIP:
-
 
429
            case FAT_DENTRY_VALID:
-
 
430
                /* skipping used and meta entries */
-
 
431
                continue;
-
 
432
            case FAT_DENTRY_FREE:
-
 
433
            case FAT_DENTRY_LAST:
-
 
434
                /* found an empty slot */
-
 
435
                goto hit;
-
 
436
            }
-
 
437
        }
-
 
438
        block_put(b);
-
 
439
    }
-
 
440
   
-
 
441
    /*
-
 
442
     * We need to grow the parent in order to create a new unused dentry.
-
 
443
     */
-
 
444
    futex_up(&parentp->idx->lock);
-
 
445
    return ENOTSUP; /* XXX */
-
 
446
 
-
 
447
hit:
-
 
448
    /*
-
 
449
     * At this point we only establish the link between the parent and the
-
 
450
     * child.  The dentry, except of the name and the extension, will remain
-
 
451
     * uninitialized until the the corresponding node is synced. Thus the
-
 
452
     * valid dentry data is kept in the child node structure.
-
 
453
     */
-
 
454
    memset(d, 0, sizeof(fat_dentry_t));
-
 
455
    fat_dentry_name_set(d, name);
-
 
456
    b->dirty = true;        /* need to sync block */
-
 
457
    block_put(b);
-
 
458
    futex_up(&parentp->idx->lock);
-
 
459
 
-
 
460
    futex_down(&childp->idx->lock);
-
 
461
   
-
 
462
    /*
-
 
463
     * If possible, create the Sub-directory Identifier Entry and the
-
 
464
     * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
-
 
465
     * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
-
 
466
     * not use them anyway, so this is rather a sign of our good will.
-
 
467
     */
-
 
468
    b = fat_block_get(bs, childp, 0, BLOCK_FLAGS_NONE);
-
 
469
    d = (fat_dentry_t *)b->data;
-
 
470
    if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
-
 
471
        strcmp(d->name, FAT_NAME_DOT) == 0) {
-
 
472
        memset(d, 0, sizeof(fat_dentry_t));
-
 
473
        strcpy(d->name, FAT_NAME_DOT);
-
 
474
        strcpy(d->ext, FAT_EXT_PAD);
-
 
475
        d->attr = FAT_ATTR_SUBDIR;
-
 
476
        d->firstc = host2uint16_t_le(childp->firstc);
-
 
477
        /* TODO: initialize also the date/time members. */
-
 
478
    }
-
 
479
    d++;
-
 
480
    if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
-
 
481
        strcmp(d->name, FAT_NAME_DOT_DOT) == 0) {
-
 
482
        memset(d, 0, sizeof(fat_dentry_t));
-
 
483
        strcpy(d->name, FAT_NAME_DOT_DOT);
-
 
484
        strcpy(d->ext, FAT_EXT_PAD);
-
 
485
        d->attr = FAT_ATTR_SUBDIR;
-
 
486
        d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
-
 
487
            host2uint16_t_le(FAT_CLST_RES0) :
-
 
488
            host2uint16_t_le(parentp->firstc);
-
 
489
        /* TODO: initialize also the date/time members. */
-
 
490
    }
-
 
491
    b->dirty = true;        /* need to sync block */
-
 
492
    block_put(b);
-
 
493
 
-
 
494
    childp->idx->pfc = parentp->firstc;
-
 
495
    childp->idx->pdi = i * dps + j;
-
 
496
    futex_up(&childp->idx->lock);
-
 
497
 
-
 
498
    futex_down(&childp->lock);
-
 
499
    childp->lnkcnt = 1;
-
 
500
    childp->dirty = true;       /* need to sync node */
-
 
501
    futex_up(&childp->lock);
-
 
502
 
-
 
503
    /*
-
 
504
     * Hash in the index structure into the position hash.
-
 
505
     */
-
 
506
    fat_idx_hashin(childp->idx);
-
 
507
 
-
 
508
    return EOK;
347
}
509
}
348
 
510
 
349
int fat_unlink(void *prnt, void *chld)
511
int fat_unlink(void *prnt, void *chld)
350
{
512
{
-
 
513
    fat_node_t *parentp = (fat_node_t *)prnt;
-
 
514
    fat_node_t *childp = (fat_node_t *)chld;
-
 
515
    fat_bs_t *bs;
-
 
516
    fat_dentry_t *d;
-
 
517
    uint16_t bps;
-
 
518
    block_t *b;
-
 
519
 
-
 
520
    futex_down(&parentp->lock);
-
 
521
    futex_down(&childp->lock);
-
 
522
    assert(childp->lnkcnt == 1);
-
 
523
    futex_down(&childp->idx->lock);
-
 
524
    bs = block_bb_get(childp->idx->dev_handle);
-
 
525
    bps = uint16_t_le2host(bs->bps);
-
 
526
 
-
 
527
    b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc,
-
 
528
        (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
-
 
529
        BLOCK_FLAGS_NONE);
-
 
530
    d = (fat_dentry_t *)b->data +
-
 
531
        (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
-
 
532
    /* mark the dentry as not-currently-used */
-
 
533
    d->name[0] = FAT_DENTRY_ERASED;
351
    return ENOTSUP; /* not supported at the moment */
534
    b->dirty = true;        /* need to sync block */
-
 
535
    block_put(b);
-
 
536
 
-
 
537
    /* remove the index structure from the position hash */
-
 
538
    fat_idx_hashout(childp->idx);
-
 
539
    /* clear position information */
-
 
540
    childp->idx->pfc = FAT_CLST_RES0;
-
 
541
    childp->idx->pdi = 0;
-
 
542
    futex_up(&childp->idx->lock);
-
 
543
    childp->lnkcnt = 0;
-
 
544
    childp->dirty = true;
-
 
545
    futex_up(&childp->lock);
-
 
546
    futex_up(&parentp->lock);
-
 
547
 
-
 
548
    return EOK;
352
}
549
}
353
 
550
 
354
void *fat_match(void *prnt, const char *component)
551
void *fat_match(void *prnt, const char *component)
355
{
552
{
356
    fat_bs_t *bs;
553
    fat_bs_t *bs;
Line 372... Line 569...
372
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
569
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
373
        for (j = 0; j < dps; j++) {
570
        for (j = 0; j < dps; j++) {
374
            d = ((fat_dentry_t *)b->data) + j;
571
            d = ((fat_dentry_t *)b->data) + j;
375
            switch (fat_classify_dentry(d)) {
572
            switch (fat_classify_dentry(d)) {
376
            case FAT_DENTRY_SKIP:
573
            case FAT_DENTRY_SKIP:
-
 
574
            case FAT_DENTRY_FREE:
377
                continue;
575
                continue;
378
            case FAT_DENTRY_LAST:
576
            case FAT_DENTRY_LAST:
379
                block_put(b);
577
                block_put(b);
380
                futex_up(&parentp->idx->lock);
578
                futex_up(&parentp->idx->lock);
381
                return NULL;
579
                return NULL;
382
            default:
580
            default:
383
            case FAT_DENTRY_VALID:
581
            case FAT_DENTRY_VALID:
384
                dentry_name_canonify(d, name);
582
                fat_dentry_name_get(d, name);
385
                break;
583
                break;
386
            }
584
            }
387
            if (stricmp(name, component) == 0) {
585
            if (fat_dentry_namecmp(name, component) == 0) {
388
                /* hit */
586
                /* hit */
389
                void *node;
587
                void *node;
390
                /*
588
                /*
391
                 * Assume tree hierarchy for locking.  We
589
                 * Assume tree hierarchy for locking.  We
392
                 * already have the parent and now we are going
590
                 * already have the parent and now we are going
Line 462... Line 660...
462
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
660
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
463
        for (j = 0; j < dps; j++) {
661
        for (j = 0; j < dps; j++) {
464
            d = ((fat_dentry_t *)b->data) + j;
662
            d = ((fat_dentry_t *)b->data) + j;
465
            switch (fat_classify_dentry(d)) {
663
            switch (fat_classify_dentry(d)) {
466
            case FAT_DENTRY_SKIP:
664
            case FAT_DENTRY_SKIP:
-
 
665
            case FAT_DENTRY_FREE:
467
                continue;
666
                continue;
468
            case FAT_DENTRY_LAST:
667
            case FAT_DENTRY_LAST:
469
                block_put(b);
668
                block_put(b);
470
                futex_up(&nodep->idx->lock);
669
                futex_up(&nodep->idx->lock);
471
                return false;
670
                return false;
Line 523... Line 722...
523
    .plb_get_char = fat_plb_get_char,
722
    .plb_get_char = fat_plb_get_char,
524
    .is_directory = fat_is_directory,
723
    .is_directory = fat_is_directory,
525
    .is_file = fat_is_file
724
    .is_file = fat_is_file
526
};
725
};
527
 
726
 
-
 
727
/*
-
 
728
 * VFS operations.
-
 
729
 */
-
 
730
 
528
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
731
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
529
{
732
{
530
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
733
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
531
    fat_bs_t *bs;
734
    fat_bs_t *bs;
532
    uint16_t bps;
735
    uint16_t bps;
Line 692... Line 895...
692
                o < bps / sizeof(fat_dentry_t);
895
                o < bps / sizeof(fat_dentry_t);
693
                o++, pos++) {
896
                o++, pos++) {
694
                d = ((fat_dentry_t *)b->data) + o;
897
                d = ((fat_dentry_t *)b->data) + o;
695
                switch (fat_classify_dentry(d)) {
898
                switch (fat_classify_dentry(d)) {
696
                case FAT_DENTRY_SKIP:
899
                case FAT_DENTRY_SKIP:
-
 
900
                case FAT_DENTRY_FREE:
697
                    continue;
901
                    continue;
698
                case FAT_DENTRY_LAST:
902
                case FAT_DENTRY_LAST:
699
                    block_put(b);
903
                    block_put(b);
700
                    goto miss;
904
                    goto miss;
701
                default:
905
                default:
702
                case FAT_DENTRY_VALID:
906
                case FAT_DENTRY_VALID:
703
                    dentry_name_canonify(d, name);
907
                    fat_dentry_name_get(d, name);
704
                    block_put(b);
908
                    block_put(b);
705
                    goto hit;
909
                    goto hit;
706
                }
910
                }
707
            }
911
            }
708
            block_put(b);
912
            block_put(b);