bake-server.c 28 KB
Newer Older
Philip Carns's avatar
Philip Carns committed
1 2 3 4 5 6
/*
 * (C) 2015 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */

7 8
#include "bake-config.h"

Philip Carns's avatar
Philip Carns committed
9
#include <assert.h>
10
#include <libpmemobj.h>
11
#include <bake-server.h>
12
#include "uthash.h"
13
#include "bake-rpc.h"
Philip Carns's avatar
Philip Carns committed
14

15
/* definition of BAKE root data structure (just a uuid for now) */
16
typedef struct
17
{   
18
    bake_target_id_t pool_id;
19
} bake_root_t;
20

21
/* definition of internal BAKE region_id_t identifier for libpmemobj back end */
22 23
typedef struct
{
24 25 26
    PMEMoid oid;
    uint64_t size;
} pmemobj_region_id_t;
27

28 29 30 31 32 33 34 35
typedef struct
{
    PMEMobjpool* pmem_pool;
    bake_root_t* pmem_root;
    bake_target_id_t target_id;
    UT_hash_handle hh;
} bake_pmem_entry_t;

36
typedef struct bake_server_context_t
37
{
38 39
    uint64_t num_targets;
    bake_pmem_entry_t* targets;
40
} bake_server_context_t;
41

42
static void bake_server_finalize_cb(void *data);
43

44
int bake_makepool(
45 46 47
        const char *pool_name,
        size_t pool_size,
        mode_t pool_mode)
48
{
49
    PMEMobjpool *pool;
50
    PMEMoid root_oid;
51
    bake_root_t *root;
52

53 54
    pool = pmemobj_create(pool_name, NULL, pool_size, pool_mode);
    if(!pool)
55
    {
56 57
        fprintf(stderr, "pmemobj_create: %s\n", pmemobj_errormsg());
        return(-1);
58 59 60
    }

    /* find root */
61
    root_oid = pmemobj_root(pool, sizeof(bake_root_t));
62 63 64
    root = pmemobj_direct(root_oid);

    /* store the target id for this bake pool at the root */
65
    uuid_generate(root->pool_id.id);
66
    pmemobj_persist(pool, root, sizeof(bake_root_t));
Shane Snyder's avatar
Shane Snyder committed
67
#if 0
68
    char target_string[64];
69
    uuid_unparse(root->id, target_string);
70
    fprintf(stderr, "created BAKE target ID: %s\n", target_string);
71
#endif
72 73 74 75 76 77

    pmemobj_close(pool);

    return(0);
}

78
int bake_provider_register(
79
        margo_instance_id mid,
80
        uint16_t provider_id,
81 82
        ABT_pool abt_pool,
        bake_provider_t* provider)
83
{
84
    bake_server_context_t *tmp_svr_ctx;
85

86
    /* check if a provider with the same multiplex id already exists */
87
    {
88 89
        hg_id_t id;
        hg_bool_t flag;
90
        margo_provider_registered_name(mid, "bake_probe_rpc", provider_id, &id, &flag);
91
        if(flag == HG_TRUE) {
92
            fprintf(stderr, "bake_provider_register(): a provider with the same id (%d) already exists\n", provider_id);
93 94
            return -1;
        }
95
    }
96

97 98 99
    /* allocate the resulting structure */    
    tmp_svr_ctx = calloc(1,sizeof(*tmp_svr_ctx));
    if(!tmp_svr_ctx)
100
        return(-1);
Philip Carns's avatar
Philip Carns committed
101

102
    /* register RPCs */
103
    hg_id_t rpc_id;
104
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_create_rpc",
105
            bake_create_in_t, bake_create_out_t, 
106 107 108
            bake_create_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_write_rpc",
109
            bake_write_in_t, bake_write_out_t, 
110 111 112
            bake_write_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_write_rpc",
113
            bake_eager_write_in_t, bake_eager_write_out_t, 
114 115 116
            bake_eager_write_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_eager_read_rpc",
117
            bake_eager_read_in_t, bake_eager_read_out_t, 
118 119 120
            bake_eager_read_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_persist_rpc",
121
            bake_persist_in_t, bake_persist_out_t, 
122 123 124
            bake_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_create_write_persist_rpc",
125
            bake_create_write_persist_in_t, bake_create_write_persist_out_t,
126 127 128
            bake_create_write_persist_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_get_size_rpc",
129
            bake_get_size_in_t, bake_get_size_out_t, 
130 131
            bake_get_size_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
132 133 134 135
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_get_data_rpc",
            bake_get_data_in_t, bake_get_data_out_t, 
            bake_get_data_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
136
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_read_rpc",
137
            bake_read_in_t, bake_read_out_t, 
138 139 140
            bake_read_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_probe_rpc",
141
            bake_probe_in_t, bake_probe_out_t, bake_probe_ult, 
142 143 144 145 146
            provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_noop_rpc",
            void, void, bake_noop_ult, provider_id, abt_pool);
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
Shane Snyder's avatar
Shane Snyder committed
147 148 149
    rpc_id = MARGO_REGISTER_PROVIDER(mid, "bake_remove_rpc",
            bake_remove_in_t, bake_remove_out_t, bake_remove_ult,
            provider_id, abt_pool);
Shane Snyder's avatar
Shane Snyder committed
150
    margo_register_data(mid, rpc_id, (void*)tmp_svr_ctx, NULL);
151

152 153 154
    /* install the bake server finalize callback */
    margo_push_finalize_callback(mid, &bake_server_finalize_cb, tmp_svr_ctx);

155 156
    if(provider != BAKE_PROVIDER_IGNORE)
        *provider = tmp_svr_ctx;
157

158
    return(0);
159 160
}

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
int bake_provider_add_storage_target(
        bake_provider_t provider,
        const char *target_name,
        bake_target_id_t* target_id)
{
    bake_pmem_entry_t* new_entry = calloc(1, sizeof(*new_entry));

    new_entry->pmem_pool = pmemobj_open(target_name, NULL);
    if(!(new_entry->pmem_pool)) {
        fprintf(stderr, "pmemobj_open: %s\n", pmemobj_errormsg());
        free(new_entry);
        return -1;
    }

    /* check to make sure the root is properly set */
    PMEMoid root_oid = pmemobj_root(new_entry->pmem_pool, sizeof(bake_root_t));
    new_entry->pmem_root = pmemobj_direct(root_oid);
    bake_target_id_t key = new_entry->pmem_root->pool_id;
    new_entry->target_id = key;

    if(uuid_is_null(key.id))
    {
        fprintf(stderr, "Error: BAKE pool %s is not properly initialized\n", target_name);
        pmemobj_close(new_entry->pmem_pool);
        free(new_entry);
        return(-1);
    }

    /* insert in the provider's hash */
    HASH_ADD(hh, provider->targets, target_id, sizeof(bake_target_id_t), new_entry);
    /* check that it was inserted */
    bake_pmem_entry_t* check_entry = NULL;
    HASH_FIND(hh, provider->targets, &key, sizeof(bake_target_id_t), check_entry);
    if(check_entry != new_entry) {
        fprintf(stderr, "Error: BAKE could not insert new pmem pool into the hash\n");
        pmemobj_close(new_entry->pmem_pool);
        free(new_entry);
        return -1;
    }

    provider->num_targets += 1;
    *target_id = key;
    return 0;
}

static bake_pmem_entry_t* find_pmem_entry(
            bake_provider_t provider,
            bake_target_id_t target_id)
{
    bake_pmem_entry_t* entry = NULL;
    HASH_FIND(hh, provider->targets, &target_id, sizeof(bake_target_id_t), entry);
    return entry;
}

int bake_provider_remove_storage_target(
        bake_provider_t provider,
        bake_target_id_t target_id)
{
    bake_pmem_entry_t* entry = NULL;
    HASH_FIND(hh, provider->targets, &target_id, sizeof(bake_target_id_t), entry);
    if(!entry) return -1;
    pmemobj_close(entry->pmem_pool);
    HASH_DEL(provider->targets, entry);
    free(entry);
    return 0;
}

int bake_provider_remove_all_storage_targets(
        bake_provider_t provider)
{
    bake_pmem_entry_t *p, *tmp;
    HASH_ITER(hh, provider->targets, p, tmp) {
        HASH_DEL(provider->targets, p);
        pmemobj_close(p->pmem_pool);
        free(p);
    }
237
    provider->num_targets = 0;
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    return 0;
}

int bake_provider_count_storage_targets(
        bake_provider_t provider,
        uint64_t* num_targets)
{
    *num_targets = provider->num_targets;
    return 0;
}

int bake_provider_list_storage_targets(
        bake_provider_t provider,
        bake_target_id_t* targets)
{
    bake_pmem_entry_t *p, *tmp;
    uint64_t i = 0;
    HASH_ITER(hh, provider->targets, p, tmp) {
        targets[i] = p->target_id;
        i += 1;
    }
    return 0;
}

262
/* service a remote RPC that creates a BAKE region */
263
static void bake_create_ult(hg_handle_t handle)
264
{
265 266
    bake_create_out_t out;
    bake_create_in_t in;
267 268 269
    hg_return_t hret;
    pmemobj_region_id_t* prid;

270 271 272 273
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
    bake_provider_t svr_ctx = 
274
        margo_registered_data(mid, info->id);
275 276
    if(!svr_ctx) {
        fprintf(stderr, "Error: BAKE create could not find provider\n"); 
277
        goto respond_with_error;
278 279
    }

280 281 282 283
    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
        goto respond_with_error;

284 285 286 287 288 289 290
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
        fprintf(stderr, "Error: BAKE create could not find storage target\n");
        goto respond_with_error;
    }

291
    /* TODO: this check needs to be somewhere else */
292
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);
293

294 295 296 297
    memset(&out, 0, sizeof(out));

    prid = (pmemobj_region_id_t*)out.rid.data;
    prid->size = in.region_size;
298 299 300
    /* find the pmem pool */
    out.ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
            in.region_size, 0, NULL, NULL);
301

302 303
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
304 305

finish:
306
    margo_destroy(handle);
307
    return;
308 309 310 311 312

respond_with_error:
    out.ret = -1;
    margo_respond(handle, &out);
    goto finish;
313
}
314
DEFINE_MARGO_RPC_HANDLER(bake_create_ult)
315

316
    /* service a remote RPC that writes to a BAKE region */
317
static void bake_write_ult(hg_handle_t handle)
318
{
319 320
    bake_write_out_t out;
    bake_write_in_t in;
321
    hg_return_t hret;
322
    hg_addr_t src_addr;
323 324
    char* buffer;
    hg_bulk_t bulk_handle;
325
    const struct hg_info *hgi;
326 327 328 329 330
    margo_instance_id mid;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

331 332
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
333
    hgi = margo_get_info(handle);
334
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
335 336 337 338 339 340
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
341

342
    hret = margo_get_input(handle, &in);
343 344 345
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
346 347
        margo_respond(handle, &out);
        margo_destroy(handle);
348 349 350 351 352 353 354 355 356 357
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
358 359 360
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
361 362
        return;
    }
363
    buffer = &buffer[in.region_offset];
364 365

    /* create bulk handle for local side of transfer */
366
    hret = margo_bulk_create(mid, 1, (void**)(&buffer), &in.bulk_size,
367
            HG_BULK_WRITE_ONLY, &bulk_handle);
368 369 370
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
371 372 373
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
374 375 376
        return;
    }

377 378
    if(in.remote_addr_str)
    {
379
        /* a proxy address was provided to pull write data from */
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
            out.ret = -1;
            margo_bulk_free(bulk_handle);
            margo_free_input(handle, &in);
            margo_respond(handle, &out);
            margo_destroy(handle);
            return;
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PULL, src_addr, in.bulk_handle,
398
            in.bulk_offset, bulk_handle, 0, in.bulk_size);
399
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
400
    {
401
        out.ret = -1;
402 403
        if(in.remote_addr_str)
            margo_addr_free(mid, src_addr);
404 405 406 407
        margo_bulk_free(bulk_handle);
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
408
        return;
Philip Carns's avatar
Philip Carns committed
409 410
    }

411 412
    out.ret = 0;

413 414
    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
415 416 417 418
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
419 420
    return;
}
421
DEFINE_MARGO_RPC_HANDLER(bake_write_ult)
422

423
    /* service a remote RPC that writes to a BAKE region in eager mode */
424
static void bake_eager_write_ult(hg_handle_t handle)
425
{
426 427
    bake_eager_write_out_t out;
    bake_eager_write_in_t in;
428 429 430 431 432 433 434
    hg_return_t hret;
    char* buffer;
    hg_bulk_t bulk_handle;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

435 436 437
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
438
    bake_provider_t svr_ctx = margo_registered_data(mid, info->id);
439 440 441 442 443 444 445
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

446
    hret = margo_get_input(handle, &in);
447 448 449
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
450 451
        margo_respond(handle, &out);
        margo_destroy(handle);
452 453 454 455 456 457 458 459
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
460
    {
461
        out.ret = -1;
462 463 464
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
465
        return;
466
    }
467
    buffer = &buffer[in.region_offset];
468 469 470 471 472

    memcpy(buffer, in.buffer, in.size);

    out.ret = 0;

473 474 475
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
476 477
    return;
}
478
DEFINE_MARGO_RPC_HANDLER(bake_eager_write_ult)
479

480
    /* service a remote RPC that persists to a BAKE region */
481
static void bake_persist_ult(hg_handle_t handle)
482
{
483 484
    bake_persist_out_t out;
    bake_persist_in_t in;
485 486 487
    hg_return_t hret;
    char* buffer;
    pmemobj_region_id_t* prid;
488

489 490
    memset(&out, 0, sizeof(out));

491 492 493
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* info = margo_get_info(handle);
494
    bake_provider_t svr_ctx = margo_registered_data(mid, info->id);
495 496 497 498 499 500 501
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

502
    hret = margo_get_input(handle, &in);
503
    if(hret != HG_SUCCESS)
504
    {
505
        out.ret = -1;
506 507
        margo_respond(handle, &out);
        margo_destroy(handle);
508
        return;
509
    }
510

511 512 513 514 515
    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
Philip Carns's avatar
Philip Carns committed
516
    {
517
        out.ret = -1;
518 519 520
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
521
        return;
Philip Carns's avatar
Philip Carns committed
522
    }
523 524

    /* TODO: should this have an abt shim in case it blocks? */
525 526
    PMEMobjpool* pmem_pool = pmemobj_pool_by_oid(prid->oid);
    pmemobj_persist(pmem_pool, buffer, prid->size);
527 528 529

    out.ret = 0;

530 531 532
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
533 534
    return;
}
535
DEFINE_MARGO_RPC_HANDLER(bake_persist_ult)
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
static void bake_create_write_persist_ult(hg_handle_t handle)
{
    bake_create_write_persist_out_t out;
    bake_create_write_persist_in_t in;
    hg_addr_t src_addr;
    char* buffer;
    hg_bulk_t bulk_handle;
    const struct hg_info *hgi;
    margo_instance_id mid;
    hg_return_t hret;
    int ret;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

552 553
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
554
    hgi = margo_get_info(handle);
555
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
556
    if(!svr_ctx) {
557
        fprintf(stderr, "Error: BAKE create_write_persist could not find provider\n");
558 559 560 561 562 563 564 565
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* TODO: this check needs to be somewhere else */
    assert(sizeof(pmemobj_region_id_t) <= BAKE_REGION_ID_DATA_SIZE);
566 567 568 569 570 571 572 573 574 575

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

576 577 578 579 580 581 582 583 584 585 586
    /* find the pmem pool */
    bake_pmem_entry_t* entry = find_pmem_entry(svr_ctx, in.bti);
    if(entry == NULL) {
        fprintf(stderr, "Error: BAKE create_write_persist could not find storage target\n");
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

587
    prid = (pmemobj_region_id_t*)out.rid.data;
588
    prid->size = in.bulk_size;
589
    ret = pmemobj_alloc(entry->pmem_pool, &prid->oid,
590
            in.bulk_size, 0, NULL, NULL);
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
    if(ret != 0)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* create bulk handle for local side of transfer */
    hret = margo_bulk_create(mid, 1, (void**)(&buffer), &in.bulk_size,
613
            HG_BULK_WRITE_ONLY, &bulk_handle);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    if(in.remote_addr_str)
    {
        /* a proxy address was provided to pull write data from */
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
            out.ret = -1;
            margo_bulk_free(bulk_handle);
            margo_free_input(handle, &in);
            margo_respond(handle, &out);
            margo_destroy(handle);
            return;
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PULL, src_addr, in.bulk_handle,
644
            in.bulk_offset, bulk_handle, 0, in.bulk_size);
645 646 647 648 649 650 651 652 653 654 655 656 657
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        if(in.remote_addr_str)
            margo_addr_free(mid, src_addr);
        margo_bulk_free(bulk_handle);
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    /* TODO: should this have an abt shim in case it blocks? */
658
    pmemobj_persist(entry->pmem_pool, buffer, prid->size);
659 660 661 662 663 664 665 666 667 668 669 670 671

    out.ret = 0;

    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_create_write_persist_ult)

672
    /* service a remote RPC that retrieves the size of a BAKE region */
673
static void bake_get_size_ult(hg_handle_t handle)
674
{
675 676
    bake_get_size_out_t out;
    bake_get_size_in_t in;
677 678 679 680 681
    hg_return_t hret;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

682 683 684
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
685
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
686 687 688 689 690 691
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
692

693
    hret = margo_get_input(handle, &in);
694
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
695
    {
696
        out.ret = -1;
697 698
        margo_respond(handle, &out);
        margo_destroy(handle);
699
        return;
Philip Carns's avatar
Philip Carns committed
700 701
    }

702 703 704 705 706 707
    prid = (pmemobj_region_id_t*)in.rid.data;

    /* kind of cheating here; the size is encoded in the RID */
    out.size = prid->size;
    out.ret = 0;

708 709 710
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
711 712
    return;
}
713
DEFINE_MARGO_RPC_HANDLER(bake_get_size_ult)
714

715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
    /* Get the raw pointer of a region */
static void bake_get_data_ult(hg_handle_t handle)
{
    bake_get_data_out_t out;
    bake_get_data_in_t in;
    hg_return_t hret;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    hret = margo_get_input(handle, &in);
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    char* buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

    out.ptr = (uint64_t)buffer;
    out.ret = 0;

    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
    return;
}
DEFINE_MARGO_RPC_HANDLER(bake_get_data_ult)

768
    /* service a remote RPC for a BAKE no-op */
769
static void bake_noop_ult(hg_handle_t handle)
770
{
771 772 773
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
774
    bake_provider_t svr_ctx = margo_registered_data(mid, hgi->id);
775

776 777
    margo_respond(handle, NULL);
    margo_destroy(handle);
778 779
    return;
}
780
DEFINE_MARGO_RPC_HANDLER(bake_noop_ult)
781

782 783
    /* TODO consolidate with write handler; read and write are nearly identical */
    /* service a remote RPC that reads from a BAKE region */
784
static void bake_read_ult(hg_handle_t handle)
785
{
786 787
    bake_read_out_t out;
    bake_read_in_t in;
788
    hg_return_t hret;
789
    hg_addr_t src_addr;
790 791
    char* buffer;
    hg_bulk_t bulk_handle;
792
    const struct hg_info *hgi;
793 794 795 796 797
    margo_instance_id mid;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

798 799
    mid = margo_hg_handle_get_instance(handle);
    assert(mid);
800
    hgi = margo_get_info(handle);
801
    bake_provider_t svr_ctx = 
802
        margo_registered_data(mid, hgi->id);
803 804 805 806 807 808
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }
809

810
    hret = margo_get_input(handle, &in);
811
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
812
    {
813
        out.ret = -1;
814 815
        margo_respond(handle, &out);
        margo_destroy(handle);
816
        return;
Philip Carns's avatar
Philip Carns committed
817 818
    }

819 820 821 822 823
    prid = (pmemobj_region_id_t*)in.rid.data;

    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
Philip Carns's avatar
Philip Carns committed
824
    {
825
        out.ret = -1;
826 827 828
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
829
        return;
Philip Carns's avatar
Philip Carns committed
830
    }
831
    buffer = &buffer[in.region_offset];
Philip Carns's avatar
Philip Carns committed
832

833
    /* create bulk handle for local side of transfer */
834
    hret = margo_bulk_create(mid, 1, (void**)(&buffer), &in.bulk_size,
835
            HG_BULK_READ_ONLY, &bulk_handle);
836
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
837
    {
838
        out.ret = -1;
839 840 841
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
842
        return;
Philip Carns's avatar
Philip Carns committed
843
    }
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    if(in.remote_addr_str)
    {
        /* a proxy address was provided to push read data to */
        hret = margo_addr_lookup(mid, in.remote_addr_str, &src_addr);
        if(hret != HG_SUCCESS)
        {
            out.ret = -1;
            margo_bulk_free(bulk_handle);
            margo_free_input(handle, &in);
            margo_respond(handle, &out);
            margo_destroy(handle);
            return;
        }
    }
    else
    {
        /* no proxy write, use the source of this request */
        src_addr = hgi->addr;
    }

    hret = margo_bulk_transfer(mid, HG_BULK_PUSH, src_addr, in.bulk_handle,
866
            in.bulk_offset, bulk_handle, 0, in.bulk_size);
867
    if(hret != HG_SUCCESS)
Philip Carns's avatar
Philip Carns committed
868
    {
869
        out.ret = -1;
870 871
        if(in.remote_addr_str)
            margo_addr_free(mid, src_addr);
872 873 874 875
        margo_bulk_free(bulk_handle);
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
876
        return;
Philip Carns's avatar
Philip Carns committed
877 878
    }

879
    out.ret = 0;
Philip Carns's avatar
Philip Carns committed
880

881 882
    if(in.remote_addr_str)
        margo_addr_free(mid, src_addr);
883 884 885 886
    margo_bulk_free(bulk_handle);
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
887 888
    return;
}
889
DEFINE_MARGO_RPC_HANDLER(bake_read_ult)
Philip Carns's avatar
Philip Carns committed
890

891 892
    /* service a remote RPC that reads from a BAKE region and eagerly sends
     * response */
893
static void bake_eager_read_ult(hg_handle_t handle)
894
{
895 896
    bake_eager_read_out_t out;
    bake_eager_read_in_t in;
897 898 899 900 901 902 903
    hg_return_t hret;
    char* buffer;
    hg_size_t size;
    pmemobj_region_id_t* prid;

    memset(&out, 0, sizeof(out));

904 905 906 907
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = 
908
        margo_registered_data(mid, hgi->id);
909 910 911 912 913 914 915
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

916
    hret = margo_get_input(handle, &in);
917 918 919
    if(hret != HG_SUCCESS)
    {
        out.ret = -1;
920 921
        margo_respond(handle, &out);
        margo_destroy(handle);
922 923 924 925
        return;
    }

    prid = (pmemobj_region_id_t*)in.rid.data;
Philip Carns's avatar
Philip Carns committed
926

927 928 929 930 931
    /* find memory address for target object */
    buffer = pmemobj_direct(prid->oid);
    if(!buffer)
    {
        out.ret = -1;
932 933 934
        margo_free_input(handle, &in);
        margo_respond(handle, &out);
        margo_destroy(handle);
935 936
        return;
    }
937
    buffer = &buffer[in.region_offset];
938

939 940 941 942
    out.ret = 0;
    out.buffer = buffer;
    out.size = in.size;

943 944 945
    margo_free_input(handle, &in);
    margo_respond(handle, &out);
    margo_destroy(handle);
946
    return;
Philip Carns's avatar
Philip Carns committed
947
}
948
DEFINE_MARGO_RPC_HANDLER(bake_eager_read_ult)
949

950
    /* service a remote RPC that probes for a BAKE target id */
951
static void bake_probe_ult(hg_handle_t handle)
952
{
953
    bake_probe_out_t out;
954

955 956
    memset(&out, 0, sizeof(out));

957 958 959 960
    margo_instance_id mid = margo_hg_handle_get_instance(handle);
    assert(mid);
    const struct hg_info* hgi = margo_get_info(handle);
    bake_provider_t svr_ctx = 
961
        margo_registered_data(mid, hgi->id);
962 963 964 965 966 967 968
    if(!svr_ctx) {
        out.ret = -1;
        margo_respond(handle, &out);
        margo_destroy(handle);
        return;
    }

969
    out.ret = 0;
970
    // XXX this is where we should handle multiple targets
971 972 973 974 975
    uint64_t targets_count;
    bake_provider_count_storage_targets(svr_ctx, &targets_count);
    bake_target_id_t targets[targets_count];
    bake_provider_list_storage_targets(svr_ctx, targets);

976
    out.targets = targets;
977
    out.num_targets = targets_count;
978

979 980
    margo_respond(handle, &out);
    margo_destroy(