proc-write-actions.c 17.1 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * (C) 2017 The University of Chicago
 * 
 * See COPYRIGHT in top-level directory.
 */
#include "proc-write-actions.h"
#include "args-write-actions.h"
#include "write-op-impl.h"
#include "utlist.h"
Rob Latham's avatar
Rob Latham committed
10
#include "log.h"
11 12 13 14 15 16 17 18 19 20 21 22 23

/**
 * This file contains the main hg_proc_mobject_store_write_op_t 
 * serialization function. It relies on helper functions to encode
 * and decode each possible write action. Encoding/decoding works
 * by creating an args_wr_action_* object and passing it the required
 * parameters, then serializing the structure along with potential
 * additional data.
 */

typedef hg_return_t (*encode_fn)(hg_proc_t, uint64_t*, void*);
typedef hg_return_t (*decode_fn)(hg_proc_t, uint64_t*, void*);

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
static hg_return_t encode_write_action_create(hg_proc_t proc, 
                                              uint64_t* pos,
                                              wr_action_create_t action);

static hg_return_t decode_write_action_create(hg_proc_t proc, 
                                              uint64_t* pos,
                                              wr_action_create_t* action);

static hg_return_t encode_write_action_write(hg_proc_t proc,
                                             uint64_t* pos,
                                             wr_action_write_t action);

static hg_return_t decode_write_action_write(hg_proc_t proc,
                                             uint64_t* pos,
                                             wr_action_write_t* action);

static hg_return_t encode_write_action_write_full(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_full_t action);

static hg_return_t decode_write_action_write_full(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_full_t* action);

static hg_return_t encode_write_action_write_same(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_same_t action);

static hg_return_t decode_write_action_write_same(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_same_t* action);

static hg_return_t encode_write_action_append(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_append_t action);

static hg_return_t decode_write_action_append(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_append_t* action);

static hg_return_t encode_write_action_remove(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_remove_t action);

static hg_return_t decode_write_action_remove(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_remove_t* action);

static hg_return_t encode_write_action_truncate(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_truncate_t action);

static hg_return_t decode_write_action_truncate(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_truncate_t* action);

static hg_return_t encode_write_action_zero(hg_proc_t proc,
                                            uint64_t* pos,
                                            wr_action_zero_t action);

static hg_return_t decode_write_action_zero(hg_proc_t proc,
                                            uint64_t* pos,
                                            wr_action_zero_t* action);

static hg_return_t encode_write_action_omap_set(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_omap_set_t action);

static hg_return_t decode_write_action_omap_set(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_omap_set_t* action);

static hg_return_t encode_write_action_omap_rm_keys(hg_proc_t proc,
                                                    uint64_t* pos,
                                                    wr_action_omap_rm_keys_t action);

static hg_return_t decode_write_action_omap_rm_keys(hg_proc_t proc,
                                                    uint64_t* pos,
                                                    wr_action_omap_rm_keys_t* action);

/**
 * The following two arrays are here to avoid a big switch.
 */

/* encoding functions */
static encode_fn encode_write_action[_WRITE_OPCODE_END_ENUM_] = {
	NULL,
	(encode_fn)encode_write_action_create,
	(encode_fn)encode_write_action_write,
	(encode_fn)encode_write_action_write_full,
	(encode_fn)encode_write_action_write_same,
	(encode_fn)encode_write_action_append,
	(encode_fn)encode_write_action_remove,
	(encode_fn)encode_write_action_truncate,
	(encode_fn)encode_write_action_zero,
	(encode_fn)encode_write_action_omap_set,
	(encode_fn)encode_write_action_omap_rm_keys
};

/* decoding functions */
static decode_fn decode_write_action[_WRITE_OPCODE_END_ENUM_] = {
	NULL,
	(decode_fn)decode_write_action_create,
	(decode_fn)decode_write_action_write,
	(decode_fn)decode_write_action_write_full,
	(decode_fn)decode_write_action_write_same,
	(decode_fn)decode_write_action_append,
	(decode_fn)decode_write_action_remove,
	(decode_fn)decode_write_action_truncate,
	(decode_fn)decode_write_action_zero,
	(decode_fn)decode_write_action_omap_set,
	(decode_fn)decode_write_action_omap_rm_keys
};

/**
 * Serialization function for mobject_store_write_op_t objects.
 * For encoding, the object should be prepared first (that is, the union fields
 * pointing to either a buffer or an offset in a bulk should be an offset in a bulk).
 */
hg_return_t hg_proc_mobject_store_write_op_t(hg_proc_t proc, mobject_store_write_op_t* write_op)
{
	wr_action_base_t elem, tmp;
	hg_return_t ret = HG_SUCCESS;
	uintptr_t position = 0;

	switch(hg_proc_get_op(proc)) {

	case HG_ENCODE:

		MOBJECT_ASSERT((*write_op)->ready, 
			"Cannot encode a write_op before it has been prepared");
		// encode the bulk handle associated with the series of operations
		ret = hg_proc_hg_bulk_t(proc, &((*write_op)->bulk_handle));
		if(ret != HG_SUCCESS) return ret;
		// encode the number of actions
		ret = hg_proc_memcpy(proc, &((*write_op)->num_actions), 
								sizeof((*write_op)->num_actions));
		if(ret != HG_SUCCESS) return ret;

		// for each action ...
		DL_FOREACH((*write_op)->actions,elem) {
			write_op_code_t opcode = elem->type;
166
			MOBJECT_ASSERT((opcode > 0 && opcode < _WRITE_OPCODE_END_ENUM_), 
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
				"Invalid write_op opcode");
			// encode the type of action
			ret = hg_proc_memcpy(proc, &opcode, sizeof(opcode));
			if(ret != HG_SUCCESS) return ret;
			// encode the action's arguments
			ret = encode_write_action[opcode](proc, &position, elem);
			if(ret != HG_SUCCESS) return ret;
		}
		break;

	case HG_DECODE:

		*write_op = mobject_store_create_write_op();
		(*write_op)->ready = 1;

		// decode the bulk handle
		ret = hg_proc_hg_bulk_t(proc, &((*write_op)->bulk_handle));
		if(ret != HG_SUCCESS) return ret;
		// decode the number of actions
		ret = hg_proc_memcpy(proc, &((*write_op)->num_actions),
								sizeof((*write_op)->num_actions));
		if(ret != HG_SUCCESS) return ret;

		wr_action_base_t next_action;
		size_t i;
		for(i = 0; i < (*write_op)->num_actions; i++) {
			// decode the current action's type
			write_op_code_t opcode;
			ret = hg_proc_memcpy(proc, &opcode, sizeof(opcode));
			if(ret != HG_SUCCESS) return ret;
197
			MOBJECT_ASSERT((opcode > 0 && opcode < _WRITE_OPCODE_END_ENUM_), 
198 199 200 201
				"Invalid write_op opcode");
			// decode the action's arguments
			ret = decode_write_action[opcode](proc, &position, &next_action);
			if(ret != HG_SUCCESS) return ret;
202
			next_action->type = opcode;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
			// append to the list
			DL_APPEND((*write_op)->actions, next_action);
		}
		break;
	
	case HG_FREE:
		mobject_store_release_write_op(*write_op);
		return HG_SUCCESS;
	}

	return ret;
}

////////////////////////////////////////////////////////////////////////////////
//                          STATIC FUNCTIONS BELOW                            //
////////////////////////////////////////////////////////////////////////////////

220 221 222
static hg_return_t encode_write_action_create(hg_proc_t proc, 
                                              uint64_t* pos,
                                              wr_action_create_t action)
223 224 225 226 227 228
{
	args_wr_action_create a;
	a.exclusive = action->exclusive;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

229 230 231
static hg_return_t decode_write_action_create(hg_proc_t proc, 
                                              uint64_t* pos,
                                              wr_action_create_t* action)
232 233 234 235 236 237 238 239 240 241 242 243
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_create a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_create_t)calloc(1, sizeof(**action));
	(*action)->exclusive = a.exclusive;

	return ret;	
}

244 245 246
static hg_return_t encode_write_action_write(hg_proc_t proc,
                                             uint64_t* pos,
                                             wr_action_write_t action)
247 248 249 250 251 252 253 254 255
{
	args_wr_action_write a;
	a.buffer_position = *pos;
	a.len             = action->len;
	a.offset          = action->offset;
	*pos             += action->len;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

256 257 258
static hg_return_t decode_write_action_write(hg_proc_t proc,
                                             uint64_t* pos,
                                             wr_action_write_t* action)
259 260 261 262 263 264 265
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_write a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_write_t)calloc(1, sizeof(**action));
266 267 268 269
	(*action)->buffer.as_offset = *pos;
	(*action)->len              = a.len;
	(*action)->offset           = a.offset;
	*pos                       += a.len;
270 271 272 273

	return ret;
}

274 275 276
static hg_return_t encode_write_action_write_full(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_full_t action)
277 278 279 280 281 282 283 284
{
	args_wr_action_write_full a;
	a.buffer_position = *pos;
	a.len             = action->len;
	*pos             += action->len;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

285 286 287
static hg_return_t decode_write_action_write_full(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_full_t* action)
288 289 290 291 292 293 294
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_write_full a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_write_full_t)calloc(1, sizeof(**action));
295
	(*action)->buffer.as_offset = *pos;
296 297 298 299 300 301
	(*action)->len       = a.len;
	*pos                += a.len;

	return ret;
}

302 303 304
static hg_return_t encode_write_action_write_same(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_same_t action)
305 306 307 308 309 310 311 312 313 314
{
	args_wr_action_write_same a;
	a.buffer_position = *pos;
	a.data_len        = action->data_len;
	a.write_len       = action->write_len;
	a.offset          = action->offset;
	*pos             += action->data_len;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

315 316 317
static hg_return_t decode_write_action_write_same(hg_proc_t proc,
                                                  uint64_t* pos,
                                                  wr_action_write_same_t* action)
318 319 320 321 322 323 324
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_write_same a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_write_same_t)calloc(1, sizeof(**action));
325 326 327 328 329
	(*action)->buffer.as_offset = *pos;
	(*action)->data_len         = a.data_len;
	(*action)->write_len        = a.write_len;
	(*action)->offset           = a.offset;
	*pos                       += a.data_len;
330 331 332 333

	return ret;
}

334 335 336
static hg_return_t encode_write_action_append(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_append_t action)
337 338 339 340 341 342 343 344
{
	args_wr_action_append a;
	a.buffer_position = *pos;
	a.len             = action->len;
	*pos             += action->len;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

345 346 347
static hg_return_t decode_write_action_append(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_append_t* action)
348 349 350 351 352 353 354
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_append a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;
	
	*action = (wr_action_append_t)calloc(1, sizeof(**action));
355 356 357
	(*action)->buffer.as_offset = *pos;
	(*action)->len              = a.len;
	*pos                       += a.len;
358 359 360 361

	return ret;
}

362 363 364
static hg_return_t encode_write_action_remove(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_remove_t action)
365 366 367 368
{
	return HG_SUCCESS;
}

369 370 371
static hg_return_t decode_write_action_remove(hg_proc_t proc,
                                              uint64_t* pos,
                                              wr_action_remove_t* action)
372 373 374 375 376 377 378
{
	hg_return_t ret = HG_SUCCESS;
	*action = (wr_action_remove_t)calloc(1, sizeof(**action));
	
	return ret;
}

379 380 381
static hg_return_t encode_write_action_truncate(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_truncate_t action)
382 383 384 385 386 387
{
	args_wr_action_truncate a;
	a.offset          = action->offset;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

388 389 390
static hg_return_t decode_write_action_truncate(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_truncate_t* action)
391 392 393 394 395 396 397 398 399 400 401 402
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_truncate a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_truncate_t)calloc(1, sizeof(**action));
    (*action)->offset = a.offset;

	return ret;
}

403 404 405
static hg_return_t encode_write_action_zero(hg_proc_t proc,
                                            uint64_t* pos,
                                            wr_action_zero_t action)
406 407 408 409 410 411 412
{
	args_wr_action_zero a;
	a.offset          = action->offset;
	a.len             = action->len;
	return hg_proc_memcpy(proc, &a, sizeof(a));
}

413 414 415
static hg_return_t decode_write_action_zero(hg_proc_t proc,
                                            uint64_t* pos,
                                            wr_action_zero_t* action)
416 417 418 419 420 421 422 423 424 425 426 427 428
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_zero a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_zero_t)calloc(1, sizeof(**action));
	(*action)->offset = a.offset;
	(*action)->len    = a.len;
	
	return ret;
}

429 430 431
static hg_return_t encode_write_action_omap_set(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_omap_set_t action)
432 433 434 435 436 437 438 439 440
{
	args_wr_action_omap_set a;
	a.num             = action->num;
	a.data_size       = action->data_size;
	hg_return_t ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;
	return hg_proc_memcpy(proc, action->data, action->data_size);
}

441 442 443
static hg_return_t decode_write_action_omap_set(hg_proc_t proc,
                                                uint64_t* pos,
                                                wr_action_omap_set_t* action)
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_omap_set a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_omap_set_t)calloc(1, sizeof(**action)-1+a.data_size);
	(*action)->num       = a.num;
	(*action)->data_size = a.data_size;
	
	ret = hg_proc_memcpy(proc, (*action)->data, a.data_size);
	
	return ret;
}

459 460 461
static hg_return_t encode_write_action_omap_rm_keys(hg_proc_t proc,
                                                    uint64_t* pos,
                                                    wr_action_omap_rm_keys_t action)
462 463 464 465 466 467 468 469 470
{
	args_wr_action_omap_rm_keys a;
	a.num_keys        = action->num_keys;
	a.data_size       = action->data_size;
	hg_return_t ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;
	return hg_proc_memcpy(proc, action->data, action->data_size);
}

471 472 473
static hg_return_t decode_write_action_omap_rm_keys(hg_proc_t proc,
                                                    uint64_t* pos,
                                                    wr_action_omap_rm_keys_t* action)
474 475 476 477 478 479 480 481 482 483 484 485 486 487
{
	hg_return_t ret = HG_SUCCESS;
	args_wr_action_omap_rm_keys a;
	ret = hg_proc_memcpy(proc, &a, sizeof(a));
	if(ret != HG_SUCCESS) return ret;

	*action = (wr_action_omap_rm_keys_t)calloc(1, sizeof(**action)-1+a.data_size);
	(*action)->num_keys  = a.num_keys;
	(*action)->data_size = a.data_size;
	
	ret = hg_proc_memcpy(proc, (*action)->data, a.data_size);

	return ret;
}