Readrecordlock changes:
[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
38 /* FIXME: Could be in shared util code with rest of ctdb */
39 static void close_noerr(int fd)
40 {
41         int olderr = errno;
42         close(fd);
43         errno = olderr;
44 }
45
46 /* FIXME: Could be in shared util code with rest of ctdb */
47 static void free_noerr(void *p)
48 {
49         int olderr = errno;
50         free(p);
51         errno = olderr;
52 }
53
54 /* FIXME: Could be in shared util code with rest of ctdb */
55 static void set_nonblocking(int fd)
56 {
57         unsigned v;
58         v = fcntl(fd, F_GETFL, 0);
59         fcntl(fd, F_SETFL, v | O_NONBLOCK);
60 }
61
62 /* FIXME: Could be in shared util code with rest of ctdb */
63 static void set_close_on_exec(int fd)
64 {
65         unsigned v;
66         v = fcntl(fd, F_GETFD, 0);
67         fcntl(fd, F_SETFD, v | FD_CLOEXEC);
68 }
69
70 static void set_pnn(struct ctdb_connection *ctdb,
71                     struct ctdb_request *req,
72                     void *unused)
73 {
74         if (ctdb_getpnn_recv(req, &ctdb->pnn) != 0) {
75                 /* FIXME: Report error. */
76                 ctdb->broken = true;
77         }
78         ctdb_request_free(req);
79 }
80
81 struct ctdb_connection *ctdb_connect(const char *addr)
82 {
83         struct ctdb_connection *ctdb;
84         struct sockaddr_un sun;
85
86         ctdb = malloc(sizeof(*ctdb));
87         if (!ctdb)
88                 goto fail;
89         ctdb->outq = NULL;
90         ctdb->doneq = NULL;
91         ctdb->in = NULL;
92         ctdb->message_handlers = NULL;
93         ctdb->next_id = 0;
94         ctdb->broken = false;
95
96         memset(&sun, 0, sizeof(sun));
97         sun.sun_family = AF_UNIX;
98         if (!addr)
99                 addr = CTDB_PATH;
100         strncpy(sun.sun_path, addr, sizeof(sun.sun_path));
101         ctdb->fd = socket(AF_UNIX, SOCK_STREAM, 0);
102         if (ctdb->fd < 0)
103                 goto free_fail;
104
105         set_nonblocking(ctdb->fd);
106         set_close_on_exec(ctdb->fd);
107
108         if (connect(ctdb->fd, (struct sockaddr *)&sun, sizeof(sun)) == -1)
109                 goto close_fail;
110
111         /* Immediately queue a request to get our pnn. */
112         if (!ctdb_getpnn_send(ctdb, CTDB_CURRENT_NODE, set_pnn, NULL))
113                 goto close_fail;
114
115         return ctdb;
116
117 close_fail:
118         close_noerr(ctdb->fd);
119 free_fail:
120         free_noerr(ctdb);
121 fail:
122         return NULL;
123 }
124
125 int ctdb_get_fd(struct ctdb_connection *ctdb)
126 {
127         return ctdb->fd;
128 }
129
130 int ctdb_which_events(struct ctdb_connection *ctdb)
131 {
132         int events = POLLIN;
133
134         if (ctdb->outq)
135                 events |= POLLOUT;
136         return events;
137 }
138
139 struct ctdb_request *new_ctdb_request(size_t len,
140                                       ctdb_callback_t cb, void *cbdata)
141 {
142         struct ctdb_request *req = malloc(sizeof(*req));
143         if (!req)
144                 return NULL;
145         req->io = new_io_elem(len);
146         if (!req->io) {
147                 free(req);
148                 return NULL;
149         }
150         req->hdr.hdr = io_elem_data(req->io, NULL);
151         req->reply = NULL;
152         req->callback = cb;
153         req->priv_data = cbdata;
154         req->extra = NULL;
155         req->extra_destructor = NULL;
156         return req;
157 }
158
159 void ctdb_request_free(struct ctdb_request *req)
160 {
161         if (req->extra_destructor) {
162                 req->extra_destructor(req);
163         }
164         if (req->reply) {
165                 free_io_elem(req->reply);
166         }
167         free_io_elem(req->io);
168         free(req);
169 }
170
171 /* Sanity-checking wrapper for reply.
172  * FIXME: logging! */
173 static struct ctdb_reply_call *unpack_reply_call(struct ctdb_request *req,
174                                                  uint32_t callid)
175 {
176         size_t len;
177         struct ctdb_reply_call *inhdr = io_elem_data(req->reply, &len);
178
179         /* ctdbd or our error if this isn't a reply call. */
180         if (len < sizeof(*inhdr) || inhdr->hdr.operation != CTDB_REPLY_CALL) {
181                 errno = EIO;
182                 return NULL;
183         }
184
185         /* Library user error if this isn't a reply to a call. */
186         if (req->hdr.hdr->operation != CTDB_REQ_CALL
187             || req->hdr.call->callid != callid) {
188                 errno = EINVAL;
189                 return NULL;
190         }
191
192         return inhdr;
193 }
194
195 /* Sanity-checking wrapper for reply.
196  * FIXME: logging! */
197 struct ctdb_reply_control *unpack_reply_control(struct ctdb_request *req,
198                                                 enum ctdb_controls control)
199 {
200         size_t len;
201         struct ctdb_reply_control *inhdr = io_elem_data(req->reply, &len);
202
203         /* Library user error if this isn't a reply to a call. */
204         if (len < sizeof(*inhdr)
205             || req->hdr.hdr->operation != CTDB_REQ_CONTROL) {
206                 errno = EINVAL;
207                 return NULL;
208         }
209
210         /* ... or if it was a different control from what we expected. */
211         if (req->hdr.control->opcode != control) {
212                 errno = EINVAL;
213                 return NULL;
214         }
215
216         /* ctdbd or our error if this isn't a reply call. */
217         if (inhdr->hdr.operation != CTDB_REPLY_CONTROL) {
218                 errno = EIO;
219                 return NULL;
220         }
221
222         return inhdr;
223 }
224
225 static void handle_incoming(struct ctdb_connection *ctdb, struct io_elem *in)
226 {
227         struct ctdb_req_header *hdr;
228         size_t len;
229         struct ctdb_request *i;
230
231         hdr = io_elem_data(in, &len);
232         /* FIXME: use len to check packet! */
233
234         if (hdr->operation == CTDB_REQ_MESSAGE) {
235                 deliver_message(ctdb, hdr);
236                 return;
237         }
238
239         for (i = ctdb->doneq; i; i = i->next) {
240                 if (i->hdr.hdr->reqid == hdr->reqid) {
241                         DLIST_REMOVE(ctdb->doneq, i);
242                         i->reply = in;
243                         i->callback(ctdb, i, i->priv_data);
244                         return;
245                 }
246         }
247         /* FIXME: report this error. */
248         free_io_elem(in);
249 }
250
251 /* Remove "harmless" errors. */
252 static ssize_t real_error(ssize_t ret)
253 {
254         if (ret < 0 && (errno == EINTR || errno == EWOULDBLOCK))
255                 return 0;
256         return ret;
257 }
258
259 int ctdb_service(struct ctdb_connection *ctdb, int revents)
260 {
261         if (ctdb->broken) {
262                 return -1;
263         }
264
265         if (revents & POLLOUT) {
266                 while (ctdb->outq) {
267                         if (real_error(write_io_elem(ctdb->fd,
268                                                      ctdb->outq->io)) < 0) {
269                                 ctdb->broken = true;
270                                 return -1;
271                         }
272                         if (io_elem_finished(ctdb->outq->io)) {
273                                 struct ctdb_request *done = ctdb->outq;
274                                 DLIST_REMOVE(ctdb->outq, done);
275                                 /* We add at the head: any dead ones
276                                  * sit and end. */
277                                 DLIST_ADD(ctdb->doneq, done);
278                         }
279                 }
280         }
281
282         while (revents & POLLIN) {
283                 int ret;
284
285                 if (!ctdb->in) {
286                         ctdb->in = new_io_elem(sizeof(struct ctdb_req_header));
287                         if (!ctdb->in) {
288                                 ctdb->broken = true;
289                                 return -1;
290                         }
291                 }
292
293                 ret = read_io_elem(ctdb->fd, ctdb->in);
294                 if (real_error(ret) < 0 || ret == 0) {
295                         /* They closed fd? */
296                         if (ret == 0)
297                                 errno = EBADF;
298                         ctdb->broken = true;
299                         return -1;
300                 } else if (ret < 0) {
301                         /* No progress, stop loop. */
302                         revents = 0;
303                 } else if (io_elem_finished(ctdb->in)) {
304                         handle_incoming(ctdb, ctdb->in);
305                         ctdb->in = NULL;
306                 }
307         }
308
309         return 0;
310 }
311
312 /* This is inefficient.  We could pull in idtree.c. */
313 static bool reqid_used(const struct ctdb_connection *ctdb, uint32_t reqid)
314 {
315         struct ctdb_request *i;
316
317         for (i = ctdb->outq; i; i = i->next) {
318                 if (i->hdr.hdr->reqid == reqid) {
319                         return true;
320                 }
321         }
322         for (i = ctdb->doneq; i; i = i->next) {
323                 if (i->hdr.hdr->reqid == reqid) {
324                         return true;
325                 }
326         }
327         return false;
328 }
329
330 uint32_t new_reqid(struct ctdb_connection *ctdb)
331 {
332         while (reqid_used(ctdb, ctdb->next_id)) {
333                 ctdb->next_id++;
334         }
335         return ctdb->next_id++;
336 }
337
338 struct ctdb_request *new_ctdb_control_request(struct ctdb_connection *ctdb,
339                                               uint32_t opcode,
340                                               uint32_t destnode,
341                                               const void *extra_data,
342                                               size_t extra,
343                                               ctdb_callback_t callback,
344                                               void *cbdata)
345 {
346         struct ctdb_request *req;
347         struct ctdb_req_control *pkt;
348
349         req = new_ctdb_request(offsetof(struct ctdb_req_control, data) + extra, callback, cbdata);
350         if (!req)
351                 return NULL;
352
353         io_elem_init_req_header(req->io,
354                                 CTDB_REQ_CONTROL, destnode, new_reqid(ctdb));
355
356         pkt = req->hdr.control;
357         pkt->pad = 0;
358         pkt->opcode = opcode;
359         pkt->srvid = 0;
360         pkt->client_id = 0;
361         pkt->flags = 0;
362         pkt->datalen = extra;
363         memcpy(pkt->data, extra_data, extra);
364         DLIST_ADD(ctdb->outq, req);
365         return req;
366 }
367
368 void ctdb_cancel_callback(struct ctdb_connection *ctdb,
369                           struct ctdb_request *req,
370                           void *unused)
371 {
372         ctdb_request_free(req);
373 }
374
375 int ctdb_cancel(struct ctdb_request *req)
376 {
377         /* FIXME: If it's not sent, we could just free it right now. */
378         req->callback = ctdb_cancel_callback;
379         return 0;
380 }
381
382 struct ctdb_db {
383         struct ctdb_connection *ctdb;
384         bool persistent;
385         uint32_t tdb_flags;
386         uint32_t id;
387         struct tdb_context *tdb;
388
389         ctdb_callback_t callback;
390         void *private_data;
391 };
392
393 static void attachdb_getdbpath_done(struct ctdb_connection *ctdb,
394                                     struct ctdb_request *req,
395                                     void *_db)
396 {
397         struct ctdb_db *db = _db;
398
399         /* Do callback on original request. */
400         db->callback(ctdb, req->extra, db->private_data);
401 }
402
403 struct ctdb_db *ctdb_attachdb_recv(struct ctdb_request *req)
404 {
405         struct ctdb_request *dbpath_req = req->extra;
406         struct ctdb_reply_control *reply;
407         struct ctdb_db *db = req->priv_data;
408         uint32_t tdb_flags = db->tdb_flags;
409
410         /* Never sent the dbpath request?  We've failed. */
411         if (!dbpath_req) {
412                 /* FIXME: Save errno? */
413                 errno = EINVAL;
414                 return NULL;
415         }
416
417         reply = unpack_reply_control(dbpath_req, CTDB_CONTROL_GETDBPATH);
418         if (!reply || reply->status != 0) {
419                 return NULL;
420         }
421
422         tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC;
423         tdb_flags |= TDB_DISALLOW_NESTING;
424
425         db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0);
426         if (db->tdb == NULL) {
427                 return NULL;
428         }
429
430         /* Finally, separate the db from the request (see destroy_req_db). */
431         req->priv_data = NULL;
432         return db;
433 }
434
435 static void attachdb_done(struct ctdb_connection *ctdb,
436                           struct ctdb_request *req,
437                           void *_db)
438 {
439         struct ctdb_db *db = _db;
440         struct ctdb_request *req2;
441         struct ctdb_reply_control *reply;
442         enum ctdb_controls control = CTDB_CONTROL_DB_ATTACH;
443
444         if (db->persistent) {
445                 control = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
446         }
447
448         reply = unpack_reply_control(req, control);
449         if (!reply || reply->status != 0) {
450                 /* We failed.  Hand request to user and have them discover it
451                  * via ctdb_attachdb_recv. */
452                 db->callback(ctdb, req, db->private_data);
453                 return;
454         }
455         db->id = *(uint32_t *)reply->data;
456
457         /* Now we do another call, to get the dbpath. */
458         req2 = new_ctdb_control_request(db->ctdb, CTDB_CONTROL_GETDBPATH,
459                                         CTDB_CURRENT_NODE,
460                                         &db->id, sizeof(db->id),
461                                         attachdb_getdbpath_done, db);
462         if (!req2) {
463                 db->callback(ctdb, req, db->private_data);
464                 return;
465         }
466         req->extra = req2;
467         req2->extra = req;
468 }
469
470 static void destroy_req_db(struct ctdb_request *req)
471 {
472         /* Incomplete db is in priv_data. */
473         free(req->priv_data);
474         /* second request is chained off this one. */
475         if (req->extra) {
476                 ctdb_request_free(req->extra);
477         }
478 }
479
480 struct ctdb_request *
481 ctdb_attachdb_send(struct ctdb_connection *ctdb,
482                    const char *name, int persistent, uint32_t tdb_flags,
483                    ctdb_callback_t callback, void *private_data)
484 {
485         struct ctdb_request *req;
486         struct ctdb_db *db;
487         uint32_t opcode;
488
489         /* FIXME: Search if db already open. */
490         db = malloc(sizeof(*db));
491         if (!db) {
492                 return NULL;
493         }
494
495         if (persistent) {
496                 opcode = CTDB_CONTROL_DB_ATTACH_PERSISTENT;
497         } else {
498                 opcode = CTDB_CONTROL_DB_ATTACH;
499         }
500
501         req = new_ctdb_control_request(ctdb, opcode, CTDB_CURRENT_NODE, name,
502                                        strlen(name) + 1, attachdb_done, db);
503         if (!req) {
504                 free(db);
505                 return NULL;
506         }
507
508         db->ctdb = ctdb;
509         db->tdb_flags = tdb_flags;
510         db->persistent = persistent;
511         db->callback = callback;
512         db->private_data = private_data;
513
514         req->extra_destructor = destroy_req_db;
515         /* This is set non-NULL when we succeed, see ctdb_attachdb_recv */
516         req->extra = NULL;
517
518         /* Flags get overloaded into srvid. */
519         req->hdr.control->srvid = tdb_flags;
520         return req;
521 }
522
523 struct ctdb_lock {
524         struct ctdb_db *ctdb_db;
525         TDB_DATA key;
526
527         /* This will always be true by the time user sees this. */
528         bool held;
529         struct ctdb_ltdb_header *hdr;
530
531         /* For convenience, we stash original callback here. */
532         ctdb_rrl_callback_t callback;
533 };
534
535 void ctdb_release_lock(struct ctdb_lock *lock)
536 {
537         if (lock->held) {
538                 tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
539                 lock->held = false;
540         }
541 }
542
543 static void ctdb_free_lock(struct ctdb_lock *lock)
544 {
545         if (lock->held) {
546                 /* FIXME: report error. Callback never released the lock */
547                 ctdb_release_lock(lock);
548         }
549
550         free(lock->hdr);
551         free(lock);
552 }
553
554 /* We keep the lock if local node is the dmaster. */
555 static bool try_readrecordlock(struct ctdb_lock *lock, TDB_DATA *data)
556 {
557         struct ctdb_ltdb_header *hdr;
558
559         if (tdb_chainlock(lock->ctdb_db->tdb, lock->key) != 0) {
560                 return NULL;
561         }
562
563         hdr = ctdb_local_fetch(lock->ctdb_db->tdb, lock->key, data);
564         if (hdr && hdr->dmaster == lock->ctdb_db->ctdb->pnn) {
565                 lock->held = true;
566                 lock->hdr = hdr;
567                 return true;
568         }
569
570         tdb_chainunlock(lock->ctdb_db->tdb, lock->key);
571         free(hdr);
572         return NULL;
573 }
574
575 /* If they shutdown before we hand them the lock, we free it here. */
576 static void destroy_lock(struct ctdb_request *req)
577 {
578         ctdb_release_lock(req->extra);
579         ctdb_free_lock(req->extra);
580 }
581
582 static void readrecordlock_retry(struct ctdb_connection *ctdb,
583                                  struct ctdb_request *req, void *private)
584 {
585         struct ctdb_lock *lock = req->extra;
586         struct ctdb_reply_call *reply;
587         TDB_DATA data;
588
589         /* OK, we've received reply to noop migration */
590         reply = unpack_reply_call(req, CTDB_NULL_FUNC);
591         if (!reply || reply->status != 0) {
592                 lock->callback(lock->ctdb_db, NULL, tdb_null, private);
593                 ctdb_request_free(req); /* Also frees lock. */
594                 ctdb_free_lock(lock);
595                 return;
596         }
597
598         /* Can we get lock now? */
599         if (try_readrecordlock(lock, &data)) {
600                 /* Now it's their responsibility to free lock & request! */
601                 req->extra_destructor = NULL;
602                 lock->callback(lock->ctdb_db, lock, data, private);
603                 ctdb_free_lock(lock);
604                 return;
605         }
606
607         /* Retransmit the same request again (we lost race). */
608         io_elem_reset(req->io);
609         DLIST_ADD(ctdb->outq, req);
610 }
611
612 bool
613 ctdb_readrecordlock_async(struct ctdb_db *ctdb_db, TDB_DATA key,
614                           ctdb_rrl_callback_t callback, void *cbdata)
615 {
616         struct ctdb_request *req;
617         struct ctdb_lock *lock;
618         TDB_DATA data;
619
620         /* Setup lock */
621         lock = malloc(sizeof(*lock) + key.dsize);
622         if (!lock) {
623                 return false;
624         }
625         lock->key.dptr = (void *)(lock + 1);
626         memcpy(lock->key.dptr, key.dptr, key.dsize);
627         lock->key.dsize = key.dsize;
628         lock->ctdb_db = ctdb_db;
629         lock->hdr = NULL;
630         lock->held = false;
631
632         /* Fast path. */
633         if (try_readrecordlock(lock, &data)) {
634                 callback(ctdb_db, lock, data, cbdata);
635                 ctdb_free_lock(lock);
636                 return true;
637         }
638
639         /* Slow path: create request. */
640         req = new_ctdb_request(offsetof(struct ctdb_req_call, data)
641                                + key.dsize, readrecordlock_retry, cbdata);
642         if (!req) {
643                 ctdb_release_lock(lock);
644                 ctdb_free_lock(lock);
645                 return NULL;
646         }
647         req->extra = lock;
648         req->extra_destructor = destroy_lock;
649         /* We store the original callback in the lock, and use our own. */
650         lock->callback = callback;
651
652         io_elem_init_req_header(req->io, CTDB_REQ_CALL, CTDB_CURRENT_NODE,
653                                 new_reqid(ctdb_db->ctdb));
654
655         req->hdr.call->flags = CTDB_IMMEDIATE_MIGRATION;
656         req->hdr.call->db_id = ctdb_db->id;
657         req->hdr.call->callid = CTDB_NULL_FUNC;
658         req->hdr.call->hopcount = 0;
659         req->hdr.call->keylen = key.dsize;
660         req->hdr.call->calldatalen = 0;
661         memcpy(req->hdr.call->data, key.dptr, key.dsize);
662         DLIST_ADD(ctdb_db->ctdb->outq, req);
663         return true;
664 }
665
666 int ctdb_writerecord(struct ctdb_lock *lock, TDB_DATA data)
667 {
668         if (lock->ctdb_db->persistent) {
669                 /* FIXME: Report error. */
670                 return -1;
671         }
672
673         if (!lock->held) {
674                 /* FIXME: Report error. */
675                 return -1;
676         }
677
678         return ctdb_local_store(lock->ctdb_db->tdb, lock->key, lock->hdr,
679                                 data);
680 }