docs: Add man 8 samba-tool.
[Samba.git] / lib / dbwrap / dbwrap.c
blob0e0422c41b5e66a0c10cc9324b87266a3045f3c2
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
6 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_private.h"
25 #include "lib/util/util_tdb.h"
28 * Fall back using fetch if no genuine exists operation is provided
31 static int dbwrap_fallback_exists(struct db_context *db, TDB_DATA key)
33 NTSTATUS status = dbwrap_parse_record(db, key, NULL, NULL);
34 return NT_STATUS_IS_OK(status) ? 1 : 0;
37 static int delete_record(struct db_record *rec, void *data)
39 NTSTATUS status = dbwrap_record_delete(rec);
40 return NT_STATUS_IS_OK(status) ? 0 : -1;
44 * Fallback wipe implementation using traverse and delete if no genuine
45 * wipe operation is provided
47 static int dbwrap_fallback_wipe(struct db_context *db)
49 NTSTATUS status = dbwrap_trans_traverse(db, delete_record, NULL);
50 return NT_STATUS_IS_OK(status) ? 0 : -1;
53 static int do_nothing(struct db_record *rec, void *unused)
55 return 0;
59 * Fallback check operation: just traverse.
61 static int dbwrap_fallback_check(struct db_context *db)
63 NTSTATUS status = dbwrap_traverse_read(db, do_nothing, NULL, NULL);
64 return NT_STATUS_IS_OK(status) ? 0 : -1;
68 * Wrapper functions for the backend methods
71 TDB_DATA dbwrap_record_get_key(const struct db_record *rec)
73 return rec->key;
76 TDB_DATA dbwrap_record_get_value(const struct db_record *rec)
78 return rec->value;
81 NTSTATUS dbwrap_record_store(struct db_record *rec, TDB_DATA data, int flags)
83 NTSTATUS status;
84 struct db_context *db;
86 status = rec->store(rec, data, flags);
87 if (!NT_STATUS_IS_OK(status)) {
88 return status;
90 db = rec->db;
91 if (db->stored_callback != NULL) {
92 db->stored_callback(db, rec,
93 db->stored_callback_private_data);
95 return NT_STATUS_OK;
98 void dbwrap_set_stored_callback(
99 struct db_context *db,
100 void (*cb)(struct db_context *db, struct db_record *rec,
101 void *private_data),
102 void *private_data)
104 db->stored_callback = cb;
105 db->stored_callback_private_data = private_data;
108 NTSTATUS dbwrap_record_delete(struct db_record *rec)
110 NTSTATUS status;
111 struct db_context *db;
113 status = rec->delete_rec(rec);
114 if (!NT_STATUS_IS_OK(status)) {
115 return status;
117 db = rec->db;
118 if (db->stored_callback != NULL) {
119 db->stored_callback(db, rec,
120 db->stored_callback_private_data);
122 return NT_STATUS_OK;
125 struct dbwrap_lock_order_state {
126 struct db_context **locked_dbs;
127 struct db_context *db;
130 static void debug_lock_order(int level, struct db_context *dbs[])
132 int i;
133 DEBUG(level, ("lock order: "));
134 for (i=0; i<DBWRAP_LOCK_ORDER_MAX; i++) {
135 DEBUGADD(level, (" %d:%s", i + 1, dbs[i] ? dbs[i]->name : "<none>"));
137 DEBUGADD(level, ("\n"));
140 static int dbwrap_lock_order_state_destructor(
141 struct dbwrap_lock_order_state *s)
143 int idx = s->db->lock_order - 1;
145 DEBUG(5, ("release lock order %d for %s\n",
146 (int)s->db->lock_order, s->db->name));
148 if (s->locked_dbs[idx] != s->db) {
149 DEBUG(0, ("locked db at lock order %d is %s, expected %s\n",
150 idx + 1, s->locked_dbs[idx]->name, s->db->name));
151 debug_lock_order(0, s->locked_dbs);
152 smb_panic("inconsistent lock_order\n");
155 s->locked_dbs[idx] = NULL;
157 debug_lock_order(10, s->locked_dbs);
159 return 0;
163 static struct dbwrap_lock_order_state *dbwrap_check_lock_order(
164 struct db_context *db, TALLOC_CTX *mem_ctx)
166 int idx;
167 static struct db_context *locked_dbs[DBWRAP_LOCK_ORDER_MAX];
168 struct dbwrap_lock_order_state *state = NULL;
170 if (db->lock_order == 0) {
172 * lock order 0 is for example for dbwrap_rbt without
173 * real locking. Return state nevertheless to avoid
174 * special cases.
176 return talloc_zero(mem_ctx, struct dbwrap_lock_order_state);
179 if (db->lock_order > DBWRAP_LOCK_ORDER_MAX) {
180 DEBUG(0,("Invalid lock order %d of %s\n",
181 (int)db->lock_order, db->name));
182 smb_panic("invalid lock_order\n");
183 return NULL;
186 DEBUG(5, ("check lock order %d for %s\n",
187 (int)db->lock_order, db->name));
190 for (idx=db->lock_order - 1; idx < DBWRAP_LOCK_ORDER_MAX; idx++) {
191 if (locked_dbs[idx] != NULL) {
192 DEBUG(0, ("Lock order violation: Trying %s at %d while %s at %d is locked\n",
193 db->name, (int)db->lock_order, locked_dbs[idx]->name, idx + 1));
194 debug_lock_order(0, locked_dbs);
195 smb_panic("invalid lock_order");
196 return NULL;
200 state = talloc(mem_ctx, struct dbwrap_lock_order_state);
201 if (state == NULL) {
202 DEBUG(1, ("talloc failed\n"));
203 return NULL;
205 state->db = db;
206 state->locked_dbs = locked_dbs;
207 talloc_set_destructor(state, dbwrap_lock_order_state_destructor);
209 locked_dbs[db->lock_order - 1] = db;
211 debug_lock_order(10, locked_dbs);
213 return state;
216 static struct db_record *dbwrap_fetch_locked_internal(
217 struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key,
218 struct db_record *(*db_fn)(struct db_context *db, TALLOC_CTX *mem_ctx,
219 TDB_DATA key))
221 struct db_record *rec;
222 struct dbwrap_lock_order_state *lock_order;
223 TALLOC_CTX *frame = talloc_stackframe();
225 lock_order = dbwrap_check_lock_order(db, frame);
226 if (lock_order == NULL) {
227 TALLOC_FREE(frame);
228 return NULL;
230 rec = db_fn(db, mem_ctx, key);
231 if (rec == NULL) {
232 TALLOC_FREE(frame);
233 return NULL;
235 (void)talloc_steal(rec, lock_order);
236 rec->db = db;
237 TALLOC_FREE(frame);
238 return rec;
241 struct db_record *dbwrap_fetch_locked(struct db_context *db,
242 TALLOC_CTX *mem_ctx,
243 TDB_DATA key)
245 return dbwrap_fetch_locked_internal(db, mem_ctx, key,
246 db->fetch_locked);
249 struct db_record *dbwrap_try_fetch_locked(struct db_context *db,
250 TALLOC_CTX *mem_ctx,
251 TDB_DATA key)
253 return dbwrap_fetch_locked_internal(
254 db, mem_ctx, key,
255 db->try_fetch_locked
256 ? db->try_fetch_locked : db->fetch_locked);
259 struct db_record *dbwrap_fetch_locked_timeout(struct db_context *db,
260 TALLOC_CTX *mem_ctx,
261 TDB_DATA key,
262 unsigned int timeout)
264 struct db_record *rec;
265 struct dbwrap_lock_order_state *lock_order;
266 TALLOC_CTX *frame = talloc_stackframe();
268 lock_order = dbwrap_check_lock_order(db, frame);
269 if (lock_order == NULL) {
270 TALLOC_FREE(frame);
271 return NULL;
273 rec = db->fetch_locked_timeout
274 ? db->fetch_locked_timeout(db, mem_ctx, key, timeout)
275 : db->fetch_locked(db, mem_ctx, key);
276 if (rec == NULL) {
277 TALLOC_FREE(frame);
278 return NULL;
280 (void)talloc_steal(rec, lock_order);
281 rec->db = db;
282 TALLOC_FREE(frame);
283 return rec;
286 struct db_context *dbwrap_record_get_db(struct db_record *rec)
288 return rec->db;
291 struct dbwrap_fetch_state {
292 TALLOC_CTX *mem_ctx;
293 TDB_DATA data;
296 static void dbwrap_fetch_parser(TDB_DATA key, TDB_DATA data,
297 void *private_data)
299 struct dbwrap_fetch_state *state =
300 (struct dbwrap_fetch_state *)private_data;
302 state->data.dsize = data.dsize;
303 state->data.dptr = (uint8_t *)talloc_memdup(state->mem_ctx, data.dptr,
304 data.dsize);
307 NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
308 TDB_DATA key, TDB_DATA *value)
310 struct dbwrap_fetch_state state;
311 NTSTATUS status;
313 if (value == NULL) {
314 return NT_STATUS_INVALID_PARAMETER;
317 state.mem_ctx = mem_ctx;
319 status = dbwrap_parse_record(db, key, dbwrap_fetch_parser, &state);
320 if (!NT_STATUS_IS_OK(status)) {
321 return status;
323 if ((state.data.dsize != 0) && (state.data.dptr == NULL)) {
324 return NT_STATUS_NO_MEMORY;
326 *value = state.data;
327 return NT_STATUS_OK;
330 bool dbwrap_exists(struct db_context *db, TDB_DATA key)
332 int result;
333 if (db->exists != NULL) {
334 result = db->exists(db, key);
335 } else {
336 result = dbwrap_fallback_exists(db,key);
338 return (result == 1);
341 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
342 TDB_DATA data, int flags)
344 struct db_record *rec;
345 NTSTATUS status;
346 TALLOC_CTX *frame = talloc_stackframe();
348 rec = dbwrap_fetch_locked(db, frame, key);
349 if (rec == NULL) {
350 TALLOC_FREE(frame);
351 return NT_STATUS_NO_MEMORY;
354 status = dbwrap_record_store(rec, data, flags);
355 TALLOC_FREE(frame);
356 return status;
359 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
361 struct db_record *rec;
362 NTSTATUS status;
363 TALLOC_CTX *frame = talloc_stackframe();
365 rec = dbwrap_fetch_locked(db, frame, key);
366 if (rec == NULL) {
367 TALLOC_FREE(frame);
368 return NT_STATUS_NO_MEMORY;
370 status = dbwrap_record_delete(rec);
371 TALLOC_FREE(frame);
372 return status;
375 NTSTATUS dbwrap_traverse(struct db_context *db,
376 int (*f)(struct db_record*, void*),
377 void *private_data,
378 int *count)
380 int ret = db->traverse(db, f, private_data);
382 if (ret < 0) {
383 return NT_STATUS_INTERNAL_DB_CORRUPTION;
386 if (count != NULL) {
387 *count = ret;
390 return NT_STATUS_OK;
393 NTSTATUS dbwrap_traverse_read(struct db_context *db,
394 int (*f)(struct db_record*, void*),
395 void *private_data,
396 int *count)
398 int ret = db->traverse_read(db, f, private_data);
400 if (ret < 0) {
401 return NT_STATUS_INTERNAL_DB_CORRUPTION;
404 if (count != NULL) {
405 *count = ret;
408 return NT_STATUS_OK;
411 static void dbwrap_null_parser(TDB_DATA key, TDB_DATA val, void* data)
413 return;
416 NTSTATUS dbwrap_parse_record(struct db_context *db, TDB_DATA key,
417 void (*parser)(TDB_DATA key, TDB_DATA data,
418 void *private_data),
419 void *private_data)
421 if (parser == NULL) {
422 parser = dbwrap_null_parser;
424 return db->parse_record(db, key, parser, private_data);
427 int dbwrap_wipe(struct db_context *db)
429 if (db->wipe == NULL) {
430 return dbwrap_fallback_wipe(db);
432 return db->wipe(db);
435 int dbwrap_hash_size(struct db_context *db)
437 return db->hash_size;
440 int dbwrap_check(struct db_context *db)
442 if (db->check == NULL) {
443 return dbwrap_fallback_check(db);
445 return db->check(db);
448 int dbwrap_get_seqnum(struct db_context *db)
450 return db->get_seqnum(db);
453 int dbwrap_transaction_start(struct db_context *db)
455 return db->transaction_start(db);
458 NTSTATUS dbwrap_transaction_start_nonblock(struct db_context *db)
460 if (db->transaction_start_nonblock) {
461 return db->transaction_start_nonblock(db);
462 } else {
463 return dbwrap_transaction_start(db) == 0 ? NT_STATUS_OK
464 : NT_STATUS_UNSUCCESSFUL;
468 int dbwrap_transaction_commit(struct db_context *db)
470 return db->transaction_commit(db);
473 int dbwrap_transaction_cancel(struct db_context *db)
475 return db->transaction_cancel(db);
478 void dbwrap_db_id(struct db_context *db, const uint8_t **id, size_t *idlen)
480 db->id(db, id, idlen);
483 const char *dbwrap_name(struct db_context *db)
485 return db->name;