Subversion Repositories HelenOS

Rev

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

Rev 4327 Rev 4581
Line 46... Line 46...
46
#include <ipc/devmap.h>
46
#include <ipc/devmap.h>
47
#include <async.h>
47
#include <async.h>
48
#include <errno.h>
48
#include <errno.h>
49
#include <string.h>
49
#include <string.h>
50
#include <byteorder.h>
50
#include <byteorder.h>
51
#include <libadt/hash_table.h>
51
#include <adt/hash_table.h>
52
#include <libadt/list.h>
52
#include <adt/list.h>
53
#include <assert.h>
53
#include <assert.h>
54
#include <futex.h>
54
#include <fibril_sync.h>
55
#include <sys/mman.h>
55
#include <sys/mman.h>
56
#include <align.h>
56
#include <align.h>
57
 
57
 
-
 
58
#define FAT_NODE(node)  ((node) ? (fat_node_t *) (node)->data : NULL)
-
 
59
#define FS_NODE(node)   ((node) ? (node)->bp : NULL)
-
 
60
 
58
/** Futex protecting the list of cached free FAT nodes. */
61
/** Mutex protecting the list of cached free FAT nodes. */
59
static futex_t ffn_futex = FUTEX_INITIALIZER;
62
static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
60
 
63
 
61
/** List of cached free FAT nodes. */
64
/** List of cached free FAT nodes. */
62
static LIST_INITIALIZE(ffn_head);
65
static LIST_INITIALIZE(ffn_head);
63
 
66
 
64
static void fat_node_initialize(fat_node_t *node)
67
static void fat_node_initialize(fat_node_t *node)
65
{
68
{
66
    futex_initialize(&node->lock, 1);
69
    fibril_mutex_initialize(&node->lock);
-
 
70
    node->bp = NULL;
67
    node->idx = NULL;
71
    node->idx = NULL;
68
    node->type = 0;
72
    node->type = 0;
69
    link_initialize(&node->ffn_link);
73
    link_initialize(&node->ffn_link);
70
    node->size = 0;
74
    node->size = 0;
71
    node->lnkcnt = 0;
75
    node->lnkcnt = 0;
Line 106... Line 110...
106
    block_put(b);
110
    block_put(b);
107
}
111
}
108
 
112
 
109
static fat_node_t *fat_node_get_new(void)
113
static fat_node_t *fat_node_get_new(void)
110
{
114
{
-
 
115
    fs_node_t *fn;
111
    fat_node_t *nodep;
116
    fat_node_t *nodep;
112
 
117
 
113
    futex_down(&ffn_futex);
118
    fibril_mutex_lock(&ffn_mutex);
114
    if (!list_empty(&ffn_head)) {
119
    if (!list_empty(&ffn_head)) {
115
        /* Try to use a cached free node structure. */
120
        /* Try to use a cached free node structure. */
116
        fat_idx_t *idxp_tmp;
121
        fat_idx_t *idxp_tmp;
117
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
122
        nodep = list_get_instance(ffn_head.next, fat_node_t, ffn_link);
118
        if (futex_trydown(&nodep->lock) == ESYNCH_WOULD_BLOCK)
123
        if (!fibril_mutex_trylock(&nodep->lock))
119
            goto skip_cache;
124
            goto skip_cache;
120
        idxp_tmp = nodep->idx;
125
        idxp_tmp = nodep->idx;
121
        if (futex_trydown(&idxp_tmp->lock) == ESYNCH_WOULD_BLOCK) {
126
        if (!fibril_mutex_trylock(&idxp_tmp->lock)) {
122
            futex_up(&nodep->lock);
127
            fibril_mutex_unlock(&nodep->lock);
123
            goto skip_cache;
128
            goto skip_cache;
124
        }
129
        }
125
        list_remove(&nodep->ffn_link);
130
        list_remove(&nodep->ffn_link);
126
        futex_up(&ffn_futex);
131
        fibril_mutex_unlock(&ffn_mutex);
127
        if (nodep->dirty)
132
        if (nodep->dirty)
128
            fat_node_sync(nodep);
133
            fat_node_sync(nodep);
129
        idxp_tmp->nodep = NULL;
134
        idxp_tmp->nodep = NULL;
130
        futex_up(&nodep->lock);
135
        fibril_mutex_unlock(&nodep->lock);
131
        futex_up(&idxp_tmp->lock);
136
        fibril_mutex_unlock(&idxp_tmp->lock);
-
 
137
        fn = FS_NODE(nodep);
132
    } else {
138
    } else {
133
skip_cache:
139
skip_cache:
134
        /* Try to allocate a new node structure. */
140
        /* Try to allocate a new node structure. */
135
        futex_up(&ffn_futex);
141
        fibril_mutex_unlock(&ffn_mutex);
-
 
142
        fn = (fs_node_t *)malloc(sizeof(fs_node_t));
-
 
143
        if (!fn)
-
 
144
            return NULL;
136
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
145
        nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
137
        if (!nodep)
146
        if (!nodep) {
-
 
147
            free(fn);
138
            return NULL;
148
            return NULL;
-
 
149
        }
139
    }
150
    }
140
    fat_node_initialize(nodep);
151
    fat_node_initialize(nodep);
-
 
152
    fs_node_initialize(fn);
-
 
153
    fn->data = nodep;
-
 
154
    nodep->bp = fn;
141
   
155
   
142
    return nodep;
156
    return nodep;
143
}
157
}
144
 
158
 
145
/** Internal version of fat_node_get().
159
/** Internal version of fat_node_get().
146
 *
160
 *
147
 * @param idxp      Locked index structure.
161
 * @param idxp      Locked index structure.
148
 */
162
 */
149
static void *fat_node_get_core(fat_idx_t *idxp)
163
static fat_node_t *fat_node_get_core(fat_idx_t *idxp)
150
{
164
{
151
    block_t *b;
165
    block_t *b;
152
    fat_bs_t *bs;
166
    fat_bs_t *bs;
153
    fat_dentry_t *d;
167
    fat_dentry_t *d;
154
    fat_node_t *nodep = NULL;
168
    fat_node_t *nodep = NULL;
Line 159... Line 173...
159
    if (idxp->nodep) {
173
    if (idxp->nodep) {
160
        /*
174
        /*
161
         * We are lucky.
175
         * We are lucky.
162
         * The node is already instantiated in memory.
176
         * The node is already instantiated in memory.
163
         */
177
         */
164
        futex_down(&idxp->nodep->lock);
178
        fibril_mutex_lock(&idxp->nodep->lock);
165
        if (!idxp->nodep->refcnt++)
179
        if (!idxp->nodep->refcnt++)
166
            list_remove(&idxp->nodep->ffn_link);
180
            list_remove(&idxp->nodep->ffn_link);
167
        futex_up(&idxp->nodep->lock);
181
        fibril_mutex_unlock(&idxp->nodep->lock);
168
        return idxp->nodep;
182
        return idxp->nodep;
169
    }
183
    }
170
 
184
 
171
    /*
185
    /*
172
     * We must instantiate the node from the file system.
186
     * We must instantiate the node from the file system.
Line 221... Line 235...
221
}
235
}
222
 
236
 
223
/*
237
/*
224
 * Forward declarations of FAT libfs operations.
238
 * Forward declarations of FAT libfs operations.
225
 */
239
 */
226
static void *fat_node_get(dev_handle_t, fs_index_t);
240
static fs_node_t *fat_node_get(dev_handle_t, fs_index_t);
227
static void fat_node_put(void *);
241
static void fat_node_put(fs_node_t *);
228
static void *fat_create_node(dev_handle_t, int);
242
static fs_node_t *fat_create_node(dev_handle_t, int);
229
static int fat_destroy_node(void *);
243
static int fat_destroy_node(fs_node_t *);
230
static int fat_link(void *, void *, const char *);
244
static int fat_link(fs_node_t *, fs_node_t *, const char *);
231
static int fat_unlink(void *, void *);
245
static int fat_unlink(fs_node_t *, fs_node_t *, const char *);
232
static void *fat_match(void *, const char *);
246
static fs_node_t *fat_match(fs_node_t *, const char *);
233
static fs_index_t fat_index_get(void *);
247
static fs_index_t fat_index_get(fs_node_t *);
234
static size_t fat_size_get(void *);
248
static size_t fat_size_get(fs_node_t *);
235
static unsigned fat_lnkcnt_get(void *);
249
static unsigned fat_lnkcnt_get(fs_node_t *);
236
static bool fat_has_children(void *);
250
static bool fat_has_children(fs_node_t *);
237
static void *fat_root_get(dev_handle_t);
251
static fs_node_t *fat_root_get(dev_handle_t);
238
static char fat_plb_get_char(unsigned);
252
static char fat_plb_get_char(unsigned);
239
static bool fat_is_directory(void *);
253
static bool fat_is_directory(fs_node_t *);
240
static bool fat_is_file(void *node);
254
static bool fat_is_file(fs_node_t *node);
241
 
255
 
242
/*
256
/*
243
 * FAT libfs operations.
257
 * FAT libfs operations.
244
 */
258
 */
245
 
259
 
246
/** Instantiate a FAT in-core node. */
260
/** Instantiate a FAT in-core node. */
247
void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
261
fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
248
{
262
{
249
    void *node;
263
    fat_node_t *nodep;
250
    fat_idx_t *idxp;
264
    fat_idx_t *idxp;
251
 
265
 
252
    idxp = fat_idx_get_by_index(dev_handle, index);
266
    idxp = fat_idx_get_by_index(dev_handle, index);
253
    if (!idxp)
267
    if (!idxp)
254
        return NULL;
268
        return NULL;
255
    /* idxp->lock held */
269
    /* idxp->lock held */
256
    node = fat_node_get_core(idxp);
270
    nodep = fat_node_get_core(idxp);
257
    futex_up(&idxp->lock);
271
    fibril_mutex_unlock(&idxp->lock);
258
    return node;
272
    return FS_NODE(nodep);
259
}
273
}
260
 
274
 
261
void fat_node_put(void *node)
275
void fat_node_put(fs_node_t *fn)
262
{
276
{
263
    fat_node_t *nodep = (fat_node_t *)node;
277
    fat_node_t *nodep = FAT_NODE(fn);
264
    bool destroy = false;
278
    bool destroy = false;
265
 
279
 
266
    futex_down(&nodep->lock);
280
    fibril_mutex_lock(&nodep->lock);
267
    if (!--nodep->refcnt) {
281
    if (!--nodep->refcnt) {
268
        if (nodep->idx) {
282
        if (nodep->idx) {
269
            futex_down(&ffn_futex);
283
            fibril_mutex_lock(&ffn_mutex);
270
            list_append(&nodep->ffn_link, &ffn_head);
284
            list_append(&nodep->ffn_link, &ffn_head);
271
            futex_up(&ffn_futex);
285
            fibril_mutex_unlock(&ffn_mutex);
272
        } else {
286
        } else {
273
            /*
287
            /*
274
             * The node does not have any index structure associated
288
             * The node does not have any index structure associated
275
             * with itself. This can only mean that we are releasing
289
             * with itself. This can only mean that we are releasing
276
             * the node after a failed attempt to allocate the index
290
             * the node after a failed attempt to allocate the index
277
             * structure for it.
291
             * structure for it.
278
             */
292
             */
279
            destroy = true;
293
            destroy = true;
280
        }
294
        }
281
    }
295
    }
282
    futex_up(&nodep->lock);
296
    fibril_mutex_unlock(&nodep->lock);
283
    if (destroy)
297
    if (destroy) {
-
 
298
        free(nodep->bp);
284
        free(node);
299
        free(nodep);
-
 
300
    }
285
}
301
}
286
 
302
 
287
void *fat_create_node(dev_handle_t dev_handle, int flags)
303
fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags)
288
{
304
{
289
    fat_idx_t *idxp;
305
    fat_idx_t *idxp;
290
    fat_node_t *nodep;
306
    fat_node_t *nodep;
291
    fat_bs_t *bs;
307
    fat_bs_t *bs;
292
    fat_cluster_t mcl, lcl;
308
    fat_cluster_t mcl, lcl;
Line 308... Line 324...
308
        return NULL;
324
        return NULL;
309
    }
325
    }
310
    idxp = fat_idx_get_new(dev_handle);
326
    idxp = fat_idx_get_new(dev_handle);
311
    if (!idxp) {
327
    if (!idxp) {
312
        fat_free_clusters(bs, dev_handle, mcl);
328
        fat_free_clusters(bs, dev_handle, mcl);
313
        fat_node_put(nodep);
329
        fat_node_put(FS_NODE(nodep));
314
        return NULL;
330
        return NULL;
315
    }
331
    }
316
    /* idxp->lock held */
332
    /* idxp->lock held */
317
    if (flags & L_DIRECTORY) {
333
    if (flags & L_DIRECTORY) {
318
        int i;
334
        int i;
Line 342... Line 358...
342
    nodep->dirty = true;
358
    nodep->dirty = true;
343
 
359
 
344
    nodep->idx = idxp;
360
    nodep->idx = idxp;
345
    idxp->nodep = nodep;
361
    idxp->nodep = nodep;
346
 
362
 
347
    futex_up(&idxp->lock);
363
    fibril_mutex_unlock(&idxp->lock);
348
    return nodep;
364
    return FS_NODE(nodep);
349
}
365
}
350
 
366
 
351
int fat_destroy_node(void *node)
367
int fat_destroy_node(fs_node_t *fn)
352
{
368
{
353
    fat_node_t *nodep = (fat_node_t *)node;
369
    fat_node_t *nodep = FAT_NODE(fn);
354
    fat_bs_t *bs;
370
    fat_bs_t *bs;
355
 
371
 
356
    /*
372
    /*
357
     * The node is not reachable from the file system. This means that the
373
     * The node is not reachable from the file system. This means that the
358
     * link count should be zero and that the index structure cannot be
374
     * link count should be zero and that the index structure cannot be
Line 362... Line 378...
362
    assert(nodep->lnkcnt == 0);
378
    assert(nodep->lnkcnt == 0);
363
 
379
 
364
    /*
380
    /*
365
     * The node may not have any children.
381
     * The node may not have any children.
366
     */
382
     */
367
    assert(fat_has_children(node) == false);
383
    assert(fat_has_children(fn) == false);
368
 
384
 
369
    bs = block_bb_get(nodep->idx->dev_handle);
385
    bs = block_bb_get(nodep->idx->dev_handle);
370
    if (nodep->firstc != FAT_CLST_RES0) {
386
    if (nodep->firstc != FAT_CLST_RES0) {
371
        assert(nodep->size);
387
        assert(nodep->size);
372
        /* Free all clusters allocated to the node. */
388
        /* Free all clusters allocated to the node. */
373
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
389
        fat_free_clusters(bs, nodep->idx->dev_handle, nodep->firstc);
374
    }
390
    }
375
 
391
 
376
    fat_idx_destroy(nodep->idx);
392
    fat_idx_destroy(nodep->idx);
-
 
393
    free(nodep->bp);
377
    free(nodep);
394
    free(nodep);
378
    return EOK;
395
    return EOK;
379
}
396
}
380
 
397
 
381
int fat_link(void *prnt, void *chld, const char *name)
398
int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
382
{
399
{
383
    fat_node_t *parentp = (fat_node_t *)prnt;
400
    fat_node_t *parentp = FAT_NODE(pfn);
384
    fat_node_t *childp = (fat_node_t *)chld;
401
    fat_node_t *childp = FAT_NODE(cfn);
385
    fat_dentry_t *d;
402
    fat_dentry_t *d;
386
    fat_bs_t *bs;
403
    fat_bs_t *bs;
387
    block_t *b;
404
    block_t *b;
388
    int i, j;
405
    int i, j;
389
    uint16_t bps;
406
    uint16_t bps;
390
    unsigned dps;
407
    unsigned dps;
391
    unsigned blocks;
408
    unsigned blocks;
392
    fat_cluster_t mcl, lcl;
409
    fat_cluster_t mcl, lcl;
393
    int rc;
410
    int rc;
394
 
411
 
395
    futex_down(&childp->lock);
412
    fibril_mutex_lock(&childp->lock);
396
    if (childp->lnkcnt == 1) {
413
    if (childp->lnkcnt == 1) {
397
        /*
414
        /*
398
         * On FAT, we don't support multiple hard links.
415
         * On FAT, we don't support multiple hard links.
399
         */
416
         */
400
        futex_up(&childp->lock);
417
        fibril_mutex_unlock(&childp->lock);
401
        return EMLINK;
418
        return EMLINK;
402
    }
419
    }
403
    assert(childp->lnkcnt == 0);
420
    assert(childp->lnkcnt == 0);
404
    futex_up(&childp->lock);
421
    fibril_mutex_unlock(&childp->lock);
405
 
422
 
406
    if (!fat_dentry_name_verify(name)) {
423
    if (!fat_dentry_name_verify(name)) {
407
        /*
424
        /*
408
         * Attempt to create unsupported name.
425
         * Attempt to create unsupported name.
409
         */
426
         */
Line 413... Line 430...
413
    /*
430
    /*
414
     * Get us an unused parent node's dentry or grow the parent and allocate
431
     * Get us an unused parent node's dentry or grow the parent and allocate
415
     * a new one.
432
     * a new one.
416
     */
433
     */
417
   
434
   
418
    futex_down(&parentp->idx->lock);
435
    fibril_mutex_lock(&parentp->idx->lock);
419
    bs = block_bb_get(parentp->idx->dev_handle);
436
    bs = block_bb_get(parentp->idx->dev_handle);
420
    bps = uint16_t_le2host(bs->bps);
437
    bps = uint16_t_le2host(bs->bps);
421
    dps = bps / sizeof(fat_dentry_t);
438
    dps = bps / sizeof(fat_dentry_t);
422
 
439
 
423
    blocks = parentp->size / bps;
440
    blocks = parentp->size / bps;
Line 444... Line 461...
444
    /*
461
    /*
445
     * We need to grow the parent in order to create a new unused dentry.
462
     * We need to grow the parent in order to create a new unused dentry.
446
     */
463
     */
447
    if (parentp->idx->pfc == FAT_CLST_ROOT) {
464
    if (parentp->idx->pfc == FAT_CLST_ROOT) {
448
        /* Can't grow the root directory. */
465
        /* Can't grow the root directory. */
449
        futex_up(&parentp->idx->lock);
466
        fibril_mutex_unlock(&parentp->idx->lock);
450
        return ENOSPC;
467
        return ENOSPC;
451
    }
468
    }
452
    rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
469
    rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
453
    if (rc != EOK) {
470
    if (rc != EOK) {
454
        futex_up(&parentp->idx->lock);
471
        fibril_mutex_unlock(&parentp->idx->lock);
455
        return rc;
472
        return rc;
456
    }
473
    }
457
    fat_append_clusters(bs, parentp, mcl);
474
    fat_append_clusters(bs, parentp, mcl);
458
    b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD);
475
    b = fat_block_get(bs, parentp, i, BLOCK_FLAGS_NOREAD);
459
    d = (fat_dentry_t *)b->data;
476
    d = (fat_dentry_t *)b->data;
Line 472... Line 489...
472
     */
489
     */
473
    memset(d, 0, sizeof(fat_dentry_t));
490
    memset(d, 0, sizeof(fat_dentry_t));
474
    fat_dentry_name_set(d, name);
491
    fat_dentry_name_set(d, name);
475
    b->dirty = true;        /* need to sync block */
492
    b->dirty = true;        /* need to sync block */
476
    block_put(b);
493
    block_put(b);
477
    futex_up(&parentp->idx->lock);
494
    fibril_mutex_unlock(&parentp->idx->lock);
478
 
495
 
479
    futex_down(&childp->idx->lock);
496
    fibril_mutex_lock(&childp->idx->lock);
480
   
497
   
481
    /*
498
    /*
482
     * If possible, create the Sub-directory Identifier Entry and the
499
     * If possible, create the Sub-directory Identifier Entry and the
483
     * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
500
     * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
484
     * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
501
     * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
Line 510... Line 527...
510
    b->dirty = true;        /* need to sync block */
527
    b->dirty = true;        /* need to sync block */
511
    block_put(b);
528
    block_put(b);
512
 
529
 
513
    childp->idx->pfc = parentp->firstc;
530
    childp->idx->pfc = parentp->firstc;
514
    childp->idx->pdi = i * dps + j;
531
    childp->idx->pdi = i * dps + j;
515
    futex_up(&childp->idx->lock);
532
    fibril_mutex_unlock(&childp->idx->lock);
516
 
533
 
517
    futex_down(&childp->lock);
534
    fibril_mutex_lock(&childp->lock);
518
    childp->lnkcnt = 1;
535
    childp->lnkcnt = 1;
519
    childp->dirty = true;       /* need to sync node */
536
    childp->dirty = true;       /* need to sync node */
520
    futex_up(&childp->lock);
537
    fibril_mutex_unlock(&childp->lock);
521
 
538
 
522
    /*
539
    /*
523
     * Hash in the index structure into the position hash.
540
     * Hash in the index structure into the position hash.
524
     */
541
     */
525
    fat_idx_hashin(childp->idx);
542
    fat_idx_hashin(childp->idx);
526
 
543
 
527
    return EOK;
544
    return EOK;
528
}
545
}
529
 
546
 
530
int fat_unlink(void *prnt, void *chld)
547
int fat_unlink(fs_node_t *pfn, fs_node_t *cfn, const char *nm)
531
{
548
{
532
    fat_node_t *parentp = (fat_node_t *)prnt;
549
    fat_node_t *parentp = FAT_NODE(pfn);
533
    fat_node_t *childp = (fat_node_t *)chld;
550
    fat_node_t *childp = FAT_NODE(cfn);
534
    fat_bs_t *bs;
551
    fat_bs_t *bs;
535
    fat_dentry_t *d;
552
    fat_dentry_t *d;
536
    uint16_t bps;
553
    uint16_t bps;
537
    block_t *b;
554
    block_t *b;
538
 
555
 
-
 
556
    if (!parentp)
-
 
557
        return EBUSY;
-
 
558
   
-
 
559
    if (fat_has_children(cfn))
-
 
560
        return ENOTEMPTY;
-
 
561
 
539
    futex_down(&parentp->lock);
562
    fibril_mutex_lock(&parentp->lock);
540
    futex_down(&childp->lock);
563
    fibril_mutex_lock(&childp->lock);
541
    assert(childp->lnkcnt == 1);
564
    assert(childp->lnkcnt == 1);
542
    futex_down(&childp->idx->lock);
565
    fibril_mutex_lock(&childp->idx->lock);
543
    bs = block_bb_get(childp->idx->dev_handle);
566
    bs = block_bb_get(childp->idx->dev_handle);
544
    bps = uint16_t_le2host(bs->bps);
567
    bps = uint16_t_le2host(bs->bps);
545
 
568
 
546
    b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc,
569
    b = _fat_block_get(bs, childp->idx->dev_handle, childp->idx->pfc,
547
        (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
570
        (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
Line 556... Line 579...
556
    /* remove the index structure from the position hash */
579
    /* remove the index structure from the position hash */
557
    fat_idx_hashout(childp->idx);
580
    fat_idx_hashout(childp->idx);
558
    /* clear position information */
581
    /* clear position information */
559
    childp->idx->pfc = FAT_CLST_RES0;
582
    childp->idx->pfc = FAT_CLST_RES0;
560
    childp->idx->pdi = 0;
583
    childp->idx->pdi = 0;
561
    futex_up(&childp->idx->lock);
584
    fibril_mutex_unlock(&childp->idx->lock);
562
    childp->lnkcnt = 0;
585
    childp->lnkcnt = 0;
563
    childp->dirty = true;
586
    childp->dirty = true;
564
    futex_up(&childp->lock);
587
    fibril_mutex_unlock(&childp->lock);
565
    futex_up(&parentp->lock);
588
    fibril_mutex_unlock(&parentp->lock);
566
 
589
 
567
    return EOK;
590
    return EOK;
568
}
591
}
569
 
592
 
570
void *fat_match(void *prnt, const char *component)
593
fs_node_t *fat_match(fs_node_t *pfn, const char *component)
571
{
594
{
572
    fat_bs_t *bs;
595
    fat_bs_t *bs;
573
    fat_node_t *parentp = (fat_node_t *)prnt;
596
    fat_node_t *parentp = FAT_NODE(pfn);
574
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
597
    char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
575
    unsigned i, j;
598
    unsigned i, j;
576
    unsigned bps;       /* bytes per sector */
599
    unsigned bps;       /* bytes per sector */
577
    unsigned dps;       /* dentries per sector */
600
    unsigned dps;       /* dentries per sector */
578
    unsigned blocks;
601
    unsigned blocks;
579
    fat_dentry_t *d;
602
    fat_dentry_t *d;
580
    block_t *b;
603
    block_t *b;
581
 
604
 
582
    futex_down(&parentp->idx->lock);
605
    fibril_mutex_lock(&parentp->idx->lock);
583
    bs = block_bb_get(parentp->idx->dev_handle);
606
    bs = block_bb_get(parentp->idx->dev_handle);
584
    bps = uint16_t_le2host(bs->bps);
607
    bps = uint16_t_le2host(bs->bps);
585
    dps = bps / sizeof(fat_dentry_t);
608
    dps = bps / sizeof(fat_dentry_t);
586
    blocks = parentp->size / bps;
609
    blocks = parentp->size / bps;
587
    for (i = 0; i < blocks; i++) {
610
    for (i = 0; i < blocks; i++) {
Line 592... Line 615...
592
            case FAT_DENTRY_SKIP:
615
            case FAT_DENTRY_SKIP:
593
            case FAT_DENTRY_FREE:
616
            case FAT_DENTRY_FREE:
594
                continue;
617
                continue;
595
            case FAT_DENTRY_LAST:
618
            case FAT_DENTRY_LAST:
596
                block_put(b);
619
                block_put(b);
597
                futex_up(&parentp->idx->lock);
620
                fibril_mutex_unlock(&parentp->idx->lock);
598
                return NULL;
621
                return NULL;
599
            default:
622
            default:
600
            case FAT_DENTRY_VALID:
623
            case FAT_DENTRY_VALID:
601
                fat_dentry_name_get(d, name);
624
                fat_dentry_name_get(d, name);
602
                break;
625
                break;
603
            }
626
            }
604
            if (fat_dentry_namecmp(name, component) == 0) {
627
            if (fat_dentry_namecmp(name, component) == 0) {
605
                /* hit */
628
                /* hit */
606
                void *node;
629
                fat_node_t *nodep;
607
                /*
630
                /*
608
                 * Assume tree hierarchy for locking.  We
631
                 * Assume tree hierarchy for locking.  We
609
                 * already have the parent and now we are going
632
                 * already have the parent and now we are going
610
                 * to lock the child.  Never lock in the oposite
633
                 * to lock the child.  Never lock in the oposite
611
                 * order.
634
                 * order.
612
                 */
635
                 */
613
                fat_idx_t *idx = fat_idx_get_by_pos(
636
                fat_idx_t *idx = fat_idx_get_by_pos(
614
                    parentp->idx->dev_handle, parentp->firstc,
637
                    parentp->idx->dev_handle, parentp->firstc,
615
                    i * dps + j);
638
                    i * dps + j);
616
                futex_up(&parentp->idx->lock);
639
                fibril_mutex_unlock(&parentp->idx->lock);
617
                if (!idx) {
640
                if (!idx) {
618
                    /*
641
                    /*
619
                     * Can happen if memory is low or if we
642
                     * Can happen if memory is low or if we
620
                     * run out of 32-bit indices.
643
                     * run out of 32-bit indices.
621
                     */
644
                     */
622
                    block_put(b);
645
                    block_put(b);
623
                    return NULL;
646
                    return NULL;
624
                }
647
                }
625
                node = fat_node_get_core(idx);
648
                nodep = fat_node_get_core(idx);
626
                futex_up(&idx->lock);
649
                fibril_mutex_unlock(&idx->lock);
627
                block_put(b);
650
                block_put(b);
628
                return node;
651
                return FS_NODE(nodep);
629
            }
652
            }
630
        }
653
        }
631
        block_put(b);
654
        block_put(b);
632
    }
655
    }
633
 
656
 
634
    futex_up(&parentp->idx->lock);
657
    fibril_mutex_unlock(&parentp->idx->lock);
635
    return NULL;
658
    return NULL;
636
}
659
}
637
 
660
 
638
fs_index_t fat_index_get(void *node)
661
fs_index_t fat_index_get(fs_node_t *fn)
639
{
662
{
640
    fat_node_t *fnodep = (fat_node_t *)node;
-
 
641
    if (!fnodep)
-
 
642
        return 0;
-
 
643
    return fnodep->idx->index;
663
    return FAT_NODE(fn)->idx->index;
644
}
664
}
645
 
665
 
646
size_t fat_size_get(void *node)
666
size_t fat_size_get(fs_node_t *fn)
647
{
667
{
648
    return ((fat_node_t *)node)->size;
668
    return FAT_NODE(fn)->size;
649
}
669
}
650
 
670
 
651
unsigned fat_lnkcnt_get(void *node)
671
unsigned fat_lnkcnt_get(fs_node_t *fn)
652
{
672
{
653
    return ((fat_node_t *)node)->lnkcnt;
673
    return FAT_NODE(fn)->lnkcnt;
654
}
674
}
655
 
675
 
656
bool fat_has_children(void *node)
676
bool fat_has_children(fs_node_t *fn)
657
{
677
{
658
    fat_bs_t *bs;
678
    fat_bs_t *bs;
659
    fat_node_t *nodep = (fat_node_t *)node;
679
    fat_node_t *nodep = FAT_NODE(fn);
660
    unsigned bps;
680
    unsigned bps;
661
    unsigned dps;
681
    unsigned dps;
662
    unsigned blocks;
682
    unsigned blocks;
663
    block_t *b;
683
    block_t *b;
664
    unsigned i, j;
684
    unsigned i, j;
665
 
685
 
666
    if (nodep->type != FAT_DIRECTORY)
686
    if (nodep->type != FAT_DIRECTORY)
667
        return false;
687
        return false;
668
   
688
   
669
    futex_down(&nodep->idx->lock);
689
    fibril_mutex_lock(&nodep->idx->lock);
670
    bs = block_bb_get(nodep->idx->dev_handle);
690
    bs = block_bb_get(nodep->idx->dev_handle);
671
    bps = uint16_t_le2host(bs->bps);
691
    bps = uint16_t_le2host(bs->bps);
672
    dps = bps / sizeof(fat_dentry_t);
692
    dps = bps / sizeof(fat_dentry_t);
673
 
693
 
674
    blocks = nodep->size / bps;
694
    blocks = nodep->size / bps;
Line 683... Line 703...
683
            case FAT_DENTRY_SKIP:
703
            case FAT_DENTRY_SKIP:
684
            case FAT_DENTRY_FREE:
704
            case FAT_DENTRY_FREE:
685
                continue;
705
                continue;
686
            case FAT_DENTRY_LAST:
706
            case FAT_DENTRY_LAST:
687
                block_put(b);
707
                block_put(b);
688
                futex_up(&nodep->idx->lock);
708
                fibril_mutex_unlock(&nodep->idx->lock);
689
                return false;
709
                return false;
690
            default:
710
            default:
691
            case FAT_DENTRY_VALID:
711
            case FAT_DENTRY_VALID:
692
                block_put(b);
712
                block_put(b);
693
                futex_up(&nodep->idx->lock);
713
                fibril_mutex_unlock(&nodep->idx->lock);
694
                return true;
714
                return true;
695
            }
715
            }
696
            block_put(b);
716
            block_put(b);
697
            futex_up(&nodep->idx->lock);
717
            fibril_mutex_unlock(&nodep->idx->lock);
698
            return true;
718
            return true;
699
        }
719
        }
700
        block_put(b);
720
        block_put(b);
701
    }
721
    }
702
 
722
 
703
    futex_up(&nodep->idx->lock);
723
    fibril_mutex_unlock(&nodep->idx->lock);
704
    return false;
724
    return false;
705
}
725
}
706
 
726
 
707
void *fat_root_get(dev_handle_t dev_handle)
727
fs_node_t *fat_root_get(dev_handle_t dev_handle)
708
{
728
{
709
    return fat_node_get(dev_handle, 0);
729
    return fat_node_get(dev_handle, 0);
710
}
730
}
711
 
731
 
712
char fat_plb_get_char(unsigned pos)
732
char fat_plb_get_char(unsigned pos)
713
{
733
{
714
    return fat_reg.plb_ro[pos % PLB_SIZE];
734
    return fat_reg.plb_ro[pos % PLB_SIZE];
715
}
735
}
716
 
736
 
717
bool fat_is_directory(void *node)
737
bool fat_is_directory(fs_node_t *fn)
718
{
738
{
719
    return ((fat_node_t *)node)->type == FAT_DIRECTORY;
739
    return FAT_NODE(fn)->type == FAT_DIRECTORY;
720
}
740
}
721
 
741
 
722
bool fat_is_file(void *node)
742
bool fat_is_file(fs_node_t *fn)
723
{
743
{
724
    return ((fat_node_t *)node)->type == FAT_FILE;
744
    return FAT_NODE(fn)->type == FAT_FILE;
725
}
745
}
726
 
746
 
727
/** libfs operations */
747
/** libfs operations */
728
libfs_ops_t fat_libfs_ops = {
748
libfs_ops_t fat_libfs_ops = {
729
    .match = fat_match,
749
    .match = fat_match,
Line 748... Line 768...
748
 */
768
 */
749
 
769
 
750
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
770
void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
751
{
771
{
752
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
772
    dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
-
 
773
    enum cache_mode cmode;
753
    fat_bs_t *bs;
774
    fat_bs_t *bs;
754
    uint16_t bps;
775
    uint16_t bps;
755
    uint16_t rde;
776
    uint16_t rde;
756
    int rc;
777
    int rc;
757
 
778
 
Line 775... Line 796...
775
        free(opts);
796
        free(opts);
776
        return;
797
        return;
777
    }
798
    }
778
    opts[size] = '\0';
799
    opts[size] = '\0';
779
 
800
 
-
 
801
    /* Check for option enabling write through. */
-
 
802
    if (str_cmp(opts, "wtcache") == 0)
-
 
803
        cmode = CACHE_MODE_WT;
-
 
804
    else
-
 
805
        cmode = CACHE_MODE_WB;
-
 
806
 
780
    /* initialize libblock */
807
    /* initialize libblock */
781
    rc = block_init(dev_handle, BS_SIZE);
808
    rc = block_init(dev_handle, BS_SIZE);
782
    if (rc != EOK) {
809
    if (rc != EOK) {
783
        ipc_answer_0(rid, rc);
810
        ipc_answer_0(rid, rc);
784
        return;
811
        return;
Line 804... Line 831...
804
        ipc_answer_0(rid, ENOTSUP);
831
        ipc_answer_0(rid, ENOTSUP);
805
        return;
832
        return;
806
    }
833
    }
807
 
834
 
808
    /* Initialize the block cache */
835
    /* Initialize the block cache */
809
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */);
836
    rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
810
    if (rc != EOK) {
837
    if (rc != EOK) {
811
        block_fini(dev_handle);
838
        block_fini(dev_handle);
812
        ipc_answer_0(rid, rc);
839
        ipc_answer_0(rid, rc);
813
        return;
840
        return;
814
    }
841
    }
Line 819... Line 846...
819
        ipc_answer_0(rid, rc);
846
        ipc_answer_0(rid, rc);
820
        return;
847
        return;
821
    }
848
    }
822
 
849
 
823
    /* Initialize the root node. */
850
    /* Initialize the root node. */
-
 
851
    fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
-
 
852
    if (!rfn) {
-
 
853
        block_fini(dev_handle);
-
 
854
        fat_idx_fini_by_dev_handle(dev_handle);
-
 
855
        ipc_answer_0(rid, ENOMEM);
-
 
856
        return;
-
 
857
    }
-
 
858
    fs_node_initialize(rfn);
824
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
859
    fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
825
    if (!rootp) {
860
    if (!rootp) {
-
 
861
        free(rfn);
826
        block_fini(dev_handle);
862
        block_fini(dev_handle);
827
        fat_idx_fini_by_dev_handle(dev_handle);
863
        fat_idx_fini_by_dev_handle(dev_handle);
828
        ipc_answer_0(rid, ENOMEM);
864
        ipc_answer_0(rid, ENOMEM);
829
        return;
865
        return;
830
    }
866
    }
831
    fat_node_initialize(rootp);
867
    fat_node_initialize(rootp);
832
 
868
 
833
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
869
    fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
834
    if (!ridxp) {
870
    if (!ridxp) {
835
        block_fini(dev_handle);
871
        free(rfn);
836
        free(rootp);
872
        free(rootp);
-
 
873
        block_fini(dev_handle);
837
        fat_idx_fini_by_dev_handle(dev_handle);
874
        fat_idx_fini_by_dev_handle(dev_handle);
838
        ipc_answer_0(rid, ENOMEM);
875
        ipc_answer_0(rid, ENOMEM);
839
        return;
876
        return;
840
    }
877
    }
841
    assert(ridxp->index == 0);
878
    assert(ridxp->index == 0);
Line 846... Line 883...
846
    rootp->refcnt = 1;
883
    rootp->refcnt = 1;
847
    rootp->lnkcnt = 0;  /* FS root is not linked */
884
    rootp->lnkcnt = 0;  /* FS root is not linked */
848
    rootp->size = rde * sizeof(fat_dentry_t);
885
    rootp->size = rde * sizeof(fat_dentry_t);
849
    rootp->idx = ridxp;
886
    rootp->idx = ridxp;
850
    ridxp->nodep = rootp;
887
    ridxp->nodep = rootp;
-
 
888
    rootp->bp = rfn;
-
 
889
    rfn->data = rootp;
851
   
890
   
852
    futex_up(&ridxp->lock);
891
    fibril_mutex_unlock(&ridxp->lock);
853
 
892
 
854
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
893
    ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
855
}
894
}
856
 
895
 
857
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
896
void fat_mount(ipc_callid_t rid, ipc_call_t *request)
858
{
897
{
859
    ipc_answer_0(rid, ENOTSUP);
898
    libfs_mount(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
860
}
899
}
861
 
900
 
862
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
901
void fat_lookup(ipc_callid_t rid, ipc_call_t *request)
863
{
902
{
864
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
903
    libfs_lookup(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
Line 867... Line 906...
867
void fat_read(ipc_callid_t rid, ipc_call_t *request)
906
void fat_read(ipc_callid_t rid, ipc_call_t *request)
868
{
907
{
869
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
908
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
870
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
909
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
871
    off_t pos = (off_t)IPC_GET_ARG3(*request);
910
    off_t pos = (off_t)IPC_GET_ARG3(*request);
872
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
911
    fs_node_t *fn = fat_node_get(dev_handle, index);
-
 
912
    fat_node_t *nodep;
873
    fat_bs_t *bs;
913
    fat_bs_t *bs;
874
    uint16_t bps;
914
    uint16_t bps;
875
    size_t bytes;
915
    size_t bytes;
876
    block_t *b;
916
    block_t *b;
877
 
917
 
878
    if (!nodep) {
918
    if (!fn) {
879
        ipc_answer_0(rid, ENOENT);
919
        ipc_answer_0(rid, ENOENT);
880
        return;
920
        return;
881
    }
921
    }
-
 
922
    nodep = FAT_NODE(fn);
882
 
923
 
883
    ipc_callid_t callid;
924
    ipc_callid_t callid;
884
    size_t len;
925
    size_t len;
885
    if (!ipc_data_read_receive(&callid, &len)) {
926
    if (!ipc_data_read_receive(&callid, &len)) {
886
        fat_node_put(nodep);
927
        fat_node_put(fn);
887
        ipc_answer_0(callid, EINVAL);
928
        ipc_answer_0(callid, EINVAL);
888
        ipc_answer_0(rid, EINVAL);
929
        ipc_answer_0(rid, EINVAL);
889
        return;
930
        return;
890
    }
931
    }
891
 
932
 
Line 952... Line 993...
952
            }
993
            }
953
            block_put(b);
994
            block_put(b);
954
            bnum++;
995
            bnum++;
955
        }
996
        }
956
miss:
997
miss:
957
        fat_node_put(nodep);
998
        fat_node_put(fn);
958
        ipc_answer_0(callid, ENOENT);
999
        ipc_answer_0(callid, ENOENT);
959
        ipc_answer_1(rid, ENOENT, 0);
1000
        ipc_answer_1(rid, ENOENT, 0);
960
        return;
1001
        return;
961
hit:
1002
hit:
962
        (void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
1003
        (void) ipc_data_read_finalize(callid, name, str_size(name) + 1);
963
        bytes = (pos - spos) + 1;
1004
        bytes = (pos - spos) + 1;
964
    }
1005
    }
965
 
1006
 
966
    fat_node_put(nodep);
1007
    fat_node_put(fn);
967
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
1008
    ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
968
}
1009
}
969
 
1010
 
970
void fat_write(ipc_callid_t rid, ipc_call_t *request)
1011
void fat_write(ipc_callid_t rid, ipc_call_t *request)
971
{
1012
{
972
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1013
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
973
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1014
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
974
    off_t pos = (off_t)IPC_GET_ARG3(*request);
1015
    off_t pos = (off_t)IPC_GET_ARG3(*request);
975
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
1016
    fs_node_t *fn = fat_node_get(dev_handle, index);
-
 
1017
    fat_node_t *nodep;
976
    fat_bs_t *bs;
1018
    fat_bs_t *bs;
977
    size_t bytes;
1019
    size_t bytes;
978
    block_t *b;
1020
    block_t *b;
979
    uint16_t bps;
1021
    uint16_t bps;
980
    unsigned spc;
1022
    unsigned spc;
981
    unsigned bpc;       /* bytes per cluster */
1023
    unsigned bpc;       /* bytes per cluster */
982
    off_t boundary;
1024
    off_t boundary;
983
    int flags = BLOCK_FLAGS_NONE;
1025
    int flags = BLOCK_FLAGS_NONE;
984
   
1026
   
985
    if (!nodep) {
1027
    if (!fn) {
986
        ipc_answer_0(rid, ENOENT);
1028
        ipc_answer_0(rid, ENOENT);
987
        return;
1029
        return;
988
    }
1030
    }
-
 
1031
    nodep = FAT_NODE(fn);
989
   
1032
   
990
    ipc_callid_t callid;
1033
    ipc_callid_t callid;
991
    size_t len;
1034
    size_t len;
992
    if (!ipc_data_write_receive(&callid, &len)) {
1035
    if (!ipc_data_write_receive(&callid, &len)) {
993
        fat_node_put(nodep);
1036
        fat_node_put(fn);
994
        ipc_answer_0(callid, EINVAL);
1037
        ipc_answer_0(callid, EINVAL);
995
        ipc_answer_0(rid, EINVAL);
1038
        ipc_answer_0(rid, EINVAL);
996
        return;
1039
        return;
997
    }
1040
    }
998
 
1041
 
Line 1029... Line 1072...
1029
        if (pos + bytes > nodep->size) {
1072
        if (pos + bytes > nodep->size) {
1030
            nodep->size = pos + bytes;
1073
            nodep->size = pos + bytes;
1031
            nodep->dirty = true;    /* need to sync node */
1074
            nodep->dirty = true;    /* need to sync node */
1032
        }
1075
        }
1033
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1076
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1034
        fat_node_put(nodep);
1077
        fat_node_put(fn);
1035
        return;
1078
        return;
1036
    } else {
1079
    } else {
1037
        /*
1080
        /*
1038
         * This is the more difficult case. We must allocate new
1081
         * This is the more difficult case. We must allocate new
1039
         * clusters for the node and zero them out.
1082
         * clusters for the node and zero them out.
Line 1045... Line 1088...
1045
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1088
        nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
1046
        /* create an independent chain of nclsts clusters in all FATs */
1089
        /* create an independent chain of nclsts clusters in all FATs */
1047
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1090
        status = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
1048
        if (status != EOK) {
1091
        if (status != EOK) {
1049
            /* could not allocate a chain of nclsts clusters */
1092
            /* could not allocate a chain of nclsts clusters */
1050
            fat_node_put(nodep);
1093
            fat_node_put(fn);
1051
            ipc_answer_0(callid, status);
1094
            ipc_answer_0(callid, status);
1052
            ipc_answer_0(rid, status);
1095
            ipc_answer_0(rid, status);
1053
            return;
1096
            return;
1054
        }
1097
        }
1055
        /* zero fill any gaps */
1098
        /* zero fill any gaps */
Line 1066... Line 1109...
1066
         */
1109
         */
1067
        fat_append_clusters(bs, nodep, mcl);
1110
        fat_append_clusters(bs, nodep, mcl);
1068
        nodep->size = pos + bytes;
1111
        nodep->size = pos + bytes;
1069
        nodep->dirty = true;        /* need to sync node */
1112
        nodep->dirty = true;        /* need to sync node */
1070
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1113
        ipc_answer_2(rid, EOK, bytes, nodep->size);
1071
        fat_node_put(nodep);
1114
        fat_node_put(fn);
1072
        return;
1115
        return;
1073
    }
1116
    }
1074
}
1117
}
1075
 
1118
 
1076
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1119
void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
1077
{
1120
{
1078
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1121
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1079
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1122
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1080
    size_t size = (off_t)IPC_GET_ARG3(*request);
1123
    size_t size = (off_t)IPC_GET_ARG3(*request);
1081
    fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
1124
    fs_node_t *fn = fat_node_get(dev_handle, index);
-
 
1125
    fat_node_t *nodep;
1082
    fat_bs_t *bs;
1126
    fat_bs_t *bs;
1083
    uint16_t bps;
1127
    uint16_t bps;
1084
    uint8_t spc;
1128
    uint8_t spc;
1085
    unsigned bpc;   /* bytes per cluster */
1129
    unsigned bpc;   /* bytes per cluster */
1086
    int rc;
1130
    int rc;
1087
 
1131
 
1088
    if (!nodep) {
1132
    if (!fn) {
1089
        ipc_answer_0(rid, ENOENT);
1133
        ipc_answer_0(rid, ENOENT);
1090
        return;
1134
        return;
1091
    }
1135
    }
-
 
1136
    nodep = FAT_NODE(fn);
1092
 
1137
 
1093
    bs = block_bb_get(dev_handle);
1138
    bs = block_bb_get(dev_handle);
1094
    bps = uint16_t_le2host(bs->bps);
1139
    bps = uint16_t_le2host(bs->bps);
1095
    spc = bs->spc;
1140
    spc = bs->spc;
1096
    bpc = bps * spc;
1141
    bpc = bps * spc;
Line 1124... Line 1169...
1124
        }
1169
        }
1125
        nodep->size = size;
1170
        nodep->size = size;
1126
        nodep->dirty = true;        /* need to sync node */
1171
        nodep->dirty = true;        /* need to sync node */
1127
        rc = EOK;  
1172
        rc = EOK;  
1128
    }
1173
    }
1129
    fat_node_put(nodep);
1174
    fat_node_put(fn);
1130
    ipc_answer_0(rid, rc);
1175
    ipc_answer_0(rid, rc);
1131
    return;
1176
    return;
1132
}
1177
}
1133
 
1178
 
-
 
1179
void fat_close(ipc_callid_t rid, ipc_call_t *request)
-
 
1180
{
-
 
1181
    ipc_answer_0(rid, EOK);
-
 
1182
}
-
 
1183
 
1134
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1184
void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
1135
{
1185
{
1136
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1186
    dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
1137
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1187
    fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
1138
    int rc;
1188
    int rc;
1139
 
1189
 
1140
    fat_node_t *nodep = fat_node_get(dev_handle, index);
1190
    fs_node_t *fn = fat_node_get(dev_handle, index);
1141
    if (!nodep) {
1191
    if (!fn) {
1142
        ipc_answer_0(rid, ENOENT);
1192
        ipc_answer_0(rid, ENOENT);
1143
        return;
1193
        return;
1144
    }
1194
    }
1145
 
1195
 
1146
    rc = fat_destroy_node(nodep);
1196
    rc = fat_destroy_node(fn);
1147
    ipc_answer_0(rid, rc);
1197
    ipc_answer_0(rid, rc);
1148
}
1198
}
1149
 
1199
 
-
 
1200
void fat_open_node(ipc_callid_t rid, ipc_call_t *request)
-
 
1201
{
-
 
1202
    libfs_open_node(&fat_libfs_ops, fat_reg.fs_handle, rid, request);
-
 
1203
}
-
 
1204
 
-
 
1205
void fat_device(ipc_callid_t rid, ipc_call_t *request)
-
 
1206
{
-
 
1207
    ipc_answer_0(rid, ENOTSUP);
-
 
1208
}
-
 
1209
 
-
 
1210
void fat_sync(ipc_callid_t rid, ipc_call_t *request)
-
 
1211
{
-
 
1212
    /* Dummy implementation */
-
 
1213
    ipc_answer_0(rid, EOK);
-
 
1214
}
-
 
1215
 
1150
/**
1216
/**
1151
 * @}
1217
 * @}
1152
 */
1218
 */