Refactoring: Make get_schannel_session_key_auth_ntlmssp return NTSTATUS
[Samba.git] / source / lib / dbwrap_ctdb.c
bloba66ea7ceb590e2b1d8dbcaabc44782ec7e78c150
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper around ctdbd
4 Copyright (C) Volker Lendecke 2007
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.
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.
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/>.
20 #include "includes.h"
21 #ifdef CLUSTER_SUPPORT
22 #include "ctdb.h"
23 #include "ctdb_private.h"
24 #include "ctdbd_conn.h"
26 struct db_ctdb_ctx {
27 struct tdb_wrap *wtdb;
28 uint32 db_id;
31 struct db_ctdb_rec {
32 struct db_ctdb_ctx *ctdb_ctx;
33 struct ctdb_ltdb_header header;
36 static NTSTATUS db_ctdb_store(struct db_record *rec, TDB_DATA data, int flag)
38 struct db_ctdb_rec *crec = talloc_get_type_abort(
39 rec->private_data, struct db_ctdb_rec);
40 TDB_DATA cdata;
41 int ret;
43 cdata.dsize = sizeof(crec->header) + data.dsize;
45 if (!(cdata.dptr = SMB_MALLOC_ARRAY(uint8, cdata.dsize))) {
46 return NT_STATUS_NO_MEMORY;
49 memcpy(cdata.dptr, &crec->header, sizeof(crec->header));
50 memcpy(cdata.dptr + sizeof(crec->header), data.dptr, data.dsize);
52 ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, cdata, TDB_REPLACE);
54 SAFE_FREE(cdata.dptr);
56 return (ret == 0) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION;
60 /* for persistent databases the store is a bit different. We have to
61 ask the ctdb daemon to push the record to all nodes after the
62 store */
63 static NTSTATUS db_ctdb_store_persistent(struct db_record *rec, TDB_DATA data, int flag)
65 struct db_ctdb_rec *crec = talloc_get_type_abort(
66 rec->private_data, struct db_ctdb_rec);
67 TDB_DATA cdata;
68 int ret;
69 NTSTATUS status;
71 cdata.dsize = sizeof(crec->header) + data.dsize;
73 if (!(cdata.dptr = SMB_MALLOC_ARRAY(uint8, cdata.dsize))) {
74 return NT_STATUS_NO_MEMORY;
77 crec->header.rsn++;
79 memcpy(cdata.dptr, &crec->header, sizeof(crec->header));
80 memcpy(cdata.dptr + sizeof(crec->header), data.dptr, data.dsize);
82 ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, cdata, TDB_REPLACE);
83 status = (ret == 0) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION;
85 /* now tell ctdbd to update this record on all other nodes */
86 if (NT_STATUS_IS_OK(status)) {
87 status = ctdbd_persistent_store(messaging_ctdbd_connection(), crec->ctdb_ctx->db_id, rec->key, cdata);
90 SAFE_FREE(cdata.dptr);
92 return status;
95 static NTSTATUS db_ctdb_delete(struct db_record *rec)
97 struct db_ctdb_rec *crec = talloc_get_type_abort(
98 rec->private_data, struct db_ctdb_rec);
99 TDB_DATA data;
100 int ret;
103 * We have to store the header with empty data. TODO: Fix the
104 * tdb-level cleanup
107 data.dptr = (uint8 *)&crec->header;
108 data.dsize = sizeof(crec->header);
110 ret = tdb_store(crec->ctdb_ctx->wtdb->tdb, rec->key, data, TDB_REPLACE);
112 return (ret == 0) ? NT_STATUS_OK : NT_STATUS_INTERNAL_DB_CORRUPTION;
115 static int db_ctdb_record_destr(struct db_record* data)
117 struct db_ctdb_rec *crec = talloc_get_type_abort(
118 data->private_data, struct db_ctdb_rec);
120 DEBUG(10, (DEBUGLEVEL > 10
121 ? "Unlocking db %u key %s\n"
122 : "Unlocking db %u key %.20s\n",
123 (int)crec->ctdb_ctx->db_id,
124 hex_encode(data, (unsigned char *)data->key.dptr,
125 data->key.dsize)));
127 if (tdb_chainunlock(crec->ctdb_ctx->wtdb->tdb, data->key) != 0) {
128 DEBUG(0, ("tdb_chainunlock failed\n"));
129 return -1;
132 return 0;
135 static struct db_record *db_ctdb_fetch_locked(struct db_context *db,
136 TALLOC_CTX *mem_ctx,
137 TDB_DATA key)
139 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
140 struct db_ctdb_ctx);
141 struct db_record *result;
142 struct db_ctdb_rec *crec;
143 NTSTATUS status;
144 TDB_DATA ctdb_data;
145 int migrate_attempts = 0;
147 if (!(result = talloc(mem_ctx, struct db_record))) {
148 DEBUG(0, ("talloc failed\n"));
149 return NULL;
152 if (!(crec = TALLOC_ZERO_P(result, struct db_ctdb_rec))) {
153 DEBUG(0, ("talloc failed\n"));
154 TALLOC_FREE(result);
155 return NULL;
158 result->private_data = (void *)crec;
159 crec->ctdb_ctx = ctx;
161 result->key.dsize = key.dsize;
162 result->key.dptr = (uint8 *)talloc_memdup(result, key.dptr, key.dsize);
163 if (result->key.dptr == NULL) {
164 DEBUG(0, ("talloc failed\n"));
165 TALLOC_FREE(result);
166 return NULL;
170 * Do a blocking lock on the record
172 again:
174 if (DEBUGLEVEL >= 10) {
175 char *keystr = hex_encode(result, key.dptr, key.dsize);
176 DEBUG(10, (DEBUGLEVEL > 10
177 ? "Locking db %u key %s\n"
178 : "Locking db %u key %.20s\n",
179 (int)crec->ctdb_ctx->db_id, keystr));
180 TALLOC_FREE(keystr);
183 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
184 DEBUG(3, ("tdb_chainlock failed\n"));
185 TALLOC_FREE(result);
186 return NULL;
189 if (db->persistent) {
190 result->store = db_ctdb_store_persistent;
191 } else {
192 result->store = db_ctdb_store;
194 result->delete_rec = db_ctdb_delete;
195 talloc_set_destructor(result, db_ctdb_record_destr);
197 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
200 * See if we have a valid record and we are the dmaster. If so, we can
201 * take the shortcut and just return it.
204 if ((ctdb_data.dptr == NULL) ||
205 (ctdb_data.dsize < sizeof(struct ctdb_ltdb_header)) ||
206 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster != get_my_vnn()
207 #if 0
208 || (random() % 2 != 0)
209 #endif
211 SAFE_FREE(ctdb_data.dptr);
212 tdb_chainunlock(ctx->wtdb->tdb, key);
213 talloc_set_destructor(result, NULL);
215 migrate_attempts += 1;
217 DEBUG(10, ("ctdb_data.dptr = %p, dmaster = %u (%u)\n",
218 ctdb_data.dptr, ctdb_data.dptr ?
219 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster : -1,
220 get_my_vnn()));
222 status = ctdbd_migrate(messaging_ctdbd_connection(),ctx->db_id, key);
223 if (!NT_STATUS_IS_OK(status)) {
224 DEBUG(5, ("ctdb_migrate failed: %s\n",
225 nt_errstr(status)));
226 TALLOC_FREE(result);
227 return NULL;
229 /* now its migrated, try again */
230 goto again;
233 if (migrate_attempts > 10) {
234 DEBUG(0, ("db_ctdb_fetch_locked needed %d attempts\n",
235 migrate_attempts));
238 memcpy(&crec->header, ctdb_data.dptr, sizeof(crec->header));
240 result->value.dsize = ctdb_data.dsize - sizeof(crec->header);
241 result->value.dptr = NULL;
243 if ((result->value.dsize != 0)
244 && !(result->value.dptr = (uint8 *)talloc_memdup(
245 result, ctdb_data.dptr + sizeof(crec->header),
246 result->value.dsize))) {
247 DEBUG(0, ("talloc failed\n"));
248 TALLOC_FREE(result);
251 SAFE_FREE(ctdb_data.dptr);
253 return result;
257 fetch (unlocked, no migration) operation on ctdb
259 static int db_ctdb_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
260 TDB_DATA key, TDB_DATA *data)
262 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
263 struct db_ctdb_ctx);
264 NTSTATUS status;
265 TDB_DATA ctdb_data;
267 /* try a direct fetch */
268 ctdb_data = tdb_fetch(ctx->wtdb->tdb, key);
271 * See if we have a valid record and we are the dmaster. If so, we can
272 * take the shortcut and just return it.
273 * we bypass the dmaster check for persistent databases
275 if ((ctdb_data.dptr != NULL) &&
276 (ctdb_data.dsize >= sizeof(struct ctdb_ltdb_header)) &&
277 (db->persistent ||
278 ((struct ctdb_ltdb_header *)ctdb_data.dptr)->dmaster == get_my_vnn())) {
279 /* we are the dmaster - avoid the ctdb protocol op */
281 data->dsize = ctdb_data.dsize - sizeof(struct ctdb_ltdb_header);
282 if (data->dsize == 0) {
283 SAFE_FREE(ctdb_data.dptr);
284 data->dptr = NULL;
285 return 0;
288 data->dptr = (uint8 *)talloc_memdup(
289 mem_ctx, ctdb_data.dptr+sizeof(struct ctdb_ltdb_header),
290 data->dsize);
292 SAFE_FREE(ctdb_data.dptr);
294 if (data->dptr == NULL) {
295 return -1;
297 return 0;
300 SAFE_FREE(ctdb_data.dptr);
302 /* we weren't able to get it locally - ask ctdb to fetch it for us */
303 status = ctdbd_fetch(messaging_ctdbd_connection(),ctx->db_id, key, mem_ctx, data);
304 if (!NT_STATUS_IS_OK(status)) {
305 DEBUG(5, ("ctdbd_fetch failed: %s\n", nt_errstr(status)));
306 return -1;
309 return 0;
312 struct traverse_state {
313 struct db_context *db;
314 int (*fn)(struct db_record *rec, void *private_data);
315 void *private_data;
318 static void traverse_callback(TDB_DATA key, TDB_DATA data, void *private_data)
320 struct traverse_state *state = (struct traverse_state *)private_data;
321 struct db_record *rec;
322 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
323 /* we have to give them a locked record to prevent races */
324 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, key);
325 if (rec && rec->value.dsize > 0) {
326 state->fn(rec, state->private_data);
328 talloc_free(tmp_ctx);
331 static int traverse_persistent_callback(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
332 void *private_data)
334 struct traverse_state *state = (struct traverse_state *)private_data;
335 struct db_record *rec;
336 TALLOC_CTX *tmp_ctx = talloc_new(state->db);
337 int ret = 0;
338 /* we have to give them a locked record to prevent races */
339 rec = db_ctdb_fetch_locked(state->db, tmp_ctx, kbuf);
340 if (rec && rec->value.dsize > 0) {
341 ret = state->fn(rec, state->private_data);
343 talloc_free(tmp_ctx);
344 return ret;
347 static int db_ctdb_traverse(struct db_context *db,
348 int (*fn)(struct db_record *rec,
349 void *private_data),
350 void *private_data)
352 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
353 struct db_ctdb_ctx);
354 struct traverse_state state;
356 state.db = db;
357 state.fn = fn;
358 state.private_data = private_data;
360 if (db->persistent) {
361 /* for persistent databases we don't need to do a ctdb traverse,
362 we can do a faster local traverse */
363 return tdb_traverse(ctx->wtdb->tdb, traverse_persistent_callback, &state);
367 ctdbd_traverse(ctx->db_id, traverse_callback, &state);
368 return 0;
371 static NTSTATUS db_ctdb_store_deny(struct db_record *rec, TDB_DATA data, int flag)
373 return NT_STATUS_MEDIA_WRITE_PROTECTED;
376 static NTSTATUS db_ctdb_delete_deny(struct db_record *rec)
378 return NT_STATUS_MEDIA_WRITE_PROTECTED;
381 static void traverse_read_callback(TDB_DATA key, TDB_DATA data, void *private_data)
383 struct traverse_state *state = (struct traverse_state *)private_data;
384 struct db_record rec;
385 rec.key = key;
386 rec.value = data;
387 rec.store = db_ctdb_store_deny;
388 rec.delete_rec = db_ctdb_delete_deny;
389 rec.private_data = state->db;
390 state->fn(&rec, state->private_data);
393 static int traverse_persistent_callback_read(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
394 void *private_data)
396 struct traverse_state *state = (struct traverse_state *)private_data;
397 struct db_record rec;
398 rec.key = kbuf;
399 rec.value = dbuf;
400 rec.store = db_ctdb_store_deny;
401 rec.delete_rec = db_ctdb_delete_deny;
402 rec.private_data = state->db;
404 if (rec.value.dsize <= sizeof(struct ctdb_ltdb_header)) {
405 /* a deleted record */
406 return 0;
408 rec.value.dsize -= sizeof(struct ctdb_ltdb_header);
409 rec.value.dptr += sizeof(struct ctdb_ltdb_header);
411 return state->fn(&rec, state->private_data);
414 static int db_ctdb_traverse_read(struct db_context *db,
415 int (*fn)(struct db_record *rec,
416 void *private_data),
417 void *private_data)
419 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
420 struct db_ctdb_ctx);
421 struct traverse_state state;
423 state.db = db;
424 state.fn = fn;
425 state.private_data = private_data;
427 if (db->persistent) {
428 /* for persistent databases we don't need to do a ctdb traverse,
429 we can do a faster local traverse */
430 return tdb_traverse_read(ctx->wtdb->tdb, traverse_persistent_callback_read, &state);
433 ctdbd_traverse(ctx->db_id, traverse_read_callback, &state);
434 return 0;
437 static int db_ctdb_get_seqnum(struct db_context *db)
439 struct db_ctdb_ctx *ctx = talloc_get_type_abort(db->private_data,
440 struct db_ctdb_ctx);
441 return tdb_get_seqnum(ctx->wtdb->tdb);
444 static int db_ctdb_trans_dummy(struct db_context *db)
447 * Not implemented yet, just return ok
449 return 0;
452 struct db_context *db_open_ctdb(TALLOC_CTX *mem_ctx,
453 const char *name,
454 int hash_size, int tdb_flags,
455 int open_flags, mode_t mode)
457 struct db_context *result;
458 struct db_ctdb_ctx *db_ctdb;
459 char *db_path;
461 if (!lp_clustering()) {
462 DEBUG(10, ("Clustering disabled -- no ctdb\n"));
463 return NULL;
466 if (!(result = TALLOC_ZERO_P(mem_ctx, struct db_context))) {
467 DEBUG(0, ("talloc failed\n"));
468 TALLOC_FREE(result);
469 return NULL;
472 if (!(db_ctdb = TALLOC_P(result, struct db_ctdb_ctx))) {
473 DEBUG(0, ("talloc failed\n"));
474 TALLOC_FREE(result);
475 return NULL;
478 if (!NT_STATUS_IS_OK(ctdbd_db_attach(messaging_ctdbd_connection(),name, &db_ctdb->db_id, tdb_flags))) {
479 DEBUG(0, ("ctdbd_db_attach failed for %s\n", name));
480 TALLOC_FREE(result);
481 return NULL;
484 db_path = ctdbd_dbpath(messaging_ctdbd_connection(), db_ctdb, db_ctdb->db_id);
486 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
488 /* only pass through specific flags */
489 tdb_flags &= TDB_SEQNUM;
491 db_ctdb->wtdb = tdb_wrap_open(db_ctdb, db_path, hash_size, tdb_flags, O_RDWR, 0);
492 if (db_ctdb->wtdb == NULL) {
493 DEBUG(0, ("Could not open tdb %s: %s\n", db_path, strerror(errno)));
494 TALLOC_FREE(result);
495 return NULL;
497 talloc_free(db_path);
499 result->private_data = (void *)db_ctdb;
500 result->fetch_locked = db_ctdb_fetch_locked;
501 result->fetch = db_ctdb_fetch;
502 result->traverse = db_ctdb_traverse;
503 result->traverse_read = db_ctdb_traverse_read;
504 result->get_seqnum = db_ctdb_get_seqnum;
505 result->transaction_start = db_ctdb_trans_dummy;
506 result->transaction_commit = db_ctdb_trans_dummy;
507 result->transaction_cancel = db_ctdb_trans_dummy;
509 DEBUG(3,("db_open_ctdb: opened database '%s' with dbid 0x%x\n",
510 name, db_ctdb->db_id));
512 return result;
514 #endif