]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
Fix prev commit
[openldap] / libraries / liblmdb / mdb.c
index 1d361a7bb12560e736034f8b04d22fe3f048c65a..4109e71c3481424a3b72b6bbb3fc8effdd42b96f 100644 (file)
@@ -59,6 +59,7 @@
 #include <unistd.h>
 
 #if !(defined(BYTE_ORDER) || defined(__BYTE_ORDER))
+#include <netinet/in.h>
 #include <resolv.h>    /* defines BYTE_ORDER on HPUX and Solaris */
 #endif
 
 #define BIG_ENDIAN     __BIG_ENDIAN
 #endif
 
-#if defined(__i386) || defined(__x86_64)
+#if defined(__i386) || defined(__x86_64) || defined(_M_IX86)
 #define MISALIGNED_OK  1
 #endif
 
@@ -198,12 +199,6 @@ mdb_sem_wait(sem_t *sem)
         */
 #define        ErrCode()       errno
 
-       /** An abstraction for a file handle.
-        *      On POSIX systems file handles are small integers. On Windows
-        *      they're opaque pointers.
-        */
-#define        HANDLE  int
-
        /**     A value for an invalid file handle.
         *      Mainly used to initialize file variables and signify that they are
         *      unused.
@@ -286,6 +281,8 @@ typedef MDB_ID      txnid_t;
 #endif
 
 #if !(__STDC_VERSION__ >= 199901L || defined(__GNUC__))
+# undef  MDB_DEBUG
+# define MDB_DEBUG     0
 # define DPRINTF       (void)  /* Vararg macros may be unsupported */
 #elif MDB_DEBUG
 static int mdb_debug;
@@ -404,6 +401,8 @@ typedef uint16_t     indx_t;
  *     slot's address is saved in thread-specific data so that subsequent read
  *     transactions started by the same thread need no further locking to proceed.
  *
+ *     If #MDB_NOTLS is set, the slot address is not saved in thread-specific data.
+ *
  *     No reader table is used if the database is on a read-only filesystem.
  *
  *     Since the database uses multi-version concurrency control, readers don't
@@ -743,6 +742,12 @@ 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))
+#define VALID_FLAGS    (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
+       MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
+
        /** Handle for the DB used to track free pages. */
 #define        FREE_DBI        0
        /** Handle for the default DB. */
@@ -822,6 +827,8 @@ struct MDB_txn {
  */
 #define DB_DIRTY       0x01            /**< DB was written in this txn */
 #define DB_STALE       0x02            /**< DB record is older than txnID */
+#define DB_NEW         0x04            /**< DB handle opened in this txn */
+#define DB_VALID       0x08            /**< DB handle is valid, see also #MDB_VALID */
 /** @} */
        /** In write txns, array of cursors for each DB */
        MDB_cursor      **mt_cursors;
@@ -841,7 +848,7 @@ struct MDB_txn {
 #define MDB_TXN_DIRTY          0x04            /**< must write, even if dirty list is empty */
 /** @} */
        unsigned int    mt_flags;               /**< @ref mdb_txn */
-       /** dirty_list maxsize - #allocated pages including in parent txns */
+       /** dirty_list maxsize - # of allocated pages allowed, including in parent txns */
        unsigned int    mt_dirty_room;
        /** Tracks which of the two meta pages was used at the start
         *      of this transaction.
@@ -888,6 +895,7 @@ struct MDB_cursor {
 #define C_SHADOW       0x08            /**< Cursor is a dup from a parent txn */
 #define C_ALLOCD       0x10            /**< Cursor was malloc'd */
 #define C_SPLITTING    0x20            /**< Cursor is in page_split */
+#define C_UNTRACK      0x40            /**< Un-track cursor when closing */
 /** @} */
        unsigned int    mc_flags;       /**< @ref mdb_cursor */
        MDB_page        *mc_pg[CURSOR_STACK];   /**< stack of pushed pages */
@@ -924,10 +932,10 @@ struct MDB_env {
        HANDLE          me_mfd;                 /**< just for writing the meta pages */
        /** Failed to update the meta page. Probably an I/O error. */
 #define        MDB_FATAL_ERROR 0x80000000U
-       /** Read-only Filesystem. Allow read access, no locking. */
-#define        MDB_ROFS        0x40000000U
        /** Some fields are initialized. */
 #define        MDB_ENV_ACTIVE  0x20000000U
+       /** me_txkey is set */
+#define        MDB_ENV_TXKEY   0x10000000U
        uint32_t        me_flags;               /**< @ref mdb_env */
        unsigned int    me_psize;       /**< size of a page, from #GET_PAGESIZE */
        unsigned int    me_maxreaders;  /**< size of the reader table */
@@ -953,10 +961,12 @@ struct MDB_env {
        MDB_page        *me_dpages;             /**< list of malloc'd blocks for re-use */
        /** IDL of pages that became unused in a write txn */
        MDB_IDL         me_free_pgs;
-       /** ID2L of pages that were written during a write txn */
-       MDB_ID2         me_dirty_list[MDB_IDL_UM_SIZE];
+       /** ID2L of pages written during a write txn. Length MDB_IDL_UM_SIZE. */
+       MDB_ID2L        me_dirty_list;
        /** Max number of freelist items that can fit in a single overflow page */
        unsigned int    me_maxfree_1pg;
+       /** Max size of a node on a page */
+       unsigned int    me_nodemax;
 #ifdef _WIN32
        HANDLE          me_rmutex;              /* Windows mutexes don't reside in shared mem */
        HANDLE          me_wmutex;
@@ -983,7 +993,7 @@ static int  mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp);
 static int  mdb_page_new(MDB_cursor *mc, uint32_t flags, int num, MDB_page **mp);
 static int  mdb_page_touch(MDB_cursor *mc);
 
-static int  mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **mp);
+static int  mdb_page_get(MDB_txn *txn, 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
@@ -999,6 +1009,9 @@ static int mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata,
 static int  mdb_env_read_header(MDB_env *env, MDB_meta *meta);
 static int  mdb_env_pick_meta(const MDB_env *env);
 static int  mdb_env_write_meta(MDB_txn *txn);
+#if !(defined(_WIN32) || defined(MDB_USE_POSIX_SEM)) /* Drop unused excl arg */
+# define mdb_env_close0(env, excl) mdb_env_close1(env)
+#endif
 static void mdb_env_close0(MDB_env *env, int excl);
 
 static MDB_node *mdb_node_search(MDB_cursor *mc, MDB_val *key, int *exactp);
@@ -1071,6 +1084,7 @@ static char *const mdb_errstr[] = {
        "MDB_PAGE_FULL: Internal error - page has no more space",
        "MDB_MAP_RESIZED: Database contents grew beyond environment mapsize",
        "MDB_INCOMPATIBLE: Database flags changed or would change",
+       "MDB_BAD_RSLOT: Invalid reuse of reader locktable slot",
 };
 
 char *
@@ -1120,7 +1134,7 @@ mdb_dkey(MDB_val *key, char *buf)
 }
 
 /** Display all the keys in the page. */
-static void
+void
 mdb_page_list(MDB_page *mp)
 {
        MDB_node *node;
@@ -1187,9 +1201,8 @@ static void mdb_audit(MDB_txn *txn)
 
        count = 0;
        for (i = 0; i<txn->mt_numdbs; i++) {
-               MDB_xcursor mx, *mxp;
-               mxp = (txn->mt_dbs[i].md_flags & MDB_DUPSORT) ? &mx : NULL;
-               mdb_cursor_init(&mc, txn, i, mxp);
+               MDB_xcursor mx;
+               mdb_cursor_init(&mc, txn, i, &mx);
                if (txn->mt_dbs[i].md_root == P_INVALID)
                        continue;
                count += txn->mt_dbs[i].md_branch_pages +
@@ -1230,29 +1243,38 @@ mdb_cmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
 int
 mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
 {
-       if (txn->mt_dbxs[dbi].md_dcmp)
-               return txn->mt_dbxs[dbi].md_dcmp(a, b);
-       else
-               return EINVAL;  /* too bad you can't distinguish this from a valid result */
+       return txn->mt_dbxs[dbi].md_dcmp(a, b);
 }
 
-/** Allocate a single page.
- * Re-use old malloc'd pages first, otherwise just malloc.
+/** Allocate a page.
+ * Re-use old malloc'd pages first for singletons, otherwise just malloc.
  */
 static MDB_page *
-mdb_page_malloc(MDB_cursor *mc) {
-       MDB_page *ret;
-       size_t sz = mc->mc_txn->mt_env->me_psize;
-       if ((ret = mc->mc_txn->mt_env->me_dpages) != NULL) {
-               VGMEMP_ALLOC(mc->mc_txn->mt_env, ret, sz);
-               VGMEMP_DEFINED(ret, sizeof(ret->mp_next));
-               mc->mc_txn->mt_env->me_dpages = ret->mp_next;
-       } else if ((ret = malloc(sz)) != NULL) {
-               VGMEMP_ALLOC(mc->mc_txn->mt_env, ret, sz);
+mdb_page_malloc(MDB_cursor *mc, unsigned num)
+{
+       MDB_env *env = mc->mc_txn->mt_env;
+       MDB_page *ret = env->me_dpages;
+       size_t sz = env->me_psize;
+       if (num == 1) {
+               if (ret) {
+                       VGMEMP_ALLOC(env, ret, sz);
+                       VGMEMP_DEFINED(ret, sizeof(ret->mp_next));
+                       env->me_dpages = ret->mp_next;
+                       return ret;
+               }
+       } else {
+               sz *= num;
+       }
+       if ((ret = malloc(sz)) != NULL) {
+               VGMEMP_ALLOC(env, ret, sz);
        }
        return ret;
 }
 
+/** Free a single page.
+ * Saves single pages to a list, for future reuse.
+ * (This is not used for multi-page overflow pages.)
+ */
 static void
 mdb_page_free(MDB_env *env, MDB_page *mp)
 {
@@ -1261,6 +1283,44 @@ mdb_page_free(MDB_env *env, MDB_page *mp)
        env->me_dpages = mp;
 }
 
+/* Return all dirty pages to dpage list */
+static void
+mdb_dlist_free(MDB_txn *txn)
+{
+       MDB_env *env = txn->mt_env;
+       MDB_ID2L dl = txn->mt_u.dirty_list;
+       unsigned i, n = dl[0].mid;
+
+       for (i = 1; i <= n; i++) {
+               MDB_page *dp = dl[i].mptr;
+               if (!IS_OVERFLOW(dp) || dp->mp_pages == 1) {
+                       mdb_page_free(env, dp);
+               } else {
+                       /* large pages just get freed directly */
+                       VGMEMP_FREE(env, dp);
+                       free(dp);
+               }
+       }
+       dl[0].mid = 0;
+}
+
+/** Find oldest txnid still referenced. Expects txn->mt_txnid > 0. */
+static txnid_t
+mdb_find_oldest(MDB_txn *txn)
+{
+       int i;
+       txnid_t mr, oldest = txn->mt_txnid - 1;
+       MDB_reader *r = txn->mt_env->me_txns->mti_readers;
+       for (i = txn->mt_env->me_txns->mti_numreaders; --i >= 0; ) {
+               if (r[i].mr_pid) {
+                       mr = r[i].mr_txnid;
+                       if (oldest > mr)
+                               oldest = mr;
+               }
+       }
+       return oldest;
+}
+
 /** Allocate pages for writing.
  * If there are free pages available from older transactions, they
  * will be re-used first. Otherwise a new page will be allocated.
@@ -1295,7 +1355,6 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                if (!txn->mt_env->me_pghead &&
                        txn->mt_dbs[FREE_DBI].md_root != P_INVALID) {
                        /* See if there's anything in the free DB */
-                       MDB_reader *r;
                        MDB_cursor m2;
                        MDB_node *leaf;
                        MDB_val data;
@@ -1320,19 +1379,8 @@ again:
                                last = *(txnid_t *)key.mv_data;
                        }
 
-                       {
-                               unsigned int i, nr;
-                               txnid_t mr;
-                               oldest = txn->mt_txnid - 1;
-                               nr = txn->mt_env->me_txns->mti_numreaders;
-                               r = txn->mt_env->me_txns->mti_readers;
-                               for (i=0; i<nr; i++) {
-                                       if (!r[i].mr_pid) continue;
-                                       mr = r[i].mr_txnid;
-                                       if (mr < oldest)
-                                               oldest = mr;
-                               }
-                       }
+                       if (!oldest)
+                               oldest = mdb_find_oldest(txn);
 
                        if (oldest > last) {
                                /* It's usable, grab it.
@@ -1383,6 +1431,7 @@ none:
 
                                mdb_cursor_init(&m2, txn, FREE_DBI, NULL);
                                do {
+#ifdef MDB_PARANOID    /* Seems like we can ignore this now */
                                        /* If on freelist, don't try to read more. If what we have
                                         * right now isn't enough just use new pages.
                                         * TODO: get all of this working. Many circular dependencies...
@@ -1391,6 +1440,7 @@ none:
                                                retry = 0;
                                                readit = 0;
                                        }
+#endif
                                        if (readit) {
                                                MDB_val key, data;
                                                pgno_t *idl, *mop2;
@@ -1399,19 +1449,7 @@ none:
 
                                                /* We haven't hit the readers list yet? */
                                                if (!oldest) {
-                                                       MDB_reader *r;
-                                                       unsigned int nr;
-                                                       txnid_t mr;
-
-                                                       oldest = txn->mt_txnid - 1;
-                                                       nr = txn->mt_env->me_txns->mti_numreaders;
-                                                       r = txn->mt_env->me_txns->mti_readers;
-                                                       for (i=0; i<nr; i++) {
-                                                               if (!r[i].mr_pid) continue;
-                                                               mr = r[i].mr_txnid;
-                                                               if (mr < oldest)
-                                                                       oldest = mr;
-                                                       }
+                                                       oldest = mdb_find_oldest(txn);
                                                }
 
                                                /* There's nothing we can use on the freelist */
@@ -1498,17 +1536,8 @@ none:
                np = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
                np->mp_pgno = pgno;
        } else {
-               if (txn->mt_env->me_dpages && num == 1) {
-                       np = txn->mt_env->me_dpages;
-                       VGMEMP_ALLOC(txn->mt_env, np, txn->mt_env->me_psize);
-                       VGMEMP_DEFINED(np, sizeof(np->mp_next));
-                       txn->mt_env->me_dpages = np->mp_next;
-               } else {
-                       size_t sz = txn->mt_env->me_psize * num;
-                       if ((np = malloc(sz)) == NULL)
-                               return ENOMEM;
-                       VGMEMP_ALLOC(txn->mt_env, np, sz);
-               }
+               if (!(np = mdb_page_malloc(mc, num)))
+                       return ENOMEM;
                if (pgno == P_INVALID) {
                        np->mp_pgno = txn->mt_next_pgno;
                        txn->mt_next_pgno += num;
@@ -1532,6 +1561,7 @@ none:
 /** Copy a page: avoid copying unused portions of the page.
  * @param[in] dst page to copy into
  * @param[in] src page to copy from
+ * @param[in] psize size of a page
  */
 static void
 mdb_page_copy(MDB_page *dst, MDB_page *src, unsigned int psize)
@@ -1600,6 +1630,8 @@ finish:
                                if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
                                if (m2->mc_pg[mc->mc_top] == mc->mc_pg[mc->mc_top]) {
                                        m2->mc_pg[mc->mc_top] = mp;
+                                       if (mc->mc_db->md_flags & MDB_DUPSORT)
+                                               m2->mc_xcursor->mx_cursor.mc_flags &= ~C_INITIALIZED;
                                }
                        }
                }
@@ -1611,32 +1643,30 @@ finish:
                        SETPGNO(NODEPTR(mc->mc_pg[mc->mc_top-1], mc->mc_ki[mc->mc_top-1]), mp->mp_pgno);
                else
                        mc->mc_db->md_root = mp->mp_pgno;
-       } else if (mc->mc_txn->mt_parent) {
+       } else if (mc->mc_txn->mt_parent && !(mp->mp_flags & P_SUBP)) {
                MDB_page *np;
-               MDB_ID2 mid;
+               MDB_ID2 mid, *dl = mc->mc_txn->mt_u.dirty_list;
                /* If txn has a parent, make sure the page is in our
                 * dirty list.
                 */
-               if (mc->mc_txn->mt_u.dirty_list[0].mid) {
-                       unsigned x = mdb_mid2l_search(mc->mc_txn->mt_u.dirty_list, mp->mp_pgno);
-                       if (x <= mc->mc_txn->mt_u.dirty_list[0].mid &&
-                               mc->mc_txn->mt_u.dirty_list[x].mid == mp->mp_pgno) {
-                               if (mc->mc_txn->mt_u.dirty_list[x].mptr != mp) {
-                                       mp = mc->mc_txn->mt_u.dirty_list[x].mptr;
-                                       mc->mc_pg[mc->mc_top] = mp;
-                               }
+               if (dl[0].mid) {
+                       unsigned x = mdb_mid2l_search(dl, mp->mp_pgno);
+                       if (x <= dl[0].mid && dl[x].mid == mp->mp_pgno) {
+                               np = dl[x].mptr;
+                               if (mp != np)
+                                       mc->mc_pg[mc->mc_top] = np;
                                return 0;
                        }
                }
-               assert(mc->mc_txn->mt_u.dirty_list[0].mid < MDB_IDL_UM_MAX);
+               assert(dl[0].mid < MDB_IDL_UM_MAX);
                /* No - copy it */
-               np = mdb_page_malloc(mc);
+               np = mdb_page_malloc(mc, 1);
                if (!np)
                        return ENOMEM;
                memcpy(np, mp, mc->mc_txn->mt_env->me_psize);
                mid.mid = np->mp_pgno;
                mid.mptr = np;
-               mdb_mid2l_insert(mc->mc_txn->mt_u.dirty_list, &mid);
+               mdb_mid2l_insert(dl, &mid);
                mp = np;
                goto finish;
        }
@@ -1689,7 +1719,7 @@ mdb_cursor_shadow(MDB_txn *src, MDB_txn *dst)
                                mc->mc_dbflag = &dst->mt_dbflags[i];
                                mc->mc_snum = m2->mc_snum;
                                mc->mc_top = m2->mc_top;
-                               mc->mc_flags = m2->mc_flags | C_SHADOW;
+                               mc->mc_flags = m2->mc_flags | (C_SHADOW|C_ALLOCD);
                                for (j=0; j<mc->mc_snum; j++) {
                                        mc->mc_pg[j] = m2->mc_pg[j];
                                        mc->mc_ki[j] = m2->mc_ki[j];
@@ -1725,30 +1755,34 @@ mdb_cursor_shadow(MDB_txn *src, MDB_txn *dst)
        return MDB_SUCCESS;
 }
 
-/** Merge shadow cursors back into parent's */
+/** Close this write txn's cursors, after optionally merging its shadow
+ * cursors back into parent's.
+ * @param[in] txn the transaction handle.
+ * @param[in] merge 0 to not merge cursors, C_SHADOW to merge.
+ * @return 0 on success, non-zero on failure.
+ */
 static void
-mdb_cursor_merge(MDB_txn *txn)
+mdb_cursors_close(MDB_txn *txn, unsigned merge)
 {
-       MDB_dbi i;
-       for (i=0; i<txn->mt_numdbs; i++) {
-               if (txn->mt_cursors[i]) {
-                       MDB_cursor *mc;
-                       while ((mc = txn->mt_cursors[i])) {
-                               txn->mt_cursors[i] = mc->mc_next;
-                               if (mc->mc_flags & C_SHADOW) {
+       MDB_cursor **cursors = txn->mt_cursors, *mc, *next;
+       int i, j;
+
+       for (i = txn->mt_numdbs; --i >= 0; ) {
+               for (mc = cursors[i]; mc; mc = next) {
+                               next = mc->mc_next;
+                               if (mc->mc_flags & merge) {
                                        MDB_cursor *m2 = mc->mc_orig;
-                                       unsigned int j;
                                        m2->mc_snum = mc->mc_snum;
                                        m2->mc_top = mc->mc_top;
-                                       for (j=0; j<mc->mc_snum; j++) {
+                                       for (j = mc->mc_snum; --j >= 0; ) {
                                                m2->mc_pg[j] = mc->mc_pg[j];
                                                m2->mc_ki[j] = mc->mc_ki[j];
                                        }
                                }
                                if (mc->mc_flags & C_ALLOCD)
                                        free(mc);
-                       }
                }
+               cursors[i] = NULL;
        }
 }
 
@@ -1757,29 +1791,32 @@ mdb_txn_reset0(MDB_txn *txn);
 
 /** Common code for #mdb_txn_begin() and #mdb_txn_renew().
  * @param[in] txn the transaction handle to initialize
- * @return 0 on success, non-zero on failure. This can only
- * fail for read-only transactions, and then only if the
- * reader table is full.
+ * @return 0 on success, non-zero on failure.
  */
 static int
 mdb_txn_renew0(MDB_txn *txn)
 {
        MDB_env *env = txn->mt_env;
        unsigned int i;
-       int rc;
+       uint16_t x;
+       int rc, new_notls = 0;
 
        /* Setup db info */
        txn->mt_numdbs = env->me_numdbs;
        txn->mt_dbxs = env->me_dbxs;    /* mostly static anyway */
 
        if (txn->mt_flags & MDB_TXN_RDONLY) {
-               if (env->me_flags & MDB_ROFS) {
+               if (!env->me_txns) {
                        i = mdb_env_pick_meta(env);
                        txn->mt_txnid = env->me_metas[i]->mm_txnid;
                        txn->mt_u.reader = NULL;
                } else {
-                       MDB_reader *r = pthread_getspecific(env->me_txkey);
-                       if (!r) {
+                       MDB_reader *r = (env->me_flags & MDB_NOTLS) ? txn->mt_u.reader :
+                               pthread_getspecific(env->me_txkey);
+                       if (r) {
+                               if (r->mr_pid != env->me_pid || r->mr_txnid != (txnid_t)-1)
+                                       return MDB_BAD_RSLOT;
+                       } else {
                                pid_t pid = env->me_pid;
                                pthread_t tid = pthread_self();
 
@@ -1799,8 +1836,9 @@ mdb_txn_renew0(MDB_txn *txn)
                                env->me_numreaders = env->me_txns->mti_numreaders;
                                UNLOCK_MUTEX_R(env);
                                r = &env->me_txns->mti_readers[i];
-                               if ((rc = pthread_setspecific(env->me_txkey, r)) != 0) {
-                                       env->me_txns->mti_readers[i].mr_pid = 0;
+                               new_notls = (env->me_flags & MDB_NOTLS);
+                               if (!new_notls && (rc=pthread_setspecific(env->me_txkey, r))) {
+                                       r->mr_pid = 0;
                                        return rc;
                                }
                        }
@@ -1830,14 +1868,19 @@ mdb_txn_renew0(MDB_txn *txn)
 
        /* Copy the DB info and flags */
        memcpy(txn->mt_dbs, env->me_metas[txn->mt_toggle]->mm_dbs, 2 * sizeof(MDB_db));
-       for (i=2; i<txn->mt_numdbs; i++)
-               txn->mt_dbs[i].md_flags = env->me_dbflags[i];
-       txn->mt_dbflags[0] = txn->mt_dbflags[1] = 0;
-       if (txn->mt_numdbs > 2)
-               memset(txn->mt_dbflags+2, DB_STALE, txn->mt_numdbs-2);
+       for (i=2; i<txn->mt_numdbs; i++) {
+               x = env->me_dbflags[i];
+               txn->mt_dbs[i].md_flags = x & PERSISTENT_FLAGS;
+               txn->mt_dbflags[i] = (x & MDB_VALID) ? DB_VALID|DB_STALE : 0;
+       }
+       txn->mt_dbflags[0] = txn->mt_dbflags[1] = DB_VALID;
 
        if (env->me_maxpg < txn->mt_next_pgno) {
                mdb_txn_reset0(txn);
+               if (new_notls) {
+                       txn->mt_u.reader->mr_pid = 0;
+                       txn->mt_u.reader = NULL;
+               }
                return MDB_MAP_RESIZED;
        }
 
@@ -1849,7 +1892,7 @@ mdb_txn_renew(MDB_txn *txn)
 {
        int rc;
 
-       if (! (txn && (txn->mt_flags & MDB_TXN_RDONLY)))
+       if (!txn || txn->mt_dbxs)       /* A reset txn has mt_dbxs==NULL */
                return EINVAL;
 
        if (txn->mt_env->me_flags & MDB_FATAL_ERROR) {
@@ -1908,14 +1951,12 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
        txn->mt_env = env;
 
        if (parent) {
-               txn->mt_free_pgs = mdb_midl_alloc();
-               if (!txn->mt_free_pgs) {
-                       free(txn);
-                       return ENOMEM;
-               }
+               unsigned int i;
                txn->mt_u.dirty_list = malloc(sizeof(MDB_ID2)*MDB_IDL_UM_SIZE);
-               if (!txn->mt_u.dirty_list) {
-                       free(txn->mt_free_pgs);
+               if (!txn->mt_u.dirty_list ||
+                       !(txn->mt_free_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX)))
+               {
+                       free(txn->mt_u.dirty_list);
                        free(txn);
                        return ENOMEM;
                }
@@ -1930,7 +1971,9 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
                txn->mt_numdbs = parent->mt_numdbs;
                txn->mt_dbxs = parent->mt_dbxs;
                memcpy(txn->mt_dbs, parent->mt_dbs, txn->mt_numdbs * sizeof(MDB_db));
-               memcpy(txn->mt_dbflags, parent->mt_dbflags, txn->mt_numdbs);
+               /* Copy parent's mt_dbflags, but clear DB_NEW */
+               for (i=0; i<txn->mt_numdbs; i++)
+                       txn->mt_dbflags[i] = parent->mt_dbflags[i] & ~DB_NEW;
                rc = 0;
                ntxn = (MDB_ntxn *)txn;
                ntxn->mnt_pgstate = env->me_pgstate; /* save parent me_pghead & co */
@@ -1962,7 +2005,34 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
        return rc;
 }
 
+/** Export or close DBI handles opened in this txn. */
+static void
+mdb_dbis_update(MDB_txn *txn, int keep)
+{
+       int i;
+       MDB_dbi n = txn->mt_numdbs;
+       MDB_env *env = txn->mt_env;
+       unsigned char *tdbflags = txn->mt_dbflags;
+
+       for (i = n; --i >= 2;) {
+               if (tdbflags[i] & DB_NEW) {
+                       if (keep) {
+                               env->me_dbflags[i] = txn->mt_dbs[i].md_flags | MDB_VALID;
+                       } else {
+                               char *ptr = env->me_dbxs[i].md_name.mv_data;
+                               env->me_dbxs[i].md_name.mv_data = NULL;
+                               env->me_dbxs[i].md_name.mv_size = 0;
+                               env->me_dbflags[i] = 0;
+                               free(ptr);
+                       }
+               }
+       }
+       if (keep && env->me_numdbs < n)
+               env->me_numdbs = n;
+}
+
 /** Common code for #mdb_txn_reset() and #mdb_txn_abort().
+ * May be called twice for readonly txns: First reset it, then abort.
  * @param[in] txn the transaction handle to reset
  */
 static void
@@ -1970,39 +2040,23 @@ mdb_txn_reset0(MDB_txn *txn)
 {
        MDB_env *env = txn->mt_env;
 
+       /* Close any DBI handles opened in this txn */
+       mdb_dbis_update(txn, 0);
+
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
-               if (!(env->me_flags & MDB_ROFS))
+               if (txn->mt_u.reader) {
                        txn->mt_u.reader->mr_txnid = (txnid_t)-1;
-       } else {
-               MDB_page *dp;
-               unsigned int i;
-
-               /* close(free) all cursors */
-               for (i=0; i<txn->mt_numdbs; i++) {
-                       if (txn->mt_cursors[i]) {
-                               MDB_cursor *mc;
-                               while ((mc = txn->mt_cursors[i])) {
-                                       txn->mt_cursors[i] = mc->mc_next;
-                                       if (mc->mc_flags & C_ALLOCD)
-                                               free(mc);
-                               }
-                       }
+                       if (!(env->me_flags & MDB_NOTLS))
+                               txn->mt_u.reader = NULL; /* txn does not own reader */
                }
+               txn->mt_numdbs = 0;             /* close nothing if called again */
+               txn->mt_dbxs = NULL;    /* mark txn as reset */
+       } else {
+               mdb_cursors_close(txn, 0);
 
                if (!(env->me_flags & MDB_WRITEMAP)) {
-                       /* return all dirty pages to dpage list */
-                       for (i=1; i<=txn->mt_u.dirty_list[0].mid; i++) {
-                               dp = txn->mt_u.dirty_list[i].mptr;
-                               if (!IS_OVERFLOW(dp) || dp->mp_pages == 1) {
-                                       mdb_page_free(txn->mt_env, dp);
-                               } else {
-                                       /* large pages just get freed directly */
-                                       VGMEMP_FREE(txn->mt_env, dp);
-                                       free(dp);
-                               }
-                       }
+                       mdb_dlist_free(txn);
                }
-
                free(env->me_pgfree);
 
                if (txn->mt_parent) {
@@ -2035,6 +2089,10 @@ mdb_txn_reset(MDB_txn *txn)
                txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
                (void *) txn, (void *)txn->mt_env, txn->mt_dbs[MAIN_DBI].md_root);
 
+       /* This call is only valid for read-only txns */
+       if (!(txn->mt_flags & MDB_TXN_RDONLY))
+               return;
+
        mdb_txn_reset0(txn);
 }
 
@@ -2052,6 +2110,10 @@ mdb_txn_abort(MDB_txn *txn)
                mdb_txn_abort(txn->mt_child);
 
        mdb_txn_reset0(txn);
+       /* Free reader slot tied to this txn (if MDB_NOTLS && writable FS) */
+       if ((txn->mt_flags & MDB_TXN_RDONLY) && txn->mt_u.reader)
+               txn->mt_u.reader->mr_pid = 0;
+
        free(txn);
 }
 
@@ -2072,20 +2134,19 @@ mdb_txn_commit(MDB_txn *txn)
        assert(txn->mt_env != NULL);
 
        if (txn->mt_child) {
-               mdb_txn_commit(txn->mt_child);
+               rc = mdb_txn_commit(txn->mt_child);
                txn->mt_child = NULL;
+               if (rc) {
+                       mdb_txn_abort(txn);
+                       return rc;
+               }
        }
 
        env = txn->mt_env;
 
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
-               if (txn->mt_numdbs > env->me_numdbs) {
-                       /* update the DB flags */
-                       MDB_dbi i;
-                       for (i = env->me_numdbs; i<txn->mt_numdbs; i++)
-                               env->me_dbflags[i] = txn->mt_dbs[i].md_flags;
-                       env->me_numdbs = i;
-               }
+               mdb_dbis_update(txn, 1);
+               txn->mt_numdbs = 2; /* so txn_abort() doesn't close any new handles */
                mdb_txn_abort(txn);
                return MDB_SUCCESS;
        }
@@ -2113,13 +2174,19 @@ mdb_txn_commit(MDB_txn *txn)
                parent->mt_next_pgno = txn->mt_next_pgno;
                parent->mt_flags = txn->mt_flags;
 
-               /* Merge (and close) our cursors with parent's */
-               mdb_cursor_merge(txn);
+               /* Merge our cursors into parent's and close them */
+               mdb_cursors_close(txn, C_SHADOW);
 
                /* Update parent's DB table. */
                memcpy(parent->mt_dbs, txn->mt_dbs, txn->mt_numdbs * sizeof(MDB_db));
-               memcpy(parent->mt_dbflags, txn->mt_dbflags, txn->mt_numdbs);
                txn->mt_parent->mt_numdbs = txn->mt_numdbs;
+               txn->mt_parent->mt_dbflags[0] = txn->mt_dbflags[0];
+               txn->mt_parent->mt_dbflags[1] = txn->mt_dbflags[1];
+               for (i=2; i<txn->mt_numdbs; i++) {
+                       /* preserve parent's DB_NEW status */
+                       x = txn->mt_parent->mt_dbflags[i] & DB_NEW;
+                       txn->mt_parent->mt_dbflags[i] = txn->mt_dbflags[i] | x;
+               }
 
                dst = txn->mt_parent->mt_u.dirty_list;
                src = txn->mt_u.dirty_list;
@@ -2167,6 +2234,8 @@ mdb_txn_commit(MDB_txn *txn)
                return EINVAL;
        }
 
+       mdb_cursors_close(txn, 0);
+
        if (!txn->mt_u.dirty_list[0].mid && !(txn->mt_flags & MDB_TXN_DIRTY))
                goto done;
 
@@ -2340,7 +2409,6 @@ free2:
                        dp = txn->mt_u.dirty_list[i].mptr;
                        /* clear dirty flag */
                        dp->mp_flags &= ~P_DIRTY;
-                       txn->mt_u.dirty_list[i].mid = 0;
                }
                txn->mt_u.dirty_list[0].mid = 0;
                goto sync;
@@ -2438,19 +2506,7 @@ free2:
 #endif
        } while (!done);
 
-       /* Drop the dirty pages.
-        */
-       for (i=1; i<=txn->mt_u.dirty_list[0].mid; i++) {
-               dp = txn->mt_u.dirty_list[i].mptr;
-               if (!IS_OVERFLOW(dp) || dp->mp_pages == 1) {
-                       mdb_page_free(txn->mt_env, dp);
-               } else {
-                       VGMEMP_FREE(txn->mt_env, dp);
-                       free(dp);
-               }
-               txn->mt_u.dirty_list[i].mid = 0;
-       }
-       txn->mt_u.dirty_list[0].mid = 0;
+       mdb_dlist_free(txn);
 
 sync:
        if ((n = mdb_env_sync(env, 0)) != 0 ||
@@ -2462,13 +2518,7 @@ sync:
 done:
        env->me_pglast = 0;
        env->me_txn = NULL;
-       if (txn->mt_numdbs > env->me_numdbs) {
-               /* update the DB flags */
-               MDB_dbi i;
-               for (i = env->me_numdbs; i<txn->mt_numdbs; i++)
-                       env->me_dbflags[i] = txn->mt_dbs[i].md_flags;
-               env->me_numdbs = i;
-       }
+       mdb_dbis_update(txn, 1);
 
        UNLOCK_MUTEX_W(env);
        free(txn);
@@ -2736,13 +2786,8 @@ mdb_env_create(MDB_env **env)
        if (!e)
                return ENOMEM;
 
-       e->me_free_pgs = mdb_midl_alloc();
-       if (!e->me_free_pgs) {
-               free(e);
-               return ENOMEM;
-       }
        e->me_maxreaders = DEFAULT_READERS;
-       e->me_maxdbs = 2;
+       e->me_maxdbs = e->me_numdbs = 2;
        e->me_fd = INVALID_HANDLE_VALUE;
        e->me_lfd = INVALID_HANDLE_VALUE;
        e->me_mfd = INVALID_HANDLE_VALUE;
@@ -2897,6 +2942,7 @@ mdb_env_open2(MDB_env *env)
        }
        env->me_psize = meta.mm_psize;
        env->me_maxfree_1pg = (env->me_psize - PAGEHDRSZ) / sizeof(pgno_t) - 1;
+       env->me_nodemax = (env->me_psize - PAGEHDRSZ) / MDB_MINKEYS;
 
        env->me_maxpg = env->me_mapsize / env->me_psize;
 
@@ -3158,65 +3204,69 @@ mdb_hash_hex(MDB_val *val, char *hexbuf)
  * @param[in] lpath The pathname of the file used for the lock region.
  * @param[in] mode The Unix permissions for the file, if we create it.
  * @param[out] excl Resulting file lock type: -1 none, 0 shared, 1 exclusive
+ * @param[in,out] excl In -1, out lock type: -1 none, 0 shared, 1 exclusive
  * @return 0 on success, non-zero on failure.
  */
 static int
 mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
 {
+#ifdef _WIN32
+#      define MDB_ERRCODE_ROFS ERROR_WRITE_PROTECT
+#else
+#      define MDB_ERRCODE_ROFS EROFS
+#ifdef O_CLOEXEC       /* Linux: Open file and set FD_CLOEXEC atomically */
+#      define MDB_CLOEXEC              O_CLOEXEC
+#else
+       int fdflags;
+#      define MDB_CLOEXEC              0
+#endif
+#endif
        int rc;
        off_t size, rsize;
 
-       *excl = -1;
-
 #ifdef _WIN32
-       if ((env->me_lfd = CreateFile(lpath, GENERIC_READ|GENERIC_WRITE,
+       env->me_lfd = CreateFile(lpath, GENERIC_READ|GENERIC_WRITE,
                FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,
-               FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE) {
+               FILE_ATTRIBUTE_NORMAL, NULL);
+#else
+       env->me_lfd = open(lpath, O_RDWR|O_CREAT|MDB_CLOEXEC, mode);
+#endif
+       if (env->me_lfd == INVALID_HANDLE_VALUE) {
                rc = ErrCode();
-               if (rc == ERROR_WRITE_PROTECT && (env->me_flags & MDB_RDONLY)) {
-                       env->me_flags |= MDB_ROFS;
+               if (rc == MDB_ERRCODE_ROFS && (env->me_flags & MDB_RDONLY)) {
                        return MDB_SUCCESS;
                }
                goto fail_errno;
        }
-       /* Try to get exclusive lock. If we succeed, then
-        * nobody is using the lock region and we should initialize it.
-        */
-       if ((rc = mdb_env_excl_lock(env, excl))) goto fail;
-       size = GetFileSize(env->me_lfd, NULL);
-
-#else
-#if !(O_CLOEXEC)
-       {
-               int fdflags;
-               if ((env->me_lfd = open(lpath, O_RDWR|O_CREAT, mode)) == -1) {
-                       rc = ErrCode();
-                       if (rc == EROFS && (env->me_flags & MDB_RDONLY)) {
-                               env->me_flags |= MDB_ROFS;
-                               return MDB_SUCCESS;
-                       }
-                       goto fail_errno;
-               }
-               /* Lose record locks when exec*() */
-               if ((fdflags = fcntl(env->me_lfd, F_GETFD) | FD_CLOEXEC) >= 0)
+#if ! ((MDB_CLOEXEC) || defined(_WIN32))
+       /* Lose record locks when exec*() */
+       if ((fdflags = fcntl(env->me_lfd, F_GETFD) | FD_CLOEXEC) >= 0)
                        fcntl(env->me_lfd, F_SETFD, fdflags);
-       }
-#else /* O_CLOEXEC on Linux: Open file and set FD_CLOEXEC atomically */
-       if ((env->me_lfd = open(lpath, O_RDWR|O_CREAT|O_CLOEXEC, mode)) == -1) {
-               rc = ErrCode();
-               if (rc == EROFS && (env->me_flags & MDB_RDONLY)) {
-                       env->me_flags |= MDB_ROFS;
-                       return MDB_SUCCESS;
+#endif
+
+       if (!(env->me_flags & MDB_NOTLS)) {
+               rc = pthread_key_create(&env->me_txkey, mdb_env_reader_dest);
+               if (rc)
+                       goto fail;
+               env->me_flags |= MDB_ENV_TXKEY;
+#ifdef _WIN32
+               /* Windows TLS callbacks need help finding their TLS info. */
+               if (mdb_tls_nkeys >= MAX_TLS_KEYS) {
+                       rc = MDB_TLS_FULL;
+                       goto fail;
                }
-               goto fail_errno;
-       }
+               mdb_tls_keys[mdb_tls_nkeys++] = env->me_txkey;
 #endif
+       }
 
        /* Try to get exclusive lock. If we succeed, then
         * nobody is using the lock region and we should initialize it.
         */
        if ((rc = mdb_env_excl_lock(env, excl))) goto fail;
 
+#ifdef _WIN32
+       size = GetFileSize(env->me_lfd, NULL);
+#else
        size = lseek(env->me_lfd, 0, SEEK_END);
 #endif
        rsize = (env->me_maxreaders-1) * sizeof(MDB_reader) + sizeof(MDB_txninfo);
@@ -3372,12 +3422,12 @@ fail:
         *      environment and re-opening it with the new flags.
         */
 #define        CHANGEABLE      (MDB_NOSYNC|MDB_NOMETASYNC|MDB_MAPASYNC)
-#define        CHANGELESS      (MDB_FIXEDMAP|MDB_NOSUBDIR|MDB_RDONLY|MDB_WRITEMAP)
+#define        CHANGELESS      (MDB_FIXEDMAP|MDB_NOSUBDIR|MDB_RDONLY|MDB_WRITEMAP|MDB_NOTLS)
 
 int
-mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
+mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
 {
-       int             oflags, rc, len, excl;
+       int             oflags, rc, len, excl = -1;
        char *lpath, *dpath;
 
        if (env->me_fd!=INVALID_HANDLE_VALUE || (flags & ~(CHANGEABLE|CHANGELESS)))
@@ -3402,11 +3452,27 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
                sprintf(dpath, "%s" DATANAME, path);
        }
 
+       rc = MDB_SUCCESS;
        flags |= env->me_flags;
-       /* silently ignore WRITEMAP if we're only getting read access */
-       if (F_ISSET(flags, MDB_RDONLY|MDB_WRITEMAP))
-               flags ^= MDB_WRITEMAP;
+       if (flags & MDB_RDONLY) {
+               /* silently ignore WRITEMAP when we're only getting read access */
+               flags &= ~MDB_WRITEMAP;
+       } else {
+               if (!((env->me_free_pgs = mdb_midl_alloc(MDB_IDL_UM_MAX)) &&
+                         (env->me_dirty_list = calloc(MDB_IDL_UM_SIZE, sizeof(MDB_ID2)))))
+                       rc = ENOMEM;
+       }
        env->me_flags = flags |= MDB_ENV_ACTIVE;
+       if (rc)
+               goto leave;
+
+       env->me_path = strdup(path);
+       env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
+       env->me_dbflags = calloc(env->me_maxdbs, sizeof(uint16_t));
+       if (!(env->me_dbxs && env->me_path && env->me_dbflags)) {
+               rc = ENOMEM;
+               goto leave;
+       }
 
        rc = mdb_env_setup_locks(env, lpath, mode, &excl);
        if (rc)
@@ -3456,29 +3522,9 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mode_t mode)
                        }
                }
                DPRINTF("opened dbenv %p", (void *) env);
-               rc = pthread_key_create(&env->me_txkey, mdb_env_reader_dest);
-               if (rc)
-                       goto leave;
-               env->me_numdbs = 2;     /* this notes that me_txkey was set */
-#ifdef _WIN32
-               /* Windows TLS callbacks need help finding their TLS info. */
-               if (mdb_tls_nkeys < MAX_TLS_KEYS)
-                       mdb_tls_keys[mdb_tls_nkeys++] = env->me_txkey;
-               else {
-                       rc = MDB_TLS_FULL;
-                       goto leave;
-               }
-#endif
                if (excl > 0) {
                        rc = mdb_env_share_locks(env, &excl);
-                       if (rc)
-                               goto leave;
                }
-               env->me_dbxs = calloc(env->me_maxdbs, sizeof(MDB_dbx));
-               env->me_dbflags = calloc(env->me_maxdbs, sizeof(uint16_t));
-               env->me_path = strdup(path);
-               if (!env->me_dbxs || !env->me_dbflags || !env->me_path)
-                       rc = ENOMEM;
        }
 
 leave:
@@ -3489,7 +3535,7 @@ leave:
        return rc;
 }
 
-/** Destroy resources from mdb_env_open() and clear our readers */
+/** Destroy resources from mdb_env_open(), clear our readers & DBIs */
 static void
 mdb_env_close0(MDB_env *env, int excl)
 {
@@ -3498,11 +3544,18 @@ mdb_env_close0(MDB_env *env, int excl)
        if (!(env->me_flags & MDB_ENV_ACTIVE))
                return;
 
+       /* Doing this here since me_dbxs may not exist during mdb_env_close */
+       for (i = env->me_maxdbs; --i > MAIN_DBI; )
+               free(env->me_dbxs[i].md_name.mv_data);
+
        free(env->me_dbflags);
        free(env->me_dbxs);
        free(env->me_path);
+       free(env->me_dirty_list);
+       if (env->me_free_pgs)
+               mdb_midl_free(env->me_free_pgs);
 
-       if (env->me_numdbs) {
+       if (env->me_flags & MDB_ENV_TXKEY) {
                pthread_key_delete(env->me_txkey);
 #ifdef _WIN32
                /* Delete our key from the global list */
@@ -3568,66 +3621,25 @@ mdb_env_close0(MDB_env *env, int excl)
                close(env->me_lfd);
        }
 
-       env->me_flags &= ~MDB_ENV_ACTIVE;
+       env->me_flags &= ~(MDB_ENV_ACTIVE|MDB_ENV_TXKEY);
 }
 
 int
-mdb_env_copy(MDB_env *env, const char *path)
+mdb_env_copyfd(MDB_env *env, int fd)
 {
        MDB_txn *txn = NULL;
-       int rc, len;
+       int rc;
        size_t wsize;
-       char *lpath, *ptr;
-       HANDLE newfd = INVALID_HANDLE_VALUE;
-
-       if (env->me_flags & MDB_NOSUBDIR) {
-               lpath = (char *)path;
-       } else {
-               len = strlen(path);
-               len += sizeof(DATANAME);
-               lpath = malloc(len);
-               if (!lpath)
-                       return ENOMEM;
-               sprintf(lpath, "%s" DATANAME, path);
-       }
-
-       /* The destination path must exist, but the destination file must not.
-        * We don't want the OS to cache the writes, since the source data is
-        * already in the OS cache.
-        */
-#ifdef _WIN32
-       newfd = CreateFile(lpath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
-                               FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH, NULL);
-#else
-       newfd = open(lpath, O_WRONLY|O_CREAT|O_EXCL
-#ifdef O_DIRECT
-               |O_DIRECT
-#endif
-               , 0666);
-#endif
-       if (!(env->me_flags & MDB_NOSUBDIR))
-               free(lpath);
-       if (newfd == INVALID_HANDLE_VALUE) {
-               rc = ErrCode();
-               goto leave;
-       }
-
-#ifdef F_NOCACHE       /* __APPLE__ */
-       rc = fcntl(newfd, F_NOCACHE, 1);
-       if (rc) {
-               rc = ErrCode();
-               goto leave;
-       }
-#endif
+       char *ptr;
 
        /* Do the lock/unlock of the reader mutex before starting the
         * write txn.  Otherwise other read txns could block writers.
         */
        rc = mdb_txn_begin(env, NULL, MDB_RDONLY, &txn);
        if (rc)
-               goto leave;
+               return rc;
 
-       if (!(env->me_flags & MDB_ROFS)) {
+       if (env->me_txns) {
                /* We must start the actual read txn after blocking writers */
                mdb_txn_reset0(txn);
 
@@ -3645,14 +3657,14 @@ mdb_env_copy(MDB_env *env, const char *path)
 #ifdef _WIN32
        {
                DWORD len;
-               rc = WriteFile(newfd, env->me_map, wsize, &len, NULL);
+               rc = WriteFile(fd, env->me_map, wsize, &len, NULL);
                rc = (len == wsize) ? MDB_SUCCESS : ErrCode();
        }
 #else
-       rc = write(newfd, env->me_map, wsize);
+       rc = write(fd, env->me_map, wsize);
        rc = (rc == (int)wsize) ? MDB_SUCCESS : ErrCode();
 #endif
-       if (! (env->me_flags & MDB_ROFS))
+       if (env->me_txns)
                UNLOCK_MUTEX_W(env);
 
        if (rc)
@@ -3668,7 +3680,7 @@ mdb_env_copy(MDB_env *env, const char *path)
                        w2 = MAX_WRITE;
                else
                        w2 = wsize;
-               rc = WriteFile(newfd, ptr, w2, &len, NULL);
+               rc = WriteFile(fd, ptr, w2, &len, NULL);
                rc = (len == w2) ? MDB_SUCCESS : ErrCode();
                if (rc) break;
                wsize -= w2;
@@ -3682,14 +3694,67 @@ mdb_env_copy(MDB_env *env, const char *path)
                        w2 = MAX_WRITE;
                else
                        w2 = wsize;
-               wres = write(newfd, ptr, w2);
+               wres = write(fd, ptr, w2);
                rc = (wres > 0) ? MDB_SUCCESS : ErrCode();
                if (rc) break;
                wsize -= wres;
                ptr += wres;
        }
 #endif
+
+leave:
        mdb_txn_abort(txn);
+       return rc;
+}
+
+int
+mdb_env_copy(MDB_env *env, const char *path)
+{
+       int rc, len;
+       char *lpath;
+       HANDLE newfd = INVALID_HANDLE_VALUE;
+
+       if (env->me_flags & MDB_NOSUBDIR) {
+               lpath = (char *)path;
+       } else {
+               len = strlen(path);
+               len += sizeof(DATANAME);
+               lpath = malloc(len);
+               if (!lpath)
+                       return ENOMEM;
+               sprintf(lpath, "%s" DATANAME, path);
+       }
+
+       /* The destination path must exist, but the destination file must not.
+        * We don't want the OS to cache the writes, since the source data is
+        * already in the OS cache.
+        */
+#ifdef _WIN32
+       newfd = CreateFile(lpath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
+                               FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH, NULL);
+#else
+       newfd = open(lpath, O_WRONLY|O_CREAT|O_EXCL
+#ifdef O_DIRECT
+               |O_DIRECT
+#endif
+               , 0666);
+#endif
+       if (!(env->me_flags & MDB_NOSUBDIR))
+               free(lpath);
+       if (newfd == INVALID_HANDLE_VALUE) {
+               rc = ErrCode();
+               goto leave;
+       }
+
+#ifdef F_NOCACHE       /* __APPLE__ */
+       rc = fcntl(newfd, F_NOCACHE, 1);
+       if (rc) {
+               rc = ErrCode();
+               goto leave;
+       }
+#endif
+
+       rc = mdb_env_copyfd(env, newfd);
 
 leave:
        if (newfd != INVALID_HANDLE_VALUE)
@@ -3714,7 +3779,6 @@ mdb_env_close(MDB_env *env)
        }
 
        mdb_env_close0(env, 0);
-       mdb_midl_free(env->me_free_pgs);
        free(env);
 }
 
@@ -3960,17 +4024,20 @@ mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
  * @param[in] txn the transaction for this access.
  * @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)
+mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret, int *lvl)
 {
        MDB_page *p = NULL;
+       int level;
 
        if (!((txn->mt_flags & MDB_TXN_RDONLY) |
                  (txn->mt_env->me_flags & MDB_WRITEMAP)))
        {
                MDB_txn *tx2 = txn;
+               level = 1;
                do {
                        MDB_ID2L dl = tx2->mt_u.dirty_list;
                        if (dl[0].mid) {
@@ -3980,19 +4047,24 @@ mdb_page_get(MDB_txn *txn, pgno_t pgno, MDB_page **ret)
                                        goto done;
                                }
                        }
+                       level++;
                } while ((tx2 = tx2->mt_parent) != NULL);
        }
 
        if (pgno < txn->mt_next_pgno) {
+               level = 0;
                p = (MDB_page *)(txn->mt_env->me_map + txn->mt_env->me_psize * pgno);
        } else {
                DPRINTF("page %zu not found", pgno);
                assert(p != NULL);
+               return MDB_PAGE_NOTFOUND;
        }
 
 done:
        *ret = p;
-       return (p != NULL) ? MDB_SUCCESS : MDB_PAGE_NOTFOUND;
+       if (lvl)
+               *lvl = level;
+       return MDB_SUCCESS;
 }
 
 /** Search for the page a given key should be in.
@@ -4002,8 +4074,7 @@ done:
  * @param[in,out] mc the cursor for this operation.
  * @param[in] key the key to search for. If NULL, search for the lowest
  * page. (This is used by #mdb_cursor_first().)
- * @param[in] flags If MDB_PS_MODIFY set, visited pages are updated with new page numbers.
- *   If MDB_PS_ROOTONLY set, just fetch root node, no further lookups.
+ * @param[in] modify If true, visited pages are updated with new page numbers.
  * @return 0 on success, non-zero on failure.
  */
 static int
@@ -4047,7 +4118,7 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int modify)
                assert(i < NUMKEYS(mp));
                node = NODEPTR(mp, i);
 
-               if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mp)))
+               if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mp, NULL)) != 0)
                        return rc;
 
                mc->mc_ki[mc->mc_top] = i;
@@ -4073,6 +4144,28 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int modify)
        return MDB_SUCCESS;
 }
 
+/** Search for the lowest key under the current branch page.
+ * This just bypasses a NUMKEYS check in the current page
+ * before calling mdb_page_search_root(), because the callers
+ * are all in situations where the current page is known to
+ * be underfilled.
+ */
+static int
+mdb_page_search_lowest(MDB_cursor *mc)
+{
+       MDB_page        *mp = mc->mc_pg[mc->mc_top];
+       MDB_node        *node = NODEPTR(mp, 0);
+       int rc;
+
+       if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mp, NULL)) != 0)
+               return rc;
+
+       mc->mc_ki[mc->mc_top] = 0;
+       if ((rc = mdb_cursor_push(mc, mp)))
+               return rc;
+       return mdb_page_search_root(mc, NULL, 0);
+}
+
 /** Search for the page a given key should be in.
  * Pushes parent pages on the cursor stack. This function just sets up
  * the search; it finds the root page for \b mc's database and sets this
@@ -4081,7 +4174,8 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int modify)
  * @param[in,out] mc the cursor for this operation.
  * @param[in] key the key to search for. If NULL, search for the lowest
  * page. (This is used by #mdb_cursor_first().)
- * @param[in] modify If true, visited pages are updated with new page numbers.
+ * @param[in] flags If MDB_PS_MODIFY set, visited pages are updated with new page numbers.
+ *   If MDB_PS_ROOTONLY set, just fetch root node, no further lookups.
  * @return 0 on success, non-zero on failure.
  */
 static int
@@ -4110,22 +4204,25 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
                                if (*mc->mc_dbflag & DB_STALE) {
                                        MDB_val data;
                                        int exact = 0;
+                                       uint16_t flags;
                                        MDB_node *leaf = mdb_node_search(&mc2,
                                                &mc->mc_dbx->md_name, &exact);
                                        if (!exact)
                                                return MDB_NOTFOUND;
                                        mdb_node_read(mc->mc_txn, leaf, &data);
+                                       memcpy(&flags, ((char *) data.mv_data + offsetof(MDB_db, md_flags)),
+                                               sizeof(uint16_t));
                                        /* The txn may not know this DBI, or another process may
                                         * have dropped and recreated the DB with other flags.
                                         */
-                                       if (mc->mc_db->md_flags != *(uint16_t *)
-                                               ((char *) data.mv_data + offsetof(MDB_db, md_flags)))
+                                       if ((mc->mc_db->md_flags & PERSISTENT_FLAGS) != flags)
                                                return MDB_INCOMPATIBLE;
                                        memcpy(mc->mc_db, data.mv_data, sizeof(MDB_db));
                                }
                                if (flags & MDB_PS_MODIFY)
                                        dbflag = DB_DIRTY;
-                               *mc->mc_dbflag = dbflag;
+                               *mc->mc_dbflag &= ~DB_STALE;
+                               *mc->mc_dbflag |= dbflag;
                        }
                }
                root = mc->mc_db->md_root;
@@ -4138,7 +4235,7 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
 
        assert(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])))
+               if ((rc = mdb_page_get(mc->mc_txn, root, &mc->mc_pg[0], NULL)) != 0)
                        return rc;
 
        mc->mc_snum = 1;
@@ -4181,7 +4278,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))) {
+       if ((rc = mdb_page_get(txn, pgno, &omp, NULL)) != 0) {
                DPRINTF("read overflow page %zu failed", pgno);
                return rc;
        }
@@ -4203,7 +4300,7 @@ mdb_get(MDB_txn *txn, MDB_dbi dbi,
        assert(data);
        DPRINTF("===> get db %u key [%s]", dbi, DKEY(key));
 
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        if (key->mv_size == 0 || key->mv_size > MDB_MAXKEYSIZE) {
@@ -4258,10 +4355,12 @@ mdb_cursor_sibling(MDB_cursor *mc, int move_right)
        assert(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)))
+       if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(indx), &mp, NULL) != 0))
                return rc;
 
        mdb_cursor_push(mc, mp);
+       if (!move_right)
+               mc->mc_ki[mc->mc_top] = NUMKEYS(mp)-1;
 
        return MDB_SUCCESS;
 }
@@ -4645,20 +4744,20 @@ mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
 
        if (!(mc->mc_flags & C_EOF)) {
 
-       if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
-               MDB_val lkey;
+               if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
+                       MDB_val lkey;
 
-               lkey.mv_size = MDB_MAXKEYSIZE+1;
-               lkey.mv_data = NULL;
-               rc = mdb_page_search(mc, &lkey, 0);
-               if (rc != MDB_SUCCESS)
-                       return rc;
-       }
-       assert(IS_LEAF(mc->mc_pg[mc->mc_top]));
+                       lkey.mv_size = MDB_MAXKEYSIZE+1;
+                       lkey.mv_data = NULL;
+                       rc = mdb_page_search(mc, &lkey, 0);
+                       if (rc != MDB_SUCCESS)
+                               return rc;
+               }
+               assert(IS_LEAF(mc->mc_pg[mc->mc_top]));
 
-       mc->mc_flags |= C_INITIALIZED|C_EOF;
-       mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]) - 1;
        }
+       mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]) - 1;
+       mc->mc_flags |= C_INITIALIZED|C_EOF;
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
 
        if (IS_LEAF2(mc->mc_pg[mc->mc_top])) {
@@ -4786,7 +4885,7 @@ fetchm:
        case MDB_PREV:
        case MDB_PREV_DUP:
        case MDB_PREV_NODUP:
-               if (!(mc->mc_flags & C_INITIALIZED) || (mc->mc_flags & C_EOF)) {
+               if (!(mc->mc_flags & C_INITIALIZED)) {
                        rc = mdb_cursor_last(mc, key, data);
                        mc->mc_flags |= C_INITIALIZED;
                        mc->mc_ki[mc->mc_top]++;
@@ -4840,12 +4939,11 @@ mdb_cursor_touch(MDB_cursor *mc)
        if (mc->mc_dbi > MAIN_DBI && !(*mc->mc_dbflag & DB_DIRTY)) {
                MDB_cursor mc2;
                MDB_xcursor mcx;
-               mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI,
-                       mc->mc_txn->mt_dbs[MAIN_DBI].md_flags & MDB_DUPSORT ? &mcx : NULL);
+               mdb_cursor_init(&mc2, mc->mc_txn, MAIN_DBI, &mcx);
                rc = mdb_page_search(&mc2, &mc->mc_dbx->md_name, MDB_PS_MODIFY);
                if (rc)
                         return rc;
-               *mc->mc_dbflag = DB_DIRTY;
+               *mc->mc_dbflag |= DB_DIRTY;
        }
        for (mc->mc_top = 0; mc->mc_top < mc->mc_snum; mc->mc_top++) {
                rc = mdb_page_touch(mc);
@@ -4907,7 +5005,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                mdb_cursor_push(mc, np);
                mc->mc_db->md_root = np->mp_pgno;
                mc->mc_db->md_depth++;
-               *mc->mc_dbflag = DB_DIRTY;
+               *mc->mc_dbflag |= DB_DIRTY;
                if ((mc->mc_db->md_flags & (MDB_DUPSORT|MDB_DUPFIXED))
                        == MDB_DUPFIXED)
                        np->mp_flags |= P_LEAF2;
@@ -4926,7 +5024,8 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                                        rc = MDB_NOTFOUND;
                                        mc->mc_ki[mc->mc_top]++;
                                } else {
-                                       rc = 0;
+                                       /* new key is <= last key */
+                                       rc = MDB_KEYEXIST;
                                }
                        }
                } else {
@@ -5035,8 +5134,7 @@ reuse:
                                }
                                offset += offset & 1;
                                if (NODESIZE + sizeof(indx_t) + NODEKSZ(leaf) + NODEDSZ(leaf) +
-                                       offset >= (mc->mc_txn->mt_env->me_psize - PAGEHDRSZ) /
-                                               MDB_MINKEYS) {
+                                       offset >= mc->mc_txn->mt_env->me_nodemax) {
                                        /* yes, convert it */
                                        dummy.md_flags = 0;
                                        if (mc->mc_db->md_flags & MDB_DUPFIXED) {
@@ -5092,18 +5190,43 @@ current:
                if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
                        MDB_page *omp;
                        pgno_t pg;
-                       int ovpages, dpages;
+                       unsigned psize = mc->mc_txn->mt_env->me_psize;
+                       int level, ovpages, dpages = OVPAGES(data->mv_size, psize);
 
-                       ovpages = OVPAGES(NODEDSZ(leaf), mc->mc_txn->mt_env->me_psize);
-                       dpages = OVPAGES(data->mv_size, mc->mc_txn->mt_env->me_psize);
                        memcpy(&pg, NODEDATA(leaf), sizeof(pg));
-                       mdb_page_get(mc->mc_txn, pg, &omp);
+                       if ((rc2 = mdb_page_get(mc->mc_txn, pg, &omp, &level)) != 0)
+                               return rc2;
+                       ovpages = omp->mp_pages;
+
                        /* Is the ov page writable and large enough? */
                        if ((omp->mp_flags & P_DIRTY) && ovpages >= dpages) {
                                /* yes, overwrite it. Note in this case we don't
-                                * bother to try shrinking the node if the new data
+                                * bother to try shrinking the page if the new data
                                 * is smaller than the overflow threshold.
                                 */
+                               if (level > 1) {
+                                       /* It is writable only in a parent txn */
+                                       size_t sz = (size_t) psize * ovpages, off;
+                                       MDB_page *np = mdb_page_malloc(mc, ovpages);
+                                       MDB_ID2 id2;
+                                       if (!np)
+                                               return ENOMEM;
+                                       id2.mid = pg;
+                                       id2.mptr = np;
+                                       mdb_mid2l_insert(mc->mc_txn->mt_u.dirty_list, &id2);
+                                       if (!(flags & MDB_RESERVE)) {
+                                               /* Copy end of page, adjusting alignment so
+                                                * compiler may copy words instead of bytes.
+                                                */
+                                               off = (PAGEHDRSZ + data->mv_size) & -sizeof(size_t);
+                                               memcpy((size_t *)((char *)np + off),
+                                                       (size_t *)((char *)omp + off), sz - off);
+                                               sz = PAGEHDRSZ;
+                                       }
+                                       memcpy(np, omp, sz); /* Copy beginning of page */
+                                       omp = np;
+                               }
+                               SETDSZ(leaf, data->mv_size);
                                if (F_ISSET(flags, MDB_RESERVE))
                                        data->mv_data = METADATA(omp);
                                else
@@ -5214,6 +5337,8 @@ put_sub:
                                                }
                                        }
                                }
+                                /* we've done our job */
+                                dkey.mv_size = 0;
                        }
                        if (flags & MDB_APPENDDUP)
                                xflags |= MDB_APPEND;
@@ -5351,7 +5476,7 @@ mdb_leaf_size(MDB_env *env, MDB_val *key, MDB_val *data)
        size_t           sz;
 
        sz = LEAFSIZE(key, data);
-       if (sz >= env->me_psize / MDB_MINKEYS) {
+       if (sz >= env->me_nodemax) {
                /* put on overflow page */
                sz -= data->mv_size - sizeof(pgno_t);
        }
@@ -5376,7 +5501,7 @@ mdb_branch_size(MDB_env *env, MDB_val *key)
        size_t           sz;
 
        sz = INDXSIZE(key);
-       if (sz >= env->me_psize / MDB_MINKEYS) {
+       if (sz >= env->me_nodemax) {
                /* put on overflow page */
                /* not implemented */
                /* sz -= key->size - sizeof(pgno_t); */
@@ -5444,7 +5569,7 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
                if (F_ISSET(flags, F_BIGDATA)) {
                        /* Data already on overflow page. */
                        node_size += sizeof(pgno_t);
-               } else if (node_size + data->mv_size >= mc->mc_txn->mt_env->me_psize / MDB_MINKEYS) {
+               } else if (node_size + data->mv_size >= mc->mc_txn->mt_env->me_nodemax) {
                        int ovpages = OVPAGES(data->mv_size, mc->mc_txn->mt_env->me_psize);
                        int rc;
                        /* Put data on overflow page. */
@@ -5699,8 +5824,8 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
        }
        DPRINTF("Sub-db %u for db %u root page %zu", mx->mx_cursor.mc_dbi, mc->mc_dbi,
                mx->mx_db.md_root);
-       mx->mx_dbflag = (F_ISSET(mc->mc_pg[mc->mc_top]->mp_flags, P_DIRTY)) ?
-               DB_DIRTY : 0;
+       mx->mx_dbflag = DB_VALID | (F_ISSET(mc->mc_pg[mc->mc_top]->mp_flags, P_DIRTY) ?
+               DB_DIRTY : 0);
        mx->mx_dbx.md_name.mv_data = NODEKEY(node);
        mx->mx_dbx.md_name.mv_size = node->mn_ksize;
 #if UINT_MAX < SIZE_MAX
@@ -5743,10 +5868,9 @@ int
 mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
 {
        MDB_cursor      *mc;
-       MDB_xcursor     *mx = NULL;
        size_t size = sizeof(MDB_cursor);
 
-       if (txn == NULL || ret == NULL || dbi >= txn->mt_numdbs)
+       if (txn == NULL || ret == NULL || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        /* Allow read access to the freelist */
@@ -5757,13 +5881,11 @@ mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
                size += sizeof(MDB_xcursor);
 
        if ((mc = malloc(size)) != NULL) {
-               if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
-                       mx = (MDB_xcursor *)(mc + 1);
-               }
-               mdb_cursor_init(mc, txn, dbi, mx);
+               mdb_cursor_init(mc, txn, dbi, (MDB_xcursor *)(mc + 1));
                if (txn->mt_cursors) {
                        mc->mc_next = txn->mt_cursors[dbi];
                        txn->mt_cursors[dbi] = mc;
+                       mc->mc_flags |= C_UNTRACK;
                }
                mc->mc_flags |= C_ALLOCD;
        } else {
@@ -5778,13 +5900,19 @@ mdb_cursor_open(MDB_txn *txn, MDB_dbi dbi, MDB_cursor **ret)
 int
 mdb_cursor_renew(MDB_txn *txn, MDB_cursor *mc)
 {
+       unsigned flags;
+
        if (txn == NULL || mc == NULL || mc->mc_dbi >= txn->mt_numdbs)
                return EINVAL;
 
-       if (txn->mt_cursors)
+       if ((mc->mc_flags & C_UNTRACK) || txn->mt_cursors)
                return EINVAL;
 
+       flags = mc->mc_flags;
+
        mdb_cursor_init(mc, txn, mc->mc_dbi, mc->mc_xcursor);
+
+       mc->mc_flags |= (flags & C_ALLOCD);
        return MDB_SUCCESS;
 }
 
@@ -5817,7 +5945,7 @@ mdb_cursor_close(MDB_cursor *mc)
 {
        if (mc != NULL) {
                /* remove from txn, if tracked */
-               if (mc->mc_txn->mt_cursors) {
+               if ((mc->mc_flags & C_UNTRACK) && mc->mc_txn->mt_cursors) {
                        MDB_cursor **prev = &mc->mc_txn->mt_cursors[mc->mc_dbi];
                        while (*prev && *prev != mc) prev = &(*prev)->mc_next;
                        if (*prev == mc)
@@ -5843,8 +5971,7 @@ mdb_cursor_dbi(MDB_cursor *mc)
 }
 
 /** Replace the key for a node with a new key.
- * @param[in] mp The page containing the node to operate on.
- * @param[in] indx The index of the node to operate on.
+ * @param[in] mc Cursor pointing to the node to operate on.
  * @param[in] key The new key to use.
  * @return 0 on success, non-zero on failure.
  */
@@ -5956,7 +6083,7 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                        unsigned int snum = csrc->mc_snum;
                        MDB_node *s2;
                        /* must find the lowest key below src */
-                       mdb_page_search_root(csrc, NULL, 0);
+                       mdb_page_search_lowest(csrc);
                        if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
                                key.mv_size = csrc->mc_db->md_pad;
                                key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], 0, key.mv_size);
@@ -5979,7 +6106,7 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                MDB_node *s2;
                MDB_val bkey;
                /* must find the lowest key below dst */
-               mdb_page_search_root(cdst, NULL, 0);
+               mdb_page_search_lowest(cdst);
                if (IS_LEAF2(cdst->mc_pg[cdst->mc_top])) {
                        bkey.mv_size = cdst->mc_db->md_pad;
                        bkey.mv_data = LEAF2KEY(cdst->mc_pg[cdst->mc_top], 0, bkey.mv_size);
@@ -6143,7 +6270,7 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                                unsigned int snum = csrc->mc_snum;
                                MDB_node *s2;
                                /* must find the lowest key below src */
-                               mdb_page_search_root(csrc, NULL, 0);
+                               mdb_page_search_lowest(csrc);
                                if (IS_LEAF2(csrc->mc_pg[csrc->mc_top])) {
                                        key.mv_size = csrc->mc_db->md_pad;
                                        key.mv_data = LEAF2KEY(csrc->mc_pg[csrc->mc_top], 0, key.mv_size);
@@ -6247,9 +6374,10 @@ mdb_rebalance(MDB_cursor *mc)
 {
        MDB_node        *node;
        int rc;
-       unsigned int ptop;
+       unsigned int ptop, minkeys;
        MDB_cursor      mn;
 
+       minkeys = 1 + (IS_BRANCH(mc->mc_pg[mc->mc_top]));
 #if MDB_DEBUG
        {
        pgno_t pgno;
@@ -6260,7 +6388,8 @@ mdb_rebalance(MDB_cursor *mc)
        }
 #endif
 
-       if (PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) >= FILL_THRESHOLD) {
+       if (PAGEFILL(mc->mc_txn->mt_env, mc->mc_pg[mc->mc_top]) >= FILL_THRESHOLD &&
+               NUMKEYS(mc->mc_pg[mc->mc_top]) >= minkeys) {
 #if MDB_DEBUG
                pgno_t pgno;
                COPY_PGNO(pgno, mc->mc_pg[mc->mc_top]->mp_pgno);
@@ -6272,6 +6401,10 @@ mdb_rebalance(MDB_cursor *mc)
 
        if (mc->mc_snum < 2) {
                MDB_page *mp = mc->mc_pg[0];
+               if (IS_SUBP(mp)) {
+                       DPUTS("Can't rebalance a subpage, ignoring");
+                       return MDB_SUCCESS;
+               }
                if (NUMKEYS(mp) == 0) {
                        DPUTS("tree is completely empty");
                        mc->mc_db->md_root = P_INVALID;
@@ -6305,8 +6438,8 @@ mdb_rebalance(MDB_cursor *mc)
                        DPUTS("collapsing root page!");
                        mdb_midl_append(&mc->mc_txn->mt_free_pgs, mp->mp_pgno);
                        mc->mc_db->md_root = NODEPGNO(NODEPTR(mp, 0));
-                       if ((rc = mdb_page_get(mc->mc_txn, mc->mc_db->md_root,
-                               &mc->mc_pg[0])))
+                       rc = mdb_page_get(mc->mc_txn,mc->mc_db->md_root,&mc->mc_pg[0],NULL);
+                       if (rc)
                                return rc;
                        mc->mc_db->md_depth--;
                        mc->mc_db->md_branch_pages--;
@@ -6327,6 +6460,8 @@ mdb_rebalance(MDB_cursor *mc)
                                        if (m3->mc_snum < mc->mc_snum) continue;
                                        if (m3->mc_pg[0] == mp) {
                                                m3->mc_pg[0] = mc->mc_pg[0];
+                                               m3->mc_snum = 1;
+                                               m3->mc_top = 0;
                                        }
                                }
                        }
@@ -6357,7 +6492,8 @@ mdb_rebalance(MDB_cursor *mc)
                DPUTS("reading right neighbor");
                mn.mc_ki[ptop]++;
                node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
-               if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mn.mc_pg[mn.mc_top])))
+               rc = mdb_page_get(mc->mc_txn,NODEPGNO(node),&mn.mc_pg[mn.mc_top],NULL);
+               if (rc)
                        return rc;
                mn.mc_ki[mn.mc_top] = 0;
                mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]);
@@ -6367,7 +6503,8 @@ mdb_rebalance(MDB_cursor *mc)
                DPUTS("reading left neighbor");
                mn.mc_ki[ptop]--;
                node = NODEPTR(mc->mc_pg[ptop], mn.mc_ki[ptop]);
-               if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(node), &mn.mc_pg[mn.mc_top])))
+               rc = mdb_page_get(mc->mc_txn,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;
                mc->mc_ki[mc->mc_top] = 0;
@@ -6376,13 +6513,12 @@ mdb_rebalance(MDB_cursor *mc)
        DPRINTF("found neighbor page %zu (%u keys, %.1f%% full)",
            mn.mc_pg[mn.mc_top]->mp_pgno, NUMKEYS(mn.mc_pg[mn.mc_top]), (float)PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) / 10);
 
-       /* If the neighbor page is above threshold and has at least three
-        * keys, move one key from it. (A page must never have fewer than
-        * 2 keys.)
-        *
-        * Otherwise we should try to merge them.
+       /* If the neighbor page is above threshold and has enough keys,
+        * move one key from it. Otherwise we should try to merge them.
+        * (A branch page must never have less than 2 keys.)
         */
-       if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > 2)
+       minkeys = 1 + (IS_BRANCH(mn.mc_pg[mn.mc_top]));
+       if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys)
                return mdb_node_move(&mn, mc);
        else {
                if (mc->mc_ki[ptop] == 0)
@@ -6403,10 +6539,14 @@ mdb_cursor_del0(MDB_cursor *mc, MDB_node *leaf)
        /* add overflow pages to free list */
        if (!IS_LEAF2(mc->mc_pg[mc->mc_top]) && F_ISSET(leaf->mn_flags, F_BIGDATA)) {
                int i, ovpages;
+               MDB_page *omp;
                pgno_t pg;
 
                memcpy(&pg, NODEDATA(leaf), sizeof(pg));
-               ovpages = OVPAGES(NODEDSZ(leaf), mc->mc_txn->mt_env->me_psize);
+               if ((rc = mdb_page_get(mc->mc_txn, pg, &omp, NULL)) != 0)
+                       return rc;
+               assert(IS_OVERFLOW(omp));
+               ovpages = omp->mp_pages;
                mc->mc_db->md_overflow_pages -= ovpages;
                for (i=0; i<ovpages; i++) {
                        DPRINTF("freed ov page %zu", pg);
@@ -6419,6 +6559,9 @@ mdb_cursor_del0(MDB_cursor *mc, MDB_node *leaf)
        rc = mdb_rebalance(mc);
        if (rc != MDB_SUCCESS)
                mc->mc_txn->mt_flags |= MDB_TXN_ERROR;
+       /* if mc points past last node in page, invalidate */
+       else if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top]))
+               mc->mc_flags &= ~C_INITIALIZED;
 
        return rc;
 }
@@ -6438,7 +6581,7 @@ mdb_del(MDB_txn *txn, MDB_dbi dbi,
 
        DPRINTF("====> delete db %u key [%s]", dbi, DKEY(key));
 
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
@@ -6745,7 +6888,7 @@ newsep:
        /* Move half of the keys to the right sibling. */
 
        /* grab a page to hold a temporary copy */
-       copy = mdb_page_malloc(mc);
+       copy = mdb_page_malloc(mc, 1);
        if (copy == NULL)
                return ENOMEM;
 
@@ -6822,6 +6965,17 @@ newsep:
                }
        } else {
                mc->mc_ki[ptop]++;
+               /* Make sure mc_ki is still valid.
+                */
+               if (mn.mc_pg[ptop] != mc->mc_pg[ptop] &&
+                   mc->mc_ki[ptop] >= NUMKEYS(mc->mc_pg[ptop])) {
+                       for (i=0; i<ptop; i++) {
+                               mc->mc_pg[i] = mn.mc_pg[i];
+                               mc->mc_ki[i] = mn.mc_ki[i];
+                       }
+                       mc->mc_pg[ptop] = mn.mc_pg[ptop];
+                       mc->mc_ki[ptop] = mn.mc_ki[ptop] - 1;
+               }
        }
 
        /* return tmp page to freelist */
@@ -6889,7 +7043,7 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
        assert(key != NULL);
        assert(data != NULL);
 
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY)) {
@@ -6900,7 +7054,7 @@ mdb_put(MDB_txn *txn, MDB_dbi dbi,
                return EINVAL;
        }
 
-       if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA|MDB_RESERVE|MDB_APPEND)) != flags)
+       if ((flags & (MDB_NOOVERWRITE|MDB_NODUPDATA|MDB_RESERVE|MDB_APPEND|MDB_APPENDDUP)) != flags)
                return EINVAL;
 
        mdb_cursor_init(&mc, txn, dbi, &mx);
@@ -7011,15 +7165,11 @@ mdb_default_cmp(MDB_txn *txn, MDB_dbi dbi)
                 : ((f & MDB_REVERSEDUP) ? mdb_cmp_memnr : mdb_cmp_memn));
 }
 
-#define PERSISTENT_FLAGS       0xffff
-#define VALID_FLAGS    (MDB_REVERSEKEY|MDB_DUPSORT|MDB_INTEGERKEY|MDB_DUPFIXED|\
-       MDB_INTEGERDUP|MDB_REVERSEDUP|MDB_CREATE)
 int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *dbi)
 {
        MDB_val key, data;
        MDB_dbi i;
        MDB_cursor mc;
-       uint16_t mdflags;
        int rc, dbflag, exact;
        unsigned int unused = 0;
        size_t len;
@@ -7069,8 +7219,12 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
        if (!unused && txn->mt_numdbs >= txn->mt_env->me_maxdbs)
                return MDB_DBS_FULL;
 
+       /* Cannot mix named databases with some mainDB flags */
+       if (txn->mt_dbs[MAIN_DBI].md_flags & (MDB_DUPSORT|MDB_INTEGERKEY))
+               return (flags & MDB_CREATE) ? MDB_INCOMPATIBLE : MDB_NOTFOUND;
+
        /* Find the DB info */
-       dbflag = 0;
+       dbflag = DB_NEW|DB_VALID;
        exact = 0;
        key.mv_size = len;
        key.mv_data = (void *)name;
@@ -7090,7 +7244,7 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
                dummy.md_root = P_INVALID;
                dummy.md_flags = flags & PERSISTENT_FLAGS;
                rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA);
-               dbflag = DB_DIRTY;
+               dbflag |= DB_DIRTY;
        }
 
        /* OK, got info, add to table */
@@ -7102,18 +7256,10 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
                txn->mt_dbflags[slot] = dbflag;
                memcpy(&txn->mt_dbs[slot], data.mv_data, sizeof(MDB_db));
                *dbi = slot;
-               txn->mt_env->me_dbflags[slot] = mdflags = txn->mt_dbs[slot].md_flags;
+               txn->mt_env->me_dbflags[slot] = txn->mt_dbs[slot].md_flags;
                mdb_default_cmp(txn, slot);
                if (!unused) {
                        txn->mt_numdbs++;
-                       txn->mt_env->me_numdbs++;
-               }
-               /* Open the DB in parent txns as well */
-               while ((txn = txn->mt_parent) != NULL) {
-                       txn->mt_dbflags[slot] = DB_STALE;
-                       txn->mt_dbs[slot].md_flags = mdflags;
-                       if (!unused)
-                               txn->mt_numdbs++;
                }
        }
 
@@ -7131,11 +7277,12 @@ int mdb_stat(MDB_txn *txn, MDB_dbi dbi, MDB_stat *arg)
 void mdb_dbi_close(MDB_env *env, MDB_dbi dbi)
 {
        char *ptr;
-       if (dbi <= MAIN_DBI || dbi >= env->me_numdbs)
+       if (dbi <= MAIN_DBI || dbi >= env->me_maxdbs)
                return;
        ptr = env->me_dbxs[dbi].md_name.mv_data;
        env->me_dbxs[dbi].md_name.mv_data = NULL;
        env->me_dbxs[dbi].md_name.mv_size = 0;
+       env->me_dbflags[dbi] = 0;
        free(ptr);
 }
 
@@ -7156,7 +7303,7 @@ mdb_drop0(MDB_cursor *mc, int subs)
                unsigned int i;
 
                /* LEAF2 pages have no nodes, cannot have sub-DBs */
-               if (!subs || IS_LEAF2(mc->mc_pg[mc->mc_top]))
+               if (IS_LEAF2(mc->mc_pg[mc->mc_top]))
                        mdb_cursor_pop(mc);
 
                mdb_cursor_copy(mc, &mx);
@@ -7164,7 +7311,21 @@ mdb_drop0(MDB_cursor *mc, int subs)
                        if (IS_LEAF(mc->mc_pg[mc->mc_top])) {
                                for (i=0; i<NUMKEYS(mc->mc_pg[mc->mc_top]); i++) {
                                        ni = NODEPTR(mc->mc_pg[mc->mc_top], i);
-                                       if (ni->mn_flags & F_SUBDATA) {
+                                       if (ni->mn_flags & F_BIGDATA) {
+                                               int j, ovpages;
+                                               MDB_page *omp;
+                                               pgno_t pg;
+                                               memcpy(&pg, NODEDATA(ni), sizeof(pg));
+                                               rc = mdb_page_get(mc->mc_txn, pg, &omp, NULL);
+                                               if (rc != 0)
+                                                       return rc;
+                                               assert(IS_OVERFLOW(omp));
+                                               ovpages = omp->mp_pages;
+                                               for (j=0; j<ovpages; j++) {
+                                                       mdb_midl_append(&mc->mc_txn->mt_free_pgs, pg);
+                                                       pg++;
+                                               }
+                                       } else if (subs && (ni->mn_flags & F_SUBDATA)) {
                                                mdb_xcursor_init1(mc, ni);
                                                rc = mdb_drop0(&mc->mc_xcursor->mx_cursor, 0);
                                                if (rc)
@@ -7182,14 +7343,18 @@ mdb_drop0(MDB_cursor *mc, int subs)
                        }
                        if (!mc->mc_top)
                                break;
+                       mc->mc_ki[mc->mc_top] = i;
                        rc = mdb_cursor_sibling(mc, 1);
                        if (rc) {
                                /* no more siblings, go back to beginning
                                 * of previous level.
                                 */
                                mdb_cursor_pop(mc);
-                               for (i=1; i<mc->mc_top; i++)
+                               mc->mc_ki[0] = 0;
+                               for (i=1; i<mc->mc_snum; i++) {
+                                       mc->mc_ki[i] = 0;
                                        mc->mc_pg[i] = mx.mc_pg[i];
+                               }
                        }
                }
                /* free it */
@@ -7204,7 +7369,7 @@ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
        MDB_cursor *mc;
        int rc;
 
-       if (!txn || !dbi || dbi >= txn->mt_numdbs || (unsigned)del > 1)
+       if (!txn || !dbi || dbi >= txn->mt_numdbs || (unsigned)del > 1 || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
@@ -7235,19 +7400,7 @@ int mdb_drop(MDB_txn *txn, MDB_dbi dbi, int del)
                txn->mt_dbs[dbi].md_entries = 0;
                txn->mt_dbs[dbi].md_root = P_INVALID;
 
-               if (!txn->mt_u.dirty_list[0].mid) {
-                       MDB_cursor m2;
-                       MDB_val key, data;
-                       /* make sure we have at least one dirty page in this txn
-                        * otherwise these changes will be ignored.
-                        */
-                       key.mv_size = sizeof(txnid_t);
-                       key.mv_data = &txn->mt_txnid;
-                       data.mv_size = sizeof(MDB_ID);
-                       data.mv_data = txn->mt_free_pgs;
-                       mdb_cursor_init(&m2, txn, FREE_DBI, NULL);
-                       rc = mdb_cursor_put(&m2, &key, &data, 0);
-               }
+               txn->mt_flags |= MDB_TXN_DIRTY;
        }
 leave:
        mdb_cursor_close(mc);
@@ -7256,7 +7409,7 @@ leave:
 
 int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_cmp = cmp;
@@ -7265,7 +7418,7 @@ int mdb_set_compare(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 
 int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_dcmp = cmp;
@@ -7274,7 +7427,7 @@ int mdb_set_dupsort(MDB_txn *txn, MDB_dbi dbi, MDB_cmp_func *cmp)
 
 int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_rel = rel;
@@ -7283,7 +7436,7 @@ int mdb_set_relfunc(MDB_txn *txn, MDB_dbi dbi, MDB_rel_func *rel)
 
 int mdb_set_relctx(MDB_txn *txn, MDB_dbi dbi, void *ctx)
 {
-       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs)
+       if (txn == NULL || !dbi || dbi >= txn->mt_numdbs || !(txn->mt_dbflags[dbi] & DB_VALID))
                return EINVAL;
 
        txn->mt_dbxs[dbi].md_relctx = ctx;