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/>.
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
)
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
)
76 TDB_DATA
dbwrap_record_get_value(const struct db_record
*rec
)
81 NTSTATUS
dbwrap_record_store(struct db_record
*rec
, TDB_DATA data
, int flags
)
84 struct db_context
*db
;
86 status
= rec
->store(rec
, data
, flags
);
87 if (!NT_STATUS_IS_OK(status
)) {
91 if (db
->stored_callback
!= NULL
) {
92 db
->stored_callback(db
, rec
,
93 db
->stored_callback_private_data
);
98 void dbwrap_set_stored_callback(
99 struct db_context
*db
,
100 void (*cb
)(struct db_context
*db
, struct db_record
*rec
,
104 db
->stored_callback
= cb
;
105 db
->stored_callback_private_data
= private_data
;
108 NTSTATUS
dbwrap_record_delete(struct db_record
*rec
)
111 struct db_context
*db
;
113 status
= rec
->delete_rec(rec
);
114 if (!NT_STATUS_IS_OK(status
)) {
118 if (db
->stored_callback
!= NULL
) {
119 db
->stored_callback(db
, rec
,
120 db
->stored_callback_private_data
);
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
[])
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
);
163 static struct dbwrap_lock_order_state
*dbwrap_check_lock_order(
164 struct db_context
*db
, TALLOC_CTX
*mem_ctx
)
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
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");
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");
200 state
= talloc(mem_ctx
, struct dbwrap_lock_order_state
);
202 DEBUG(1, ("talloc failed\n"));
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
);
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
,
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
) {
230 rec
= db_fn(db
, mem_ctx
, key
);
235 (void)talloc_steal(rec
, lock_order
);
241 struct db_record
*dbwrap_fetch_locked(struct db_context
*db
,
245 return dbwrap_fetch_locked_internal(db
, mem_ctx
, key
,
249 struct db_record
*dbwrap_try_fetch_locked(struct db_context
*db
,
253 return dbwrap_fetch_locked_internal(
256 ? db
->try_fetch_locked
: db
->fetch_locked
);
259 struct db_record
*dbwrap_fetch_locked_timeout(struct db_context
*db
,
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
) {
273 rec
= db
->fetch_locked_timeout
274 ? db
->fetch_locked_timeout(db
, mem_ctx
, key
, timeout
)
275 : db
->fetch_locked(db
, mem_ctx
, key
);
280 (void)talloc_steal(rec
, lock_order
);
286 struct db_context
*dbwrap_record_get_db(struct db_record
*rec
)
291 struct dbwrap_fetch_state
{
296 static void dbwrap_fetch_parser(TDB_DATA key
, TDB_DATA 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
,
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
;
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
)) {
323 if ((state
.data
.dsize
!= 0) && (state
.data
.dptr
== NULL
)) {
324 return NT_STATUS_NO_MEMORY
;
330 bool dbwrap_exists(struct db_context
*db
, TDB_DATA key
)
333 if (db
->exists
!= NULL
) {
334 result
= db
->exists(db
, key
);
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
;
346 TALLOC_CTX
*frame
= talloc_stackframe();
348 rec
= dbwrap_fetch_locked(db
, frame
, key
);
351 return NT_STATUS_NO_MEMORY
;
354 status
= dbwrap_record_store(rec
, data
, flags
);
359 NTSTATUS
dbwrap_delete(struct db_context
*db
, TDB_DATA key
)
361 struct db_record
*rec
;
363 TALLOC_CTX
*frame
= talloc_stackframe();
365 rec
= dbwrap_fetch_locked(db
, frame
, key
);
368 return NT_STATUS_NO_MEMORY
;
370 status
= dbwrap_record_delete(rec
);
375 NTSTATUS
dbwrap_traverse(struct db_context
*db
,
376 int (*f
)(struct db_record
*, void*),
380 int ret
= db
->traverse(db
, f
, private_data
);
383 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
393 NTSTATUS
dbwrap_traverse_read(struct db_context
*db
,
394 int (*f
)(struct db_record
*, void*),
398 int ret
= db
->traverse_read(db
, f
, private_data
);
401 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
411 static void dbwrap_null_parser(TDB_DATA key
, TDB_DATA val
, void* data
)
416 NTSTATUS
dbwrap_parse_record(struct db_context
*db
, TDB_DATA key
,
417 void (*parser
)(TDB_DATA key
, TDB_DATA 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
);
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
);
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
)