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