4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "system/filesys.h"
28 #include "common/logging.h"
30 #include "lib/tdb_wrap/tdb_wrap.h"
31 #include "lib/util/tevent_unix.h"
32 #include "lib/util/dlinklist.h"
33 #include "lib/util/debug.h"
35 #include "protocol/protocol.h"
36 #include "protocol/protocol_api.h"
37 #include "client/client_private.h"
38 #include "client/client.h"
40 static struct ctdb_db_context *client_db_handle(
41 struct ctdb_client_context *client,
44 struct ctdb_db_context *db;
46 for (db = client->db; db != NULL; db = db->next) {
47 if (strcmp(db_name, db->db_name) == 0) {
55 struct ctdb_set_db_flags_state {
56 struct tevent_context *ev;
57 struct ctdb_client_context *client;
58 struct timeval timeout;
61 bool readonly_done, sticky_done;
66 static void ctdb_set_db_flags_nodemap_done(struct tevent_req *subreq);
67 static void ctdb_set_db_flags_readonly_done(struct tevent_req *subreq);
68 static void ctdb_set_db_flags_sticky_done(struct tevent_req *subreq);
70 static struct tevent_req *ctdb_set_db_flags_send(
72 struct tevent_context *ev,
73 struct ctdb_client_context *client,
74 uint32_t destnode, struct timeval timeout,
75 uint32_t db_id, uint8_t db_flags)
77 struct tevent_req *req, *subreq;
78 struct ctdb_set_db_flags_state *state;
79 struct ctdb_req_control request;
81 req = tevent_req_create(mem_ctx, &state,
82 struct ctdb_set_db_flags_state);
87 if (! (db_flags & (CTDB_DB_FLAGS_READONLY | CTDB_DB_FLAGS_STICKY))) {
89 return tevent_req_post(req, ev);
93 state->client = client;
94 state->timeout = timeout;
96 state->db_flags = db_flags;
98 ctdb_req_control_get_nodemap(&request);
99 subreq = ctdb_client_control_send(state, ev, client, destnode, timeout,
101 if (tevent_req_nomem(subreq, req)) {
102 return tevent_req_post(req, ev);
104 tevent_req_set_callback(subreq, ctdb_set_db_flags_nodemap_done, req);
109 static void ctdb_set_db_flags_nodemap_done(struct tevent_req *subreq)
111 struct tevent_req *req = tevent_req_callback_data(
112 subreq, struct tevent_req);
113 struct ctdb_set_db_flags_state *state = tevent_req_data(
114 req, struct ctdb_set_db_flags_state);
115 struct ctdb_req_control request;
116 struct ctdb_reply_control *reply;
117 struct ctdb_node_map *nodemap;
121 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
125 ("set_db_flags: 0x%08x GET_NODEMAP failed, ret=%d\n",
127 tevent_req_error(req, ret);
131 ret = ctdb_reply_control_get_nodemap(reply, state, &nodemap);
135 ("set_db_flags: 0x%08x GET_NODEMAP parse failed, ret=%d\n",
137 tevent_req_error(req, ret);
141 state->count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
142 state, &state->pnn_list);
143 talloc_free(nodemap);
144 if (state->count <= 0) {
146 ("set_db_flags: 0x%08x no connected nodes, count=%d\n",
147 state->db_id, state->count));
148 tevent_req_error(req, ENOMEM);
152 if (state->db_flags & CTDB_DB_FLAGS_READONLY) {
153 ctdb_req_control_set_db_readonly(&request, state->db_id);
154 subreq = ctdb_client_control_multi_send(
155 state, state->ev, state->client,
156 state->pnn_list, state->count,
157 state->timeout, &request);
158 if (tevent_req_nomem(subreq, req)) {
161 tevent_req_set_callback(subreq,
162 ctdb_set_db_flags_readonly_done, req);
164 state->readonly_done = true;
167 if (state->db_flags & CTDB_DB_FLAGS_STICKY) {
168 ctdb_req_control_set_db_sticky(&request, state->db_id);
169 subreq = ctdb_client_control_multi_send(
170 state, state->ev, state->client,
171 state->pnn_list, state->count,
172 state->timeout, &request);
173 if (tevent_req_nomem(subreq, req)) {
176 tevent_req_set_callback(subreq, ctdb_set_db_flags_sticky_done,
179 state->sticky_done = true;
183 static void ctdb_set_db_flags_readonly_done(struct tevent_req *subreq)
185 struct tevent_req *req = tevent_req_callback_data(
186 subreq, struct tevent_req);
187 struct ctdb_set_db_flags_state *state = tevent_req_data(
188 req, struct ctdb_set_db_flags_state);
192 status = ctdb_client_control_multi_recv(subreq, &ret, NULL, NULL,
197 ("set_db_flags: 0x%08x SET_DB_READONLY failed, ret=%d\n",
199 tevent_req_error(req, ret);
203 state->readonly_done = true;
205 if (state->readonly_done && state->sticky_done) {
206 tevent_req_done(req);
210 static void ctdb_set_db_flags_sticky_done(struct tevent_req *subreq)
212 struct tevent_req *req = tevent_req_callback_data(
213 subreq, struct tevent_req);
214 struct ctdb_set_db_flags_state *state = tevent_req_data(
215 req, struct ctdb_set_db_flags_state);
219 status = ctdb_client_control_multi_recv(subreq, &ret, NULL, NULL,
224 ("set_db_flags: 0x%08x SET_DB_STICKY failed, ret=%d\n",
226 tevent_req_error(req, ret);
230 state->sticky_done = true;
232 if (state->readonly_done && state->sticky_done) {
233 tevent_req_done(req);
237 static bool ctdb_set_db_flags_recv(struct tevent_req *req, int *perr)
241 if (tevent_req_is_unix_error(req, &err)) {
250 struct ctdb_attach_state {
251 struct tevent_context *ev;
252 struct ctdb_client_context *client;
253 struct timeval timeout;
257 struct ctdb_db_context *db;
260 static void ctdb_attach_mutex_done(struct tevent_req *subreq);
261 static void ctdb_attach_dbid_done(struct tevent_req *subreq);
262 static void ctdb_attach_dbpath_done(struct tevent_req *subreq);
263 static void ctdb_attach_health_done(struct tevent_req *subreq);
264 static void ctdb_attach_flags_done(struct tevent_req *subreq);
266 struct tevent_req *ctdb_attach_send(TALLOC_CTX *mem_ctx,
267 struct tevent_context *ev,
268 struct ctdb_client_context *client,
269 struct timeval timeout,
270 const char *db_name, uint8_t db_flags)
272 struct tevent_req *req, *subreq;
273 struct ctdb_attach_state *state;
274 struct ctdb_req_control request;
276 req = tevent_req_create(mem_ctx, &state, struct ctdb_attach_state);
281 state->db = client_db_handle(client, db_name);
282 if (state->db != NULL) {
283 tevent_req_done(req);
284 return tevent_req_post(req, ev);
288 state->client = client;
289 state->timeout = timeout;
290 state->destnode = ctdb_client_pnn(client);
291 state->db_flags = db_flags;
293 state->db = talloc_zero(client, struct ctdb_db_context);
294 if (tevent_req_nomem(state->db, req)) {
295 return tevent_req_post(req, ev);
298 state->db->db_name = talloc_strdup(state->db, db_name);
299 if (tevent_req_nomem(state->db, req)) {
300 return tevent_req_post(req, ev);
303 if (db_flags & CTDB_DB_FLAGS_PERSISTENT) {
304 state->db->persistent = true;
307 ctdb_req_control_get_tunable(&request, "TDBMutexEnabled");
308 subreq = ctdb_client_control_send(state, ev, client,
309 ctdb_client_pnn(client), timeout,
311 if (tevent_req_nomem(subreq, req)) {
312 return tevent_req_post(req, ev);
314 tevent_req_set_callback(subreq, ctdb_attach_mutex_done, req);
319 static void ctdb_attach_mutex_done(struct tevent_req *subreq)
321 struct tevent_req *req = tevent_req_callback_data(
322 subreq, struct tevent_req);
323 struct ctdb_attach_state *state = tevent_req_data(
324 req, struct ctdb_attach_state);
325 struct ctdb_reply_control *reply;
326 struct ctdb_req_control request;
327 uint32_t mutex_enabled;
331 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
334 DEBUG(DEBUG_ERR, ("attach: %s GET_TUNABLE failed, ret=%d\n",
335 state->db->db_name, ret));
336 tevent_req_error(req, ret);
340 ret = ctdb_reply_control_get_tunable(reply, &mutex_enabled);
342 /* Treat error as mutex support not available */
346 if (state->db->persistent) {
347 state->tdb_flags = TDB_DEFAULT;
349 state->tdb_flags = (TDB_NOSYNC | TDB_INCOMPATIBLE_HASH |
351 if (mutex_enabled == 1) {
352 state->tdb_flags |= TDB_MUTEX_LOCKING;
356 if (state->db->persistent) {
357 ctdb_req_control_db_attach_persistent(&request,
361 ctdb_req_control_db_attach(&request, state->db->db_name,
365 subreq = ctdb_client_control_send(state, state->ev, state->client,
366 state->destnode, state->timeout,
368 if (tevent_req_nomem(subreq, req)) {
371 tevent_req_set_callback(subreq, ctdb_attach_dbid_done, req);
374 static void ctdb_attach_dbid_done(struct tevent_req *subreq)
376 struct tevent_req *req = tevent_req_callback_data(
377 subreq, struct tevent_req);
378 struct ctdb_attach_state *state = tevent_req_data(
379 req, struct ctdb_attach_state);
380 struct ctdb_req_control request;
381 struct ctdb_reply_control *reply;
385 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
388 DEBUG(DEBUG_ERR, ("attach: %s %s failed, ret=%d\n",
390 (state->db->persistent
391 ? "DB_ATTACH_PERSISTENT"
394 tevent_req_error(req, ret);
398 if (state->db->persistent) {
399 ret = ctdb_reply_control_db_attach_persistent(
400 reply, &state->db->db_id);
402 ret = ctdb_reply_control_db_attach(reply, &state->db->db_id);
406 DEBUG(DEBUG_ERR, ("attach: %s failed to get db_id, ret=%d\n",
407 state->db->db_name, ret));
408 tevent_req_error(req, ret);
412 ctdb_req_control_getdbpath(&request, state->db->db_id);
413 subreq = ctdb_client_control_send(state, state->ev, state->client,
414 state->destnode, state->timeout,
416 if (tevent_req_nomem(subreq, req)) {
419 tevent_req_set_callback(subreq, ctdb_attach_dbpath_done, req);
422 static void ctdb_attach_dbpath_done(struct tevent_req *subreq)
424 struct tevent_req *req = tevent_req_callback_data(
425 subreq, struct tevent_req);
426 struct ctdb_attach_state *state = tevent_req_data(
427 req, struct ctdb_attach_state);
428 struct ctdb_reply_control *reply;
429 struct ctdb_req_control request;
433 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
436 DEBUG(DEBUG_ERR, ("attach: %s GETDBPATH failed, ret=%d\n",
437 state->db->db_name, ret));
438 tevent_req_error(req, ret);
442 ret = ctdb_reply_control_getdbpath(reply, state->db,
443 &state->db->db_path);
446 DEBUG(DEBUG_ERR, ("attach: %s GETDBPATH parse failed, ret=%d\n",
447 state->db->db_name, ret));
448 tevent_req_error(req, ret);
452 ctdb_req_control_db_get_health(&request, state->db->db_id);
453 subreq = ctdb_client_control_send(state, state->ev, state->client,
454 state->destnode, state->timeout,
456 if (tevent_req_nomem(subreq, req)) {
459 tevent_req_set_callback(subreq, ctdb_attach_health_done, req);
462 static void ctdb_attach_health_done(struct tevent_req *subreq)
464 struct tevent_req *req = tevent_req_callback_data(
465 subreq, struct tevent_req);
466 struct ctdb_attach_state *state = tevent_req_data(
467 req, struct ctdb_attach_state);
468 struct ctdb_reply_control *reply;
473 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
476 DEBUG(DEBUG_ERR, ("attach: %s DB_GET_HEALTH failed, ret=%d\n",
477 state->db->db_name, ret));
478 tevent_req_error(req, ret);
482 ret = ctdb_reply_control_db_get_health(reply, state, &reason);
485 ("attach: %s DB_GET_HEALTH parse failed, ret=%d\n",
486 state->db->db_name, ret));
487 tevent_req_error(req, ret);
491 if (reason != NULL) {
492 /* Database unhealthy, avoid attach */
493 DEBUG(DEBUG_ERR, ("attach: %s database unhealthy (%s)\n",
494 state->db->db_name, reason));
495 tevent_req_error(req, EIO);
499 subreq = ctdb_set_db_flags_send(state, state->ev, state->client,
500 state->destnode, state->timeout,
501 state->db->db_id, state->db_flags);
502 if (tevent_req_nomem(subreq, req)) {
505 tevent_req_set_callback(subreq, ctdb_attach_flags_done, req);
508 static void ctdb_attach_flags_done(struct tevent_req *subreq)
510 struct tevent_req *req = tevent_req_callback_data(
511 subreq, struct tevent_req);
512 struct ctdb_attach_state *state = tevent_req_data(
513 req, struct ctdb_attach_state);
517 status = ctdb_set_db_flags_recv(subreq, &ret);
520 DEBUG(DEBUG_ERR, ("attach: %s set db flags 0x%08x failed\n",
521 state->db->db_name, state->db_flags));
522 tevent_req_error(req, ret);
526 state->db->ltdb = tdb_wrap_open(state->db, state->db->db_path, 0,
527 state->tdb_flags, O_RDWR, 0);
528 if (tevent_req_nomem(state->db->ltdb, req)) {
529 DEBUG(DEBUG_ERR, ("attach: %s tdb_wrap_open failed\n",
530 state->db->db_name));
533 DLIST_ADD(state->client->db, state->db);
535 tevent_req_done(req);
538 bool ctdb_attach_recv(struct tevent_req *req, int *perr,
539 struct ctdb_db_context **out)
541 struct ctdb_attach_state *state = tevent_req_data(
542 req, struct ctdb_attach_state);
545 if (tevent_req_is_unix_error(req, &err)) {
558 int ctdb_attach(struct tevent_context *ev,
559 struct ctdb_client_context *client,
560 struct timeval timeout,
561 const char *db_name, uint8_t db_flags,
562 struct ctdb_db_context **out)
565 struct tevent_req *req;
569 mem_ctx = talloc_new(client);
570 if (mem_ctx == NULL) {
574 req = ctdb_attach_send(mem_ctx, ev, client, timeout,
577 talloc_free(mem_ctx);
581 tevent_req_poll(req, ev);
583 status = ctdb_attach_recv(req, &ret, out);
585 talloc_free(mem_ctx);
590 ctdb_set_call(db, CTDB_NULL_FUNC, ctdb_null_func);
591 ctdb_set_call(db, CTDB_FETCH_FUNC, ctdb_fetch_func);
592 ctdb_set_call(db, CTDB_FETCH_WITH_HEADER_FUNC, ctdb_fetch_with_header_func);
595 talloc_free(mem_ctx);
599 int ctdb_detach(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
600 struct ctdb_client_context *client,
601 struct timeval timeout, uint32_t db_id)
603 struct ctdb_db_context *db;
606 ret = ctdb_ctrl_db_detach(mem_ctx, ev, client, client->pnn, timeout,
612 for (db = client->db; db != NULL; db = db->next) {
613 if (db->db_id == db_id) {
614 DLIST_REMOVE(client->db, db);
622 uint32_t ctdb_db_id(struct ctdb_db_context *db)
627 struct ctdb_db_traverse_state {
628 ctdb_rec_parser_func_t parser;
634 static int ctdb_db_traverse_handler(struct tdb_context *tdb, TDB_DATA key,
635 TDB_DATA data, void *private_data)
637 struct ctdb_db_traverse_state *state =
638 (struct ctdb_db_traverse_state *)private_data;
641 if (state->extract_header) {
642 struct ctdb_ltdb_header header;
644 ret = ctdb_ltdb_header_extract(&data, &header);
650 ret = state->parser(0, &header, key, data, state->private_data);
652 ret = state->parser(0, NULL, key, data, state->private_data);
663 int ctdb_db_traverse(struct ctdb_db_context *db, bool readonly,
665 ctdb_rec_parser_func_t parser, void *private_data)
667 struct ctdb_db_traverse_state state;
670 state.parser = parser;
671 state.private_data = private_data;
672 state.extract_header = extract_header;
676 ret = tdb_traverse_read(db->ltdb->tdb,
677 ctdb_db_traverse_handler, &state);
679 ret = tdb_traverse(db->ltdb->tdb,
680 ctdb_db_traverse_handler, &state);
690 int ctdb_ltdb_fetch(struct ctdb_db_context *db, TDB_DATA key,
691 struct ctdb_ltdb_header *header,
692 TALLOC_CTX *mem_ctx, TDB_DATA *data)
697 rec = tdb_fetch(db->ltdb->tdb, key);
698 if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
699 /* No record present */
700 if (rec.dptr != NULL) {
704 if (tdb_error(db->ltdb->tdb) != TDB_ERR_NOEXIST) {
709 header->dmaster = CTDB_UNKNOWN_PNN;
718 ret = ctdb_ltdb_header_pull(rec.dptr, rec.dsize, header);
725 size_t offset = ctdb_ltdb_header_len(header);
727 data->dsize = rec.dsize - offset;
728 data->dptr = talloc_memdup(mem_ctx, rec.dptr + offset,
730 if (data->dptr == NULL) {
740 * Fetch a record from volatile database
743 * 1. Get a lock on the hash chain
744 * 2. If the record does not exist, migrate the record
745 * 3. If readonly=true and delegations do not exist, migrate the record.
746 * 4. If readonly=false and delegations exist, migrate the record.
747 * 5. If the local node is not dmaster, migrate the record.
751 struct ctdb_fetch_lock_state {
752 struct tevent_context *ev;
753 struct ctdb_client_context *client;
754 struct ctdb_record_handle *h;
759 static int ctdb_fetch_lock_check(struct tevent_req *req);
760 static void ctdb_fetch_lock_migrate(struct tevent_req *req);
761 static void ctdb_fetch_lock_migrate_done(struct tevent_req *subreq);
763 struct tevent_req *ctdb_fetch_lock_send(TALLOC_CTX *mem_ctx,
764 struct tevent_context *ev,
765 struct ctdb_client_context *client,
766 struct ctdb_db_context *db,
767 TDB_DATA key, bool readonly)
769 struct ctdb_fetch_lock_state *state;
770 struct tevent_req *req;
773 req = tevent_req_create(mem_ctx, &state, struct ctdb_fetch_lock_state);
779 state->client = client;
781 state->h = talloc_zero(db, struct ctdb_record_handle);
782 if (tevent_req_nomem(state->h, req)) {
783 return tevent_req_post(req, ev);
785 state->h->client = client;
787 state->h->key.dptr = talloc_memdup(state->h, key.dptr, key.dsize);
788 if (tevent_req_nomem(state->h->key.dptr, req)) {
789 return tevent_req_post(req, ev);
791 state->h->key.dsize = key.dsize;
792 state->h->readonly = false;
794 state->readonly = readonly;
795 state->pnn = ctdb_client_pnn(client);
797 /* Check that database is not persistent */
798 if (db->persistent) {
799 DEBUG(DEBUG_ERR, ("fetch_lock: %s database not volatile\n",
801 tevent_req_error(req, EINVAL);
802 return tevent_req_post(req, ev);
805 ret = ctdb_fetch_lock_check(req);
807 tevent_req_done(req);
808 return tevent_req_post(req, ev);
811 tevent_req_error(req, ret);
812 return tevent_req_post(req, ev);
817 static int ctdb_fetch_lock_check(struct tevent_req *req)
819 struct ctdb_fetch_lock_state *state = tevent_req_data(
820 req, struct ctdb_fetch_lock_state);
821 struct ctdb_record_handle *h = state->h;
822 struct ctdb_ltdb_header header;
823 TDB_DATA data = tdb_null;
825 bool do_migrate = false;
827 ret = tdb_chainlock(h->db->ltdb->tdb, h->key);
830 ("fetch_lock: %s tdb_chainlock failed, %s\n",
831 h->db->db_name, tdb_errorstr(h->db->ltdb->tdb)));
836 data = tdb_fetch(h->db->ltdb->tdb, h->key);
837 if (data.dptr == NULL) {
838 if (tdb_error(h->db->ltdb->tdb) == TDB_ERR_NOEXIST) {
847 ret = ctdb_ltdb_header_pull(data.dptr, data.dsize, &header);
853 if (! state->readonly) {
854 /* Read/write access */
855 if (header.dmaster == state->pnn &&
856 header.flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
860 if (header.dmaster != state->pnn) {
864 /* Readonly access */
865 if (header.dmaster != state->pnn &&
866 ! (header.flags & (CTDB_REC_RO_HAVE_READONLY |
867 CTDB_REC_RO_HAVE_DELEGATIONS))) {
872 /* We are the dmaster or readonly delegation */
875 if (header.flags & (CTDB_REC_RO_HAVE_READONLY |
876 CTDB_REC_RO_HAVE_DELEGATIONS)) {
886 if (data.dptr != NULL) {
889 ret = tdb_chainunlock(h->db->ltdb->tdb, h->key);
892 ("fetch_lock: %s tdb_chainunlock failed, %s\n",
893 h->db->db_name, tdb_errorstr(h->db->ltdb->tdb)));
898 ctdb_fetch_lock_migrate(req);
903 static void ctdb_fetch_lock_migrate(struct tevent_req *req)
905 struct ctdb_fetch_lock_state *state = tevent_req_data(
906 req, struct ctdb_fetch_lock_state);
907 struct ctdb_req_call request;
908 struct tevent_req *subreq;
910 ZERO_STRUCT(request);
911 request.flags = CTDB_IMMEDIATE_MIGRATION;
912 if (state->readonly) {
913 request.flags |= CTDB_WANT_READONLY;
915 request.db_id = state->h->db->db_id;
916 request.callid = CTDB_NULL_FUNC;
917 request.key = state->h->key;
918 request.calldata = tdb_null;
920 subreq = ctdb_client_call_send(state, state->ev, state->client,
922 if (tevent_req_nomem(subreq, req)) {
926 tevent_req_set_callback(subreq, ctdb_fetch_lock_migrate_done, req);
929 static void ctdb_fetch_lock_migrate_done(struct tevent_req *subreq)
931 struct tevent_req *req = tevent_req_callback_data(
932 subreq, struct tevent_req);
933 struct ctdb_fetch_lock_state *state = tevent_req_data(
934 req, struct ctdb_fetch_lock_state);
935 struct ctdb_reply_call *reply;
939 status = ctdb_client_call_recv(subreq, state, &reply, &ret);
942 DEBUG(DEBUG_ERR, ("fetch_lock: %s CALL failed, ret=%d\n",
943 state->h->db->db_name, ret));
944 tevent_req_error(req, ret);
948 if (reply->status != 0) {
949 tevent_req_error(req, EIO);
954 ret = ctdb_fetch_lock_check(req);
957 tevent_req_error(req, ret);
962 tevent_req_done(req);
965 static int ctdb_record_handle_destructor(struct ctdb_record_handle *h)
969 ret = tdb_chainunlock(h->db->ltdb->tdb, h->key);
972 ("fetch_lock: %s tdb_chainunlock failed, %s\n",
973 h->db->db_name, tdb_errorstr(h->db->ltdb->tdb)));
979 struct ctdb_record_handle *ctdb_fetch_lock_recv(struct tevent_req *req,
980 struct ctdb_ltdb_header *header,
982 TDB_DATA *data, int *perr)
984 struct ctdb_fetch_lock_state *state = tevent_req_data(
985 req, struct ctdb_fetch_lock_state);
986 struct ctdb_record_handle *h = state->h;
989 if (tevent_req_is_unix_error(req, &err)) {
991 TALLOC_FREE(state->h);
997 if (header != NULL) {
1003 offset = ctdb_ltdb_header_len(&h->header);
1005 data->dsize = h->data.dsize - offset;
1006 data->dptr = talloc_memdup(mem_ctx, h->data.dptr + offset,
1008 if (data->dptr == NULL) {
1009 TALLOC_FREE(state->h);
1017 talloc_set_destructor(h, ctdb_record_handle_destructor);
1021 int ctdb_fetch_lock(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1022 struct ctdb_client_context *client,
1023 struct ctdb_db_context *db, TDB_DATA key, bool readonly,
1024 struct ctdb_record_handle **out,
1025 struct ctdb_ltdb_header *header, TDB_DATA *data)
1027 struct tevent_req *req;
1028 struct ctdb_record_handle *h;
1031 req = ctdb_fetch_lock_send(mem_ctx, ev, client, db, key, readonly);
1036 tevent_req_poll(req, ev);
1038 h = ctdb_fetch_lock_recv(req, header, mem_ctx, data, &ret);
1047 int ctdb_store_record(struct ctdb_record_handle *h, TDB_DATA data)
1049 uint8_t header[sizeof(struct ctdb_ltdb_header)];
1053 /* Cannot modify the record if it was obtained as a readonly copy */
1058 /* Check if the new data is same */
1059 if (h->data.dsize == data.dsize &&
1060 memcmp(h->data.dptr, data.dptr, data.dsize) == 0) {
1061 /* No need to do anything */
1065 ctdb_ltdb_header_push(&h->header, header);
1067 rec[0].dsize = ctdb_ltdb_header_len(&h->header);
1068 rec[0].dptr = header;
1070 rec[1].dsize = data.dsize;
1071 rec[1].dptr = data.dptr;
1073 ret = tdb_storev(h->db->ltdb->tdb, h->key, rec, 2, TDB_REPLACE);
1076 ("store_record: %s tdb_storev failed, %s\n",
1077 h->db->db_name, tdb_errorstr(h->db->ltdb->tdb)));
1084 struct ctdb_delete_record_state {
1085 struct ctdb_record_handle *h;
1088 static void ctdb_delete_record_done(struct tevent_req *subreq);
1090 struct tevent_req *ctdb_delete_record_send(TALLOC_CTX *mem_ctx,
1091 struct tevent_context *ev,
1092 struct ctdb_record_handle *h)
1094 struct tevent_req *req, *subreq;
1095 struct ctdb_delete_record_state *state;
1096 struct ctdb_key_data key;
1097 struct ctdb_req_control request;
1098 uint8_t header[sizeof(struct ctdb_ltdb_header)];
1102 req = tevent_req_create(mem_ctx, &state,
1103 struct ctdb_delete_record_state);
1110 /* Cannot delete the record if it was obtained as a readonly copy */
1112 DEBUG(DEBUG_ERR, ("fetch_lock delete: %s readonly record\n",
1114 tevent_req_error(req, EINVAL);
1115 return tevent_req_post(req, ev);
1118 ctdb_ltdb_header_push(&h->header, header);
1120 rec.dsize = ctdb_ltdb_header_len(&h->header);
1123 ret = tdb_store(h->db->ltdb->tdb, h->key, rec, TDB_REPLACE);
1126 ("fetch_lock delete: %s tdb_sore failed, %s\n",
1127 h->db->db_name, tdb_errorstr(h->db->ltdb->tdb)));
1128 tevent_req_error(req, EIO);
1129 return tevent_req_post(req, ev);
1132 key.db_id = h->db->db_id;
1133 key.header = h->header;
1136 ctdb_req_control_schedule_for_deletion(&request, &key);
1137 subreq = ctdb_client_control_send(state, ev, h->client,
1138 ctdb_client_pnn(h->client),
1139 tevent_timeval_zero(),
1141 if (tevent_req_nomem(subreq, req)) {
1142 return tevent_req_post(req, ev);
1144 tevent_req_set_callback(subreq, ctdb_delete_record_done, req);
1149 static void ctdb_delete_record_done(struct tevent_req *subreq)
1151 struct tevent_req *req = tevent_req_callback_data(
1152 subreq, struct tevent_req);
1153 struct ctdb_delete_record_state *state = tevent_req_data(
1154 req, struct ctdb_delete_record_state);
1158 status = ctdb_client_control_recv(subreq, &ret, NULL, NULL);
1159 TALLOC_FREE(subreq);
1162 ("delete_record: %s SCHDULE_FOR_DELETION failed, "
1163 "ret=%d\n", state->h->db->db_name, ret));
1164 tevent_req_error(req, ret);
1168 tevent_req_done(req);
1171 bool ctdb_delete_record_recv(struct tevent_req *req, int *perr)
1175 if (tevent_req_is_unix_error(req, &err)) {
1186 int ctdb_delete_record(struct ctdb_record_handle *h)
1188 struct tevent_context *ev = h->ev;
1189 TALLOC_CTX *mem_ctx;
1190 struct tevent_req *req;
1194 mem_ctx = talloc_new(NULL);
1195 if (mem_ctx == NULL) {
1199 req = ctdb_delete_record_send(mem_ctx, ev, h);
1201 talloc_free(mem_ctx);
1205 tevent_req_poll(req, ev);
1207 status = ctdb_delete_record_recv(req, &ret);
1208 talloc_free(mem_ctx);
1217 * Global lock functions
1220 struct ctdb_g_lock_lock_state {
1221 struct tevent_context *ev;
1222 struct ctdb_client_context *client;
1223 struct ctdb_db_context *db;
1225 struct ctdb_server_id my_sid;
1226 enum ctdb_g_lock_type lock_type;
1227 struct ctdb_record_handle *h;
1228 /* state for verification of active locks */
1229 struct ctdb_g_lock_list *lock_list;
1230 unsigned int current;
1233 static void ctdb_g_lock_lock_fetched(struct tevent_req *subreq);
1234 static void ctdb_g_lock_lock_process_locks(struct tevent_req *req);
1235 static void ctdb_g_lock_lock_checked(struct tevent_req *subreq);
1236 static int ctdb_g_lock_lock_update(struct tevent_req *req);
1237 static void ctdb_g_lock_lock_retry(struct tevent_req *subreq);
1239 static bool ctdb_g_lock_conflicts(enum ctdb_g_lock_type l1,
1240 enum ctdb_g_lock_type l2)
1242 if ((l1 == CTDB_G_LOCK_READ) && (l2 == CTDB_G_LOCK_READ)) {
1248 struct tevent_req *ctdb_g_lock_lock_send(TALLOC_CTX *mem_ctx,
1249 struct tevent_context *ev,
1250 struct ctdb_client_context *client,
1251 struct ctdb_db_context *db,
1252 const char *keyname,
1253 struct ctdb_server_id *sid,
1256 struct tevent_req *req, *subreq;
1257 struct ctdb_g_lock_lock_state *state;
1259 req = tevent_req_create(mem_ctx, &state,
1260 struct ctdb_g_lock_lock_state);
1266 state->client = client;
1268 state->key.dptr = discard_const(keyname);
1269 state->key.dsize = strlen(keyname) + 1;
1270 state->my_sid = *sid;
1271 state->lock_type = (readonly ? CTDB_G_LOCK_READ : CTDB_G_LOCK_WRITE);
1273 subreq = ctdb_fetch_lock_send(state, ev, client, db, state->key,
1275 if (tevent_req_nomem(subreq, req)) {
1276 return tevent_req_post(req, ev);
1278 tevent_req_set_callback(subreq, ctdb_g_lock_lock_fetched, req);
1283 static void ctdb_g_lock_lock_fetched(struct tevent_req *subreq)
1285 struct tevent_req *req = tevent_req_callback_data(
1286 subreq, struct tevent_req);
1287 struct ctdb_g_lock_lock_state *state = tevent_req_data(
1288 req, struct ctdb_g_lock_lock_state);
1292 state->h = ctdb_fetch_lock_recv(subreq, NULL, state, &data, &ret);
1293 TALLOC_FREE(subreq);
1294 if (state->h == NULL) {
1295 DEBUG(DEBUG_ERR, ("g_lock_lock: %s fetch lock failed\n",
1296 (char *)state->key.dptr));
1297 tevent_req_error(req, ret);
1301 if (state->lock_list != NULL) {
1302 TALLOC_FREE(state->lock_list);
1306 ret = ctdb_g_lock_list_pull(data.dptr, data.dsize, state,
1308 talloc_free(data.dptr);
1310 DEBUG(DEBUG_ERR, ("g_lock_lock: %s invalid lock data\n",
1311 (char *)state->key.dptr));
1312 tevent_req_error(req, ret);
1316 ctdb_g_lock_lock_process_locks(req);
1319 static void ctdb_g_lock_lock_process_locks(struct tevent_req *req)
1321 struct ctdb_g_lock_lock_state *state = tevent_req_data(
1322 req, struct ctdb_g_lock_lock_state);
1323 struct tevent_req *subreq;
1324 struct ctdb_g_lock *lock;
1325 bool check_server = false;
1328 while (state->current < state->lock_list->num) {
1329 lock = &state->lock_list->lock[state->current];
1331 /* We should not ask for the same lock more than once */
1332 if (ctdb_server_id_equal(&lock->sid, &state->my_sid)) {
1333 DEBUG(DEBUG_ERR, ("g_lock_lock: %s deadlock\n",
1334 (char *)state->key.dptr));
1335 tevent_req_error(req, EDEADLK);
1339 if (ctdb_g_lock_conflicts(lock->type, state->lock_type)) {
1340 check_server = true;
1344 state->current += 1;
1348 struct ctdb_req_control request;
1350 ctdb_req_control_process_exists(&request, lock->sid.pid);
1351 subreq = ctdb_client_control_send(state, state->ev,
1354 tevent_timeval_zero(),
1356 if (tevent_req_nomem(subreq, req)) {
1359 tevent_req_set_callback(subreq, ctdb_g_lock_lock_checked, req);
1363 /* There is no conflict, add ourself to the lock_list */
1364 state->lock_list->lock = talloc_realloc(state->lock_list,
1365 state->lock_list->lock,
1367 state->lock_list->num + 1);
1368 if (state->lock_list->lock == NULL) {
1369 tevent_req_error(req, ENOMEM);
1373 lock = &state->lock_list->lock[state->lock_list->num];
1374 lock->type = state->lock_type;
1375 lock->sid = state->my_sid;
1376 state->lock_list->num += 1;
1378 ret = ctdb_g_lock_lock_update(req);
1380 tevent_req_error(req, ret);
1384 TALLOC_FREE(state->h);
1385 tevent_req_done(req);
1388 static void ctdb_g_lock_lock_checked(struct tevent_req *subreq)
1390 struct tevent_req *req = tevent_req_callback_data(
1391 subreq, struct tevent_req);
1392 struct ctdb_g_lock_lock_state *state = tevent_req_data(
1393 req, struct ctdb_g_lock_lock_state);
1394 struct ctdb_reply_control *reply;
1398 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
1399 TALLOC_FREE(subreq);
1402 ("g_lock_lock: %s PROCESS_EXISTS failed, ret=%d\n",
1403 (char *)state->key.dptr, ret));
1404 tevent_req_error(req, ret);
1408 ret = ctdb_reply_control_process_exists(reply, &value);
1410 tevent_req_error(req, ret);
1416 /* server process exists, need to retry */
1417 TALLOC_FREE(state->h);
1418 subreq = tevent_wakeup_send(state, state->ev,
1419 tevent_timeval_current_ofs(0,1000));
1420 if (tevent_req_nomem(subreq, req)) {
1423 tevent_req_set_callback(subreq, ctdb_g_lock_lock_retry, req);
1427 /* server process does not exist, remove conflicting entry */
1428 state->lock_list->lock[state->current] =
1429 state->lock_list->lock[state->lock_list->num-1];
1430 state->lock_list->num -= 1;
1432 ret = ctdb_g_lock_lock_update(req);
1434 tevent_req_error(req, ret);
1438 ctdb_g_lock_lock_process_locks(req);
1441 static int ctdb_g_lock_lock_update(struct tevent_req *req)
1443 struct ctdb_g_lock_lock_state *state = tevent_req_data(
1444 req, struct ctdb_g_lock_lock_state);
1448 data.dsize = ctdb_g_lock_list_len(state->lock_list);
1449 data.dptr = talloc_size(state, data.dsize);
1450 if (data.dptr == NULL) {
1454 ctdb_g_lock_list_push(state->lock_list, data.dptr);
1455 ret = ctdb_store_record(state->h, data);
1456 talloc_free(data.dptr);
1460 static void ctdb_g_lock_lock_retry(struct tevent_req *subreq)
1462 struct tevent_req *req = tevent_req_callback_data(
1463 subreq, struct tevent_req);
1464 struct ctdb_g_lock_lock_state *state = tevent_req_data(
1465 req, struct ctdb_g_lock_lock_state);
1468 success = tevent_wakeup_recv(subreq);
1469 TALLOC_FREE(subreq);
1471 tevent_req_error(req, ENOMEM);
1475 subreq = ctdb_fetch_lock_send(state, state->ev, state->client,
1476 state->db, state->key, false);
1477 if (tevent_req_nomem(subreq, req)) {
1480 tevent_req_set_callback(subreq, ctdb_g_lock_lock_fetched, req);
1483 bool ctdb_g_lock_lock_recv(struct tevent_req *req, int *perr)
1485 struct ctdb_g_lock_lock_state *state = tevent_req_data(
1486 req, struct ctdb_g_lock_lock_state);
1489 TALLOC_FREE(state->h);
1491 if (tevent_req_is_unix_error(req, &err)) {
1501 struct ctdb_g_lock_unlock_state {
1502 struct tevent_context *ev;
1503 struct ctdb_client_context *client;
1504 struct ctdb_db_context *db;
1506 struct ctdb_server_id my_sid;
1507 struct ctdb_record_handle *h;
1508 struct ctdb_g_lock_list *lock_list;
1511 static void ctdb_g_lock_unlock_fetched(struct tevent_req *subreq);
1512 static int ctdb_g_lock_unlock_update(struct tevent_req *req);
1513 static void ctdb_g_lock_unlock_deleted(struct tevent_req *subreq);
1515 struct tevent_req *ctdb_g_lock_unlock_send(TALLOC_CTX *mem_ctx,
1516 struct tevent_context *ev,
1517 struct ctdb_client_context *client,
1518 struct ctdb_db_context *db,
1519 const char *keyname,
1520 struct ctdb_server_id sid)
1522 struct tevent_req *req, *subreq;
1523 struct ctdb_g_lock_unlock_state *state;
1525 req = tevent_req_create(mem_ctx, &state,
1526 struct ctdb_g_lock_unlock_state);
1532 state->client = client;
1534 state->key.dptr = discard_const(keyname);
1535 state->key.dsize = strlen(keyname) + 1;
1536 state->my_sid = sid;
1538 subreq = ctdb_fetch_lock_send(state, ev, client, db, state->key,
1540 if (tevent_req_nomem(subreq, req)) {
1541 return tevent_req_post(req, ev);
1543 tevent_req_set_callback(subreq, ctdb_g_lock_unlock_fetched, req);
1548 static void ctdb_g_lock_unlock_fetched(struct tevent_req *subreq)
1550 struct tevent_req *req = tevent_req_callback_data(
1551 subreq, struct tevent_req);
1552 struct ctdb_g_lock_unlock_state *state = tevent_req_data(
1553 req, struct ctdb_g_lock_unlock_state);
1557 state->h = ctdb_fetch_lock_recv(subreq, NULL, state, &data, &ret);
1558 TALLOC_FREE(subreq);
1559 if (state->h == NULL) {
1560 DEBUG(DEBUG_ERR, ("g_lock_unlock: %s fetch lock failed\n",
1561 (char *)state->key.dptr));
1562 tevent_req_error(req, ret);
1566 ret = ctdb_g_lock_list_pull(data.dptr, data.dsize, state,
1569 DEBUG(DEBUG_ERR, ("g_lock_unlock: %s invalid lock data\n",
1570 (char *)state->key.dptr));
1571 tevent_req_error(req, ret);
1575 ret = ctdb_g_lock_unlock_update(req);
1577 tevent_req_error(req, ret);
1581 if (state->lock_list->num == 0) {
1582 subreq = ctdb_delete_record_send(state, state->ev, state->h);
1583 if (tevent_req_nomem(subreq, req)) {
1586 tevent_req_set_callback(subreq, ctdb_g_lock_unlock_deleted,
1591 TALLOC_FREE(state->h);
1592 tevent_req_done(req);
1595 static int ctdb_g_lock_unlock_update(struct tevent_req *req)
1597 struct ctdb_g_lock_unlock_state *state = tevent_req_data(
1598 req, struct ctdb_g_lock_unlock_state);
1599 struct ctdb_g_lock *lock;
1602 for (i=0; i<state->lock_list->num; i++) {
1603 lock = &state->lock_list->lock[i];
1605 if (ctdb_server_id_equal(&lock->sid, &state->my_sid)) {
1610 if (i < state->lock_list->num) {
1611 state->lock_list->lock[i] =
1612 state->lock_list->lock[state->lock_list->num-1];
1613 state->lock_list->num -= 1;
1616 if (state->lock_list->num != 0) {
1619 data.dsize = ctdb_g_lock_list_len(state->lock_list);
1620 data.dptr = talloc_size(state, data.dsize);
1621 if (data.dptr == NULL) {
1625 ctdb_g_lock_list_push(state->lock_list, data.dptr);
1626 ret = ctdb_store_record(state->h, data);
1627 talloc_free(data.dptr);
1636 static void ctdb_g_lock_unlock_deleted(struct tevent_req *subreq)
1638 struct tevent_req *req = tevent_req_callback_data(
1639 subreq, struct tevent_req);
1640 struct ctdb_g_lock_unlock_state *state = tevent_req_data(
1641 req, struct ctdb_g_lock_unlock_state);
1645 status = ctdb_delete_record_recv(subreq, &ret);
1648 ("g_lock_unlock %s delete record failed, ret=%d\n",
1649 (char *)state->key.dptr, ret));
1650 tevent_req_error(req, ret);
1654 TALLOC_FREE(state->h);
1655 tevent_req_done(req);
1658 bool ctdb_g_lock_unlock_recv(struct tevent_req *req, int *perr)
1660 struct ctdb_g_lock_unlock_state *state = tevent_req_data(
1661 req, struct ctdb_g_lock_unlock_state);
1664 TALLOC_FREE(state->h);
1666 if (tevent_req_is_unix_error(req, &err)) {
1677 * Persistent database functions
1679 struct ctdb_transaction_start_state {
1680 struct tevent_context *ev;
1681 struct ctdb_client_context *client;
1682 struct timeval timeout;
1683 struct ctdb_transaction_handle *h;
1687 static void ctdb_transaction_g_lock_attached(struct tevent_req *subreq);
1688 static void ctdb_transaction_g_lock_done(struct tevent_req *subreq);
1690 struct tevent_req *ctdb_transaction_start_send(TALLOC_CTX *mem_ctx,
1691 struct tevent_context *ev,
1692 struct ctdb_client_context *client,
1693 struct timeval timeout,
1694 struct ctdb_db_context *db,
1697 struct ctdb_transaction_start_state *state;
1698 struct tevent_req *req, *subreq;
1699 struct ctdb_transaction_handle *h;
1701 req = tevent_req_create(mem_ctx, &state,
1702 struct ctdb_transaction_start_state);
1707 if (! db->persistent) {
1708 tevent_req_error(req, EINVAL);
1709 return tevent_req_post(req, ev);
1713 state->client = client;
1714 state->destnode = ctdb_client_pnn(client);
1716 h = talloc_zero(db, struct ctdb_transaction_handle);
1717 if (tevent_req_nomem(h, req)) {
1718 return tevent_req_post(req, ev);
1724 h->readonly = readonly;
1727 /* SRVID is unique for databases, so client can have transactions
1728 * active for multiple databases */
1729 h->sid = ctdb_client_get_server_id(client, db->db_id);
1731 h->recbuf = ctdb_rec_buffer_init(h, db->db_id);
1732 if (tevent_req_nomem(h->recbuf, req)) {
1733 return tevent_req_post(req, ev);
1736 h->lock_name = talloc_asprintf(h, "transaction_db_0x%08x", db->db_id);
1737 if (tevent_req_nomem(h->lock_name, req)) {
1738 return tevent_req_post(req, ev);
1743 subreq = ctdb_attach_send(state, ev, client, timeout, "g_lock.tdb", 0);
1744 if (tevent_req_nomem(subreq, req)) {
1745 return tevent_req_post(req, ev);
1747 tevent_req_set_callback(subreq, ctdb_transaction_g_lock_attached, req);
1752 static void ctdb_transaction_g_lock_attached(struct tevent_req *subreq)
1754 struct tevent_req *req = tevent_req_callback_data(
1755 subreq, struct tevent_req);
1756 struct ctdb_transaction_start_state *state = tevent_req_data(
1757 req, struct ctdb_transaction_start_state);
1761 status = ctdb_attach_recv(subreq, &ret, &state->h->db_g_lock);
1762 TALLOC_FREE(subreq);
1765 ("transaction_start: %s attach g_lock.tdb failed\n",
1766 state->h->db->db_name));
1767 tevent_req_error(req, ret);
1771 subreq = ctdb_g_lock_lock_send(state, state->ev, state->client,
1772 state->h->db_g_lock,
1773 state->h->lock_name,
1774 &state->h->sid, state->h->readonly);
1775 if (tevent_req_nomem(subreq, req)) {
1778 tevent_req_set_callback(subreq, ctdb_transaction_g_lock_done, req);
1781 static void ctdb_transaction_g_lock_done(struct tevent_req *subreq)
1783 struct tevent_req *req = tevent_req_callback_data(
1784 subreq, struct tevent_req);
1785 struct ctdb_transaction_start_state *state = tevent_req_data(
1786 req, struct ctdb_transaction_start_state);
1790 status = ctdb_g_lock_lock_recv(subreq, &ret);
1791 TALLOC_FREE(subreq);
1794 ("transaction_start: %s g_lock lock failed, ret=%d\n",
1795 state->h->db->db_name, ret));
1796 tevent_req_error(req, ret);
1800 tevent_req_done(req);
1803 struct ctdb_transaction_handle *ctdb_transaction_start_recv(
1804 struct tevent_req *req,
1807 struct ctdb_transaction_start_state *state = tevent_req_data(
1808 req, struct ctdb_transaction_start_state);
1811 if (tevent_req_is_unix_error(req, &err)) {
1821 int ctdb_transaction_start(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
1822 struct ctdb_client_context *client,
1823 struct timeval timeout,
1824 struct ctdb_db_context *db, bool readonly,
1825 struct ctdb_transaction_handle **out)
1827 struct tevent_req *req;
1828 struct ctdb_transaction_handle *h;
1831 req = ctdb_transaction_start_send(mem_ctx, ev, client, timeout, db,
1837 tevent_req_poll(req, ev);
1839 h = ctdb_transaction_start_recv(req, &ret);
1848 struct ctdb_transaction_record_fetch_state {
1850 struct ctdb_ltdb_header header;
1854 static int ctdb_transaction_record_fetch_traverse(
1856 struct ctdb_ltdb_header *nullheader,
1857 TDB_DATA key, TDB_DATA data,
1860 struct ctdb_transaction_record_fetch_state *state =
1861 (struct ctdb_transaction_record_fetch_state *)private_data;
1863 if (state->key.dsize == key.dsize &&
1864 memcmp(state->key.dptr, key.dptr, key.dsize) == 0) {
1867 ret = ctdb_ltdb_header_extract(&data, &state->header);
1870 ("record_fetch: Failed to extract header, "
1876 state->found = true;
1882 static int ctdb_transaction_record_fetch(struct ctdb_transaction_handle *h,
1884 struct ctdb_ltdb_header *header,
1887 struct ctdb_transaction_record_fetch_state state;
1891 state.found = false;
1893 ret = ctdb_rec_buffer_traverse(h->recbuf,
1894 ctdb_transaction_record_fetch_traverse,
1901 if (header != NULL) {
1902 *header = state.header;
1913 int ctdb_transaction_fetch_record(struct ctdb_transaction_handle *h,
1915 TALLOC_CTX *mem_ctx, TDB_DATA *data)
1918 struct ctdb_ltdb_header header;
1921 ret = ctdb_transaction_record_fetch(h, key, NULL, &tmp_data);
1923 data->dptr = talloc_memdup(mem_ctx, tmp_data.dptr,
1925 if (data->dptr == NULL) {
1928 data->dsize = tmp_data.dsize;
1932 ret = ctdb_ltdb_fetch(h->db, key, &header, mem_ctx, data);
1937 ret = ctdb_rec_buffer_add(h, h->recbuf, 0, &header, key, *data);
1945 int ctdb_transaction_store_record(struct ctdb_transaction_handle *h,
1946 TDB_DATA key, TDB_DATA data)
1948 TALLOC_CTX *tmp_ctx;
1949 struct ctdb_ltdb_header header;
1957 tmp_ctx = talloc_new(h);
1958 if (tmp_ctx == NULL) {
1962 ret = ctdb_transaction_record_fetch(h, key, &header, &old_data);
1964 ret = ctdb_ltdb_fetch(h->db, key, &header, tmp_ctx, &old_data);
1970 if (old_data.dsize == data.dsize &&
1971 memcmp(old_data.dptr, data.dptr, data.dsize) == 0) {
1972 talloc_free(tmp_ctx);
1976 header.dmaster = ctdb_client_pnn(h->client);
1979 ret = ctdb_rec_buffer_add(h, h->recbuf, 0, &header, key, data);
1980 talloc_free(tmp_ctx);
1989 int ctdb_transaction_delete_record(struct ctdb_transaction_handle *h,
1992 return ctdb_transaction_store_record(h, key, tdb_null);
1995 static int ctdb_transaction_fetch_db_seqnum(struct ctdb_transaction_handle *h,
1998 const char *keyname = CTDB_DB_SEQNUM_KEY;
2000 struct ctdb_ltdb_header header;
2003 key.dptr = discard_const(keyname);
2004 key.dsize = strlen(keyname) + 1;
2006 ret = ctdb_ltdb_fetch(h->db, key, &header, h, &data);
2009 ("transaction_commit: %s seqnum fetch failed, ret=%d\n",
2010 h->db->db_name, ret));
2014 if (data.dsize == 0) {
2020 if (data.dsize != sizeof(uint64_t)) {
2021 talloc_free(data.dptr);
2025 *seqnum = *(uint64_t *)data.dptr;
2027 talloc_free(data.dptr);
2031 static int ctdb_transaction_store_db_seqnum(struct ctdb_transaction_handle *h,
2034 const char *keyname = CTDB_DB_SEQNUM_KEY;
2037 key.dptr = discard_const(keyname);
2038 key.dsize = strlen(keyname) + 1;
2040 data.dptr = (uint8_t *)&seqnum;
2041 data.dsize = sizeof(seqnum);
2043 return ctdb_transaction_store_record(h, key, data);
2046 struct ctdb_transaction_commit_state {
2047 struct tevent_context *ev;
2048 struct timeval timeout;
2049 struct ctdb_transaction_handle *h;
2053 static void ctdb_transaction_commit_done(struct tevent_req *subreq);
2054 static void ctdb_transaction_commit_g_lock_done(struct tevent_req *subreq);
2056 struct tevent_req *ctdb_transaction_commit_send(
2057 TALLOC_CTX *mem_ctx,
2058 struct tevent_context *ev,
2059 struct timeval timeout,
2060 struct ctdb_transaction_handle *h)
2062 struct tevent_req *req, *subreq;
2063 struct ctdb_transaction_commit_state *state;
2064 struct ctdb_req_control request;
2067 req = tevent_req_create(mem_ctx, &state,
2068 struct ctdb_transaction_commit_state);
2074 state->timeout = timeout;
2077 ret = ctdb_transaction_fetch_db_seqnum(h, &state->seqnum);
2079 tevent_req_error(req, ret);
2080 return tevent_req_post(req, ev);
2083 ret = ctdb_transaction_store_db_seqnum(h, state->seqnum+1);
2085 tevent_req_error(req, ret);
2086 return tevent_req_post(req, ev);
2089 ctdb_req_control_trans3_commit(&request, h->recbuf);
2090 subreq = ctdb_client_control_send(state, ev, h->client,
2091 ctdb_client_pnn(h->client),
2093 if (tevent_req_nomem(subreq, req)) {
2094 return tevent_req_post(req, ev);
2096 tevent_req_set_callback(subreq, ctdb_transaction_commit_done, req);
2101 static void ctdb_transaction_commit_done(struct tevent_req *subreq)
2103 struct tevent_req *req = tevent_req_callback_data(
2104 subreq, struct tevent_req);
2105 struct ctdb_transaction_commit_state *state = tevent_req_data(
2106 req, struct ctdb_transaction_commit_state);
2107 struct ctdb_transaction_handle *h = state->h;
2108 struct ctdb_reply_control *reply;
2113 status = ctdb_client_control_recv(subreq, &ret, state, &reply);
2114 TALLOC_FREE(subreq);
2117 ("transaction_commit: %s TRANS3_COMMIT failed, ret=%d\n",
2118 h->db->db_name, ret));
2119 tevent_req_error(req, ret);
2123 ret = ctdb_reply_control_trans3_commit(reply);
2127 /* Control failed due to recovery */
2129 ret = ctdb_transaction_fetch_db_seqnum(h, &seqnum);
2131 tevent_req_error(req, ret);
2135 if (seqnum == state->seqnum) {
2136 struct ctdb_req_control request;
2139 ctdb_req_control_trans3_commit(&request,
2141 subreq = ctdb_client_control_send(
2142 state, state->ev, state->h->client,
2143 ctdb_client_pnn(state->h->client),
2144 state->timeout, &request);
2145 if (tevent_req_nomem(subreq, req)) {
2148 tevent_req_set_callback(subreq,
2149 ctdb_transaction_commit_done,
2154 if (seqnum != state->seqnum + 1) {
2156 ("transaction_commit: %s seqnum mismatch "
2157 "0x%"PRIx64" != 0x%"PRIx64" + 1\n",
2158 state->h->db->db_name, seqnum, state->seqnum));
2159 tevent_req_error(req, EIO);
2164 /* trans3_commit successful */
2165 subreq = ctdb_g_lock_unlock_send(state, state->ev, h->client,
2166 h->db_g_lock, h->lock_name, h->sid);
2167 if (tevent_req_nomem(subreq, req)) {
2170 tevent_req_set_callback(subreq, ctdb_transaction_commit_g_lock_done,
2174 static void ctdb_transaction_commit_g_lock_done(struct tevent_req *subreq)
2176 struct tevent_req *req = tevent_req_callback_data(
2177 subreq, struct tevent_req);
2178 struct ctdb_transaction_commit_state *state = tevent_req_data(
2179 req, struct ctdb_transaction_commit_state);
2183 status = ctdb_g_lock_unlock_recv(subreq, &ret);
2184 TALLOC_FREE(subreq);
2187 ("transaction_commit: %s g_lock unlock failed, ret=%d\n",
2188 state->h->db->db_name, ret));
2189 tevent_req_error(req, ret);
2193 talloc_free(state->h);
2194 tevent_req_done(req);
2197 bool ctdb_transaction_commit_recv(struct tevent_req *req, int *perr)
2201 if (tevent_req_is_unix_error(req, &err)) {
2211 int ctdb_transaction_commit(struct ctdb_transaction_handle *h)
2213 struct tevent_context *ev = h->ev;
2214 TALLOC_CTX *mem_ctx;
2215 struct tevent_req *req;
2219 if (h->readonly || ! h->updated) {
2220 return ctdb_transaction_cancel(h);
2223 mem_ctx = talloc_new(NULL);
2224 if (mem_ctx == NULL) {
2228 req = ctdb_transaction_commit_send(mem_ctx, ev,
2229 tevent_timeval_zero(), h);
2231 talloc_free(mem_ctx);
2235 tevent_req_poll(req, ev);
2237 status = ctdb_transaction_commit_recv(req, &ret);
2239 talloc_free(mem_ctx);
2243 talloc_free(mem_ctx);
2247 struct ctdb_transaction_cancel_state {
2248 struct tevent_context *ev;
2249 struct ctdb_transaction_handle *h;
2250 struct timeval timeout;
2253 static void ctdb_transaction_cancel_done(struct tevent_req *subreq);
2255 struct tevent_req *ctdb_transaction_cancel_send(
2256 TALLOC_CTX *mem_ctx,
2257 struct tevent_context *ev,
2258 struct timeval timeout,
2259 struct ctdb_transaction_handle *h)
2261 struct tevent_req *req, *subreq;
2262 struct ctdb_transaction_cancel_state *state;
2264 req = tevent_req_create(mem_ctx, &state,
2265 struct ctdb_transaction_cancel_state);
2272 state->timeout = timeout;
2274 subreq = ctdb_g_lock_unlock_send(state, state->ev, state->h->client,
2275 state->h->db_g_lock,
2276 state->h->lock_name, state->h->sid);
2277 if (tevent_req_nomem(subreq, req)) {
2278 return tevent_req_post(req, ev);
2280 tevent_req_set_callback(subreq, ctdb_transaction_cancel_done,
2286 static void ctdb_transaction_cancel_done(struct tevent_req *subreq)
2288 struct tevent_req *req = tevent_req_callback_data(
2289 subreq, struct tevent_req);
2290 struct ctdb_transaction_cancel_state *state = tevent_req_data(
2291 req, struct ctdb_transaction_cancel_state);
2295 status = ctdb_g_lock_unlock_recv(subreq, &ret);
2296 TALLOC_FREE(subreq);
2299 ("transaction_cancel: %s g_lock unlock failed, ret=%d\n",
2300 state->h->db->db_name, ret));
2301 talloc_free(state->h);
2302 tevent_req_error(req, ret);
2306 talloc_free(state->h);
2307 tevent_req_done(req);
2310 bool ctdb_transaction_cancel_recv(struct tevent_req *req, int *perr)
2314 if (tevent_req_is_unix_error(req, &err)) {
2324 int ctdb_transaction_cancel(struct ctdb_transaction_handle *h)
2326 struct tevent_context *ev = h->ev;
2327 struct tevent_req *req;
2328 TALLOC_CTX *mem_ctx;
2332 mem_ctx = talloc_new(NULL);
2333 if (mem_ctx == NULL) {
2338 req = ctdb_transaction_cancel_send(mem_ctx, ev,
2339 tevent_timeval_zero(), h);
2341 talloc_free(mem_ctx);
2346 tevent_req_poll(req, ev);
2348 status = ctdb_transaction_cancel_recv(req, &ret);
2350 talloc_free(mem_ctx);
2354 talloc_free(mem_ctx);
2361 * In future Samba should register SERVER_ID.
2362 * Make that structure same as struct srvid {}.