pidl:NDR: keep interface->{ORIGINAL}
[Samba.git] / lib / dbwrap / dbwrap_tdb.c
blobe12ec4405d604a5fe1698f424f4493a403aebd8b
1 /*
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/>.
20 #include "includes.h"
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"
29 struct db_tdb_ctx {
30 struct tdb_wrap *wtdb;
32 struct {
33 dev_t dev;
34 ino_t ino;
35 } id;
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)
43 size_t len;
44 char *keystr;
45 TALLOC_CTX *frame;
46 if (DEBUGLEVEL < 10) {
47 return;
49 frame = talloc_stackframe();
50 len = key.dsize;
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),
58 len);
59 DEBUG(10, ("%s key %s\n", prefix, keystr));
60 TALLOC_FREE(frame);
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);
70 return 0;
73 struct tdb_fetch_locked_state {
74 TALLOC_CTX *mem_ctx;
75 struct db_record *result;
78 static int db_tdb_fetchlock_parse(TDB_DATA key, TDB_DATA data,
79 void *private_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(
86 state->mem_ctx,
87 sizeof(struct db_record) + key.dsize + data.dsize);
89 if (result == NULL) {
90 return 0;
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);
104 else {
105 result->value.dptr = NULL;
108 return 0;
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,
115 struct db_tdb_ctx);
116 struct tdb_fetch_locked_state state;
118 state.mem_ctx = mem_ctx;
119 state.result = NULL;
121 if ((tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_fetchlock_parse,
122 &state) < 0) &&
123 (tdb_error(ctx->wtdb->tdb) != TDB_ERR_NOEXIST)) {
124 tdb_chainunlock(ctx->wtdb->tdb, key);
125 return NULL;
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);
134 return NULL;
137 talloc_set_destructor(state.result, db_tdb_record_destr);
139 state.result->private_data = 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));
145 return 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,
152 struct db_tdb_ctx);
154 db_tdb_log_key("Locking", key);
155 if (tdb_chainlock(ctx->wtdb->tdb, key) != 0) {
156 DEBUG(3, ("tdb_chainlock failed\n"));
157 return NULL;
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,
166 struct db_tdb_ctx);
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"));
171 return NULL;
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,
200 void *private_data);
201 void *private_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);
215 return 0;
218 static NTSTATUS db_tdb_parse(struct db_context *db, TDB_DATA key,
219 void (*parser)(TDB_DATA key, TDB_DATA data,
220 void *private_data),
221 void *private_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;
226 int ret;
228 state.parser = parser;
229 state.private_data = private_data;
231 ret = tdb_parse_record(ctx->wtdb->tdb, key, db_tdb_parser, &state);
233 if (ret != 0) {
234 return map_nt_error_from_tdb(tdb_error(ctx->wtdb->tdb));
236 return NT_STATUS_OK;
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,
242 struct db_tdb_ctx);
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,
257 struct db_tdb_ctx);
259 if (tdb_delete(ctx->wtdb->tdb, rec->key) == 0) {
260 return NT_STATUS_OK;
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);
273 void *private_data;
276 static int db_tdb_traverse_func(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf,
277 void *private_data)
279 struct db_tdb_traverse_ctx *ctx =
280 (struct db_tdb_traverse_ctx *)private_data;
281 struct db_record rec;
283 rec.key = kbuf;
284 rec.value = dbuf;
285 rec.store = db_tdb_store;
286 rec.delete_rec = db_tdb_delete;
287 rec.private_data = ctx->db->private_data;
288 rec.db = ctx->db;
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),
295 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;
301 ctx.db = db;
302 ctx.f = f;
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,
318 void *private_data)
320 struct db_tdb_traverse_ctx *ctx =
321 (struct db_tdb_traverse_ctx *)private_data;
322 struct db_record rec;
324 rec.key = kbuf;
325 rec.value = dbuf;
326 rec.store = db_tdb_store_deny;
327 rec.delete_rec = db_tdb_delete_deny;
328 rec.private_data = ctx->db->private_data;
329 rec.db = ctx->db;
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),
336 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;
342 ctx.db = db;
343 ctx.f = f;
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);
367 int ret;
369 ret = tdb_transaction_start_nonblock(db_ctx->wtdb->tdb);
370 if (ret != 0) {
371 return map_nt_error_from_tdb(tdb_error(db_ctx->wtdb->tdb));
373 return NT_STATUS_OK;
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);
388 return 0;
391 static size_t db_tdb_id(struct db_context *db, 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);
396 if (idlen >= sizeof(db_ctx->id)) {
397 memcpy(id, &db_ctx->id, sizeof(db_ctx->id));
400 return sizeof(db_ctx->id);
403 struct db_context *db_open_tdb(TALLOC_CTX *mem_ctx,
404 const char *name,
405 int hash_size, int tdb_flags,
406 int open_flags, mode_t mode,
407 enum dbwrap_lock_order lock_order,
408 uint64_t dbwrap_flags)
410 struct db_context *result = NULL;
411 struct db_tdb_ctx *db_tdb;
412 struct stat st;
414 result = talloc_zero(mem_ctx, struct db_context);
415 if (result == NULL) {
416 DEBUG(0, ("talloc failed\n"));
417 goto fail;
420 result->private_data = db_tdb = talloc(result, struct db_tdb_ctx);
421 if (db_tdb == NULL) {
422 DEBUG(0, ("talloc failed\n"));
423 goto fail;
425 result->lock_order = lock_order;
427 db_tdb->wtdb = tdb_wrap_open(db_tdb, name, hash_size, tdb_flags,
428 open_flags, mode);
429 if (db_tdb->wtdb == NULL) {
430 DEBUG(3, ("Could not open tdb: %s\n", strerror(errno)));
431 goto fail;
434 ZERO_STRUCT(db_tdb->id);
436 if (fstat(tdb_fd(db_tdb->wtdb->tdb), &st) == -1) {
437 DEBUG(3, ("fstat failed: %s\n", strerror(errno)));
438 goto fail;
440 db_tdb->id.dev = st.st_dev;
441 db_tdb->id.ino = st.st_ino;
443 result->fetch_locked = db_tdb_fetch_locked;
444 result->try_fetch_locked = db_tdb_try_fetch_locked;
445 result->traverse = db_tdb_traverse;
446 result->traverse_read = db_tdb_traverse_read;
447 result->parse_record = db_tdb_parse;
448 result->get_seqnum = db_tdb_get_seqnum;
449 result->persistent = ((tdb_flags & TDB_CLEAR_IF_FIRST) == 0);
450 result->transaction_start = db_tdb_transaction_start;
451 result->transaction_start_nonblock = db_tdb_transaction_start_nonblock;
452 result->transaction_commit = db_tdb_transaction_commit;
453 result->transaction_cancel = db_tdb_transaction_cancel;
454 result->exists = db_tdb_exists;
455 result->wipe = db_tdb_wipe;
456 result->id = db_tdb_id;
457 result->check = db_tdb_check;
458 result->name = tdb_name(db_tdb->wtdb->tdb);
459 return result;
461 fail:
462 TALLOC_FREE(result);
463 return NULL;