]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
Set/clear mp_pad, md_pad (MDB_DUPFIXED data size).
[openldap] / libraries / liblmdb / mdb.c
index adc20f893b9136c3e5d05bfcb162a05fda2e9b4a..797c67b32e5cac99cedc3bee2f7f9b5a7f1aee9d 100644 (file)
@@ -185,6 +185,12 @@ extern int cacheflush(char *addr, int nbytes, int cache);
 #define ESECT
 #endif
 
+#ifdef _MSC_VER
+#define CALL_CONV WINAPI
+#else
+#define CALL_CONV
+#endif
+
 /** @defgroup internal LMDB Internals
  *     @{
  */
@@ -1010,7 +1016,11 @@ struct MDB_txn {
  *     @ingroup internal
  *     @{
  */
-#define MDB_TXN_RDONLY         0x01            /**< read-only transaction */
+       /** #mdb_txn_begin() flags */
+#define MDB_TXN_BEGIN_FLAGS    MDB_RDONLY
+#define MDB_TXN_RDONLY         MDB_RDONLY      /**< read-only transaction */
+       /* internal txn flags */
+#define MDB_TXN_WRITEMAP       MDB_WRITEMAP    /**< copy of #MDB_env flag in writers */
 #define MDB_TXN_ERROR          0x02            /**< txn is unusable after an error */
 #define MDB_TXN_DIRTY          0x04            /**< must write, even if dirty list is empty */
 #define MDB_TXN_SPILLS         0x08            /**< txn or a parent has spilled pages */
@@ -1115,7 +1125,8 @@ struct MDB_env {
        unsigned int    me_psize;       /**< DB page size, inited from me_os_psize */
        unsigned int    me_os_psize;    /**< OS page size, from #GET_PAGESIZE */
        unsigned int    me_maxreaders;  /**< size of the reader table */
-       unsigned int    me_numreaders;  /**< max numreaders set by this env */
+       /** Max #MDB_txninfo.%mti_numreaders of interest to #mdb_env_close() */
+       volatile int    me_close_readers;
        MDB_dbi         me_numdbs;              /**< number of DBs opened */
        MDB_dbi         me_maxdbs;              /**< size of the DB table */
        MDB_PID_T       me_pid;         /**< process ID of this env */
@@ -1242,6 +1253,7 @@ static int        mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data);
 static void    mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx);
 static void    mdb_xcursor_init0(MDB_cursor *mc);
 static void    mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node);
+static void    mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int force);
 
 static int     mdb_drop0(MDB_cursor *mc, int subs);
 static void mdb_default_cmp(MDB_txn *txn, MDB_dbi dbi);
@@ -1934,7 +1946,7 @@ mdb_page_dirty(MDB_txn *txn, MDB_page *mp)
        MDB_ID2 mid;
        int rc, (*insert)(MDB_ID2L, MDB_ID2 *);
 
-       if (txn->mt_env->me_flags & MDB_WRITEMAP) {
+       if (txn->mt_flags & MDB_TXN_WRITEMAP) {
                insert = mdb_mid2l_append;
        } else {
                insert = mdb_mid2l_insert;
@@ -2538,15 +2550,22 @@ mdb_txn_renew0(MDB_txn *txn)
                                        UNLOCK_MUTEX_R(env);
                                        return MDB_READERS_FULL;
                                }
-                               ti->mti_readers[i].mr_pid = pid;
-                               ti->mti_readers[i].mr_tid = tid;
+                               r = &ti->mti_readers[i];
+                               /* Claim the reader slot, carefully since other code
+                                * uses the reader table un-mutexed: First reset the
+                                * slot, next publish it in mti_numreaders.  After
+                                * that, it is safe for mdb_env_close() to touch it.
+                                * When it will be closed, we can finally claim it.
+                                */
+                               r->mr_pid = 0;
+                               r->mr_txnid = (txnid_t)-1;
+                               r->mr_tid = tid;
                                if (i == nr)
                                        ti->mti_numreaders = ++nr;
-                               /* Save numreaders for un-mutexed mdb_env_close() */
-                               env->me_numreaders = nr;
+                               env->me_close_readers = nr;
+                               r->mr_pid = pid;
                                UNLOCK_MUTEX_R(env);
 
-                               r = &ti->mti_readers[i];
                                new_notls = (env->me_flags & MDB_NOTLS);
                                if (!new_notls && (rc=pthread_setspecific(env->me_txkey, r))) {
                                        r->mr_pid = 0;
@@ -2562,6 +2581,7 @@ mdb_txn_renew0(MDB_txn *txn)
                }
                txn->mt_dbxs = env->me_dbxs;    /* mostly static anyway */
        } else {
+               /* Not yet touching txn == env->me_txn0, it may be active */
                if (ti) {
                        LOCK_MUTEX_W(env);
 
@@ -2645,33 +2665,36 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
 {
        MDB_txn *txn;
        MDB_ntxn *ntxn;
-       int rc, size, tsize = sizeof(MDB_txn);
+       int rc, size, tsize;
+
+       flags &= MDB_TXN_BEGIN_FLAGS;
+       flags |= env->me_flags & MDB_WRITEMAP;
 
        if (env->me_flags & MDB_FATAL_ERROR) {
                DPUTS("environment had fatal error, must shutdown!");
                return MDB_PANIC;
        }
-       if ((env->me_flags & MDB_RDONLY) && !(flags & MDB_RDONLY))
+       if (env->me_flags & MDB_RDONLY & ~flags) /* write txn in RDONLY env */
                return EACCES;
+
+       size = tsize = sizeof(MDB_txn);
        if (parent) {
                /* Nested transactions: Max 1 child, write txns only, no writemap */
+               flags |= parent->mt_flags;
                if (parent->mt_child ||
-                       (flags & MDB_RDONLY) ||
-                       (parent->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_ERROR)) ||
-                       (env->me_flags & MDB_WRITEMAP))
+                       (flags & (MDB_RDONLY|MDB_WRITEMAP|MDB_TXN_ERROR)))
                {
                        return (parent->mt_flags & MDB_TXN_RDONLY) ? EINVAL : MDB_BAD_TXN;
                }
-               tsize = sizeof(MDB_ntxn);
-       }
-       size = tsize;
-       if (!(flags & MDB_RDONLY)) {
-               if (!parent) {
-                       txn = env->me_txn0;     /* just reuse preallocated write txn */
-                       goto ok;
-               }
-               /* child txns use own copy of cursors */
+               /* Child txns save MDB_pgstate and use own copy of cursors */
+               size = tsize = sizeof(MDB_ntxn);
                size += env->me_maxdbs * sizeof(MDB_cursor *);
+       } else if (!(flags & MDB_RDONLY)) {
+               /* Reuse preallocated write txn. However, do not touch it until
+                * mdb_txn_renew0() succeeds, since it currently may be active.
+                */
+               txn = env->me_txn0;
+               goto renew;
        }
        size += env->me_maxdbs * (sizeof(MDB_db)+1);
 
@@ -2681,7 +2704,6 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
        }
        txn->mt_dbs = (MDB_db *) ((char *)txn + tsize);
        if (flags & MDB_RDONLY) {
-               txn->mt_flags |= MDB_TXN_RDONLY;
                txn->mt_dbflags = (unsigned char *)(txn->mt_dbs + env->me_maxdbs);
                txn->mt_dbiseqs = env->me_dbiseqs;
        } else {
@@ -2694,9 +2716,9 @@ mdb_txn_begin(MDB_env *env, MDB_txn *parent, unsigned int flags, MDB_txn **ret)
                        txn->mt_dbflags = (unsigned char *)(txn->mt_dbiseqs + env->me_maxdbs);
                }
        }
+       txn->mt_flags = flags;
        txn->mt_env = env;
 
-ok:
        if (parent) {
                unsigned int i;
                txn->mt_u.dirty_list = malloc(sizeof(MDB_ID2)*MDB_IDL_UM_SIZE);
@@ -2715,7 +2737,6 @@ ok:
                parent->mt_child = txn;
                txn->mt_parent = parent;
                txn->mt_numdbs = parent->mt_numdbs;
-               txn->mt_flags = parent->mt_flags;
                txn->mt_dbxs = parent->mt_dbxs;
                memcpy(txn->mt_dbs, parent->mt_dbs, txn->mt_numdbs * sizeof(MDB_db));
                /* Copy parent's mt_dbflags, but clear DB_NEW */
@@ -2737,15 +2758,17 @@ ok:
                if (rc)
                        mdb_txn_reset0(txn, "beginchild-fail");
        } else {
+renew:
                rc = mdb_txn_renew0(txn);
        }
        if (rc) {
                if (txn != env->me_txn0)
                        free(txn);
        } else {
+               txn->mt_flags |= flags; /* for txn==me_txn0, no effect otherwise */
                *ret = txn;
                DPRINTF(("begin txn %"Z"u%c %p on mdbenv %p, root page %"Z"u",
-                       txn->mt_txnid, (txn->mt_flags & MDB_TXN_RDONLY) ? 'r' : 'w',
+                       txn->mt_txnid, (flags & MDB_RDONLY) ? 'r' : 'w',
                        (void *) txn, (void *) env, txn->mt_dbs[MAIN_DBI].md_root));
        }
 
@@ -2759,6 +2782,13 @@ mdb_txn_env(MDB_txn *txn)
        return txn->mt_env;
 }
 
+size_t
+mdb_txn_id(MDB_txn *txn)
+{
+    if(!txn) return 0;
+    return txn->mt_txnid;
+}
+
 /** Export or close DBI handles opened in this txn. */
 static void
 mdb_dbis_update(MDB_txn *txn, int keep)
@@ -3506,6 +3536,7 @@ mdb_env_read_header(MDB_env *env, MDB_meta *meta)
        return 0;
 }
 
+/** Fill in most of the zeroed #MDB_meta for an empty database environment */
 static void ESECT
 mdb_env_init_meta0(MDB_env *env, MDB_meta *meta)
 {
@@ -3522,7 +3553,7 @@ mdb_env_init_meta0(MDB_env *env, MDB_meta *meta)
 
 /** Write the environment parameters of a freshly created DB environment.
  * @param[in] env the environment handle
- * @param[out] meta address of where to store the meta information
+ * @param[in] meta the #MDB_meta to write
  * @return 0 on success, non-zero on failure.
  */
 static int ESECT
@@ -3550,9 +3581,10 @@ mdb_env_init_meta(MDB_env *env, MDB_meta *meta)
 
        psize = env->me_psize;
 
-       mdb_env_init_meta0(env, meta);
-
        p = calloc(2, psize);
+       if (!p)
+               return ENOMEM;
+
        p->mp_pgno = 0;
        p->mp_flags = P_META;
        *(MDB_meta *)METADATA(p) = *meta;
@@ -3582,6 +3614,7 @@ mdb_env_write_meta(MDB_txn *txn)
 {
        MDB_env *env;
        MDB_meta        meta, metab, *mp;
+       unsigned flags;
        size_t mapsize;
        off_t off;
        int rc, len, toggle;
@@ -3598,19 +3631,20 @@ mdb_env_write_meta(MDB_txn *txn)
                toggle, txn->mt_dbs[MAIN_DBI].md_root));
 
        env = txn->mt_env;
+       flags = env->me_flags;
        mp = env->me_metas[toggle];
        mapsize = env->me_metas[toggle ^ 1]->mm_mapsize;
        /* Persist any increases of mapsize config */
        if (mapsize < env->me_mapsize)
                mapsize = env->me_mapsize;
 
-       if (env->me_flags & MDB_WRITEMAP) {
+       if (flags & MDB_WRITEMAP) {
                mp->mm_mapsize = mapsize;
                mp->mm_dbs[0] = txn->mt_dbs[0];
                mp->mm_dbs[1] = txn->mt_dbs[1];
                mp->mm_last_pg = txn->mt_next_pgno - 1;
                mp->mm_txnid = txn->mt_txnid;
-               if (!(env->me_flags & (MDB_NOMETASYNC|MDB_NOSYNC))) {
+               if (!(flags & (MDB_NOMETASYNC|MDB_NOSYNC))) {
                        unsigned meta_size = env->me_psize;
                        rc = (env->me_flags & MDB_MAPASYNC) ? MS_ASYNC : MS_SYNC;
                        ptr = env->me_map;
@@ -3646,9 +3680,7 @@ mdb_env_write_meta(MDB_txn *txn)
        off += PAGEHDRSZ;
 
        /* Write to the SYNC fd */
-       mfd = env->me_flags & (MDB_NOSYNC|MDB_NOMETASYNC) ?
-               env->me_fd : env->me_mfd;
-retry_write:
+       mfd = (flags & (MDB_NOSYNC|MDB_NOMETASYNC)) ? env->me_fd : env->me_mfd;
 #ifdef _WIN32
        {
                memset(&ov, 0, sizeof(ov));
@@ -3657,12 +3689,15 @@ retry_write:
                        rc = -1;
        }
 #else
+retry_write:
        rc = pwrite(mfd, ptr, len, off);
 #endif
        if (rc != len) {
                rc = rc < 0 ? ErrCode() : EIO;
+#ifndef _WIN32
                if (rc == EINTR)
                        goto retry_write;
+#endif
                DPUTS("write failed, disk error?");
                /* On a failure, the pagecache still contains the new data.
                 * Write some old data back, to prevent it from being used.
@@ -3824,16 +3859,16 @@ mdb_env_set_mapsize(MDB_env *env, size_t size)
         */
        if (env->me_map) {
                int rc;
+               MDB_meta *meta;
                void *old;
                if (env->me_txn)
                        return EINVAL;
+               meta = env->me_metas[mdb_env_pick_meta(env)];
                if (!size)
-                       size = env->me_metas[mdb_env_pick_meta(env)]->mm_mapsize;
-               else if (size < env->me_mapsize) {
-                       /* If the configured size is smaller, make sure it's
-                        * still big enough. Silently round up to minimum if not.
-                        */
-                       size_t minsize = (env->me_metas[mdb_env_pick_meta(env)]->mm_last_pg + 1) * env->me_psize;
+                       size = meta->mm_mapsize;
+               {
+                       /* Silently round up to minimum if the size is too small */
+                       size_t minsize = (meta->mm_last_pg + 1) * env->me_psize;
                        if (size < minsize)
                                size = minsize;
                }
@@ -3968,8 +4003,6 @@ mdb_env_open2(MDB_env *env)
        }
 #endif
 
-       memset(&meta, 0, sizeof(meta));
-
        if ((i = mdb_env_read_header(env, &meta)) != 0) {
                if (i != ENOENT)
                        return i;
@@ -3978,24 +4011,40 @@ mdb_env_open2(MDB_env *env)
                env->me_psize = env->me_os_psize;
                if (env->me_psize > MAX_PAGESIZE)
                        env->me_psize = MAX_PAGESIZE;
+               memset(&meta, 0, sizeof(meta));
+               mdb_env_init_meta0(env, &meta);
+               meta.mm_mapsize = DEFAULT_MAPSIZE;
        } else {
                env->me_psize = meta.mm_psize;
        }
 
        /* Was a mapsize configured? */
        if (!env->me_mapsize) {
-               /* If this is a new environment, take the default,
-                * else use the size recorded in the existing env.
-                */
-               env->me_mapsize = newenv ? DEFAULT_MAPSIZE : meta.mm_mapsize;
-       } else if (env->me_mapsize < meta.mm_mapsize) {
-               /* If the configured size is smaller, make sure it's
-                * still big enough. Silently round up to minimum if not.
+               env->me_mapsize = meta.mm_mapsize;
+       }
+       {
+               /* Make sure mapsize >= committed data size.  Even when using
+                * mm_mapsize, which could be broken in old files (ITS#7789).
                 */
                size_t minsize = (meta.mm_last_pg + 1) * meta.mm_psize;
                if (env->me_mapsize < minsize)
                        env->me_mapsize = minsize;
        }
+       meta.mm_mapsize = env->me_mapsize;
+
+       if (newenv && !(flags & MDB_FIXEDMAP)) {
+               /* mdb_env_map() may grow the datafile.  Write the metapages
+                * first, so the file will be valid if initialization fails.
+                * Except with FIXEDMAP, since we do not yet know mm_address.
+                * We could fill in mm_address later, but then a different
+                * program might end up doing that - one with a memory layout
+                * and map address which does not suit the main program.
+                */
+               rc = mdb_env_init_meta(env, &meta);
+               if (rc)
+                       return rc;
+               newenv = 0;
+       }
 
        rc = mdb_env_map(env, (flags & MDB_FIXEDMAP) ? meta.mm_address : NULL);
        if (rc)
@@ -4637,15 +4686,13 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
                        if (rc)
                                goto leave;
                }
-               if (!((flags & MDB_RDONLY) ||
-                         (env->me_pbuf = calloc(1, env->me_psize))))
-                       rc = ENOMEM;
                if (!(flags & MDB_RDONLY)) {
                        MDB_txn *txn;
                        int tsize = sizeof(MDB_txn), size = tsize + env->me_maxdbs *
                                (sizeof(MDB_db)+sizeof(MDB_cursor *)+sizeof(unsigned int)+1);
-                       txn = calloc(1, size);
-                       if (txn) {
+                       if ((env->me_pbuf = calloc(1, env->me_psize)) &&
+                               (txn = calloc(1, size)))
+                       {
                                txn->mt_dbs = (MDB_db *)((char *)txn + tsize);
                                txn->mt_cursors = (MDB_cursor **)(txn->mt_dbs + env->me_maxdbs);
                                txn->mt_dbiseqs = (unsigned int *)(txn->mt_cursors + env->me_maxdbs);
@@ -4715,8 +4762,12 @@ mdb_env_close0(MDB_env *env, int excl)
                MDB_PID_T pid = env->me_pid;
                /* Clearing readers is done in this function because
                 * me_txkey with its destructor must be disabled first.
+                *
+                * We skip the the reader mutex, so we touch only
+                * data owned by this process (me_close_readers and
+                * our readers), and clear each reader atomically.
                 */
-               for (i = env->me_numreaders; --i >= 0; )
+               for (i = env->me_close_readers; --i >= 0; )
                        if (env->me_txns->mti_readers[i].mr_pid == pid)
                                env->me_txns->mti_readers[i].mr_pid = 0;
 #ifdef _WIN32
@@ -5037,7 +5088,7 @@ 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) | (env->me_flags & MDB_WRITEMAP))) {
+       if (! (txn->mt_flags & (MDB_TXN_RDONLY|MDB_TXN_WRITEMAP))) {
                MDB_txn *tx2 = txn;
                level = 1;
                do {
@@ -6105,7 +6156,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        enum { MDB_NO_ROOT = MDB_LAST_ERRCODE+10 }; /* internal code */
        MDB_env         *env;
        MDB_node        *leaf = NULL;
-       MDB_page        *fp, *mp;
+       MDB_page        *fp, *mp, *sub_root = NULL;
        uint16_t        fp_flags;
        MDB_val         xdata, *rdata, dkey, olddata;
        MDB_db dummy;
@@ -6385,6 +6436,7 @@ prep_subDB:
                                        offset = env->me_psize - olddata.mv_size;
                                        flags |= F_DUPDATA|F_SUBDATA;
                                        dummy.md_root = mp->mp_pgno;
+                                       sub_root = mp;
                        }
                        if (mp != fp) {
                                mp->mp_flags = fp_flags | P_DIRTY;
@@ -6531,7 +6583,7 @@ new_sub:
                 * DB are all zero size.
                 */
                if (do_sub) {
-                       int xflags;
+                       int xflags, new_dupdata;
                        size_t ecount;
 put_sub:
                        xdata.mv_size = 0;
@@ -6544,27 +6596,37 @@ put_sub:
                                xflags = (flags & MDB_NODUPDATA) ?
                                        MDB_NOOVERWRITE|MDB_NOSPILL : MDB_NOSPILL;
                        }
+                       if (sub_root)
+                               mc->mc_xcursor->mx_cursor.mc_pg[0] = sub_root;
+                       new_dupdata = (int)dkey.mv_size;
                        /* converted, write the original data first */
                        if (dkey.mv_size) {
                                rc = mdb_cursor_put(&mc->mc_xcursor->mx_cursor, &dkey, &xdata, xflags);
                                if (rc)
                                        goto bad_sub;
-                               {
-                                       /* Adjust other cursors pointing to mp */
-                                       MDB_cursor *m2;
-                                       unsigned i = mc->mc_top;
-                                       MDB_page *mp = mc->mc_pg[i];
-
-                                       for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
-                                               if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
-                                               if (!(m2->mc_flags & C_INITIALIZED)) continue;
-                                               if (m2->mc_pg[i] == mp && m2->mc_ki[i] == mc->mc_ki[i]) {
-                                                       mdb_xcursor_init1(m2, leaf);
+                               /* we've done our job */
+                               dkey.mv_size = 0;
+                       }
+                       if (!(leaf->mn_flags & F_SUBDATA) || sub_root) {
+                               /* Adjust other cursors pointing to mp */
+                               MDB_cursor *m2;
+                               MDB_xcursor *mx = mc->mc_xcursor;
+                               unsigned i = mc->mc_top;
+                               MDB_page *mp = mc->mc_pg[i];
+
+                               for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
+                                       if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
+                                       if (!(m2->mc_flags & C_INITIALIZED)) continue;
+                                       if (m2->mc_pg[i] == mp) {
+                                               if (m2->mc_ki[i] == mc->mc_ki[i]) {
+                                                       mdb_xcursor_init2(m2, mx, new_dupdata);
+                                               } else if (!insert_key) {
+                                                       MDB_node *n2 = NODEPTR(mp, m2->mc_ki[i]);
+                                                       if (!(n2->mn_flags & F_SUBDATA))
+                                                               m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
                                                }
                                        }
                                }
-                               /* we've done our job */
-                               dkey.mv_size = 0;
                        }
                        ecount = mc->mc_xcursor->mx_db.md_entries;
                        if (flags & MDB_APPENDDUP)
@@ -6660,12 +6722,19 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
                                        mdb_node_shrink(mp, mc->mc_ki[mc->mc_top]);
                                        leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
                                        mc->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
-                                       /* fix other sub-DB cursors pointed at this fake page */
+                                       /* fix other sub-DB cursors pointed at fake pages on this page */
                                        for (m2 = mc->mc_txn->mt_cursors[mc->mc_dbi]; m2; m2=m2->mc_next) {
                                                if (m2 == mc || m2->mc_snum < mc->mc_snum) continue;
-                                               if (m2->mc_pg[mc->mc_top] == mp &&
-                                                       m2->mc_ki[mc->mc_top] == mc->mc_ki[mc->mc_top])
-                                                       m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
+                                               if (!(m2->mc_flags & C_INITIALIZED)) continue;
+                                               if (m2->mc_pg[mc->mc_top] == mp) {
+                                                       if (m2->mc_ki[mc->mc_top] == mc->mc_ki[mc->mc_top]) {
+                                                               m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(leaf);
+                                                       } else {
+                                                               MDB_node *n2 = NODEPTR(mp, m2->mc_ki[mc->mc_top]);
+                                                               if (!(n2->mn_flags & F_SUBDATA))
+                                                                       m2->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(n2);
+                                                       }
+                                               }
                                        }
                                }
                                mc->mc_db->md_entries--;
@@ -7088,7 +7157,7 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
                mx->mx_cursor.mc_flags = C_SUB;
        } else {
                MDB_page *fp = NODEDATA(node);
-               mx->mx_db.md_pad = mc->mc_pg[mc->mc_top]->mp_pad;
+               mx->mx_db.md_pad = 0;
                mx->mx_db.md_flags = 0;
                mx->mx_db.md_depth = 1;
                mx->mx_db.md_branch_pages = 0;
@@ -7117,6 +7186,38 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
 #endif
 }
 
+
+/** Fixup a sorted-dups cursor due to underlying update.
+ *     Sets up some fields that depend on the data from the main cursor.
+ *     Almost the same as init1, but skips initialization steps if the
+ *     xcursor had already been used.
+ * @param[in] mc The main cursor whose sorted-dups cursor is to be fixed up.
+ * @param[in] src_mx The xcursor of an up-to-date cursor.
+ * @param[in] new_dupdata True if converting from a non-#F_DUPDATA item.
+ */
+static void
+mdb_xcursor_init2(MDB_cursor *mc, MDB_xcursor *src_mx, int new_dupdata)
+{
+       MDB_xcursor *mx = mc->mc_xcursor;
+
+       if (new_dupdata) {
+               mx->mx_cursor.mc_snum = 1;
+               mx->mx_cursor.mc_top = 0;
+               mx->mx_cursor.mc_flags |= C_INITIALIZED;
+               mx->mx_cursor.mc_ki[0] = 0;
+               mx->mx_dbflag = DB_VALID|DB_USRVALID|DB_DIRTY; /* DB_DIRTY guides mdb_cursor_touch */
+#if UINT_MAX < SIZE_MAX
+               mx->mx_dbx.md_cmp = src_mx->mx_dbx.md_cmp;
+#endif
+       } else if (!(mx->mx_cursor.mc_flags & C_INITIALIZED)) {
+               return;
+       }
+       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 %"Z"u", mx->mx_cursor.mc_dbi,
+               mx->mx_db.md_root));
+}
+
 /** Initialize a cursor for a given transaction and database. */
 static void
 mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
@@ -7435,8 +7536,22 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                /* Adjust other cursors pointing to mp */
                MDB_cursor *m2, *m3;
                MDB_dbi dbi = csrc->mc_dbi;
-               MDB_page *mp = csrc->mc_pg[csrc->mc_top];
+               MDB_page *mp;
+
+               mp = cdst->mc_pg[csrc->mc_top];
+               for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+                       if (csrc->mc_flags & C_SUB)
+                               m3 = &m2->mc_xcursor->mx_cursor;
+                       else
+                               m3 = m2;
+                       if (m3 == cdst) continue;
+                       if (m3->mc_pg[csrc->mc_top] == mp && m3->mc_ki[csrc->mc_top] >=
+                               cdst->mc_ki[csrc->mc_top]) {
+                               m3->mc_ki[csrc->mc_top]++;
+                       }
+               }
 
+               mp = csrc->mc_pg[csrc->mc_top];
                for (m2 = csrc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
                        if (csrc->mc_flags & C_SUB)
                                m3 = &m2->mc_xcursor->mx_cursor;
@@ -7638,9 +7753,9 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                uint16_t depth = cdst->mc_db->md_depth;
                mdb_cursor_pop(cdst);
                rc = mdb_rebalance(cdst);
-               /* Did the tree shrink? */
-               if (depth > cdst->mc_db->md_depth)
-                       snum--;
+               /* Did the tree height change? */
+               if (depth != cdst->mc_db->md_depth)
+                       snum += cdst->mc_db->md_depth - depth;
                cdst->mc_snum = snum;
                cdst->mc_top = snum-1;
        }
@@ -7680,17 +7795,23 @@ mdb_rebalance(MDB_cursor *mc)
 {
        MDB_node        *node;
        int rc;
-       unsigned int ptop, minkeys;
+       unsigned int ptop, minkeys, thresh;
        MDB_cursor      mn;
        indx_t oldki;
 
-       minkeys = 1 + (IS_BRANCH(mc->mc_pg[mc->mc_top]));
+       if (IS_BRANCH(mc->mc_pg[mc->mc_top])) {
+               minkeys = 2;
+               thresh = 1;
+       } else {
+               minkeys = 1;
+               thresh = FILL_THRESHOLD;
+       }
        DPRINTF(("rebalancing %s page %"Z"u (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]) >= FILL_THRESHOLD &&
+       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 %"Z"u, above fill threshold",
                    mdb_dbg_pgno(mc->mc_pg[mc->mc_top])));
@@ -7824,10 +7945,9 @@ mdb_rebalance(MDB_cursor *mc)
         * move one key from it. Otherwise we should try to merge them.
         * (A branch page must never have less than 2 keys.)
         */
-       minkeys = 1 + (IS_BRANCH(mn.mc_pg[mn.mc_top]));
-       if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= FILL_THRESHOLD && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys) {
+       if (PAGEFILL(mc->mc_txn->mt_env, mn.mc_pg[mn.mc_top]) >= thresh && NUMKEYS(mn.mc_pg[mn.mc_top]) > minkeys) {
                rc = mdb_node_move(&mn, mc);
-               if (mc->mc_ki[ptop]) {
+               if (mc->mc_ki[mc->mc_top-1]) {
                        oldki++;
                }
        } else {
@@ -7867,16 +7987,35 @@ mdb_cursor_del0(MDB_cursor *mc)
        MDB_page *mp;
        indx_t ki;
        unsigned int nkeys;
+       MDB_cursor *m2, *m3;
+       MDB_dbi dbi = mc->mc_dbi;
 
        ki = mc->mc_ki[mc->mc_top];
+       mp = mc->mc_pg[mc->mc_top];
        mdb_node_del(mc, mc->mc_db->md_pad);
        mc->mc_db->md_entries--;
+       {
+               /* Adjust other cursors pointing to mp */
+               for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+                       m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
+                       if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
+                               continue;
+                       if (m3 == mc || m3->mc_snum < mc->mc_snum)
+                               continue;
+                       if (m3->mc_pg[mc->mc_top] == mp) {
+                               if (m3->mc_ki[mc->mc_top] >= ki) {
+                                       m3->mc_flags |= C_DEL;
+                                       if (m3->mc_ki[mc->mc_top] > ki)
+                                               m3->mc_ki[mc->mc_top]--;
+                                       else if (mc->mc_db->md_flags & MDB_DUPSORT)
+                                               m3->mc_xcursor->mx_cursor.mc_flags |= C_EOF;
+                               }
+                       }
+               }
+       }
        rc = mdb_rebalance(mc);
 
        if (rc == MDB_SUCCESS) {
-               MDB_cursor *m2, *m3;
-               MDB_dbi dbi = mc->mc_dbi;
-
                /* DB is totally empty now, just bail out.
                 * Other cursors adjustments were already done
                 * by mdb_rebalance and aren't needed here.
@@ -7887,30 +8026,15 @@ mdb_cursor_del0(MDB_cursor *mc)
                mp = mc->mc_pg[mc->mc_top];
                nkeys = NUMKEYS(mp);
 
-               /* if mc points past last node in page, find next sibling */
-               if (mc->mc_ki[mc->mc_top] >= nkeys) {
-                       rc = mdb_cursor_sibling(mc, 1);
-                       if (rc == MDB_NOTFOUND) {
-                               mc->mc_flags |= C_EOF;
-                               rc = MDB_SUCCESS;
-                       }
-               }
-
                /* Adjust other cursors pointing to mp */
                for (m2 = mc->mc_txn->mt_cursors[dbi]; !rc && m2; m2=m2->mc_next) {
                        m3 = (mc->mc_flags & C_SUB) ? &m2->mc_xcursor->mx_cursor : m2;
                        if (! (m2->mc_flags & m3->mc_flags & C_INITIALIZED))
                                continue;
-                       if (m3 == mc || m3->mc_snum < mc->mc_snum)
+                       if (m3->mc_snum < mc->mc_snum)
                                continue;
                        if (m3->mc_pg[mc->mc_top] == mp) {
-                               if (m3->mc_ki[mc->mc_top] >= ki) {
-                                       m3->mc_flags |= C_DEL;
-                                       if (m3->mc_ki[mc->mc_top] > ki)
-                                               m3->mc_ki[mc->mc_top]--;
-                                       else if (mc->mc_db->md_flags & MDB_DUPSORT)
-                                               m3->mc_xcursor->mx_cursor.mc_flags |= C_EOF;
-                               }
+                               /* if m3 points past last node in page, find next sibling */
                                if (m3->mc_ki[mc->mc_top] >= nkeys) {
                                        rc = mdb_cursor_sibling(m3, 1);
                                        if (rc == MDB_NOTFOUND) {
@@ -8028,6 +8152,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
        /* Create a right sibling. */
        if ((rc = mdb_page_new(mc, mp->mp_flags, 1, &rp)))
                return rc;
+       rp->mp_pad = mp->mp_pad;
        DPRINTF(("new right sibling: page %"Z"u", rp->mp_pgno));
 
        if (mc->mc_snum < 2) {
@@ -8040,8 +8165,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                mc->mc_ki[0] = 0;
                mc->mc_db->md_root = pp->mp_pgno;
                DPRINTF(("root split! new root = %"Z"u", pp->mp_pgno));
-               mc->mc_db->md_depth++;
-               new_root = 1;
+               new_root = mc->mc_db->md_depth++;
 
                /* Add left (implicit) pointer. */
                if ((rc = mdb_node_add(mc, 0, NULL, NULL, mp->mp_pgno, 0)) != MDB_SUCCESS) {
@@ -8162,7 +8286,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                                psize = 0;
                                if (newindx <= split_indx || newindx >= nkeys) {
                                        i = 0; j = 1;
-                                       k = newindx >= nkeys ? nkeys : split_indx+2;
+                                       k = newindx >= nkeys ? nkeys : split_indx+1+IS_LEAF(mp);
                                } else {
                                        i = nkeys; j = -1;
                                        k = split_indx-1;
@@ -8354,7 +8478,7 @@ mdb_page_split(MDB_cursor *mc, MDB_val *newkey, MDB_val *newdata, pgno_t newpgno
                        if (new_root) {
                                int k;
                                /* root split */
-                               for (k=m3->mc_top; k>=0; k--) {
+                               for (k=new_root; k>=0; k--) {
                                        m3->mc_ki[k+1] = m3->mc_ki[k];
                                        m3->mc_pg[k+1] = m3->mc_pg[k];
                                }
@@ -8431,7 +8555,7 @@ typedef struct mdb_copy {
 } mdb_copy;
 
        /** Dedicated writer thread for compacting copy. */
-static THREAD_RET ESECT
+static THREAD_RET ESECT CALL_CONV
 mdb_env_copythr(void *arg)
 {
        mdb_copy *my = arg;
@@ -9077,11 +9201,7 @@ mdb_env_info(MDB_env *env, MDB_envinfo *arg)
        arg->me_mapaddr = env->me_metas[toggle]->mm_address;
        arg->me_mapsize = env->me_mapsize;
        arg->me_maxreaders = env->me_maxreaders;
-
-       /* me_numreaders may be zero if this process never used any readers. Use
-        * the shared numreader count if it exists.
-        */
-       arg->me_numreaders = env->me_txns ? env->me_txns->mti_numreaders : env->me_numreaders;
+       arg->me_numreaders = env->me_txns ? env->me_txns->mti_numreaders : 0;
 
        arg->me_last_pgno = env->me_metas[toggle]->mm_last_pg;
        arg->me_last_txnid = env->me_metas[toggle]->mm_txnid;