]> git.ozlabs.org Git - ccan/blob - ccan/tdb2/tdb.c
tdb2: make jenkins_hash function non-static, rename to tdb_jenkins_hash.
[ccan] / ccan / tdb2 / tdb.c
1  /*
2    Trivial Database 2: fetch, store and misc routines.
3    Copyright (C) Rusty Russell 2010
4
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.
9
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.
14
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/>.
17 */
18 #include "private.h"
19 #include <ccan/asprintf/asprintf.h>
20 #include <stdarg.h>
21
22 static enum TDB_ERROR update_rec_hdr(struct tdb_context *tdb,
23                                      tdb_off_t off,
24                                      tdb_len_t keylen,
25                                      tdb_len_t datalen,
26                                      struct tdb_used_record *rec,
27                                      uint64_t h)
28 {
29         uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
30         enum TDB_ERROR ecode;
31
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));
36         }
37         return ecode;
38 }
39
40 static enum TDB_ERROR replace_data(struct tdb_context *tdb,
41                                    struct hash_info *h,
42                                    struct tdb_data key, struct tdb_data dbuf,
43                                    tdb_off_t old_off, tdb_len_t old_room,
44                                    bool growing)
45 {
46         tdb_off_t new_off;
47         enum TDB_ERROR ecode;
48
49         /* Allocate a new record. */
50         new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC,
51                         growing);
52         if (TDB_OFF_IS_ERR(new_off)) {
53                 return new_off;
54         }
55
56         /* We didn't like the existing one: remove it. */
57         if (old_off) {
58                 tdb->stats.frees++;
59                 ecode = add_free_record(tdb, old_off,
60                                         sizeof(struct tdb_used_record)
61                                         + key.dsize + old_room,
62                                         TDB_LOCK_WAIT, true);
63                 if (ecode == TDB_SUCCESS)
64                         ecode = replace_in_hash(tdb, h, new_off);
65         } else {
66                 ecode = add_to_hash(tdb, h, new_off);
67         }
68         if (ecode != TDB_SUCCESS) {
69                 return ecode;
70         }
71
72         new_off += sizeof(struct tdb_used_record);
73         ecode = tdb->tdb2.io->twrite(tdb, new_off, key.dptr, key.dsize);
74         if (ecode != TDB_SUCCESS) {
75                 return ecode;
76         }
77
78         new_off += key.dsize;
79         ecode = tdb->tdb2.io->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize);
80         if (ecode != TDB_SUCCESS) {
81                 return ecode;
82         }
83
84         if (tdb->flags & TDB_SEQNUM)
85                 tdb_inc_seqnum(tdb);
86
87         return TDB_SUCCESS;
88 }
89
90 static enum TDB_ERROR update_data(struct tdb_context *tdb,
91                                   tdb_off_t off,
92                                   struct tdb_data dbuf,
93                                   tdb_len_t extra)
94 {
95         enum TDB_ERROR ecode;
96
97         ecode = tdb->tdb2.io->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->tdb2.io->twrite(tdb, off + dbuf.dsize, "", 1);
101         }
102         if (tdb->flags & TDB_SEQNUM)
103                 tdb_inc_seqnum(tdb);
104
105         return ecode;
106 }
107
108 enum TDB_ERROR tdb_store(struct tdb_context *tdb,
109                          struct tdb_data key, struct tdb_data dbuf, int flag)
110 {
111         struct hash_info h;
112         tdb_off_t off;
113         tdb_len_t old_room = 0;
114         struct tdb_used_record rec;
115         enum TDB_ERROR ecode;
116
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;
120         }
121
122         /* Now we have lock on this hash bucket. */
123         if (flag == TDB_INSERT) {
124                 if (off) {
125                         ecode = TDB_ERR_EXISTS;
126                         goto out;
127                 }
128         } else {
129                 if (off) {
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,
136                                                        &rec, h.h);
137                                 if (ecode != TDB_SUCCESS) {
138                                         goto out;
139                                 }
140                                 ecode = update_data(tdb,
141                                                     off + sizeof(rec)
142                                                     + key.dsize, dbuf,
143                                                     old_room - dbuf.dsize);
144                                 if (ecode != TDB_SUCCESS) {
145                                         goto out;
146                                 }
147                                 tdb_unlock_hashes(tdb, h.hlock_start,
148                                                   h.hlock_range, F_WRLCK);
149                                 return tdb->last_error = TDB_SUCCESS;
150                         }
151                 } else {
152                         if (flag == TDB_MODIFY) {
153                                 /* if the record doesn't exist and we
154                                    are in TDB_MODIFY mode then we should fail
155                                    the store */
156                                 ecode = TDB_ERR_NOEXIST;
157                                 goto out;
158                         }
159                 }
160         }
161
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);
164 out:
165         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
166         return tdb->last_error = ecode;
167 }
168
169 enum TDB_ERROR tdb_append(struct tdb_context *tdb,
170                           struct tdb_data key, struct tdb_data dbuf)
171 {
172         struct hash_info h;
173         tdb_off_t off;
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;
179
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;
183         }
184
185         if (off) {
186                 old_dlen = rec_data_length(&rec);
187                 old_room = old_dlen + rec_extra_padding(&rec);
188
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,
193                                                h.h);
194                         if (ecode != TDB_SUCCESS) {
195                                 goto out;
196                         }
197
198                         off += sizeof(rec) + key.dsize + old_dlen;
199                         ecode = update_data(tdb, off, dbuf,
200                                             rec_extra_padding(&rec));
201                         goto out;
202                 }
203
204                 /* Slow path. */
205                 newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
206                 if (!newdata) {
207                         ecode = tdb_logerr(tdb, TDB_ERR_OOM, TDB_LOG_ERROR,
208                                            "tdb_append:"
209                                            " failed to allocate %zu bytes",
210                                            (size_t)(key.dsize + old_dlen
211                                                     + dbuf.dsize));
212                         goto out;
213                 }
214                 ecode = tdb->tdb2.io->tread(tdb, off + sizeof(rec) + key.dsize,
215                                             newdata, old_dlen);
216                 if (ecode != TDB_SUCCESS) {
217                         goto out_free_newdata;
218                 }
219                 memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
220                 new_dbuf.dptr = newdata;
221                 new_dbuf.dsize = old_dlen + dbuf.dsize;
222         } else {
223                 newdata = NULL;
224                 new_dbuf = dbuf;
225         }
226
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);
229
230 out_free_newdata:
231         free(newdata);
232 out:
233         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
234         return tdb->last_error = ecode;
235 }
236
237 enum TDB_ERROR tdb_fetch(struct tdb_context *tdb, struct tdb_data key,
238                          struct tdb_data *data)
239 {
240         tdb_off_t off;
241         struct tdb_used_record rec;
242         struct hash_info h;
243         enum TDB_ERROR ecode;
244
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;
248         }
249
250         if (!off) {
251                 ecode = TDB_ERR_NOEXIST;
252         } else {
253                 data->dsize = rec_data_length(&rec);
254                 data->dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
255                                             data->dsize);
256                 if (TDB_PTR_IS_ERR(data->dptr)) {
257                         ecode = TDB_PTR_ERR(data->dptr);
258                 } else
259                         ecode = TDB_SUCCESS;
260         }
261
262         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
263         return tdb->last_error = ecode;
264 }
265
266 bool tdb_exists(struct tdb_context *tdb, TDB_DATA key)
267 {
268         tdb_off_t off;
269         struct tdb_used_record rec;
270         struct hash_info h;
271
272         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
273         if (TDB_OFF_IS_ERR(off)) {
274                 tdb->last_error = off;
275                 return false;
276         }
277         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
278
279         tdb->last_error = TDB_SUCCESS;
280         return off ? true : false;
281 }
282
283 enum TDB_ERROR tdb_delete(struct tdb_context *tdb, struct tdb_data key)
284 {
285         tdb_off_t off;
286         struct tdb_used_record rec;
287         struct hash_info h;
288         enum TDB_ERROR ecode;
289
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;
293         }
294
295         if (!off) {
296                 ecode = TDB_ERR_NOEXIST;
297                 goto unlock;
298         }
299
300         ecode = delete_from_hash(tdb, &h);
301         if (ecode != TDB_SUCCESS) {
302                 goto unlock;
303         }
304
305         /* Free the deleted entry. */
306         tdb->stats.frees++;
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);
313
314         if (tdb->flags & TDB_SEQNUM)
315                 tdb_inc_seqnum(tdb);
316
317 unlock:
318         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
319         return tdb->last_error = ecode;
320 }
321
322 unsigned int tdb_get_flags(struct tdb_context *tdb)
323 {
324         return tdb->flags;
325 }
326
327 static bool inside_transaction(const struct tdb_context *tdb)
328 {
329         if (tdb->flags & TDB_VERSION1)
330                 return tdb->tdb1.transaction != NULL;
331         else
332                 return tdb->tdb2.transaction != NULL;
333 }
334
335 static bool readonly_changable(struct tdb_context *tdb, const char *caller)
336 {
337         if (inside_transaction(tdb)) {
338                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
339                                              TDB_LOG_USE_ERROR,
340                                              "%s: can't change"
341                                              " TDB_RDONLY inside transaction",
342                                              caller);
343                 return false;
344         }
345
346         if (tdb->file->allrecord_lock.count != 0
347             || tdb->file->num_lockrecs != 0) {
348                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
349                                              TDB_LOG_USE_ERROR,
350                                              "%s: can't change"
351                                              " TDB_RDONLY holding locks",
352                                              caller);
353                 return false;
354         }
355         return true;
356 }
357
358 void tdb_add_flag(struct tdb_context *tdb, unsigned flag)
359 {
360         if (tdb->flags & TDB_INTERNAL) {
361                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
362                                              TDB_LOG_USE_ERROR,
363                                              "tdb_add_flag: internal db");
364                 return;
365         }
366         switch (flag) {
367         case TDB_NOLOCK:
368                 tdb->flags |= TDB_NOLOCK;
369                 break;
370         case TDB_NOMMAP:
371                 tdb->flags |= TDB_NOMMAP;
372                 tdb_munmap(tdb->file);
373                 break;
374         case TDB_NOSYNC:
375                 tdb->flags |= TDB_NOSYNC;
376                 break;
377         case TDB_SEQNUM:
378                 tdb->flags |= TDB_SEQNUM;
379                 break;
380         case TDB_ALLOW_NESTING:
381                 tdb->flags |= TDB_ALLOW_NESTING;
382                 break;
383         case TDB_RDONLY:
384                 if (readonly_changable(tdb, "tdb_add_flag"))
385                         tdb->flags |= TDB_RDONLY;
386                 break;
387         default:
388                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
389                                              TDB_LOG_USE_ERROR,
390                                              "tdb_add_flag: Unknown flag %u",
391                                              flag);
392         }
393 }
394
395 void tdb_remove_flag(struct tdb_context *tdb, unsigned flag)
396 {
397         if (tdb->flags & TDB_INTERNAL) {
398                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
399                                              TDB_LOG_USE_ERROR,
400                                              "tdb_remove_flag: internal db");
401                 return;
402         }
403         switch (flag) {
404         case TDB_NOLOCK:
405                 tdb->flags &= ~TDB_NOLOCK;
406                 break;
407         case TDB_NOMMAP:
408                 tdb->flags &= ~TDB_NOMMAP;
409                 tdb_mmap(tdb);
410                 break;
411         case TDB_NOSYNC:
412                 tdb->flags &= ~TDB_NOSYNC;
413                 break;
414         case TDB_SEQNUM:
415                 tdb->flags &= ~TDB_SEQNUM;
416                 break;
417         case TDB_ALLOW_NESTING:
418                 tdb->flags &= ~TDB_ALLOW_NESTING;
419                 break;
420         case TDB_RDONLY:
421                 if ((tdb->open_flags & O_ACCMODE) == O_RDONLY) {
422                         tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
423                                                      TDB_LOG_USE_ERROR,
424                                                      "tdb_remove_flag: can't"
425                                                      " remove TDB_RDONLY on tdb"
426                                                      " opened with O_RDONLY");
427                         break;
428                 }
429                 if (readonly_changable(tdb, "tdb_remove_flag"))
430                         tdb->flags &= ~TDB_RDONLY;
431                 break;
432         default:
433                 tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL,
434                                              TDB_LOG_USE_ERROR,
435                                              "tdb_remove_flag: Unknown flag %u",
436                                              flag);
437         }
438 }
439
440 const char *tdb_errorstr(enum TDB_ERROR ecode)
441 {
442         /* Gcc warns if you miss a case in the switch, so use that. */
443         switch (ecode) {
444         case TDB_SUCCESS: return "Success";
445         case TDB_ERR_CORRUPT: return "Corrupt database";
446         case TDB_ERR_IO: return "IO Error";
447         case TDB_ERR_LOCK: return "Locking error";
448         case TDB_ERR_OOM: return "Out of memory";
449         case TDB_ERR_EXISTS: return "Record exists";
450         case TDB_ERR_EINVAL: return "Invalid parameter";
451         case TDB_ERR_NOEXIST: return "Record does not exist";
452         case TDB_ERR_RDONLY: return "write not permitted";
453         }
454         return "Invalid error code";
455 }
456
457 enum TDB_ERROR tdb_error(struct tdb_context *tdb)
458 {
459         return tdb->last_error;
460 }
461
462 enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb,
463                                enum TDB_ERROR ecode,
464                                enum tdb_log_level level,
465                                const char *fmt, ...)
466 {
467         char *message;
468         va_list ap;
469         size_t len;
470         /* tdb_open paths care about errno, so save it. */
471         int saved_errno = errno;
472
473         if (!tdb->log_fn)
474                 return ecode;
475
476         va_start(ap, fmt);
477         len = vasprintf(&message, fmt, ap);
478         va_end(ap);
479
480         if (len < 0) {
481                 tdb->log_fn(tdb, TDB_LOG_ERROR, TDB_ERR_OOM,
482                             "out of memory formatting message:", tdb->log_data);
483                 tdb->log_fn(tdb, level, ecode, fmt, tdb->log_data);
484         } else {
485                 tdb->log_fn(tdb, level, ecode, message, tdb->log_data);
486                 free(message);
487         }
488         errno = saved_errno;
489         return ecode;
490 }
491
492 enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
493                                  TDB_DATA key,
494                                  enum TDB_ERROR (*parse)(TDB_DATA k,
495                                                          TDB_DATA d,
496                                                          void *data),
497                                  void *data)
498 {
499         tdb_off_t off;
500         struct tdb_used_record rec;
501         struct hash_info h;
502         enum TDB_ERROR ecode;
503
504         off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
505         if (TDB_OFF_IS_ERR(off)) {
506                 return tdb->last_error = off;
507         }
508
509         if (!off) {
510                 ecode = TDB_ERR_NOEXIST;
511         } else {
512                 const void *dptr;
513                 dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
514                                        rec_data_length(&rec), false);
515                 if (TDB_PTR_IS_ERR(dptr)) {
516                         ecode = TDB_PTR_ERR(dptr);
517                 } else {
518                         TDB_DATA d = tdb_mkdata(dptr, rec_data_length(&rec));
519
520                         ecode = parse(key, d, data);
521                         tdb_access_release(tdb, dptr);
522                 }
523         }
524
525         tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
526         return tdb->last_error = ecode;
527 }
528
529 const char *tdb_name(const struct tdb_context *tdb)
530 {
531         return tdb->name;
532 }
533
534 int64_t tdb_get_seqnum(struct tdb_context *tdb)
535 {
536         tdb_off_t off = tdb_read_off(tdb, offsetof(struct tdb_header, seqnum));
537         if (TDB_OFF_IS_ERR(off))
538                 tdb->last_error = off;
539         else
540                 tdb->last_error = TDB_SUCCESS;
541         return off;
542 }
543         
544
545 int tdb_fd(const struct tdb_context *tdb)
546 {
547         return tdb->file->fd;
548 }