merge from tridge
[metze/ctdb/wip.git] / 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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/events/events.h"
23 #include "lib/tdb/include/tdb.h"
24 #include "system/network.h"
25 #include "system/filesys.h"
26 #include "../include/ctdb_private.h"
27 #include "db_wrap.h"
28 #include "lib/util/dlinklist.h"
29
30 /*
31   find an attached ctdb_db handle given a name
32  */
33 struct ctdb_db_context *ctdb_db_handle(struct ctdb_context *ctdb, const char *name)
34 {
35         struct ctdb_db_context *tmp_db;
36         for (tmp_db=ctdb->db_list;tmp_db;tmp_db=tmp_db->next) {
37                 if (strcmp(name, tmp_db->db_name) == 0) {
38                         return tmp_db;
39                 }
40         }
41         return NULL;
42 }
43
44
45 /*
46   return the lmaster given a key
47 */
48 uint32_t ctdb_lmaster(struct ctdb_context *ctdb, const TDB_DATA *key)
49 {
50         uint32_t idx, lmaster;
51
52         idx = ctdb_hash(key) % ctdb->vnn_map->size;
53         lmaster = ctdb->vnn_map->map[idx];
54
55         return lmaster;
56 }
57
58
59 /*
60   construct an initial header for a record with no ltdb header yet
61 */
62 static void ltdb_initial_header(struct ctdb_db_context *ctdb_db, 
63                                 TDB_DATA key,
64                                 struct ctdb_ltdb_header *header)
65 {
66         header->rsn = 0;
67         /* initial dmaster is the lmaster */
68         header->dmaster = ctdb_lmaster(ctdb_db->ctdb, &key);
69         header->laccessor = header->dmaster;
70         header->lacount = 0;
71 }
72
73
74 /*
75   fetch a record from the ltdb, separating out the header information
76   and returning the body of the record. A valid (initial) header is
77   returned if the record is not present
78 */
79 int ctdb_ltdb_fetch(struct ctdb_db_context *ctdb_db, 
80                     TDB_DATA key, struct ctdb_ltdb_header *header, 
81                     TALLOC_CTX *mem_ctx, TDB_DATA *data)
82 {
83         TDB_DATA rec;
84         struct ctdb_context *ctdb = ctdb_db->ctdb;
85
86         rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
87         if (rec.dsize < sizeof(*header)) {
88                 TDB_DATA d2;
89                 /* return an initial header */
90                 if (rec.dptr) free(rec.dptr);
91                 if (ctdb->vnn_map == NULL) {
92                         /* called from the client */
93                         ZERO_STRUCTP(data);
94                         header->dmaster = (uint32_t)-1;
95                         return -1;
96                 }
97                 ltdb_initial_header(ctdb_db, key, header);
98                 ZERO_STRUCT(d2);
99                 if (data) {
100                         *data = d2;
101                 }
102                 ctdb_ltdb_store(ctdb_db, key, header, d2);
103                 return 0;
104         }
105
106         *header = *(struct ctdb_ltdb_header *)rec.dptr;
107
108         if (data) {
109                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
110                 data->dptr = talloc_memdup(mem_ctx, 
111                                            sizeof(struct ctdb_ltdb_header)+rec.dptr,
112                                            data->dsize);
113         }
114
115         free(rec.dptr);
116         if (data) {
117                 CTDB_NO_MEMORY(ctdb, data->dptr);
118         }
119
120         return 0;
121 }
122
123
124 /*
125   fetch a record from the ltdb, separating out the header information
126   and returning the body of the record. A valid (initial) header is
127   returned if the record is not present
128 */
129 int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
130                     struct ctdb_ltdb_header *header, TDB_DATA data)
131 {
132         struct ctdb_context *ctdb = ctdb_db->ctdb;
133         TDB_DATA rec;
134         int ret;
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(0,("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         ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
155         talloc_free(rec.dptr);
156
157         return ret;
158 }
159
160
161 /*
162   lock a record in the ltdb, given a key
163  */
164 int ctdb_ltdb_lock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
165 {
166         return tdb_chainlock(ctdb_db->ltdb->tdb, key);
167 }
168
169 /*
170   unlock a record in the ltdb, given a key
171  */
172 int ctdb_ltdb_unlock(struct ctdb_db_context *ctdb_db, TDB_DATA key)
173 {
174         int ret = tdb_chainunlock(ctdb_db->ltdb->tdb, key);
175         if (ret != 0) {
176                 DEBUG(0,("tdb_chainunlock failed\n"));
177         }
178         return ret;
179 }