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


Ignore:
Timestamp:
2009-05-05T22:09:13Z (15 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
770d281
Parents:
c852f4be
Message:

Introduce the concept of FS nodes. A FS node is a typed abstraction of
file-system-specific node type. It replaces the void * in libfs interfaces
and is suitable for holding various information such as mount point data.

File:
1 edited

Legend:

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

    rc852f4be rb6035ba  
    5656#include <align.h>
    5757
     58#define FAT_NODE(node)  ((node) ? (fat_node_t *) (node)->data : NULL)
     59#define FS_NODE(node)   ((node) ? (node)->bp : NULL)
     60
    5861/** Futex protecting the list of cached free FAT nodes. */
    5962static futex_t ffn_futex = FUTEX_INITIALIZER;
     
    6568{
    6669        futex_initialize(&node->lock, 1);
     70        node->bp = NULL;
    6771        node->idx = NULL;
    6872        node->type = 0;
     
    109113static fat_node_t *fat_node_get_new(void)
    110114{
     115        fs_node_t *fn;
    111116        fat_node_t *nodep;
    112117
     
    130135                futex_up(&nodep->lock);
    131136                futex_up(&idxp_tmp->lock);
     137                fn = FS_NODE(nodep);
    132138        } else {
    133139skip_cache:
    134140                /* Try to allocate a new node structure. */
    135141                futex_up(&ffn_futex);
     142                fn = (fs_node_t *)malloc(sizeof(fs_node_t));
     143                if (!fn)
     144                        return NULL;
    136145                nodep = (fat_node_t *)malloc(sizeof(fat_node_t));
    137                 if (!nodep)
     146                if (!nodep) {
     147                        free(fn);
    138148                        return NULL;
     149                }
    139150        }
    140151        fat_node_initialize(nodep);
     152        fn->data = nodep;
     153        nodep->bp = fn;
    141154       
    142155        return nodep;
     
    147160 * @param idxp          Locked index structure.
    148161 */
    149 static void *fat_node_get_core(fat_idx_t *idxp)
     162static fat_node_t *fat_node_get_core(fat_idx_t *idxp)
    150163{
    151164        block_t *b;
     
    224237 * Forward declarations of FAT libfs operations.
    225238 */
    226 static void *fat_node_get(dev_handle_t, fs_index_t);
    227 static void fat_node_put(void *);
    228 static void *fat_create_node(dev_handle_t, int);
    229 static int fat_destroy_node(void *);
    230 static int fat_link(void *, void *, const char *);
    231 static int fat_unlink(void *, void *);
    232 static void *fat_match(void *, const char *);
    233 static fs_index_t fat_index_get(void *);
    234 static size_t fat_size_get(void *);
    235 static unsigned fat_lnkcnt_get(void *);
    236 static bool fat_has_children(void *);
    237 static void *fat_root_get(dev_handle_t);
     239static fs_node_t *fat_node_get(dev_handle_t, fs_index_t);
     240static void fat_node_put(fs_node_t *);
     241static fs_node_t *fat_create_node(dev_handle_t, int);
     242static int fat_destroy_node(fs_node_t *);
     243static int fat_link(fs_node_t *, fs_node_t *, const char *);
     244static int fat_unlink(fs_node_t *, fs_node_t *);
     245static fs_node_t *fat_match(fs_node_t *, const char *);
     246static fs_index_t fat_index_get(fs_node_t *);
     247static size_t fat_size_get(fs_node_t *);
     248static unsigned fat_lnkcnt_get(fs_node_t *);
     249static bool fat_has_children(fs_node_t *);
     250static fs_node_t *fat_root_get(dev_handle_t);
    238251static char fat_plb_get_char(unsigned);
    239 static bool fat_is_directory(void *);
    240 static bool fat_is_file(void *node);
     252static bool fat_is_directory(fs_node_t *);
     253static bool fat_is_file(fs_node_t *node);
    241254
    242255/*
     
    245258
    246259/** Instantiate a FAT in-core node. */
    247 void *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
    248 {
    249         void *node;
     260fs_node_t *fat_node_get(dev_handle_t dev_handle, fs_index_t index)
     261{
     262        fat_node_t *nodep;
    250263        fat_idx_t *idxp;
    251264
     
    254267                return NULL;
    255268        /* idxp->lock held */
    256         node = fat_node_get_core(idxp);
     269        nodep = fat_node_get_core(idxp);
    257270        futex_up(&idxp->lock);
    258         return node;
    259 }
    260 
    261 void fat_node_put(void *node)
    262 {
    263         fat_node_t *nodep = (fat_node_t *)node;
     271        return FS_NODE(nodep);
     272}
     273
     274void fat_node_put(fs_node_t *fn)
     275{
     276        fat_node_t *nodep = FAT_NODE(fn);
    264277        bool destroy = false;
    265278
     
    281294        }
    282295        futex_up(&nodep->lock);
    283         if (destroy)
    284                 free(node);
    285 }
    286 
    287 void *fat_create_node(dev_handle_t dev_handle, int flags)
     296        if (destroy) {
     297                free(nodep->bp);
     298                free(nodep);
     299        }
     300}
     301
     302fs_node_t *fat_create_node(dev_handle_t dev_handle, int flags)
    288303{
    289304        fat_idx_t *idxp;
     
    311326        if (!idxp) {
    312327                fat_free_clusters(bs, dev_handle, mcl);
    313                 fat_node_put(nodep);
     328                fat_node_put(FS_NODE(nodep));
    314329                return NULL;
    315330        }
     
    346361
    347362        futex_up(&idxp->lock);
    348         return nodep;
    349 }
    350 
    351 int fat_destroy_node(void *node)
    352 {
    353         fat_node_t *nodep = (fat_node_t *)node;
     363        return FS_NODE(nodep);
     364}
     365
     366int fat_destroy_node(fs_node_t *fn)
     367{
     368        fat_node_t *nodep = FAT_NODE(fn);
    354369        fat_bs_t *bs;
    355370
     
    365380         * The node may not have any children.
    366381         */
    367         assert(fat_has_children(node) == false);
     382        assert(fat_has_children(fn) == false);
    368383
    369384        bs = block_bb_get(nodep->idx->dev_handle);
     
    375390
    376391        fat_idx_destroy(nodep->idx);
     392        free(nodep->bp);
    377393        free(nodep);
    378394        return EOK;
    379395}
    380396
    381 int fat_link(void *prnt, void *chld, const char *name)
    382 {
    383         fat_node_t *parentp = (fat_node_t *)prnt;
    384         fat_node_t *childp = (fat_node_t *)chld;
     397int fat_link(fs_node_t *pfn, fs_node_t *cfn, const char *name)
     398{
     399        fat_node_t *parentp = FAT_NODE(pfn);
     400        fat_node_t *childp = FAT_NODE(cfn);
    385401        fat_dentry_t *d;
    386402        fat_bs_t *bs;
     
    528544}
    529545
    530 int fat_unlink(void *prnt, void *chld)
    531 {
    532         fat_node_t *parentp = (fat_node_t *)prnt;
    533         fat_node_t *childp = (fat_node_t *)chld;
     546int fat_unlink(fs_node_t *pfn, fs_node_t *cfn)
     547{
     548        fat_node_t *parentp = FAT_NODE(pfn);
     549        fat_node_t *childp = FAT_NODE(cfn);
    534550        fat_bs_t *bs;
    535551        fat_dentry_t *d;
     
    568584}
    569585
    570 void *fat_match(void *prnt, const char *component)
     586fs_node_t *fat_match(fs_node_t *pfn, const char *component)
    571587{
    572588        fat_bs_t *bs;
    573         fat_node_t *parentp = (fat_node_t *)prnt;
     589        fat_node_t *parentp = FAT_NODE(pfn);
    574590        char name[FAT_NAME_LEN + 1 + FAT_EXT_LEN + 1];
    575591        unsigned i, j;
     
    604620                        if (fat_dentry_namecmp(name, component) == 0) {
    605621                                /* hit */
    606                                 void *node;
     622                                fat_node_t *nodep;
    607623                                /*
    608624                                 * Assume tree hierarchy for locking.  We
     
    623639                                        return NULL;
    624640                                }
    625                                 node = fat_node_get_core(idx);
     641                                nodep = fat_node_get_core(idx);
    626642                                futex_up(&idx->lock);
    627643                                block_put(b);
    628                                 return node;
     644                                return FS_NODE(nodep);
    629645                        }
    630646                }
     
    636652}
    637653
    638 fs_index_t fat_index_get(void *node)
    639 {
    640         fat_node_t *fnodep = (fat_node_t *)node;
    641         if (!fnodep)
    642                 return 0;
    643         return fnodep->idx->index;
    644 }
    645 
    646 size_t fat_size_get(void *node)
    647 {
    648         return ((fat_node_t *)node)->size;
    649 }
    650 
    651 unsigned fat_lnkcnt_get(void *node)
    652 {
    653         return ((fat_node_t *)node)->lnkcnt;
    654 }
    655 
    656 bool fat_has_children(void *node)
     654fs_index_t fat_index_get(fs_node_t *fn)
     655{
     656        return FAT_NODE(fn)->idx->index;
     657}
     658
     659size_t fat_size_get(fs_node_t *fn)
     660{
     661        return FAT_NODE(fn)->size;
     662}
     663
     664unsigned fat_lnkcnt_get(fs_node_t *fn)
     665{
     666        return FAT_NODE(fn)->lnkcnt;
     667}
     668
     669bool fat_has_children(fs_node_t *fn)
    657670{
    658671        fat_bs_t *bs;
    659         fat_node_t *nodep = (fat_node_t *)node;
     672        fat_node_t *nodep = FAT_NODE(fn);
    660673        unsigned bps;
    661674        unsigned dps;
     
    705718}
    706719
    707 void *fat_root_get(dev_handle_t dev_handle)
     720fs_node_t *fat_root_get(dev_handle_t dev_handle)
    708721{
    709722        return fat_node_get(dev_handle, 0);
     
    715728}
    716729
    717 bool fat_is_directory(void *node)
    718 {
    719         return ((fat_node_t *)node)->type == FAT_DIRECTORY;
    720 }
    721 
    722 bool fat_is_file(void *node)
    723 {
    724         return ((fat_node_t *)node)->type == FAT_FILE;
     730bool fat_is_directory(fs_node_t *fn)
     731{
     732        return FAT_NODE(fn)->type == FAT_DIRECTORY;
     733}
     734
     735bool fat_is_file(fs_node_t *fn)
     736{
     737        return FAT_NODE(fn)->type == FAT_FILE;
    725738}
    726739
     
    822835
    823836        /* Initialize the root node. */
    824         fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
    825         if (!rootp) {
     837        fs_node_t *rfn = (fs_node_t *)malloc(sizeof(fs_node_t));
     838        if (!rfn) {
    826839                block_fini(dev_handle);
    827840                fat_idx_fini_by_dev_handle(dev_handle);
     
    829842                return;
    830843        }
     844        fat_node_t *rootp = (fat_node_t *)malloc(sizeof(fat_node_t));
     845        if (!rootp) {
     846                free(rfn);
     847                block_fini(dev_handle);
     848                fat_idx_fini_by_dev_handle(dev_handle);
     849                ipc_answer_0(rid, ENOMEM);
     850                return;
     851        }
    831852        fat_node_initialize(rootp);
    832853
    833854        fat_idx_t *ridxp = fat_idx_get_by_pos(dev_handle, FAT_CLST_ROOTPAR, 0);
    834855        if (!ridxp) {
     856                free(rfn);
     857                free(rootp);
    835858                block_fini(dev_handle);
    836                 free(rootp);
    837859                fat_idx_fini_by_dev_handle(dev_handle);
    838860                ipc_answer_0(rid, ENOMEM);
     
    849871        rootp->idx = ridxp;
    850872        ridxp->nodep = rootp;
     873        rootp->bp = rfn;
     874        rfn->data = rootp;
    851875       
    852876        futex_up(&ridxp->lock);
     
    870894        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    871895        off_t pos = (off_t)IPC_GET_ARG3(*request);
    872         fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
     896        fs_node_t *fn = fat_node_get(dev_handle, index);
     897        fat_node_t *nodep;
    873898        fat_bs_t *bs;
    874899        uint16_t bps;
     
    876901        block_t *b;
    877902
    878         if (!nodep) {
     903        if (!fn) {
    879904                ipc_answer_0(rid, ENOENT);
    880905                return;
    881906        }
     907        nodep = FAT_NODE(fn);
    882908
    883909        ipc_callid_t callid;
    884910        size_t len;
    885911        if (!ipc_data_read_receive(&callid, &len)) {
    886                 fat_node_put(nodep);
     912                fat_node_put(fn);
    887913                ipc_answer_0(callid, EINVAL);
    888914                ipc_answer_0(rid, EINVAL);
     
    955981                }
    956982miss:
    957                 fat_node_put(nodep);
     983                fat_node_put(fn);
    958984                ipc_answer_0(callid, ENOENT);
    959985                ipc_answer_1(rid, ENOENT, 0);
     
    964990        }
    965991
    966         fat_node_put(nodep);
     992        fat_node_put(fn);
    967993        ipc_answer_1(rid, EOK, (ipcarg_t)bytes);
    968994}
     
    973999        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    9741000        off_t pos = (off_t)IPC_GET_ARG3(*request);
    975         fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
     1001        fs_node_t *fn = fat_node_get(dev_handle, index);
     1002        fat_node_t *nodep;
    9761003        fat_bs_t *bs;
    9771004        size_t bytes;
     
    9831010        int flags = BLOCK_FLAGS_NONE;
    9841011       
    985         if (!nodep) {
     1012        if (!fn) {
    9861013                ipc_answer_0(rid, ENOENT);
    9871014                return;
    9881015        }
     1016        nodep = FAT_NODE(fn);
    9891017       
    9901018        ipc_callid_t callid;
    9911019        size_t len;
    9921020        if (!ipc_data_write_receive(&callid, &len)) {
    993                 fat_node_put(nodep);
     1021                fat_node_put(fn);
    9941022                ipc_answer_0(callid, EINVAL);
    9951023                ipc_answer_0(rid, EINVAL);
     
    10321060                }
    10331061                ipc_answer_2(rid, EOK, bytes, nodep->size);     
    1034                 fat_node_put(nodep);
     1062                fat_node_put(fn);
    10351063                return;
    10361064        } else {
     
    10481076                if (status != EOK) {
    10491077                        /* could not allocate a chain of nclsts clusters */
    1050                         fat_node_put(nodep);
     1078                        fat_node_put(fn);
    10511079                        ipc_answer_0(callid, status);
    10521080                        ipc_answer_0(rid, status);
     
    10691097                nodep->dirty = true;            /* need to sync node */
    10701098                ipc_answer_2(rid, EOK, bytes, nodep->size);
    1071                 fat_node_put(nodep);
     1099                fat_node_put(fn);
    10721100                return;
    10731101        }
     
    10791107        fs_index_t index = (fs_index_t)IPC_GET_ARG2(*request);
    10801108        size_t size = (off_t)IPC_GET_ARG3(*request);
    1081         fat_node_t *nodep = (fat_node_t *)fat_node_get(dev_handle, index);
     1109        fs_node_t *fn = fat_node_get(dev_handle, index);
     1110        fat_node_t *nodep;
    10821111        fat_bs_t *bs;
    10831112        uint16_t bps;
     
    10861115        int rc;
    10871116
    1088         if (!nodep) {
     1117        if (!fn) {
    10891118                ipc_answer_0(rid, ENOENT);
    10901119                return;
    10911120        }
     1121        nodep = FAT_NODE(fn);
    10921122
    10931123        bs = block_bb_get(dev_handle);
     
    11271157                rc = EOK;       
    11281158        }
    1129         fat_node_put(nodep);
     1159        fat_node_put(fn);
    11301160        ipc_answer_0(rid, rc);
    11311161        return;
     
    11381168        int rc;
    11391169
    1140         fat_node_t *nodep = fat_node_get(dev_handle, index);
    1141         if (!nodep) {
     1170        fs_node_t *fn = fat_node_get(dev_handle, index);
     1171        if (!fn) {
    11421172                ipc_answer_0(rid, ENOENT);
    11431173                return;
    11441174        }
    11451175
    1146         rc = fat_destroy_node(nodep);
     1176        rc = fat_destroy_node(fn);
    11471177        ipc_answer_0(rid, rc);
    11481178}
Note: See TracChangeset for help on using the changeset viewer.