Subversion Repositories HelenOS

Rev

Rev 3500 | Rev 3503 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2627 jermar 1
/*
2793 jermar 2
 * Copyright (c) 2008 Jakub Jermar
2627 jermar 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup fs
30
 * @{
31
 */
32
 
33
/**
34
 * @file    fat_ops.c
35
 * @brief   Implementation of VFS operations for the FAT file system server.
36
 */
37
 
38
#include "fat.h"
2638 jermar 39
#include "../../vfs/vfs.h"
2793 jermar 40
#include <libfs.h>
2627 jermar 41
#include <ipc/ipc.h>
3257 jermar 42
#include <ipc/services.h>
43
#include <ipc/devmap.h>
2627 jermar 44
#include <async.h>
45
#include <errno.h>
2793 jermar 46
#include <string.h>
2798 jermar 47
#include <byteorder.h>
2831 jermar 48
#include <libadt/hash_table.h>
49
#include <libadt/list.h>
50
#include <assert.h>
2856 jermar 51
#include <futex.h>
3257 jermar 52
#include <sys/mman.h>
3499 jermar 53
#include <align.h>
2627 jermar 54
 
2843 jermar 55
#define BS_BLOCK        0
3253 jermar 56
#define BS_SIZE         512
2843 jermar 57
 
2951 jermar 58
/** Futex protecting the list of cached free FAT nodes. */
59
static futex_t ffn_futex = FUTEX_INITIALIZER;
2843 jermar 60
 
2951 jermar 61
/** List of cached free FAT nodes. */
62
static LIST_INITIALIZE(ffn_head);
63
 
2638 jermar 64
#define FAT_NAME_LEN        8
65
#define FAT_EXT_LEN     3
66
 
67
#define FAT_PAD         ' ' 
68
 
69
#define FAT_DENTRY_UNUSED   0x00
70
#define FAT_DENTRY_E5_ESC   0x05
71
#define FAT_DENTRY_DOT      0x2e
72
#define FAT_DENTRY_ERASED   0xe5
73
 
3308 jermar 74
#define min(a, b)       ((a) < (b) ? (a) : (b))
75
 
2793 jermar 76
static void dentry_name_canonify(fat_dentry_t *d, char *buf)
2638 jermar 77
{
2793 jermar 78
    int i;
2639 jermar 79
 
2793 jermar 80
    for (i = 0; i < FAT_NAME_LEN; i++) {
3272 jermar 81
        if (d->name[i] == FAT_PAD)
2793 jermar 82
            break;
83
        if (d->name[i] == FAT_DENTRY_E5_ESC)
84
            *buf++ = 0xe5;
85
        else
86
            *buf++ = d->name[i];
87
    }
88
    if (d->ext[0] != FAT_PAD)
89
        *buf++ = '.';
90
    for (i = 0; i < FAT_EXT_LEN; i++) {
91
        if (d->ext[i] == FAT_PAD) {
92
            *buf = '\0';
93
            return;
94
        }
95
        if (d->ext[i] == FAT_DENTRY_E5_ESC)
96
            *buf++ = 0xe5;
97
        else
98
            *buf++ = d->ext[i];
99
    }
3272 jermar 100
    *buf = '\0';
2793 jermar 101
}
102
 
3257 jermar 103
static int dev_phone = -1;      /* FIXME */
104
static void *dev_buffer = NULL;     /* FIXME */
105
 
2910 jermar 106
/* TODO move somewhere else */
2822 jermar 107
typedef struct {
108
    void *data;
3257 jermar 109
    size_t size;
3499 jermar 110
    bool dirty;
2822 jermar 111
} block_t;
112
 
3253 jermar 113
static block_t *block_get(dev_handle_t dev_handle, off_t offset, size_t bs)
2793 jermar 114
{
3257 jermar 115
    /* FIXME */
116
    block_t *b;
117
    off_t bufpos = 0;
118
    size_t buflen = 0;
3272 jermar 119
    off_t pos = offset * bs;
3257 jermar 120
 
121
    assert(dev_phone != -1);
122
    assert(dev_buffer);
123
 
124
    b = malloc(sizeof(block_t));
125
    if (!b)
126
        return NULL;
127
 
128
    b->data = malloc(bs);
129
    if (!b->data) {
130
        free(b);
131
        return NULL;
132
    }
133
    b->size = bs;
134
 
3272 jermar 135
    if (!libfs_blockread(dev_phone, dev_buffer, &bufpos, &buflen, &pos,
3257 jermar 136
        b->data, bs, bs)) {
137
        free(b->data);
138
        free(b);
139
        return NULL;
140
    }
141
 
142
    return b;
2793 jermar 143
}
144
 
2822 jermar 145
static void block_put(block_t *block)
2793 jermar 146
{
3257 jermar 147
    /* FIXME */
148
    free(block->data);
149
    free(block);
2793 jermar 150
}
151
 
2859 jermar 152
#define FAT_BS(b)       ((fat_bs_t *)((b)->data))
153
 
2864 jermar 154
#define FAT_CLST_RES0   0x0000
3119 jermar 155
#define FAT_CLST_RES1   0x0001
2859 jermar 156
#define FAT_CLST_FIRST  0x0002
157
#define FAT_CLST_BAD    0xfff7
158
#define FAT_CLST_LAST1  0xfff8
159
#define FAT_CLST_LAST8  0xffff
160
 
3119 jermar 161
/* internally used to mark root directory's parent */
162
#define FAT_CLST_ROOTPAR    FAT_CLST_RES0
163
/* internally used to mark root directory */
164
#define FAT_CLST_ROOT       FAT_CLST_RES1
165
 
2891 jermar 166
#define fat_block_get(np, off) \
167
    _fat_block_get((np)->idx->dev_handle, (np)->firstc, (off))
168
 
169
static block_t *
2893 jermar 170
_fat_block_get(dev_handle_t dev_handle, fat_cluster_t firstc, off_t offset)
2859 jermar 171
{
172
    block_t *bb;
173
    block_t *b;
174
    unsigned bps;
175
    unsigned spc;
176
    unsigned rscnt;     /* block address of the first FAT */
177
    unsigned fatcnt;
178
    unsigned rde;
179
    unsigned rds;       /* root directory size */
180
    unsigned sf;
181
    unsigned ssa;       /* size of the system area */
182
    unsigned clusters;
2893 jermar 183
    fat_cluster_t clst = firstc;
2859 jermar 184
    unsigned i;
185
 
3253 jermar 186
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
2859 jermar 187
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
188
    spc = FAT_BS(bb)->spc;
189
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
190
    fatcnt = FAT_BS(bb)->fatcnt;
191
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
192
    sf = uint16_t_le2host(FAT_BS(bb)->sec_per_fat);
193
    block_put(bb);
194
 
195
    rds = (sizeof(fat_dentry_t) * rde) / bps;
196
    rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
197
    ssa = rscnt + fatcnt * sf + rds;
198
 
3119 jermar 199
    if (firstc == FAT_CLST_ROOT) {
2859 jermar 200
        /* root directory special case */
201
        assert(offset < rds);
3253 jermar 202
        b = block_get(dev_handle, rscnt + fatcnt * sf + offset, bps);
2859 jermar 203
        return b;
204
    }
205
 
206
    clusters = offset / spc;
207
    for (i = 0; i < clusters; i++) {
208
        unsigned fsec;  /* sector offset relative to FAT1 */
209
        unsigned fidx;  /* FAT1 entry index */
210
 
211
        assert(clst >= FAT_CLST_FIRST && clst < FAT_CLST_BAD);
2864 jermar 212
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
213
        fidx = clst % (bps / sizeof(fat_cluster_t));
2859 jermar 214
        /* read FAT1 */
3253 jermar 215
        b = block_get(dev_handle, rscnt + fsec, bps);
2864 jermar 216
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
2859 jermar 217
        assert(clst != FAT_CLST_BAD);
218
        assert(clst < FAT_CLST_LAST1);
219
        block_put(b);
220
    }
221
 
2891 jermar 222
    b = block_get(dev_handle, ssa + (clst - FAT_CLST_FIRST) * spc +
3253 jermar 223
        offset % spc, bps);
2859 jermar 224
 
225
    return b;
226
}
227
 
3325 jermar 228
/** Return number of blocks allocated to a file.
229
 *
230
 * @param dev_handle    Device handle of the device with the file.
231
 * @param firstc    First cluster of the file.
232
 *
233
 * @return      Number of blocks allocated to the file.
234
 */
235
static uint16_t
236
_fat_blcks_get(dev_handle_t dev_handle, fat_cluster_t firstc)
237
{
238
    block_t *bb;
239
    block_t *b;
240
    unsigned bps;
241
    unsigned spc;
242
    unsigned rscnt;     /* block address of the first FAT */
243
    unsigned clusters = 0;
244
    fat_cluster_t clst = firstc;
245
 
246
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
247
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
248
    spc = FAT_BS(bb)->spc;
249
    rscnt = uint16_t_le2host(FAT_BS(bb)->rscnt);
250
    block_put(bb);
251
 
252
    if (firstc == FAT_CLST_RES0) {
253
        /* No space allocated to the file. */
254
        return 0;
255
    }
256
 
257
    while (clst < FAT_CLST_LAST1) {
258
        unsigned fsec;  /* sector offset relative to FAT1 */
259
        unsigned fidx;  /* FAT1 entry index */
260
 
261
        assert(clst >= FAT_CLST_FIRST);
262
        fsec = (clst * sizeof(fat_cluster_t)) / bps;
263
        fidx = clst % (bps / sizeof(fat_cluster_t));
264
        /* read FAT1 */
265
        b = block_get(dev_handle, rscnt + fsec, bps);
266
        clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
267
        assert(clst != FAT_CLST_BAD);
268
        block_put(b);
269
        clusters++;
270
    }
271
 
272
    return clusters * spc;
273
}
274
 
2831 jermar 275
static void fat_node_initialize(fat_node_t *node)
2793 jermar 276
{
2951 jermar 277
    futex_initialize(&node->lock, 1);
2864 jermar 278
    node->idx = NULL;
2831 jermar 279
    node->type = 0;
280
    link_initialize(&node->ffn_link);
281
    node->size = 0;
282
    node->lnkcnt = 0;
283
    node->refcnt = 0;
284
    node->dirty = false;
2793 jermar 285
}
286
 
2843 jermar 287
static uint16_t fat_bps_get(dev_handle_t dev_handle)
288
{
289
    block_t *bb;
290
    uint16_t bps;
291
 
3253 jermar 292
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
2843 jermar 293
    assert(bb != NULL);
2859 jermar 294
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
2843 jermar 295
    block_put(bb);
296
 
297
    return bps;
298
}
299
 
2845 jermar 300
typedef enum {
301
    FAT_DENTRY_SKIP,
302
    FAT_DENTRY_LAST,
303
    FAT_DENTRY_VALID
304
} fat_dentry_clsf_t;
305
 
306
static fat_dentry_clsf_t fat_classify_dentry(fat_dentry_t *d)
307
{
308
    if (d->attr & FAT_ATTR_VOLLABEL) {
309
        /* volume label entry */
310
        return FAT_DENTRY_SKIP;
311
    }
312
    if (d->name[0] == FAT_DENTRY_ERASED) {
313
        /* not-currently-used entry */
314
        return FAT_DENTRY_SKIP;
315
    }
316
    if (d->name[0] == FAT_DENTRY_UNUSED) {
317
        /* never used entry */
318
        return FAT_DENTRY_LAST;
319
    }
320
    if (d->name[0] == FAT_DENTRY_DOT) {
321
        /*
322
         * Most likely '.' or '..'.
323
         * It cannot occur in a regular file name.
324
         */
325
        return FAT_DENTRY_SKIP;
326
    }
327
    return FAT_DENTRY_VALID;
328
}
329
 
2893 jermar 330
static void fat_node_sync(fat_node_t *node)
2831 jermar 331
{
332
    /* TODO */
333
}
334
 
2951 jermar 335
/** Internal version of fat_node_get().
336
 *
337
 * @param idxp      Locked index structure.
338
 */
339
static void *fat_node_get_core(fat_idx_t *idxp)
2831 jermar 340
{
2891 jermar 341
    block_t *b;
342
    fat_dentry_t *d;
3312 jermar 343
    fat_node_t *nodep = NULL;
2891 jermar 344
    unsigned bps;
345
    unsigned dps;
346
 
2951 jermar 347
    if (idxp->nodep) {
2891 jermar 348
        /*
349
         * We are lucky.
350
         * The node is already instantiated in memory.
351
         */
2951 jermar 352
        futex_down(&idxp->nodep->lock);
353
        if (!idxp->nodep->refcnt++)
3312 jermar 354
            list_remove(&idxp->nodep->ffn_link);
2951 jermar 355
        futex_up(&idxp->nodep->lock);
356
        return idxp->nodep;
2891 jermar 357
    }
358
 
359
    /*
360
     * We must instantiate the node from the file system.
361
     */
362
 
2951 jermar 363
    assert(idxp->pfc);
2891 jermar 364
 
2951 jermar 365
    futex_down(&ffn_futex);
2893 jermar 366
    if (!list_empty(&ffn_head)) {
2951 jermar 367
        /* Try to use a cached free node structure. */
368
        fat_idx_t *idxp_tmp;
2893 jermar 369
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
2951 jermar 370
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
371
            goto skip_cache;
372
        idxp_tmp = nodep->idx;
373
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
374
            futex_up(&nodep->lock);
375
            goto skip_cache;
376
        }
377
        list_remove(&nodep->ffn_link);
378
        futex_up(&ffn_futex);
2893 jermar 379
        if (nodep->dirty)
380
            fat_node_sync(nodep);
2951 jermar 381
        idxp_tmp->nodep = NULL;
382
        futex_up(&nodep->lock);
383
        futex_up(&idxp_tmp->lock);
2893 jermar 384
    } else {
2951 jermar 385
skip_cache:
2893 jermar 386
        /* Try to allocate a new node structure. */
2951 jermar 387
        futex_up(&ffn_futex);
2893 jermar 388
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
389
        if (!nodep)
390
            return NULL;
391
    }
2891 jermar 392
    fat_node_initialize(nodep);
393
 
2951 jermar 394
    bps = fat_bps_get(idxp->dev_handle);
2891 jermar 395
    dps = bps / sizeof(fat_dentry_t);
396
 
2893 jermar 397
    /* Read the block that contains the dentry of interest. */
2951 jermar 398
    b = _fat_block_get(idxp->dev_handle, idxp->pfc,
399
        (idxp->pdi * sizeof(fat_dentry_t)) / bps);
2891 jermar 400
    assert(b);
401
 
2951 jermar 402
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
2893 jermar 403
    if (d->attr & FAT_ATTR_SUBDIR) {
404
        /*
405
         * The only directory which does not have this bit set is the
406
         * root directory itself. The root directory node is handled
407
         * and initialized elsewhere.
408
         */
409
        nodep->type = FAT_DIRECTORY;
3282 jermar 410
        /*
3325 jermar 411
         * Unfortunately, the 'size' field of the FAT dentry is not
412
         * defined for the directory entry type. We must determine the
413
         * size of the directory by walking the FAT.
3282 jermar 414
         */
3325 jermar 415
        nodep->size = bps * _fat_blcks_get(idxp->dev_handle,
416
            uint16_t_le2host(d->firstc));
2893 jermar 417
    } else {
418
        nodep->type = FAT_FILE;
3282 jermar 419
        nodep->size = uint32_t_le2host(d->size);
2893 jermar 420
    }
421
    nodep->firstc = uint16_t_le2host(d->firstc);
422
    nodep->lnkcnt = 1;
423
    nodep->refcnt = 1;
424
 
425
    block_put(b);
426
 
427
    /* Link the idx structure with the node structure. */
2951 jermar 428
    nodep->idx = idxp;
429
    idxp->nodep = nodep;
2893 jermar 430
 
431
    return nodep;
2831 jermar 432
}
433
 
2951 jermar 434
/** Instantiate a FAT in-core node. */
435
static void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
436
{
437
    void *node;
438
    fat_idx_t *idxp;
439
 
440
    idxp = fat_idx_get_by_index(dev_handle, index);
441
    if (!idxp)
442
        return NULL;
443
    /* idxp->lock held */
444
    node = fat_node_get_core(idxp);
445
    futex_up(&idxp->lock);
446
    return node;
447
}
448
 
2852 jermar 449
static void fat_node_put(void *node)
450
{
2910 jermar 451
    fat_node_t *nodep = (fat_node_t *)node;
452
 
2951 jermar 453
    futex_down(&nodep->lock);
2910 jermar 454
    if (!--nodep->refcnt) {
2951 jermar 455
        futex_down(&ffn_futex);
2910 jermar 456
        list_append(&nodep->ffn_link, &ffn_head);
2951 jermar 457
        futex_up(&ffn_futex);
2910 jermar 458
    }
2951 jermar 459
    futex_up(&nodep->lock);
2852 jermar 460
}
461
 
2857 jermar 462
static void *fat_create(int flags)
463
{
464
    return NULL;    /* not supported at the moment */
465
}
466
 
2858 jermar 467
static int fat_destroy(void *node)
2857 jermar 468
{
2858 jermar 469
    return ENOTSUP; /* not supported at the moment */
2857 jermar 470
}
471
 
472
static bool fat_link(void *prnt, void *chld, const char *name)
473
{
474
    return false;   /* not supported at the moment */
475
}
476
 
477
static int fat_unlink(void *prnt, void *chld)
478
{
479
    return ENOTSUP; /* not supported at the moment */
480
}
481
 
2793 jermar 482
static void *fat_match(void *prnt, const char *component)
483
{
484
    fat_node_t *parentp = (fat_node_t *)prnt;
485
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
2822 jermar 486
    unsigned i, j;
2828 jermar 487
    unsigned bps;       /* bytes per sector */
2822 jermar 488
    unsigned dps;       /* dentries per sector */
489
    unsigned blocks;
2793 jermar 490
    fat_dentry_t *d;
2822 jermar 491
    block_t *b;
2793 jermar 492
 
2953 jermar 493
    futex_down(&parentp->idx->lock);
2864 jermar 494
    bps = fat_bps_get(parentp->idx->dev_handle);
2828 jermar 495
    dps = bps / sizeof(fat_dentry_t);
496
    blocks = parentp->size / bps + (parentp->size % bps != 0);
2822 jermar 497
    for (i = 0; i < blocks; i++) {
498
        unsigned dentries;
2793 jermar 499
 
2864 jermar 500
        b = fat_block_get(parentp, i);
2822 jermar 501
        dentries = (i == blocks - 1) ?
502
            parentp->size % sizeof(fat_dentry_t) :
503
            dps;
504
        for (j = 0; j < dentries; j++) {
505
            d = ((fat_dentry_t *)b->data) + j;
2845 jermar 506
            switch (fat_classify_dentry(d)) {
507
            case FAT_DENTRY_SKIP:
2822 jermar 508
                continue;
2845 jermar 509
            case FAT_DENTRY_LAST:
2822 jermar 510
                block_put(b);
2953 jermar 511
                futex_up(&parentp->idx->lock);
2822 jermar 512
                return NULL;
2845 jermar 513
            default:
514
            case FAT_DENTRY_VALID:
515
                dentry_name_canonify(d, name);
516
                break;
2822 jermar 517
            }
3272 jermar 518
            if (stricmp(name, component) == 0) {
2822 jermar 519
                /* hit */
2951 jermar 520
                void *node;
2953 jermar 521
                /*
522
                 * Assume tree hierarchy for locking.  We
523
                 * already have the parent and now we are going
524
                 * to lock the child.  Never lock in the oposite
525
                 * order.
526
                 */
2890 jermar 527
                fat_idx_t *idx = fat_idx_get_by_pos(
2881 jermar 528
                    parentp->idx->dev_handle, parentp->firstc,
2864 jermar 529
                    i * dps + j);
2953 jermar 530
                futex_up(&parentp->idx->lock);
2890 jermar 531
                if (!idx) {
532
                    /*
533
                     * Can happen if memory is low or if we
534
                     * run out of 32-bit indices.
535
                     */
536
                    block_put(b);
537
                    return NULL;
538
                }
2951 jermar 539
                node = fat_node_get_core(idx);
540
                futex_up(&idx->lock);
2822 jermar 541
                block_put(b);
542
                return node;
543
            }
2793 jermar 544
        }
2822 jermar 545
        block_put(b);
2639 jermar 546
    }
2953 jermar 547
    futex_up(&parentp->idx->lock);
2793 jermar 548
    return NULL;
2638 jermar 549
}
550
 
2831 jermar 551
static fs_index_t fat_index_get(void *node)
552
{
553
    fat_node_t *fnodep = (fat_node_t *)node;
554
    if (!fnodep)
555
        return 0;
2864 jermar 556
    return fnodep->idx->index;
2831 jermar 557
}
558
 
559
static size_t fat_size_get(void *node)
560
{
561
    return ((fat_node_t *)node)->size;
562
}
563
 
564
static unsigned fat_lnkcnt_get(void *node)
565
{
566
    return ((fat_node_t *)node)->lnkcnt;
567
}
568
 
2845 jermar 569
static bool fat_has_children(void *node)
570
{
571
    fat_node_t *nodep = (fat_node_t *)node;
572
    unsigned bps;
573
    unsigned dps;
574
    unsigned blocks;
575
    block_t *b;
576
    unsigned i, j;
577
 
578
    if (nodep->type != FAT_DIRECTORY)
579
        return false;
580
 
2951 jermar 581
    futex_down(&nodep->idx->lock);
2864 jermar 582
    bps = fat_bps_get(nodep->idx->dev_handle);
2845 jermar 583
    dps = bps / sizeof(fat_dentry_t);
584
 
585
    blocks = nodep->size / bps + (nodep->size % bps != 0);
586
 
587
    for (i = 0; i < blocks; i++) {
588
        unsigned dentries;
589
        fat_dentry_t *d;
590
 
2864 jermar 591
        b = fat_block_get(nodep, i);
2845 jermar 592
        dentries = (i == blocks - 1) ?
593
            nodep->size % sizeof(fat_dentry_t) :
594
            dps;
595
        for (j = 0; j < dentries; j++) {
596
            d = ((fat_dentry_t *)b->data) + j;
597
            switch (fat_classify_dentry(d)) {
598
            case FAT_DENTRY_SKIP:
599
                continue;
600
            case FAT_DENTRY_LAST:
601
                block_put(b);
2951 jermar 602
                futex_up(&nodep->idx->lock);
2845 jermar 603
                return false;
604
            default:
605
            case FAT_DENTRY_VALID:
606
                block_put(b);
2951 jermar 607
                futex_up(&nodep->idx->lock);
2845 jermar 608
                return true;
609
            }
610
            block_put(b);
2951 jermar 611
            futex_up(&nodep->idx->lock);
2845 jermar 612
            return true;
613
        }
614
        block_put(b);
615
    }
616
 
2951 jermar 617
    futex_up(&nodep->idx->lock);
2845 jermar 618
    return false;
619
}
620
 
2844 jermar 621
static void *fat_root_get(dev_handle_t dev_handle)
622
{
3119 jermar 623
    return fat_node_get(dev_handle, 0);
2844 jermar 624
}
625
 
626
static char fat_plb_get_char(unsigned pos)
627
{
628
    return fat_reg.plb_ro[pos % PLB_SIZE];
629
}
630
 
2831 jermar 631
static bool fat_is_directory(void *node)
632
{
633
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
634
}
635
 
636
static bool fat_is_file(void *node)
637
{
638
    return ((fat_node_t *)node)->type == FAT_FILE;
639
}
640
 
2793 jermar 641
/** libfs operations */
642
libfs_ops_t fat_libfs_ops = {
643
    .match = fat_match,
644
    .node_get = fat_node_get,
2852 jermar 645
    .node_put = fat_node_put,
2857 jermar 646
    .create = fat_create,
647
    .destroy = fat_destroy,
648
    .link = fat_link,
649
    .unlink = fat_unlink,
2831 jermar 650
    .index_get = fat_index_get,
651
    .size_get = fat_size_get,
652
    .lnkcnt_get = fat_lnkcnt_get,
2845 jermar 653
    .has_children = fat_has_children,
2844 jermar 654
    .root_get = fat_root_get,
655
    .plb_get_char = fat_plb_get_char,
2831 jermar 656
    .is_directory = fat_is_directory,
657
    .is_file = fat_is_file
2793 jermar 658
};
659
 
3110 jermar 660
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
661
{
662
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
3119 jermar 663
    block_t *bb;
3257 jermar 664
    uint16_t bps;
3119 jermar 665
    uint16_t rde;
3110 jermar 666
    int rc;
667
 
3257 jermar 668
    /*
669
     * For now, we don't bother to remember dev_handle, dev_phone or
670
     * dev_buffer in some data structure. We use global variables because we
671
     * know there will be at most one mount on this file system.
672
     * Of course, this is a huge TODO item.
673
     */
674
    dev_buffer = mmap(NULL, BS_SIZE, PROTO_READ | PROTO_WRITE,
675
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
676
 
677
    if (!dev_buffer) {
678
        ipc_answer_0(rid, ENOMEM);
679
        return;
680
    }
681
 
682
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
683
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
684
 
685
    if (dev_phone < 0) {
686
        munmap(dev_buffer, BS_SIZE);
687
        ipc_answer_0(rid, dev_phone);
688
        return;
689
    }
690
 
691
    rc = ipc_share_out_start(dev_phone, dev_buffer,
692
        AS_AREA_READ | AS_AREA_WRITE);
693
    if (rc != EOK) {
694
            munmap(dev_buffer, BS_SIZE);
695
        ipc_answer_0(rid, rc);
696
        return;
697
    }
698
 
3119 jermar 699
    /* Read the number of root directory entries. */
3253 jermar 700
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
3257 jermar 701
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
3119 jermar 702
    rde = uint16_t_le2host(FAT_BS(bb)->root_ent_max);
703
    block_put(bb);
704
 
3257 jermar 705
    if (bps != BS_SIZE) {
706
        munmap(dev_buffer, BS_SIZE);
707
        ipc_answer_0(rid, ENOTSUP);
708
        return;
709
    }
710
 
3110 jermar 711
    rc = fat_idx_init_by_dev_handle(dev_handle);
712
    if (rc != EOK) {
3257 jermar 713
            munmap(dev_buffer, BS_SIZE);
3110 jermar 714
        ipc_answer_0(rid, rc);
715
        return;
716
    }
717
 
3119 jermar 718
    /* Initialize the root node. */
719
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
720
    if (!rootp) {
3257 jermar 721
            munmap(dev_buffer, BS_SIZE);
3119 jermar 722
        fat_idx_fini_by_dev_handle(dev_handle);
723
        ipc_answer_0(rid, ENOMEM);
724
        return;
725
    }
726
    fat_node_initialize(rootp);
727
 
728
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
729
    if (!ridxp) {
3257 jermar 730
            munmap(dev_buffer, BS_SIZE);
3119 jermar 731
        free(rootp);
732
        fat_idx_fini_by_dev_handle(dev_handle);
733
        ipc_answer_0(rid, ENOMEM);
734
        return;
735
    }
736
    assert(ridxp->index == 0);
737
    /* ridxp->lock held */
738
 
739
    rootp->type = FAT_DIRECTORY;
740
    rootp->firstc = FAT_CLST_ROOT;
741
    rootp->refcnt = 1;
3352 jermar 742
    rootp->lnkcnt = 0;  /* FS root is not linked */
3119 jermar 743
    rootp->size = rde * sizeof(fat_dentry_t);
744
    rootp->idx = ridxp;
745
    ridxp->nodep = rootp;
746
 
747
    futex_up(&ridxp->lock);
748
 
3352 jermar 749
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
3110 jermar 750
}
751
 
752
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
753
{
754
    ipc_answer_0(rid, ENOTSUP);
755
}
756
 
2627 jermar 757
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
758
{
2793 jermar 759
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
2627 jermar 760
}
761
 
3307 jermar 762
void fat_read(ipc_callid_t rid, ipc_call_t *request)
763
{
764
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
765
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
766
    off_t pos = (off_t)IPC_GET_ARG3(*request);
767
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3308 jermar 768
    uint16_t bps = fat_bps_get(dev_handle);
769
    size_t bytes;
3335 jermar 770
    block_t *b;
3308 jermar 771
 
3307 jermar 772
    if (!nodep) {
773
        ipc_answer_0(rid, ENOENT);
774
        return;
775
    }
776
 
777
    ipc_callid_t callid;
778
    size_t len;
3314 jermar 779
    if (!ipc_data_read_receive(&callid, &len)) {
3307 jermar 780
        fat_node_put(nodep);
781
        ipc_answer_0(callid, EINVAL);
782
        ipc_answer_0(rid, EINVAL);
783
        return;
784
    }
785
 
786
    if (nodep->type == FAT_FILE) {
3335 jermar 787
        /*
788
         * Our strategy for regular file reads is to read one block at
789
         * most and make use of the possibility to return less data than
790
         * requested. This keeps the code very simple.
791
         */
3308 jermar 792
        bytes = min(len, bps - pos % bps);
793
        b = fat_block_get(nodep, pos / bps);
794
        (void) ipc_data_read_finalize(callid, b->data + pos % bps,
795
            bytes);
796
        block_put(b);
3307 jermar 797
    } else {
3335 jermar 798
        unsigned bnum;
799
        off_t spos = pos;
800
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
801
        fat_dentry_t *d;
802
 
3307 jermar 803
        assert(nodep->type == FAT_DIRECTORY);
3335 jermar 804
        assert(nodep->size % bps == 0);
805
        assert(bps % sizeof(fat_dentry_t) == 0);
806
 
807
        /*
808
         * Our strategy for readdir() is to use the position pointer as
809
         * an index into the array of all dentries. On entry, it points
810
         * to the first unread dentry. If we skip any dentries, we bump
811
         * the position pointer accordingly.
812
         */
813
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
814
        while (bnum < nodep->size / bps) {
815
            off_t o;
816
 
817
            b = fat_block_get(nodep, bnum);
818
            for (o = pos % (bps / sizeof(fat_dentry_t));
819
                o < bps / sizeof(fat_dentry_t);
820
                o++, pos++) {
821
                d = ((fat_dentry_t *)b->data) + o;
822
                switch (fat_classify_dentry(d)) {
823
                case FAT_DENTRY_SKIP:
824
                    continue;
825
                case FAT_DENTRY_LAST:
826
                    block_put(b);
827
                    goto miss;
828
                default:
829
                case FAT_DENTRY_VALID:
830
                    dentry_name_canonify(d, name);
831
                    block_put(b);
832
                    goto hit;
833
                }
834
            }
835
            block_put(b);
836
            bnum++;
837
        }
838
miss:
3307 jermar 839
        fat_node_put(nodep);
3335 jermar 840
        ipc_answer_0(callid, ENOENT);
841
        ipc_answer_1(rid, ENOENT, 0);
3307 jermar 842
        return;
3335 jermar 843
hit:
844
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
845
        bytes = (pos - spos) + 1;
3307 jermar 846
    }
847
 
848
    fat_node_put(nodep);
3308 jermar 849
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
3307 jermar 850
}
851
 
3501 jermar 852
/** Fill the gap between EOF and a new file position.
853
 *
854
 * @param nodep     FAT node with the gap.
855
 * @param mcl       First cluster in an independent cluster chain that will
856
 *          be later appended to the end of the node's own cluster
857
 *          chain. If pos is still in the last allocated cluster,
858
 *          this argument is ignored.
859
 * @param pos       Position in the last node block.
860
 */
3500 jermar 861
static void
3501 jermar 862
fat_fill_gap(fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
3499 jermar 863
{
3501 jermar 864
    uint16_t bps;
865
    unsigned spc;
866
    block_t *bb, *b;
867
    off_t o, boundary;
868
 
869
    bb = block_get(nodep->idx->dev_handle, BS_BLOCK, BS_SIZE);
870
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
871
    spc = FAT_BS(bb)->spc;
872
    block_put(bb);
873
 
874
    boundary = ROUND_UP(nodep->size, bps * spc);
875
 
876
    /* zero out already allocated space */
877
    for (o = nodep->size - 1; o < pos && o < boundary;
878
        o = ALIGN_DOWN(o + bps, bps)) {
879
        b = fat_block_get(nodep, o / bps);
880
        memset(b->data + o % bps, 0, bps - o % bps);
881
        b->dirty = true;        /* need to sync node */
882
        block_put(b);
883
    }
884
 
885
    if (o >= pos)
886
        return;
887
 
888
    /* zero out the initial part of the new cluster chain */
889
    for (o = boundary; o < pos; o += bps) {
890
        b = _fat_block_get(nodep->idx->dev_handle, mcl,
891
            (o - boundary) / bps);
892
        memset(b->data, 0, min(bps, pos - o));
893
        b->dirty = true;
894
        block_put(b);
895
    }
3499 jermar 896
}
897
 
3500 jermar 898
static int
899
fat_alloc_clusters(unsigned nclsts, fat_cluster_t *mcl, fat_cluster_t *lcl)
900
{
901
    return ENOTSUP; /* TODO */
902
}
903
 
904
static void
905
fat_append_clusters(fat_node_t *nodep, fat_cluster_t mcl)
906
{
907
}
908
 
3497 jermar 909
void fat_write(ipc_callid_t rid, ipc_call_t *request)
910
{
3499 jermar 911
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
912
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
913
    off_t pos = (off_t)IPC_GET_ARG3(*request);
914
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
915
    size_t bytes;
916
    block_t *b, *bb;
917
    uint16_t bps;
918
    unsigned spc;
3501 jermar 919
    off_t boundary;
3499 jermar 920
 
921
    if (!nodep) {
922
        ipc_answer_0(rid, ENOENT);
923
        return;
924
    }
925
 
926
    /* XXX remove me when you are ready */
927
    {
928
        ipc_answer_0(rid, ENOTSUP);
929
        fat_node_put(nodep);
930
        return;
931
    }
932
 
933
    ipc_callid_t callid;
934
    size_t len;
935
    if (!ipc_data_write_receive(&callid, &len)) {
936
        fat_node_put(nodep);
937
        ipc_answer_0(callid, EINVAL);
938
        ipc_answer_0(rid, EINVAL);
939
        return;
940
    }
941
 
942
    /*
943
     * In all scenarios, we will attempt to write out only one block worth
944
     * of data at maximum. There might be some more efficient approaches,
945
     * but this one greatly simplifies fat_write(). Note that we can afford
946
     * to do this because the client must be ready to handle the return
947
     * value signalizing a smaller number of bytes written.
948
     */
949
    bytes = min(len, bps - pos % bps);
950
 
951
    bb = block_get(dev_handle, BS_BLOCK, BS_SIZE);
952
    bps = uint16_t_le2host(FAT_BS(bb)->bps);
953
    spc = FAT_BS(bb)->spc;
954
    block_put(bb);
955
 
3501 jermar 956
    boundary = ROUND_UP(nodep->size, bps * spc);
957
    if (pos < boundary) {
3499 jermar 958
        /*
959
         * This is the easier case - we are either overwriting already
960
         * existing contents or writing behind the EOF, but still within
961
         * the limits of the last cluster. The node size may grow to the
962
         * next block size boundary.
963
         */
3500 jermar 964
        fat_fill_gap(nodep, FAT_CLST_RES0, pos);
3499 jermar 965
        b = fat_block_get(nodep, pos / bps);
966
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
967
            bytes);
968
        b->dirty = true;        /* need to sync block */
3500 jermar 969
        block_put(b);
3499 jermar 970
        if (pos + bytes > nodep->size) {
971
            nodep->size = pos + bytes;
972
            nodep->dirty = true;    /* need to sync node */
973
        }
974
        fat_node_put(nodep);
975
        ipc_answer_1(rid, EOK, bytes); 
976
        return;
977
    } else {
978
        /*
979
         * This is the more difficult case. We must allocate new
980
         * clusters for the node and zero them out.
981
         */
3500 jermar 982
        int status;
3499 jermar 983
        unsigned nclsts;
3500 jermar 984
        fat_cluster_t mcl, lcl;
985
 
3501 jermar 986
        nclsts = (ROUND_UP(pos + bytes, bps * spc) - boundary) /
3500 jermar 987
            bps * spc;
988
        /* create an independent chain of nclsts clusters in all FATs */
989
        status = fat_alloc_clusters(nclsts, &mcl, &lcl);
990
        if (status != EOK) {
991
            /* could not allocate a chain of nclsts clusters */
992
            fat_node_put(nodep);
993
            ipc_answer_0(callid, status);
994
            ipc_answer_0(rid, status);
995
            return;
996
        }
997
        /* zero fill any gaps */
998
        fat_fill_gap(nodep, mcl, pos);
999
        b = _fat_block_get(dev_handle, lcl, (pos / bps) % spc);
1000
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
1001
            bytes);
3501 jermar 1002
        b->dirty = true;        /* need to sync block */
3500 jermar 1003
        block_put(b);
1004
        /*
1005
         * Append the cluster chain starting in mcl to the end of the
1006
         * node's cluster chain.
1007
         */
1008
        fat_append_clusters(nodep, mcl);
1009
        nodep->size = pos + bytes;
3501 jermar 1010
        nodep->dirty = true;        /* need to sync node */
3500 jermar 1011
        fat_node_put(nodep);
1012
        ipc_answer_1(rid, EOK, bytes);
1013
        return;
3499 jermar 1014
    }
3497 jermar 1015
}
1016
 
2627 jermar 1017
/**
1018
 * @}
1019
 */