Ignore:
File:
1 edited

Legend:

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

    r4802dd7 rfaba839  
    4141#include <stdio.h>
    4242#include <unistd.h>
     43#include <ipc/bd.h>
    4344#include <async.h>
    4445#include <as.h>
    45 #include <bd_srv.h>
    4646#include <fibril_synch.h>
    4747#include <loc.h>
     
    6262
    6363static service_id_t service_id;
    64 static bd_srv_t bd_srv;
    6564static fibril_mutex_t dev_lock;
    6665
     
    6867static int file_bd_init(const char *fname);
    6968static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *);
    70 
    71 static int file_bd_open(bd_srv_t *);
    72 static int file_bd_close(bd_srv_t *);
    73 static int file_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
    74 static int file_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
    75 static int file_bd_get_block_size(bd_srv_t *, size_t *);
    76 static int file_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
    77 
    78 static 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 };
     69static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf);
     70static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
    8671
    8772int main(int argc, char **argv)
     
    154139static int file_bd_init(const char *fname)
    155140{
    156         bd_srv_init(&bd_srv);
    157         bd_srv.ops = &file_bd_ops;
    158        
    159141        async_set_client_connection(file_bd_connection);
    160142        int rc = loc_server_register(NAME);
     
    188170static void file_bd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    189171{
    190         bd_conn(iid, icall, &bd_srv);
    191 }
    192 
    193 /** Open device. */
    194 static int file_bd_open(bd_srv_t *bd)
    195 {
    196         return EOK;
    197 }
    198 
    199 /** Close device. */
    200 static int file_bd_close(bd_srv_t *bd)
    201 {
    202         return EOK;
     172        void *fs_va = NULL;
     173        ipc_callid_t callid;
     174        ipc_call_t call;
     175        sysarg_t method;
     176        size_t comm_size;
     177        unsigned int flags;
     178        int retval;
     179        uint64_t ba;
     180        size_t cnt;
     181
     182        /* Answer the IPC_M_CONNECT_ME_TO call. */
     183        async_answer_0(iid, EOK);
     184
     185        if (!async_share_out_receive(&callid, &comm_size, &flags)) {
     186                async_answer_0(callid, EHANGUP);
     187                return;
     188        }
     189
     190        (void) async_share_out_finalize(callid, &fs_va);
     191        if (fs_va == AS_MAP_FAILED) {
     192                async_answer_0(callid, EHANGUP);
     193                return;
     194        }
     195
     196        while (true) {
     197                callid = async_get_call(&call);
     198                method = IPC_GET_IMETHOD(call);
     199               
     200                if (!method) {
     201                        /* The other side has hung up. */
     202                        async_answer_0(callid, EOK);
     203                        return;
     204                }
     205               
     206                switch (method) {
     207                case BD_READ_BLOCKS:
     208                        ba = MERGE_LOUP32(IPC_GET_ARG1(call),
     209                            IPC_GET_ARG2(call));
     210                        cnt = IPC_GET_ARG3(call);
     211                        if (cnt * block_size > comm_size) {
     212                                retval = ELIMIT;
     213                                break;
     214                        }
     215                        retval = file_bd_read_blocks(ba, cnt, fs_va);
     216                        break;
     217                case BD_WRITE_BLOCKS:
     218                        ba = MERGE_LOUP32(IPC_GET_ARG1(call),
     219                            IPC_GET_ARG2(call));
     220                        cnt = IPC_GET_ARG3(call);
     221                        if (cnt * block_size > comm_size) {
     222                                retval = ELIMIT;
     223                                break;
     224                        }
     225                        retval = file_bd_write_blocks(ba, cnt, fs_va);
     226                        break;
     227                case BD_GET_BLOCK_SIZE:
     228                        async_answer_1(callid, EOK, block_size);
     229                        continue;
     230                case BD_GET_NUM_BLOCKS:
     231                        async_answer_2(callid, EOK, LOWER32(num_blocks),
     232                            UPPER32(num_blocks));
     233                        continue;
     234                default:
     235                        retval = EINVAL;
     236                        break;
     237                }
     238                async_answer_0(callid, retval);
     239        }
    203240}
    204241
    205242/** Read blocks from the device. */
    206 static int file_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
    207     size_t size)
     243static int file_bd_read_blocks(uint64_t ba, size_t cnt, void *buf)
    208244{
    209245        size_t n_rd;
    210246        int rc;
    211 
    212         if (size < cnt * block_size)
    213                 return EINVAL;
    214247
    215248        /* Check whether access is within device address bounds. */
     
    246279
    247280/** Write blocks to the device. */
    248 static int file_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
    249     const void *buf, size_t size)
     281static int file_bd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
    250282{
    251283        size_t n_wr;
    252284        int rc;
    253 
    254         if (size < cnt * block_size)
    255                 return EINVAL;
    256285
    257286        /* Check whether access is within device address bounds. */
     
    289318}
    290319
    291 /** Get device block size. */
    292 static 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. */
    299 static int file_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
    300 {
    301         *rnb = num_blocks;
    302         return EOK;
    303 }
    304 
    305320/**
    306321 * @}
Note: See TracChangeset for help on using the changeset viewer.