]> git.samba.org - sahlberg/ctdb.git/blob - libctdb/ctdb.c
libctdb: connect TDB logging to our logging
[sahlberg/ctdb.git] / libctdb / ctdb.c
1 /*
2    core of libctdb
3
4    Copyright (C) Rusty Russell 2010
5
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.
10
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.
15
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/>.
18 */
19 #include <ctdb.h>
20 #include <poll.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include "libctdb_private.h"
28 #include "io_elem.h"
29 #include "local_tdb.h"
30 #include "messages.h"
31 #include <dlinklist.h>
32 #include <ctdb_protocol.h>
33
34 /* Remove type-safety macros. */
35 #undef ctdb_attachdb_send
36 #undef ctdb_readrecordlock_async
37 #undef ctdb_connect
38
39 struct ctdb_lock {
40         struct ctdb_lock *next, *prev;
41
42         struct ctdb_db *ctdb_db;
43         TDB_DATA key;
44
45         /* This will always be set by the time user sees this. */
46         unsigned long held_magic;
47         struct ctdb_ltdb_header *hdr;
48
49         /* For convenience, we stash original callback here. */
50         ctdb_rrl_callback_t callback;
51 };
52
53 static void remove_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
54 {
55         DLIST_REMOVE(ctdb->locks, lock);
56 }
57
58 /* FIXME: for thread safety, need tid info too. */
59 static bool holding_lock(struct ctdb_connection *ctdb)
60 {
61         /* For the moment, you can't ever hold more than 1 lock. */
62         return (ctdb->locks != NULL);
63 }
64
65 static void add_lock(struct ctdb_connection *ctdb, struct ctdb_lock *lock)
66 {
67         DLIST_ADD(ctdb->locks, lock);
68 }
69
70 /* FIXME: Could be in shared util code with rest of ctdb */
71 static void close_noerr(int fd)
72 {
73         int olderr = errno;
74         close(fd);
75         errno = olderr;
76 }
77
78 /* FIXME: Could be in shared util code with rest of ctdb */
79 static void free_noerr(void *p)
80 {
81         int olderr = errno;
82         free(p);
83         errno = olderr;
84 }
85
86 /* FIXME: Could be in shared util code with rest of ctdb */
87 static void set_nonblocking(int fd)
88 {
89         unsigned v;
90         v = fcntl(fd, F_GETFL, 0);
91         fcntl(fd, F_SETFL, v | O_NONBLOCK);
92 }
93
94 /* FIXME: Could be in shared util code with rest of ctdb */
95 static void set_close_on_exec(int fd)
96 {
97         unsigned v;
98         v = fcntl(fd, F_GETFD, 0);
99         fcntl(fd, F_SETFD, v | FD_CLOEXEC);
100 }
101
102 static void set_pnn(struct ctdb_connection *ctdb,
103                     struct ctdb_request *req,
104                     void *unused)
105 {
106         if (!ctdb_getpnn_recv(ctdb, req, &ctdb->pnn)) {
107                 DEBUG(ctdb, LOG_CRIT,
108                       "ctdb_connect(async): failed to get pnn");
109                 ctdb->broken = true;
110         }
111         ctdb_request_free(ctdb, req);
112 }
113
114 struct ctdb_connection *ctdb_connect(const char *addr,
115                                      ctdb_log_fn_t log_fn, void *log_priv)
116 {
117         struct ctdb_connection *ctdb;
118         struct sockaddr_un sun;
119
120         ctdb = malloc(sizeof(*ctdb));
121         if (!ctdb) {
122                 /* With no format string, we hope it doesn't use ap! */
123                 va_list ap;
124                 memset(&ap, 0, sizeof(ap));
125                 errno = ENOMEM;
126                 log_fn(log_priv, LOG_ERR, "ctdb_connect: no memory", ap);
127                 goto fail;
128         }
129         ctdb->outq = NULL;
130         ctdb->doneq = NULL;
131         ctdb->in = NULL;
132         ctdb->message_handlers = NULL;
133         ctdb->next_id = 0;
134         ctdb->broken = false;
135         ctdb->log = log_fn;
136         ctdb->log_priv = log_priv;
137         ctdb->locks = NULL;
138
139         memset(&sun, 0, sizeof(sun));
140         sun.sun_family = AF_UNIX;
141         if (!addr)
142                 addr = CTDB_PATH;
143         strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
144         ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
145         if (ctdb->fd < 0)
146                 goto free_fail;
147
148         set_nonblocking(ctdb->fd);
149         set_close_on_exec(ctdb->fd);
150
151         if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
152                 goto close_fail;
153
154         /* Immediately queue a request to get our pnn. */
155         if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
156                 goto close_fail;
157
158         return ctdb;
159
160 close_fail:
161         close_noerr(ctdb->fd);
162 free_fail:
163         free_noerr(ctdb);
164 fail:
165         return NULL;
166 }
167
168 int ctdb_get_fd(struct ctdb_connection *ctdb)
169 {
170         return ctdb->fd;
171 }
172
173 int ctdb_which_events(struct ctdb_connection *ctdb)
174 {
175         int events = POLLIN;
176
177         if (ctdb->outq)
178                 events |= POLLOUT;
179         return events;
180 }
181
182 struct ctdb_request *new_ctdb_request(size_t len,
183                                       ctdb_callback_t cb, void *cbdata)
184 {
185         struct ctdb_request *req = malloc(sizeof(*req));
186         if (!req)
187                 return NULL;
188         req->io = new_io_elem(len);
189         if (!req->io) {
190                 free(req);
191                 return NULL;
192         }
193         req->hdr.hdr = io_elem_data(req->io, NULL);
194         req->reply = NULL;
195         req->callback = cb;
196         req->priv_data = cbdata;
197         req->extra = NULL;
198         req->extra_destructor = NULL;
199         return req;
200 }
201
202 void ctdb_request_free(struct ctdb_connection *ctdb, struct ctdb_request *req)
203 {
204         if (req->extra_destructor) {
205                 req->extra_destructor(ctdb, req);
206         }
207         if (req->reply) {
208                 free_io_elem(req->reply);
209         }
210         free_io_elem(req->io);
211         free(req);
212 }
213
214 /* Sanity-checking wrapper for reply. */
215 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_connection *ctdb,
216                                                  struct ctdb_request *req,
217                                                  uint32_t callid)
218 {
219         size_t len;
220         struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
221
222         /* Library user error if this isn't a reply to a call. */
223         if (req->hdr.hdr->operation != CTDB_REQ_CALL) {
224                 errno = EINVAL;
225                 DEBUG(ctdb, LOG_ALERT,
226                       "This was not a ctdbd call request: operation %u",
227                       req->hdr.hdr->operation);
228                 return NULL;
229         }
230
231         if (req->hdr.call->callid != callid) {
232                 errno = EINVAL;
233                 DEBUG(ctdb, LOG_ALERT,
234                       "This was not a ctdbd %u call request: %u",
235                       callid, req->hdr.call->callid);
236                 return NULL;
237         }
238
239         /* ctdbd or our error if this isn't a reply call. */
240         if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
241                 errno = EIO;
242                 DEBUG(ctdb, LOG_CRIT,
243                       "Invalid ctdbd call reply: len %zu, operation %u",
244                       len, inhdr->hdr.operation);
245                 return NULL;
246         }
247
248         return inhdr;
249 }
250
251 /* Sanity-checking wrapper for reply. */
252 struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb,
253                                                 struct ctdb_request *req,
254                                                 enum ctdb_controls control)
255 {
256         size_t len;
257         struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
258
259         /* Library user error if this isn't a reply to a call. */
260         if (len < sizeof(*inhdr)) {
261                 errno = EINVAL;
262                 DEBUG(ctdb, LOG_ALERT,
263                       "Short ctdbd control reply: %zu bytes", len);
264                 return NULL;
265         }
266         if (req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
267                 errno = EINVAL;
268                 DEBUG(ctdb, LOG_ALERT,
269                       "This was not a ctdbd control request: operation %u",
270                       req->hdr.hdr->operation);
271                 return NULL;
272         }
273
274         /* ... or if it was a different control from what we expected. */
275         if (req->hdr.control->opcode != control) {
276                 errno = EINVAL;
277                 DEBUG(ctdb, LOG_ALERT,
278                       "This was not an opcode %u ctdbd control request: %u",
279                       control, req->hdr.control->opcode);
280                 return NULL;
281         }
282
283         /* ctdbd or our error if this isn't a reply call. */
284         if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
285                 errno = EIO;
286                 DEBUG(ctdb, LOG_CRIT,
287                       "Invalid ctdbd control reply: operation %u",
288                       inhdr->hdr.operation);
289                 return NULL;
290         }
291
292         return inhdr;
293 }
294
295 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
296 {
297         struct ctdb_req_header *hdr;
298         size_t len;
299         struct ctdb_request *i;
300
301         hdr = io_elem_data(in, &len);
302         /* FIXME: use len to check packet! */
303
304         if (hdr->operation == CTDB_REQ_MESSAGE) {
305                 deliver_message(ctdb, hdr);
306                 return;
307         }
308
309         for (i = ctdb->doneq; i; i = i->next) {
310                 if (i->hdr.hdr->reqid == hdr->reqid) {
311                         DLIST_REMOVE(ctdb->doneq, i);
312                         i->reply = in;
313                         i->callback(ctdb, i, i->priv_data);
314                         return;
315                 }
316         }
317         DEBUG(ctdb, LOG_WARNING,
318               "Unexpected ctdbd request reply: operation %u reqid %u",
319               hdr->operation, hdr->reqid);
320         free_io_elem(in);
321 }
322
323 /* Remove "harmless" errors. */
324 static ssize_t real_error(ssize_t ret)
325 {
326         if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
327                 return 0;
328         return ret;
329 }
330
331 bool ctdb_service(struct ctdb_connection *ctdb, int revents)
332 {
333         if (ctdb->broken) {
334                 return false;
335         }
336
337         if (holding_lock(ctdb)) {
338                 DEBUG(ctdb, LOG_ALERT, "Do not block while holding lock!");
339         }
340
341         if (revents & POLLOUT) {
342                 while (ctdb->outq) {
343                         if (real_error(write_io_elem(ctdb->fd,
344                                                      ctdb->outq->io)) < 0) {
345                                 DEBUG(ctdb, LOG_ERR,
346                                       "ctdb_service: error writing to ctdbd");
347                                 ctdb->broken = true;
348                                 return false;
349                         }
350                         if (io_elem_finished(ctdb->outq->io)) {
351                                 struct ctdb_request *done = ctdb->outq;
352                                 DLIST_REMOVE(ctdb->outq, done);
353                                 /* We add at the head: any dead ones
354                                  * sit and end. */
355                                 DLIST_ADD(ctdb->doneq, done);
356                         }
357                 }
358         }
359
360         while (revents & POLLIN) {
361                 int ret;
362
363                 if (!ctdb->in) {
364                         ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
365                         if (!ctdb->in) {
366                                 DEBUG(ctdb, LOG_ERR,
367                                       "ctdb_service: allocating readbuf");
368                                 ctdb->broken = true;
369                                 return false;
370                         }
371                 }
372
373                 ret = read_io_elem(ctdb->fd, ctdb->in);
374                 if (real_error(ret) < 0 || ret == 0) {
375                         /* They closed fd? */
376                         if (ret == 0)
377                                 errno = EBADF;
378                         DEBUG(ctdb, LOG_ERR,
379                               "ctdb_service: error reading from ctdbd");
380                         ctdb->broken = true;
381                         return false;
382                 } else if (ret < 0) {
383                         /* No progress, stop loop. */
384                         revents = 0;
385                 } else if (io_elem_finished(ctdb->in)) {
386                         handle_incoming(ctdb, ctdb->in);
387                         ctdb->in = NULL;
388                 }
389         }
390
391         return true;
392 }
393
394 /* This is inefficient.  We could pull in idtree.c. */
395 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
396 {
397         struct ctdb_request *i;
398
399         for (i = ctdb->outq; i; i = i->next) {
400                 if (i->hdr.hdr->reqid == reqid) {
401                         return true;
402                 }
403         }
404         for (i = ctdb->doneq; i; i = i->next) {
405                 if (i->hdr.hdr->reqid == reqid) {
406                         return true;
407                 }
408         }
409         return false;
410 }
411
412 uint32_t new_reqid(struct ctdb_connection *ctdb)
413 {
414         while (reqid_used(ctdb, ctdb->next_id)) {
415                 ctdb->next_id++;
416         }
417         return ctdb->next_id++;
418 }
419
420 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
421                                               uint32_t opcode,
422                                               uint32_t destnode,
423                                               const void *extra_data,
424                                               size_t extra,
425                                               ctdb_callback_t callback,
426                                               void *cbdata)
427 {
428         struct ctdb_request *req;
429         struct ctdb_req_control *pkt;
430
431         req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
432         if (!req)
433                 return NULL;
434
435         io_elem_init_req_header(req->io,
436                                 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
437
438         pkt = req->hdr.control;
439         pkt->pad = 0;
440         pkt->opcode = opcode;
441         pkt->srvid = 0;
442         pkt->client_id = 0;
443         pkt->flags = 0;
444         pkt->datalen = extra;
445         memcpy(pkt->data, extra_data, extra);
446         DLIST_ADD(ctdb->outq, req);
447         return req;
448 }
449
450 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
451                           struct ctdb_request *req,
452                           void *unused)
453 {
454         ctdb_request_free(ctdb, req);
455 }
456
457 void ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req)
458 {
459         DEBUG(ctdb, LOG_DEBUG, "ctdb_cancel: %p (id %u)",
460               req, req->hdr.hdr ? req->hdr.hdr->reqid : 0);
461
462         /* FIXME: If it's not sent, we could just free it right now. */
463         req->callback = ctdb_cancel_callback;
464 }
465
466 struct ctdb_db {
467         struct ctdb_connection *ctdb;
468         bool persistent;
469         uint32_t tdb_flags;
470         uint32_t id;
471         struct tdb_context *tdb;
472
473         /* The lock we are holding, if any (we can only have one!) */
474         struct ctdb_lock *lock;
475
476         ctdb_callback_t callback;
477         void *private_data;
478 };
479
480 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
481                                     struct ctdb_request *req,
482                                     void *_db)
483 {
484         struct ctdb_db *db = _db;
485
486         /* Do callback on original request. */
487         db->callback(ctdb, req->extra, db->private_data);
488 }
489
490 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb,
491                                    struct ctdb_request *req)
492 {
493         struct ctdb_request *dbpath_req = req->extra;
494         struct ctdb_reply_control *reply;
495         struct ctdb_db *db = req->priv_data;
496         uint32_t tdb_flags = db->tdb_flags;
497         struct tdb_logging_context log;
498
499         /* Never sent the dbpath request?  We've failed. */
500         if (!dbpath_req) {
501                 /* FIXME: Save errno? */
502                 errno = EINVAL;
503                 return NULL;
504         }
505
506         reply = unpack_reply_control(ctdb, dbpath_req, CTDB_CONTROL_GETDBPATH);
507         if (!reply) {
508                 return NULL;
509         }
510         if (reply->status != 0) {
511                 DEBUG(db->ctdb, LOG_ERR,
512                       "ctdb_attachdb_recv: reply status %i", reply->status);
513                 return NULL;
514         }
515
516         tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
517         tdb_flags |= TDB_DISALLOW_NESTING;
518
519         log.log_fn = ctdb_tdb_log_bridge;
520         log.log_private = ctdb;
521         db->tdb = tdb_open_ex((char *)reply->data, 0, tdb_flags, O_RDWR, 0,
522                               &log, NULL);
523         if (db->tdb == NULL) {
524                 DEBUG(db->ctdb, LOG_ERR,
525                       "ctdb_attachdb_recv: failed to tdb_open %s",
526                       (char *)reply->data);
527                 return NULL;
528         }
529
530         /* Finally, separate the db from the request (see destroy_req_db). */
531         req->priv_data = NULL;
532         DEBUG(db->ctdb, LOG_DEBUG,
533               "ctdb_attachdb_recv: db %p, tdb %s", db, (char *)reply->data);
534         return db;
535 }
536
537 static void attachdb_done(struct ctdb_connection *ctdb,
538                           struct ctdb_request *req,
539                           void *_db)
540 {
541         struct ctdb_db *db = _db;
542         struct ctdb_request *req2;
543         struct ctdb_reply_control *reply;
544         enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
545
546         if (db->persistent) {
547                 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
548         }
549
550         reply = unpack_reply_control(ctdb, req, control);
551         if (!reply || reply->status != 0) {
552                 if (reply) {
553                         DEBUG(ctdb, LOG_ERR,
554                               "ctdb_attachdb_send(async): DB_ATTACH status %i",
555                               reply->status);
556                 }
557                 /* We failed.  Hand request to user and have them discover it
558                  * via ctdb_attachdb_recv. */
559                 db->callback(ctdb, req, db->private_data);
560                 return;
561         }
562         db->id = *(uint32_t *)reply->data;
563
564         /* Now we do another call, to get the dbpath. */
565         req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
566                                         CTDB_CURRENT_NODE,
567                                         &db->id, sizeof(db->id),
568                                         attachdb_getdbpath_done, db);
569         if (!req2) {
570                 DEBUG(db->ctdb, LOG_ERR,
571                       "ctdb_attachdb_send(async): failed to allocate");
572                 db->callback(ctdb, req, db->private_data);
573                 return;
574         }
575         req->extra = req2;
576         req2->extra = req;
577         DEBUG(db->ctdb, LOG_DEBUG,
578               "ctdb_attachdb_send(async): created getdbpath request");
579 }
580
581 static void destroy_req_db(struct ctdb_connection *ctdb,
582                            struct ctdb_request *req)
583 {
584         /* Incomplete db is in priv_data. */
585         free(req->priv_data);
586         /* second request is chained off this one. */
587         if (req->extra) {
588                 ctdb_request_free(ctdb, req->extra);
589         }
590 }
591
592 struct ctdb_request *
593 ctdb_attachdb_send(struct ctdb_connection *ctdb,
594                    const char *name, bool persistent, uint32_t tdb_flags,
595                    ctdb_callback_t callback, void *private_data)
596 {
597         struct ctdb_request *req;
598         struct ctdb_db *db;
599         uint32_t opcode;
600
601         /* FIXME: Search if db already open. */
602         db = malloc(sizeof(*db));
603         if (!db) {
604                 return NULL;
605         }
606
607         if (persistent) {
608                 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
609         } else {
610                 opcode = CTDB_CONTROL_DB_ATTACH;
611         }
612
613         req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
614                                        strlen(name) + 1, attachdb_done, db);
615         if (!req) {
616                 DEBUG(db->ctdb, LOG_ERR,
617                       "ctdb_attachdb_send: failed allocating DB_ATTACH");
618                 free(db);
619                 return NULL;
620         }
621
622         db->ctdb = ctdb;
623         db->tdb_flags = tdb_flags;
624         db->persistent = persistent;
625         db->callback = callback;
626         db->private_data = private_data;
627
628         req->extra_destructor = destroy_req_db;
629         /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
630         req->extra = NULL;
631
632         /* Flags get overloaded into srvid. */
633         req->hdr.control->srvid = tdb_flags;
634         DEBUG(db->ctdb, LOG_DEBUG,
635               "ctdb_attachdb_send: DB_ATTACH request %p", req);
636         return req;
637 }
638
639 static unsigned long lock_magic(struct ctdb_lock *lock)
640 {
641         /* A non-zero magic specific to this structure. */
642         return ((unsigned long)lock->key.dptr
643                 ^ (((unsigned long)lock->key.dptr) << 16)
644                 ^ 0xBADC0FFEEBADC0DEULL)
645                 | 1;
646 }
647
648 /* This is only called on locks before they're held. */
649 static void free_lock(struct ctdb_lock *lock)
650 {
651         if (lock->held_magic) {
652                 DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
653                       "free_lock invalid lock %p", lock);
654         }
655         free(lock->hdr);
656         free(lock);
657 }
658
659
660 void ctdb_release_lock(struct ctdb_db *ctdb_db, struct ctdb_lock *lock)
661 {
662         if (lock->held_magic != lock_magic(lock)) {
663                 DEBUG(lock->ctdb_db->ctdb, LOG_ALERT,
664                       "ctdb_release_lock invalid lock %p", lock);
665         } else if (lock->ctdb_db != ctdb_db) {
666                 errno = EBADF;
667                 DEBUG(ctdb_db->ctdb, LOG_ALERT,
668                       "ctdb_release_lock: wrong ctdb_db.");
669         } else {
670                 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
671                 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
672                       "ctdb_release_lock %p", lock);
673                 remove_lock(lock->ctdb_db->ctdb, lock);
674         }
675         lock->held_magic = 0;
676         free_lock(lock);
677 }
678
679
680 /* We keep the lock if local node is the dmaster. */
681 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
682 {
683         struct ctdb_ltdb_header *hdr;
684
685         if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
686                 DEBUG(lock->ctdb_db->ctdb, LOG_WARNING,
687                       "ctdb_readrecordlock_async: failed to chainlock");
688                 return NULL;
689         }
690
691         hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
692         if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
693                 DEBUG(lock->ctdb_db->ctdb, LOG_DEBUG,
694                       "ctdb_readrecordlock_async: got local lock");
695                 lock->held_magic = lock_magic(lock);
696                 lock->hdr = hdr;
697                 add_lock(lock->ctdb_db->ctdb, lock);
698                 return true;
699         }
700
701         tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
702         free(hdr);
703         return NULL;
704 }
705
706 /* If they shutdown before we hand them the lock, we free it here. */
707 static void destroy_lock(struct ctdb_connection *ctdb,
708                          struct ctdb_request *req)
709 {
710         free_lock(req->extra);
711 }
712
713 static void readrecordlock_retry(struct ctdb_connection *ctdb,
714                                  struct ctdb_request *req, void *private)
715 {
716         struct ctdb_lock *lock = req->extra;
717         struct ctdb_reply_call *reply;
718         TDB_DATA data;
719
720         /* OK, we've received reply to noop migration */
721         reply = unpack_reply_call(ctdb, req, CTDB_NULL_FUNC);
722         if (!reply || reply->status != 0) {
723                 if (reply) {
724                         DEBUG(ctdb, LOG_ERR,
725                               "ctdb_readrecordlock_async(async):"
726                               " NULL_FUNC returned %i", reply->status);
727                 }
728                 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
729                 ctdb_request_free(ctdb, req); /* Also frees lock. */
730                 return;
731         }
732
733         /* Can we get lock now? */
734         if (try_readrecordlock(lock, &data)) {
735                 /* Now it's their responsibility to free lock & request! */
736                 req->extra_destructor = NULL;
737                 lock->callback(lock->ctdb_db, lock, data, private);
738                 return;
739         }
740
741         /* Retransmit the same request again (we lost race). */
742         io_elem_reset(req->io);
743         DLIST_ADD(ctdb->outq, req);
744 }
745
746 bool
747 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
748                           ctdb_rrl_callback_t callback, void *cbdata)
749 {
750         struct ctdb_request *req;
751         struct ctdb_lock *lock;
752         TDB_DATA data;
753
754         if (holding_lock(ctdb_db->ctdb)) {
755                 DEBUG(ctdb_db->ctdb, LOG_ALERT,
756                       "ctdb_readrecordlock_async: already holding lock");
757                 return false;
758         }
759
760         /* Setup lock */
761         lock = malloc(sizeof(*lock) + key.dsize);
762         if (!lock) {
763                 DEBUG(ctdb_db->ctdb, LOG_ERR,
764                       "ctdb_readrecordlock_async: lock allocation failed");
765                 return false;
766         }
767         lock->key.dptr = (void *)(lock + 1);
768         memcpy(lock->key.dptr, key.dptr, key.dsize);
769         lock->key.dsize = key.dsize;
770         lock->ctdb_db = ctdb_db;
771         lock->hdr = NULL;
772         lock->held_magic = 0;
773
774         /* Fast path. */
775         if (try_readrecordlock(lock, &data)) {
776                 callback(ctdb_db, lock, data, cbdata);
777                 return true;
778         }
779
780         /* Slow path: create request. */
781         req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
782                                + key.dsize, readrecordlock_retry, cbdata);
783         if (!req) {
784                 DEBUG(ctdb_db->ctdb, LOG_ERR,
785                       "ctdb_readrecordlock_async: allocation failed");
786                 free_lock(lock);
787                 return NULL;
788         }
789         req->extra = lock;
790         req->extra_destructor = destroy_lock;
791         /* We store the original callback in the lock, and use our own. */
792         lock->callback = callback;
793
794         io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
795                                 new_reqid(ctdb_db->ctdb));
796
797         req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
798         req->hdr.call->db_id = ctdb_db->id;
799         req->hdr.call->callid = CTDB_NULL_FUNC;
800         req->hdr.call->hopcount = 0;
801         req->hdr.call->keylen = key.dsize;
802         req->hdr.call->calldatalen = 0;
803         memcpy(req->hdr.call->data, key.dptr, key.dsize);
804         DLIST_ADD(ctdb_db->ctdb->outq, req);
805         return true;
806 }
807
808 bool ctdb_writerecord(struct ctdb_db *ctdb_db,
809                       struct ctdb_lock *lock, TDB_DATA data)
810 {
811         if (lock->ctdb_db != ctdb_db) {
812                 errno = EBADF;
813                 DEBUG(ctdb_db->ctdb, LOG_ALERT,
814                       "ctdb_writerecord: Can not write, wrong ctdb_db.");
815                 return false;
816         }
817
818         if (lock->held_magic != lock_magic(lock)) {
819                 errno = EBADF;
820                 DEBUG(ctdb_db->ctdb, LOG_ALERT,
821                       "ctdb_writerecord: Can not write. Lock has been released.");
822                 return false;
823         }
824                 
825         if (ctdb_db->persistent) {
826                 errno = EINVAL;
827                 DEBUG(ctdb_db->ctdb, LOG_ALERT,
828                       "ctdb_writerecord: cannot write to persistent db");
829                 return false;
830         }
831
832         switch (ctdb_local_store(ctdb_db->tdb, lock->key, lock->hdr, data)) {
833         case 0:
834                 DEBUG(ctdb_db->ctdb, LOG_DEBUG,
835                       "ctdb_writerecord: optimized away noop write.");
836                 /* fall thru */
837         case 1:
838                 return true;
839
840         default:
841                 switch (errno) {
842                 case ENOMEM:
843                         DEBUG(ctdb_db->ctdb, LOG_CRIT,
844                               "ctdb_writerecord: out of memory.");
845                         break;
846                 case EINVAL:
847                         DEBUG(ctdb_db->ctdb, LOG_ALERT,
848                               "ctdb_writerecord: record changed under lock?");
849                         break;
850                 default: /* TDB already logged. */
851                         break;
852                 }
853                 return false;
854         }
855 }