Ignore:
File:
1 edited

Legend:

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

    rc7bbf029 rebddd71  
    5959typedef struct {
    6060        link_t          link;
    61         devmap_handle_t devmap_handle;
     61        service_id_t    service_id;
    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         link_t          freed_head;
     69        list_t freed_list;
    7070} unused_t;
    7171
     
    7474
    7575/** List of unused structures. */
    76 static LIST_INITIALIZE(unused_head);
    77 
    78 static void unused_initialize(unused_t *u, devmap_handle_t devmap_handle)
     76static LIST_INITIALIZE(unused_list);
     77
     78static void unused_initialize(unused_t *u, service_id_t service_id)
    7979{
    8080        link_initialize(&u->link);
    81         u->devmap_handle = devmap_handle;
     81        u->service_id = service_id;
    8282        u->next = 0;
    8383        u->remaining = ((uint64_t)((fs_index_t)-1)) + 1;
    84         list_initialize(&u->freed_head);
    85 }
    86 
    87 static unused_t *unused_find(devmap_handle_t devmap_handle, bool lock)
     84        list_initialize(&u->freed_list);
     85}
     86
     87static unused_t *unused_find(service_id_t service_id, bool lock)
    8888{
    8989        unused_t *u;
    90         link_t *l;
    9190
    9291        if (lock)
    9392                fibril_mutex_lock(&unused_lock);
    94         for (l = unused_head.next; l != &unused_head; l = l->next) {
     93
     94        list_foreach(unused_list, l) {
    9595                u = list_get_instance(l, unused_t, link);
    96                 if (u->devmap_handle == devmap_handle)
     96                if (u->service_id == service_id)
    9797                        return u;
    9898        }
     99       
    99100        if (lock)
    100101                fibril_mutex_unlock(&unused_lock);
     
    107108/**
    108109 * Global hash table of all used fat_idx_t structures.
    109  * The index structures are hashed by the devmap_handle, parent node's first
     110 * The index structures are hashed by the service_id, parent node's first
    110111 * cluster and index within the parent directory.
    111112 */
     
    115116#define UPH_BUCKETS     (1 << UPH_BUCKETS_LOG)
    116117
    117 #define UPH_DH_KEY      0
     118#define UPH_SID_KEY     0
    118119#define UPH_PFC_KEY     1
    119120#define UPH_PDI_KEY     2
     
    121122static hash_index_t pos_hash(unsigned long key[])
    122123{
    123         devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
     124        service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
    124125        fat_cluster_t pfc = (fat_cluster_t)key[UPH_PFC_KEY];
    125126        unsigned pdi = (unsigned)key[UPH_PDI_KEY];
     
    141142        h |= (pdi & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
    142143            (UPH_BUCKETS_LOG / 2);
    143         h |= (devmap_handle & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
     144        h |= (service_id & ((1 << (UPH_BUCKETS_LOG / 4)) - 1)) <<
    144145            (3 * (UPH_BUCKETS_LOG / 4));
    145146
     
    149150static int pos_compare(unsigned long key[], hash_count_t keys, link_t *item)
    150151{
    151         devmap_handle_t devmap_handle = (devmap_handle_t)key[UPH_DH_KEY];
     152        service_id_t service_id = (service_id_t)key[UPH_SID_KEY];
    152153        fat_cluster_t pfc;
    153154        unsigned pdi;
     
    156157        switch (keys) {
    157158        case 1:
    158                 return (devmap_handle == fidx->devmap_handle);
     159                return (service_id == fidx->service_id);
    159160        case 3:
    160161                pfc = (fat_cluster_t) key[UPH_PFC_KEY];
    161162                pdi = (unsigned) key[UPH_PDI_KEY];
    162                 return (devmap_handle == fidx->devmap_handle) && (pfc == fidx->pfc) &&
     163                return (service_id == fidx->service_id) && (pfc == fidx->pfc) &&
    163164                    (pdi == fidx->pdi);
    164165        default:
     
    182183/**
    183184 * Global hash table of all used fat_idx_t structures.
    184  * The index structures are hashed by the devmap_handle and index.
     185 * The index structures are hashed by the service_id and index.
    185186 */
    186187static hash_table_t ui_hash;
     
    189190#define UIH_BUCKETS     (1 << UIH_BUCKETS_LOG)
    190191
    191 #define UIH_DH_KEY      0
     192#define UIH_SID_KEY     0
    192193#define UIH_INDEX_KEY   1
    193194
    194195static hash_index_t idx_hash(unsigned long key[])
    195196{
    196         devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
     197        service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
    197198        fs_index_t index = (fs_index_t)key[UIH_INDEX_KEY];
    198199
    199200        hash_index_t h;
    200201
    201         h = devmap_handle & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
     202        h = service_id & ((1 << (UIH_BUCKETS_LOG / 2)) - 1);
    202203        h |= (index & ((1 << (UIH_BUCKETS_LOG / 2)) - 1)) <<
    203204            (UIH_BUCKETS_LOG / 2);
     
    208209static int idx_compare(unsigned long key[], hash_count_t keys, link_t *item)
    209210{
    210         devmap_handle_t devmap_handle = (devmap_handle_t)key[UIH_DH_KEY];
     211        service_id_t service_id = (service_id_t)key[UIH_SID_KEY];
    211212        fs_index_t index;
    212213        fat_idx_t *fidx = list_get_instance(item, fat_idx_t, uih_link);
     
    214215        switch (keys) {
    215216        case 1:
    216                 return (devmap_handle == fidx->devmap_handle);
     217                return (service_id == fidx->service_id);
    217218        case 2:
    218219                index = (fs_index_t) key[UIH_INDEX_KEY];
    219                 return (devmap_handle == fidx->devmap_handle) &&
     220                return (service_id == fidx->service_id) &&
    220221                    (index == fidx->index);
    221222        default:
     
    240241
    241242/** Allocate a VFS index which is not currently in use. */
    242 static bool fat_index_alloc(devmap_handle_t devmap_handle, fs_index_t *index)
     243static bool fat_index_alloc(service_id_t service_id, fs_index_t *index)
    243244{
    244245        unused_t *u;
    245246       
    246247        assert(index);
    247         u = unused_find(devmap_handle, true);
     248        u = unused_find(service_id, true);
    248249        if (!u)
    249250                return false;   
    250251
    251         if (list_empty(&u->freed_head)) {
     252        if (list_empty(&u->freed_list)) {
    252253                if (u->remaining) {
    253254                        /*
     
    262263        } else {
    263264                /* There are some freed indices which we can reuse. */
    264                 freed_t *f = list_get_instance(u->freed_head.next, freed_t,
    265                     link);
     265                freed_t *f = list_get_instance(list_first(&u->freed_list),
     266                    freed_t, link);
    266267                *index = f->first;
    267268                if (f->first++ == f->last) {
     
    302303
    303304/** Free a VFS index, which is no longer in use. */
    304 static void fat_index_free(devmap_handle_t devmap_handle, fs_index_t index)
     305static void fat_index_free(service_id_t service_id, fs_index_t index)
    305306{
    306307        unused_t *u;
    307308
    308         u = unused_find(devmap_handle, true);
     309        u = unused_find(service_id, true);
    309310        assert(u);
    310311
     
    320321                link_t *lnk;
    321322                freed_t *n;
    322                 for (lnk = u->freed_head.next; lnk != &u->freed_head;
     323                for (lnk = u->freed_list.head.next; lnk != &u->freed_list.head;
    323324                    lnk = lnk->next) {
    324325                        freed_t *f = list_get_instance(lnk, freed_t, link);
    325326                        if (f->first == index + 1) {
    326327                                f->first--;
    327                                 if (lnk->prev != &u->freed_head)
     328                                if (lnk->prev != &u->freed_list.head)
    328329                                        try_coalesce_intervals(lnk->prev, lnk,
    329330                                            lnk);
     
    333334                        if (f->last == index - 1) {
    334335                                f->last++;
    335                                 if (lnk->next != &u->freed_head)
     336                                if (lnk->next != &u->freed_list.head)
    336337                                        try_coalesce_intervals(lnk, lnk->next,
    337338                                            lnk);
     
    359360                n->first = index;
    360361                n->last = index;
    361                 list_append(&n->link, &u->freed_head);
     362                list_append(&n->link, &u->freed_list);
    362363        }
    363364        fibril_mutex_unlock(&unused_lock);
    364365}
    365366
    366 static int fat_idx_create(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
     367static int fat_idx_create(fat_idx_t **fidxp, service_id_t service_id)
    367368{
    368369        fat_idx_t *fidx;
     
    371372        if (!fidx)
    372373                return ENOMEM;
    373         if (!fat_index_alloc(devmap_handle, &fidx->index)) {
     374        if (!fat_index_alloc(service_id, &fidx->index)) {
    374375                free(fidx);
    375376                return ENOSPC;
     
    379380        link_initialize(&fidx->uih_link);
    380381        fibril_mutex_initialize(&fidx->lock);
    381         fidx->devmap_handle = devmap_handle;
     382        fidx->service_id = service_id;
    382383        fidx->pfc = FAT_CLST_RES0;      /* no parent yet */
    383384        fidx->pdi = 0;
     
    388389}
    389390
    390 int fat_idx_get_new(fat_idx_t **fidxp, devmap_handle_t devmap_handle)
     391int fat_idx_get_new(fat_idx_t **fidxp, service_id_t service_id)
    391392{
    392393        fat_idx_t *fidx;
     
    394395
    395396        fibril_mutex_lock(&used_lock);
    396         rc = fat_idx_create(&fidx, devmap_handle);
     397        rc = fat_idx_create(&fidx, service_id);
    397398        if (rc != EOK) {
    398399                fibril_mutex_unlock(&used_lock);
     
    401402               
    402403        unsigned long ikey[] = {
    403                 [UIH_DH_KEY] = devmap_handle,
     404                [UIH_SID_KEY] = service_id,
    404405                [UIH_INDEX_KEY] = fidx->index,
    405406        };
     
    414415
    415416fat_idx_t *
    416 fat_idx_get_by_pos(devmap_handle_t devmap_handle, fat_cluster_t pfc, unsigned pdi)
     417fat_idx_get_by_pos(service_id_t service_id, fat_cluster_t pfc, unsigned pdi)
    417418{
    418419        fat_idx_t *fidx;
    419420        link_t *l;
    420421        unsigned long pkey[] = {
    421                 [UPH_DH_KEY] = devmap_handle,
     422                [UPH_SID_KEY] = service_id,
    422423                [UPH_PFC_KEY] = pfc,
    423424                [UPH_PDI_KEY] = pdi,
     
    431432                int rc;
    432433
    433                 rc = fat_idx_create(&fidx, devmap_handle);
     434                rc = fat_idx_create(&fidx, service_id);
    434435                if (rc != EOK) {
    435436                        fibril_mutex_unlock(&used_lock);
     
    438439               
    439440                unsigned long ikey[] = {
    440                         [UIH_DH_KEY] = devmap_handle,
     441                        [UIH_SID_KEY] = service_id,
    441442                        [UIH_INDEX_KEY] = fidx->index,
    442443                };
     
    457458{
    458459        unsigned long pkey[] = {
    459                 [UPH_DH_KEY] = idx->devmap_handle,
     460                [UPH_SID_KEY] = idx->service_id,
    460461                [UPH_PFC_KEY] = idx->pfc,
    461462                [UPH_PDI_KEY] = idx->pdi,
     
    470471{
    471472        unsigned long pkey[] = {
    472                 [UPH_DH_KEY] = idx->devmap_handle,
     473                [UPH_SID_KEY] = idx->service_id,
    473474                [UPH_PFC_KEY] = idx->pfc,
    474475                [UPH_PDI_KEY] = idx->pdi,
     
    481482
    482483fat_idx_t *
    483 fat_idx_get_by_index(devmap_handle_t devmap_handle, fs_index_t index)
     484fat_idx_get_by_index(service_id_t service_id, fs_index_t index)
    484485{
    485486        fat_idx_t *fidx = NULL;
    486487        link_t *l;
    487488        unsigned long ikey[] = {
    488                 [UIH_DH_KEY] = devmap_handle,
     489                [UIH_SID_KEY] = service_id,
    489490                [UIH_INDEX_KEY] = index,
    490491        };
     
    508509{
    509510        unsigned long ikey[] = {
    510                 [UIH_DH_KEY] = idx->devmap_handle,
     511                [UIH_SID_KEY] = idx->service_id,
    511512                [UIH_INDEX_KEY] = idx->index,
    512513        };
    513         devmap_handle_t devmap_handle = idx->devmap_handle;
     514        service_id_t service_id = idx->service_id;
    514515        fs_index_t index = idx->index;
    515516
     
    525526        fibril_mutex_unlock(&used_lock);
    526527        /* Release the VFS index. */
    527         fat_index_free(devmap_handle, index);
     528        fat_index_free(service_id, index);
    528529        /* The index structure itself is freed in idx_remove_callback(). */
    529530}
     
    547548}
    548549
    549 int fat_idx_init_by_devmap_handle(devmap_handle_t devmap_handle)
     550int fat_idx_init_by_service_id(service_id_t service_id)
    550551{
    551552        unused_t *u;
     
    555556        if (!u)
    556557                return ENOMEM;
    557         unused_initialize(u, devmap_handle);
     558        unused_initialize(u, service_id);
    558559        fibril_mutex_lock(&unused_lock);
    559         if (!unused_find(devmap_handle, false)) {
    560                 list_append(&u->link, &unused_head);
     560        if (!unused_find(service_id, false)) {
     561                list_append(&u->link, &unused_list);
    561562        } else {
    562563                free(u);
     
    567568}
    568569
    569 void fat_idx_fini_by_devmap_handle(devmap_handle_t devmap_handle)
     570void fat_idx_fini_by_service_id(service_id_t service_id)
    570571{
    571572        unsigned long ikey[] = {
    572                 [UIH_DH_KEY] = devmap_handle
     573                [UIH_SID_KEY] = service_id
    573574        };
    574575        unsigned long pkey[] = {
    575                 [UPH_DH_KEY] = devmap_handle
     576                [UPH_SID_KEY] = service_id
    576577        };
    577578
     
    589590         * Free the unused and freed structures for this instance.
    590591         */
    591         unused_t *u = unused_find(devmap_handle, true);
     592        unused_t *u = unused_find(service_id, true);
    592593        assert(u);
    593594        list_remove(&u->link);
    594595        fibril_mutex_unlock(&unused_lock);
    595596
    596         while (!list_empty(&u->freed_head)) {
     597        while (!list_empty(&u->freed_list)) {
    597598                freed_t *f;
    598                 f = list_get_instance(u->freed_head.next, freed_t, link);
     599                f = list_get_instance(list_first(&u->freed_list), freed_t, link);
    599600                list_remove(&f->link);
    600601                free(f);
Note: See TracChangeset for help on using the changeset viewer.