Subversion Repositories HelenOS

Rev

Rev 3629 | Rev 3634 | 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"
3505 jermar 39
#include "fat_dentry.h"
40
#include "fat_fat.h"
2638 jermar 41
#include "../../vfs/vfs.h"
2793 jermar 42
#include <libfs.h>
3521 jermar 43
#include <libblock.h>
2627 jermar 44
#include <ipc/ipc.h>
3257 jermar 45
#include <ipc/services.h>
46
#include <ipc/devmap.h>
2627 jermar 47
#include <async.h>
48
#include <errno.h>
2793 jermar 49
#include <string.h>
2798 jermar 50
#include <byteorder.h>
2831 jermar 51
#include <libadt/hash_table.h>
52
#include <libadt/list.h>
53
#include <assert.h>
2856 jermar 54
#include <futex.h>
3257 jermar 55
#include <sys/mman.h>
3499 jermar 56
#include <align.h>
2627 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
 
2831 jermar 64
static void fat_node_initialize(fat_node_t *node)
2793 jermar 65
{
2951 jermar 66
    futex_initialize(&node->lock, 1);
2864 jermar 67
    node->idx = NULL;
2831 jermar 68
    node->type = 0;
69
    link_initialize(&node->ffn_link);
70
    node->size = 0;
71
    node->lnkcnt = 0;
72
    node->refcnt = 0;
73
    node->dirty = false;
2793 jermar 74
}
75
 
2893 jermar 76
static void fat_node_sync(fat_node_t *node)
2831 jermar 77
{
3530 jermar 78
    block_t *b;
79
    fat_bs_t *bs;
3519 jermar 80
    fat_dentry_t *d;
81
    uint16_t bps;
82
    unsigned dps;
83
 
84
    assert(node->dirty);
85
 
3530 jermar 86
    bs = block_bb_get(node->idx->dev_handle);
87
    bps = uint16_t_le2host(bs->bps);
3519 jermar 88
    dps = bps / sizeof(fat_dentry_t);
89
 
90
    /* Read the block that contains the dentry of interest. */
3530 jermar 91
    b = _fat_block_get(bs, node->idx->dev_handle, node->idx->pfc,
3595 jermar 92
        (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
3519 jermar 93
 
94
    d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
95
 
96
    d->firstc = host2uint16_t_le(node->firstc);
3629 jermar 97
    if (node->type == FAT_FILE) {
3519 jermar 98
        d->size = host2uint32_t_le(node->size);
3629 jermar 99
    } else if (node->type == FAT_DIRECTORY) {
100
        d->attr = FAT_ATTR_SUBDIR;
101
    }
3519 jermar 102
 
3629 jermar 103
    /* TODO: update other fields? (e.g time fields) */
104
 
3519 jermar 105
    b->dirty = true;        /* need to sync block */
106
    block_put(b);
2831 jermar 107
}
108
 
3574 jermar 109
static fat_node_t *fat_node_get_new(void)
110
{
111
    fat_node_t *nodep;
112
 
113
    futex_down(&ffn_futex);
114
    if (!list_empty(&ffn_head)) {
115
        /* Try to use a cached free node structure. */
116
        fat_idx_t *idxp_tmp;
117
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
118
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
119
            goto skip_cache;
120
        idxp_tmp = nodep->idx;
121
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
122
            futex_up(&nodep->lock);
123
            goto skip_cache;
124
        }
125
        list_remove(&nodep->ffn_link);
126
        futex_up(&ffn_futex);
127
        if (nodep->dirty)
128
            fat_node_sync(nodep);
129
        idxp_tmp->nodep = NULL;
130
        futex_up(&nodep->lock);
131
        futex_up(&idxp_tmp->lock);
132
    } else {
133
skip_cache:
134
        /* Try to allocate a new node structure. */
135
        futex_up(&ffn_futex);
136
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
137
        if (!nodep)
138
            return NULL;
139
    }
140
    fat_node_initialize(nodep);
141
 
142
    return nodep;
143
}
144
 
2951 jermar 145
/** Internal version of fat_node_get().
146
 *
147
 * @param idxp      Locked index structure.
148
 */
149
static void *fat_node_get_core(fat_idx_t *idxp)
2831 jermar 150
{
3530 jermar 151
    block_t *b;
152
    fat_bs_t *bs;
2891 jermar 153
    fat_dentry_t *d;
3312 jermar 154
    fat_node_t *nodep = NULL;
2891 jermar 155
    unsigned bps;
3550 jermar 156
    unsigned spc;
2891 jermar 157
    unsigned dps;
158
 
2951 jermar 159
    if (idxp->nodep) {
2891 jermar 160
        /*
161
         * We are lucky.
162
         * The node is already instantiated in memory.
163
         */
2951 jermar 164
        futex_down(&idxp->nodep->lock);
165
        if (!idxp->nodep->refcnt++)
3312 jermar 166
            list_remove(&idxp->nodep->ffn_link);
2951 jermar 167
        futex_up(&idxp->nodep->lock);
168
        return idxp->nodep;
2891 jermar 169
    }
170
 
171
    /*
172
     * We must instantiate the node from the file system.
173
     */
174
 
2951 jermar 175
    assert(idxp->pfc);
2891 jermar 176
 
3574 jermar 177
    nodep = fat_node_get_new();
178
    if (!nodep)
179
        return NULL;
2891 jermar 180
 
3530 jermar 181
    bs = block_bb_get(idxp->dev_handle);
182
    bps = uint16_t_le2host(bs->bps);
3550 jermar 183
    spc = bs->spc;
2891 jermar 184
    dps = bps / sizeof(fat_dentry_t);
185
 
2893 jermar 186
    /* Read the block that contains the dentry of interest. */
3530 jermar 187
    b = _fat_block_get(bs, idxp->dev_handle, idxp->pfc,
3595 jermar 188
        (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
2891 jermar 189
    assert(b);
190
 
2951 jermar 191
    d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
2893 jermar 192
    if (d->attr & FAT_ATTR_SUBDIR) {
193
        /*
194
         * The only directory which does not have this bit set is the
195
         * root directory itself. The root directory node is handled
196
         * and initialized elsewhere.
197
         */
198
        nodep->type = FAT_DIRECTORY;
3282 jermar 199
        /*
3325 jermar 200
         * Unfortunately, the 'size' field of the FAT dentry is not
201
         * defined for the directory entry type. We must determine the
202
         * size of the directory by walking the FAT.
3282 jermar 203
         */
3550 jermar 204
        nodep->size = bps * spc * fat_clusters_get(bs, idxp->dev_handle,
205
            uint16_t_le2host(d->firstc));
2893 jermar 206
    } else {
207
        nodep->type = FAT_FILE;
3282 jermar 208
        nodep->size = uint32_t_le2host(d->size);
2893 jermar 209
    }
210
    nodep->firstc = uint16_t_le2host(d->firstc);
211
    nodep->lnkcnt = 1;
212
    nodep->refcnt = 1;
213
 
214
    block_put(b);
215
 
216
    /* Link the idx structure with the node structure. */
2951 jermar 217
    nodep->idx = idxp;
218
    idxp->nodep = nodep;
2893 jermar 219
 
220
    return nodep;
2831 jermar 221
}
222
 
3621 jermar 223
/*
224
 * Forward declarations of FAT libfs operations.
225
 */
226
static void *fat_node_get(dev_handle_t, fs_index_t);
227
static void fat_node_put(void *);
228
static void *fat_create_node(dev_handle_t, int);
229
static int fat_destroy_node(void *);
3625 jermar 230
static int fat_link(void *, void *, const char *);
3621 jermar 231
static int fat_unlink(void *, void *);
232
static void *fat_match(void *, const char *);
233
static fs_index_t fat_index_get(void *);
234
static size_t fat_size_get(void *);
235
static unsigned fat_lnkcnt_get(void *);
236
static bool fat_has_children(void *);
237
static void *fat_root_get(dev_handle_t);
238
static char fat_plb_get_char(unsigned);
239
static bool fat_is_directory(void *);
240
static bool fat_is_file(void *node);
241
 
242
/*
243
 * FAT libfs operations.
244
 */
245
 
2951 jermar 246
/** Instantiate a FAT in-core node. */
3621 jermar 247
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
2951 jermar 248
{
249
    void *node;
250
    fat_idx_t *idxp;
251
 
252
    idxp = fat_idx_get_by_index(dev_handle, index);
253
    if (!idxp)
254
        return NULL;
255
    /* idxp->lock held */
256
    node = fat_node_get_core(idxp);
257
    futex_up(&idxp->lock);
258
    return node;
259
}
260
 
3621 jermar 261
void fat_node_put(void *node)
2852 jermar 262
{
2910 jermar 263
    fat_node_t *nodep = (fat_node_t *)node;
3609 jermar 264
    bool destroy = false;
2910 jermar 265
 
2951 jermar 266
    futex_down(&nodep->lock);
2910 jermar 267
    if (!--nodep->refcnt) {
3609 jermar 268
        if (nodep->idx) {
269
            futex_down(&ffn_futex);
270
            list_append(&nodep->ffn_link, &ffn_head);
271
            futex_up(&ffn_futex);
272
        } else {
273
            /*
274
             * The node does not have any index structure associated
275
             * with itself. This can only mean that we are releasing
276
             * the node after a failed attempt to allocate the index
277
             * structure for it.
278
             */
279
            destroy = true;
280
        }
2910 jermar 281
    }
2951 jermar 282
    futex_up(&nodep->lock);
3609 jermar 283
    if (destroy)
284
        free(node);
2852 jermar 285
}
286
 
3621 jermar 287
void *fat_create_node(dev_handle_t dev_handle, int flags)
2857 jermar 288
{
3609 jermar 289
    fat_idx_t *idxp;
290
    fat_node_t *nodep;
3633 jermar 291
    fat_bs_t *bs;
292
    fat_cluster_t mcl, lcl;
293
    uint16_t bps;
294
    int rc;
3609 jermar 295
 
3633 jermar 296
    bs = block_bb_get(dev_handle);
297
    bps = uint16_t_le2host(bs->bps);
298
    if (flags & L_DIRECTORY) {
299
        /* allocate a cluster */
300
        rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
301
        if (rc != EOK)
302
            return NULL;
303
    }
304
 
3609 jermar 305
    nodep = fat_node_get_new();
3633 jermar 306
    if (!nodep) {
307
        fat_free_clusters(bs, dev_handle, mcl);
3609 jermar 308
        return NULL;
3633 jermar 309
    }
3609 jermar 310
    idxp = fat_idx_get_new(dev_handle);
311
    if (!idxp) {
3633 jermar 312
        fat_free_clusters(bs, dev_handle, mcl);
3609 jermar 313
        fat_node_put(nodep);
314
        return NULL;
315
    }
316
    /* idxp->lock held */
317
    if (flags & L_DIRECTORY) {
3633 jermar 318
        int i;
319
        block_t *b;
320
 
321
        /*
322
         * Populate the new cluster with unused dentries.
323
         * We don't create the '.' and '..' entries, since they are
324
         * optional and HelenOS VFS does not need them.
325
         */
326
        for (i = 0; i < bs->spc; i++) {
327
            b = _fat_block_get(bs, dev_handle, mcl, i,
328
                BLOCK_FLAGS_NOREAD);
329
            /* mark all dentries as never-used */
330
            memset(b->data, 0, bps);
331
            b->dirty = false;
332
            block_put(b);
333
        }
3609 jermar 334
        nodep->type = FAT_DIRECTORY;
3633 jermar 335
        nodep->firstc = mcl;
336
        nodep->size = bps * bs->spc;
3609 jermar 337
    } else {
338
        nodep->type = FAT_FILE;
3633 jermar 339
        nodep->firstc = FAT_CLST_RES0;
340
        nodep->size = 0;
3609 jermar 341
    }
342
    nodep->lnkcnt = 0;  /* not linked anywhere */
343
    nodep->refcnt = 1;
3633 jermar 344
    nodep->dirty = true;
3609 jermar 345
 
346
    nodep->idx = idxp;
347
    idxp->nodep = nodep;
348
 
349
    futex_up(&idxp->lock);
350
    return nodep;
2857 jermar 351
}
352
 
3621 jermar 353
int fat_destroy_node(void *node)
2857 jermar 354
{
3621 jermar 355
    fat_node_t *nodep = (fat_node_t *)node;
356
    fat_bs_t *bs;
357
 
358
    /*
359
     * The node is not reachable from the file system. This means that the
360
     * link count should be zero and that the index structure cannot be
361
     * found in the position hash. Obviously, we don't need to lock the node
362
     * nor its index structure.
363
     */
364
    assert(nodep->lnkcnt == 0);
365
 
366
    /*
367
     * The node may not have any children.
368
     */
369
    assert(fat_has_children(node) == false);
370
 
371
    bs = block_bb_get(nodep->idx->dev_handle);
372
    if (nodep->firstc != FAT_CLST_RES0) {
373
        assert(nodep->size);
374
        /* Free all clusters allocated to the node. */
375
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
376
    }
377
 
378
    fat_idx_destroy(nodep->idx);
379
    free(nodep);
380
    return EOK;
2857 jermar 381
}
382
 
3625 jermar 383
int fat_link(void *prnt, void *chld, const char *name)
2857 jermar 384
{
3628 jermar 385
    fat_node_t *parentp = (fat_node_t *)prnt;
386
    fat_node_t *childp = (fat_node_t *)chld;
387
    fat_dentry_t *d;
388
    fat_bs_t *bs;
389
    block_t *b;
390
    int i, j;
391
    uint16_t bps;
392
    unsigned dps;
393
    unsigned blocks;
394
 
395
    futex_down(&childp->lock);
396
    if (childp->lnkcnt == 1) {
397
        /*
398
         * On FAT, we don't support multiple hard links.
399
         */
400
        futex_up(&childp->lock);
401
        return EMLINK;
402
    }
403
    assert(childp->lnkcnt == 0);
404
    futex_up(&childp->lock);
405
 
406
    if (!fat_dentry_name_verify(name)) {
407
        /*
408
         * Attempt to create unsupported name.
409
         */
410
        return ENOTSUP;
411
    }
412
 
413
    /*
414
     * Get us an unused parent node's dentry or grow the parent and allocate
415
     * a new one.
416
     */
417
 
418
    futex_down(&parentp->idx->lock);
419
    bs = block_bb_get(parentp->idx->dev_handle);
420
    bps = uint16_t_le2host(bs->bps);
421
    dps = bps / sizeof(fat_dentry_t);
422
 
423
    blocks = parentp->size / bps;
424
 
425
    for (i = 0; i < blocks; i++) {
426
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
427
        for (j = 0; j < dps; j++) {
428
            d = ((fat_dentry_t *)b->data) + j;
429
            switch (fat_classify_dentry(d)) {
430
            case FAT_DENTRY_SKIP:
431
            case FAT_DENTRY_VALID:
432
                /* skipping used and meta entries */
433
                continue;
434
            case FAT_DENTRY_FREE:
435
            case FAT_DENTRY_LAST:
436
                /* found an empty slot */
437
                goto hit;
438
            }
439
        }
440
        block_put(b);
441
    }
442
 
443
    /*
444
     * We need to grow the parent in order to create a new unused dentry.
445
     */
446
    futex_up(&parentp->idx->lock);
447
    return ENOTSUP; /* XXX */
448
 
449
hit:
450
    /*
451
     * At this point we only establish the link between the parent and the
452
     * child.  The dentry, except of the name and the extension, will remain
453
     * uninitialized until the the corresponding node is synced. Thus the
454
     * valid dentry data is kept in the child node structure.
455
     */
456
    memset(d, 0, sizeof(fat_dentry_t));
457
    fat_dentry_name_set(d, name);
458
    b->dirty = true;        /* need to sync block */
459
    block_put(b);
460
    futex_up(&parentp->idx->lock);
461
 
462
    futex_down(&childp->idx->lock);
463
    childp->idx->pfc = parentp->firstc;
464
    childp->idx->pdi = i * dps + j;
465
    futex_up(&childp->idx->lock);
466
 
467
    futex_down(&childp->lock);
468
    childp->lnkcnt = 1;
469
    childp->dirty = true;       /* need to sync node */
470
    futex_up(&childp->lock);
471
 
472
    /*
473
     * Hash in the index structure into the position hash.
474
     */
475
    fat_idx_hashin(childp->idx);
476
 
477
    return EOK;
2857 jermar 478
}
479
 
3621 jermar 480
int fat_unlink(void *prnt, void *chld)
2857 jermar 481
{
482
    return ENOTSUP; /* not supported at the moment */
483
}
484
 
3621 jermar 485
void *fat_match(void *prnt, const char *component)
2793 jermar 486
{
3530 jermar 487
    fat_bs_t *bs;
2793 jermar 488
    fat_node_t *parentp = (fat_node_t *)prnt;
489
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
2822 jermar 490
    unsigned i, j;
2828 jermar 491
    unsigned bps;       /* bytes per sector */
2822 jermar 492
    unsigned dps;       /* dentries per sector */
493
    unsigned blocks;
2793 jermar 494
    fat_dentry_t *d;
3530 jermar 495
    block_t *b;
2793 jermar 496
 
2953 jermar 497
    futex_down(&parentp->idx->lock);
3530 jermar 498
    bs = block_bb_get(parentp->idx->dev_handle);
499
    bps = uint16_t_le2host(bs->bps);
2828 jermar 500
    dps = bps / sizeof(fat_dentry_t);
3526 jermar 501
    blocks = parentp->size / bps;
2822 jermar 502
    for (i = 0; i < blocks; i++) {
3595 jermar 503
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
3526 jermar 504
        for (j = 0; j < dps; j++) {
2822 jermar 505
            d = ((fat_dentry_t *)b->data) + j;
2845 jermar 506
            switch (fat_classify_dentry(d)) {
507
            case FAT_DENTRY_SKIP:
3628 jermar 508
            case FAT_DENTRY_FREE:
2822 jermar 509
                continue;
2845 jermar 510
            case FAT_DENTRY_LAST:
2822 jermar 511
                block_put(b);
2953 jermar 512
                futex_up(&parentp->idx->lock);
2822 jermar 513
                return NULL;
2845 jermar 514
            default:
515
            case FAT_DENTRY_VALID:
3628 jermar 516
                fat_dentry_name_get(d, name);
2845 jermar 517
                break;
2822 jermar 518
            }
3272 jermar 519
            if (stricmp(name, component) == 0) {
2822 jermar 520
                /* hit */
2951 jermar 521
                void *node;
2953 jermar 522
                /*
523
                 * Assume tree hierarchy for locking.  We
524
                 * already have the parent and now we are going
525
                 * to lock the child.  Never lock in the oposite
526
                 * order.
527
                 */
2890 jermar 528
                fat_idx_t *idx = fat_idx_get_by_pos(
2881 jermar 529
                    parentp->idx->dev_handle, parentp->firstc,
2864 jermar 530
                    i * dps + j);
2953 jermar 531
                futex_up(&parentp->idx->lock);
2890 jermar 532
                if (!idx) {
533
                    /*
534
                     * Can happen if memory is low or if we
535
                     * run out of 32-bit indices.
536
                     */
537
                    block_put(b);
538
                    return NULL;
539
                }
2951 jermar 540
                node = fat_node_get_core(idx);
541
                futex_up(&idx->lock);
2822 jermar 542
                block_put(b);
543
                return node;
544
            }
2793 jermar 545
        }
2822 jermar 546
        block_put(b);
2639 jermar 547
    }
3516 jermar 548
 
2953 jermar 549
    futex_up(&parentp->idx->lock);
2793 jermar 550
    return NULL;
2638 jermar 551
}
552
 
3621 jermar 553
fs_index_t fat_index_get(void *node)
2831 jermar 554
{
555
    fat_node_t *fnodep = (fat_node_t *)node;
556
    if (!fnodep)
557
        return 0;
2864 jermar 558
    return fnodep->idx->index;
2831 jermar 559
}
560
 
3621 jermar 561
size_t fat_size_get(void *node)
2831 jermar 562
{
563
    return ((fat_node_t *)node)->size;
564
}
565
 
3621 jermar 566
unsigned fat_lnkcnt_get(void *node)
2831 jermar 567
{
568
    return ((fat_node_t *)node)->lnkcnt;
569
}
570
 
3621 jermar 571
bool fat_has_children(void *node)
2845 jermar 572
{
3530 jermar 573
    fat_bs_t *bs;
2845 jermar 574
    fat_node_t *nodep = (fat_node_t *)node;
575
    unsigned bps;
576
    unsigned dps;
577
    unsigned blocks;
3530 jermar 578
    block_t *b;
2845 jermar 579
    unsigned i, j;
580
 
581
    if (nodep->type != FAT_DIRECTORY)
582
        return false;
3526 jermar 583
 
2951 jermar 584
    futex_down(&nodep->idx->lock);
3530 jermar 585
    bs = block_bb_get(nodep->idx->dev_handle);
586
    bps = uint16_t_le2host(bs->bps);
2845 jermar 587
    dps = bps / sizeof(fat_dentry_t);
588
 
3526 jermar 589
    blocks = nodep->size / bps;
2845 jermar 590
 
591
    for (i = 0; i < blocks; i++) {
592
        fat_dentry_t *d;
593
 
3595 jermar 594
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
3526 jermar 595
        for (j = 0; j < dps; j++) {
2845 jermar 596
            d = ((fat_dentry_t *)b->data) + j;
597
            switch (fat_classify_dentry(d)) {
598
            case FAT_DENTRY_SKIP:
3628 jermar 599
            case FAT_DENTRY_FREE:
2845 jermar 600
                continue;
601
            case FAT_DENTRY_LAST:
602
                block_put(b);
2951 jermar 603
                futex_up(&nodep->idx->lock);
2845 jermar 604
                return false;
605
            default:
606
            case FAT_DENTRY_VALID:
607
                block_put(b);
2951 jermar 608
                futex_up(&nodep->idx->lock);
2845 jermar 609
                return true;
610
            }
611
            block_put(b);
2951 jermar 612
            futex_up(&nodep->idx->lock);
2845 jermar 613
            return true;
614
        }
615
        block_put(b);
616
    }
617
 
2951 jermar 618
    futex_up(&nodep->idx->lock);
2845 jermar 619
    return false;
620
}
621
 
3621 jermar 622
void *fat_root_get(dev_handle_t dev_handle)
2844 jermar 623
{
3119 jermar 624
    return fat_node_get(dev_handle, 0);
2844 jermar 625
}
626
 
3621 jermar 627
char fat_plb_get_char(unsigned pos)
2844 jermar 628
{
629
    return fat_reg.plb_ro[pos % PLB_SIZE];
630
}
631
 
3621 jermar 632
bool fat_is_directory(void *node)
2831 jermar 633
{
634
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
635
}
636
 
3621 jermar 637
bool fat_is_file(void *node)
2831 jermar 638
{
639
    return ((fat_node_t *)node)->type == FAT_FILE;
640
}
641
 
2793 jermar 642
/** libfs operations */
643
libfs_ops_t fat_libfs_ops = {
644
    .match = fat_match,
645
    .node_get = fat_node_get,
2852 jermar 646
    .node_put = fat_node_put,
3609 jermar 647
    .create = fat_create_node,
648
    .destroy = fat_destroy_node,
2857 jermar 649
    .link = fat_link,
650
    .unlink = fat_unlink,
2831 jermar 651
    .index_get = fat_index_get,
652
    .size_get = fat_size_get,
653
    .lnkcnt_get = fat_lnkcnt_get,
2845 jermar 654
    .has_children = fat_has_children,
2844 jermar 655
    .root_get = fat_root_get,
656
    .plb_get_char = fat_plb_get_char,
2831 jermar 657
    .is_directory = fat_is_directory,
658
    .is_file = fat_is_file
2793 jermar 659
};
660
 
3625 jermar 661
/*
662
 * VFS operations.
663
 */
664
 
3110 jermar 665
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
666
{
667
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
3530 jermar 668
    fat_bs_t *bs;
3257 jermar 669
    uint16_t bps;
3119 jermar 670
    uint16_t rde;
3110 jermar 671
    int rc;
672
 
3530 jermar 673
    /* initialize libblock */
3537 jermar 674
    rc = block_init(dev_handle, BS_SIZE);
3257 jermar 675
    if (rc != EOK) {
3537 jermar 676
        ipc_answer_0(rid, rc);
3257 jermar 677
        return;
678
    }
679
 
3537 jermar 680
    /* prepare the boot block */
681
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
682
    if (rc != EOK) {
683
        block_fini(dev_handle);
684
        ipc_answer_0(rid, rc);
685
        return;
686
    }
687
 
3530 jermar 688
    /* get the buffer with the boot sector */
689
    bs = block_bb_get(dev_handle);
690
 
3119 jermar 691
    /* Read the number of root directory entries. */
3530 jermar 692
    bps = uint16_t_le2host(bs->bps);
693
    rde = uint16_t_le2host(bs->root_ent_max);
3119 jermar 694
 
3257 jermar 695
    if (bps != BS_SIZE) {
3530 jermar 696
        block_fini(dev_handle);
3257 jermar 697
        ipc_answer_0(rid, ENOTSUP);
698
        return;
699
    }
700
 
3539 jermar 701
    /* Initialize the block cache */
702
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
703
    if (rc != EOK) {
704
        block_fini(dev_handle);
705
        ipc_answer_0(rid, rc);
706
        return;
707
    }
708
 
3110 jermar 709
    rc = fat_idx_init_by_dev_handle(dev_handle);
710
    if (rc != EOK) {
3530 jermar 711
        block_fini(dev_handle);
3110 jermar 712
        ipc_answer_0(rid, rc);
713
        return;
714
    }
715
 
3119 jermar 716
    /* Initialize the root node. */
717
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
718
    if (!rootp) {
3530 jermar 719
        block_fini(dev_handle);
3119 jermar 720
        fat_idx_fini_by_dev_handle(dev_handle);
721
        ipc_answer_0(rid, ENOMEM);
722
        return;
723
    }
724
    fat_node_initialize(rootp);
725
 
726
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
727
    if (!ridxp) {
3530 jermar 728
        block_fini(dev_handle);
3119 jermar 729
        free(rootp);
730
        fat_idx_fini_by_dev_handle(dev_handle);
731
        ipc_answer_0(rid, ENOMEM);
732
        return;
733
    }
734
    assert(ridxp->index == 0);
735
    /* ridxp->lock held */
736
 
737
    rootp->type = FAT_DIRECTORY;
738
    rootp->firstc = FAT_CLST_ROOT;
739
    rootp->refcnt = 1;
3352 jermar 740
    rootp->lnkcnt = 0;  /* FS root is not linked */
3119 jermar 741
    rootp->size = rde * sizeof(fat_dentry_t);
742
    rootp->idx = ridxp;
743
    ridxp->nodep = rootp;
744
 
745
    futex_up(&ridxp->lock);
746
 
3352 jermar 747
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
3110 jermar 748
}
749
 
750
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
751
{
752
    ipc_answer_0(rid, ENOTSUP);
753
}
754
 
2627 jermar 755
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
756
{
2793 jermar 757
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
2627 jermar 758
}
759
 
3307 jermar 760
void fat_read(ipc_callid_t rid, ipc_call_t *request)
761
{
762
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
763
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
764
    off_t pos = (off_t)IPC_GET_ARG3(*request);
765
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3530 jermar 766
    fat_bs_t *bs;
3516 jermar 767
    uint16_t bps;
3308 jermar 768
    size_t bytes;
3530 jermar 769
    block_t *b;
3308 jermar 770
 
3307 jermar 771
    if (!nodep) {
772
        ipc_answer_0(rid, ENOENT);
773
        return;
774
    }
775
 
776
    ipc_callid_t callid;
777
    size_t len;
3314 jermar 778
    if (!ipc_data_read_receive(&callid, &len)) {
3307 jermar 779
        fat_node_put(nodep);
780
        ipc_answer_0(callid, EINVAL);
781
        ipc_answer_0(rid, EINVAL);
782
        return;
783
    }
784
 
3530 jermar 785
    bs = block_bb_get(dev_handle);
786
    bps = uint16_t_le2host(bs->bps);
3516 jermar 787
 
3307 jermar 788
    if (nodep->type == FAT_FILE) {
3335 jermar 789
        /*
790
         * Our strategy for regular file reads is to read one block at
791
         * most and make use of the possibility to return less data than
792
         * requested. This keeps the code very simple.
793
         */
3532 jermar 794
        if (pos >= nodep->size) {
3533 jermar 795
            /* reading beyond the EOF */
796
            bytes = 0;
3532 jermar 797
            (void) ipc_data_read_finalize(callid, NULL, 0);
798
        } else {
799
            bytes = min(len, bps - pos % bps);
800
            bytes = min(bytes, nodep->size - pos);
3595 jermar 801
            b = fat_block_get(bs, nodep, pos / bps,
802
                BLOCK_FLAGS_NONE);
3532 jermar 803
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
804
                bytes);
805
            block_put(b);
806
        }
3307 jermar 807
    } else {
3335 jermar 808
        unsigned bnum;
809
        off_t spos = pos;
810
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
811
        fat_dentry_t *d;
812
 
3307 jermar 813
        assert(nodep->type == FAT_DIRECTORY);
3335 jermar 814
        assert(nodep->size % bps == 0);
815
        assert(bps % sizeof(fat_dentry_t) == 0);
816
 
817
        /*
818
         * Our strategy for readdir() is to use the position pointer as
819
         * an index into the array of all dentries. On entry, it points
820
         * to the first unread dentry. If we skip any dentries, we bump
821
         * the position pointer accordingly.
822
         */
823
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
824
        while (bnum < nodep->size / bps) {
825
            off_t o;
826
 
3595 jermar 827
            b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
3335 jermar 828
            for (o = pos % (bps / sizeof(fat_dentry_t));
829
                o < bps / sizeof(fat_dentry_t);
830
                o++, pos++) {
831
                d = ((fat_dentry_t *)b->data) + o;
832
                switch (fat_classify_dentry(d)) {
833
                case FAT_DENTRY_SKIP:
3628 jermar 834
                case FAT_DENTRY_FREE:
3335 jermar 835
                    continue;
836
                case FAT_DENTRY_LAST:
837
                    block_put(b);
838
                    goto miss;
839
                default:
840
                case FAT_DENTRY_VALID:
3628 jermar 841
                    fat_dentry_name_get(d, name);
3335 jermar 842
                    block_put(b);
843
                    goto hit;
844
                }
845
            }
846
            block_put(b);
847
            bnum++;
848
        }
849
miss:
3307 jermar 850
        fat_node_put(nodep);
3335 jermar 851
        ipc_answer_0(callid, ENOENT);
852
        ipc_answer_1(rid, ENOENT, 0);
3307 jermar 853
        return;
3335 jermar 854
hit:
855
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
856
        bytes = (pos - spos) + 1;
3307 jermar 857
    }
858
 
859
    fat_node_put(nodep);
3308 jermar 860
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
3307 jermar 861
}
862
 
3497 jermar 863
void fat_write(ipc_callid_t rid, ipc_call_t *request)
864
{
3499 jermar 865
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
866
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
867
    off_t pos = (off_t)IPC_GET_ARG3(*request);
868
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3530 jermar 869
    fat_bs_t *bs;
3499 jermar 870
    size_t bytes;
3530 jermar 871
    block_t *b;
3499 jermar 872
    uint16_t bps;
873
    unsigned spc;
3572 jermar 874
    unsigned bpc;       /* bytes per cluster */
3501 jermar 875
    off_t boundary;
3595 jermar 876
    int flags = BLOCK_FLAGS_NONE;
3499 jermar 877
 
878
    if (!nodep) {
879
        ipc_answer_0(rid, ENOENT);
880
        return;
881
    }
882
 
883
    ipc_callid_t callid;
884
    size_t len;
885
    if (!ipc_data_write_receive(&callid, &len)) {
886
        fat_node_put(nodep);
887
        ipc_answer_0(callid, EINVAL);
888
        ipc_answer_0(rid, EINVAL);
889
        return;
890
    }
891
 
3572 jermar 892
    bs = block_bb_get(dev_handle);
893
    bps = uint16_t_le2host(bs->bps);
894
    spc = bs->spc;
895
    bpc = bps * spc;
896
 
3499 jermar 897
    /*
898
     * In all scenarios, we will attempt to write out only one block worth
899
     * of data at maximum. There might be some more efficient approaches,
900
     * but this one greatly simplifies fat_write(). Note that we can afford
901
     * to do this because the client must be ready to handle the return
902
     * value signalizing a smaller number of bytes written.
903
     */
904
    bytes = min(len, bps - pos % bps);
3595 jermar 905
    if (bytes == bps)
906
        flags |= BLOCK_FLAGS_NOREAD;
3499 jermar 907
 
3572 jermar 908
    boundary = ROUND_UP(nodep->size, bpc);
3501 jermar 909
    if (pos < boundary) {
3499 jermar 910
        /*
911
         * This is the easier case - we are either overwriting already
912
         * existing contents or writing behind the EOF, but still within
913
         * the limits of the last cluster. The node size may grow to the
914
         * next block size boundary.
915
         */
3530 jermar 916
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
3595 jermar 917
        b = fat_block_get(bs, nodep, pos / bps, flags);
3499 jermar 918
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
919
            bytes);
920
        b->dirty = true;        /* need to sync block */
3500 jermar 921
        block_put(b);
3499 jermar 922
        if (pos + bytes > nodep->size) {
923
            nodep->size = pos + bytes;
924
            nodep->dirty = true;    /* need to sync node */
925
        }
3573 jermar 926
        ipc_answer_2(rid, EOK, bytes, nodep->size);
3499 jermar 927
        fat_node_put(nodep);
928
        return;
929
    } else {
930
        /*
931
         * This is the more difficult case. We must allocate new
932
         * clusters for the node and zero them out.
933
         */
3500 jermar 934
        int status;
3499 jermar 935
        unsigned nclsts;
3548 jermar 936
        fat_cluster_t mcl, lcl;
937
 
3572 jermar 938
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
3500 jermar 939
        /* create an independent chain of nclsts clusters in all FATs */
3572 jermar 940
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
3500 jermar 941
        if (status != EOK) {
942
            /* could not allocate a chain of nclsts clusters */
943
            fat_node_put(nodep);
944
            ipc_answer_0(callid, status);
945
            ipc_answer_0(rid, status);
946
            return;
947
        }
948
        /* zero fill any gaps */
3530 jermar 949
        fat_fill_gap(bs, nodep, mcl, pos);
3595 jermar 950
        b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
951
            flags);
3500 jermar 952
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
953
            bytes);
3501 jermar 954
        b->dirty = true;        /* need to sync block */
3500 jermar 955
        block_put(b);
956
        /*
957
         * Append the cluster chain starting in mcl to the end of the
958
         * node's cluster chain.
959
         */
3530 jermar 960
        fat_append_clusters(bs, nodep, mcl);
3500 jermar 961
        nodep->size = pos + bytes;
3501 jermar 962
        nodep->dirty = true;        /* need to sync node */
3573 jermar 963
        ipc_answer_2(rid, EOK, bytes, nodep->size);
3500 jermar 964
        fat_node_put(nodep);
965
        return;
3499 jermar 966
    }
3497 jermar 967
}
968
 
3546 jermar 969
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
970
{
3548 jermar 971
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
972
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
973
    size_t size = (off_t)IPC_GET_ARG3(*request);
974
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3572 jermar 975
    fat_bs_t *bs;
976
    uint16_t bps;
977
    uint8_t spc;
978
    unsigned bpc;   /* bytes per cluster */
3548 jermar 979
    int rc;
980
 
981
    if (!nodep) {
982
        ipc_answer_0(rid, ENOENT);
983
        return;
984
    }
985
 
3572 jermar 986
    bs = block_bb_get(dev_handle);
987
    bps = uint16_t_le2host(bs->bps);
988
    spc = bs->spc;
989
    bpc = bps * spc;
990
 
3548 jermar 991
    if (nodep->size == size) {
992
        rc = EOK;
993
    } else if (nodep->size < size) {
994
        /*
3572 jermar 995
         * The standard says we have the freedom to grow the node.
3548 jermar 996
         * For now, we simply return an error.
997
         */
998
        rc = EINVAL;
3572 jermar 999
    } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
1000
        /*
1001
         * The node will be shrunk, but no clusters will be deallocated.
1002
         */
1003
        nodep->size = size;
1004
        nodep->dirty = true;        /* need to sync node */
1005
        rc = EOK;  
3548 jermar 1006
    } else {
1007
        /*
3572 jermar 1008
         * The node will be shrunk, clusters will be deallocated.
3548 jermar 1009
         */
3572 jermar 1010
        if (size == 0) {
1011
            fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
1012
        } else {
1013
            fat_cluster_t lastc;
1014
            (void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
1015
                &lastc, (size - 1) / bpc);
1016
            fat_chop_clusters(bs, nodep, lastc);
1017
        }
1018
        nodep->size = size;
1019
        nodep->dirty = true;        /* need to sync node */
1020
        rc = EOK;  
3548 jermar 1021
    }
1022
    fat_node_put(nodep);
1023
    ipc_answer_0(rid, rc);
1024
    return;
3546 jermar 1025
}
1026
 
3621 jermar 1027
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1028
{
1029
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1030
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1031
    int rc;
1032
 
1033
    fat_node_t *nodep = fat_node_get(dev_handle, index);
1034
    if (!nodep) {
1035
        ipc_answer_0(rid, ENOENT);
1036
        return;
1037
    }
1038
 
1039
    rc = fat_destroy_node(nodep);
1040
    ipc_answer_0(rid, rc);
1041
}
1042
 
2627 jermar 1043
/**
1044
 * @}
1045
 */