Ignore:
File:
1 edited

Legend:

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

    rebddd71 rc7bbf029  
    5959typedef struct {
    6060        link_t          link;
    61         service_id_t    service_id;
     61        devmap_handle_t devmap_handle;
    6262
    6363        /** Next unassigned index. */
    64         fs_index_t next;
     64        fs_index_t      next;
    6565        /** Number of remaining unassigned indices. */
    66         uint64_t remaining;
     66        uint64_t        remaining;
    6767
    6868        /** Sorted list of intervals of freed indices. */
    69         list_t freed_list;
     69        link_t          freed_head;
    7070} unused_t;
    7171
     
    7474
    7575/** List of unused structures. */
    76 static LIST_INITIALIZE(unused_list);
    77 
    78 static void unused_initialize(unused_t *u, service_id_t service_id)
     76static LIST_INITIALIZE(unused_head);
     77
     78static void unused_initialize(unused_t *u, devmap_handle_t devmap_handle)
    7979{
    8080        link_initialize(&u->link);
    81         u->service_id = service_id;
     81        u->devmap_handle = devmap_handle;
    8282        u->next = 0;
    8383        u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
    84         list_initialize(&u->freed_list);
    85 }
    86 
    87 static unused_t *unused_find(service_id_t service_id, bool lock)
     84        list_initialize(&u->freed_head);
     85}
     86
     87static unused_t *unused_find(devmap_handle_t devmap_handle, bool lock)
    8888{
    8989        unused_t *u;
     90        link_t *l;
    9091
    9192        if (lock)
    9293                fibril_mutex_lock(&unused_lock);
    93 
    94         list_foreach(unused_list, l) {
     94        for (l = unused_head.next; l != &unused_head; l = l->next) {
    9595                u = list_get_instance(l, unused_t, link);
    96                 if (u->service_id == service_id)
     96                if (u->devmap_handle == devmap_handle)
    9797                        return u;
    9898        }
    99        
    10099        if (lock)
    101100                fibril_mutex_unlock(&unused_lock);
     
    108107/**
    109108 * Global hash table of all used fat_idx_t structures.
    110  * The index structures are hashed by the service_id, parent node's first
     109 * The index structures are hashed by the devmap_handle, parent node's first
    111110 * cluster and index within the parent directory.
    112111 */
     
    116115#define UPH_BUCKETS     (1 << UPH_BUCKETS_LOG)
    117116
    118 #define UPH_SID_KEY     0
     117#define UPH_DH_KEY      0
    119118#define UPH_PFC_KEY     1
    120119#define UPH_PDI_KEY     2
     
    122121static hash_index_t pos_hash(unsigned long key[])
    123122{
    124         service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
     123        devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
    125124        fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
    126125        unsigned pdi = (unsigned)key[UPH_PDI_KEY];
     
    142141        h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
    143142            (UPH_BUCKETS_LOG / 2);
    144         h |= (service_id & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
     143        h |= (devmap_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
    145144            (3 * (UPH_BUCKETS_LOG / 4));
    146145
     
    150149static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
    151150{
    152         service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
     151        devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
    153152        fat_cluster_t pfc;
    154153        unsigned pdi;
     
    157156        switch (keys) {
    158157        case 1:
    159                 return (service_id == fidx->service_id);
     158                return (devmap_handle == fidx->devmap_handle);
    160159        case 3:
    161160                pfc = (fat_cluster_t) key[UPH_PFC_KEY];
    162161                pdi = (unsigned) key[UPH_PDI_KEY];
    163                 return (service_id == fidx->service_id) && (pfc == fidx->pfc) &&
     162                return (devmap_handle == fidx->devmap_handle) && (pfc == fidx->pfc) &&
    164163                    (pdi == fidx->pdi);
    165164        default:
     
    183182/**
    184183 * Global hash table of all used fat_idx_t structures.
    185  * The index structures are hashed by the service_id and index.
     184 * The index structures are hashed by the devmap_handle and index.
    186185 */
    187186static hash_table_t ui_hash;
     
    190189#define UIH_BUCKETS     (1 << UIH_BUCKETS_LOG)
    191190
    192 #define UIH_SID_KEY     0
     191#define UIH_DH_KEY      0
    193192#define UIH_INDEX_KEY   1
    194193
    195194static hash_index_t idx_hash(unsigned long key[])
    196195{
    197         service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
     196        devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
    198197        fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
    199198
    200199        hash_index_t h;
    201200
    202         h = service_id & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
     201        h = devmap_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
    203202        h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
    204203            (UIH_BUCKETS_LOG / 2);
     
    209208static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
    210209{
    211         service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
     210        devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
    212211        fs_index_t index;
    213212        fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
     
    215214        switch (keys) {
    216215        case 1:
    217                 return (service_id == fidx->service_id);
     216                return (devmap_handle == fidx->devmap_handle);
    218217        case 2:
    219218                index = (fs_index_t) key[UIH_INDEX_KEY];
    220                 return (service_id == fidx->service_id) &&
     219                return (devmap_handle == fidx->devmap_handle) &&
    221220                    (index == fidx->index);
    222221        default:
     
    241240
    242241/** Allocate a VFS index which is not currently in use. */
    243 static bool fat_index_alloc(service_id_t service_id, fs_index_t *index)
     242static bool fat_index_alloc(devmap_handle_t devmap_handle, fs_index_t *index)
    244243{
    245244        unused_t *u;
    246245       
    247246        assert(index);
    248         u = unused_find(service_id, true);
     247        u = unused_find(devmap_handle, true);
    249248        if (!u)
    250249                return false;   
    251250
    252         if (list_empty(&u->freed_list)) {
     251        if (list_empty(&u->freed_head)) {
    253252                if (u->remaining) {
    254253                        /*
     
    263262        } else {
    264263                /* There are some freed indices which we can reuse. */
    265                 freed_t *f = list_get_instance(list_first(&u->freed_list),
    266                     freed_t, link);
     264                freed_t *f = list_get_instance(u->freed_head.next, freed_t,
     265                    link);
    267266                *index = f->first;
    268267                if (f->first++ == f->last) {
     
    303302
    304303/** Free a VFS index, which is no longer in use. */
    305 static void fat_index_free(service_id_t service_id, fs_index_t index)
     304static void fat_index_free(devmap_handle_t devmap_handle, fs_index_t index)
    306305{
    307306        unused_t *u;
    308307
    309         u = unused_find(service_id, true);
     308        u = unused_find(devmap_handle, true);
    310309        assert(u);
    311310
     
    321320                link_t *lnk;
    322321                freed_t *n;
    323                 for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
     322                for (lnk = u->freed_head.next; lnk != &u->freed_head;
    324323                    lnk = lnk->next) {
    325324                        freed_t *f = list_get_instance(lnk, freed_t, link);
    326325                        if (f->first == index + 1) {
    327326                                f->first--;
    328                                 if (lnk->prev != &u->freed_list.head)
     327                                if (lnk->prev != &u->freed_head)
    329328                                        try_coalesce_intervals(lnk->prev, lnk,
    330329                                            lnk);
     
    334333                        if (f->last == index - 1) {
    335334                                f->last++;
    336                                 if (lnk->next != &u->freed_list.head)
     335                                if (lnk->next != &u->freed_head)
    337336                                        try_coalesce_intervals(lnk, lnk->next,
    338337                                            lnk);
     
    360359                n->first = index;
    361360                n->last = index;
    362                 list_append(&n->link, &u->freed_list);
     361                list_append(&n->link, &u->freed_head);
    363362        }
    364363        fibril_mutex_unlock(&unused_lock);
    365364}
    366365
    367 static int fat_idx_create(fat_idx_t **fidxp, service_id_t service_id)
     366static int fat_idx_create(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
    368367{
    369368        fat_idx_t *fidx;
     
    372371        if (!fidx)
    373372                return ENOMEM;
    374         if (!fat_index_alloc(service_id, &fidx->index)) {
     373        if (!fat_index_alloc(devmap_handle, &fidx->index)) {
    375374                free(fidx);
    376375                return ENOSPC;
     
    380379        link_initialize(&fidx->uih_link);
    381380        fibril_mutex_initialize(&fidx->lock);
    382         fidx->service_id = service_id;
     381        fidx->devmap_handle = devmap_handle;
    383382        fidx->pfc = FAT_CLST_RES0;      /* no parent yet */
    384383        fidx->pdi = 0;
     
    389388}
    390389
    391 int fat_idx_get_new(fat_idx_t **fidxp, service_id_t service_id)
     390int fat_idx_get_new(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
    392391{
    393392        fat_idx_t *fidx;
     
    395394
    396395        fibril_mutex_lock(&used_lock);
    397         rc = fat_idx_create(&fidx, service_id);
     396        rc = fat_idx_create(&fidx, devmap_handle);
    398397        if (rc != EOK) {
    399398                fibril_mutex_unlock(&used_lock);
     
    402401               
    403402        unsigned long ikey[] = {
    404                 [UIH_SID_KEY] = service_id,
     403                [UIH_DH_KEY] = devmap_handle,
    405404                [UIH_INDEX_KEY] = fidx->index,
    406405        };
     
    415414
    416415fat_idx_t *
    417 fat_idx_get_by_pos(service_id_t service_id, fat_cluster_t pfc, unsigned pdi)
     416fat_idx_get_by_pos(devmap_handle_t devmap_handle, fat_cluster_t pfc, unsigned pdi)
    418417{
    419418        fat_idx_t *fidx;
    420419        link_t *l;
    421420        unsigned long pkey[] = {
    422                 [UPH_SID_KEY] = service_id,
     421                [UPH_DH_KEY] = devmap_handle,
    423422                [UPH_PFC_KEY] = pfc,
    424423                [UPH_PDI_KEY] = pdi,
     
    432431                int rc;
    433432
    434                 rc = fat_idx_create(&fidx, service_id);
     433                rc = fat_idx_create(&fidx, devmap_handle);
    435434                if (rc != EOK) {
    436435                        fibril_mutex_unlock(&used_lock);
     
    439438               
    440439                unsigned long ikey[] = {
    441                         [UIH_SID_KEY] = service_id,
     440                        [UIH_DH_KEY] = devmap_handle,
    442441                        [UIH_INDEX_KEY] = fidx->index,
    443442                };
     
    458457{
    459458        unsigned long pkey[] = {
    460                 [UPH_SID_KEY] = idx->service_id,
     459                [UPH_DH_KEY] = idx->devmap_handle,
    461460                [UPH_PFC_KEY] = idx->pfc,
    462461                [UPH_PDI_KEY] = idx->pdi,
     
    471470{
    472471        unsigned long pkey[] = {
    473                 [UPH_SID_KEY] = idx->service_id,
     472                [UPH_DH_KEY] = idx->devmap_handle,
    474473                [UPH_PFC_KEY] = idx->pfc,
    475474                [UPH_PDI_KEY] = idx->pdi,
     
    482481
    483482fat_idx_t *
    484 fat_idx_get_by_index(service_id_t service_id, fs_index_t index)
     483fat_idx_get_by_index(devmap_handle_t devmap_handle, fs_index_t index)
    485484{
    486485        fat_idx_t *fidx = NULL;
    487486        link_t *l;
    488487        unsigned long ikey[] = {
    489                 [UIH_SID_KEY] = service_id,
     488                [UIH_DH_KEY] = devmap_handle,
    490489                [UIH_INDEX_KEY] = index,
    491490        };
     
    509508{
    510509        unsigned long ikey[] = {
    511                 [UIH_SID_KEY] = idx->service_id,
     510                [UIH_DH_KEY] = idx->devmap_handle,
    512511                [UIH_INDEX_KEY] = idx->index,
    513512        };
    514         service_id_t service_id = idx->service_id;
     513        devmap_handle_t devmap_handle = idx->devmap_handle;
    515514        fs_index_t index = idx->index;
    516515
     
    526525        fibril_mutex_unlock(&used_lock);
    527526        /* Release the VFS index. */
    528         fat_index_free(service_id, index);
     527        fat_index_free(devmap_handle, index);
    529528        /* The index structure itself is freed in idx_remove_callback(). */
    530529}
     
    548547}
    549548
    550 int fat_idx_init_by_service_id(service_id_t service_id)
     549int fat_idx_init_by_devmap_handle(devmap_handle_t devmap_handle)
    551550{
    552551        unused_t *u;
     
    556555        if (!u)
    557556                return ENOMEM;
    558         unused_initialize(u, service_id);
     557        unused_initialize(u, devmap_handle);
    559558        fibril_mutex_lock(&unused_lock);
    560         if (!unused_find(service_id, false)) {
    561                 list_append(&u->link, &unused_list);
     559        if (!unused_find(devmap_handle, false)) {
     560                list_append(&u->link, &unused_head);
    562561        } else {
    563562                free(u);
     
    568567}
    569568
    570 void fat_idx_fini_by_service_id(service_id_t service_id)
     569void fat_idx_fini_by_devmap_handle(devmap_handle_t devmap_handle)
    571570{
    572571        unsigned long ikey[] = {
    573                 [UIH_SID_KEY] = service_id
     572                [UIH_DH_KEY] = devmap_handle
    574573        };
    575574        unsigned long pkey[] = {
    576                 [UPH_SID_KEY] = service_id
     575                [UPH_DH_KEY] = devmap_handle
    577576        };
    578577
     
    590589         * Free the unused and freed structures for this instance.
    591590         */
    592         unused_t *u = unused_find(service_id, true);
     591        unused_t *u = unused_find(devmap_handle, true);
    593592        assert(u);
    594593        list_remove(&u->link);
    595594        fibril_mutex_unlock(&unused_lock);
    596595
    597         while (!list_empty(&u->freed_list)) {
     596        while (!list_empty(&u->freed_head)) {
    598597                freed_t *f;
    599                 f = list_get_instance(list_first(&u->freed_list), freed_t, link);
     598                f = list_get_instance(u->freed_head.next, freed_t, link);
    600599                list_remove(&f->link);
    601600                free(f);
Note: See TracChangeset for help on using the changeset viewer.