Changeset 5d95f02 in mainline for uspace/srv/fs/fat/fat_fat.c


Ignore:
Timestamp:
2011-08-26T23:04:07Z (14 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4bf6895
Parents:
0dbe5ac
Message:

Cleanup.

File:
1 edited

Legend:

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

    r0dbe5ac r5d95f02  
    5151#include <mem.h>
    5252
    53 /*
    54  * Convenience macros for computing some frequently used values from the
    55  * primitive boot sector members.
    56  */
    57 #define CLBN2PBN(bs, cl, bn) \
    58         (SSA((bs)) + ((cl) - FAT_CLST_FIRST) * SPC((bs)) + (bn) % SPC((bs)))
    59 
    6053#define IS_ODD(number)  (number & 0x1)
    6154
     
    244237 * @return              EOK on success or a negative error code.
    245238 */
    246 int fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
     239int
     240fat_fill_gap(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl, aoff64_t pos)
    247241{
    248242        block_t *b;
     
    286280}
    287281
    288 /** Get cluster from the first FAT. FAT12 version
     282/** Get cluster from the first FAT.
    289283 *
    290284 * @param bs            Buffer holding the boot sector for the file system.
     
    304298        int rc;
    305299
    306         offset = (clst + clst/2);
     300        offset = (clst + clst / 2);
    307301        if (offset / BPS(bs) >= SF(bs))
    308302                return ERANGE;
     
    313307                return rc;
    314308
    315         byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
     309        byte1 = ((uint8_t *) b->data)[offset % BPS(bs)];
    316310        /* This cluster access spans a sector boundary. Check only for FAT12 */
    317311        if ((offset % BPS(bs)) + 1 == BPS(bs)) {
    318                 /* Is it last sector of FAT? */
     312                /* Is this the last sector of FAT? */
    319313                if (offset / BPS(bs) < SF(bs)) {
    320                         /* No. Reading next sector */
     314                        /* No, read the next sector */
    321315                        rc = block_get(&b1, service_id, 1 + RSCNT(bs) +
    322                                 SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
     316                            SF(bs) * fatno + offset / BPS(bs),
     317                            BLOCK_FLAGS_NONE);
    323318                        if (rc != EOK) {
    324319                                block_put(b);
     
    336331                                return rc;
    337332                        }
    338                 }
    339                 else {
    340                         /* Yes. It is last sector of FAT */
     333                } else {
     334                        /* Yes. This is the last sector of FAT */
    341335                        block_put(b);
    342336                        return ERANGE;
    343337                }
    344         }
    345         else
    346                 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
     338        } else
     339                byte2 = ((uint8_t *) b->data)[(offset % BPS(bs)) + 1];
    347340
    348341        *value = uint16_t_le2host(byte1 | (byte2 << 8));
     
    353346       
    354347        rc = block_put(b);
     348
    355349        return rc;
    356350}
    357351
    358 /** Get cluster from the first FAT. FAT16 version
     352/** Get cluster from the first FAT.
    359353 *
    360354 * @param bs            Buffer holding the boot sector for the file system.
     
    387381}
    388382
    389 /** Get cluster from the first FAT. FAT32 version
     383/** Get cluster from the first FAT.
    390384 *
    391385 * @param bs            Buffer holding the boot sector for the file system.
     
    411405                return rc;
    412406
    413         *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) & FAT32_MASK;
     407        *value = uint32_t_le2host(*(uint32_t *)(b->data + offset % BPS(bs))) &
     408            FAT32_MASK;
    414409
    415410        rc = block_put(b);
     
    436431        assert(fatno < FATCNT(bs));
    437432
    438         if (FAT_IS_FAT12(bs)) {
     433        if (FAT_IS_FAT12(bs))
    439434                rc = fat_get_cluster_fat12(bs, service_id, fatno, clst, value);
    440         }
    441         else {
    442                 if (FAT_IS_FAT32(bs))
    443                         rc = fat_get_cluster_fat32(bs, service_id, fatno, clst, value);
    444                 else
    445                         rc = fat_get_cluster_fat16(bs, service_id, fatno, clst, value);
    446         }
     435        else if (FAT_IS_FAT16(bs))
     436                rc = fat_get_cluster_fat16(bs, service_id, fatno, clst, value);
     437        else
     438                rc = fat_get_cluster_fat32(bs, service_id, fatno, clst, value);
    447439
    448440        return rc;
    449441}
    450442
    451 /** Set cluster in one instance of FAT. FAT12 version.
     443/** Set cluster in one instance of FAT.
    452444 *
    453445 * @param bs            Buffer holding the boot sector for the file system.
     
    463455    fat_cluster_t clst, fat_cluster_t value)
    464456{
    465         block_t *b, *b1=NULL;
     457        block_t *b, *b1 = NULL;
    466458        aoff64_t offset;
    467459        uint16_t byte1, byte2;
    468460        int rc;
    469461
    470         offset = (clst + clst/2);
     462        offset = (clst + clst / 2);
    471463        if (offset / BPS(bs) >= SF(bs))
    472464                return ERANGE;
     
    479471        byte1 = ((uint8_t*) b->data)[offset % BPS(bs)];
    480472        bool border = false;
    481         /* This cluster access spans a sector boundary. Check only for FAT12 */
    482         if ((offset % BPS(bs))+1 == BPS(bs)) {
    483                 /* Is it last sector of FAT? */
     473        /* This cluster access spans a sector boundary. */
     474        if ((offset % BPS(bs)) + 1 == BPS(bs)) {
     475                /* Is it the last sector of FAT? */
    484476                if (offset / BPS(bs) < SF(bs)) {
    485                         /* No. Reading next sector */
     477                        /* No, read the next sector */
    486478                        rc = block_get(&b1, service_id, 1 + RSCNT(bs) +
    487                                 SF(bs)*fatno + offset / BPS(bs), BLOCK_FLAGS_NONE);
     479                            SF(bs) * fatno + offset / BPS(bs),
     480                            BLOCK_FLAGS_NONE);
    488481                        if (rc != EOK) {
    489482                                block_put(b);
     
    494487                         * first byte of next sector
    495488                         */
    496                         byte2 = ((uint8_t*) b1->data)[0];
     489                        byte2 = ((uint8_t *) b1->data)[0];
    497490                        border = true;
    498                 }
    499                 else {
    500                         /* Yes. It is last sector of fat */
     491                } else {
     492                        /* Yes. This is the last sector of FAT */
    501493                        block_put(b);
    502494                        return ERANGE;
     
    504496        }
    505497        else
    506                 byte2 = ((uint8_t*) b->data)[(offset % BPS(bs))+1];
     498                byte2 = ((uint8_t*) b->data)[(offset % BPS(bs)) + 1];
    507499
    508500        if (IS_ODD(clst)) {
     
    519511        byte2 = byte2 | (value >> 8);
    520512
    521         ((uint8_t*) b->data)[(offset % BPS(bs))] = byte1;
     513        ((uint8_t *) b->data)[(offset % BPS(bs))] = byte1;
    522514        if (border) {
    523                 ((uint8_t*) b1->data)[0] = byte2;
     515                ((uint8_t *) b1->data)[0] = byte2;
    524516
    525517                b1->dirty = true;
     
    530522                }
    531523        } else
    532                 ((uint8_t*) b->data)[(offset % BPS(bs))+1] = byte2;
     524                ((uint8_t *) b->data)[(offset % BPS(bs)) + 1] = byte2;
    533525
    534526        b->dirty = true;        /* need to sync block */
    535527        rc = block_put(b);
     528
    536529        return rc;
    537530}
    538531
    539 /** Set cluster in one instance of FAT. FAT16 version.
     532/** Set cluster in one instance of FAT.
    540533 *
    541534 * @param bs            Buffer holding the boot sector for the file system.
     
    566559        b->dirty = true;        /* need to sync block */
    567560        rc = block_put(b);
     561
    568562        return rc;
    569563}
    570564
    571 /** Set cluster in one instance of FAT. FAT32 version.
     565/** Set cluster in one instance of FAT.
    572566 *
    573567 * @param bs            Buffer holding the boot sector for the file system.
     
    602596        b->dirty = true;        /* need to sync block */
    603597        rc = block_put(b);
     598
    604599        return rc;
    605600}
     
    625620        if (FAT_IS_FAT12(bs))
    626621                rc = fat_set_cluster_fat12(bs, service_id, fatno, clst, value);
    627         else if (FAT_IS_FAT32(bs))
     622        else if (FAT_IS_FAT16(bs))
     623                rc = fat_set_cluster_fat16(bs, service_id, fatno, clst, value);
     624        else
    628625                rc = fat_set_cluster_fat32(bs, service_id, fatno, clst, value);
    629         else
    630                 rc = fat_set_cluster_fat16(bs, service_id, fatno, clst, value);
    631626
    632627        return rc;
     
    695690         */
    696691        fibril_mutex_lock(&fat_alloc_lock);
    697         for (clst=FAT_CLST_FIRST; clst < CC(bs)+2 && found < nclsts; clst++) {
     692        for (clst = FAT_CLST_FIRST; clst < CC(bs) + 2 && found < nclsts;
     693            clst++) {
    698694                rc = fat_get_cluster(bs, service_id, FAT1, clst, &value);
    699695                if (rc != EOK)
    700                 break;
     696                        break;
    701697
    702698                if (value == FAT_CLST_RES0) {
    703                 /*
    704                 * The cluster is free. Put it into our stack
    705                 * of found clusters and mark it as non-free.
    706                 */
    707                 lifo[found] = clst;
    708                 rc = fat_set_cluster(bs, service_id, FAT1, clst,
    709                     (found == 0) ?  clst_last1 : lifo[found - 1]);
    710                 if (rc != EOK)
    711                         break;
    712 
    713                 found++;
     699                        /*
     700                        * The cluster is free. Put it into our stack
     701                        * of found clusters and mark it as non-free.
     702                        */
     703                        lifo[found] = clst;
     704                        rc = fat_set_cluster(bs, service_id, FAT1, clst,
     705                            (found == 0) ?  clst_last1 : lifo[found - 1]);
     706                        if (rc != EOK)
     707                                break;
     708
     709                        found++;
    714710                }
    715711        }
     
    727723
    728724        /* If something wrong - free the clusters */
    729         if (found > 0) {
    730                 while (found--) {
    731                 rc = fat_set_cluster(bs, service_id, FAT1, lifo[found],
     725        while (found--) {
     726                (void) fat_set_cluster(bs, service_id, FAT1, lifo[found],
    732727                    FAT_CLST_RES0);
    733                 }
    734728        }
    735729
    736730        free(lifo);
    737731        fibril_mutex_unlock(&fat_alloc_lock);
     732
    738733        return ENOSPC;
    739734}
     
    757752        while (firstc < FAT_CLST_LAST1(bs)) {
    758753                assert(firstc >= FAT_CLST_FIRST && firstc < clst_bad);
     754
    759755                rc = fat_get_cluster(bs, service_id, FAT1, firstc, &nextc);
    760756                if (rc != EOK)
    761757                        return rc;
     758
    762759                for (fatno = FAT1; fatno < FATCNT(bs); fatno++) {
    763760                        rc = fat_set_cluster(bs, service_id, fatno, firstc,
     
    766763                                return rc;
    767764                }
    768 
    769765                firstc = nextc;
    770766        }
     
    782778 * @return              EOK on success or a negative error code.
    783779 */
    784 int
    785 fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
     780int fat_append_clusters(fat_bs_t *bs, fat_node_t *nodep, fat_cluster_t mcl,
    786781    fat_cluster_t lcl)
    787782{
     
    942937         * sanitized to support file systems with this property.
    943938         */
    944         if (!FAT_IS_FAT32(bs) && (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)
     939        if (!FAT_IS_FAT32(bs) &&
     940            (RDE(bs) * sizeof(fat_dentry_t)) % BPS(bs) != 0)
    945941                return ENOTSUP;
    946942
     
    955951                        return EIO;
    956952
    957                 /* Check that first byte of FAT contains the media descriptor. */
     953                /*
     954                 * Check that first byte of FAT contains the media descriptor.
     955                 */
    958956                if ((e0 & 0xff) != bs->mdesc)
    959957                        return ENOTSUP;
     
    964962                 */
    965963                if (!FAT_IS_FAT12(bs) &&
    966                         ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs)))
     964                    ((e0 >> 8) != (FAT_MASK(bs) >> 8) || e1 != FAT_MASK(bs)))
    967965                        return ENOTSUP;
    968966        }
Note: See TracChangeset for help on using the changeset viewer.