Ignore:
File:
1 edited

Legend:

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

    r991f645 rca093b3  
    3939#include "../../vfs/vfs.h"
    4040#include <errno.h>
    41 #include <str.h>
     41#include <string.h>
    4242#include <adt/hash_table.h>
    4343#include <adt/list.h>
    4444#include <assert.h>
    45 #include <fibril_synch.h>
     45#include <fibril_sync.h>
    4646
    4747/** Each instance of this type describes one interval of freed VFS indices. */
     
    5858typedef struct {
    5959        link_t          link;
    60         devmap_handle_t devmap_handle;
     60        dev_handle_t    dev_handle;
    6161
    6262        /** Next unassigned index. */
     
    7575static LIST_INITIALIZE(unused_head);
    7676
    77 static void unused_initialize(unused_t *u, devmap_handle_t devmap_handle)
     77static void unused_initialize(unused_t *u, dev_handle_t dev_handle)
    7878{
    7979        link_initialize(&u->link);
    80         u->devmap_handle = devmap_handle;
     80        u->dev_handle = dev_handle;
    8181        u->next = 0;
    8282        u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
     
    8484}
    8585
    86 static unused_t *unused_find(devmap_handle_t devmap_handle, bool lock)
     86static unused_t *unused_find(dev_handle_t dev_handle, bool lock)
    8787{
    8888        unused_t *u;
     
    9393        for (l = unused_head.next; l != &unused_head; l = l->next) {
    9494                u = list_get_instance(l, unused_t, link);
    95                 if (u->devmap_handle == devmap_handle)
     95                if (u->dev_handle == dev_handle)
    9696                        return u;
    9797        }
     
    106106/**
    107107 * Global hash table of all used fat_idx_t structures.
    108  * The index structures are hashed by the devmap_handle, parent node's first
     108 * The index structures are hashed by the dev_handle, parent node's first
    109109 * cluster and index within the parent directory.
    110110 */
     
    120120static hash_index_t pos_hash(unsigned long key[])
    121121{
    122         devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
     122        dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
    123123        fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
    124124        unsigned pdi = (unsigned)key[UPH_PDI_KEY];
     
    140140        h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
    141141            (UPH_BUCKETS_LOG / 2);
    142         h |= (devmap_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
     142        h |= (dev_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
    143143            (3 * (UPH_BUCKETS_LOG / 4));
    144144
     
    148148static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
    149149{
    150         devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
    151         fat_cluster_t pfc;
    152         unsigned pdi;
     150        dev_handle_t dev_handle = (dev_handle_t)key[UPH_DH_KEY];
     151        fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
     152        unsigned pdi = (unsigned)key[UPH_PDI_KEY];
    153153        fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uph_link);
    154154
    155         switch (keys) {
    156         case 1:
    157                 return (devmap_handle == fidx->devmap_handle);
    158         case 3:
    159                 pfc = (fat_cluster_t) key[UPH_PFC_KEY];
    160                 pdi = (unsigned) key[UPH_PDI_KEY];
    161                 return (devmap_handle == fidx->devmap_handle) && (pfc == fidx->pfc) &&
    162                     (pdi == fidx->pdi);
    163         default:
    164                 assert((keys == 1) || (keys == 3));
    165         }
    166 
    167         return 0;
     155        return (dev_handle == fidx->dev_handle) && (pfc == fidx->pfc) &&
     156            (pdi == fidx->pdi);
    168157}
    169158
     
    181170/**
    182171 * Global hash table of all used fat_idx_t structures.
    183  * The index structures are hashed by the devmap_handle and index.
     172 * The index structures are hashed by the dev_handle and index.
    184173 */
    185174static hash_table_t ui_hash;
     
    193182static hash_index_t idx_hash(unsigned long key[])
    194183{
    195         devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
     184        dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
    196185        fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
    197186
    198187        hash_index_t h;
    199188
    200         h = devmap_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
     189        h = dev_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
    201190        h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
    202191            (UIH_BUCKETS_LOG / 2);
     
    207196static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
    208197{
    209         devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
    210         fs_index_t index;
     198        dev_handle_t dev_handle = (dev_handle_t)key[UIH_DH_KEY];
     199        fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
    211200        fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
    212201
    213         switch (keys) {
    214         case 1:
    215                 return (devmap_handle == fidx->devmap_handle);
    216         case 2:
    217                 index = (fs_index_t) key[UIH_INDEX_KEY];
    218                 return (devmap_handle == fidx->devmap_handle) &&
    219                     (index == fidx->index);
    220         default:
    221                 assert((keys == 1) || (keys == 2));
    222         }
    223 
    224         return 0;
     202        return (dev_handle == fidx->dev_handle) && (index == fidx->index);
    225203}
    226204
    227205static void idx_remove_callback(link_t *item)
    228206{
    229         fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
    230 
    231         free(fidx);
     207        /* nothing to do */
    232208}
    233209
     
    239215
    240216/** Allocate a VFS index which is not currently in use. */
    241 static bool fat_index_alloc(devmap_handle_t devmap_handle, fs_index_t *index)
     217static bool fat_index_alloc(dev_handle_t dev_handle, fs_index_t *index)
    242218{
    243219        unused_t *u;
    244220       
    245221        assert(index);
    246         u = unused_find(devmap_handle, true);
     222        u = unused_find(dev_handle, true);
    247223        if (!u)
    248224                return false;   
     
    301277
    302278/** Free a VFS index, which is no longer in use. */
    303 static void fat_index_free(devmap_handle_t devmap_handle, fs_index_t index)
     279static void fat_index_free(dev_handle_t dev_handle, fs_index_t index)
    304280{
    305281        unused_t *u;
    306282
    307         u = unused_find(devmap_handle, true);
     283        u = unused_find(dev_handle, true);
    308284        assert(u);
    309285
     
    363339}
    364340
    365 static int fat_idx_create(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
     341static fat_idx_t *fat_idx_create(dev_handle_t dev_handle)
    366342{
    367343        fat_idx_t *fidx;
     
    369345        fidx = (fat_idx_t *) malloc(sizeof(fat_idx_t));
    370346        if (!fidx)
    371                 return ENOMEM;
    372         if (!fat_index_alloc(devmap_handle, &fidx->index)) {
     347                return NULL;
     348        if (!fat_index_alloc(dev_handle, &fidx->index)) {
    373349                free(fidx);
    374                 return ENOSPC;
     350                return NULL;
    375351        }
    376352               
     
    378354        link_initialize(&fidx->uih_link);
    379355        fibril_mutex_initialize(&fidx->lock);
    380         fidx->devmap_handle = devmap_handle;
     356        fidx->dev_handle = dev_handle;
    381357        fidx->pfc = FAT_CLST_RES0;      /* no parent yet */
    382358        fidx->pdi = 0;
    383359        fidx->nodep = NULL;
    384360
    385         *fidxp = fidx;
    386         return EOK;
    387 }
    388 
    389 int fat_idx_get_new(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
     361        return fidx;
     362}
     363
     364fat_idx_t *fat_idx_get_new(dev_handle_t dev_handle)
    390365{
    391366        fat_idx_t *fidx;
    392         int rc;
    393367
    394368        fibril_mutex_lock(&used_lock);
    395         rc = fat_idx_create(&fidx, devmap_handle);
    396         if (rc != EOK) {
     369        fidx = fat_idx_create(dev_handle);
     370        if (!fidx) {
    397371                fibril_mutex_unlock(&used_lock);
    398                 return rc;
     372                return NULL;
    399373        }
    400374               
    401375        unsigned long ikey[] = {
    402                 [UIH_DH_KEY] = devmap_handle,
     376                [UIH_DH_KEY] = dev_handle,
    403377                [UIH_INDEX_KEY] = fidx->index,
    404378        };
     
    408382        fibril_mutex_unlock(&used_lock);
    409383
    410         *fidxp = fidx;
    411         return EOK;
     384        return fidx;
    412385}
    413386
    414387fat_idx_t *
    415 fat_idx_get_by_pos(devmap_handle_t devmap_handle, fat_cluster_t pfc, unsigned pdi)
     388fat_idx_get_by_pos(dev_handle_t dev_handle, fat_cluster_t pfc, unsigned pdi)
    416389{
    417390        fat_idx_t *fidx;
    418391        link_t *l;
    419392        unsigned long pkey[] = {
    420                 [UPH_DH_KEY] = devmap_handle,
     393                [UPH_DH_KEY] = dev_handle,
    421394                [UPH_PFC_KEY] = pfc,
    422395                [UPH_PDI_KEY] = pdi,
     
    428401                fidx = hash_table_get_instance(l, fat_idx_t, uph_link);
    429402        } else {
    430                 int rc;
    431 
    432                 rc = fat_idx_create(&fidx, devmap_handle);
    433                 if (rc != EOK) {
     403                fidx = fat_idx_create(dev_handle);
     404                if (!fidx) {
    434405                        fibril_mutex_unlock(&used_lock);
    435406                        return NULL;
     
    437408               
    438409                unsigned long ikey[] = {
    439                         [UIH_DH_KEY] = devmap_handle,
     410                        [UIH_DH_KEY] = dev_handle,
    440411                        [UIH_INDEX_KEY] = fidx->index,
    441412                };
     
    456427{
    457428        unsigned long pkey[] = {
    458                 [UPH_DH_KEY] = idx->devmap_handle,
     429                [UPH_DH_KEY] = idx->dev_handle,
    459430                [UPH_PFC_KEY] = idx->pfc,
    460431                [UPH_PDI_KEY] = idx->pdi,
     
    469440{
    470441        unsigned long pkey[] = {
    471                 [UPH_DH_KEY] = idx->devmap_handle,
     442                [UPH_DH_KEY] = idx->dev_handle,
    472443                [UPH_PFC_KEY] = idx->pfc,
    473444                [UPH_PDI_KEY] = idx->pdi,
     
    480451
    481452fat_idx_t *
    482 fat_idx_get_by_index(devmap_handle_t devmap_handle, fs_index_t index)
     453fat_idx_get_by_index(dev_handle_t dev_handle, fs_index_t index)
    483454{
    484455        fat_idx_t *fidx = NULL;
    485456        link_t *l;
    486457        unsigned long ikey[] = {
    487                 [UIH_DH_KEY] = devmap_handle,
     458                [UIH_DH_KEY] = dev_handle,
    488459                [UIH_INDEX_KEY] = index,
    489460        };
     
    507478{
    508479        unsigned long ikey[] = {
    509                 [UIH_DH_KEY] = idx->devmap_handle,
     480                [UIH_DH_KEY] = idx->dev_handle,
    510481                [UIH_INDEX_KEY] = idx->index,
    511482        };
    512         devmap_handle_t devmap_handle = idx->devmap_handle;
    513         fs_index_t index = idx->index;
    514483
    515484        assert(idx->pfc == FAT_CLST_RES0);
     
    524493        fibril_mutex_unlock(&used_lock);
    525494        /* Release the VFS index. */
    526         fat_index_free(devmap_handle, index);
    527         /* The index structure itself is freed in idx_remove_callback(). */
     495        fat_index_free(idx->dev_handle, idx->index);
     496        /* Deallocate the structure. */
     497        free(idx);
    528498}
    529499
     
    546516}
    547517
    548 int fat_idx_init_by_devmap_handle(devmap_handle_t devmap_handle)
     518int fat_idx_init_by_dev_handle(dev_handle_t dev_handle)
    549519{
    550520        unused_t *u;
     
    554524        if (!u)
    555525                return ENOMEM;
    556         unused_initialize(u, devmap_handle);
     526        unused_initialize(u, dev_handle);
    557527        fibril_mutex_lock(&unused_lock);
    558         if (!unused_find(devmap_handle, false)) {
     528        if (!unused_find(dev_handle, false))
    559529                list_append(&u->link, &unused_head);
    560         } else {
    561                 free(u);
     530        else
    562531                rc = EEXIST;
    563         }
    564532        fibril_mutex_unlock(&unused_lock);
    565533        return rc;
    566534}
    567535
    568 void fat_idx_fini_by_devmap_handle(devmap_handle_t devmap_handle)
    569 {
    570         unsigned long ikey[] = {
    571                 [UIH_DH_KEY] = devmap_handle
    572         };
    573         unsigned long pkey[] = {
    574                 [UPH_DH_KEY] = devmap_handle
    575         };
    576 
    577         /*
    578          * Remove this instance's index structure from up_hash and ui_hash.
    579          * Process up_hash first and ui_hash second because the index structure
    580          * is actually removed in idx_remove_callback().
    581          */
    582         fibril_mutex_lock(&used_lock);
    583         hash_table_remove(&up_hash, pkey, 1);
    584         hash_table_remove(&ui_hash, ikey, 1);
    585         fibril_mutex_unlock(&used_lock);
    586 
    587         /*
    588          * Free the unused and freed structures for this instance.
    589          */
    590         unused_t *u = unused_find(devmap_handle, true);
     536void fat_idx_fini_by_dev_handle(dev_handle_t dev_handle)
     537{
     538        unused_t *u;
     539
     540        u = unused_find(dev_handle, true);
    591541        assert(u);
    592542        list_remove(&u->link);
Note: See TracChangeset for help on using the changeset viewer.