Subversion Repositories HelenOS

Rev

Rev 3628 | Rev 3633 | 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;
291
 
292
    nodep = fat_node_get_new();
293
    if (!nodep)
294
        return NULL;
295
    idxp = fat_idx_get_new(dev_handle);
296
    if (!idxp) {
297
        fat_node_put(nodep);
298
        return NULL;
299
    }
300
    /* idxp->lock held */
301
    if (flags & L_DIRECTORY) {
302
        nodep->type = FAT_DIRECTORY;
303
    } else {
304
        nodep->type = FAT_FILE;
305
    }
306
    nodep->size = 0;
307
    nodep->firstc = FAT_CLST_RES0;
308
    nodep->lnkcnt = 0;  /* not linked anywhere */
309
    nodep->refcnt = 1;
310
 
311
    nodep->idx = idxp;
312
    idxp->nodep = nodep;
313
 
314
    futex_up(&idxp->lock);
315
    return nodep;
2857 jermar 316
}
317
 
3621 jermar 318
int fat_destroy_node(void *node)
2857 jermar 319
{
3621 jermar 320
    fat_node_t *nodep = (fat_node_t *)node;
321
    fat_bs_t *bs;
322
 
323
    /*
324
     * The node is not reachable from the file system. This means that the
325
     * link count should be zero and that the index structure cannot be
326
     * found in the position hash. Obviously, we don't need to lock the node
327
     * nor its index structure.
328
     */
329
    assert(nodep->lnkcnt == 0);
330
 
331
    /*
332
     * The node may not have any children.
333
     */
334
    assert(fat_has_children(node) == false);
335
 
336
    bs = block_bb_get(nodep->idx->dev_handle);
337
    if (nodep->firstc != FAT_CLST_RES0) {
338
        assert(nodep->size);
339
        /* Free all clusters allocated to the node. */
340
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
341
    }
342
 
343
    fat_idx_destroy(nodep->idx);
344
    free(nodep);
345
    return EOK;
2857 jermar 346
}
347
 
3625 jermar 348
int fat_link(void *prnt, void *chld, const char *name)
2857 jermar 349
{
3628 jermar 350
    fat_node_t *parentp = (fat_node_t *)prnt;
351
    fat_node_t *childp = (fat_node_t *)chld;
352
    fat_dentry_t *d;
353
    fat_bs_t *bs;
354
    block_t *b;
355
    int i, j;
356
    uint16_t bps;
357
    unsigned dps;
358
    unsigned blocks;
359
 
360
    futex_down(&childp->lock);
361
    if (childp->lnkcnt == 1) {
362
        /*
363
         * On FAT, we don't support multiple hard links.
364
         */
365
        futex_up(&childp->lock);
366
        return EMLINK;
367
    }
368
    assert(childp->lnkcnt == 0);
369
    futex_up(&childp->lock);
370
 
371
    if (!fat_dentry_name_verify(name)) {
372
        /*
373
         * Attempt to create unsupported name.
374
         */
375
        return ENOTSUP;
376
    }
377
 
378
    /*
379
     * Get us an unused parent node's dentry or grow the parent and allocate
380
     * a new one.
381
     */
382
 
383
    futex_down(&parentp->idx->lock);
384
    bs = block_bb_get(parentp->idx->dev_handle);
385
    bps = uint16_t_le2host(bs->bps);
386
    dps = bps / sizeof(fat_dentry_t);
387
 
388
    blocks = parentp->size / bps;
389
 
390
    for (i = 0; i < blocks; i++) {
391
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
392
        for (j = 0; j < dps; j++) {
393
            d = ((fat_dentry_t *)b->data) + j;
394
            switch (fat_classify_dentry(d)) {
395
            case FAT_DENTRY_SKIP:
396
            case FAT_DENTRY_VALID:
397
                /* skipping used and meta entries */
398
                continue;
399
            case FAT_DENTRY_FREE:
400
            case FAT_DENTRY_LAST:
401
                /* found an empty slot */
402
                goto hit;
403
            }
404
        }
405
        block_put(b);
406
    }
407
 
408
    /*
409
     * We need to grow the parent in order to create a new unused dentry.
410
     */
411
    futex_up(&parentp->idx->lock);
412
    return ENOTSUP; /* XXX */
413
 
414
hit:
415
    /*
416
     * At this point we only establish the link between the parent and the
417
     * child.  The dentry, except of the name and the extension, will remain
418
     * uninitialized until the the corresponding node is synced. Thus the
419
     * valid dentry data is kept in the child node structure.
420
     */
421
    memset(d, 0, sizeof(fat_dentry_t));
422
    fat_dentry_name_set(d, name);
423
    b->dirty = true;        /* need to sync block */
424
    block_put(b);
425
    futex_up(&parentp->idx->lock);
426
 
427
    futex_down(&childp->idx->lock);
428
    childp->idx->pfc = parentp->firstc;
429
    childp->idx->pdi = i * dps + j;
430
    futex_up(&childp->idx->lock);
431
 
432
    futex_down(&childp->lock);
433
    childp->lnkcnt = 1;
434
    childp->dirty = true;       /* need to sync node */
435
    futex_up(&childp->lock);
436
 
437
    /*
438
     * Hash in the index structure into the position hash.
439
     */
440
    fat_idx_hashin(childp->idx);
441
 
442
    return EOK;
2857 jermar 443
}
444
 
3621 jermar 445
int fat_unlink(void *prnt, void *chld)
2857 jermar 446
{
447
    return ENOTSUP; /* not supported at the moment */
448
}
449
 
3621 jermar 450
void *fat_match(void *prnt, const char *component)
2793 jermar 451
{
3530 jermar 452
    fat_bs_t *bs;
2793 jermar 453
    fat_node_t *parentp = (fat_node_t *)prnt;
454
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
2822 jermar 455
    unsigned i, j;
2828 jermar 456
    unsigned bps;       /* bytes per sector */
2822 jermar 457
    unsigned dps;       /* dentries per sector */
458
    unsigned blocks;
2793 jermar 459
    fat_dentry_t *d;
3530 jermar 460
    block_t *b;
2793 jermar 461
 
2953 jermar 462
    futex_down(&parentp->idx->lock);
3530 jermar 463
    bs = block_bb_get(parentp->idx->dev_handle);
464
    bps = uint16_t_le2host(bs->bps);
2828 jermar 465
    dps = bps / sizeof(fat_dentry_t);
3526 jermar 466
    blocks = parentp->size / bps;
2822 jermar 467
    for (i = 0; i < blocks; i++) {
3595 jermar 468
        b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NONE);
3526 jermar 469
        for (j = 0; j < dps; j++) {
2822 jermar 470
            d = ((fat_dentry_t *)b->data) + j;
2845 jermar 471
            switch (fat_classify_dentry(d)) {
472
            case FAT_DENTRY_SKIP:
3628 jermar 473
            case FAT_DENTRY_FREE:
2822 jermar 474
                continue;
2845 jermar 475
            case FAT_DENTRY_LAST:
2822 jermar 476
                block_put(b);
2953 jermar 477
                futex_up(&parentp->idx->lock);
2822 jermar 478
                return NULL;
2845 jermar 479
            default:
480
            case FAT_DENTRY_VALID:
3628 jermar 481
                fat_dentry_name_get(d, name);
2845 jermar 482
                break;
2822 jermar 483
            }
3272 jermar 484
            if (stricmp(name, component) == 0) {
2822 jermar 485
                /* hit */
2951 jermar 486
                void *node;
2953 jermar 487
                /*
488
                 * Assume tree hierarchy for locking.  We
489
                 * already have the parent and now we are going
490
                 * to lock the child.  Never lock in the oposite
491
                 * order.
492
                 */
2890 jermar 493
                fat_idx_t *idx = fat_idx_get_by_pos(
2881 jermar 494
                    parentp->idx->dev_handle, parentp->firstc,
2864 jermar 495
                    i * dps + j);
2953 jermar 496
                futex_up(&parentp->idx->lock);
2890 jermar 497
                if (!idx) {
498
                    /*
499
                     * Can happen if memory is low or if we
500
                     * run out of 32-bit indices.
501
                     */
502
                    block_put(b);
503
                    return NULL;
504
                }
2951 jermar 505
                node = fat_node_get_core(idx);
506
                futex_up(&idx->lock);
2822 jermar 507
                block_put(b);
508
                return node;
509
            }
2793 jermar 510
        }
2822 jermar 511
        block_put(b);
2639 jermar 512
    }
3516 jermar 513
 
2953 jermar 514
    futex_up(&parentp->idx->lock);
2793 jermar 515
    return NULL;
2638 jermar 516
}
517
 
3621 jermar 518
fs_index_t fat_index_get(void *node)
2831 jermar 519
{
520
    fat_node_t *fnodep = (fat_node_t *)node;
521
    if (!fnodep)
522
        return 0;
2864 jermar 523
    return fnodep->idx->index;
2831 jermar 524
}
525
 
3621 jermar 526
size_t fat_size_get(void *node)
2831 jermar 527
{
528
    return ((fat_node_t *)node)->size;
529
}
530
 
3621 jermar 531
unsigned fat_lnkcnt_get(void *node)
2831 jermar 532
{
533
    return ((fat_node_t *)node)->lnkcnt;
534
}
535
 
3621 jermar 536
bool fat_has_children(void *node)
2845 jermar 537
{
3530 jermar 538
    fat_bs_t *bs;
2845 jermar 539
    fat_node_t *nodep = (fat_node_t *)node;
540
    unsigned bps;
541
    unsigned dps;
542
    unsigned blocks;
3530 jermar 543
    block_t *b;
2845 jermar 544
    unsigned i, j;
545
 
546
    if (nodep->type != FAT_DIRECTORY)
547
        return false;
3526 jermar 548
 
2951 jermar 549
    futex_down(&nodep->idx->lock);
3530 jermar 550
    bs = block_bb_get(nodep->idx->dev_handle);
551
    bps = uint16_t_le2host(bs->bps);
2845 jermar 552
    dps = bps / sizeof(fat_dentry_t);
553
 
3526 jermar 554
    blocks = nodep->size / bps;
2845 jermar 555
 
556
    for (i = 0; i < blocks; i++) {
557
        fat_dentry_t *d;
558
 
3595 jermar 559
        b = fat_block_get(bs, nodep, i, BLOCK_FLAGS_NONE);
3526 jermar 560
        for (j = 0; j < dps; j++) {
2845 jermar 561
            d = ((fat_dentry_t *)b->data) + j;
562
            switch (fat_classify_dentry(d)) {
563
            case FAT_DENTRY_SKIP:
3628 jermar 564
            case FAT_DENTRY_FREE:
2845 jermar 565
                continue;
566
            case FAT_DENTRY_LAST:
567
                block_put(b);
2951 jermar 568
                futex_up(&nodep->idx->lock);
2845 jermar 569
                return false;
570
            default:
571
            case FAT_DENTRY_VALID:
572
                block_put(b);
2951 jermar 573
                futex_up(&nodep->idx->lock);
2845 jermar 574
                return true;
575
            }
576
            block_put(b);
2951 jermar 577
            futex_up(&nodep->idx->lock);
2845 jermar 578
            return true;
579
        }
580
        block_put(b);
581
    }
582
 
2951 jermar 583
    futex_up(&nodep->idx->lock);
2845 jermar 584
    return false;
585
}
586
 
3621 jermar 587
void *fat_root_get(dev_handle_t dev_handle)
2844 jermar 588
{
3119 jermar 589
    return fat_node_get(dev_handle, 0);
2844 jermar 590
}
591
 
3621 jermar 592
char fat_plb_get_char(unsigned pos)
2844 jermar 593
{
594
    return fat_reg.plb_ro[pos % PLB_SIZE];
595
}
596
 
3621 jermar 597
bool fat_is_directory(void *node)
2831 jermar 598
{
599
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
600
}
601
 
3621 jermar 602
bool fat_is_file(void *node)
2831 jermar 603
{
604
    return ((fat_node_t *)node)->type == FAT_FILE;
605
}
606
 
2793 jermar 607
/** libfs operations */
608
libfs_ops_t fat_libfs_ops = {
609
    .match = fat_match,
610
    .node_get = fat_node_get,
2852 jermar 611
    .node_put = fat_node_put,
3609 jermar 612
    .create = fat_create_node,
613
    .destroy = fat_destroy_node,
2857 jermar 614
    .link = fat_link,
615
    .unlink = fat_unlink,
2831 jermar 616
    .index_get = fat_index_get,
617
    .size_get = fat_size_get,
618
    .lnkcnt_get = fat_lnkcnt_get,
2845 jermar 619
    .has_children = fat_has_children,
2844 jermar 620
    .root_get = fat_root_get,
621
    .plb_get_char = fat_plb_get_char,
2831 jermar 622
    .is_directory = fat_is_directory,
623
    .is_file = fat_is_file
2793 jermar 624
};
625
 
3625 jermar 626
/*
627
 * VFS operations.
628
 */
629
 
3110 jermar 630
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
631
{
632
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
3530 jermar 633
    fat_bs_t *bs;
3257 jermar 634
    uint16_t bps;
3119 jermar 635
    uint16_t rde;
3110 jermar 636
    int rc;
637
 
3530 jermar 638
    /* initialize libblock */
3537 jermar 639
    rc = block_init(dev_handle, BS_SIZE);
3257 jermar 640
    if (rc != EOK) {
3537 jermar 641
        ipc_answer_0(rid, rc);
3257 jermar 642
        return;
643
    }
644
 
3537 jermar 645
    /* prepare the boot block */
646
    rc = block_bb_read(dev_handle, BS_BLOCK * BS_SIZE, BS_SIZE);
647
    if (rc != EOK) {
648
        block_fini(dev_handle);
649
        ipc_answer_0(rid, rc);
650
        return;
651
    }
652
 
3530 jermar 653
    /* get the buffer with the boot sector */
654
    bs = block_bb_get(dev_handle);
655
 
3119 jermar 656
    /* Read the number of root directory entries. */
3530 jermar 657
    bps = uint16_t_le2host(bs->bps);
658
    rde = uint16_t_le2host(bs->root_ent_max);
3119 jermar 659
 
3257 jermar 660
    if (bps != BS_SIZE) {
3530 jermar 661
        block_fini(dev_handle);
3257 jermar 662
        ipc_answer_0(rid, ENOTSUP);
663
        return;
664
    }
665
 
3539 jermar 666
    /* Initialize the block cache */
667
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
668
    if (rc != EOK) {
669
        block_fini(dev_handle);
670
        ipc_answer_0(rid, rc);
671
        return;
672
    }
673
 
3110 jermar 674
    rc = fat_idx_init_by_dev_handle(dev_handle);
675
    if (rc != EOK) {
3530 jermar 676
        block_fini(dev_handle);
3110 jermar 677
        ipc_answer_0(rid, rc);
678
        return;
679
    }
680
 
3119 jermar 681
    /* Initialize the root node. */
682
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
683
    if (!rootp) {
3530 jermar 684
        block_fini(dev_handle);
3119 jermar 685
        fat_idx_fini_by_dev_handle(dev_handle);
686
        ipc_answer_0(rid, ENOMEM);
687
        return;
688
    }
689
    fat_node_initialize(rootp);
690
 
691
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
692
    if (!ridxp) {
3530 jermar 693
        block_fini(dev_handle);
3119 jermar 694
        free(rootp);
695
        fat_idx_fini_by_dev_handle(dev_handle);
696
        ipc_answer_0(rid, ENOMEM);
697
        return;
698
    }
699
    assert(ridxp->index == 0);
700
    /* ridxp->lock held */
701
 
702
    rootp->type = FAT_DIRECTORY;
703
    rootp->firstc = FAT_CLST_ROOT;
704
    rootp->refcnt = 1;
3352 jermar 705
    rootp->lnkcnt = 0;  /* FS root is not linked */
3119 jermar 706
    rootp->size = rde * sizeof(fat_dentry_t);
707
    rootp->idx = ridxp;
708
    ridxp->nodep = rootp;
709
 
710
    futex_up(&ridxp->lock);
711
 
3352 jermar 712
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
3110 jermar 713
}
714
 
715
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
716
{
717
    ipc_answer_0(rid, ENOTSUP);
718
}
719
 
2627 jermar 720
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
721
{
2793 jermar 722
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
2627 jermar 723
}
724
 
3307 jermar 725
void fat_read(ipc_callid_t rid, ipc_call_t *request)
726
{
727
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
728
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
729
    off_t pos = (off_t)IPC_GET_ARG3(*request);
730
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3530 jermar 731
    fat_bs_t *bs;
3516 jermar 732
    uint16_t bps;
3308 jermar 733
    size_t bytes;
3530 jermar 734
    block_t *b;
3308 jermar 735
 
3307 jermar 736
    if (!nodep) {
737
        ipc_answer_0(rid, ENOENT);
738
        return;
739
    }
740
 
741
    ipc_callid_t callid;
742
    size_t len;
3314 jermar 743
    if (!ipc_data_read_receive(&callid, &len)) {
3307 jermar 744
        fat_node_put(nodep);
745
        ipc_answer_0(callid, EINVAL);
746
        ipc_answer_0(rid, EINVAL);
747
        return;
748
    }
749
 
3530 jermar 750
    bs = block_bb_get(dev_handle);
751
    bps = uint16_t_le2host(bs->bps);
3516 jermar 752
 
3307 jermar 753
    if (nodep->type == FAT_FILE) {
3335 jermar 754
        /*
755
         * Our strategy for regular file reads is to read one block at
756
         * most and make use of the possibility to return less data than
757
         * requested. This keeps the code very simple.
758
         */
3532 jermar 759
        if (pos >= nodep->size) {
3533 jermar 760
            /* reading beyond the EOF */
761
            bytes = 0;
3532 jermar 762
            (void) ipc_data_read_finalize(callid, NULL, 0);
763
        } else {
764
            bytes = min(len, bps - pos % bps);
765
            bytes = min(bytes, nodep->size - pos);
3595 jermar 766
            b = fat_block_get(bs, nodep, pos / bps,
767
                BLOCK_FLAGS_NONE);
3532 jermar 768
            (void) ipc_data_read_finalize(callid, b->data + pos % bps,
769
                bytes);
770
            block_put(b);
771
        }
3307 jermar 772
    } else {
3335 jermar 773
        unsigned bnum;
774
        off_t spos = pos;
775
        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
776
        fat_dentry_t *d;
777
 
3307 jermar 778
        assert(nodep->type == FAT_DIRECTORY);
3335 jermar 779
        assert(nodep->size % bps == 0);
780
        assert(bps % sizeof(fat_dentry_t) == 0);
781
 
782
        /*
783
         * Our strategy for readdir() is to use the position pointer as
784
         * an index into the array of all dentries. On entry, it points
785
         * to the first unread dentry. If we skip any dentries, we bump
786
         * the position pointer accordingly.
787
         */
788
        bnum = (pos * sizeof(fat_dentry_t)) / bps;
789
        while (bnum < nodep->size / bps) {
790
            off_t o;
791
 
3595 jermar 792
            b = fat_block_get(bs, nodep, bnum, BLOCK_FLAGS_NONE);
3335 jermar 793
            for (o = pos % (bps / sizeof(fat_dentry_t));
794
                o < bps / sizeof(fat_dentry_t);
795
                o++, pos++) {
796
                d = ((fat_dentry_t *)b->data) + o;
797
                switch (fat_classify_dentry(d)) {
798
                case FAT_DENTRY_SKIP:
3628 jermar 799
                case FAT_DENTRY_FREE:
3335 jermar 800
                    continue;
801
                case FAT_DENTRY_LAST:
802
                    block_put(b);
803
                    goto miss;
804
                default:
805
                case FAT_DENTRY_VALID:
3628 jermar 806
                    fat_dentry_name_get(d, name);
3335 jermar 807
                    block_put(b);
808
                    goto hit;
809
                }
810
            }
811
            block_put(b);
812
            bnum++;
813
        }
814
miss:
3307 jermar 815
        fat_node_put(nodep);
3335 jermar 816
        ipc_answer_0(callid, ENOENT);
817
        ipc_answer_1(rid, ENOENT, 0);
3307 jermar 818
        return;
3335 jermar 819
hit:
820
        (void) ipc_data_read_finalize(callid, name, strlen(name) + 1);
821
        bytes = (pos - spos) + 1;
3307 jermar 822
    }
823
 
824
    fat_node_put(nodep);
3308 jermar 825
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
3307 jermar 826
}
827
 
3497 jermar 828
void fat_write(ipc_callid_t rid, ipc_call_t *request)
829
{
3499 jermar 830
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
831
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
832
    off_t pos = (off_t)IPC_GET_ARG3(*request);
833
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3530 jermar 834
    fat_bs_t *bs;
3499 jermar 835
    size_t bytes;
3530 jermar 836
    block_t *b;
3499 jermar 837
    uint16_t bps;
838
    unsigned spc;
3572 jermar 839
    unsigned bpc;       /* bytes per cluster */
3501 jermar 840
    off_t boundary;
3595 jermar 841
    int flags = BLOCK_FLAGS_NONE;
3499 jermar 842
 
843
    if (!nodep) {
844
        ipc_answer_0(rid, ENOENT);
845
        return;
846
    }
847
 
848
    ipc_callid_t callid;
849
    size_t len;
850
    if (!ipc_data_write_receive(&callid, &len)) {
851
        fat_node_put(nodep);
852
        ipc_answer_0(callid, EINVAL);
853
        ipc_answer_0(rid, EINVAL);
854
        return;
855
    }
856
 
3572 jermar 857
    bs = block_bb_get(dev_handle);
858
    bps = uint16_t_le2host(bs->bps);
859
    spc = bs->spc;
860
    bpc = bps * spc;
861
 
3499 jermar 862
    /*
863
     * In all scenarios, we will attempt to write out only one block worth
864
     * of data at maximum. There might be some more efficient approaches,
865
     * but this one greatly simplifies fat_write(). Note that we can afford
866
     * to do this because the client must be ready to handle the return
867
     * value signalizing a smaller number of bytes written.
868
     */
869
    bytes = min(len, bps - pos % bps);
3595 jermar 870
    if (bytes == bps)
871
        flags |= BLOCK_FLAGS_NOREAD;
3499 jermar 872
 
3572 jermar 873
    boundary = ROUND_UP(nodep->size, bpc);
3501 jermar 874
    if (pos < boundary) {
3499 jermar 875
        /*
876
         * This is the easier case - we are either overwriting already
877
         * existing contents or writing behind the EOF, but still within
878
         * the limits of the last cluster. The node size may grow to the
879
         * next block size boundary.
880
         */
3530 jermar 881
        fat_fill_gap(bs, nodep, FAT_CLST_RES0, pos);
3595 jermar 882
        b = fat_block_get(bs, nodep, pos / bps, flags);
3499 jermar 883
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
884
            bytes);
885
        b->dirty = true;        /* need to sync block */
3500 jermar 886
        block_put(b);
3499 jermar 887
        if (pos + bytes > nodep->size) {
888
            nodep->size = pos + bytes;
889
            nodep->dirty = true;    /* need to sync node */
890
        }
3573 jermar 891
        ipc_answer_2(rid, EOK, bytes, nodep->size);
3499 jermar 892
        fat_node_put(nodep);
893
        return;
894
    } else {
895
        /*
896
         * This is the more difficult case. We must allocate new
897
         * clusters for the node and zero them out.
898
         */
3500 jermar 899
        int status;
3499 jermar 900
        unsigned nclsts;
3548 jermar 901
        fat_cluster_t mcl, lcl;
902
 
3572 jermar 903
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
3500 jermar 904
        /* create an independent chain of nclsts clusters in all FATs */
3572 jermar 905
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
3500 jermar 906
        if (status != EOK) {
907
            /* could not allocate a chain of nclsts clusters */
908
            fat_node_put(nodep);
909
            ipc_answer_0(callid, status);
910
            ipc_answer_0(rid, status);
911
            return;
912
        }
913
        /* zero fill any gaps */
3530 jermar 914
        fat_fill_gap(bs, nodep, mcl, pos);
3595 jermar 915
        b = _fat_block_get(bs, dev_handle, lcl, (pos / bps) % spc,
916
            flags);
3500 jermar 917
        (void) ipc_data_write_finalize(callid, b->data + pos % bps,
918
            bytes);
3501 jermar 919
        b->dirty = true;        /* need to sync block */
3500 jermar 920
        block_put(b);
921
        /*
922
         * Append the cluster chain starting in mcl to the end of the
923
         * node's cluster chain.
924
         */
3530 jermar 925
        fat_append_clusters(bs, nodep, mcl);
3500 jermar 926
        nodep->size = pos + bytes;
3501 jermar 927
        nodep->dirty = true;        /* need to sync node */
3573 jermar 928
        ipc_answer_2(rid, EOK, bytes, nodep->size);
3500 jermar 929
        fat_node_put(nodep);
930
        return;
3499 jermar 931
    }
3497 jermar 932
}
933
 
3546 jermar 934
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
935
{
3548 jermar 936
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
937
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
938
    size_t size = (off_t)IPC_GET_ARG3(*request);
939
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
3572 jermar 940
    fat_bs_t *bs;
941
    uint16_t bps;
942
    uint8_t spc;
943
    unsigned bpc;   /* bytes per cluster */
3548 jermar 944
    int rc;
945
 
946
    if (!nodep) {
947
        ipc_answer_0(rid, ENOENT);
948
        return;
949
    }
950
 
3572 jermar 951
    bs = block_bb_get(dev_handle);
952
    bps = uint16_t_le2host(bs->bps);
953
    spc = bs->spc;
954
    bpc = bps * spc;
955
 
3548 jermar 956
    if (nodep->size == size) {
957
        rc = EOK;
958
    } else if (nodep->size < size) {
959
        /*
3572 jermar 960
         * The standard says we have the freedom to grow the node.
3548 jermar 961
         * For now, we simply return an error.
962
         */
963
        rc = EINVAL;
3572 jermar 964
    } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
965
        /*
966
         * The node will be shrunk, but no clusters will be deallocated.
967
         */
968
        nodep->size = size;
969
        nodep->dirty = true;        /* need to sync node */
970
        rc = EOK;  
3548 jermar 971
    } else {
972
        /*
3572 jermar 973
         * The node will be shrunk, clusters will be deallocated.
3548 jermar 974
         */
3572 jermar 975
        if (size == 0) {
976
            fat_chop_clusters(bs, nodep, FAT_CLST_RES0);
977
        } else {
978
            fat_cluster_t lastc;
979
            (void) fat_cluster_walk(bs, dev_handle, nodep->firstc,
980
                &lastc, (size - 1) / bpc);
981
            fat_chop_clusters(bs, nodep, lastc);
982
        }
983
        nodep->size = size;
984
        nodep->dirty = true;        /* need to sync node */
985
        rc = EOK;  
3548 jermar 986
    }
987
    fat_node_put(nodep);
988
    ipc_answer_0(rid, rc);
989
    return;
3546 jermar 990
}
991
 
3621 jermar 992
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
993
{
994
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
995
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
996
    int rc;
997
 
998
    fat_node_t *nodep = fat_node_get(dev_handle, index);
999
    if (!nodep) {
1000
        ipc_answer_0(rid, ENOENT);
1001
        return;
1002
    }
1003
 
1004
    rc = fat_destroy_node(nodep);
1005
    ipc_answer_0(rid, rc);
1006
}
1007
 
2627 jermar 1008
/**
1009
 * @}
1010
 */