2 Trivial Database 2: fetch, store and misc routines.
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #ifndef HAVE_LIBREPLACE
20 #include <ccan/asprintf/asprintf.h>
24 static enum NTDB_ERROR update_rec_hdr(struct ntdb_context *ntdb,
28 struct ntdb_used_record *rec)
30 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
31 enum NTDB_ERROR ecode;
33 ecode = set_header(ntdb, rec, NTDB_USED_MAGIC, keylen, datalen,
35 if (ecode == NTDB_SUCCESS) {
36 ecode = ntdb_write_convert(ntdb, off, rec, sizeof(*rec));
41 static enum NTDB_ERROR replace_data(struct ntdb_context *ntdb,
43 NTDB_DATA key, NTDB_DATA dbuf,
44 ntdb_off_t old_off, ntdb_len_t old_room,
48 enum NTDB_ERROR ecode;
50 /* Allocate a new record. */
51 new_off = alloc(ntdb, key.dsize, dbuf.dsize, NTDB_USED_MAGIC, growing);
52 if (NTDB_OFF_IS_ERR(new_off)) {
53 return NTDB_OFF_TO_ERR(new_off);
56 /* We didn't like the existing one: remove it. */
59 ecode = add_free_record(ntdb, old_off,
60 sizeof(struct ntdb_used_record)
61 + key.dsize + old_room,
62 NTDB_LOCK_WAIT, true);
63 if (ecode == NTDB_SUCCESS)
64 ecode = replace_in_hash(ntdb, h, new_off);
66 ecode = add_to_hash(ntdb, h, new_off);
68 if (ecode != NTDB_SUCCESS) {
72 new_off += sizeof(struct ntdb_used_record);
73 ecode = ntdb->io->twrite(ntdb, new_off, key.dptr, key.dsize);
74 if (ecode != NTDB_SUCCESS) {
79 ecode = ntdb->io->twrite(ntdb, new_off, dbuf.dptr, dbuf.dsize);
80 if (ecode != NTDB_SUCCESS) {
84 if (ntdb->flags & NTDB_SEQNUM)
85 ntdb_inc_seqnum(ntdb);
90 static enum NTDB_ERROR update_data(struct ntdb_context *ntdb,
95 enum NTDB_ERROR ecode;
97 ecode = ntdb->io->twrite(ntdb, off, dbuf.dptr, dbuf.dsize);
98 if (ecode == NTDB_SUCCESS && extra) {
99 /* Put a zero in; future versions may append other data. */
100 ecode = ntdb->io->twrite(ntdb, off + dbuf.dsize, "", 1);
102 if (ntdb->flags & NTDB_SEQNUM)
103 ntdb_inc_seqnum(ntdb);
108 _PUBLIC_ enum NTDB_ERROR ntdb_store(struct ntdb_context *ntdb,
109 NTDB_DATA key, NTDB_DATA dbuf, int flag)
113 ntdb_len_t old_room = 0;
114 struct ntdb_used_record rec;
115 enum NTDB_ERROR ecode;
117 off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec);
118 if (NTDB_OFF_IS_ERR(off)) {
119 return NTDB_OFF_TO_ERR(off);
122 /* Now we have lock on this hash bucket. */
123 if (flag == NTDB_INSERT) {
125 ecode = NTDB_ERR_EXISTS;
130 old_room = rec_data_length(&rec)
131 + rec_extra_padding(&rec);
132 if (old_room >= dbuf.dsize) {
133 /* Can modify in-place. Easy! */
134 ecode = update_rec_hdr(ntdb, off,
135 key.dsize, dbuf.dsize,
137 if (ecode != NTDB_SUCCESS) {
140 ecode = update_data(ntdb,
143 old_room - dbuf.dsize);
144 if (ecode != NTDB_SUCCESS) {
147 ntdb_unlock_hash(ntdb, h.h, F_WRLCK);
151 if (flag == NTDB_MODIFY) {
152 /* if the record doesn't exist and we
153 are in NTDB_MODIFY mode then we should fail
155 ecode = NTDB_ERR_NOEXIST;
161 /* If we didn't use the old record, this implies we're growing. */
162 ecode = replace_data(ntdb, &h, key, dbuf, off, old_room, off);
164 ntdb_unlock_hash(ntdb, h.h, F_WRLCK);
168 _PUBLIC_ enum NTDB_ERROR ntdb_append(struct ntdb_context *ntdb,
169 NTDB_DATA key, NTDB_DATA dbuf)
173 struct ntdb_used_record rec;
174 ntdb_len_t old_room = 0, old_dlen;
175 unsigned char *newdata;
177 enum NTDB_ERROR ecode;
179 off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec);
180 if (NTDB_OFF_IS_ERR(off)) {
181 return NTDB_OFF_TO_ERR(off);
185 old_dlen = rec_data_length(&rec);
186 old_room = old_dlen + rec_extra_padding(&rec);
188 /* Fast path: can append in place. */
189 if (rec_extra_padding(&rec) >= dbuf.dsize) {
190 ecode = update_rec_hdr(ntdb, off, key.dsize,
191 old_dlen + dbuf.dsize, &rec);
192 if (ecode != NTDB_SUCCESS) {
196 off += sizeof(rec) + key.dsize + old_dlen;
197 ecode = update_data(ntdb, off, dbuf,
198 rec_extra_padding(&rec));
203 newdata = ntdb->alloc_fn(ntdb, key.dsize + old_dlen + dbuf.dsize,
206 ecode = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
208 " failed to allocate %zu bytes",
209 (size_t)(key.dsize + old_dlen
213 ecode = ntdb->io->tread(ntdb, off + sizeof(rec) + key.dsize,
215 if (ecode != NTDB_SUCCESS) {
216 goto out_free_newdata;
218 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
219 new_dbuf.dptr = newdata;
220 new_dbuf.dsize = old_dlen + dbuf.dsize;
226 /* If they're using ntdb_append(), it implies they're growing record. */
227 ecode = replace_data(ntdb, &h, key, new_dbuf, off, old_room, true);
230 ntdb->free_fn(newdata, ntdb->alloc_data);
232 ntdb_unlock_hash(ntdb, h.h, F_WRLCK);
236 _PUBLIC_ enum NTDB_ERROR ntdb_fetch(struct ntdb_context *ntdb, NTDB_DATA key,
240 struct ntdb_used_record rec;
242 enum NTDB_ERROR ecode;
244 off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec);
245 if (NTDB_OFF_IS_ERR(off)) {
246 return NTDB_OFF_TO_ERR(off);
250 ecode = NTDB_ERR_NOEXIST;
252 data->dsize = rec_data_length(&rec);
253 data->dptr = ntdb_alloc_read(ntdb, off + sizeof(rec) + key.dsize,
255 if (NTDB_PTR_IS_ERR(data->dptr)) {
256 ecode = NTDB_PTR_ERR(data->dptr);
258 ecode = NTDB_SUCCESS;
261 ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
265 _PUBLIC_ bool ntdb_exists(struct ntdb_context *ntdb, NTDB_DATA key)
268 struct ntdb_used_record rec;
271 off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec);
272 if (NTDB_OFF_IS_ERR(off)) {
275 ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
277 return off ? true : false;
280 _PUBLIC_ enum NTDB_ERROR ntdb_delete(struct ntdb_context *ntdb, NTDB_DATA key)
283 struct ntdb_used_record rec;
285 enum NTDB_ERROR ecode;
287 off = find_and_lock(ntdb, key, F_WRLCK, &h, &rec);
288 if (NTDB_OFF_IS_ERR(off)) {
289 return NTDB_OFF_TO_ERR(off);
293 ecode = NTDB_ERR_NOEXIST;
297 ecode = delete_from_hash(ntdb, &h);
298 if (ecode != NTDB_SUCCESS) {
302 /* Free the deleted entry. */
304 ecode = add_free_record(ntdb, off,
305 sizeof(struct ntdb_used_record)
306 + rec_key_length(&rec)
307 + rec_data_length(&rec)
308 + rec_extra_padding(&rec),
309 NTDB_LOCK_WAIT, true);
311 if (ntdb->flags & NTDB_SEQNUM)
312 ntdb_inc_seqnum(ntdb);
315 ntdb_unlock_hash(ntdb, h.h, F_WRLCK);
319 _PUBLIC_ unsigned int ntdb_get_flags(struct ntdb_context *ntdb)
324 static bool inside_transaction(const struct ntdb_context *ntdb)
326 return ntdb->transaction != NULL;
329 static bool readonly_changable(struct ntdb_context *ntdb, const char *caller)
331 if (inside_transaction(ntdb)) {
332 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
334 " NTDB_RDONLY inside transaction",
341 _PUBLIC_ void ntdb_add_flag(struct ntdb_context *ntdb, unsigned flag)
343 if (ntdb->flags & NTDB_INTERNAL) {
344 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
345 "ntdb_add_flag: internal db");
350 ntdb->flags |= NTDB_NOLOCK;
353 ntdb->flags |= NTDB_NOMMAP;
354 #ifndef HAVE_INCOHERENT_MMAP
355 ntdb_munmap(ntdb->file);
359 ntdb->flags |= NTDB_NOSYNC;
362 ntdb->flags |= NTDB_SEQNUM;
364 case NTDB_ALLOW_NESTING:
365 ntdb->flags |= NTDB_ALLOW_NESTING;
368 if (readonly_changable(ntdb, "ntdb_add_flag"))
369 ntdb->flags |= NTDB_RDONLY;
372 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
373 "ntdb_add_flag: Unknown flag %u", flag);
377 _PUBLIC_ void ntdb_remove_flag(struct ntdb_context *ntdb, unsigned flag)
379 if (ntdb->flags & NTDB_INTERNAL) {
380 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
381 "ntdb_remove_flag: internal db");
386 ntdb->flags &= ~NTDB_NOLOCK;
389 ntdb->flags &= ~NTDB_NOMMAP;
390 #ifndef HAVE_INCOHERENT_MMAP
391 /* If mmap incoherent, we were mmaping anyway. */
396 ntdb->flags &= ~NTDB_NOSYNC;
399 ntdb->flags &= ~NTDB_SEQNUM;
401 case NTDB_ALLOW_NESTING:
402 ntdb->flags &= ~NTDB_ALLOW_NESTING;
405 if ((ntdb->open_flags & O_ACCMODE) == O_RDONLY) {
406 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
407 "ntdb_remove_flag: can't"
408 " remove NTDB_RDONLY on ntdb"
409 " opened with O_RDONLY");
412 if (readonly_changable(ntdb, "ntdb_remove_flag"))
413 ntdb->flags &= ~NTDB_RDONLY;
416 ntdb_logerr(ntdb, NTDB_ERR_EINVAL, NTDB_LOG_USE_ERROR,
417 "ntdb_remove_flag: Unknown flag %u",
422 _PUBLIC_ const char *ntdb_errorstr(enum NTDB_ERROR ecode)
424 /* Gcc warns if you miss a case in the switch, so use that. */
425 switch (NTDB_ERR_TO_OFF(ecode)) {
426 case NTDB_ERR_TO_OFF(NTDB_SUCCESS): return "Success";
427 case NTDB_ERR_TO_OFF(NTDB_ERR_CORRUPT): return "Corrupt database";
428 case NTDB_ERR_TO_OFF(NTDB_ERR_IO): return "IO Error";
429 case NTDB_ERR_TO_OFF(NTDB_ERR_LOCK): return "Locking error";
430 case NTDB_ERR_TO_OFF(NTDB_ERR_OOM): return "Out of memory";
431 case NTDB_ERR_TO_OFF(NTDB_ERR_EXISTS): return "Record exists";
432 case NTDB_ERR_TO_OFF(NTDB_ERR_EINVAL): return "Invalid parameter";
433 case NTDB_ERR_TO_OFF(NTDB_ERR_NOEXIST): return "Record does not exist";
434 case NTDB_ERR_TO_OFF(NTDB_ERR_RDONLY): return "write not permitted";
436 return "Invalid error code";
439 enum NTDB_ERROR COLD ntdb_logerr(struct ntdb_context *ntdb,
440 enum NTDB_ERROR ecode,
441 enum ntdb_log_level level,
442 const char *fmt, ...)
447 /* ntdb_open paths care about errno, so save it. */
448 int saved_errno = errno;
454 len = vsnprintf(NULL, 0, fmt, ap);
457 message = ntdb->alloc_fn(ntdb, len + 1, ntdb->alloc_data);
459 ntdb->log_fn(ntdb, NTDB_LOG_ERROR, NTDB_ERR_OOM,
460 "out of memory formatting message:", ntdb->log_data);
461 ntdb->log_fn(ntdb, level, ecode, fmt, ntdb->log_data);
464 vsnprintf(message, len+1, fmt, ap);
466 ntdb->log_fn(ntdb, level, ecode, message, ntdb->log_data);
467 ntdb->free_fn(message, ntdb->alloc_data);
473 _PUBLIC_ enum NTDB_ERROR ntdb_parse_record_(struct ntdb_context *ntdb,
475 enum NTDB_ERROR (*parse)(NTDB_DATA k,
481 struct ntdb_used_record rec;
483 enum NTDB_ERROR ecode;
485 off = find_and_lock(ntdb, key, F_RDLCK, &h, &rec);
486 if (NTDB_OFF_IS_ERR(off)) {
487 return NTDB_OFF_TO_ERR(off);
491 ecode = NTDB_ERR_NOEXIST;
494 dptr = ntdb_access_read(ntdb, off + sizeof(rec) + key.dsize,
495 rec_data_length(&rec), false);
496 if (NTDB_PTR_IS_ERR(dptr)) {
497 ecode = NTDB_PTR_ERR(dptr);
499 NTDB_DATA d = ntdb_mkdata(dptr, rec_data_length(&rec));
501 ecode = parse(key, d, data);
502 ntdb_access_release(ntdb, dptr);
506 ntdb_unlock_hash(ntdb, h.h, F_RDLCK);
510 _PUBLIC_ const char *ntdb_name(const struct ntdb_context *ntdb)
515 _PUBLIC_ int64_t ntdb_get_seqnum(struct ntdb_context *ntdb)
517 return ntdb_read_off(ntdb, offsetof(struct ntdb_header, seqnum));
521 _PUBLIC_ int ntdb_fd(const struct ntdb_context *ntdb)
523 return ntdb->file->fd;
526 struct traverse_state {
527 enum NTDB_ERROR error;
528 struct ntdb_context *dest_db;
532 traverse function for repacking
534 static int repack_traverse(struct ntdb_context *ntdb, NTDB_DATA key, NTDB_DATA data,
535 struct traverse_state *state)
537 state->error = ntdb_store(state->dest_db, key, data, NTDB_INSERT);
538 if (state->error != NTDB_SUCCESS) {
544 _PUBLIC_ enum NTDB_ERROR ntdb_repack(struct ntdb_context *ntdb)
546 struct ntdb_context *tmp_db;
547 struct traverse_state state;
549 state.error = ntdb_transaction_start(ntdb);
550 if (state.error != NTDB_SUCCESS) {
554 tmp_db = ntdb_open("tmpdb", NTDB_INTERNAL, O_RDWR|O_CREAT, 0, NULL);
555 if (tmp_db == NULL) {
556 state.error = ntdb_logerr(ntdb, NTDB_ERR_OOM, NTDB_LOG_ERROR,
558 " Failed to create tmp_db");
559 ntdb_transaction_cancel(ntdb);
563 state.dest_db = tmp_db;
564 if (ntdb_traverse(ntdb, repack_traverse, &state) < 0) {
568 state.error = ntdb_wipe_all(ntdb);
569 if (state.error != NTDB_SUCCESS) {
573 state.dest_db = ntdb;
574 if (ntdb_traverse(tmp_db, repack_traverse, &state) < 0) {
579 return ntdb_transaction_commit(ntdb);
582 ntdb_transaction_cancel(ntdb);