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 (!DBWRAP_LOCK_ORDER_VALID(db
->lock_order
)) {
171 DEBUG(0,("Invalid lock order %d of %s\n",
172 (int)db
->lock_order
, db
->name
));
173 smb_panic("invalid lock_order\n");
177 DEBUG(5, ("check lock order %d for %s\n",
178 (int)db
->lock_order
, db
->name
));
181 for (idx
=db
->lock_order
- 1; idx
< DBWRAP_LOCK_ORDER_MAX
; idx
++) {
182 if (locked_dbs
[idx
] != NULL
) {
183 DEBUG(0, ("Lock order violation: Trying %s at %d while %s at %d is locked\n",
184 db
->name
, (int)db
->lock_order
, locked_dbs
[idx
]->name
, idx
+ 1));
185 debug_lock_order(0, locked_dbs
);
186 smb_panic("invalid lock_order");
191 state
= talloc(mem_ctx
, struct dbwrap_lock_order_state
);
193 DEBUG(1, ("talloc failed\n"));
197 state
->locked_dbs
= locked_dbs
;
198 talloc_set_destructor(state
, dbwrap_lock_order_state_destructor
);
200 locked_dbs
[db
->lock_order
- 1] = db
;
202 debug_lock_order(10, locked_dbs
);
207 static struct db_record
*dbwrap_fetch_locked_internal(
208 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, TDB_DATA key
,
209 struct db_record
*(*db_fn
)(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
212 struct db_record
*rec
;
213 struct dbwrap_lock_order_state
*lock_order
= NULL
;
215 if (db
->lock_order
!= DBWRAP_LOCK_ORDER_NONE
) {
216 lock_order
= dbwrap_check_lock_order(db
, mem_ctx
);
217 if (lock_order
== NULL
) {
221 rec
= db_fn(db
, mem_ctx
, key
);
223 TALLOC_FREE(lock_order
);
226 (void)talloc_steal(rec
, lock_order
);
231 struct db_record
*dbwrap_fetch_locked(struct db_context
*db
,
235 return dbwrap_fetch_locked_internal(db
, mem_ctx
, key
,
239 struct db_record
*dbwrap_try_fetch_locked(struct db_context
*db
,
243 return dbwrap_fetch_locked_internal(
246 ? db
->try_fetch_locked
: db
->fetch_locked
);
249 struct db_context
*dbwrap_record_get_db(struct db_record
*rec
)
254 struct dbwrap_fetch_state
{
259 static void dbwrap_fetch_parser(TDB_DATA key
, TDB_DATA data
,
262 struct dbwrap_fetch_state
*state
=
263 (struct dbwrap_fetch_state
*)private_data
;
265 state
->data
.dsize
= data
.dsize
;
266 state
->data
.dptr
= (uint8_t *)talloc_memdup(state
->mem_ctx
, data
.dptr
,
270 NTSTATUS
dbwrap_fetch(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
271 TDB_DATA key
, TDB_DATA
*value
)
273 struct dbwrap_fetch_state state
;
277 return NT_STATUS_INVALID_PARAMETER
;
280 state
.mem_ctx
= mem_ctx
;
282 status
= dbwrap_parse_record(db
, key
, dbwrap_fetch_parser
, &state
);
283 if (!NT_STATUS_IS_OK(status
)) {
286 if ((state
.data
.dsize
!= 0) && (state
.data
.dptr
== NULL
)) {
287 return NT_STATUS_NO_MEMORY
;
293 bool dbwrap_exists(struct db_context
*db
, TDB_DATA key
)
296 if (db
->exists
!= NULL
) {
297 result
= db
->exists(db
, key
);
299 result
= dbwrap_fallback_exists(db
,key
);
301 return (result
== 1);
304 NTSTATUS
dbwrap_store(struct db_context
*db
, TDB_DATA key
,
305 TDB_DATA data
, int flags
)
307 struct db_record
*rec
;
309 TALLOC_CTX
*frame
= talloc_stackframe();
311 rec
= dbwrap_fetch_locked(db
, frame
, key
);
314 return NT_STATUS_NO_MEMORY
;
317 status
= dbwrap_record_store(rec
, data
, flags
);
322 NTSTATUS
dbwrap_delete(struct db_context
*db
, TDB_DATA key
)
324 struct db_record
*rec
;
326 TALLOC_CTX
*frame
= talloc_stackframe();
328 rec
= dbwrap_fetch_locked(db
, frame
, key
);
331 return NT_STATUS_NO_MEMORY
;
333 status
= dbwrap_record_delete(rec
);
338 NTSTATUS
dbwrap_traverse(struct db_context
*db
,
339 int (*f
)(struct db_record
*, void*),
343 int ret
= db
->traverse(db
, f
, private_data
);
346 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
356 NTSTATUS
dbwrap_traverse_read(struct db_context
*db
,
357 int (*f
)(struct db_record
*, void*),
361 int ret
= db
->traverse_read(db
, f
, private_data
);
364 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
374 static void dbwrap_null_parser(TDB_DATA key
, TDB_DATA val
, void* data
)
379 NTSTATUS
dbwrap_parse_record(struct db_context
*db
, TDB_DATA key
,
380 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
384 if (parser
== NULL
) {
385 parser
= dbwrap_null_parser
;
387 return db
->parse_record(db
, key
, parser
, private_data
);
390 int dbwrap_wipe(struct db_context
*db
)
392 if (db
->wipe
== NULL
) {
393 return dbwrap_fallback_wipe(db
);
398 int dbwrap_hash_size(struct db_context
*db
)
400 return db
->hash_size
;
403 int dbwrap_check(struct db_context
*db
)
405 if (db
->check
== NULL
) {
406 return dbwrap_fallback_check(db
);
408 return db
->check(db
);
411 int dbwrap_get_seqnum(struct db_context
*db
)
413 return db
->get_seqnum(db
);
416 int dbwrap_transaction_start(struct db_context
*db
)
418 if (!db
->persistent
) {
420 * dbwrap_ctdb has two different data models for persistent
421 * and non-persistent databases. Transactions are supported
422 * only for the persistent databases. This check is here to
423 * prevent breakages of the cluster case, autobuild at this
424 * point only tests non-clustered Samba. Before removing this
425 * check, please make sure that this facility has also been
426 * added to dbwrap_ctdb.
430 DEBUG(1, ("transactions not supported on non-persistent "
431 "database %s\n", db
->name
));
434 return db
->transaction_start(db
);
437 NTSTATUS
dbwrap_transaction_start_nonblock(struct db_context
*db
)
439 if (db
->transaction_start_nonblock
) {
440 return db
->transaction_start_nonblock(db
);
442 return dbwrap_transaction_start(db
) == 0 ? NT_STATUS_OK
443 : NT_STATUS_UNSUCCESSFUL
;
447 int dbwrap_transaction_commit(struct db_context
*db
)
449 return db
->transaction_commit(db
);
452 int dbwrap_transaction_cancel(struct db_context
*db
)
454 return db
->transaction_cancel(db
);
457 void dbwrap_db_id(struct db_context
*db
, const uint8_t **id
, size_t *idlen
)
459 db
->id(db
, id
, idlen
);
462 bool dbwrap_is_persistent(struct db_context
*db
)
464 return db
->persistent
;
467 const char *dbwrap_name(struct db_context
*db
)