4 Copyright (C) Andrew Tridgell 2004
5 Copyright (C) Stefan Metzmacher 2004
6 Copyright (C) Simo Sorce 2006-2008
7 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
9 ** NOTE! The following LGPL license applies to the ldb
10 ** library. This does NOT imply that all of Samba is released
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 3 of the License, or (at your option) any later version.
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 * Component: ldb tdb backend
32 * Description: core functions for tdb backend
34 * Author: Andrew Tridgell
35 * Author: Stefan Metzmacher
39 * - description: make the module use asynchronous calls
43 * - description: make it possible to use event contexts
47 * - description: fix up memory leaks and small bugs
49 * Author: Matthias Dieter Wallnöfer
53 #include "ldb_private.h"
54 #include "../ldb_key_value/ldb_kv.h"
58 lock the database for read - use by ltdb_search and ltdb_sequence_number
60 static int ltdb_lock_read(struct ldb_module
*module
)
62 void *data
= ldb_module_get_private(module
);
63 struct ldb_kv_private
*ldb_kv
=
64 talloc_get_type(data
, struct ldb_kv_private
);
69 if (ldb_kv
->pid
!= pid
) {
70 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
72 ": Reusing ldb opened by pid %d in "
76 return LDB_ERR_PROTOCOL_ERROR
;
79 if (tdb_transaction_active(ldb_kv
->tdb
) == false &&
80 ldb_kv
->read_lock_count
== 0) {
81 tdb_ret
= tdb_lockall_read(ldb_kv
->tdb
);
84 ldb_kv
->read_lock_count
++;
87 ret
= ltdb_err_map(tdb_error(ldb_kv
->tdb
));
88 if (ret
== LDB_SUCCESS
) {
89 ret
= LDB_ERR_OPERATIONS_ERROR
;
91 ldb_debug_set(ldb_module_get_ctx(module
),
93 "Failure during ltdb_lock_read(): %s -> %s",
94 tdb_errorstr(ldb_kv
->tdb
),
100 unlock the database after a ltdb_lock_read()
102 static int ltdb_unlock_read(struct ldb_module
*module
)
104 void *data
= ldb_module_get_private(module
);
105 struct ldb_kv_private
*ldb_kv
=
106 talloc_get_type(data
, struct ldb_kv_private
);
107 pid_t pid
= getpid();
109 if (ldb_kv
->pid
!= pid
) {
110 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
112 ": Reusing ldb opened by pid %d in "
116 return LDB_ERR_PROTOCOL_ERROR
;
118 if (!tdb_transaction_active(ldb_kv
->tdb
) &&
119 ldb_kv
->read_lock_count
== 1) {
120 tdb_unlockall_read(ldb_kv
->tdb
);
121 ldb_kv
->read_lock_count
--;
124 ldb_kv
->read_lock_count
--;
128 static int ltdb_store(struct ldb_kv_private
*ldb_kv
,
129 struct ldb_val ldb_key
,
130 struct ldb_val ldb_data
,
134 .dptr
= ldb_key
.data
,
135 .dsize
= ldb_key
.length
138 .dptr
= ldb_data
.data
,
139 .dsize
= ldb_data
.length
141 bool transaction_active
= tdb_transaction_active(ldb_kv
->tdb
);
142 if (transaction_active
== false){
143 return LDB_ERR_PROTOCOL_ERROR
;
145 return tdb_store(ldb_kv
->tdb
, key
, data
, flags
);
148 static int ltdb_error(struct ldb_kv_private
*ldb_kv
)
150 return ltdb_err_map(tdb_error(ldb_kv
->tdb
));
153 static const char *ltdb_errorstr(struct ldb_kv_private
*ldb_kv
)
155 return tdb_errorstr(ldb_kv
->tdb
);
158 static int ltdb_delete(struct ldb_kv_private
*ldb_kv
, struct ldb_val ldb_key
)
161 .dptr
= ldb_key
.data
,
162 .dsize
= ldb_key
.length
164 bool transaction_active
= tdb_transaction_active(ldb_kv
->tdb
);
165 if (transaction_active
== false){
166 return LDB_ERR_PROTOCOL_ERROR
;
168 return tdb_delete(ldb_kv
->tdb
, tdb_key
);
171 static int ltdb_transaction_start(struct ldb_kv_private
*ldb_kv
)
173 pid_t pid
= getpid();
175 if (ldb_kv
->pid
!= pid
) {
176 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv
->module
),
178 ": Reusing ldb opened by pid %d in "
182 return LDB_ERR_PROTOCOL_ERROR
;
185 return tdb_transaction_start(ldb_kv
->tdb
);
188 static int ltdb_transaction_cancel(struct ldb_kv_private
*ldb_kv
)
190 pid_t pid
= getpid();
192 if (ldb_kv
->pid
!= pid
) {
193 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv
->module
),
195 ": Reusing ldb opened by pid %d in "
199 return LDB_ERR_PROTOCOL_ERROR
;
202 return tdb_transaction_cancel(ldb_kv
->tdb
);
205 static int ltdb_transaction_prepare_commit(struct ldb_kv_private
*ldb_kv
)
207 pid_t pid
= getpid();
209 if (ldb_kv
->pid
!= pid
) {
210 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv
->module
),
212 ": Reusing ldb opened by pid %d in "
216 return LDB_ERR_PROTOCOL_ERROR
;
219 return tdb_transaction_prepare_commit(ldb_kv
->tdb
);
222 static int ltdb_transaction_commit(struct ldb_kv_private
*ldb_kv
)
224 pid_t pid
= getpid();
226 if (ldb_kv
->pid
!= pid
) {
227 ldb_asprintf_errstring(ldb_module_get_ctx(ldb_kv
->module
),
229 ": Reusing ldb opened by pid %d in "
233 return LDB_ERR_PROTOCOL_ERROR
;
236 return tdb_transaction_commit(ldb_kv
->tdb
);
239 ldb_kv_traverse_fn kv_traverse_fn
;
241 struct ldb_kv_private
*ldb_kv
;
242 int (*parser
)(struct ldb_val key
,
248 static int ltdb_traverse_fn_wrapper(struct tdb_context
*tdb
,
253 struct kv_ctx
*kv_ctx
= ctx
;
254 struct ldb_val key
= {
255 .length
= tdb_key
.dsize
,
256 .data
= tdb_key
.dptr
,
258 struct ldb_val data
= {
259 .length
= tdb_data
.dsize
,
260 .data
= tdb_data
.dptr
,
262 return kv_ctx
->kv_traverse_fn(kv_ctx
->ldb_kv
, key
, data
, kv_ctx
->ctx
);
265 static int ltdb_traverse_fn(struct ldb_kv_private
*ldb_kv
,
266 ldb_kv_traverse_fn fn
,
269 struct kv_ctx kv_ctx
= {
270 .kv_traverse_fn
= fn
, .ctx
= ctx
, .ldb_kv
= ldb_kv
};
271 if (tdb_transaction_active(ldb_kv
->tdb
)) {
273 ldb_kv
->tdb
, ltdb_traverse_fn_wrapper
, &kv_ctx
);
275 return tdb_traverse_read(
276 ldb_kv
->tdb
, ltdb_traverse_fn_wrapper
, &kv_ctx
);
280 static int ltdb_update_in_iterate(struct ldb_kv_private
*ldb_kv
,
281 struct ldb_val ldb_key
,
282 struct ldb_val ldb_key2
,
283 struct ldb_val ldb_data
,
287 struct ldb_context
*ldb
;
288 struct ldb_kv_reindex_context
*ctx
=
289 (struct ldb_kv_reindex_context
*)state
;
290 struct ldb_module
*module
= ldb_kv
->module
;
292 .dptr
= ldb_key
.data
,
293 .dsize
= ldb_key
.length
296 .dptr
= ldb_key2
.data
,
297 .dsize
= ldb_key2
.length
300 .dptr
= ldb_data
.data
,
301 .dsize
= ldb_data
.length
304 ldb
= ldb_module_get_ctx(module
);
306 tdb_ret
= tdb_delete(ldb_kv
->tdb
, key
);
310 "Failed to delete %*.*s "
311 "for rekey as %*.*s: %s",
314 (const char *)key
.dptr
,
317 (const char *)key
.dptr
,
318 tdb_errorstr(ldb_kv
->tdb
));
319 ctx
->error
= ltdb_err_map(tdb_error(ldb_kv
->tdb
));
322 tdb_ret
= tdb_store(ldb_kv
->tdb
, key2
, data
, 0);
326 "Failed to rekey %*.*s as %*.*s: %s",
329 (const char *)key
.dptr
,
332 (const char *)key
.dptr
,
333 tdb_errorstr(ldb_kv
->tdb
));
334 ctx
->error
= ltdb_err_map(tdb_error(ldb_kv
->tdb
));
340 static int ltdb_parse_record_wrapper(TDB_DATA tdb_key
,
344 struct kv_ctx
*kv_ctx
= ctx
;
345 struct ldb_val key
= {
346 .length
= tdb_key
.dsize
,
347 .data
= tdb_key
.dptr
,
349 struct ldb_val data
= {
350 .length
= tdb_data
.dsize
,
351 .data
= tdb_data
.dptr
,
354 kv_ctx
->parser_ret
= kv_ctx
->parser(key
, data
, kv_ctx
->ctx
);
355 return kv_ctx
->parser_ret
;
358 static int ltdb_parse_record(struct ldb_kv_private
*ldb_kv
,
359 struct ldb_val ldb_key
,
360 int (*parser
)(struct ldb_val key
,
365 struct kv_ctx kv_ctx
= {.parser
= parser
, .ctx
= ctx
, .ldb_kv
= ldb_kv
};
367 .dptr
= ldb_key
.data
,
368 .dsize
= ldb_key
.length
372 if (tdb_transaction_active(ldb_kv
->tdb
) == false &&
373 ldb_kv
->read_lock_count
== 0) {
374 return LDB_ERR_PROTOCOL_ERROR
;
377 ret
= tdb_parse_record(
378 ldb_kv
->tdb
, key
, ltdb_parse_record_wrapper
, &kv_ctx
);
379 if (kv_ctx
.parser_ret
!= LDB_SUCCESS
) {
380 return kv_ctx
.parser_ret
;
381 } else if (ret
== 0) {
384 return ltdb_err_map(tdb_error(ldb_kv
->tdb
));
387 static int ltdb_iterate_range(struct ldb_kv_private
*ldb_kv
,
388 struct ldb_val start_key
,
389 struct ldb_val end_key
,
390 ldb_kv_traverse_fn fn
,
394 * We do not implement this operation because we do not know how to
395 * iterate from one key to the next (in a sorted fashion).
397 * We could mimic it potentially, but it would violate boundaries of
398 * knowledge (data type representation).
400 return LDB_ERR_OPERATIONS_ERROR
;
403 static const char *ltdb_name(struct ldb_kv_private
*ldb_kv
)
405 return tdb_name(ldb_kv
->tdb
);
408 static bool ltdb_changed(struct ldb_kv_private
*ldb_kv
)
410 int seq
= tdb_get_seqnum(ldb_kv
->tdb
);
411 bool has_changed
= (seq
!= ldb_kv
->tdb_seqnum
);
413 ldb_kv
->tdb_seqnum
= seq
;
418 static bool ltdb_transaction_active(struct ldb_kv_private
*ldb_kv
)
420 return tdb_transaction_active(ldb_kv
->tdb
);
424 * Get an estimate of the number of records in a tdb database.
426 * This implementation will overestimate the number of records in a sparsely
427 * populated database. The size estimate is only used for allocating
428 * an in memory tdb to cache index records during a reindex, overestimating
429 * the contents is acceptable, and preferable to underestimating
431 #define RECORD_SIZE 500
432 static size_t ltdb_get_size(struct ldb_kv_private
*ldb_kv
)
434 size_t map_size
= tdb_map_size(ldb_kv
->tdb
);
435 size_t size
= map_size
/ RECORD_SIZE
;
441 * Start a sub transaction
442 * As TDB does not currently support nested transactions, we do nothing and
445 static int ltdb_nested_transaction_start(struct ldb_kv_private
*ldb_kv
)
451 * Commit a sub transaction
452 * As TDB does not currently support nested transactions, we do nothing and
455 static int ltdb_nested_transaction_commit(struct ldb_kv_private
*ldb_kv
)
461 * Cancel a sub transaction
462 * As TDB does not currently support nested transactions, we do nothing and
465 static int ltdb_nested_transaction_cancel(struct ldb_kv_private
*ldb_kv
)
470 static const struct kv_db_ops key_value_ops
= {
471 /* No support for any additional features */
475 .delete = ltdb_delete
,
476 .iterate
= ltdb_traverse_fn
,
477 .update_in_iterate
= ltdb_update_in_iterate
,
478 .fetch_and_parse
= ltdb_parse_record
,
479 .iterate_range
= ltdb_iterate_range
,
480 .lock_read
= ltdb_lock_read
,
481 .unlock_read
= ltdb_unlock_read
,
482 .begin_write
= ltdb_transaction_start
,
483 .prepare_write
= ltdb_transaction_prepare_commit
,
484 .finish_write
= ltdb_transaction_commit
,
485 .abort_write
= ltdb_transaction_cancel
,
487 .errorstr
= ltdb_errorstr
,
489 .has_changed
= ltdb_changed
,
490 .transaction_active
= ltdb_transaction_active
,
491 .get_size
= ltdb_get_size
,
492 .begin_nested_write
= ltdb_nested_transaction_start
,
493 .finish_nested_write
= ltdb_nested_transaction_commit
,
494 .abort_nested_write
= ltdb_nested_transaction_cancel
,
498 connect to the database
500 int ltdb_connect(struct ldb_context
*ldb
, const char *url
,
501 unsigned int flags
, const char *options
[],
502 struct ldb_module
**_module
)
505 int tdb_flags
, open_flags
;
506 struct ldb_kv_private
*ldb_kv
;
509 * We hold locks, so we must use a private event context
510 * on each returned handle
512 ldb_set_require_private_event_context(ldb
);
515 if (strchr(url
, ':')) {
516 if (strncmp(url
, "tdb://", 6) != 0) {
517 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
518 "Invalid tdb URL '%s'", url
);
519 return LDB_ERR_OPERATIONS_ERROR
;
526 tdb_flags
= TDB_DEFAULT
| TDB_SEQNUM
| TDB_DISALLOW_NESTING
;
528 /* check for the 'nosync' option */
529 if (flags
& LDB_FLG_NOSYNC
) {
530 tdb_flags
|= TDB_NOSYNC
;
533 /* and nommap option */
534 if (flags
& LDB_FLG_NOMMAP
) {
535 tdb_flags
|= TDB_NOMMAP
;
538 ldb_kv
= talloc_zero(ldb
, struct ldb_kv_private
);
541 return LDB_ERR_OPERATIONS_ERROR
;
544 if (flags
& LDB_FLG_RDONLY
) {
546 * This is weird, but because we can only have one tdb
547 * in this process, and the other one could be
548 * read-write, we can't use the tdb readonly. Plus a
549 * read only tdb prohibits the all-record lock.
553 ldb_kv
->read_only
= true;
555 } else if (flags
& LDB_FLG_DONT_CREATE_DB
) {
557 * This is used by ldbsearch to prevent creation of the database
558 * if the name is wrong
563 * This is the normal case
565 open_flags
= O_CREAT
| O_RDWR
;
568 ldb_kv
->kv_ops
= &key_value_ops
;
571 /* note that we use quite a large default hash size */
572 ldb_kv
->tdb
= ltdb_wrap_open(ldb_kv
,
577 ldb_get_create_perms(ldb
),
580 ldb_asprintf_errstring(ldb
,
581 "Unable to open tdb '%s': %s", path
, strerror(errno
));
582 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
583 "Unable to open tdb '%s': %s", path
, strerror(errno
));
585 if (errno
== EACCES
|| errno
== EPERM
) {
586 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
588 return LDB_ERR_OPERATIONS_ERROR
;
591 return ldb_kv_init_store(
592 ldb_kv
, "ldb_tdb backend", ldb
, options
, _module
);