Changeset 4c53333 in mainline for uspace/srv/bd/file_bd/file_bd.c


Ignore:
Timestamp:
2013-07-11T08:21:10Z (13 years ago)
Author:
Maurizio Lombardi <m.lombardi85@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
64e63ce1
Parents:
80445cf (diff), c8bb1633 (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:

merge mainline changes

File:
1 edited

Legend:

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

    r80445cf r4c53333  
    4141#include <stdio.h>
    4242#include <unistd.h>
    43 #include <ipc/bd.h>
    4443#include <async.h>
    4544#include <as.h>
     45#include <bd_srv.h>
    4646#include <fibril_synch.h>
    4747#include <loc.h>
     
    4949#include <sys/typefmt.h>
    5050#include <errno.h>
    51 #include <bool.h>
     51#include <stdbool.h>
    5252#include <task.h>
    5353#include <macros.h>
     
    6262
    6363static service_id_t service_id;
     64static bd_srvs_t bd_srvs;
    6465static fibril_mutex_t dev_lock;
    6566
     
    6768static int file_bd_init(const char *fname);
    6869static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
    69 static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
    70 static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
     70
     71static int file_bd_open(bd_srvs_t *, bd_srv_t *);
     72static int file_bd_close(bd_srv_t *);
     73static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
     74static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
     75static int file_bd_get_block_size(bd_srv_t *, size_t *);
     76static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
     77
     78static bd_ops_t file_bd_ops = {
     79        .open = file_bd_open,
     80        .close = file_bd_close,
     81        .read_blocks = file_bd_read_blocks,
     82        .write_blocks = file_bd_write_blocks,
     83        .get_block_size = file_bd_get_block_size,
     84        .get_num_blocks = file_bd_get_num_blocks
     85};
    7186
    7287int main(int argc, char **argv)
     
    119134        rc = loc_service_register(device_name, &service_id);
    120135        if (rc != EOK) {
    121                 printf(NAME ": Unable to register device '%s'.\n",
    122                         device_name);
     136                printf("%s: Unable to register device '%s'.\n",
     137                    NAME, device_name);
    123138                return rc;
    124139        }
    125 
    126         printf(NAME ": Accepting connections\n");
     140       
     141        printf("%s: Accepting connections\n", NAME);
    127142        task_retval(0);
    128143        async_manager();
    129 
     144       
    130145        /* Not reached */
    131146        return 0;
     
    139154static int file_bd_init(const char *fname)
    140155{
    141         int rc;
    142         long img_size;
     156        bd_srvs_init(&bd_srvs);
     157        bd_srvs.ops = &file_bd_ops;
    143158       
    144159        async_set_client_connection(file_bd_connection);
    145         rc = loc_server_register(NAME);
    146         if (rc < 0) {
    147                 printf(NAME ": Unable to register driver.\n");
     160        int rc = loc_server_register(NAME);
     161        if (rc != EOK) {
     162                printf("%s: Unable to register driver.\n", NAME);
    148163                return rc;
    149164        }
    150 
     165       
    151166        img = fopen(fname, "rb+");
    152167        if (img == NULL)
    153168                return EINVAL;
    154 
     169       
    155170        if (fseek(img, 0, SEEK_END) != 0) {
    156171                fclose(img);
    157172                return EIO;
    158173        }
    159 
    160         img_size = ftell(img);
     174       
     175        off64_t img_size = ftell(img);
    161176        if (img_size < 0) {
    162177                fclose(img);
    163178                return EIO;
    164179        }
    165 
     180       
    166181        num_blocks = img_size / block_size;
    167 
     182       
    168183        fibril_mutex_initialize(&dev_lock);
    169 
     184       
    170185        return EOK;
    171186}
     
    173188static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    174189{
    175         void *fs_va = NULL;
    176         ipc_callid_t callid;
    177         ipc_call_t call;
    178         sysarg_t method;
    179         size_t comm_size;
    180         unsigned int flags;
    181         int retval;
    182         uint64_t ba;
    183         size_t cnt;
    184 
    185         /* Answer the IPC_M_CONNECT_ME_TO call. */
    186         async_answer_0(iid, EOK);
    187 
    188         if (!async_share_out_receive(&callid, &comm_size, &flags)) {
    189                 async_answer_0(callid, EHANGUP);
    190                 return;
    191         }
    192 
    193         (void) async_share_out_finalize(callid, &fs_va);
    194         if (fs_va == (void *) -1) {
    195                 async_answer_0(callid, EHANGUP);
    196                 return;
    197         }
    198 
    199         while (true) {
    200                 callid = async_get_call(&call);
    201                 method = IPC_GET_IMETHOD(call);
    202                
    203                 if (!method) {
    204                         /* The other side has hung up. */
    205                         async_answer_0(callid, EOK);
    206                         return;
    207                 }
    208                
    209                 switch (method) {
    210                 case BD_READ_BLOCKS:
    211                         ba = MERGE_LOUP32(IPC_GET_ARG1(call),
    212                             IPC_GET_ARG2(call));
    213                         cnt = IPC_GET_ARG3(call);
    214                         if (cnt * block_size > comm_size) {
    215                                 retval = ELIMIT;
    216                                 break;
    217                         }
    218                         retval = file_bd_read_blocks(ba, cnt, fs_va);
    219                         break;
    220                 case BD_WRITE_BLOCKS:
    221                         ba = MERGE_LOUP32(IPC_GET_ARG1(call),
    222                             IPC_GET_ARG2(call));
    223                         cnt = IPC_GET_ARG3(call);
    224                         if (cnt * block_size > comm_size) {
    225                                 retval = ELIMIT;
    226                                 break;
    227                         }
    228                         retval = file_bd_write_blocks(ba, cnt, fs_va);
    229                         break;
    230                 case BD_GET_BLOCK_SIZE:
    231                         async_answer_1(callid, EOK, block_size);
    232                         continue;
    233                 case BD_GET_NUM_BLOCKS:
    234                         async_answer_2(callid, EOK, LOWER32(num_blocks),
    235                             UPPER32(num_blocks));
    236                         continue;
    237                 default:
    238                         retval = EINVAL;
    239                         break;
    240                 }
    241                 async_answer_0(callid, retval);
    242         }
     190        bd_conn(iid, icall, &bd_srvs);
     191}
     192
     193/** Open device. */
     194static int file_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
     195{
     196        return EOK;
     197}
     198
     199/** Close device. */
     200static int file_bd_close(bd_srv_t *bd)
     201{
     202        return EOK;
    243203}
    244204
    245205/** Read blocks from the device. */
    246 static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
     206static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
     207    size_t size)
    247208{
    248209        size_t n_rd;
    249210        int rc;
     211
     212        if (size < cnt * block_size)
     213                return EINVAL;
    250214
    251215        /* Check whether access is within device address bounds. */
     
    282246
    283247/** Write blocks to the device. */
    284 static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
     248static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
     249    const void *buf, size_t size)
    285250{
    286251        size_t n_wr;
    287252        int rc;
     253
     254        if (size < cnt * block_size)
     255                return EINVAL;
    288256
    289257        /* Check whether access is within device address bounds. */
     
    321289}
    322290
     291/** Get device block size. */
     292static int file_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
     293{
     294        *rsize = block_size;
     295        return EOK;
     296}
     297
     298/** Get number of blocks on device. */
     299static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
     300{
     301        *rnb = num_blocks;
     302        return EOK;
     303}
     304
    323305/**
    324306 * @}
Note: See TracChangeset for help on using the changeset viewer.