Changeset 89c57b6 in mainline for uspace/srv/fs/fat/fat_ops.c


Ignore:
Timestamp:
2011-04-13T14:45:41Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
88634420
Parents:
cefb126 (diff), 17279ead (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    rcefb126 r89c57b6  
    4242#include <libfs.h>
    4343#include <libblock.h>
    44 #include <ipc/ipc.h>
    4544#include <ipc/services.h>
    4645#include <ipc/devmap.h>
     
    6059#define FS_NODE(node)   ((node) ? (node)->bp : NULL)
    6160
     61#define DPS(bs)         (BPS((bs)) / sizeof(fat_dentry_t))
     62#define BPC(bs)         (BPS((bs)) * SPC((bs)))
     63
    6264/** Mutex protecting the list of cached free FAT nodes. */
    6365static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
     
    6971 * Forward declarations of FAT libfs operations.
    7072 */
    71 static int fat_root_get(fs_node_t **, dev_handle_t);
     73static int fat_root_get(fs_node_t **, devmap_handle_t);
    7274static int fat_match(fs_node_t **, fs_node_t *, const char *);
    73 static int fat_node_get(fs_node_t **, dev_handle_t, fs_index_t);
     75static int fat_node_get(fs_node_t **, devmap_handle_t, fs_index_t);
    7476static int fat_node_open(fs_node_t *);
    7577static int fat_node_put(fs_node_t *);
    76 static int fat_create_node(fs_node_t **, dev_handle_t, int);
     78static int fat_create_node(fs_node_t **, devmap_handle_t, int);
    7779static int fat_destroy_node(fs_node_t *);
    7880static int fat_link(fs_node_t *, fs_node_t *, const char *);
     
    8587static bool fat_is_directory(fs_node_t *);
    8688static bool fat_is_file(fs_node_t *node);
    87 static dev_handle_t fat_device_get(fs_node_t *node);
     89static devmap_handle_t fat_device_get(fs_node_t *node);
    8890
    8991/*
     
    101103        node->refcnt = 0;
    102104        node->dirty = false;
     105        node->lastc_cached_valid = false;
     106        node->lastc_cached_value = FAT_CLST_LAST1;
     107        node->currc_cached_valid = false;
     108        node->currc_cached_bn = 0;
     109        node->currc_cached_value = FAT_CLST_LAST1;
    103110}
    104111
     
    108115        fat_bs_t *bs;
    109116        fat_dentry_t *d;
    110         uint16_t bps;
    111         unsigned dps;
    112117        int rc;
    113118       
    114119        assert(node->dirty);
    115120
    116         bs = block_bb_get(node->idx->dev_handle);
    117         bps = uint16_t_le2host(bs->bps);
    118         dps = bps / sizeof(fat_dentry_t);
     121        bs = block_bb_get(node->idx->devmap_handle);
    119122       
    120123        /* Read the block that contains the dentry of interest. */
    121         rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc,
    122             (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
     124        rc = _fat_block_get(&b, bs, node->idx->devmap_handle, node->idx->pfc,
     125            NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
     126            BLOCK_FLAGS_NONE);
    123127        if (rc != EOK)
    124128                return rc;
    125129
    126         d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
     130        d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));
    127131
    128132        d->firstc = host2uint16_t_le(node->firstc);
     
    140144}
    141145
    142 static int fat_node_fini_by_dev_handle(dev_handle_t dev_handle)
     146static int fat_node_fini_by_devmap_handle(devmap_handle_t devmap_handle)
    143147{
    144148        link_t *lnk;
     
    165169                        goto restart;
    166170                }
    167                 if (nodep->idx->dev_handle != dev_handle) {
     171                if (nodep->idx->devmap_handle != devmap_handle) {
    168172                        fibril_mutex_unlock(&nodep->idx->lock);
    169173                        fibril_mutex_unlock(&nodep->lock);
     
    266270        fat_dentry_t *d;
    267271        fat_node_t *nodep = NULL;
    268         unsigned bps;
    269         unsigned spc;
    270         unsigned dps;
    271272        int rc;
    272273
     
    297298                return rc;
    298299
    299         bs = block_bb_get(idxp->dev_handle);
    300         bps = uint16_t_le2host(bs->bps);
    301         spc = bs->spc;
    302         dps = bps / sizeof(fat_dentry_t);
     300        bs = block_bb_get(idxp->devmap_handle);
    303301
    304302        /* Read the block that contains the dentry of interest. */
    305         rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc,
    306             (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
     303        rc = _fat_block_get(&b, bs, idxp->devmap_handle, idxp->pfc, NULL,
     304            (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
    307305        if (rc != EOK) {
    308306                (void) fat_node_put(FS_NODE(nodep));
     
    310308        }
    311309
    312         d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
     310        d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
    313311        if (d->attr & FAT_ATTR_SUBDIR) {
    314312                /*
     
    324322                 */
    325323                uint16_t clusters;
    326                 rc = fat_clusters_get(&clusters, bs, idxp->dev_handle,
     324                rc = fat_clusters_get(&clusters, bs, idxp->devmap_handle,
    327325                    uint16_t_le2host(d->firstc));
    328326                if (rc != EOK) {
     327                        (void) block_put(b);
    329328                        (void) fat_node_put(FS_NODE(nodep));
    330329                        return rc;
    331330                }
    332                 nodep->size = bps * spc * clusters;
     331                nodep->size = BPS(bs) * SPC(bs) * clusters;
    333332        } else {
    334333                nodep->type = FAT_FILE;
     
    357356 */
    358357
    359 int fat_root_get(fs_node_t **rfn, dev_handle_t dev_handle)
    360 {
    361         return fat_node_get(rfn, dev_handle, 0);
     358int fat_root_get(fs_node_t **rfn, devmap_handle_t devmap_handle)
     359{
     360        return fat_node_get(rfn, devmap_handle, 0);
    362361}
    363362
     
    368367        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
    369368        unsigned i, j;
    370         unsigned bps;           /* bytes per sector */
    371         unsigned dps;           /* dentries per sector */
    372369        unsigned blocks;
    373370        fat_dentry_t *d;
     371        devmap_handle_t devmap_handle;
    374372        block_t *b;
    375373        int rc;
    376374
    377375        fibril_mutex_lock(&parentp->idx->lock);
    378         bs = block_bb_get(parentp->idx->dev_handle);
    379         bps = uint16_t_le2host(bs->bps);
    380         dps = bps / sizeof(fat_dentry_t);
    381         blocks = parentp->size / bps;
     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);
    382381        for (i = 0; i < blocks; i++) {
    383382                rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
    384                 if (rc != EOK) {
    385                         fibril_mutex_unlock(&parentp->idx->lock);
     383                if (rc != EOK)
    386384                        return rc;
    387                 }
    388                 for (j = 0; j < dps; j++) {
     385                for (j = 0; j < DPS(bs); j++) {
    389386                        d = ((fat_dentry_t *)b->data) + j;
    390387                        switch (fat_classify_dentry(d)) {
     
    395392                                /* miss */
    396393                                rc = block_put(b);
    397                                 fibril_mutex_unlock(&parentp->idx->lock);
    398394                                *rfn = NULL;
    399395                                return rc;
     
    406402                                /* hit */
    407403                                fat_node_t *nodep;
    408                                 /*
    409                                  * Assume tree hierarchy for locking.  We
    410                                  * already have the parent and now we are going
    411                                  * to lock the child.  Never lock in the oposite
    412                                  * order.
    413                                  */
    414                                 fat_idx_t *idx = fat_idx_get_by_pos(
    415                                     parentp->idx->dev_handle, parentp->firstc,
    416                                     i * dps + j);
    417                                 fibril_mutex_unlock(&parentp->idx->lock);
     404                                fat_idx_t *idx = fat_idx_get_by_pos(devmap_handle,
     405                                    parentp->firstc, i * DPS(bs) + j);
    418406                                if (!idx) {
    419407                                        /*
     
    438426                }
    439427                rc = block_put(b);
    440                 if (rc != EOK) {
    441                         fibril_mutex_unlock(&parentp->idx->lock);
     428                if (rc != EOK)
    442429                        return rc;
    443                 }
    444         }
    445 
    446         fibril_mutex_unlock(&parentp->idx->lock);
     430        }
     431
    447432        *rfn = NULL;
    448433        return EOK;
     
    450435
    451436/** Instantiate a FAT in-core node. */
    452 int fat_node_get(fs_node_t **rfn, dev_handle_t dev_handle, fs_index_t index)
     437int fat_node_get(fs_node_t **rfn, devmap_handle_t devmap_handle, fs_index_t index)
    453438{
    454439        fat_node_t *nodep;
     
    456441        int rc;
    457442
    458         idxp = fat_idx_get_by_index(dev_handle, index);
     443        idxp = fat_idx_get_by_index(devmap_handle, index);
    459444        if (!idxp) {
    460445                *rfn = NULL;
     
    507492}
    508493
    509 int fat_create_node(fs_node_t **rfn, dev_handle_t dev_handle, int flags)
     494int fat_create_node(fs_node_t **rfn, devmap_handle_t devmap_handle, int flags)
    510495{
    511496        fat_idx_t *idxp;
     
    513498        fat_bs_t *bs;
    514499        fat_cluster_t mcl, lcl;
    515         uint16_t bps;
    516500        int rc;
    517501
    518         bs = block_bb_get(dev_handle);
    519         bps = uint16_t_le2host(bs->bps);
     502        bs = block_bb_get(devmap_handle);
    520503        if (flags & L_DIRECTORY) {
    521504                /* allocate a cluster */
    522                 rc = fat_alloc_clusters(bs, dev_handle, 1, &mcl, &lcl);
     505                rc = fat_alloc_clusters(bs, devmap_handle, 1, &mcl, &lcl);
    523506                if (rc != EOK)
    524507                        return rc;
    525508                /* populate the new cluster with unused dentries */
    526                 rc = fat_zero_cluster(bs, dev_handle, mcl);
     509                rc = fat_zero_cluster(bs, devmap_handle, mcl);
    527510                if (rc != EOK) {
    528                         (void) fat_free_clusters(bs, dev_handle, mcl);
     511                        (void) fat_free_clusters(bs, devmap_handle, mcl);
    529512                        return rc;
    530513                }
     
    533516        rc = fat_node_get_new(&nodep);
    534517        if (rc != EOK) {
    535                 (void) fat_free_clusters(bs, dev_handle, mcl);
     518                (void) fat_free_clusters(bs, devmap_handle, mcl);
    536519                return rc;
    537520        }
    538         rc = fat_idx_get_new(&idxp, dev_handle);
    539         if (rc != EOK) {
    540                 (void) fat_free_clusters(bs, dev_handle, mcl); 
     521        rc = fat_idx_get_new(&idxp, devmap_handle);
     522        if (rc != EOK) {
     523                (void) fat_free_clusters(bs, devmap_handle, mcl);       
    541524                (void) fat_node_put(FS_NODE(nodep));
    542525                return rc;
     
    546529                nodep->type = FAT_DIRECTORY;
    547530                nodep->firstc = mcl;
    548                 nodep->size = bps * bs->spc;
     531                nodep->size = BPS(bs) * SPC(bs);
    549532        } else {
    550533                nodep->type = FAT_FILE;
     
    587570        assert(!has_children);
    588571
    589         bs = block_bb_get(nodep->idx->dev_handle);
     572        bs = block_bb_get(nodep->idx->devmap_handle);
    590573        if (nodep->firstc != FAT_CLST_RES0) {
    591574                assert(nodep->size);
    592575                /* Free all clusters allocated to the node. */
    593                 rc = fat_free_clusters(bs, nodep->idx->dev_handle,
     576                rc = fat_free_clusters(bs, nodep->idx->devmap_handle,
    594577                    nodep->firstc);
    595578        }
     
    609592        block_t *b;
    610593        unsigned i, j;
    611         uint16_t bps;
    612         unsigned dps;
    613594        unsigned blocks;
    614595        fat_cluster_t mcl, lcl;
     
    639620       
    640621        fibril_mutex_lock(&parentp->idx->lock);
    641         bs = block_bb_get(parentp->idx->dev_handle);
    642         bps = uint16_t_le2host(bs->bps);
    643         dps = bps / sizeof(fat_dentry_t);
    644 
    645         blocks = parentp->size / bps;
     622        bs = block_bb_get(parentp->idx->devmap_handle);
     623
     624        blocks = parentp->size / BPS(bs);
    646625
    647626        for (i = 0; i < blocks; i++) {
     
    651630                        return rc;
    652631                }
    653                 for (j = 0; j < dps; j++) {
     632                for (j = 0; j < DPS(bs); j++) {
    654633                        d = ((fat_dentry_t *)b->data) + j;
    655634                        switch (fat_classify_dentry(d)) {
     
    680659                return ENOSPC;
    681660        }
    682         rc = fat_alloc_clusters(bs, parentp->idx->dev_handle, 1, &mcl, &lcl);
     661        rc = fat_alloc_clusters(bs, parentp->idx->devmap_handle, 1, &mcl, &lcl);
    683662        if (rc != EOK) {
    684663                fibril_mutex_unlock(&parentp->idx->lock);
    685664                return rc;
    686665        }
    687         rc = fat_zero_cluster(bs, parentp->idx->dev_handle, mcl);
    688         if (rc != EOK) {
    689                 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
     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);
    690669                fibril_mutex_unlock(&parentp->idx->lock);
    691670                return rc;
    692671        }
    693         rc = fat_append_clusters(bs, parentp, mcl);
    694         if (rc != EOK) {
    695                 (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
     672        rc = fat_append_clusters(bs, parentp, mcl, lcl);
     673        if (rc != EOK) {
     674                (void) fat_free_clusters(bs, parentp->idx->devmap_handle, mcl);
    696675                fibril_mutex_unlock(&parentp->idx->lock);
    697676                return rc;
    698677        }
    699         parentp->size += bps * bs->spc;
     678        parentp->size += BPS(bs) * SPC(bs);
    700679        parentp->dirty = true;          /* need to sync node */
    701680        rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
     
    743722                    (str_cmp((char *) d->name, FAT_NAME_DOT)) == 0) {
    744723                        memset(d, 0, sizeof(fat_dentry_t));
    745                         str_cpy((char *) d->name, 8, FAT_NAME_DOT);
    746                         str_cpy((char *) d->ext, 3, FAT_EXT_PAD);
     724                        memcpy(d->name, FAT_NAME_DOT, FAT_NAME_LEN);
     725                        memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
    747726                        d->attr = FAT_ATTR_SUBDIR;
    748727                        d->firstc = host2uint16_t_le(childp->firstc);
     
    753732                    (str_cmp((char *) d->name, FAT_NAME_DOT_DOT) == 0)) {
    754733                        memset(d, 0, sizeof(fat_dentry_t));
    755                         str_cpy((char *) d->name, 8, FAT_NAME_DOT_DOT);
    756                         str_cpy((char *) d->ext, 3, FAT_EXT_PAD);
     734                        memcpy(d->name, FAT_NAME_DOT_DOT, FAT_NAME_LEN);
     735                        memcpy(d->ext, FAT_EXT_PAD, FAT_EXT_LEN);
    757736                        d->attr = FAT_ATTR_SUBDIR;
    758737                        d->firstc = (parentp->firstc == FAT_CLST_ROOT) ?
     
    771750
    772751        childp->idx->pfc = parentp->firstc;
    773         childp->idx->pdi = i * dps + j;
     752        childp->idx->pdi = i * DPS(bs) + j;
    774753        fibril_mutex_unlock(&childp->idx->lock);
    775754
     
    793772        fat_bs_t *bs;
    794773        fat_dentry_t *d;
    795         uint16_t bps;
    796774        block_t *b;
    797775        bool has_children;
     
    811789        assert(childp->lnkcnt == 1);
    812790        fibril_mutex_lock(&childp->idx->lock);
    813         bs = block_bb_get(childp->idx->dev_handle);
    814         bps = uint16_t_le2host(bs->bps);
    815 
    816         rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
    817             (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
     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),
    818795            BLOCK_FLAGS_NONE);
    819796        if (rc != EOK)
    820797                goto error;
    821798        d = (fat_dentry_t *)b->data +
    822             (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
     799            (childp->idx->pdi % (BPS(bs) / sizeof(fat_dentry_t)));
    823800        /* mark the dentry as not-currently-used */
    824801        d->name[0] = FAT_DENTRY_ERASED;
     
    835812        fibril_mutex_unlock(&childp->idx->lock);
    836813        childp->lnkcnt = 0;
     814        childp->refcnt++;       /* keep the node in memory until destroyed */
    837815        childp->dirty = true;
    838816        fibril_mutex_unlock(&childp->lock);
     
    852830        fat_bs_t *bs;
    853831        fat_node_t *nodep = FAT_NODE(fn);
    854         unsigned bps;
    855         unsigned dps;
    856832        unsigned blocks;
    857833        block_t *b;
     
    865841       
    866842        fibril_mutex_lock(&nodep->idx->lock);
    867         bs = block_bb_get(nodep->idx->dev_handle);
    868         bps = uint16_t_le2host(bs->bps);
    869         dps = bps / sizeof(fat_dentry_t);
    870 
    871         blocks = nodep->size / bps;
     843        bs = block_bb_get(nodep->idx->devmap_handle);
     844
     845        blocks = nodep->size / BPS(bs);
    872846
    873847        for (i = 0; i < blocks; i++) {
     
    879853                        return rc;
    880854                }
    881                 for (j = 0; j < dps; j++) {
     855                for (j = 0; j < DPS(bs); j++) {
    882856                        d = ((fat_dentry_t *)b->data) + j;
    883857                        switch (fat_classify_dentry(d)) {
     
    941915}
    942916
    943 dev_handle_t fat_device_get(fs_node_t *node)
     917devmap_handle_t fat_device_get(fs_node_t *node)
    944918{
    945919        return 0;
     
    973947void fat_mounted(ipc_callid_t rid, ipc_call_t *request)
    974948{
    975         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     949        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    976950        enum cache_mode cmode;
    977951        fat_bs_t *bs;
    978         uint16_t bps;
    979         uint16_t rde;
    980952       
    981953        /* Accept the mount options */
     
    984956       
    985957        if (rc != EOK) {
    986                 ipc_answer_0(rid, rc);
     958                async_answer_0(rid, rc);
    987959                return;
    988960        }
     
    997969
    998970        /* initialize libblock */
    999         rc = block_init(dev_handle, BS_SIZE);
    1000         if (rc != EOK) {
    1001                 ipc_answer_0(rid, rc);
     971        rc = block_init(devmap_handle, BS_SIZE);
     972        if (rc != EOK) {
     973                async_answer_0(rid, rc);
    1002974                return;
    1003975        }
    1004976
    1005977        /* prepare the boot block */
    1006         rc = block_bb_read(dev_handle, BS_BLOCK);
    1007         if (rc != EOK) {
    1008                 block_fini(dev_handle);
    1009                 ipc_answer_0(rid, rc);
     978        rc = block_bb_read(devmap_handle, BS_BLOCK);
     979        if (rc != EOK) {
     980                block_fini(devmap_handle);
     981                async_answer_0(rid, rc);
    1010982                return;
    1011983        }
    1012984
    1013985        /* get the buffer with the boot sector */
    1014         bs = block_bb_get(dev_handle);
    1015        
    1016         /* Read the number of root directory entries. */
    1017         bps = uint16_t_le2host(bs->bps);
    1018         rde = uint16_t_le2host(bs->root_ent_max);
    1019 
    1020         if (bps != BS_SIZE) {
    1021                 block_fini(dev_handle);
    1022                 ipc_answer_0(rid, ENOTSUP);
     986        bs = block_bb_get(devmap_handle);
     987       
     988        if (BPS(bs) != BS_SIZE) {
     989                block_fini(devmap_handle);
     990                async_answer_0(rid, ENOTSUP);
    1023991                return;
    1024992        }
    1025993
    1026994        /* Initialize the block cache */
    1027         rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
    1028         if (rc != EOK) {
    1029                 block_fini(dev_handle);
    1030                 ipc_answer_0(rid, rc);
     995        rc = block_cache_init(devmap_handle, BPS(bs), 0 /* XXX */, cmode);
     996        if (rc != EOK) {
     997                block_fini(devmap_handle);
     998                async_answer_0(rid, rc);
    1031999                return;
    10321000        }
    10331001
    10341002        /* Do some simple sanity checks on the file system. */
    1035         rc = fat_sanity_check(bs, dev_handle);
    1036         if (rc != EOK) {
    1037                 (void) block_cache_fini(dev_handle);
    1038                 block_fini(dev_handle);
    1039                 ipc_answer_0(rid, rc);
    1040                 return;
    1041         }
    1042 
    1043         rc = fat_idx_init_by_dev_handle(dev_handle);
    1044         if (rc != EOK) {
    1045                 (void) block_cache_fini(dev_handle);
    1046                 block_fini(dev_handle);
    1047                 ipc_answer_0(rid, rc);
     1003        rc = fat_sanity_check(bs, devmap_handle);
     1004        if (rc != EOK) {
     1005                (void) block_cache_fini(devmap_handle);
     1006                block_fini(devmap_handle);
     1007                async_answer_0(rid, rc);
     1008                return;
     1009        }
     1010
     1011        rc = fat_idx_init_by_devmap_handle(devmap_handle);
     1012        if (rc != EOK) {
     1013                (void) block_cache_fini(devmap_handle);
     1014                block_fini(devmap_handle);
     1015                async_answer_0(rid, rc);
    10481016                return;
    10491017        }
     
    10521020        fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
    10531021        if (!rfn) {
    1054                 (void) block_cache_fini(dev_handle);
    1055                 block_fini(dev_handle);
    1056                 fat_idx_fini_by_dev_handle(dev_handle);
    1057                 ipc_answer_0(rid, ENOMEM);
     1022                (void) block_cache_fini(devmap_handle);
     1023                block_fini(devmap_handle);
     1024                fat_idx_fini_by_devmap_handle(devmap_handle);
     1025                async_answer_0(rid, ENOMEM);
    10581026                return;
    10591027        }
     
    10621030        if (!rootp) {
    10631031                free(rfn);
    1064                 (void) block_cache_fini(dev_handle);
    1065                 block_fini(dev_handle);
    1066                 fat_idx_fini_by_dev_handle(dev_handle);
    1067                 ipc_answer_0(rid, ENOMEM);
     1032                (void) block_cache_fini(devmap_handle);
     1033                block_fini(devmap_handle);
     1034                fat_idx_fini_by_devmap_handle(devmap_handle);
     1035                async_answer_0(rid, ENOMEM);
    10681036                return;
    10691037        }
    10701038        fat_node_initialize(rootp);
    10711039
    1072         fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
     1040        fat_idx_t *ridxp = fat_idx_get_by_pos(devmap_handle, FAT_CLST_ROOTPAR, 0);
    10731041        if (!ridxp) {
    10741042                free(rfn);
    10751043                free(rootp);
    1076                 (void) block_cache_fini(dev_handle);
    1077                 block_fini(dev_handle);
    1078                 fat_idx_fini_by_dev_handle(dev_handle);
    1079                 ipc_answer_0(rid, ENOMEM);
     1044                (void) block_cache_fini(devmap_handle);
     1045                block_fini(devmap_handle);
     1046                fat_idx_fini_by_devmap_handle(devmap_handle);
     1047                async_answer_0(rid, ENOMEM);
    10801048                return;
    10811049        }
     
    10871055        rootp->refcnt = 1;
    10881056        rootp->lnkcnt = 0;      /* FS root is not linked */
    1089         rootp->size = rde * sizeof(fat_dentry_t);
     1057        rootp->size = RDE(bs) * sizeof(fat_dentry_t);
    10901058        rootp->idx = ridxp;
    10911059        ridxp->nodep = rootp;
     
    10951063        fibril_mutex_unlock(&ridxp->lock);
    10961064
    1097         ipc_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
     1065        async_answer_3(rid, EOK, ridxp->index, rootp->size, rootp->lnkcnt);
    10981066}
    10991067
     
    11051073void fat_unmounted(ipc_callid_t rid, ipc_call_t *request)
    11061074{
    1107         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     1075        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    11081076        fs_node_t *fn;
    11091077        fat_node_t *nodep;
    11101078        int rc;
    11111079
    1112         rc = fat_root_get(&fn, dev_handle);
    1113         if (rc != EOK) {
    1114                 ipc_answer_0(rid, rc);
     1080        rc = fat_root_get(&fn, devmap_handle);
     1081        if (rc != EOK) {
     1082                async_answer_0(rid, rc);
    11151083                return;
    11161084        }
     
    11231091        if (nodep->refcnt != 2) {
    11241092                (void) fat_node_put(fn);
    1125                 ipc_answer_0(rid, EBUSY);
     1093                async_answer_0(rid, EBUSY);
    11261094                return;
    11271095        }
     
    11381106         * stop using libblock for this instance.
    11391107         */
    1140         (void) fat_node_fini_by_dev_handle(dev_handle);
    1141         fat_idx_fini_by_dev_handle(dev_handle);
    1142         (void) block_cache_fini(dev_handle);
    1143         block_fini(dev_handle);
    1144 
    1145         ipc_answer_0(rid, EOK);
     1108        (void) fat_node_fini_by_devmap_handle(devmap_handle);
     1109        fat_idx_fini_by_devmap_handle(devmap_handle);
     1110        (void) block_cache_fini(devmap_handle);
     1111        block_fini(devmap_handle);
     1112
     1113        async_answer_0(rid, EOK);
    11461114}
    11471115
     
    11581126void fat_read(ipc_callid_t rid, ipc_call_t *request)
    11591127{
    1160         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     1128        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    11611129        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    11621130        aoff64_t pos =
     
    11651133        fat_node_t *nodep;
    11661134        fat_bs_t *bs;
    1167         uint16_t bps;
    11681135        size_t bytes;
    11691136        block_t *b;
    11701137        int rc;
    11711138
    1172         rc = fat_node_get(&fn, dev_handle, index);
    1173         if (rc != EOK) {
    1174                 ipc_answer_0(rid, rc);
     1139        rc = fat_node_get(&fn, devmap_handle, index);
     1140        if (rc != EOK) {
     1141                async_answer_0(rid, rc);
    11751142                return;
    11761143        }
    11771144        if (!fn) {
    1178                 ipc_answer_0(rid, ENOENT);
     1145                async_answer_0(rid, ENOENT);
    11791146                return;
    11801147        }
     
    11851152        if (!async_data_read_receive(&callid, &len)) {
    11861153                fat_node_put(fn);
    1187                 ipc_answer_0(callid, EINVAL);
    1188                 ipc_answer_0(rid, EINVAL);
    1189                 return;
    1190         }
    1191 
    1192         bs = block_bb_get(dev_handle);
    1193         bps = uint16_t_le2host(bs->bps);
     1154                async_answer_0(callid, EINVAL);
     1155                async_answer_0(rid, EINVAL);
     1156                return;
     1157        }
     1158
     1159        bs = block_bb_get(devmap_handle);
    11941160
    11951161        if (nodep->type == FAT_FILE) {
     
    12041170                        (void) async_data_read_finalize(callid, NULL, 0);
    12051171                } else {
    1206                         bytes = min(len, bps - pos % bps);
     1172                        bytes = min(len, BPS(bs) - pos % BPS(bs));
    12071173                        bytes = min(bytes, nodep->size - pos);
    1208                         rc = fat_block_get(&b, bs, nodep, pos / bps,
     1174                        rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
    12091175                            BLOCK_FLAGS_NONE);
    12101176                        if (rc != EOK) {
    12111177                                fat_node_put(fn);
    1212                                 ipc_answer_0(callid, rc);
    1213                                 ipc_answer_0(rid, rc);
     1178                                async_answer_0(callid, rc);
     1179                                async_answer_0(rid, rc);
    12141180                                return;
    12151181                        }
    1216                         (void) async_data_read_finalize(callid, b->data + pos % bps,
    1217                             bytes);
     1182                        (void) async_data_read_finalize(callid,
     1183                            b->data + pos % BPS(bs), bytes);
    12181184                        rc = block_put(b);
    12191185                        if (rc != EOK) {
    12201186                                fat_node_put(fn);
    1221                                 ipc_answer_0(rid, rc);
     1187                                async_answer_0(rid, rc);
    12221188                                return;
    12231189                        }
     
    12301196
    12311197                assert(nodep->type == FAT_DIRECTORY);
    1232                 assert(nodep->size % bps == 0);
    1233                 assert(bps % sizeof(fat_dentry_t) == 0);
     1198                assert(nodep->size % BPS(bs) == 0);
     1199                assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
    12341200
    12351201                /*
     
    12391205                 * the position pointer accordingly.
    12401206                 */
    1241                 bnum = (pos * sizeof(fat_dentry_t)) / bps;
    1242                 while (bnum < nodep->size / bps) {
     1207                bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs);
     1208                while (bnum < nodep->size / BPS(bs)) {
    12431209                        aoff64_t o;
    12441210
     
    12471213                        if (rc != EOK)
    12481214                                goto err;
    1249                         for (o = pos % (bps / sizeof(fat_dentry_t));
    1250                             o < bps / sizeof(fat_dentry_t);
     1215                        for (o = pos % (BPS(bs) / sizeof(fat_dentry_t));
     1216                            o < BPS(bs) / sizeof(fat_dentry_t);
    12511217                            o++, pos++) {
    12521218                                d = ((fat_dentry_t *)b->data) + o;
     
    12761242miss:
    12771243                rc = fat_node_put(fn);
    1278                 ipc_answer_0(callid, rc != EOK ? rc : ENOENT);
    1279                 ipc_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
     1244                async_answer_0(callid, rc != EOK ? rc : ENOENT);
     1245                async_answer_1(rid, rc != EOK ? rc : ENOENT, 0);
    12801246                return;
    12811247
    12821248err:
    12831249                (void) fat_node_put(fn);
    1284                 ipc_answer_0(callid, rc);
    1285                 ipc_answer_0(rid, rc);
     1250                async_answer_0(callid, rc);
     1251                async_answer_0(rid, rc);
    12861252                return;
    12871253
     
    12921258
    12931259        rc = fat_node_put(fn);
    1294         ipc_answer_1(rid, rc, (ipcarg_t)bytes);
     1260        async_answer_1(rid, rc, (sysarg_t)bytes);
    12951261}
    12961262
    12971263void fat_write(ipc_callid_t rid, ipc_call_t *request)
    12981264{
    1299         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     1265        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    13001266        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    13011267        aoff64_t pos =
     
    13061272        size_t bytes, size;
    13071273        block_t *b;
    1308         uint16_t bps;
    1309         unsigned spc;
    1310         unsigned bpc;           /* bytes per cluster */
    13111274        aoff64_t boundary;
    13121275        int flags = BLOCK_FLAGS_NONE;
    13131276        int rc;
    13141277       
    1315         rc = fat_node_get(&fn, dev_handle, index);
    1316         if (rc != EOK) {
    1317                 ipc_answer_0(rid, rc);
     1278        rc = fat_node_get(&fn, devmap_handle, index);
     1279        if (rc != EOK) {
     1280                async_answer_0(rid, rc);
    13181281                return;
    13191282        }
    13201283        if (!fn) {
    1321                 ipc_answer_0(rid, ENOENT);
     1284                async_answer_0(rid, ENOENT);
    13221285                return;
    13231286        }
     
    13281291        if (!async_data_write_receive(&callid, &len)) {
    13291292                (void) fat_node_put(fn);
    1330                 ipc_answer_0(callid, EINVAL);
    1331                 ipc_answer_0(rid, EINVAL);
    1332                 return;
    1333         }
    1334 
    1335         bs = block_bb_get(dev_handle);
    1336         bps = uint16_t_le2host(bs->bps);
    1337         spc = bs->spc;
    1338         bpc = bps * spc;
     1293                async_answer_0(callid, EINVAL);
     1294                async_answer_0(rid, EINVAL);
     1295                return;
     1296        }
     1297
     1298        bs = block_bb_get(devmap_handle);
    13391299
    13401300        /*
     
    13451305         * value signalizing a smaller number of bytes written.
    13461306         */
    1347         bytes = min(len, bps - pos % bps);
    1348         if (bytes == bps)
     1307        bytes = min(len, BPS(bs) - pos % BPS(bs));
     1308        if (bytes == BPS(bs))
    13491309                flags |= BLOCK_FLAGS_NOREAD;
    13501310       
    1351         boundary = ROUND_UP(nodep->size, bpc);
     1311        boundary = ROUND_UP(nodep->size, BPC(bs));
    13521312        if (pos < boundary) {
    13531313                /*
     
    13601320                if (rc != EOK) {
    13611321                        (void) fat_node_put(fn);
    1362                         ipc_answer_0(callid, rc);
    1363                         ipc_answer_0(rid, rc);
     1322                        async_answer_0(callid, rc);
     1323                        async_answer_0(rid, rc);
    13641324                        return;
    13651325                }
    1366                 rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
     1326                rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
    13671327                if (rc != EOK) {
    13681328                        (void) fat_node_put(fn);
    1369                         ipc_answer_0(callid, rc);
    1370                         ipc_answer_0(rid, rc);
     1329                        async_answer_0(callid, rc);
     1330                        async_answer_0(rid, rc);
    13711331                        return;
    13721332                }
    1373                 (void) async_data_write_finalize(callid, b->data + pos % bps,
    1374                     bytes);
     1333                (void) async_data_write_finalize(callid,
     1334                    b->data + pos % BPS(bs), bytes);
    13751335                b->dirty = true;                /* need to sync block */
    13761336                rc = block_put(b);
    13771337                if (rc != EOK) {
    13781338                        (void) fat_node_put(fn);
    1379                         ipc_answer_0(rid, rc);
     1339                        async_answer_0(rid, rc);
    13801340                        return;
    13811341                }
     
    13861346                size = nodep->size;
    13871347                rc = fat_node_put(fn);
    1388                 ipc_answer_2(rid, rc, bytes, nodep->size);
     1348                async_answer_2(rid, rc, bytes, nodep->size);
    13891349                return;
    13901350        } else {
     
    13961356                fat_cluster_t mcl, lcl;
    13971357 
    1398                 nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
     1358                nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
    13991359                /* create an independent chain of nclsts clusters in all FATs */
    1400                 rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
     1360                rc = fat_alloc_clusters(bs, devmap_handle, nclsts, &mcl, &lcl);
    14011361                if (rc != EOK) {
    14021362                        /* could not allocate a chain of nclsts clusters */
    14031363                        (void) fat_node_put(fn);
    1404                         ipc_answer_0(callid, rc);
    1405                         ipc_answer_0(rid, rc);
     1364                        async_answer_0(callid, rc);
     1365                        async_answer_0(rid, rc);
    14061366                        return;
    14071367                }
     
    14091369                rc = fat_fill_gap(bs, nodep, mcl, pos);
    14101370                if (rc != EOK) {
    1411                         (void) fat_free_clusters(bs, dev_handle, mcl);
     1371                        (void) fat_free_clusters(bs, devmap_handle, mcl);
    14121372                        (void) fat_node_put(fn);
    1413                         ipc_answer_0(callid, rc);
    1414                         ipc_answer_0(rid, rc);
     1373                        async_answer_0(callid, rc);
     1374                        async_answer_0(rid, rc);
    14151375                        return;
    14161376                }
    1417                 rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
    1418                     flags);
     1377                rc = _fat_block_get(&b, bs, devmap_handle, lcl, NULL,
     1378                    (pos / BPS(bs)) % SPC(bs), flags);
    14191379                if (rc != EOK) {
    1420                         (void) fat_free_clusters(bs, dev_handle, mcl);
     1380                        (void) fat_free_clusters(bs, devmap_handle, mcl);
    14211381                        (void) fat_node_put(fn);
    1422                         ipc_answer_0(callid, rc);
    1423                         ipc_answer_0(rid, rc);
     1382                        async_answer_0(callid, rc);
     1383                        async_answer_0(rid, rc);
    14241384                        return;
    14251385                }
    1426                 (void) async_data_write_finalize(callid, b->data + pos % bps,
    1427                     bytes);
     1386                (void) async_data_write_finalize(callid,
     1387                    b->data + pos % BPS(bs), bytes);
    14281388                b->dirty = true;                /* need to sync block */
    14291389                rc = block_put(b);
    14301390                if (rc != EOK) {
    1431                         (void) fat_free_clusters(bs, dev_handle, mcl);
     1391                        (void) fat_free_clusters(bs, devmap_handle, mcl);
    14321392                        (void) fat_node_put(fn);
    1433                         ipc_answer_0(rid, rc);
     1393                        async_answer_0(rid, rc);
    14341394                        return;
    14351395                }
     
    14381398                 * node's cluster chain.
    14391399                 */
    1440                 rc = fat_append_clusters(bs, nodep, mcl);
     1400                rc = fat_append_clusters(bs, nodep, mcl, lcl);
    14411401                if (rc != EOK) {
    1442                         (void) fat_free_clusters(bs, dev_handle, mcl);
     1402                        (void) fat_free_clusters(bs, devmap_handle, mcl);
    14431403                        (void) fat_node_put(fn);
    1444                         ipc_answer_0(rid, rc);
     1404                        async_answer_0(rid, rc);
    14451405                        return;
    14461406                }
     
    14481408                nodep->dirty = true;            /* need to sync node */
    14491409                rc = fat_node_put(fn);
    1450                 ipc_answer_2(rid, rc, bytes, size);
     1410                async_answer_2(rid, rc, bytes, size);
    14511411                return;
    14521412        }
     
    14551415void fat_truncate(ipc_callid_t rid, ipc_call_t *request)
    14561416{
    1457         dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     1417        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
    14581418        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
    14591419        aoff64_t size =
     
    14621422        fat_node_t *nodep;
    14631423        fat_bs_t *bs;
    1464         uint16_t bps;
    1465         uint8_t spc;
    1466         unsigned bpc;   /* bytes per cluster */
    14671424        int rc;
    14681425
    1469         rc = fat_node_get(&fn, dev_handle, index);
    1470         if (rc != EOK) {
    1471                 ipc_answer_0(rid, rc);
     1426        rc = fat_node_get(&fn, devmap_handle, index);
     1427        if (rc != EOK) {
     1428                async_answer_0(rid, rc);
    14721429                return;
    14731430        }
    14741431        if (!fn) {
    1475                 ipc_answer_0(rid, ENOENT);
     1432                async_answer_0(rid, ENOENT);
    14761433                return;
    14771434        }
    14781435        nodep = FAT_NODE(fn);
    14791436
    1480         bs = block_bb_get(dev_handle);
    1481         bps = uint16_t_le2host(bs->bps);
    1482         spc = bs->spc;
    1483         bpc = bps * spc;
     1437        bs = block_bb_get(devmap_handle);
    14841438
    14851439        if (nodep->size == size) {
     
    14911445                 */
    14921446                rc = EINVAL;
    1493         } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
     1447        } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
    14941448                /*
    14951449                 * The node will be shrunk, but no clusters will be deallocated.
     
    15081462                } else {
    15091463                        fat_cluster_t lastc;
    1510                         rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
    1511                             &lastc, NULL, (size - 1) / bpc);
     1464                        rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
     1465                            &lastc, NULL, (size - 1) / BPC(bs));
    15121466                        if (rc != EOK)
    15131467                                goto out;
     
    15221476out:
    15231477        fat_node_put(fn);
    1524         ipc_answer_0(rid, rc);
     1478        async_answer_0(rid, rc);
    15251479        return;
    15261480}
     
    15281482void fat_close(ipc_callid_t rid, ipc_call_t *request)
    15291483{
    1530         ipc_answer_0(rid, EOK);
     1484        async_answer_0(rid, EOK);
    15311485}
    15321486
    15331487void fat_destroy(ipc_callid_t rid, ipc_call_t *request)
    15341488{
    1535         dev_handle_t dev_handle = (dev_handle_t)IPC_GET_ARG1(*request);
     1489        devmap_handle_t devmap_handle = (devmap_handle_t)IPC_GET_ARG1(*request);
    15361490        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    15371491        fs_node_t *fn;
     1492        fat_node_t *nodep;
    15381493        int rc;
    15391494
    1540         rc = fat_node_get(&fn, dev_handle, index);
    1541         if (rc != EOK) {
    1542                 ipc_answer_0(rid, rc);
     1495        rc = fat_node_get(&fn, devmap_handle, index);
     1496        if (rc != EOK) {
     1497                async_answer_0(rid, rc);
    15431498                return;
    15441499        }
    15451500        if (!fn) {
    1546                 ipc_answer_0(rid, ENOENT);
    1547                 return;
    1548         }
     1501                async_answer_0(rid, ENOENT);
     1502                return;
     1503        }
     1504
     1505        nodep = FAT_NODE(fn);
     1506        /*
     1507         * We should have exactly two references. One for the above
     1508         * call to fat_node_get() and one from fat_unlink().
     1509         */
     1510        assert(nodep->refcnt == 2);
    15491511
    15501512        rc = fat_destroy_node(fn);
    1551         ipc_answer_0(rid, rc);
     1513        async_answer_0(rid, rc);
    15521514}
    15531515
     
    15641526void fat_sync(ipc_callid_t rid, ipc_call_t *request)
    15651527{
    1566         /* Dummy implementation */
    1567         ipc_answer_0(rid, EOK);
     1528        devmap_handle_t devmap_handle = (devmap_handle_t) IPC_GET_ARG1(*request);
     1529        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
     1530       
     1531        fs_node_t *fn;
     1532        int rc = fat_node_get(&fn, devmap_handle, index);
     1533        if (rc != EOK) {
     1534                async_answer_0(rid, rc);
     1535                return;
     1536        }
     1537        if (!fn) {
     1538                async_answer_0(rid, ENOENT);
     1539                return;
     1540        }
     1541       
     1542        fat_node_t *nodep = FAT_NODE(fn);
     1543       
     1544        nodep->dirty = true;
     1545        rc = fat_node_sync(nodep);
     1546       
     1547        fat_node_put(fn);
     1548        async_answer_0(rid, rc);
    15681549}
    15691550
Note: See TracChangeset for help on using the changeset viewer.