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