build: Fix tdb.h path to enable building with system TDB library
[vlendec/samba-autobuild/.git] / ctdb / common / ctdb_ltdb.c
1 /* 
2    ctdb ltdb code
3
4    Copyright (C) Andrew Tridgell  2006
5    Copyright (C) Ronnie sahlberg  2011
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
21 #include "includes.h"
22 #include "tdb.h"
23 #include "system/network.h"
24 #include "system/filesys.h"
25 #include "../include/ctdb_private.h"
26 #include "db_wrap.h"
27 #include "lib/util/dlinklist.h"
28
29 /*
30   find an attached ctdb_db handle given a name
31  */
32 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name)
33 {
34         struct ctdb_db_context *tmp_db;
35         for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) {
36                 if (strcmp(name, tmp_db->db_name) == 0) {
37                         return tmp_db;
38                 }
39         }
40         return NULL;
41 }
42
43
44 /*
45   return the lmaster given a key
46 */
47 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key)
48 {
49         uint32_t idx, lmaster;
50
51         idx = ctdb_hash(key) % ctdb->vnn_map->size;
52         lmaster = ctdb->vnn_map->map[idx];
53
54         return lmaster;
55 }
56
57
58 /*
59   construct an initial header for a record with no ltdb header yet
60 */
61 static void ltdb_initial_header(struct ctdb_db_context *ctdb_db, 
62                                 TDB_DATA key,
63                                 struct ctdb_ltdb_header *header)
64 {
65         ZERO_STRUCTP(header);
66         /* initial dmaster is the lmaster */
67         header->dmaster = ctdb_lmaster(ctdb_db->ctdb, &key);
68         header->flags = CTDB_REC_FLAG_AUTOMATIC;
69         header->laccessor = header->dmaster;
70 }
71
72
73 /*
74   fetch a record from the ltdb, separating out the header information
75   and returning the body of the record. A valid (initial) header is
76   returned if the record is not present
77 */
78 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
79                     TDB_DATA key, struct ctdb_ltdb_header *header, 
80                     TALLOC_CTX *mem_ctx, TDB_DATA *data)
81 {
82         TDB_DATA rec;
83         struct ctdb_context *ctdb = ctdb_db->ctdb;
84
85         rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
86         if (rec.dsize < sizeof(*header)) {
87                 TDB_DATA d2;
88                 /* return an initial header */
89                 if (rec.dptr) free(rec.dptr);
90                 if (ctdb->vnn_map == NULL) {
91                         /* called from the client */
92                         ZERO_STRUCTP(data);
93                         header->dmaster = (uint32_t)-1;
94                         return -1;
95                 }
96                 ltdb_initial_header(ctdb_db, key, header);
97                 ZERO_STRUCT(d2);
98                 if (data) {
99                         *data = d2;
100                 }
101                 ctdb_ltdb_store(ctdb_db, key, header, d2);
102                 return 0;
103         }
104
105         *header = *(struct ctdb_ltdb_header *)rec.dptr;
106
107         if (data) {
108                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
109                 data->dptr = talloc_memdup(mem_ctx, 
110                                            sizeof(struct ctdb_ltdb_header)+rec.dptr,
111                                            data->dsize);
112         }
113
114         free(rec.dptr);
115         if (data) {
116                 CTDB_NO_MEMORY(ctdb, data->dptr);
117         }
118
119         return 0;
120 }
121
122 /*
123   fetch a record from the ltdb, separating out the header information
124   and returning the body of the record.
125   if the record does not exist, *header will be NULL
126   and data = {0, NULL}
127 */
128 int ctdb_ltdb_fetch_with_header(struct ctdb_db_context *ctdb_db, 
129                     TDB_DATA key, struct ctdb_ltdb_header *header, 
130                     TALLOC_CTX *mem_ctx, TDB_DATA *data)
131 {
132         TDB_DATA rec;
133
134         rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
135         if (rec.dsize < sizeof(*header)) {
136                 free(rec.dptr);
137
138                 data->dsize = 0;
139                 data->dptr = NULL;
140                 return -1;
141         }
142
143         *header = *(struct ctdb_ltdb_header *)rec.dptr;
144         if (data) {
145                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
146                 data->dptr = talloc_memdup(mem_ctx, 
147                                            sizeof(struct ctdb_ltdb_header)+rec.dptr,
148                                            data->dsize);
149         }
150
151         free(rec.dptr);
152
153         return 0;
154 }
155
156
157 /*
158   write a record to a normal database
159 */
160 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
161                     struct ctdb_ltdb_header *header, TDB_DATA data)
162 {
163         struct ctdb_context *ctdb = ctdb_db->ctdb;
164         TDB_DATA rec;
165         int ret;
166         bool seqnum_suppressed = false;
167
168         if (ctdb_db->ctdb_ltdb_store_fn) {
169                 return ctdb_db->ctdb_ltdb_store_fn(ctdb_db, key, header, data);
170         }
171
172         if (ctdb->flags & CTDB_FLAG_TORTURE) {
173                 struct ctdb_ltdb_header *h2;
174                 rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
175                 h2 = (struct ctdb_ltdb_header *)rec.dptr;
176                 if (rec.dptr && rec.dsize >= sizeof(h2) && h2->rsn > header->rsn) {
177                         DEBUG(DEBUG_CRIT,("RSN regression! %llu %llu\n",
178                                  (unsigned long long)h2->rsn, (unsigned long long)header->rsn));
179                 }
180                 if (rec.dptr) free(rec.dptr);
181         }
182
183         rec.dsize = sizeof(*header) + data.dsize;
184         rec.dptr = talloc_size(ctdb, rec.dsize);
185         CTDB_NO_MEMORY(ctdb, rec.dptr);
186
187         memcpy(rec.dptr, header, sizeof(*header));
188         memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
189
190         /* Databases with seqnum updates enabled only get their seqnum
191            changes when/if we modify the data */
192         if (ctdb_db->seqnum_update != NULL) {
193                 TDB_DATA old;
194                 old = tdb_fetch(ctdb_db->ltdb->tdb, key);
195
196                 if ( (old.dsize == rec.dsize)
197                 && !memcmp(old.dptr+sizeof(struct ctdb_ltdb_header),
198                           rec.dptr+sizeof(struct ctdb_ltdb_header),
199                           rec.dsize-sizeof(struct ctdb_ltdb_header)) ) {
200                         tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
201                         seqnum_suppressed = true;
202                 }
203                 if (old.dptr) free(old.dptr);
204         }
205         ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
206         if (ret != 0) {
207                 DEBUG(DEBUG_ERR, (__location__ " Failed to store dynamic data\n"));
208         }
209         if (seqnum_suppressed) {
210                 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
211         }
212
213         talloc_free(rec.dptr);
214
215         return ret;
216 }
217
218 /*
219   lock a record in the ltdb, given a key
220  */
221 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
222 {
223         return tdb_chainlock(ctdb_db->ltdb->tdb, key);
224 }
225
226 /*
227   unlock a record in the ltdb, given a key
228  */
229 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
230 {
231         int ret = tdb_chainunlock(ctdb_db->ltdb->tdb, key);
232         if (ret != 0) {
233                 DEBUG(DEBUG_ERR,("tdb_chainunlock failed on db %s [%s]\n", ctdb_db->db_name, tdb_errorstr(ctdb_db->ltdb->tdb)));
234         }
235         return ret;
236 }
237
238
239 /*
240   delete a record from a normal database
241 */
242 int ctdb_ltdb_delete(struct ctdb_db_context *ctdb_db, TDB_DATA key)
243 {
244         if (ctdb_db->persistent != 0) {
245                 DEBUG(DEBUG_ERR,("Trying to delete emty record in persistent database\n"));
246                 return 0;
247         }
248         if (tdb_delete(ctdb_db->ltdb->tdb, key) != 0) {
249                 DEBUG(DEBUG_ERR,("Failed to delete empty record."));
250                 return -1;
251         }
252         return 0;
253 }
254
255 int ctdb_trackingdb_add_pnn(struct ctdb_context *ctdb, TDB_DATA *data, uint32_t pnn)
256 {
257         int byte_pos = pnn / 8;
258         int bit_mask   = 1 << (pnn % 8);
259
260         if (byte_pos + 1 > data->dsize) {
261                 char *buf;
262
263                 buf = malloc(byte_pos + 1);
264                 memset(buf, 0, byte_pos + 1);
265                 if (buf == NULL) {
266                         DEBUG(DEBUG_ERR, ("Out of memory when allocating buffer of %d bytes for trackingdb\n", byte_pos + 1));
267                         return -1;
268                 }
269                 if (data->dptr != NULL) {
270                         memcpy(buf, data->dptr, data->dsize);
271                         free(data->dptr);
272                 }
273                 data->dptr  = (uint8_t *)buf;
274                 data->dsize = byte_pos + 1;
275         }
276
277         data->dptr[byte_pos] |= bit_mask;
278         return 0;
279 }
280
281 void ctdb_trackingdb_traverse(struct ctdb_context *ctdb, TDB_DATA data, ctdb_trackingdb_cb cb, void *private_data)
282 {
283         int i;
284
285         for(i = 0; i < data.dsize; i++) {
286                 int j;
287
288                 for (j=0; j<8; j++) {
289                         int mask = 1<<j;
290
291                         if (data.dptr[i] & mask) {
292                                 cb(ctdb, i * 8 + j, private_data);
293                         }
294                 }
295         }
296 }
297
298 /*
299   this is the dummy null procedure that all databases support
300 */
301 int ctdb_null_func(struct ctdb_call_info *call)
302 {
303         return 0;
304 }
305
306 /*
307   this is a plain fetch procedure that all databases support
308 */
309 int ctdb_fetch_func(struct ctdb_call_info *call)
310 {
311         call->reply_data = &call->record_data;
312         return 0;
313 }
314
315 /*
316   this is a plain fetch procedure that all databases support
317   this returns the full record including the ltdb header
318 */
319 int ctdb_fetch_with_header_func(struct ctdb_call_info *call)
320 {
321         call->reply_data = talloc(call, TDB_DATA);
322         if (call->reply_data == NULL) {
323                 return -1;
324         }
325         call->reply_data->dsize = sizeof(struct ctdb_ltdb_header) + call->record_data.dsize;
326         call->reply_data->dptr  = talloc_size(call->reply_data, call->reply_data->dsize);
327         if (call->reply_data->dptr == NULL) {
328                 return -1;
329         }
330         memcpy(call->reply_data->dptr, call->header, sizeof(struct ctdb_ltdb_header));
331         memcpy(&call->reply_data->dptr[sizeof(struct ctdb_ltdb_header)], call->record_data.dptr, call->record_data.dsize);
332
333         return 0;
334 }
335