Ignore:
File:
1 edited

Legend:

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

    r2f636b6 rc91f2d1b  
    6161 * @param dev_handle    Device handle of the device with the file.
    6262 * @param firstc        First cluster to start the walk with.
    63  * @param lastc         If non-NULL, output argument hodling the last cluster
    64  *                      number visited.
    65  * @param numc          If non-NULL, output argument holding the number of
    66  *                      clusters seen during the walk.
     63 * @param lastc         If non-NULL, output argument hodling the last cluster number visited.
    6764 * @param max_clusters  Maximum number of clusters to visit.   
    6865 *
    69  * @return              EOK on success or a negative error code.
    70  */
    71 int
     66 * @return              Number of clusters seen during the walk.
     67 */
     68uint16_t
    7269fat_cluster_walk(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
    73     fat_cluster_t *lastc, uint16_t *numc, uint16_t max_clusters)
     70    fat_cluster_t *lastc, uint16_t max_clusters)
    7471{
    7572        block_t *b;
     
    8784                if (lastc)
    8885                        *lastc = firstc;
    89                 if (numc)
    90                         *numc = 0;
    91                 return EOK;
     86                return 0;
    9287        }
    9388
     
    10398                /* read FAT1 */
    10499                rc = block_get(&b, dev_handle, rscnt + fsec, BLOCK_FLAGS_NONE);
    105                 if (rc != EOK)
    106                         return rc;
     100                assert(rc == EOK);
    107101                clst = uint16_t_le2host(((fat_cluster_t *)b->data)[fidx]);
    108102                assert(clst != FAT_CLST_BAD);
    109103                rc = block_put(b);
    110                 if (rc != EOK)
    111                         return rc;
     104                assert(rc == EOK);
    112105                clusters++;
    113106        }
     
    115108        if (lastc && clst < FAT_CLST_LAST1)
    116109                *lastc = clst;
    117         if (numc)
    118                 *numc = clusters;
    119 
    120         return EOK;
     110
     111        return clusters;
    121112}
    122113
    123114/** Read block from file located on a FAT file system.
    124115 *
    125  * @param block         Pointer to a block pointer for storing result.
    126116 * @param bs            Buffer holding the boot sector of the file system.
    127117 * @param dev_handle    Device handle of the file system.
     
    131121 * @param flags         Flags passed to libblock.
    132122 *
    133  * @return              EOK on success or a negative error code.
    134  */
    135 int
    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 {
     123 * @return              Block structure holding the requested block.
     124 */
     125block_t *
     126_fat_block_get(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc,
     127    bn_t bn, int flags)
     128{
     129        block_t *b;
    139130        unsigned bps;
    140131        unsigned rscnt;         /* block address of the first FAT */
     
    143134        unsigned sf;
    144135        unsigned ssa;           /* size of the system area */
    145         uint16_t clusters;
    146         unsigned max_clusters;
     136        unsigned clusters, max_clusters;
    147137        fat_cluster_t lastc;
    148138        int rc;
     
    160150                /* root directory special case */
    161151                assert(bn < rds);
    162                 rc = block_get(block, dev_handle, rscnt + bs->fatcnt * sf + bn,
     152                rc = block_get(&b, dev_handle, rscnt + bs->fatcnt * sf + bn,
    163153                    flags);
    164                 return rc;
     154                assert(rc == EOK);
     155                return b;
    165156        }
    166157
    167158        max_clusters = bn / bs->spc;
    168         rc = fat_cluster_walk(bs, dev_handle, firstc, &lastc, &clusters,
     159        clusters = fat_cluster_walk(bs, dev_handle, firstc, &lastc,
    169160            max_clusters);
    170         if (rc != EOK)
    171                 return rc;
    172161        assert(clusters == max_clusters);
    173162
    174         rc = block_get(block, dev_handle,
    175             ssa + (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
    176 
    177         return rc;
     163        rc = block_get(&b, dev_handle, ssa +
     164            (lastc - FAT_CLST_FIRST) * bs->spc + bn % bs->spc, flags);
     165        assert(rc == EOK);
     166
     167        return b;
    178168}
    179169
     
    187177 *                      this argument is ignored.
    188178 * @param pos           Position in the last node block.
    189  *
    190  * @return              EOK on success or a negative error code.
    191  */
    192 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
     179 */
     180void fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, off_t pos)
    193181{
    194182        uint16_t bps;
     
    208196                int flags = (o % bps == 0) ?
    209197                    BLOCK_FLAGS_NOREAD : BLOCK_FLAGS_NONE;
    210                 rc = fat_block_get(&b, bs, nodep, o / bps, flags);
    211                 if (rc != EOK)
    212                         return rc;
     198                b = fat_block_get(bs, nodep, o / bps, flags);
    213199                memset(b->data + o % bps, 0, bps - o % bps);
    214200                b->dirty = true;                /* need to sync node */
    215201                rc = block_put(b);
    216                 if (rc != EOK)
    217                         return rc;
     202                assert(rc == EOK);
    218203        }
    219204       
    220205        if (o >= pos)
    221                 return EOK;
     206                return;
    222207       
    223208        /* zero out the initial part of the new cluster chain */
    224209        for (o = boundary; o < pos; o += bps) {
    225                 rc = _fat_block_get(&b, bs, nodep->idx->dev_handle, mcl,
     210                b = _fat_block_get(bs, nodep->idx->dev_handle, mcl,
    226211                    (o - boundary) / bps, BLOCK_FLAGS_NOREAD);
    227                 if (rc != EOK)
    228                         return rc;
    229212                memset(b->data, 0, min(bps, pos - o));
    230213                b->dirty = true;                /* need to sync node */
    231214                rc = block_put(b);
    232                 if (rc != EOK)
    233                         return rc;
    234         }
    235 
    236         return EOK;
     215                assert(rc == EOK);
     216        }
    237217}
    238218
     
    242222 * @param dev_handle    Device handle for the file system.
    243223 * @param clst          Cluster which to get.
    244  * @param value         Output argument holding the value of the cluster.
    245  *
    246  * @return              EOK or a negative error code.
    247  */
    248 int
    249 fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst,
    250     fat_cluster_t *value)
     224 *
     225 * @return              Value found in the cluster.
     226 */
     227fat_cluster_t
     228fat_get_cluster(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t clst)
    251229{
    252230        block_t *b;
    253231        uint16_t bps;
    254232        uint16_t rscnt;
    255         fat_cluster_t *cp;
     233        fat_cluster_t *cp, value;
    256234        int rc;
    257235
     
    261239        rc = block_get(&b, dev_handle, rscnt +
    262240            (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
    263         if (rc != EOK)
    264                 return rc;
     241        assert(rc == EOK);
    265242        cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
    266         *value = uint16_t_le2host(*cp);
     243        value = uint16_t_le2host(*cp);
    267244        rc = block_put(b);
    268        
    269         return rc;
     245        assert(rc == EOK);
     246       
     247        return value;
    270248}
    271249
     
    277255 * @param clst          Cluster which is to be set.
    278256 * @param value         Value to set the cluster with.
    279  *
    280  * @return              EOK on success or a negative error code.
    281  */
    282 int
     257 */
     258void
    283259fat_set_cluster(fat_bs_t *bs, dev_handle_t dev_handle, unsigned fatno,
    284260    fat_cluster_t clst, fat_cluster_t value)
     
    298274        rc = block_get(&b, dev_handle, rscnt + sf * fatno +
    299275            (clst * sizeof(fat_cluster_t)) / bps, BLOCK_FLAGS_NONE);
    300         if (rc != EOK)
    301                 return rc;
     276        assert(rc == EOK);
    302277        cp = (fat_cluster_t *)b->data + clst % (bps / sizeof(fat_cluster_t));
    303278        *cp = host2uint16_t_le(value);
    304279        b->dirty = true;                /* need to sync block */
    305280        rc = block_put(b);
    306         return rc;
     281        assert(rc == EOK);
    307282}
    308283
     
    313288 * @param lifo          Chain of allocated clusters.
    314289 * @param nclsts        Number of clusters in the lifo chain.
    315  *
    316  * @return              EOK on success or a negative error code.
    317  */
    318 int fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
     290 */
     291void fat_alloc_shadow_clusters(fat_bs_t *bs, dev_handle_t dev_handle,
    319292    fat_cluster_t *lifo, unsigned nclsts)
    320293{
    321294        uint8_t fatno;
    322295        unsigned c;
    323         int rc;
    324296
    325297        for (fatno = FAT1 + 1; fatno < bs->fatcnt; fatno++) {
    326298                for (c = 0; c < nclsts; c++) {
    327                         rc = fat_set_cluster(bs, dev_handle, fatno, lifo[c],
     299                        fat_set_cluster(bs, dev_handle, fatno, lifo[c],
    328300                            c == 0 ? FAT_CLST_LAST1 : lifo[c - 1]);
    329                         if (rc != EOK)
    330                                 return rc;
    331301                }
    332302        }
    333 
    334         return EOK;
    335303}
    336304
     
    379347        for (b = 0, cl = 0; b < sf; b++) {
    380348                rc = block_get(&blk, dev_handle, rscnt + b, BLOCK_FLAGS_NONE);
    381                 if (rc != EOK)
    382                         goto error;
    383349                for (c = 0; c < bps / sizeof(fat_cluster_t); c++, cl++) {
    384350                        fat_cluster_t *clst = (fat_cluster_t *)blk->data + c;
     
    396362                                        /* we are almost done */
    397363                                        rc = block_put(blk);
    398                                         if (rc != EOK)
    399                                                 goto error;
     364                                        assert(rc == EOK);
    400365                                        /* update the shadow copies of FAT */
    401                                         rc = fat_alloc_shadow_clusters(bs,
     366                                        fat_alloc_shadow_clusters(bs,
    402367                                            dev_handle, lifo, nclsts);
    403                                         if (rc != EOK)
    404                                                 goto error;
    405368                                        *mcl = lifo[found - 1];
    406369                                        *lcl = lifo[0];
     
    412375                }
    413376                rc = block_put(blk);
    414                 if (rc != EOK) {
    415 error:
    416                         fibril_mutex_unlock(&fat_alloc_lock);
    417                         free(lifo);
    418                         return rc;
    419                 }
     377                assert(rc == EOK);
    420378        }
    421379        fibril_mutex_unlock(&fat_alloc_lock);
     
    426384         */
    427385        while (found--) {
    428                 rc = fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
     386                fat_set_cluster(bs, dev_handle, FAT1, lifo[found],
    429387                    FAT_CLST_RES0);
    430                 if (rc != EOK) {
    431                         free(lifo);
    432                         return rc;
    433                 }
    434388        }
    435389       
     
    443397 * @param dev_handle    Device handle of the file system.
    444398 * @param firstc        First cluster in the chain which is to be freed.
    445  *
    446  * @return              EOK on success or a negative return code.
    447  */
    448 int
     399 */
     400void
    449401fat_free_clusters(fat_bs_t *bs, dev_handle_t dev_handle, fat_cluster_t firstc)
    450402{
    451403        unsigned fatno;
    452404        fat_cluster_t nextc;
    453         int rc;
    454405
    455406        /* Mark all clusters in the chain as free in all copies of FAT. */
    456407        while (firstc < FAT_CLST_LAST1) {
    457408                assert(firstc >= FAT_CLST_FIRST && firstc < FAT_CLST_BAD);
    458                 rc = fat_get_cluster(bs, dev_handle, firstc, &nextc);
    459                 if (rc != EOK)
    460                         return rc;
    461                 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
    462                         rc = fat_set_cluster(bs, dev_handle, fatno, firstc,
     409                nextc = fat_get_cluster(bs, dev_handle, firstc);
     410                for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
     411                        fat_set_cluster(bs, dev_handle, fatno, firstc,
    463412                            FAT_CLST_RES0);
    464                         if (rc != EOK)
    465                                 return rc;
    466                 }
    467 
    468413                firstc = nextc;
    469414        }
    470 
    471         return EOK;
    472415}
    473416
     
    477420 * @param nodep         Node representing the file.
    478421 * @param mcl           First cluster of the cluster chain to append.
    479  *
    480  * @return              EOK on success or a negative error code.
    481  */
    482 int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
     422 */
     423void fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl)
    483424{
    484425        dev_handle_t dev_handle = nodep->idx->dev_handle;
    485426        fat_cluster_t lcl;
    486         uint16_t numc;
    487427        uint8_t fatno;
    488         int rc;
    489 
    490         rc = fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl, &numc,
    491             (uint16_t) -1);
    492         if (rc != EOK)
    493                 return rc;
    494 
    495         if (numc == 0) {
     428
     429        if (fat_cluster_walk(bs, dev_handle, nodep->firstc, &lcl,
     430            (uint16_t) -1) == 0) {
    496431                /* No clusters allocated to the node yet. */
    497432                nodep->firstc = mcl;
    498433                nodep->dirty = true;            /* need to sync node */
    499                 return EOK;
    500         }
    501 
    502         for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
    503                 rc = fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl,
    504                     mcl);
    505                 if (rc != EOK)
    506                         return rc;
    507         }
    508 
    509         return EOK;
     434                return;
     435        }
     436
     437        for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
     438                fat_set_cluster(bs, nodep->idx->dev_handle, fatno, lcl, mcl);
    510439}
    511440
     
    517446 *                      argument is FAT_CLST_RES0, then all clusters will
    518447 *                      be chopped off.
    519  *
    520  * @return              EOK on success or a negative return code.
    521  */
    522 int fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
    523 {
    524         int rc;
    525 
     448 */
     449void fat_chop_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t lastc)
     450{
    526451        dev_handle_t dev_handle = nodep->idx->dev_handle;
    527452        if (lastc == FAT_CLST_RES0) {
    528453                /* The node will have zero size and no clusters allocated. */
    529                 rc = fat_free_clusters(bs, dev_handle, nodep->firstc);
    530                 if (rc != EOK)
    531                         return rc;
     454                fat_free_clusters(bs, dev_handle, nodep->firstc);
    532455                nodep->firstc = FAT_CLST_RES0;
    533456                nodep->dirty = true;            /* need to sync node */
     
    536459                unsigned fatno;
    537460
    538                 rc = fat_get_cluster(bs, dev_handle, lastc, &nextc);
    539                 if (rc != EOK)
    540                         return rc;
     461                nextc = fat_get_cluster(bs, dev_handle, lastc);
    541462
    542463                /* Terminate the cluster chain in all copies of FAT. */
    543                 for (fatno = FAT1; fatno < bs->fatcnt; fatno++) {
    544                         rc = fat_set_cluster(bs, dev_handle, fatno, lastc,
    545                             FAT_CLST_LAST1);
    546                         if (rc != EOK)
    547                                 return rc;
    548                 }
     464                for (fatno = FAT1; fatno < bs->fatcnt; fatno++)
     465                        fat_set_cluster(bs, dev_handle, fatno, lastc, FAT_CLST_LAST1);
    549466
    550467                /* Free all following clusters. */
    551                 rc = fat_free_clusters(bs, dev_handle, nextc);
    552                 if (rc != EOK)
    553                         return rc;
    554         }
    555 
    556         return EOK;
    557 }
    558 
    559 int
     468                fat_free_clusters(bs, dev_handle, nextc);
     469        }
     470}
     471
     472void
    560473fat_zero_cluster(struct fat_bs *bs, dev_handle_t dev_handle, fat_cluster_t c)
    561474{
     
    568481       
    569482        for (i = 0; i < bs->spc; i++) {
    570                 rc = _fat_block_get(&b, bs, dev_handle, c, i,
    571                     BLOCK_FLAGS_NOREAD);
    572                 if (rc != EOK)
    573                         return rc;
     483                b = _fat_block_get(bs, dev_handle, c, i, BLOCK_FLAGS_NOREAD);
    574484                memset(b->data, 0, bps);
    575485                b->dirty = true;
    576486                rc = block_put(b);
    577                 if (rc != EOK)
    578                         return rc;
    579         }
    580 
    581         return EOK;
     487                assert(rc == EOK);
     488        }
    582489}
    583490
Note: See TracChangeset for help on using the changeset viewer.