Ignore:
File:
1 edited

Legend:

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

    red903174 r1e4cada  
    11/*
    2  * Copyright (c) 2008 Jakub Jermar
    3  * Copyright (c) 2008 Martin Decky
     2 * Copyright (c) 2008 Jakub Jermar 
     3 * Copyright (c) 2008 Martin Decky 
    44 * All rights reserved.
    55 *
     
    5252#include <macros.h>
    5353#include <mem.h>
    54 #include <sys/typefmt.h>
    55 #include <stacktrace.h>
    5654
    5755/** Lock protecting the device connection list */
     
    8179        size_t comm_size;
    8280        void *bb_buf;
    83         aoff64_t bb_addr;
     81        bn_t bb_addr;
    8482        size_t pblock_size;             /**< Physical block size. */
    8583        cache_t *cache;
    8684} devcon_t;
    8785
    88 static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt);
    89 static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt);
     86static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt);
     87static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt);
    9088static int get_block_size(int dev_phone, size_t *bsize);
    91 static int get_num_blocks(int dev_phone, aoff64_t *nblocks);
    9289
    9390static devcon_t *devcon_search(dev_handle_t dev_handle)
     
    200197        assert(devcon);
    201198       
    202         if (devcon->cache)
    203                 (void) block_cache_fini(dev_handle);
    204 
    205199        devcon_remove(devcon);
    206200
     
    208202                free(devcon->bb_buf);
    209203
     204        if (devcon->cache) {
     205                hash_table_destroy(&devcon->cache->block_hash);
     206                free(devcon->cache);
     207        }
     208
    210209        munmap(devcon->comm_area, devcon->comm_size);
    211210        ipc_hangup(devcon->dev_phone);
     
    214213}
    215214
    216 int block_bb_read(dev_handle_t dev_handle, aoff64_t ba)
     215int block_bb_read(dev_handle_t dev_handle, bn_t ba)
    217216{
    218217        void *bb_buf;
     
    305304}
    306305
    307 int block_cache_fini(dev_handle_t dev_handle)
    308 {
    309         devcon_t *devcon = devcon_search(dev_handle);
    310         cache_t *cache;
    311         int rc;
    312 
    313         if (!devcon)
    314                 return ENOENT;
    315         if (!devcon->cache)
    316                 return EOK;
    317         cache = devcon->cache;
    318        
    319         /*
    320          * We are expecting to find all blocks for this device handle on the
    321          * free list, i.e. the block reference count should be zero. Do not
    322          * bother with the cache and block locks because we are single-threaded.
    323          */
    324         while (!list_empty(&cache->free_head)) {
    325                 block_t *b = list_get_instance(cache->free_head.next,
    326                     block_t, free_link);
    327 
    328                 list_remove(&b->free_link);
    329                 if (b->dirty) {
    330                         memcpy(devcon->comm_area, b->data, b->size);
    331                         rc = write_blocks(devcon, b->boff, 1);
    332                         if (rc != EOK)
    333                                 return rc;
    334                 }
    335 
    336                 unsigned long key = b->boff;
    337                 hash_table_remove(&cache->block_hash, &key, 1);
    338                
    339                 free(b->data);
    340                 free(b);
    341         }
    342 
    343         hash_table_destroy(&cache->block_hash);
    344         devcon->cache = NULL;
    345         free(cache);
    346 
    347         return EOK;
    348 }
    349 
    350306#define CACHE_LO_WATERMARK      10     
    351307#define CACHE_HI_WATERMARK      20     
     
    382338 * @return                      EOK on success or a negative error code.
    383339 */
    384 int block_get(block_t **block, dev_handle_t dev_handle, aoff64_t boff, int flags)
     340int block_get(block_t **block, dev_handle_t dev_handle, bn_t boff, int flags)
    385341{
    386342        devcon_t *devcon;
     
    657613 * @return              EOK on success or a negative return code on failure.
    658614 */
    659 int block_seqread(dev_handle_t dev_handle, size_t *bufpos, size_t *buflen,
    660     aoff64_t *pos, void *dst, size_t size)
    661 {
    662         size_t offset = 0;
     615int block_seqread(dev_handle_t dev_handle, off_t *bufpos, size_t *buflen,
     616    off_t *pos, void *dst, size_t size)
     617{
     618        off_t offset = 0;
    663619        size_t left = size;
    664620        size_t block_size;
     
    690646                }
    691647               
    692                 if (*bufpos == *buflen) {
     648                if (*bufpos == (off_t) *buflen) {
    693649                        /* Refill the communication buffer with a new block. */
    694650                        int rc;
     
    718674 * @return              EOK on success or negative error code on failure.
    719675 */
    720 int block_read_direct(dev_handle_t dev_handle, aoff64_t ba, size_t cnt, void *buf)
     676int block_read_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt, void *buf)
    721677{
    722678        devcon_t *devcon;
     
    746702 * @return              EOK on success or negative error code on failure.
    747703 */
    748 int block_write_direct(dev_handle_t dev_handle, aoff64_t ba, size_t cnt,
     704int block_write_direct(dev_handle_t dev_handle, bn_t ba, size_t cnt,
    749705    const void *data)
    750706{
     
    758714
    759715        memcpy(devcon->comm_area, data, devcon->pblock_size * cnt);
    760         rc = write_blocks(devcon, ba, cnt);
     716        rc = read_blocks(devcon, ba, cnt);
    761717
    762718        fibril_mutex_unlock(&devcon->comm_area_lock);
     
    780736       
    781737        return get_block_size(devcon->dev_phone, bsize);
    782 }
    783 
    784 /** Get number of blocks on device.
    785  *
    786  * @param dev_handle    Device handle of the block device.
    787  * @param nblocks       Output number of blocks.
    788  *
    789  * @return              EOK on success or negative error code on failure.
    790  */
    791 int block_get_nblocks(dev_handle_t dev_handle, aoff64_t *nblocks)
    792 {
    793         devcon_t *devcon;
    794 
    795         devcon = devcon_search(dev_handle);
    796         assert(devcon);
    797        
    798         return get_num_blocks(devcon->dev_phone, nblocks);
    799738}
    800739
     
    808747 * @return              EOK on success or negative error code on failure.
    809748 */
    810 static int read_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
     749static int read_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
    811750{
    812751        int rc;
     
    815754        rc = async_req_3_0(devcon->dev_phone, BD_READ_BLOCKS, LOWER32(ba),
    816755            UPPER32(ba), cnt);
    817         if (rc != EOK) {
    818                 printf("Error %d reading %d blocks starting at block %" PRIuOFF64
    819                     " from device handle %d\n", rc, cnt, ba,
    820                     devcon->dev_handle);
    821 #ifndef NDEBUG
    822                 stacktrace_print();
    823 #endif
    824         }
    825756        return rc;
    826757}
     
    835766 * @return              EOK on success or negative error code on failure.
    836767 */
    837 static int write_blocks(devcon_t *devcon, aoff64_t ba, size_t cnt)
     768static int write_blocks(devcon_t *devcon, bn_t ba, size_t cnt)
    838769{
    839770        int rc;
     
    842773        rc = async_req_3_0(devcon->dev_phone, BD_WRITE_BLOCKS, LOWER32(ba),
    843774            UPPER32(ba), cnt);
    844         if (rc != EOK) {
    845                 printf("Error %d writing %d blocks starting at block %" PRIuOFF64
    846                     " to device handle %d\n", rc, cnt, ba, devcon->dev_handle);
    847 #ifndef NDEBUG
    848                 stacktrace_print();
    849 #endif
    850         }
    851775        return rc;
    852776}
     
    865789}
    866790
    867 /** Get total number of blocks on block device. */
    868 static int get_num_blocks(int dev_phone, aoff64_t *nblocks)
    869 {
    870         ipcarg_t nb_l, nb_h;
    871         int rc;
    872 
    873         rc = async_req_0_2(dev_phone, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
    874         if (rc == EOK) {
    875                 *nblocks = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
    876         }
    877 
    878         return rc;
    879 }
    880 
    881791/** @}
    882792 */
Note: See TracChangeset for help on using the changeset viewer.