]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
Drop spurious Errcode() call
[openldap] / libraries / liblmdb / mdb.c
index 1edc282b814ca220ca3b4e15a458b1e8320edcce..9c85230a6bcc67b2191ec732b285bd8024310c12 100644 (file)
@@ -35,6 +35,9 @@
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE 1
 #endif
+#if defined(__WIN64__)
+#define _FILE_OFFSET_BITS      64
+#endif
 #ifdef _WIN32
 #include <malloc.h>
 #include <windows.h>
@@ -244,10 +247,9 @@ typedef SSIZE_T    ssize_t;
 /** Some platforms define the EOWNERDEAD error code
  * even though they don't support Robust Mutexes.
  * Compile with -DMDB_USE_ROBUST=0, or use some other
- * mechanism like -DMDB_USE_SYSV_SEM instead of
- * -DMDB_USE_POSIX_MUTEX. (SysV semaphores are
- * also Robust, but some systems don't support them
- * either.)
+ * mechanism like -DMDB_USE_POSIX_SEM instead of
+ * -DMDB_USE_POSIX_MUTEX.
+ * (Posix semaphores are not robust.)
  */
 #ifndef MDB_USE_ROBUST
 /* Android currently lacks Robust Mutex support. So does glibc < 2.4. */
@@ -256,6 +258,10 @@ typedef SSIZE_T    ssize_t;
 #  define MDB_USE_ROBUST       0
 # else
 #  define MDB_USE_ROBUST       1
+# endif
+#endif /* !MDB_USE_ROBUST */
+
+#if defined(MDB_USE_POSIX_MUTEX) && (MDB_USE_ROBUST)
 /* glibc < 2.12 only provided _np API */
 #  if (defined(__GLIBC__) && GLIBC_VER < 0x02000c) || \
        (defined(PTHREAD_MUTEX_ROBUST_NP) && !defined(PTHREAD_MUTEX_ROBUST))
@@ -263,10 +269,9 @@ typedef SSIZE_T    ssize_t;
 #   define pthread_mutexattr_setrobust(attr, flag)     pthread_mutexattr_setrobust_np(attr, flag)
 #   define pthread_mutex_consistent(mutex)     pthread_mutex_consistent_np(mutex)
 #  endif
-# endif
-#endif /* MDB_USE_ROBUST */
+#endif /* MDB_USE_POSIX_MUTEX && MDB_USE_ROBUST */
 
-#if defined(MDB_OWNERDEAD) && MDB_USE_ROBUST
+#if defined(MDB_OWNERDEAD) && (MDB_USE_ROBUST)
 #define MDB_ROBUST_SUPPORTED   1
 #endif
 
@@ -289,8 +294,10 @@ typedef HANDLE mdb_mutex_t, mdb_mutexref_t;
 #define pthread_mutex_lock(x)  WaitForSingleObject(*x, INFINITE)
 #define pthread_cond_signal(x) SetEvent(*x)
 #define pthread_cond_wait(cond,mutex)  do{SignalObjectAndWait(*mutex, *cond, INFINITE, FALSE); WaitForSingleObject(*mutex, INFINITE);}while(0)
-#define THREAD_CREATE(thr,start,arg)   thr=CreateThread(NULL,0,start,arg,0,NULL)
-#define THREAD_FINISH(thr)     WaitForSingleObject(thr, INFINITE)
+#define THREAD_CREATE(thr,start,arg) \
+       (((thr) = CreateThread(NULL, 0, start, arg, 0, NULL)) ? 0 : ErrCode())
+#define THREAD_FINISH(thr) \
+       (WaitForSingleObject(thr, INFINITE) ? ErrCode() : 0)
 #define LOCK_MUTEX0(mutex)             WaitForSingleObject(mutex, INFINITE)
 #define UNLOCK_MUTEX(mutex)            ReleaseMutex(mutex)
 #define mdb_mutex_consistent(mutex)    0
@@ -764,9 +771,23 @@ typedef struct MDB_txninfo {
          + (((MDB_PIDLOCK) != 0) << 16)))
 /** @} */
 
-/** Common header for all page types.
- * Overflow records occupy a number of contiguous pages with no
- * headers on any page after the first.
+/** Common header for all page types. The page type depends on #mp_flags.
+ *
+ * #P_BRANCH and #P_LEAF pages have unsorted '#MDB_node's at the end, with
+ * sorted #mp_ptrs[] entries referring to them. Exception: #P_LEAF2 pages
+ * omit mp_ptrs and pack sorted #MDB_DUPFIXED values after the page header.
+ *
+ * #P_OVERFLOW records occupy one or more contiguous pages where only the
+ * first has a page header. They hold the real data of #F_BIGDATA nodes.
+ *
+ * #P_SUBP sub-pages are small leaf "pages" with duplicate data.
+ * A node with flag #F_DUPDATA but not #F_SUBDATA contains a sub-page.
+ * (Duplicate data can also go in sub-databases, which use normal pages.)
+ *
+ * #P_META pages contain #MDB_meta, the start point of an LMDB snapshot.
+ *
+ * Each non-metapage up to #MDB_meta.%mm_last_pg is reachable exactly once
+ * in the snapshot: Either used by a database or listed in a freeDB record.
  */
 typedef struct MDB_page {
 #define        mp_pgno mp_p.p_pgno
@@ -775,7 +796,7 @@ typedef struct MDB_page {
                pgno_t          p_pgno; /**< page number */
                struct MDB_page *p_next; /**< for in-memory list of freed pages */
        } mp_p;
-       uint16_t        mp_pad;
+       uint16_t        mp_pad;                 /**< key size if this is a LEAF2 page */
 /**    @defgroup mdb_page      Page Flags
  *     @ingroup internal
  *     Flags for the page headers.
@@ -842,7 +863,9 @@ typedef struct MDB_page {
        /** The number of overflow pages needed to store the given size. */
 #define OVPAGES(size, psize)   ((PAGEHDRSZ-1 + (size)) / (psize) + 1)
 
-       /** Link in #MDB_txn.%mt_loose_pgs list */
+       /** Link in #MDB_txn.%mt_loose_pgs list.
+        *  Kept outside the page header, which is needed when reusing the page.
+        */
 #define NEXT_LOOSE_PAGE(p)             (*(MDB_page **)((p) + 2))
 
        /** Header for a single key/data pair within a page.
@@ -970,9 +993,9 @@ typedef struct MDB_db {
        pgno_t          md_root;                /**< the root page of this tree */
 } MDB_db;
 
-       /** mdb_dbi_open flags */
 #define MDB_VALID      0x8000          /**< DB handle is valid, for me_dbflags */
 #define PERSISTENT_FLAGS       (0xffff & ~(MDB_VALID))
+       /** #mdb_dbi_open() flags */
 #define VALID_FLAGS    (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
        MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
 
@@ -1003,7 +1026,10 @@ typedef struct MDB_meta {
 #define        mm_psize        mm_dbs[FREE_DBI].md_pad
        /** Any persistent environment flags. @ref mdb_env */
 #define        mm_flags        mm_dbs[FREE_DBI].md_flags
-       pgno_t          mm_last_pg;                     /**< last used page in file */
+       /** Last used page in the datafile.
+        *      Actually the file may be shorter if the freeDB lists the final pages.
+        */
+       pgno_t          mm_last_pg;
        volatile txnid_t        mm_txnid;       /**< txnid that committed this page */
 } MDB_meta;
 
@@ -1053,7 +1079,7 @@ struct MDB_txn {
         *      in this transaction, linked through #NEXT_LOOSE_PAGE(page).
         */
        MDB_page        *mt_loose_pgs;
-       /* #Number of loose pages (#mt_loose_pgs) */
+       /*Number of loose pages (#mt_loose_pgs) */
        int                     mt_loose_count;
        /** The sorted list of dirty pages we temporarily wrote to disk
         *      because the dirty list was full. page numbers in here are
@@ -1076,11 +1102,12 @@ struct MDB_txn {
  *     @ingroup internal
  * @{
  */
-#define DB_DIRTY       0x01            /**< DB was modified or is DUPSORT data */
+#define DB_DIRTY       0x01            /**< DB was written in this txn */
 #define DB_STALE       0x02            /**< Named-DB record is older than txnID */
 #define DB_NEW         0x04            /**< Named-DB handle opened in this txn */
 #define DB_VALID       0x08            /**< DB handle is valid, see also #MDB_VALID */
 #define DB_USRVALID    0x10            /**< As #DB_VALID, but not set for #FREE_DBI */
+#define DB_DUPDATA     0x20            /**< DB is #MDB_DUPSORT data */
 /** @} */
        /** In write txns, array of cursors for each DB */
        MDB_cursor      **mt_cursors;
@@ -1185,6 +1212,21 @@ typedef struct MDB_xcursor {
        unsigned char mx_dbflag;
 } MDB_xcursor;
 
+       /** Check if there is an inited xcursor, so #XCURSOR_REFRESH() is proper */
+#define XCURSOR_INITED(mc) \
+       ((mc)->mc_xcursor && ((mc)->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
+
+       /** Update sub-page pointer, if any, in \b mc->mc_xcursor.  Needed
+        *      when the node which contains the sub-page may have moved.  Called
+        *      with \b mp = mc->mc_pg[mc->mc_top], \b ki = mc->mc_ki[mc->mc_top].
+        */
+#define XCURSOR_REFRESH(mc, mp, ki) do { \
+       MDB_page *xr_pg = (mp); \
+       MDB_node *xr_node = NODEPTR(xr_pg, ki); \
+       if ((xr_node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) \
+               (mc)->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(xr_node); \
+} while (0)
+
        /** State of FreeDB old pages, stored in the MDB_env */
 typedef struct MDB_pgstate {
        pgno_t          *mf_pghead;     /**< Reclaimed freeDB pages, or NULL before use */
@@ -1299,7 +1341,7 @@ enum {
 #define MDB_END_SLOT MDB_NOTLS /**< release any reader slot if #MDB_NOTLS */
 static void mdb_txn_end(MDB_txn *txn, unsigned mode);
 
-static int  mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **mp, int *lvl);
+static int  mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **mp, int *lvl);
 static int  mdb_page_search_root(MDB_cursor *mc,
                            MDB_val *key, int modify);
 #define MDB_PS_MODIFY  1
@@ -1328,7 +1370,7 @@ static int  mdb_node_add(MDB_cursor *mc, indx_t indx,
 static void mdb_node_del(MDB_cursor *mc, int ksize);
 static void mdb_node_shrink(MDB_page *mp, indx_t indx);
 static int     mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft);
-static int  mdb_node_read(MDB_txn *txn, MDB_node *leaf, MDB_val *data);
+static int  mdb_node_read(MDB_cursor *mc, MDB_node *leaf, MDB_val *data);
 static size_t  mdb_leaf_size(MDB_env *env, MDB_val *key, MDB_val *data);
 static size_t  mdb_branch_size(MDB_env *env, MDB_val *key);
 
@@ -1419,7 +1461,7 @@ mdb_strerror(int err)
         *      and the actual use of the message uses more than 4K of stack.
         */
 #define MSGSIZE        1024
-#define PADSIZE 4096
+#define PADSIZE        4096
        char buf[MSGSIZE+PADSIZE], *ptr = buf;
 #endif
        int i;
@@ -1560,7 +1602,7 @@ mdb_page_list(MDB_page *mp)
                        pgno, ((MDB_meta *)METADATA(mp))->mm_txnid);
                return;
        default:
-               fprintf(stderr, "Bad page %"Z"u flags 0x%u\n", pgno, mp->mp_flags);
+               fprintf(stderr, "Bad page %"Z"u flags 0x%X\n", pgno, mp->mp_flags);
                return;
        }
 
@@ -1615,7 +1657,7 @@ mdb_cursor_chk(MDB_cursor *mc)
        }
        if (mc->mc_ki[i] >= NUMKEYS(mc->mc_pg[i]))
                printf("ack!\n");
-       if (mc->mc_xcursor && (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
+       if (XCURSOR_INITED(mc)) {
                node = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
                if (((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) &&
                        mc->mc_xcursor->mx_cursor.mc_pg[0] != NODEDATA(node)) {
@@ -1676,7 +1718,7 @@ static void mdb_audit(MDB_txn *txn)
                }
        }
        if (freecount + count + NUM_METAS != txn->mt_next_pgno) {
-               fprintf(stderr, "audit: %lu freecount: %lu count: %lu total: %lu next_pgno: %lu\n",
+               fprintf(stderr, "audit: %"Z"u freecount: %"Z"u count: %"Z"u total: %"Z"u next_pgno: %"Z"u\n",
                        txn->mt_txnid, freecount, count+NUM_METAS,
                        freecount+count+NUM_METAS, txn->mt_next_pgno);
        }
@@ -1844,7 +1886,7 @@ mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
 {
        enum { Mask = P_SUBP|P_DIRTY|P_LOOSE|P_KEEP };
        MDB_txn *txn = mc->mc_txn;
-       MDB_cursor *m3;
+       MDB_cursor *m3, *m0 = mc;
        MDB_xcursor *mx;
        MDB_page *dp, *mp;
        MDB_node *leaf;
@@ -1887,7 +1929,7 @@ mdb_pages_xkeep(MDB_cursor *mc, unsigned pflags, int all)
                                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)
+                               if ((rc = mdb_page_get(m0, pgno, &dp, &level)) != MDB_SUCCESS)
                                        break;
                                if ((dp->mp_flags & Mask) == pflags && level <= 1)
                                        dp->mp_flags ^= P_KEEP;
@@ -2191,7 +2233,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                }
                np = m2.mc_pg[m2.mc_top];
                leaf = NODEPTR(np, m2.mc_ki[m2.mc_top]);
-               if ((rc = mdb_node_read(txn, leaf, &data)) != MDB_SUCCESS)
+               if ((rc = mdb_node_read(&m2, leaf, &data)) != MDB_SUCCESS)
                        return rc;
 
                idl = (MDB_ID *) data.mv_data;
@@ -2430,14 +2472,8 @@ done:
                        if (m2 == mc) continue;
                        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_xcursor->mx_cursor.mc_flags & C_INITIALIZED))
-                               {
-                                       MDB_node *leaf = NODEPTR(np, m2->mc_ki[mc->mc_top]);
-                                       if ((leaf->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
-                                               m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
-                               }
+                               if (XCURSOR_INITED(m2) && IS_LEAF(np))
+                                       XCURSOR_REFRESH(m2, np, m2->mc_ki[mc->mc_top]);
                        }
                }
        }
@@ -4487,7 +4523,7 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
                if (rc == MDB_ERRCODE_ROFS && (env->me_flags & MDB_RDONLY)) {
                        return MDB_SUCCESS;
                }
-               goto fail_errno;
+               goto fail;
        }
 #if ! ((MDB_CLOEXEC) || defined(_WIN32))
        /* Lose record locks when exec*() */
@@ -5217,15 +5253,16 @@ mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 }
 
 /** Find the address of the page corresponding to a given page number.
- * @param[in] txn the transaction for this access.
+ * @param[in] mc the cursor accessing the page.
  * @param[in] pgno the page number for the page to retrieve.
  * @param[out] ret address of a pointer where the page's address will be stored.
  * @param[out] lvl dirty_list inheritance level of found page. 1=current txn, 0=mapped page.
  * @return 0 on success, non-zero on failure.
  */
 static int
-mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret, int *lvl)
+mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **ret, int *lvl)
 {
+       MDB_txn *txn = mc->mc_txn;
        MDB_env *env = txn->mt_env;
        MDB_page *p = NULL;
        int level;
@@ -5320,7 +5357,7 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
                mdb_cassert(mc, i < NUMKEYS(mp));
                node = NODEPTR(mp, i);
 
-               if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mp, NULL)) != 0)
+               if ((rc = mdb_page_get(mc, NODEPGNO(node), &mp, NULL)) != 0)
                        return rc;
 
                mc->mc_ki[mc->mc_top] = i;
@@ -5362,7 +5399,7 @@ mdb_page_search_lowest(MDB_cursor *mc)
        MDB_node        *node = NODEPTR(mp, 0);
        int rc;
 
-       if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mp, NULL)) != 0)
+       if ((rc = mdb_page_get(mc, NODEPGNO(node), &mp, NULL)) != 0)
                return rc;
 
        mc->mc_ki[mc->mc_top] = 0;
@@ -5414,7 +5451,7 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
                                                return MDB_NOTFOUND;
                                        if ((leaf->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
                                                return MDB_INCOMPATIBLE; /* not a named DB */
-                                       rc = mdb_node_read(mc->mc_txn, leaf, &data);
+                                       rc = mdb_node_read(&mc2, leaf, &data);
                                        if (rc)
                                                return rc;
                                        memcpy(&flags, ((char *) data.mv_data + offsetof(MDB_db, md_flags)),
@@ -5438,7 +5475,7 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
 
        mdb_cassert(mc, root > 1);
        if (!mc->mc_pg[0] || mc->mc_pg[0]->mp_pgno != root)
-               if ((rc = mdb_page_get(mc->mc_txn, root, &mc->mc_pg[0], NULL)) != 0)
+               if ((rc = mdb_page_get(mc, root, &mc->mc_pg[0], NULL)) != 0)
                        return rc;
 
        mc->mc_snum = 1;
@@ -5535,13 +5572,13 @@ release:
 }
 
 /** Return the data associated with a given node.
- * @param[in] txn The transaction for this operation.
+ * @param[in] mc The cursor for this operation.
  * @param[in] leaf The node being read.
  * @param[out] data Updated to point to the node's data.
  * @return 0 on success, non-zero on failure.
  */
 static int
-mdb_node_read(MDB_txn *txn, MDB_node *leaf, MDB_val *data)
+mdb_node_read(MDB_cursor *mc, MDB_node *leaf, MDB_val *data)
 {
        MDB_page        *omp;           /* overflow page */
        pgno_t           pgno;
@@ -5557,7 +5594,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) {
+       if ((rc = mdb_page_get(mc, pgno, &omp, NULL)) != 0) {
                DPRINTF(("read overflow page %"Z"u failed", pgno));
                return rc;
        }
@@ -5631,7 +5668,7 @@ mdb_cursor_sibling(MDB_cursor *mc, int move_right)
        mdb_cassert(mc, IS_BRANCH(mc->mc_pg[mc->mc_top]));
 
        indx = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
-       if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(indx), &mp, NULL)) != 0) {
+       if ((rc = mdb_page_get(mc, NODEPGNO(indx), &mp, NULL)) != 0) {
                /* mc will be inconsistent if caller does mc_snum++ as above */
                mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
                return rc;
@@ -5714,7 +5751,7 @@ skip:
                mdb_xcursor_init1(mc, leaf);
        }
        if (data) {
-               if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
+               if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
                        return rc;
 
                if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@@ -5797,7 +5834,7 @@ mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                mdb_xcursor_init1(mc, leaf);
        }
        if (data) {
-               if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
+               if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
                        return rc;
 
                if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@@ -5891,6 +5928,7 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                                }
                                        }
                                        rc = 0;
+                                       mc->mc_flags &= ~C_EOF;
                                        goto set2;
                                }
                        }
@@ -5979,7 +6017,7 @@ set1:
                } else if (op == MDB_GET_BOTH || op == MDB_GET_BOTH_RANGE) {
                        MDB_val olddata;
                        MDB_cmp_func *dcmp;
-                       if ((rc = mdb_node_read(mc->mc_txn, leaf, &olddata)) != MDB_SUCCESS)
+                       if ((rc = mdb_node_read(mc, leaf, &olddata)) != MDB_SUCCESS)
                                return rc;
                        dcmp = mc->mc_dbx->md_dcmp;
 #if UINT_MAX < SIZE_MAX
@@ -5997,7 +6035,7 @@ set1:
                } else {
                        if (mc->mc_xcursor)
                                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
-                       if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
+                       if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
                                return rc;
                }
        }
@@ -6046,7 +6084,7 @@ mdb_cursor_first(MDB_cursor *mc, MDB_val *key, MDB_val *data)
                        if (rc)
                                return rc;
                } else {
-                       if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
+                       if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
                                return rc;
                }
        }
@@ -6091,7 +6129,7 @@ mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
                        if (rc)
                                return rc;
                } else {
-                       if ((rc = mdb_node_read(mc->mc_txn, leaf, data)) != MDB_SUCCESS)
+                       if ((rc = mdb_node_read(mc, leaf, data)) != MDB_SUCCESS)
                                return rc;
                }
        }
@@ -6137,7 +6175,7 @@ mdb_cursor_get(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                        if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                                                rc = mdb_cursor_get(&mc->mc_xcursor->mx_cursor, data, NULL, MDB_GET_CURRENT);
                                        } else {
-                                               rc = mdb_node_read(mc->mc_txn, leaf, data);
+                                               rc = mdb_node_read(mc, leaf, data);
                                        }
                                }
                        }
@@ -6202,6 +6240,30 @@ fetchm:
                        }
                }
                break;
+       case MDB_PREV_MULTIPLE:
+               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_last(mc, key, data);
+               else
+                       rc = MDB_SUCCESS;
+               if (rc == MDB_SUCCESS) {
+                       MDB_cursor *mx = &mc->mc_xcursor->mx_cursor;
+                       if (mx->mc_flags & C_INITIALIZED) {
+                               rc = mdb_cursor_sibling(mx, 0);
+                               if (rc == MDB_SUCCESS)
+                                       goto fetchm;
+                       } else {
+                               rc = MDB_NOTFOUND;
+                       }
+               }
+               break;
        case MDB_NEXT:
        case MDB_NEXT_DUP:
        case MDB_NEXT_NODUP:
@@ -6230,7 +6292,7 @@ fetchm:
                        MDB_node *leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
                        if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                                MDB_GET_KEY(leaf, key);
-                               rc = mdb_node_read(mc->mc_txn, leaf, data);
+                               rc = mdb_node_read(mc, leaf, data);
                                break;
                        }
                }
@@ -6267,7 +6329,8 @@ mdb_cursor_touch(MDB_cursor *mc)
 {
        int rc = MDB_SUCCESS;
 
-       if (mc->mc_dbi >= CORE_DBS && !(*mc->mc_dbflag & DB_DIRTY)) {
+       if (mc->mc_dbi >= CORE_DBS && !(*mc->mc_dbflag & (DB_DIRTY|DB_DUPDATA))) {
+               /* Touch DB record of named DB */
                MDB_cursor mc2;
                MDB_xcursor mcx;
                if (TXN_DBI_CHANGED(mc->mc_txn, mc->mc_dbi))
@@ -6614,7 +6677,7 @@ current:
                        int level, ovpages, dpages = OVPAGES(data->mv_size, env->me_psize);
 
                        memcpy(&pg, olddata.mv_data, sizeof(pg));
-                       if ((rc2 = mdb_page_get(mc->mc_txn, pg, &omp, &level)) != 0)
+                       if ((rc2 = mdb_page_get(mc, pg, &omp, &level)) != 0)
                                return rc2;
                        ovpages = omp->mp_pages;
 
@@ -6721,11 +6784,8 @@ new_sub:
                                if (m3->mc_ki[i] >= mc->mc_ki[i] && insert_key) {
                                        m3->mc_ki[i]++;
                                }
-                               if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
-                                       MDB_node *n2 = NODEPTR(mp, m3->mc_ki[i]);
-                                       if ((n2->mn_flags & (F_SUBDATA|F_DUPDATA)) == F_DUPDATA)
-                                               m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
-                               }
+                               if (XCURSOR_INITED(m3))
+                                       XCURSOR_REFRESH(m3, mp, m3->mc_ki[i]);
                        }
                }
        }
@@ -6776,9 +6836,7 @@ put_sub:
                                                if (m2->mc_ki[i] == mc->mc_ki[i]) {
                                                        mdb_xcursor_init2(m2, mx, new_dupdata);
                                                } else if (!insert_key && m2->mc_ki[i] < nkeys) {
-                                                       MDB_node *n2 = NODEPTR(mp, m2->mc_ki[i]);
-                                                       if ((n2->mn_flags & (F_SUBDATA|F_DUPDATA)) == F_DUPDATA)
-                                                               m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
+                                                       XCURSOR_REFRESH(m2, mp, m2->mc_ki[i]);
                                                }
                                        }
                                }
@@ -6883,13 +6941,12 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
                                                if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
                                                if (!(m2->mc_flags & C_INITIALIZED)) continue;
                                                if (m2->mc_pg[mc->mc_top] == mp) {
-                                                       if (m2->mc_ki[mc->mc_top] == mc->mc_ki[mc->mc_top]) {
-                                                               m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
-                                                       } else {
-                                                               MDB_node *n2 = NODEPTR(mp, m2->mc_ki[mc->mc_top]);
-                                                               if (!(n2->mn_flags & F_SUBDATA))
-                                                                       m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
+                                                       MDB_node *n2 = leaf;
+                                                       if (m2->mc_ki[mc->mc_top] != mc->mc_ki[mc->mc_top]) {
+                                                               n2 = NODEPTR(mp, m2->mc_ki[mc->mc_top]);
+                                                               if (n2->mn_flags & F_SUBDATA) continue;
                                                        }
+                                                       m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
                                                }
                                        }
                                }
@@ -6920,7 +6977,7 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
                pgno_t pg;
 
                memcpy(&pg, NODEDATA(leaf), sizeof(pg));
-               if ((rc = mdb_page_get(mc->mc_txn, pg, &omp, NULL)) ||
+               if ((rc = mdb_page_get(mc, pg, &omp, NULL)) ||
                        (rc = mdb_ovpage_free(mc, omp)))
                        goto fail;
        }
@@ -7328,7 +7385,7 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
        }
        DPRINTF(("Sub-db -%u root page %"Z"u", mx->mx_cursor.mc_dbi,
                mx->mx_db.md_root));
-       mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DIRTY; /* DB_DIRTY guides mdb_cursor_touch */
+       mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DUPDATA;
 #if UINT_MAX < SIZE_MAX
        if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t))
                mx->mx_dbx.md_cmp = mdb_cmp_clong;
@@ -7354,7 +7411,7 @@ mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
                mx->mx_cursor.mc_top = 0;
                mx->mx_cursor.mc_flags |= C_INITIALIZED;
                mx->mx_cursor.mc_ki[0] = 0;
-               mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DIRTY; /* DB_DIRTY guides mdb_cursor_touch */
+               mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DUPDATA;
 #if UINT_MAX < SIZE_MAX
                mx->mx_dbx.md_cmp = src_mx->mx_dbx.md_cmp;
 #endif
@@ -7726,12 +7783,8 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
                                        m3->mc_ki[csrc->mc_top] = cdst->mc_ki[cdst->mc_top];
                                        m3->mc_ki[csrc->mc_top-1]++;
                                }
-                               if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) &&
-                                       IS_LEAF(mps)) {
-                                       MDB_node *node = NODEPTR(m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
-                                       if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
-                                               m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
-                               }
+                               if (XCURSOR_INITED(m3) && IS_LEAF(mps))
+                                       XCURSOR_REFRESH(m3, m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
                        }
                } else
                /* Adding on the right, bump others down */
@@ -7752,12 +7805,8 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
                                        } else {
                                                m3->mc_ki[csrc->mc_top]--;
                                        }
-                                       if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) &&
-                                               IS_LEAF(mps)) {
-                                               MDB_node *node = NODEPTR(m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
-                                               if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
-                                                       m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
-                                       }
+                                       if (XCURSOR_INITED(m3) && IS_LEAF(mps))
+                                               XCURSOR_REFRESH(m3, m3->mc_pg[csrc->mc_top], m3->mc_ki[csrc->mc_top]);
                                }
                        }
                }
@@ -7958,12 +8007,8 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                                m3->mc_ki[top-1] > csrc->mc_ki[top-1]) {
                                m3->mc_ki[top-1]--;
                        }
-                       if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) &&
-                               IS_LEAF(psrc)) {
-                               MDB_node *node = NODEPTR(m3->mc_pg[top], m3->mc_ki[top]);
-                               if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
-                                       m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
-                       }
+                       if (XCURSOR_INITED(m3) && IS_LEAF(psrc))
+                               XCURSOR_REFRESH(m3, m3->mc_pg[top], m3->mc_ki[top]);
                }
        }
        {
@@ -8079,7 +8124,7 @@ mdb_rebalance(MDB_cursor *mc)
                        if (rc)
                                return rc;
                        mc->mc_db->md_root = NODEPGNO(NODEPTR(mp, 0));
-                       rc = mdb_page_get(mc->mc_txn,mc->mc_db->md_root,&mc->mc_pg[0],NULL);
+                       rc = mdb_page_get(mc, mc->mc_db->md_root, &mc->mc_pg[0], NULL);
                        if (rc)
                                return rc;
                        mc->mc_db->md_depth--;
@@ -8140,7 +8185,7 @@ mdb_rebalance(MDB_cursor *mc)
                DPUTS("reading right neighbor");
                mn.mc_ki[ptop]++;
                node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
-               rc = mdb_page_get(mc->mc_txn,NODEPGNO(node),&mn.mc_pg[mn.mc_top],NULL);
+               rc = mdb_page_get(mc, NODEPGNO(node), &mn.mc_pg[mn.mc_top], NULL);
                if (rc)
                        return rc;
                mn.mc_ki[mn.mc_top] = 0;
@@ -8152,7 +8197,7 @@ mdb_rebalance(MDB_cursor *mc)
                DPUTS("reading left neighbor");
                mn.mc_ki[ptop]--;
                node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
-               rc = mdb_page_get(mc->mc_txn,NODEPGNO(node),&mn.mc_pg[mn.mc_top],NULL);
+               rc = mdb_page_get(mc, NODEPGNO(node), &mn.mc_pg[mn.mc_top], NULL);
                if (rc)
                        return rc;
                mn.mc_ki[mn.mc_top] = NUMKEYS(mn.mc_pg[mn.mc_top]) - 1;
@@ -8217,14 +8262,16 @@ mdb_cursor_del0(MDB_cursor *mc)
                        if (m3->mc_pg[mc->mc_top] == mp) {
                                if (m3->mc_ki[mc->mc_top] == ki) {
                                        m3->mc_flags |= C_DEL;
+                                       if (mc->mc_db->md_flags & MDB_DUPSORT) {
+                                               /* Sub-cursor referred into dataset which is gone */
+                                               m3->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
+                                       }
+                                       continue;
                                } else if (m3->mc_ki[mc->mc_top] > ki) {
                                        m3->mc_ki[mc->mc_top]--;
                                }
-                               if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)) {
-                                       MDB_node *node = NODEPTR(m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
-                                       if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
-                                               m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
-                               }
+                               if (XCURSOR_INITED(m3))
+                                       XCURSOR_REFRESH(m3, m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
                        }
                }
        }
@@ -8261,9 +8308,15 @@ mdb_cursor_del0(MDB_cursor *mc)
                                        }
                                        if (mc->mc_db->md_flags & MDB_DUPSORT) {
                                                MDB_node *node = NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]);
-                                               if (node->mn_flags & F_DUPDATA) {
-                                                       mdb_xcursor_init1(m3, node);
-                                                       m3->mc_xcursor->mx_cursor.mc_flags |= C_DEL;
+                                               /* If this node is a fake page, it needs to be reinited
+                                                * because its data has moved. But just reset mc_pg[0]
+                                                * if the xcursor is already live.
+                                                */
+                                               if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) {
+                                                       if (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)
+                                                               m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
+                                                       else
+                                                               mdb_xcursor_init1(m3, node);
                                                }
                                        }
                                }
@@ -8748,12 +8801,8 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                                m3->mc_ki[ptop] >= mc->mc_ki[ptop]) {
                                m3->mc_ki[ptop]++;
                        }
-                       if (m3->mc_xcursor && (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) &&
-                               IS_LEAF(mp)) {
-                               MDB_node *node = NODEPTR(m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
-                               if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA)
-                                       m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
-                       }
+                       if (XCURSOR_INITED(m3) && IS_LEAF(mp))
+                               XCURSOR_REFRESH(m3, m3->mc_pg[mc->mc_top], m3->mc_ki[mc->mc_top]);
                }
        }
        DPRINTF(("mp left: %d, rp left: %d", SIZELEFT(mp), SIZELEFT(rp)));
@@ -8794,23 +8843,26 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
 #ifndef MDB_WBUF
 #define MDB_WBUF       (1024*1024)
 #endif
+#define MDB_EOF                0x10    /**< #mdb_env_copyfd1() is done reading */
 
-       /** State needed for a compacting copy. */
+       /** State needed for a double-buffering compacting copy. */
 typedef struct mdb_copy {
+       MDB_env *mc_env;
+       MDB_txn *mc_txn;
        pthread_mutex_t mc_mutex;
-       pthread_cond_t mc_cond;
+       pthread_cond_t mc_cond; /**< Condition variable for #mc_new */
        char *mc_wbuf[2];
        char *mc_over[2];
-       MDB_env *mc_env;
-       MDB_txn *mc_txn;
        int mc_wlen[2];
        int mc_olen[2];
        pgno_t mc_next_pgno;
        HANDLE mc_fd;
-       int mc_status;
-       volatile int mc_new;
-       int mc_toggle;
-
+       int mc_toggle;                  /**< Buffer number in provider */
+       int mc_new;                             /**< (0-2 buffers to write) | (#MDB_EOF at end) */
+       /** Error code.  Never cleared if set.  Both threads can set nonzero
+        *      to fail the copy.  Not mutex-protected, LMDB expects atomic int.
+        */
+       volatile int mc_error;
 } mdb_copy;
 
        /** Dedicated writer thread for compacting copy. */
@@ -8829,20 +8881,16 @@ mdb_env_copythr(void *arg)
 #endif
 
        pthread_mutex_lock(&my->mc_mutex);
-       my->mc_new = 0;
-       pthread_cond_signal(&my->mc_cond);
        for(;;) {
                while (!my->mc_new)
                        pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
-               if (my->mc_new < 0) {
-                       my->mc_new = 0;
+               if (my->mc_new == 0 + MDB_EOF) /* 0 buffers, just EOF */
                        break;
-               }
-               my->mc_new = 0;
                wsize = my->mc_wlen[toggle];
                ptr = my->mc_wbuf[toggle];
 again:
-               while (wsize > 0) {
+               rc = MDB_SUCCESS;
+               while (wsize > 0 && !my->mc_error) {
                        DO_WRITE(rc, my->mc_fd, ptr, wsize, len);
                        if (!rc) {
                                rc = ErrCode();
@@ -8858,8 +8906,7 @@ again:
                        }
                }
                if (rc) {
-                       my->mc_status = rc;
-                       break;
+                       my->mc_error = rc;
                }
                /* If there's an overflow page tail, write it too */
                if (my->mc_olen[toggle]) {
@@ -8870,39 +8917,45 @@ again:
                }
                my->mc_wlen[toggle] = 0;
                toggle ^= 1;
+               /* Return the empty buffer to provider */
+               my->mc_new--;
                pthread_cond_signal(&my->mc_cond);
        }
-       pthread_cond_signal(&my->mc_cond);
        pthread_mutex_unlock(&my->mc_mutex);
        return (THREAD_RET)0;
 #undef DO_WRITE
 }
 
-       /** Tell the writer thread there's a buffer ready to write */
+       /** Give buffer and/or #MDB_EOF to writer thread, await unused buffer.
+        *
+        * @param[in] my control structure.
+        * @param[in] adjust (1 to hand off 1 buffer) | (MDB_EOF when ending).
+        */
 static int ESECT
-mdb_env_cthr_toggle(mdb_copy *my, int st)
+mdb_env_cthr_toggle(mdb_copy *my, int adjust)
 {
-       int toggle = my->mc_toggle ^ 1;
        pthread_mutex_lock(&my->mc_mutex);
-       if (my->mc_status) {
-               pthread_mutex_unlock(&my->mc_mutex);
-               return my->mc_status;
-       }
-       while (my->mc_new == 1)
-               pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
-       my->mc_new = st;
-       my->mc_toggle = toggle;
+       my->mc_new += adjust;
        pthread_cond_signal(&my->mc_cond);
+       while (my->mc_new & 2)          /* both buffers in use */
+               pthread_cond_wait(&my->mc_cond, &my->mc_mutex);
        pthread_mutex_unlock(&my->mc_mutex);
-       return 0;
+
+       my->mc_toggle ^= (adjust & 1);
+       /* Both threads reset mc_wlen, to be safe from threading errors */
+       my->mc_wlen[my->mc_toggle] = 0;
+       return my->mc_error;
 }
 
-       /** Depth-first tree traversal for compacting copy. */
+       /** Depth-first tree traversal for compacting copy.
+        * @param[in] my control structure.
+        * @param[in,out] pg database root.
+        * @param[in] flags includes #F_DUPDATA if it is a sorted-duplicate sub-DB.
+        */
 static int ESECT
 mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
 {
        MDB_cursor mc = {0};
-       MDB_txn *txn = my->mc_txn;
        MDB_node *ni;
        MDB_page *mo, *mp, *leaf;
        char *buf, *ptr;
@@ -8914,9 +8967,9 @@ mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
                return MDB_SUCCESS;
 
        mc.mc_snum = 1;
-       mc.mc_txn = txn;
+       mc.mc_txn = my->mc_txn;
 
-       rc = mdb_page_get(my->mc_txn, *pg, &mc.mc_pg[0], NULL);
+       rc = mdb_page_get(&mc, *pg, &mc.mc_pg[0], NULL);
        if (rc)
                return rc;
        rc = mdb_page_search_root(&mc, NULL, MDB_PS_FIRST);
@@ -8960,7 +9013,8 @@ mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
                                                }
 
                                                memcpy(&pg, NODEDATA(ni), sizeof(pg));
-                                               rc = mdb_page_get(txn, pg, &omp, NULL);
+                                               memcpy(NODEDATA(ni), &my->mc_next_pgno, sizeof(pgno_t));
+                                               rc = mdb_page_get(&mc, pg, &omp, NULL);
                                                if (rc)
                                                        goto done;
                                                if (my->mc_wlen[toggle] >= MDB_WBUF) {
@@ -8982,7 +9036,6 @@ mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
                                                                goto done;
                                                        toggle = my->mc_toggle;
                                                }
-                                               memcpy(NODEDATA(ni), &mo->mp_pgno, sizeof(pgno_t));
                                        } else if (ni->mn_flags & F_SUBDATA) {
                                                MDB_db db;
 
@@ -9011,7 +9064,7 @@ mdb_env_cwalk(mdb_copy *my, pgno_t *pg, int flags)
 again:
                                ni = NODEPTR(mp, mc.mc_ki[mc.mc_top]);
                                pg = NODEPGNO(ni);
-                               rc = mdb_page_get(txn, pg, &mp, NULL);
+                               rc = mdb_page_get(&mc, pg, &mp, NULL);
                                if (rc)
                                        goto done;
                                mc.mc_top++;
@@ -9060,47 +9113,56 @@ mdb_env_copyfd1(MDB_env *env, HANDLE fd)
 {
        MDB_meta *mm;
        MDB_page *mp;
-       mdb_copy my;
+       mdb_copy my = {0};
        MDB_txn *txn = NULL;
        pthread_t thr;
-       int rc;
+       pgno_t root, new_root;
+       int rc = MDB_SUCCESS;
 
 #ifdef _WIN32
-       my.mc_mutex = CreateMutex(NULL, FALSE, NULL);
-       my.mc_cond = CreateEvent(NULL, FALSE, FALSE, NULL);
+       if (!(my.mc_mutex = CreateMutex(NULL, FALSE, NULL)) ||
+               !(my.mc_cond = CreateEvent(NULL, FALSE, FALSE, NULL))) {
+               rc = ErrCode();
+               goto done;
+       }
        my.mc_wbuf[0] = _aligned_malloc(MDB_WBUF*2, env->me_os_psize);
-       if (my.mc_wbuf[0] == NULL)
-               return errno;
+       if (my.mc_wbuf[0] == NULL) {
+               /* _aligned_malloc() sets errno, but we use Windows error codes */
+               rc = ERROR_NOT_ENOUGH_MEMORY;
+               goto done;
+       }
 #else
-       pthread_mutex_init(&my.mc_mutex, NULL);
-       pthread_cond_init(&my.mc_cond, NULL);
+       if ((rc = pthread_mutex_init(&my.mc_mutex, NULL)) != 0)
+               return rc;
+       if ((rc = pthread_cond_init(&my.mc_cond, NULL)) != 0)
+               goto done2;
 #ifdef HAVE_MEMALIGN
        my.mc_wbuf[0] = memalign(env->me_os_psize, MDB_WBUF*2);
-       if (my.mc_wbuf[0] == NULL)
-               return errno;
+       if (my.mc_wbuf[0] == NULL) {
+               rc = errno;
+               goto done;
+       }
 #else
-       rc = posix_memalign((void **)&my.mc_wbuf[0], env->me_os_psize, MDB_WBUF*2);
-       if (rc)
-               return rc;
+       {
+               void *p;
+               if ((rc = posix_memalign(&p, env->me_os_psize, MDB_WBUF*2)) != 0)
+                       goto done;
+               my.mc_wbuf[0] = p;
+       }
 #endif
 #endif
        memset(my.mc_wbuf[0], 0, MDB_WBUF*2);
        my.mc_wbuf[1] = my.mc_wbuf[0] + MDB_WBUF;
-       my.mc_wlen[0] = 0;
-       my.mc_wlen[1] = 0;
-       my.mc_olen[0] = 0;
-       my.mc_olen[1] = 0;
        my.mc_next_pgno = NUM_METAS;
-       my.mc_status = 0;
-       my.mc_new = 1;
-       my.mc_toggle = 0;
        my.mc_env = env;
        my.mc_fd = fd;
-       THREAD_CREATE(thr, mdb_env_copythr, &my);
+       rc = THREAD_CREATE(thr, mdb_env_copythr, &my);
+       if (rc)
+               goto done;
 
        rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
        if (rc)
-               return rc;
+               goto finish;
 
        mp = (MDB_page *)my.mc_wbuf[0];
        memset(mp, 0, NUM_METAS * env->me_psize);
@@ -9116,57 +9178,64 @@ mdb_env_copyfd1(MDB_env *env, HANDLE fd)
        *(MDB_meta *)METADATA(mp) = *mm;
        mm = (MDB_meta *)METADATA(mp);
 
-       /* Count the number of free pages, subtract from lastpg to find
-        * number of active pages
-        */
-       {
+       /* Set metapage 1 with current main DB */
+       root = new_root = txn->mt_dbs[MAIN_DBI].md_root;
+       if (root != P_INVALID) {
+               /* Count free pages + freeDB pages.  Subtract from last_pg
+                * to find the new last_pg, which also becomes the new root.
+                */
                MDB_ID freecount = 0;
                MDB_cursor mc;
                MDB_val key, data;
                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;
+               if (rc != MDB_NOTFOUND)
+                       goto finish;
                freecount += txn->mt_dbs[FREE_DBI].md_branch_pages +
                        txn->mt_dbs[FREE_DBI].md_leaf_pages +
                        txn->mt_dbs[FREE_DBI].md_overflow_pages;
 
-               /* Set metapage 1 */
-               mm->mm_last_pg = txn->mt_next_pgno - freecount - 1;
+               new_root = txn->mt_next_pgno - 1 - freecount;
+               mm->mm_last_pg = new_root;
                mm->mm_dbs[MAIN_DBI] = txn->mt_dbs[MAIN_DBI];
-               if (mm->mm_last_pg > NUM_METAS-1) {
-                       mm->mm_dbs[MAIN_DBI].md_root = mm->mm_last_pg;
-                       mm->mm_txnid = 1;
-               } else {
-                       mm->mm_dbs[MAIN_DBI].md_root = P_INVALID;
-               }
+               mm->mm_dbs[MAIN_DBI].md_root = new_root;
+       } else {
+               /* When the DB is empty, handle it specially to
+                * fix any breakage like page leaks from ITS#8174.
+                */
+               mm->mm_dbs[MAIN_DBI].md_flags = txn->mt_dbs[MAIN_DBI].md_flags;
        }
+       if (root != P_INVALID || mm->mm_dbs[MAIN_DBI].md_flags) {
+               mm->mm_txnid = 1;               /* use metapage 1 */
+       }
+
        my.mc_wlen[0] = env->me_psize * NUM_METAS;
        my.mc_txn = txn;
-       pthread_mutex_lock(&my.mc_mutex);
-       while(my.mc_new)
-               pthread_cond_wait(&my.mc_cond, &my.mc_mutex);
-       pthread_mutex_unlock(&my.mc_mutex);
-       rc = mdb_env_cwalk(&my, &txn->mt_dbs[MAIN_DBI].md_root, 0);
-       if (rc == MDB_SUCCESS && my.mc_wlen[my.mc_toggle])
-               rc = mdb_env_cthr_toggle(&my, 1);
-       mdb_env_cthr_toggle(&my, -1);
-       pthread_mutex_lock(&my.mc_mutex);
-       while(my.mc_new)
-               pthread_cond_wait(&my.mc_cond, &my.mc_mutex);
-       pthread_mutex_unlock(&my.mc_mutex);
-       THREAD_FINISH(thr);
+       rc = mdb_env_cwalk(&my, &root, 0);
+       if (rc == MDB_SUCCESS && root != new_root) {
+               rc = MDB_INCOMPATIBLE;  /* page leak or corrupt DB */
+       }
 
+finish:
+       if (rc)
+               my.mc_error = rc;
+       mdb_env_cthr_toggle(&my, 1 | MDB_EOF);
+       rc = THREAD_FINISH(thr);
        mdb_txn_abort(txn);
+
+done:
 #ifdef _WIN32
-       CloseHandle(my.mc_cond);
-       CloseHandle(my.mc_mutex);
-       _aligned_free(my.mc_wbuf[0]);
+       if (my.mc_wbuf[0]) _aligned_free(my.mc_wbuf[0]);
+       if (my.mc_cond)  CloseHandle(my.mc_cond);
+       if (my.mc_mutex) CloseHandle(my.mc_mutex);
 #else
+       free(my.mc_wbuf[0]);
        pthread_cond_destroy(&my.mc_cond);
+done2:
        pthread_mutex_destroy(&my.mc_mutex);
-       free(my.mc_wbuf[0]);
 #endif
-       return rc;
+       return rc ? rc : my.mc_error;
 }
 
        /** Copy environment as-is. */
@@ -9325,17 +9394,12 @@ mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
        }
 
        if (env->me_psize >= env->me_os_psize) {
-#ifdef O_DIRECT
+#ifdef F_NOCACHE       /* __APPLE__ */
+       (void) fcntl(newfd, F_NOCACHE, 1);
+#elif defined O_DIRECT
        /* Set O_DIRECT if the file system supports it */
        if ((rc = fcntl(newfd, F_GETFL)) != -1)
                (void) fcntl(newfd, F_SETFL, rc | O_DIRECT);
-#endif
-#ifdef F_NOCACHE       /* __APPLE__ */
-       rc = fcntl(newfd, F_NOCACHE, 1);
-       if (rc) {
-               rc = ErrCode();
-               goto leave;
-       }
 #endif
        }
 
@@ -9694,7 +9758,7 @@ mdb_drop0(MDB_cursor *mc, int subs)
                                                MDB_page *omp;
                                                pgno_t pg;
                                                memcpy(&pg, NODEDATA(ni), sizeof(pg));
-                                               rc = mdb_page_get(txn, pg, &omp, NULL);
+                                               rc = mdb_page_get(mc, pg, &omp, NULL);
                                                if (rc != 0)
                                                        goto done;
                                                mdb_cassert(mc, IS_OVERFLOW(omp));