remove the reclock file we store pnn counts in.
[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(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_marshall_buffer *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_marshall_buffer *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_marshall_buffer);
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_marshall_buffer, 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_marshall_buffer *reply = (struct ctdb_marshall_buffer *)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_marshall_buffer, 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         return 0;
463 }
464
465 /* this is called when the client process has completed ctdb_recovery_lock()
466    and has written data back to us through the pipe.
467 */
468 static void set_recmode_handler(struct event_context *ev, struct fd_event *fde, 
469                              uint16_t flags, void *private_data)
470 {
471         struct ctdb_set_recmode_state *state= talloc_get_type(private_data, 
472                                              struct ctdb_set_recmode_state);
473         char c = 0;
474         int ret;
475
476         /* we got a response from our child process so we can abort the
477            timeout.
478         */
479         talloc_free(state->te);
480         state->te = NULL;
481
482
483         /* read the childs status when trying to lock the reclock file.
484            child wrote 0 if everything is fine and 1 if it did manage
485            to lock the file, which would be a problem since that means
486            we got a request to exit from recovery but we could still lock
487            the file   which at this time SHOULD be locked by the recovery
488            daemon on the recmaster
489         */              
490         ret = read(state->fd[0], &c, 1);
491         if (ret != 1 || c != 0) {
492                 ctdb_request_control_reply(state->ctdb, state->c, NULL, -1, "managed to lock reclock file from inside daemon");
493                 talloc_free(state);
494                 return;
495         }
496
497         state->ctdb->recovery_mode = state->recmode;
498
499         ctdb_request_control_reply(state->ctdb, state->c, NULL, 0, NULL);
500         talloc_free(state);
501         return;
502 }
503
504 /*
505   set the recovery mode
506  */
507 int32_t ctdb_control_set_recmode(struct ctdb_context *ctdb, 
508                                  struct ctdb_req_control *c,
509                                  TDB_DATA indata, bool *async_reply,
510                                  const char **errormsg)
511 {
512         uint32_t recmode = *(uint32_t *)indata.dptr;
513         int ret;
514         struct ctdb_set_recmode_state *state;
515         pid_t parent = getpid();
516
517         if (ctdb->freeze_mode != CTDB_FREEZE_FROZEN) {
518                 DEBUG(DEBUG_ERR,("Attempt to change recovery mode to %u when not frozen\n", 
519                          recmode));
520                 (*errormsg) = "Cannot change recovery mode while not frozen";
521                 return -1;
522         }
523
524         if (recmode != ctdb->recovery_mode) {
525                 DEBUG(DEBUG_NOTICE,(__location__ " Recovery mode set to %s\n", 
526                          recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"ACTIVE"));
527         }
528
529         if (recmode != CTDB_RECOVERY_NORMAL ||
530             ctdb->recovery_mode != CTDB_RECOVERY_ACTIVE) {
531                 ctdb->recovery_mode = recmode;
532                 return 0;
533         }
534
535         /* some special handling when ending recovery mode */
536
537         /* force the databased to thaw */
538         if (ctdb->freeze_handle) {
539                 ctdb_control_thaw(ctdb);
540         }
541
542         state = talloc(ctdb, struct ctdb_set_recmode_state);
543         CTDB_NO_MEMORY(ctdb, state);
544
545         /* For the rest of what needs to be done, we need to do this in
546            a child process since 
547            1, the call to ctdb_recovery_lock() can block if the cluster
548               filesystem is in the process of recovery.
549            2, running of the script may take a while.
550         */
551         ret = pipe(state->fd);
552         if (ret != 0) {
553                 talloc_free(state);
554                 DEBUG(DEBUG_CRIT,(__location__ " Failed to open pipe for set_recmode child\n"));
555                 return -1;
556         }
557
558         state->child = fork();
559         if (state->child == (pid_t)-1) {
560                 close(state->fd[0]);
561                 close(state->fd[1]);
562                 talloc_free(state);
563                 return -1;
564         }
565
566         if (state->child == 0) {
567                 char cc = 0;
568                 close(state->fd[0]);
569
570                 /* we should not be able to get the lock on the nodes list, 
571                   as it should  be held by the recovery master 
572                 */
573                 if (ctdb_recovery_lock(ctdb, false)) {
574                         DEBUG(DEBUG_CRIT,("ERROR: recovery lock file %s not locked when recovering!\n", ctdb->recovery_lock_file));
575                         cc = 1;
576                 }
577
578                 write(state->fd[1], &cc, 1);
579                 /* make sure we die when our parent dies */
580                 while (kill(parent, 0) == 0 || errno != ESRCH) {
581                         sleep(5);
582                 }
583                 _exit(0);
584         }
585         close(state->fd[1]);
586
587         talloc_set_destructor(state, set_recmode_destructor);
588
589         state->te = event_add_timed(ctdb->ev, state, timeval_current_ofs(3, 0),
590                         ctdb_set_recmode_timeout, state);
591
592         state->fde = event_add_fd(ctdb->ev, state, state->fd[0],
593                                 EVENT_FD_READ|EVENT_FD_AUTOCLOSE,
594                                 set_recmode_handler,
595                                 (void *)state);
596         if (state->fde == NULL) {
597                 talloc_free(state);
598                 return -1;
599         }
600
601         state->ctdb    = ctdb;
602         state->recmode = recmode;
603         state->c       = talloc_steal(state, c);
604
605         *async_reply = true;
606
607         return 0;
608 }
609
610
611 /*
612   try and get the recovery lock in shared storage - should only work
613   on the recovery master recovery daemon. Anywhere else is a bug
614  */
615 bool ctdb_recovery_lock(struct ctdb_context *ctdb, bool keep)
616 {
617         struct flock lock;
618
619         if (ctdb->recovery_lock_fd != -1) {
620                 close(ctdb->recovery_lock_fd);
621         }
622         ctdb->recovery_lock_fd = open(ctdb->recovery_lock_file, O_RDWR|O_CREAT, 0600);
623         if (ctdb->recovery_lock_fd == -1) {
624                 DEBUG(DEBUG_ERR,("ctdb_recovery_lock: Unable to open %s - (%s)\n", 
625                          ctdb->recovery_lock_file, strerror(errno)));
626                 return false;
627         }
628
629         set_close_on_exec(ctdb->recovery_lock_fd);
630
631         lock.l_type = F_WRLCK;
632         lock.l_whence = SEEK_SET;
633         lock.l_start = 0;
634         lock.l_len = 1;
635         lock.l_pid = 0;
636
637         if (fcntl(ctdb->recovery_lock_fd, F_SETLK, &lock) != 0) {
638                 close(ctdb->recovery_lock_fd);
639                 ctdb->recovery_lock_fd = -1;
640                 if (keep) {
641                         DEBUG(DEBUG_CRIT,("ctdb_recovery_lock: Failed to get recovery lock on '%s'\n", ctdb->recovery_lock_file));
642                 }
643                 return false;
644         }
645
646         if (!keep) {
647                 close(ctdb->recovery_lock_fd);
648                 ctdb->recovery_lock_fd = -1;
649         }
650
651         DEBUG(DEBUG_NOTICE,("ctdb_recovery_lock: Got recovery lock on '%s'\n", ctdb->recovery_lock_file));
652
653         return true;
654 }
655
656 /*
657   delete a record as part of the vacuum process
658   only delete if we are not lmaster or dmaster, and our rsn is <= the provided rsn
659   use non-blocking locks
660
661   return 0 if the record was successfully deleted (i.e. it does not exist
662   when the function returns)
663   or !0 is the record still exists in the tdb after returning.
664  */
665 static int delete_tdb_record(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, struct ctdb_rec_data *rec)
666 {
667         TDB_DATA key, data;
668         struct ctdb_ltdb_header *hdr, *hdr2;
669         
670         /* these are really internal tdb functions - but we need them here for
671            non-blocking lock of the freelist */
672         int tdb_lock_nonblock(struct tdb_context *tdb, int list, int ltype);
673         int tdb_unlock(struct tdb_context *tdb, int list, int ltype);
674
675
676         key.dsize = rec->keylen;
677         key.dptr  = &rec->data[0];
678         data.dsize = rec->datalen;
679         data.dptr = &rec->data[rec->keylen];
680
681         if (ctdb_lmaster(ctdb, &key) == ctdb->pnn) {
682                 DEBUG(DEBUG_INFO,(__location__ " Called delete on record where we are lmaster\n"));
683                 return -1;
684         }
685
686         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
687                 DEBUG(DEBUG_ERR,(__location__ " Bad record size\n"));
688                 return -1;
689         }
690
691         hdr = (struct ctdb_ltdb_header *)data.dptr;
692
693         /* use a non-blocking lock */
694         if (tdb_chainlock_nonblock(ctdb_db->ltdb->tdb, key) != 0) {
695                 return -1;
696         }
697
698         data = tdb_fetch(ctdb_db->ltdb->tdb, key);
699         if (data.dptr == NULL) {
700                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
701                 return 0;
702         }
703
704         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
705                 if (tdb_lock_nonblock(ctdb_db->ltdb->tdb, -1, F_WRLCK) == 0) {
706                         tdb_delete(ctdb_db->ltdb->tdb, key);
707                         tdb_unlock(ctdb_db->ltdb->tdb, -1, F_WRLCK);
708                         DEBUG(DEBUG_CRIT,(__location__ " Deleted corrupt record\n"));
709                 }
710                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
711                 free(data.dptr);
712                 return 0;
713         }
714         
715         hdr2 = (struct ctdb_ltdb_header *)data.dptr;
716
717         if (hdr2->rsn > hdr->rsn) {
718                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
719                 DEBUG(DEBUG_INFO,(__location__ " Skipping record with rsn=%llu - called with rsn=%llu\n",
720                          (unsigned long long)hdr2->rsn, (unsigned long long)hdr->rsn));
721                 free(data.dptr);
722                 return -1;              
723         }
724
725         if (hdr2->dmaster == ctdb->pnn) {
726                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
727                 DEBUG(DEBUG_INFO,(__location__ " Attempted delete record where we are the dmaster\n"));
728                 free(data.dptr);
729                 return -1;                              
730         }
731
732         if (tdb_lock_nonblock(ctdb_db->ltdb->tdb, -1, F_WRLCK) != 0) {
733                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
734                 free(data.dptr);
735                 return -1;                              
736         }
737
738         if (tdb_delete(ctdb_db->ltdb->tdb, key) != 0) {
739                 tdb_unlock(ctdb_db->ltdb->tdb, -1, F_WRLCK);
740                 tdb_chainunlock(ctdb_db->ltdb->tdb, key);
741                 DEBUG(DEBUG_INFO,(__location__ " Failed to delete record\n"));
742                 free(data.dptr);
743                 return -1;                                              
744         }
745
746         tdb_unlock(ctdb_db->ltdb->tdb, -1, F_WRLCK);
747         tdb_chainunlock(ctdb_db->ltdb->tdb, key);
748         free(data.dptr);
749         return 0;       
750 }
751
752
753
754 struct recovery_callback_state {
755         struct ctdb_req_control *c;
756 };
757
758
759 /*
760   called when the 'recovered' event script has finished
761  */
762 static void ctdb_end_recovery_callback(struct ctdb_context *ctdb, int status, void *p)
763 {
764         struct recovery_callback_state *state = talloc_get_type(p, struct recovery_callback_state);
765
766         ctdb_enable_monitoring(ctdb);
767
768         if (status != 0) {
769                 DEBUG(DEBUG_ERR,(__location__ " recovered event script failed (status %d)\n", status));
770         }
771
772         ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
773         talloc_free(state);
774
775         gettimeofday(&ctdb->last_recovery_finished, NULL);
776 }
777
778 /*
779   recovery has finished
780  */
781 int32_t ctdb_control_end_recovery(struct ctdb_context *ctdb, 
782                                 struct ctdb_req_control *c,
783                                 bool *async_reply)
784 {
785         int ret;
786         struct recovery_callback_state *state;
787
788         DEBUG(DEBUG_NOTICE,("Recovery has finished\n"));
789
790         state = talloc(ctdb, struct recovery_callback_state);
791         CTDB_NO_MEMORY(ctdb, state);
792
793         state->c    = talloc_steal(state, c);
794
795         ctdb_disable_monitoring(ctdb);
796
797         ret = ctdb_event_script_callback(ctdb, 
798                                          timeval_current_ofs(ctdb->tunable.script_timeout, 0),
799                                          state, 
800                                          ctdb_end_recovery_callback, 
801                                          state, "recovered");
802
803         if (ret != 0) {
804                 ctdb_enable_monitoring(ctdb);
805
806                 DEBUG(DEBUG_ERR,(__location__ " Failed to end recovery\n"));
807                 talloc_free(state);
808                 return -1;
809         }
810
811         /* tell the control that we will be reply asynchronously */
812         *async_reply = true;
813         return 0;
814 }
815
816 /*
817   called when the 'startrecovery' event script has finished
818  */
819 static void ctdb_start_recovery_callback(struct ctdb_context *ctdb, int status, void *p)
820 {
821         struct recovery_callback_state *state = talloc_get_type(p, struct recovery_callback_state);
822
823         if (status != 0) {
824                 DEBUG(DEBUG_ERR,(__location__ " startrecovery event script failed (status %d)\n", status));
825         }
826
827         ctdb_request_control_reply(ctdb, state->c, NULL, status, NULL);
828         talloc_free(state);
829 }
830
831 /*
832   run the startrecovery eventscript
833  */
834 int32_t ctdb_control_start_recovery(struct ctdb_context *ctdb, 
835                                 struct ctdb_req_control *c,
836                                 bool *async_reply)
837 {
838         int ret;
839         struct recovery_callback_state *state;
840
841         DEBUG(DEBUG_NOTICE,(__location__ " startrecovery eventscript has been invoked\n"));
842         gettimeofday(&ctdb->last_recovery_started, NULL);
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  try to delete all these records as part of the vacuuming process
870  and return the records we failed to delete
871 */
872 int32_t ctdb_control_try_delete_records(struct ctdb_context *ctdb, TDB_DATA indata, TDB_DATA *outdata)
873 {
874         struct ctdb_marshall_buffer *reply = (struct ctdb_marshall_buffer *)indata.dptr;
875         struct ctdb_db_context *ctdb_db;
876         int i;
877         struct ctdb_rec_data *rec;
878         struct ctdb_marshall_buffer *records;
879
880         if (indata.dsize < offsetof(struct ctdb_marshall_buffer, data)) {
881                 DEBUG(DEBUG_ERR,(__location__ " invalid data in try_delete_records\n"));
882                 return -1;
883         }
884
885         ctdb_db = find_ctdb_db(ctdb, reply->db_id);
886         if (!ctdb_db) {
887                 DEBUG(DEBUG_ERR,(__location__ " Unknown db 0x%08x\n", reply->db_id));
888                 return -1;
889         }
890
891
892         DEBUG(DEBUG_DEBUG,("starting try_delete_records of %u records for dbid 0x%x\n",
893                  reply->count, reply->db_id));
894
895
896         /* create a blob to send back the records we couldnt delete */  
897         records = (struct ctdb_marshall_buffer *)
898                         talloc_zero_size(outdata, 
899                                     offsetof(struct ctdb_marshall_buffer, data));
900         if (records == NULL) {
901                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
902                 return -1;
903         }
904         records->db_id = ctdb_db->db_id;
905
906
907         rec = (struct ctdb_rec_data *)&reply->data[0];
908         for (i=0;i<reply->count;i++) {
909                 TDB_DATA key, data;
910
911                 key.dptr = &rec->data[0];
912                 key.dsize = rec->keylen;
913                 data.dptr = &rec->data[key.dsize];
914                 data.dsize = rec->datalen;
915
916                 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
917                         DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record in indata\n"));
918                         return -1;
919                 }
920
921                 /* If we cant delete the record we must add it to the reply
922                    so the lmaster knows it may not purge this record
923                 */
924                 if (delete_tdb_record(ctdb, ctdb_db, rec) != 0) {
925                         size_t old_size;
926                         struct ctdb_ltdb_header *hdr;
927
928                         hdr = (struct ctdb_ltdb_header *)data.dptr;
929                         data.dptr += sizeof(*hdr);
930                         data.dsize -= sizeof(*hdr);
931
932                         DEBUG(DEBUG_INFO, (__location__ " Failed to vacuum delete record with hash 0x%08x\n", ctdb_hash(&key)));
933
934                         old_size = talloc_get_size(records);
935                         records = talloc_realloc_size(outdata, records, old_size + rec->length);
936                         if (records == NULL) {
937                                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
938                                 return -1;
939                         }
940                         records->count++;
941                         memcpy(old_size+(uint8_t *)records, rec, rec->length);
942                 } 
943
944                 rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
945         }           
946
947
948         outdata->dptr = (uint8_t *)records;
949         outdata->dsize = talloc_get_size(records);
950
951         return 0;
952 }
953
954 /*
955   report capabilities
956  */
957 int32_t ctdb_control_get_capabilities(struct ctdb_context *ctdb, TDB_DATA *outdata)
958 {
959         uint32_t *capabilities = NULL;
960
961         capabilities = talloc(outdata, uint32_t);
962         CTDB_NO_MEMORY(ctdb, capabilities);
963         *capabilities = ctdb->capabilities;
964
965         outdata->dsize = sizeof(uint32_t);
966         outdata->dptr = (uint8_t *)capabilities;
967
968         return 0;       
969 }
970