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"
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;
55 * Wrapper functions for the backend methods
58 TDB_DATA
dbwrap_record_get_key(const struct db_record
*rec
)
63 TDB_DATA
dbwrap_record_get_value(const struct db_record
*rec
)
68 NTSTATUS
dbwrap_record_store(struct db_record
*rec
, TDB_DATA data
, int flags
)
71 struct db_context
*db
;
73 status
= rec
->store(rec
, data
, flags
);
74 if (!NT_STATUS_IS_OK(status
)) {
78 if (db
->stored_callback
!= NULL
) {
79 db
->stored_callback(db
, rec
,
80 db
->stored_callback_private_data
);
85 void dbwrap_set_stored_callback(
86 struct db_context
*db
,
87 void (*cb
)(struct db_context
*db
, struct db_record
*rec
,
91 db
->stored_callback
= cb
;
92 db
->stored_callback_private_data
= private_data
;
95 NTSTATUS
dbwrap_record_delete(struct db_record
*rec
)
98 struct db_context
*db
;
100 status
= rec
->delete_rec(rec
);
101 if (!NT_STATUS_IS_OK(status
)) {
105 if (db
->stored_callback
!= NULL
) {
106 db
->stored_callback(db
, rec
,
107 db
->stored_callback_private_data
);
112 struct dbwrap_lock_order_state
{
113 uint8_t *plock_order_mask
;
117 static int dbwrap_lock_order_state_destructor(
118 struct dbwrap_lock_order_state
*s
)
120 *s
->plock_order_mask
&= ~s
->bitmask
;
124 static struct dbwrap_lock_order_state
*dbwrap_check_lock_order(
125 struct db_context
*db
, TALLOC_CTX
*mem_ctx
)
128 * Store the lock_order of currently locked records as bits in
129 * "lock_order_mask". We only use levels 1,2,3 right now, so a
130 * single uint8_t is enough.
132 static uint8_t lock_order_mask
;
134 struct dbwrap_lock_order_state
*state
;
138 if (db
->lock_order
== 0) {
140 * lock order 0 is for example for dbwrap_rbt without
141 * real locking. Return state nevertheless to avoid
144 return talloc(mem_ctx
, struct dbwrap_lock_order_state
);
148 * We fill bits from the high bits, to be able to use
149 * "ffs(lock_order_mask)"
151 idx
= sizeof(lock_order_mask
)*8 - db
->lock_order
;
153 used
= ffs(lock_order_mask
);
155 DEBUG(5, ("used=%d, lock_order=%d, idx=%d\n", used
,
156 (int)db
->lock_order
, (int)idx
));
158 if ((used
!= 0) && (used
-1 <= idx
)) {
159 DEBUG(0, ("Lock order violation: Trying %d, order_mask=%x\n",
160 (int)db
->lock_order
, (int)lock_order_mask
));
164 state
= talloc(mem_ctx
, struct dbwrap_lock_order_state
);
166 DEBUG(1, ("talloc failed\n"));
169 state
->bitmask
= 1 << idx
;
170 state
->plock_order_mask
= &lock_order_mask
;
172 talloc_set_destructor(state
, dbwrap_lock_order_state_destructor
);
173 lock_order_mask
|= state
->bitmask
;
178 static struct db_record
*dbwrap_fetch_locked_internal(
179 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, TDB_DATA key
,
180 struct db_record
*(*db_fn
)(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
183 struct db_record
*rec
;
184 struct dbwrap_lock_order_state
*lock_order
;
185 TALLOC_CTX
*frame
= talloc_stackframe();
187 lock_order
= dbwrap_check_lock_order(db
, frame
);
188 if (lock_order
== NULL
) {
192 rec
= db_fn(db
, mem_ctx
, key
);
197 (void)talloc_steal(rec
, lock_order
);
203 struct db_record
*dbwrap_fetch_locked(struct db_context
*db
,
207 return dbwrap_fetch_locked_internal(db
, mem_ctx
, key
,
211 struct db_record
*dbwrap_try_fetch_locked(struct db_context
*db
,
215 return dbwrap_fetch_locked_internal(
218 ? db
->try_fetch_locked
: db
->fetch_locked
);
221 struct db_context
*dbwrap_record_get_db(struct db_record
*rec
)
226 struct dbwrap_fetch_state
{
231 static void dbwrap_fetch_parser(TDB_DATA key
, TDB_DATA data
,
234 struct dbwrap_fetch_state
*state
=
235 (struct dbwrap_fetch_state
*)private_data
;
237 state
->data
.dsize
= data
.dsize
;
238 state
->data
.dptr
= (uint8_t *)talloc_memdup(state
->mem_ctx
, data
.dptr
,
242 NTSTATUS
dbwrap_fetch(struct db_context
*db
, TALLOC_CTX
*mem_ctx
,
243 TDB_DATA key
, TDB_DATA
*value
)
245 struct dbwrap_fetch_state state
;
249 return NT_STATUS_INVALID_PARAMETER
;
252 state
.mem_ctx
= mem_ctx
;
254 status
= dbwrap_parse_record(db
, key
, dbwrap_fetch_parser
, &state
);
255 if (!NT_STATUS_IS_OK(status
)) {
258 if ((state
.data
.dsize
!= 0) && (state
.data
.dptr
== NULL
)) {
259 return NT_STATUS_NO_MEMORY
;
265 bool dbwrap_exists(struct db_context
*db
, TDB_DATA key
)
268 if (db
->exists
!= NULL
) {
269 result
= db
->exists(db
, key
);
271 result
= dbwrap_fallback_exists(db
,key
);
273 return (result
== 1);
276 NTSTATUS
dbwrap_store(struct db_context
*db
, TDB_DATA key
,
277 TDB_DATA data
, int flags
)
279 struct db_record
*rec
;
281 TALLOC_CTX
*frame
= talloc_stackframe();
283 rec
= dbwrap_fetch_locked(db
, frame
, key
);
286 return NT_STATUS_NO_MEMORY
;
289 status
= dbwrap_record_store(rec
, data
, flags
);
294 NTSTATUS
dbwrap_delete(struct db_context
*db
, TDB_DATA key
)
296 struct db_record
*rec
;
298 TALLOC_CTX
*frame
= talloc_stackframe();
300 rec
= dbwrap_fetch_locked(db
, frame
, key
);
303 return NT_STATUS_NO_MEMORY
;
305 status
= dbwrap_record_delete(rec
);
310 NTSTATUS
dbwrap_traverse(struct db_context
*db
,
311 int (*f
)(struct db_record
*, void*),
315 int ret
= db
->traverse(db
, f
, private_data
);
318 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
328 NTSTATUS
dbwrap_traverse_read(struct db_context
*db
,
329 int (*f
)(struct db_record
*, void*),
333 int ret
= db
->traverse_read(db
, f
, private_data
);
336 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
346 static void dbwrap_null_parser(TDB_DATA key
, TDB_DATA val
, void* data
)
351 NTSTATUS
dbwrap_parse_record(struct db_context
*db
, TDB_DATA key
,
352 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
356 if (parser
== NULL
) {
357 parser
= dbwrap_null_parser
;
359 return db
->parse_record(db
, key
, parser
, private_data
);
362 int dbwrap_wipe(struct db_context
*db
)
364 if (db
->wipe
== NULL
) {
365 return dbwrap_fallback_wipe(db
);
370 int dbwrap_get_seqnum(struct db_context
*db
)
372 return db
->get_seqnum(db
);
375 int dbwrap_get_flags(struct db_context
*db
)
377 return db
->get_flags(db
);
380 int dbwrap_transaction_start(struct db_context
*db
)
382 return db
->transaction_start(db
);
385 int dbwrap_transaction_commit(struct db_context
*db
)
387 return db
->transaction_commit(db
);
390 int dbwrap_transaction_cancel(struct db_context
*db
)
392 return db
->transaction_cancel(db
);
395 void dbwrap_db_id(struct db_context
*db
, const uint8_t **id
, size_t *idlen
)
397 db
->id(db
, id
, idlen
);