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)) {
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);
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);
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)) {
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);
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)) {
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);
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)) {
258 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
260 return off ? true : false;
263 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
266 struct tdb_used_record rec;
268 enum TDB_ERROR ecode;
270 off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
271 if (TDB_OFF_IS_ERR(off)) {
276 ecode = TDB_ERR_NOEXIST;
280 ecode = delete_from_hash(tdb, &h);
281 if (ecode != TDB_SUCCESS) {
285 /* Free the deleted entry. */
286 add_stat(tdb, frees, 1);
287 ecode = add_free_record(tdb, off,
288 sizeof(struct tdb_used_record)
289 + rec_key_length(&rec)
290 + rec_data_length(&rec)
291 + rec_extra_padding(&rec));
293 if (tdb->flags & TDB_SEQNUM)
297 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
301 unsigned int tdb_get_flags(struct tdb_context *tdb)
306 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
308 if (tdb->flags & TDB_INTERNAL) {
309 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
310 "tdb_add_flag: internal db");
315 tdb->flags |= TDB_NOLOCK;
318 tdb->flags |= TDB_NOMMAP;
319 tdb_munmap(tdb->file);
322 tdb->flags |= TDB_NOSYNC;
325 tdb->flags |= TDB_SEQNUM;
328 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
329 "tdb_add_flag: Unknown flag %u", flag);
333 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
335 if (tdb->flags & TDB_INTERNAL) {
336 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
337 "tdb_remove_flag: internal db");
342 tdb->flags &= ~TDB_NOLOCK;
345 tdb->flags &= ~TDB_NOMMAP;
349 tdb->flags &= ~TDB_NOSYNC;
352 tdb->flags &= ~TDB_SEQNUM;
355 tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
356 "tdb_remove_flag: Unknown flag %u", flag);
360 const char *tdb_errorstr(enum TDB_ERROR ecode)
362 /* Gcc warns if you miss a case in the switch, so use that. */
364 case TDB_SUCCESS: return "Success";
365 case TDB_ERR_CORRUPT: return "Corrupt database";
366 case TDB_ERR_IO: return "IO Error";
367 case TDB_ERR_LOCK: return "Locking error";
368 case TDB_ERR_OOM: return "Out of memory";
369 case TDB_ERR_EXISTS: return "Record exists";
370 case TDB_ERR_EINVAL: return "Invalid parameter";
371 case TDB_ERR_NOEXIST: return "Record does not exist";
372 case TDB_ERR_RDONLY: return "write not permitted";
374 return "Invalid error code";
377 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
378 enum TDB_ERROR ecode,
379 enum tdb_log_level level,
380 const char *fmt, ...)
385 /* tdb_open paths care about errno, so save it. */
386 int saved_errno = errno;
392 len = vasprintf(&message, fmt, ap);
396 tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
397 "out of memory formatting message:");
398 tdb->logfn(tdb, level, tdb->log_private, fmt);
400 tdb->logfn(tdb, level, tdb->log_private, message);
407 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
409 enum TDB_ERROR (*parse)(TDB_DATA key,
415 struct tdb_used_record rec;
418 enum TDB_ERROR ecode;
420 off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
421 if (TDB_OFF_IS_ERR(off)) {
426 ecode = TDB_ERR_NOEXIST;
428 data.dsize = rec_data_length(&rec);
429 data.dptr = (void *)tdb_access_read(tdb,
433 if (TDB_PTR_IS_ERR(data.dptr)) {
434 ecode = TDB_PTR_ERR(data.dptr);
436 ecode = parse(key, data, p);
437 tdb_access_release(tdb, data.dptr);
441 tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
445 const char *tdb_name(const struct tdb_context *tdb)
450 int64_t tdb_get_seqnum(struct tdb_context *tdb)
452 return tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
456 int tdb_fd(const struct tdb_context *tdb)
458 return tdb->file->fd;