Ignore:
File:
1 edited

Legend:

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

    rdba4a23 r69a60c4  
    6060#define FS_NODE(node)   ((node) ? (node)->bp : NULL)
    6161
    62 #define DPS(bs)         (BPS((bs)) / sizeof(fat_dentry_t))
    63 #define BPC(bs)         (BPS((bs)) * SPC((bs)))
    64 
    6562/** Mutex protecting the list of cached free FAT nodes. */
    6663static FIBRIL_MUTEX_INITIALIZE(ffn_mutex);
     
    104101        node->refcnt = 0;
    105102        node->dirty = false;
    106         node->lastc_cached_valid = false;
    107         node->lastc_cached_value = FAT_CLST_LAST1;
    108         node->currc_cached_valid = false;
    109         node->currc_cached_bn = 0;
    110         node->currc_cached_value = FAT_CLST_LAST1;
    111103}
    112104
     
    116108        fat_bs_t *bs;
    117109        fat_dentry_t *d;
     110        uint16_t bps;
     111        unsigned dps;
    118112        int rc;
    119113       
     
    121115
    122116        bs = block_bb_get(node->idx->dev_handle);
     117        bps = uint16_t_le2host(bs->bps);
     118        dps = bps / sizeof(fat_dentry_t);
    123119       
    124120        /* Read the block that contains the dentry of interest. */
    125121        rc = _fat_block_get(&b, bs, node->idx->dev_handle, node->idx->pfc,
    126             NULL, (node->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
    127             BLOCK_FLAGS_NONE);
     122            (node->idx->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
    128123        if (rc != EOK)
    129124                return rc;
    130125
    131         d = ((fat_dentry_t *)b->data) + (node->idx->pdi % DPS(bs));
     126        d = ((fat_dentry_t *)b->data) + (node->idx->pdi % dps);
    132127
    133128        d->firstc = host2uint16_t_le(node->firstc);
     
    271266        fat_dentry_t *d;
    272267        fat_node_t *nodep = NULL;
     268        unsigned bps;
     269        unsigned spc;
     270        unsigned dps;
    273271        int rc;
    274272
     
    300298
    301299        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);
    302303
    303304        /* Read the block that contains the dentry of interest. */
    304         rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc, NULL,
    305             (idxp->pdi * sizeof(fat_dentry_t)) / BPS(bs), BLOCK_FLAGS_NONE);
     305        rc = _fat_block_get(&b, bs, idxp->dev_handle, idxp->pfc,
     306            (idxp->pdi * sizeof(fat_dentry_t)) / bps, BLOCK_FLAGS_NONE);
    306307        if (rc != EOK) {
    307308                (void) fat_node_put(FS_NODE(nodep));
     
    309310        }
    310311
    311         d = ((fat_dentry_t *)b->data) + (idxp->pdi % DPS(bs));
     312        d = ((fat_dentry_t *)b->data) + (idxp->pdi % dps);
    312313        if (d->attr & FAT_ATTR_SUBDIR) {
    313314                /*
     
    329330                        return rc;
    330331                }
    331                 nodep->size = BPS(bs) * SPC(bs) * clusters;
     332                nodep->size = bps * spc * clusters;
    332333        } else {
    333334                nodep->type = FAT_FILE;
     
    367368        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
    368369        unsigned i, j;
     370        unsigned bps;           /* bytes per sector */
     371        unsigned dps;           /* dentries per sector */
    369372        unsigned blocks;
    370373        fat_dentry_t *d;
     
    374377        fibril_mutex_lock(&parentp->idx->lock);
    375378        bs = block_bb_get(parentp->idx->dev_handle);
    376         blocks = parentp->size / BPS(bs);
     379        bps = uint16_t_le2host(bs->bps);
     380        dps = bps / sizeof(fat_dentry_t);
     381        blocks = parentp->size / bps;
    377382        for (i = 0; i < blocks; i++) {
    378383                rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
     
    381386                        return rc;
    382387                }
    383                 for (j = 0; j < DPS(bs); j++) {
     388                for (j = 0; j < dps; j++) {
    384389                        d = ((fat_dentry_t *)b->data) + j;
    385390                        switch (fat_classify_dentry(d)) {
     
    409414                                fat_idx_t *idx = fat_idx_get_by_pos(
    410415                                    parentp->idx->dev_handle, parentp->firstc,
    411                                     i * DPS(bs) + j);
     416                                    i * dps + j);
    412417                                fibril_mutex_unlock(&parentp->idx->lock);
    413418                                if (!idx) {
     
    508513        fat_bs_t *bs;
    509514        fat_cluster_t mcl, lcl;
     515        uint16_t bps;
    510516        int rc;
    511517
    512518        bs = block_bb_get(dev_handle);
     519        bps = uint16_t_le2host(bs->bps);
    513520        if (flags & L_DIRECTORY) {
    514521                /* allocate a cluster */
     
    539546                nodep->type = FAT_DIRECTORY;
    540547                nodep->firstc = mcl;
    541                 nodep->size = BPS(bs) * SPC(bs);
     548                nodep->size = bps * bs->spc;
    542549        } else {
    543550                nodep->type = FAT_FILE;
     
    602609        block_t *b;
    603610        unsigned i, j;
     611        uint16_t bps;
     612        unsigned dps;
    604613        unsigned blocks;
    605614        fat_cluster_t mcl, lcl;
     
    631640        fibril_mutex_lock(&parentp->idx->lock);
    632641        bs = block_bb_get(parentp->idx->dev_handle);
    633 
    634         blocks = parentp->size / BPS(bs);
     642        bps = uint16_t_le2host(bs->bps);
     643        dps = bps / sizeof(fat_dentry_t);
     644
     645        blocks = parentp->size / bps;
    635646
    636647        for (i = 0; i < blocks; i++) {
     
    640651                        return rc;
    641652                }
    642                 for (j = 0; j < DPS(bs); j++) {
     653                for (j = 0; j < dps; j++) {
    643654                        d = ((fat_dentry_t *)b->data) + j;
    644655                        switch (fat_classify_dentry(d)) {
     
    680691                return rc;
    681692        }
    682         rc = fat_append_clusters(bs, parentp, mcl, lcl);
     693        rc = fat_append_clusters(bs, parentp, mcl);
    683694        if (rc != EOK) {
    684695                (void) fat_free_clusters(bs, parentp->idx->dev_handle, mcl);
     
    686697                return rc;
    687698        }
    688         parentp->size += BPS(bs) * SPC(bs);
     699        parentp->size += bps * bs->spc;
    689700        parentp->dirty = true;          /* need to sync node */
    690701        rc = fat_block_get(&b, bs, parentp, i, BLOCK_FLAGS_NONE);
     
    760771
    761772        childp->idx->pfc = parentp->firstc;
    762         childp->idx->pdi = i * DPS(bs) + j;
     773        childp->idx->pdi = i * dps + j;
    763774        fibril_mutex_unlock(&childp->idx->lock);
    764775
     
    782793        fat_bs_t *bs;
    783794        fat_dentry_t *d;
     795        uint16_t bps;
    784796        block_t *b;
    785797        bool has_children;
     
    800812        fibril_mutex_lock(&childp->idx->lock);
    801813        bs = block_bb_get(childp->idx->dev_handle);
     814        bps = uint16_t_le2host(bs->bps);
    802815
    803816        rc = _fat_block_get(&b, bs, childp->idx->dev_handle, childp->idx->pfc,
    804             NULL, (childp->idx->pdi * sizeof(fat_dentry_t)) / BPS(bs),
     817            (childp->idx->pdi * sizeof(fat_dentry_t)) / bps,
    805818            BLOCK_FLAGS_NONE);
    806819        if (rc != EOK)
    807820                goto error;
    808821        d = (fat_dentry_t *)b->data +
    809             (childp->idx->pdi % (BPS(bs) / sizeof(fat_dentry_t)));
     822            (childp->idx->pdi % (bps / sizeof(fat_dentry_t)));
    810823        /* mark the dentry as not-currently-used */
    811824        d->name[0] = FAT_DENTRY_ERASED;
     
    839852        fat_bs_t *bs;
    840853        fat_node_t *nodep = FAT_NODE(fn);
     854        unsigned bps;
     855        unsigned dps;
    841856        unsigned blocks;
    842857        block_t *b;
     
    851866        fibril_mutex_lock(&nodep->idx->lock);
    852867        bs = block_bb_get(nodep->idx->dev_handle);
    853 
    854         blocks = nodep->size / BPS(bs);
     868        bps = uint16_t_le2host(bs->bps);
     869        dps = bps / sizeof(fat_dentry_t);
     870
     871        blocks = nodep->size / bps;
    855872
    856873        for (i = 0; i < blocks; i++) {
     
    862879                        return rc;
    863880                }
    864                 for (j = 0; j < DPS(bs); j++) {
     881                for (j = 0; j < dps; j++) {
    865882                        d = ((fat_dentry_t *)b->data) + j;
    866883                        switch (fat_classify_dentry(d)) {
     
    959976        enum cache_mode cmode;
    960977        fat_bs_t *bs;
     978        uint16_t bps;
     979        uint16_t rde;
    961980       
    962981        /* Accept the mount options */
     
    9951014        bs = block_bb_get(dev_handle);
    9961015       
    997         if (BPS(bs) != BS_SIZE) {
     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) {
    9981021                block_fini(dev_handle);
    9991022                ipc_answer_0(rid, ENOTSUP);
     
    10021025
    10031026        /* Initialize the block cache */
    1004         rc = block_cache_init(dev_handle, BPS(bs), 0 /* XXX */, cmode);
     1027        rc = block_cache_init(dev_handle, bps, 0 /* XXX */, cmode);
    10051028        if (rc != EOK) {
    10061029                block_fini(dev_handle);
     
    10641087        rootp->refcnt = 1;
    10651088        rootp->lnkcnt = 0;      /* FS root is not linked */
    1066         rootp->size = RDE(bs) * sizeof(fat_dentry_t);
     1089        rootp->size = rde * sizeof(fat_dentry_t);
    10671090        rootp->idx = ridxp;
    10681091        ridxp->nodep = rootp;
     
    11421165        fat_node_t *nodep;
    11431166        fat_bs_t *bs;
     1167        uint16_t bps;
    11441168        size_t bytes;
    11451169        block_t *b;
     
    11671191
    11681192        bs = block_bb_get(dev_handle);
     1193        bps = uint16_t_le2host(bs->bps);
    11691194
    11701195        if (nodep->type == FAT_FILE) {
     
    11791204                        (void) async_data_read_finalize(callid, NULL, 0);
    11801205                } else {
    1181                         bytes = min(len, BPS(bs) - pos % BPS(bs));
     1206                        bytes = min(len, bps - pos % bps);
    11821207                        bytes = min(bytes, nodep->size - pos);
    1183                         rc = fat_block_get(&b, bs, nodep, pos / BPS(bs),
     1208                        rc = fat_block_get(&b, bs, nodep, pos / bps,
    11841209                            BLOCK_FLAGS_NONE);
    11851210                        if (rc != EOK) {
     
    11891214                                return;
    11901215                        }
    1191                         (void) async_data_read_finalize(callid,
    1192                             b->data + pos % BPS(bs), bytes);
     1216                        (void) async_data_read_finalize(callid, b->data + pos % bps,
     1217                            bytes);
    11931218                        rc = block_put(b);
    11941219                        if (rc != EOK) {
     
    12051230
    12061231                assert(nodep->type == FAT_DIRECTORY);
    1207                 assert(nodep->size % BPS(bs) == 0);
    1208                 assert(BPS(bs) % sizeof(fat_dentry_t) == 0);
     1232                assert(nodep->size % bps == 0);
     1233                assert(bps % sizeof(fat_dentry_t) == 0);
    12091234
    12101235                /*
     
    12141239                 * the position pointer accordingly.
    12151240                 */
    1216                 bnum = (pos * sizeof(fat_dentry_t)) / BPS(bs);
    1217                 while (bnum < nodep->size / BPS(bs)) {
     1241                bnum = (pos * sizeof(fat_dentry_t)) / bps;
     1242                while (bnum < nodep->size / bps) {
    12181243                        aoff64_t o;
    12191244
     
    12221247                        if (rc != EOK)
    12231248                                goto err;
    1224                         for (o = pos % (BPS(bs) / sizeof(fat_dentry_t));
    1225                             o < BPS(bs) / sizeof(fat_dentry_t);
     1249                        for (o = pos % (bps / sizeof(fat_dentry_t));
     1250                            o < bps / sizeof(fat_dentry_t);
    12261251                            o++, pos++) {
    12271252                                d = ((fat_dentry_t *)b->data) + o;
     
    12811306        size_t bytes, size;
    12821307        block_t *b;
     1308        uint16_t bps;
     1309        unsigned spc;
     1310        unsigned bpc;           /* bytes per cluster */
    12831311        aoff64_t boundary;
    12841312        int flags = BLOCK_FLAGS_NONE;
     
    13061334
    13071335        bs = block_bb_get(dev_handle);
     1336        bps = uint16_t_le2host(bs->bps);
     1337        spc = bs->spc;
     1338        bpc = bps * spc;
    13081339
    13091340        /*
     
    13141345         * value signalizing a smaller number of bytes written.
    13151346         */
    1316         bytes = min(len, BPS(bs) - pos % BPS(bs));
    1317         if (bytes == BPS(bs))
     1347        bytes = min(len, bps - pos % bps);
     1348        if (bytes == bps)
    13181349                flags |= BLOCK_FLAGS_NOREAD;
    13191350       
    1320         boundary = ROUND_UP(nodep->size, BPC(bs));
     1351        boundary = ROUND_UP(nodep->size, bpc);
    13211352        if (pos < boundary) {
    13221353                /*
     
    13331364                        return;
    13341365                }
    1335                 rc = fat_block_get(&b, bs, nodep, pos / BPS(bs), flags);
     1366                rc = fat_block_get(&b, bs, nodep, pos / bps, flags);
    13361367                if (rc != EOK) {
    13371368                        (void) fat_node_put(fn);
     
    13401371                        return;
    13411372                }
    1342                 (void) async_data_write_finalize(callid,
    1343                     b->data + pos % BPS(bs), bytes);
     1373                (void) async_data_write_finalize(callid, b->data + pos % bps,
     1374                    bytes);
    13441375                b->dirty = true;                /* need to sync block */
    13451376                rc = block_put(b);
     
    13651396                fat_cluster_t mcl, lcl;
    13661397 
    1367                 nclsts = (ROUND_UP(pos + bytes, BPC(bs)) - boundary) / BPC(bs);
     1398                nclsts = (ROUND_UP(pos + bytes, bpc) - boundary) / bpc;
    13681399                /* create an independent chain of nclsts clusters in all FATs */
    13691400                rc = fat_alloc_clusters(bs, dev_handle, nclsts, &mcl, &lcl);
     
    13841415                        return;
    13851416                }
    1386                 rc = _fat_block_get(&b, bs, dev_handle, lcl, NULL,
    1387                     (pos / BPS(bs)) % SPC(bs), flags);
     1417                rc = _fat_block_get(&b, bs, dev_handle, lcl, (pos / bps) % spc,
     1418                    flags);
    13881419                if (rc != EOK) {
    13891420                        (void) fat_free_clusters(bs, dev_handle, mcl);
     
    13931424                        return;
    13941425                }
    1395                 (void) async_data_write_finalize(callid,
    1396                     b->data + pos % BPS(bs), bytes);
     1426                (void) async_data_write_finalize(callid, b->data + pos % bps,
     1427                    bytes);
    13971428                b->dirty = true;                /* need to sync block */
    13981429                rc = block_put(b);
     
    14071438                 * node's cluster chain.
    14081439                 */
    1409                 rc = fat_append_clusters(bs, nodep, mcl, lcl);
     1440                rc = fat_append_clusters(bs, nodep, mcl);
    14101441                if (rc != EOK) {
    14111442                        (void) fat_free_clusters(bs, dev_handle, mcl);
     
    14311462        fat_node_t *nodep;
    14321463        fat_bs_t *bs;
     1464        uint16_t bps;
     1465        uint8_t spc;
     1466        unsigned bpc;   /* bytes per cluster */
    14331467        int rc;
    14341468
     
    14451479
    14461480        bs = block_bb_get(dev_handle);
     1481        bps = uint16_t_le2host(bs->bps);
     1482        spc = bs->spc;
     1483        bpc = bps * spc;
    14471484
    14481485        if (nodep->size == size) {
     
    14541491                 */
    14551492                rc = EINVAL;
    1456         } else if (ROUND_UP(nodep->size, BPC(bs)) == ROUND_UP(size, BPC(bs))) {
     1493        } else if (ROUND_UP(nodep->size, bpc) == ROUND_UP(size, bpc)) {
    14571494                /*
    14581495                 * The node will be shrunk, but no clusters will be deallocated.
     
    14721509                        fat_cluster_t lastc;
    14731510                        rc = fat_cluster_walk(bs, dev_handle, nodep->firstc,
    1474                             &lastc, NULL, (size - 1) / BPC(bs));
     1511                            &lastc, NULL, (size - 1) / bpc);
    14751512                        if (rc != EOK)
    14761513                                goto out;
     
    15271564void fat_sync(ipc_callid_t rid, ipc_call_t *request)
    15281565{
    1529         /* Dummy implementation */
    1530         ipc_answer_0(rid, EOK);
     1566        dev_handle_t dev_handle = (dev_handle_t) IPC_GET_ARG1(*request);
     1567        fs_index_t index = (fs_index_t) IPC_GET_ARG2(*request);
     1568       
     1569        fs_node_t *fn;
     1570        int rc = fat_node_get(&fn, dev_handle, index);
     1571        if (rc != EOK) {
     1572                ipc_answer_0(rid, rc);
     1573                return;
     1574        }
     1575        if (!fn) {
     1576                ipc_answer_0(rid, ENOENT);
     1577                return;
     1578        }
     1579       
     1580        fat_node_t *nodep = FAT_NODE(fn);
     1581       
     1582        nodep->dirty = true;
     1583        rc = fat_node_sync(nodep);
     1584       
     1585        fat_node_put(fn);
     1586        ipc_answer_0(rid, rc);
    15311587}
    15321588
Note: See TracChangeset for help on using the changeset viewer.