]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
More for page_loosen
[openldap] / libraries / liblmdb / mdb.c
index 7e2905891eb770c4d6eb53f751bd05b2738a0c7a..efd15e5beaf25b39d72868b5807d523db584ed17 100644 (file)
@@ -5,7 +5,7 @@
  *     BerkeleyDB API, but much simplified.
  */
 /*
- * Copyright 2011-2013 Howard Chu, Symas Corp.
+ * Copyright 2011-2014 Howard Chu, Symas Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -352,7 +352,7 @@ static txnid_t mdb_debug_start;
 
        /**     @brief The maximum size of a database page.
         *
-        *      This is 32k, since it must fit in #MDB_page.#mp_upper.
+        *      This is 32k, since it must fit in #MDB_page.%mp_upper.
         *
         *      LMDB will use database pages < OS pages if needed.
         *      That causes more I/O in write transactions: The OS must
@@ -650,6 +650,7 @@ typedef struct MDB_page {
 #define        P_DIRTY          0x10           /**< dirty page, also set for #P_SUBP pages */
 #define        P_LEAF2          0x20           /**< for #MDB_DUPFIXED records */
 #define        P_SUBP           0x40           /**< for #MDB_DUPSORT sub-pages */
+#define        P_LOOSE          0x4000         /**< page was dirtied then freed, can be reused */
 #define        P_KEEP           0x8000         /**< leave this page alone during spill */
 /** @} */
        uint16_t        mp_flags;               /**< @ref mdb_page */
@@ -936,12 +937,12 @@ struct MDB_txn {
  *     @{
  */
 #define MDB_TXN_RDONLY         0x01            /**< read-only transaction */
-#define MDB_TXN_ERROR          0x02            /**< an error has occurred */
+#define MDB_TXN_ERROR          0x02            /**< txn is unusable after an error */
 #define MDB_TXN_DIRTY          0x04            /**< must write, even if dirty list is empty */
 #define MDB_TXN_SPILLS         0x08            /**< txn or a parent has spilled pages */
 /** @} */
        unsigned int    mt_flags;               /**< @ref mdb_txn */
-       /** dirty_list room: Array size - #dirty pages visible to this txn.
+       /** #dirty_list room: Array size - \#dirty pages visible to this txn.
         *      Includes ancestor txns' dirty pages not hidden by other txns'
         *      dirty/spilled pages. Thus commit(nested txn) has room to merge
         *      dirty_list into mt_parent after freeing hidden mt_parent pages.
@@ -1021,6 +1022,7 @@ typedef struct MDB_xcursor {
 typedef struct MDB_pgstate {
        pgno_t          *mf_pghead;     /**< Reclaimed freeDB pages, or NULL before use */
        txnid_t         mf_pglast;      /**< ID of last used record, or 0 if !mf_pghead */
+       MDB_page        *mf_pgloose;    /**< Dirty pages that can be reused */
 } MDB_pgstate;
 
        /** The database environment. */
@@ -1034,8 +1036,6 @@ struct MDB_env {
 #define        MDB_ENV_ACTIVE  0x20000000U
        /** me_txkey is set */
 #define        MDB_ENV_TXKEY   0x10000000U
-       /** Have liveness lock in reader table */
-#define        MDB_LIVE_READER 0x08000000U
        uint32_t        me_flags;               /**< @ref mdb_env */
        unsigned int    me_psize;       /**< DB page size, inited from me_os_psize */
        unsigned int    me_os_psize;    /**< OS page size, from #GET_PAGESIZE */
@@ -1059,6 +1059,7 @@ struct MDB_env {
        MDB_pgstate     me_pgstate;             /**< state of old pages from freeDB */
 #      define          me_pglast       me_pgstate.mf_pglast
 #      define          me_pghead       me_pgstate.mf_pghead
+#      define          me_pgloose      me_pgstate.mf_pgloose
        MDB_page        *me_dpages;             /**< list of malloc'd blocks for re-use */
        /** IDL of pages that became unused in a write txn */
        MDB_IDL         me_free_pgs;
@@ -1071,6 +1072,7 @@ struct MDB_env {
 #if !(MDB_MAXKEYSIZE)
        unsigned int    me_maxkey;      /**< max size of a key */
 #endif
+       int             me_live_reader;         /**< have liveness lock in reader table */
 #ifdef _WIN32
        int             me_pidquery;            /**< Used in OpenProcess */
        HANDLE          me_rmutex;              /* Windows mutexes don't reside in shared mem */
@@ -1096,9 +1098,13 @@ typedef struct MDB_ntxn {
 #define MDB_COMMIT_PAGES       IOV_MAX
 #endif
 
-       /* max bytes to write in one call */
+       /** max bytes to write in one call */
 #define MAX_WRITE              (0x80000000U >> (sizeof(ssize_t) == 4))
 
+       /** Check \b txn and \b dbi arguments to a function */
+#define TXN_DBI_EXIST(txn, dbi) \
+       ((txn) && (dbi) < (txn)->mt_numdbs && ((txn)->mt_dbflags[dbi] & DB_VALID))
+
 static int  mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp);
 static int  mdb_page_new(MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp);
 static int  mdb_page_touch(MDB_cursor *mc);
@@ -1142,7 +1148,8 @@ static int        mdb_update_key(MDB_cursor *mc, MDB_val *key);
 static void    mdb_cursor_pop(MDB_cursor *mc);
 static int     mdb_cursor_push(MDB_cursor *mc, MDB_page *mp);
 
-static int     mdb_cursor_del0(MDB_cursor *mc, MDB_node *leaf);
+static int     mdb_cursor_del0(MDB_cursor *mc);
+static int     mdb_del0(MDB_txn *txn, MDB_dbi dbi, MDB_val *key, MDB_val *data, unsigned flags);
 static int     mdb_cursor_sibling(MDB_cursor *mc, int move_right);
 static int     mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op);
 static int     mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op);
@@ -1198,7 +1205,7 @@ static char *const mdb_errstr[] = {
        "MDB_INCOMPATIBLE: Operation and DB incompatible, or DB flags changed",
        "MDB_BAD_RSLOT: Invalid reuse of reader locktable slot",
        "MDB_BAD_TXN: Transaction cannot recover - it must be aborted",
-       "MDB_BAD_VALSIZE: Too big key/data, key is empty, or wrong DUPFIXED size",
+       "MDB_BAD_VALSIZE: Unsupported size of key/DB name/data, or wrong DUPFIXED size",
 };
 
 char *
@@ -1283,18 +1290,55 @@ mdb_dkey(MDB_val *key, char *buf)
        return buf;
 }
 
+static const char *
+mdb_leafnode_type(MDB_node *n)
+{
+       static char *const tp[2][2] = {{"", ": DB"}, {": sub-page", ": sub-DB"}};
+       return F_ISSET(n->mn_flags, F_BIGDATA) ? ": overflow page" :
+               tp[F_ISSET(n->mn_flags, F_DUPDATA)][F_ISSET(n->mn_flags, F_SUBDATA)];
+}
+
 /** Display all the keys in the page. */
 void
 mdb_page_list(MDB_page *mp)
 {
+       pgno_t pgno = mdb_dbg_pgno(mp);
+       const char *type, *state = (mp->mp_flags & P_DIRTY) ? ", dirty" : "";
        MDB_node *node;
        unsigned int i, nkeys, nsize, total = 0;
        MDB_val key;
        DKBUF;
 
+       switch (mp->mp_flags & (P_BRANCH|P_LEAF|P_LEAF2|P_META|P_OVERFLOW|P_SUBP)) {
+       case P_BRANCH:              type = "Branch page";               break;
+       case P_LEAF:                type = "Leaf page";                 break;
+       case P_LEAF|P_SUBP:         type = "Sub-page";                  break;
+       case P_LEAF|P_LEAF2:        type = "LEAF2 page";                break;
+       case P_LEAF|P_LEAF2|P_SUBP: type = "LEAF2 sub-page";    break;
+       case P_OVERFLOW:
+               fprintf(stderr, "Overflow page %"Z"u pages %u%s\n",
+                       pgno, mp->mp_pages, state);
+               return;
+       case P_META:
+               fprintf(stderr, "Meta-page %"Z"u txnid %"Z"u\n",
+                       pgno, ((MDB_meta *)METADATA(mp))->mm_txnid);
+               return;
+       default:
+               fprintf(stderr, "Bad page %"Z"u flags 0x%u\n", pgno, mp->mp_flags);
+               return;
+       }
+
        nkeys = NUMKEYS(mp);
-       fprintf(stderr, "Page %"Z"u numkeys %d\n", mdb_dbg_pgno(mp), nkeys);
+       fprintf(stderr, "%s %"Z"u numkeys %d%s\n", type, pgno, nkeys, state);
+
        for (i=0; i<nkeys; i++) {
+               if (IS_LEAF2(mp)) {     /* LEAF2 pages have no mp_ptrs[] or node headers */
+                       key.mv_size = nsize = mp->mp_pad;
+                       key.mv_data = LEAF2KEY(mp, i, nsize);
+                       total += nsize;
+                       fprintf(stderr, "key %d: nsize %d, %s\n", i, nsize, DKEY(&key));
+                       continue;
+               }
                node = NODEPTR(mp, i);
                key.mv_size = node->mn_ksize;
                key.mv_data = node->mn_data;
@@ -1310,11 +1354,13 @@ mdb_page_list(MDB_page *mp)
                                nsize += NODEDSZ(node);
                        total += nsize;
                        nsize += sizeof(indx_t);
-                       fprintf(stderr, "key %d: nsize %d, %s\n", i, nsize, DKEY(&key));
+                       fprintf(stderr, "key %d: nsize %d, %s%s\n",
+                               i, nsize, DKEY(&key), mdb_leafnode_type(node));
                }
                total = EVEN(total);
        }
-       fprintf(stderr, "Total: %d\n", total);
+       fprintf(stderr, "Total: header %d + contents %d + unused %d\n",
+               IS_LEAF2(mp) ? PAGEHDRSZ : mp->mp_lower, total, SIZELEFT(mp));
 }
 
 void
@@ -1340,6 +1386,7 @@ mdb_cursor_chk(MDB_cursor *mc)
 /** Count all the pages in each DB and in the freelist
  *  and make sure it matches the actual number of pages
  *  being used.
+ *  All named DBs must be open for a correct count.
  */
 static void mdb_audit(MDB_txn *txn)
 {
@@ -1353,10 +1400,13 @@ static void mdb_audit(MDB_txn *txn)
        mdb_cursor_init(&mc, txn, FREE_DBI, NULL);
        while ((rc = mdb_cursor_get(&mc, &key, &data, MDB_NEXT)) == 0)
                freecount += *(MDB_ID *)data.mv_data;
+       mdb_tassert(txn, rc == MDB_NOTFOUND);
 
        count = 0;
        for (i = 0; i<txn->mt_numdbs; i++) {
                MDB_xcursor mx;
+               if (!(txn->mt_dbflags[i] & DB_VALID))
+                       continue;
                mdb_cursor_init(&mc, txn, i, &mx);
                if (txn->mt_dbs[i].md_root == P_INVALID)
                        continue;
@@ -1364,8 +1414,8 @@ static void mdb_audit(MDB_txn *txn)
                        txn->mt_dbs[i].md_leaf_pages +
                        txn->mt_dbs[i].md_overflow_pages;
                if (txn->mt_dbs[i].md_flags & MDB_DUPSORT) {
-                       mdb_page_search(&mc, NULL, MDB_PS_FIRST);
-                       do {
+                       rc = mdb_page_search(&mc, NULL, MDB_PS_FIRST);
+                       for (; rc == MDB_SUCCESS; rc = mdb_cursor_sibling(&mc, 1)) {
                                unsigned j;
                                MDB_page *mp;
                                mp = mc.mc_pg[mc.mc_top];
@@ -1379,7 +1429,7 @@ static void mdb_audit(MDB_txn *txn)
                                        }
                                }
                        }
-                       while (mdb_cursor_sibling(&mc, 1) == 0);
+                       mdb_tassert(txn, rc == MDB_NOTFOUND);
                }
        }
        if (freecount + count + 2 /* metapages */ != txn->mt_next_pgno) {
@@ -1438,7 +1488,6 @@ mdb_page_malloc(MDB_txn *txn, unsigned num)
        }
        return ret;
 }
-
 /** Free a single page.
  * Saves single pages to a list, for future reuse.
  * (This is not used for multi-page overflow pages.)
@@ -1478,6 +1527,23 @@ mdb_dlist_free(MDB_txn *txn)
        dl[0].mid = 0;
 }
 
+/** Loosen a single page.
+ * Saves single pages to a list for future reuse
+ * in this same txn. It has been pulled from the freeDB
+ * and already resides on the dirty list, but has been
+ * deleted. Use these pages first before pulling again
+ * from the freeDB.
+ */
+static void
+mdb_page_loose(MDB_env *env, MDB_page *mp)
+{
+               pgno_t *pp = (pgno_t *)mp->mp_ptrs;
+               *pp = mp->mp_pgno;
+               mp->mp_next = env->me_pgloose;
+               env->me_pgloose = mp;
+               mp->mp_flags |= P_LOOSE;
+}
+
 /** Set or clear P_KEEP in dirty, non-overflow, non-sub pages watched by txn.
  * @param[in] mc A cursor handle for the current operation.
  * @param[in] pflags Flags of the pages to update:
@@ -1526,6 +1592,12 @@ mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
                        break;
        }
 
+       /* Loose pages shouldn't be spilled */
+       for (dp = txn->mt_env->me_pgloose; dp; dp=dp->mp_next) {
+               if ((dp->mp_flags & Mask) == pflags)
+                       dp->mp_flags ^= P_KEEP;
+       }
+
        if (all) {
                /* Mark dirty root pages */
                for (i=0; i<txn->mt_numdbs; i++) {
@@ -1743,7 +1815,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
 #else
        enum { Paranoid = 0, Max_retries = INT_MAX /*infinite*/ };
 #endif
-       int rc, retry = Max_retries;
+       int rc, retry = num * 20;
        MDB_txn *txn = mc->mc_txn;
        MDB_env *env = txn->mt_env;
        pgno_t pgno, *mop = env->me_pghead;
@@ -1753,6 +1825,17 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
        MDB_cursor_op op;
        MDB_cursor m2;
 
+       /* If there are any loose pages, just use them */
+       if (num == 1 && env->me_pgloose) {
+               pgno_t *pp;
+               np = env->me_pgloose;
+               env->me_pgloose = np->mp_next;
+               pp = (pgno_t *)np->mp_ptrs;
+               np->mp_pgno = *pp;
+               *mp = np;
+               return MDB_SUCCESS;
+       }
+
        *mp = NULL;
 
        /* If our dirty list is already full, we can't do anything */
@@ -1776,7 +1859,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                                if (mop[i-n2] == pgno+n2)
                                        goto search_done;
                        } while (--i > n2);
-                       if (Max_retries < INT_MAX && --retry < 0)
+                       if (--retry < 0)
                                break;
                }
 
@@ -2059,6 +2142,7 @@ done:
                        if (m2->mc_pg[mc->mc_top] == mp) {
                                m2->mc_pg[mc->mc_top] = np;
                                if ((mc->mc_db->md_flags & MDB_DUPSORT) &&
+                                       IS_LEAF(np) &&
                                        m2->mc_ki[mc->mc_top] == mc->mc_ki[mc->mc_top])
                                {
                                        MDB_node *leaf = NODEPTR(np, mc->mc_ki[mc->mc_top]);
@@ -2268,11 +2352,11 @@ mdb_txn_renew0(MDB_txn *txn)
                                MDB_PID_T pid = env->me_pid;
                                pthread_t tid = pthread_self();
 
-                               if (!(env->me_flags & MDB_LIVE_READER)) {
+                               if (!env->me_live_reader) {
                                        rc = mdb_reader_pid(env, Pidset, pid);
                                        if (rc)
                                                return rc;
-                                       env->me_flags |= MDB_LIVE_READER;
+                                       env->me_live_reader = 1;
                                }
 
                                LOCK_MUTEX_R(env);
@@ -2613,6 +2697,38 @@ mdb_freelist_save(MDB_txn *txn)
                        return rc;
        }
 
+       /* Dispose of loose pages. Usually they will have all
+        * been used up by the time we get here.
+        */
+       if (env->me_pgloose) {
+               MDB_page *mp = env->me_pgloose;
+               pgno_t *pp;
+               /* Just return them to freeDB */
+               if (env->me_pghead) {
+                       int i, j;
+                       mop = env->me_pghead;
+                       while(mp) {
+                               pgno_t pg;
+                               pp = (pgno_t *)mp->mp_ptrs;
+                               pg = *pp;
+                               j = mop[0] + 1;
+                               for (i = mop[0]; i && mop[i] < pg; i--)
+                                       mop[j--] = mop[i];
+                               mop[j] = pg;
+                               mop[0] += 1;
+                               mp = mp->mp_next;
+                       }
+               } else {
+               /* Oh well, they were wasted. Put on freelist */
+                       while(mp) {
+                               pp = (pgno_t *)mp->mp_ptrs;
+                               mdb_midl_append(&txn->mt_free_pgs, *pp);
+                               mp = mp->mp_next;
+                       }
+               }
+               env->me_pgloose = NULL;
+       }
+
        /* MDB_RESERVE cancels meminit in ovpage malloc (when no WRITEMAP) */
        clean_limit = (env->me_flags & (MDB_NOMEMINIT|MDB_WRITEMAP))
                ? SSIZE_MAX : maxfree_1pg;
@@ -2722,7 +2838,6 @@ mdb_freelist_save(MDB_txn *txn)
                mop += mop_len;
                rc = mdb_cursor_first(&mc, &key, &data);
                for (; !rc; rc = mdb_cursor_next(&mc, &key, &data, MDB_NEXT)) {
-                       unsigned flags = MDB_CURRENT;
                        txnid_t id = *(txnid_t *)key.mv_data;
                        ssize_t len = (ssize_t)(data.mv_size / sizeof(MDB_ID)) - 1;
                        MDB_ID save;
@@ -2732,12 +2847,11 @@ mdb_freelist_save(MDB_txn *txn)
                        if (len > mop_len) {
                                len = mop_len;
                                data.mv_size = (len + 1) * sizeof(MDB_ID);
-                               flags = 0;
                        }
                        data.mv_data = mop -= len;
                        save = mop[0];
                        mop[0] = len;
-                       rc = mdb_cursor_put(&mc, &key, &data, flags);
+                       rc = mdb_cursor_put(&mc, &key, &data, MDB_CURRENT);
                        mop[0] = save;
                        if (rc || !(mop_len -= len))
                                break;
@@ -3631,7 +3745,9 @@ static void NTAPI mdb_tls_callback(PVOID module, DWORD reason, PVOID ptr)
        case DLL_THREAD_DETACH:
                for (i=0; i<mdb_tls_nkeys; i++) {
                        MDB_reader *r = pthread_getspecific(mdb_tls_keys[i]);
-                       mdb_env_reader_dest(r);
+                       if (r) {
+                               mdb_env_reader_dest(r);
+                       }
                }
                break;
        case DLL_PROCESS_DETACH: break;
@@ -4498,6 +4614,13 @@ mdb_cmp_cint(const MDB_val *a, const MDB_val *b)
 #endif
 }
 
+/** Compare two items pointing at size_t's of unknown alignment. */
+#ifdef MISALIGNED_OK
+# define mdb_cmp_clong mdb_cmp_long
+#else
+# define mdb_cmp_clong mdb_cmp_cint
+#endif
+
 /** Compare two items lexically */
 static int
 mdb_cmp_memn(const MDB_val *a, const MDB_val *b)
@@ -5041,12 +5164,9 @@ mdb_get(MDB_txn *txn, MDB_dbi dbi,
        int exact = 0;
        DKBUF;
 
-       if (key == NULL || data == NULL)
-               return EINVAL;
-
        DPRINTF(("===> get db %u key [%s]", dbi, DKEY(key)));
 
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (!key || !data || dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        if (txn->mt_flags & MDB_TXN_ERROR)
@@ -5369,7 +5489,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                if (!mc->mc_top) {
                        /* There are no other pages */
                        mc->mc_ki[mc->mc_top] = 0;
-                       if (op == MDB_SET_RANGE) {
+                       if (op == MDB_SET_RANGE && !exactp) {
                                rc = 0;
                                goto set1;
                        } else
@@ -5405,8 +5525,10 @@ set1:
        mc->mc_flags &= ~C_EOF;
 
        if (IS_LEAF2(mp)) {
-               key->mv_size = mc->mc_db->md_pad;
-               key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
+               if (op == MDB_SET_RANGE || op == MDB_SET_KEY) {
+                       key->mv_size = mc->mc_db->md_pad;
+                       key->mv_data = LEAF2KEY(mp, mc->mc_ki[mc->mc_top], key->mv_size);
+               }
                return MDB_SUCCESS;
        }
 
@@ -5754,7 +5876,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        uint16_t        fp_flags;
        MDB_val         xdata, *rdata, dkey, olddata;
        MDB_db dummy;
-       int do_sub = 0, insert;
+       int do_sub = 0, insert_key, insert_data;
        unsigned int mcount = 0, dcount = 0, nospill;
        size_t nsize;
        int rc, rc2;
@@ -5782,7 +5904,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        if (mc->mc_txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR))
                return (mc->mc_txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
 
-       if (flags != MDB_CURRENT && key->mv_size-1 >= ENV_MAXKEY(env))
+       if (key->mv_size-1 >= ENV_MAXKEY(env))
                return MDB_BAD_VALSIZE;
 
 #if SIZE_MAX > MAXDATASIZE
@@ -5873,8 +5995,8 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                        return rc2;
        }
 
-       insert = rc;
-       if (insert) {
+       insert_key = insert_data = rc;
+       if (insert_key) {
                /* The key does not exist */
                DPRINTF(("inserting key at index %i", mc->mc_ki[mc->mc_top]));
                if ((mc->mc_db->md_flags & MDB_DUPSORT) &&
@@ -5890,13 +6012,12 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        } else {
                /* there's only a key anyway, so this is a no-op */
                if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
+                       char *ptr;
                        unsigned int ksize = mc->mc_db->md_pad;
                        if (key->mv_size != ksize)
                                return MDB_BAD_VALSIZE;
-                       if (flags == MDB_CURRENT) {
-                               char *ptr = LEAF2KEY(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], ksize);
-                               memcpy(ptr, key->mv_data, ksize);
-                       }
+                       ptr = LEAF2KEY(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], ksize);
+                       memcpy(ptr, key->mv_data, ksize);
                        return MDB_SUCCESS;
                }
 
@@ -5924,21 +6045,14 @@ more:
 
 #if UINT_MAX < SIZE_MAX
                                if (mc->mc_dbx->md_dcmp == mdb_cmp_int && olddata.mv_size == sizeof(size_t))
-#ifdef MISALIGNED_OK
-                                       mc->mc_dbx->md_dcmp = mdb_cmp_long;
-#else
-                                       mc->mc_dbx->md_dcmp = mdb_cmp_cint;
+                                       mc->mc_dbx->md_dcmp = mdb_cmp_clong;
 #endif
-#endif
-                               /* if data matches, skip it */
+                               /* does data match? */
                                if (!mc->mc_dbx->md_dcmp(data, &olddata)) {
                                        if (flags & MDB_NODUPDATA)
-                                               rc = MDB_KEYEXIST;
-                                       else if (flags & MDB_MULTIPLE)
-                                               goto next_mult;
-                                       else
-                                               rc = MDB_SUCCESS;
-                                       return rc;
+                                               return MDB_KEYEXIST;
+                                       /* overwrite it */
+                                       goto current;
                                }
 
                                /* Back up original data item */
@@ -6035,7 +6149,7 @@ prep_subDB:
                        rdata = &xdata;
                        flags |= F_DUPDATA;
                        do_sub = 1;
-                       if (!insert)
+                       if (!insert_key)
                                mdb_node_del(mc, 0);
                        goto new_sub;
                }
@@ -6076,8 +6190,8 @@ current:
                                                return ENOMEM;
                                        id2.mid = pg;
                                        id2.mptr = np;
-                                       rc = mdb_mid2l_insert(mc->mc_txn->mt_u.dirty_list, &id2);
-                                       mdb_cassert(mc, rc == 0);
+                                       rc2 = mdb_mid2l_insert(mc->mc_txn->mt_u.dirty_list, &id2);
+                                       mdb_cassert(mc, rc2 == 0);
                                        if (!(flags & MDB_RESERVE)) {
                                                /* Copy end of page, adjusting alignment so
                                                 * compiler may copy words instead of bytes.
@@ -6095,7 +6209,7 @@ current:
                                        data->mv_data = METADATA(omp);
                                else
                                        memcpy(METADATA(omp), data->mv_data, data->mv_size);
-                               goto done;
+                               return MDB_SUCCESS;
                          }
                        }
                        if ((rc2 = mdb_ovpage_free(mc, omp)) != MDB_SUCCESS)
@@ -6107,14 +6221,13 @@ current:
                         */
                        if (F_ISSET(flags, MDB_RESERVE))
                                data->mv_data = olddata.mv_data;
-                       else if (data->mv_size)
+                       else if (!(mc->mc_flags & C_SUB))
                                memcpy(olddata.mv_data, data->mv_data, data->mv_size);
                        else
                                memcpy(NODEKEY(leaf), key->mv_data, key->mv_size);
-                       goto done;
+                       return MDB_SUCCESS;
                }
                mdb_node_del(mc, 0);
-               mc->mc_db->md_entries--;
        }
 
        rdata = data;
@@ -6124,14 +6237,14 @@ new_sub:
        nsize = IS_LEAF2(mc->mc_pg[mc->mc_top]) ? key->mv_size : mdb_leaf_size(env, key, rdata);
        if (SIZELEFT(mc->mc_pg[mc->mc_top]) < nsize) {
                if (( flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA )
-                       nflags &= ~MDB_APPEND;
-               if (!insert)
+                       nflags &= ~MDB_APPEND; /* sub-page may need room to grow */
+               if (!insert_key)
                        nflags |= MDB_SPLIT_REPLACE;
                rc = mdb_page_split(mc, key, rdata, P_INVALID, nflags);
        } else {
                /* There is room already in this leaf page. */
                rc = mdb_node_add(mc, mc->mc_ki[mc->mc_top], key, rdata, 0, nflags);
-               if (rc == 0 && !do_sub && insert) {
+               if (rc == 0 && insert_key) {
                        /* Adjust other cursors pointing to mp */
                        MDB_cursor *m2, *m3;
                        MDB_dbi dbi = mc->mc_dbi;
@@ -6151,9 +6264,7 @@ new_sub:
                }
        }
 
-       if (rc != MDB_SUCCESS)
-               mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
-       else {
+       if (rc == MDB_SUCCESS) {
                /* Now store the actual data in the child DB. Note that we're
                 * storing the user data in the keys field, so there are strict
                 * size limits on dupdata. The actual data fields of the child
@@ -6161,6 +6272,7 @@ new_sub:
                 */
                if (do_sub) {
                        int xflags;
+                       size_t ecount;
 put_sub:
                        xdata.mv_size = 0;
                        xdata.mv_data = "";
@@ -6176,7 +6288,7 @@ put_sub:
                        if (dkey.mv_size) {
                                rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, &dkey, &xdata, xflags);
                                if (rc)
-                                       return rc;
+                                       goto bad_sub;
                                {
                                        /* Adjust other cursors pointing to mp */
                                        MDB_cursor *m2;
@@ -6194,6 +6306,7 @@ put_sub:
                                /* we've done our job */
                                dkey.mv_size = 0;
                        }
+                       ecount = mc->mc_xcursor->mx_db.md_entries;
                        if (flags & MDB_APPENDDUP)
                                xflags |= MDB_APPEND;
                        rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, data, &xdata, xflags);
@@ -6201,31 +6314,38 @@ put_sub:
                                void *db = NODEDATA(leaf);
                                memcpy(db, &mc->mc_xcursor->mx_db, sizeof(MDB_db));
                        }
+                       insert_data = mc->mc_xcursor->mx_db.md_entries - ecount;
                }
-               /* sub-writes might have failed so check rc again.
-                * Don't increment count if we just replaced an existing item.
-                */
-               if (!rc && !(flags & MDB_CURRENT))
+               /* Increment count unless we just replaced an existing item. */
+               if (insert_data)
                        mc->mc_db->md_entries++;
+               if (insert_key) {
+                       /* Invalidate txn if we created an empty sub-DB */
+                       if (rc)
+                               goto bad_sub;
+                       /* If we succeeded and the key didn't exist before,
+                        * make sure the cursor is marked valid.
+                        */
+                       mc->mc_flags |= C_INITIALIZED;
+               }
                if (flags & MDB_MULTIPLE) {
                        if (!rc) {
-next_mult:
                                mcount++;
                                /* let caller know how many succeeded, if any */
                                data[1].mv_size = mcount;
                                if (mcount < dcount) {
                                        data[0].mv_data = (char *)data[0].mv_data + data[0].mv_size;
+                                       insert_key = insert_data = 0;
                                        goto more;
                                }
                        }
                }
+               return rc;
+bad_sub:
+               if (rc == MDB_KEYEXIST) /* should not happen, we deleted that item */
+                       rc = MDB_CORRUPTED;
        }
-done:
-       /* If we succeeded and the key didn't exist before, make sure
-        * the cursor is marked valid.
-        */
-       if (!rc && insert)
-               mc->mc_flags |= C_INITIALIZED;
+       mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
        return rc;
 }
 
@@ -6253,14 +6373,21 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
                return rc;
 
        mp = mc->mc_pg[mc->mc_top];
+       if (IS_LEAF2(mp))
+               goto del_key;
        leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
 
-       if (!IS_LEAF2(mp) && F_ISSET(leaf->mn_flags, F_DUPDATA)) {
-               if (!(flags & MDB_NODUPDATA)) {
+       if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
+               if (flags & MDB_NODUPDATA) {
+                       /* mdb_cursor_del0() will subtract the final entry */
+                       mc->mc_db->md_entries -= mc->mc_xcursor->mx_db.md_entries - 1;
+               } else {
                        if (!F_ISSET(leaf->mn_flags, F_SUBDATA)) {
                                mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
                        }
                        rc = mdb_cursor_del(&mc->mc_xcursor->mx_cursor, MDB_NOSPILL);
+                       if (rc)
+                               return rc;
                        /* If sub-DB still has entries, we're done */
                        if (mc->mc_xcursor->mx_db.md_entries) {
                                if (leaf->mn_flags & F_SUBDATA) {
@@ -6291,14 +6418,28 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
                if (leaf->mn_flags & F_SUBDATA) {
                        /* add all the child DB's pages to the free list */
                        rc = mdb_drop0(&mc->mc_xcursor->mx_cursor, 0);
-                       if (rc == MDB_SUCCESS) {
-                               mc->mc_db->md_entries -=
-                                       mc->mc_xcursor->mx_db.md_entries;
-                       }
+                       if (rc)
+                               goto fail;
                }
        }
 
-       return mdb_cursor_del0(mc, leaf);
+       /* add overflow pages to free list */
+       if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
+               MDB_page *omp;
+               pgno_t pg;
+
+               memcpy(&pg, NODEDATA(leaf), sizeof(pg));
+               if ((rc = mdb_page_get(mc->mc_txn, pg, &omp, NULL)) ||
+                       (rc = mdb_ovpage_free(mc, omp)))
+                       goto fail;
+       }
+
+del_key:
+       return mdb_cursor_del0(mc);
+
+fail:
+       mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
+       return rc;
 }
 
 /** Allocate and initialize new pages for a database.
@@ -6525,8 +6666,7 @@ full:
 }
 
 /** Delete the specified node from a page.
- * @param[in] mp The page to operate on.
- * @param[in] indx The index of the node to delete.
+ * @param[in] mc Cursor pointing to the node to delete.
  * @param[in] ksize The size of a node. Only used if the page is
  * part of a #MDB_DUPFIXED database.
  */
@@ -6708,11 +6848,7 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
        mx->mx_dbflag = DB_VALID|DB_DIRTY; /* DB_DIRTY guides mdb_cursor_touch */
 #if UINT_MAX < SIZE_MAX
        if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t))
-#ifdef MISALIGNED_OK
-               mx->mx_dbx.md_cmp = mdb_cmp_long;
-#else
-               mx->mx_dbx.md_cmp = mdb_cmp_cint;
-#endif
+               mx->mx_dbx.md_cmp = mdb_cmp_clong;
 #endif
 }
 
@@ -6749,7 +6885,7 @@ mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
        MDB_cursor      *mc;
        size_t size = sizeof(MDB_cursor);
 
-       if (txn == NULL || ret == NULL || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (!ret || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        if (txn->mt_flags & MDB_TXN_ERROR)
@@ -6781,12 +6917,15 @@ mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
 int
 mdb_cursor_renew(MDB_txn *txn, MDB_cursor *mc)
 {
-       if (txn == NULL || mc == NULL || mc->mc_dbi >= txn->mt_numdbs)
+       if (!mc || !TXN_DBI_EXIST(txn, mc->mc_dbi))
                return EINVAL;
 
        if ((mc->mc_flags & C_UNTRACK) || txn->mt_cursors)
                return EINVAL;
 
+       if (txn->mt_flags & MDB_TXN_ERROR)
+               return MDB_BAD_TXN;
+
        mdb_cursor_init(mc, txn, mc->mc_dbi, mc->mc_xcursor);
        return MDB_SUCCESS;
 }
@@ -6803,6 +6942,9 @@ mdb_cursor_count(MDB_cursor *mc, size_t *countp)
        if (mc->mc_xcursor == NULL)
                return MDB_INCOMPATIBLE;
 
+       if (mc->mc_txn->mt_flags & MDB_TXN_ERROR)
+               return MDB_BAD_TXN;
+
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
        if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                *countp = 1;
@@ -6955,7 +7097,9 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                        unsigned int snum = csrc->mc_snum;
                        MDB_node *s2;
                        /* must find the lowest key below src */
-                       mdb_page_search_lowest(csrc);
+                       rc = mdb_page_search_lowest(csrc);
+                       if (rc)
+                               return rc;
                        if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
                                key.mv_size = csrc->mc_db->md_pad;
                                key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], 0, key.mv_size);
@@ -6978,18 +7122,20 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                MDB_node *s2;
                MDB_val bkey;
                /* must find the lowest key below dst */
-               mdb_page_search_lowest(cdst);
-               if (IS_LEAF2(cdst->mc_pg[cdst->mc_top])) {
-                       bkey.mv_size = cdst->mc_db->md_pad;
-                       bkey.mv_data = LEAF2KEY(cdst->mc_pg[cdst->mc_top], 0, bkey.mv_size);
+               mdb_cursor_copy(cdst, &mn);
+               rc = mdb_page_search_lowest(&mn);
+               if (rc)
+                       return rc;
+               if (IS_LEAF2(mn.mc_pg[mn.mc_top])) {
+                       bkey.mv_size = mn.mc_db->md_pad;
+                       bkey.mv_data = LEAF2KEY(mn.mc_pg[mn.mc_top], 0, bkey.mv_size);
                } else {
-                       s2 = NODEPTR(cdst->mc_pg[cdst->mc_top], 0);
+                       s2 = NODEPTR(mn.mc_pg[mn.mc_top], 0);
                        bkey.mv_size = NODEKSZ(s2);
                        bkey.mv_data = NODEKEY(s2);
                }
-               cdst->mc_snum = snum--;
-               cdst->mc_top = snum;
-               mdb_cursor_copy(cdst, &mn);
+               mn.mc_snum = snum--;
+               mn.mc_top = snum;
                mn.mc_ki[snum] = 0;
                rc = mdb_update_key(&mn, &bkey);
                if (rc)
@@ -7100,18 +7246,22 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
  *     the \b csrc page will be freed.
  * @param[in] csrc Cursor pointing to the source page.
  * @param[in] cdst Cursor pointing to the destination page.
+ * @return 0 on success, non-zero on failure.
  */
 static int
 mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
 {
-       int                      rc;
-       indx_t                   i, j;
-       MDB_node                *srcnode;
+       MDB_page        *psrc, *pdst;
+       MDB_node        *srcnode;
        MDB_val          key, data;
-       unsigned        nkeys;
+       unsigned         nkeys;
+       int                      rc;
+       indx_t           i, j;
 
-       DPRINTF(("merging page %"Z"u into %"Z"u", csrc->mc_pg[csrc->mc_top]->mp_pgno,
-               cdst->mc_pg[cdst->mc_top]->mp_pgno));
+       psrc = csrc->mc_pg[csrc->mc_top];
+       pdst = cdst->mc_pg[cdst->mc_top];
+
+       DPRINTF(("merging page %"Z"u into %"Z"u", psrc->mp_pgno, pdst->mp_pgno));
 
        mdb_cassert(csrc, csrc->mc_snum > 1);   /* can't merge root page */
        mdb_cassert(csrc, cdst->mc_snum > 1);
@@ -7122,34 +7272,35 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
 
        /* Move all nodes from src to dst.
         */
-       j = nkeys = NUMKEYS(cdst->mc_pg[cdst->mc_top]);
-       if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
+       j = nkeys = NUMKEYS(pdst);
+       if (IS_LEAF2(psrc)) {
                key.mv_size = csrc->mc_db->md_pad;
-               key.mv_data = METADATA(csrc->mc_pg[csrc->mc_top]);
-               for (i = 0; i < NUMKEYS(csrc->mc_pg[csrc->mc_top]); i++, j++) {
+               key.mv_data = METADATA(psrc);
+               for (i = 0; i < NUMKEYS(psrc); i++, j++) {
                        rc = mdb_node_add(cdst, j, &key, NULL, 0, 0);
                        if (rc != MDB_SUCCESS)
                                return rc;
                        key.mv_data = (char *)key.mv_data + key.mv_size;
                }
        } else {
-               for (i = 0; i < NUMKEYS(csrc->mc_pg[csrc->mc_top]); i++, j++) {
-                       srcnode = NODEPTR(csrc->mc_pg[csrc->mc_top], i);
-                       if (i == 0 && IS_BRANCH(csrc->mc_pg[csrc->mc_top])) {
-                               unsigned int snum = csrc->mc_snum;
+               for (i = 0; i < NUMKEYS(psrc); i++, j++) {
+                       srcnode = NODEPTR(psrc, i);
+                       if (i == 0 && IS_BRANCH(psrc)) {
+                               MDB_cursor mn;
                                MDB_node *s2;
+                               mdb_cursor_copy(csrc, &mn);
                                /* must find the lowest key below src */
-                               mdb_page_search_lowest(csrc);
-                               if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
-                                       key.mv_size = csrc->mc_db->md_pad;
-                                       key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], 0, key.mv_size);
+                               rc = mdb_page_search_lowest(&mn);
+                               if (rc)
+                                       return rc;
+                               if (IS_LEAF2(mn.mc_pg[mn.mc_top])) {
+                                       key.mv_size = mn.mc_db->md_pad;
+                                       key.mv_data = LEAF2KEY(mn.mc_pg[mn.mc_top], 0, key.mv_size);
                                } else {
-                                       s2 = NODEPTR(csrc->mc_pg[csrc->mc_top], 0);
+                                       s2 = NODEPTR(mn.mc_pg[mn.mc_top], 0);
                                        key.mv_size = NODEKSZ(s2);
                                        key.mv_data = NODEKEY(s2);
                                }
-                               csrc->mc_snum = snum--;
-                               csrc->mc_top = snum;
                        } else {
                                key.mv_size = srcnode->mn_ksize;
                                key.mv_data = NODEKEY(srcnode);
@@ -7164,8 +7315,8 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
        }
 
        DPRINTF(("dst page %"Z"u now has %u keys (%.1f%% filled)",
-           cdst->mc_pg[cdst->mc_top]->mp_pgno, NUMKEYS(cdst->mc_pg[cdst->mc_top]),
-               (float)PAGEFILL(cdst->mc_txn->mt_env, cdst->mc_pg[cdst->mc_top]) / 10));
+           pdst->mp_pgno, NUMKEYS(pdst),
+               (float)PAGEFILL(cdst->mc_txn->mt_env, pdst) / 10));
 
        /* Unlink the src page from parent and add to free list.
         */
@@ -7181,11 +7332,18 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
        }
        csrc->mc_top++;
 
-       rc = mdb_midl_append(&csrc->mc_txn->mt_free_pgs,
-               csrc->mc_pg[csrc->mc_top]->mp_pgno);
-       if (rc)
-               return rc;
-       if (IS_LEAF(csrc->mc_pg[csrc->mc_top]))
+       psrc = csrc->mc_pg[csrc->mc_top];
+       /* If not operating on FreeDB, allow this page to be reused
+        * in this txn.
+        */
+       if ((psrc->mp_flags & P_DIRTY) && csrc->mc_dbi != FREE_DBI) {
+               mdb_page_loose(csrc->mc_txn->mt_env, psrc);
+       } else {
+               rc = mdb_midl_append(&csrc->mc_txn->mt_free_pgs, psrc->mp_pgno);
+               if (rc)
+                       return rc;
+       }
+       if (IS_LEAF(psrc))
                csrc->mc_db->md_leaf_pages--;
        else
                csrc->mc_db->md_branch_pages--;
@@ -7193,7 +7351,6 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                /* Adjust other cursors pointing to mp */
                MDB_cursor *m2, *m3;
                MDB_dbi dbi = csrc->mc_dbi;
-               MDB_page *mp = cdst->mc_pg[cdst->mc_top];
 
                for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
                        if (csrc->mc_flags & C_SUB)
@@ -7202,15 +7359,24 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                                m3 = m2;
                        if (m3 == csrc) continue;
                        if (m3->mc_snum < csrc->mc_snum) continue;
-                       if (m3->mc_pg[csrc->mc_top] == csrc->mc_pg[csrc->mc_top]) {
-                               m3->mc_pg[csrc->mc_top] = mp;
+                       if (m3->mc_pg[csrc->mc_top] == psrc) {
+                               m3->mc_pg[csrc->mc_top] = pdst;
                                m3->mc_ki[csrc->mc_top] += nkeys;
                        }
                }
        }
-       mdb_cursor_pop(csrc);
-
-       return mdb_rebalance(csrc);
+       {
+               unsigned int snum = cdst->mc_snum;
+               uint16_t depth = cdst->mc_db->md_depth;
+               mdb_cursor_pop(cdst);
+               rc = mdb_rebalance(cdst);
+               /* Did the tree shrink? */
+               if (depth > cdst->mc_db->md_depth)
+                       snum--;
+               cdst->mc_snum = snum;
+               cdst->mc_top = snum-1;
+       }
+       return rc;
 }
 
 /** Copy the contents of a cursor.
@@ -7248,6 +7414,7 @@ mdb_rebalance(MDB_cursor *mc)
        int rc;
        unsigned int ptop, minkeys;
        MDB_cursor      mn;
+       indx_t oldki;
 
        minkeys = 1 + (IS_BRANCH(mc->mc_pg[mc->mc_top]));
        DPRINTF(("rebalancing %s page %"Z"u (has %u keys, %.1f%% full)",
@@ -7298,6 +7465,7 @@ mdb_rebalance(MDB_cursor *mc)
                                }
                        }
                } else if (IS_BRANCH(mp) && NUMKEYS(mp) == 1) {
+                       int i;
                        DPUTS("collapsing root page!");
                        rc = mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
                        if (rc)
@@ -7309,6 +7477,10 @@ mdb_rebalance(MDB_cursor *mc)
                        mc->mc_db->md_depth--;
                        mc->mc_db->md_branch_pages--;
                        mc->mc_ki[0] = mc->mc_ki[1];
+                       for (i = 1; i<mc->mc_db->md_depth; i++) {
+                               mc->mc_pg[i] = mc->mc_pg[i+1];
+                               mc->mc_ki[i] = mc->mc_ki[i+1];
+                       }
                        {
                                /* Adjust other cursors pointing to mp */
                                MDB_cursor *m2, *m3;
@@ -7321,7 +7493,6 @@ mdb_rebalance(MDB_cursor *mc)
                                                m3 = m2;
                                        if (m3 == mc || m3->mc_snum < mc->mc_snum) continue;
                                        if (m3->mc_pg[0] == mp) {
-                                               int i;
                                                m3->mc_snum--;
                                                m3->mc_top--;
                                                for (i=0; i<m3->mc_snum; i++) {
@@ -7352,6 +7523,7 @@ mdb_rebalance(MDB_cursor *mc)
        mdb_cursor_copy(mc, &mn);
        mn.mc_xcursor = NULL;
 
+       oldki = mc->mc_ki[mc->mc_top];
        if (mc->mc_ki[ptop] == 0) {
                /* We're the leftmost leaf in our parent.
                 */
@@ -7385,49 +7557,41 @@ mdb_rebalance(MDB_cursor *mc)
         * (A branch page must never have less than 2 keys.)
         */
        minkeys = 1 + (IS_BRANCH(mn.mc_pg[mn.mc_top]));
-       if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys)
-               return mdb_node_move(&mn, mc);
-       else {
-               if (mc->mc_ki[ptop] == 0)
+       if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys) {
+               rc = mdb_node_move(&mn, mc);
+               if (mc->mc_ki[ptop]) {
+                       oldki++;
+               }
+       } else {
+               if (mc->mc_ki[ptop] == 0) {
                        rc = mdb_page_merge(&mn, mc);
-               else {
+               } else {
+                       oldki += NUMKEYS(mn.mc_pg[mn.mc_top]);
                        mn.mc_ki[mn.mc_top] += mc->mc_ki[mn.mc_top] + 1;
                        rc = mdb_page_merge(mc, &mn);
                        mdb_cursor_copy(&mn, mc);
                }
-               mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
+               mc->mc_flags &= ~C_EOF;
        }
+       mc->mc_ki[mc->mc_top] = oldki;
        return rc;
 }
 
 /** Complete a delete operation started by #mdb_cursor_del(). */
 static int
-mdb_cursor_del0(MDB_cursor *mc, MDB_node *leaf)
+mdb_cursor_del0(MDB_cursor *mc)
 {
        int rc;
        MDB_page *mp;
        indx_t ki;
        unsigned int nkeys;
 
-       mp = mc->mc_pg[mc->mc_top];
        ki = mc->mc_ki[mc->mc_top];
-
-       /* add overflow pages to free list */
-       if (!IS_LEAF2(mp) && F_ISSET(leaf->mn_flags, F_BIGDATA)) {
-               MDB_page *omp;
-               pgno_t pg;
-
-               memcpy(&pg, NODEDATA(leaf), sizeof(pg));
-               if ((rc = mdb_page_get(mc->mc_txn, pg, &omp, NULL)) ||
-                       (rc = mdb_ovpage_free(mc, omp)))
-                       return rc;
-       }
        mdb_node_del(mc, mc->mc_db->md_pad);
        mc->mc_db->md_entries--;
        rc = mdb_rebalance(mc);
-       if (rc != MDB_SUCCESS)
-               mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
-       else {
+
+       if (rc == MDB_SUCCESS) {
                MDB_cursor *m2, *m3;
                MDB_dbi dbi = mc->mc_dbi;
 
@@ -7435,11 +7599,14 @@ mdb_cursor_del0(MDB_cursor *mc, MDB_node *leaf)
                nkeys = NUMKEYS(mp);
 
                /* if mc points past last node in page, find next sibling */
-               if (mc->mc_ki[mc->mc_top] >= nkeys)
-                       mdb_cursor_sibling(mc, 1);
+               if (mc->mc_ki[mc->mc_top] >= nkeys) {
+                       rc = mdb_cursor_sibling(mc, 1);
+                       if (rc == MDB_NOTFOUND)
+                               rc = MDB_SUCCESS;
+               }
 
                /* Adjust other cursors pointing to mp */
-               for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+               for (m2 = mc->mc_txn->mt_cursors[dbi]; !rc && m2; m2=m2->mc_next) {
                        m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
                        if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
                                continue;
@@ -7451,45 +7618,54 @@ mdb_cursor_del0(MDB_cursor *mc, MDB_node *leaf)
                                        if (m3->mc_ki[mc->mc_top] > ki)
                                                m3->mc_ki[mc->mc_top]--;
                                }
-                               if (m3->mc_ki[mc->mc_top] >= nkeys)
-                                       mdb_cursor_sibling(m3, 1);
+                               if (m3->mc_ki[mc->mc_top] >= nkeys) {
+                                       rc = mdb_cursor_sibling(m3, 1);
+                                       if (rc == MDB_NOTFOUND)
+                                               rc = MDB_SUCCESS;
+                               }
                        }
                }
                mc->mc_flags |= C_DEL;
        }
 
+       if (rc)
+               mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
        return rc;
 }
 
 int
 mdb_del(MDB_txn *txn, MDB_dbi dbi,
     MDB_val *key, MDB_val *data)
+{
+       if (!key || dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
+               return EINVAL;
+
+       if (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR))
+               return (txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
+
+       if (!F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
+               /* must ignore any data */
+               data = NULL;
+       }
+
+       return mdb_del0(txn, dbi, key, data, 0);
+}
+
+static int
+mdb_del0(MDB_txn *txn, MDB_dbi dbi,
+       MDB_val *key, MDB_val *data, unsigned flags)
 {
        MDB_cursor mc;
        MDB_xcursor mx;
        MDB_cursor_op op;
        MDB_val rdata, *xdata;
-       int              rc, exact;
+       int              rc, exact = 0;
        DKBUF;
 
-       if (key == NULL)
-               return EINVAL;
-
        DPRINTF(("====> delete db %u key [%s]", dbi, DKEY(key)));
 
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
-               return EINVAL;
-
-       if (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR))
-               return (txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
-
        mdb_cursor_init(&mc, txn, dbi, &mx);
 
-       exact = 0;
-       if (!F_ISSET(txn->mt_dbs[dbi].md_flags, MDB_DUPSORT)) {
-               /* must ignore any data */
-               data = NULL;
-       }
        if (data) {
                op = MDB_GET_BOTH;
                rdata = *data;
@@ -7497,6 +7673,7 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
        } else {
                op = MDB_SET;
                xdata = NULL;
+               flags |= MDB_NODUPDATA;
        }
        rc = mdb_cursor_set(&mc, key, xdata, op, &exact);
        if (rc == 0) {
@@ -7511,7 +7688,7 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
                mc.mc_flags |= C_UNTRACK;
                mc.mc_next = txn->mt_cursors[dbi];
                txn->mt_cursors[dbi] = &mc;
-               rc = mdb_cursor_del(&mc, data ? 0 : MDB_NODUPDATA);
+               rc = mdb_cursor_del(&mc, flags);
                txn->mt_cursors[dbi] = mc.mc_next;
        }
        return rc;
@@ -7560,7 +7737,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
 
        if (mc->mc_snum < 2) {
                if ((rc = mdb_page_new(mc, P_BRANCH, 1, &pp)))
-                       return rc;
+                       goto done;
                /* shift current top to make room for new parent */
                mc->mc_pg[1] = mc->mc_pg[0];
                mc->mc_ki[1] = mc->mc_ki[0];
@@ -7578,7 +7755,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                        mc->mc_ki[0] = mc->mc_ki[1];
                        mc->mc_db->md_root = mp->mp_pgno;
                        mc->mc_db->md_depth--;
-                       return rc;
+                       goto done;
                }
                mc->mc_snum = 2;
                mc->mc_top = 1;
@@ -7607,7 +7784,6 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                        int x;
                        unsigned int lsize, rsize, ksize;
                        /* Move half of the keys to the right sibling */
-                       copy = NULL;
                        x = mc->mc_ki[mc->mc_top] - split_indx;
                        ksize = mc->mc_db->md_pad;
                        split = LEAF2KEY(mp, split_indx, ksize);
@@ -7654,8 +7830,10 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
 
                        /* grab a page to hold a temporary copy */
                        copy = mdb_page_malloc(mc->mc_txn, 1);
-                       if (copy == NULL)
-                               return ENOMEM;
+                       if (copy == NULL) {
+                               rc = ENOMEM;
+                               goto done;
+                       }
                        copy->mp_pgno  = mp->mp_pgno;
                        copy->mp_flags = mp->mp_flags;
                        copy->mp_lower = PAGEHDRSZ;
@@ -7735,6 +7913,8 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                mn.mc_top--;
                did_split = 1;
                rc = mdb_page_split(&mn, &sepkey, NULL, rp->mp_pgno, 0);
+               if (rc)
+                       goto done;
 
                /* root split? */
                if (mn.mc_snum == mc->mc_snum) {
@@ -7756,7 +7936,13 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                                mc->mc_ki[i] = mn.mc_ki[i];
                        }
                        mc->mc_pg[ptop] = mn.mc_pg[ptop];
-                       mc->mc_ki[ptop] = mn.mc_ki[ptop] - 1;
+                       if (mn.mc_ki[ptop]) {
+                               mc->mc_ki[ptop] = mn.mc_ki[ptop] - 1;
+                       } else {
+                               /* find right page's left sibling */
+                               mc->mc_ki[ptop] = mn.mc_ki[ptop];
+                               mdb_cursor_sibling(mc, 0);
+                       }
                }
        } else {
                mn.mc_top--;
@@ -7765,14 +7951,14 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
        }
        mc->mc_flags ^= C_SPLITTING;
        if (rc != MDB_SUCCESS) {
-               return rc;
+               goto done;
        }
        if (nflags & MDB_APPEND) {
                mc->mc_pg[mc->mc_top] = rp;
                mc->mc_ki[mc->mc_top] = 0;
                rc = mdb_node_add(mc, 0, newkey, newdata, newpgno, nflags);
                if (rc)
-                       return rc;
+                       goto done;
                for (i=0; i<mc->mc_top; i++)
                        mc->mc_ki[i] = mn.mc_ki[i];
        } else if (!IS_LEAF2(mp)) {
@@ -7810,11 +7996,8 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                        }
 
                        rc = mdb_node_add(mc, j, &rkey, rdata, pgno, flags);
-                       if (rc) {
-                               /* return tmp page to freelist */
-                               mdb_page_free(env, copy);
-                               return rc;
-                       }
+                       if (rc)
+                               goto done;
                        if (i == nkeys) {
                                i = 0;
                                j = 0;
@@ -7848,16 +8031,12 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                         */
                        if (mn.mc_pg[ptop] != mc->mc_pg[ptop] &&
                                mc->mc_ki[ptop] >= NUMKEYS(mc->mc_pg[ptop])) {
-                               for (i=0; i<ptop; i++) {
+                               for (i=0; i<=ptop; i++) {
                                        mc->mc_pg[i] = mn.mc_pg[i];
                                        mc->mc_ki[i] = mn.mc_ki[i];
                                }
-                               mc->mc_pg[ptop] = mn.mc_pg[ptop];
-                               mc->mc_ki[ptop] = mn.mc_ki[ptop] - 1;
                        }
                }
-               /* return tmp page to freelist */
-               mdb_page_free(env, copy);
        }
 
        {
@@ -7908,6 +8087,12 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                }
        }
        DPRINTF(("mp left: %d, rp left: %d", SIZELEFT(mp), SIZELEFT(rp)));
+
+done:
+       if (copy)                                       /* tmp page */
+               mdb_page_free(env, copy);
+       if (rc)
+               mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
        return rc;
 }
 
@@ -7918,10 +8103,7 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
        MDB_cursor mc;
        MDB_xcursor mx;
 
-       if (key == NULL || data == NULL)
-               return EINVAL;
-
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (!key || !data || dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA|MDB_RESERVE|MDB_APPEND|MDB_APPENDDUP)) != flags)
@@ -8180,9 +8362,12 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
 
 int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
 {
-       if (txn == NULL || arg == NULL || dbi >= txn->mt_numdbs)
+       if (!arg || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
+       if (txn->mt_flags & MDB_TXN_ERROR)
+               return MDB_BAD_TXN;
+
        if (txn->mt_dbflags[dbi] & DB_STALE) {
                MDB_cursor mc;
                MDB_xcursor mx;
@@ -8207,7 +8392,7 @@ void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
 int mdb_dbi_flags(MDB_txn *txn, MDB_dbi dbi, unsigned int *flags)
 {
        /* We could return the flags for the FREE_DBI too but what's the point? */
-       if (txn == NULL || dbi < MAIN_DBI || dbi >= txn->mt_numdbs)
+       if (dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
        *flags = txn->mt_dbs[dbi].md_flags & PERSISTENT_FLAGS;
        return MDB_SUCCESS;
@@ -8247,22 +8432,22 @@ mdb_drop0(MDB_cursor *mc, int subs)
                                                memcpy(&pg, NODEDATA(ni), sizeof(pg));
                                                rc = mdb_page_get(txn, pg, &omp, NULL);
                                                if (rc != 0)
-                                                       return rc;
+                                                       goto done;
                                                mdb_cassert(mc, IS_OVERFLOW(omp));
                                                rc = mdb_midl_append_range(&txn->mt_free_pgs,
                                                        pg, omp->mp_pages);
                                                if (rc)
-                                                       return rc;
+                                                       goto done;
                                        } else if (subs && (ni->mn_flags & F_SUBDATA)) {
                                                mdb_xcursor_init1(mc, ni);
                                                rc = mdb_drop0(&mc->mc_xcursor->mx_cursor, 0);
                                                if (rc)
-                                                       return rc;
+                                                       goto done;
                                        }
                                }
                        } else {
                                if ((rc = mdb_midl_need(&txn->mt_free_pgs, n)) != 0)
-                                       return rc;
+                                       goto done;
                                for (i=0; i<n; i++) {
                                        pgno_t pg;
                                        ni = NODEPTR(mp, i);
@@ -8276,6 +8461,8 @@ mdb_drop0(MDB_cursor *mc, int subs)
                        mc->mc_ki[mc->mc_top] = i;
                        rc = mdb_cursor_sibling(mc, 1);
                        if (rc) {
+                               if (rc != MDB_NOTFOUND)
+                                       goto done;
                                /* no more siblings, go back to beginning
                                 * of previous level.
                                 */
@@ -8289,6 +8476,9 @@ mdb_drop0(MDB_cursor *mc, int subs)
                }
                /* free it */
                rc = mdb_midl_append(&txn->mt_free_pgs, mc->mc_db->md_root);
+done:
+               if (rc)
+                       txn->mt_flags |= MDB_TXN_ERROR;
        } else if (rc == MDB_NOTFOUND) {
                rc = MDB_SUCCESS;
        }
@@ -8300,7 +8490,7 @@ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
        MDB_cursor *mc, *m2;
        int rc;
 
-       if (!txn || !dbi || dbi >= txn->mt_numdbs || (unsigned)del > 1 || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if ((unsigned)del > 1 || dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
@@ -8319,10 +8509,12 @@ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
 
        /* Can't delete the main DB */
        if (del && dbi > MAIN_DBI) {
-               rc = mdb_del(txn, MAIN_DBI, &mc->mc_dbx->md_name, NULL);
+               rc = mdb_del0(txn, MAIN_DBI, &mc->mc_dbx->md_name, NULL, 0);
                if (!rc) {
                        txn->mt_dbflags[dbi] = DB_STALE;
                        mdb_dbi_close(txn->mt_env, dbi);
+               } else {
+                       txn->mt_flags |= MDB_TXN_ERROR;
                }
        } else {
                /* reset the DB record, mark it dirty */
@@ -8343,7 +8535,7 @@ leave:
 
 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_cmp = cmp;
@@ -8352,7 +8544,7 @@ int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 
 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_dcmp = cmp;
@@ -8361,7 +8553,7 @@ int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 
 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_rel = rel;
@@ -8370,7 +8562,7 @@ int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
 
 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
+       if (dbi == FREE_DBI || !TXN_DBI_EXIST(txn, dbi))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_relctx = ctx;