Changeset 4802dd7 in mainline for uspace/srv/bd/part


Ignore:
Timestamp:
2012-08-13T22:01:04Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
135486d, 71b0d4d4, 9dec6d4, cddcc4a3
Parents:
4820360
Message:

Factor out client and server IPC stubs for block devices.

Location:
uspace/srv/bd/part
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/bd/part/guid_part/guid_part.c

    r4820360 r4802dd7  
    4747#include <stdlib.h>
    4848#include <unistd.h>
    49 #include <ipc/bd.h>
    5049#include <async.h>
    5150#include <as.h>
     51#include <bd_srv.h>
    5252#include <fibril_synch.h>
    5353#include <loc.h>
     
    8383        /** Service representing the partition (outbound device) */
    8484        service_id_t dsid;
     85        /** Block device server structure */
     86        bd_srv_t bd;
    8587        /** Points to next partition structure. */
    8688        struct part *next;
     
    100102static void gpt_pte_to_part(const gpt_entry_t *pte, part_t *part);
    101103static void gpt_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
    102 static int gpt_bd_read(part_t *p, aoff64_t ba, size_t cnt, void *buf);
    103 static int gpt_bd_write(part_t *p, aoff64_t ba, size_t cnt, const void *buf);
    104104static int gpt_bsa_translate(part_t *p, aoff64_t ba, size_t cnt, aoff64_t *gba);
     105
     106static int gpt_bd_open(bd_srv_t *);
     107static int gpt_bd_close(bd_srv_t *);
     108static int gpt_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
     109static int gpt_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
     110static int gpt_bd_get_block_size(bd_srv_t *, size_t *);
     111static int gpt_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
     112
     113static bd_ops_t gpt_bd_ops = {
     114        .open = gpt_bd_open,
     115        .close = gpt_bd_close,
     116        .read_blocks = gpt_bd_read_blocks,
     117        .write_blocks = gpt_bd_write_blocks,
     118        .get_block_size = gpt_bd_get_block_size,
     119        .get_num_blocks = gpt_bd_get_num_blocks
     120};
     121
     122static part_t *bd_srv_part(bd_srv_t *bd)
     123{
     124        return (part_t *)bd->arg;
     125}
    105126
    106127int main(int argc, char **argv)
     
    304325        }
    305326
     327        bd_srv_init(&part->bd);
     328        part->bd.ops = &gpt_bd_ops;
     329        part->bd.arg = part;
     330
    306331        part->dsid = 0;
    307332        part->next = NULL;
     
    310335static void gpt_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    311336{
    312         size_t comm_size;
    313         void *fs_va = NULL;
    314         ipc_callid_t callid;
    315         ipc_call_t call;
    316         sysarg_t method;
    317337        service_id_t dh;
    318         unsigned int flags;
    319         int retval;
    320         aoff64_t ba;
    321         size_t cnt;
    322338        part_t *part;
    323339
     
    341357        assert(part->present == true);
    342358
    343         /* Answer the IPC_M_CONNECT_ME_TO call. */
    344         async_answer_0(iid, EOK);
    345 
    346         if (!async_share_out_receive(&callid, &comm_size, &flags)) {
    347                 async_answer_0(callid, EHANGUP);
    348                 return;
    349         }
    350 
    351         (void) async_share_out_finalize(callid, &fs_va);
    352         if (fs_va == AS_MAP_FAILED) {
    353                 async_answer_0(callid, EHANGUP);
    354                 return;
    355         }
    356 
    357         while (true) {
    358                 callid = async_get_call(&call);
    359                 method = IPC_GET_IMETHOD(call);
    360                
    361                 if (!method) {
    362                         /* The other side has hung up. */
    363                         async_answer_0(callid, EOK);
    364                         return;
    365                 }
    366                
    367                 switch (method) {
    368                 case BD_READ_BLOCKS:
    369                         ba = MERGE_LOUP32(IPC_GET_ARG1(call),
    370                             IPC_GET_ARG2(call));
    371                         cnt = IPC_GET_ARG3(call);
    372                         if (cnt * block_size > comm_size) {
    373                                 retval = ELIMIT;
    374                                 break;
    375                         }
    376                         retval = gpt_bd_read(part, ba, cnt, fs_va);
    377                         break;
    378                 case BD_WRITE_BLOCKS:
    379                         ba = MERGE_LOUP32(IPC_GET_ARG1(call),
    380                             IPC_GET_ARG2(call));
    381                         cnt = IPC_GET_ARG3(call);
    382                         if (cnt * block_size > comm_size) {
    383                                 retval = ELIMIT;
    384                                 break;
    385                         }
    386                         retval = gpt_bd_write(part, ba, cnt, fs_va);
    387                         break;
    388                 case BD_GET_BLOCK_SIZE:
    389                         async_answer_1(callid, EOK, block_size);
    390                         continue;
    391                 case BD_GET_NUM_BLOCKS:
    392                         async_answer_2(callid, EOK, LOWER32(part->length),
    393                             UPPER32(part->length));
    394                         continue;
    395                 default:
    396                         retval = EINVAL;
    397                         break;
    398                 }
    399                 async_answer_0(callid, retval);
    400         }
     359        bd_conn(iid, icall, &part->bd);
     360}
     361
     362/** Open device. */
     363static int gpt_bd_open(bd_srv_t *bd)
     364{
     365        return EOK;
     366}
     367
     368/** Close device. */
     369static int gpt_bd_close(bd_srv_t *bd)
     370{
     371        return EOK;
    401372}
    402373
    403374/** Read blocks from partition. */
    404 static int gpt_bd_read(part_t *p, aoff64_t ba, size_t cnt, void *buf)
    405 {
     375static int gpt_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
     376    size_t size)
     377{
     378        part_t *p = bd_srv_part(bd);
    406379        aoff64_t gba;
     380
     381        if (cnt * block_size < size)
     382                return EINVAL;
    407383
    408384        if (gpt_bsa_translate(p, ba, cnt, &gba) != EOK)
     
    413389
    414390/** Write blocks to partition. */
    415 static int gpt_bd_write(part_t *p, aoff64_t ba, size_t cnt, const void *buf)
    416 {
     391static int gpt_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
     392    const void *buf, size_t size)
     393{
     394        part_t *p = bd_srv_part(bd);
    417395        aoff64_t gba;
     396
     397        if (cnt * block_size < size)
     398                return EINVAL;
    418399
    419400        if (gpt_bsa_translate(p, ba, cnt, &gba) != EOK)
     
    422403        return block_write_direct(indev_sid, gba, cnt, buf);
    423404}
     405
     406/** Get device block size. */
     407static int gpt_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
     408{
     409        *rsize = block_size;
     410        return EOK;
     411}
     412
     413/** Get number of blocks on device. */
     414static int gpt_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
     415{
     416        part_t *part = bd_srv_part(bd);
     417
     418        *rnb = part->length;
     419        return EOK;
     420}
     421
    424422
    425423/** Translate block segment address with range checking. */
  • uspace/srv/bd/part/mbr_part/mbr_part.c

    r4820360 r4802dd7  
    4444 *
    4545 * Referemces:
    46  *     
     46 *
    4747 * The source of MBR structures for this driver have been the following
    4848 * Wikipedia articles:
     
    5757#include <stdlib.h>
    5858#include <unistd.h>
    59 #include <ipc/bd.h>
    6059#include <async.h>
    6160#include <as.h>
     61#include <bd_srv.h>
    6262#include <fibril_synch.h>
    6363#include <loc.h>
     
    100100        /** Device representing the partition (outbound device) */
    101101        service_id_t dsid;
     102        /** Block device server structure */
     103        bd_srv_t bd;
    102104        /** Points to next partition structure. */
    103105        struct part *next;
     
    140142
    141143/** Partitioned device (inbound device) */
    142 static service_id_t indef_sid;
     144static service_id_t indev_sid;
    143145
    144146/** List of partitions. This structure is an empty head. */
     
    150152static void mbr_pte_to_part(uint32_t base, const pt_entry_t *pte, part_t *part);
    151153static void mbr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
    152 static int mbr_bd_read(part_t *p, uint64_t ba, size_t cnt, void *buf);
    153 static int mbr_bd_write(part_t *p, uint64_t ba, size_t cnt, const void *buf);
    154154static int mbr_bsa_translate(part_t *p, uint64_t ba, size_t cnt, uint64_t *gba);
     155
     156static int mbr_bd_open(bd_srv_t *);
     157static int mbr_bd_close(bd_srv_t *);
     158static int mbr_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
     159static int mbr_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
     160static int mbr_bd_get_block_size(bd_srv_t *, size_t *);
     161static int mbr_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
     162
     163static bd_ops_t mbr_bd_ops = {
     164        .open = mbr_bd_open,
     165        .close = mbr_bd_close,
     166        .read_blocks = mbr_bd_read_blocks,
     167        .write_blocks = mbr_bd_write_blocks,
     168        .get_block_size = mbr_bd_get_block_size,
     169        .get_num_blocks = mbr_bd_get_num_blocks
     170};
     171
     172static part_t *bd_srv_part(bd_srv_t *bd)
     173{
     174        return (part_t *)bd->arg;
     175}
    155176
    156177int main(int argc, char **argv)
     
    183204        part_t *part;
    184205
    185         rc = loc_service_get_id(dev_name, &indef_sid, 0);
     206        rc = loc_service_get_id(dev_name, &indev_sid, 0);
    186207        if (rc != EOK) {
    187208                printf(NAME ": could not resolve device `%s'.\n", dev_name);
     
    189210        }
    190211
    191         rc = block_init(EXCHANGE_SERIALIZE, indef_sid, 2048);
     212        rc = block_init(EXCHANGE_SERIALIZE, indev_sid, 2048);
    192213        if (rc != EOK)  {
    193214                printf(NAME ": could not init libblock.\n");
     
    197218        /* Determine and verify block size. */
    198219
    199         rc = block_get_bsize(indef_sid, &block_size);
     220        rc = block_get_bsize(indev_sid, &block_size);
    200221        if (rc != EOK) {
    201222                printf(NAME ": error getting block size.\n");
     
    281302         */
    282303
    283         rc = block_read_direct(indef_sid, 0, 1, brb);
     304        rc = block_read_direct(indev_sid, 0, 1, brb);
    284305        if (rc != EOK) {
    285306                printf(NAME ": Failed reading MBR block.\n");
     
    332353                 */
    333354                ba = cp.start_addr;
    334                 rc = block_read_direct(indef_sid, ba, 1, brb);
     355                rc = block_read_direct(indev_sid, ba, 1, brb);
    335356                if (rc != EOK) {
    336357                        printf(NAME ": Failed reading EBR block at %"
     
    381402        part->present = (pte->ptype != PT_UNUSED) ? true : false;
    382403
     404        bd_srv_init(&part->bd);
     405        part->bd.ops = &mbr_bd_ops;
     406        part->bd.arg = part;
     407
    383408        part->dsid = 0;
    384409        part->next = NULL;
     
    387412static void mbr_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
    388413{
    389         size_t comm_size;
    390         void *fs_va = NULL;
    391         ipc_callid_t callid;
    392         ipc_call_t call;
    393         sysarg_t method;
    394414        service_id_t dh;
    395         unsigned int flags;
    396         int retval;
    397         uint64_t ba;
    398         size_t cnt;
    399415        part_t *part;
    400416
     
    417433
    418434        assert(part->present == true);
    419 
    420         /* Answer the IPC_M_CONNECT_ME_TO call. */
    421         async_answer_0(iid, EOK);
    422 
    423         if (!async_share_out_receive(&callid, &comm_size, &flags)) {
    424                 async_answer_0(callid, EHANGUP);
    425                 return;
    426         }
    427 
    428         (void) async_share_out_finalize(callid, &fs_va);
    429         if (fs_va == AS_MAP_FAILED) {
    430                 async_answer_0(callid, EHANGUP);
    431                 return;
    432         }
    433 
    434         while (1) {
    435                 callid = async_get_call(&call);
    436                 method = IPC_GET_IMETHOD(call);
    437                
    438                 if (!method) {
    439                         /* The other side has hung up. */
    440                         async_answer_0(callid, EOK);
    441                         return;
    442                 }
    443                
    444                 switch (method) {
    445                 case BD_READ_BLOCKS:
    446                         ba = MERGE_LOUP32(IPC_GET_ARG1(call),
    447                             IPC_GET_ARG2(call));
    448                         cnt = IPC_GET_ARG3(call);
    449                         if (cnt * block_size > comm_size) {
    450                                 retval = ELIMIT;
    451                                 break;
    452                         }
    453                         retval = mbr_bd_read(part, ba, cnt, fs_va);
    454                         break;
    455                 case BD_WRITE_BLOCKS:
    456                         ba = MERGE_LOUP32(IPC_GET_ARG1(call),
    457                             IPC_GET_ARG2(call));
    458                         cnt = IPC_GET_ARG3(call);
    459                         if (cnt * block_size > comm_size) {
    460                                 retval = ELIMIT;
    461                                 break;
    462                         }
    463                         retval = mbr_bd_write(part, ba, cnt, fs_va);
    464                         break;
    465                 case BD_GET_BLOCK_SIZE:
    466                         async_answer_1(callid, EOK, block_size);
    467                         continue;
    468                 case BD_GET_NUM_BLOCKS:
    469                         async_answer_2(callid, EOK, LOWER32(part->length),
    470                             UPPER32(part->length));
    471                         continue;
    472                 default:
    473                         retval = EINVAL;
    474                         break;
    475                 }
    476                 async_answer_0(callid, retval);
    477         }
     435        bd_conn(iid, icall, &part->bd);
     436}
     437
     438/** Open device. */
     439static int mbr_bd_open(bd_srv_t *bd)
     440{
     441        return EOK;
     442}
     443
     444/** Close device. */
     445static int mbr_bd_close(bd_srv_t *bd)
     446{
     447        return EOK;
    478448}
    479449
    480450/** Read blocks from partition. */
    481 static int mbr_bd_read(part_t *p, uint64_t ba, size_t cnt, void *buf)
    482 {
    483         uint64_t gba;
     451static int mbr_bd_read_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt, void *buf,
     452    size_t size)
     453{
     454        part_t *p = bd_srv_part(bd);
     455        aoff64_t gba;
     456
     457        if (cnt * block_size < size)
     458                return EINVAL;
    484459
    485460        if (mbr_bsa_translate(p, ba, cnt, &gba) != EOK)
    486461                return ELIMIT;
    487462
    488         return block_read_direct(indef_sid, gba, cnt, buf);
     463        return block_read_direct(indev_sid, gba, cnt, buf);
    489464}
    490465
    491466/** Write blocks to partition. */
    492 static int mbr_bd_write(part_t *p, uint64_t ba, size_t cnt, const void *buf)
    493 {
    494         uint64_t gba;
     467static int mbr_bd_write_blocks(bd_srv_t *bd, aoff64_t ba, size_t cnt,
     468    const void *buf, size_t size)
     469{
     470        part_t *p = bd_srv_part(bd);
     471        aoff64_t gba;
     472
     473        if (cnt * block_size < size)
     474                return EINVAL;
    495475
    496476        if (mbr_bsa_translate(p, ba, cnt, &gba) != EOK)
    497477                return ELIMIT;
    498478
    499         return block_write_direct(indef_sid, gba, cnt, buf);
     479        return block_write_direct(indev_sid, gba, cnt, buf);
     480}
     481
     482/** Get device block size. */
     483static int mbr_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
     484{
     485        *rsize = block_size;
     486        return EOK;
     487}
     488
     489/** Get number of blocks on device. */
     490static int mbr_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
     491{
     492        part_t *part = bd_srv_part(bd);
     493
     494        *rnb = part->length;
     495        return EOK;
    500496}
    501497
Note: See TracChangeset for help on using the changeset viewer.