]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
Simplify last commits
[openldap] / libraries / liblmdb / mdb.c
index 9a8521c4322b12ca0d1b41b0927e98eb6b695575..640419c6e746cd9495054977e9bc5639b4c950f4 100644 (file)
  *     @{
  */
 #ifdef _WIN32
+#define MDB_PIDLOCK    0
 #define pthread_t      DWORD
 #define pthread_mutex_t        HANDLE
 #define pthread_key_t  DWORD
 #define GET_PAGESIZE(x) {SYSTEM_INFO si; GetSystemInfo(&si); (x) = si.dwPageSize;}
 #define        close(fd)       (CloseHandle(fd) ? 0 : -1)
 #define        munmap(ptr,len) UnmapViewOfFile(ptr)
+#ifdef PROCESS_QUERY_LIMITED_INFORMATION
+#define MDB_PROCESS_QUERY_LIMITED_INFORMATION PROCESS_QUERY_LIMITED_INFORMATION
+#else
+#define MDB_PROCESS_QUERY_LIMITED_INFORMATION 0x1000
+#endif
 #define        Z       "I"
 #else
 
 #define        Z       "z"
 
+       /** For MDB_LOCK_FORMAT: True if readers take a pid lock in the lockfile */
+#define MDB_PIDLOCK                    1
+
 #ifdef MDB_USE_POSIX_SEM
 
 #define LOCK_MUTEX_R(env)      mdb_sem_wait((env)->me_rmutex)
@@ -284,33 +293,30 @@ typedef MDB_ID    txnid_t;
  *     @{
  */
 #ifndef MDB_DEBUG
-       /**     Enable debug output.
+       /**     Enable debug output.  Needs variable argument macros (a C99 feature).
         *      Set this to 1 for copious tracing. Set to 2 to add dumps of all IDLs
         *      read from and written to the database (used for free space management).
         */
 #define MDB_DEBUG 0
 #endif
 
-#if !(__STDC_VERSION__ >= 199901L || defined(__GNUC__))
-# undef  MDB_DEBUG
-# define MDB_DEBUG     0
-# define DPRINTF       (void)  /* Vararg macros may be unsupported */
-#elif MDB_DEBUG
+#if MDB_DEBUG
 static int mdb_debug;
 static txnid_t mdb_debug_start;
 
-       /**     Print a debug message with printf formatting. */
-# define DPRINTF(fmt, ...)     /**< Requires 2 or more args */ \
-       ((void) ((mdb_debug) && \
-        fprintf(stderr, "%s:%d " fmt "\n", __func__, __LINE__, __VA_ARGS__)))
+       /**     Print a debug message with printf formatting.
+        *      Requires double parenthesis around 2 or more args.
+        */
+# define DPRINTF(args) ((void) ((mdb_debug) && DPRINTF0 args))
+# define DPRINTF0(fmt, ...) \
+       fprintf(stderr, "%s:%d " fmt "\n", __func__, __LINE__, __VA_ARGS__)
 #else
-# define DPRINTF(fmt, ...)     ((void) 0)
-# define MDB_DEBUG_SKIP
+# define DPRINTF(args) ((void) 0)
 #endif
        /**     Print a debug string.
         *      The string is printed literally, with no format processing.
         */
-#define DPUTS(arg)     DPRINTF("%s", arg)
+#define DPUTS(arg)     DPRINTF(("%s", arg))
 /** @} */
 
        /** A default memory page size.
@@ -386,7 +392,7 @@ static txnid_t mdb_debug_start;
         */
 #define        DKEY(x) mdb_dkey(x, kbuf)
 #else
-#define        DKBUF   typedef int dummy_kbuf  /* so we can put ';' after */
+#define        DKBUF
 #define DKEY(x)        0
 #endif
 
@@ -518,8 +524,8 @@ typedef struct MDB_txbody {
                /** Stamp identifying this as an MDB file. It must be set
                 *      to #MDB_MAGIC. */
        uint32_t        mtb_magic;
-               /** Version number of this lock file. Must be set to #MDB_LOCK_VERSION. */
-       uint32_t        mtb_version;
+               /** Format of this lock file. Must be set to #MDB_LOCK_FORMAT. */
+       uint32_t        mtb_format;
 #if defined(_WIN32) || defined(MDB_USE_POSIX_SEM)
        char    mtb_rmname[MNAME_LEN];
 #else
@@ -545,7 +551,7 @@ typedef struct MDB_txninfo {
        union {
                MDB_txbody mtb;
 #define mti_magic      mt1.mtb.mtb_magic
-#define mti_version    mt1.mtb.mtb_version
+#define mti_format     mt1.mtb.mtb_format
 #define mti_mutex      mt1.mtb.mtb_mutex
 #define mti_rmname     mt1.mtb.mtb_rmname
 #define mti_txnid      mt1.mtb.mtb_txnid
@@ -564,6 +570,13 @@ typedef struct MDB_txninfo {
        } mt2;
        MDB_reader      mti_readers[1];
 } MDB_txninfo;
+
+       /** Lockfile format signature: version, features and field layout */
+#define MDB_LOCK_FORMAT \
+       ((uint32_t) \
+        ((MDB_LOCK_VERSION) \
+         /* Flags which describe functionality */ \
+         + (((MDB_PIDLOCK) != 0) << 16)))
 /** @} */
 
 /** Common header for all page types.
@@ -743,9 +756,12 @@ typedef struct MDB_node {
         */
 #define LEAF2KEY(p, i, ks)     ((char *)(p) + PAGEHDRSZ + ((i)*(ks)))
 
-       /** Set the \b node's key into \b key, if requested. */
-#define MDB_GET_KEY(node, key) { if ((key) != NULL) { \
-       (key)->mv_size = NODEKSZ(node); (key)->mv_data = NODEKEY(node); } }
+       /** Set the \b node's key into \b keyptr, if requested. */
+#define MDB_GET_KEY(node, keyptr)      { if ((keyptr) != NULL) { \
+       (keyptr)->mv_size = NODEKSZ(node); (keyptr)->mv_data = NODEKEY(node); } }
+
+       /** Set the \b node's key into \b key. */
+#define MDB_GET_KEY2(node, key)        { key.mv_size = NODEKSZ(node); key.mv_data = NODEKEY(node); }
 
        /** Information about a single database in the environment. */
 typedef struct MDB_db {
@@ -1109,8 +1125,10 @@ static char *const mdb_errstr[] = {
        "MDB_CURSOR_FULL: Internal error - cursor stack limit reached",
        "MDB_PAGE_FULL: Internal error - page has no more space",
        "MDB_MAP_RESIZED: Database contents grew beyond environment mapsize",
-       "MDB_INCOMPATIBLE: Database flags changed or would change",
+       "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",
 };
 
 char *
@@ -1207,7 +1225,7 @@ mdb_cursor_chk(MDB_cursor *mc)
 }
 #endif
 
-#if MDB_DEBUG > 2
+#if (MDB_DEBUG) > 2
 /** Count all the pages in each DB and in the freelist
  *  and make sure it matches the actual number of pages
  *  being used.
@@ -1336,45 +1354,61 @@ mdb_dlist_free(MDB_txn *txn)
        dl[0].mid = 0;
 }
 
-/* Set or clear P_KEEP in non-overflow, non-sub pages in known cursors.
- * When clearing, only consider backup cursors (from parent txns) since
- * other P_KEEP flags have already been cleared.
+/* 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:
  * P_DIRTY to set P_KEEP, P_DIRTY|P_KEEP to clear it.
+ * @param[in] all No shortcuts. Needed except after a full #mdb_page_flush().
+ * @return 0 on success, non-zero on failure.
  */
-static void
-mdb_cursorpages_mark(MDB_cursor *mc, unsigned pflags)
+static int
+mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
 {
        MDB_txn *txn = mc->mc_txn;
-       MDB_cursor *m2, *m3;
+       MDB_cursor *m3;
        MDB_xcursor *mx;
+       MDB_page *dp;
        unsigned i, j;
+       int rc = MDB_SUCCESS, level;
 
+       /* Mark pages seen by cursors */
        if (mc->mc_flags & C_UNTRACK)
                mc = NULL;                              /* will find mc in mt_cursors */
        for (i = txn->mt_numdbs;; mc = txn->mt_cursors[--i]) {
                for (; mc; mc=mc->mc_next) {
-                       m2 = pflags == P_DIRTY ? mc : mc->mc_backup;
-                       for (; m2; m2 = m2->mc_backup) {
-                               for (m3=m2; m3->mc_flags & C_INITIALIZED; m3=&mx->mx_cursor) {
+                       for (m3 = mc; m3->mc_flags & C_INITIALIZED; m3 = &mx->mx_cursor) {
                                        for (j=0; j<m3->mc_snum; j++)
                                                if ((m3->mc_pg[j]->mp_flags & (P_SUBP|P_DIRTY|P_KEEP))
                                                                == pflags)
                                                        m3->mc_pg[j]->mp_flags ^= P_KEEP;
-                                       if (!(m3->mc_db->md_flags & MDB_DUPSORT))
+                                       mx = m3->mc_xcursor;
+                                       if (mx == NULL)
                                                break;
-                                       /* Cursor backups have mx malloced at the end of m2 */
-                                       mx = (m3 == mc ? m3->mc_xcursor : (MDB_xcursor *)(m3+1));
-                               }
                        }
                }
                if (i == 0)
                        break;
        }
+
+       if (all) {
+               /* Mark dirty root pages */
+               for (i=0; i<txn->mt_numdbs; i++) {
+                       if (txn->mt_dbflags[i] & DB_DIRTY) {
+                               pgno_t pgno = txn->mt_dbs[i].md_root;
+                               if (pgno == P_INVALID)
+                                       continue;
+                               if ((rc = mdb_page_get(txn, pgno, &dp, &level)) != MDB_SUCCESS)
+                                       break;
+                               if ((dp->mp_flags & (P_DIRTY|P_KEEP)) == pflags && level <= 1)
+                                       dp->mp_flags ^= P_KEEP;
+                       }
+               }
+       }
+
+       return rc;
 }
 
-static int mdb_page_flush(MDB_txn *txn);
+static int mdb_page_flush(MDB_txn *txn, int keep);
 
 /**    Spill pages from the dirty list back to disk.
  * This is intended to prevent running into #MDB_TXN_FULL situations,
@@ -1417,7 +1451,7 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
        MDB_txn *txn = m0->mc_txn;
        MDB_page *dp;
        MDB_ID2L dl = txn->mt_u.dirty_list;
-       unsigned int i, j;
+       unsigned int i, j, need;
        int rc;
 
        if (m0->mc_flags & C_SUB)
@@ -1432,6 +1466,7 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
        if (key)
                i += (LEAFSIZE(key, data) + txn->mt_env->me_psize) / txn->mt_env->me_psize;
        i += i; /* double it for good measure */
+       need = i;
 
        if (txn->mt_dirty_room > i)
                return MDB_SUCCESS;
@@ -1442,22 +1477,23 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
                        return ENOMEM;
        }
 
-       /* Mark all the dirty root pages we want to preserve */
-       for (i=0; i<txn->mt_numdbs; i++) {
-               if (txn->mt_dbflags[i] & DB_DIRTY) {
-                       j = mdb_mid2l_search(dl, txn->mt_dbs[i].md_root);
-                       if (j <= dl[0].mid) {
-                               dp = dl[j].mptr;
-                               dp->mp_flags |= P_KEEP;
-                       }
-               }
-       }
+       /* Preserve pages which may soon be dirtied again */
+       if ((rc = mdb_pages_xkeep(m0, P_DIRTY, 1)) != MDB_SUCCESS)
+               goto done;
 
-       /* Preserve pages used by cursors */
-       mdb_cursorpages_mark(m0, P_DIRTY);
+       /* Less aggressive spill - we originally spilled the entire dirty list,
+        * with a few exceptions for cursor pages and DB root pages. But this
+        * turns out to be a lot of wasted effort because in a large txn many
+        * of those pages will need to be used again. So now we spill only 1/8th
+        * of the dirty pages. Testing revealed this to be a good tradeoff,
+        * better than 1/2, 1/4, or 1/10.
+        */
+       if (need < MDB_IDL_UM_MAX / 8)
+               need = MDB_IDL_UM_MAX / 8;
 
        /* Save the page IDs of all the pages we're flushing */
-       for (i=1; i<=dl[0].mid; i++) {
+       /* flush from the tail forward, this saves a lot of shifting later on. */
+       for (i=dl[0].mid; i && need; i--) {
                dp = dl[i].mptr;
                if (dp->mp_flags & P_KEEP)
                        continue;
@@ -1479,14 +1515,19 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
                                continue;
                }
                if ((rc = mdb_midl_append(&txn->mt_spill_pgs, dl[i].mid)))
-                       return rc;
+                       goto done;
+               need--;
        }
        mdb_midl_sort(txn->mt_spill_pgs);
 
-       rc = mdb_page_flush(txn);
+       /* Flush the spilled part of dirty list */
+       if ((rc = mdb_page_flush(txn, i)) != MDB_SUCCESS)
+               goto done;
 
-       mdb_cursorpages_mark(m0, P_DIRTY|P_KEEP);
+       /* Reset any dirty pages we kept that page_flush didn't see */
+       rc = mdb_pages_xkeep(m0, P_DIRTY|P_KEEP, i);
 
+done:
        if (rc == 0) {
                if (txn->mt_parent) {
                        MDB_txn *tx2;
@@ -1509,6 +1550,8 @@ mdb_page_spill(MDB_cursor *m0, MDB_val *key, MDB_val *data)
                        txn->mt_dirty_room = MDB_IDL_UM_MAX - dl[0].mid;
                }
                txn->mt_flags |= MDB_TXN_SPILLS;
+       } else {
+               txn->mt_flags |= MDB_TXN_ERROR;
        }
        return rc;
 }
@@ -1657,11 +1700,11 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                        mop = env->me_pghead;
                }
                env->me_pglast = last;
-#if MDB_DEBUG > 1
-               DPRINTF("IDL read txn %"Z"u root %"Z"u num %u",
-                               last, txn->mt_dbs[FREE_DBI].md_root, i);
+#if (MDB_DEBUG) > 1
+               DPRINTF(("IDL read txn %"Z"u root %"Z"u num %u",
+                       last, txn->mt_dbs[FREE_DBI].md_root, i));
                for (k = i; k; k--)
-                       DPRINTF("IDL %"Z"u", idl[k]);
+                       DPRINTF(("IDL %"Z"u", idl[k]));
 #endif
                /* Merge in descending sorted order */
                j = mop_len;
@@ -1829,7 +1872,7 @@ mdb_page_touch(MDB_cursor *mc)
                        (rc = mdb_page_alloc(mc, 1, &np)))
                        return rc;
                pgno = np->mp_pgno;
-               DPRINTF("touched db %u page %"Z"u -> %"Z"u", mc->mc_dbi,mp->mp_pgno,pgno);
+               DPRINTF(("touched db %u page %"Z"u -> %"Z"u", mc->mc_dbi,mp->mp_pgno,pgno));
                assert(mp->mp_pgno != pgno);
                mdb_midl_xappend(txn->mt_free_pgs, mp->mp_pgno);
                /* Update the parent page, if any, to point to the new page */
@@ -2002,13 +2045,13 @@ mdb_cursors_close(MDB_txn *txn, unsigned merge)
        }
 }
 
-#ifdef MDB_DEBUG_SKIP
+#if !(MDB_DEBUG)
 #define mdb_txn_reset0(txn, act) mdb_txn_reset0(txn)
 #endif
 static void
 mdb_txn_reset0(MDB_txn *txn, const char *act);
 
-#ifdef _WIN32
+#if !(MDB_PIDLOCK)             /* Currently the same as defined(_WIN32) */
 enum Pidlock_op {
        Pidset, Pidcheck
 };
@@ -2019,7 +2062,8 @@ enum Pidlock_op {
 #endif
 
 /** Set or check a pid lock. Set returns 0 on success.
- * Check returns 0 if lock exists (meaning the process is alive).
+ * Check returns 0 if the process is certainly dead, nonzero if it may
+ * be alive (the lock exists or an error happened so we do not know).
  *
  * On Windows Pidset is a no-op, we merely check for the existence
  * of the process with the given pid. On POSIX we use a single byte
@@ -2028,33 +2072,36 @@ enum Pidlock_op {
 static int
 mdb_reader_pid(MDB_env *env, enum Pidlock_op op, pid_t pid)
 {
-#ifdef _WIN32
+#if !(MDB_PIDLOCK)             /* Currently the same as defined(_WIN32) */
+       int ret = 0;
        HANDLE h;
-       int ver, query;
-       switch(op) {
-       case Pidset:
-               break;
-       case Pidcheck:
+       if (op == Pidcheck) {
                h = OpenProcess(env->me_pidquery, FALSE, pid);
+               /* No documented "no such process" code, but other program use this: */
                if (!h)
-                       return GetLastError();
+                       return ErrCode() != ERROR_INVALID_PARAMETER;
+               /* A process exists until all handles to it close. Has it exited? */
+               ret = WaitForSingleObject(h, 0) != 0;
                CloseHandle(h);
-               break;
        }
-       return 0;
+       return ret;
 #else
-       int rc;
-       struct flock lock_info;
-       memset((void *)&lock_info, 0, sizeof(lock_info));
-       lock_info.l_type = F_WRLCK;
-       lock_info.l_whence = SEEK_SET;
-       lock_info.l_start = pid;
-       lock_info.l_len = 1;
-       while ((rc = fcntl(env->me_lfd, op, &lock_info)) &&
-                       (rc = ErrCode()) == EINTR) ;
-       if (op == F_GETLK && rc == 0 && lock_info.l_type == F_UNLCK)
-               rc = -1;
-       return rc;
+       for (;;) {
+               int rc;
+               struct flock lock_info;
+               memset(&lock_info, 0, sizeof(lock_info));
+               lock_info.l_type = F_WRLCK;
+               lock_info.l_whence = SEEK_SET;
+               lock_info.l_start = pid;
+               lock_info.l_len = 1;
+               if ((rc = fcntl(env->me_lfd, op, &lock_info)) == 0) {
+                       if (op == F_GETLK && lock_info.l_type != F_UNLCK)
+                               rc = -1;
+               } else if ((rc = ErrCode()) == EINTR) {
+                       continue;
+               }
+               return rc;
+       }
 #endif
 }
 
@@ -2183,9 +2230,9 @@ mdb_txn_renew(MDB_txn *txn)
 
        rc = mdb_txn_renew0(txn);
        if (rc == MDB_SUCCESS) {
-               DPRINTF("renew txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
+               DPRINTF(("renew txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
                        txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
-                       (void *)txn, (void *)txn->mt_env, txn->mt_dbs[MAIN_DBI].md_root);
+                       (void *)txn, (void *)txn->mt_env, txn->mt_dbs[MAIN_DBI].md_root));
        }
        return rc;
 }
@@ -2206,10 +2253,11 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
        if (parent) {
                /* Nested transactions: Max 1 child, write txns only, no writemap */
                if (parent->mt_child ||
-                       (flags & MDB_RDONLY) || (parent->mt_flags & MDB_TXN_RDONLY) ||
+                       (flags & MDB_RDONLY) ||
+                       (parent->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR)) ||
                        (env->me_flags & MDB_WRITEMAP))
                {
-                       return EINVAL;
+                       return (parent->mt_flags & MDB_TXN_RDONLY) ? EINVAL : MDB_BAD_TXN;
                }
                tsize = sizeof(MDB_ntxn);
        }
@@ -2218,7 +2266,7 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
                size += env->me_maxdbs * sizeof(MDB_cursor *);
 
        if ((txn = calloc(1, size)) == NULL) {
-               DPRINTF("calloc: %s", strerror(ErrCode()));
+               DPRINTF(("calloc: %s", strerror(ErrCode())));
                return ENOMEM;
        }
        txn->mt_dbs = (MDB_db *) ((char *)txn + tsize);
@@ -2278,14 +2326,21 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
                free(txn);
        else {
                *ret = txn;
-               DPRINTF("begin txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
+               DPRINTF(("begin txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
                        txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
-                       (void *) txn, (void *) env, txn->mt_dbs[MAIN_DBI].md_root);
+                       (void *) txn, (void *) env, txn->mt_dbs[MAIN_DBI].md_root));
        }
 
        return rc;
 }
 
+MDB_env *
+mdb_txn_env(MDB_txn *txn)
+{
+       if(!txn) return NULL;
+       return txn->mt_env;
+}
+
 /** Export or close DBI handles opened in this txn. */
 static void
 mdb_dbis_update(MDB_txn *txn, int keep)
@@ -2325,9 +2380,9 @@ mdb_txn_reset0(MDB_txn *txn, const char *act)
        /* Close any DBI handles opened in this txn */
        mdb_dbis_update(txn, 0);
 
-       DPRINTF("%s txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
+       DPRINTF(("%s txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
                act, txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
-               (void *) txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root);
+               (void *) txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root));
 
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
                if (txn->mt_u.reader) {
@@ -2465,13 +2520,13 @@ mdb_freelist_save(MDB_txn *txn)
                        } while (freecnt < free_pgs[0]);
                        mdb_midl_sort(free_pgs);
                        memcpy(data.mv_data, free_pgs, data.mv_size);
-#if MDB_DEBUG > 1
+#if (MDB_DEBUG) > 1
                        {
                                unsigned int i = free_pgs[0];
-                               DPRINTF("IDL write txn %"Z"u root %"Z"u num %u",
-                                       txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, i);
+                               DPRINTF(("IDL write txn %"Z"u root %"Z"u num %u",
+                                       txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, i));
                                for (; i; i--)
-                                       DPRINTF("IDL %"Z"u", free_pgs[i]);
+                                       DPRINTF(("IDL %"Z"u", free_pgs[i]));
                        }
 #endif
                        continue;
@@ -2545,10 +2600,13 @@ mdb_freelist_save(MDB_txn *txn)
        return rc;
 }
 
-/** Flush dirty pages to the map, after clearing their dirty flag.
+/** Flush (some) dirty pages to the map, after clearing their dirty flag.
+ * @param[in] txn the transaction that's being committed
+ * @param[in] keep number of initial pages in dirty_list to keep dirty.
+ * @return 0 on success, non-zero on failure.
  */
 static int
-mdb_page_flush(MDB_txn *txn)
+mdb_page_flush(MDB_txn *txn, int keep)
 {
        MDB_env         *env = txn->mt_env;
        MDB_ID2L        dl = txn->mt_u.dirty_list;
@@ -2566,10 +2624,11 @@ mdb_page_flush(MDB_txn *txn)
        int                     n = 0;
 #endif
 
-       j = 0;
+       j = i = keep;
+
        if (env->me_flags & MDB_WRITEMAP) {
                /* Clear dirty flags */
-               for (i = pagecount; i; i--) {
+               while (++i <= pagecount) {
                        dp = dl[i].mptr;
                        /* Don't flush this page yet */
                        if (dp->mp_flags & P_KEEP) {
@@ -2584,8 +2643,8 @@ mdb_page_flush(MDB_txn *txn)
        }
 
        /* Write the pages */
-       for (i = 1;; i++) {
-               if (i <= pagecount) {
+       for (;;) {
+               if (++i <= pagecount) {
                        dp = dl[i].mptr;
                        /* Don't flush this page yet */
                        if (dp->mp_flags & P_KEEP) {
@@ -2610,13 +2669,13 @@ mdb_page_flush(MDB_txn *txn)
                 * the write offset, to at least save the overhead of a Seek
                 * system call.
                 */
-               DPRINTF("committing page %"Z"u", pgno);
+               DPRINTF(("committing page %"Z"u", pgno));
                memset(&ov, 0, sizeof(ov));
                ov.Offset = pos & 0xffffffff;
                ov.OffsetHigh = pos >> 16 >> 16;
                if (!WriteFile(env->me_fd, dp, size, NULL, &ov)) {
                        rc = ErrCode();
-                       DPRINTF("WriteFile: %d", rc);
+                       DPRINTF(("WriteFile: %d", rc));
                        return rc;
                }
 #else
@@ -2632,7 +2691,7 @@ mdb_page_flush(MDB_txn *txn)
                                } else {
                                        if (lseek(env->me_fd, wpos, SEEK_SET) == -1) {
                                                rc = ErrCode();
-                                               DPRINTF("lseek: %s", strerror(rc));
+                                               DPRINTF(("lseek: %s", strerror(rc)));
                                                return rc;
                                        }
                                        wres = writev(env->me_fd, iov, n);
@@ -2641,7 +2700,7 @@ mdb_page_flush(MDB_txn *txn)
                                if (wres != wsize) {
                                        if (wres < 0) {
                                                rc = ErrCode();
-                                               DPRINTF("Write error: %s", strerror(rc));
+                                               DPRINTF(("Write error: %s", strerror(rc)));
                                        } else {
                                                rc = EIO; /* TODO: Use which error code? */
                                                DPUTS("short write, filesystem full?");
@@ -2655,7 +2714,7 @@ mdb_page_flush(MDB_txn *txn)
                        wpos = pos;
                        wsize = 0;
                }
-               DPRINTF("committing page %"Z"u", pgno);
+               DPRINTF(("committing page %"Z"u", pgno));
                next_pos = pos + size;
                iov[n].iov_len = size;
                iov[n].iov_base = (char *)dp;
@@ -2664,8 +2723,7 @@ mdb_page_flush(MDB_txn *txn)
 #endif /* _WIN32 */
        }
 
-       j = 0;
-       for (i=1; i<=pagecount; i++) {
+       for (i = keep; ++i <= pagecount; ) {
                dp = dl[i].mptr;
                /* This is a page we skipped above */
                if (!dl[i].mid) {
@@ -2710,7 +2768,7 @@ mdb_txn_commit(MDB_txn *txn)
                DPUTS("error flag is set, can't commit");
                if (txn->mt_parent)
                        txn->mt_parent->mt_flags |= MDB_TXN_ERROR;
-               rc = EINVAL;
+               rc = MDB_BAD_TXN;
                goto fail;
        }
 
@@ -2830,11 +2888,12 @@ mdb_txn_commit(MDB_txn *txn)
 
        mdb_cursors_close(txn, 0);
 
-       if (!txn->mt_u.dirty_list[0].mid && !(txn->mt_flags & MDB_TXN_DIRTY))
+       if (!txn->mt_u.dirty_list[0].mid &&
+               !(txn->mt_flags & (MDB_TXN_DIRTY|MDB_TXN_SPILLS)))
                goto done;
 
-       DPRINTF("committing txn %"Z"u %p on mdbenv %p, root page %"Z"u",
-           txn->mt_txnid, (void *)txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root);
+       DPRINTF(("committing txn %"Z"u %p on mdbenv %p, root page %"Z"u",
+           txn->mt_txnid, (void*)txn, (void*)env, txn->mt_dbs[MAIN_DBI].md_root));
 
        /* Update DB root pointers */
        if (txn->mt_numdbs > 2) {
@@ -2863,11 +2922,11 @@ mdb_txn_commit(MDB_txn *txn)
        if (mdb_midl_shrink(&txn->mt_free_pgs))
                env->me_free_pgs = txn->mt_free_pgs;
 
-#if MDB_DEBUG > 2
+#if (MDB_DEBUG) > 2
        mdb_audit(txn);
 #endif
 
-       if ((rc = mdb_page_flush(txn)) ||
+       if ((rc = mdb_page_flush(txn, 0)) ||
                (rc = mdb_env_sync(env, 0)) ||
                (rc = mdb_env_write_meta(txn)))
                goto fail;
@@ -2921,14 +2980,14 @@ mdb_env_read_header(MDB_env *env, MDB_meta *meta)
                        if (rc == 0 && off == 0)
                                return ENOENT;
                        rc = rc < 0 ? (int) ErrCode() : MDB_INVALID;
-                       DPRINTF("read: %s", mdb_strerror(rc));
+                       DPRINTF(("read: %s", mdb_strerror(rc)));
                        return rc;
                }
 
                p = (MDB_page *)&pbuf;
 
                if (!F_ISSET(p->mp_flags, P_META)) {
-                       DPRINTF("page %"Z"u not a meta page", p->mp_pgno);
+                       DPRINTF(("page %"Z"u not a meta page", p->mp_pgno));
                        return MDB_INVALID;
                }
 
@@ -2939,8 +2998,8 @@ mdb_env_read_header(MDB_env *env, MDB_meta *meta)
                }
 
                if (m->mm_version != MDB_DATA_VERSION) {
-                       DPRINTF("database is version %u, expected version %u",
-                               m->mm_version, MDB_DATA_VERSION);
+                       DPRINTF(("database is version %u, expected version %u",
+                               m->mm_version, MDB_DATA_VERSION));
                        return MDB_VERSION_MISMATCH;
                }
 
@@ -3033,8 +3092,8 @@ mdb_env_write_meta(MDB_txn *txn)
        assert(txn->mt_env != NULL);
 
        toggle = !txn->mt_toggle;
-       DPRINTF("writing meta page %d for root page %"Z"u",
-               toggle, txn->mt_dbs[MAIN_DBI].md_root);
+       DPRINTF(("writing meta page %d for root page %"Z"u",
+               toggle, txn->mt_dbs[MAIN_DBI].md_root));
 
        env = txn->mt_env;
        mp = env->me_metas[toggle];
@@ -3110,6 +3169,7 @@ mdb_env_write_meta(MDB_txn *txn)
                WriteFile(env->me_fd, ptr, len, NULL, &ov);
 #else
                r2 = pwrite(env->me_fd, ptr, len, off);
+               (void)r2;       /* Silence warnings. We don't care about pwrite's return value */
 #endif
 fail:
                env->me_flags |= MDB_FATAL_ERROR;
@@ -3247,7 +3307,7 @@ mdb_env_open2(MDB_env *env)
                /* See if we should use QueryLimited */
                rc = GetVersion();
                if ((rc & 0xff) > 5)
-                       env->me_pidquery = PROCESS_QUERY_LIMITED_INFORMATION;
+                       env->me_pidquery = MDB_PROCESS_QUERY_LIMITED_INFORMATION;
                else
                        env->me_pidquery = PROCESS_QUERY_INFORMATION;
 
@@ -3327,15 +3387,15 @@ mdb_env_open2(MDB_env *env)
                int toggle = mdb_env_pick_meta(env);
                MDB_db *db = &env->me_metas[toggle]->mm_dbs[MAIN_DBI];
 
-               DPRINTF("opened database version %u, pagesize %u",
-                       env->me_metas[0]->mm_version, env->me_psize);
-               DPRINTF("using meta page %d",  toggle);
-               DPRINTF("depth: %u",           db->md_depth);
-               DPRINTF("entries: %"Z"u",        db->md_entries);
-               DPRINTF("branch pages: %"Z"u",   db->md_branch_pages);
-               DPRINTF("leaf pages: %"Z"u",     db->md_leaf_pages);
-               DPRINTF("overflow pages: %"Z"u", db->md_overflow_pages);
-               DPRINTF("root: %"Z"u",           db->md_root);
+               DPRINTF(("opened database version %u, pagesize %u",
+                       env->me_metas[0]->mm_version, env->me_psize));
+               DPRINTF(("using meta page %d",    toggle));
+               DPRINTF(("depth: %u",             db->md_depth));
+               DPRINTF(("entries: %"Z"u",        db->md_entries));
+               DPRINTF(("branch pages: %"Z"u",   db->md_branch_pages));
+               DPRINTF(("leaf pages: %"Z"u",     db->md_leaf_pages));
+               DPRINTF(("overflow pages: %"Z"u", db->md_overflow_pages));
+               DPRINTF(("root: %"Z"u",           db->md_root));
        }
 #endif
 
@@ -3580,10 +3640,9 @@ static void
 mdb_hash_enc(MDB_val *val, char *encbuf)
 {
        mdb_hash_t h = mdb_hash_val(val, MDB_HASH_INIT);
-       unsigned long *l = (unsigned long *)&h;
 
-       mdb_pack85(l[0], encbuf);
-       mdb_pack85(l[1], encbuf+5);
+       mdb_pack85(h, encbuf);
+       mdb_pack85(h>>32, encbuf+5);
        encbuf[10] = '\0';
 }
 #endif
@@ -3767,8 +3826,8 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                pthread_mutexattr_destroy(&mattr);
 #endif /* _WIN32 || MDB_USE_POSIX_SEM */
 
-               env->me_txns->mti_version = MDB_LOCK_VERSION;
                env->me_txns->mti_magic = MDB_MAGIC;
+               env->me_txns->mti_format = MDB_LOCK_FORMAT;
                env->me_txns->mti_txnid = 0;
                env->me_txns->mti_numreaders = 0;
 
@@ -3778,9 +3837,9 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                        rc = MDB_INVALID;
                        goto fail;
                }
-               if (env->me_txns->mti_version != MDB_LOCK_VERSION) {
-                       DPRINTF("lock region is version %u, expected version %u",
-                               env->me_txns->mti_version, MDB_LOCK_VERSION);
+               if (env->me_txns->mti_format != MDB_LOCK_FORMAT) {
+                       DPRINTF(("lock region has format+version 0x%x, expected 0x%x",
+                               env->me_txns->mti_format, MDB_LOCK_FORMAT));
                        rc = MDB_VERSION_MISMATCH;
                        goto fail;
                }
@@ -3871,9 +3930,12 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
                goto leave;
        }
 
-       rc = mdb_env_setup_locks(env, lpath, mode, &excl);
-       if (rc)
-               goto leave;
+       /* For RDONLY, get lockfile after we know datafile exists */
+       if (!F_ISSET(flags, MDB_RDONLY)) {
+               rc = mdb_env_setup_locks(env, lpath, mode, &excl);
+               if (rc)
+                       goto leave;
+       }
 
 #ifdef _WIN32
        if (F_ISSET(flags, MDB_RDONLY)) {
@@ -3899,6 +3961,12 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
                goto leave;
        }
 
+       if (F_ISSET(flags, MDB_RDONLY)) {
+               rc = mdb_env_setup_locks(env, lpath, mode, &excl);
+               if (rc)
+                       goto leave;
+       }
+
        if ((rc = mdb_env_open2(env)) == MDB_SUCCESS) {
                if (flags & (MDB_RDONLY|MDB_WRITEMAP)) {
                        env->me_mfd = env->me_fd;
@@ -3907,10 +3975,12 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
                         * MDB_NOSYNC/MDB_NOMETASYNC, in case these get reset.
                         */
 #ifdef _WIN32
+                       len = OPEN_EXISTING;
                        env->me_mfd = CreateFile(dpath, oflags,
                                FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, len,
                                mode | FILE_FLAG_WRITE_THROUGH, NULL);
 #else
+                       oflags &= ~O_CREAT;
                        env->me_mfd = open(dpath, oflags | MDB_DSYNC, mode);
 #endif
                        if (env->me_mfd == INVALID_HANDLE_VALUE) {
@@ -3918,7 +3988,7 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
                                goto leave;
                        }
                }
-               DPRINTF("opened dbenv %p", (void *) env);
+               DPRINTF(("opened dbenv %p", (void *) env));
                if (excl > 0) {
                        rc = mdb_env_share_locks(env, &excl);
                }
@@ -4291,9 +4361,9 @@ mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
        {
        pgno_t pgno;
        COPY_PGNO(pgno, mp->mp_pgno);
-       DPRINTF("searching %u keys in %s %spage %"Z"u",
+       DPRINTF(("searching %u keys in %s %spage %"Z"u",
            nkeys, IS_LEAF(mp) ? "leaf" : "branch", IS_SUBP(mp) ? "sub-" : "",
-           pgno);
+           pgno));
        }
 #endif
 
@@ -4320,8 +4390,8 @@ mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
                        i = (low + high) >> 1;
                        nodekey.mv_data = LEAF2KEY(mp, i, nodekey.mv_size);
                        rc = cmp(key, &nodekey);
-                       DPRINTF("found leaf index %u [%s], rc = %i",
-                           i, DKEY(&nodekey), rc);
+                       DPRINTF(("found leaf index %u [%s], rc = %i",
+                           i, DKEY(&nodekey), rc));
                        if (rc == 0)
                                break;
                        if (rc > 0)
@@ -4340,11 +4410,11 @@ mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
                        rc = cmp(key, &nodekey);
 #if MDB_DEBUG
                        if (IS_LEAF(mp))
-                               DPRINTF("found leaf index %u [%s], rc = %i",
-                                   i, DKEY(&nodekey), rc);
+                               DPRINTF(("found leaf index %u [%s], rc = %i",
+                                   i, DKEY(&nodekey), rc));
                        else
-                               DPRINTF("found branch index %u [%s -> %"Z"u], rc = %i",
-                                   i, DKEY(&nodekey), NODEPGNO(node), rc);
+                               DPRINTF(("found branch index %u [%s -> %"Z"u], rc = %i",
+                                   i, DKEY(&nodekey), NODEPGNO(node), rc));
 #endif
                        if (rc == 0)
                                break;
@@ -4391,15 +4461,15 @@ static void
 mdb_cursor_pop(MDB_cursor *mc)
 {
        if (mc->mc_snum) {
-#ifndef MDB_DEBUG_SKIP
+#if MDB_DEBUG
                MDB_page        *top = mc->mc_pg[mc->mc_top];
 #endif
                mc->mc_snum--;
                if (mc->mc_snum)
                        mc->mc_top--;
 
-               DPRINTF("popped page %"Z"u off db %u cursor %p", top->mp_pgno,
-                       mc->mc_dbi, (void *) mc);
+               DPRINTF(("popped page %"Z"u off db %u cursor %p", top->mp_pgno,
+                       mc->mc_dbi, (void *) mc));
        }
 }
 
@@ -4407,8 +4477,8 @@ mdb_cursor_pop(MDB_cursor *mc)
 static int
 mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 {
-       DPRINTF("pushing page %"Z"u on db %u cursor %p", mp->mp_pgno,
-               mc->mc_dbi, (void *) mc);
+       DPRINTF(("pushing page %"Z"u on db %u cursor %p", mp->mp_pgno,
+               mc->mc_dbi, (void *) mc));
 
        if (mc->mc_snum >= CURSOR_STACK) {
                assert(mc->mc_snum < CURSOR_STACK);
@@ -4432,12 +4502,11 @@ mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 static int
 mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret, int *lvl)
 {
+       MDB_env *env = txn->mt_env;
        MDB_page *p = NULL;
        int level;
 
-       if (!((txn->mt_flags & MDB_TXN_RDONLY) |
-                 (txn->mt_env->me_flags & MDB_WRITEMAP)))
-       {
+       if (!((txn->mt_flags & MDB_TXN_RDONLY) | (env->me_flags & MDB_WRITEMAP))) {
                MDB_txn *tx2 = txn;
                level = 1;
                do {
@@ -4451,7 +4520,7 @@ mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret, int *lvl)
                        if (tx2->mt_spill_pgs) {
                                x = mdb_midl_search(tx2->mt_spill_pgs, pgno);
                                if (x <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[x] == pgno) {
-                                       p = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
+                                       p = (MDB_page *)(env->me_map + env->me_psize * pgno);
                                        goto done;
                                }
                        }
@@ -4468,9 +4537,9 @@ mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret, int *lvl)
 
        if (pgno < txn->mt_next_pgno) {
                level = 0;
-               p = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
+               p = (MDB_page *)(env->me_map + env->me_psize * pgno);
        } else {
-               DPRINTF("page %"Z"u not found", pgno);
+               DPRINTF(("page %"Z"u not found", pgno));
                assert(p != NULL);
                return MDB_PAGE_NOTFOUND;
        }
@@ -4496,17 +4565,16 @@ static int
 mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int modify)
 {
        MDB_page        *mp = mc->mc_pg[mc->mc_top];
-       DKBUF;
        int rc;
-
+       DKBUF;
 
        while (IS_BRANCH(mp)) {
                MDB_node        *node;
                indx_t          i;
 
-               DPRINTF("branch page %"Z"u has %u keys", mp->mp_pgno, NUMKEYS(mp));
+               DPRINTF(("branch page %"Z"u has %u keys", mp->mp_pgno, NUMKEYS(mp)));
                assert(NUMKEYS(mp) > 1);
-               DPRINTF("found index 0 to page %"Z"u", NODEPGNO(NODEPTR(mp, 0)));
+               DPRINTF(("found index 0 to page %"Z"u", NODEPGNO(NODEPTR(mp, 0))));
 
                if (key == NULL)        /* Initialize cursor to first page. */
                        i = 0;
@@ -4528,8 +4596,7 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int modify)
                }
 
                if (key)
-                       DPRINTF("following index %u for key [%s]",
-                           i, DKEY(key));
+                       DPRINTF(("following index %u for key [%s]", i, DKEY(key)));
                assert(i < NUMKEYS(mp));
                node = NODEPTR(mp, i);
 
@@ -4548,13 +4615,13 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int modify)
        }
 
        if (!IS_LEAF(mp)) {
-               DPRINTF("internal error, index points to a %02X page!?",
-                   mp->mp_flags);
+               DPRINTF(("internal error, index points to a %02X page!?",
+                   mp->mp_flags));
                return MDB_CORRUPTED;
        }
 
-       DPRINTF("found leaf page %"Z"u for key [%s]", mp->mp_pgno,
-           key ? DKEY(key) : NULL);
+       DPRINTF(("found leaf page %"Z"u for key [%s]", mp->mp_pgno,
+           key ? DKEY(key) : NULL));
        mc->mc_flags |= C_INITIALIZED;
        mc->mc_flags &= ~C_EOF;
 
@@ -4606,7 +4673,7 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
         */
        if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_ERROR)) {
                DPUTS("transaction has failed, must abort");
-               return EINVAL;
+               return MDB_BAD_TXN;
        } else {
                /* Make sure we're using an up-to-date root */
                if (mc->mc_dbi > MAIN_DBI) {
@@ -4660,8 +4727,8 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
        mc->mc_snum = 1;
        mc->mc_top = 0;
 
-       DPRINTF("db %u root page %"Z"u has flags 0x%X",
-               mc->mc_dbi, root, mc->mc_pg[0]->mp_flags);
+       DPRINTF(("db %u root page %"Z"u has flags 0x%X",
+               mc->mc_dbi, root, mc->mc_pg[0]->mp_flags));
 
        if (flags & MDB_PS_MODIFY) {
                if ((rc = mdb_page_touch(mc)))
@@ -4679,33 +4746,38 @@ mdb_ovpage_free(MDB_cursor *mc, MDB_page *mp)
 {
        MDB_txn *txn = mc->mc_txn;
        pgno_t pg = mp->mp_pgno;
-       unsigned i, ovpages = mp->mp_pages;
+       unsigned x = 0, ovpages = mp->mp_pages;
        MDB_env *env = txn->mt_env;
+       MDB_IDL sl = txn->mt_spill_pgs;
        int rc;
 
-       DPRINTF("free ov page %"Z"u (%d)", pg, ovpages);
+       DPRINTF(("free ov page %"Z"u (%d)", pg, ovpages));
        /* If the page is dirty or on the spill list we just acquired it,
         * so we should give it back to our current free list, if any.
-        * Not currently supported in nested txns.
         * Otherwise put it onto the list of pages we freed in this txn.
+        *
+        * Won't create me_pghead: me_pglast must be inited along with it.
+        * Unsupported in nested txns: They would need to hide the page
+        * range in ancestor txns' dirty and spilled lists.
         */
-       if (!(mp->mp_flags & P_DIRTY) && txn->mt_spill_pgs) {
-               unsigned x = mdb_midl_search(txn->mt_spill_pgs, pg);
-               if (x <= txn->mt_spill_pgs[0] && txn->mt_spill_pgs[x] == pg) {
-                       /* This page is no longer spilled */
-                       for (; x < txn->mt_spill_pgs[0]; x++)
-                               txn->mt_spill_pgs[x] = txn->mt_spill_pgs[x+1];
-                       txn->mt_spill_pgs[0]--;
-                       goto release;
-               }
-       }
-       if ((mp->mp_flags & P_DIRTY) && !txn->mt_parent && env->me_pghead) {
-               unsigned j, x;
+       if (env->me_pghead &&
+               !txn->mt_parent &&
+               ((mp->mp_flags & P_DIRTY) ||
+                (sl && (x = mdb_midl_search(sl, pg)) <= sl[0] && sl[x] == pg)))
+       {
+               unsigned i, j;
                pgno_t *mop;
                MDB_ID2 *dl, ix, iy;
                rc = mdb_midl_need(&env->me_pghead, ovpages);
                if (rc)
                        return rc;
+               if (!(mp->mp_flags & P_DIRTY)) {
+                       /* This page is no longer spilled */
+                       for (; x < sl[0]; x++)
+                               sl[x] = sl[x+1];
+                       sl[0]--;
+                       goto release;
+               }
                /* Remove from dirty list */
                dl = txn->mt_u.dirty_list;
                x = dl[0].mid--;
@@ -4766,7 +4838,7 @@ mdb_node_read(MDB_txn *txn, MDB_node *leaf, MDB_val *data)
        data->mv_size = NODEDSZ(leaf);
        memcpy(&pgno, NODEDATA(leaf), sizeof(pgno));
        if ((rc = mdb_page_get(txn, pgno, &omp, NULL)) != 0) {
-               DPRINTF("read overflow page %"Z"u failed", pgno);
+               DPRINTF(("read overflow page %"Z"u failed", pgno));
                return rc;
        }
        data->mv_data = METADATA(omp);
@@ -4785,13 +4857,16 @@ mdb_get(MDB_txn *txn, MDB_dbi dbi,
 
        assert(key);
        assert(data);
-       DPRINTF("===> get db %u key [%s]", dbi, DKEY(key));
+       DPRINTF(("===> get 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_ERROR)
+               return MDB_BAD_TXN;
+
        if (key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
-               return EINVAL;
+               return MDB_BAD_VALSIZE;
        }
 
        mdb_cursor_init(&mc, txn, dbi, &mx);
@@ -4818,13 +4893,13 @@ mdb_cursor_sibling(MDB_cursor *mc, int move_right)
        }
 
        mdb_cursor_pop(mc);
-       DPRINTF("parent page is page %"Z"u, index %u",
-               mc->mc_pg[mc->mc_top]->mp_pgno, mc->mc_ki[mc->mc_top]);
+       DPRINTF(("parent page is page %"Z"u, index %u",
+               mc->mc_pg[mc->mc_top]->mp_pgno, mc->mc_ki[mc->mc_top]));
 
        if (move_right ? (mc->mc_ki[mc->mc_top] + 1u >= NUMKEYS(mc->mc_pg[mc->mc_top]))
                       : (mc->mc_ki[mc->mc_top] == 0)) {
-               DPRINTF("no more keys left, moving to %s sibling",
-                   move_right ? "right" : "left");
+               DPRINTF(("no more keys left, moving to %s sibling",
+                   move_right ? "right" : "left"));
                if ((rc = mdb_cursor_sibling(mc, move_right)) != MDB_SUCCESS) {
                        /* undo cursor_pop before returning */
                        mc->mc_top++;
@@ -4836,8 +4911,8 @@ mdb_cursor_sibling(MDB_cursor *mc, int move_right)
                        mc->mc_ki[mc->mc_top]++;
                else
                        mc->mc_ki[mc->mc_top]--;
-               DPRINTF("just moving to %s index key %u",
-                   move_right ? "right" : "left", mc->mc_ki[mc->mc_top]);
+               DPRINTF(("just moving to %s index key %u",
+                   move_right ? "right" : "left", mc->mc_ki[mc->mc_top]));
        }
        assert(IS_BRANCH(mc->mc_pg[mc->mc_top]));
 
@@ -4883,7 +4958,7 @@ mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                }
        }
 
-       DPRINTF("cursor_next: top page is %"Z"u in cursor %p", mp->mp_pgno, (void *) mc);
+       DPRINTF(("cursor_next: top page is %"Z"u in cursor %p", mp->mp_pgno, (void *) mc));
 
        if (mc->mc_ki[mc->mc_top] + 1u >= NUMKEYS(mp)) {
                DPUTS("=====> move to next sibling page");
@@ -4892,12 +4967,12 @@ mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                        return rc;
                }
                mp = mc->mc_pg[mc->mc_top];
-               DPRINTF("next page is %"Z"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]);
+               DPRINTF(("next page is %"Z"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
        } else
                mc->mc_ki[mc->mc_top]++;
 
-       DPRINTF("==> cursor points to page %"Z"u with %u keys, key index %u",
-           mp->mp_pgno, NUMKEYS(mp), mc->mc_ki[mc->mc_top]);
+       DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
+           mp->mp_pgno, NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
 
        if (IS_LEAF2(mp)) {
                key->mv_size = mc->mc_db->md_pad;
@@ -4953,7 +5028,7 @@ mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                }
        }
 
-       DPRINTF("cursor_prev: top page is %"Z"u in cursor %p", mp->mp_pgno, (void *) mc);
+       DPRINTF(("cursor_prev: top page is %"Z"u in cursor %p", mp->mp_pgno, (void *) mc));
 
        if (mc->mc_ki[mc->mc_top] == 0)  {
                DPUTS("=====> move to prev sibling page");
@@ -4962,14 +5037,14 @@ mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                }
                mp = mc->mc_pg[mc->mc_top];
                mc->mc_ki[mc->mc_top] = NUMKEYS(mp) - 1;
-               DPRINTF("prev page is %"Z"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]);
+               DPRINTF(("prev page is %"Z"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
        } else
                mc->mc_ki[mc->mc_top]--;
 
        mc->mc_flags &= ~C_EOF;
 
-       DPRINTF("==> cursor points to page %"Z"u with %u keys, key index %u",
-           mp->mp_pgno, NUMKEYS(mp), mc->mc_ki[mc->mc_top]);
+       DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
+           mp->mp_pgno, NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
 
        if (IS_LEAF2(mp)) {
                key->mv_size = mc->mc_db->md_pad;
@@ -5029,7 +5104,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                        nodekey.mv_data = LEAF2KEY(mp, 0, nodekey.mv_size);
                } else {
                        leaf = NODEPTR(mp, 0);
-                       MDB_GET_KEY(leaf, &nodekey);
+                       MDB_GET_KEY2(leaf, nodekey);
                }
                rc = mc->mc_dbx->md_cmp(key, &nodekey);
                if (rc == 0) {
@@ -5050,7 +5125,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                                 nkeys-1, nodekey.mv_size);
                                } else {
                                        leaf = NODEPTR(mp, nkeys-1);
-                                       MDB_GET_KEY(leaf, &nodekey);
+                                       MDB_GET_KEY2(leaf, nodekey);
                                }
                                rc = mc->mc_dbx->md_cmp(key, &nodekey);
                                if (rc == 0) {
@@ -5068,7 +5143,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                                                 mc->mc_ki[mc->mc_top], nodekey.mv_size);
                                                } else {
                                                        leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
-                                                       MDB_GET_KEY(leaf, &nodekey);
+                                                       MDB_GET_KEY2(leaf, nodekey);
                                                }
                                                rc = mc->mc_dbx->md_cmp(key, &nodekey);
                                                if (rc == 0) {
@@ -5175,7 +5250,7 @@ set1:
        /* The key already matches in all other cases */
        if (op == MDB_SET_RANGE || op == MDB_SET_KEY)
                MDB_GET_KEY(leaf, key);
-       DPRINTF("==> cursor placed on key [%s]", DKEY(key));
+       DPRINTF(("==> cursor placed on key [%s]", DKEY(key)));
 
        return rc;
 }
@@ -5280,9 +5355,13 @@ mdb_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
 {
        int              rc;
        int              exact = 0;
+       int              (*mfunc)(MDB_cursor *mc, MDB_val *key, MDB_val *data);
 
        assert(mc);
 
+       if (mc->mc_txn->mt_flags & MDB_TXN_ERROR)
+               return MDB_BAD_TXN;
+
        switch (op) {
        case MDB_GET_CURRENT:
                if (!(mc->mc_flags & C_INITIALIZED)) {
@@ -5313,39 +5392,50 @@ mdb_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                break;
        case MDB_GET_BOTH:
        case MDB_GET_BOTH_RANGE:
-               if (data == NULL || mc->mc_xcursor == NULL) {
+               if (data == NULL) {
                        rc = EINVAL;
                        break;
                }
+               if (mc->mc_xcursor == NULL) {
+                       rc = MDB_INCOMPATIBLE;
+                       break;
+               }
                /* FALLTHRU */
        case MDB_SET:
        case MDB_SET_KEY:
        case MDB_SET_RANGE:
-               if (key == NULL || key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
+               if (key == NULL) {
                        rc = EINVAL;
+               } else if (key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
+                       rc = MDB_BAD_VALSIZE;
                } else if (op == MDB_SET_RANGE)
                        rc = mdb_cursor_set(mc, key, data, op, NULL);
                else
                        rc = mdb_cursor_set(mc, key, data, op, &exact);
                break;
        case MDB_GET_MULTIPLE:
-               if (data == NULL ||
-                       !(mc->mc_db->md_flags & MDB_DUPFIXED) ||
-                       !(mc->mc_flags & C_INITIALIZED)) {
+               if (data == NULL || !(mc->mc_flags & C_INITIALIZED)) {
                        rc = EINVAL;
                        break;
                }
+               if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
+                       rc = MDB_INCOMPATIBLE;
+                       break;
+               }
                rc = MDB_SUCCESS;
                if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) ||
                        (mc->mc_xcursor->mx_cursor.mc_flags & C_EOF))
                        break;
                goto fetchm;
        case MDB_NEXT_MULTIPLE:
-               if (data == NULL ||
-                       !(mc->mc_db->md_flags & MDB_DUPFIXED)) {
+               if (data == NULL) {
                        rc = EINVAL;
                        break;
                }
+               if (!(mc->mc_db->md_flags & MDB_DUPFIXED)) {
+                       rc = MDB_INCOMPATIBLE;
+                       break;
+               }
                if (!(mc->mc_flags & C_INITIALIZED))
                        rc = mdb_cursor_first(mc, key, data);
                else
@@ -5388,30 +5478,30 @@ fetchm:
                rc = mdb_cursor_first(mc, key, data);
                break;
        case MDB_FIRST_DUP:
-               if (data == NULL ||
-                       !(mc->mc_db->md_flags & MDB_DUPSORT) ||
-                       !(mc->mc_flags & C_INITIALIZED) ||
-                       !(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
+               mfunc = mdb_cursor_first;
+       mmove:
+               if (data == NULL || !(mc->mc_flags & C_INITIALIZED)) {
                        rc = EINVAL;
                        break;
                }
-               rc = mdb_cursor_first(&mc->mc_xcursor->mx_cursor, data, NULL);
+               if (mc->mc_xcursor == NULL) {
+                       rc = MDB_INCOMPATIBLE;
+                       break;
+               }
+               if (!(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
+                       rc = EINVAL;
+                       break;
+               }
+               rc = mfunc(&mc->mc_xcursor->mx_cursor, data, NULL);
                break;
        case MDB_LAST:
                rc = mdb_cursor_last(mc, key, data);
                break;
        case MDB_LAST_DUP:
-               if (data == NULL ||
-                       !(mc->mc_db->md_flags & MDB_DUPSORT) ||
-                       !(mc->mc_flags & C_INITIALIZED) ||
-                       !(mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
-                       rc = EINVAL;
-                       break;
-               }
-               rc = mdb_cursor_last(&mc->mc_xcursor->mx_cursor, data, NULL);
-               break;
+               mfunc = mdb_cursor_last;
+               goto mmove;
        default:
-               DPRINTF("unhandled/unimplemented cursor operation %u", op);
+               DPRINTF(("unhandled/unimplemented cursor operation %u", op));
                rc = EINVAL;
                break;
        }
@@ -5474,28 +5564,28 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                dcount = data[1].mv_size;
                data[1].mv_size = 0;
                if (!F_ISSET(mc->mc_db->md_flags, MDB_DUPFIXED))
-                       return EINVAL;
+                       return MDB_INCOMPATIBLE;
        }
 
        nospill = flags & MDB_NOSPILL;
        flags &= ~MDB_NOSPILL;
 
-       if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_RDONLY))
-               return EACCES;
+       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 == 0 || key->mv_size > MDB_MAXKEYSIZE))
-               return EINVAL;
+               return MDB_BAD_VALSIZE;
 
        if (F_ISSET(mc->mc_db->md_flags, MDB_DUPSORT) && data->mv_size > MDB_MAXKEYSIZE)
-               return EINVAL;
+               return MDB_BAD_VALSIZE;
 
 #if SIZE_MAX > MAXDATASIZE
        if (data->mv_size > MAXDATASIZE)
-               return EINVAL;
+               return MDB_BAD_VALSIZE;
 #endif
 
-       DPRINTF("==> put db %u key [%s], size %"Z"u, data size %"Z"u",
-               mc->mc_dbi, DKEY(key), key ? key->mv_size:0, data->mv_size);
+       DPRINTF(("==> put db %u key [%s], size %"Z"u, data size %"Z"u",
+               mc->mc_dbi, DKEY(key), key ? key->mv_size:0, data->mv_size));
 
        dkey.mv_size = 0;
 
@@ -5528,7 +5618,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                        rc = mdb_cursor_set(mc, key, &d2, MDB_SET, &exact);
                }
                if ((flags & MDB_NOOVERWRITE) && rc == 0) {
-                       DPRINTF("duplicate key [%s]", DKEY(key));
+                       DPRINTF(("duplicate key [%s]", DKEY(key)));
                        *data = d2;
                        return MDB_KEYEXIST;
                }
@@ -5576,7 +5666,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
                        unsigned int ksize = mc->mc_db->md_pad;
                        if (key->mv_size != ksize)
-                               return EINVAL;
+                               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);
@@ -5605,9 +5695,16 @@ more:
                                        mc->mc_dbx->md_dcmp = mdb_cmp_cint;
 #endif
 #endif
-                               /* if data matches, ignore it */
-                               if (!mc->mc_dbx->md_dcmp(data, &dkey))
-                                       return (flags == MDB_NODUPDATA) ? MDB_KEYEXIST : MDB_SUCCESS;
+                               /* if data matches, skip it */
+                               if (!mc->mc_dbx->md_dcmp(data, &dkey)) {
+                                       if (flags & MDB_NODUPDATA)
+                                               rc = MDB_KEYEXIST;
+                                       else if (flags & MDB_MULTIPLE)
+                                               goto next_mult;
+                                       else
+                                               rc = MDB_SUCCESS;
+                                       return rc;
+                               }
 
                                /* create a fake page for the dup items */
                                memcpy(dbuf, dkey.mv_data, dkey.mv_size);
@@ -5789,7 +5886,7 @@ current:
                mdb_node_del(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], 0);
                mc->mc_db->md_entries--;
        } else {
-               DPRINTF("inserting key at index %i", mc->mc_ki[mc->mc_top]);
+               DPRINTF(("inserting key at index %i", mc->mc_ki[mc->mc_top]));
                insert = 1;
        }
 
@@ -5888,15 +5985,16 @@ put_sub:
                        mc->mc_db->md_entries++;
                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;
                                        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
                                        goto more;
                                }
                        }
-                       /* let caller know how many succeeded, if any */
-                       data[1].mv_size = mcount;
                }
        }
 done:
@@ -5914,8 +6012,8 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
        MDB_node        *leaf;
        int rc;
 
-       if (F_ISSET(mc->mc_txn->mt_flags, MDB_TXN_RDONLY))
-               return EACCES;
+       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 (!(mc->mc_flags & C_INITIALIZED))
                return EINVAL;
@@ -5931,7 +6029,7 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
 
        if (!IS_LEAF2(mc->mc_pg[mc->mc_top]) && F_ISSET(leaf->mn_flags, F_DUPDATA)) {
-               if (flags != MDB_NODUPDATA) {
+               if (!(flags & MDB_NODUPDATA)) {
                        if (!F_ISSET(leaf->mn_flags, F_SUBDATA)) {
                                mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
                        }
@@ -5991,8 +6089,8 @@ mdb_page_new(MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp)
 
        if ((rc = mdb_page_alloc(mc, num, &np)))
                return rc;
-       DPRINTF("allocated new mpage %"Z"u, page size %u",
-           np->mp_pgno, mc->mc_txn->mt_env->me_psize);
+       DPRINTF(("allocated new mpage %"Z"u, page size %u",
+           np->mp_pgno, mc->mc_txn->mt_env->me_psize));
        np->mp_flags = flags | P_DIRTY;
        np->mp_lower = PAGEHDRSZ;
        np->mp_upper = mc->mc_txn->mt_env->me_psize;
@@ -6090,11 +6188,11 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
 
        assert(mp->mp_upper >= mp->mp_lower);
 
-       DPRINTF("add to %s %spage %"Z"u index %i, data size %"Z"u key size %"Z"u [%s]",
+       DPRINTF(("add to %s %spage %"Z"u index %i, data size %"Z"u key size %"Z"u [%s]",
            IS_LEAF(mp) ? "leaf" : "branch",
                IS_SUBP(mp) ? "sub-" : "",
            mp->mp_pgno, indx, data ? data->mv_size : 0,
-               key ? key->mv_size : 0, key ? DKEY(key) : NULL);
+               key ? key->mv_size : 0, key ? DKEY(key) : NULL));
 
        if (IS_LEAF2(mp)) {
                /* Move higher keys up one slot. */
@@ -6124,12 +6222,12 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
                        int ovpages = OVPAGES(data->mv_size, mc->mc_txn->mt_env->me_psize);
                        int rc;
                        /* Put data on overflow page. */
-                       DPRINTF("data size is %"Z"u, node would be %"Z"u, put data on overflow page",
-                           data->mv_size, node_size+data->mv_size);
+                       DPRINTF(("data size is %"Z"u, node would be %"Z"u, put data on overflow page",
+                           data->mv_size, node_size+data->mv_size));
                        node_size += sizeof(pgno_t);
                        if ((rc = mdb_page_new(mc, P_OVERFLOW, ovpages, &ofp)))
                                return rc;
-                       DPRINTF("allocated overflow page %"Z"u", ofp->mp_pgno);
+                       DPRINTF(("allocated overflow page %"Z"u", ofp->mp_pgno));
                        flags |= F_BIGDATA;
                } else {
                        node_size += data->mv_size;
@@ -6138,11 +6236,11 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
        node_size += node_size & 1;
 
        if (node_size + sizeof(indx_t) > SIZELEFT(mp)) {
-               DPRINTF("not enough room in page %"Z"u, got %u ptrs",
-                   mp->mp_pgno, NUMKEYS(mp));
-               DPRINTF("upper - lower = %u - %u = %u", mp->mp_upper, mp->mp_lower,
-                   mp->mp_upper - mp->mp_lower);
-               DPRINTF("node size = %"Z"u", node_size);
+               DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
+                   mp->mp_pgno, NUMKEYS(mp)));
+               DPRINTF(("upper - lower = %u - %u = %u", mp->mp_upper, mp->mp_lower,
+                   mp->mp_upper - mp->mp_lower));
+               DPRINTF(("node size = %"Z"u", node_size));
                return MDB_PAGE_FULL;
        }
 
@@ -6211,8 +6309,8 @@ mdb_node_del(MDB_page *mp, indx_t indx, int ksize)
        {
        pgno_t pgno;
        COPY_PGNO(pgno, mp->mp_pgno);
-       DPRINTF("delete node %u on %s page %"Z"u", indx,
-           IS_LEAF(mp) ? "leaf" : "branch", pgno);
+       DPRINTF(("delete node %u on %s page %"Z"u", indx,
+           IS_LEAF(mp) ? "leaf" : "branch", pgno));
        }
 #endif
        assert(indx < NUMKEYS(mp));
@@ -6373,8 +6471,8 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
                                mx->mx_db.md_flags |= MDB_INTEGERKEY;
                }
        }
-       DPRINTF("Sub-db %u for db %u root page %"Z"u", mx->mx_cursor.mc_dbi, mc->mc_dbi,
-               mx->mx_db.md_root);
+       DPRINTF(("Sub-db %u for db %u root page %"Z"u", mx->mx_cursor.mc_dbi, mc->mc_dbi,
+               mx->mx_db.md_root));
        mx->mx_dbflag = DB_VALID | (F_ISSET(mc->mc_pg[mc->mc_top]->mp_flags, P_DIRTY) ?
                DB_DIRTY : 0);
        mx->mx_dbx.md_name.mv_data = NODEKEY(node);
@@ -6425,6 +6523,9 @@ mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
        if (txn == NULL || ret == NULL || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
+       if (txn->mt_flags & MDB_TXN_ERROR)
+               return MDB_BAD_TXN;
+
        /* Allow read access to the freelist */
        if (!dbi && !F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
                return EINVAL;
@@ -6470,8 +6571,8 @@ mdb_cursor_count(MDB_cursor *mc, size_t *countp)
        if (mc == NULL || countp == NULL)
                return EINVAL;
 
-       if (!(mc->mc_db->md_flags & MDB_DUPSORT))
-               return EINVAL;
+       if (mc->mc_xcursor == NULL)
+               return MDB_INCOMPATIBLE;
 
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
        if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@@ -6540,11 +6641,11 @@ mdb_update_key(MDB_cursor *mc, MDB_val *key)
                char kbuf2[(MDB_MAXKEYSIZE*2+1)];
                k2.mv_data = NODEKEY(node);
                k2.mv_size = node->mn_ksize;
-               DPRINTF("update key %u (ofs %u) [%s] to [%s] on page %"Z"u",
+               DPRINTF(("update key %u (ofs %u) [%s] to [%s] on page %"Z"u",
                        indx, ptr,
                        mdb_dkey(&k2, kbuf2),
                        DKEY(key),
-                       mp->mp_pgno);
+                       mp->mp_pgno));
        }
 #endif
 
@@ -6558,7 +6659,7 @@ mdb_update_key(MDB_cursor *mc, MDB_val *key)
                if (delta > 0 && SIZELEFT(mp) < delta) {
                        pgno_t pgno;
                        /* not enough space left, do a delete and split */
-                       DPRINTF("Not enough room, delta = %d, splitting...", delta);
+                       DPRINTF(("Not enough room, delta = %d, splitting...", delta));
                        pgno = NODEPGNO(node);
                        mdb_node_del(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top], 0);
                        return mdb_page_split(mc, key, NULL, pgno, MDB_SPLIT_REPLACE);
@@ -6668,12 +6769,12 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                        return rc;
        }
 
-       DPRINTF("moving %s node %u [%s] on page %"Z"u to node %u on page %"Z"u",
+       DPRINTF(("moving %s node %u [%s] on page %"Z"u to node %u on page %"Z"u",
            IS_LEAF(csrc->mc_pg[csrc->mc_top]) ? "leaf" : "branch",
            csrc->mc_ki[csrc->mc_top],
                DKEY(&key),
            csrc->mc_pg[csrc->mc_top]->mp_pgno,
-           cdst->mc_ki[cdst->mc_top], cdst->mc_pg[cdst->mc_top]->mp_pgno);
+           cdst->mc_ki[cdst->mc_top], cdst->mc_pg[cdst->mc_top]->mp_pgno));
 
        /* Add the node to the destination page.
         */
@@ -6719,8 +6820,8 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                                key.mv_size = NODEKSZ(srcnode);
                                key.mv_data = NODEKEY(srcnode);
                        }
-                       DPRINTF("update separator for source page %"Z"u to [%s]",
-                               csrc->mc_pg[csrc->mc_top]->mp_pgno, DKEY(&key));
+                       DPRINTF(("update separator for source page %"Z"u to [%s]",
+                               csrc->mc_pg[csrc->mc_top]->mp_pgno, DKEY(&key)));
                        mdb_cursor_copy(csrc, &mn);
                        mn.mc_snum--;
                        mn.mc_top--;
@@ -6747,8 +6848,8 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                                key.mv_size = NODEKSZ(srcnode);
                                key.mv_data = NODEKEY(srcnode);
                        }
-                       DPRINTF("update separator for destination page %"Z"u to [%s]",
-                               cdst->mc_pg[cdst->mc_top]->mp_pgno, DKEY(&key));
+                       DPRINTF(("update separator for destination page %"Z"u to [%s]",
+                               cdst->mc_pg[cdst->mc_top]->mp_pgno, DKEY(&key)));
                        mdb_cursor_copy(cdst, &mn);
                        mn.mc_snum--;
                        mn.mc_top--;
@@ -6785,8 +6886,8 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
        MDB_val          key, data;
        unsigned        nkeys;
 
-       DPRINTF("merging page %"Z"u into %"Z"u", csrc->mc_pg[csrc->mc_top]->mp_pgno,
-               cdst->mc_pg[cdst->mc_top]->mp_pgno);
+       DPRINTF(("merging page %"Z"u into %"Z"u", csrc->mc_pg[csrc->mc_top]->mp_pgno,
+               cdst->mc_pg[cdst->mc_top]->mp_pgno));
 
        assert(csrc->mc_snum > 1);      /* can't merge root page */
        assert(cdst->mc_snum > 1);
@@ -6838,8 +6939,9 @@ 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);
+       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));
 
        /* Unlink the src page from parent and add to free list.
         */
@@ -6929,9 +7031,10 @@ mdb_rebalance(MDB_cursor *mc)
        {
        pgno_t pgno;
        COPY_PGNO(pgno, mc->mc_pg[mc->mc_top]->mp_pgno);
-       DPRINTF("rebalancing %s page %"Z"u (has %u keys, %.1f%% full)",
+       DPRINTF(("rebalancing %s page %"Z"u (has %u keys, %.1f%% full)",
            IS_LEAF(mc->mc_pg[mc->mc_top]) ? "leaf" : "branch",
-           pgno, NUMKEYS(mc->mc_pg[mc->mc_top]), (float)PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) / 10);
+           pgno, NUMKEYS(mc->mc_pg[mc->mc_top]),
+               (float)PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) / 10));
        }
 #endif
 
@@ -6940,8 +7043,8 @@ mdb_rebalance(MDB_cursor *mc)
 #if MDB_DEBUG
                pgno_t pgno;
                COPY_PGNO(pgno, mc->mc_pg[mc->mc_top]->mp_pgno);
-               DPRINTF("no need to rebalance page %"Z"u, above fill threshold",
-                   pgno);
+               DPRINTF(("no need to rebalance page %"Z"u, above fill threshold",
+                   pgno));
 #endif
                return MDB_SUCCESS;
        }
@@ -7061,8 +7164,9 @@ mdb_rebalance(MDB_cursor *mc)
                mc->mc_ki[mc->mc_top] = 0;
        }
 
-       DPRINTF("found neighbor page %"Z"u (%u keys, %.1f%% full)",
-           mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]), (float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10);
+       DPRINTF(("found neighbor page %"Z"u (%u keys, %.1f%% full)",
+           mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]),
+               (float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10));
 
        /* If the neighbor page is above threshold and has enough keys,
         * move one key from it. Otherwise we should try to merge them.
@@ -7149,22 +7253,25 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
 
        assert(key != NULL);
 
-       DPRINTF("====> delete db %u key [%s]", dbi, DKEY(key));
+       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 (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
-               return EACCES;
-       }
+       if (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR))
+               return (txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
 
        if (key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
-               return EINVAL;
+               return MDB_BAD_VALSIZE;
        }
 
        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;
@@ -7222,14 +7329,14 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
        mp = mc->mc_pg[mc->mc_top];
        newindx = mc->mc_ki[mc->mc_top];
 
-       DPRINTF("-----> splitting %s page %"Z"u and adding [%s] at index %i",
+       DPRINTF(("-----> splitting %s page %"Z"u and adding [%s] at index %i",
            IS_LEAF(mp) ? "leaf" : "branch", mp->mp_pgno,
-           DKEY(newkey), mc->mc_ki[mc->mc_top]);
+           DKEY(newkey), mc->mc_ki[mc->mc_top]));
 
        /* Create a right sibling. */
        if ((rc = mdb_page_new(mc, mp->mp_flags, 1, &rp)))
                return rc;
-       DPRINTF("new right sibling: page %"Z"u", rp->mp_pgno);
+       DPRINTF(("new right sibling: page %"Z"u", rp->mp_pgno));
 
        if (mc->mc_snum < 2) {
                if ((rc = mdb_page_new(mc, P_BRANCH, 1, &pp)))
@@ -7240,7 +7347,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                mc->mc_pg[0] = pp;
                mc->mc_ki[0] = 0;
                mc->mc_db->md_root = pp->mp_pgno;
-               DPRINTF("root split! new root = %"Z"u", pp->mp_pgno);
+               DPRINTF(("root split! new root = %"Z"u", pp->mp_pgno));
                mc->mc_db->md_depth++;
                new_root = 1;
 
@@ -7258,7 +7365,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                ptop = 0;
        } else {
                ptop = mc->mc_top-1;
-               DPRINTF("parent branch page is %"Z"u", mc->mc_pg[ptop]->mp_pgno);
+               DPRINTF(("parent branch page is %"Z"u", mc->mc_pg[ptop]->mp_pgno));
        }
 
        mc->mc_flags |= C_SPLITTING;
@@ -7401,7 +7508,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
        }
 
 newsep:
-       DPRINTF("separator is [%s]", DKEY(&sepkey));
+       DPRINTF(("separator is [%s]", DKEY(&sepkey)));
 
        /* Copy separator key to the parent.
         */
@@ -7588,7 +7695,7 @@ done:
                                m3->mc_snum++;
                                m3->mc_top++;
                        }
-                       if (m3->mc_pg[mc->mc_top] == mp) {
+                       if (m3->mc_top >= mc->mc_top && m3->mc_pg[mc->mc_top] == mp) {
                                if (m3->mc_ki[mc->mc_top] >= newindx && !(nflags & MDB_SPLIT_REPLACE))
                                        m3->mc_ki[mc->mc_top]++;
                                if (m3->mc_ki[mc->mc_top] >= fixup) {
@@ -7596,7 +7703,7 @@ done:
                                        m3->mc_ki[mc->mc_top] -= fixup;
                                        m3->mc_ki[ptop] = mn.mc_ki[ptop];
                                }
-                       } else if (!did_split && m3->mc_pg[ptop] == mc->mc_pg[ptop] &&
+                       } else if (!did_split && m3->mc_top >= ptop && m3->mc_pg[ptop] == mc->mc_pg[ptop] &&
                                m3->mc_ki[ptop] >= mc->mc_ki[ptop]) {
                                m3->mc_ki[ptop]++;
                        }
@@ -7618,12 +7725,11 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
        if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
-       if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
-               return EACCES;
-       }
+       if (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR))
+               return (txn->mt_flags & MDB_TXN_RDONLY) ? EACCES : MDB_BAD_TXN;
 
        if (key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
-               return EINVAL;
+               return MDB_BAD_VALSIZE;
        }
 
        if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA|MDB_RESERVE|MDB_APPEND|MDB_APPENDDUP)) != flags)
@@ -7757,6 +7863,8 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
 
        if ((flags & VALID_FLAGS) != flags)
                return EINVAL;
+       if (txn->mt_flags & MDB_TXN_ERROR)
+               return MDB_BAD_TXN;
 
        /* main DB? */
        if (!name) {
@@ -7811,7 +7919,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
                /* make sure this is actually a DB */
                MDB_node *node = NODEPTR(mc.mc_pg[mc.mc_top], mc.mc_ki[mc.mc_top]);
                if (!(node->mn_flags & F_SUBDATA))
-                       return EINVAL;
+                       return MDB_INCOMPATIBLE;
        } else if (rc == MDB_NOTFOUND && (flags & MDB_CREATE)) {
                /* Create if requested */
                MDB_db dummy;
@@ -7872,7 +7980,7 @@ void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
 int mdb_dbi_flags(MDB_env *env, MDB_dbi dbi, unsigned int *flags)
 {
        /* We could return the flags for the FREE_DBI too but what's the point? */
-       if (dbi <= MAIN_DBI || dbi >= env->me_numdbs)
+       if (dbi < MAIN_DBI || dbi >= env->me_numdbs)
                return EINVAL;
        *flags = env->me_dbflags[dbi];
        return MDB_SUCCESS;
@@ -8042,6 +8150,11 @@ int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
        return MDB_SUCCESS;
 }
 
+int mdb_env_get_maxkeysize(MDB_env *env)
+{
+       return MDB_MAXKEYSIZE;
+}
+
 int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
 {
        unsigned int i, rdrs;
@@ -8144,9 +8257,10 @@ int mdb_reader_check(MDB_env *env, int *dead)
                if (mr[i].mr_pid && mr[i].mr_pid != env->me_pid) {
                        pid = mr[i].mr_pid;
                        if (mdb_pid_insert(pids, pid) == 0) {
-                               if (mdb_reader_pid(env, Pidcheck, pid)) {
+                               if (!mdb_reader_pid(env, Pidcheck, pid)) {
                                        LOCK_MUTEX_R(env);
-                                       if (mdb_reader_pid(env, Pidcheck, pid)) {
+                                       /* Recheck, a new process may have reused pid */
+                                       if (!mdb_reader_pid(env, Pidcheck, pid)) {
                                                for (j=i; j<rdrs; j++)
                                                        if (mr[j].mr_pid == pid) {
                                                                mr[j].mr_pid = 0;