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 #include <ccan/asprintf/asprintf.h>
22 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
26 struct tdb_used_record *rec,
29 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
32 ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
33 keylen + dataroom, h);
34 if (ecode == TDB_SUCCESS) {
35 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
40 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
42 struct tdb_data key, struct tdb_data dbuf,
43 tdb_off_t old_off, tdb_len_t old_room,
49 /* Allocate a new record. */
50 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
52 if (TDB_OFF_IS_ERR(new_off)) {
56 /* We didn't like the existing one: remove it. */
59 ecode = add_free_record(tdb, old_off,
60 sizeof(struct tdb_used_record)
61 + key.dsize + old_room,
63 if (ecode == TDB_SUCCESS)
64 ecode = replace_in_hash(tdb, h, new_off);
66 ecode = add_to_hash(tdb, h, new_off);
68 if (ecode != TDB_SUCCESS) {
72 new_off += sizeof(struct tdb_used_record);
73 ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
74 if (ecode != TDB_SUCCESS) {
79 ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
80 if (ecode != TDB_SUCCESS) {
84 if (tdb->flags & TDB_SEQNUM)
90 static enum TDB_ERROR update_data(struct tdb_context *tdb,
97 ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
98 if (ecode == TDB_SUCCESS && extra) {
99 /* Put a zero in; future versions may append other data. */
100 ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
102 if (tdb->flags & TDB_SEQNUM)
108 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
109 struct tdb_data key, struct tdb_data dbuf, int flag)
113 tdb_len_t old_room = 0;
114 struct tdb_used_record rec;
115 enum TDB_ERROR ecode;
117 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
118 if (TDB_OFF_IS_ERR(off)) {
119 return tdb->last_error = off;
122 /* Now we have lock on this hash bucket. */
123 if (flag == TDB_INSERT) {
125 ecode = TDB_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(tdb, off,
135 key.dsize, dbuf.dsize,
137 if (ecode != TDB_SUCCESS) {
140 ecode = update_data(tdb,
143 old_room - dbuf.dsize);
144 if (ecode != TDB_SUCCESS) {
147 tdb_unlock_hashes(tdb, h.hlock_start,
148 h.hlock_range, F_WRLCK);
149 return tdb->last_error = TDB_SUCCESS;
152 if (flag == TDB_MODIFY) {
153 /* if the record doesn't exist and we
154 are in TDB_MODIFY mode then we should fail
156 ecode = TDB_ERR_NOEXIST;
162 /* If we didn't use the old record, this implies we're growing. */
163 ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
165 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
166 return tdb->last_error = ecode;
169 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
170 struct tdb_data key, struct tdb_data dbuf)
174 struct tdb_used_record rec;
175 tdb_len_t old_room = 0, old_dlen;
176 unsigned char *newdata;
177 struct tdb_data new_dbuf;
178 enum TDB_ERROR ecode;
180 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
181 if (TDB_OFF_IS_ERR(off)) {
182 return tdb->last_error = off;
186 old_dlen = rec_data_length(&rec);
187 old_room = old_dlen + rec_extra_padding(&rec);
189 /* Fast path: can append in place. */
190 if (rec_extra_padding(&rec) >= dbuf.dsize) {
191 ecode = update_rec_hdr(tdb, off, key.dsize,
192 old_dlen + dbuf.dsize, &rec,
194 if (ecode != TDB_SUCCESS) {
198 off += sizeof(rec) + key.dsize + old_dlen;
199 ecode = update_data(tdb, off, dbuf,
200 rec_extra_padding(&rec));
205 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
207 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
209 " failed to allocate %zu bytes",
210 (size_t)(key.dsize + old_dlen
214 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
216 if (ecode != TDB_SUCCESS) {
217 goto out_free_newdata;
219 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
220 new_dbuf.dptr = newdata;
221 new_dbuf.dsize = old_dlen + dbuf.dsize;
227 /* If they're using tdb_append(), it implies they're growing record. */
228 ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
233 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
234 return tdb->last_error = ecode;
237 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
238 struct tdb_data *data)
241 struct tdb_used_record rec;
243 enum TDB_ERROR ecode;
245 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
246 if (TDB_OFF_IS_ERR(off)) {
247 return tdb->last_error = off;
251 ecode = TDB_ERR_NOEXIST;
253 data->dsize = rec_data_length(&rec);
254 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
256 if (TDB_PTR_IS_ERR(data->dptr)) {
257 ecode = TDB_PTR_ERR(data->dptr);
262 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
263 return tdb->last_error = ecode;
266 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
269 struct tdb_used_record rec;
272 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
273 if (TDB_OFF_IS_ERR(off)) {
274 tdb->last_error = off;
277 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
279 tdb->last_error = TDB_SUCCESS;
280 return off ? true : false;
283 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
286 struct tdb_used_record rec;
288 enum TDB_ERROR ecode;
290 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
291 if (TDB_OFF_IS_ERR(off)) {
292 return tdb->last_error = off;
296 ecode = TDB_ERR_NOEXIST;
300 ecode = delete_from_hash(tdb, &h);
301 if (ecode != TDB_SUCCESS) {
305 /* Free the deleted entry. */
307 ecode = add_free_record(tdb, off,
308 sizeof(struct tdb_used_record)
309 + rec_key_length(&rec)
310 + rec_data_length(&rec)
311 + rec_extra_padding(&rec),
312 TDB_LOCK_WAIT, true);
314 if (tdb->flags & TDB_SEQNUM)
318 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
319 return tdb->last_error = ecode;
322 unsigned int tdb_get_flags(struct tdb_context *tdb)
327 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
329 if (tdb->flags & TDB_INTERNAL) {
330 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
332 "tdb_add_flag: internal db");
337 tdb->flags |= TDB_NOLOCK;
340 tdb->flags |= TDB_NOMMAP;
341 tdb_munmap(tdb->file);
344 tdb->flags |= TDB_NOSYNC;
347 tdb->flags |= TDB_SEQNUM;
349 case TDB_ALLOW_NESTING:
350 tdb->flags |= TDB_ALLOW_NESTING;
353 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
355 "tdb_add_flag: Unknown flag %u",
360 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
362 if (tdb->flags & TDB_INTERNAL) {
363 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
365 "tdb_remove_flag: internal db");
370 tdb->flags &= ~TDB_NOLOCK;
373 tdb->flags &= ~TDB_NOMMAP;
377 tdb->flags &= ~TDB_NOSYNC;
380 tdb->flags &= ~TDB_SEQNUM;
382 case TDB_ALLOW_NESTING:
383 tdb->flags &= ~TDB_ALLOW_NESTING;
386 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
388 "tdb_remove_flag: Unknown flag %u",
393 const char *tdb_errorstr(enum TDB_ERROR ecode)
395 /* Gcc warns if you miss a case in the switch, so use that. */
397 case TDB_SUCCESS: return "Success";
398 case TDB_ERR_CORRUPT: return "Corrupt database";
399 case TDB_ERR_IO: return "IO Error";
400 case TDB_ERR_LOCK: return "Locking error";
401 case TDB_ERR_OOM: return "Out of memory";
402 case TDB_ERR_EXISTS: return "Record exists";
403 case TDB_ERR_EINVAL: return "Invalid parameter";
404 case TDB_ERR_NOEXIST: return "Record does not exist";
405 case TDB_ERR_RDONLY: return "write not permitted";
407 return "Invalid error code";
410 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
412 return tdb->last_error;
415 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
416 enum TDB_ERROR ecode,
417 enum tdb_log_level level,
418 const char *fmt, ...)
423 /* tdb_open paths care about errno, so save it. */
424 int saved_errno = errno;
430 len = vasprintf(&message, fmt, ap);
434 tdb->log_fn(tdb, TDB_LOG_ERROR,
435 "out of memory formatting message:", tdb->log_data);
436 tdb->log_fn(tdb, level, fmt, tdb->log_data);
438 tdb->log_fn(tdb, level, message, tdb->log_data);
445 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
447 enum TDB_ERROR (*parse)(TDB_DATA k,
453 struct tdb_used_record rec;
455 enum TDB_ERROR ecode;
457 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
458 if (TDB_OFF_IS_ERR(off)) {
459 return tdb->last_error = off;
463 ecode = TDB_ERR_NOEXIST;
466 dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
467 rec_data_length(&rec), false);
468 if (TDB_PTR_IS_ERR(dptr)) {
469 ecode = TDB_PTR_ERR(dptr);
471 TDB_DATA d = tdb_mkdata(dptr, rec_data_length(&rec));
473 ecode = parse(key, d, data);
474 tdb_access_release(tdb, dptr);
478 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
479 return tdb->last_error = ecode;
482 const char *tdb_name(const struct tdb_context *tdb)
487 int64_t tdb_get_seqnum(struct tdb_context *tdb)
489 tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
490 if (TDB_OFF_IS_ERR(off))
491 tdb->last_error = off;
493 tdb->last_error = TDB_SUCCESS;
498 int tdb_fd(const struct tdb_context *tdb)
500 return tdb->file->fd;