]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
Clean up MDB_USE_ROBUST #defines
[openldap] / libraries / liblmdb / mdb.c
index 77b05b1edcfa4a34a68591b55eec402c69eeb46e..32f0a5a881148b74c614c5ee4899831a18cf542b 100644 (file)
@@ -228,7 +228,7 @@ union semun {
 
 #if (BYTE_ORDER == LITTLE_ENDIAN) == (BYTE_ORDER == BIG_ENDIAN)
 # error "Unknown or unsupported endianness (BYTE_ORDER)"
-#elif (-6 & 5) || CHAR_BIT != 8 || UINT_MAX < 0xffffffff || ULONG_MAX % 0xFFFF
+#elif (-6 & 5) || CHAR_BIT!=8 || UINT_MAX!=0xffffffff || MDB_SIZE_MAX%UINT_MAX
 # error "Two's complement, reasonably sized integer types, please"
 #endif
 
@@ -303,6 +303,10 @@ union semun {
 #  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))
@@ -310,10 +314,9 @@ union semun {
 #   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
 
@@ -355,12 +358,10 @@ typedef HANDLE mdb_mutex_t, mdb_mutexref_t;
 #else
 #define MDB_PROCESS_QUERY_LIMITED_INFORMATION 0x1000
 #endif
-#define        Z       "I"
 #else
 #define THREAD_RET     void *
 #define THREAD_CREATE(thr,start,arg)   pthread_create(&thr,NULL,start,arg)
 #define THREAD_FINISH(thr)     pthread_join(thr,NULL)
-#define        Z       "z"                     /**< printf format modifier for size_t */
 
        /** For MDB_LOCK_FORMAT: True if readers take a pid lock in the lockfile */
 #define MDB_PIDLOCK                    1
@@ -458,15 +459,9 @@ typedef pthread_mutex_t mdb_mutex_t[1], *mdb_mutexref_t;
 #define        GET_PAGESIZE(x) ((x) = sysconf(_SC_PAGE_SIZE))
 #endif
 
-#ifdef MDB_VL32
-#ifdef _WIN32
-#define        Y       "I64"
-#else
-#define        Y       "ll"
-#endif
-#else
-#define Y      Z
-#endif
+#define        Z       MDB_FMT_Z       /**< printf/scanf format modifier for size_t */
+#define        Yu      MDB_PRIy(u)     /**< printf format for #mdb_size_t */
+#define        Yd      MDB_PRIy(d)     /**< printf format for "signed #mdb_size_t" */
 
 #if defined(_WIN32) || defined(MDB_USE_POSIX_SEM)
 #define MNAME_LEN      32
@@ -876,9 +871,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
@@ -887,7 +896,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.
@@ -954,7 +963,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.
@@ -1037,7 +1048,7 @@ typedef struct MDB_node {
 #ifdef MISALIGNED_OK
 #define COPY_PGNO(dst,src)     dst = src
 #else
-#if SIZE_MAX > 4294967295UL
+#if MDB_SIZE_MAX > 0xffffffffU
 #define COPY_PGNO(dst,src)     do { \
        unsigned short *s, *d;  \
        s = (unsigned short *)&(src);   \
@@ -1082,9 +1093,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)
 
@@ -1123,7 +1134,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;
 
@@ -1176,7 +1190,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
@@ -1313,6 +1327,11 @@ struct MDB_cursor {
        indx_t          mc_ki[CURSOR_STACK];    /**< stack of page indices */
 #ifdef MDB_VL32
        MDB_page        *mc_ovpg;               /**< a referenced overflow page */
+#      define MC_OVPG(mc)                      ((mc)->mc_ovpg)
+#      define MC_SET_OVPG(mc, pg)      ((mc)->mc_ovpg = (pg))
+#else
+#      define MC_OVPG(mc)                      ((MDB_page *)0)
+#      define MC_SET_OVPG(mc, pg)      ((void)0)
 #endif
 };
 
@@ -1518,13 +1537,18 @@ static int mdb_reader_check0(MDB_env *env, int rlocked, int *dead);
 static MDB_cmp_func    mdb_cmp_memn, mdb_cmp_memnr, mdb_cmp_int, mdb_cmp_cint, mdb_cmp_long;
 /** @endcond */
 
-/** Compare two items pointing at size_t's of unknown alignment. */
+/** Compare two items pointing at '#mdb_size_t's of unknown alignment. */
 #ifdef MISALIGNED_OK
 # define mdb_cmp_clong mdb_cmp_long
 #else
 # define mdb_cmp_clong mdb_cmp_cint
 #endif
 
+/** True if we need #mdb_cmp_clong() instead of \b cmp for #MDB_INTEGERDUP */
+#define NEED_CMP_CLONG(cmp, ksize) \
+       (UINT_MAX < MDB_SIZE_MAX && \
+        (cmp) == mdb_cmp_int && (ksize) == sizeof(mdb_size_t))
+
 #ifdef _WIN32
 static SECURITY_DESCRIPTOR mdb_null_sd;
 static SECURITY_ATTRIBUTES mdb_all_sa;
@@ -1565,6 +1589,7 @@ static char *const mdb_errstr[] = {
        "MDB_BAD_TXN: Transaction must abort, has a child, or is invalid",
        "MDB_BAD_VALSIZE: Unsupported size of key/DB name/data, or wrong DUPFIXED size",
        "MDB_BAD_DBI: The specified DBI handle was closed/changed unexpectedly",
+       "MDB_PROBLEM: Unexpected problem - txn should abort",
 };
 
 char *
@@ -1709,20 +1734,20 @@ mdb_page_list(MDB_page *mp)
        case P_LEAF|P_LEAF2:        type = "LEAF2 page";                break;
        case P_LEAF|P_LEAF2|P_SUBP: type = "LEAF2 sub-page";    break;
        case P_OVERFLOW:
-               fprintf(stderr, "Overflow page %"Y"u pages %u%s\n",
+               fprintf(stderr, "Overflow page %"Yu" pages %u%s\n",
                        pgno, mp->mp_pages, state);
                return;
        case P_META:
-               fprintf(stderr, "Meta-page %"Y"u txnid %"Y"u\n",
+               fprintf(stderr, "Meta-page %"Yu" txnid %"Yu"\n",
                        pgno, ((MDB_meta *)METADATA(mp))->mm_txnid);
                return;
        default:
-               fprintf(stderr, "Bad page %"Y"u flags 0x%u\n", pgno, mp->mp_flags);
+               fprintf(stderr, "Bad page %"Yu" flags 0x%X\n", pgno, mp->mp_flags);
                return;
        }
 
        nkeys = NUMKEYS(mp);
-       fprintf(stderr, "%s %"Y"u numkeys %d%s\n", type, pgno, nkeys, state);
+       fprintf(stderr, "%s %"Yu" numkeys %d%s\n", type, pgno, nkeys, state);
 
        for (i=0; i<nkeys; i++) {
                if (IS_LEAF2(mp)) {     /* LEAF2 pages have no mp_ptrs[] or node headers */
@@ -1737,7 +1762,7 @@ mdb_page_list(MDB_page *mp)
                key.mv_data = node->mn_data;
                nsize = NODESIZE + key.mv_size;
                if (IS_BRANCH(mp)) {
-                       fprintf(stderr, "key %d: page %"Y"u, %s\n", i, NODEPGNO(node),
+                       fprintf(stderr, "key %d: page %"Yu", %s\n", i, NODEPGNO(node),
                                DKEY(&key));
                        total += nsize;
                } else {
@@ -1833,7 +1858,7 @@ static void mdb_audit(MDB_txn *txn)
                }
        }
        if (freecount + count + NUM_METAS != txn->mt_next_pgno) {
-               fprintf(stderr, "audit: %"Y"u freecount: %"Y"u count: %"Y"u total: %"Y"u next_pgno: %"Y"u\n",
+               fprintf(stderr, "audit: %"Yu" freecount: %"Yu" count: %"Yu" total: %"Yu" next_pgno: %"Yu"\n",
                        txn->mt_txnid, freecount, count+NUM_METAS,
                        freecount+count+NUM_METAS, txn->mt_next_pgno);
        }
@@ -1850,10 +1875,8 @@ int
 mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
 {
        MDB_cmp_func *dcmp = txn->mt_dbxs[dbi].md_dcmp;
-#if UINT_MAX < SIZE_MAX || defined(MDB_VL32)
-       if (dcmp == mdb_cmp_int && a->mv_size == sizeof(mdb_size_t))
+       if (NEED_CMP_CLONG(dcmp, a->mv_size))
                dcmp = mdb_cmp_clong;
-#endif
        return dcmp(a, b);
 }
 
@@ -1968,8 +1991,14 @@ mdb_cursor_unref(MDB_cursor *mc)
        mc->mc_pg[0] = NULL;
        mc->mc_flags &= ~C_INITIALIZED;
 }
+#define MDB_CURSOR_UNREF(mc, force) \
+       (((force) || ((mc)->mc_flags & C_INITIALIZED)) \
+        ? mdb_cursor_unref(mc) \
+        : (void)0)
+
 #else
 #define MDB_PAGE_UNREF(txn, mp)
+#define MDB_CURSOR_UNREF(mc, force) ((void)0)
 #endif /* MDB_VL32 */
 
 /** Loosen or free a single page.
@@ -2001,7 +2030,7 @@ mdb_page_loose(MDB_cursor *mc, MDB_page *mp)
                                        if (mp != dl[x].mptr) { /* bad cursor? */
                                                mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
                                                txn->mt_flags |= MDB_TXN_ERROR;
-                                               return MDB_CORRUPTED;
+                                               return MDB_PROBLEM;
                                        }
                                        /* ok, it's ours */
                                        loose = 1;
@@ -2013,8 +2042,7 @@ mdb_page_loose(MDB_cursor *mc, MDB_page *mp)
                }
        }
        if (loose) {
-               DPRINTF(("loosen db %d page %"Y"u", DDBI(mc),
-                       mp->mp_pgno));
+               DPRINTF(("loosen db %d page %"Yu, DDBI(mc), mp->mp_pgno));
                NEXT_LOOSE_PAGE(mp) = txn->mt_loose_pgs;
                txn->mt_loose_pgs = mp;
                txn->mt_loose_count++;
@@ -2311,8 +2339,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                np = txn->mt_loose_pgs;
                txn->mt_loose_pgs = NEXT_LOOSE_PAGE(np);
                txn->mt_loose_count--;
-               DPRINTF(("db %d use loose page %"Y"u", DDBI(mc),
-                               np->mp_pgno));
+               DPRINTF(("db %d use loose page %"Yu, DDBI(mc), np->mp_pgno));
                *mp = np;
                return MDB_SUCCESS;
        }
@@ -2414,10 +2441,10 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                }
                env->me_pglast = last;
 #if (MDB_DEBUG) > 1
-               DPRINTF(("IDL read txn %"Y"u root %"Y"u num %u",
+               DPRINTF(("IDL read txn %"Yu" root %"Yu" num %u",
                        last, txn->mt_dbs[FREE_DBI].md_root, i));
                for (j = i; j; j--)
-                       DPRINTF(("IDL %"Y"u", idl[j]));
+                       DPRINTF(("IDL %"Yu, idl[j]));
 #endif
                /* Merge in descending sorted order */
                mdb_midl_xmerge(mop, idl);
@@ -2587,7 +2614,7 @@ mdb_page_touch(MDB_cursor *mc)
                        (rc = mdb_page_alloc(mc, 1, &np)))
                        goto fail;
                pgno = np->mp_pgno;
-               DPRINTF(("touched db %d page %"Y"u -> %"Y"u", DDBI(mc),
+               DPRINTF(("touched db %d page %"Yu" -> %"Yu, DDBI(mc),
                        mp->mp_pgno, pgno));
                mdb_cassert(mc, mp->mp_pgno != pgno);
                mdb_midl_xappend(txn->mt_free_pgs, mp->mp_pgno);
@@ -2611,7 +2638,7 @@ mdb_page_touch(MDB_cursor *mc)
                                if (mp != dl[x].mptr) { /* bad cursor? */
                                        mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
                                        txn->mt_flags |= MDB_TXN_ERROR;
-                                       return MDB_CORRUPTED;
+                                       return MDB_PROBLEM;
                                }
                                return 0;
                        }
@@ -2988,7 +3015,7 @@ mdb_txn_renew(MDB_txn *txn)
 
        rc = mdb_txn_renew0(txn);
        if (rc == MDB_SUCCESS) {
-               DPRINTF(("renew txn %"Y"u%c %p on mdbenv %p, root page %"Y"u",
+               DPRINTF(("renew txn %"Yu"%c %p on mdbenv %p, root page %"Yu,
                        txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
                        (void *)txn, (void *)txn->mt_env, txn->mt_dbs[MAIN_DBI].md_root));
        }
@@ -3106,7 +3133,7 @@ renew:
        } else {
                txn->mt_flags |= flags; /* could not change txn=me_txn0 earlier */
                *ret = txn;
-               DPRINTF(("begin txn %"Y"u%c %p on mdbenv %p, root page %"Y"u",
+               DPRINTF(("begin txn %"Yu"%c %p on mdbenv %p, root page %"Yu,
                        txn->mt_txnid, (flags & MDB_RDONLY) ? 'r' : 'w',
                        (void *) txn, (void *) env, txn->mt_dbs[MAIN_DBI].md_root));
        }
@@ -3173,7 +3200,7 @@ mdb_txn_end(MDB_txn *txn, unsigned mode)
        /* Export or close DBI handles opened in this txn */
        mdb_dbis_update(txn, mode & MDB_END_UPDATE);
 
-       DPRINTF(("%s txn %"Y"u%c %p on mdbenv %p, root page %"Y"u",
+       DPRINTF(("%s txn %"Yu"%c %p on mdbenv %p, root page %"Yu,
                names[mode & MDB_END_OPMASK],
                txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
                (void *) txn, (void *)env, txn->mt_dbs[MAIN_DBI].md_root));
@@ -3374,10 +3401,10 @@ mdb_freelist_save(MDB_txn *txn)
 #if (MDB_DEBUG) > 1
                        {
                                unsigned int i = free_pgs[0];
-                               DPRINTF(("IDL write txn %"Y"u root %"Y"u num %u",
+                               DPRINTF(("IDL write txn %"Yu" root %"Yu" num %u",
                                        txn->mt_txnid, txn->mt_dbs[FREE_DBI].md_root, i));
                                for (; i; i--)
-                                       DPRINTF(("IDL %"Y"u", free_pgs[i]));
+                                       DPRINTF(("IDL %"Yu, free_pgs[i]));
                        }
 #endif
                        continue;
@@ -3545,7 +3572,7 @@ mdb_page_flush(MDB_txn *txn, int keep)
                 * the write offset, to at least save the overhead of a Seek
                 * system call.
                 */
-               DPRINTF(("committing page %"Z"u", pgno));
+               DPRINTF(("committing page %"Yu, pgno));
                memset(&ov, 0, sizeof(ov));
                ov.Offset = pos & 0xffffffff;
                ov.OffsetHigh = pos >> 16 >> 16;
@@ -3596,7 +3623,7 @@ retry_seek:
                        wpos = pos;
                        wsize = 0;
                }
-               DPRINTF(("committing page %"Y"u", pgno));
+               DPRINTF(("committing page %"Yu, pgno));
                next_pos = pos + size;
                iov[n].iov_len = size;
                iov[n].iov_base = (char *)dp;
@@ -3809,7 +3836,7 @@ mdb_txn_commit(MDB_txn *txn)
                !(txn->mt_flags & (MDB_TXN_DIRTY|MDB_TXN_SPILLS)))
                goto done;
 
-       DPRINTF(("committing txn %"Y"u %p on mdbenv %p, root page %"Y"u",
+       DPRINTF(("committing txn %"Yu" %p on mdbenv %p, root page %"Yu,
            txn->mt_txnid, (void*)txn, (void*)env, txn->mt_dbs[MAIN_DBI].md_root));
 
        /* Update DB root pointers */
@@ -3907,7 +3934,7 @@ mdb_env_read_header(MDB_env *env, MDB_meta *meta)
                p = (MDB_page *)&pbuf;
 
                if (!F_ISSET(p->mp_flags, P_META)) {
-                       DPRINTF(("page %"Y"u not a meta page", p->mp_pgno));
+                       DPRINTF(("page %"Yu" not a meta page", p->mp_pgno));
                        return MDB_INVALID;
                }
 
@@ -4019,7 +4046,7 @@ mdb_env_write_meta(MDB_txn *txn)
 #endif
 
        toggle = txn->mt_txnid & 1;
-       DPRINTF(("writing meta page %d for root page %"Y"u",
+       DPRINTF(("writing meta page %d for root page %"Yu,
                toggle, txn->mt_dbs[MAIN_DBI].md_root));
 
        env = txn->mt_env;
@@ -4513,13 +4540,13 @@ mdb_env_open2(MDB_env *env)
 
                DPRINTF(("opened database version %u, pagesize %u",
                        meta->mm_version, env->me_psize));
-               DPRINTF(("using meta page %d",    (int) (meta->mm_txnid & 1)));
-               DPRINTF(("depth: %u",             db->md_depth));
-               DPRINTF(("entries: %"Y"u",        db->md_entries));
-               DPRINTF(("branch pages: %"Y"u",   db->md_branch_pages));
-               DPRINTF(("leaf pages: %"Y"u",     db->md_leaf_pages));
-               DPRINTF(("overflow pages: %"Y"u", db->md_overflow_pages));
-               DPRINTF(("root: %"Y"u",           db->md_root));
+               DPRINTF(("using meta page %d",  (int) (meta->mm_txnid & 1)));
+               DPRINTF(("depth: %u",           db->md_depth));
+               DPRINTF(("entries: %"Yu,        db->md_entries));
+               DPRINTF(("branch pages: %"Yu,   db->md_branch_pages));
+               DPRINTF(("leaf pages: %"Yu,     db->md_leaf_pages));
+               DPRINTF(("overflow pages: %"Yu, db->md_overflow_pages));
+               DPRINTF(("root: %"Yu,           db->md_root));
        }
 #endif
 
@@ -5409,7 +5436,7 @@ mdb_env_close(MDB_env *env)
        free(env);
 }
 
-/** Compare two items pointing at aligned mdb_size_t's */
+/** Compare two items pointing at aligned #mdb_size_t's */
 static int
 mdb_cmp_long(const MDB_val *a, const MDB_val *b)
 {
@@ -5420,7 +5447,7 @@ mdb_cmp_long(const MDB_val *a, const MDB_val *b)
 /** Compare two items pointing at aligned unsigned int's.
  *
  *     This is also set as #MDB_INTEGERDUP|#MDB_DUPFIXED's #MDB_dbx.%md_dcmp,
- *     but #mdb_cmp_clong() is called instead if the data type is mdb_size_t.
+ *     but #mdb_cmp_clong() is called instead if the data type is #mdb_size_t.
  */
 static int
 mdb_cmp_int(const MDB_val *a, const MDB_val *b)
@@ -5525,7 +5552,7 @@ mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
 
        nkeys = NUMKEYS(mp);
 
-       DPRINTF(("searching %u keys in %s %spage %"Y"u",
+       DPRINTF(("searching %u keys in %s %spage %"Yu,
            nkeys, IS_LEAF(mp) ? "leaf" : "branch", IS_SUBP(mp) ? "sub-" : "",
            mdb_dbg_pgno(mp)));
 
@@ -5573,7 +5600,7 @@ mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp)
                                DPRINTF(("found leaf index %u [%s], rc = %i",
                                    i, DKEY(&nodekey), rc));
                        else
-                               DPRINTF(("found branch index %u [%s -> %"Y"u], rc = %i",
+                               DPRINTF(("found branch index %u [%s -> %"Yu"], rc = %i",
                                    i, DKEY(&nodekey), NODEPGNO(node), rc));
 #endif
                        if (rc == 0)
@@ -5621,7 +5648,7 @@ static void
 mdb_cursor_pop(MDB_cursor *mc)
 {
        if (mc->mc_snum) {
-               DPRINTF(("popping page %"Y"u off db %d cursor %p",
+               DPRINTF(("popping page %"Yu" off db %d cursor %p",
                        mc->mc_pg[mc->mc_top]->mp_pgno, DDBI(mc), (void *) mc));
 
                mc->mc_snum--;
@@ -5637,7 +5664,7 @@ mdb_cursor_pop(MDB_cursor *mc)
 static int
 mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 {
-       DPRINTF(("pushing page %"Y"u on db %d cursor %p", mp->mp_pgno,
+       DPRINTF(("pushing page %"Yu" on db %d cursor %p", mp->mp_pgno,
                DDBI(mc), (void *) mc));
 
        if (mc->mc_snum >= CURSOR_STACK) {
@@ -5951,9 +5978,6 @@ static int
 mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **ret, int *lvl)
 {
        MDB_txn *txn = mc->mc_txn;
-#ifndef MDB_VL32
-       MDB_env *env = txn->mt_env;
-#endif
        MDB_page *p = NULL;
        int level;
 
@@ -5972,14 +5996,7 @@ mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **ret, int *lvl)
                                MDB_ID pn = pgno << 1;
                                x = mdb_midl_search(tx2->mt_spill_pgs, pn);
                                if (x <= tx2->mt_spill_pgs[0] && tx2->mt_spill_pgs[x] == pn) {
-#ifdef MDB_VL32
-                                       int rc = mdb_rpage_get(txn, pgno, &p);
-                                       if (rc)
-                                               return rc;
-#else
-                                       p = (MDB_page *)(env->me_map + env->me_psize * pgno);
-#endif
-                                       goto done;
+                                       goto mapped;
                                }
                        }
                        if (dl[0].mid) {
@@ -5993,21 +6010,24 @@ mdb_page_get(MDB_cursor *mc, pgno_t pgno, MDB_page **ret, int *lvl)
                } while ((tx2 = tx2->mt_parent) != NULL);
        }
 
-       if (pgno < txn->mt_next_pgno) {
-               level = 0;
+       if (pgno >= txn->mt_next_pgno) {
+               DPRINTF(("page %"Yu" not found", pgno));
+               txn->mt_flags |= MDB_TXN_ERROR;
+               return MDB_PAGE_NOTFOUND;
+       }
+
+       level = 0;
+
+mapped:
+       {
 #ifdef MDB_VL32
-               {
-                       int rc = mdb_rpage_get(txn, pgno, &p);
-                       if (rc)
-                               return rc;
-               }
+               int rc = mdb_rpage_get(txn, pgno, &p);
+               if (rc)
+                       return rc;
 #else
+               MDB_env *env = txn->mt_env;
                p = (MDB_page *)(env->me_map + env->me_psize * pgno);
 #endif
-       } else {
-               DPRINTF(("page %"Y"u not found", pgno));
-               txn->mt_flags |= MDB_TXN_ERROR;
-               return MDB_PAGE_NOTFOUND;
        }
 
 done:
@@ -6031,13 +6051,13 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
                MDB_node        *node;
                indx_t          i;
 
-               DPRINTF(("branch page %"Y"u has %u keys", mp->mp_pgno, NUMKEYS(mp)));
+               DPRINTF(("branch page %"Yu" has %u keys", mp->mp_pgno, NUMKEYS(mp)));
                /* Don't assert on branch pages in the FreeDB. We can get here
                 * while in the process of rebalancing a FreeDB branch page; we must
                 * let that proceed. ITS#8336
                 */
                mdb_cassert(mc, !mc->mc_dbi || NUMKEYS(mp) > 1);
-               DPRINTF(("found index 0 to page %"Y"u", NODEPGNO(NODEPTR(mp, 0))));
+               DPRINTF(("found index 0 to page %"Yu, NODEPGNO(NODEPTR(mp, 0))));
 
                if (flags & (MDB_PS_FIRST|MDB_PS_LAST)) {
                        i = 0;
@@ -6082,7 +6102,7 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
                return MDB_CORRUPTED;
        }
 
-       DPRINTF(("found leaf page %"Y"u for key [%s]", mp->mp_pgno,
+       DPRINTF(("found leaf page %"Yu" for key [%s]", mp->mp_pgno,
            key ? DKEY(key) : "null"));
        mc->mc_flags |= C_INITIALIZED;
        mc->mc_flags &= ~C_EOF;
@@ -6197,7 +6217,7 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
        mc->mc_snum = 1;
        mc->mc_top = 0;
 
-       DPRINTF(("db %d root page %"Y"u has flags 0x%X",
+       DPRINTF(("db %d root page %"Yu" has flags 0x%X",
                DDBI(mc), root, mc->mc_pg[0]->mp_flags));
 
        if (flags & MDB_PS_MODIFY) {
@@ -6222,7 +6242,7 @@ mdb_ovpage_free(MDB_cursor *mc, MDB_page *mp)
        MDB_ID pn = pg << 1;
        int rc;
 
-       DPRINTF(("free ov page %"Y"u (%d)", pg, ovpages));
+       DPRINTF(("free ov page %"Yu" (%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.
         * Otherwise put it onto the list of pages we freed in this txn.
@@ -6263,7 +6283,7 @@ mdb_ovpage_free(MDB_cursor *mc, MDB_page *mp)
                                j = ++(dl[0].mid);
                                dl[j] = ix;             /* Unsorted. OK when MDB_TXN_ERROR. */
                                txn->mt_flags |= MDB_TXN_ERROR;
-                               return MDB_CORRUPTED;
+                               return MDB_PROBLEM;
                        }
                }
                txn->mt_dirty_room++;
@@ -6300,12 +6320,10 @@ mdb_node_read(MDB_cursor *mc, MDB_node *leaf, MDB_val *data)
        pgno_t           pgno;
        int rc;
 
-#ifdef MDB_VL32
-       if (mc->mc_ovpg) {
-               MDB_PAGE_UNREF(mc->mc_txn, mc->mc_ovpg);
-               mc->mc_ovpg = 0;
+       if (MC_OVPG(mc)) {
+               MDB_PAGE_UNREF(mc->mc_txn, MC_OVPG(mc));
+               MC_SET_OVPG(mc, NULL);
        }
-#endif
        if (!F_ISSET(leaf->mn_flags, F_BIGDATA)) {
                data->mv_size = NODEDSZ(leaf);
                data->mv_data = NODEDATA(leaf);
@@ -6317,13 +6335,11 @@ mdb_node_read(MDB_cursor *mc, MDB_node *leaf, MDB_val *data)
        data->mv_size = NODEDSZ(leaf);
        memcpy(&pgno, NODEDATA(leaf), sizeof(pgno));
        if ((rc = mdb_page_get(mc, pgno, &omp, NULL)) != 0) {
-               DPRINTF(("read overflow page %"Y"u failed", pgno));
+               DPRINTF(("read overflow page %"Yu" failed", pgno));
                return rc;
        }
        data->mv_data = METADATA(omp);
-#ifdef MDB_VL32
-       mc->mc_ovpg = omp;
-#endif
+       MC_SET_OVPG(mc, omp);
 
        return MDB_SUCCESS;
 }
@@ -6347,14 +6363,10 @@ mdb_get(MDB_txn *txn, MDB_dbi dbi,
 
        mdb_cursor_init(&mc, txn, dbi, &mx);
        rc = mdb_cursor_set(&mc, key, data, MDB_SET, &exact);
-#ifdef MDB_VL32
-       {
-               /* unref all the pages - caller must copy the data
-                * before doing anything else
-                */
-               mdb_cursor_unref(&mc);
-       }
-#endif
+       /* unref all the pages when MDB_VL32 - caller must copy the data
+        * before doing anything else
+        */
+       MDB_CURSOR_UNREF(&mc, 1);
        return rc;
 }
 
@@ -6384,7 +6396,7 @@ mdb_cursor_sibling(MDB_cursor *mc, int move_right)
        op = mc->mc_pg[mc->mc_top];
 #endif
        mdb_cursor_pop(mc);
-       DPRINTF(("parent page is page %"Y"u, index %u",
+       DPRINTF(("parent page is page %"Yu", 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]))
@@ -6451,13 +6463,9 @@ mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                                        return rc;
                                }
                        }
-#ifdef MDB_VL32
                        else {
-                               if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
-                                       mdb_cursor_unref(&mc->mc_xcursor->mx_cursor);
-                               }
+                               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
                        }
-#endif
                } else {
                        mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
                        if (op == MDB_NEXT_DUP)
@@ -6465,7 +6473,7 @@ mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                }
        }
 
-       DPRINTF(("cursor_next: top page is %"Y"u in cursor %p",
+       DPRINTF(("cursor_next: top page is %"Yu" in cursor %p",
                mdb_dbg_pgno(mp), (void *) mc));
        if (mc->mc_flags & C_DEL) {
                mc->mc_flags ^= C_DEL;
@@ -6479,12 +6487,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 %"Y"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
+               DPRINTF(("next page is %"Yu", key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
        } else
                mc->mc_ki[mc->mc_top]++;
 
 skip:
-       DPRINTF(("==> cursor points to page %"Y"u with %u keys, key index %u",
+       DPRINTF(("==> cursor points to page %"Yu" with %u keys, key index %u",
            mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
 
        if (IS_LEAF2(mp)) {
@@ -6544,13 +6552,9 @@ mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                                        return rc;
                                }
                        }
-#ifdef MDB_VL32
                        else {
-                               if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
-                                       mdb_cursor_unref(&mc->mc_xcursor->mx_cursor);
-                               }
+                               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
                        }
-#endif
                } else {
                        mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
                        if (op == MDB_PREV_DUP)
@@ -6558,7 +6562,7 @@ mdb_cursor_prev(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
                }
        }
 
-       DPRINTF(("cursor_prev: top page is %"Y"u in cursor %p",
+       DPRINTF(("cursor_prev: top page is %"Yu" in cursor %p",
                mdb_dbg_pgno(mp), (void *) mc));
 
        mc->mc_flags &= ~(C_EOF|C_DEL);
@@ -6570,13 +6574,13 @@ 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 %"Y"u, key index %u", mp->mp_pgno, mc->mc_ki[mc->mc_top]));
+               DPRINTF(("prev page is %"Yu", 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 %"Y"u with %u keys, key index %u",
+       DPRINTF(("==> cursor points to page %"Yu" with %u keys, key index %u",
            mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
 
        if (IS_LEAF2(mp)) {
@@ -6619,8 +6623,10 @@ mdb_cursor_set(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        if (key->mv_size == 0)
                return MDB_BAD_VALSIZE;
 
-       if (mc->mc_xcursor)
+       if (mc->mc_xcursor) {
+               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
+       }
 
        /* See if we're already on the right page */
        if (mc->mc_flags & C_INITIALIZED) {
@@ -6752,11 +6758,6 @@ set1:
                return MDB_SUCCESS;
        }
 
-#ifdef MDB_VL32
-       if (mc->mc_xcursor && mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
-               mdb_cursor_unref(&mc->mc_xcursor->mx_cursor);
-       }
-#endif
        if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                mdb_xcursor_init1(mc, leaf);
        }
@@ -6782,10 +6783,8 @@ set1:
                        if ((rc = mdb_node_read(mc, leaf, &olddata)) != MDB_SUCCESS)
                                return rc;
                        dcmp = mc->mc_dbx->md_dcmp;
-#if UINT_MAX < SIZE_MAX || defined(MDB_VL32)
-                       if (dcmp == mdb_cmp_int && olddata.mv_size == sizeof(mdb_size_t))
+                       if (NEED_CMP_CLONG(dcmp, olddata.mv_size))
                                dcmp = mdb_cmp_clong;
-#endif
                        rc = dcmp(data, &olddata);
                        if (rc) {
                                if (op == MDB_GET_BOTH || rc > 0)
@@ -6818,11 +6817,7 @@ mdb_cursor_first(MDB_cursor *mc, MDB_val *key, MDB_val *data)
        MDB_node        *leaf;
 
        if (mc->mc_xcursor) {
-#ifdef MDB_VL32
-               if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
-                       mdb_cursor_unref(&mc->mc_xcursor->mx_cursor);
-               }
-#endif
+               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
        }
 
@@ -6868,11 +6863,7 @@ mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
        MDB_node        *leaf;
 
        if (mc->mc_xcursor) {
-#ifdef MDB_VL32
-               if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
-                       mdb_cursor_unref(&mc->mc_xcursor->mx_cursor);
-               }
-#endif
+               MDB_CURSOR_UNREF(&mc->mc_xcursor->mx_cursor, 0);
                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
        }
 
@@ -7328,10 +7319,8 @@ more:
                                if (flags == MDB_CURRENT)
                                        goto current;
                                dcmp = mc->mc_dbx->md_dcmp;
-#if UINT_MAX < SIZE_MAX || defined(MDB_VL32)
-                               if (dcmp == mdb_cmp_int && olddata.mv_size == sizeof(mdb_size_t))
+                               if (NEED_CMP_CLONG(dcmp, olddata.mv_size))
                                        dcmp = mdb_cmp_clong;
-#endif
                                /* does data match? */
                                if (!dcmp(data, &olddata)) {
                                        if (flags & (MDB_NODUPDATA|MDB_APPENDDUP))
@@ -7656,7 +7645,7 @@ put_sub:
                return rc;
 bad_sub:
                if (rc == MDB_KEYEXIST) /* should not happen, we deleted that item */
-                       rc = MDB_CORRUPTED;
+                       rc = MDB_PROBLEM;
        }
        mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
        return rc;
@@ -7785,7 +7774,7 @@ 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 %"Y"u, page size %u",
+       DPRINTF(("allocated new mpage %"Yu", page size %u",
            np->mp_pgno, mc->mc_txn->mt_env->me_psize));
        np->mp_flags = flags | P_DIRTY;
        np->mp_lower = (PAGEHDRSZ-PAGEBASE);
@@ -7885,7 +7874,7 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
 
        mdb_cassert(mc, mp->mp_upper >= mp->mp_lower);
 
-       DPRINTF(("add to %s %spage %"Y"u index %i, data size %"Z"u key size %"Z"u [%s]",
+       DPRINTF(("add to %s %spage %"Yu" index %i, data size %"Z"u key size %"Z"u [%s]",
            IS_LEAF(mp) ? "leaf" : "branch",
                IS_SUBP(mp) ? "sub-" : "",
                mdb_dbg_pgno(mp), indx, data ? data->mv_size : 0,
@@ -7926,7 +7915,7 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
                                goto full;
                        if ((rc = mdb_page_new(mc, P_OVERFLOW, ovpages, &ofp)))
                                return rc;
-                       DPRINTF(("allocated overflow page %"Y"u", ofp->mp_pgno));
+                       DPRINTF(("allocated overflow page %"Yu, ofp->mp_pgno));
                        flags |= F_BIGDATA;
                        goto update;
                } else {
@@ -7983,7 +7972,7 @@ update:
        return MDB_SUCCESS;
 
 full:
-       DPRINTF(("not enough room in page %"Y"u, got %u ptrs",
+       DPRINTF(("not enough room in page %"Yu", got %u ptrs",
                mdb_dbg_pgno(mp), NUMKEYS(mp)));
        DPRINTF(("upper-lower = %u - %u = %"Z"d", mp->mp_upper,mp->mp_lower,room));
        DPRINTF(("node size = %"Z"u", node_size));
@@ -8006,7 +7995,7 @@ mdb_node_del(MDB_cursor *mc, int ksize)
        MDB_node        *node;
        char            *base;
 
-       DPRINTF(("delete node %u on %s page %"Y"u", indx,
+       DPRINTF(("delete node %u on %s page %"Yu, indx,
            IS_LEAF(mp) ? "leaf" : "branch", mdb_dbg_pgno(mp)));
        numkeys = NUMKEYS(mp);
        mdb_cassert(mc, indx < numkeys);
@@ -8115,9 +8104,7 @@ mdb_xcursor_init0(MDB_cursor *mc)
        mx->mx_cursor.mc_dbflag = &mx->mx_dbflag;
        mx->mx_cursor.mc_snum = 0;
        mx->mx_cursor.mc_top = 0;
-#ifdef MDB_VL32
-       mx->mx_cursor.mc_ovpg = 0;
-#endif
+       MC_SET_OVPG(&mx->mx_cursor, NULL);
        mx->mx_cursor.mc_flags = C_SUB | (mc->mc_flags & (C_ORIG_RDONLY|C_WRITEMAP));
        mx->mx_dbx.md_name.mv_size = 0;
        mx->mx_dbx.md_name.mv_data = NULL;
@@ -8165,13 +8152,11 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
                                mx->mx_db.md_flags |= MDB_INTEGERKEY;
                }
        }
-       DPRINTF(("Sub-db -%u root page %"Y"u", mx->mx_cursor.mc_dbi,
+       DPRINTF(("Sub-db -%u root page %"Yu, 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 */
-#if UINT_MAX < SIZE_MAX || defined(MDB_VL32)
-       if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(mdb_size_t))
+       if (NEED_CMP_CLONG(mx->mx_dbx.md_cmp, mx->mx_db.md_pad))
                mx->mx_dbx.md_cmp = mdb_cmp_clong;
-#endif
 }
 
 
@@ -8194,7 +8179,7 @@ mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
                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 */
-#if UINT_MAX < SIZE_MAX
+#if UINT_MAX < MDB_SIZE_MAX    /* matches mdb_xcursor_init1:NEED_CMP_CLONG() */
                mx->mx_dbx.md_cmp = src_mx->mx_dbx.md_cmp;
 #endif
        } else if (!(mx->mx_cursor.mc_flags & C_INITIALIZED)) {
@@ -8202,7 +8187,7 @@ mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
        }
        mx->mx_db = src_mx->mx_db;
        mx->mx_cursor.mc_pg[0] = src_mx->mx_cursor.mc_pg[0];
-       DPRINTF(("Sub-db -%u root page %"Y"u", mx->mx_cursor.mc_dbi,
+       DPRINTF(("Sub-db -%u root page %"Yu, mx->mx_cursor.mc_dbi,
                mx->mx_db.md_root));
 }
 
@@ -8221,9 +8206,7 @@ mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
        mc->mc_top = 0;
        mc->mc_pg[0] = 0;
        mc->mc_ki[0] = 0;
-#ifdef MDB_VL32
-       mc->mc_ovpg = 0;
-#endif
+       MC_SET_OVPG(mc, NULL);
        mc->mc_flags = txn->mt_flags & (C_ORIG_RDONLY|C_WRITEMAP);
        if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
                mdb_tassert(txn, mx != NULL);
@@ -8323,6 +8306,9 @@ mdb_cursor_count(MDB_cursor *mc, mdb_size_t *countp)
 void
 mdb_cursor_close(MDB_cursor *mc)
 {
+       if (mc) {
+               MDB_CURSOR_UNREF(mc, 0);
+       }
        if (mc && !mc->mc_backup) {
                /* remove from txn, if tracked */
                if ((mc->mc_flags & C_UNTRACK) && mc->mc_txn->mt_cursors) {
@@ -8374,7 +8360,7 @@ mdb_update_key(MDB_cursor *mc, MDB_val *key)
                char kbuf2[DKBUF_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 %"Y"u",
+               DPRINTF(("update key %u (ofs %u) [%s] to [%s] on page %"Yu,
                        indx, ptr,
                        mdb_dkey(&k2, kbuf2),
                        DKEY(key),
@@ -8522,7 +8508,7 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
                        return rc;
        }
 
-       DPRINTF(("moving %s node %u [%s] on page %"Y"u to node %u on page %"Y"u",
+       DPRINTF(("moving %s node %u [%s] on page %"Yu" to node %u on page %"Yu,
            IS_LEAF(csrc->mc_pg[csrc->mc_top]) ? "leaf" : "branch",
            csrc->mc_ki[csrc->mc_top],
                DKEY(&key),
@@ -8616,7 +8602,7 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
                                key.mv_size = NODEKSZ(srcnode);
                                key.mv_data = NODEKEY(srcnode);
                        }
-                       DPRINTF(("update separator for source page %"Y"u to [%s]",
+                       DPRINTF(("update separator for source page %"Yu" to [%s]",
                                csrc->mc_pg[csrc->mc_top]->mp_pgno, DKEY(&key)));
                        mdb_cursor_copy(csrc, &mn);
                        mn.mc_snum--;
@@ -8647,7 +8633,7 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst, int fromleft)
                                key.mv_size = NODEKSZ(srcnode);
                                key.mv_data = NODEKEY(srcnode);
                        }
-                       DPRINTF(("update separator for destination page %"Y"u to [%s]",
+                       DPRINTF(("update separator for destination page %"Yu" to [%s]",
                                cdst->mc_pg[cdst->mc_top]->mp_pgno, DKEY(&key)));
                        mdb_cursor_copy(cdst, &mn);
                        mn.mc_snum--;
@@ -8693,7 +8679,7 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
        psrc = csrc->mc_pg[csrc->mc_top];
        pdst = cdst->mc_pg[cdst->mc_top];
 
-       DPRINTF(("merging page %"Y"u into %"Y"u", psrc->mp_pgno, pdst->mp_pgno));
+       DPRINTF(("merging page %"Yu" into %"Yu, psrc->mp_pgno, pdst->mp_pgno));
 
        mdb_cassert(csrc, csrc->mc_snum > 1);   /* can't merge root page */
        mdb_cassert(csrc, cdst->mc_snum > 1);
@@ -8750,7 +8736,7 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                }
        }
 
-       DPRINTF(("dst page %"Y"u now has %u keys (%.1f%% filled)",
+       DPRINTF(("dst page %"Yu" now has %u keys (%.1f%% filled)",
            pdst->mp_pgno, NUMKEYS(pdst),
                (float)PAGEFILL(cdst->mc_txn->mt_env, pdst) / 10));
 
@@ -8838,9 +8824,7 @@ mdb_cursor_copy(const MDB_cursor *csrc, MDB_cursor *cdst)
        cdst->mc_snum = csrc->mc_snum;
        cdst->mc_top = csrc->mc_top;
        cdst->mc_flags = csrc->mc_flags;
-#ifdef MDB_VL32
-       cdst->mc_ovpg = csrc->mc_ovpg;
-#endif
+       MC_SET_OVPG(cdst, MC_OVPG(csrc));
 
        for (i=0; i<csrc->mc_snum; i++) {
                cdst->mc_pg[i] = csrc->mc_pg[i];
@@ -8869,14 +8853,14 @@ mdb_rebalance(MDB_cursor *mc)
                minkeys = 1;
                thresh = FILL_THRESHOLD;
        }
-       DPRINTF(("rebalancing %s page %"Y"u (has %u keys, %.1f%% full)",
+       DPRINTF(("rebalancing %s page %"Yu" (has %u keys, %.1f%% full)",
            IS_LEAF(mc->mc_pg[mc->mc_top]) ? "leaf" : "branch",
            mdb_dbg_pgno(mc->mc_pg[mc->mc_top]), NUMKEYS(mc->mc_pg[mc->mc_top]),
                (float)PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) / 10));
 
        if (PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) >= thresh &&
                NUMKEYS(mc->mc_pg[mc->mc_top]) >= minkeys) {
-               DPRINTF(("no need to rebalance page %"Y"u, above fill threshold",
+               DPRINTF(("no need to rebalance page %"Yu", above fill threshold",
                    mdb_dbg_pgno(mc->mc_pg[mc->mc_top])));
                return MDB_SUCCESS;
        }
@@ -9005,7 +8989,7 @@ mdb_rebalance(MDB_cursor *mc)
                fromleft = 1;
        }
 
-       DPRINTF(("found neighbor page %"Y"u (%u keys, %.1f%% full)",
+       DPRINTF(("found neighbor page %"Yu" (%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));
 
@@ -9215,7 +9199,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
        newindx = mc->mc_ki[mc->mc_top];
        nkeys = NUMKEYS(mp);
 
-       DPRINTF(("-----> splitting %s page %"Y"u and adding [%s] at index %i/%i",
+       DPRINTF(("-----> splitting %s page %"Yu" and adding [%s] at index %i/%i",
            IS_LEAF(mp) ? "leaf" : "branch", mp->mp_pgno,
            DKEY(newkey), mc->mc_ki[mc->mc_top], nkeys));
 
@@ -9223,7 +9207,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
        if ((rc = mdb_page_new(mc, mp->mp_flags, 1, &rp)))
                return rc;
        rp->mp_pad = mp->mp_pad;
-       DPRINTF(("new right sibling: page %"Y"u", rp->mp_pgno));
+       DPRINTF(("new right sibling: page %"Yu, rp->mp_pgno));
 
        /* Usually when splitting the root page, the cursor
         * height is 1. But when called from mdb_update_key,
@@ -9241,7 +9225,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 = %"Y"u", pp->mp_pgno));
+               DPRINTF(("root split! new root = %"Yu, pp->mp_pgno));
                new_root = mc->mc_db->md_depth++;
 
                /* Add left (implicit) pointer. */
@@ -9258,7 +9242,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 %"Y"u", mc->mc_pg[ptop]->mp_pgno));
+               DPRINTF(("parent branch page is %"Yu, mc->mc_pg[ptop]->mp_pgno));
        }
 
        mdb_cursor_copy(mc, &mn);
@@ -9906,6 +9890,7 @@ mdb_env_copyfd1(MDB_env *env, HANDLE fd)
        mdb_copy my = {0};
        MDB_txn *txn = NULL;
        pthread_t thr;
+       pgno_t root, new_root;
        int rc = MDB_SUCCESS;
 
 #ifdef _WIN32
@@ -9921,9 +9906,10 @@ mdb_env_copyfd1(MDB_env *env, HANDLE fd)
                goto done;
        }
 #else
-       if ((rc = pthread_mutex_init(&my.mc_mutex, NULL)) ||
-               (rc = 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) {
@@ -9931,9 +9917,12 @@ mdb_env_copyfd1(MDB_env *env, HANDLE fd)
                goto done;
        }
 #else
-       rc = posix_memalign((void **)&my.mc_wbuf[0], env->me_os_psize, MDB_WBUF*2);
-       if (rc)
-               goto done;
+       {
+               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);
@@ -9963,10 +9952,12 @@ 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;
@@ -9979,19 +9970,26 @@ mdb_env_copyfd1(MDB_env *env, HANDLE fd)
                        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;
-       rc = mdb_env_cwalk(&my, &txn->mt_dbs[MAIN_DBI].md_root, 0);
+       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)
@@ -10002,13 +10000,14 @@ finish:
 
 done:
 #ifdef _WIN32
+       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);
-       _aligned_free(my.mc_wbuf[0]);
 #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 ? rc : my.mc_error;
 }
@@ -10598,10 +10597,8 @@ pop:
 done:
                if (rc)
                        txn->mt_flags |= MDB_TXN_ERROR;
-#ifdef MDB_VL32
                /* drop refcount for mx's pages */
-               mdb_cursor_unref(&mx);
-#endif
+               MDB_CURSOR_UNREF(&mx, 0);
        } else if (rc == MDB_NOTFOUND) {
                rc = MDB_SUCCESS;
        }
@@ -10721,7 +10718,7 @@ mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
                if (mr[i].mr_pid) {
                        txnid_t txnid = mr[i].mr_txnid;
                        sprintf(buf, txnid == (txnid_t)-1 ?
-                               "%10d %"Z"x -\n" : "%10d %"Z"x %"Y"u\n",
+                               "%10d %"Z"x -\n" : "%10d %"Z"x %"Yu"\n",
                                (int)mr[i].mr_pid, (size_t)mr[i].mr_tid, txnid);
                        if (first) {
                                first = 0;
@@ -10826,7 +10823,7 @@ mdb_reader_check0(MDB_env *env, int rlocked, int *dead)
                                        }
                                        for (; j<rdrs; j++)
                                                        if (mr[j].mr_pid == pid) {
-                                                               DPRINTF(("clear stale reader pid %u txn %"Y"d",
+                                                               DPRINTF(("clear stale reader pid %u txn %"Yd,
                                                                        (unsigned) pid, mr[j].mr_txnid));
                                                                mr[j].mr_pid = 0;
                                                                count++;