tdb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. #include "private.h"
  2. #include <ccan/tdb2/tdb2.h>
  3. #include <ccan/build_assert/build_assert.h>
  4. #include <ccan/likely/likely.h>
  5. #include <assert.h>
  6. /* The null return. */
  7. struct tdb_data tdb_null = { .dptr = NULL, .dsize = 0 };
  8. /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
  9. static struct tdb_context *tdbs = NULL;
  10. PRINTF_ATTRIBUTE(4, 5) static void
  11. null_log_fn(struct tdb_context *tdb,
  12. enum tdb_debug_level level, void *priv,
  13. const char *fmt, ...)
  14. {
  15. }
  16. static bool tdb_already_open(dev_t device, ino_t ino)
  17. {
  18. struct tdb_context *i;
  19. for (i = tdbs; i; i = i->next) {
  20. if (i->device == device && i->inode == ino) {
  21. return true;
  22. }
  23. }
  24. return false;
  25. }
  26. static uint64_t random_number(struct tdb_context *tdb)
  27. {
  28. int fd;
  29. uint64_t ret = 0;
  30. struct timeval now;
  31. fd = open("/dev/urandom", O_RDONLY);
  32. if (fd >= 0) {
  33. if (tdb_read_all(fd, &ret, sizeof(ret))) {
  34. tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
  35. "tdb_open: random from /dev/urandom\n");
  36. close(fd);
  37. return ret;
  38. }
  39. close(fd);
  40. }
  41. /* FIXME: Untested! Based on Wikipedia protocol description! */
  42. fd = open("/dev/egd-pool", O_RDWR);
  43. if (fd >= 0) {
  44. /* Command is 1, next byte is size we want to read. */
  45. char cmd[2] = { 1, sizeof(uint64_t) };
  46. if (write(fd, cmd, sizeof(cmd)) == sizeof(cmd)) {
  47. char reply[1 + sizeof(uint64_t)];
  48. int r = read(fd, reply, sizeof(reply));
  49. if (r > 1) {
  50. tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
  51. "tdb_open: %u random bytes from"
  52. " /dev/egd-pool\n", r-1);
  53. /* Copy at least some bytes. */
  54. memcpy(&ret, reply+1, r - 1);
  55. if (reply[0] == sizeof(uint64_t)
  56. && r == sizeof(reply)) {
  57. close(fd);
  58. return ret;
  59. }
  60. }
  61. }
  62. close(fd);
  63. }
  64. /* Fallback: pid and time. */
  65. gettimeofday(&now, NULL);
  66. ret = getpid() * 100132289ULL + now.tv_sec * 1000000ULL + now.tv_usec;
  67. tdb->log(tdb, TDB_DEBUG_TRACE, tdb->log_priv,
  68. "tdb_open: random from getpid and time\n");
  69. return ret;
  70. }
  71. struct new_database {
  72. struct tdb_header hdr;
  73. /* Initial free zone. */
  74. struct free_zone_header zhdr;
  75. tdb_off_t free[BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS) + 1];
  76. struct tdb_free_record frec;
  77. /* Rest up to 1 << INITIAL_ZONE_BITS is empty. */
  78. char space[(1 << INITIAL_ZONE_BITS)
  79. - sizeof(struct free_zone_header)
  80. - sizeof(tdb_off_t) * (BUCKETS_FOR_ZONE(INITIAL_ZONE_BITS)+1)
  81. - sizeof(struct tdb_free_record)];
  82. uint8_t tailer;
  83. /* Don't count final padding! */
  84. };
  85. /* initialise a new database */
  86. static int tdb_new_database(struct tdb_context *tdb, struct tdb_header *hdr)
  87. {
  88. /* We make it up in memory, then write it out if not internal */
  89. struct new_database newdb;
  90. unsigned int bucket, magic_len, dbsize;
  91. /* Don't want any extra padding! */
  92. dbsize = offsetof(struct new_database, tailer) + sizeof(newdb.tailer);
  93. /* Fill in the header */
  94. newdb.hdr.version = TDB_VERSION;
  95. newdb.hdr.hash_seed = random_number(tdb);
  96. newdb.hdr.hash_test = TDB_HASH_MAGIC;
  97. newdb.hdr.hash_test = tdb->khash(&newdb.hdr.hash_test,
  98. sizeof(newdb.hdr.hash_test),
  99. newdb.hdr.hash_seed,
  100. tdb->hash_priv);
  101. memset(newdb.hdr.reserved, 0, sizeof(newdb.hdr.reserved));
  102. /* Initial hashes are empty. */
  103. memset(newdb.hdr.hashtable, 0, sizeof(newdb.hdr.hashtable));
  104. /* Free is mostly empty... */
  105. newdb.zhdr.zone_bits = INITIAL_ZONE_BITS;
  106. memset(newdb.free, 0, sizeof(newdb.free));
  107. /* Create the single free entry. */
  108. newdb.frec.magic_and_meta = TDB_FREE_MAGIC | INITIAL_ZONE_BITS;
  109. newdb.frec.data_len = (sizeof(newdb.frec)
  110. - sizeof(struct tdb_used_record)
  111. + sizeof(newdb.space));
  112. /* Add it to the correct bucket. */
  113. bucket = size_to_bucket(INITIAL_ZONE_BITS, newdb.frec.data_len);
  114. newdb.free[bucket] = offsetof(struct new_database, frec);
  115. newdb.frec.next = newdb.frec.prev = 0;
  116. /* Clear free space to keep valgrind happy, and avoid leaking stack. */
  117. memset(newdb.space, 0, sizeof(newdb.space));
  118. /* Tailer contains maximum number of free_zone bits. */
  119. newdb.tailer = INITIAL_ZONE_BITS;
  120. /* Magic food */
  121. memset(newdb.hdr.magic_food, 0, sizeof(newdb.hdr.magic_food));
  122. strcpy(newdb.hdr.magic_food, TDB_MAGIC_FOOD);
  123. /* This creates an endian-converted database, as if read from disk */
  124. magic_len = sizeof(newdb.hdr.magic_food);
  125. tdb_convert(tdb,
  126. (char *)&newdb.hdr + magic_len,
  127. offsetof(struct new_database, space) - magic_len);
  128. *hdr = newdb.hdr;
  129. if (tdb->flags & TDB_INTERNAL) {
  130. tdb->map_size = dbsize;
  131. tdb->map_ptr = malloc(tdb->map_size);
  132. if (!tdb->map_ptr) {
  133. tdb->ecode = TDB_ERR_OOM;
  134. return -1;
  135. }
  136. memcpy(tdb->map_ptr, &newdb, tdb->map_size);
  137. return 0;
  138. }
  139. if (lseek(tdb->fd, 0, SEEK_SET) == -1)
  140. return -1;
  141. if (ftruncate(tdb->fd, 0) == -1)
  142. return -1;
  143. if (!tdb_pwrite_all(tdb->fd, &newdb, dbsize, 0)) {
  144. tdb->ecode = TDB_ERR_IO;
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. struct tdb_context *tdb_open(const char *name, int tdb_flags,
  150. int open_flags, mode_t mode,
  151. union tdb_attribute *attr)
  152. {
  153. struct tdb_context *tdb;
  154. struct stat st;
  155. int save_errno;
  156. uint64_t hash_test;
  157. unsigned v;
  158. struct tdb_header hdr;
  159. tdb = malloc(sizeof(*tdb));
  160. if (!tdb) {
  161. /* Can't log this */
  162. errno = ENOMEM;
  163. goto fail;
  164. }
  165. tdb->name = NULL;
  166. tdb->map_ptr = NULL;
  167. tdb->direct_access = 0;
  168. tdb->fd = -1;
  169. tdb->map_size = sizeof(struct tdb_header);
  170. tdb->ecode = TDB_SUCCESS;
  171. tdb->flags = tdb_flags;
  172. tdb->log = null_log_fn;
  173. tdb->log_priv = NULL;
  174. tdb->transaction = NULL;
  175. tdb_hash_init(tdb);
  176. /* last_zone will be set below. */
  177. tdb_io_init(tdb);
  178. tdb_lock_init(tdb);
  179. while (attr) {
  180. switch (attr->base.attr) {
  181. case TDB_ATTRIBUTE_LOG:
  182. tdb->log = attr->log.log_fn;
  183. tdb->log_priv = attr->log.log_private;
  184. break;
  185. case TDB_ATTRIBUTE_HASH:
  186. tdb->khash = attr->hash.hash_fn;
  187. tdb->hash_priv = attr->hash.hash_private;
  188. break;
  189. default:
  190. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  191. "tdb_open: unknown attribute type %u\n",
  192. attr->base.attr);
  193. errno = EINVAL;
  194. goto fail;
  195. }
  196. attr = attr->base.next;
  197. }
  198. if ((open_flags & O_ACCMODE) == O_WRONLY) {
  199. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  200. "tdb_open: can't open tdb %s write-only\n", name);
  201. errno = EINVAL;
  202. goto fail;
  203. }
  204. if ((open_flags & O_ACCMODE) == O_RDONLY) {
  205. tdb->read_only = true;
  206. /* read only databases don't do locking */
  207. tdb->flags |= TDB_NOLOCK;
  208. tdb->mmap_flags = PROT_READ;
  209. } else {
  210. tdb->read_only = false;
  211. tdb->mmap_flags = PROT_READ | PROT_WRITE;
  212. }
  213. /* internal databases don't need any of the rest. */
  214. if (tdb->flags & TDB_INTERNAL) {
  215. tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP);
  216. if (tdb_new_database(tdb, &hdr) != 0) {
  217. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  218. "tdb_open: tdb_new_database failed!");
  219. goto fail;
  220. }
  221. tdb_convert(tdb, &hdr.hash_seed, sizeof(hdr.hash_seed));
  222. tdb->hash_seed = hdr.hash_seed;
  223. tdb_zone_init(tdb);
  224. return tdb;
  225. }
  226. if ((tdb->fd = open(name, open_flags, mode)) == -1) {
  227. tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
  228. "tdb_open: could not open file %s: %s\n",
  229. name, strerror(errno));
  230. goto fail; /* errno set by open(2) */
  231. }
  232. /* on exec, don't inherit the fd */
  233. v = fcntl(tdb->fd, F_GETFD, 0);
  234. fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC);
  235. /* ensure there is only one process initialising at once */
  236. if (tdb_lock_open(tdb) == -1) {
  237. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  238. "tdb_open: failed to get open lock on %s: %s\n",
  239. name, strerror(errno));
  240. goto fail; /* errno set by tdb_brlock */
  241. }
  242. if (!tdb_pread_all(tdb->fd, &hdr, sizeof(hdr), 0)
  243. || strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
  244. if (!(open_flags & O_CREAT) || tdb_new_database(tdb, &hdr) == -1) {
  245. if (errno == 0) {
  246. errno = EIO; /* ie bad format or something */
  247. }
  248. goto fail;
  249. }
  250. } else if (hdr.version != TDB_VERSION) {
  251. if (hdr.version == bswap_64(TDB_VERSION))
  252. tdb->flags |= TDB_CONVERT;
  253. else {
  254. /* wrong version */
  255. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  256. "tdb_open: %s is unknown version 0x%llx\n",
  257. name, (long long)hdr.version);
  258. errno = EIO;
  259. goto fail;
  260. }
  261. }
  262. tdb_convert(tdb, &hdr, sizeof(hdr));
  263. tdb->hash_seed = hdr.hash_seed;
  264. hash_test = TDB_HASH_MAGIC;
  265. hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
  266. if (hdr.hash_test != hash_test) {
  267. /* wrong hash variant */
  268. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  269. "tdb_open: %s uses a different hash function\n",
  270. name);
  271. errno = EIO;
  272. goto fail;
  273. }
  274. if (fstat(tdb->fd, &st) == -1)
  275. goto fail;
  276. /* Is it already in the open list? If so, fail. */
  277. if (tdb_already_open(st.st_dev, st.st_ino)) {
  278. /* FIXME */
  279. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  280. "tdb_open: %s (%d,%d) is already open in this process\n",
  281. name, (int)st.st_dev, (int)st.st_ino);
  282. errno = EBUSY;
  283. goto fail;
  284. }
  285. tdb->name = strdup(name);
  286. if (!tdb->name) {
  287. errno = ENOMEM;
  288. goto fail;
  289. }
  290. tdb->device = st.st_dev;
  291. tdb->inode = st.st_ino;
  292. tdb_unlock_open(tdb);
  293. /* This make sure we have current map_size and mmap. */
  294. tdb->methods->oob(tdb, tdb->map_size + 1, true);
  295. /* Now we can pick a random free zone to start from. */
  296. if (tdb_zone_init(tdb) == -1)
  297. goto fail;
  298. tdb->next = tdbs;
  299. tdbs = tdb;
  300. return tdb;
  301. fail:
  302. save_errno = errno;
  303. if (!tdb)
  304. return NULL;
  305. #ifdef TDB_TRACE
  306. close(tdb->tracefd);
  307. #endif
  308. if (tdb->map_ptr) {
  309. if (tdb->flags & TDB_INTERNAL) {
  310. free(tdb->map_ptr);
  311. } else
  312. tdb_munmap(tdb);
  313. }
  314. free((char *)tdb->name);
  315. if (tdb->fd != -1)
  316. if (close(tdb->fd) != 0)
  317. tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
  318. "tdb_open: failed to close tdb->fd"
  319. " on error!\n");
  320. free(tdb);
  321. errno = save_errno;
  322. return NULL;
  323. }
  324. /* FIXME: modify, don't rewrite! */
  325. static int update_rec_hdr(struct tdb_context *tdb,
  326. tdb_off_t off,
  327. tdb_len_t keylen,
  328. tdb_len_t datalen,
  329. struct tdb_used_record *rec,
  330. uint64_t h)
  331. {
  332. uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec);
  333. if (set_header(tdb, rec, keylen, datalen, keylen + dataroom, h,
  334. rec_zone_bits(rec)))
  335. return -1;
  336. return tdb_write_convert(tdb, off, rec, sizeof(*rec));
  337. }
  338. /* Returns -1 on error, 0 on OK */
  339. static int replace_data(struct tdb_context *tdb,
  340. struct hash_info *h,
  341. struct tdb_data key, struct tdb_data dbuf,
  342. tdb_off_t old_off, tdb_len_t old_room,
  343. unsigned old_zone,
  344. bool growing)
  345. {
  346. tdb_off_t new_off;
  347. /* Allocate a new record. */
  348. new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, growing);
  349. if (unlikely(new_off == TDB_OFF_ERR))
  350. return -1;
  351. /* We didn't like the existing one: remove it. */
  352. if (old_off) {
  353. add_free_record(tdb, old_zone, old_off,
  354. sizeof(struct tdb_used_record)
  355. + key.dsize + old_room);
  356. if (replace_in_hash(tdb, h, new_off) == -1)
  357. return -1;
  358. } else {
  359. if (add_to_hash(tdb, h, new_off) == -1)
  360. return -1;
  361. }
  362. new_off += sizeof(struct tdb_used_record);
  363. if (tdb->methods->write(tdb, new_off, key.dptr, key.dsize) == -1)
  364. return -1;
  365. new_off += key.dsize;
  366. if (tdb->methods->write(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1)
  367. return -1;
  368. /* FIXME: tdb_increment_seqnum(tdb); */
  369. return 0;
  370. }
  371. int tdb_store(struct tdb_context *tdb,
  372. struct tdb_data key, struct tdb_data dbuf, int flag)
  373. {
  374. struct hash_info h;
  375. tdb_off_t off;
  376. tdb_len_t old_room = 0;
  377. struct tdb_used_record rec;
  378. int ret;
  379. off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
  380. if (unlikely(off == TDB_OFF_ERR))
  381. return -1;
  382. /* Now we have lock on this hash bucket. */
  383. if (flag == TDB_INSERT) {
  384. if (off) {
  385. tdb->ecode = TDB_ERR_EXISTS;
  386. goto fail;
  387. }
  388. } else {
  389. if (off) {
  390. old_room = rec_data_length(&rec)
  391. + rec_extra_padding(&rec);
  392. if (old_room >= dbuf.dsize) {
  393. /* Can modify in-place. Easy! */
  394. if (update_rec_hdr(tdb, off,
  395. key.dsize, dbuf.dsize,
  396. &rec, h.h))
  397. goto fail;
  398. if (tdb->methods->write(tdb, off + sizeof(rec)
  399. + key.dsize,
  400. dbuf.dptr, dbuf.dsize))
  401. goto fail;
  402. tdb_unlock_hashes(tdb, h.hlock_start,
  403. h.hlock_range, F_WRLCK);
  404. return 0;
  405. }
  406. /* FIXME: See if right record is free? */
  407. } else {
  408. if (flag == TDB_MODIFY) {
  409. /* if the record doesn't exist and we
  410. are in TDB_MODIFY mode then we should fail
  411. the store */
  412. tdb->ecode = TDB_ERR_NOEXIST;
  413. goto fail;
  414. }
  415. }
  416. }
  417. /* If we didn't use the old record, this implies we're growing. */
  418. ret = replace_data(tdb, &h, key, dbuf, off, old_room,
  419. rec_zone_bits(&rec), off != 0);
  420. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  421. return ret;
  422. fail:
  423. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  424. return -1;
  425. }
  426. int tdb_append(struct tdb_context *tdb,
  427. struct tdb_data key, struct tdb_data dbuf)
  428. {
  429. struct hash_info h;
  430. tdb_off_t off;
  431. struct tdb_used_record rec;
  432. tdb_len_t old_room = 0, old_dlen;
  433. unsigned char *newdata;
  434. struct tdb_data new_dbuf;
  435. int ret;
  436. off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
  437. if (unlikely(off == TDB_OFF_ERR))
  438. return -1;
  439. if (off) {
  440. old_dlen = rec_data_length(&rec);
  441. old_room = old_dlen + rec_extra_padding(&rec);
  442. /* Fast path: can append in place. */
  443. if (rec_extra_padding(&rec) >= dbuf.dsize) {
  444. if (update_rec_hdr(tdb, off, key.dsize,
  445. old_dlen + dbuf.dsize, &rec, h.h))
  446. goto fail;
  447. off += sizeof(rec) + key.dsize + old_dlen;
  448. if (tdb->methods->write(tdb, off, dbuf.dptr,
  449. dbuf.dsize) == -1)
  450. goto fail;
  451. /* FIXME: tdb_increment_seqnum(tdb); */
  452. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range,
  453. F_WRLCK);
  454. return 0;
  455. }
  456. /* FIXME: Check right record free? */
  457. /* Slow path. */
  458. newdata = malloc(key.dsize + old_dlen + dbuf.dsize);
  459. if (!newdata) {
  460. tdb->ecode = TDB_ERR_OOM;
  461. tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
  462. "tdb_append: cannot allocate %llu bytes!\n",
  463. (long long)key.dsize + old_dlen + dbuf.dsize);
  464. goto fail;
  465. }
  466. if (tdb->methods->read(tdb, off + sizeof(rec) + key.dsize,
  467. newdata, old_dlen) != 0) {
  468. free(newdata);
  469. goto fail;
  470. }
  471. memcpy(newdata + old_dlen, dbuf.dptr, dbuf.dsize);
  472. new_dbuf.dptr = newdata;
  473. new_dbuf.dsize = old_dlen + dbuf.dsize;
  474. } else {
  475. newdata = NULL;
  476. new_dbuf = dbuf;
  477. }
  478. /* If they're using tdb_append(), it implies they're growing record. */
  479. ret = replace_data(tdb, &h, key, new_dbuf, off,
  480. old_room, rec_zone_bits(&rec), true);
  481. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  482. free(newdata);
  483. return ret;
  484. fail:
  485. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  486. return -1;
  487. }
  488. struct tdb_data tdb_fetch(struct tdb_context *tdb, struct tdb_data key)
  489. {
  490. tdb_off_t off;
  491. struct tdb_used_record rec;
  492. struct hash_info h;
  493. struct tdb_data ret;
  494. off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
  495. if (unlikely(off == TDB_OFF_ERR))
  496. return tdb_null;
  497. if (!off) {
  498. tdb->ecode = TDB_ERR_NOEXIST;
  499. ret = tdb_null;
  500. } else {
  501. ret.dsize = rec_data_length(&rec);
  502. ret.dptr = tdb_alloc_read(tdb, off + sizeof(rec) + key.dsize,
  503. ret.dsize);
  504. }
  505. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_RDLCK);
  506. return ret;
  507. }
  508. int tdb_delete(struct tdb_context *tdb, struct tdb_data key)
  509. {
  510. tdb_off_t off;
  511. struct tdb_used_record rec;
  512. struct hash_info h;
  513. off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL);
  514. if (unlikely(off == TDB_OFF_ERR))
  515. return -1;
  516. if (!off) {
  517. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  518. tdb->ecode = TDB_ERR_NOEXIST;
  519. return -1;
  520. }
  521. if (delete_from_hash(tdb, &h) == -1)
  522. goto unlock_err;
  523. /* Free the deleted entry. */
  524. if (add_free_record(tdb, rec_zone_bits(&rec), off,
  525. sizeof(struct tdb_used_record)
  526. + rec_key_length(&rec)
  527. + rec_data_length(&rec)
  528. + rec_extra_padding(&rec)) != 0)
  529. goto unlock_err;
  530. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  531. return 0;
  532. unlock_err:
  533. tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK);
  534. return -1;
  535. }
  536. int tdb_close(struct tdb_context *tdb)
  537. {
  538. struct tdb_context **i;
  539. int ret = 0;
  540. /* FIXME:
  541. if (tdb->transaction) {
  542. tdb_transaction_cancel(tdb);
  543. }
  544. */
  545. tdb_trace(tdb, "tdb_close");
  546. if (tdb->map_ptr) {
  547. if (tdb->flags & TDB_INTERNAL)
  548. free(tdb->map_ptr);
  549. else
  550. tdb_munmap(tdb);
  551. }
  552. free((char *)tdb->name);
  553. if (tdb->fd != -1) {
  554. ret = close(tdb->fd);
  555. tdb->fd = -1;
  556. }
  557. free(tdb->lockrecs);
  558. /* Remove from contexts list */
  559. for (i = &tdbs; *i; i = &(*i)->next) {
  560. if (*i == tdb) {
  561. *i = tdb->next;
  562. break;
  563. }
  564. }
  565. #ifdef TDB_TRACE
  566. close(tdb->tracefd);
  567. #endif
  568. free(tdb);
  569. return ret;
  570. }
  571. enum TDB_ERROR tdb_error(struct tdb_context *tdb)
  572. {
  573. return tdb->ecode;
  574. }
  575. const char *tdb_errorstr(struct tdb_context *tdb)
  576. {
  577. /* Gcc warns if you miss a case in the switch, so use that. */
  578. switch (tdb->ecode) {
  579. case TDB_SUCCESS: return "Success";
  580. case TDB_ERR_CORRUPT: return "Corrupt database";
  581. case TDB_ERR_IO: return "IO Error";
  582. case TDB_ERR_LOCK: return "Locking error";
  583. case TDB_ERR_OOM: return "Out of memory";
  584. case TDB_ERR_EXISTS: return "Record exists";
  585. case TDB_ERR_NESTING: return "Transaction already started";
  586. case TDB_ERR_EINVAL: return "Invalid parameter";
  587. case TDB_ERR_NOEXIST: return "Record does not exist";
  588. case TDB_ERR_RDONLY: return "write not permitted";
  589. }
  590. return "Invalid error code";
  591. }