Subversion Repositories HelenOS

Rev

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

Rev 3526 Rev 3530
Line 73... Line 73...
73
    node->dirty = false;
73
    node->dirty = false;
74
}
74
}
75
 
75
 
76
static void fat_node_sync(fat_node_t *node)
76
static void fat_node_sync(fat_node_t *node)
77
{
77
{
78
    block_t *bb, *b;
78
    block_t *b;
-
 
79
    fat_bs_t *bs;
79
    fat_dentry_t *d;
80
    fat_dentry_t *d;
80
    uint16_t bps;
81
    uint16_t bps;
81
    unsigned dps;
82
    unsigned dps;
82
   
83
   
83
    assert(node->dirty);
84
    assert(node->dirty);
84
 
85
 
85
    bb = block_get(node->idx->dev_handle, BS_BLOCK, BS_SIZE);
86
    bs = block_bb_get(node->idx->dev_handle);
86
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
87
    bps = uint16_t_le2host(bs->bps);
87
    dps = bps / sizeof(fat_dentry_t);
88
    dps = bps / sizeof(fat_dentry_t);
88
   
89
   
89
    /* Read the block that contains the dentry of interest. */
90
    /* Read the block that contains the dentry of interest. */
90
    b = _fat_block_get(bb->data, node->idx->dev_handle, node->idx->pfc,
91
    b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc,
91
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps);
92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps);
92
 
93
 
93
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
94
 
95
 
95
    d->firstc = host2uint16_t_le(node->firstc);
96
    d->firstc = host2uint16_t_le(node->firstc);
Line 97... Line 98...
97
        d->size = host2uint32_t_le(node->size);
98
        d->size = host2uint32_t_le(node->size);
98
    /* TODO: update other fields? (e.g time fields, attr field) */
99
    /* TODO: update other fields? (e.g time fields, attr field) */
99
   
100
   
100
    b->dirty = true;        /* need to sync block */
101
    b->dirty = true;        /* need to sync block */
101
    block_put(b);
102
    block_put(b);
102
    block_put(bb);
-
 
103
}
103
}
104
 
104
 
105
/** Internal version of fat_node_get().
105
/** Internal version of fat_node_get().
106
 *
106
 *
107
 * @param idxp      Locked index structure.
107
 * @param idxp      Locked index structure.
108
 */
108
 */
109
static void *fat_node_get_core(fat_idx_t *idxp)
109
static void *fat_node_get_core(fat_idx_t *idxp)
110
{
110
{
111
    block_t *bb, *b;
111
    block_t *b;
-
 
112
    fat_bs_t *bs;
112
    fat_dentry_t *d;
113
    fat_dentry_t *d;
113
    fat_node_t *nodep = NULL;
114
    fat_node_t *nodep = NULL;
114
    unsigned bps;
115
    unsigned bps;
115
    unsigned dps;
116
    unsigned dps;
116
 
117
 
Line 159... Line 160...
159
        if (!nodep)
160
        if (!nodep)
160
            return NULL;
161
            return NULL;
161
    }
162
    }
162
    fat_node_initialize(nodep);
163
    fat_node_initialize(nodep);
163
 
164
 
164
    bb = block_get(idxp->dev_handle, BS_BLOCK, BS_SIZE);
165
    bs = block_bb_get(idxp->dev_handle);
165
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
166
    bps = uint16_t_le2host(bs->bps);
166
    dps = bps / sizeof(fat_dentry_t);
167
    dps = bps / sizeof(fat_dentry_t);
167
 
168
 
168
    /* Read the block that contains the dentry of interest. */
169
    /* Read the block that contains the dentry of interest. */
169
    b = _fat_block_get(bb->data, idxp->dev_handle, idxp->pfc,
170
    b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
170
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
171
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
171
    assert(b);
172
    assert(b);
172
 
173
 
173
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
174
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
174
    if (d->attr & FAT_ATTR_SUBDIR) {
175
    if (d->attr & FAT_ATTR_SUBDIR) {
Line 181... Line 182...
181
        /*
182
        /*
182
         * Unfortunately, the 'size' field of the FAT dentry is not
183
         * Unfortunately, the 'size' field of the FAT dentry is not
183
         * defined for the directory entry type. We must determine the
184
         * defined for the directory entry type. We must determine the
184
         * size of the directory by walking the FAT.
185
         * size of the directory by walking the FAT.
185
         */
186
         */
186
        nodep->size = bps * _fat_blcks_get(bb->data, idxp->dev_handle,
187
        nodep->size = bps * _fat_blcks_get(bs, idxp->dev_handle,
187
            uint16_t_le2host(d->firstc), NULL);
188
            uint16_t_le2host(d->firstc), NULL);
188
    } else {
189
    } else {
189
        nodep->type = FAT_FILE;
190
        nodep->type = FAT_FILE;
190
        nodep->size = uint32_t_le2host(d->size);
191
        nodep->size = uint32_t_le2host(d->size);
191
    }
192
    }
192
    nodep->firstc = uint16_t_le2host(d->firstc);
193
    nodep->firstc = uint16_t_le2host(d->firstc);
193
    nodep->lnkcnt = 1;
194
    nodep->lnkcnt = 1;
194
    nodep->refcnt = 1;
195
    nodep->refcnt = 1;
195
 
196
 
196
    block_put(b);
197
    block_put(b);
197
    block_put(bb);
-
 
198
 
198
 
199
    /* Link the idx structure with the node structure. */
199
    /* Link the idx structure with the node structure. */
200
    nodep->idx = idxp;
200
    nodep->idx = idxp;
201
    idxp->nodep = nodep;
201
    idxp->nodep = nodep;
202
 
202
 
Line 251... Line 251...
251
    return ENOTSUP; /* not supported at the moment */
251
    return ENOTSUP; /* not supported at the moment */
252
}
252
}
253
 
253
 
254
static void *fat_match(void *prnt, const char *component)
254
static void *fat_match(void *prnt, const char *component)
255
{
255
{
-
 
256
    fat_bs_t *bs;
256
    fat_node_t *parentp = (fat_node_t *)prnt;
257
    fat_node_t *parentp = (fat_node_t *)prnt;
257
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
258
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
258
    unsigned i, j;
259
    unsigned i, j;
259
    unsigned bps;       /* bytes per sector */
260
    unsigned bps;       /* bytes per sector */
260
    unsigned dps;       /* dentries per sector */
261
    unsigned dps;       /* dentries per sector */
261
    unsigned blocks;
262
    unsigned blocks;
262
    fat_dentry_t *d;
263
    fat_dentry_t *d;
263
    block_t *bb, *b;
264
    block_t *b;
264
 
265
 
265
    futex_down(&parentp->idx->lock);
266
    futex_down(&parentp->idx->lock);
266
    bb = block_get(parentp->idx->dev_handle, BS_BLOCK, BS_SIZE);
267
    bs = block_bb_get(parentp->idx->dev_handle);
267
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
268
    bps = uint16_t_le2host(bs->bps);
268
    dps = bps / sizeof(fat_dentry_t);
269
    dps = bps / sizeof(fat_dentry_t);
269
    blocks = parentp->size / bps;
270
    blocks = parentp->size / bps;
270
    for (i = 0; i < blocks; i++) {
271
    for (i = 0; i < blocks; i++) {
271
        b = fat_block_get(bb->data, parentp, i);
272
        b = fat_block_get(bs, parentp, i);
272
        for (j = 0; j < dps; j++) {
273
        for (j = 0; j < dps; j++) {
273
            d = ((fat_dentry_t *)b->data) + j;
274
            d = ((fat_dentry_t *)b->data) + j;
274
            switch (fat_classify_dentry(d)) {
275
            switch (fat_classify_dentry(d)) {
275
            case FAT_DENTRY_SKIP:
276
            case FAT_DENTRY_SKIP:
276
                continue;
277
                continue;
277
            case FAT_DENTRY_LAST:
278
            case FAT_DENTRY_LAST:
278
                block_put(b);
279
                block_put(b);
279
                block_put(bb);
-
 
280
                futex_up(&parentp->idx->lock);
280
                futex_up(&parentp->idx->lock);
281
                return NULL;
281
                return NULL;
282
            default:
282
            default:
283
            case FAT_DENTRY_VALID:
283
            case FAT_DENTRY_VALID:
284
                dentry_name_canonify(d, name);
284
                dentry_name_canonify(d, name);
Line 301... Line 301...
301
                    /*
301
                    /*
302
                     * Can happen if memory is low or if we
302
                     * Can happen if memory is low or if we
303
                     * run out of 32-bit indices.
303
                     * run out of 32-bit indices.
304
                     */
304
                     */
305
                    block_put(b);
305
                    block_put(b);
306
                    block_put(bb);
-
 
307
                    return NULL;
306
                    return NULL;
308
                }
307
                }
309
                node = fat_node_get_core(idx);
308
                node = fat_node_get_core(idx);
310
                futex_up(&idx->lock);
309
                futex_up(&idx->lock);
311
                block_put(b);
310
                block_put(b);
312
                block_put(bb);
-
 
313
                return node;
311
                return node;
314
            }
312
            }
315
        }
313
        }
316
        block_put(b);
314
        block_put(b);
317
    }
315
    }
318
    block_put(bb);
-
 
319
 
316
 
320
    futex_up(&parentp->idx->lock);
317
    futex_up(&parentp->idx->lock);
321
    return NULL;
318
    return NULL;
322
}
319
}
323
 
320
 
Line 339... Line 336...
339
    return ((fat_node_t *)node)->lnkcnt;
336
    return ((fat_node_t *)node)->lnkcnt;
340
}
337
}
341
 
338
 
342
static bool fat_has_children(void *node)
339
static bool fat_has_children(void *node)
343
{
340
{
-
 
341
    fat_bs_t *bs;
344
    fat_node_t *nodep = (fat_node_t *)node;
342
    fat_node_t *nodep = (fat_node_t *)node;
345
    unsigned bps;
343
    unsigned bps;
346
    unsigned dps;
344
    unsigned dps;
347
    unsigned blocks;
345
    unsigned blocks;
348
    block_t *bb, *b;
346
    block_t *b;
349
    unsigned i, j;
347
    unsigned i, j;
350
 
348
 
351
    if (nodep->type != FAT_DIRECTORY)
349
    if (nodep->type != FAT_DIRECTORY)
352
        return false;
350
        return false;
353
   
351
   
354
    futex_down(&nodep->idx->lock);
352
    futex_down(&nodep->idx->lock);
355
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE);
353
    bs = block_bb_get(nodep->idx->dev_handle);
356
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
354
    bps = uint16_t_le2host(bs->bps);
357
    dps = bps / sizeof(fat_dentry_t);
355
    dps = bps / sizeof(fat_dentry_t);
358
 
356
 
359
    blocks = nodep->size / bps;
357
    blocks = nodep->size / bps;
360
 
358
 
361
    for (i = 0; i < blocks; i++) {
359
    for (i = 0; i < blocks; i++) {
362
        fat_dentry_t *d;
360
        fat_dentry_t *d;
363
   
361
   
364
        b = fat_block_get(bb->data, nodep, i);
362
        b = fat_block_get(bs, nodep, i);
365
        for (j = 0; j < dps; j++) {
363
        for (j = 0; j < dps; j++) {
366
            d = ((fat_dentry_t *)b->data) + j;
364
            d = ((fat_dentry_t *)b->data) + j;
367
            switch (fat_classify_dentry(d)) {
365
            switch (fat_classify_dentry(d)) {
368
            case FAT_DENTRY_SKIP:
366
            case FAT_DENTRY_SKIP:
369
                continue;
367
                continue;
370
            case FAT_DENTRY_LAST:
368
            case FAT_DENTRY_LAST:
371
                block_put(b);
369
                block_put(b);
372
                block_put(bb);
-
 
373
                futex_up(&nodep->idx->lock);
370
                futex_up(&nodep->idx->lock);
374
                return false;
371
                return false;
375
            default:
372
            default:
376
            case FAT_DENTRY_VALID:
373
            case FAT_DENTRY_VALID:
377
                block_put(b);
374
                block_put(b);
378
                block_put(bb);
-
 
379
                futex_up(&nodep->idx->lock);
375
                futex_up(&nodep->idx->lock);
380
                return true;
376
                return true;
381
            }
377
            }
382
            block_put(b);
378
            block_put(b);
383
            block_put(bb);
-
 
384
            futex_up(&nodep->idx->lock);
379
            futex_up(&nodep->idx->lock);
385
            return true;
380
            return true;
386
        }
381
        }
387
        block_put(b);
382
        block_put(b);
388
    }
383
    }
389
    block_put(bb);
-
 
390
 
384
 
391
    futex_up(&nodep->idx->lock);
385
    futex_up(&nodep->idx->lock);
392
    return false;
386
    return false;
393
}
387
}
394
 
388
 
Line 432... Line 426...
432
};
426
};
433
 
427
 
434
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
428
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
435
{
429
{
436
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
430
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
437
    block_t *bb;
431
    fat_bs_t *bs;
438
    uint16_t bps;
432
    uint16_t bps;
439
    uint16_t rde;
433
    uint16_t rde;
440
    int rc;
434
    int rc;
441
 
435
 
442
    /*
-
 
443
     * For now, we don't bother to remember dev_handle, dev_phone or
-
 
444
     * dev_buffer in some data structure. We use global variables because we
-
 
445
     * know there will be at most one mount on this file system.
-
 
446
     * Of course, this is a huge TODO item.
436
    /* initialize libblock */
447
     */
-
 
448
    dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
437
    rc = block_init(dev_handle, BS_SIZE, BS_BLOCK * BS_SIZE, BS_SIZE);
449
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
-
 
450
   
-
 
451
    if (!dev_buffer) {
-
 
452
        ipc_answer_0(rid, ENOMEM);
-
 
453
        return;
-
 
454
    }
-
 
455
 
-
 
456
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
-
 
457
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
-
 
458
 
-
 
459
    if (dev_phone < 0) {
-
 
460
        munmap(dev_buffer, BS_SIZE);
-
 
461
        ipc_answer_0(rid, dev_phone);
-
 
462
        return;
-
 
463
    }
-
 
464
 
-
 
465
    rc = ipc_share_out_start(dev_phone, dev_buffer,
-
 
466
        AS_AREA_READ | AS_AREA_WRITE);
-
 
467
    if (rc != EOK) {
438
    if (rc != EOK) {
468
            munmap(dev_buffer, BS_SIZE);
-
 
469
        ipc_answer_0(rid, rc);
439
        ipc_answer_0(rid, 0);
470
        return;
440
        return;
471
    }
441
    }
472
 
442
 
-
 
443
    /* get the buffer with the boot sector */
-
 
444
    bs = block_bb_get(dev_handle);
-
 
445
   
473
    /* Read the number of root directory entries. */
446
    /* Read the number of root directory entries. */
474
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
-
 
475
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
447
    bps = uint16_t_le2host(bs->bps);
476
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
448
    rde = uint16_t_le2host(bs->root_ent_max);
477
    block_put(bb);
-
 
478
 
449
 
479
    if (bps != BS_SIZE) {
450
    if (bps != BS_SIZE) {
480
        munmap(dev_buffer, BS_SIZE);
451
        block_fini(dev_handle);
481
        ipc_answer_0(rid, ENOTSUP);
452
        ipc_answer_0(rid, ENOTSUP);
482
        return;
453
        return;
483
    }
454
    }
484
 
455
 
485
    rc = fat_idx_init_by_dev_handle(dev_handle);
456
    rc = fat_idx_init_by_dev_handle(dev_handle);
486
    if (rc != EOK) {
457
    if (rc != EOK) {
487
            munmap(dev_buffer, BS_SIZE);
458
        block_fini(dev_handle);
488
        ipc_answer_0(rid, rc);
459
        ipc_answer_0(rid, rc);
489
        return;
460
        return;
490
    }
461
    }
491
 
462
 
492
    /* Initialize the root node. */
463
    /* Initialize the root node. */
493
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
464
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
494
    if (!rootp) {
465
    if (!rootp) {
495
            munmap(dev_buffer, BS_SIZE);
466
        block_fini(dev_handle);
496
        fat_idx_fini_by_dev_handle(dev_handle);
467
        fat_idx_fini_by_dev_handle(dev_handle);
497
        ipc_answer_0(rid, ENOMEM);
468
        ipc_answer_0(rid, ENOMEM);
498
        return;
469
        return;
499
    }
470
    }
500
    fat_node_initialize(rootp);
471
    fat_node_initialize(rootp);
501
 
472
 
502
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
473
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
503
    if (!ridxp) {
474
    if (!ridxp) {
504
            munmap(dev_buffer, BS_SIZE);
475
        block_fini(dev_handle);
505
        free(rootp);
476
        free(rootp);
506
        fat_idx_fini_by_dev_handle(dev_handle);
477
        fat_idx_fini_by_dev_handle(dev_handle);
507
        ipc_answer_0(rid, ENOMEM);
478
        ipc_answer_0(rid, ENOMEM);
508
        return;
479
        return;
509
    }
480
    }
Line 537... Line 508...
537
{
508
{
538
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
509
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
539
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
510
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
540
    off_t pos = (off_t)IPC_GET_ARG3(*request);
511
    off_t pos = (off_t)IPC_GET_ARG3(*request);
541
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
512
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
-
 
513
    fat_bs_t *bs;
542
    uint16_t bps;
514
    uint16_t bps;
543
    size_t bytes;
515
    size_t bytes;
544
    block_t *bb, *b;
516
    block_t *b;
545
 
517
 
546
    if (!nodep) {
518
    if (!nodep) {
547
        ipc_answer_0(rid, ENOENT);
519
        ipc_answer_0(rid, ENOENT);
548
        return;
520
        return;
549
    }
521
    }
Line 555... Line 527...
555
        ipc_answer_0(callid, EINVAL);
527
        ipc_answer_0(callid, EINVAL);
556
        ipc_answer_0(rid, EINVAL);
528
        ipc_answer_0(rid, EINVAL);
557
        return;
529
        return;
558
    }
530
    }
559
 
531
 
560
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
532
    bs = block_bb_get(dev_handle);
561
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
533
    bps = uint16_t_le2host(bs->bps);
562
 
534
 
563
    if (nodep->type == FAT_FILE) {
535
    if (nodep->type == FAT_FILE) {
564
        /*
536
        /*
565
         * Our strategy for regular file reads is to read one block at
537
         * Our strategy for regular file reads is to read one block at
566
         * most and make use of the possibility to return less data than
538
         * most and make use of the possibility to return less data than
567
         * requested. This keeps the code very simple.
539
         * requested. This keeps the code very simple.
568
         */
540
         */
569
        bytes = min(len, bps - pos % bps);
541
        bytes = min(len, bps - pos % bps);
570
        b = fat_block_get(bb->data, nodep, pos / bps);
542
        b = fat_block_get(bs, nodep, pos / bps);
571
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
543
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
572
            bytes);
544
            bytes);
573
        block_put(b);
545
        block_put(b);
574
    } else {
546
    } else {
575
        unsigned bnum;
547
        unsigned bnum;
Line 589... Line 561...
589
         */
561
         */
590
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
562
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
591
        while (bnum < nodep->size / bps) {
563
        while (bnum < nodep->size / bps) {
592
            off_t o;
564
            off_t o;
593
 
565
 
594
            b = fat_block_get(bb->data, nodep, bnum);
566
            b = fat_block_get(bs, nodep, bnum);
595
            for (o = pos % (bps / sizeof(fat_dentry_t));
567
            for (o = pos % (bps / sizeof(fat_dentry_t));
596
                o < bps / sizeof(fat_dentry_t);
568
                o < bps / sizeof(fat_dentry_t);
597
                o++, pos++) {
569
                o++, pos++) {
598
                d = ((fat_dentry_t *)b->data) + o;
570
                d = ((fat_dentry_t *)b->data) + o;
599
                switch (fat_classify_dentry(d)) {
571
                switch (fat_classify_dentry(d)) {
Line 612... Line 584...
612
            block_put(b);
584
            block_put(b);
613
            bnum++;
585
            bnum++;
614
        }
586
        }
615
miss:
587
miss:
616
        fat_node_put(nodep);
588
        fat_node_put(nodep);
617
        block_put(bb);
-
 
618
        ipc_answer_0(callid, ENOENT);
589
        ipc_answer_0(callid, ENOENT);
619
        ipc_answer_1(rid, ENOENT, 0);
590
        ipc_answer_1(rid, ENOENT, 0);
620
        return;
591
        return;
621
hit:
592
hit:
622
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
593
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
623
        bytes = (pos - spos) + 1;
594
        bytes = (pos - spos) + 1;
624
    }
595
    }
625
 
596
 
626
    fat_node_put(nodep);
597
    fat_node_put(nodep);
627
    block_put(bb);
-
 
628
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
598
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
629
}
599
}
630
 
600
 
631
void fat_write(ipc_callid_t rid, ipc_call_t *request)
601
void fat_write(ipc_callid_t rid, ipc_call_t *request)
632
{
602
{
633
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
603
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
634
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
604
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
635
    off_t pos = (off_t)IPC_GET_ARG3(*request);
605
    off_t pos = (off_t)IPC_GET_ARG3(*request);
636
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
606
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
-
 
607
    fat_bs_t *bs;
637
    size_t bytes;
608
    size_t bytes;
638
    block_t *b, *bb;
609
    block_t *b;
639
    uint16_t bps;
610
    uint16_t bps;
640
    unsigned spc;
611
    unsigned spc;
641
    off_t boundary;
612
    off_t boundary;
642
   
613
   
643
    if (!nodep) {
614
    if (!nodep) {
Line 668... Line 639...
668
     * to do this because the client must be ready to handle the return
639
     * to do this because the client must be ready to handle the return
669
     * value signalizing a smaller number of bytes written.
640
     * value signalizing a smaller number of bytes written.
670
     */
641
     */
671
    bytes = min(len, bps - pos % bps);
642
    bytes = min(len, bps - pos % bps);
672
 
643
 
673
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
644
    bs = block_bb_get(dev_handle);
674
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
645
    bps = uint16_t_le2host(bs->bps);
675
    spc = FAT_BS(bb)->spc;
646
    spc = bs->spc;
676
   
647
   
677
    boundary = ROUND_UP(nodep->size, bps * spc);
648
    boundary = ROUND_UP(nodep->size, bps * spc);
678
    if (pos < boundary) {
649
    if (pos < boundary) {
679
        /*
650
        /*
680
         * This is the easier case - we are either overwriting already
651
         * This is the easier case - we are either overwriting already
681
         * existing contents or writing behind the EOF, but still within
652
         * existing contents or writing behind the EOF, but still within
682
         * the limits of the last cluster. The node size may grow to the
653
         * the limits of the last cluster. The node size may grow to the
683
         * next block size boundary.
654
         * next block size boundary.
684
         */
655
         */
685
        fat_fill_gap(bb->data, nodep, FAT_CLST_RES0, pos);
656
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
686
        b = fat_block_get(bb->data, nodep, pos / bps);
657
        b = fat_block_get(bs, nodep, pos / bps);
687
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
658
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
688
            bytes);
659
            bytes);
689
        b->dirty = true;        /* need to sync block */
660
        b->dirty = true;        /* need to sync block */
690
        block_put(b);
661
        block_put(b);
691
        if (pos + bytes > nodep->size) {
662
        if (pos + bytes > nodep->size) {
692
            nodep->size = pos + bytes;
663
            nodep->size = pos + bytes;
693
            nodep->dirty = true;    /* need to sync node */
664
            nodep->dirty = true;    /* need to sync node */
694
        }
665
        }
695
        fat_node_put(nodep);
666
        fat_node_put(nodep);
696
        block_put(bb);
-
 
697
        ipc_answer_1(rid, EOK, bytes); 
667
        ipc_answer_1(rid, EOK, bytes); 
698
        return;
668
        return;
699
    } else {
669
    } else {
700
        /*
670
        /*
701
         * This is the more difficult case. We must allocate new
671
         * This is the more difficult case. We must allocate new
Line 706... Line 676...
706
        fat_cluster_t mcl, lcl;
676
        fat_cluster_t mcl, lcl;
707
   
677
   
708
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
678
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
709
            bps * spc;
679
            bps * spc;
710
        /* create an independent chain of nclsts clusters in all FATs */
680
        /* create an independent chain of nclsts clusters in all FATs */
711
        status = fat_alloc_clusters(bb->data, dev_handle, nclsts, &mcl,
681
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl,
712
            &lcl);
682
            &lcl);
713
        if (status != EOK) {
683
        if (status != EOK) {
714
            /* could not allocate a chain of nclsts clusters */
684
            /* could not allocate a chain of nclsts clusters */
715
            fat_node_put(nodep);
685
            fat_node_put(nodep);
716
            block_put(bb);
-
 
717
            ipc_answer_0(callid, status);
686
            ipc_answer_0(callid, status);
718
            ipc_answer_0(rid, status);
687
            ipc_answer_0(rid, status);
719
            return;
688
            return;
720
        }
689
        }
721
        /* zero fill any gaps */
690
        /* zero fill any gaps */
722
        fat_fill_gap(bb->data, nodep, mcl, pos);
691
        fat_fill_gap(bs, nodep, mcl, pos);
723
        b = _fat_block_get(bb->data, dev_handle, lcl,
692
        b = _fat_block_get(bs, dev_handle, lcl,
724
            (pos / bps) % spc);
693
            (pos / bps) % spc);
725
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
694
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
726
            bytes);
695
            bytes);
727
        b->dirty = true;        /* need to sync block */
696
        b->dirty = true;        /* need to sync block */
728
        block_put(b);
697
        block_put(b);
729
        /*
698
        /*
730
         * Append the cluster chain starting in mcl to the end of the
699
         * Append the cluster chain starting in mcl to the end of the
731
         * node's cluster chain.
700
         * node's cluster chain.
732
         */
701
         */
733
        fat_append_clusters(bb->data, nodep, mcl);
702
        fat_append_clusters(bs, nodep, mcl);
734
        nodep->size = pos + bytes;
703
        nodep->size = pos + bytes;
735
        nodep->dirty = true;        /* need to sync node */
704
        nodep->dirty = true;        /* need to sync node */
736
        fat_node_put(nodep);
705
        fat_node_put(nodep);
737
        block_put(bb);
-
 
738
        ipc_answer_1(rid, EOK, bytes);
706
        ipc_answer_1(rid, EOK, bytes);
739
        return;
707
        return;
740
    }
708
    }
741
}
709
}
742
 
710