Changeset 1787e527 in mainline for uspace/srv/bd/file_bd/file_bd.c


Ignore:
Timestamp:
2009-11-16T21:22:54Z (14 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5ebdf94
Parents:
fcbd1be (diff), 9c70ed6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merged with head (unstable)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/bd/file_bd/file_bd.c

    rfcbd1be r1787e527  
    5151#include <bool.h>
    5252#include <task.h>
     53#include <macros.h>
    5354
    5455#define NAME "file_bd"
    5556
    56 static size_t comm_size;
     57static const size_t block_size = 512;
    5758static FILE *img;
    5859
     
    6263static int file_bd_init(const char *fname);
    6364static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall);
    64 static int file_bd_read(off_t blk_idx, size_t size, void *buf);
    65 static int file_bd_write(off_t blk_idx, size_t size, void *buf);
     65static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
     66static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
    6667
    6768int main(int argc, char **argv)
     
    120121        ipc_call_t call;
    121122        ipcarg_t method;
     123        size_t comm_size;
    122124        int flags;
    123125        int retval;
    124         off_t idx;
    125         size_t size;
     126        uint64_t ba;
     127        size_t cnt;
    126128
    127129        /* Answer the IPC_M_CONNECT_ME_TO call. */
    128130        ipc_answer_0(iid, EOK);
    129131
    130         if (!ipc_share_out_receive(&callid, &comm_size, &flags)) {
     132        if (!async_share_out_receive(&callid, &comm_size, &flags)) {
    131133                ipc_answer_0(callid, EHANGUP);
    132134                return;
     
    139141        }
    140142
    141         (void) ipc_share_out_finalize(callid, fs_va);
     143        (void) async_share_out_finalize(callid, fs_va);
    142144
    143145        while (1) {
     
    149151                        ipc_answer_0(callid, EOK);
    150152                        return;
    151                 case BD_READ_BLOCK:
    152                 case BD_WRITE_BLOCK:
    153                         idx = IPC_GET_ARG1(call);
    154                         size = IPC_GET_ARG2(call);
    155                         if (size > comm_size) {
    156                                 retval = EINVAL;
     153                case BD_READ_BLOCKS:
     154                        ba = MERGE_LOUP32(IPC_GET_ARG1(call),
     155                            IPC_GET_ARG2(call));
     156                        cnt = IPC_GET_ARG3(call);
     157                        if (cnt * block_size > comm_size) {
     158                                retval = ELIMIT;
    157159                                break;
    158160                        }
    159                         if (method == BD_READ_BLOCK)
    160                                 retval = file_bd_read(idx, size, fs_va);
    161                         else
    162                                 retval = file_bd_write(idx, size, fs_va);
     161                        retval = file_bd_read_blocks(ba, cnt, fs_va);
    163162                        break;
     163                case BD_WRITE_BLOCKS:
     164                        ba = MERGE_LOUP32(IPC_GET_ARG1(call),
     165                            IPC_GET_ARG2(call));
     166                        cnt = IPC_GET_ARG3(call);
     167                        if (cnt * block_size > comm_size) {
     168                                retval = ELIMIT;
     169                                break;
     170                        }
     171                        retval = file_bd_write_blocks(ba, cnt, fs_va);
     172                        break;
     173                case BD_GET_BLOCK_SIZE:
     174                        ipc_answer_1(callid, EOK, block_size);
     175                        continue;
    164176                default:
    165177                        retval = EINVAL;
     
    170182}
    171183
    172 static int file_bd_read(off_t blk_idx, size_t size, void *buf)
     184/** Read blocks from the device. */
     185static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
    173186{
    174187        size_t n_rd;
     
    176189        fibril_mutex_lock(&dev_lock);
    177190
    178         fseek(img, blk_idx * size, SEEK_SET);
    179         n_rd = fread(buf, 1, size, img);
     191        fseek(img, ba * block_size, SEEK_SET);
     192        n_rd = fread(buf, block_size, cnt, img);
    180193
    181194        if (ferror(img)) {
     
    186199        fibril_mutex_unlock(&dev_lock);
    187200
    188         if (n_rd < size)
    189                 return EINVAL;  /* Read beyond end of disk */
     201        if (n_rd < cnt)
     202                return EINVAL;  /* Read beyond end of device */
    190203
    191204        return EOK;
    192205}
    193206
    194 static int file_bd_write(off_t blk_idx, size_t size, void *buf)
     207/** Write blocks to the device. */
     208static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
    195209{
    196210        size_t n_wr;
     
    198212        fibril_mutex_lock(&dev_lock);
    199213
    200         fseek(img, blk_idx * size, SEEK_SET);
    201         n_wr = fread(buf, 1, size, img);
    202 
    203         if (ferror(img) || n_wr < size) {
     214        fseek(img, ba * block_size, SEEK_SET);
     215        n_wr = fread(buf, block_size, cnt, img);
     216
     217        if (ferror(img) || n_wr < cnt) {
    204218                fibril_mutex_unlock(&dev_lock);
    205219                return EIO;     /* Write error */
Note: See TracChangeset for help on using the changeset viewer.