Subversion Repositories HelenOS

Rev

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

Rev 4305 Rev 4357
Line 53... Line 53...
53
#include <assert.h>
53
#include <assert.h>
54
#include <futex.h>
54
#include <futex.h>
55
#include <sys/mman.h>
55
#include <sys/mman.h>
56
#include <align.h>
56
#include <align.h>
57
 
57
 
-
 
58
#define FAT_NODE(node)  ((node) ? (fat_node_t *) (node)->data : NULL)
-
 
59
#define FS_NODE(node)   ((node) ? (node)->bp : NULL)
-
 
60
 
58
/** Futex protecting the list of cached free FAT nodes. */
61
/** Futex protecting the list of cached free FAT nodes. */
59
static futex_t ffn_futex = FUTEX_INITIALIZER;
62
static futex_t ffn_futex = FUTEX_INITIALIZER;
60
 
63
 
61
/** List of cached free FAT nodes. */
64
/** List of cached free FAT nodes. */
62
static LIST_INITIALIZE(ffn_head);
65
static LIST_INITIALIZE(ffn_head);
63
 
66
 
64
static void fat_node_initialize(fat_node_t *node)
67
static void fat_node_initialize(fat_node_t *node)
65
{
68
{
66
    futex_initialize(&node->lock, 1);
69
    futex_initialize(&node->lock, 1);
-
 
70
    node->bp = NULL;
67
    node->idx = NULL;
71
    node->idx = NULL;
68
    node->type = 0;
72
    node->type = 0;
69
    link_initialize(&node->ffn_link);
73
    link_initialize(&node->ffn_link);
70
    node->size = 0;
74
    node->size = 0;
71
    node->lnkcnt = 0;
75
    node->lnkcnt = 0;
Line 106... Line 110...
106
    block_put(b);
110
    block_put(b);
107
}
111
}
108
 
112
 
109
static fat_node_t *fat_node_get_new(void)
113
static fat_node_t *fat_node_get_new(void)
110
{
114
{
-
 
115
    fs_node_t *fn;
111
    fat_node_t *nodep;
116
    fat_node_t *nodep;
112
 
117
 
113
    futex_down(&ffn_futex);
118
    futex_down(&ffn_futex);
114
    if (!list_empty(&ffn_head)) {
119
    if (!list_empty(&ffn_head)) {
115
        /* Try to use a cached free node structure. */
120
        /* Try to use a cached free node structure. */
Line 127... Line 132...
127
        if (nodep->dirty)
132
        if (nodep->dirty)
128
            fat_node_sync(nodep);
133
            fat_node_sync(nodep);
129
        idxp_tmp->nodep = NULL;
134
        idxp_tmp->nodep = NULL;
130
        futex_up(&nodep->lock);
135
        futex_up(&nodep->lock);
131
        futex_up(&idxp_tmp->lock);
136
        futex_up(&idxp_tmp->lock);
-
 
137
        fn = FS_NODE(nodep);
132
    } else {
138
    } else {
133
skip_cache:
139
skip_cache:
134
        /* Try to allocate a new node structure. */
140
        /* Try to allocate a new node structure. */
135
        futex_up(&ffn_futex);
141
        futex_up(&ffn_futex);
-
 
142
        fn = (fs_node_t *)malloc(sizeof(fs_node_t));
-
 
143
        if (!fn)
-
 
144
            return NULL;
136
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
145
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
137
        if (!nodep)
146
        if (!nodep) {
-
 
147
            free(fn);
138
            return NULL;
148
            return NULL;
-
 
149
        }
139
    }
150
    }
140
    fat_node_initialize(nodep);
151
    fat_node_initialize(nodep);
-
 
152
    fn->data = nodep;
-
 
153
    nodep->bp = fn;
141
   
154
   
142
    return nodep;
155
    return nodep;
143
}
156
}
144
 
157
 
145
/** Internal version of fat_node_get().
158
/** Internal version of fat_node_get().
146
 *
159
 *
147
 * @param idxp      Locked index structure.
160
 * @param idxp      Locked index structure.
148
 */
161
 */
149
static void *fat_node_get_core(fat_idx_t *idxp)
162
static fat_node_t *fat_node_get_core(fat_idx_t *idxp)
150
{
163
{
151
    block_t *b;
164
    block_t *b;
152
    fat_bs_t *bs;
165
    fat_bs_t *bs;
153
    fat_dentry_t *d;
166
    fat_dentry_t *d;
154
    fat_node_t *nodep = NULL;
167
    fat_node_t *nodep = NULL;
Line 221... Line 234...
221
}
234
}
222
 
235
 
223
/*
236
/*
224
 * Forward declarations of FAT libfs operations.
237
 * Forward declarations of FAT libfs operations.
225
 */
238
 */
226
static void *fat_node_get(dev_handle_t, fs_index_t);
239
static fs_node_t *fat_node_get(dev_handle_t, fs_index_t);
227
static void fat_node_put(void *);
240
static void fat_node_put(fs_node_t *);
228
static void *fat_create_node(dev_handle_t, int);
241
static fs_node_t *fat_create_node(dev_handle_t, int);
229
static int fat_destroy_node(void *);
242
static int fat_destroy_node(fs_node_t *);
230
static int fat_link(void *, void *, const char *);
243
static int fat_link(fs_node_t *, fs_node_t *, const char *);
231
static int fat_unlink(void *, void *);
244
static int fat_unlink(fs_node_t *, fs_node_t *);
232
static void *fat_match(void *, const char *);
245
static fs_node_t *fat_match(fs_node_t *, const char *);
233
static fs_index_t fat_index_get(void *);
246
static fs_index_t fat_index_get(fs_node_t *);
234
static size_t fat_size_get(void *);
247
static size_t fat_size_get(fs_node_t *);
235
static unsigned fat_lnkcnt_get(void *);
248
static unsigned fat_lnkcnt_get(fs_node_t *);
236
static bool fat_has_children(void *);
249
static bool fat_has_children(fs_node_t *);
237
static void *fat_root_get(dev_handle_t);
250
static fs_node_t *fat_root_get(dev_handle_t);
238
static char fat_plb_get_char(unsigned);
251
static char fat_plb_get_char(unsigned);
239
static bool fat_is_directory(void *);
252
static bool fat_is_directory(fs_node_t *);
240
static bool fat_is_file(void *node);
253
static bool fat_is_file(fs_node_t *node);
241
 
254
 
242
/*
255
/*
243
 * FAT libfs operations.
256
 * FAT libfs operations.
244
 */
257
 */
245
 
258
 
246
/** Instantiate a FAT in-core node. */
259
/** Instantiate a FAT in-core node. */
247
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
260
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
248
{
261
{
249
    void *node;
262
    fat_node_t *nodep;
250
    fat_idx_t *idxp;
263
    fat_idx_t *idxp;
251
 
264
 
252
    idxp = fat_idx_get_by_index(dev_handle, index);
265
    idxp = fat_idx_get_by_index(dev_handle, index);
253
    if (!idxp)
266
    if (!idxp)
254
        return NULL;
267
        return NULL;
255
    /* idxp->lock held */
268
    /* idxp->lock held */
256
    node = fat_node_get_core(idxp);
269
    nodep = fat_node_get_core(idxp);
257
    futex_up(&idxp->lock);
270
    futex_up(&idxp->lock);
258
    return node;
271
    return FS_NODE(nodep);
259
}
272
}
260
 
273
 
261
void fat_node_put(void *node)
274
void fat_node_put(fs_node_t *fn)
262
{
275
{
263
    fat_node_t *nodep = (fat_node_t *)node;
276
    fat_node_t *nodep = FAT_NODE(fn);
264
    bool destroy = false;
277
    bool destroy = false;
265
 
278
 
266
    futex_down(&nodep->lock);
279
    futex_down(&nodep->lock);
267
    if (!--nodep->refcnt) {
280
    if (!--nodep->refcnt) {
268
        if (nodep->idx) {
281
        if (nodep->idx) {
Line 278... Line 291...
278
             */
291
             */
279
            destroy = true;
292
            destroy = true;
280
        }
293
        }
281
    }
294
    }
282
    futex_up(&nodep->lock);
295
    futex_up(&nodep->lock);
283
    if (destroy)
296
    if (destroy) {
-
 
297
        free(nodep->bp);
284
        free(node);
298
        free(nodep);
-
 
299
    }
285
}
300
}
286
 
301
 
287
void *fat_create_node(dev_handle_t dev_handle, int flags)
302
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags)
288
{
303
{
289
    fat_idx_t *idxp;
304
    fat_idx_t *idxp;
290
    fat_node_t *nodep;
305
    fat_node_t *nodep;
291
    fat_bs_t *bs;
306
    fat_bs_t *bs;
292
    fat_cluster_t mcl, lcl;
307
    fat_cluster_t mcl, lcl;
Line 308... Line 323...
308
        return NULL;
323
        return NULL;
309
    }
324
    }
310
    idxp = fat_idx_get_new(dev_handle);
325
    idxp = fat_idx_get_new(dev_handle);
311
    if (!idxp) {
326
    if (!idxp) {
312
        fat_free_clusters(bs, dev_handle, mcl);
327
        fat_free_clusters(bs, dev_handle, mcl);
313
        fat_node_put(nodep);
328
        fat_node_put(FS_NODE(nodep));
314
        return NULL;
329
        return NULL;
315
    }
330
    }
316
    /* idxp->lock held */
331
    /* idxp->lock held */
317
    if (flags & L_DIRECTORY) {
332
    if (flags & L_DIRECTORY) {
318
        int i;
333
        int i;
Line 343... Line 358...
343
 
358
 
344
    nodep->idx = idxp;
359
    nodep->idx = idxp;
345
    idxp->nodep = nodep;
360
    idxp->nodep = nodep;
346
 
361
 
347
    futex_up(&idxp->lock);
362
    futex_up(&idxp->lock);
348
    return nodep;
363
    return FS_NODE(nodep);
349
}
364
}
350
 
365
 
351
int fat_destroy_node(void *node)
366
int fat_destroy_node(fs_node_t *fn)
352
{
367
{
353
    fat_node_t *nodep = (fat_node_t *)node;
368
    fat_node_t *nodep = FAT_NODE(fn);
354
    fat_bs_t *bs;
369
    fat_bs_t *bs;
355
 
370
 
356
    /*
371
    /*
357
     * The node is not reachable from the file system. This means that the
372
     * The node is not reachable from the file system. This means that the
358
     * link count should be zero and that the index structure cannot be
373
     * link count should be zero and that the index structure cannot be
Line 362... Line 377...
362
    assert(nodep->lnkcnt == 0);
377
    assert(nodep->lnkcnt == 0);
363
 
378
 
364
    /*
379
    /*
365
     * The node may not have any children.
380
     * The node may not have any children.
366
     */
381
     */
367
    assert(fat_has_children(node) == false);
382
    assert(fat_has_children(fn) == false);
368
 
383
 
369
    bs = block_bb_get(nodep->idx->dev_handle);
384
    bs = block_bb_get(nodep->idx->dev_handle);
370
    if (nodep->firstc != FAT_CLST_RES0) {
385
    if (nodep->firstc != FAT_CLST_RES0) {
371
        assert(nodep->size);
386
        assert(nodep->size);
372
        /* Free all clusters allocated to the node. */
387
        /* Free all clusters allocated to the node. */
373
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
388
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
374
    }
389
    }
375
 
390
 
376
    fat_idx_destroy(nodep->idx);
391
    fat_idx_destroy(nodep->idx);
-
 
392
    free(nodep->bp);
377
    free(nodep);
393
    free(nodep);
378
    return EOK;
394
    return EOK;
379
}
395
}
380
 
396
 
381
int fat_link(void *prnt, void *chld, const char *name)
397
int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
382
{
398
{
383
    fat_node_t *parentp = (fat_node_t *)prnt;
399
    fat_node_t *parentp = FAT_NODE(pfn);
384
    fat_node_t *childp = (fat_node_t *)chld;
400
    fat_node_t *childp = FAT_NODE(cfn);
385
    fat_dentry_t *d;
401
    fat_dentry_t *d;
386
    fat_bs_t *bs;
402
    fat_bs_t *bs;
387
    block_t *b;
403
    block_t *b;
388
    int i, j;
404
    int i, j;
389
    uint16_t bps;
405
    uint16_t bps;
Line 525... Line 541...
525
    fat_idx_hashin(childp->idx);
541
    fat_idx_hashin(childp->idx);
526
 
542
 
527
    return EOK;
543
    return EOK;
528
}
544
}
529
 
545
 
530
int fat_unlink(void *prnt, void *chld)
546
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn)
531
{
547
{
532
    fat_node_t *parentp = (fat_node_t *)prnt;
548
    fat_node_t *parentp = FAT_NODE(pfn);
533
    fat_node_t *childp = (fat_node_t *)chld;
549
    fat_node_t *childp = FAT_NODE(cfn);
534
    fat_bs_t *bs;
550
    fat_bs_t *bs;
535
    fat_dentry_t *d;
551
    fat_dentry_t *d;
536
    uint16_t bps;
552
    uint16_t bps;
537
    block_t *b;
553
    block_t *b;
538
 
554
 
Line 565... Line 581...
565
    futex_up(&parentp->lock);
581
    futex_up(&parentp->lock);
566
 
582
 
567
    return EOK;
583
    return EOK;
568
}
584
}
569
 
585
 
570
void *fat_match(void *prnt, const char *component)
586
fs_node_t *fat_match(fs_node_t *pfn, const char *component)
571
{
587
{
572
    fat_bs_t *bs;
588
    fat_bs_t *bs;
573
    fat_node_t *parentp = (fat_node_t *)prnt;
589
    fat_node_t *parentp = FAT_NODE(pfn);
574
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
590
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
575
    unsigned i, j;
591
    unsigned i, j;
576
    unsigned bps;       /* bytes per sector */
592
    unsigned bps;       /* bytes per sector */
577
    unsigned dps;       /* dentries per sector */
593
    unsigned dps;       /* dentries per sector */
578
    unsigned blocks;
594
    unsigned blocks;
Line 601... Line 617...
601
                fat_dentry_name_get(d, name);
617
                fat_dentry_name_get(d, name);
602
                break;
618
                break;
603
            }
619
            }
604
            if (fat_dentry_namecmp(name, component) == 0) {
620
            if (fat_dentry_namecmp(name, component) == 0) {
605
                /* hit */
621
                /* hit */
606
                void *node;
622
                fat_node_t *nodep;
607
                /*
623
                /*
608
                 * Assume tree hierarchy for locking.  We
624
                 * Assume tree hierarchy for locking.  We
609
                 * already have the parent and now we are going
625
                 * already have the parent and now we are going
610
                 * to lock the child.  Never lock in the oposite
626
                 * to lock the child.  Never lock in the oposite
611
                 * order.
627
                 * order.
Line 620... Line 636...
620
                     * run out of 32-bit indices.
636
                     * run out of 32-bit indices.
621
                     */
637
                     */
622
                    block_put(b);
638
                    block_put(b);
623
                    return NULL;
639
                    return NULL;
624
                }
640
                }
625
                node = fat_node_get_core(idx);
641
                nodep = fat_node_get_core(idx);
626
                futex_up(&idx->lock);
642
                futex_up(&idx->lock);
627
                block_put(b);
643
                block_put(b);
628
                return node;
644
                return FS_NODE(nodep);
629
            }
645
            }
630
        }
646
        }
631
        block_put(b);
647
        block_put(b);
632
    }
648
    }
633
 
649
 
634
    futex_up(&parentp->idx->lock);
650
    futex_up(&parentp->idx->lock);
635
    return NULL;
651
    return NULL;
636
}
652
}
637
 
653
 
638
fs_index_t fat_index_get(void *node)
654
fs_index_t fat_index_get(fs_node_t *fn)
639
{
655
{
640
    fat_node_t *fnodep = (fat_node_t *)node;
-
 
641
    if (!fnodep)
-
 
642
        return 0;
-
 
643
    return fnodep->idx->index;
656
    return FAT_NODE(fn)->idx->index;
644
}
657
}
645
 
658
 
646
size_t fat_size_get(void *node)
659
size_t fat_size_get(fs_node_t *fn)
647
{
660
{
648
    return ((fat_node_t *)node)->size;
661
    return FAT_NODE(fn)->size;
649
}
662
}
650
 
663
 
651
unsigned fat_lnkcnt_get(void *node)
664
unsigned fat_lnkcnt_get(fs_node_t *fn)
652
{
665
{
653
    return ((fat_node_t *)node)->lnkcnt;
666
    return FAT_NODE(fn)->lnkcnt;
654
}
667
}
655
 
668
 
656
bool fat_has_children(void *node)
669
bool fat_has_children(fs_node_t *fn)
657
{
670
{
658
    fat_bs_t *bs;
671
    fat_bs_t *bs;
659
    fat_node_t *nodep = (fat_node_t *)node;
672
    fat_node_t *nodep = FAT_NODE(fn);
660
    unsigned bps;
673
    unsigned bps;
661
    unsigned dps;
674
    unsigned dps;
662
    unsigned blocks;
675
    unsigned blocks;
663
    block_t *b;
676
    block_t *b;
664
    unsigned i, j;
677
    unsigned i, j;
Line 702... Line 715...
702
 
715
 
703
    futex_up(&nodep->idx->lock);
716
    futex_up(&nodep->idx->lock);
704
    return false;
717
    return false;
705
}
718
}
706
 
719
 
707
void *fat_root_get(dev_handle_t dev_handle)
720
fs_node_t *fat_root_get(dev_handle_t dev_handle)
708
{
721
{
709
    return fat_node_get(dev_handle, 0);
722
    return fat_node_get(dev_handle, 0);
710
}
723
}
711
 
724
 
712
char fat_plb_get_char(unsigned pos)
725
char fat_plb_get_char(unsigned pos)
713
{
726
{
714
    return fat_reg.plb_ro[pos % PLB_SIZE];
727
    return fat_reg.plb_ro[pos % PLB_SIZE];
715
}
728
}
716
 
729
 
717
bool fat_is_directory(void *node)
730
bool fat_is_directory(fs_node_t *fn)
718
{
731
{
719
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
732
    return FAT_NODE(fn)->type == FAT_DIRECTORY;
720
}
733
}
721
 
734
 
722
bool fat_is_file(void *node)
735
bool fat_is_file(fs_node_t *fn)
723
{
736
{
724
    return ((fat_node_t *)node)->type == FAT_FILE;
737
    return FAT_NODE(fn)->type == FAT_FILE;
725
}
738
}
726
 
739
 
727
/** libfs operations */
740
/** libfs operations */
728
libfs_ops_t fat_libfs_ops = {
741
libfs_ops_t fat_libfs_ops = {
729
    .match = fat_match,
742
    .match = fat_match,
Line 819... Line 832...
819
        ipc_answer_0(rid, rc);
832
        ipc_answer_0(rid, rc);
820
        return;
833
        return;
821
    }
834
    }
822
 
835
 
823
    /* Initialize the root node. */
836
    /* Initialize the root node. */
-
 
837
    fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
-
 
838
    if (!rfn) {
-
 
839
        block_fini(dev_handle);
-
 
840
        fat_idx_fini_by_dev_handle(dev_handle);
-
 
841
        ipc_answer_0(rid, ENOMEM);
-
 
842
        return;
-
 
843
    }
824
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
844
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
825
    if (!rootp) {
845
    if (!rootp) {
-
 
846
        free(rfn);
826
        block_fini(dev_handle);
847
        block_fini(dev_handle);
827
        fat_idx_fini_by_dev_handle(dev_handle);
848
        fat_idx_fini_by_dev_handle(dev_handle);
828
        ipc_answer_0(rid, ENOMEM);
849
        ipc_answer_0(rid, ENOMEM);
829
        return;
850
        return;
830
    }
851
    }
831
    fat_node_initialize(rootp);
852
    fat_node_initialize(rootp);
832
 
853
 
833
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
854
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
834
    if (!ridxp) {
855
    if (!ridxp) {
835
        block_fini(dev_handle);
856
        free(rfn);
836
        free(rootp);
857
        free(rootp);
-
 
858
        block_fini(dev_handle);
837
        fat_idx_fini_by_dev_handle(dev_handle);
859
        fat_idx_fini_by_dev_handle(dev_handle);
838
        ipc_answer_0(rid, ENOMEM);
860
        ipc_answer_0(rid, ENOMEM);
839
        return;
861
        return;
840
    }
862
    }
841
    assert(ridxp->index == 0);
863
    assert(ridxp->index == 0);
Line 846... Line 868...
846
    rootp->refcnt = 1;
868
    rootp->refcnt = 1;
847
    rootp->lnkcnt = 0;  /* FS root is not linked */
869
    rootp->lnkcnt = 0;  /* FS root is not linked */
848
    rootp->size = rde * sizeof(fat_dentry_t);
870
    rootp->size = rde * sizeof(fat_dentry_t);
849
    rootp->idx = ridxp;
871
    rootp->idx = ridxp;
850
    ridxp->nodep = rootp;
872
    ridxp->nodep = rootp;
-
 
873
    rootp->bp = rfn;
-
 
874
    rfn->data = rootp;
851
   
875
   
852
    futex_up(&ridxp->lock);
876
    futex_up(&ridxp->lock);
853
 
877
 
854
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
878
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
855
}
879
}
Line 867... Line 891...
867
void fat_read(ipc_callid_t rid, ipc_call_t *request)
891
void fat_read(ipc_callid_t rid, ipc_call_t *request)
868
{
892
{
869
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
893
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
870
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
894
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
871
    off_t pos = (off_t)IPC_GET_ARG3(*request);
895
    off_t pos = (off_t)IPC_GET_ARG3(*request);
872
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
896
    fs_node_t *fn = fat_node_get(dev_handle, index);
-
 
897
    fat_node_t *nodep;
873
    fat_bs_t *bs;
898
    fat_bs_t *bs;
874
    uint16_t bps;
899
    uint16_t bps;
875
    size_t bytes;
900
    size_t bytes;
876
    block_t *b;
901
    block_t *b;
877
 
902
 
878
    if (!nodep) {
903
    if (!fn) {
879
        ipc_answer_0(rid, ENOENT);
904
        ipc_answer_0(rid, ENOENT);
880
        return;
905
        return;
881
    }
906
    }
-
 
907
    nodep = FAT_NODE(fn);
882
 
908
 
883
    ipc_callid_t callid;
909
    ipc_callid_t callid;
884
    size_t len;
910
    size_t len;
885
    if (!ipc_data_read_receive(&callid, &len)) {
911
    if (!ipc_data_read_receive(&callid, &len)) {
886
        fat_node_put(nodep);
912
        fat_node_put(fn);
887
        ipc_answer_0(callid, EINVAL);
913
        ipc_answer_0(callid, EINVAL);
888
        ipc_answer_0(rid, EINVAL);
914
        ipc_answer_0(rid, EINVAL);
889
        return;
915
        return;
890
    }
916
    }
891
 
917
 
Line 952... Line 978...
952
            }
978
            }
953
            block_put(b);
979
            block_put(b);
954
            bnum++;
980
            bnum++;
955
        }
981
        }
956
miss:
982
miss:
957
        fat_node_put(nodep);
983
        fat_node_put(fn);
958
        ipc_answer_0(callid, ENOENT);
984
        ipc_answer_0(callid, ENOENT);
959
        ipc_answer_1(rid, ENOENT, 0);
985
        ipc_answer_1(rid, ENOENT, 0);
960
        return;
986
        return;
961
hit:
987
hit:
962
        (void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
988
        (void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
963
        bytes = (pos - spos) + 1;
989
        bytes = (pos - spos) + 1;
964
    }
990
    }
965
 
991
 
966
    fat_node_put(nodep);
992
    fat_node_put(fn);
967
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
993
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
968
}
994
}
969
 
995
 
970
void fat_write(ipc_callid_t rid, ipc_call_t *request)
996
void fat_write(ipc_callid_t rid, ipc_call_t *request)
971
{
997
{
972
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
998
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
973
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
999
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
974
    off_t pos = (off_t)IPC_GET_ARG3(*request);
1000
    off_t pos = (off_t)IPC_GET_ARG3(*request);
975
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
1001
    fs_node_t *fn = fat_node_get(dev_handle, index);
-
 
1002
    fat_node_t *nodep;
976
    fat_bs_t *bs;
1003
    fat_bs_t *bs;
977
    size_t bytes;
1004
    size_t bytes;
978
    block_t *b;
1005
    block_t *b;
979
    uint16_t bps;
1006
    uint16_t bps;
980
    unsigned spc;
1007
    unsigned spc;
981
    unsigned bpc;       /* bytes per cluster */
1008
    unsigned bpc;       /* bytes per cluster */
982
    off_t boundary;
1009
    off_t boundary;
983
    int flags = BLOCK_FLAGS_NONE;
1010
    int flags = BLOCK_FLAGS_NONE;
984
   
1011
   
985
    if (!nodep) {
1012
    if (!fn) {
986
        ipc_answer_0(rid, ENOENT);
1013
        ipc_answer_0(rid, ENOENT);
987
        return;
1014
        return;
988
    }
1015
    }
-
 
1016
    nodep = FAT_NODE(fn);
989
   
1017
   
990
    ipc_callid_t callid;
1018
    ipc_callid_t callid;
991
    size_t len;
1019
    size_t len;
992
    if (!ipc_data_write_receive(&callid, &len)) {
1020
    if (!ipc_data_write_receive(&callid, &len)) {
993
        fat_node_put(nodep);
1021
        fat_node_put(fn);
994
        ipc_answer_0(callid, EINVAL);
1022
        ipc_answer_0(callid, EINVAL);
995
        ipc_answer_0(rid, EINVAL);
1023
        ipc_answer_0(rid, EINVAL);
996
        return;
1024
        return;
997
    }
1025
    }
998
 
1026
 
Line 1029... Line 1057...
1029
        if (pos + bytes > nodep->size) {
1057
        if (pos + bytes > nodep->size) {
1030
            nodep->size = pos + bytes;
1058
            nodep->size = pos + bytes;
1031
            nodep->dirty = true;    /* need to sync node */
1059
            nodep->dirty = true;    /* need to sync node */
1032
        }
1060
        }
1033
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1061
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1034
        fat_node_put(nodep);
1062
        fat_node_put(fn);
1035
        return;
1063
        return;
1036
    } else {
1064
    } else {
1037
        /*
1065
        /*
1038
         * This is the more difficult case. We must allocate new
1066
         * This is the more difficult case. We must allocate new
1039
         * clusters for the node and zero them out.
1067
         * clusters for the node and zero them out.
Line 1045... Line 1073...
1045
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1073
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1046
        /* create an independent chain of nclsts clusters in all FATs */
1074
        /* create an independent chain of nclsts clusters in all FATs */
1047
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1075
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1048
        if (status != EOK) {
1076
        if (status != EOK) {
1049
            /* could not allocate a chain of nclsts clusters */
1077
            /* could not allocate a chain of nclsts clusters */
1050
            fat_node_put(nodep);
1078
            fat_node_put(fn);
1051
            ipc_answer_0(callid, status);
1079
            ipc_answer_0(callid, status);
1052
            ipc_answer_0(rid, status);
1080
            ipc_answer_0(rid, status);
1053
            return;
1081
            return;
1054
        }
1082
        }
1055
        /* zero fill any gaps */
1083
        /* zero fill any gaps */
Line 1066... Line 1094...
1066
         */
1094
         */
1067
        fat_append_clusters(bs, nodep, mcl);
1095
        fat_append_clusters(bs, nodep, mcl);
1068
        nodep->size = pos + bytes;
1096
        nodep->size = pos + bytes;
1069
        nodep->dirty = true;        /* need to sync node */
1097
        nodep->dirty = true;        /* need to sync node */
1070
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1098
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1071
        fat_node_put(nodep);
1099
        fat_node_put(fn);
1072
        return;
1100
        return;
1073
    }
1101
    }
1074
}
1102
}
1075
 
1103
 
1076
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1104
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1077
{
1105
{
1078
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1106
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1079
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1107
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1080
    size_t size = (off_t)IPC_GET_ARG3(*request);
1108
    size_t size = (off_t)IPC_GET_ARG3(*request);
1081
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
1109
    fs_node_t *fn = fat_node_get(dev_handle, index);
-
 
1110
    fat_node_t *nodep;
1082
    fat_bs_t *bs;
1111
    fat_bs_t *bs;
1083
    uint16_t bps;
1112
    uint16_t bps;
1084
    uint8_t spc;
1113
    uint8_t spc;
1085
    unsigned bpc;   /* bytes per cluster */
1114
    unsigned bpc;   /* bytes per cluster */
1086
    int rc;
1115
    int rc;
1087
 
1116
 
1088
    if (!nodep) {
1117
    if (!fn) {
1089
        ipc_answer_0(rid, ENOENT);
1118
        ipc_answer_0(rid, ENOENT);
1090
        return;
1119
        return;
1091
    }
1120
    }
-
 
1121
    nodep = FAT_NODE(fn);
1092
 
1122
 
1093
    bs = block_bb_get(dev_handle);
1123
    bs = block_bb_get(dev_handle);
1094
    bps = uint16_t_le2host(bs->bps);
1124
    bps = uint16_t_le2host(bs->bps);
1095
    spc = bs->spc;
1125
    spc = bs->spc;
1096
    bpc = bps * spc;
1126
    bpc = bps * spc;
Line 1124... Line 1154...
1124
        }
1154
        }
1125
        nodep->size = size;
1155
        nodep->size = size;
1126
        nodep->dirty = true;        /* need to sync node */
1156
        nodep->dirty = true;        /* need to sync node */
1127
        rc = EOK;  
1157
        rc = EOK;  
1128
    }
1158
    }
1129
    fat_node_put(nodep);
1159
    fat_node_put(fn);
1130
    ipc_answer_0(rid, rc);
1160
    ipc_answer_0(rid, rc);
1131
    return;
1161
    return;
1132
}
1162
}
1133
 
1163
 
1134
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1164
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1135
{
1165
{
1136
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1166
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1137
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1167
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1138
    int rc;
1168
    int rc;
1139
 
1169
 
1140
    fat_node_t *nodep = fat_node_get(dev_handle, index);
1170
    fs_node_t *fn = fat_node_get(dev_handle, index);
1141
    if (!nodep) {
1171
    if (!fn) {
1142
        ipc_answer_0(rid, ENOENT);
1172
        ipc_answer_0(rid, ENOENT);
1143
        return;
1173
        return;
1144
    }
1174
    }
1145
 
1175
 
1146
    rc = fat_destroy_node(nodep);
1176
    rc = fat_destroy_node(fn);
1147
    ipc_answer_0(rid, rc);
1177
    ipc_answer_0(rid, rc);
1148
}
1178
}
1149
 
1179
 
1150
/**
1180
/**
1151
 * @}
1181
 * @}