Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/fs/fat/fat_ops.c

    r991f645 r4cac2d69  
    4545#include <ipc/services.h>
    4646#include <ipc/devmap.h>
    47 #include <macros.h>
    4847#include <async.h>
    4948#include <errno.h>
    50 #include <str.h>
     49#include <string.h>
    5150#include <byteorder.h>
    5251#include <adt/hash_table.h>
     
    6059#define FS_NODE(node)   ((node) ? (node)->bp : NULL)
    6160
    62 #define DPS(bs)         (BPS((bs)) / sizeof(fat_dentry_t))
    63 #define BPC(bs)         (BPS((bs)) * SPC((bs)))
    64 
    6561/** Mutex protecting the list of cached free FAT nodes. */
    6662static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
     
    7268 * Forward declarations of FAT libfs operations.
    7369 */
    74 static int fat_root_get(fs_node_t **, devmap_handle_t);
     70static int fat_root_get(fs_node_t **, dev_handle_t);
    7571static int fat_match(fs_node_t **, fs_node_t *, const char *);
    76 static int fat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
     72static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t);
    7773static int fat_node_open(fs_node_t *);
    7874static int fat_node_put(fs_node_t *);
    79 static int fat_create_node(fs_node_t **, devmap_handle_t, int);
     75static int fat_create_node(fs_node_t **, dev_handle_t, int);
    8076static int fat_destroy_node(fs_node_t *);
    8177static int fat_link(fs_node_t *, fs_node_t *, const char *);
     
    8379static int fat_has_children(bool *, fs_node_t *);
    8480static fs_index_t fat_index_get(fs_node_t *);
    85 static aoff64_t fat_size_get(fs_node_t *);
     81static size_t fat_size_get(fs_node_t *);
    8682static unsigned fat_lnkcnt_get(fs_node_t *);
    8783static char fat_plb_get_char(unsigned);
    8884static bool fat_is_directory(fs_node_t *);
    8985static bool fat_is_file(fs_node_t *node);
    90 static devmap_handle_t fat_device_get(fs_node_t *node);
     86static dev_handle_t fat_device_get(fs_node_t *node);
    9187
    9288/*
     
    104100        node->refcnt = 0;
    105101        node->dirty = false;
    106         node->lastc_cached_valid = false;
    107         node->lastc_cached_value = FAT_CLST_LAST1;
    108         node->currc_cached_valid = false;
    109         node->currc_cached_bn = 0;
    110         node->currc_cached_value = FAT_CLST_LAST1;
    111102}
    112103
     
    116107        fat_bs_t *bs;
    117108        fat_dentry_t *d;
     109        uint16_t bps;
     110        unsigned dps;
    118111        int rc;
    119112       
    120113        assert(node->dirty);
    121114
    122         bs = block_bb_get(node->idx->devmap_handle);
     115        bs = block_bb_get(node->idx->dev_handle);
     116        bps = uint16_t_le2host(bs->bps);
     117        dps = bps / sizeof(fat_dentry_t);
    123118       
    124119        /* Read the block that contains the dentry of interest. */
    125         rc = _fat_block_get(&b, bs, node->idx->devmap_handle, node->idx->pfc,
    126             NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
    127             BLOCK_FLAGS_NONE);
     120        rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc,
     121            (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
    128122        if (rc != EOK)
    129123                return rc;
    130124
    131         d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));
     125        d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
    132126
    133127        d->firstc = host2uint16_t_le(node->firstc);
     
    145139}
    146140
    147 static int fat_node_fini_by_devmap_handle(devmap_handle_t devmap_handle)
     141static int fat_node_fini_by_dev_handle(dev_handle_t dev_handle)
    148142{
    149143        link_t *lnk;
     
    170164                        goto restart;
    171165                }
    172                 if (nodep->idx->devmap_handle != devmap_handle) {
     166                if (nodep->idx->dev_handle != dev_handle) {
    173167                        fibril_mutex_unlock(&nodep->idx->lock);
    174168                        fibril_mutex_unlock(&nodep->lock);
     
    271265        fat_dentry_t *d;
    272266        fat_node_t *nodep = NULL;
     267        unsigned bps;
     268        unsigned spc;
     269        unsigned dps;
    273270        int rc;
    274271
     
    299296                return rc;
    300297
    301         bs = block_bb_get(idxp->devmap_handle);
     298        bs = block_bb_get(idxp->dev_handle);
     299        bps = uint16_t_le2host(bs->bps);
     300        spc = bs->spc;
     301        dps = bps / sizeof(fat_dentry_t);
    302302
    303303        /* Read the block that contains the dentry of interest. */
    304         rc = _fat_block_get(&b, bs, idxp->devmap_handle, idxp->pfc, NULL,
    305             (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
     304        rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc,
     305            (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
    306306        if (rc != EOK) {
    307307                (void) fat_node_put(FS_NODE(nodep));
     
    309309        }
    310310
    311         d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
     311        d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
    312312        if (d->attr & FAT_ATTR_SUBDIR) {
    313313                /*
     
    323323                 */
    324324                uint16_t clusters;
    325                 rc = fat_clusters_get(&clusters, bs, idxp->devmap_handle,
     325                rc = fat_clusters_get(&clusters, bs, idxp->dev_handle,
    326326                    uint16_t_le2host(d->firstc));
    327327                if (rc != EOK) {
     
    329329                        return rc;
    330330                }
    331                 nodep->size = BPS(bs) * SPC(bs) * clusters;
     331                nodep->size = bps * spc * clusters;
    332332        } else {
    333333                nodep->type = FAT_FILE;
     
    356356 */
    357357
    358 int fat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
    359 {
    360         return fat_node_get(rfn, devmap_handle, 0);
     358int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
     359{
     360        return fat_node_get(rfn, dev_handle, 0);
    361361}
    362362
     
    367367        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
    368368        unsigned i, j;
     369        unsigned bps;           /* bytes per sector */
     370        unsigned dps;           /* dentries per sector */
    369371        unsigned blocks;
    370372        fat_dentry_t *d;
    371         devmap_handle_t devmap_handle;
    372373        block_t *b;
    373374        int rc;
    374375
    375376        fibril_mutex_lock(&parentp->idx->lock);
    376         devmap_handle = parentp->idx->devmap_handle;
    377         fibril_mutex_unlock(&parentp->idx->lock);
    378 
    379         bs = block_bb_get(devmap_handle);
    380         blocks = parentp->size / BPS(bs);
     377        bs = block_bb_get(parentp->idx->dev_handle);
     378        bps = uint16_t_le2host(bs->bps);
     379        dps = bps / sizeof(fat_dentry_t);
     380        blocks = parentp->size / bps;
    381381        for (i = 0; i < blocks; i++) {
    382382                rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
    383                 if (rc != EOK)
     383                if (rc != EOK) {
     384                        fibril_mutex_unlock(&parentp->idx->lock);
    384385                        return rc;
    385                 for (j = 0; j < DPS(bs); j++) {
     386                }
     387                for (j = 0; j < dps; j++) {
    386388                        d = ((fat_dentry_t *)b->data) + j;
    387389                        switch (fat_classify_dentry(d)) {
     
    392394                                /* miss */
    393395                                rc = block_put(b);
     396                                fibril_mutex_unlock(&parentp->idx->lock);
    394397                                *rfn = NULL;
    395398                                return rc;
     
    402405                                /* hit */
    403406                                fat_node_t *nodep;
    404                                 fat_idx_t *idx = fat_idx_get_by_pos(devmap_handle,
    405                                     parentp->firstc, i * DPS(bs) + j);
     407                                /*
     408                                 * Assume tree hierarchy for locking.  We
     409                                 * already have the parent and now we are going
     410                                 * to lock the child.  Never lock in the oposite
     411                                 * order.
     412                                 */
     413                                fat_idx_t *idx = fat_idx_get_by_pos(
     414                                    parentp->idx->dev_handle, parentp->firstc,
     415                                    i * dps + j);
     416                                fibril_mutex_unlock(&parentp->idx->lock);
    406417                                if (!idx) {
    407418                                        /*
     
    426437                }
    427438                rc = block_put(b);
    428                 if (rc != EOK)
     439                if (rc != EOK) {
     440                        fibril_mutex_unlock(&parentp->idx->lock);
    429441                        return rc;
    430         }
    431 
     442                }
     443        }
     444
     445        fibril_mutex_unlock(&parentp->idx->lock);
    432446        *rfn = NULL;
    433447        return EOK;
     
    435449
    436450/** Instantiate a FAT in-core node. */
    437 int fat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
     451int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
    438452{
    439453        fat_node_t *nodep;
     
    441455        int rc;
    442456
    443         idxp = fat_idx_get_by_index(devmap_handle, index);
     457        idxp = fat_idx_get_by_index(dev_handle, index);
    444458        if (!idxp) {
    445459                *rfn = NULL;
     
    492506}
    493507
    494 int fat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
     508int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags)
    495509{
    496510        fat_idx_t *idxp;
     
    498512        fat_bs_t *bs;
    499513        fat_cluster_t mcl, lcl;
     514        uint16_t bps;
    500515        int rc;
    501516
    502         bs = block_bb_get(devmap_handle);
     517        bs = block_bb_get(dev_handle);
     518        bps = uint16_t_le2host(bs->bps);
    503519        if (flags & L_DIRECTORY) {
    504520                /* allocate a cluster */
    505                 rc = fat_alloc_clusters(bs, devmap_handle, 1, &mcl, &lcl);
     521                rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
    506522                if (rc != EOK)
    507523                        return rc;
    508524                /* populate the new cluster with unused dentries */
    509                 rc = fat_zero_cluster(bs, devmap_handle, mcl);
     525                rc = fat_zero_cluster(bs, dev_handle, mcl);
    510526                if (rc != EOK) {
    511                         (void) fat_free_clusters(bs, devmap_handle, mcl);
     527                        (void) fat_free_clusters(bs, dev_handle, mcl);
    512528                        return rc;
    513529                }
     
    516532        rc = fat_node_get_new(&nodep);
    517533        if (rc != EOK) {
    518                 (void) fat_free_clusters(bs, devmap_handle, mcl);
     534                (void) fat_free_clusters(bs, dev_handle, mcl);
    519535                return rc;
    520536        }
    521         rc = fat_idx_get_new(&idxp, devmap_handle);
    522         if (rc != EOK) {
    523                 (void) fat_free_clusters(bs, devmap_handle, mcl);       
     537        rc = fat_idx_get_new(&idxp, dev_handle);
     538        if (rc != EOK) {
     539                (void) fat_free_clusters(bs, dev_handle, mcl); 
    524540                (void) fat_node_put(FS_NODE(nodep));
    525541                return rc;
     
    529545                nodep->type = FAT_DIRECTORY;
    530546                nodep->firstc = mcl;
    531                 nodep->size = BPS(bs) * SPC(bs);
     547                nodep->size = bps * bs->spc;
    532548        } else {
    533549                nodep->type = FAT_FILE;
     
    570586        assert(!has_children);
    571587
    572         bs = block_bb_get(nodep->idx->devmap_handle);
     588        bs = block_bb_get(nodep->idx->dev_handle);
    573589        if (nodep->firstc != FAT_CLST_RES0) {
    574590                assert(nodep->size);
    575591                /* Free all clusters allocated to the node. */
    576                 rc = fat_free_clusters(bs, nodep->idx->devmap_handle,
     592                rc = fat_free_clusters(bs, nodep->idx->dev_handle,
    577593                    nodep->firstc);
    578594        }
     
    592608        block_t *b;
    593609        unsigned i, j;
     610        uint16_t bps;
     611        unsigned dps;
    594612        unsigned blocks;
    595613        fat_cluster_t mcl, lcl;
     
    620638       
    621639        fibril_mutex_lock(&parentp->idx->lock);
    622         bs = block_bb_get(parentp->idx->devmap_handle);
    623 
    624         blocks = parentp->size / BPS(bs);
     640        bs = block_bb_get(parentp->idx->dev_handle);
     641        bps = uint16_t_le2host(bs->bps);
     642        dps = bps / sizeof(fat_dentry_t);
     643
     644        blocks = parentp->size / bps;
    625645
    626646        for (i = 0; i < blocks; i++) {
     
    630650                        return rc;
    631651                }
    632                 for (j = 0; j < DPS(bs); j++) {
     652                for (j = 0; j < dps; j++) {
    633653                        d = ((fat_dentry_t *)b->data) + j;
    634654                        switch (fat_classify_dentry(d)) {
     
    659679                return ENOSPC;
    660680        }
    661         rc = fat_alloc_clusters(bs, parentp->idx->devmap_handle, 1, &mcl, &lcl);
     681        rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
    662682        if (rc != EOK) {
    663683                fibril_mutex_unlock(&parentp->idx->lock);
    664684                return rc;
    665685        }
    666         rc = fat_zero_cluster(bs, parentp->idx->devmap_handle, mcl);
    667         if (rc != EOK) {
    668                 (void) fat_free_clusters(bs, parentp->idx->devmap_handle, mcl);
     686        rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
     687        if (rc != EOK) {
     688                (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
    669689                fibril_mutex_unlock(&parentp->idx->lock);
    670690                return rc;
    671691        }
    672         rc = fat_append_clusters(bs, parentp, mcl, lcl);
    673         if (rc != EOK) {
    674                 (void) fat_free_clusters(bs, parentp->idx->devmap_handle, mcl);
     692        rc = fat_append_clusters(bs, parentp, mcl);
     693        if (rc != EOK) {
     694                (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
    675695                fibril_mutex_unlock(&parentp->idx->lock);
    676696                return rc;
    677697        }
    678         parentp->size += BPS(bs) * SPC(bs);
     698        parentp->size += bps * bs->spc;
    679699        parentp->dirty = true;          /* need to sync node */
    680700        rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
     
    702722        fibril_mutex_lock(&childp->idx->lock);
    703723       
    704         if (childp->type == FAT_DIRECTORY) {
     724        /*
     725         * If possible, create the Sub-directory Identifier Entry and the
     726         * Sub-directory Parent Pointer Entry (i.e. "." and ".."). These entries
     727         * are not mandatory according to Standard ECMA-107 and HelenOS VFS does
     728         * not use them anyway, so this is rather a sign of our good will.
     729         */
     730        rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
     731        if (rc != EOK) {
    705732                /*
    706                  * If possible, create the Sub-directory Identifier Entry and
    707                  * the Sub-directory Parent Pointer Entry (i.e. "." and "..").
    708                  * These entries are not mandatory according to Standard
    709                  * ECMA-107 and HelenOS VFS does not use them anyway, so this is
    710                  * rather a sign of our good will.
     733                 * Rather than returning an error, simply skip the creation of
     734                 * these two entries.
    711735                 */
    712                 rc = fat_block_get(&b, bs, childp, 0, BLOCK_FLAGS_NONE);
    713                 if (rc != EOK) {
    714                         /*
    715                          * Rather than returning an error, simply skip the
    716                          * creation of these two entries.
    717                          */
    718                         goto skip_dots;
    719                 }
    720                 d = (fat_dentry_t *) b->data;
    721                 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
    722                     (str_cmp((char *) d->name, FAT_NAME_DOT)) == 0) {
    723                         memset(d, 0, sizeof(fat_dentry_t));
    724                         str_cpy((char *) d->name, 8, FAT_NAME_DOT);
    725                         str_cpy((char *) d->ext, 3, FAT_EXT_PAD);
    726                         d->attr = FAT_ATTR_SUBDIR;
    727                         d->firstc = host2uint16_t_le(childp->firstc);
    728                         /* TODO: initialize also the date/time members. */
    729                 }
    730                 d++;
    731                 if ((fat_classify_dentry(d) == FAT_DENTRY_LAST) ||
    732                     (str_cmp((char *) d->name, FAT_NAME_DOT_DOT) == 0)) {
    733                         memset(d, 0, sizeof(fat_dentry_t));
    734                         str_cpy((char *) d->name, 8, FAT_NAME_DOT_DOT);
    735                         str_cpy((char *) d->ext, 3, FAT_EXT_PAD);
    736                         d->attr = FAT_ATTR_SUBDIR;
    737                         d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
    738                             host2uint16_t_le(FAT_CLST_RES0) :
    739                             host2uint16_t_le(parentp->firstc);
    740                         /* TODO: initialize also the date/time members. */
    741                 }
    742                 b->dirty = true;                /* need to sync block */
    743                 /*
    744                  * Ignore the return value as we would have fallen through on error
    745                  * anyway.
    746                  */
    747                 (void) block_put(b);
    748         }
     736                goto skip_dots;
     737        }
     738        d = (fat_dentry_t *)b->data;
     739        if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
     740            str_cmp(d->name, FAT_NAME_DOT) == 0) {
     741                memset(d, 0, sizeof(fat_dentry_t));
     742                str_cpy(d->name, 8, FAT_NAME_DOT);
     743                str_cpy(d->ext, 3, FAT_EXT_PAD);
     744                d->attr = FAT_ATTR_SUBDIR;
     745                d->firstc = host2uint16_t_le(childp->firstc);
     746                /* TODO: initialize also the date/time members. */
     747        }
     748        d++;
     749        if (fat_classify_dentry(d) == FAT_DENTRY_LAST ||
     750            str_cmp(d->name, FAT_NAME_DOT_DOT) == 0) {
     751                memset(d, 0, sizeof(fat_dentry_t));
     752                str_cpy(d->name, 8, FAT_NAME_DOT_DOT);
     753                str_cpy(d->ext, 3, FAT_EXT_PAD);
     754                d->attr = FAT_ATTR_SUBDIR;
     755                d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
     756                    host2uint16_t_le(FAT_CLST_RES0) :
     757                    host2uint16_t_le(parentp->firstc);
     758                /* TODO: initialize also the date/time members. */
     759        }
     760        b->dirty = true;                /* need to sync block */
     761        /*
     762         * Ignore the return value as we would have fallen through on error
     763         * anyway.
     764         */
     765        (void) block_put(b);
    749766skip_dots:
    750767
    751768        childp->idx->pfc = parentp->firstc;
    752         childp->idx->pdi = i * DPS(bs) + j;
     769        childp->idx->pdi = i * dps + j;
    753770        fibril_mutex_unlock(&childp->idx->lock);
    754771
     
    772789        fat_bs_t *bs;
    773790        fat_dentry_t *d;
     791        uint16_t bps;
    774792        block_t *b;
    775793        bool has_children;
     
    789807        assert(childp->lnkcnt == 1);
    790808        fibril_mutex_lock(&childp->idx->lock);
    791         bs = block_bb_get(childp->idx->devmap_handle);
    792 
    793         rc = _fat_block_get(&b, bs, childp->idx->devmap_handle, childp->idx->pfc,
    794             NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
     809        bs = block_bb_get(childp->idx->dev_handle);
     810        bps = uint16_t_le2host(bs->bps);
     811
     812        rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
     813            (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
    795814            BLOCK_FLAGS_NONE);
    796815        if (rc != EOK)
    797816                goto error;
    798817        d = (fat_dentry_t *)b->data +
    799             (childp->idx->pdi % (BPS(bs) / sizeof(fat_dentry_t)));
     818            (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
    800819        /* mark the dentry as not-currently-used */
    801820        d->name[0] = FAT_DENTRY_ERASED;
     
    829848        fat_bs_t *bs;
    830849        fat_node_t *nodep = FAT_NODE(fn);
     850        unsigned bps;
     851        unsigned dps;
    831852        unsigned blocks;
    832853        block_t *b;
     
    840861       
    841862        fibril_mutex_lock(&nodep->idx->lock);
    842         bs = block_bb_get(nodep->idx->devmap_handle);
    843 
    844         blocks = nodep->size / BPS(bs);
     863        bs = block_bb_get(nodep->idx->dev_handle);
     864        bps = uint16_t_le2host(bs->bps);
     865        dps = bps / sizeof(fat_dentry_t);
     866
     867        blocks = nodep->size / bps;
    845868
    846869        for (i = 0; i < blocks; i++) {
     
    852875                        return rc;
    853876                }
    854                 for (j = 0; j < DPS(bs); j++) {
     877                for (j = 0; j < dps; j++) {
    855878                        d = ((fat_dentry_t *)b->data) + j;
    856879                        switch (fat_classify_dentry(d)) {
     
    889912}
    890913
    891 aoff64_t fat_size_get(fs_node_t *fn)
     914size_t fat_size_get(fs_node_t *fn)
    892915{
    893916        return FAT_NODE(fn)->size;
     
    914937}
    915938
    916 devmap_handle_t fat_device_get(fs_node_t *node)
     939dev_handle_t fat_device_get(fs_node_t *node)
    917940{
    918941        return 0;
     
    946969void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
    947970{
    948         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
     971        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    949972        enum cache_mode cmode;
    950973        fat_bs_t *bs;
     974        uint16_t bps;
     975        uint16_t rde;
    951976       
    952977        /* Accept the mount options */
     
    968993
    969994        /* initialize libblock */
    970         rc = block_init(devmap_handle, BS_SIZE);
     995        rc = block_init(dev_handle, BS_SIZE);
    971996        if (rc != EOK) {
    972997                ipc_answer_0(rid, rc);
     
    9751000
    9761001        /* prepare the boot block */
    977         rc = block_bb_read(devmap_handle, BS_BLOCK);
    978         if (rc != EOK) {
    979                 block_fini(devmap_handle);
     1002        rc = block_bb_read(dev_handle, BS_BLOCK);
     1003        if (rc != EOK) {
     1004                block_fini(dev_handle);
    9801005                ipc_answer_0(rid, rc);
    9811006                return;
     
    9831008
    9841009        /* get the buffer with the boot sector */
    985         bs = block_bb_get(devmap_handle);
    986        
    987         if (BPS(bs) != BS_SIZE) {
    988                 block_fini(devmap_handle);
     1010        bs = block_bb_get(dev_handle);
     1011       
     1012        /* Read the number of root directory entries. */
     1013        bps = uint16_t_le2host(bs->bps);
     1014        rde = uint16_t_le2host(bs->root_ent_max);
     1015
     1016        if (bps != BS_SIZE) {
     1017                block_fini(dev_handle);
    9891018                ipc_answer_0(rid, ENOTSUP);
    9901019                return;
     
    9921021
    9931022        /* Initialize the block cache */
    994         rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
    995         if (rc != EOK) {
    996                 block_fini(devmap_handle);
     1023        rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
     1024        if (rc != EOK) {
     1025                block_fini(dev_handle);
    9971026                ipc_answer_0(rid, rc);
    9981027                return;
     
    10001029
    10011030        /* Do some simple sanity checks on the file system. */
    1002         rc = fat_sanity_check(bs, devmap_handle);
    1003         if (rc != EOK) {
    1004                 (void) block_cache_fini(devmap_handle);
    1005                 block_fini(devmap_handle);
     1031        rc = fat_sanity_check(bs, dev_handle);
     1032        if (rc != EOK) {
     1033                (void) block_cache_fini(dev_handle);
     1034                block_fini(dev_handle);
    10061035                ipc_answer_0(rid, rc);
    10071036                return;
    10081037        }
    10091038
    1010         rc = fat_idx_init_by_devmap_handle(devmap_handle);
    1011         if (rc != EOK) {
    1012                 (void) block_cache_fini(devmap_handle);
    1013                 block_fini(devmap_handle);
     1039        rc = fat_idx_init_by_dev_handle(dev_handle);
     1040        if (rc != EOK) {
     1041                (void) block_cache_fini(dev_handle);
     1042                block_fini(dev_handle);
    10141043                ipc_answer_0(rid, rc);
    10151044                return;
     
    10191048        fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
    10201049        if (!rfn) {
    1021                 (void) block_cache_fini(devmap_handle);
    1022                 block_fini(devmap_handle);
    1023                 fat_idx_fini_by_devmap_handle(devmap_handle);
     1050                (void) block_cache_fini(dev_handle);
     1051                block_fini(dev_handle);
     1052                fat_idx_fini_by_dev_handle(dev_handle);
    10241053                ipc_answer_0(rid, ENOMEM);
    10251054                return;
     
    10291058        if (!rootp) {
    10301059                free(rfn);
    1031                 (void) block_cache_fini(devmap_handle);
    1032                 block_fini(devmap_handle);
    1033                 fat_idx_fini_by_devmap_handle(devmap_handle);
     1060                (void) block_cache_fini(dev_handle);
     1061                block_fini(dev_handle);
     1062                fat_idx_fini_by_dev_handle(dev_handle);
    10341063                ipc_answer_0(rid, ENOMEM);
    10351064                return;
     
    10371066        fat_node_initialize(rootp);
    10381067
    1039         fat_idx_t *ridxp = fat_idx_get_by_pos(devmap_handle, FAT_CLST_ROOTPAR, 0);
     1068        fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
    10401069        if (!ridxp) {
    10411070                free(rfn);
    10421071                free(rootp);
    1043                 (void) block_cache_fini(devmap_handle);
    1044                 block_fini(devmap_handle);
    1045                 fat_idx_fini_by_devmap_handle(devmap_handle);
     1072                (void) block_cache_fini(dev_handle);
     1073                block_fini(dev_handle);
     1074                fat_idx_fini_by_dev_handle(dev_handle);
    10461075                ipc_answer_0(rid, ENOMEM);
    10471076                return;
     
    10541083        rootp->refcnt = 1;
    10551084        rootp->lnkcnt = 0;      /* FS root is not linked */
    1056         rootp->size = RDE(bs) * sizeof(fat_dentry_t);
     1085        rootp->size = rde * sizeof(fat_dentry_t);
    10571086        rootp->idx = ridxp;
    10581087        ridxp->nodep = rootp;
     
    10721101void fat_unmounted(ipc_callid_t rid, ipc_call_t *request)
    10731102{
    1074         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
     1103        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
    10751104        fs_node_t *fn;
    10761105        fat_node_t *nodep;
    10771106        int rc;
    10781107
    1079         rc = fat_root_get(&fn, devmap_handle);
     1108        rc = fat_root_get(&fn, dev_handle);
    10801109        if (rc != EOK) {
    10811110                ipc_answer_0(rid, rc);
     
    11051134         * stop using libblock for this instance.
    11061135         */
    1107         (void) fat_node_fini_by_devmap_handle(devmap_handle);
    1108         fat_idx_fini_by_devmap_handle(devmap_handle);
    1109         (void) block_cache_fini(devmap_handle);
    1110         block_fini(devmap_handle);
     1136        (void) fat_node_fini_by_dev_handle(dev_handle);
     1137        fat_idx_fini_by_dev_handle(dev_handle);
     1138        (void) block_cache_fini(dev_handle);
     1139        block_fini(dev_handle);
    11111140
    11121141        ipc_answer_0(rid, EOK);
     
    11251154void fat_read(ipc_callid_t rid, ipc_call_t *request)
    11261155{
    1127         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    1128         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    1129         aoff64_t pos =
    1130             (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
     1156        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     1157        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     1158        off_t pos = (off_t)IPC_GET_ARG3(*request);
    11311159        fs_node_t *fn;
    11321160        fat_node_t *nodep;
    11331161        fat_bs_t *bs;
     1162        uint16_t bps;
    11341163        size_t bytes;
    11351164        block_t *b;
    11361165        int rc;
    11371166
    1138         rc = fat_node_get(&fn, devmap_handle, index);
     1167        rc = fat_node_get(&fn, dev_handle, index);
    11391168        if (rc != EOK) {
    11401169                ipc_answer_0(rid, rc);
     
    11561185        }
    11571186
    1158         bs = block_bb_get(devmap_handle);
     1187        bs = block_bb_get(dev_handle);
     1188        bps = uint16_t_le2host(bs->bps);
    11591189
    11601190        if (nodep->type == FAT_FILE) {
     
    11691199                        (void) async_data_read_finalize(callid, NULL, 0);
    11701200                } else {
    1171                         bytes = min(len, BPS(bs) - pos % BPS(bs));
     1201                        bytes = min(len, bps - pos % bps);
    11721202                        bytes = min(bytes, nodep->size - pos);
    1173                         rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
     1203                        rc = fat_block_get(&b, bs, nodep, pos / bps,
    11741204                            BLOCK_FLAGS_NONE);
    11751205                        if (rc != EOK) {
     
    11791209                                return;
    11801210                        }
    1181                         (void) async_data_read_finalize(callid,
    1182                             b->data + pos % BPS(bs), bytes);
     1211                        (void) async_data_read_finalize(callid, b->data + pos % bps,
     1212                            bytes);
    11831213                        rc = block_put(b);
    11841214                        if (rc != EOK) {
     
    11901220        } else {
    11911221                unsigned bnum;
    1192                 aoff64_t spos = pos;
     1222                off_t spos = pos;
    11931223                char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
    11941224                fat_dentry_t *d;
    11951225
    11961226                assert(nodep->type == FAT_DIRECTORY);
    1197                 assert(nodep->size % BPS(bs) == 0);
    1198                 assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
     1227                assert(nodep->size % bps == 0);
     1228                assert(bps % sizeof(fat_dentry_t) == 0);
    11991229
    12001230                /*
     
    12041234                 * the position pointer accordingly.
    12051235                 */
    1206                 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs);
    1207                 while (bnum < nodep->size / BPS(bs)) {
    1208                         aoff64_t o;
     1236                bnum = (pos * sizeof(fat_dentry_t)) / bps;
     1237                while (bnum < nodep->size / bps) {
     1238                        off_t o;
    12091239
    12101240                        rc = fat_block_get(&b, bs, nodep, bnum,
     
    12121242                        if (rc != EOK)
    12131243                                goto err;
    1214                         for (o = pos % (BPS(bs) / sizeof(fat_dentry_t));
    1215                             o < BPS(bs) / sizeof(fat_dentry_t);
     1244                        for (o = pos % (bps / sizeof(fat_dentry_t));
     1245                            o < bps / sizeof(fat_dentry_t);
    12161246                            o++, pos++) {
    12171247                                d = ((fat_dentry_t *)b->data) + o;
     
    12621292void fat_write(ipc_callid_t rid, ipc_call_t *request)
    12631293{
    1264         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    1265         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    1266         aoff64_t pos =
    1267             (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
     1294        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     1295        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     1296        off_t pos = (off_t)IPC_GET_ARG3(*request);
    12681297        fs_node_t *fn;
    12691298        fat_node_t *nodep;
     
    12711300        size_t bytes, size;
    12721301        block_t *b;
    1273         aoff64_t boundary;
     1302        uint16_t bps;
     1303        unsigned spc;
     1304        unsigned bpc;           /* bytes per cluster */
     1305        off_t boundary;
    12741306        int flags = BLOCK_FLAGS_NONE;
    12751307        int rc;
    12761308       
    1277         rc = fat_node_get(&fn, devmap_handle, index);
     1309        rc = fat_node_get(&fn, dev_handle, index);
    12781310        if (rc != EOK) {
    12791311                ipc_answer_0(rid, rc);
     
    12951327        }
    12961328
    1297         bs = block_bb_get(devmap_handle);
     1329        bs = block_bb_get(dev_handle);
     1330        bps = uint16_t_le2host(bs->bps);
     1331        spc = bs->spc;
     1332        bpc = bps * spc;
    12981333
    12991334        /*
     
    13041339         * value signalizing a smaller number of bytes written.
    13051340         */
    1306         bytes = min(len, BPS(bs) - pos % BPS(bs));
    1307         if (bytes == BPS(bs))
     1341        bytes = min(len, bps - pos % bps);
     1342        if (bytes == bps)
    13081343                flags |= BLOCK_FLAGS_NOREAD;
    13091344       
    1310         boundary = ROUND_UP(nodep->size, BPC(bs));
     1345        boundary = ROUND_UP(nodep->size, bpc);
    13111346        if (pos < boundary) {
    13121347                /*
     
    13231358                        return;
    13241359                }
    1325                 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
     1360                rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
    13261361                if (rc != EOK) {
    13271362                        (void) fat_node_put(fn);
     
    13301365                        return;
    13311366                }
    1332                 (void) async_data_write_finalize(callid,
    1333                     b->data + pos % BPS(bs), bytes);
     1367                (void) async_data_write_finalize(callid, b->data + pos % bps,
     1368                    bytes);
    13341369                b->dirty = true;                /* need to sync block */
    13351370                rc = block_put(b);
     
    13551390                fat_cluster_t mcl, lcl;
    13561391 
    1357                 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
     1392                nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
    13581393                /* create an independent chain of nclsts clusters in all FATs */
    1359                 rc = fat_alloc_clusters(bs, devmap_handle, nclsts, &mcl, &lcl);
     1394                rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
    13601395                if (rc != EOK) {
    13611396                        /* could not allocate a chain of nclsts clusters */
     
    13681403                rc = fat_fill_gap(bs, nodep, mcl, pos);
    13691404                if (rc != EOK) {
    1370                         (void) fat_free_clusters(bs, devmap_handle, mcl);
     1405                        (void) fat_free_clusters(bs, dev_handle, mcl);
    13711406                        (void) fat_node_put(fn);
    13721407                        ipc_answer_0(callid, rc);
     
    13741409                        return;
    13751410                }
    1376                 rc = _fat_block_get(&b, bs, devmap_handle, lcl, NULL,
    1377                     (pos / BPS(bs)) % SPC(bs), flags);
     1411                rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
     1412                    flags);
    13781413                if (rc != EOK) {
    1379                         (void) fat_free_clusters(bs, devmap_handle, mcl);
     1414                        (void) fat_free_clusters(bs, dev_handle, mcl);
    13801415                        (void) fat_node_put(fn);
    13811416                        ipc_answer_0(callid, rc);
     
    13831418                        return;
    13841419                }
    1385                 (void) async_data_write_finalize(callid,
    1386                     b->data + pos % BPS(bs), bytes);
     1420                (void) async_data_write_finalize(callid, b->data + pos % bps,
     1421                    bytes);
    13871422                b->dirty = true;                /* need to sync block */
    13881423                rc = block_put(b);
    13891424                if (rc != EOK) {
    1390                         (void) fat_free_clusters(bs, devmap_handle, mcl);
     1425                        (void) fat_free_clusters(bs, dev_handle, mcl);
    13911426                        (void) fat_node_put(fn);
    13921427                        ipc_answer_0(rid, rc);
     
    13971432                 * node's cluster chain.
    13981433                 */
    1399                 rc = fat_append_clusters(bs, nodep, mcl, lcl);
     1434                rc = fat_append_clusters(bs, nodep, mcl);
    14001435                if (rc != EOK) {
    1401                         (void) fat_free_clusters(bs, devmap_handle, mcl);
     1436                        (void) fat_free_clusters(bs, dev_handle, mcl);
    14021437                        (void) fat_node_put(fn);
    14031438                        ipc_answer_0(rid, rc);
     
    14141449void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
    14151450{
    1416         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    1417         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    1418         aoff64_t size =
    1419             (aoff64_t) MERGE_LOUP32(IPC_GET_ARG3(*request), IPC_GET_ARG4(*request));
     1451        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     1452        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
     1453        size_t size = (off_t)IPC_GET_ARG3(*request);
    14201454        fs_node_t *fn;
    14211455        fat_node_t *nodep;
    14221456        fat_bs_t *bs;
     1457        uint16_t bps;
     1458        uint8_t spc;
     1459        unsigned bpc;   /* bytes per cluster */
    14231460        int rc;
    14241461
    1425         rc = fat_node_get(&fn, devmap_handle, index);
     1462        rc = fat_node_get(&fn, dev_handle, index);
    14261463        if (rc != EOK) {
    14271464                ipc_answer_0(rid, rc);
     
    14341471        nodep = FAT_NODE(fn);
    14351472
    1436         bs = block_bb_get(devmap_handle);
     1473        bs = block_bb_get(dev_handle);
     1474        bps = uint16_t_le2host(bs->bps);
     1475        spc = bs->spc;
     1476        bpc = bps * spc;
    14371477
    14381478        if (nodep->size == size) {
     
    14441484                 */
    14451485                rc = EINVAL;
    1446         } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
     1486        } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
    14471487                /*
    14481488                 * The node will be shrunk, but no clusters will be deallocated.
     
    14611501                } else {
    14621502                        fat_cluster_t lastc;
    1463                         rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
    1464                             &lastc, NULL, (size - 1) / BPC(bs));
     1503                        rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
     1504                            &lastc, NULL, (size - 1) / bpc);
    14651505                        if (rc != EOK)
    14661506                                goto out;
     
    14861526void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
    14871527{
    1488         devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
     1528        dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
    14891529        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    14901530        fs_node_t *fn;
    14911531        int rc;
    14921532
    1493         rc = fat_node_get(&fn, devmap_handle, index);
     1533        rc = fat_node_get(&fn, dev_handle, index);
    14941534        if (rc != EOK) {
    14951535                ipc_answer_0(rid, rc);
     
    15171557void fat_sync(ipc_callid_t rid, ipc_call_t *request)
    15181558{
    1519         devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    1520         fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    1521        
    1522         fs_node_t *fn;
    1523         int rc = fat_node_get(&fn, devmap_handle, index);
    1524         if (rc != EOK) {
    1525                 ipc_answer_0(rid, rc);
    1526                 return;
    1527         }
    1528         if (!fn) {
    1529                 ipc_answer_0(rid, ENOENT);
    1530                 return;
    1531         }
    1532        
    1533         fat_node_t *nodep = FAT_NODE(fn);
    1534        
    1535         nodep->dirty = true;
    1536         rc = fat_node_sync(nodep);
    1537        
    1538         fat_node_put(fn);
    1539         ipc_answer_0(rid, rc);
     1559        /* Dummy implementation */
     1560        ipc_answer_0(rid, EOK);
    15401561}
    15411562
Note: See TracChangeset for help on using the changeset viewer.