1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-10 01:18:15 +03:00

ctdb-protocol: Fix marshalling for ctdb_ltdb_header

Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Amitay Isaacs 2017-06-29 22:39:41 +10:00 committed by Martin Schwenke
parent 04d0b30592
commit 25217a9bb5
8 changed files with 117 additions and 48 deletions

View File

@ -1101,6 +1101,7 @@ int ctdb_ltdb_fetch(struct ctdb_db_context *db, TDB_DATA key,
TALLOC_CTX *mem_ctx, TDB_DATA *data)
{
TDB_DATA rec;
size_t np;
int ret;
rec = tdb_fetch(db->ltdb->tdb, key);
@ -1124,17 +1125,15 @@ int ctdb_ltdb_fetch(struct ctdb_db_context *db, TDB_DATA key,
return 0;
}
ret = ctdb_ltdb_header_pull(rec.dptr, rec.dsize, header);
ret = ctdb_ltdb_header_pull(rec.dptr, rec.dsize, header, &np);
if (ret != 0) {
return ret;
}
ret = 0;
if (data != NULL) {
size_t offset = ctdb_ltdb_header_len(header);
data->dsize = rec.dsize - offset;
data->dptr = talloc_memdup(mem_ctx, rec.dptr + offset,
data->dsize = rec.dsize - np;
data->dptr = talloc_memdup(mem_ctx, rec.dptr + np,
data->dsize);
if (data->dptr == NULL) {
ret = ENOMEM;
@ -1230,6 +1229,7 @@ static int ctdb_fetch_lock_check(struct tevent_req *req)
struct ctdb_record_handle *h = state->h;
struct ctdb_ltdb_header header;
TDB_DATA data = tdb_null;
size_t np;
int ret, err = 0;
bool do_migrate = false;
@ -1253,7 +1253,7 @@ static int ctdb_fetch_lock_check(struct tevent_req *req)
}
/* Got the record */
ret = ctdb_ltdb_header_pull(data.dptr, data.dsize, &header);
ret = ctdb_ltdb_header_pull(data.dptr, data.dsize, &header, &np);
if (ret != 0) {
err = ret;
goto failed;
@ -1457,6 +1457,7 @@ int ctdb_store_record(struct ctdb_record_handle *h, TDB_DATA data)
{
uint8_t header[sizeof(struct ctdb_ltdb_header)];
TDB_DATA rec[2];
size_t np;
int ret;
/* Cannot modify the record if it was obtained as a readonly copy */
@ -1471,9 +1472,9 @@ int ctdb_store_record(struct ctdb_record_handle *h, TDB_DATA data)
return 0;
}
ctdb_ltdb_header_push(&h->header, header);
ctdb_ltdb_header_push(&h->header, header, &np);
rec[0].dsize = ctdb_ltdb_header_len(&h->header);
rec[0].dsize = np;
rec[0].dptr = header;
rec[1].dsize = data.dsize;
@ -1506,6 +1507,7 @@ struct tevent_req *ctdb_delete_record_send(TALLOC_CTX *mem_ctx,
struct ctdb_req_control request;
uint8_t header[sizeof(struct ctdb_ltdb_header)];
TDB_DATA rec;
size_t np;
int ret;
req = tevent_req_create(mem_ctx, &state,
@ -1524,9 +1526,9 @@ struct tevent_req *ctdb_delete_record_send(TALLOC_CTX *mem_ctx,
return tevent_req_post(req, ev);
}
ctdb_ltdb_header_push(&h->header, header);
ctdb_ltdb_header_push(&h->header, header, &np);
rec.dsize = ctdb_ltdb_header_len(&h->header);
rec.dsize = np;
rec.dptr = header;
ret = tdb_store(h->db->ltdb->tdb, h->key, rec, TDB_REPLACE);

View File

@ -26,10 +26,11 @@
/* From protocol/protocol_types.c */
size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *header);
void ctdb_ltdb_header_push(struct ctdb_ltdb_header *header, uint8_t *buf);
size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *in);
void ctdb_ltdb_header_push(struct ctdb_ltdb_header *in, uint8_t *buf,
size_t *npush);
int ctdb_ltdb_header_pull(uint8_t *buf, size_t buflen,
struct ctdb_ltdb_header *header);
struct ctdb_ltdb_header *out, size_t *npull);
int ctdb_ltdb_header_extract(TDB_DATA *data, struct ctdb_ltdb_header *header);

View File

@ -1197,38 +1197,91 @@ fail:
return ret;
}
size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *header)
size_t ctdb_ltdb_header_len(struct ctdb_ltdb_header *in)
{
return sizeof(struct ctdb_ltdb_header);
return ctdb_uint64_len(&in->rsn) +
ctdb_uint32_len(&in->dmaster) +
ctdb_uint32_len(&in->reserved1) +
ctdb_uint32_len(&in->flags) +
ctdb_padding_len(4);
}
void ctdb_ltdb_header_push(struct ctdb_ltdb_header *header, uint8_t *buf)
void ctdb_ltdb_header_push(struct ctdb_ltdb_header *in, uint8_t *buf,
size_t *npush)
{
memcpy(buf, header, sizeof(struct ctdb_ltdb_header));
size_t offset = 0, np;
ctdb_uint64_push(&in->rsn, buf+offset, &np);
offset += np;
ctdb_uint32_push(&in->dmaster, buf+offset, &np);
offset += np;
ctdb_uint32_push(&in->reserved1, buf+offset, &np);
offset += np;
ctdb_uint32_push(&in->flags, buf+offset, &np);
offset += np;
ctdb_padding_push(4, buf+offset, &np);
offset += np;
*npush = offset;
}
int ctdb_ltdb_header_pull(uint8_t *buf, size_t buflen,
struct ctdb_ltdb_header *header)
struct ctdb_ltdb_header *out, size_t *npull)
{
if (buflen < sizeof(struct ctdb_ltdb_header)) {
return EMSGSIZE;
}
size_t offset = 0, np;
int ret;
memcpy(header, buf, sizeof(struct ctdb_ltdb_header));
ret = ctdb_uint64_pull(buf+offset, buflen-offset, &out->rsn, &np);
if (ret != 0) {
return ret;
}
offset += np;
ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->dmaster, &np);
if (ret != 0) {
return ret;
}
offset += np;
ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->reserved1,
&np);
if (ret != 0) {
return ret;
}
offset += np;
ret = ctdb_uint32_pull(buf+offset, buflen-offset, &out->flags, &np);
if (ret != 0) {
return ret;
}
offset += np;
ret = ctdb_padding_pull(buf+offset, buflen-offset, 4, &np);
if (ret != 0) {
return ret;
}
offset += np;
*npull = offset;
return 0;
}
int ctdb_ltdb_header_extract(TDB_DATA *data, struct ctdb_ltdb_header *header)
{
size_t np;
int ret;
ret = ctdb_ltdb_header_pull(data->dptr, data->dsize, header);
ret = ctdb_ltdb_header_pull(data->dptr, data->dsize, header, &np);
if (ret != 0) {
return ret;
}
data->dptr += sizeof(struct ctdb_ltdb_header);
data->dsize -= sizeof(struct ctdb_ltdb_header);
data->dptr += np;
data->dsize -= np;
return 0;
}

View File

@ -515,10 +515,11 @@ void verify_ctdb_pulldb_ext(struct ctdb_pulldb_ext *p1,
assert(p1->srvid == p2->srvid);
}
void fill_ctdb_ltdb_header(TALLOC_CTX *mem_ctx, struct ctdb_ltdb_header *p)
void fill_ctdb_ltdb_header(struct ctdb_ltdb_header *p)
{
p->rsn = rand64();
p->dmaster = rand32();
p->reserved1 = rand32();
p->flags = rand32();
}
@ -527,6 +528,7 @@ void verify_ctdb_ltdb_header(struct ctdb_ltdb_header *p1,
{
assert(p1->rsn == p2->rsn);
assert(p1->dmaster == p2->dmaster);
assert(p1->reserved1 == p2->reserved1);
assert(p1->flags == p2->flags);
}
@ -536,7 +538,7 @@ void fill_ctdb_rec_data(TALLOC_CTX *mem_ctx, struct ctdb_rec_data *p)
if (p->reqid % 5 == 0) {
p->header = talloc(mem_ctx, struct ctdb_ltdb_header);
assert(p->header != NULL);
fill_ctdb_ltdb_header(mem_ctx, p->header);
fill_ctdb_ltdb_header(p->header);
} else {
p->header = NULL;
}
@ -1071,7 +1073,7 @@ void verify_ctdb_statistics_list(struct ctdb_statistics_list *p1,
void fill_ctdb_key_data(TALLOC_CTX *mem_ctx, struct ctdb_key_data *p)
{
p->db_id = rand32();
fill_ctdb_ltdb_header(mem_ctx, &p->header);
fill_ctdb_ltdb_header(&p->header);
fill_tdb_data_nonnull(mem_ctx, &p->key);
}

View File

@ -219,7 +219,7 @@ void fill_ctdb_pulldb_ext(TALLOC_CTX *mem_ctx, struct ctdb_pulldb_ext *p);
void verify_ctdb_pulldb_ext(struct ctdb_pulldb_ext *p1,
struct ctdb_pulldb_ext *p2);
void fill_ctdb_ltdb_header(TALLOC_CTX *mem_ctx, struct ctdb_ltdb_header *p);
void fill_ctdb_ltdb_header(struct ctdb_ltdb_header *p);
void verify_ctdb_ltdb_header(struct ctdb_ltdb_header *p1,
struct ctdb_ltdb_header *p2);

View File

@ -299,6 +299,28 @@ static int ctdb_pulldb_ext_pull_old(uint8_t *buf, size_t buflen,
return 0;
}
static size_t ctdb_ltdb_header_len_old(struct ctdb_ltdb_header *in)
{
return sizeof(struct ctdb_ltdb_header);
}
static void ctdb_ltdb_header_push_old(struct ctdb_ltdb_header *in,
uint8_t *buf)
{
memcpy(buf, in, sizeof(struct ctdb_ltdb_header));
}
static int ctdb_ltdb_header_pull_old(uint8_t *buf, size_t buflen,
struct ctdb_ltdb_header *out)
{
if (buflen < sizeof(struct ctdb_ltdb_header)) {
return EMSGSIZE;
}
memcpy(out, buf, sizeof(struct ctdb_ltdb_header));
return 0;
}
COMPAT_TYPE3_TEST(struct ctdb_statistics, ctdb_statistics);
COMPAT_TYPE3_TEST(struct ctdb_vnn_map, ctdb_vnn_map);
@ -306,6 +328,8 @@ COMPAT_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
COMPAT_TYPE3_TEST(struct ctdb_pulldb, ctdb_pulldb);
COMPAT_TYPE3_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext);
COMPAT_TYPE1_TEST(struct ctdb_ltdb_header, ctdb_ltdb_header);
int main(int argc, char *argv[])
{
if (argc == 2) {
@ -318,6 +342,7 @@ int main(int argc, char *argv[])
COMPAT_TEST_FUNC(ctdb_dbid_map)();
COMPAT_TEST_FUNC(ctdb_pulldb)();
COMPAT_TEST_FUNC(ctdb_pulldb_ext)();
COMPAT_TEST_FUNC(ctdb_ltdb_header)();
return 0;
}

View File

@ -31,22 +31,6 @@ PROTOCOL_TYPE2_TEST(TDB_DATA, ctdb_tdb_data);
PROTOCOL_TYPE2_TEST(TDB_DATA, ctdb_tdb_datan);
PROTOCOL_TYPE1_TEST(struct ctdb_latency_counter, ctdb_latency_counter);
static void test_ctdb_ltdb_header(void)
{
TALLOC_CTX *mem_ctx = talloc_new(NULL);
struct ctdb_ltdb_header p1, p2;
size_t buflen;
int ret;
fill_ctdb_ltdb_header(mem_ctx, &p1);
buflen = ctdb_ltdb_header_len(&p1);
ctdb_ltdb_header_push(&p1, BUFFER);
ret = ctdb_ltdb_header_pull(BUFFER, buflen, &p2);
assert(ret == 0);
verify_ctdb_ltdb_header(&p1, &p2);
talloc_free(mem_ctx);
}
static void test_ctdb_g_lock(void)
{
TALLOC_CTX *mem_ctx = talloc_new(NULL);
@ -69,6 +53,7 @@ PROTOCOL_TYPE3_TEST(struct ctdb_dbid, ctdb_dbid);
PROTOCOL_TYPE3_TEST(struct ctdb_dbid_map, ctdb_dbid_map);
PROTOCOL_TYPE3_TEST(struct ctdb_pulldb, ctdb_pulldb);
PROTOCOL_TYPE3_TEST(struct ctdb_pulldb_ext, ctdb_pulldb_ext);
PROTOCOL_TYPE1_TEST(struct ctdb_ltdb_header, ctdb_ltdb_header);
DEFINE_TEST(struct ctdb_rec_data, ctdb_rec_data);
DEFINE_TEST(struct ctdb_rec_buffer, ctdb_rec_buffer);
DEFINE_TEST(struct ctdb_traverse_start, ctdb_traverse_start);
@ -160,7 +145,6 @@ int main(int argc, char *argv[])
TEST_FUNC(ctdb_tdb_data)();
TEST_FUNC(ctdb_tdb_datan)();
TEST_FUNC(ctdb_latency_counter)();
test_ctdb_ltdb_header();
test_ctdb_g_lock();
TEST_FUNC(ctdb_statistics)();
@ -169,6 +153,7 @@ int main(int argc, char *argv[])
TEST_FUNC(ctdb_dbid_map)();
TEST_FUNC(ctdb_pulldb)();
TEST_FUNC(ctdb_pulldb_ext)();
TEST_FUNC(ctdb_ltdb_header)();
TEST_FUNC(ctdb_rec_data)();
TEST_FUNC(ctdb_rec_buffer)();
TEST_FUNC(ctdb_traverse_start)();

View File

@ -5281,6 +5281,7 @@ static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
TDB_DATA key, data[2], value;
struct ctdb_ltdb_header header;
uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
size_t np;
int ret;
if (argc < 3 || argc > 5) {
@ -5319,9 +5320,9 @@ static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
header.flags = (uint32_t)atol(argv[5]);
}
ctdb_ltdb_header_push(&header, header_buf);
ctdb_ltdb_header_push(&header, header_buf, &np);
data[0].dsize = ctdb_ltdb_header_len(&header);
data[0].dsize = np;
data[0].dptr = header_buf;
data[1].dsize = value.dsize;