second try for safe transaction stores into persistend tdb databases
[samba.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/events/events.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         header->rsn = 0;
66         /* initial dmaster is the lmaster */
67         header->dmaster = ctdb_lmaster(ctdb_db->ctdb, &key);
68         header->laccessor = header->dmaster;
69         header->lacount = 0;
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 /*
124   write a record to a normal database
125 */
126 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
127                     struct ctdb_ltdb_header *header, TDB_DATA data)
128 {
129         struct ctdb_context *ctdb = ctdb_db->ctdb;
130         TDB_DATA rec;
131         int ret;
132
133         if (ctdb->flags & CTDB_FLAG_TORTURE) {
134                 struct ctdb_ltdb_header *h2;
135                 rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
136                 h2 = (struct ctdb_ltdb_header *)rec.dptr;
137                 if (rec.dptr && rec.dsize >= sizeof(h2) && h2->rsn > header->rsn) {
138                         DEBUG(DEBUG_CRIT,("RSN regression! %llu %llu\n",
139                                  (unsigned long long)h2->rsn, (unsigned long long)header->rsn));
140                 }
141                 if (rec.dptr) free(rec.dptr);
142         }
143
144         rec.dsize = sizeof(*header) + data.dsize;
145         rec.dptr = talloc_size(ctdb, rec.dsize);
146         CTDB_NO_MEMORY(ctdb, rec.dptr);
147
148         memcpy(rec.dptr, header, sizeof(*header));
149         memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
150
151         ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
152         if (ret != 0) {
153                 DEBUG(DEBUG_ERR, (__location__ " Failed to store dynamic data\n"));
154         }
155
156         talloc_free(rec.dptr);
157
158         return ret;
159 }
160
161 /*
162   write a record to a persistent database
163   at this stage the the record is locked by a lockwait child.
164 */
165 int ctdb_ltdb_persistent_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
166                     struct ctdb_ltdb_header *header, TDB_DATA data)
167 {
168         struct ctdb_context *ctdb = ctdb_db->ctdb;
169         TDB_DATA rec;
170         int ret;
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         /* if this is a persistent database without NOSYNC then we
191            will do this via a transaction */
192         if (ctdb_db->persistent && !(ctdb_db->client_tdb_flags & TDB_NOSYNC)) {
193                 ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
194                 if (ret != 0) {
195                         DEBUG(DEBUG_ERR, (__location__ " Failed to start local transaction\n"));
196                         goto failed;
197                 }
198                 ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
199                 if (ret != 0) {
200                         DEBUG(DEBUG_ERR, (__location__ " Failed to store persistent data\n"));
201                         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
202                         goto failed;
203                 }
204                 ret = tdb_transaction_commit(ctdb_db->ltdb->tdb);
205                 if (ret != 0) {
206                         DEBUG(DEBUG_ERR, (__location__ " Failed to commit persistent store transaction.\n"));
207                         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
208                         goto failed;
209                 }
210         } else {
211                 ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
212         }
213
214 failed:
215         talloc_free(rec.dptr);
216
217         return ret;
218 }
219
220 /*
221   lock a record in the ltdb, given a key
222  */
223 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
224 {
225         return tdb_chainlock(ctdb_db->ltdb->tdb, key);
226 }
227
228 /*
229   unlock a record in the ltdb, given a key
230  */
231 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
232 {
233         int ret = tdb_chainunlock(ctdb_db->ltdb->tdb, key);
234         if (ret != 0) {
235                 DEBUG(DEBUG_ERR,("tdb_chainunlock failed\n"));
236         }
237         return ret;
238 }