| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Database interface wrapper around ctdbd
|
|---|
| 4 | Copyright (C) Volker Lendecke 2007-2009
|
|---|
| 5 | Copyright (C) Michael Adam 2009
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 | #include "system/filesys.h"
|
|---|
| 23 | #include "lib/util/tdb_wrap.h"
|
|---|
| 24 | #include "util_tdb.h"
|
|---|
| 25 |
|
|---|
| 26 | #ifdef CLUSTER_SUPPORT
|
|---|
| 27 | #include "ctdb.h"
|
|---|
| 28 | #include "ctdb_private.h"
|
|---|
| 29 | #include "ctdbd_conn.h"
|
|---|
| 30 | #include "g_lock.h"
|
|---|
| 31 | #include "messages.h"
|
|---|
| 32 |
|
|---|
| 33 | struct db_ctdb_transaction_handle {
|
|---|
| 34 | struct db_ctdb_ctx *ctx;
|
|---|
| 35 | /*
|
|---|
| 36 | * we store the reads and writes done under a transaction:
|
|---|
| 37 | * - one list stores both reads and writes (m_all),
|
|---|
| 38 | * - the other just writes (m_write)
|
|---|
| 39 | */
|
|---|
| 40 | struct ctdb_marshall_buffer *m_all;
|
|---|
| 41 | struct ctdb_marshall_buffer *m_write;
|
|---|
| 42 | uint32_t nesting;
|
|---|
| 43 | bool nested_cancel;
|
|---|
| 44 | char *lock_name;
|
|---|
| 45 | };
|
|---|
| 46 |
|
|---|
| 47 | struct db_ctdb_ctx {
|
|---|
| 48 | struct db_context *db;
|
|---|
| 49 | struct tdb_wrap *wtdb;
|
|---|
| 50 | uint32 db_id;
|
|---|
| 51 | struct db_ctdb_transaction_handle *transaction;
|
|---|
| 52 | struct g_lock_ctx *lock_ctx;
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | struct db_ctdb_rec {
|
|---|
| 56 | struct db_ctdb_ctx *ctdb_ctx;
|
|---|
| 57 | struct ctdb_ltdb_header header;
|
|---|
| 58 | struct timeval lock_time;
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 | static NTSTATUS tdb_error_to_ntstatus(struct tdb_context *tdb)
|
|---|
| 62 | {
|
|---|
| 63 | NTSTATUS status;
|
|---|
| 64 | enum TDB_ERROR tret = tdb_error(tdb);
|
|---|
| 65 |
|
|---|
| 66 | switch (tret) {
|
|---|
| 67 | case TDB_ERR_EXISTS:
|
|---|
| 68 | status = NT_STATUS_OBJECT_NAME_COLLISION;
|
|---|
| 69 | break;
|
|---|
| 70 | case TDB_ERR_NOEXIST:
|
|---|
| 71 | status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
|
|---|
| 72 | break;
|
|---|
| 73 | default:
|
|---|
| 74 | status = NT_STATUS_INTERNAL_DB_CORRUPTION;
|
|---|
| 75 | break;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | return status;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * fetch a record from the tdb, separating out the header
|
|---|
| 84 | * information and returning the body of the record.
|
|---|
| 85 | */
|
|---|
| 86 | static NTSTATUS db_ctdb_ltdb_fetch(struct db_ctdb_ctx *db,
|
|---|
| 87 | TDB_DATA key,
|
|---|
| 88 | struct ctdb_ltdb_header *header,
|
|---|
| 89 | TALLOC_CTX *mem_ctx,
|
|---|
| 90 | TDB_DATA *data)
|
|---|
| 91 | {
|
|---|
| 92 | TDB_DATA rec;
|
|---|
| 93 | NTSTATUS status;
|
|---|
| 94 |
|
|---|
| 95 | rec = tdb_fetch(db->wtdb->tdb, key);
|
|---|
| 96 | if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
|
|---|
| 97 | status = NT_STATUS_NOT_FOUND;
|
|---|
| 98 | if (data) {
|
|---|
| 99 | ZERO_STRUCTP(data);
|
|---|
| 100 | }
|
|---|
| 101 | if (header) {
|
|---|
| 102 | header->dmaster = (uint32_t)-1;
|
|---|
| 103 | header->rsn = 0;
|
|---|
| 104 | }
|
|---|
| 105 | goto done;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if (header) {
|
|---|
| 109 | *header = *(struct ctdb_ltdb_header *)rec.dptr;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (data) {
|
|---|
| 113 | data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
|
|---|
| 114 | if (data->dsize == 0) {
|
|---|
| 115 | data->dptr = NULL;
|
|---|
| 116 | } else {
|
|---|
| 117 | data->dptr = (unsigned char *)talloc_memdup(mem_ctx,
|
|---|
| 118 | rec.dptr
|
|---|
| 119 | + sizeof(struct ctdb_ltdb_header),
|
|---|
| 120 | data->dsize);
|
|---|
| 121 | if (data->dptr == NULL) {
|
|---|
| 122 | status = NT_STATUS_NO_MEMORY;
|
|---|
| 123 | goto done;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | status = NT_STATUS_OK;
|
|---|
| 129 |
|
|---|
| 130 | done:
|
|---|
| 131 | SAFE_FREE(rec.dptr);
|
|---|
| 132 | return status;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /*
|
|---|
| 136 | * Store a record together with the ctdb record header
|
|---|
| 137 | * in the local copy of the database.
|
|---|
| 138 | */
|
|---|
| 139 | static NTSTATUS db_ctdb_ltdb_store(struct db_ctdb_ctx *db,
|
|---|
| 140 | TDB_DATA key,
|
|---|
| 141 | struct ctdb_ltdb_header *header,
|
|---|
| 142 | TDB_DATA data)
|
|---|
| 143 | {
|
|---|
| 144 | TALLOC_CTX *tmp_ctx = talloc_stackframe();
|
|---|
| 145 | TDB_DATA rec;
|
|---|
| 146 | int ret;
|
|---|
| 147 |
|
|---|
| 148 | rec.dsize = data.dsize + sizeof(struct ctdb_ltdb_header);
|
|---|
| 149 | rec.dptr = (uint8_t *)talloc_size(tmp_ctx, rec.dsize);
|
|---|
| 150 |
|
|---|
| 151 | if (rec.dptr == NULL) {
|
|---|
| 152 | talloc_free(tmp_ctx);
|
|---|
| 153 | return NT_STATUS_NO_MEMORY;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | memcpy(rec.dptr, header, sizeof(struct ctdb_ltdb_header));
|
|---|
| 157 | memcpy(sizeof(struct ctdb_ltdb_header) + (uint8_t *)rec.dptr, data.dptr, data.dsize);
|
|---|
| 158 |
|
|---|
| 159 | ret = tdb_store(db->wtdb->tdb, key, rec, TDB_REPLACE);
|
|---|
| 160 |
|
|---|
| 161 | talloc_free(tmp_ctx);
|
|---|
| 162 |
|
|---|
| 163 | return (ret == 0) ? NT_STATUS_OK
|
|---|
| 164 | : tdb_error_to_ntstatus(db->wtdb->tdb);
|
|---|
| 165 |
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /*
|
|---|
| 169 | form a ctdb_rec_data record from a key/data pair
|
|---|
| 170 |
|
|---|
| 171 | note that header may be NULL. If not NULL then it is included in the data portion
|
|---|
| 172 | of the record
|
|---|
| 173 | */
|
|---|
| 174 | static struct ctdb_rec_data *db_ctdb_marshall_record(TALLOC_CTX *mem_ctx, uint32_t reqid,
|
|---|
| 175 | TDB_DATA key,
|
|---|
| 176 | struct ctdb_ltdb_header *header,
|
|---|
| 177 | TDB_DATA data)
|
|---|
| 178 | {
|
|---|
| 179 | size_t length;
|
|---|
| 180 | struct ctdb_rec_data *d;
|
|---|
| 181 |
|
|---|
| 182 | length = offsetof(struct ctdb_rec_data, data) + key.dsize +
|
|---|
| 183 | data.dsize + (header?sizeof(*header):0);
|
|---|
| 184 | d = (struct ctdb_rec_data *)talloc_size(mem_ctx, length);
|
|---|
| 185 | if (d == NULL) {
|
|---|
| 186 | return NULL;
|
|---|
| 187 | }
|
|---|
| 188 | d->length = length;
|
|---|
| 189 | d->reqid = reqid;
|
|---|
| 190 | d->keylen = key.dsize;
|
|---|
| 191 | memcpy(&d->data[0], key.dptr, key.dsize);
|
|---|
| 192 | if (header) {
|
|---|
| 193 | d->datalen = data.dsize + sizeof(*header);
|
|---|
| 194 | memcpy(&d->data[key.dsize], header, sizeof(*header));
|
|---|
| 195 | memcpy(&d->data[key.dsize+sizeof(*header)], data.dptr, data.dsize);
|
|---|
| 196 | } else {
|
|---|
| 197 | d->datalen = data.dsize;
|
|---|
| 198 | memcpy(&d->data[key.dsize], data.dptr, data.dsize);
|
|---|
| 199 | }
|
|---|
| 200 | return d;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 | /* helper function for marshalling multiple records */
|
|---|
| 205 | static struct ctdb_marshall_buffer *db_ctdb_marshall_add(TALLOC_CTX *mem_ctx,
|
|---|
| 206 | struct ctdb_marshall_buffer *m,
|
|---|
| 207 | uint64_t db_id,
|
|---|
| 208 | uint32_t reqid,
|
|---|
| 209 | TDB_DATA key,
|
|---|
| 210 | struct ctdb_ltdb_header *header,
|
|---|
| 211 | TDB_DATA data)
|
|---|
| 212 | {
|
|---|
| 213 | struct ctdb_rec_data *r;
|
|---|
| 214 | size_t m_size, r_size;
|
|---|
| 215 | struct ctdb_marshall_buffer *m2 = NULL;
|
|---|
| 216 |
|
|---|
| 217 | r = db_ctdb_marshall_record(talloc_tos(), reqid, key, header, data);
|
|---|
| 218 | if (r == NULL) {
|
|---|
| 219 | talloc_free(m);
|
|---|
| 220 | return NULL;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | if (m == NULL) {
|
|---|
| 224 | m = (struct ctdb_marshall_buffer *)talloc_zero_size(
|
|---|
| 225 | mem_ctx, offsetof(struct ctdb_marshall_buffer, data));
|
|---|
| 226 | if (m == NULL) {
|
|---|
| 227 | goto done;
|
|---|
| 228 | }
|
|---|
| 229 | m->db_id = db_id;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | m_size = talloc_get_size(m);
|
|---|
| 233 | r_size = talloc_get_size(r);
|
|---|
| 234 |
|
|---|
| 235 | m2 = (struct ctdb_marshall_buffer *)talloc_realloc_size(
|
|---|
| 236 | mem_ctx, m, m_size + r_size);
|
|---|
| 237 | if (m2 == NULL) {
|
|---|
| 238 | talloc_free(m);
|
|---|
| 239 | goto done;
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | memcpy(m_size + (uint8_t *)m2, r, r_size);
|
|---|
| 243 |
|
|---|
| 244 | m2->count++;
|
|---|
| 245 |
|
|---|
| 246 | done:
|
|---|
| 247 | talloc_free(r);
|
|---|
| 248 | return m2;
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /* we've finished marshalling, return a data blob with the marshalled records */
|
|---|
| 252 | static TDB_DATA db_ctdb_marshall_finish(struct ctdb_marshall_buffer *m)
|
|---|
| 253 | {
|
|---|
| 254 | TDB_DATA data;
|
|---|
| 255 | data.dptr = (uint8_t *)m;
|
|---|
| 256 | data.dsize = talloc_get_size(m);
|
|---|
| 257 | return data;
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | /*
|
|---|
| 261 | loop over a marshalling buffer
|
|---|
| 262 |
|
|---|
| 263 | - pass r==NULL to start
|
|---|
| 264 | - loop the number of times indicated by m->count
|
|---|
| 265 | */
|
|---|
| 266 | static struct ctdb_rec_data *db_ctdb_marshall_loop_next(struct ctdb_marshall_buffer *m, struct ctdb_rec_data *r,
|
|---|
| 267 | uint32_t *reqid,
|
|---|
| 268 | struct ctdb_ltdb_header *header,
|
|---|
| 269 | TDB_DATA *key, TDB_DATA *data)
|
|---|
| 270 | {
|
|---|
| 271 | if (r == NULL) {
|
|---|
| 272 | r = (struct ctdb_rec_data *)&m->data[0];
|
|---|
| 273 | } else {
|
|---|
| 274 | r = (struct ctdb_rec_data *)(r->length + (uint8_t *)r);
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | if (reqid != NULL) {
|
|---|
| 278 | *reqid = r->reqid;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | if (key != NULL) {
|
|---|
| 282 | key->dptr = &r->data[0];
|
|---|
| 283 | key->dsize = r->keylen;
|
|---|
| 284 | }
|
|---|
| 285 | if (data != NULL) {
|
|---|
| 286 | data->dptr = &r->data[r->keylen];
|
|---|
| 287 | data->dsize = r->datalen;
|
|---|
| 288 | if (header != NULL) {
|
|---|
| 289 | data->dptr += sizeof(*header);
|
|---|
| 290 | data->dsize -= sizeof(*header);
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | if (header != NULL) {
|
|---|
| 295 | if (r->datalen < sizeof(*header)) {
|
|---|
| 296 | return NULL;
|
|---|
| 297 | }
|
|---|
| 298 | *header = *(struct ctdb_ltdb_header *)&r->data[r->keylen];
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | return r;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /**
|
|---|
| 305 | * CTDB transaction destructor
|
|---|
| 306 | */
|
|---|
| 307 | static int db_ctdb_transaction_destructor(struct db_ctdb_transaction_handle *h)
|
|---|
| 308 | {
|
|---|
| 309 | NTSTATUS status;
|
|---|
| 310 |
|
|---|
| 311 | status = g_lock_unlock(h->ctx->lock_ctx, h->lock_name);
|
|---|
| 312 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 313 | DEBUG(0, ("g_lock_unlock failed: %s\n", nt_errstr(status)));
|
|---|
| 314 | return -1;
|
|---|
| 315 | }
|
|---|
| 316 | return 0;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /**
|
|---|
| 320 | * CTDB dbwrap API: transaction_start function
|
|---|
| 321 | * starts a transaction on a persistent database
|
|---|
| 322 | */
|
|---|
| 323 | static int db_ctdb_transaction_start(struct db_context *db)
|
|---|
| 324 | {
|
|---|
| 325 | struct db_ctdb_transaction_handle *h;
|
|---|
| 326 | NTSTATUS status;
|
|---|
| 327 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 328 | struct db_ctdb_ctx);
|
|---|
| 329 |
|
|---|
| 330 | if (!db->persistent) {
|
|---|
| 331 | DEBUG(0,("transactions not supported on non-persistent database 0x%08x\n",
|
|---|
| 332 | ctx->db_id));
|
|---|
| 333 | return -1;
|
|---|
| 334 | }
|
|---|
| 335 |
|
|---|
| 336 | if (ctx->transaction) {
|
|---|
| 337 | ctx->transaction->nesting++;
|
|---|
| 338 | return 0;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | h = talloc_zero(db, struct db_ctdb_transaction_handle);
|
|---|
| 342 | if (h == NULL) {
|
|---|
| 343 | DEBUG(0,(__location__ " oom for transaction handle\n"));
|
|---|
| 344 | return -1;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | h->ctx = ctx;
|
|---|
| 348 |
|
|---|
| 349 | h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x",
|
|---|
| 350 | (unsigned int)ctx->db_id);
|
|---|
| 351 | if (h->lock_name == NULL) {
|
|---|
| 352 | DEBUG(0, ("talloc_asprintf failed\n"));
|
|---|
| 353 | TALLOC_FREE(h);
|
|---|
| 354 | return -1;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /*
|
|---|
| 358 | * Wait a day, i.e. forever...
|
|---|
| 359 | */
|
|---|
| 360 | status = g_lock_lock(ctx->lock_ctx, h->lock_name, G_LOCK_WRITE,
|
|---|
| 361 | timeval_set(86400, 0));
|
|---|
| 362 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 363 | DEBUG(0, ("g_lock_lock failed: %s\n", nt_errstr(status)));
|
|---|
| 364 | TALLOC_FREE(h);
|
|---|
| 365 | return -1;
|
|---|
| 366 | }
|
|---|
| 367 |
|
|---|
| 368 | talloc_set_destructor(h, db_ctdb_transaction_destructor);
|
|---|
| 369 |
|
|---|
| 370 | ctx->transaction = h;
|
|---|
| 371 |
|
|---|
| 372 | DEBUG(5,(__location__ " Started transaction on db 0x%08x\n", ctx->db_id));
|
|---|
| 373 |
|
|---|
| 374 | return 0;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | static bool pull_newest_from_marshall_buffer(struct ctdb_marshall_buffer *buf,
|
|---|
| 378 | TDB_DATA key,
|
|---|
| 379 | struct ctdb_ltdb_header *pheader,
|
|---|
| 380 | TALLOC_CTX *mem_ctx,
|
|---|
| 381 | TDB_DATA *pdata)
|
|---|
| 382 | {
|
|---|
| 383 | struct ctdb_rec_data *rec = NULL;
|
|---|
| 384 | struct ctdb_ltdb_header h;
|
|---|
| 385 | bool found = false;
|
|---|
| 386 | TDB_DATA data;
|
|---|
| 387 | int i;
|
|---|
| 388 |
|
|---|
| 389 | if (buf == NULL) {
|
|---|
| 390 | return false;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | ZERO_STRUCT(h);
|
|---|
| 394 | ZERO_STRUCT(data);
|
|---|
| 395 |
|
|---|
| 396 | /*
|
|---|
| 397 | * Walk the list of records written during this
|
|---|
| 398 | * transaction. If we want to read one we have already
|
|---|
| 399 | * written, return the last written sample. Thus we do not do
|
|---|
| 400 | * a "break;" for the first hit, this record might have been
|
|---|
| 401 | * overwritten later.
|
|---|
| 402 | */
|
|---|
| 403 |
|
|---|
| 404 | for (i=0; i<buf->count; i++) {
|
|---|
| 405 | TDB_DATA tkey, tdata;
|
|---|
| 406 | uint32_t reqid;
|
|---|
| 407 | struct ctdb_ltdb_header hdr;
|
|---|
| 408 |
|
|---|
| 409 | ZERO_STRUCT(hdr);
|
|---|
| 410 |
|
|---|
| 411 | rec = db_ctdb_marshall_loop_next(buf, rec, &reqid, &hdr, &tkey,
|
|---|
| 412 | &tdata);
|
|---|
| 413 | if (rec == NULL) {
|
|---|
| 414 | return false;
|
|---|
| 415 | }
|
|---|
| 416 |
|
|---|
| 417 | if (tdb_data_equal(key, tkey)) {
|
|---|
| 418 | found = true;
|
|---|
| 419 | data = tdata;
|
|---|
| 420 | h = hdr;
|
|---|
| 421 | }
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | if (!found) {
|
|---|
| 425 | return false;
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | if (pdata != NULL) {
|
|---|
| 429 | data.dptr = (uint8_t *)talloc_memdup(mem_ctx, data.dptr,
|
|---|
| 430 | data.dsize);
|
|---|
| 431 | if ((data.dsize != 0) && (data.dptr == NULL)) {
|
|---|
| 432 | return false;
|
|---|
| 433 | }
|
|---|
| 434 | *pdata = data;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | if (pheader != NULL) {
|
|---|
| 438 | *pheader = h;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | return true;
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | /*
|
|---|
| 445 | fetch a record inside a transaction
|
|---|
| 446 | */
|
|---|
| 447 | static int db_ctdb_transaction_fetch(struct db_ctdb_ctx *db,
|
|---|
| 448 | TALLOC_CTX *mem_ctx,
|
|---|
| 449 | TDB_DATA key, TDB_DATA *data)
|
|---|
| 450 | {
|
|---|
| 451 | struct db_ctdb_transaction_handle *h = db->transaction;
|
|---|
| 452 | NTSTATUS status;
|
|---|
| 453 | bool found;
|
|---|
| 454 |
|
|---|
| 455 | found = pull_newest_from_marshall_buffer(h->m_write, key, NULL,
|
|---|
| 456 | mem_ctx, data);
|
|---|
| 457 | if (found) {
|
|---|
| 458 | return 0;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | status = db_ctdb_ltdb_fetch(h->ctx, key, NULL, mem_ctx, data);
|
|---|
| 462 |
|
|---|
| 463 | if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
|
|---|
| 464 | *data = tdb_null;
|
|---|
| 465 | } else if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 466 | return -1;
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 1, key,
|
|---|
| 470 | NULL, *data);
|
|---|
| 471 | if (h->m_all == NULL) {
|
|---|
| 472 | DEBUG(0,(__location__ " Failed to add to marshalling "
|
|---|
| 473 | "record\n"));
|
|---|
| 474 | data->dsize = 0;
|
|---|
| 475 | talloc_free(data->dptr);
|
|---|
| 476 | return -1;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | return 0;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | /**
|
|---|
| 483 | * Fetch a record from a persistent database
|
|---|
| 484 | * without record locking and without an active transaction.
|
|---|
| 485 | *
|
|---|
| 486 | * This just fetches from the local database copy.
|
|---|
| 487 | * Since the databases are kept in syc cluster-wide,
|
|---|
| 488 | * there is no point in doing a ctdb call to fetch the
|
|---|
| 489 | * record from the lmaster. It does even harm since migration
|
|---|
| 490 | * of records bump their RSN and hence render the persistent
|
|---|
| 491 | * database inconsistent.
|
|---|
| 492 | */
|
|---|
| 493 | static int db_ctdb_fetch_persistent(struct db_ctdb_ctx *db,
|
|---|
| 494 | TALLOC_CTX *mem_ctx,
|
|---|
| 495 | TDB_DATA key, TDB_DATA *data)
|
|---|
| 496 | {
|
|---|
| 497 | NTSTATUS status;
|
|---|
| 498 |
|
|---|
| 499 | status = db_ctdb_ltdb_fetch(db, key, NULL, mem_ctx, data);
|
|---|
| 500 |
|
|---|
| 501 | if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
|
|---|
| 502 | *data = tdb_null;
|
|---|
| 503 | } else if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 504 | return -1;
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | return 0;
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag);
|
|---|
| 511 | static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec);
|
|---|
| 512 |
|
|---|
| 513 | static struct db_record *db_ctdb_fetch_locked_transaction(struct db_ctdb_ctx *ctx,
|
|---|
| 514 | TALLOC_CTX *mem_ctx,
|
|---|
| 515 | TDB_DATA key)
|
|---|
| 516 | {
|
|---|
| 517 | struct db_record *result;
|
|---|
| 518 | TDB_DATA ctdb_data;
|
|---|
| 519 |
|
|---|
| 520 | if (!(result = talloc(mem_ctx, struct db_record))) {
|
|---|
| 521 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 522 | return NULL;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | result->private_data = ctx->transaction;
|
|---|
| 526 |
|
|---|
| 527 | result->key.dsize = key.dsize;
|
|---|
| 528 | result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
|
|---|
| 529 | if (result->key.dptr == NULL) {
|
|---|
| 530 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 531 | TALLOC_FREE(result);
|
|---|
| 532 | return NULL;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | result->store = db_ctdb_store_transaction;
|
|---|
| 536 | result->delete_rec = db_ctdb_delete_transaction;
|
|---|
| 537 |
|
|---|
| 538 | if (pull_newest_from_marshall_buffer(ctx->transaction->m_write, key,
|
|---|
| 539 | NULL, result, &result->value)) {
|
|---|
| 540 | return result;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
|
|---|
| 544 | if (ctdb_data.dptr == NULL) {
|
|---|
| 545 | /* create the record */
|
|---|
| 546 | result->value = tdb_null;
|
|---|
| 547 | return result;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | result->value.dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
|
|---|
| 551 | result->value.dptr = NULL;
|
|---|
| 552 |
|
|---|
| 553 | if ((result->value.dsize != 0)
|
|---|
| 554 | && !(result->value.dptr = (uint8 *)talloc_memdup(
|
|---|
| 555 | result, ctdb_data.dptr + sizeof(struct ctdb_ltdb_header),
|
|---|
| 556 | result->value.dsize))) {
|
|---|
| 557 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 558 | TALLOC_FREE(result);
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | SAFE_FREE(ctdb_data.dptr);
|
|---|
| 562 |
|
|---|
| 563 | return result;
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | static int db_ctdb_record_destructor(struct db_record **recp)
|
|---|
| 567 | {
|
|---|
| 568 | struct db_record *rec = talloc_get_type_abort(*recp, struct db_record);
|
|---|
| 569 | struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
|
|---|
| 570 | rec->private_data, struct db_ctdb_transaction_handle);
|
|---|
| 571 | int ret = h->ctx->db->transaction_commit(h->ctx->db);
|
|---|
| 572 | if (ret != 0) {
|
|---|
| 573 | DEBUG(0,(__location__ " transaction_commit failed\n"));
|
|---|
| 574 | }
|
|---|
| 575 | return 0;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | /*
|
|---|
| 579 | auto-create a transaction for persistent databases
|
|---|
| 580 | */
|
|---|
| 581 | static struct db_record *db_ctdb_fetch_locked_persistent(struct db_ctdb_ctx *ctx,
|
|---|
| 582 | TALLOC_CTX *mem_ctx,
|
|---|
| 583 | TDB_DATA key)
|
|---|
| 584 | {
|
|---|
| 585 | int res;
|
|---|
| 586 | struct db_record *rec, **recp;
|
|---|
| 587 |
|
|---|
| 588 | res = db_ctdb_transaction_start(ctx->db);
|
|---|
| 589 | if (res == -1) {
|
|---|
| 590 | return NULL;
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | rec = db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
|
|---|
| 594 | if (rec == NULL) {
|
|---|
| 595 | ctx->db->transaction_cancel(ctx->db);
|
|---|
| 596 | return NULL;
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | /* destroy this transaction when we release the lock */
|
|---|
| 600 | recp = talloc(rec, struct db_record *);
|
|---|
| 601 | if (recp == NULL) {
|
|---|
| 602 | ctx->db->transaction_cancel(ctx->db);
|
|---|
| 603 | talloc_free(rec);
|
|---|
| 604 | return NULL;
|
|---|
| 605 | }
|
|---|
| 606 | *recp = rec;
|
|---|
| 607 | talloc_set_destructor(recp, db_ctdb_record_destructor);
|
|---|
| 608 | return rec;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 | /*
|
|---|
| 613 | stores a record inside a transaction
|
|---|
| 614 | */
|
|---|
| 615 | static NTSTATUS db_ctdb_transaction_store(struct db_ctdb_transaction_handle *h,
|
|---|
| 616 | TDB_DATA key, TDB_DATA data)
|
|---|
| 617 | {
|
|---|
| 618 | TALLOC_CTX *tmp_ctx = talloc_new(h);
|
|---|
| 619 | TDB_DATA rec;
|
|---|
| 620 | struct ctdb_ltdb_header header;
|
|---|
| 621 |
|
|---|
| 622 | ZERO_STRUCT(header);
|
|---|
| 623 |
|
|---|
| 624 | /* we need the header so we can update the RSN */
|
|---|
| 625 |
|
|---|
| 626 | if (!pull_newest_from_marshall_buffer(h->m_write, key, &header,
|
|---|
| 627 | NULL, NULL)) {
|
|---|
| 628 |
|
|---|
| 629 | rec = tdb_fetch(h->ctx->wtdb->tdb, key);
|
|---|
| 630 |
|
|---|
| 631 | if (rec.dptr != NULL) {
|
|---|
| 632 | memcpy(&header, rec.dptr,
|
|---|
| 633 | sizeof(struct ctdb_ltdb_header));
|
|---|
| 634 | rec.dsize -= sizeof(struct ctdb_ltdb_header);
|
|---|
| 635 |
|
|---|
| 636 | /*
|
|---|
| 637 | * a special case, we are writing the same
|
|---|
| 638 | * data that is there now
|
|---|
| 639 | */
|
|---|
| 640 | if (data.dsize == rec.dsize &&
|
|---|
| 641 | memcmp(data.dptr,
|
|---|
| 642 | rec.dptr + sizeof(struct ctdb_ltdb_header),
|
|---|
| 643 | data.dsize) == 0) {
|
|---|
| 644 | SAFE_FREE(rec.dptr);
|
|---|
| 645 | talloc_free(tmp_ctx);
|
|---|
| 646 | return NT_STATUS_OK;
|
|---|
| 647 | }
|
|---|
| 648 | }
|
|---|
| 649 | SAFE_FREE(rec.dptr);
|
|---|
| 650 | }
|
|---|
| 651 |
|
|---|
| 652 | header.dmaster = get_my_vnn();
|
|---|
| 653 | header.rsn++;
|
|---|
| 654 |
|
|---|
| 655 | h->m_all = db_ctdb_marshall_add(h, h->m_all, h->ctx->db_id, 0, key,
|
|---|
| 656 | NULL, data);
|
|---|
| 657 | if (h->m_all == NULL) {
|
|---|
| 658 | DEBUG(0,(__location__ " Failed to add to marshalling "
|
|---|
| 659 | "record\n"));
|
|---|
| 660 | talloc_free(tmp_ctx);
|
|---|
| 661 | return NT_STATUS_NO_MEMORY;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | h->m_write = db_ctdb_marshall_add(h, h->m_write, h->ctx->db_id, 0, key, &header, data);
|
|---|
| 665 | if (h->m_write == NULL) {
|
|---|
| 666 | DEBUG(0,(__location__ " Failed to add to marshalling record\n"));
|
|---|
| 667 | talloc_free(tmp_ctx);
|
|---|
| 668 | return NT_STATUS_NO_MEMORY;
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | talloc_free(tmp_ctx);
|
|---|
| 672 | return NT_STATUS_OK;
|
|---|
| 673 | }
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 | /*
|
|---|
| 677 | a record store inside a transaction
|
|---|
| 678 | */
|
|---|
| 679 | static NTSTATUS db_ctdb_store_transaction(struct db_record *rec, TDB_DATA data, int flag)
|
|---|
| 680 | {
|
|---|
| 681 | struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
|
|---|
| 682 | rec->private_data, struct db_ctdb_transaction_handle);
|
|---|
| 683 | NTSTATUS status;
|
|---|
| 684 |
|
|---|
| 685 | status = db_ctdb_transaction_store(h, rec->key, data);
|
|---|
| 686 | return status;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | /*
|
|---|
| 690 | a record delete inside a transaction
|
|---|
| 691 | */
|
|---|
| 692 | static NTSTATUS db_ctdb_delete_transaction(struct db_record *rec)
|
|---|
| 693 | {
|
|---|
| 694 | struct db_ctdb_transaction_handle *h = talloc_get_type_abort(
|
|---|
| 695 | rec->private_data, struct db_ctdb_transaction_handle);
|
|---|
| 696 | NTSTATUS status;
|
|---|
| 697 |
|
|---|
| 698 | status = db_ctdb_transaction_store(h, rec->key, tdb_null);
|
|---|
| 699 | return status;
|
|---|
| 700 | }
|
|---|
| 701 |
|
|---|
| 702 | /**
|
|---|
| 703 | * Fetch the db sequence number of a persistent db directly from the db.
|
|---|
| 704 | */
|
|---|
| 705 | static NTSTATUS db_ctdb_fetch_db_seqnum_from_db(struct db_ctdb_ctx *db,
|
|---|
| 706 | uint64_t *seqnum)
|
|---|
| 707 | {
|
|---|
| 708 | NTSTATUS status;
|
|---|
| 709 | const char *keyname = CTDB_DB_SEQNUM_KEY;
|
|---|
| 710 | TDB_DATA key;
|
|---|
| 711 | TDB_DATA data;
|
|---|
| 712 | struct ctdb_ltdb_header header;
|
|---|
| 713 | TALLOC_CTX *mem_ctx = talloc_stackframe();
|
|---|
| 714 |
|
|---|
| 715 | if (seqnum == NULL) {
|
|---|
| 716 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 717 | }
|
|---|
| 718 |
|
|---|
| 719 | key = string_term_tdb_data(keyname);
|
|---|
| 720 |
|
|---|
| 721 | status = db_ctdb_ltdb_fetch(db, key, &header, mem_ctx, &data);
|
|---|
| 722 | if (!NT_STATUS_IS_OK(status) &&
|
|---|
| 723 | !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND))
|
|---|
| 724 | {
|
|---|
| 725 | goto done;
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | status = NT_STATUS_OK;
|
|---|
| 729 |
|
|---|
| 730 | if (data.dsize != sizeof(uint64_t)) {
|
|---|
| 731 | *seqnum = 0;
|
|---|
| 732 | goto done;
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | *seqnum = *(uint64_t *)data.dptr;
|
|---|
| 736 |
|
|---|
| 737 | done:
|
|---|
| 738 | TALLOC_FREE(mem_ctx);
|
|---|
| 739 | return status;
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | /**
|
|---|
| 743 | * Store the database sequence number inside a transaction.
|
|---|
| 744 | */
|
|---|
| 745 | static NTSTATUS db_ctdb_store_db_seqnum(struct db_ctdb_transaction_handle *h,
|
|---|
| 746 | uint64_t seqnum)
|
|---|
| 747 | {
|
|---|
| 748 | NTSTATUS status;
|
|---|
| 749 | const char *keyname = CTDB_DB_SEQNUM_KEY;
|
|---|
| 750 | TDB_DATA key;
|
|---|
| 751 | TDB_DATA data;
|
|---|
| 752 |
|
|---|
| 753 | key = string_term_tdb_data(keyname);
|
|---|
| 754 |
|
|---|
| 755 | data.dptr = (uint8_t *)&seqnum;
|
|---|
| 756 | data.dsize = sizeof(uint64_t);
|
|---|
| 757 |
|
|---|
| 758 | status = db_ctdb_transaction_store(h, key, data);
|
|---|
| 759 |
|
|---|
| 760 | return status;
|
|---|
| 761 | }
|
|---|
| 762 |
|
|---|
| 763 | /*
|
|---|
| 764 | commit a transaction
|
|---|
| 765 | */
|
|---|
| 766 | static int db_ctdb_transaction_commit(struct db_context *db)
|
|---|
| 767 | {
|
|---|
| 768 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 769 | struct db_ctdb_ctx);
|
|---|
| 770 | NTSTATUS rets;
|
|---|
| 771 | int status;
|
|---|
| 772 | struct db_ctdb_transaction_handle *h = ctx->transaction;
|
|---|
| 773 | uint64_t old_seqnum, new_seqnum;
|
|---|
| 774 | int ret;
|
|---|
| 775 |
|
|---|
| 776 | if (h == NULL) {
|
|---|
| 777 | DEBUG(0,(__location__ " transaction commit with no open transaction on db 0x%08x\n", ctx->db_id));
|
|---|
| 778 | return -1;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | if (h->nested_cancel) {
|
|---|
| 782 | db->transaction_cancel(db);
|
|---|
| 783 | DEBUG(5,(__location__ " Failed transaction commit after nested cancel\n"));
|
|---|
| 784 | return -1;
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | if (h->nesting != 0) {
|
|---|
| 788 | h->nesting--;
|
|---|
| 789 | return 0;
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | if (h->m_write == NULL) {
|
|---|
| 793 | /*
|
|---|
| 794 | * No changes were made, so don't change the seqnum,
|
|---|
| 795 | * don't push to other node, just exit with success.
|
|---|
| 796 | */
|
|---|
| 797 | ret = 0;
|
|---|
| 798 | goto done;
|
|---|
| 799 | }
|
|---|
| 800 |
|
|---|
| 801 | DEBUG(5,(__location__ " Commit transaction on db 0x%08x\n", ctx->db_id));
|
|---|
| 802 |
|
|---|
| 803 | /*
|
|---|
| 804 | * As the last db action before committing, bump the database sequence
|
|---|
| 805 | * number. Note that this undoes all changes to the seqnum records
|
|---|
| 806 | * performed under the transaction. This record is not meant to be
|
|---|
| 807 | * modified by user interaction. It is for internal use only...
|
|---|
| 808 | */
|
|---|
| 809 | rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &old_seqnum);
|
|---|
| 810 | if (!NT_STATUS_IS_OK(rets)) {
|
|---|
| 811 | DEBUG(1, (__location__ " failed to fetch the db sequence number "
|
|---|
| 812 | "in transaction commit on db 0x%08x\n", ctx->db_id));
|
|---|
| 813 | ret = -1;
|
|---|
| 814 | goto done;
|
|---|
| 815 | }
|
|---|
| 816 |
|
|---|
| 817 | new_seqnum = old_seqnum + 1;
|
|---|
| 818 |
|
|---|
| 819 | rets = db_ctdb_store_db_seqnum(h, new_seqnum);
|
|---|
| 820 | if (!NT_STATUS_IS_OK(rets)) {
|
|---|
| 821 | DEBUG(1, (__location__ "failed to store the db sequence number "
|
|---|
| 822 | " in transaction commit on db 0x%08x\n", ctx->db_id));
|
|---|
| 823 | ret = -1;
|
|---|
| 824 | goto done;
|
|---|
| 825 | }
|
|---|
| 826 |
|
|---|
| 827 | again:
|
|---|
| 828 | /* tell ctdbd to commit to the other nodes */
|
|---|
| 829 | rets = ctdbd_control_local(messaging_ctdbd_connection(),
|
|---|
| 830 | CTDB_CONTROL_TRANS3_COMMIT,
|
|---|
| 831 | h->ctx->db_id, 0,
|
|---|
| 832 | db_ctdb_marshall_finish(h->m_write),
|
|---|
| 833 | NULL, NULL, &status);
|
|---|
| 834 | if (!NT_STATUS_IS_OK(rets) || status != 0) {
|
|---|
| 835 | /*
|
|---|
| 836 | * The TRANS3_COMMIT control should only possibly fail when a
|
|---|
| 837 | * recovery has been running concurrently. In any case, the db
|
|---|
| 838 | * will be the same on all nodes, either the new copy or the
|
|---|
| 839 | * old copy. This can be detected by comparing the old and new
|
|---|
| 840 | * local sequence numbers.
|
|---|
| 841 | */
|
|---|
| 842 | rets = db_ctdb_fetch_db_seqnum_from_db(ctx, &new_seqnum);
|
|---|
| 843 | if (!NT_STATUS_IS_OK(rets)) {
|
|---|
| 844 | DEBUG(1, (__location__ " failed to refetch db sequence "
|
|---|
| 845 | "number after failed TRANS3_COMMIT\n"));
|
|---|
| 846 | ret = -1;
|
|---|
| 847 | goto done;
|
|---|
| 848 | }
|
|---|
| 849 |
|
|---|
| 850 | if (new_seqnum == old_seqnum) {
|
|---|
| 851 | /* Recovery prevented all our changes: retry. */
|
|---|
| 852 | goto again;
|
|---|
| 853 | } else if (new_seqnum != (old_seqnum + 1)) {
|
|---|
| 854 | DEBUG(0, (__location__ " ERROR: new_seqnum[%lu] != "
|
|---|
| 855 | "old_seqnum[%lu] + (0 or 1) after failed "
|
|---|
| 856 | "TRANS3_COMMIT - this should not happen!\n",
|
|---|
| 857 | (unsigned long)new_seqnum,
|
|---|
| 858 | (unsigned long)old_seqnum));
|
|---|
| 859 | ret = -1;
|
|---|
| 860 | goto done;
|
|---|
| 861 | }
|
|---|
| 862 | /*
|
|---|
| 863 | * Recovery propagated our changes to all nodes, completing
|
|---|
| 864 | * our commit for us - succeed.
|
|---|
| 865 | */
|
|---|
| 866 | }
|
|---|
| 867 |
|
|---|
| 868 | ret = 0;
|
|---|
| 869 |
|
|---|
| 870 | done:
|
|---|
| 871 | h->ctx->transaction = NULL;
|
|---|
| 872 | talloc_free(h);
|
|---|
| 873 | return ret;
|
|---|
| 874 | }
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 | /*
|
|---|
| 878 | cancel a transaction
|
|---|
| 879 | */
|
|---|
| 880 | static int db_ctdb_transaction_cancel(struct db_context *db)
|
|---|
| 881 | {
|
|---|
| 882 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 883 | struct db_ctdb_ctx);
|
|---|
| 884 | struct db_ctdb_transaction_handle *h = ctx->transaction;
|
|---|
| 885 |
|
|---|
| 886 | if (h == NULL) {
|
|---|
| 887 | DEBUG(0,(__location__ " transaction cancel with no open transaction on db 0x%08x\n", ctx->db_id));
|
|---|
| 888 | return -1;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | if (h->nesting != 0) {
|
|---|
| 892 | h->nesting--;
|
|---|
| 893 | h->nested_cancel = true;
|
|---|
| 894 | return 0;
|
|---|
| 895 | }
|
|---|
| 896 |
|
|---|
| 897 | DEBUG(5,(__location__ " Cancel transaction on db 0x%08x\n", ctx->db_id));
|
|---|
| 898 |
|
|---|
| 899 | ctx->transaction = NULL;
|
|---|
| 900 | talloc_free(h);
|
|---|
| 901 | return 0;
|
|---|
| 902 | }
|
|---|
| 903 |
|
|---|
| 904 |
|
|---|
| 905 | static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
|
|---|
| 906 | {
|
|---|
| 907 | struct db_ctdb_rec *crec = talloc_get_type_abort(
|
|---|
| 908 | rec->private_data, struct db_ctdb_rec);
|
|---|
| 909 |
|
|---|
| 910 | return db_ctdb_ltdb_store(crec->ctdb_ctx, rec->key, &(crec->header), data);
|
|---|
| 911 | }
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 | #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
|
|---|
| 916 | static NTSTATUS db_ctdb_send_schedule_for_deletion(struct db_record *rec)
|
|---|
| 917 | {
|
|---|
| 918 | NTSTATUS status;
|
|---|
| 919 | struct ctdb_control_schedule_for_deletion *dd;
|
|---|
| 920 | TDB_DATA indata;
|
|---|
| 921 | int cstatus;
|
|---|
| 922 | struct db_ctdb_rec *crec = talloc_get_type_abort(
|
|---|
| 923 | rec->private_data, struct db_ctdb_rec);
|
|---|
| 924 |
|
|---|
| 925 | indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + rec->key.dsize;
|
|---|
| 926 | indata.dptr = talloc_zero_array(crec, uint8_t, indata.dsize);
|
|---|
| 927 | if (indata.dptr == NULL) {
|
|---|
| 928 | DEBUG(0, (__location__ " talloc failed!\n"));
|
|---|
| 929 | return NT_STATUS_NO_MEMORY;
|
|---|
| 930 | }
|
|---|
| 931 |
|
|---|
| 932 | dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
|
|---|
| 933 | dd->db_id = crec->ctdb_ctx->db_id;
|
|---|
| 934 | dd->hdr = crec->header;
|
|---|
| 935 | dd->keylen = rec->key.dsize;
|
|---|
| 936 | memcpy(dd->key, rec->key.dptr, rec->key.dsize);
|
|---|
| 937 |
|
|---|
| 938 | status = ctdbd_control_local(messaging_ctdbd_connection(),
|
|---|
| 939 | CTDB_CONTROL_SCHEDULE_FOR_DELETION,
|
|---|
| 940 | crec->ctdb_ctx->db_id,
|
|---|
| 941 | CTDB_CTRL_FLAG_NOREPLY, /* flags */
|
|---|
| 942 | indata,
|
|---|
| 943 | NULL, /* outdata */
|
|---|
| 944 | NULL, /* errmsg */
|
|---|
| 945 | &cstatus);
|
|---|
| 946 | talloc_free(indata.dptr);
|
|---|
| 947 |
|
|---|
| 948 | if (!NT_STATUS_IS_OK(status) || cstatus != 0) {
|
|---|
| 949 | DEBUG(1, (__location__ " Error sending local control "
|
|---|
| 950 | "SCHEDULE_FOR_DELETION: %s, cstatus = %d\n",
|
|---|
| 951 | nt_errstr(status), cstatus));
|
|---|
| 952 | if (NT_STATUS_IS_OK(status)) {
|
|---|
| 953 | status = NT_STATUS_UNSUCCESSFUL;
|
|---|
| 954 | }
|
|---|
| 955 | }
|
|---|
| 956 |
|
|---|
| 957 | return status;
|
|---|
| 958 | }
|
|---|
| 959 | #endif
|
|---|
| 960 |
|
|---|
| 961 | static NTSTATUS db_ctdb_delete(struct db_record *rec)
|
|---|
| 962 | {
|
|---|
| 963 | TDB_DATA data;
|
|---|
| 964 | NTSTATUS status;
|
|---|
| 965 |
|
|---|
| 966 | /*
|
|---|
| 967 | * We have to store the header with empty data. TODO: Fix the
|
|---|
| 968 | * tdb-level cleanup
|
|---|
| 969 | */
|
|---|
| 970 |
|
|---|
| 971 | ZERO_STRUCT(data);
|
|---|
| 972 |
|
|---|
| 973 | status = db_ctdb_store(rec, data, 0);
|
|---|
| 974 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 975 | return status;
|
|---|
| 976 | }
|
|---|
| 977 |
|
|---|
| 978 | #ifdef HAVE_CTDB_CONTROL_SCHEDULE_FOR_DELETION_DECL
|
|---|
| 979 | status = db_ctdb_send_schedule_for_deletion(rec);
|
|---|
| 980 | #endif
|
|---|
| 981 |
|
|---|
| 982 | return status;
|
|---|
| 983 | }
|
|---|
| 984 |
|
|---|
| 985 | static int db_ctdb_record_destr(struct db_record* data)
|
|---|
| 986 | {
|
|---|
| 987 | struct db_ctdb_rec *crec = talloc_get_type_abort(
|
|---|
| 988 | data->private_data, struct db_ctdb_rec);
|
|---|
| 989 | int threshold;
|
|---|
| 990 |
|
|---|
| 991 | DEBUG(10, (DEBUGLEVEL > 10
|
|---|
| 992 | ? "Unlocking db %u key %s\n"
|
|---|
| 993 | : "Unlocking db %u key %.20s\n",
|
|---|
| 994 | (int)crec->ctdb_ctx->db_id,
|
|---|
| 995 | hex_encode_talloc(data, (unsigned char *)data->key.dptr,
|
|---|
| 996 | data->key.dsize)));
|
|---|
| 997 |
|
|---|
| 998 | if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
|
|---|
| 999 | DEBUG(0, ("tdb_chainunlock failed\n"));
|
|---|
| 1000 | return -1;
|
|---|
| 1001 | }
|
|---|
| 1002 |
|
|---|
| 1003 | threshold = lp_ctdb_locktime_warn_threshold();
|
|---|
| 1004 | if (threshold != 0) {
|
|---|
| 1005 | double timediff = timeval_elapsed(&crec->lock_time);
|
|---|
| 1006 | if ((timediff * 1000) > threshold) {
|
|---|
| 1007 | DEBUG(0, ("Held tdb lock %f seconds\n", timediff));
|
|---|
| 1008 | }
|
|---|
| 1009 | }
|
|---|
| 1010 |
|
|---|
| 1011 | return 0;
|
|---|
| 1012 | }
|
|---|
| 1013 |
|
|---|
| 1014 | static struct db_record *fetch_locked_internal(struct db_ctdb_ctx *ctx,
|
|---|
| 1015 | TALLOC_CTX *mem_ctx,
|
|---|
| 1016 | TDB_DATA key)
|
|---|
| 1017 | {
|
|---|
| 1018 | struct db_record *result;
|
|---|
| 1019 | struct db_ctdb_rec *crec;
|
|---|
| 1020 | NTSTATUS status;
|
|---|
| 1021 | TDB_DATA ctdb_data;
|
|---|
| 1022 | int migrate_attempts = 0;
|
|---|
| 1023 |
|
|---|
| 1024 | if (!(result = talloc(mem_ctx, struct db_record))) {
|
|---|
| 1025 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1026 | return NULL;
|
|---|
| 1027 | }
|
|---|
| 1028 |
|
|---|
| 1029 | if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
|
|---|
| 1030 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1031 | TALLOC_FREE(result);
|
|---|
| 1032 | return NULL;
|
|---|
| 1033 | }
|
|---|
| 1034 |
|
|---|
| 1035 | result->private_data = (void *)crec;
|
|---|
| 1036 | crec->ctdb_ctx = ctx;
|
|---|
| 1037 |
|
|---|
| 1038 | result->key.dsize = key.dsize;
|
|---|
| 1039 | result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
|
|---|
| 1040 | if (result->key.dptr == NULL) {
|
|---|
| 1041 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1042 | TALLOC_FREE(result);
|
|---|
| 1043 | return NULL;
|
|---|
| 1044 | }
|
|---|
| 1045 |
|
|---|
| 1046 | /*
|
|---|
| 1047 | * Do a blocking lock on the record
|
|---|
| 1048 | */
|
|---|
| 1049 | again:
|
|---|
| 1050 |
|
|---|
| 1051 | if (DEBUGLEVEL >= 10) {
|
|---|
| 1052 | char *keystr = hex_encode_talloc(result, key.dptr, key.dsize);
|
|---|
| 1053 | DEBUG(10, (DEBUGLEVEL > 10
|
|---|
| 1054 | ? "Locking db %u key %s\n"
|
|---|
| 1055 | : "Locking db %u key %.20s\n",
|
|---|
| 1056 | (int)crec->ctdb_ctx->db_id, keystr));
|
|---|
| 1057 | TALLOC_FREE(keystr);
|
|---|
| 1058 | }
|
|---|
| 1059 |
|
|---|
| 1060 | if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
|
|---|
| 1061 | DEBUG(3, ("tdb_chainlock failed\n"));
|
|---|
| 1062 | TALLOC_FREE(result);
|
|---|
| 1063 | return NULL;
|
|---|
| 1064 | }
|
|---|
| 1065 |
|
|---|
| 1066 | result->store = db_ctdb_store;
|
|---|
| 1067 | result->delete_rec = db_ctdb_delete;
|
|---|
| 1068 | talloc_set_destructor(result, db_ctdb_record_destr);
|
|---|
| 1069 |
|
|---|
| 1070 | ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
|
|---|
| 1071 |
|
|---|
| 1072 | /*
|
|---|
| 1073 | * See if we have a valid record and we are the dmaster. If so, we can
|
|---|
| 1074 | * take the shortcut and just return it.
|
|---|
| 1075 | */
|
|---|
| 1076 |
|
|---|
| 1077 | if ((ctdb_data.dptr == NULL) ||
|
|---|
| 1078 | (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
|
|---|
| 1079 | ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
|
|---|
| 1080 | #if 0
|
|---|
| 1081 | || (random() % 2 != 0)
|
|---|
| 1082 | #endif
|
|---|
| 1083 | ) {
|
|---|
| 1084 | SAFE_FREE(ctdb_data.dptr);
|
|---|
| 1085 | tdb_chainunlock(ctx->wtdb->tdb, key);
|
|---|
| 1086 | talloc_set_destructor(result, NULL);
|
|---|
| 1087 |
|
|---|
| 1088 | migrate_attempts += 1;
|
|---|
| 1089 |
|
|---|
| 1090 | DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
|
|---|
| 1091 | ctdb_data.dptr, ctdb_data.dptr ?
|
|---|
| 1092 | ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
|
|---|
| 1093 | get_my_vnn()));
|
|---|
| 1094 |
|
|---|
| 1095 | status = ctdbd_migrate(messaging_ctdbd_connection(), ctx->db_id,
|
|---|
| 1096 | key);
|
|---|
| 1097 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1098 | DEBUG(5, ("ctdb_migrate failed: %s\n",
|
|---|
| 1099 | nt_errstr(status)));
|
|---|
| 1100 | TALLOC_FREE(result);
|
|---|
| 1101 | return NULL;
|
|---|
| 1102 | }
|
|---|
| 1103 | /* now its migrated, try again */
|
|---|
| 1104 | goto again;
|
|---|
| 1105 | }
|
|---|
| 1106 |
|
|---|
| 1107 | if (migrate_attempts > 10) {
|
|---|
| 1108 | DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
|
|---|
| 1109 | migrate_attempts));
|
|---|
| 1110 | }
|
|---|
| 1111 |
|
|---|
| 1112 | GetTimeOfDay(&crec->lock_time);
|
|---|
| 1113 |
|
|---|
| 1114 | memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
|
|---|
| 1115 |
|
|---|
| 1116 | result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
|
|---|
| 1117 | result->value.dptr = NULL;
|
|---|
| 1118 |
|
|---|
| 1119 | if ((result->value.dsize != 0)
|
|---|
| 1120 | && !(result->value.dptr = (uint8 *)talloc_memdup(
|
|---|
| 1121 | result, ctdb_data.dptr + sizeof(crec->header),
|
|---|
| 1122 | result->value.dsize))) {
|
|---|
| 1123 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1124 | TALLOC_FREE(result);
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | SAFE_FREE(ctdb_data.dptr);
|
|---|
| 1128 |
|
|---|
| 1129 | return result;
|
|---|
| 1130 | }
|
|---|
| 1131 |
|
|---|
| 1132 | static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
|
|---|
| 1133 | TALLOC_CTX *mem_ctx,
|
|---|
| 1134 | TDB_DATA key)
|
|---|
| 1135 | {
|
|---|
| 1136 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 1137 | struct db_ctdb_ctx);
|
|---|
| 1138 |
|
|---|
| 1139 | if (ctx->transaction != NULL) {
|
|---|
| 1140 | return db_ctdb_fetch_locked_transaction(ctx, mem_ctx, key);
|
|---|
| 1141 | }
|
|---|
| 1142 |
|
|---|
| 1143 | if (db->persistent) {
|
|---|
| 1144 | return db_ctdb_fetch_locked_persistent(ctx, mem_ctx, key);
|
|---|
| 1145 | }
|
|---|
| 1146 |
|
|---|
| 1147 | return fetch_locked_internal(ctx, mem_ctx, key);
|
|---|
| 1148 | }
|
|---|
| 1149 |
|
|---|
| 1150 | /*
|
|---|
| 1151 | fetch (unlocked, no migration) operation on ctdb
|
|---|
| 1152 | */
|
|---|
| 1153 | static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
|
|---|
| 1154 | TDB_DATA key, TDB_DATA *data)
|
|---|
| 1155 | {
|
|---|
| 1156 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 1157 | struct db_ctdb_ctx);
|
|---|
| 1158 | NTSTATUS status;
|
|---|
| 1159 | TDB_DATA ctdb_data;
|
|---|
| 1160 |
|
|---|
| 1161 | if (ctx->transaction) {
|
|---|
| 1162 | return db_ctdb_transaction_fetch(ctx, mem_ctx, key, data);
|
|---|
| 1163 | }
|
|---|
| 1164 |
|
|---|
| 1165 | if (db->persistent) {
|
|---|
| 1166 | return db_ctdb_fetch_persistent(ctx, mem_ctx, key, data);
|
|---|
| 1167 | }
|
|---|
| 1168 |
|
|---|
| 1169 | /* try a direct fetch */
|
|---|
| 1170 | ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
|
|---|
| 1171 |
|
|---|
| 1172 | /*
|
|---|
| 1173 | * See if we have a valid record and we are the dmaster. If so, we can
|
|---|
| 1174 | * take the shortcut and just return it.
|
|---|
| 1175 | * we bypass the dmaster check for persistent databases
|
|---|
| 1176 | */
|
|---|
| 1177 | if ((ctdb_data.dptr != NULL) &&
|
|---|
| 1178 | (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
|
|---|
| 1179 | ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())
|
|---|
| 1180 | {
|
|---|
| 1181 | /* we are the dmaster - avoid the ctdb protocol op */
|
|---|
| 1182 |
|
|---|
| 1183 | data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
|
|---|
| 1184 | if (data->dsize == 0) {
|
|---|
| 1185 | SAFE_FREE(ctdb_data.dptr);
|
|---|
| 1186 | data->dptr = NULL;
|
|---|
| 1187 | return 0;
|
|---|
| 1188 | }
|
|---|
| 1189 |
|
|---|
| 1190 | data->dptr = (uint8 *)talloc_memdup(
|
|---|
| 1191 | mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
|
|---|
| 1192 | data->dsize);
|
|---|
| 1193 |
|
|---|
| 1194 | SAFE_FREE(ctdb_data.dptr);
|
|---|
| 1195 |
|
|---|
| 1196 | if (data->dptr == NULL) {
|
|---|
| 1197 | return -1;
|
|---|
| 1198 | }
|
|---|
| 1199 | return 0;
|
|---|
| 1200 | }
|
|---|
| 1201 |
|
|---|
| 1202 | SAFE_FREE(ctdb_data.dptr);
|
|---|
| 1203 |
|
|---|
| 1204 | /* we weren't able to get it locally - ask ctdb to fetch it for us */
|
|---|
| 1205 | status = ctdbd_fetch(messaging_ctdbd_connection(), ctx->db_id, key,
|
|---|
| 1206 | mem_ctx, data);
|
|---|
| 1207 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1208 | DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
|
|---|
| 1209 | return -1;
|
|---|
| 1210 | }
|
|---|
| 1211 |
|
|---|
| 1212 | return 0;
|
|---|
| 1213 | }
|
|---|
| 1214 |
|
|---|
| 1215 | struct traverse_state {
|
|---|
| 1216 | struct db_context *db;
|
|---|
| 1217 | int (*fn)(struct db_record *rec, void *private_data);
|
|---|
| 1218 | void *private_data;
|
|---|
| 1219 | };
|
|---|
| 1220 |
|
|---|
| 1221 | static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
|
|---|
| 1222 | {
|
|---|
| 1223 | struct traverse_state *state = (struct traverse_state *)private_data;
|
|---|
| 1224 | struct db_record *rec;
|
|---|
| 1225 | TALLOC_CTX *tmp_ctx = talloc_new(state->db);
|
|---|
| 1226 | /* we have to give them a locked record to prevent races */
|
|---|
| 1227 | rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
|
|---|
| 1228 | if (rec && rec->value.dsize > 0) {
|
|---|
| 1229 | state->fn(rec, state->private_data);
|
|---|
| 1230 | }
|
|---|
| 1231 | talloc_free(tmp_ctx);
|
|---|
| 1232 | }
|
|---|
| 1233 |
|
|---|
| 1234 | static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
|---|
| 1235 | void *private_data)
|
|---|
| 1236 | {
|
|---|
| 1237 | struct traverse_state *state = (struct traverse_state *)private_data;
|
|---|
| 1238 | struct db_record *rec;
|
|---|
| 1239 | TALLOC_CTX *tmp_ctx = talloc_new(state->db);
|
|---|
| 1240 | int ret = 0;
|
|---|
| 1241 | /* we have to give them a locked record to prevent races */
|
|---|
| 1242 | rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
|
|---|
| 1243 | if (rec && rec->value.dsize > 0) {
|
|---|
| 1244 | ret = state->fn(rec, state->private_data);
|
|---|
| 1245 | }
|
|---|
| 1246 | talloc_free(tmp_ctx);
|
|---|
| 1247 | return ret;
|
|---|
| 1248 | }
|
|---|
| 1249 |
|
|---|
| 1250 | /* wrapper to use traverse_persistent_callback with dbwrap */
|
|---|
| 1251 | static int traverse_persistent_callback_dbwrap(struct db_record *rec, void* data)
|
|---|
| 1252 | {
|
|---|
| 1253 | return traverse_persistent_callback(NULL, rec->key, rec->value, data);
|
|---|
| 1254 | }
|
|---|
| 1255 |
|
|---|
| 1256 |
|
|---|
| 1257 | static int db_ctdb_traverse(struct db_context *db,
|
|---|
| 1258 | int (*fn)(struct db_record *rec,
|
|---|
| 1259 | void *private_data),
|
|---|
| 1260 | void *private_data)
|
|---|
| 1261 | {
|
|---|
| 1262 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 1263 | struct db_ctdb_ctx);
|
|---|
| 1264 | struct traverse_state state;
|
|---|
| 1265 |
|
|---|
| 1266 | state.db = db;
|
|---|
| 1267 | state.fn = fn;
|
|---|
| 1268 | state.private_data = private_data;
|
|---|
| 1269 |
|
|---|
| 1270 | if (db->persistent) {
|
|---|
| 1271 | struct tdb_context *ltdb = ctx->wtdb->tdb;
|
|---|
| 1272 | int ret;
|
|---|
| 1273 |
|
|---|
| 1274 | /* for persistent databases we don't need to do a ctdb traverse,
|
|---|
| 1275 | we can do a faster local traverse */
|
|---|
| 1276 | ret = tdb_traverse(ltdb, traverse_persistent_callback, &state);
|
|---|
| 1277 | if (ret < 0) {
|
|---|
| 1278 | return ret;
|
|---|
| 1279 | }
|
|---|
| 1280 | if (ctx->transaction && ctx->transaction->m_write) {
|
|---|
| 1281 | /*
|
|---|
| 1282 | * we now have to handle keys not yet
|
|---|
| 1283 | * present at transaction start
|
|---|
| 1284 | */
|
|---|
| 1285 | struct db_context *newkeys = db_open_rbt(talloc_tos());
|
|---|
| 1286 | struct ctdb_marshall_buffer *mbuf = ctx->transaction->m_write;
|
|---|
| 1287 | struct ctdb_rec_data *rec=NULL;
|
|---|
| 1288 | NTSTATUS status;
|
|---|
| 1289 | int i;
|
|---|
| 1290 | int count = 0;
|
|---|
| 1291 |
|
|---|
| 1292 | if (newkeys == NULL) {
|
|---|
| 1293 | return -1;
|
|---|
| 1294 | }
|
|---|
| 1295 |
|
|---|
| 1296 | for (i=0; i<mbuf->count; i++) {
|
|---|
| 1297 | TDB_DATA key;
|
|---|
| 1298 | rec =db_ctdb_marshall_loop_next(mbuf, rec,
|
|---|
| 1299 | NULL, NULL,
|
|---|
| 1300 | &key, NULL);
|
|---|
| 1301 | SMB_ASSERT(rec != NULL);
|
|---|
| 1302 |
|
|---|
| 1303 | if (!tdb_exists(ltdb, key)) {
|
|---|
| 1304 | dbwrap_store(newkeys, key, tdb_null, 0);
|
|---|
| 1305 | }
|
|---|
| 1306 | }
|
|---|
| 1307 | status = dbwrap_traverse(newkeys,
|
|---|
| 1308 | traverse_persistent_callback_dbwrap,
|
|---|
| 1309 | &state,
|
|---|
| 1310 | &count);
|
|---|
| 1311 | talloc_free(newkeys);
|
|---|
| 1312 | if (!NT_STATUS_IS_OK(status)) {
|
|---|
| 1313 | return -1;
|
|---|
| 1314 | }
|
|---|
| 1315 | ret += count;
|
|---|
| 1316 | }
|
|---|
| 1317 | return ret;
|
|---|
| 1318 | }
|
|---|
| 1319 |
|
|---|
| 1320 |
|
|---|
| 1321 | ctdbd_traverse(ctx->db_id, traverse_callback, &state);
|
|---|
| 1322 | return 0;
|
|---|
| 1323 | }
|
|---|
| 1324 |
|
|---|
| 1325 | static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
|
|---|
| 1326 | {
|
|---|
| 1327 | return NT_STATUS_MEDIA_WRITE_PROTECTED;
|
|---|
| 1328 | }
|
|---|
| 1329 |
|
|---|
| 1330 | static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
|
|---|
| 1331 | {
|
|---|
| 1332 | return NT_STATUS_MEDIA_WRITE_PROTECTED;
|
|---|
| 1333 | }
|
|---|
| 1334 |
|
|---|
| 1335 | static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
|
|---|
| 1336 | {
|
|---|
| 1337 | struct traverse_state *state = (struct traverse_state *)private_data;
|
|---|
| 1338 | struct db_record rec;
|
|---|
| 1339 | rec.key = key;
|
|---|
| 1340 | rec.value = data;
|
|---|
| 1341 | rec.store = db_ctdb_store_deny;
|
|---|
| 1342 | rec.delete_rec = db_ctdb_delete_deny;
|
|---|
| 1343 | rec.private_data = state->db;
|
|---|
| 1344 | state->fn(&rec, state->private_data);
|
|---|
| 1345 | }
|
|---|
| 1346 |
|
|---|
| 1347 | static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
|
|---|
| 1348 | void *private_data)
|
|---|
| 1349 | {
|
|---|
| 1350 | struct traverse_state *state = (struct traverse_state *)private_data;
|
|---|
| 1351 | struct db_record rec;
|
|---|
| 1352 | rec.key = kbuf;
|
|---|
| 1353 | rec.value = dbuf;
|
|---|
| 1354 | rec.store = db_ctdb_store_deny;
|
|---|
| 1355 | rec.delete_rec = db_ctdb_delete_deny;
|
|---|
| 1356 | rec.private_data = state->db;
|
|---|
| 1357 |
|
|---|
| 1358 | if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
|
|---|
| 1359 | /* a deleted record */
|
|---|
| 1360 | return 0;
|
|---|
| 1361 | }
|
|---|
| 1362 | rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
|
|---|
| 1363 | rec.value.dptr += sizeof(struct ctdb_ltdb_header);
|
|---|
| 1364 |
|
|---|
| 1365 | return state->fn(&rec, state->private_data);
|
|---|
| 1366 | }
|
|---|
| 1367 |
|
|---|
| 1368 | static int db_ctdb_traverse_read(struct db_context *db,
|
|---|
| 1369 | int (*fn)(struct db_record *rec,
|
|---|
| 1370 | void *private_data),
|
|---|
| 1371 | void *private_data)
|
|---|
| 1372 | {
|
|---|
| 1373 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 1374 | struct db_ctdb_ctx);
|
|---|
| 1375 | struct traverse_state state;
|
|---|
| 1376 |
|
|---|
| 1377 | state.db = db;
|
|---|
| 1378 | state.fn = fn;
|
|---|
| 1379 | state.private_data = private_data;
|
|---|
| 1380 |
|
|---|
| 1381 | if (db->persistent) {
|
|---|
| 1382 | /* for persistent databases we don't need to do a ctdb traverse,
|
|---|
| 1383 | we can do a faster local traverse */
|
|---|
| 1384 | return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
|
|---|
| 1385 | }
|
|---|
| 1386 |
|
|---|
| 1387 | ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
|
|---|
| 1388 | return 0;
|
|---|
| 1389 | }
|
|---|
| 1390 |
|
|---|
| 1391 | static int db_ctdb_get_seqnum(struct db_context *db)
|
|---|
| 1392 | {
|
|---|
| 1393 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 1394 | struct db_ctdb_ctx);
|
|---|
| 1395 | return tdb_get_seqnum(ctx->wtdb->tdb);
|
|---|
| 1396 | }
|
|---|
| 1397 |
|
|---|
| 1398 | static int db_ctdb_get_flags(struct db_context *db)
|
|---|
| 1399 | {
|
|---|
| 1400 | struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
|
|---|
| 1401 | struct db_ctdb_ctx);
|
|---|
| 1402 | return tdb_get_flags(ctx->wtdb->tdb);
|
|---|
| 1403 | }
|
|---|
| 1404 |
|
|---|
| 1405 | struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
|
|---|
| 1406 | const char *name,
|
|---|
| 1407 | int hash_size, int tdb_flags,
|
|---|
| 1408 | int open_flags, mode_t mode)
|
|---|
| 1409 | {
|
|---|
| 1410 | struct db_context *result;
|
|---|
| 1411 | struct db_ctdb_ctx *db_ctdb;
|
|---|
| 1412 | char *db_path;
|
|---|
| 1413 | struct ctdbd_connection *conn;
|
|---|
| 1414 |
|
|---|
| 1415 | if (!lp_clustering()) {
|
|---|
| 1416 | DEBUG(10, ("Clustering disabled -- no ctdb\n"));
|
|---|
| 1417 | return NULL;
|
|---|
| 1418 | }
|
|---|
| 1419 |
|
|---|
| 1420 | if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
|
|---|
| 1421 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1422 | TALLOC_FREE(result);
|
|---|
| 1423 | return NULL;
|
|---|
| 1424 | }
|
|---|
| 1425 |
|
|---|
| 1426 | if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
|
|---|
| 1427 | DEBUG(0, ("talloc failed\n"));
|
|---|
| 1428 | TALLOC_FREE(result);
|
|---|
| 1429 | return NULL;
|
|---|
| 1430 | }
|
|---|
| 1431 |
|
|---|
| 1432 | db_ctdb->transaction = NULL;
|
|---|
| 1433 | db_ctdb->db = result;
|
|---|
| 1434 |
|
|---|
| 1435 | conn = messaging_ctdbd_connection();
|
|---|
| 1436 | if (conn == NULL) {
|
|---|
| 1437 | DEBUG(1, ("Could not connect to ctdb\n"));
|
|---|
| 1438 | TALLOC_FREE(result);
|
|---|
| 1439 | return NULL;
|
|---|
| 1440 | }
|
|---|
| 1441 |
|
|---|
| 1442 | if (!NT_STATUS_IS_OK(ctdbd_db_attach(conn, name, &db_ctdb->db_id, tdb_flags))) {
|
|---|
| 1443 | DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
|
|---|
| 1444 | TALLOC_FREE(result);
|
|---|
| 1445 | return NULL;
|
|---|
| 1446 | }
|
|---|
| 1447 |
|
|---|
| 1448 | db_path = ctdbd_dbpath(conn, db_ctdb, db_ctdb->db_id);
|
|---|
| 1449 |
|
|---|
| 1450 | result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
|
|---|
| 1451 |
|
|---|
| 1452 | /* only pass through specific flags */
|
|---|
| 1453 | tdb_flags &= TDB_SEQNUM;
|
|---|
| 1454 |
|
|---|
| 1455 | /* honor permissions if user has specified O_CREAT */
|
|---|
| 1456 | if (open_flags & O_CREAT) {
|
|---|
| 1457 | chmod(db_path, mode);
|
|---|
| 1458 | }
|
|---|
| 1459 |
|
|---|
| 1460 | db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
|
|---|
| 1461 | if (db_ctdb->wtdb == NULL) {
|
|---|
| 1462 | DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
|
|---|
| 1463 | TALLOC_FREE(result);
|
|---|
| 1464 | return NULL;
|
|---|
| 1465 | }
|
|---|
| 1466 | talloc_free(db_path);
|
|---|
| 1467 |
|
|---|
| 1468 | if (result->persistent) {
|
|---|
| 1469 | db_ctdb->lock_ctx = g_lock_ctx_init(db_ctdb,
|
|---|
| 1470 | ctdb_conn_msg_ctx(conn));
|
|---|
| 1471 | if (db_ctdb->lock_ctx == NULL) {
|
|---|
| 1472 | DEBUG(0, ("g_lock_ctx_init failed\n"));
|
|---|
| 1473 | TALLOC_FREE(result);
|
|---|
| 1474 | return NULL;
|
|---|
| 1475 | }
|
|---|
| 1476 | }
|
|---|
| 1477 |
|
|---|
| 1478 | result->private_data = (void *)db_ctdb;
|
|---|
| 1479 | result->fetch_locked = db_ctdb_fetch_locked;
|
|---|
| 1480 | result->fetch = db_ctdb_fetch;
|
|---|
| 1481 | result->traverse = db_ctdb_traverse;
|
|---|
| 1482 | result->traverse_read = db_ctdb_traverse_read;
|
|---|
| 1483 | result->get_seqnum = db_ctdb_get_seqnum;
|
|---|
| 1484 | result->get_flags = db_ctdb_get_flags;
|
|---|
| 1485 | result->transaction_start = db_ctdb_transaction_start;
|
|---|
| 1486 | result->transaction_commit = db_ctdb_transaction_commit;
|
|---|
| 1487 | result->transaction_cancel = db_ctdb_transaction_cancel;
|
|---|
| 1488 |
|
|---|
| 1489 | DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
|
|---|
| 1490 | name, db_ctdb->db_id));
|
|---|
| 1491 |
|
|---|
| 1492 | return result;
|
|---|
| 1493 | }
|
|---|
| 1494 | #endif
|
|---|