ctdb-vacuum: rename private->private_data in vacuum_traverse
[obnox/samba/samba-obnox.git] / ctdb / server / ctdb_vacuum.c
1 /*
2    ctdb vacuuming events
3
4    Copyright (C) Ronnie Sahlberg  2009
5    Copyright (C) Michael Adam 2010-2013
6    Copyright (C) Stefan Metzmacher 2010-2011
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "system/dir.h"
27 #include "../include/ctdb_private.h"
28 #include "db_wrap.h"
29 #include "lib/util/dlinklist.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32
33 #define TIMELIMIT() timeval_current_ofs(10, 0)
34
35 enum vacuum_child_status { VACUUM_RUNNING, VACUUM_OK, VACUUM_ERROR, VACUUM_TIMEOUT};
36
37 struct ctdb_vacuum_child_context {
38         struct ctdb_vacuum_child_context *next, *prev;
39         struct ctdb_vacuum_handle *vacuum_handle;
40         /* fd child writes status to */
41         int fd[2];
42         pid_t child_pid;
43         enum vacuum_child_status status;
44         struct timeval start_time;
45 };
46
47 struct ctdb_vacuum_handle {
48         struct ctdb_db_context *ctdb_db;
49         struct ctdb_vacuum_child_context *child_ctx;
50         uint32_t fast_path_count;
51 };
52
53
54 /*  a list of records to possibly delete */
55 struct vacuum_data {
56         uint32_t vacuum_limit;
57         uint32_t repack_limit;
58         struct ctdb_context *ctdb;
59         struct ctdb_db_context *ctdb_db;
60         struct tdb_context *dest_db;
61         trbt_tree_t *delete_list;
62         uint32_t delete_count;
63         struct ctdb_marshall_buffer **vacuum_fetch_list;
64         struct timeval start;
65         bool traverse_error;
66         bool vacuum;
67         uint32_t total;
68         uint32_t vacuumed;
69         uint32_t copied;
70         uint32_t fast_added_to_vacuum_fetch_list;
71         uint32_t fast_added_to_delete_list;
72         uint32_t fast_deleted;
73         uint32_t fast_skipped;
74         uint32_t fast_error;
75         uint32_t fast_total;
76         uint32_t full_added_to_vacuum_fetch_list;
77         uint32_t full_added_to_delete_list;
78         uint32_t full_skipped;
79         uint32_t full_error;
80         uint32_t full_total;
81         uint32_t delete_left;
82         uint32_t delete_remote_error;
83         uint32_t delete_local_error;
84         uint32_t delete_deleted;
85         uint32_t delete_skipped;
86 };
87
88 /* this structure contains the information for one record to be deleted */
89 struct delete_record_data {
90         struct ctdb_context *ctdb;
91         struct ctdb_db_context *ctdb_db;
92         struct ctdb_ltdb_header hdr;
93         TDB_DATA key;
94         uint8_t keydata[1];
95 };
96
97 struct delete_records_list {
98         struct ctdb_marshall_buffer *records;
99         struct vacuum_data *vdata;
100 };
101
102 /**
103  * Store key and header in a tree, indexed by the key hash.
104  */
105 static int insert_delete_record_data_into_tree(struct ctdb_context *ctdb,
106                                                struct ctdb_db_context *ctdb_db,
107                                                trbt_tree_t *tree,
108                                                const struct ctdb_ltdb_header *hdr,
109                                                TDB_DATA key)
110 {
111         struct delete_record_data *dd;
112         uint32_t hash;
113         size_t len;
114
115         len = offsetof(struct delete_record_data, keydata) + key.dsize;
116
117         dd = (struct delete_record_data *)talloc_size(tree, len);
118         if (dd == NULL) {
119                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
120                 return -1;
121         }
122         talloc_set_name_const(dd, "struct delete_record_data");
123
124         dd->ctdb      = ctdb;
125         dd->ctdb_db   = ctdb_db;
126         dd->key.dsize = key.dsize;
127         dd->key.dptr  = dd->keydata;
128         memcpy(dd->keydata, key.dptr, key.dsize);
129
130         dd->hdr = *hdr;
131
132         hash = ctdb_hash(&key);
133
134         trbt_insert32(tree, hash, dd);
135
136         return 0;
137 }
138
139 static int add_record_to_delete_list(struct vacuum_data *vdata, TDB_DATA key,
140                                      struct ctdb_ltdb_header *hdr)
141 {
142         struct ctdb_context *ctdb = vdata->ctdb;
143         struct ctdb_db_context *ctdb_db = vdata->ctdb_db;
144         uint32_t hash;
145         int ret;
146
147         hash = ctdb_hash(&key);
148
149         if (trbt_lookup32(vdata->delete_list, hash)) {
150                 DEBUG(DEBUG_INFO, (__location__ " Hash collision when vacuuming, skipping this record.\n"));
151                 return 0;
152         }
153
154         ret = insert_delete_record_data_into_tree(ctdb, ctdb_db,
155                                                   vdata->delete_list,
156                                                   hdr, key);
157         if (ret != 0) {
158                 return -1;
159         }
160
161         vdata->delete_count++;
162
163         return 0;
164 }
165
166 /**
167  * Add a record to the list of records to be sent
168  * to their lmaster with VACUUM_FETCH.
169  */
170 static int add_record_to_vacuum_fetch_list(struct vacuum_data *vdata,
171                                            TDB_DATA key)
172 {
173         struct ctdb_context *ctdb = vdata->ctdb;
174         struct ctdb_rec_data *rec;
175         uint32_t lmaster;
176         size_t old_size;
177         struct ctdb_marshall_buffer *vfl;
178
179         lmaster = ctdb_lmaster(ctdb, &key);
180
181         vfl = vdata->vacuum_fetch_list[lmaster];
182
183         rec = ctdb_marshall_record(vfl, ctdb->pnn, key, NULL, tdb_null);
184         if (rec == NULL) {
185                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
186                 vdata->traverse_error = true;
187                 return -1;
188         }
189
190         old_size = talloc_get_size(vfl);
191         vfl = talloc_realloc_size(NULL, vfl, old_size + rec->length);
192         if (vfl == NULL) {
193                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
194                 vdata->traverse_error = true;
195                 return -1;
196         }
197         vdata->vacuum_fetch_list[lmaster] = vfl;
198
199         vfl->count++;
200         memcpy(old_size+(uint8_t *)vfl, rec, rec->length);
201         talloc_free(rec);
202
203         vdata->total++;
204
205         return 0;
206 }
207
208
209 static void ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
210                               struct timeval t, void *private_data);
211
212 static int vacuum_record_parser(TDB_DATA key, TDB_DATA data, void *private_data)
213 {
214         struct ctdb_ltdb_header *header =
215                 (struct ctdb_ltdb_header *)private_data;
216
217         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
218                 return -1;
219         }
220
221         *header = *(struct ctdb_ltdb_header *)data.dptr;
222
223         return 0;
224 }
225
226 /*
227  * traverse function for gathering the records that can be deleted
228  */
229 static int vacuum_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data,
230                            void *private_data)
231 {
232         struct vacuum_data *vdata = talloc_get_type(private_data,
233                                                     struct vacuum_data);
234         struct ctdb_context *ctdb = vdata->ctdb;
235         uint32_t lmaster;
236         struct ctdb_ltdb_header *hdr;
237         int res = 0;
238
239         vdata->full_total++;
240
241         lmaster = ctdb_lmaster(ctdb, &key);
242         if (lmaster >= ctdb->num_nodes) {
243                 vdata->full_error++;
244                 DEBUG(DEBUG_CRIT, (__location__
245                                    " lmaster[%u] >= ctdb->num_nodes[%u] for key"
246                                    " with hash[%u]!\n",
247                                    (unsigned)lmaster,
248                                    (unsigned)ctdb->num_nodes,
249                                    (unsigned)ctdb_hash(&key)));
250                 return -1;
251         }
252
253         if (data.dsize != sizeof(struct ctdb_ltdb_header)) {
254                 /* it is not a deleted record */
255                 vdata->full_skipped++;
256                 return 0;
257         }
258
259         hdr = (struct ctdb_ltdb_header *)data.dptr;
260
261         if (hdr->dmaster != ctdb->pnn) {
262                 vdata->full_skipped++;
263                 return 0;
264         }
265
266         if (lmaster == ctdb->pnn) {
267                 /*
268                  * We are both lmaster and dmaster, and the record is empty.
269                  * So we should be able to delete it.
270                  */
271                 res = add_record_to_delete_list(vdata, key, hdr);
272                 if (res != 0) {
273                         vdata->full_error++;
274                 } else {
275                         vdata->full_added_to_delete_list++;
276                 }
277         } else {
278                 /*
279                  * We are not lmaster.
280                  * Add the record to the blob ready to send to the nodes.
281                  */
282                 res = add_record_to_vacuum_fetch_list(vdata, key);
283                 if (res != 0) {
284                         vdata->full_error++;
285                 } else {
286                         vdata->full_added_to_vacuum_fetch_list++;
287                 }
288         }
289
290         return res;
291 }
292
293 /*
294  * traverse the tree of records to delete and marshall them into
295  * a blob
296  */
297 static int delete_marshall_traverse(void *param, void *data)
298 {
299         struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
300         struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
301         struct ctdb_rec_data *rec;
302         size_t old_size;
303
304         rec = ctdb_marshall_record(dd, recs->records->db_id, dd->key, &dd->hdr, tdb_null);
305         if (rec == NULL) {
306                 DEBUG(DEBUG_ERR, (__location__ " failed to marshall record\n"));
307                 return 0;
308         }
309
310         old_size = talloc_get_size(recs->records);
311         recs->records = talloc_realloc_size(NULL, recs->records, old_size + rec->length);
312         if (recs->records == NULL) {
313                 DEBUG(DEBUG_ERR,(__location__ " Failed to expand\n"));
314                 return 0;
315         }
316         recs->records->count++;
317         memcpy(old_size+(uint8_t *)(recs->records), rec, rec->length);
318         return 0;
319 }
320
321 /**
322  * Variant of delete_marshall_traverse() that bumps the
323  * RSN of each traversed record in the database.
324  *
325  * This is needed to ensure that when rolling out our
326  * empty record copy before remote deletion, we as the
327  * record's dmaster keep a higher RSN than the non-dmaster
328  * nodes. This is needed to prevent old copies from
329  * resurrection in recoveries.
330  */
331 static int delete_marshall_traverse_first(void *param, void *data)
332 {
333         struct delete_record_data *dd = talloc_get_type(data, struct delete_record_data);
334         struct delete_records_list *recs = talloc_get_type(param, struct delete_records_list);
335         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
336         struct ctdb_context *ctdb = ctdb_db->ctdb;
337         struct ctdb_ltdb_header *header;
338         TDB_DATA tdb_data, ctdb_data;
339         uint32_t lmaster;
340         uint32_t hash = ctdb_hash(&(dd->key));
341         int res;
342
343         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
344         if (res != 0) {
345                 DEBUG(DEBUG_ERR,
346                       (__location__ " Error getting chainlock on record with "
347                        "key hash [0x%08x] on database db[%s].\n",
348                        hash, ctdb_db->db_name));
349                 recs->vdata->delete_skipped++;
350                 talloc_free(dd);
351                 return 0;
352         }
353
354         /*
355          * Verify that the record is still empty, its RSN has not
356          * changed and that we are still its lmaster and dmaster.
357          */
358
359         tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
360         if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
361                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
362                                    "on database db[%s] does not exist or is not"
363                                    " a ctdb-record.  skipping.\n",
364                                    hash, ctdb_db->db_name));
365                 goto skip;
366         }
367
368         if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
369                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
370                                    "on database db[%s] has been recycled. "
371                                    "skipping.\n",
372                                    hash, ctdb_db->db_name));
373                 goto skip;
374         }
375
376         header = (struct ctdb_ltdb_header *)tdb_data.dptr;
377
378         if (header->flags & CTDB_REC_RO_FLAGS) {
379                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
380                                    "on database db[%s] has read-only flags. "
381                                    "skipping.\n",
382                                    hash, ctdb_db->db_name));
383                 goto skip;
384         }
385
386         if (header->dmaster != ctdb->pnn) {
387                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
388                                    "on database db[%s] has been migrated away. "
389                                    "skipping.\n",
390                                    hash, ctdb_db->db_name));
391                 goto skip;
392         }
393
394         if (header->rsn != dd->hdr.rsn) {
395                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
396                                    "on database db[%s] seems to have been "
397                                    "migrated away and back again (with empty "
398                                    "data). skipping.\n",
399                                    hash, ctdb_db->db_name));
400                 goto skip;
401         }
402
403         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
404
405         if (lmaster != ctdb->pnn) {
406                 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
407                                    "delete list (key hash [0x%08x], db[%s]). "
408                                    "Strange! skipping.\n",
409                                    hash, ctdb_db->db_name));
410                 goto skip;
411         }
412
413         /*
414          * Increment the record's RSN to ensure the dmaster (i.e. the current
415          * node) has the highest RSN of the record in the cluster.
416          * This is to prevent old record copies from resurrecting in recoveries
417          * if something should fail during the deletion process.
418          * Note that ctdb_ltdb_store_server() increments the RSN if called
419          * on the record's dmaster.
420          */
421
422         ctdb_data.dptr = tdb_data.dptr + sizeof(struct ctdb_ltdb_header);
423         ctdb_data.dsize = tdb_data.dsize - sizeof(struct ctdb_ltdb_header);
424
425         res = ctdb_ltdb_store(ctdb_db, dd->key, header, ctdb_data);
426         if (res != 0) {
427                 DEBUG(DEBUG_ERR, (__location__ ": Failed to store record with "
428                                   "key hash [0x%08x] on database db[%s].\n",
429                                   hash, ctdb_db->db_name));
430                 goto skip;
431         }
432
433         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
434
435         goto done;
436
437 skip:
438         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
439
440         recs->vdata->delete_skipped++;
441         talloc_free(dd);
442         dd = NULL;
443
444 done:
445         if (tdb_data.dptr != NULL) {
446                 free(tdb_data.dptr);
447         }
448
449         if (dd == NULL) {
450                 return 0;
451         }
452
453         return delete_marshall_traverse(param, data);
454 }
455
456 /**
457  * traverse function for the traversal of the delete_queue,
458  * the fast-path vacuuming list.
459  *
460  *  - If the record has been migrated off the node
461  *    or has been revived (filled with data) on the node,
462  *    then skip the record.
463  *
464  *  - If the current node is the record's lmaster and it is
465  *    a record that has never been migrated with data, then
466  *    delete the record from the local tdb.
467  *
468  *  - If the current node is the record's lmaster and it has
469  *    been migrated with data, then schedule it for the normal
470  *    vacuuming procedure (i.e. add it to the delete_list).
471  *
472  *  - If the current node is NOT the record's lmaster then
473  *    add it to the list of records that are to be sent to
474  *    the lmaster with the VACUUM_FETCH message.
475  */
476 static int delete_queue_traverse(void *param, void *data)
477 {
478         struct delete_record_data *dd =
479                 talloc_get_type(data, struct delete_record_data);
480         struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
481         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
482         struct ctdb_context *ctdb = ctdb_db->ctdb; /* or dd->ctdb ??? */
483         int res;
484         struct ctdb_ltdb_header header;
485         uint32_t lmaster;
486         uint32_t hash = ctdb_hash(&(dd->key));
487
488         vdata->fast_total++;
489
490         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
491         if (res != 0) {
492                 DEBUG(DEBUG_ERR,
493                       (__location__ " Error getting chainlock on record with "
494                        "key hash [0x%08x] on database db[%s].\n",
495                        hash, ctdb_db->db_name));
496                 vdata->fast_error++;
497                 return 0;
498         }
499
500         res = tdb_parse_record(ctdb_db->ltdb->tdb, dd->key,
501                                vacuum_record_parser, &header);
502         if (res != 0) {
503                 goto skipped;
504         }
505
506         if (header.dmaster != ctdb->pnn) {
507                 /* The record has been migrated off the node. Skip. */
508                 goto skipped;
509         }
510
511         if (header.rsn != dd->hdr.rsn) {
512                 /*
513                  * The record has been migrated off the node and back again.
514                  * But not requeued for deletion. Skip it.
515                  */
516                 goto skipped;
517         }
518
519         /*
520          * We are dmaster, and the record has no data, and it has
521          * not been migrated after it has been queued for deletion.
522          *
523          * At this stage, the record could still have been revived locally
524          * and last been written with empty data. This can only be
525          * fixed with the addition of an active or delete flag. (TODO)
526          */
527
528         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
529
530         if (lmaster != ctdb->pnn) {
531                 res = add_record_to_vacuum_fetch_list(vdata, dd->key);
532
533                 if (res != 0) {
534                         DEBUG(DEBUG_ERR,
535                               (__location__ " Error adding record to list "
536                                "of records to send to lmaster.\n"));
537                         vdata->fast_error++;
538                 } else {
539                         vdata->fast_added_to_vacuum_fetch_list++;
540                 }
541                 goto done;
542         }
543
544         /* use header->flags or dd->hdr.flags ?? */
545         if (dd->hdr.flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
546                 res = add_record_to_delete_list(vdata, dd->key, &dd->hdr);
547
548                 if (res != 0) {
549                         DEBUG(DEBUG_ERR,
550                               (__location__ " Error adding record to list "
551                                "of records for deletion on lmaster.\n"));
552                         vdata->fast_error++;
553                 } else {
554                         vdata->fast_added_to_delete_list++;
555                 }
556         } else {
557                 res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
558
559                 if (res != 0) {
560                         DEBUG(DEBUG_ERR,
561                               (__location__ " Error deleting record with key "
562                                "hash [0x%08x] from local data base db[%s].\n",
563                                hash, ctdb_db->db_name));
564                         vdata->fast_error++;
565                         goto done;
566                 }
567
568                 DEBUG(DEBUG_DEBUG,
569                       (__location__ " Deleted record with key hash "
570                        "[0x%08x] from local data base db[%s].\n",
571                        hash, ctdb_db->db_name));
572                 vdata->fast_deleted++;
573         }
574
575         goto done;
576
577 skipped:
578         vdata->fast_skipped++;
579
580 done:
581         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
582
583         return 0;
584 }
585
586 /**
587  * Delete the records that we are lmaster and dmaster for and
588  * that could be deleted on all other nodes via the TRY_DELETE_RECORDS
589  * control.
590  */
591 static int delete_record_traverse(void *param, void *data)
592 {
593         struct delete_record_data *dd =
594                 talloc_get_type(data, struct delete_record_data);
595         struct vacuum_data *vdata = talloc_get_type(param, struct vacuum_data);
596         struct ctdb_db_context *ctdb_db = dd->ctdb_db;
597         struct ctdb_context *ctdb = ctdb_db->ctdb;
598         int res;
599         struct ctdb_ltdb_header *header;
600         TDB_DATA tdb_data;
601         uint32_t lmaster;
602         uint32_t hash = ctdb_hash(&(dd->key));
603
604         res = tdb_chainlock(ctdb_db->ltdb->tdb, dd->key);
605         if (res != 0) {
606                 DEBUG(DEBUG_ERR,
607                       (__location__ " Error getting chainlock on record with "
608                        "key hash [0x%08x] on database db[%s].\n",
609                        hash, ctdb_db->db_name));
610                 vdata->delete_local_error++;
611                 vdata->delete_left--;
612                 talloc_free(dd);
613                 return 0;
614         }
615
616         /*
617          * Verify that the record is still empty, its RSN has not
618          * changed and that we are still its lmaster and dmaster.
619          */
620
621         tdb_data = tdb_fetch(ctdb_db->ltdb->tdb, dd->key);
622         if (tdb_data.dsize < sizeof(struct ctdb_ltdb_header)) {
623                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
624                                    "on database db[%s] does not exist or is not"
625                                    " a ctdb-record.  skipping.\n",
626                                    hash, ctdb_db->db_name));
627                 goto skip;
628         }
629
630         if (tdb_data.dsize > sizeof(struct ctdb_ltdb_header)) {
631                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
632                                    "on database db[%s] has been recycled. "
633                                    "skipping.\n",
634                                    hash, ctdb_db->db_name));
635                 goto skip;
636         }
637
638         header = (struct ctdb_ltdb_header *)tdb_data.dptr;
639
640         if (header->flags & CTDB_REC_RO_FLAGS) {
641                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
642                                    "on database db[%s] has read-only flags. "
643                                    "skipping.\n",
644                                    hash, ctdb_db->db_name));
645                 goto skip;
646         }
647
648         if (header->dmaster != ctdb->pnn) {
649                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
650                                    "on database db[%s] has been migrated away. "
651                                    "skipping.\n",
652                                    hash, ctdb_db->db_name));
653                 goto skip;
654         }
655
656         if (header->rsn != dd->hdr.rsn + 1) {
657                 /*
658                  * The record has been migrated off the node and back again.
659                  * But not requeued for deletion. Skip it.
660                  * (Note that the first marshall traverse has bumped the RSN
661                  *  on disk.)
662                  */
663                 DEBUG(DEBUG_INFO, (__location__ ": record with hash [0x%08x] "
664                                    "on database db[%s] seems to have been "
665                                    "migrated away and back again (with empty "
666                                    "data). skipping.\n",
667                                    hash, ctdb_db->db_name));
668                 goto skip;
669         }
670
671         lmaster = ctdb_lmaster(ctdb_db->ctdb, &dd->key);
672
673         if (lmaster != ctdb->pnn) {
674                 DEBUG(DEBUG_INFO, (__location__ ": not lmaster for record in "
675                                    "delete list (key hash [0x%08x], db[%s]). "
676                                    "Strange! skipping.\n",
677                                    hash, ctdb_db->db_name));
678                 goto skip;
679         }
680
681         res = tdb_delete(ctdb_db->ltdb->tdb, dd->key);
682
683         if (res != 0) {
684                 DEBUG(DEBUG_ERR,
685                       (__location__ " Error deleting record with key hash "
686                        "[0x%08x] from local data base db[%s].\n",
687                        hash, ctdb_db->db_name));
688                 vdata->delete_local_error++;
689                 goto done;
690         }
691
692         DEBUG(DEBUG_DEBUG,
693               (__location__ " Deleted record with key hash [0x%08x] from "
694                "local data base db[%s].\n", hash, ctdb_db->db_name));
695
696         vdata->delete_deleted++;
697         goto done;
698
699 skip:
700         vdata->delete_skipped++;
701
702 done:
703         free(tdb_data.dptr);
704
705         tdb_chainunlock(ctdb_db->ltdb->tdb, dd->key);
706
707         talloc_free(dd);
708         vdata->delete_left--;
709
710         return 0;
711 }
712
713 /**
714  * Fast vacuuming run:
715  * Traverse the delete_queue.
716  * This fills the same lists as the database traverse.
717  */
718 static void ctdb_vacuum_db_fast(struct ctdb_db_context *ctdb_db,
719                                 struct vacuum_data *vdata)
720 {
721         uint32_t sum;
722
723         trbt_traversearray32(ctdb_db->delete_queue, 1, delete_queue_traverse, vdata);
724
725         sum = vdata->fast_deleted
726             + vdata->fast_skipped
727             + vdata->fast_error
728             + vdata->fast_added_to_delete_list
729             + vdata->fast_added_to_vacuum_fetch_list;
730
731         if (vdata->fast_total != sum) {
732                 DEBUG(DEBUG_ERR, (__location__ " Inconsistency in fast vacuum "
733                       "counts for db[%s]: total[%u] != sum[%u]\n",
734                       ctdb_db->db_name, (unsigned)vdata->fast_total,
735                       (unsigned)sum));
736         }
737
738         if (vdata->fast_total > 0) {
739                 DEBUG(DEBUG_INFO,
740                       (__location__
741                        " fast vacuuming delete_queue traverse statistics: "
742                        "db[%s] "
743                        "total[%u] "
744                        "del[%u] "
745                        "skp[%u] "
746                        "err[%u] "
747                        "adl[%u] "
748                        "avf[%u]\n",
749                        ctdb_db->db_name,
750                        (unsigned)vdata->fast_total,
751                        (unsigned)vdata->fast_deleted,
752                        (unsigned)vdata->fast_skipped,
753                        (unsigned)vdata->fast_error,
754                        (unsigned)vdata->fast_added_to_delete_list,
755                        (unsigned)vdata->fast_added_to_vacuum_fetch_list));
756         }
757
758         return;
759 }
760
761 /**
762  * Full vacuum run:
763  * read-only traverse of the database, looking for records that
764  * might be able to be vacuumed.
765  *
766  * This is not done each time but only every tunable
767  * VacuumFastPathCount times.
768  */
769 static int ctdb_vacuum_db_full(struct ctdb_db_context *ctdb_db,
770                                struct vacuum_data *vdata)
771 {
772         int ret;
773
774         ret = tdb_traverse_read(ctdb_db->ltdb->tdb, vacuum_traverse, vdata);
775         if (ret == -1 || vdata->traverse_error) {
776                 DEBUG(DEBUG_ERR, (__location__ " Traverse error in vacuuming "
777                                   "'%s'\n", ctdb_db->db_name));
778                 return -1;
779         }
780
781         if (vdata->full_total > 0) {
782                 DEBUG(DEBUG_INFO,
783                       (__location__
784                        " full vacuuming db traverse statistics: "
785                        "db[%s] "
786                        "total[%u] "
787                        "skp[%u] "
788                        "err[%u] "
789                        "adl[%u] "
790                        "avf[%u]\n",
791                        ctdb_db->db_name,
792                        (unsigned)vdata->full_total,
793                        (unsigned)vdata->full_skipped,
794                        (unsigned)vdata->full_error,
795                        (unsigned)vdata->full_added_to_delete_list,
796                        (unsigned)vdata->full_added_to_vacuum_fetch_list));
797         }
798
799         return 0;
800 }
801
802 /**
803  * Process the vacuum fetch lists:
804  * For records for which we are not the lmaster, tell the lmaster to
805  * fetch the record.
806  */
807 static int ctdb_process_vacuum_fetch_lists(struct ctdb_db_context *ctdb_db,
808                                            struct vacuum_data *vdata)
809 {
810         int i;
811         struct ctdb_context *ctdb = ctdb_db->ctdb;
812
813         for (i = 0; i < ctdb->num_nodes; i++) {
814                 TDB_DATA data;
815                 struct ctdb_marshall_buffer *vfl = vdata->vacuum_fetch_list[i];
816
817                 if (ctdb->nodes[i]->pnn == ctdb->pnn) {
818                         continue;
819                 }
820
821                 if (vfl->count == 0) {
822                         continue;
823                 }
824
825                 DEBUG(DEBUG_INFO, ("Found %u records for lmaster %u in '%s'\n",
826                                    vfl->count, ctdb->nodes[i]->pnn,
827                                    ctdb_db->db_name));
828
829                 data.dsize = talloc_get_size(vfl);
830                 data.dptr  = (void *)vfl;
831                 if (ctdb_client_send_message(ctdb, ctdb->nodes[i]->pnn,
832                                              CTDB_SRVID_VACUUM_FETCH,
833                                              data) != 0)
834                 {
835                         DEBUG(DEBUG_ERR, (__location__ " Failed to send vacuum "
836                                           "fetch message to %u\n",
837                                           ctdb->nodes[i]->pnn));
838                         return -1;
839                 }
840         }
841
842         return 0;
843 }
844
845 /**
846  * Process the delete list:
847  *
848  * This is the last step of vacuuming that consistently deletes
849  * those records that have been migrated with data and can hence
850  * not be deleted when leaving a node.
851  *
852  * In this step, the lmaster does the final deletion of those empty
853  * records that it is also dmaster for. It has ususally received
854  * at least some of these records previously from the former dmasters
855  * with the vacuum fetch message.
856  *
857  * This last step is implemented as a 3-phase process to protect from
858  * races leading to data corruption:
859  *
860  *  1) Send the lmaster's copy to all other active nodes with the
861  *     RECEIVE_RECORDS control: The remote nodes store the lmaster's copy.
862  *  2) Send the records that could successfully be stored remotely
863  *     in step #1 to all active nodes with the TRY_DELETE_RECORDS
864  *     control. The remote notes delete their local copy.
865  *  3) The lmaster locally deletes its copies of all records that
866  *     could successfully be deleted remotely in step #2.
867  */
868 static int ctdb_process_delete_list(struct ctdb_db_context *ctdb_db,
869                                     struct vacuum_data *vdata)
870 {
871         int ret, i;
872         struct ctdb_context *ctdb = ctdb_db->ctdb;
873         struct delete_records_list *recs;
874         TDB_DATA indata;
875         struct ctdb_node_map *nodemap;
876         uint32_t *active_nodes;
877         int num_active_nodes;
878         TALLOC_CTX *tmp_ctx;
879
880         if (vdata->delete_count == 0) {
881                 return 0;
882         }
883
884         tmp_ctx = talloc_new(vdata);
885         if (tmp_ctx == NULL) {
886                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
887                 return 0;
888         }
889
890         vdata->delete_left = vdata->delete_count;
891
892         /*
893          * get the list of currently active nodes
894          */
895
896         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
897                                    CTDB_CURRENT_NODE,
898                                    tmp_ctx,
899                                    &nodemap);
900         if (ret != 0) {
901                 DEBUG(DEBUG_ERR,(__location__ " unable to get node map\n"));
902                 ret = -1;
903                 goto done;
904         }
905
906         active_nodes = list_of_active_nodes(ctdb, nodemap,
907                                             nodemap, /* talloc context */
908                                             false /* include self */);
909         /* yuck! ;-) */
910         num_active_nodes = talloc_get_size(active_nodes)/sizeof(*active_nodes);
911
912         /*
913          * Now delete the records all active nodes in a three-phase process:
914          * 1) send all active remote nodes the current empty copy with this
915          *    node as DMASTER
916          * 2) if all nodes could store the new copy,
917          *    tell all the active remote nodes to delete all their copy
918          * 3) if all remote nodes deleted their record copy, delete it locally
919          */
920
921         /*
922          * Step 1:
923          * Send currently empty record copy to all active nodes for storing.
924          */
925
926         recs = talloc_zero(tmp_ctx, struct delete_records_list);
927         if (recs == NULL) {
928                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
929                 ret = -1;
930                 goto done;
931         }
932         recs->records = (struct ctdb_marshall_buffer *)
933                 talloc_zero_size(recs,
934                                  offsetof(struct ctdb_marshall_buffer, data));
935         if (recs->records == NULL) {
936                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
937                 ret = -1;
938                 goto done;
939         }
940         recs->records->db_id = ctdb_db->db_id;
941         recs->vdata = vdata;
942
943         /*
944          * traverse the tree of all records we want to delete and
945          * create a blob we can send to the other nodes.
946          *
947          * We call delete_marshall_traverse_first() to bump the
948          * records' RSNs in the database, to ensure we (as dmaster)
949          * keep the highest RSN of the records in the cluster.
950          */
951         trbt_traversearray32(vdata->delete_list, 1,
952                              delete_marshall_traverse_first, recs);
953
954         indata.dsize = talloc_get_size(recs->records);
955         indata.dptr  = (void *)recs->records;
956
957         for (i = 0; i < num_active_nodes; i++) {
958                 struct ctdb_marshall_buffer *records;
959                 struct ctdb_rec_data *rec;
960                 int32_t res;
961                 TDB_DATA outdata;
962
963                 ret = ctdb_control(ctdb, active_nodes[i], 0,
964                                 CTDB_CONTROL_RECEIVE_RECORDS, 0,
965                                 indata, recs, &outdata, &res,
966                                 NULL, NULL);
967                 if (ret != 0 || res != 0) {
968                         DEBUG(DEBUG_ERR, ("Error storing record copies on "
969                                           "node %u: ret[%d] res[%d]\n",
970                                           active_nodes[i], ret, res));
971                         ret = -1;
972                         goto done;
973                 }
974
975                 /*
976                  * outdata contains the list of records coming back
977                  * from the node: These are the records that the
978                  * remote node could not store. We remove these from
979                  * the list to process further.
980                  */
981                 records = (struct ctdb_marshall_buffer *)outdata.dptr;
982                 rec = (struct ctdb_rec_data *)&records->data[0];
983                 while (records->count-- > 1) {
984                         TDB_DATA reckey, recdata;
985                         struct ctdb_ltdb_header *rechdr;
986                         struct delete_record_data *dd;
987
988                         reckey.dptr = &rec->data[0];
989                         reckey.dsize = rec->keylen;
990                         recdata.dptr = &rec->data[reckey.dsize];
991                         recdata.dsize = rec->datalen;
992
993                         if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
994                                 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
995                                 ret = -1;
996                                 goto done;
997                         }
998                         rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
999                         recdata.dptr += sizeof(*rechdr);
1000                         recdata.dsize -= sizeof(*rechdr);
1001
1002                         dd = (struct delete_record_data *)trbt_lookup32(
1003                                         vdata->delete_list,
1004                                         ctdb_hash(&reckey));
1005                         if (dd != NULL) {
1006                                 /*
1007                                  * The other node could not store the record
1008                                  * copy and it is the first node that failed.
1009                                  * So we should remove it from the tree and
1010                                  * update statistics.
1011                                  */
1012                                 talloc_free(dd);
1013                                 vdata->delete_remote_error++;
1014                                 vdata->delete_left--;
1015                         }
1016
1017                         rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1018                 }
1019         }
1020
1021         if (vdata->delete_left == 0) {
1022                 goto success;
1023         }
1024
1025         /*
1026          * Step 2:
1027          * Send the remaining records to all active nodes for deletion.
1028          *
1029          * The lmaster's (i.e. our) copies of these records have been stored
1030          * successfully on the other nodes.
1031          */
1032
1033         /*
1034          * Create a marshall blob from the remaining list of records to delete.
1035          */
1036
1037         talloc_free(recs->records);
1038
1039         recs->records = (struct ctdb_marshall_buffer *)
1040                 talloc_zero_size(recs,
1041                                  offsetof(struct ctdb_marshall_buffer, data));
1042         if (recs->records == NULL) {
1043                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1044                 ret = -1;
1045                 goto done;
1046         }
1047         recs->records->db_id = ctdb_db->db_id;
1048
1049         trbt_traversearray32(vdata->delete_list, 1,
1050                              delete_marshall_traverse, recs);
1051
1052         indata.dsize = talloc_get_size(recs->records);
1053         indata.dptr  = (void *)recs->records;
1054
1055         for (i = 0; i < num_active_nodes; i++) {
1056                 struct ctdb_marshall_buffer *records;
1057                 struct ctdb_rec_data *rec;
1058                 int32_t res;
1059                 TDB_DATA outdata;
1060
1061                 ret = ctdb_control(ctdb, active_nodes[i], 0,
1062                                 CTDB_CONTROL_TRY_DELETE_RECORDS, 0,
1063                                 indata, recs, &outdata, &res,
1064                                 NULL, NULL);
1065                 if (ret != 0 || res != 0) {
1066                         DEBUG(DEBUG_ERR, ("Failed to delete records on "
1067                                           "node %u: ret[%d] res[%d]\n",
1068                                           active_nodes[i], ret, res));
1069                         ret = -1;
1070                         goto done;
1071                 }
1072
1073                 /*
1074                  * outdata contains the list of records coming back
1075                  * from the node: These are the records that the
1076                  * remote node could not delete. We remove these from
1077                  * the list to delete locally.
1078                  */
1079                 records = (struct ctdb_marshall_buffer *)outdata.dptr;
1080                 rec = (struct ctdb_rec_data *)&records->data[0];
1081                 while (records->count-- > 1) {
1082                         TDB_DATA reckey, recdata;
1083                         struct ctdb_ltdb_header *rechdr;
1084                         struct delete_record_data *dd;
1085
1086                         reckey.dptr = &rec->data[0];
1087                         reckey.dsize = rec->keylen;
1088                         recdata.dptr = &rec->data[reckey.dsize];
1089                         recdata.dsize = rec->datalen;
1090
1091                         if (recdata.dsize < sizeof(struct ctdb_ltdb_header)) {
1092                                 DEBUG(DEBUG_CRIT,(__location__ " bad ltdb record\n"));
1093                                 ret = -1;
1094                                 goto done;
1095                         }
1096                         rechdr = (struct ctdb_ltdb_header *)recdata.dptr;
1097                         recdata.dptr += sizeof(*rechdr);
1098                         recdata.dsize -= sizeof(*rechdr);
1099
1100                         dd = (struct delete_record_data *)trbt_lookup32(
1101                                         vdata->delete_list,
1102                                         ctdb_hash(&reckey));
1103                         if (dd != NULL) {
1104                                 /*
1105                                  * The other node could not delete the
1106                                  * record and it is the first node that
1107                                  * failed. So we should remove it from
1108                                  * the tree and update statistics.
1109                                  */
1110                                 talloc_free(dd);
1111                                 vdata->delete_remote_error++;
1112                                 vdata->delete_left--;
1113                         }
1114
1115                         rec = (struct ctdb_rec_data *)(rec->length + (uint8_t *)rec);
1116                 }
1117         }
1118
1119         if (vdata->delete_left == 0) {
1120                 goto success;
1121         }
1122
1123         /*
1124          * Step 3:
1125          * Delete the remaining records locally.
1126          *
1127          * These records have successfully been deleted on all
1128          * active remote nodes.
1129          */
1130
1131         trbt_traversearray32(vdata->delete_list, 1,
1132                              delete_record_traverse, vdata);
1133
1134 success:
1135
1136         if (vdata->delete_count > 0) {
1137                 DEBUG(DEBUG_INFO,
1138                       (__location__
1139                        " vacuum delete list statistics: "
1140                        "db[%s] "
1141                        "total[%u] "
1142                        "del[%u] "
1143                        "skip[%u] "
1144                        "rem.err[%u] "
1145                        "loc.err[%u] "
1146                        "left[%u]\n",
1147                        ctdb_db->db_name,
1148                        (unsigned)vdata->delete_count,
1149                        (unsigned)vdata->delete_deleted,
1150                        (unsigned)vdata->delete_skipped,
1151                        (unsigned)vdata->delete_remote_error,
1152                        (unsigned)vdata->delete_local_error,
1153                        (unsigned)vdata->delete_left));
1154         }
1155
1156         ret = 0;
1157
1158 done:
1159         talloc_free(tmp_ctx);
1160
1161         return ret;
1162 }
1163
1164 /**
1165  * initialize the vacuum_data
1166  */
1167 static int ctdb_vacuum_init_vacuum_data(struct ctdb_db_context *ctdb_db,
1168                                         struct vacuum_data *vdata)
1169 {
1170         int i;
1171         struct ctdb_context *ctdb = ctdb_db->ctdb;
1172
1173         vdata->fast_added_to_delete_list = 0;
1174         vdata->fast_added_to_vacuum_fetch_list = 0;
1175         vdata->fast_deleted = 0;
1176         vdata->fast_skipped = 0;
1177         vdata->fast_error = 0;
1178         vdata->fast_total = 0;
1179         vdata->full_added_to_delete_list = 0;
1180         vdata->full_added_to_vacuum_fetch_list = 0;
1181         vdata->full_skipped = 0;
1182         vdata->full_error = 0;
1183         vdata->full_total = 0;
1184         vdata->delete_count = 0;
1185         vdata->delete_left = 0;
1186         vdata->delete_remote_error = 0;
1187         vdata->delete_local_error = 0;
1188         vdata->delete_skipped = 0;
1189         vdata->delete_deleted = 0;
1190
1191         /* the list needs to be of length num_nodes */
1192         vdata->vacuum_fetch_list = talloc_zero_array(vdata,
1193                                                 struct ctdb_marshall_buffer *,
1194                                                 ctdb->num_nodes);
1195         if (vdata->vacuum_fetch_list == NULL) {
1196                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1197                 return -1;
1198         }
1199         for (i = 0; i < ctdb->num_nodes; i++) {
1200                 vdata->vacuum_fetch_list[i] = (struct ctdb_marshall_buffer *)
1201                         talloc_zero_size(vdata->vacuum_fetch_list,
1202                                          offsetof(struct ctdb_marshall_buffer, data));
1203                 if (vdata->vacuum_fetch_list[i] == NULL) {
1204                         DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1205                         return -1;
1206                 }
1207                 vdata->vacuum_fetch_list[i]->db_id = ctdb_db->db_id;
1208         }
1209
1210         return 0;
1211 }
1212
1213 /**
1214  * Vacuum a DB:
1215  *  - Always do the fast vacuuming run, which traverses
1216  *    the in-memory delete queue: these records have been
1217  *    scheduled for deletion.
1218  *  - Only if explicitly requested, the database is traversed
1219  *    in order to use the traditional heuristics on empty records
1220  *    to trigger deletion.
1221  *    This is done only every VacuumFastPathCount'th vacuuming run.
1222  *
1223  * The traverse runs fill two lists:
1224  *
1225  * - The delete_list:
1226  *   This is the list of empty records the current
1227  *   node is lmaster and dmaster for. These records are later
1228  *   deleted first on other nodes and then locally.
1229  *
1230  *   The fast vacuuming run has a short cut for those records
1231  *   that have never been migrated with data: these records
1232  *   are immediately deleted locally, since they have left
1233  *   no trace on other nodes.
1234  *
1235  * - The vacuum_fetch lists
1236  *   (one for each other lmaster node):
1237  *   The records in this list are sent for deletion to
1238  *   their lmaster in a bulk VACUUM_FETCH message.
1239  *
1240  *   The lmaster then migrates all these records to itelf
1241  *   so that they can be vacuumed there.
1242  *
1243  * This executes in the child context.
1244  */
1245 static int ctdb_vacuum_db(struct ctdb_db_context *ctdb_db,
1246                           struct vacuum_data *vdata,
1247                           bool full_vacuum_run)
1248 {
1249         struct ctdb_context *ctdb = ctdb_db->ctdb;
1250         int ret, pnn;
1251
1252         DEBUG(DEBUG_INFO, (__location__ " Entering %s vacuum run for db "
1253                            "%s db_id[0x%08x]\n",
1254                            full_vacuum_run ? "full" : "fast",
1255                            ctdb_db->db_name, ctdb_db->db_id));
1256
1257         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &ctdb->vnn_map);
1258         if (ret != 0) {
1259                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from local node\n"));
1260                 return ret;
1261         }
1262
1263         pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1264         if (pnn == -1) {
1265                 DEBUG(DEBUG_ERR, ("Unable to get pnn from local node\n"));
1266                 return -1;
1267         }
1268
1269         ctdb->pnn = pnn;
1270
1271         ret = ctdb_vacuum_init_vacuum_data(ctdb_db, vdata);
1272         if (ret != 0) {
1273                 return ret;
1274         }
1275
1276         ctdb_vacuum_db_fast(ctdb_db, vdata);
1277
1278         if (full_vacuum_run) {
1279                 ret = ctdb_vacuum_db_full(ctdb_db, vdata);
1280                 if (ret != 0) {
1281                         return ret;
1282                 }
1283         }
1284
1285         ret = ctdb_process_vacuum_fetch_lists(ctdb_db, vdata);
1286         if (ret != 0) {
1287                 return ret;
1288         }
1289
1290         ret = ctdb_process_delete_list(ctdb_db, vdata);
1291         if (ret != 0) {
1292                 return ret;
1293         }
1294
1295         /* this ensures we run our event queue */
1296         ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE);
1297
1298         return 0;
1299 }
1300
1301
1302 /*
1303  * traverse function for repacking
1304  */
1305 static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
1306 {
1307         struct vacuum_data *vdata = (struct vacuum_data *)private;
1308
1309         if (vdata->vacuum) {
1310                 uint32_t hash = ctdb_hash(&key);
1311                 struct delete_record_data *kd;
1312                 /*
1313                  * check if we can ignore this record because it's in the delete_list
1314                  */
1315                 kd = (struct delete_record_data *)trbt_lookup32(vdata->delete_list, hash);
1316                 /*
1317                  * there might be hash collisions so we have to compare the keys here to be sure
1318                  */
1319                 if (kd && kd->key.dsize == key.dsize && memcmp(kd->key.dptr, key.dptr, key.dsize) == 0) {
1320                         struct ctdb_ltdb_header *hdr = (struct ctdb_ltdb_header *)data.dptr;
1321                         /*
1322                          * we have to check if the record hasn't changed in the meantime in order to
1323                          * savely remove it from the database
1324                          */
1325                         if (data.dsize == sizeof(struct ctdb_ltdb_header) &&
1326                                 hdr->dmaster == kd->ctdb->pnn &&
1327                                 ctdb_lmaster(kd->ctdb, &(kd->key)) == kd->ctdb->pnn &&
1328                                 kd->hdr.rsn == hdr->rsn) {
1329                                 vdata->vacuumed++;
1330                                 return 0;
1331                         }
1332                 }
1333         }
1334         if (tdb_store(vdata->dest_db, key, data, TDB_INSERT) != 0) {
1335                 vdata->traverse_error = true;
1336                 return -1;
1337         }
1338         vdata->copied++;
1339         return 0;
1340 }
1341
1342 /*
1343  * repack a tdb
1344  */
1345 static int ctdb_repack_tdb(struct tdb_context *tdb, TALLOC_CTX *mem_ctx, struct vacuum_data *vdata)
1346 {
1347         struct tdb_context *tmp_db;
1348
1349         if (tdb_transaction_start(tdb) != 0) {
1350                 DEBUG(DEBUG_ERR,(__location__ " Failed to start transaction\n"));
1351                 return -1;
1352         }
1353
1354         tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb),
1355                           TDB_INTERNAL|TDB_DISALLOW_NESTING,
1356                           O_RDWR|O_CREAT, 0);
1357         if (tmp_db == NULL) {
1358                 DEBUG(DEBUG_ERR,(__location__ " Failed to create tmp_db\n"));
1359                 tdb_transaction_cancel(tdb);
1360                 return -1;
1361         }
1362
1363         vdata->traverse_error = false;
1364         vdata->dest_db = tmp_db;
1365         vdata->vacuum = true;
1366         vdata->vacuumed = 0;
1367         vdata->copied = 0;
1368
1369         /*
1370          * repack and vacuum on-the-fly by not writing the records that are
1371          * no longer needed
1372          */
1373         if (tdb_traverse_read(tdb, repack_traverse, vdata) == -1) {
1374                 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying out\n"));
1375                 tdb_transaction_cancel(tdb);
1376                 tdb_close(tmp_db);
1377                 return -1;              
1378         }
1379
1380         DEBUG(DEBUG_INFO,(__location__ " %u records vacuumed\n", vdata->vacuumed));
1381         
1382         if (vdata->traverse_error) {
1383                 DEBUG(DEBUG_ERR,(__location__ " Error during traversal\n"));
1384                 tdb_transaction_cancel(tdb);
1385                 tdb_close(tmp_db);
1386                 return -1;
1387         }
1388
1389         if (tdb_wipe_all(tdb) != 0) {
1390                 DEBUG(DEBUG_ERR,(__location__ " Failed to wipe database\n"));
1391                 tdb_transaction_cancel(tdb);
1392                 tdb_close(tmp_db);
1393                 return -1;
1394         }
1395
1396         vdata->traverse_error = false;
1397         vdata->dest_db = tdb;
1398         vdata->vacuum = false;
1399         vdata->copied = 0;
1400
1401         if (tdb_traverse_read(tmp_db, repack_traverse, vdata) == -1) {
1402                 DEBUG(DEBUG_ERR,(__location__ " Failed to traverse copying back\n"));
1403                 tdb_transaction_cancel(tdb);
1404                 tdb_close(tmp_db);
1405                 return -1;              
1406         }
1407
1408         if (vdata->traverse_error) {
1409                 DEBUG(DEBUG_ERR,(__location__ " Error during second traversal\n"));
1410                 tdb_transaction_cancel(tdb);
1411                 tdb_close(tmp_db);
1412                 return -1;
1413         }
1414
1415         tdb_close(tmp_db);
1416
1417
1418         if (tdb_transaction_commit(tdb) != 0) {
1419                 DEBUG(DEBUG_ERR,(__location__ " Failed to commit\n"));
1420                 return -1;
1421         }
1422         DEBUG(DEBUG_INFO,(__location__ " %u records copied\n", vdata->copied));
1423
1424         return 0;
1425 }
1426
1427 /*
1428  * repack and vaccum a db
1429  * called from the child context
1430  */
1431 static int ctdb_vacuum_and_repack_db(struct ctdb_db_context *ctdb_db,
1432                                      TALLOC_CTX *mem_ctx,
1433                                      bool full_vacuum_run)
1434 {
1435         uint32_t repack_limit = ctdb_db->ctdb->tunable.repack_limit;
1436         uint32_t vacuum_limit = ctdb_db->ctdb->tunable.vacuum_limit;
1437         const char *name = ctdb_db->db_name;
1438         int freelist_size = 0;
1439         struct vacuum_data *vdata;
1440
1441         vdata = talloc_zero(mem_ctx, struct vacuum_data);
1442         if (vdata == NULL) {
1443                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1444                 return -1;
1445         }
1446
1447         vdata->ctdb = ctdb_db->ctdb;
1448         vdata->vacuum_limit = vacuum_limit;
1449         vdata->repack_limit = repack_limit;
1450         vdata->delete_list = trbt_create(vdata, 0);
1451         vdata->ctdb_db = ctdb_db;
1452         if (vdata->delete_list == NULL) {
1453                 DEBUG(DEBUG_ERR,(__location__ " Out of memory\n"));
1454                 talloc_free(vdata);
1455                 return -1;
1456         }
1457
1458         vdata->start = timeval_current();
1459  
1460         /*
1461          * gather all records that can be deleted in vdata
1462          */
1463         if (ctdb_vacuum_db(ctdb_db, vdata, full_vacuum_run) != 0) {
1464                 DEBUG(DEBUG_ERR,(__location__ " Failed to vacuum '%s'\n", name));
1465         }
1466
1467         if (repack_limit != 0) {
1468                 freelist_size = tdb_freelist_size(ctdb_db->ltdb->tdb);
1469                 if (freelist_size == -1) {
1470                         DEBUG(DEBUG_ERR,(__location__ " Failed to get freelist size for '%s'\n", name));
1471                         talloc_free(vdata);
1472                         return -1;
1473                 }
1474         }
1475
1476         /*
1477          * decide if a repack is necessary
1478          */
1479         if ((repack_limit == 0 || (uint32_t)freelist_size < repack_limit) &&
1480             vdata->delete_left < vacuum_limit)
1481         {
1482                 talloc_free(vdata);
1483                 return 0;
1484         }
1485
1486         DEBUG(DEBUG_INFO,("Repacking %s with %u freelist entries and %u records to delete\n", 
1487                         name, freelist_size, vdata->delete_left));
1488
1489         /*
1490          * repack and implicitely get rid of the records we can delete
1491          */
1492         if (ctdb_repack_tdb(ctdb_db->ltdb->tdb, mem_ctx, vdata) != 0) {
1493                 DEBUG(DEBUG_ERR,(__location__ " Failed to repack '%s'\n", name));
1494                 talloc_free(vdata);
1495                 return -1;
1496         }
1497         talloc_free(vdata);
1498
1499         return 0;
1500 }
1501
1502 static uint32_t get_vacuum_interval(struct ctdb_db_context *ctdb_db)
1503 {
1504         uint32_t interval = ctdb_db->ctdb->tunable.vacuum_interval;
1505
1506         return interval;
1507 }
1508
1509 static int vacuum_child_destructor(struct ctdb_vacuum_child_context *child_ctx)
1510 {
1511         double l = timeval_elapsed(&child_ctx->start_time);
1512         struct ctdb_db_context *ctdb_db = child_ctx->vacuum_handle->ctdb_db;
1513         struct ctdb_context *ctdb = ctdb_db->ctdb;
1514
1515         DEBUG(DEBUG_INFO,("Vacuuming took %.3f seconds for database %s\n", l, ctdb_db->db_name));
1516
1517         if (child_ctx->child_pid != -1) {
1518                 ctdb_kill(ctdb, child_ctx->child_pid, SIGKILL);
1519         } else {
1520                 /* Bump the number of successful fast-path runs. */
1521                 child_ctx->vacuum_handle->fast_path_count++;
1522         }
1523
1524         DLIST_REMOVE(ctdb->vacuumers, child_ctx);
1525
1526         event_add_timed(ctdb->ev, child_ctx->vacuum_handle,
1527                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
1528                         ctdb_vacuum_event, child_ctx->vacuum_handle);
1529
1530         return 0;
1531 }
1532
1533 /*
1534  * this event is generated when a vacuum child process times out
1535  */
1536 static void vacuum_child_timeout(struct event_context *ev, struct timed_event *te,
1537                                          struct timeval t, void *private_data)
1538 {
1539         struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1540
1541         DEBUG(DEBUG_ERR,("Vacuuming child process timed out for db %s\n", child_ctx->vacuum_handle->ctdb_db->db_name));
1542
1543         child_ctx->status = VACUUM_TIMEOUT;
1544
1545         talloc_free(child_ctx);
1546 }
1547
1548
1549 /*
1550  * this event is generated when a vacuum child process has completed
1551  */
1552 static void vacuum_child_handler(struct event_context *ev, struct fd_event *fde,
1553                              uint16_t flags, void *private_data)
1554 {
1555         struct ctdb_vacuum_child_context *child_ctx = talloc_get_type(private_data, struct ctdb_vacuum_child_context);
1556         char c = 0;
1557         int ret;
1558
1559         DEBUG(DEBUG_INFO,("Vacuuming child process %d finished for db %s\n", child_ctx->child_pid, child_ctx->vacuum_handle->ctdb_db->db_name));
1560         child_ctx->child_pid = -1;
1561
1562         ret = read(child_ctx->fd[0], &c, 1);
1563         if (ret != 1 || c != 0) {
1564                 child_ctx->status = VACUUM_ERROR;
1565                 DEBUG(DEBUG_ERR, ("A vacuum child process failed with an error for database %s. ret=%d c=%d\n", child_ctx->vacuum_handle->ctdb_db->db_name, ret, c));
1566         } else {
1567                 child_ctx->status = VACUUM_OK;
1568         }
1569
1570         talloc_free(child_ctx);
1571 }
1572
1573 /*
1574  * this event is called every time we need to start a new vacuum process
1575  */
1576 static void
1577 ctdb_vacuum_event(struct event_context *ev, struct timed_event *te,
1578                                struct timeval t, void *private_data)
1579 {
1580         struct ctdb_vacuum_handle *vacuum_handle = talloc_get_type(private_data, struct ctdb_vacuum_handle);
1581         struct ctdb_db_context *ctdb_db = vacuum_handle->ctdb_db;
1582         struct ctdb_context *ctdb = ctdb_db->ctdb;
1583         struct ctdb_vacuum_child_context *child_ctx;
1584         struct tevent_fd *fde;
1585         int ret;
1586
1587         /* we dont vacuum if we are in recovery mode, or db frozen */
1588         if (ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ||
1589             ctdb->freeze_mode[ctdb_db->priority] != CTDB_FREEZE_NONE) {
1590                 DEBUG(DEBUG_INFO, ("Not vacuuming %s (%s)\n", ctdb_db->db_name,
1591                                    ctdb->recovery_mode == CTDB_RECOVERY_ACTIVE ? "in recovery"
1592                                    : ctdb->freeze_mode[ctdb_db->priority] == CTDB_FREEZE_PENDING
1593                                    ? "freeze pending"
1594                                    : "frozen"));
1595                 event_add_timed(ctdb->ev, vacuum_handle,
1596                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1597                         ctdb_vacuum_event, vacuum_handle);
1598                 return;
1599         }
1600
1601         child_ctx = talloc(vacuum_handle, struct ctdb_vacuum_child_context);
1602         if (child_ctx == NULL) {
1603                 DEBUG(DEBUG_CRIT, (__location__ " Failed to allocate child context for vacuuming of %s\n", ctdb_db->db_name));
1604                 ctdb_fatal(ctdb, "Out of memory when crating vacuum child context. Shutting down\n");
1605         }
1606
1607
1608         ret = pipe(child_ctx->fd);
1609         if (ret != 0) {
1610                 talloc_free(child_ctx);
1611                 DEBUG(DEBUG_ERR, ("Failed to create pipe for vacuum child process.\n"));
1612                 event_add_timed(ctdb->ev, vacuum_handle,
1613                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1614                         ctdb_vacuum_event, vacuum_handle);
1615                 return;
1616         }
1617
1618         if (vacuum_handle->fast_path_count > ctdb->tunable.vacuum_fast_path_count) {
1619                 vacuum_handle->fast_path_count = 0;
1620         }
1621
1622         child_ctx->child_pid = ctdb_fork(ctdb);
1623         if (child_ctx->child_pid == (pid_t)-1) {
1624                 close(child_ctx->fd[0]);
1625                 close(child_ctx->fd[1]);
1626                 talloc_free(child_ctx);
1627                 DEBUG(DEBUG_ERR, ("Failed to fork vacuum child process.\n"));
1628                 event_add_timed(ctdb->ev, vacuum_handle,
1629                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0),
1630                         ctdb_vacuum_event, vacuum_handle);
1631                 return;
1632         }
1633
1634
1635         if (child_ctx->child_pid == 0) {
1636                 char cc = 0;
1637                 bool full_vacuum_run = false;
1638                 close(child_ctx->fd[0]);
1639
1640                 DEBUG(DEBUG_INFO,("Vacuuming child process %d for db %s started\n", getpid(), ctdb_db->db_name));
1641                 ctdb_set_process_name("ctdb_vacuum");
1642                 if (switch_from_server_to_client(ctdb, "vacuum-%s", ctdb_db->db_name) != 0) {
1643                         DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch vacuum daemon into client mode. Shutting down.\n"));
1644                         _exit(1);
1645                 }
1646
1647                 /* 
1648                  * repack the db
1649                  */
1650                 if ((ctdb->tunable.vacuum_fast_path_count > 0) &&
1651                     (vacuum_handle->fast_path_count == 0))
1652                 {
1653                         full_vacuum_run = true;
1654                 }
1655                 cc = ctdb_vacuum_and_repack_db(ctdb_db, child_ctx,
1656                                                full_vacuum_run);
1657
1658                 write(child_ctx->fd[1], &cc, 1);
1659                 _exit(0);
1660         }
1661
1662         set_close_on_exec(child_ctx->fd[0]);
1663         close(child_ctx->fd[1]);
1664
1665         child_ctx->status = VACUUM_RUNNING;
1666         child_ctx->start_time = timeval_current();
1667
1668         DLIST_ADD(ctdb->vacuumers, child_ctx);
1669         talloc_set_destructor(child_ctx, vacuum_child_destructor);
1670
1671         /*
1672          * Clear the fastpath vacuuming list in the parent.
1673          */
1674         talloc_free(ctdb_db->delete_queue);
1675         ctdb_db->delete_queue = trbt_create(ctdb_db, 0);
1676         if (ctdb_db->delete_queue == NULL) {
1677                 /* fatal here? ... */
1678                 ctdb_fatal(ctdb, "Out of memory when re-creating vacuum tree "
1679                                  "in parent context. Shutting down\n");
1680         }
1681
1682         event_add_timed(ctdb->ev, child_ctx,
1683                 timeval_current_ofs(ctdb->tunable.vacuum_max_run_time, 0),
1684                 vacuum_child_timeout, child_ctx);
1685
1686         DEBUG(DEBUG_DEBUG, (__location__ " Created PIPE FD:%d to child vacuum process\n", child_ctx->fd[0]));
1687
1688         fde = event_add_fd(ctdb->ev, child_ctx, child_ctx->fd[0],
1689                            EVENT_FD_READ, vacuum_child_handler, child_ctx);
1690         tevent_fd_set_auto_close(fde);
1691
1692         vacuum_handle->child_ctx = child_ctx;
1693         child_ctx->vacuum_handle = vacuum_handle;
1694 }
1695
1696 void ctdb_stop_vacuuming(struct ctdb_context *ctdb)
1697 {
1698         /* Simply free them all. */
1699         while (ctdb->vacuumers) {
1700                 DEBUG(DEBUG_INFO, ("Aborting vacuuming for %s (%i)\n",
1701                            ctdb->vacuumers->vacuum_handle->ctdb_db->db_name,
1702                            (int)ctdb->vacuumers->child_pid));
1703                 /* vacuum_child_destructor kills it, removes from list */
1704                 talloc_free(ctdb->vacuumers);
1705         }
1706 }
1707
1708 /* this function initializes the vacuuming context for a database
1709  * starts the vacuuming events
1710  */
1711 int ctdb_vacuum_init(struct ctdb_db_context *ctdb_db)
1712 {
1713         if (ctdb_db->persistent != 0) {
1714                 DEBUG(DEBUG_ERR,("Vacuuming is disabled for persistent database %s\n", ctdb_db->db_name));
1715                 return 0;
1716         }
1717
1718         ctdb_db->vacuum_handle = talloc(ctdb_db, struct ctdb_vacuum_handle);
1719         CTDB_NO_MEMORY(ctdb_db->ctdb, ctdb_db->vacuum_handle);
1720
1721         ctdb_db->vacuum_handle->ctdb_db         = ctdb_db;
1722         ctdb_db->vacuum_handle->fast_path_count = 0;
1723
1724         event_add_timed(ctdb_db->ctdb->ev, ctdb_db->vacuum_handle, 
1725                         timeval_current_ofs(get_vacuum_interval(ctdb_db), 0), 
1726                         ctdb_vacuum_event, ctdb_db->vacuum_handle);
1727
1728         return 0;
1729 }
1730
1731 static void remove_record_from_delete_queue(struct ctdb_db_context *ctdb_db,
1732                                             const struct ctdb_ltdb_header *hdr,
1733                                             const TDB_DATA key)
1734 {
1735         struct delete_record_data *kd;
1736         uint32_t hash;
1737
1738         hash = (uint32_t)ctdb_hash(&key);
1739
1740         DEBUG(DEBUG_DEBUG, (__location__
1741                             " remove_record_from_delete_queue: "
1742                             "db[%s] "
1743                             "db_id[0x%08x] "
1744                             "key_hash[0x%08x] "
1745                             "lmaster[%u] "
1746                             "migrated_with_data[%s]\n",
1747                              ctdb_db->db_name, ctdb_db->db_id,
1748                              hash,
1749                              ctdb_lmaster(ctdb_db->ctdb, &key),
1750                              hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1751
1752         kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1753         if (kd == NULL) {
1754                 DEBUG(DEBUG_DEBUG, (__location__
1755                                     " remove_record_from_delete_queue: "
1756                                     "record not in queue (hash[0x%08x])\n.",
1757                                     hash));
1758                 return;
1759         }
1760
1761         if ((kd->key.dsize != key.dsize) ||
1762             (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1763         {
1764                 DEBUG(DEBUG_DEBUG, (__location__
1765                                     " remove_record_from_delete_queue: "
1766                                     "hash collision for key with hash[0x%08x] "
1767                                     "in db[%s] - skipping\n",
1768                                     hash, ctdb_db->db_name));
1769                 return;
1770         }
1771
1772         DEBUG(DEBUG_DEBUG, (__location__
1773                             " remove_record_from_delete_queue: "
1774                             "removing key with hash[0x%08x]\n",
1775                              hash));
1776
1777         talloc_free(kd);
1778
1779         return;
1780 }
1781
1782 /**
1783  * Insert a record into the ctdb_db context's delete queue,
1784  * handling hash collisions.
1785  */
1786 static int insert_record_into_delete_queue(struct ctdb_db_context *ctdb_db,
1787                                            const struct ctdb_ltdb_header *hdr,
1788                                            TDB_DATA key)
1789 {
1790         struct delete_record_data *kd;
1791         uint32_t hash;
1792         int ret;
1793
1794         hash = (uint32_t)ctdb_hash(&key);
1795
1796         DEBUG(DEBUG_INFO, (__location__ " schedule for deletion: db[%s] "
1797                            "db_id[0x%08x] "
1798                            "key_hash[0x%08x] "
1799                            "lmaster[%u] "
1800                            "migrated_with_data[%s]\n",
1801                             ctdb_db->db_name, ctdb_db->db_id,
1802                             hash,
1803                             ctdb_lmaster(ctdb_db->ctdb, &key),
1804                             hdr->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA ? "yes" : "no"));
1805
1806         kd = (struct delete_record_data *)trbt_lookup32(ctdb_db->delete_queue, hash);
1807         if (kd != NULL) {
1808                 if ((kd->key.dsize != key.dsize) ||
1809                     (memcmp(kd->key.dptr, key.dptr, key.dsize) != 0))
1810                 {
1811                         DEBUG(DEBUG_INFO,
1812                               (__location__ " schedule for deletion: "
1813                                "hash collision for key hash [0x%08x]. "
1814                                "Skipping the record.\n", hash));
1815                         return 0;
1816                 } else {
1817                         DEBUG(DEBUG_DEBUG,
1818                               (__location__ " schedule for deletion: "
1819                                "updating entry for key with hash [0x%08x].\n",
1820                                hash));
1821                 }
1822         }
1823
1824         ret = insert_delete_record_data_into_tree(ctdb_db->ctdb, ctdb_db,
1825                                                   ctdb_db->delete_queue,
1826                                                   hdr, key);
1827         if (ret != 0) {
1828                 DEBUG(DEBUG_INFO,
1829                       (__location__ " schedule for deletion: error "
1830                        "inserting key with hash [0x%08x] into delete queue\n",
1831                        hash));
1832                 return -1;
1833         }
1834
1835         return 0;
1836 }
1837
1838 /**
1839  * Schedule a record for deletetion.
1840  * Called from the parent context.
1841  */
1842 int32_t ctdb_control_schedule_for_deletion(struct ctdb_context *ctdb,
1843                                            TDB_DATA indata)
1844 {
1845         struct ctdb_control_schedule_for_deletion *dd;
1846         struct ctdb_db_context *ctdb_db;
1847         int ret;
1848         TDB_DATA key;
1849
1850         dd = (struct ctdb_control_schedule_for_deletion *)indata.dptr;
1851
1852         ctdb_db = find_ctdb_db(ctdb, dd->db_id);
1853         if (ctdb_db == NULL) {
1854                 DEBUG(DEBUG_ERR, (__location__ " Unknown db id 0x%08x\n",
1855                                   dd->db_id));
1856                 return -1;
1857         }
1858
1859         key.dsize = dd->keylen;
1860         key.dptr = dd->key;
1861
1862         ret = insert_record_into_delete_queue(ctdb_db, &dd->hdr, key);
1863
1864         return ret;
1865 }
1866
1867 int32_t ctdb_local_schedule_for_deletion(struct ctdb_db_context *ctdb_db,
1868                                          const struct ctdb_ltdb_header *hdr,
1869                                          TDB_DATA key)
1870 {
1871         int ret;
1872         struct ctdb_control_schedule_for_deletion *dd;
1873         TDB_DATA indata;
1874         int32_t status;
1875
1876         if (ctdb_db->ctdb->ctdbd_pid == getpid()) {
1877                 /* main daemon - directly queue */
1878                 ret = insert_record_into_delete_queue(ctdb_db, hdr, key);
1879
1880                 return ret;
1881         }
1882
1883         /* if we dont have a connection to the daemon we can not send
1884            a control. For example sometimes from update_record control child
1885            process.
1886         */
1887         if (!ctdb_db->ctdb->can_send_controls) {
1888                 return -1;
1889         }
1890
1891
1892         /* child process: send the main daemon a control */
1893         indata.dsize = offsetof(struct ctdb_control_schedule_for_deletion, key) + key.dsize;
1894         indata.dptr = talloc_zero_array(ctdb_db, uint8_t, indata.dsize);
1895         if (indata.dptr == NULL) {
1896                 DEBUG(DEBUG_ERR, (__location__ " out of memory\n"));
1897                 return -1;
1898         }
1899         dd = (struct ctdb_control_schedule_for_deletion *)(void *)indata.dptr;
1900         dd->db_id = ctdb_db->db_id;
1901         dd->hdr = *hdr;
1902         dd->keylen = key.dsize;
1903         memcpy(dd->key, key.dptr, key.dsize);
1904
1905         ret = ctdb_control(ctdb_db->ctdb,
1906                            CTDB_CURRENT_NODE,
1907                            ctdb_db->db_id,
1908                            CTDB_CONTROL_SCHEDULE_FOR_DELETION,
1909                            CTDB_CTRL_FLAG_NOREPLY, /* flags */
1910                            indata,
1911                            NULL, /* mem_ctx */
1912                            NULL, /* outdata */
1913                            &status,
1914                            NULL, /* timeout : NULL == wait forever */
1915                            NULL); /* error message */
1916
1917         talloc_free(indata.dptr);
1918
1919         if (ret != 0 || status != 0) {
1920                 DEBUG(DEBUG_ERR, (__location__ " Error sending "
1921                                   "SCHEDULE_FOR_DELETION "
1922                                   "control.\n"));
1923                 if (status != 0) {
1924                         ret = -1;
1925                 }
1926         }
1927
1928         return ret;
1929 }
1930
1931 void ctdb_local_remove_from_delete_queue(struct ctdb_db_context *ctdb_db,
1932                                          const struct ctdb_ltdb_header *hdr,
1933                                          const TDB_DATA key)
1934 {
1935         if (ctdb_db->ctdb->ctdbd_pid != getpid()) {
1936                 /*
1937                  * Only remove the record from the delete queue if called
1938                  * in the main daemon.
1939                  */
1940                 return;
1941         }
1942
1943         remove_record_from_delete_queue(ctdb_db, hdr, key);
1944
1945         return;
1946 }