Codebase list cyrus-imapd / upstream/3.0.8 lib / cyrusdb_lmdb.c
upstream/3.0.8

Tree @upstream/3.0.8 (Download .tar.gz)

cyrusdb_lmdb.c @upstream/3.0.8raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/*  cyrusdb_lmdb: a backend built on Lightning Memory-Mapped Database (LMDB)
 *
 * Copyright (c) 2016 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any legal
 *    details, please contact
 *      Carnegie Mellon University
 *      Center for Technology Transfer and Enterprise Creation
 *      4615 Forbes Avenue
 *      Suite 302
 *      Pittsburgh, PA  15213
 *      (412) 268-7393, fax: (412) 268-7395
 *      innovation@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 * This backend implements the Cyrus DB API on top of the Lightning
 * Memory-Mapped Database (LMDB).
 *
 * These two APIs work together nicely most of the time, but there are
 * a few caveats:
 *
 * - LMDB requires database environments to be set to a (user-configurable)
 *   maximum size. This backend sets a reasonable high default size (see
 *   maxdbsize). Cyrus installations may override this by setting the
 *   environment variable CYRUSDB_LMDB_MAXSIZE. The value of this variable
 *   must be an integer, optionally followed (without space) by "mb" or "gb"
 *   to define the maximum size in bytes, megabytes or gigabytes. The size
 *   should be a multiple of the OS page size.
 *
 * - Callbacks within foreach might call a database engine without a
 *   transaction, although the foreach operates within an existing or
 *   newly created transaction. But LMDB only allows one unnested
 *   transaction per thread.
 *   As a consequence, the database engine has to keep track of the
 *   current transaction and reuses that one if no transaction has been
 *   supplied by caller. This is similar to the inner workings of the
 *   skiplist and twoskip backends.
 *
 * - Callbacks within foreach might update the currently seeked data
 *   item. To protect the database from corruption, the cursor is
 *   invalidated and reseeked after the operation, causing foreach()
 *   performance degrade to O(m*log n), where m denotes the iteration
 *   count and n the number of items in the database.
 *
 * - LMDB transactions operate on database environments and database
 *   environments may contain multiple databases. A single writer
 *   locks all other operations within this environment. To avoid
 *   (already observed) deadlocks between multiple open databases
 *   of Cyrus DB callers, this backend creates an unique environment
 *   for each database.
 *
 */

/* TODO:
 * - support optional resize in myopen: e.g. resize if DB is >80% full
 */

#include <config.h>

#include <assert.h>
#include <errno.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "bsearch.h"
#include "cyrusdb.h"
#include "hash.h"
#include "xstrlcpy.h"
#include "util.h"

#include "lmdb.h"

struct txn {
    MDB_dbi dbi;
    MDB_txn *mtxn;
};

struct dbengine {
    MDB_env *env;     /* mdb environment, one per database */
    char *fname;      /* external filename of the database */
    int flags;        /* create flags */
    void *data;       /* allocated buffer for fetched data */
    size_t datalen;   /* bytes allocated in data */
    struct txn *tid;  /* master transaction, if any. See begin_txn. */
    MDB_cursor *mcur; /* current cursor of foreach(), if any */
};

struct dblist {
    struct dbengine *db;
    struct dblist *next;
};

/* Default environment size is 512MB */
static size_t maxdbsize = 1 << 29;

#define DEBUG 0

#if DEBUG
#    define PDEBUG(fmt, args...) syslog(LOG_DEBUG, fmt, ## args)
#else
#    define PDEBUG(fmt, args...) /* do nothing */
#endif

/* Global list of open dbs for internal bookkeeping */
static struct dblist *dbs = NULL;

static void register_db(struct dbengine *db)
{
    struct dblist *l = xzmalloc(sizeof(struct dblist));
    assert(db);
    l->db = db;
    l->next = dbs;
    dbs = l;
}

static void unregister_db(struct dbengine *db)
{
    /* Caller must make sure that memory pointed to by db is freed */
    struct dblist *l = dbs;
    assert(db && dbs);

    if (dbs->db == db) {
        /* Reset head of list */
        dbs = l->next;
        free(l);
        return;
    }
    while (l->next) {
        /* Purge db from list */
        if (l->next->db == db) {
            struct dblist *tmp = l->next;
            l->next = l->next->next;
            free(tmp);
            return;
        }
        l = l->next;
    }
    /* Should never be reached */
    assert(0);
}

static int my_mdberror(int mr) {
    switch (mr) {
        case MDB_MAP_FULL:
            return CYRUSDB_FULL;
        case MDB_NOTFOUND:
            return CYRUSDB_NOTFOUND;
        case MDB_MAP_RESIZED:
            /* Should be CYRUSDB_AGAIN, but resize requires to reopen env */
            return CYRUSDB_IOERROR;
        case MDB_SUCCESS:
            return CYRUSDB_OK;
        default:
            return CYRUSDB_INTERNAL;
    }
}

static int mboxcmp(const MDB_val *a, const MDB_val *b);

static int bufferval(struct dbengine *db, MDB_val val, const char **dst, size_t *dstlen)
{
    assert(db && val.mv_data);

    /* Allocate at least 1 byte so data never points to NULL */
    db->data = xrealloc(db->data, val.mv_size + 1);
    memcpy(db->data, val.mv_data, val.mv_size);
    *dst = db->data;
    *dstlen = val.mv_size;
    db->datalen = val.mv_size;

    return CYRUSDB_OK;
}

static int commit_txn(struct dbengine *db, struct txn *tid)
{
    int mr;

    assert(db && tid);

    PDEBUG("cyrusdb_lmdb(%s)[commit_txn] tid=%p", db->fname, tid);

    mr = mdb_txn_commit(tid->mtxn);
    free(tid);
    if (tid == db->tid) db->tid = NULL;

    if (mr) syslog(LOG_ERR, "lmdb: %s", mdb_strerror(mr));
    return mr ? my_mdberror(mr) : CYRUSDB_OK;
}

static int abort_txn(struct dbengine *db, struct txn *tid)
{
    assert(db && tid);

    PDEBUG("cyrusdb_lmdb(%s)[abort_txn] tid=%p", db->fname, tid);

    mdb_txn_abort(tid->mtxn);
    free(tid);
    if (tid == db->tid) db->tid = NULL;

    return CYRUSDB_OK;
}

static int begin_txn(struct dbengine *db, struct txn **tidptr, int readonly)
{
    struct txn *tid = xzmalloc(sizeof(struct txn));
    int mflags, mr, r;
    struct MDB_txn *parent = NULL;

    assert(db && tidptr);

    PDEBUG("cyrusdb_lmdb(%s)[begin_txn] readonly=%d tidptr=%p *tidptr=%p",
            db->fname, readonly, tidptr, tidptr ? *tidptr : NULL);

    /* Read-only transactions may only be master transactions and do
     * not allow nested transactions. */
    readonly = !db->tid ? readonly : 0;

    /*
     * Hacky workaround, similar to skiplist
     *
     * If no transaction was passed, but we're in a transaction,
     * then create a nested exception within the current main
     * transaction.
     *
     * Note that transactions are always either the main transaction,
     * or a direct descendant of it. There are no deeper transaction
     * levels supported (although LMDB supports them).
     */
    if (db->tid) {
        parent = db->tid->mtxn;
    }

    /* Begin a new LMDB transaction */
    mr = mdb_txn_begin(db->env, parent, readonly ? MDB_RDONLY : 0, &tid->mtxn);
    if (mr) goto fail;

    /* Open the database */
    mflags = db->flags & CYRUSDB_CREATE ? MDB_CREATE : 0;
    mr = mdb_dbi_open(tid->mtxn, NULL /*name*/, mflags, &tid->dbi);
    if (mr) goto fail;

    if (db->flags & CYRUSDB_MBOXSORT) {
        /* Set mboxsort order */
        mr = mdb_set_compare(tid->mtxn, tid->dbi, mboxcmp);
        if (mr) goto fail;
    }

    if (!db->tid) {
        /* Set the master transaction */
        db->tid = tid;
    }
    *tidptr = tid;
    return CYRUSDB_OK;

fail:
    r = my_mdberror(mr);
    if (tid->mtxn) abort_txn(db, tid);
    syslog(LOG_ERR, "cyrusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
    return r;
}

/*
 * Get or create an existing transaction from tidptr, and store it in _tidptr.
 */
static int getorset_txn(struct dbengine *db,
                        struct txn **tidptr,
                        struct txn **_tidptr,
                        int readonly)
{
    struct txn *tid;
    int r;

    assert(_tidptr);
    tid = *_tidptr;

    if (!tidptr || !*tidptr) {
        /* Create a new transaction */
        if ((r = begin_txn(db, &tid, readonly))) {
            *_tidptr = NULL;
            return r;
        }
    } else {
        /* Reuse the existing transaction */
        tid = *tidptr;
    }
    if (tidptr) {
        /* Refresh tidptr */
        *tidptr = tid;
    }
    /* Return the transaction */
    *_tidptr = tid;

    PDEBUG("cyrusdb_lmdb(%s)[getorset_txn] tidptr=%p *tidptr=%p _tidptr=%p *_tidptr=%p ",
            db->fname, tidptr, tidptr ? *tidptr : NULL, _tidptr, _tidptr ? *_tidptr : NULL);

    return CYRUSDB_OK;
}

static int mboxcmp(const MDB_val *a, const MDB_val *b)
{
    return bsearch_ncompare_mbox((const char *) a->mv_data, a->mv_size,
                                 (const char *) b->mv_data, b->mv_size);
}

static int my_mkparentdir(const char *fname)
{
    struct buf buf = BUF_INITIALIZER;
    int r;

    buf_setcstr(&buf, fname);
    r = cyrus_mkdir(buf_cstring(&buf), 0755);
    buf_free(&buf);
    return r;
}

static int my_stat(const char *fname)
{
    struct stat sb;
    int r;

    r = stat(fname, &sb);
    if (r < 0)
        r = -errno;
    return r;
}

static int myopen(const char *fname, int flags, struct dbengine **ret,
                  struct txn **tidptr)
{
    struct dbengine *db;
    int r, mr = 0;

    PDEBUG("cyrusdb_lmdb(%s)[open] flags=0x%x tidptr=%p *tidptr=%p",
            fname, flags, tidptr, tidptr ? *tidptr : NULL);
    assert(fname && ret);

    /* Create a new database engine */
    db = (struct dbengine *) xzmalloc(sizeof(struct dbengine));
    db->fname = xstrdup(fname);
    db->flags = flags;

    /* Assert that either the parent directory or the database exists */
    r = flags & CYRUSDB_CREATE ? my_mkparentdir(fname) : my_stat(fname);
    if (r) {
        /* Both my_stat and my_mkparentdir preserve errno */
        r = errno == ENOENT ? CYRUSDB_NOTFOUND : CYRUSDB_IOERROR;
        goto fail;
    }

    /* Create the environment for this database */
    mr = mdb_env_create(&db->env);
    if (mr) {
        r = CYRUSDB_INTERNAL;
        goto fail;
    }

    /* Size (or resize) the environment */
    mr = mdb_env_set_mapsize(db->env, maxdbsize);
    if (mr) {
        r = CYRUSDB_INTERNAL;
        goto fail;
    }

    /* Open the environment */
    mr = mdb_env_open(db->env, fname, MDB_NOSUBDIR, 0600);
    if (mr) {
        r = CYRUSDB_IOERROR;
        goto fail;
    }

    /* Lock the database, if requested */
    if (tidptr) {
        r = begin_txn(db, tidptr, 0);
        if (r) goto fail;
    }

    /* Keep database for internal bookkeeping */
    register_db(db);

    /* Return the database handle */
    *ret = db;
    return CYRUSDB_OK;

fail:
    if (mr)
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
    if (db->env)
        mdb_env_close(db->env);
    free(db->fname);
    free(db);
    return r;
}

static void close_db(struct dbengine *db)
{
    assert(db);

    if (db->tid) {
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): stray transaction %p",
                db->fname, db->tid);
        abort_txn(db, db->tid);
    }
    if (db->env) {
        mdb_env_close(db->env);
    }
    if (db->data) {
        free(db->data);
    }
    free(db->fname);
    free(db);
}

static int myclose(struct dbengine *db)
{
    assert(db);

    if (db->mcur) {
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): close within foreach", db->fname);
        return CYRUSDB_INTERNAL;
    }

    PDEBUG("cyrusdb_lmdb(%s)[close]", db->fname);
    close_db(db);
    unregister_db(db);
    return CYRUSDB_OK;
}

static int fetch(struct dbengine *db, const char *key, size_t keylen,
                 const char **data, size_t *datalen, struct txn **tidptr)
{
    MDB_val mkey, mval;
    struct txn *tid;
    int r, r2 = 0, mr;

    PDEBUG("cyrusdb_lmdb(%s)[fetch] tidptr=%p *tidptr=%p key='%.*s'",
            db->fname, tidptr, tidptr ? *tidptr : NULL, (int) keylen, key);
    assert(db && key);

    mkey.mv_data = (void*) key;
    mkey.mv_size = keylen;

    /* Open or reuse transaction */
    r = getorset_txn(db, tidptr, &tid, !tidptr /*readonly*/);
    if (r) goto fail;

    mr = mdb_get(tid->mtxn, tid->dbi, &mkey, &mval);
    if (mr == MDB_NOTFOUND) {
        /* That's not an error */
        r = CYRUSDB_NOTFOUND;
        if (datalen) *datalen = 0;
        if (data) *data = NULL;
    } else if (mr) {
        /* That's an error */
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
        r = CYRUSDB_INTERNAL;
        goto fail;
    } else if (data && datalen) {
        /* Cache the fetched data from LMDB memory in own buffer */
        r = bufferval(db, mval, data, datalen);
        if (r) goto fail;
    }

    /* Commit or export the transaction */
    if (!tidptr) {
        r2 = commit_txn(db, tid);
        if (r2) goto fail;
    } else {
        *tidptr = tid;
        r2 = CYRUSDB_OK;
    }

    return r ? r : r2;

fail:
    if (tid && (!tidptr || !*tidptr)) abort_txn(db, tid);
    return r ? r : r2;
}

static int foreach(struct dbengine *db, const char *prefix, size_t prefixlen,
                   foreach_p *p, foreach_cb *cb, void *rock, struct txn **tidptr)
{
    int r, r2, mr = 0;
    struct txn *tid = NULL;
    MDB_val mkey, mval;
    enum MDB_cursor_op op;
    struct buf cur = BUF_INITIALIZER;

    PDEBUG("cyrusdb_lmdb(%s)[foreach] tidptr=%p *tidptr=%p prefix='%.*s'",
            db->fname, tidptr, tidptr ? *tidptr : NULL, (int) prefixlen, prefix);
    assert(db);

    /* Open or reuse transaction */
    r = getorset_txn(db, tidptr, &tid, 0);
    if (r) goto fail;

    mr = mdb_cursor_open(tid->mtxn, tid->dbi, &db->mcur);
    if (mr) goto fail;

    /* Normalize and set prefix for search */
    if (prefix && !prefixlen) {
        prefix = NULL;
    }

    /* Initialize cursor */
    mkey.mv_data = (void*) prefix;
    mkey.mv_size = prefix ? prefixlen : 0;
    op = prefix ? MDB_SET_RANGE : MDB_FIRST;
    mr = mdb_cursor_get(db->mcur, &mkey, &mval, op);

    /* Iterate cursor until no records or out of range */
    while (!mr) {
        if (prefixlen && (mkey.mv_size < prefixlen))
            break;

        if (prefix && memcmp(mkey.mv_data, prefix, prefixlen))
            break;

        if (!p || p(rock, mkey.mv_data, mkey.mv_size, mval.mv_data, mval.mv_size)) {
            /* Cache the current position in local memory */
            buf_setmap(&cur, mkey.mv_data, mkey.mv_size);

            r = cb(rock, cur.s, cur.len, mval.mv_data, mval.mv_size);
            if (r) break;

            if (db->mcur == NULL) {
                /* An update has invalidated the cursor. Reseek cursor. */
                mr = mdb_cursor_open(tid->mtxn, tid->dbi, &db->mcur);
                if (mr) break;

                mkey.mv_data = cur.s;
                mkey.mv_size = cur.len;
                mr = mdb_cursor_get(db->mcur, &mkey, &mval, MDB_SET_RANGE);
                if (mr) break;

                if (mkey.mv_size != cur.len || memcmp(mkey.mv_data, cur.s, cur.len)) {
                    /* The current position has been deleted. */
                    continue;
                }
            }
        }

        /* Advance cursor */
        mr = mdb_cursor_get(db->mcur, &mkey, &mval, MDB_NEXT);
    }

    if (mr && mr != MDB_NOTFOUND)
        goto fail;

    if (db->mcur) {
        mdb_cursor_close(db->mcur);
        db->mcur = NULL;
    }
    buf_free(&cur);

    /* Export or commit transaction */
    r2 = tidptr ? CYRUSDB_OK : commit_txn(db, tid);

    return r ? r : r2;

fail:
    if (db->mcur) {
        mdb_cursor_close(db->mcur);
        db->mcur = NULL;
    }
    buf_free(&cur);

    if (tid && (!tidptr || !*tidptr))
        abort_txn(db, tid);
    if (mr) {
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
        r = my_mdberror(mr);
    }
    return r;
}

static int put(struct dbengine *db, const char *key, size_t keylen,
               const char *data, size_t datalen, struct txn **tidptr, int mflags)
{
    MDB_val mkey, mval;
    struct txn *tid;
    int r, mr;

    mkey.mv_data = (void*) key;
    mkey.mv_size = keylen;
    mval.mv_data = (void*) data;
    mval.mv_size = datalen;

    /* Invalidate cursor */
    if (db->mcur) {
        mdb_cursor_close(db->mcur);
        db->mcur = NULL;
    }

    /* Open or reuse transaction */
    r = getorset_txn(db, tidptr, &tid, 0);
    if (r) goto fail;

    mr = mdb_put(tid->mtxn, tid->dbi, &mkey, &mval, mflags);
    if (mr) {
        /* Return the appropriate error code for existing key overwrites */
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
        r = (mr == MDB_KEYEXIST && (mflags & MDB_NOOVERWRITE)) ? \
            CYRUSDB_EXISTS : CYRUSDB_INTERNAL;
        goto fail;
    }

    /* Commit or export the transaction */
    if (!tidptr) {
        r = commit_txn(db, tid);
        if (r) goto fail;
    } else {
        *tidptr = tid;
    }

    return CYRUSDB_OK;

fail:
    if (tid && (!tidptr || !*tidptr)) abort_txn(db, tid);
    return r;
}

static int create(struct dbengine *db, const char *key, size_t keylen,
                  const char *data, size_t datalen, struct txn **tidptr)
{

    PDEBUG("cyrusdb_lmdb(%s)[create] tidptr=%p *tidptr=%p key='%.*s' data='%.*s'",
            db->fname, tidptr, tidptr ? *tidptr : NULL,
            (int) keylen, key, (int) datalen, data);
    return put(db, key, keylen, data, datalen, tidptr, MDB_NOOVERWRITE);
}

static int store(struct dbengine *db, const char *key, size_t keylen,
                  const char *data, size_t datalen, struct txn **tidptr)
{
    PDEBUG("cyrusdb_lmdb(%s)[store] tidptr=%p *tidptr=%p key='%.*s' data='%.*s'",
            db->fname, tidptr, tidptr ? *tidptr : NULL,
            (int) keylen, key, (int) datalen, data);
    return put(db, key, keylen, data, datalen, tidptr, 0);
}

static int delete(struct dbengine *db, const char *key, size_t keylen,
                  struct txn **tidptr, int force)
{
    MDB_val mkey;
    struct txn *tid;
    int r, mr;

    PDEBUG("cyrusdb_lmdb(%s)[delete] tidptr=%p *tidptr=%p",
            db->fname, tidptr, tidptr ? *tidptr : NULL);

    mkey.mv_data = (void*) key;
    mkey.mv_size = keylen;

    /* Invalidate cursor */
    if (db->mcur) {
        mdb_cursor_close(db->mcur);
        db->mcur = NULL;
    }

    /* Open or reuse transaction */
    r = getorset_txn(db, tidptr, &tid, 0);
    if (r) goto fail;

    mr = mdb_del(tid->mtxn, tid->dbi, &mkey, NULL);
    if (mr == MDB_NOTFOUND) {
        /* Force deleting an inexistent key is not an error */
        r = force ? CYRUSDB_OK : CYRUSDB_NOTFOUND;
        if (r) goto fail;
    } else if (mr) {
        syslog(LOG_ERR, "cyrusdb_lmdb(%s): %s", db->fname, mdb_strerror(mr));
        r = CYRUSDB_INTERNAL;
        goto fail;
    }

    /* Commit or export the transaction */
    if (!tidptr) {
        r = commit_txn(db, tid);
        if (r) goto fail;
    } else {
        *tidptr = tid;
    }

    return CYRUSDB_OK;

fail:
    if (tid && (!tidptr || !*tidptr)) abort_txn(db, tid);
    return r;
}

static int mycompar(struct dbengine *db, const char *a, int alen,
                    const char *b, int blen)
{
    int cmp;
    struct txn *tid = NULL;
    MDB_val ma, mb;
    int use_mboxcmp = db->flags & CYRUSDB_MBOXSORT;

    assert(db && a && b);

    /* LMDB internal order requires a transaction to operate on */
    if (!use_mboxcmp && begin_txn(db, &tid, 1 /*readonly*/)) {
        syslog(LOG_ERR, "lmdb_compar(%s): internal error", db->fname);
        return -1;
    }

    ma.mv_data = (void *) a;
    ma.mv_size = alen;
    mb.mv_data = (void *) b;
    mb.mv_size = blen;

    /* Compare values using appropriate comparator */
    cmp = use_mboxcmp ? mboxcmp(&ma, &mb) : mdb_cmp(tid->mtxn, tid->dbi, &ma, &mb);

    /* Discard LMDB-owned transaction, if any */
    if (!use_mboxcmp && abort_txn(db, tid)) {
        syslog(LOG_ERR, "lmdb_compar(%s): internal error", db->fname);
        return -1;
    }

    return cmp;
}

static int init(const char *dbdir __attribute__((unused)),
                int myflags __attribute__((unused)))
{
    PDEBUG("cyrusdb_lmdb(%s)[init]", dbdir);

    char *val = getenv("CYRUSDB_LMDB_MAXSIZE");
    int pagesize;

    if (val) {
        /* Parse user-defined maximum database size */
        char *endptr = NULL;
        long size;
        int myerrno;
        size_t guard, factor = 1;

        /* Convert and keep conversion error */
        size = strtol(val, &endptr, 10);
        myerrno = errno;
        if (endptr) {
            /* Be nice to humans: parse 1gb, 3mb or byte size */
            if (!strcasecmp(endptr, "mb")) {
                factor = 1024 * 1024;
            } else if (!strcasecmp(endptr, "gb")) {
                factor = 1024 * 1024 * 1024;
            } else if (*endptr) {
                factor = 0;
            }
        }
        /* Check for overflows */
        guard = (size_t) size * factor;
        if (factor != 0 && guard/factor != (size_t) size) myerrno = ERANGE;

        /* Validate input */
        if (factor == 0 || myerrno == ERANGE || size < 0) {
            syslog(LOG_ERR, "cyrusdb_lmdb: invalid CYRUSDB_LMDB_MAXSIZE: %s", val);
            return CYRUSDB_INTERNAL;
        }
        /* Use user-defined value */
        maxdbsize = guard;
        PDEBUG("cyrusdb_lmdb: set maximum db size to %zu bytes", maxdbsize);
    }
    /* Warn about sub-par configuration */
    pagesize = getpagesize();
    if ((maxdbsize % pagesize) != 0) {
        syslog(LOG_ERR,
                "cyrusdb_lmdb: db size %zu isn't a multiple of pagesize %d",
                maxdbsize, pagesize);
    }
    return CYRUSDB_OK;
}

static int done(void)
{
    struct dblist *l;

    PDEBUG("cyrusdb_lmdb[done]");
    l = dbs;
    while (l) {
        struct dblist *tmp;
        syslog(LOG_ERR, "cyrusdb_lmdb: closing stray database %s", l->db->fname);
        close_db(l->db);
        tmp = l;
        l = l->next;
        free(tmp);
    }
    dbs = NULL;
    return CYRUSDB_OK;
}

static int mysync(void)
{
    int r, mr;
    struct dblist *l;

    r = CYRUSDB_OK;
    for (l = dbs; l; l = l->next) {
        PDEBUG("cyrusdb_lmdb(%s)[sync]", l->db->fname);
        mr = mdb_env_sync(l->db->env, 1 /*force*/);
        if (mr) {
            syslog(LOG_ERR, "cyrusdb_lmdb(%s): sync: %s", l->db->fname, mdb_strerror(mr));
            r = CYRUSDB_INTERNAL;
            break;
        }
    }
    return r;
}

static int archive(const strarray_t *fnames, const char *dirname)
{
    struct hash_table want = HASH_TABLE_INITIALIZER;
    struct dblist *l = dbs;
    char dstname[1024], *dp;
    size_t length, rest, n;
    int i, r, init = 1;

    PDEBUG("cyrusdb_lmdb[archive]");

    if (!strarray_size(fnames))
        return 0;

    construct_hash_table(&want, strarray_size(fnames), 0);
    for (i = 0; i < fnames->count; i++) {
        hash_insert(strarray_nth(fnames, i), (void*) 1, &want);
    }

    /* Prepare filename buffer */
    n = strlcpy(dstname, dirname, sizeof(dstname));
    if (n == sizeof(dstname)) {
        syslog(LOG_ERR, "cyrusdb_lmdb: archive: long dirname %s", dirname);
        r = CYRUSDB_IOERROR;
        goto fail;
    }
    length = strlen(dstname);
    dp = dstname + length;
    rest = sizeof(dstname) - length;

    r = CYRUSDB_OK;
    for(l = dbs; l; l = l->next) {
        char *base;
        int mr;

        /* Skip unwanted databases */
        if (!hash_lookup(l->db->fname, &want)) {
            continue;
        }

        /* Append the basename of the source database file to dirname.
         * This flattens all fnames into the same target directory, which
         * isn't entirely safe but replicates the generic archiver */
        base = strrchr(l->db->fname, '/');
        base = base ? base : l->db->fname;
        if (strlen(base) + length > sizeof(dstname)) {
            syslog(LOG_ERR, "cyrusdb_lmdb: archive: long filename %s/%s", dirname, base);
            r = CYRUSDB_IOERROR;
            break;
        }
        strlcpy(dp, base, rest);

        /* If this is the first archival, make sure that dirname exists */
        if (init) {
            r = my_mkparentdir(dstname);
            if (r) {
                syslog(LOG_ERR, "cyrusdb_lmdb(%s): archive: %s", dirname, strerror(errno));
                r = CYRUSDB_IOERROR;
                break;
            }
            init = 0;
        }

        /* Archive the current database */
        PDEBUG("cyrusdb_lmdb(%s): archiving to %s", l->db->fname, dstname);
        if ((mr = mdb_env_copy(l->db->env, dstname))) {
            syslog(LOG_ERR, "cyrusdb_lmdb(%s): archive: %s", dstname, mdb_strerror(mr));
            r = mr == EEXIST ? CYRUSDB_EXISTS : CYRUSDB_INTERNAL;
            break;
        }
    }

fail:
    free_hash_table(&want, NULL);
    return r;
}

static int myunlink(const char *fname, int flags __attribute__((unused)))
{
    /* XXX: smart lock checks? */
    char *lockname = strconcat(fname, "-lock", (char *)NULL);
    unlink(fname);
    unlink(lockname);
    free(lockname);
    return 0;
}

EXPORTED struct cyrusdb_backend cyrusdb_lmdb =
{
    "lmdb",                     /* name */

    &init,
    &done,
    &mysync,
    &archive,
    &myunlink,

    &myopen,
    &myclose,

    &fetch,
    &fetch, /* fetchlock */
    NULL,   /* fetchnext */

    &foreach,
    &create,
    &store,
    &delete,

    &commit_txn,
    &abort_txn,

    NULL, /* dump */
    NULL, /* consistent */
    NULL, /* repack */
    &mycompar
};