Subversion Repositories HelenOS

Rev

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

Rev 3513 Rev 3516
Line 123... Line 123...
123
 *
123
 *
124
 * @param idxp      Locked index structure.
124
 * @param idxp      Locked index structure.
125
 */
125
 */
126
static void *fat_node_get_core(fat_idx_t *idxp)
126
static void *fat_node_get_core(fat_idx_t *idxp)
127
{
127
{
128
    block_t *b;
128
    block_t *bb, *b;
129
    fat_dentry_t *d;
129
    fat_dentry_t *d;
130
    fat_node_t *nodep = NULL;
130
    fat_node_t *nodep = NULL;
131
    unsigned bps;
131
    unsigned bps;
132
    unsigned dps;
132
    unsigned dps;
133
 
133
 
Line 176... Line 176...
176
        if (!nodep)
176
        if (!nodep)
177
            return NULL;
177
            return NULL;
178
    }
178
    }
179
    fat_node_initialize(nodep);
179
    fat_node_initialize(nodep);
180
 
180
 
181
    bps = fat_bps_get(idxp->dev_handle);
181
    bb = block_get(idxp->dev_handle, BS_BLOCK, BS_SIZE);
-
 
182
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
182
    dps = bps / sizeof(fat_dentry_t);
183
    dps = bps / sizeof(fat_dentry_t);
183
 
184
 
184
    /* Read the block that contains the dentry of interest. */
185
    /* Read the block that contains the dentry of interest. */
185
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
186
    b = _fat_block_get(bb->data, idxp->dev_handle, idxp->pfc,
186
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
187
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
187
    assert(b);
188
    assert(b);
188
 
189
 
189
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
190
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
190
    if (d->attr & FAT_ATTR_SUBDIR) {
191
    if (d->attr & FAT_ATTR_SUBDIR) {
Line 197... Line 198...
197
        /*
198
        /*
198
         * Unfortunately, the 'size' field of the FAT dentry is not
199
         * Unfortunately, the 'size' field of the FAT dentry is not
199
         * defined for the directory entry type. We must determine the
200
         * defined for the directory entry type. We must determine the
200
         * size of the directory by walking the FAT.
201
         * size of the directory by walking the FAT.
201
         */
202
         */
202
        nodep->size = bps * _fat_blcks_get(idxp->dev_handle,
203
        nodep->size = bps * _fat_blcks_get(bb->data, idxp->dev_handle,
203
            uint16_t_le2host(d->firstc), NULL);
204
            uint16_t_le2host(d->firstc), NULL);
204
    } else {
205
    } else {
205
        nodep->type = FAT_FILE;
206
        nodep->type = FAT_FILE;
206
        nodep->size = uint32_t_le2host(d->size);
207
        nodep->size = uint32_t_le2host(d->size);
207
    }
208
    }
208
    nodep->firstc = uint16_t_le2host(d->firstc);
209
    nodep->firstc = uint16_t_le2host(d->firstc);
209
    nodep->lnkcnt = 1;
210
    nodep->lnkcnt = 1;
210
    nodep->refcnt = 1;
211
    nodep->refcnt = 1;
211
 
212
 
212
    block_put(b);
213
    block_put(b);
-
 
214
    block_put(bb);
213
 
215
 
214
    /* Link the idx structure with the node structure. */
216
    /* Link the idx structure with the node structure. */
215
    nodep->idx = idxp;
217
    nodep->idx = idxp;
216
    idxp->nodep = nodep;
218
    idxp->nodep = nodep;
217
 
219
 
Line 273... Line 275...
273
    unsigned i, j;
275
    unsigned i, j;
274
    unsigned bps;       /* bytes per sector */
276
    unsigned bps;       /* bytes per sector */
275
    unsigned dps;       /* dentries per sector */
277
    unsigned dps;       /* dentries per sector */
276
    unsigned blocks;
278
    unsigned blocks;
277
    fat_dentry_t *d;
279
    fat_dentry_t *d;
278
    block_t *b;
280
    block_t *bb, *b;
279
 
281
 
280
    futex_down(&parentp->idx->lock);
282
    futex_down(&parentp->idx->lock);
281
    bps = fat_bps_get(parentp->idx->dev_handle);
283
    bb = block_get(parentp->idx->dev_handle, BS_BLOCK, BS_SIZE);
-
 
284
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
282
    dps = bps / sizeof(fat_dentry_t);
285
    dps = bps / sizeof(fat_dentry_t);
283
    blocks = parentp->size / bps + (parentp->size % bps != 0);
286
    blocks = parentp->size / bps + (parentp->size % bps != 0);
284
    for (i = 0; i < blocks; i++) {
287
    for (i = 0; i < blocks; i++) {
285
        unsigned dentries;
288
        unsigned dentries;
286
       
289
       
287
        b = fat_block_get(parentp, i);
290
        b = fat_block_get(bb->data, parentp, i);
288
        dentries = (i == blocks - 1) ?
291
        dentries = (i == blocks - 1) ?
289
            parentp->size % sizeof(fat_dentry_t) :
292
            parentp->size % sizeof(fat_dentry_t) :
290
            dps;
293
            dps;
291
        for (j = 0; j < dentries; j++) {
294
        for (j = 0; j < dentries; j++) {
292
            d = ((fat_dentry_t *)b->data) + j;
295
            d = ((fat_dentry_t *)b->data) + j;
293
            switch (fat_classify_dentry(d)) {
296
            switch (fat_classify_dentry(d)) {
294
            case FAT_DENTRY_SKIP:
297
            case FAT_DENTRY_SKIP:
295
                continue;
298
                continue;
296
            case FAT_DENTRY_LAST:
299
            case FAT_DENTRY_LAST:
297
                block_put(b);
300
                block_put(b);
-
 
301
                block_put(bb);
298
                futex_up(&parentp->idx->lock);
302
                futex_up(&parentp->idx->lock);
299
                return NULL;
303
                return NULL;
300
            default:
304
            default:
301
            case FAT_DENTRY_VALID:
305
            case FAT_DENTRY_VALID:
302
                dentry_name_canonify(d, name);
306
                dentry_name_canonify(d, name);
Line 319... Line 323...
319
                    /*
323
                    /*
320
                     * Can happen if memory is low or if we
324
                     * Can happen if memory is low or if we
321
                     * run out of 32-bit indices.
325
                     * run out of 32-bit indices.
322
                     */
326
                     */
323
                    block_put(b);
327
                    block_put(b);
-
 
328
                    block_put(bb);
324
                    return NULL;
329
                    return NULL;
325
                }
330
                }
326
                node = fat_node_get_core(idx);
331
                node = fat_node_get_core(idx);
327
                futex_up(&idx->lock);
332
                futex_up(&idx->lock);
328
                block_put(b);
333
                block_put(b);
-
 
334
                block_put(bb);
329
                return node;
335
                return node;
330
            }
336
            }
331
        }
337
        }
332
        block_put(b);
338
        block_put(b);
333
    }
339
    }
-
 
340
    block_put(bb);
-
 
341
 
334
    futex_up(&parentp->idx->lock);
342
    futex_up(&parentp->idx->lock);
335
    return NULL;
343
    return NULL;
336
}
344
}
337
 
345
 
338
static fs_index_t fat_index_get(void *node)
346
static fs_index_t fat_index_get(void *node)
Line 357... Line 365...
357
{
365
{
358
    fat_node_t *nodep = (fat_node_t *)node;
366
    fat_node_t *nodep = (fat_node_t *)node;
359
    unsigned bps;
367
    unsigned bps;
360
    unsigned dps;
368
    unsigned dps;
361
    unsigned blocks;
369
    unsigned blocks;
362
    block_t *b;
370
    block_t *bb, *b;
363
    unsigned i, j;
371
    unsigned i, j;
364
 
372
 
365
    if (nodep->type != FAT_DIRECTORY)
373
    if (nodep->type != FAT_DIRECTORY)
366
        return false;
374
        return false;
367
 
375
 
368
    futex_down(&nodep->idx->lock);
376
    futex_down(&nodep->idx->lock);
369
    bps = fat_bps_get(nodep->idx->dev_handle);
377
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE);
-
 
378
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
370
    dps = bps / sizeof(fat_dentry_t);
379
    dps = bps / sizeof(fat_dentry_t);
371
 
380
 
372
    blocks = nodep->size / bps + (nodep->size % bps != 0);
381
    blocks = nodep->size / bps + (nodep->size % bps != 0);
373
 
382
 
374
    for (i = 0; i < blocks; i++) {
383
    for (i = 0; i < blocks; i++) {
375
        unsigned dentries;
384
        unsigned dentries;
376
        fat_dentry_t *d;
385
        fat_dentry_t *d;
377
   
386
   
378
        b = fat_block_get(nodep, i);
387
        b = fat_block_get(bb->data, nodep, i);
379
        dentries = (i == blocks - 1) ?
388
        dentries = (i == blocks - 1) ?
380
            nodep->size % sizeof(fat_dentry_t) :
389
            nodep->size % sizeof(fat_dentry_t) :
381
            dps;
390
            dps;
382
        for (j = 0; j < dentries; j++) {
391
        for (j = 0; j < dentries; j++) {
383
            d = ((fat_dentry_t *)b->data) + j;
392
            d = ((fat_dentry_t *)b->data) + j;
384
            switch (fat_classify_dentry(d)) {
393
            switch (fat_classify_dentry(d)) {
385
            case FAT_DENTRY_SKIP:
394
            case FAT_DENTRY_SKIP:
386
                continue;
395
                continue;
387
            case FAT_DENTRY_LAST:
396
            case FAT_DENTRY_LAST:
388
                block_put(b);
397
                block_put(b);
-
 
398
                block_put(bb);
389
                futex_up(&nodep->idx->lock);
399
                futex_up(&nodep->idx->lock);
390
                return false;
400
                return false;
391
            default:
401
            default:
392
            case FAT_DENTRY_VALID:
402
            case FAT_DENTRY_VALID:
393
                block_put(b);
403
                block_put(b);
-
 
404
                block_put(bb);
394
                futex_up(&nodep->idx->lock);
405
                futex_up(&nodep->idx->lock);
395
                return true;
406
                return true;
396
            }
407
            }
397
            block_put(b);
408
            block_put(b);
-
 
409
            block_put(bb);
398
            futex_up(&nodep->idx->lock);
410
            futex_up(&nodep->idx->lock);
399
            return true;
411
            return true;
400
        }
412
        }
401
        block_put(b);
413
        block_put(b);
402
    }
414
    }
-
 
415
    block_put(bb);
403
 
416
 
404
    futex_up(&nodep->idx->lock);
417
    futex_up(&nodep->idx->lock);
405
    return false;
418
    return false;
406
}
419
}
407
 
420
 
Line 550... Line 563...
550
{
563
{
551
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
564
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
552
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
565
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
553
    off_t pos = (off_t)IPC_GET_ARG3(*request);
566
    off_t pos = (off_t)IPC_GET_ARG3(*request);
554
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
567
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
555
    uint16_t bps = fat_bps_get(dev_handle);
568
    uint16_t bps;
556
    size_t bytes;
569
    size_t bytes;
557
    block_t *b;
570
    block_t *bb, *b;
558
 
571
 
559
    if (!nodep) {
572
    if (!nodep) {
560
        ipc_answer_0(rid, ENOENT);
573
        ipc_answer_0(rid, ENOENT);
561
        return;
574
        return;
562
    }
575
    }
Line 568... Line 581...
568
        ipc_answer_0(callid, EINVAL);
581
        ipc_answer_0(callid, EINVAL);
569
        ipc_answer_0(rid, EINVAL);
582
        ipc_answer_0(rid, EINVAL);
570
        return;
583
        return;
571
    }
584
    }
572
 
585
 
-
 
586
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
587
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
-
 
588
 
573
    if (nodep->type == FAT_FILE) {
589
    if (nodep->type == FAT_FILE) {
574
        /*
590
        /*
575
         * Our strategy for regular file reads is to read one block at
591
         * Our strategy for regular file reads is to read one block at
576
         * most and make use of the possibility to return less data than
592
         * most and make use of the possibility to return less data than
577
         * requested. This keeps the code very simple.
593
         * requested. This keeps the code very simple.
578
         */
594
         */
579
        bytes = min(len, bps - pos % bps);
595
        bytes = min(len, bps - pos % bps);
580
        b = fat_block_get(nodep, pos / bps);
596
        b = fat_block_get(bb->data, nodep, pos / bps);
581
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
597
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
582
            bytes);
598
            bytes);
583
        block_put(b);
599
        block_put(b);
584
    } else {
600
    } else {
585
        unsigned bnum;
601
        unsigned bnum;
Line 599... Line 615...
599
         */
615
         */
600
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
616
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
601
        while (bnum < nodep->size / bps) {
617
        while (bnum < nodep->size / bps) {
602
            off_t o;
618
            off_t o;
603
 
619
 
604
            b = fat_block_get(nodep, bnum);
620
            b = fat_block_get(bb->data, nodep, bnum);
605
            for (o = pos % (bps / sizeof(fat_dentry_t));
621
            for (o = pos % (bps / sizeof(fat_dentry_t));
606
                o < bps / sizeof(fat_dentry_t);
622
                o < bps / sizeof(fat_dentry_t);
607
                o++, pos++) {
623
                o++, pos++) {
608
                d = ((fat_dentry_t *)b->data) + o;
624
                d = ((fat_dentry_t *)b->data) + o;
609
                switch (fat_classify_dentry(d)) {
625
                switch (fat_classify_dentry(d)) {
Line 622... Line 638...
622
            block_put(b);
638
            block_put(b);
623
            bnum++;
639
            bnum++;
624
        }
640
        }
625
miss:
641
miss:
626
        fat_node_put(nodep);
642
        fat_node_put(nodep);
-
 
643
        block_put(bb);
627
        ipc_answer_0(callid, ENOENT);
644
        ipc_answer_0(callid, ENOENT);
628
        ipc_answer_1(rid, ENOENT, 0);
645
        ipc_answer_1(rid, ENOENT, 0);
629
        return;
646
        return;
630
hit:
647
hit:
631
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
648
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
632
        bytes = (pos - spos) + 1;
649
        bytes = (pos - spos) + 1;
633
    }
650
    }
634
 
651
 
635
    fat_node_put(nodep);
652
    fat_node_put(nodep);
-
 
653
    block_put(bb);
636
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
654
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
637
}
655
}
638
 
656
 
639
void fat_write(ipc_callid_t rid, ipc_call_t *request)
657
void fat_write(ipc_callid_t rid, ipc_call_t *request)
640
{
658
{
Line 679... Line 697...
679
    bytes = min(len, bps - pos % bps);
697
    bytes = min(len, bps - pos % bps);
680
 
698
 
681
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
699
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
682
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
700
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
683
    spc = FAT_BS(bb)->spc;
701
    spc = FAT_BS(bb)->spc;
684
    block_put(bb);
-
 
685
   
702
   
686
    boundary = ROUND_UP(nodep->size, bps * spc);
703
    boundary = ROUND_UP(nodep->size, bps * spc);
687
    if (pos < boundary) {
704
    if (pos < boundary) {
688
        /*
705
        /*
689
         * This is the easier case - we are either overwriting already
706
         * This is the easier case - we are either overwriting already
690
         * existing contents or writing behind the EOF, but still within
707
         * existing contents or writing behind the EOF, but still within
691
         * the limits of the last cluster. The node size may grow to the
708
         * the limits of the last cluster. The node size may grow to the
692
         * next block size boundary.
709
         * next block size boundary.
693
         */
710
         */
694
        fat_fill_gap(nodep, FAT_CLST_RES0, pos);
711
        fat_fill_gap(bb->data, nodep, FAT_CLST_RES0, pos);
695
        b = fat_block_get(nodep, pos / bps);
712
        b = fat_block_get(bb->data, nodep, pos / bps);
696
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
713
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
697
            bytes);
714
            bytes);
698
        b->dirty = true;        /* need to sync block */
715
        b->dirty = true;        /* need to sync block */
699
        block_put(b);
716
        block_put(b);
700
        if (pos + bytes > nodep->size) {
717
        if (pos + bytes > nodep->size) {
701
            nodep->size = pos + bytes;
718
            nodep->size = pos + bytes;
702
            nodep->dirty = true;    /* need to sync node */
719
            nodep->dirty = true;    /* need to sync node */
703
        }
720
        }
704
        fat_node_put(nodep);
721
        fat_node_put(nodep);
-
 
722
        block_put(bb);
705
        ipc_answer_1(rid, EOK, bytes); 
723
        ipc_answer_1(rid, EOK, bytes); 
706
        return;
724
        return;
707
    } else {
725
    } else {
708
        /*
726
        /*
709
         * This is the more difficult case. We must allocate new
727
         * This is the more difficult case. We must allocate new
Line 714... Line 732...
714
        fat_cluster_t mcl, lcl;
732
        fat_cluster_t mcl, lcl;
715
   
733
   
716
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
734
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
717
            bps * spc;
735
            bps * spc;
718
        /* create an independent chain of nclsts clusters in all FATs */
736
        /* create an independent chain of nclsts clusters in all FATs */
719
        status = fat_alloc_clusters(dev_handle, nclsts, &mcl, &lcl);
737
        status = fat_alloc_clusters(bb->data, dev_handle, nclsts, &mcl,
-
 
738
            &lcl);
720
        if (status != EOK) {
739
        if (status != EOK) {
721
            /* could not allocate a chain of nclsts clusters */
740
            /* could not allocate a chain of nclsts clusters */
722
            fat_node_put(nodep);
741
            fat_node_put(nodep);
-
 
742
            block_put(bb);
723
            ipc_answer_0(callid, status);
743
            ipc_answer_0(callid, status);
724
            ipc_answer_0(rid, status);
744
            ipc_answer_0(rid, status);
725
            return;
745
            return;
726
        }
746
        }
727
        /* zero fill any gaps */
747
        /* zero fill any gaps */
728
        fat_fill_gap(nodep, mcl, pos);
748
        fat_fill_gap(bb->data, nodep, mcl, pos);
729
        b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc);
749
        b = _fat_block_get(bb->data, dev_handle, lcl,
-
 
750
            (pos / bps) % spc);
730
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
751
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
731
            bytes);
752
            bytes);
732
        b->dirty = true;        /* need to sync block */
753
        b->dirty = true;        /* need to sync block */
733
        block_put(b);
754
        block_put(b);
734
        /*
755
        /*
735
         * Append the cluster chain starting in mcl to the end of the
756
         * Append the cluster chain starting in mcl to the end of the
736
         * node's cluster chain.
757
         * node's cluster chain.
737
         */
758
         */
738
        fat_append_clusters(nodep, mcl);
759
        fat_append_clusters(bb->data, nodep, mcl);
739
        nodep->size = pos + bytes;
760
        nodep->size = pos + bytes;
740
        nodep->dirty = true;        /* need to sync node */
761
        nodep->dirty = true;        /* need to sync node */
741
        fat_node_put(nodep);
762
        fat_node_put(nodep);
-
 
763
        block_put(bb);
742
        ipc_answer_1(rid, EOK, bytes);
764
        ipc_answer_1(rid, EOK, bytes);
743
        return;
765
        return;
744
    }
766
    }
745
}
767
}
746
 
768