2 #include <ccan/asprintf/asprintf.h>
5 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
9 struct tdb_used_record *rec,
12 uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
15 ecode = set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen,
16 keylen + dataroom, h);
17 if (ecode == TDB_SUCCESS) {
18 ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec));
23 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
25 struct tdb_data key, struct tdb_data dbuf,
26 tdb_off_t old_off, tdb_len_t old_room,
32 /* Allocate a new record. */
33 new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
35 if (TDB_OFF_IS_ERR(new_off)) {
39 /* We didn't like the existing one: remove it. */
41 add_stat(tdb, frees, 1);
42 ecode = add_free_record(tdb, old_off,
43 sizeof(struct tdb_used_record)
44 + key.dsize + old_room);
45 if (ecode == TDB_SUCCESS)
46 ecode = replace_in_hash(tdb, h, new_off);
48 ecode = add_to_hash(tdb, h, new_off);
50 if (ecode != TDB_SUCCESS) {
54 new_off += sizeof(struct tdb_used_record);
55 ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize);
56 if (ecode != TDB_SUCCESS) {
61 ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
62 if (ecode != TDB_SUCCESS) {
66 if (tdb->flags & TDB_SEQNUM)
72 static enum TDB_ERROR update_data(struct tdb_context *tdb,
79 ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, dbuf.dsize);
80 if (ecode == TDB_SUCCESS && extra) {
81 /* Put a zero in; future versions may append other data. */
82 ecode = tdb->methods->twrite(tdb, off + dbuf.dsize, "", 1);
84 if (tdb->flags & TDB_SEQNUM)
90 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
91 struct tdb_data key, struct tdb_data dbuf, int flag)
95 tdb_len_t old_room = 0;
96 struct tdb_used_record rec;
99 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
100 if (TDB_OFF_IS_ERR(off)) {
101 return tdb->last_error = off;
104 /* Now we have lock on this hash bucket. */
105 if (flag == TDB_INSERT) {
107 ecode = TDB_ERR_EXISTS;
112 old_room = rec_data_length(&rec)
113 + rec_extra_padding(&rec);
114 if (old_room >= dbuf.dsize) {
115 /* Can modify in-place. Easy! */
116 ecode = update_rec_hdr(tdb, off,
117 key.dsize, dbuf.dsize,
119 if (ecode != TDB_SUCCESS) {
122 ecode = update_data(tdb,
125 old_room - dbuf.dsize);
126 if (ecode != TDB_SUCCESS) {
129 tdb_unlock_hashes(tdb, h.hlock_start,
130 h.hlock_range, F_WRLCK);
131 return tdb->last_error = TDB_SUCCESS;
134 if (flag == TDB_MODIFY) {
135 /* if the record doesn't exist and we
136 are in TDB_MODIFY mode then we should fail
138 ecode = TDB_ERR_NOEXIST;
144 /* If we didn't use the old record, this implies we're growing. */
145 ecode = replace_data(tdb, &h, key, dbuf, off, old_room, off);
147 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
148 return tdb->last_error = ecode;
151 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
152 struct tdb_data key, struct tdb_data dbuf)
156 struct tdb_used_record rec;
157 tdb_len_t old_room = 0, old_dlen;
158 unsigned char *newdata;
159 struct tdb_data new_dbuf;
160 enum TDB_ERROR ecode;
162 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
163 if (TDB_OFF_IS_ERR(off)) {
164 return tdb->last_error = off;
168 old_dlen = rec_data_length(&rec);
169 old_room = old_dlen + rec_extra_padding(&rec);
171 /* Fast path: can append in place. */
172 if (rec_extra_padding(&rec) >= dbuf.dsize) {
173 ecode = update_rec_hdr(tdb, off, key.dsize,
174 old_dlen + dbuf.dsize, &rec,
176 if (ecode != TDB_SUCCESS) {
180 off += sizeof(rec) + key.dsize + old_dlen;
181 ecode = update_data(tdb, off, dbuf,
182 rec_extra_padding(&rec));
187 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
189 ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
191 " failed to allocate %zu bytes",
192 (size_t)(key.dsize + old_dlen
196 ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize,
198 if (ecode != TDB_SUCCESS) {
199 goto out_free_newdata;
201 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
202 new_dbuf.dptr = newdata;
203 new_dbuf.dsize = old_dlen + dbuf.dsize;
209 /* If they're using tdb_append(), it implies they're growing record. */
210 ecode = replace_data(tdb, &h, key, new_dbuf, off, old_room, true);
215 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
216 return tdb->last_error = ecode;
219 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
220 struct tdb_data *data)
223 struct tdb_used_record rec;
225 enum TDB_ERROR ecode;
227 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
228 if (TDB_OFF_IS_ERR(off)) {
229 return tdb->last_error = off;
233 ecode = TDB_ERR_NOEXIST;
235 data->dsize = rec_data_length(&rec);
236 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
238 if (TDB_PTR_IS_ERR(data->dptr)) {
239 ecode = TDB_PTR_ERR(data->dptr);
244 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
245 return tdb->last_error = ecode;
248 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
251 struct tdb_used_record rec;
254 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
255 if (TDB_OFF_IS_ERR(off)) {
256 tdb->last_error = off;
259 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
261 tdb->last_error = TDB_SUCCESS;
262 return off ? true : false;
265 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
268 struct tdb_used_record rec;
270 enum TDB_ERROR ecode;
272 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
273 if (TDB_OFF_IS_ERR(off)) {
274 return tdb->last_error = off;
278 ecode = TDB_ERR_NOEXIST;
282 ecode = delete_from_hash(tdb, &h);
283 if (ecode != TDB_SUCCESS) {
287 /* Free the deleted entry. */
288 add_stat(tdb, frees, 1);
289 ecode = add_free_record(tdb, off,
290 sizeof(struct tdb_used_record)
291 + rec_key_length(&rec)
292 + rec_data_length(&rec)
293 + rec_extra_padding(&rec));
295 if (tdb->flags & TDB_SEQNUM)
299 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
300 return tdb->last_error = ecode;
303 unsigned int tdb_get_flags(struct tdb_context *tdb)
308 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
310 if (tdb->flags & TDB_INTERNAL) {
311 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
313 "tdb_add_flag: internal db");
318 tdb->flags |= TDB_NOLOCK;
321 tdb->flags |= TDB_NOMMAP;
322 tdb_munmap(tdb->file);
325 tdb->flags |= TDB_NOSYNC;
328 tdb->flags |= TDB_SEQNUM;
331 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
333 "tdb_add_flag: Unknown flag %u",
338 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
340 if (tdb->flags & TDB_INTERNAL) {
341 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
343 "tdb_remove_flag: internal db");
348 tdb->flags &= ~TDB_NOLOCK;
351 tdb->flags &= ~TDB_NOMMAP;
355 tdb->flags &= ~TDB_NOSYNC;
358 tdb->flags &= ~TDB_SEQNUM;
361 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
363 "tdb_remove_flag: Unknown flag %u",
368 const char *tdb_errorstr(enum TDB_ERROR ecode)
370 /* Gcc warns if you miss a case in the switch, so use that. */
372 case TDB_SUCCESS: return "Success";
373 case TDB_ERR_CORRUPT: return "Corrupt database";
374 case TDB_ERR_IO: return "IO Error";
375 case TDB_ERR_LOCK: return "Locking error";
376 case TDB_ERR_OOM: return "Out of memory";
377 case TDB_ERR_EXISTS: return "Record exists";
378 case TDB_ERR_EINVAL: return "Invalid parameter";
379 case TDB_ERR_NOEXIST: return "Record does not exist";
380 case TDB_ERR_RDONLY: return "write not permitted";
382 return "Invalid error code";
385 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
386 enum TDB_ERROR ecode,
387 enum tdb_log_level level,
388 const char *fmt, ...)
393 /* tdb_open paths care about errno, so save it. */
394 int saved_errno = errno;
400 len = vasprintf(&message, fmt, ap);
404 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
405 "out of memory formatting message:");
406 tdb->logfn(tdb, level, tdb->log_private, fmt);
408 tdb->logfn(tdb, level, tdb->log_private, message);
415 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
417 enum TDB_ERROR (*parse)(TDB_DATA key,
423 struct tdb_used_record rec;
425 enum TDB_ERROR ecode;
427 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
428 if (TDB_OFF_IS_ERR(off)) {
429 return tdb->last_error = off;
433 ecode = TDB_ERR_NOEXIST;
436 dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
437 rec_data_length(&rec), false);
438 if (TDB_PTR_IS_ERR(dptr)) {
439 ecode = TDB_PTR_ERR(dptr);
441 TDB_DATA data = tdb_mkdata(dptr, rec_data_length(&rec));
443 ecode = parse(key, data, p);
444 tdb_access_release(tdb, dptr);
448 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
449 return tdb->last_error = ecode;
452 const char *tdb_name(const struct tdb_context *tdb)
457 int64_t tdb_get_seqnum(struct tdb_context *tdb)
459 tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
460 if (TDB_OFF_IS_ERR(off))
461 tdb->last_error = off;
463 tdb->last_error = TDB_SUCCESS;
468 int tdb_fd(const struct tdb_context *tdb)
470 return tdb->file->fd;