Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/block/libblock.c

    r08cba4b rb72efe8  
    3939#include "libblock.h"
    4040#include "../../srv/vfs/vfs.h"
    41 #include <ipc/loc.h>
     41#include <ipc/devmap.h>
    4242#include <ipc/bd.h>
    4343#include <ipc/services.h>
     
    7878typedef struct {
    7979        link_t link;
    80         service_id_t service_id;
     80        devmap_handle_t devmap_handle;
    8181        async_sess_t *sess;
    8282        fibril_mutex_t comm_area_lock;
     
    9595static aoff64_t ba_ltop(devcon_t *, aoff64_t);
    9696
    97 static devcon_t *devcon_search(service_id_t service_id)
     97static devcon_t *devcon_search(devmap_handle_t devmap_handle)
    9898{
    9999        fibril_mutex_lock(&dcl_lock);
     
    101101        list_foreach(dcl, cur) {
    102102                devcon_t *devcon = list_get_instance(cur, devcon_t, link);
    103                 if (devcon->service_id == service_id) {
     103                if (devcon->devmap_handle == devmap_handle) {
    104104                        fibril_mutex_unlock(&dcl_lock);
    105105                        return devcon;
     
    111111}
    112112
    113 static int devcon_add(service_id_t service_id, async_sess_t *sess,
     113static int devcon_add(devmap_handle_t devmap_handle, async_sess_t *sess,
    114114    size_t bsize, void *comm_area, size_t comm_size)
    115115{
     
    124124       
    125125        link_initialize(&devcon->link);
    126         devcon->service_id = service_id;
     126        devcon->devmap_handle = devmap_handle;
    127127        devcon->sess = sess;
    128128        fibril_mutex_initialize(&devcon->comm_area_lock);
     
    137137        list_foreach(dcl, cur) {
    138138                devcon_t *d = list_get_instance(cur, devcon_t, link);
    139                 if (d->service_id == service_id) {
     139                if (d->devmap_handle == devmap_handle) {
    140140                        fibril_mutex_unlock(&dcl_lock);
    141141                        free(devcon);
     
    155155}
    156156
    157 int block_init(exch_mgmt_t mgmt, service_id_t service_id,
     157int block_init(exch_mgmt_t mgmt, devmap_handle_t devmap_handle,
    158158    size_t comm_size)
    159159{
     
    163163                return ENOMEM;
    164164       
    165         async_sess_t *sess = loc_service_connect(mgmt, service_id,
     165        async_sess_t *sess = devmap_device_connect(mgmt, devmap_handle,
    166166            IPC_FLAG_BLOCKING);
    167167        if (!sess) {
     
    190190        }
    191191       
    192         rc = devcon_add(service_id, sess, bsize, comm_area, comm_size);
     192        rc = devcon_add(devmap_handle, sess, bsize, comm_area, comm_size);
    193193        if (rc != EOK) {
    194194                munmap(comm_area, comm_size);
     
    200200}
    201201
    202 void block_fini(service_id_t service_id)
    203 {
    204         devcon_t *devcon = devcon_search(service_id);
     202void block_fini(devmap_handle_t devmap_handle)
     203{
     204        devcon_t *devcon = devcon_search(devmap_handle);
    205205        assert(devcon);
    206206       
    207207        if (devcon->cache)
    208                 (void) block_cache_fini(service_id);
     208                (void) block_cache_fini(devmap_handle);
    209209       
    210210        devcon_remove(devcon);
     
    219219}
    220220
    221 int block_bb_read(service_id_t service_id, aoff64_t ba)
     221int block_bb_read(devmap_handle_t devmap_handle, aoff64_t ba)
    222222{
    223223        void *bb_buf;
    224224        int rc;
    225225
    226         devcon_t *devcon = devcon_search(service_id);
     226        devcon_t *devcon = devcon_search(devmap_handle);
    227227        if (!devcon)
    228228                return ENOENT;
     
    249249}
    250250
    251 void *block_bb_get(service_id_t service_id)
    252 {
    253         devcon_t *devcon = devcon_search(service_id);
     251void *block_bb_get(devmap_handle_t devmap_handle)
     252{
     253        devcon_t *devcon = devcon_search(devmap_handle);
    254254        assert(devcon);
    255255        return devcon->bb_buf;
     
    258258static hash_index_t cache_hash(unsigned long *key)
    259259{
    260         return MERGE_LOUP32(key[0], key[1]) & (CACHE_BUCKETS - 1);
     260        return *key & (CACHE_BUCKETS - 1);
    261261}
    262262
     
    264264{
    265265        block_t *b = hash_table_get_instance(item, block_t, hash_link);
    266         return b->lba == MERGE_LOUP32(key[0], key[1]);
     266        return b->lba == *key;
    267267}
    268268
     
    277277};
    278278
    279 int block_cache_init(service_id_t service_id, size_t size, unsigned blocks,
     279int block_cache_init(devmap_handle_t devmap_handle, size_t size, unsigned blocks,
    280280    enum cache_mode mode)
    281281{
    282         devcon_t *devcon = devcon_search(service_id);
     282        devcon_t *devcon = devcon_search(devmap_handle);
    283283        cache_t *cache;
    284284        if (!devcon)
     
    305305        cache->blocks_cluster = cache->lblock_size / devcon->pblock_size;
    306306
    307         if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 2,
     307        if (!hash_table_create(&cache->block_hash, CACHE_BUCKETS, 1,
    308308            &cache_ops)) {
    309309                free(cache);
     
    315315}
    316316
    317 int block_cache_fini(service_id_t service_id)
    318 {
    319         devcon_t *devcon = devcon_search(service_id);
     317int block_cache_fini(devmap_handle_t devmap_handle)
     318{
     319        devcon_t *devcon = devcon_search(devmap_handle);
    320320        cache_t *cache;
    321321        int rc;
     
    344344                }
    345345
    346                 unsigned long key[2] = {
    347                         LOWER32(b->lba),
    348                         UPPER32(b->lba)
    349                 };
    350                 hash_table_remove(&cache->block_hash, key, 2);
     346                unsigned long key = b->lba;
     347                hash_table_remove(&cache->block_hash, &key, 1);
    351348               
    352349                free(b->data);
     
    387384 * @param block                 Pointer to where the function will store the
    388385 *                              block pointer on success.
    389  * @param service_id            Service ID of the block device.
     386 * @param devmap_handle         Device handle of the block device.
    390387 * @param ba                    Block address (logical).
    391388 * @param flags                 If BLOCK_FLAGS_NOREAD is specified, block_get()
     
    395392 * @return                      EOK on success or a negative error code.
    396393 */
    397 int block_get(block_t **block, service_id_t service_id, aoff64_t ba, int flags)
     394int block_get(block_t **block, devmap_handle_t devmap_handle, aoff64_t ba, int flags)
    398395{
    399396        devcon_t *devcon;
     
    401398        block_t *b;
    402399        link_t *l;
    403         unsigned long key[2] = {
    404                 LOWER32(ba),
    405                 UPPER32(ba)
    406         };
    407 
     400        unsigned long key = ba;
    408401        int rc;
    409402       
    410         devcon = devcon_search(service_id);
     403        devcon = devcon_search(devmap_handle);
    411404
    412405        assert(devcon);
     
    420413
    421414        fibril_mutex_lock(&cache->lock);
    422         l = hash_table_find(&cache->block_hash, key);
     415        l = hash_table_find(&cache->block_hash, &key);
    423416        if (l) {
    424417found:
     
    458451                         * Try to recycle a block from the free list.
    459452                         */
     453                        unsigned long temp_key;
    460454recycle:
    461455                        if (list_empty(&cache->free_list)) {
     
    505499                                        goto retry;
    506500                                }
    507                                 l = hash_table_find(&cache->block_hash, key);
     501                                l = hash_table_find(&cache->block_hash, &key);
    508502                                if (l) {
    509503                                        /*
     
    528522                         */
    529523                        list_remove(&b->free_link);
    530                         unsigned long temp_key[2] = {
    531                                 LOWER32(b->lba),
    532                                 UPPER32(b->lba)
    533                         };
    534                         hash_table_remove(&cache->block_hash, temp_key, 2);
     524                        temp_key = b->lba;
     525                        hash_table_remove(&cache->block_hash, &temp_key, 1);
    535526                }
    536527
    537528                block_initialize(b);
    538                 b->service_id = service_id;
     529                b->devmap_handle = devmap_handle;
    539530                b->size = cache->lblock_size;
    540531                b->lba = ba;
    541532                b->pba = ba_ltop(devcon, b->lba);
    542                 hash_table_insert(&cache->block_hash, key, &b->hash_link);
     533                hash_table_insert(&cache->block_hash, &key, &b->hash_link);
    543534
    544535                /*
     
    586577int block_put(block_t *block)
    587578{
    588         devcon_t *devcon = devcon_search(block->service_id);
     579        devcon_t *devcon = devcon_search(block->devmap_handle);
    589580        cache_t *cache;
    590581        unsigned blocks_cached;
     
    652643                         * Take the block out of the cache and free it.
    653644                         */
    654                         unsigned long key[2] = {
    655                                 LOWER32(block->lba),
    656                                 UPPER32(block->lba)
    657                         };
    658                         hash_table_remove(&cache->block_hash, key, 2);
     645                        unsigned long key = block->lba;
     646                        hash_table_remove(&cache->block_hash, &key, 1);
    659647                        fibril_mutex_unlock(&block->lock);
    660648                        free(block->data);
     
    687675/** Read sequential data from a block device.
    688676 *
    689  * @param service_id    Service ID of the block device.
     677 * @param devmap_handle Device handle of the block device.
    690678 * @param bufpos        Pointer to the first unread valid offset within the
    691679 *                      communication buffer.
     
    699687 * @return              EOK on success or a negative return code on failure.
    700688 */
    701 int block_seqread(service_id_t service_id, size_t *bufpos, size_t *buflen,
     689int block_seqread(devmap_handle_t devmap_handle, size_t *bufpos, size_t *buflen,
    702690    aoff64_t *pos, void *dst, size_t size)
    703691{
     
    707695        devcon_t *devcon;
    708696
    709         devcon = devcon_search(service_id);
     697        devcon = devcon_search(devmap_handle);
    710698        assert(devcon);
    711699        block_size = devcon->pblock_size;
     
    753741/** Read blocks directly from device (bypass cache).
    754742 *
    755  * @param service_id    Service ID of the block device.
     743 * @param devmap_handle Device handle of the block device.
    756744 * @param ba            Address of first block (physical).
    757745 * @param cnt           Number of blocks.
     
    760748 * @return              EOK on success or negative error code on failure.
    761749 */
    762 int block_read_direct(service_id_t service_id, aoff64_t ba, size_t cnt, void *buf)
     750int block_read_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt, void *buf)
    763751{
    764752        devcon_t *devcon;
    765753        int rc;
    766754
    767         devcon = devcon_search(service_id);
     755        devcon = devcon_search(devmap_handle);
    768756        assert(devcon);
    769757       
     
    781769/** Write blocks directly to device (bypass cache).
    782770 *
    783  * @param service_id    Service ID of the block device.
     771 * @param devmap_handle Device handle of the block device.
    784772 * @param ba            Address of first block (physical).
    785773 * @param cnt           Number of blocks.
     
    788776 * @return              EOK on success or negative error code on failure.
    789777 */
    790 int block_write_direct(service_id_t service_id, aoff64_t ba, size_t cnt,
     778int block_write_direct(devmap_handle_t devmap_handle, aoff64_t ba, size_t cnt,
    791779    const void *data)
    792780{
     
    794782        int rc;
    795783
    796         devcon = devcon_search(service_id);
     784        devcon = devcon_search(devmap_handle);
    797785        assert(devcon);
    798786       
     
    809797/** Get device block size.
    810798 *
    811  * @param service_id    Service ID of the block device.
     799 * @param devmap_handle Device handle of the block device.
    812800 * @param bsize         Output block size.
    813801 *
    814802 * @return              EOK on success or negative error code on failure.
    815803 */
    816 int block_get_bsize(service_id_t service_id, size_t *bsize)
     804int block_get_bsize(devmap_handle_t devmap_handle, size_t *bsize)
    817805{
    818806        devcon_t *devcon;
    819807
    820         devcon = devcon_search(service_id);
     808        devcon = devcon_search(devmap_handle);
    821809        assert(devcon);
    822810       
     
    826814/** Get number of blocks on device.
    827815 *
    828  * @param service_id    Service ID of the block device.
     816 * @param devmap_handle Device handle of the block device.
    829817 * @param nblocks       Output number of blocks.
    830818 *
    831819 * @return              EOK on success or negative error code on failure.
    832820 */
    833 int block_get_nblocks(service_id_t service_id, aoff64_t *nblocks)
    834 {
    835         devcon_t *devcon = devcon_search(service_id);
     821int block_get_nblocks(devmap_handle_t devmap_handle, aoff64_t *nblocks)
     822{
     823        devcon_t *devcon = devcon_search(devmap_handle);
    836824        assert(devcon);
    837825       
     
    841829/** Read bytes directly from the device (bypass cache)
    842830 *
    843  * @param service_id    Service ID of the block device.
     831 * @param devmap_handle Device handle of the block device.
    844832 * @param abs_offset    Absolute offset in bytes where to start reading
    845833 * @param bytes                 Number of bytes to read
     
    848836 * @return              EOK on success or negative error code on failure.
    849837 */
    850 int block_read_bytes_direct(service_id_t service_id, aoff64_t abs_offset,
     838int block_read_bytes_direct(devmap_handle_t devmap_handle, aoff64_t abs_offset,
    851839    size_t bytes, void *data)
    852840{
     
    860848        size_t offset;
    861849       
    862         rc = block_get_bsize(service_id, &phys_block_size);
     850        rc = block_get_bsize(devmap_handle, &phys_block_size);
    863851        if (rc != EOK) {
    864852                return rc;
     
    878866        }
    879867       
    880         rc = block_read_direct(service_id, first_block, blocks, buffer);
     868        rc = block_read_direct(devmap_handle, first_block, blocks, buffer);
    881869        if (rc != EOK) {
    882870                free(buffer);
     
    889877       
    890878        return EOK;
    891 }
    892 
    893 /** Get TOC from device.
    894  *
    895  * @param service_id Service ID of the block device.
    896  * @param session    Starting session.
    897  *
    898  * @return Allocated TOC structure.
    899  * @return NULL on failure.
    900  *
    901  */
    902 toc_block_t *block_get_toc(service_id_t service_id, uint8_t session)
    903 {
    904         devcon_t *devcon = devcon_search(service_id);
    905         assert(devcon);
    906        
    907         toc_block_t *toc = NULL;
    908        
    909         fibril_mutex_lock(&devcon->comm_area_lock);
    910        
    911         async_exch_t *exch = async_exchange_begin(devcon->sess);
    912         int rc = async_req_1_0(exch, BD_READ_TOC, session);
    913         async_exchange_end(exch);
    914        
    915         if (rc == EOK) {
    916                 toc = (toc_block_t *) malloc(sizeof(toc_block_t));
    917                 if (toc != NULL) {
    918                         memset(toc, 0, sizeof(toc_block_t));
    919                         memcpy(toc, devcon->comm_area,
    920                             min(devcon->pblock_size, sizeof(toc_block_t)));
    921                 }
    922         }
    923        
    924        
    925         fibril_mutex_unlock(&devcon->comm_area_lock);
    926        
    927         return toc;
    928879}
    929880
     
    949900                printf("Error %d reading %zu blocks starting at block %" PRIuOFF64
    950901                    " from device handle %" PRIun "\n", rc, cnt, ba,
    951                     devcon->service_id);
     902                    devcon->devmap_handle);
    952903#ifndef NDEBUG
    953904                stacktrace_print();
     
    978929        if (rc != EOK) {
    979930                printf("Error %d writing %zu blocks starting at block %" PRIuOFF64
    980                     " to device handle %" PRIun "\n", rc, cnt, ba, devcon->service_id);
     931                    " to device handle %" PRIun "\n", rc, cnt, ba, devcon->devmap_handle);
    981932#ifndef NDEBUG
    982933                stacktrace_print();
Note: See TracChangeset for help on using the changeset viewer.