2 Unix SMB/CIFS implementation.
3 Database interface wrapper around tdb
4 Copyright (C) Volker Lendecke 2005-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/>.
21 #include "dbwrap/dbwrap.h"
22 #include "dbwrap/dbwrap_private.h"
23 #include "dbwrap/dbwrap_tdb.h"
24 #include "lib/tdb_wrap/tdb_wrap.h"
25 #include "lib/util/util_tdb.h"
26 #include "system/filesys.h"
27 #include "lib/param/param.h"
30 struct tdb_wrap
*wtdb
;
38 static NTSTATUS
db_tdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
);
39 static NTSTATUS
db_tdb_delete(struct db_record
*rec
);
41 static void db_tdb_log_key(const char *prefix
, TDB_DATA key
)
46 if (DEBUGLEVEL
< 10) {
49 frame
= talloc_stackframe();
51 if (DEBUGLEVEL
== 10) {
53 * Only fully spam at debuglevel > 10
55 len
= MIN(10, key
.dsize
);
57 keystr
= hex_encode_talloc(frame
, (unsigned char *)(key
.dptr
),
59 DEBUG(10, ("%s key %s\n", prefix
, keystr
));
63 static int db_tdb_record_destr(struct db_record
* data
)
65 struct db_tdb_ctx
*ctx
=
66 talloc_get_type_abort(data
->private_data
, struct db_tdb_ctx
);
68 db_tdb_log_key("Unlocking", data
->key
);
69 tdb_chainunlock(ctx
->wtdb
->tdb
, data
->key
);
73 struct tdb_fetch_locked_state
{
75 struct db_record
*result
;
78 static int db_tdb_fetchlock_parse(TDB_DATA key
, TDB_DATA data
,
81 struct tdb_fetch_locked_state
*state
=
82 (struct tdb_fetch_locked_state
*)private_data
;
83 struct db_record
*result
;
85 result
= (struct db_record
*)talloc_size(
87 sizeof(struct db_record
) + key
.dsize
+ data
.dsize
);
92 state
->result
= result
;
94 result
->key
.dsize
= key
.dsize
;
95 result
->key
.dptr
= ((uint8_t *)result
) + sizeof(struct db_record
);
96 memcpy(result
->key
.dptr
, key
.dptr
, key
.dsize
);
98 result
->value
.dsize
= data
.dsize
;
100 if (data
.dsize
> 0) {
101 result
->value
.dptr
= result
->key
.dptr
+key
.dsize
;
102 memcpy(result
->value
.dptr
, data
.dptr
, data
.dsize
);
105 result
->value
.dptr
= NULL
;
111 static struct db_record
*db_tdb_fetch_locked_internal(
112 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, TDB_DATA key
)
114 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
116 struct tdb_fetch_locked_state state
;
118 state
.mem_ctx
= mem_ctx
;
121 if ((tdb_parse_record(ctx
->wtdb
->tdb
, key
, db_tdb_fetchlock_parse
,
123 (tdb_error(ctx
->wtdb
->tdb
) != TDB_ERR_NOEXIST
)) {
124 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
128 if (state
.result
== NULL
) {
129 db_tdb_fetchlock_parse(key
, tdb_null
, &state
);
132 if (state
.result
== NULL
) {
133 tdb_chainunlock(ctx
->wtdb
->tdb
, key
);
137 talloc_set_destructor(state
.result
, db_tdb_record_destr
);
139 state
.result
->private_data
= talloc_reference(state
.result
, ctx
);
140 state
.result
->store
= db_tdb_store
;
141 state
.result
->delete_rec
= db_tdb_delete
;
143 DEBUG(10, ("Allocated locked data 0x%p\n", state
.result
));
148 static struct db_record
*db_tdb_fetch_locked(
149 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, TDB_DATA key
)
151 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
154 db_tdb_log_key("Locking", key
);
155 if (tdb_chainlock(ctx
->wtdb
->tdb
, key
) != 0) {
156 DEBUG(3, ("tdb_chainlock failed\n"));
159 return db_tdb_fetch_locked_internal(db
, mem_ctx
, key
);
162 static struct db_record
*db_tdb_try_fetch_locked(
163 struct db_context
*db
, TALLOC_CTX
*mem_ctx
, TDB_DATA key
)
165 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(db
->private_data
,
168 db_tdb_log_key("Trying to lock", key
);
169 if (tdb_chainlock_nonblock(ctx
->wtdb
->tdb
, key
) != 0) {
170 DEBUG(3, ("tdb_chainlock_nonblock failed\n"));
173 return db_tdb_fetch_locked_internal(db
, mem_ctx
, key
);
177 static int db_tdb_exists(struct db_context
*db
, TDB_DATA key
)
179 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(
180 db
->private_data
, struct db_tdb_ctx
);
181 return tdb_exists(ctx
->wtdb
->tdb
, key
);
184 static int db_tdb_wipe(struct db_context
*db
)
186 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(
187 db
->private_data
, struct db_tdb_ctx
);
188 return tdb_wipe_all(ctx
->wtdb
->tdb
);
191 static int db_tdb_check(struct db_context
*db
)
193 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(
194 db
->private_data
, struct db_tdb_ctx
);
195 return tdb_check(ctx
->wtdb
->tdb
, NULL
, NULL
);
198 struct db_tdb_parse_state
{
199 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
205 * tdb_parse_record expects a parser returning int, mixing up tdb and
206 * parser errors. Wrap around that by always returning 0 and have
207 * dbwrap_parse_record expect a parser returning void.
210 static int db_tdb_parser(TDB_DATA key
, TDB_DATA data
, void *private_data
)
212 struct db_tdb_parse_state
*state
=
213 (struct db_tdb_parse_state
*)private_data
;
214 state
->parser(key
, data
, state
->private_data
);
218 static NTSTATUS
db_tdb_parse(struct db_context
*db
, TDB_DATA key
,
219 void (*parser
)(TDB_DATA key
, TDB_DATA data
,
223 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(
224 db
->private_data
, struct db_tdb_ctx
);
225 struct db_tdb_parse_state state
;
228 state
.parser
= parser
;
229 state
.private_data
= private_data
;
231 ret
= tdb_parse_record(ctx
->wtdb
->tdb
, key
, db_tdb_parser
, &state
);
234 return map_nt_error_from_tdb(tdb_error(ctx
->wtdb
->tdb
));
239 static NTSTATUS
db_tdb_store(struct db_record
*rec
, TDB_DATA data
, int flag
)
241 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(rec
->private_data
,
245 * This has a bug: We need to replace rec->value for correct
246 * operation, but right now brlock and locking don't use the value
247 * anymore after it was stored.
250 return (tdb_store(ctx
->wtdb
->tdb
, rec
->key
, data
, flag
) == 0) ?
251 NT_STATUS_OK
: NT_STATUS_UNSUCCESSFUL
;
254 static NTSTATUS
db_tdb_delete(struct db_record
*rec
)
256 struct db_tdb_ctx
*ctx
= talloc_get_type_abort(rec
->private_data
,
259 if (tdb_delete(ctx
->wtdb
->tdb
, rec
->key
) == 0) {
263 if (tdb_error(ctx
->wtdb
->tdb
) == TDB_ERR_NOEXIST
) {
264 return NT_STATUS_NOT_FOUND
;
267 return NT_STATUS_UNSUCCESSFUL
;
270 struct db_tdb_traverse_ctx
{
271 struct db_context
*db
;
272 int (*f
)(struct db_record
*rec
, void *private_data
);
276 static int db_tdb_traverse_func(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
279 struct db_tdb_traverse_ctx
*ctx
=
280 (struct db_tdb_traverse_ctx
*)private_data
;
281 struct db_record rec
;
285 rec
.store
= db_tdb_store
;
286 rec
.delete_rec
= db_tdb_delete
;
287 rec
.private_data
= ctx
->db
->private_data
;
290 return ctx
->f(&rec
, ctx
->private_data
);
293 static int db_tdb_traverse(struct db_context
*db
,
294 int (*f
)(struct db_record
*rec
, void *private_data
),
297 struct db_tdb_ctx
*db_ctx
=
298 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
299 struct db_tdb_traverse_ctx ctx
;
303 ctx
.private_data
= private_data
;
304 return tdb_traverse(db_ctx
->wtdb
->tdb
, db_tdb_traverse_func
, &ctx
);
307 static NTSTATUS
db_tdb_store_deny(struct db_record
*rec
, TDB_DATA data
, int flag
)
309 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
312 static NTSTATUS
db_tdb_delete_deny(struct db_record
*rec
)
314 return NT_STATUS_MEDIA_WRITE_PROTECTED
;
317 static int db_tdb_traverse_read_func(TDB_CONTEXT
*tdb
, TDB_DATA kbuf
, TDB_DATA dbuf
,
320 struct db_tdb_traverse_ctx
*ctx
=
321 (struct db_tdb_traverse_ctx
*)private_data
;
322 struct db_record rec
;
326 rec
.store
= db_tdb_store_deny
;
327 rec
.delete_rec
= db_tdb_delete_deny
;
328 rec
.private_data
= ctx
->db
->private_data
;
331 return ctx
->f(&rec
, ctx
->private_data
);
334 static int db_tdb_traverse_read(struct db_context
*db
,
335 int (*f
)(struct db_record
*rec
, void *private_data
),
338 struct db_tdb_ctx
*db_ctx
=
339 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
340 struct db_tdb_traverse_ctx ctx
;
344 ctx
.private_data
= private_data
;
345 return tdb_traverse_read(db_ctx
->wtdb
->tdb
, db_tdb_traverse_read_func
, &ctx
);
348 static int db_tdb_get_seqnum(struct db_context
*db
)
351 struct db_tdb_ctx
*db_ctx
=
352 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
353 return tdb_get_seqnum(db_ctx
->wtdb
->tdb
);
356 static int db_tdb_transaction_start(struct db_context
*db
)
358 struct db_tdb_ctx
*db_ctx
=
359 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
360 return tdb_transaction_start(db_ctx
->wtdb
->tdb
) ? -1 : 0;
363 static NTSTATUS
db_tdb_transaction_start_nonblock(struct db_context
*db
)
365 struct db_tdb_ctx
*db_ctx
=
366 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
369 ret
= tdb_transaction_start_nonblock(db_ctx
->wtdb
->tdb
);
371 return map_nt_error_from_tdb(tdb_error(db_ctx
->wtdb
->tdb
));
376 static int db_tdb_transaction_commit(struct db_context
*db
)
378 struct db_tdb_ctx
*db_ctx
=
379 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
380 return tdb_transaction_commit(db_ctx
->wtdb
->tdb
) ? -1 : 0;
383 static int db_tdb_transaction_cancel(struct db_context
*db
)
385 struct db_tdb_ctx
*db_ctx
=
386 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
387 tdb_transaction_cancel(db_ctx
->wtdb
->tdb
);
391 static void db_tdb_id(struct db_context
*db
, const uint8_t **id
, size_t *idlen
)
393 struct db_tdb_ctx
*db_ctx
=
394 talloc_get_type_abort(db
->private_data
, struct db_tdb_ctx
);
395 *id
= (uint8_t *)&db_ctx
->id
;
396 *idlen
= sizeof(db_ctx
->id
);
399 struct db_context
*db_open_tdb(TALLOC_CTX
*mem_ctx
,
400 struct loadparm_context
*lp_ctx
,
402 int hash_size
, int tdb_flags
,
403 int open_flags
, mode_t mode
,
404 enum dbwrap_lock_order lock_order
,
405 uint64_t dbrwap_flags
)
407 struct db_context
*result
= NULL
;
408 struct db_tdb_ctx
*db_tdb
;
411 result
= talloc_zero(mem_ctx
, struct db_context
);
412 if (result
== NULL
) {
413 DEBUG(0, ("talloc failed\n"));
417 result
->private_data
= db_tdb
= talloc(result
, struct db_tdb_ctx
);
418 if (db_tdb
== NULL
) {
419 DEBUG(0, ("talloc failed\n"));
422 result
->lock_order
= lock_order
;
424 if (hash_size
== 0) {
425 hash_size
= lpcfg_tdb_hash_size(lp_ctx
, name
);
428 db_tdb
->wtdb
= tdb_wrap_open(db_tdb
, name
, hash_size
,
429 lpcfg_tdb_flags(lp_ctx
, tdb_flags
),
431 if (db_tdb
->wtdb
== NULL
) {
432 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno
)));
436 ZERO_STRUCT(db_tdb
->id
);
438 if (fstat(tdb_fd(db_tdb
->wtdb
->tdb
), &st
) == -1) {
439 DEBUG(3, ("fstat failed: %s\n", strerror(errno
)));
442 db_tdb
->id
.dev
= st
.st_dev
;
443 db_tdb
->id
.ino
= st
.st_ino
;
445 result
->fetch_locked
= db_tdb_fetch_locked
;
446 result
->try_fetch_locked
= db_tdb_try_fetch_locked
;
447 result
->traverse
= db_tdb_traverse
;
448 result
->traverse_read
= db_tdb_traverse_read
;
449 result
->parse_record
= db_tdb_parse
;
450 result
->get_seqnum
= db_tdb_get_seqnum
;
451 result
->persistent
= ((tdb_flags
& TDB_CLEAR_IF_FIRST
) == 0);
452 result
->transaction_start
= db_tdb_transaction_start
;
453 result
->transaction_start_nonblock
= db_tdb_transaction_start_nonblock
;
454 result
->transaction_commit
= db_tdb_transaction_commit
;
455 result
->transaction_cancel
= db_tdb_transaction_cancel
;
456 result
->exists
= db_tdb_exists
;
457 result
->wipe
= db_tdb_wipe
;
458 result
->id
= db_tdb_id
;
459 result
->check
= db_tdb_check
;
460 result
->name
= tdb_name(db_tdb
->wtdb
->tdb
);
461 result
->hash_size
= hash_size
;