server: Use the ctdb_ltdb_store_server() in the ctdb daemon for non-persistent dbs
[vlendec/samba-autobuild/.git] / ctdb / common / ctdb_ltdb.c
1 /* 
2    ctdb ltdb code
3
4    Copyright (C) Andrew Tridgell  2006
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "lib/tevent/tevent.h"
22 #include "lib/tdb/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 }
69
70
71 /*
72   fetch a record from the ltdb, separating out the header information
73   and returning the body of the record. A valid (initial) header is
74   returned if the record is not present
75 */
76 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
77                     TDB_DATA key, struct ctdb_ltdb_header *header, 
78                     TALLOC_CTX *mem_ctx, TDB_DATA *data)
79 {
80         TDB_DATA rec;
81         struct ctdb_context *ctdb = ctdb_db->ctdb;
82
83         rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
84         if (rec.dsize < sizeof(*header)) {
85                 TDB_DATA d2;
86                 /* return an initial header */
87                 if (rec.dptr) free(rec.dptr);
88                 if (ctdb->vnn_map == NULL) {
89                         /* called from the client */
90                         ZERO_STRUCTP(data);
91                         header->dmaster = (uint32_t)-1;
92                         return -1;
93                 }
94                 ltdb_initial_header(ctdb_db, key, header);
95                 ZERO_STRUCT(d2);
96                 if (data) {
97                         *data = d2;
98                 }
99                 ctdb_ltdb_store(ctdb_db, key, header, d2);
100                 return 0;
101         }
102
103         *header = *(struct ctdb_ltdb_header *)rec.dptr;
104
105         if (data) {
106                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
107                 data->dptr = talloc_memdup(mem_ctx, 
108                                            sizeof(struct ctdb_ltdb_header)+rec.dptr,
109                                            data->dsize);
110         }
111
112         free(rec.dptr);
113         if (data) {
114                 CTDB_NO_MEMORY(ctdb, data->dptr);
115         }
116
117         return 0;
118 }
119
120
121 /*
122   write a record to a normal database
123 */
124 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
125                     struct ctdb_ltdb_header *header, TDB_DATA data)
126 {
127         struct ctdb_context *ctdb = ctdb_db->ctdb;
128         TDB_DATA rec;
129         int ret;
130         bool seqnum_suppressed = false;
131
132         if (ctdb_db->ctdb_ltdb_store_fn) {
133                 return ctdb_db->ctdb_ltdb_store_fn(ctdb_db, key, header, data);
134         }
135
136         if (ctdb->flags & CTDB_FLAG_TORTURE) {
137                 struct ctdb_ltdb_header *h2;
138                 rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
139                 h2 = (struct ctdb_ltdb_header *)rec.dptr;
140                 if (rec.dptr && rec.dsize >= sizeof(h2) && h2->rsn > header->rsn) {
141                         DEBUG(DEBUG_CRIT,("RSN regression! %llu %llu\n",
142                                  (unsigned long long)h2->rsn, (unsigned long long)header->rsn));
143                 }
144                 if (rec.dptr) free(rec.dptr);
145         }
146
147         rec.dsize = sizeof(*header) + data.dsize;
148         rec.dptr = talloc_size(ctdb, rec.dsize);
149         CTDB_NO_MEMORY(ctdb, rec.dptr);
150
151         memcpy(rec.dptr, header, sizeof(*header));
152         memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
153
154         /* Databases with seqnum updates enabled only get their seqnum
155            changes when/if we modify the data */
156         if (ctdb_db->seqnum_update != NULL) {
157                 TDB_DATA old;
158                 old = tdb_fetch(ctdb_db->ltdb->tdb, key);
159
160                 if ( (old.dsize == rec.dsize)
161                 && !memcmp(old.dptr+sizeof(struct ctdb_ltdb_header),
162                           rec.dptr+sizeof(struct ctdb_ltdb_header),
163                           rec.dsize-sizeof(struct ctdb_ltdb_header)) ) {
164                         tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
165                         seqnum_suppressed = true;
166                 }
167                 if (old.dptr) free(old.dptr);
168         }
169         ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
170         if (ret != 0) {
171                 DEBUG(DEBUG_ERR, (__location__ " Failed to store dynamic data\n"));
172         }
173         if (seqnum_suppressed) {
174                 tdb_add_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
175         }
176
177         talloc_free(rec.dptr);
178
179         return ret;
180 }
181
182 /*
183   lock a record in the ltdb, given a key
184  */
185 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
186 {
187         return tdb_chainlock(ctdb_db->ltdb->tdb, key);
188 }
189
190 /*
191   unlock a record in the ltdb, given a key
192  */
193 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
194 {
195         int ret = tdb_chainunlock(ctdb_db->ltdb->tdb, key);
196         if (ret != 0) {
197                 DEBUG(DEBUG_ERR,("tdb_chainunlock failed on db %s [%s]\n", ctdb_db->db_name, tdb_errorstr(ctdb_db->ltdb->tdb)));
198         }
199         return ret;
200 }
201
202
203 /*
204   delete a record from a normal database
205 */
206 int ctdb_ltdb_delete(struct ctdb_db_context *ctdb_db, TDB_DATA key)
207 {
208         struct ctdb_context *ctdb = ctdb_db->ctdb;
209
210         if (ctdb_db->persistent != 0) {
211                 DEBUG(DEBUG_ERR,("Trying to delete emty record in persistent database\n"));
212                 return 0;
213         }
214         if (tdb_delete(ctdb_db->ltdb->tdb, key) != 0) {
215                 DEBUG(DEBUG_ERR,("Failed to delete empty record."));
216                 return -1;
217         }
218         return 0;
219 }