bef945193047cfe51cd0ed8647da3be236d652a8
[sahlberg/ctdb.git] / server / ctdb_recover.c
1 /* 
2    ctdb recovery code
3
4    Copyright (C) Andrew Tridgell  2007
5    Copyright (C) Ronnie Sahlberg  2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20 #include "includes.h"
21 #include "lib/events/events.h"
22 #include "lib/tdb/include/tdb.h"
23 #include "system/time.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/wait.h"
27 #include "../include/ctdb_private.h"
28 #include "lib/util/dlinklist.h"
29 #include "db_wrap.h"
30
31 /*
32   lock all databases - mark only
33  */
34 static int ctdb_lock_all_databases_mark(struct ctdb_context *ctdb)
35 {
36         struct ctdb_db_context *ctdb_db;
37         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
38                 DEBUG(DEBUG_ERR,("Attempt to mark all databases locked when not frozen\n"));
39                 return -1;
40         }
41         for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
42                 if (tdb_lockall_mark(ctdb_db->ltdb->tdb) != 0) {
43                         return -1;
44                 }
45         }
46         return 0;
47 }
48
49 /*
50   lock all databases - unmark only
51  */
52 static int ctdb_lock_all_databases_unmark(struct ctdb_context *ctdb)
53 {
54         struct ctdb_db_context *ctdb_db;
55         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
56                 DEBUG(DEBUG_ERR,("Attempt to unmark all databases locked when not frozen\n"));
57                 return -1;
58         }
59         for (ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next) {
60                 if (tdb_lockall_unmark(ctdb_db->ltdb->tdb) != 0) {
61                         return -1;
62                 }
63         }
64         return 0;
65 }
66
67
68 int 
69 ctdb_control_getvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
70 {
71         CHECK_CONTROL_DATA_SIZE(0);
72         struct ctdb_vnn_map_wire *map;
73         size_t len;
74
75         len = offsetof(struct ctdb_vnn_map_wire, map) + sizeof(uint32_t)*ctdb->vnn_map->size;
76         map = talloc_size(outdata, len);
77         CTDB_NO_MEMORY_VOID(ctdb, map);
78
79         map->generation = ctdb->vnn_map->generation;
80         map->size = ctdb->vnn_map->size;
81         memcpy(map->map, ctdb->vnn_map->map, sizeof(uint32_t)*map->size);
82
83         outdata->dsize = len;
84         outdata->dptr  = (uint8_t *)map;
85
86         return 0;
87 }
88
89 int 
90 ctdb_control_setvnnmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
91 {
92         struct ctdb_vnn_map_wire *map = (struct ctdb_vnn_map_wire *)indata.dptr;
93
94         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
95                 DEBUG(DEBUG_ERR,("Attempt to set vnnmap when not frozen\n"));
96                 return -1;
97         }
98
99         talloc_free(ctdb->vnn_map);
100
101         ctdb->vnn_map = talloc(ctdb, struct ctdb_vnn_map);
102         CTDB_NO_MEMORY(ctdb, ctdb->vnn_map);
103
104         ctdb->vnn_map->generation = map->generation;
105         ctdb->vnn_map->size       = map->size;
106         ctdb->vnn_map->map = talloc_array(ctdb->vnn_map, uint32_t, map->size);
107         CTDB_NO_MEMORY(ctdb, ctdb->vnn_map->map);
108
109         memcpy(ctdb->vnn_map->map, map->map, sizeof(uint32_t)*map->size);
110
111         return 0;
112 }
113
114 int 
115 ctdb_control_getdbmap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
116 {
117         uint32_t i, len;
118         struct ctdb_db_context *ctdb_db;
119         struct ctdb_dbid_map *dbid_map;
120
121         CHECK_CONTROL_DATA_SIZE(0);
122
123         len = 0;
124         for(ctdb_db=ctdb->db_list;ctdb_db;ctdb_db=ctdb_db->next){
125                 len++;
126         }
127
128
129         outdata->dsize = offsetof(struct ctdb_dbid_map, dbs) + sizeof(dbid_map->dbs[0])*len;
130         outdata->dptr  = (unsigned char *)talloc_zero_size(outdata, outdata->dsize);
131         if (!outdata->dptr) {
132                 DEBUG(DEBUG_ALERT, (__location__ " Failed to allocate dbmap array\n"));
133                 exit(1);
134         }
135
136         dbid_map = (struct ctdb_dbid_map *)outdata->dptr;
137         dbid_map->num = len;
138         for (i=0,ctdb_db=ctdb->db_list;ctdb_db;i++,ctdb_db=ctdb_db->next){
139                 dbid_map->dbs[i].dbid       = ctdb_db->db_id;
140                 dbid_map->dbs[i].persistent = ctdb_db->persistent;
141         }
142
143         return 0;
144 }
145
146 int 
147 ctdb_control_getnodemap(struct ctdb_context *ctdb, uint32_t opcode, TDB_DATA indata, TDB_DATA *outdata)
148 {
149         uint32_t i, num_nodes;
150         struct ctdb_node_map *node_map;
151
152         CHECK_CONTROL_DATA_SIZE(0);
153
154         num_nodes = ctdb->num_nodes;
155
156         outdata->dsize = offsetof(struct ctdb_node_map, nodes) + num_nodes*sizeof(struct ctdb_node_and_flags);
157         outdata->dptr  = (unsigned char *)talloc_zero_size(outdata, outdata->dsize);
158         if (!outdata->dptr) {
159                 DEBUG(DEBUG_ALERT, (__location__ " Failed to allocate nodemap array\n"));
160                 exit(1);
161         }
162
163         node_map = (struct ctdb_node_map *)outdata->dptr;
164         node_map->num = num_nodes;
165         for (i=0; i<num_nodes; i++) {
166                 inet_aton(ctdb->nodes[i]->address.address, &node_map->nodes[i].sin.sin_addr);
167                 node_map->nodes[i].pnn   = ctdb->nodes[i]->pnn;
168                 node_map->nodes[i].flags = ctdb->nodes[i]->flags;
169         }
170
171         return 0;
172 }
173
174 static void
175 ctdb_reload_nodes_event(struct event_context *ev, struct timed_event *te, 
176                                struct timeval t, void *private_data)
177 {
178         int ret;
179         struct ctdb_context *ctdb = talloc_get_type(private_data, struct ctdb_context);
180         int ctdb_tcp_init(struct ctdb_context *);
181
182         /* shut down the transport */
183         if (ctdb->methods != NULL) {
184                 ctdb->methods->shutdown(ctdb);
185         }
186
187         /* start the transport again */
188         ctdb_load_nodes_file(ctdb);
189         ret = ctdb_tcp_init(ctdb);
190         if (ret != 0) {
191                 DEBUG(DEBUG_CRIT, (__location__ " Failed to init TCP\n"));
192                 exit(1);
193         }
194
195         if (ctdb->methods == NULL) {
196                 DEBUG(DEBUG_ALERT,(__location__ " Can not restart transport. ctdb->methods==NULL\n"));
197                 ctdb_fatal(ctdb, "can not reinitialize transport.");
198         }
199         ctdb->methods->initialise(ctdb);
200         ctdb->methods->start(ctdb);
201
202         return;
203 }
204
205 /*
206   reload the nodes file after a short delay (so that we can send the response
207   back first
208 */
209 int 
210 ctdb_control_reload_nodes_file(struct ctdb_context *ctdb, uint32_t opcode)
211 {
212         event_add_timed(ctdb->ev, ctdb, timeval_current_ofs(1,0), ctdb_reload_nodes_event, ctdb);
213
214         return 0;
215 }
216
217 /* 
218    a traverse function for pulling all relevent records from pulldb
219  */
220 struct pulldb_data {
221         struct ctdb_context *ctdb;
222         struct ctdb_control_pulldb_reply *pulldata;
223         uint32_t len;
224         bool failed;
225 };
226
227 static int traverse_pulldb(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *p)
228 {
229         struct pulldb_data *params = (struct pulldb_data *)p;
230         struct ctdb_rec_data *rec;
231
232         /* add the record to the blob */
233         rec = ctdb_marshall_record(params->pulldata, 0, key, NULL, data);
234         if (rec == NULL) {
235                 params->failed = true;
236                 return -1;
237         }
238         params->pulldata = talloc_realloc_size(NULL, params->pulldata, rec->length + params->len);
239         if (params->pulldata == NULL) {
240                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand pulldb_data to %u (%u records)\n", 
241                          rec->length + params->len, params->pulldata->count));
242                 params->failed = true;
243                 return -1;
244         }
245         params->pulldata->count++;
246         memcpy(params->len+(uint8_t *)params->pulldata, rec, rec->length);
247         params->len += rec->length;
248         talloc_free(rec);
249
250         return 0;
251 }
252
253 /*
254   pul a bunch of records from a ltdb, filtering by lmaster
255  */
256 int32_t ctdb_control_pull_db(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
257 {
258         struct ctdb_control_pulldb *pull;
259         struct ctdb_db_context *ctdb_db;
260         struct pulldb_data params;
261         struct ctdb_control_pulldb_reply *reply;
262
263         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
264                 DEBUG(DEBUG_DEBUG,("rejecting ctdb_control_pull_db when not frozen\n"));
265                 return -1;
266         }
267
268         pull = (struct ctdb_control_pulldb *)indata.dptr;
269         
270         ctdb_db = find_ctdb_db(ctdb, pull->db_id);
271         if (!ctdb_db) {
272                 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%08x\n", pull->db_id));
273                 return -1;
274         }
275
276         reply = talloc_zero(outdata, struct ctdb_control_pulldb_reply);
277         CTDB_NO_MEMORY(ctdb, reply);
278
279         reply->db_id = pull->db_id;
280
281         params.ctdb = ctdb;
282         params.pulldata = reply;
283         params.len = offsetof(struct ctdb_control_pulldb_reply, data);
284         params.failed = false;
285
286         if (ctdb_lock_all_databases_mark(ctdb) != 0) {
287                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock on entired db - failing\n"));
288                 return -1;
289         }
290
291         if (tdb_traverse_read(ctdb_db->ltdb->tdb, traverse_pulldb, &params) == -1) {
292                 DEBUG(DEBUG_ERR,(__location__ " Failed to get traverse db '%s'\n", ctdb_db->db_name));
293                 ctdb_lock_all_databases_unmark(ctdb);
294                 talloc_free(params.pulldata);
295                 return -1;
296         }
297
298         ctdb_lock_all_databases_unmark(ctdb);
299
300         outdata->dptr = (uint8_t *)params.pulldata;
301         outdata->dsize = params.len;
302
303         return 0;
304 }
305
306 /*
307   push a bunch of records into a ltdb, filtering by rsn
308  */
309 int32_t ctdb_control_push_db(struct ctdb_context *ctdb, TDB_DATA indata)
310 {
311         struct ctdb_control_pulldb_reply *reply = (struct ctdb_control_pulldb_reply *)indata.dptr;
312         struct ctdb_db_context *ctdb_db;
313         int i, ret;
314         struct ctdb_rec_data *rec;
315
316         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
317                 DEBUG(DEBUG_DEBUG,("rejecting ctdb_control_push_db when not frozen\n"));
318                 return -1;
319         }
320
321         if (indata.dsize < offsetof(struct ctdb_control_pulldb_reply, data)) {
322                 DEBUG(DEBUG_ERR,(__location__ " invalid data in pulldb reply\n"));
323                 return -1;
324         }
325
326         ctdb_db = find_ctdb_db(ctdb, reply->db_id);
327         if (!ctdb_db) {
328                 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%08x\n", reply->db_id));
329                 return -1;
330         }
331
332         if (ctdb_lock_all_databases_mark(ctdb) != 0) {
333                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock on entired db - failing\n"));
334                 return -1;
335         }
336
337         rec = (struct ctdb_rec_data *)&reply->data[0];
338
339         DEBUG(DEBUG_INFO,("starting push of %u records for dbid 0x%x\n",
340                  reply->count, reply->db_id));
341
342         for (i=0;i<reply->count;i++) {
343                 TDB_DATA key, data;
344                 struct ctdb_ltdb_header *hdr;
345
346                 key.dptr = &rec->data[0];
347                 key.dsize = rec->keylen;
348                 data.dptr = &rec->data[key.dsize];
349                 data.dsize = rec->datalen;
350
351                 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
352                         DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
353                         goto failed;
354                 }
355                 hdr = (struct ctdb_ltdb_header *)data.dptr;
356                 data.dptr += sizeof(*hdr);
357                 data.dsize -= sizeof(*hdr);
358
359                 ret = ctdb_ltdb_store(ctdb_db, key, hdr, data);
360                 if (ret != 0) {
361                         DEBUG(DEBUG_CRIT, (__location__ " Unable to store record\n"));
362                         goto failed;
363                 }
364
365                 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
366         }           
367
368         DEBUG(DEBUG_DEBUG,("finished push of %u records for dbid 0x%x\n",
369                  reply->count, reply->db_id));
370
371         ctdb_lock_all_databases_unmark(ctdb);
372         return 0;
373
374 failed:
375         ctdb_lock_all_databases_unmark(ctdb);
376         return -1;
377 }
378
379
380 static int traverse_setdmaster(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *p)
381 {
382         uint32_t *dmaster = (uint32_t *)p;
383         struct ctdb_ltdb_header *header = (struct ctdb_ltdb_header *)data.dptr;
384         int ret;
385
386         /* skip if already correct */
387         if (header->dmaster == *dmaster) {
388                 return 0;
389         }
390
391         header->dmaster = *dmaster;
392
393         ret = tdb_store(tdb, key, data, TDB_REPLACE);
394         if (ret) {
395                 DEBUG(DEBUG_CRIT,(__location__ " failed to write tdb data back  ret:%d\n",ret));
396                 return ret;
397         }
398
399         /* TODO: add error checking here */
400
401         return 0;
402 }
403
404 int32_t ctdb_control_set_dmaster(struct ctdb_context *ctdb, TDB_DATA indata)
405 {
406         struct ctdb_control_set_dmaster *p = (struct ctdb_control_set_dmaster *)indata.dptr;
407         struct ctdb_db_context *ctdb_db;
408
409         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
410                 DEBUG(DEBUG_DEBUG,("rejecting ctdb_control_set_dmaster when not frozen\n"));
411                 return -1;
412         }
413
414         ctdb_db = find_ctdb_db(ctdb, p->db_id);
415         if (!ctdb_db) {
416                 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%08x\n", p->db_id));
417                 return -1;
418         }
419
420         if (ctdb_lock_all_databases_mark(ctdb) != 0) {
421                 DEBUG(DEBUG_ERR,(__location__ " Failed to get lock on entired db - failing\n"));
422                 return -1;
423         }
424
425         tdb_traverse(ctdb_db->ltdb->tdb, traverse_setdmaster, &p->dmaster);
426
427         ctdb_lock_all_databases_unmark(ctdb);
428         
429         return 0;
430 }
431
432 struct ctdb_set_recmode_state {
433         struct ctdb_context *ctdb;
434         struct ctdb_req_control *c;
435         uint32_t recmode;
436         int fd[2];
437         struct timed_event *te;
438         struct fd_event *fde;
439         pid_t child;
440 };
441
442 /*
443   called if our set_recmode child times out. this would happen if
444   ctdb_recovery_lock() would block.
445  */
446 static void ctdb_set_recmode_timeout(struct event_context *ev, struct timed_event *te, 
447                                          struct timeval t, void *private_data)
448 {
449         struct ctdb_set_recmode_state *state = talloc_get_type(private_data, 
450                                            struct ctdb_set_recmode_state);
451
452         ctdb_request_control_reply(state->ctdb, state->c, NULL, -1, "timeout in ctdb_set_recmode");
453         talloc_free(state);
454 }
455
456
457 /* when we free the recmode state we must kill any child process.
458 */
459 static int set_recmode_destructor(struct ctdb_set_recmode_state *state)
460 {
461         kill(state->child, SIGKILL);
462         waitpid(state->child, NULL, 0);
463         return 0;
464 }
465
466 /* this is called when the client process has completed ctdb_recovery_lock()
467    and has written data back to us through the pipe.
468 */
469 static void set_recmode_handler(struct event_context *ev, struct fd_event *fde, 
470                              uint16_t flags, void *private_data)
471 {
472         struct ctdb_set_recmode_state *state= talloc_get_type(private_data, 
473                                              struct ctdb_set_recmode_state);
474         char c = 0;
475         int ret;
476
477         /* we got a response from our child process so we can abort the
478            timeout.
479         */
480         talloc_free(state->te);
481         state->te = NULL;
482
483
484         /* read the childs status when trying to lock the reclock file.
485            child wrote 0 if everything is fine and 1 if it did manage
486            to lock the file, which would be a problem since that means
487            we got a request to exit from recovery but we could still lock
488            the file   which at this time SHOULD be locked by the recovery
489            daemon on the recmaster
490         */              
491         ret = read(state->fd[0], &c, 1);
492         if (ret != 1 || c != 0) {
493                 ctdb_request_control_reply(state->ctdb, state->c, NULL, -1, "managed to lock reclock file from inside daemon");
494                 talloc_free(state);
495                 return;
496         }
497
498         state->ctdb->recovery_mode = state->recmode;
499
500         ctdb_request_control_reply(state->ctdb, state->c, NULL, 0, NULL);
501         talloc_free(state);
502         return;
503 }
504
505 /*
506   set the recovery mode
507  */
508 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
509                                  struct ctdb_req_control *c,
510                                  TDB_DATA indata, bool *async_reply,
511                                  const char **errormsg)
512 {
513         uint32_t recmode = *(uint32_t *)indata.dptr;
514         int ret;
515         struct ctdb_set_recmode_state *state;
516         pid_t parent = getpid();
517
518         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
519                 DEBUG(DEBUG_ERR,("Attempt to change recovery mode to %u when not frozen\n", 
520                          recmode));
521                 (*errormsg) = "Cannot change recovery mode while not frozen";
522                 return -1;
523         }
524
525         if (recmode != ctdb->recovery_mode) {
526                 DEBUG(DEBUG_NOTICE,(__location__ " Recovery mode set to %s\n", 
527                          recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"ACTIVE"));
528         }
529
530         if (recmode != CTDB_RECOVERY_NORMAL ||
531             ctdb->recovery_mode != CTDB_RECOVERY_ACTIVE) {
532                 ctdb->recovery_mode = recmode;
533                 return 0;
534         }
535
536         /* some special handling when ending recovery mode */
537
538         /* force the databased to thaw */
539         if (ctdb->freeze_handle) {
540                 ctdb_control_thaw(ctdb);
541         }
542
543         state = talloc(ctdb, struct ctdb_set_recmode_state);
544         CTDB_NO_MEMORY(ctdb, state);
545
546         /* For the rest of what needs to be done, we need to do this in
547            a child process since 
548            1, the call to ctdb_recovery_lock() can block if the cluster
549               filesystem is in the process of recovery.
550            2, running of the script may take a while.
551         */
552         ret = pipe(state->fd);
553         if (ret != 0) {
554                 talloc_free(state);
555                 DEBUG(DEBUG_CRIT,(__location__ " Failed to open pipe for set_recmode child\n"));
556                 return -1;
557         }
558
559         state->child = fork();
560         if (state->child == (pid_t)-1) {
561                 close(state->fd[0]);
562                 close(state->fd[1]);
563                 talloc_free(state);
564                 return -1;
565         }
566
567         if (state->child == 0) {
568                 char cc = 0;
569                 close(state->fd[0]);
570
571                 /* we should not be able to get the lock on the nodes list, 
572                   as it should  be held by the recovery master 
573                 */
574                 if (ctdb_recovery_lock(ctdb, false)) {
575                         DEBUG(DEBUG_CRIT,("ERROR: recovery lock file %s not locked when recovering!\n", ctdb->recovery_lock_file));
576                         cc = 1;
577                 }
578
579                 write(state->fd[1], &cc, 1);
580                 /* make sure we die when our parent dies */
581                 while (kill(parent, 0) == 0 || errno != ESRCH) {
582                         sleep(5);
583                 }
584                 _exit(0);
585         }
586         close(state->fd[1]);
587
588         talloc_set_destructor(state, set_recmode_destructor);
589
590         state->te = event_add_timed(ctdb->ev, state, timeval_current_ofs(3, 0),
591                         ctdb_set_recmode_timeout, state);
592
593         state->fde = event_add_fd(ctdb->ev, state, state->fd[0],
594                                 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
595                                 set_recmode_handler,
596                                 (void *)state);
597         if (state->fde == NULL) {
598                 talloc_free(state);
599                 return -1;
600         }
601
602         state->ctdb    = ctdb;
603         state->recmode = recmode;
604         state->c       = talloc_steal(state, c);
605
606         *async_reply = true;
607
608         return 0;
609 }
610
611
612 /*
613   try and get the recovery lock in shared storage - should only work
614   on the recovery master recovery daemon. Anywhere else is a bug
615  */
616 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep)
617 {
618         struct flock lock;
619
620         if (ctdb->recovery_lock_fd != -1) {
621                 close(ctdb->recovery_lock_fd);
622         }
623         ctdb->recovery_lock_fd = open(ctdb->recovery_lock_file, O_RDWR|O_CREAT, 0600);
624         if (ctdb->recovery_lock_fd == -1) {
625                 DEBUG(DEBUG_ERR,("ctdb_recovery_lock: Unable to open %s - (%s)\n", 
626                          ctdb->recovery_lock_file, strerror(errno)));
627                 return false;
628         }
629
630         set_close_on_exec(ctdb->recovery_lock_fd);
631
632         lock.l_type = F_WRLCK;
633         lock.l_whence = SEEK_SET;
634         lock.l_start = 0;
635         lock.l_len = 1;
636         lock.l_pid = 0;
637
638         if (fcntl(ctdb->recovery_lock_fd, F_SETLK, &lock) != 0) {
639                 close(ctdb->recovery_lock_fd);
640                 ctdb->recovery_lock_fd = -1;
641                 if (keep) {
642                         DEBUG(DEBUG_CRIT,("ctdb_recovery_lock: Failed to get recovery lock on '%s'\n", ctdb->recovery_lock_file));
643                 }
644                 return false;
645         }
646
647         if (!keep) {
648                 close(ctdb->recovery_lock_fd);
649                 ctdb->recovery_lock_fd = -1;
650         }
651
652         DEBUG(DEBUG_NOTICE,("ctdb_recovery_lock: Got recovery lock on '%s'\n", ctdb->recovery_lock_file));
653
654         return true;
655 }
656
657 /*
658   delete a record as part of the vacuum process
659   only delete if we are not lmaster or dmaster, and our rsn is <= the provided rsn
660   use non-blocking locks
661
662   return 0 if the record was successfully deleted (i.e. it does not exist
663   when the function returns)
664   or !0 is the record still exists in the tdb after returning.
665  */
666 static int delete_tdb_record(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, struct ctdb_rec_data *rec)
667 {
668         TDB_DATA key, data;
669         struct ctdb_ltdb_header *hdr, *hdr2;
670         
671         /* these are really internal tdb functions - but we need them here for
672            non-blocking lock of the freelist */
673         int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype);
674         int tdb_unlock(struct tdb_context *tdb, int list, int ltype);
675
676
677         key.dsize = rec->keylen;
678         key.dptr  = &rec->data[0];
679         data.dsize = rec->datalen;
680         data.dptr = &rec->data[rec->keylen];
681
682         if (ctdb_lmaster(ctdb, &key) == ctdb->pnn) {
683                 DEBUG(DEBUG_INFO,(__location__ " Called delete on record where we are lmaster\n"));
684                 return -1;
685         }
686
687         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
688                 DEBUG(DEBUG_ERR,(__location__ " Bad record size\n"));
689                 return -1;
690         }
691
692         hdr = (struct ctdb_ltdb_header *)data.dptr;
693
694         /* use a non-blocking lock */
695         if (tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, key) != 0) {
696                 return -1;
697         }
698
699         data = tdb_fetch(ctdb_db->ltdb->tdb, key);
700         if (data.dptr == NULL) {
701                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
702                 return 0;
703         }
704
705         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
706                 if (tdb_lock_nonblock(ctdb_db->ltdb->tdb, -1, F_WRLCK) == 0) {
707                         tdb_delete(ctdb_db->ltdb->tdb, key);
708                         tdb_unlock(ctdb_db->ltdb->tdb, -1, F_WRLCK);
709                         DEBUG(DEBUG_CRIT,(__location__ " Deleted corrupt record\n"));
710                 }
711                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
712                 free(data.dptr);
713                 return 0;
714         }
715         
716         hdr2 = (struct ctdb_ltdb_header *)data.dptr;
717
718         if (hdr2->rsn > hdr->rsn) {
719                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
720                 DEBUG(DEBUG_INFO,(__location__ " Skipping record with rsn=%llu - called with rsn=%llu\n",
721                          (unsigned long long)hdr2->rsn, (unsigned long long)hdr->rsn));
722                 free(data.dptr);
723                 return -1;              
724         }
725
726         if (hdr2->dmaster == ctdb->pnn) {
727                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
728                 DEBUG(DEBUG_INFO,(__location__ " Attempted delete record where we are the dmaster\n"));
729                 free(data.dptr);
730                 return -1;                              
731         }
732
733         if (tdb_lock_nonblock(ctdb_db->ltdb->tdb, -1, F_WRLCK) != 0) {
734                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
735                 free(data.dptr);
736                 return -1;                              
737         }
738
739         if (tdb_delete(ctdb_db->ltdb->tdb, key) != 0) {
740                 tdb_unlock(ctdb_db->ltdb->tdb, -1, F_WRLCK);
741                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
742                 DEBUG(DEBUG_INFO,(__location__ " Failed to delete record\n"));
743                 free(data.dptr);
744                 return -1;                                              
745         }
746
747         tdb_unlock(ctdb_db->ltdb->tdb, -1, F_WRLCK);
748         tdb_chainunlock(ctdb_db->ltdb->tdb, key);
749         free(data.dptr);
750         return 0;       
751 }
752
753
754
755 struct recovery_callback_state {
756         struct ctdb_req_control *c;
757 };
758
759
760 /*
761   called when the 'recovered' event script has finished
762  */
763 static void ctdb_end_recovery_callback(struct ctdb_context *ctdb, int status, void *p)
764 {
765         struct recovery_callback_state *state = talloc_get_type(p, struct recovery_callback_state);
766
767         ctdb_enable_monitoring(ctdb);
768
769         if (status != 0) {
770                 DEBUG(DEBUG_ERR,(__location__ " recovered event script failed (status %d)\n", status));
771         }
772
773         ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
774         talloc_free(state);
775
776         gettimeofday(&ctdb->last_recovery_time, NULL);
777 }
778
779 /*
780   recovery has finished
781  */
782 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
783                                 struct ctdb_req_control *c,
784                                 bool *async_reply)
785 {
786         int ret;
787         struct recovery_callback_state *state;
788
789         DEBUG(DEBUG_NOTICE,("Recovery has finished\n"));
790
791         state = talloc(ctdb, struct recovery_callback_state);
792         CTDB_NO_MEMORY(ctdb, state);
793
794         state->c    = talloc_steal(state, c);
795
796         ctdb_disable_monitoring(ctdb);
797
798         ret = ctdb_event_script_callback(ctdb, 
799                                          timeval_current_ofs(ctdb->tunable.script_timeout, 0),
800                                          state, 
801                                          ctdb_end_recovery_callback, 
802                                          state, "recovered");
803
804         if (ret != 0) {
805                 ctdb_enable_monitoring(ctdb);
806
807                 DEBUG(DEBUG_ERR,(__location__ " Failed to end recovery\n"));
808                 talloc_free(state);
809                 return -1;
810         }
811
812         /* tell the control that we will be reply asynchronously */
813         *async_reply = true;
814         return 0;
815 }
816
817 /*
818   called when the 'startrecovery' event script has finished
819  */
820 static void ctdb_start_recovery_callback(struct ctdb_context *ctdb, int status, void *p)
821 {
822         struct recovery_callback_state *state = talloc_get_type(p, struct recovery_callback_state);
823
824         if (status != 0) {
825                 DEBUG(DEBUG_ERR,(__location__ " startrecovery event script failed (status %d)\n", status));
826         }
827
828         ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
829         talloc_free(state);
830 }
831
832 /*
833   start a recuvery
834  */
835 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
836                                 struct ctdb_req_control *c,
837                                 bool *async_reply)
838 {
839         int ret;
840         struct recovery_callback_state *state;
841
842         DEBUG(DEBUG_NOTICE,("Recovery has started\n"));
843
844         state = talloc(ctdb, struct recovery_callback_state);
845         CTDB_NO_MEMORY(ctdb, state);
846
847         state->c    = talloc_steal(state, c);
848
849         ctdb_disable_monitoring(ctdb);
850
851         ret = ctdb_event_script_callback(ctdb, 
852                                          timeval_current_ofs(ctdb->tunable.script_timeout, 0),
853                                          state, 
854                                          ctdb_start_recovery_callback, 
855                                          state, "startrecovery");
856
857         if (ret != 0) {
858                 DEBUG(DEBUG_ERR,(__location__ " Failed to start recovery\n"));
859                 talloc_free(state);
860                 return -1;
861         }
862
863         /* tell the control that we will be reply asynchronously */
864         *async_reply = true;
865         return 0;
866 }
867
868 /*
869   report the location for the reclock file
870  */
871 int32_t ctdb_control_get_reclock_file(struct ctdb_context *ctdb, TDB_DATA *outdata)
872 {
873         char *reclock = NULL;
874
875         reclock = talloc_strdup(outdata, ctdb->recovery_lock_file);
876         CTDB_NO_MEMORY(ctdb, reclock);
877
878         outdata->dsize = strlen(reclock)+1;
879         outdata->dptr = (uint8_t *)reclock;
880
881         return 0;       
882 }
883
884 /*
885  try to delete all these records as part of the vacuuming process
886  and return the records we failed to delete
887 */
888 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
889 {
890         struct ctdb_control_pulldb_reply *reply = (struct ctdb_control_pulldb_reply *)indata.dptr;
891         struct ctdb_db_context *ctdb_db;
892         int i;
893         struct ctdb_rec_data *rec;
894         struct ctdb_control_pulldb_reply *records;
895
896         if (indata.dsize < offsetof(struct ctdb_control_pulldb_reply, data)) {
897                 DEBUG(DEBUG_ERR,(__location__ " invalid data in try_delete_records\n"));
898                 return -1;
899         }
900
901         ctdb_db = find_ctdb_db(ctdb, reply->db_id);
902         if (!ctdb_db) {
903                 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%08x\n", reply->db_id));
904                 return -1;
905         }
906
907
908         DEBUG(DEBUG_DEBUG,("starting try_delete_records of %u records for dbid 0x%x\n",
909                  reply->count, reply->db_id));
910
911
912         /* create a blob to send back the records we couldnt delete */  
913         records = (struct ctdb_control_pulldb_reply *)
914                         talloc_zero_size(outdata, 
915                                     offsetof(struct ctdb_control_pulldb_reply, data));
916         if (records == NULL) {
917                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
918                 return -1;
919         }
920         records->db_id = ctdb_db->db_id;
921
922
923         rec = (struct ctdb_rec_data *)&reply->data[0];
924         for (i=0;i<reply->count;i++) {
925                 TDB_DATA key, data;
926
927                 key.dptr = &rec->data[0];
928                 key.dsize = rec->keylen;
929                 data.dptr = &rec->data[key.dsize];
930                 data.dsize = rec->datalen;
931
932                 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
933                         DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record in indata\n"));
934                         return -1;
935                 }
936
937                 /* If we cant delete the record we must add it to the reply
938                    so the lmaster knows it may not purge this record
939                 */
940                 if (delete_tdb_record(ctdb, ctdb_db, rec) != 0) {
941                         size_t old_size;
942                         struct ctdb_ltdb_header *hdr;
943
944                         hdr = (struct ctdb_ltdb_header *)data.dptr;
945                         data.dptr += sizeof(*hdr);
946                         data.dsize -= sizeof(*hdr);
947
948                         DEBUG(DEBUG_INFO, (__location__ " Failed to vacuum delete record with hash 0x%08x\n", ctdb_hash(&key)));
949
950                         old_size = talloc_get_size(records);
951                         records = talloc_realloc_size(outdata, records, old_size + rec->length);
952                         if (records == NULL) {
953                                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
954                                 return -1;
955                         }
956                         records->count++;
957                         memcpy(old_size+(uint8_t *)records, rec, rec->length);
958                 } 
959
960                 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
961         }           
962
963
964         outdata->dptr = (uint8_t *)records;
965         outdata->dsize = talloc_get_size(records);
966
967         return 0;
968 }
969
970 /*
971   report capabilities
972  */
973 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata)
974 {
975         uint32_t *capabilities = NULL;
976
977         capabilities = talloc(outdata, uint32_t);
978         CTDB_NO_MEMORY(ctdb, capabilities);
979         *capabilities = ctdb->capabilities;
980
981         outdata->dsize = sizeof(uint32_t);
982         outdata->dptr = (uint8_t *)capabilities;
983
984         return 0;       
985 }
986