Ignore:
File:
1 edited

Legend:

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

    r991f645 r3f93cdbe  
    4949#include <mem.h>
    5050
    51 /*
    52  * Convenience macros for computing some frequently used values from the
    53  * primitive boot sector members.
    54  */
    55 #define RDS(bs)         ((sizeof(fat_dentry_t) * RDE((bs))) / BPS((bs))) + \
    56                         (((sizeof(fat_dentry_t) * RDE((bs))) % BPS((bs))) != 0)
    57 #define SSA(bs)         (RSCNT((bs)) + FATCNT((bs)) * SF((bs)) + RDS(bs))
    58 
    59 #define CLBN2PBN(bs, cl, bn) \
    60         (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
    61 
    6251/**
    6352 * The fat_alloc_lock mutex protects all copies of the File Allocation Table
     
    7059 *
    7160 * @param bs            Buffer holding the boot sector for the file.
    72  * @param devmap_handle Device handle of the device with the file.
     61 * @param dev_handle    Device handle of the device with the file.
    7362 * @param firstc        First cluster to start the walk with.
    7463 * @param lastc         If non-NULL, output argument hodling the last cluster
     
    8170 */
    8271int
    83 fat_cluster_walk(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc,
     72fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
    8473    fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
    8574{
    8675        block_t *b;
     76        unsigned bps;
     77        unsigned rscnt;         /* block address of the first FAT */
    8778        uint16_t clusters = 0;
    8879        fat_cluster_t clst = firstc;
    8980        int rc;
     81
     82        bps = uint16_t_le2host(bs->bps);
     83        rscnt = uint16_t_le2host(bs->rscnt);
    9084
    9185        if (firstc == FAT_CLST_RES0) {
     
    9993
    10094        while (clst < FAT_CLST_LAST1 && clusters < max_clusters) {
    101                 aoff64_t fsec;  /* sector offset relative to FAT1 */
     95                bn_t fsec;      /* sector offset relative to FAT1 */
    10296                unsigned fidx;  /* FAT1 entry index */
    10397
     
    10599                if (lastc)
    106100                        *lastc = clst;  /* remember the last cluster number */
    107                 fsec = (clst * sizeof(fat_cluster_t)) / BPS(bs);
    108                 fidx = clst % (BPS(bs) / sizeof(fat_cluster_t));
     101                fsec = (clst * sizeof(fat_cluster_t)) / bps;
     102                fidx = clst % (bps / sizeof(fat_cluster_t));
    109103                /* read FAT1 */
    110                 rc = block_get(&b, devmap_handle, RSCNT(bs) + fsec,
    111                     BLOCK_FLAGS_NONE);
     104                rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
    112105                if (rc != EOK)
    113106                        return rc;
     
    132125 * @param block         Pointer to a block pointer for storing result.
    133126 * @param bs            Buffer holding the boot sector of the file system.
    134  * @param nodep         FAT node.
     127 * @param dev_handle    Device handle of the file system.
     128 * @param firstc        First cluster used by the file. Can be zero if the file
     129 *                      is empty.
    135130 * @param bn            Block number.
    136131 * @param flags         Flags passed to libblock.
     
    139134 */
    140135int
    141 fat_block_get(block_t **block, struct fat_bs *bs, fat_node_t *nodep,
    142     aoff64_t bn, int flags)
    143 {
    144         fat_cluster_t firstc = nodep->firstc;
    145         fat_cluster_t currc;
    146         aoff64_t relbn = bn;
    147         int rc;
    148 
    149         if (!nodep->size)
    150                 return ELIMIT;
    151 
    152         if (nodep->firstc == FAT_CLST_ROOT)
    153                 goto fall_through;
    154 
    155         if (((((nodep->size - 1) / BPS(bs)) / SPC(bs)) == bn / SPC(bs)) &&
    156             nodep->lastc_cached_valid) {
    157                 /*
    158                  * This is a request to read a block within the last cluster
    159                  * when fortunately we have the last cluster number cached.
    160                  */
    161                 return block_get(block, nodep->idx->devmap_handle,
    162                     CLBN2PBN(bs, nodep->lastc_cached_value, bn), flags);
    163         }
    164 
    165         if (nodep->currc_cached_valid && bn >= nodep->currc_cached_bn) {
    166                 /*
    167                  * We can start with the cluster cached by the previous call to
    168                  * fat_block_get().
    169                  */
    170                 firstc = nodep->currc_cached_value;
    171                 relbn -= (nodep->currc_cached_bn / SPC(bs)) * SPC(bs);
    172         }
    173 
    174 fall_through:
    175         rc = _fat_block_get(block, bs, nodep->idx->devmap_handle, firstc,
    176             &currc, relbn, flags);
    177         if (rc != EOK)
    178                 return rc;
    179        
    180         /*
    181          * Update the "current" cluster cache.
    182          */
    183         nodep->currc_cached_valid = true;
    184         nodep->currc_cached_bn = bn;
    185         nodep->currc_cached_value = currc;
    186 
    187         return rc;
    188 }
    189 
    190 /** Read block from file located on a FAT file system.
    191  *
    192  * @param block         Pointer to a block pointer for storing result.
    193  * @param bs            Buffer holding the boot sector of the file system.
    194  * @param devmap_handle Device handle of the file system.
    195  * @param fcl           First cluster used by the file. Can be zero if the file
    196  *                      is empty.
    197  * @param clp           If not NULL, address where the cluster containing bn
    198  *                      will be stored.
    199  *                      stored
    200  * @param bn            Block number.
    201  * @param flags         Flags passed to libblock.
    202  *
    203  * @return              EOK on success or a negative error code.
    204  */
    205 int
    206 _fat_block_get(block_t **block, fat_bs_t *bs, devmap_handle_t devmap_handle,
    207     fat_cluster_t fcl, fat_cluster_t *clp, aoff64_t bn, int flags)
    208 {
     136_fat_block_get(block_t **block, fat_bs_t *bs, dev_handle_t dev_handle,
     137    fat_cluster_t firstc, bn_t bn, int flags)
     138{
     139        unsigned bps;
     140        unsigned rscnt;         /* block address of the first FAT */
     141        unsigned rde;
     142        unsigned rds;           /* root directory size */
     143        unsigned sf;
     144        unsigned ssa;           /* size of the system area */
    209145        uint16_t clusters;
    210146        unsigned max_clusters;
    211         fat_cluster_t c;
    212         int rc;
    213 
    214         /*
    215          * This function can only operate on non-zero length files.
    216          */
    217         if (fcl == FAT_CLST_RES0)
    218                 return ELIMIT;
    219 
    220         if (fcl == FAT_CLST_ROOT) {
     147        fat_cluster_t lastc;
     148        int rc;
     149
     150        bps = uint16_t_le2host(bs->bps);
     151        rscnt = uint16_t_le2host(bs->rscnt);
     152        rde = uint16_t_le2host(bs->root_ent_max);
     153        sf = uint16_t_le2host(bs->sec_per_fat);
     154
     155        rds = (sizeof(fat_dentry_t) * rde) / bps;
     156        rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
     157        ssa = rscnt + bs->fatcnt * sf + rds;
     158
     159        if (firstc == FAT_CLST_ROOT) {
    221160                /* root directory special case */
    222                 assert(bn < RDS(bs));
    223                 rc = block_get(block, devmap_handle,
    224                     RSCNT(bs) + FATCNT(bs) * SF(bs) + bn, flags);
     161                assert(bn < rds);
     162                rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn,
     163                    flags);
    225164                return rc;
    226165        }
    227166
    228         max_clusters = bn / SPC(bs);
    229         rc = fat_cluster_walk(bs, devmap_handle, fcl, &c, &clusters, max_clusters);
     167        max_clusters = bn / bs->spc;
     168        rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,
     169            max_clusters);
    230170        if (rc != EOK)
    231171                return rc;
    232172        assert(clusters == max_clusters);
    233173
    234         rc = block_get(block, devmap_handle, CLBN2PBN(bs, c, bn), flags);
    235 
    236         if (clp)
    237                 *clp = c;
     174        rc = block_get(block, dev_handle,
     175            ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
    238176
    239177        return rc;
     
    252190 * @return              EOK on success or a negative error code.
    253191 */
    254 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
    255 {
     192int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
     193{
     194        uint16_t bps;
     195        unsigned spc;
    256196        block_t *b;
    257         aoff64_t o, boundary;
    258         int rc;
    259 
    260         boundary = ROUND_UP(nodep->size, BPS(bs) * SPC(bs));
     197        off_t o, boundary;
     198        int rc;
     199
     200        bps = uint16_t_le2host(bs->bps);
     201        spc = bs->spc;
     202       
     203        boundary = ROUND_UP(nodep->size, bps * spc);
    261204
    262205        /* zero out already allocated space */
    263206        for (o = nodep->size; o < pos && o < boundary;
    264             o = ALIGN_DOWN(o + BPS(bs), BPS(bs))) {
    265                 int flags = (o % BPS(bs) == 0) ?
     207            o = ALIGN_DOWN(o + bps, bps)) {
     208                int flags = (o % bps == 0) ?
    266209                    BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
    267                 rc = fat_block_get(&b, bs, nodep, o / BPS(bs), flags);
    268                 if (rc != EOK)
    269                         return rc;
    270                 memset(b->data + o % BPS(bs), 0, BPS(bs) - o % BPS(bs));
     210                rc = fat_block_get(&b, bs, nodep, o / bps, flags);
     211                if (rc != EOK)
     212                        return rc;
     213                memset(b->data + o % bps, 0, bps - o % bps);
    271214                b->dirty = true;                /* need to sync node */
    272215                rc = block_put(b);
     
    279222       
    280223        /* zero out the initial part of the new cluster chain */
    281         for (o = boundary; o < pos; o += BPS(bs)) {
    282                 rc = _fat_block_get(&b, bs, nodep->idx->devmap_handle, mcl,
    283                     NULL, (o - boundary) / BPS(bs), BLOCK_FLAGS_NOREAD);
    284                 if (rc != EOK)
    285                         return rc;
    286                 memset(b->data, 0, min(BPS(bs), pos - o));
     224        for (o = boundary; o < pos; o += bps) {
     225                rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
     226                    (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
     227                if (rc != EOK)
     228                        return rc;
     229                memset(b->data, 0, min(bps, pos - o));
    287230                b->dirty = true;                /* need to sync node */
    288231                rc = block_put(b);
     
    297240 *
    298241 * @param bs            Buffer holding the boot sector for the file system.
    299  * @param devmap_handle Device handle for the file system.
     242 * @param dev_handle    Device handle for the file system.
    300243 * @param clst          Cluster which to get.
    301244 * @param value         Output argument holding the value of the cluster.
     
    304247 */
    305248int
    306 fat_get_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
     249fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
    307250    fat_cluster_t clst, fat_cluster_t *value)
    308251{
    309252        block_t *b;
     253        uint16_t bps;
     254        uint16_t rscnt;
     255        uint16_t sf;
    310256        fat_cluster_t *cp;
    311257        int rc;
    312258
    313         rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
    314             (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
     259        bps = uint16_t_le2host(bs->bps);
     260        rscnt = uint16_t_le2host(bs->rscnt);
     261        sf = uint16_t_le2host(bs->sec_per_fat);
     262
     263        rc = block_get(&b, dev_handle, rscnt + sf * fatno +
     264            (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
    315265        if (rc != EOK)
    316266                return rc;
    317         cp = (fat_cluster_t *)b->data +
    318             clst % (BPS(bs) / sizeof(fat_cluster_t));
     267        cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
    319268        *value = uint16_t_le2host(*cp);
    320269        rc = block_put(b);
     
    326275 *
    327276 * @param bs            Buffer holding the boot sector for the file system.
    328  * @param devmap_handle Device handle for the file system.
     277 * @param dev_handle    Device handle for the file system.
    329278 * @param fatno         Number of the FAT instance where to make the change.
    330279 * @param clst          Cluster which is to be set.
     
    334283 */
    335284int
    336 fat_set_cluster(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned fatno,
     285fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
    337286    fat_cluster_t clst, fat_cluster_t value)
    338287{
    339288        block_t *b;
     289        uint16_t bps;
     290        uint16_t rscnt;
     291        uint16_t sf;
    340292        fat_cluster_t *cp;
    341293        int rc;
    342294
    343         assert(fatno < FATCNT(bs));
    344         rc = block_get(&b, devmap_handle, RSCNT(bs) + SF(bs) * fatno +
    345             (clst * sizeof(fat_cluster_t)) / BPS(bs), BLOCK_FLAGS_NONE);
     295        bps = uint16_t_le2host(bs->bps);
     296        rscnt = uint16_t_le2host(bs->rscnt);
     297        sf = uint16_t_le2host(bs->sec_per_fat);
     298
     299        assert(fatno < bs->fatcnt);
     300        rc = block_get(&b, dev_handle, rscnt + sf * fatno +
     301            (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
    346302        if (rc != EOK)
    347303                return rc;
    348         cp = (fat_cluster_t *)b->data +
    349             clst % (BPS(bs) / sizeof(fat_cluster_t));
     304        cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
    350305        *cp = host2uint16_t_le(value);
    351306        b->dirty = true;                /* need to sync block */
     
    357312 *
    358313 * @param bs            Buffer holding the boot sector of the file system.
    359  * @param devmap_handle Device handle of the file system.
     314 * @param dev_handle    Device handle of the file system.
    360315 * @param lifo          Chain of allocated clusters.
    361316 * @param nclsts        Number of clusters in the lifo chain.
     
    363318 * @return              EOK on success or a negative error code.
    364319 */
    365 int fat_alloc_shadow_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle,
     320int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
    366321    fat_cluster_t *lifo, unsigned nclsts)
    367322{
     
    372327        for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
    373328                for (c = 0; c < nclsts; c++) {
    374                         rc = fat_set_cluster(bs, devmap_handle, fatno, lifo[c],
     329                        rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
    375330                            c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
    376331                        if (rc != EOK)
     
    390345 *
    391346 * @param bs            Buffer holding the boot sector of the file system.
    392  * @param devmap_handle Device handle of the file system.
     347 * @param dev_handle    Device handle of the file system.
    393348 * @param nclsts        Number of clusters to allocate.
    394349 * @param mcl           Output parameter where the first cluster in the chain
     
    400355 */
    401356int
    402 fat_alloc_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, unsigned nclsts,
     357fat_alloc_clusters(fat_bs_t *bs, dev_handle_t dev_handle, unsigned nclsts,
    403358    fat_cluster_t *mcl, fat_cluster_t *lcl)
    404359{
     360        uint16_t bps;
     361        uint16_t rscnt;
     362        uint16_t sf;
     363        uint32_t ts;
     364        unsigned rde;
     365        unsigned rds;
     366        unsigned ssa;
    405367        block_t *blk;
    406368        fat_cluster_t *lifo;    /* stack for storing free cluster numbers */
     
    413375                return ENOMEM;
    414376       
     377        bps = uint16_t_le2host(bs->bps);
     378        rscnt = uint16_t_le2host(bs->rscnt);
     379        sf = uint16_t_le2host(bs->sec_per_fat);
     380        rde = uint16_t_le2host(bs->root_ent_max);
     381        ts = (uint32_t) uint16_t_le2host(bs->totsec16);
     382        if (ts == 0)
     383                ts = uint32_t_le2host(bs->totsec32);
     384
     385        rds = (sizeof(fat_dentry_t) * rde) / bps;
     386        rds += ((sizeof(fat_dentry_t) * rde) % bps != 0);
     387        ssa = rscnt + bs->fatcnt * sf + rds;
     388       
    415389        /*
    416390         * Search FAT1 for unused clusters.
    417391         */
    418392        fibril_mutex_lock(&fat_alloc_lock);
    419         for (b = 0, cl = 0; b < SF(bs); b++) {
    420                 rc = block_get(&blk, devmap_handle, RSCNT(bs) + b,
    421                     BLOCK_FLAGS_NONE);
     393        for (b = 0, cl = 0; b < sf; b++) {
     394                rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
    422395                if (rc != EOK)
    423396                        goto error;
    424                 for (c = 0; c < BPS(bs) / sizeof(fat_cluster_t); c++, cl++) {
     397                for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
    425398                        /*
    426                          * Check if the entire cluster is physically there.
    427                          * This check becomes necessary when the file system is
    428                          * created with fewer total sectors than how many is
    429                          * inferred from the size of the file allocation table
    430                          * or when the last cluster ends beyond the end of the
    431                          * device.
     399                         * Check if the cluster is physically there. This check
     400                         * becomes necessary when the file system is created
     401                         * with fewer total sectors than how many is inferred
     402                         * from the size of the file allocation table.
    432403                         */
    433                         if ((cl >= FAT_CLST_FIRST) &&
    434                             CLBN2PBN(bs, cl, SPC(bs) - 1) >= TS(bs)) {
     404                        if ((cl >= 2) && ((cl - 2) * bs->spc + ssa >= ts)) {
    435405                                rc = block_put(blk);
    436406                                if (rc != EOK)
     
    457427                                        /* update the shadow copies of FAT */
    458428                                        rc = fat_alloc_shadow_clusters(bs,
    459                                             devmap_handle, lifo, nclsts);
     429                                            dev_handle, lifo, nclsts);
    460430                                        if (rc != EOK)
    461431                                                goto error;
     
    484454         */
    485455        while (found--) {
    486                 rc = fat_set_cluster(bs, devmap_handle, FAT1, lifo[found],
     456                rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
    487457                    FAT_CLST_RES0);
    488458                if (rc != EOK) {
     
    499469 *
    500470 * @param bs            Buffer hodling the boot sector of the file system.
    501  * @param devmap_handle Device handle of the file system.
     471 * @param dev_handle    Device handle of the file system.
    502472 * @param firstc        First cluster in the chain which is to be freed.
    503473 *
     
    505475 */
    506476int
    507 fat_free_clusters(fat_bs_t *bs, devmap_handle_t devmap_handle, fat_cluster_t firstc)
     477fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
    508478{
    509479        unsigned fatno;
     
    514484        while (firstc < FAT_CLST_LAST1) {
    515485                assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
    516                 rc = fat_get_cluster(bs, devmap_handle, FAT1, firstc, &nextc);
     486                rc = fat_get_cluster(bs, dev_handle, FAT1, firstc, &nextc);
    517487                if (rc != EOK)
    518488                        return rc;
    519489                for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
    520                         rc = fat_set_cluster(bs, devmap_handle, fatno, firstc,
     490                        rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
    521491                            FAT_CLST_RES0);
    522492                        if (rc != EOK)
     
    535505 * @param nodep         Node representing the file.
    536506 * @param mcl           First cluster of the cluster chain to append.
    537  * @param lcl           Last cluster of the cluster chain to append.
    538507 *
    539508 * @return              EOK on success or a negative error code.
    540509 */
    541 int
    542 fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
    543     fat_cluster_t lcl)
    544 {
    545         devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
    546         fat_cluster_t lastc;
     510int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
     511{
     512        dev_handle_t dev_handle = nodep->idx->dev_handle;
     513        fat_cluster_t lcl;
     514        uint16_t numc;
    547515        uint8_t fatno;
    548516        int rc;
    549517
    550         if (nodep->firstc == FAT_CLST_RES0) {
     518        rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc,
     519            (uint16_t) -1);
     520        if (rc != EOK)
     521                return rc;
     522
     523        if (numc == 0) {
    551524                /* No clusters allocated to the node yet. */
    552525                nodep->firstc = mcl;
    553                 nodep->dirty = true;    /* need to sync node */
    554         } else {
    555                 if (nodep->lastc_cached_valid) {
    556                         lastc = nodep->lastc_cached_value;
    557                         nodep->lastc_cached_valid = false;
    558                 } else {
    559                         rc = fat_cluster_walk(bs, devmap_handle, nodep->firstc,
    560                             &lastc, NULL, (uint16_t) -1);
    561                         if (rc != EOK)
    562                                 return rc;
    563                 }
    564 
    565                 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
    566                         rc = fat_set_cluster(bs, nodep->idx->devmap_handle, fatno,
    567                             lastc, mcl);
    568                         if (rc != EOK)
    569                                 return rc;
    570                 }
    571         }
    572 
    573         nodep->lastc_cached_valid = true;
    574         nodep->lastc_cached_value = lcl;
     526                nodep->dirty = true;            /* need to sync node */
     527                return EOK;
     528        }
     529
     530        for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
     531                rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl,
     532                    mcl);
     533                if (rc != EOK)
     534                        return rc;
     535        }
    575536
    576537        return EOK;
     
    581542 * @param bs            Buffer holding the boot sector of the file system.
    582543 * @param nodep         FAT node where the chopping will take place.
    583  * @param lcl           Last cluster which will remain in the node. If this
     544 * @param lastc         Last cluster which will remain in the node. If this
    584545 *                      argument is FAT_CLST_RES0, then all clusters will
    585546 *                      be chopped off.
     
    587548 * @return              EOK on success or a negative return code.
    588549 */
    589 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lcl)
    590 {
    591         int rc;
    592         devmap_handle_t devmap_handle = nodep->idx->devmap_handle;
    593 
    594         /*
    595          * Invalidate cached cluster numbers.
    596          */
    597         nodep->lastc_cached_valid = false;
    598         if (nodep->currc_cached_value != lcl)
    599                 nodep->currc_cached_valid = false;
    600 
    601         if (lcl == FAT_CLST_RES0) {
     550int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
     551{
     552        int rc;
     553
     554        dev_handle_t dev_handle = nodep->idx->dev_handle;
     555        if (lastc == FAT_CLST_RES0) {
    602556                /* The node will have zero size and no clusters allocated. */
    603                 rc = fat_free_clusters(bs, devmap_handle, nodep->firstc);
     557                rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
    604558                if (rc != EOK)
    605559                        return rc;
     
    610564                unsigned fatno;
    611565
    612                 rc = fat_get_cluster(bs, devmap_handle, FAT1, lcl, &nextc);
     566                rc = fat_get_cluster(bs, dev_handle, FAT1, lastc, &nextc);
    613567                if (rc != EOK)
    614568                        return rc;
     
    616570                /* Terminate the cluster chain in all copies of FAT. */
    617571                for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
    618                         rc = fat_set_cluster(bs, devmap_handle, fatno, lcl,
     572                        rc = fat_set_cluster(bs, dev_handle, fatno, lastc,
    619573                            FAT_CLST_LAST1);
    620574                        if (rc != EOK)
     
    623577
    624578                /* Free all following clusters. */
    625                 rc = fat_free_clusters(bs, devmap_handle, nextc);
    626                 if (rc != EOK)
    627                         return rc;
    628         }
    629 
    630         /*
    631          * Update and re-enable the last cluster cache.
    632          */
    633         nodep->lastc_cached_valid = true;
    634         nodep->lastc_cached_value = lcl;
     579                rc = fat_free_clusters(bs, dev_handle, nextc);
     580                if (rc != EOK)
     581                        return rc;
     582        }
    635583
    636584        return EOK;
     
    638586
    639587int
    640 fat_zero_cluster(struct fat_bs *bs, devmap_handle_t devmap_handle, fat_cluster_t c)
     588fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
    641589{
    642590        int i;
    643591        block_t *b;
    644         int rc;
    645 
    646         for (i = 0; i < SPC(bs); i++) {
    647                 rc = _fat_block_get(&b, bs, devmap_handle, c, NULL, i,
     592        unsigned bps;
     593        int rc;
     594
     595        bps = uint16_t_le2host(bs->bps);
     596       
     597        for (i = 0; i < bs->spc; i++) {
     598                rc = _fat_block_get(&b, bs, dev_handle, c, i,
    648599                    BLOCK_FLAGS_NOREAD);
    649600                if (rc != EOK)
    650601                        return rc;
    651                 memset(b->data, 0, BPS(bs));
     602                memset(b->data, 0, bps);
    652603                b->dirty = true;
    653604                rc = block_put(b);
     
    665616 * does not contain a fat file system.
    666617 */
    667 int fat_sanity_check(fat_bs_t *bs, devmap_handle_t devmap_handle)
     618int fat_sanity_check(fat_bs_t *bs, dev_handle_t dev_handle)
    668619{
    669620        fat_cluster_t e0, e1;
     
    706657
    707658        for (fat_no = 0; fat_no < bs->fatcnt; fat_no++) {
    708                 rc = fat_get_cluster(bs, devmap_handle, fat_no, 0, &e0);
     659                rc = fat_get_cluster(bs, dev_handle, fat_no, 0, &e0);
    709660                if (rc != EOK)
    710661                        return EIO;
    711662
    712                 rc = fat_get_cluster(bs, devmap_handle, fat_no, 1, &e1);
     663                rc = fat_get_cluster(bs, dev_handle, fat_no, 1, &e1);
    713664                if (rc != EOK)
    714665                        return EIO;
Note: See TracChangeset for help on using the changeset viewer.